v4l2-ioctl.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  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-core.h>
  27. #define CREATE_TRACE_POINTS
  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. [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
  130. [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
  131. [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
  132. [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
  133. [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
  134. [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
  135. [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
  136. [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
  137. [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
  138. [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
  139. [V4L2_BUF_TYPE_SDR_CAPTURE] = "sdr-cap",
  140. };
  141. EXPORT_SYMBOL(v4l2_type_names);
  142. static const char *v4l2_memory_names[] = {
  143. [V4L2_MEMORY_MMAP] = "mmap",
  144. [V4L2_MEMORY_USERPTR] = "userptr",
  145. [V4L2_MEMORY_OVERLAY] = "overlay",
  146. [V4L2_MEMORY_DMABUF] = "dmabuf",
  147. };
  148. #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
  149. /* ------------------------------------------------------------------ */
  150. /* debug help functions */
  151. static void v4l_print_querycap(const void *arg, bool write_only)
  152. {
  153. const struct v4l2_capability *p = arg;
  154. pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, "
  155. "capabilities=0x%08x, device_caps=0x%08x\n",
  156. (int)sizeof(p->driver), p->driver,
  157. (int)sizeof(p->card), p->card,
  158. (int)sizeof(p->bus_info), p->bus_info,
  159. p->version, p->capabilities, p->device_caps);
  160. }
  161. static void v4l_print_enuminput(const void *arg, bool write_only)
  162. {
  163. const struct v4l2_input *p = arg;
  164. pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, "
  165. "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
  166. p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
  167. p->tuner, (unsigned long long)p->std, p->status,
  168. p->capabilities);
  169. }
  170. static void v4l_print_enumoutput(const void *arg, bool write_only)
  171. {
  172. const struct v4l2_output *p = arg;
  173. pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, "
  174. "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. unsigned i;
  219. pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
  220. switch (p->type) {
  221. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  222. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  223. pix = &p->fmt.pix;
  224. pr_cont(", width=%u, height=%u, "
  225. "pixelformat=%c%c%c%c, field=%s, "
  226. "bytesperline=%u, sizeimage=%u, colorspace=%d, "
  227. "flags=0x%x, ycbcr_enc=%u, quantization=%u\n",
  228. pix->width, pix->height,
  229. (pix->pixelformat & 0xff),
  230. (pix->pixelformat >> 8) & 0xff,
  231. (pix->pixelformat >> 16) & 0xff,
  232. (pix->pixelformat >> 24) & 0xff,
  233. prt_names(pix->field, v4l2_field_names),
  234. pix->bytesperline, pix->sizeimage,
  235. pix->colorspace, pix->flags, pix->ycbcr_enc,
  236. pix->quantization);
  237. break;
  238. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  239. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  240. mp = &p->fmt.pix_mp;
  241. pr_cont(", width=%u, height=%u, "
  242. "format=%c%c%c%c, field=%s, "
  243. "colorspace=%d, num_planes=%u, flags=0x%x, "
  244. "ycbcr_enc=%u, quantization=%u\n",
  245. mp->width, mp->height,
  246. (mp->pixelformat & 0xff),
  247. (mp->pixelformat >> 8) & 0xff,
  248. (mp->pixelformat >> 16) & 0xff,
  249. (mp->pixelformat >> 24) & 0xff,
  250. prt_names(mp->field, v4l2_field_names),
  251. mp->colorspace, mp->num_planes, mp->flags,
  252. mp->ycbcr_enc, mp->quantization);
  253. for (i = 0; i < mp->num_planes; i++)
  254. printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
  255. mp->plane_fmt[i].bytesperline,
  256. mp->plane_fmt[i].sizeimage);
  257. break;
  258. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  259. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  260. win = &p->fmt.win;
  261. /* Note: we can't print the clip list here since the clips
  262. * pointer is a userspace pointer, not a kernelspace
  263. * pointer. */
  264. 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",
  265. win->w.width, win->w.height, win->w.left, win->w.top,
  266. prt_names(win->field, v4l2_field_names),
  267. win->chromakey, win->clipcount, win->clips,
  268. win->bitmap, win->global_alpha);
  269. break;
  270. case V4L2_BUF_TYPE_VBI_CAPTURE:
  271. case V4L2_BUF_TYPE_VBI_OUTPUT:
  272. vbi = &p->fmt.vbi;
  273. pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
  274. "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
  275. vbi->sampling_rate, vbi->offset,
  276. vbi->samples_per_line,
  277. (vbi->sample_format & 0xff),
  278. (vbi->sample_format >> 8) & 0xff,
  279. (vbi->sample_format >> 16) & 0xff,
  280. (vbi->sample_format >> 24) & 0xff,
  281. vbi->start[0], vbi->start[1],
  282. vbi->count[0], vbi->count[1]);
  283. break;
  284. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  285. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  286. sliced = &p->fmt.sliced;
  287. pr_cont(", service_set=0x%08x, io_size=%d\n",
  288. sliced->service_set, sliced->io_size);
  289. for (i = 0; i < 24; i++)
  290. printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
  291. sliced->service_lines[0][i],
  292. sliced->service_lines[1][i]);
  293. break;
  294. case V4L2_BUF_TYPE_SDR_CAPTURE:
  295. sdr = &p->fmt.sdr;
  296. pr_cont(", pixelformat=%c%c%c%c\n",
  297. (sdr->pixelformat >> 0) & 0xff,
  298. (sdr->pixelformat >> 8) & 0xff,
  299. (sdr->pixelformat >> 16) & 0xff,
  300. (sdr->pixelformat >> 24) & 0xff);
  301. break;
  302. }
  303. }
  304. static void v4l_print_framebuffer(const void *arg, bool write_only)
  305. {
  306. const struct v4l2_framebuffer *p = arg;
  307. pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
  308. "height=%u, pixelformat=%c%c%c%c, "
  309. "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
  310. p->capability, p->flags, p->base,
  311. p->fmt.width, p->fmt.height,
  312. (p->fmt.pixelformat & 0xff),
  313. (p->fmt.pixelformat >> 8) & 0xff,
  314. (p->fmt.pixelformat >> 16) & 0xff,
  315. (p->fmt.pixelformat >> 24) & 0xff,
  316. p->fmt.bytesperline, p->fmt.sizeimage,
  317. p->fmt.colorspace);
  318. }
  319. static void v4l_print_buftype(const void *arg, bool write_only)
  320. {
  321. pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
  322. }
  323. static void v4l_print_modulator(const void *arg, bool write_only)
  324. {
  325. const struct v4l2_modulator *p = arg;
  326. if (write_only)
  327. pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
  328. else
  329. pr_cont("index=%u, name=%.*s, capability=0x%x, "
  330. "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
  331. p->index, (int)sizeof(p->name), p->name, p->capability,
  332. p->rangelow, p->rangehigh, p->txsubchans);
  333. }
  334. static void v4l_print_tuner(const void *arg, bool write_only)
  335. {
  336. const struct v4l2_tuner *p = arg;
  337. if (write_only)
  338. pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
  339. else
  340. pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, "
  341. "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
  342. "rxsubchans=0x%x, audmode=%u\n",
  343. p->index, (int)sizeof(p->name), p->name, p->type,
  344. p->capability, p->rangelow,
  345. p->rangehigh, p->signal, p->afc,
  346. p->rxsubchans, p->audmode);
  347. }
  348. static void v4l_print_frequency(const void *arg, bool write_only)
  349. {
  350. const struct v4l2_frequency *p = arg;
  351. pr_cont("tuner=%u, type=%u, frequency=%u\n",
  352. p->tuner, p->type, p->frequency);
  353. }
  354. static void v4l_print_standard(const void *arg, bool write_only)
  355. {
  356. const struct v4l2_standard *p = arg;
  357. pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, "
  358. "framelines=%u\n", p->index,
  359. (unsigned long long)p->id, (int)sizeof(p->name), p->name,
  360. p->frameperiod.numerator,
  361. p->frameperiod.denominator,
  362. p->framelines);
  363. }
  364. static void v4l_print_std(const void *arg, bool write_only)
  365. {
  366. pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
  367. }
  368. static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
  369. {
  370. const struct v4l2_hw_freq_seek *p = arg;
  371. pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
  372. "rangelow=%u, rangehigh=%u\n",
  373. p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
  374. p->rangelow, p->rangehigh);
  375. }
  376. static void v4l_print_requestbuffers(const void *arg, bool write_only)
  377. {
  378. const struct v4l2_requestbuffers *p = arg;
  379. pr_cont("count=%d, type=%s, memory=%s\n",
  380. p->count,
  381. prt_names(p->type, v4l2_type_names),
  382. prt_names(p->memory, v4l2_memory_names));
  383. }
  384. static void v4l_print_buffer(const void *arg, bool write_only)
  385. {
  386. const struct v4l2_buffer *p = arg;
  387. const struct v4l2_timecode *tc = &p->timecode;
  388. const struct v4l2_plane *plane;
  389. int i;
  390. pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
  391. "flags=0x%08x, field=%s, sequence=%d, memory=%s",
  392. p->timestamp.tv_sec / 3600,
  393. (int)(p->timestamp.tv_sec / 60) % 60,
  394. (int)(p->timestamp.tv_sec % 60),
  395. (long)p->timestamp.tv_usec,
  396. p->index,
  397. prt_names(p->type, v4l2_type_names),
  398. p->flags, prt_names(p->field, v4l2_field_names),
  399. p->sequence, prt_names(p->memory, v4l2_memory_names));
  400. if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
  401. pr_cont("\n");
  402. for (i = 0; i < p->length; ++i) {
  403. plane = &p->m.planes[i];
  404. printk(KERN_DEBUG
  405. "plane %d: bytesused=%d, data_offset=0x%08x, "
  406. "offset/userptr=0x%lx, length=%d\n",
  407. i, plane->bytesused, plane->data_offset,
  408. plane->m.userptr, plane->length);
  409. }
  410. } else {
  411. pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
  412. p->bytesused, p->m.userptr, p->length);
  413. }
  414. printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
  415. "flags=0x%08x, frames=%d, userbits=0x%08x\n",
  416. tc->hours, tc->minutes, tc->seconds,
  417. tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
  418. }
  419. static void v4l_print_exportbuffer(const void *arg, bool write_only)
  420. {
  421. const struct v4l2_exportbuffer *p = arg;
  422. pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
  423. p->fd, prt_names(p->type, v4l2_type_names),
  424. p->index, p->plane, p->flags);
  425. }
  426. static void v4l_print_create_buffers(const void *arg, bool write_only)
  427. {
  428. const struct v4l2_create_buffers *p = arg;
  429. pr_cont("index=%d, count=%d, memory=%s, ",
  430. p->index, p->count,
  431. prt_names(p->memory, v4l2_memory_names));
  432. v4l_print_format(&p->format, write_only);
  433. }
  434. static void v4l_print_streamparm(const void *arg, bool write_only)
  435. {
  436. const struct v4l2_streamparm *p = arg;
  437. pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
  438. if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  439. p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  440. const struct v4l2_captureparm *c = &p->parm.capture;
  441. pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
  442. "extendedmode=%d, readbuffers=%d\n",
  443. c->capability, c->capturemode,
  444. c->timeperframe.numerator, c->timeperframe.denominator,
  445. c->extendedmode, c->readbuffers);
  446. } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  447. p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  448. const struct v4l2_outputparm *c = &p->parm.output;
  449. pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
  450. "extendedmode=%d, writebuffers=%d\n",
  451. c->capability, c->outputmode,
  452. c->timeperframe.numerator, c->timeperframe.denominator,
  453. c->extendedmode, c->writebuffers);
  454. } else {
  455. pr_cont("\n");
  456. }
  457. }
  458. static void v4l_print_queryctrl(const void *arg, bool write_only)
  459. {
  460. const struct v4l2_queryctrl *p = arg;
  461. pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
  462. "step=%d, default=%d, flags=0x%08x\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. }
  467. static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
  468. {
  469. const struct v4l2_query_ext_ctrl *p = arg;
  470. pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, "
  471. "step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, "
  472. "nr_of_dims=%u, dims=%u,%u,%u,%u\n",
  473. p->id, p->type, (int)sizeof(p->name), p->name,
  474. p->minimum, p->maximum,
  475. p->step, p->default_value, p->flags,
  476. p->elem_size, p->elems, p->nr_of_dims,
  477. p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
  478. }
  479. static void v4l_print_querymenu(const void *arg, bool write_only)
  480. {
  481. const struct v4l2_querymenu *p = arg;
  482. pr_cont("id=0x%x, index=%d\n", p->id, p->index);
  483. }
  484. static void v4l_print_control(const void *arg, bool write_only)
  485. {
  486. const struct v4l2_control *p = arg;
  487. pr_cont("id=0x%x, value=%d\n", p->id, p->value);
  488. }
  489. static void v4l_print_ext_controls(const void *arg, bool write_only)
  490. {
  491. const struct v4l2_ext_controls *p = arg;
  492. int i;
  493. pr_cont("class=0x%x, count=%d, error_idx=%d",
  494. p->ctrl_class, p->count, p->error_idx);
  495. for (i = 0; i < p->count; i++) {
  496. if (!p->controls[i].size)
  497. pr_cont(", id/val=0x%x/0x%x",
  498. p->controls[i].id, p->controls[i].value);
  499. else
  500. pr_cont(", id/size=0x%x/%u",
  501. p->controls[i].id, p->controls[i].size);
  502. }
  503. pr_cont("\n");
  504. }
  505. static void v4l_print_cropcap(const void *arg, bool write_only)
  506. {
  507. const struct v4l2_cropcap *p = arg;
  508. pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
  509. "defrect wxh=%dx%d, x,y=%d,%d, "
  510. "pixelaspect %d/%d\n",
  511. prt_names(p->type, v4l2_type_names),
  512. p->bounds.width, p->bounds.height,
  513. p->bounds.left, p->bounds.top,
  514. p->defrect.width, p->defrect.height,
  515. p->defrect.left, p->defrect.top,
  516. p->pixelaspect.numerator, p->pixelaspect.denominator);
  517. }
  518. static void v4l_print_crop(const void *arg, bool write_only)
  519. {
  520. const struct v4l2_crop *p = arg;
  521. pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
  522. prt_names(p->type, v4l2_type_names),
  523. p->c.width, p->c.height,
  524. p->c.left, p->c.top);
  525. }
  526. static void v4l_print_selection(const void *arg, bool write_only)
  527. {
  528. const struct v4l2_selection *p = arg;
  529. pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
  530. prt_names(p->type, v4l2_type_names),
  531. p->target, p->flags,
  532. p->r.width, p->r.height, p->r.left, p->r.top);
  533. }
  534. static void v4l_print_jpegcompression(const void *arg, bool write_only)
  535. {
  536. const struct v4l2_jpegcompression *p = arg;
  537. pr_cont("quality=%d, APPn=%d, APP_len=%d, "
  538. "COM_len=%d, jpeg_markers=0x%x\n",
  539. p->quality, p->APPn, p->APP_len,
  540. p->COM_len, p->jpeg_markers);
  541. }
  542. static void v4l_print_enc_idx(const void *arg, bool write_only)
  543. {
  544. const struct v4l2_enc_idx *p = arg;
  545. pr_cont("entries=%d, entries_cap=%d\n",
  546. p->entries, p->entries_cap);
  547. }
  548. static void v4l_print_encoder_cmd(const void *arg, bool write_only)
  549. {
  550. const struct v4l2_encoder_cmd *p = arg;
  551. pr_cont("cmd=%d, flags=0x%x\n",
  552. p->cmd, p->flags);
  553. }
  554. static void v4l_print_decoder_cmd(const void *arg, bool write_only)
  555. {
  556. const struct v4l2_decoder_cmd *p = arg;
  557. pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
  558. if (p->cmd == V4L2_DEC_CMD_START)
  559. pr_info("speed=%d, format=%u\n",
  560. p->start.speed, p->start.format);
  561. else if (p->cmd == V4L2_DEC_CMD_STOP)
  562. pr_info("pts=%llu\n", p->stop.pts);
  563. }
  564. static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
  565. {
  566. const struct v4l2_dbg_chip_info *p = arg;
  567. pr_cont("type=%u, ", p->match.type);
  568. if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
  569. pr_cont("name=%.*s, ",
  570. (int)sizeof(p->match.name), p->match.name);
  571. else
  572. pr_cont("addr=%u, ", p->match.addr);
  573. pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
  574. }
  575. static void v4l_print_dbg_register(const void *arg, bool write_only)
  576. {
  577. const struct v4l2_dbg_register *p = arg;
  578. pr_cont("type=%u, ", p->match.type);
  579. if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
  580. pr_cont("name=%.*s, ",
  581. (int)sizeof(p->match.name), p->match.name);
  582. else
  583. pr_cont("addr=%u, ", p->match.addr);
  584. pr_cont("reg=0x%llx, val=0x%llx\n",
  585. p->reg, p->val);
  586. }
  587. static void v4l_print_dv_timings(const void *arg, bool write_only)
  588. {
  589. const struct v4l2_dv_timings *p = arg;
  590. switch (p->type) {
  591. case V4L2_DV_BT_656_1120:
  592. pr_cont("type=bt-656/1120, interlaced=%u, "
  593. "pixelclock=%llu, "
  594. "width=%u, height=%u, polarities=0x%x, "
  595. "hfrontporch=%u, hsync=%u, "
  596. "hbackporch=%u, vfrontporch=%u, "
  597. "vsync=%u, vbackporch=%u, "
  598. "il_vfrontporch=%u, il_vsync=%u, "
  599. "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
  600. p->bt.interlaced, p->bt.pixelclock,
  601. p->bt.width, p->bt.height,
  602. p->bt.polarities, p->bt.hfrontporch,
  603. p->bt.hsync, p->bt.hbackporch,
  604. p->bt.vfrontporch, p->bt.vsync,
  605. p->bt.vbackporch, p->bt.il_vfrontporch,
  606. p->bt.il_vsync, p->bt.il_vbackporch,
  607. p->bt.standards, p->bt.flags);
  608. break;
  609. default:
  610. pr_cont("type=%d\n", p->type);
  611. break;
  612. }
  613. }
  614. static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
  615. {
  616. const struct v4l2_enum_dv_timings *p = arg;
  617. pr_cont("index=%u, ", p->index);
  618. v4l_print_dv_timings(&p->timings, write_only);
  619. }
  620. static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
  621. {
  622. const struct v4l2_dv_timings_cap *p = arg;
  623. switch (p->type) {
  624. case V4L2_DV_BT_656_1120:
  625. pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
  626. "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
  627. p->bt.min_width, p->bt.max_width,
  628. p->bt.min_height, p->bt.max_height,
  629. p->bt.min_pixelclock, p->bt.max_pixelclock,
  630. p->bt.standards, p->bt.capabilities);
  631. break;
  632. default:
  633. pr_cont("type=%u\n", p->type);
  634. break;
  635. }
  636. }
  637. static void v4l_print_frmsizeenum(const void *arg, bool write_only)
  638. {
  639. const struct v4l2_frmsizeenum *p = arg;
  640. pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
  641. p->index,
  642. (p->pixel_format & 0xff),
  643. (p->pixel_format >> 8) & 0xff,
  644. (p->pixel_format >> 16) & 0xff,
  645. (p->pixel_format >> 24) & 0xff,
  646. p->type);
  647. switch (p->type) {
  648. case V4L2_FRMSIZE_TYPE_DISCRETE:
  649. pr_cont(", wxh=%ux%u\n",
  650. p->discrete.width, p->discrete.height);
  651. break;
  652. case V4L2_FRMSIZE_TYPE_STEPWISE:
  653. pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
  654. p->stepwise.min_width, p->stepwise.min_height,
  655. p->stepwise.step_width, p->stepwise.step_height,
  656. p->stepwise.max_width, p->stepwise.max_height);
  657. break;
  658. case V4L2_FRMSIZE_TYPE_CONTINUOUS:
  659. /* fall through */
  660. default:
  661. pr_cont("\n");
  662. break;
  663. }
  664. }
  665. static void v4l_print_frmivalenum(const void *arg, bool write_only)
  666. {
  667. const struct v4l2_frmivalenum *p = arg;
  668. pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
  669. p->index,
  670. (p->pixel_format & 0xff),
  671. (p->pixel_format >> 8) & 0xff,
  672. (p->pixel_format >> 16) & 0xff,
  673. (p->pixel_format >> 24) & 0xff,
  674. p->width, p->height, p->type);
  675. switch (p->type) {
  676. case V4L2_FRMIVAL_TYPE_DISCRETE:
  677. pr_cont(", fps=%d/%d\n",
  678. p->discrete.numerator,
  679. p->discrete.denominator);
  680. break;
  681. case V4L2_FRMIVAL_TYPE_STEPWISE:
  682. pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
  683. p->stepwise.min.numerator,
  684. p->stepwise.min.denominator,
  685. p->stepwise.max.numerator,
  686. p->stepwise.max.denominator,
  687. p->stepwise.step.numerator,
  688. p->stepwise.step.denominator);
  689. break;
  690. case V4L2_FRMIVAL_TYPE_CONTINUOUS:
  691. /* fall through */
  692. default:
  693. pr_cont("\n");
  694. break;
  695. }
  696. }
  697. static void v4l_print_event(const void *arg, bool write_only)
  698. {
  699. const struct v4l2_event *p = arg;
  700. const struct v4l2_event_ctrl *c;
  701. pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
  702. "timestamp=%lu.%9.9lu\n",
  703. p->type, p->pending, p->sequence, p->id,
  704. p->timestamp.tv_sec, p->timestamp.tv_nsec);
  705. switch (p->type) {
  706. case V4L2_EVENT_VSYNC:
  707. printk(KERN_DEBUG "field=%s\n",
  708. prt_names(p->u.vsync.field, v4l2_field_names));
  709. break;
  710. case V4L2_EVENT_CTRL:
  711. c = &p->u.ctrl;
  712. printk(KERN_DEBUG "changes=0x%x, type=%u, ",
  713. c->changes, c->type);
  714. if (c->type == V4L2_CTRL_TYPE_INTEGER64)
  715. pr_cont("value64=%lld, ", c->value64);
  716. else
  717. pr_cont("value=%d, ", c->value);
  718. pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, "
  719. "default_value=%d\n",
  720. c->flags, c->minimum, c->maximum,
  721. c->step, c->default_value);
  722. break;
  723. case V4L2_EVENT_FRAME_SYNC:
  724. pr_cont("frame_sequence=%u\n",
  725. p->u.frame_sync.frame_sequence);
  726. break;
  727. }
  728. }
  729. static void v4l_print_event_subscription(const void *arg, bool write_only)
  730. {
  731. const struct v4l2_event_subscription *p = arg;
  732. pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
  733. p->type, p->id, p->flags);
  734. }
  735. static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
  736. {
  737. const struct v4l2_sliced_vbi_cap *p = arg;
  738. int i;
  739. pr_cont("type=%s, service_set=0x%08x\n",
  740. prt_names(p->type, v4l2_type_names), p->service_set);
  741. for (i = 0; i < 24; i++)
  742. printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
  743. p->service_lines[0][i],
  744. p->service_lines[1][i]);
  745. }
  746. static void v4l_print_freq_band(const void *arg, bool write_only)
  747. {
  748. const struct v4l2_frequency_band *p = arg;
  749. pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
  750. "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
  751. p->tuner, p->type, p->index,
  752. p->capability, p->rangelow,
  753. p->rangehigh, p->modulation);
  754. }
  755. static void v4l_print_edid(const void *arg, bool write_only)
  756. {
  757. const struct v4l2_edid *p = arg;
  758. pr_cont("pad=%u, start_block=%u, blocks=%u\n",
  759. p->pad, p->start_block, p->blocks);
  760. }
  761. static void v4l_print_u32(const void *arg, bool write_only)
  762. {
  763. pr_cont("value=%u\n", *(const u32 *)arg);
  764. }
  765. static void v4l_print_newline(const void *arg, bool write_only)
  766. {
  767. pr_cont("\n");
  768. }
  769. static void v4l_print_default(const void *arg, bool write_only)
  770. {
  771. pr_cont("driver-specific ioctl\n");
  772. }
  773. static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
  774. {
  775. __u32 i;
  776. /* zero the reserved fields */
  777. c->reserved[0] = c->reserved[1] = 0;
  778. for (i = 0; i < c->count; i++)
  779. c->controls[i].reserved2[0] = 0;
  780. /* V4L2_CID_PRIVATE_BASE cannot be used as control class
  781. when using extended controls.
  782. Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
  783. is it allowed for backwards compatibility.
  784. */
  785. if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
  786. return 0;
  787. if (c->ctrl_class == 0)
  788. return 1;
  789. /* Check that all controls are from the same control class. */
  790. for (i = 0; i < c->count; i++) {
  791. if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
  792. c->error_idx = i;
  793. return 0;
  794. }
  795. }
  796. return 1;
  797. }
  798. static int check_fmt(struct file *file, enum v4l2_buf_type type)
  799. {
  800. struct video_device *vfd = video_devdata(file);
  801. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  802. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  803. bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
  804. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  805. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  806. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  807. if (ops == NULL)
  808. return -EINVAL;
  809. switch (type) {
  810. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  811. if (is_vid && is_rx &&
  812. (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
  813. return 0;
  814. break;
  815. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  816. if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
  817. return 0;
  818. break;
  819. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  820. if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
  821. return 0;
  822. break;
  823. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  824. if (is_vid && is_tx &&
  825. (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
  826. return 0;
  827. break;
  828. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  829. if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
  830. return 0;
  831. break;
  832. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  833. if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
  834. return 0;
  835. break;
  836. case V4L2_BUF_TYPE_VBI_CAPTURE:
  837. if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
  838. return 0;
  839. break;
  840. case V4L2_BUF_TYPE_VBI_OUTPUT:
  841. if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
  842. return 0;
  843. break;
  844. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  845. if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
  846. return 0;
  847. break;
  848. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  849. if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
  850. return 0;
  851. break;
  852. case V4L2_BUF_TYPE_SDR_CAPTURE:
  853. if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
  854. return 0;
  855. break;
  856. default:
  857. break;
  858. }
  859. return -EINVAL;
  860. }
  861. static void v4l_sanitize_format(struct v4l2_format *fmt)
  862. {
  863. unsigned int offset;
  864. /*
  865. * The v4l2_pix_format structure has been extended with fields that were
  866. * not previously required to be set to zero by applications. The priv
  867. * field, when set to a magic value, indicates the the extended fields
  868. * are valid. Otherwise they will contain undefined values. To simplify
  869. * the API towards drivers zero the extended fields and set the priv
  870. * field to the magic value when the extended pixel format structure
  871. * isn't used by applications.
  872. */
  873. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  874. fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  875. return;
  876. if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
  877. return;
  878. fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  879. offset = offsetof(struct v4l2_pix_format, priv)
  880. + sizeof(fmt->fmt.pix.priv);
  881. memset(((void *)&fmt->fmt.pix) + offset, 0,
  882. sizeof(fmt->fmt.pix) - offset);
  883. }
  884. static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
  885. struct file *file, void *fh, void *arg)
  886. {
  887. struct v4l2_capability *cap = (struct v4l2_capability *)arg;
  888. int ret;
  889. cap->version = LINUX_VERSION_CODE;
  890. ret = ops->vidioc_querycap(file, fh, cap);
  891. cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
  892. /*
  893. * Drivers MUST fill in device_caps, so check for this and
  894. * warn if it was forgotten.
  895. */
  896. WARN_ON(!(cap->capabilities & V4L2_CAP_DEVICE_CAPS) ||
  897. !cap->device_caps);
  898. cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
  899. return ret;
  900. }
  901. static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
  902. struct file *file, void *fh, void *arg)
  903. {
  904. return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
  905. }
  906. static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
  907. struct file *file, void *fh, void *arg)
  908. {
  909. return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
  910. }
  911. static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
  912. struct file *file, void *fh, void *arg)
  913. {
  914. struct video_device *vfd;
  915. u32 *p = arg;
  916. vfd = video_devdata(file);
  917. *p = v4l2_prio_max(vfd->prio);
  918. return 0;
  919. }
  920. static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
  921. struct file *file, void *fh, void *arg)
  922. {
  923. struct video_device *vfd;
  924. struct v4l2_fh *vfh;
  925. u32 *p = arg;
  926. vfd = video_devdata(file);
  927. if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
  928. return -ENOTTY;
  929. vfh = file->private_data;
  930. return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
  931. }
  932. static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
  933. struct file *file, void *fh, void *arg)
  934. {
  935. struct video_device *vfd = video_devdata(file);
  936. struct v4l2_input *p = arg;
  937. /*
  938. * We set the flags for CAP_DV_TIMINGS &
  939. * CAP_STD here based on ioctl handler provided by the
  940. * driver. If the driver doesn't support these
  941. * for a specific input, it must override these flags.
  942. */
  943. if (is_valid_ioctl(vfd, VIDIOC_S_STD))
  944. p->capabilities |= V4L2_IN_CAP_STD;
  945. return ops->vidioc_enum_input(file, fh, p);
  946. }
  947. static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
  948. struct file *file, void *fh, void *arg)
  949. {
  950. struct video_device *vfd = video_devdata(file);
  951. struct v4l2_output *p = arg;
  952. /*
  953. * We set the flags for CAP_DV_TIMINGS &
  954. * CAP_STD here based on ioctl handler provided by the
  955. * driver. If the driver doesn't support these
  956. * for a specific output, it must override these flags.
  957. */
  958. if (is_valid_ioctl(vfd, VIDIOC_S_STD))
  959. p->capabilities |= V4L2_OUT_CAP_STD;
  960. return ops->vidioc_enum_output(file, fh, p);
  961. }
  962. static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
  963. struct file *file, void *fh, void *arg)
  964. {
  965. struct v4l2_fmtdesc *p = arg;
  966. struct video_device *vfd = video_devdata(file);
  967. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  968. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  969. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  970. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  971. switch (p->type) {
  972. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  973. if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap))
  974. break;
  975. return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
  976. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  977. if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap_mplane))
  978. break;
  979. return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
  980. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  981. if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_overlay))
  982. break;
  983. return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
  984. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  985. if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out))
  986. break;
  987. return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
  988. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  989. if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out_mplane))
  990. break;
  991. return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
  992. case V4L2_BUF_TYPE_SDR_CAPTURE:
  993. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_enum_fmt_sdr_cap))
  994. break;
  995. return ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
  996. }
  997. return -EINVAL;
  998. }
  999. static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
  1000. struct file *file, void *fh, void *arg)
  1001. {
  1002. struct v4l2_format *p = arg;
  1003. struct video_device *vfd = video_devdata(file);
  1004. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1005. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1006. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1007. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1008. int ret;
  1009. /*
  1010. * fmt can't be cleared for these overlay types due to the 'clips'
  1011. * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
  1012. * Those are provided by the user. So handle these two overlay types
  1013. * first, and then just do a simple memset for the other types.
  1014. */
  1015. switch (p->type) {
  1016. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1017. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
  1018. struct v4l2_clip __user *clips = p->fmt.win.clips;
  1019. u32 clipcount = p->fmt.win.clipcount;
  1020. void __user *bitmap = p->fmt.win.bitmap;
  1021. memset(&p->fmt, 0, sizeof(p->fmt));
  1022. p->fmt.win.clips = clips;
  1023. p->fmt.win.clipcount = clipcount;
  1024. p->fmt.win.bitmap = bitmap;
  1025. break;
  1026. }
  1027. default:
  1028. memset(&p->fmt, 0, sizeof(p->fmt));
  1029. break;
  1030. }
  1031. switch (p->type) {
  1032. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1033. if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
  1034. break;
  1035. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1036. ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
  1037. /* just in case the driver zeroed it again */
  1038. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1039. return ret;
  1040. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1041. if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
  1042. break;
  1043. return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
  1044. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1045. if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
  1046. break;
  1047. return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
  1048. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1049. if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
  1050. break;
  1051. return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
  1052. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1053. if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
  1054. break;
  1055. return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
  1056. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1057. if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
  1058. break;
  1059. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1060. ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
  1061. /* just in case the driver zeroed it again */
  1062. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1063. return ret;
  1064. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1065. if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
  1066. break;
  1067. return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
  1068. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1069. if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
  1070. break;
  1071. return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
  1072. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1073. if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
  1074. break;
  1075. return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
  1076. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1077. if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
  1078. break;
  1079. return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
  1080. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1081. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_g_fmt_sdr_cap))
  1082. break;
  1083. return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
  1084. }
  1085. return -EINVAL;
  1086. }
  1087. static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
  1088. struct file *file, void *fh, void *arg)
  1089. {
  1090. struct v4l2_format *p = arg;
  1091. struct video_device *vfd = video_devdata(file);
  1092. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1093. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1094. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1095. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1096. int ret;
  1097. v4l_sanitize_format(p);
  1098. switch (p->type) {
  1099. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1100. if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
  1101. break;
  1102. CLEAR_AFTER_FIELD(p, fmt.pix);
  1103. ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
  1104. /* just in case the driver zeroed it again */
  1105. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1106. return ret;
  1107. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1108. if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
  1109. break;
  1110. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1111. return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
  1112. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1113. if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
  1114. break;
  1115. CLEAR_AFTER_FIELD(p, fmt.win);
  1116. return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
  1117. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1118. if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
  1119. break;
  1120. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1121. return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
  1122. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1123. if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
  1124. break;
  1125. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1126. return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
  1127. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1128. if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
  1129. break;
  1130. CLEAR_AFTER_FIELD(p, fmt.pix);
  1131. ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
  1132. /* just in case the driver zeroed it again */
  1133. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1134. return ret;
  1135. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1136. if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
  1137. break;
  1138. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1139. return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
  1140. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1141. if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
  1142. break;
  1143. CLEAR_AFTER_FIELD(p, fmt.win);
  1144. return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
  1145. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1146. if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
  1147. break;
  1148. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1149. return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
  1150. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1151. if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
  1152. break;
  1153. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1154. return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
  1155. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1156. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_s_fmt_sdr_cap))
  1157. break;
  1158. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1159. return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
  1160. }
  1161. return -EINVAL;
  1162. }
  1163. static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
  1164. struct file *file, void *fh, void *arg)
  1165. {
  1166. struct v4l2_format *p = arg;
  1167. struct video_device *vfd = video_devdata(file);
  1168. bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
  1169. bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
  1170. bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
  1171. bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
  1172. int ret;
  1173. v4l_sanitize_format(p);
  1174. switch (p->type) {
  1175. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1176. if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
  1177. break;
  1178. CLEAR_AFTER_FIELD(p, fmt.pix);
  1179. ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
  1180. /* just in case the driver zeroed it again */
  1181. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1182. return ret;
  1183. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  1184. if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
  1185. break;
  1186. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1187. return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
  1188. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1189. if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
  1190. break;
  1191. CLEAR_AFTER_FIELD(p, fmt.win);
  1192. return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
  1193. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1194. if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
  1195. break;
  1196. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1197. return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
  1198. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  1199. if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
  1200. break;
  1201. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1202. return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
  1203. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1204. if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
  1205. break;
  1206. CLEAR_AFTER_FIELD(p, fmt.pix);
  1207. ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
  1208. /* just in case the driver zeroed it again */
  1209. p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1210. return ret;
  1211. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  1212. if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
  1213. break;
  1214. CLEAR_AFTER_FIELD(p, fmt.pix_mp);
  1215. return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
  1216. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  1217. if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
  1218. break;
  1219. CLEAR_AFTER_FIELD(p, fmt.win);
  1220. return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
  1221. case V4L2_BUF_TYPE_VBI_OUTPUT:
  1222. if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
  1223. break;
  1224. CLEAR_AFTER_FIELD(p, fmt.vbi);
  1225. return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
  1226. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  1227. if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
  1228. break;
  1229. CLEAR_AFTER_FIELD(p, fmt.sliced);
  1230. return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
  1231. case V4L2_BUF_TYPE_SDR_CAPTURE:
  1232. if (unlikely(!is_rx || !is_sdr || !ops->vidioc_try_fmt_sdr_cap))
  1233. break;
  1234. CLEAR_AFTER_FIELD(p, fmt.sdr);
  1235. return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
  1236. }
  1237. return -EINVAL;
  1238. }
  1239. static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
  1240. struct file *file, void *fh, void *arg)
  1241. {
  1242. return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
  1243. }
  1244. static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
  1245. struct file *file, void *fh, void *arg)
  1246. {
  1247. return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
  1248. }
  1249. static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
  1250. struct file *file, void *fh, void *arg)
  1251. {
  1252. struct video_device *vfd = video_devdata(file);
  1253. struct v4l2_tuner *p = arg;
  1254. int err;
  1255. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1256. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1257. err = ops->vidioc_g_tuner(file, fh, p);
  1258. if (!err)
  1259. p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
  1260. return err;
  1261. }
  1262. static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
  1263. struct file *file, void *fh, void *arg)
  1264. {
  1265. struct video_device *vfd = video_devdata(file);
  1266. struct v4l2_tuner *p = arg;
  1267. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1268. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1269. return ops->vidioc_s_tuner(file, fh, p);
  1270. }
  1271. static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
  1272. struct file *file, void *fh, void *arg)
  1273. {
  1274. struct v4l2_modulator *p = arg;
  1275. int err;
  1276. err = ops->vidioc_g_modulator(file, fh, p);
  1277. if (!err)
  1278. p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
  1279. return err;
  1280. }
  1281. static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
  1282. struct file *file, void *fh, void *arg)
  1283. {
  1284. struct video_device *vfd = video_devdata(file);
  1285. struct v4l2_frequency *p = arg;
  1286. if (vfd->vfl_type == VFL_TYPE_SDR)
  1287. p->type = V4L2_TUNER_ADC;
  1288. else
  1289. p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1290. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1291. return ops->vidioc_g_frequency(file, fh, p);
  1292. }
  1293. static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
  1294. struct file *file, void *fh, void *arg)
  1295. {
  1296. struct video_device *vfd = video_devdata(file);
  1297. const struct v4l2_frequency *p = arg;
  1298. enum v4l2_tuner_type type;
  1299. if (vfd->vfl_type == VFL_TYPE_SDR) {
  1300. if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
  1301. return -EINVAL;
  1302. } else {
  1303. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1304. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1305. if (type != p->type)
  1306. return -EINVAL;
  1307. }
  1308. return ops->vidioc_s_frequency(file, fh, p);
  1309. }
  1310. static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
  1311. struct file *file, void *fh, void *arg)
  1312. {
  1313. struct video_device *vfd = video_devdata(file);
  1314. struct v4l2_standard *p = arg;
  1315. v4l2_std_id id = vfd->tvnorms, curr_id = 0;
  1316. unsigned int index = p->index, i, j = 0;
  1317. const char *descr = "";
  1318. /* Return -ENODATA if the tvnorms for the current input
  1319. or output is 0, meaning that it doesn't support this API. */
  1320. if (id == 0)
  1321. return -ENODATA;
  1322. /* Return norm array in a canonical way */
  1323. for (i = 0; i <= index && id; i++) {
  1324. /* last std value in the standards array is 0, so this
  1325. while always ends there since (id & 0) == 0. */
  1326. while ((id & standards[j].std) != standards[j].std)
  1327. j++;
  1328. curr_id = standards[j].std;
  1329. descr = standards[j].descr;
  1330. j++;
  1331. if (curr_id == 0)
  1332. break;
  1333. if (curr_id != V4L2_STD_PAL &&
  1334. curr_id != V4L2_STD_SECAM &&
  1335. curr_id != V4L2_STD_NTSC)
  1336. id &= ~curr_id;
  1337. }
  1338. if (i <= index)
  1339. return -EINVAL;
  1340. v4l2_video_std_construct(p, curr_id, descr);
  1341. return 0;
  1342. }
  1343. static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
  1344. struct file *file, void *fh, void *arg)
  1345. {
  1346. struct video_device *vfd = video_devdata(file);
  1347. v4l2_std_id id = *(v4l2_std_id *)arg, norm;
  1348. norm = id & vfd->tvnorms;
  1349. if (vfd->tvnorms && !norm) /* Check if std is supported */
  1350. return -EINVAL;
  1351. /* Calls the specific handler */
  1352. return ops->vidioc_s_std(file, fh, norm);
  1353. }
  1354. static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
  1355. struct file *file, void *fh, void *arg)
  1356. {
  1357. struct video_device *vfd = video_devdata(file);
  1358. v4l2_std_id *p = arg;
  1359. /*
  1360. * If no signal is detected, then the driver should return
  1361. * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
  1362. * any standards that do not apply removed.
  1363. *
  1364. * This means that tuners, audio and video decoders can join
  1365. * their efforts to improve the standards detection.
  1366. */
  1367. *p = vfd->tvnorms;
  1368. return ops->vidioc_querystd(file, fh, arg);
  1369. }
  1370. static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
  1371. struct file *file, void *fh, void *arg)
  1372. {
  1373. struct video_device *vfd = video_devdata(file);
  1374. struct v4l2_hw_freq_seek *p = arg;
  1375. enum v4l2_tuner_type type;
  1376. /* s_hw_freq_seek is not supported for SDR for now */
  1377. if (vfd->vfl_type == VFL_TYPE_SDR)
  1378. return -EINVAL;
  1379. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1380. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1381. if (p->type != type)
  1382. return -EINVAL;
  1383. return ops->vidioc_s_hw_freq_seek(file, fh, p);
  1384. }
  1385. static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
  1386. struct file *file, void *fh, void *arg)
  1387. {
  1388. return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
  1389. }
  1390. static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
  1391. struct file *file, void *fh, void *arg)
  1392. {
  1393. struct v4l2_requestbuffers *p = arg;
  1394. int ret = check_fmt(file, p->type);
  1395. if (ret)
  1396. return ret;
  1397. CLEAR_AFTER_FIELD(p, memory);
  1398. return ops->vidioc_reqbufs(file, fh, p);
  1399. }
  1400. static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
  1401. struct file *file, void *fh, void *arg)
  1402. {
  1403. struct v4l2_buffer *p = arg;
  1404. int ret = check_fmt(file, p->type);
  1405. return ret ? ret : ops->vidioc_querybuf(file, fh, p);
  1406. }
  1407. static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
  1408. struct file *file, void *fh, void *arg)
  1409. {
  1410. struct v4l2_buffer *p = arg;
  1411. int ret = check_fmt(file, p->type);
  1412. return ret ? ret : ops->vidioc_qbuf(file, fh, p);
  1413. }
  1414. static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
  1415. struct file *file, void *fh, void *arg)
  1416. {
  1417. struct v4l2_buffer *p = arg;
  1418. int ret = check_fmt(file, p->type);
  1419. return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
  1420. }
  1421. static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
  1422. struct file *file, void *fh, void *arg)
  1423. {
  1424. struct v4l2_create_buffers *create = arg;
  1425. int ret = check_fmt(file, create->format.type);
  1426. if (ret)
  1427. return ret;
  1428. v4l_sanitize_format(&create->format);
  1429. ret = ops->vidioc_create_bufs(file, fh, create);
  1430. if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1431. create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1432. create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
  1433. return ret;
  1434. }
  1435. static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
  1436. struct file *file, void *fh, void *arg)
  1437. {
  1438. struct v4l2_buffer *b = arg;
  1439. int ret = check_fmt(file, b->type);
  1440. return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
  1441. }
  1442. static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
  1443. struct file *file, void *fh, void *arg)
  1444. {
  1445. struct v4l2_streamparm *p = arg;
  1446. v4l2_std_id std;
  1447. int ret = check_fmt(file, p->type);
  1448. if (ret)
  1449. return ret;
  1450. if (ops->vidioc_g_parm)
  1451. return ops->vidioc_g_parm(file, fh, p);
  1452. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1453. p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1454. return -EINVAL;
  1455. p->parm.capture.readbuffers = 2;
  1456. ret = ops->vidioc_g_std(file, fh, &std);
  1457. if (ret == 0)
  1458. v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
  1459. return ret;
  1460. }
  1461. static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
  1462. struct file *file, void *fh, void *arg)
  1463. {
  1464. struct v4l2_streamparm *p = arg;
  1465. int ret = check_fmt(file, p->type);
  1466. return ret ? ret : ops->vidioc_s_parm(file, fh, p);
  1467. }
  1468. static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
  1469. struct file *file, void *fh, void *arg)
  1470. {
  1471. struct video_device *vfd = video_devdata(file);
  1472. struct v4l2_queryctrl *p = arg;
  1473. struct v4l2_fh *vfh =
  1474. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1475. if (vfh && vfh->ctrl_handler)
  1476. return v4l2_queryctrl(vfh->ctrl_handler, p);
  1477. if (vfd->ctrl_handler)
  1478. return v4l2_queryctrl(vfd->ctrl_handler, p);
  1479. if (ops->vidioc_queryctrl)
  1480. return ops->vidioc_queryctrl(file, fh, p);
  1481. return -ENOTTY;
  1482. }
  1483. static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
  1484. struct file *file, void *fh, void *arg)
  1485. {
  1486. struct video_device *vfd = video_devdata(file);
  1487. struct v4l2_query_ext_ctrl *p = arg;
  1488. struct v4l2_fh *vfh =
  1489. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1490. if (vfh && vfh->ctrl_handler)
  1491. return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
  1492. if (vfd->ctrl_handler)
  1493. return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
  1494. if (ops->vidioc_query_ext_ctrl)
  1495. return ops->vidioc_query_ext_ctrl(file, fh, p);
  1496. return -ENOTTY;
  1497. }
  1498. static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
  1499. struct file *file, void *fh, void *arg)
  1500. {
  1501. struct video_device *vfd = video_devdata(file);
  1502. struct v4l2_querymenu *p = arg;
  1503. struct v4l2_fh *vfh =
  1504. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1505. if (vfh && vfh->ctrl_handler)
  1506. return v4l2_querymenu(vfh->ctrl_handler, p);
  1507. if (vfd->ctrl_handler)
  1508. return v4l2_querymenu(vfd->ctrl_handler, p);
  1509. if (ops->vidioc_querymenu)
  1510. return ops->vidioc_querymenu(file, fh, p);
  1511. return -ENOTTY;
  1512. }
  1513. static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
  1514. struct file *file, void *fh, void *arg)
  1515. {
  1516. struct video_device *vfd = video_devdata(file);
  1517. struct v4l2_control *p = arg;
  1518. struct v4l2_fh *vfh =
  1519. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1520. struct v4l2_ext_controls ctrls;
  1521. struct v4l2_ext_control ctrl;
  1522. if (vfh && vfh->ctrl_handler)
  1523. return v4l2_g_ctrl(vfh->ctrl_handler, p);
  1524. if (vfd->ctrl_handler)
  1525. return v4l2_g_ctrl(vfd->ctrl_handler, p);
  1526. if (ops->vidioc_g_ctrl)
  1527. return ops->vidioc_g_ctrl(file, fh, p);
  1528. if (ops->vidioc_g_ext_ctrls == NULL)
  1529. return -ENOTTY;
  1530. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1531. ctrls.count = 1;
  1532. ctrls.controls = &ctrl;
  1533. ctrl.id = p->id;
  1534. ctrl.value = p->value;
  1535. if (check_ext_ctrls(&ctrls, 1)) {
  1536. int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
  1537. if (ret == 0)
  1538. p->value = ctrl.value;
  1539. return ret;
  1540. }
  1541. return -EINVAL;
  1542. }
  1543. static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
  1544. struct file *file, void *fh, void *arg)
  1545. {
  1546. struct video_device *vfd = video_devdata(file);
  1547. struct v4l2_control *p = arg;
  1548. struct v4l2_fh *vfh =
  1549. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1550. struct v4l2_ext_controls ctrls;
  1551. struct v4l2_ext_control ctrl;
  1552. if (vfh && vfh->ctrl_handler)
  1553. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
  1554. if (vfd->ctrl_handler)
  1555. return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
  1556. if (ops->vidioc_s_ctrl)
  1557. return ops->vidioc_s_ctrl(file, fh, p);
  1558. if (ops->vidioc_s_ext_ctrls == NULL)
  1559. return -ENOTTY;
  1560. ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
  1561. ctrls.count = 1;
  1562. ctrls.controls = &ctrl;
  1563. ctrl.id = p->id;
  1564. ctrl.value = p->value;
  1565. if (check_ext_ctrls(&ctrls, 1))
  1566. return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
  1567. return -EINVAL;
  1568. }
  1569. static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1570. struct file *file, void *fh, void *arg)
  1571. {
  1572. struct video_device *vfd = video_devdata(file);
  1573. struct v4l2_ext_controls *p = arg;
  1574. struct v4l2_fh *vfh =
  1575. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1576. p->error_idx = p->count;
  1577. if (vfh && vfh->ctrl_handler)
  1578. return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
  1579. if (vfd->ctrl_handler)
  1580. return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
  1581. if (ops->vidioc_g_ext_ctrls == NULL)
  1582. return -ENOTTY;
  1583. return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
  1584. -EINVAL;
  1585. }
  1586. static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1587. struct file *file, void *fh, void *arg)
  1588. {
  1589. struct video_device *vfd = video_devdata(file);
  1590. struct v4l2_ext_controls *p = arg;
  1591. struct v4l2_fh *vfh =
  1592. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1593. p->error_idx = p->count;
  1594. if (vfh && vfh->ctrl_handler)
  1595. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
  1596. if (vfd->ctrl_handler)
  1597. return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
  1598. if (ops->vidioc_s_ext_ctrls == NULL)
  1599. return -ENOTTY;
  1600. return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
  1601. -EINVAL;
  1602. }
  1603. static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
  1604. struct file *file, void *fh, void *arg)
  1605. {
  1606. struct video_device *vfd = video_devdata(file);
  1607. struct v4l2_ext_controls *p = arg;
  1608. struct v4l2_fh *vfh =
  1609. test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
  1610. p->error_idx = p->count;
  1611. if (vfh && vfh->ctrl_handler)
  1612. return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
  1613. if (vfd->ctrl_handler)
  1614. return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
  1615. if (ops->vidioc_try_ext_ctrls == NULL)
  1616. return -ENOTTY;
  1617. return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
  1618. -EINVAL;
  1619. }
  1620. static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
  1621. struct file *file, void *fh, void *arg)
  1622. {
  1623. struct v4l2_crop *p = arg;
  1624. struct v4l2_selection s = {
  1625. .type = p->type,
  1626. };
  1627. int ret;
  1628. if (ops->vidioc_g_crop)
  1629. return ops->vidioc_g_crop(file, fh, p);
  1630. /* simulate capture crop using selection api */
  1631. /* crop means compose for output devices */
  1632. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1633. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1634. else
  1635. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1636. ret = ops->vidioc_g_selection(file, fh, &s);
  1637. /* copying results to old structure on success */
  1638. if (!ret)
  1639. p->c = s.r;
  1640. return ret;
  1641. }
  1642. static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
  1643. struct file *file, void *fh, void *arg)
  1644. {
  1645. struct v4l2_crop *p = arg;
  1646. struct v4l2_selection s = {
  1647. .type = p->type,
  1648. .r = p->c,
  1649. };
  1650. if (ops->vidioc_s_crop)
  1651. return ops->vidioc_s_crop(file, fh, p);
  1652. /* simulate capture crop using selection api */
  1653. /* crop means compose for output devices */
  1654. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1655. s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  1656. else
  1657. s.target = V4L2_SEL_TGT_CROP_ACTIVE;
  1658. return ops->vidioc_s_selection(file, fh, &s);
  1659. }
  1660. static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
  1661. struct file *file, void *fh, void *arg)
  1662. {
  1663. struct v4l2_cropcap *p = arg;
  1664. if (ops->vidioc_g_selection) {
  1665. struct v4l2_selection s = { .type = p->type };
  1666. int ret;
  1667. /* obtaining bounds */
  1668. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1669. s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
  1670. else
  1671. s.target = V4L2_SEL_TGT_CROP_BOUNDS;
  1672. ret = ops->vidioc_g_selection(file, fh, &s);
  1673. if (ret)
  1674. return ret;
  1675. p->bounds = s.r;
  1676. /* obtaining defrect */
  1677. if (V4L2_TYPE_IS_OUTPUT(p->type))
  1678. s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
  1679. else
  1680. s.target = V4L2_SEL_TGT_CROP_DEFAULT;
  1681. ret = ops->vidioc_g_selection(file, fh, &s);
  1682. if (ret)
  1683. return ret;
  1684. p->defrect = s.r;
  1685. }
  1686. /* setting trivial pixelaspect */
  1687. p->pixelaspect.numerator = 1;
  1688. p->pixelaspect.denominator = 1;
  1689. if (ops->vidioc_cropcap)
  1690. return ops->vidioc_cropcap(file, fh, p);
  1691. return 0;
  1692. }
  1693. static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
  1694. struct file *file, void *fh, void *arg)
  1695. {
  1696. struct video_device *vfd = video_devdata(file);
  1697. int ret;
  1698. if (vfd->v4l2_dev)
  1699. pr_info("%s: ================= START STATUS =================\n",
  1700. vfd->v4l2_dev->name);
  1701. ret = ops->vidioc_log_status(file, fh);
  1702. if (vfd->v4l2_dev)
  1703. pr_info("%s: ================== END STATUS ==================\n",
  1704. vfd->v4l2_dev->name);
  1705. return ret;
  1706. }
  1707. static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
  1708. struct file *file, void *fh, void *arg)
  1709. {
  1710. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1711. struct v4l2_dbg_register *p = arg;
  1712. struct video_device *vfd = video_devdata(file);
  1713. struct v4l2_subdev *sd;
  1714. int idx = 0;
  1715. if (!capable(CAP_SYS_ADMIN))
  1716. return -EPERM;
  1717. if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
  1718. if (vfd->v4l2_dev == NULL)
  1719. return -EINVAL;
  1720. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
  1721. if (p->match.addr == idx++)
  1722. return v4l2_subdev_call(sd, core, g_register, p);
  1723. return -EINVAL;
  1724. }
  1725. if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
  1726. (ops->vidioc_g_chip_info || p->match.addr == 0))
  1727. return ops->vidioc_g_register(file, fh, p);
  1728. return -EINVAL;
  1729. #else
  1730. return -ENOTTY;
  1731. #endif
  1732. }
  1733. static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
  1734. struct file *file, void *fh, void *arg)
  1735. {
  1736. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1737. const struct v4l2_dbg_register *p = arg;
  1738. struct video_device *vfd = video_devdata(file);
  1739. struct v4l2_subdev *sd;
  1740. int idx = 0;
  1741. if (!capable(CAP_SYS_ADMIN))
  1742. return -EPERM;
  1743. if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
  1744. if (vfd->v4l2_dev == NULL)
  1745. return -EINVAL;
  1746. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
  1747. if (p->match.addr == idx++)
  1748. return v4l2_subdev_call(sd, core, s_register, p);
  1749. return -EINVAL;
  1750. }
  1751. if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
  1752. (ops->vidioc_g_chip_info || p->match.addr == 0))
  1753. return ops->vidioc_s_register(file, fh, p);
  1754. return -EINVAL;
  1755. #else
  1756. return -ENOTTY;
  1757. #endif
  1758. }
  1759. static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
  1760. struct file *file, void *fh, void *arg)
  1761. {
  1762. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1763. struct video_device *vfd = video_devdata(file);
  1764. struct v4l2_dbg_chip_info *p = arg;
  1765. struct v4l2_subdev *sd;
  1766. int idx = 0;
  1767. switch (p->match.type) {
  1768. case V4L2_CHIP_MATCH_BRIDGE:
  1769. if (ops->vidioc_s_register)
  1770. p->flags |= V4L2_CHIP_FL_WRITABLE;
  1771. if (ops->vidioc_g_register)
  1772. p->flags |= V4L2_CHIP_FL_READABLE;
  1773. strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
  1774. if (ops->vidioc_g_chip_info)
  1775. return ops->vidioc_g_chip_info(file, fh, arg);
  1776. if (p->match.addr)
  1777. return -EINVAL;
  1778. return 0;
  1779. case V4L2_CHIP_MATCH_SUBDEV:
  1780. if (vfd->v4l2_dev == NULL)
  1781. break;
  1782. v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
  1783. if (p->match.addr != idx++)
  1784. continue;
  1785. if (sd->ops->core && sd->ops->core->s_register)
  1786. p->flags |= V4L2_CHIP_FL_WRITABLE;
  1787. if (sd->ops->core && sd->ops->core->g_register)
  1788. p->flags |= V4L2_CHIP_FL_READABLE;
  1789. strlcpy(p->name, sd->name, sizeof(p->name));
  1790. return 0;
  1791. }
  1792. break;
  1793. }
  1794. return -EINVAL;
  1795. #else
  1796. return -ENOTTY;
  1797. #endif
  1798. }
  1799. static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
  1800. struct file *file, void *fh, void *arg)
  1801. {
  1802. return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
  1803. }
  1804. static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
  1805. struct file *file, void *fh, void *arg)
  1806. {
  1807. return ops->vidioc_subscribe_event(fh, arg);
  1808. }
  1809. static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
  1810. struct file *file, void *fh, void *arg)
  1811. {
  1812. return ops->vidioc_unsubscribe_event(fh, arg);
  1813. }
  1814. static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
  1815. struct file *file, void *fh, void *arg)
  1816. {
  1817. struct v4l2_sliced_vbi_cap *p = arg;
  1818. int ret = check_fmt(file, p->type);
  1819. if (ret)
  1820. return ret;
  1821. /* Clear up to type, everything after type is zeroed already */
  1822. memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
  1823. return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
  1824. }
  1825. static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
  1826. struct file *file, void *fh, void *arg)
  1827. {
  1828. struct video_device *vfd = video_devdata(file);
  1829. struct v4l2_frequency_band *p = arg;
  1830. enum v4l2_tuner_type type;
  1831. int err;
  1832. if (vfd->vfl_type == VFL_TYPE_SDR) {
  1833. if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
  1834. return -EINVAL;
  1835. type = p->type;
  1836. } else {
  1837. type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
  1838. V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1839. if (type != p->type)
  1840. return -EINVAL;
  1841. }
  1842. if (ops->vidioc_enum_freq_bands) {
  1843. err = ops->vidioc_enum_freq_bands(file, fh, p);
  1844. if (err != -ENOTTY)
  1845. return err;
  1846. }
  1847. if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
  1848. struct v4l2_tuner t = {
  1849. .index = p->tuner,
  1850. .type = type,
  1851. };
  1852. if (p->index)
  1853. return -EINVAL;
  1854. err = ops->vidioc_g_tuner(file, fh, &t);
  1855. if (err)
  1856. return err;
  1857. p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
  1858. p->rangelow = t.rangelow;
  1859. p->rangehigh = t.rangehigh;
  1860. p->modulation = (type == V4L2_TUNER_RADIO) ?
  1861. V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
  1862. return 0;
  1863. }
  1864. if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
  1865. struct v4l2_modulator m = {
  1866. .index = p->tuner,
  1867. };
  1868. if (type != V4L2_TUNER_RADIO)
  1869. return -EINVAL;
  1870. if (p->index)
  1871. return -EINVAL;
  1872. err = ops->vidioc_g_modulator(file, fh, &m);
  1873. if (err)
  1874. return err;
  1875. p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
  1876. p->rangelow = m.rangelow;
  1877. p->rangehigh = m.rangehigh;
  1878. p->modulation = (type == V4L2_TUNER_RADIO) ?
  1879. V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
  1880. return 0;
  1881. }
  1882. return -ENOTTY;
  1883. }
  1884. struct v4l2_ioctl_info {
  1885. unsigned int ioctl;
  1886. u32 flags;
  1887. const char * const name;
  1888. union {
  1889. u32 offset;
  1890. int (*func)(const struct v4l2_ioctl_ops *ops,
  1891. struct file *file, void *fh, void *p);
  1892. } u;
  1893. void (*debug)(const void *arg, bool write_only);
  1894. };
  1895. /* This control needs a priority check */
  1896. #define INFO_FL_PRIO (1 << 0)
  1897. /* This control can be valid if the filehandle passes a control handler. */
  1898. #define INFO_FL_CTRL (1 << 1)
  1899. /* This is a standard ioctl, no need for special code */
  1900. #define INFO_FL_STD (1 << 2)
  1901. /* This is ioctl has its own function */
  1902. #define INFO_FL_FUNC (1 << 3)
  1903. /* Queuing ioctl */
  1904. #define INFO_FL_QUEUE (1 << 4)
  1905. /* Zero struct from after the field to the end */
  1906. #define INFO_FL_CLEAR(v4l2_struct, field) \
  1907. ((offsetof(struct v4l2_struct, field) + \
  1908. sizeof(((struct v4l2_struct *)0)->field)) << 16)
  1909. #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
  1910. #define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
  1911. [_IOC_NR(_ioctl)] = { \
  1912. .ioctl = _ioctl, \
  1913. .flags = _flags | INFO_FL_STD, \
  1914. .name = #_ioctl, \
  1915. .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
  1916. .debug = _debug, \
  1917. }
  1918. #define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
  1919. [_IOC_NR(_ioctl)] = { \
  1920. .ioctl = _ioctl, \
  1921. .flags = _flags | INFO_FL_FUNC, \
  1922. .name = #_ioctl, \
  1923. .u.func = _func, \
  1924. .debug = _debug, \
  1925. }
  1926. static struct v4l2_ioctl_info v4l2_ioctls[] = {
  1927. IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
  1928. IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
  1929. IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
  1930. IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
  1931. IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
  1932. IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
  1933. IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
  1934. IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
  1935. IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
  1936. IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
  1937. IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
  1938. IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
  1939. IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
  1940. IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
  1941. IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
  1942. IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
  1943. IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
  1944. IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
  1945. IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
  1946. IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
  1947. IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
  1948. IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
  1949. IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
  1950. IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
  1951. IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
  1952. IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
  1953. IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
  1954. IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
  1955. IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
  1956. IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
  1957. IOCTL_INFO_STD(VIDIOC_G_EDID, vidioc_g_edid, v4l_print_edid, 0),
  1958. IOCTL_INFO_STD(VIDIOC_S_EDID, vidioc_s_edid, v4l_print_edid, INFO_FL_PRIO),
  1959. IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
  1960. IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
  1961. IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
  1962. IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
  1963. IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
  1964. IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
  1965. IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
  1966. IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
  1967. IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
  1968. IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
  1969. IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
  1970. IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
  1971. IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
  1972. IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
  1973. IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
  1974. IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
  1975. IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
  1976. IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
  1977. IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
  1978. IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
  1979. IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
  1980. IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
  1981. 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)),
  1982. IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
  1983. IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  1984. IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
  1985. IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
  1986. IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
  1987. IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
  1988. IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
  1989. IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  1990. IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
  1991. IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
  1992. IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
  1993. IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
  1994. IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
  1995. IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
  1996. IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
  1997. IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
  1998. IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
  1999. IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
  2000. IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
  2001. IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
  2002. IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
  2003. IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
  2004. IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
  2005. IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
  2006. IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
  2007. 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)),
  2008. 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)),
  2009. };
  2010. #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
  2011. bool v4l2_is_known_ioctl(unsigned int cmd)
  2012. {
  2013. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  2014. return false;
  2015. return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
  2016. }
  2017. struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
  2018. {
  2019. if (_IOC_NR(cmd) >= V4L2_IOCTLS)
  2020. return vdev->lock;
  2021. if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
  2022. return NULL;
  2023. if (vdev->queue && vdev->queue->lock &&
  2024. (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
  2025. return vdev->queue->lock;
  2026. return vdev->lock;
  2027. }
  2028. /* Common ioctl debug function. This function can be used by
  2029. external ioctl messages as well as internal V4L ioctl */
  2030. void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
  2031. {
  2032. const char *dir, *type;
  2033. if (prefix)
  2034. printk(KERN_DEBUG "%s: ", prefix);
  2035. switch (_IOC_TYPE(cmd)) {
  2036. case 'd':
  2037. type = "v4l2_int";
  2038. break;
  2039. case 'V':
  2040. if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
  2041. type = "v4l2";
  2042. break;
  2043. }
  2044. pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
  2045. return;
  2046. default:
  2047. type = "unknown";
  2048. break;
  2049. }
  2050. switch (_IOC_DIR(cmd)) {
  2051. case _IOC_NONE: dir = "--"; break;
  2052. case _IOC_READ: dir = "r-"; break;
  2053. case _IOC_WRITE: dir = "-w"; break;
  2054. case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
  2055. default: dir = "*ERR*"; break;
  2056. }
  2057. pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
  2058. type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
  2059. }
  2060. EXPORT_SYMBOL(v4l_printk_ioctl);
  2061. static long __video_do_ioctl(struct file *file,
  2062. unsigned int cmd, void *arg)
  2063. {
  2064. struct video_device *vfd = video_devdata(file);
  2065. const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
  2066. bool write_only = false;
  2067. struct v4l2_ioctl_info default_info;
  2068. const struct v4l2_ioctl_info *info;
  2069. void *fh = file->private_data;
  2070. struct v4l2_fh *vfh = NULL;
  2071. int dev_debug = vfd->dev_debug;
  2072. long ret = -ENOTTY;
  2073. if (ops == NULL) {
  2074. pr_warn("%s: has no ioctl_ops.\n",
  2075. video_device_node_name(vfd));
  2076. return ret;
  2077. }
  2078. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
  2079. vfh = file->private_data;
  2080. if (v4l2_is_known_ioctl(cmd)) {
  2081. info = &v4l2_ioctls[_IOC_NR(cmd)];
  2082. if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
  2083. !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
  2084. goto done;
  2085. if (vfh && (info->flags & INFO_FL_PRIO)) {
  2086. ret = v4l2_prio_check(vfd->prio, vfh->prio);
  2087. if (ret)
  2088. goto done;
  2089. }
  2090. } else {
  2091. default_info.ioctl = cmd;
  2092. default_info.flags = 0;
  2093. default_info.debug = v4l_print_default;
  2094. info = &default_info;
  2095. }
  2096. write_only = _IOC_DIR(cmd) == _IOC_WRITE;
  2097. if (info->flags & INFO_FL_STD) {
  2098. typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
  2099. const void *p = vfd->ioctl_ops;
  2100. const vidioc_op *vidioc = p + info->u.offset;
  2101. ret = (*vidioc)(file, fh, arg);
  2102. } else if (info->flags & INFO_FL_FUNC) {
  2103. ret = info->u.func(ops, file, fh, arg);
  2104. } else if (!ops->vidioc_default) {
  2105. ret = -ENOTTY;
  2106. } else {
  2107. ret = ops->vidioc_default(file, fh,
  2108. vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
  2109. cmd, arg);
  2110. }
  2111. done:
  2112. if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
  2113. if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
  2114. (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
  2115. return ret;
  2116. v4l_printk_ioctl(video_device_node_name(vfd), cmd);
  2117. if (ret < 0)
  2118. pr_cont(": error %ld", ret);
  2119. if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
  2120. pr_cont("\n");
  2121. else if (_IOC_DIR(cmd) == _IOC_NONE)
  2122. info->debug(arg, write_only);
  2123. else {
  2124. pr_cont(": ");
  2125. info->debug(arg, write_only);
  2126. }
  2127. }
  2128. return ret;
  2129. }
  2130. static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
  2131. void __user **user_ptr, void ***kernel_ptr)
  2132. {
  2133. int ret = 0;
  2134. switch (cmd) {
  2135. case VIDIOC_PREPARE_BUF:
  2136. case VIDIOC_QUERYBUF:
  2137. case VIDIOC_QBUF:
  2138. case VIDIOC_DQBUF: {
  2139. struct v4l2_buffer *buf = parg;
  2140. if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
  2141. if (buf->length > VIDEO_MAX_PLANES) {
  2142. ret = -EINVAL;
  2143. break;
  2144. }
  2145. *user_ptr = (void __user *)buf->m.planes;
  2146. *kernel_ptr = (void **)&buf->m.planes;
  2147. *array_size = sizeof(struct v4l2_plane) * buf->length;
  2148. ret = 1;
  2149. }
  2150. break;
  2151. }
  2152. case VIDIOC_G_EDID:
  2153. case VIDIOC_S_EDID: {
  2154. struct v4l2_edid *edid = parg;
  2155. if (edid->blocks) {
  2156. if (edid->blocks > 256) {
  2157. ret = -EINVAL;
  2158. break;
  2159. }
  2160. *user_ptr = (void __user *)edid->edid;
  2161. *kernel_ptr = (void **)&edid->edid;
  2162. *array_size = edid->blocks * 128;
  2163. ret = 1;
  2164. }
  2165. break;
  2166. }
  2167. case VIDIOC_S_EXT_CTRLS:
  2168. case VIDIOC_G_EXT_CTRLS:
  2169. case VIDIOC_TRY_EXT_CTRLS: {
  2170. struct v4l2_ext_controls *ctrls = parg;
  2171. if (ctrls->count != 0) {
  2172. if (ctrls->count > V4L2_CID_MAX_CTRLS) {
  2173. ret = -EINVAL;
  2174. break;
  2175. }
  2176. *user_ptr = (void __user *)ctrls->controls;
  2177. *kernel_ptr = (void **)&ctrls->controls;
  2178. *array_size = sizeof(struct v4l2_ext_control)
  2179. * ctrls->count;
  2180. ret = 1;
  2181. }
  2182. break;
  2183. }
  2184. }
  2185. return ret;
  2186. }
  2187. long
  2188. video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
  2189. v4l2_kioctl func)
  2190. {
  2191. char sbuf[128];
  2192. void *mbuf = NULL;
  2193. void *parg = (void *)arg;
  2194. long err = -EINVAL;
  2195. bool has_array_args;
  2196. size_t array_size = 0;
  2197. void __user *user_ptr = NULL;
  2198. void **kernel_ptr = NULL;
  2199. /* Copy arguments into temp kernel buffer */
  2200. if (_IOC_DIR(cmd) != _IOC_NONE) {
  2201. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  2202. parg = sbuf;
  2203. } else {
  2204. /* too big to allocate from stack */
  2205. mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  2206. if (NULL == mbuf)
  2207. return -ENOMEM;
  2208. parg = mbuf;
  2209. }
  2210. err = -EFAULT;
  2211. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  2212. unsigned int n = _IOC_SIZE(cmd);
  2213. /*
  2214. * In some cases, only a few fields are used as input,
  2215. * i.e. when the app sets "index" and then the driver
  2216. * fills in the rest of the structure for the thing
  2217. * with that index. We only need to copy up the first
  2218. * non-input field.
  2219. */
  2220. if (v4l2_is_known_ioctl(cmd)) {
  2221. u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
  2222. if (flags & INFO_FL_CLEAR_MASK)
  2223. n = (flags & INFO_FL_CLEAR_MASK) >> 16;
  2224. }
  2225. if (copy_from_user(parg, (void __user *)arg, n))
  2226. goto out;
  2227. /* zero out anything we don't copy from userspace */
  2228. if (n < _IOC_SIZE(cmd))
  2229. memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
  2230. } else {
  2231. /* read-only ioctl */
  2232. memset(parg, 0, _IOC_SIZE(cmd));
  2233. }
  2234. }
  2235. err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
  2236. if (err < 0)
  2237. goto out;
  2238. has_array_args = err;
  2239. if (has_array_args) {
  2240. /*
  2241. * When adding new types of array args, make sure that the
  2242. * parent argument to ioctl (which contains the pointer to the
  2243. * array) fits into sbuf (so that mbuf will still remain
  2244. * unused up to here).
  2245. */
  2246. mbuf = kmalloc(array_size, GFP_KERNEL);
  2247. err = -ENOMEM;
  2248. if (NULL == mbuf)
  2249. goto out_array_args;
  2250. err = -EFAULT;
  2251. if (copy_from_user(mbuf, user_ptr, array_size))
  2252. goto out_array_args;
  2253. *kernel_ptr = mbuf;
  2254. }
  2255. /* Handles IOCTL */
  2256. err = func(file, cmd, parg);
  2257. if (err == -ENOIOCTLCMD)
  2258. err = -ENOTTY;
  2259. if (err == 0) {
  2260. if (cmd == VIDIOC_DQBUF)
  2261. trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
  2262. else if (cmd == VIDIOC_QBUF)
  2263. trace_v4l2_qbuf(video_devdata(file)->minor, parg);
  2264. }
  2265. if (has_array_args) {
  2266. *kernel_ptr = (void __force *)user_ptr;
  2267. if (copy_to_user(user_ptr, mbuf, array_size))
  2268. err = -EFAULT;
  2269. goto out_array_args;
  2270. }
  2271. /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
  2272. results that must be returned. */
  2273. if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
  2274. goto out;
  2275. out_array_args:
  2276. /* Copy results into user buffer */
  2277. switch (_IOC_DIR(cmd)) {
  2278. case _IOC_READ:
  2279. case (_IOC_WRITE | _IOC_READ):
  2280. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  2281. err = -EFAULT;
  2282. break;
  2283. }
  2284. out:
  2285. kfree(mbuf);
  2286. return err;
  2287. }
  2288. EXPORT_SYMBOL(video_usercopy);
  2289. long video_ioctl2(struct file *file,
  2290. unsigned int cmd, unsigned long arg)
  2291. {
  2292. return video_usercopy(file, cmd, arg, __video_do_ioctl);
  2293. }
  2294. EXPORT_SYMBOL(video_ioctl2);