v4l2-ioctl.c 59 KB

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