v4l2-ioctl.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454
  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/v4l2-chip-ident.h>
  27. #define dbgarg(cmd, fmt, arg...) \
  28. do { \
  29. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { \
  30. printk(KERN_DEBUG "%s: ", vfd->name); \
  31. v4l_printk_ioctl(cmd); \
  32. printk(" " fmt, ## arg); \
  33. } \
  34. } while (0)
  35. #define dbgarg2(fmt, arg...) \
  36. do { \
  37. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
  38. printk(KERN_DEBUG "%s: " fmt, vfd->name, ## arg);\
  39. } while (0)
  40. #define dbgarg3(fmt, arg...) \
  41. do { \
  42. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
  43. printk(KERN_CONT "%s: " fmt, vfd->name, ## arg);\
  44. } while (0)
  45. /* Zero out the end of the struct pointed to by p. Everything after, but
  46. * not including, the specified field is cleared. */
  47. #define CLEAR_AFTER_FIELD(p, field) \
  48. memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
  49. 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
  50. #define have_fmt_ops(foo) ( \
  51. ops->vidioc_##foo##_fmt_vid_cap || \
  52. ops->vidioc_##foo##_fmt_vid_out || \
  53. ops->vidioc_##foo##_fmt_vid_cap_mplane || \
  54. ops->vidioc_##foo##_fmt_vid_out_mplane || \
  55. ops->vidioc_##foo##_fmt_vid_overlay || \
  56. ops->vidioc_##foo##_fmt_vbi_cap || \
  57. ops->vidioc_##foo##_fmt_vid_out_overlay || \
  58. ops->vidioc_##foo##_fmt_vbi_out || \
  59. ops->vidioc_##foo##_fmt_sliced_vbi_cap || \
  60. ops->vidioc_##foo##_fmt_sliced_vbi_out || \
  61. ops->vidioc_##foo##_fmt_type_private)
  62. struct std_descr {
  63. v4l2_std_id std;
  64. const char *descr;
  65. };
  66. static const struct std_descr standards[] = {
  67. { V4L2_STD_NTSC, "NTSC" },
  68. { V4L2_STD_NTSC_M, "NTSC-M" },
  69. { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
  70. { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
  71. { V4L2_STD_NTSC_443, "NTSC-443" },
  72. { V4L2_STD_PAL, "PAL" },
  73. { V4L2_STD_PAL_BG, "PAL-BG" },
  74. { V4L2_STD_PAL_B, "PAL-B" },
  75. { V4L2_STD_PAL_B1, "PAL-B1" },
  76. { V4L2_STD_PAL_G, "PAL-G" },
  77. { V4L2_STD_PAL_H, "PAL-H" },
  78. { V4L2_STD_PAL_I, "PAL-I" },
  79. { V4L2_STD_PAL_DK, "PAL-DK" },
  80. { V4L2_STD_PAL_D, "PAL-D" },
  81. { V4L2_STD_PAL_D1, "PAL-D1" },
  82. { V4L2_STD_PAL_K, "PAL-K" },
  83. { V4L2_STD_PAL_M, "PAL-M" },
  84. { V4L2_STD_PAL_N, "PAL-N" },
  85. { V4L2_STD_PAL_Nc, "PAL-Nc" },
  86. { V4L2_STD_PAL_60, "PAL-60" },
  87. { V4L2_STD_SECAM, "SECAM" },
  88. { V4L2_STD_SECAM_B, "SECAM-B" },
  89. { V4L2_STD_SECAM_G, "SECAM-G" },
  90. { V4L2_STD_SECAM_H, "SECAM-H" },
  91. { V4L2_STD_SECAM_DK, "SECAM-DK" },
  92. { V4L2_STD_SECAM_D, "SECAM-D" },
  93. { V4L2_STD_SECAM_K, "SECAM-K" },
  94. { V4L2_STD_SECAM_K1, "SECAM-K1" },
  95. { V4L2_STD_SECAM_L, "SECAM-L" },
  96. { V4L2_STD_SECAM_LC, "SECAM-Lc" },
  97. { 0, "Unknown" }
  98. };
  99. /* video4linux standard ID conversion to standard name
  100. */
  101. const char *v4l2_norm_to_name(v4l2_std_id id)
  102. {
  103. u32 myid = id;
  104. int i;
  105. /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
  106. 64 bit comparations. So, on that architecture, with some gcc
  107. variants, compilation fails. Currently, the max value is 30bit wide.
  108. */
  109. BUG_ON(myid != id);
  110. for (i = 0; standards[i].std; i++)
  111. if (myid == standards[i].std)
  112. break;
  113. return standards[i].descr;
  114. }
  115. EXPORT_SYMBOL(v4l2_norm_to_name);
  116. /* Returns frame period for the given standard */
  117. void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
  118. {
  119. if (id & V4L2_STD_525_60) {
  120. frameperiod->numerator = 1001;
  121. frameperiod->denominator = 30000;
  122. } else {
  123. frameperiod->numerator = 1;
  124. frameperiod->denominator = 25;
  125. }
  126. }
  127. EXPORT_SYMBOL(v4l2_video_std_frame_period);
  128. /* Fill in the fields of a v4l2_standard structure according to the
  129. 'id' and 'transmission' parameters. Returns negative on error. */
  130. int v4l2_video_std_construct(struct v4l2_standard *vs,
  131. int id, const char *name)
  132. {
  133. vs->id = id;
  134. v4l2_video_std_frame_period(id, &vs->frameperiod);
  135. vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
  136. strlcpy(vs->name, name, sizeof(vs->name));
  137. return 0;
  138. }
  139. EXPORT_SYMBOL(v4l2_video_std_construct);
  140. /* ----------------------------------------------------------------- */
  141. /* some arrays for pretty-printing debug messages of enum types */
  142. const char *v4l2_field_names[] = {
  143. [V4L2_FIELD_ANY] = "any",
  144. [V4L2_FIELD_NONE] = "none",
  145. [V4L2_FIELD_TOP] = "top",
  146. [V4L2_FIELD_BOTTOM] = "bottom",
  147. [V4L2_FIELD_INTERLACED] = "interlaced",
  148. [V4L2_FIELD_SEQ_TB] = "seq-tb",
  149. [V4L2_FIELD_SEQ_BT] = "seq-bt",
  150. [V4L2_FIELD_ALTERNATE] = "alternate",
  151. [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
  152. [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
  153. };
  154. EXPORT_SYMBOL(v4l2_field_names);
  155. const char *v4l2_type_names[] = {
  156. [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
  157. [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
  158. [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
  159. [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
  160. [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
  161. [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
  162. [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
  163. [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
  164. [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
  165. [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
  166. };
  167. EXPORT_SYMBOL(v4l2_type_names);
  168. static const char *v4l2_memory_names[] = {
  169. [V4L2_MEMORY_MMAP] = "mmap",
  170. [V4L2_MEMORY_USERPTR] = "userptr",
  171. [V4L2_MEMORY_OVERLAY] = "overlay",
  172. };
  173. #define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
  174. arr[a] : "unknown")
  175. /* ------------------------------------------------------------------ */
  176. /* debug help functions */
  177. static const char *v4l2_ioctls[] = {
  178. [_IOC_NR(VIDIOC_QUERYCAP)] = "VIDIOC_QUERYCAP",
  179. [_IOC_NR(VIDIOC_RESERVED)] = "VIDIOC_RESERVED",
  180. [_IOC_NR(VIDIOC_ENUM_FMT)] = "VIDIOC_ENUM_FMT",
  181. [_IOC_NR(VIDIOC_G_FMT)] = "VIDIOC_G_FMT",
  182. [_IOC_NR(VIDIOC_S_FMT)] = "VIDIOC_S_FMT",
  183. [_IOC_NR(VIDIOC_REQBUFS)] = "VIDIOC_REQBUFS",
  184. [_IOC_NR(VIDIOC_QUERYBUF)] = "VIDIOC_QUERYBUF",
  185. [_IOC_NR(VIDIOC_G_FBUF)] = "VIDIOC_G_FBUF",
  186. [_IOC_NR(VIDIOC_S_FBUF)] = "VIDIOC_S_FBUF",
  187. [_IOC_NR(VIDIOC_OVERLAY)] = "VIDIOC_OVERLAY",
  188. [_IOC_NR(VIDIOC_QBUF)] = "VIDIOC_QBUF",
  189. [_IOC_NR(VIDIOC_DQBUF)] = "VIDIOC_DQBUF",
  190. [_IOC_NR(VIDIOC_STREAMON)] = "VIDIOC_STREAMON",
  191. [_IOC_NR(VIDIOC_STREAMOFF)] = "VIDIOC_STREAMOFF",
  192. [_IOC_NR(VIDIOC_G_PARM)] = "VIDIOC_G_PARM",
  193. [_IOC_NR(VIDIOC_S_PARM)] = "VIDIOC_S_PARM",
  194. [_IOC_NR(VIDIOC_G_STD)] = "VIDIOC_G_STD",
  195. [_IOC_NR(VIDIOC_S_STD)] = "VIDIOC_S_STD",
  196. [_IOC_NR(VIDIOC_ENUMSTD)] = "VIDIOC_ENUMSTD",
  197. [_IOC_NR(VIDIOC_ENUMINPUT)] = "VIDIOC_ENUMINPUT",
  198. [_IOC_NR(VIDIOC_G_CTRL)] = "VIDIOC_G_CTRL",
  199. [_IOC_NR(VIDIOC_S_CTRL)] = "VIDIOC_S_CTRL",
  200. [_IOC_NR(VIDIOC_G_TUNER)] = "VIDIOC_G_TUNER",
  201. [_IOC_NR(VIDIOC_S_TUNER)] = "VIDIOC_S_TUNER",
  202. [_IOC_NR(VIDIOC_G_AUDIO)] = "VIDIOC_G_AUDIO",
  203. [_IOC_NR(VIDIOC_S_AUDIO)] = "VIDIOC_S_AUDIO",
  204. [_IOC_NR(VIDIOC_QUERYCTRL)] = "VIDIOC_QUERYCTRL",
  205. [_IOC_NR(VIDIOC_QUERYMENU)] = "VIDIOC_QUERYMENU",
  206. [_IOC_NR(VIDIOC_G_INPUT)] = "VIDIOC_G_INPUT",
  207. [_IOC_NR(VIDIOC_S_INPUT)] = "VIDIOC_S_INPUT",
  208. [_IOC_NR(VIDIOC_G_OUTPUT)] = "VIDIOC_G_OUTPUT",
  209. [_IOC_NR(VIDIOC_S_OUTPUT)] = "VIDIOC_S_OUTPUT",
  210. [_IOC_NR(VIDIOC_ENUMOUTPUT)] = "VIDIOC_ENUMOUTPUT",
  211. [_IOC_NR(VIDIOC_G_AUDOUT)] = "VIDIOC_G_AUDOUT",
  212. [_IOC_NR(VIDIOC_S_AUDOUT)] = "VIDIOC_S_AUDOUT",
  213. [_IOC_NR(VIDIOC_G_MODULATOR)] = "VIDIOC_G_MODULATOR",
  214. [_IOC_NR(VIDIOC_S_MODULATOR)] = "VIDIOC_S_MODULATOR",
  215. [_IOC_NR(VIDIOC_G_FREQUENCY)] = "VIDIOC_G_FREQUENCY",
  216. [_IOC_NR(VIDIOC_S_FREQUENCY)] = "VIDIOC_S_FREQUENCY",
  217. [_IOC_NR(VIDIOC_CROPCAP)] = "VIDIOC_CROPCAP",
  218. [_IOC_NR(VIDIOC_G_CROP)] = "VIDIOC_G_CROP",
  219. [_IOC_NR(VIDIOC_S_CROP)] = "VIDIOC_S_CROP",
  220. [_IOC_NR(VIDIOC_G_SELECTION)] = "VIDIOC_G_SELECTION",
  221. [_IOC_NR(VIDIOC_S_SELECTION)] = "VIDIOC_S_SELECTION",
  222. [_IOC_NR(VIDIOC_G_JPEGCOMP)] = "VIDIOC_G_JPEGCOMP",
  223. [_IOC_NR(VIDIOC_S_JPEGCOMP)] = "VIDIOC_S_JPEGCOMP",
  224. [_IOC_NR(VIDIOC_QUERYSTD)] = "VIDIOC_QUERYSTD",
  225. [_IOC_NR(VIDIOC_TRY_FMT)] = "VIDIOC_TRY_FMT",
  226. [_IOC_NR(VIDIOC_ENUMAUDIO)] = "VIDIOC_ENUMAUDIO",
  227. [_IOC_NR(VIDIOC_ENUMAUDOUT)] = "VIDIOC_ENUMAUDOUT",
  228. [_IOC_NR(VIDIOC_G_PRIORITY)] = "VIDIOC_G_PRIORITY",
  229. [_IOC_NR(VIDIOC_S_PRIORITY)] = "VIDIOC_S_PRIORITY",
  230. [_IOC_NR(VIDIOC_G_SLICED_VBI_CAP)] = "VIDIOC_G_SLICED_VBI_CAP",
  231. [_IOC_NR(VIDIOC_LOG_STATUS)] = "VIDIOC_LOG_STATUS",
  232. [_IOC_NR(VIDIOC_G_EXT_CTRLS)] = "VIDIOC_G_EXT_CTRLS",
  233. [_IOC_NR(VIDIOC_S_EXT_CTRLS)] = "VIDIOC_S_EXT_CTRLS",
  234. [_IOC_NR(VIDIOC_TRY_EXT_CTRLS)] = "VIDIOC_TRY_EXT_CTRLS",
  235. #if 1
  236. [_IOC_NR(VIDIOC_ENUM_FRAMESIZES)] = "VIDIOC_ENUM_FRAMESIZES",
  237. [_IOC_NR(VIDIOC_ENUM_FRAMEINTERVALS)] = "VIDIOC_ENUM_FRAMEINTERVALS",
  238. [_IOC_NR(VIDIOC_G_ENC_INDEX)] = "VIDIOC_G_ENC_INDEX",
  239. [_IOC_NR(VIDIOC_ENCODER_CMD)] = "VIDIOC_ENCODER_CMD",
  240. [_IOC_NR(VIDIOC_TRY_ENCODER_CMD)] = "VIDIOC_TRY_ENCODER_CMD",
  241. [_IOC_NR(VIDIOC_DBG_S_REGISTER)] = "VIDIOC_DBG_S_REGISTER",
  242. [_IOC_NR(VIDIOC_DBG_G_REGISTER)] = "VIDIOC_DBG_G_REGISTER",
  243. [_IOC_NR(VIDIOC_DBG_G_CHIP_IDENT)] = "VIDIOC_DBG_G_CHIP_IDENT",
  244. [_IOC_NR(VIDIOC_S_HW_FREQ_SEEK)] = "VIDIOC_S_HW_FREQ_SEEK",
  245. #endif
  246. [_IOC_NR(VIDIOC_ENUM_DV_PRESETS)] = "VIDIOC_ENUM_DV_PRESETS",
  247. [_IOC_NR(VIDIOC_S_DV_PRESET)] = "VIDIOC_S_DV_PRESET",
  248. [_IOC_NR(VIDIOC_G_DV_PRESET)] = "VIDIOC_G_DV_PRESET",
  249. [_IOC_NR(VIDIOC_QUERY_DV_PRESET)] = "VIDIOC_QUERY_DV_PRESET",
  250. [_IOC_NR(VIDIOC_S_DV_TIMINGS)] = "VIDIOC_S_DV_TIMINGS",
  251. [_IOC_NR(VIDIOC_G_DV_TIMINGS)] = "VIDIOC_G_DV_TIMINGS",
  252. [_IOC_NR(VIDIOC_DQEVENT)] = "VIDIOC_DQEVENT",
  253. [_IOC_NR(VIDIOC_SUBSCRIBE_EVENT)] = "VIDIOC_SUBSCRIBE_EVENT",
  254. [_IOC_NR(VIDIOC_UNSUBSCRIBE_EVENT)] = "VIDIOC_UNSUBSCRIBE_EVENT",
  255. [_IOC_NR(VIDIOC_CREATE_BUFS)] = "VIDIOC_CREATE_BUFS",
  256. [_IOC_NR(VIDIOC_PREPARE_BUF)] = "VIDIOC_PREPARE_BUF",
  257. };
  258. #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
  259. /* Common ioctl debug function. This function can be used by
  260. external ioctl messages as well as internal V4L ioctl */
  261. void v4l_printk_ioctl(unsigned int cmd)
  262. {
  263. char *dir, *type;
  264. switch (_IOC_TYPE(cmd)) {
  265. case 'd':
  266. type = "v4l2_int";
  267. break;
  268. case 'V':
  269. if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
  270. type = "v4l2";
  271. break;
  272. }
  273. printk("%s", v4l2_ioctls[_IOC_NR(cmd)]);
  274. return;
  275. default:
  276. type = "unknown";
  277. }
  278. switch (_IOC_DIR(cmd)) {
  279. case _IOC_NONE: dir = "--"; break;
  280. case _IOC_READ: dir = "r-"; break;
  281. case _IOC_WRITE: dir = "-w"; break;
  282. case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
  283. default: dir = "*ERR*"; break;
  284. }
  285. printk("%s ioctl '%c', dir=%s, #%d (0x%08x)",
  286. type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
  287. }
  288. EXPORT_SYMBOL(v4l_printk_ioctl);
  289. static void dbgbuf(unsigned int cmd, struct video_device *vfd,
  290. struct v4l2_buffer *p)
  291. {
  292. struct v4l2_timecode *tc = &p->timecode;
  293. struct v4l2_plane *plane;
  294. int i;
  295. dbgarg(cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
  296. "flags=0x%08d, field=%0d, sequence=%d, memory=%s\n",
  297. p->timestamp.tv_sec / 3600,
  298. (int)(p->timestamp.tv_sec / 60) % 60,
  299. (int)(p->timestamp.tv_sec % 60),
  300. (long)p->timestamp.tv_usec,
  301. p->index,
  302. prt_names(p->type, v4l2_type_names),
  303. p->flags, p->field, p->sequence,
  304. prt_names(p->memory, v4l2_memory_names));
  305. if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
  306. for (i = 0; i < p->length; ++i) {
  307. plane = &p->m.planes[i];
  308. dbgarg2("plane %d: bytesused=%d, data_offset=0x%08x "
  309. "offset/userptr=0x%08lx, length=%d\n",
  310. i, plane->bytesused, plane->data_offset,
  311. plane->m.userptr, plane->length);
  312. }
  313. } else {
  314. dbgarg2("bytesused=%d, offset/userptr=0x%08lx, length=%d\n",
  315. p->bytesused, p->m.userptr, p->length);
  316. }
  317. dbgarg2("timecode=%02d:%02d:%02d type=%d, "
  318. "flags=0x%08d, frames=%d, userbits=0x%08x\n",
  319. tc->hours, tc->minutes, tc->seconds,
  320. tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
  321. }
  322. static inline void dbgrect(struct video_device *vfd, char *s,
  323. struct v4l2_rect *r)
  324. {
  325. dbgarg2("%sRect start at %dx%d, size=%dx%d\n", s, r->left, r->top,
  326. r->width, r->height);
  327. };
  328. static inline void v4l_print_pix_fmt(struct video_device *vfd,
  329. struct v4l2_pix_format *fmt)
  330. {
  331. dbgarg2("width=%d, height=%d, format=%c%c%c%c, field=%s, "
  332. "bytesperline=%d sizeimage=%d, colorspace=%d\n",
  333. fmt->width, fmt->height,
  334. (fmt->pixelformat & 0xff),
  335. (fmt->pixelformat >> 8) & 0xff,
  336. (fmt->pixelformat >> 16) & 0xff,
  337. (fmt->pixelformat >> 24) & 0xff,
  338. prt_names(fmt->field, v4l2_field_names),
  339. fmt->bytesperline, fmt->sizeimage, fmt->colorspace);
  340. };
  341. static inline void v4l_print_pix_fmt_mplane(struct video_device *vfd,
  342. struct v4l2_pix_format_mplane *fmt)
  343. {
  344. int i;
  345. dbgarg2("width=%d, height=%d, format=%c%c%c%c, field=%s, "
  346. "colorspace=%d, num_planes=%d\n",
  347. fmt->width, fmt->height,
  348. (fmt->pixelformat & 0xff),
  349. (fmt->pixelformat >> 8) & 0xff,
  350. (fmt->pixelformat >> 16) & 0xff,
  351. (fmt->pixelformat >> 24) & 0xff,
  352. prt_names(fmt->field, v4l2_field_names),
  353. fmt->colorspace, fmt->num_planes);
  354. for (i = 0; i < fmt->num_planes; ++i)
  355. dbgarg2("plane %d: bytesperline=%d sizeimage=%d\n", i,
  356. fmt->plane_fmt[i].bytesperline,
  357. fmt->plane_fmt[i].sizeimage);
  358. }
  359. static inline void v4l_print_ext_ctrls(unsigned int cmd,
  360. struct video_device *vfd, struct v4l2_ext_controls *c, int show_vals)
  361. {
  362. __u32 i;
  363. if (!(vfd->debug & V4L2_DEBUG_IOCTL_ARG))
  364. return;
  365. dbgarg(cmd, "");
  366. printk(KERN_CONT "class=0x%x", c->ctrl_class);
  367. for (i = 0; i < c->count; i++) {
  368. if (show_vals && !c->controls[i].size)
  369. printk(KERN_CONT " id/val=0x%x/0x%x",
  370. c->controls[i].id, c->controls[i].value);
  371. else
  372. printk(KERN_CONT " id=0x%x,size=%u",
  373. c->controls[i].id, c->controls[i].size);
  374. }
  375. printk(KERN_CONT "\n");
  376. };
  377. static inline int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
  378. {
  379. __u32 i;
  380. /* zero the reserved fields */
  381. c->reserved[0] = c->reserved[1] = 0;
  382. for (i = 0; i < c->count; i++)
  383. c->controls[i].reserved2[0] = 0;
  384. /* V4L2_CID_PRIVATE_BASE cannot be used as control class
  385. when using extended controls.
  386. Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
  387. is it allowed for backwards compatibility.
  388. */
  389. if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
  390. return 0;
  391. /* Check that all controls are from the same control class. */
  392. for (i = 0; i < c->count; i++) {
  393. if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
  394. c->error_idx = i;
  395. return 0;
  396. }
  397. }
  398. return 1;
  399. }
  400. static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
  401. {
  402. if (ops == NULL)
  403. return -EINVAL;
  404. switch (type) {
  405. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  406. if (ops->vidioc_g_fmt_vid_cap ||
  407. ops->vidioc_g_fmt_vid_cap_mplane)
  408. return 0;
  409. break;
  410. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  411. if (ops->vidioc_g_fmt_vid_cap_mplane)
  412. return 0;
  413. break;
  414. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  415. if (ops->vidioc_g_fmt_vid_overlay)
  416. return 0;
  417. break;
  418. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  419. if (ops->vidioc_g_fmt_vid_out ||
  420. ops->vidioc_g_fmt_vid_out_mplane)
  421. return 0;
  422. break;
  423. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  424. if (ops->vidioc_g_fmt_vid_out_mplane)
  425. return 0;
  426. break;
  427. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  428. if (ops->vidioc_g_fmt_vid_out_overlay)
  429. return 0;
  430. break;
  431. case V4L2_BUF_TYPE_VBI_CAPTURE:
  432. if (ops->vidioc_g_fmt_vbi_cap)
  433. return 0;
  434. break;
  435. case V4L2_BUF_TYPE_VBI_OUTPUT:
  436. if (ops->vidioc_g_fmt_vbi_out)
  437. return 0;
  438. break;
  439. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  440. if (ops->vidioc_g_fmt_sliced_vbi_cap)
  441. return 0;
  442. break;
  443. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  444. if (ops->vidioc_g_fmt_sliced_vbi_out)
  445. return 0;
  446. break;
  447. case V4L2_BUF_TYPE_PRIVATE:
  448. if (ops->vidioc_g_fmt_type_private)
  449. return 0;
  450. break;
  451. }
  452. return -EINVAL;
  453. }
  454. static long __video_do_ioctl(struct file *file,
  455. unsigned int cmd, void *arg)
  456. {
  457. struct video_device *vfd = video_devdata(file);
  458. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  459. void *fh = file->private_data;
  460. struct v4l2_fh *vfh = NULL;
  461. int use_fh_prio = 0;
  462. long ret_prio = 0;
  463. long ret = -ENOTTY;
  464. if (ops == NULL) {
  465. printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
  466. vfd->name);
  467. return ret;
  468. }
  469. if ((vfd->debug & V4L2_DEBUG_IOCTL) &&
  470. !(vfd->debug & V4L2_DEBUG_IOCTL_ARG)) {
  471. v4l_print_ioctl(vfd->name, cmd);
  472. printk(KERN_CONT "\n");
  473. }
  474. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  475. vfh = file->private_data;
  476. use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  477. }
  478. if (use_fh_prio)
  479. ret_prio = v4l2_prio_check(vfd->prio, vfh->prio);
  480. switch (cmd) {
  481. /* --- capabilities ------------------------------------------ */
  482. case VIDIOC_QUERYCAP:
  483. {
  484. struct v4l2_capability *cap = (struct v4l2_capability *)arg;
  485. if (!ops->vidioc_querycap)
  486. break;
  487. cap->version = LINUX_VERSION_CODE;
  488. ret = ops->vidioc_querycap(file, fh, cap);
  489. if (!ret)
  490. dbgarg(cmd, "driver=%s, card=%s, bus=%s, "
  491. "version=0x%08x, "
  492. "capabilities=0x%08x\n",
  493. cap->driver, cap->card, cap->bus_info,
  494. cap->version,
  495. cap->capabilities);
  496. break;
  497. }
  498. /* --- priority ------------------------------------------ */
  499. case VIDIOC_G_PRIORITY:
  500. {
  501. enum v4l2_priority *p = arg;
  502. if (ops->vidioc_g_priority) {
  503. ret = ops->vidioc_g_priority(file, fh, p);
  504. } else if (use_fh_prio) {
  505. *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
  506. ret = 0;
  507. }
  508. if (!ret)
  509. dbgarg(cmd, "priority is %d\n", *p);
  510. break;
  511. }
  512. case VIDIOC_S_PRIORITY:
  513. {
  514. enum v4l2_priority *p = arg;
  515. if (!ops->vidioc_s_priority && !use_fh_prio)
  516. break;
  517. dbgarg(cmd, "setting priority to %d\n", *p);
  518. if (ops->vidioc_s_priority)
  519. ret = ops->vidioc_s_priority(file, fh, *p);
  520. else
  521. ret = ret_prio ? ret_prio :
  522. v4l2_prio_change(&vfd->v4l2_dev->prio,
  523. &vfh->prio, *p);
  524. break;
  525. }
  526. /* --- capture ioctls ---------------------------------------- */
  527. case VIDIOC_ENUM_FMT:
  528. {
  529. struct v4l2_fmtdesc *f = arg;
  530. switch (f->type) {
  531. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  532. if (likely(ops->vidioc_enum_fmt_vid_cap))
  533. ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f);
  534. break;
  535. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  536. if (likely(ops->vidioc_enum_fmt_vid_cap_mplane))
  537. ret = ops->vidioc_enum_fmt_vid_cap_mplane(file,
  538. fh, f);
  539. break;
  540. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  541. if (likely(ops->vidioc_enum_fmt_vid_overlay))
  542. ret = ops->vidioc_enum_fmt_vid_overlay(file,
  543. fh, f);
  544. break;
  545. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  546. if (likely(ops->vidioc_enum_fmt_vid_out))
  547. ret = ops->vidioc_enum_fmt_vid_out(file, fh, f);
  548. break;
  549. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  550. if (likely(ops->vidioc_enum_fmt_vid_out_mplane))
  551. ret = ops->vidioc_enum_fmt_vid_out_mplane(file,
  552. fh, f);
  553. break;
  554. case V4L2_BUF_TYPE_PRIVATE:
  555. if (likely(ops->vidioc_enum_fmt_type_private))
  556. ret = ops->vidioc_enum_fmt_type_private(file,
  557. fh, f);
  558. break;
  559. default:
  560. break;
  561. }
  562. if (likely (!ret))
  563. dbgarg(cmd, "index=%d, type=%d, flags=%d, "
  564. "pixelformat=%c%c%c%c, description='%s'\n",
  565. f->index, f->type, f->flags,
  566. (f->pixelformat & 0xff),
  567. (f->pixelformat >> 8) & 0xff,
  568. (f->pixelformat >> 16) & 0xff,
  569. (f->pixelformat >> 24) & 0xff,
  570. f->description);
  571. else if (ret == -ENOTTY &&
  572. (ops->vidioc_enum_fmt_vid_cap ||
  573. ops->vidioc_enum_fmt_vid_out ||
  574. ops->vidioc_enum_fmt_vid_cap_mplane ||
  575. ops->vidioc_enum_fmt_vid_out_mplane ||
  576. ops->vidioc_enum_fmt_vid_overlay ||
  577. ops->vidioc_enum_fmt_type_private))
  578. ret = -EINVAL;
  579. break;
  580. }
  581. case VIDIOC_G_FMT:
  582. {
  583. struct v4l2_format *f = (struct v4l2_format *)arg;
  584. /* FIXME: Should be one dump per type */
  585. dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
  586. switch (f->type) {
  587. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  588. if (ops->vidioc_g_fmt_vid_cap)
  589. ret = ops->vidioc_g_fmt_vid_cap(file, fh, f);
  590. if (!ret)
  591. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  592. break;
  593. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  594. if (ops->vidioc_g_fmt_vid_cap_mplane)
  595. ret = ops->vidioc_g_fmt_vid_cap_mplane(file,
  596. fh, f);
  597. if (!ret)
  598. v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
  599. break;
  600. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  601. if (likely(ops->vidioc_g_fmt_vid_overlay))
  602. ret = ops->vidioc_g_fmt_vid_overlay(file,
  603. fh, f);
  604. break;
  605. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  606. if (ops->vidioc_g_fmt_vid_out)
  607. ret = ops->vidioc_g_fmt_vid_out(file, fh, f);
  608. if (!ret)
  609. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  610. break;
  611. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  612. if (ops->vidioc_g_fmt_vid_out_mplane)
  613. ret = ops->vidioc_g_fmt_vid_out_mplane(file,
  614. fh, f);
  615. if (!ret)
  616. v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
  617. break;
  618. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  619. if (likely(ops->vidioc_g_fmt_vid_out_overlay))
  620. ret = ops->vidioc_g_fmt_vid_out_overlay(file,
  621. fh, f);
  622. break;
  623. case V4L2_BUF_TYPE_VBI_CAPTURE:
  624. if (likely(ops->vidioc_g_fmt_vbi_cap))
  625. ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f);
  626. break;
  627. case V4L2_BUF_TYPE_VBI_OUTPUT:
  628. if (likely(ops->vidioc_g_fmt_vbi_out))
  629. ret = ops->vidioc_g_fmt_vbi_out(file, fh, f);
  630. break;
  631. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  632. if (likely(ops->vidioc_g_fmt_sliced_vbi_cap))
  633. ret = ops->vidioc_g_fmt_sliced_vbi_cap(file,
  634. fh, f);
  635. break;
  636. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  637. if (likely(ops->vidioc_g_fmt_sliced_vbi_out))
  638. ret = ops->vidioc_g_fmt_sliced_vbi_out(file,
  639. fh, f);
  640. break;
  641. case V4L2_BUF_TYPE_PRIVATE:
  642. if (likely(ops->vidioc_g_fmt_type_private))
  643. ret = ops->vidioc_g_fmt_type_private(file,
  644. fh, f);
  645. break;
  646. }
  647. if (unlikely(ret == -ENOTTY && have_fmt_ops(g)))
  648. ret = -EINVAL;
  649. break;
  650. }
  651. case VIDIOC_S_FMT:
  652. {
  653. struct v4l2_format *f = (struct v4l2_format *)arg;
  654. if (!have_fmt_ops(s))
  655. break;
  656. if (ret_prio) {
  657. ret = ret_prio;
  658. break;
  659. }
  660. ret = -EINVAL;
  661. /* FIXME: Should be one dump per type */
  662. dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
  663. switch (f->type) {
  664. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  665. CLEAR_AFTER_FIELD(f, fmt.pix);
  666. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  667. if (ops->vidioc_s_fmt_vid_cap)
  668. ret = ops->vidioc_s_fmt_vid_cap(file, fh, f);
  669. break;
  670. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  671. CLEAR_AFTER_FIELD(f, fmt.pix_mp);
  672. v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
  673. if (ops->vidioc_s_fmt_vid_cap_mplane)
  674. ret = ops->vidioc_s_fmt_vid_cap_mplane(file,
  675. fh, f);
  676. break;
  677. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  678. CLEAR_AFTER_FIELD(f, fmt.win);
  679. if (ops->vidioc_s_fmt_vid_overlay)
  680. ret = ops->vidioc_s_fmt_vid_overlay(file,
  681. fh, f);
  682. break;
  683. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  684. CLEAR_AFTER_FIELD(f, fmt.pix);
  685. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  686. if (ops->vidioc_s_fmt_vid_out)
  687. ret = ops->vidioc_s_fmt_vid_out(file, fh, f);
  688. break;
  689. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  690. CLEAR_AFTER_FIELD(f, fmt.pix_mp);
  691. v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
  692. if (ops->vidioc_s_fmt_vid_out_mplane)
  693. ret = ops->vidioc_s_fmt_vid_out_mplane(file,
  694. fh, f);
  695. break;
  696. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  697. CLEAR_AFTER_FIELD(f, fmt.win);
  698. if (ops->vidioc_s_fmt_vid_out_overlay)
  699. ret = ops->vidioc_s_fmt_vid_out_overlay(file,
  700. fh, f);
  701. break;
  702. case V4L2_BUF_TYPE_VBI_CAPTURE:
  703. CLEAR_AFTER_FIELD(f, fmt.vbi);
  704. if (likely(ops->vidioc_s_fmt_vbi_cap))
  705. ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
  706. break;
  707. case V4L2_BUF_TYPE_VBI_OUTPUT:
  708. CLEAR_AFTER_FIELD(f, fmt.vbi);
  709. if (likely(ops->vidioc_s_fmt_vbi_out))
  710. ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
  711. break;
  712. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  713. CLEAR_AFTER_FIELD(f, fmt.sliced);
  714. if (likely(ops->vidioc_s_fmt_sliced_vbi_cap))
  715. ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
  716. fh, f);
  717. break;
  718. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  719. CLEAR_AFTER_FIELD(f, fmt.sliced);
  720. if (likely(ops->vidioc_s_fmt_sliced_vbi_out))
  721. ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
  722. fh, f);
  723. break;
  724. case V4L2_BUF_TYPE_PRIVATE:
  725. /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
  726. if (likely(ops->vidioc_s_fmt_type_private))
  727. ret = ops->vidioc_s_fmt_type_private(file,
  728. fh, f);
  729. break;
  730. }
  731. break;
  732. }
  733. case VIDIOC_TRY_FMT:
  734. {
  735. struct v4l2_format *f = (struct v4l2_format *)arg;
  736. /* FIXME: Should be one dump per type */
  737. dbgarg(cmd, "type=%s\n", prt_names(f->type,
  738. v4l2_type_names));
  739. switch (f->type) {
  740. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  741. CLEAR_AFTER_FIELD(f, fmt.pix);
  742. if (ops->vidioc_try_fmt_vid_cap)
  743. ret = ops->vidioc_try_fmt_vid_cap(file, fh, f);
  744. if (!ret)
  745. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  746. break;
  747. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  748. CLEAR_AFTER_FIELD(f, fmt.pix_mp);
  749. if (ops->vidioc_try_fmt_vid_cap_mplane)
  750. ret = ops->vidioc_try_fmt_vid_cap_mplane(file,
  751. fh, f);
  752. if (!ret)
  753. v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
  754. break;
  755. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  756. CLEAR_AFTER_FIELD(f, fmt.win);
  757. if (likely(ops->vidioc_try_fmt_vid_overlay))
  758. ret = ops->vidioc_try_fmt_vid_overlay(file,
  759. fh, f);
  760. break;
  761. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  762. CLEAR_AFTER_FIELD(f, fmt.pix);
  763. if (ops->vidioc_try_fmt_vid_out)
  764. ret = ops->vidioc_try_fmt_vid_out(file, fh, f);
  765. if (!ret)
  766. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  767. break;
  768. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  769. CLEAR_AFTER_FIELD(f, fmt.pix_mp);
  770. if (ops->vidioc_try_fmt_vid_out_mplane)
  771. ret = ops->vidioc_try_fmt_vid_out_mplane(file,
  772. fh, f);
  773. if (!ret)
  774. v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
  775. break;
  776. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  777. CLEAR_AFTER_FIELD(f, fmt.win);
  778. if (likely(ops->vidioc_try_fmt_vid_out_overlay))
  779. ret = ops->vidioc_try_fmt_vid_out_overlay(file,
  780. fh, f);
  781. break;
  782. case V4L2_BUF_TYPE_VBI_CAPTURE:
  783. CLEAR_AFTER_FIELD(f, fmt.vbi);
  784. if (likely(ops->vidioc_try_fmt_vbi_cap))
  785. ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f);
  786. break;
  787. case V4L2_BUF_TYPE_VBI_OUTPUT:
  788. CLEAR_AFTER_FIELD(f, fmt.vbi);
  789. if (likely(ops->vidioc_try_fmt_vbi_out))
  790. ret = ops->vidioc_try_fmt_vbi_out(file, fh, f);
  791. break;
  792. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  793. CLEAR_AFTER_FIELD(f, fmt.sliced);
  794. if (likely(ops->vidioc_try_fmt_sliced_vbi_cap))
  795. ret = ops->vidioc_try_fmt_sliced_vbi_cap(file,
  796. fh, f);
  797. break;
  798. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  799. CLEAR_AFTER_FIELD(f, fmt.sliced);
  800. if (likely(ops->vidioc_try_fmt_sliced_vbi_out))
  801. ret = ops->vidioc_try_fmt_sliced_vbi_out(file,
  802. fh, f);
  803. break;
  804. case V4L2_BUF_TYPE_PRIVATE:
  805. /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
  806. if (likely(ops->vidioc_try_fmt_type_private))
  807. ret = ops->vidioc_try_fmt_type_private(file,
  808. fh, f);
  809. break;
  810. }
  811. if (unlikely(ret == -ENOTTY && have_fmt_ops(try)))
  812. ret = -EINVAL;
  813. break;
  814. }
  815. /* FIXME: Those buf reqs could be handled here,
  816. with some changes on videobuf to allow its header to be included at
  817. videodev2.h or being merged at videodev2.
  818. */
  819. case VIDIOC_REQBUFS:
  820. {
  821. struct v4l2_requestbuffers *p = arg;
  822. if (!ops->vidioc_reqbufs)
  823. break;
  824. if (ret_prio) {
  825. ret = ret_prio;
  826. break;
  827. }
  828. ret = check_fmt(ops, p->type);
  829. if (ret)
  830. break;
  831. if (p->type < V4L2_BUF_TYPE_PRIVATE)
  832. CLEAR_AFTER_FIELD(p, memory);
  833. ret = ops->vidioc_reqbufs(file, fh, p);
  834. dbgarg(cmd, "count=%d, type=%s, memory=%s\n",
  835. p->count,
  836. prt_names(p->type, v4l2_type_names),
  837. prt_names(p->memory, v4l2_memory_names));
  838. break;
  839. }
  840. case VIDIOC_QUERYBUF:
  841. {
  842. struct v4l2_buffer *p = arg;
  843. if (!ops->vidioc_querybuf)
  844. break;
  845. ret = check_fmt(ops, p->type);
  846. if (ret)
  847. break;
  848. ret = ops->vidioc_querybuf(file, fh, p);
  849. if (!ret)
  850. dbgbuf(cmd, vfd, p);
  851. break;
  852. }
  853. case VIDIOC_QBUF:
  854. {
  855. struct v4l2_buffer *p = arg;
  856. if (!ops->vidioc_qbuf)
  857. break;
  858. ret = check_fmt(ops, p->type);
  859. if (ret)
  860. break;
  861. ret = ops->vidioc_qbuf(file, fh, p);
  862. if (!ret)
  863. dbgbuf(cmd, vfd, p);
  864. break;
  865. }
  866. case VIDIOC_DQBUF:
  867. {
  868. struct v4l2_buffer *p = arg;
  869. if (!ops->vidioc_dqbuf)
  870. break;
  871. ret = check_fmt(ops, p->type);
  872. if (ret)
  873. break;
  874. ret = ops->vidioc_dqbuf(file, fh, p);
  875. if (!ret)
  876. dbgbuf(cmd, vfd, p);
  877. break;
  878. }
  879. case VIDIOC_OVERLAY:
  880. {
  881. int *i = arg;
  882. if (!ops->vidioc_overlay)
  883. break;
  884. if (ret_prio) {
  885. ret = ret_prio;
  886. break;
  887. }
  888. dbgarg(cmd, "value=%d\n", *i);
  889. ret = ops->vidioc_overlay(file, fh, *i);
  890. break;
  891. }
  892. case VIDIOC_G_FBUF:
  893. {
  894. struct v4l2_framebuffer *p = arg;
  895. if (!ops->vidioc_g_fbuf)
  896. break;
  897. ret = ops->vidioc_g_fbuf(file, fh, arg);
  898. if (!ret) {
  899. dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
  900. p->capability, p->flags,
  901. (unsigned long)p->base);
  902. v4l_print_pix_fmt(vfd, &p->fmt);
  903. }
  904. break;
  905. }
  906. case VIDIOC_S_FBUF:
  907. {
  908. struct v4l2_framebuffer *p = arg;
  909. if (!ops->vidioc_s_fbuf)
  910. break;
  911. if (ret_prio) {
  912. ret = ret_prio;
  913. break;
  914. }
  915. dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
  916. p->capability, p->flags, (unsigned long)p->base);
  917. v4l_print_pix_fmt(vfd, &p->fmt);
  918. ret = ops->vidioc_s_fbuf(file, fh, arg);
  919. break;
  920. }
  921. case VIDIOC_STREAMON:
  922. {
  923. enum v4l2_buf_type i = *(int *)arg;
  924. if (!ops->vidioc_streamon)
  925. break;
  926. if (ret_prio) {
  927. ret = ret_prio;
  928. break;
  929. }
  930. dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
  931. ret = ops->vidioc_streamon(file, fh, i);
  932. break;
  933. }
  934. case VIDIOC_STREAMOFF:
  935. {
  936. enum v4l2_buf_type i = *(int *)arg;
  937. if (!ops->vidioc_streamoff)
  938. break;
  939. if (ret_prio) {
  940. ret = ret_prio;
  941. break;
  942. }
  943. dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
  944. ret = ops->vidioc_streamoff(file, fh, i);
  945. break;
  946. }
  947. /* ---------- tv norms ---------- */
  948. case VIDIOC_ENUMSTD:
  949. {
  950. struct v4l2_standard *p = arg;
  951. v4l2_std_id id = vfd->tvnorms, curr_id = 0;
  952. unsigned int index = p->index, i, j = 0;
  953. const char *descr = "";
  954. if (id == 0)
  955. break;
  956. ret = -EINVAL;
  957. /* Return norm array in a canonical way */
  958. for (i = 0; i <= index && id; i++) {
  959. /* last std value in the standards array is 0, so this
  960. while always ends there since (id & 0) == 0. */
  961. while ((id & standards[j].std) != standards[j].std)
  962. j++;
  963. curr_id = standards[j].std;
  964. descr = standards[j].descr;
  965. j++;
  966. if (curr_id == 0)
  967. break;
  968. if (curr_id != V4L2_STD_PAL &&
  969. curr_id != V4L2_STD_SECAM &&
  970. curr_id != V4L2_STD_NTSC)
  971. id &= ~curr_id;
  972. }
  973. if (i <= index)
  974. break;
  975. v4l2_video_std_construct(p, curr_id, descr);
  976. dbgarg(cmd, "index=%d, id=0x%Lx, name=%s, fps=%d/%d, "
  977. "framelines=%d\n", p->index,
  978. (unsigned long long)p->id, p->name,
  979. p->frameperiod.numerator,
  980. p->frameperiod.denominator,
  981. p->framelines);
  982. ret = 0;
  983. break;
  984. }
  985. case VIDIOC_G_STD:
  986. {
  987. v4l2_std_id *id = arg;
  988. /* Calls the specific handler */
  989. if (ops->vidioc_g_std)
  990. ret = ops->vidioc_g_std(file, fh, id);
  991. else if (vfd->current_norm) {
  992. ret = 0;
  993. *id = vfd->current_norm;
  994. }
  995. if (likely(!ret))
  996. dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
  997. break;
  998. }
  999. case VIDIOC_S_STD:
  1000. {
  1001. v4l2_std_id *id = arg, norm;
  1002. dbgarg(cmd, "std=%08Lx\n", (long long unsigned)*id);
  1003. if (!ops->vidioc_s_std)
  1004. break;
  1005. if (ret_prio) {
  1006. ret = ret_prio;
  1007. break;
  1008. }
  1009. ret = -EINVAL;
  1010. norm = (*id) & vfd->tvnorms;
  1011. if (vfd->tvnorms && !norm) /* Check if std is supported */
  1012. break;
  1013. /* Calls the specific handler */
  1014. ret = ops->vidioc_s_std(file, fh, &norm);
  1015. /* Updates standard information */
  1016. if (ret >= 0)
  1017. vfd->current_norm = norm;
  1018. break;
  1019. }
  1020. case VIDIOC_QUERYSTD:
  1021. {
  1022. v4l2_std_id *p = arg;
  1023. if (!ops->vidioc_querystd)
  1024. break;
  1025. /*
  1026. * If nothing detected, it should return all supported
  1027. * Drivers just need to mask the std argument, in order
  1028. * to remove the standards that don't apply from the mask.
  1029. * This means that tuners, audio and video decoders can join
  1030. * their efforts to improve the standards detection
  1031. */
  1032. *p = vfd->tvnorms;
  1033. ret = ops->vidioc_querystd(file, fh, arg);
  1034. if (!ret)
  1035. dbgarg(cmd, "detected std=%08Lx\n",
  1036. (unsigned long long)*p);
  1037. break;
  1038. }
  1039. /* ------ input switching ---------- */
  1040. /* FIXME: Inputs can be handled inside videodev2 */
  1041. case VIDIOC_ENUMINPUT:
  1042. {
  1043. struct v4l2_input *p = arg;
  1044. /*
  1045. * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
  1046. * CAP_STD here based on ioctl handler provided by the
  1047. * driver. If the driver doesn't support these
  1048. * for a specific input, it must override these flags.
  1049. */
  1050. if (ops->vidioc_s_std)
  1051. p->capabilities |= V4L2_IN_CAP_STD;
  1052. if (ops->vidioc_s_dv_preset)
  1053. p->capabilities |= V4L2_IN_CAP_PRESETS;
  1054. if (ops->vidioc_s_dv_timings)
  1055. p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
  1056. if (!ops->vidioc_enum_input)
  1057. break;
  1058. ret = ops->vidioc_enum_input(file, fh, p);
  1059. if (!ret)
  1060. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1061. "audioset=%d, "
  1062. "tuner=%d, std=%08Lx, status=%d\n",
  1063. p->index, p->name, p->type, p->audioset,
  1064. p->tuner,
  1065. (unsigned long long)p->std,
  1066. p->status);
  1067. break;
  1068. }
  1069. case VIDIOC_G_INPUT:
  1070. {
  1071. unsigned int *i = arg;
  1072. if (!ops->vidioc_g_input)
  1073. break;
  1074. ret = ops->vidioc_g_input(file, fh, i);
  1075. if (!ret)
  1076. dbgarg(cmd, "value=%d\n", *i);
  1077. break;
  1078. }
  1079. case VIDIOC_S_INPUT:
  1080. {
  1081. unsigned int *i = arg;
  1082. if (!ops->vidioc_s_input)
  1083. break;
  1084. if (ret_prio) {
  1085. ret = ret_prio;
  1086. break;
  1087. }
  1088. dbgarg(cmd, "value=%d\n", *i);
  1089. ret = ops->vidioc_s_input(file, fh, *i);
  1090. break;
  1091. }
  1092. /* ------ output switching ---------- */
  1093. case VIDIOC_ENUMOUTPUT:
  1094. {
  1095. struct v4l2_output *p = arg;
  1096. if (!ops->vidioc_enum_output)
  1097. break;
  1098. /*
  1099. * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
  1100. * CAP_STD here based on ioctl handler provided by the
  1101. * driver. If the driver doesn't support these
  1102. * for a specific output, it must override these flags.
  1103. */
  1104. if (ops->vidioc_s_std)
  1105. p->capabilities |= V4L2_OUT_CAP_STD;
  1106. if (ops->vidioc_s_dv_preset)
  1107. p->capabilities |= V4L2_OUT_CAP_PRESETS;
  1108. if (ops->vidioc_s_dv_timings)
  1109. p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
  1110. ret = ops->vidioc_enum_output(file, fh, p);
  1111. if (!ret)
  1112. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1113. "audioset=0x%x, "
  1114. "modulator=%d, std=0x%08Lx\n",
  1115. p->index, p->name, p->type, p->audioset,
  1116. p->modulator, (unsigned long long)p->std);
  1117. break;
  1118. }
  1119. case VIDIOC_G_OUTPUT:
  1120. {
  1121. unsigned int *i = arg;
  1122. if (!ops->vidioc_g_output)
  1123. break;
  1124. ret = ops->vidioc_g_output(file, fh, i);
  1125. if (!ret)
  1126. dbgarg(cmd, "value=%d\n", *i);
  1127. break;
  1128. }
  1129. case VIDIOC_S_OUTPUT:
  1130. {
  1131. unsigned int *i = arg;
  1132. if (!ops->vidioc_s_output)
  1133. break;
  1134. if (ret_prio) {
  1135. ret = ret_prio;
  1136. break;
  1137. }
  1138. dbgarg(cmd, "value=%d\n", *i);
  1139. ret = ops->vidioc_s_output(file, fh, *i);
  1140. break;
  1141. }
  1142. /* --- controls ---------------------------------------------- */
  1143. case VIDIOC_QUERYCTRL:
  1144. {
  1145. struct v4l2_queryctrl *p = arg;
  1146. if (vfh && vfh->ctrl_handler)
  1147. ret = v4l2_queryctrl(vfh->ctrl_handler, p);
  1148. else if (vfd->ctrl_handler)
  1149. ret = v4l2_queryctrl(vfd->ctrl_handler, p);
  1150. else if (ops->vidioc_queryctrl)
  1151. ret = ops->vidioc_queryctrl(file, fh, p);
  1152. else
  1153. break;
  1154. if (!ret)
  1155. dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
  1156. "step=%d, default=%d, flags=0x%08x\n",
  1157. p->id, p->type, p->name,
  1158. p->minimum, p->maximum,
  1159. p->step, p->default_value, p->flags);
  1160. else
  1161. dbgarg(cmd, "id=0x%x\n", p->id);
  1162. break;
  1163. }
  1164. case VIDIOC_G_CTRL:
  1165. {
  1166. struct v4l2_control *p = arg;
  1167. if (vfh && vfh->ctrl_handler)
  1168. ret = v4l2_g_ctrl(vfh->ctrl_handler, p);
  1169. else if (vfd->ctrl_handler)
  1170. ret = v4l2_g_ctrl(vfd->ctrl_handler, p);
  1171. else if (ops->vidioc_g_ctrl)
  1172. ret = ops->vidioc_g_ctrl(file, fh, p);
  1173. else if (ops->vidioc_g_ext_ctrls) {
  1174. struct v4l2_ext_controls ctrls;
  1175. struct v4l2_ext_control ctrl;
  1176. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1177. ctrls.count = 1;
  1178. ctrls.controls = &ctrl;
  1179. ctrl.id = p->id;
  1180. ctrl.value = p->value;
  1181. if (check_ext_ctrls(&ctrls, 1)) {
  1182. ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
  1183. if (ret == 0)
  1184. p->value = ctrl.value;
  1185. }
  1186. } else
  1187. break;
  1188. if (!ret)
  1189. dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
  1190. else
  1191. dbgarg(cmd, "id=0x%x\n", p->id);
  1192. break;
  1193. }
  1194. case VIDIOC_S_CTRL:
  1195. {
  1196. struct v4l2_control *p = arg;
  1197. struct v4l2_ext_controls ctrls;
  1198. struct v4l2_ext_control ctrl;
  1199. if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
  1200. !ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
  1201. break;
  1202. if (ret_prio) {
  1203. ret = ret_prio;
  1204. break;
  1205. }
  1206. dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
  1207. if (vfh && vfh->ctrl_handler) {
  1208. ret = v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
  1209. break;
  1210. }
  1211. if (vfd->ctrl_handler) {
  1212. ret = v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
  1213. break;
  1214. }
  1215. if (ops->vidioc_s_ctrl) {
  1216. ret = ops->vidioc_s_ctrl(file, fh, p);
  1217. break;
  1218. }
  1219. if (!ops->vidioc_s_ext_ctrls)
  1220. break;
  1221. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1222. ctrls.count = 1;
  1223. ctrls.controls = &ctrl;
  1224. ctrl.id = p->id;
  1225. ctrl.value = p->value;
  1226. if (check_ext_ctrls(&ctrls, 1))
  1227. ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
  1228. else
  1229. ret = -EINVAL;
  1230. break;
  1231. }
  1232. case VIDIOC_G_EXT_CTRLS:
  1233. {
  1234. struct v4l2_ext_controls *p = arg;
  1235. p->error_idx = p->count;
  1236. if (vfh && vfh->ctrl_handler)
  1237. ret = v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
  1238. else if (vfd->ctrl_handler)
  1239. ret = v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
  1240. else if (ops->vidioc_g_ext_ctrls)
  1241. ret = check_ext_ctrls(p, 0) ?
  1242. ops->vidioc_g_ext_ctrls(file, fh, p) :
  1243. -EINVAL;
  1244. else
  1245. break;
  1246. v4l_print_ext_ctrls(cmd, vfd, p, !ret);
  1247. break;
  1248. }
  1249. case VIDIOC_S_EXT_CTRLS:
  1250. {
  1251. struct v4l2_ext_controls *p = arg;
  1252. p->error_idx = p->count;
  1253. if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
  1254. !ops->vidioc_s_ext_ctrls)
  1255. break;
  1256. if (ret_prio) {
  1257. ret = ret_prio;
  1258. break;
  1259. }
  1260. v4l_print_ext_ctrls(cmd, vfd, p, 1);
  1261. if (vfh && vfh->ctrl_handler)
  1262. ret = v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
  1263. else if (vfd->ctrl_handler)
  1264. ret = v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
  1265. else if (check_ext_ctrls(p, 0))
  1266. ret = ops->vidioc_s_ext_ctrls(file, fh, p);
  1267. else
  1268. ret = -EINVAL;
  1269. break;
  1270. }
  1271. case VIDIOC_TRY_EXT_CTRLS:
  1272. {
  1273. struct v4l2_ext_controls *p = arg;
  1274. p->error_idx = p->count;
  1275. if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
  1276. !ops->vidioc_try_ext_ctrls)
  1277. break;
  1278. v4l_print_ext_ctrls(cmd, vfd, p, 1);
  1279. if (vfh && vfh->ctrl_handler)
  1280. ret = v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
  1281. else if (vfd->ctrl_handler)
  1282. ret = v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
  1283. else if (check_ext_ctrls(p, 0))
  1284. ret = ops->vidioc_try_ext_ctrls(file, fh, p);
  1285. else
  1286. ret = -EINVAL;
  1287. break;
  1288. }
  1289. case VIDIOC_QUERYMENU:
  1290. {
  1291. struct v4l2_querymenu *p = arg;
  1292. if (vfh && vfh->ctrl_handler)
  1293. ret = v4l2_querymenu(vfh->ctrl_handler, p);
  1294. else if (vfd->ctrl_handler)
  1295. ret = v4l2_querymenu(vfd->ctrl_handler, p);
  1296. else if (ops->vidioc_querymenu)
  1297. ret = ops->vidioc_querymenu(file, fh, p);
  1298. else
  1299. break;
  1300. if (!ret)
  1301. dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
  1302. p->id, p->index, p->name);
  1303. else
  1304. dbgarg(cmd, "id=0x%x, index=%d\n",
  1305. p->id, p->index);
  1306. break;
  1307. }
  1308. /* --- audio ---------------------------------------------- */
  1309. case VIDIOC_ENUMAUDIO:
  1310. {
  1311. struct v4l2_audio *p = arg;
  1312. if (!ops->vidioc_enumaudio)
  1313. break;
  1314. ret = ops->vidioc_enumaudio(file, fh, p);
  1315. if (!ret)
  1316. dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
  1317. "mode=0x%x\n", p->index, p->name,
  1318. p->capability, p->mode);
  1319. else
  1320. dbgarg(cmd, "index=%d\n", p->index);
  1321. break;
  1322. }
  1323. case VIDIOC_G_AUDIO:
  1324. {
  1325. struct v4l2_audio *p = arg;
  1326. if (!ops->vidioc_g_audio)
  1327. break;
  1328. ret = ops->vidioc_g_audio(file, fh, p);
  1329. if (!ret)
  1330. dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
  1331. "mode=0x%x\n", p->index,
  1332. p->name, p->capability, p->mode);
  1333. else
  1334. dbgarg(cmd, "index=%d\n", p->index);
  1335. break;
  1336. }
  1337. case VIDIOC_S_AUDIO:
  1338. {
  1339. struct v4l2_audio *p = arg;
  1340. if (!ops->vidioc_s_audio)
  1341. break;
  1342. if (ret_prio) {
  1343. ret = ret_prio;
  1344. break;
  1345. }
  1346. dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
  1347. "mode=0x%x\n", p->index, p->name,
  1348. p->capability, p->mode);
  1349. ret = ops->vidioc_s_audio(file, fh, p);
  1350. break;
  1351. }
  1352. case VIDIOC_ENUMAUDOUT:
  1353. {
  1354. struct v4l2_audioout *p = arg;
  1355. if (!ops->vidioc_enumaudout)
  1356. break;
  1357. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1358. ret = ops->vidioc_enumaudout(file, fh, p);
  1359. if (!ret)
  1360. dbgarg2("index=%d, name=%s, capability=%d, "
  1361. "mode=%d\n", p->index, p->name,
  1362. p->capability, p->mode);
  1363. break;
  1364. }
  1365. case VIDIOC_G_AUDOUT:
  1366. {
  1367. struct v4l2_audioout *p = arg;
  1368. if (!ops->vidioc_g_audout)
  1369. break;
  1370. ret = ops->vidioc_g_audout(file, fh, p);
  1371. if (!ret)
  1372. dbgarg2("index=%d, name=%s, capability=%d, "
  1373. "mode=%d\n", p->index, p->name,
  1374. p->capability, p->mode);
  1375. break;
  1376. }
  1377. case VIDIOC_S_AUDOUT:
  1378. {
  1379. struct v4l2_audioout *p = arg;
  1380. if (!ops->vidioc_s_audout)
  1381. break;
  1382. if (ret_prio) {
  1383. ret = ret_prio;
  1384. break;
  1385. }
  1386. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1387. "mode=%d\n", p->index, p->name,
  1388. p->capability, p->mode);
  1389. ret = ops->vidioc_s_audout(file, fh, p);
  1390. break;
  1391. }
  1392. case VIDIOC_G_MODULATOR:
  1393. {
  1394. struct v4l2_modulator *p = arg;
  1395. if (!ops->vidioc_g_modulator)
  1396. break;
  1397. ret = ops->vidioc_g_modulator(file, fh, p);
  1398. if (!ret)
  1399. dbgarg(cmd, "index=%d, name=%s, "
  1400. "capability=%d, rangelow=%d,"
  1401. " rangehigh=%d, txsubchans=%d\n",
  1402. p->index, p->name, p->capability,
  1403. p->rangelow, p->rangehigh,
  1404. p->txsubchans);
  1405. break;
  1406. }
  1407. case VIDIOC_S_MODULATOR:
  1408. {
  1409. struct v4l2_modulator *p = arg;
  1410. if (!ops->vidioc_s_modulator)
  1411. break;
  1412. if (ret_prio) {
  1413. ret = ret_prio;
  1414. break;
  1415. }
  1416. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1417. "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
  1418. p->index, p->name, p->capability, p->rangelow,
  1419. p->rangehigh, p->txsubchans);
  1420. ret = ops->vidioc_s_modulator(file, fh, p);
  1421. break;
  1422. }
  1423. case VIDIOC_G_CROP:
  1424. {
  1425. struct v4l2_crop *p = arg;
  1426. if (!ops->vidioc_g_crop && !ops->vidioc_g_selection)
  1427. break;
  1428. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1429. if (ops->vidioc_g_crop) {
  1430. ret = ops->vidioc_g_crop(file, fh, p);
  1431. } else {
  1432. /* simulate capture crop using selection api */
  1433. struct v4l2_selection s = {
  1434. .type = p->type,
  1435. };
  1436. /* crop means compose for output devices */
  1437. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1438. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1439. else
  1440. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1441. ret = ops->vidioc_g_selection(file, fh, &s);
  1442. /* copying results to old structure on success */
  1443. if (!ret)
  1444. p->c = s.r;
  1445. }
  1446. if (!ret)
  1447. dbgrect(vfd, "", &p->c);
  1448. break;
  1449. }
  1450. case VIDIOC_S_CROP:
  1451. {
  1452. struct v4l2_crop *p = arg;
  1453. if (!ops->vidioc_s_crop && !ops->vidioc_s_selection)
  1454. break;
  1455. if (ret_prio) {
  1456. ret = ret_prio;
  1457. break;
  1458. }
  1459. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1460. dbgrect(vfd, "", &p->c);
  1461. if (ops->vidioc_s_crop) {
  1462. ret = ops->vidioc_s_crop(file, fh, p);
  1463. } else {
  1464. /* simulate capture crop using selection api */
  1465. struct v4l2_selection s = {
  1466. .type = p->type,
  1467. .r = p->c,
  1468. };
  1469. /* crop means compose for output devices */
  1470. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1471. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1472. else
  1473. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1474. ret = ops->vidioc_s_selection(file, fh, &s);
  1475. }
  1476. break;
  1477. }
  1478. case VIDIOC_G_SELECTION:
  1479. {
  1480. struct v4l2_selection *p = arg;
  1481. if (!ops->vidioc_g_selection)
  1482. break;
  1483. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1484. ret = ops->vidioc_g_selection(file, fh, p);
  1485. if (!ret)
  1486. dbgrect(vfd, "", &p->r);
  1487. break;
  1488. }
  1489. case VIDIOC_S_SELECTION:
  1490. {
  1491. struct v4l2_selection *p = arg;
  1492. if (!ops->vidioc_s_selection)
  1493. break;
  1494. if (ret_prio) {
  1495. ret = ret_prio;
  1496. break;
  1497. }
  1498. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1499. dbgrect(vfd, "", &p->r);
  1500. ret = ops->vidioc_s_selection(file, fh, p);
  1501. break;
  1502. }
  1503. case VIDIOC_CROPCAP:
  1504. {
  1505. struct v4l2_cropcap *p = arg;
  1506. /*FIXME: Should also show v4l2_fract pixelaspect */
  1507. if (!ops->vidioc_cropcap && !ops->vidioc_g_selection)
  1508. break;
  1509. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1510. if (ops->vidioc_cropcap) {
  1511. ret = ops->vidioc_cropcap(file, fh, p);
  1512. } else {
  1513. struct v4l2_selection s = { .type = p->type };
  1514. /* obtaining bounds */
  1515. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1516. s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
  1517. else
  1518. s.target = V4L2_SEL_TGT_CROP_BOUNDS;
  1519. ret = ops->vidioc_g_selection(file, fh, &s);
  1520. if (ret)
  1521. break;
  1522. p->bounds = s.r;
  1523. /* obtaining defrect */
  1524. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1525. s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
  1526. else
  1527. s.target = V4L2_SEL_TGT_CROP_DEFAULT;
  1528. ret = ops->vidioc_g_selection(file, fh, &s);
  1529. if (ret)
  1530. break;
  1531. p->defrect = s.r;
  1532. /* setting trivial pixelaspect */
  1533. p->pixelaspect.numerator = 1;
  1534. p->pixelaspect.denominator = 1;
  1535. }
  1536. if (!ret) {
  1537. dbgrect(vfd, "bounds ", &p->bounds);
  1538. dbgrect(vfd, "defrect ", &p->defrect);
  1539. }
  1540. break;
  1541. }
  1542. case VIDIOC_G_JPEGCOMP:
  1543. {
  1544. struct v4l2_jpegcompression *p = arg;
  1545. if (!ops->vidioc_g_jpegcomp)
  1546. break;
  1547. ret = ops->vidioc_g_jpegcomp(file, fh, p);
  1548. if (!ret)
  1549. dbgarg(cmd, "quality=%d, APPn=%d, "
  1550. "APP_len=%d, COM_len=%d, "
  1551. "jpeg_markers=%d\n",
  1552. p->quality, p->APPn, p->APP_len,
  1553. p->COM_len, p->jpeg_markers);
  1554. break;
  1555. }
  1556. case VIDIOC_S_JPEGCOMP:
  1557. {
  1558. struct v4l2_jpegcompression *p = arg;
  1559. if (!ops->vidioc_g_jpegcomp)
  1560. break;
  1561. if (ret_prio) {
  1562. ret = ret_prio;
  1563. break;
  1564. }
  1565. dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
  1566. "COM_len=%d, jpeg_markers=%d\n",
  1567. p->quality, p->APPn, p->APP_len,
  1568. p->COM_len, p->jpeg_markers);
  1569. ret = ops->vidioc_s_jpegcomp(file, fh, p);
  1570. break;
  1571. }
  1572. case VIDIOC_G_ENC_INDEX:
  1573. {
  1574. struct v4l2_enc_idx *p = arg;
  1575. if (!ops->vidioc_g_enc_index)
  1576. break;
  1577. ret = ops->vidioc_g_enc_index(file, fh, p);
  1578. if (!ret)
  1579. dbgarg(cmd, "entries=%d, entries_cap=%d\n",
  1580. p->entries, p->entries_cap);
  1581. break;
  1582. }
  1583. case VIDIOC_ENCODER_CMD:
  1584. {
  1585. struct v4l2_encoder_cmd *p = arg;
  1586. if (!ops->vidioc_encoder_cmd)
  1587. break;
  1588. if (ret_prio) {
  1589. ret = ret_prio;
  1590. break;
  1591. }
  1592. ret = ops->vidioc_encoder_cmd(file, fh, p);
  1593. if (!ret)
  1594. dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
  1595. break;
  1596. }
  1597. case VIDIOC_TRY_ENCODER_CMD:
  1598. {
  1599. struct v4l2_encoder_cmd *p = arg;
  1600. if (!ops->vidioc_try_encoder_cmd)
  1601. break;
  1602. ret = ops->vidioc_try_encoder_cmd(file, fh, p);
  1603. if (!ret)
  1604. dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
  1605. break;
  1606. }
  1607. case VIDIOC_G_PARM:
  1608. {
  1609. struct v4l2_streamparm *p = arg;
  1610. if (!ops->vidioc_g_parm && !vfd->current_norm)
  1611. break;
  1612. if (ops->vidioc_g_parm) {
  1613. ret = check_fmt(ops, p->type);
  1614. if (ret)
  1615. break;
  1616. ret = ops->vidioc_g_parm(file, fh, p);
  1617. } else {
  1618. v4l2_std_id std = vfd->current_norm;
  1619. ret = -EINVAL;
  1620. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1621. break;
  1622. ret = 0;
  1623. if (ops->vidioc_g_std)
  1624. ret = ops->vidioc_g_std(file, fh, &std);
  1625. if (ret == 0)
  1626. v4l2_video_std_frame_period(std,
  1627. &p->parm.capture.timeperframe);
  1628. }
  1629. dbgarg(cmd, "type=%d\n", p->type);
  1630. break;
  1631. }
  1632. case VIDIOC_S_PARM:
  1633. {
  1634. struct v4l2_streamparm *p = arg;
  1635. if (!ops->vidioc_s_parm)
  1636. break;
  1637. if (ret_prio) {
  1638. ret = ret_prio;
  1639. break;
  1640. }
  1641. ret = check_fmt(ops, p->type);
  1642. if (ret)
  1643. break;
  1644. dbgarg(cmd, "type=%d\n", p->type);
  1645. ret = ops->vidioc_s_parm(file, fh, p);
  1646. break;
  1647. }
  1648. case VIDIOC_G_TUNER:
  1649. {
  1650. struct v4l2_tuner *p = arg;
  1651. if (!ops->vidioc_g_tuner)
  1652. break;
  1653. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1654. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1655. ret = ops->vidioc_g_tuner(file, fh, p);
  1656. if (!ret)
  1657. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1658. "capability=0x%x, rangelow=%d, "
  1659. "rangehigh=%d, signal=%d, afc=%d, "
  1660. "rxsubchans=0x%x, audmode=%d\n",
  1661. p->index, p->name, p->type,
  1662. p->capability, p->rangelow,
  1663. p->rangehigh, p->signal, p->afc,
  1664. p->rxsubchans, p->audmode);
  1665. break;
  1666. }
  1667. case VIDIOC_S_TUNER:
  1668. {
  1669. struct v4l2_tuner *p = arg;
  1670. if (!ops->vidioc_s_tuner)
  1671. break;
  1672. if (ret_prio) {
  1673. ret = ret_prio;
  1674. break;
  1675. }
  1676. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1677. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1678. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1679. "capability=0x%x, rangelow=%d, "
  1680. "rangehigh=%d, signal=%d, afc=%d, "
  1681. "rxsubchans=0x%x, audmode=%d\n",
  1682. p->index, p->name, p->type,
  1683. p->capability, p->rangelow,
  1684. p->rangehigh, p->signal, p->afc,
  1685. p->rxsubchans, p->audmode);
  1686. ret = ops->vidioc_s_tuner(file, fh, p);
  1687. break;
  1688. }
  1689. case VIDIOC_G_FREQUENCY:
  1690. {
  1691. struct v4l2_frequency *p = arg;
  1692. if (!ops->vidioc_g_frequency)
  1693. break;
  1694. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1695. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1696. ret = ops->vidioc_g_frequency(file, fh, p);
  1697. if (!ret)
  1698. dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
  1699. p->tuner, p->type, p->frequency);
  1700. break;
  1701. }
  1702. case VIDIOC_S_FREQUENCY:
  1703. {
  1704. struct v4l2_frequency *p = arg;
  1705. enum v4l2_tuner_type type;
  1706. if (!ops->vidioc_s_frequency)
  1707. break;
  1708. if (ret_prio) {
  1709. ret = ret_prio;
  1710. break;
  1711. }
  1712. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1713. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1714. dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
  1715. p->tuner, p->type, p->frequency);
  1716. if (p->type != type)
  1717. ret = -EINVAL;
  1718. else
  1719. ret = ops->vidioc_s_frequency(file, fh, p);
  1720. break;
  1721. }
  1722. case VIDIOC_G_SLICED_VBI_CAP:
  1723. {
  1724. struct v4l2_sliced_vbi_cap *p = arg;
  1725. if (!ops->vidioc_g_sliced_vbi_cap)
  1726. break;
  1727. /* Clear up to type, everything after type is zerod already */
  1728. memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
  1729. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1730. ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
  1731. if (!ret)
  1732. dbgarg2("service_set=%d\n", p->service_set);
  1733. break;
  1734. }
  1735. case VIDIOC_LOG_STATUS:
  1736. {
  1737. if (!ops->vidioc_log_status)
  1738. break;
  1739. ret = ops->vidioc_log_status(file, fh);
  1740. break;
  1741. }
  1742. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1743. case VIDIOC_DBG_G_REGISTER:
  1744. {
  1745. struct v4l2_dbg_register *p = arg;
  1746. if (ops->vidioc_g_register) {
  1747. if (!capable(CAP_SYS_ADMIN))
  1748. ret = -EPERM;
  1749. else
  1750. ret = ops->vidioc_g_register(file, fh, p);
  1751. }
  1752. break;
  1753. }
  1754. case VIDIOC_DBG_S_REGISTER:
  1755. {
  1756. struct v4l2_dbg_register *p = arg;
  1757. if (ops->vidioc_s_register) {
  1758. if (!capable(CAP_SYS_ADMIN))
  1759. ret = -EPERM;
  1760. else
  1761. ret = ops->vidioc_s_register(file, fh, p);
  1762. }
  1763. break;
  1764. }
  1765. #endif
  1766. case VIDIOC_DBG_G_CHIP_IDENT:
  1767. {
  1768. struct v4l2_dbg_chip_ident *p = arg;
  1769. if (!ops->vidioc_g_chip_ident)
  1770. break;
  1771. p->ident = V4L2_IDENT_NONE;
  1772. p->revision = 0;
  1773. ret = ops->vidioc_g_chip_ident(file, fh, p);
  1774. if (!ret)
  1775. dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
  1776. break;
  1777. }
  1778. case VIDIOC_S_HW_FREQ_SEEK:
  1779. {
  1780. struct v4l2_hw_freq_seek *p = arg;
  1781. enum v4l2_tuner_type type;
  1782. if (!ops->vidioc_s_hw_freq_seek)
  1783. break;
  1784. if (ret_prio) {
  1785. ret = ret_prio;
  1786. break;
  1787. }
  1788. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1789. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1790. dbgarg(cmd,
  1791. "tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u\n",
  1792. p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing);
  1793. if (p->type != type)
  1794. ret = -EINVAL;
  1795. else
  1796. ret = ops->vidioc_s_hw_freq_seek(file, fh, p);
  1797. break;
  1798. }
  1799. case VIDIOC_ENUM_FRAMESIZES:
  1800. {
  1801. struct v4l2_frmsizeenum *p = arg;
  1802. if (!ops->vidioc_enum_framesizes)
  1803. break;
  1804. ret = ops->vidioc_enum_framesizes(file, fh, p);
  1805. dbgarg(cmd,
  1806. "index=%d, pixelformat=%c%c%c%c, type=%d ",
  1807. p->index,
  1808. (p->pixel_format & 0xff),
  1809. (p->pixel_format >> 8) & 0xff,
  1810. (p->pixel_format >> 16) & 0xff,
  1811. (p->pixel_format >> 24) & 0xff,
  1812. p->type);
  1813. switch (p->type) {
  1814. case V4L2_FRMSIZE_TYPE_DISCRETE:
  1815. dbgarg3("width = %d, height=%d\n",
  1816. p->discrete.width, p->discrete.height);
  1817. break;
  1818. case V4L2_FRMSIZE_TYPE_STEPWISE:
  1819. dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
  1820. p->stepwise.min_width, p->stepwise.min_height,
  1821. p->stepwise.step_width, p->stepwise.step_height,
  1822. p->stepwise.max_width, p->stepwise.max_height);
  1823. break;
  1824. case V4L2_FRMSIZE_TYPE_CONTINUOUS:
  1825. dbgarg3("continuous\n");
  1826. break;
  1827. default:
  1828. dbgarg3("- Unknown type!\n");
  1829. }
  1830. break;
  1831. }
  1832. case VIDIOC_ENUM_FRAMEINTERVALS:
  1833. {
  1834. struct v4l2_frmivalenum *p = arg;
  1835. if (!ops->vidioc_enum_frameintervals)
  1836. break;
  1837. ret = ops->vidioc_enum_frameintervals(file, fh, p);
  1838. dbgarg(cmd,
  1839. "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
  1840. p->index, p->pixel_format,
  1841. p->width, p->height, p->type);
  1842. switch (p->type) {
  1843. case V4L2_FRMIVAL_TYPE_DISCRETE:
  1844. dbgarg2("fps=%d/%d\n",
  1845. p->discrete.numerator,
  1846. p->discrete.denominator);
  1847. break;
  1848. case V4L2_FRMIVAL_TYPE_STEPWISE:
  1849. dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
  1850. p->stepwise.min.numerator,
  1851. p->stepwise.min.denominator,
  1852. p->stepwise.max.numerator,
  1853. p->stepwise.max.denominator,
  1854. p->stepwise.step.numerator,
  1855. p->stepwise.step.denominator);
  1856. break;
  1857. case V4L2_FRMIVAL_TYPE_CONTINUOUS:
  1858. dbgarg2("continuous\n");
  1859. break;
  1860. default:
  1861. dbgarg2("- Unknown type!\n");
  1862. }
  1863. break;
  1864. }
  1865. case VIDIOC_ENUM_DV_PRESETS:
  1866. {
  1867. struct v4l2_dv_enum_preset *p = arg;
  1868. if (!ops->vidioc_enum_dv_presets)
  1869. break;
  1870. ret = ops->vidioc_enum_dv_presets(file, fh, p);
  1871. if (!ret)
  1872. dbgarg(cmd,
  1873. "index=%d, preset=%d, name=%s, width=%d,"
  1874. " height=%d ",
  1875. p->index, p->preset, p->name, p->width,
  1876. p->height);
  1877. break;
  1878. }
  1879. case VIDIOC_S_DV_PRESET:
  1880. {
  1881. struct v4l2_dv_preset *p = arg;
  1882. if (!ops->vidioc_s_dv_preset)
  1883. break;
  1884. if (ret_prio) {
  1885. ret = ret_prio;
  1886. break;
  1887. }
  1888. dbgarg(cmd, "preset=%d\n", p->preset);
  1889. ret = ops->vidioc_s_dv_preset(file, fh, p);
  1890. break;
  1891. }
  1892. case VIDIOC_G_DV_PRESET:
  1893. {
  1894. struct v4l2_dv_preset *p = arg;
  1895. if (!ops->vidioc_g_dv_preset)
  1896. break;
  1897. ret = ops->vidioc_g_dv_preset(file, fh, p);
  1898. if (!ret)
  1899. dbgarg(cmd, "preset=%d\n", p->preset);
  1900. break;
  1901. }
  1902. case VIDIOC_QUERY_DV_PRESET:
  1903. {
  1904. struct v4l2_dv_preset *p = arg;
  1905. if (!ops->vidioc_query_dv_preset)
  1906. break;
  1907. ret = ops->vidioc_query_dv_preset(file, fh, p);
  1908. if (!ret)
  1909. dbgarg(cmd, "preset=%d\n", p->preset);
  1910. break;
  1911. }
  1912. case VIDIOC_S_DV_TIMINGS:
  1913. {
  1914. struct v4l2_dv_timings *p = arg;
  1915. if (!ops->vidioc_s_dv_timings)
  1916. break;
  1917. if (ret_prio) {
  1918. ret = ret_prio;
  1919. break;
  1920. }
  1921. switch (p->type) {
  1922. case V4L2_DV_BT_656_1120:
  1923. dbgarg2("bt-656/1120:interlaced=%d, pixelclock=%lld,"
  1924. " width=%d, height=%d, polarities=%x,"
  1925. " hfrontporch=%d, hsync=%d, hbackporch=%d,"
  1926. " vfrontporch=%d, vsync=%d, vbackporch=%d,"
  1927. " il_vfrontporch=%d, il_vsync=%d,"
  1928. " il_vbackporch=%d\n",
  1929. p->bt.interlaced, p->bt.pixelclock,
  1930. p->bt.width, p->bt.height, p->bt.polarities,
  1931. p->bt.hfrontporch, p->bt.hsync,
  1932. p->bt.hbackporch, p->bt.vfrontporch,
  1933. p->bt.vsync, p->bt.vbackporch,
  1934. p->bt.il_vfrontporch, p->bt.il_vsync,
  1935. p->bt.il_vbackporch);
  1936. ret = ops->vidioc_s_dv_timings(file, fh, p);
  1937. break;
  1938. default:
  1939. dbgarg2("Unknown type %d!\n", p->type);
  1940. break;
  1941. }
  1942. break;
  1943. }
  1944. case VIDIOC_G_DV_TIMINGS:
  1945. {
  1946. struct v4l2_dv_timings *p = arg;
  1947. if (!ops->vidioc_g_dv_timings)
  1948. break;
  1949. ret = ops->vidioc_g_dv_timings(file, fh, p);
  1950. if (!ret) {
  1951. switch (p->type) {
  1952. case V4L2_DV_BT_656_1120:
  1953. dbgarg2("bt-656/1120:interlaced=%d,"
  1954. " pixelclock=%lld,"
  1955. " width=%d, height=%d, polarities=%x,"
  1956. " hfrontporch=%d, hsync=%d,"
  1957. " hbackporch=%d, vfrontporch=%d,"
  1958. " vsync=%d, vbackporch=%d,"
  1959. " il_vfrontporch=%d, il_vsync=%d,"
  1960. " il_vbackporch=%d\n",
  1961. p->bt.interlaced, p->bt.pixelclock,
  1962. p->bt.width, p->bt.height,
  1963. p->bt.polarities, p->bt.hfrontporch,
  1964. p->bt.hsync, p->bt.hbackporch,
  1965. p->bt.vfrontporch, p->bt.vsync,
  1966. p->bt.vbackporch, p->bt.il_vfrontporch,
  1967. p->bt.il_vsync, p->bt.il_vbackporch);
  1968. break;
  1969. default:
  1970. dbgarg2("Unknown type %d!\n", p->type);
  1971. break;
  1972. }
  1973. }
  1974. break;
  1975. }
  1976. case VIDIOC_DQEVENT:
  1977. {
  1978. struct v4l2_event *ev = arg;
  1979. if (!ops->vidioc_subscribe_event)
  1980. break;
  1981. ret = v4l2_event_dequeue(fh, ev, file->f_flags & O_NONBLOCK);
  1982. if (ret < 0) {
  1983. dbgarg(cmd, "no pending events?");
  1984. break;
  1985. }
  1986. dbgarg(cmd,
  1987. "pending=%d, type=0x%8.8x, sequence=%d, "
  1988. "timestamp=%lu.%9.9lu ",
  1989. ev->pending, ev->type, ev->sequence,
  1990. ev->timestamp.tv_sec, ev->timestamp.tv_nsec);
  1991. break;
  1992. }
  1993. case VIDIOC_SUBSCRIBE_EVENT:
  1994. {
  1995. struct v4l2_event_subscription *sub = arg;
  1996. if (!ops->vidioc_subscribe_event)
  1997. break;
  1998. ret = ops->vidioc_subscribe_event(fh, sub);
  1999. if (ret < 0) {
  2000. dbgarg(cmd, "failed, ret=%ld", ret);
  2001. break;
  2002. }
  2003. dbgarg(cmd, "type=0x%8.8x", sub->type);
  2004. break;
  2005. }
  2006. case VIDIOC_UNSUBSCRIBE_EVENT:
  2007. {
  2008. struct v4l2_event_subscription *sub = arg;
  2009. if (!ops->vidioc_unsubscribe_event)
  2010. break;
  2011. ret = ops->vidioc_unsubscribe_event(fh, sub);
  2012. if (ret < 0) {
  2013. dbgarg(cmd, "failed, ret=%ld", ret);
  2014. break;
  2015. }
  2016. dbgarg(cmd, "type=0x%8.8x", sub->type);
  2017. break;
  2018. }
  2019. case VIDIOC_CREATE_BUFS:
  2020. {
  2021. struct v4l2_create_buffers *create = arg;
  2022. if (!ops->vidioc_create_bufs)
  2023. break;
  2024. if (ret_prio) {
  2025. ret = ret_prio;
  2026. break;
  2027. }
  2028. ret = check_fmt(ops, create->format.type);
  2029. if (ret)
  2030. break;
  2031. ret = ops->vidioc_create_bufs(file, fh, create);
  2032. dbgarg(cmd, "count=%d @ %d\n", create->count, create->index);
  2033. break;
  2034. }
  2035. case VIDIOC_PREPARE_BUF:
  2036. {
  2037. struct v4l2_buffer *b = arg;
  2038. if (!ops->vidioc_prepare_buf)
  2039. break;
  2040. ret = check_fmt(ops, b->type);
  2041. if (ret)
  2042. break;
  2043. ret = ops->vidioc_prepare_buf(file, fh, b);
  2044. dbgarg(cmd, "index=%d", b->index);
  2045. break;
  2046. }
  2047. default:
  2048. if (!ops->vidioc_default)
  2049. break;
  2050. ret = ops->vidioc_default(file, fh, ret_prio >= 0, cmd, arg);
  2051. break;
  2052. } /* switch */
  2053. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
  2054. if (ret < 0) {
  2055. v4l_print_ioctl(vfd->name, cmd);
  2056. printk(KERN_CONT " error %ld\n", ret);
  2057. }
  2058. }
  2059. return ret;
  2060. }
  2061. /* In some cases, only a few fields are used as input, i.e. when the app sets
  2062. * "index" and then the driver fills in the rest of the structure for the thing
  2063. * with that index. We only need to copy up the first non-input field. */
  2064. static unsigned long cmd_input_size(unsigned int cmd)
  2065. {
  2066. /* Size of structure up to and including 'field' */
  2067. #define CMDINSIZE(cmd, type, field) \
  2068. case VIDIOC_##cmd: \
  2069. return offsetof(struct v4l2_##type, field) + \
  2070. sizeof(((struct v4l2_##type *)0)->field);
  2071. switch (cmd) {
  2072. CMDINSIZE(ENUM_FMT, fmtdesc, type);
  2073. CMDINSIZE(G_FMT, format, type);
  2074. CMDINSIZE(QUERYBUF, buffer, length);
  2075. CMDINSIZE(G_PARM, streamparm, type);
  2076. CMDINSIZE(ENUMSTD, standard, index);
  2077. CMDINSIZE(ENUMINPUT, input, index);
  2078. CMDINSIZE(G_CTRL, control, id);
  2079. CMDINSIZE(G_TUNER, tuner, index);
  2080. CMDINSIZE(QUERYCTRL, queryctrl, id);
  2081. CMDINSIZE(QUERYMENU, querymenu, index);
  2082. CMDINSIZE(ENUMOUTPUT, output, index);
  2083. CMDINSIZE(G_MODULATOR, modulator, index);
  2084. CMDINSIZE(G_FREQUENCY, frequency, tuner);
  2085. CMDINSIZE(CROPCAP, cropcap, type);
  2086. CMDINSIZE(G_CROP, crop, type);
  2087. CMDINSIZE(ENUMAUDIO, audio, index);
  2088. CMDINSIZE(ENUMAUDOUT, audioout, index);
  2089. CMDINSIZE(ENCODER_CMD, encoder_cmd, flags);
  2090. CMDINSIZE(TRY_ENCODER_CMD, encoder_cmd, flags);
  2091. CMDINSIZE(G_SLICED_VBI_CAP, sliced_vbi_cap, type);
  2092. CMDINSIZE(ENUM_FRAMESIZES, frmsizeenum, pixel_format);
  2093. CMDINSIZE(ENUM_FRAMEINTERVALS, frmivalenum, height);
  2094. default:
  2095. return _IOC_SIZE(cmd);
  2096. }
  2097. }
  2098. static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
  2099. void * __user *user_ptr, void ***kernel_ptr)
  2100. {
  2101. int ret = 0;
  2102. switch (cmd) {
  2103. case VIDIOC_QUERYBUF:
  2104. case VIDIOC_QBUF:
  2105. case VIDIOC_DQBUF: {
  2106. struct v4l2_buffer *buf = parg;
  2107. if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
  2108. if (buf->length > VIDEO_MAX_PLANES) {
  2109. ret = -EINVAL;
  2110. break;
  2111. }
  2112. *user_ptr = (void __user *)buf->m.planes;
  2113. *kernel_ptr = (void *)&buf->m.planes;
  2114. *array_size = sizeof(struct v4l2_plane) * buf->length;
  2115. ret = 1;
  2116. }
  2117. break;
  2118. }
  2119. case VIDIOC_S_EXT_CTRLS:
  2120. case VIDIOC_G_EXT_CTRLS:
  2121. case VIDIOC_TRY_EXT_CTRLS: {
  2122. struct v4l2_ext_controls *ctrls = parg;
  2123. if (ctrls->count != 0) {
  2124. if (ctrls->count > V4L2_CID_MAX_CTRLS) {
  2125. ret = -EINVAL;
  2126. break;
  2127. }
  2128. *user_ptr = (void __user *)ctrls->controls;
  2129. *kernel_ptr = (void *)&ctrls->controls;
  2130. *array_size = sizeof(struct v4l2_ext_control)
  2131. * ctrls->count;
  2132. ret = 1;
  2133. }
  2134. break;
  2135. }
  2136. }
  2137. return ret;
  2138. }
  2139. long
  2140. video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
  2141. v4l2_kioctl func)
  2142. {
  2143. char sbuf[128];
  2144. void *mbuf = NULL;
  2145. void *parg = (void *)arg;
  2146. long err = -EINVAL;
  2147. bool has_array_args;
  2148. size_t array_size = 0;
  2149. void __user *user_ptr = NULL;
  2150. void **kernel_ptr = NULL;
  2151. /* Copy arguments into temp kernel buffer */
  2152. if (_IOC_DIR(cmd) != _IOC_NONE) {
  2153. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  2154. parg = sbuf;
  2155. } else {
  2156. /* too big to allocate from stack */
  2157. mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  2158. if (NULL == mbuf)
  2159. return -ENOMEM;
  2160. parg = mbuf;
  2161. }
  2162. err = -EFAULT;
  2163. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  2164. unsigned long n = cmd_input_size(cmd);
  2165. if (copy_from_user(parg, (void __user *)arg, n))
  2166. goto out;
  2167. /* zero out anything we don't copy from userspace */
  2168. if (n < _IOC_SIZE(cmd))
  2169. memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
  2170. } else {
  2171. /* read-only ioctl */
  2172. memset(parg, 0, _IOC_SIZE(cmd));
  2173. }
  2174. }
  2175. err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
  2176. if (err < 0)
  2177. goto out;
  2178. has_array_args = err;
  2179. if (has_array_args) {
  2180. /*
  2181. * When adding new types of array args, make sure that the
  2182. * parent argument to ioctl (which contains the pointer to the
  2183. * array) fits into sbuf (so that mbuf will still remain
  2184. * unused up to here).
  2185. */
  2186. mbuf = kmalloc(array_size, GFP_KERNEL);
  2187. err = -ENOMEM;
  2188. if (NULL == mbuf)
  2189. goto out_array_args;
  2190. err = -EFAULT;
  2191. if (copy_from_user(mbuf, user_ptr, array_size))
  2192. goto out_array_args;
  2193. *kernel_ptr = mbuf;
  2194. }
  2195. /* Handles IOCTL */
  2196. err = func(file, cmd, parg);
  2197. if (err == -ENOIOCTLCMD)
  2198. err = -EINVAL;
  2199. if (has_array_args) {
  2200. *kernel_ptr = user_ptr;
  2201. if (copy_to_user(user_ptr, mbuf, array_size))
  2202. err = -EFAULT;
  2203. goto out_array_args;
  2204. }
  2205. if (err < 0)
  2206. goto out;
  2207. out_array_args:
  2208. /* Copy results into user buffer */
  2209. switch (_IOC_DIR(cmd)) {
  2210. case _IOC_READ:
  2211. case (_IOC_WRITE | _IOC_READ):
  2212. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  2213. err = -EFAULT;
  2214. break;
  2215. }
  2216. out:
  2217. kfree(mbuf);
  2218. return err;
  2219. }
  2220. EXPORT_SYMBOL(video_usercopy);
  2221. long video_ioctl2(struct file *file,
  2222. unsigned int cmd, unsigned long arg)
  2223. {
  2224. return video_usercopy(file, cmd, arg, __video_do_ioctl);
  2225. }
  2226. EXPORT_SYMBOL(video_ioctl2);