vivid-vid-out.c 33 KB

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