v4l2-ioctl.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  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/types.h>
  16. #include <linux/kernel.h>
  17. #define __OLD_VIDIOC_ /* To allow fixing old calls */
  18. #include <linux/videodev.h>
  19. #include <linux/videodev2.h>
  20. #ifdef CONFIG_VIDEO_V4L1
  21. #include <linux/videodev.h>
  22. #endif
  23. #include <media/v4l2-common.h>
  24. #include <media/v4l2-ioctl.h>
  25. #include <media/v4l2-chip-ident.h>
  26. #define dbgarg(cmd, fmt, arg...) \
  27. do { \
  28. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { \
  29. printk(KERN_DEBUG "%s: ", vfd->name); \
  30. v4l_printk_ioctl(cmd); \
  31. printk(" " fmt, ## arg); \
  32. } \
  33. } while (0)
  34. #define dbgarg2(fmt, arg...) \
  35. do { \
  36. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
  37. printk(KERN_DEBUG "%s: " fmt, vfd->name, ## arg);\
  38. } while (0)
  39. #define dbgarg3(fmt, arg...) \
  40. do { \
  41. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
  42. printk(KERN_CONT "%s: " fmt, vfd->name, ## arg);\
  43. } while (0)
  44. /* Zero out the end of the struct pointed to by p. Everthing after, but
  45. * not including, the specified field is cleared. */
  46. #define CLEAR_AFTER_FIELD(p, field) \
  47. memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
  48. 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
  49. struct std_descr {
  50. v4l2_std_id std;
  51. const char *descr;
  52. };
  53. static const struct std_descr standards[] = {
  54. { V4L2_STD_NTSC, "NTSC" },
  55. { V4L2_STD_NTSC_M, "NTSC-M" },
  56. { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
  57. { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
  58. { V4L2_STD_NTSC_443, "NTSC-443" },
  59. { V4L2_STD_PAL, "PAL" },
  60. { V4L2_STD_PAL_BG, "PAL-BG" },
  61. { V4L2_STD_PAL_B, "PAL-B" },
  62. { V4L2_STD_PAL_B1, "PAL-B1" },
  63. { V4L2_STD_PAL_G, "PAL-G" },
  64. { V4L2_STD_PAL_H, "PAL-H" },
  65. { V4L2_STD_PAL_I, "PAL-I" },
  66. { V4L2_STD_PAL_DK, "PAL-DK" },
  67. { V4L2_STD_PAL_D, "PAL-D" },
  68. { V4L2_STD_PAL_D1, "PAL-D1" },
  69. { V4L2_STD_PAL_K, "PAL-K" },
  70. { V4L2_STD_PAL_M, "PAL-M" },
  71. { V4L2_STD_PAL_N, "PAL-N" },
  72. { V4L2_STD_PAL_Nc, "PAL-Nc" },
  73. { V4L2_STD_PAL_60, "PAL-60" },
  74. { V4L2_STD_SECAM, "SECAM" },
  75. { V4L2_STD_SECAM_B, "SECAM-B" },
  76. { V4L2_STD_SECAM_G, "SECAM-G" },
  77. { V4L2_STD_SECAM_H, "SECAM-H" },
  78. { V4L2_STD_SECAM_DK, "SECAM-DK" },
  79. { V4L2_STD_SECAM_D, "SECAM-D" },
  80. { V4L2_STD_SECAM_K, "SECAM-K" },
  81. { V4L2_STD_SECAM_K1, "SECAM-K1" },
  82. { V4L2_STD_SECAM_L, "SECAM-L" },
  83. { V4L2_STD_SECAM_LC, "SECAM-Lc" },
  84. { 0, "Unknown" }
  85. };
  86. /* video4linux standard ID conversion to standard name
  87. */
  88. const char *v4l2_norm_to_name(v4l2_std_id id)
  89. {
  90. u32 myid = id;
  91. int i;
  92. /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
  93. 64 bit comparations. So, on that architecture, with some gcc
  94. variants, compilation fails. Currently, the max value is 30bit wide.
  95. */
  96. BUG_ON(myid != id);
  97. for (i = 0; standards[i].std; i++)
  98. if (myid == standards[i].std)
  99. break;
  100. return standards[i].descr;
  101. }
  102. EXPORT_SYMBOL(v4l2_norm_to_name);
  103. /* Returns frame period for the given standard */
  104. void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
  105. {
  106. if (id & V4L2_STD_525_60) {
  107. frameperiod->numerator = 1001;
  108. frameperiod->denominator = 30000;
  109. } else {
  110. frameperiod->numerator = 1;
  111. frameperiod->denominator = 25;
  112. }
  113. }
  114. EXPORT_SYMBOL(v4l2_video_std_frame_period);
  115. /* Fill in the fields of a v4l2_standard structure according to the
  116. 'id' and 'transmission' parameters. Returns negative on error. */
  117. int v4l2_video_std_construct(struct v4l2_standard *vs,
  118. int id, const char *name)
  119. {
  120. vs->id = id;
  121. v4l2_video_std_frame_period(id, &vs->frameperiod);
  122. vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
  123. strlcpy(vs->name, name, sizeof(vs->name));
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(v4l2_video_std_construct);
  127. /* ----------------------------------------------------------------- */
  128. /* some arrays for pretty-printing debug messages of enum types */
  129. const char *v4l2_field_names[] = {
  130. [V4L2_FIELD_ANY] = "any",
  131. [V4L2_FIELD_NONE] = "none",
  132. [V4L2_FIELD_TOP] = "top",
  133. [V4L2_FIELD_BOTTOM] = "bottom",
  134. [V4L2_FIELD_INTERLACED] = "interlaced",
  135. [V4L2_FIELD_SEQ_TB] = "seq-tb",
  136. [V4L2_FIELD_SEQ_BT] = "seq-bt",
  137. [V4L2_FIELD_ALTERNATE] = "alternate",
  138. [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
  139. [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
  140. };
  141. EXPORT_SYMBOL(v4l2_field_names);
  142. const char *v4l2_type_names[] = {
  143. [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
  144. [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
  145. [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
  146. [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
  147. [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
  148. [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
  149. [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
  150. [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
  151. };
  152. EXPORT_SYMBOL(v4l2_type_names);
  153. static const char *v4l2_memory_names[] = {
  154. [V4L2_MEMORY_MMAP] = "mmap",
  155. [V4L2_MEMORY_USERPTR] = "userptr",
  156. [V4L2_MEMORY_OVERLAY] = "overlay",
  157. };
  158. #define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
  159. arr[a] : "unknown")
  160. /* ------------------------------------------------------------------ */
  161. /* debug help functions */
  162. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  163. static const char *v4l1_ioctls[] = {
  164. [_IOC_NR(VIDIOCGCAP)] = "VIDIOCGCAP",
  165. [_IOC_NR(VIDIOCGCHAN)] = "VIDIOCGCHAN",
  166. [_IOC_NR(VIDIOCSCHAN)] = "VIDIOCSCHAN",
  167. [_IOC_NR(VIDIOCGTUNER)] = "VIDIOCGTUNER",
  168. [_IOC_NR(VIDIOCSTUNER)] = "VIDIOCSTUNER",
  169. [_IOC_NR(VIDIOCGPICT)] = "VIDIOCGPICT",
  170. [_IOC_NR(VIDIOCSPICT)] = "VIDIOCSPICT",
  171. [_IOC_NR(VIDIOCCAPTURE)] = "VIDIOCCAPTURE",
  172. [_IOC_NR(VIDIOCGWIN)] = "VIDIOCGWIN",
  173. [_IOC_NR(VIDIOCSWIN)] = "VIDIOCSWIN",
  174. [_IOC_NR(VIDIOCGFBUF)] = "VIDIOCGFBUF",
  175. [_IOC_NR(VIDIOCSFBUF)] = "VIDIOCSFBUF",
  176. [_IOC_NR(VIDIOCKEY)] = "VIDIOCKEY",
  177. [_IOC_NR(VIDIOCGFREQ)] = "VIDIOCGFREQ",
  178. [_IOC_NR(VIDIOCSFREQ)] = "VIDIOCSFREQ",
  179. [_IOC_NR(VIDIOCGAUDIO)] = "VIDIOCGAUDIO",
  180. [_IOC_NR(VIDIOCSAUDIO)] = "VIDIOCSAUDIO",
  181. [_IOC_NR(VIDIOCSYNC)] = "VIDIOCSYNC",
  182. [_IOC_NR(VIDIOCMCAPTURE)] = "VIDIOCMCAPTURE",
  183. [_IOC_NR(VIDIOCGMBUF)] = "VIDIOCGMBUF",
  184. [_IOC_NR(VIDIOCGUNIT)] = "VIDIOCGUNIT",
  185. [_IOC_NR(VIDIOCGCAPTURE)] = "VIDIOCGCAPTURE",
  186. [_IOC_NR(VIDIOCSCAPTURE)] = "VIDIOCSCAPTURE",
  187. [_IOC_NR(VIDIOCSPLAYMODE)] = "VIDIOCSPLAYMODE",
  188. [_IOC_NR(VIDIOCSWRITEMODE)] = "VIDIOCSWRITEMODE",
  189. [_IOC_NR(VIDIOCGPLAYINFO)] = "VIDIOCGPLAYINFO",
  190. [_IOC_NR(VIDIOCSMICROCODE)] = "VIDIOCSMICROCODE",
  191. [_IOC_NR(VIDIOCGVBIFMT)] = "VIDIOCGVBIFMT",
  192. [_IOC_NR(VIDIOCSVBIFMT)] = "VIDIOCSVBIFMT"
  193. };
  194. #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
  195. #endif
  196. static const char *v4l2_ioctls[] = {
  197. [_IOC_NR(VIDIOC_QUERYCAP)] = "VIDIOC_QUERYCAP",
  198. [_IOC_NR(VIDIOC_RESERVED)] = "VIDIOC_RESERVED",
  199. [_IOC_NR(VIDIOC_ENUM_FMT)] = "VIDIOC_ENUM_FMT",
  200. [_IOC_NR(VIDIOC_G_FMT)] = "VIDIOC_G_FMT",
  201. [_IOC_NR(VIDIOC_S_FMT)] = "VIDIOC_S_FMT",
  202. [_IOC_NR(VIDIOC_REQBUFS)] = "VIDIOC_REQBUFS",
  203. [_IOC_NR(VIDIOC_QUERYBUF)] = "VIDIOC_QUERYBUF",
  204. [_IOC_NR(VIDIOC_G_FBUF)] = "VIDIOC_G_FBUF",
  205. [_IOC_NR(VIDIOC_S_FBUF)] = "VIDIOC_S_FBUF",
  206. [_IOC_NR(VIDIOC_OVERLAY)] = "VIDIOC_OVERLAY",
  207. [_IOC_NR(VIDIOC_QBUF)] = "VIDIOC_QBUF",
  208. [_IOC_NR(VIDIOC_DQBUF)] = "VIDIOC_DQBUF",
  209. [_IOC_NR(VIDIOC_STREAMON)] = "VIDIOC_STREAMON",
  210. [_IOC_NR(VIDIOC_STREAMOFF)] = "VIDIOC_STREAMOFF",
  211. [_IOC_NR(VIDIOC_G_PARM)] = "VIDIOC_G_PARM",
  212. [_IOC_NR(VIDIOC_S_PARM)] = "VIDIOC_S_PARM",
  213. [_IOC_NR(VIDIOC_G_STD)] = "VIDIOC_G_STD",
  214. [_IOC_NR(VIDIOC_S_STD)] = "VIDIOC_S_STD",
  215. [_IOC_NR(VIDIOC_ENUMSTD)] = "VIDIOC_ENUMSTD",
  216. [_IOC_NR(VIDIOC_ENUMINPUT)] = "VIDIOC_ENUMINPUT",
  217. [_IOC_NR(VIDIOC_G_CTRL)] = "VIDIOC_G_CTRL",
  218. [_IOC_NR(VIDIOC_S_CTRL)] = "VIDIOC_S_CTRL",
  219. [_IOC_NR(VIDIOC_G_TUNER)] = "VIDIOC_G_TUNER",
  220. [_IOC_NR(VIDIOC_S_TUNER)] = "VIDIOC_S_TUNER",
  221. [_IOC_NR(VIDIOC_G_AUDIO)] = "VIDIOC_G_AUDIO",
  222. [_IOC_NR(VIDIOC_S_AUDIO)] = "VIDIOC_S_AUDIO",
  223. [_IOC_NR(VIDIOC_QUERYCTRL)] = "VIDIOC_QUERYCTRL",
  224. [_IOC_NR(VIDIOC_QUERYMENU)] = "VIDIOC_QUERYMENU",
  225. [_IOC_NR(VIDIOC_G_INPUT)] = "VIDIOC_G_INPUT",
  226. [_IOC_NR(VIDIOC_S_INPUT)] = "VIDIOC_S_INPUT",
  227. [_IOC_NR(VIDIOC_G_OUTPUT)] = "VIDIOC_G_OUTPUT",
  228. [_IOC_NR(VIDIOC_S_OUTPUT)] = "VIDIOC_S_OUTPUT",
  229. [_IOC_NR(VIDIOC_ENUMOUTPUT)] = "VIDIOC_ENUMOUTPUT",
  230. [_IOC_NR(VIDIOC_G_AUDOUT)] = "VIDIOC_G_AUDOUT",
  231. [_IOC_NR(VIDIOC_S_AUDOUT)] = "VIDIOC_S_AUDOUT",
  232. [_IOC_NR(VIDIOC_G_MODULATOR)] = "VIDIOC_G_MODULATOR",
  233. [_IOC_NR(VIDIOC_S_MODULATOR)] = "VIDIOC_S_MODULATOR",
  234. [_IOC_NR(VIDIOC_G_FREQUENCY)] = "VIDIOC_G_FREQUENCY",
  235. [_IOC_NR(VIDIOC_S_FREQUENCY)] = "VIDIOC_S_FREQUENCY",
  236. [_IOC_NR(VIDIOC_CROPCAP)] = "VIDIOC_CROPCAP",
  237. [_IOC_NR(VIDIOC_G_CROP)] = "VIDIOC_G_CROP",
  238. [_IOC_NR(VIDIOC_S_CROP)] = "VIDIOC_S_CROP",
  239. [_IOC_NR(VIDIOC_G_JPEGCOMP)] = "VIDIOC_G_JPEGCOMP",
  240. [_IOC_NR(VIDIOC_S_JPEGCOMP)] = "VIDIOC_S_JPEGCOMP",
  241. [_IOC_NR(VIDIOC_QUERYSTD)] = "VIDIOC_QUERYSTD",
  242. [_IOC_NR(VIDIOC_TRY_FMT)] = "VIDIOC_TRY_FMT",
  243. [_IOC_NR(VIDIOC_ENUMAUDIO)] = "VIDIOC_ENUMAUDIO",
  244. [_IOC_NR(VIDIOC_ENUMAUDOUT)] = "VIDIOC_ENUMAUDOUT",
  245. [_IOC_NR(VIDIOC_G_PRIORITY)] = "VIDIOC_G_PRIORITY",
  246. [_IOC_NR(VIDIOC_S_PRIORITY)] = "VIDIOC_S_PRIORITY",
  247. [_IOC_NR(VIDIOC_G_SLICED_VBI_CAP)] = "VIDIOC_G_SLICED_VBI_CAP",
  248. [_IOC_NR(VIDIOC_LOG_STATUS)] = "VIDIOC_LOG_STATUS",
  249. [_IOC_NR(VIDIOC_G_EXT_CTRLS)] = "VIDIOC_G_EXT_CTRLS",
  250. [_IOC_NR(VIDIOC_S_EXT_CTRLS)] = "VIDIOC_S_EXT_CTRLS",
  251. [_IOC_NR(VIDIOC_TRY_EXT_CTRLS)] = "VIDIOC_TRY_EXT_CTRLS",
  252. #if 1
  253. [_IOC_NR(VIDIOC_ENUM_FRAMESIZES)] = "VIDIOC_ENUM_FRAMESIZES",
  254. [_IOC_NR(VIDIOC_ENUM_FRAMEINTERVALS)] = "VIDIOC_ENUM_FRAMEINTERVALS",
  255. [_IOC_NR(VIDIOC_G_ENC_INDEX)] = "VIDIOC_G_ENC_INDEX",
  256. [_IOC_NR(VIDIOC_ENCODER_CMD)] = "VIDIOC_ENCODER_CMD",
  257. [_IOC_NR(VIDIOC_TRY_ENCODER_CMD)] = "VIDIOC_TRY_ENCODER_CMD",
  258. [_IOC_NR(VIDIOC_DBG_S_REGISTER)] = "VIDIOC_DBG_S_REGISTER",
  259. [_IOC_NR(VIDIOC_DBG_G_REGISTER)] = "VIDIOC_DBG_G_REGISTER",
  260. [_IOC_NR(VIDIOC_DBG_G_CHIP_IDENT)] = "VIDIOC_DBG_G_CHIP_IDENT",
  261. [_IOC_NR(VIDIOC_S_HW_FREQ_SEEK)] = "VIDIOC_S_HW_FREQ_SEEK",
  262. #endif
  263. };
  264. #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
  265. /* Common ioctl debug function. This function can be used by
  266. external ioctl messages as well as internal V4L ioctl */
  267. void v4l_printk_ioctl(unsigned int cmd)
  268. {
  269. char *dir, *type;
  270. switch (_IOC_TYPE(cmd)) {
  271. case 'd':
  272. type = "v4l2_int";
  273. break;
  274. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  275. case 'v':
  276. if (_IOC_NR(cmd) >= V4L1_IOCTLS) {
  277. type = "v4l1";
  278. break;
  279. }
  280. printk("%s", v4l1_ioctls[_IOC_NR(cmd)]);
  281. return;
  282. #endif
  283. case 'V':
  284. if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
  285. type = "v4l2";
  286. break;
  287. }
  288. printk("%s", v4l2_ioctls[_IOC_NR(cmd)]);
  289. return;
  290. default:
  291. type = "unknown";
  292. }
  293. switch (_IOC_DIR(cmd)) {
  294. case _IOC_NONE: dir = "--"; break;
  295. case _IOC_READ: dir = "r-"; break;
  296. case _IOC_WRITE: dir = "-w"; break;
  297. case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
  298. default: dir = "*ERR*"; break;
  299. }
  300. printk("%s ioctl '%c', dir=%s, #%d (0x%08x)",
  301. type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
  302. }
  303. EXPORT_SYMBOL(v4l_printk_ioctl);
  304. /*
  305. * helper function -- handles userspace copying for ioctl arguments
  306. */
  307. #ifdef __OLD_VIDIOC_
  308. static unsigned int
  309. video_fix_command(unsigned int cmd)
  310. {
  311. switch (cmd) {
  312. case VIDIOC_OVERLAY_OLD:
  313. cmd = VIDIOC_OVERLAY;
  314. break;
  315. case VIDIOC_S_PARM_OLD:
  316. cmd = VIDIOC_S_PARM;
  317. break;
  318. case VIDIOC_S_CTRL_OLD:
  319. cmd = VIDIOC_S_CTRL;
  320. break;
  321. case VIDIOC_G_AUDIO_OLD:
  322. cmd = VIDIOC_G_AUDIO;
  323. break;
  324. case VIDIOC_G_AUDOUT_OLD:
  325. cmd = VIDIOC_G_AUDOUT;
  326. break;
  327. case VIDIOC_CROPCAP_OLD:
  328. cmd = VIDIOC_CROPCAP;
  329. break;
  330. }
  331. return cmd;
  332. }
  333. #endif
  334. /*
  335. * Obsolete usercopy function - Should be removed soon
  336. */
  337. long
  338. video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
  339. v4l2_kioctl func)
  340. {
  341. char sbuf[128];
  342. void *mbuf = NULL;
  343. void *parg = NULL;
  344. long err = -EINVAL;
  345. int is_ext_ctrl;
  346. size_t ctrls_size = 0;
  347. void __user *user_ptr = NULL;
  348. #ifdef __OLD_VIDIOC_
  349. cmd = video_fix_command(cmd);
  350. #endif
  351. is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
  352. cmd == VIDIOC_TRY_EXT_CTRLS);
  353. /* Copy arguments into temp kernel buffer */
  354. switch (_IOC_DIR(cmd)) {
  355. case _IOC_NONE:
  356. parg = NULL;
  357. break;
  358. case _IOC_READ:
  359. case _IOC_WRITE:
  360. case (_IOC_WRITE | _IOC_READ):
  361. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  362. parg = sbuf;
  363. } else {
  364. /* too big to allocate from stack */
  365. mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  366. if (NULL == mbuf)
  367. return -ENOMEM;
  368. parg = mbuf;
  369. }
  370. err = -EFAULT;
  371. if (_IOC_DIR(cmd) & _IOC_WRITE)
  372. if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
  373. goto out;
  374. break;
  375. }
  376. if (is_ext_ctrl) {
  377. struct v4l2_ext_controls *p = parg;
  378. /* In case of an error, tell the caller that it wasn't
  379. a specific control that caused it. */
  380. p->error_idx = p->count;
  381. user_ptr = (void __user *)p->controls;
  382. if (p->count) {
  383. ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
  384. /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
  385. mbuf = kmalloc(ctrls_size, GFP_KERNEL);
  386. err = -ENOMEM;
  387. if (NULL == mbuf)
  388. goto out_ext_ctrl;
  389. err = -EFAULT;
  390. if (copy_from_user(mbuf, user_ptr, ctrls_size))
  391. goto out_ext_ctrl;
  392. p->controls = mbuf;
  393. }
  394. }
  395. /* call driver */
  396. err = func(file, cmd, parg);
  397. if (err == -ENOIOCTLCMD)
  398. err = -EINVAL;
  399. if (is_ext_ctrl) {
  400. struct v4l2_ext_controls *p = parg;
  401. p->controls = (void *)user_ptr;
  402. if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
  403. err = -EFAULT;
  404. goto out_ext_ctrl;
  405. }
  406. if (err < 0)
  407. goto out;
  408. out_ext_ctrl:
  409. /* Copy results into user buffer */
  410. switch (_IOC_DIR(cmd)) {
  411. case _IOC_READ:
  412. case (_IOC_WRITE | _IOC_READ):
  413. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  414. err = -EFAULT;
  415. break;
  416. }
  417. out:
  418. kfree(mbuf);
  419. return err;
  420. }
  421. EXPORT_SYMBOL(video_usercopy);
  422. static void dbgbuf(unsigned int cmd, struct video_device *vfd,
  423. struct v4l2_buffer *p)
  424. {
  425. struct v4l2_timecode *tc = &p->timecode;
  426. dbgarg(cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
  427. "bytesused=%d, flags=0x%08d, "
  428. "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx, length=%d\n",
  429. p->timestamp.tv_sec / 3600,
  430. (int)(p->timestamp.tv_sec / 60) % 60,
  431. (int)(p->timestamp.tv_sec % 60),
  432. (long)p->timestamp.tv_usec,
  433. p->index,
  434. prt_names(p->type, v4l2_type_names),
  435. p->bytesused, p->flags,
  436. p->field, p->sequence,
  437. prt_names(p->memory, v4l2_memory_names),
  438. p->m.userptr, p->length);
  439. dbgarg2("timecode=%02d:%02d:%02d type=%d, "
  440. "flags=0x%08d, frames=%d, userbits=0x%08x\n",
  441. tc->hours, tc->minutes, tc->seconds,
  442. tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
  443. }
  444. static inline void dbgrect(struct video_device *vfd, char *s,
  445. struct v4l2_rect *r)
  446. {
  447. dbgarg2("%sRect start at %dx%d, size=%dx%d\n", s, r->left, r->top,
  448. r->width, r->height);
  449. };
  450. static inline void v4l_print_pix_fmt(struct video_device *vfd,
  451. struct v4l2_pix_format *fmt)
  452. {
  453. dbgarg2("width=%d, height=%d, format=%c%c%c%c, field=%s, "
  454. "bytesperline=%d sizeimage=%d, colorspace=%d\n",
  455. fmt->width, fmt->height,
  456. (fmt->pixelformat & 0xff),
  457. (fmt->pixelformat >> 8) & 0xff,
  458. (fmt->pixelformat >> 16) & 0xff,
  459. (fmt->pixelformat >> 24) & 0xff,
  460. prt_names(fmt->field, v4l2_field_names),
  461. fmt->bytesperline, fmt->sizeimage, fmt->colorspace);
  462. };
  463. static inline void v4l_print_ext_ctrls(unsigned int cmd,
  464. struct video_device *vfd, struct v4l2_ext_controls *c, int show_vals)
  465. {
  466. __u32 i;
  467. if (!(vfd->debug & V4L2_DEBUG_IOCTL_ARG))
  468. return;
  469. dbgarg(cmd, "");
  470. printk(KERN_CONT "class=0x%x", c->ctrl_class);
  471. for (i = 0; i < c->count; i++) {
  472. if (show_vals && !c->controls[i].size)
  473. printk(KERN_CONT " id/val=0x%x/0x%x",
  474. c->controls[i].id, c->controls[i].value);
  475. else
  476. printk(KERN_CONT " id=0x%x,size=%u",
  477. c->controls[i].id, c->controls[i].size);
  478. }
  479. printk(KERN_CONT "\n");
  480. };
  481. static inline int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
  482. {
  483. __u32 i;
  484. /* zero the reserved fields */
  485. c->reserved[0] = c->reserved[1] = 0;
  486. for (i = 0; i < c->count; i++)
  487. c->controls[i].reserved2[0] = 0;
  488. /* V4L2_CID_PRIVATE_BASE cannot be used as control class
  489. when using extended controls.
  490. Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
  491. is it allowed for backwards compatibility.
  492. */
  493. if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
  494. return 0;
  495. /* Check that all controls are from the same control class. */
  496. for (i = 0; i < c->count; i++) {
  497. if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
  498. c->error_idx = i;
  499. return 0;
  500. }
  501. }
  502. return 1;
  503. }
  504. static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
  505. {
  506. if (ops == NULL)
  507. return -EINVAL;
  508. switch (type) {
  509. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  510. if (ops->vidioc_g_fmt_vid_cap)
  511. return 0;
  512. break;
  513. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  514. if (ops->vidioc_g_fmt_vid_overlay)
  515. return 0;
  516. break;
  517. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  518. if (ops->vidioc_g_fmt_vid_out)
  519. return 0;
  520. break;
  521. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  522. if (ops->vidioc_g_fmt_vid_out_overlay)
  523. return 0;
  524. break;
  525. case V4L2_BUF_TYPE_VBI_CAPTURE:
  526. if (ops->vidioc_g_fmt_vbi_cap)
  527. return 0;
  528. break;
  529. case V4L2_BUF_TYPE_VBI_OUTPUT:
  530. if (ops->vidioc_g_fmt_vbi_out)
  531. return 0;
  532. break;
  533. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  534. if (ops->vidioc_g_fmt_sliced_vbi_cap)
  535. return 0;
  536. break;
  537. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  538. if (ops->vidioc_g_fmt_sliced_vbi_out)
  539. return 0;
  540. break;
  541. case V4L2_BUF_TYPE_PRIVATE:
  542. if (ops->vidioc_g_fmt_type_private)
  543. return 0;
  544. break;
  545. }
  546. return -EINVAL;
  547. }
  548. static long __video_do_ioctl(struct file *file,
  549. unsigned int cmd, void *arg)
  550. {
  551. struct video_device *vfd = video_devdata(file);
  552. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  553. void *fh = file->private_data;
  554. long ret = -EINVAL;
  555. if ((vfd->debug & V4L2_DEBUG_IOCTL) &&
  556. !(vfd->debug & V4L2_DEBUG_IOCTL_ARG)) {
  557. v4l_print_ioctl(vfd->name, cmd);
  558. printk(KERN_CONT "\n");
  559. }
  560. if (ops == NULL) {
  561. printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
  562. vfd->name);
  563. return -EINVAL;
  564. }
  565. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  566. /***********************************************************
  567. Handles calls to the obsoleted V4L1 API
  568. Due to the nature of VIDIOCGMBUF, each driver that supports
  569. V4L1 should implement its own handler for this ioctl.
  570. ***********************************************************/
  571. /* --- streaming capture ------------------------------------- */
  572. if (cmd == VIDIOCGMBUF) {
  573. struct video_mbuf *p = arg;
  574. if (!ops->vidiocgmbuf)
  575. return ret;
  576. ret = ops->vidiocgmbuf(file, fh, p);
  577. if (!ret)
  578. dbgarg(cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
  579. p->size, p->frames,
  580. (unsigned long)p->offsets);
  581. return ret;
  582. }
  583. /********************************************************
  584. All other V4L1 calls are handled by v4l1_compat module.
  585. Those calls will be translated into V4L2 calls, and
  586. __video_do_ioctl will be called again, with one or more
  587. V4L2 ioctls.
  588. ********************************************************/
  589. if (_IOC_TYPE(cmd) == 'v' && _IOC_NR(cmd) < BASE_VIDIOCPRIVATE)
  590. return v4l_compat_translate_ioctl(file, cmd, arg,
  591. __video_do_ioctl);
  592. #endif
  593. switch (cmd) {
  594. /* --- capabilities ------------------------------------------ */
  595. case VIDIOC_QUERYCAP:
  596. {
  597. struct v4l2_capability *cap = (struct v4l2_capability *)arg;
  598. if (!ops->vidioc_querycap)
  599. break;
  600. ret = ops->vidioc_querycap(file, fh, cap);
  601. if (!ret)
  602. dbgarg(cmd, "driver=%s, card=%s, bus=%s, "
  603. "version=0x%08x, "
  604. "capabilities=0x%08x\n",
  605. cap->driver, cap->card, cap->bus_info,
  606. cap->version,
  607. cap->capabilities);
  608. break;
  609. }
  610. /* --- priority ------------------------------------------ */
  611. case VIDIOC_G_PRIORITY:
  612. {
  613. enum v4l2_priority *p = arg;
  614. if (!ops->vidioc_g_priority)
  615. break;
  616. ret = ops->vidioc_g_priority(file, fh, p);
  617. if (!ret)
  618. dbgarg(cmd, "priority is %d\n", *p);
  619. break;
  620. }
  621. case VIDIOC_S_PRIORITY:
  622. {
  623. enum v4l2_priority *p = arg;
  624. if (!ops->vidioc_s_priority)
  625. break;
  626. dbgarg(cmd, "setting priority to %d\n", *p);
  627. ret = ops->vidioc_s_priority(file, fh, *p);
  628. break;
  629. }
  630. /* --- capture ioctls ---------------------------------------- */
  631. case VIDIOC_ENUM_FMT:
  632. {
  633. struct v4l2_fmtdesc *f = arg;
  634. switch (f->type) {
  635. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  636. if (ops->vidioc_enum_fmt_vid_cap)
  637. ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f);
  638. break;
  639. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  640. if (ops->vidioc_enum_fmt_vid_overlay)
  641. ret = ops->vidioc_enum_fmt_vid_overlay(file,
  642. fh, f);
  643. break;
  644. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  645. if (ops->vidioc_enum_fmt_vid_out)
  646. ret = ops->vidioc_enum_fmt_vid_out(file, fh, f);
  647. break;
  648. case V4L2_BUF_TYPE_PRIVATE:
  649. if (ops->vidioc_enum_fmt_type_private)
  650. ret = ops->vidioc_enum_fmt_type_private(file,
  651. fh, f);
  652. break;
  653. default:
  654. break;
  655. }
  656. if (!ret)
  657. dbgarg(cmd, "index=%d, type=%d, flags=%d, "
  658. "pixelformat=%c%c%c%c, description='%s'\n",
  659. f->index, f->type, f->flags,
  660. (f->pixelformat & 0xff),
  661. (f->pixelformat >> 8) & 0xff,
  662. (f->pixelformat >> 16) & 0xff,
  663. (f->pixelformat >> 24) & 0xff,
  664. f->description);
  665. break;
  666. }
  667. case VIDIOC_G_FMT:
  668. {
  669. struct v4l2_format *f = (struct v4l2_format *)arg;
  670. /* FIXME: Should be one dump per type */
  671. dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
  672. switch (f->type) {
  673. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  674. if (ops->vidioc_g_fmt_vid_cap)
  675. ret = ops->vidioc_g_fmt_vid_cap(file, fh, f);
  676. if (!ret)
  677. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  678. break;
  679. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  680. if (ops->vidioc_g_fmt_vid_overlay)
  681. ret = ops->vidioc_g_fmt_vid_overlay(file,
  682. fh, f);
  683. break;
  684. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  685. if (ops->vidioc_g_fmt_vid_out)
  686. ret = ops->vidioc_g_fmt_vid_out(file, fh, f);
  687. if (!ret)
  688. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  689. break;
  690. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  691. if (ops->vidioc_g_fmt_vid_out_overlay)
  692. ret = ops->vidioc_g_fmt_vid_out_overlay(file,
  693. fh, f);
  694. break;
  695. case V4L2_BUF_TYPE_VBI_CAPTURE:
  696. if (ops->vidioc_g_fmt_vbi_cap)
  697. ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f);
  698. break;
  699. case V4L2_BUF_TYPE_VBI_OUTPUT:
  700. if (ops->vidioc_g_fmt_vbi_out)
  701. ret = ops->vidioc_g_fmt_vbi_out(file, fh, f);
  702. break;
  703. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  704. if (ops->vidioc_g_fmt_sliced_vbi_cap)
  705. ret = ops->vidioc_g_fmt_sliced_vbi_cap(file,
  706. fh, f);
  707. break;
  708. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  709. if (ops->vidioc_g_fmt_sliced_vbi_out)
  710. ret = ops->vidioc_g_fmt_sliced_vbi_out(file,
  711. fh, f);
  712. break;
  713. case V4L2_BUF_TYPE_PRIVATE:
  714. if (ops->vidioc_g_fmt_type_private)
  715. ret = ops->vidioc_g_fmt_type_private(file,
  716. fh, f);
  717. break;
  718. }
  719. break;
  720. }
  721. case VIDIOC_S_FMT:
  722. {
  723. struct v4l2_format *f = (struct v4l2_format *)arg;
  724. /* FIXME: Should be one dump per type */
  725. dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
  726. switch (f->type) {
  727. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  728. CLEAR_AFTER_FIELD(f, fmt.pix);
  729. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  730. if (ops->vidioc_s_fmt_vid_cap)
  731. ret = ops->vidioc_s_fmt_vid_cap(file, fh, f);
  732. break;
  733. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  734. CLEAR_AFTER_FIELD(f, fmt.win);
  735. if (ops->vidioc_s_fmt_vid_overlay)
  736. ret = ops->vidioc_s_fmt_vid_overlay(file,
  737. fh, f);
  738. break;
  739. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  740. CLEAR_AFTER_FIELD(f, fmt.pix);
  741. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  742. if (ops->vidioc_s_fmt_vid_out)
  743. ret = ops->vidioc_s_fmt_vid_out(file, fh, f);
  744. break;
  745. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  746. CLEAR_AFTER_FIELD(f, fmt.win);
  747. if (ops->vidioc_s_fmt_vid_out_overlay)
  748. ret = ops->vidioc_s_fmt_vid_out_overlay(file,
  749. fh, f);
  750. break;
  751. case V4L2_BUF_TYPE_VBI_CAPTURE:
  752. CLEAR_AFTER_FIELD(f, fmt.vbi);
  753. if (ops->vidioc_s_fmt_vbi_cap)
  754. ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
  755. break;
  756. case V4L2_BUF_TYPE_VBI_OUTPUT:
  757. CLEAR_AFTER_FIELD(f, fmt.vbi);
  758. if (ops->vidioc_s_fmt_vbi_out)
  759. ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
  760. break;
  761. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  762. CLEAR_AFTER_FIELD(f, fmt.sliced);
  763. if (ops->vidioc_s_fmt_sliced_vbi_cap)
  764. ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
  765. fh, f);
  766. break;
  767. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  768. CLEAR_AFTER_FIELD(f, fmt.sliced);
  769. if (ops->vidioc_s_fmt_sliced_vbi_out)
  770. ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
  771. fh, f);
  772. break;
  773. case V4L2_BUF_TYPE_PRIVATE:
  774. /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
  775. if (ops->vidioc_s_fmt_type_private)
  776. ret = ops->vidioc_s_fmt_type_private(file,
  777. fh, f);
  778. break;
  779. }
  780. break;
  781. }
  782. case VIDIOC_TRY_FMT:
  783. {
  784. struct v4l2_format *f = (struct v4l2_format *)arg;
  785. /* FIXME: Should be one dump per type */
  786. dbgarg(cmd, "type=%s\n", prt_names(f->type,
  787. v4l2_type_names));
  788. switch (f->type) {
  789. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  790. CLEAR_AFTER_FIELD(f, fmt.pix);
  791. if (ops->vidioc_try_fmt_vid_cap)
  792. ret = ops->vidioc_try_fmt_vid_cap(file, fh, f);
  793. if (!ret)
  794. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  795. break;
  796. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  797. CLEAR_AFTER_FIELD(f, fmt.win);
  798. if (ops->vidioc_try_fmt_vid_overlay)
  799. ret = ops->vidioc_try_fmt_vid_overlay(file,
  800. fh, f);
  801. break;
  802. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  803. CLEAR_AFTER_FIELD(f, fmt.pix);
  804. if (ops->vidioc_try_fmt_vid_out)
  805. ret = ops->vidioc_try_fmt_vid_out(file, fh, f);
  806. if (!ret)
  807. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  808. break;
  809. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  810. CLEAR_AFTER_FIELD(f, fmt.win);
  811. if (ops->vidioc_try_fmt_vid_out_overlay)
  812. ret = ops->vidioc_try_fmt_vid_out_overlay(file,
  813. fh, f);
  814. break;
  815. case V4L2_BUF_TYPE_VBI_CAPTURE:
  816. CLEAR_AFTER_FIELD(f, fmt.vbi);
  817. if (ops->vidioc_try_fmt_vbi_cap)
  818. ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f);
  819. break;
  820. case V4L2_BUF_TYPE_VBI_OUTPUT:
  821. CLEAR_AFTER_FIELD(f, fmt.vbi);
  822. if (ops->vidioc_try_fmt_vbi_out)
  823. ret = ops->vidioc_try_fmt_vbi_out(file, fh, f);
  824. break;
  825. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  826. CLEAR_AFTER_FIELD(f, fmt.sliced);
  827. if (ops->vidioc_try_fmt_sliced_vbi_cap)
  828. ret = ops->vidioc_try_fmt_sliced_vbi_cap(file,
  829. fh, f);
  830. break;
  831. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  832. CLEAR_AFTER_FIELD(f, fmt.sliced);
  833. if (ops->vidioc_try_fmt_sliced_vbi_out)
  834. ret = ops->vidioc_try_fmt_sliced_vbi_out(file,
  835. fh, f);
  836. break;
  837. case V4L2_BUF_TYPE_PRIVATE:
  838. /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
  839. if (ops->vidioc_try_fmt_type_private)
  840. ret = ops->vidioc_try_fmt_type_private(file,
  841. fh, f);
  842. break;
  843. }
  844. break;
  845. }
  846. /* FIXME: Those buf reqs could be handled here,
  847. with some changes on videobuf to allow its header to be included at
  848. videodev2.h or being merged at videodev2.
  849. */
  850. case VIDIOC_REQBUFS:
  851. {
  852. struct v4l2_requestbuffers *p = arg;
  853. if (!ops->vidioc_reqbufs)
  854. break;
  855. ret = check_fmt(ops, p->type);
  856. if (ret)
  857. break;
  858. if (p->type < V4L2_BUF_TYPE_PRIVATE)
  859. CLEAR_AFTER_FIELD(p, memory);
  860. ret = ops->vidioc_reqbufs(file, fh, p);
  861. dbgarg(cmd, "count=%d, type=%s, memory=%s\n",
  862. p->count,
  863. prt_names(p->type, v4l2_type_names),
  864. prt_names(p->memory, v4l2_memory_names));
  865. break;
  866. }
  867. case VIDIOC_QUERYBUF:
  868. {
  869. struct v4l2_buffer *p = arg;
  870. if (!ops->vidioc_querybuf)
  871. break;
  872. ret = check_fmt(ops, p->type);
  873. if (ret)
  874. break;
  875. ret = ops->vidioc_querybuf(file, fh, p);
  876. if (!ret)
  877. dbgbuf(cmd, vfd, p);
  878. break;
  879. }
  880. case VIDIOC_QBUF:
  881. {
  882. struct v4l2_buffer *p = arg;
  883. if (!ops->vidioc_qbuf)
  884. break;
  885. ret = check_fmt(ops, p->type);
  886. if (ret)
  887. break;
  888. ret = ops->vidioc_qbuf(file, fh, p);
  889. if (!ret)
  890. dbgbuf(cmd, vfd, p);
  891. break;
  892. }
  893. case VIDIOC_DQBUF:
  894. {
  895. struct v4l2_buffer *p = arg;
  896. if (!ops->vidioc_dqbuf)
  897. break;
  898. ret = check_fmt(ops, p->type);
  899. if (ret)
  900. break;
  901. ret = ops->vidioc_dqbuf(file, fh, p);
  902. if (!ret)
  903. dbgbuf(cmd, vfd, p);
  904. break;
  905. }
  906. case VIDIOC_OVERLAY:
  907. {
  908. int *i = arg;
  909. if (!ops->vidioc_overlay)
  910. break;
  911. dbgarg(cmd, "value=%d\n", *i);
  912. ret = ops->vidioc_overlay(file, fh, *i);
  913. break;
  914. }
  915. case VIDIOC_G_FBUF:
  916. {
  917. struct v4l2_framebuffer *p = arg;
  918. if (!ops->vidioc_g_fbuf)
  919. break;
  920. ret = ops->vidioc_g_fbuf(file, fh, arg);
  921. if (!ret) {
  922. dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
  923. p->capability, p->flags,
  924. (unsigned long)p->base);
  925. v4l_print_pix_fmt(vfd, &p->fmt);
  926. }
  927. break;
  928. }
  929. case VIDIOC_S_FBUF:
  930. {
  931. struct v4l2_framebuffer *p = arg;
  932. if (!ops->vidioc_s_fbuf)
  933. break;
  934. dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
  935. p->capability, p->flags, (unsigned long)p->base);
  936. v4l_print_pix_fmt(vfd, &p->fmt);
  937. ret = ops->vidioc_s_fbuf(file, fh, arg);
  938. break;
  939. }
  940. case VIDIOC_STREAMON:
  941. {
  942. enum v4l2_buf_type i = *(int *)arg;
  943. if (!ops->vidioc_streamon)
  944. break;
  945. dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
  946. ret = ops->vidioc_streamon(file, fh, i);
  947. break;
  948. }
  949. case VIDIOC_STREAMOFF:
  950. {
  951. enum v4l2_buf_type i = *(int *)arg;
  952. if (!ops->vidioc_streamoff)
  953. break;
  954. dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
  955. ret = ops->vidioc_streamoff(file, fh, i);
  956. break;
  957. }
  958. /* ---------- tv norms ---------- */
  959. case VIDIOC_ENUMSTD:
  960. {
  961. struct v4l2_standard *p = arg;
  962. v4l2_std_id id = vfd->tvnorms, curr_id = 0;
  963. unsigned int index = p->index, i, j = 0;
  964. const char *descr = "";
  965. /* Return norm array in a canonical way */
  966. for (i = 0; i <= index && id; i++) {
  967. /* last std value in the standards array is 0, so this
  968. while always ends there since (id & 0) == 0. */
  969. while ((id & standards[j].std) != standards[j].std)
  970. j++;
  971. curr_id = standards[j].std;
  972. descr = standards[j].descr;
  973. j++;
  974. if (curr_id == 0)
  975. break;
  976. if (curr_id != V4L2_STD_PAL &&
  977. curr_id != V4L2_STD_SECAM &&
  978. curr_id != V4L2_STD_NTSC)
  979. id &= ~curr_id;
  980. }
  981. if (i <= index)
  982. return -EINVAL;
  983. v4l2_video_std_construct(p, curr_id, descr);
  984. dbgarg(cmd, "index=%d, id=0x%Lx, name=%s, fps=%d/%d, "
  985. "framelines=%d\n", p->index,
  986. (unsigned long long)p->id, p->name,
  987. p->frameperiod.numerator,
  988. p->frameperiod.denominator,
  989. p->framelines);
  990. ret = 0;
  991. break;
  992. }
  993. case VIDIOC_G_STD:
  994. {
  995. v4l2_std_id *id = arg;
  996. ret = 0;
  997. /* Calls the specific handler */
  998. if (ops->vidioc_g_std)
  999. ret = ops->vidioc_g_std(file, fh, id);
  1000. else if (vfd->current_norm)
  1001. *id = vfd->current_norm;
  1002. else
  1003. ret = -EINVAL;
  1004. if (!ret)
  1005. dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
  1006. break;
  1007. }
  1008. case VIDIOC_S_STD:
  1009. {
  1010. v4l2_std_id *id = arg, norm;
  1011. dbgarg(cmd, "std=%08Lx\n", (long long unsigned)*id);
  1012. norm = (*id) & vfd->tvnorms;
  1013. if (vfd->tvnorms && !norm) /* Check if std is supported */
  1014. break;
  1015. /* Calls the specific handler */
  1016. if (ops->vidioc_s_std)
  1017. ret = ops->vidioc_s_std(file, fh, &norm);
  1018. else
  1019. ret = -EINVAL;
  1020. /* Updates standard information */
  1021. if (ret >= 0)
  1022. vfd->current_norm = norm;
  1023. break;
  1024. }
  1025. case VIDIOC_QUERYSTD:
  1026. {
  1027. v4l2_std_id *p = arg;
  1028. if (!ops->vidioc_querystd)
  1029. break;
  1030. ret = ops->vidioc_querystd(file, fh, arg);
  1031. if (!ret)
  1032. dbgarg(cmd, "detected std=%08Lx\n",
  1033. (unsigned long long)*p);
  1034. break;
  1035. }
  1036. /* ------ input switching ---------- */
  1037. /* FIXME: Inputs can be handled inside videodev2 */
  1038. case VIDIOC_ENUMINPUT:
  1039. {
  1040. struct v4l2_input *p = arg;
  1041. if (!ops->vidioc_enum_input)
  1042. break;
  1043. ret = ops->vidioc_enum_input(file, fh, p);
  1044. if (!ret)
  1045. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1046. "audioset=%d, "
  1047. "tuner=%d, std=%08Lx, status=%d\n",
  1048. p->index, p->name, p->type, p->audioset,
  1049. p->tuner,
  1050. (unsigned long long)p->std,
  1051. p->status);
  1052. break;
  1053. }
  1054. case VIDIOC_G_INPUT:
  1055. {
  1056. unsigned int *i = arg;
  1057. if (!ops->vidioc_g_input)
  1058. break;
  1059. ret = ops->vidioc_g_input(file, fh, i);
  1060. if (!ret)
  1061. dbgarg(cmd, "value=%d\n", *i);
  1062. break;
  1063. }
  1064. case VIDIOC_S_INPUT:
  1065. {
  1066. unsigned int *i = arg;
  1067. if (!ops->vidioc_s_input)
  1068. break;
  1069. dbgarg(cmd, "value=%d\n", *i);
  1070. ret = ops->vidioc_s_input(file, fh, *i);
  1071. break;
  1072. }
  1073. /* ------ output switching ---------- */
  1074. case VIDIOC_ENUMOUTPUT:
  1075. {
  1076. struct v4l2_output *p = arg;
  1077. if (!ops->vidioc_enum_output)
  1078. break;
  1079. ret = ops->vidioc_enum_output(file, fh, p);
  1080. if (!ret)
  1081. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1082. "audioset=0x%x, "
  1083. "modulator=%d, std=0x%08Lx\n",
  1084. p->index, p->name, p->type, p->audioset,
  1085. p->modulator, (unsigned long long)p->std);
  1086. break;
  1087. }
  1088. case VIDIOC_G_OUTPUT:
  1089. {
  1090. unsigned int *i = arg;
  1091. if (!ops->vidioc_g_output)
  1092. break;
  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. if (!ops->vidioc_s_output)
  1102. break;
  1103. dbgarg(cmd, "value=%d\n", *i);
  1104. ret = ops->vidioc_s_output(file, fh, *i);
  1105. break;
  1106. }
  1107. /* --- controls ---------------------------------------------- */
  1108. case VIDIOC_QUERYCTRL:
  1109. {
  1110. struct v4l2_queryctrl *p = arg;
  1111. if (!ops->vidioc_queryctrl)
  1112. break;
  1113. ret = ops->vidioc_queryctrl(file, fh, p);
  1114. if (!ret)
  1115. dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
  1116. "step=%d, default=%d, flags=0x%08x\n",
  1117. p->id, p->type, p->name,
  1118. p->minimum, p->maximum,
  1119. p->step, p->default_value, p->flags);
  1120. else
  1121. dbgarg(cmd, "id=0x%x\n", p->id);
  1122. break;
  1123. }
  1124. case VIDIOC_G_CTRL:
  1125. {
  1126. struct v4l2_control *p = arg;
  1127. if (ops->vidioc_g_ctrl)
  1128. ret = ops->vidioc_g_ctrl(file, fh, p);
  1129. else if (ops->vidioc_g_ext_ctrls) {
  1130. struct v4l2_ext_controls ctrls;
  1131. struct v4l2_ext_control ctrl;
  1132. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1133. ctrls.count = 1;
  1134. ctrls.controls = &ctrl;
  1135. ctrl.id = p->id;
  1136. ctrl.value = p->value;
  1137. if (check_ext_ctrls(&ctrls, 1)) {
  1138. ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
  1139. if (ret == 0)
  1140. p->value = ctrl.value;
  1141. }
  1142. } else
  1143. break;
  1144. if (!ret)
  1145. dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
  1146. else
  1147. dbgarg(cmd, "id=0x%x\n", p->id);
  1148. break;
  1149. }
  1150. case VIDIOC_S_CTRL:
  1151. {
  1152. struct v4l2_control *p = arg;
  1153. struct v4l2_ext_controls ctrls;
  1154. struct v4l2_ext_control ctrl;
  1155. if (!ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
  1156. break;
  1157. dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
  1158. if (ops->vidioc_s_ctrl) {
  1159. ret = ops->vidioc_s_ctrl(file, fh, p);
  1160. break;
  1161. }
  1162. if (!ops->vidioc_s_ext_ctrls)
  1163. break;
  1164. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1165. ctrls.count = 1;
  1166. ctrls.controls = &ctrl;
  1167. ctrl.id = p->id;
  1168. ctrl.value = p->value;
  1169. if (check_ext_ctrls(&ctrls, 1))
  1170. ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
  1171. break;
  1172. }
  1173. case VIDIOC_G_EXT_CTRLS:
  1174. {
  1175. struct v4l2_ext_controls *p = arg;
  1176. p->error_idx = p->count;
  1177. if (!ops->vidioc_g_ext_ctrls)
  1178. break;
  1179. if (check_ext_ctrls(p, 0))
  1180. ret = ops->vidioc_g_ext_ctrls(file, fh, p);
  1181. v4l_print_ext_ctrls(cmd, vfd, p, !ret);
  1182. break;
  1183. }
  1184. case VIDIOC_S_EXT_CTRLS:
  1185. {
  1186. struct v4l2_ext_controls *p = arg;
  1187. p->error_idx = p->count;
  1188. if (!ops->vidioc_s_ext_ctrls)
  1189. break;
  1190. v4l_print_ext_ctrls(cmd, vfd, p, 1);
  1191. if (check_ext_ctrls(p, 0))
  1192. ret = ops->vidioc_s_ext_ctrls(file, fh, p);
  1193. break;
  1194. }
  1195. case VIDIOC_TRY_EXT_CTRLS:
  1196. {
  1197. struct v4l2_ext_controls *p = arg;
  1198. p->error_idx = p->count;
  1199. if (!ops->vidioc_try_ext_ctrls)
  1200. break;
  1201. v4l_print_ext_ctrls(cmd, vfd, p, 1);
  1202. if (check_ext_ctrls(p, 0))
  1203. ret = ops->vidioc_try_ext_ctrls(file, fh, p);
  1204. break;
  1205. }
  1206. case VIDIOC_QUERYMENU:
  1207. {
  1208. struct v4l2_querymenu *p = arg;
  1209. if (!ops->vidioc_querymenu)
  1210. break;
  1211. ret = ops->vidioc_querymenu(file, fh, p);
  1212. if (!ret)
  1213. dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
  1214. p->id, p->index, p->name);
  1215. else
  1216. dbgarg(cmd, "id=0x%x, index=%d\n",
  1217. p->id, p->index);
  1218. break;
  1219. }
  1220. /* --- audio ---------------------------------------------- */
  1221. case VIDIOC_ENUMAUDIO:
  1222. {
  1223. struct v4l2_audio *p = arg;
  1224. if (!ops->vidioc_enumaudio)
  1225. break;
  1226. ret = ops->vidioc_enumaudio(file, fh, p);
  1227. if (!ret)
  1228. dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
  1229. "mode=0x%x\n", p->index, p->name,
  1230. p->capability, p->mode);
  1231. else
  1232. dbgarg(cmd, "index=%d\n", p->index);
  1233. break;
  1234. }
  1235. case VIDIOC_G_AUDIO:
  1236. {
  1237. struct v4l2_audio *p = arg;
  1238. if (!ops->vidioc_g_audio)
  1239. break;
  1240. ret = ops->vidioc_g_audio(file, fh, p);
  1241. if (!ret)
  1242. dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
  1243. "mode=0x%x\n", p->index,
  1244. p->name, p->capability, p->mode);
  1245. else
  1246. dbgarg(cmd, "index=%d\n", p->index);
  1247. break;
  1248. }
  1249. case VIDIOC_S_AUDIO:
  1250. {
  1251. struct v4l2_audio *p = arg;
  1252. if (!ops->vidioc_s_audio)
  1253. break;
  1254. dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
  1255. "mode=0x%x\n", p->index, p->name,
  1256. p->capability, p->mode);
  1257. ret = ops->vidioc_s_audio(file, fh, p);
  1258. break;
  1259. }
  1260. case VIDIOC_ENUMAUDOUT:
  1261. {
  1262. struct v4l2_audioout *p = arg;
  1263. if (!ops->vidioc_enumaudout)
  1264. break;
  1265. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1266. ret = ops->vidioc_enumaudout(file, fh, p);
  1267. if (!ret)
  1268. dbgarg2("index=%d, name=%s, capability=%d, "
  1269. "mode=%d\n", p->index, p->name,
  1270. p->capability, p->mode);
  1271. break;
  1272. }
  1273. case VIDIOC_G_AUDOUT:
  1274. {
  1275. struct v4l2_audioout *p = arg;
  1276. if (!ops->vidioc_g_audout)
  1277. break;
  1278. ret = ops->vidioc_g_audout(file, fh, p);
  1279. if (!ret)
  1280. dbgarg2("index=%d, name=%s, capability=%d, "
  1281. "mode=%d\n", p->index, p->name,
  1282. p->capability, p->mode);
  1283. break;
  1284. }
  1285. case VIDIOC_S_AUDOUT:
  1286. {
  1287. struct v4l2_audioout *p = arg;
  1288. if (!ops->vidioc_s_audout)
  1289. break;
  1290. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1291. "mode=%d\n", p->index, p->name,
  1292. p->capability, p->mode);
  1293. ret = ops->vidioc_s_audout(file, fh, p);
  1294. break;
  1295. }
  1296. case VIDIOC_G_MODULATOR:
  1297. {
  1298. struct v4l2_modulator *p = arg;
  1299. if (!ops->vidioc_g_modulator)
  1300. break;
  1301. ret = ops->vidioc_g_modulator(file, fh, p);
  1302. if (!ret)
  1303. dbgarg(cmd, "index=%d, name=%s, "
  1304. "capability=%d, rangelow=%d,"
  1305. " rangehigh=%d, txsubchans=%d\n",
  1306. p->index, p->name, p->capability,
  1307. p->rangelow, p->rangehigh,
  1308. p->txsubchans);
  1309. break;
  1310. }
  1311. case VIDIOC_S_MODULATOR:
  1312. {
  1313. struct v4l2_modulator *p = arg;
  1314. if (!ops->vidioc_s_modulator)
  1315. break;
  1316. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1317. "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
  1318. p->index, p->name, p->capability, p->rangelow,
  1319. p->rangehigh, p->txsubchans);
  1320. ret = ops->vidioc_s_modulator(file, fh, p);
  1321. break;
  1322. }
  1323. case VIDIOC_G_CROP:
  1324. {
  1325. struct v4l2_crop *p = arg;
  1326. if (!ops->vidioc_g_crop)
  1327. break;
  1328. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1329. ret = ops->vidioc_g_crop(file, fh, p);
  1330. if (!ret)
  1331. dbgrect(vfd, "", &p->c);
  1332. break;
  1333. }
  1334. case VIDIOC_S_CROP:
  1335. {
  1336. struct v4l2_crop *p = arg;
  1337. if (!ops->vidioc_s_crop)
  1338. break;
  1339. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1340. dbgrect(vfd, "", &p->c);
  1341. ret = ops->vidioc_s_crop(file, fh, p);
  1342. break;
  1343. }
  1344. case VIDIOC_CROPCAP:
  1345. {
  1346. struct v4l2_cropcap *p = arg;
  1347. /*FIXME: Should also show v4l2_fract pixelaspect */
  1348. if (!ops->vidioc_cropcap)
  1349. break;
  1350. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1351. ret = ops->vidioc_cropcap(file, fh, p);
  1352. if (!ret) {
  1353. dbgrect(vfd, "bounds ", &p->bounds);
  1354. dbgrect(vfd, "defrect ", &p->defrect);
  1355. }
  1356. break;
  1357. }
  1358. case VIDIOC_G_JPEGCOMP:
  1359. {
  1360. struct v4l2_jpegcompression *p = arg;
  1361. if (!ops->vidioc_g_jpegcomp)
  1362. break;
  1363. ret = ops->vidioc_g_jpegcomp(file, fh, p);
  1364. if (!ret)
  1365. dbgarg(cmd, "quality=%d, APPn=%d, "
  1366. "APP_len=%d, COM_len=%d, "
  1367. "jpeg_markers=%d\n",
  1368. p->quality, p->APPn, p->APP_len,
  1369. p->COM_len, p->jpeg_markers);
  1370. break;
  1371. }
  1372. case VIDIOC_S_JPEGCOMP:
  1373. {
  1374. struct v4l2_jpegcompression *p = arg;
  1375. if (!ops->vidioc_g_jpegcomp)
  1376. break;
  1377. dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
  1378. "COM_len=%d, jpeg_markers=%d\n",
  1379. p->quality, p->APPn, p->APP_len,
  1380. p->COM_len, p->jpeg_markers);
  1381. ret = ops->vidioc_s_jpegcomp(file, fh, p);
  1382. break;
  1383. }
  1384. case VIDIOC_G_ENC_INDEX:
  1385. {
  1386. struct v4l2_enc_idx *p = arg;
  1387. if (!ops->vidioc_g_enc_index)
  1388. break;
  1389. ret = ops->vidioc_g_enc_index(file, fh, p);
  1390. if (!ret)
  1391. dbgarg(cmd, "entries=%d, entries_cap=%d\n",
  1392. p->entries, p->entries_cap);
  1393. break;
  1394. }
  1395. case VIDIOC_ENCODER_CMD:
  1396. {
  1397. struct v4l2_encoder_cmd *p = arg;
  1398. if (!ops->vidioc_encoder_cmd)
  1399. break;
  1400. ret = ops->vidioc_encoder_cmd(file, fh, p);
  1401. if (!ret)
  1402. dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
  1403. break;
  1404. }
  1405. case VIDIOC_TRY_ENCODER_CMD:
  1406. {
  1407. struct v4l2_encoder_cmd *p = arg;
  1408. if (!ops->vidioc_try_encoder_cmd)
  1409. break;
  1410. ret = ops->vidioc_try_encoder_cmd(file, fh, p);
  1411. if (!ret)
  1412. dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
  1413. break;
  1414. }
  1415. case VIDIOC_G_PARM:
  1416. {
  1417. struct v4l2_streamparm *p = arg;
  1418. if (ops->vidioc_g_parm) {
  1419. ret = check_fmt(ops, p->type);
  1420. if (ret)
  1421. break;
  1422. ret = ops->vidioc_g_parm(file, fh, p);
  1423. } else {
  1424. v4l2_std_id std = vfd->current_norm;
  1425. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1426. return -EINVAL;
  1427. ret = 0;
  1428. if (ops->vidioc_g_std)
  1429. ret = ops->vidioc_g_std(file, fh, &std);
  1430. else if (std == 0)
  1431. ret = -EINVAL;
  1432. if (ret == 0)
  1433. v4l2_video_std_frame_period(std,
  1434. &p->parm.capture.timeperframe);
  1435. }
  1436. dbgarg(cmd, "type=%d\n", p->type);
  1437. break;
  1438. }
  1439. case VIDIOC_S_PARM:
  1440. {
  1441. struct v4l2_streamparm *p = arg;
  1442. if (!ops->vidioc_s_parm)
  1443. break;
  1444. ret = check_fmt(ops, p->type);
  1445. if (ret)
  1446. break;
  1447. dbgarg(cmd, "type=%d\n", p->type);
  1448. ret = ops->vidioc_s_parm(file, fh, p);
  1449. break;
  1450. }
  1451. case VIDIOC_G_TUNER:
  1452. {
  1453. struct v4l2_tuner *p = arg;
  1454. if (!ops->vidioc_g_tuner)
  1455. break;
  1456. ret = ops->vidioc_g_tuner(file, fh, p);
  1457. if (!ret)
  1458. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1459. "capability=0x%x, rangelow=%d, "
  1460. "rangehigh=%d, signal=%d, afc=%d, "
  1461. "rxsubchans=0x%x, audmode=%d\n",
  1462. p->index, p->name, p->type,
  1463. p->capability, p->rangelow,
  1464. p->rangehigh, p->signal, p->afc,
  1465. p->rxsubchans, p->audmode);
  1466. break;
  1467. }
  1468. case VIDIOC_S_TUNER:
  1469. {
  1470. struct v4l2_tuner *p = arg;
  1471. if (!ops->vidioc_s_tuner)
  1472. break;
  1473. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1474. "capability=0x%x, rangelow=%d, "
  1475. "rangehigh=%d, signal=%d, afc=%d, "
  1476. "rxsubchans=0x%x, audmode=%d\n",
  1477. p->index, p->name, p->type,
  1478. p->capability, p->rangelow,
  1479. p->rangehigh, p->signal, p->afc,
  1480. p->rxsubchans, p->audmode);
  1481. ret = ops->vidioc_s_tuner(file, fh, p);
  1482. break;
  1483. }
  1484. case VIDIOC_G_FREQUENCY:
  1485. {
  1486. struct v4l2_frequency *p = arg;
  1487. if (!ops->vidioc_g_frequency)
  1488. break;
  1489. ret = ops->vidioc_g_frequency(file, fh, p);
  1490. if (!ret)
  1491. dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
  1492. p->tuner, p->type, p->frequency);
  1493. break;
  1494. }
  1495. case VIDIOC_S_FREQUENCY:
  1496. {
  1497. struct v4l2_frequency *p = arg;
  1498. if (!ops->vidioc_s_frequency)
  1499. break;
  1500. dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
  1501. p->tuner, p->type, p->frequency);
  1502. ret = ops->vidioc_s_frequency(file, fh, p);
  1503. break;
  1504. }
  1505. case VIDIOC_G_SLICED_VBI_CAP:
  1506. {
  1507. struct v4l2_sliced_vbi_cap *p = arg;
  1508. if (!ops->vidioc_g_sliced_vbi_cap)
  1509. break;
  1510. /* Clear up to type, everything after type is zerod already */
  1511. memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
  1512. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1513. ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
  1514. if (!ret)
  1515. dbgarg2("service_set=%d\n", p->service_set);
  1516. break;
  1517. }
  1518. case VIDIOC_LOG_STATUS:
  1519. {
  1520. if (!ops->vidioc_log_status)
  1521. break;
  1522. ret = ops->vidioc_log_status(file, fh);
  1523. break;
  1524. }
  1525. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1526. case VIDIOC_DBG_G_REGISTER:
  1527. {
  1528. struct v4l2_dbg_register *p = arg;
  1529. if (!capable(CAP_SYS_ADMIN))
  1530. ret = -EPERM;
  1531. else if (ops->vidioc_g_register)
  1532. ret = ops->vidioc_g_register(file, fh, p);
  1533. break;
  1534. }
  1535. case VIDIOC_DBG_S_REGISTER:
  1536. {
  1537. struct v4l2_dbg_register *p = arg;
  1538. if (!capable(CAP_SYS_ADMIN))
  1539. ret = -EPERM;
  1540. else if (ops->vidioc_s_register)
  1541. ret = ops->vidioc_s_register(file, fh, p);
  1542. break;
  1543. }
  1544. #endif
  1545. case VIDIOC_DBG_G_CHIP_IDENT:
  1546. {
  1547. struct v4l2_dbg_chip_ident *p = arg;
  1548. if (!ops->vidioc_g_chip_ident)
  1549. break;
  1550. p->ident = V4L2_IDENT_NONE;
  1551. p->revision = 0;
  1552. ret = ops->vidioc_g_chip_ident(file, fh, p);
  1553. if (!ret)
  1554. dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
  1555. break;
  1556. }
  1557. case VIDIOC_S_HW_FREQ_SEEK:
  1558. {
  1559. struct v4l2_hw_freq_seek *p = arg;
  1560. if (!ops->vidioc_s_hw_freq_seek)
  1561. break;
  1562. dbgarg(cmd,
  1563. "tuner=%d, type=%d, seek_upward=%d, wrap_around=%d\n",
  1564. p->tuner, p->type, p->seek_upward, p->wrap_around);
  1565. ret = ops->vidioc_s_hw_freq_seek(file, fh, p);
  1566. break;
  1567. }
  1568. case VIDIOC_ENUM_FRAMESIZES:
  1569. {
  1570. struct v4l2_frmsizeenum *p = arg;
  1571. if (!ops->vidioc_enum_framesizes)
  1572. break;
  1573. ret = ops->vidioc_enum_framesizes(file, fh, p);
  1574. dbgarg(cmd,
  1575. "index=%d, pixelformat=%c%c%c%c, type=%d ",
  1576. p->index,
  1577. (p->pixel_format & 0xff),
  1578. (p->pixel_format >> 8) & 0xff,
  1579. (p->pixel_format >> 16) & 0xff,
  1580. (p->pixel_format >> 24) & 0xff,
  1581. p->type);
  1582. switch (p->type) {
  1583. case V4L2_FRMSIZE_TYPE_DISCRETE:
  1584. dbgarg3("width = %d, height=%d\n",
  1585. p->discrete.width, p->discrete.height);
  1586. break;
  1587. case V4L2_FRMSIZE_TYPE_STEPWISE:
  1588. dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
  1589. p->stepwise.min_width, p->stepwise.min_height,
  1590. p->stepwise.step_width, p->stepwise.step_height,
  1591. p->stepwise.max_width, p->stepwise.max_height);
  1592. break;
  1593. case V4L2_FRMSIZE_TYPE_CONTINUOUS:
  1594. dbgarg3("continuous\n");
  1595. break;
  1596. default:
  1597. dbgarg3("- Unknown type!\n");
  1598. }
  1599. break;
  1600. }
  1601. case VIDIOC_ENUM_FRAMEINTERVALS:
  1602. {
  1603. struct v4l2_frmivalenum *p = arg;
  1604. if (!ops->vidioc_enum_frameintervals)
  1605. break;
  1606. ret = ops->vidioc_enum_frameintervals(file, fh, p);
  1607. dbgarg(cmd,
  1608. "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
  1609. p->index, p->pixel_format,
  1610. p->width, p->height, p->type);
  1611. switch (p->type) {
  1612. case V4L2_FRMIVAL_TYPE_DISCRETE:
  1613. dbgarg2("fps=%d/%d\n",
  1614. p->discrete.numerator,
  1615. p->discrete.denominator);
  1616. break;
  1617. case V4L2_FRMIVAL_TYPE_STEPWISE:
  1618. dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
  1619. p->stepwise.min.numerator,
  1620. p->stepwise.min.denominator,
  1621. p->stepwise.max.numerator,
  1622. p->stepwise.max.denominator,
  1623. p->stepwise.step.numerator,
  1624. p->stepwise.step.denominator);
  1625. break;
  1626. case V4L2_FRMIVAL_TYPE_CONTINUOUS:
  1627. dbgarg2("continuous\n");
  1628. break;
  1629. default:
  1630. dbgarg2("- Unknown type!\n");
  1631. }
  1632. break;
  1633. }
  1634. default:
  1635. {
  1636. if (!ops->vidioc_default)
  1637. break;
  1638. ret = ops->vidioc_default(file, fh, cmd, arg);
  1639. break;
  1640. }
  1641. } /* switch */
  1642. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
  1643. if (ret < 0) {
  1644. v4l_print_ioctl(vfd->name, cmd);
  1645. printk(KERN_CONT " error %ld\n", ret);
  1646. }
  1647. }
  1648. return ret;
  1649. }
  1650. /* In some cases, only a few fields are used as input, i.e. when the app sets
  1651. * "index" and then the driver fills in the rest of the structure for the thing
  1652. * with that index. We only need to copy up the first non-input field. */
  1653. static unsigned long cmd_input_size(unsigned int cmd)
  1654. {
  1655. /* Size of structure up to and including 'field' */
  1656. #define CMDINSIZE(cmd, type, field) \
  1657. case VIDIOC_##cmd: \
  1658. return offsetof(struct v4l2_##type, field) + \
  1659. sizeof(((struct v4l2_##type *)0)->field);
  1660. switch (cmd) {
  1661. CMDINSIZE(ENUM_FMT, fmtdesc, type);
  1662. CMDINSIZE(G_FMT, format, type);
  1663. CMDINSIZE(QUERYBUF, buffer, type);
  1664. CMDINSIZE(G_PARM, streamparm, type);
  1665. CMDINSIZE(ENUMSTD, standard, index);
  1666. CMDINSIZE(ENUMINPUT, input, index);
  1667. CMDINSIZE(G_CTRL, control, id);
  1668. CMDINSIZE(G_TUNER, tuner, index);
  1669. CMDINSIZE(QUERYCTRL, queryctrl, id);
  1670. CMDINSIZE(QUERYMENU, querymenu, index);
  1671. CMDINSIZE(ENUMOUTPUT, output, index);
  1672. CMDINSIZE(G_MODULATOR, modulator, index);
  1673. CMDINSIZE(G_FREQUENCY, frequency, tuner);
  1674. CMDINSIZE(CROPCAP, cropcap, type);
  1675. CMDINSIZE(G_CROP, crop, type);
  1676. CMDINSIZE(ENUMAUDIO, audio, index);
  1677. CMDINSIZE(ENUMAUDOUT, audioout, index);
  1678. CMDINSIZE(ENCODER_CMD, encoder_cmd, flags);
  1679. CMDINSIZE(TRY_ENCODER_CMD, encoder_cmd, flags);
  1680. CMDINSIZE(G_SLICED_VBI_CAP, sliced_vbi_cap, type);
  1681. CMDINSIZE(ENUM_FRAMESIZES, frmsizeenum, pixel_format);
  1682. CMDINSIZE(ENUM_FRAMEINTERVALS, frmivalenum, height);
  1683. default:
  1684. return _IOC_SIZE(cmd);
  1685. }
  1686. }
  1687. long video_ioctl2(struct file *file,
  1688. unsigned int cmd, unsigned long arg)
  1689. {
  1690. char sbuf[128];
  1691. void *mbuf = NULL;
  1692. void *parg = NULL;
  1693. long err = -EINVAL;
  1694. int is_ext_ctrl;
  1695. size_t ctrls_size = 0;
  1696. void __user *user_ptr = NULL;
  1697. #ifdef __OLD_VIDIOC_
  1698. cmd = video_fix_command(cmd);
  1699. #endif
  1700. is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
  1701. cmd == VIDIOC_TRY_EXT_CTRLS);
  1702. /* Copy arguments into temp kernel buffer */
  1703. if (_IOC_DIR(cmd) != _IOC_NONE) {
  1704. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  1705. parg = sbuf;
  1706. } else {
  1707. /* too big to allocate from stack */
  1708. mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  1709. if (NULL == mbuf)
  1710. return -ENOMEM;
  1711. parg = mbuf;
  1712. }
  1713. err = -EFAULT;
  1714. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  1715. unsigned long n = cmd_input_size(cmd);
  1716. if (copy_from_user(parg, (void __user *)arg, n))
  1717. goto out;
  1718. /* zero out anything we don't copy from userspace */
  1719. if (n < _IOC_SIZE(cmd))
  1720. memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
  1721. } else {
  1722. /* read-only ioctl */
  1723. memset(parg, 0, _IOC_SIZE(cmd));
  1724. }
  1725. }
  1726. if (is_ext_ctrl) {
  1727. struct v4l2_ext_controls *p = parg;
  1728. /* In case of an error, tell the caller that it wasn't
  1729. a specific control that caused it. */
  1730. p->error_idx = p->count;
  1731. user_ptr = (void __user *)p->controls;
  1732. if (p->count) {
  1733. ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
  1734. /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
  1735. mbuf = kmalloc(ctrls_size, GFP_KERNEL);
  1736. err = -ENOMEM;
  1737. if (NULL == mbuf)
  1738. goto out_ext_ctrl;
  1739. err = -EFAULT;
  1740. if (copy_from_user(mbuf, user_ptr, ctrls_size))
  1741. goto out_ext_ctrl;
  1742. p->controls = mbuf;
  1743. }
  1744. }
  1745. /* Handles IOCTL */
  1746. err = __video_do_ioctl(file, cmd, parg);
  1747. if (err == -ENOIOCTLCMD)
  1748. err = -EINVAL;
  1749. if (is_ext_ctrl) {
  1750. struct v4l2_ext_controls *p = parg;
  1751. p->controls = (void *)user_ptr;
  1752. if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
  1753. err = -EFAULT;
  1754. goto out_ext_ctrl;
  1755. }
  1756. if (err < 0)
  1757. goto out;
  1758. out_ext_ctrl:
  1759. /* Copy results into user buffer */
  1760. switch (_IOC_DIR(cmd)) {
  1761. case _IOC_READ:
  1762. case (_IOC_WRITE | _IOC_READ):
  1763. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  1764. err = -EFAULT;
  1765. break;
  1766. }
  1767. out:
  1768. kfree(mbuf);
  1769. return err;
  1770. }
  1771. EXPORT_SYMBOL(video_ioctl2);