vivid-vid-cap.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  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 vivid_quantization_cap(struct vivid_dev *dev)
  458. {
  459. if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
  460. return tpg_g_quantization(&dev->tpg);
  461. return dev->quantization_out;
  462. }
  463. int vivid_g_fmt_vid_cap(struct file *file, void *priv,
  464. struct v4l2_format *f)
  465. {
  466. struct vivid_dev *dev = video_drvdata(file);
  467. struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
  468. unsigned p;
  469. mp->width = dev->fmt_cap_rect.width;
  470. mp->height = dev->fmt_cap_rect.height;
  471. mp->field = dev->field_cap;
  472. mp->pixelformat = dev->fmt_cap->fourcc;
  473. mp->colorspace = vivid_colorspace_cap(dev);
  474. mp->xfer_func = vivid_xfer_func_cap(dev);
  475. mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
  476. mp->quantization = vivid_quantization_cap(dev);
  477. mp->num_planes = dev->fmt_cap->buffers;
  478. for (p = 0; p < mp->num_planes; p++) {
  479. mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
  480. mp->plane_fmt[p].sizeimage =
  481. tpg_g_line_width(&dev->tpg, p) * mp->height +
  482. dev->fmt_cap->data_offset[p];
  483. }
  484. return 0;
  485. }
  486. int vivid_try_fmt_vid_cap(struct file *file, void *priv,
  487. struct v4l2_format *f)
  488. {
  489. struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
  490. struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
  491. struct vivid_dev *dev = video_drvdata(file);
  492. const struct vivid_fmt *fmt;
  493. unsigned bytesperline, max_bpl;
  494. unsigned factor = 1;
  495. unsigned w, h;
  496. unsigned p;
  497. fmt = vivid_get_format(dev, mp->pixelformat);
  498. if (!fmt) {
  499. dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
  500. mp->pixelformat);
  501. mp->pixelformat = V4L2_PIX_FMT_YUYV;
  502. fmt = vivid_get_format(dev, mp->pixelformat);
  503. }
  504. mp->field = vivid_field_cap(dev, mp->field);
  505. if (vivid_is_webcam(dev)) {
  506. const struct v4l2_frmsize_discrete *sz =
  507. v4l2_find_nearest_format(&webcam_probe, mp->width, mp->height);
  508. w = sz->width;
  509. h = sz->height;
  510. } else if (vivid_is_sdtv_cap(dev)) {
  511. w = 720;
  512. h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
  513. } else {
  514. w = dev->src_rect.width;
  515. h = dev->src_rect.height;
  516. }
  517. if (V4L2_FIELD_HAS_T_OR_B(mp->field))
  518. factor = 2;
  519. if (vivid_is_webcam(dev) ||
  520. (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
  521. mp->width = w;
  522. mp->height = h / factor;
  523. } else {
  524. struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
  525. v4l2_rect_set_min_size(&r, &vivid_min_rect);
  526. v4l2_rect_set_max_size(&r, &vivid_max_rect);
  527. if (dev->has_scaler_cap && !dev->has_compose_cap) {
  528. struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
  529. v4l2_rect_set_max_size(&r, &max_r);
  530. } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
  531. v4l2_rect_set_max_size(&r, &dev->src_rect);
  532. } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
  533. v4l2_rect_set_min_size(&r, &dev->src_rect);
  534. }
  535. mp->width = r.width;
  536. mp->height = r.height / factor;
  537. }
  538. /* This driver supports custom bytesperline values */
  539. mp->num_planes = fmt->buffers;
  540. for (p = 0; p < mp->num_planes; p++) {
  541. /* Calculate the minimum supported bytesperline value */
  542. bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
  543. /* Calculate the maximum supported bytesperline value */
  544. max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
  545. if (pfmt[p].bytesperline > max_bpl)
  546. pfmt[p].bytesperline = max_bpl;
  547. if (pfmt[p].bytesperline < bytesperline)
  548. pfmt[p].bytesperline = bytesperline;
  549. pfmt[p].sizeimage = tpg_calc_line_width(&dev->tpg, p, pfmt[p].bytesperline) *
  550. mp->height + fmt->data_offset[p];
  551. memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
  552. }
  553. mp->colorspace = vivid_colorspace_cap(dev);
  554. mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
  555. mp->xfer_func = vivid_xfer_func_cap(dev);
  556. mp->quantization = vivid_quantization_cap(dev);
  557. memset(mp->reserved, 0, sizeof(mp->reserved));
  558. return 0;
  559. }
  560. int vivid_s_fmt_vid_cap(struct file *file, void *priv,
  561. struct v4l2_format *f)
  562. {
  563. struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
  564. struct vivid_dev *dev = video_drvdata(file);
  565. struct v4l2_rect *crop = &dev->crop_cap;
  566. struct v4l2_rect *compose = &dev->compose_cap;
  567. struct vb2_queue *q = &dev->vb_vid_cap_q;
  568. int ret = vivid_try_fmt_vid_cap(file, priv, f);
  569. unsigned factor = 1;
  570. unsigned p;
  571. unsigned i;
  572. if (ret < 0)
  573. return ret;
  574. if (vb2_is_busy(q)) {
  575. dprintk(dev, 1, "%s device busy\n", __func__);
  576. return -EBUSY;
  577. }
  578. if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
  579. dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
  580. return -EBUSY;
  581. }
  582. dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
  583. if (V4L2_FIELD_HAS_T_OR_B(mp->field))
  584. factor = 2;
  585. /* Note: the webcam input doesn't support scaling, cropping or composing */
  586. if (!vivid_is_webcam(dev) &&
  587. (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
  588. struct v4l2_rect r = { 0, 0, mp->width, mp->height };
  589. if (dev->has_scaler_cap) {
  590. if (dev->has_compose_cap)
  591. v4l2_rect_map_inside(compose, &r);
  592. else
  593. *compose = r;
  594. if (dev->has_crop_cap && !dev->has_compose_cap) {
  595. struct v4l2_rect min_r = {
  596. 0, 0,
  597. r.width / MAX_ZOOM,
  598. factor * r.height / MAX_ZOOM
  599. };
  600. struct v4l2_rect max_r = {
  601. 0, 0,
  602. r.width * MAX_ZOOM,
  603. factor * r.height * MAX_ZOOM
  604. };
  605. v4l2_rect_set_min_size(crop, &min_r);
  606. v4l2_rect_set_max_size(crop, &max_r);
  607. v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
  608. } else if (dev->has_crop_cap) {
  609. struct v4l2_rect min_r = {
  610. 0, 0,
  611. compose->width / MAX_ZOOM,
  612. factor * compose->height / MAX_ZOOM
  613. };
  614. struct v4l2_rect max_r = {
  615. 0, 0,
  616. compose->width * MAX_ZOOM,
  617. factor * compose->height * MAX_ZOOM
  618. };
  619. v4l2_rect_set_min_size(crop, &min_r);
  620. v4l2_rect_set_max_size(crop, &max_r);
  621. v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
  622. }
  623. } else if (dev->has_crop_cap && !dev->has_compose_cap) {
  624. r.height *= factor;
  625. v4l2_rect_set_size_to(crop, &r);
  626. v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
  627. r = *crop;
  628. r.height /= factor;
  629. v4l2_rect_set_size_to(compose, &r);
  630. } else if (!dev->has_crop_cap) {
  631. v4l2_rect_map_inside(compose, &r);
  632. } else {
  633. r.height *= factor;
  634. v4l2_rect_set_max_size(crop, &r);
  635. v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
  636. compose->top *= factor;
  637. compose->height *= factor;
  638. v4l2_rect_set_size_to(compose, crop);
  639. v4l2_rect_map_inside(compose, &r);
  640. compose->top /= factor;
  641. compose->height /= factor;
  642. }
  643. } else if (vivid_is_webcam(dev)) {
  644. /* Guaranteed to be a match */
  645. for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
  646. if (webcam_sizes[i].width == mp->width &&
  647. webcam_sizes[i].height == mp->height)
  648. break;
  649. dev->webcam_size_idx = i;
  650. if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
  651. dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
  652. vivid_update_format_cap(dev, false);
  653. } else {
  654. struct v4l2_rect r = { 0, 0, mp->width, mp->height };
  655. v4l2_rect_set_size_to(compose, &r);
  656. r.height *= factor;
  657. v4l2_rect_set_size_to(crop, &r);
  658. }
  659. dev->fmt_cap_rect.width = mp->width;
  660. dev->fmt_cap_rect.height = mp->height;
  661. tpg_s_buf_height(&dev->tpg, mp->height);
  662. tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
  663. for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
  664. tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
  665. dev->field_cap = mp->field;
  666. if (dev->field_cap == V4L2_FIELD_ALTERNATE)
  667. tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
  668. else
  669. tpg_s_field(&dev->tpg, dev->field_cap, false);
  670. tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
  671. if (vivid_is_sdtv_cap(dev))
  672. dev->tv_field_cap = mp->field;
  673. tpg_update_mv_step(&dev->tpg);
  674. return 0;
  675. }
  676. int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
  677. struct v4l2_format *f)
  678. {
  679. struct vivid_dev *dev = video_drvdata(file);
  680. if (!dev->multiplanar)
  681. return -ENOTTY;
  682. return vivid_g_fmt_vid_cap(file, priv, f);
  683. }
  684. int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
  685. struct v4l2_format *f)
  686. {
  687. struct vivid_dev *dev = video_drvdata(file);
  688. if (!dev->multiplanar)
  689. return -ENOTTY;
  690. return vivid_try_fmt_vid_cap(file, priv, f);
  691. }
  692. int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
  693. struct v4l2_format *f)
  694. {
  695. struct vivid_dev *dev = video_drvdata(file);
  696. if (!dev->multiplanar)
  697. return -ENOTTY;
  698. return vivid_s_fmt_vid_cap(file, priv, f);
  699. }
  700. int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  701. struct v4l2_format *f)
  702. {
  703. struct vivid_dev *dev = video_drvdata(file);
  704. if (dev->multiplanar)
  705. return -ENOTTY;
  706. return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
  707. }
  708. int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  709. struct v4l2_format *f)
  710. {
  711. struct vivid_dev *dev = video_drvdata(file);
  712. if (dev->multiplanar)
  713. return -ENOTTY;
  714. return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
  715. }
  716. int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  717. struct v4l2_format *f)
  718. {
  719. struct vivid_dev *dev = video_drvdata(file);
  720. if (dev->multiplanar)
  721. return -ENOTTY;
  722. return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
  723. }
  724. int vivid_vid_cap_g_selection(struct file *file, void *priv,
  725. struct v4l2_selection *sel)
  726. {
  727. struct vivid_dev *dev = video_drvdata(file);
  728. if (!dev->has_crop_cap && !dev->has_compose_cap)
  729. return -ENOTTY;
  730. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  731. return -EINVAL;
  732. if (vivid_is_webcam(dev))
  733. return -ENODATA;
  734. sel->r.left = sel->r.top = 0;
  735. switch (sel->target) {
  736. case V4L2_SEL_TGT_CROP:
  737. if (!dev->has_crop_cap)
  738. return -EINVAL;
  739. sel->r = dev->crop_cap;
  740. break;
  741. case V4L2_SEL_TGT_CROP_DEFAULT:
  742. case V4L2_SEL_TGT_CROP_BOUNDS:
  743. if (!dev->has_crop_cap)
  744. return -EINVAL;
  745. sel->r = dev->src_rect;
  746. break;
  747. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  748. if (!dev->has_compose_cap)
  749. return -EINVAL;
  750. sel->r = vivid_max_rect;
  751. break;
  752. case V4L2_SEL_TGT_COMPOSE:
  753. if (!dev->has_compose_cap)
  754. return -EINVAL;
  755. sel->r = dev->compose_cap;
  756. break;
  757. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  758. if (!dev->has_compose_cap)
  759. return -EINVAL;
  760. sel->r = dev->fmt_cap_rect;
  761. break;
  762. default:
  763. return -EINVAL;
  764. }
  765. return 0;
  766. }
  767. int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
  768. {
  769. struct vivid_dev *dev = video_drvdata(file);
  770. struct v4l2_rect *crop = &dev->crop_cap;
  771. struct v4l2_rect *compose = &dev->compose_cap;
  772. unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
  773. int ret;
  774. if (!dev->has_crop_cap && !dev->has_compose_cap)
  775. return -ENOTTY;
  776. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  777. return -EINVAL;
  778. if (vivid_is_webcam(dev))
  779. return -ENODATA;
  780. switch (s->target) {
  781. case V4L2_SEL_TGT_CROP:
  782. if (!dev->has_crop_cap)
  783. return -EINVAL;
  784. ret = vivid_vid_adjust_sel(s->flags, &s->r);
  785. if (ret)
  786. return ret;
  787. v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
  788. v4l2_rect_set_max_size(&s->r, &dev->src_rect);
  789. v4l2_rect_map_inside(&s->r, &dev->crop_bounds_cap);
  790. s->r.top /= factor;
  791. s->r.height /= factor;
  792. if (dev->has_scaler_cap) {
  793. struct v4l2_rect fmt = dev->fmt_cap_rect;
  794. struct v4l2_rect max_rect = {
  795. 0, 0,
  796. s->r.width * MAX_ZOOM,
  797. s->r.height * MAX_ZOOM
  798. };
  799. struct v4l2_rect min_rect = {
  800. 0, 0,
  801. s->r.width / MAX_ZOOM,
  802. s->r.height / MAX_ZOOM
  803. };
  804. v4l2_rect_set_min_size(&fmt, &min_rect);
  805. if (!dev->has_compose_cap)
  806. v4l2_rect_set_max_size(&fmt, &max_rect);
  807. if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
  808. vb2_is_busy(&dev->vb_vid_cap_q))
  809. return -EBUSY;
  810. if (dev->has_compose_cap) {
  811. v4l2_rect_set_min_size(compose, &min_rect);
  812. v4l2_rect_set_max_size(compose, &max_rect);
  813. }
  814. dev->fmt_cap_rect = fmt;
  815. tpg_s_buf_height(&dev->tpg, fmt.height);
  816. } else if (dev->has_compose_cap) {
  817. struct v4l2_rect fmt = dev->fmt_cap_rect;
  818. v4l2_rect_set_min_size(&fmt, &s->r);
  819. if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
  820. vb2_is_busy(&dev->vb_vid_cap_q))
  821. return -EBUSY;
  822. dev->fmt_cap_rect = fmt;
  823. tpg_s_buf_height(&dev->tpg, fmt.height);
  824. v4l2_rect_set_size_to(compose, &s->r);
  825. v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
  826. } else {
  827. if (!v4l2_rect_same_size(&s->r, &dev->fmt_cap_rect) &&
  828. vb2_is_busy(&dev->vb_vid_cap_q))
  829. return -EBUSY;
  830. v4l2_rect_set_size_to(&dev->fmt_cap_rect, &s->r);
  831. v4l2_rect_set_size_to(compose, &s->r);
  832. v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
  833. tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
  834. }
  835. s->r.top *= factor;
  836. s->r.height *= factor;
  837. *crop = s->r;
  838. break;
  839. case V4L2_SEL_TGT_COMPOSE:
  840. if (!dev->has_compose_cap)
  841. return -EINVAL;
  842. ret = vivid_vid_adjust_sel(s->flags, &s->r);
  843. if (ret)
  844. return ret;
  845. v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
  846. v4l2_rect_set_max_size(&s->r, &dev->fmt_cap_rect);
  847. if (dev->has_scaler_cap) {
  848. struct v4l2_rect max_rect = {
  849. 0, 0,
  850. dev->src_rect.width * MAX_ZOOM,
  851. (dev->src_rect.height / factor) * MAX_ZOOM
  852. };
  853. v4l2_rect_set_max_size(&s->r, &max_rect);
  854. if (dev->has_crop_cap) {
  855. struct v4l2_rect min_rect = {
  856. 0, 0,
  857. s->r.width / MAX_ZOOM,
  858. (s->r.height * factor) / MAX_ZOOM
  859. };
  860. struct v4l2_rect max_rect = {
  861. 0, 0,
  862. s->r.width * MAX_ZOOM,
  863. (s->r.height * factor) * MAX_ZOOM
  864. };
  865. v4l2_rect_set_min_size(crop, &min_rect);
  866. v4l2_rect_set_max_size(crop, &max_rect);
  867. v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
  868. }
  869. } else if (dev->has_crop_cap) {
  870. s->r.top *= factor;
  871. s->r.height *= factor;
  872. v4l2_rect_set_max_size(&s->r, &dev->src_rect);
  873. v4l2_rect_set_size_to(crop, &s->r);
  874. v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
  875. s->r.top /= factor;
  876. s->r.height /= factor;
  877. } else {
  878. v4l2_rect_set_size_to(&s->r, &dev->src_rect);
  879. s->r.height /= factor;
  880. }
  881. v4l2_rect_map_inside(&s->r, &dev->fmt_cap_rect);
  882. if (dev->bitmap_cap && (compose->width != s->r.width ||
  883. compose->height != s->r.height)) {
  884. kfree(dev->bitmap_cap);
  885. dev->bitmap_cap = NULL;
  886. }
  887. *compose = s->r;
  888. break;
  889. default:
  890. return -EINVAL;
  891. }
  892. tpg_s_crop_compose(&dev->tpg, crop, compose);
  893. return 0;
  894. }
  895. int vivid_vid_cap_cropcap(struct file *file, void *priv,
  896. struct v4l2_cropcap *cap)
  897. {
  898. struct vivid_dev *dev = video_drvdata(file);
  899. if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  900. return -EINVAL;
  901. switch (vivid_get_pixel_aspect(dev)) {
  902. case TPG_PIXEL_ASPECT_NTSC:
  903. cap->pixelaspect.numerator = 11;
  904. cap->pixelaspect.denominator = 10;
  905. break;
  906. case TPG_PIXEL_ASPECT_PAL:
  907. cap->pixelaspect.numerator = 54;
  908. cap->pixelaspect.denominator = 59;
  909. break;
  910. case TPG_PIXEL_ASPECT_SQUARE:
  911. cap->pixelaspect.numerator = 1;
  912. cap->pixelaspect.denominator = 1;
  913. break;
  914. }
  915. return 0;
  916. }
  917. int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
  918. struct v4l2_fmtdesc *f)
  919. {
  920. struct vivid_dev *dev = video_drvdata(file);
  921. const struct vivid_fmt *fmt;
  922. if (dev->multiplanar)
  923. return -ENOTTY;
  924. if (f->index >= ARRAY_SIZE(formats_ovl))
  925. return -EINVAL;
  926. fmt = &formats_ovl[f->index];
  927. f->pixelformat = fmt->fourcc;
  928. return 0;
  929. }
  930. int vidioc_g_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. unsigned clipcount = win->clipcount;
  937. if (dev->multiplanar)
  938. return -ENOTTY;
  939. win->w.top = dev->overlay_cap_top;
  940. win->w.left = dev->overlay_cap_left;
  941. win->w.width = compose->width;
  942. win->w.height = compose->height;
  943. win->field = dev->overlay_cap_field;
  944. win->clipcount = dev->clipcount_cap;
  945. if (clipcount > dev->clipcount_cap)
  946. clipcount = dev->clipcount_cap;
  947. if (dev->bitmap_cap == NULL)
  948. win->bitmap = NULL;
  949. else if (win->bitmap) {
  950. if (copy_to_user(win->bitmap, dev->bitmap_cap,
  951. ((compose->width + 7) / 8) * compose->height))
  952. return -EFAULT;
  953. }
  954. if (clipcount && win->clips) {
  955. if (copy_to_user(win->clips, dev->clips_cap,
  956. clipcount * sizeof(dev->clips_cap[0])))
  957. return -EFAULT;
  958. }
  959. return 0;
  960. }
  961. int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
  962. struct v4l2_format *f)
  963. {
  964. struct vivid_dev *dev = video_drvdata(file);
  965. const struct v4l2_rect *compose = &dev->compose_cap;
  966. struct v4l2_window *win = &f->fmt.win;
  967. int i, j;
  968. if (dev->multiplanar)
  969. return -ENOTTY;
  970. win->w.left = clamp_t(int, win->w.left,
  971. -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
  972. win->w.top = clamp_t(int, win->w.top,
  973. -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
  974. win->w.width = compose->width;
  975. win->w.height = compose->height;
  976. if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
  977. win->field = V4L2_FIELD_ANY;
  978. win->chromakey = 0;
  979. win->global_alpha = 0;
  980. if (win->clipcount && !win->clips)
  981. win->clipcount = 0;
  982. if (win->clipcount > MAX_CLIPS)
  983. win->clipcount = MAX_CLIPS;
  984. if (win->clipcount) {
  985. if (copy_from_user(dev->try_clips_cap, win->clips,
  986. win->clipcount * sizeof(dev->clips_cap[0])))
  987. return -EFAULT;
  988. for (i = 0; i < win->clipcount; i++) {
  989. struct v4l2_rect *r = &dev->try_clips_cap[i].c;
  990. r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
  991. r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
  992. r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
  993. r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
  994. }
  995. /*
  996. * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
  997. * number and it's typically a one-time deal.
  998. */
  999. for (i = 0; i < win->clipcount - 1; i++) {
  1000. struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
  1001. for (j = i + 1; j < win->clipcount; j++) {
  1002. struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
  1003. if (v4l2_rect_overlap(r1, r2))
  1004. return -EINVAL;
  1005. }
  1006. }
  1007. if (copy_to_user(win->clips, dev->try_clips_cap,
  1008. win->clipcount * sizeof(dev->clips_cap[0])))
  1009. return -EFAULT;
  1010. }
  1011. return 0;
  1012. }
  1013. int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
  1014. struct v4l2_format *f)
  1015. {
  1016. struct vivid_dev *dev = video_drvdata(file);
  1017. const struct v4l2_rect *compose = &dev->compose_cap;
  1018. struct v4l2_window *win = &f->fmt.win;
  1019. int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
  1020. unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
  1021. unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
  1022. void *new_bitmap = NULL;
  1023. if (ret)
  1024. return ret;
  1025. if (win->bitmap) {
  1026. new_bitmap = vzalloc(bitmap_size);
  1027. if (new_bitmap == NULL)
  1028. return -ENOMEM;
  1029. if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
  1030. vfree(new_bitmap);
  1031. return -EFAULT;
  1032. }
  1033. }
  1034. dev->overlay_cap_top = win->w.top;
  1035. dev->overlay_cap_left = win->w.left;
  1036. dev->overlay_cap_field = win->field;
  1037. vfree(dev->bitmap_cap);
  1038. dev->bitmap_cap = new_bitmap;
  1039. dev->clipcount_cap = win->clipcount;
  1040. if (dev->clipcount_cap)
  1041. memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
  1042. return 0;
  1043. }
  1044. int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
  1045. {
  1046. struct vivid_dev *dev = video_drvdata(file);
  1047. if (dev->multiplanar)
  1048. return -ENOTTY;
  1049. if (i && dev->fb_vbase_cap == NULL)
  1050. return -EINVAL;
  1051. if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
  1052. dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
  1053. return -EINVAL;
  1054. }
  1055. if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
  1056. return -EBUSY;
  1057. dev->overlay_cap_owner = i ? fh : NULL;
  1058. return 0;
  1059. }
  1060. int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
  1061. struct v4l2_framebuffer *a)
  1062. {
  1063. struct vivid_dev *dev = video_drvdata(file);
  1064. if (dev->multiplanar)
  1065. return -ENOTTY;
  1066. *a = dev->fb_cap;
  1067. a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
  1068. V4L2_FBUF_CAP_LIST_CLIPPING;
  1069. a->flags = V4L2_FBUF_FLAG_PRIMARY;
  1070. a->fmt.field = V4L2_FIELD_NONE;
  1071. a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  1072. a->fmt.priv = 0;
  1073. return 0;
  1074. }
  1075. int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
  1076. const struct v4l2_framebuffer *a)
  1077. {
  1078. struct vivid_dev *dev = video_drvdata(file);
  1079. const struct vivid_fmt *fmt;
  1080. if (dev->multiplanar)
  1081. return -ENOTTY;
  1082. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  1083. return -EPERM;
  1084. if (dev->overlay_cap_owner)
  1085. return -EBUSY;
  1086. if (a->base == NULL) {
  1087. dev->fb_cap.base = NULL;
  1088. dev->fb_vbase_cap = NULL;
  1089. return 0;
  1090. }
  1091. if (a->fmt.width < 48 || a->fmt.height < 32)
  1092. return -EINVAL;
  1093. fmt = vivid_get_format(dev, a->fmt.pixelformat);
  1094. if (!fmt || !fmt->can_do_overlay)
  1095. return -EINVAL;
  1096. if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
  1097. return -EINVAL;
  1098. if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage)
  1099. return -EINVAL;
  1100. dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
  1101. dev->fb_cap = *a;
  1102. dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
  1103. -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
  1104. dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
  1105. -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
  1106. return 0;
  1107. }
  1108. static const struct v4l2_audio vivid_audio_inputs[] = {
  1109. { 0, "TV", V4L2_AUDCAP_STEREO },
  1110. { 1, "Line-In", V4L2_AUDCAP_STEREO },
  1111. };
  1112. int vidioc_enum_input(struct file *file, void *priv,
  1113. struct v4l2_input *inp)
  1114. {
  1115. struct vivid_dev *dev = video_drvdata(file);
  1116. if (inp->index >= dev->num_inputs)
  1117. return -EINVAL;
  1118. inp->type = V4L2_INPUT_TYPE_CAMERA;
  1119. switch (dev->input_type[inp->index]) {
  1120. case WEBCAM:
  1121. snprintf(inp->name, sizeof(inp->name), "Webcam %u",
  1122. dev->input_name_counter[inp->index]);
  1123. inp->capabilities = 0;
  1124. break;
  1125. case TV:
  1126. snprintf(inp->name, sizeof(inp->name), "TV %u",
  1127. dev->input_name_counter[inp->index]);
  1128. inp->type = V4L2_INPUT_TYPE_TUNER;
  1129. inp->std = V4L2_STD_ALL;
  1130. if (dev->has_audio_inputs)
  1131. inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
  1132. inp->capabilities = V4L2_IN_CAP_STD;
  1133. break;
  1134. case SVID:
  1135. snprintf(inp->name, sizeof(inp->name), "S-Video %u",
  1136. dev->input_name_counter[inp->index]);
  1137. inp->std = V4L2_STD_ALL;
  1138. if (dev->has_audio_inputs)
  1139. inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
  1140. inp->capabilities = V4L2_IN_CAP_STD;
  1141. break;
  1142. case HDMI:
  1143. snprintf(inp->name, sizeof(inp->name), "HDMI %u",
  1144. dev->input_name_counter[inp->index]);
  1145. inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
  1146. if (dev->edid_blocks == 0 ||
  1147. dev->dv_timings_signal_mode == NO_SIGNAL)
  1148. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  1149. else if (dev->dv_timings_signal_mode == NO_LOCK ||
  1150. dev->dv_timings_signal_mode == OUT_OF_RANGE)
  1151. inp->status |= V4L2_IN_ST_NO_H_LOCK;
  1152. break;
  1153. }
  1154. if (dev->sensor_hflip)
  1155. inp->status |= V4L2_IN_ST_HFLIP;
  1156. if (dev->sensor_vflip)
  1157. inp->status |= V4L2_IN_ST_VFLIP;
  1158. if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
  1159. if (dev->std_signal_mode == NO_SIGNAL) {
  1160. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  1161. } else if (dev->std_signal_mode == NO_LOCK) {
  1162. inp->status |= V4L2_IN_ST_NO_H_LOCK;
  1163. } else if (vivid_is_tv_cap(dev)) {
  1164. switch (tpg_g_quality(&dev->tpg)) {
  1165. case TPG_QUAL_GRAY:
  1166. inp->status |= V4L2_IN_ST_COLOR_KILL;
  1167. break;
  1168. case TPG_QUAL_NOISE:
  1169. inp->status |= V4L2_IN_ST_NO_H_LOCK;
  1170. break;
  1171. default:
  1172. break;
  1173. }
  1174. }
  1175. }
  1176. return 0;
  1177. }
  1178. int vidioc_g_input(struct file *file, void *priv, unsigned *i)
  1179. {
  1180. struct vivid_dev *dev = video_drvdata(file);
  1181. *i = dev->input;
  1182. return 0;
  1183. }
  1184. int vidioc_s_input(struct file *file, void *priv, unsigned i)
  1185. {
  1186. struct vivid_dev *dev = video_drvdata(file);
  1187. struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
  1188. unsigned brightness;
  1189. if (i >= dev->num_inputs)
  1190. return -EINVAL;
  1191. if (i == dev->input)
  1192. return 0;
  1193. if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
  1194. return -EBUSY;
  1195. dev->input = i;
  1196. dev->vid_cap_dev.tvnorms = 0;
  1197. if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
  1198. dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
  1199. dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
  1200. }
  1201. dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
  1202. vivid_update_format_cap(dev, false);
  1203. if (dev->colorspace) {
  1204. switch (dev->input_type[i]) {
  1205. case WEBCAM:
  1206. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
  1207. break;
  1208. case TV:
  1209. case SVID:
  1210. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
  1211. break;
  1212. case HDMI:
  1213. if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
  1214. if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
  1215. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
  1216. else
  1217. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
  1218. } else {
  1219. v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
  1220. }
  1221. break;
  1222. }
  1223. }
  1224. /*
  1225. * Modify the brightness range depending on the input.
  1226. * This makes it easy to use vivid to test if applications can
  1227. * handle control range modifications and is also how this is
  1228. * typically used in practice as different inputs may be hooked
  1229. * up to different receivers with different control ranges.
  1230. */
  1231. brightness = 128 * i + dev->input_brightness[i];
  1232. v4l2_ctrl_modify_range(dev->brightness,
  1233. 128 * i, 255 + 128 * i, 1, 128 + 128 * i);
  1234. v4l2_ctrl_s_ctrl(dev->brightness, brightness);
  1235. return 0;
  1236. }
  1237. int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
  1238. {
  1239. if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
  1240. return -EINVAL;
  1241. *vin = vivid_audio_inputs[vin->index];
  1242. return 0;
  1243. }
  1244. int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
  1245. {
  1246. struct vivid_dev *dev = video_drvdata(file);
  1247. if (!vivid_is_sdtv_cap(dev))
  1248. return -EINVAL;
  1249. *vin = vivid_audio_inputs[dev->tv_audio_input];
  1250. return 0;
  1251. }
  1252. int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
  1253. {
  1254. struct vivid_dev *dev = video_drvdata(file);
  1255. if (!vivid_is_sdtv_cap(dev))
  1256. return -EINVAL;
  1257. if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
  1258. return -EINVAL;
  1259. dev->tv_audio_input = vin->index;
  1260. return 0;
  1261. }
  1262. int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
  1263. {
  1264. struct vivid_dev *dev = video_drvdata(file);
  1265. if (vf->tuner != 0)
  1266. return -EINVAL;
  1267. vf->frequency = dev->tv_freq;
  1268. return 0;
  1269. }
  1270. int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
  1271. {
  1272. struct vivid_dev *dev = video_drvdata(file);
  1273. if (vf->tuner != 0)
  1274. return -EINVAL;
  1275. dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
  1276. if (vivid_is_tv_cap(dev))
  1277. vivid_update_quality(dev);
  1278. return 0;
  1279. }
  1280. int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
  1281. {
  1282. struct vivid_dev *dev = video_drvdata(file);
  1283. if (vt->index != 0)
  1284. return -EINVAL;
  1285. if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
  1286. return -EINVAL;
  1287. dev->tv_audmode = vt->audmode;
  1288. return 0;
  1289. }
  1290. int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  1291. {
  1292. struct vivid_dev *dev = video_drvdata(file);
  1293. enum tpg_quality qual;
  1294. if (vt->index != 0)
  1295. return -EINVAL;
  1296. vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
  1297. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  1298. vt->audmode = dev->tv_audmode;
  1299. vt->rangelow = MIN_TV_FREQ;
  1300. vt->rangehigh = MAX_TV_FREQ;
  1301. qual = vivid_get_quality(dev, &vt->afc);
  1302. if (qual == TPG_QUAL_COLOR)
  1303. vt->signal = 0xffff;
  1304. else if (qual == TPG_QUAL_GRAY)
  1305. vt->signal = 0x8000;
  1306. else
  1307. vt->signal = 0;
  1308. if (qual == TPG_QUAL_NOISE) {
  1309. vt->rxsubchans = 0;
  1310. } else if (qual == TPG_QUAL_GRAY) {
  1311. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  1312. } else {
  1313. unsigned channel_nr = dev->tv_freq / (6 * 16);
  1314. unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
  1315. switch (channel_nr % options) {
  1316. case 0:
  1317. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  1318. break;
  1319. case 1:
  1320. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1321. break;
  1322. case 2:
  1323. if (dev->std_cap & V4L2_STD_NTSC_M)
  1324. vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
  1325. else
  1326. vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  1327. break;
  1328. case 3:
  1329. vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
  1330. break;
  1331. }
  1332. }
  1333. strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
  1334. return 0;
  1335. }
  1336. /* Must remain in sync with the vivid_ctrl_standard_strings array */
  1337. const v4l2_std_id vivid_standard[] = {
  1338. V4L2_STD_NTSC_M,
  1339. V4L2_STD_NTSC_M_JP,
  1340. V4L2_STD_NTSC_M_KR,
  1341. V4L2_STD_NTSC_443,
  1342. V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
  1343. V4L2_STD_PAL_I,
  1344. V4L2_STD_PAL_DK,
  1345. V4L2_STD_PAL_M,
  1346. V4L2_STD_PAL_N,
  1347. V4L2_STD_PAL_Nc,
  1348. V4L2_STD_PAL_60,
  1349. V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
  1350. V4L2_STD_SECAM_DK,
  1351. V4L2_STD_SECAM_L,
  1352. V4L2_STD_SECAM_LC,
  1353. V4L2_STD_UNKNOWN
  1354. };
  1355. /* Must remain in sync with the vivid_standard array */
  1356. const char * const vivid_ctrl_standard_strings[] = {
  1357. "NTSC-M",
  1358. "NTSC-M-JP",
  1359. "NTSC-M-KR",
  1360. "NTSC-443",
  1361. "PAL-BGH",
  1362. "PAL-I",
  1363. "PAL-DK",
  1364. "PAL-M",
  1365. "PAL-N",
  1366. "PAL-Nc",
  1367. "PAL-60",
  1368. "SECAM-BGH",
  1369. "SECAM-DK",
  1370. "SECAM-L",
  1371. "SECAM-Lc",
  1372. NULL,
  1373. };
  1374. int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
  1375. {
  1376. struct vivid_dev *dev = video_drvdata(file);
  1377. if (!vivid_is_sdtv_cap(dev))
  1378. return -ENODATA;
  1379. if (dev->std_signal_mode == NO_SIGNAL ||
  1380. dev->std_signal_mode == NO_LOCK) {
  1381. *id = V4L2_STD_UNKNOWN;
  1382. return 0;
  1383. }
  1384. if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
  1385. *id = V4L2_STD_UNKNOWN;
  1386. } else if (dev->std_signal_mode == CURRENT_STD) {
  1387. *id = dev->std_cap;
  1388. } else if (dev->std_signal_mode == SELECTED_STD) {
  1389. *id = dev->query_std;
  1390. } else {
  1391. *id = vivid_standard[dev->query_std_last];
  1392. dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
  1393. }
  1394. return 0;
  1395. }
  1396. int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
  1397. {
  1398. struct vivid_dev *dev = video_drvdata(file);
  1399. if (!vivid_is_sdtv_cap(dev))
  1400. return -ENODATA;
  1401. if (dev->std_cap == id)
  1402. return 0;
  1403. if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
  1404. return -EBUSY;
  1405. dev->std_cap = id;
  1406. vivid_update_format_cap(dev, false);
  1407. return 0;
  1408. }
  1409. static void find_aspect_ratio(u32 width, u32 height,
  1410. u32 *num, u32 *denom)
  1411. {
  1412. if (!(height % 3) && ((height * 4 / 3) == width)) {
  1413. *num = 4;
  1414. *denom = 3;
  1415. } else if (!(height % 9) && ((height * 16 / 9) == width)) {
  1416. *num = 16;
  1417. *denom = 9;
  1418. } else if (!(height % 10) && ((height * 16 / 10) == width)) {
  1419. *num = 16;
  1420. *denom = 10;
  1421. } else if (!(height % 4) && ((height * 5 / 4) == width)) {
  1422. *num = 5;
  1423. *denom = 4;
  1424. } else if (!(height % 9) && ((height * 15 / 9) == width)) {
  1425. *num = 15;
  1426. *denom = 9;
  1427. } else { /* default to 16:9 */
  1428. *num = 16;
  1429. *denom = 9;
  1430. }
  1431. }
  1432. static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
  1433. {
  1434. struct v4l2_bt_timings *bt = &timings->bt;
  1435. u32 total_h_pixel;
  1436. u32 total_v_lines;
  1437. u32 h_freq;
  1438. if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
  1439. NULL, NULL))
  1440. return false;
  1441. total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
  1442. total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
  1443. h_freq = (u32)bt->pixelclock / total_h_pixel;
  1444. if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
  1445. if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
  1446. bt->polarities, bt->interlaced, timings))
  1447. return true;
  1448. }
  1449. if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
  1450. struct v4l2_fract aspect_ratio;
  1451. find_aspect_ratio(bt->width, bt->height,
  1452. &aspect_ratio.numerator,
  1453. &aspect_ratio.denominator);
  1454. if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
  1455. bt->polarities, bt->interlaced,
  1456. aspect_ratio, timings))
  1457. return true;
  1458. }
  1459. return false;
  1460. }
  1461. int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
  1462. struct v4l2_dv_timings *timings)
  1463. {
  1464. struct vivid_dev *dev = video_drvdata(file);
  1465. if (!vivid_is_hdmi_cap(dev))
  1466. return -ENODATA;
  1467. if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
  1468. 0, NULL, NULL) &&
  1469. !valid_cvt_gtf_timings(timings))
  1470. return -EINVAL;
  1471. if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0, false))
  1472. return 0;
  1473. if (vb2_is_busy(&dev->vb_vid_cap_q))
  1474. return -EBUSY;
  1475. dev->dv_timings_cap = *timings;
  1476. vivid_update_format_cap(dev, false);
  1477. return 0;
  1478. }
  1479. int vidioc_query_dv_timings(struct file *file, void *_fh,
  1480. struct v4l2_dv_timings *timings)
  1481. {
  1482. struct vivid_dev *dev = video_drvdata(file);
  1483. if (!vivid_is_hdmi_cap(dev))
  1484. return -ENODATA;
  1485. if (dev->dv_timings_signal_mode == NO_SIGNAL ||
  1486. dev->edid_blocks == 0)
  1487. return -ENOLINK;
  1488. if (dev->dv_timings_signal_mode == NO_LOCK)
  1489. return -ENOLCK;
  1490. if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
  1491. timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
  1492. return -ERANGE;
  1493. }
  1494. if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
  1495. *timings = dev->dv_timings_cap;
  1496. } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
  1497. *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
  1498. } else {
  1499. *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
  1500. dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
  1501. dev->query_dv_timings_size;
  1502. }
  1503. return 0;
  1504. }
  1505. int vidioc_s_edid(struct file *file, void *_fh,
  1506. struct v4l2_edid *edid)
  1507. {
  1508. struct vivid_dev *dev = video_drvdata(file);
  1509. u16 phys_addr;
  1510. unsigned int i;
  1511. int ret;
  1512. memset(edid->reserved, 0, sizeof(edid->reserved));
  1513. if (edid->pad >= dev->num_inputs)
  1514. return -EINVAL;
  1515. if (dev->input_type[edid->pad] != HDMI || edid->start_block)
  1516. return -EINVAL;
  1517. if (edid->blocks == 0) {
  1518. dev->edid_blocks = 0;
  1519. phys_addr = CEC_PHYS_ADDR_INVALID;
  1520. goto set_phys_addr;
  1521. }
  1522. if (edid->blocks > dev->edid_max_blocks) {
  1523. edid->blocks = dev->edid_max_blocks;
  1524. return -E2BIG;
  1525. }
  1526. phys_addr = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
  1527. ret = cec_phys_addr_validate(phys_addr, &phys_addr, NULL);
  1528. if (ret)
  1529. return ret;
  1530. if (vb2_is_busy(&dev->vb_vid_cap_q))
  1531. return -EBUSY;
  1532. dev->edid_blocks = edid->blocks;
  1533. memcpy(dev->edid, edid->edid, edid->blocks * 128);
  1534. set_phys_addr:
  1535. /* TODO: a proper hotplug detect cycle should be emulated here */
  1536. cec_s_phys_addr(dev->cec_rx_adap, phys_addr, false);
  1537. for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
  1538. cec_s_phys_addr(dev->cec_tx_adap[i],
  1539. cec_phys_addr_for_input(phys_addr, i + 1),
  1540. false);
  1541. return 0;
  1542. }
  1543. int vidioc_enum_framesizes(struct file *file, void *fh,
  1544. struct v4l2_frmsizeenum *fsize)
  1545. {
  1546. struct vivid_dev *dev = video_drvdata(file);
  1547. if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
  1548. return -EINVAL;
  1549. if (vivid_get_format(dev, fsize->pixel_format) == NULL)
  1550. return -EINVAL;
  1551. if (vivid_is_webcam(dev)) {
  1552. if (fsize->index >= ARRAY_SIZE(webcam_sizes))
  1553. return -EINVAL;
  1554. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1555. fsize->discrete = webcam_sizes[fsize->index];
  1556. return 0;
  1557. }
  1558. if (fsize->index)
  1559. return -EINVAL;
  1560. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1561. fsize->stepwise.min_width = MIN_WIDTH;
  1562. fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
  1563. fsize->stepwise.step_width = 2;
  1564. fsize->stepwise.min_height = MIN_HEIGHT;
  1565. fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
  1566. fsize->stepwise.step_height = 2;
  1567. return 0;
  1568. }
  1569. /* timeperframe is arbitrary and continuous */
  1570. int vidioc_enum_frameintervals(struct file *file, void *priv,
  1571. struct v4l2_frmivalenum *fival)
  1572. {
  1573. struct vivid_dev *dev = video_drvdata(file);
  1574. const struct vivid_fmt *fmt;
  1575. int i;
  1576. fmt = vivid_get_format(dev, fival->pixel_format);
  1577. if (!fmt)
  1578. return -EINVAL;
  1579. if (!vivid_is_webcam(dev)) {
  1580. if (fival->index)
  1581. return -EINVAL;
  1582. if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
  1583. return -EINVAL;
  1584. if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
  1585. return -EINVAL;
  1586. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1587. fival->discrete = dev->timeperframe_vid_cap;
  1588. return 0;
  1589. }
  1590. for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
  1591. if (fival->width == webcam_sizes[i].width &&
  1592. fival->height == webcam_sizes[i].height)
  1593. break;
  1594. if (i == ARRAY_SIZE(webcam_sizes))
  1595. return -EINVAL;
  1596. if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
  1597. return -EINVAL;
  1598. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1599. fival->discrete = webcam_intervals[fival->index];
  1600. return 0;
  1601. }
  1602. int vivid_vid_cap_g_parm(struct file *file, void *priv,
  1603. struct v4l2_streamparm *parm)
  1604. {
  1605. struct vivid_dev *dev = video_drvdata(file);
  1606. if (parm->type != (dev->multiplanar ?
  1607. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
  1608. V4L2_BUF_TYPE_VIDEO_CAPTURE))
  1609. return -EINVAL;
  1610. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1611. parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
  1612. parm->parm.capture.readbuffers = 1;
  1613. return 0;
  1614. }
  1615. #define FRACT_CMP(a, OP, b) \
  1616. ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
  1617. int vivid_vid_cap_s_parm(struct file *file, void *priv,
  1618. struct v4l2_streamparm *parm)
  1619. {
  1620. struct vivid_dev *dev = video_drvdata(file);
  1621. unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
  1622. struct v4l2_fract tpf;
  1623. unsigned i;
  1624. if (parm->type != (dev->multiplanar ?
  1625. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
  1626. V4L2_BUF_TYPE_VIDEO_CAPTURE))
  1627. return -EINVAL;
  1628. if (!vivid_is_webcam(dev))
  1629. return vivid_vid_cap_g_parm(file, priv, parm);
  1630. tpf = parm->parm.capture.timeperframe;
  1631. if (tpf.denominator == 0)
  1632. tpf = webcam_intervals[ival_sz - 1];
  1633. for (i = 0; i < ival_sz; i++)
  1634. if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
  1635. break;
  1636. if (i == ival_sz)
  1637. i = ival_sz - 1;
  1638. dev->webcam_ival_idx = i;
  1639. tpf = webcam_intervals[dev->webcam_ival_idx];
  1640. tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
  1641. tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
  1642. /* resync the thread's timings */
  1643. dev->cap_seq_resync = true;
  1644. dev->timeperframe_vid_cap = tpf;
  1645. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1646. parm->parm.capture.timeperframe = tpf;
  1647. parm->parm.capture.readbuffers = 1;
  1648. return 0;
  1649. }