mem2mem_testdev.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*
  2. * A virtual v4l2-mem2mem example device.
  3. *
  4. * This is a virtual device driver for testing mem-to-mem videobuf framework.
  5. * It simulates a device that uses memory buffers for both source and
  6. * destination, processes the data and issues an "irq" (simulated by a timer).
  7. * The device is capable of multi-instance, multi-buffer-per-transaction
  8. * operation (via the mem2mem framework).
  9. *
  10. * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
  11. * Pawel Osciak, <pawel@osciak.com>
  12. * Marek Szyprowski, <m.szyprowski@samsung.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the
  17. * License, or (at your option) any later version
  18. */
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <linux/fs.h>
  22. #include <linux/timer.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/platform_device.h>
  26. #include <media/v4l2-mem2mem.h>
  27. #include <media/v4l2-device.h>
  28. #include <media/v4l2-ioctl.h>
  29. #include <media/v4l2-ctrls.h>
  30. #include <media/v4l2-event.h>
  31. #include <media/videobuf2-vmalloc.h>
  32. #define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
  33. MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
  34. MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  35. MODULE_LICENSE("GPL");
  36. MODULE_VERSION("0.1.1");
  37. static unsigned debug;
  38. module_param(debug, uint, 0644);
  39. MODULE_PARM_DESC(debug, "activates debug info");
  40. #define MIN_W 32
  41. #define MIN_H 32
  42. #define MAX_W 640
  43. #define MAX_H 480
  44. #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
  45. /* Flags that indicate a format can be used for capture/output */
  46. #define MEM2MEM_CAPTURE (1 << 0)
  47. #define MEM2MEM_OUTPUT (1 << 1)
  48. #define MEM2MEM_NAME "m2m-testdev"
  49. /* Per queue */
  50. #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
  51. /* In bytes, per queue */
  52. #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
  53. /* Default transaction time in msec */
  54. #define MEM2MEM_DEF_TRANSTIME 40
  55. #define MEM2MEM_COLOR_STEP (0xff >> 4)
  56. #define MEM2MEM_NUM_TILES 8
  57. /* Flags that indicate processing mode */
  58. #define MEM2MEM_HFLIP (1 << 0)
  59. #define MEM2MEM_VFLIP (1 << 1)
  60. #define dprintk(dev, fmt, arg...) \
  61. v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
  62. static void m2mtest_dev_release(struct device *dev)
  63. {}
  64. static struct platform_device m2mtest_pdev = {
  65. .name = MEM2MEM_NAME,
  66. .dev.release = m2mtest_dev_release,
  67. };
  68. struct m2mtest_fmt {
  69. char *name;
  70. u32 fourcc;
  71. int depth;
  72. /* Types the format can be used for */
  73. u32 types;
  74. };
  75. static struct m2mtest_fmt formats[] = {
  76. {
  77. .name = "RGB565 (BE)",
  78. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  79. .depth = 16,
  80. /* Both capture and output format */
  81. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  82. },
  83. {
  84. .name = "4:2:2, packed, YUYV",
  85. .fourcc = V4L2_PIX_FMT_YUYV,
  86. .depth = 16,
  87. /* Output-only format */
  88. .types = MEM2MEM_OUTPUT,
  89. },
  90. };
  91. #define NUM_FORMATS ARRAY_SIZE(formats)
  92. /* Per-queue, driver-specific private data */
  93. struct m2mtest_q_data {
  94. unsigned int width;
  95. unsigned int height;
  96. unsigned int sizeimage;
  97. unsigned int sequence;
  98. struct m2mtest_fmt *fmt;
  99. };
  100. enum {
  101. V4L2_M2M_SRC = 0,
  102. V4L2_M2M_DST = 1,
  103. };
  104. #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
  105. #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
  106. static struct m2mtest_fmt *find_format(struct v4l2_format *f)
  107. {
  108. struct m2mtest_fmt *fmt;
  109. unsigned int k;
  110. for (k = 0; k < NUM_FORMATS; k++) {
  111. fmt = &formats[k];
  112. if (fmt->fourcc == f->fmt.pix.pixelformat)
  113. break;
  114. }
  115. if (k == NUM_FORMATS)
  116. return NULL;
  117. return &formats[k];
  118. }
  119. struct m2mtest_dev {
  120. struct v4l2_device v4l2_dev;
  121. struct video_device *vfd;
  122. atomic_t num_inst;
  123. struct mutex dev_mutex;
  124. spinlock_t irqlock;
  125. struct timer_list timer;
  126. struct v4l2_m2m_dev *m2m_dev;
  127. };
  128. struct m2mtest_ctx {
  129. struct v4l2_fh fh;
  130. struct m2mtest_dev *dev;
  131. struct v4l2_ctrl_handler hdl;
  132. /* Processed buffers in this transaction */
  133. u8 num_processed;
  134. /* Transaction length (i.e. how many buffers per transaction) */
  135. u32 translen;
  136. /* Transaction time (i.e. simulated processing time) in milliseconds */
  137. u32 transtime;
  138. /* Abort requested by m2m */
  139. int aborting;
  140. /* Processing mode */
  141. int mode;
  142. enum v4l2_colorspace colorspace;
  143. /* Source and destination queue data */
  144. struct m2mtest_q_data q_data[2];
  145. };
  146. static inline struct m2mtest_ctx *file2ctx(struct file *file)
  147. {
  148. return container_of(file->private_data, struct m2mtest_ctx, fh);
  149. }
  150. static struct m2mtest_q_data *get_q_data(struct m2mtest_ctx *ctx,
  151. enum v4l2_buf_type type)
  152. {
  153. switch (type) {
  154. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  155. return &ctx->q_data[V4L2_M2M_SRC];
  156. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  157. return &ctx->q_data[V4L2_M2M_DST];
  158. default:
  159. BUG();
  160. }
  161. return NULL;
  162. }
  163. static int device_process(struct m2mtest_ctx *ctx,
  164. struct vb2_buffer *in_vb,
  165. struct vb2_buffer *out_vb)
  166. {
  167. struct m2mtest_dev *dev = ctx->dev;
  168. struct m2mtest_q_data *q_data;
  169. u8 *p_in, *p_out;
  170. int x, y, t, w;
  171. int tile_w, bytes_left;
  172. int width, height, bytesperline;
  173. q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  174. width = q_data->width;
  175. height = q_data->height;
  176. bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  177. p_in = vb2_plane_vaddr(in_vb, 0);
  178. p_out = vb2_plane_vaddr(out_vb, 0);
  179. if (!p_in || !p_out) {
  180. v4l2_err(&dev->v4l2_dev,
  181. "Acquiring kernel pointers to buffers failed\n");
  182. return -EFAULT;
  183. }
  184. if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
  185. v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
  186. return -EINVAL;
  187. }
  188. tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
  189. / MEM2MEM_NUM_TILES;
  190. bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
  191. w = 0;
  192. out_vb->v4l2_buf.sequence = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
  193. in_vb->v4l2_buf.sequence = q_data->sequence++;
  194. memcpy(&out_vb->v4l2_buf.timestamp,
  195. &in_vb->v4l2_buf.timestamp,
  196. sizeof(struct timeval));
  197. if (in_vb->v4l2_buf.flags & V4L2_BUF_FLAG_TIMECODE)
  198. memcpy(&out_vb->v4l2_buf.timecode, &in_vb->v4l2_buf.timecode,
  199. sizeof(struct v4l2_timecode));
  200. out_vb->v4l2_buf.field = in_vb->v4l2_buf.field;
  201. out_vb->v4l2_buf.flags = in_vb->v4l2_buf.flags &
  202. (V4L2_BUF_FLAG_TIMECODE |
  203. V4L2_BUF_FLAG_KEYFRAME |
  204. V4L2_BUF_FLAG_PFRAME |
  205. V4L2_BUF_FLAG_BFRAME |
  206. V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
  207. switch (ctx->mode) {
  208. case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
  209. p_out += bytesperline * height - bytes_left;
  210. for (y = 0; y < height; ++y) {
  211. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  212. if (w & 0x1) {
  213. for (x = 0; x < tile_w; ++x)
  214. *--p_out = *p_in++ +
  215. MEM2MEM_COLOR_STEP;
  216. } else {
  217. for (x = 0; x < tile_w; ++x)
  218. *--p_out = *p_in++ -
  219. MEM2MEM_COLOR_STEP;
  220. }
  221. ++w;
  222. }
  223. p_in += bytes_left;
  224. p_out -= bytes_left;
  225. }
  226. break;
  227. case MEM2MEM_HFLIP:
  228. for (y = 0; y < height; ++y) {
  229. p_out += MEM2MEM_NUM_TILES * tile_w;
  230. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  231. if (w & 0x01) {
  232. for (x = 0; x < tile_w; ++x)
  233. *--p_out = *p_in++ +
  234. MEM2MEM_COLOR_STEP;
  235. } else {
  236. for (x = 0; x < tile_w; ++x)
  237. *--p_out = *p_in++ -
  238. MEM2MEM_COLOR_STEP;
  239. }
  240. ++w;
  241. }
  242. p_in += bytes_left;
  243. p_out += bytesperline;
  244. }
  245. break;
  246. case MEM2MEM_VFLIP:
  247. p_out += bytesperline * (height - 1);
  248. for (y = 0; y < height; ++y) {
  249. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  250. if (w & 0x1) {
  251. for (x = 0; x < tile_w; ++x)
  252. *p_out++ = *p_in++ +
  253. MEM2MEM_COLOR_STEP;
  254. } else {
  255. for (x = 0; x < tile_w; ++x)
  256. *p_out++ = *p_in++ -
  257. MEM2MEM_COLOR_STEP;
  258. }
  259. ++w;
  260. }
  261. p_in += bytes_left;
  262. p_out += bytes_left - 2 * bytesperline;
  263. }
  264. break;
  265. default:
  266. for (y = 0; y < height; ++y) {
  267. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  268. if (w & 0x1) {
  269. for (x = 0; x < tile_w; ++x)
  270. *p_out++ = *p_in++ +
  271. MEM2MEM_COLOR_STEP;
  272. } else {
  273. for (x = 0; x < tile_w; ++x)
  274. *p_out++ = *p_in++ -
  275. MEM2MEM_COLOR_STEP;
  276. }
  277. ++w;
  278. }
  279. p_in += bytes_left;
  280. p_out += bytes_left;
  281. }
  282. }
  283. return 0;
  284. }
  285. static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
  286. {
  287. dprintk(dev, "Scheduling a simulated irq\n");
  288. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
  289. }
  290. /*
  291. * mem2mem callbacks
  292. */
  293. /**
  294. * job_ready() - check whether an instance is ready to be scheduled to run
  295. */
  296. static int job_ready(void *priv)
  297. {
  298. struct m2mtest_ctx *ctx = priv;
  299. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
  300. || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
  301. dprintk(ctx->dev, "Not enough buffers available\n");
  302. return 0;
  303. }
  304. return 1;
  305. }
  306. static void job_abort(void *priv)
  307. {
  308. struct m2mtest_ctx *ctx = priv;
  309. /* Will cancel the transaction in the next interrupt handler */
  310. ctx->aborting = 1;
  311. }
  312. /* device_run() - prepares and starts the device
  313. *
  314. * This simulates all the immediate preparations required before starting
  315. * a device. This will be called by the framework when it decides to schedule
  316. * a particular instance.
  317. */
  318. static void device_run(void *priv)
  319. {
  320. struct m2mtest_ctx *ctx = priv;
  321. struct m2mtest_dev *dev = ctx->dev;
  322. struct vb2_buffer *src_buf, *dst_buf;
  323. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  324. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  325. device_process(ctx, src_buf, dst_buf);
  326. /* Run a timer, which simulates a hardware irq */
  327. schedule_irq(dev, ctx->transtime);
  328. }
  329. static void device_isr(unsigned long priv)
  330. {
  331. struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
  332. struct m2mtest_ctx *curr_ctx;
  333. struct vb2_buffer *src_vb, *dst_vb;
  334. unsigned long flags;
  335. curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
  336. if (NULL == curr_ctx) {
  337. pr_err("Instance released before the end of transaction\n");
  338. return;
  339. }
  340. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  341. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  342. curr_ctx->num_processed++;
  343. spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
  344. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  345. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  346. spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
  347. if (curr_ctx->num_processed == curr_ctx->translen
  348. || curr_ctx->aborting) {
  349. dprintk(curr_ctx->dev, "Finishing transaction\n");
  350. curr_ctx->num_processed = 0;
  351. v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
  352. } else {
  353. device_run(curr_ctx);
  354. }
  355. }
  356. /*
  357. * video ioctls
  358. */
  359. static int vidioc_querycap(struct file *file, void *priv,
  360. struct v4l2_capability *cap)
  361. {
  362. strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
  363. strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
  364. snprintf(cap->bus_info, sizeof(cap->bus_info),
  365. "platform:%s", MEM2MEM_NAME);
  366. cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  367. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  368. return 0;
  369. }
  370. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  371. {
  372. int i, num;
  373. struct m2mtest_fmt *fmt;
  374. num = 0;
  375. for (i = 0; i < NUM_FORMATS; ++i) {
  376. if (formats[i].types & type) {
  377. /* index-th format of type type found ? */
  378. if (num == f->index)
  379. break;
  380. /* Correct type but haven't reached our index yet,
  381. * just increment per-type index */
  382. ++num;
  383. }
  384. }
  385. if (i < NUM_FORMATS) {
  386. /* Format found */
  387. fmt = &formats[i];
  388. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  389. f->pixelformat = fmt->fourcc;
  390. return 0;
  391. }
  392. /* Format not found */
  393. return -EINVAL;
  394. }
  395. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  396. struct v4l2_fmtdesc *f)
  397. {
  398. return enum_fmt(f, MEM2MEM_CAPTURE);
  399. }
  400. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  401. struct v4l2_fmtdesc *f)
  402. {
  403. return enum_fmt(f, MEM2MEM_OUTPUT);
  404. }
  405. static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  406. {
  407. struct vb2_queue *vq;
  408. struct m2mtest_q_data *q_data;
  409. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  410. if (!vq)
  411. return -EINVAL;
  412. q_data = get_q_data(ctx, f->type);
  413. f->fmt.pix.width = q_data->width;
  414. f->fmt.pix.height = q_data->height;
  415. f->fmt.pix.field = V4L2_FIELD_NONE;
  416. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  417. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  418. f->fmt.pix.sizeimage = q_data->sizeimage;
  419. f->fmt.pix.colorspace = ctx->colorspace;
  420. return 0;
  421. }
  422. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  423. struct v4l2_format *f)
  424. {
  425. return vidioc_g_fmt(file2ctx(file), f);
  426. }
  427. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  428. struct v4l2_format *f)
  429. {
  430. return vidioc_g_fmt(file2ctx(file), f);
  431. }
  432. static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
  433. {
  434. /* V4L2 specification suggests the driver corrects the format struct
  435. * if any of the dimensions is unsupported */
  436. if (f->fmt.pix.height < MIN_H)
  437. f->fmt.pix.height = MIN_H;
  438. else if (f->fmt.pix.height > MAX_H)
  439. f->fmt.pix.height = MAX_H;
  440. if (f->fmt.pix.width < MIN_W)
  441. f->fmt.pix.width = MIN_W;
  442. else if (f->fmt.pix.width > MAX_W)
  443. f->fmt.pix.width = MAX_W;
  444. f->fmt.pix.width &= ~DIM_ALIGN_MASK;
  445. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  446. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  447. f->fmt.pix.field = V4L2_FIELD_NONE;
  448. return 0;
  449. }
  450. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  451. struct v4l2_format *f)
  452. {
  453. struct m2mtest_fmt *fmt;
  454. struct m2mtest_ctx *ctx = file2ctx(file);
  455. fmt = find_format(f);
  456. if (!fmt) {
  457. f->fmt.pix.pixelformat = formats[0].fourcc;
  458. fmt = find_format(f);
  459. }
  460. if (!(fmt->types & MEM2MEM_CAPTURE)) {
  461. v4l2_err(&ctx->dev->v4l2_dev,
  462. "Fourcc format (0x%08x) invalid.\n",
  463. f->fmt.pix.pixelformat);
  464. return -EINVAL;
  465. }
  466. f->fmt.pix.colorspace = ctx->colorspace;
  467. return vidioc_try_fmt(f, fmt);
  468. }
  469. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  470. struct v4l2_format *f)
  471. {
  472. struct m2mtest_fmt *fmt;
  473. struct m2mtest_ctx *ctx = file2ctx(file);
  474. fmt = find_format(f);
  475. if (!fmt) {
  476. f->fmt.pix.pixelformat = formats[0].fourcc;
  477. fmt = find_format(f);
  478. }
  479. if (!(fmt->types & MEM2MEM_OUTPUT)) {
  480. v4l2_err(&ctx->dev->v4l2_dev,
  481. "Fourcc format (0x%08x) invalid.\n",
  482. f->fmt.pix.pixelformat);
  483. return -EINVAL;
  484. }
  485. if (!f->fmt.pix.colorspace)
  486. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  487. return vidioc_try_fmt(f, fmt);
  488. }
  489. static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  490. {
  491. struct m2mtest_q_data *q_data;
  492. struct vb2_queue *vq;
  493. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  494. if (!vq)
  495. return -EINVAL;
  496. q_data = get_q_data(ctx, f->type);
  497. if (!q_data)
  498. return -EINVAL;
  499. if (vb2_is_busy(vq)) {
  500. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  501. return -EBUSY;
  502. }
  503. q_data->fmt = find_format(f);
  504. q_data->width = f->fmt.pix.width;
  505. q_data->height = f->fmt.pix.height;
  506. q_data->sizeimage = q_data->width * q_data->height
  507. * q_data->fmt->depth >> 3;
  508. dprintk(ctx->dev,
  509. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  510. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  511. return 0;
  512. }
  513. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  514. struct v4l2_format *f)
  515. {
  516. int ret;
  517. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  518. if (ret)
  519. return ret;
  520. return vidioc_s_fmt(file2ctx(file), f);
  521. }
  522. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  523. struct v4l2_format *f)
  524. {
  525. struct m2mtest_ctx *ctx = file2ctx(file);
  526. int ret;
  527. ret = vidioc_try_fmt_vid_out(file, priv, f);
  528. if (ret)
  529. return ret;
  530. ret = vidioc_s_fmt(file2ctx(file), f);
  531. if (!ret)
  532. ctx->colorspace = f->fmt.pix.colorspace;
  533. return ret;
  534. }
  535. static int m2mtest_s_ctrl(struct v4l2_ctrl *ctrl)
  536. {
  537. struct m2mtest_ctx *ctx =
  538. container_of(ctrl->handler, struct m2mtest_ctx, hdl);
  539. switch (ctrl->id) {
  540. case V4L2_CID_HFLIP:
  541. if (ctrl->val)
  542. ctx->mode |= MEM2MEM_HFLIP;
  543. else
  544. ctx->mode &= ~MEM2MEM_HFLIP;
  545. break;
  546. case V4L2_CID_VFLIP:
  547. if (ctrl->val)
  548. ctx->mode |= MEM2MEM_VFLIP;
  549. else
  550. ctx->mode &= ~MEM2MEM_VFLIP;
  551. break;
  552. case V4L2_CID_TRANS_TIME_MSEC:
  553. ctx->transtime = ctrl->val;
  554. break;
  555. case V4L2_CID_TRANS_NUM_BUFS:
  556. ctx->translen = ctrl->val;
  557. break;
  558. default:
  559. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  560. return -EINVAL;
  561. }
  562. return 0;
  563. }
  564. static const struct v4l2_ctrl_ops m2mtest_ctrl_ops = {
  565. .s_ctrl = m2mtest_s_ctrl,
  566. };
  567. static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
  568. .vidioc_querycap = vidioc_querycap,
  569. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  570. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  571. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  572. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  573. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  574. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  575. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  576. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  577. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  578. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  579. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  580. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  581. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  582. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  583. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  584. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  585. };
  586. /*
  587. * Queue operations
  588. */
  589. static int m2mtest_queue_setup(struct vb2_queue *vq,
  590. const struct v4l2_format *fmt,
  591. unsigned int *nbuffers, unsigned int *nplanes,
  592. unsigned int sizes[], void *alloc_ctxs[])
  593. {
  594. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
  595. struct m2mtest_q_data *q_data;
  596. unsigned int size, count = *nbuffers;
  597. q_data = get_q_data(ctx, vq->type);
  598. size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  599. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  600. (count)--;
  601. *nplanes = 1;
  602. *nbuffers = count;
  603. sizes[0] = size;
  604. /*
  605. * videobuf2-vmalloc allocator is context-less so no need to set
  606. * alloc_ctxs array.
  607. */
  608. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  609. return 0;
  610. }
  611. static int m2mtest_buf_prepare(struct vb2_buffer *vb)
  612. {
  613. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  614. struct m2mtest_q_data *q_data;
  615. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  616. q_data = get_q_data(ctx, vb->vb2_queue->type);
  617. if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  618. if (vb->v4l2_buf.field == V4L2_FIELD_ANY)
  619. vb->v4l2_buf.field = V4L2_FIELD_NONE;
  620. if (vb->v4l2_buf.field != V4L2_FIELD_NONE) {
  621. dprintk(ctx->dev, "%s field isn't supported\n",
  622. __func__);
  623. return -EINVAL;
  624. }
  625. }
  626. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  627. dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
  628. __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
  629. return -EINVAL;
  630. }
  631. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  632. return 0;
  633. }
  634. static void m2mtest_buf_queue(struct vb2_buffer *vb)
  635. {
  636. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  637. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
  638. }
  639. static int m2mtest_start_streaming(struct vb2_queue *q, unsigned count)
  640. {
  641. struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
  642. struct m2mtest_q_data *q_data = get_q_data(ctx, q->type);
  643. q_data->sequence = 0;
  644. return 0;
  645. }
  646. static void m2mtest_stop_streaming(struct vb2_queue *q)
  647. {
  648. struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
  649. struct vb2_buffer *vb;
  650. unsigned long flags;
  651. for (;;) {
  652. if (V4L2_TYPE_IS_OUTPUT(q->type))
  653. vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  654. else
  655. vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  656. if (vb == NULL)
  657. return;
  658. spin_lock_irqsave(&ctx->dev->irqlock, flags);
  659. v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
  660. spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
  661. }
  662. }
  663. static struct vb2_ops m2mtest_qops = {
  664. .queue_setup = m2mtest_queue_setup,
  665. .buf_prepare = m2mtest_buf_prepare,
  666. .buf_queue = m2mtest_buf_queue,
  667. .start_streaming = m2mtest_start_streaming,
  668. .stop_streaming = m2mtest_stop_streaming,
  669. .wait_prepare = vb2_ops_wait_prepare,
  670. .wait_finish = vb2_ops_wait_finish,
  671. };
  672. static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
  673. {
  674. struct m2mtest_ctx *ctx = priv;
  675. int ret;
  676. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  677. src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  678. src_vq->drv_priv = ctx;
  679. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  680. src_vq->ops = &m2mtest_qops;
  681. src_vq->mem_ops = &vb2_vmalloc_memops;
  682. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  683. src_vq->lock = &ctx->dev->dev_mutex;
  684. ret = vb2_queue_init(src_vq);
  685. if (ret)
  686. return ret;
  687. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  688. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  689. dst_vq->drv_priv = ctx;
  690. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  691. dst_vq->ops = &m2mtest_qops;
  692. dst_vq->mem_ops = &vb2_vmalloc_memops;
  693. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  694. dst_vq->lock = &ctx->dev->dev_mutex;
  695. return vb2_queue_init(dst_vq);
  696. }
  697. static const struct v4l2_ctrl_config m2mtest_ctrl_trans_time_msec = {
  698. .ops = &m2mtest_ctrl_ops,
  699. .id = V4L2_CID_TRANS_TIME_MSEC,
  700. .name = "Transaction Time (msec)",
  701. .type = V4L2_CTRL_TYPE_INTEGER,
  702. .def = MEM2MEM_DEF_TRANSTIME,
  703. .min = 1,
  704. .max = 10001,
  705. .step = 1,
  706. };
  707. static const struct v4l2_ctrl_config m2mtest_ctrl_trans_num_bufs = {
  708. .ops = &m2mtest_ctrl_ops,
  709. .id = V4L2_CID_TRANS_NUM_BUFS,
  710. .name = "Buffers Per Transaction",
  711. .type = V4L2_CTRL_TYPE_INTEGER,
  712. .def = 1,
  713. .min = 1,
  714. .max = MEM2MEM_DEF_NUM_BUFS,
  715. .step = 1,
  716. };
  717. /*
  718. * File operations
  719. */
  720. static int m2mtest_open(struct file *file)
  721. {
  722. struct m2mtest_dev *dev = video_drvdata(file);
  723. struct m2mtest_ctx *ctx = NULL;
  724. struct v4l2_ctrl_handler *hdl;
  725. int rc = 0;
  726. if (mutex_lock_interruptible(&dev->dev_mutex))
  727. return -ERESTARTSYS;
  728. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  729. if (!ctx) {
  730. rc = -ENOMEM;
  731. goto open_unlock;
  732. }
  733. v4l2_fh_init(&ctx->fh, video_devdata(file));
  734. file->private_data = &ctx->fh;
  735. ctx->dev = dev;
  736. hdl = &ctx->hdl;
  737. v4l2_ctrl_handler_init(hdl, 4);
  738. v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  739. v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  740. v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_time_msec, NULL);
  741. v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_num_bufs, NULL);
  742. if (hdl->error) {
  743. rc = hdl->error;
  744. v4l2_ctrl_handler_free(hdl);
  745. goto open_unlock;
  746. }
  747. ctx->fh.ctrl_handler = hdl;
  748. v4l2_ctrl_handler_setup(hdl);
  749. ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
  750. ctx->q_data[V4L2_M2M_SRC].width = 640;
  751. ctx->q_data[V4L2_M2M_SRC].height = 480;
  752. ctx->q_data[V4L2_M2M_SRC].sizeimage =
  753. ctx->q_data[V4L2_M2M_SRC].width *
  754. ctx->q_data[V4L2_M2M_SRC].height *
  755. (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
  756. ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
  757. ctx->colorspace = V4L2_COLORSPACE_REC709;
  758. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  759. if (IS_ERR(ctx->fh.m2m_ctx)) {
  760. rc = PTR_ERR(ctx->fh.m2m_ctx);
  761. v4l2_ctrl_handler_free(hdl);
  762. kfree(ctx);
  763. goto open_unlock;
  764. }
  765. v4l2_fh_add(&ctx->fh);
  766. atomic_inc(&dev->num_inst);
  767. dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
  768. ctx, ctx->fh.m2m_ctx);
  769. open_unlock:
  770. mutex_unlock(&dev->dev_mutex);
  771. return rc;
  772. }
  773. static int m2mtest_release(struct file *file)
  774. {
  775. struct m2mtest_dev *dev = video_drvdata(file);
  776. struct m2mtest_ctx *ctx = file2ctx(file);
  777. dprintk(dev, "Releasing instance %p\n", ctx);
  778. v4l2_fh_del(&ctx->fh);
  779. v4l2_fh_exit(&ctx->fh);
  780. v4l2_ctrl_handler_free(&ctx->hdl);
  781. mutex_lock(&dev->dev_mutex);
  782. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  783. mutex_unlock(&dev->dev_mutex);
  784. kfree(ctx);
  785. atomic_dec(&dev->num_inst);
  786. return 0;
  787. }
  788. static const struct v4l2_file_operations m2mtest_fops = {
  789. .owner = THIS_MODULE,
  790. .open = m2mtest_open,
  791. .release = m2mtest_release,
  792. .poll = v4l2_m2m_fop_poll,
  793. .unlocked_ioctl = video_ioctl2,
  794. .mmap = v4l2_m2m_fop_mmap,
  795. };
  796. static struct video_device m2mtest_videodev = {
  797. .name = MEM2MEM_NAME,
  798. .vfl_dir = VFL_DIR_M2M,
  799. .fops = &m2mtest_fops,
  800. .ioctl_ops = &m2mtest_ioctl_ops,
  801. .minor = -1,
  802. .release = video_device_release,
  803. };
  804. static struct v4l2_m2m_ops m2m_ops = {
  805. .device_run = device_run,
  806. .job_ready = job_ready,
  807. .job_abort = job_abort,
  808. };
  809. static int m2mtest_probe(struct platform_device *pdev)
  810. {
  811. struct m2mtest_dev *dev;
  812. struct video_device *vfd;
  813. int ret;
  814. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  815. if (!dev)
  816. return -ENOMEM;
  817. spin_lock_init(&dev->irqlock);
  818. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  819. if (ret)
  820. return ret;
  821. atomic_set(&dev->num_inst, 0);
  822. mutex_init(&dev->dev_mutex);
  823. vfd = video_device_alloc();
  824. if (!vfd) {
  825. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  826. ret = -ENOMEM;
  827. goto unreg_dev;
  828. }
  829. *vfd = m2mtest_videodev;
  830. vfd->lock = &dev->dev_mutex;
  831. vfd->v4l2_dev = &dev->v4l2_dev;
  832. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  833. if (ret) {
  834. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  835. goto rel_vdev;
  836. }
  837. video_set_drvdata(vfd, dev);
  838. snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
  839. dev->vfd = vfd;
  840. v4l2_info(&dev->v4l2_dev,
  841. "Device registered as /dev/video%d\n", vfd->num);
  842. setup_timer(&dev->timer, device_isr, (long)dev);
  843. platform_set_drvdata(pdev, dev);
  844. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  845. if (IS_ERR(dev->m2m_dev)) {
  846. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  847. ret = PTR_ERR(dev->m2m_dev);
  848. goto err_m2m;
  849. }
  850. return 0;
  851. err_m2m:
  852. v4l2_m2m_release(dev->m2m_dev);
  853. video_unregister_device(dev->vfd);
  854. rel_vdev:
  855. video_device_release(vfd);
  856. unreg_dev:
  857. v4l2_device_unregister(&dev->v4l2_dev);
  858. return ret;
  859. }
  860. static int m2mtest_remove(struct platform_device *pdev)
  861. {
  862. struct m2mtest_dev *dev = platform_get_drvdata(pdev);
  863. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
  864. v4l2_m2m_release(dev->m2m_dev);
  865. del_timer_sync(&dev->timer);
  866. video_unregister_device(dev->vfd);
  867. v4l2_device_unregister(&dev->v4l2_dev);
  868. return 0;
  869. }
  870. static struct platform_driver m2mtest_pdrv = {
  871. .probe = m2mtest_probe,
  872. .remove = m2mtest_remove,
  873. .driver = {
  874. .name = MEM2MEM_NAME,
  875. .owner = THIS_MODULE,
  876. },
  877. };
  878. static void __exit m2mtest_exit(void)
  879. {
  880. platform_driver_unregister(&m2mtest_pdrv);
  881. platform_device_unregister(&m2mtest_pdev);
  882. }
  883. static int __init m2mtest_init(void)
  884. {
  885. int ret;
  886. ret = platform_device_register(&m2mtest_pdev);
  887. if (ret)
  888. return ret;
  889. ret = platform_driver_register(&m2mtest_pdrv);
  890. if (ret)
  891. platform_device_unregister(&m2mtest_pdev);
  892. return 0;
  893. }
  894. module_init(m2mtest_init);
  895. module_exit(m2mtest_exit);