vivid-vid-out.c 32 KB

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