v4l2-ioctl.c 60 KB

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