vivid-vid-out.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vivid-vid-out.c - video output support functions.
  4. *
  5. * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/videodev2.h>
  11. #include <linux/v4l2-dv-timings.h>
  12. #include <media/v4l2-common.h>
  13. #include <media/v4l2-event.h>
  14. #include <media/v4l2-dv-timings.h>
  15. #include <media/v4l2-rect.h>
  16. #include "vivid-core.h"
  17. #include "vivid-vid-common.h"
  18. #include "vivid-kthread-out.h"
  19. #include "vivid-vid-out.h"
  20. static int vid_out_queue_setup(struct vb2_queue *vq,
  21. unsigned *nbuffers, unsigned *nplanes,
  22. unsigned sizes[], struct device *alloc_devs[])
  23. {
  24. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  25. const struct vivid_fmt *vfmt = dev->fmt_out;
  26. unsigned planes = vfmt->buffers;
  27. unsigned h = dev->fmt_out_rect.height;
  28. unsigned size = dev->bytesperline_out[0] * h;
  29. unsigned p;
  30. for (p = vfmt->buffers; p < vfmt->planes; p++)
  31. size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p];
  32. if (dev->field_out == V4L2_FIELD_ALTERNATE) {
  33. /*
  34. * You cannot use write() with FIELD_ALTERNATE since the field
  35. * information (TOP/BOTTOM) cannot be passed to the kernel.
  36. */
  37. if (vb2_fileio_is_active(vq))
  38. return -EINVAL;
  39. }
  40. if (dev->queue_setup_error) {
  41. /*
  42. * Error injection: test what happens if queue_setup() returns
  43. * an error.
  44. */
  45. dev->queue_setup_error = false;
  46. return -EINVAL;
  47. }
  48. if (*nplanes) {
  49. /*
  50. * Check if the number of requested planes match
  51. * the number of planes in the current format. You can't mix that.
  52. */
  53. if (*nplanes != planes)
  54. return -EINVAL;
  55. if (sizes[0] < size)
  56. return -EINVAL;
  57. for (p = 1; p < planes; p++) {
  58. if (sizes[p] < dev->bytesperline_out[p] * h)
  59. return -EINVAL;
  60. }
  61. } else {
  62. for (p = 0; p < planes; p++)
  63. sizes[p] = p ? dev->bytesperline_out[p] * h : size;
  64. }
  65. if (vq->num_buffers + *nbuffers < 2)
  66. *nbuffers = 2 - vq->num_buffers;
  67. *nplanes = planes;
  68. dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
  69. for (p = 0; p < planes; p++)
  70. dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
  71. return 0;
  72. }
  73. static int vid_out_buf_prepare(struct vb2_buffer *vb)
  74. {
  75. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  76. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  77. unsigned long size;
  78. unsigned planes;
  79. unsigned p;
  80. dprintk(dev, 1, "%s\n", __func__);
  81. if (WARN_ON(NULL == dev->fmt_out))
  82. return -EINVAL;
  83. planes = dev->fmt_out->planes;
  84. if (dev->buf_prepare_error) {
  85. /*
  86. * Error injection: test what happens if buf_prepare() returns
  87. * an error.
  88. */
  89. dev->buf_prepare_error = false;
  90. return -EINVAL;
  91. }
  92. if (dev->field_out != V4L2_FIELD_ALTERNATE)
  93. vbuf->field = dev->field_out;
  94. else if (vbuf->field != V4L2_FIELD_TOP &&
  95. vbuf->field != V4L2_FIELD_BOTTOM)
  96. return -EINVAL;
  97. for (p = 0; p < planes; p++) {
  98. size = dev->bytesperline_out[p] * dev->fmt_out_rect.height +
  99. vb->planes[p].data_offset;
  100. if (vb2_get_plane_payload(vb, p) < size) {
  101. dprintk(dev, 1, "%s the payload is too small for plane %u (%lu < %lu)\n",
  102. __func__, p, vb2_get_plane_payload(vb, p), size);
  103. return -EINVAL;
  104. }
  105. }
  106. return 0;
  107. }
  108. static void vid_out_buf_queue(struct vb2_buffer *vb)
  109. {
  110. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  111. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  112. struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
  113. dprintk(dev, 1, "%s\n", __func__);
  114. spin_lock(&dev->slock);
  115. list_add_tail(&buf->list, &dev->vid_out_active);
  116. spin_unlock(&dev->slock);
  117. }
  118. static int vid_out_start_streaming(struct vb2_queue *vq, unsigned count)
  119. {
  120. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  121. int err;
  122. if (vb2_is_streaming(&dev->vb_vid_cap_q))
  123. dev->can_loop_video = vivid_vid_can_loop(dev);
  124. dev->vid_out_seq_count = 0;
  125. dprintk(dev, 1, "%s\n", __func__);
  126. if (dev->start_streaming_error) {
  127. dev->start_streaming_error = false;
  128. err = -EINVAL;
  129. } else {
  130. err = vivid_start_generating_vid_out(dev, &dev->vid_out_streaming);
  131. }
  132. if (err) {
  133. struct vivid_buffer *buf, *tmp;
  134. list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
  135. list_del(&buf->list);
  136. vb2_buffer_done(&buf->vb.vb2_buf,
  137. VB2_BUF_STATE_QUEUED);
  138. }
  139. }
  140. return err;
  141. }
  142. /* abort streaming and wait for last buffer */
  143. static void vid_out_stop_streaming(struct vb2_queue *vq)
  144. {
  145. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  146. dprintk(dev, 1, "%s\n", __func__);
  147. vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
  148. dev->can_loop_video = false;
  149. }
  150. const struct vb2_ops vivid_vid_out_qops = {
  151. .queue_setup = vid_out_queue_setup,
  152. .buf_prepare = vid_out_buf_prepare,
  153. .buf_queue = vid_out_buf_queue,
  154. .start_streaming = vid_out_start_streaming,
  155. .stop_streaming = vid_out_stop_streaming,
  156. .wait_prepare = vb2_ops_wait_prepare,
  157. .wait_finish = vb2_ops_wait_finish,
  158. };
  159. /*
  160. * Called whenever the format has to be reset which can occur when
  161. * changing outputs, standard, timings, etc.
  162. */
  163. void vivid_update_format_out(struct vivid_dev *dev)
  164. {
  165. struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
  166. unsigned size, p;
  167. u64 pixelclock;
  168. switch (dev->output_type[dev->output]) {
  169. case SVID:
  170. default:
  171. dev->field_out = dev->tv_field_out;
  172. dev->sink_rect.width = 720;
  173. if (dev->std_out & V4L2_STD_525_60) {
  174. dev->sink_rect.height = 480;
  175. dev->timeperframe_vid_out = (struct v4l2_fract) { 1001, 30000 };
  176. dev->service_set_out = V4L2_SLICED_CAPTION_525;
  177. } else {
  178. dev->sink_rect.height = 576;
  179. dev->timeperframe_vid_out = (struct v4l2_fract) { 1000, 25000 };
  180. dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
  181. }
  182. dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
  183. break;
  184. case HDMI:
  185. dev->sink_rect.width = bt->width;
  186. dev->sink_rect.height = bt->height;
  187. size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
  188. if (can_reduce_fps(bt) && (bt->flags & V4L2_DV_FL_REDUCED_FPS))
  189. pixelclock = div_u64(bt->pixelclock * 1000, 1001);
  190. else
  191. pixelclock = bt->pixelclock;
  192. dev->timeperframe_vid_out = (struct v4l2_fract) {
  193. size / 100, (u32)pixelclock / 100
  194. };
  195. if (bt->interlaced)
  196. dev->field_out = V4L2_FIELD_ALTERNATE;
  197. else
  198. dev->field_out = V4L2_FIELD_NONE;
  199. if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
  200. if (bt->width == 720 && bt->height <= 576)
  201. dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
  202. else
  203. dev->colorspace_out = V4L2_COLORSPACE_REC709;
  204. } else {
  205. dev->colorspace_out = V4L2_COLORSPACE_SRGB;
  206. }
  207. break;
  208. }
  209. dev->xfer_func_out = V4L2_XFER_FUNC_DEFAULT;
  210. dev->ycbcr_enc_out = V4L2_YCBCR_ENC_DEFAULT;
  211. dev->hsv_enc_out = V4L2_HSV_ENC_180;
  212. dev->quantization_out = V4L2_QUANTIZATION_DEFAULT;
  213. dev->compose_out = dev->sink_rect;
  214. dev->compose_bounds_out = dev->sink_rect;
  215. dev->crop_out = dev->compose_out;
  216. if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
  217. dev->crop_out.height /= 2;
  218. dev->fmt_out_rect = dev->crop_out;
  219. for (p = 0; p < dev->fmt_out->planes; p++)
  220. dev->bytesperline_out[p] =
  221. (dev->sink_rect.width * dev->fmt_out->bit_depth[p]) / 8;
  222. }
  223. /* Map the field to something that is valid for the current output */
  224. static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
  225. {
  226. if (vivid_is_svid_out(dev)) {
  227. switch (field) {
  228. case V4L2_FIELD_INTERLACED_TB:
  229. case V4L2_FIELD_INTERLACED_BT:
  230. case V4L2_FIELD_SEQ_TB:
  231. case V4L2_FIELD_SEQ_BT:
  232. case V4L2_FIELD_ALTERNATE:
  233. return field;
  234. case V4L2_FIELD_INTERLACED:
  235. default:
  236. return V4L2_FIELD_INTERLACED;
  237. }
  238. }
  239. if (vivid_is_hdmi_out(dev))
  240. return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
  241. V4L2_FIELD_NONE;
  242. return V4L2_FIELD_NONE;
  243. }
  244. static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
  245. {
  246. if (vivid_is_svid_out(dev))
  247. return (dev->std_out & V4L2_STD_525_60) ?
  248. TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
  249. if (vivid_is_hdmi_out(dev) &&
  250. dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
  251. return dev->sink_rect.height == 480 ?
  252. TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
  253. return TPG_PIXEL_ASPECT_SQUARE;
  254. }
  255. int vivid_g_fmt_vid_out(struct file *file, void *priv,
  256. struct v4l2_format *f)
  257. {
  258. struct vivid_dev *dev = video_drvdata(file);
  259. struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
  260. const struct vivid_fmt *fmt = dev->fmt_out;
  261. unsigned p;
  262. mp->width = dev->fmt_out_rect.width;
  263. mp->height = dev->fmt_out_rect.height;
  264. mp->field = dev->field_out;
  265. mp->pixelformat = fmt->fourcc;
  266. mp->colorspace = dev->colorspace_out;
  267. mp->xfer_func = dev->xfer_func_out;
  268. mp->ycbcr_enc = dev->ycbcr_enc_out;
  269. mp->quantization = dev->quantization_out;
  270. mp->num_planes = fmt->buffers;
  271. for (p = 0; p < mp->num_planes; p++) {
  272. mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
  273. mp->plane_fmt[p].sizeimage =
  274. mp->plane_fmt[p].bytesperline * mp->height;
  275. }
  276. for (p = fmt->buffers; p < fmt->planes; p++) {
  277. unsigned stride = dev->bytesperline_out[p];
  278. mp->plane_fmt[0].sizeimage +=
  279. (stride * mp->height) / fmt->vdownsampling[p];
  280. }
  281. return 0;
  282. }
  283. int vivid_try_fmt_vid_out(struct file *file, void *priv,
  284. struct v4l2_format *f)
  285. {
  286. struct vivid_dev *dev = video_drvdata(file);
  287. struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
  288. struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
  289. struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
  290. const struct vivid_fmt *fmt;
  291. unsigned bytesperline, max_bpl;
  292. unsigned factor = 1;
  293. unsigned w, h;
  294. unsigned p;
  295. fmt = vivid_get_format(dev, mp->pixelformat);
  296. if (!fmt) {
  297. dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
  298. mp->pixelformat);
  299. mp->pixelformat = V4L2_PIX_FMT_YUYV;
  300. fmt = vivid_get_format(dev, mp->pixelformat);
  301. }
  302. mp->field = vivid_field_out(dev, mp->field);
  303. if (vivid_is_svid_out(dev)) {
  304. w = 720;
  305. h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
  306. } else {
  307. w = dev->sink_rect.width;
  308. h = dev->sink_rect.height;
  309. }
  310. if (V4L2_FIELD_HAS_T_OR_B(mp->field))
  311. factor = 2;
  312. if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
  313. mp->width = w;
  314. mp->height = h / factor;
  315. } else {
  316. struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
  317. v4l2_rect_set_min_size(&r, &vivid_min_rect);
  318. v4l2_rect_set_max_size(&r, &vivid_max_rect);
  319. if (dev->has_scaler_out && !dev->has_crop_out) {
  320. struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
  321. v4l2_rect_set_max_size(&r, &max_r);
  322. } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
  323. v4l2_rect_set_max_size(&r, &dev->sink_rect);
  324. } else if (!dev->has_scaler_out && !dev->has_compose_out) {
  325. v4l2_rect_set_min_size(&r, &dev->sink_rect);
  326. }
  327. mp->width = r.width;
  328. mp->height = r.height / factor;
  329. }
  330. /* This driver supports custom bytesperline values */
  331. mp->num_planes = fmt->buffers;
  332. for (p = 0; p < fmt->buffers; p++) {
  333. /* Calculate the minimum supported bytesperline value */
  334. bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
  335. /* Calculate the maximum supported bytesperline value */
  336. max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
  337. if (pfmt[p].bytesperline > max_bpl)
  338. pfmt[p].bytesperline = max_bpl;
  339. if (pfmt[p].bytesperline < bytesperline)
  340. pfmt[p].bytesperline = bytesperline;
  341. pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
  342. fmt->vdownsampling[p];
  343. memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
  344. }
  345. for (p = fmt->buffers; p < fmt->planes; p++)
  346. pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
  347. (fmt->bit_depth[p] / fmt->vdownsampling[p])) /
  348. (fmt->bit_depth[0] / fmt->vdownsampling[0]);
  349. mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  350. mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  351. mp->quantization = V4L2_QUANTIZATION_DEFAULT;
  352. if (vivid_is_svid_out(dev)) {
  353. mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
  354. } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
  355. mp->colorspace = V4L2_COLORSPACE_SRGB;
  356. if (dev->dvi_d_out)
  357. mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
  358. } else if (bt->width == 720 && bt->height <= 576) {
  359. mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
  360. } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
  361. mp->colorspace != V4L2_COLORSPACE_REC709 &&
  362. mp->colorspace != V4L2_COLORSPACE_OPRGB &&
  363. mp->colorspace != V4L2_COLORSPACE_BT2020 &&
  364. mp->colorspace != V4L2_COLORSPACE_SRGB) {
  365. mp->colorspace = V4L2_COLORSPACE_REC709;
  366. }
  367. memset(mp->reserved, 0, sizeof(mp->reserved));
  368. return 0;
  369. }
  370. int vivid_s_fmt_vid_out(struct file *file, void *priv,
  371. struct v4l2_format *f)
  372. {
  373. struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
  374. struct vivid_dev *dev = video_drvdata(file);
  375. struct v4l2_rect *crop = &dev->crop_out;
  376. struct v4l2_rect *compose = &dev->compose_out;
  377. struct vb2_queue *q = &dev->vb_vid_out_q;
  378. int ret = vivid_try_fmt_vid_out(file, priv, f);
  379. unsigned factor = 1;
  380. unsigned p;
  381. if (ret < 0)
  382. return ret;
  383. if (vb2_is_busy(q) &&
  384. (vivid_is_svid_out(dev) ||
  385. mp->width != dev->fmt_out_rect.width ||
  386. mp->height != dev->fmt_out_rect.height ||
  387. mp->pixelformat != dev->fmt_out->fourcc ||
  388. mp->field != dev->field_out)) {
  389. dprintk(dev, 1, "%s device busy\n", __func__);
  390. return -EBUSY;
  391. }
  392. /*
  393. * Allow for changing the colorspace on the fly. Useful for testing
  394. * purposes, and it is something that HDMI transmitters are able
  395. * to do.
  396. */
  397. if (vb2_is_busy(q))
  398. goto set_colorspace;
  399. dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
  400. if (V4L2_FIELD_HAS_T_OR_B(mp->field))
  401. factor = 2;
  402. if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
  403. struct v4l2_rect r = { 0, 0, mp->width, mp->height };
  404. if (dev->has_scaler_out) {
  405. if (dev->has_crop_out)
  406. v4l2_rect_map_inside(crop, &r);
  407. else
  408. *crop = r;
  409. if (dev->has_compose_out && !dev->has_crop_out) {
  410. struct v4l2_rect min_r = {
  411. 0, 0,
  412. r.width / MAX_ZOOM,
  413. factor * r.height / MAX_ZOOM
  414. };
  415. struct v4l2_rect max_r = {
  416. 0, 0,
  417. r.width * MAX_ZOOM,
  418. factor * r.height * MAX_ZOOM
  419. };
  420. v4l2_rect_set_min_size(compose, &min_r);
  421. v4l2_rect_set_max_size(compose, &max_r);
  422. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  423. } else if (dev->has_compose_out) {
  424. struct v4l2_rect min_r = {
  425. 0, 0,
  426. crop->width / MAX_ZOOM,
  427. factor * crop->height / MAX_ZOOM
  428. };
  429. struct v4l2_rect max_r = {
  430. 0, 0,
  431. crop->width * MAX_ZOOM,
  432. factor * crop->height * MAX_ZOOM
  433. };
  434. v4l2_rect_set_min_size(compose, &min_r);
  435. v4l2_rect_set_max_size(compose, &max_r);
  436. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  437. }
  438. } else if (dev->has_compose_out && !dev->has_crop_out) {
  439. v4l2_rect_set_size_to(crop, &r);
  440. r.height *= factor;
  441. v4l2_rect_set_size_to(compose, &r);
  442. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  443. } else if (!dev->has_compose_out) {
  444. v4l2_rect_map_inside(crop, &r);
  445. r.height /= factor;
  446. v4l2_rect_set_size_to(compose, &r);
  447. } else {
  448. r.height *= factor;
  449. v4l2_rect_set_max_size(compose, &r);
  450. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  451. crop->top *= factor;
  452. crop->height *= factor;
  453. v4l2_rect_set_size_to(crop, compose);
  454. v4l2_rect_map_inside(crop, &r);
  455. crop->top /= factor;
  456. crop->height /= factor;
  457. }
  458. } else {
  459. struct v4l2_rect r = { 0, 0, mp->width, mp->height };
  460. v4l2_rect_set_size_to(crop, &r);
  461. r.height /= factor;
  462. v4l2_rect_set_size_to(compose, &r);
  463. }
  464. dev->fmt_out_rect.width = mp->width;
  465. dev->fmt_out_rect.height = mp->height;
  466. for (p = 0; p < mp->num_planes; p++)
  467. dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
  468. for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
  469. dev->bytesperline_out[p] =
  470. (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
  471. dev->fmt_out->bit_depth[0];
  472. dev->field_out = mp->field;
  473. if (vivid_is_svid_out(dev))
  474. dev->tv_field_out = mp->field;
  475. set_colorspace:
  476. dev->colorspace_out = mp->colorspace;
  477. dev->xfer_func_out = mp->xfer_func;
  478. dev->ycbcr_enc_out = mp->ycbcr_enc;
  479. dev->quantization_out = mp->quantization;
  480. if (dev->loop_video) {
  481. vivid_send_source_change(dev, SVID);
  482. vivid_send_source_change(dev, HDMI);
  483. }
  484. return 0;
  485. }
  486. int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
  487. struct v4l2_format *f)
  488. {
  489. struct vivid_dev *dev = video_drvdata(file);
  490. if (!dev->multiplanar)
  491. return -ENOTTY;
  492. return vivid_g_fmt_vid_out(file, priv, f);
  493. }
  494. int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
  495. struct v4l2_format *f)
  496. {
  497. struct vivid_dev *dev = video_drvdata(file);
  498. if (!dev->multiplanar)
  499. return -ENOTTY;
  500. return vivid_try_fmt_vid_out(file, priv, f);
  501. }
  502. int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
  503. struct v4l2_format *f)
  504. {
  505. struct vivid_dev *dev = video_drvdata(file);
  506. if (!dev->multiplanar)
  507. return -ENOTTY;
  508. return vivid_s_fmt_vid_out(file, priv, f);
  509. }
  510. int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  511. struct v4l2_format *f)
  512. {
  513. struct vivid_dev *dev = video_drvdata(file);
  514. if (dev->multiplanar)
  515. return -ENOTTY;
  516. return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
  517. }
  518. int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  519. struct v4l2_format *f)
  520. {
  521. struct vivid_dev *dev = video_drvdata(file);
  522. if (dev->multiplanar)
  523. return -ENOTTY;
  524. return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
  525. }
  526. int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  527. struct v4l2_format *f)
  528. {
  529. struct vivid_dev *dev = video_drvdata(file);
  530. if (dev->multiplanar)
  531. return -ENOTTY;
  532. return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
  533. }
  534. int vivid_vid_out_g_selection(struct file *file, void *priv,
  535. struct v4l2_selection *sel)
  536. {
  537. struct vivid_dev *dev = video_drvdata(file);
  538. if (!dev->has_crop_out && !dev->has_compose_out)
  539. return -ENOTTY;
  540. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  541. return -EINVAL;
  542. sel->r.left = sel->r.top = 0;
  543. switch (sel->target) {
  544. case V4L2_SEL_TGT_CROP:
  545. if (!dev->has_crop_out)
  546. return -EINVAL;
  547. sel->r = dev->crop_out;
  548. break;
  549. case V4L2_SEL_TGT_CROP_DEFAULT:
  550. if (!dev->has_crop_out)
  551. return -EINVAL;
  552. sel->r = dev->fmt_out_rect;
  553. break;
  554. case V4L2_SEL_TGT_CROP_BOUNDS:
  555. if (!dev->has_crop_out)
  556. return -EINVAL;
  557. sel->r = vivid_max_rect;
  558. break;
  559. case V4L2_SEL_TGT_COMPOSE:
  560. if (!dev->has_compose_out)
  561. return -EINVAL;
  562. sel->r = dev->compose_out;
  563. break;
  564. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  565. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  566. if (!dev->has_compose_out)
  567. return -EINVAL;
  568. sel->r = dev->sink_rect;
  569. break;
  570. default:
  571. return -EINVAL;
  572. }
  573. return 0;
  574. }
  575. int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
  576. {
  577. struct vivid_dev *dev = video_drvdata(file);
  578. struct v4l2_rect *crop = &dev->crop_out;
  579. struct v4l2_rect *compose = &dev->compose_out;
  580. unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
  581. int ret;
  582. if (!dev->has_crop_out && !dev->has_compose_out)
  583. return -ENOTTY;
  584. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  585. return -EINVAL;
  586. switch (s->target) {
  587. case V4L2_SEL_TGT_CROP:
  588. if (!dev->has_crop_out)
  589. return -EINVAL;
  590. ret = vivid_vid_adjust_sel(s->flags, &s->r);
  591. if (ret)
  592. return ret;
  593. v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
  594. v4l2_rect_set_max_size(&s->r, &dev->fmt_out_rect);
  595. if (dev->has_scaler_out) {
  596. struct v4l2_rect max_rect = {
  597. 0, 0,
  598. dev->sink_rect.width * MAX_ZOOM,
  599. (dev->sink_rect.height / factor) * MAX_ZOOM
  600. };
  601. v4l2_rect_set_max_size(&s->r, &max_rect);
  602. if (dev->has_compose_out) {
  603. struct v4l2_rect min_rect = {
  604. 0, 0,
  605. s->r.width / MAX_ZOOM,
  606. (s->r.height * factor) / MAX_ZOOM
  607. };
  608. struct v4l2_rect max_rect = {
  609. 0, 0,
  610. s->r.width * MAX_ZOOM,
  611. (s->r.height * factor) * MAX_ZOOM
  612. };
  613. v4l2_rect_set_min_size(compose, &min_rect);
  614. v4l2_rect_set_max_size(compose, &max_rect);
  615. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  616. }
  617. } else if (dev->has_compose_out) {
  618. s->r.top *= factor;
  619. s->r.height *= factor;
  620. v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
  621. v4l2_rect_set_size_to(compose, &s->r);
  622. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  623. s->r.top /= factor;
  624. s->r.height /= factor;
  625. } else {
  626. v4l2_rect_set_size_to(&s->r, &dev->sink_rect);
  627. s->r.height /= factor;
  628. }
  629. v4l2_rect_map_inside(&s->r, &dev->fmt_out_rect);
  630. *crop = s->r;
  631. break;
  632. case V4L2_SEL_TGT_COMPOSE:
  633. if (!dev->has_compose_out)
  634. return -EINVAL;
  635. ret = vivid_vid_adjust_sel(s->flags, &s->r);
  636. if (ret)
  637. return ret;
  638. v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
  639. v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
  640. v4l2_rect_map_inside(&s->r, &dev->compose_bounds_out);
  641. s->r.top /= factor;
  642. s->r.height /= factor;
  643. if (dev->has_scaler_out) {
  644. struct v4l2_rect fmt = dev->fmt_out_rect;
  645. struct v4l2_rect max_rect = {
  646. 0, 0,
  647. s->r.width * MAX_ZOOM,
  648. s->r.height * MAX_ZOOM
  649. };
  650. struct v4l2_rect min_rect = {
  651. 0, 0,
  652. s->r.width / MAX_ZOOM,
  653. s->r.height / MAX_ZOOM
  654. };
  655. v4l2_rect_set_min_size(&fmt, &min_rect);
  656. if (!dev->has_crop_out)
  657. v4l2_rect_set_max_size(&fmt, &max_rect);
  658. if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
  659. vb2_is_busy(&dev->vb_vid_out_q))
  660. return -EBUSY;
  661. if (dev->has_crop_out) {
  662. v4l2_rect_set_min_size(crop, &min_rect);
  663. v4l2_rect_set_max_size(crop, &max_rect);
  664. }
  665. dev->fmt_out_rect = fmt;
  666. } else if (dev->has_crop_out) {
  667. struct v4l2_rect fmt = dev->fmt_out_rect;
  668. v4l2_rect_set_min_size(&fmt, &s->r);
  669. if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
  670. vb2_is_busy(&dev->vb_vid_out_q))
  671. return -EBUSY;
  672. dev->fmt_out_rect = fmt;
  673. v4l2_rect_set_size_to(crop, &s->r);
  674. v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
  675. } else {
  676. if (!v4l2_rect_same_size(&s->r, &dev->fmt_out_rect) &&
  677. vb2_is_busy(&dev->vb_vid_out_q))
  678. return -EBUSY;
  679. v4l2_rect_set_size_to(&dev->fmt_out_rect, &s->r);
  680. v4l2_rect_set_size_to(crop, &s->r);
  681. crop->height /= factor;
  682. v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
  683. }
  684. s->r.top *= factor;
  685. s->r.height *= factor;
  686. if (dev->bitmap_out && (compose->width != s->r.width ||
  687. compose->height != s->r.height)) {
  688. kfree(dev->bitmap_out);
  689. dev->bitmap_out = NULL;
  690. }
  691. *compose = s->r;
  692. break;
  693. default:
  694. return -EINVAL;
  695. }
  696. return 0;
  697. }
  698. int vivid_vid_out_cropcap(struct file *file, void *priv,
  699. struct v4l2_cropcap *cap)
  700. {
  701. struct vivid_dev *dev = video_drvdata(file);
  702. if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  703. return -EINVAL;
  704. switch (vivid_get_pixel_aspect(dev)) {
  705. case TPG_PIXEL_ASPECT_NTSC:
  706. cap->pixelaspect.numerator = 11;
  707. cap->pixelaspect.denominator = 10;
  708. break;
  709. case TPG_PIXEL_ASPECT_PAL:
  710. cap->pixelaspect.numerator = 54;
  711. cap->pixelaspect.denominator = 59;
  712. break;
  713. case TPG_PIXEL_ASPECT_SQUARE:
  714. cap->pixelaspect.numerator = 1;
  715. cap->pixelaspect.denominator = 1;
  716. break;
  717. }
  718. return 0;
  719. }
  720. int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
  721. struct v4l2_format *f)
  722. {
  723. struct vivid_dev *dev = video_drvdata(file);
  724. const struct v4l2_rect *compose = &dev->compose_out;
  725. struct v4l2_window *win = &f->fmt.win;
  726. unsigned clipcount = win->clipcount;
  727. if (!dev->has_fb)
  728. return -EINVAL;
  729. win->w.top = dev->overlay_out_top;
  730. win->w.left = dev->overlay_out_left;
  731. win->w.width = compose->width;
  732. win->w.height = compose->height;
  733. win->clipcount = dev->clipcount_out;
  734. win->field = V4L2_FIELD_ANY;
  735. win->chromakey = dev->chromakey_out;
  736. win->global_alpha = dev->global_alpha_out;
  737. if (clipcount > dev->clipcount_out)
  738. clipcount = dev->clipcount_out;
  739. if (dev->bitmap_out == NULL)
  740. win->bitmap = NULL;
  741. else if (win->bitmap) {
  742. if (copy_to_user(win->bitmap, dev->bitmap_out,
  743. ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
  744. return -EFAULT;
  745. }
  746. if (clipcount && win->clips) {
  747. if (copy_to_user(win->clips, dev->clips_out,
  748. clipcount * sizeof(dev->clips_out[0])))
  749. return -EFAULT;
  750. }
  751. return 0;
  752. }
  753. int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
  754. struct v4l2_format *f)
  755. {
  756. struct vivid_dev *dev = video_drvdata(file);
  757. const struct v4l2_rect *compose = &dev->compose_out;
  758. struct v4l2_window *win = &f->fmt.win;
  759. int i, j;
  760. if (!dev->has_fb)
  761. return -EINVAL;
  762. win->w.left = clamp_t(int, win->w.left,
  763. -dev->display_width, dev->display_width);
  764. win->w.top = clamp_t(int, win->w.top,
  765. -dev->display_height, dev->display_height);
  766. win->w.width = compose->width;
  767. win->w.height = compose->height;
  768. /*
  769. * It makes no sense for an OSD to overlay only top or bottom fields,
  770. * so always set this to ANY.
  771. */
  772. win->field = V4L2_FIELD_ANY;
  773. if (win->clipcount && !win->clips)
  774. win->clipcount = 0;
  775. if (win->clipcount > MAX_CLIPS)
  776. win->clipcount = MAX_CLIPS;
  777. if (win->clipcount) {
  778. if (copy_from_user(dev->try_clips_out, win->clips,
  779. win->clipcount * sizeof(dev->clips_out[0])))
  780. return -EFAULT;
  781. for (i = 0; i < win->clipcount; i++) {
  782. struct v4l2_rect *r = &dev->try_clips_out[i].c;
  783. r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
  784. r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
  785. r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
  786. r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
  787. }
  788. /*
  789. * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
  790. * number and it's typically a one-time deal.
  791. */
  792. for (i = 0; i < win->clipcount - 1; i++) {
  793. struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
  794. for (j = i + 1; j < win->clipcount; j++) {
  795. struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
  796. if (v4l2_rect_overlap(r1, r2))
  797. return -EINVAL;
  798. }
  799. }
  800. if (copy_to_user(win->clips, dev->try_clips_out,
  801. win->clipcount * sizeof(dev->clips_out[0])))
  802. return -EFAULT;
  803. }
  804. return 0;
  805. }
  806. int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
  807. struct v4l2_format *f)
  808. {
  809. struct vivid_dev *dev = video_drvdata(file);
  810. const struct v4l2_rect *compose = &dev->compose_out;
  811. struct v4l2_window *win = &f->fmt.win;
  812. int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
  813. unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
  814. unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
  815. void *new_bitmap = NULL;
  816. if (ret)
  817. return ret;
  818. if (win->bitmap) {
  819. new_bitmap = memdup_user(win->bitmap, bitmap_size);
  820. if (IS_ERR(new_bitmap))
  821. return PTR_ERR(new_bitmap);
  822. }
  823. dev->overlay_out_top = win->w.top;
  824. dev->overlay_out_left = win->w.left;
  825. kfree(dev->bitmap_out);
  826. dev->bitmap_out = new_bitmap;
  827. dev->clipcount_out = win->clipcount;
  828. if (dev->clipcount_out)
  829. memcpy(dev->clips_out, dev->try_clips_out, clips_size);
  830. dev->chromakey_out = win->chromakey;
  831. dev->global_alpha_out = win->global_alpha;
  832. return ret;
  833. }
  834. int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
  835. {
  836. struct vivid_dev *dev = video_drvdata(file);
  837. if (i && !dev->fmt_out->can_do_overlay) {
  838. dprintk(dev, 1, "unsupported output format for output overlay\n");
  839. return -EINVAL;
  840. }
  841. dev->overlay_out_enabled = i;
  842. return 0;
  843. }
  844. int vivid_vid_out_g_fbuf(struct file *file, void *fh,
  845. struct v4l2_framebuffer *a)
  846. {
  847. struct vivid_dev *dev = video_drvdata(file);
  848. a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
  849. V4L2_FBUF_CAP_BITMAP_CLIPPING |
  850. V4L2_FBUF_CAP_LIST_CLIPPING |
  851. V4L2_FBUF_CAP_CHROMAKEY |
  852. V4L2_FBUF_CAP_SRC_CHROMAKEY |
  853. V4L2_FBUF_CAP_GLOBAL_ALPHA |
  854. V4L2_FBUF_CAP_LOCAL_ALPHA |
  855. V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
  856. a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
  857. a->base = (void *)dev->video_pbase;
  858. a->fmt.width = dev->display_width;
  859. a->fmt.height = dev->display_height;
  860. if (dev->fb_defined.green.length == 5)
  861. a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
  862. else
  863. a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
  864. a->fmt.bytesperline = dev->display_byte_stride;
  865. a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
  866. a->fmt.field = V4L2_FIELD_NONE;
  867. a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  868. a->fmt.priv = 0;
  869. return 0;
  870. }
  871. int vivid_vid_out_s_fbuf(struct file *file, void *fh,
  872. const struct v4l2_framebuffer *a)
  873. {
  874. struct vivid_dev *dev = video_drvdata(file);
  875. const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
  876. V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  877. const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
  878. V4L2_FBUF_FLAG_LOCAL_ALPHA |
  879. V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
  880. if ((a->flags & chroma_flags) == chroma_flags)
  881. return -EINVAL;
  882. switch (a->flags & alpha_flags) {
  883. case 0:
  884. case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
  885. case V4L2_FBUF_FLAG_LOCAL_ALPHA:
  886. case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
  887. break;
  888. default:
  889. return -EINVAL;
  890. }
  891. dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
  892. dev->fbuf_out_flags |= a->flags & (chroma_flags | alpha_flags);
  893. return 0;
  894. }
  895. static const struct v4l2_audioout vivid_audio_outputs[] = {
  896. { 0, "Line-Out 1" },
  897. { 1, "Line-Out 2" },
  898. };
  899. int vidioc_enum_output(struct file *file, void *priv,
  900. struct v4l2_output *out)
  901. {
  902. struct vivid_dev *dev = video_drvdata(file);
  903. if (out->index >= dev->num_outputs)
  904. return -EINVAL;
  905. out->type = V4L2_OUTPUT_TYPE_ANALOG;
  906. switch (dev->output_type[out->index]) {
  907. case SVID:
  908. snprintf(out->name, sizeof(out->name), "S-Video %u",
  909. dev->output_name_counter[out->index]);
  910. out->std = V4L2_STD_ALL;
  911. if (dev->has_audio_outputs)
  912. out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
  913. out->capabilities = V4L2_OUT_CAP_STD;
  914. break;
  915. case HDMI:
  916. snprintf(out->name, sizeof(out->name), "HDMI %u",
  917. dev->output_name_counter[out->index]);
  918. out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
  919. break;
  920. }
  921. return 0;
  922. }
  923. int vidioc_g_output(struct file *file, void *priv, unsigned *o)
  924. {
  925. struct vivid_dev *dev = video_drvdata(file);
  926. *o = dev->output;
  927. return 0;
  928. }
  929. int vidioc_s_output(struct file *file, void *priv, unsigned o)
  930. {
  931. struct vivid_dev *dev = video_drvdata(file);
  932. if (o >= dev->num_outputs)
  933. return -EINVAL;
  934. if (o == dev->output)
  935. return 0;
  936. if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
  937. return -EBUSY;
  938. dev->output = o;
  939. dev->tv_audio_output = 0;
  940. if (dev->output_type[o] == SVID)
  941. dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
  942. else
  943. dev->vid_out_dev.tvnorms = 0;
  944. dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
  945. vivid_update_format_out(dev);
  946. return 0;
  947. }
  948. int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
  949. {
  950. if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
  951. return -EINVAL;
  952. *vout = vivid_audio_outputs[vout->index];
  953. return 0;
  954. }
  955. int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
  956. {
  957. struct vivid_dev *dev = video_drvdata(file);
  958. if (!vivid_is_svid_out(dev))
  959. return -EINVAL;
  960. *vout = vivid_audio_outputs[dev->tv_audio_output];
  961. return 0;
  962. }
  963. int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
  964. {
  965. struct vivid_dev *dev = video_drvdata(file);
  966. if (!vivid_is_svid_out(dev))
  967. return -EINVAL;
  968. if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
  969. return -EINVAL;
  970. dev->tv_audio_output = vout->index;
  971. return 0;
  972. }
  973. int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
  974. {
  975. struct vivid_dev *dev = video_drvdata(file);
  976. if (!vivid_is_svid_out(dev))
  977. return -ENODATA;
  978. if (dev->std_out == id)
  979. return 0;
  980. if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
  981. return -EBUSY;
  982. dev->std_out = id;
  983. vivid_update_format_out(dev);
  984. return 0;
  985. }
  986. static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
  987. {
  988. struct v4l2_bt_timings *bt = &timings->bt;
  989. if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
  990. v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
  991. return true;
  992. return false;
  993. }
  994. int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
  995. struct v4l2_dv_timings *timings)
  996. {
  997. struct vivid_dev *dev = video_drvdata(file);
  998. if (!vivid_is_hdmi_out(dev))
  999. return -ENODATA;
  1000. if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
  1001. 0, NULL, NULL) &&
  1002. !valid_cvt_gtf_timings(timings))
  1003. return -EINVAL;
  1004. if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0, true))
  1005. return 0;
  1006. if (vb2_is_busy(&dev->vb_vid_out_q))
  1007. return -EBUSY;
  1008. dev->dv_timings_out = *timings;
  1009. vivid_update_format_out(dev);
  1010. return 0;
  1011. }
  1012. int vivid_vid_out_g_parm(struct file *file, void *priv,
  1013. struct v4l2_streamparm *parm)
  1014. {
  1015. struct vivid_dev *dev = video_drvdata(file);
  1016. if (parm->type != (dev->multiplanar ?
  1017. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
  1018. V4L2_BUF_TYPE_VIDEO_OUTPUT))
  1019. return -EINVAL;
  1020. parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  1021. parm->parm.output.timeperframe = dev->timeperframe_vid_out;
  1022. parm->parm.output.writebuffers = 1;
  1023. return 0;
  1024. }
  1025. int vidioc_subscribe_event(struct v4l2_fh *fh,
  1026. const struct v4l2_event_subscription *sub)
  1027. {
  1028. switch (sub->type) {
  1029. case V4L2_EVENT_SOURCE_CHANGE:
  1030. if (fh->vdev->vfl_dir == VFL_DIR_RX)
  1031. return v4l2_src_change_event_subscribe(fh, sub);
  1032. break;
  1033. default:
  1034. return v4l2_ctrl_subscribe_event(fh, sub);
  1035. }
  1036. return -EINVAL;
  1037. }