gsc-m2m.c 19 KB

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