vivid-vid-cap.c 51 KB

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