mem2mem_testdev.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. #define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
  33. MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
  34. MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  35. MODULE_LICENSE("GPL");
  36. MODULE_VERSION("0.1.1");
  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 "m2m-testdev"
  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 1000
  55. /* Default number of buffers per transaction */
  56. #define MEM2MEM_DEF_TRANSLEN 1
  57. #define MEM2MEM_COLOR_STEP (0xff >> 4)
  58. #define MEM2MEM_NUM_TILES 8
  59. /* Flags that indicate processing mode */
  60. #define MEM2MEM_HFLIP (1 << 0)
  61. #define MEM2MEM_VFLIP (1 << 1)
  62. #define dprintk(dev, fmt, arg...) \
  63. v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
  64. static void m2mtest_dev_release(struct device *dev)
  65. {}
  66. static struct platform_device m2mtest_pdev = {
  67. .name = MEM2MEM_NAME,
  68. .dev.release = m2mtest_dev_release,
  69. };
  70. struct m2mtest_fmt {
  71. char *name;
  72. u32 fourcc;
  73. int depth;
  74. /* Types the format can be used for */
  75. u32 types;
  76. };
  77. static struct m2mtest_fmt formats[] = {
  78. {
  79. .name = "RGB565 (BE)",
  80. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  81. .depth = 16,
  82. /* Both capture and output format */
  83. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  84. },
  85. {
  86. .name = "4:2:2, packed, YUYV",
  87. .fourcc = V4L2_PIX_FMT_YUYV,
  88. .depth = 16,
  89. /* Output-only format */
  90. .types = MEM2MEM_OUTPUT,
  91. },
  92. };
  93. #define NUM_FORMATS ARRAY_SIZE(formats)
  94. /* Per-queue, driver-specific private data */
  95. struct m2mtest_q_data {
  96. unsigned int width;
  97. unsigned int height;
  98. unsigned int sizeimage;
  99. struct m2mtest_fmt *fmt;
  100. };
  101. enum {
  102. V4L2_M2M_SRC = 0,
  103. V4L2_M2M_DST = 1,
  104. };
  105. #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
  106. #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
  107. static struct m2mtest_fmt *find_format(struct v4l2_format *f)
  108. {
  109. struct m2mtest_fmt *fmt;
  110. unsigned int k;
  111. for (k = 0; k < NUM_FORMATS; k++) {
  112. fmt = &formats[k];
  113. if (fmt->fourcc == f->fmt.pix.pixelformat)
  114. break;
  115. }
  116. if (k == NUM_FORMATS)
  117. return NULL;
  118. return &formats[k];
  119. }
  120. struct m2mtest_dev {
  121. struct v4l2_device v4l2_dev;
  122. struct video_device *vfd;
  123. atomic_t num_inst;
  124. struct mutex dev_mutex;
  125. spinlock_t irqlock;
  126. struct timer_list timer;
  127. struct v4l2_m2m_dev *m2m_dev;
  128. };
  129. struct m2mtest_ctx {
  130. struct v4l2_fh fh;
  131. struct m2mtest_dev *dev;
  132. struct v4l2_ctrl_handler hdl;
  133. /* Processed buffers in this transaction */
  134. u8 num_processed;
  135. /* Transaction length (i.e. how many buffers per transaction) */
  136. u32 translen;
  137. /* Transaction time (i.e. simulated processing time) in milliseconds */
  138. u32 transtime;
  139. /* Abort requested by m2m */
  140. int aborting;
  141. /* Processing mode */
  142. int mode;
  143. enum v4l2_colorspace colorspace;
  144. struct v4l2_m2m_ctx *m2m_ctx;
  145. /* Source and destination queue data */
  146. struct m2mtest_q_data q_data[2];
  147. };
  148. static inline struct m2mtest_ctx *file2ctx(struct file *file)
  149. {
  150. return container_of(file->private_data, struct m2mtest_ctx, fh);
  151. }
  152. static struct m2mtest_q_data *get_q_data(struct m2mtest_ctx *ctx,
  153. enum v4l2_buf_type type)
  154. {
  155. switch (type) {
  156. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  157. return &ctx->q_data[V4L2_M2M_SRC];
  158. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  159. return &ctx->q_data[V4L2_M2M_DST];
  160. default:
  161. BUG();
  162. }
  163. return NULL;
  164. }
  165. static int device_process(struct m2mtest_ctx *ctx,
  166. struct vb2_buffer *in_vb,
  167. struct vb2_buffer *out_vb)
  168. {
  169. struct m2mtest_dev *dev = ctx->dev;
  170. struct m2mtest_q_data *q_data;
  171. u8 *p_in, *p_out;
  172. int x, y, t, w;
  173. int tile_w, bytes_left;
  174. int width, height, bytesperline;
  175. q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  176. width = q_data->width;
  177. height = q_data->height;
  178. bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  179. p_in = vb2_plane_vaddr(in_vb, 0);
  180. p_out = vb2_plane_vaddr(out_vb, 0);
  181. if (!p_in || !p_out) {
  182. v4l2_err(&dev->v4l2_dev,
  183. "Acquiring kernel pointers to buffers failed\n");
  184. return -EFAULT;
  185. }
  186. if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
  187. v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
  188. return -EINVAL;
  189. }
  190. tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
  191. / MEM2MEM_NUM_TILES;
  192. bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
  193. w = 0;
  194. memcpy(&out_vb->v4l2_buf.timestamp,
  195. &in_vb->v4l2_buf.timestamp,
  196. sizeof(struct timeval));
  197. switch (ctx->mode) {
  198. case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
  199. p_out += bytesperline * height - bytes_left;
  200. for (y = 0; y < height; ++y) {
  201. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  202. if (w & 0x1) {
  203. for (x = 0; x < tile_w; ++x)
  204. *--p_out = *p_in++ +
  205. MEM2MEM_COLOR_STEP;
  206. } else {
  207. for (x = 0; x < tile_w; ++x)
  208. *--p_out = *p_in++ -
  209. MEM2MEM_COLOR_STEP;
  210. }
  211. ++w;
  212. }
  213. p_in += bytes_left;
  214. p_out -= bytes_left;
  215. }
  216. break;
  217. case MEM2MEM_HFLIP:
  218. for (y = 0; y < height; ++y) {
  219. p_out += MEM2MEM_NUM_TILES * tile_w;
  220. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  221. if (w & 0x01) {
  222. for (x = 0; x < tile_w; ++x)
  223. *--p_out = *p_in++ +
  224. MEM2MEM_COLOR_STEP;
  225. } else {
  226. for (x = 0; x < tile_w; ++x)
  227. *--p_out = *p_in++ -
  228. MEM2MEM_COLOR_STEP;
  229. }
  230. ++w;
  231. }
  232. p_in += bytes_left;
  233. p_out += bytesperline;
  234. }
  235. break;
  236. case MEM2MEM_VFLIP:
  237. p_out += bytesperline * (height - 1);
  238. for (y = 0; y < height; ++y) {
  239. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  240. if (w & 0x1) {
  241. for (x = 0; x < tile_w; ++x)
  242. *p_out++ = *p_in++ +
  243. MEM2MEM_COLOR_STEP;
  244. } else {
  245. for (x = 0; x < tile_w; ++x)
  246. *p_out++ = *p_in++ -
  247. MEM2MEM_COLOR_STEP;
  248. }
  249. ++w;
  250. }
  251. p_in += bytes_left;
  252. p_out += bytes_left - 2 * bytesperline;
  253. }
  254. break;
  255. default:
  256. for (y = 0; y < height; ++y) {
  257. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  258. if (w & 0x1) {
  259. for (x = 0; x < tile_w; ++x)
  260. *p_out++ = *p_in++ +
  261. MEM2MEM_COLOR_STEP;
  262. } else {
  263. for (x = 0; x < tile_w; ++x)
  264. *p_out++ = *p_in++ -
  265. MEM2MEM_COLOR_STEP;
  266. }
  267. ++w;
  268. }
  269. p_in += bytes_left;
  270. p_out += bytes_left;
  271. }
  272. }
  273. return 0;
  274. }
  275. static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
  276. {
  277. dprintk(dev, "Scheduling a simulated irq\n");
  278. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
  279. }
  280. /*
  281. * mem2mem callbacks
  282. */
  283. /**
  284. * job_ready() - check whether an instance is ready to be scheduled to run
  285. */
  286. static int job_ready(void *priv)
  287. {
  288. struct m2mtest_ctx *ctx = priv;
  289. if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < ctx->translen
  290. || v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) < ctx->translen) {
  291. dprintk(ctx->dev, "Not enough buffers available\n");
  292. return 0;
  293. }
  294. return 1;
  295. }
  296. static void job_abort(void *priv)
  297. {
  298. struct m2mtest_ctx *ctx = priv;
  299. /* Will cancel the transaction in the next interrupt handler */
  300. ctx->aborting = 1;
  301. }
  302. static void m2mtest_lock(void *priv)
  303. {
  304. struct m2mtest_ctx *ctx = priv;
  305. struct m2mtest_dev *dev = ctx->dev;
  306. mutex_lock(&dev->dev_mutex);
  307. }
  308. static void m2mtest_unlock(void *priv)
  309. {
  310. struct m2mtest_ctx *ctx = priv;
  311. struct m2mtest_dev *dev = ctx->dev;
  312. mutex_unlock(&dev->dev_mutex);
  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 m2mtest_ctx *ctx = priv;
  323. struct m2mtest_dev *dev = ctx->dev;
  324. struct vb2_buffer *src_buf, *dst_buf;
  325. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  326. dst_buf = v4l2_m2m_next_dst_buf(ctx->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(unsigned long priv)
  332. {
  333. struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
  334. struct m2mtest_ctx *curr_ctx;
  335. struct vb2_buffer *src_vb, *dst_vb;
  336. unsigned long flags;
  337. curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_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->m2m_ctx);
  343. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  344. curr_ctx->num_processed++;
  345. spin_lock_irqsave(&m2mtest_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(&m2mtest_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(m2mtest_dev->m2m_dev, curr_ctx->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 m2mtest_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. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  391. f->pixelformat = fmt->fourcc;
  392. return 0;
  393. }
  394. /* Format not found */
  395. return -EINVAL;
  396. }
  397. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  398. struct v4l2_fmtdesc *f)
  399. {
  400. return enum_fmt(f, MEM2MEM_CAPTURE);
  401. }
  402. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  403. struct v4l2_fmtdesc *f)
  404. {
  405. return enum_fmt(f, MEM2MEM_OUTPUT);
  406. }
  407. static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  408. {
  409. struct vb2_queue *vq;
  410. struct m2mtest_q_data *q_data;
  411. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  412. if (!vq)
  413. return -EINVAL;
  414. q_data = get_q_data(ctx, f->type);
  415. f->fmt.pix.width = q_data->width;
  416. f->fmt.pix.height = q_data->height;
  417. f->fmt.pix.field = V4L2_FIELD_NONE;
  418. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  419. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  420. f->fmt.pix.sizeimage = q_data->sizeimage;
  421. f->fmt.pix.colorspace = ctx->colorspace;
  422. return 0;
  423. }
  424. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  425. struct v4l2_format *f)
  426. {
  427. return vidioc_g_fmt(file2ctx(file), f);
  428. }
  429. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  430. struct v4l2_format *f)
  431. {
  432. return vidioc_g_fmt(file2ctx(file), f);
  433. }
  434. static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
  435. {
  436. enum v4l2_field field;
  437. field = f->fmt.pix.field;
  438. if (field == V4L2_FIELD_ANY)
  439. field = V4L2_FIELD_NONE;
  440. else if (V4L2_FIELD_NONE != field)
  441. return -EINVAL;
  442. /* V4L2 specification suggests the driver corrects the format struct
  443. * if any of the dimensions is unsupported */
  444. f->fmt.pix.field = field;
  445. if (f->fmt.pix.height < MIN_H)
  446. f->fmt.pix.height = MIN_H;
  447. else if (f->fmt.pix.height > MAX_H)
  448. f->fmt.pix.height = MAX_H;
  449. if (f->fmt.pix.width < MIN_W)
  450. f->fmt.pix.width = MIN_W;
  451. else if (f->fmt.pix.width > MAX_W)
  452. f->fmt.pix.width = MAX_W;
  453. f->fmt.pix.width &= ~DIM_ALIGN_MASK;
  454. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  455. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  456. return 0;
  457. }
  458. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  459. struct v4l2_format *f)
  460. {
  461. struct m2mtest_fmt *fmt;
  462. struct m2mtest_ctx *ctx = file2ctx(file);
  463. fmt = find_format(f);
  464. if (!fmt || !(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. return vidioc_try_fmt(f, fmt);
  472. }
  473. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  474. struct v4l2_format *f)
  475. {
  476. struct m2mtest_fmt *fmt;
  477. struct m2mtest_ctx *ctx = file2ctx(file);
  478. fmt = find_format(f);
  479. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
  480. v4l2_err(&ctx->dev->v4l2_dev,
  481. "Fourcc format (0x%08x) invalid.\n",
  482. f->fmt.pix.pixelformat);
  483. return -EINVAL;
  484. }
  485. if (!f->fmt.pix.colorspace)
  486. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  487. return vidioc_try_fmt(f, fmt);
  488. }
  489. static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  490. {
  491. struct m2mtest_q_data *q_data;
  492. struct vb2_queue *vq;
  493. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  494. if (!vq)
  495. return -EINVAL;
  496. q_data = get_q_data(ctx, f->type);
  497. if (!q_data)
  498. return -EINVAL;
  499. if (vb2_is_busy(vq)) {
  500. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  501. return -EBUSY;
  502. }
  503. q_data->fmt = find_format(f);
  504. q_data->width = f->fmt.pix.width;
  505. q_data->height = f->fmt.pix.height;
  506. q_data->sizeimage = q_data->width * q_data->height
  507. * q_data->fmt->depth >> 3;
  508. dprintk(ctx->dev,
  509. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  510. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  511. return 0;
  512. }
  513. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  514. struct v4l2_format *f)
  515. {
  516. int ret;
  517. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  518. if (ret)
  519. return ret;
  520. return vidioc_s_fmt(file2ctx(file), f);
  521. }
  522. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  523. struct v4l2_format *f)
  524. {
  525. struct m2mtest_ctx *ctx = file2ctx(file);
  526. int ret;
  527. ret = vidioc_try_fmt_vid_out(file, priv, f);
  528. if (ret)
  529. return ret;
  530. ret = vidioc_s_fmt(file2ctx(file), f);
  531. if (!ret)
  532. ctx->colorspace = f->fmt.pix.colorspace;
  533. return ret;
  534. }
  535. static int vidioc_reqbufs(struct file *file, void *priv,
  536. struct v4l2_requestbuffers *reqbufs)
  537. {
  538. struct m2mtest_ctx *ctx = file2ctx(file);
  539. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  540. }
  541. static int vidioc_querybuf(struct file *file, void *priv,
  542. struct v4l2_buffer *buf)
  543. {
  544. struct m2mtest_ctx *ctx = file2ctx(file);
  545. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  546. }
  547. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  548. {
  549. struct m2mtest_ctx *ctx = file2ctx(file);
  550. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  551. }
  552. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  553. {
  554. struct m2mtest_ctx *ctx = file2ctx(file);
  555. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  556. }
  557. static int vidioc_streamon(struct file *file, void *priv,
  558. enum v4l2_buf_type type)
  559. {
  560. struct m2mtest_ctx *ctx = file2ctx(file);
  561. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  562. }
  563. static int vidioc_streamoff(struct file *file, void *priv,
  564. enum v4l2_buf_type type)
  565. {
  566. struct m2mtest_ctx *ctx = file2ctx(file);
  567. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  568. }
  569. static int m2mtest_s_ctrl(struct v4l2_ctrl *ctrl)
  570. {
  571. struct m2mtest_ctx *ctx =
  572. container_of(ctrl->handler, struct m2mtest_ctx, hdl);
  573. switch (ctrl->id) {
  574. case V4L2_CID_HFLIP:
  575. if (ctrl->val)
  576. ctx->mode |= MEM2MEM_HFLIP;
  577. else
  578. ctx->mode &= ~MEM2MEM_HFLIP;
  579. break;
  580. case V4L2_CID_VFLIP:
  581. if (ctrl->val)
  582. ctx->mode |= MEM2MEM_VFLIP;
  583. else
  584. ctx->mode &= ~MEM2MEM_VFLIP;
  585. break;
  586. case V4L2_CID_TRANS_TIME_MSEC:
  587. ctx->transtime = ctrl->val;
  588. break;
  589. case V4L2_CID_TRANS_NUM_BUFS:
  590. ctx->translen = ctrl->val;
  591. break;
  592. default:
  593. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  594. return -EINVAL;
  595. }
  596. return 0;
  597. }
  598. static const struct v4l2_ctrl_ops m2mtest_ctrl_ops = {
  599. .s_ctrl = m2mtest_s_ctrl,
  600. };
  601. static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
  602. .vidioc_querycap = vidioc_querycap,
  603. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  604. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  605. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  606. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  607. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  608. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  609. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  610. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  611. .vidioc_reqbufs = vidioc_reqbufs,
  612. .vidioc_querybuf = vidioc_querybuf,
  613. .vidioc_qbuf = vidioc_qbuf,
  614. .vidioc_dqbuf = vidioc_dqbuf,
  615. .vidioc_streamon = vidioc_streamon,
  616. .vidioc_streamoff = vidioc_streamoff,
  617. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  618. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  619. };
  620. /*
  621. * Queue operations
  622. */
  623. static int m2mtest_queue_setup(struct vb2_queue *vq,
  624. const struct v4l2_format *fmt,
  625. unsigned int *nbuffers, unsigned int *nplanes,
  626. unsigned int sizes[], void *alloc_ctxs[])
  627. {
  628. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
  629. struct m2mtest_q_data *q_data;
  630. unsigned int size, count = *nbuffers;
  631. q_data = get_q_data(ctx, vq->type);
  632. size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  633. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  634. (count)--;
  635. *nplanes = 1;
  636. *nbuffers = count;
  637. sizes[0] = size;
  638. /*
  639. * videobuf2-vmalloc allocator is context-less so no need to set
  640. * alloc_ctxs array.
  641. */
  642. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  643. return 0;
  644. }
  645. static int m2mtest_buf_prepare(struct vb2_buffer *vb)
  646. {
  647. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  648. struct m2mtest_q_data *q_data;
  649. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  650. q_data = get_q_data(ctx, vb->vb2_queue->type);
  651. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  652. dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
  653. __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
  654. return -EINVAL;
  655. }
  656. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  657. return 0;
  658. }
  659. static void m2mtest_buf_queue(struct vb2_buffer *vb)
  660. {
  661. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  662. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  663. }
  664. static void m2mtest_wait_prepare(struct vb2_queue *q)
  665. {
  666. struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
  667. m2mtest_unlock(ctx);
  668. }
  669. static void m2mtest_wait_finish(struct vb2_queue *q)
  670. {
  671. struct m2mtest_ctx *ctx = vb2_get_drv_priv(q);
  672. m2mtest_lock(ctx);
  673. }
  674. static struct vb2_ops m2mtest_qops = {
  675. .queue_setup = m2mtest_queue_setup,
  676. .buf_prepare = m2mtest_buf_prepare,
  677. .buf_queue = m2mtest_buf_queue,
  678. .wait_prepare = m2mtest_wait_prepare,
  679. .wait_finish = m2mtest_wait_finish,
  680. };
  681. static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
  682. {
  683. struct m2mtest_ctx *ctx = priv;
  684. int ret;
  685. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  686. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  687. src_vq->drv_priv = ctx;
  688. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  689. src_vq->ops = &m2mtest_qops;
  690. src_vq->mem_ops = &vb2_vmalloc_memops;
  691. src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  692. ret = vb2_queue_init(src_vq);
  693. if (ret)
  694. return ret;
  695. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  696. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  697. dst_vq->drv_priv = ctx;
  698. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  699. dst_vq->ops = &m2mtest_qops;
  700. dst_vq->mem_ops = &vb2_vmalloc_memops;
  701. dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  702. return vb2_queue_init(dst_vq);
  703. }
  704. static const struct v4l2_ctrl_config m2mtest_ctrl_trans_time_msec = {
  705. .ops = &m2mtest_ctrl_ops,
  706. .id = V4L2_CID_TRANS_TIME_MSEC,
  707. .name = "Transaction Time (msec)",
  708. .type = V4L2_CTRL_TYPE_INTEGER,
  709. .def = 1001,
  710. .min = 1,
  711. .max = 10001,
  712. .step = 100,
  713. };
  714. static const struct v4l2_ctrl_config m2mtest_ctrl_trans_num_bufs = {
  715. .ops = &m2mtest_ctrl_ops,
  716. .id = V4L2_CID_TRANS_NUM_BUFS,
  717. .name = "Buffers Per Transaction",
  718. .type = V4L2_CTRL_TYPE_INTEGER,
  719. .def = 1,
  720. .min = 1,
  721. .max = MEM2MEM_DEF_NUM_BUFS,
  722. .step = 1,
  723. };
  724. /*
  725. * File operations
  726. */
  727. static int m2mtest_open(struct file *file)
  728. {
  729. struct m2mtest_dev *dev = video_drvdata(file);
  730. struct m2mtest_ctx *ctx = NULL;
  731. struct v4l2_ctrl_handler *hdl;
  732. int rc = 0;
  733. if (mutex_lock_interruptible(&dev->dev_mutex))
  734. return -ERESTARTSYS;
  735. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  736. if (!ctx) {
  737. rc = -ENOMEM;
  738. goto open_unlock;
  739. }
  740. v4l2_fh_init(&ctx->fh, video_devdata(file));
  741. file->private_data = &ctx->fh;
  742. ctx->dev = dev;
  743. hdl = &ctx->hdl;
  744. v4l2_ctrl_handler_init(hdl, 4);
  745. v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  746. v4l2_ctrl_new_std(hdl, &m2mtest_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  747. v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_time_msec, NULL);
  748. v4l2_ctrl_new_custom(hdl, &m2mtest_ctrl_trans_num_bufs, NULL);
  749. if (hdl->error) {
  750. rc = hdl->error;
  751. v4l2_ctrl_handler_free(hdl);
  752. goto open_unlock;
  753. }
  754. ctx->fh.ctrl_handler = hdl;
  755. v4l2_ctrl_handler_setup(hdl);
  756. ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
  757. ctx->q_data[V4L2_M2M_SRC].width = 640;
  758. ctx->q_data[V4L2_M2M_SRC].height = 480;
  759. ctx->q_data[V4L2_M2M_SRC].sizeimage =
  760. ctx->q_data[V4L2_M2M_SRC].width *
  761. ctx->q_data[V4L2_M2M_SRC].height *
  762. (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
  763. ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
  764. ctx->colorspace = V4L2_COLORSPACE_REC709;
  765. ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  766. if (IS_ERR(ctx->m2m_ctx)) {
  767. rc = PTR_ERR(ctx->m2m_ctx);
  768. v4l2_ctrl_handler_free(hdl);
  769. kfree(ctx);
  770. goto open_unlock;
  771. }
  772. v4l2_fh_add(&ctx->fh);
  773. atomic_inc(&dev->num_inst);
  774. dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
  775. open_unlock:
  776. mutex_unlock(&dev->dev_mutex);
  777. return rc;
  778. }
  779. static int m2mtest_release(struct file *file)
  780. {
  781. struct m2mtest_dev *dev = video_drvdata(file);
  782. struct m2mtest_ctx *ctx = file2ctx(file);
  783. dprintk(dev, "Releasing instance %p\n", ctx);
  784. v4l2_fh_del(&ctx->fh);
  785. v4l2_fh_exit(&ctx->fh);
  786. v4l2_ctrl_handler_free(&ctx->hdl);
  787. mutex_lock(&dev->dev_mutex);
  788. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  789. mutex_unlock(&dev->dev_mutex);
  790. kfree(ctx);
  791. atomic_dec(&dev->num_inst);
  792. return 0;
  793. }
  794. static unsigned int m2mtest_poll(struct file *file,
  795. struct poll_table_struct *wait)
  796. {
  797. struct m2mtest_ctx *ctx = file2ctx(file);
  798. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  799. }
  800. static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
  801. {
  802. struct m2mtest_dev *dev = video_drvdata(file);
  803. struct m2mtest_ctx *ctx = file2ctx(file);
  804. int res;
  805. if (mutex_lock_interruptible(&dev->dev_mutex))
  806. return -ERESTARTSYS;
  807. res = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  808. mutex_unlock(&dev->dev_mutex);
  809. return res;
  810. }
  811. static const struct v4l2_file_operations m2mtest_fops = {
  812. .owner = THIS_MODULE,
  813. .open = m2mtest_open,
  814. .release = m2mtest_release,
  815. .poll = m2mtest_poll,
  816. .unlocked_ioctl = video_ioctl2,
  817. .mmap = m2mtest_mmap,
  818. };
  819. static struct video_device m2mtest_videodev = {
  820. .name = MEM2MEM_NAME,
  821. .vfl_dir = VFL_DIR_M2M,
  822. .fops = &m2mtest_fops,
  823. .ioctl_ops = &m2mtest_ioctl_ops,
  824. .minor = -1,
  825. .release = video_device_release,
  826. };
  827. static struct v4l2_m2m_ops m2m_ops = {
  828. .device_run = device_run,
  829. .job_ready = job_ready,
  830. .job_abort = job_abort,
  831. .lock = m2mtest_lock,
  832. .unlock = m2mtest_unlock,
  833. };
  834. static int m2mtest_probe(struct platform_device *pdev)
  835. {
  836. struct m2mtest_dev *dev;
  837. struct video_device *vfd;
  838. int ret;
  839. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  840. if (!dev)
  841. return -ENOMEM;
  842. spin_lock_init(&dev->irqlock);
  843. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  844. if (ret)
  845. return ret;
  846. atomic_set(&dev->num_inst, 0);
  847. mutex_init(&dev->dev_mutex);
  848. vfd = video_device_alloc();
  849. if (!vfd) {
  850. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  851. ret = -ENOMEM;
  852. goto unreg_dev;
  853. }
  854. *vfd = m2mtest_videodev;
  855. vfd->lock = &dev->dev_mutex;
  856. vfd->v4l2_dev = &dev->v4l2_dev;
  857. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  858. if (ret) {
  859. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  860. goto rel_vdev;
  861. }
  862. video_set_drvdata(vfd, dev);
  863. snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
  864. dev->vfd = vfd;
  865. v4l2_info(&dev->v4l2_dev,
  866. "Device registered as /dev/video%d\n", vfd->num);
  867. setup_timer(&dev->timer, device_isr, (long)dev);
  868. platform_set_drvdata(pdev, dev);
  869. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  870. if (IS_ERR(dev->m2m_dev)) {
  871. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  872. ret = PTR_ERR(dev->m2m_dev);
  873. goto err_m2m;
  874. }
  875. return 0;
  876. err_m2m:
  877. v4l2_m2m_release(dev->m2m_dev);
  878. video_unregister_device(dev->vfd);
  879. rel_vdev:
  880. video_device_release(vfd);
  881. unreg_dev:
  882. v4l2_device_unregister(&dev->v4l2_dev);
  883. return ret;
  884. }
  885. static int m2mtest_remove(struct platform_device *pdev)
  886. {
  887. struct m2mtest_dev *dev =
  888. (struct m2mtest_dev *)platform_get_drvdata(pdev);
  889. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
  890. v4l2_m2m_release(dev->m2m_dev);
  891. del_timer_sync(&dev->timer);
  892. video_unregister_device(dev->vfd);
  893. v4l2_device_unregister(&dev->v4l2_dev);
  894. return 0;
  895. }
  896. static struct platform_driver m2mtest_pdrv = {
  897. .probe = m2mtest_probe,
  898. .remove = m2mtest_remove,
  899. .driver = {
  900. .name = MEM2MEM_NAME,
  901. .owner = THIS_MODULE,
  902. },
  903. };
  904. static void __exit m2mtest_exit(void)
  905. {
  906. platform_driver_unregister(&m2mtest_pdrv);
  907. platform_device_unregister(&m2mtest_pdev);
  908. }
  909. static int __init m2mtest_init(void)
  910. {
  911. int ret;
  912. ret = platform_device_register(&m2mtest_pdev);
  913. if (ret)
  914. return ret;
  915. ret = platform_driver_register(&m2mtest_pdrv);
  916. if (ret)
  917. platform_device_unregister(&m2mtest_pdev);
  918. return 0;
  919. }
  920. module_init(m2mtest_init);
  921. module_exit(m2mtest_exit);