mem2mem_testdev.c 26 KB

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