v4l2-ioctl.c 94 KB

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