v4l2-ioctl.c 98 KB

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