v4l2-ioctl.c 63 KB

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