v4l2-ioctl.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324
  1. /*
  2. * Video capture interface for Linux version 2
  3. *
  4. * A generic framework to process V4L2 ioctl commands.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
  12. * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
  13. */
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/version.h>
  19. #include <linux/videodev2.h>
  20. #include <media/v4l2-common.h>
  21. #include <media/v4l2-ioctl.h>
  22. #include <media/v4l2-ctrls.h>
  23. #include <media/v4l2-fh.h>
  24. #include <media/v4l2-event.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-chip-ident.h>
  27. #include <media/videobuf2-core.h>
  28. /* Zero out the end of the struct pointed to by p. Everything after, but
  29. * not including, the specified field is cleared. */
  30. #define CLEAR_AFTER_FIELD(p, field) \
  31. memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
  32. 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
  33. struct std_descr {
  34. v4l2_std_id std;
  35. const char *descr;
  36. };
  37. static const struct std_descr standards[] = {
  38. { V4L2_STD_NTSC, "NTSC" },
  39. { V4L2_STD_NTSC_M, "NTSC-M" },
  40. { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
  41. { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
  42. { V4L2_STD_NTSC_443, "NTSC-443" },
  43. { V4L2_STD_PAL, "PAL" },
  44. { V4L2_STD_PAL_BG, "PAL-BG" },
  45. { V4L2_STD_PAL_B, "PAL-B" },
  46. { V4L2_STD_PAL_B1, "PAL-B1" },
  47. { V4L2_STD_PAL_G, "PAL-G" },
  48. { V4L2_STD_PAL_H, "PAL-H" },
  49. { V4L2_STD_PAL_I, "PAL-I" },
  50. { V4L2_STD_PAL_DK, "PAL-DK" },
  51. { V4L2_STD_PAL_D, "PAL-D" },
  52. { V4L2_STD_PAL_D1, "PAL-D1" },
  53. { V4L2_STD_PAL_K, "PAL-K" },
  54. { V4L2_STD_PAL_M, "PAL-M" },
  55. { V4L2_STD_PAL_N, "PAL-N" },
  56. { V4L2_STD_PAL_Nc, "PAL-Nc" },
  57. { V4L2_STD_PAL_60, "PAL-60" },
  58. { V4L2_STD_SECAM, "SECAM" },
  59. { V4L2_STD_SECAM_B, "SECAM-B" },
  60. { V4L2_STD_SECAM_G, "SECAM-G" },
  61. { V4L2_STD_SECAM_H, "SECAM-H" },
  62. { V4L2_STD_SECAM_DK, "SECAM-DK" },
  63. { V4L2_STD_SECAM_D, "SECAM-D" },
  64. { V4L2_STD_SECAM_K, "SECAM-K" },
  65. { V4L2_STD_SECAM_K1, "SECAM-K1" },
  66. { V4L2_STD_SECAM_L, "SECAM-L" },
  67. { V4L2_STD_SECAM_LC, "SECAM-Lc" },
  68. { 0, "Unknown" }
  69. };
  70. /* video4linux standard ID conversion to standard name
  71. */
  72. const char *v4l2_norm_to_name(v4l2_std_id id)
  73. {
  74. u32 myid = id;
  75. int i;
  76. /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
  77. 64 bit comparations. So, on that architecture, with some gcc
  78. variants, compilation fails. Currently, the max value is 30bit wide.
  79. */
  80. BUG_ON(myid != id);
  81. for (i = 0; standards[i].std; i++)
  82. if (myid == standards[i].std)
  83. break;
  84. return standards[i].descr;
  85. }
  86. EXPORT_SYMBOL(v4l2_norm_to_name);
  87. /* Returns frame period for the given standard */
  88. void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
  89. {
  90. if (id & V4L2_STD_525_60) {
  91. frameperiod->numerator = 1001;
  92. frameperiod->denominator = 30000;
  93. } else {
  94. frameperiod->numerator = 1;
  95. frameperiod->denominator = 25;
  96. }
  97. }
  98. EXPORT_SYMBOL(v4l2_video_std_frame_period);
  99. /* Fill in the fields of a v4l2_standard structure according to the
  100. 'id' and 'transmission' parameters. Returns negative on error. */
  101. int v4l2_video_std_construct(struct v4l2_standard *vs,
  102. int id, const char *name)
  103. {
  104. vs->id = id;
  105. v4l2_video_std_frame_period(id, &vs->frameperiod);
  106. vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
  107. strlcpy(vs->name, name, sizeof(vs->name));
  108. return 0;
  109. }
  110. EXPORT_SYMBOL(v4l2_video_std_construct);
  111. /* ----------------------------------------------------------------- */
  112. /* some arrays for pretty-printing debug messages of enum types */
  113. const char *v4l2_field_names[] = {
  114. [V4L2_FIELD_ANY] = "any",
  115. [V4L2_FIELD_NONE] = "none",
  116. [V4L2_FIELD_TOP] = "top",
  117. [V4L2_FIELD_BOTTOM] = "bottom",
  118. [V4L2_FIELD_INTERLACED] = "interlaced",
  119. [V4L2_FIELD_SEQ_TB] = "seq-tb",
  120. [V4L2_FIELD_SEQ_BT] = "seq-bt",
  121. [V4L2_FIELD_ALTERNATE] = "alternate",
  122. [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
  123. [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
  124. };
  125. EXPORT_SYMBOL(v4l2_field_names);
  126. const char *v4l2_type_names[] = {
  127. [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
  128. [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
  129. [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
  130. [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
  131. [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
  132. [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
  133. [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
  134. [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
  135. [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
  136. [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
  137. };
  138. EXPORT_SYMBOL(v4l2_type_names);
  139. static const char *v4l2_memory_names[] = {
  140. [V4L2_MEMORY_MMAP] = "mmap",
  141. [V4L2_MEMORY_USERPTR] = "userptr",
  142. [V4L2_MEMORY_OVERLAY] = "overlay",
  143. };
  144. #define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
  145. arr[a] : "unknown")
  146. /* ------------------------------------------------------------------ */
  147. /* debug help functions */
  148. static void v4l_print_querycap(const void *arg, bool write_only)
  149. {
  150. const struct v4l2_capability *p = arg;
  151. pr_cont("driver=%s, card=%s, bus=%s, version=0x%08x, "
  152. "capabilities=0x%08x, device_caps=0x%08x\n",
  153. p->driver, p->card, p->bus_info,
  154. p->version, p->capabilities, p->device_caps);
  155. }
  156. static void v4l_print_enuminput(const void *arg, bool write_only)
  157. {
  158. const struct v4l2_input *p = arg;
  159. pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, tuner=%u, "
  160. "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
  161. p->index, p->name, p->type, p->audioset, p->tuner,
  162. (unsigned long long)p->std, p->status, p->capabilities);
  163. }
  164. static void v4l_print_enumoutput(const void *arg, bool write_only)
  165. {
  166. const struct v4l2_output *p = arg;
  167. pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, "
  168. "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
  169. p->index, p->name, p->type, p->audioset, p->modulator,
  170. (unsigned long long)p->std, p->capabilities);
  171. }
  172. static void v4l_print_audio(const void *arg, bool write_only)
  173. {
  174. const struct v4l2_audio *p = arg;
  175. if (write_only)
  176. pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
  177. else
  178. pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
  179. p->index, p->name, p->capability, p->mode);
  180. }
  181. static void v4l_print_audioout(const void *arg, bool write_only)
  182. {
  183. const struct v4l2_audioout *p = arg;
  184. if (write_only)
  185. pr_cont("index=%u\n", p->index);
  186. else
  187. pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
  188. p->index, p->name, p->capability, p->mode);
  189. }
  190. static void v4l_print_fmtdesc(const void *arg, bool write_only)
  191. {
  192. const struct v4l2_fmtdesc *p = arg;
  193. pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%s'\n",
  194. p->index, prt_names(p->type, v4l2_type_names),
  195. p->flags, (p->pixelformat & 0xff),
  196. (p->pixelformat >> 8) & 0xff,
  197. (p->pixelformat >> 16) & 0xff,
  198. (p->pixelformat >> 24) & 0xff,
  199. p->description);
  200. }
  201. static void v4l_print_format(const void *arg, bool write_only)
  202. {
  203. const struct v4l2_format *p = arg;
  204. const struct v4l2_pix_format *pix;
  205. const struct v4l2_pix_format_mplane *mp;
  206. const struct v4l2_vbi_format *vbi;
  207. const struct v4l2_sliced_vbi_format *sliced;
  208. const struct v4l2_window *win;
  209. const struct v4l2_clip *clip;
  210. unsigned i;
  211. pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
  212. switch (p->type) {
  213. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  214. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  215. pix = &p->fmt.pix;
  216. pr_cont(", width=%u, height=%u, "
  217. "pixelformat=%c%c%c%c, field=%s, "
  218. "bytesperline=%u sizeimage=%u, colorspace=%d\n",
  219. pix->width, pix->height,
  220. (pix->pixelformat & 0xff),
  221. (pix->pixelformat >> 8) & 0xff,
  222. (pix->pixelformat >> 16) & 0xff,
  223. (pix->pixelformat >> 24) & 0xff,
  224. prt_names(pix->field, v4l2_field_names),
  225. pix->bytesperline, pix->sizeimage,
  226. pix->colorspace);
  227. break;
  228. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  229. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  230. mp = &p->fmt.pix_mp;
  231. pr_cont(", width=%u, height=%u, "
  232. "format=%c%c%c%c, field=%s, "
  233. "colorspace=%d, num_planes=%u\n",
  234. mp->width, mp->height,
  235. (mp->pixelformat & 0xff),
  236. (mp->pixelformat >> 8) & 0xff,
  237. (mp->pixelformat >> 16) & 0xff,
  238. (mp->pixelformat >> 24) & 0xff,
  239. prt_names(mp->field, v4l2_field_names),
  240. mp->colorspace, mp->num_planes);
  241. for (i = 0; i < mp->num_planes; i++)
  242. printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
  243. mp->plane_fmt[i].bytesperline,
  244. mp->plane_fmt[i].sizeimage);
  245. break;
  246. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  247. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  248. win = &p->fmt.win;
  249. pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, "
  250. "chromakey=0x%08x, bitmap=%p, "
  251. "global_alpha=0x%02x\n",
  252. win->w.width, win->w.height,
  253. win->w.left, win->w.top,
  254. prt_names(win->field, v4l2_field_names),
  255. win->chromakey, win->bitmap, win->global_alpha);
  256. clip = win->clips;
  257. for (i = 0; i < win->clipcount; i++) {
  258. printk(KERN_DEBUG "clip %u: wxh=%dx%d, x,y=%d,%d\n",
  259. i, clip->c.width, clip->c.height,
  260. clip->c.left, clip->c.top);
  261. clip = clip->next;
  262. }
  263. break;
  264. case V4L2_BUF_TYPE_VBI_CAPTURE:
  265. case V4L2_BUF_TYPE_VBI_OUTPUT:
  266. vbi = &p->fmt.vbi;
  267. pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
  268. "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
  269. vbi->sampling_rate, vbi->offset,
  270. vbi->samples_per_line,
  271. (vbi->sample_format & 0xff),
  272. (vbi->sample_format >> 8) & 0xff,
  273. (vbi->sample_format >> 16) & 0xff,
  274. (vbi->sample_format >> 24) & 0xff,
  275. vbi->start[0], vbi->start[1],
  276. vbi->count[0], vbi->count[1]);
  277. break;
  278. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  279. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  280. sliced = &p->fmt.sliced;
  281. pr_cont(", service_set=0x%08x, io_size=%d\n",
  282. sliced->service_set, sliced->io_size);
  283. for (i = 0; i < 24; i++)
  284. printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
  285. sliced->service_lines[0][i],
  286. sliced->service_lines[1][i]);
  287. break;
  288. case V4L2_BUF_TYPE_PRIVATE:
  289. pr_cont("\n");
  290. break;
  291. }
  292. }
  293. static void v4l_print_framebuffer(const void *arg, bool write_only)
  294. {
  295. const struct v4l2_framebuffer *p = arg;
  296. pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
  297. "height=%u, pixelformat=%c%c%c%c, "
  298. "bytesperline=%u sizeimage=%u, colorspace=%d\n",
  299. p->capability, p->flags, p->base,
  300. p->fmt.width, p->fmt.height,
  301. (p->fmt.pixelformat & 0xff),
  302. (p->fmt.pixelformat >> 8) & 0xff,
  303. (p->fmt.pixelformat >> 16) & 0xff,
  304. (p->fmt.pixelformat >> 24) & 0xff,
  305. p->fmt.bytesperline, p->fmt.sizeimage,
  306. p->fmt.colorspace);
  307. }
  308. static void v4l_print_buftype(const void *arg, bool write_only)
  309. {
  310. pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
  311. }
  312. static void v4l_print_modulator(const void *arg, bool write_only)
  313. {
  314. const struct v4l2_modulator *p = arg;
  315. if (write_only)
  316. pr_cont("index=%u, txsubchans=0x%x", p->index, p->txsubchans);
  317. else
  318. pr_cont("index=%u, name=%s, capability=0x%x, "
  319. "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
  320. p->index, p->name, p->capability,
  321. p->rangelow, p->rangehigh, p->txsubchans);
  322. }
  323. static void v4l_print_tuner(const void *arg, bool write_only)
  324. {
  325. const struct v4l2_tuner *p = arg;
  326. if (write_only)
  327. pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
  328. else
  329. pr_cont("index=%u, name=%s, type=%u, capability=0x%x, "
  330. "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
  331. "rxsubchans=0x%x, audmode=%u\n",
  332. p->index, p->name, p->type,
  333. p->capability, p->rangelow,
  334. p->rangehigh, p->signal, p->afc,
  335. p->rxsubchans, p->audmode);
  336. }
  337. static void v4l_print_frequency(const void *arg, bool write_only)
  338. {
  339. const struct v4l2_frequency *p = arg;
  340. pr_cont("tuner=%u, type=%u, frequency=%u\n",
  341. p->tuner, p->type, p->frequency);
  342. }
  343. static void v4l_print_standard(const void *arg, bool write_only)
  344. {
  345. const struct v4l2_standard *p = arg;
  346. pr_cont("index=%u, id=0x%Lx, name=%s, fps=%u/%u, "
  347. "framelines=%u\n", p->index,
  348. (unsigned long long)p->id, p->name,
  349. p->frameperiod.numerator,
  350. p->frameperiod.denominator,
  351. p->framelines);
  352. }
  353. static void v4l_print_std(const void *arg, bool write_only)
  354. {
  355. pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
  356. }
  357. static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
  358. {
  359. const struct v4l2_hw_freq_seek *p = arg;
  360. pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u\n",
  361. p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing);
  362. }
  363. static void v4l_print_requestbuffers(const void *arg, bool write_only)
  364. {
  365. const struct v4l2_requestbuffers *p = arg;
  366. pr_cont("count=%d, type=%s, memory=%s\n",
  367. p->count,
  368. prt_names(p->type, v4l2_type_names),
  369. prt_names(p->memory, v4l2_memory_names));
  370. }
  371. static void v4l_print_buffer(const void *arg, bool write_only)
  372. {
  373. const struct v4l2_buffer *p = arg;
  374. const struct v4l2_timecode *tc = &p->timecode;
  375. const struct v4l2_plane *plane;
  376. int i;
  377. pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
  378. "flags=0x%08x, field=%s, sequence=%d, memory=%s",
  379. p->timestamp.tv_sec / 3600,
  380. (int)(p->timestamp.tv_sec / 60) % 60,
  381. (int)(p->timestamp.tv_sec % 60),
  382. (long)p->timestamp.tv_usec,
  383. p->index,
  384. prt_names(p->type, v4l2_type_names),
  385. p->flags, prt_names(p->field, v4l2_field_names),
  386. p->sequence, prt_names(p->memory, v4l2_memory_names));
  387. if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
  388. pr_cont("\n");
  389. for (i = 0; i < p->length; ++i) {
  390. plane = &p->m.planes[i];
  391. printk(KERN_DEBUG
  392. "plane %d: bytesused=%d, data_offset=0x%08x "
  393. "offset/userptr=0x%lx, length=%d\n",
  394. i, plane->bytesused, plane->data_offset,
  395. plane->m.userptr, plane->length);
  396. }
  397. } else {
  398. pr_cont("bytesused=%d, offset/userptr=0x%lx, length=%d\n",
  399. p->bytesused, p->m.userptr, p->length);
  400. }
  401. printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
  402. "flags=0x%08x, frames=%d, userbits=0x%08x\n",
  403. tc->hours, tc->minutes, tc->seconds,
  404. tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
  405. }
  406. static void v4l_print_create_buffers(const void *arg, bool write_only)
  407. {
  408. const struct v4l2_create_buffers *p = arg;
  409. pr_cont("index=%d, count=%d, memory=%s, ",
  410. p->index, p->count,
  411. prt_names(p->memory, v4l2_memory_names));
  412. v4l_print_format(&p->format, write_only);
  413. }
  414. static void v4l_print_streamparm(const void *arg, bool write_only)
  415. {
  416. const struct v4l2_streamparm *p = arg;
  417. pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
  418. if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  419. p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  420. const struct v4l2_captureparm *c = &p->parm.capture;
  421. pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
  422. "extendedmode=%d, readbuffers=%d\n",
  423. c->capability, c->capturemode,
  424. c->timeperframe.numerator, c->timeperframe.denominator,
  425. c->extendedmode, c->readbuffers);
  426. } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  427. p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  428. const struct v4l2_outputparm *c = &p->parm.output;
  429. pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
  430. "extendedmode=%d, writebuffers=%d\n",
  431. c->capability, c->outputmode,
  432. c->timeperframe.numerator, c->timeperframe.denominator,
  433. c->extendedmode, c->writebuffers);
  434. }
  435. }
  436. static void v4l_print_queryctrl(const void *arg, bool write_only)
  437. {
  438. const struct v4l2_queryctrl *p = arg;
  439. pr_cont("id=0x%x, type=%d, name=%s, min/max=%d/%d, "
  440. "step=%d, default=%d, flags=0x%08x\n",
  441. p->id, p->type, p->name,
  442. p->minimum, p->maximum,
  443. p->step, p->default_value, p->flags);
  444. }
  445. static void v4l_print_querymenu(const void *arg, bool write_only)
  446. {
  447. const struct v4l2_querymenu *p = arg;
  448. pr_cont("id=0x%x, index=%d\n", p->id, p->index);
  449. }
  450. static void v4l_print_control(const void *arg, bool write_only)
  451. {
  452. const struct v4l2_control *p = arg;
  453. pr_cont("id=0x%x, value=%d\n", p->id, p->value);
  454. }
  455. static void v4l_print_ext_controls(const void *arg, bool write_only)
  456. {
  457. const struct v4l2_ext_controls *p = arg;
  458. int i;
  459. pr_cont("class=0x%x, count=%d, error_idx=%d",
  460. p->ctrl_class, p->count, p->error_idx);
  461. for (i = 0; i < p->count; i++) {
  462. if (p->controls[i].size)
  463. pr_cont(", id/val=0x%x/0x%x",
  464. p->controls[i].id, p->controls[i].value);
  465. else
  466. pr_cont(", id/size=0x%x/%u",
  467. p->controls[i].id, p->controls[i].size);
  468. }
  469. pr_cont("\n");
  470. }
  471. static void v4l_print_cropcap(const void *arg, bool write_only)
  472. {
  473. const struct v4l2_cropcap *p = arg;
  474. pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
  475. "defrect wxh=%dx%d, x,y=%d,%d\n, "
  476. "pixelaspect %d/%d\n",
  477. prt_names(p->type, v4l2_type_names),
  478. p->bounds.width, p->bounds.height,
  479. p->bounds.left, p->bounds.top,
  480. p->defrect.width, p->defrect.height,
  481. p->defrect.left, p->defrect.top,
  482. p->pixelaspect.numerator, p->pixelaspect.denominator);
  483. }
  484. static void v4l_print_crop(const void *arg, bool write_only)
  485. {
  486. const struct v4l2_crop *p = arg;
  487. pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
  488. prt_names(p->type, v4l2_type_names),
  489. p->c.width, p->c.height,
  490. p->c.left, p->c.top);
  491. }
  492. static void v4l_print_selection(const void *arg, bool write_only)
  493. {
  494. const struct v4l2_selection *p = arg;
  495. pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
  496. prt_names(p->type, v4l2_type_names),
  497. p->target, p->flags,
  498. p->r.width, p->r.height, p->r.left, p->r.top);
  499. }
  500. static void v4l_print_jpegcompression(const void *arg, bool write_only)
  501. {
  502. const struct v4l2_jpegcompression *p = arg;
  503. pr_cont("quality=%d, APPn=%d, APP_len=%d, "
  504. "COM_len=%d, jpeg_markers=0x%x\n",
  505. p->quality, p->APPn, p->APP_len,
  506. p->COM_len, p->jpeg_markers);
  507. }
  508. static void v4l_print_enc_idx(const void *arg, bool write_only)
  509. {
  510. const struct v4l2_enc_idx *p = arg;
  511. pr_cont("entries=%d, entries_cap=%d\n",
  512. p->entries, p->entries_cap);
  513. }
  514. static void v4l_print_encoder_cmd(const void *arg, bool write_only)
  515. {
  516. const struct v4l2_encoder_cmd *p = arg;
  517. pr_cont("cmd=%d, flags=0x%x\n",
  518. p->cmd, p->flags);
  519. }
  520. static void v4l_print_decoder_cmd(const void *arg, bool write_only)
  521. {
  522. const struct v4l2_decoder_cmd *p = arg;
  523. pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
  524. if (p->cmd == V4L2_DEC_CMD_START)
  525. pr_info("speed=%d, format=%u\n",
  526. p->start.speed, p->start.format);
  527. else if (p->cmd == V4L2_DEC_CMD_STOP)
  528. pr_info("pts=%llu\n", p->stop.pts);
  529. }
  530. static void v4l_print_dbg_chip_ident(const void *arg, bool write_only)
  531. {
  532. const struct v4l2_dbg_chip_ident *p = arg;
  533. pr_cont("type=%u, ", p->match.type);
  534. if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
  535. pr_cont("name=%s, ", p->match.name);
  536. else
  537. pr_cont("addr=%u, ", p->match.addr);
  538. pr_cont("chip_ident=%u, revision=0x%x\n",
  539. p->ident, p->revision);
  540. }
  541. static void v4l_print_dbg_register(const void *arg, bool write_only)
  542. {
  543. const struct v4l2_dbg_register *p = arg;
  544. pr_cont("type=%u, ", p->match.type);
  545. if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
  546. pr_cont("name=%s, ", p->match.name);
  547. else
  548. pr_cont("addr=%u, ", p->match.addr);
  549. pr_cont("reg=0x%llx, val=0x%llx\n",
  550. p->reg, p->val);
  551. }
  552. static void v4l_print_dv_enum_presets(const void *arg, bool write_only)
  553. {
  554. const struct v4l2_dv_enum_preset *p = arg;
  555. pr_cont("index=%u, preset=%u, name=%s, width=%u, height=%u\n",
  556. p->index, p->preset, p->name, p->width, p->height);
  557. }
  558. static void v4l_print_dv_preset(const void *arg, bool write_only)
  559. {
  560. const struct v4l2_dv_preset *p = arg;
  561. pr_cont("preset=%u\n", p->preset);
  562. }
  563. static void v4l_print_dv_timings(const void *arg, bool write_only)
  564. {
  565. const struct v4l2_dv_timings *p = arg;
  566. switch (p->type) {
  567. case V4L2_DV_BT_656_1120:
  568. pr_cont("type=bt-656/1120, interlaced=%u, "
  569. "pixelclock=%llu, "
  570. "width=%u, height=%u, polarities=0x%x, "
  571. "hfrontporch=%u, hsync=%u, "
  572. "hbackporch=%u, vfrontporch=%u, "
  573. "vsync=%u, vbackporch=%u, "
  574. "il_vfrontporch=%u, il_vsync=%u, "
  575. "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
  576. p->bt.interlaced, p->bt.pixelclock,
  577. p->bt.width, p->bt.height,
  578. p->bt.polarities, p->bt.hfrontporch,
  579. p->bt.hsync, p->bt.hbackporch,
  580. p->bt.vfrontporch, p->bt.vsync,
  581. p->bt.vbackporch, p->bt.il_vfrontporch,
  582. p->bt.il_vsync, p->bt.il_vbackporch,
  583. p->bt.standards, p->bt.flags);
  584. break;
  585. default:
  586. pr_cont("type=%d\n", p->type);
  587. break;
  588. }
  589. }
  590. static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
  591. {
  592. const struct v4l2_enum_dv_timings *p = arg;
  593. pr_cont("index=%u, ", p->index);
  594. v4l_print_dv_timings(&p->timings, write_only);
  595. }
  596. static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
  597. {
  598. const struct v4l2_dv_timings_cap *p = arg;
  599. switch (p->type) {
  600. case V4L2_DV_BT_656_1120:
  601. pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
  602. "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
  603. p->bt.min_width, p->bt.max_width,
  604. p->bt.min_height, p->bt.max_height,
  605. p->bt.min_pixelclock, p->bt.max_pixelclock,
  606. p->bt.standards, p->bt.capabilities);
  607. break;
  608. default:
  609. pr_cont("type=%u\n", p->type);
  610. break;
  611. }
  612. }
  613. static void v4l_print_frmsizeenum(const void *arg, bool write_only)
  614. {
  615. const struct v4l2_frmsizeenum *p = arg;
  616. pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
  617. p->index,
  618. (p->pixel_format & 0xff),
  619. (p->pixel_format >> 8) & 0xff,
  620. (p->pixel_format >> 16) & 0xff,
  621. (p->pixel_format >> 24) & 0xff,
  622. p->type);
  623. switch (p->type) {
  624. case V4L2_FRMSIZE_TYPE_DISCRETE:
  625. pr_cont(" wxh=%ux%u\n",
  626. p->discrete.width, p->discrete.height);
  627. break;
  628. case V4L2_FRMSIZE_TYPE_STEPWISE:
  629. pr_cont(" min=%ux%u, max=%ux%u, step=%ux%u\n",
  630. p->stepwise.min_width, p->stepwise.min_height,
  631. p->stepwise.step_width, p->stepwise.step_height,
  632. p->stepwise.max_width, p->stepwise.max_height);
  633. break;
  634. case V4L2_FRMSIZE_TYPE_CONTINUOUS:
  635. /* fall through */
  636. default:
  637. pr_cont("\n");
  638. break;
  639. }
  640. }
  641. static void v4l_print_frmivalenum(const void *arg, bool write_only)
  642. {
  643. const struct v4l2_frmivalenum *p = arg;
  644. pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
  645. p->index,
  646. (p->pixel_format & 0xff),
  647. (p->pixel_format >> 8) & 0xff,
  648. (p->pixel_format >> 16) & 0xff,
  649. (p->pixel_format >> 24) & 0xff,
  650. p->width, p->height, p->type);
  651. switch (p->type) {
  652. case V4L2_FRMIVAL_TYPE_DISCRETE:
  653. pr_cont(" fps=%d/%d\n",
  654. p->discrete.numerator,
  655. p->discrete.denominator);
  656. break;
  657. case V4L2_FRMIVAL_TYPE_STEPWISE:
  658. pr_cont(" min=%d/%d, max=%d/%d, step=%d/%d\n",
  659. p->stepwise.min.numerator,
  660. p->stepwise.min.denominator,
  661. p->stepwise.max.numerator,
  662. p->stepwise.max.denominator,
  663. p->stepwise.step.numerator,
  664. p->stepwise.step.denominator);
  665. break;
  666. case V4L2_FRMIVAL_TYPE_CONTINUOUS:
  667. /* fall through */
  668. default:
  669. pr_cont("\n");
  670. break;
  671. }
  672. }
  673. static void v4l_print_event(const void *arg, bool write_only)
  674. {
  675. const struct v4l2_event *p = arg;
  676. const struct v4l2_event_ctrl *c;
  677. pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
  678. "timestamp=%lu.%9.9lu\n",
  679. p->type, p->pending, p->sequence, p->id,
  680. p->timestamp.tv_sec, p->timestamp.tv_nsec);
  681. switch (p->type) {
  682. case V4L2_EVENT_VSYNC:
  683. printk(KERN_DEBUG "field=%s\n",
  684. prt_names(p->u.vsync.field, v4l2_field_names));
  685. break;
  686. case V4L2_EVENT_CTRL:
  687. c = &p->u.ctrl;
  688. printk(KERN_DEBUG "changes=0x%x, type=%u, ",
  689. c->changes, c->type);
  690. if (c->type == V4L2_CTRL_TYPE_INTEGER64)
  691. pr_cont("value64=%lld, ", c->value64);
  692. else
  693. pr_cont("value=%d, ", c->value);
  694. pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d,"
  695. " default_value=%d\n",
  696. c->flags, c->minimum, c->maximum,
  697. c->step, c->default_value);
  698. break;
  699. case V4L2_EVENT_FRAME_SYNC:
  700. pr_cont("frame_sequence=%u\n",
  701. p->u.frame_sync.frame_sequence);
  702. break;
  703. }
  704. }
  705. static void v4l_print_event_subscription(const void *arg, bool write_only)
  706. {
  707. const struct v4l2_event_subscription *p = arg;
  708. pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
  709. p->type, p->id, p->flags);
  710. }
  711. static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
  712. {
  713. const struct v4l2_sliced_vbi_cap *p = arg;
  714. int i;
  715. pr_cont("type=%s, service_set=0x%08x\n",
  716. prt_names(p->type, v4l2_type_names), p->service_set);
  717. for (i = 0; i < 24; i++)
  718. printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
  719. p->service_lines[0][i],
  720. p->service_lines[1][i]);
  721. }
  722. static void v4l_print_freq_band(const void *arg, bool write_only)
  723. {
  724. const struct v4l2_frequency_band *p = arg;
  725. pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
  726. "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
  727. p->tuner, p->type, p->index,
  728. p->capability, p->rangelow,
  729. p->rangehigh, p->modulation);
  730. }
  731. static void v4l_print_u32(const void *arg, bool write_only)
  732. {
  733. pr_cont("value=%u\n", *(const u32 *)arg);
  734. }
  735. static void v4l_print_newline(const void *arg, bool write_only)
  736. {
  737. pr_cont("\n");
  738. }
  739. static void v4l_print_default(const void *arg, bool write_only)
  740. {
  741. pr_cont("driver-specific ioctl\n");
  742. }
  743. static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
  744. {
  745. __u32 i;
  746. /* zero the reserved fields */
  747. c->reserved[0] = c->reserved[1] = 0;
  748. for (i = 0; i < c->count; i++)
  749. c->controls[i].reserved2[0] = 0;
  750. /* V4L2_CID_PRIVATE_BASE cannot be used as control class
  751. when using extended controls.
  752. Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
  753. is it allowed for backwards compatibility.
  754. */
  755. if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
  756. return 0;
  757. /* Check that all controls are from the same control class. */
  758. for (i = 0; i < c->count; i++) {
  759. if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
  760. c->error_idx = i;
  761. return 0;
  762. }
  763. }
  764. return 1;
  765. }
  766. static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
  767. {
  768. if (ops == NULL)
  769. return -EINVAL;
  770. switch (type) {
  771. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  772. if (ops->vidioc_g_fmt_vid_cap ||
  773. ops->vidioc_g_fmt_vid_cap_mplane)
  774. return 0;
  775. break;
  776. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  777. if (ops->vidioc_g_fmt_vid_cap_mplane)
  778. return 0;
  779. break;
  780. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  781. if (ops->vidioc_g_fmt_vid_overlay)
  782. return 0;
  783. break;
  784. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  785. if (ops->vidioc_g_fmt_vid_out ||
  786. ops->vidioc_g_fmt_vid_out_mplane)
  787. return 0;
  788. break;
  789. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  790. if (ops->vidioc_g_fmt_vid_out_mplane)
  791. return 0;
  792. break;
  793. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  794. if (ops->vidioc_g_fmt_vid_out_overlay)
  795. return 0;
  796. break;
  797. case V4L2_BUF_TYPE_VBI_CAPTURE:
  798. if (ops->vidioc_g_fmt_vbi_cap)
  799. return 0;
  800. break;
  801. case V4L2_BUF_TYPE_VBI_OUTPUT:
  802. if (ops->vidioc_g_fmt_vbi_out)
  803. return 0;
  804. break;
  805. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  806. if (ops->vidioc_g_fmt_sliced_vbi_cap)
  807. return 0;
  808. break;
  809. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  810. if (ops->vidioc_g_fmt_sliced_vbi_out)
  811. return 0;
  812. break;
  813. case V4L2_BUF_TYPE_PRIVATE:
  814. if (ops->vidioc_g_fmt_type_private)
  815. return 0;
  816. break;
  817. }
  818. return -EINVAL;
  819. }
  820. static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
  821. struct file *file, void *fh, void *arg)
  822. {
  823. struct v4l2_capability *cap = (struct v4l2_capability *)arg;
  824. cap->version = LINUX_VERSION_CODE;
  825. return ops->vidioc_querycap(file, fh, cap);
  826. }
  827. static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
  828. struct file *file, void *fh, void *arg)
  829. {
  830. return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
  831. }
  832. static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
  833. struct file *file, void *fh, void *arg)
  834. {
  835. return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
  836. }
  837. static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
  838. struct file *file, void *fh, void *arg)
  839. {
  840. struct video_device *vfd;
  841. u32 *p = arg;
  842. if (ops->vidioc_g_priority)
  843. return ops->vidioc_g_priority(file, fh, arg);
  844. vfd = video_devdata(file);
  845. *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
  846. return 0;
  847. }
  848. static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
  849. struct file *file, void *fh, void *arg)
  850. {
  851. struct video_device *vfd;
  852. struct v4l2_fh *vfh;
  853. u32 *p = arg;
  854. if (ops->vidioc_s_priority)
  855. return ops->vidioc_s_priority(file, fh, *p);
  856. vfd = video_devdata(file);
  857. vfh = file->private_data;
  858. return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
  859. }
  860. static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
  861. struct file *file, void *fh, void *arg)
  862. {
  863. struct v4l2_input *p = arg;
  864. /*
  865. * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
  866. * CAP_STD here based on ioctl handler provided by the
  867. * driver. If the driver doesn't support these
  868. * for a specific input, it must override these flags.
  869. */
  870. if (ops->vidioc_s_std)
  871. p->capabilities |= V4L2_IN_CAP_STD;
  872. if (ops->vidioc_s_dv_preset)
  873. p->capabilities |= V4L2_IN_CAP_PRESETS;
  874. if (ops->vidioc_s_dv_timings)
  875. p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
  876. return ops->vidioc_enum_input(file, fh, p);
  877. }
  878. static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
  879. struct file *file, void *fh, void *arg)
  880. {
  881. struct v4l2_output *p = arg;
  882. /*
  883. * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
  884. * CAP_STD here based on ioctl handler provided by the
  885. * driver. If the driver doesn't support these
  886. * for a specific output, it must override these flags.
  887. */
  888. if (ops->vidioc_s_std)
  889. p->capabilities |= V4L2_OUT_CAP_STD;
  890. if (ops->vidioc_s_dv_preset)
  891. p->capabilities |= V4L2_OUT_CAP_PRESETS;
  892. if (ops->vidioc_s_dv_timings)
  893. p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
  894. return ops->vidioc_enum_output(file, fh, p);
  895. }
  896. static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
  897. struct file *file, void *fh, void *arg)
  898. {
  899. struct v4l2_fmtdesc *p = arg;
  900. switch (p->type) {
  901. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  902. if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
  903. break;
  904. return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
  905. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  906. if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
  907. break;
  908. return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
  909. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  910. if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
  911. break;
  912. return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
  913. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  914. if (unlikely(!ops->vidioc_enum_fmt_vid_out))
  915. break;
  916. return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
  917. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  918. if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
  919. break;
  920. return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
  921. case V4L2_BUF_TYPE_PRIVATE:
  922. if (unlikely(!ops->vidioc_enum_fmt_type_private))
  923. break;
  924. return ops->vidioc_enum_fmt_type_private(file, fh, arg);
  925. }
  926. return -EINVAL;
  927. }
  928. static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
  929. struct file *file, void *fh, void *arg)
  930. {
  931. struct v4l2_format *p = arg;
  932. switch (p->type) {
  933. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  934. if (unlikely(!ops->vidioc_g_fmt_vid_cap))
  935. break;
  936. return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
  937. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  938. if (unlikely(!ops->vidioc_g_fmt_vid_cap_mplane))
  939. break;
  940. return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
  941. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  942. if (unlikely(!ops->vidioc_g_fmt_vid_overlay))
  943. break;
  944. return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
  945. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  946. if (unlikely(!ops->vidioc_g_fmt_vid_out))
  947. break;
  948. return ops->vidioc_g_fmt_vid_out(file, fh, arg);
  949. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  950. if (unlikely(!ops->vidioc_g_fmt_vid_out_mplane))
  951. break;
  952. return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
  953. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  954. if (unlikely(!ops->vidioc_g_fmt_vid_out_overlay))
  955. break;
  956. return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
  957. case V4L2_BUF_TYPE_VBI_CAPTURE:
  958. if (unlikely(!ops->vidioc_g_fmt_vbi_cap))
  959. break;
  960. return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
  961. case V4L2_BUF_TYPE_VBI_OUTPUT:
  962. if (unlikely(!ops->vidioc_g_fmt_vbi_out))
  963. break;
  964. return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
  965. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  966. if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_cap))
  967. break;
  968. return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
  969. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  970. if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_out))
  971. break;
  972. return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
  973. case V4L2_BUF_TYPE_PRIVATE:
  974. if (unlikely(!ops->vidioc_g_fmt_type_private))
  975. break;
  976. return ops->vidioc_g_fmt_type_private(file, fh, arg);
  977. }
  978. return -EINVAL;
  979. }
  980. static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
  981. struct file *file, void *fh, void *arg)
  982. {
  983. struct v4l2_format *p = arg;
  984. switch (p->type) {
  985. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  986. if (unlikely(!ops->vidioc_s_fmt_vid_cap))
  987. break;
  988. CLEAR_AFTER_FIELD(p, fmt.pix);
  989. return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
  990. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  991. if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
  992. break;
  993. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  994. return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
  995. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  996. if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
  997. break;
  998. CLEAR_AFTER_FIELD(p, fmt.win);
  999. return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
  1000. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1001. if (unlikely(!ops->vidioc_s_fmt_vid_out))
  1002. break;
  1003. CLEAR_AFTER_FIELD(p, fmt.pix);
  1004. return ops->vidioc_s_fmt_vid_out(file, fh, arg);
  1005. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1006. if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
  1007. break;
  1008. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1009. return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
  1010. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1011. if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
  1012. break;
  1013. CLEAR_AFTER_FIELD(p, fmt.win);
  1014. return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
  1015. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1016. if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
  1017. break;
  1018. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1019. return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
  1020. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1021. if (unlikely(!ops->vidioc_s_fmt_vbi_out))
  1022. break;
  1023. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1024. return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
  1025. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1026. if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
  1027. break;
  1028. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1029. return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
  1030. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1031. if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
  1032. break;
  1033. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1034. return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
  1035. case V4L2_BUF_TYPE_PRIVATE:
  1036. if (unlikely(!ops->vidioc_s_fmt_type_private))
  1037. break;
  1038. return ops->vidioc_s_fmt_type_private(file, fh, arg);
  1039. }
  1040. return -EINVAL;
  1041. }
  1042. static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
  1043. struct file *file, void *fh, void *arg)
  1044. {
  1045. struct v4l2_format *p = arg;
  1046. switch (p->type) {
  1047. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1048. if (unlikely(!ops->vidioc_try_fmt_vid_cap))
  1049. break;
  1050. CLEAR_AFTER_FIELD(p, fmt.pix);
  1051. return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
  1052. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1053. if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
  1054. break;
  1055. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1056. return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
  1057. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1058. if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
  1059. break;
  1060. CLEAR_AFTER_FIELD(p, fmt.win);
  1061. return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
  1062. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1063. if (unlikely(!ops->vidioc_try_fmt_vid_out))
  1064. break;
  1065. CLEAR_AFTER_FIELD(p, fmt.pix);
  1066. return ops->vidioc_try_fmt_vid_out(file, fh, arg);
  1067. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1068. if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
  1069. break;
  1070. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1071. return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
  1072. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1073. if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
  1074. break;
  1075. CLEAR_AFTER_FIELD(p, fmt.win);
  1076. return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
  1077. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1078. if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
  1079. break;
  1080. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1081. return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
  1082. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1083. if (unlikely(!ops->vidioc_try_fmt_vbi_out))
  1084. break;
  1085. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1086. return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
  1087. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1088. if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
  1089. break;
  1090. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1091. return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
  1092. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1093. if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
  1094. break;
  1095. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1096. return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
  1097. case V4L2_BUF_TYPE_PRIVATE:
  1098. if (unlikely(!ops->vidioc_try_fmt_type_private))
  1099. break;
  1100. return ops->vidioc_try_fmt_type_private(file, fh, arg);
  1101. }
  1102. return -EINVAL;
  1103. }
  1104. static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
  1105. struct file *file, void *fh, void *arg)
  1106. {
  1107. return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
  1108. }
  1109. static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
  1110. struct file *file, void *fh, void *arg)
  1111. {
  1112. return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
  1113. }
  1114. static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
  1115. struct file *file, void *fh, void *arg)
  1116. {
  1117. struct video_device *vfd = video_devdata(file);
  1118. struct v4l2_tuner *p = arg;
  1119. int err;
  1120. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1121. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1122. err = ops->vidioc_g_tuner(file, fh, p);
  1123. if (!err)
  1124. p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
  1125. return err;
  1126. }
  1127. static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
  1128. struct file *file, void *fh, void *arg)
  1129. {
  1130. struct video_device *vfd = video_devdata(file);
  1131. struct v4l2_tuner *p = arg;
  1132. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1133. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1134. return ops->vidioc_s_tuner(file, fh, p);
  1135. }
  1136. static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
  1137. struct file *file, void *fh, void *arg)
  1138. {
  1139. struct v4l2_modulator *p = arg;
  1140. int err;
  1141. err = ops->vidioc_g_modulator(file, fh, p);
  1142. if (!err)
  1143. p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
  1144. return err;
  1145. }
  1146. static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
  1147. struct file *file, void *fh, void *arg)
  1148. {
  1149. struct video_device *vfd = video_devdata(file);
  1150. struct v4l2_frequency *p = arg;
  1151. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1152. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1153. return ops->vidioc_g_frequency(file, fh, p);
  1154. }
  1155. static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
  1156. struct file *file, void *fh, void *arg)
  1157. {
  1158. struct video_device *vfd = video_devdata(file);
  1159. struct v4l2_frequency *p = arg;
  1160. enum v4l2_tuner_type type;
  1161. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1162. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1163. if (p->type != type)
  1164. return -EINVAL;
  1165. return ops->vidioc_s_frequency(file, fh, p);
  1166. }
  1167. static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
  1168. struct file *file, void *fh, void *arg)
  1169. {
  1170. struct video_device *vfd = video_devdata(file);
  1171. struct v4l2_standard *p = arg;
  1172. v4l2_std_id id = vfd->tvnorms, curr_id = 0;
  1173. unsigned int index = p->index, i, j = 0;
  1174. const char *descr = "";
  1175. /* Return norm array in a canonical way */
  1176. for (i = 0; i <= index && id; i++) {
  1177. /* last std value in the standards array is 0, so this
  1178. while always ends there since (id & 0) == 0. */
  1179. while ((id & standards[j].std) != standards[j].std)
  1180. j++;
  1181. curr_id = standards[j].std;
  1182. descr = standards[j].descr;
  1183. j++;
  1184. if (curr_id == 0)
  1185. break;
  1186. if (curr_id != V4L2_STD_PAL &&
  1187. curr_id != V4L2_STD_SECAM &&
  1188. curr_id != V4L2_STD_NTSC)
  1189. id &= ~curr_id;
  1190. }
  1191. if (i <= index)
  1192. return -EINVAL;
  1193. v4l2_video_std_construct(p, curr_id, descr);
  1194. return 0;
  1195. }
  1196. static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
  1197. struct file *file, void *fh, void *arg)
  1198. {
  1199. struct video_device *vfd = video_devdata(file);
  1200. v4l2_std_id *id = arg;
  1201. /* Calls the specific handler */
  1202. if (ops->vidioc_g_std)
  1203. return ops->vidioc_g_std(file, fh, arg);
  1204. if (vfd->current_norm) {
  1205. *id = vfd->current_norm;
  1206. return 0;
  1207. }
  1208. return -ENOTTY;
  1209. }
  1210. static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
  1211. struct file *file, void *fh, void *arg)
  1212. {
  1213. struct video_device *vfd = video_devdata(file);
  1214. v4l2_std_id *id = arg, norm;
  1215. int ret;
  1216. norm = (*id) & vfd->tvnorms;
  1217. if (vfd->tvnorms && !norm) /* Check if std is supported */
  1218. return -EINVAL;
  1219. /* Calls the specific handler */
  1220. ret = ops->vidioc_s_std(file, fh, &norm);
  1221. /* Updates standard information */
  1222. if (ret >= 0)
  1223. vfd->current_norm = norm;
  1224. return ret;
  1225. }
  1226. static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
  1227. struct file *file, void *fh, void *arg)
  1228. {
  1229. struct video_device *vfd = video_devdata(file);
  1230. v4l2_std_id *p = arg;
  1231. /*
  1232. * If nothing detected, it should return all supported
  1233. * standard.
  1234. * Drivers just need to mask the std argument, in order
  1235. * to remove the standards that don't apply from the mask.
  1236. * This means that tuners, audio and video decoders can join
  1237. * their efforts to improve the standards detection.
  1238. */
  1239. *p = vfd->tvnorms;
  1240. return ops->vidioc_querystd(file, fh, arg);
  1241. }
  1242. static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
  1243. struct file *file, void *fh, void *arg)
  1244. {
  1245. struct video_device *vfd = video_devdata(file);
  1246. struct v4l2_hw_freq_seek *p = arg;
  1247. enum v4l2_tuner_type type;
  1248. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1249. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1250. if (p->type != type)
  1251. return -EINVAL;
  1252. return ops->vidioc_s_hw_freq_seek(file, fh, p);
  1253. }
  1254. static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
  1255. struct file *file, void *fh, void *arg)
  1256. {
  1257. struct v4l2_requestbuffers *p = arg;
  1258. int ret = check_fmt(ops, p->type);
  1259. if (ret)
  1260. return ret;
  1261. if (p->type < V4L2_BUF_TYPE_PRIVATE)
  1262. CLEAR_AFTER_FIELD(p, memory);
  1263. return ops->vidioc_reqbufs(file, fh, p);
  1264. }
  1265. static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
  1266. struct file *file, void *fh, void *arg)
  1267. {
  1268. struct v4l2_buffer *p = arg;
  1269. int ret = check_fmt(ops, p->type);
  1270. return ret ? ret : ops->vidioc_querybuf(file, fh, p);
  1271. }
  1272. static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
  1273. struct file *file, void *fh, void *arg)
  1274. {
  1275. struct v4l2_buffer *p = arg;
  1276. int ret = check_fmt(ops, p->type);
  1277. return ret ? ret : ops->vidioc_qbuf(file, fh, p);
  1278. }
  1279. static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
  1280. struct file *file, void *fh, void *arg)
  1281. {
  1282. struct v4l2_buffer *p = arg;
  1283. int ret = check_fmt(ops, p->type);
  1284. return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
  1285. }
  1286. static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
  1287. struct file *file, void *fh, void *arg)
  1288. {
  1289. struct v4l2_create_buffers *create = arg;
  1290. int ret = check_fmt(ops, create->format.type);
  1291. return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
  1292. }
  1293. static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
  1294. struct file *file, void *fh, void *arg)
  1295. {
  1296. struct v4l2_buffer *b = arg;
  1297. int ret = check_fmt(ops, b->type);
  1298. return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
  1299. }
  1300. static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
  1301. struct file *file, void *fh, void *arg)
  1302. {
  1303. struct video_device *vfd = video_devdata(file);
  1304. struct v4l2_streamparm *p = arg;
  1305. v4l2_std_id std;
  1306. int ret = check_fmt(ops, p->type);
  1307. if (ret)
  1308. return ret;
  1309. if (ops->vidioc_g_parm)
  1310. return ops->vidioc_g_parm(file, fh, p);
  1311. std = vfd->current_norm;
  1312. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1313. p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1314. return -EINVAL;
  1315. p->parm.capture.readbuffers = 2;
  1316. if (ops->vidioc_g_std)
  1317. ret = ops->vidioc_g_std(file, fh, &std);
  1318. if (ret == 0)
  1319. v4l2_video_std_frame_period(std,
  1320. &p->parm.capture.timeperframe);
  1321. return ret;
  1322. }
  1323. static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
  1324. struct file *file, void *fh, void *arg)
  1325. {
  1326. struct v4l2_streamparm *p = arg;
  1327. int ret = check_fmt(ops, p->type);
  1328. return ret ? ret : ops->vidioc_s_parm(file, fh, p);
  1329. }
  1330. static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
  1331. struct file *file, void *fh, void *arg)
  1332. {
  1333. struct video_device *vfd = video_devdata(file);
  1334. struct v4l2_queryctrl *p = arg;
  1335. struct v4l2_fh *vfh =
  1336. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1337. if (vfh && vfh->ctrl_handler)
  1338. return v4l2_queryctrl(vfh->ctrl_handler, p);
  1339. if (vfd->ctrl_handler)
  1340. return v4l2_queryctrl(vfd->ctrl_handler, p);
  1341. if (ops->vidioc_queryctrl)
  1342. return ops->vidioc_queryctrl(file, fh, p);
  1343. return -ENOTTY;
  1344. }
  1345. static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
  1346. struct file *file, void *fh, void *arg)
  1347. {
  1348. struct video_device *vfd = video_devdata(file);
  1349. struct v4l2_querymenu *p = arg;
  1350. struct v4l2_fh *vfh =
  1351. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1352. if (vfh && vfh->ctrl_handler)
  1353. return v4l2_querymenu(vfh->ctrl_handler, p);
  1354. if (vfd->ctrl_handler)
  1355. return v4l2_querymenu(vfd->ctrl_handler, p);
  1356. if (ops->vidioc_querymenu)
  1357. return ops->vidioc_querymenu(file, fh, p);
  1358. return -ENOTTY;
  1359. }
  1360. static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
  1361. struct file *file, void *fh, void *arg)
  1362. {
  1363. struct video_device *vfd = video_devdata(file);
  1364. struct v4l2_control *p = arg;
  1365. struct v4l2_fh *vfh =
  1366. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1367. struct v4l2_ext_controls ctrls;
  1368. struct v4l2_ext_control ctrl;
  1369. if (vfh && vfh->ctrl_handler)
  1370. return v4l2_g_ctrl(vfh->ctrl_handler, p);
  1371. if (vfd->ctrl_handler)
  1372. return v4l2_g_ctrl(vfd->ctrl_handler, p);
  1373. if (ops->vidioc_g_ctrl)
  1374. return ops->vidioc_g_ctrl(file, fh, p);
  1375. if (ops->vidioc_g_ext_ctrls == NULL)
  1376. return -ENOTTY;
  1377. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1378. ctrls.count = 1;
  1379. ctrls.controls = &ctrl;
  1380. ctrl.id = p->id;
  1381. ctrl.value = p->value;
  1382. if (check_ext_ctrls(&ctrls, 1)) {
  1383. int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
  1384. if (ret == 0)
  1385. p->value = ctrl.value;
  1386. return ret;
  1387. }
  1388. return -EINVAL;
  1389. }
  1390. static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
  1391. struct file *file, void *fh, void *arg)
  1392. {
  1393. struct video_device *vfd = video_devdata(file);
  1394. struct v4l2_control *p = arg;
  1395. struct v4l2_fh *vfh =
  1396. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1397. struct v4l2_ext_controls ctrls;
  1398. struct v4l2_ext_control ctrl;
  1399. if (vfh && vfh->ctrl_handler)
  1400. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
  1401. if (vfd->ctrl_handler)
  1402. return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
  1403. if (ops->vidioc_s_ctrl)
  1404. return ops->vidioc_s_ctrl(file, fh, p);
  1405. if (ops->vidioc_s_ext_ctrls == NULL)
  1406. return -ENOTTY;
  1407. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1408. ctrls.count = 1;
  1409. ctrls.controls = &ctrl;
  1410. ctrl.id = p->id;
  1411. ctrl.value = p->value;
  1412. if (check_ext_ctrls(&ctrls, 1))
  1413. return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
  1414. return -EINVAL;
  1415. }
  1416. static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1417. struct file *file, void *fh, void *arg)
  1418. {
  1419. struct video_device *vfd = video_devdata(file);
  1420. struct v4l2_ext_controls *p = arg;
  1421. struct v4l2_fh *vfh =
  1422. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1423. p->error_idx = p->count;
  1424. if (vfh && vfh->ctrl_handler)
  1425. return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
  1426. if (vfd->ctrl_handler)
  1427. return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
  1428. if (ops->vidioc_g_ext_ctrls == NULL)
  1429. return -ENOTTY;
  1430. return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
  1431. -EINVAL;
  1432. }
  1433. static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1434. struct file *file, void *fh, void *arg)
  1435. {
  1436. struct video_device *vfd = video_devdata(file);
  1437. struct v4l2_ext_controls *p = arg;
  1438. struct v4l2_fh *vfh =
  1439. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1440. p->error_idx = p->count;
  1441. if (vfh && vfh->ctrl_handler)
  1442. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
  1443. if (vfd->ctrl_handler)
  1444. return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
  1445. if (ops->vidioc_s_ext_ctrls == NULL)
  1446. return -ENOTTY;
  1447. return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
  1448. -EINVAL;
  1449. }
  1450. static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1451. struct file *file, void *fh, void *arg)
  1452. {
  1453. struct video_device *vfd = video_devdata(file);
  1454. struct v4l2_ext_controls *p = arg;
  1455. struct v4l2_fh *vfh =
  1456. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1457. p->error_idx = p->count;
  1458. if (vfh && vfh->ctrl_handler)
  1459. return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
  1460. if (vfd->ctrl_handler)
  1461. return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
  1462. if (ops->vidioc_try_ext_ctrls == NULL)
  1463. return -ENOTTY;
  1464. return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
  1465. -EINVAL;
  1466. }
  1467. static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
  1468. struct file *file, void *fh, void *arg)
  1469. {
  1470. struct v4l2_crop *p = arg;
  1471. struct v4l2_selection s = {
  1472. .type = p->type,
  1473. };
  1474. int ret;
  1475. if (ops->vidioc_g_crop)
  1476. return ops->vidioc_g_crop(file, fh, p);
  1477. /* simulate capture crop using selection api */
  1478. /* crop means compose for output devices */
  1479. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1480. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1481. else
  1482. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1483. ret = ops->vidioc_g_selection(file, fh, &s);
  1484. /* copying results to old structure on success */
  1485. if (!ret)
  1486. p->c = s.r;
  1487. return ret;
  1488. }
  1489. static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
  1490. struct file *file, void *fh, void *arg)
  1491. {
  1492. struct v4l2_crop *p = arg;
  1493. struct v4l2_selection s = {
  1494. .type = p->type,
  1495. .r = p->c,
  1496. };
  1497. if (ops->vidioc_s_crop)
  1498. return ops->vidioc_s_crop(file, fh, p);
  1499. /* simulate capture crop using selection api */
  1500. /* crop means compose for output devices */
  1501. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1502. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1503. else
  1504. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1505. return ops->vidioc_s_selection(file, fh, &s);
  1506. }
  1507. static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
  1508. struct file *file, void *fh, void *arg)
  1509. {
  1510. struct v4l2_cropcap *p = arg;
  1511. struct v4l2_selection s = { .type = p->type };
  1512. int ret;
  1513. if (ops->vidioc_cropcap)
  1514. return ops->vidioc_cropcap(file, fh, p);
  1515. /* obtaining bounds */
  1516. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1517. s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
  1518. else
  1519. s.target = V4L2_SEL_TGT_CROP_BOUNDS;
  1520. ret = ops->vidioc_g_selection(file, fh, &s);
  1521. if (ret)
  1522. return ret;
  1523. p->bounds = s.r;
  1524. /* obtaining defrect */
  1525. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1526. s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
  1527. else
  1528. s.target = V4L2_SEL_TGT_CROP_DEFAULT;
  1529. ret = ops->vidioc_g_selection(file, fh, &s);
  1530. if (ret)
  1531. return ret;
  1532. p->defrect = s.r;
  1533. /* setting trivial pixelaspect */
  1534. p->pixelaspect.numerator = 1;
  1535. p->pixelaspect.denominator = 1;
  1536. return 0;
  1537. }
  1538. static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
  1539. struct file *file, void *fh, void *arg)
  1540. {
  1541. struct video_device *vfd = video_devdata(file);
  1542. int ret;
  1543. if (vfd->v4l2_dev)
  1544. pr_info("%s: ================= START STATUS =================\n",
  1545. vfd->v4l2_dev->name);
  1546. ret = ops->vidioc_log_status(file, fh);
  1547. if (vfd->v4l2_dev)
  1548. pr_info("%s: ================== END STATUS ==================\n",
  1549. vfd->v4l2_dev->name);
  1550. return ret;
  1551. }
  1552. static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
  1553. struct file *file, void *fh, void *arg)
  1554. {
  1555. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1556. struct v4l2_dbg_register *p = arg;
  1557. if (!capable(CAP_SYS_ADMIN))
  1558. return -EPERM;
  1559. return ops->vidioc_g_register(file, fh, p);
  1560. #else
  1561. return -ENOTTY;
  1562. #endif
  1563. }
  1564. static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
  1565. struct file *file, void *fh, void *arg)
  1566. {
  1567. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1568. struct v4l2_dbg_register *p = arg;
  1569. if (!capable(CAP_SYS_ADMIN))
  1570. return -EPERM;
  1571. return ops->vidioc_s_register(file, fh, p);
  1572. #else
  1573. return -ENOTTY;
  1574. #endif
  1575. }
  1576. static int v4l_dbg_g_chip_ident(const struct v4l2_ioctl_ops *ops,
  1577. struct file *file, void *fh, void *arg)
  1578. {
  1579. struct v4l2_dbg_chip_ident *p = arg;
  1580. p->ident = V4L2_IDENT_NONE;
  1581. p->revision = 0;
  1582. return ops->vidioc_g_chip_ident(file, fh, p);
  1583. }
  1584. static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
  1585. struct file *file, void *fh, void *arg)
  1586. {
  1587. return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
  1588. }
  1589. static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
  1590. struct file *file, void *fh, void *arg)
  1591. {
  1592. return ops->vidioc_subscribe_event(fh, arg);
  1593. }
  1594. static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
  1595. struct file *file, void *fh, void *arg)
  1596. {
  1597. return ops->vidioc_unsubscribe_event(fh, arg);
  1598. }
  1599. static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
  1600. struct file *file, void *fh, void *arg)
  1601. {
  1602. struct v4l2_sliced_vbi_cap *p = arg;
  1603. /* Clear up to type, everything after type is zeroed already */
  1604. memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
  1605. return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
  1606. }
  1607. static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
  1608. struct file *file, void *fh, void *arg)
  1609. {
  1610. struct video_device *vfd = video_devdata(file);
  1611. struct v4l2_frequency_band *p = arg;
  1612. enum v4l2_tuner_type type;
  1613. int err;
  1614. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1615. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1616. if (type != p->type)
  1617. return -EINVAL;
  1618. if (ops->vidioc_enum_freq_bands)
  1619. return ops->vidioc_enum_freq_bands(file, fh, p);
  1620. if (ops->vidioc_g_tuner) {
  1621. struct v4l2_tuner t = {
  1622. .index = p->tuner,
  1623. .type = type,
  1624. };
  1625. err = ops->vidioc_g_tuner(file, fh, &t);
  1626. if (err)
  1627. return err;
  1628. p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
  1629. p->rangelow = t.rangelow;
  1630. p->rangehigh = t.rangehigh;
  1631. p->modulation = (type == V4L2_TUNER_RADIO) ?
  1632. V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
  1633. return 0;
  1634. }
  1635. if (ops->vidioc_g_modulator) {
  1636. struct v4l2_modulator m = {
  1637. .index = p->tuner,
  1638. };
  1639. if (type != V4L2_TUNER_RADIO)
  1640. return -EINVAL;
  1641. err = ops->vidioc_g_modulator(file, fh, &m);
  1642. if (err)
  1643. return err;
  1644. p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
  1645. p->rangelow = m.rangelow;
  1646. p->rangehigh = m.rangehigh;
  1647. p->modulation = (type == V4L2_TUNER_RADIO) ?
  1648. V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
  1649. return 0;
  1650. }
  1651. return -ENOTTY;
  1652. }
  1653. struct v4l2_ioctl_info {
  1654. unsigned int ioctl;
  1655. u32 flags;
  1656. const char * const name;
  1657. union {
  1658. u32 offset;
  1659. int (*func)(const struct v4l2_ioctl_ops *ops,
  1660. struct file *file, void *fh, void *p);
  1661. } u;
  1662. void (*debug)(const void *arg, bool write_only);
  1663. };
  1664. /* This control needs a priority check */
  1665. #define INFO_FL_PRIO (1 << 0)
  1666. /* This control can be valid if the filehandle passes a control handler. */
  1667. #define INFO_FL_CTRL (1 << 1)
  1668. /* This is a standard ioctl, no need for special code */
  1669. #define INFO_FL_STD (1 << 2)
  1670. /* This is ioctl has its own function */
  1671. #define INFO_FL_FUNC (1 << 3)
  1672. /* Queuing ioctl */
  1673. #define INFO_FL_QUEUE (1 << 4)
  1674. /* Zero struct from after the field to the end */
  1675. #define INFO_FL_CLEAR(v4l2_struct, field) \
  1676. ((offsetof(struct v4l2_struct, field) + \
  1677. sizeof(((struct v4l2_struct *)0)->field)) << 16)
  1678. #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
  1679. #define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
  1680. [_IOC_NR(_ioctl)] = { \
  1681. .ioctl = _ioctl, \
  1682. .flags = _flags | INFO_FL_STD, \
  1683. .name = #_ioctl, \
  1684. .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
  1685. .debug = _debug, \
  1686. }
  1687. #define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
  1688. [_IOC_NR(_ioctl)] = { \
  1689. .ioctl = _ioctl, \
  1690. .flags = _flags | INFO_FL_FUNC, \
  1691. .name = #_ioctl, \
  1692. .u.func = _func, \
  1693. .debug = _debug, \
  1694. }
  1695. static struct v4l2_ioctl_info v4l2_ioctls[] = {
  1696. IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
  1697. IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
  1698. IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
  1699. IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
  1700. IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
  1701. IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
  1702. IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
  1703. IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
  1704. IOCTL_INFO_STD(VIDIOC_OVERLAY, vidioc_overlay, v4l_print_u32, INFO_FL_PRIO),
  1705. IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
  1706. IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
  1707. IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
  1708. IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
  1709. IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
  1710. IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
  1711. IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
  1712. IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
  1713. IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
  1714. IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
  1715. IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
  1716. IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
  1717. IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
  1718. IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
  1719. IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
  1720. IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
  1721. IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
  1722. IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
  1723. IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
  1724. IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
  1725. IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
  1726. IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
  1727. IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
  1728. IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
  1729. IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
  1730. IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
  1731. IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
  1732. IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
  1733. IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
  1734. IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
  1735. IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
  1736. IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
  1737. IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
  1738. IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
  1739. IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
  1740. IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
  1741. IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
  1742. IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
  1743. IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
  1744. IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
  1745. IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
  1746. IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
  1747. IOCTL_INFO_FNC(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
  1748. IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
  1749. IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  1750. IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
  1751. IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  1752. IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
  1753. IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
  1754. IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
  1755. IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  1756. IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  1757. IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
  1758. IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
  1759. IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
  1760. IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
  1761. IOCTL_INFO_FNC(VIDIOC_DBG_G_CHIP_IDENT, v4l_dbg_g_chip_ident, v4l_print_dbg_chip_ident, 0),
  1762. IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
  1763. IOCTL_INFO_STD(VIDIOC_ENUM_DV_PRESETS, vidioc_enum_dv_presets, v4l_print_dv_enum_presets, 0),
  1764. IOCTL_INFO_STD(VIDIOC_S_DV_PRESET, vidioc_s_dv_preset, v4l_print_dv_preset, INFO_FL_PRIO),
  1765. IOCTL_INFO_STD(VIDIOC_G_DV_PRESET, vidioc_g_dv_preset, v4l_print_dv_preset, 0),
  1766. IOCTL_INFO_STD(VIDIOC_QUERY_DV_PRESET, vidioc_query_dv_preset, v4l_print_dv_preset, 0),
  1767. IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
  1768. IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
  1769. IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
  1770. IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
  1771. IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
  1772. IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
  1773. IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
  1774. IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
  1775. IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
  1776. IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
  1777. IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
  1778. };
  1779. #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
  1780. bool v4l2_is_known_ioctl(unsigned int cmd)
  1781. {
  1782. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  1783. return false;
  1784. return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
  1785. }
  1786. struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
  1787. {
  1788. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  1789. return vdev->lock;
  1790. if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
  1791. return NULL;
  1792. if (vdev->queue && vdev->queue->lock &&
  1793. (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
  1794. return vdev->queue->lock;
  1795. return vdev->lock;
  1796. }
  1797. /* Common ioctl debug function. This function can be used by
  1798. external ioctl messages as well as internal V4L ioctl */
  1799. void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
  1800. {
  1801. const char *dir, *type;
  1802. if (prefix)
  1803. printk(KERN_DEBUG "%s: ", prefix);
  1804. switch (_IOC_TYPE(cmd)) {
  1805. case 'd':
  1806. type = "v4l2_int";
  1807. break;
  1808. case 'V':
  1809. if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
  1810. type = "v4l2";
  1811. break;
  1812. }
  1813. pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
  1814. return;
  1815. default:
  1816. type = "unknown";
  1817. break;
  1818. }
  1819. switch (_IOC_DIR(cmd)) {
  1820. case _IOC_NONE: dir = "--"; break;
  1821. case _IOC_READ: dir = "r-"; break;
  1822. case _IOC_WRITE: dir = "-w"; break;
  1823. case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
  1824. default: dir = "*ERR*"; break;
  1825. }
  1826. pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
  1827. type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
  1828. }
  1829. EXPORT_SYMBOL(v4l_printk_ioctl);
  1830. static long __video_do_ioctl(struct file *file,
  1831. unsigned int cmd, void *arg)
  1832. {
  1833. struct video_device *vfd = video_devdata(file);
  1834. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  1835. bool write_only = false;
  1836. struct v4l2_ioctl_info default_info;
  1837. const struct v4l2_ioctl_info *info;
  1838. void *fh = file->private_data;
  1839. struct v4l2_fh *vfh = NULL;
  1840. int use_fh_prio = 0;
  1841. int debug = vfd->debug;
  1842. long ret = -ENOTTY;
  1843. if (ops == NULL) {
  1844. pr_warn("%s: has no ioctl_ops.\n",
  1845. video_device_node_name(vfd));
  1846. return ret;
  1847. }
  1848. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  1849. vfh = file->private_data;
  1850. use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  1851. }
  1852. if (v4l2_is_known_ioctl(cmd)) {
  1853. info = &v4l2_ioctls[_IOC_NR(cmd)];
  1854. if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
  1855. !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
  1856. goto done;
  1857. if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
  1858. ret = v4l2_prio_check(vfd->prio, vfh->prio);
  1859. if (ret)
  1860. goto done;
  1861. }
  1862. } else {
  1863. default_info.ioctl = cmd;
  1864. default_info.flags = 0;
  1865. default_info.debug = v4l_print_default;
  1866. info = &default_info;
  1867. }
  1868. write_only = _IOC_DIR(cmd) == _IOC_WRITE;
  1869. if (write_only && debug > V4L2_DEBUG_IOCTL) {
  1870. v4l_printk_ioctl(video_device_node_name(vfd), cmd);
  1871. pr_cont(": ");
  1872. info->debug(arg, write_only);
  1873. }
  1874. if (info->flags & INFO_FL_STD) {
  1875. typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
  1876. const void *p = vfd->ioctl_ops;
  1877. const vidioc_op *vidioc = p + info->u.offset;
  1878. ret = (*vidioc)(file, fh, arg);
  1879. } else if (info->flags & INFO_FL_FUNC) {
  1880. ret = info->u.func(ops, file, fh, arg);
  1881. } else if (!ops->vidioc_default) {
  1882. ret = -ENOTTY;
  1883. } else {
  1884. ret = ops->vidioc_default(file, fh,
  1885. use_fh_prio ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
  1886. cmd, arg);
  1887. }
  1888. done:
  1889. if (debug) {
  1890. if (write_only && debug > V4L2_DEBUG_IOCTL) {
  1891. if (ret < 0)
  1892. printk(KERN_DEBUG "%s: error %ld\n",
  1893. video_device_node_name(vfd), ret);
  1894. return ret;
  1895. }
  1896. v4l_printk_ioctl(video_device_node_name(vfd), cmd);
  1897. if (ret < 0)
  1898. pr_cont(": error %ld\n", ret);
  1899. else if (debug == V4L2_DEBUG_IOCTL)
  1900. pr_cont("\n");
  1901. else if (_IOC_DIR(cmd) == _IOC_NONE)
  1902. info->debug(arg, write_only);
  1903. else {
  1904. pr_cont(": ");
  1905. info->debug(arg, write_only);
  1906. }
  1907. }
  1908. return ret;
  1909. }
  1910. static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
  1911. void * __user *user_ptr, void ***kernel_ptr)
  1912. {
  1913. int ret = 0;
  1914. switch (cmd) {
  1915. case VIDIOC_QUERYBUF:
  1916. case VIDIOC_QBUF:
  1917. case VIDIOC_DQBUF: {
  1918. struct v4l2_buffer *buf = parg;
  1919. if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
  1920. if (buf->length > VIDEO_MAX_PLANES) {
  1921. ret = -EINVAL;
  1922. break;
  1923. }
  1924. *user_ptr = (void __user *)buf->m.planes;
  1925. *kernel_ptr = (void *)&buf->m.planes;
  1926. *array_size = sizeof(struct v4l2_plane) * buf->length;
  1927. ret = 1;
  1928. }
  1929. break;
  1930. }
  1931. case VIDIOC_S_EXT_CTRLS:
  1932. case VIDIOC_G_EXT_CTRLS:
  1933. case VIDIOC_TRY_EXT_CTRLS: {
  1934. struct v4l2_ext_controls *ctrls = parg;
  1935. if (ctrls->count != 0) {
  1936. if (ctrls->count > V4L2_CID_MAX_CTRLS) {
  1937. ret = -EINVAL;
  1938. break;
  1939. }
  1940. *user_ptr = (void __user *)ctrls->controls;
  1941. *kernel_ptr = (void *)&ctrls->controls;
  1942. *array_size = sizeof(struct v4l2_ext_control)
  1943. * ctrls->count;
  1944. ret = 1;
  1945. }
  1946. break;
  1947. }
  1948. }
  1949. return ret;
  1950. }
  1951. long
  1952. video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
  1953. v4l2_kioctl func)
  1954. {
  1955. char sbuf[128];
  1956. void *mbuf = NULL;
  1957. void *parg = (void *)arg;
  1958. long err = -EINVAL;
  1959. bool has_array_args;
  1960. size_t array_size = 0;
  1961. void __user *user_ptr = NULL;
  1962. void **kernel_ptr = NULL;
  1963. /* Copy arguments into temp kernel buffer */
  1964. if (_IOC_DIR(cmd) != _IOC_NONE) {
  1965. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  1966. parg = sbuf;
  1967. } else {
  1968. /* too big to allocate from stack */
  1969. mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  1970. if (NULL == mbuf)
  1971. return -ENOMEM;
  1972. parg = mbuf;
  1973. }
  1974. err = -EFAULT;
  1975. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  1976. unsigned int n = _IOC_SIZE(cmd);
  1977. /*
  1978. * In some cases, only a few fields are used as input,
  1979. * i.e. when the app sets "index" and then the driver
  1980. * fills in the rest of the structure for the thing
  1981. * with that index. We only need to copy up the first
  1982. * non-input field.
  1983. */
  1984. if (v4l2_is_known_ioctl(cmd)) {
  1985. u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
  1986. if (flags & INFO_FL_CLEAR_MASK)
  1987. n = (flags & INFO_FL_CLEAR_MASK) >> 16;
  1988. }
  1989. if (copy_from_user(parg, (void __user *)arg, n))
  1990. goto out;
  1991. /* zero out anything we don't copy from userspace */
  1992. if (n < _IOC_SIZE(cmd))
  1993. memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
  1994. } else {
  1995. /* read-only ioctl */
  1996. memset(parg, 0, _IOC_SIZE(cmd));
  1997. }
  1998. }
  1999. err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
  2000. if (err < 0)
  2001. goto out;
  2002. has_array_args = err;
  2003. if (has_array_args) {
  2004. /*
  2005. * When adding new types of array args, make sure that the
  2006. * parent argument to ioctl (which contains the pointer to the
  2007. * array) fits into sbuf (so that mbuf will still remain
  2008. * unused up to here).
  2009. */
  2010. mbuf = kmalloc(array_size, GFP_KERNEL);
  2011. err = -ENOMEM;
  2012. if (NULL == mbuf)
  2013. goto out_array_args;
  2014. err = -EFAULT;
  2015. if (copy_from_user(mbuf, user_ptr, array_size))
  2016. goto out_array_args;
  2017. *kernel_ptr = mbuf;
  2018. }
  2019. /* Handles IOCTL */
  2020. err = func(file, cmd, parg);
  2021. if (err == -ENOIOCTLCMD)
  2022. err = -ENOTTY;
  2023. if (has_array_args) {
  2024. *kernel_ptr = user_ptr;
  2025. if (copy_to_user(user_ptr, mbuf, array_size))
  2026. err = -EFAULT;
  2027. goto out_array_args;
  2028. }
  2029. /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
  2030. results that must be returned. */
  2031. if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
  2032. goto out;
  2033. out_array_args:
  2034. /* Copy results into user buffer */
  2035. switch (_IOC_DIR(cmd)) {
  2036. case _IOC_READ:
  2037. case (_IOC_WRITE | _IOC_READ):
  2038. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  2039. err = -EFAULT;
  2040. break;
  2041. }
  2042. out:
  2043. kfree(mbuf);
  2044. return err;
  2045. }
  2046. EXPORT_SYMBOL(video_usercopy);
  2047. long video_ioctl2(struct file *file,
  2048. unsigned int cmd, unsigned long arg)
  2049. {
  2050. return video_usercopy(file, cmd, arg, __video_do_ioctl);
  2051. }
  2052. EXPORT_SYMBOL(video_ioctl2);