v4l2-ioctl.c 60 KB

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