v4l2-ioctl.c 97 KB

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