v4l2-ioctl.c 94 KB

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