v4l2-ioctl.c 73 KB

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