mem2mem_testdev.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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, <p.osciak@samsung.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/version.h>
  23. #include <linux/timer.h>
  24. #include <linux/sched.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/videobuf-vmalloc.h>
  30. #define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
  31. MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
  32. MODULE_AUTHOR("Pawel Osciak, <p.osciak@samsung.com>");
  33. MODULE_LICENSE("GPL");
  34. #define MIN_W 32
  35. #define MIN_H 32
  36. #define MAX_W 640
  37. #define MAX_H 480
  38. #define DIM_ALIGN_MASK 0x08 /* 8-alignment for dimensions */
  39. /* Flags that indicate a format can be used for capture/output */
  40. #define MEM2MEM_CAPTURE (1 << 0)
  41. #define MEM2MEM_OUTPUT (1 << 1)
  42. #define MEM2MEM_NAME "m2m-testdev"
  43. /* Per queue */
  44. #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
  45. /* In bytes, per queue */
  46. #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
  47. /* Default transaction time in msec */
  48. #define MEM2MEM_DEF_TRANSTIME 1000
  49. /* Default number of buffers per transaction */
  50. #define MEM2MEM_DEF_TRANSLEN 1
  51. #define MEM2MEM_COLOR_STEP (0xff >> 4)
  52. #define MEM2MEM_NUM_TILES 8
  53. #define dprintk(dev, fmt, arg...) \
  54. v4l2_dbg(1, 1, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
  55. void m2mtest_dev_release(struct device *dev)
  56. {}
  57. static struct platform_device m2mtest_pdev = {
  58. .name = MEM2MEM_NAME,
  59. .dev.release = m2mtest_dev_release,
  60. };
  61. struct m2mtest_fmt {
  62. char *name;
  63. u32 fourcc;
  64. int depth;
  65. /* Types the format can be used for */
  66. u32 types;
  67. };
  68. static struct m2mtest_fmt formats[] = {
  69. {
  70. .name = "RGB565 (BE)",
  71. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  72. .depth = 16,
  73. /* Both capture and output format */
  74. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  75. },
  76. {
  77. .name = "4:2:2, packed, YUYV",
  78. .fourcc = V4L2_PIX_FMT_YUYV,
  79. .depth = 16,
  80. /* Output-only format */
  81. .types = MEM2MEM_OUTPUT,
  82. },
  83. };
  84. /* Per-queue, driver-specific private data */
  85. struct m2mtest_q_data {
  86. unsigned int width;
  87. unsigned int height;
  88. unsigned int sizeimage;
  89. struct m2mtest_fmt *fmt;
  90. };
  91. enum {
  92. V4L2_M2M_SRC = 0,
  93. V4L2_M2M_DST = 1,
  94. };
  95. /* Source and destination queue data */
  96. static struct m2mtest_q_data q_data[2];
  97. static struct m2mtest_q_data *get_q_data(enum v4l2_buf_type type)
  98. {
  99. switch (type) {
  100. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  101. return &q_data[V4L2_M2M_SRC];
  102. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  103. return &q_data[V4L2_M2M_DST];
  104. default:
  105. BUG();
  106. }
  107. return NULL;
  108. }
  109. #define V4L2_CID_TRANS_TIME_MSEC V4L2_CID_PRIVATE_BASE
  110. #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_PRIVATE_BASE + 1)
  111. static struct v4l2_queryctrl m2mtest_ctrls[] = {
  112. {
  113. .id = V4L2_CID_TRANS_TIME_MSEC,
  114. .type = V4L2_CTRL_TYPE_INTEGER,
  115. .name = "Transaction time (msec)",
  116. .minimum = 1,
  117. .maximum = 10000,
  118. .step = 100,
  119. .default_value = 1000,
  120. .flags = 0,
  121. }, {
  122. .id = V4L2_CID_TRANS_NUM_BUFS,
  123. .type = V4L2_CTRL_TYPE_INTEGER,
  124. .name = "Buffers per transaction",
  125. .minimum = 1,
  126. .maximum = MEM2MEM_DEF_NUM_BUFS,
  127. .step = 1,
  128. .default_value = 1,
  129. .flags = 0,
  130. },
  131. };
  132. #define NUM_FORMATS ARRAY_SIZE(formats)
  133. static struct m2mtest_fmt *find_format(struct v4l2_format *f)
  134. {
  135. struct m2mtest_fmt *fmt;
  136. unsigned int k;
  137. for (k = 0; k < NUM_FORMATS; k++) {
  138. fmt = &formats[k];
  139. if (fmt->fourcc == f->fmt.pix.pixelformat)
  140. break;
  141. }
  142. if (k == NUM_FORMATS)
  143. return NULL;
  144. return &formats[k];
  145. }
  146. struct m2mtest_dev {
  147. struct v4l2_device v4l2_dev;
  148. struct video_device *vfd;
  149. atomic_t num_inst;
  150. struct mutex dev_mutex;
  151. spinlock_t irqlock;
  152. struct timer_list timer;
  153. struct v4l2_m2m_dev *m2m_dev;
  154. };
  155. struct m2mtest_ctx {
  156. struct m2mtest_dev *dev;
  157. /* Processed buffers in this transaction */
  158. u8 num_processed;
  159. /* Transaction length (i.e. how many buffers per transaction) */
  160. u32 translen;
  161. /* Transaction time (i.e. simulated processing time) in milliseconds */
  162. u32 transtime;
  163. /* Abort requested by m2m */
  164. int aborting;
  165. struct v4l2_m2m_ctx *m2m_ctx;
  166. };
  167. struct m2mtest_buffer {
  168. /* vb must be first! */
  169. struct videobuf_buffer vb;
  170. };
  171. static struct v4l2_queryctrl *get_ctrl(int id)
  172. {
  173. int i;
  174. for (i = 0; i < ARRAY_SIZE(m2mtest_ctrls); ++i) {
  175. if (id == m2mtest_ctrls[i].id)
  176. return &m2mtest_ctrls[i];
  177. }
  178. return NULL;
  179. }
  180. static int device_process(struct m2mtest_ctx *ctx,
  181. struct m2mtest_buffer *in_buf,
  182. struct m2mtest_buffer *out_buf)
  183. {
  184. struct m2mtest_dev *dev = ctx->dev;
  185. u8 *p_in, *p_out;
  186. int x, y, t, w;
  187. int tile_w, bytes_left;
  188. struct videobuf_queue *src_q;
  189. struct videobuf_queue *dst_q;
  190. src_q = v4l2_m2m_get_src_vq(ctx->m2m_ctx);
  191. dst_q = v4l2_m2m_get_dst_vq(ctx->m2m_ctx);
  192. p_in = videobuf_queue_to_vaddr(src_q, &in_buf->vb);
  193. p_out = videobuf_queue_to_vaddr(dst_q, &out_buf->vb);
  194. if (!p_in || !p_out) {
  195. v4l2_err(&dev->v4l2_dev,
  196. "Acquiring kernel pointers to buffers failed\n");
  197. return -EFAULT;
  198. }
  199. if (in_buf->vb.size < out_buf->vb.size) {
  200. v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
  201. return -EINVAL;
  202. }
  203. tile_w = (in_buf->vb.width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
  204. / MEM2MEM_NUM_TILES;
  205. bytes_left = in_buf->vb.bytesperline - tile_w * MEM2MEM_NUM_TILES;
  206. w = 0;
  207. for (y = 0; y < in_buf->vb.height; ++y) {
  208. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  209. if (w & 0x1) {
  210. for (x = 0; x < tile_w; ++x)
  211. *p_out++ = *p_in++ + MEM2MEM_COLOR_STEP;
  212. } else {
  213. for (x = 0; x < tile_w; ++x)
  214. *p_out++ = *p_in++ - MEM2MEM_COLOR_STEP;
  215. }
  216. ++w;
  217. }
  218. p_in += bytes_left;
  219. p_out += bytes_left;
  220. }
  221. return 0;
  222. }
  223. static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
  224. {
  225. dprintk(dev, "Scheduling a simulated irq\n");
  226. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
  227. }
  228. /*
  229. * mem2mem callbacks
  230. */
  231. /**
  232. * job_ready() - check whether an instance is ready to be scheduled to run
  233. */
  234. static int job_ready(void *priv)
  235. {
  236. struct m2mtest_ctx *ctx = priv;
  237. if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < ctx->translen
  238. || v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) < ctx->translen) {
  239. dprintk(ctx->dev, "Not enough buffers available\n");
  240. return 0;
  241. }
  242. return 1;
  243. }
  244. static void job_abort(void *priv)
  245. {
  246. struct m2mtest_ctx *ctx = priv;
  247. /* Will cancel the transaction in the next interrupt handler */
  248. ctx->aborting = 1;
  249. }
  250. /* device_run() - prepares and starts the device
  251. *
  252. * This simulates all the immediate preparations required before starting
  253. * a device. This will be called by the framework when it decides to schedule
  254. * a particular instance.
  255. */
  256. static void device_run(void *priv)
  257. {
  258. struct m2mtest_ctx *ctx = priv;
  259. struct m2mtest_dev *dev = ctx->dev;
  260. struct m2mtest_buffer *src_buf, *dst_buf;
  261. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  262. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  263. device_process(ctx, src_buf, dst_buf);
  264. /* Run a timer, which simulates a hardware irq */
  265. schedule_irq(dev, ctx->transtime);
  266. }
  267. static void device_isr(unsigned long priv)
  268. {
  269. struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
  270. struct m2mtest_ctx *curr_ctx;
  271. struct m2mtest_buffer *src_buf, *dst_buf;
  272. unsigned long flags;
  273. curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
  274. if (NULL == curr_ctx) {
  275. printk(KERN_ERR
  276. "Instance released before the end of transaction\n");
  277. return;
  278. }
  279. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  280. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  281. curr_ctx->num_processed++;
  282. if (curr_ctx->num_processed == curr_ctx->translen
  283. || curr_ctx->aborting) {
  284. dprintk(curr_ctx->dev, "Finishing transaction\n");
  285. curr_ctx->num_processed = 0;
  286. spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
  287. src_buf->vb.state = dst_buf->vb.state = VIDEOBUF_DONE;
  288. wake_up(&src_buf->vb.done);
  289. wake_up(&dst_buf->vb.done);
  290. spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
  291. v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->m2m_ctx);
  292. } else {
  293. spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
  294. src_buf->vb.state = dst_buf->vb.state = VIDEOBUF_DONE;
  295. wake_up(&src_buf->vb.done);
  296. wake_up(&dst_buf->vb.done);
  297. spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
  298. device_run(curr_ctx);
  299. }
  300. }
  301. /*
  302. * video ioctls
  303. */
  304. static int vidioc_querycap(struct file *file, void *priv,
  305. struct v4l2_capability *cap)
  306. {
  307. strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
  308. strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
  309. cap->bus_info[0] = 0;
  310. cap->version = KERNEL_VERSION(0, 1, 0);
  311. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
  312. | V4L2_CAP_STREAMING;
  313. return 0;
  314. }
  315. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  316. {
  317. int i, num;
  318. struct m2mtest_fmt *fmt;
  319. num = 0;
  320. for (i = 0; i < NUM_FORMATS; ++i) {
  321. if (formats[i].types & type) {
  322. /* index-th format of type type found ? */
  323. if (num == f->index)
  324. break;
  325. /* Correct type but haven't reached our index yet,
  326. * just increment per-type index */
  327. ++num;
  328. }
  329. }
  330. if (i < NUM_FORMATS) {
  331. /* Format found */
  332. fmt = &formats[i];
  333. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  334. f->pixelformat = fmt->fourcc;
  335. return 0;
  336. }
  337. /* Format not found */
  338. return -EINVAL;
  339. }
  340. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  341. struct v4l2_fmtdesc *f)
  342. {
  343. return enum_fmt(f, MEM2MEM_CAPTURE);
  344. }
  345. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  346. struct v4l2_fmtdesc *f)
  347. {
  348. return enum_fmt(f, MEM2MEM_OUTPUT);
  349. }
  350. static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  351. {
  352. struct videobuf_queue *vq;
  353. struct m2mtest_q_data *q_data;
  354. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  355. if (!vq)
  356. return -EINVAL;
  357. q_data = get_q_data(f->type);
  358. f->fmt.pix.width = q_data->width;
  359. f->fmt.pix.height = q_data->height;
  360. f->fmt.pix.field = vq->field;
  361. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  362. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  363. f->fmt.pix.sizeimage = q_data->sizeimage;
  364. return 0;
  365. }
  366. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  367. struct v4l2_format *f)
  368. {
  369. return vidioc_g_fmt(priv, f);
  370. }
  371. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  372. struct v4l2_format *f)
  373. {
  374. return vidioc_g_fmt(priv, f);
  375. }
  376. static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
  377. {
  378. enum v4l2_field field;
  379. field = f->fmt.pix.field;
  380. if (field == V4L2_FIELD_ANY)
  381. field = V4L2_FIELD_NONE;
  382. else if (V4L2_FIELD_NONE != field)
  383. return -EINVAL;
  384. /* V4L2 specification suggests the driver corrects the format struct
  385. * if any of the dimensions is unsupported */
  386. f->fmt.pix.field = field;
  387. if (f->fmt.pix.height < MIN_H)
  388. f->fmt.pix.height = MIN_H;
  389. else if (f->fmt.pix.height > MAX_H)
  390. f->fmt.pix.height = MAX_H;
  391. if (f->fmt.pix.width < MIN_W)
  392. f->fmt.pix.width = MIN_W;
  393. else if (f->fmt.pix.width > MAX_W)
  394. f->fmt.pix.width = MAX_W;
  395. f->fmt.pix.width &= ~DIM_ALIGN_MASK;
  396. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  397. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  398. return 0;
  399. }
  400. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  401. struct v4l2_format *f)
  402. {
  403. struct m2mtest_fmt *fmt;
  404. struct m2mtest_ctx *ctx = priv;
  405. fmt = find_format(f);
  406. if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
  407. v4l2_err(&ctx->dev->v4l2_dev,
  408. "Fourcc format (0x%08x) invalid.\n",
  409. f->fmt.pix.pixelformat);
  410. return -EINVAL;
  411. }
  412. return vidioc_try_fmt(f, fmt);
  413. }
  414. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  415. struct v4l2_format *f)
  416. {
  417. struct m2mtest_fmt *fmt;
  418. struct m2mtest_ctx *ctx = priv;
  419. fmt = find_format(f);
  420. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
  421. v4l2_err(&ctx->dev->v4l2_dev,
  422. "Fourcc format (0x%08x) invalid.\n",
  423. f->fmt.pix.pixelformat);
  424. return -EINVAL;
  425. }
  426. return vidioc_try_fmt(f, fmt);
  427. }
  428. static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  429. {
  430. struct m2mtest_q_data *q_data;
  431. struct videobuf_queue *vq;
  432. int ret = 0;
  433. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  434. if (!vq)
  435. return -EINVAL;
  436. q_data = get_q_data(f->type);
  437. if (!q_data)
  438. return -EINVAL;
  439. mutex_lock(&vq->vb_lock);
  440. if (videobuf_queue_is_busy(vq)) {
  441. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  442. ret = -EBUSY;
  443. goto out;
  444. }
  445. q_data->fmt = find_format(f);
  446. q_data->width = f->fmt.pix.width;
  447. q_data->height = f->fmt.pix.height;
  448. q_data->sizeimage = q_data->width * q_data->height
  449. * q_data->fmt->depth >> 3;
  450. vq->field = f->fmt.pix.field;
  451. dprintk(ctx->dev,
  452. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  453. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  454. out:
  455. mutex_unlock(&vq->vb_lock);
  456. return ret;
  457. }
  458. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  459. struct v4l2_format *f)
  460. {
  461. int ret;
  462. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  463. if (ret)
  464. return ret;
  465. return vidioc_s_fmt(priv, f);
  466. }
  467. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  468. struct v4l2_format *f)
  469. {
  470. int ret;
  471. ret = vidioc_try_fmt_vid_out(file, priv, f);
  472. if (ret)
  473. return ret;
  474. return vidioc_s_fmt(priv, f);
  475. }
  476. static int vidioc_reqbufs(struct file *file, void *priv,
  477. struct v4l2_requestbuffers *reqbufs)
  478. {
  479. struct m2mtest_ctx *ctx = priv;
  480. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  481. }
  482. static int vidioc_querybuf(struct file *file, void *priv,
  483. struct v4l2_buffer *buf)
  484. {
  485. struct m2mtest_ctx *ctx = priv;
  486. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  487. }
  488. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  489. {
  490. struct m2mtest_ctx *ctx = priv;
  491. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  492. }
  493. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  494. {
  495. struct m2mtest_ctx *ctx = priv;
  496. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  497. }
  498. static int vidioc_streamon(struct file *file, void *priv,
  499. enum v4l2_buf_type type)
  500. {
  501. struct m2mtest_ctx *ctx = priv;
  502. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  503. }
  504. static int vidioc_streamoff(struct file *file, void *priv,
  505. enum v4l2_buf_type type)
  506. {
  507. struct m2mtest_ctx *ctx = priv;
  508. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  509. }
  510. static int vidioc_queryctrl(struct file *file, void *priv,
  511. struct v4l2_queryctrl *qc)
  512. {
  513. struct v4l2_queryctrl *c;
  514. c = get_ctrl(qc->id);
  515. if (!c)
  516. return -EINVAL;
  517. *qc = *c;
  518. return 0;
  519. }
  520. static int vidioc_g_ctrl(struct file *file, void *priv,
  521. struct v4l2_control *ctrl)
  522. {
  523. struct m2mtest_ctx *ctx = priv;
  524. switch (ctrl->id) {
  525. case V4L2_CID_TRANS_TIME_MSEC:
  526. ctrl->value = ctx->transtime;
  527. break;
  528. case V4L2_CID_TRANS_NUM_BUFS:
  529. ctrl->value = ctx->translen;
  530. break;
  531. default:
  532. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  533. return -EINVAL;
  534. }
  535. return 0;
  536. }
  537. static int check_ctrl_val(struct m2mtest_ctx *ctx, struct v4l2_control *ctrl)
  538. {
  539. struct v4l2_queryctrl *c;
  540. c = get_ctrl(ctrl->id);
  541. if (!c)
  542. return -EINVAL;
  543. if (ctrl->value < c->minimum || ctrl->value > c->maximum) {
  544. v4l2_err(&ctx->dev->v4l2_dev, "Value out of range\n");
  545. return -ERANGE;
  546. }
  547. return 0;
  548. }
  549. static int vidioc_s_ctrl(struct file *file, void *priv,
  550. struct v4l2_control *ctrl)
  551. {
  552. struct m2mtest_ctx *ctx = priv;
  553. int ret = 0;
  554. ret = check_ctrl_val(ctx, ctrl);
  555. if (ret != 0)
  556. return ret;
  557. switch (ctrl->id) {
  558. case V4L2_CID_TRANS_TIME_MSEC:
  559. ctx->transtime = ctrl->value;
  560. break;
  561. case V4L2_CID_TRANS_NUM_BUFS:
  562. ctx->translen = ctrl->value;
  563. break;
  564. default:
  565. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  566. return -EINVAL;
  567. }
  568. return 0;
  569. }
  570. static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
  571. .vidioc_querycap = vidioc_querycap,
  572. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  573. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  574. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  575. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  576. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  577. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  578. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  579. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  580. .vidioc_reqbufs = vidioc_reqbufs,
  581. .vidioc_querybuf = vidioc_querybuf,
  582. .vidioc_qbuf = vidioc_qbuf,
  583. .vidioc_dqbuf = vidioc_dqbuf,
  584. .vidioc_streamon = vidioc_streamon,
  585. .vidioc_streamoff = vidioc_streamoff,
  586. .vidioc_queryctrl = vidioc_queryctrl,
  587. .vidioc_g_ctrl = vidioc_g_ctrl,
  588. .vidioc_s_ctrl = vidioc_s_ctrl,
  589. };
  590. /*
  591. * Queue operations
  592. */
  593. static void m2mtest_buf_release(struct videobuf_queue *vq,
  594. struct videobuf_buffer *vb)
  595. {
  596. struct m2mtest_ctx *ctx = vq->priv_data;
  597. dprintk(ctx->dev, "type: %d, index: %d, state: %d\n",
  598. vq->type, vb->i, vb->state);
  599. videobuf_vmalloc_free(vb);
  600. vb->state = VIDEOBUF_NEEDS_INIT;
  601. }
  602. static int m2mtest_buf_setup(struct videobuf_queue *vq, unsigned int *count,
  603. unsigned int *size)
  604. {
  605. struct m2mtest_ctx *ctx = vq->priv_data;
  606. struct m2mtest_q_data *q_data;
  607. q_data = get_q_data(vq->type);
  608. *size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  609. dprintk(ctx->dev, "size:%d, w/h %d/%d, depth: %d\n",
  610. *size, q_data->width, q_data->height, q_data->fmt->depth);
  611. if (0 == *count)
  612. *count = MEM2MEM_DEF_NUM_BUFS;
  613. while (*size * *count > MEM2MEM_VID_MEM_LIMIT)
  614. (*count)--;
  615. v4l2_info(&ctx->dev->v4l2_dev,
  616. "%d buffers of size %d set up.\n", *count, *size);
  617. return 0;
  618. }
  619. static int m2mtest_buf_prepare(struct videobuf_queue *vq,
  620. struct videobuf_buffer *vb,
  621. enum v4l2_field field)
  622. {
  623. struct m2mtest_ctx *ctx = vq->priv_data;
  624. struct m2mtest_q_data *q_data;
  625. int ret;
  626. dprintk(ctx->dev, "type: %d, index: %d, state: %d\n",
  627. vq->type, vb->i, vb->state);
  628. q_data = get_q_data(vq->type);
  629. if (vb->baddr) {
  630. /* User-provided buffer */
  631. if (vb->bsize < q_data->sizeimage) {
  632. /* Buffer too small to fit a frame */
  633. v4l2_err(&ctx->dev->v4l2_dev,
  634. "User-provided buffer too small\n");
  635. return -EINVAL;
  636. }
  637. } else if (vb->state != VIDEOBUF_NEEDS_INIT
  638. && vb->bsize < q_data->sizeimage) {
  639. /* We provide the buffer, but it's already been initialized
  640. * and is too small */
  641. return -EINVAL;
  642. }
  643. vb->width = q_data->width;
  644. vb->height = q_data->height;
  645. vb->bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  646. vb->size = q_data->sizeimage;
  647. vb->field = field;
  648. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  649. ret = videobuf_iolock(vq, vb, NULL);
  650. if (ret) {
  651. v4l2_err(&ctx->dev->v4l2_dev,
  652. "Iolock failed\n");
  653. goto fail;
  654. }
  655. }
  656. vb->state = VIDEOBUF_PREPARED;
  657. return 0;
  658. fail:
  659. m2mtest_buf_release(vq, vb);
  660. return ret;
  661. }
  662. static void m2mtest_buf_queue(struct videobuf_queue *vq,
  663. struct videobuf_buffer *vb)
  664. {
  665. struct m2mtest_ctx *ctx = vq->priv_data;
  666. v4l2_m2m_buf_queue(ctx->m2m_ctx, vq, vb);
  667. }
  668. static struct videobuf_queue_ops m2mtest_qops = {
  669. .buf_setup = m2mtest_buf_setup,
  670. .buf_prepare = m2mtest_buf_prepare,
  671. .buf_queue = m2mtest_buf_queue,
  672. .buf_release = m2mtest_buf_release,
  673. };
  674. static void queue_init(void *priv, struct videobuf_queue *vq,
  675. enum v4l2_buf_type type)
  676. {
  677. struct m2mtest_ctx *ctx = priv;
  678. videobuf_queue_vmalloc_init(vq, &m2mtest_qops, ctx->dev->v4l2_dev.dev,
  679. &ctx->dev->irqlock, type, V4L2_FIELD_NONE,
  680. sizeof(struct m2mtest_buffer), priv);
  681. }
  682. /*
  683. * File operations
  684. */
  685. static int m2mtest_open(struct file *file)
  686. {
  687. struct m2mtest_dev *dev = video_drvdata(file);
  688. struct m2mtest_ctx *ctx = NULL;
  689. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  690. if (!ctx)
  691. return -ENOMEM;
  692. file->private_data = ctx;
  693. ctx->dev = dev;
  694. ctx->translen = MEM2MEM_DEF_TRANSLEN;
  695. ctx->transtime = MEM2MEM_DEF_TRANSTIME;
  696. ctx->num_processed = 0;
  697. ctx->m2m_ctx = v4l2_m2m_ctx_init(ctx, dev->m2m_dev, queue_init);
  698. if (IS_ERR(ctx->m2m_ctx)) {
  699. int ret = PTR_ERR(ctx->m2m_ctx);
  700. kfree(ctx);
  701. return ret;
  702. }
  703. atomic_inc(&dev->num_inst);
  704. dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
  705. return 0;
  706. }
  707. static int m2mtest_release(struct file *file)
  708. {
  709. struct m2mtest_dev *dev = video_drvdata(file);
  710. struct m2mtest_ctx *ctx = file->private_data;
  711. dprintk(dev, "Releasing instance %p\n", ctx);
  712. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  713. kfree(ctx);
  714. atomic_dec(&dev->num_inst);
  715. return 0;
  716. }
  717. static unsigned int m2mtest_poll(struct file *file,
  718. struct poll_table_struct *wait)
  719. {
  720. struct m2mtest_ctx *ctx = (struct m2mtest_ctx *)file->private_data;
  721. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  722. }
  723. static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
  724. {
  725. struct m2mtest_ctx *ctx = (struct m2mtest_ctx *)file->private_data;
  726. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  727. }
  728. static const struct v4l2_file_operations m2mtest_fops = {
  729. .owner = THIS_MODULE,
  730. .open = m2mtest_open,
  731. .release = m2mtest_release,
  732. .poll = m2mtest_poll,
  733. .ioctl = video_ioctl2,
  734. .mmap = m2mtest_mmap,
  735. };
  736. static struct video_device m2mtest_videodev = {
  737. .name = MEM2MEM_NAME,
  738. .fops = &m2mtest_fops,
  739. .ioctl_ops = &m2mtest_ioctl_ops,
  740. .minor = -1,
  741. .release = video_device_release,
  742. };
  743. static struct v4l2_m2m_ops m2m_ops = {
  744. .device_run = device_run,
  745. .job_ready = job_ready,
  746. .job_abort = job_abort,
  747. };
  748. static int m2mtest_probe(struct platform_device *pdev)
  749. {
  750. struct m2mtest_dev *dev;
  751. struct video_device *vfd;
  752. int ret;
  753. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  754. if (!dev)
  755. return -ENOMEM;
  756. spin_lock_init(&dev->irqlock);
  757. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  758. if (ret)
  759. goto free_dev;
  760. atomic_set(&dev->num_inst, 0);
  761. mutex_init(&dev->dev_mutex);
  762. vfd = video_device_alloc();
  763. if (!vfd) {
  764. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  765. ret = -ENOMEM;
  766. goto unreg_dev;
  767. }
  768. *vfd = m2mtest_videodev;
  769. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  770. if (ret) {
  771. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  772. goto rel_vdev;
  773. }
  774. video_set_drvdata(vfd, dev);
  775. snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
  776. dev->vfd = vfd;
  777. v4l2_info(&dev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
  778. "Device registered as /dev/video%d\n", vfd->num);
  779. setup_timer(&dev->timer, device_isr, (long)dev);
  780. platform_set_drvdata(pdev, dev);
  781. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  782. if (IS_ERR(dev->m2m_dev)) {
  783. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  784. ret = PTR_ERR(dev->m2m_dev);
  785. goto err_m2m;
  786. }
  787. return 0;
  788. err_m2m:
  789. video_unregister_device(dev->vfd);
  790. rel_vdev:
  791. video_device_release(vfd);
  792. unreg_dev:
  793. v4l2_device_unregister(&dev->v4l2_dev);
  794. free_dev:
  795. kfree(dev);
  796. return ret;
  797. }
  798. static int m2mtest_remove(struct platform_device *pdev)
  799. {
  800. struct m2mtest_dev *dev =
  801. (struct m2mtest_dev *)platform_get_drvdata(pdev);
  802. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
  803. v4l2_m2m_release(dev->m2m_dev);
  804. del_timer_sync(&dev->timer);
  805. video_unregister_device(dev->vfd);
  806. v4l2_device_unregister(&dev->v4l2_dev);
  807. kfree(dev);
  808. return 0;
  809. }
  810. static struct platform_driver m2mtest_pdrv = {
  811. .probe = m2mtest_probe,
  812. .remove = m2mtest_remove,
  813. .driver = {
  814. .name = MEM2MEM_NAME,
  815. .owner = THIS_MODULE,
  816. },
  817. };
  818. static void __exit m2mtest_exit(void)
  819. {
  820. platform_driver_unregister(&m2mtest_pdrv);
  821. platform_device_unregister(&m2mtest_pdev);
  822. }
  823. static int __init m2mtest_init(void)
  824. {
  825. int ret;
  826. ret = platform_device_register(&m2mtest_pdev);
  827. if (ret)
  828. return ret;
  829. ret = platform_driver_register(&m2mtest_pdrv);
  830. if (ret)
  831. platform_device_unregister(&m2mtest_pdev);
  832. return 0;
  833. }
  834. module_init(m2mtest_init);
  835. module_exit(m2mtest_exit);