mem2mem_testdev.c 26 KB

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