v4l2-ioctl.c 60 KB

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