v4l2-ioctl.c 92 KB

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