vim2m.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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, <[email protected]>
  14. * Marek Szyprowski, <[email protected]>
  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, <[email protected]>");
  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. v4l2_info(&dev->v4l2_dev,
  1099. "Device registered as /dev/video%d\n", vfd->num);
  1100. platform_set_drvdata(pdev, dev);
  1101. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  1102. if (IS_ERR(dev->m2m_dev)) {
  1103. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  1104. ret = PTR_ERR(dev->m2m_dev);
  1105. dev->m2m_dev = NULL;
  1106. goto error_dev;
  1107. }
  1108. #ifdef CONFIG_MEDIA_CONTROLLER
  1109. dev->mdev.dev = &pdev->dev;
  1110. strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model));
  1111. strscpy(dev->mdev.bus_info, "platform:vim2m",
  1112. sizeof(dev->mdev.bus_info));
  1113. media_device_init(&dev->mdev);
  1114. dev->mdev.ops = &m2m_media_ops;
  1115. dev->v4l2_dev.mdev = &dev->mdev;
  1116. #endif
  1117. ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
  1118. if (ret) {
  1119. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1120. goto error_m2m;
  1121. }
  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 int 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. return 0;
  1162. }
  1163. static struct platform_driver vim2m_pdrv = {
  1164. .probe = vim2m_probe,
  1165. .remove = vim2m_remove,
  1166. .driver = {
  1167. .name = MEM2MEM_NAME,
  1168. },
  1169. };
  1170. static void __exit vim2m_exit(void)
  1171. {
  1172. platform_driver_unregister(&vim2m_pdrv);
  1173. platform_device_unregister(&vim2m_pdev);
  1174. }
  1175. static int __init vim2m_init(void)
  1176. {
  1177. int ret;
  1178. ret = platform_device_register(&vim2m_pdev);
  1179. if (ret)
  1180. return ret;
  1181. ret = platform_driver_register(&vim2m_pdrv);
  1182. if (ret)
  1183. platform_device_unregister(&vim2m_pdev);
  1184. return ret;
  1185. }
  1186. module_init(vim2m_init);
  1187. module_exit(vim2m_exit);