v4l2-ioctl.c 59 KB

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