v4l2-ioctl.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981
  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@kernel.org> (version 2)
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/version.h>
  20. #include <linux/videodev2.h>
  21. #include <media/v4l2-common.h>
  22. #include <media/v4l2-ioctl.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/v4l2-fh.h>
  25. #include <media/v4l2-event.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/videobuf2-v4l2.h>
  28. #include <media/v4l2-mc.h>
  29. #include <trace/events/v4l2.h>
  30. /* Zero out the end of the struct pointed to by p. Everything after, but
  31. * not including, the specified field is cleared. */
  32. #define CLEAR_AFTER_FIELD(p, field) \
  33. memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
  34. 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
  35. #define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
  36. struct std_descr {
  37. v4l2_std_id std;
  38. const char *descr;
  39. };
  40. static const struct std_descr standards[] = {
  41. { V4L2_STD_NTSC, "NTSC" },
  42. { V4L2_STD_NTSC_M, "NTSC-M" },
  43. { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
  44. { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
  45. { V4L2_STD_NTSC_443, "NTSC-443" },
  46. { V4L2_STD_PAL, "PAL" },
  47. { V4L2_STD_PAL_BG, "PAL-BG" },
  48. { V4L2_STD_PAL_B, "PAL-B" },
  49. { V4L2_STD_PAL_B1, "PAL-B1" },
  50. { V4L2_STD_PAL_G, "PAL-G" },
  51. { V4L2_STD_PAL_H, "PAL-H" },
  52. { V4L2_STD_PAL_I, "PAL-I" },
  53. { V4L2_STD_PAL_DK, "PAL-DK" },
  54. { V4L2_STD_PAL_D, "PAL-D" },
  55. { V4L2_STD_PAL_D1, "PAL-D1" },
  56. { V4L2_STD_PAL_K, "PAL-K" },
  57. { V4L2_STD_PAL_M, "PAL-M" },
  58. { V4L2_STD_PAL_N, "PAL-N" },
  59. { V4L2_STD_PAL_Nc, "PAL-Nc" },
  60. { V4L2_STD_PAL_60, "PAL-60" },
  61. { V4L2_STD_SECAM, "SECAM" },
  62. { V4L2_STD_SECAM_B, "SECAM-B" },
  63. { V4L2_STD_SECAM_G, "SECAM-G" },
  64. { V4L2_STD_SECAM_H, "SECAM-H" },
  65. { V4L2_STD_SECAM_DK, "SECAM-DK" },
  66. { V4L2_STD_SECAM_D, "SECAM-D" },
  67. { V4L2_STD_SECAM_K, "SECAM-K" },
  68. { V4L2_STD_SECAM_K1, "SECAM-K1" },
  69. { V4L2_STD_SECAM_L, "SECAM-L" },
  70. { V4L2_STD_SECAM_LC, "SECAM-Lc" },
  71. { 0, "Unknown" }
  72. };
  73. /* video4linux standard ID conversion to standard name
  74. */
  75. const char *v4l2_norm_to_name(v4l2_std_id id)
  76. {
  77. u32 myid = id;
  78. int i;
  79. /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
  80. 64 bit comparations. So, on that architecture, with some gcc
  81. variants, compilation fails. Currently, the max value is 30bit wide.
  82. */
  83. BUG_ON(myid != id);
  84. for (i = 0; standards[i].std; i++)
  85. if (myid == standards[i].std)
  86. break;
  87. return standards[i].descr;
  88. }
  89. EXPORT_SYMBOL(v4l2_norm_to_name);
  90. /* Returns frame period for the given standard */
  91. void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
  92. {
  93. if (id & V4L2_STD_525_60) {
  94. frameperiod->numerator = 1001;
  95. frameperiod->denominator = 30000;
  96. } else {
  97. frameperiod->numerator = 1;
  98. frameperiod->denominator = 25;
  99. }
  100. }
  101. EXPORT_SYMBOL(v4l2_video_std_frame_period);
  102. /* Fill in the fields of a v4l2_standard structure according to the
  103. 'id' and 'transmission' parameters. Returns negative on error. */
  104. int v4l2_video_std_construct(struct v4l2_standard *vs,
  105. int id, const char *name)
  106. {
  107. vs->id = id;
  108. v4l2_video_std_frame_period(id, &vs->frameperiod);
  109. vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
  110. strlcpy(vs->name, name, sizeof(vs->name));
  111. return 0;
  112. }
  113. EXPORT_SYMBOL(v4l2_video_std_construct);
  114. /* ----------------------------------------------------------------- */
  115. /* some arrays for pretty-printing debug messages of enum types */
  116. const char *v4l2_field_names[] = {
  117. [V4L2_FIELD_ANY] = "any",
  118. [V4L2_FIELD_NONE] = "none",
  119. [V4L2_FIELD_TOP] = "top",
  120. [V4L2_FIELD_BOTTOM] = "bottom",
  121. [V4L2_FIELD_INTERLACED] = "interlaced",
  122. [V4L2_FIELD_SEQ_TB] = "seq-tb",
  123. [V4L2_FIELD_SEQ_BT] = "seq-bt",
  124. [V4L2_FIELD_ALTERNATE] = "alternate",
  125. [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
  126. [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
  127. };
  128. EXPORT_SYMBOL(v4l2_field_names);
  129. const char *v4l2_type_names[] = {
  130. [0] = "0",
  131. [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
  132. [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
  133. [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
  134. [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
  135. [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
  136. [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
  137. [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
  138. [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
  139. [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
  140. [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
  141. [V4L2_BUF_TYPE_SDR_CAPTURE] = "sdr-cap",
  142. [V4L2_BUF_TYPE_SDR_OUTPUT] = "sdr-out",
  143. [V4L2_BUF_TYPE_META_CAPTURE] = "meta-cap",
  144. };
  145. EXPORT_SYMBOL(v4l2_type_names);
  146. static const char *v4l2_memory_names[] = {
  147. [V4L2_MEMORY_MMAP] = "mmap",
  148. [V4L2_MEMORY_USERPTR] = "userptr",
  149. [V4L2_MEMORY_OVERLAY] = "overlay",
  150. [V4L2_MEMORY_DMABUF] = "dmabuf",
  151. };
  152. #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
  153. /* ------------------------------------------------------------------ */
  154. /* debug help functions */
  155. static void v4l_print_querycap(const void *arg, bool write_only)
  156. {
  157. const struct v4l2_capability *p = arg;
  158. pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, capabilities=0x%08x, device_caps=0x%08x\n",
  159. (int)sizeof(p->driver), p->driver,
  160. (int)sizeof(p->card), p->card,
  161. (int)sizeof(p->bus_info), p->bus_info,
  162. p->version, p->capabilities, p->device_caps);
  163. }
  164. static void v4l_print_enuminput(const void *arg, bool write_only)
  165. {
  166. const struct v4l2_input *p = arg;
  167. pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
  168. p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
  169. p->tuner, (unsigned long long)p->std, p->status,
  170. p->capabilities);
  171. }
  172. static void v4l_print_enumoutput(const void *arg, bool write_only)
  173. {
  174. const struct v4l2_output *p = arg;
  175. pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
  176. p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
  177. p->modulator, (unsigned long long)p->std, p->capabilities);
  178. }
  179. static void v4l_print_audio(const void *arg, bool write_only)
  180. {
  181. const struct v4l2_audio *p = arg;
  182. if (write_only)
  183. pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
  184. else
  185. pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
  186. p->index, (int)sizeof(p->name), p->name,
  187. p->capability, p->mode);
  188. }
  189. static void v4l_print_audioout(const void *arg, bool write_only)
  190. {
  191. const struct v4l2_audioout *p = arg;
  192. if (write_only)
  193. pr_cont("index=%u\n", p->index);
  194. else
  195. pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
  196. p->index, (int)sizeof(p->name), p->name,
  197. p->capability, p->mode);
  198. }
  199. static void v4l_print_fmtdesc(const void *arg, bool write_only)
  200. {
  201. const struct v4l2_fmtdesc *p = arg;
  202. pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
  203. p->index, prt_names(p->type, v4l2_type_names),
  204. p->flags, (p->pixelformat & 0xff),
  205. (p->pixelformat >> 8) & 0xff,
  206. (p->pixelformat >> 16) & 0xff,
  207. (p->pixelformat >> 24) & 0xff,
  208. (int)sizeof(p->description), p->description);
  209. }
  210. static void v4l_print_format(const void *arg, bool write_only)
  211. {
  212. const struct v4l2_format *p = arg;
  213. const struct v4l2_pix_format *pix;
  214. const struct v4l2_pix_format_mplane *mp;
  215. const struct v4l2_vbi_format *vbi;
  216. const struct v4l2_sliced_vbi_format *sliced;
  217. const struct v4l2_window *win;
  218. const struct v4l2_sdr_format *sdr;
  219. const struct v4l2_meta_format *meta;
  220. unsigned i;
  221. pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
  222. switch (p->type) {
  223. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  224. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  225. pix = &p->fmt.pix;
  226. pr_cont(", width=%u, height=%u, pixelformat=%c%c%c%c, field=%s, bytesperline=%u, sizeimage=%u, colorspace=%d, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
  227. pix->width, pix->height,
  228. (pix->pixelformat & 0xff),
  229. (pix->pixelformat >> 8) & 0xff,
  230. (pix->pixelformat >> 16) & 0xff,
  231. (pix->pixelformat >> 24) & 0xff,
  232. prt_names(pix->field, v4l2_field_names),
  233. pix->bytesperline, pix->sizeimage,
  234. pix->colorspace, pix->flags, pix->ycbcr_enc,
  235. pix->quantization, pix->xfer_func);
  236. break;
  237. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  238. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  239. mp = &p->fmt.pix_mp;
  240. pr_cont(", width=%u, height=%u, format=%c%c%c%c, field=%s, colorspace=%d, num_planes=%u, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
  241. mp->width, mp->height,
  242. (mp->pixelformat & 0xff),
  243. (mp->pixelformat >> 8) & 0xff,
  244. (mp->pixelformat >> 16) & 0xff,
  245. (mp->pixelformat >> 24) & 0xff,
  246. prt_names(mp->field, v4l2_field_names),
  247. mp->colorspace, mp->num_planes, mp->flags,
  248. mp->ycbcr_enc, mp->quantization, mp->xfer_func);
  249. for (i = 0; i < mp->num_planes; i++)
  250. printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
  251. mp->plane_fmt[i].bytesperline,
  252. mp->plane_fmt[i].sizeimage);
  253. break;
  254. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  255. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  256. win = &p->fmt.win;
  257. /* Note: we can't print the clip list here since the clips
  258. * pointer is a userspace pointer, not a kernelspace
  259. * pointer. */
  260. pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, chromakey=0x%08x, clipcount=%u, clips=%p, bitmap=%p, global_alpha=0x%02x\n",
  261. win->w.width, win->w.height, win->w.left, win->w.top,
  262. prt_names(win->field, v4l2_field_names),
  263. win->chromakey, win->clipcount, win->clips,
  264. win->bitmap, win->global_alpha);
  265. break;
  266. case V4L2_BUF_TYPE_VBI_CAPTURE:
  267. case V4L2_BUF_TYPE_VBI_OUTPUT:
  268. vbi = &p->fmt.vbi;
  269. pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
  270. vbi->sampling_rate, vbi->offset,
  271. vbi->samples_per_line,
  272. (vbi->sample_format & 0xff),
  273. (vbi->sample_format >> 8) & 0xff,
  274. (vbi->sample_format >> 16) & 0xff,
  275. (vbi->sample_format >> 24) & 0xff,
  276. vbi->start[0], vbi->start[1],
  277. vbi->count[0], vbi->count[1]);
  278. break;
  279. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  280. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  281. sliced = &p->fmt.sliced;
  282. pr_cont(", service_set=0x%08x, io_size=%d\n",
  283. sliced->service_set, sliced->io_size);
  284. for (i = 0; i < 24; i++)
  285. printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
  286. sliced->service_lines[0][i],
  287. sliced->service_lines[1][i]);
  288. break;
  289. case V4L2_BUF_TYPE_SDR_CAPTURE:
  290. case V4L2_BUF_TYPE_SDR_OUTPUT:
  291. sdr = &p->fmt.sdr;
  292. pr_cont(", pixelformat=%c%c%c%c\n",
  293. (sdr->pixelformat >> 0) & 0xff,
  294. (sdr->pixelformat >> 8) & 0xff,
  295. (sdr->pixelformat >> 16) & 0xff,
  296. (sdr->pixelformat >> 24) & 0xff);
  297. break;
  298. case V4L2_BUF_TYPE_META_CAPTURE:
  299. meta = &p->fmt.meta;
  300. pr_cont(", dataformat=%c%c%c%c, buffersize=%u\n",
  301. (meta->dataformat >> 0) & 0xff,
  302. (meta->dataformat >> 8) & 0xff,
  303. (meta->dataformat >> 16) & 0xff,
  304. (meta->dataformat >> 24) & 0xff,
  305. meta->buffersize);
  306. break;
  307. }
  308. }
  309. static void v4l_print_framebuffer(const void *arg, bool write_only)
  310. {
  311. const struct v4l2_framebuffer *p = arg;
  312. pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, height=%u, pixelformat=%c%c%c%c, bytesperline=%u, sizeimage=%u, colorspace=%d\n",
  313. p->capability, p->flags, p->base,
  314. p->fmt.width, p->fmt.height,
  315. (p->fmt.pixelformat & 0xff),
  316. (p->fmt.pixelformat >> 8) & 0xff,
  317. (p->fmt.pixelformat >> 16) & 0xff,
  318. (p->fmt.pixelformat >> 24) & 0xff,
  319. p->fmt.bytesperline, p->fmt.sizeimage,
  320. p->fmt.colorspace);
  321. }
  322. static void v4l_print_buftype(const void *arg, bool write_only)
  323. {
  324. pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
  325. }
  326. static void v4l_print_modulator(const void *arg, bool write_only)
  327. {
  328. const struct v4l2_modulator *p = arg;
  329. if (write_only)
  330. pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
  331. else
  332. pr_cont("index=%u, name=%.*s, capability=0x%x, rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
  333. p->index, (int)sizeof(p->name), p->name, p->capability,
  334. p->rangelow, p->rangehigh, p->txsubchans);
  335. }
  336. static void v4l_print_tuner(const void *arg, bool write_only)
  337. {
  338. const struct v4l2_tuner *p = arg;
  339. if (write_only)
  340. pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
  341. else
  342. pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, rangelow=%u, rangehigh=%u, signal=%u, afc=%d, rxsubchans=0x%x, audmode=%u\n",
  343. p->index, (int)sizeof(p->name), p->name, p->type,
  344. p->capability, p->rangelow,
  345. p->rangehigh, p->signal, p->afc,
  346. p->rxsubchans, p->audmode);
  347. }
  348. static void v4l_print_frequency(const void *arg, bool write_only)
  349. {
  350. const struct v4l2_frequency *p = arg;
  351. pr_cont("tuner=%u, type=%u, frequency=%u\n",
  352. p->tuner, p->type, p->frequency);
  353. }
  354. static void v4l_print_standard(const void *arg, bool write_only)
  355. {
  356. const struct v4l2_standard *p = arg;
  357. pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, framelines=%u\n",
  358. p->index,
  359. (unsigned long long)p->id, (int)sizeof(p->name), p->name,
  360. p->frameperiod.numerator,
  361. p->frameperiod.denominator,
  362. p->framelines);
  363. }
  364. static void v4l_print_std(const void *arg, bool write_only)
  365. {
  366. pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
  367. }
  368. static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
  369. {
  370. const struct v4l2_hw_freq_seek *p = arg;
  371. pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, rangelow=%u, rangehigh=%u\n",
  372. p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
  373. p->rangelow, p->rangehigh);
  374. }
  375. static void v4l_print_requestbuffers(const void *arg, bool write_only)
  376. {
  377. const struct v4l2_requestbuffers *p = arg;
  378. pr_cont("count=%d, type=%s, memory=%s\n",
  379. p->count,
  380. prt_names(p->type, v4l2_type_names),
  381. prt_names(p->memory, v4l2_memory_names));
  382. }
  383. static void v4l_print_buffer(const void *arg, bool write_only)
  384. {
  385. const struct v4l2_buffer *p = arg;
  386. const struct v4l2_timecode *tc = &p->timecode;
  387. const struct v4l2_plane *plane;
  388. int i;
  389. pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, flags=0x%08x, field=%s, sequence=%d, memory=%s",
  390. p->timestamp.tv_sec / 3600,
  391. (int)(p->timestamp.tv_sec / 60) % 60,
  392. (int)(p->timestamp.tv_sec % 60),
  393. (long)p->timestamp.tv_usec,
  394. p->index,
  395. prt_names(p->type, v4l2_type_names),
  396. p->flags, prt_names(p->field, v4l2_field_names),
  397. p->sequence, prt_names(p->memory, v4l2_memory_names));
  398. if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
  399. pr_cont("\n");
  400. for (i = 0; i < p->length; ++i) {
  401. plane = &p->m.planes[i];
  402. printk(KERN_DEBUG
  403. "plane %d: bytesused=%d, data_offset=0x%08x, offset/userptr=0x%lx, length=%d\n",
  404. i, plane->bytesused, plane->data_offset,
  405. plane->m.userptr, plane->length);
  406. }
  407. } else {
  408. pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
  409. p->bytesused, p->m.userptr, p->length);
  410. }
  411. printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, flags=0x%08x, frames=%d, userbits=0x%08x\n",
  412. tc->hours, tc->minutes, tc->seconds,
  413. tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
  414. }
  415. static void v4l_print_exportbuffer(const void *arg, bool write_only)
  416. {
  417. const struct v4l2_exportbuffer *p = arg;
  418. pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
  419. p->fd, prt_names(p->type, v4l2_type_names),
  420. p->index, p->plane, p->flags);
  421. }
  422. static void v4l_print_create_buffers(const void *arg, bool write_only)
  423. {
  424. const struct v4l2_create_buffers *p = arg;
  425. pr_cont("index=%d, count=%d, memory=%s, ",
  426. p->index, p->count,
  427. prt_names(p->memory, v4l2_memory_names));
  428. v4l_print_format(&p->format, write_only);
  429. }
  430. static void v4l_print_streamparm(const void *arg, bool write_only)
  431. {
  432. const struct v4l2_streamparm *p = arg;
  433. pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
  434. if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  435. p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  436. const struct v4l2_captureparm *c = &p->parm.capture;
  437. pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, extendedmode=%d, readbuffers=%d\n",
  438. c->capability, c->capturemode,
  439. c->timeperframe.numerator, c->timeperframe.denominator,
  440. c->extendedmode, c->readbuffers);
  441. } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  442. p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  443. const struct v4l2_outputparm *c = &p->parm.output;
  444. pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, extendedmode=%d, writebuffers=%d\n",
  445. c->capability, c->outputmode,
  446. c->timeperframe.numerator, c->timeperframe.denominator,
  447. c->extendedmode, c->writebuffers);
  448. } else {
  449. pr_cont("\n");
  450. }
  451. }
  452. static void v4l_print_queryctrl(const void *arg, bool write_only)
  453. {
  454. const struct v4l2_queryctrl *p = arg;
  455. pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, step=%d, default=%d, flags=0x%08x\n",
  456. p->id, p->type, (int)sizeof(p->name), p->name,
  457. p->minimum, p->maximum,
  458. p->step, p->default_value, p->flags);
  459. }
  460. static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
  461. {
  462. const struct v4l2_query_ext_ctrl *p = arg;
  463. pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, nr_of_dims=%u, dims=%u,%u,%u,%u\n",
  464. p->id, p->type, (int)sizeof(p->name), p->name,
  465. p->minimum, p->maximum,
  466. p->step, p->default_value, p->flags,
  467. p->elem_size, p->elems, p->nr_of_dims,
  468. p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
  469. }
  470. static void v4l_print_querymenu(const void *arg, bool write_only)
  471. {
  472. const struct v4l2_querymenu *p = arg;
  473. pr_cont("id=0x%x, index=%d\n", p->id, p->index);
  474. }
  475. static void v4l_print_control(const void *arg, bool write_only)
  476. {
  477. const struct v4l2_control *p = arg;
  478. pr_cont("id=0x%x, value=%d\n", p->id, p->value);
  479. }
  480. static void v4l_print_ext_controls(const void *arg, bool write_only)
  481. {
  482. const struct v4l2_ext_controls *p = arg;
  483. int i;
  484. pr_cont("which=0x%x, count=%d, error_idx=%d",
  485. p->which, p->count, p->error_idx);
  486. for (i = 0; i < p->count; i++) {
  487. if (!p->controls[i].size)
  488. pr_cont(", id/val=0x%x/0x%x",
  489. p->controls[i].id, p->controls[i].value);
  490. else
  491. pr_cont(", id/size=0x%x/%u",
  492. p->controls[i].id, p->controls[i].size);
  493. }
  494. pr_cont("\n");
  495. }
  496. static void v4l_print_cropcap(const void *arg, bool write_only)
  497. {
  498. const struct v4l2_cropcap *p = arg;
  499. pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, defrect wxh=%dx%d, x,y=%d,%d, pixelaspect %d/%d\n",
  500. prt_names(p->type, v4l2_type_names),
  501. p->bounds.width, p->bounds.height,
  502. p->bounds.left, p->bounds.top,
  503. p->defrect.width, p->defrect.height,
  504. p->defrect.left, p->defrect.top,
  505. p->pixelaspect.numerator, p->pixelaspect.denominator);
  506. }
  507. static void v4l_print_crop(const void *arg, bool write_only)
  508. {
  509. const struct v4l2_crop *p = arg;
  510. pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
  511. prt_names(p->type, v4l2_type_names),
  512. p->c.width, p->c.height,
  513. p->c.left, p->c.top);
  514. }
  515. static void v4l_print_selection(const void *arg, bool write_only)
  516. {
  517. const struct v4l2_selection *p = arg;
  518. pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
  519. prt_names(p->type, v4l2_type_names),
  520. p->target, p->flags,
  521. p->r.width, p->r.height, p->r.left, p->r.top);
  522. }
  523. static void v4l_print_jpegcompression(const void *arg, bool write_only)
  524. {
  525. const struct v4l2_jpegcompression *p = arg;
  526. pr_cont("quality=%d, APPn=%d, APP_len=%d, COM_len=%d, jpeg_markers=0x%x\n",
  527. p->quality, p->APPn, p->APP_len,
  528. p->COM_len, p->jpeg_markers);
  529. }
  530. static void v4l_print_enc_idx(const void *arg, bool write_only)
  531. {
  532. const struct v4l2_enc_idx *p = arg;
  533. pr_cont("entries=%d, entries_cap=%d\n",
  534. p->entries, p->entries_cap);
  535. }
  536. static void v4l_print_encoder_cmd(const void *arg, bool write_only)
  537. {
  538. const struct v4l2_encoder_cmd *p = arg;
  539. pr_cont("cmd=%d, flags=0x%x\n",
  540. p->cmd, p->flags);
  541. }
  542. static void v4l_print_decoder_cmd(const void *arg, bool write_only)
  543. {
  544. const struct v4l2_decoder_cmd *p = arg;
  545. pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
  546. if (p->cmd == V4L2_DEC_CMD_START)
  547. pr_info("speed=%d, format=%u\n",
  548. p->start.speed, p->start.format);
  549. else if (p->cmd == V4L2_DEC_CMD_STOP)
  550. pr_info("pts=%llu\n", p->stop.pts);
  551. }
  552. static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
  553. {
  554. const struct v4l2_dbg_chip_info *p = arg;
  555. pr_cont("type=%u, ", p->match.type);
  556. if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
  557. pr_cont("name=%.*s, ",
  558. (int)sizeof(p->match.name), p->match.name);
  559. else
  560. pr_cont("addr=%u, ", p->match.addr);
  561. pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
  562. }
  563. static void v4l_print_dbg_register(const void *arg, bool write_only)
  564. {
  565. const struct v4l2_dbg_register *p = arg;
  566. pr_cont("type=%u, ", p->match.type);
  567. if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
  568. pr_cont("name=%.*s, ",
  569. (int)sizeof(p->match.name), p->match.name);
  570. else
  571. pr_cont("addr=%u, ", p->match.addr);
  572. pr_cont("reg=0x%llx, val=0x%llx\n",
  573. p->reg, p->val);
  574. }
  575. static void v4l_print_dv_timings(const void *arg, bool write_only)
  576. {
  577. const struct v4l2_dv_timings *p = arg;
  578. switch (p->type) {
  579. case V4L2_DV_BT_656_1120:
  580. pr_cont("type=bt-656/1120, interlaced=%u, pixelclock=%llu, width=%u, height=%u, polarities=0x%x, hfrontporch=%u, hsync=%u, hbackporch=%u, vfrontporch=%u, vsync=%u, vbackporch=%u, il_vfrontporch=%u, il_vsync=%u, il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
  581. p->bt.interlaced, p->bt.pixelclock,
  582. p->bt.width, p->bt.height,
  583. p->bt.polarities, p->bt.hfrontporch,
  584. p->bt.hsync, p->bt.hbackporch,
  585. p->bt.vfrontporch, p->bt.vsync,
  586. p->bt.vbackporch, p->bt.il_vfrontporch,
  587. p->bt.il_vsync, p->bt.il_vbackporch,
  588. p->bt.standards, p->bt.flags);
  589. break;
  590. default:
  591. pr_cont("type=%d\n", p->type);
  592. break;
  593. }
  594. }
  595. static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
  596. {
  597. const struct v4l2_enum_dv_timings *p = arg;
  598. pr_cont("index=%u, ", p->index);
  599. v4l_print_dv_timings(&p->timings, write_only);
  600. }
  601. static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
  602. {
  603. const struct v4l2_dv_timings_cap *p = arg;
  604. switch (p->type) {
  605. case V4L2_DV_BT_656_1120:
  606. pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
  607. p->bt.min_width, p->bt.max_width,
  608. p->bt.min_height, p->bt.max_height,
  609. p->bt.min_pixelclock, p->bt.max_pixelclock,
  610. p->bt.standards, p->bt.capabilities);
  611. break;
  612. default:
  613. pr_cont("type=%u\n", p->type);
  614. break;
  615. }
  616. }
  617. static void v4l_print_frmsizeenum(const void *arg, bool write_only)
  618. {
  619. const struct v4l2_frmsizeenum *p = arg;
  620. pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
  621. p->index,
  622. (p->pixel_format & 0xff),
  623. (p->pixel_format >> 8) & 0xff,
  624. (p->pixel_format >> 16) & 0xff,
  625. (p->pixel_format >> 24) & 0xff,
  626. p->type);
  627. switch (p->type) {
  628. case V4L2_FRMSIZE_TYPE_DISCRETE:
  629. pr_cont(", wxh=%ux%u\n",
  630. p->discrete.width, p->discrete.height);
  631. break;
  632. case V4L2_FRMSIZE_TYPE_STEPWISE:
  633. pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
  634. p->stepwise.min_width,
  635. p->stepwise.min_height,
  636. p->stepwise.max_width,
  637. p->stepwise.max_height,
  638. p->stepwise.step_width,
  639. p->stepwise.step_height);
  640. break;
  641. case V4L2_FRMSIZE_TYPE_CONTINUOUS:
  642. /* fall through */
  643. default:
  644. pr_cont("\n");
  645. break;
  646. }
  647. }
  648. static void v4l_print_frmivalenum(const void *arg, bool write_only)
  649. {
  650. const struct v4l2_frmivalenum *p = arg;
  651. pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
  652. p->index,
  653. (p->pixel_format & 0xff),
  654. (p->pixel_format >> 8) & 0xff,
  655. (p->pixel_format >> 16) & 0xff,
  656. (p->pixel_format >> 24) & 0xff,
  657. p->width, p->height, p->type);
  658. switch (p->type) {
  659. case V4L2_FRMIVAL_TYPE_DISCRETE:
  660. pr_cont(", fps=%d/%d\n",
  661. p->discrete.numerator,
  662. p->discrete.denominator);
  663. break;
  664. case V4L2_FRMIVAL_TYPE_STEPWISE:
  665. pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
  666. p->stepwise.min.numerator,
  667. p->stepwise.min.denominator,
  668. p->stepwise.max.numerator,
  669. p->stepwise.max.denominator,
  670. p->stepwise.step.numerator,
  671. p->stepwise.step.denominator);
  672. break;
  673. case V4L2_FRMIVAL_TYPE_CONTINUOUS:
  674. /* fall through */
  675. default:
  676. pr_cont("\n");
  677. break;
  678. }
  679. }
  680. static void v4l_print_event(const void *arg, bool write_only)
  681. {
  682. const struct v4l2_event *p = arg;
  683. const struct v4l2_event_ctrl *c;
  684. pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%lu.%9.9lu\n",
  685. p->type, p->pending, p->sequence, p->id,
  686. p->timestamp.tv_sec, p->timestamp.tv_nsec);
  687. switch (p->type) {
  688. case V4L2_EVENT_VSYNC:
  689. printk(KERN_DEBUG "field=%s\n",
  690. prt_names(p->u.vsync.field, v4l2_field_names));
  691. break;
  692. case V4L2_EVENT_CTRL:
  693. c = &p->u.ctrl;
  694. printk(KERN_DEBUG "changes=0x%x, type=%u, ",
  695. c->changes, c->type);
  696. if (c->type == V4L2_CTRL_TYPE_INTEGER64)
  697. pr_cont("value64=%lld, ", c->value64);
  698. else
  699. pr_cont("value=%d, ", c->value);
  700. pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
  701. c->flags, c->minimum, c->maximum,
  702. c->step, c->default_value);
  703. break;
  704. case V4L2_EVENT_FRAME_SYNC:
  705. pr_cont("frame_sequence=%u\n",
  706. p->u.frame_sync.frame_sequence);
  707. break;
  708. }
  709. }
  710. static void v4l_print_event_subscription(const void *arg, bool write_only)
  711. {
  712. const struct v4l2_event_subscription *p = arg;
  713. pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
  714. p->type, p->id, p->flags);
  715. }
  716. static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
  717. {
  718. const struct v4l2_sliced_vbi_cap *p = arg;
  719. int i;
  720. pr_cont("type=%s, service_set=0x%08x\n",
  721. prt_names(p->type, v4l2_type_names), p->service_set);
  722. for (i = 0; i < 24; i++)
  723. printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
  724. p->service_lines[0][i],
  725. p->service_lines[1][i]);
  726. }
  727. static void v4l_print_freq_band(const void *arg, bool write_only)
  728. {
  729. const struct v4l2_frequency_band *p = arg;
  730. pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
  731. p->tuner, p->type, p->index,
  732. p->capability, p->rangelow,
  733. p->rangehigh, p->modulation);
  734. }
  735. static void v4l_print_edid(const void *arg, bool write_only)
  736. {
  737. const struct v4l2_edid *p = arg;
  738. pr_cont("pad=%u, start_block=%u, blocks=%u\n",
  739. p->pad, p->start_block, p->blocks);
  740. }
  741. static void v4l_print_u32(const void *arg, bool write_only)
  742. {
  743. pr_cont("value=%u\n", *(const u32 *)arg);
  744. }
  745. static void v4l_print_newline(const void *arg, bool write_only)
  746. {
  747. pr_cont("\n");
  748. }
  749. static void v4l_print_default(const void *arg, bool write_only)
  750. {
  751. pr_cont("driver-specific ioctl\n");
  752. }
  753. static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
  754. {
  755. __u32 i;
  756. /* zero the reserved fields */
  757. c->reserved[0] = c->reserved[1] = 0;
  758. for (i = 0; i < c->count; i++)
  759. c->controls[i].reserved2[0] = 0;
  760. /* V4L2_CID_PRIVATE_BASE cannot be used as control class
  761. when using extended controls.
  762. Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
  763. is it allowed for backwards compatibility.
  764. */
  765. if (!allow_priv && c->which == V4L2_CID_PRIVATE_BASE)
  766. return 0;
  767. if (!c->which)
  768. return 1;
  769. /* Check that all controls are from the same control class. */
  770. for (i = 0; i < c->count; i++) {
  771. if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
  772. c->error_idx = i;
  773. return 0;
  774. }
  775. }
  776. return 1;
  777. }
  778. static int check_fmt(struct file *file, enum v4l2_buf_type type)
  779. {
  780. struct video_device *vfd = video_devdata(file);
  781. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  782. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  783. bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
  784. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  785. bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
  786. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  787. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  788. if (ops == NULL)
  789. return -EINVAL;
  790. switch (type) {
  791. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  792. if ((is_vid || is_tch) && is_rx &&
  793. (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
  794. return 0;
  795. break;
  796. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  797. if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
  798. return 0;
  799. break;
  800. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  801. if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
  802. return 0;
  803. break;
  804. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  805. if (is_vid && is_tx &&
  806. (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
  807. return 0;
  808. break;
  809. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  810. if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
  811. return 0;
  812. break;
  813. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  814. if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
  815. return 0;
  816. break;
  817. case V4L2_BUF_TYPE_VBI_CAPTURE:
  818. if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
  819. return 0;
  820. break;
  821. case V4L2_BUF_TYPE_VBI_OUTPUT:
  822. if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
  823. return 0;
  824. break;
  825. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  826. if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
  827. return 0;
  828. break;
  829. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  830. if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
  831. return 0;
  832. break;
  833. case V4L2_BUF_TYPE_SDR_CAPTURE:
  834. if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
  835. return 0;
  836. break;
  837. case V4L2_BUF_TYPE_SDR_OUTPUT:
  838. if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
  839. return 0;
  840. break;
  841. case V4L2_BUF_TYPE_META_CAPTURE:
  842. if (is_vid && is_rx && ops->vidioc_g_fmt_meta_cap)
  843. return 0;
  844. break;
  845. default:
  846. break;
  847. }
  848. return -EINVAL;
  849. }
  850. static void v4l_sanitize_format(struct v4l2_format *fmt)
  851. {
  852. unsigned int offset;
  853. /*
  854. * The v4l2_pix_format structure has been extended with fields that were
  855. * not previously required to be set to zero by applications. The priv
  856. * field, when set to a magic value, indicates the the extended fields
  857. * are valid. Otherwise they will contain undefined values. To simplify
  858. * the API towards drivers zero the extended fields and set the priv
  859. * field to the magic value when the extended pixel format structure
  860. * isn't used by applications.
  861. */
  862. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  863. fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  864. return;
  865. if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
  866. return;
  867. fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  868. offset = offsetof(struct v4l2_pix_format, priv)
  869. + sizeof(fmt->fmt.pix.priv);
  870. memset(((void *)&fmt->fmt.pix) + offset, 0,
  871. sizeof(fmt->fmt.pix) - offset);
  872. }
  873. static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
  874. struct file *file, void *fh, void *arg)
  875. {
  876. struct v4l2_capability *cap = (struct v4l2_capability *)arg;
  877. struct video_device *vfd = video_devdata(file);
  878. int ret;
  879. cap->version = LINUX_VERSION_CODE;
  880. cap->device_caps = vfd->device_caps;
  881. cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
  882. ret = ops->vidioc_querycap(file, fh, cap);
  883. cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
  884. /*
  885. * Drivers MUST fill in device_caps, so check for this and
  886. * warn if it was forgotten.
  887. */
  888. WARN(!(cap->capabilities & V4L2_CAP_DEVICE_CAPS) ||
  889. !cap->device_caps, "Bad caps for driver %s, %x %x",
  890. cap->driver, cap->capabilities, cap->device_caps);
  891. cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
  892. return ret;
  893. }
  894. static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
  895. struct file *file, void *fh, void *arg)
  896. {
  897. struct video_device *vfd = video_devdata(file);
  898. int ret;
  899. ret = v4l_enable_media_source(vfd);
  900. if (ret)
  901. return ret;
  902. return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
  903. }
  904. static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
  905. struct file *file, void *fh, void *arg)
  906. {
  907. return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
  908. }
  909. static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
  910. struct file *file, void *fh, void *arg)
  911. {
  912. struct video_device *vfd;
  913. u32 *p = arg;
  914. vfd = video_devdata(file);
  915. *p = v4l2_prio_max(vfd->prio);
  916. return 0;
  917. }
  918. static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
  919. struct file *file, void *fh, void *arg)
  920. {
  921. struct video_device *vfd;
  922. struct v4l2_fh *vfh;
  923. u32 *p = arg;
  924. vfd = video_devdata(file);
  925. if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
  926. return -ENOTTY;
  927. vfh = file->private_data;
  928. return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
  929. }
  930. static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
  931. struct file *file, void *fh, void *arg)
  932. {
  933. struct video_device *vfd = video_devdata(file);
  934. struct v4l2_input *p = arg;
  935. /*
  936. * We set the flags for CAP_DV_TIMINGS &
  937. * CAP_STD here based on ioctl handler provided by the
  938. * driver. If the driver doesn't support these
  939. * for a specific input, it must override these flags.
  940. */
  941. if (is_valid_ioctl(vfd, VIDIOC_S_STD))
  942. p->capabilities |= V4L2_IN_CAP_STD;
  943. return ops->vidioc_enum_input(file, fh, p);
  944. }
  945. static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
  946. struct file *file, void *fh, void *arg)
  947. {
  948. struct video_device *vfd = video_devdata(file);
  949. struct v4l2_output *p = arg;
  950. /*
  951. * We set the flags for CAP_DV_TIMINGS &
  952. * CAP_STD here based on ioctl handler provided by the
  953. * driver. If the driver doesn't support these
  954. * for a specific output, it must override these flags.
  955. */
  956. if (is_valid_ioctl(vfd, VIDIOC_S_STD))
  957. p->capabilities |= V4L2_OUT_CAP_STD;
  958. return ops->vidioc_enum_output(file, fh, p);
  959. }
  960. static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
  961. {
  962. const unsigned sz = sizeof(fmt->description);
  963. const char *descr = NULL;
  964. u32 flags = 0;
  965. /*
  966. * We depart from the normal coding style here since the descriptions
  967. * should be aligned so it is easy to see which descriptions will be
  968. * longer than 31 characters (the max length for a description).
  969. * And frankly, this is easier to read anyway.
  970. *
  971. * Note that gcc will use O(log N) comparisons to find the right case.
  972. */
  973. switch (fmt->pixelformat) {
  974. /* Max description length mask: descr = "0123456789012345678901234567890" */
  975. case V4L2_PIX_FMT_RGB332: descr = "8-bit RGB 3-3-2"; break;
  976. case V4L2_PIX_FMT_RGB444: descr = "16-bit A/XRGB 4-4-4-4"; break;
  977. case V4L2_PIX_FMT_ARGB444: descr = "16-bit ARGB 4-4-4-4"; break;
  978. case V4L2_PIX_FMT_XRGB444: descr = "16-bit XRGB 4-4-4-4"; break;
  979. case V4L2_PIX_FMT_RGB555: descr = "16-bit A/XRGB 1-5-5-5"; break;
  980. case V4L2_PIX_FMT_ARGB555: descr = "16-bit ARGB 1-5-5-5"; break;
  981. case V4L2_PIX_FMT_XRGB555: descr = "16-bit XRGB 1-5-5-5"; break;
  982. case V4L2_PIX_FMT_RGB565: descr = "16-bit RGB 5-6-5"; break;
  983. case V4L2_PIX_FMT_RGB555X: descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
  984. case V4L2_PIX_FMT_ARGB555X: descr = "16-bit ARGB 1-5-5-5 BE"; break;
  985. case V4L2_PIX_FMT_XRGB555X: descr = "16-bit XRGB 1-5-5-5 BE"; break;
  986. case V4L2_PIX_FMT_RGB565X: descr = "16-bit RGB 5-6-5 BE"; break;
  987. case V4L2_PIX_FMT_BGR666: descr = "18-bit BGRX 6-6-6-14"; break;
  988. case V4L2_PIX_FMT_BGR24: descr = "24-bit BGR 8-8-8"; break;
  989. case V4L2_PIX_FMT_RGB24: descr = "24-bit RGB 8-8-8"; break;
  990. case V4L2_PIX_FMT_BGR32: descr = "32-bit BGRA/X 8-8-8-8"; break;
  991. case V4L2_PIX_FMT_ABGR32: descr = "32-bit BGRA 8-8-8-8"; break;
  992. case V4L2_PIX_FMT_XBGR32: descr = "32-bit BGRX 8-8-8-8"; break;
  993. case V4L2_PIX_FMT_RGB32: descr = "32-bit A/XRGB 8-8-8-8"; break;
  994. case V4L2_PIX_FMT_ARGB32: descr = "32-bit ARGB 8-8-8-8"; break;
  995. case V4L2_PIX_FMT_XRGB32: descr = "32-bit XRGB 8-8-8-8"; break;
  996. case V4L2_PIX_FMT_GREY: descr = "8-bit Greyscale"; break;
  997. case V4L2_PIX_FMT_Y4: descr = "4-bit Greyscale"; break;
  998. case V4L2_PIX_FMT_Y6: descr = "6-bit Greyscale"; break;
  999. case V4L2_PIX_FMT_Y10: descr = "10-bit Greyscale"; break;
  1000. case V4L2_PIX_FMT_Y12: descr = "12-bit Greyscale"; break;
  1001. case V4L2_PIX_FMT_Y16: descr = "16-bit Greyscale"; break;
  1002. case V4L2_PIX_FMT_Y16_BE: descr = "16-bit Greyscale BE"; break;
  1003. case V4L2_PIX_FMT_Y10BPACK: descr = "10-bit Greyscale (Packed)"; break;
  1004. case V4L2_PIX_FMT_Y8I: descr = "Interleaved 8-bit Greyscale"; break;
  1005. case V4L2_PIX_FMT_Y12I: descr = "Interleaved 12-bit Greyscale"; break;
  1006. case V4L2_PIX_FMT_Z16: descr = "16-bit Depth"; break;
  1007. case V4L2_PIX_FMT_INZI: descr = "Planar 10:16 Greyscale Depth"; break;
  1008. case V4L2_PIX_FMT_PAL8: descr = "8-bit Palette"; break;
  1009. case V4L2_PIX_FMT_UV8: descr = "8-bit Chrominance UV 4-4"; break;
  1010. case V4L2_PIX_FMT_YVU410: descr = "Planar YVU 4:1:0"; break;
  1011. case V4L2_PIX_FMT_YVU420: descr = "Planar YVU 4:2:0"; break;
  1012. case V4L2_PIX_FMT_YUYV: descr = "YUYV 4:2:2"; break;
  1013. case V4L2_PIX_FMT_YYUV: descr = "YYUV 4:2:2"; break;
  1014. case V4L2_PIX_FMT_YVYU: descr = "YVYU 4:2:2"; break;
  1015. case V4L2_PIX_FMT_UYVY: descr = "UYVY 4:2:2"; break;
  1016. case V4L2_PIX_FMT_VYUY: descr = "VYUY 4:2:2"; break;
  1017. case V4L2_PIX_FMT_YUV422P: descr = "Planar YUV 4:2:2"; break;
  1018. case V4L2_PIX_FMT_YUV411P: descr = "Planar YUV 4:1:1"; break;
  1019. case V4L2_PIX_FMT_Y41P: descr = "YUV 4:1:1 (Packed)"; break;
  1020. case V4L2_PIX_FMT_YUV444: descr = "16-bit A/XYUV 4-4-4-4"; break;
  1021. case V4L2_PIX_FMT_YUV555: descr = "16-bit A/XYUV 1-5-5-5"; break;
  1022. case V4L2_PIX_FMT_YUV565: descr = "16-bit YUV 5-6-5"; break;
  1023. case V4L2_PIX_FMT_YUV32: descr = "32-bit A/XYUV 8-8-8-8"; break;
  1024. case V4L2_PIX_FMT_YUV410: descr = "Planar YUV 4:1:0"; break;
  1025. case V4L2_PIX_FMT_YUV420: descr = "Planar YUV 4:2:0"; break;
  1026. case V4L2_PIX_FMT_HI240: descr = "8-bit Dithered RGB (BTTV)"; break;
  1027. case V4L2_PIX_FMT_HM12: descr = "YUV 4:2:0 (16x16 Macroblocks)"; break;
  1028. case V4L2_PIX_FMT_M420: descr = "YUV 4:2:0 (M420)"; break;
  1029. case V4L2_PIX_FMT_NV12: descr = "Y/CbCr 4:2:0"; break;
  1030. case V4L2_PIX_FMT_NV21: descr = "Y/CrCb 4:2:0"; break;
  1031. case V4L2_PIX_FMT_NV16: descr = "Y/CbCr 4:2:2"; break;
  1032. case V4L2_PIX_FMT_NV61: descr = "Y/CrCb 4:2:2"; break;
  1033. case V4L2_PIX_FMT_NV24: descr = "Y/CbCr 4:4:4"; break;
  1034. case V4L2_PIX_FMT_NV42: descr = "Y/CrCb 4:4:4"; break;
  1035. case V4L2_PIX_FMT_NV12M: descr = "Y/CbCr 4:2:0 (N-C)"; break;
  1036. case V4L2_PIX_FMT_NV21M: descr = "Y/CrCb 4:2:0 (N-C)"; break;
  1037. case V4L2_PIX_FMT_NV16M: descr = "Y/CbCr 4:2:2 (N-C)"; break;
  1038. case V4L2_PIX_FMT_NV61M: descr = "Y/CrCb 4:2:2 (N-C)"; break;
  1039. case V4L2_PIX_FMT_NV12MT: descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
  1040. case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
  1041. case V4L2_PIX_FMT_YUV420M: descr = "Planar YUV 4:2:0 (N-C)"; break;
  1042. case V4L2_PIX_FMT_YVU420M: descr = "Planar YVU 4:2:0 (N-C)"; break;
  1043. case V4L2_PIX_FMT_YUV422M: descr = "Planar YUV 4:2:2 (N-C)"; break;
  1044. case V4L2_PIX_FMT_YVU422M: descr = "Planar YVU 4:2:2 (N-C)"; break;
  1045. case V4L2_PIX_FMT_YUV444M: descr = "Planar YUV 4:4:4 (N-C)"; break;
  1046. case V4L2_PIX_FMT_YVU444M: descr = "Planar YVU 4:4:4 (N-C)"; break;
  1047. case V4L2_PIX_FMT_SBGGR8: descr = "8-bit Bayer BGBG/GRGR"; break;
  1048. case V4L2_PIX_FMT_SGBRG8: descr = "8-bit Bayer GBGB/RGRG"; break;
  1049. case V4L2_PIX_FMT_SGRBG8: descr = "8-bit Bayer GRGR/BGBG"; break;
  1050. case V4L2_PIX_FMT_SRGGB8: descr = "8-bit Bayer RGRG/GBGB"; break;
  1051. case V4L2_PIX_FMT_SBGGR10: descr = "10-bit Bayer BGBG/GRGR"; break;
  1052. case V4L2_PIX_FMT_SGBRG10: descr = "10-bit Bayer GBGB/RGRG"; break;
  1053. case V4L2_PIX_FMT_SGRBG10: descr = "10-bit Bayer GRGR/BGBG"; break;
  1054. case V4L2_PIX_FMT_SRGGB10: descr = "10-bit Bayer RGRG/GBGB"; break;
  1055. case V4L2_PIX_FMT_SBGGR10P: descr = "10-bit Bayer BGBG/GRGR Packed"; break;
  1056. case V4L2_PIX_FMT_SGBRG10P: descr = "10-bit Bayer GBGB/RGRG Packed"; break;
  1057. case V4L2_PIX_FMT_SGRBG10P: descr = "10-bit Bayer GRGR/BGBG Packed"; break;
  1058. case V4L2_PIX_FMT_SRGGB10P: descr = "10-bit Bayer RGRG/GBGB Packed"; break;
  1059. case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
  1060. case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
  1061. case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
  1062. case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
  1063. case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
  1064. case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
  1065. case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
  1066. case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
  1067. case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
  1068. case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
  1069. case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
  1070. case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
  1071. case V4L2_PIX_FMT_SBGGR12: descr = "12-bit Bayer BGBG/GRGR"; break;
  1072. case V4L2_PIX_FMT_SGBRG12: descr = "12-bit Bayer GBGB/RGRG"; break;
  1073. case V4L2_PIX_FMT_SGRBG12: descr = "12-bit Bayer GRGR/BGBG"; break;
  1074. case V4L2_PIX_FMT_SRGGB12: descr = "12-bit Bayer RGRG/GBGB"; break;
  1075. case V4L2_PIX_FMT_SBGGR12P: descr = "12-bit Bayer BGBG/GRGR Packed"; break;
  1076. case V4L2_PIX_FMT_SGBRG12P: descr = "12-bit Bayer GBGB/RGRG Packed"; break;
  1077. case V4L2_PIX_FMT_SGRBG12P: descr = "12-bit Bayer GRGR/BGBG Packed"; break;
  1078. case V4L2_PIX_FMT_SRGGB12P: descr = "12-bit Bayer RGRG/GBGB Packed"; break;
  1079. case V4L2_PIX_FMT_SBGGR16: descr = "16-bit Bayer BGBG/GRGR"; break;
  1080. case V4L2_PIX_FMT_SGBRG16: descr = "16-bit Bayer GBGB/RGRG"; break;
  1081. case V4L2_PIX_FMT_SGRBG16: descr = "16-bit Bayer GRGR/BGBG"; break;
  1082. case V4L2_PIX_FMT_SRGGB16: descr = "16-bit Bayer RGRG/GBGB"; break;
  1083. case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
  1084. case V4L2_PIX_FMT_SPCA501: descr = "GSPCA SPCA501"; break;
  1085. case V4L2_PIX_FMT_SPCA505: descr = "GSPCA SPCA505"; break;
  1086. case V4L2_PIX_FMT_SPCA508: descr = "GSPCA SPCA508"; break;
  1087. case V4L2_PIX_FMT_STV0680: descr = "GSPCA STV0680"; break;
  1088. case V4L2_PIX_FMT_TM6000: descr = "A/V + VBI Mux Packet"; break;
  1089. case V4L2_PIX_FMT_CIT_YYVYUY: descr = "GSPCA CIT YYVYUY"; break;
  1090. case V4L2_PIX_FMT_KONICA420: descr = "GSPCA KONICA420"; break;
  1091. case V4L2_PIX_FMT_HSV24: descr = "24-bit HSV 8-8-8"; break;
  1092. case V4L2_PIX_FMT_HSV32: descr = "32-bit XHSV 8-8-8-8"; break;
  1093. case V4L2_SDR_FMT_CU8: descr = "Complex U8"; break;
  1094. case V4L2_SDR_FMT_CU16LE: descr = "Complex U16LE"; break;
  1095. case V4L2_SDR_FMT_CS8: descr = "Complex S8"; break;
  1096. case V4L2_SDR_FMT_CS14LE: descr = "Complex S14LE"; break;
  1097. case V4L2_SDR_FMT_RU12LE: descr = "Real U12LE"; break;
  1098. case V4L2_SDR_FMT_PCU16BE: descr = "Planar Complex U16BE"; break;
  1099. case V4L2_SDR_FMT_PCU18BE: descr = "Planar Complex U18BE"; break;
  1100. case V4L2_SDR_FMT_PCU20BE: descr = "Planar Complex U20BE"; break;
  1101. case V4L2_TCH_FMT_DELTA_TD16: descr = "16-bit signed deltas"; break;
  1102. case V4L2_TCH_FMT_DELTA_TD08: descr = "8-bit signed deltas"; break;
  1103. case V4L2_TCH_FMT_TU16: descr = "16-bit unsigned touch data"; break;
  1104. case V4L2_TCH_FMT_TU08: descr = "8-bit unsigned touch data"; break;
  1105. case V4L2_META_FMT_VSP1_HGO: descr = "R-Car VSP1 1-D Histogram"; break;
  1106. case V4L2_META_FMT_VSP1_HGT: descr = "R-Car VSP1 2-D Histogram"; break;
  1107. case V4L2_META_FMT_UVC: descr = "UVC payload header metadata"; break;
  1108. default:
  1109. /* Compressed formats */
  1110. flags = V4L2_FMT_FLAG_COMPRESSED;
  1111. switch (fmt->pixelformat) {
  1112. /* Max description length mask: descr = "0123456789012345678901234567890" */
  1113. case V4L2_PIX_FMT_MJPEG: descr = "Motion-JPEG"; break;
  1114. case V4L2_PIX_FMT_JPEG: descr = "JFIF JPEG"; break;
  1115. case V4L2_PIX_FMT_DV: descr = "1394"; break;
  1116. case V4L2_PIX_FMT_MPEG: descr = "MPEG-1/2/4"; break;
  1117. case V4L2_PIX_FMT_H264: descr = "H.264"; break;
  1118. case V4L2_PIX_FMT_H264_NO_SC: descr = "H.264 (No Start Codes)"; break;
  1119. case V4L2_PIX_FMT_H264_MVC: descr = "H.264 MVC"; break;
  1120. case V4L2_PIX_FMT_H263: descr = "H.263"; break;
  1121. case V4L2_PIX_FMT_MPEG1: descr = "MPEG-1 ES"; break;
  1122. case V4L2_PIX_FMT_MPEG2: descr = "MPEG-2 ES"; break;
  1123. case V4L2_PIX_FMT_MPEG4: descr = "MPEG-4 part 2 ES"; break;
  1124. case V4L2_PIX_FMT_XVID: descr = "Xvid"; break;
  1125. case V4L2_PIX_FMT_VC1_ANNEX_G: descr = "VC-1 (SMPTE 412M Annex G)"; break;
  1126. case V4L2_PIX_FMT_VC1_ANNEX_L: descr = "VC-1 (SMPTE 412M Annex L)"; break;
  1127. case V4L2_PIX_FMT_VP8: descr = "VP8"; break;
  1128. case V4L2_PIX_FMT_VP9: descr = "VP9"; break;
  1129. case V4L2_PIX_FMT_HEVC: descr = "HEVC"; break; /* aka H.265 */
  1130. case V4L2_PIX_FMT_CPIA1: descr = "GSPCA CPiA YUV"; break;
  1131. case V4L2_PIX_FMT_WNVA: descr = "WNVA"; break;
  1132. case V4L2_PIX_FMT_SN9C10X: descr = "GSPCA SN9C10X"; break;
  1133. case V4L2_PIX_FMT_PWC1: descr = "Raw Philips Webcam Type (Old)"; break;
  1134. case V4L2_PIX_FMT_PWC2: descr = "Raw Philips Webcam Type (New)"; break;
  1135. case V4L2_PIX_FMT_ET61X251: descr = "GSPCA ET61X251"; break;
  1136. case V4L2_PIX_FMT_SPCA561: descr = "GSPCA SPCA561"; break;
  1137. case V4L2_PIX_FMT_PAC207: descr = "GSPCA PAC207"; break;
  1138. case V4L2_PIX_FMT_MR97310A: descr = "GSPCA MR97310A"; break;
  1139. case V4L2_PIX_FMT_JL2005BCD: descr = "GSPCA JL2005BCD"; break;
  1140. case V4L2_PIX_FMT_SN9C2028: descr = "GSPCA SN9C2028"; break;
  1141. case V4L2_PIX_FMT_SQ905C: descr = "GSPCA SQ905C"; break;
  1142. case V4L2_PIX_FMT_PJPG: descr = "GSPCA PJPG"; break;
  1143. case V4L2_PIX_FMT_OV511: descr = "GSPCA OV511"; break;
  1144. case V4L2_PIX_FMT_OV518: descr = "GSPCA OV518"; break;
  1145. case V4L2_PIX_FMT_JPGL: descr = "JPEG Lite"; break;
  1146. case V4L2_PIX_FMT_SE401: descr = "GSPCA SE401"; break;
  1147. case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
  1148. case V4L2_PIX_FMT_MT21C: descr = "Mediatek Compressed Format"; break;
  1149. default:
  1150. WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
  1151. if (fmt->description[0])
  1152. return;
  1153. flags = 0;
  1154. snprintf(fmt->description, sz, "%c%c%c%c%s",
  1155. (char)(fmt->pixelformat & 0x7f),
  1156. (char)((fmt->pixelformat >> 8) & 0x7f),
  1157. (char)((fmt->pixelformat >> 16) & 0x7f),
  1158. (char)((fmt->pixelformat >> 24) & 0x7f),
  1159. (fmt->pixelformat & (1 << 31)) ? "-BE" : "");
  1160. break;
  1161. }
  1162. }
  1163. if (descr)
  1164. WARN_ON(strlcpy(fmt->description, descr, sz) >= sz);
  1165. fmt->flags = flags;
  1166. }
  1167. static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
  1168. struct file *file, void *fh, void *arg)
  1169. {
  1170. struct v4l2_fmtdesc *p = arg;
  1171. int ret = check_fmt(file, p->type);
  1172. if (ret)
  1173. return ret;
  1174. ret = -EINVAL;
  1175. switch (p->type) {
  1176. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1177. if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
  1178. break;
  1179. ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
  1180. break;
  1181. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1182. if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
  1183. break;
  1184. ret = ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
  1185. break;
  1186. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1187. if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
  1188. break;
  1189. ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
  1190. break;
  1191. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1192. if (unlikely(!ops->vidioc_enum_fmt_vid_out))
  1193. break;
  1194. ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
  1195. break;
  1196. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1197. if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
  1198. break;
  1199. ret = ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
  1200. break;
  1201. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1202. if (unlikely(!ops->vidioc_enum_fmt_sdr_cap))
  1203. break;
  1204. ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
  1205. break;
  1206. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1207. if (unlikely(!ops->vidioc_enum_fmt_sdr_out))
  1208. break;
  1209. ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
  1210. break;
  1211. case V4L2_BUF_TYPE_META_CAPTURE:
  1212. if (unlikely(!ops->vidioc_enum_fmt_meta_cap))
  1213. break;
  1214. ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
  1215. break;
  1216. }
  1217. if (ret == 0)
  1218. v4l_fill_fmtdesc(p);
  1219. return ret;
  1220. }
  1221. static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
  1222. struct file *file, void *fh, void *arg)
  1223. {
  1224. struct v4l2_format *p = arg;
  1225. int ret = check_fmt(file, p->type);
  1226. if (ret)
  1227. return ret;
  1228. /*
  1229. * fmt can't be cleared for these overlay types due to the 'clips'
  1230. * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
  1231. * Those are provided by the user. So handle these two overlay types
  1232. * first, and then just do a simple memset for the other types.
  1233. */
  1234. switch (p->type) {
  1235. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1236. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
  1237. struct v4l2_clip __user *clips = p->fmt.win.clips;
  1238. u32 clipcount = p->fmt.win.clipcount;
  1239. void __user *bitmap = p->fmt.win.bitmap;
  1240. memset(&p->fmt, 0, sizeof(p->fmt));
  1241. p->fmt.win.clips = clips;
  1242. p->fmt.win.clipcount = clipcount;
  1243. p->fmt.win.bitmap = bitmap;
  1244. break;
  1245. }
  1246. default:
  1247. memset(&p->fmt, 0, sizeof(p->fmt));
  1248. break;
  1249. }
  1250. switch (p->type) {
  1251. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1252. if (unlikely(!ops->vidioc_g_fmt_vid_cap))
  1253. break;
  1254. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1255. ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
  1256. /* just in case the driver zeroed it again */
  1257. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1258. return ret;
  1259. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1260. return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
  1261. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1262. return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
  1263. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1264. return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
  1265. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1266. return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
  1267. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1268. if (unlikely(!ops->vidioc_g_fmt_vid_out))
  1269. break;
  1270. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1271. ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
  1272. /* just in case the driver zeroed it again */
  1273. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1274. return ret;
  1275. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1276. return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
  1277. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1278. return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
  1279. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1280. return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
  1281. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1282. return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
  1283. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1284. return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
  1285. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1286. return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
  1287. case V4L2_BUF_TYPE_META_CAPTURE:
  1288. return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
  1289. }
  1290. return -EINVAL;
  1291. }
  1292. static void v4l_pix_format_touch(struct v4l2_pix_format *p)
  1293. {
  1294. /*
  1295. * The v4l2_pix_format structure contains fields that make no sense for
  1296. * touch. Set them to default values in this case.
  1297. */
  1298. p->field = V4L2_FIELD_NONE;
  1299. p->colorspace = V4L2_COLORSPACE_RAW;
  1300. p->flags = 0;
  1301. p->ycbcr_enc = 0;
  1302. p->quantization = 0;
  1303. p->xfer_func = 0;
  1304. }
  1305. static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
  1306. struct file *file, void *fh, void *arg)
  1307. {
  1308. struct v4l2_format *p = arg;
  1309. struct video_device *vfd = video_devdata(file);
  1310. int ret = check_fmt(file, p->type);
  1311. if (ret)
  1312. return ret;
  1313. ret = v4l_enable_media_source(vfd);
  1314. if (ret)
  1315. return ret;
  1316. v4l_sanitize_format(p);
  1317. switch (p->type) {
  1318. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1319. if (unlikely(!ops->vidioc_s_fmt_vid_cap))
  1320. break;
  1321. CLEAR_AFTER_FIELD(p, fmt.pix);
  1322. ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
  1323. /* just in case the driver zeroed it again */
  1324. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1325. if (vfd->vfl_type == VFL_TYPE_TOUCH)
  1326. v4l_pix_format_touch(&p->fmt.pix);
  1327. return ret;
  1328. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1329. if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
  1330. break;
  1331. CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
  1332. return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
  1333. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1334. if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
  1335. break;
  1336. CLEAR_AFTER_FIELD(p, fmt.win);
  1337. return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
  1338. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1339. if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
  1340. break;
  1341. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1342. return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
  1343. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1344. if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
  1345. break;
  1346. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1347. return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
  1348. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1349. if (unlikely(!ops->vidioc_s_fmt_vid_out))
  1350. break;
  1351. CLEAR_AFTER_FIELD(p, fmt.pix);
  1352. ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
  1353. /* just in case the driver zeroed it again */
  1354. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1355. return ret;
  1356. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1357. if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
  1358. break;
  1359. CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
  1360. return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
  1361. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1362. if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
  1363. break;
  1364. CLEAR_AFTER_FIELD(p, fmt.win);
  1365. return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
  1366. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1367. if (unlikely(!ops->vidioc_s_fmt_vbi_out))
  1368. break;
  1369. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1370. return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
  1371. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1372. if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
  1373. break;
  1374. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1375. return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
  1376. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1377. if (unlikely(!ops->vidioc_s_fmt_sdr_cap))
  1378. break;
  1379. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1380. return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
  1381. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1382. if (unlikely(!ops->vidioc_s_fmt_sdr_out))
  1383. break;
  1384. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1385. return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
  1386. case V4L2_BUF_TYPE_META_CAPTURE:
  1387. if (unlikely(!ops->vidioc_s_fmt_meta_cap))
  1388. break;
  1389. CLEAR_AFTER_FIELD(p, fmt.meta);
  1390. return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
  1391. }
  1392. return -EINVAL;
  1393. }
  1394. static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
  1395. struct file *file, void *fh, void *arg)
  1396. {
  1397. struct v4l2_format *p = arg;
  1398. int ret = check_fmt(file, p->type);
  1399. if (ret)
  1400. return ret;
  1401. v4l_sanitize_format(p);
  1402. switch (p->type) {
  1403. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1404. if (unlikely(!ops->vidioc_try_fmt_vid_cap))
  1405. break;
  1406. CLEAR_AFTER_FIELD(p, fmt.pix);
  1407. ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
  1408. /* just in case the driver zeroed it again */
  1409. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1410. return ret;
  1411. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1412. if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
  1413. break;
  1414. CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
  1415. return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
  1416. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1417. if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
  1418. break;
  1419. CLEAR_AFTER_FIELD(p, fmt.win);
  1420. return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
  1421. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1422. if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
  1423. break;
  1424. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1425. return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
  1426. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1427. if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
  1428. break;
  1429. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1430. return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
  1431. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1432. if (unlikely(!ops->vidioc_try_fmt_vid_out))
  1433. break;
  1434. CLEAR_AFTER_FIELD(p, fmt.pix);
  1435. ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
  1436. /* just in case the driver zeroed it again */
  1437. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1438. return ret;
  1439. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1440. if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
  1441. break;
  1442. CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
  1443. return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
  1444. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1445. if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
  1446. break;
  1447. CLEAR_AFTER_FIELD(p, fmt.win);
  1448. return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
  1449. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1450. if (unlikely(!ops->vidioc_try_fmt_vbi_out))
  1451. break;
  1452. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1453. return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
  1454. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1455. if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
  1456. break;
  1457. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1458. return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
  1459. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1460. if (unlikely(!ops->vidioc_try_fmt_sdr_cap))
  1461. break;
  1462. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1463. return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
  1464. case V4L2_BUF_TYPE_SDR_OUTPUT:
  1465. if (unlikely(!ops->vidioc_try_fmt_sdr_out))
  1466. break;
  1467. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1468. return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
  1469. case V4L2_BUF_TYPE_META_CAPTURE:
  1470. if (unlikely(!ops->vidioc_try_fmt_meta_cap))
  1471. break;
  1472. CLEAR_AFTER_FIELD(p, fmt.meta);
  1473. return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
  1474. }
  1475. return -EINVAL;
  1476. }
  1477. static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
  1478. struct file *file, void *fh, void *arg)
  1479. {
  1480. return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
  1481. }
  1482. static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
  1483. struct file *file, void *fh, void *arg)
  1484. {
  1485. return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
  1486. }
  1487. static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
  1488. struct file *file, void *fh, void *arg)
  1489. {
  1490. struct video_device *vfd = video_devdata(file);
  1491. struct v4l2_tuner *p = arg;
  1492. int err;
  1493. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1494. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1495. err = ops->vidioc_g_tuner(file, fh, p);
  1496. if (!err)
  1497. p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
  1498. return err;
  1499. }
  1500. static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
  1501. struct file *file, void *fh, void *arg)
  1502. {
  1503. struct video_device *vfd = video_devdata(file);
  1504. struct v4l2_tuner *p = arg;
  1505. int ret;
  1506. ret = v4l_enable_media_source(vfd);
  1507. if (ret)
  1508. return ret;
  1509. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1510. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1511. return ops->vidioc_s_tuner(file, fh, p);
  1512. }
  1513. static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
  1514. struct file *file, void *fh, void *arg)
  1515. {
  1516. struct video_device *vfd = video_devdata(file);
  1517. struct v4l2_modulator *p = arg;
  1518. int err;
  1519. if (vfd->vfl_type == VFL_TYPE_RADIO)
  1520. p->type = V4L2_TUNER_RADIO;
  1521. err = ops->vidioc_g_modulator(file, fh, p);
  1522. if (!err)
  1523. p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
  1524. return err;
  1525. }
  1526. static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
  1527. struct file *file, void *fh, void *arg)
  1528. {
  1529. struct video_device *vfd = video_devdata(file);
  1530. struct v4l2_modulator *p = arg;
  1531. if (vfd->vfl_type == VFL_TYPE_RADIO)
  1532. p->type = V4L2_TUNER_RADIO;
  1533. return ops->vidioc_s_modulator(file, fh, p);
  1534. }
  1535. static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
  1536. struct file *file, void *fh, void *arg)
  1537. {
  1538. struct video_device *vfd = video_devdata(file);
  1539. struct v4l2_frequency *p = arg;
  1540. if (vfd->vfl_type == VFL_TYPE_SDR)
  1541. p->type = V4L2_TUNER_SDR;
  1542. else
  1543. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1544. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1545. return ops->vidioc_g_frequency(file, fh, p);
  1546. }
  1547. static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
  1548. struct file *file, void *fh, void *arg)
  1549. {
  1550. struct video_device *vfd = video_devdata(file);
  1551. const struct v4l2_frequency *p = arg;
  1552. enum v4l2_tuner_type type;
  1553. int ret;
  1554. ret = v4l_enable_media_source(vfd);
  1555. if (ret)
  1556. return ret;
  1557. if (vfd->vfl_type == VFL_TYPE_SDR) {
  1558. if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
  1559. return -EINVAL;
  1560. } else {
  1561. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1562. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1563. if (type != p->type)
  1564. return -EINVAL;
  1565. }
  1566. return ops->vidioc_s_frequency(file, fh, p);
  1567. }
  1568. static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
  1569. struct file *file, void *fh, void *arg)
  1570. {
  1571. struct video_device *vfd = video_devdata(file);
  1572. struct v4l2_standard *p = arg;
  1573. v4l2_std_id id = vfd->tvnorms, curr_id = 0;
  1574. unsigned int index = p->index, i, j = 0;
  1575. const char *descr = "";
  1576. /* Return -ENODATA if the tvnorms for the current input
  1577. or output is 0, meaning that it doesn't support this API. */
  1578. if (id == 0)
  1579. return -ENODATA;
  1580. /* Return norm array in a canonical way */
  1581. for (i = 0; i <= index && id; i++) {
  1582. /* last std value in the standards array is 0, so this
  1583. while always ends there since (id & 0) == 0. */
  1584. while ((id & standards[j].std) != standards[j].std)
  1585. j++;
  1586. curr_id = standards[j].std;
  1587. descr = standards[j].descr;
  1588. j++;
  1589. if (curr_id == 0)
  1590. break;
  1591. if (curr_id != V4L2_STD_PAL &&
  1592. curr_id != V4L2_STD_SECAM &&
  1593. curr_id != V4L2_STD_NTSC)
  1594. id &= ~curr_id;
  1595. }
  1596. if (i <= index)
  1597. return -EINVAL;
  1598. v4l2_video_std_construct(p, curr_id, descr);
  1599. return 0;
  1600. }
  1601. static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
  1602. struct file *file, void *fh, void *arg)
  1603. {
  1604. struct video_device *vfd = video_devdata(file);
  1605. v4l2_std_id id = *(v4l2_std_id *)arg, norm;
  1606. int ret;
  1607. ret = v4l_enable_media_source(vfd);
  1608. if (ret)
  1609. return ret;
  1610. norm = id & vfd->tvnorms;
  1611. if (vfd->tvnorms && !norm) /* Check if std is supported */
  1612. return -EINVAL;
  1613. /* Calls the specific handler */
  1614. return ops->vidioc_s_std(file, fh, norm);
  1615. }
  1616. static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
  1617. struct file *file, void *fh, void *arg)
  1618. {
  1619. struct video_device *vfd = video_devdata(file);
  1620. v4l2_std_id *p = arg;
  1621. int ret;
  1622. ret = v4l_enable_media_source(vfd);
  1623. if (ret)
  1624. return ret;
  1625. /*
  1626. * If no signal is detected, then the driver should return
  1627. * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
  1628. * any standards that do not apply removed.
  1629. *
  1630. * This means that tuners, audio and video decoders can join
  1631. * their efforts to improve the standards detection.
  1632. */
  1633. *p = vfd->tvnorms;
  1634. return ops->vidioc_querystd(file, fh, arg);
  1635. }
  1636. static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
  1637. struct file *file, void *fh, void *arg)
  1638. {
  1639. struct video_device *vfd = video_devdata(file);
  1640. struct v4l2_hw_freq_seek *p = arg;
  1641. enum v4l2_tuner_type type;
  1642. int ret;
  1643. ret = v4l_enable_media_source(vfd);
  1644. if (ret)
  1645. return ret;
  1646. /* s_hw_freq_seek is not supported for SDR for now */
  1647. if (vfd->vfl_type == VFL_TYPE_SDR)
  1648. return -EINVAL;
  1649. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1650. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1651. if (p->type != type)
  1652. return -EINVAL;
  1653. return ops->vidioc_s_hw_freq_seek(file, fh, p);
  1654. }
  1655. static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
  1656. struct file *file, void *fh, void *arg)
  1657. {
  1658. return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
  1659. }
  1660. static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
  1661. struct file *file, void *fh, void *arg)
  1662. {
  1663. struct v4l2_requestbuffers *p = arg;
  1664. int ret = check_fmt(file, p->type);
  1665. if (ret)
  1666. return ret;
  1667. CLEAR_AFTER_FIELD(p, memory);
  1668. return ops->vidioc_reqbufs(file, fh, p);
  1669. }
  1670. static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
  1671. struct file *file, void *fh, void *arg)
  1672. {
  1673. struct v4l2_buffer *p = arg;
  1674. int ret = check_fmt(file, p->type);
  1675. return ret ? ret : ops->vidioc_querybuf(file, fh, p);
  1676. }
  1677. static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
  1678. struct file *file, void *fh, void *arg)
  1679. {
  1680. struct v4l2_buffer *p = arg;
  1681. int ret = check_fmt(file, p->type);
  1682. return ret ? ret : ops->vidioc_qbuf(file, fh, p);
  1683. }
  1684. static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
  1685. struct file *file, void *fh, void *arg)
  1686. {
  1687. struct v4l2_buffer *p = arg;
  1688. int ret = check_fmt(file, p->type);
  1689. return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
  1690. }
  1691. static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
  1692. struct file *file, void *fh, void *arg)
  1693. {
  1694. struct v4l2_create_buffers *create = arg;
  1695. int ret = check_fmt(file, create->format.type);
  1696. if (ret)
  1697. return ret;
  1698. CLEAR_AFTER_FIELD(create, format);
  1699. v4l_sanitize_format(&create->format);
  1700. ret = ops->vidioc_create_bufs(file, fh, create);
  1701. if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1702. create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1703. create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1704. return ret;
  1705. }
  1706. static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
  1707. struct file *file, void *fh, void *arg)
  1708. {
  1709. struct v4l2_buffer *b = arg;
  1710. int ret = check_fmt(file, b->type);
  1711. return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
  1712. }
  1713. static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
  1714. struct file *file, void *fh, void *arg)
  1715. {
  1716. struct v4l2_streamparm *p = arg;
  1717. v4l2_std_id std;
  1718. int ret = check_fmt(file, p->type);
  1719. if (ret)
  1720. return ret;
  1721. if (ops->vidioc_g_parm)
  1722. return ops->vidioc_g_parm(file, fh, p);
  1723. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1724. p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1725. return -EINVAL;
  1726. p->parm.capture.readbuffers = 2;
  1727. ret = ops->vidioc_g_std(file, fh, &std);
  1728. if (ret == 0)
  1729. v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
  1730. return ret;
  1731. }
  1732. static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
  1733. struct file *file, void *fh, void *arg)
  1734. {
  1735. struct v4l2_streamparm *p = arg;
  1736. int ret = check_fmt(file, p->type);
  1737. if (ret)
  1738. return ret;
  1739. /* Note: extendedmode is never used in drivers */
  1740. if (V4L2_TYPE_IS_OUTPUT(p->type)) {
  1741. memset(p->parm.output.reserved, 0,
  1742. sizeof(p->parm.output.reserved));
  1743. p->parm.output.extendedmode = 0;
  1744. p->parm.output.outputmode &= V4L2_MODE_HIGHQUALITY;
  1745. } else {
  1746. memset(p->parm.capture.reserved, 0,
  1747. sizeof(p->parm.capture.reserved));
  1748. p->parm.capture.extendedmode = 0;
  1749. p->parm.capture.capturemode &= V4L2_MODE_HIGHQUALITY;
  1750. }
  1751. return ops->vidioc_s_parm(file, fh, p);
  1752. }
  1753. static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
  1754. struct file *file, void *fh, void *arg)
  1755. {
  1756. struct video_device *vfd = video_devdata(file);
  1757. struct v4l2_queryctrl *p = arg;
  1758. struct v4l2_fh *vfh =
  1759. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1760. if (vfh && vfh->ctrl_handler)
  1761. return v4l2_queryctrl(vfh->ctrl_handler, p);
  1762. if (vfd->ctrl_handler)
  1763. return v4l2_queryctrl(vfd->ctrl_handler, p);
  1764. if (ops->vidioc_queryctrl)
  1765. return ops->vidioc_queryctrl(file, fh, p);
  1766. return -ENOTTY;
  1767. }
  1768. static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
  1769. struct file *file, void *fh, void *arg)
  1770. {
  1771. struct video_device *vfd = video_devdata(file);
  1772. struct v4l2_query_ext_ctrl *p = arg;
  1773. struct v4l2_fh *vfh =
  1774. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1775. if (vfh && vfh->ctrl_handler)
  1776. return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
  1777. if (vfd->ctrl_handler)
  1778. return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
  1779. if (ops->vidioc_query_ext_ctrl)
  1780. return ops->vidioc_query_ext_ctrl(file, fh, p);
  1781. return -ENOTTY;
  1782. }
  1783. static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
  1784. struct file *file, void *fh, void *arg)
  1785. {
  1786. struct video_device *vfd = video_devdata(file);
  1787. struct v4l2_querymenu *p = arg;
  1788. struct v4l2_fh *vfh =
  1789. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1790. if (vfh && vfh->ctrl_handler)
  1791. return v4l2_querymenu(vfh->ctrl_handler, p);
  1792. if (vfd->ctrl_handler)
  1793. return v4l2_querymenu(vfd->ctrl_handler, p);
  1794. if (ops->vidioc_querymenu)
  1795. return ops->vidioc_querymenu(file, fh, p);
  1796. return -ENOTTY;
  1797. }
  1798. static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
  1799. struct file *file, void *fh, void *arg)
  1800. {
  1801. struct video_device *vfd = video_devdata(file);
  1802. struct v4l2_control *p = arg;
  1803. struct v4l2_fh *vfh =
  1804. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1805. struct v4l2_ext_controls ctrls;
  1806. struct v4l2_ext_control ctrl;
  1807. if (vfh && vfh->ctrl_handler)
  1808. return v4l2_g_ctrl(vfh->ctrl_handler, p);
  1809. if (vfd->ctrl_handler)
  1810. return v4l2_g_ctrl(vfd->ctrl_handler, p);
  1811. if (ops->vidioc_g_ctrl)
  1812. return ops->vidioc_g_ctrl(file, fh, p);
  1813. if (ops->vidioc_g_ext_ctrls == NULL)
  1814. return -ENOTTY;
  1815. ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
  1816. ctrls.count = 1;
  1817. ctrls.controls = &ctrl;
  1818. ctrl.id = p->id;
  1819. ctrl.value = p->value;
  1820. if (check_ext_ctrls(&ctrls, 1)) {
  1821. int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
  1822. if (ret == 0)
  1823. p->value = ctrl.value;
  1824. return ret;
  1825. }
  1826. return -EINVAL;
  1827. }
  1828. static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
  1829. struct file *file, void *fh, void *arg)
  1830. {
  1831. struct video_device *vfd = video_devdata(file);
  1832. struct v4l2_control *p = arg;
  1833. struct v4l2_fh *vfh =
  1834. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1835. struct v4l2_ext_controls ctrls;
  1836. struct v4l2_ext_control ctrl;
  1837. if (vfh && vfh->ctrl_handler)
  1838. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
  1839. if (vfd->ctrl_handler)
  1840. return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
  1841. if (ops->vidioc_s_ctrl)
  1842. return ops->vidioc_s_ctrl(file, fh, p);
  1843. if (ops->vidioc_s_ext_ctrls == NULL)
  1844. return -ENOTTY;
  1845. ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
  1846. ctrls.count = 1;
  1847. ctrls.controls = &ctrl;
  1848. ctrl.id = p->id;
  1849. ctrl.value = p->value;
  1850. if (check_ext_ctrls(&ctrls, 1))
  1851. return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
  1852. return -EINVAL;
  1853. }
  1854. static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1855. struct file *file, void *fh, void *arg)
  1856. {
  1857. struct video_device *vfd = video_devdata(file);
  1858. struct v4l2_ext_controls *p = arg;
  1859. struct v4l2_fh *vfh =
  1860. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1861. p->error_idx = p->count;
  1862. if (vfh && vfh->ctrl_handler)
  1863. return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
  1864. if (vfd->ctrl_handler)
  1865. return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
  1866. if (ops->vidioc_g_ext_ctrls == NULL)
  1867. return -ENOTTY;
  1868. return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
  1869. -EINVAL;
  1870. }
  1871. static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1872. struct file *file, void *fh, void *arg)
  1873. {
  1874. struct video_device *vfd = video_devdata(file);
  1875. struct v4l2_ext_controls *p = arg;
  1876. struct v4l2_fh *vfh =
  1877. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1878. p->error_idx = p->count;
  1879. if (vfh && vfh->ctrl_handler)
  1880. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
  1881. if (vfd->ctrl_handler)
  1882. return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
  1883. if (ops->vidioc_s_ext_ctrls == NULL)
  1884. return -ENOTTY;
  1885. return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
  1886. -EINVAL;
  1887. }
  1888. static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1889. struct file *file, void *fh, void *arg)
  1890. {
  1891. struct video_device *vfd = video_devdata(file);
  1892. struct v4l2_ext_controls *p = arg;
  1893. struct v4l2_fh *vfh =
  1894. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1895. p->error_idx = p->count;
  1896. if (vfh && vfh->ctrl_handler)
  1897. return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
  1898. if (vfd->ctrl_handler)
  1899. return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
  1900. if (ops->vidioc_try_ext_ctrls == NULL)
  1901. return -ENOTTY;
  1902. return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
  1903. -EINVAL;
  1904. }
  1905. /*
  1906. * The selection API specified originally that the _MPLANE buffer types
  1907. * shouldn't be used. The reasons for this are lost in the mists of time
  1908. * (or just really crappy memories). Regardless, this is really annoying
  1909. * for userspace. So to keep things simple we map _MPLANE buffer types
  1910. * to their 'regular' counterparts before calling the driver. And we
  1911. * restore it afterwards. This way applications can use either buffer
  1912. * type and drivers don't need to check for both.
  1913. */
  1914. static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
  1915. struct file *file, void *fh, void *arg)
  1916. {
  1917. struct v4l2_selection *p = arg;
  1918. u32 old_type = p->type;
  1919. int ret;
  1920. if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1921. p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1922. else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1923. p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1924. ret = ops->vidioc_g_selection(file, fh, p);
  1925. p->type = old_type;
  1926. return ret;
  1927. }
  1928. static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
  1929. struct file *file, void *fh, void *arg)
  1930. {
  1931. struct v4l2_selection *p = arg;
  1932. u32 old_type = p->type;
  1933. int ret;
  1934. if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1935. p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1936. else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1937. p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1938. ret = ops->vidioc_s_selection(file, fh, p);
  1939. p->type = old_type;
  1940. return ret;
  1941. }
  1942. static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
  1943. struct file *file, void *fh, void *arg)
  1944. {
  1945. struct v4l2_crop *p = arg;
  1946. struct v4l2_selection s = {
  1947. .type = p->type,
  1948. };
  1949. int ret;
  1950. if (ops->vidioc_g_crop)
  1951. return ops->vidioc_g_crop(file, fh, p);
  1952. /* simulate capture crop using selection api */
  1953. /* crop means compose for output devices */
  1954. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1955. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1956. else
  1957. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1958. ret = v4l_g_selection(ops, file, fh, &s);
  1959. /* copying results to old structure on success */
  1960. if (!ret)
  1961. p->c = s.r;
  1962. return ret;
  1963. }
  1964. static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
  1965. struct file *file, void *fh, void *arg)
  1966. {
  1967. struct v4l2_crop *p = arg;
  1968. struct v4l2_selection s = {
  1969. .type = p->type,
  1970. .r = p->c,
  1971. };
  1972. if (ops->vidioc_s_crop)
  1973. return ops->vidioc_s_crop(file, fh, p);
  1974. /* simulate capture crop using selection api */
  1975. /* crop means compose for output devices */
  1976. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1977. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1978. else
  1979. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1980. return v4l_s_selection(ops, file, fh, &s);
  1981. }
  1982. static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
  1983. struct file *file, void *fh, void *arg)
  1984. {
  1985. struct v4l2_cropcap *p = arg;
  1986. struct v4l2_selection s = { .type = p->type };
  1987. int ret = 0;
  1988. /* setting trivial pixelaspect */
  1989. p->pixelaspect.numerator = 1;
  1990. p->pixelaspect.denominator = 1;
  1991. /*
  1992. * The determine_valid_ioctls() call already should ensure
  1993. * that this can never happen, but just in case...
  1994. */
  1995. if (WARN_ON(!ops->vidioc_cropcap && !ops->vidioc_g_selection))
  1996. return -ENOTTY;
  1997. if (ops->vidioc_cropcap)
  1998. ret = ops->vidioc_cropcap(file, fh, p);
  1999. if (!ops->vidioc_g_selection)
  2000. return ret;
  2001. /*
  2002. * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
  2003. * square pixel aspect ratio in that case.
  2004. */
  2005. if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
  2006. return ret;
  2007. /* Use g_selection() to fill in the bounds and defrect rectangles */
  2008. /* obtaining bounds */
  2009. if (V4L2_TYPE_IS_OUTPUT(p->type))
  2010. s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
  2011. else
  2012. s.target = V4L2_SEL_TGT_CROP_BOUNDS;
  2013. ret = v4l_g_selection(ops, file, fh, &s);
  2014. if (ret)
  2015. return ret;
  2016. p->bounds = s.r;
  2017. /* obtaining defrect */
  2018. if (V4L2_TYPE_IS_OUTPUT(p->type))
  2019. s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
  2020. else
  2021. s.target = V4L2_SEL_TGT_CROP_DEFAULT;
  2022. ret = v4l_g_selection(ops, file, fh, &s);
  2023. if (ret)
  2024. return ret;
  2025. p->defrect = s.r;
  2026. return 0;
  2027. }
  2028. static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
  2029. struct file *file, void *fh, void *arg)
  2030. {
  2031. struct video_device *vfd = video_devdata(file);
  2032. int ret;
  2033. if (vfd->v4l2_dev)
  2034. pr_info("%s: ================= START STATUS =================\n",
  2035. vfd->v4l2_dev->name);
  2036. ret = ops->vidioc_log_status(file, fh);
  2037. if (vfd->v4l2_dev)
  2038. pr_info("%s: ================== END STATUS ==================\n",
  2039. vfd->v4l2_dev->name);
  2040. return ret;
  2041. }
  2042. static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
  2043. struct file *file, void *fh, void *arg)
  2044. {
  2045. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2046. struct v4l2_dbg_register *p = arg;
  2047. struct video_device *vfd = video_devdata(file);
  2048. struct v4l2_subdev *sd;
  2049. int idx = 0;
  2050. if (!capable(CAP_SYS_ADMIN))
  2051. return -EPERM;
  2052. if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
  2053. if (vfd->v4l2_dev == NULL)
  2054. return -EINVAL;
  2055. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
  2056. if (p->match.addr == idx++)
  2057. return v4l2_subdev_call(sd, core, g_register, p);
  2058. return -EINVAL;
  2059. }
  2060. if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
  2061. (ops->vidioc_g_chip_info || p->match.addr == 0))
  2062. return ops->vidioc_g_register(file, fh, p);
  2063. return -EINVAL;
  2064. #else
  2065. return -ENOTTY;
  2066. #endif
  2067. }
  2068. static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
  2069. struct file *file, void *fh, void *arg)
  2070. {
  2071. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2072. const struct v4l2_dbg_register *p = arg;
  2073. struct video_device *vfd = video_devdata(file);
  2074. struct v4l2_subdev *sd;
  2075. int idx = 0;
  2076. if (!capable(CAP_SYS_ADMIN))
  2077. return -EPERM;
  2078. if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
  2079. if (vfd->v4l2_dev == NULL)
  2080. return -EINVAL;
  2081. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
  2082. if (p->match.addr == idx++)
  2083. return v4l2_subdev_call(sd, core, s_register, p);
  2084. return -EINVAL;
  2085. }
  2086. if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
  2087. (ops->vidioc_g_chip_info || p->match.addr == 0))
  2088. return ops->vidioc_s_register(file, fh, p);
  2089. return -EINVAL;
  2090. #else
  2091. return -ENOTTY;
  2092. #endif
  2093. }
  2094. static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
  2095. struct file *file, void *fh, void *arg)
  2096. {
  2097. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2098. struct video_device *vfd = video_devdata(file);
  2099. struct v4l2_dbg_chip_info *p = arg;
  2100. struct v4l2_subdev *sd;
  2101. int idx = 0;
  2102. switch (p->match.type) {
  2103. case V4L2_CHIP_MATCH_BRIDGE:
  2104. if (ops->vidioc_s_register)
  2105. p->flags |= V4L2_CHIP_FL_WRITABLE;
  2106. if (ops->vidioc_g_register)
  2107. p->flags |= V4L2_CHIP_FL_READABLE;
  2108. strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
  2109. if (ops->vidioc_g_chip_info)
  2110. return ops->vidioc_g_chip_info(file, fh, arg);
  2111. if (p->match.addr)
  2112. return -EINVAL;
  2113. return 0;
  2114. case V4L2_CHIP_MATCH_SUBDEV:
  2115. if (vfd->v4l2_dev == NULL)
  2116. break;
  2117. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
  2118. if (p->match.addr != idx++)
  2119. continue;
  2120. if (sd->ops->core && sd->ops->core->s_register)
  2121. p->flags |= V4L2_CHIP_FL_WRITABLE;
  2122. if (sd->ops->core && sd->ops->core->g_register)
  2123. p->flags |= V4L2_CHIP_FL_READABLE;
  2124. strlcpy(p->name, sd->name, sizeof(p->name));
  2125. return 0;
  2126. }
  2127. break;
  2128. }
  2129. return -EINVAL;
  2130. #else
  2131. return -ENOTTY;
  2132. #endif
  2133. }
  2134. static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
  2135. struct file *file, void *fh, void *arg)
  2136. {
  2137. return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
  2138. }
  2139. static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
  2140. struct file *file, void *fh, void *arg)
  2141. {
  2142. return ops->vidioc_subscribe_event(fh, arg);
  2143. }
  2144. static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
  2145. struct file *file, void *fh, void *arg)
  2146. {
  2147. return ops->vidioc_unsubscribe_event(fh, arg);
  2148. }
  2149. static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
  2150. struct file *file, void *fh, void *arg)
  2151. {
  2152. struct v4l2_sliced_vbi_cap *p = arg;
  2153. int ret = check_fmt(file, p->type);
  2154. if (ret)
  2155. return ret;
  2156. /* Clear up to type, everything after type is zeroed already */
  2157. memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
  2158. return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
  2159. }
  2160. static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
  2161. struct file *file, void *fh, void *arg)
  2162. {
  2163. struct video_device *vfd = video_devdata(file);
  2164. struct v4l2_frequency_band *p = arg;
  2165. enum v4l2_tuner_type type;
  2166. int err;
  2167. if (vfd->vfl_type == VFL_TYPE_SDR) {
  2168. if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
  2169. return -EINVAL;
  2170. type = p->type;
  2171. } else {
  2172. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  2173. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  2174. if (type != p->type)
  2175. return -EINVAL;
  2176. }
  2177. if (ops->vidioc_enum_freq_bands) {
  2178. err = ops->vidioc_enum_freq_bands(file, fh, p);
  2179. if (err != -ENOTTY)
  2180. return err;
  2181. }
  2182. if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
  2183. struct v4l2_tuner t = {
  2184. .index = p->tuner,
  2185. .type = type,
  2186. };
  2187. if (p->index)
  2188. return -EINVAL;
  2189. err = ops->vidioc_g_tuner(file, fh, &t);
  2190. if (err)
  2191. return err;
  2192. p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
  2193. p->rangelow = t.rangelow;
  2194. p->rangehigh = t.rangehigh;
  2195. p->modulation = (type == V4L2_TUNER_RADIO) ?
  2196. V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
  2197. return 0;
  2198. }
  2199. if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
  2200. struct v4l2_modulator m = {
  2201. .index = p->tuner,
  2202. };
  2203. if (type != V4L2_TUNER_RADIO)
  2204. return -EINVAL;
  2205. if (p->index)
  2206. return -EINVAL;
  2207. err = ops->vidioc_g_modulator(file, fh, &m);
  2208. if (err)
  2209. return err;
  2210. p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
  2211. p->rangelow = m.rangelow;
  2212. p->rangehigh = m.rangehigh;
  2213. p->modulation = (type == V4L2_TUNER_RADIO) ?
  2214. V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
  2215. return 0;
  2216. }
  2217. return -ENOTTY;
  2218. }
  2219. struct v4l2_ioctl_info {
  2220. unsigned int ioctl;
  2221. u32 flags;
  2222. const char * const name;
  2223. int (*func)(const struct v4l2_ioctl_ops *ops, struct file *file,
  2224. void *fh, void *p);
  2225. void (*debug)(const void *arg, bool write_only);
  2226. };
  2227. /* This control needs a priority check */
  2228. #define INFO_FL_PRIO (1 << 0)
  2229. /* This control can be valid if the filehandle passes a control handler. */
  2230. #define INFO_FL_CTRL (1 << 1)
  2231. /* Queuing ioctl */
  2232. #define INFO_FL_QUEUE (1 << 2)
  2233. /* Always copy back result, even on error */
  2234. #define INFO_FL_ALWAYS_COPY (1 << 3)
  2235. /* Zero struct from after the field to the end */
  2236. #define INFO_FL_CLEAR(v4l2_struct, field) \
  2237. ((offsetof(struct v4l2_struct, field) + \
  2238. sizeof(((struct v4l2_struct *)0)->field)) << 16)
  2239. #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
  2240. #define DEFINE_V4L_STUB_FUNC(_vidioc) \
  2241. static int v4l_stub_ ## _vidioc( \
  2242. const struct v4l2_ioctl_ops *ops, \
  2243. struct file *file, void *fh, void *p) \
  2244. { \
  2245. return ops->vidioc_ ## _vidioc(file, fh, p); \
  2246. }
  2247. #define IOCTL_INFO(_ioctl, _func, _debug, _flags) \
  2248. [_IOC_NR(_ioctl)] = { \
  2249. .ioctl = _ioctl, \
  2250. .flags = _flags, \
  2251. .name = #_ioctl, \
  2252. .func = _func, \
  2253. .debug = _debug, \
  2254. }
  2255. DEFINE_V4L_STUB_FUNC(g_fbuf)
  2256. DEFINE_V4L_STUB_FUNC(s_fbuf)
  2257. DEFINE_V4L_STUB_FUNC(expbuf)
  2258. DEFINE_V4L_STUB_FUNC(g_std)
  2259. DEFINE_V4L_STUB_FUNC(g_audio)
  2260. DEFINE_V4L_STUB_FUNC(s_audio)
  2261. DEFINE_V4L_STUB_FUNC(g_input)
  2262. DEFINE_V4L_STUB_FUNC(g_edid)
  2263. DEFINE_V4L_STUB_FUNC(s_edid)
  2264. DEFINE_V4L_STUB_FUNC(g_output)
  2265. DEFINE_V4L_STUB_FUNC(g_audout)
  2266. DEFINE_V4L_STUB_FUNC(s_audout)
  2267. DEFINE_V4L_STUB_FUNC(g_jpegcomp)
  2268. DEFINE_V4L_STUB_FUNC(s_jpegcomp)
  2269. DEFINE_V4L_STUB_FUNC(enumaudio)
  2270. DEFINE_V4L_STUB_FUNC(enumaudout)
  2271. DEFINE_V4L_STUB_FUNC(enum_framesizes)
  2272. DEFINE_V4L_STUB_FUNC(enum_frameintervals)
  2273. DEFINE_V4L_STUB_FUNC(g_enc_index)
  2274. DEFINE_V4L_STUB_FUNC(encoder_cmd)
  2275. DEFINE_V4L_STUB_FUNC(try_encoder_cmd)
  2276. DEFINE_V4L_STUB_FUNC(decoder_cmd)
  2277. DEFINE_V4L_STUB_FUNC(try_decoder_cmd)
  2278. DEFINE_V4L_STUB_FUNC(s_dv_timings)
  2279. DEFINE_V4L_STUB_FUNC(g_dv_timings)
  2280. DEFINE_V4L_STUB_FUNC(enum_dv_timings)
  2281. DEFINE_V4L_STUB_FUNC(query_dv_timings)
  2282. DEFINE_V4L_STUB_FUNC(dv_timings_cap)
  2283. static struct v4l2_ioctl_info v4l2_ioctls[] = {
  2284. IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
  2285. IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
  2286. IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
  2287. IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
  2288. IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
  2289. IOCTL_INFO(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
  2290. IOCTL_INFO(VIDIOC_G_FBUF, v4l_stub_g_fbuf, v4l_print_framebuffer, 0),
  2291. IOCTL_INFO(VIDIOC_S_FBUF, v4l_stub_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
  2292. IOCTL_INFO(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
  2293. IOCTL_INFO(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
  2294. IOCTL_INFO(VIDIOC_EXPBUF, v4l_stub_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
  2295. IOCTL_INFO(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
  2296. IOCTL_INFO(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
  2297. IOCTL_INFO(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
  2298. IOCTL_INFO(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
  2299. IOCTL_INFO(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
  2300. IOCTL_INFO(VIDIOC_G_STD, v4l_stub_g_std, v4l_print_std, 0),
  2301. IOCTL_INFO(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
  2302. IOCTL_INFO(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
  2303. IOCTL_INFO(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
  2304. IOCTL_INFO(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
  2305. IOCTL_INFO(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
  2306. IOCTL_INFO(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
  2307. IOCTL_INFO(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
  2308. IOCTL_INFO(VIDIOC_G_AUDIO, v4l_stub_g_audio, v4l_print_audio, 0),
  2309. IOCTL_INFO(VIDIOC_S_AUDIO, v4l_stub_s_audio, v4l_print_audio, INFO_FL_PRIO),
  2310. IOCTL_INFO(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
  2311. IOCTL_INFO(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
  2312. IOCTL_INFO(VIDIOC_G_INPUT, v4l_stub_g_input, v4l_print_u32, 0),
  2313. IOCTL_INFO(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
  2314. IOCTL_INFO(VIDIOC_G_EDID, v4l_stub_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
  2315. IOCTL_INFO(VIDIOC_S_EDID, v4l_stub_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
  2316. IOCTL_INFO(VIDIOC_G_OUTPUT, v4l_stub_g_output, v4l_print_u32, 0),
  2317. IOCTL_INFO(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
  2318. IOCTL_INFO(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
  2319. IOCTL_INFO(VIDIOC_G_AUDOUT, v4l_stub_g_audout, v4l_print_audioout, 0),
  2320. IOCTL_INFO(VIDIOC_S_AUDOUT, v4l_stub_s_audout, v4l_print_audioout, INFO_FL_PRIO),
  2321. IOCTL_INFO(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
  2322. IOCTL_INFO(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
  2323. IOCTL_INFO(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
  2324. IOCTL_INFO(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
  2325. IOCTL_INFO(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
  2326. IOCTL_INFO(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
  2327. IOCTL_INFO(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
  2328. IOCTL_INFO(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
  2329. IOCTL_INFO(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
  2330. IOCTL_INFO(VIDIOC_G_JPEGCOMP, v4l_stub_g_jpegcomp, v4l_print_jpegcompression, 0),
  2331. IOCTL_INFO(VIDIOC_S_JPEGCOMP, v4l_stub_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
  2332. IOCTL_INFO(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
  2333. IOCTL_INFO(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
  2334. IOCTL_INFO(VIDIOC_ENUMAUDIO, v4l_stub_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
  2335. IOCTL_INFO(VIDIOC_ENUMAUDOUT, v4l_stub_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
  2336. IOCTL_INFO(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
  2337. IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
  2338. IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
  2339. IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
  2340. IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  2341. IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
  2342. IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  2343. IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
  2344. IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
  2345. IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
  2346. IOCTL_INFO(VIDIOC_ENCODER_CMD, v4l_stub_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  2347. IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, v4l_stub_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  2348. IOCTL_INFO(VIDIOC_DECODER_CMD, v4l_stub_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
  2349. IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, v4l_stub_try_decoder_cmd, v4l_print_decoder_cmd, 0),
  2350. IOCTL_INFO(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
  2351. IOCTL_INFO(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
  2352. IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
  2353. IOCTL_INFO(VIDIOC_S_DV_TIMINGS, v4l_stub_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_dv_timings, bt.flags)),
  2354. IOCTL_INFO(VIDIOC_G_DV_TIMINGS, v4l_stub_g_dv_timings, v4l_print_dv_timings, 0),
  2355. IOCTL_INFO(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
  2356. IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
  2357. IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
  2358. IOCTL_INFO(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
  2359. IOCTL_INFO(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
  2360. IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, v4l_stub_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
  2361. IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, v4l_stub_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
  2362. IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, v4l_stub_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, pad)),
  2363. IOCTL_INFO(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
  2364. IOCTL_INFO(VIDIOC_DBG_G_CHIP_INFO, v4l_dbg_g_chip_info, v4l_print_dbg_chip_info, INFO_FL_CLEAR(v4l2_dbg_chip_info, match)),
  2365. IOCTL_INFO(VIDIOC_QUERY_EXT_CTRL, v4l_query_ext_ctrl, v4l_print_query_ext_ctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_query_ext_ctrl, id)),
  2366. };
  2367. #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
  2368. bool v4l2_is_known_ioctl(unsigned int cmd)
  2369. {
  2370. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  2371. return false;
  2372. return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
  2373. }
  2374. struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
  2375. {
  2376. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  2377. return vdev->lock;
  2378. if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
  2379. return NULL;
  2380. if (vdev->queue && vdev->queue->lock &&
  2381. (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
  2382. return vdev->queue->lock;
  2383. return vdev->lock;
  2384. }
  2385. /* Common ioctl debug function. This function can be used by
  2386. external ioctl messages as well as internal V4L ioctl */
  2387. void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
  2388. {
  2389. const char *dir, *type;
  2390. if (prefix)
  2391. printk(KERN_DEBUG "%s: ", prefix);
  2392. switch (_IOC_TYPE(cmd)) {
  2393. case 'd':
  2394. type = "v4l2_int";
  2395. break;
  2396. case 'V':
  2397. if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
  2398. type = "v4l2";
  2399. break;
  2400. }
  2401. pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
  2402. return;
  2403. default:
  2404. type = "unknown";
  2405. break;
  2406. }
  2407. switch (_IOC_DIR(cmd)) {
  2408. case _IOC_NONE: dir = "--"; break;
  2409. case _IOC_READ: dir = "r-"; break;
  2410. case _IOC_WRITE: dir = "-w"; break;
  2411. case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
  2412. default: dir = "*ERR*"; break;
  2413. }
  2414. pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
  2415. type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
  2416. }
  2417. EXPORT_SYMBOL(v4l_printk_ioctl);
  2418. static long __video_do_ioctl(struct file *file,
  2419. unsigned int cmd, void *arg)
  2420. {
  2421. struct video_device *vfd = video_devdata(file);
  2422. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  2423. bool write_only = false;
  2424. struct v4l2_ioctl_info default_info;
  2425. const struct v4l2_ioctl_info *info;
  2426. void *fh = file->private_data;
  2427. struct v4l2_fh *vfh = NULL;
  2428. int dev_debug = vfd->dev_debug;
  2429. long ret = -ENOTTY;
  2430. if (ops == NULL) {
  2431. pr_warn("%s: has no ioctl_ops.\n",
  2432. video_device_node_name(vfd));
  2433. return ret;
  2434. }
  2435. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
  2436. vfh = file->private_data;
  2437. if (v4l2_is_known_ioctl(cmd)) {
  2438. info = &v4l2_ioctls[_IOC_NR(cmd)];
  2439. if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
  2440. !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
  2441. goto done;
  2442. if (vfh && (info->flags & INFO_FL_PRIO)) {
  2443. ret = v4l2_prio_check(vfd->prio, vfh->prio);
  2444. if (ret)
  2445. goto done;
  2446. }
  2447. } else {
  2448. default_info.ioctl = cmd;
  2449. default_info.flags = 0;
  2450. default_info.debug = v4l_print_default;
  2451. info = &default_info;
  2452. }
  2453. write_only = _IOC_DIR(cmd) == _IOC_WRITE;
  2454. if (info != &default_info) {
  2455. ret = info->func(ops, file, fh, arg);
  2456. } else if (!ops->vidioc_default) {
  2457. ret = -ENOTTY;
  2458. } else {
  2459. ret = ops->vidioc_default(file, fh,
  2460. vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
  2461. cmd, arg);
  2462. }
  2463. done:
  2464. if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
  2465. if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
  2466. (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
  2467. return ret;
  2468. v4l_printk_ioctl(video_device_node_name(vfd), cmd);
  2469. if (ret < 0)
  2470. pr_cont(": error %ld", ret);
  2471. if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
  2472. pr_cont("\n");
  2473. else if (_IOC_DIR(cmd) == _IOC_NONE)
  2474. info->debug(arg, write_only);
  2475. else {
  2476. pr_cont(": ");
  2477. info->debug(arg, write_only);
  2478. }
  2479. }
  2480. return ret;
  2481. }
  2482. static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
  2483. void __user **user_ptr, void ***kernel_ptr)
  2484. {
  2485. int ret = 0;
  2486. switch (cmd) {
  2487. case VIDIOC_PREPARE_BUF:
  2488. case VIDIOC_QUERYBUF:
  2489. case VIDIOC_QBUF:
  2490. case VIDIOC_DQBUF: {
  2491. struct v4l2_buffer *buf = parg;
  2492. if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
  2493. if (buf->length > VIDEO_MAX_PLANES) {
  2494. ret = -EINVAL;
  2495. break;
  2496. }
  2497. *user_ptr = (void __user *)buf->m.planes;
  2498. *kernel_ptr = (void **)&buf->m.planes;
  2499. *array_size = sizeof(struct v4l2_plane) * buf->length;
  2500. ret = 1;
  2501. }
  2502. break;
  2503. }
  2504. case VIDIOC_G_EDID:
  2505. case VIDIOC_S_EDID: {
  2506. struct v4l2_edid *edid = parg;
  2507. if (edid->blocks) {
  2508. if (edid->blocks > 256) {
  2509. ret = -EINVAL;
  2510. break;
  2511. }
  2512. *user_ptr = (void __user *)edid->edid;
  2513. *kernel_ptr = (void **)&edid->edid;
  2514. *array_size = edid->blocks * 128;
  2515. ret = 1;
  2516. }
  2517. break;
  2518. }
  2519. case VIDIOC_S_EXT_CTRLS:
  2520. case VIDIOC_G_EXT_CTRLS:
  2521. case VIDIOC_TRY_EXT_CTRLS: {
  2522. struct v4l2_ext_controls *ctrls = parg;
  2523. if (ctrls->count != 0) {
  2524. if (ctrls->count > V4L2_CID_MAX_CTRLS) {
  2525. ret = -EINVAL;
  2526. break;
  2527. }
  2528. *user_ptr = (void __user *)ctrls->controls;
  2529. *kernel_ptr = (void **)&ctrls->controls;
  2530. *array_size = sizeof(struct v4l2_ext_control)
  2531. * ctrls->count;
  2532. ret = 1;
  2533. }
  2534. break;
  2535. }
  2536. }
  2537. return ret;
  2538. }
  2539. long
  2540. video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
  2541. v4l2_kioctl func)
  2542. {
  2543. char sbuf[128];
  2544. void *mbuf = NULL;
  2545. void *parg = (void *)arg;
  2546. long err = -EINVAL;
  2547. bool has_array_args;
  2548. bool always_copy = false;
  2549. size_t array_size = 0;
  2550. void __user *user_ptr = NULL;
  2551. void **kernel_ptr = NULL;
  2552. const size_t ioc_size = _IOC_SIZE(cmd);
  2553. /* Copy arguments into temp kernel buffer */
  2554. if (_IOC_DIR(cmd) != _IOC_NONE) {
  2555. if (ioc_size <= sizeof(sbuf)) {
  2556. parg = sbuf;
  2557. } else {
  2558. /* too big to allocate from stack */
  2559. mbuf = kvmalloc(ioc_size, GFP_KERNEL);
  2560. if (NULL == mbuf)
  2561. return -ENOMEM;
  2562. parg = mbuf;
  2563. }
  2564. err = -EFAULT;
  2565. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  2566. unsigned int n = ioc_size;
  2567. /*
  2568. * In some cases, only a few fields are used as input,
  2569. * i.e. when the app sets "index" and then the driver
  2570. * fills in the rest of the structure for the thing
  2571. * with that index. We only need to copy up the first
  2572. * non-input field.
  2573. */
  2574. if (v4l2_is_known_ioctl(cmd)) {
  2575. u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
  2576. if (flags & INFO_FL_CLEAR_MASK)
  2577. n = (flags & INFO_FL_CLEAR_MASK) >> 16;
  2578. always_copy = flags & INFO_FL_ALWAYS_COPY;
  2579. }
  2580. if (copy_from_user(parg, (void __user *)arg, n))
  2581. goto out;
  2582. /* zero out anything we don't copy from userspace */
  2583. if (n < ioc_size)
  2584. memset((u8 *)parg + n, 0, ioc_size - n);
  2585. } else {
  2586. /* read-only ioctl */
  2587. memset(parg, 0, ioc_size);
  2588. }
  2589. }
  2590. err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
  2591. if (err < 0)
  2592. goto out;
  2593. has_array_args = err;
  2594. if (has_array_args) {
  2595. /*
  2596. * When adding new types of array args, make sure that the
  2597. * parent argument to ioctl (which contains the pointer to the
  2598. * array) fits into sbuf (so that mbuf will still remain
  2599. * unused up to here).
  2600. */
  2601. mbuf = kvmalloc(array_size, GFP_KERNEL);
  2602. err = -ENOMEM;
  2603. if (NULL == mbuf)
  2604. goto out_array_args;
  2605. err = -EFAULT;
  2606. if (copy_from_user(mbuf, user_ptr, array_size))
  2607. goto out_array_args;
  2608. *kernel_ptr = mbuf;
  2609. }
  2610. /* Handles IOCTL */
  2611. err = func(file, cmd, parg);
  2612. if (err == -ENOTTY || err == -ENOIOCTLCMD) {
  2613. err = -ENOTTY;
  2614. goto out;
  2615. }
  2616. if (err == 0) {
  2617. if (cmd == VIDIOC_DQBUF)
  2618. trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
  2619. else if (cmd == VIDIOC_QBUF)
  2620. trace_v4l2_qbuf(video_devdata(file)->minor, parg);
  2621. }
  2622. if (has_array_args) {
  2623. *kernel_ptr = (void __force *)user_ptr;
  2624. if (copy_to_user(user_ptr, mbuf, array_size))
  2625. err = -EFAULT;
  2626. goto out_array_args;
  2627. }
  2628. /*
  2629. * Some ioctls can return an error, but still have valid
  2630. * results that must be returned.
  2631. */
  2632. if (err < 0 && !always_copy)
  2633. goto out;
  2634. out_array_args:
  2635. /* Copy results into user buffer */
  2636. switch (_IOC_DIR(cmd)) {
  2637. case _IOC_READ:
  2638. case (_IOC_WRITE | _IOC_READ):
  2639. if (copy_to_user((void __user *)arg, parg, ioc_size))
  2640. err = -EFAULT;
  2641. break;
  2642. }
  2643. out:
  2644. kvfree(mbuf);
  2645. return err;
  2646. }
  2647. EXPORT_SYMBOL(video_usercopy);
  2648. long video_ioctl2(struct file *file,
  2649. unsigned int cmd, unsigned long arg)
  2650. {
  2651. return video_usercopy(file, cmd, arg, __video_do_ioctl);
  2652. }
  2653. EXPORT_SYMBOL(video_ioctl2);