vivid-vid-cap.c 51 KB

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