v4l2-ioctl.c 53 KB

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