gsc-m2m.c 18 KB

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