vim2m.c 34 KB

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