mem2mem_testdev.c 26 KB

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