vim2m.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
  33. MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  34. MODULE_LICENSE("GPL");
  35. MODULE_VERSION("0.1.1");
  36. MODULE_ALIAS("mem2mem_testdev");
  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 "vim2m"
  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 vim2m_dev_release(struct device *dev)
  63. {}
  64. static struct platform_device vim2m_pdev = {
  65. .name = MEM2MEM_NAME,
  66. .dev.release = vim2m_dev_release,
  67. };
  68. struct vim2m_fmt {
  69. u32 fourcc;
  70. int depth;
  71. /* Types the format can be used for */
  72. u32 types;
  73. };
  74. static struct vim2m_fmt formats[] = {
  75. {
  76. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  77. .depth = 16,
  78. /* Both capture and output format */
  79. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  80. },
  81. {
  82. .fourcc = V4L2_PIX_FMT_YUYV,
  83. .depth = 16,
  84. /* Output-only format */
  85. .types = MEM2MEM_OUTPUT,
  86. },
  87. };
  88. #define NUM_FORMATS ARRAY_SIZE(formats)
  89. /* Per-queue, driver-specific private data */
  90. struct vim2m_q_data {
  91. unsigned int width;
  92. unsigned int height;
  93. unsigned int sizeimage;
  94. unsigned int sequence;
  95. struct vim2m_fmt *fmt;
  96. };
  97. enum {
  98. V4L2_M2M_SRC = 0,
  99. V4L2_M2M_DST = 1,
  100. };
  101. #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
  102. #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
  103. static struct vim2m_fmt *find_format(struct v4l2_format *f)
  104. {
  105. struct vim2m_fmt *fmt;
  106. unsigned int k;
  107. for (k = 0; k < NUM_FORMATS; k++) {
  108. fmt = &formats[k];
  109. if (fmt->fourcc == f->fmt.pix.pixelformat)
  110. break;
  111. }
  112. if (k == NUM_FORMATS)
  113. return NULL;
  114. return &formats[k];
  115. }
  116. struct vim2m_dev {
  117. struct v4l2_device v4l2_dev;
  118. struct video_device vfd;
  119. atomic_t num_inst;
  120. struct mutex dev_mutex;
  121. spinlock_t irqlock;
  122. struct timer_list timer;
  123. struct v4l2_m2m_dev *m2m_dev;
  124. };
  125. struct vim2m_ctx {
  126. struct v4l2_fh fh;
  127. struct vim2m_dev *dev;
  128. struct v4l2_ctrl_handler hdl;
  129. /* Processed buffers in this transaction */
  130. u8 num_processed;
  131. /* Transaction length (i.e. how many buffers per transaction) */
  132. u32 translen;
  133. /* Transaction time (i.e. simulated processing time) in milliseconds */
  134. u32 transtime;
  135. /* Abort requested by m2m */
  136. int aborting;
  137. /* Processing mode */
  138. int mode;
  139. enum v4l2_colorspace colorspace;
  140. /* Source and destination queue data */
  141. struct vim2m_q_data q_data[2];
  142. };
  143. static inline struct vim2m_ctx *file2ctx(struct file *file)
  144. {
  145. return container_of(file->private_data, struct vim2m_ctx, fh);
  146. }
  147. static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
  148. enum v4l2_buf_type type)
  149. {
  150. switch (type) {
  151. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  152. return &ctx->q_data[V4L2_M2M_SRC];
  153. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  154. return &ctx->q_data[V4L2_M2M_DST];
  155. default:
  156. BUG();
  157. }
  158. return NULL;
  159. }
  160. static int device_process(struct vim2m_ctx *ctx,
  161. struct vb2_v4l2_buffer *in_vb,
  162. struct vb2_v4l2_buffer *out_vb)
  163. {
  164. struct vim2m_dev *dev = ctx->dev;
  165. struct vim2m_q_data *q_data;
  166. u8 *p_in, *p_out;
  167. int x, y, t, w;
  168. int tile_w, bytes_left;
  169. int width, height, bytesperline;
  170. q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  171. width = q_data->width;
  172. height = q_data->height;
  173. bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  174. p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
  175. p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
  176. if (!p_in || !p_out) {
  177. v4l2_err(&dev->v4l2_dev,
  178. "Acquiring kernel pointers to buffers failed\n");
  179. return -EFAULT;
  180. }
  181. if (vb2_plane_size(&in_vb->vb2_buf, 0) >
  182. vb2_plane_size(&out_vb->vb2_buf, 0)) {
  183. v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
  184. return -EINVAL;
  185. }
  186. tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
  187. / MEM2MEM_NUM_TILES;
  188. bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
  189. w = 0;
  190. out_vb->sequence =
  191. get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
  192. in_vb->sequence = q_data->sequence++;
  193. out_vb->vb2_buf.timestamp = in_vb->vb2_buf.timestamp;
  194. if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE)
  195. out_vb->timecode = in_vb->timecode;
  196. out_vb->field = in_vb->field;
  197. out_vb->flags = in_vb->flags &
  198. (V4L2_BUF_FLAG_TIMECODE |
  199. V4L2_BUF_FLAG_KEYFRAME |
  200. V4L2_BUF_FLAG_PFRAME |
  201. V4L2_BUF_FLAG_BFRAME |
  202. V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
  203. switch (ctx->mode) {
  204. case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
  205. p_out += bytesperline * height - bytes_left;
  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++ +
  211. MEM2MEM_COLOR_STEP;
  212. } else {
  213. for (x = 0; x < tile_w; ++x)
  214. *--p_out = *p_in++ -
  215. MEM2MEM_COLOR_STEP;
  216. }
  217. ++w;
  218. }
  219. p_in += bytes_left;
  220. p_out -= bytes_left;
  221. }
  222. break;
  223. case MEM2MEM_HFLIP:
  224. for (y = 0; y < height; ++y) {
  225. p_out += MEM2MEM_NUM_TILES * tile_w;
  226. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  227. if (w & 0x01) {
  228. for (x = 0; x < tile_w; ++x)
  229. *--p_out = *p_in++ +
  230. MEM2MEM_COLOR_STEP;
  231. } else {
  232. for (x = 0; x < tile_w; ++x)
  233. *--p_out = *p_in++ -
  234. MEM2MEM_COLOR_STEP;
  235. }
  236. ++w;
  237. }
  238. p_in += bytes_left;
  239. p_out += bytesperline;
  240. }
  241. break;
  242. case MEM2MEM_VFLIP:
  243. p_out += bytesperline * (height - 1);
  244. for (y = 0; y < height; ++y) {
  245. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  246. if (w & 0x1) {
  247. for (x = 0; x < tile_w; ++x)
  248. *p_out++ = *p_in++ +
  249. MEM2MEM_COLOR_STEP;
  250. } else {
  251. for (x = 0; x < tile_w; ++x)
  252. *p_out++ = *p_in++ -
  253. MEM2MEM_COLOR_STEP;
  254. }
  255. ++w;
  256. }
  257. p_in += bytes_left;
  258. p_out += bytes_left - 2 * bytesperline;
  259. }
  260. break;
  261. default:
  262. for (y = 0; y < height; ++y) {
  263. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  264. if (w & 0x1) {
  265. for (x = 0; x < tile_w; ++x)
  266. *p_out++ = *p_in++ +
  267. MEM2MEM_COLOR_STEP;
  268. } else {
  269. for (x = 0; x < tile_w; ++x)
  270. *p_out++ = *p_in++ -
  271. MEM2MEM_COLOR_STEP;
  272. }
  273. ++w;
  274. }
  275. p_in += bytes_left;
  276. p_out += bytes_left;
  277. }
  278. }
  279. return 0;
  280. }
  281. static void schedule_irq(struct vim2m_dev *dev, int msec_timeout)
  282. {
  283. dprintk(dev, "Scheduling a simulated irq\n");
  284. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
  285. }
  286. /*
  287. * mem2mem callbacks
  288. */
  289. /**
  290. * job_ready() - check whether an instance is ready to be scheduled to run
  291. */
  292. static int job_ready(void *priv)
  293. {
  294. struct vim2m_ctx *ctx = priv;
  295. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
  296. || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
  297. dprintk(ctx->dev, "Not enough buffers available\n");
  298. return 0;
  299. }
  300. return 1;
  301. }
  302. static void job_abort(void *priv)
  303. {
  304. struct vim2m_ctx *ctx = priv;
  305. /* Will cancel the transaction in the next interrupt handler */
  306. ctx->aborting = 1;
  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 vim2m_ctx *ctx = priv;
  317. struct vim2m_dev *dev = ctx->dev;
  318. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  319. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  320. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.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 vim2m_dev *vim2m_dev = (struct vim2m_dev *)priv;
  328. struct vim2m_ctx *curr_ctx;
  329. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  330. unsigned long flags;
  331. curr_ctx = v4l2_m2m_get_curr_priv(vim2m_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->fh.m2m_ctx);
  337. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  338. curr_ctx->num_processed++;
  339. spin_lock_irqsave(&vim2m_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(&vim2m_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(vim2m_dev->m2m_dev, curr_ctx->fh.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 vim2m_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. f->pixelformat = fmt->fourcc;
  385. return 0;
  386. }
  387. /* Format not found */
  388. return -EINVAL;
  389. }
  390. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  391. struct v4l2_fmtdesc *f)
  392. {
  393. return enum_fmt(f, MEM2MEM_CAPTURE);
  394. }
  395. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  396. struct v4l2_fmtdesc *f)
  397. {
  398. return enum_fmt(f, MEM2MEM_OUTPUT);
  399. }
  400. static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
  401. {
  402. struct vb2_queue *vq;
  403. struct vim2m_q_data *q_data;
  404. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  405. if (!vq)
  406. return -EINVAL;
  407. q_data = get_q_data(ctx, f->type);
  408. f->fmt.pix.width = q_data->width;
  409. f->fmt.pix.height = q_data->height;
  410. f->fmt.pix.field = V4L2_FIELD_NONE;
  411. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  412. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  413. f->fmt.pix.sizeimage = q_data->sizeimage;
  414. f->fmt.pix.colorspace = ctx->colorspace;
  415. return 0;
  416. }
  417. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  418. struct v4l2_format *f)
  419. {
  420. return vidioc_g_fmt(file2ctx(file), f);
  421. }
  422. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  423. struct v4l2_format *f)
  424. {
  425. return vidioc_g_fmt(file2ctx(file), f);
  426. }
  427. static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
  428. {
  429. /* V4L2 specification suggests the driver corrects the format struct
  430. * if any of the dimensions is unsupported */
  431. if (f->fmt.pix.height < MIN_H)
  432. f->fmt.pix.height = MIN_H;
  433. else if (f->fmt.pix.height > MAX_H)
  434. f->fmt.pix.height = MAX_H;
  435. if (f->fmt.pix.width < MIN_W)
  436. f->fmt.pix.width = MIN_W;
  437. else if (f->fmt.pix.width > MAX_W)
  438. f->fmt.pix.width = MAX_W;
  439. f->fmt.pix.width &= ~DIM_ALIGN_MASK;
  440. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  441. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  442. f->fmt.pix.field = V4L2_FIELD_NONE;
  443. return 0;
  444. }
  445. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  446. struct v4l2_format *f)
  447. {
  448. struct vim2m_fmt *fmt;
  449. struct vim2m_ctx *ctx = file2ctx(file);
  450. fmt = find_format(f);
  451. if (!fmt) {
  452. f->fmt.pix.pixelformat = formats[0].fourcc;
  453. fmt = find_format(f);
  454. }
  455. if (!(fmt->types & MEM2MEM_CAPTURE)) {
  456. v4l2_err(&ctx->dev->v4l2_dev,
  457. "Fourcc format (0x%08x) invalid.\n",
  458. f->fmt.pix.pixelformat);
  459. return -EINVAL;
  460. }
  461. f->fmt.pix.colorspace = ctx->colorspace;
  462. return vidioc_try_fmt(f, fmt);
  463. }
  464. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  465. struct v4l2_format *f)
  466. {
  467. struct vim2m_fmt *fmt;
  468. struct vim2m_ctx *ctx = file2ctx(file);
  469. fmt = find_format(f);
  470. if (!fmt) {
  471. f->fmt.pix.pixelformat = formats[0].fourcc;
  472. fmt = find_format(f);
  473. }
  474. if (!(fmt->types & MEM2MEM_OUTPUT)) {
  475. v4l2_err(&ctx->dev->v4l2_dev,
  476. "Fourcc format (0x%08x) invalid.\n",
  477. f->fmt.pix.pixelformat);
  478. return -EINVAL;
  479. }
  480. if (!f->fmt.pix.colorspace)
  481. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  482. return vidioc_try_fmt(f, fmt);
  483. }
  484. static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
  485. {
  486. struct vim2m_q_data *q_data;
  487. struct vb2_queue *vq;
  488. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  489. if (!vq)
  490. return -EINVAL;
  491. q_data = get_q_data(ctx, f->type);
  492. if (!q_data)
  493. return -EINVAL;
  494. if (vb2_is_busy(vq)) {
  495. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  496. return -EBUSY;
  497. }
  498. q_data->fmt = find_format(f);
  499. q_data->width = f->fmt.pix.width;
  500. q_data->height = f->fmt.pix.height;
  501. q_data->sizeimage = q_data->width * q_data->height
  502. * q_data->fmt->depth >> 3;
  503. dprintk(ctx->dev,
  504. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  505. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  506. return 0;
  507. }
  508. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  509. struct v4l2_format *f)
  510. {
  511. int ret;
  512. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  513. if (ret)
  514. return ret;
  515. return vidioc_s_fmt(file2ctx(file), f);
  516. }
  517. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  518. struct v4l2_format *f)
  519. {
  520. struct vim2m_ctx *ctx = file2ctx(file);
  521. int ret;
  522. ret = vidioc_try_fmt_vid_out(file, priv, f);
  523. if (ret)
  524. return ret;
  525. ret = vidioc_s_fmt(file2ctx(file), f);
  526. if (!ret)
  527. ctx->colorspace = f->fmt.pix.colorspace;
  528. return ret;
  529. }
  530. static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
  531. {
  532. struct vim2m_ctx *ctx =
  533. container_of(ctrl->handler, struct vim2m_ctx, hdl);
  534. switch (ctrl->id) {
  535. case V4L2_CID_HFLIP:
  536. if (ctrl->val)
  537. ctx->mode |= MEM2MEM_HFLIP;
  538. else
  539. ctx->mode &= ~MEM2MEM_HFLIP;
  540. break;
  541. case V4L2_CID_VFLIP:
  542. if (ctrl->val)
  543. ctx->mode |= MEM2MEM_VFLIP;
  544. else
  545. ctx->mode &= ~MEM2MEM_VFLIP;
  546. break;
  547. case V4L2_CID_TRANS_TIME_MSEC:
  548. ctx->transtime = ctrl->val;
  549. break;
  550. case V4L2_CID_TRANS_NUM_BUFS:
  551. ctx->translen = ctrl->val;
  552. break;
  553. default:
  554. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  555. return -EINVAL;
  556. }
  557. return 0;
  558. }
  559. static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
  560. .s_ctrl = vim2m_s_ctrl,
  561. };
  562. static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
  563. .vidioc_querycap = vidioc_querycap,
  564. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  565. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  566. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  567. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  568. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  569. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  570. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  571. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  572. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  573. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  574. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  575. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  576. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  577. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  578. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  579. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  580. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  581. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  582. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  583. };
  584. /*
  585. * Queue operations
  586. */
  587. static int vim2m_queue_setup(struct vb2_queue *vq,
  588. unsigned int *nbuffers, unsigned int *nplanes,
  589. unsigned int sizes[], void *alloc_ctxs[])
  590. {
  591. struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
  592. struct vim2m_q_data *q_data;
  593. unsigned int size, count = *nbuffers;
  594. q_data = get_q_data(ctx, vq->type);
  595. size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  596. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  597. (count)--;
  598. *nbuffers = count;
  599. if (*nplanes)
  600. return sizes[0] < size ? -EINVAL : 0;
  601. *nplanes = 1;
  602. sizes[0] = size;
  603. /*
  604. * videobuf2-vmalloc allocator is context-less so no need to set
  605. * alloc_ctxs array.
  606. */
  607. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  608. return 0;
  609. }
  610. static int vim2m_buf_prepare(struct vb2_buffer *vb)
  611. {
  612. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  613. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  614. struct vim2m_q_data *q_data;
  615. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  616. q_data = get_q_data(ctx, vb->vb2_queue->type);
  617. if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  618. if (vbuf->field == V4L2_FIELD_ANY)
  619. vbuf->field = V4L2_FIELD_NONE;
  620. if (vbuf->field != V4L2_FIELD_NONE) {
  621. dprintk(ctx->dev, "%s field isn't supported\n",
  622. __func__);
  623. return -EINVAL;
  624. }
  625. }
  626. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  627. dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
  628. __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
  629. return -EINVAL;
  630. }
  631. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  632. return 0;
  633. }
  634. static void vim2m_buf_queue(struct vb2_buffer *vb)
  635. {
  636. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  637. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  638. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  639. }
  640. static int vim2m_start_streaming(struct vb2_queue *q, unsigned count)
  641. {
  642. struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
  643. struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
  644. q_data->sequence = 0;
  645. return 0;
  646. }
  647. static void vim2m_stop_streaming(struct vb2_queue *q)
  648. {
  649. struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
  650. struct vb2_v4l2_buffer *vbuf;
  651. unsigned long flags;
  652. for (;;) {
  653. if (V4L2_TYPE_IS_OUTPUT(q->type))
  654. vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  655. else
  656. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  657. if (vbuf == NULL)
  658. return;
  659. spin_lock_irqsave(&ctx->dev->irqlock, flags);
  660. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  661. spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
  662. }
  663. }
  664. static struct vb2_ops vim2m_qops = {
  665. .queue_setup = vim2m_queue_setup,
  666. .buf_prepare = vim2m_buf_prepare,
  667. .buf_queue = vim2m_buf_queue,
  668. .start_streaming = vim2m_start_streaming,
  669. .stop_streaming = vim2m_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 vim2m_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 = &vim2m_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 = &vim2m_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 vim2m_ctrl_trans_time_msec = {
  699. .ops = &vim2m_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 vim2m_ctrl_trans_num_bufs = {
  709. .ops = &vim2m_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 vim2m_open(struct file *file)
  722. {
  723. struct vim2m_dev *dev = video_drvdata(file);
  724. struct vim2m_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, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  740. v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  741. v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
  742. v4l2_ctrl_new_custom(hdl, &vim2m_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 vim2m_release(struct file *file)
  775. {
  776. struct vim2m_dev *dev = video_drvdata(file);
  777. struct vim2m_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 vim2m_fops = {
  790. .owner = THIS_MODULE,
  791. .open = vim2m_open,
  792. .release = vim2m_release,
  793. .poll = v4l2_m2m_fop_poll,
  794. .unlocked_ioctl = video_ioctl2,
  795. .mmap = v4l2_m2m_fop_mmap,
  796. };
  797. static struct video_device vim2m_videodev = {
  798. .name = MEM2MEM_NAME,
  799. .vfl_dir = VFL_DIR_M2M,
  800. .fops = &vim2m_fops,
  801. .ioctl_ops = &vim2m_ioctl_ops,
  802. .minor = -1,
  803. .release = video_device_release_empty,
  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 vim2m_probe(struct platform_device *pdev)
  811. {
  812. struct vim2m_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. dev->vfd = vim2m_videodev;
  825. vfd = &dev->vfd;
  826. vfd->lock = &dev->dev_mutex;
  827. vfd->v4l2_dev = &dev->v4l2_dev;
  828. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  829. if (ret) {
  830. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  831. goto unreg_dev;
  832. }
  833. video_set_drvdata(vfd, dev);
  834. snprintf(vfd->name, sizeof(vfd->name), "%s", vim2m_videodev.name);
  835. v4l2_info(&dev->v4l2_dev,
  836. "Device registered as /dev/video%d\n", vfd->num);
  837. setup_timer(&dev->timer, device_isr, (long)dev);
  838. platform_set_drvdata(pdev, dev);
  839. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  840. if (IS_ERR(dev->m2m_dev)) {
  841. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  842. ret = PTR_ERR(dev->m2m_dev);
  843. goto err_m2m;
  844. }
  845. return 0;
  846. err_m2m:
  847. v4l2_m2m_release(dev->m2m_dev);
  848. video_unregister_device(&dev->vfd);
  849. unreg_dev:
  850. v4l2_device_unregister(&dev->v4l2_dev);
  851. return ret;
  852. }
  853. static int vim2m_remove(struct platform_device *pdev)
  854. {
  855. struct vim2m_dev *dev = platform_get_drvdata(pdev);
  856. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
  857. v4l2_m2m_release(dev->m2m_dev);
  858. del_timer_sync(&dev->timer);
  859. video_unregister_device(&dev->vfd);
  860. v4l2_device_unregister(&dev->v4l2_dev);
  861. return 0;
  862. }
  863. static struct platform_driver vim2m_pdrv = {
  864. .probe = vim2m_probe,
  865. .remove = vim2m_remove,
  866. .driver = {
  867. .name = MEM2MEM_NAME,
  868. },
  869. };
  870. static void __exit vim2m_exit(void)
  871. {
  872. platform_driver_unregister(&vim2m_pdrv);
  873. platform_device_unregister(&vim2m_pdev);
  874. }
  875. static int __init vim2m_init(void)
  876. {
  877. int ret;
  878. ret = platform_device_register(&vim2m_pdev);
  879. if (ret)
  880. return ret;
  881. ret = platform_driver_register(&vim2m_pdrv);
  882. if (ret)
  883. platform_device_unregister(&vim2m_pdev);
  884. return ret;
  885. }
  886. module_init(vim2m_init);
  887. module_exit(vim2m_exit);