vim2m.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * A virtual v4l2-mem2mem example device.
  4. *
  5. * This is a virtual device driver for testing mem-to-mem videobuf framework.
  6. * It simulates a device that uses memory buffers for both source and
  7. * destination, processes the data and issues an "irq" (simulated by a delayed
  8. * workqueue).
  9. * The device is capable of multi-instance, multi-buffer-per-transaction
  10. * operation (via the mem2mem framework).
  11. *
  12. * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
  13. * Pawel Osciak, <pawel@osciak.com>
  14. * Marek Szyprowski, <m.szyprowski@samsung.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by the
  18. * Free Software Foundation; either version 2 of the
  19. * License, or (at your option) any later version
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/fs.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/platform_device.h>
  27. #include <media/v4l2-mem2mem.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-ioctl.h>
  30. #include <media/v4l2-ctrls.h>
  31. #include <media/v4l2-event.h>
  32. #include <media/videobuf2-vmalloc.h>
  33. MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
  34. MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  35. MODULE_LICENSE("GPL");
  36. MODULE_VERSION("0.2");
  37. MODULE_ALIAS("mem2mem_testdev");
  38. static unsigned int debug;
  39. module_param(debug, uint, 0644);
  40. MODULE_PARM_DESC(debug, "debug level");
  41. /* Default transaction time in msec */
  42. static unsigned int default_transtime = 40; /* Max 25 fps */
  43. module_param(default_transtime, uint, 0644);
  44. MODULE_PARM_DESC(default_transtime, "default transaction time in ms");
  45. #define MIN_W 32
  46. #define MIN_H 32
  47. #define MAX_W 640
  48. #define MAX_H 480
  49. /* Pixel alignment for non-bayer formats */
  50. #define WIDTH_ALIGN 2
  51. #define HEIGHT_ALIGN 1
  52. /* Pixel alignment for bayer formats */
  53. #define BAYER_WIDTH_ALIGN 2
  54. #define BAYER_HEIGHT_ALIGN 2
  55. /* Flags that indicate a format can be used for capture/output */
  56. #define MEM2MEM_CAPTURE BIT(0)
  57. #define MEM2MEM_OUTPUT BIT(1)
  58. #define MEM2MEM_NAME "vim2m"
  59. /* Per queue */
  60. #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
  61. /* In bytes, per queue */
  62. #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
  63. /* Flags that indicate processing mode */
  64. #define MEM2MEM_HFLIP BIT(0)
  65. #define MEM2MEM_VFLIP BIT(1)
  66. #define dprintk(dev, lvl, fmt, arg...) \
  67. v4l2_dbg(lvl, debug, &(dev)->v4l2_dev, "%s: " fmt, __func__, ## arg)
  68. static void vim2m_dev_release(struct device *dev)
  69. {}
  70. static struct platform_device vim2m_pdev = {
  71. .name = MEM2MEM_NAME,
  72. .dev.release = vim2m_dev_release,
  73. };
  74. struct vim2m_fmt {
  75. u32 fourcc;
  76. int depth;
  77. /* Types the format can be used for */
  78. u32 types;
  79. };
  80. static struct vim2m_fmt formats[] = {
  81. {
  82. .fourcc = V4L2_PIX_FMT_RGB565, /* rrrrrggg gggbbbbb */
  83. .depth = 16,
  84. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  85. }, {
  86. .fourcc = V4L2_PIX_FMT_RGB565X, /* gggbbbbb rrrrrggg */
  87. .depth = 16,
  88. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  89. }, {
  90. .fourcc = V4L2_PIX_FMT_RGB24,
  91. .depth = 24,
  92. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  93. }, {
  94. .fourcc = V4L2_PIX_FMT_BGR24,
  95. .depth = 24,
  96. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  97. }, {
  98. .fourcc = V4L2_PIX_FMT_YUYV,
  99. .depth = 16,
  100. .types = MEM2MEM_CAPTURE,
  101. }, {
  102. .fourcc = V4L2_PIX_FMT_SBGGR8,
  103. .depth = 8,
  104. .types = MEM2MEM_CAPTURE,
  105. }, {
  106. .fourcc = V4L2_PIX_FMT_SGBRG8,
  107. .depth = 8,
  108. .types = MEM2MEM_CAPTURE,
  109. }, {
  110. .fourcc = V4L2_PIX_FMT_SGRBG8,
  111. .depth = 8,
  112. .types = MEM2MEM_CAPTURE,
  113. }, {
  114. .fourcc = V4L2_PIX_FMT_SRGGB8,
  115. .depth = 8,
  116. .types = MEM2MEM_CAPTURE,
  117. },
  118. };
  119. #define NUM_FORMATS ARRAY_SIZE(formats)
  120. /* Per-queue, driver-specific private data */
  121. struct vim2m_q_data {
  122. unsigned int width;
  123. unsigned int height;
  124. unsigned int sizeimage;
  125. unsigned int sequence;
  126. struct vim2m_fmt *fmt;
  127. };
  128. enum {
  129. V4L2_M2M_SRC = 0,
  130. V4L2_M2M_DST = 1,
  131. };
  132. #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
  133. #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
  134. static struct vim2m_fmt *find_format(u32 fourcc)
  135. {
  136. struct vim2m_fmt *fmt;
  137. unsigned int k;
  138. for (k = 0; k < NUM_FORMATS; k++) {
  139. fmt = &formats[k];
  140. if (fmt->fourcc == fourcc)
  141. break;
  142. }
  143. if (k == NUM_FORMATS)
  144. return NULL;
  145. return &formats[k];
  146. }
  147. static void get_alignment(u32 fourcc,
  148. unsigned int *walign, unsigned int *halign)
  149. {
  150. switch (fourcc) {
  151. case V4L2_PIX_FMT_SBGGR8:
  152. case V4L2_PIX_FMT_SGBRG8:
  153. case V4L2_PIX_FMT_SGRBG8:
  154. case V4L2_PIX_FMT_SRGGB8:
  155. *walign = BAYER_WIDTH_ALIGN;
  156. *halign = BAYER_HEIGHT_ALIGN;
  157. return;
  158. default:
  159. *walign = WIDTH_ALIGN;
  160. *halign = HEIGHT_ALIGN;
  161. return;
  162. }
  163. }
  164. struct vim2m_dev {
  165. struct v4l2_device v4l2_dev;
  166. struct video_device vfd;
  167. #ifdef CONFIG_MEDIA_CONTROLLER
  168. struct media_device mdev;
  169. #endif
  170. atomic_t num_inst;
  171. struct mutex dev_mutex;
  172. struct v4l2_m2m_dev *m2m_dev;
  173. };
  174. struct vim2m_ctx {
  175. struct v4l2_fh fh;
  176. struct vim2m_dev *dev;
  177. struct v4l2_ctrl_handler hdl;
  178. /* Processed buffers in this transaction */
  179. u8 num_processed;
  180. /* Transaction length (i.e. how many buffers per transaction) */
  181. u32 translen;
  182. /* Transaction time (i.e. simulated processing time) in milliseconds */
  183. u32 transtime;
  184. struct mutex vb_mutex;
  185. struct delayed_work work_run;
  186. /* Abort requested by m2m */
  187. int aborting;
  188. /* Processing mode */
  189. int mode;
  190. enum v4l2_colorspace colorspace;
  191. enum v4l2_ycbcr_encoding ycbcr_enc;
  192. enum v4l2_xfer_func xfer_func;
  193. enum v4l2_quantization quant;
  194. /* Source and destination queue data */
  195. struct vim2m_q_data q_data[2];
  196. };
  197. static inline struct vim2m_ctx *file2ctx(struct file *file)
  198. {
  199. return container_of(file->private_data, struct vim2m_ctx, fh);
  200. }
  201. static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
  202. enum v4l2_buf_type type)
  203. {
  204. switch (type) {
  205. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  206. return &ctx->q_data[V4L2_M2M_SRC];
  207. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  208. return &ctx->q_data[V4L2_M2M_DST];
  209. default:
  210. return NULL;
  211. }
  212. }
  213. static const char *type_name(enum v4l2_buf_type type)
  214. {
  215. switch (type) {
  216. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  217. return "Output";
  218. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  219. return "Capture";
  220. default:
  221. return "Invalid";
  222. }
  223. }
  224. #define CLIP(__color) \
  225. (u8)(((__color) > 0xff) ? 0xff : (((__color) < 0) ? 0 : (__color)))
  226. static void copy_line(struct vim2m_q_data *q_data_out,
  227. u8 *src, u8 *dst, bool reverse)
  228. {
  229. int x, depth = q_data_out->fmt->depth >> 3;
  230. if (!reverse) {
  231. memcpy(dst, src, q_data_out->width * depth);
  232. } else {
  233. for (x = 0; x < q_data_out->width >> 1; x++) {
  234. memcpy(dst, src, depth);
  235. memcpy(dst + depth, src - depth, depth);
  236. src -= depth << 1;
  237. dst += depth << 1;
  238. }
  239. return;
  240. }
  241. }
  242. static void copy_two_pixels(struct vim2m_q_data *q_data_in,
  243. struct vim2m_q_data *q_data_out,
  244. u8 *src[2], u8 **dst, int ypos, bool reverse)
  245. {
  246. struct vim2m_fmt *out = q_data_out->fmt;
  247. struct vim2m_fmt *in = q_data_in->fmt;
  248. u8 _r[2], _g[2], _b[2], *r, *g, *b;
  249. int i;
  250. /* Step 1: read two consecutive pixels from src pointer */
  251. r = _r;
  252. g = _g;
  253. b = _b;
  254. switch (in->fourcc) {
  255. case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
  256. for (i = 0; i < 2; i++) {
  257. u16 pix = le16_to_cpu(*(__le16 *)(src[i]));
  258. *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
  259. *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
  260. *b++ = (u8)((pix & 0x1f) << 3) | 0x07;
  261. }
  262. break;
  263. case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
  264. for (i = 0; i < 2; i++) {
  265. u16 pix = be16_to_cpu(*(__be16 *)(src[i]));
  266. *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
  267. *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
  268. *b++ = (u8)((pix & 0x1f) << 3) | 0x07;
  269. }
  270. break;
  271. default:
  272. case V4L2_PIX_FMT_RGB24:
  273. for (i = 0; i < 2; i++) {
  274. *r++ = src[i][0];
  275. *g++ = src[i][1];
  276. *b++ = src[i][2];
  277. }
  278. break;
  279. case V4L2_PIX_FMT_BGR24:
  280. for (i = 0; i < 2; i++) {
  281. *b++ = src[i][0];
  282. *g++ = src[i][1];
  283. *r++ = src[i][2];
  284. }
  285. break;
  286. }
  287. /* Step 2: store two consecutive points, reversing them if needed */
  288. r = _r;
  289. g = _g;
  290. b = _b;
  291. switch (out->fourcc) {
  292. case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
  293. for (i = 0; i < 2; i++) {
  294. u16 pix;
  295. __le16 *dst_pix = (__le16 *)*dst;
  296. pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
  297. (*b >> 3);
  298. *dst_pix = cpu_to_le16(pix);
  299. *dst += 2;
  300. }
  301. return;
  302. case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
  303. for (i = 0; i < 2; i++) {
  304. u16 pix;
  305. __be16 *dst_pix = (__be16 *)*dst;
  306. pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
  307. (*b >> 3);
  308. *dst_pix = cpu_to_be16(pix);
  309. *dst += 2;
  310. }
  311. return;
  312. case V4L2_PIX_FMT_RGB24:
  313. for (i = 0; i < 2; i++) {
  314. *(*dst)++ = *r++;
  315. *(*dst)++ = *g++;
  316. *(*dst)++ = *b++;
  317. }
  318. return;
  319. case V4L2_PIX_FMT_BGR24:
  320. for (i = 0; i < 2; i++) {
  321. *(*dst)++ = *b++;
  322. *(*dst)++ = *g++;
  323. *(*dst)++ = *r++;
  324. }
  325. return;
  326. case V4L2_PIX_FMT_YUYV:
  327. default:
  328. {
  329. u8 y, y1, u, v;
  330. y = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
  331. + 524288) >> 15);
  332. u = ((-4878 * (*r) - 9578 * (*g) + 14456 * (*b)
  333. + 4210688) >> 15);
  334. v = ((14456 * (*r++) - 12105 * (*g++) - 2351 * (*b++)
  335. + 4210688) >> 15);
  336. y1 = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
  337. + 524288) >> 15);
  338. *(*dst)++ = y;
  339. *(*dst)++ = u;
  340. *(*dst)++ = y1;
  341. *(*dst)++ = v;
  342. return;
  343. }
  344. case V4L2_PIX_FMT_SBGGR8:
  345. if (!(ypos & 1)) {
  346. *(*dst)++ = *b;
  347. *(*dst)++ = *++g;
  348. } else {
  349. *(*dst)++ = *g;
  350. *(*dst)++ = *++r;
  351. }
  352. return;
  353. case V4L2_PIX_FMT_SGBRG8:
  354. if (!(ypos & 1)) {
  355. *(*dst)++ = *g;
  356. *(*dst)++ = *++b;
  357. } else {
  358. *(*dst)++ = *r;
  359. *(*dst)++ = *++g;
  360. }
  361. return;
  362. case V4L2_PIX_FMT_SGRBG8:
  363. if (!(ypos & 1)) {
  364. *(*dst)++ = *g;
  365. *(*dst)++ = *++r;
  366. } else {
  367. *(*dst)++ = *b;
  368. *(*dst)++ = *++g;
  369. }
  370. return;
  371. case V4L2_PIX_FMT_SRGGB8:
  372. if (!(ypos & 1)) {
  373. *(*dst)++ = *r;
  374. *(*dst)++ = *++g;
  375. } else {
  376. *(*dst)++ = *g;
  377. *(*dst)++ = *++b;
  378. }
  379. return;
  380. }
  381. }
  382. static int device_process(struct vim2m_ctx *ctx,
  383. struct vb2_v4l2_buffer *in_vb,
  384. struct vb2_v4l2_buffer *out_vb)
  385. {
  386. struct vim2m_dev *dev = ctx->dev;
  387. struct vim2m_q_data *q_data_in, *q_data_out;
  388. u8 *p_in, *p_line, *p_in_x[2], *p, *p_out;
  389. unsigned int width, height, bytesperline, bytes_per_pixel;
  390. unsigned int x, y, y_in, y_out, x_int, x_fract, x_err, x_offset;
  391. int start, end, step;
  392. q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  393. if (!q_data_in)
  394. return 0;
  395. bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3;
  396. bytes_per_pixel = q_data_in->fmt->depth >> 3;
  397. q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  398. if (!q_data_out)
  399. return 0;
  400. /* As we're doing scaling, use the output dimensions here */
  401. height = q_data_out->height;
  402. width = q_data_out->width;
  403. p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
  404. p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
  405. if (!p_in || !p_out) {
  406. v4l2_err(&dev->v4l2_dev,
  407. "Acquiring kernel pointers to buffers failed\n");
  408. return -EFAULT;
  409. }
  410. out_vb->sequence = q_data_out->sequence++;
  411. in_vb->sequence = q_data_in->sequence++;
  412. v4l2_m2m_buf_copy_metadata(in_vb, out_vb, true);
  413. if (ctx->mode & MEM2MEM_VFLIP) {
  414. start = height - 1;
  415. end = -1;
  416. step = -1;
  417. } else {
  418. start = 0;
  419. end = height;
  420. step = 1;
  421. }
  422. y_out = 0;
  423. /*
  424. * When format and resolution are identical,
  425. * we can use a faster copy logic
  426. */
  427. if (q_data_in->fmt->fourcc == q_data_out->fmt->fourcc &&
  428. q_data_in->width == q_data_out->width &&
  429. q_data_in->height == q_data_out->height) {
  430. for (y = start; y != end; y += step, y_out++) {
  431. p = p_in + (y * bytesperline);
  432. if (ctx->mode & MEM2MEM_HFLIP)
  433. p += bytesperline - (q_data_in->fmt->depth >> 3);
  434. copy_line(q_data_out, p, p_out,
  435. ctx->mode & MEM2MEM_HFLIP);
  436. p_out += bytesperline;
  437. }
  438. return 0;
  439. }
  440. /* Slower algorithm with format conversion, hflip, vflip and scaler */
  441. /* To speed scaler up, use Bresenham for X dimension */
  442. x_int = q_data_in->width / q_data_out->width;
  443. x_fract = q_data_in->width % q_data_out->width;
  444. for (y = start; y != end; y += step, y_out++) {
  445. y_in = (y * q_data_in->height) / q_data_out->height;
  446. x_offset = 0;
  447. x_err = 0;
  448. p_line = p_in + (y_in * bytesperline);
  449. if (ctx->mode & MEM2MEM_HFLIP)
  450. p_line += bytesperline - (q_data_in->fmt->depth >> 3);
  451. p_in_x[0] = p_line;
  452. for (x = 0; x < width >> 1; x++) {
  453. x_offset += x_int;
  454. x_err += x_fract;
  455. if (x_err > width) {
  456. x_offset++;
  457. x_err -= width;
  458. }
  459. if (ctx->mode & MEM2MEM_HFLIP)
  460. p_in_x[1] = p_line - x_offset * bytes_per_pixel;
  461. else
  462. p_in_x[1] = p_line + x_offset * bytes_per_pixel;
  463. copy_two_pixels(q_data_in, q_data_out,
  464. p_in_x, &p_out, y_out,
  465. ctx->mode & MEM2MEM_HFLIP);
  466. /* Calculate the next p_in_x0 */
  467. x_offset += x_int;
  468. x_err += x_fract;
  469. if (x_err > width) {
  470. x_offset++;
  471. x_err -= width;
  472. }
  473. if (ctx->mode & MEM2MEM_HFLIP)
  474. p_in_x[0] = p_line - x_offset * bytes_per_pixel;
  475. else
  476. p_in_x[0] = p_line + x_offset * bytes_per_pixel;
  477. }
  478. }
  479. return 0;
  480. }
  481. /*
  482. * mem2mem callbacks
  483. */
  484. /*
  485. * job_ready() - check whether an instance is ready to be scheduled to run
  486. */
  487. static int job_ready(void *priv)
  488. {
  489. struct vim2m_ctx *ctx = priv;
  490. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
  491. || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
  492. dprintk(ctx->dev, 1, "Not enough buffers available\n");
  493. return 0;
  494. }
  495. return 1;
  496. }
  497. static void job_abort(void *priv)
  498. {
  499. struct vim2m_ctx *ctx = priv;
  500. /* Will cancel the transaction in the next interrupt handler */
  501. ctx->aborting = 1;
  502. }
  503. /* device_run() - prepares and starts the device
  504. *
  505. * This simulates all the immediate preparations required before starting
  506. * a device. This will be called by the framework when it decides to schedule
  507. * a particular instance.
  508. */
  509. static void device_run(void *priv)
  510. {
  511. struct vim2m_ctx *ctx = priv;
  512. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  513. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  514. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  515. /* Apply request controls if any */
  516. v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req,
  517. &ctx->hdl);
  518. device_process(ctx, src_buf, dst_buf);
  519. /* Complete request controls if any */
  520. v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
  521. &ctx->hdl);
  522. /* Run delayed work, which simulates a hardware irq */
  523. schedule_delayed_work(&ctx->work_run, msecs_to_jiffies(ctx->transtime));
  524. }
  525. static void device_work(struct work_struct *w)
  526. {
  527. struct vim2m_ctx *curr_ctx;
  528. struct vim2m_dev *vim2m_dev;
  529. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  530. curr_ctx = container_of(w, struct vim2m_ctx, work_run.work);
  531. if (!curr_ctx) {
  532. pr_err("Instance released before the end of transaction\n");
  533. return;
  534. }
  535. vim2m_dev = curr_ctx->dev;
  536. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  537. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  538. curr_ctx->num_processed++;
  539. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  540. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  541. if (curr_ctx->num_processed == curr_ctx->translen
  542. || curr_ctx->aborting) {
  543. dprintk(curr_ctx->dev, 2, "Finishing capture buffer fill\n");
  544. curr_ctx->num_processed = 0;
  545. v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
  546. } else {
  547. device_run(curr_ctx);
  548. }
  549. }
  550. /*
  551. * video ioctls
  552. */
  553. static int vidioc_querycap(struct file *file, void *priv,
  554. struct v4l2_capability *cap)
  555. {
  556. strscpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver));
  557. strscpy(cap->card, MEM2MEM_NAME, sizeof(cap->card));
  558. snprintf(cap->bus_info, sizeof(cap->bus_info),
  559. "platform:%s", MEM2MEM_NAME);
  560. return 0;
  561. }
  562. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  563. {
  564. int i, num;
  565. struct vim2m_fmt *fmt;
  566. num = 0;
  567. for (i = 0; i < NUM_FORMATS; ++i) {
  568. if (formats[i].types & type) {
  569. /* index-th format of type type found ? */
  570. if (num == f->index)
  571. break;
  572. /*
  573. * Correct type but haven't reached our index yet,
  574. * just increment per-type index
  575. */
  576. ++num;
  577. }
  578. }
  579. if (i < NUM_FORMATS) {
  580. /* Format found */
  581. fmt = &formats[i];
  582. f->pixelformat = fmt->fourcc;
  583. return 0;
  584. }
  585. /* Format not found */
  586. return -EINVAL;
  587. }
  588. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  589. struct v4l2_fmtdesc *f)
  590. {
  591. return enum_fmt(f, MEM2MEM_CAPTURE);
  592. }
  593. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  594. struct v4l2_fmtdesc *f)
  595. {
  596. return enum_fmt(f, MEM2MEM_OUTPUT);
  597. }
  598. static int vidioc_enum_framesizes(struct file *file, void *priv,
  599. struct v4l2_frmsizeenum *fsize)
  600. {
  601. if (fsize->index != 0)
  602. return -EINVAL;
  603. if (!find_format(fsize->pixel_format))
  604. return -EINVAL;
  605. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  606. fsize->stepwise.min_width = MIN_W;
  607. fsize->stepwise.min_height = MIN_H;
  608. fsize->stepwise.max_width = MAX_W;
  609. fsize->stepwise.max_height = MAX_H;
  610. get_alignment(fsize->pixel_format,
  611. &fsize->stepwise.step_width,
  612. &fsize->stepwise.step_height);
  613. return 0;
  614. }
  615. static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
  616. {
  617. struct vb2_queue *vq;
  618. struct vim2m_q_data *q_data;
  619. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  620. if (!vq)
  621. return -EINVAL;
  622. q_data = get_q_data(ctx, f->type);
  623. if (!q_data)
  624. return -EINVAL;
  625. f->fmt.pix.width = q_data->width;
  626. f->fmt.pix.height = q_data->height;
  627. f->fmt.pix.field = V4L2_FIELD_NONE;
  628. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  629. f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
  630. f->fmt.pix.sizeimage = q_data->sizeimage;
  631. f->fmt.pix.colorspace = ctx->colorspace;
  632. f->fmt.pix.xfer_func = ctx->xfer_func;
  633. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  634. f->fmt.pix.quantization = ctx->quant;
  635. return 0;
  636. }
  637. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  638. struct v4l2_format *f)
  639. {
  640. return vidioc_g_fmt(file2ctx(file), f);
  641. }
  642. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  643. struct v4l2_format *f)
  644. {
  645. return vidioc_g_fmt(file2ctx(file), f);
  646. }
  647. static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
  648. {
  649. int walign, halign;
  650. /*
  651. * V4L2 specification specifies the driver corrects the
  652. * format struct if any of the dimensions is unsupported
  653. */
  654. if (f->fmt.pix.height < MIN_H)
  655. f->fmt.pix.height = MIN_H;
  656. else if (f->fmt.pix.height > MAX_H)
  657. f->fmt.pix.height = MAX_H;
  658. if (f->fmt.pix.width < MIN_W)
  659. f->fmt.pix.width = MIN_W;
  660. else if (f->fmt.pix.width > MAX_W)
  661. f->fmt.pix.width = MAX_W;
  662. get_alignment(f->fmt.pix.pixelformat, &walign, &halign);
  663. f->fmt.pix.width &= ~(walign - 1);
  664. f->fmt.pix.height &= ~(halign - 1);
  665. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  666. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  667. f->fmt.pix.field = V4L2_FIELD_NONE;
  668. return 0;
  669. }
  670. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  671. struct v4l2_format *f)
  672. {
  673. struct vim2m_fmt *fmt;
  674. struct vim2m_ctx *ctx = file2ctx(file);
  675. fmt = find_format(f->fmt.pix.pixelformat);
  676. if (!fmt) {
  677. f->fmt.pix.pixelformat = formats[0].fourcc;
  678. fmt = find_format(f->fmt.pix.pixelformat);
  679. }
  680. if (!(fmt->types & MEM2MEM_CAPTURE)) {
  681. v4l2_err(&ctx->dev->v4l2_dev,
  682. "Fourcc format (0x%08x) invalid.\n",
  683. f->fmt.pix.pixelformat);
  684. return -EINVAL;
  685. }
  686. f->fmt.pix.colorspace = ctx->colorspace;
  687. f->fmt.pix.xfer_func = ctx->xfer_func;
  688. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  689. f->fmt.pix.quantization = ctx->quant;
  690. return vidioc_try_fmt(f, fmt);
  691. }
  692. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  693. struct v4l2_format *f)
  694. {
  695. struct vim2m_fmt *fmt;
  696. struct vim2m_ctx *ctx = file2ctx(file);
  697. fmt = find_format(f->fmt.pix.pixelformat);
  698. if (!fmt) {
  699. f->fmt.pix.pixelformat = formats[0].fourcc;
  700. fmt = find_format(f->fmt.pix.pixelformat);
  701. }
  702. if (!(fmt->types & MEM2MEM_OUTPUT)) {
  703. v4l2_err(&ctx->dev->v4l2_dev,
  704. "Fourcc format (0x%08x) invalid.\n",
  705. f->fmt.pix.pixelformat);
  706. return -EINVAL;
  707. }
  708. if (!f->fmt.pix.colorspace)
  709. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  710. return vidioc_try_fmt(f, fmt);
  711. }
  712. static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
  713. {
  714. struct vim2m_q_data *q_data;
  715. struct vb2_queue *vq;
  716. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  717. if (!vq)
  718. return -EINVAL;
  719. q_data = get_q_data(ctx, f->type);
  720. if (!q_data)
  721. return -EINVAL;
  722. if (vb2_is_busy(vq)) {
  723. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  724. return -EBUSY;
  725. }
  726. q_data->fmt = find_format(f->fmt.pix.pixelformat);
  727. q_data->width = f->fmt.pix.width;
  728. q_data->height = f->fmt.pix.height;
  729. q_data->sizeimage = q_data->width * q_data->height
  730. * q_data->fmt->depth >> 3;
  731. dprintk(ctx->dev, 1,
  732. "Format for type %s: %dx%d (%d bpp), fmt: %c%c%c%c\n",
  733. type_name(f->type), q_data->width, q_data->height,
  734. q_data->fmt->depth,
  735. (q_data->fmt->fourcc & 0xff),
  736. (q_data->fmt->fourcc >> 8) & 0xff,
  737. (q_data->fmt->fourcc >> 16) & 0xff,
  738. (q_data->fmt->fourcc >> 24) & 0xff);
  739. return 0;
  740. }
  741. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  742. struct v4l2_format *f)
  743. {
  744. int ret;
  745. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  746. if (ret)
  747. return ret;
  748. return vidioc_s_fmt(file2ctx(file), f);
  749. }
  750. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  751. struct v4l2_format *f)
  752. {
  753. struct vim2m_ctx *ctx = file2ctx(file);
  754. int ret;
  755. ret = vidioc_try_fmt_vid_out(file, priv, f);
  756. if (ret)
  757. return ret;
  758. ret = vidioc_s_fmt(file2ctx(file), f);
  759. if (!ret) {
  760. ctx->colorspace = f->fmt.pix.colorspace;
  761. ctx->xfer_func = f->fmt.pix.xfer_func;
  762. ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
  763. ctx->quant = f->fmt.pix.quantization;
  764. }
  765. return ret;
  766. }
  767. static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
  768. {
  769. struct vim2m_ctx *ctx =
  770. container_of(ctrl->handler, struct vim2m_ctx, hdl);
  771. switch (ctrl->id) {
  772. case V4L2_CID_HFLIP:
  773. if (ctrl->val)
  774. ctx->mode |= MEM2MEM_HFLIP;
  775. else
  776. ctx->mode &= ~MEM2MEM_HFLIP;
  777. break;
  778. case V4L2_CID_VFLIP:
  779. if (ctrl->val)
  780. ctx->mode |= MEM2MEM_VFLIP;
  781. else
  782. ctx->mode &= ~MEM2MEM_VFLIP;
  783. break;
  784. case V4L2_CID_TRANS_TIME_MSEC:
  785. ctx->transtime = ctrl->val;
  786. if (ctx->transtime < 1)
  787. ctx->transtime = 1;
  788. break;
  789. case V4L2_CID_TRANS_NUM_BUFS:
  790. ctx->translen = ctrl->val;
  791. break;
  792. default:
  793. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  794. return -EINVAL;
  795. }
  796. return 0;
  797. }
  798. static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
  799. .s_ctrl = vim2m_s_ctrl,
  800. };
  801. static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
  802. .vidioc_querycap = vidioc_querycap,
  803. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  804. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  805. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  806. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  807. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  808. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  809. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  810. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  811. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  812. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  813. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  814. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  815. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  816. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  817. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  818. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  819. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  820. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  821. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  822. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  823. };
  824. /*
  825. * Queue operations
  826. */
  827. static int vim2m_queue_setup(struct vb2_queue *vq,
  828. unsigned int *nbuffers,
  829. unsigned int *nplanes,
  830. unsigned int sizes[],
  831. struct device *alloc_devs[])
  832. {
  833. struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
  834. struct vim2m_q_data *q_data;
  835. unsigned int size, count = *nbuffers;
  836. q_data = get_q_data(ctx, vq->type);
  837. if (!q_data)
  838. return -EINVAL;
  839. size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
  840. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  841. (count)--;
  842. *nbuffers = count;
  843. if (*nplanes)
  844. return sizes[0] < size ? -EINVAL : 0;
  845. *nplanes = 1;
  846. sizes[0] = size;
  847. dprintk(ctx->dev, 1, "%s: get %d buffer(s) of size %d each.\n",
  848. type_name(vq->type), count, size);
  849. return 0;
  850. }
  851. static int vim2m_buf_out_validate(struct vb2_buffer *vb)
  852. {
  853. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  854. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  855. if (vbuf->field == V4L2_FIELD_ANY)
  856. vbuf->field = V4L2_FIELD_NONE;
  857. if (vbuf->field != V4L2_FIELD_NONE) {
  858. dprintk(ctx->dev, 1, "%s field isn't supported\n", __func__);
  859. return -EINVAL;
  860. }
  861. return 0;
  862. }
  863. static int vim2m_buf_prepare(struct vb2_buffer *vb)
  864. {
  865. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  866. struct vim2m_q_data *q_data;
  867. dprintk(ctx->dev, 2, "type: %s\n", type_name(vb->vb2_queue->type));
  868. q_data = get_q_data(ctx, vb->vb2_queue->type);
  869. if (!q_data)
  870. return -EINVAL;
  871. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  872. dprintk(ctx->dev, 1,
  873. "%s data will not fit into plane (%lu < %lu)\n",
  874. __func__, vb2_plane_size(vb, 0),
  875. (long)q_data->sizeimage);
  876. return -EINVAL;
  877. }
  878. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  879. return 0;
  880. }
  881. static void vim2m_buf_queue(struct vb2_buffer *vb)
  882. {
  883. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  884. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  885. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  886. }
  887. static int vim2m_start_streaming(struct vb2_queue *q, unsigned int count)
  888. {
  889. struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
  890. struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
  891. if (!q_data)
  892. return -EINVAL;
  893. if (V4L2_TYPE_IS_OUTPUT(q->type))
  894. ctx->aborting = 0;
  895. q_data->sequence = 0;
  896. return 0;
  897. }
  898. static void vim2m_stop_streaming(struct vb2_queue *q)
  899. {
  900. struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
  901. struct vb2_v4l2_buffer *vbuf;
  902. cancel_delayed_work_sync(&ctx->work_run);
  903. for (;;) {
  904. if (V4L2_TYPE_IS_OUTPUT(q->type))
  905. vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  906. else
  907. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  908. if (!vbuf)
  909. return;
  910. v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
  911. &ctx->hdl);
  912. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  913. }
  914. }
  915. static void vim2m_buf_request_complete(struct vb2_buffer *vb)
  916. {
  917. struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  918. v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
  919. }
  920. static const struct vb2_ops vim2m_qops = {
  921. .queue_setup = vim2m_queue_setup,
  922. .buf_out_validate = vim2m_buf_out_validate,
  923. .buf_prepare = vim2m_buf_prepare,
  924. .buf_queue = vim2m_buf_queue,
  925. .start_streaming = vim2m_start_streaming,
  926. .stop_streaming = vim2m_stop_streaming,
  927. .wait_prepare = vb2_ops_wait_prepare,
  928. .wait_finish = vb2_ops_wait_finish,
  929. .buf_request_complete = vim2m_buf_request_complete,
  930. };
  931. static int queue_init(void *priv, struct vb2_queue *src_vq,
  932. struct vb2_queue *dst_vq)
  933. {
  934. struct vim2m_ctx *ctx = priv;
  935. int ret;
  936. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  937. src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  938. src_vq->drv_priv = ctx;
  939. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  940. src_vq->ops = &vim2m_qops;
  941. src_vq->mem_ops = &vb2_vmalloc_memops;
  942. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  943. src_vq->lock = &ctx->vb_mutex;
  944. src_vq->supports_requests = true;
  945. ret = vb2_queue_init(src_vq);
  946. if (ret)
  947. return ret;
  948. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  949. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  950. dst_vq->drv_priv = ctx;
  951. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  952. dst_vq->ops = &vim2m_qops;
  953. dst_vq->mem_ops = &vb2_vmalloc_memops;
  954. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  955. dst_vq->lock = &ctx->vb_mutex;
  956. return vb2_queue_init(dst_vq);
  957. }
  958. static struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
  959. .ops = &vim2m_ctrl_ops,
  960. .id = V4L2_CID_TRANS_TIME_MSEC,
  961. .name = "Transaction Time (msec)",
  962. .type = V4L2_CTRL_TYPE_INTEGER,
  963. .min = 1,
  964. .max = 10001,
  965. .step = 1,
  966. };
  967. static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
  968. .ops = &vim2m_ctrl_ops,
  969. .id = V4L2_CID_TRANS_NUM_BUFS,
  970. .name = "Buffers Per Transaction",
  971. .type = V4L2_CTRL_TYPE_INTEGER,
  972. .def = 1,
  973. .min = 1,
  974. .max = MEM2MEM_DEF_NUM_BUFS,
  975. .step = 1,
  976. };
  977. /*
  978. * File operations
  979. */
  980. static int vim2m_open(struct file *file)
  981. {
  982. struct vim2m_dev *dev = video_drvdata(file);
  983. struct vim2m_ctx *ctx = NULL;
  984. struct v4l2_ctrl_handler *hdl;
  985. int rc = 0;
  986. if (mutex_lock_interruptible(&dev->dev_mutex))
  987. return -ERESTARTSYS;
  988. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  989. if (!ctx) {
  990. rc = -ENOMEM;
  991. goto open_unlock;
  992. }
  993. v4l2_fh_init(&ctx->fh, video_devdata(file));
  994. file->private_data = &ctx->fh;
  995. ctx->dev = dev;
  996. hdl = &ctx->hdl;
  997. v4l2_ctrl_handler_init(hdl, 4);
  998. v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  999. v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  1000. vim2m_ctrl_trans_time_msec.def = default_transtime;
  1001. v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
  1002. v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
  1003. if (hdl->error) {
  1004. rc = hdl->error;
  1005. v4l2_ctrl_handler_free(hdl);
  1006. kfree(ctx);
  1007. goto open_unlock;
  1008. }
  1009. ctx->fh.ctrl_handler = hdl;
  1010. v4l2_ctrl_handler_setup(hdl);
  1011. ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
  1012. ctx->q_data[V4L2_M2M_SRC].width = 640;
  1013. ctx->q_data[V4L2_M2M_SRC].height = 480;
  1014. ctx->q_data[V4L2_M2M_SRC].sizeimage =
  1015. ctx->q_data[V4L2_M2M_SRC].width *
  1016. ctx->q_data[V4L2_M2M_SRC].height *
  1017. (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
  1018. ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
  1019. ctx->colorspace = V4L2_COLORSPACE_REC709;
  1020. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  1021. mutex_init(&ctx->vb_mutex);
  1022. INIT_DELAYED_WORK(&ctx->work_run, device_work);
  1023. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1024. rc = PTR_ERR(ctx->fh.m2m_ctx);
  1025. v4l2_ctrl_handler_free(hdl);
  1026. v4l2_fh_exit(&ctx->fh);
  1027. kfree(ctx);
  1028. goto open_unlock;
  1029. }
  1030. v4l2_fh_add(&ctx->fh);
  1031. atomic_inc(&dev->num_inst);
  1032. dprintk(dev, 1, "Created instance: %p, m2m_ctx: %p\n",
  1033. ctx, ctx->fh.m2m_ctx);
  1034. open_unlock:
  1035. mutex_unlock(&dev->dev_mutex);
  1036. return rc;
  1037. }
  1038. static int vim2m_release(struct file *file)
  1039. {
  1040. struct vim2m_dev *dev = video_drvdata(file);
  1041. struct vim2m_ctx *ctx = file2ctx(file);
  1042. dprintk(dev, 1, "Releasing instance %p\n", ctx);
  1043. v4l2_fh_del(&ctx->fh);
  1044. v4l2_fh_exit(&ctx->fh);
  1045. v4l2_ctrl_handler_free(&ctx->hdl);
  1046. mutex_lock(&dev->dev_mutex);
  1047. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1048. mutex_unlock(&dev->dev_mutex);
  1049. kfree(ctx);
  1050. atomic_dec(&dev->num_inst);
  1051. return 0;
  1052. }
  1053. static void vim2m_device_release(struct video_device *vdev)
  1054. {
  1055. struct vim2m_dev *dev = container_of(vdev, struct vim2m_dev, vfd);
  1056. v4l2_device_unregister(&dev->v4l2_dev);
  1057. v4l2_m2m_release(dev->m2m_dev);
  1058. #ifdef CONFIG_MEDIA_CONTROLLER
  1059. media_device_cleanup(&dev->mdev);
  1060. #endif
  1061. kfree(dev);
  1062. }
  1063. static const struct v4l2_file_operations vim2m_fops = {
  1064. .owner = THIS_MODULE,
  1065. .open = vim2m_open,
  1066. .release = vim2m_release,
  1067. .poll = v4l2_m2m_fop_poll,
  1068. .unlocked_ioctl = video_ioctl2,
  1069. .mmap = v4l2_m2m_fop_mmap,
  1070. };
  1071. static const struct video_device vim2m_videodev = {
  1072. .name = MEM2MEM_NAME,
  1073. .vfl_dir = VFL_DIR_M2M,
  1074. .fops = &vim2m_fops,
  1075. .ioctl_ops = &vim2m_ioctl_ops,
  1076. .minor = -1,
  1077. .release = vim2m_device_release,
  1078. .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
  1079. };
  1080. static const struct v4l2_m2m_ops m2m_ops = {
  1081. .device_run = device_run,
  1082. .job_ready = job_ready,
  1083. .job_abort = job_abort,
  1084. };
  1085. static const struct media_device_ops m2m_media_ops = {
  1086. .req_validate = vb2_request_validate,
  1087. .req_queue = v4l2_m2m_request_queue,
  1088. };
  1089. static int vim2m_probe(struct platform_device *pdev)
  1090. {
  1091. struct vim2m_dev *dev;
  1092. struct video_device *vfd;
  1093. int ret;
  1094. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1095. if (!dev)
  1096. return -ENOMEM;
  1097. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1098. if (ret)
  1099. goto error_free;
  1100. atomic_set(&dev->num_inst, 0);
  1101. mutex_init(&dev->dev_mutex);
  1102. dev->vfd = vim2m_videodev;
  1103. vfd = &dev->vfd;
  1104. vfd->lock = &dev->dev_mutex;
  1105. vfd->v4l2_dev = &dev->v4l2_dev;
  1106. ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
  1107. if (ret) {
  1108. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1109. goto error_v4l2;
  1110. }
  1111. video_set_drvdata(vfd, dev);
  1112. v4l2_info(&dev->v4l2_dev,
  1113. "Device registered as /dev/video%d\n", vfd->num);
  1114. platform_set_drvdata(pdev, dev);
  1115. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  1116. if (IS_ERR(dev->m2m_dev)) {
  1117. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  1118. ret = PTR_ERR(dev->m2m_dev);
  1119. dev->m2m_dev = NULL;
  1120. goto error_dev;
  1121. }
  1122. #ifdef CONFIG_MEDIA_CONTROLLER
  1123. dev->mdev.dev = &pdev->dev;
  1124. strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model));
  1125. strscpy(dev->mdev.bus_info, "platform:vim2m",
  1126. sizeof(dev->mdev.bus_info));
  1127. media_device_init(&dev->mdev);
  1128. dev->mdev.ops = &m2m_media_ops;
  1129. dev->v4l2_dev.mdev = &dev->mdev;
  1130. ret = v4l2_m2m_register_media_controller(dev->m2m_dev, vfd,
  1131. MEDIA_ENT_F_PROC_VIDEO_SCALER);
  1132. if (ret) {
  1133. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
  1134. goto error_dev;
  1135. }
  1136. ret = media_device_register(&dev->mdev);
  1137. if (ret) {
  1138. v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
  1139. goto error_m2m_mc;
  1140. }
  1141. #endif
  1142. return 0;
  1143. #ifdef CONFIG_MEDIA_CONTROLLER
  1144. error_m2m_mc:
  1145. v4l2_m2m_unregister_media_controller(dev->m2m_dev);
  1146. #endif
  1147. error_dev:
  1148. video_unregister_device(&dev->vfd);
  1149. /* vim2m_device_release called by video_unregister_device to release various objects */
  1150. return ret;
  1151. error_v4l2:
  1152. v4l2_device_unregister(&dev->v4l2_dev);
  1153. error_free:
  1154. kfree(dev);
  1155. return ret;
  1156. }
  1157. static int vim2m_remove(struct platform_device *pdev)
  1158. {
  1159. struct vim2m_dev *dev = platform_get_drvdata(pdev);
  1160. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
  1161. #ifdef CONFIG_MEDIA_CONTROLLER
  1162. media_device_unregister(&dev->mdev);
  1163. v4l2_m2m_unregister_media_controller(dev->m2m_dev);
  1164. #endif
  1165. video_unregister_device(&dev->vfd);
  1166. return 0;
  1167. }
  1168. static struct platform_driver vim2m_pdrv = {
  1169. .probe = vim2m_probe,
  1170. .remove = vim2m_remove,
  1171. .driver = {
  1172. .name = MEM2MEM_NAME,
  1173. },
  1174. };
  1175. static void __exit vim2m_exit(void)
  1176. {
  1177. platform_driver_unregister(&vim2m_pdrv);
  1178. platform_device_unregister(&vim2m_pdev);
  1179. }
  1180. static int __init vim2m_init(void)
  1181. {
  1182. int ret;
  1183. ret = platform_device_register(&vim2m_pdev);
  1184. if (ret)
  1185. return ret;
  1186. ret = platform_driver_register(&vim2m_pdrv);
  1187. if (ret)
  1188. platform_device_unregister(&vim2m_pdev);
  1189. return ret;
  1190. }
  1191. module_init(vim2m_init);
  1192. module_exit(vim2m_exit);