v4l2-ioctl.c 67 KB

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