mem2mem_testdev.c 26 KB

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