v4l2-ioctl.c 93 KB

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