v4l2-ioctl.c 95 KB

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