mem2mem_testdev.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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, <p.osciak@samsung.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/version.h>
  23. #include <linux/timer.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/platform_device.h>
  27. #include <media/v4l2-mem2mem.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-ioctl.h>
  30. #include <media/videobuf-vmalloc.h>
  31. #define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
  32. MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
  33. MODULE_AUTHOR("Pawel Osciak, <p.osciak@samsung.com>");
  34. MODULE_LICENSE("GPL");
  35. #define MIN_W 32
  36. #define MIN_H 32
  37. #define MAX_W 640
  38. #define MAX_H 480
  39. #define DIM_ALIGN_MASK 0x08 /* 8-alignment for dimensions */
  40. /* Flags that indicate a format can be used for capture/output */
  41. #define MEM2MEM_CAPTURE (1 << 0)
  42. #define MEM2MEM_OUTPUT (1 << 1)
  43. #define MEM2MEM_NAME "m2m-testdev"
  44. /* Per queue */
  45. #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
  46. /* In bytes, per queue */
  47. #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
  48. /* Default transaction time in msec */
  49. #define MEM2MEM_DEF_TRANSTIME 1000
  50. /* Default number of buffers per transaction */
  51. #define MEM2MEM_DEF_TRANSLEN 1
  52. #define MEM2MEM_COLOR_STEP (0xff >> 4)
  53. #define MEM2MEM_NUM_TILES 8
  54. #define dprintk(dev, fmt, arg...) \
  55. v4l2_dbg(1, 1, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
  56. void m2mtest_dev_release(struct device *dev)
  57. {}
  58. static struct platform_device m2mtest_pdev = {
  59. .name = MEM2MEM_NAME,
  60. .dev.release = m2mtest_dev_release,
  61. };
  62. struct m2mtest_fmt {
  63. char *name;
  64. u32 fourcc;
  65. int depth;
  66. /* Types the format can be used for */
  67. u32 types;
  68. };
  69. static struct m2mtest_fmt formats[] = {
  70. {
  71. .name = "RGB565 (BE)",
  72. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  73. .depth = 16,
  74. /* Both capture and output format */
  75. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  76. },
  77. {
  78. .name = "4:2:2, packed, YUYV",
  79. .fourcc = V4L2_PIX_FMT_YUYV,
  80. .depth = 16,
  81. /* Output-only format */
  82. .types = MEM2MEM_OUTPUT,
  83. },
  84. };
  85. /* Per-queue, driver-specific private data */
  86. struct m2mtest_q_data {
  87. unsigned int width;
  88. unsigned int height;
  89. unsigned int sizeimage;
  90. struct m2mtest_fmt *fmt;
  91. };
  92. enum {
  93. V4L2_M2M_SRC = 0,
  94. V4L2_M2M_DST = 1,
  95. };
  96. /* Source and destination queue data */
  97. static struct m2mtest_q_data q_data[2];
  98. static struct m2mtest_q_data *get_q_data(enum v4l2_buf_type type)
  99. {
  100. switch (type) {
  101. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  102. return &q_data[V4L2_M2M_SRC];
  103. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  104. return &q_data[V4L2_M2M_DST];
  105. default:
  106. BUG();
  107. }
  108. return NULL;
  109. }
  110. #define V4L2_CID_TRANS_TIME_MSEC V4L2_CID_PRIVATE_BASE
  111. #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_PRIVATE_BASE + 1)
  112. static struct v4l2_queryctrl m2mtest_ctrls[] = {
  113. {
  114. .id = V4L2_CID_TRANS_TIME_MSEC,
  115. .type = V4L2_CTRL_TYPE_INTEGER,
  116. .name = "Transaction time (msec)",
  117. .minimum = 1,
  118. .maximum = 10000,
  119. .step = 100,
  120. .default_value = 1000,
  121. .flags = 0,
  122. }, {
  123. .id = V4L2_CID_TRANS_NUM_BUFS,
  124. .type = V4L2_CTRL_TYPE_INTEGER,
  125. .name = "Buffers per transaction",
  126. .minimum = 1,
  127. .maximum = MEM2MEM_DEF_NUM_BUFS,
  128. .step = 1,
  129. .default_value = 1,
  130. .flags = 0,
  131. },
  132. };
  133. #define NUM_FORMATS ARRAY_SIZE(formats)
  134. static struct m2mtest_fmt *find_format(struct v4l2_format *f)
  135. {
  136. struct m2mtest_fmt *fmt;
  137. unsigned int k;
  138. for (k = 0; k < NUM_FORMATS; k++) {
  139. fmt = &formats[k];
  140. if (fmt->fourcc == f->fmt.pix.pixelformat)
  141. break;
  142. }
  143. if (k == NUM_FORMATS)
  144. return NULL;
  145. return &formats[k];
  146. }
  147. struct m2mtest_dev {
  148. struct v4l2_device v4l2_dev;
  149. struct video_device *vfd;
  150. atomic_t num_inst;
  151. struct mutex dev_mutex;
  152. spinlock_t irqlock;
  153. struct timer_list timer;
  154. struct v4l2_m2m_dev *m2m_dev;
  155. };
  156. struct m2mtest_ctx {
  157. struct m2mtest_dev *dev;
  158. /* Processed buffers in this transaction */
  159. u8 num_processed;
  160. /* Transaction length (i.e. how many buffers per transaction) */
  161. u32 translen;
  162. /* Transaction time (i.e. simulated processing time) in milliseconds */
  163. u32 transtime;
  164. /* Abort requested by m2m */
  165. int aborting;
  166. struct v4l2_m2m_ctx *m2m_ctx;
  167. };
  168. struct m2mtest_buffer {
  169. /* vb must be first! */
  170. struct videobuf_buffer vb;
  171. };
  172. static struct v4l2_queryctrl *get_ctrl(int id)
  173. {
  174. int i;
  175. for (i = 0; i < ARRAY_SIZE(m2mtest_ctrls); ++i) {
  176. if (id == m2mtest_ctrls[i].id)
  177. return &m2mtest_ctrls[i];
  178. }
  179. return NULL;
  180. }
  181. static int device_process(struct m2mtest_ctx *ctx,
  182. struct m2mtest_buffer *in_buf,
  183. struct m2mtest_buffer *out_buf)
  184. {
  185. struct m2mtest_dev *dev = ctx->dev;
  186. u8 *p_in, *p_out;
  187. int x, y, t, w;
  188. int tile_w, bytes_left;
  189. struct videobuf_queue *src_q;
  190. struct videobuf_queue *dst_q;
  191. src_q = v4l2_m2m_get_src_vq(ctx->m2m_ctx);
  192. dst_q = v4l2_m2m_get_dst_vq(ctx->m2m_ctx);
  193. p_in = videobuf_queue_to_vaddr(src_q, &in_buf->vb);
  194. p_out = videobuf_queue_to_vaddr(dst_q, &out_buf->vb);
  195. if (!p_in || !p_out) {
  196. v4l2_err(&dev->v4l2_dev,
  197. "Acquiring kernel pointers to buffers failed\n");
  198. return -EFAULT;
  199. }
  200. if (in_buf->vb.size > out_buf->vb.size) {
  201. v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
  202. return -EINVAL;
  203. }
  204. tile_w = (in_buf->vb.width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
  205. / MEM2MEM_NUM_TILES;
  206. bytes_left = in_buf->vb.bytesperline - tile_w * MEM2MEM_NUM_TILES;
  207. w = 0;
  208. for (y = 0; y < in_buf->vb.height; ++y) {
  209. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  210. if (w & 0x1) {
  211. for (x = 0; x < tile_w; ++x)
  212. *p_out++ = *p_in++ + MEM2MEM_COLOR_STEP;
  213. } else {
  214. for (x = 0; x < tile_w; ++x)
  215. *p_out++ = *p_in++ - MEM2MEM_COLOR_STEP;
  216. }
  217. ++w;
  218. }
  219. p_in += bytes_left;
  220. p_out += bytes_left;
  221. }
  222. return 0;
  223. }
  224. static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
  225. {
  226. dprintk(dev, "Scheduling a simulated irq\n");
  227. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
  228. }
  229. /*
  230. * mem2mem callbacks
  231. */
  232. /**
  233. * job_ready() - check whether an instance is ready to be scheduled to run
  234. */
  235. static int job_ready(void *priv)
  236. {
  237. struct m2mtest_ctx *ctx = priv;
  238. if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < ctx->translen
  239. || v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) < ctx->translen) {
  240. dprintk(ctx->dev, "Not enough buffers available\n");
  241. return 0;
  242. }
  243. return 1;
  244. }
  245. static void job_abort(void *priv)
  246. {
  247. struct m2mtest_ctx *ctx = priv;
  248. /* Will cancel the transaction in the next interrupt handler */
  249. ctx->aborting = 1;
  250. }
  251. /* device_run() - prepares and starts the device
  252. *
  253. * This simulates all the immediate preparations required before starting
  254. * a device. This will be called by the framework when it decides to schedule
  255. * a particular instance.
  256. */
  257. static void device_run(void *priv)
  258. {
  259. struct m2mtest_ctx *ctx = priv;
  260. struct m2mtest_dev *dev = ctx->dev;
  261. struct m2mtest_buffer *src_buf, *dst_buf;
  262. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  263. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  264. device_process(ctx, src_buf, dst_buf);
  265. /* Run a timer, which simulates a hardware irq */
  266. schedule_irq(dev, ctx->transtime);
  267. }
  268. static void device_isr(unsigned long priv)
  269. {
  270. struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
  271. struct m2mtest_ctx *curr_ctx;
  272. struct m2mtest_buffer *src_buf, *dst_buf;
  273. unsigned long flags;
  274. curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
  275. if (NULL == curr_ctx) {
  276. printk(KERN_ERR
  277. "Instance released before the end of transaction\n");
  278. return;
  279. }
  280. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  281. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  282. curr_ctx->num_processed++;
  283. if (curr_ctx->num_processed == curr_ctx->translen
  284. || curr_ctx->aborting) {
  285. dprintk(curr_ctx->dev, "Finishing transaction\n");
  286. curr_ctx->num_processed = 0;
  287. spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
  288. src_buf->vb.state = dst_buf->vb.state = VIDEOBUF_DONE;
  289. wake_up(&src_buf->vb.done);
  290. wake_up(&dst_buf->vb.done);
  291. spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
  292. v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->m2m_ctx);
  293. } else {
  294. spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
  295. src_buf->vb.state = dst_buf->vb.state = VIDEOBUF_DONE;
  296. wake_up(&src_buf->vb.done);
  297. wake_up(&dst_buf->vb.done);
  298. spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
  299. device_run(curr_ctx);
  300. }
  301. }
  302. /*
  303. * video ioctls
  304. */
  305. static int vidioc_querycap(struct file *file, void *priv,
  306. struct v4l2_capability *cap)
  307. {
  308. strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
  309. strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
  310. cap->bus_info[0] = 0;
  311. cap->version = KERNEL_VERSION(0, 1, 0);
  312. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
  313. | V4L2_CAP_STREAMING;
  314. return 0;
  315. }
  316. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  317. {
  318. int i, num;
  319. struct m2mtest_fmt *fmt;
  320. num = 0;
  321. for (i = 0; i < NUM_FORMATS; ++i) {
  322. if (formats[i].types & type) {
  323. /* index-th format of type type found ? */
  324. if (num == f->index)
  325. break;
  326. /* Correct type but haven't reached our index yet,
  327. * just increment per-type index */
  328. ++num;
  329. }
  330. }
  331. if (i < NUM_FORMATS) {
  332. /* Format found */
  333. fmt = &formats[i];
  334. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  335. f->pixelformat = fmt->fourcc;
  336. return 0;
  337. }
  338. /* Format not found */
  339. return -EINVAL;
  340. }
  341. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  342. struct v4l2_fmtdesc *f)
  343. {
  344. return enum_fmt(f, MEM2MEM_CAPTURE);
  345. }
  346. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  347. struct v4l2_fmtdesc *f)
  348. {
  349. return enum_fmt(f, MEM2MEM_OUTPUT);
  350. }
  351. static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  352. {
  353. struct videobuf_queue *vq;
  354. struct m2mtest_q_data *q_data;
  355. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  356. if (!vq)
  357. return -EINVAL;
  358. q_data = get_q_data(f->type);
  359. f->fmt.pix.width = q_data->width;
  360. f->fmt.pix.height = q_data->height;
  361. f->fmt.pix.field = vq->field;
  362. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  363. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  364. f->fmt.pix.sizeimage = q_data->sizeimage;
  365. return 0;
  366. }
  367. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  368. struct v4l2_format *f)
  369. {
  370. return vidioc_g_fmt(priv, f);
  371. }
  372. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  373. struct v4l2_format *f)
  374. {
  375. return vidioc_g_fmt(priv, f);
  376. }
  377. static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
  378. {
  379. enum v4l2_field field;
  380. field = f->fmt.pix.field;
  381. if (field == V4L2_FIELD_ANY)
  382. field = V4L2_FIELD_NONE;
  383. else if (V4L2_FIELD_NONE != field)
  384. return -EINVAL;
  385. /* V4L2 specification suggests the driver corrects the format struct
  386. * if any of the dimensions is unsupported */
  387. f->fmt.pix.field = field;
  388. if (f->fmt.pix.height < MIN_H)
  389. f->fmt.pix.height = MIN_H;
  390. else if (f->fmt.pix.height > MAX_H)
  391. f->fmt.pix.height = MAX_H;
  392. if (f->fmt.pix.width < MIN_W)
  393. f->fmt.pix.width = MIN_W;
  394. else if (f->fmt.pix.width > MAX_W)
  395. f->fmt.pix.width = MAX_W;
  396. f->fmt.pix.width &= ~DIM_ALIGN_MASK;
  397. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  398. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  399. return 0;
  400. }
  401. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  402. struct v4l2_format *f)
  403. {
  404. struct m2mtest_fmt *fmt;
  405. struct m2mtest_ctx *ctx = priv;
  406. fmt = find_format(f);
  407. if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
  408. v4l2_err(&ctx->dev->v4l2_dev,
  409. "Fourcc format (0x%08x) invalid.\n",
  410. f->fmt.pix.pixelformat);
  411. return -EINVAL;
  412. }
  413. return vidioc_try_fmt(f, fmt);
  414. }
  415. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  416. struct v4l2_format *f)
  417. {
  418. struct m2mtest_fmt *fmt;
  419. struct m2mtest_ctx *ctx = priv;
  420. fmt = find_format(f);
  421. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
  422. v4l2_err(&ctx->dev->v4l2_dev,
  423. "Fourcc format (0x%08x) invalid.\n",
  424. f->fmt.pix.pixelformat);
  425. return -EINVAL;
  426. }
  427. return vidioc_try_fmt(f, fmt);
  428. }
  429. static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  430. {
  431. struct m2mtest_q_data *q_data;
  432. struct videobuf_queue *vq;
  433. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  434. if (!vq)
  435. return -EINVAL;
  436. q_data = get_q_data(f->type);
  437. if (!q_data)
  438. return -EINVAL;
  439. if (videobuf_queue_is_busy(vq)) {
  440. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  441. return -EBUSY;
  442. }
  443. q_data->fmt = find_format(f);
  444. q_data->width = f->fmt.pix.width;
  445. q_data->height = f->fmt.pix.height;
  446. q_data->sizeimage = q_data->width * q_data->height
  447. * q_data->fmt->depth >> 3;
  448. vq->field = f->fmt.pix.field;
  449. dprintk(ctx->dev,
  450. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  451. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  452. return 0;
  453. }
  454. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  455. struct v4l2_format *f)
  456. {
  457. int ret;
  458. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  459. if (ret)
  460. return ret;
  461. return vidioc_s_fmt(priv, f);
  462. }
  463. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  464. struct v4l2_format *f)
  465. {
  466. int ret;
  467. ret = vidioc_try_fmt_vid_out(file, priv, f);
  468. if (ret)
  469. return ret;
  470. return vidioc_s_fmt(priv, f);
  471. }
  472. static int vidioc_reqbufs(struct file *file, void *priv,
  473. struct v4l2_requestbuffers *reqbufs)
  474. {
  475. struct m2mtest_ctx *ctx = priv;
  476. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  477. }
  478. static int vidioc_querybuf(struct file *file, void *priv,
  479. struct v4l2_buffer *buf)
  480. {
  481. struct m2mtest_ctx *ctx = priv;
  482. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  483. }
  484. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  485. {
  486. struct m2mtest_ctx *ctx = priv;
  487. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  488. }
  489. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  490. {
  491. struct m2mtest_ctx *ctx = priv;
  492. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  493. }
  494. static int vidioc_streamon(struct file *file, void *priv,
  495. enum v4l2_buf_type type)
  496. {
  497. struct m2mtest_ctx *ctx = priv;
  498. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  499. }
  500. static int vidioc_streamoff(struct file *file, void *priv,
  501. enum v4l2_buf_type type)
  502. {
  503. struct m2mtest_ctx *ctx = priv;
  504. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  505. }
  506. static int vidioc_queryctrl(struct file *file, void *priv,
  507. struct v4l2_queryctrl *qc)
  508. {
  509. struct v4l2_queryctrl *c;
  510. c = get_ctrl(qc->id);
  511. if (!c)
  512. return -EINVAL;
  513. *qc = *c;
  514. return 0;
  515. }
  516. static int vidioc_g_ctrl(struct file *file, void *priv,
  517. struct v4l2_control *ctrl)
  518. {
  519. struct m2mtest_ctx *ctx = priv;
  520. switch (ctrl->id) {
  521. case V4L2_CID_TRANS_TIME_MSEC:
  522. ctrl->value = ctx->transtime;
  523. break;
  524. case V4L2_CID_TRANS_NUM_BUFS:
  525. ctrl->value = ctx->translen;
  526. break;
  527. default:
  528. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  529. return -EINVAL;
  530. }
  531. return 0;
  532. }
  533. static int check_ctrl_val(struct m2mtest_ctx *ctx, struct v4l2_control *ctrl)
  534. {
  535. struct v4l2_queryctrl *c;
  536. c = get_ctrl(ctrl->id);
  537. if (!c)
  538. return -EINVAL;
  539. if (ctrl->value < c->minimum || ctrl->value > c->maximum) {
  540. v4l2_err(&ctx->dev->v4l2_dev, "Value out of range\n");
  541. return -ERANGE;
  542. }
  543. return 0;
  544. }
  545. static int vidioc_s_ctrl(struct file *file, void *priv,
  546. struct v4l2_control *ctrl)
  547. {
  548. struct m2mtest_ctx *ctx = priv;
  549. int ret = 0;
  550. ret = check_ctrl_val(ctx, ctrl);
  551. if (ret != 0)
  552. return ret;
  553. switch (ctrl->id) {
  554. case V4L2_CID_TRANS_TIME_MSEC:
  555. ctx->transtime = ctrl->value;
  556. break;
  557. case V4L2_CID_TRANS_NUM_BUFS:
  558. ctx->translen = ctrl->value;
  559. break;
  560. default:
  561. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  562. return -EINVAL;
  563. }
  564. return 0;
  565. }
  566. static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
  567. .vidioc_querycap = vidioc_querycap,
  568. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  569. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  570. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  571. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  572. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  573. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  574. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  575. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  576. .vidioc_reqbufs = vidioc_reqbufs,
  577. .vidioc_querybuf = vidioc_querybuf,
  578. .vidioc_qbuf = vidioc_qbuf,
  579. .vidioc_dqbuf = vidioc_dqbuf,
  580. .vidioc_streamon = vidioc_streamon,
  581. .vidioc_streamoff = vidioc_streamoff,
  582. .vidioc_queryctrl = vidioc_queryctrl,
  583. .vidioc_g_ctrl = vidioc_g_ctrl,
  584. .vidioc_s_ctrl = vidioc_s_ctrl,
  585. };
  586. /*
  587. * Queue operations
  588. */
  589. static void m2mtest_buf_release(struct videobuf_queue *vq,
  590. struct videobuf_buffer *vb)
  591. {
  592. struct m2mtest_ctx *ctx = vq->priv_data;
  593. dprintk(ctx->dev, "type: %d, index: %d, state: %d\n",
  594. vq->type, vb->i, vb->state);
  595. videobuf_vmalloc_free(vb);
  596. vb->state = VIDEOBUF_NEEDS_INIT;
  597. }
  598. static int m2mtest_buf_setup(struct videobuf_queue *vq, unsigned int *count,
  599. unsigned int *size)
  600. {
  601. struct m2mtest_ctx *ctx = vq->priv_data;
  602. struct m2mtest_q_data *q_data;
  603. q_data = get_q_data(vq->type);
  604. *size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  605. dprintk(ctx->dev, "size:%d, w/h %d/%d, depth: %d\n",
  606. *size, q_data->width, q_data->height, q_data->fmt->depth);
  607. if (0 == *count)
  608. *count = MEM2MEM_DEF_NUM_BUFS;
  609. while (*size * *count > MEM2MEM_VID_MEM_LIMIT)
  610. (*count)--;
  611. v4l2_info(&ctx->dev->v4l2_dev,
  612. "%d buffers of size %d set up.\n", *count, *size);
  613. return 0;
  614. }
  615. static int m2mtest_buf_prepare(struct videobuf_queue *vq,
  616. struct videobuf_buffer *vb,
  617. enum v4l2_field field)
  618. {
  619. struct m2mtest_ctx *ctx = vq->priv_data;
  620. struct m2mtest_q_data *q_data;
  621. int ret;
  622. dprintk(ctx->dev, "type: %d, index: %d, state: %d\n",
  623. vq->type, vb->i, vb->state);
  624. q_data = get_q_data(vq->type);
  625. if (vb->baddr) {
  626. /* User-provided buffer */
  627. if (vb->bsize < q_data->sizeimage) {
  628. /* Buffer too small to fit a frame */
  629. v4l2_err(&ctx->dev->v4l2_dev,
  630. "User-provided buffer too small\n");
  631. return -EINVAL;
  632. }
  633. } else if (vb->state != VIDEOBUF_NEEDS_INIT
  634. && vb->bsize < q_data->sizeimage) {
  635. /* We provide the buffer, but it's already been initialized
  636. * and is too small */
  637. return -EINVAL;
  638. }
  639. vb->width = q_data->width;
  640. vb->height = q_data->height;
  641. vb->bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  642. vb->size = q_data->sizeimage;
  643. vb->field = field;
  644. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  645. ret = videobuf_iolock(vq, vb, NULL);
  646. if (ret) {
  647. v4l2_err(&ctx->dev->v4l2_dev,
  648. "Iolock failed\n");
  649. goto fail;
  650. }
  651. }
  652. vb->state = VIDEOBUF_PREPARED;
  653. return 0;
  654. fail:
  655. m2mtest_buf_release(vq, vb);
  656. return ret;
  657. }
  658. static void m2mtest_buf_queue(struct videobuf_queue *vq,
  659. struct videobuf_buffer *vb)
  660. {
  661. struct m2mtest_ctx *ctx = vq->priv_data;
  662. v4l2_m2m_buf_queue(ctx->m2m_ctx, vq, vb);
  663. }
  664. static struct videobuf_queue_ops m2mtest_qops = {
  665. .buf_setup = m2mtest_buf_setup,
  666. .buf_prepare = m2mtest_buf_prepare,
  667. .buf_queue = m2mtest_buf_queue,
  668. .buf_release = m2mtest_buf_release,
  669. };
  670. static void queue_init(void *priv, struct videobuf_queue *vq,
  671. enum v4l2_buf_type type)
  672. {
  673. struct m2mtest_ctx *ctx = priv;
  674. struct m2mtest_dev *dev = ctx->dev;
  675. videobuf_queue_vmalloc_init(vq, &m2mtest_qops, dev->v4l2_dev.dev,
  676. &dev->irqlock, type, V4L2_FIELD_NONE,
  677. sizeof(struct m2mtest_buffer), priv,
  678. &dev->dev_mutex);
  679. }
  680. /*
  681. * File operations
  682. */
  683. static int m2mtest_open(struct file *file)
  684. {
  685. struct m2mtest_dev *dev = video_drvdata(file);
  686. struct m2mtest_ctx *ctx = NULL;
  687. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  688. if (!ctx)
  689. return -ENOMEM;
  690. file->private_data = ctx;
  691. ctx->dev = dev;
  692. ctx->translen = MEM2MEM_DEF_TRANSLEN;
  693. ctx->transtime = MEM2MEM_DEF_TRANSTIME;
  694. ctx->num_processed = 0;
  695. ctx->m2m_ctx = v4l2_m2m_ctx_init(ctx, dev->m2m_dev, queue_init);
  696. if (IS_ERR(ctx->m2m_ctx)) {
  697. int ret = PTR_ERR(ctx->m2m_ctx);
  698. kfree(ctx);
  699. return ret;
  700. }
  701. atomic_inc(&dev->num_inst);
  702. dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
  703. return 0;
  704. }
  705. static int m2mtest_release(struct file *file)
  706. {
  707. struct m2mtest_dev *dev = video_drvdata(file);
  708. struct m2mtest_ctx *ctx = file->private_data;
  709. dprintk(dev, "Releasing instance %p\n", ctx);
  710. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  711. kfree(ctx);
  712. atomic_dec(&dev->num_inst);
  713. return 0;
  714. }
  715. static unsigned int m2mtest_poll(struct file *file,
  716. struct poll_table_struct *wait)
  717. {
  718. struct m2mtest_ctx *ctx = file->private_data;
  719. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  720. }
  721. static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
  722. {
  723. struct m2mtest_ctx *ctx = file->private_data;
  724. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  725. }
  726. static const struct v4l2_file_operations m2mtest_fops = {
  727. .owner = THIS_MODULE,
  728. .open = m2mtest_open,
  729. .release = m2mtest_release,
  730. .poll = m2mtest_poll,
  731. .unlocked_ioctl = video_ioctl2,
  732. .mmap = m2mtest_mmap,
  733. };
  734. static struct video_device m2mtest_videodev = {
  735. .name = MEM2MEM_NAME,
  736. .fops = &m2mtest_fops,
  737. .ioctl_ops = &m2mtest_ioctl_ops,
  738. .minor = -1,
  739. .release = video_device_release,
  740. };
  741. static struct v4l2_m2m_ops m2m_ops = {
  742. .device_run = device_run,
  743. .job_ready = job_ready,
  744. .job_abort = job_abort,
  745. };
  746. static int m2mtest_probe(struct platform_device *pdev)
  747. {
  748. struct m2mtest_dev *dev;
  749. struct video_device *vfd;
  750. int ret;
  751. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  752. if (!dev)
  753. return -ENOMEM;
  754. spin_lock_init(&dev->irqlock);
  755. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  756. if (ret)
  757. goto free_dev;
  758. atomic_set(&dev->num_inst, 0);
  759. mutex_init(&dev->dev_mutex);
  760. vfd = video_device_alloc();
  761. if (!vfd) {
  762. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  763. ret = -ENOMEM;
  764. goto unreg_dev;
  765. }
  766. *vfd = m2mtest_videodev;
  767. vfd->lock = &dev->dev_mutex;
  768. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  769. if (ret) {
  770. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  771. goto rel_vdev;
  772. }
  773. video_set_drvdata(vfd, dev);
  774. snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
  775. dev->vfd = vfd;
  776. v4l2_info(&dev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
  777. "Device registered as /dev/video%d\n", vfd->num);
  778. setup_timer(&dev->timer, device_isr, (long)dev);
  779. platform_set_drvdata(pdev, dev);
  780. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  781. if (IS_ERR(dev->m2m_dev)) {
  782. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  783. ret = PTR_ERR(dev->m2m_dev);
  784. goto err_m2m;
  785. }
  786. q_data[V4L2_M2M_SRC].fmt = &formats[0];
  787. q_data[V4L2_M2M_DST].fmt = &formats[0];
  788. return 0;
  789. err_m2m:
  790. video_unregister_device(dev->vfd);
  791. rel_vdev:
  792. video_device_release(vfd);
  793. unreg_dev:
  794. v4l2_device_unregister(&dev->v4l2_dev);
  795. free_dev:
  796. kfree(dev);
  797. return ret;
  798. }
  799. static int m2mtest_remove(struct platform_device *pdev)
  800. {
  801. struct m2mtest_dev *dev =
  802. (struct m2mtest_dev *)platform_get_drvdata(pdev);
  803. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
  804. v4l2_m2m_release(dev->m2m_dev);
  805. del_timer_sync(&dev->timer);
  806. video_unregister_device(dev->vfd);
  807. v4l2_device_unregister(&dev->v4l2_dev);
  808. kfree(dev);
  809. return 0;
  810. }
  811. static struct platform_driver m2mtest_pdrv = {
  812. .probe = m2mtest_probe,
  813. .remove = m2mtest_remove,
  814. .driver = {
  815. .name = MEM2MEM_NAME,
  816. .owner = THIS_MODULE,
  817. },
  818. };
  819. static void __exit m2mtest_exit(void)
  820. {
  821. platform_driver_unregister(&m2mtest_pdrv);
  822. platform_device_unregister(&m2mtest_pdev);
  823. }
  824. static int __init m2mtest_init(void)
  825. {
  826. int ret;
  827. ret = platform_device_register(&m2mtest_pdev);
  828. if (ret)
  829. return ret;
  830. ret = platform_driver_register(&m2mtest_pdrv);
  831. if (ret)
  832. platform_device_unregister(&m2mtest_pdev);
  833. return 0;
  834. }
  835. module_init(m2mtest_init);
  836. module_exit(m2mtest_exit);