v4l2-ioctl.c 64 KB

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