vim2m.c 33 KB

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