gsc-m2m.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * Samsung EXYNOS5 SoC series G-Scaler driver
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation, either version 2 of the License,
  10. * or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/bug.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/list.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/clk.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include "gsc-core.h"
  27. static int gsc_m2m_ctx_stop_req(struct gsc_ctx *ctx)
  28. {
  29. struct gsc_ctx *curr_ctx;
  30. struct gsc_dev *gsc = ctx->gsc_dev;
  31. int ret;
  32. curr_ctx = v4l2_m2m_get_curr_priv(gsc->m2m.m2m_dev);
  33. if (!gsc_m2m_pending(gsc) || (curr_ctx != ctx))
  34. return 0;
  35. gsc_ctx_state_lock_set(GSC_CTX_STOP_REQ, ctx);
  36. ret = wait_event_timeout(gsc->irq_queue,
  37. !gsc_ctx_state_is_set(GSC_CTX_STOP_REQ, ctx),
  38. GSC_SHUTDOWN_TIMEOUT);
  39. return ret == 0 ? -ETIMEDOUT : ret;
  40. }
  41. static int gsc_m2m_start_streaming(struct vb2_queue *q, unsigned int count)
  42. {
  43. struct gsc_ctx *ctx = q->drv_priv;
  44. int ret;
  45. ret = pm_runtime_get_sync(&ctx->gsc_dev->pdev->dev);
  46. return ret > 0 ? 0 : ret;
  47. }
  48. static int gsc_m2m_stop_streaming(struct vb2_queue *q)
  49. {
  50. struct gsc_ctx *ctx = q->drv_priv;
  51. int ret;
  52. ret = gsc_m2m_ctx_stop_req(ctx);
  53. if (ret == -ETIMEDOUT)
  54. gsc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
  55. pm_runtime_put(&ctx->gsc_dev->pdev->dev);
  56. return 0;
  57. }
  58. void gsc_m2m_job_finish(struct gsc_ctx *ctx, int vb_state)
  59. {
  60. struct vb2_buffer *src_vb, *dst_vb;
  61. if (!ctx || !ctx->m2m_ctx)
  62. return;
  63. src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  64. dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  65. if (src_vb && dst_vb) {
  66. v4l2_m2m_buf_done(src_vb, vb_state);
  67. v4l2_m2m_buf_done(dst_vb, vb_state);
  68. v4l2_m2m_job_finish(ctx->gsc_dev->m2m.m2m_dev,
  69. ctx->m2m_ctx);
  70. }
  71. }
  72. static void gsc_m2m_job_abort(void *priv)
  73. {
  74. struct gsc_ctx *ctx = priv;
  75. int ret;
  76. ret = gsc_m2m_ctx_stop_req(ctx);
  77. if (ret == -ETIMEDOUT)
  78. gsc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
  79. }
  80. static int gsc_fill_addr(struct gsc_ctx *ctx)
  81. {
  82. struct gsc_frame *s_frame, *d_frame;
  83. struct vb2_buffer *vb = NULL;
  84. int ret;
  85. s_frame = &ctx->s_frame;
  86. d_frame = &ctx->d_frame;
  87. vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  88. ret = gsc_prepare_addr(ctx, vb, s_frame, &s_frame->addr);
  89. if (ret)
  90. return ret;
  91. vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  92. return gsc_prepare_addr(ctx, vb, d_frame, &d_frame->addr);
  93. }
  94. static void gsc_m2m_device_run(void *priv)
  95. {
  96. struct gsc_ctx *ctx = priv;
  97. struct gsc_dev *gsc;
  98. unsigned long flags;
  99. int ret;
  100. bool is_set = false;
  101. if (WARN(!ctx, "null hardware context\n"))
  102. return;
  103. gsc = ctx->gsc_dev;
  104. spin_lock_irqsave(&gsc->slock, flags);
  105. set_bit(ST_M2M_PEND, &gsc->state);
  106. /* Reconfigure hardware if the context has changed. */
  107. if (gsc->m2m.ctx != ctx) {
  108. pr_debug("gsc->m2m.ctx = 0x%p, current_ctx = 0x%p",
  109. gsc->m2m.ctx, ctx);
  110. ctx->state |= GSC_PARAMS;
  111. gsc->m2m.ctx = ctx;
  112. }
  113. is_set = (ctx->state & GSC_CTX_STOP_REQ) ? 1 : 0;
  114. ctx->state &= ~GSC_CTX_STOP_REQ;
  115. if (is_set) {
  116. wake_up(&gsc->irq_queue);
  117. goto put_device;
  118. }
  119. ret = gsc_fill_addr(ctx);
  120. if (ret) {
  121. pr_err("Wrong address");
  122. goto put_device;
  123. }
  124. gsc_set_prefbuf(gsc, &ctx->s_frame);
  125. gsc_hw_set_input_addr(gsc, &ctx->s_frame.addr, GSC_M2M_BUF_NUM);
  126. gsc_hw_set_output_addr(gsc, &ctx->d_frame.addr, GSC_M2M_BUF_NUM);
  127. if (ctx->state & GSC_PARAMS) {
  128. gsc_hw_set_input_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
  129. gsc_hw_set_output_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
  130. gsc_hw_set_frm_done_irq_mask(gsc, false);
  131. gsc_hw_set_gsc_irq_enable(gsc, true);
  132. if (gsc_set_scaler_info(ctx)) {
  133. pr_err("Scaler setup error");
  134. goto put_device;
  135. }
  136. gsc_hw_set_input_path(ctx);
  137. gsc_hw_set_in_size(ctx);
  138. gsc_hw_set_in_image_format(ctx);
  139. gsc_hw_set_output_path(ctx);
  140. gsc_hw_set_out_size(ctx);
  141. gsc_hw_set_out_image_format(ctx);
  142. gsc_hw_set_prescaler(ctx);
  143. gsc_hw_set_mainscaler(ctx);
  144. gsc_hw_set_rotation(ctx);
  145. gsc_hw_set_global_alpha(ctx);
  146. }
  147. /* update shadow registers */
  148. gsc_hw_set_sfr_update(ctx);
  149. ctx->state &= ~GSC_PARAMS;
  150. gsc_hw_enable_control(gsc, true);
  151. spin_unlock_irqrestore(&gsc->slock, flags);
  152. return;
  153. put_device:
  154. ctx->state &= ~GSC_PARAMS;
  155. spin_unlock_irqrestore(&gsc->slock, flags);
  156. }
  157. static int gsc_m2m_queue_setup(struct vb2_queue *vq,
  158. const struct v4l2_format *fmt,
  159. unsigned int *num_buffers, unsigned int *num_planes,
  160. unsigned int sizes[], void *allocators[])
  161. {
  162. struct gsc_ctx *ctx = vb2_get_drv_priv(vq);
  163. struct gsc_frame *frame;
  164. int i;
  165. frame = ctx_get_frame(ctx, vq->type);
  166. if (IS_ERR(frame))
  167. return PTR_ERR(frame);
  168. if (!frame->fmt)
  169. return -EINVAL;
  170. *num_planes = frame->fmt->num_planes;
  171. for (i = 0; i < frame->fmt->num_planes; i++) {
  172. sizes[i] = frame->payload[i];
  173. allocators[i] = ctx->gsc_dev->alloc_ctx;
  174. }
  175. return 0;
  176. }
  177. static int gsc_m2m_buf_prepare(struct vb2_buffer *vb)
  178. {
  179. struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  180. struct gsc_frame *frame;
  181. int i;
  182. frame = ctx_get_frame(ctx, vb->vb2_queue->type);
  183. if (IS_ERR(frame))
  184. return PTR_ERR(frame);
  185. if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  186. for (i = 0; i < frame->fmt->num_planes; i++)
  187. vb2_set_plane_payload(vb, i, frame->payload[i]);
  188. }
  189. return 0;
  190. }
  191. static void gsc_m2m_buf_queue(struct vb2_buffer *vb)
  192. {
  193. struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  194. pr_debug("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
  195. if (ctx->m2m_ctx)
  196. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  197. }
  198. static struct vb2_ops gsc_m2m_qops = {
  199. .queue_setup = gsc_m2m_queue_setup,
  200. .buf_prepare = gsc_m2m_buf_prepare,
  201. .buf_queue = gsc_m2m_buf_queue,
  202. .wait_prepare = gsc_unlock,
  203. .wait_finish = gsc_lock,
  204. .stop_streaming = gsc_m2m_stop_streaming,
  205. .start_streaming = gsc_m2m_start_streaming,
  206. };
  207. static int gsc_m2m_querycap(struct file *file, void *fh,
  208. struct v4l2_capability *cap)
  209. {
  210. struct gsc_ctx *ctx = fh_to_ctx(fh);
  211. struct gsc_dev *gsc = ctx->gsc_dev;
  212. strlcpy(cap->driver, gsc->pdev->name, sizeof(cap->driver));
  213. strlcpy(cap->card, gsc->pdev->name, sizeof(cap->card));
  214. strlcpy(cap->bus_info, "platform", sizeof(cap->bus_info));
  215. cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE |
  216. V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  217. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  218. return 0;
  219. }
  220. static int gsc_m2m_enum_fmt_mplane(struct file *file, void *priv,
  221. struct v4l2_fmtdesc *f)
  222. {
  223. return gsc_enum_fmt_mplane(f);
  224. }
  225. static int gsc_m2m_g_fmt_mplane(struct file *file, void *fh,
  226. struct v4l2_format *f)
  227. {
  228. struct gsc_ctx *ctx = fh_to_ctx(fh);
  229. return gsc_g_fmt_mplane(ctx, f);
  230. }
  231. static int gsc_m2m_try_fmt_mplane(struct file *file, void *fh,
  232. struct v4l2_format *f)
  233. {
  234. struct gsc_ctx *ctx = fh_to_ctx(fh);
  235. return gsc_try_fmt_mplane(ctx, f);
  236. }
  237. static int gsc_m2m_s_fmt_mplane(struct file *file, void *fh,
  238. struct v4l2_format *f)
  239. {
  240. struct gsc_ctx *ctx = fh_to_ctx(fh);
  241. struct vb2_queue *vq;
  242. struct gsc_frame *frame;
  243. struct v4l2_pix_format_mplane *pix;
  244. int i, ret = 0;
  245. ret = gsc_m2m_try_fmt_mplane(file, fh, f);
  246. if (ret)
  247. return ret;
  248. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  249. if (vb2_is_streaming(vq)) {
  250. pr_err("queue (%d) busy", f->type);
  251. return -EBUSY;
  252. }
  253. if (V4L2_TYPE_IS_OUTPUT(f->type))
  254. frame = &ctx->s_frame;
  255. else
  256. frame = &ctx->d_frame;
  257. pix = &f->fmt.pix_mp;
  258. frame->fmt = find_fmt(&pix->pixelformat, NULL, 0);
  259. frame->colorspace = pix->colorspace;
  260. if (!frame->fmt)
  261. return -EINVAL;
  262. for (i = 0; i < frame->fmt->num_planes; i++)
  263. frame->payload[i] = pix->plane_fmt[i].sizeimage;
  264. gsc_set_frame_size(frame, pix->width, pix->height);
  265. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  266. gsc_ctx_state_lock_set(GSC_PARAMS | GSC_DST_FMT, ctx);
  267. else
  268. gsc_ctx_state_lock_set(GSC_PARAMS | GSC_SRC_FMT, ctx);
  269. pr_debug("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
  270. return 0;
  271. }
  272. static int gsc_m2m_reqbufs(struct file *file, void *fh,
  273. struct v4l2_requestbuffers *reqbufs)
  274. {
  275. struct gsc_ctx *ctx = fh_to_ctx(fh);
  276. struct gsc_dev *gsc = ctx->gsc_dev;
  277. struct gsc_frame *frame;
  278. u32 max_cnt;
  279. max_cnt = (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
  280. gsc->variant->in_buf_cnt : gsc->variant->out_buf_cnt;
  281. if (reqbufs->count > max_cnt) {
  282. return -EINVAL;
  283. } else if (reqbufs->count == 0) {
  284. if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  285. gsc_ctx_state_lock_clear(GSC_SRC_FMT, ctx);
  286. else
  287. gsc_ctx_state_lock_clear(GSC_DST_FMT, ctx);
  288. }
  289. frame = ctx_get_frame(ctx, reqbufs->type);
  290. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  291. }
  292. static int gsc_m2m_querybuf(struct file *file, void *fh,
  293. struct v4l2_buffer *buf)
  294. {
  295. struct gsc_ctx *ctx = fh_to_ctx(fh);
  296. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  297. }
  298. static int gsc_m2m_qbuf(struct file *file, void *fh,
  299. struct v4l2_buffer *buf)
  300. {
  301. struct gsc_ctx *ctx = fh_to_ctx(fh);
  302. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  303. }
  304. static int gsc_m2m_dqbuf(struct file *file, void *fh,
  305. struct v4l2_buffer *buf)
  306. {
  307. struct gsc_ctx *ctx = fh_to_ctx(fh);
  308. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  309. }
  310. static int gsc_m2m_streamon(struct file *file, void *fh,
  311. enum v4l2_buf_type type)
  312. {
  313. struct gsc_ctx *ctx = fh_to_ctx(fh);
  314. /* The source and target color format need to be set */
  315. if (V4L2_TYPE_IS_OUTPUT(type)) {
  316. if (!gsc_ctx_state_is_set(GSC_SRC_FMT, ctx))
  317. return -EINVAL;
  318. } else if (!gsc_ctx_state_is_set(GSC_DST_FMT, ctx)) {
  319. return -EINVAL;
  320. }
  321. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  322. }
  323. static int gsc_m2m_streamoff(struct file *file, void *fh,
  324. enum v4l2_buf_type type)
  325. {
  326. struct gsc_ctx *ctx = fh_to_ctx(fh);
  327. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  328. }
  329. /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
  330. static int is_rectangle_enclosed(struct v4l2_rect *a, struct v4l2_rect *b)
  331. {
  332. if (a->left < b->left || a->top < b->top)
  333. return 0;
  334. if (a->left + a->width > b->left + b->width)
  335. return 0;
  336. if (a->top + a->height > b->top + b->height)
  337. return 0;
  338. return 1;
  339. }
  340. static int gsc_m2m_g_selection(struct file *file, void *fh,
  341. struct v4l2_selection *s)
  342. {
  343. struct gsc_frame *frame;
  344. struct gsc_ctx *ctx = fh_to_ctx(fh);
  345. if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
  346. (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
  347. return -EINVAL;
  348. frame = ctx_get_frame(ctx, s->type);
  349. if (IS_ERR(frame))
  350. return PTR_ERR(frame);
  351. switch (s->target) {
  352. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  353. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  354. case V4L2_SEL_TGT_CROP_BOUNDS:
  355. case V4L2_SEL_TGT_CROP_DEFAULT:
  356. s->r.left = 0;
  357. s->r.top = 0;
  358. s->r.width = frame->f_width;
  359. s->r.height = frame->f_height;
  360. return 0;
  361. case V4L2_SEL_TGT_COMPOSE:
  362. case V4L2_SEL_TGT_CROP:
  363. s->r.left = frame->crop.left;
  364. s->r.top = frame->crop.top;
  365. s->r.width = frame->crop.width;
  366. s->r.height = frame->crop.height;
  367. return 0;
  368. }
  369. return -EINVAL;
  370. }
  371. static int gsc_m2m_s_selection(struct file *file, void *fh,
  372. struct v4l2_selection *s)
  373. {
  374. struct gsc_frame *frame;
  375. struct gsc_ctx *ctx = fh_to_ctx(fh);
  376. struct v4l2_crop cr;
  377. struct gsc_variant *variant = ctx->gsc_dev->variant;
  378. int ret;
  379. cr.type = s->type;
  380. cr.c = s->r;
  381. if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
  382. (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
  383. return -EINVAL;
  384. ret = gsc_try_crop(ctx, &cr);
  385. if (ret)
  386. return ret;
  387. if (s->flags & V4L2_SEL_FLAG_LE &&
  388. !is_rectangle_enclosed(&cr.c, &s->r))
  389. return -ERANGE;
  390. if (s->flags & V4L2_SEL_FLAG_GE &&
  391. !is_rectangle_enclosed(&s->r, &cr.c))
  392. return -ERANGE;
  393. s->r = cr.c;
  394. switch (s->target) {
  395. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  396. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  397. case V4L2_SEL_TGT_COMPOSE:
  398. frame = &ctx->s_frame;
  399. break;
  400. case V4L2_SEL_TGT_CROP_BOUNDS:
  401. case V4L2_SEL_TGT_CROP:
  402. case V4L2_SEL_TGT_CROP_DEFAULT:
  403. frame = &ctx->d_frame;
  404. break;
  405. default:
  406. return -EINVAL;
  407. }
  408. /* Check to see if scaling ratio is within supported range */
  409. if (gsc_ctx_state_is_set(GSC_DST_FMT | GSC_SRC_FMT, ctx)) {
  410. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  411. ret = gsc_check_scaler_ratio(variant, cr.c.width,
  412. cr.c.height, ctx->d_frame.crop.width,
  413. ctx->d_frame.crop.height,
  414. ctx->gsc_ctrls.rotate->val, ctx->out_path);
  415. } else {
  416. ret = gsc_check_scaler_ratio(variant,
  417. ctx->s_frame.crop.width,
  418. ctx->s_frame.crop.height, cr.c.width,
  419. cr.c.height, ctx->gsc_ctrls.rotate->val,
  420. ctx->out_path);
  421. }
  422. if (ret) {
  423. pr_err("Out of scaler range");
  424. return -EINVAL;
  425. }
  426. }
  427. frame->crop = cr.c;
  428. gsc_ctx_state_lock_set(GSC_PARAMS, ctx);
  429. return 0;
  430. }
  431. static const struct v4l2_ioctl_ops gsc_m2m_ioctl_ops = {
  432. .vidioc_querycap = gsc_m2m_querycap,
  433. .vidioc_enum_fmt_vid_cap_mplane = gsc_m2m_enum_fmt_mplane,
  434. .vidioc_enum_fmt_vid_out_mplane = gsc_m2m_enum_fmt_mplane,
  435. .vidioc_g_fmt_vid_cap_mplane = gsc_m2m_g_fmt_mplane,
  436. .vidioc_g_fmt_vid_out_mplane = gsc_m2m_g_fmt_mplane,
  437. .vidioc_try_fmt_vid_cap_mplane = gsc_m2m_try_fmt_mplane,
  438. .vidioc_try_fmt_vid_out_mplane = gsc_m2m_try_fmt_mplane,
  439. .vidioc_s_fmt_vid_cap_mplane = gsc_m2m_s_fmt_mplane,
  440. .vidioc_s_fmt_vid_out_mplane = gsc_m2m_s_fmt_mplane,
  441. .vidioc_reqbufs = gsc_m2m_reqbufs,
  442. .vidioc_querybuf = gsc_m2m_querybuf,
  443. .vidioc_qbuf = gsc_m2m_qbuf,
  444. .vidioc_dqbuf = gsc_m2m_dqbuf,
  445. .vidioc_streamon = gsc_m2m_streamon,
  446. .vidioc_streamoff = gsc_m2m_streamoff,
  447. .vidioc_g_selection = gsc_m2m_g_selection,
  448. .vidioc_s_selection = gsc_m2m_s_selection
  449. };
  450. static int queue_init(void *priv, struct vb2_queue *src_vq,
  451. struct vb2_queue *dst_vq)
  452. {
  453. struct gsc_ctx *ctx = priv;
  454. int ret;
  455. memset(src_vq, 0, sizeof(*src_vq));
  456. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  457. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  458. src_vq->drv_priv = ctx;
  459. src_vq->ops = &gsc_m2m_qops;
  460. src_vq->mem_ops = &vb2_dma_contig_memops;
  461. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  462. ret = vb2_queue_init(src_vq);
  463. if (ret)
  464. return ret;
  465. memset(dst_vq, 0, sizeof(*dst_vq));
  466. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  467. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  468. dst_vq->drv_priv = ctx;
  469. dst_vq->ops = &gsc_m2m_qops;
  470. dst_vq->mem_ops = &vb2_dma_contig_memops;
  471. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  472. return vb2_queue_init(dst_vq);
  473. }
  474. static int gsc_m2m_open(struct file *file)
  475. {
  476. struct gsc_dev *gsc = video_drvdata(file);
  477. struct gsc_ctx *ctx = NULL;
  478. int ret;
  479. pr_debug("pid: %d, state: 0x%lx", task_pid_nr(current), gsc->state);
  480. if (mutex_lock_interruptible(&gsc->lock))
  481. return -ERESTARTSYS;
  482. ctx = kzalloc(sizeof (*ctx), GFP_KERNEL);
  483. if (!ctx) {
  484. ret = -ENOMEM;
  485. goto unlock;
  486. }
  487. v4l2_fh_init(&ctx->fh, gsc->m2m.vfd);
  488. ret = gsc_ctrls_create(ctx);
  489. if (ret)
  490. goto error_fh;
  491. /* Use separate control handler per file handle */
  492. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  493. file->private_data = &ctx->fh;
  494. v4l2_fh_add(&ctx->fh);
  495. ctx->gsc_dev = gsc;
  496. /* Default color format */
  497. ctx->s_frame.fmt = get_format(0);
  498. ctx->d_frame.fmt = get_format(0);
  499. /* Setup the device context for mem2mem mode. */
  500. ctx->state = GSC_CTX_M2M;
  501. ctx->flags = 0;
  502. ctx->in_path = GSC_DMA;
  503. ctx->out_path = GSC_DMA;
  504. ctx->m2m_ctx = v4l2_m2m_ctx_init(gsc->m2m.m2m_dev, ctx, queue_init);
  505. if (IS_ERR(ctx->m2m_ctx)) {
  506. pr_err("Failed to initialize m2m context");
  507. ret = PTR_ERR(ctx->m2m_ctx);
  508. goto error_ctrls;
  509. }
  510. if (gsc->m2m.refcnt++ == 0)
  511. set_bit(ST_M2M_OPEN, &gsc->state);
  512. pr_debug("gsc m2m driver is opened, ctx(0x%p)", ctx);
  513. mutex_unlock(&gsc->lock);
  514. return 0;
  515. error_ctrls:
  516. gsc_ctrls_delete(ctx);
  517. error_fh:
  518. v4l2_fh_del(&ctx->fh);
  519. v4l2_fh_exit(&ctx->fh);
  520. kfree(ctx);
  521. unlock:
  522. mutex_unlock(&gsc->lock);
  523. return ret;
  524. }
  525. static int gsc_m2m_release(struct file *file)
  526. {
  527. struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
  528. struct gsc_dev *gsc = ctx->gsc_dev;
  529. pr_debug("pid: %d, state: 0x%lx, refcnt= %d",
  530. task_pid_nr(current), gsc->state, gsc->m2m.refcnt);
  531. mutex_lock(&gsc->lock);
  532. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  533. gsc_ctrls_delete(ctx);
  534. v4l2_fh_del(&ctx->fh);
  535. v4l2_fh_exit(&ctx->fh);
  536. if (--gsc->m2m.refcnt <= 0)
  537. clear_bit(ST_M2M_OPEN, &gsc->state);
  538. kfree(ctx);
  539. mutex_unlock(&gsc->lock);
  540. return 0;
  541. }
  542. static unsigned int gsc_m2m_poll(struct file *file,
  543. struct poll_table_struct *wait)
  544. {
  545. struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
  546. struct gsc_dev *gsc = ctx->gsc_dev;
  547. int ret;
  548. if (mutex_lock_interruptible(&gsc->lock))
  549. return -ERESTARTSYS;
  550. ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  551. mutex_unlock(&gsc->lock);
  552. return ret;
  553. }
  554. static int gsc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
  555. {
  556. struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
  557. struct gsc_dev *gsc = ctx->gsc_dev;
  558. int ret;
  559. if (mutex_lock_interruptible(&gsc->lock))
  560. return -ERESTARTSYS;
  561. ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  562. mutex_unlock(&gsc->lock);
  563. return ret;
  564. }
  565. static const struct v4l2_file_operations gsc_m2m_fops = {
  566. .owner = THIS_MODULE,
  567. .open = gsc_m2m_open,
  568. .release = gsc_m2m_release,
  569. .poll = gsc_m2m_poll,
  570. .unlocked_ioctl = video_ioctl2,
  571. .mmap = gsc_m2m_mmap,
  572. };
  573. static struct v4l2_m2m_ops gsc_m2m_ops = {
  574. .device_run = gsc_m2m_device_run,
  575. .job_abort = gsc_m2m_job_abort,
  576. };
  577. int gsc_register_m2m_device(struct gsc_dev *gsc)
  578. {
  579. struct platform_device *pdev;
  580. int ret;
  581. if (!gsc)
  582. return -ENODEV;
  583. pdev = gsc->pdev;
  584. gsc->vdev.fops = &gsc_m2m_fops;
  585. gsc->vdev.ioctl_ops = &gsc_m2m_ioctl_ops;
  586. gsc->vdev.release = video_device_release_empty;
  587. gsc->vdev.lock = &gsc->lock;
  588. gsc->vdev.vfl_dir = VFL_DIR_M2M;
  589. snprintf(gsc->vdev.name, sizeof(gsc->vdev.name), "%s.%d:m2m",
  590. GSC_MODULE_NAME, gsc->id);
  591. video_set_drvdata(&gsc->vdev, gsc);
  592. gsc->m2m.vfd = &gsc->vdev;
  593. gsc->m2m.m2m_dev = v4l2_m2m_init(&gsc_m2m_ops);
  594. if (IS_ERR(gsc->m2m.m2m_dev)) {
  595. dev_err(&pdev->dev, "failed to initialize v4l2-m2m device\n");
  596. ret = PTR_ERR(gsc->m2m.m2m_dev);
  597. goto err_m2m_r1;
  598. }
  599. ret = video_register_device(&gsc->vdev, VFL_TYPE_GRABBER, -1);
  600. if (ret) {
  601. dev_err(&pdev->dev,
  602. "%s(): failed to register video device\n", __func__);
  603. goto err_m2m_r2;
  604. }
  605. pr_debug("gsc m2m driver registered as /dev/video%d", gsc->vdev.num);
  606. return 0;
  607. err_m2m_r2:
  608. v4l2_m2m_release(gsc->m2m.m2m_dev);
  609. err_m2m_r1:
  610. video_device_release(gsc->m2m.vfd);
  611. return ret;
  612. }
  613. void gsc_unregister_m2m_device(struct gsc_dev *gsc)
  614. {
  615. if (gsc)
  616. v4l2_m2m_release(gsc->m2m.m2m_dev);
  617. }