vim2m.c 26 KB

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