v4l2-ioctl.c 59 KB

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