v4l2-ioctl.c 74 KB

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