vivid-vid-out.c 32 KB

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