gsc-m2m.c 19 KB

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