mem2mem_testdev.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. int ret = 0;
  434. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  435. if (!vq)
  436. return -EINVAL;
  437. q_data = get_q_data(f->type);
  438. if (!q_data)
  439. return -EINVAL;
  440. mutex_lock(&vq->vb_lock);
  441. if (videobuf_queue_is_busy(vq)) {
  442. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  443. ret = -EBUSY;
  444. goto out;
  445. }
  446. q_data->fmt = find_format(f);
  447. q_data->width = f->fmt.pix.width;
  448. q_data->height = f->fmt.pix.height;
  449. q_data->sizeimage = q_data->width * q_data->height
  450. * q_data->fmt->depth >> 3;
  451. vq->field = f->fmt.pix.field;
  452. dprintk(ctx->dev,
  453. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  454. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  455. out:
  456. mutex_unlock(&vq->vb_lock);
  457. return ret;
  458. }
  459. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  460. struct v4l2_format *f)
  461. {
  462. int ret;
  463. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  464. if (ret)
  465. return ret;
  466. return vidioc_s_fmt(priv, f);
  467. }
  468. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  469. struct v4l2_format *f)
  470. {
  471. int ret;
  472. ret = vidioc_try_fmt_vid_out(file, priv, f);
  473. if (ret)
  474. return ret;
  475. return vidioc_s_fmt(priv, f);
  476. }
  477. static int vidioc_reqbufs(struct file *file, void *priv,
  478. struct v4l2_requestbuffers *reqbufs)
  479. {
  480. struct m2mtest_ctx *ctx = priv;
  481. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  482. }
  483. static int vidioc_querybuf(struct file *file, void *priv,
  484. struct v4l2_buffer *buf)
  485. {
  486. struct m2mtest_ctx *ctx = priv;
  487. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  488. }
  489. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  490. {
  491. struct m2mtest_ctx *ctx = priv;
  492. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  493. }
  494. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  495. {
  496. struct m2mtest_ctx *ctx = priv;
  497. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  498. }
  499. static int vidioc_streamon(struct file *file, void *priv,
  500. enum v4l2_buf_type type)
  501. {
  502. struct m2mtest_ctx *ctx = priv;
  503. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  504. }
  505. static int vidioc_streamoff(struct file *file, void *priv,
  506. enum v4l2_buf_type type)
  507. {
  508. struct m2mtest_ctx *ctx = priv;
  509. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  510. }
  511. static int vidioc_queryctrl(struct file *file, void *priv,
  512. struct v4l2_queryctrl *qc)
  513. {
  514. struct v4l2_queryctrl *c;
  515. c = get_ctrl(qc->id);
  516. if (!c)
  517. return -EINVAL;
  518. *qc = *c;
  519. return 0;
  520. }
  521. static int vidioc_g_ctrl(struct file *file, void *priv,
  522. struct v4l2_control *ctrl)
  523. {
  524. struct m2mtest_ctx *ctx = priv;
  525. switch (ctrl->id) {
  526. case V4L2_CID_TRANS_TIME_MSEC:
  527. ctrl->value = ctx->transtime;
  528. break;
  529. case V4L2_CID_TRANS_NUM_BUFS:
  530. ctrl->value = ctx->translen;
  531. break;
  532. default:
  533. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  534. return -EINVAL;
  535. }
  536. return 0;
  537. }
  538. static int check_ctrl_val(struct m2mtest_ctx *ctx, struct v4l2_control *ctrl)
  539. {
  540. struct v4l2_queryctrl *c;
  541. c = get_ctrl(ctrl->id);
  542. if (!c)
  543. return -EINVAL;
  544. if (ctrl->value < c->minimum || ctrl->value > c->maximum) {
  545. v4l2_err(&ctx->dev->v4l2_dev, "Value out of range\n");
  546. return -ERANGE;
  547. }
  548. return 0;
  549. }
  550. static int vidioc_s_ctrl(struct file *file, void *priv,
  551. struct v4l2_control *ctrl)
  552. {
  553. struct m2mtest_ctx *ctx = priv;
  554. int ret = 0;
  555. ret = check_ctrl_val(ctx, ctrl);
  556. if (ret != 0)
  557. return ret;
  558. switch (ctrl->id) {
  559. case V4L2_CID_TRANS_TIME_MSEC:
  560. ctx->transtime = ctrl->value;
  561. break;
  562. case V4L2_CID_TRANS_NUM_BUFS:
  563. ctx->translen = ctrl->value;
  564. break;
  565. default:
  566. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  567. return -EINVAL;
  568. }
  569. return 0;
  570. }
  571. static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
  572. .vidioc_querycap = vidioc_querycap,
  573. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  574. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  575. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  576. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  577. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  578. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  579. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  580. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  581. .vidioc_reqbufs = vidioc_reqbufs,
  582. .vidioc_querybuf = vidioc_querybuf,
  583. .vidioc_qbuf = vidioc_qbuf,
  584. .vidioc_dqbuf = vidioc_dqbuf,
  585. .vidioc_streamon = vidioc_streamon,
  586. .vidioc_streamoff = vidioc_streamoff,
  587. .vidioc_queryctrl = vidioc_queryctrl,
  588. .vidioc_g_ctrl = vidioc_g_ctrl,
  589. .vidioc_s_ctrl = vidioc_s_ctrl,
  590. };
  591. /*
  592. * Queue operations
  593. */
  594. static void m2mtest_buf_release(struct videobuf_queue *vq,
  595. struct videobuf_buffer *vb)
  596. {
  597. struct m2mtest_ctx *ctx = vq->priv_data;
  598. dprintk(ctx->dev, "type: %d, index: %d, state: %d\n",
  599. vq->type, vb->i, vb->state);
  600. videobuf_vmalloc_free(vb);
  601. vb->state = VIDEOBUF_NEEDS_INIT;
  602. }
  603. static int m2mtest_buf_setup(struct videobuf_queue *vq, unsigned int *count,
  604. unsigned int *size)
  605. {
  606. struct m2mtest_ctx *ctx = vq->priv_data;
  607. struct m2mtest_q_data *q_data;
  608. q_data = get_q_data(vq->type);
  609. *size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  610. dprintk(ctx->dev, "size:%d, w/h %d/%d, depth: %d\n",
  611. *size, q_data->width, q_data->height, q_data->fmt->depth);
  612. if (0 == *count)
  613. *count = MEM2MEM_DEF_NUM_BUFS;
  614. while (*size * *count > MEM2MEM_VID_MEM_LIMIT)
  615. (*count)--;
  616. v4l2_info(&ctx->dev->v4l2_dev,
  617. "%d buffers of size %d set up.\n", *count, *size);
  618. return 0;
  619. }
  620. static int m2mtest_buf_prepare(struct videobuf_queue *vq,
  621. struct videobuf_buffer *vb,
  622. enum v4l2_field field)
  623. {
  624. struct m2mtest_ctx *ctx = vq->priv_data;
  625. struct m2mtest_q_data *q_data;
  626. int ret;
  627. dprintk(ctx->dev, "type: %d, index: %d, state: %d\n",
  628. vq->type, vb->i, vb->state);
  629. q_data = get_q_data(vq->type);
  630. if (vb->baddr) {
  631. /* User-provided buffer */
  632. if (vb->bsize < q_data->sizeimage) {
  633. /* Buffer too small to fit a frame */
  634. v4l2_err(&ctx->dev->v4l2_dev,
  635. "User-provided buffer too small\n");
  636. return -EINVAL;
  637. }
  638. } else if (vb->state != VIDEOBUF_NEEDS_INIT
  639. && vb->bsize < q_data->sizeimage) {
  640. /* We provide the buffer, but it's already been initialized
  641. * and is too small */
  642. return -EINVAL;
  643. }
  644. vb->width = q_data->width;
  645. vb->height = q_data->height;
  646. vb->bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  647. vb->size = q_data->sizeimage;
  648. vb->field = field;
  649. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  650. ret = videobuf_iolock(vq, vb, NULL);
  651. if (ret) {
  652. v4l2_err(&ctx->dev->v4l2_dev,
  653. "Iolock failed\n");
  654. goto fail;
  655. }
  656. }
  657. vb->state = VIDEOBUF_PREPARED;
  658. return 0;
  659. fail:
  660. m2mtest_buf_release(vq, vb);
  661. return ret;
  662. }
  663. static void m2mtest_buf_queue(struct videobuf_queue *vq,
  664. struct videobuf_buffer *vb)
  665. {
  666. struct m2mtest_ctx *ctx = vq->priv_data;
  667. v4l2_m2m_buf_queue(ctx->m2m_ctx, vq, vb);
  668. }
  669. static struct videobuf_queue_ops m2mtest_qops = {
  670. .buf_setup = m2mtest_buf_setup,
  671. .buf_prepare = m2mtest_buf_prepare,
  672. .buf_queue = m2mtest_buf_queue,
  673. .buf_release = m2mtest_buf_release,
  674. };
  675. static void queue_init(void *priv, struct videobuf_queue *vq,
  676. enum v4l2_buf_type type)
  677. {
  678. struct m2mtest_ctx *ctx = priv;
  679. videobuf_queue_vmalloc_init(vq, &m2mtest_qops, ctx->dev->v4l2_dev.dev,
  680. &ctx->dev->irqlock, type, V4L2_FIELD_NONE,
  681. sizeof(struct m2mtest_buffer), priv);
  682. }
  683. /*
  684. * File operations
  685. */
  686. static int m2mtest_open(struct file *file)
  687. {
  688. struct m2mtest_dev *dev = video_drvdata(file);
  689. struct m2mtest_ctx *ctx = NULL;
  690. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  691. if (!ctx)
  692. return -ENOMEM;
  693. file->private_data = ctx;
  694. ctx->dev = dev;
  695. ctx->translen = MEM2MEM_DEF_TRANSLEN;
  696. ctx->transtime = MEM2MEM_DEF_TRANSTIME;
  697. ctx->num_processed = 0;
  698. ctx->m2m_ctx = v4l2_m2m_ctx_init(ctx, dev->m2m_dev, queue_init);
  699. if (IS_ERR(ctx->m2m_ctx)) {
  700. int ret = PTR_ERR(ctx->m2m_ctx);
  701. kfree(ctx);
  702. return ret;
  703. }
  704. atomic_inc(&dev->num_inst);
  705. dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
  706. return 0;
  707. }
  708. static int m2mtest_release(struct file *file)
  709. {
  710. struct m2mtest_dev *dev = video_drvdata(file);
  711. struct m2mtest_ctx *ctx = file->private_data;
  712. dprintk(dev, "Releasing instance %p\n", ctx);
  713. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  714. kfree(ctx);
  715. atomic_dec(&dev->num_inst);
  716. return 0;
  717. }
  718. static unsigned int m2mtest_poll(struct file *file,
  719. struct poll_table_struct *wait)
  720. {
  721. struct m2mtest_ctx *ctx = file->private_data;
  722. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  723. }
  724. static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
  725. {
  726. struct m2mtest_ctx *ctx = file->private_data;
  727. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  728. }
  729. static const struct v4l2_file_operations m2mtest_fops = {
  730. .owner = THIS_MODULE,
  731. .open = m2mtest_open,
  732. .release = m2mtest_release,
  733. .poll = m2mtest_poll,
  734. .ioctl = video_ioctl2,
  735. .mmap = m2mtest_mmap,
  736. };
  737. static struct video_device m2mtest_videodev = {
  738. .name = MEM2MEM_NAME,
  739. .fops = &m2mtest_fops,
  740. .ioctl_ops = &m2mtest_ioctl_ops,
  741. .minor = -1,
  742. .release = video_device_release,
  743. };
  744. static struct v4l2_m2m_ops m2m_ops = {
  745. .device_run = device_run,
  746. .job_ready = job_ready,
  747. .job_abort = job_abort,
  748. };
  749. static int m2mtest_probe(struct platform_device *pdev)
  750. {
  751. struct m2mtest_dev *dev;
  752. struct video_device *vfd;
  753. int ret;
  754. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  755. if (!dev)
  756. return -ENOMEM;
  757. spin_lock_init(&dev->irqlock);
  758. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  759. if (ret)
  760. goto free_dev;
  761. atomic_set(&dev->num_inst, 0);
  762. mutex_init(&dev->dev_mutex);
  763. vfd = video_device_alloc();
  764. if (!vfd) {
  765. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  766. ret = -ENOMEM;
  767. goto unreg_dev;
  768. }
  769. *vfd = m2mtest_videodev;
  770. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  771. if (ret) {
  772. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  773. goto rel_vdev;
  774. }
  775. video_set_drvdata(vfd, dev);
  776. snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
  777. dev->vfd = vfd;
  778. v4l2_info(&dev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
  779. "Device registered as /dev/video%d\n", vfd->num);
  780. setup_timer(&dev->timer, device_isr, (long)dev);
  781. platform_set_drvdata(pdev, dev);
  782. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  783. if (IS_ERR(dev->m2m_dev)) {
  784. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  785. ret = PTR_ERR(dev->m2m_dev);
  786. goto err_m2m;
  787. }
  788. q_data[V4L2_M2M_SRC].fmt = &formats[0];
  789. q_data[V4L2_M2M_DST].fmt = &formats[0];
  790. return 0;
  791. err_m2m:
  792. video_unregister_device(dev->vfd);
  793. rel_vdev:
  794. video_device_release(vfd);
  795. unreg_dev:
  796. v4l2_device_unregister(&dev->v4l2_dev);
  797. free_dev:
  798. kfree(dev);
  799. return ret;
  800. }
  801. static int m2mtest_remove(struct platform_device *pdev)
  802. {
  803. struct m2mtest_dev *dev =
  804. (struct m2mtest_dev *)platform_get_drvdata(pdev);
  805. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
  806. v4l2_m2m_release(dev->m2m_dev);
  807. del_timer_sync(&dev->timer);
  808. video_unregister_device(dev->vfd);
  809. v4l2_device_unregister(&dev->v4l2_dev);
  810. kfree(dev);
  811. return 0;
  812. }
  813. static struct platform_driver m2mtest_pdrv = {
  814. .probe = m2mtest_probe,
  815. .remove = m2mtest_remove,
  816. .driver = {
  817. .name = MEM2MEM_NAME,
  818. .owner = THIS_MODULE,
  819. },
  820. };
  821. static void __exit m2mtest_exit(void)
  822. {
  823. platform_driver_unregister(&m2mtest_pdrv);
  824. platform_device_unregister(&m2mtest_pdev);
  825. }
  826. static int __init m2mtest_init(void)
  827. {
  828. int ret;
  829. ret = platform_device_register(&m2mtest_pdev);
  830. if (ret)
  831. return ret;
  832. ret = platform_driver_register(&m2mtest_pdrv);
  833. if (ret)
  834. platform_device_unregister(&m2mtest_pdev);
  835. return 0;
  836. }
  837. module_init(m2mtest_init);
  838. module_exit(m2mtest_exit);