vivid-vid-cap.c 52 KB

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