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