v4l2-ioctl.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970
  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, p->stepwise.min_height,
  635. p->stepwise.step_width, p->stepwise.step_height,
  636. p->stepwise.max_width, p->stepwise.max_height);
  637. break;
  638. case V4L2_FRMSIZE_TYPE_CONTINUOUS:
  639. /* fall through */
  640. default:
  641. pr_cont("\n");
  642. break;
  643. }
  644. }
  645. static void v4l_print_frmivalenum(const void *arg, bool write_only)
  646. {
  647. const struct v4l2_frmivalenum *p = arg;
  648. pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
  649. p->index,
  650. (p->pixel_format & 0xff),
  651. (p->pixel_format >> 8) & 0xff,
  652. (p->pixel_format >> 16) & 0xff,
  653. (p->pixel_format >> 24) & 0xff,
  654. p->width, p->height, p->type);
  655. switch (p->type) {
  656. case V4L2_FRMIVAL_TYPE_DISCRETE:
  657. pr_cont(", fps=%d/%d\n",
  658. p->discrete.numerator,
  659. p->discrete.denominator);
  660. break;
  661. case V4L2_FRMIVAL_TYPE_STEPWISE:
  662. pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
  663. p->stepwise.min.numerator,
  664. p->stepwise.min.denominator,
  665. p->stepwise.max.numerator,
  666. p->stepwise.max.denominator,
  667. p->stepwise.step.numerator,
  668. p->stepwise.step.denominator);
  669. break;
  670. case V4L2_FRMIVAL_TYPE_CONTINUOUS:
  671. /* fall through */
  672. default:
  673. pr_cont("\n");
  674. break;
  675. }
  676. }
  677. static void v4l_print_event(const void *arg, bool write_only)
  678. {
  679. const struct v4l2_event *p = arg;
  680. const struct v4l2_event_ctrl *c;
  681. pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%lu.%9.9lu\n",
  682. p->type, p->pending, p->sequence, p->id,
  683. p->timestamp.tv_sec, p->timestamp.tv_nsec);
  684. switch (p->type) {
  685. case V4L2_EVENT_VSYNC:
  686. printk(KERN_DEBUG "field=%s\n",
  687. prt_names(p->u.vsync.field, v4l2_field_names));
  688. break;
  689. case V4L2_EVENT_CTRL:
  690. c = &p->u.ctrl;
  691. printk(KERN_DEBUG "changes=0x%x, type=%u, ",
  692. c->changes, c->type);
  693. if (c->type == V4L2_CTRL_TYPE_INTEGER64)
  694. pr_cont("value64=%lld, ", c->value64);
  695. else
  696. pr_cont("value=%d, ", c->value);
  697. pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
  698. c->flags, c->minimum, c->maximum,
  699. c->step, c->default_value);
  700. break;
  701. case V4L2_EVENT_FRAME_SYNC:
  702. pr_cont("frame_sequence=%u\n",
  703. p->u.frame_sync.frame_sequence);
  704. break;
  705. }
  706. }
  707. static void v4l_print_event_subscription(const void *arg, bool write_only)
  708. {
  709. const struct v4l2_event_subscription *p = arg;
  710. pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
  711. p->type, p->id, p->flags);
  712. }
  713. static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
  714. {
  715. const struct v4l2_sliced_vbi_cap *p = arg;
  716. int i;
  717. pr_cont("type=%s, service_set=0x%08x\n",
  718. prt_names(p->type, v4l2_type_names), p->service_set);
  719. for (i = 0; i < 24; i++)
  720. printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
  721. p->service_lines[0][i],
  722. p->service_lines[1][i]);
  723. }
  724. static void v4l_print_freq_band(const void *arg, bool write_only)
  725. {
  726. const struct v4l2_frequency_band *p = arg;
  727. pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
  728. p->tuner, p->type, p->index,
  729. p->capability, p->rangelow,
  730. p->rangehigh, p->modulation);
  731. }
  732. static void v4l_print_edid(const void *arg, bool write_only)
  733. {
  734. const struct v4l2_edid *p = arg;
  735. pr_cont("pad=%u, start_block=%u, blocks=%u\n",
  736. p->pad, p->start_block, p->blocks);
  737. }
  738. static void v4l_print_u32(const void *arg, bool write_only)
  739. {
  740. pr_cont("value=%u\n", *(const u32 *)arg);
  741. }
  742. static void v4l_print_newline(const void *arg, bool write_only)
  743. {
  744. pr_cont("\n");
  745. }
  746. static void v4l_print_default(const void *arg, bool write_only)
  747. {
  748. pr_cont("driver-specific ioctl\n");
  749. }
  750. static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
  751. {
  752. __u32 i;
  753. /* zero the reserved fields */
  754. c->reserved[0] = c->reserved[1] = 0;
  755. for (i = 0; i < c->count; i++)
  756. c->controls[i].reserved2[0] = 0;
  757. /* V4L2_CID_PRIVATE_BASE cannot be used as control class
  758. when using extended controls.
  759. Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
  760. is it allowed for backwards compatibility.
  761. */
  762. if (!allow_priv && c->which == V4L2_CID_PRIVATE_BASE)
  763. return 0;
  764. if (!c->which)
  765. return 1;
  766. /* Check that all controls are from the same control class. */
  767. for (i = 0; i < c->count; i++) {
  768. if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
  769. c->error_idx = i;
  770. return 0;
  771. }
  772. }
  773. return 1;
  774. }
  775. static int check_fmt(struct file *file, enum v4l2_buf_type type)
  776. {
  777. struct video_device *vfd = video_devdata(file);
  778. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  779. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  780. bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
  781. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  782. bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
  783. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  784. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  785. if (ops == NULL)
  786. return -EINVAL;
  787. switch (type) {
  788. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  789. if ((is_vid || is_tch) && is_rx &&
  790. (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
  791. return 0;
  792. break;
  793. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  794. if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
  795. return 0;
  796. break;
  797. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  798. if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
  799. return 0;
  800. break;
  801. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  802. if (is_vid && is_tx &&
  803. (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
  804. return 0;
  805. break;
  806. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  807. if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
  808. return 0;
  809. break;
  810. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  811. if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
  812. return 0;
  813. break;
  814. case V4L2_BUF_TYPE_VBI_CAPTURE:
  815. if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
  816. return 0;
  817. break;
  818. case V4L2_BUF_TYPE_VBI_OUTPUT:
  819. if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
  820. return 0;
  821. break;
  822. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  823. if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
  824. return 0;
  825. break;
  826. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  827. if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
  828. return 0;
  829. break;
  830. case V4L2_BUF_TYPE_SDR_CAPTURE:
  831. if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
  832. return 0;
  833. break;
  834. case V4L2_BUF_TYPE_SDR_OUTPUT:
  835. if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
  836. return 0;
  837. break;
  838. case V4L2_BUF_TYPE_META_CAPTURE:
  839. if (is_vid && is_rx && ops->vidioc_g_fmt_meta_cap)
  840. return 0;
  841. break;
  842. default:
  843. break;
  844. }
  845. return -EINVAL;
  846. }
  847. static void v4l_sanitize_format(struct v4l2_format *fmt)
  848. {
  849. unsigned int offset;
  850. /*
  851. * The v4l2_pix_format structure has been extended with fields that were
  852. * not previously required to be set to zero by applications. The priv
  853. * field, when set to a magic value, indicates the the extended fields
  854. * are valid. Otherwise they will contain undefined values. To simplify
  855. * the API towards drivers zero the extended fields and set the priv
  856. * field to the magic value when the extended pixel format structure
  857. * isn't used by applications.
  858. */
  859. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  860. fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  861. return;
  862. if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
  863. return;
  864. fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  865. offset = offsetof(struct v4l2_pix_format, priv)
  866. + sizeof(fmt->fmt.pix.priv);
  867. memset(((void *)&fmt->fmt.pix) + offset, 0,
  868. sizeof(fmt->fmt.pix) - offset);
  869. }
  870. static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
  871. struct file *file, void *fh, void *arg)
  872. {
  873. struct v4l2_capability *cap = (struct v4l2_capability *)arg;
  874. struct video_device *vfd = video_devdata(file);
  875. int ret;
  876. cap->version = LINUX_VERSION_CODE;
  877. cap->device_caps = vfd->device_caps;
  878. cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
  879. ret = ops->vidioc_querycap(file, fh, cap);
  880. cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
  881. /*
  882. * Drivers MUST fill in device_caps, so check for this and
  883. * warn if it was forgotten.
  884. */
  885. WARN(!(cap->capabilities & V4L2_CAP_DEVICE_CAPS) ||
  886. !cap->device_caps, "Bad caps for driver %s, %x %x",
  887. cap->driver, cap->capabilities, cap->device_caps);
  888. cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
  889. return ret;
  890. }
  891. static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
  892. struct file *file, void *fh, void *arg)
  893. {
  894. struct video_device *vfd = video_devdata(file);
  895. int ret;
  896. ret = v4l_enable_media_source(vfd);
  897. if (ret)
  898. return ret;
  899. return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
  900. }
  901. static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
  902. struct file *file, void *fh, void *arg)
  903. {
  904. return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
  905. }
  906. static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
  907. struct file *file, void *fh, void *arg)
  908. {
  909. struct video_device *vfd;
  910. u32 *p = arg;
  911. vfd = video_devdata(file);
  912. *p = v4l2_prio_max(vfd->prio);
  913. return 0;
  914. }
  915. static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
  916. struct file *file, void *fh, void *arg)
  917. {
  918. struct video_device *vfd;
  919. struct v4l2_fh *vfh;
  920. u32 *p = arg;
  921. vfd = video_devdata(file);
  922. if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
  923. return -ENOTTY;
  924. vfh = file->private_data;
  925. return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
  926. }
  927. static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
  928. struct file *file, void *fh, void *arg)
  929. {
  930. struct video_device *vfd = video_devdata(file);
  931. struct v4l2_input *p = arg;
  932. /*
  933. * We set the flags for CAP_DV_TIMINGS &
  934. * CAP_STD here based on ioctl handler provided by the
  935. * driver. If the driver doesn't support these
  936. * for a specific input, it must override these flags.
  937. */
  938. if (is_valid_ioctl(vfd, VIDIOC_S_STD))
  939. p->capabilities |= V4L2_IN_CAP_STD;
  940. return ops->vidioc_enum_input(file, fh, p);
  941. }
  942. static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
  943. struct file *file, void *fh, void *arg)
  944. {
  945. struct video_device *vfd = video_devdata(file);
  946. struct v4l2_output *p = arg;
  947. /*
  948. * We set the flags for CAP_DV_TIMINGS &
  949. * CAP_STD here based on ioctl handler provided by the
  950. * driver. If the driver doesn't support these
  951. * for a specific output, it must override these flags.
  952. */
  953. if (is_valid_ioctl(vfd, VIDIOC_S_STD))
  954. p->capabilities |= V4L2_OUT_CAP_STD;
  955. return ops->vidioc_enum_output(file, fh, p);
  956. }
  957. static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
  958. {
  959. const unsigned sz = sizeof(fmt->description);
  960. const char *descr = NULL;
  961. u32 flags = 0;
  962. /*
  963. * We depart from the normal coding style here since the descriptions
  964. * should be aligned so it is easy to see which descriptions will be
  965. * longer than 31 characters (the max length for a description).
  966. * And frankly, this is easier to read anyway.
  967. *
  968. * Note that gcc will use O(log N) comparisons to find the right case.
  969. */
  970. switch (fmt->pixelformat) {
  971. /* Max description length mask: descr = "0123456789012345678901234567890" */
  972. case V4L2_PIX_FMT_RGB332: descr = "8-bit RGB 3-3-2"; break;
  973. case V4L2_PIX_FMT_RGB444: descr = "16-bit A/XRGB 4-4-4-4"; break;
  974. case V4L2_PIX_FMT_ARGB444: descr = "16-bit ARGB 4-4-4-4"; break;
  975. case V4L2_PIX_FMT_XRGB444: descr = "16-bit XRGB 4-4-4-4"; break;
  976. case V4L2_PIX_FMT_RGB555: descr = "16-bit A/XRGB 1-5-5-5"; break;
  977. case V4L2_PIX_FMT_ARGB555: descr = "16-bit ARGB 1-5-5-5"; break;
  978. case V4L2_PIX_FMT_XRGB555: descr = "16-bit XRGB 1-5-5-5"; break;
  979. case V4L2_PIX_FMT_RGB565: descr = "16-bit RGB 5-6-5"; break;
  980. case V4L2_PIX_FMT_RGB555X: descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
  981. case V4L2_PIX_FMT_ARGB555X: descr = "16-bit ARGB 1-5-5-5 BE"; break;
  982. case V4L2_PIX_FMT_XRGB555X: descr = "16-bit XRGB 1-5-5-5 BE"; break;
  983. case V4L2_PIX_FMT_RGB565X: descr = "16-bit RGB 5-6-5 BE"; break;
  984. case V4L2_PIX_FMT_BGR666: descr = "18-bit BGRX 6-6-6-14"; break;
  985. case V4L2_PIX_FMT_BGR24: descr = "24-bit BGR 8-8-8"; break;
  986. case V4L2_PIX_FMT_RGB24: descr = "24-bit RGB 8-8-8"; break;
  987. case V4L2_PIX_FMT_BGR32: descr = "32-bit BGRA/X 8-8-8-8"; break;
  988. case V4L2_PIX_FMT_ABGR32: descr = "32-bit BGRA 8-8-8-8"; break;
  989. case V4L2_PIX_FMT_XBGR32: descr = "32-bit BGRX 8-8-8-8"; break;
  990. case V4L2_PIX_FMT_RGB32: descr = "32-bit A/XRGB 8-8-8-8"; break;
  991. case V4L2_PIX_FMT_ARGB32: descr = "32-bit ARGB 8-8-8-8"; break;
  992. case V4L2_PIX_FMT_XRGB32: descr = "32-bit XRGB 8-8-8-8"; break;
  993. case V4L2_PIX_FMT_GREY: descr = "8-bit Greyscale"; break;
  994. case V4L2_PIX_FMT_Y4: descr = "4-bit Greyscale"; break;
  995. case V4L2_PIX_FMT_Y6: descr = "6-bit Greyscale"; break;
  996. case V4L2_PIX_FMT_Y10: descr = "10-bit Greyscale"; break;
  997. case V4L2_PIX_FMT_Y12: descr = "12-bit Greyscale"; break;
  998. case V4L2_PIX_FMT_Y16: descr = "16-bit Greyscale"; break;
  999. case V4L2_PIX_FMT_Y16_BE: descr = "16-bit Greyscale BE"; break;
  1000. case V4L2_PIX_FMT_Y10BPACK: descr = "10-bit Greyscale (Packed)"; break;
  1001. case V4L2_PIX_FMT_Y8I: descr = "Interleaved 8-bit Greyscale"; break;
  1002. case V4L2_PIX_FMT_Y12I: descr = "Interleaved 12-bit Greyscale"; break;
  1003. case V4L2_PIX_FMT_Z16: descr = "16-bit Depth"; break;
  1004. case V4L2_PIX_FMT_INZI: descr = "Planar 10:16 Greyscale Depth"; break;
  1005. case V4L2_PIX_FMT_PAL8: descr = "8-bit Palette"; break;
  1006. case V4L2_PIX_FMT_UV8: descr = "8-bit Chrominance UV 4-4"; break;
  1007. case V4L2_PIX_FMT_YVU410: descr = "Planar YVU 4:1:0"; break;
  1008. case V4L2_PIX_FMT_YVU420: descr = "Planar YVU 4:2:0"; break;
  1009. case V4L2_PIX_FMT_YUYV: descr = "YUYV 4:2:2"; break;
  1010. case V4L2_PIX_FMT_YYUV: descr = "YYUV 4:2:2"; break;
  1011. case V4L2_PIX_FMT_YVYU: descr = "YVYU 4:2:2"; break;
  1012. case V4L2_PIX_FMT_UYVY: descr = "UYVY 4:2:2"; break;
  1013. case V4L2_PIX_FMT_VYUY: descr = "VYUY 4:2:2"; break;
  1014. case V4L2_PIX_FMT_YUV422P: descr = "Planar YUV 4:2:2"; break;
  1015. case V4L2_PIX_FMT_YUV411P: descr = "Planar YUV 4:1:1"; break;
  1016. case V4L2_PIX_FMT_Y41P: descr = "YUV 4:1:1 (Packed)"; break;
  1017. case V4L2_PIX_FMT_YUV444: descr = "16-bit A/XYUV 4-4-4-4"; break;
  1018. case V4L2_PIX_FMT_YUV555: descr = "16-bit A/XYUV 1-5-5-5"; break;
  1019. case V4L2_PIX_FMT_YUV565: descr = "16-bit YUV 5-6-5"; break;
  1020. case V4L2_PIX_FMT_YUV32: descr = "32-bit A/XYUV 8-8-8-8"; break;
  1021. case V4L2_PIX_FMT_YUV410: descr = "Planar YUV 4:1:0"; break;
  1022. case V4L2_PIX_FMT_YUV420: descr = "Planar YUV 4:2:0"; break;
  1023. case V4L2_PIX_FMT_HI240: descr = "8-bit Dithered RGB (BTTV)"; break;
  1024. case V4L2_PIX_FMT_HM12: descr = "YUV 4:2:0 (16x16 Macroblocks)"; break;
  1025. case V4L2_PIX_FMT_M420: descr = "YUV 4:2:0 (M420)"; break;
  1026. case V4L2_PIX_FMT_NV12: descr = "Y/CbCr 4:2:0"; break;
  1027. case V4L2_PIX_FMT_NV21: descr = "Y/CrCb 4:2:0"; break;
  1028. case V4L2_PIX_FMT_NV16: descr = "Y/CbCr 4:2:2"; break;
  1029. case V4L2_PIX_FMT_NV61: descr = "Y/CrCb 4:2:2"; break;
  1030. case V4L2_PIX_FMT_NV24: descr = "Y/CbCr 4:4:4"; break;
  1031. case V4L2_PIX_FMT_NV42: descr = "Y/CrCb 4:4:4"; break;
  1032. case V4L2_PIX_FMT_NV12M: descr = "Y/CbCr 4:2:0 (N-C)"; break;
  1033. case V4L2_PIX_FMT_NV21M: descr = "Y/CrCb 4:2:0 (N-C)"; break;
  1034. case V4L2_PIX_FMT_NV16M: descr = "Y/CbCr 4:2:2 (N-C)"; break;
  1035. case V4L2_PIX_FMT_NV61M: descr = "Y/CrCb 4:2:2 (N-C)"; break;
  1036. case V4L2_PIX_FMT_NV12MT: descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
  1037. case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
  1038. case V4L2_PIX_FMT_YUV420M: descr = "Planar YUV 4:2:0 (N-C)"; break;
  1039. case V4L2_PIX_FMT_YVU420M: descr = "Planar YVU 4:2:0 (N-C)"; break;
  1040. case V4L2_PIX_FMT_YUV422M: descr = "Planar YUV 4:2:2 (N-C)"; break;
  1041. case V4L2_PIX_FMT_YVU422M: descr = "Planar YVU 4:2:2 (N-C)"; break;
  1042. case V4L2_PIX_FMT_YUV444M: descr = "Planar YUV 4:4:4 (N-C)"; break;
  1043. case V4L2_PIX_FMT_YVU444M: descr = "Planar YVU 4:4:4 (N-C)"; break;
  1044. case V4L2_PIX_FMT_SBGGR8: descr = "8-bit Bayer BGBG/GRGR"; break;
  1045. case V4L2_PIX_FMT_SGBRG8: descr = "8-bit Bayer GBGB/RGRG"; break;
  1046. case V4L2_PIX_FMT_SGRBG8: descr = "8-bit Bayer GRGR/BGBG"; break;
  1047. case V4L2_PIX_FMT_SRGGB8: descr = "8-bit Bayer RGRG/GBGB"; break;
  1048. case V4L2_PIX_FMT_SBGGR10: descr = "10-bit Bayer BGBG/GRGR"; break;
  1049. case V4L2_PIX_FMT_SGBRG10: descr = "10-bit Bayer GBGB/RGRG"; break;
  1050. case V4L2_PIX_FMT_SGRBG10: descr = "10-bit Bayer GRGR/BGBG"; break;
  1051. case V4L2_PIX_FMT_SRGGB10: descr = "10-bit Bayer RGRG/GBGB"; break;
  1052. case V4L2_PIX_FMT_SBGGR10P: descr = "10-bit Bayer BGBG/GRGR Packed"; break;
  1053. case V4L2_PIX_FMT_SGBRG10P: descr = "10-bit Bayer GBGB/RGRG Packed"; break;
  1054. case V4L2_PIX_FMT_SGRBG10P: descr = "10-bit Bayer GRGR/BGBG Packed"; break;
  1055. case V4L2_PIX_FMT_SRGGB10P: descr = "10-bit Bayer RGRG/GBGB Packed"; break;
  1056. case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
  1057. case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
  1058. case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
  1059. case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
  1060. case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
  1061. case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
  1062. case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
  1063. case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
  1064. case V4L2_PIX_FMT_SBGGR12: descr = "12-bit Bayer BGBG/GRGR"; break;
  1065. case V4L2_PIX_FMT_SGBRG12: descr = "12-bit Bayer GBGB/RGRG"; break;
  1066. case V4L2_PIX_FMT_SGRBG12: descr = "12-bit Bayer GRGR/BGBG"; break;
  1067. case V4L2_PIX_FMT_SRGGB12: descr = "12-bit Bayer RGRG/GBGB"; break;
  1068. case V4L2_PIX_FMT_SBGGR12P: descr = "12-bit Bayer BGBG/GRGR Packed"; break;
  1069. case V4L2_PIX_FMT_SGBRG12P: descr = "12-bit Bayer GBGB/RGRG Packed"; break;
  1070. case V4L2_PIX_FMT_SGRBG12P: descr = "12-bit Bayer GRGR/BGBG Packed"; break;
  1071. case V4L2_PIX_FMT_SRGGB12P: descr = "12-bit Bayer RGRG/GBGB Packed"; break;
  1072. case V4L2_PIX_FMT_SBGGR16: descr = "16-bit Bayer BGBG/GRGR"; break;
  1073. case V4L2_PIX_FMT_SGBRG16: descr = "16-bit Bayer GBGB/RGRG"; break;
  1074. case V4L2_PIX_FMT_SGRBG16: descr = "16-bit Bayer GRGR/BGBG"; break;
  1075. case V4L2_PIX_FMT_SRGGB16: descr = "16-bit Bayer RGRG/GBGB"; break;
  1076. case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
  1077. case V4L2_PIX_FMT_SPCA501: descr = "GSPCA SPCA501"; break;
  1078. case V4L2_PIX_FMT_SPCA505: descr = "GSPCA SPCA505"; break;
  1079. case V4L2_PIX_FMT_SPCA508: descr = "GSPCA SPCA508"; break;
  1080. case V4L2_PIX_FMT_STV0680: descr = "GSPCA STV0680"; break;
  1081. case V4L2_PIX_FMT_TM6000: descr = "A/V + VBI Mux Packet"; break;
  1082. case V4L2_PIX_FMT_CIT_YYVYUY: descr = "GSPCA CIT YYVYUY"; break;
  1083. case V4L2_PIX_FMT_KONICA420: descr = "GSPCA KONICA420"; break;
  1084. case V4L2_PIX_FMT_HSV24: descr = "24-bit HSV 8-8-8"; break;
  1085. case V4L2_PIX_FMT_HSV32: descr = "32-bit XHSV 8-8-8-8"; break;
  1086. case V4L2_SDR_FMT_CU8: descr = "Complex U8"; break;
  1087. case V4L2_SDR_FMT_CU16LE: descr = "Complex U16LE"; break;
  1088. case V4L2_SDR_FMT_CS8: descr = "Complex S8"; break;
  1089. case V4L2_SDR_FMT_CS14LE: descr = "Complex S14LE"; break;
  1090. case V4L2_SDR_FMT_RU12LE: descr = "Real U12LE"; break;
  1091. case V4L2_SDR_FMT_PCU16BE: descr = "Planar Complex U16BE"; break;
  1092. case V4L2_SDR_FMT_PCU18BE: descr = "Planar Complex U18BE"; break;
  1093. case V4L2_SDR_FMT_PCU20BE: descr = "Planar Complex U20BE"; break;
  1094. case V4L2_TCH_FMT_DELTA_TD16: descr = "16-bit signed deltas"; break;
  1095. case V4L2_TCH_FMT_DELTA_TD08: descr = "8-bit signed deltas"; break;
  1096. case V4L2_TCH_FMT_TU16: descr = "16-bit unsigned touch data"; break;
  1097. case V4L2_TCH_FMT_TU08: descr = "8-bit unsigned touch data"; break;
  1098. case V4L2_META_FMT_VSP1_HGO: descr = "R-Car VSP1 1-D Histogram"; break;
  1099. case V4L2_META_FMT_VSP1_HGT: descr = "R-Car VSP1 2-D Histogram"; break;
  1100. default:
  1101. /* Compressed formats */
  1102. flags = V4L2_FMT_FLAG_COMPRESSED;
  1103. switch (fmt->pixelformat) {
  1104. /* Max description length mask: descr = "0123456789012345678901234567890" */
  1105. case V4L2_PIX_FMT_MJPEG: descr = "Motion-JPEG"; break;
  1106. case V4L2_PIX_FMT_JPEG: descr = "JFIF JPEG"; break;
  1107. case V4L2_PIX_FMT_DV: descr = "1394"; break;
  1108. case V4L2_PIX_FMT_MPEG: descr = "MPEG-1/2/4"; break;
  1109. case V4L2_PIX_FMT_H264: descr = "H.264"; break;
  1110. case V4L2_PIX_FMT_H264_NO_SC: descr = "H.264 (No Start Codes)"; break;
  1111. case V4L2_PIX_FMT_H264_MVC: descr = "H.264 MVC"; break;
  1112. case V4L2_PIX_FMT_H263: descr = "H.263"; break;
  1113. case V4L2_PIX_FMT_MPEG1: descr = "MPEG-1 ES"; break;
  1114. case V4L2_PIX_FMT_MPEG2: descr = "MPEG-2 ES"; break;
  1115. case V4L2_PIX_FMT_MPEG4: descr = "MPEG-4 part 2 ES"; break;
  1116. case V4L2_PIX_FMT_XVID: descr = "Xvid"; break;
  1117. case V4L2_PIX_FMT_VC1_ANNEX_G: descr = "VC-1 (SMPTE 412M Annex G)"; break;
  1118. case V4L2_PIX_FMT_VC1_ANNEX_L: descr = "VC-1 (SMPTE 412M Annex L)"; break;
  1119. case V4L2_PIX_FMT_VP8: descr = "VP8"; break;
  1120. case V4L2_PIX_FMT_VP9: descr = "VP9"; break;
  1121. case V4L2_PIX_FMT_CPIA1: descr = "GSPCA CPiA YUV"; break;
  1122. case V4L2_PIX_FMT_WNVA: descr = "WNVA"; break;
  1123. case V4L2_PIX_FMT_SN9C10X: descr = "GSPCA SN9C10X"; break;
  1124. case V4L2_PIX_FMT_PWC1: descr = "Raw Philips Webcam Type (Old)"; break;
  1125. case V4L2_PIX_FMT_PWC2: descr = "Raw Philips Webcam Type (New)"; break;
  1126. case V4L2_PIX_FMT_ET61X251: descr = "GSPCA ET61X251"; break;
  1127. case V4L2_PIX_FMT_SPCA561: descr = "GSPCA SPCA561"; break;
  1128. case V4L2_PIX_FMT_PAC207: descr = "GSPCA PAC207"; break;
  1129. case V4L2_PIX_FMT_MR97310A: descr = "GSPCA MR97310A"; break;
  1130. case V4L2_PIX_FMT_JL2005BCD: descr = "GSPCA JL2005BCD"; break;
  1131. case V4L2_PIX_FMT_SN9C2028: descr = "GSPCA SN9C2028"; break;
  1132. case V4L2_PIX_FMT_SQ905C: descr = "GSPCA SQ905C"; break;
  1133. case V4L2_PIX_FMT_PJPG: descr = "GSPCA PJPG"; break;
  1134. case V4L2_PIX_FMT_OV511: descr = "GSPCA OV511"; break;
  1135. case V4L2_PIX_FMT_OV518: descr = "GSPCA OV518"; break;
  1136. case V4L2_PIX_FMT_JPGL: descr = "JPEG Lite"; break;
  1137. case V4L2_PIX_FMT_SE401: descr = "GSPCA SE401"; break;
  1138. case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
  1139. case V4L2_PIX_FMT_MT21C: descr = "Mediatek Compressed Format"; break;
  1140. default:
  1141. WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
  1142. if (fmt->description[0])
  1143. return;
  1144. flags = 0;
  1145. snprintf(fmt->description, sz, "%c%c%c%c%s",
  1146. (char)(fmt->pixelformat & 0x7f),
  1147. (char)((fmt->pixelformat >> 8) & 0x7f),
  1148. (char)((fmt->pixelformat >> 16) & 0x7f),
  1149. (char)((fmt->pixelformat >> 24) & 0x7f),
  1150. (fmt->pixelformat & (1 << 31)) ? "-BE" : "");
  1151. break;
  1152. }
  1153. }
  1154. if (descr)
  1155. WARN_ON(strlcpy(fmt->description, descr, sz) >= sz);
  1156. fmt->flags = flags;
  1157. }
  1158. static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
  1159. struct file *file, void *fh, void *arg)
  1160. {
  1161. struct v4l2_fmtdesc *p = arg;
  1162. struct video_device *vfd = video_devdata(file);
  1163. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1164. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1165. bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
  1166. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1167. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1168. int ret = -EINVAL;
  1169. switch (p->type) {
  1170. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1171. if (unlikely(!is_rx || (!is_vid && !is_tch) || !ops->vidioc_enum_fmt_vid_cap))
  1172. break;
  1173. ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
  1174. break;
  1175. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1176. if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap_mplane))
  1177. break;
  1178. ret = ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
  1179. break;
  1180. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1181. if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_overlay))
  1182. break;
  1183. ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
  1184. break;
  1185. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1186. if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out))
  1187. break;
  1188. ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
  1189. break;
  1190. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1191. if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out_mplane))
  1192. break;
  1193. ret = ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
  1194. break;
  1195. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1196. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_enum_fmt_sdr_cap))
  1197. break;
  1198. ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
  1199. break;
  1200. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1201. if (unlikely(!is_tx || !is_sdr || !ops->vidioc_enum_fmt_sdr_out))
  1202. break;
  1203. ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
  1204. break;
  1205. case V4L2_BUF_TYPE_META_CAPTURE:
  1206. if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_meta_cap))
  1207. break;
  1208. ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
  1209. break;
  1210. }
  1211. if (ret == 0)
  1212. v4l_fill_fmtdesc(p);
  1213. return ret;
  1214. }
  1215. static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
  1216. struct file *file, void *fh, void *arg)
  1217. {
  1218. struct v4l2_format *p = arg;
  1219. struct video_device *vfd = video_devdata(file);
  1220. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1221. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1222. bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
  1223. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1224. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1225. int ret;
  1226. /*
  1227. * fmt can't be cleared for these overlay types due to the 'clips'
  1228. * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
  1229. * Those are provided by the user. So handle these two overlay types
  1230. * first, and then just do a simple memset for the other types.
  1231. */
  1232. switch (p->type) {
  1233. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1234. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
  1235. struct v4l2_clip __user *clips = p->fmt.win.clips;
  1236. u32 clipcount = p->fmt.win.clipcount;
  1237. void __user *bitmap = p->fmt.win.bitmap;
  1238. memset(&p->fmt, 0, sizeof(p->fmt));
  1239. p->fmt.win.clips = clips;
  1240. p->fmt.win.clipcount = clipcount;
  1241. p->fmt.win.bitmap = bitmap;
  1242. break;
  1243. }
  1244. default:
  1245. memset(&p->fmt, 0, sizeof(p->fmt));
  1246. break;
  1247. }
  1248. switch (p->type) {
  1249. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1250. if (unlikely(!is_rx || (!is_vid && !is_tch) || !ops->vidioc_g_fmt_vid_cap))
  1251. break;
  1252. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1253. ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
  1254. /* just in case the driver zeroed it again */
  1255. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1256. return ret;
  1257. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1258. if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
  1259. break;
  1260. return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
  1261. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1262. if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
  1263. break;
  1264. return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
  1265. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1266. if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
  1267. break;
  1268. return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
  1269. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1270. if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
  1271. break;
  1272. return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
  1273. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1274. if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
  1275. break;
  1276. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1277. ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
  1278. /* just in case the driver zeroed it again */
  1279. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1280. return ret;
  1281. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1282. if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
  1283. break;
  1284. return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
  1285. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1286. if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
  1287. break;
  1288. return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
  1289. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1290. if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
  1291. break;
  1292. return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
  1293. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1294. if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
  1295. break;
  1296. return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
  1297. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1298. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_g_fmt_sdr_cap))
  1299. break;
  1300. return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
  1301. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1302. if (unlikely(!is_tx || !is_sdr || !ops->vidioc_g_fmt_sdr_out))
  1303. break;
  1304. return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
  1305. case V4L2_BUF_TYPE_META_CAPTURE:
  1306. if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_meta_cap))
  1307. break;
  1308. return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
  1309. }
  1310. return -EINVAL;
  1311. }
  1312. static void v4l_pix_format_touch(struct v4l2_pix_format *p)
  1313. {
  1314. /*
  1315. * The v4l2_pix_format structure contains fields that make no sense for
  1316. * touch. Set them to default values in this case.
  1317. */
  1318. p->field = V4L2_FIELD_NONE;
  1319. p->colorspace = V4L2_COLORSPACE_RAW;
  1320. p->flags = 0;
  1321. p->ycbcr_enc = 0;
  1322. p->quantization = 0;
  1323. p->xfer_func = 0;
  1324. }
  1325. static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
  1326. struct file *file, void *fh, void *arg)
  1327. {
  1328. struct v4l2_format *p = arg;
  1329. struct video_device *vfd = video_devdata(file);
  1330. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1331. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1332. bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
  1333. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1334. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1335. int ret;
  1336. ret = v4l_enable_media_source(vfd);
  1337. if (ret)
  1338. return ret;
  1339. v4l_sanitize_format(p);
  1340. switch (p->type) {
  1341. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1342. if (unlikely(!is_rx || (!is_vid && !is_tch) || !ops->vidioc_s_fmt_vid_cap))
  1343. break;
  1344. CLEAR_AFTER_FIELD(p, fmt.pix);
  1345. ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
  1346. /* just in case the driver zeroed it again */
  1347. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1348. if (is_tch)
  1349. v4l_pix_format_touch(&p->fmt.pix);
  1350. return ret;
  1351. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1352. if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
  1353. break;
  1354. CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
  1355. return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
  1356. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1357. if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
  1358. break;
  1359. CLEAR_AFTER_FIELD(p, fmt.win);
  1360. return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
  1361. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1362. if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
  1363. break;
  1364. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1365. return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
  1366. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1367. if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
  1368. break;
  1369. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1370. return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
  1371. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1372. if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
  1373. break;
  1374. CLEAR_AFTER_FIELD(p, fmt.pix);
  1375. ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
  1376. /* just in case the driver zeroed it again */
  1377. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1378. return ret;
  1379. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1380. if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
  1381. break;
  1382. CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
  1383. return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
  1384. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1385. if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
  1386. break;
  1387. CLEAR_AFTER_FIELD(p, fmt.win);
  1388. return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
  1389. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1390. if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
  1391. break;
  1392. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1393. return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
  1394. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1395. if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
  1396. break;
  1397. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1398. return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
  1399. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1400. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_s_fmt_sdr_cap))
  1401. break;
  1402. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1403. return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
  1404. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1405. if (unlikely(!is_tx || !is_sdr || !ops->vidioc_s_fmt_sdr_out))
  1406. break;
  1407. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1408. return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
  1409. case V4L2_BUF_TYPE_META_CAPTURE:
  1410. if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_meta_cap))
  1411. break;
  1412. CLEAR_AFTER_FIELD(p, fmt.meta);
  1413. return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
  1414. }
  1415. return -EINVAL;
  1416. }
  1417. static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
  1418. struct file *file, void *fh, void *arg)
  1419. {
  1420. struct v4l2_format *p = arg;
  1421. struct video_device *vfd = video_devdata(file);
  1422. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1423. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1424. bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
  1425. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1426. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1427. int ret;
  1428. v4l_sanitize_format(p);
  1429. switch (p->type) {
  1430. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1431. if (unlikely(!is_rx || (!is_vid && !is_tch) || !ops->vidioc_try_fmt_vid_cap))
  1432. break;
  1433. CLEAR_AFTER_FIELD(p, fmt.pix);
  1434. ret = ops->vidioc_try_fmt_vid_cap(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_CAPTURE_MPLANE:
  1439. if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
  1440. break;
  1441. CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
  1442. return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
  1443. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1444. if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
  1445. break;
  1446. CLEAR_AFTER_FIELD(p, fmt.win);
  1447. return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
  1448. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1449. if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
  1450. break;
  1451. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1452. return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
  1453. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1454. if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
  1455. break;
  1456. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1457. return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
  1458. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1459. if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
  1460. break;
  1461. CLEAR_AFTER_FIELD(p, fmt.pix);
  1462. ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
  1463. /* just in case the driver zeroed it again */
  1464. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1465. return ret;
  1466. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1467. if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
  1468. break;
  1469. CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
  1470. return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
  1471. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1472. if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
  1473. break;
  1474. CLEAR_AFTER_FIELD(p, fmt.win);
  1475. return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
  1476. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1477. if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
  1478. break;
  1479. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1480. return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
  1481. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1482. if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
  1483. break;
  1484. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1485. return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
  1486. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1487. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_try_fmt_sdr_cap))
  1488. break;
  1489. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1490. return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
  1491. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1492. if (unlikely(!is_tx || !is_sdr || !ops->vidioc_try_fmt_sdr_out))
  1493. break;
  1494. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1495. return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
  1496. case V4L2_BUF_TYPE_META_CAPTURE:
  1497. if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_meta_cap))
  1498. break;
  1499. CLEAR_AFTER_FIELD(p, fmt.meta);
  1500. return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
  1501. }
  1502. return -EINVAL;
  1503. }
  1504. static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
  1505. struct file *file, void *fh, void *arg)
  1506. {
  1507. return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
  1508. }
  1509. static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
  1510. struct file *file, void *fh, void *arg)
  1511. {
  1512. return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
  1513. }
  1514. static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
  1515. struct file *file, void *fh, void *arg)
  1516. {
  1517. struct video_device *vfd = video_devdata(file);
  1518. struct v4l2_tuner *p = arg;
  1519. int err;
  1520. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1521. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1522. err = ops->vidioc_g_tuner(file, fh, p);
  1523. if (!err)
  1524. p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
  1525. return err;
  1526. }
  1527. static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
  1528. struct file *file, void *fh, void *arg)
  1529. {
  1530. struct video_device *vfd = video_devdata(file);
  1531. struct v4l2_tuner *p = arg;
  1532. int ret;
  1533. ret = v4l_enable_media_source(vfd);
  1534. if (ret)
  1535. return ret;
  1536. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1537. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1538. return ops->vidioc_s_tuner(file, fh, p);
  1539. }
  1540. static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
  1541. struct file *file, void *fh, void *arg)
  1542. {
  1543. struct video_device *vfd = video_devdata(file);
  1544. struct v4l2_modulator *p = arg;
  1545. int err;
  1546. if (vfd->vfl_type == VFL_TYPE_RADIO)
  1547. p->type = V4L2_TUNER_RADIO;
  1548. err = ops->vidioc_g_modulator(file, fh, p);
  1549. if (!err)
  1550. p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
  1551. return err;
  1552. }
  1553. static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
  1554. struct file *file, void *fh, void *arg)
  1555. {
  1556. struct video_device *vfd = video_devdata(file);
  1557. struct v4l2_modulator *p = arg;
  1558. if (vfd->vfl_type == VFL_TYPE_RADIO)
  1559. p->type = V4L2_TUNER_RADIO;
  1560. return ops->vidioc_s_modulator(file, fh, p);
  1561. }
  1562. static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
  1563. struct file *file, void *fh, void *arg)
  1564. {
  1565. struct video_device *vfd = video_devdata(file);
  1566. struct v4l2_frequency *p = arg;
  1567. if (vfd->vfl_type == VFL_TYPE_SDR)
  1568. p->type = V4L2_TUNER_SDR;
  1569. else
  1570. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1571. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1572. return ops->vidioc_g_frequency(file, fh, p);
  1573. }
  1574. static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
  1575. struct file *file, void *fh, void *arg)
  1576. {
  1577. struct video_device *vfd = video_devdata(file);
  1578. const struct v4l2_frequency *p = arg;
  1579. enum v4l2_tuner_type type;
  1580. int ret;
  1581. ret = v4l_enable_media_source(vfd);
  1582. if (ret)
  1583. return ret;
  1584. if (vfd->vfl_type == VFL_TYPE_SDR) {
  1585. if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
  1586. return -EINVAL;
  1587. } else {
  1588. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1589. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1590. if (type != p->type)
  1591. return -EINVAL;
  1592. }
  1593. return ops->vidioc_s_frequency(file, fh, p);
  1594. }
  1595. static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
  1596. struct file *file, void *fh, void *arg)
  1597. {
  1598. struct video_device *vfd = video_devdata(file);
  1599. struct v4l2_standard *p = arg;
  1600. v4l2_std_id id = vfd->tvnorms, curr_id = 0;
  1601. unsigned int index = p->index, i, j = 0;
  1602. const char *descr = "";
  1603. /* Return -ENODATA if the tvnorms for the current input
  1604. or output is 0, meaning that it doesn't support this API. */
  1605. if (id == 0)
  1606. return -ENODATA;
  1607. /* Return norm array in a canonical way */
  1608. for (i = 0; i <= index && id; i++) {
  1609. /* last std value in the standards array is 0, so this
  1610. while always ends there since (id & 0) == 0. */
  1611. while ((id & standards[j].std) != standards[j].std)
  1612. j++;
  1613. curr_id = standards[j].std;
  1614. descr = standards[j].descr;
  1615. j++;
  1616. if (curr_id == 0)
  1617. break;
  1618. if (curr_id != V4L2_STD_PAL &&
  1619. curr_id != V4L2_STD_SECAM &&
  1620. curr_id != V4L2_STD_NTSC)
  1621. id &= ~curr_id;
  1622. }
  1623. if (i <= index)
  1624. return -EINVAL;
  1625. v4l2_video_std_construct(p, curr_id, descr);
  1626. return 0;
  1627. }
  1628. static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
  1629. struct file *file, void *fh, void *arg)
  1630. {
  1631. struct video_device *vfd = video_devdata(file);
  1632. v4l2_std_id id = *(v4l2_std_id *)arg, norm;
  1633. int ret;
  1634. ret = v4l_enable_media_source(vfd);
  1635. if (ret)
  1636. return ret;
  1637. norm = id & vfd->tvnorms;
  1638. if (vfd->tvnorms && !norm) /* Check if std is supported */
  1639. return -EINVAL;
  1640. /* Calls the specific handler */
  1641. return ops->vidioc_s_std(file, fh, norm);
  1642. }
  1643. static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
  1644. struct file *file, void *fh, void *arg)
  1645. {
  1646. struct video_device *vfd = video_devdata(file);
  1647. v4l2_std_id *p = arg;
  1648. int ret;
  1649. ret = v4l_enable_media_source(vfd);
  1650. if (ret)
  1651. return ret;
  1652. /*
  1653. * If no signal is detected, then the driver should return
  1654. * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
  1655. * any standards that do not apply removed.
  1656. *
  1657. * This means that tuners, audio and video decoders can join
  1658. * their efforts to improve the standards detection.
  1659. */
  1660. *p = vfd->tvnorms;
  1661. return ops->vidioc_querystd(file, fh, arg);
  1662. }
  1663. static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
  1664. struct file *file, void *fh, void *arg)
  1665. {
  1666. struct video_device *vfd = video_devdata(file);
  1667. struct v4l2_hw_freq_seek *p = arg;
  1668. enum v4l2_tuner_type type;
  1669. int ret;
  1670. ret = v4l_enable_media_source(vfd);
  1671. if (ret)
  1672. return ret;
  1673. /* s_hw_freq_seek is not supported for SDR for now */
  1674. if (vfd->vfl_type == VFL_TYPE_SDR)
  1675. return -EINVAL;
  1676. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1677. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1678. if (p->type != type)
  1679. return -EINVAL;
  1680. return ops->vidioc_s_hw_freq_seek(file, fh, p);
  1681. }
  1682. static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
  1683. struct file *file, void *fh, void *arg)
  1684. {
  1685. return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
  1686. }
  1687. static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
  1688. struct file *file, void *fh, void *arg)
  1689. {
  1690. struct v4l2_requestbuffers *p = arg;
  1691. int ret = check_fmt(file, p->type);
  1692. if (ret)
  1693. return ret;
  1694. CLEAR_AFTER_FIELD(p, memory);
  1695. return ops->vidioc_reqbufs(file, fh, p);
  1696. }
  1697. static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
  1698. struct file *file, void *fh, void *arg)
  1699. {
  1700. struct v4l2_buffer *p = arg;
  1701. int ret = check_fmt(file, p->type);
  1702. return ret ? ret : ops->vidioc_querybuf(file, fh, p);
  1703. }
  1704. static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
  1705. struct file *file, void *fh, void *arg)
  1706. {
  1707. struct v4l2_buffer *p = arg;
  1708. int ret = check_fmt(file, p->type);
  1709. return ret ? ret : ops->vidioc_qbuf(file, fh, p);
  1710. }
  1711. static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
  1712. struct file *file, void *fh, void *arg)
  1713. {
  1714. struct v4l2_buffer *p = arg;
  1715. int ret = check_fmt(file, p->type);
  1716. return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
  1717. }
  1718. static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
  1719. struct file *file, void *fh, void *arg)
  1720. {
  1721. struct v4l2_create_buffers *create = arg;
  1722. int ret = check_fmt(file, create->format.type);
  1723. if (ret)
  1724. return ret;
  1725. CLEAR_AFTER_FIELD(create, format);
  1726. v4l_sanitize_format(&create->format);
  1727. ret = ops->vidioc_create_bufs(file, fh, create);
  1728. if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1729. create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1730. create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1731. return ret;
  1732. }
  1733. static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
  1734. struct file *file, void *fh, void *arg)
  1735. {
  1736. struct v4l2_buffer *b = arg;
  1737. int ret = check_fmt(file, b->type);
  1738. return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
  1739. }
  1740. static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
  1741. struct file *file, void *fh, void *arg)
  1742. {
  1743. struct v4l2_streamparm *p = arg;
  1744. v4l2_std_id std;
  1745. int ret = check_fmt(file, p->type);
  1746. if (ret)
  1747. return ret;
  1748. if (ops->vidioc_g_parm)
  1749. return ops->vidioc_g_parm(file, fh, p);
  1750. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1751. p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1752. return -EINVAL;
  1753. p->parm.capture.readbuffers = 2;
  1754. ret = ops->vidioc_g_std(file, fh, &std);
  1755. if (ret == 0)
  1756. v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
  1757. return ret;
  1758. }
  1759. static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
  1760. struct file *file, void *fh, void *arg)
  1761. {
  1762. struct v4l2_streamparm *p = arg;
  1763. int ret = check_fmt(file, p->type);
  1764. return ret ? ret : ops->vidioc_s_parm(file, fh, p);
  1765. }
  1766. static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
  1767. struct file *file, void *fh, void *arg)
  1768. {
  1769. struct video_device *vfd = video_devdata(file);
  1770. struct v4l2_queryctrl *p = arg;
  1771. struct v4l2_fh *vfh =
  1772. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1773. if (vfh && vfh->ctrl_handler)
  1774. return v4l2_queryctrl(vfh->ctrl_handler, p);
  1775. if (vfd->ctrl_handler)
  1776. return v4l2_queryctrl(vfd->ctrl_handler, p);
  1777. if (ops->vidioc_queryctrl)
  1778. return ops->vidioc_queryctrl(file, fh, p);
  1779. return -ENOTTY;
  1780. }
  1781. static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
  1782. struct file *file, void *fh, void *arg)
  1783. {
  1784. struct video_device *vfd = video_devdata(file);
  1785. struct v4l2_query_ext_ctrl *p = arg;
  1786. struct v4l2_fh *vfh =
  1787. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1788. if (vfh && vfh->ctrl_handler)
  1789. return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
  1790. if (vfd->ctrl_handler)
  1791. return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
  1792. if (ops->vidioc_query_ext_ctrl)
  1793. return ops->vidioc_query_ext_ctrl(file, fh, p);
  1794. return -ENOTTY;
  1795. }
  1796. static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
  1797. struct file *file, void *fh, void *arg)
  1798. {
  1799. struct video_device *vfd = video_devdata(file);
  1800. struct v4l2_querymenu *p = arg;
  1801. struct v4l2_fh *vfh =
  1802. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1803. if (vfh && vfh->ctrl_handler)
  1804. return v4l2_querymenu(vfh->ctrl_handler, p);
  1805. if (vfd->ctrl_handler)
  1806. return v4l2_querymenu(vfd->ctrl_handler, p);
  1807. if (ops->vidioc_querymenu)
  1808. return ops->vidioc_querymenu(file, fh, p);
  1809. return -ENOTTY;
  1810. }
  1811. static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
  1812. struct file *file, void *fh, void *arg)
  1813. {
  1814. struct video_device *vfd = video_devdata(file);
  1815. struct v4l2_control *p = arg;
  1816. struct v4l2_fh *vfh =
  1817. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1818. struct v4l2_ext_controls ctrls;
  1819. struct v4l2_ext_control ctrl;
  1820. if (vfh && vfh->ctrl_handler)
  1821. return v4l2_g_ctrl(vfh->ctrl_handler, p);
  1822. if (vfd->ctrl_handler)
  1823. return v4l2_g_ctrl(vfd->ctrl_handler, p);
  1824. if (ops->vidioc_g_ctrl)
  1825. return ops->vidioc_g_ctrl(file, fh, p);
  1826. if (ops->vidioc_g_ext_ctrls == NULL)
  1827. return -ENOTTY;
  1828. ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
  1829. ctrls.count = 1;
  1830. ctrls.controls = &ctrl;
  1831. ctrl.id = p->id;
  1832. ctrl.value = p->value;
  1833. if (check_ext_ctrls(&ctrls, 1)) {
  1834. int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
  1835. if (ret == 0)
  1836. p->value = ctrl.value;
  1837. return ret;
  1838. }
  1839. return -EINVAL;
  1840. }
  1841. static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
  1842. struct file *file, void *fh, void *arg)
  1843. {
  1844. struct video_device *vfd = video_devdata(file);
  1845. struct v4l2_control *p = arg;
  1846. struct v4l2_fh *vfh =
  1847. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1848. struct v4l2_ext_controls ctrls;
  1849. struct v4l2_ext_control ctrl;
  1850. if (vfh && vfh->ctrl_handler)
  1851. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
  1852. if (vfd->ctrl_handler)
  1853. return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
  1854. if (ops->vidioc_s_ctrl)
  1855. return ops->vidioc_s_ctrl(file, fh, p);
  1856. if (ops->vidioc_s_ext_ctrls == NULL)
  1857. return -ENOTTY;
  1858. ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
  1859. ctrls.count = 1;
  1860. ctrls.controls = &ctrl;
  1861. ctrl.id = p->id;
  1862. ctrl.value = p->value;
  1863. if (check_ext_ctrls(&ctrls, 1))
  1864. return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
  1865. return -EINVAL;
  1866. }
  1867. static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1868. struct file *file, void *fh, void *arg)
  1869. {
  1870. struct video_device *vfd = video_devdata(file);
  1871. struct v4l2_ext_controls *p = arg;
  1872. struct v4l2_fh *vfh =
  1873. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1874. p->error_idx = p->count;
  1875. if (vfh && vfh->ctrl_handler)
  1876. return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
  1877. if (vfd->ctrl_handler)
  1878. return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
  1879. if (ops->vidioc_g_ext_ctrls == NULL)
  1880. return -ENOTTY;
  1881. return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
  1882. -EINVAL;
  1883. }
  1884. static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1885. struct file *file, void *fh, void *arg)
  1886. {
  1887. struct video_device *vfd = video_devdata(file);
  1888. struct v4l2_ext_controls *p = arg;
  1889. struct v4l2_fh *vfh =
  1890. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1891. p->error_idx = p->count;
  1892. if (vfh && vfh->ctrl_handler)
  1893. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
  1894. if (vfd->ctrl_handler)
  1895. return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
  1896. if (ops->vidioc_s_ext_ctrls == NULL)
  1897. return -ENOTTY;
  1898. return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
  1899. -EINVAL;
  1900. }
  1901. static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1902. struct file *file, void *fh, void *arg)
  1903. {
  1904. struct video_device *vfd = video_devdata(file);
  1905. struct v4l2_ext_controls *p = arg;
  1906. struct v4l2_fh *vfh =
  1907. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1908. p->error_idx = p->count;
  1909. if (vfh && vfh->ctrl_handler)
  1910. return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
  1911. if (vfd->ctrl_handler)
  1912. return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
  1913. if (ops->vidioc_try_ext_ctrls == NULL)
  1914. return -ENOTTY;
  1915. return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
  1916. -EINVAL;
  1917. }
  1918. /*
  1919. * The selection API specified originally that the _MPLANE buffer types
  1920. * shouldn't be used. The reasons for this are lost in the mists of time
  1921. * (or just really crappy memories). Regardless, this is really annoying
  1922. * for userspace. So to keep things simple we map _MPLANE buffer types
  1923. * to their 'regular' counterparts before calling the driver. And we
  1924. * restore it afterwards. This way applications can use either buffer
  1925. * type and drivers don't need to check for both.
  1926. */
  1927. static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
  1928. struct file *file, void *fh, void *arg)
  1929. {
  1930. struct v4l2_selection *p = arg;
  1931. u32 old_type = p->type;
  1932. int ret;
  1933. if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1934. p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1935. else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1936. p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1937. ret = ops->vidioc_g_selection(file, fh, p);
  1938. p->type = old_type;
  1939. return ret;
  1940. }
  1941. static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
  1942. struct file *file, void *fh, void *arg)
  1943. {
  1944. struct v4l2_selection *p = arg;
  1945. u32 old_type = p->type;
  1946. int ret;
  1947. if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1948. p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1949. else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1950. p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1951. ret = ops->vidioc_s_selection(file, fh, p);
  1952. p->type = old_type;
  1953. return ret;
  1954. }
  1955. static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
  1956. struct file *file, void *fh, void *arg)
  1957. {
  1958. struct v4l2_crop *p = arg;
  1959. struct v4l2_selection s = {
  1960. .type = p->type,
  1961. };
  1962. int ret;
  1963. if (ops->vidioc_g_crop)
  1964. return ops->vidioc_g_crop(file, fh, p);
  1965. /* simulate capture crop using selection api */
  1966. /* crop means compose for output devices */
  1967. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1968. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1969. else
  1970. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1971. ret = v4l_g_selection(ops, file, fh, &s);
  1972. /* copying results to old structure on success */
  1973. if (!ret)
  1974. p->c = s.r;
  1975. return ret;
  1976. }
  1977. static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
  1978. struct file *file, void *fh, void *arg)
  1979. {
  1980. struct v4l2_crop *p = arg;
  1981. struct v4l2_selection s = {
  1982. .type = p->type,
  1983. .r = p->c,
  1984. };
  1985. if (ops->vidioc_s_crop)
  1986. return ops->vidioc_s_crop(file, fh, p);
  1987. /* simulate capture crop using selection api */
  1988. /* crop means compose for output devices */
  1989. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1990. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1991. else
  1992. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1993. return v4l_s_selection(ops, file, fh, &s);
  1994. }
  1995. static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
  1996. struct file *file, void *fh, void *arg)
  1997. {
  1998. struct v4l2_cropcap *p = arg;
  1999. struct v4l2_selection s = { .type = p->type };
  2000. int ret = 0;
  2001. /* setting trivial pixelaspect */
  2002. p->pixelaspect.numerator = 1;
  2003. p->pixelaspect.denominator = 1;
  2004. /*
  2005. * The determine_valid_ioctls() call already should ensure
  2006. * that this can never happen, but just in case...
  2007. */
  2008. if (WARN_ON(!ops->vidioc_cropcap && !ops->vidioc_g_selection))
  2009. return -ENOTTY;
  2010. if (ops->vidioc_cropcap)
  2011. ret = ops->vidioc_cropcap(file, fh, p);
  2012. if (!ops->vidioc_g_selection)
  2013. return ret;
  2014. /*
  2015. * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
  2016. * square pixel aspect ratio in that case.
  2017. */
  2018. if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
  2019. return ret;
  2020. /* Use g_selection() to fill in the bounds and defrect rectangles */
  2021. /* obtaining bounds */
  2022. if (V4L2_TYPE_IS_OUTPUT(p->type))
  2023. s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
  2024. else
  2025. s.target = V4L2_SEL_TGT_CROP_BOUNDS;
  2026. ret = v4l_g_selection(ops, file, fh, &s);
  2027. if (ret)
  2028. return ret;
  2029. p->bounds = s.r;
  2030. /* obtaining defrect */
  2031. if (V4L2_TYPE_IS_OUTPUT(p->type))
  2032. s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
  2033. else
  2034. s.target = V4L2_SEL_TGT_CROP_DEFAULT;
  2035. ret = v4l_g_selection(ops, file, fh, &s);
  2036. if (ret)
  2037. return ret;
  2038. p->defrect = s.r;
  2039. return 0;
  2040. }
  2041. static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
  2042. struct file *file, void *fh, void *arg)
  2043. {
  2044. struct video_device *vfd = video_devdata(file);
  2045. int ret;
  2046. if (vfd->v4l2_dev)
  2047. pr_info("%s: ================= START STATUS =================\n",
  2048. vfd->v4l2_dev->name);
  2049. ret = ops->vidioc_log_status(file, fh);
  2050. if (vfd->v4l2_dev)
  2051. pr_info("%s: ================== END STATUS ==================\n",
  2052. vfd->v4l2_dev->name);
  2053. return ret;
  2054. }
  2055. static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
  2056. struct file *file, void *fh, void *arg)
  2057. {
  2058. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2059. struct v4l2_dbg_register *p = arg;
  2060. struct video_device *vfd = video_devdata(file);
  2061. struct v4l2_subdev *sd;
  2062. int idx = 0;
  2063. if (!capable(CAP_SYS_ADMIN))
  2064. return -EPERM;
  2065. if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
  2066. if (vfd->v4l2_dev == NULL)
  2067. return -EINVAL;
  2068. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
  2069. if (p->match.addr == idx++)
  2070. return v4l2_subdev_call(sd, core, g_register, p);
  2071. return -EINVAL;
  2072. }
  2073. if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
  2074. (ops->vidioc_g_chip_info || p->match.addr == 0))
  2075. return ops->vidioc_g_register(file, fh, p);
  2076. return -EINVAL;
  2077. #else
  2078. return -ENOTTY;
  2079. #endif
  2080. }
  2081. static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
  2082. struct file *file, void *fh, void *arg)
  2083. {
  2084. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2085. const struct v4l2_dbg_register *p = arg;
  2086. struct video_device *vfd = video_devdata(file);
  2087. struct v4l2_subdev *sd;
  2088. int idx = 0;
  2089. if (!capable(CAP_SYS_ADMIN))
  2090. return -EPERM;
  2091. if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
  2092. if (vfd->v4l2_dev == NULL)
  2093. return -EINVAL;
  2094. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
  2095. if (p->match.addr == idx++)
  2096. return v4l2_subdev_call(sd, core, s_register, p);
  2097. return -EINVAL;
  2098. }
  2099. if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
  2100. (ops->vidioc_g_chip_info || p->match.addr == 0))
  2101. return ops->vidioc_s_register(file, fh, p);
  2102. return -EINVAL;
  2103. #else
  2104. return -ENOTTY;
  2105. #endif
  2106. }
  2107. static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
  2108. struct file *file, void *fh, void *arg)
  2109. {
  2110. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2111. struct video_device *vfd = video_devdata(file);
  2112. struct v4l2_dbg_chip_info *p = arg;
  2113. struct v4l2_subdev *sd;
  2114. int idx = 0;
  2115. switch (p->match.type) {
  2116. case V4L2_CHIP_MATCH_BRIDGE:
  2117. if (ops->vidioc_s_register)
  2118. p->flags |= V4L2_CHIP_FL_WRITABLE;
  2119. if (ops->vidioc_g_register)
  2120. p->flags |= V4L2_CHIP_FL_READABLE;
  2121. strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
  2122. if (ops->vidioc_g_chip_info)
  2123. return ops->vidioc_g_chip_info(file, fh, arg);
  2124. if (p->match.addr)
  2125. return -EINVAL;
  2126. return 0;
  2127. case V4L2_CHIP_MATCH_SUBDEV:
  2128. if (vfd->v4l2_dev == NULL)
  2129. break;
  2130. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
  2131. if (p->match.addr != idx++)
  2132. continue;
  2133. if (sd->ops->core && sd->ops->core->s_register)
  2134. p->flags |= V4L2_CHIP_FL_WRITABLE;
  2135. if (sd->ops->core && sd->ops->core->g_register)
  2136. p->flags |= V4L2_CHIP_FL_READABLE;
  2137. strlcpy(p->name, sd->name, sizeof(p->name));
  2138. return 0;
  2139. }
  2140. break;
  2141. }
  2142. return -EINVAL;
  2143. #else
  2144. return -ENOTTY;
  2145. #endif
  2146. }
  2147. static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
  2148. struct file *file, void *fh, void *arg)
  2149. {
  2150. return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
  2151. }
  2152. static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
  2153. struct file *file, void *fh, void *arg)
  2154. {
  2155. return ops->vidioc_subscribe_event(fh, arg);
  2156. }
  2157. static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
  2158. struct file *file, void *fh, void *arg)
  2159. {
  2160. return ops->vidioc_unsubscribe_event(fh, arg);
  2161. }
  2162. static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
  2163. struct file *file, void *fh, void *arg)
  2164. {
  2165. struct v4l2_sliced_vbi_cap *p = arg;
  2166. int ret = check_fmt(file, p->type);
  2167. if (ret)
  2168. return ret;
  2169. /* Clear up to type, everything after type is zeroed already */
  2170. memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
  2171. return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
  2172. }
  2173. static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
  2174. struct file *file, void *fh, void *arg)
  2175. {
  2176. struct video_device *vfd = video_devdata(file);
  2177. struct v4l2_frequency_band *p = arg;
  2178. enum v4l2_tuner_type type;
  2179. int err;
  2180. if (vfd->vfl_type == VFL_TYPE_SDR) {
  2181. if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
  2182. return -EINVAL;
  2183. type = p->type;
  2184. } else {
  2185. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  2186. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  2187. if (type != p->type)
  2188. return -EINVAL;
  2189. }
  2190. if (ops->vidioc_enum_freq_bands) {
  2191. err = ops->vidioc_enum_freq_bands(file, fh, p);
  2192. if (err != -ENOTTY)
  2193. return err;
  2194. }
  2195. if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
  2196. struct v4l2_tuner t = {
  2197. .index = p->tuner,
  2198. .type = type,
  2199. };
  2200. if (p->index)
  2201. return -EINVAL;
  2202. err = ops->vidioc_g_tuner(file, fh, &t);
  2203. if (err)
  2204. return err;
  2205. p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
  2206. p->rangelow = t.rangelow;
  2207. p->rangehigh = t.rangehigh;
  2208. p->modulation = (type == V4L2_TUNER_RADIO) ?
  2209. V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
  2210. return 0;
  2211. }
  2212. if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
  2213. struct v4l2_modulator m = {
  2214. .index = p->tuner,
  2215. };
  2216. if (type != V4L2_TUNER_RADIO)
  2217. return -EINVAL;
  2218. if (p->index)
  2219. return -EINVAL;
  2220. err = ops->vidioc_g_modulator(file, fh, &m);
  2221. if (err)
  2222. return err;
  2223. p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
  2224. p->rangelow = m.rangelow;
  2225. p->rangehigh = m.rangehigh;
  2226. p->modulation = (type == V4L2_TUNER_RADIO) ?
  2227. V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
  2228. return 0;
  2229. }
  2230. return -ENOTTY;
  2231. }
  2232. struct v4l2_ioctl_info {
  2233. unsigned int ioctl;
  2234. u32 flags;
  2235. const char * const name;
  2236. union {
  2237. u32 offset;
  2238. int (*func)(const struct v4l2_ioctl_ops *ops,
  2239. struct file *file, void *fh, void *p);
  2240. } u;
  2241. void (*debug)(const void *arg, bool write_only);
  2242. };
  2243. /* This control needs a priority check */
  2244. #define INFO_FL_PRIO (1 << 0)
  2245. /* This control can be valid if the filehandle passes a control handler. */
  2246. #define INFO_FL_CTRL (1 << 1)
  2247. /* This is a standard ioctl, no need for special code */
  2248. #define INFO_FL_STD (1 << 2)
  2249. /* This is ioctl has its own function */
  2250. #define INFO_FL_FUNC (1 << 3)
  2251. /* Queuing ioctl */
  2252. #define INFO_FL_QUEUE (1 << 4)
  2253. /* Always copy back result, even on error */
  2254. #define INFO_FL_ALWAYS_COPY (1 << 5)
  2255. /* Zero struct from after the field to the end */
  2256. #define INFO_FL_CLEAR(v4l2_struct, field) \
  2257. ((offsetof(struct v4l2_struct, field) + \
  2258. sizeof(((struct v4l2_struct *)0)->field)) << 16)
  2259. #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
  2260. #define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
  2261. [_IOC_NR(_ioctl)] = { \
  2262. .ioctl = _ioctl, \
  2263. .flags = _flags | INFO_FL_STD, \
  2264. .name = #_ioctl, \
  2265. .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
  2266. .debug = _debug, \
  2267. }
  2268. #define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
  2269. [_IOC_NR(_ioctl)] = { \
  2270. .ioctl = _ioctl, \
  2271. .flags = _flags | INFO_FL_FUNC, \
  2272. .name = #_ioctl, \
  2273. .u.func = _func, \
  2274. .debug = _debug, \
  2275. }
  2276. static struct v4l2_ioctl_info v4l2_ioctls[] = {
  2277. IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
  2278. IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
  2279. IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
  2280. IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
  2281. IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
  2282. IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
  2283. IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
  2284. IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
  2285. IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
  2286. IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
  2287. IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
  2288. IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
  2289. IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
  2290. IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
  2291. IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
  2292. IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
  2293. IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
  2294. IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
  2295. IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
  2296. IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
  2297. IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
  2298. IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
  2299. IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
  2300. IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
  2301. IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
  2302. IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
  2303. IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
  2304. IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
  2305. IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
  2306. IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
  2307. IOCTL_INFO_STD(VIDIOC_G_EDID, vidioc_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
  2308. IOCTL_INFO_STD(VIDIOC_S_EDID, vidioc_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
  2309. IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
  2310. IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
  2311. IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
  2312. IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
  2313. IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
  2314. IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
  2315. IOCTL_INFO_FNC(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
  2316. IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
  2317. IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
  2318. IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
  2319. IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
  2320. IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
  2321. IOCTL_INFO_FNC(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
  2322. IOCTL_INFO_FNC(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
  2323. IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
  2324. IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
  2325. IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
  2326. IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
  2327. IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
  2328. IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
  2329. IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
  2330. IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
  2331. 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)),
  2332. IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
  2333. IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  2334. IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
  2335. IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  2336. IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
  2337. IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
  2338. IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
  2339. IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  2340. IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  2341. IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
  2342. IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
  2343. IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
  2344. IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
  2345. IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
  2346. 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)),
  2347. IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
  2348. IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
  2349. IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
  2350. IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
  2351. IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
  2352. IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
  2353. IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
  2354. IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
  2355. IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
  2356. IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
  2357. 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)),
  2358. 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)),
  2359. };
  2360. #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
  2361. bool v4l2_is_known_ioctl(unsigned int cmd)
  2362. {
  2363. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  2364. return false;
  2365. return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
  2366. }
  2367. struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
  2368. {
  2369. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  2370. return vdev->lock;
  2371. if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
  2372. return NULL;
  2373. if (vdev->queue && vdev->queue->lock &&
  2374. (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
  2375. return vdev->queue->lock;
  2376. return vdev->lock;
  2377. }
  2378. /* Common ioctl debug function. This function can be used by
  2379. external ioctl messages as well as internal V4L ioctl */
  2380. void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
  2381. {
  2382. const char *dir, *type;
  2383. if (prefix)
  2384. printk(KERN_DEBUG "%s: ", prefix);
  2385. switch (_IOC_TYPE(cmd)) {
  2386. case 'd':
  2387. type = "v4l2_int";
  2388. break;
  2389. case 'V':
  2390. if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
  2391. type = "v4l2";
  2392. break;
  2393. }
  2394. pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
  2395. return;
  2396. default:
  2397. type = "unknown";
  2398. break;
  2399. }
  2400. switch (_IOC_DIR(cmd)) {
  2401. case _IOC_NONE: dir = "--"; break;
  2402. case _IOC_READ: dir = "r-"; break;
  2403. case _IOC_WRITE: dir = "-w"; break;
  2404. case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
  2405. default: dir = "*ERR*"; break;
  2406. }
  2407. pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
  2408. type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
  2409. }
  2410. EXPORT_SYMBOL(v4l_printk_ioctl);
  2411. static long __video_do_ioctl(struct file *file,
  2412. unsigned int cmd, void *arg)
  2413. {
  2414. struct video_device *vfd = video_devdata(file);
  2415. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  2416. bool write_only = false;
  2417. struct v4l2_ioctl_info default_info;
  2418. const struct v4l2_ioctl_info *info;
  2419. void *fh = file->private_data;
  2420. struct v4l2_fh *vfh = NULL;
  2421. int dev_debug = vfd->dev_debug;
  2422. long ret = -ENOTTY;
  2423. if (ops == NULL) {
  2424. pr_warn("%s: has no ioctl_ops.\n",
  2425. video_device_node_name(vfd));
  2426. return ret;
  2427. }
  2428. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
  2429. vfh = file->private_data;
  2430. if (v4l2_is_known_ioctl(cmd)) {
  2431. info = &v4l2_ioctls[_IOC_NR(cmd)];
  2432. if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
  2433. !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
  2434. goto done;
  2435. if (vfh && (info->flags & INFO_FL_PRIO)) {
  2436. ret = v4l2_prio_check(vfd->prio, vfh->prio);
  2437. if (ret)
  2438. goto done;
  2439. }
  2440. } else {
  2441. default_info.ioctl = cmd;
  2442. default_info.flags = 0;
  2443. default_info.debug = v4l_print_default;
  2444. info = &default_info;
  2445. }
  2446. write_only = _IOC_DIR(cmd) == _IOC_WRITE;
  2447. if (info->flags & INFO_FL_STD) {
  2448. typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
  2449. const void *p = vfd->ioctl_ops;
  2450. const vidioc_op *vidioc = p + info->u.offset;
  2451. ret = (*vidioc)(file, fh, arg);
  2452. } else if (info->flags & INFO_FL_FUNC) {
  2453. ret = info->u.func(ops, file, fh, arg);
  2454. } else if (!ops->vidioc_default) {
  2455. ret = -ENOTTY;
  2456. } else {
  2457. ret = ops->vidioc_default(file, fh,
  2458. vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
  2459. cmd, arg);
  2460. }
  2461. done:
  2462. if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
  2463. if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
  2464. (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
  2465. return ret;
  2466. v4l_printk_ioctl(video_device_node_name(vfd), cmd);
  2467. if (ret < 0)
  2468. pr_cont(": error %ld", ret);
  2469. if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
  2470. pr_cont("\n");
  2471. else if (_IOC_DIR(cmd) == _IOC_NONE)
  2472. info->debug(arg, write_only);
  2473. else {
  2474. pr_cont(": ");
  2475. info->debug(arg, write_only);
  2476. }
  2477. }
  2478. return ret;
  2479. }
  2480. static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
  2481. void __user **user_ptr, void ***kernel_ptr)
  2482. {
  2483. int ret = 0;
  2484. switch (cmd) {
  2485. case VIDIOC_PREPARE_BUF:
  2486. case VIDIOC_QUERYBUF:
  2487. case VIDIOC_QBUF:
  2488. case VIDIOC_DQBUF: {
  2489. struct v4l2_buffer *buf = parg;
  2490. if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
  2491. if (buf->length > VIDEO_MAX_PLANES) {
  2492. ret = -EINVAL;
  2493. break;
  2494. }
  2495. *user_ptr = (void __user *)buf->m.planes;
  2496. *kernel_ptr = (void **)&buf->m.planes;
  2497. *array_size = sizeof(struct v4l2_plane) * buf->length;
  2498. ret = 1;
  2499. }
  2500. break;
  2501. }
  2502. case VIDIOC_G_EDID:
  2503. case VIDIOC_S_EDID: {
  2504. struct v4l2_edid *edid = parg;
  2505. if (edid->blocks) {
  2506. if (edid->blocks > 256) {
  2507. ret = -EINVAL;
  2508. break;
  2509. }
  2510. *user_ptr = (void __user *)edid->edid;
  2511. *kernel_ptr = (void **)&edid->edid;
  2512. *array_size = edid->blocks * 128;
  2513. ret = 1;
  2514. }
  2515. break;
  2516. }
  2517. case VIDIOC_S_EXT_CTRLS:
  2518. case VIDIOC_G_EXT_CTRLS:
  2519. case VIDIOC_TRY_EXT_CTRLS: {
  2520. struct v4l2_ext_controls *ctrls = parg;
  2521. if (ctrls->count != 0) {
  2522. if (ctrls->count > V4L2_CID_MAX_CTRLS) {
  2523. ret = -EINVAL;
  2524. break;
  2525. }
  2526. *user_ptr = (void __user *)ctrls->controls;
  2527. *kernel_ptr = (void **)&ctrls->controls;
  2528. *array_size = sizeof(struct v4l2_ext_control)
  2529. * ctrls->count;
  2530. ret = 1;
  2531. }
  2532. break;
  2533. }
  2534. }
  2535. return ret;
  2536. }
  2537. long
  2538. video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
  2539. v4l2_kioctl func)
  2540. {
  2541. char sbuf[128];
  2542. void *mbuf = NULL;
  2543. void *parg = (void *)arg;
  2544. long err = -EINVAL;
  2545. bool has_array_args;
  2546. bool always_copy = false;
  2547. size_t array_size = 0;
  2548. void __user *user_ptr = NULL;
  2549. void **kernel_ptr = NULL;
  2550. /* Copy arguments into temp kernel buffer */
  2551. if (_IOC_DIR(cmd) != _IOC_NONE) {
  2552. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  2553. parg = sbuf;
  2554. } else {
  2555. /* too big to allocate from stack */
  2556. mbuf = kvmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  2557. if (NULL == mbuf)
  2558. return -ENOMEM;
  2559. parg = mbuf;
  2560. }
  2561. err = -EFAULT;
  2562. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  2563. unsigned int n = _IOC_SIZE(cmd);
  2564. /*
  2565. * In some cases, only a few fields are used as input,
  2566. * i.e. when the app sets "index" and then the driver
  2567. * fills in the rest of the structure for the thing
  2568. * with that index. We only need to copy up the first
  2569. * non-input field.
  2570. */
  2571. if (v4l2_is_known_ioctl(cmd)) {
  2572. u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
  2573. if (flags & INFO_FL_CLEAR_MASK)
  2574. n = (flags & INFO_FL_CLEAR_MASK) >> 16;
  2575. always_copy = flags & INFO_FL_ALWAYS_COPY;
  2576. }
  2577. if (copy_from_user(parg, (void __user *)arg, n))
  2578. goto out;
  2579. /* zero out anything we don't copy from userspace */
  2580. if (n < _IOC_SIZE(cmd))
  2581. memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
  2582. } else {
  2583. /* read-only ioctl */
  2584. memset(parg, 0, _IOC_SIZE(cmd));
  2585. }
  2586. }
  2587. err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
  2588. if (err < 0)
  2589. goto out;
  2590. has_array_args = err;
  2591. if (has_array_args) {
  2592. /*
  2593. * When adding new types of array args, make sure that the
  2594. * parent argument to ioctl (which contains the pointer to the
  2595. * array) fits into sbuf (so that mbuf will still remain
  2596. * unused up to here).
  2597. */
  2598. mbuf = kvmalloc(array_size, GFP_KERNEL);
  2599. err = -ENOMEM;
  2600. if (NULL == mbuf)
  2601. goto out_array_args;
  2602. err = -EFAULT;
  2603. if (copy_from_user(mbuf, user_ptr, array_size))
  2604. goto out_array_args;
  2605. *kernel_ptr = mbuf;
  2606. }
  2607. /* Handles IOCTL */
  2608. err = func(file, cmd, parg);
  2609. if (err == -ENOIOCTLCMD)
  2610. err = -ENOTTY;
  2611. if (err == 0) {
  2612. if (cmd == VIDIOC_DQBUF)
  2613. trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
  2614. else if (cmd == VIDIOC_QBUF)
  2615. trace_v4l2_qbuf(video_devdata(file)->minor, parg);
  2616. }
  2617. if (has_array_args) {
  2618. *kernel_ptr = (void __force *)user_ptr;
  2619. if (copy_to_user(user_ptr, mbuf, array_size))
  2620. err = -EFAULT;
  2621. goto out_array_args;
  2622. }
  2623. /*
  2624. * Some ioctls can return an error, but still have valid
  2625. * results that must be returned.
  2626. */
  2627. if (err < 0 && !always_copy)
  2628. goto out;
  2629. out_array_args:
  2630. /* Copy results into user buffer */
  2631. switch (_IOC_DIR(cmd)) {
  2632. case _IOC_READ:
  2633. case (_IOC_WRITE | _IOC_READ):
  2634. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  2635. err = -EFAULT;
  2636. break;
  2637. }
  2638. out:
  2639. kvfree(mbuf);
  2640. return err;
  2641. }
  2642. EXPORT_SYMBOL(video_usercopy);
  2643. long video_ioctl2(struct file *file,
  2644. unsigned int cmd, unsigned long arg)
  2645. {
  2646. return video_usercopy(file, cmd, arg, __video_do_ioctl);
  2647. }
  2648. EXPORT_SYMBOL(video_ioctl2);