v4l2-ioctl.c 58 KB

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