vivid-vid-cap.c 47 KB

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