vim2m.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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. MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
  33. MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  34. MODULE_LICENSE("GPL");
  35. MODULE_VERSION("0.1.1");
  36. MODULE_ALIAS("mem2mem_testdev");
  37. static unsigned debug;
  38. module_param(debug, uint, 0644);
  39. MODULE_PARM_DESC(debug, "activates debug info");
  40. #define MIN_W 32
  41. #define MIN_H 32
  42. #define MAX_W 640
  43. #define MAX_H 480
  44. #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
  45. /* Flags that indicate a format can be used for capture/output */
  46. #define MEM2MEM_CAPTURE (1 << 0)
  47. #define MEM2MEM_OUTPUT (1 << 1)
  48. #define MEM2MEM_NAME "vim2m"
  49. /* Per queue */
  50. #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
  51. /* In bytes, per queue */
  52. #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
  53. /* Default transaction time in msec */
  54. #define MEM2MEM_DEF_TRANSTIME 40
  55. #define MEM2MEM_COLOR_STEP (0xff >> 4)
  56. #define MEM2MEM_NUM_TILES 8
  57. /* Flags that indicate processing mode */
  58. #define MEM2MEM_HFLIP (1 << 0)
  59. #define MEM2MEM_VFLIP (1 << 1)
  60. #define dprintk(dev, fmt, arg...) \
  61. v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
  62. static void vim2m_dev_release(struct device *dev)
  63. {}
  64. static struct platform_device vim2m_pdev = {
  65. .name = MEM2MEM_NAME,
  66. .dev.release = vim2m_dev_release,
  67. };
  68. struct vim2m_fmt {
  69. u32 fourcc;
  70. int depth;
  71. /* Types the format can be used for */
  72. u32 types;
  73. };
  74. static struct vim2m_fmt formats[] = {
  75. {
  76. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  77. .depth = 16,
  78. /* Both capture and output format */
  79. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  80. },
  81. {
  82. .fourcc = V4L2_PIX_FMT_YUYV,
  83. .depth = 16,
  84. /* Output-only format */
  85. .types = MEM2MEM_OUTPUT,
  86. },
  87. };
  88. #define NUM_FORMATS ARRAY_SIZE(formats)
  89. /* Per-queue, driver-specific private data */
  90. struct vim2m_q_data {
  91. unsigned int width;
  92. unsigned int height;
  93. unsigned int sizeimage;
  94. unsigned int sequence;
  95. struct vim2m_fmt *fmt;
  96. };
  97. enum {
  98. V4L2_M2M_SRC = 0,
  99. V4L2_M2M_DST = 1,
  100. };
  101. #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
  102. #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
  103. static struct vim2m_fmt *find_format(struct v4l2_format *f)
  104. {
  105. struct vim2m_fmt *fmt;
  106. unsigned int k;
  107. for (k = 0; k < NUM_FORMATS; k++) {
  108. fmt = &formats[k];
  109. if (fmt->fourcc == f->fmt.pix.pixelformat)
  110. break;
  111. }
  112. if (k == NUM_FORMATS)
  113. return NULL;
  114. return &formats[k];
  115. }
  116. struct vim2m_dev {
  117. struct v4l2_device v4l2_dev;
  118. struct video_device vfd;
  119. atomic_t num_inst;
  120. struct mutex dev_mutex;
  121. spinlock_t irqlock;
  122. struct timer_list timer;
  123. struct v4l2_m2m_dev *m2m_dev;
  124. };
  125. struct vim2m_ctx {
  126. struct v4l2_fh fh;
  127. struct vim2m_dev *dev;
  128. struct v4l2_ctrl_handler hdl;
  129. /* Processed buffers in this transaction */
  130. u8 num_processed;
  131. /* Transaction length (i.e. how many buffers per transaction) */
  132. u32 translen;
  133. /* Transaction time (i.e. simulated processing time) in milliseconds */
  134. u32 transtime;
  135. /* Abort requested by m2m */
  136. int aborting;
  137. /* Processing mode */
  138. int mode;
  139. enum v4l2_colorspace colorspace;
  140. /* Source and destination queue data */
  141. struct vim2m_q_data q_data[2];
  142. };
  143. static inline struct vim2m_ctx *file2ctx(struct file *file)
  144. {
  145. return container_of(file->private_data, struct vim2m_ctx, fh);
  146. }
  147. static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
  148. enum v4l2_buf_type type)
  149. {
  150. switch (type) {
  151. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  152. return &ctx->q_data[V4L2_M2M_SRC];
  153. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  154. return &ctx->q_data[V4L2_M2M_DST];
  155. default:
  156. BUG();
  157. }
  158. return NULL;
  159. }
  160. static int device_process(struct vim2m_ctx *ctx,
  161. struct vb2_v4l2_buffer *in_vb,
  162. struct vb2_v4l2_buffer *out_vb)
  163. {
  164. struct vim2m_dev *dev = ctx->dev;
  165. struct vim2m_q_data *q_data;
  166. u8 *p_in, *p_out;
  167. int x, y, t, w;
  168. int tile_w, bytes_left;
  169. int width, height, bytesperline;
  170. q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  171. width = q_data->width;
  172. height = q_data->height;
  173. bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  174. p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
  175. p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
  176. if (!p_in || !p_out) {
  177. v4l2_err(&dev->v4l2_dev,
  178. "Acquiring kernel pointers to buffers failed\n");
  179. return -EFAULT;
  180. }
  181. if (vb2_plane_size(&in_vb->vb2_buf, 0) >
  182. vb2_plane_size(&out_vb->vb2_buf, 0)) {
  183. v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
  184. return -EINVAL;
  185. }
  186. tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
  187. / MEM2MEM_NUM_TILES;
  188. bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
  189. w = 0;
  190. out_vb->sequence =
  191. get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
  192. in_vb->sequence = q_data->sequence++;
  193. out_vb->timestamp = in_vb->timestamp;
  194. if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE)
  195. out_vb->timecode = in_vb->timecode;
  196. out_vb->field = in_vb->field;
  197. out_vb->flags = in_vb->flags &
  198. (V4L2_BUF_FLAG_TIMECODE |
  199. V4L2_BUF_FLAG_KEYFRAME |
  200. V4L2_BUF_FLAG_PFRAME |
  201. V4L2_BUF_FLAG_BFRAME |
  202. V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
  203. switch (ctx->mode) {
  204. case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
  205. p_out += bytesperline * height - bytes_left;
  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++ +
  211. MEM2MEM_COLOR_STEP;
  212. } else {
  213. for (x = 0; x < tile_w; ++x)
  214. *--p_out = *p_in++ -
  215. MEM2MEM_COLOR_STEP;
  216. }
  217. ++w;
  218. }
  219. p_in += bytes_left;
  220. p_out -= bytes_left;
  221. }
  222. break;
  223. case MEM2MEM_HFLIP:
  224. for (y = 0; y < height; ++y) {
  225. p_out += MEM2MEM_NUM_TILES * tile_w;
  226. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  227. if (w & 0x01) {
  228. for (x = 0; x < tile_w; ++x)
  229. *--p_out = *p_in++ +
  230. MEM2MEM_COLOR_STEP;
  231. } else {
  232. for (x = 0; x < tile_w; ++x)
  233. *--p_out = *p_in++ -
  234. MEM2MEM_COLOR_STEP;
  235. }
  236. ++w;
  237. }
  238. p_in += bytes_left;
  239. p_out += bytesperline;
  240. }
  241. break;
  242. case MEM2MEM_VFLIP:
  243. p_out += bytesperline * (height - 1);
  244. for (y = 0; y < height; ++y) {
  245. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  246. if (w & 0x1) {
  247. for (x = 0; x < tile_w; ++x)
  248. *p_out++ = *p_in++ +
  249. MEM2MEM_COLOR_STEP;
  250. } else {
  251. for (x = 0; x < tile_w; ++x)
  252. *p_out++ = *p_in++ -
  253. MEM2MEM_COLOR_STEP;
  254. }
  255. ++w;
  256. }
  257. p_in += bytes_left;
  258. p_out += bytes_left - 2 * bytesperline;
  259. }
  260. break;
  261. default:
  262. for (y = 0; y < height; ++y) {
  263. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  264. if (w & 0x1) {
  265. for (x = 0; x < tile_w; ++x)
  266. *p_out++ = *p_in++ +
  267. MEM2MEM_COLOR_STEP;
  268. } else {
  269. for (x = 0; x < tile_w; ++x)
  270. *p_out++ = *p_in++ -
  271. MEM2MEM_COLOR_STEP;
  272. }
  273. ++w;
  274. }
  275. p_in += bytes_left;
  276. p_out += bytes_left;
  277. }
  278. }
  279. return 0;
  280. }
  281. static void schedule_irq(struct vim2m_dev *dev, int msec_timeout)
  282. {
  283. dprintk(dev, "Scheduling a simulated irq\n");
  284. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
  285. }
  286. /*
  287. * mem2mem callbacks
  288. */
  289. /**
  290. * job_ready() - check whether an instance is ready to be scheduled to run
  291. */
  292. static int job_ready(void *priv)
  293. {
  294. struct vim2m_ctx *ctx = priv;
  295. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
  296. || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
  297. dprintk(ctx->dev, "Not enough buffers available\n");
  298. return 0;
  299. }
  300. return 1;
  301. }
  302. static void job_abort(void *priv)
  303. {
  304. struct vim2m_ctx *ctx = priv;
  305. /* Will cancel the transaction in the next interrupt handler */
  306. ctx->aborting = 1;
  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 vim2m_ctx *ctx = priv;
  317. struct vim2m_dev *dev = ctx->dev;
  318. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  319. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  320. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.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 vim2m_dev *vim2m_dev = (struct vim2m_dev *)priv;
  328. struct vim2m_ctx *curr_ctx;
  329. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  330. unsigned long flags;
  331. curr_ctx = v4l2_m2m_get_curr_priv(vim2m_dev->m2m_dev);
  332. if (NULL == curr_ctx) {
  333. pr_err("Instance released before the end of transaction\n");
  334. return;
  335. }
  336. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  337. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  338. curr_ctx->num_processed++;
  339. spin_lock_irqsave(&vim2m_dev->irqlock, flags);
  340. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  341. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  342. spin_unlock_irqrestore(&vim2m_dev->irqlock, flags);
  343. if (curr_ctx->num_processed == curr_ctx->translen
  344. || curr_ctx->aborting) {
  345. dprintk(curr_ctx->dev, "Finishing transaction\n");
  346. curr_ctx->num_processed = 0;
  347. v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
  348. } else {
  349. device_run(curr_ctx);
  350. }
  351. }
  352. /*
  353. * video ioctls
  354. */
  355. static int vidioc_querycap(struct file *file, void *priv,
  356. struct v4l2_capability *cap)
  357. {
  358. strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
  359. strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
  360. snprintf(cap->bus_info, sizeof(cap->bus_info),
  361. "platform:%s", MEM2MEM_NAME);
  362. cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  363. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  364. return 0;
  365. }
  366. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  367. {
  368. int i, num;
  369. struct vim2m_fmt *fmt;
  370. num = 0;
  371. for (i = 0; i < NUM_FORMATS; ++i) {
  372. if (formats[i].types & type) {
  373. /* index-th format of type type found ? */
  374. if (num == f->index)
  375. break;
  376. /* Correct type but haven't reached our index yet,
  377. * just increment per-type index */
  378. ++num;
  379. }
  380. }
  381. if (i < NUM_FORMATS) {
  382. /* Format found */
  383. fmt = &formats[i];
  384. f->pixelformat = fmt->fourcc;
  385. return 0;
  386. }
  387. /* Format not found */
  388. return -EINVAL;
  389. }
  390. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  391. struct v4l2_fmtdesc *f)
  392. {
  393. return enum_fmt(f, MEM2MEM_CAPTURE);
  394. }
  395. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  396. struct v4l2_fmtdesc *f)
  397. {
  398. return enum_fmt(f, MEM2MEM_OUTPUT);
  399. }
  400. static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
  401. {
  402. struct vb2_queue *vq;
  403. struct vim2m_q_data *q_data;
  404. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  405. if (!vq)
  406. return -EINVAL;
  407. q_data = get_q_data(ctx, f->type);
  408. f->fmt.pix.width = q_data->width;
  409. f->fmt.pix.height = q_data->height;
  410. f->fmt.pix.field = V4L2_FIELD_NONE;
  411. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  412. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  413. f->fmt.pix.sizeimage = q_data->sizeimage;
  414. f->fmt.pix.colorspace = ctx->colorspace;
  415. return 0;
  416. }
  417. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  418. struct v4l2_format *f)
  419. {
  420. return vidioc_g_fmt(file2ctx(file), f);
  421. }
  422. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  423. struct v4l2_format *f)
  424. {
  425. return vidioc_g_fmt(file2ctx(file), f);
  426. }
  427. static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
  428. {
  429. /* V4L2 specification suggests the driver corrects the format struct
  430. * if any of the dimensions is unsupported */
  431. if (f->fmt.pix.height < MIN_H)
  432. f->fmt.pix.height = MIN_H;
  433. else if (f->fmt.pix.height > MAX_H)
  434. f->fmt.pix.height = MAX_H;
  435. if (f->fmt.pix.width < MIN_W)
  436. f->fmt.pix.width = MIN_W;
  437. else if (f->fmt.pix.width > MAX_W)
  438. f->fmt.pix.width = MAX_W;
  439. f->fmt.pix.width &= ~DIM_ALIGN_MASK;
  440. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  441. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  442. f->fmt.pix.field = V4L2_FIELD_NONE;
  443. return 0;
  444. }
  445. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  446. struct v4l2_format *f)
  447. {
  448. struct vim2m_fmt *fmt;
  449. struct vim2m_ctx *ctx = file2ctx(file);
  450. fmt = find_format(f);
  451. if (!fmt) {
  452. f->fmt.pix.pixelformat = formats[0].fourcc;
  453. fmt = find_format(f);
  454. }
  455. if (!(fmt->types & MEM2MEM_CAPTURE)) {
  456. v4l2_err(&ctx->dev->v4l2_dev,
  457. "Fourcc format (0x%08x) invalid.\n",
  458. f->fmt.pix.pixelformat);
  459. return -EINVAL;
  460. }
  461. f->fmt.pix.colorspace = ctx->colorspace;
  462. return vidioc_try_fmt(f, fmt);
  463. }
  464. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  465. struct v4l2_format *f)
  466. {
  467. struct vim2m_fmt *fmt;
  468. struct vim2m_ctx *ctx = file2ctx(file);
  469. fmt = find_format(f);
  470. if (!fmt) {
  471. f->fmt.pix.pixelformat = formats[0].fourcc;
  472. fmt = find_format(f);
  473. }
  474. if (!(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 vim2m_ctx *ctx, struct v4l2_format *f)
  485. {
  486. struct vim2m_q_data *q_data;
  487. struct vb2_queue *vq;
  488. vq = v4l2_m2m_get_vq(ctx->fh.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 vim2m_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 vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
  531. {
  532. struct vim2m_ctx *ctx =
  533. container_of(ctrl->handler, struct vim2m_ctx, hdl);
  534. switch (ctrl->id) {
  535. case V4L2_CID_HFLIP:
  536. if (ctrl->val)
  537. ctx->mode |= MEM2MEM_HFLIP;
  538. else
  539. ctx->mode &= ~MEM2MEM_HFLIP;
  540. break;
  541. case V4L2_CID_VFLIP:
  542. if (ctrl->val)
  543. ctx->mode |= MEM2MEM_VFLIP;
  544. else
  545. ctx->mode &= ~MEM2MEM_VFLIP;
  546. break;
  547. case V4L2_CID_TRANS_TIME_MSEC:
  548. ctx->transtime = ctrl->val;
  549. break;
  550. case V4L2_CID_TRANS_NUM_BUFS:
  551. ctx->translen = ctrl->val;
  552. break;
  553. default:
  554. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  555. return -EINVAL;
  556. }
  557. return 0;
  558. }
  559. static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
  560. .s_ctrl = vim2m_s_ctrl,
  561. };
  562. static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
  563. .vidioc_querycap = vidioc_querycap,
  564. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  565. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  566. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  567. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  568. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  569. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  570. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  571. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  572. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  573. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  574. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  575. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  576. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  577. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  578. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  579. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  580. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  581. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  582. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  583. };
  584. /*
  585. * Queue operations
  586. */
  587. static int vim2m_queue_setup(struct vb2_queue *vq,
  588. const void *parg,
  589. unsigned int *nbuffers, unsigned int *nplanes,
  590. unsigned int sizes[], void *alloc_ctxs[])
  591. {
  592. const struct v4l2_format *fmt = parg;
  593. struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
  594. struct vim2m_q_data *q_data;
  595. unsigned int size, count = *nbuffers;
  596. q_data = get_q_data(ctx, vq->type);
  597. size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  598. if (fmt) {
  599. if (fmt->fmt.pix.sizeimage < size)
  600. return -EINVAL;
  601. size = fmt->fmt.pix.sizeimage;
  602. }
  603. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  604. (count)--;
  605. *nplanes = 1;
  606. *nbuffers = count;
  607. sizes[0] = size;
  608. /*
  609. * videobuf2-vmalloc allocator is context-less so no need to set
  610. * alloc_ctxs array.
  611. */
  612. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  613. return 0;
  614. }
  615. static int vim2m_buf_prepare(struct vb2_buffer *vb)
  616. {
  617. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  618. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  619. struct vim2m_q_data *q_data;
  620. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  621. q_data = get_q_data(ctx, vb->vb2_queue->type);
  622. if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  623. if (vbuf->field == V4L2_FIELD_ANY)
  624. vbuf->field = V4L2_FIELD_NONE;
  625. if (vbuf->field != V4L2_FIELD_NONE) {
  626. dprintk(ctx->dev, "%s field isn't supported\n",
  627. __func__);
  628. return -EINVAL;
  629. }
  630. }
  631. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  632. dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
  633. __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
  634. return -EINVAL;
  635. }
  636. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  637. return 0;
  638. }
  639. static void vim2m_buf_queue(struct vb2_buffer *vb)
  640. {
  641. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  642. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  643. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  644. }
  645. static int vim2m_start_streaming(struct vb2_queue *q, unsigned count)
  646. {
  647. struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
  648. struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
  649. q_data->sequence = 0;
  650. return 0;
  651. }
  652. static void vim2m_stop_streaming(struct vb2_queue *q)
  653. {
  654. struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
  655. struct vb2_v4l2_buffer *vbuf;
  656. unsigned long flags;
  657. for (;;) {
  658. if (V4L2_TYPE_IS_OUTPUT(q->type))
  659. vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  660. else
  661. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  662. if (vbuf == NULL)
  663. return;
  664. spin_lock_irqsave(&ctx->dev->irqlock, flags);
  665. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  666. spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
  667. }
  668. }
  669. static struct vb2_ops vim2m_qops = {
  670. .queue_setup = vim2m_queue_setup,
  671. .buf_prepare = vim2m_buf_prepare,
  672. .buf_queue = vim2m_buf_queue,
  673. .start_streaming = vim2m_start_streaming,
  674. .stop_streaming = vim2m_stop_streaming,
  675. .wait_prepare = vb2_ops_wait_prepare,
  676. .wait_finish = vb2_ops_wait_finish,
  677. };
  678. static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
  679. {
  680. struct vim2m_ctx *ctx = priv;
  681. int ret;
  682. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  683. src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  684. src_vq->drv_priv = ctx;
  685. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  686. src_vq->ops = &vim2m_qops;
  687. src_vq->mem_ops = &vb2_vmalloc_memops;
  688. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  689. src_vq->lock = &ctx->dev->dev_mutex;
  690. ret = vb2_queue_init(src_vq);
  691. if (ret)
  692. return ret;
  693. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  694. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  695. dst_vq->drv_priv = ctx;
  696. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  697. dst_vq->ops = &vim2m_qops;
  698. dst_vq->mem_ops = &vb2_vmalloc_memops;
  699. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  700. dst_vq->lock = &ctx->dev->dev_mutex;
  701. return vb2_queue_init(dst_vq);
  702. }
  703. static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
  704. .ops = &vim2m_ctrl_ops,
  705. .id = V4L2_CID_TRANS_TIME_MSEC,
  706. .name = "Transaction Time (msec)",
  707. .type = V4L2_CTRL_TYPE_INTEGER,
  708. .def = MEM2MEM_DEF_TRANSTIME,
  709. .min = 1,
  710. .max = 10001,
  711. .step = 1,
  712. };
  713. static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
  714. .ops = &vim2m_ctrl_ops,
  715. .id = V4L2_CID_TRANS_NUM_BUFS,
  716. .name = "Buffers Per Transaction",
  717. .type = V4L2_CTRL_TYPE_INTEGER,
  718. .def = 1,
  719. .min = 1,
  720. .max = MEM2MEM_DEF_NUM_BUFS,
  721. .step = 1,
  722. };
  723. /*
  724. * File operations
  725. */
  726. static int vim2m_open(struct file *file)
  727. {
  728. struct vim2m_dev *dev = video_drvdata(file);
  729. struct vim2m_ctx *ctx = NULL;
  730. struct v4l2_ctrl_handler *hdl;
  731. int rc = 0;
  732. if (mutex_lock_interruptible(&dev->dev_mutex))
  733. return -ERESTARTSYS;
  734. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  735. if (!ctx) {
  736. rc = -ENOMEM;
  737. goto open_unlock;
  738. }
  739. v4l2_fh_init(&ctx->fh, video_devdata(file));
  740. file->private_data = &ctx->fh;
  741. ctx->dev = dev;
  742. hdl = &ctx->hdl;
  743. v4l2_ctrl_handler_init(hdl, 4);
  744. v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  745. v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  746. v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
  747. v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
  748. if (hdl->error) {
  749. rc = hdl->error;
  750. v4l2_ctrl_handler_free(hdl);
  751. goto open_unlock;
  752. }
  753. ctx->fh.ctrl_handler = hdl;
  754. v4l2_ctrl_handler_setup(hdl);
  755. ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
  756. ctx->q_data[V4L2_M2M_SRC].width = 640;
  757. ctx->q_data[V4L2_M2M_SRC].height = 480;
  758. ctx->q_data[V4L2_M2M_SRC].sizeimage =
  759. ctx->q_data[V4L2_M2M_SRC].width *
  760. ctx->q_data[V4L2_M2M_SRC].height *
  761. (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
  762. ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
  763. ctx->colorspace = V4L2_COLORSPACE_REC709;
  764. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  765. if (IS_ERR(ctx->fh.m2m_ctx)) {
  766. rc = PTR_ERR(ctx->fh.m2m_ctx);
  767. v4l2_ctrl_handler_free(hdl);
  768. kfree(ctx);
  769. goto open_unlock;
  770. }
  771. v4l2_fh_add(&ctx->fh);
  772. atomic_inc(&dev->num_inst);
  773. dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
  774. ctx, ctx->fh.m2m_ctx);
  775. open_unlock:
  776. mutex_unlock(&dev->dev_mutex);
  777. return rc;
  778. }
  779. static int vim2m_release(struct file *file)
  780. {
  781. struct vim2m_dev *dev = video_drvdata(file);
  782. struct vim2m_ctx *ctx = file2ctx(file);
  783. dprintk(dev, "Releasing instance %p\n", ctx);
  784. v4l2_fh_del(&ctx->fh);
  785. v4l2_fh_exit(&ctx->fh);
  786. v4l2_ctrl_handler_free(&ctx->hdl);
  787. mutex_lock(&dev->dev_mutex);
  788. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  789. mutex_unlock(&dev->dev_mutex);
  790. kfree(ctx);
  791. atomic_dec(&dev->num_inst);
  792. return 0;
  793. }
  794. static const struct v4l2_file_operations vim2m_fops = {
  795. .owner = THIS_MODULE,
  796. .open = vim2m_open,
  797. .release = vim2m_release,
  798. .poll = v4l2_m2m_fop_poll,
  799. .unlocked_ioctl = video_ioctl2,
  800. .mmap = v4l2_m2m_fop_mmap,
  801. };
  802. static struct video_device vim2m_videodev = {
  803. .name = MEM2MEM_NAME,
  804. .vfl_dir = VFL_DIR_M2M,
  805. .fops = &vim2m_fops,
  806. .ioctl_ops = &vim2m_ioctl_ops,
  807. .minor = -1,
  808. .release = video_device_release_empty,
  809. };
  810. static struct v4l2_m2m_ops m2m_ops = {
  811. .device_run = device_run,
  812. .job_ready = job_ready,
  813. .job_abort = job_abort,
  814. };
  815. static int vim2m_probe(struct platform_device *pdev)
  816. {
  817. struct vim2m_dev *dev;
  818. struct video_device *vfd;
  819. int ret;
  820. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  821. if (!dev)
  822. return -ENOMEM;
  823. spin_lock_init(&dev->irqlock);
  824. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  825. if (ret)
  826. return ret;
  827. atomic_set(&dev->num_inst, 0);
  828. mutex_init(&dev->dev_mutex);
  829. dev->vfd = vim2m_videodev;
  830. vfd = &dev->vfd;
  831. vfd->lock = &dev->dev_mutex;
  832. vfd->v4l2_dev = &dev->v4l2_dev;
  833. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  834. if (ret) {
  835. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  836. goto unreg_dev;
  837. }
  838. video_set_drvdata(vfd, dev);
  839. snprintf(vfd->name, sizeof(vfd->name), "%s", vim2m_videodev.name);
  840. v4l2_info(&dev->v4l2_dev,
  841. "Device registered as /dev/video%d\n", vfd->num);
  842. setup_timer(&dev->timer, device_isr, (long)dev);
  843. platform_set_drvdata(pdev, dev);
  844. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  845. if (IS_ERR(dev->m2m_dev)) {
  846. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  847. ret = PTR_ERR(dev->m2m_dev);
  848. goto err_m2m;
  849. }
  850. return 0;
  851. err_m2m:
  852. v4l2_m2m_release(dev->m2m_dev);
  853. video_unregister_device(&dev->vfd);
  854. unreg_dev:
  855. v4l2_device_unregister(&dev->v4l2_dev);
  856. return ret;
  857. }
  858. static int vim2m_remove(struct platform_device *pdev)
  859. {
  860. struct vim2m_dev *dev = platform_get_drvdata(pdev);
  861. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
  862. v4l2_m2m_release(dev->m2m_dev);
  863. del_timer_sync(&dev->timer);
  864. video_unregister_device(&dev->vfd);
  865. v4l2_device_unregister(&dev->v4l2_dev);
  866. return 0;
  867. }
  868. static struct platform_driver vim2m_pdrv = {
  869. .probe = vim2m_probe,
  870. .remove = vim2m_remove,
  871. .driver = {
  872. .name = MEM2MEM_NAME,
  873. },
  874. };
  875. static void __exit vim2m_exit(void)
  876. {
  877. platform_driver_unregister(&vim2m_pdrv);
  878. platform_device_unregister(&vim2m_pdev);
  879. }
  880. static int __init vim2m_init(void)
  881. {
  882. int ret;
  883. ret = platform_device_register(&vim2m_pdev);
  884. if (ret)
  885. return ret;
  886. ret = platform_driver_register(&vim2m_pdrv);
  887. if (ret)
  888. platform_device_unregister(&vim2m_pdev);
  889. return 0;
  890. }
  891. module_init(vim2m_init);
  892. module_exit(vim2m_exit);