mem2mem_testdev.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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/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/videobuf2-vmalloc.h>
  31. #define MEM2MEM_TEST_MODULE_NAME "mem2mem-testdev"
  32. MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
  33. MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  34. MODULE_LICENSE("GPL");
  35. #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. static struct v4l2_queryctrl *get_ctrl(int id)
  169. {
  170. int i;
  171. for (i = 0; i < ARRAY_SIZE(m2mtest_ctrls); ++i) {
  172. if (id == m2mtest_ctrls[i].id)
  173. return &m2mtest_ctrls[i];
  174. }
  175. return NULL;
  176. }
  177. static int device_process(struct m2mtest_ctx *ctx,
  178. struct vb2_buffer *in_vb,
  179. struct vb2_buffer *out_vb)
  180. {
  181. struct m2mtest_dev *dev = ctx->dev;
  182. struct m2mtest_q_data *q_data;
  183. u8 *p_in, *p_out;
  184. int x, y, t, w;
  185. int tile_w, bytes_left;
  186. int width, height, bytesperline;
  187. q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_OUTPUT);
  188. width = q_data->width;
  189. height = q_data->height;
  190. bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  191. p_in = vb2_plane_vaddr(in_vb, 0);
  192. p_out = vb2_plane_vaddr(out_vb, 0);
  193. if (!p_in || !p_out) {
  194. v4l2_err(&dev->v4l2_dev,
  195. "Acquiring kernel pointers to buffers failed\n");
  196. return -EFAULT;
  197. }
  198. if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
  199. v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
  200. return -EINVAL;
  201. }
  202. tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
  203. / MEM2MEM_NUM_TILES;
  204. bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
  205. w = 0;
  206. for (y = 0; y < height; ++y) {
  207. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  208. if (w & 0x1) {
  209. for (x = 0; x < tile_w; ++x)
  210. *p_out++ = *p_in++ + MEM2MEM_COLOR_STEP;
  211. } else {
  212. for (x = 0; x < tile_w; ++x)
  213. *p_out++ = *p_in++ - MEM2MEM_COLOR_STEP;
  214. }
  215. ++w;
  216. }
  217. p_in += bytes_left;
  218. p_out += bytes_left;
  219. }
  220. return 0;
  221. }
  222. static void schedule_irq(struct m2mtest_dev *dev, int msec_timeout)
  223. {
  224. dprintk(dev, "Scheduling a simulated irq\n");
  225. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
  226. }
  227. /*
  228. * mem2mem callbacks
  229. */
  230. /**
  231. * job_ready() - check whether an instance is ready to be scheduled to run
  232. */
  233. static int job_ready(void *priv)
  234. {
  235. struct m2mtest_ctx *ctx = priv;
  236. if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < ctx->translen
  237. || v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) < ctx->translen) {
  238. dprintk(ctx->dev, "Not enough buffers available\n");
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. static void job_abort(void *priv)
  244. {
  245. struct m2mtest_ctx *ctx = priv;
  246. /* Will cancel the transaction in the next interrupt handler */
  247. ctx->aborting = 1;
  248. }
  249. static void m2mtest_lock(void *priv)
  250. {
  251. struct m2mtest_ctx *ctx = priv;
  252. struct m2mtest_dev *dev = ctx->dev;
  253. mutex_lock(&dev->dev_mutex);
  254. }
  255. static void m2mtest_unlock(void *priv)
  256. {
  257. struct m2mtest_ctx *ctx = priv;
  258. struct m2mtest_dev *dev = ctx->dev;
  259. mutex_unlock(&dev->dev_mutex);
  260. }
  261. /* device_run() - prepares and starts the device
  262. *
  263. * This simulates all the immediate preparations required before starting
  264. * a device. This will be called by the framework when it decides to schedule
  265. * a particular instance.
  266. */
  267. static void device_run(void *priv)
  268. {
  269. struct m2mtest_ctx *ctx = priv;
  270. struct m2mtest_dev *dev = ctx->dev;
  271. struct vb2_buffer *src_buf, *dst_buf;
  272. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  273. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  274. device_process(ctx, src_buf, dst_buf);
  275. /* Run a timer, which simulates a hardware irq */
  276. schedule_irq(dev, ctx->transtime);
  277. }
  278. static void device_isr(unsigned long priv)
  279. {
  280. struct m2mtest_dev *m2mtest_dev = (struct m2mtest_dev *)priv;
  281. struct m2mtest_ctx *curr_ctx;
  282. struct vb2_buffer *src_vb, *dst_vb;
  283. unsigned long flags;
  284. curr_ctx = v4l2_m2m_get_curr_priv(m2mtest_dev->m2m_dev);
  285. if (NULL == curr_ctx) {
  286. printk(KERN_ERR
  287. "Instance released before the end of transaction\n");
  288. return;
  289. }
  290. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  291. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  292. curr_ctx->num_processed++;
  293. spin_lock_irqsave(&m2mtest_dev->irqlock, flags);
  294. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  295. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  296. spin_unlock_irqrestore(&m2mtest_dev->irqlock, flags);
  297. if (curr_ctx->num_processed == curr_ctx->translen
  298. || curr_ctx->aborting) {
  299. dprintk(curr_ctx->dev, "Finishing transaction\n");
  300. curr_ctx->num_processed = 0;
  301. v4l2_m2m_job_finish(m2mtest_dev->m2m_dev, curr_ctx->m2m_ctx);
  302. } else {
  303. device_run(curr_ctx);
  304. }
  305. }
  306. /*
  307. * video ioctls
  308. */
  309. static int vidioc_querycap(struct file *file, void *priv,
  310. struct v4l2_capability *cap)
  311. {
  312. strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
  313. strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
  314. cap->bus_info[0] = 0;
  315. cap->version = KERNEL_VERSION(0, 1, 0);
  316. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
  317. | V4L2_CAP_STREAMING;
  318. return 0;
  319. }
  320. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  321. {
  322. int i, num;
  323. struct m2mtest_fmt *fmt;
  324. num = 0;
  325. for (i = 0; i < NUM_FORMATS; ++i) {
  326. if (formats[i].types & type) {
  327. /* index-th format of type type found ? */
  328. if (num == f->index)
  329. break;
  330. /* Correct type but haven't reached our index yet,
  331. * just increment per-type index */
  332. ++num;
  333. }
  334. }
  335. if (i < NUM_FORMATS) {
  336. /* Format found */
  337. fmt = &formats[i];
  338. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  339. f->pixelformat = fmt->fourcc;
  340. return 0;
  341. }
  342. /* Format not found */
  343. return -EINVAL;
  344. }
  345. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  346. struct v4l2_fmtdesc *f)
  347. {
  348. return enum_fmt(f, MEM2MEM_CAPTURE);
  349. }
  350. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  351. struct v4l2_fmtdesc *f)
  352. {
  353. return enum_fmt(f, MEM2MEM_OUTPUT);
  354. }
  355. static int vidioc_g_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  356. {
  357. struct vb2_queue *vq;
  358. struct m2mtest_q_data *q_data;
  359. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  360. if (!vq)
  361. return -EINVAL;
  362. q_data = get_q_data(f->type);
  363. f->fmt.pix.width = q_data->width;
  364. f->fmt.pix.height = q_data->height;
  365. f->fmt.pix.field = V4L2_FIELD_NONE;
  366. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  367. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  368. f->fmt.pix.sizeimage = q_data->sizeimage;
  369. return 0;
  370. }
  371. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  372. struct v4l2_format *f)
  373. {
  374. return vidioc_g_fmt(priv, f);
  375. }
  376. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  377. struct v4l2_format *f)
  378. {
  379. return vidioc_g_fmt(priv, f);
  380. }
  381. static int vidioc_try_fmt(struct v4l2_format *f, struct m2mtest_fmt *fmt)
  382. {
  383. enum v4l2_field field;
  384. field = f->fmt.pix.field;
  385. if (field == V4L2_FIELD_ANY)
  386. field = V4L2_FIELD_NONE;
  387. else if (V4L2_FIELD_NONE != field)
  388. return -EINVAL;
  389. /* V4L2 specification suggests the driver corrects the format struct
  390. * if any of the dimensions is unsupported */
  391. f->fmt.pix.field = field;
  392. if (f->fmt.pix.height < MIN_H)
  393. f->fmt.pix.height = MIN_H;
  394. else if (f->fmt.pix.height > MAX_H)
  395. f->fmt.pix.height = MAX_H;
  396. if (f->fmt.pix.width < MIN_W)
  397. f->fmt.pix.width = MIN_W;
  398. else if (f->fmt.pix.width > MAX_W)
  399. f->fmt.pix.width = MAX_W;
  400. f->fmt.pix.width &= ~DIM_ALIGN_MASK;
  401. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  402. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  403. return 0;
  404. }
  405. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  406. struct v4l2_format *f)
  407. {
  408. struct m2mtest_fmt *fmt;
  409. struct m2mtest_ctx *ctx = priv;
  410. fmt = find_format(f);
  411. if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
  412. v4l2_err(&ctx->dev->v4l2_dev,
  413. "Fourcc format (0x%08x) invalid.\n",
  414. f->fmt.pix.pixelformat);
  415. return -EINVAL;
  416. }
  417. return vidioc_try_fmt(f, fmt);
  418. }
  419. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  420. struct v4l2_format *f)
  421. {
  422. struct m2mtest_fmt *fmt;
  423. struct m2mtest_ctx *ctx = priv;
  424. fmt = find_format(f);
  425. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
  426. v4l2_err(&ctx->dev->v4l2_dev,
  427. "Fourcc format (0x%08x) invalid.\n",
  428. f->fmt.pix.pixelformat);
  429. return -EINVAL;
  430. }
  431. return vidioc_try_fmt(f, fmt);
  432. }
  433. static int vidioc_s_fmt(struct m2mtest_ctx *ctx, struct v4l2_format *f)
  434. {
  435. struct m2mtest_q_data *q_data;
  436. struct vb2_queue *vq;
  437. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  438. if (!vq)
  439. return -EINVAL;
  440. q_data = get_q_data(f->type);
  441. if (!q_data)
  442. return -EINVAL;
  443. if (vb2_is_busy(vq)) {
  444. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  445. return -EBUSY;
  446. }
  447. q_data->fmt = find_format(f);
  448. q_data->width = f->fmt.pix.width;
  449. q_data->height = f->fmt.pix.height;
  450. q_data->sizeimage = q_data->width * q_data->height
  451. * q_data->fmt->depth >> 3;
  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. return 0;
  456. }
  457. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  458. struct v4l2_format *f)
  459. {
  460. int ret;
  461. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  462. if (ret)
  463. return ret;
  464. return vidioc_s_fmt(priv, f);
  465. }
  466. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  467. struct v4l2_format *f)
  468. {
  469. int ret;
  470. ret = vidioc_try_fmt_vid_out(file, priv, f);
  471. if (ret)
  472. return ret;
  473. return vidioc_s_fmt(priv, f);
  474. }
  475. static int vidioc_reqbufs(struct file *file, void *priv,
  476. struct v4l2_requestbuffers *reqbufs)
  477. {
  478. struct m2mtest_ctx *ctx = priv;
  479. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  480. }
  481. static int vidioc_querybuf(struct file *file, void *priv,
  482. struct v4l2_buffer *buf)
  483. {
  484. struct m2mtest_ctx *ctx = priv;
  485. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  486. }
  487. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  488. {
  489. struct m2mtest_ctx *ctx = priv;
  490. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  491. }
  492. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  493. {
  494. struct m2mtest_ctx *ctx = priv;
  495. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  496. }
  497. static int vidioc_streamon(struct file *file, void *priv,
  498. enum v4l2_buf_type type)
  499. {
  500. struct m2mtest_ctx *ctx = priv;
  501. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  502. }
  503. static int vidioc_streamoff(struct file *file, void *priv,
  504. enum v4l2_buf_type type)
  505. {
  506. struct m2mtest_ctx *ctx = priv;
  507. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  508. }
  509. static int vidioc_queryctrl(struct file *file, void *priv,
  510. struct v4l2_queryctrl *qc)
  511. {
  512. struct v4l2_queryctrl *c;
  513. c = get_ctrl(qc->id);
  514. if (!c)
  515. return -EINVAL;
  516. *qc = *c;
  517. return 0;
  518. }
  519. static int vidioc_g_ctrl(struct file *file, void *priv,
  520. struct v4l2_control *ctrl)
  521. {
  522. struct m2mtest_ctx *ctx = priv;
  523. switch (ctrl->id) {
  524. case V4L2_CID_TRANS_TIME_MSEC:
  525. ctrl->value = ctx->transtime;
  526. break;
  527. case V4L2_CID_TRANS_NUM_BUFS:
  528. ctrl->value = ctx->translen;
  529. break;
  530. default:
  531. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  532. return -EINVAL;
  533. }
  534. return 0;
  535. }
  536. static int check_ctrl_val(struct m2mtest_ctx *ctx, struct v4l2_control *ctrl)
  537. {
  538. struct v4l2_queryctrl *c;
  539. c = get_ctrl(ctrl->id);
  540. if (!c)
  541. return -EINVAL;
  542. if (ctrl->value < c->minimum || ctrl->value > c->maximum) {
  543. v4l2_err(&ctx->dev->v4l2_dev, "Value out of range\n");
  544. return -ERANGE;
  545. }
  546. return 0;
  547. }
  548. static int vidioc_s_ctrl(struct file *file, void *priv,
  549. struct v4l2_control *ctrl)
  550. {
  551. struct m2mtest_ctx *ctx = priv;
  552. int ret = 0;
  553. ret = check_ctrl_val(ctx, ctrl);
  554. if (ret != 0)
  555. return ret;
  556. switch (ctrl->id) {
  557. case V4L2_CID_TRANS_TIME_MSEC:
  558. ctx->transtime = ctrl->value;
  559. break;
  560. case V4L2_CID_TRANS_NUM_BUFS:
  561. ctx->translen = ctrl->value;
  562. break;
  563. default:
  564. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  565. return -EINVAL;
  566. }
  567. return 0;
  568. }
  569. static const struct v4l2_ioctl_ops m2mtest_ioctl_ops = {
  570. .vidioc_querycap = vidioc_querycap,
  571. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  572. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  573. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  574. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  575. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  576. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  577. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  578. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  579. .vidioc_reqbufs = vidioc_reqbufs,
  580. .vidioc_querybuf = vidioc_querybuf,
  581. .vidioc_qbuf = vidioc_qbuf,
  582. .vidioc_dqbuf = vidioc_dqbuf,
  583. .vidioc_streamon = vidioc_streamon,
  584. .vidioc_streamoff = vidioc_streamoff,
  585. .vidioc_queryctrl = vidioc_queryctrl,
  586. .vidioc_g_ctrl = vidioc_g_ctrl,
  587. .vidioc_s_ctrl = vidioc_s_ctrl,
  588. };
  589. /*
  590. * Queue operations
  591. */
  592. static int m2mtest_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
  593. unsigned int *nplanes, unsigned long sizes[],
  594. void *alloc_ctxs[])
  595. {
  596. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vq);
  597. struct m2mtest_q_data *q_data;
  598. unsigned int size, count = *nbuffers;
  599. q_data = get_q_data(vq->type);
  600. size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  601. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  602. (count)--;
  603. *nplanes = 1;
  604. *nbuffers = count;
  605. sizes[0] = size;
  606. /*
  607. * videobuf2-vmalloc allocator is context-less so no need to set
  608. * alloc_ctxs array.
  609. */
  610. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  611. return 0;
  612. }
  613. static int m2mtest_buf_prepare(struct vb2_buffer *vb)
  614. {
  615. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  616. struct m2mtest_q_data *q_data;
  617. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  618. q_data = get_q_data(vb->vb2_queue->type);
  619. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  620. dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
  621. __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
  622. return -EINVAL;
  623. }
  624. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  625. return 0;
  626. }
  627. static void m2mtest_buf_queue(struct vb2_buffer *vb)
  628. {
  629. struct m2mtest_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  630. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  631. }
  632. static struct vb2_ops m2mtest_qops = {
  633. .queue_setup = m2mtest_queue_setup,
  634. .buf_prepare = m2mtest_buf_prepare,
  635. .buf_queue = m2mtest_buf_queue,
  636. };
  637. static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
  638. {
  639. struct m2mtest_ctx *ctx = priv;
  640. int ret;
  641. memset(src_vq, 0, sizeof(*src_vq));
  642. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  643. src_vq->io_modes = VB2_MMAP;
  644. src_vq->drv_priv = ctx;
  645. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  646. src_vq->ops = &m2mtest_qops;
  647. src_vq->mem_ops = &vb2_vmalloc_memops;
  648. ret = vb2_queue_init(src_vq);
  649. if (ret)
  650. return ret;
  651. memset(dst_vq, 0, sizeof(*dst_vq));
  652. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  653. dst_vq->io_modes = VB2_MMAP;
  654. dst_vq->drv_priv = ctx;
  655. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  656. dst_vq->ops = &m2mtest_qops;
  657. dst_vq->mem_ops = &vb2_vmalloc_memops;
  658. return vb2_queue_init(dst_vq);
  659. }
  660. /*
  661. * File operations
  662. */
  663. static int m2mtest_open(struct file *file)
  664. {
  665. struct m2mtest_dev *dev = video_drvdata(file);
  666. struct m2mtest_ctx *ctx = NULL;
  667. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  668. if (!ctx)
  669. return -ENOMEM;
  670. file->private_data = ctx;
  671. ctx->dev = dev;
  672. ctx->translen = MEM2MEM_DEF_TRANSLEN;
  673. ctx->transtime = MEM2MEM_DEF_TRANSTIME;
  674. ctx->num_processed = 0;
  675. ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  676. if (IS_ERR(ctx->m2m_ctx)) {
  677. int ret = PTR_ERR(ctx->m2m_ctx);
  678. kfree(ctx);
  679. return ret;
  680. }
  681. atomic_inc(&dev->num_inst);
  682. dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
  683. return 0;
  684. }
  685. static int m2mtest_release(struct file *file)
  686. {
  687. struct m2mtest_dev *dev = video_drvdata(file);
  688. struct m2mtest_ctx *ctx = file->private_data;
  689. dprintk(dev, "Releasing instance %p\n", ctx);
  690. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  691. kfree(ctx);
  692. atomic_dec(&dev->num_inst);
  693. return 0;
  694. }
  695. static unsigned int m2mtest_poll(struct file *file,
  696. struct poll_table_struct *wait)
  697. {
  698. struct m2mtest_ctx *ctx = file->private_data;
  699. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  700. }
  701. static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
  702. {
  703. struct m2mtest_ctx *ctx = file->private_data;
  704. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  705. }
  706. static const struct v4l2_file_operations m2mtest_fops = {
  707. .owner = THIS_MODULE,
  708. .open = m2mtest_open,
  709. .release = m2mtest_release,
  710. .poll = m2mtest_poll,
  711. .unlocked_ioctl = video_ioctl2,
  712. .mmap = m2mtest_mmap,
  713. };
  714. static struct video_device m2mtest_videodev = {
  715. .name = MEM2MEM_NAME,
  716. .fops = &m2mtest_fops,
  717. .ioctl_ops = &m2mtest_ioctl_ops,
  718. .minor = -1,
  719. .release = video_device_release,
  720. };
  721. static struct v4l2_m2m_ops m2m_ops = {
  722. .device_run = device_run,
  723. .job_ready = job_ready,
  724. .job_abort = job_abort,
  725. .lock = m2mtest_lock,
  726. .unlock = m2mtest_unlock,
  727. };
  728. static int m2mtest_probe(struct platform_device *pdev)
  729. {
  730. struct m2mtest_dev *dev;
  731. struct video_device *vfd;
  732. int ret;
  733. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  734. if (!dev)
  735. return -ENOMEM;
  736. spin_lock_init(&dev->irqlock);
  737. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  738. if (ret)
  739. goto free_dev;
  740. atomic_set(&dev->num_inst, 0);
  741. mutex_init(&dev->dev_mutex);
  742. vfd = video_device_alloc();
  743. if (!vfd) {
  744. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  745. ret = -ENOMEM;
  746. goto unreg_dev;
  747. }
  748. *vfd = m2mtest_videodev;
  749. vfd->lock = &dev->dev_mutex;
  750. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  751. if (ret) {
  752. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  753. goto rel_vdev;
  754. }
  755. video_set_drvdata(vfd, dev);
  756. snprintf(vfd->name, sizeof(vfd->name), "%s", m2mtest_videodev.name);
  757. dev->vfd = vfd;
  758. v4l2_info(&dev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
  759. "Device registered as /dev/video%d\n", vfd->num);
  760. setup_timer(&dev->timer, device_isr, (long)dev);
  761. platform_set_drvdata(pdev, dev);
  762. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  763. if (IS_ERR(dev->m2m_dev)) {
  764. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  765. ret = PTR_ERR(dev->m2m_dev);
  766. goto err_m2m;
  767. }
  768. q_data[V4L2_M2M_SRC].fmt = &formats[0];
  769. q_data[V4L2_M2M_DST].fmt = &formats[0];
  770. return 0;
  771. v4l2_m2m_release(dev->m2m_dev);
  772. err_m2m:
  773. video_unregister_device(dev->vfd);
  774. rel_vdev:
  775. video_device_release(vfd);
  776. unreg_dev:
  777. v4l2_device_unregister(&dev->v4l2_dev);
  778. free_dev:
  779. kfree(dev);
  780. return ret;
  781. }
  782. static int m2mtest_remove(struct platform_device *pdev)
  783. {
  784. struct m2mtest_dev *dev =
  785. (struct m2mtest_dev *)platform_get_drvdata(pdev);
  786. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
  787. v4l2_m2m_release(dev->m2m_dev);
  788. del_timer_sync(&dev->timer);
  789. video_unregister_device(dev->vfd);
  790. v4l2_device_unregister(&dev->v4l2_dev);
  791. kfree(dev);
  792. return 0;
  793. }
  794. static struct platform_driver m2mtest_pdrv = {
  795. .probe = m2mtest_probe,
  796. .remove = m2mtest_remove,
  797. .driver = {
  798. .name = MEM2MEM_NAME,
  799. .owner = THIS_MODULE,
  800. },
  801. };
  802. static void __exit m2mtest_exit(void)
  803. {
  804. platform_driver_unregister(&m2mtest_pdrv);
  805. platform_device_unregister(&m2mtest_pdev);
  806. }
  807. static int __init m2mtest_init(void)
  808. {
  809. int ret;
  810. ret = platform_device_register(&m2mtest_pdev);
  811. if (ret)
  812. return ret;
  813. ret = platform_driver_register(&m2mtest_pdrv);
  814. if (ret)
  815. platform_device_unregister(&m2mtest_pdev);
  816. return 0;
  817. }
  818. module_init(m2mtest_init);
  819. module_exit(m2mtest_exit);