vim2m.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. 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. enum v4l2_ycbcr_encoding ycbcr_enc;
  141. enum v4l2_xfer_func xfer_func;
  142. enum v4l2_quantization quant;
  143. /* Source and destination queue data */
  144. struct vim2m_q_data q_data[2];
  145. };
  146. static inline struct vim2m_ctx *file2ctx(struct file *file)
  147. {
  148. return container_of(file->private_data, struct vim2m_ctx, fh);
  149. }
  150. static struct vim2m_q_data *get_q_data(struct vim2m_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 vim2m_ctx *ctx,
  164. struct vb2_v4l2_buffer *in_vb,
  165. struct vb2_v4l2_buffer *out_vb)
  166. {
  167. struct vim2m_dev *dev = ctx->dev;
  168. struct vim2m_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->vb2_buf, 0);
  178. p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 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->vb2_buf, 0) >
  185. vb2_plane_size(&out_vb->vb2_buf, 0)) {
  186. v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
  187. return -EINVAL;
  188. }
  189. tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
  190. / MEM2MEM_NUM_TILES;
  191. bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
  192. w = 0;
  193. out_vb->sequence =
  194. get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
  195. in_vb->sequence = q_data->sequence++;
  196. out_vb->vb2_buf.timestamp = in_vb->vb2_buf.timestamp;
  197. if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE)
  198. out_vb->timecode = in_vb->timecode;
  199. out_vb->field = in_vb->field;
  200. out_vb->flags = in_vb->flags &
  201. (V4L2_BUF_FLAG_TIMECODE |
  202. V4L2_BUF_FLAG_KEYFRAME |
  203. V4L2_BUF_FLAG_PFRAME |
  204. V4L2_BUF_FLAG_BFRAME |
  205. V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
  206. switch (ctx->mode) {
  207. case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
  208. p_out += bytesperline * height - bytes_left;
  209. for (y = 0; y < height; ++y) {
  210. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  211. if (w & 0x1) {
  212. for (x = 0; x < tile_w; ++x)
  213. *--p_out = *p_in++ +
  214. MEM2MEM_COLOR_STEP;
  215. } else {
  216. for (x = 0; x < tile_w; ++x)
  217. *--p_out = *p_in++ -
  218. MEM2MEM_COLOR_STEP;
  219. }
  220. ++w;
  221. }
  222. p_in += bytes_left;
  223. p_out -= bytes_left;
  224. }
  225. break;
  226. case MEM2MEM_HFLIP:
  227. for (y = 0; y < height; ++y) {
  228. p_out += MEM2MEM_NUM_TILES * tile_w;
  229. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  230. if (w & 0x01) {
  231. for (x = 0; x < tile_w; ++x)
  232. *--p_out = *p_in++ +
  233. MEM2MEM_COLOR_STEP;
  234. } else {
  235. for (x = 0; x < tile_w; ++x)
  236. *--p_out = *p_in++ -
  237. MEM2MEM_COLOR_STEP;
  238. }
  239. ++w;
  240. }
  241. p_in += bytes_left;
  242. p_out += bytesperline;
  243. }
  244. break;
  245. case MEM2MEM_VFLIP:
  246. p_out += bytesperline * (height - 1);
  247. for (y = 0; y < height; ++y) {
  248. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  249. if (w & 0x1) {
  250. for (x = 0; x < tile_w; ++x)
  251. *p_out++ = *p_in++ +
  252. MEM2MEM_COLOR_STEP;
  253. } else {
  254. for (x = 0; x < tile_w; ++x)
  255. *p_out++ = *p_in++ -
  256. MEM2MEM_COLOR_STEP;
  257. }
  258. ++w;
  259. }
  260. p_in += bytes_left;
  261. p_out += bytes_left - 2 * bytesperline;
  262. }
  263. break;
  264. default:
  265. for (y = 0; y < height; ++y) {
  266. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  267. if (w & 0x1) {
  268. for (x = 0; x < tile_w; ++x)
  269. *p_out++ = *p_in++ +
  270. MEM2MEM_COLOR_STEP;
  271. } else {
  272. for (x = 0; x < tile_w; ++x)
  273. *p_out++ = *p_in++ -
  274. MEM2MEM_COLOR_STEP;
  275. }
  276. ++w;
  277. }
  278. p_in += bytes_left;
  279. p_out += bytes_left;
  280. }
  281. }
  282. return 0;
  283. }
  284. static void schedule_irq(struct vim2m_dev *dev, int msec_timeout)
  285. {
  286. dprintk(dev, "Scheduling a simulated irq\n");
  287. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
  288. }
  289. /*
  290. * mem2mem callbacks
  291. */
  292. /**
  293. * job_ready() - check whether an instance is ready to be scheduled to run
  294. */
  295. static int job_ready(void *priv)
  296. {
  297. struct vim2m_ctx *ctx = priv;
  298. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
  299. || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
  300. dprintk(ctx->dev, "Not enough buffers available\n");
  301. return 0;
  302. }
  303. return 1;
  304. }
  305. static void job_abort(void *priv)
  306. {
  307. struct vim2m_ctx *ctx = priv;
  308. /* Will cancel the transaction in the next interrupt handler */
  309. ctx->aborting = 1;
  310. }
  311. /* device_run() - prepares and starts the device
  312. *
  313. * This simulates all the immediate preparations required before starting
  314. * a device. This will be called by the framework when it decides to schedule
  315. * a particular instance.
  316. */
  317. static void device_run(void *priv)
  318. {
  319. struct vim2m_ctx *ctx = priv;
  320. struct vim2m_dev *dev = ctx->dev;
  321. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  322. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  323. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  324. device_process(ctx, src_buf, dst_buf);
  325. /* Run a timer, which simulates a hardware irq */
  326. schedule_irq(dev, ctx->transtime);
  327. }
  328. static void device_isr(unsigned long priv)
  329. {
  330. struct vim2m_dev *vim2m_dev = (struct vim2m_dev *)priv;
  331. struct vim2m_ctx *curr_ctx;
  332. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  333. unsigned long flags;
  334. curr_ctx = v4l2_m2m_get_curr_priv(vim2m_dev->m2m_dev);
  335. if (NULL == curr_ctx) {
  336. pr_err("Instance released before the end of transaction\n");
  337. return;
  338. }
  339. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  340. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  341. curr_ctx->num_processed++;
  342. spin_lock_irqsave(&vim2m_dev->irqlock, flags);
  343. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  344. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  345. spin_unlock_irqrestore(&vim2m_dev->irqlock, flags);
  346. if (curr_ctx->num_processed == curr_ctx->translen
  347. || curr_ctx->aborting) {
  348. dprintk(curr_ctx->dev, "Finishing transaction\n");
  349. curr_ctx->num_processed = 0;
  350. v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
  351. } else {
  352. device_run(curr_ctx);
  353. }
  354. }
  355. /*
  356. * video ioctls
  357. */
  358. static int vidioc_querycap(struct file *file, void *priv,
  359. struct v4l2_capability *cap)
  360. {
  361. strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
  362. strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
  363. snprintf(cap->bus_info, sizeof(cap->bus_info),
  364. "platform:%s", MEM2MEM_NAME);
  365. cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  366. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  367. return 0;
  368. }
  369. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  370. {
  371. int i, num;
  372. struct vim2m_fmt *fmt;
  373. num = 0;
  374. for (i = 0; i < NUM_FORMATS; ++i) {
  375. if (formats[i].types & type) {
  376. /* index-th format of type type found ? */
  377. if (num == f->index)
  378. break;
  379. /* Correct type but haven't reached our index yet,
  380. * just increment per-type index */
  381. ++num;
  382. }
  383. }
  384. if (i < NUM_FORMATS) {
  385. /* Format found */
  386. fmt = &formats[i];
  387. f->pixelformat = fmt->fourcc;
  388. return 0;
  389. }
  390. /* Format not found */
  391. return -EINVAL;
  392. }
  393. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  394. struct v4l2_fmtdesc *f)
  395. {
  396. return enum_fmt(f, MEM2MEM_CAPTURE);
  397. }
  398. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  399. struct v4l2_fmtdesc *f)
  400. {
  401. return enum_fmt(f, MEM2MEM_OUTPUT);
  402. }
  403. static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
  404. {
  405. struct vb2_queue *vq;
  406. struct vim2m_q_data *q_data;
  407. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  408. if (!vq)
  409. return -EINVAL;
  410. q_data = get_q_data(ctx, f->type);
  411. f->fmt.pix.width = q_data->width;
  412. f->fmt.pix.height = q_data->height;
  413. f->fmt.pix.field = V4L2_FIELD_NONE;
  414. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  415. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  416. f->fmt.pix.sizeimage = q_data->sizeimage;
  417. f->fmt.pix.colorspace = ctx->colorspace;
  418. f->fmt.pix.xfer_func = ctx->xfer_func;
  419. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  420. f->fmt.pix.quantization = ctx->quant;
  421. return 0;
  422. }
  423. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  424. struct v4l2_format *f)
  425. {
  426. return vidioc_g_fmt(file2ctx(file), f);
  427. }
  428. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  429. struct v4l2_format *f)
  430. {
  431. return vidioc_g_fmt(file2ctx(file), f);
  432. }
  433. static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
  434. {
  435. /* V4L2 specification suggests the driver corrects the format struct
  436. * if any of the dimensions is unsupported */
  437. if (f->fmt.pix.height < MIN_H)
  438. f->fmt.pix.height = MIN_H;
  439. else if (f->fmt.pix.height > MAX_H)
  440. f->fmt.pix.height = MAX_H;
  441. if (f->fmt.pix.width < MIN_W)
  442. f->fmt.pix.width = MIN_W;
  443. else if (f->fmt.pix.width > MAX_W)
  444. f->fmt.pix.width = MAX_W;
  445. f->fmt.pix.width &= ~DIM_ALIGN_MASK;
  446. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  447. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  448. f->fmt.pix.field = V4L2_FIELD_NONE;
  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 vim2m_fmt *fmt;
  455. struct vim2m_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. f->fmt.pix.xfer_func = ctx->xfer_func;
  469. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  470. f->fmt.pix.quantization = ctx->quant;
  471. return vidioc_try_fmt(f, fmt);
  472. }
  473. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  474. struct v4l2_format *f)
  475. {
  476. struct vim2m_fmt *fmt;
  477. struct vim2m_ctx *ctx = file2ctx(file);
  478. fmt = find_format(f);
  479. if (!fmt) {
  480. f->fmt.pix.pixelformat = formats[0].fourcc;
  481. fmt = find_format(f);
  482. }
  483. if (!(fmt->types & MEM2MEM_OUTPUT)) {
  484. v4l2_err(&ctx->dev->v4l2_dev,
  485. "Fourcc format (0x%08x) invalid.\n",
  486. f->fmt.pix.pixelformat);
  487. return -EINVAL;
  488. }
  489. if (!f->fmt.pix.colorspace)
  490. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  491. return vidioc_try_fmt(f, fmt);
  492. }
  493. static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
  494. {
  495. struct vim2m_q_data *q_data;
  496. struct vb2_queue *vq;
  497. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  498. if (!vq)
  499. return -EINVAL;
  500. q_data = get_q_data(ctx, f->type);
  501. if (!q_data)
  502. return -EINVAL;
  503. if (vb2_is_busy(vq)) {
  504. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  505. return -EBUSY;
  506. }
  507. q_data->fmt = find_format(f);
  508. q_data->width = f->fmt.pix.width;
  509. q_data->height = f->fmt.pix.height;
  510. q_data->sizeimage = q_data->width * q_data->height
  511. * q_data->fmt->depth >> 3;
  512. dprintk(ctx->dev,
  513. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  514. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  515. return 0;
  516. }
  517. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  518. struct v4l2_format *f)
  519. {
  520. int ret;
  521. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  522. if (ret)
  523. return ret;
  524. return vidioc_s_fmt(file2ctx(file), f);
  525. }
  526. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  527. struct v4l2_format *f)
  528. {
  529. struct vim2m_ctx *ctx = file2ctx(file);
  530. int ret;
  531. ret = vidioc_try_fmt_vid_out(file, priv, f);
  532. if (ret)
  533. return ret;
  534. ret = vidioc_s_fmt(file2ctx(file), f);
  535. if (!ret) {
  536. ctx->colorspace = f->fmt.pix.colorspace;
  537. ctx->xfer_func = f->fmt.pix.xfer_func;
  538. ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
  539. ctx->quant = f->fmt.pix.quantization;
  540. }
  541. return ret;
  542. }
  543. static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
  544. {
  545. struct vim2m_ctx *ctx =
  546. container_of(ctrl->handler, struct vim2m_ctx, hdl);
  547. switch (ctrl->id) {
  548. case V4L2_CID_HFLIP:
  549. if (ctrl->val)
  550. ctx->mode |= MEM2MEM_HFLIP;
  551. else
  552. ctx->mode &= ~MEM2MEM_HFLIP;
  553. break;
  554. case V4L2_CID_VFLIP:
  555. if (ctrl->val)
  556. ctx->mode |= MEM2MEM_VFLIP;
  557. else
  558. ctx->mode &= ~MEM2MEM_VFLIP;
  559. break;
  560. case V4L2_CID_TRANS_TIME_MSEC:
  561. ctx->transtime = ctrl->val;
  562. break;
  563. case V4L2_CID_TRANS_NUM_BUFS:
  564. ctx->translen = ctrl->val;
  565. break;
  566. default:
  567. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  568. return -EINVAL;
  569. }
  570. return 0;
  571. }
  572. static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
  573. .s_ctrl = vim2m_s_ctrl,
  574. };
  575. static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
  576. .vidioc_querycap = vidioc_querycap,
  577. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  578. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  579. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  580. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  581. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  582. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  583. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  584. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  585. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  586. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  587. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  588. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  589. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  590. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  591. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  592. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  593. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  594. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  595. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  596. };
  597. /*
  598. * Queue operations
  599. */
  600. static int vim2m_queue_setup(struct vb2_queue *vq,
  601. unsigned int *nbuffers, unsigned int *nplanes,
  602. unsigned int sizes[], struct device *alloc_devs[])
  603. {
  604. struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
  605. struct vim2m_q_data *q_data;
  606. unsigned int size, count = *nbuffers;
  607. q_data = get_q_data(ctx, vq->type);
  608. size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  609. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  610. (count)--;
  611. *nbuffers = count;
  612. if (*nplanes)
  613. return sizes[0] < size ? -EINVAL : 0;
  614. *nplanes = 1;
  615. sizes[0] = size;
  616. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  617. return 0;
  618. }
  619. static int vim2m_buf_prepare(struct vb2_buffer *vb)
  620. {
  621. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  622. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  623. struct vim2m_q_data *q_data;
  624. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  625. q_data = get_q_data(ctx, vb->vb2_queue->type);
  626. if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  627. if (vbuf->field == V4L2_FIELD_ANY)
  628. vbuf->field = V4L2_FIELD_NONE;
  629. if (vbuf->field != V4L2_FIELD_NONE) {
  630. dprintk(ctx->dev, "%s field isn't supported\n",
  631. __func__);
  632. return -EINVAL;
  633. }
  634. }
  635. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  636. dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
  637. __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
  638. return -EINVAL;
  639. }
  640. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  641. return 0;
  642. }
  643. static void vim2m_buf_queue(struct vb2_buffer *vb)
  644. {
  645. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  646. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  647. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  648. }
  649. static int vim2m_start_streaming(struct vb2_queue *q, unsigned count)
  650. {
  651. struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
  652. struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
  653. q_data->sequence = 0;
  654. return 0;
  655. }
  656. static void vim2m_stop_streaming(struct vb2_queue *q)
  657. {
  658. struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
  659. struct vb2_v4l2_buffer *vbuf;
  660. unsigned long flags;
  661. for (;;) {
  662. if (V4L2_TYPE_IS_OUTPUT(q->type))
  663. vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  664. else
  665. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  666. if (vbuf == NULL)
  667. return;
  668. spin_lock_irqsave(&ctx->dev->irqlock, flags);
  669. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  670. spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
  671. }
  672. }
  673. static const struct vb2_ops vim2m_qops = {
  674. .queue_setup = vim2m_queue_setup,
  675. .buf_prepare = vim2m_buf_prepare,
  676. .buf_queue = vim2m_buf_queue,
  677. .start_streaming = vim2m_start_streaming,
  678. .stop_streaming = vim2m_stop_streaming,
  679. .wait_prepare = vb2_ops_wait_prepare,
  680. .wait_finish = vb2_ops_wait_finish,
  681. };
  682. static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
  683. {
  684. struct vim2m_ctx *ctx = priv;
  685. int ret;
  686. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  687. src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  688. src_vq->drv_priv = ctx;
  689. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  690. src_vq->ops = &vim2m_qops;
  691. src_vq->mem_ops = &vb2_vmalloc_memops;
  692. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  693. src_vq->lock = &ctx->dev->dev_mutex;
  694. ret = vb2_queue_init(src_vq);
  695. if (ret)
  696. return ret;
  697. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  698. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  699. dst_vq->drv_priv = ctx;
  700. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  701. dst_vq->ops = &vim2m_qops;
  702. dst_vq->mem_ops = &vb2_vmalloc_memops;
  703. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  704. dst_vq->lock = &ctx->dev->dev_mutex;
  705. return vb2_queue_init(dst_vq);
  706. }
  707. static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
  708. .ops = &vim2m_ctrl_ops,
  709. .id = V4L2_CID_TRANS_TIME_MSEC,
  710. .name = "Transaction Time (msec)",
  711. .type = V4L2_CTRL_TYPE_INTEGER,
  712. .def = MEM2MEM_DEF_TRANSTIME,
  713. .min = 1,
  714. .max = 10001,
  715. .step = 1,
  716. };
  717. static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
  718. .ops = &vim2m_ctrl_ops,
  719. .id = V4L2_CID_TRANS_NUM_BUFS,
  720. .name = "Buffers Per Transaction",
  721. .type = V4L2_CTRL_TYPE_INTEGER,
  722. .def = 1,
  723. .min = 1,
  724. .max = MEM2MEM_DEF_NUM_BUFS,
  725. .step = 1,
  726. };
  727. /*
  728. * File operations
  729. */
  730. static int vim2m_open(struct file *file)
  731. {
  732. struct vim2m_dev *dev = video_drvdata(file);
  733. struct vim2m_ctx *ctx = NULL;
  734. struct v4l2_ctrl_handler *hdl;
  735. int rc = 0;
  736. if (mutex_lock_interruptible(&dev->dev_mutex))
  737. return -ERESTARTSYS;
  738. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  739. if (!ctx) {
  740. rc = -ENOMEM;
  741. goto open_unlock;
  742. }
  743. v4l2_fh_init(&ctx->fh, video_devdata(file));
  744. file->private_data = &ctx->fh;
  745. ctx->dev = dev;
  746. hdl = &ctx->hdl;
  747. v4l2_ctrl_handler_init(hdl, 4);
  748. v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  749. v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  750. v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
  751. v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
  752. if (hdl->error) {
  753. rc = hdl->error;
  754. v4l2_ctrl_handler_free(hdl);
  755. goto open_unlock;
  756. }
  757. ctx->fh.ctrl_handler = hdl;
  758. v4l2_ctrl_handler_setup(hdl);
  759. ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
  760. ctx->q_data[V4L2_M2M_SRC].width = 640;
  761. ctx->q_data[V4L2_M2M_SRC].height = 480;
  762. ctx->q_data[V4L2_M2M_SRC].sizeimage =
  763. ctx->q_data[V4L2_M2M_SRC].width *
  764. ctx->q_data[V4L2_M2M_SRC].height *
  765. (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
  766. ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
  767. ctx->colorspace = V4L2_COLORSPACE_REC709;
  768. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  769. if (IS_ERR(ctx->fh.m2m_ctx)) {
  770. rc = PTR_ERR(ctx->fh.m2m_ctx);
  771. v4l2_ctrl_handler_free(hdl);
  772. kfree(ctx);
  773. goto open_unlock;
  774. }
  775. v4l2_fh_add(&ctx->fh);
  776. atomic_inc(&dev->num_inst);
  777. dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
  778. ctx, ctx->fh.m2m_ctx);
  779. open_unlock:
  780. mutex_unlock(&dev->dev_mutex);
  781. return rc;
  782. }
  783. static int vim2m_release(struct file *file)
  784. {
  785. struct vim2m_dev *dev = video_drvdata(file);
  786. struct vim2m_ctx *ctx = file2ctx(file);
  787. dprintk(dev, "Releasing instance %p\n", ctx);
  788. v4l2_fh_del(&ctx->fh);
  789. v4l2_fh_exit(&ctx->fh);
  790. v4l2_ctrl_handler_free(&ctx->hdl);
  791. mutex_lock(&dev->dev_mutex);
  792. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  793. mutex_unlock(&dev->dev_mutex);
  794. kfree(ctx);
  795. atomic_dec(&dev->num_inst);
  796. return 0;
  797. }
  798. static const struct v4l2_file_operations vim2m_fops = {
  799. .owner = THIS_MODULE,
  800. .open = vim2m_open,
  801. .release = vim2m_release,
  802. .poll = v4l2_m2m_fop_poll,
  803. .unlocked_ioctl = video_ioctl2,
  804. .mmap = v4l2_m2m_fop_mmap,
  805. };
  806. static struct video_device vim2m_videodev = {
  807. .name = MEM2MEM_NAME,
  808. .vfl_dir = VFL_DIR_M2M,
  809. .fops = &vim2m_fops,
  810. .ioctl_ops = &vim2m_ioctl_ops,
  811. .minor = -1,
  812. .release = video_device_release_empty,
  813. };
  814. static struct v4l2_m2m_ops m2m_ops = {
  815. .device_run = device_run,
  816. .job_ready = job_ready,
  817. .job_abort = job_abort,
  818. };
  819. static int vim2m_probe(struct platform_device *pdev)
  820. {
  821. struct vim2m_dev *dev;
  822. struct video_device *vfd;
  823. int ret;
  824. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  825. if (!dev)
  826. return -ENOMEM;
  827. spin_lock_init(&dev->irqlock);
  828. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  829. if (ret)
  830. return ret;
  831. atomic_set(&dev->num_inst, 0);
  832. mutex_init(&dev->dev_mutex);
  833. dev->vfd = vim2m_videodev;
  834. vfd = &dev->vfd;
  835. vfd->lock = &dev->dev_mutex;
  836. vfd->v4l2_dev = &dev->v4l2_dev;
  837. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  838. if (ret) {
  839. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  840. goto unreg_dev;
  841. }
  842. video_set_drvdata(vfd, dev);
  843. snprintf(vfd->name, sizeof(vfd->name), "%s", vim2m_videodev.name);
  844. v4l2_info(&dev->v4l2_dev,
  845. "Device registered as /dev/video%d\n", vfd->num);
  846. setup_timer(&dev->timer, device_isr, (long)dev);
  847. platform_set_drvdata(pdev, dev);
  848. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  849. if (IS_ERR(dev->m2m_dev)) {
  850. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  851. ret = PTR_ERR(dev->m2m_dev);
  852. goto err_m2m;
  853. }
  854. return 0;
  855. err_m2m:
  856. v4l2_m2m_release(dev->m2m_dev);
  857. video_unregister_device(&dev->vfd);
  858. unreg_dev:
  859. v4l2_device_unregister(&dev->v4l2_dev);
  860. return ret;
  861. }
  862. static int vim2m_remove(struct platform_device *pdev)
  863. {
  864. struct vim2m_dev *dev = platform_get_drvdata(pdev);
  865. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
  866. v4l2_m2m_release(dev->m2m_dev);
  867. del_timer_sync(&dev->timer);
  868. video_unregister_device(&dev->vfd);
  869. v4l2_device_unregister(&dev->v4l2_dev);
  870. return 0;
  871. }
  872. static struct platform_driver vim2m_pdrv = {
  873. .probe = vim2m_probe,
  874. .remove = vim2m_remove,
  875. .driver = {
  876. .name = MEM2MEM_NAME,
  877. },
  878. };
  879. static void __exit vim2m_exit(void)
  880. {
  881. platform_driver_unregister(&vim2m_pdrv);
  882. platform_device_unregister(&vim2m_pdev);
  883. }
  884. static int __init vim2m_init(void)
  885. {
  886. int ret;
  887. ret = platform_device_register(&vim2m_pdev);
  888. if (ret)
  889. return ret;
  890. ret = platform_driver_register(&vim2m_pdrv);
  891. if (ret)
  892. platform_device_unregister(&vim2m_pdev);
  893. return ret;
  894. }
  895. module_init(vim2m_init);
  896. module_exit(vim2m_exit);