v4l2-compat-ioctl32.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /*
  2. * ioctl32.c: Conversion between 32bit and 64bit native ioctls.
  3. * Separated from fs stuff by Arnd Bergmann <arnd@arndb.de>
  4. *
  5. * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
  6. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  7. * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
  8. * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz)
  9. * Copyright (C) 2005 Philippe De Muyter (phdm@macqel.be)
  10. * Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
  11. *
  12. * These routines maintain argument size conversion between 32bit and 64bit
  13. * ioctls.
  14. */
  15. #include <linux/compat.h>
  16. #include <linux/module.h>
  17. #include <linux/videodev2.h>
  18. #include <linux/v4l2-subdev.h>
  19. #include <media/v4l2-dev.h>
  20. #include <media/v4l2-fh.h>
  21. #include <media/v4l2-ctrls.h>
  22. #include <media/v4l2-ioctl.h>
  23. /* Use the same argument order as copy_in_user */
  24. #define assign_in_user(to, from) \
  25. ({ \
  26. typeof(*from) __assign_tmp; \
  27. \
  28. get_user(__assign_tmp, from) || put_user(__assign_tmp, to); \
  29. })
  30. static long native_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  31. {
  32. long ret = -ENOIOCTLCMD;
  33. if (file->f_op->unlocked_ioctl)
  34. ret = file->f_op->unlocked_ioctl(file, cmd, arg);
  35. return ret;
  36. }
  37. struct v4l2_clip32 {
  38. struct v4l2_rect c;
  39. compat_caddr_t next;
  40. };
  41. struct v4l2_window32 {
  42. struct v4l2_rect w;
  43. __u32 field; /* enum v4l2_field */
  44. __u32 chromakey;
  45. compat_caddr_t clips; /* actually struct v4l2_clip32 * */
  46. __u32 clipcount;
  47. compat_caddr_t bitmap;
  48. __u8 global_alpha;
  49. };
  50. static int get_v4l2_window32(struct v4l2_window __user *kp,
  51. struct v4l2_window32 __user *up,
  52. void __user *aux_buf, u32 aux_space)
  53. {
  54. struct v4l2_clip32 __user *uclips;
  55. struct v4l2_clip __user *kclips;
  56. compat_caddr_t p;
  57. u32 clipcount;
  58. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  59. copy_in_user(&kp->w, &up->w, sizeof(up->w)) ||
  60. assign_in_user(&kp->field, &up->field) ||
  61. assign_in_user(&kp->chromakey, &up->chromakey) ||
  62. assign_in_user(&kp->global_alpha, &up->global_alpha) ||
  63. get_user(clipcount, &up->clipcount) ||
  64. put_user(clipcount, &kp->clipcount))
  65. return -EFAULT;
  66. if (clipcount > 2048)
  67. return -EINVAL;
  68. if (!clipcount)
  69. return put_user(NULL, &kp->clips);
  70. if (get_user(p, &up->clips))
  71. return -EFAULT;
  72. uclips = compat_ptr(p);
  73. if (aux_space < clipcount * sizeof(*kclips))
  74. return -EFAULT;
  75. kclips = aux_buf;
  76. if (put_user(kclips, &kp->clips))
  77. return -EFAULT;
  78. while (clipcount--) {
  79. if (copy_in_user(&kclips->c, &uclips->c, sizeof(uclips->c)))
  80. return -EFAULT;
  81. if (put_user(clipcount ? kclips + 1 : NULL, &kclips->next))
  82. return -EFAULT;
  83. uclips++;
  84. kclips++;
  85. }
  86. return 0;
  87. }
  88. static int put_v4l2_window32(struct v4l2_window __user *kp,
  89. struct v4l2_window32 __user *up)
  90. {
  91. struct v4l2_clip __user *kclips;
  92. struct v4l2_clip32 __user *uclips;
  93. compat_caddr_t p;
  94. u32 clipcount;
  95. if (copy_in_user(&up->w, &kp->w, sizeof(kp->w)) ||
  96. assign_in_user(&up->field, &kp->field) ||
  97. assign_in_user(&up->chromakey, &kp->chromakey) ||
  98. assign_in_user(&up->global_alpha, &kp->global_alpha) ||
  99. get_user(clipcount, &kp->clipcount) ||
  100. put_user(clipcount, &up->clipcount))
  101. return -EFAULT;
  102. if (!clipcount)
  103. return 0;
  104. if (get_user(kclips, &kp->clips))
  105. return -EFAULT;
  106. if (get_user(p, &up->clips))
  107. return -EFAULT;
  108. uclips = compat_ptr(p);
  109. while (clipcount--) {
  110. if (copy_in_user(&uclips->c, &kclips->c, sizeof(uclips->c)))
  111. return -EFAULT;
  112. uclips++;
  113. kclips++;
  114. }
  115. return 0;
  116. }
  117. struct v4l2_format32 {
  118. __u32 type; /* enum v4l2_buf_type */
  119. union {
  120. struct v4l2_pix_format pix;
  121. struct v4l2_pix_format_mplane pix_mp;
  122. struct v4l2_window32 win;
  123. struct v4l2_vbi_format vbi;
  124. struct v4l2_sliced_vbi_format sliced;
  125. struct v4l2_sdr_format sdr;
  126. struct v4l2_meta_format meta;
  127. __u8 raw_data[200]; /* user-defined */
  128. } fmt;
  129. };
  130. /**
  131. * struct v4l2_create_buffers32 - VIDIOC_CREATE_BUFS32 argument
  132. * @index: on return, index of the first created buffer
  133. * @count: entry: number of requested buffers,
  134. * return: number of created buffers
  135. * @memory: buffer memory type
  136. * @format: frame format, for which buffers are requested
  137. * @reserved: future extensions
  138. */
  139. struct v4l2_create_buffers32 {
  140. __u32 index;
  141. __u32 count;
  142. __u32 memory; /* enum v4l2_memory */
  143. struct v4l2_format32 format;
  144. __u32 reserved[8];
  145. };
  146. static int __bufsize_v4l2_format(struct v4l2_format32 __user *up, u32 *size)
  147. {
  148. u32 type;
  149. if (get_user(type, &up->type))
  150. return -EFAULT;
  151. switch (type) {
  152. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  153. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
  154. u32 clipcount;
  155. if (get_user(clipcount, &up->fmt.win.clipcount))
  156. return -EFAULT;
  157. if (clipcount > 2048)
  158. return -EINVAL;
  159. *size = clipcount * sizeof(struct v4l2_clip);
  160. return 0;
  161. }
  162. default:
  163. *size = 0;
  164. return 0;
  165. }
  166. }
  167. static int bufsize_v4l2_format(struct v4l2_format32 __user *up, u32 *size)
  168. {
  169. if (!access_ok(VERIFY_READ, up, sizeof(*up)))
  170. return -EFAULT;
  171. return __bufsize_v4l2_format(up, size);
  172. }
  173. static int __get_v4l2_format32(struct v4l2_format __user *kp,
  174. struct v4l2_format32 __user *up,
  175. void __user *aux_buf, u32 aux_space)
  176. {
  177. u32 type;
  178. if (get_user(type, &up->type) || put_user(type, &kp->type))
  179. return -EFAULT;
  180. switch (type) {
  181. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  182. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  183. return copy_in_user(&kp->fmt.pix, &up->fmt.pix,
  184. sizeof(kp->fmt.pix)) ? -EFAULT : 0;
  185. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  186. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  187. return copy_in_user(&kp->fmt.pix_mp, &up->fmt.pix_mp,
  188. sizeof(kp->fmt.pix_mp)) ? -EFAULT : 0;
  189. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  190. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  191. return get_v4l2_window32(&kp->fmt.win, &up->fmt.win,
  192. aux_buf, aux_space);
  193. case V4L2_BUF_TYPE_VBI_CAPTURE:
  194. case V4L2_BUF_TYPE_VBI_OUTPUT:
  195. return copy_in_user(&kp->fmt.vbi, &up->fmt.vbi,
  196. sizeof(kp->fmt.vbi)) ? -EFAULT : 0;
  197. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  198. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  199. return copy_in_user(&kp->fmt.sliced, &up->fmt.sliced,
  200. sizeof(kp->fmt.sliced)) ? -EFAULT : 0;
  201. case V4L2_BUF_TYPE_SDR_CAPTURE:
  202. case V4L2_BUF_TYPE_SDR_OUTPUT:
  203. return copy_in_user(&kp->fmt.sdr, &up->fmt.sdr,
  204. sizeof(kp->fmt.sdr)) ? -EFAULT : 0;
  205. case V4L2_BUF_TYPE_META_CAPTURE:
  206. return copy_in_user(&kp->fmt.meta, &up->fmt.meta,
  207. sizeof(kp->fmt.meta)) ? -EFAULT : 0;
  208. default:
  209. return -EINVAL;
  210. }
  211. }
  212. static int get_v4l2_format32(struct v4l2_format __user *kp,
  213. struct v4l2_format32 __user *up,
  214. void __user *aux_buf, u32 aux_space)
  215. {
  216. if (!access_ok(VERIFY_READ, up, sizeof(*up)))
  217. return -EFAULT;
  218. return __get_v4l2_format32(kp, up, aux_buf, aux_space);
  219. }
  220. static int bufsize_v4l2_create(struct v4l2_create_buffers32 __user *up,
  221. u32 *size)
  222. {
  223. if (!access_ok(VERIFY_READ, up, sizeof(*up)))
  224. return -EFAULT;
  225. return __bufsize_v4l2_format(&up->format, size);
  226. }
  227. static int get_v4l2_create32(struct v4l2_create_buffers __user *kp,
  228. struct v4l2_create_buffers32 __user *up,
  229. void __user *aux_buf, u32 aux_space)
  230. {
  231. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  232. copy_in_user(kp, up,
  233. offsetof(struct v4l2_create_buffers32, format)))
  234. return -EFAULT;
  235. return __get_v4l2_format32(&kp->format, &up->format,
  236. aux_buf, aux_space);
  237. }
  238. static int __put_v4l2_format32(struct v4l2_format __user *kp,
  239. struct v4l2_format32 __user *up)
  240. {
  241. u32 type;
  242. if (get_user(type, &kp->type))
  243. return -EFAULT;
  244. switch (type) {
  245. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  246. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  247. return copy_in_user(&up->fmt.pix, &kp->fmt.pix,
  248. sizeof(kp->fmt.pix)) ? -EFAULT : 0;
  249. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  250. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  251. return copy_in_user(&up->fmt.pix_mp, &kp->fmt.pix_mp,
  252. sizeof(kp->fmt.pix_mp)) ? -EFAULT : 0;
  253. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  254. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  255. return put_v4l2_window32(&kp->fmt.win, &up->fmt.win);
  256. case V4L2_BUF_TYPE_VBI_CAPTURE:
  257. case V4L2_BUF_TYPE_VBI_OUTPUT:
  258. return copy_in_user(&up->fmt.vbi, &kp->fmt.vbi,
  259. sizeof(kp->fmt.vbi)) ? -EFAULT : 0;
  260. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  261. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  262. return copy_in_user(&up->fmt.sliced, &kp->fmt.sliced,
  263. sizeof(kp->fmt.sliced)) ? -EFAULT : 0;
  264. case V4L2_BUF_TYPE_SDR_CAPTURE:
  265. case V4L2_BUF_TYPE_SDR_OUTPUT:
  266. return copy_in_user(&up->fmt.sdr, &kp->fmt.sdr,
  267. sizeof(kp->fmt.sdr)) ? -EFAULT : 0;
  268. case V4L2_BUF_TYPE_META_CAPTURE:
  269. return copy_in_user(&up->fmt.meta, &kp->fmt.meta,
  270. sizeof(kp->fmt.meta)) ? -EFAULT : 0;
  271. default:
  272. return -EINVAL;
  273. }
  274. }
  275. static int put_v4l2_format32(struct v4l2_format __user *kp,
  276. struct v4l2_format32 __user *up)
  277. {
  278. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)))
  279. return -EFAULT;
  280. return __put_v4l2_format32(kp, up);
  281. }
  282. static int put_v4l2_create32(struct v4l2_create_buffers __user *kp,
  283. struct v4l2_create_buffers32 __user *up)
  284. {
  285. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  286. copy_in_user(up, kp,
  287. offsetof(struct v4l2_create_buffers32, format)) ||
  288. copy_in_user(up->reserved, kp->reserved, sizeof(kp->reserved)))
  289. return -EFAULT;
  290. return __put_v4l2_format32(&kp->format, &up->format);
  291. }
  292. struct v4l2_standard32 {
  293. __u32 index;
  294. compat_u64 id;
  295. __u8 name[24];
  296. struct v4l2_fract frameperiod; /* Frames, not fields */
  297. __u32 framelines;
  298. __u32 reserved[4];
  299. };
  300. static int get_v4l2_standard32(struct v4l2_standard __user *kp,
  301. struct v4l2_standard32 __user *up)
  302. {
  303. /* other fields are not set by the user, nor used by the driver */
  304. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  305. assign_in_user(&kp->index, &up->index))
  306. return -EFAULT;
  307. return 0;
  308. }
  309. static int put_v4l2_standard32(struct v4l2_standard __user *kp,
  310. struct v4l2_standard32 __user *up)
  311. {
  312. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  313. assign_in_user(&up->index, &kp->index) ||
  314. assign_in_user(&up->id, &kp->id) ||
  315. copy_in_user(up->name, kp->name, sizeof(up->name)) ||
  316. copy_in_user(&up->frameperiod, &kp->frameperiod,
  317. sizeof(up->frameperiod)) ||
  318. assign_in_user(&up->framelines, &kp->framelines) ||
  319. copy_in_user(up->reserved, kp->reserved, sizeof(up->reserved)))
  320. return -EFAULT;
  321. return 0;
  322. }
  323. struct v4l2_plane32 {
  324. __u32 bytesused;
  325. __u32 length;
  326. union {
  327. __u32 mem_offset;
  328. compat_long_t userptr;
  329. __s32 fd;
  330. } m;
  331. __u32 data_offset;
  332. __u32 reserved[11];
  333. };
  334. struct v4l2_buffer32 {
  335. __u32 index;
  336. __u32 type; /* enum v4l2_buf_type */
  337. __u32 bytesused;
  338. __u32 flags;
  339. __u32 field; /* enum v4l2_field */
  340. struct compat_timeval timestamp;
  341. struct v4l2_timecode timecode;
  342. __u32 sequence;
  343. /* memory location */
  344. __u32 memory; /* enum v4l2_memory */
  345. union {
  346. __u32 offset;
  347. compat_long_t userptr;
  348. compat_caddr_t planes;
  349. __s32 fd;
  350. } m;
  351. __u32 length;
  352. __u32 reserved2;
  353. __u32 reserved;
  354. };
  355. static int get_v4l2_plane32(struct v4l2_plane __user *up,
  356. struct v4l2_plane32 __user *up32,
  357. enum v4l2_memory memory)
  358. {
  359. compat_ulong_t p;
  360. if (copy_in_user(up, up32, 2 * sizeof(__u32)) ||
  361. copy_in_user(&up->data_offset, &up32->data_offset,
  362. sizeof(up->data_offset)))
  363. return -EFAULT;
  364. switch (memory) {
  365. case V4L2_MEMORY_MMAP:
  366. case V4L2_MEMORY_OVERLAY:
  367. if (copy_in_user(&up->m.mem_offset, &up32->m.mem_offset,
  368. sizeof(up32->m.mem_offset)))
  369. return -EFAULT;
  370. break;
  371. case V4L2_MEMORY_USERPTR:
  372. if (get_user(p, &up32->m.userptr) ||
  373. put_user((unsigned long)compat_ptr(p), &up->m.userptr))
  374. return -EFAULT;
  375. break;
  376. case V4L2_MEMORY_DMABUF:
  377. if (copy_in_user(&up->m.fd, &up32->m.fd, sizeof(up32->m.fd)))
  378. return -EFAULT;
  379. break;
  380. }
  381. return 0;
  382. }
  383. static int put_v4l2_plane32(struct v4l2_plane __user *up,
  384. struct v4l2_plane32 __user *up32,
  385. enum v4l2_memory memory)
  386. {
  387. unsigned long p;
  388. if (copy_in_user(up32, up, 2 * sizeof(__u32)) ||
  389. copy_in_user(&up32->data_offset, &up->data_offset,
  390. sizeof(up->data_offset)))
  391. return -EFAULT;
  392. switch (memory) {
  393. case V4L2_MEMORY_MMAP:
  394. case V4L2_MEMORY_OVERLAY:
  395. if (copy_in_user(&up32->m.mem_offset, &up->m.mem_offset,
  396. sizeof(up->m.mem_offset)))
  397. return -EFAULT;
  398. break;
  399. case V4L2_MEMORY_USERPTR:
  400. if (get_user(p, &up->m.userptr) ||
  401. put_user((compat_ulong_t)ptr_to_compat((void __user *)p),
  402. &up32->m.userptr))
  403. return -EFAULT;
  404. break;
  405. case V4L2_MEMORY_DMABUF:
  406. if (copy_in_user(&up32->m.fd, &up->m.fd, sizeof(up->m.fd)))
  407. return -EFAULT;
  408. break;
  409. }
  410. return 0;
  411. }
  412. static int bufsize_v4l2_buffer(struct v4l2_buffer32 __user *up, u32 *size)
  413. {
  414. u32 type;
  415. u32 length;
  416. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  417. get_user(type, &up->type) ||
  418. get_user(length, &up->length))
  419. return -EFAULT;
  420. if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
  421. if (length > VIDEO_MAX_PLANES)
  422. return -EINVAL;
  423. /*
  424. * We don't really care if userspace decides to kill itself
  425. * by passing a very big length value
  426. */
  427. *size = length * sizeof(struct v4l2_plane);
  428. } else {
  429. *size = 0;
  430. }
  431. return 0;
  432. }
  433. static int get_v4l2_buffer32(struct v4l2_buffer __user *kp,
  434. struct v4l2_buffer32 __user *up,
  435. void __user *aux_buf, u32 aux_space)
  436. {
  437. u32 type;
  438. u32 length;
  439. enum v4l2_memory memory;
  440. struct v4l2_plane32 __user *uplane32;
  441. struct v4l2_plane __user *uplane;
  442. compat_caddr_t p;
  443. int ret;
  444. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  445. assign_in_user(&kp->index, &up->index) ||
  446. get_user(type, &up->type) ||
  447. put_user(type, &kp->type) ||
  448. assign_in_user(&kp->flags, &up->flags) ||
  449. get_user(memory, &up->memory) ||
  450. put_user(memory, &kp->memory) ||
  451. get_user(length, &up->length) ||
  452. put_user(length, &kp->length))
  453. return -EFAULT;
  454. if (V4L2_TYPE_IS_OUTPUT(type))
  455. if (assign_in_user(&kp->bytesused, &up->bytesused) ||
  456. assign_in_user(&kp->field, &up->field) ||
  457. assign_in_user(&kp->timestamp.tv_sec,
  458. &up->timestamp.tv_sec) ||
  459. assign_in_user(&kp->timestamp.tv_usec,
  460. &up->timestamp.tv_usec))
  461. return -EFAULT;
  462. if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
  463. u32 num_planes = length;
  464. if (num_planes == 0) {
  465. /*
  466. * num_planes == 0 is legal, e.g. when userspace doesn't
  467. * need planes array on DQBUF
  468. */
  469. return put_user(NULL, &kp->m.planes);
  470. }
  471. if (num_planes > VIDEO_MAX_PLANES)
  472. return -EINVAL;
  473. if (get_user(p, &up->m.planes))
  474. return -EFAULT;
  475. uplane32 = compat_ptr(p);
  476. if (!access_ok(VERIFY_READ, uplane32,
  477. num_planes * sizeof(*uplane32)))
  478. return -EFAULT;
  479. /*
  480. * We don't really care if userspace decides to kill itself
  481. * by passing a very big num_planes value
  482. */
  483. if (aux_space < num_planes * sizeof(*uplane))
  484. return -EFAULT;
  485. uplane = aux_buf;
  486. if (put_user((__force struct v4l2_plane *)uplane,
  487. &kp->m.planes))
  488. return -EFAULT;
  489. while (num_planes--) {
  490. ret = get_v4l2_plane32(uplane, uplane32, memory);
  491. if (ret)
  492. return ret;
  493. uplane++;
  494. uplane32++;
  495. }
  496. } else {
  497. switch (memory) {
  498. case V4L2_MEMORY_MMAP:
  499. case V4L2_MEMORY_OVERLAY:
  500. if (assign_in_user(&kp->m.offset, &up->m.offset))
  501. return -EFAULT;
  502. break;
  503. case V4L2_MEMORY_USERPTR: {
  504. compat_ulong_t userptr;
  505. if (get_user(userptr, &up->m.userptr) ||
  506. put_user((unsigned long)compat_ptr(userptr),
  507. &kp->m.userptr))
  508. return -EFAULT;
  509. break;
  510. }
  511. case V4L2_MEMORY_DMABUF:
  512. if (assign_in_user(&kp->m.fd, &up->m.fd))
  513. return -EFAULT;
  514. break;
  515. }
  516. }
  517. return 0;
  518. }
  519. static int put_v4l2_buffer32(struct v4l2_buffer __user *kp,
  520. struct v4l2_buffer32 __user *up)
  521. {
  522. u32 type;
  523. u32 length;
  524. enum v4l2_memory memory;
  525. struct v4l2_plane32 __user *uplane32;
  526. struct v4l2_plane *uplane;
  527. compat_caddr_t p;
  528. int ret;
  529. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  530. assign_in_user(&up->index, &kp->index) ||
  531. get_user(type, &kp->type) ||
  532. put_user(type, &up->type) ||
  533. assign_in_user(&up->flags, &kp->flags) ||
  534. get_user(memory, &kp->memory) ||
  535. put_user(memory, &up->memory))
  536. return -EFAULT;
  537. if (assign_in_user(&up->bytesused, &kp->bytesused) ||
  538. assign_in_user(&up->field, &kp->field) ||
  539. assign_in_user(&up->timestamp.tv_sec, &kp->timestamp.tv_sec) ||
  540. assign_in_user(&up->timestamp.tv_usec, &kp->timestamp.tv_usec) ||
  541. copy_in_user(&up->timecode, &kp->timecode, sizeof(kp->timecode)) ||
  542. assign_in_user(&up->sequence, &kp->sequence) ||
  543. assign_in_user(&up->reserved2, &kp->reserved2) ||
  544. assign_in_user(&up->reserved, &kp->reserved) ||
  545. get_user(length, &kp->length) ||
  546. put_user(length, &up->length))
  547. return -EFAULT;
  548. if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
  549. u32 num_planes = length;
  550. if (num_planes == 0)
  551. return 0;
  552. /* We need to define uplane without __user, even though
  553. * it does point to data in userspace here. The reason is
  554. * that v4l2-ioctl.c copies it from userspace to kernelspace,
  555. * so its definition in videodev2.h doesn't have a
  556. * __user markup. Defining uplane with __user causes
  557. * smatch warnings, so instead declare it without __user
  558. * and cast it as a userspace pointer to put_v4l2_plane32().
  559. */
  560. if (get_user(uplane, &kp->m.planes))
  561. return -EFAULT;
  562. if (get_user(p, &up->m.planes))
  563. return -EFAULT;
  564. uplane32 = compat_ptr(p);
  565. while (num_planes--) {
  566. ret = put_v4l2_plane32((void __user *)uplane,
  567. uplane32, memory);
  568. if (ret)
  569. return ret;
  570. ++uplane;
  571. ++uplane32;
  572. }
  573. } else {
  574. switch (memory) {
  575. case V4L2_MEMORY_MMAP:
  576. case V4L2_MEMORY_OVERLAY:
  577. if (assign_in_user(&up->m.offset, &kp->m.offset))
  578. return -EFAULT;
  579. break;
  580. case V4L2_MEMORY_USERPTR:
  581. if (assign_in_user(&up->m.userptr, &kp->m.userptr))
  582. return -EFAULT;
  583. break;
  584. case V4L2_MEMORY_DMABUF:
  585. if (assign_in_user(&up->m.fd, &kp->m.fd))
  586. return -EFAULT;
  587. break;
  588. }
  589. }
  590. return 0;
  591. }
  592. struct v4l2_framebuffer32 {
  593. __u32 capability;
  594. __u32 flags;
  595. compat_caddr_t base;
  596. struct {
  597. __u32 width;
  598. __u32 height;
  599. __u32 pixelformat;
  600. __u32 field;
  601. __u32 bytesperline;
  602. __u32 sizeimage;
  603. __u32 colorspace;
  604. __u32 priv;
  605. } fmt;
  606. };
  607. static int get_v4l2_framebuffer32(struct v4l2_framebuffer __user *kp,
  608. struct v4l2_framebuffer32 __user *up)
  609. {
  610. compat_caddr_t tmp;
  611. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  612. get_user(tmp, &up->base) ||
  613. put_user((void __force *)compat_ptr(tmp), &kp->base) ||
  614. assign_in_user(&kp->capability, &up->capability) ||
  615. assign_in_user(&kp->flags, &up->flags) ||
  616. copy_in_user(&kp->fmt, &up->fmt, sizeof(kp->fmt)))
  617. return -EFAULT;
  618. return 0;
  619. }
  620. static int put_v4l2_framebuffer32(struct v4l2_framebuffer __user *kp,
  621. struct v4l2_framebuffer32 __user *up)
  622. {
  623. void *base;
  624. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  625. get_user(base, &kp->base) ||
  626. put_user(ptr_to_compat((void __user *)base), &up->base) ||
  627. assign_in_user(&up->capability, &kp->capability) ||
  628. assign_in_user(&up->flags, &kp->flags) ||
  629. copy_in_user(&up->fmt, &kp->fmt, sizeof(kp->fmt)))
  630. return -EFAULT;
  631. return 0;
  632. }
  633. struct v4l2_input32 {
  634. __u32 index; /* Which input */
  635. __u8 name[32]; /* Label */
  636. __u32 type; /* Type of input */
  637. __u32 audioset; /* Associated audios (bitfield) */
  638. __u32 tuner; /* Associated tuner */
  639. compat_u64 std;
  640. __u32 status;
  641. __u32 capabilities;
  642. __u32 reserved[3];
  643. };
  644. /*
  645. * The 64-bit v4l2_input struct has extra padding at the end of the struct.
  646. * Otherwise it is identical to the 32-bit version.
  647. */
  648. static inline int get_v4l2_input32(struct v4l2_input __user *kp,
  649. struct v4l2_input32 __user *up)
  650. {
  651. if (copy_in_user(kp, up, sizeof(*up)))
  652. return -EFAULT;
  653. return 0;
  654. }
  655. static inline int put_v4l2_input32(struct v4l2_input __user *kp,
  656. struct v4l2_input32 __user *up)
  657. {
  658. if (copy_in_user(up, kp, sizeof(*up)))
  659. return -EFAULT;
  660. return 0;
  661. }
  662. struct v4l2_ext_controls32 {
  663. __u32 which;
  664. __u32 count;
  665. __u32 error_idx;
  666. __u32 reserved[2];
  667. compat_caddr_t controls; /* actually struct v4l2_ext_control32 * */
  668. };
  669. struct v4l2_ext_control32 {
  670. __u32 id;
  671. __u32 size;
  672. __u32 reserved2[1];
  673. union {
  674. __s32 value;
  675. __s64 value64;
  676. compat_caddr_t string; /* actually char * */
  677. };
  678. } __attribute__ ((packed));
  679. /* Return true if this control is a pointer type. */
  680. static inline bool ctrl_is_pointer(struct file *file, u32 id)
  681. {
  682. struct video_device *vdev = video_devdata(file);
  683. struct v4l2_fh *fh = NULL;
  684. struct v4l2_ctrl_handler *hdl = NULL;
  685. struct v4l2_query_ext_ctrl qec = { id };
  686. const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
  687. if (test_bit(V4L2_FL_USES_V4L2_FH, &vdev->flags))
  688. fh = file->private_data;
  689. if (fh && fh->ctrl_handler)
  690. hdl = fh->ctrl_handler;
  691. else if (vdev->ctrl_handler)
  692. hdl = vdev->ctrl_handler;
  693. if (hdl) {
  694. struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, id);
  695. return ctrl && ctrl->is_ptr;
  696. }
  697. if (!ops || !ops->vidioc_query_ext_ctrl)
  698. return false;
  699. return !ops->vidioc_query_ext_ctrl(file, fh, &qec) &&
  700. (qec.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD);
  701. }
  702. static int bufsize_v4l2_ext_controls(struct v4l2_ext_controls32 __user *up,
  703. u32 *size)
  704. {
  705. u32 count;
  706. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  707. get_user(count, &up->count))
  708. return -EFAULT;
  709. if (count > V4L2_CID_MAX_CTRLS)
  710. return -EINVAL;
  711. *size = count * sizeof(struct v4l2_ext_control);
  712. return 0;
  713. }
  714. static int get_v4l2_ext_controls32(struct file *file,
  715. struct v4l2_ext_controls __user *kp,
  716. struct v4l2_ext_controls32 __user *up,
  717. void __user *aux_buf, u32 aux_space)
  718. {
  719. struct v4l2_ext_control32 __user *ucontrols;
  720. struct v4l2_ext_control __user *kcontrols;
  721. u32 count;
  722. u32 n;
  723. compat_caddr_t p;
  724. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  725. assign_in_user(&kp->which, &up->which) ||
  726. get_user(count, &up->count) ||
  727. put_user(count, &kp->count) ||
  728. assign_in_user(&kp->error_idx, &up->error_idx) ||
  729. copy_in_user(kp->reserved, up->reserved, sizeof(kp->reserved)))
  730. return -EFAULT;
  731. if (count == 0)
  732. return put_user(NULL, &kp->controls);
  733. if (count > V4L2_CID_MAX_CTRLS)
  734. return -EINVAL;
  735. if (get_user(p, &up->controls))
  736. return -EFAULT;
  737. ucontrols = compat_ptr(p);
  738. if (!access_ok(VERIFY_READ, ucontrols, count * sizeof(*ucontrols)))
  739. return -EFAULT;
  740. if (aux_space < count * sizeof(*kcontrols))
  741. return -EFAULT;
  742. kcontrols = aux_buf;
  743. if (put_user((__force struct v4l2_ext_control *)kcontrols,
  744. &kp->controls))
  745. return -EFAULT;
  746. for (n = 0; n < count; n++) {
  747. u32 id;
  748. if (copy_in_user(kcontrols, ucontrols, sizeof(*ucontrols)))
  749. return -EFAULT;
  750. if (get_user(id, &kcontrols->id))
  751. return -EFAULT;
  752. if (ctrl_is_pointer(file, id)) {
  753. void __user *s;
  754. if (get_user(p, &ucontrols->string))
  755. return -EFAULT;
  756. s = compat_ptr(p);
  757. if (put_user(s, &kcontrols->string))
  758. return -EFAULT;
  759. }
  760. ucontrols++;
  761. kcontrols++;
  762. }
  763. return 0;
  764. }
  765. static int put_v4l2_ext_controls32(struct file *file,
  766. struct v4l2_ext_controls __user *kp,
  767. struct v4l2_ext_controls32 __user *up)
  768. {
  769. struct v4l2_ext_control32 __user *ucontrols;
  770. struct v4l2_ext_control *kcontrols;
  771. u32 count;
  772. u32 n;
  773. compat_caddr_t p;
  774. /*
  775. * We need to define kcontrols without __user, even though it does
  776. * point to data in userspace here. The reason is that v4l2-ioctl.c
  777. * copies it from userspace to kernelspace, so its definition in
  778. * videodev2.h doesn't have a __user markup. Defining kcontrols
  779. * with __user causes smatch warnings, so instead declare it
  780. * without __user and cast it as a userspace pointer where needed.
  781. */
  782. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  783. assign_in_user(&up->which, &kp->which) ||
  784. get_user(count, &kp->count) ||
  785. put_user(count, &up->count) ||
  786. assign_in_user(&up->error_idx, &kp->error_idx) ||
  787. copy_in_user(up->reserved, kp->reserved, sizeof(up->reserved)) ||
  788. get_user(kcontrols, &kp->controls))
  789. return -EFAULT;
  790. if (!count || count > (U32_MAX/sizeof(*ucontrols)))
  791. return 0;
  792. if (get_user(p, &up->controls))
  793. return -EFAULT;
  794. ucontrols = compat_ptr(p);
  795. if (!access_ok(VERIFY_WRITE, ucontrols, count * sizeof(*ucontrols)))
  796. return -EFAULT;
  797. for (n = 0; n < count; n++) {
  798. unsigned int size = sizeof(*ucontrols);
  799. u32 id;
  800. if (get_user(id, (unsigned int __user *)&kcontrols->id) ||
  801. put_user(id, &ucontrols->id) ||
  802. assign_in_user(&ucontrols->size,
  803. (unsigned int __user *)&kcontrols->size) ||
  804. copy_in_user(&ucontrols->reserved2,
  805. (void __user *)&kcontrols->reserved2,
  806. sizeof(ucontrols->reserved2)))
  807. return -EFAULT;
  808. /*
  809. * Do not modify the pointer when copying a pointer control.
  810. * The contents of the pointer was changed, not the pointer
  811. * itself.
  812. */
  813. if (ctrl_is_pointer(file, id))
  814. size -= sizeof(ucontrols->value64);
  815. if (copy_in_user(ucontrols,
  816. (void __user *)kcontrols, size))
  817. return -EFAULT;
  818. ucontrols++;
  819. kcontrols++;
  820. }
  821. return 0;
  822. }
  823. struct v4l2_event32 {
  824. __u32 type;
  825. union {
  826. compat_s64 value64;
  827. __u8 data[64];
  828. } u;
  829. __u32 pending;
  830. __u32 sequence;
  831. struct compat_timespec timestamp;
  832. __u32 id;
  833. __u32 reserved[8];
  834. };
  835. static int put_v4l2_event32(struct v4l2_event __user *kp,
  836. struct v4l2_event32 __user *up)
  837. {
  838. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  839. assign_in_user(&up->type, &kp->type) ||
  840. copy_in_user(&up->u, &kp->u, sizeof(kp->u)) ||
  841. assign_in_user(&up->pending, &kp->pending) ||
  842. assign_in_user(&up->sequence, &kp->sequence) ||
  843. assign_in_user(&up->timestamp.tv_sec, &kp->timestamp.tv_sec) ||
  844. assign_in_user(&up->timestamp.tv_nsec, &kp->timestamp.tv_nsec) ||
  845. assign_in_user(&up->id, &kp->id) ||
  846. copy_in_user(up->reserved, kp->reserved, sizeof(up->reserved)))
  847. return -EFAULT;
  848. return 0;
  849. }
  850. struct v4l2_edid32 {
  851. __u32 pad;
  852. __u32 start_block;
  853. __u32 blocks;
  854. __u32 reserved[5];
  855. compat_caddr_t edid;
  856. };
  857. static int get_v4l2_edid32(struct v4l2_edid __user *kp,
  858. struct v4l2_edid32 __user *up)
  859. {
  860. compat_uptr_t tmp;
  861. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  862. assign_in_user(&kp->pad, &up->pad) ||
  863. assign_in_user(&kp->start_block, &up->start_block) ||
  864. assign_in_user(&kp->blocks,
  865. (u32 __user *)&up->blocks) ||
  866. get_user(tmp, &up->edid) ||
  867. put_user((void __force *)compat_ptr(tmp), &kp->edid) ||
  868. copy_in_user(kp->reserved, up->reserved, sizeof(kp->reserved)))
  869. return -EFAULT;
  870. return 0;
  871. }
  872. static int put_v4l2_edid32(struct v4l2_edid __user *kp,
  873. struct v4l2_edid32 __user *up)
  874. {
  875. void *edid;
  876. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  877. assign_in_user(&up->pad, &kp->pad) ||
  878. assign_in_user(&up->start_block, &kp->start_block) ||
  879. assign_in_user(&up->blocks, &kp->blocks) ||
  880. get_user(edid, &kp->edid) ||
  881. put_user(ptr_to_compat((void __user *)edid), &up->edid) ||
  882. copy_in_user(up->reserved, kp->reserved, sizeof(up->reserved)))
  883. return -EFAULT;
  884. return 0;
  885. }
  886. #define VIDIOC_G_FMT32 _IOWR('V', 4, struct v4l2_format32)
  887. #define VIDIOC_S_FMT32 _IOWR('V', 5, struct v4l2_format32)
  888. #define VIDIOC_QUERYBUF32 _IOWR('V', 9, struct v4l2_buffer32)
  889. #define VIDIOC_G_FBUF32 _IOR ('V', 10, struct v4l2_framebuffer32)
  890. #define VIDIOC_S_FBUF32 _IOW ('V', 11, struct v4l2_framebuffer32)
  891. #define VIDIOC_QBUF32 _IOWR('V', 15, struct v4l2_buffer32)
  892. #define VIDIOC_DQBUF32 _IOWR('V', 17, struct v4l2_buffer32)
  893. #define VIDIOC_ENUMSTD32 _IOWR('V', 25, struct v4l2_standard32)
  894. #define VIDIOC_ENUMINPUT32 _IOWR('V', 26, struct v4l2_input32)
  895. #define VIDIOC_G_EDID32 _IOWR('V', 40, struct v4l2_edid32)
  896. #define VIDIOC_S_EDID32 _IOWR('V', 41, struct v4l2_edid32)
  897. #define VIDIOC_TRY_FMT32 _IOWR('V', 64, struct v4l2_format32)
  898. #define VIDIOC_G_EXT_CTRLS32 _IOWR('V', 71, struct v4l2_ext_controls32)
  899. #define VIDIOC_S_EXT_CTRLS32 _IOWR('V', 72, struct v4l2_ext_controls32)
  900. #define VIDIOC_TRY_EXT_CTRLS32 _IOWR('V', 73, struct v4l2_ext_controls32)
  901. #define VIDIOC_DQEVENT32 _IOR ('V', 89, struct v4l2_event32)
  902. #define VIDIOC_CREATE_BUFS32 _IOWR('V', 92, struct v4l2_create_buffers32)
  903. #define VIDIOC_PREPARE_BUF32 _IOWR('V', 93, struct v4l2_buffer32)
  904. #define VIDIOC_OVERLAY32 _IOW ('V', 14, s32)
  905. #define VIDIOC_STREAMON32 _IOW ('V', 18, s32)
  906. #define VIDIOC_STREAMOFF32 _IOW ('V', 19, s32)
  907. #define VIDIOC_G_INPUT32 _IOR ('V', 38, s32)
  908. #define VIDIOC_S_INPUT32 _IOWR('V', 39, s32)
  909. #define VIDIOC_G_OUTPUT32 _IOR ('V', 46, s32)
  910. #define VIDIOC_S_OUTPUT32 _IOWR('V', 47, s32)
  911. static int alloc_userspace(unsigned int size, u32 aux_space,
  912. void __user **up_native)
  913. {
  914. *up_native = compat_alloc_user_space(size + aux_space);
  915. if (!*up_native)
  916. return -ENOMEM;
  917. if (clear_user(*up_native, size))
  918. return -EFAULT;
  919. return 0;
  920. }
  921. static long do_video_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  922. {
  923. void __user *up = compat_ptr(arg);
  924. void __user *up_native = NULL;
  925. void __user *aux_buf;
  926. u32 aux_space;
  927. int compatible_arg = 1;
  928. long err = 0;
  929. /* First, convert the command. */
  930. switch (cmd) {
  931. case VIDIOC_G_FMT32: cmd = VIDIOC_G_FMT; break;
  932. case VIDIOC_S_FMT32: cmd = VIDIOC_S_FMT; break;
  933. case VIDIOC_QUERYBUF32: cmd = VIDIOC_QUERYBUF; break;
  934. case VIDIOC_G_FBUF32: cmd = VIDIOC_G_FBUF; break;
  935. case VIDIOC_S_FBUF32: cmd = VIDIOC_S_FBUF; break;
  936. case VIDIOC_QBUF32: cmd = VIDIOC_QBUF; break;
  937. case VIDIOC_DQBUF32: cmd = VIDIOC_DQBUF; break;
  938. case VIDIOC_ENUMSTD32: cmd = VIDIOC_ENUMSTD; break;
  939. case VIDIOC_ENUMINPUT32: cmd = VIDIOC_ENUMINPUT; break;
  940. case VIDIOC_TRY_FMT32: cmd = VIDIOC_TRY_FMT; break;
  941. case VIDIOC_G_EXT_CTRLS32: cmd = VIDIOC_G_EXT_CTRLS; break;
  942. case VIDIOC_S_EXT_CTRLS32: cmd = VIDIOC_S_EXT_CTRLS; break;
  943. case VIDIOC_TRY_EXT_CTRLS32: cmd = VIDIOC_TRY_EXT_CTRLS; break;
  944. case VIDIOC_DQEVENT32: cmd = VIDIOC_DQEVENT; break;
  945. case VIDIOC_OVERLAY32: cmd = VIDIOC_OVERLAY; break;
  946. case VIDIOC_STREAMON32: cmd = VIDIOC_STREAMON; break;
  947. case VIDIOC_STREAMOFF32: cmd = VIDIOC_STREAMOFF; break;
  948. case VIDIOC_G_INPUT32: cmd = VIDIOC_G_INPUT; break;
  949. case VIDIOC_S_INPUT32: cmd = VIDIOC_S_INPUT; break;
  950. case VIDIOC_G_OUTPUT32: cmd = VIDIOC_G_OUTPUT; break;
  951. case VIDIOC_S_OUTPUT32: cmd = VIDIOC_S_OUTPUT; break;
  952. case VIDIOC_CREATE_BUFS32: cmd = VIDIOC_CREATE_BUFS; break;
  953. case VIDIOC_PREPARE_BUF32: cmd = VIDIOC_PREPARE_BUF; break;
  954. case VIDIOC_G_EDID32: cmd = VIDIOC_G_EDID; break;
  955. case VIDIOC_S_EDID32: cmd = VIDIOC_S_EDID; break;
  956. }
  957. switch (cmd) {
  958. case VIDIOC_OVERLAY:
  959. case VIDIOC_STREAMON:
  960. case VIDIOC_STREAMOFF:
  961. case VIDIOC_S_INPUT:
  962. case VIDIOC_S_OUTPUT:
  963. err = alloc_userspace(sizeof(unsigned int), 0, &up_native);
  964. if (!err && assign_in_user((unsigned int __user *)up_native,
  965. (compat_uint_t __user *)up))
  966. err = -EFAULT;
  967. compatible_arg = 0;
  968. break;
  969. case VIDIOC_G_INPUT:
  970. case VIDIOC_G_OUTPUT:
  971. err = alloc_userspace(sizeof(unsigned int), 0, &up_native);
  972. compatible_arg = 0;
  973. break;
  974. case VIDIOC_G_EDID:
  975. case VIDIOC_S_EDID:
  976. err = alloc_userspace(sizeof(struct v4l2_edid), 0, &up_native);
  977. if (!err)
  978. err = get_v4l2_edid32(up_native, up);
  979. compatible_arg = 0;
  980. break;
  981. case VIDIOC_G_FMT:
  982. case VIDIOC_S_FMT:
  983. case VIDIOC_TRY_FMT:
  984. err = bufsize_v4l2_format(up, &aux_space);
  985. if (!err)
  986. err = alloc_userspace(sizeof(struct v4l2_format),
  987. aux_space, &up_native);
  988. if (!err) {
  989. aux_buf = up_native + sizeof(struct v4l2_format);
  990. err = get_v4l2_format32(up_native, up,
  991. aux_buf, aux_space);
  992. }
  993. compatible_arg = 0;
  994. break;
  995. case VIDIOC_CREATE_BUFS:
  996. err = bufsize_v4l2_create(up, &aux_space);
  997. if (!err)
  998. err = alloc_userspace(sizeof(struct v4l2_create_buffers),
  999. aux_space, &up_native);
  1000. if (!err) {
  1001. aux_buf = up_native + sizeof(struct v4l2_create_buffers);
  1002. err = get_v4l2_create32(up_native, up,
  1003. aux_buf, aux_space);
  1004. }
  1005. compatible_arg = 0;
  1006. break;
  1007. case VIDIOC_PREPARE_BUF:
  1008. case VIDIOC_QUERYBUF:
  1009. case VIDIOC_QBUF:
  1010. case VIDIOC_DQBUF:
  1011. err = bufsize_v4l2_buffer(up, &aux_space);
  1012. if (!err)
  1013. err = alloc_userspace(sizeof(struct v4l2_buffer),
  1014. aux_space, &up_native);
  1015. if (!err) {
  1016. aux_buf = up_native + sizeof(struct v4l2_buffer);
  1017. err = get_v4l2_buffer32(up_native, up,
  1018. aux_buf, aux_space);
  1019. }
  1020. compatible_arg = 0;
  1021. break;
  1022. case VIDIOC_S_FBUF:
  1023. err = alloc_userspace(sizeof(struct v4l2_framebuffer), 0,
  1024. &up_native);
  1025. if (!err)
  1026. err = get_v4l2_framebuffer32(up_native, up);
  1027. compatible_arg = 0;
  1028. break;
  1029. case VIDIOC_G_FBUF:
  1030. err = alloc_userspace(sizeof(struct v4l2_framebuffer), 0,
  1031. &up_native);
  1032. compatible_arg = 0;
  1033. break;
  1034. case VIDIOC_ENUMSTD:
  1035. err = alloc_userspace(sizeof(struct v4l2_standard), 0,
  1036. &up_native);
  1037. if (!err)
  1038. err = get_v4l2_standard32(up_native, up);
  1039. compatible_arg = 0;
  1040. break;
  1041. case VIDIOC_ENUMINPUT:
  1042. err = alloc_userspace(sizeof(struct v4l2_input), 0, &up_native);
  1043. if (!err)
  1044. err = get_v4l2_input32(up_native, up);
  1045. compatible_arg = 0;
  1046. break;
  1047. case VIDIOC_G_EXT_CTRLS:
  1048. case VIDIOC_S_EXT_CTRLS:
  1049. case VIDIOC_TRY_EXT_CTRLS:
  1050. err = bufsize_v4l2_ext_controls(up, &aux_space);
  1051. if (!err)
  1052. err = alloc_userspace(sizeof(struct v4l2_ext_controls),
  1053. aux_space, &up_native);
  1054. if (!err) {
  1055. aux_buf = up_native + sizeof(struct v4l2_ext_controls);
  1056. err = get_v4l2_ext_controls32(file, up_native, up,
  1057. aux_buf, aux_space);
  1058. }
  1059. compatible_arg = 0;
  1060. break;
  1061. case VIDIOC_DQEVENT:
  1062. err = alloc_userspace(sizeof(struct v4l2_event), 0, &up_native);
  1063. compatible_arg = 0;
  1064. break;
  1065. }
  1066. if (err)
  1067. return err;
  1068. if (compatible_arg)
  1069. err = native_ioctl(file, cmd, (unsigned long)up);
  1070. else
  1071. err = native_ioctl(file, cmd, (unsigned long)up_native);
  1072. if (err == -ENOTTY)
  1073. return err;
  1074. /*
  1075. * Special case: even after an error we need to put the
  1076. * results back for these ioctls since the error_idx will
  1077. * contain information on which control failed.
  1078. */
  1079. switch (cmd) {
  1080. case VIDIOC_G_EXT_CTRLS:
  1081. case VIDIOC_S_EXT_CTRLS:
  1082. case VIDIOC_TRY_EXT_CTRLS:
  1083. if (put_v4l2_ext_controls32(file, up_native, up))
  1084. err = -EFAULT;
  1085. break;
  1086. case VIDIOC_S_EDID:
  1087. if (put_v4l2_edid32(up_native, up))
  1088. err = -EFAULT;
  1089. break;
  1090. }
  1091. if (err)
  1092. return err;
  1093. switch (cmd) {
  1094. case VIDIOC_S_INPUT:
  1095. case VIDIOC_S_OUTPUT:
  1096. case VIDIOC_G_INPUT:
  1097. case VIDIOC_G_OUTPUT:
  1098. if (assign_in_user((compat_uint_t __user *)up,
  1099. ((unsigned int __user *)up_native)))
  1100. err = -EFAULT;
  1101. break;
  1102. case VIDIOC_G_FBUF:
  1103. err = put_v4l2_framebuffer32(up_native, up);
  1104. break;
  1105. case VIDIOC_DQEVENT:
  1106. err = put_v4l2_event32(up_native, up);
  1107. break;
  1108. case VIDIOC_G_EDID:
  1109. err = put_v4l2_edid32(up_native, up);
  1110. break;
  1111. case VIDIOC_G_FMT:
  1112. case VIDIOC_S_FMT:
  1113. case VIDIOC_TRY_FMT:
  1114. err = put_v4l2_format32(up_native, up);
  1115. break;
  1116. case VIDIOC_CREATE_BUFS:
  1117. err = put_v4l2_create32(up_native, up);
  1118. break;
  1119. case VIDIOC_PREPARE_BUF:
  1120. case VIDIOC_QUERYBUF:
  1121. case VIDIOC_QBUF:
  1122. case VIDIOC_DQBUF:
  1123. err = put_v4l2_buffer32(up_native, up);
  1124. break;
  1125. case VIDIOC_ENUMSTD:
  1126. err = put_v4l2_standard32(up_native, up);
  1127. break;
  1128. case VIDIOC_ENUMINPUT:
  1129. err = put_v4l2_input32(up_native, up);
  1130. break;
  1131. }
  1132. return err;
  1133. }
  1134. long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
  1135. {
  1136. struct video_device *vdev = video_devdata(file);
  1137. long ret = -ENOIOCTLCMD;
  1138. if (!file->f_op->unlocked_ioctl)
  1139. return ret;
  1140. if (_IOC_TYPE(cmd) == 'V' && _IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
  1141. ret = do_video_ioctl(file, cmd, arg);
  1142. else if (vdev->fops->compat_ioctl32)
  1143. ret = vdev->fops->compat_ioctl32(file, cmd, arg);
  1144. if (ret == -ENOIOCTLCMD)
  1145. pr_debug("compat_ioctl32: unknown ioctl '%c', dir=%d, #%d (0x%08x)\n",
  1146. _IOC_TYPE(cmd), _IOC_DIR(cmd), _IOC_NR(cmd), cmd);
  1147. return ret;
  1148. }
  1149. EXPORT_SYMBOL_GPL(v4l2_compat_ioctl32);