vim2m.c 27 KB

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