v4l2-ioctl.c 50 KB

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