mem2mem_testdev.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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/videobuf2-vmalloc.h>
  31. #define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
  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. #define MIN_W 32
  37. #define MIN_H 32
  38. #define MAX_W 640
  39. #define MAX_H 480
  40. #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
  41. /* Flags that indicate a format can be used for capture/output */
  42. #define MEM2MEM_CAPTURE (1 << 0)
  43. #define MEM2MEM_OUTPUT (1 << 1)
  44. #define MEM2MEM_NAME "m2m-testdev"
  45. /* Per queue */
  46. #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
  47. /* In bytes, per queue */
  48. #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
  49. /* Default transaction time in msec */
  50. #define MEM2MEM_DEF_TRANSTIME 1000
  51. /* Default number of buffers per transaction */
  52. #define MEM2MEM_DEF_TRANSLEN 1
  53. #define MEM2MEM_COLOR_STEP (0xff >> 4)
  54. #define MEM2MEM_NUM_TILES 8
  55. /* Flags that indicate processing mode */
  56. #define MEM2MEM_HFLIP (1 << 0)
  57. #define MEM2MEM_VFLIP (1 << 1)
  58. #define dprintk(dev, fmt, arg...) \
  59. v4l2_dbg(1, 1, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
  60. void m2mtest_dev_release(struct device *dev)
  61. {}
  62. static struct platform_device m2mtest_pdev = {
  63. .name = MEM2MEM_NAME,
  64. .dev.release = m2mtest_dev_release,
  65. };
  66. struct m2mtest_fmt {
  67. char *name;
  68. u32 fourcc;
  69. int depth;
  70. /* Types the format can be used for */
  71. u32 types;
  72. };
  73. static struct m2mtest_fmt formats[] = {
  74. {
  75. .name = "RGB565 (BE)",
  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. .name = "4:2:2, packed, YUYV",
  83. .fourcc = V4L2_PIX_FMT_YUYV,
  84. .depth = 16,
  85. /* Output-only format */
  86. .types = MEM2MEM_OUTPUT,
  87. },
  88. };
  89. #define NUM_FORMATS ARRAY_SIZE(formats)
  90. /* Per-queue, driver-specific private data */
  91. struct m2mtest_q_data {
  92. unsigned int width;
  93. unsigned int height;
  94. unsigned int sizeimage;
  95. struct m2mtest_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 m2mtest_fmt *find_format(struct v4l2_format *f)
  104. {
  105. struct m2mtest_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 m2mtest_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 m2mtest_ctx {
  126. struct v4l2_fh fh;
  127. struct m2mtest_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. struct v4l2_m2m_ctx *m2m_ctx;
  140. /* Source and destination queue data */
  141. struct m2mtest_q_data q_data[2];
  142. };
  143. static inline struct m2mtest_ctx *file2ctx(struct file *file)
  144. {
  145. return container_of(file->private_data, struct m2mtest_ctx, fh);
  146. }
  147. static struct m2mtest_q_data *get_q_data(struct m2mtest_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 m2mtest_ctx *ctx,
  161. struct vb2_buffer *in_vb,
  162. struct vb2_buffer *out_vb)
  163. {
  164. struct m2mtest_dev *dev = ctx->dev;
  165. struct m2mtest_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. switch (ctx->mode) {
  190. case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
  191. p_out += bytesperline * height - bytes_left;
  192. for (y = 0; y < height; ++y) {
  193. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  194. if (w & 0x1) {
  195. for (x = 0; x < tile_w; ++x)
  196. *--p_out = *p_in++ +
  197. MEM2MEM_COLOR_STEP;
  198. } else {
  199. for (x = 0; x < tile_w; ++x)
  200. *--p_out = *p_in++ -
  201. MEM2MEM_COLOR_STEP;
  202. }
  203. ++w;
  204. }
  205. p_in += bytes_left;
  206. p_out -= bytes_left;
  207. }
  208. break;
  209. case MEM2MEM_HFLIP:
  210. for (y = 0; y < height; ++y) {
  211. p_out += MEM2MEM_NUM_TILES * tile_w;
  212. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  213. if (w & 0x01) {
  214. for (x = 0; x < tile_w; ++x)
  215. *--p_out = *p_in++ +
  216. MEM2MEM_COLOR_STEP;
  217. } else {
  218. for (x = 0; x < tile_w; ++x)
  219. *--p_out = *p_in++ -
  220. MEM2MEM_COLOR_STEP;
  221. }
  222. ++w;
  223. }
  224. p_in += bytes_left;
  225. p_out += bytesperline;
  226. }
  227. break;
  228. case MEM2MEM_VFLIP:
  229. p_out += bytesperline * (height - 1);
  230. for (y = 0; y < height; ++y) {
  231. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  232. if (w & 0x1) {
  233. for (x = 0; x < tile_w; ++x)
  234. *p_out++ = *p_in++ +
  235. MEM2MEM_COLOR_STEP;
  236. } else {
  237. for (x = 0; x < tile_w; ++x)
  238. *p_out++ = *p_in++ -
  239. MEM2MEM_COLOR_STEP;
  240. }
  241. ++w;
  242. }
  243. p_in += bytes_left;
  244. p_out += bytes_left - 2 * bytesperline;
  245. }
  246. break;
  247. default:
  248. for (y = 0; y < height; ++y) {
  249. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  250. if (w & 0x1) {
  251. for (x = 0; x < tile_w; ++x)
  252. *p_out++ = *p_in++ +
  253. MEM2MEM_COLOR_STEP;
  254. } else {
  255. for (x = 0; x < tile_w; ++x)
  256. *p_out++ = *p_in++ -
  257. MEM2MEM_COLOR_STEP;
  258. }
  259. ++w;
  260. }
  261. p_in += bytes_left;
  262. p_out += bytes_left;
  263. }
  264. }
  265. return 0;
  266. }
  267. static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
  268. {
  269. dprintk(dev, "Scheduling a simulated irq\n");
  270. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
  271. }
  272. /*
  273. * mem2mem callbacks
  274. */
  275. /**
  276. * job_ready() - check whether an instance is ready to be scheduled to run
  277. */
  278. static int job_ready(void *priv)
  279. {
  280. struct m2mtest_ctx *ctx = priv;
  281. if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < ctx->translen
  282. || v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) < ctx->translen) {
  283. dprintk(ctx->dev, "Not enough buffers available\n");
  284. return 0;
  285. }
  286. return 1;
  287. }
  288. static void job_abort(void *priv)
  289. {
  290. struct m2mtest_ctx *ctx = priv;
  291. /* Will cancel the transaction in the next interrupt handler */
  292. ctx->aborting = 1;
  293. }
  294. static void m2mtest_lock(void *priv)
  295. {
  296. struct m2mtest_ctx *ctx = priv;
  297. struct m2mtest_dev *dev = ctx->dev;
  298. mutex_lock(&dev->dev_mutex);
  299. }
  300. static void m2mtest_unlock(void *priv)
  301. {
  302. struct m2mtest_ctx *ctx = priv;
  303. struct m2mtest_dev *dev = ctx->dev;
  304. mutex_unlock(&dev->dev_mutex);
  305. }
  306. /* device_run() - prepares and starts the device
  307. *
  308. * This simulates all the immediate preparations required before starting
  309. * a device. This will be called by the framework when it decides to schedule
  310. * a particular instance.
  311. */
  312. static void device_run(void *priv)
  313. {
  314. struct m2mtest_ctx *ctx = priv;
  315. struct m2mtest_dev *dev = ctx->dev;
  316. struct vb2_buffer *src_buf, *dst_buf;
  317. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  318. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  319. device_process(ctx, src_buf, dst_buf);
  320. /* Run a timer, which simulates a hardware irq */
  321. schedule_irq(dev, ctx->transtime);
  322. }
  323. static void device_isr(unsigned long priv)
  324. {
  325. struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
  326. struct m2mtest_ctx *curr_ctx;
  327. struct vb2_buffer *src_vb, *dst_vb;
  328. unsigned long flags;
  329. curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
  330. if (NULL == curr_ctx) {
  331. printk(KERN_ERR
  332. "Instance released before the end of transaction\n");
  333. return;
  334. }
  335. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  336. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  337. curr_ctx->num_processed++;
  338. spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
  339. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  340. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  341. spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
  342. if (curr_ctx->num_processed == curr_ctx->translen
  343. || curr_ctx->aborting) {
  344. dprintk(curr_ctx->dev, "Finishing transaction\n");
  345. curr_ctx->num_processed = 0;
  346. v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->m2m_ctx);
  347. } else {
  348. device_run(curr_ctx);
  349. }
  350. }
  351. /*
  352. * video ioctls
  353. */
  354. static int vidioc_querycap(struct file *file, void *priv,
  355. struct v4l2_capability *cap)
  356. {
  357. strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
  358. strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
  359. cap->bus_info[0] = 0;
  360. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
  361. | V4L2_CAP_STREAMING;
  362. return 0;
  363. }
  364. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  365. {
  366. int i, num;
  367. struct m2mtest_fmt *fmt;
  368. num = 0;
  369. for (i = 0; i < NUM_FORMATS; ++i) {
  370. if (formats[i].types & type) {
  371. /* index-th format of type type found ? */
  372. if (num == f->index)
  373. break;
  374. /* Correct type but haven't reached our index yet,
  375. * just increment per-type index */
  376. ++num;
  377. }
  378. }
  379. if (i < NUM_FORMATS) {
  380. /* Format found */
  381. fmt = &formats[i];
  382. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  383. f->pixelformat = fmt->fourcc;
  384. return 0;
  385. }
  386. /* Format not found */
  387. return -EINVAL;
  388. }
  389. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  390. struct v4l2_fmtdesc *f)
  391. {
  392. return enum_fmt(f, MEM2MEM_CAPTURE);
  393. }
  394. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  395. struct v4l2_fmtdesc *f)
  396. {
  397. return enum_fmt(f, MEM2MEM_OUTPUT);
  398. }
  399. static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  400. {
  401. struct vb2_queue *vq;
  402. struct m2mtest_q_data *q_data;
  403. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  404. if (!vq)
  405. return -EINVAL;
  406. q_data = get_q_data(ctx, f->type);
  407. f->fmt.pix.width = q_data->width;
  408. f->fmt.pix.height = q_data->height;
  409. f->fmt.pix.field = V4L2_FIELD_NONE;
  410. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  411. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  412. f->fmt.pix.sizeimage = q_data->sizeimage;
  413. return 0;
  414. }
  415. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  416. struct v4l2_format *f)
  417. {
  418. return vidioc_g_fmt(file2ctx(file), f);
  419. }
  420. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  421. struct v4l2_format *f)
  422. {
  423. return vidioc_g_fmt(file2ctx(file), f);
  424. }
  425. static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
  426. {
  427. enum v4l2_field field;
  428. field = f->fmt.pix.field;
  429. if (field == V4L2_FIELD_ANY)
  430. field = V4L2_FIELD_NONE;
  431. else if (V4L2_FIELD_NONE != field)
  432. return -EINVAL;
  433. /* V4L2 specification suggests the driver corrects the format struct
  434. * if any of the dimensions is unsupported */
  435. f->fmt.pix.field = field;
  436. if (f->fmt.pix.height < MIN_H)
  437. f->fmt.pix.height = MIN_H;
  438. else if (f->fmt.pix.height > MAX_H)
  439. f->fmt.pix.height = MAX_H;
  440. if (f->fmt.pix.width < MIN_W)
  441. f->fmt.pix.width = MIN_W;
  442. else if (f->fmt.pix.width > MAX_W)
  443. f->fmt.pix.width = MAX_W;
  444. f->fmt.pix.width &= ~DIM_ALIGN_MASK;
  445. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  446. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  447. return 0;
  448. }
  449. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  450. struct v4l2_format *f)
  451. {
  452. struct m2mtest_fmt *fmt;
  453. struct m2mtest_ctx *ctx = file2ctx(file);
  454. fmt = find_format(f);
  455. if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
  456. v4l2_err(&ctx->dev->v4l2_dev,
  457. "Fourcc format (0x%08x) invalid.\n",
  458. f->fmt.pix.pixelformat);
  459. return -EINVAL;
  460. }
  461. return vidioc_try_fmt(f, fmt);
  462. }
  463. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  464. struct v4l2_format *f)
  465. {
  466. struct m2mtest_fmt *fmt;
  467. struct m2mtest_ctx *ctx = file2ctx(file);
  468. fmt = find_format(f);
  469. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
  470. v4l2_err(&ctx->dev->v4l2_dev,
  471. "Fourcc format (0x%08x) invalid.\n",
  472. f->fmt.pix.pixelformat);
  473. return -EINVAL;
  474. }
  475. return vidioc_try_fmt(f, fmt);
  476. }
  477. static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  478. {
  479. struct m2mtest_q_data *q_data;
  480. struct vb2_queue *vq;
  481. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  482. if (!vq)
  483. return -EINVAL;
  484. q_data = get_q_data(ctx, f->type);
  485. if (!q_data)
  486. return -EINVAL;
  487. if (vb2_is_busy(vq)) {
  488. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  489. return -EBUSY;
  490. }
  491. q_data->fmt = find_format(f);
  492. q_data->width = f->fmt.pix.width;
  493. q_data->height = f->fmt.pix.height;
  494. q_data->sizeimage = q_data->width * q_data->height
  495. * q_data->fmt->depth >> 3;
  496. dprintk(ctx->dev,
  497. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  498. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  499. return 0;
  500. }
  501. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  502. struct v4l2_format *f)
  503. {
  504. int ret;
  505. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  506. if (ret)
  507. return ret;
  508. return vidioc_s_fmt(file2ctx(file), f);
  509. }
  510. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  511. struct v4l2_format *f)
  512. {
  513. int ret;
  514. ret = vidioc_try_fmt_vid_out(file, priv, f);
  515. if (ret)
  516. return ret;
  517. return vidioc_s_fmt(file2ctx(file), f);
  518. }
  519. static int vidioc_reqbufs(struct file *file, void *priv,
  520. struct v4l2_requestbuffers *reqbufs)
  521. {
  522. struct m2mtest_ctx *ctx = file2ctx(file);
  523. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  524. }
  525. static int vidioc_querybuf(struct file *file, void *priv,
  526. struct v4l2_buffer *buf)
  527. {
  528. struct m2mtest_ctx *ctx = file2ctx(file);
  529. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  530. }
  531. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  532. {
  533. struct m2mtest_ctx *ctx = file2ctx(file);
  534. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  535. }
  536. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  537. {
  538. struct m2mtest_ctx *ctx = file2ctx(file);
  539. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  540. }
  541. static int vidioc_streamon(struct file *file, void *priv,
  542. enum v4l2_buf_type type)
  543. {
  544. struct m2mtest_ctx *ctx = file2ctx(file);
  545. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  546. }
  547. static int vidioc_streamoff(struct file *file, void *priv,
  548. enum v4l2_buf_type type)
  549. {
  550. struct m2mtest_ctx *ctx = file2ctx(file);
  551. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  552. }
  553. static int m2mtest_s_ctrl(struct v4l2_ctrl *ctrl)
  554. {
  555. struct m2mtest_ctx *ctx =
  556. container_of(ctrl->handler, struct m2mtest_ctx, hdl);
  557. switch (ctrl->id) {
  558. case V4L2_CID_HFLIP:
  559. if (ctrl->val)
  560. ctx->mode |= MEM2MEM_HFLIP;
  561. else
  562. ctx->mode &= ~MEM2MEM_HFLIP;
  563. break;
  564. case V4L2_CID_VFLIP:
  565. if (ctrl->val)
  566. ctx->mode |= MEM2MEM_VFLIP;
  567. else
  568. ctx->mode &= ~MEM2MEM_VFLIP;
  569. break;
  570. case V4L2_CID_TRANS_TIME_MSEC:
  571. ctx->transtime = ctrl->val;
  572. break;
  573. case V4L2_CID_TRANS_NUM_BUFS:
  574. ctx->translen = ctrl->val;
  575. break;
  576. default:
  577. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  578. return -EINVAL;
  579. }
  580. return 0;
  581. }
  582. static const struct v4l2_ctrl_ops m2mtest_ctrl_ops = {
  583. .s_ctrl = m2mtest_s_ctrl,
  584. };
  585. static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
  586. .vidioc_querycap = vidioc_querycap,
  587. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  588. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  589. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  590. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  591. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  592. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  593. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  594. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  595. .vidioc_reqbufs = vidioc_reqbufs,
  596. .vidioc_querybuf = vidioc_querybuf,
  597. .vidioc_qbuf = vidioc_qbuf,
  598. .vidioc_dqbuf = vidioc_dqbuf,
  599. .vidioc_streamon = vidioc_streamon,
  600. .vidioc_streamoff = vidioc_streamoff,
  601. };
  602. /*
  603. * Queue operations
  604. */
  605. static int m2mtest_queue_setup(struct vb2_queue *vq,
  606. const struct v4l2_format *fmt,
  607. unsigned int *nbuffers, unsigned int *nplanes,
  608. unsigned int sizes[], void *alloc_ctxs[])
  609. {
  610. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
  611. struct m2mtest_q_data *q_data;
  612. unsigned int size, count = *nbuffers;
  613. q_data = get_q_data(ctx, vq->type);
  614. size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  615. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  616. (count)--;
  617. *nplanes = 1;
  618. *nbuffers = count;
  619. sizes[0] = size;
  620. /*
  621. * videobuf2-vmalloc allocator is context-less so no need to set
  622. * alloc_ctxs array.
  623. */
  624. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  625. return 0;
  626. }
  627. static int m2mtest_buf_prepare(struct vb2_buffer *vb)
  628. {
  629. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  630. struct m2mtest_q_data *q_data;
  631. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  632. q_data = get_q_data(ctx, vb->vb2_queue->type);
  633. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  634. dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
  635. __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
  636. return -EINVAL;
  637. }
  638. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  639. return 0;
  640. }
  641. static void m2mtest_buf_queue(struct vb2_buffer *vb)
  642. {
  643. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  644. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  645. }
  646. static void m2mtest_wait_prepare(struct vb2_queue *q)
  647. {
  648. struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
  649. m2mtest_unlock(ctx);
  650. }
  651. static void m2mtest_wait_finish(struct vb2_queue *q)
  652. {
  653. struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
  654. m2mtest_lock(ctx);
  655. }
  656. static struct vb2_ops m2mtest_qops = {
  657. .queue_setup = m2mtest_queue_setup,
  658. .buf_prepare = m2mtest_buf_prepare,
  659. .buf_queue = m2mtest_buf_queue,
  660. .wait_prepare = m2mtest_wait_prepare,
  661. .wait_finish = m2mtest_wait_finish,
  662. };
  663. static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
  664. {
  665. struct m2mtest_ctx *ctx = priv;
  666. int ret;
  667. memset(src_vq, 0, sizeof(*src_vq));
  668. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  669. src_vq->io_modes = VB2_MMAP;
  670. src_vq->drv_priv = ctx;
  671. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  672. src_vq->ops = &m2mtest_qops;
  673. src_vq->mem_ops = &vb2_vmalloc_memops;
  674. ret = vb2_queue_init(src_vq);
  675. if (ret)
  676. return ret;
  677. memset(dst_vq, 0, sizeof(*dst_vq));
  678. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  679. dst_vq->io_modes = VB2_MMAP;
  680. dst_vq->drv_priv = ctx;
  681. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  682. dst_vq->ops = &m2mtest_qops;
  683. dst_vq->mem_ops = &vb2_vmalloc_memops;
  684. return vb2_queue_init(dst_vq);
  685. }
  686. static const struct v4l2_ctrl_config m2mtest_ctrl_trans_time_msec = {
  687. .ops = &m2mtest_ctrl_ops,
  688. .id = V4L2_CID_TRANS_TIME_MSEC,
  689. .name = "Transaction Time (msec)",
  690. .type = V4L2_CTRL_TYPE_INTEGER,
  691. .def = 1001,
  692. .min = 1,
  693. .max = 10001,
  694. .step = 100,
  695. };
  696. static const struct v4l2_ctrl_config m2mtest_ctrl_trans_num_bufs = {
  697. .ops = &m2mtest_ctrl_ops,
  698. .id = V4L2_CID_TRANS_NUM_BUFS,
  699. .name = "Buffers Per Transaction",
  700. .type = V4L2_CTRL_TYPE_INTEGER,
  701. .def = 1,
  702. .min = 1,
  703. .max = MEM2MEM_DEF_NUM_BUFS,
  704. .step = 1,
  705. };
  706. /*
  707. * File operations
  708. */
  709. static int m2mtest_open(struct file *file)
  710. {
  711. struct m2mtest_dev *dev = video_drvdata(file);
  712. struct m2mtest_ctx *ctx = NULL;
  713. struct v4l2_ctrl_handler *hdl;
  714. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  715. if (!ctx)
  716. return -ENOMEM;
  717. v4l2_fh_init(&ctx->fh, video_devdata(file));
  718. file->private_data = &ctx->fh;
  719. ctx->dev = dev;
  720. hdl = &ctx->hdl;
  721. v4l2_ctrl_handler_init(hdl, 4);
  722. v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  723. v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  724. v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_time_msec, NULL);
  725. v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_num_bufs, NULL);
  726. if (hdl->error) {
  727. int err = hdl->error;
  728. v4l2_ctrl_handler_free(hdl);
  729. return err;
  730. }
  731. ctx->fh.ctrl_handler = hdl;
  732. v4l2_ctrl_handler_setup(hdl);
  733. ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
  734. ctx->q_data[V4L2_M2M_DST].fmt = &formats[0];
  735. ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  736. if (IS_ERR(ctx->m2m_ctx)) {
  737. int ret = PTR_ERR(ctx->m2m_ctx);
  738. v4l2_ctrl_handler_free(hdl);
  739. kfree(ctx);
  740. return ret;
  741. }
  742. v4l2_fh_add(&ctx->fh);
  743. atomic_inc(&dev->num_inst);
  744. dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
  745. return 0;
  746. }
  747. static int m2mtest_release(struct file *file)
  748. {
  749. struct m2mtest_dev *dev = video_drvdata(file);
  750. struct m2mtest_ctx *ctx = file2ctx(file);
  751. dprintk(dev, "Releasing instance %p\n", ctx);
  752. v4l2_fh_del(&ctx->fh);
  753. v4l2_fh_exit(&ctx->fh);
  754. v4l2_ctrl_handler_free(&ctx->hdl);
  755. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  756. kfree(ctx);
  757. atomic_dec(&dev->num_inst);
  758. return 0;
  759. }
  760. static unsigned int m2mtest_poll(struct file *file,
  761. struct poll_table_struct *wait)
  762. {
  763. struct m2mtest_ctx *ctx = file2ctx(file);
  764. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  765. }
  766. static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
  767. {
  768. struct m2mtest_ctx *ctx = file2ctx(file);
  769. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  770. }
  771. static const struct v4l2_file_operations m2mtest_fops = {
  772. .owner = THIS_MODULE,
  773. .open = m2mtest_open,
  774. .release = m2mtest_release,
  775. .poll = m2mtest_poll,
  776. .unlocked_ioctl = video_ioctl2,
  777. .mmap = m2mtest_mmap,
  778. };
  779. static struct video_device m2mtest_videodev = {
  780. .name = MEM2MEM_NAME,
  781. .fops = &m2mtest_fops,
  782. .ioctl_ops = &m2mtest_ioctl_ops,
  783. .minor = -1,
  784. .release = video_device_release,
  785. };
  786. static struct v4l2_m2m_ops m2m_ops = {
  787. .device_run = device_run,
  788. .job_ready = job_ready,
  789. .job_abort = job_abort,
  790. .lock = m2mtest_lock,
  791. .unlock = m2mtest_unlock,
  792. };
  793. static int m2mtest_probe(struct platform_device *pdev)
  794. {
  795. struct m2mtest_dev *dev;
  796. struct video_device *vfd;
  797. int ret;
  798. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  799. if (!dev)
  800. return -ENOMEM;
  801. spin_lock_init(&dev->irqlock);
  802. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  803. if (ret)
  804. goto free_dev;
  805. atomic_set(&dev->num_inst, 0);
  806. mutex_init(&dev->dev_mutex);
  807. vfd = video_device_alloc();
  808. if (!vfd) {
  809. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  810. ret = -ENOMEM;
  811. goto unreg_dev;
  812. }
  813. *vfd = m2mtest_videodev;
  814. /* Locking in file operations other than ioctl should be done
  815. by the driver, not the V4L2 core.
  816. This driver needs auditing so that this flag can be removed. */
  817. set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
  818. vfd->lock = &dev->dev_mutex;
  819. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  820. if (ret) {
  821. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  822. goto rel_vdev;
  823. }
  824. video_set_drvdata(vfd, dev);
  825. snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
  826. dev->vfd = vfd;
  827. v4l2_info(&dev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
  828. "Device registered as /dev/video%d\n", vfd->num);
  829. setup_timer(&dev->timer, device_isr, (long)dev);
  830. platform_set_drvdata(pdev, dev);
  831. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  832. if (IS_ERR(dev->m2m_dev)) {
  833. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  834. ret = PTR_ERR(dev->m2m_dev);
  835. goto err_m2m;
  836. }
  837. return 0;
  838. v4l2_m2m_release(dev->m2m_dev);
  839. err_m2m:
  840. video_unregister_device(dev->vfd);
  841. rel_vdev:
  842. video_device_release(vfd);
  843. unreg_dev:
  844. v4l2_device_unregister(&dev->v4l2_dev);
  845. free_dev:
  846. kfree(dev);
  847. return ret;
  848. }
  849. static int m2mtest_remove(struct platform_device *pdev)
  850. {
  851. struct m2mtest_dev *dev =
  852. (struct m2mtest_dev *)platform_get_drvdata(pdev);
  853. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
  854. v4l2_m2m_release(dev->m2m_dev);
  855. del_timer_sync(&dev->timer);
  856. video_unregister_device(dev->vfd);
  857. v4l2_device_unregister(&dev->v4l2_dev);
  858. kfree(dev);
  859. return 0;
  860. }
  861. static struct platform_driver m2mtest_pdrv = {
  862. .probe = m2mtest_probe,
  863. .remove = m2mtest_remove,
  864. .driver = {
  865. .name = MEM2MEM_NAME,
  866. .owner = THIS_MODULE,
  867. },
  868. };
  869. static void __exit m2mtest_exit(void)
  870. {
  871. platform_driver_unregister(&m2mtest_pdrv);
  872. platform_device_unregister(&m2mtest_pdev);
  873. }
  874. static int __init m2mtest_init(void)
  875. {
  876. int ret;
  877. ret = platform_device_register(&m2mtest_pdev);
  878. if (ret)
  879. return ret;
  880. ret = platform_driver_register(&m2mtest_pdrv);
  881. if (ret)
  882. platform_device_unregister(&m2mtest_pdev);
  883. return 0;
  884. }
  885. module_init(m2mtest_init);
  886. module_exit(m2mtest_exit);