v4l2-ioctl.c 77 KB

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