vivid-vid-cap.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  1. /*
  2. * vivid-vid-cap.c - video capture 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/vmalloc.h>
  23. #include <linux/videodev2.h>
  24. #include <linux/v4l2-dv-timings.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-event.h>
  27. #include <media/v4l2-dv-timings.h>
  28. #include "vivid-core.h"
  29. #include "vivid-vid-common.h"
  30. #include "vivid-kthread-cap.h"
  31. #include "vivid-vid-cap.h"
  32. /* timeperframe: min/max and default */
  33. static const struct v4l2_fract
  34. tpf_min = {.numerator = 1, .denominator = FPS_MAX},
  35. tpf_max = {.numerator = FPS_MAX, .denominator = 1},
  36. tpf_default = {.numerator = 1, .denominator = 30};
  37. static const struct vivid_fmt formats_ovl[] = {
  38. {
  39. .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
  40. .vdownsampling = { 1 },
  41. .bit_depth = { 16 },
  42. .planes = 1,
  43. .buffers = 1,
  44. },
  45. {
  46. .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
  47. .vdownsampling = { 1 },
  48. .bit_depth = { 16 },
  49. .planes = 1,
  50. .buffers = 1,
  51. },
  52. {
  53. .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
  54. .vdownsampling = { 1 },
  55. .bit_depth = { 16 },
  56. .planes = 1,
  57. .buffers = 1,
  58. },
  59. };
  60. /* The number of discrete webcam framesizes */
  61. #define VIVID_WEBCAM_SIZES 4
  62. /* The number of discrete webcam frameintervals */
  63. #define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
  64. /* Sizes must be in increasing order */
  65. static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
  66. { 320, 180 },
  67. { 640, 360 },
  68. { 1280, 720 },
  69. { 1920, 1080 },
  70. };
  71. /*
  72. * Intervals must be in increasing order and there must be twice as many
  73. * elements in this array as there are in webcam_sizes.
  74. */
  75. static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
  76. { 1, 2 },
  77. { 1, 5 },
  78. { 1, 10 },
  79. { 1, 15 },
  80. { 1, 25 },
  81. { 1, 30 },
  82. { 1, 50 },
  83. { 1, 60 },
  84. };
  85. static const struct v4l2_discrete_probe webcam_probe = {
  86. webcam_sizes,
  87. VIVID_WEBCAM_SIZES
  88. };
  89. static int vid_cap_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  90. unsigned *nbuffers, unsigned *nplanes,
  91. unsigned sizes[], void *alloc_ctxs[])
  92. {
  93. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  94. unsigned buffers = tpg_g_buffers(&dev->tpg);
  95. unsigned h = dev->fmt_cap_rect.height;
  96. unsigned p;
  97. if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
  98. /*
  99. * You cannot use read() with FIELD_ALTERNATE since the field
  100. * information (TOP/BOTTOM) cannot be passed back to the user.
  101. */
  102. if (vb2_fileio_is_active(vq))
  103. return -EINVAL;
  104. }
  105. if (dev->queue_setup_error) {
  106. /*
  107. * Error injection: test what happens if queue_setup() returns
  108. * an error.
  109. */
  110. dev->queue_setup_error = false;
  111. return -EINVAL;
  112. }
  113. if (fmt) {
  114. const struct v4l2_pix_format_mplane *mp;
  115. struct v4l2_format mp_fmt;
  116. const struct vivid_fmt *vfmt;
  117. if (!V4L2_TYPE_IS_MULTIPLANAR(fmt->type)) {
  118. fmt_sp2mp(fmt, &mp_fmt);
  119. fmt = &mp_fmt;
  120. }
  121. mp = &fmt->fmt.pix_mp;
  122. /*
  123. * Check if the number of planes in the specified format match
  124. * the number of buffers in the current format. You can't mix that.
  125. */
  126. if (mp->num_planes != buffers)
  127. return -EINVAL;
  128. vfmt = vivid_get_format(dev, mp->pixelformat);
  129. for (p = 0; p < buffers; p++) {
  130. sizes[p] = mp->plane_fmt[p].sizeimage;
  131. if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h +
  132. vfmt->data_offset[p])
  133. return -EINVAL;
  134. }
  135. } else {
  136. for (p = 0; p < buffers; p++)
  137. sizes[p] = tpg_g_line_width(&dev->tpg, p) * h +
  138. dev->fmt_cap->data_offset[p];
  139. }
  140. if (vq->num_buffers + *nbuffers < 2)
  141. *nbuffers = 2 - vq->num_buffers;
  142. *nplanes = buffers;
  143. /*
  144. * videobuf2-vmalloc allocator is context-less so no need to set
  145. * alloc_ctxs array.
  146. */
  147. dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
  148. for (p = 0; p < buffers; p++)
  149. dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
  150. return 0;
  151. }
  152. static int vid_cap_buf_prepare(struct vb2_buffer *vb)
  153. {
  154. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  155. unsigned long size;
  156. unsigned buffers = tpg_g_buffers(&dev->tpg);
  157. unsigned p;
  158. dprintk(dev, 1, "%s\n", __func__);
  159. if (WARN_ON(NULL == dev->fmt_cap))
  160. return -EINVAL;
  161. if (dev->buf_prepare_error) {
  162. /*
  163. * Error injection: test what happens if buf_prepare() returns
  164. * an error.
  165. */
  166. dev->buf_prepare_error = false;
  167. return -EINVAL;
  168. }
  169. for (p = 0; p < buffers; p++) {
  170. size = tpg_g_line_width(&dev->tpg, p) * dev->fmt_cap_rect.height +
  171. dev->fmt_cap->data_offset[p];
  172. if (vb2_plane_size(vb, p) < size) {
  173. dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
  174. __func__, p, vb2_plane_size(vb, p), size);
  175. return -EINVAL;
  176. }
  177. vb2_set_plane_payload(vb, p, size);
  178. vb->v4l2_planes[p].data_offset = dev->fmt_cap->data_offset[p];
  179. }
  180. return 0;
  181. }
  182. static void vid_cap_buf_finish(struct vb2_buffer *vb)
  183. {
  184. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  185. struct v4l2_timecode *tc = &vb->v4l2_buf.timecode;
  186. unsigned fps = 25;
  187. unsigned seq = vb->v4l2_buf.sequence;
  188. if (!vivid_is_sdtv_cap(dev))
  189. return;
  190. /*
  191. * Set the timecode. Rarely used, so it is interesting to
  192. * test this.
  193. */
  194. vb->v4l2_buf.flags |= V4L2_BUF_FLAG_TIMECODE;
  195. if (dev->std_cap & V4L2_STD_525_60)
  196. fps = 30;
  197. tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
  198. tc->flags = 0;
  199. tc->frames = seq % fps;
  200. tc->seconds = (seq / fps) % 60;
  201. tc->minutes = (seq / (60 * fps)) % 60;
  202. tc->hours = (seq / (60 * 60 * fps)) % 24;
  203. }
  204. static void vid_cap_buf_queue(struct vb2_buffer *vb)
  205. {
  206. struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  207. struct vivid_buffer *buf = container_of(vb, struct vivid_buffer, vb);
  208. dprintk(dev, 1, "%s\n", __func__);
  209. spin_lock(&dev->slock);
  210. list_add_tail(&buf->list, &dev->vid_cap_active);
  211. spin_unlock(&dev->slock);
  212. }
  213. static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
  214. {
  215. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  216. unsigned i;
  217. int err;
  218. if (vb2_is_streaming(&dev->vb_vid_out_q))
  219. dev->can_loop_video = vivid_vid_can_loop(dev);
  220. if (dev->kthread_vid_cap)
  221. return 0;
  222. dev->vid_cap_seq_count = 0;
  223. dprintk(dev, 1, "%s\n", __func__);
  224. for (i = 0; i < VIDEO_MAX_FRAME; i++)
  225. dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
  226. if (dev->start_streaming_error) {
  227. dev->start_streaming_error = false;
  228. err = -EINVAL;
  229. } else {
  230. err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
  231. }
  232. if (err) {
  233. struct vivid_buffer *buf, *tmp;
  234. list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
  235. list_del(&buf->list);
  236. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
  237. }
  238. }
  239. return err;
  240. }
  241. /* abort streaming and wait for last buffer */
  242. static void vid_cap_stop_streaming(struct vb2_queue *vq)
  243. {
  244. struct vivid_dev *dev = vb2_get_drv_priv(vq);
  245. dprintk(dev, 1, "%s\n", __func__);
  246. vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
  247. dev->can_loop_video = false;
  248. }
  249. const struct vb2_ops vivid_vid_cap_qops = {
  250. .queue_setup = vid_cap_queue_setup,
  251. .buf_prepare = vid_cap_buf_prepare,
  252. .buf_finish = vid_cap_buf_finish,
  253. .buf_queue = vid_cap_buf_queue,
  254. .start_streaming = vid_cap_start_streaming,
  255. .stop_streaming = vid_cap_stop_streaming,
  256. .wait_prepare = vb2_ops_wait_prepare,
  257. .wait_finish = vb2_ops_wait_finish,
  258. };
  259. /*
  260. * Determine the 'picture' quality based on the current TV frequency: either
  261. * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
  262. * signal or NOISE for no signal.
  263. */
  264. void vivid_update_quality(struct vivid_dev *dev)
  265. {
  266. unsigned freq_modulus;
  267. if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
  268. /*
  269. * The 'noise' will only be replaced by the actual video
  270. * if the output video matches the input video settings.
  271. */
  272. tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
  273. return;
  274. }
  275. if (vivid_is_hdmi_cap(dev) && VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode)) {
  276. tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
  277. return;
  278. }
  279. if (vivid_is_sdtv_cap(dev) && VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
  280. tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
  281. return;
  282. }
  283. if (!vivid_is_tv_cap(dev)) {
  284. tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
  285. return;
  286. }
  287. /*
  288. * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
  289. * From +/- 0.25 MHz around the channel there is color, and from
  290. * +/- 1 MHz there is grayscale (chroma is lost).
  291. * Everywhere else it is just noise.
  292. */
  293. freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
  294. if (freq_modulus > 2 * 16) {
  295. tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
  296. next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
  297. return;
  298. }
  299. if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
  300. tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
  301. else
  302. tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
  303. }
  304. /*
  305. * Get the current picture quality and the associated afc value.
  306. */
  307. static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
  308. {
  309. unsigned freq_modulus;
  310. if (afc)
  311. *afc = 0;
  312. if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
  313. tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
  314. return tpg_g_quality(&dev->tpg);
  315. /*
  316. * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
  317. * From +/- 0.25 MHz around the channel there is color, and from
  318. * +/- 1 MHz there is grayscale (chroma is lost).
  319. * Everywhere else it is just gray.
  320. */
  321. freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
  322. if (afc)
  323. *afc = freq_modulus - 1 * 16;
  324. return TPG_QUAL_GRAY;
  325. }
  326. enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
  327. {
  328. if (vivid_is_sdtv_cap(dev))
  329. return dev->std_aspect_ratio;
  330. if (vivid_is_hdmi_cap(dev))
  331. return dev->dv_timings_aspect_ratio;
  332. return TPG_VIDEO_ASPECT_IMAGE;
  333. }
  334. static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
  335. {
  336. if (vivid_is_sdtv_cap(dev))
  337. return (dev->std_cap & V4L2_STD_525_60) ?
  338. TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
  339. if (vivid_is_hdmi_cap(dev) &&
  340. dev->src_rect.width == 720 && dev->src_rect.height <= 576)
  341. return dev->src_rect.height == 480 ?
  342. TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
  343. return TPG_PIXEL_ASPECT_SQUARE;
  344. }
  345. /*
  346. * Called whenever the format has to be reset which can occur when
  347. * changing inputs, standard, timings, etc.
  348. */
  349. void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
  350. {
  351. struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
  352. unsigned size;
  353. switch (dev->input_type[dev->input]) {
  354. case WEBCAM:
  355. default:
  356. dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
  357. dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
  358. dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
  359. dev->field_cap = V4L2_FIELD_NONE;
  360. tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
  361. break;
  362. case TV:
  363. case SVID:
  364. dev->field_cap = dev->tv_field_cap;
  365. dev->src_rect.width = 720;
  366. if (dev->std_cap & V4L2_STD_525_60) {
  367. dev->src_rect.height = 480;
  368. dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
  369. dev->service_set_cap = V4L2_SLICED_CAPTION_525;
  370. } else {
  371. dev->src_rect.height = 576;
  372. dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
  373. dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
  374. }
  375. tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
  376. break;
  377. case HDMI:
  378. dev->src_rect.width = bt->width;
  379. dev->src_rect.height = bt->height;
  380. size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
  381. dev->timeperframe_vid_cap = (struct v4l2_fract) {
  382. size / 100, (u32)bt->pixelclock / 100
  383. };
  384. if (bt->interlaced)
  385. dev->field_cap = V4L2_FIELD_ALTERNATE;
  386. else
  387. dev->field_cap = V4L2_FIELD_NONE;
  388. /*
  389. * We can be called from within s_ctrl, in that case we can't
  390. * set/get controls. Luckily we don't need to in that case.
  391. */
  392. if (keep_controls || !dev->colorspace)
  393. break;
  394. if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
  395. if (bt->width == 720 && bt->height <= 576)
  396. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
  397. else
  398. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
  399. v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
  400. } else {
  401. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
  402. v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
  403. }
  404. tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
  405. break;
  406. }
  407. vivid_update_quality(dev);
  408. tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
  409. dev->crop_cap = dev->src_rect;
  410. dev->crop_bounds_cap = dev->src_rect;
  411. dev->compose_cap = dev->crop_cap;
  412. if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
  413. dev->compose_cap.height /= 2;
  414. dev->fmt_cap_rect = dev->compose_cap;
  415. tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
  416. tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
  417. tpg_update_mv_step(&dev->tpg);
  418. }
  419. /* Map the field to something that is valid for the current input */
  420. static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
  421. {
  422. if (vivid_is_sdtv_cap(dev)) {
  423. switch (field) {
  424. case V4L2_FIELD_INTERLACED_TB:
  425. case V4L2_FIELD_INTERLACED_BT:
  426. case V4L2_FIELD_SEQ_TB:
  427. case V4L2_FIELD_SEQ_BT:
  428. case V4L2_FIELD_TOP:
  429. case V4L2_FIELD_BOTTOM:
  430. case V4L2_FIELD_ALTERNATE:
  431. return field;
  432. case V4L2_FIELD_INTERLACED:
  433. default:
  434. return V4L2_FIELD_INTERLACED;
  435. }
  436. }
  437. if (vivid_is_hdmi_cap(dev))
  438. return dev->dv_timings_cap.bt.interlaced ? V4L2_FIELD_ALTERNATE :
  439. V4L2_FIELD_NONE;
  440. return V4L2_FIELD_NONE;
  441. }
  442. static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
  443. {
  444. if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
  445. return tpg_g_colorspace(&dev->tpg);
  446. return dev->colorspace_out;
  447. }
  448. static unsigned vivid_xfer_func_cap(struct vivid_dev *dev)
  449. {
  450. if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
  451. return tpg_g_xfer_func(&dev->tpg);
  452. return dev->xfer_func_out;
  453. }
  454. static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
  455. {
  456. if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
  457. return tpg_g_ycbcr_enc(&dev->tpg);
  458. return dev->ycbcr_enc_out;
  459. }
  460. static unsigned vivid_quantization_cap(struct vivid_dev *dev)
  461. {
  462. if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
  463. return tpg_g_quantization(&dev->tpg);
  464. return dev->quantization_out;
  465. }
  466. int vivid_g_fmt_vid_cap(struct file *file, void *priv,
  467. struct v4l2_format *f)
  468. {
  469. struct vivid_dev *dev = video_drvdata(file);
  470. struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
  471. unsigned p;
  472. mp->width = dev->fmt_cap_rect.width;
  473. mp->height = dev->fmt_cap_rect.height;
  474. mp->field = dev->field_cap;
  475. mp->pixelformat = dev->fmt_cap->fourcc;
  476. mp->colorspace = vivid_colorspace_cap(dev);
  477. mp->xfer_func = vivid_xfer_func_cap(dev);
  478. mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
  479. mp->quantization = vivid_quantization_cap(dev);
  480. mp->num_planes = dev->fmt_cap->buffers;
  481. for (p = 0; p < mp->num_planes; p++) {
  482. mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
  483. mp->plane_fmt[p].sizeimage =
  484. tpg_g_line_width(&dev->tpg, p) * mp->height +
  485. dev->fmt_cap->data_offset[p];
  486. }
  487. return 0;
  488. }
  489. int vivid_try_fmt_vid_cap(struct file *file, void *priv,
  490. struct v4l2_format *f)
  491. {
  492. struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
  493. struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
  494. struct vivid_dev *dev = video_drvdata(file);
  495. const struct vivid_fmt *fmt;
  496. unsigned bytesperline, max_bpl;
  497. unsigned factor = 1;
  498. unsigned w, h;
  499. unsigned p;
  500. fmt = vivid_get_format(dev, mp->pixelformat);
  501. if (!fmt) {
  502. dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
  503. mp->pixelformat);
  504. mp->pixelformat = V4L2_PIX_FMT_YUYV;
  505. fmt = vivid_get_format(dev, mp->pixelformat);
  506. }
  507. mp->field = vivid_field_cap(dev, mp->field);
  508. if (vivid_is_webcam(dev)) {
  509. const struct v4l2_frmsize_discrete *sz =
  510. v4l2_find_nearest_format(&webcam_probe, mp->width, mp->height);
  511. w = sz->width;
  512. h = sz->height;
  513. } else if (vivid_is_sdtv_cap(dev)) {
  514. w = 720;
  515. h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
  516. } else {
  517. w = dev->src_rect.width;
  518. h = dev->src_rect.height;
  519. }
  520. if (V4L2_FIELD_HAS_T_OR_B(mp->field))
  521. factor = 2;
  522. if (vivid_is_webcam(dev) ||
  523. (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
  524. mp->width = w;
  525. mp->height = h / factor;
  526. } else {
  527. struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
  528. rect_set_min_size(&r, &vivid_min_rect);
  529. rect_set_max_size(&r, &vivid_max_rect);
  530. if (dev->has_scaler_cap && !dev->has_compose_cap) {
  531. struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
  532. rect_set_max_size(&r, &max_r);
  533. } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
  534. rect_set_max_size(&r, &dev->src_rect);
  535. } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
  536. rect_set_min_size(&r, &dev->src_rect);
  537. }
  538. mp->width = r.width;
  539. mp->height = r.height / factor;
  540. }
  541. /* This driver supports custom bytesperline values */
  542. mp->num_planes = fmt->buffers;
  543. for (p = 0; p < mp->num_planes; p++) {
  544. /* Calculate the minimum supported bytesperline value */
  545. bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
  546. /* Calculate the maximum supported bytesperline value */
  547. max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
  548. if (pfmt[p].bytesperline > max_bpl)
  549. pfmt[p].bytesperline = max_bpl;
  550. if (pfmt[p].bytesperline < bytesperline)
  551. pfmt[p].bytesperline = bytesperline;
  552. pfmt[p].sizeimage = tpg_calc_line_width(&dev->tpg, p, pfmt[p].bytesperline) *
  553. mp->height + fmt->data_offset[p];
  554. memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
  555. }
  556. mp->colorspace = vivid_colorspace_cap(dev);
  557. mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
  558. mp->xfer_func = vivid_xfer_func_cap(dev);
  559. mp->quantization = vivid_quantization_cap(dev);
  560. memset(mp->reserved, 0, sizeof(mp->reserved));
  561. return 0;
  562. }
  563. int vivid_s_fmt_vid_cap(struct file *file, void *priv,
  564. struct v4l2_format *f)
  565. {
  566. struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
  567. struct vivid_dev *dev = video_drvdata(file);
  568. struct v4l2_rect *crop = &dev->crop_cap;
  569. struct v4l2_rect *compose = &dev->compose_cap;
  570. struct vb2_queue *q = &dev->vb_vid_cap_q;
  571. int ret = vivid_try_fmt_vid_cap(file, priv, f);
  572. unsigned factor = 1;
  573. unsigned p;
  574. unsigned i;
  575. if (ret < 0)
  576. return ret;
  577. if (vb2_is_busy(q)) {
  578. dprintk(dev, 1, "%s device busy\n", __func__);
  579. return -EBUSY;
  580. }
  581. if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
  582. dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
  583. return -EBUSY;
  584. }
  585. dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
  586. if (V4L2_FIELD_HAS_T_OR_B(mp->field))
  587. factor = 2;
  588. /* Note: the webcam input doesn't support scaling, cropping or composing */
  589. if (!vivid_is_webcam(dev) &&
  590. (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
  591. struct v4l2_rect r = { 0, 0, mp->width, mp->height };
  592. if (dev->has_scaler_cap) {
  593. if (dev->has_compose_cap)
  594. rect_map_inside(compose, &r);
  595. else
  596. *compose = r;
  597. if (dev->has_crop_cap && !dev->has_compose_cap) {
  598. struct v4l2_rect min_r = {
  599. 0, 0,
  600. r.width / MAX_ZOOM,
  601. factor * r.height / MAX_ZOOM
  602. };
  603. struct v4l2_rect max_r = {
  604. 0, 0,
  605. r.width * MAX_ZOOM,
  606. factor * r.height * MAX_ZOOM
  607. };
  608. rect_set_min_size(crop, &min_r);
  609. rect_set_max_size(crop, &max_r);
  610. rect_map_inside(crop, &dev->crop_bounds_cap);
  611. } else if (dev->has_crop_cap) {
  612. struct v4l2_rect min_r = {
  613. 0, 0,
  614. compose->width / MAX_ZOOM,
  615. factor * compose->height / MAX_ZOOM
  616. };
  617. struct v4l2_rect max_r = {
  618. 0, 0,
  619. compose->width * MAX_ZOOM,
  620. factor * compose->height * MAX_ZOOM
  621. };
  622. rect_set_min_size(crop, &min_r);
  623. rect_set_max_size(crop, &max_r);
  624. rect_map_inside(crop, &dev->crop_bounds_cap);
  625. }
  626. } else if (dev->has_crop_cap && !dev->has_compose_cap) {
  627. r.height *= factor;
  628. rect_set_size_to(crop, &r);
  629. rect_map_inside(crop, &dev->crop_bounds_cap);
  630. r = *crop;
  631. r.height /= factor;
  632. rect_set_size_to(compose, &r);
  633. } else if (!dev->has_crop_cap) {
  634. rect_map_inside(compose, &r);
  635. } else {
  636. r.height *= factor;
  637. rect_set_max_size(crop, &r);
  638. rect_map_inside(crop, &dev->crop_bounds_cap);
  639. compose->top *= factor;
  640. compose->height *= factor;
  641. rect_set_size_to(compose, crop);
  642. rect_map_inside(compose, &r);
  643. compose->top /= factor;
  644. compose->height /= factor;
  645. }
  646. } else if (vivid_is_webcam(dev)) {
  647. /* Guaranteed to be a match */
  648. for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
  649. if (webcam_sizes[i].width == mp->width &&
  650. webcam_sizes[i].height == mp->height)
  651. break;
  652. dev->webcam_size_idx = i;
  653. if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
  654. dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
  655. vivid_update_format_cap(dev, false);
  656. } else {
  657. struct v4l2_rect r = { 0, 0, mp->width, mp->height };
  658. rect_set_size_to(compose, &r);
  659. r.height *= factor;
  660. rect_set_size_to(crop, &r);
  661. }
  662. dev->fmt_cap_rect.width = mp->width;
  663. dev->fmt_cap_rect.height = mp->height;
  664. tpg_s_buf_height(&dev->tpg, mp->height);
  665. tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
  666. for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
  667. tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
  668. dev->field_cap = mp->field;
  669. if (dev->field_cap == V4L2_FIELD_ALTERNATE)
  670. tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
  671. else
  672. tpg_s_field(&dev->tpg, dev->field_cap, false);
  673. tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
  674. if (vivid_is_sdtv_cap(dev))
  675. dev->tv_field_cap = mp->field;
  676. tpg_update_mv_step(&dev->tpg);
  677. return 0;
  678. }
  679. int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
  680. struct v4l2_format *f)
  681. {
  682. struct vivid_dev *dev = video_drvdata(file);
  683. if (!dev->multiplanar)
  684. return -ENOTTY;
  685. return vivid_g_fmt_vid_cap(file, priv, f);
  686. }
  687. int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
  688. struct v4l2_format *f)
  689. {
  690. struct vivid_dev *dev = video_drvdata(file);
  691. if (!dev->multiplanar)
  692. return -ENOTTY;
  693. return vivid_try_fmt_vid_cap(file, priv, f);
  694. }
  695. int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
  696. struct v4l2_format *f)
  697. {
  698. struct vivid_dev *dev = video_drvdata(file);
  699. if (!dev->multiplanar)
  700. return -ENOTTY;
  701. return vivid_s_fmt_vid_cap(file, priv, f);
  702. }
  703. int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  704. struct v4l2_format *f)
  705. {
  706. struct vivid_dev *dev = video_drvdata(file);
  707. if (dev->multiplanar)
  708. return -ENOTTY;
  709. return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
  710. }
  711. int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  712. struct v4l2_format *f)
  713. {
  714. struct vivid_dev *dev = video_drvdata(file);
  715. if (dev->multiplanar)
  716. return -ENOTTY;
  717. return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
  718. }
  719. int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  720. struct v4l2_format *f)
  721. {
  722. struct vivid_dev *dev = video_drvdata(file);
  723. if (dev->multiplanar)
  724. return -ENOTTY;
  725. return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
  726. }
  727. int vivid_vid_cap_g_selection(struct file *file, void *priv,
  728. struct v4l2_selection *sel)
  729. {
  730. struct vivid_dev *dev = video_drvdata(file);
  731. if (!dev->has_crop_cap && !dev->has_compose_cap)
  732. return -ENOTTY;
  733. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  734. return -EINVAL;
  735. if (vivid_is_webcam(dev))
  736. return -EINVAL;
  737. sel->r.left = sel->r.top = 0;
  738. switch (sel->target) {
  739. case V4L2_SEL_TGT_CROP:
  740. if (!dev->has_crop_cap)
  741. return -EINVAL;
  742. sel->r = dev->crop_cap;
  743. break;
  744. case V4L2_SEL_TGT_CROP_DEFAULT:
  745. case V4L2_SEL_TGT_CROP_BOUNDS:
  746. if (!dev->has_crop_cap)
  747. return -EINVAL;
  748. sel->r = dev->src_rect;
  749. break;
  750. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  751. if (!dev->has_compose_cap)
  752. return -EINVAL;
  753. sel->r = vivid_max_rect;
  754. break;
  755. case V4L2_SEL_TGT_COMPOSE:
  756. if (!dev->has_compose_cap)
  757. return -EINVAL;
  758. sel->r = dev->compose_cap;
  759. break;
  760. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  761. if (!dev->has_compose_cap)
  762. return -EINVAL;
  763. sel->r = dev->fmt_cap_rect;
  764. break;
  765. default:
  766. return -EINVAL;
  767. }
  768. return 0;
  769. }
  770. int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
  771. {
  772. struct vivid_dev *dev = video_drvdata(file);
  773. struct v4l2_rect *crop = &dev->crop_cap;
  774. struct v4l2_rect *compose = &dev->compose_cap;
  775. unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
  776. int ret;
  777. if (!dev->has_crop_cap && !dev->has_compose_cap)
  778. return -ENOTTY;
  779. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  780. return -EINVAL;
  781. if (vivid_is_webcam(dev))
  782. return -EINVAL;
  783. switch (s->target) {
  784. case V4L2_SEL_TGT_CROP:
  785. if (!dev->has_crop_cap)
  786. return -EINVAL;
  787. ret = vivid_vid_adjust_sel(s->flags, &s->r);
  788. if (ret)
  789. return ret;
  790. rect_set_min_size(&s->r, &vivid_min_rect);
  791. rect_set_max_size(&s->r, &dev->src_rect);
  792. rect_map_inside(&s->r, &dev->crop_bounds_cap);
  793. s->r.top /= factor;
  794. s->r.height /= factor;
  795. if (dev->has_scaler_cap) {
  796. struct v4l2_rect fmt = dev->fmt_cap_rect;
  797. struct v4l2_rect max_rect = {
  798. 0, 0,
  799. s->r.width * MAX_ZOOM,
  800. s->r.height * MAX_ZOOM
  801. };
  802. struct v4l2_rect min_rect = {
  803. 0, 0,
  804. s->r.width / MAX_ZOOM,
  805. s->r.height / MAX_ZOOM
  806. };
  807. rect_set_min_size(&fmt, &min_rect);
  808. if (!dev->has_compose_cap)
  809. rect_set_max_size(&fmt, &max_rect);
  810. if (!rect_same_size(&dev->fmt_cap_rect, &fmt) &&
  811. vb2_is_busy(&dev->vb_vid_cap_q))
  812. return -EBUSY;
  813. if (dev->has_compose_cap) {
  814. rect_set_min_size(compose, &min_rect);
  815. rect_set_max_size(compose, &max_rect);
  816. }
  817. dev->fmt_cap_rect = fmt;
  818. tpg_s_buf_height(&dev->tpg, fmt.height);
  819. } else if (dev->has_compose_cap) {
  820. struct v4l2_rect fmt = dev->fmt_cap_rect;
  821. rect_set_min_size(&fmt, &s->r);
  822. if (!rect_same_size(&dev->fmt_cap_rect, &fmt) &&
  823. vb2_is_busy(&dev->vb_vid_cap_q))
  824. return -EBUSY;
  825. dev->fmt_cap_rect = fmt;
  826. tpg_s_buf_height(&dev->tpg, fmt.height);
  827. rect_set_size_to(compose, &s->r);
  828. rect_map_inside(compose, &dev->fmt_cap_rect);
  829. } else {
  830. if (!rect_same_size(&s->r, &dev->fmt_cap_rect) &&
  831. vb2_is_busy(&dev->vb_vid_cap_q))
  832. return -EBUSY;
  833. rect_set_size_to(&dev->fmt_cap_rect, &s->r);
  834. rect_set_size_to(compose, &s->r);
  835. rect_map_inside(compose, &dev->fmt_cap_rect);
  836. tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
  837. }
  838. s->r.top *= factor;
  839. s->r.height *= factor;
  840. *crop = s->r;
  841. break;
  842. case V4L2_SEL_TGT_COMPOSE:
  843. if (!dev->has_compose_cap)
  844. return -EINVAL;
  845. ret = vivid_vid_adjust_sel(s->flags, &s->r);
  846. if (ret)
  847. return ret;
  848. rect_set_min_size(&s->r, &vivid_min_rect);
  849. rect_set_max_size(&s->r, &dev->fmt_cap_rect);
  850. if (dev->has_scaler_cap) {
  851. struct v4l2_rect max_rect = {
  852. 0, 0,
  853. dev->src_rect.width * MAX_ZOOM,
  854. (dev->src_rect.height / factor) * MAX_ZOOM
  855. };
  856. rect_set_max_size(&s->r, &max_rect);
  857. if (dev->has_crop_cap) {
  858. struct v4l2_rect min_rect = {
  859. 0, 0,
  860. s->r.width / MAX_ZOOM,
  861. (s->r.height * factor) / MAX_ZOOM
  862. };
  863. struct v4l2_rect max_rect = {
  864. 0, 0,
  865. s->r.width * MAX_ZOOM,
  866. (s->r.height * factor) * MAX_ZOOM
  867. };
  868. rect_set_min_size(crop, &min_rect);
  869. rect_set_max_size(crop, &max_rect);
  870. rect_map_inside(crop, &dev->crop_bounds_cap);
  871. }
  872. } else if (dev->has_crop_cap) {
  873. s->r.top *= factor;
  874. s->r.height *= factor;
  875. rect_set_max_size(&s->r, &dev->src_rect);
  876. rect_set_size_to(crop, &s->r);
  877. rect_map_inside(crop, &dev->crop_bounds_cap);
  878. s->r.top /= factor;
  879. s->r.height /= factor;
  880. } else {
  881. rect_set_size_to(&s->r, &dev->src_rect);
  882. s->r.height /= factor;
  883. }
  884. rect_map_inside(&s->r, &dev->fmt_cap_rect);
  885. if (dev->bitmap_cap && (compose->width != s->r.width ||
  886. compose->height != s->r.height)) {
  887. kfree(dev->bitmap_cap);
  888. dev->bitmap_cap = NULL;
  889. }
  890. *compose = s->r;
  891. break;
  892. default:
  893. return -EINVAL;
  894. }
  895. tpg_s_crop_compose(&dev->tpg, crop, compose);
  896. return 0;
  897. }
  898. int vivid_vid_cap_cropcap(struct file *file, void *priv,
  899. struct v4l2_cropcap *cap)
  900. {
  901. struct vivid_dev *dev = video_drvdata(file);
  902. if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  903. return -EINVAL;
  904. switch (vivid_get_pixel_aspect(dev)) {
  905. case TPG_PIXEL_ASPECT_NTSC:
  906. cap->pixelaspect.numerator = 11;
  907. cap->pixelaspect.denominator = 10;
  908. break;
  909. case TPG_PIXEL_ASPECT_PAL:
  910. cap->pixelaspect.numerator = 54;
  911. cap->pixelaspect.denominator = 59;
  912. break;
  913. case TPG_PIXEL_ASPECT_SQUARE:
  914. cap->pixelaspect.numerator = 1;
  915. cap->pixelaspect.denominator = 1;
  916. break;
  917. }
  918. return 0;
  919. }
  920. int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
  921. struct v4l2_fmtdesc *f)
  922. {
  923. struct vivid_dev *dev = video_drvdata(file);
  924. const struct vivid_fmt *fmt;
  925. if (dev->multiplanar)
  926. return -ENOTTY;
  927. if (f->index >= ARRAY_SIZE(formats_ovl))
  928. return -EINVAL;
  929. fmt = &formats_ovl[f->index];
  930. f->pixelformat = fmt->fourcc;
  931. return 0;
  932. }
  933. int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
  934. struct v4l2_format *f)
  935. {
  936. struct vivid_dev *dev = video_drvdata(file);
  937. const struct v4l2_rect *compose = &dev->compose_cap;
  938. struct v4l2_window *win = &f->fmt.win;
  939. unsigned clipcount = win->clipcount;
  940. if (dev->multiplanar)
  941. return -ENOTTY;
  942. win->w.top = dev->overlay_cap_top;
  943. win->w.left = dev->overlay_cap_left;
  944. win->w.width = compose->width;
  945. win->w.height = compose->height;
  946. win->field = dev->overlay_cap_field;
  947. win->clipcount = dev->clipcount_cap;
  948. if (clipcount > dev->clipcount_cap)
  949. clipcount = dev->clipcount_cap;
  950. if (dev->bitmap_cap == NULL)
  951. win->bitmap = NULL;
  952. else if (win->bitmap) {
  953. if (copy_to_user(win->bitmap, dev->bitmap_cap,
  954. ((compose->width + 7) / 8) * compose->height))
  955. return -EFAULT;
  956. }
  957. if (clipcount && win->clips) {
  958. if (copy_to_user(win->clips, dev->clips_cap,
  959. clipcount * sizeof(dev->clips_cap[0])))
  960. return -EFAULT;
  961. }
  962. return 0;
  963. }
  964. int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
  965. struct v4l2_format *f)
  966. {
  967. struct vivid_dev *dev = video_drvdata(file);
  968. const struct v4l2_rect *compose = &dev->compose_cap;
  969. struct v4l2_window *win = &f->fmt.win;
  970. int i, j;
  971. if (dev->multiplanar)
  972. return -ENOTTY;
  973. win->w.left = clamp_t(int, win->w.left,
  974. -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
  975. win->w.top = clamp_t(int, win->w.top,
  976. -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
  977. win->w.width = compose->width;
  978. win->w.height = compose->height;
  979. if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
  980. win->field = V4L2_FIELD_ANY;
  981. win->chromakey = 0;
  982. win->global_alpha = 0;
  983. if (win->clipcount && !win->clips)
  984. win->clipcount = 0;
  985. if (win->clipcount > MAX_CLIPS)
  986. win->clipcount = MAX_CLIPS;
  987. if (win->clipcount) {
  988. if (copy_from_user(dev->try_clips_cap, win->clips,
  989. win->clipcount * sizeof(dev->clips_cap[0])))
  990. return -EFAULT;
  991. for (i = 0; i < win->clipcount; i++) {
  992. struct v4l2_rect *r = &dev->try_clips_cap[i].c;
  993. r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
  994. r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
  995. r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
  996. r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
  997. }
  998. /*
  999. * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
  1000. * number and it's typically a one-time deal.
  1001. */
  1002. for (i = 0; i < win->clipcount - 1; i++) {
  1003. struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
  1004. for (j = i + 1; j < win->clipcount; j++) {
  1005. struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
  1006. if (rect_overlap(r1, r2))
  1007. return -EINVAL;
  1008. }
  1009. }
  1010. if (copy_to_user(win->clips, dev->try_clips_cap,
  1011. win->clipcount * sizeof(dev->clips_cap[0])))
  1012. return -EFAULT;
  1013. }
  1014. return 0;
  1015. }
  1016. int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
  1017. struct v4l2_format *f)
  1018. {
  1019. struct vivid_dev *dev = video_drvdata(file);
  1020. const struct v4l2_rect *compose = &dev->compose_cap;
  1021. struct v4l2_window *win = &f->fmt.win;
  1022. int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
  1023. unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
  1024. unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
  1025. void *new_bitmap = NULL;
  1026. if (ret)
  1027. return ret;
  1028. if (win->bitmap) {
  1029. new_bitmap = vzalloc(bitmap_size);
  1030. if (new_bitmap == NULL)
  1031. return -ENOMEM;
  1032. if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
  1033. vfree(new_bitmap);
  1034. return -EFAULT;
  1035. }
  1036. }
  1037. dev->overlay_cap_top = win->w.top;
  1038. dev->overlay_cap_left = win->w.left;
  1039. dev->overlay_cap_field = win->field;
  1040. vfree(dev->bitmap_cap);
  1041. dev->bitmap_cap = new_bitmap;
  1042. dev->clipcount_cap = win->clipcount;
  1043. if (dev->clipcount_cap)
  1044. memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
  1045. return 0;
  1046. }
  1047. int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
  1048. {
  1049. struct vivid_dev *dev = video_drvdata(file);
  1050. if (dev->multiplanar)
  1051. return -ENOTTY;
  1052. if (i && dev->fb_vbase_cap == NULL)
  1053. return -EINVAL;
  1054. if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
  1055. dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
  1056. return -EINVAL;
  1057. }
  1058. if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
  1059. return -EBUSY;
  1060. dev->overlay_cap_owner = i ? fh : NULL;
  1061. return 0;
  1062. }
  1063. int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
  1064. struct v4l2_framebuffer *a)
  1065. {
  1066. struct vivid_dev *dev = video_drvdata(file);
  1067. if (dev->multiplanar)
  1068. return -ENOTTY;
  1069. *a = dev->fb_cap;
  1070. a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
  1071. V4L2_FBUF_CAP_LIST_CLIPPING;
  1072. a->flags = V4L2_FBUF_FLAG_PRIMARY;
  1073. a->fmt.field = V4L2_FIELD_NONE;
  1074. a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  1075. a->fmt.priv = 0;
  1076. return 0;
  1077. }
  1078. int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
  1079. const struct v4l2_framebuffer *a)
  1080. {
  1081. struct vivid_dev *dev = video_drvdata(file);
  1082. const struct vivid_fmt *fmt;
  1083. if (dev->multiplanar)
  1084. return -ENOTTY;
  1085. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  1086. return -EPERM;
  1087. if (dev->overlay_cap_owner)
  1088. return -EBUSY;
  1089. if (a->base == NULL) {
  1090. dev->fb_cap.base = NULL;
  1091. dev->fb_vbase_cap = NULL;
  1092. return 0;
  1093. }
  1094. if (a->fmt.width < 48 || a->fmt.height < 32)
  1095. return -EINVAL;
  1096. fmt = vivid_get_format(dev, a->fmt.pixelformat);
  1097. if (!fmt || !fmt->can_do_overlay)
  1098. return -EINVAL;
  1099. if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
  1100. return -EINVAL;
  1101. if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage)
  1102. return -EINVAL;
  1103. dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
  1104. dev->fb_cap = *a;
  1105. dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
  1106. -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
  1107. dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
  1108. -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
  1109. return 0;
  1110. }
  1111. static const struct v4l2_audio vivid_audio_inputs[] = {
  1112. { 0, "TV", V4L2_AUDCAP_STEREO },
  1113. { 1, "Line-In", V4L2_AUDCAP_STEREO },
  1114. };
  1115. int vidioc_enum_input(struct file *file, void *priv,
  1116. struct v4l2_input *inp)
  1117. {
  1118. struct vivid_dev *dev = video_drvdata(file);
  1119. if (inp->index >= dev->num_inputs)
  1120. return -EINVAL;
  1121. inp->type = V4L2_INPUT_TYPE_CAMERA;
  1122. switch (dev->input_type[inp->index]) {
  1123. case WEBCAM:
  1124. snprintf(inp->name, sizeof(inp->name), "Webcam %u",
  1125. dev->input_name_counter[inp->index]);
  1126. inp->capabilities = 0;
  1127. break;
  1128. case TV:
  1129. snprintf(inp->name, sizeof(inp->name), "TV %u",
  1130. dev->input_name_counter[inp->index]);
  1131. inp->type = V4L2_INPUT_TYPE_TUNER;
  1132. inp->std = V4L2_STD_ALL;
  1133. if (dev->has_audio_inputs)
  1134. inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
  1135. inp->capabilities = V4L2_IN_CAP_STD;
  1136. break;
  1137. case SVID:
  1138. snprintf(inp->name, sizeof(inp->name), "S-Video %u",
  1139. dev->input_name_counter[inp->index]);
  1140. inp->std = V4L2_STD_ALL;
  1141. if (dev->has_audio_inputs)
  1142. inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
  1143. inp->capabilities = V4L2_IN_CAP_STD;
  1144. break;
  1145. case HDMI:
  1146. snprintf(inp->name, sizeof(inp->name), "HDMI %u",
  1147. dev->input_name_counter[inp->index]);
  1148. inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
  1149. if (dev->edid_blocks == 0 ||
  1150. dev->dv_timings_signal_mode == NO_SIGNAL)
  1151. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  1152. else if (dev->dv_timings_signal_mode == NO_LOCK ||
  1153. dev->dv_timings_signal_mode == OUT_OF_RANGE)
  1154. inp->status |= V4L2_IN_ST_NO_H_LOCK;
  1155. break;
  1156. }
  1157. if (dev->sensor_hflip)
  1158. inp->status |= V4L2_IN_ST_HFLIP;
  1159. if (dev->sensor_vflip)
  1160. inp->status |= V4L2_IN_ST_VFLIP;
  1161. if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
  1162. if (dev->std_signal_mode == NO_SIGNAL) {
  1163. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  1164. } else if (dev->std_signal_mode == NO_LOCK) {
  1165. inp->status |= V4L2_IN_ST_NO_H_LOCK;
  1166. } else if (vivid_is_tv_cap(dev)) {
  1167. switch (tpg_g_quality(&dev->tpg)) {
  1168. case TPG_QUAL_GRAY:
  1169. inp->status |= V4L2_IN_ST_COLOR_KILL;
  1170. break;
  1171. case TPG_QUAL_NOISE:
  1172. inp->status |= V4L2_IN_ST_NO_H_LOCK;
  1173. break;
  1174. default:
  1175. break;
  1176. }
  1177. }
  1178. }
  1179. return 0;
  1180. }
  1181. int vidioc_g_input(struct file *file, void *priv, unsigned *i)
  1182. {
  1183. struct vivid_dev *dev = video_drvdata(file);
  1184. *i = dev->input;
  1185. return 0;
  1186. }
  1187. int vidioc_s_input(struct file *file, void *priv, unsigned i)
  1188. {
  1189. struct vivid_dev *dev = video_drvdata(file);
  1190. struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
  1191. unsigned brightness;
  1192. if (i >= dev->num_inputs)
  1193. return -EINVAL;
  1194. if (i == dev->input)
  1195. return 0;
  1196. if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
  1197. return -EBUSY;
  1198. dev->input = i;
  1199. dev->vid_cap_dev.tvnorms = 0;
  1200. if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
  1201. dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
  1202. dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
  1203. }
  1204. dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
  1205. vivid_update_format_cap(dev, false);
  1206. if (dev->colorspace) {
  1207. switch (dev->input_type[i]) {
  1208. case WEBCAM:
  1209. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
  1210. break;
  1211. case TV:
  1212. case SVID:
  1213. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
  1214. break;
  1215. case HDMI:
  1216. if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
  1217. if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
  1218. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
  1219. else
  1220. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
  1221. } else {
  1222. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
  1223. }
  1224. break;
  1225. }
  1226. }
  1227. /*
  1228. * Modify the brightness range depending on the input.
  1229. * This makes it easy to use vivid to test if applications can
  1230. * handle control range modifications and is also how this is
  1231. * typically used in practice as different inputs may be hooked
  1232. * up to different receivers with different control ranges.
  1233. */
  1234. brightness = 128 * i + dev->input_brightness[i];
  1235. v4l2_ctrl_modify_range(dev->brightness,
  1236. 128 * i, 255 + 128 * i, 1, 128 + 128 * i);
  1237. v4l2_ctrl_s_ctrl(dev->brightness, brightness);
  1238. return 0;
  1239. }
  1240. int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
  1241. {
  1242. if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
  1243. return -EINVAL;
  1244. *vin = vivid_audio_inputs[vin->index];
  1245. return 0;
  1246. }
  1247. int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
  1248. {
  1249. struct vivid_dev *dev = video_drvdata(file);
  1250. if (!vivid_is_sdtv_cap(dev))
  1251. return -EINVAL;
  1252. *vin = vivid_audio_inputs[dev->tv_audio_input];
  1253. return 0;
  1254. }
  1255. int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
  1256. {
  1257. struct vivid_dev *dev = video_drvdata(file);
  1258. if (!vivid_is_sdtv_cap(dev))
  1259. return -EINVAL;
  1260. if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
  1261. return -EINVAL;
  1262. dev->tv_audio_input = vin->index;
  1263. return 0;
  1264. }
  1265. int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
  1266. {
  1267. struct vivid_dev *dev = video_drvdata(file);
  1268. if (vf->tuner != 0)
  1269. return -EINVAL;
  1270. vf->frequency = dev->tv_freq;
  1271. return 0;
  1272. }
  1273. int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
  1274. {
  1275. struct vivid_dev *dev = video_drvdata(file);
  1276. if (vf->tuner != 0)
  1277. return -EINVAL;
  1278. dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
  1279. if (vivid_is_tv_cap(dev))
  1280. vivid_update_quality(dev);
  1281. return 0;
  1282. }
  1283. int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
  1284. {
  1285. struct vivid_dev *dev = video_drvdata(file);
  1286. if (vt->index != 0)
  1287. return -EINVAL;
  1288. if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
  1289. return -EINVAL;
  1290. dev->tv_audmode = vt->audmode;
  1291. return 0;
  1292. }
  1293. int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  1294. {
  1295. struct vivid_dev *dev = video_drvdata(file);
  1296. enum tpg_quality qual;
  1297. if (vt->index != 0)
  1298. return -EINVAL;
  1299. vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
  1300. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  1301. vt->audmode = dev->tv_audmode;
  1302. vt->rangelow = MIN_TV_FREQ;
  1303. vt->rangehigh = MAX_TV_FREQ;
  1304. qual = vivid_get_quality(dev, &vt->afc);
  1305. if (qual == TPG_QUAL_COLOR)
  1306. vt->signal = 0xffff;
  1307. else if (qual == TPG_QUAL_GRAY)
  1308. vt->signal = 0x8000;
  1309. else
  1310. vt->signal = 0;
  1311. if (qual == TPG_QUAL_NOISE) {
  1312. vt->rxsubchans = 0;
  1313. } else if (qual == TPG_QUAL_GRAY) {
  1314. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  1315. } else {
  1316. unsigned channel_nr = dev->tv_freq / (6 * 16);
  1317. unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
  1318. switch (channel_nr % options) {
  1319. case 0:
  1320. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  1321. break;
  1322. case 1:
  1323. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1324. break;
  1325. case 2:
  1326. if (dev->std_cap & V4L2_STD_NTSC_M)
  1327. vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
  1328. else
  1329. vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  1330. break;
  1331. case 3:
  1332. vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
  1333. break;
  1334. }
  1335. }
  1336. strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
  1337. return 0;
  1338. }
  1339. /* Must remain in sync with the vivid_ctrl_standard_strings array */
  1340. const v4l2_std_id vivid_standard[] = {
  1341. V4L2_STD_NTSC_M,
  1342. V4L2_STD_NTSC_M_JP,
  1343. V4L2_STD_NTSC_M_KR,
  1344. V4L2_STD_NTSC_443,
  1345. V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
  1346. V4L2_STD_PAL_I,
  1347. V4L2_STD_PAL_DK,
  1348. V4L2_STD_PAL_M,
  1349. V4L2_STD_PAL_N,
  1350. V4L2_STD_PAL_Nc,
  1351. V4L2_STD_PAL_60,
  1352. V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
  1353. V4L2_STD_SECAM_DK,
  1354. V4L2_STD_SECAM_L,
  1355. V4L2_STD_SECAM_LC,
  1356. V4L2_STD_UNKNOWN
  1357. };
  1358. /* Must remain in sync with the vivid_standard array */
  1359. const char * const vivid_ctrl_standard_strings[] = {
  1360. "NTSC-M",
  1361. "NTSC-M-JP",
  1362. "NTSC-M-KR",
  1363. "NTSC-443",
  1364. "PAL-BGH",
  1365. "PAL-I",
  1366. "PAL-DK",
  1367. "PAL-M",
  1368. "PAL-N",
  1369. "PAL-Nc",
  1370. "PAL-60",
  1371. "SECAM-BGH",
  1372. "SECAM-DK",
  1373. "SECAM-L",
  1374. "SECAM-Lc",
  1375. NULL,
  1376. };
  1377. int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
  1378. {
  1379. struct vivid_dev *dev = video_drvdata(file);
  1380. if (!vivid_is_sdtv_cap(dev))
  1381. return -ENODATA;
  1382. if (dev->std_signal_mode == NO_SIGNAL ||
  1383. dev->std_signal_mode == NO_LOCK) {
  1384. *id = V4L2_STD_UNKNOWN;
  1385. return 0;
  1386. }
  1387. if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
  1388. *id = V4L2_STD_UNKNOWN;
  1389. } else if (dev->std_signal_mode == CURRENT_STD) {
  1390. *id = dev->std_cap;
  1391. } else if (dev->std_signal_mode == SELECTED_STD) {
  1392. *id = dev->query_std;
  1393. } else {
  1394. *id = vivid_standard[dev->query_std_last];
  1395. dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
  1396. }
  1397. return 0;
  1398. }
  1399. int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
  1400. {
  1401. struct vivid_dev *dev = video_drvdata(file);
  1402. if (!vivid_is_sdtv_cap(dev))
  1403. return -ENODATA;
  1404. if (dev->std_cap == id)
  1405. return 0;
  1406. if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
  1407. return -EBUSY;
  1408. dev->std_cap = id;
  1409. vivid_update_format_cap(dev, false);
  1410. return 0;
  1411. }
  1412. static void find_aspect_ratio(u32 width, u32 height,
  1413. u32 *num, u32 *denom)
  1414. {
  1415. if (!(height % 3) && ((height * 4 / 3) == width)) {
  1416. *num = 4;
  1417. *denom = 3;
  1418. } else if (!(height % 9) && ((height * 16 / 9) == width)) {
  1419. *num = 16;
  1420. *denom = 9;
  1421. } else if (!(height % 10) && ((height * 16 / 10) == width)) {
  1422. *num = 16;
  1423. *denom = 10;
  1424. } else if (!(height % 4) && ((height * 5 / 4) == width)) {
  1425. *num = 5;
  1426. *denom = 4;
  1427. } else if (!(height % 9) && ((height * 15 / 9) == width)) {
  1428. *num = 15;
  1429. *denom = 9;
  1430. } else { /* default to 16:9 */
  1431. *num = 16;
  1432. *denom = 9;
  1433. }
  1434. }
  1435. static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
  1436. {
  1437. struct v4l2_bt_timings *bt = &timings->bt;
  1438. u32 total_h_pixel;
  1439. u32 total_v_lines;
  1440. u32 h_freq;
  1441. if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
  1442. NULL, NULL))
  1443. return false;
  1444. total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
  1445. total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
  1446. h_freq = (u32)bt->pixelclock / total_h_pixel;
  1447. if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
  1448. if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync,
  1449. bt->polarities, bt->interlaced, timings))
  1450. return true;
  1451. }
  1452. if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
  1453. struct v4l2_fract aspect_ratio;
  1454. find_aspect_ratio(bt->width, bt->height,
  1455. &aspect_ratio.numerator,
  1456. &aspect_ratio.denominator);
  1457. if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
  1458. bt->polarities, bt->interlaced,
  1459. aspect_ratio, timings))
  1460. return true;
  1461. }
  1462. return false;
  1463. }
  1464. int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
  1465. struct v4l2_dv_timings *timings)
  1466. {
  1467. struct vivid_dev *dev = video_drvdata(file);
  1468. if (!vivid_is_hdmi_cap(dev))
  1469. return -ENODATA;
  1470. if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
  1471. 0, NULL, NULL) &&
  1472. !valid_cvt_gtf_timings(timings))
  1473. return -EINVAL;
  1474. if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0))
  1475. return 0;
  1476. if (vb2_is_busy(&dev->vb_vid_cap_q))
  1477. return -EBUSY;
  1478. dev->dv_timings_cap = *timings;
  1479. vivid_update_format_cap(dev, false);
  1480. return 0;
  1481. }
  1482. int vidioc_query_dv_timings(struct file *file, void *_fh,
  1483. struct v4l2_dv_timings *timings)
  1484. {
  1485. struct vivid_dev *dev = video_drvdata(file);
  1486. if (!vivid_is_hdmi_cap(dev))
  1487. return -ENODATA;
  1488. if (dev->dv_timings_signal_mode == NO_SIGNAL ||
  1489. dev->edid_blocks == 0)
  1490. return -ENOLINK;
  1491. if (dev->dv_timings_signal_mode == NO_LOCK)
  1492. return -ENOLCK;
  1493. if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
  1494. timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
  1495. return -ERANGE;
  1496. }
  1497. if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
  1498. *timings = dev->dv_timings_cap;
  1499. } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
  1500. *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
  1501. } else {
  1502. *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
  1503. dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
  1504. dev->query_dv_timings_size;
  1505. }
  1506. return 0;
  1507. }
  1508. int vidioc_s_edid(struct file *file, void *_fh,
  1509. struct v4l2_edid *edid)
  1510. {
  1511. struct vivid_dev *dev = video_drvdata(file);
  1512. memset(edid->reserved, 0, sizeof(edid->reserved));
  1513. if (edid->pad >= dev->num_inputs)
  1514. return -EINVAL;
  1515. if (dev->input_type[edid->pad] != HDMI || edid->start_block)
  1516. return -EINVAL;
  1517. if (edid->blocks == 0) {
  1518. dev->edid_blocks = 0;
  1519. return 0;
  1520. }
  1521. if (edid->blocks > dev->edid_max_blocks) {
  1522. edid->blocks = dev->edid_max_blocks;
  1523. return -E2BIG;
  1524. }
  1525. dev->edid_blocks = edid->blocks;
  1526. memcpy(dev->edid, edid->edid, edid->blocks * 128);
  1527. return 0;
  1528. }
  1529. int vidioc_enum_framesizes(struct file *file, void *fh,
  1530. struct v4l2_frmsizeenum *fsize)
  1531. {
  1532. struct vivid_dev *dev = video_drvdata(file);
  1533. if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
  1534. return -EINVAL;
  1535. if (vivid_get_format(dev, fsize->pixel_format) == NULL)
  1536. return -EINVAL;
  1537. if (vivid_is_webcam(dev)) {
  1538. if (fsize->index >= ARRAY_SIZE(webcam_sizes))
  1539. return -EINVAL;
  1540. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1541. fsize->discrete = webcam_sizes[fsize->index];
  1542. return 0;
  1543. }
  1544. if (fsize->index)
  1545. return -EINVAL;
  1546. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1547. fsize->stepwise.min_width = MIN_WIDTH;
  1548. fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
  1549. fsize->stepwise.step_width = 2;
  1550. fsize->stepwise.min_height = MIN_HEIGHT;
  1551. fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
  1552. fsize->stepwise.step_height = 2;
  1553. return 0;
  1554. }
  1555. /* timeperframe is arbitrary and continuous */
  1556. int vidioc_enum_frameintervals(struct file *file, void *priv,
  1557. struct v4l2_frmivalenum *fival)
  1558. {
  1559. struct vivid_dev *dev = video_drvdata(file);
  1560. const struct vivid_fmt *fmt;
  1561. int i;
  1562. fmt = vivid_get_format(dev, fival->pixel_format);
  1563. if (!fmt)
  1564. return -EINVAL;
  1565. if (!vivid_is_webcam(dev)) {
  1566. if (fival->index)
  1567. return -EINVAL;
  1568. if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
  1569. return -EINVAL;
  1570. if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
  1571. return -EINVAL;
  1572. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1573. fival->discrete = dev->timeperframe_vid_cap;
  1574. return 0;
  1575. }
  1576. for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
  1577. if (fival->width == webcam_sizes[i].width &&
  1578. fival->height == webcam_sizes[i].height)
  1579. break;
  1580. if (i == ARRAY_SIZE(webcam_sizes))
  1581. return -EINVAL;
  1582. if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
  1583. return -EINVAL;
  1584. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1585. fival->discrete = webcam_intervals[fival->index];
  1586. return 0;
  1587. }
  1588. int vivid_vid_cap_g_parm(struct file *file, void *priv,
  1589. struct v4l2_streamparm *parm)
  1590. {
  1591. struct vivid_dev *dev = video_drvdata(file);
  1592. if (parm->type != (dev->multiplanar ?
  1593. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
  1594. V4L2_BUF_TYPE_VIDEO_CAPTURE))
  1595. return -EINVAL;
  1596. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1597. parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
  1598. parm->parm.capture.readbuffers = 1;
  1599. return 0;
  1600. }
  1601. #define FRACT_CMP(a, OP, b) \
  1602. ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
  1603. int vivid_vid_cap_s_parm(struct file *file, void *priv,
  1604. struct v4l2_streamparm *parm)
  1605. {
  1606. struct vivid_dev *dev = video_drvdata(file);
  1607. unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
  1608. struct v4l2_fract tpf;
  1609. unsigned i;
  1610. if (parm->type != (dev->multiplanar ?
  1611. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
  1612. V4L2_BUF_TYPE_VIDEO_CAPTURE))
  1613. return -EINVAL;
  1614. if (!vivid_is_webcam(dev))
  1615. return vivid_vid_cap_g_parm(file, priv, parm);
  1616. tpf = parm->parm.capture.timeperframe;
  1617. if (tpf.denominator == 0)
  1618. tpf = webcam_intervals[ival_sz - 1];
  1619. for (i = 0; i < ival_sz; i++)
  1620. if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
  1621. break;
  1622. if (i == ival_sz)
  1623. i = ival_sz - 1;
  1624. dev->webcam_ival_idx = i;
  1625. tpf = webcam_intervals[dev->webcam_ival_idx];
  1626. tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
  1627. tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
  1628. /* resync the thread's timings */
  1629. dev->cap_seq_resync = true;
  1630. dev->timeperframe_vid_cap = tpf;
  1631. parm->parm.capture.timeperframe = tpf;
  1632. parm->parm.capture.readbuffers = 1;
  1633. return 0;
  1634. }