v4l2-ioctl.c 73 KB

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