v4l2-ioctl.c 71 KB

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