v4l2-ioctl.c 94 KB

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