v4l2-ioctl.c 74 KB

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