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)
  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", c->controls[i].id);
  477. }
  478. printk(KERN_CONT "\n");
  479. };
  480. static inline int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
  481. {
  482. __u32 i;
  483. /* zero the reserved fields */
  484. c->reserved[0] = c->reserved[1] = 0;
  485. for (i = 0; i < c->count; i++) {
  486. c->controls[i].reserved2[0] = 0;
  487. c->controls[i].reserved2[1] = 0;
  488. }
  489. /* V4L2_CID_PRIVATE_BASE cannot be used as control class
  490. when using extended controls.
  491. Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
  492. is it allowed for backwards compatibility.
  493. */
  494. if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
  495. return 0;
  496. /* Check that all controls are from the same control class. */
  497. for (i = 0; i < c->count; i++) {
  498. if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
  499. c->error_idx = i;
  500. return 0;
  501. }
  502. }
  503. return 1;
  504. }
  505. static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
  506. {
  507. if (ops == NULL)
  508. return -EINVAL;
  509. switch (type) {
  510. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  511. if (ops->vidioc_g_fmt_vid_cap)
  512. return 0;
  513. break;
  514. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  515. if (ops->vidioc_g_fmt_vid_overlay)
  516. return 0;
  517. break;
  518. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  519. if (ops->vidioc_g_fmt_vid_out)
  520. return 0;
  521. break;
  522. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  523. if (ops->vidioc_g_fmt_vid_out_overlay)
  524. return 0;
  525. break;
  526. case V4L2_BUF_TYPE_VBI_CAPTURE:
  527. if (ops->vidioc_g_fmt_vbi_cap)
  528. return 0;
  529. break;
  530. case V4L2_BUF_TYPE_VBI_OUTPUT:
  531. if (ops->vidioc_g_fmt_vbi_out)
  532. return 0;
  533. break;
  534. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  535. if (ops->vidioc_g_fmt_sliced_vbi_cap)
  536. return 0;
  537. break;
  538. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  539. if (ops->vidioc_g_fmt_sliced_vbi_out)
  540. return 0;
  541. break;
  542. case V4L2_BUF_TYPE_PRIVATE:
  543. if (ops->vidioc_g_fmt_type_private)
  544. return 0;
  545. break;
  546. }
  547. return -EINVAL;
  548. }
  549. static long __video_do_ioctl(struct file *file,
  550. unsigned int cmd, void *arg)
  551. {
  552. struct video_device *vfd = video_devdata(file);
  553. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  554. void *fh = file->private_data;
  555. long ret = -EINVAL;
  556. if ((vfd->debug & V4L2_DEBUG_IOCTL) &&
  557. !(vfd->debug & V4L2_DEBUG_IOCTL_ARG)) {
  558. v4l_print_ioctl(vfd->name, cmd);
  559. printk(KERN_CONT "\n");
  560. }
  561. if (ops == NULL) {
  562. printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
  563. vfd->name);
  564. return -EINVAL;
  565. }
  566. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  567. /***********************************************************
  568. Handles calls to the obsoleted V4L1 API
  569. Due to the nature of VIDIOCGMBUF, each driver that supports
  570. V4L1 should implement its own handler for this ioctl.
  571. ***********************************************************/
  572. /* --- streaming capture ------------------------------------- */
  573. if (cmd == VIDIOCGMBUF) {
  574. struct video_mbuf *p = arg;
  575. if (!ops->vidiocgmbuf)
  576. return ret;
  577. ret = ops->vidiocgmbuf(file, fh, p);
  578. if (!ret)
  579. dbgarg(cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
  580. p->size, p->frames,
  581. (unsigned long)p->offsets);
  582. return ret;
  583. }
  584. /********************************************************
  585. All other V4L1 calls are handled by v4l1_compat module.
  586. Those calls will be translated into V4L2 calls, and
  587. __video_do_ioctl will be called again, with one or more
  588. V4L2 ioctls.
  589. ********************************************************/
  590. if (_IOC_TYPE(cmd) == 'v' && _IOC_NR(cmd) < BASE_VIDIOCPRIVATE)
  591. return v4l_compat_translate_ioctl(file, cmd, arg,
  592. __video_do_ioctl);
  593. #endif
  594. switch (cmd) {
  595. /* --- capabilities ------------------------------------------ */
  596. case VIDIOC_QUERYCAP:
  597. {
  598. struct v4l2_capability *cap = (struct v4l2_capability *)arg;
  599. if (!ops->vidioc_querycap)
  600. break;
  601. ret = ops->vidioc_querycap(file, fh, cap);
  602. if (!ret)
  603. dbgarg(cmd, "driver=%s, card=%s, bus=%s, "
  604. "version=0x%08x, "
  605. "capabilities=0x%08x\n",
  606. cap->driver, cap->card, cap->bus_info,
  607. cap->version,
  608. cap->capabilities);
  609. break;
  610. }
  611. /* --- priority ------------------------------------------ */
  612. case VIDIOC_G_PRIORITY:
  613. {
  614. enum v4l2_priority *p = arg;
  615. if (!ops->vidioc_g_priority)
  616. break;
  617. ret = ops->vidioc_g_priority(file, fh, p);
  618. if (!ret)
  619. dbgarg(cmd, "priority is %d\n", *p);
  620. break;
  621. }
  622. case VIDIOC_S_PRIORITY:
  623. {
  624. enum v4l2_priority *p = arg;
  625. if (!ops->vidioc_s_priority)
  626. break;
  627. dbgarg(cmd, "setting priority to %d\n", *p);
  628. ret = ops->vidioc_s_priority(file, fh, *p);
  629. break;
  630. }
  631. /* --- capture ioctls ---------------------------------------- */
  632. case VIDIOC_ENUM_FMT:
  633. {
  634. struct v4l2_fmtdesc *f = arg;
  635. switch (f->type) {
  636. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  637. if (ops->vidioc_enum_fmt_vid_cap)
  638. ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f);
  639. break;
  640. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  641. if (ops->vidioc_enum_fmt_vid_overlay)
  642. ret = ops->vidioc_enum_fmt_vid_overlay(file,
  643. fh, f);
  644. break;
  645. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  646. if (ops->vidioc_enum_fmt_vid_out)
  647. ret = ops->vidioc_enum_fmt_vid_out(file, fh, f);
  648. break;
  649. case V4L2_BUF_TYPE_PRIVATE:
  650. if (ops->vidioc_enum_fmt_type_private)
  651. ret = ops->vidioc_enum_fmt_type_private(file,
  652. fh, f);
  653. break;
  654. default:
  655. break;
  656. }
  657. if (!ret)
  658. dbgarg(cmd, "index=%d, type=%d, flags=%d, "
  659. "pixelformat=%c%c%c%c, description='%s'\n",
  660. f->index, f->type, f->flags,
  661. (f->pixelformat & 0xff),
  662. (f->pixelformat >> 8) & 0xff,
  663. (f->pixelformat >> 16) & 0xff,
  664. (f->pixelformat >> 24) & 0xff,
  665. f->description);
  666. break;
  667. }
  668. case VIDIOC_G_FMT:
  669. {
  670. struct v4l2_format *f = (struct v4l2_format *)arg;
  671. /* FIXME: Should be one dump per type */
  672. dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
  673. switch (f->type) {
  674. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  675. if (ops->vidioc_g_fmt_vid_cap)
  676. ret = ops->vidioc_g_fmt_vid_cap(file, fh, f);
  677. if (!ret)
  678. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  679. break;
  680. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  681. if (ops->vidioc_g_fmt_vid_overlay)
  682. ret = ops->vidioc_g_fmt_vid_overlay(file,
  683. fh, f);
  684. break;
  685. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  686. if (ops->vidioc_g_fmt_vid_out)
  687. ret = ops->vidioc_g_fmt_vid_out(file, fh, f);
  688. if (!ret)
  689. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  690. break;
  691. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  692. if (ops->vidioc_g_fmt_vid_out_overlay)
  693. ret = ops->vidioc_g_fmt_vid_out_overlay(file,
  694. fh, f);
  695. break;
  696. case V4L2_BUF_TYPE_VBI_CAPTURE:
  697. if (ops->vidioc_g_fmt_vbi_cap)
  698. ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f);
  699. break;
  700. case V4L2_BUF_TYPE_VBI_OUTPUT:
  701. if (ops->vidioc_g_fmt_vbi_out)
  702. ret = ops->vidioc_g_fmt_vbi_out(file, fh, f);
  703. break;
  704. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  705. if (ops->vidioc_g_fmt_sliced_vbi_cap)
  706. ret = ops->vidioc_g_fmt_sliced_vbi_cap(file,
  707. fh, f);
  708. break;
  709. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  710. if (ops->vidioc_g_fmt_sliced_vbi_out)
  711. ret = ops->vidioc_g_fmt_sliced_vbi_out(file,
  712. fh, f);
  713. break;
  714. case V4L2_BUF_TYPE_PRIVATE:
  715. if (ops->vidioc_g_fmt_type_private)
  716. ret = ops->vidioc_g_fmt_type_private(file,
  717. fh, f);
  718. break;
  719. }
  720. break;
  721. }
  722. case VIDIOC_S_FMT:
  723. {
  724. struct v4l2_format *f = (struct v4l2_format *)arg;
  725. /* FIXME: Should be one dump per type */
  726. dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
  727. switch (f->type) {
  728. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  729. CLEAR_AFTER_FIELD(f, fmt.pix);
  730. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  731. if (ops->vidioc_s_fmt_vid_cap)
  732. ret = ops->vidioc_s_fmt_vid_cap(file, fh, f);
  733. break;
  734. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  735. CLEAR_AFTER_FIELD(f, fmt.win);
  736. if (ops->vidioc_s_fmt_vid_overlay)
  737. ret = ops->vidioc_s_fmt_vid_overlay(file,
  738. fh, f);
  739. break;
  740. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  741. CLEAR_AFTER_FIELD(f, fmt.pix);
  742. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  743. if (ops->vidioc_s_fmt_vid_out)
  744. ret = ops->vidioc_s_fmt_vid_out(file, fh, f);
  745. break;
  746. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  747. CLEAR_AFTER_FIELD(f, fmt.win);
  748. if (ops->vidioc_s_fmt_vid_out_overlay)
  749. ret = ops->vidioc_s_fmt_vid_out_overlay(file,
  750. fh, f);
  751. break;
  752. case V4L2_BUF_TYPE_VBI_CAPTURE:
  753. CLEAR_AFTER_FIELD(f, fmt.vbi);
  754. if (ops->vidioc_s_fmt_vbi_cap)
  755. ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
  756. break;
  757. case V4L2_BUF_TYPE_VBI_OUTPUT:
  758. CLEAR_AFTER_FIELD(f, fmt.vbi);
  759. if (ops->vidioc_s_fmt_vbi_out)
  760. ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
  761. break;
  762. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  763. CLEAR_AFTER_FIELD(f, fmt.sliced);
  764. if (ops->vidioc_s_fmt_sliced_vbi_cap)
  765. ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
  766. fh, f);
  767. break;
  768. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  769. CLEAR_AFTER_FIELD(f, fmt.sliced);
  770. if (ops->vidioc_s_fmt_sliced_vbi_out)
  771. ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
  772. fh, f);
  773. break;
  774. case V4L2_BUF_TYPE_PRIVATE:
  775. /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
  776. if (ops->vidioc_s_fmt_type_private)
  777. ret = ops->vidioc_s_fmt_type_private(file,
  778. fh, f);
  779. break;
  780. }
  781. break;
  782. }
  783. case VIDIOC_TRY_FMT:
  784. {
  785. struct v4l2_format *f = (struct v4l2_format *)arg;
  786. /* FIXME: Should be one dump per type */
  787. dbgarg(cmd, "type=%s\n", prt_names(f->type,
  788. v4l2_type_names));
  789. switch (f->type) {
  790. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  791. CLEAR_AFTER_FIELD(f, fmt.pix);
  792. if (ops->vidioc_try_fmt_vid_cap)
  793. ret = ops->vidioc_try_fmt_vid_cap(file, fh, f);
  794. if (!ret)
  795. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  796. break;
  797. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  798. CLEAR_AFTER_FIELD(f, fmt.win);
  799. if (ops->vidioc_try_fmt_vid_overlay)
  800. ret = ops->vidioc_try_fmt_vid_overlay(file,
  801. fh, f);
  802. break;
  803. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  804. CLEAR_AFTER_FIELD(f, fmt.pix);
  805. if (ops->vidioc_try_fmt_vid_out)
  806. ret = ops->vidioc_try_fmt_vid_out(file, fh, f);
  807. if (!ret)
  808. v4l_print_pix_fmt(vfd, &f->fmt.pix);
  809. break;
  810. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  811. CLEAR_AFTER_FIELD(f, fmt.win);
  812. if (ops->vidioc_try_fmt_vid_out_overlay)
  813. ret = ops->vidioc_try_fmt_vid_out_overlay(file,
  814. fh, f);
  815. break;
  816. case V4L2_BUF_TYPE_VBI_CAPTURE:
  817. CLEAR_AFTER_FIELD(f, fmt.vbi);
  818. if (ops->vidioc_try_fmt_vbi_cap)
  819. ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f);
  820. break;
  821. case V4L2_BUF_TYPE_VBI_OUTPUT:
  822. CLEAR_AFTER_FIELD(f, fmt.vbi);
  823. if (ops->vidioc_try_fmt_vbi_out)
  824. ret = ops->vidioc_try_fmt_vbi_out(file, fh, f);
  825. break;
  826. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  827. CLEAR_AFTER_FIELD(f, fmt.sliced);
  828. if (ops->vidioc_try_fmt_sliced_vbi_cap)
  829. ret = ops->vidioc_try_fmt_sliced_vbi_cap(file,
  830. fh, f);
  831. break;
  832. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  833. CLEAR_AFTER_FIELD(f, fmt.sliced);
  834. if (ops->vidioc_try_fmt_sliced_vbi_out)
  835. ret = ops->vidioc_try_fmt_sliced_vbi_out(file,
  836. fh, f);
  837. break;
  838. case V4L2_BUF_TYPE_PRIVATE:
  839. /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
  840. if (ops->vidioc_try_fmt_type_private)
  841. ret = ops->vidioc_try_fmt_type_private(file,
  842. fh, f);
  843. break;
  844. }
  845. break;
  846. }
  847. /* FIXME: Those buf reqs could be handled here,
  848. with some changes on videobuf to allow its header to be included at
  849. videodev2.h or being merged at videodev2.
  850. */
  851. case VIDIOC_REQBUFS:
  852. {
  853. struct v4l2_requestbuffers *p = arg;
  854. if (!ops->vidioc_reqbufs)
  855. break;
  856. ret = check_fmt(ops, p->type);
  857. if (ret)
  858. break;
  859. if (p->type < V4L2_BUF_TYPE_PRIVATE)
  860. CLEAR_AFTER_FIELD(p, memory);
  861. ret = ops->vidioc_reqbufs(file, fh, p);
  862. dbgarg(cmd, "count=%d, type=%s, memory=%s\n",
  863. p->count,
  864. prt_names(p->type, v4l2_type_names),
  865. prt_names(p->memory, v4l2_memory_names));
  866. break;
  867. }
  868. case VIDIOC_QUERYBUF:
  869. {
  870. struct v4l2_buffer *p = arg;
  871. if (!ops->vidioc_querybuf)
  872. break;
  873. ret = check_fmt(ops, p->type);
  874. if (ret)
  875. break;
  876. ret = ops->vidioc_querybuf(file, fh, p);
  877. if (!ret)
  878. dbgbuf(cmd, vfd, p);
  879. break;
  880. }
  881. case VIDIOC_QBUF:
  882. {
  883. struct v4l2_buffer *p = arg;
  884. if (!ops->vidioc_qbuf)
  885. break;
  886. ret = check_fmt(ops, p->type);
  887. if (ret)
  888. break;
  889. ret = ops->vidioc_qbuf(file, fh, p);
  890. if (!ret)
  891. dbgbuf(cmd, vfd, p);
  892. break;
  893. }
  894. case VIDIOC_DQBUF:
  895. {
  896. struct v4l2_buffer *p = arg;
  897. if (!ops->vidioc_dqbuf)
  898. break;
  899. ret = check_fmt(ops, p->type);
  900. if (ret)
  901. break;
  902. ret = ops->vidioc_dqbuf(file, fh, p);
  903. if (!ret)
  904. dbgbuf(cmd, vfd, p);
  905. break;
  906. }
  907. case VIDIOC_OVERLAY:
  908. {
  909. int *i = arg;
  910. if (!ops->vidioc_overlay)
  911. break;
  912. dbgarg(cmd, "value=%d\n", *i);
  913. ret = ops->vidioc_overlay(file, fh, *i);
  914. break;
  915. }
  916. case VIDIOC_G_FBUF:
  917. {
  918. struct v4l2_framebuffer *p = arg;
  919. if (!ops->vidioc_g_fbuf)
  920. break;
  921. ret = ops->vidioc_g_fbuf(file, fh, arg);
  922. if (!ret) {
  923. dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
  924. p->capability, p->flags,
  925. (unsigned long)p->base);
  926. v4l_print_pix_fmt(vfd, &p->fmt);
  927. }
  928. break;
  929. }
  930. case VIDIOC_S_FBUF:
  931. {
  932. struct v4l2_framebuffer *p = arg;
  933. if (!ops->vidioc_s_fbuf)
  934. break;
  935. dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
  936. p->capability, p->flags, (unsigned long)p->base);
  937. v4l_print_pix_fmt(vfd, &p->fmt);
  938. ret = ops->vidioc_s_fbuf(file, fh, arg);
  939. break;
  940. }
  941. case VIDIOC_STREAMON:
  942. {
  943. enum v4l2_buf_type i = *(int *)arg;
  944. if (!ops->vidioc_streamon)
  945. break;
  946. dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
  947. ret = ops->vidioc_streamon(file, fh, i);
  948. break;
  949. }
  950. case VIDIOC_STREAMOFF:
  951. {
  952. enum v4l2_buf_type i = *(int *)arg;
  953. if (!ops->vidioc_streamoff)
  954. break;
  955. dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
  956. ret = ops->vidioc_streamoff(file, fh, i);
  957. break;
  958. }
  959. /* ---------- tv norms ---------- */
  960. case VIDIOC_ENUMSTD:
  961. {
  962. struct v4l2_standard *p = arg;
  963. v4l2_std_id id = vfd->tvnorms, curr_id = 0;
  964. unsigned int index = p->index, i, j = 0;
  965. const char *descr = "";
  966. /* Return norm array in a canonical way */
  967. for (i = 0; i <= index && id; i++) {
  968. /* last std value in the standards array is 0, so this
  969. while always ends there since (id & 0) == 0. */
  970. while ((id & standards[j].std) != standards[j].std)
  971. j++;
  972. curr_id = standards[j].std;
  973. descr = standards[j].descr;
  974. j++;
  975. if (curr_id == 0)
  976. break;
  977. if (curr_id != V4L2_STD_PAL &&
  978. curr_id != V4L2_STD_SECAM &&
  979. curr_id != V4L2_STD_NTSC)
  980. id &= ~curr_id;
  981. }
  982. if (i <= index)
  983. return -EINVAL;
  984. v4l2_video_std_construct(p, curr_id, descr);
  985. dbgarg(cmd, "index=%d, id=0x%Lx, name=%s, fps=%d/%d, "
  986. "framelines=%d\n", p->index,
  987. (unsigned long long)p->id, p->name,
  988. p->frameperiod.numerator,
  989. p->frameperiod.denominator,
  990. p->framelines);
  991. ret = 0;
  992. break;
  993. }
  994. case VIDIOC_G_STD:
  995. {
  996. v4l2_std_id *id = arg;
  997. ret = 0;
  998. /* Calls the specific handler */
  999. if (ops->vidioc_g_std)
  1000. ret = ops->vidioc_g_std(file, fh, id);
  1001. else if (vfd->current_norm)
  1002. *id = vfd->current_norm;
  1003. else
  1004. ret = -EINVAL;
  1005. if (!ret)
  1006. dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
  1007. break;
  1008. }
  1009. case VIDIOC_S_STD:
  1010. {
  1011. v4l2_std_id *id = arg, norm;
  1012. dbgarg(cmd, "std=%08Lx\n", (long long unsigned)*id);
  1013. norm = (*id) & vfd->tvnorms;
  1014. if (vfd->tvnorms && !norm) /* Check if std is supported */
  1015. break;
  1016. /* Calls the specific handler */
  1017. if (ops->vidioc_s_std)
  1018. ret = ops->vidioc_s_std(file, fh, &norm);
  1019. else
  1020. ret = -EINVAL;
  1021. /* Updates standard information */
  1022. if (ret >= 0)
  1023. vfd->current_norm = norm;
  1024. break;
  1025. }
  1026. case VIDIOC_QUERYSTD:
  1027. {
  1028. v4l2_std_id *p = arg;
  1029. if (!ops->vidioc_querystd)
  1030. break;
  1031. ret = ops->vidioc_querystd(file, fh, arg);
  1032. if (!ret)
  1033. dbgarg(cmd, "detected std=%08Lx\n",
  1034. (unsigned long long)*p);
  1035. break;
  1036. }
  1037. /* ------ input switching ---------- */
  1038. /* FIXME: Inputs can be handled inside videodev2 */
  1039. case VIDIOC_ENUMINPUT:
  1040. {
  1041. struct v4l2_input *p = arg;
  1042. if (!ops->vidioc_enum_input)
  1043. break;
  1044. ret = ops->vidioc_enum_input(file, fh, p);
  1045. if (!ret)
  1046. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1047. "audioset=%d, "
  1048. "tuner=%d, std=%08Lx, status=%d\n",
  1049. p->index, p->name, p->type, p->audioset,
  1050. p->tuner,
  1051. (unsigned long long)p->std,
  1052. p->status);
  1053. break;
  1054. }
  1055. case VIDIOC_G_INPUT:
  1056. {
  1057. unsigned int *i = arg;
  1058. if (!ops->vidioc_g_input)
  1059. break;
  1060. ret = ops->vidioc_g_input(file, fh, i);
  1061. if (!ret)
  1062. dbgarg(cmd, "value=%d\n", *i);
  1063. break;
  1064. }
  1065. case VIDIOC_S_INPUT:
  1066. {
  1067. unsigned int *i = arg;
  1068. if (!ops->vidioc_s_input)
  1069. break;
  1070. dbgarg(cmd, "value=%d\n", *i);
  1071. ret = ops->vidioc_s_input(file, fh, *i);
  1072. break;
  1073. }
  1074. /* ------ output switching ---------- */
  1075. case VIDIOC_ENUMOUTPUT:
  1076. {
  1077. struct v4l2_output *p = arg;
  1078. if (!ops->vidioc_enum_output)
  1079. break;
  1080. ret = ops->vidioc_enum_output(file, fh, p);
  1081. if (!ret)
  1082. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1083. "audioset=0x%x, "
  1084. "modulator=%d, std=0x%08Lx\n",
  1085. p->index, p->name, p->type, p->audioset,
  1086. p->modulator, (unsigned long long)p->std);
  1087. break;
  1088. }
  1089. case VIDIOC_G_OUTPUT:
  1090. {
  1091. unsigned int *i = arg;
  1092. if (!ops->vidioc_g_output)
  1093. break;
  1094. ret = ops->vidioc_g_output(file, fh, i);
  1095. if (!ret)
  1096. dbgarg(cmd, "value=%d\n", *i);
  1097. break;
  1098. }
  1099. case VIDIOC_S_OUTPUT:
  1100. {
  1101. unsigned int *i = arg;
  1102. if (!ops->vidioc_s_output)
  1103. break;
  1104. dbgarg(cmd, "value=%d\n", *i);
  1105. ret = ops->vidioc_s_output(file, fh, *i);
  1106. break;
  1107. }
  1108. /* --- controls ---------------------------------------------- */
  1109. case VIDIOC_QUERYCTRL:
  1110. {
  1111. struct v4l2_queryctrl *p = arg;
  1112. if (!ops->vidioc_queryctrl)
  1113. break;
  1114. ret = ops->vidioc_queryctrl(file, fh, p);
  1115. if (!ret)
  1116. dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
  1117. "step=%d, default=%d, flags=0x%08x\n",
  1118. p->id, p->type, p->name,
  1119. p->minimum, p->maximum,
  1120. p->step, p->default_value, p->flags);
  1121. else
  1122. dbgarg(cmd, "id=0x%x\n", p->id);
  1123. break;
  1124. }
  1125. case VIDIOC_G_CTRL:
  1126. {
  1127. struct v4l2_control *p = arg;
  1128. if (ops->vidioc_g_ctrl)
  1129. ret = ops->vidioc_g_ctrl(file, fh, p);
  1130. else if (ops->vidioc_g_ext_ctrls) {
  1131. struct v4l2_ext_controls ctrls;
  1132. struct v4l2_ext_control ctrl;
  1133. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1134. ctrls.count = 1;
  1135. ctrls.controls = &ctrl;
  1136. ctrl.id = p->id;
  1137. ctrl.value = p->value;
  1138. if (check_ext_ctrls(&ctrls, 1)) {
  1139. ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
  1140. if (ret == 0)
  1141. p->value = ctrl.value;
  1142. }
  1143. } else
  1144. break;
  1145. if (!ret)
  1146. dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
  1147. else
  1148. dbgarg(cmd, "id=0x%x\n", p->id);
  1149. break;
  1150. }
  1151. case VIDIOC_S_CTRL:
  1152. {
  1153. struct v4l2_control *p = arg;
  1154. struct v4l2_ext_controls ctrls;
  1155. struct v4l2_ext_control ctrl;
  1156. if (!ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
  1157. break;
  1158. dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
  1159. if (ops->vidioc_s_ctrl) {
  1160. ret = ops->vidioc_s_ctrl(file, fh, p);
  1161. break;
  1162. }
  1163. if (!ops->vidioc_s_ext_ctrls)
  1164. break;
  1165. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1166. ctrls.count = 1;
  1167. ctrls.controls = &ctrl;
  1168. ctrl.id = p->id;
  1169. ctrl.value = p->value;
  1170. if (check_ext_ctrls(&ctrls, 1))
  1171. ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
  1172. break;
  1173. }
  1174. case VIDIOC_G_EXT_CTRLS:
  1175. {
  1176. struct v4l2_ext_controls *p = arg;
  1177. p->error_idx = p->count;
  1178. if (!ops->vidioc_g_ext_ctrls)
  1179. break;
  1180. if (check_ext_ctrls(p, 0))
  1181. ret = ops->vidioc_g_ext_ctrls(file, fh, p);
  1182. v4l_print_ext_ctrls(cmd, vfd, p, !ret);
  1183. break;
  1184. }
  1185. case VIDIOC_S_EXT_CTRLS:
  1186. {
  1187. struct v4l2_ext_controls *p = arg;
  1188. p->error_idx = p->count;
  1189. if (!ops->vidioc_s_ext_ctrls)
  1190. break;
  1191. v4l_print_ext_ctrls(cmd, vfd, p, 1);
  1192. if (check_ext_ctrls(p, 0))
  1193. ret = ops->vidioc_s_ext_ctrls(file, fh, p);
  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 (!ops->vidioc_try_ext_ctrls)
  1201. break;
  1202. v4l_print_ext_ctrls(cmd, vfd, p, 1);
  1203. if (check_ext_ctrls(p, 0))
  1204. ret = ops->vidioc_try_ext_ctrls(file, fh, p);
  1205. break;
  1206. }
  1207. case VIDIOC_QUERYMENU:
  1208. {
  1209. struct v4l2_querymenu *p = arg;
  1210. if (!ops->vidioc_querymenu)
  1211. break;
  1212. ret = ops->vidioc_querymenu(file, fh, p);
  1213. if (!ret)
  1214. dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
  1215. p->id, p->index, p->name);
  1216. else
  1217. dbgarg(cmd, "id=0x%x, index=%d\n",
  1218. p->id, p->index);
  1219. break;
  1220. }
  1221. /* --- audio ---------------------------------------------- */
  1222. case VIDIOC_ENUMAUDIO:
  1223. {
  1224. struct v4l2_audio *p = arg;
  1225. if (!ops->vidioc_enumaudio)
  1226. break;
  1227. ret = ops->vidioc_enumaudio(file, fh, p);
  1228. if (!ret)
  1229. dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
  1230. "mode=0x%x\n", p->index, p->name,
  1231. p->capability, p->mode);
  1232. else
  1233. dbgarg(cmd, "index=%d\n", p->index);
  1234. break;
  1235. }
  1236. case VIDIOC_G_AUDIO:
  1237. {
  1238. struct v4l2_audio *p = arg;
  1239. if (!ops->vidioc_g_audio)
  1240. break;
  1241. ret = ops->vidioc_g_audio(file, fh, p);
  1242. if (!ret)
  1243. dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
  1244. "mode=0x%x\n", p->index,
  1245. p->name, p->capability, p->mode);
  1246. else
  1247. dbgarg(cmd, "index=%d\n", p->index);
  1248. break;
  1249. }
  1250. case VIDIOC_S_AUDIO:
  1251. {
  1252. struct v4l2_audio *p = arg;
  1253. if (!ops->vidioc_s_audio)
  1254. break;
  1255. dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
  1256. "mode=0x%x\n", p->index, p->name,
  1257. p->capability, p->mode);
  1258. ret = ops->vidioc_s_audio(file, fh, p);
  1259. break;
  1260. }
  1261. case VIDIOC_ENUMAUDOUT:
  1262. {
  1263. struct v4l2_audioout *p = arg;
  1264. if (!ops->vidioc_enumaudout)
  1265. break;
  1266. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1267. ret = ops->vidioc_enumaudout(file, fh, p);
  1268. if (!ret)
  1269. dbgarg2("index=%d, name=%s, capability=%d, "
  1270. "mode=%d\n", p->index, p->name,
  1271. p->capability, p->mode);
  1272. break;
  1273. }
  1274. case VIDIOC_G_AUDOUT:
  1275. {
  1276. struct v4l2_audioout *p = arg;
  1277. if (!ops->vidioc_g_audout)
  1278. break;
  1279. ret = ops->vidioc_g_audout(file, fh, p);
  1280. if (!ret)
  1281. dbgarg2("index=%d, name=%s, capability=%d, "
  1282. "mode=%d\n", p->index, p->name,
  1283. p->capability, p->mode);
  1284. break;
  1285. }
  1286. case VIDIOC_S_AUDOUT:
  1287. {
  1288. struct v4l2_audioout *p = arg;
  1289. if (!ops->vidioc_s_audout)
  1290. break;
  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. if (!ops->vidioc_g_modulator)
  1301. break;
  1302. ret = ops->vidioc_g_modulator(file, fh, p);
  1303. if (!ret)
  1304. dbgarg(cmd, "index=%d, name=%s, "
  1305. "capability=%d, rangelow=%d,"
  1306. " rangehigh=%d, txsubchans=%d\n",
  1307. p->index, p->name, p->capability,
  1308. p->rangelow, p->rangehigh,
  1309. p->txsubchans);
  1310. break;
  1311. }
  1312. case VIDIOC_S_MODULATOR:
  1313. {
  1314. struct v4l2_modulator *p = arg;
  1315. if (!ops->vidioc_s_modulator)
  1316. break;
  1317. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1318. "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
  1319. p->index, p->name, p->capability, p->rangelow,
  1320. p->rangehigh, p->txsubchans);
  1321. ret = ops->vidioc_s_modulator(file, fh, p);
  1322. break;
  1323. }
  1324. case VIDIOC_G_CROP:
  1325. {
  1326. struct v4l2_crop *p = arg;
  1327. if (!ops->vidioc_g_crop)
  1328. break;
  1329. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1330. ret = ops->vidioc_g_crop(file, fh, p);
  1331. if (!ret)
  1332. dbgrect(vfd, "", &p->c);
  1333. break;
  1334. }
  1335. case VIDIOC_S_CROP:
  1336. {
  1337. struct v4l2_crop *p = arg;
  1338. if (!ops->vidioc_s_crop)
  1339. break;
  1340. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1341. dbgrect(vfd, "", &p->c);
  1342. ret = ops->vidioc_s_crop(file, fh, p);
  1343. break;
  1344. }
  1345. case VIDIOC_CROPCAP:
  1346. {
  1347. struct v4l2_cropcap *p = arg;
  1348. /*FIXME: Should also show v4l2_fract pixelaspect */
  1349. if (!ops->vidioc_cropcap)
  1350. break;
  1351. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1352. ret = ops->vidioc_cropcap(file, fh, p);
  1353. if (!ret) {
  1354. dbgrect(vfd, "bounds ", &p->bounds);
  1355. dbgrect(vfd, "defrect ", &p->defrect);
  1356. }
  1357. break;
  1358. }
  1359. case VIDIOC_G_JPEGCOMP:
  1360. {
  1361. struct v4l2_jpegcompression *p = arg;
  1362. if (!ops->vidioc_g_jpegcomp)
  1363. break;
  1364. ret = ops->vidioc_g_jpegcomp(file, fh, p);
  1365. if (!ret)
  1366. dbgarg(cmd, "quality=%d, APPn=%d, "
  1367. "APP_len=%d, COM_len=%d, "
  1368. "jpeg_markers=%d\n",
  1369. p->quality, p->APPn, p->APP_len,
  1370. p->COM_len, p->jpeg_markers);
  1371. break;
  1372. }
  1373. case VIDIOC_S_JPEGCOMP:
  1374. {
  1375. struct v4l2_jpegcompression *p = arg;
  1376. if (!ops->vidioc_g_jpegcomp)
  1377. break;
  1378. dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
  1379. "COM_len=%d, jpeg_markers=%d\n",
  1380. p->quality, p->APPn, p->APP_len,
  1381. p->COM_len, p->jpeg_markers);
  1382. ret = ops->vidioc_s_jpegcomp(file, fh, p);
  1383. break;
  1384. }
  1385. case VIDIOC_G_ENC_INDEX:
  1386. {
  1387. struct v4l2_enc_idx *p = arg;
  1388. if (!ops->vidioc_g_enc_index)
  1389. break;
  1390. ret = ops->vidioc_g_enc_index(file, fh, p);
  1391. if (!ret)
  1392. dbgarg(cmd, "entries=%d, entries_cap=%d\n",
  1393. p->entries, p->entries_cap);
  1394. break;
  1395. }
  1396. case VIDIOC_ENCODER_CMD:
  1397. {
  1398. struct v4l2_encoder_cmd *p = arg;
  1399. if (!ops->vidioc_encoder_cmd)
  1400. break;
  1401. ret = ops->vidioc_encoder_cmd(file, fh, p);
  1402. if (!ret)
  1403. dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
  1404. break;
  1405. }
  1406. case VIDIOC_TRY_ENCODER_CMD:
  1407. {
  1408. struct v4l2_encoder_cmd *p = arg;
  1409. if (!ops->vidioc_try_encoder_cmd)
  1410. break;
  1411. ret = ops->vidioc_try_encoder_cmd(file, fh, p);
  1412. if (!ret)
  1413. dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
  1414. break;
  1415. }
  1416. case VIDIOC_G_PARM:
  1417. {
  1418. struct v4l2_streamparm *p = arg;
  1419. if (ops->vidioc_g_parm) {
  1420. ret = check_fmt(ops, p->type);
  1421. if (ret)
  1422. break;
  1423. ret = ops->vidioc_g_parm(file, fh, p);
  1424. } else {
  1425. v4l2_std_id std = vfd->current_norm;
  1426. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1427. return -EINVAL;
  1428. ret = 0;
  1429. if (ops->vidioc_g_std)
  1430. ret = ops->vidioc_g_std(file, fh, &std);
  1431. else if (std == 0)
  1432. ret = -EINVAL;
  1433. if (ret == 0)
  1434. v4l2_video_std_frame_period(std,
  1435. &p->parm.capture.timeperframe);
  1436. }
  1437. dbgarg(cmd, "type=%d\n", p->type);
  1438. break;
  1439. }
  1440. case VIDIOC_S_PARM:
  1441. {
  1442. struct v4l2_streamparm *p = arg;
  1443. if (!ops->vidioc_s_parm)
  1444. break;
  1445. ret = check_fmt(ops, p->type);
  1446. if (ret)
  1447. break;
  1448. dbgarg(cmd, "type=%d\n", p->type);
  1449. ret = ops->vidioc_s_parm(file, fh, p);
  1450. break;
  1451. }
  1452. case VIDIOC_G_TUNER:
  1453. {
  1454. struct v4l2_tuner *p = arg;
  1455. if (!ops->vidioc_g_tuner)
  1456. break;
  1457. ret = ops->vidioc_g_tuner(file, fh, p);
  1458. if (!ret)
  1459. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1460. "capability=0x%x, rangelow=%d, "
  1461. "rangehigh=%d, signal=%d, afc=%d, "
  1462. "rxsubchans=0x%x, audmode=%d\n",
  1463. p->index, p->name, p->type,
  1464. p->capability, p->rangelow,
  1465. p->rangehigh, p->signal, p->afc,
  1466. p->rxsubchans, p->audmode);
  1467. break;
  1468. }
  1469. case VIDIOC_S_TUNER:
  1470. {
  1471. struct v4l2_tuner *p = arg;
  1472. if (!ops->vidioc_s_tuner)
  1473. break;
  1474. dbgarg(cmd, "index=%d, name=%s, type=%d, "
  1475. "capability=0x%x, rangelow=%d, "
  1476. "rangehigh=%d, signal=%d, afc=%d, "
  1477. "rxsubchans=0x%x, audmode=%d\n",
  1478. p->index, p->name, p->type,
  1479. p->capability, p->rangelow,
  1480. p->rangehigh, p->signal, p->afc,
  1481. p->rxsubchans, p->audmode);
  1482. ret = ops->vidioc_s_tuner(file, fh, p);
  1483. break;
  1484. }
  1485. case VIDIOC_G_FREQUENCY:
  1486. {
  1487. struct v4l2_frequency *p = arg;
  1488. if (!ops->vidioc_g_frequency)
  1489. break;
  1490. ret = ops->vidioc_g_frequency(file, fh, p);
  1491. if (!ret)
  1492. dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
  1493. p->tuner, p->type, p->frequency);
  1494. break;
  1495. }
  1496. case VIDIOC_S_FREQUENCY:
  1497. {
  1498. struct v4l2_frequency *p = arg;
  1499. if (!ops->vidioc_s_frequency)
  1500. break;
  1501. dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
  1502. p->tuner, p->type, p->frequency);
  1503. ret = ops->vidioc_s_frequency(file, fh, p);
  1504. break;
  1505. }
  1506. case VIDIOC_G_SLICED_VBI_CAP:
  1507. {
  1508. struct v4l2_sliced_vbi_cap *p = arg;
  1509. if (!ops->vidioc_g_sliced_vbi_cap)
  1510. break;
  1511. /* Clear up to type, everything after type is zerod already */
  1512. memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
  1513. dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
  1514. ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
  1515. if (!ret)
  1516. dbgarg2("service_set=%d\n", p->service_set);
  1517. break;
  1518. }
  1519. case VIDIOC_LOG_STATUS:
  1520. {
  1521. if (!ops->vidioc_log_status)
  1522. break;
  1523. ret = ops->vidioc_log_status(file, fh);
  1524. break;
  1525. }
  1526. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1527. case VIDIOC_DBG_G_REGISTER:
  1528. {
  1529. struct v4l2_dbg_register *p = arg;
  1530. if (!capable(CAP_SYS_ADMIN))
  1531. ret = -EPERM;
  1532. else if (ops->vidioc_g_register)
  1533. ret = ops->vidioc_g_register(file, fh, p);
  1534. break;
  1535. }
  1536. case VIDIOC_DBG_S_REGISTER:
  1537. {
  1538. struct v4l2_dbg_register *p = arg;
  1539. if (!capable(CAP_SYS_ADMIN))
  1540. ret = -EPERM;
  1541. else if (ops->vidioc_s_register)
  1542. ret = ops->vidioc_s_register(file, fh, p);
  1543. break;
  1544. }
  1545. #endif
  1546. case VIDIOC_DBG_G_CHIP_IDENT:
  1547. {
  1548. struct v4l2_dbg_chip_ident *p = arg;
  1549. if (!ops->vidioc_g_chip_ident)
  1550. break;
  1551. p->ident = V4L2_IDENT_NONE;
  1552. p->revision = 0;
  1553. ret = ops->vidioc_g_chip_ident(file, fh, p);
  1554. if (!ret)
  1555. dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
  1556. break;
  1557. }
  1558. case VIDIOC_S_HW_FREQ_SEEK:
  1559. {
  1560. struct v4l2_hw_freq_seek *p = arg;
  1561. if (!ops->vidioc_s_hw_freq_seek)
  1562. break;
  1563. dbgarg(cmd,
  1564. "tuner=%d, type=%d, seek_upward=%d, wrap_around=%d\n",
  1565. p->tuner, p->type, p->seek_upward, p->wrap_around);
  1566. ret = ops->vidioc_s_hw_freq_seek(file, fh, p);
  1567. break;
  1568. }
  1569. case VIDIOC_ENUM_FRAMESIZES:
  1570. {
  1571. struct v4l2_frmsizeenum *p = arg;
  1572. if (!ops->vidioc_enum_framesizes)
  1573. break;
  1574. ret = ops->vidioc_enum_framesizes(file, fh, p);
  1575. dbgarg(cmd,
  1576. "index=%d, pixelformat=%c%c%c%c, type=%d ",
  1577. p->index,
  1578. (p->pixel_format & 0xff),
  1579. (p->pixel_format >> 8) & 0xff,
  1580. (p->pixel_format >> 16) & 0xff,
  1581. (p->pixel_format >> 24) & 0xff,
  1582. p->type);
  1583. switch (p->type) {
  1584. case V4L2_FRMSIZE_TYPE_DISCRETE:
  1585. dbgarg3("width = %d, height=%d\n",
  1586. p->discrete.width, p->discrete.height);
  1587. break;
  1588. case V4L2_FRMSIZE_TYPE_STEPWISE:
  1589. dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
  1590. p->stepwise.min_width, p->stepwise.min_height,
  1591. p->stepwise.step_width, p->stepwise.step_height,
  1592. p->stepwise.max_width, p->stepwise.max_height);
  1593. break;
  1594. case V4L2_FRMSIZE_TYPE_CONTINUOUS:
  1595. dbgarg3("continuous\n");
  1596. break;
  1597. default:
  1598. dbgarg3("- Unknown type!\n");
  1599. }
  1600. break;
  1601. }
  1602. case VIDIOC_ENUM_FRAMEINTERVALS:
  1603. {
  1604. struct v4l2_frmivalenum *p = arg;
  1605. if (!ops->vidioc_enum_frameintervals)
  1606. break;
  1607. ret = ops->vidioc_enum_frameintervals(file, fh, p);
  1608. dbgarg(cmd,
  1609. "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
  1610. p->index, p->pixel_format,
  1611. p->width, p->height, p->type);
  1612. switch (p->type) {
  1613. case V4L2_FRMIVAL_TYPE_DISCRETE:
  1614. dbgarg2("fps=%d/%d\n",
  1615. p->discrete.numerator,
  1616. p->discrete.denominator);
  1617. break;
  1618. case V4L2_FRMIVAL_TYPE_STEPWISE:
  1619. dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
  1620. p->stepwise.min.numerator,
  1621. p->stepwise.min.denominator,
  1622. p->stepwise.max.numerator,
  1623. p->stepwise.max.denominator,
  1624. p->stepwise.step.numerator,
  1625. p->stepwise.step.denominator);
  1626. break;
  1627. case V4L2_FRMIVAL_TYPE_CONTINUOUS:
  1628. dbgarg2("continuous\n");
  1629. break;
  1630. default:
  1631. dbgarg2("- Unknown type!\n");
  1632. }
  1633. break;
  1634. }
  1635. default:
  1636. {
  1637. if (!ops->vidioc_default)
  1638. break;
  1639. ret = ops->vidioc_default(file, fh, cmd, arg);
  1640. break;
  1641. }
  1642. } /* switch */
  1643. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
  1644. if (ret < 0) {
  1645. v4l_print_ioctl(vfd->name, cmd);
  1646. printk(KERN_CONT " error %ld\n", ret);
  1647. }
  1648. }
  1649. return ret;
  1650. }
  1651. /* In some cases, only a few fields are used as input, i.e. when the app sets
  1652. * "index" and then the driver fills in the rest of the structure for the thing
  1653. * with that index. We only need to copy up the first non-input field. */
  1654. static unsigned long cmd_input_size(unsigned int cmd)
  1655. {
  1656. /* Size of structure up to and including 'field' */
  1657. #define CMDINSIZE(cmd, type, field) \
  1658. case VIDIOC_##cmd: \
  1659. return offsetof(struct v4l2_##type, field) + \
  1660. sizeof(((struct v4l2_##type *)0)->field);
  1661. switch (cmd) {
  1662. CMDINSIZE(ENUM_FMT, fmtdesc, type);
  1663. CMDINSIZE(G_FMT, format, type);
  1664. CMDINSIZE(QUERYBUF, buffer, type);
  1665. CMDINSIZE(G_PARM, streamparm, type);
  1666. CMDINSIZE(ENUMSTD, standard, index);
  1667. CMDINSIZE(ENUMINPUT, input, index);
  1668. CMDINSIZE(G_CTRL, control, id);
  1669. CMDINSIZE(G_TUNER, tuner, index);
  1670. CMDINSIZE(QUERYCTRL, queryctrl, id);
  1671. CMDINSIZE(QUERYMENU, querymenu, index);
  1672. CMDINSIZE(ENUMOUTPUT, output, index);
  1673. CMDINSIZE(G_MODULATOR, modulator, index);
  1674. CMDINSIZE(G_FREQUENCY, frequency, tuner);
  1675. CMDINSIZE(CROPCAP, cropcap, type);
  1676. CMDINSIZE(G_CROP, crop, type);
  1677. CMDINSIZE(ENUMAUDIO, audio, index);
  1678. CMDINSIZE(ENUMAUDOUT, audioout, index);
  1679. CMDINSIZE(ENCODER_CMD, encoder_cmd, flags);
  1680. CMDINSIZE(TRY_ENCODER_CMD, encoder_cmd, flags);
  1681. CMDINSIZE(G_SLICED_VBI_CAP, sliced_vbi_cap, type);
  1682. CMDINSIZE(ENUM_FRAMESIZES, frmsizeenum, pixel_format);
  1683. CMDINSIZE(ENUM_FRAMEINTERVALS, frmivalenum, height);
  1684. default:
  1685. return _IOC_SIZE(cmd);
  1686. }
  1687. }
  1688. long video_ioctl2(struct file *file,
  1689. unsigned int cmd, unsigned long arg)
  1690. {
  1691. char sbuf[128];
  1692. void *mbuf = NULL;
  1693. void *parg = NULL;
  1694. long err = -EINVAL;
  1695. int is_ext_ctrl;
  1696. size_t ctrls_size = 0;
  1697. void __user *user_ptr = NULL;
  1698. #ifdef __OLD_VIDIOC_
  1699. cmd = video_fix_command(cmd);
  1700. #endif
  1701. is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
  1702. cmd == VIDIOC_TRY_EXT_CTRLS);
  1703. /* Copy arguments into temp kernel buffer */
  1704. if (_IOC_DIR(cmd) != _IOC_NONE) {
  1705. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  1706. parg = sbuf;
  1707. } else {
  1708. /* too big to allocate from stack */
  1709. mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  1710. if (NULL == mbuf)
  1711. return -ENOMEM;
  1712. parg = mbuf;
  1713. }
  1714. err = -EFAULT;
  1715. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  1716. unsigned long n = cmd_input_size(cmd);
  1717. if (copy_from_user(parg, (void __user *)arg, n))
  1718. goto out;
  1719. /* zero out anything we don't copy from userspace */
  1720. if (n < _IOC_SIZE(cmd))
  1721. memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
  1722. } else {
  1723. /* read-only ioctl */
  1724. memset(parg, 0, _IOC_SIZE(cmd));
  1725. }
  1726. }
  1727. if (is_ext_ctrl) {
  1728. struct v4l2_ext_controls *p = parg;
  1729. /* In case of an error, tell the caller that it wasn't
  1730. a specific control that caused it. */
  1731. p->error_idx = p->count;
  1732. user_ptr = (void __user *)p->controls;
  1733. if (p->count) {
  1734. ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
  1735. /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
  1736. mbuf = kmalloc(ctrls_size, GFP_KERNEL);
  1737. err = -ENOMEM;
  1738. if (NULL == mbuf)
  1739. goto out_ext_ctrl;
  1740. err = -EFAULT;
  1741. if (copy_from_user(mbuf, user_ptr, ctrls_size))
  1742. goto out_ext_ctrl;
  1743. p->controls = mbuf;
  1744. }
  1745. }
  1746. /* Handles IOCTL */
  1747. err = __video_do_ioctl(file, cmd, parg);
  1748. if (err == -ENOIOCTLCMD)
  1749. err = -EINVAL;
  1750. if (is_ext_ctrl) {
  1751. struct v4l2_ext_controls *p = parg;
  1752. p->controls = (void *)user_ptr;
  1753. if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
  1754. err = -EFAULT;
  1755. goto out_ext_ctrl;
  1756. }
  1757. if (err < 0)
  1758. goto out;
  1759. out_ext_ctrl:
  1760. /* Copy results into user buffer */
  1761. switch (_IOC_DIR(cmd)) {
  1762. case _IOC_READ:
  1763. case (_IOC_WRITE | _IOC_READ):
  1764. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  1765. err = -EFAULT;
  1766. break;
  1767. }
  1768. out:
  1769. kfree(mbuf);
  1770. return err;
  1771. }
  1772. EXPORT_SYMBOL(video_ioctl2);