v4l2-ioctl.c 69 KB

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