v4l2-ioctl.c 71 KB

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