vim2m.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. enum v4l2_ycbcr_encoding ycbcr_enc;
  141. enum v4l2_xfer_func xfer_func;
  142. enum v4l2_quantization quant;
  143. /* Source and destination queue data */
  144. struct vim2m_q_data q_data[2];
  145. };
  146. static inline struct vim2m_ctx *file2ctx(struct file *file)
  147. {
  148. return container_of(file->private_data, struct vim2m_ctx, fh);
  149. }
  150. static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
  151. enum v4l2_buf_type type)
  152. {
  153. switch (type) {
  154. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  155. return &ctx->q_data[V4L2_M2M_SRC];
  156. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  157. return &ctx->q_data[V4L2_M2M_DST];
  158. default:
  159. BUG();
  160. }
  161. return NULL;
  162. }
  163. static int device_process(struct vim2m_ctx *ctx,
  164. struct vb2_v4l2_buffer *in_vb,
  165. struct vb2_v4l2_buffer *out_vb)
  166. {
  167. struct vim2m_dev *dev = ctx->dev;
  168. struct vim2m_q_data *q_data;
  169. u8 *p_in, *p_out;
  170. int x, y, t, w;
  171. int tile_w, bytes_left;
  172. int width, height, bytesperline;
  173. q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  174. width = q_data->width;
  175. height = q_data->height;
  176. bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  177. p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
  178. p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
  179. if (!p_in || !p_out) {
  180. v4l2_err(&dev->v4l2_dev,
  181. "Acquiring kernel pointers to buffers failed\n");
  182. return -EFAULT;
  183. }
  184. if (vb2_plane_size(&in_vb->vb2_buf, 0) >
  185. vb2_plane_size(&out_vb->vb2_buf, 0)) {
  186. v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
  187. return -EINVAL;
  188. }
  189. tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
  190. / MEM2MEM_NUM_TILES;
  191. bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
  192. w = 0;
  193. out_vb->sequence =
  194. get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
  195. in_vb->sequence = q_data->sequence++;
  196. out_vb->vb2_buf.timestamp = in_vb->vb2_buf.timestamp;
  197. if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE)
  198. out_vb->timecode = in_vb->timecode;
  199. out_vb->field = in_vb->field;
  200. out_vb->flags = in_vb->flags &
  201. (V4L2_BUF_FLAG_TIMECODE |
  202. V4L2_BUF_FLAG_KEYFRAME |
  203. V4L2_BUF_FLAG_PFRAME |
  204. V4L2_BUF_FLAG_BFRAME |
  205. V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
  206. switch (ctx->mode) {
  207. case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
  208. p_out += bytesperline * height - bytes_left;
  209. for (y = 0; y < height; ++y) {
  210. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  211. if (w & 0x1) {
  212. for (x = 0; x < tile_w; ++x)
  213. *--p_out = *p_in++ +
  214. MEM2MEM_COLOR_STEP;
  215. } else {
  216. for (x = 0; x < tile_w; ++x)
  217. *--p_out = *p_in++ -
  218. MEM2MEM_COLOR_STEP;
  219. }
  220. ++w;
  221. }
  222. p_in += bytes_left;
  223. p_out -= bytes_left;
  224. }
  225. break;
  226. case MEM2MEM_HFLIP:
  227. for (y = 0; y < height; ++y) {
  228. p_out += MEM2MEM_NUM_TILES * tile_w;
  229. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  230. if (w & 0x01) {
  231. for (x = 0; x < tile_w; ++x)
  232. *--p_out = *p_in++ +
  233. MEM2MEM_COLOR_STEP;
  234. } else {
  235. for (x = 0; x < tile_w; ++x)
  236. *--p_out = *p_in++ -
  237. MEM2MEM_COLOR_STEP;
  238. }
  239. ++w;
  240. }
  241. p_in += bytes_left;
  242. p_out += bytesperline;
  243. }
  244. break;
  245. case MEM2MEM_VFLIP:
  246. p_out += bytesperline * (height - 1);
  247. for (y = 0; y < height; ++y) {
  248. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  249. if (w & 0x1) {
  250. for (x = 0; x < tile_w; ++x)
  251. *p_out++ = *p_in++ +
  252. MEM2MEM_COLOR_STEP;
  253. } else {
  254. for (x = 0; x < tile_w; ++x)
  255. *p_out++ = *p_in++ -
  256. MEM2MEM_COLOR_STEP;
  257. }
  258. ++w;
  259. }
  260. p_in += bytes_left;
  261. p_out += bytes_left - 2 * bytesperline;
  262. }
  263. break;
  264. default:
  265. for (y = 0; y < height; ++y) {
  266. for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
  267. if (w & 0x1) {
  268. for (x = 0; x < tile_w; ++x)
  269. *p_out++ = *p_in++ +
  270. MEM2MEM_COLOR_STEP;
  271. } else {
  272. for (x = 0; x < tile_w; ++x)
  273. *p_out++ = *p_in++ -
  274. MEM2MEM_COLOR_STEP;
  275. }
  276. ++w;
  277. }
  278. p_in += bytes_left;
  279. p_out += bytes_left;
  280. }
  281. }
  282. return 0;
  283. }
  284. static void schedule_irq(struct vim2m_dev *dev, int msec_timeout)
  285. {
  286. dprintk(dev, "Scheduling a simulated irq\n");
  287. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
  288. }
  289. /*
  290. * mem2mem callbacks
  291. */
  292. /**
  293. * job_ready() - check whether an instance is ready to be scheduled to run
  294. */
  295. static int job_ready(void *priv)
  296. {
  297. struct vim2m_ctx *ctx = priv;
  298. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
  299. || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
  300. dprintk(ctx->dev, "Not enough buffers available\n");
  301. return 0;
  302. }
  303. return 1;
  304. }
  305. static void job_abort(void *priv)
  306. {
  307. struct vim2m_ctx *ctx = priv;
  308. /* Will cancel the transaction in the next interrupt handler */
  309. ctx->aborting = 1;
  310. }
  311. /* device_run() - prepares and starts the device
  312. *
  313. * This simulates all the immediate preparations required before starting
  314. * a device. This will be called by the framework when it decides to schedule
  315. * a particular instance.
  316. */
  317. static void device_run(void *priv)
  318. {
  319. struct vim2m_ctx *ctx = priv;
  320. struct vim2m_dev *dev = ctx->dev;
  321. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  322. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  323. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  324. device_process(ctx, src_buf, dst_buf);
  325. /* Run a timer, which simulates a hardware irq */
  326. schedule_irq(dev, ctx->transtime);
  327. }
  328. static void device_isr(unsigned long priv)
  329. {
  330. struct vim2m_dev *vim2m_dev = (struct vim2m_dev *)priv;
  331. struct vim2m_ctx *curr_ctx;
  332. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  333. unsigned long flags;
  334. curr_ctx = v4l2_m2m_get_curr_priv(vim2m_dev->m2m_dev);
  335. if (NULL == curr_ctx) {
  336. pr_err("Instance released before the end of transaction\n");
  337. return;
  338. }
  339. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  340. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  341. curr_ctx->num_processed++;
  342. spin_lock_irqsave(&vim2m_dev->irqlock, flags);
  343. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  344. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  345. spin_unlock_irqrestore(&vim2m_dev->irqlock, flags);
  346. if (curr_ctx->num_processed == curr_ctx->translen
  347. || curr_ctx->aborting) {
  348. dprintk(curr_ctx->dev, "Finishing transaction\n");
  349. curr_ctx->num_processed = 0;
  350. v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
  351. } else {
  352. device_run(curr_ctx);
  353. }
  354. }
  355. /*
  356. * video ioctls
  357. */
  358. static int vidioc_querycap(struct file *file, void *priv,
  359. struct v4l2_capability *cap)
  360. {
  361. strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
  362. strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
  363. snprintf(cap->bus_info, sizeof(cap->bus_info),
  364. "platform:%s", MEM2MEM_NAME);
  365. cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  366. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  367. return 0;
  368. }
  369. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  370. {
  371. int i, num;
  372. struct vim2m_fmt *fmt;
  373. num = 0;
  374. for (i = 0; i < NUM_FORMATS; ++i) {
  375. if (formats[i].types & type) {
  376. /* index-th format of type type found ? */
  377. if (num == f->index)
  378. break;
  379. /* Correct type but haven't reached our index yet,
  380. * just increment per-type index */
  381. ++num;
  382. }
  383. }
  384. if (i < NUM_FORMATS) {
  385. /* Format found */
  386. fmt = &formats[i];
  387. f->pixelformat = fmt->fourcc;
  388. return 0;
  389. }
  390. /* Format not found */
  391. return -EINVAL;
  392. }
  393. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  394. struct v4l2_fmtdesc *f)
  395. {
  396. return enum_fmt(f, MEM2MEM_CAPTURE);
  397. }
  398. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  399. struct v4l2_fmtdesc *f)
  400. {
  401. return enum_fmt(f, MEM2MEM_OUTPUT);
  402. }
  403. static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
  404. {
  405. struct vb2_queue *vq;
  406. struct vim2m_q_data *q_data;
  407. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  408. if (!vq)
  409. return -EINVAL;
  410. q_data = get_q_data(ctx, f->type);
  411. f->fmt.pix.width = q_data->width;
  412. f->fmt.pix.height = q_data->height;
  413. f->fmt.pix.field = V4L2_FIELD_NONE;
  414. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  415. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  416. f->fmt.pix.sizeimage = q_data->sizeimage;
  417. f->fmt.pix.colorspace = ctx->colorspace;
  418. f->fmt.pix.xfer_func = ctx->xfer_func;
  419. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  420. f->fmt.pix.quantization = ctx->quant;
  421. return 0;
  422. }
  423. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  424. struct v4l2_format *f)
  425. {
  426. return vidioc_g_fmt(file2ctx(file), f);
  427. }
  428. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  429. struct v4l2_format *f)
  430. {
  431. return vidioc_g_fmt(file2ctx(file), f);
  432. }
  433. static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
  434. {
  435. /* V4L2 specification suggests the driver corrects the format struct
  436. * if any of the dimensions is unsupported */
  437. if (f->fmt.pix.height < MIN_H)
  438. f->fmt.pix.height = MIN_H;
  439. else if (f->fmt.pix.height > MAX_H)
  440. f->fmt.pix.height = MAX_H;
  441. if (f->fmt.pix.width < MIN_W)
  442. f->fmt.pix.width = MIN_W;
  443. else if (f->fmt.pix.width > MAX_W)
  444. f->fmt.pix.width = MAX_W;
  445. f->fmt.pix.width &= ~DIM_ALIGN_MASK;
  446. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  447. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  448. f->fmt.pix.field = V4L2_FIELD_NONE;
  449. return 0;
  450. }
  451. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  452. struct v4l2_format *f)
  453. {
  454. struct vim2m_fmt *fmt;
  455. struct vim2m_ctx *ctx = file2ctx(file);
  456. fmt = find_format(f);
  457. if (!fmt) {
  458. f->fmt.pix.pixelformat = formats[0].fourcc;
  459. fmt = find_format(f);
  460. }
  461. if (!(fmt->types & MEM2MEM_CAPTURE)) {
  462. v4l2_err(&ctx->dev->v4l2_dev,
  463. "Fourcc format (0x%08x) invalid.\n",
  464. f->fmt.pix.pixelformat);
  465. return -EINVAL;
  466. }
  467. f->fmt.pix.colorspace = ctx->colorspace;
  468. f->fmt.pix.xfer_func = ctx->xfer_func;
  469. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  470. f->fmt.pix.quantization = ctx->quant;
  471. return vidioc_try_fmt(f, fmt);
  472. }
  473. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  474. struct v4l2_format *f)
  475. {
  476. struct vim2m_fmt *fmt;
  477. struct vim2m_ctx *ctx = file2ctx(file);
  478. fmt = find_format(f);
  479. if (!fmt) {
  480. f->fmt.pix.pixelformat = formats[0].fourcc;
  481. fmt = find_format(f);
  482. }
  483. if (!(fmt->types & MEM2MEM_OUTPUT)) {
  484. v4l2_err(&ctx->dev->v4l2_dev,
  485. "Fourcc format (0x%08x) invalid.\n",
  486. f->fmt.pix.pixelformat);
  487. return -EINVAL;
  488. }
  489. if (!f->fmt.pix.colorspace)
  490. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  491. return vidioc_try_fmt(f, fmt);
  492. }
  493. static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
  494. {
  495. struct vim2m_q_data *q_data;
  496. struct vb2_queue *vq;
  497. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  498. if (!vq)
  499. return -EINVAL;
  500. q_data = get_q_data(ctx, f->type);
  501. if (!q_data)
  502. return -EINVAL;
  503. if (vb2_is_busy(vq)) {
  504. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  505. return -EBUSY;
  506. }
  507. q_data->fmt = find_format(f);
  508. q_data->width = f->fmt.pix.width;
  509. q_data->height = f->fmt.pix.height;
  510. q_data->sizeimage = q_data->width * q_data->height
  511. * q_data->fmt->depth >> 3;
  512. dprintk(ctx->dev,
  513. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  514. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  515. return 0;
  516. }
  517. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  518. struct v4l2_format *f)
  519. {
  520. int ret;
  521. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  522. if (ret)
  523. return ret;
  524. return vidioc_s_fmt(file2ctx(file), f);
  525. }
  526. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  527. struct v4l2_format *f)
  528. {
  529. struct vim2m_ctx *ctx = file2ctx(file);
  530. int ret;
  531. ret = vidioc_try_fmt_vid_out(file, priv, f);
  532. if (ret)
  533. return ret;
  534. ret = vidioc_s_fmt(file2ctx(file), f);
  535. if (!ret) {
  536. ctx->colorspace = f->fmt.pix.colorspace;
  537. ctx->xfer_func = f->fmt.pix.xfer_func;
  538. ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
  539. ctx->quant = f->fmt.pix.quantization;
  540. }
  541. return ret;
  542. }
  543. static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
  544. {
  545. struct vim2m_ctx *ctx =
  546. container_of(ctrl->handler, struct vim2m_ctx, hdl);
  547. switch (ctrl->id) {
  548. case V4L2_CID_HFLIP:
  549. if (ctrl->val)
  550. ctx->mode |= MEM2MEM_HFLIP;
  551. else
  552. ctx->mode &= ~MEM2MEM_HFLIP;
  553. break;
  554. case V4L2_CID_VFLIP:
  555. if (ctrl->val)
  556. ctx->mode |= MEM2MEM_VFLIP;
  557. else
  558. ctx->mode &= ~MEM2MEM_VFLIP;
  559. break;
  560. case V4L2_CID_TRANS_TIME_MSEC:
  561. ctx->transtime = ctrl->val;
  562. break;
  563. case V4L2_CID_TRANS_NUM_BUFS:
  564. ctx->translen = ctrl->val;
  565. break;
  566. default:
  567. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  568. return -EINVAL;
  569. }
  570. return 0;
  571. }
  572. static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
  573. .s_ctrl = vim2m_s_ctrl,
  574. };
  575. static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
  576. .vidioc_querycap = vidioc_querycap,
  577. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  578. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  579. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  580. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  581. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  582. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  583. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  584. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  585. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  586. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  587. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  588. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  589. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  590. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  591. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  592. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  593. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  594. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  595. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  596. };
  597. /*
  598. * Queue operations
  599. */
  600. static int vim2m_queue_setup(struct vb2_queue *vq,
  601. unsigned int *nbuffers, unsigned int *nplanes,
  602. unsigned int sizes[], struct device *alloc_devs[])
  603. {
  604. struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
  605. struct vim2m_q_data *q_data;
  606. unsigned int size, count = *nbuffers;
  607. q_data = get_q_data(ctx, vq->type);
  608. size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  609. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  610. (count)--;
  611. *nbuffers = count;
  612. if (*nplanes)
  613. return sizes[0] < size ? -EINVAL : 0;
  614. *nplanes = 1;
  615. sizes[0] = size;
  616. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  617. return 0;
  618. }
  619. static int vim2m_buf_prepare(struct vb2_buffer *vb)
  620. {
  621. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  622. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  623. struct vim2m_q_data *q_data;
  624. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  625. q_data = get_q_data(ctx, vb->vb2_queue->type);
  626. if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  627. if (vbuf->field == V4L2_FIELD_ANY)
  628. vbuf->field = V4L2_FIELD_NONE;
  629. if (vbuf->field != V4L2_FIELD_NONE) {
  630. dprintk(ctx->dev, "%s field isn't supported\n",
  631. __func__);
  632. return -EINVAL;
  633. }
  634. }
  635. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  636. dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
  637. __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
  638. return -EINVAL;
  639. }
  640. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  641. return 0;
  642. }
  643. static void vim2m_buf_queue(struct vb2_buffer *vb)
  644. {
  645. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  646. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  647. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  648. }
  649. static int vim2m_start_streaming(struct vb2_queue *q, unsigned count)
  650. {
  651. struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
  652. struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
  653. q_data->sequence = 0;
  654. return 0;
  655. }
  656. static void vim2m_stop_streaming(struct vb2_queue *q)
  657. {
  658. struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
  659. struct vb2_v4l2_buffer *vbuf;
  660. unsigned long flags;
  661. for (;;) {
  662. if (V4L2_TYPE_IS_OUTPUT(q->type))
  663. vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  664. else
  665. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  666. if (vbuf == NULL)
  667. return;
  668. spin_lock_irqsave(&ctx->dev->irqlock, flags);
  669. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  670. spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
  671. }
  672. }
  673. static const struct vb2_ops vim2m_qops = {
  674. .queue_setup = vim2m_queue_setup,
  675. .buf_prepare = vim2m_buf_prepare,
  676. .buf_queue = vim2m_buf_queue,
  677. .start_streaming = vim2m_start_streaming,
  678. .stop_streaming = vim2m_stop_streaming,
  679. .wait_prepare = vb2_ops_wait_prepare,
  680. .wait_finish = vb2_ops_wait_finish,
  681. };
  682. static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
  683. {
  684. struct vim2m_ctx *ctx = priv;
  685. int ret;
  686. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  687. src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  688. src_vq->drv_priv = ctx;
  689. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  690. src_vq->ops = &vim2m_qops;
  691. src_vq->mem_ops = &vb2_vmalloc_memops;
  692. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  693. src_vq->lock = &ctx->dev->dev_mutex;
  694. ret = vb2_queue_init(src_vq);
  695. if (ret)
  696. return ret;
  697. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  698. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  699. dst_vq->drv_priv = ctx;
  700. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  701. dst_vq->ops = &vim2m_qops;
  702. dst_vq->mem_ops = &vb2_vmalloc_memops;
  703. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  704. dst_vq->lock = &ctx->dev->dev_mutex;
  705. return vb2_queue_init(dst_vq);
  706. }
  707. static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
  708. .ops = &vim2m_ctrl_ops,
  709. .id = V4L2_CID_TRANS_TIME_MSEC,
  710. .name = "Transaction Time (msec)",
  711. .type = V4L2_CTRL_TYPE_INTEGER,
  712. .def = MEM2MEM_DEF_TRANSTIME,
  713. .min = 1,
  714. .max = 10001,
  715. .step = 1,
  716. };
  717. static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
  718. .ops = &vim2m_ctrl_ops,
  719. .id = V4L2_CID_TRANS_NUM_BUFS,
  720. .name = "Buffers Per Transaction",
  721. .type = V4L2_CTRL_TYPE_INTEGER,
  722. .def = 1,
  723. .min = 1,
  724. .max = MEM2MEM_DEF_NUM_BUFS,
  725. .step = 1,
  726. };
  727. /*
  728. * File operations
  729. */
  730. static int vim2m_open(struct file *file)
  731. {
  732. struct vim2m_dev *dev = video_drvdata(file);
  733. struct vim2m_ctx *ctx = NULL;
  734. struct v4l2_ctrl_handler *hdl;
  735. int rc = 0;
  736. if (mutex_lock_interruptible(&dev->dev_mutex))
  737. return -ERESTARTSYS;
  738. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  739. if (!ctx) {
  740. rc = -ENOMEM;
  741. goto open_unlock;
  742. }
  743. v4l2_fh_init(&ctx->fh, video_devdata(file));
  744. file->private_data = &ctx->fh;
  745. ctx->dev = dev;
  746. hdl = &ctx->hdl;
  747. v4l2_ctrl_handler_init(hdl, 4);
  748. v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  749. v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  750. v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
  751. v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
  752. if (hdl->error) {
  753. rc = hdl->error;
  754. v4l2_ctrl_handler_free(hdl);
  755. kfree(ctx);
  756. goto open_unlock;
  757. }
  758. ctx->fh.ctrl_handler = hdl;
  759. v4l2_ctrl_handler_setup(hdl);
  760. ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
  761. ctx->q_data[V4L2_M2M_SRC].width = 640;
  762. ctx->q_data[V4L2_M2M_SRC].height = 480;
  763. ctx->q_data[V4L2_M2M_SRC].sizeimage =
  764. ctx->q_data[V4L2_M2M_SRC].width *
  765. ctx->q_data[V4L2_M2M_SRC].height *
  766. (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
  767. ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
  768. ctx->colorspace = V4L2_COLORSPACE_REC709;
  769. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  770. if (IS_ERR(ctx->fh.m2m_ctx)) {
  771. rc = PTR_ERR(ctx->fh.m2m_ctx);
  772. v4l2_ctrl_handler_free(hdl);
  773. v4l2_fh_exit(&ctx->fh);
  774. kfree(ctx);
  775. goto open_unlock;
  776. }
  777. v4l2_fh_add(&ctx->fh);
  778. atomic_inc(&dev->num_inst);
  779. dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
  780. ctx, ctx->fh.m2m_ctx);
  781. open_unlock:
  782. mutex_unlock(&dev->dev_mutex);
  783. return rc;
  784. }
  785. static int vim2m_release(struct file *file)
  786. {
  787. struct vim2m_dev *dev = video_drvdata(file);
  788. struct vim2m_ctx *ctx = file2ctx(file);
  789. dprintk(dev, "Releasing instance %p\n", ctx);
  790. v4l2_fh_del(&ctx->fh);
  791. v4l2_fh_exit(&ctx->fh);
  792. v4l2_ctrl_handler_free(&ctx->hdl);
  793. mutex_lock(&dev->dev_mutex);
  794. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  795. mutex_unlock(&dev->dev_mutex);
  796. kfree(ctx);
  797. atomic_dec(&dev->num_inst);
  798. return 0;
  799. }
  800. static const struct v4l2_file_operations vim2m_fops = {
  801. .owner = THIS_MODULE,
  802. .open = vim2m_open,
  803. .release = vim2m_release,
  804. .poll = v4l2_m2m_fop_poll,
  805. .unlocked_ioctl = video_ioctl2,
  806. .mmap = v4l2_m2m_fop_mmap,
  807. };
  808. static const struct video_device vim2m_videodev = {
  809. .name = MEM2MEM_NAME,
  810. .vfl_dir = VFL_DIR_M2M,
  811. .fops = &vim2m_fops,
  812. .ioctl_ops = &vim2m_ioctl_ops,
  813. .minor = -1,
  814. .release = video_device_release_empty,
  815. };
  816. static const struct v4l2_m2m_ops m2m_ops = {
  817. .device_run = device_run,
  818. .job_ready = job_ready,
  819. .job_abort = job_abort,
  820. };
  821. static int vim2m_probe(struct platform_device *pdev)
  822. {
  823. struct vim2m_dev *dev;
  824. struct video_device *vfd;
  825. int ret;
  826. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  827. if (!dev)
  828. return -ENOMEM;
  829. spin_lock_init(&dev->irqlock);
  830. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  831. if (ret)
  832. return ret;
  833. atomic_set(&dev->num_inst, 0);
  834. mutex_init(&dev->dev_mutex);
  835. dev->vfd = vim2m_videodev;
  836. vfd = &dev->vfd;
  837. vfd->lock = &dev->dev_mutex;
  838. vfd->v4l2_dev = &dev->v4l2_dev;
  839. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  840. if (ret) {
  841. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  842. goto unreg_dev;
  843. }
  844. video_set_drvdata(vfd, dev);
  845. snprintf(vfd->name, sizeof(vfd->name), "%s", vim2m_videodev.name);
  846. v4l2_info(&dev->v4l2_dev,
  847. "Device registered as /dev/video%d\n", vfd->num);
  848. setup_timer(&dev->timer, device_isr, (long)dev);
  849. platform_set_drvdata(pdev, dev);
  850. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  851. if (IS_ERR(dev->m2m_dev)) {
  852. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  853. ret = PTR_ERR(dev->m2m_dev);
  854. goto err_m2m;
  855. }
  856. return 0;
  857. err_m2m:
  858. v4l2_m2m_release(dev->m2m_dev);
  859. video_unregister_device(&dev->vfd);
  860. unreg_dev:
  861. v4l2_device_unregister(&dev->v4l2_dev);
  862. return ret;
  863. }
  864. static int vim2m_remove(struct platform_device *pdev)
  865. {
  866. struct vim2m_dev *dev = platform_get_drvdata(pdev);
  867. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
  868. v4l2_m2m_release(dev->m2m_dev);
  869. del_timer_sync(&dev->timer);
  870. video_unregister_device(&dev->vfd);
  871. v4l2_device_unregister(&dev->v4l2_dev);
  872. return 0;
  873. }
  874. static struct platform_driver vim2m_pdrv = {
  875. .probe = vim2m_probe,
  876. .remove = vim2m_remove,
  877. .driver = {
  878. .name = MEM2MEM_NAME,
  879. },
  880. };
  881. static void __exit vim2m_exit(void)
  882. {
  883. platform_driver_unregister(&vim2m_pdrv);
  884. platform_device_unregister(&vim2m_pdev);
  885. }
  886. static int __init vim2m_init(void)
  887. {
  888. int ret;
  889. ret = platform_device_register(&vim2m_pdev);
  890. if (ret)
  891. return ret;
  892. ret = platform_driver_register(&vim2m_pdrv);
  893. if (ret)
  894. platform_device_unregister(&vim2m_pdev);
  895. return ret;
  896. }
  897. module_init(vim2m_init);
  898. module_exit(vim2m_exit);