vivid-vid-out.c 33 KB

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