v4l2-ioctl.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  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 int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
  748. {
  749. __u32 i;
  750. /* zero the reserved fields */
  751. c->reserved[0] = c->reserved[1] = 0;
  752. for (i = 0; i < c->count; i++)
  753. c->controls[i].reserved2[0] = 0;
  754. /* V4L2_CID_PRIVATE_BASE cannot be used as control class
  755. when using extended controls.
  756. Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
  757. is it allowed for backwards compatibility.
  758. */
  759. if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
  760. return 0;
  761. /* Check that all controls are from the same control class. */
  762. for (i = 0; i < c->count; i++) {
  763. if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
  764. c->error_idx = i;
  765. return 0;
  766. }
  767. }
  768. return 1;
  769. }
  770. static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
  771. {
  772. if (ops == NULL)
  773. return -EINVAL;
  774. switch (type) {
  775. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  776. if (ops->vidioc_g_fmt_vid_cap ||
  777. ops->vidioc_g_fmt_vid_cap_mplane)
  778. return 0;
  779. break;
  780. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  781. if (ops->vidioc_g_fmt_vid_cap_mplane)
  782. return 0;
  783. break;
  784. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  785. if (ops->vidioc_g_fmt_vid_overlay)
  786. return 0;
  787. break;
  788. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  789. if (ops->vidioc_g_fmt_vid_out ||
  790. ops->vidioc_g_fmt_vid_out_mplane)
  791. return 0;
  792. break;
  793. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  794. if (ops->vidioc_g_fmt_vid_out_mplane)
  795. return 0;
  796. break;
  797. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  798. if (ops->vidioc_g_fmt_vid_out_overlay)
  799. return 0;
  800. break;
  801. case V4L2_BUF_TYPE_VBI_CAPTURE:
  802. if (ops->vidioc_g_fmt_vbi_cap)
  803. return 0;
  804. break;
  805. case V4L2_BUF_TYPE_VBI_OUTPUT:
  806. if (ops->vidioc_g_fmt_vbi_out)
  807. return 0;
  808. break;
  809. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  810. if (ops->vidioc_g_fmt_sliced_vbi_cap)
  811. return 0;
  812. break;
  813. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  814. if (ops->vidioc_g_fmt_sliced_vbi_out)
  815. return 0;
  816. break;
  817. case V4L2_BUF_TYPE_PRIVATE:
  818. if (ops->vidioc_g_fmt_type_private)
  819. return 0;
  820. break;
  821. }
  822. return -EINVAL;
  823. }
  824. static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
  825. struct file *file, void *fh, void *arg)
  826. {
  827. struct v4l2_capability *cap = (struct v4l2_capability *)arg;
  828. cap->version = LINUX_VERSION_CODE;
  829. return ops->vidioc_querycap(file, fh, cap);
  830. }
  831. static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
  832. struct file *file, void *fh, void *arg)
  833. {
  834. return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
  835. }
  836. static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
  837. struct file *file, void *fh, void *arg)
  838. {
  839. return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
  840. }
  841. static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
  842. struct file *file, void *fh, void *arg)
  843. {
  844. struct video_device *vfd;
  845. u32 *p = arg;
  846. if (ops->vidioc_g_priority)
  847. return ops->vidioc_g_priority(file, fh, arg);
  848. vfd = video_devdata(file);
  849. *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
  850. return 0;
  851. }
  852. static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
  853. struct file *file, void *fh, void *arg)
  854. {
  855. struct video_device *vfd;
  856. struct v4l2_fh *vfh;
  857. u32 *p = arg;
  858. if (ops->vidioc_s_priority)
  859. return ops->vidioc_s_priority(file, fh, *p);
  860. vfd = video_devdata(file);
  861. vfh = file->private_data;
  862. return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
  863. }
  864. static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
  865. struct file *file, void *fh, void *arg)
  866. {
  867. struct v4l2_input *p = arg;
  868. /*
  869. * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
  870. * CAP_STD here based on ioctl handler provided by the
  871. * driver. If the driver doesn't support these
  872. * for a specific input, it must override these flags.
  873. */
  874. if (ops->vidioc_s_std)
  875. p->capabilities |= V4L2_IN_CAP_STD;
  876. if (ops->vidioc_s_dv_preset)
  877. p->capabilities |= V4L2_IN_CAP_PRESETS;
  878. if (ops->vidioc_s_dv_timings)
  879. p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
  880. return ops->vidioc_enum_input(file, fh, p);
  881. }
  882. static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
  883. struct file *file, void *fh, void *arg)
  884. {
  885. struct v4l2_output *p = arg;
  886. /*
  887. * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
  888. * CAP_STD here based on ioctl handler provided by the
  889. * driver. If the driver doesn't support these
  890. * for a specific output, it must override these flags.
  891. */
  892. if (ops->vidioc_s_std)
  893. p->capabilities |= V4L2_OUT_CAP_STD;
  894. if (ops->vidioc_s_dv_preset)
  895. p->capabilities |= V4L2_OUT_CAP_PRESETS;
  896. if (ops->vidioc_s_dv_timings)
  897. p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
  898. return ops->vidioc_enum_output(file, fh, p);
  899. }
  900. static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
  901. struct file *file, void *fh, void *arg)
  902. {
  903. struct v4l2_fmtdesc *p = arg;
  904. switch (p->type) {
  905. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  906. if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
  907. break;
  908. return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
  909. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  910. if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
  911. break;
  912. return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
  913. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  914. if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
  915. break;
  916. return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
  917. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  918. if (unlikely(!ops->vidioc_enum_fmt_vid_out))
  919. break;
  920. return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
  921. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  922. if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
  923. break;
  924. return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
  925. case V4L2_BUF_TYPE_PRIVATE:
  926. if (unlikely(!ops->vidioc_enum_fmt_type_private))
  927. break;
  928. return ops->vidioc_enum_fmt_type_private(file, fh, arg);
  929. }
  930. return -EINVAL;
  931. }
  932. static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
  933. struct file *file, void *fh, void *arg)
  934. {
  935. struct v4l2_format *p = arg;
  936. switch (p->type) {
  937. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  938. if (unlikely(!ops->vidioc_g_fmt_vid_cap))
  939. break;
  940. return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
  941. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  942. if (unlikely(!ops->vidioc_g_fmt_vid_cap_mplane))
  943. break;
  944. return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
  945. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  946. if (unlikely(!ops->vidioc_g_fmt_vid_overlay))
  947. break;
  948. return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
  949. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  950. if (unlikely(!ops->vidioc_g_fmt_vid_out))
  951. break;
  952. return ops->vidioc_g_fmt_vid_out(file, fh, arg);
  953. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  954. if (unlikely(!ops->vidioc_g_fmt_vid_out_mplane))
  955. break;
  956. return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
  957. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  958. if (unlikely(!ops->vidioc_g_fmt_vid_out_overlay))
  959. break;
  960. return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
  961. case V4L2_BUF_TYPE_VBI_CAPTURE:
  962. if (unlikely(!ops->vidioc_g_fmt_vbi_cap))
  963. break;
  964. return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
  965. case V4L2_BUF_TYPE_VBI_OUTPUT:
  966. if (unlikely(!ops->vidioc_g_fmt_vbi_out))
  967. break;
  968. return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
  969. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  970. if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_cap))
  971. break;
  972. return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
  973. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  974. if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_out))
  975. break;
  976. return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
  977. case V4L2_BUF_TYPE_PRIVATE:
  978. if (unlikely(!ops->vidioc_g_fmt_type_private))
  979. break;
  980. return ops->vidioc_g_fmt_type_private(file, fh, arg);
  981. }
  982. return -EINVAL;
  983. }
  984. static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
  985. struct file *file, void *fh, void *arg)
  986. {
  987. struct v4l2_format *p = arg;
  988. switch (p->type) {
  989. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  990. if (unlikely(!ops->vidioc_s_fmt_vid_cap))
  991. break;
  992. CLEAR_AFTER_FIELD(p, fmt.pix);
  993. return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
  994. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  995. if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
  996. break;
  997. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  998. return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
  999. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1000. if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
  1001. break;
  1002. CLEAR_AFTER_FIELD(p, fmt.win);
  1003. return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
  1004. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1005. if (unlikely(!ops->vidioc_s_fmt_vid_out))
  1006. break;
  1007. CLEAR_AFTER_FIELD(p, fmt.pix);
  1008. return ops->vidioc_s_fmt_vid_out(file, fh, arg);
  1009. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1010. if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
  1011. break;
  1012. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1013. return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
  1014. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1015. if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
  1016. break;
  1017. CLEAR_AFTER_FIELD(p, fmt.win);
  1018. return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
  1019. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1020. if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
  1021. break;
  1022. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1023. return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
  1024. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1025. if (unlikely(!ops->vidioc_s_fmt_vbi_out))
  1026. break;
  1027. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1028. return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
  1029. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1030. if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
  1031. break;
  1032. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1033. return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
  1034. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1035. if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
  1036. break;
  1037. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1038. return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
  1039. case V4L2_BUF_TYPE_PRIVATE:
  1040. if (unlikely(!ops->vidioc_s_fmt_type_private))
  1041. break;
  1042. return ops->vidioc_s_fmt_type_private(file, fh, arg);
  1043. }
  1044. return -EINVAL;
  1045. }
  1046. static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
  1047. struct file *file, void *fh, void *arg)
  1048. {
  1049. struct v4l2_format *p = arg;
  1050. switch (p->type) {
  1051. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1052. if (unlikely(!ops->vidioc_try_fmt_vid_cap))
  1053. break;
  1054. CLEAR_AFTER_FIELD(p, fmt.pix);
  1055. return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
  1056. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1057. if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
  1058. break;
  1059. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1060. return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
  1061. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1062. if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
  1063. break;
  1064. CLEAR_AFTER_FIELD(p, fmt.win);
  1065. return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
  1066. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1067. if (unlikely(!ops->vidioc_try_fmt_vid_out))
  1068. break;
  1069. CLEAR_AFTER_FIELD(p, fmt.pix);
  1070. return ops->vidioc_try_fmt_vid_out(file, fh, arg);
  1071. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1072. if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
  1073. break;
  1074. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1075. return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
  1076. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1077. if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
  1078. break;
  1079. CLEAR_AFTER_FIELD(p, fmt.win);
  1080. return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
  1081. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1082. if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
  1083. break;
  1084. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1085. return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
  1086. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1087. if (unlikely(!ops->vidioc_try_fmt_vbi_out))
  1088. break;
  1089. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1090. return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
  1091. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1092. if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
  1093. break;
  1094. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1095. return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
  1096. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1097. if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
  1098. break;
  1099. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1100. return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
  1101. case V4L2_BUF_TYPE_PRIVATE:
  1102. if (unlikely(!ops->vidioc_try_fmt_type_private))
  1103. break;
  1104. return ops->vidioc_try_fmt_type_private(file, fh, arg);
  1105. }
  1106. return -EINVAL;
  1107. }
  1108. static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
  1109. struct file *file, void *fh, void *arg)
  1110. {
  1111. return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
  1112. }
  1113. static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
  1114. struct file *file, void *fh, void *arg)
  1115. {
  1116. return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
  1117. }
  1118. static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
  1119. struct file *file, void *fh, void *arg)
  1120. {
  1121. struct video_device *vfd = video_devdata(file);
  1122. struct v4l2_tuner *p = arg;
  1123. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1124. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1125. return ops->vidioc_g_tuner(file, fh, p);
  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_frequency(const struct v4l2_ioctl_ops *ops,
  1137. struct file *file, void *fh, void *arg)
  1138. {
  1139. struct video_device *vfd = video_devdata(file);
  1140. struct v4l2_frequency *p = arg;
  1141. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1142. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1143. return ops->vidioc_g_frequency(file, fh, p);
  1144. }
  1145. static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
  1146. struct file *file, void *fh, void *arg)
  1147. {
  1148. struct video_device *vfd = video_devdata(file);
  1149. struct v4l2_frequency *p = arg;
  1150. enum v4l2_tuner_type type;
  1151. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1152. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1153. if (p->type != type)
  1154. return -EINVAL;
  1155. return ops->vidioc_s_frequency(file, fh, p);
  1156. }
  1157. static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
  1158. struct file *file, void *fh, void *arg)
  1159. {
  1160. struct video_device *vfd = video_devdata(file);
  1161. struct v4l2_standard *p = arg;
  1162. v4l2_std_id id = vfd->tvnorms, curr_id = 0;
  1163. unsigned int index = p->index, i, j = 0;
  1164. const char *descr = "";
  1165. /* Return norm array in a canonical way */
  1166. for (i = 0; i <= index && id; i++) {
  1167. /* last std value in the standards array is 0, so this
  1168. while always ends there since (id & 0) == 0. */
  1169. while ((id & standards[j].std) != standards[j].std)
  1170. j++;
  1171. curr_id = standards[j].std;
  1172. descr = standards[j].descr;
  1173. j++;
  1174. if (curr_id == 0)
  1175. break;
  1176. if (curr_id != V4L2_STD_PAL &&
  1177. curr_id != V4L2_STD_SECAM &&
  1178. curr_id != V4L2_STD_NTSC)
  1179. id &= ~curr_id;
  1180. }
  1181. if (i <= index)
  1182. return -EINVAL;
  1183. v4l2_video_std_construct(p, curr_id, descr);
  1184. return 0;
  1185. }
  1186. static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
  1187. struct file *file, void *fh, void *arg)
  1188. {
  1189. struct video_device *vfd = video_devdata(file);
  1190. v4l2_std_id *id = arg;
  1191. /* Calls the specific handler */
  1192. if (ops->vidioc_g_std)
  1193. return ops->vidioc_g_std(file, fh, arg);
  1194. if (vfd->current_norm) {
  1195. *id = vfd->current_norm;
  1196. return 0;
  1197. }
  1198. return -ENOTTY;
  1199. }
  1200. static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
  1201. struct file *file, void *fh, void *arg)
  1202. {
  1203. struct video_device *vfd = video_devdata(file);
  1204. v4l2_std_id *id = arg, norm;
  1205. int ret;
  1206. norm = (*id) & vfd->tvnorms;
  1207. if (vfd->tvnorms && !norm) /* Check if std is supported */
  1208. return -EINVAL;
  1209. /* Calls the specific handler */
  1210. ret = ops->vidioc_s_std(file, fh, &norm);
  1211. /* Updates standard information */
  1212. if (ret >= 0)
  1213. vfd->current_norm = norm;
  1214. return ret;
  1215. }
  1216. static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
  1217. struct file *file, void *fh, void *arg)
  1218. {
  1219. struct video_device *vfd = video_devdata(file);
  1220. v4l2_std_id *p = arg;
  1221. /*
  1222. * If nothing detected, it should return all supported
  1223. * standard.
  1224. * Drivers just need to mask the std argument, in order
  1225. * to remove the standards that don't apply from the mask.
  1226. * This means that tuners, audio and video decoders can join
  1227. * their efforts to improve the standards detection.
  1228. */
  1229. *p = vfd->tvnorms;
  1230. return ops->vidioc_querystd(file, fh, arg);
  1231. }
  1232. static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
  1233. struct file *file, void *fh, void *arg)
  1234. {
  1235. struct video_device *vfd = video_devdata(file);
  1236. struct v4l2_hw_freq_seek *p = arg;
  1237. enum v4l2_tuner_type type;
  1238. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1239. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1240. if (p->type != type)
  1241. return -EINVAL;
  1242. return ops->vidioc_s_hw_freq_seek(file, fh, p);
  1243. }
  1244. static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
  1245. struct file *file, void *fh, void *arg)
  1246. {
  1247. struct v4l2_requestbuffers *p = arg;
  1248. int ret = check_fmt(ops, p->type);
  1249. if (ret)
  1250. return ret;
  1251. if (p->type < V4L2_BUF_TYPE_PRIVATE)
  1252. CLEAR_AFTER_FIELD(p, memory);
  1253. return ops->vidioc_reqbufs(file, fh, p);
  1254. }
  1255. static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
  1256. struct file *file, void *fh, void *arg)
  1257. {
  1258. struct v4l2_buffer *p = arg;
  1259. int ret = check_fmt(ops, p->type);
  1260. return ret ? ret : ops->vidioc_querybuf(file, fh, p);
  1261. }
  1262. static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
  1263. struct file *file, void *fh, void *arg)
  1264. {
  1265. struct v4l2_buffer *p = arg;
  1266. int ret = check_fmt(ops, p->type);
  1267. return ret ? ret : ops->vidioc_qbuf(file, fh, p);
  1268. }
  1269. static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
  1270. struct file *file, void *fh, void *arg)
  1271. {
  1272. struct v4l2_buffer *p = arg;
  1273. int ret = check_fmt(ops, p->type);
  1274. return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
  1275. }
  1276. static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
  1277. struct file *file, void *fh, void *arg)
  1278. {
  1279. struct v4l2_create_buffers *create = arg;
  1280. int ret = check_fmt(ops, create->format.type);
  1281. return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
  1282. }
  1283. static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
  1284. struct file *file, void *fh, void *arg)
  1285. {
  1286. struct v4l2_buffer *b = arg;
  1287. int ret = check_fmt(ops, b->type);
  1288. return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
  1289. }
  1290. static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
  1291. struct file *file, void *fh, void *arg)
  1292. {
  1293. struct video_device *vfd = video_devdata(file);
  1294. struct v4l2_streamparm *p = arg;
  1295. v4l2_std_id std;
  1296. int ret = check_fmt(ops, p->type);
  1297. if (ret)
  1298. return ret;
  1299. if (ops->vidioc_g_parm)
  1300. return ops->vidioc_g_parm(file, fh, p);
  1301. std = vfd->current_norm;
  1302. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1303. p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1304. return -EINVAL;
  1305. p->parm.capture.readbuffers = 2;
  1306. if (ops->vidioc_g_std)
  1307. ret = ops->vidioc_g_std(file, fh, &std);
  1308. if (ret == 0)
  1309. v4l2_video_std_frame_period(std,
  1310. &p->parm.capture.timeperframe);
  1311. return ret;
  1312. }
  1313. static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
  1314. struct file *file, void *fh, void *arg)
  1315. {
  1316. struct v4l2_streamparm *p = arg;
  1317. int ret = check_fmt(ops, p->type);
  1318. return ret ? ret : ops->vidioc_s_parm(file, fh, p);
  1319. }
  1320. static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
  1321. struct file *file, void *fh, void *arg)
  1322. {
  1323. struct video_device *vfd = video_devdata(file);
  1324. struct v4l2_queryctrl *p = arg;
  1325. struct v4l2_fh *vfh = fh;
  1326. if (vfh && vfh->ctrl_handler)
  1327. return v4l2_queryctrl(vfh->ctrl_handler, p);
  1328. if (vfd->ctrl_handler)
  1329. return v4l2_queryctrl(vfd->ctrl_handler, p);
  1330. if (ops->vidioc_queryctrl)
  1331. return ops->vidioc_queryctrl(file, fh, p);
  1332. return -ENOTTY;
  1333. }
  1334. static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
  1335. struct file *file, void *fh, void *arg)
  1336. {
  1337. struct video_device *vfd = video_devdata(file);
  1338. struct v4l2_querymenu *p = arg;
  1339. struct v4l2_fh *vfh = fh;
  1340. if (vfh && vfh->ctrl_handler)
  1341. return v4l2_querymenu(vfh->ctrl_handler, p);
  1342. if (vfd->ctrl_handler)
  1343. return v4l2_querymenu(vfd->ctrl_handler, p);
  1344. if (ops->vidioc_querymenu)
  1345. return ops->vidioc_querymenu(file, fh, p);
  1346. return -ENOTTY;
  1347. }
  1348. static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
  1349. struct file *file, void *fh, void *arg)
  1350. {
  1351. struct video_device *vfd = video_devdata(file);
  1352. struct v4l2_control *p = arg;
  1353. struct v4l2_fh *vfh = fh;
  1354. struct v4l2_ext_controls ctrls;
  1355. struct v4l2_ext_control ctrl;
  1356. if (vfh && vfh->ctrl_handler)
  1357. return v4l2_g_ctrl(vfh->ctrl_handler, p);
  1358. if (vfd->ctrl_handler)
  1359. return v4l2_g_ctrl(vfd->ctrl_handler, p);
  1360. if (ops->vidioc_g_ctrl)
  1361. return ops->vidioc_g_ctrl(file, fh, p);
  1362. if (ops->vidioc_g_ext_ctrls == NULL)
  1363. return -ENOTTY;
  1364. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1365. ctrls.count = 1;
  1366. ctrls.controls = &ctrl;
  1367. ctrl.id = p->id;
  1368. ctrl.value = p->value;
  1369. if (check_ext_ctrls(&ctrls, 1)) {
  1370. int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
  1371. if (ret == 0)
  1372. p->value = ctrl.value;
  1373. return ret;
  1374. }
  1375. return -EINVAL;
  1376. }
  1377. static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
  1378. struct file *file, void *fh, void *arg)
  1379. {
  1380. struct video_device *vfd = video_devdata(file);
  1381. struct v4l2_control *p = arg;
  1382. struct v4l2_fh *vfh = fh;
  1383. struct v4l2_ext_controls ctrls;
  1384. struct v4l2_ext_control ctrl;
  1385. if (vfh && vfh->ctrl_handler)
  1386. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
  1387. if (vfd->ctrl_handler)
  1388. return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
  1389. if (ops->vidioc_s_ctrl)
  1390. return ops->vidioc_s_ctrl(file, fh, p);
  1391. if (ops->vidioc_s_ext_ctrls == NULL)
  1392. return -ENOTTY;
  1393. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1394. ctrls.count = 1;
  1395. ctrls.controls = &ctrl;
  1396. ctrl.id = p->id;
  1397. ctrl.value = p->value;
  1398. if (check_ext_ctrls(&ctrls, 1))
  1399. return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
  1400. return -EINVAL;
  1401. }
  1402. static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1403. struct file *file, void *fh, void *arg)
  1404. {
  1405. struct video_device *vfd = video_devdata(file);
  1406. struct v4l2_ext_controls *p = arg;
  1407. struct v4l2_fh *vfh = fh;
  1408. p->error_idx = p->count;
  1409. if (vfh && vfh->ctrl_handler)
  1410. return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
  1411. if (vfd->ctrl_handler)
  1412. return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
  1413. if (ops->vidioc_g_ext_ctrls == NULL)
  1414. return -ENOTTY;
  1415. return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
  1416. -EINVAL;
  1417. }
  1418. static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1419. struct file *file, void *fh, void *arg)
  1420. {
  1421. struct video_device *vfd = video_devdata(file);
  1422. struct v4l2_ext_controls *p = arg;
  1423. struct v4l2_fh *vfh = fh;
  1424. p->error_idx = p->count;
  1425. if (vfh && vfh->ctrl_handler)
  1426. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
  1427. if (vfd->ctrl_handler)
  1428. return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
  1429. if (ops->vidioc_s_ext_ctrls == NULL)
  1430. return -ENOTTY;
  1431. return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
  1432. -EINVAL;
  1433. }
  1434. static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1435. struct file *file, void *fh, void *arg)
  1436. {
  1437. struct video_device *vfd = video_devdata(file);
  1438. struct v4l2_ext_controls *p = arg;
  1439. struct v4l2_fh *vfh = fh;
  1440. p->error_idx = p->count;
  1441. if (vfh && vfh->ctrl_handler)
  1442. return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
  1443. if (vfd->ctrl_handler)
  1444. return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
  1445. if (ops->vidioc_try_ext_ctrls == NULL)
  1446. return -ENOTTY;
  1447. return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
  1448. -EINVAL;
  1449. }
  1450. static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
  1451. struct file *file, void *fh, void *arg)
  1452. {
  1453. struct v4l2_crop *p = arg;
  1454. struct v4l2_selection s = {
  1455. .type = p->type,
  1456. };
  1457. int ret;
  1458. if (ops->vidioc_g_crop)
  1459. return ops->vidioc_g_crop(file, fh, p);
  1460. /* simulate capture crop using selection api */
  1461. /* crop means compose for output devices */
  1462. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1463. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1464. else
  1465. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1466. ret = ops->vidioc_g_selection(file, fh, &s);
  1467. /* copying results to old structure on success */
  1468. if (!ret)
  1469. p->c = s.r;
  1470. return ret;
  1471. }
  1472. static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
  1473. struct file *file, void *fh, void *arg)
  1474. {
  1475. struct v4l2_crop *p = arg;
  1476. struct v4l2_selection s = {
  1477. .type = p->type,
  1478. .r = p->c,
  1479. };
  1480. if (ops->vidioc_s_crop)
  1481. return ops->vidioc_s_crop(file, fh, p);
  1482. /* simulate capture crop using selection api */
  1483. /* crop means compose for output devices */
  1484. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1485. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1486. else
  1487. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1488. return ops->vidioc_s_selection(file, fh, &s);
  1489. }
  1490. static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
  1491. struct file *file, void *fh, void *arg)
  1492. {
  1493. struct v4l2_cropcap *p = arg;
  1494. struct v4l2_selection s = { .type = p->type };
  1495. int ret;
  1496. if (ops->vidioc_cropcap)
  1497. return ops->vidioc_cropcap(file, fh, p);
  1498. /* obtaining bounds */
  1499. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1500. s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
  1501. else
  1502. s.target = V4L2_SEL_TGT_CROP_BOUNDS;
  1503. ret = ops->vidioc_g_selection(file, fh, &s);
  1504. if (ret)
  1505. return ret;
  1506. p->bounds = s.r;
  1507. /* obtaining defrect */
  1508. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1509. s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
  1510. else
  1511. s.target = V4L2_SEL_TGT_CROP_DEFAULT;
  1512. ret = ops->vidioc_g_selection(file, fh, &s);
  1513. if (ret)
  1514. return ret;
  1515. p->defrect = s.r;
  1516. /* setting trivial pixelaspect */
  1517. p->pixelaspect.numerator = 1;
  1518. p->pixelaspect.denominator = 1;
  1519. return 0;
  1520. }
  1521. static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
  1522. struct file *file, void *fh, void *arg)
  1523. {
  1524. struct video_device *vfd = video_devdata(file);
  1525. int ret;
  1526. if (vfd->v4l2_dev)
  1527. pr_info("%s: ================= START STATUS =================\n",
  1528. vfd->v4l2_dev->name);
  1529. ret = ops->vidioc_log_status(file, fh);
  1530. if (vfd->v4l2_dev)
  1531. pr_info("%s: ================== END STATUS ==================\n",
  1532. vfd->v4l2_dev->name);
  1533. return ret;
  1534. }
  1535. static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
  1536. struct file *file, void *fh, void *arg)
  1537. {
  1538. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1539. struct v4l2_dbg_register *p = arg;
  1540. if (!capable(CAP_SYS_ADMIN))
  1541. return -EPERM;
  1542. return ops->vidioc_g_register(file, fh, p);
  1543. #else
  1544. return -ENOTTY;
  1545. #endif
  1546. }
  1547. static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
  1548. struct file *file, void *fh, void *arg)
  1549. {
  1550. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1551. struct v4l2_dbg_register *p = arg;
  1552. if (!capable(CAP_SYS_ADMIN))
  1553. return -EPERM;
  1554. return ops->vidioc_s_register(file, fh, p);
  1555. #else
  1556. return -ENOTTY;
  1557. #endif
  1558. }
  1559. static int v4l_dbg_g_chip_ident(const struct v4l2_ioctl_ops *ops,
  1560. struct file *file, void *fh, void *arg)
  1561. {
  1562. struct v4l2_dbg_chip_ident *p = arg;
  1563. p->ident = V4L2_IDENT_NONE;
  1564. p->revision = 0;
  1565. return ops->vidioc_g_chip_ident(file, fh, p);
  1566. }
  1567. static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
  1568. struct file *file, void *fh, void *arg)
  1569. {
  1570. return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
  1571. }
  1572. static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
  1573. struct file *file, void *fh, void *arg)
  1574. {
  1575. return ops->vidioc_subscribe_event(fh, arg);
  1576. }
  1577. static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
  1578. struct file *file, void *fh, void *arg)
  1579. {
  1580. return ops->vidioc_unsubscribe_event(fh, arg);
  1581. }
  1582. static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
  1583. struct file *file, void *fh, void *arg)
  1584. {
  1585. struct v4l2_sliced_vbi_cap *p = arg;
  1586. /* Clear up to type, everything after type is zeroed already */
  1587. memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
  1588. return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
  1589. }
  1590. struct v4l2_ioctl_info {
  1591. unsigned int ioctl;
  1592. u32 flags;
  1593. const char * const name;
  1594. union {
  1595. u32 offset;
  1596. int (*func)(const struct v4l2_ioctl_ops *ops,
  1597. struct file *file, void *fh, void *p);
  1598. };
  1599. void (*debug)(const void *arg, bool write_only);
  1600. };
  1601. /* This control needs a priority check */
  1602. #define INFO_FL_PRIO (1 << 0)
  1603. /* This control can be valid if the filehandle passes a control handler. */
  1604. #define INFO_FL_CTRL (1 << 1)
  1605. /* This is a standard ioctl, no need for special code */
  1606. #define INFO_FL_STD (1 << 2)
  1607. /* This is ioctl has its own function */
  1608. #define INFO_FL_FUNC (1 << 3)
  1609. /* Zero struct from after the field to the end */
  1610. #define INFO_FL_CLEAR(v4l2_struct, field) \
  1611. ((offsetof(struct v4l2_struct, field) + \
  1612. sizeof(((struct v4l2_struct *)0)->field)) << 16)
  1613. #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
  1614. #define IOCTL_INFO(_ioctl, _flags) [_IOC_NR(_ioctl)] = { \
  1615. .ioctl = _ioctl, \
  1616. .flags = _flags, \
  1617. .name = #_ioctl, \
  1618. }
  1619. #define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
  1620. [_IOC_NR(_ioctl)] = { \
  1621. .ioctl = _ioctl, \
  1622. .flags = _flags | INFO_FL_STD, \
  1623. .name = #_ioctl, \
  1624. .offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
  1625. .debug = _debug, \
  1626. }
  1627. #define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
  1628. [_IOC_NR(_ioctl)] = { \
  1629. .ioctl = _ioctl, \
  1630. .flags = _flags | INFO_FL_FUNC, \
  1631. .name = #_ioctl, \
  1632. .func = _func, \
  1633. .debug = _debug, \
  1634. }
  1635. static struct v4l2_ioctl_info v4l2_ioctls[] = {
  1636. IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
  1637. IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
  1638. IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
  1639. IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
  1640. IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO),
  1641. IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_CLEAR(v4l2_buffer, length)),
  1642. IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
  1643. IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
  1644. IOCTL_INFO_STD(VIDIOC_OVERLAY, vidioc_overlay, v4l_print_u32, INFO_FL_PRIO),
  1645. IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, 0),
  1646. IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, 0),
  1647. IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO),
  1648. IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO),
  1649. IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
  1650. IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
  1651. IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
  1652. IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
  1653. IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
  1654. IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
  1655. IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
  1656. IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
  1657. IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
  1658. IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
  1659. IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
  1660. IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
  1661. IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
  1662. IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
  1663. IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
  1664. IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
  1665. IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
  1666. IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
  1667. IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
  1668. IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
  1669. IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
  1670. IOCTL_INFO_STD(VIDIOC_G_MODULATOR, vidioc_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
  1671. IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
  1672. IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
  1673. IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
  1674. IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
  1675. IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
  1676. IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
  1677. IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
  1678. IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
  1679. IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
  1680. IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
  1681. IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
  1682. IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
  1683. IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
  1684. IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
  1685. IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
  1686. IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
  1687. 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)),
  1688. IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
  1689. IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  1690. IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
  1691. IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, 0),
  1692. IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
  1693. IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
  1694. IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
  1695. IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  1696. IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  1697. IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
  1698. IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
  1699. IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
  1700. IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
  1701. IOCTL_INFO_FNC(VIDIOC_DBG_G_CHIP_IDENT, v4l_dbg_g_chip_ident, v4l_print_dbg_chip_ident, 0),
  1702. IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
  1703. IOCTL_INFO_STD(VIDIOC_ENUM_DV_PRESETS, vidioc_enum_dv_presets, v4l_print_dv_enum_presets, 0),
  1704. IOCTL_INFO_STD(VIDIOC_S_DV_PRESET, vidioc_s_dv_preset, v4l_print_dv_preset, INFO_FL_PRIO),
  1705. IOCTL_INFO_STD(VIDIOC_G_DV_PRESET, vidioc_g_dv_preset, v4l_print_dv_preset, 0),
  1706. IOCTL_INFO_STD(VIDIOC_QUERY_DV_PRESET, vidioc_query_dv_preset, v4l_print_dv_preset, 0),
  1707. IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
  1708. IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
  1709. IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
  1710. IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
  1711. IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
  1712. IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO),
  1713. IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, 0),
  1714. IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
  1715. IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
  1716. IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, 0),
  1717. };
  1718. #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
  1719. bool v4l2_is_known_ioctl(unsigned int cmd)
  1720. {
  1721. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  1722. return false;
  1723. return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
  1724. }
  1725. /* Common ioctl debug function. This function can be used by
  1726. external ioctl messages as well as internal V4L ioctl */
  1727. void v4l_printk_ioctl(unsigned int cmd)
  1728. {
  1729. const char *dir, *type;
  1730. switch (_IOC_TYPE(cmd)) {
  1731. case 'd':
  1732. type = "v4l2_int";
  1733. break;
  1734. case 'V':
  1735. if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
  1736. type = "v4l2";
  1737. break;
  1738. }
  1739. pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
  1740. return;
  1741. default:
  1742. type = "unknown";
  1743. break;
  1744. }
  1745. switch (_IOC_DIR(cmd)) {
  1746. case _IOC_NONE: dir = "--"; break;
  1747. case _IOC_READ: dir = "r-"; break;
  1748. case _IOC_WRITE: dir = "-w"; break;
  1749. case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
  1750. default: dir = "*ERR*"; break;
  1751. }
  1752. pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
  1753. type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
  1754. }
  1755. EXPORT_SYMBOL(v4l_printk_ioctl);
  1756. static long __video_do_ioctl(struct file *file,
  1757. unsigned int cmd, void *arg)
  1758. {
  1759. struct video_device *vfd = video_devdata(file);
  1760. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  1761. bool write_only = false;
  1762. struct v4l2_ioctl_info default_info;
  1763. const struct v4l2_ioctl_info *info;
  1764. void *fh = file->private_data;
  1765. struct v4l2_fh *vfh = NULL;
  1766. int use_fh_prio = 0;
  1767. long ret = -ENOTTY;
  1768. if (ops == NULL) {
  1769. printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
  1770. vfd->name);
  1771. return ret;
  1772. }
  1773. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  1774. vfh = file->private_data;
  1775. use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  1776. }
  1777. if (v4l2_is_known_ioctl(cmd)) {
  1778. info = &v4l2_ioctls[_IOC_NR(cmd)];
  1779. if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
  1780. !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
  1781. goto done;
  1782. if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
  1783. ret = v4l2_prio_check(vfd->prio, vfh->prio);
  1784. if (ret)
  1785. goto done;
  1786. }
  1787. } else {
  1788. default_info.ioctl = cmd;
  1789. default_info.flags = 0;
  1790. default_info.debug = NULL;
  1791. info = &default_info;
  1792. }
  1793. write_only = _IOC_DIR(cmd) == _IOC_WRITE;
  1794. if (info->debug && write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
  1795. v4l_print_ioctl(vfd->name, cmd);
  1796. pr_cont(": ");
  1797. info->debug(arg, write_only);
  1798. }
  1799. if (info->flags & INFO_FL_STD) {
  1800. typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
  1801. const void *p = vfd->ioctl_ops;
  1802. const vidioc_op *vidioc = p + info->offset;
  1803. ret = (*vidioc)(file, fh, arg);
  1804. goto done;
  1805. } else if (info->flags & INFO_FL_FUNC) {
  1806. ret = info->func(ops, file, fh, arg);
  1807. goto done;
  1808. }
  1809. switch (cmd) {
  1810. default:
  1811. if (!ops->vidioc_default)
  1812. break;
  1813. ret = ops->vidioc_default(file, fh, use_fh_prio ?
  1814. v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
  1815. cmd, arg);
  1816. break;
  1817. } /* switch */
  1818. done:
  1819. if (vfd->debug) {
  1820. if (write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
  1821. if (ret < 0)
  1822. printk(KERN_DEBUG "%s: error %ld\n",
  1823. video_device_node_name(vfd), ret);
  1824. return ret;
  1825. }
  1826. v4l_print_ioctl(vfd->name, cmd);
  1827. if (ret < 0)
  1828. pr_cont(": error %ld\n", ret);
  1829. else if (vfd->debug == V4L2_DEBUG_IOCTL)
  1830. pr_cont("\n");
  1831. else if (!info->debug)
  1832. return ret;
  1833. else if (_IOC_DIR(cmd) == _IOC_NONE)
  1834. info->debug(arg, write_only);
  1835. else {
  1836. pr_cont(": ");
  1837. info->debug(arg, write_only);
  1838. }
  1839. }
  1840. return ret;
  1841. }
  1842. static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
  1843. void * __user *user_ptr, void ***kernel_ptr)
  1844. {
  1845. int ret = 0;
  1846. switch (cmd) {
  1847. case VIDIOC_QUERYBUF:
  1848. case VIDIOC_QBUF:
  1849. case VIDIOC_DQBUF: {
  1850. struct v4l2_buffer *buf = parg;
  1851. if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
  1852. if (buf->length > VIDEO_MAX_PLANES) {
  1853. ret = -EINVAL;
  1854. break;
  1855. }
  1856. *user_ptr = (void __user *)buf->m.planes;
  1857. *kernel_ptr = (void *)&buf->m.planes;
  1858. *array_size = sizeof(struct v4l2_plane) * buf->length;
  1859. ret = 1;
  1860. }
  1861. break;
  1862. }
  1863. case VIDIOC_S_EXT_CTRLS:
  1864. case VIDIOC_G_EXT_CTRLS:
  1865. case VIDIOC_TRY_EXT_CTRLS: {
  1866. struct v4l2_ext_controls *ctrls = parg;
  1867. if (ctrls->count != 0) {
  1868. if (ctrls->count > V4L2_CID_MAX_CTRLS) {
  1869. ret = -EINVAL;
  1870. break;
  1871. }
  1872. *user_ptr = (void __user *)ctrls->controls;
  1873. *kernel_ptr = (void *)&ctrls->controls;
  1874. *array_size = sizeof(struct v4l2_ext_control)
  1875. * ctrls->count;
  1876. ret = 1;
  1877. }
  1878. break;
  1879. }
  1880. }
  1881. return ret;
  1882. }
  1883. long
  1884. video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
  1885. v4l2_kioctl func)
  1886. {
  1887. char sbuf[128];
  1888. void *mbuf = NULL;
  1889. void *parg = (void *)arg;
  1890. long err = -EINVAL;
  1891. bool has_array_args;
  1892. size_t array_size = 0;
  1893. void __user *user_ptr = NULL;
  1894. void **kernel_ptr = NULL;
  1895. /* Copy arguments into temp kernel buffer */
  1896. if (_IOC_DIR(cmd) != _IOC_NONE) {
  1897. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  1898. parg = sbuf;
  1899. } else {
  1900. /* too big to allocate from stack */
  1901. mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  1902. if (NULL == mbuf)
  1903. return -ENOMEM;
  1904. parg = mbuf;
  1905. }
  1906. err = -EFAULT;
  1907. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  1908. unsigned int n = _IOC_SIZE(cmd);
  1909. /*
  1910. * In some cases, only a few fields are used as input,
  1911. * i.e. when the app sets "index" and then the driver
  1912. * fills in the rest of the structure for the thing
  1913. * with that index. We only need to copy up the first
  1914. * non-input field.
  1915. */
  1916. if (v4l2_is_known_ioctl(cmd)) {
  1917. u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
  1918. if (flags & INFO_FL_CLEAR_MASK)
  1919. n = (flags & INFO_FL_CLEAR_MASK) >> 16;
  1920. }
  1921. if (copy_from_user(parg, (void __user *)arg, n))
  1922. goto out;
  1923. /* zero out anything we don't copy from userspace */
  1924. if (n < _IOC_SIZE(cmd))
  1925. memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
  1926. } else {
  1927. /* read-only ioctl */
  1928. memset(parg, 0, _IOC_SIZE(cmd));
  1929. }
  1930. }
  1931. err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
  1932. if (err < 0)
  1933. goto out;
  1934. has_array_args = err;
  1935. if (has_array_args) {
  1936. /*
  1937. * When adding new types of array args, make sure that the
  1938. * parent argument to ioctl (which contains the pointer to the
  1939. * array) fits into sbuf (so that mbuf will still remain
  1940. * unused up to here).
  1941. */
  1942. mbuf = kmalloc(array_size, GFP_KERNEL);
  1943. err = -ENOMEM;
  1944. if (NULL == mbuf)
  1945. goto out_array_args;
  1946. err = -EFAULT;
  1947. if (copy_from_user(mbuf, user_ptr, array_size))
  1948. goto out_array_args;
  1949. *kernel_ptr = mbuf;
  1950. }
  1951. /* Handles IOCTL */
  1952. err = func(file, cmd, parg);
  1953. if (err == -ENOIOCTLCMD)
  1954. err = -ENOTTY;
  1955. if (has_array_args) {
  1956. *kernel_ptr = user_ptr;
  1957. if (copy_to_user(user_ptr, mbuf, array_size))
  1958. err = -EFAULT;
  1959. goto out_array_args;
  1960. }
  1961. /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
  1962. results that must be returned. */
  1963. if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
  1964. goto out;
  1965. out_array_args:
  1966. /* Copy results into user buffer */
  1967. switch (_IOC_DIR(cmd)) {
  1968. case _IOC_READ:
  1969. case (_IOC_WRITE | _IOC_READ):
  1970. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  1971. err = -EFAULT;
  1972. break;
  1973. }
  1974. out:
  1975. kfree(mbuf);
  1976. return err;
  1977. }
  1978. EXPORT_SYMBOL(video_usercopy);
  1979. long video_ioctl2(struct file *file,
  1980. unsigned int cmd, unsigned long arg)
  1981. {
  1982. return video_usercopy(file, cmd, arg, __video_do_ioctl);
  1983. }
  1984. EXPORT_SYMBOL(video_ioctl2);