videodev.c 58 KB

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