v4l2-ioctl.c 78 KB

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