vim2m.c 27 KB

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