v4l2-ioctl.c 59 KB

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