vivid-vid-cap.c 50 KB

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