v4l2-ioctl.c 62 KB

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