gsc-m2m.c 19 KB

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