v4l2-ioctl.c 65 KB

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