v4l2-ioctl.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880
  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-v4l2.h>
  27. #include <media/v4l2-mc.h>
  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. [0] = "0",
  130. [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
  131. [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
  132. [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
  133. [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
  134. [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
  135. [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
  136. [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
  137. [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
  138. [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
  139. [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
  140. [V4L2_BUF_TYPE_SDR_CAPTURE] = "sdr-cap",
  141. [V4L2_BUF_TYPE_SDR_OUTPUT] = "sdr-out",
  142. };
  143. EXPORT_SYMBOL(v4l2_type_names);
  144. static const char *v4l2_memory_names[] = {
  145. [V4L2_MEMORY_MMAP] = "mmap",
  146. [V4L2_MEMORY_USERPTR] = "userptr",
  147. [V4L2_MEMORY_OVERLAY] = "overlay",
  148. [V4L2_MEMORY_DMABUF] = "dmabuf",
  149. };
  150. #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
  151. /* ------------------------------------------------------------------ */
  152. /* debug help functions */
  153. static void v4l_print_querycap(const void *arg, bool write_only)
  154. {
  155. const struct v4l2_capability *p = arg;
  156. pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, "
  157. "capabilities=0x%08x, device_caps=0x%08x\n",
  158. (int)sizeof(p->driver), p->driver,
  159. (int)sizeof(p->card), p->card,
  160. (int)sizeof(p->bus_info), p->bus_info,
  161. p->version, p->capabilities, p->device_caps);
  162. }
  163. static void v4l_print_enuminput(const void *arg, bool write_only)
  164. {
  165. const struct v4l2_input *p = arg;
  166. pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, "
  167. "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
  168. p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
  169. p->tuner, (unsigned long long)p->std, p->status,
  170. p->capabilities);
  171. }
  172. static void v4l_print_enumoutput(const void *arg, bool write_only)
  173. {
  174. const struct v4l2_output *p = arg;
  175. pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, "
  176. "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
  177. p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
  178. p->modulator, (unsigned long long)p->std, p->capabilities);
  179. }
  180. static void v4l_print_audio(const void *arg, bool write_only)
  181. {
  182. const struct v4l2_audio *p = arg;
  183. if (write_only)
  184. pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
  185. else
  186. pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
  187. p->index, (int)sizeof(p->name), p->name,
  188. p->capability, p->mode);
  189. }
  190. static void v4l_print_audioout(const void *arg, bool write_only)
  191. {
  192. const struct v4l2_audioout *p = arg;
  193. if (write_only)
  194. pr_cont("index=%u\n", p->index);
  195. else
  196. pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
  197. p->index, (int)sizeof(p->name), p->name,
  198. p->capability, p->mode);
  199. }
  200. static void v4l_print_fmtdesc(const void *arg, bool write_only)
  201. {
  202. const struct v4l2_fmtdesc *p = arg;
  203. pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
  204. p->index, prt_names(p->type, v4l2_type_names),
  205. p->flags, (p->pixelformat & 0xff),
  206. (p->pixelformat >> 8) & 0xff,
  207. (p->pixelformat >> 16) & 0xff,
  208. (p->pixelformat >> 24) & 0xff,
  209. (int)sizeof(p->description), p->description);
  210. }
  211. static void v4l_print_format(const void *arg, bool write_only)
  212. {
  213. const struct v4l2_format *p = arg;
  214. const struct v4l2_pix_format *pix;
  215. const struct v4l2_pix_format_mplane *mp;
  216. const struct v4l2_vbi_format *vbi;
  217. const struct v4l2_sliced_vbi_format *sliced;
  218. const struct v4l2_window *win;
  219. const struct v4l2_sdr_format *sdr;
  220. unsigned i;
  221. pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
  222. switch (p->type) {
  223. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  224. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  225. pix = &p->fmt.pix;
  226. pr_cont(", width=%u, height=%u, "
  227. "pixelformat=%c%c%c%c, field=%s, "
  228. "bytesperline=%u, sizeimage=%u, colorspace=%d, "
  229. "flags=0x%x, ycbcr_enc=%u, quantization=%u, "
  230. "xfer_func=%u\n",
  231. pix->width, pix->height,
  232. (pix->pixelformat & 0xff),
  233. (pix->pixelformat >> 8) & 0xff,
  234. (pix->pixelformat >> 16) & 0xff,
  235. (pix->pixelformat >> 24) & 0xff,
  236. prt_names(pix->field, v4l2_field_names),
  237. pix->bytesperline, pix->sizeimage,
  238. pix->colorspace, pix->flags, pix->ycbcr_enc,
  239. pix->quantization, pix->xfer_func);
  240. break;
  241. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  242. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  243. mp = &p->fmt.pix_mp;
  244. pr_cont(", width=%u, height=%u, "
  245. "format=%c%c%c%c, field=%s, "
  246. "colorspace=%d, num_planes=%u, flags=0x%x, "
  247. "ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
  248. mp->width, mp->height,
  249. (mp->pixelformat & 0xff),
  250. (mp->pixelformat >> 8) & 0xff,
  251. (mp->pixelformat >> 16) & 0xff,
  252. (mp->pixelformat >> 24) & 0xff,
  253. prt_names(mp->field, v4l2_field_names),
  254. mp->colorspace, mp->num_planes, mp->flags,
  255. mp->ycbcr_enc, mp->quantization, mp->xfer_func);
  256. for (i = 0; i < mp->num_planes; i++)
  257. printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
  258. mp->plane_fmt[i].bytesperline,
  259. mp->plane_fmt[i].sizeimage);
  260. break;
  261. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  262. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  263. win = &p->fmt.win;
  264. /* Note: we can't print the clip list here since the clips
  265. * pointer is a userspace pointer, not a kernelspace
  266. * pointer. */
  267. 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",
  268. win->w.width, win->w.height, win->w.left, win->w.top,
  269. prt_names(win->field, v4l2_field_names),
  270. win->chromakey, win->clipcount, win->clips,
  271. win->bitmap, win->global_alpha);
  272. break;
  273. case V4L2_BUF_TYPE_VBI_CAPTURE:
  274. case V4L2_BUF_TYPE_VBI_OUTPUT:
  275. vbi = &p->fmt.vbi;
  276. pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
  277. "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
  278. vbi->sampling_rate, vbi->offset,
  279. vbi->samples_per_line,
  280. (vbi->sample_format & 0xff),
  281. (vbi->sample_format >> 8) & 0xff,
  282. (vbi->sample_format >> 16) & 0xff,
  283. (vbi->sample_format >> 24) & 0xff,
  284. vbi->start[0], vbi->start[1],
  285. vbi->count[0], vbi->count[1]);
  286. break;
  287. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  288. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  289. sliced = &p->fmt.sliced;
  290. pr_cont(", service_set=0x%08x, io_size=%d\n",
  291. sliced->service_set, sliced->io_size);
  292. for (i = 0; i < 24; i++)
  293. printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
  294. sliced->service_lines[0][i],
  295. sliced->service_lines[1][i]);
  296. break;
  297. case V4L2_BUF_TYPE_SDR_CAPTURE:
  298. case V4L2_BUF_TYPE_SDR_OUTPUT:
  299. sdr = &p->fmt.sdr;
  300. pr_cont(", pixelformat=%c%c%c%c\n",
  301. (sdr->pixelformat >> 0) & 0xff,
  302. (sdr->pixelformat >> 8) & 0xff,
  303. (sdr->pixelformat >> 16) & 0xff,
  304. (sdr->pixelformat >> 24) & 0xff);
  305. break;
  306. }
  307. }
  308. static void v4l_print_framebuffer(const void *arg, bool write_only)
  309. {
  310. const struct v4l2_framebuffer *p = arg;
  311. pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
  312. "height=%u, pixelformat=%c%c%c%c, "
  313. "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
  314. p->capability, p->flags, p->base,
  315. p->fmt.width, p->fmt.height,
  316. (p->fmt.pixelformat & 0xff),
  317. (p->fmt.pixelformat >> 8) & 0xff,
  318. (p->fmt.pixelformat >> 16) & 0xff,
  319. (p->fmt.pixelformat >> 24) & 0xff,
  320. p->fmt.bytesperline, p->fmt.sizeimage,
  321. p->fmt.colorspace);
  322. }
  323. static void v4l_print_buftype(const void *arg, bool write_only)
  324. {
  325. pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
  326. }
  327. static void v4l_print_modulator(const void *arg, bool write_only)
  328. {
  329. const struct v4l2_modulator *p = arg;
  330. if (write_only)
  331. pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
  332. else
  333. pr_cont("index=%u, name=%.*s, capability=0x%x, "
  334. "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
  335. p->index, (int)sizeof(p->name), p->name, p->capability,
  336. p->rangelow, p->rangehigh, p->txsubchans);
  337. }
  338. static void v4l_print_tuner(const void *arg, bool write_only)
  339. {
  340. const struct v4l2_tuner *p = arg;
  341. if (write_only)
  342. pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
  343. else
  344. pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, "
  345. "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
  346. "rxsubchans=0x%x, audmode=%u\n",
  347. p->index, (int)sizeof(p->name), p->name, p->type,
  348. p->capability, p->rangelow,
  349. p->rangehigh, p->signal, p->afc,
  350. p->rxsubchans, p->audmode);
  351. }
  352. static void v4l_print_frequency(const void *arg, bool write_only)
  353. {
  354. const struct v4l2_frequency *p = arg;
  355. pr_cont("tuner=%u, type=%u, frequency=%u\n",
  356. p->tuner, p->type, p->frequency);
  357. }
  358. static void v4l_print_standard(const void *arg, bool write_only)
  359. {
  360. const struct v4l2_standard *p = arg;
  361. pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, "
  362. "framelines=%u\n", p->index,
  363. (unsigned long long)p->id, (int)sizeof(p->name), p->name,
  364. p->frameperiod.numerator,
  365. p->frameperiod.denominator,
  366. p->framelines);
  367. }
  368. static void v4l_print_std(const void *arg, bool write_only)
  369. {
  370. pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
  371. }
  372. static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
  373. {
  374. const struct v4l2_hw_freq_seek *p = arg;
  375. pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
  376. "rangelow=%u, rangehigh=%u\n",
  377. p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
  378. p->rangelow, p->rangehigh);
  379. }
  380. static void v4l_print_requestbuffers(const void *arg, bool write_only)
  381. {
  382. const struct v4l2_requestbuffers *p = arg;
  383. pr_cont("count=%d, type=%s, memory=%s\n",
  384. p->count,
  385. prt_names(p->type, v4l2_type_names),
  386. prt_names(p->memory, v4l2_memory_names));
  387. }
  388. static void v4l_print_buffer(const void *arg, bool write_only)
  389. {
  390. const struct v4l2_buffer *p = arg;
  391. const struct v4l2_timecode *tc = &p->timecode;
  392. const struct v4l2_plane *plane;
  393. int i;
  394. pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
  395. "flags=0x%08x, field=%s, sequence=%d, memory=%s",
  396. p->timestamp.tv_sec / 3600,
  397. (int)(p->timestamp.tv_sec / 60) % 60,
  398. (int)(p->timestamp.tv_sec % 60),
  399. (long)p->timestamp.tv_usec,
  400. p->index,
  401. prt_names(p->type, v4l2_type_names),
  402. p->flags, prt_names(p->field, v4l2_field_names),
  403. p->sequence, prt_names(p->memory, v4l2_memory_names));
  404. if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
  405. pr_cont("\n");
  406. for (i = 0; i < p->length; ++i) {
  407. plane = &p->m.planes[i];
  408. printk(KERN_DEBUG
  409. "plane %d: bytesused=%d, data_offset=0x%08x, "
  410. "offset/userptr=0x%lx, length=%d\n",
  411. i, plane->bytesused, plane->data_offset,
  412. plane->m.userptr, plane->length);
  413. }
  414. } else {
  415. pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
  416. p->bytesused, p->m.userptr, p->length);
  417. }
  418. printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
  419. "flags=0x%08x, frames=%d, userbits=0x%08x\n",
  420. tc->hours, tc->minutes, tc->seconds,
  421. tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
  422. }
  423. static void v4l_print_exportbuffer(const void *arg, bool write_only)
  424. {
  425. const struct v4l2_exportbuffer *p = arg;
  426. pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
  427. p->fd, prt_names(p->type, v4l2_type_names),
  428. p->index, p->plane, p->flags);
  429. }
  430. static void v4l_print_create_buffers(const void *arg, bool write_only)
  431. {
  432. const struct v4l2_create_buffers *p = arg;
  433. pr_cont("index=%d, count=%d, memory=%s, ",
  434. p->index, p->count,
  435. prt_names(p->memory, v4l2_memory_names));
  436. v4l_print_format(&p->format, write_only);
  437. }
  438. static void v4l_print_streamparm(const void *arg, bool write_only)
  439. {
  440. const struct v4l2_streamparm *p = arg;
  441. pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
  442. if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  443. p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  444. const struct v4l2_captureparm *c = &p->parm.capture;
  445. pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
  446. "extendedmode=%d, readbuffers=%d\n",
  447. c->capability, c->capturemode,
  448. c->timeperframe.numerator, c->timeperframe.denominator,
  449. c->extendedmode, c->readbuffers);
  450. } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  451. p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  452. const struct v4l2_outputparm *c = &p->parm.output;
  453. pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
  454. "extendedmode=%d, writebuffers=%d\n",
  455. c->capability, c->outputmode,
  456. c->timeperframe.numerator, c->timeperframe.denominator,
  457. c->extendedmode, c->writebuffers);
  458. } else {
  459. pr_cont("\n");
  460. }
  461. }
  462. static void v4l_print_queryctrl(const void *arg, bool write_only)
  463. {
  464. const struct v4l2_queryctrl *p = arg;
  465. pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
  466. "step=%d, default=%d, flags=0x%08x\n",
  467. p->id, p->type, (int)sizeof(p->name), p->name,
  468. p->minimum, p->maximum,
  469. p->step, p->default_value, p->flags);
  470. }
  471. static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
  472. {
  473. const struct v4l2_query_ext_ctrl *p = arg;
  474. pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, "
  475. "step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, "
  476. "nr_of_dims=%u, dims=%u,%u,%u,%u\n",
  477. p->id, p->type, (int)sizeof(p->name), p->name,
  478. p->minimum, p->maximum,
  479. p->step, p->default_value, p->flags,
  480. p->elem_size, p->elems, p->nr_of_dims,
  481. p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
  482. }
  483. static void v4l_print_querymenu(const void *arg, bool write_only)
  484. {
  485. const struct v4l2_querymenu *p = arg;
  486. pr_cont("id=0x%x, index=%d\n", p->id, p->index);
  487. }
  488. static void v4l_print_control(const void *arg, bool write_only)
  489. {
  490. const struct v4l2_control *p = arg;
  491. pr_cont("id=0x%x, value=%d\n", p->id, p->value);
  492. }
  493. static void v4l_print_ext_controls(const void *arg, bool write_only)
  494. {
  495. const struct v4l2_ext_controls *p = arg;
  496. int i;
  497. pr_cont("which=0x%x, count=%d, error_idx=%d",
  498. p->which, p->count, p->error_idx);
  499. for (i = 0; i < p->count; i++) {
  500. if (!p->controls[i].size)
  501. pr_cont(", id/val=0x%x/0x%x",
  502. p->controls[i].id, p->controls[i].value);
  503. else
  504. pr_cont(", id/size=0x%x/%u",
  505. p->controls[i].id, p->controls[i].size);
  506. }
  507. pr_cont("\n");
  508. }
  509. static void v4l_print_cropcap(const void *arg, bool write_only)
  510. {
  511. const struct v4l2_cropcap *p = arg;
  512. pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
  513. "defrect wxh=%dx%d, x,y=%d,%d, "
  514. "pixelaspect %d/%d\n",
  515. prt_names(p->type, v4l2_type_names),
  516. p->bounds.width, p->bounds.height,
  517. p->bounds.left, p->bounds.top,
  518. p->defrect.width, p->defrect.height,
  519. p->defrect.left, p->defrect.top,
  520. p->pixelaspect.numerator, p->pixelaspect.denominator);
  521. }
  522. static void v4l_print_crop(const void *arg, bool write_only)
  523. {
  524. const struct v4l2_crop *p = arg;
  525. pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
  526. prt_names(p->type, v4l2_type_names),
  527. p->c.width, p->c.height,
  528. p->c.left, p->c.top);
  529. }
  530. static void v4l_print_selection(const void *arg, bool write_only)
  531. {
  532. const struct v4l2_selection *p = arg;
  533. pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
  534. prt_names(p->type, v4l2_type_names),
  535. p->target, p->flags,
  536. p->r.width, p->r.height, p->r.left, p->r.top);
  537. }
  538. static void v4l_print_jpegcompression(const void *arg, bool write_only)
  539. {
  540. const struct v4l2_jpegcompression *p = arg;
  541. pr_cont("quality=%d, APPn=%d, APP_len=%d, "
  542. "COM_len=%d, jpeg_markers=0x%x\n",
  543. p->quality, p->APPn, p->APP_len,
  544. p->COM_len, p->jpeg_markers);
  545. }
  546. static void v4l_print_enc_idx(const void *arg, bool write_only)
  547. {
  548. const struct v4l2_enc_idx *p = arg;
  549. pr_cont("entries=%d, entries_cap=%d\n",
  550. p->entries, p->entries_cap);
  551. }
  552. static void v4l_print_encoder_cmd(const void *arg, bool write_only)
  553. {
  554. const struct v4l2_encoder_cmd *p = arg;
  555. pr_cont("cmd=%d, flags=0x%x\n",
  556. p->cmd, p->flags);
  557. }
  558. static void v4l_print_decoder_cmd(const void *arg, bool write_only)
  559. {
  560. const struct v4l2_decoder_cmd *p = arg;
  561. pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
  562. if (p->cmd == V4L2_DEC_CMD_START)
  563. pr_info("speed=%d, format=%u\n",
  564. p->start.speed, p->start.format);
  565. else if (p->cmd == V4L2_DEC_CMD_STOP)
  566. pr_info("pts=%llu\n", p->stop.pts);
  567. }
  568. static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
  569. {
  570. const struct v4l2_dbg_chip_info *p = arg;
  571. pr_cont("type=%u, ", p->match.type);
  572. if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
  573. pr_cont("name=%.*s, ",
  574. (int)sizeof(p->match.name), p->match.name);
  575. else
  576. pr_cont("addr=%u, ", p->match.addr);
  577. pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
  578. }
  579. static void v4l_print_dbg_register(const void *arg, bool write_only)
  580. {
  581. const struct v4l2_dbg_register *p = arg;
  582. pr_cont("type=%u, ", p->match.type);
  583. if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
  584. pr_cont("name=%.*s, ",
  585. (int)sizeof(p->match.name), p->match.name);
  586. else
  587. pr_cont("addr=%u, ", p->match.addr);
  588. pr_cont("reg=0x%llx, val=0x%llx\n",
  589. p->reg, p->val);
  590. }
  591. static void v4l_print_dv_timings(const void *arg, bool write_only)
  592. {
  593. const struct v4l2_dv_timings *p = arg;
  594. switch (p->type) {
  595. case V4L2_DV_BT_656_1120:
  596. pr_cont("type=bt-656/1120, interlaced=%u, "
  597. "pixelclock=%llu, "
  598. "width=%u, height=%u, polarities=0x%x, "
  599. "hfrontporch=%u, hsync=%u, "
  600. "hbackporch=%u, vfrontporch=%u, "
  601. "vsync=%u, vbackporch=%u, "
  602. "il_vfrontporch=%u, il_vsync=%u, "
  603. "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
  604. p->bt.interlaced, p->bt.pixelclock,
  605. p->bt.width, p->bt.height,
  606. p->bt.polarities, p->bt.hfrontporch,
  607. p->bt.hsync, p->bt.hbackporch,
  608. p->bt.vfrontporch, p->bt.vsync,
  609. p->bt.vbackporch, p->bt.il_vfrontporch,
  610. p->bt.il_vsync, p->bt.il_vbackporch,
  611. p->bt.standards, p->bt.flags);
  612. break;
  613. default:
  614. pr_cont("type=%d\n", p->type);
  615. break;
  616. }
  617. }
  618. static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
  619. {
  620. const struct v4l2_enum_dv_timings *p = arg;
  621. pr_cont("index=%u, ", p->index);
  622. v4l_print_dv_timings(&p->timings, write_only);
  623. }
  624. static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
  625. {
  626. const struct v4l2_dv_timings_cap *p = arg;
  627. switch (p->type) {
  628. case V4L2_DV_BT_656_1120:
  629. pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
  630. "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
  631. p->bt.min_width, p->bt.max_width,
  632. p->bt.min_height, p->bt.max_height,
  633. p->bt.min_pixelclock, p->bt.max_pixelclock,
  634. p->bt.standards, p->bt.capabilities);
  635. break;
  636. default:
  637. pr_cont("type=%u\n", p->type);
  638. break;
  639. }
  640. }
  641. static void v4l_print_frmsizeenum(const void *arg, bool write_only)
  642. {
  643. const struct v4l2_frmsizeenum *p = arg;
  644. pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
  645. p->index,
  646. (p->pixel_format & 0xff),
  647. (p->pixel_format >> 8) & 0xff,
  648. (p->pixel_format >> 16) & 0xff,
  649. (p->pixel_format >> 24) & 0xff,
  650. p->type);
  651. switch (p->type) {
  652. case V4L2_FRMSIZE_TYPE_DISCRETE:
  653. pr_cont(", wxh=%ux%u\n",
  654. p->discrete.width, p->discrete.height);
  655. break;
  656. case V4L2_FRMSIZE_TYPE_STEPWISE:
  657. pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
  658. p->stepwise.min_width, p->stepwise.min_height,
  659. p->stepwise.step_width, p->stepwise.step_height,
  660. p->stepwise.max_width, p->stepwise.max_height);
  661. break;
  662. case V4L2_FRMSIZE_TYPE_CONTINUOUS:
  663. /* fall through */
  664. default:
  665. pr_cont("\n");
  666. break;
  667. }
  668. }
  669. static void v4l_print_frmivalenum(const void *arg, bool write_only)
  670. {
  671. const struct v4l2_frmivalenum *p = arg;
  672. pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
  673. p->index,
  674. (p->pixel_format & 0xff),
  675. (p->pixel_format >> 8) & 0xff,
  676. (p->pixel_format >> 16) & 0xff,
  677. (p->pixel_format >> 24) & 0xff,
  678. p->width, p->height, p->type);
  679. switch (p->type) {
  680. case V4L2_FRMIVAL_TYPE_DISCRETE:
  681. pr_cont(", fps=%d/%d\n",
  682. p->discrete.numerator,
  683. p->discrete.denominator);
  684. break;
  685. case V4L2_FRMIVAL_TYPE_STEPWISE:
  686. pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
  687. p->stepwise.min.numerator,
  688. p->stepwise.min.denominator,
  689. p->stepwise.max.numerator,
  690. p->stepwise.max.denominator,
  691. p->stepwise.step.numerator,
  692. p->stepwise.step.denominator);
  693. break;
  694. case V4L2_FRMIVAL_TYPE_CONTINUOUS:
  695. /* fall through */
  696. default:
  697. pr_cont("\n");
  698. break;
  699. }
  700. }
  701. static void v4l_print_event(const void *arg, bool write_only)
  702. {
  703. const struct v4l2_event *p = arg;
  704. const struct v4l2_event_ctrl *c;
  705. pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
  706. "timestamp=%lu.%9.9lu\n",
  707. p->type, p->pending, p->sequence, p->id,
  708. p->timestamp.tv_sec, p->timestamp.tv_nsec);
  709. switch (p->type) {
  710. case V4L2_EVENT_VSYNC:
  711. printk(KERN_DEBUG "field=%s\n",
  712. prt_names(p->u.vsync.field, v4l2_field_names));
  713. break;
  714. case V4L2_EVENT_CTRL:
  715. c = &p->u.ctrl;
  716. printk(KERN_DEBUG "changes=0x%x, type=%u, ",
  717. c->changes, c->type);
  718. if (c->type == V4L2_CTRL_TYPE_INTEGER64)
  719. pr_cont("value64=%lld, ", c->value64);
  720. else
  721. pr_cont("value=%d, ", c->value);
  722. pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, "
  723. "default_value=%d\n",
  724. c->flags, c->minimum, c->maximum,
  725. c->step, c->default_value);
  726. break;
  727. case V4L2_EVENT_FRAME_SYNC:
  728. pr_cont("frame_sequence=%u\n",
  729. p->u.frame_sync.frame_sequence);
  730. break;
  731. }
  732. }
  733. static void v4l_print_event_subscription(const void *arg, bool write_only)
  734. {
  735. const struct v4l2_event_subscription *p = arg;
  736. pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
  737. p->type, p->id, p->flags);
  738. }
  739. static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
  740. {
  741. const struct v4l2_sliced_vbi_cap *p = arg;
  742. int i;
  743. pr_cont("type=%s, service_set=0x%08x\n",
  744. prt_names(p->type, v4l2_type_names), p->service_set);
  745. for (i = 0; i < 24; i++)
  746. printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
  747. p->service_lines[0][i],
  748. p->service_lines[1][i]);
  749. }
  750. static void v4l_print_freq_band(const void *arg, bool write_only)
  751. {
  752. const struct v4l2_frequency_band *p = arg;
  753. pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
  754. "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
  755. p->tuner, p->type, p->index,
  756. p->capability, p->rangelow,
  757. p->rangehigh, p->modulation);
  758. }
  759. static void v4l_print_edid(const void *arg, bool write_only)
  760. {
  761. const struct v4l2_edid *p = arg;
  762. pr_cont("pad=%u, start_block=%u, blocks=%u\n",
  763. p->pad, p->start_block, p->blocks);
  764. }
  765. static void v4l_print_u32(const void *arg, bool write_only)
  766. {
  767. pr_cont("value=%u\n", *(const u32 *)arg);
  768. }
  769. static void v4l_print_newline(const void *arg, bool write_only)
  770. {
  771. pr_cont("\n");
  772. }
  773. static void v4l_print_default(const void *arg, bool write_only)
  774. {
  775. pr_cont("driver-specific ioctl\n");
  776. }
  777. static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
  778. {
  779. __u32 i;
  780. /* zero the reserved fields */
  781. c->reserved[0] = c->reserved[1] = 0;
  782. for (i = 0; i < c->count; i++)
  783. c->controls[i].reserved2[0] = 0;
  784. /* V4L2_CID_PRIVATE_BASE cannot be used as control class
  785. when using extended controls.
  786. Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
  787. is it allowed for backwards compatibility.
  788. */
  789. if (!allow_priv && c->which == V4L2_CID_PRIVATE_BASE)
  790. return 0;
  791. if (!c->which)
  792. return 1;
  793. /* Check that all controls are from the same control class. */
  794. for (i = 0; i < c->count; i++) {
  795. if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
  796. c->error_idx = i;
  797. return 0;
  798. }
  799. }
  800. return 1;
  801. }
  802. static int check_fmt(struct file *file, enum v4l2_buf_type type)
  803. {
  804. struct video_device *vfd = video_devdata(file);
  805. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  806. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  807. bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
  808. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  809. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  810. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  811. if (ops == NULL)
  812. return -EINVAL;
  813. switch (type) {
  814. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  815. if (is_vid && is_rx &&
  816. (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
  817. return 0;
  818. break;
  819. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  820. if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
  821. return 0;
  822. break;
  823. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  824. if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
  825. return 0;
  826. break;
  827. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  828. if (is_vid && is_tx &&
  829. (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
  830. return 0;
  831. break;
  832. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  833. if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
  834. return 0;
  835. break;
  836. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  837. if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
  838. return 0;
  839. break;
  840. case V4L2_BUF_TYPE_VBI_CAPTURE:
  841. if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
  842. return 0;
  843. break;
  844. case V4L2_BUF_TYPE_VBI_OUTPUT:
  845. if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
  846. return 0;
  847. break;
  848. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  849. if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
  850. return 0;
  851. break;
  852. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  853. if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
  854. return 0;
  855. break;
  856. case V4L2_BUF_TYPE_SDR_CAPTURE:
  857. if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
  858. return 0;
  859. break;
  860. case V4L2_BUF_TYPE_SDR_OUTPUT:
  861. if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
  862. return 0;
  863. break;
  864. default:
  865. break;
  866. }
  867. return -EINVAL;
  868. }
  869. static void v4l_sanitize_format(struct v4l2_format *fmt)
  870. {
  871. unsigned int offset;
  872. /*
  873. * The v4l2_pix_format structure has been extended with fields that were
  874. * not previously required to be set to zero by applications. The priv
  875. * field, when set to a magic value, indicates the the extended fields
  876. * are valid. Otherwise they will contain undefined values. To simplify
  877. * the API towards drivers zero the extended fields and set the priv
  878. * field to the magic value when the extended pixel format structure
  879. * isn't used by applications.
  880. */
  881. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  882. fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  883. return;
  884. if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
  885. return;
  886. fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  887. offset = offsetof(struct v4l2_pix_format, priv)
  888. + sizeof(fmt->fmt.pix.priv);
  889. memset(((void *)&fmt->fmt.pix) + offset, 0,
  890. sizeof(fmt->fmt.pix) - offset);
  891. }
  892. static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
  893. struct file *file, void *fh, void *arg)
  894. {
  895. struct v4l2_capability *cap = (struct v4l2_capability *)arg;
  896. struct video_device *vfd = video_devdata(file);
  897. int ret;
  898. cap->version = LINUX_VERSION_CODE;
  899. cap->device_caps = vfd->device_caps;
  900. cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
  901. ret = ops->vidioc_querycap(file, fh, cap);
  902. cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
  903. /*
  904. * Drivers MUST fill in device_caps, so check for this and
  905. * warn if it was forgotten.
  906. */
  907. WARN(!(cap->capabilities & V4L2_CAP_DEVICE_CAPS) ||
  908. !cap->device_caps, "Bad caps for driver %s, %x %x",
  909. cap->driver, cap->capabilities, cap->device_caps);
  910. cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
  911. return ret;
  912. }
  913. static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
  914. struct file *file, void *fh, void *arg)
  915. {
  916. struct video_device *vfd = video_devdata(file);
  917. int ret;
  918. ret = v4l_enable_media_source(vfd);
  919. if (ret)
  920. return ret;
  921. return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
  922. }
  923. static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
  924. struct file *file, void *fh, void *arg)
  925. {
  926. return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
  927. }
  928. static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
  929. struct file *file, void *fh, void *arg)
  930. {
  931. struct video_device *vfd;
  932. u32 *p = arg;
  933. vfd = video_devdata(file);
  934. *p = v4l2_prio_max(vfd->prio);
  935. return 0;
  936. }
  937. static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
  938. struct file *file, void *fh, void *arg)
  939. {
  940. struct video_device *vfd;
  941. struct v4l2_fh *vfh;
  942. u32 *p = arg;
  943. vfd = video_devdata(file);
  944. if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
  945. return -ENOTTY;
  946. vfh = file->private_data;
  947. return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
  948. }
  949. static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
  950. struct file *file, void *fh, void *arg)
  951. {
  952. struct video_device *vfd = video_devdata(file);
  953. struct v4l2_input *p = arg;
  954. /*
  955. * We set the flags for CAP_DV_TIMINGS &
  956. * CAP_STD here based on ioctl handler provided by the
  957. * driver. If the driver doesn't support these
  958. * for a specific input, it must override these flags.
  959. */
  960. if (is_valid_ioctl(vfd, VIDIOC_S_STD))
  961. p->capabilities |= V4L2_IN_CAP_STD;
  962. return ops->vidioc_enum_input(file, fh, p);
  963. }
  964. static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
  965. struct file *file, void *fh, void *arg)
  966. {
  967. struct video_device *vfd = video_devdata(file);
  968. struct v4l2_output *p = arg;
  969. /*
  970. * We set the flags for CAP_DV_TIMINGS &
  971. * CAP_STD here based on ioctl handler provided by the
  972. * driver. If the driver doesn't support these
  973. * for a specific output, it must override these flags.
  974. */
  975. if (is_valid_ioctl(vfd, VIDIOC_S_STD))
  976. p->capabilities |= V4L2_OUT_CAP_STD;
  977. return ops->vidioc_enum_output(file, fh, p);
  978. }
  979. static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
  980. {
  981. const unsigned sz = sizeof(fmt->description);
  982. const char *descr = NULL;
  983. u32 flags = 0;
  984. /*
  985. * We depart from the normal coding style here since the descriptions
  986. * should be aligned so it is easy to see which descriptions will be
  987. * longer than 31 characters (the max length for a description).
  988. * And frankly, this is easier to read anyway.
  989. *
  990. * Note that gcc will use O(log N) comparisons to find the right case.
  991. */
  992. switch (fmt->pixelformat) {
  993. /* Max description length mask: descr = "0123456789012345678901234567890" */
  994. case V4L2_PIX_FMT_RGB332: descr = "8-bit RGB 3-3-2"; break;
  995. case V4L2_PIX_FMT_RGB444: descr = "16-bit A/XRGB 4-4-4-4"; break;
  996. case V4L2_PIX_FMT_ARGB444: descr = "16-bit ARGB 4-4-4-4"; break;
  997. case V4L2_PIX_FMT_XRGB444: descr = "16-bit XRGB 4-4-4-4"; break;
  998. case V4L2_PIX_FMT_RGB555: descr = "16-bit A/XRGB 1-5-5-5"; break;
  999. case V4L2_PIX_FMT_ARGB555: descr = "16-bit ARGB 1-5-5-5"; break;
  1000. case V4L2_PIX_FMT_XRGB555: descr = "16-bit XRGB 1-5-5-5"; break;
  1001. case V4L2_PIX_FMT_RGB565: descr = "16-bit RGB 5-6-5"; break;
  1002. case V4L2_PIX_FMT_RGB555X: descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
  1003. case V4L2_PIX_FMT_ARGB555X: descr = "16-bit ARGB 1-5-5-5 BE"; break;
  1004. case V4L2_PIX_FMT_XRGB555X: descr = "16-bit XRGB 1-5-5-5 BE"; break;
  1005. case V4L2_PIX_FMT_RGB565X: descr = "16-bit RGB 5-6-5 BE"; break;
  1006. case V4L2_PIX_FMT_BGR666: descr = "18-bit BGRX 6-6-6-14"; break;
  1007. case V4L2_PIX_FMT_BGR24: descr = "24-bit BGR 8-8-8"; break;
  1008. case V4L2_PIX_FMT_RGB24: descr = "24-bit RGB 8-8-8"; break;
  1009. case V4L2_PIX_FMT_BGR32: descr = "32-bit BGRA/X 8-8-8-8"; break;
  1010. case V4L2_PIX_FMT_ABGR32: descr = "32-bit BGRA 8-8-8-8"; break;
  1011. case V4L2_PIX_FMT_XBGR32: descr = "32-bit BGRX 8-8-8-8"; break;
  1012. case V4L2_PIX_FMT_RGB32: descr = "32-bit A/XRGB 8-8-8-8"; break;
  1013. case V4L2_PIX_FMT_ARGB32: descr = "32-bit ARGB 8-8-8-8"; break;
  1014. case V4L2_PIX_FMT_XRGB32: descr = "32-bit XRGB 8-8-8-8"; break;
  1015. case V4L2_PIX_FMT_GREY: descr = "8-bit Greyscale"; break;
  1016. case V4L2_PIX_FMT_Y4: descr = "4-bit Greyscale"; break;
  1017. case V4L2_PIX_FMT_Y6: descr = "6-bit Greyscale"; break;
  1018. case V4L2_PIX_FMT_Y10: descr = "10-bit Greyscale"; break;
  1019. case V4L2_PIX_FMT_Y12: descr = "12-bit Greyscale"; break;
  1020. case V4L2_PIX_FMT_Y16: descr = "16-bit Greyscale"; break;
  1021. case V4L2_PIX_FMT_Y16_BE: descr = "16-bit Greyscale BE"; break;
  1022. case V4L2_PIX_FMT_Y10BPACK: descr = "10-bit Greyscale (Packed)"; break;
  1023. case V4L2_PIX_FMT_PAL8: descr = "8-bit Palette"; break;
  1024. case V4L2_PIX_FMT_UV8: descr = "8-bit Chrominance UV 4-4"; break;
  1025. case V4L2_PIX_FMT_YVU410: descr = "Planar YVU 4:1:0"; break;
  1026. case V4L2_PIX_FMT_YVU420: descr = "Planar YVU 4:2:0"; break;
  1027. case V4L2_PIX_FMT_YUYV: descr = "YUYV 4:2:2"; break;
  1028. case V4L2_PIX_FMT_YYUV: descr = "YYUV 4:2:2"; break;
  1029. case V4L2_PIX_FMT_YVYU: descr = "YVYU 4:2:2"; break;
  1030. case V4L2_PIX_FMT_UYVY: descr = "UYVY 4:2:2"; break;
  1031. case V4L2_PIX_FMT_VYUY: descr = "VYUY 4:2:2"; break;
  1032. case V4L2_PIX_FMT_YUV422P: descr = "Planar YUV 4:2:2"; break;
  1033. case V4L2_PIX_FMT_YUV411P: descr = "Planar YUV 4:1:1"; break;
  1034. case V4L2_PIX_FMT_Y41P: descr = "YUV 4:1:1 (Packed)"; break;
  1035. case V4L2_PIX_FMT_YUV444: descr = "16-bit A/XYUV 4-4-4-4"; break;
  1036. case V4L2_PIX_FMT_YUV555: descr = "16-bit A/XYUV 1-5-5-5"; break;
  1037. case V4L2_PIX_FMT_YUV565: descr = "16-bit YUV 5-6-5"; break;
  1038. case V4L2_PIX_FMT_YUV32: descr = "32-bit A/XYUV 8-8-8-8"; break;
  1039. case V4L2_PIX_FMT_YUV410: descr = "Planar YUV 4:1:0"; break;
  1040. case V4L2_PIX_FMT_YUV420: descr = "Planar YUV 4:2:0"; break;
  1041. case V4L2_PIX_FMT_HI240: descr = "8-bit Dithered RGB (BTTV)"; break;
  1042. case V4L2_PIX_FMT_HM12: descr = "YUV 4:2:0 (16x16 Macroblocks)"; break;
  1043. case V4L2_PIX_FMT_M420: descr = "YUV 4:2:0 (M420)"; break;
  1044. case V4L2_PIX_FMT_NV12: descr = "Y/CbCr 4:2:0"; break;
  1045. case V4L2_PIX_FMT_NV21: descr = "Y/CrCb 4:2:0"; break;
  1046. case V4L2_PIX_FMT_NV16: descr = "Y/CbCr 4:2:2"; break;
  1047. case V4L2_PIX_FMT_NV61: descr = "Y/CrCb 4:2:2"; break;
  1048. case V4L2_PIX_FMT_NV24: descr = "Y/CbCr 4:4:4"; break;
  1049. case V4L2_PIX_FMT_NV42: descr = "Y/CrCb 4:4:4"; break;
  1050. case V4L2_PIX_FMT_NV12M: descr = "Y/CbCr 4:2:0 (N-C)"; break;
  1051. case V4L2_PIX_FMT_NV21M: descr = "Y/CrCb 4:2:0 (N-C)"; break;
  1052. case V4L2_PIX_FMT_NV16M: descr = "Y/CbCr 4:2:2 (N-C)"; break;
  1053. case V4L2_PIX_FMT_NV61M: descr = "Y/CrCb 4:2:2 (N-C)"; break;
  1054. case V4L2_PIX_FMT_NV12MT: descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
  1055. case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
  1056. case V4L2_PIX_FMT_YUV420M: descr = "Planar YUV 4:2:0 (N-C)"; break;
  1057. case V4L2_PIX_FMT_YVU420M: descr = "Planar YVU 4:2:0 (N-C)"; break;
  1058. case V4L2_PIX_FMT_YUV422M: descr = "Planar YUV 4:2:2 (N-C)"; break;
  1059. case V4L2_PIX_FMT_YVU422M: descr = "Planar YVU 4:2:2 (N-C)"; break;
  1060. case V4L2_PIX_FMT_YUV444M: descr = "Planar YUV 4:4:4 (N-C)"; break;
  1061. case V4L2_PIX_FMT_YVU444M: descr = "Planar YVU 4:4:4 (N-C)"; break;
  1062. case V4L2_PIX_FMT_SBGGR8: descr = "8-bit Bayer BGBG/GRGR"; break;
  1063. case V4L2_PIX_FMT_SGBRG8: descr = "8-bit Bayer GBGB/RGRG"; break;
  1064. case V4L2_PIX_FMT_SGRBG8: descr = "8-bit Bayer GRGR/BGBG"; break;
  1065. case V4L2_PIX_FMT_SRGGB8: descr = "8-bit Bayer RGRG/GBGB"; break;
  1066. case V4L2_PIX_FMT_SBGGR10: descr = "10-bit Bayer BGBG/GRGR"; break;
  1067. case V4L2_PIX_FMT_SGBRG10: descr = "10-bit Bayer GBGB/RGRG"; break;
  1068. case V4L2_PIX_FMT_SGRBG10: descr = "10-bit Bayer GRGR/BGBG"; break;
  1069. case V4L2_PIX_FMT_SRGGB10: descr = "10-bit Bayer RGRG/GBGB"; break;
  1070. case V4L2_PIX_FMT_SBGGR12: descr = "12-bit Bayer BGBG/GRGR"; break;
  1071. case V4L2_PIX_FMT_SGBRG12: descr = "12-bit Bayer GBGB/RGRG"; break;
  1072. case V4L2_PIX_FMT_SGRBG12: descr = "12-bit Bayer GRGR/BGBG"; break;
  1073. case V4L2_PIX_FMT_SRGGB12: descr = "12-bit Bayer RGRG/GBGB"; break;
  1074. case V4L2_PIX_FMT_SBGGR10P: descr = "10-bit Bayer BGBG/GRGR Packed"; break;
  1075. case V4L2_PIX_FMT_SGBRG10P: descr = "10-bit Bayer GBGB/RGRG Packed"; break;
  1076. case V4L2_PIX_FMT_SGRBG10P: descr = "10-bit Bayer GRGR/BGBG Packed"; break;
  1077. case V4L2_PIX_FMT_SRGGB10P: descr = "10-bit Bayer RGRG/GBGB Packed"; break;
  1078. case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
  1079. case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
  1080. case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
  1081. case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
  1082. case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
  1083. case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
  1084. case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
  1085. case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
  1086. case V4L2_PIX_FMT_SBGGR16: descr = "16-bit Bayer BGBG/GRGR (Exp.)"; break;
  1087. case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
  1088. case V4L2_PIX_FMT_SPCA501: descr = "GSPCA SPCA501"; break;
  1089. case V4L2_PIX_FMT_SPCA505: descr = "GSPCA SPCA505"; break;
  1090. case V4L2_PIX_FMT_SPCA508: descr = "GSPCA SPCA508"; break;
  1091. case V4L2_PIX_FMT_STV0680: descr = "GSPCA STV0680"; break;
  1092. case V4L2_PIX_FMT_TM6000: descr = "A/V + VBI Mux Packet"; break;
  1093. case V4L2_PIX_FMT_CIT_YYVYUY: descr = "GSPCA CIT YYVYUY"; break;
  1094. case V4L2_PIX_FMT_KONICA420: descr = "GSPCA KONICA420"; break;
  1095. case V4L2_SDR_FMT_CU8: descr = "Complex U8"; break;
  1096. case V4L2_SDR_FMT_CU16LE: descr = "Complex U16LE"; break;
  1097. case V4L2_SDR_FMT_CS8: descr = "Complex S8"; break;
  1098. case V4L2_SDR_FMT_CS14LE: descr = "Complex S14LE"; break;
  1099. case V4L2_SDR_FMT_RU12LE: descr = "Real U12LE"; break;
  1100. default:
  1101. /* Compressed formats */
  1102. flags = V4L2_FMT_FLAG_COMPRESSED;
  1103. switch (fmt->pixelformat) {
  1104. /* Max description length mask: descr = "0123456789012345678901234567890" */
  1105. case V4L2_PIX_FMT_MJPEG: descr = "Motion-JPEG"; break;
  1106. case V4L2_PIX_FMT_JPEG: descr = "JFIF JPEG"; break;
  1107. case V4L2_PIX_FMT_DV: descr = "1394"; break;
  1108. case V4L2_PIX_FMT_MPEG: descr = "MPEG-1/2/4"; break;
  1109. case V4L2_PIX_FMT_H264: descr = "H.264"; break;
  1110. case V4L2_PIX_FMT_H264_NO_SC: descr = "H.264 (No Start Codes)"; break;
  1111. case V4L2_PIX_FMT_H264_MVC: descr = "H.264 MVC"; break;
  1112. case V4L2_PIX_FMT_H263: descr = "H.263"; break;
  1113. case V4L2_PIX_FMT_MPEG1: descr = "MPEG-1 ES"; break;
  1114. case V4L2_PIX_FMT_MPEG2: descr = "MPEG-2 ES"; break;
  1115. case V4L2_PIX_FMT_MPEG4: descr = "MPEG-4 part 2 ES"; break;
  1116. case V4L2_PIX_FMT_XVID: descr = "Xvid"; break;
  1117. case V4L2_PIX_FMT_VC1_ANNEX_G: descr = "VC-1 (SMPTE 412M Annex G)"; break;
  1118. case V4L2_PIX_FMT_VC1_ANNEX_L: descr = "VC-1 (SMPTE 412M Annex L)"; break;
  1119. case V4L2_PIX_FMT_VP8: descr = "VP8"; break;
  1120. case V4L2_PIX_FMT_CPIA1: descr = "GSPCA CPiA YUV"; break;
  1121. case V4L2_PIX_FMT_WNVA: descr = "WNVA"; break;
  1122. case V4L2_PIX_FMT_SN9C10X: descr = "GSPCA SN9C10X"; break;
  1123. case V4L2_PIX_FMT_PWC1: descr = "Raw Philips Webcam Type (Old)"; break;
  1124. case V4L2_PIX_FMT_PWC2: descr = "Raw Philips Webcam Type (New)"; break;
  1125. case V4L2_PIX_FMT_ET61X251: descr = "GSPCA ET61X251"; break;
  1126. case V4L2_PIX_FMT_SPCA561: descr = "GSPCA SPCA561"; break;
  1127. case V4L2_PIX_FMT_PAC207: descr = "GSPCA PAC207"; break;
  1128. case V4L2_PIX_FMT_MR97310A: descr = "GSPCA MR97310A"; break;
  1129. case V4L2_PIX_FMT_JL2005BCD: descr = "GSPCA JL2005BCD"; break;
  1130. case V4L2_PIX_FMT_SN9C2028: descr = "GSPCA SN9C2028"; break;
  1131. case V4L2_PIX_FMT_SQ905C: descr = "GSPCA SQ905C"; break;
  1132. case V4L2_PIX_FMT_PJPG: descr = "GSPCA PJPG"; break;
  1133. case V4L2_PIX_FMT_OV511: descr = "GSPCA OV511"; break;
  1134. case V4L2_PIX_FMT_OV518: descr = "GSPCA OV518"; break;
  1135. case V4L2_PIX_FMT_JPGL: descr = "JPEG Lite"; break;
  1136. case V4L2_PIX_FMT_SE401: descr = "GSPCA SE401"; break;
  1137. case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
  1138. default:
  1139. WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
  1140. if (fmt->description[0])
  1141. return;
  1142. flags = 0;
  1143. snprintf(fmt->description, sz, "%c%c%c%c%s",
  1144. (char)(fmt->pixelformat & 0x7f),
  1145. (char)((fmt->pixelformat >> 8) & 0x7f),
  1146. (char)((fmt->pixelformat >> 16) & 0x7f),
  1147. (char)((fmt->pixelformat >> 24) & 0x7f),
  1148. (fmt->pixelformat & (1 << 31)) ? "-BE" : "");
  1149. break;
  1150. }
  1151. }
  1152. if (descr)
  1153. WARN_ON(strlcpy(fmt->description, descr, sz) >= sz);
  1154. fmt->flags = flags;
  1155. }
  1156. static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
  1157. struct file *file, void *fh, void *arg)
  1158. {
  1159. struct v4l2_fmtdesc *p = arg;
  1160. struct video_device *vfd = video_devdata(file);
  1161. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1162. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1163. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1164. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1165. int ret = -EINVAL;
  1166. switch (p->type) {
  1167. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1168. if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap))
  1169. break;
  1170. ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
  1171. break;
  1172. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1173. if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap_mplane))
  1174. break;
  1175. ret = ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
  1176. break;
  1177. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1178. if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_overlay))
  1179. break;
  1180. ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
  1181. break;
  1182. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1183. if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out))
  1184. break;
  1185. ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
  1186. break;
  1187. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1188. if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out_mplane))
  1189. break;
  1190. ret = ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
  1191. break;
  1192. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1193. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_enum_fmt_sdr_cap))
  1194. break;
  1195. ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
  1196. break;
  1197. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1198. if (unlikely(!is_tx || !is_sdr || !ops->vidioc_enum_fmt_sdr_out))
  1199. break;
  1200. ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
  1201. break;
  1202. }
  1203. if (ret == 0)
  1204. v4l_fill_fmtdesc(p);
  1205. return ret;
  1206. }
  1207. static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
  1208. struct file *file, void *fh, void *arg)
  1209. {
  1210. struct v4l2_format *p = arg;
  1211. struct video_device *vfd = video_devdata(file);
  1212. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1213. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1214. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1215. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1216. int ret;
  1217. /*
  1218. * fmt can't be cleared for these overlay types due to the 'clips'
  1219. * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
  1220. * Those are provided by the user. So handle these two overlay types
  1221. * first, and then just do a simple memset for the other types.
  1222. */
  1223. switch (p->type) {
  1224. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1225. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
  1226. struct v4l2_clip __user *clips = p->fmt.win.clips;
  1227. u32 clipcount = p->fmt.win.clipcount;
  1228. void __user *bitmap = p->fmt.win.bitmap;
  1229. memset(&p->fmt, 0, sizeof(p->fmt));
  1230. p->fmt.win.clips = clips;
  1231. p->fmt.win.clipcount = clipcount;
  1232. p->fmt.win.bitmap = bitmap;
  1233. break;
  1234. }
  1235. default:
  1236. memset(&p->fmt, 0, sizeof(p->fmt));
  1237. break;
  1238. }
  1239. switch (p->type) {
  1240. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1241. if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
  1242. break;
  1243. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1244. ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
  1245. /* just in case the driver zeroed it again */
  1246. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1247. return ret;
  1248. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1249. if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
  1250. break;
  1251. return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
  1252. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1253. if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
  1254. break;
  1255. return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
  1256. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1257. if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
  1258. break;
  1259. return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
  1260. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1261. if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
  1262. break;
  1263. return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
  1264. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1265. if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
  1266. break;
  1267. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1268. ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
  1269. /* just in case the driver zeroed it again */
  1270. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1271. return ret;
  1272. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1273. if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
  1274. break;
  1275. return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
  1276. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1277. if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
  1278. break;
  1279. return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
  1280. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1281. if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
  1282. break;
  1283. return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
  1284. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1285. if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
  1286. break;
  1287. return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
  1288. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1289. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_g_fmt_sdr_cap))
  1290. break;
  1291. return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
  1292. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1293. if (unlikely(!is_tx || !is_sdr || !ops->vidioc_g_fmt_sdr_out))
  1294. break;
  1295. return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
  1296. }
  1297. return -EINVAL;
  1298. }
  1299. static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
  1300. struct file *file, void *fh, void *arg)
  1301. {
  1302. struct v4l2_format *p = arg;
  1303. struct video_device *vfd = video_devdata(file);
  1304. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1305. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1306. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1307. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1308. int ret;
  1309. ret = v4l_enable_media_source(vfd);
  1310. if (ret)
  1311. return ret;
  1312. v4l_sanitize_format(p);
  1313. switch (p->type) {
  1314. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1315. if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
  1316. break;
  1317. CLEAR_AFTER_FIELD(p, fmt.pix);
  1318. ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
  1319. /* just in case the driver zeroed it again */
  1320. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1321. return ret;
  1322. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1323. if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
  1324. break;
  1325. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1326. return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
  1327. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1328. if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
  1329. break;
  1330. CLEAR_AFTER_FIELD(p, fmt.win);
  1331. return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
  1332. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1333. if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
  1334. break;
  1335. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1336. return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
  1337. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1338. if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
  1339. break;
  1340. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1341. return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
  1342. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1343. if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
  1344. break;
  1345. CLEAR_AFTER_FIELD(p, fmt.pix);
  1346. ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
  1347. /* just in case the driver zeroed it again */
  1348. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1349. return ret;
  1350. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1351. if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
  1352. break;
  1353. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1354. return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
  1355. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1356. if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
  1357. break;
  1358. CLEAR_AFTER_FIELD(p, fmt.win);
  1359. return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
  1360. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1361. if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
  1362. break;
  1363. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1364. return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
  1365. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1366. if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
  1367. break;
  1368. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1369. return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
  1370. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1371. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_s_fmt_sdr_cap))
  1372. break;
  1373. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1374. return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
  1375. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1376. if (unlikely(!is_tx || !is_sdr || !ops->vidioc_s_fmt_sdr_out))
  1377. break;
  1378. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1379. return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
  1380. }
  1381. return -EINVAL;
  1382. }
  1383. static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
  1384. struct file *file, void *fh, void *arg)
  1385. {
  1386. struct v4l2_format *p = arg;
  1387. struct video_device *vfd = video_devdata(file);
  1388. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1389. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1390. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1391. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1392. int ret;
  1393. v4l_sanitize_format(p);
  1394. switch (p->type) {
  1395. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1396. if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
  1397. break;
  1398. CLEAR_AFTER_FIELD(p, fmt.pix);
  1399. ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
  1400. /* just in case the driver zeroed it again */
  1401. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1402. return ret;
  1403. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1404. if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
  1405. break;
  1406. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1407. return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
  1408. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1409. if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
  1410. break;
  1411. CLEAR_AFTER_FIELD(p, fmt.win);
  1412. return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
  1413. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1414. if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
  1415. break;
  1416. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1417. return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
  1418. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1419. if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
  1420. break;
  1421. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1422. return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
  1423. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1424. if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
  1425. break;
  1426. CLEAR_AFTER_FIELD(p, fmt.pix);
  1427. ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
  1428. /* just in case the driver zeroed it again */
  1429. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1430. return ret;
  1431. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1432. if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
  1433. break;
  1434. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1435. return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
  1436. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1437. if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
  1438. break;
  1439. CLEAR_AFTER_FIELD(p, fmt.win);
  1440. return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
  1441. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1442. if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
  1443. break;
  1444. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1445. return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
  1446. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1447. if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
  1448. break;
  1449. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1450. return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
  1451. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1452. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_try_fmt_sdr_cap))
  1453. break;
  1454. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1455. return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
  1456. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1457. if (unlikely(!is_tx || !is_sdr || !ops->vidioc_try_fmt_sdr_out))
  1458. break;
  1459. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1460. return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
  1461. }
  1462. return -EINVAL;
  1463. }
  1464. static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
  1465. struct file *file, void *fh, void *arg)
  1466. {
  1467. return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
  1468. }
  1469. static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
  1470. struct file *file, void *fh, void *arg)
  1471. {
  1472. return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
  1473. }
  1474. static int v4l_g_tuner(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_tuner *p = arg;
  1479. int err;
  1480. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1481. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1482. err = ops->vidioc_g_tuner(file, fh, p);
  1483. if (!err)
  1484. p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
  1485. return err;
  1486. }
  1487. static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
  1488. struct file *file, void *fh, void *arg)
  1489. {
  1490. struct video_device *vfd = video_devdata(file);
  1491. struct v4l2_tuner *p = arg;
  1492. int ret;
  1493. ret = v4l_enable_media_source(vfd);
  1494. if (ret)
  1495. return ret;
  1496. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1497. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1498. return ops->vidioc_s_tuner(file, fh, p);
  1499. }
  1500. static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
  1501. struct file *file, void *fh, void *arg)
  1502. {
  1503. struct video_device *vfd = video_devdata(file);
  1504. struct v4l2_modulator *p = arg;
  1505. int err;
  1506. if (vfd->vfl_type == VFL_TYPE_RADIO)
  1507. p->type = V4L2_TUNER_RADIO;
  1508. err = ops->vidioc_g_modulator(file, fh, p);
  1509. if (!err)
  1510. p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
  1511. return err;
  1512. }
  1513. static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
  1514. struct file *file, void *fh, void *arg)
  1515. {
  1516. struct video_device *vfd = video_devdata(file);
  1517. struct v4l2_modulator *p = arg;
  1518. if (vfd->vfl_type == VFL_TYPE_RADIO)
  1519. p->type = V4L2_TUNER_RADIO;
  1520. return ops->vidioc_s_modulator(file, fh, p);
  1521. }
  1522. static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
  1523. struct file *file, void *fh, void *arg)
  1524. {
  1525. struct video_device *vfd = video_devdata(file);
  1526. struct v4l2_frequency *p = arg;
  1527. if (vfd->vfl_type == VFL_TYPE_SDR)
  1528. p->type = V4L2_TUNER_SDR;
  1529. else
  1530. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1531. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1532. return ops->vidioc_g_frequency(file, fh, p);
  1533. }
  1534. static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
  1535. struct file *file, void *fh, void *arg)
  1536. {
  1537. struct video_device *vfd = video_devdata(file);
  1538. const struct v4l2_frequency *p = arg;
  1539. enum v4l2_tuner_type type;
  1540. int ret;
  1541. ret = v4l_enable_media_source(vfd);
  1542. if (ret)
  1543. return ret;
  1544. if (vfd->vfl_type == VFL_TYPE_SDR) {
  1545. if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
  1546. return -EINVAL;
  1547. } else {
  1548. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1549. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1550. if (type != p->type)
  1551. return -EINVAL;
  1552. }
  1553. return ops->vidioc_s_frequency(file, fh, p);
  1554. }
  1555. static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
  1556. struct file *file, void *fh, void *arg)
  1557. {
  1558. struct video_device *vfd = video_devdata(file);
  1559. struct v4l2_standard *p = arg;
  1560. v4l2_std_id id = vfd->tvnorms, curr_id = 0;
  1561. unsigned int index = p->index, i, j = 0;
  1562. const char *descr = "";
  1563. /* Return -ENODATA if the tvnorms for the current input
  1564. or output is 0, meaning that it doesn't support this API. */
  1565. if (id == 0)
  1566. return -ENODATA;
  1567. /* Return norm array in a canonical way */
  1568. for (i = 0; i <= index && id; i++) {
  1569. /* last std value in the standards array is 0, so this
  1570. while always ends there since (id & 0) == 0. */
  1571. while ((id & standards[j].std) != standards[j].std)
  1572. j++;
  1573. curr_id = standards[j].std;
  1574. descr = standards[j].descr;
  1575. j++;
  1576. if (curr_id == 0)
  1577. break;
  1578. if (curr_id != V4L2_STD_PAL &&
  1579. curr_id != V4L2_STD_SECAM &&
  1580. curr_id != V4L2_STD_NTSC)
  1581. id &= ~curr_id;
  1582. }
  1583. if (i <= index)
  1584. return -EINVAL;
  1585. v4l2_video_std_construct(p, curr_id, descr);
  1586. return 0;
  1587. }
  1588. static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
  1589. struct file *file, void *fh, void *arg)
  1590. {
  1591. struct video_device *vfd = video_devdata(file);
  1592. v4l2_std_id id = *(v4l2_std_id *)arg, norm;
  1593. int ret;
  1594. ret = v4l_enable_media_source(vfd);
  1595. if (ret)
  1596. return ret;
  1597. norm = id & vfd->tvnorms;
  1598. if (vfd->tvnorms && !norm) /* Check if std is supported */
  1599. return -EINVAL;
  1600. /* Calls the specific handler */
  1601. return ops->vidioc_s_std(file, fh, norm);
  1602. }
  1603. static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
  1604. struct file *file, void *fh, void *arg)
  1605. {
  1606. struct video_device *vfd = video_devdata(file);
  1607. v4l2_std_id *p = arg;
  1608. int ret;
  1609. ret = v4l_enable_media_source(vfd);
  1610. if (ret)
  1611. return ret;
  1612. /*
  1613. * If no signal is detected, then the driver should return
  1614. * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
  1615. * any standards that do not apply removed.
  1616. *
  1617. * This means that tuners, audio and video decoders can join
  1618. * their efforts to improve the standards detection.
  1619. */
  1620. *p = vfd->tvnorms;
  1621. return ops->vidioc_querystd(file, fh, arg);
  1622. }
  1623. static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
  1624. struct file *file, void *fh, void *arg)
  1625. {
  1626. struct video_device *vfd = video_devdata(file);
  1627. struct v4l2_hw_freq_seek *p = arg;
  1628. enum v4l2_tuner_type type;
  1629. int ret;
  1630. ret = v4l_enable_media_source(vfd);
  1631. if (ret)
  1632. return ret;
  1633. /* s_hw_freq_seek is not supported for SDR for now */
  1634. if (vfd->vfl_type == VFL_TYPE_SDR)
  1635. return -EINVAL;
  1636. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1637. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1638. if (p->type != type)
  1639. return -EINVAL;
  1640. return ops->vidioc_s_hw_freq_seek(file, fh, p);
  1641. }
  1642. static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
  1643. struct file *file, void *fh, void *arg)
  1644. {
  1645. return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
  1646. }
  1647. static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
  1648. struct file *file, void *fh, void *arg)
  1649. {
  1650. struct v4l2_requestbuffers *p = arg;
  1651. int ret = check_fmt(file, p->type);
  1652. if (ret)
  1653. return ret;
  1654. CLEAR_AFTER_FIELD(p, memory);
  1655. return ops->vidioc_reqbufs(file, fh, p);
  1656. }
  1657. static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
  1658. struct file *file, void *fh, void *arg)
  1659. {
  1660. struct v4l2_buffer *p = arg;
  1661. int ret = check_fmt(file, p->type);
  1662. return ret ? ret : ops->vidioc_querybuf(file, fh, p);
  1663. }
  1664. static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
  1665. struct file *file, void *fh, void *arg)
  1666. {
  1667. struct v4l2_buffer *p = arg;
  1668. int ret = check_fmt(file, p->type);
  1669. return ret ? ret : ops->vidioc_qbuf(file, fh, p);
  1670. }
  1671. static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
  1672. struct file *file, void *fh, void *arg)
  1673. {
  1674. struct v4l2_buffer *p = arg;
  1675. int ret = check_fmt(file, p->type);
  1676. return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
  1677. }
  1678. static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
  1679. struct file *file, void *fh, void *arg)
  1680. {
  1681. struct v4l2_create_buffers *create = arg;
  1682. int ret = check_fmt(file, create->format.type);
  1683. if (ret)
  1684. return ret;
  1685. CLEAR_AFTER_FIELD(create, format);
  1686. v4l_sanitize_format(&create->format);
  1687. ret = ops->vidioc_create_bufs(file, fh, create);
  1688. if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1689. create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1690. create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1691. return ret;
  1692. }
  1693. static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
  1694. struct file *file, void *fh, void *arg)
  1695. {
  1696. struct v4l2_buffer *b = arg;
  1697. int ret = check_fmt(file, b->type);
  1698. return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
  1699. }
  1700. static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
  1701. struct file *file, void *fh, void *arg)
  1702. {
  1703. struct v4l2_streamparm *p = arg;
  1704. v4l2_std_id std;
  1705. int ret = check_fmt(file, p->type);
  1706. if (ret)
  1707. return ret;
  1708. if (ops->vidioc_g_parm)
  1709. return ops->vidioc_g_parm(file, fh, p);
  1710. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1711. p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1712. return -EINVAL;
  1713. p->parm.capture.readbuffers = 2;
  1714. ret = ops->vidioc_g_std(file, fh, &std);
  1715. if (ret == 0)
  1716. v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
  1717. return ret;
  1718. }
  1719. static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
  1720. struct file *file, void *fh, void *arg)
  1721. {
  1722. struct v4l2_streamparm *p = arg;
  1723. int ret = check_fmt(file, p->type);
  1724. return ret ? ret : ops->vidioc_s_parm(file, fh, p);
  1725. }
  1726. static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
  1727. struct file *file, void *fh, void *arg)
  1728. {
  1729. struct video_device *vfd = video_devdata(file);
  1730. struct v4l2_queryctrl *p = arg;
  1731. struct v4l2_fh *vfh =
  1732. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1733. if (vfh && vfh->ctrl_handler)
  1734. return v4l2_queryctrl(vfh->ctrl_handler, p);
  1735. if (vfd->ctrl_handler)
  1736. return v4l2_queryctrl(vfd->ctrl_handler, p);
  1737. if (ops->vidioc_queryctrl)
  1738. return ops->vidioc_queryctrl(file, fh, p);
  1739. return -ENOTTY;
  1740. }
  1741. static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
  1742. struct file *file, void *fh, void *arg)
  1743. {
  1744. struct video_device *vfd = video_devdata(file);
  1745. struct v4l2_query_ext_ctrl *p = arg;
  1746. struct v4l2_fh *vfh =
  1747. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1748. if (vfh && vfh->ctrl_handler)
  1749. return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
  1750. if (vfd->ctrl_handler)
  1751. return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
  1752. if (ops->vidioc_query_ext_ctrl)
  1753. return ops->vidioc_query_ext_ctrl(file, fh, p);
  1754. return -ENOTTY;
  1755. }
  1756. static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
  1757. struct file *file, void *fh, void *arg)
  1758. {
  1759. struct video_device *vfd = video_devdata(file);
  1760. struct v4l2_querymenu *p = arg;
  1761. struct v4l2_fh *vfh =
  1762. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1763. if (vfh && vfh->ctrl_handler)
  1764. return v4l2_querymenu(vfh->ctrl_handler, p);
  1765. if (vfd->ctrl_handler)
  1766. return v4l2_querymenu(vfd->ctrl_handler, p);
  1767. if (ops->vidioc_querymenu)
  1768. return ops->vidioc_querymenu(file, fh, p);
  1769. return -ENOTTY;
  1770. }
  1771. static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
  1772. struct file *file, void *fh, void *arg)
  1773. {
  1774. struct video_device *vfd = video_devdata(file);
  1775. struct v4l2_control *p = arg;
  1776. struct v4l2_fh *vfh =
  1777. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1778. struct v4l2_ext_controls ctrls;
  1779. struct v4l2_ext_control ctrl;
  1780. if (vfh && vfh->ctrl_handler)
  1781. return v4l2_g_ctrl(vfh->ctrl_handler, p);
  1782. if (vfd->ctrl_handler)
  1783. return v4l2_g_ctrl(vfd->ctrl_handler, p);
  1784. if (ops->vidioc_g_ctrl)
  1785. return ops->vidioc_g_ctrl(file, fh, p);
  1786. if (ops->vidioc_g_ext_ctrls == NULL)
  1787. return -ENOTTY;
  1788. ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
  1789. ctrls.count = 1;
  1790. ctrls.controls = &ctrl;
  1791. ctrl.id = p->id;
  1792. ctrl.value = p->value;
  1793. if (check_ext_ctrls(&ctrls, 1)) {
  1794. int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
  1795. if (ret == 0)
  1796. p->value = ctrl.value;
  1797. return ret;
  1798. }
  1799. return -EINVAL;
  1800. }
  1801. static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
  1802. struct file *file, void *fh, void *arg)
  1803. {
  1804. struct video_device *vfd = video_devdata(file);
  1805. struct v4l2_control *p = arg;
  1806. struct v4l2_fh *vfh =
  1807. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1808. struct v4l2_ext_controls ctrls;
  1809. struct v4l2_ext_control ctrl;
  1810. if (vfh && vfh->ctrl_handler)
  1811. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
  1812. if (vfd->ctrl_handler)
  1813. return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
  1814. if (ops->vidioc_s_ctrl)
  1815. return ops->vidioc_s_ctrl(file, fh, p);
  1816. if (ops->vidioc_s_ext_ctrls == NULL)
  1817. return -ENOTTY;
  1818. ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
  1819. ctrls.count = 1;
  1820. ctrls.controls = &ctrl;
  1821. ctrl.id = p->id;
  1822. ctrl.value = p->value;
  1823. if (check_ext_ctrls(&ctrls, 1))
  1824. return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
  1825. return -EINVAL;
  1826. }
  1827. static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1828. struct file *file, void *fh, void *arg)
  1829. {
  1830. struct video_device *vfd = video_devdata(file);
  1831. struct v4l2_ext_controls *p = arg;
  1832. struct v4l2_fh *vfh =
  1833. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1834. p->error_idx = p->count;
  1835. if (vfh && vfh->ctrl_handler)
  1836. return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
  1837. if (vfd->ctrl_handler)
  1838. return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
  1839. if (ops->vidioc_g_ext_ctrls == NULL)
  1840. return -ENOTTY;
  1841. return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
  1842. -EINVAL;
  1843. }
  1844. static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1845. struct file *file, void *fh, void *arg)
  1846. {
  1847. struct video_device *vfd = video_devdata(file);
  1848. struct v4l2_ext_controls *p = arg;
  1849. struct v4l2_fh *vfh =
  1850. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1851. p->error_idx = p->count;
  1852. if (vfh && vfh->ctrl_handler)
  1853. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
  1854. if (vfd->ctrl_handler)
  1855. return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
  1856. if (ops->vidioc_s_ext_ctrls == NULL)
  1857. return -ENOTTY;
  1858. return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
  1859. -EINVAL;
  1860. }
  1861. static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1862. struct file *file, void *fh, void *arg)
  1863. {
  1864. struct video_device *vfd = video_devdata(file);
  1865. struct v4l2_ext_controls *p = arg;
  1866. struct v4l2_fh *vfh =
  1867. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1868. p->error_idx = p->count;
  1869. if (vfh && vfh->ctrl_handler)
  1870. return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
  1871. if (vfd->ctrl_handler)
  1872. return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
  1873. if (ops->vidioc_try_ext_ctrls == NULL)
  1874. return -ENOTTY;
  1875. return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
  1876. -EINVAL;
  1877. }
  1878. static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
  1879. struct file *file, void *fh, void *arg)
  1880. {
  1881. struct v4l2_crop *p = arg;
  1882. struct v4l2_selection s = {
  1883. .type = p->type,
  1884. };
  1885. int ret;
  1886. if (ops->vidioc_g_crop)
  1887. return ops->vidioc_g_crop(file, fh, p);
  1888. /* simulate capture crop using selection api */
  1889. /* crop means compose for output devices */
  1890. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1891. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1892. else
  1893. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1894. ret = ops->vidioc_g_selection(file, fh, &s);
  1895. /* copying results to old structure on success */
  1896. if (!ret)
  1897. p->c = s.r;
  1898. return ret;
  1899. }
  1900. static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
  1901. struct file *file, void *fh, void *arg)
  1902. {
  1903. struct v4l2_crop *p = arg;
  1904. struct v4l2_selection s = {
  1905. .type = p->type,
  1906. .r = p->c,
  1907. };
  1908. if (ops->vidioc_s_crop)
  1909. return ops->vidioc_s_crop(file, fh, p);
  1910. /* simulate capture crop using selection api */
  1911. /* crop means compose for output devices */
  1912. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1913. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1914. else
  1915. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1916. return ops->vidioc_s_selection(file, fh, &s);
  1917. }
  1918. static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
  1919. struct file *file, void *fh, void *arg)
  1920. {
  1921. struct v4l2_cropcap *p = arg;
  1922. struct v4l2_selection s = { .type = p->type };
  1923. int ret = 0;
  1924. /* setting trivial pixelaspect */
  1925. p->pixelaspect.numerator = 1;
  1926. p->pixelaspect.denominator = 1;
  1927. /*
  1928. * The determine_valid_ioctls() call already should ensure
  1929. * that this can never happen, but just in case...
  1930. */
  1931. if (WARN_ON(!ops->vidioc_cropcap && !ops->vidioc_g_selection))
  1932. return -ENOTTY;
  1933. if (ops->vidioc_cropcap)
  1934. ret = ops->vidioc_cropcap(file, fh, p);
  1935. if (!ops->vidioc_g_selection)
  1936. return ret;
  1937. /*
  1938. * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
  1939. * square pixel aspect ratio in that case.
  1940. */
  1941. if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
  1942. return ret;
  1943. /* Use g_selection() to fill in the bounds and defrect rectangles */
  1944. /* obtaining bounds */
  1945. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1946. s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
  1947. else
  1948. s.target = V4L2_SEL_TGT_CROP_BOUNDS;
  1949. ret = ops->vidioc_g_selection(file, fh, &s);
  1950. if (ret)
  1951. return ret;
  1952. p->bounds = s.r;
  1953. /* obtaining defrect */
  1954. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1955. s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
  1956. else
  1957. s.target = V4L2_SEL_TGT_CROP_DEFAULT;
  1958. ret = ops->vidioc_g_selection(file, fh, &s);
  1959. if (ret)
  1960. return ret;
  1961. p->defrect = s.r;
  1962. return 0;
  1963. }
  1964. static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
  1965. struct file *file, void *fh, void *arg)
  1966. {
  1967. struct video_device *vfd = video_devdata(file);
  1968. int ret;
  1969. if (vfd->v4l2_dev)
  1970. pr_info("%s: ================= START STATUS =================\n",
  1971. vfd->v4l2_dev->name);
  1972. ret = ops->vidioc_log_status(file, fh);
  1973. if (vfd->v4l2_dev)
  1974. pr_info("%s: ================== END STATUS ==================\n",
  1975. vfd->v4l2_dev->name);
  1976. return ret;
  1977. }
  1978. static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
  1979. struct file *file, void *fh, void *arg)
  1980. {
  1981. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1982. struct v4l2_dbg_register *p = arg;
  1983. struct video_device *vfd = video_devdata(file);
  1984. struct v4l2_subdev *sd;
  1985. int idx = 0;
  1986. if (!capable(CAP_SYS_ADMIN))
  1987. return -EPERM;
  1988. if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
  1989. if (vfd->v4l2_dev == NULL)
  1990. return -EINVAL;
  1991. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
  1992. if (p->match.addr == idx++)
  1993. return v4l2_subdev_call(sd, core, g_register, p);
  1994. return -EINVAL;
  1995. }
  1996. if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
  1997. (ops->vidioc_g_chip_info || p->match.addr == 0))
  1998. return ops->vidioc_g_register(file, fh, p);
  1999. return -EINVAL;
  2000. #else
  2001. return -ENOTTY;
  2002. #endif
  2003. }
  2004. static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
  2005. struct file *file, void *fh, void *arg)
  2006. {
  2007. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2008. const struct v4l2_dbg_register *p = arg;
  2009. struct video_device *vfd = video_devdata(file);
  2010. struct v4l2_subdev *sd;
  2011. int idx = 0;
  2012. if (!capable(CAP_SYS_ADMIN))
  2013. return -EPERM;
  2014. if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
  2015. if (vfd->v4l2_dev == NULL)
  2016. return -EINVAL;
  2017. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
  2018. if (p->match.addr == idx++)
  2019. return v4l2_subdev_call(sd, core, s_register, p);
  2020. return -EINVAL;
  2021. }
  2022. if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
  2023. (ops->vidioc_g_chip_info || p->match.addr == 0))
  2024. return ops->vidioc_s_register(file, fh, p);
  2025. return -EINVAL;
  2026. #else
  2027. return -ENOTTY;
  2028. #endif
  2029. }
  2030. static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
  2031. struct file *file, void *fh, void *arg)
  2032. {
  2033. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2034. struct video_device *vfd = video_devdata(file);
  2035. struct v4l2_dbg_chip_info *p = arg;
  2036. struct v4l2_subdev *sd;
  2037. int idx = 0;
  2038. switch (p->match.type) {
  2039. case V4L2_CHIP_MATCH_BRIDGE:
  2040. if (ops->vidioc_s_register)
  2041. p->flags |= V4L2_CHIP_FL_WRITABLE;
  2042. if (ops->vidioc_g_register)
  2043. p->flags |= V4L2_CHIP_FL_READABLE;
  2044. strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
  2045. if (ops->vidioc_g_chip_info)
  2046. return ops->vidioc_g_chip_info(file, fh, arg);
  2047. if (p->match.addr)
  2048. return -EINVAL;
  2049. return 0;
  2050. case V4L2_CHIP_MATCH_SUBDEV:
  2051. if (vfd->v4l2_dev == NULL)
  2052. break;
  2053. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
  2054. if (p->match.addr != idx++)
  2055. continue;
  2056. if (sd->ops->core && sd->ops->core->s_register)
  2057. p->flags |= V4L2_CHIP_FL_WRITABLE;
  2058. if (sd->ops->core && sd->ops->core->g_register)
  2059. p->flags |= V4L2_CHIP_FL_READABLE;
  2060. strlcpy(p->name, sd->name, sizeof(p->name));
  2061. return 0;
  2062. }
  2063. break;
  2064. }
  2065. return -EINVAL;
  2066. #else
  2067. return -ENOTTY;
  2068. #endif
  2069. }
  2070. static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
  2071. struct file *file, void *fh, void *arg)
  2072. {
  2073. return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
  2074. }
  2075. static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
  2076. struct file *file, void *fh, void *arg)
  2077. {
  2078. return ops->vidioc_subscribe_event(fh, arg);
  2079. }
  2080. static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
  2081. struct file *file, void *fh, void *arg)
  2082. {
  2083. return ops->vidioc_unsubscribe_event(fh, arg);
  2084. }
  2085. static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
  2086. struct file *file, void *fh, void *arg)
  2087. {
  2088. struct v4l2_sliced_vbi_cap *p = arg;
  2089. int ret = check_fmt(file, p->type);
  2090. if (ret)
  2091. return ret;
  2092. /* Clear up to type, everything after type is zeroed already */
  2093. memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
  2094. return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
  2095. }
  2096. static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
  2097. struct file *file, void *fh, void *arg)
  2098. {
  2099. struct video_device *vfd = video_devdata(file);
  2100. struct v4l2_frequency_band *p = arg;
  2101. enum v4l2_tuner_type type;
  2102. int err;
  2103. if (vfd->vfl_type == VFL_TYPE_SDR) {
  2104. if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
  2105. return -EINVAL;
  2106. type = p->type;
  2107. } else {
  2108. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  2109. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  2110. if (type != p->type)
  2111. return -EINVAL;
  2112. }
  2113. if (ops->vidioc_enum_freq_bands) {
  2114. err = ops->vidioc_enum_freq_bands(file, fh, p);
  2115. if (err != -ENOTTY)
  2116. return err;
  2117. }
  2118. if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
  2119. struct v4l2_tuner t = {
  2120. .index = p->tuner,
  2121. .type = type,
  2122. };
  2123. if (p->index)
  2124. return -EINVAL;
  2125. err = ops->vidioc_g_tuner(file, fh, &t);
  2126. if (err)
  2127. return err;
  2128. p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
  2129. p->rangelow = t.rangelow;
  2130. p->rangehigh = t.rangehigh;
  2131. p->modulation = (type == V4L2_TUNER_RADIO) ?
  2132. V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
  2133. return 0;
  2134. }
  2135. if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
  2136. struct v4l2_modulator m = {
  2137. .index = p->tuner,
  2138. };
  2139. if (type != V4L2_TUNER_RADIO)
  2140. return -EINVAL;
  2141. if (p->index)
  2142. return -EINVAL;
  2143. err = ops->vidioc_g_modulator(file, fh, &m);
  2144. if (err)
  2145. return err;
  2146. p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
  2147. p->rangelow = m.rangelow;
  2148. p->rangehigh = m.rangehigh;
  2149. p->modulation = (type == V4L2_TUNER_RADIO) ?
  2150. V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
  2151. return 0;
  2152. }
  2153. return -ENOTTY;
  2154. }
  2155. struct v4l2_ioctl_info {
  2156. unsigned int ioctl;
  2157. u32 flags;
  2158. const char * const name;
  2159. union {
  2160. u32 offset;
  2161. int (*func)(const struct v4l2_ioctl_ops *ops,
  2162. struct file *file, void *fh, void *p);
  2163. } u;
  2164. void (*debug)(const void *arg, bool write_only);
  2165. };
  2166. /* This control needs a priority check */
  2167. #define INFO_FL_PRIO (1 << 0)
  2168. /* This control can be valid if the filehandle passes a control handler. */
  2169. #define INFO_FL_CTRL (1 << 1)
  2170. /* This is a standard ioctl, no need for special code */
  2171. #define INFO_FL_STD (1 << 2)
  2172. /* This is ioctl has its own function */
  2173. #define INFO_FL_FUNC (1 << 3)
  2174. /* Queuing ioctl */
  2175. #define INFO_FL_QUEUE (1 << 4)
  2176. /* Zero struct from after the field to the end */
  2177. #define INFO_FL_CLEAR(v4l2_struct, field) \
  2178. ((offsetof(struct v4l2_struct, field) + \
  2179. sizeof(((struct v4l2_struct *)0)->field)) << 16)
  2180. #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
  2181. #define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
  2182. [_IOC_NR(_ioctl)] = { \
  2183. .ioctl = _ioctl, \
  2184. .flags = _flags | INFO_FL_STD, \
  2185. .name = #_ioctl, \
  2186. .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
  2187. .debug = _debug, \
  2188. }
  2189. #define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
  2190. [_IOC_NR(_ioctl)] = { \
  2191. .ioctl = _ioctl, \
  2192. .flags = _flags | INFO_FL_FUNC, \
  2193. .name = #_ioctl, \
  2194. .u.func = _func, \
  2195. .debug = _debug, \
  2196. }
  2197. static struct v4l2_ioctl_info v4l2_ioctls[] = {
  2198. IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
  2199. IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
  2200. IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
  2201. IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
  2202. IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
  2203. IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
  2204. IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
  2205. IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
  2206. IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
  2207. IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
  2208. IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
  2209. IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
  2210. IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
  2211. IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
  2212. IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
  2213. IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
  2214. IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
  2215. IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
  2216. IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
  2217. IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
  2218. IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
  2219. IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
  2220. IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
  2221. IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
  2222. IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
  2223. IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
  2224. IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
  2225. IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
  2226. IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
  2227. IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
  2228. IOCTL_INFO_STD(VIDIOC_G_EDID, vidioc_g_edid, v4l_print_edid, 0),
  2229. IOCTL_INFO_STD(VIDIOC_S_EDID, vidioc_s_edid, v4l_print_edid, INFO_FL_PRIO),
  2230. IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
  2231. IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
  2232. IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
  2233. IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
  2234. IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
  2235. IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
  2236. IOCTL_INFO_FNC(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
  2237. IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
  2238. IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
  2239. IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
  2240. IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
  2241. IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
  2242. IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
  2243. IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
  2244. IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
  2245. IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
  2246. IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
  2247. IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
  2248. IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
  2249. IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
  2250. IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
  2251. IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
  2252. 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)),
  2253. IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
  2254. IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  2255. IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
  2256. IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  2257. IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
  2258. IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
  2259. IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
  2260. IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  2261. IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  2262. IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
  2263. IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
  2264. IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
  2265. IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
  2266. IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
  2267. IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
  2268. IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
  2269. IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
  2270. IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
  2271. IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
  2272. IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
  2273. IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
  2274. IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
  2275. IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
  2276. IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
  2277. IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
  2278. 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)),
  2279. 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)),
  2280. };
  2281. #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
  2282. bool v4l2_is_known_ioctl(unsigned int cmd)
  2283. {
  2284. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  2285. return false;
  2286. return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
  2287. }
  2288. struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
  2289. {
  2290. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  2291. return vdev->lock;
  2292. if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
  2293. return NULL;
  2294. if (vdev->queue && vdev->queue->lock &&
  2295. (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
  2296. return vdev->queue->lock;
  2297. return vdev->lock;
  2298. }
  2299. /* Common ioctl debug function. This function can be used by
  2300. external ioctl messages as well as internal V4L ioctl */
  2301. void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
  2302. {
  2303. const char *dir, *type;
  2304. if (prefix)
  2305. printk(KERN_DEBUG "%s: ", prefix);
  2306. switch (_IOC_TYPE(cmd)) {
  2307. case 'd':
  2308. type = "v4l2_int";
  2309. break;
  2310. case 'V':
  2311. if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
  2312. type = "v4l2";
  2313. break;
  2314. }
  2315. pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
  2316. return;
  2317. default:
  2318. type = "unknown";
  2319. break;
  2320. }
  2321. switch (_IOC_DIR(cmd)) {
  2322. case _IOC_NONE: dir = "--"; break;
  2323. case _IOC_READ: dir = "r-"; break;
  2324. case _IOC_WRITE: dir = "-w"; break;
  2325. case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
  2326. default: dir = "*ERR*"; break;
  2327. }
  2328. pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
  2329. type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
  2330. }
  2331. EXPORT_SYMBOL(v4l_printk_ioctl);
  2332. static long __video_do_ioctl(struct file *file,
  2333. unsigned int cmd, void *arg)
  2334. {
  2335. struct video_device *vfd = video_devdata(file);
  2336. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  2337. bool write_only = false;
  2338. struct v4l2_ioctl_info default_info;
  2339. const struct v4l2_ioctl_info *info;
  2340. void *fh = file->private_data;
  2341. struct v4l2_fh *vfh = NULL;
  2342. int dev_debug = vfd->dev_debug;
  2343. long ret = -ENOTTY;
  2344. if (ops == NULL) {
  2345. pr_warn("%s: has no ioctl_ops.\n",
  2346. video_device_node_name(vfd));
  2347. return ret;
  2348. }
  2349. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
  2350. vfh = file->private_data;
  2351. if (v4l2_is_known_ioctl(cmd)) {
  2352. info = &v4l2_ioctls[_IOC_NR(cmd)];
  2353. if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
  2354. !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
  2355. goto done;
  2356. if (vfh && (info->flags & INFO_FL_PRIO)) {
  2357. ret = v4l2_prio_check(vfd->prio, vfh->prio);
  2358. if (ret)
  2359. goto done;
  2360. }
  2361. } else {
  2362. default_info.ioctl = cmd;
  2363. default_info.flags = 0;
  2364. default_info.debug = v4l_print_default;
  2365. info = &default_info;
  2366. }
  2367. write_only = _IOC_DIR(cmd) == _IOC_WRITE;
  2368. if (info->flags & INFO_FL_STD) {
  2369. typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
  2370. const void *p = vfd->ioctl_ops;
  2371. const vidioc_op *vidioc = p + info->u.offset;
  2372. ret = (*vidioc)(file, fh, arg);
  2373. } else if (info->flags & INFO_FL_FUNC) {
  2374. ret = info->u.func(ops, file, fh, arg);
  2375. } else if (!ops->vidioc_default) {
  2376. ret = -ENOTTY;
  2377. } else {
  2378. ret = ops->vidioc_default(file, fh,
  2379. vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
  2380. cmd, arg);
  2381. }
  2382. done:
  2383. if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
  2384. if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
  2385. (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
  2386. return ret;
  2387. v4l_printk_ioctl(video_device_node_name(vfd), cmd);
  2388. if (ret < 0)
  2389. pr_cont(": error %ld", ret);
  2390. if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
  2391. pr_cont("\n");
  2392. else if (_IOC_DIR(cmd) == _IOC_NONE)
  2393. info->debug(arg, write_only);
  2394. else {
  2395. pr_cont(": ");
  2396. info->debug(arg, write_only);
  2397. }
  2398. }
  2399. return ret;
  2400. }
  2401. static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
  2402. void __user **user_ptr, void ***kernel_ptr)
  2403. {
  2404. int ret = 0;
  2405. switch (cmd) {
  2406. case VIDIOC_PREPARE_BUF:
  2407. case VIDIOC_QUERYBUF:
  2408. case VIDIOC_QBUF:
  2409. case VIDIOC_DQBUF: {
  2410. struct v4l2_buffer *buf = parg;
  2411. if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
  2412. if (buf->length > VIDEO_MAX_PLANES) {
  2413. ret = -EINVAL;
  2414. break;
  2415. }
  2416. *user_ptr = (void __user *)buf->m.planes;
  2417. *kernel_ptr = (void **)&buf->m.planes;
  2418. *array_size = sizeof(struct v4l2_plane) * buf->length;
  2419. ret = 1;
  2420. }
  2421. break;
  2422. }
  2423. case VIDIOC_G_EDID:
  2424. case VIDIOC_S_EDID: {
  2425. struct v4l2_edid *edid = parg;
  2426. if (edid->blocks) {
  2427. if (edid->blocks > 256) {
  2428. ret = -EINVAL;
  2429. break;
  2430. }
  2431. *user_ptr = (void __user *)edid->edid;
  2432. *kernel_ptr = (void **)&edid->edid;
  2433. *array_size = edid->blocks * 128;
  2434. ret = 1;
  2435. }
  2436. break;
  2437. }
  2438. case VIDIOC_S_EXT_CTRLS:
  2439. case VIDIOC_G_EXT_CTRLS:
  2440. case VIDIOC_TRY_EXT_CTRLS: {
  2441. struct v4l2_ext_controls *ctrls = parg;
  2442. if (ctrls->count != 0) {
  2443. if (ctrls->count > V4L2_CID_MAX_CTRLS) {
  2444. ret = -EINVAL;
  2445. break;
  2446. }
  2447. *user_ptr = (void __user *)ctrls->controls;
  2448. *kernel_ptr = (void **)&ctrls->controls;
  2449. *array_size = sizeof(struct v4l2_ext_control)
  2450. * ctrls->count;
  2451. ret = 1;
  2452. }
  2453. break;
  2454. }
  2455. }
  2456. return ret;
  2457. }
  2458. long
  2459. video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
  2460. v4l2_kioctl func)
  2461. {
  2462. char sbuf[128];
  2463. void *mbuf = NULL;
  2464. void *parg = (void *)arg;
  2465. long err = -EINVAL;
  2466. bool has_array_args;
  2467. size_t array_size = 0;
  2468. void __user *user_ptr = NULL;
  2469. void **kernel_ptr = NULL;
  2470. /* Copy arguments into temp kernel buffer */
  2471. if (_IOC_DIR(cmd) != _IOC_NONE) {
  2472. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  2473. parg = sbuf;
  2474. } else {
  2475. /* too big to allocate from stack */
  2476. mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  2477. if (NULL == mbuf)
  2478. return -ENOMEM;
  2479. parg = mbuf;
  2480. }
  2481. err = -EFAULT;
  2482. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  2483. unsigned int n = _IOC_SIZE(cmd);
  2484. /*
  2485. * In some cases, only a few fields are used as input,
  2486. * i.e. when the app sets "index" and then the driver
  2487. * fills in the rest of the structure for the thing
  2488. * with that index. We only need to copy up the first
  2489. * non-input field.
  2490. */
  2491. if (v4l2_is_known_ioctl(cmd)) {
  2492. u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
  2493. if (flags & INFO_FL_CLEAR_MASK)
  2494. n = (flags & INFO_FL_CLEAR_MASK) >> 16;
  2495. }
  2496. if (copy_from_user(parg, (void __user *)arg, n))
  2497. goto out;
  2498. /* zero out anything we don't copy from userspace */
  2499. if (n < _IOC_SIZE(cmd))
  2500. memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
  2501. } else {
  2502. /* read-only ioctl */
  2503. memset(parg, 0, _IOC_SIZE(cmd));
  2504. }
  2505. }
  2506. err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
  2507. if (err < 0)
  2508. goto out;
  2509. has_array_args = err;
  2510. if (has_array_args) {
  2511. /*
  2512. * When adding new types of array args, make sure that the
  2513. * parent argument to ioctl (which contains the pointer to the
  2514. * array) fits into sbuf (so that mbuf will still remain
  2515. * unused up to here).
  2516. */
  2517. mbuf = kmalloc(array_size, GFP_KERNEL);
  2518. err = -ENOMEM;
  2519. if (NULL == mbuf)
  2520. goto out_array_args;
  2521. err = -EFAULT;
  2522. if (copy_from_user(mbuf, user_ptr, array_size))
  2523. goto out_array_args;
  2524. *kernel_ptr = mbuf;
  2525. }
  2526. /* Handles IOCTL */
  2527. err = func(file, cmd, parg);
  2528. if (err == -ENOIOCTLCMD)
  2529. err = -ENOTTY;
  2530. if (err == 0) {
  2531. if (cmd == VIDIOC_DQBUF)
  2532. trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
  2533. else if (cmd == VIDIOC_QBUF)
  2534. trace_v4l2_qbuf(video_devdata(file)->minor, parg);
  2535. }
  2536. if (has_array_args) {
  2537. *kernel_ptr = (void __force *)user_ptr;
  2538. if (copy_to_user(user_ptr, mbuf, array_size))
  2539. err = -EFAULT;
  2540. goto out_array_args;
  2541. }
  2542. /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
  2543. results that must be returned. */
  2544. if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
  2545. goto out;
  2546. out_array_args:
  2547. /* Copy results into user buffer */
  2548. switch (_IOC_DIR(cmd)) {
  2549. case _IOC_READ:
  2550. case (_IOC_WRITE | _IOC_READ):
  2551. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  2552. err = -EFAULT;
  2553. break;
  2554. }
  2555. out:
  2556. kfree(mbuf);
  2557. return err;
  2558. }
  2559. EXPORT_SYMBOL(video_usercopy);
  2560. long video_ioctl2(struct file *file,
  2561. unsigned int cmd, unsigned long arg)
  2562. {
  2563. return video_usercopy(file, cmd, arg, __video_do_ioctl);
  2564. }
  2565. EXPORT_SYMBOL(video_ioctl2);