v4l2-ioctl.c 66 KB

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