vivid-vid-out.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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. /* Calculate the minimum supported bytesperline value */
  346. bytesperline = (mp->width * fmt->bit_depth[0]) >> 3;
  347. /* Calculate the maximum supported bytesperline value */
  348. max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[0]) >> 3;
  349. mp->num_planes = fmt->buffers;
  350. for (p = 0; p < mp->num_planes; p++) {
  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. memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
  357. }
  358. for (p = fmt->buffers; p < fmt->planes; p++)
  359. pfmt[0].sizeimage += (pfmt[0].bytesperline * fmt->bit_depth[p]) /
  360. (fmt->bit_depth[0] * fmt->vdownsampling[p]);
  361. mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  362. mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  363. mp->quantization = V4L2_QUANTIZATION_DEFAULT;
  364. if (vivid_is_svid_out(dev)) {
  365. mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
  366. } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
  367. mp->colorspace = V4L2_COLORSPACE_SRGB;
  368. if (dev->dvi_d_out)
  369. mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
  370. } else if (bt->width == 720 && bt->height <= 576) {
  371. mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
  372. } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
  373. mp->colorspace != V4L2_COLORSPACE_REC709 &&
  374. mp->colorspace != V4L2_COLORSPACE_ADOBERGB &&
  375. mp->colorspace != V4L2_COLORSPACE_BT2020 &&
  376. mp->colorspace != V4L2_COLORSPACE_SRGB) {
  377. mp->colorspace = V4L2_COLORSPACE_REC709;
  378. }
  379. memset(mp->reserved, 0, sizeof(mp->reserved));
  380. return 0;
  381. }
  382. int vivid_s_fmt_vid_out(struct file *file, void *priv,
  383. struct v4l2_format *f)
  384. {
  385. struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
  386. struct vivid_dev *dev = video_drvdata(file);
  387. struct v4l2_rect *crop = &dev->crop_out;
  388. struct v4l2_rect *compose = &dev->compose_out;
  389. struct vb2_queue *q = &dev->vb_vid_out_q;
  390. int ret = vivid_try_fmt_vid_out(file, priv, f);
  391. unsigned factor = 1;
  392. unsigned p;
  393. if (ret < 0)
  394. return ret;
  395. if (vb2_is_busy(q) &&
  396. (vivid_is_svid_out(dev) ||
  397. mp->width != dev->fmt_out_rect.width ||
  398. mp->height != dev->fmt_out_rect.height ||
  399. mp->pixelformat != dev->fmt_out->fourcc ||
  400. mp->field != dev->field_out)) {
  401. dprintk(dev, 1, "%s device busy\n", __func__);
  402. return -EBUSY;
  403. }
  404. /*
  405. * Allow for changing the colorspace on the fly. Useful for testing
  406. * purposes, and it is something that HDMI transmitters are able
  407. * to do.
  408. */
  409. if (vb2_is_busy(q))
  410. goto set_colorspace;
  411. dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
  412. if (V4L2_FIELD_HAS_T_OR_B(mp->field))
  413. factor = 2;
  414. if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
  415. struct v4l2_rect r = { 0, 0, mp->width, mp->height };
  416. if (dev->has_scaler_out) {
  417. if (dev->has_crop_out)
  418. v4l2_rect_map_inside(crop, &r);
  419. else
  420. *crop = r;
  421. if (dev->has_compose_out && !dev->has_crop_out) {
  422. struct v4l2_rect min_r = {
  423. 0, 0,
  424. r.width / MAX_ZOOM,
  425. factor * r.height / MAX_ZOOM
  426. };
  427. struct v4l2_rect max_r = {
  428. 0, 0,
  429. r.width * MAX_ZOOM,
  430. factor * r.height * MAX_ZOOM
  431. };
  432. v4l2_rect_set_min_size(compose, &min_r);
  433. v4l2_rect_set_max_size(compose, &max_r);
  434. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  435. } else if (dev->has_compose_out) {
  436. struct v4l2_rect min_r = {
  437. 0, 0,
  438. crop->width / MAX_ZOOM,
  439. factor * crop->height / MAX_ZOOM
  440. };
  441. struct v4l2_rect max_r = {
  442. 0, 0,
  443. crop->width * MAX_ZOOM,
  444. factor * crop->height * MAX_ZOOM
  445. };
  446. v4l2_rect_set_min_size(compose, &min_r);
  447. v4l2_rect_set_max_size(compose, &max_r);
  448. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  449. }
  450. } else if (dev->has_compose_out && !dev->has_crop_out) {
  451. v4l2_rect_set_size_to(crop, &r);
  452. r.height *= factor;
  453. v4l2_rect_set_size_to(compose, &r);
  454. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  455. } else if (!dev->has_compose_out) {
  456. v4l2_rect_map_inside(crop, &r);
  457. r.height /= factor;
  458. v4l2_rect_set_size_to(compose, &r);
  459. } else {
  460. r.height *= factor;
  461. v4l2_rect_set_max_size(compose, &r);
  462. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  463. crop->top *= factor;
  464. crop->height *= factor;
  465. v4l2_rect_set_size_to(crop, compose);
  466. v4l2_rect_map_inside(crop, &r);
  467. crop->top /= factor;
  468. crop->height /= factor;
  469. }
  470. } else {
  471. struct v4l2_rect r = { 0, 0, mp->width, mp->height };
  472. v4l2_rect_set_size_to(crop, &r);
  473. r.height /= factor;
  474. v4l2_rect_set_size_to(compose, &r);
  475. }
  476. dev->fmt_out_rect.width = mp->width;
  477. dev->fmt_out_rect.height = mp->height;
  478. for (p = 0; p < mp->num_planes; p++)
  479. dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
  480. for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
  481. dev->bytesperline_out[p] =
  482. (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
  483. dev->fmt_out->bit_depth[0];
  484. dev->field_out = mp->field;
  485. if (vivid_is_svid_out(dev))
  486. dev->tv_field_out = mp->field;
  487. set_colorspace:
  488. dev->colorspace_out = mp->colorspace;
  489. dev->xfer_func_out = mp->xfer_func;
  490. dev->ycbcr_enc_out = mp->ycbcr_enc;
  491. dev->quantization_out = mp->quantization;
  492. if (dev->loop_video) {
  493. vivid_send_source_change(dev, SVID);
  494. vivid_send_source_change(dev, HDMI);
  495. }
  496. return 0;
  497. }
  498. int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
  499. struct v4l2_format *f)
  500. {
  501. struct vivid_dev *dev = video_drvdata(file);
  502. if (!dev->multiplanar)
  503. return -ENOTTY;
  504. return vivid_g_fmt_vid_out(file, priv, f);
  505. }
  506. int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
  507. struct v4l2_format *f)
  508. {
  509. struct vivid_dev *dev = video_drvdata(file);
  510. if (!dev->multiplanar)
  511. return -ENOTTY;
  512. return vivid_try_fmt_vid_out(file, priv, f);
  513. }
  514. int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
  515. struct v4l2_format *f)
  516. {
  517. struct vivid_dev *dev = video_drvdata(file);
  518. if (!dev->multiplanar)
  519. return -ENOTTY;
  520. return vivid_s_fmt_vid_out(file, priv, f);
  521. }
  522. int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  523. struct v4l2_format *f)
  524. {
  525. struct vivid_dev *dev = video_drvdata(file);
  526. if (dev->multiplanar)
  527. return -ENOTTY;
  528. return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
  529. }
  530. int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  531. struct v4l2_format *f)
  532. {
  533. struct vivid_dev *dev = video_drvdata(file);
  534. if (dev->multiplanar)
  535. return -ENOTTY;
  536. return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
  537. }
  538. int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  539. struct v4l2_format *f)
  540. {
  541. struct vivid_dev *dev = video_drvdata(file);
  542. if (dev->multiplanar)
  543. return -ENOTTY;
  544. return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
  545. }
  546. int vivid_vid_out_g_selection(struct file *file, void *priv,
  547. struct v4l2_selection *sel)
  548. {
  549. struct vivid_dev *dev = video_drvdata(file);
  550. if (!dev->has_crop_out && !dev->has_compose_out)
  551. return -ENOTTY;
  552. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  553. return -EINVAL;
  554. sel->r.left = sel->r.top = 0;
  555. switch (sel->target) {
  556. case V4L2_SEL_TGT_CROP:
  557. if (!dev->has_crop_out)
  558. return -EINVAL;
  559. sel->r = dev->crop_out;
  560. break;
  561. case V4L2_SEL_TGT_CROP_DEFAULT:
  562. if (!dev->has_crop_out)
  563. return -EINVAL;
  564. sel->r = dev->fmt_out_rect;
  565. break;
  566. case V4L2_SEL_TGT_CROP_BOUNDS:
  567. if (!dev->has_crop_out)
  568. return -EINVAL;
  569. sel->r = vivid_max_rect;
  570. break;
  571. case V4L2_SEL_TGT_COMPOSE:
  572. if (!dev->has_compose_out)
  573. return -EINVAL;
  574. sel->r = dev->compose_out;
  575. break;
  576. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  577. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  578. if (!dev->has_compose_out)
  579. return -EINVAL;
  580. sel->r = dev->sink_rect;
  581. break;
  582. default:
  583. return -EINVAL;
  584. }
  585. return 0;
  586. }
  587. int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
  588. {
  589. struct vivid_dev *dev = video_drvdata(file);
  590. struct v4l2_rect *crop = &dev->crop_out;
  591. struct v4l2_rect *compose = &dev->compose_out;
  592. unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
  593. int ret;
  594. if (!dev->has_crop_out && !dev->has_compose_out)
  595. return -ENOTTY;
  596. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  597. return -EINVAL;
  598. switch (s->target) {
  599. case V4L2_SEL_TGT_CROP:
  600. if (!dev->has_crop_out)
  601. return -EINVAL;
  602. ret = vivid_vid_adjust_sel(s->flags, &s->r);
  603. if (ret)
  604. return ret;
  605. v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
  606. v4l2_rect_set_max_size(&s->r, &dev->fmt_out_rect);
  607. if (dev->has_scaler_out) {
  608. struct v4l2_rect max_rect = {
  609. 0, 0,
  610. dev->sink_rect.width * MAX_ZOOM,
  611. (dev->sink_rect.height / factor) * MAX_ZOOM
  612. };
  613. v4l2_rect_set_max_size(&s->r, &max_rect);
  614. if (dev->has_compose_out) {
  615. struct v4l2_rect min_rect = {
  616. 0, 0,
  617. s->r.width / MAX_ZOOM,
  618. (s->r.height * factor) / MAX_ZOOM
  619. };
  620. struct v4l2_rect max_rect = {
  621. 0, 0,
  622. s->r.width * MAX_ZOOM,
  623. (s->r.height * factor) * MAX_ZOOM
  624. };
  625. v4l2_rect_set_min_size(compose, &min_rect);
  626. v4l2_rect_set_max_size(compose, &max_rect);
  627. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  628. }
  629. } else if (dev->has_compose_out) {
  630. s->r.top *= factor;
  631. s->r.height *= factor;
  632. v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
  633. v4l2_rect_set_size_to(compose, &s->r);
  634. v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
  635. s->r.top /= factor;
  636. s->r.height /= factor;
  637. } else {
  638. v4l2_rect_set_size_to(&s->r, &dev->sink_rect);
  639. s->r.height /= factor;
  640. }
  641. v4l2_rect_map_inside(&s->r, &dev->fmt_out_rect);
  642. *crop = s->r;
  643. break;
  644. case V4L2_SEL_TGT_COMPOSE:
  645. if (!dev->has_compose_out)
  646. return -EINVAL;
  647. ret = vivid_vid_adjust_sel(s->flags, &s->r);
  648. if (ret)
  649. return ret;
  650. v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
  651. v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
  652. v4l2_rect_map_inside(&s->r, &dev->compose_bounds_out);
  653. s->r.top /= factor;
  654. s->r.height /= factor;
  655. if (dev->has_scaler_out) {
  656. struct v4l2_rect fmt = dev->fmt_out_rect;
  657. struct v4l2_rect max_rect = {
  658. 0, 0,
  659. s->r.width * MAX_ZOOM,
  660. s->r.height * MAX_ZOOM
  661. };
  662. struct v4l2_rect min_rect = {
  663. 0, 0,
  664. s->r.width / MAX_ZOOM,
  665. s->r.height / MAX_ZOOM
  666. };
  667. v4l2_rect_set_min_size(&fmt, &min_rect);
  668. if (!dev->has_crop_out)
  669. v4l2_rect_set_max_size(&fmt, &max_rect);
  670. if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
  671. vb2_is_busy(&dev->vb_vid_out_q))
  672. return -EBUSY;
  673. if (dev->has_crop_out) {
  674. v4l2_rect_set_min_size(crop, &min_rect);
  675. v4l2_rect_set_max_size(crop, &max_rect);
  676. }
  677. dev->fmt_out_rect = fmt;
  678. } else if (dev->has_crop_out) {
  679. struct v4l2_rect fmt = dev->fmt_out_rect;
  680. v4l2_rect_set_min_size(&fmt, &s->r);
  681. if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
  682. vb2_is_busy(&dev->vb_vid_out_q))
  683. return -EBUSY;
  684. dev->fmt_out_rect = fmt;
  685. v4l2_rect_set_size_to(crop, &s->r);
  686. v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
  687. } else {
  688. if (!v4l2_rect_same_size(&s->r, &dev->fmt_out_rect) &&
  689. vb2_is_busy(&dev->vb_vid_out_q))
  690. return -EBUSY;
  691. v4l2_rect_set_size_to(&dev->fmt_out_rect, &s->r);
  692. v4l2_rect_set_size_to(crop, &s->r);
  693. crop->height /= factor;
  694. v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
  695. }
  696. s->r.top *= factor;
  697. s->r.height *= factor;
  698. if (dev->bitmap_out && (compose->width != s->r.width ||
  699. compose->height != s->r.height)) {
  700. kfree(dev->bitmap_out);
  701. dev->bitmap_out = NULL;
  702. }
  703. *compose = s->r;
  704. break;
  705. default:
  706. return -EINVAL;
  707. }
  708. return 0;
  709. }
  710. int vivid_vid_out_cropcap(struct file *file, void *priv,
  711. struct v4l2_cropcap *cap)
  712. {
  713. struct vivid_dev *dev = video_drvdata(file);
  714. if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  715. return -EINVAL;
  716. switch (vivid_get_pixel_aspect(dev)) {
  717. case TPG_PIXEL_ASPECT_NTSC:
  718. cap->pixelaspect.numerator = 11;
  719. cap->pixelaspect.denominator = 10;
  720. break;
  721. case TPG_PIXEL_ASPECT_PAL:
  722. cap->pixelaspect.numerator = 54;
  723. cap->pixelaspect.denominator = 59;
  724. break;
  725. case TPG_PIXEL_ASPECT_SQUARE:
  726. cap->pixelaspect.numerator = 1;
  727. cap->pixelaspect.denominator = 1;
  728. break;
  729. }
  730. return 0;
  731. }
  732. int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
  733. struct v4l2_format *f)
  734. {
  735. struct vivid_dev *dev = video_drvdata(file);
  736. const struct v4l2_rect *compose = &dev->compose_out;
  737. struct v4l2_window *win = &f->fmt.win;
  738. unsigned clipcount = win->clipcount;
  739. if (!dev->has_fb)
  740. return -EINVAL;
  741. win->w.top = dev->overlay_out_top;
  742. win->w.left = dev->overlay_out_left;
  743. win->w.width = compose->width;
  744. win->w.height = compose->height;
  745. win->clipcount = dev->clipcount_out;
  746. win->field = V4L2_FIELD_ANY;
  747. win->chromakey = dev->chromakey_out;
  748. win->global_alpha = dev->global_alpha_out;
  749. if (clipcount > dev->clipcount_out)
  750. clipcount = dev->clipcount_out;
  751. if (dev->bitmap_out == NULL)
  752. win->bitmap = NULL;
  753. else if (win->bitmap) {
  754. if (copy_to_user(win->bitmap, dev->bitmap_out,
  755. ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
  756. return -EFAULT;
  757. }
  758. if (clipcount && win->clips) {
  759. if (copy_to_user(win->clips, dev->clips_out,
  760. clipcount * sizeof(dev->clips_out[0])))
  761. return -EFAULT;
  762. }
  763. return 0;
  764. }
  765. int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
  766. struct v4l2_format *f)
  767. {
  768. struct vivid_dev *dev = video_drvdata(file);
  769. const struct v4l2_rect *compose = &dev->compose_out;
  770. struct v4l2_window *win = &f->fmt.win;
  771. int i, j;
  772. if (!dev->has_fb)
  773. return -EINVAL;
  774. win->w.left = clamp_t(int, win->w.left,
  775. -dev->display_width, dev->display_width);
  776. win->w.top = clamp_t(int, win->w.top,
  777. -dev->display_height, dev->display_height);
  778. win->w.width = compose->width;
  779. win->w.height = compose->height;
  780. /*
  781. * It makes no sense for an OSD to overlay only top or bottom fields,
  782. * so always set this to ANY.
  783. */
  784. win->field = V4L2_FIELD_ANY;
  785. if (win->clipcount && !win->clips)
  786. win->clipcount = 0;
  787. if (win->clipcount > MAX_CLIPS)
  788. win->clipcount = MAX_CLIPS;
  789. if (win->clipcount) {
  790. if (copy_from_user(dev->try_clips_out, win->clips,
  791. win->clipcount * sizeof(dev->clips_out[0])))
  792. return -EFAULT;
  793. for (i = 0; i < win->clipcount; i++) {
  794. struct v4l2_rect *r = &dev->try_clips_out[i].c;
  795. r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
  796. r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
  797. r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
  798. r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
  799. }
  800. /*
  801. * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
  802. * number and it's typically a one-time deal.
  803. */
  804. for (i = 0; i < win->clipcount - 1; i++) {
  805. struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
  806. for (j = i + 1; j < win->clipcount; j++) {
  807. struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
  808. if (v4l2_rect_overlap(r1, r2))
  809. return -EINVAL;
  810. }
  811. }
  812. if (copy_to_user(win->clips, dev->try_clips_out,
  813. win->clipcount * sizeof(dev->clips_out[0])))
  814. return -EFAULT;
  815. }
  816. return 0;
  817. }
  818. int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
  819. struct v4l2_format *f)
  820. {
  821. struct vivid_dev *dev = video_drvdata(file);
  822. const struct v4l2_rect *compose = &dev->compose_out;
  823. struct v4l2_window *win = &f->fmt.win;
  824. int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
  825. unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
  826. unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
  827. void *new_bitmap = NULL;
  828. if (ret)
  829. return ret;
  830. if (win->bitmap) {
  831. new_bitmap = memdup_user(win->bitmap, bitmap_size);
  832. if (IS_ERR(new_bitmap))
  833. return PTR_ERR(new_bitmap);
  834. }
  835. dev->overlay_out_top = win->w.top;
  836. dev->overlay_out_left = win->w.left;
  837. kfree(dev->bitmap_out);
  838. dev->bitmap_out = new_bitmap;
  839. dev->clipcount_out = win->clipcount;
  840. if (dev->clipcount_out)
  841. memcpy(dev->clips_out, dev->try_clips_out, clips_size);
  842. dev->chromakey_out = win->chromakey;
  843. dev->global_alpha_out = win->global_alpha;
  844. return ret;
  845. }
  846. int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
  847. {
  848. struct vivid_dev *dev = video_drvdata(file);
  849. if (i && !dev->fmt_out->can_do_overlay) {
  850. dprintk(dev, 1, "unsupported output format for output overlay\n");
  851. return -EINVAL;
  852. }
  853. dev->overlay_out_enabled = i;
  854. return 0;
  855. }
  856. int vivid_vid_out_g_fbuf(struct file *file, void *fh,
  857. struct v4l2_framebuffer *a)
  858. {
  859. struct vivid_dev *dev = video_drvdata(file);
  860. a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
  861. V4L2_FBUF_CAP_BITMAP_CLIPPING |
  862. V4L2_FBUF_CAP_LIST_CLIPPING |
  863. V4L2_FBUF_CAP_CHROMAKEY |
  864. V4L2_FBUF_CAP_SRC_CHROMAKEY |
  865. V4L2_FBUF_CAP_GLOBAL_ALPHA |
  866. V4L2_FBUF_CAP_LOCAL_ALPHA |
  867. V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
  868. a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
  869. a->base = (void *)dev->video_pbase;
  870. a->fmt.width = dev->display_width;
  871. a->fmt.height = dev->display_height;
  872. if (dev->fb_defined.green.length == 5)
  873. a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
  874. else
  875. a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
  876. a->fmt.bytesperline = dev->display_byte_stride;
  877. a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
  878. a->fmt.field = V4L2_FIELD_NONE;
  879. a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  880. a->fmt.priv = 0;
  881. return 0;
  882. }
  883. int vivid_vid_out_s_fbuf(struct file *file, void *fh,
  884. const struct v4l2_framebuffer *a)
  885. {
  886. struct vivid_dev *dev = video_drvdata(file);
  887. const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
  888. V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  889. const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
  890. V4L2_FBUF_FLAG_LOCAL_ALPHA |
  891. V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
  892. if ((a->flags & chroma_flags) == chroma_flags)
  893. return -EINVAL;
  894. switch (a->flags & alpha_flags) {
  895. case 0:
  896. case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
  897. case V4L2_FBUF_FLAG_LOCAL_ALPHA:
  898. case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
  899. break;
  900. default:
  901. return -EINVAL;
  902. }
  903. dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
  904. dev->fbuf_out_flags = a->flags & (chroma_flags | alpha_flags);
  905. return 0;
  906. }
  907. static const struct v4l2_audioout vivid_audio_outputs[] = {
  908. { 0, "Line-Out 1" },
  909. { 1, "Line-Out 2" },
  910. };
  911. int vidioc_enum_output(struct file *file, void *priv,
  912. struct v4l2_output *out)
  913. {
  914. struct vivid_dev *dev = video_drvdata(file);
  915. if (out->index >= dev->num_outputs)
  916. return -EINVAL;
  917. out->type = V4L2_OUTPUT_TYPE_ANALOG;
  918. switch (dev->output_type[out->index]) {
  919. case SVID:
  920. snprintf(out->name, sizeof(out->name), "S-Video %u",
  921. dev->output_name_counter[out->index]);
  922. out->std = V4L2_STD_ALL;
  923. if (dev->has_audio_outputs)
  924. out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
  925. out->capabilities = V4L2_OUT_CAP_STD;
  926. break;
  927. case HDMI:
  928. snprintf(out->name, sizeof(out->name), "HDMI %u",
  929. dev->output_name_counter[out->index]);
  930. out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
  931. break;
  932. }
  933. return 0;
  934. }
  935. int vidioc_g_output(struct file *file, void *priv, unsigned *o)
  936. {
  937. struct vivid_dev *dev = video_drvdata(file);
  938. *o = dev->output;
  939. return 0;
  940. }
  941. int vidioc_s_output(struct file *file, void *priv, unsigned o)
  942. {
  943. struct vivid_dev *dev = video_drvdata(file);
  944. if (o >= dev->num_outputs)
  945. return -EINVAL;
  946. if (o == dev->output)
  947. return 0;
  948. if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
  949. return -EBUSY;
  950. dev->output = o;
  951. dev->tv_audio_output = 0;
  952. if (dev->output_type[o] == SVID)
  953. dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
  954. else
  955. dev->vid_out_dev.tvnorms = 0;
  956. dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
  957. vivid_update_format_out(dev);
  958. return 0;
  959. }
  960. int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
  961. {
  962. if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
  963. return -EINVAL;
  964. *vout = vivid_audio_outputs[vout->index];
  965. return 0;
  966. }
  967. int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
  968. {
  969. struct vivid_dev *dev = video_drvdata(file);
  970. if (!vivid_is_svid_out(dev))
  971. return -EINVAL;
  972. *vout = vivid_audio_outputs[dev->tv_audio_output];
  973. return 0;
  974. }
  975. int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
  976. {
  977. struct vivid_dev *dev = video_drvdata(file);
  978. if (!vivid_is_svid_out(dev))
  979. return -EINVAL;
  980. if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
  981. return -EINVAL;
  982. dev->tv_audio_output = vout->index;
  983. return 0;
  984. }
  985. int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
  986. {
  987. struct vivid_dev *dev = video_drvdata(file);
  988. if (!vivid_is_svid_out(dev))
  989. return -ENODATA;
  990. if (dev->std_out == id)
  991. return 0;
  992. if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
  993. return -EBUSY;
  994. dev->std_out = id;
  995. vivid_update_format_out(dev);
  996. return 0;
  997. }
  998. static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
  999. {
  1000. struct v4l2_bt_timings *bt = &timings->bt;
  1001. if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
  1002. v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
  1003. return true;
  1004. return false;
  1005. }
  1006. int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
  1007. struct v4l2_dv_timings *timings)
  1008. {
  1009. struct vivid_dev *dev = video_drvdata(file);
  1010. if (!vivid_is_hdmi_out(dev))
  1011. return -ENODATA;
  1012. if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
  1013. 0, NULL, NULL) &&
  1014. !valid_cvt_gtf_timings(timings))
  1015. return -EINVAL;
  1016. if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0, true))
  1017. return 0;
  1018. if (vb2_is_busy(&dev->vb_vid_out_q))
  1019. return -EBUSY;
  1020. dev->dv_timings_out = *timings;
  1021. vivid_update_format_out(dev);
  1022. return 0;
  1023. }
  1024. int vivid_vid_out_g_parm(struct file *file, void *priv,
  1025. struct v4l2_streamparm *parm)
  1026. {
  1027. struct vivid_dev *dev = video_drvdata(file);
  1028. if (parm->type != (dev->multiplanar ?
  1029. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
  1030. V4L2_BUF_TYPE_VIDEO_OUTPUT))
  1031. return -EINVAL;
  1032. parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  1033. parm->parm.output.timeperframe = dev->timeperframe_vid_out;
  1034. parm->parm.output.writebuffers = 1;
  1035. return 0;
  1036. }
  1037. int vidioc_subscribe_event(struct v4l2_fh *fh,
  1038. const struct v4l2_event_subscription *sub)
  1039. {
  1040. switch (sub->type) {
  1041. case V4L2_EVENT_CTRL:
  1042. return v4l2_ctrl_subscribe_event(fh, sub);
  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. break;
  1049. }
  1050. return -EINVAL;
  1051. }