vivid-vid-cap.c 50 KB

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