v4l2-compat-ioctl32.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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-ioctl.h>
  21. static long native_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  22. {
  23. long ret = -ENOIOCTLCMD;
  24. if (file->f_op->unlocked_ioctl)
  25. ret = file->f_op->unlocked_ioctl(file, cmd, arg);
  26. return ret;
  27. }
  28. struct v4l2_clip32 {
  29. struct v4l2_rect c;
  30. compat_caddr_t next;
  31. };
  32. struct v4l2_window32 {
  33. struct v4l2_rect w;
  34. __u32 field; /* enum v4l2_field */
  35. __u32 chromakey;
  36. compat_caddr_t clips; /* actually struct v4l2_clip32 * */
  37. __u32 clipcount;
  38. compat_caddr_t bitmap;
  39. };
  40. static int get_v4l2_window32(struct v4l2_window *kp, struct v4l2_window32 __user *up)
  41. {
  42. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_window32)) ||
  43. copy_from_user(&kp->w, &up->w, sizeof(up->w)) ||
  44. get_user(kp->field, &up->field) ||
  45. get_user(kp->chromakey, &up->chromakey) ||
  46. get_user(kp->clipcount, &up->clipcount))
  47. return -EFAULT;
  48. if (kp->clipcount > 2048)
  49. return -EINVAL;
  50. if (kp->clipcount) {
  51. struct v4l2_clip32 __user *uclips;
  52. struct v4l2_clip __user *kclips;
  53. int n = kp->clipcount;
  54. compat_caddr_t p;
  55. if (get_user(p, &up->clips))
  56. return -EFAULT;
  57. uclips = compat_ptr(p);
  58. kclips = compat_alloc_user_space(n * sizeof(struct v4l2_clip));
  59. kp->clips = kclips;
  60. while (--n >= 0) {
  61. if (copy_in_user(&kclips->c, &uclips->c, sizeof(uclips->c)))
  62. return -EFAULT;
  63. if (put_user(n ? kclips + 1 : NULL, &kclips->next))
  64. return -EFAULT;
  65. uclips += 1;
  66. kclips += 1;
  67. }
  68. } else
  69. kp->clips = NULL;
  70. return 0;
  71. }
  72. static int put_v4l2_window32(struct v4l2_window *kp, struct v4l2_window32 __user *up)
  73. {
  74. if (copy_to_user(&up->w, &kp->w, sizeof(kp->w)) ||
  75. put_user(kp->field, &up->field) ||
  76. put_user(kp->chromakey, &up->chromakey) ||
  77. put_user(kp->clipcount, &up->clipcount))
  78. return -EFAULT;
  79. return 0;
  80. }
  81. static inline int get_v4l2_pix_format(struct v4l2_pix_format *kp, struct v4l2_pix_format __user *up)
  82. {
  83. if (copy_from_user(kp, up, sizeof(struct v4l2_pix_format)))
  84. return -EFAULT;
  85. return 0;
  86. }
  87. static inline int get_v4l2_pix_format_mplane(struct v4l2_pix_format_mplane *kp,
  88. struct v4l2_pix_format_mplane __user *up)
  89. {
  90. if (copy_from_user(kp, up, sizeof(struct v4l2_pix_format_mplane)))
  91. return -EFAULT;
  92. return 0;
  93. }
  94. static inline int put_v4l2_pix_format(struct v4l2_pix_format *kp, struct v4l2_pix_format __user *up)
  95. {
  96. if (copy_to_user(up, kp, sizeof(struct v4l2_pix_format)))
  97. return -EFAULT;
  98. return 0;
  99. }
  100. static inline int put_v4l2_pix_format_mplane(struct v4l2_pix_format_mplane *kp,
  101. struct v4l2_pix_format_mplane __user *up)
  102. {
  103. if (copy_to_user(up, kp, sizeof(struct v4l2_pix_format_mplane)))
  104. return -EFAULT;
  105. return 0;
  106. }
  107. static inline int get_v4l2_vbi_format(struct v4l2_vbi_format *kp, struct v4l2_vbi_format __user *up)
  108. {
  109. if (copy_from_user(kp, up, sizeof(struct v4l2_vbi_format)))
  110. return -EFAULT;
  111. return 0;
  112. }
  113. static inline int put_v4l2_vbi_format(struct v4l2_vbi_format *kp, struct v4l2_vbi_format __user *up)
  114. {
  115. if (copy_to_user(up, kp, sizeof(struct v4l2_vbi_format)))
  116. return -EFAULT;
  117. return 0;
  118. }
  119. static inline int get_v4l2_sliced_vbi_format(struct v4l2_sliced_vbi_format *kp, struct v4l2_sliced_vbi_format __user *up)
  120. {
  121. if (copy_from_user(kp, up, sizeof(struct v4l2_sliced_vbi_format)))
  122. return -EFAULT;
  123. return 0;
  124. }
  125. static inline int put_v4l2_sliced_vbi_format(struct v4l2_sliced_vbi_format *kp, struct v4l2_sliced_vbi_format __user *up)
  126. {
  127. if (copy_to_user(up, kp, sizeof(struct v4l2_sliced_vbi_format)))
  128. return -EFAULT;
  129. return 0;
  130. }
  131. static inline int get_v4l2_sdr_format(struct v4l2_sdr_format *kp, struct v4l2_sdr_format __user *up)
  132. {
  133. if (copy_from_user(kp, up, sizeof(struct v4l2_sdr_format)))
  134. return -EFAULT;
  135. return 0;
  136. }
  137. static inline int put_v4l2_sdr_format(struct v4l2_sdr_format *kp, struct v4l2_sdr_format __user *up)
  138. {
  139. if (copy_to_user(up, kp, sizeof(struct v4l2_sdr_format)))
  140. return -EFAULT;
  141. return 0;
  142. }
  143. struct v4l2_format32 {
  144. __u32 type; /* enum v4l2_buf_type */
  145. union {
  146. struct v4l2_pix_format pix;
  147. struct v4l2_pix_format_mplane pix_mp;
  148. struct v4l2_window32 win;
  149. struct v4l2_vbi_format vbi;
  150. struct v4l2_sliced_vbi_format sliced;
  151. struct v4l2_sdr_format sdr;
  152. __u8 raw_data[200]; /* user-defined */
  153. } fmt;
  154. };
  155. /**
  156. * struct v4l2_create_buffers32 - VIDIOC_CREATE_BUFS32 argument
  157. * @index: on return, index of the first created buffer
  158. * @count: entry: number of requested buffers,
  159. * return: number of created buffers
  160. * @memory: buffer memory type
  161. * @format: frame format, for which buffers are requested
  162. * @reserved: future extensions
  163. */
  164. struct v4l2_create_buffers32 {
  165. __u32 index;
  166. __u32 count;
  167. __u32 memory; /* enum v4l2_memory */
  168. struct v4l2_format32 format;
  169. __u32 reserved[8];
  170. };
  171. static int __get_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
  172. {
  173. if (get_user(kp->type, &up->type))
  174. return -EFAULT;
  175. switch (kp->type) {
  176. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  177. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  178. return get_v4l2_pix_format(&kp->fmt.pix, &up->fmt.pix);
  179. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  180. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  181. return get_v4l2_pix_format_mplane(&kp->fmt.pix_mp,
  182. &up->fmt.pix_mp);
  183. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  184. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  185. return get_v4l2_window32(&kp->fmt.win, &up->fmt.win);
  186. case V4L2_BUF_TYPE_VBI_CAPTURE:
  187. case V4L2_BUF_TYPE_VBI_OUTPUT:
  188. return get_v4l2_vbi_format(&kp->fmt.vbi, &up->fmt.vbi);
  189. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  190. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  191. return get_v4l2_sliced_vbi_format(&kp->fmt.sliced, &up->fmt.sliced);
  192. case V4L2_BUF_TYPE_SDR_CAPTURE:
  193. case V4L2_BUF_TYPE_SDR_OUTPUT:
  194. return get_v4l2_sdr_format(&kp->fmt.sdr, &up->fmt.sdr);
  195. default:
  196. pr_info("compat_ioctl32: unexpected VIDIOC_FMT type %d\n",
  197. kp->type);
  198. return -EINVAL;
  199. }
  200. }
  201. static int get_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
  202. {
  203. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_format32)))
  204. return -EFAULT;
  205. return __get_v4l2_format32(kp, up);
  206. }
  207. static int get_v4l2_create32(struct v4l2_create_buffers *kp, struct v4l2_create_buffers32 __user *up)
  208. {
  209. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_create_buffers32)) ||
  210. copy_from_user(kp, up, offsetof(struct v4l2_create_buffers32, format)))
  211. return -EFAULT;
  212. return __get_v4l2_format32(&kp->format, &up->format);
  213. }
  214. static int __put_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
  215. {
  216. if (put_user(kp->type, &up->type))
  217. return -EFAULT;
  218. switch (kp->type) {
  219. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  220. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  221. return put_v4l2_pix_format(&kp->fmt.pix, &up->fmt.pix);
  222. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  223. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  224. return put_v4l2_pix_format_mplane(&kp->fmt.pix_mp,
  225. &up->fmt.pix_mp);
  226. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  227. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  228. return put_v4l2_window32(&kp->fmt.win, &up->fmt.win);
  229. case V4L2_BUF_TYPE_VBI_CAPTURE:
  230. case V4L2_BUF_TYPE_VBI_OUTPUT:
  231. return put_v4l2_vbi_format(&kp->fmt.vbi, &up->fmt.vbi);
  232. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  233. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  234. return put_v4l2_sliced_vbi_format(&kp->fmt.sliced, &up->fmt.sliced);
  235. case V4L2_BUF_TYPE_SDR_CAPTURE:
  236. case V4L2_BUF_TYPE_SDR_OUTPUT:
  237. return put_v4l2_sdr_format(&kp->fmt.sdr, &up->fmt.sdr);
  238. default:
  239. pr_info("compat_ioctl32: unexpected VIDIOC_FMT type %d\n",
  240. kp->type);
  241. return -EINVAL;
  242. }
  243. }
  244. static int put_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
  245. {
  246. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_format32)))
  247. return -EFAULT;
  248. return __put_v4l2_format32(kp, up);
  249. }
  250. static int put_v4l2_create32(struct v4l2_create_buffers *kp, struct v4l2_create_buffers32 __user *up)
  251. {
  252. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_create_buffers32)) ||
  253. copy_to_user(up, kp, offsetof(struct v4l2_create_buffers32, format)) ||
  254. copy_to_user(up->reserved, kp->reserved, sizeof(kp->reserved)))
  255. return -EFAULT;
  256. return __put_v4l2_format32(&kp->format, &up->format);
  257. }
  258. struct v4l2_standard32 {
  259. __u32 index;
  260. compat_u64 id;
  261. __u8 name[24];
  262. struct v4l2_fract frameperiod; /* Frames, not fields */
  263. __u32 framelines;
  264. __u32 reserved[4];
  265. };
  266. static int get_v4l2_standard32(struct v4l2_standard *kp, struct v4l2_standard32 __user *up)
  267. {
  268. /* other fields are not set by the user, nor used by the driver */
  269. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_standard32)) ||
  270. get_user(kp->index, &up->index))
  271. return -EFAULT;
  272. return 0;
  273. }
  274. static int put_v4l2_standard32(struct v4l2_standard *kp, struct v4l2_standard32 __user *up)
  275. {
  276. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_standard32)) ||
  277. put_user(kp->index, &up->index) ||
  278. put_user(kp->id, &up->id) ||
  279. copy_to_user(up->name, kp->name, 24) ||
  280. copy_to_user(&up->frameperiod, &kp->frameperiod, sizeof(kp->frameperiod)) ||
  281. put_user(kp->framelines, &up->framelines) ||
  282. copy_to_user(up->reserved, kp->reserved, 4 * sizeof(__u32)))
  283. return -EFAULT;
  284. return 0;
  285. }
  286. struct v4l2_plane32 {
  287. __u32 bytesused;
  288. __u32 length;
  289. union {
  290. __u32 mem_offset;
  291. compat_long_t userptr;
  292. __s32 fd;
  293. } m;
  294. __u32 data_offset;
  295. __u32 reserved[11];
  296. };
  297. struct v4l2_buffer32 {
  298. __u32 index;
  299. __u32 type; /* enum v4l2_buf_type */
  300. __u32 bytesused;
  301. __u32 flags;
  302. __u32 field; /* enum v4l2_field */
  303. struct compat_timeval timestamp;
  304. struct v4l2_timecode timecode;
  305. __u32 sequence;
  306. /* memory location */
  307. __u32 memory; /* enum v4l2_memory */
  308. union {
  309. __u32 offset;
  310. compat_long_t userptr;
  311. compat_caddr_t planes;
  312. __s32 fd;
  313. } m;
  314. __u32 length;
  315. __u32 reserved2;
  316. __u32 reserved;
  317. };
  318. static int get_v4l2_plane32(struct v4l2_plane __user *up, struct v4l2_plane32 __user *up32,
  319. enum v4l2_memory memory)
  320. {
  321. void __user *up_pln;
  322. compat_long_t p;
  323. if (copy_in_user(up, up32, 2 * sizeof(__u32)) ||
  324. copy_in_user(&up->data_offset, &up32->data_offset,
  325. sizeof(__u32)))
  326. return -EFAULT;
  327. if (memory == V4L2_MEMORY_USERPTR) {
  328. if (get_user(p, &up32->m.userptr))
  329. return -EFAULT;
  330. up_pln = compat_ptr(p);
  331. if (put_user((unsigned long)up_pln, &up->m.userptr))
  332. return -EFAULT;
  333. } else if (memory == V4L2_MEMORY_DMABUF) {
  334. if (copy_in_user(&up->m.fd, &up32->m.fd, sizeof(int)))
  335. return -EFAULT;
  336. } else {
  337. if (copy_in_user(&up->m.mem_offset, &up32->m.mem_offset,
  338. sizeof(__u32)))
  339. return -EFAULT;
  340. }
  341. return 0;
  342. }
  343. static int put_v4l2_plane32(struct v4l2_plane __user *up, struct v4l2_plane32 __user *up32,
  344. enum v4l2_memory memory)
  345. {
  346. if (copy_in_user(up32, up, 2 * sizeof(__u32)) ||
  347. copy_in_user(&up32->data_offset, &up->data_offset,
  348. sizeof(__u32)))
  349. return -EFAULT;
  350. /* For MMAP, driver might've set up the offset, so copy it back.
  351. * USERPTR stays the same (was userspace-provided), so no copying. */
  352. if (memory == V4L2_MEMORY_MMAP)
  353. if (copy_in_user(&up32->m.mem_offset, &up->m.mem_offset,
  354. sizeof(__u32)))
  355. return -EFAULT;
  356. /* For DMABUF, driver might've set up the fd, so copy it back. */
  357. if (memory == V4L2_MEMORY_DMABUF)
  358. if (copy_in_user(&up32->m.fd, &up->m.fd,
  359. sizeof(int)))
  360. return -EFAULT;
  361. return 0;
  362. }
  363. static int get_v4l2_buffer32(struct v4l2_buffer *kp, struct v4l2_buffer32 __user *up)
  364. {
  365. struct v4l2_plane32 __user *uplane32;
  366. struct v4l2_plane __user *uplane;
  367. compat_caddr_t p;
  368. int ret;
  369. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_buffer32)) ||
  370. get_user(kp->index, &up->index) ||
  371. get_user(kp->type, &up->type) ||
  372. get_user(kp->flags, &up->flags) ||
  373. get_user(kp->memory, &up->memory) ||
  374. get_user(kp->length, &up->length))
  375. return -EFAULT;
  376. if (V4L2_TYPE_IS_OUTPUT(kp->type))
  377. if (get_user(kp->bytesused, &up->bytesused) ||
  378. get_user(kp->field, &up->field) ||
  379. get_user(kp->timestamp.tv_sec, &up->timestamp.tv_sec) ||
  380. get_user(kp->timestamp.tv_usec,
  381. &up->timestamp.tv_usec))
  382. return -EFAULT;
  383. if (V4L2_TYPE_IS_MULTIPLANAR(kp->type)) {
  384. unsigned int num_planes;
  385. if (kp->length == 0) {
  386. kp->m.planes = NULL;
  387. /* num_planes == 0 is legal, e.g. when userspace doesn't
  388. * need planes array on DQBUF*/
  389. return 0;
  390. } else if (kp->length > VIDEO_MAX_PLANES) {
  391. return -EINVAL;
  392. }
  393. if (get_user(p, &up->m.planes))
  394. return -EFAULT;
  395. uplane32 = compat_ptr(p);
  396. if (!access_ok(VERIFY_READ, uplane32,
  397. kp->length * sizeof(struct v4l2_plane32)))
  398. return -EFAULT;
  399. /* We don't really care if userspace decides to kill itself
  400. * by passing a very big num_planes value */
  401. uplane = compat_alloc_user_space(kp->length *
  402. sizeof(struct v4l2_plane));
  403. kp->m.planes = (__force struct v4l2_plane *)uplane;
  404. for (num_planes = 0; num_planes < kp->length; num_planes++) {
  405. ret = get_v4l2_plane32(uplane, uplane32, kp->memory);
  406. if (ret)
  407. return ret;
  408. ++uplane;
  409. ++uplane32;
  410. }
  411. } else {
  412. switch (kp->memory) {
  413. case V4L2_MEMORY_MMAP:
  414. if (get_user(kp->m.offset, &up->m.offset))
  415. return -EFAULT;
  416. break;
  417. case V4L2_MEMORY_USERPTR:
  418. {
  419. compat_long_t tmp;
  420. if (get_user(tmp, &up->m.userptr))
  421. return -EFAULT;
  422. kp->m.userptr = (unsigned long)compat_ptr(tmp);
  423. }
  424. break;
  425. case V4L2_MEMORY_OVERLAY:
  426. if (get_user(kp->m.offset, &up->m.offset))
  427. return -EFAULT;
  428. break;
  429. case V4L2_MEMORY_DMABUF:
  430. if (get_user(kp->m.fd, &up->m.fd))
  431. return -EFAULT;
  432. break;
  433. }
  434. }
  435. return 0;
  436. }
  437. static int put_v4l2_buffer32(struct v4l2_buffer *kp, struct v4l2_buffer32 __user *up)
  438. {
  439. struct v4l2_plane32 __user *uplane32;
  440. struct v4l2_plane __user *uplane;
  441. compat_caddr_t p;
  442. int num_planes;
  443. int ret;
  444. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_buffer32)) ||
  445. put_user(kp->index, &up->index) ||
  446. put_user(kp->type, &up->type) ||
  447. put_user(kp->flags, &up->flags) ||
  448. put_user(kp->memory, &up->memory))
  449. return -EFAULT;
  450. if (put_user(kp->bytesused, &up->bytesused) ||
  451. put_user(kp->field, &up->field) ||
  452. put_user(kp->timestamp.tv_sec, &up->timestamp.tv_sec) ||
  453. put_user(kp->timestamp.tv_usec, &up->timestamp.tv_usec) ||
  454. copy_to_user(&up->timecode, &kp->timecode, sizeof(struct v4l2_timecode)) ||
  455. put_user(kp->sequence, &up->sequence) ||
  456. put_user(kp->reserved2, &up->reserved2) ||
  457. put_user(kp->reserved, &up->reserved) ||
  458. put_user(kp->length, &up->length))
  459. return -EFAULT;
  460. if (V4L2_TYPE_IS_MULTIPLANAR(kp->type)) {
  461. num_planes = kp->length;
  462. if (num_planes == 0)
  463. return 0;
  464. uplane = (__force struct v4l2_plane __user *)kp->m.planes;
  465. if (get_user(p, &up->m.planes))
  466. return -EFAULT;
  467. uplane32 = compat_ptr(p);
  468. while (--num_planes >= 0) {
  469. ret = put_v4l2_plane32(uplane, uplane32, kp->memory);
  470. if (ret)
  471. return ret;
  472. ++uplane;
  473. ++uplane32;
  474. }
  475. } else {
  476. switch (kp->memory) {
  477. case V4L2_MEMORY_MMAP:
  478. if (put_user(kp->m.offset, &up->m.offset))
  479. return -EFAULT;
  480. break;
  481. case V4L2_MEMORY_USERPTR:
  482. if (put_user(kp->m.userptr, &up->m.userptr))
  483. return -EFAULT;
  484. break;
  485. case V4L2_MEMORY_OVERLAY:
  486. if (put_user(kp->m.offset, &up->m.offset))
  487. return -EFAULT;
  488. break;
  489. case V4L2_MEMORY_DMABUF:
  490. if (put_user(kp->m.fd, &up->m.fd))
  491. return -EFAULT;
  492. break;
  493. }
  494. }
  495. return 0;
  496. }
  497. struct v4l2_framebuffer32 {
  498. __u32 capability;
  499. __u32 flags;
  500. compat_caddr_t base;
  501. struct {
  502. __u32 width;
  503. __u32 height;
  504. __u32 pixelformat;
  505. __u32 field;
  506. __u32 bytesperline;
  507. __u32 sizeimage;
  508. __u32 colorspace;
  509. __u32 priv;
  510. } fmt;
  511. };
  512. static int get_v4l2_framebuffer32(struct v4l2_framebuffer *kp, struct v4l2_framebuffer32 __user *up)
  513. {
  514. u32 tmp;
  515. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_framebuffer32)) ||
  516. get_user(tmp, &up->base) ||
  517. get_user(kp->capability, &up->capability) ||
  518. get_user(kp->flags, &up->flags) ||
  519. copy_from_user(&kp->fmt, &up->fmt, sizeof(up->fmt)))
  520. return -EFAULT;
  521. kp->base = (__force void *)compat_ptr(tmp);
  522. return 0;
  523. }
  524. static int put_v4l2_framebuffer32(struct v4l2_framebuffer *kp, struct v4l2_framebuffer32 __user *up)
  525. {
  526. u32 tmp = (u32)((unsigned long)kp->base);
  527. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_framebuffer32)) ||
  528. put_user(tmp, &up->base) ||
  529. put_user(kp->capability, &up->capability) ||
  530. put_user(kp->flags, &up->flags) ||
  531. copy_to_user(&up->fmt, &kp->fmt, sizeof(up->fmt)))
  532. return -EFAULT;
  533. return 0;
  534. }
  535. struct v4l2_input32 {
  536. __u32 index; /* Which input */
  537. __u8 name[32]; /* Label */
  538. __u32 type; /* Type of input */
  539. __u32 audioset; /* Associated audios (bitfield) */
  540. __u32 tuner; /* Associated tuner */
  541. compat_u64 std;
  542. __u32 status;
  543. __u32 reserved[4];
  544. };
  545. /* The 64-bit v4l2_input struct has extra padding at the end of the struct.
  546. Otherwise it is identical to the 32-bit version. */
  547. static inline int get_v4l2_input32(struct v4l2_input *kp, struct v4l2_input32 __user *up)
  548. {
  549. if (copy_from_user(kp, up, sizeof(struct v4l2_input32)))
  550. return -EFAULT;
  551. return 0;
  552. }
  553. static inline int put_v4l2_input32(struct v4l2_input *kp, struct v4l2_input32 __user *up)
  554. {
  555. if (copy_to_user(up, kp, sizeof(struct v4l2_input32)))
  556. return -EFAULT;
  557. return 0;
  558. }
  559. struct v4l2_ext_controls32 {
  560. __u32 which;
  561. __u32 count;
  562. __u32 error_idx;
  563. __u32 reserved[2];
  564. compat_caddr_t controls; /* actually struct v4l2_ext_control32 * */
  565. };
  566. struct v4l2_ext_control32 {
  567. __u32 id;
  568. __u32 size;
  569. __u32 reserved2[1];
  570. union {
  571. __s32 value;
  572. __s64 value64;
  573. compat_caddr_t string; /* actually char * */
  574. };
  575. } __attribute__ ((packed));
  576. /* The following function really belong in v4l2-common, but that causes
  577. a circular dependency between modules. We need to think about this, but
  578. for now this will do. */
  579. /* Return non-zero if this control is a pointer type. Currently only
  580. type STRING is a pointer type. */
  581. static inline int ctrl_is_pointer(u32 id)
  582. {
  583. switch (id) {
  584. case V4L2_CID_RDS_TX_PS_NAME:
  585. case V4L2_CID_RDS_TX_RADIO_TEXT:
  586. return 1;
  587. default:
  588. return 0;
  589. }
  590. }
  591. static int get_v4l2_ext_controls32(struct v4l2_ext_controls *kp, struct v4l2_ext_controls32 __user *up)
  592. {
  593. struct v4l2_ext_control32 __user *ucontrols;
  594. struct v4l2_ext_control __user *kcontrols;
  595. unsigned int n;
  596. compat_caddr_t p;
  597. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_ext_controls32)) ||
  598. get_user(kp->which, &up->which) ||
  599. get_user(kp->count, &up->count) ||
  600. get_user(kp->error_idx, &up->error_idx) ||
  601. copy_from_user(kp->reserved, up->reserved,
  602. sizeof(kp->reserved)))
  603. return -EFAULT;
  604. if (kp->count == 0) {
  605. kp->controls = NULL;
  606. return 0;
  607. } else if (kp->count > V4L2_CID_MAX_CTRLS) {
  608. return -EINVAL;
  609. }
  610. if (get_user(p, &up->controls))
  611. return -EFAULT;
  612. ucontrols = compat_ptr(p);
  613. if (!access_ok(VERIFY_READ, ucontrols,
  614. kp->count * sizeof(struct v4l2_ext_control32)))
  615. return -EFAULT;
  616. kcontrols = compat_alloc_user_space(kp->count *
  617. sizeof(struct v4l2_ext_control));
  618. kp->controls = (__force struct v4l2_ext_control *)kcontrols;
  619. for (n = 0; n < kp->count; n++) {
  620. u32 id;
  621. if (copy_in_user(kcontrols, ucontrols, sizeof(*ucontrols)))
  622. return -EFAULT;
  623. if (get_user(id, &kcontrols->id))
  624. return -EFAULT;
  625. if (ctrl_is_pointer(id)) {
  626. void __user *s;
  627. if (get_user(p, &ucontrols->string))
  628. return -EFAULT;
  629. s = compat_ptr(p);
  630. if (put_user(s, &kcontrols->string))
  631. return -EFAULT;
  632. }
  633. ucontrols++;
  634. kcontrols++;
  635. }
  636. return 0;
  637. }
  638. static int put_v4l2_ext_controls32(struct v4l2_ext_controls *kp, struct v4l2_ext_controls32 __user *up)
  639. {
  640. struct v4l2_ext_control32 __user *ucontrols;
  641. struct v4l2_ext_control __user *kcontrols =
  642. (__force struct v4l2_ext_control __user *)kp->controls;
  643. int n = kp->count;
  644. compat_caddr_t p;
  645. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_ext_controls32)) ||
  646. put_user(kp->which, &up->which) ||
  647. put_user(kp->count, &up->count) ||
  648. put_user(kp->error_idx, &up->error_idx) ||
  649. copy_to_user(up->reserved, kp->reserved, sizeof(up->reserved)))
  650. return -EFAULT;
  651. if (!kp->count)
  652. return 0;
  653. if (get_user(p, &up->controls))
  654. return -EFAULT;
  655. ucontrols = compat_ptr(p);
  656. if (!access_ok(VERIFY_WRITE, ucontrols,
  657. n * sizeof(struct v4l2_ext_control32)))
  658. return -EFAULT;
  659. while (--n >= 0) {
  660. unsigned size = sizeof(*ucontrols);
  661. u32 id;
  662. if (get_user(id, &kcontrols->id))
  663. return -EFAULT;
  664. /* Do not modify the pointer when copying a pointer control.
  665. The contents of the pointer was changed, not the pointer
  666. itself. */
  667. if (ctrl_is_pointer(id))
  668. size -= sizeof(ucontrols->value64);
  669. if (copy_in_user(ucontrols, kcontrols, size))
  670. return -EFAULT;
  671. ucontrols++;
  672. kcontrols++;
  673. }
  674. return 0;
  675. }
  676. struct v4l2_event32 {
  677. __u32 type;
  678. union {
  679. compat_s64 value64;
  680. __u8 data[64];
  681. } u;
  682. __u32 pending;
  683. __u32 sequence;
  684. struct compat_timespec timestamp;
  685. __u32 id;
  686. __u32 reserved[8];
  687. };
  688. static int put_v4l2_event32(struct v4l2_event *kp, struct v4l2_event32 __user *up)
  689. {
  690. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_event32)) ||
  691. put_user(kp->type, &up->type) ||
  692. copy_to_user(&up->u, &kp->u, sizeof(kp->u)) ||
  693. put_user(kp->pending, &up->pending) ||
  694. put_user(kp->sequence, &up->sequence) ||
  695. compat_put_timespec(&kp->timestamp, &up->timestamp) ||
  696. put_user(kp->id, &up->id) ||
  697. copy_to_user(up->reserved, kp->reserved, 8 * sizeof(__u32)))
  698. return -EFAULT;
  699. return 0;
  700. }
  701. struct v4l2_edid32 {
  702. __u32 pad;
  703. __u32 start_block;
  704. __u32 blocks;
  705. __u32 reserved[5];
  706. compat_caddr_t edid;
  707. };
  708. static int get_v4l2_edid32(struct v4l2_edid *kp, struct v4l2_edid32 __user *up)
  709. {
  710. u32 tmp;
  711. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_edid32)) ||
  712. get_user(kp->pad, &up->pad) ||
  713. get_user(kp->start_block, &up->start_block) ||
  714. get_user(kp->blocks, &up->blocks) ||
  715. get_user(tmp, &up->edid) ||
  716. copy_from_user(kp->reserved, up->reserved, sizeof(kp->reserved)))
  717. return -EFAULT;
  718. kp->edid = (__force u8 *)compat_ptr(tmp);
  719. return 0;
  720. }
  721. static int put_v4l2_edid32(struct v4l2_edid *kp, struct v4l2_edid32 __user *up)
  722. {
  723. u32 tmp = (u32)((unsigned long)kp->edid);
  724. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_edid32)) ||
  725. put_user(kp->pad, &up->pad) ||
  726. put_user(kp->start_block, &up->start_block) ||
  727. put_user(kp->blocks, &up->blocks) ||
  728. put_user(tmp, &up->edid) ||
  729. copy_to_user(up->reserved, kp->reserved, sizeof(up->reserved)))
  730. return -EFAULT;
  731. return 0;
  732. }
  733. #define VIDIOC_G_FMT32 _IOWR('V', 4, struct v4l2_format32)
  734. #define VIDIOC_S_FMT32 _IOWR('V', 5, struct v4l2_format32)
  735. #define VIDIOC_QUERYBUF32 _IOWR('V', 9, struct v4l2_buffer32)
  736. #define VIDIOC_G_FBUF32 _IOR ('V', 10, struct v4l2_framebuffer32)
  737. #define VIDIOC_S_FBUF32 _IOW ('V', 11, struct v4l2_framebuffer32)
  738. #define VIDIOC_QBUF32 _IOWR('V', 15, struct v4l2_buffer32)
  739. #define VIDIOC_DQBUF32 _IOWR('V', 17, struct v4l2_buffer32)
  740. #define VIDIOC_ENUMSTD32 _IOWR('V', 25, struct v4l2_standard32)
  741. #define VIDIOC_ENUMINPUT32 _IOWR('V', 26, struct v4l2_input32)
  742. #define VIDIOC_G_EDID32 _IOWR('V', 40, struct v4l2_edid32)
  743. #define VIDIOC_S_EDID32 _IOWR('V', 41, struct v4l2_edid32)
  744. #define VIDIOC_TRY_FMT32 _IOWR('V', 64, struct v4l2_format32)
  745. #define VIDIOC_G_EXT_CTRLS32 _IOWR('V', 71, struct v4l2_ext_controls32)
  746. #define VIDIOC_S_EXT_CTRLS32 _IOWR('V', 72, struct v4l2_ext_controls32)
  747. #define VIDIOC_TRY_EXT_CTRLS32 _IOWR('V', 73, struct v4l2_ext_controls32)
  748. #define VIDIOC_DQEVENT32 _IOR ('V', 89, struct v4l2_event32)
  749. #define VIDIOC_CREATE_BUFS32 _IOWR('V', 92, struct v4l2_create_buffers32)
  750. #define VIDIOC_PREPARE_BUF32 _IOWR('V', 93, struct v4l2_buffer32)
  751. #define VIDIOC_OVERLAY32 _IOW ('V', 14, s32)
  752. #define VIDIOC_STREAMON32 _IOW ('V', 18, s32)
  753. #define VIDIOC_STREAMOFF32 _IOW ('V', 19, s32)
  754. #define VIDIOC_G_INPUT32 _IOR ('V', 38, s32)
  755. #define VIDIOC_S_INPUT32 _IOWR('V', 39, s32)
  756. #define VIDIOC_G_OUTPUT32 _IOR ('V', 46, s32)
  757. #define VIDIOC_S_OUTPUT32 _IOWR('V', 47, s32)
  758. static long do_video_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  759. {
  760. union {
  761. struct v4l2_format v2f;
  762. struct v4l2_buffer v2b;
  763. struct v4l2_framebuffer v2fb;
  764. struct v4l2_input v2i;
  765. struct v4l2_standard v2s;
  766. struct v4l2_ext_controls v2ecs;
  767. struct v4l2_event v2ev;
  768. struct v4l2_create_buffers v2crt;
  769. struct v4l2_edid v2edid;
  770. unsigned long vx;
  771. int vi;
  772. } karg;
  773. void __user *up = compat_ptr(arg);
  774. int compatible_arg = 1;
  775. long err = 0;
  776. /* First, convert the command. */
  777. switch (cmd) {
  778. case VIDIOC_G_FMT32: cmd = VIDIOC_G_FMT; break;
  779. case VIDIOC_S_FMT32: cmd = VIDIOC_S_FMT; break;
  780. case VIDIOC_QUERYBUF32: cmd = VIDIOC_QUERYBUF; break;
  781. case VIDIOC_G_FBUF32: cmd = VIDIOC_G_FBUF; break;
  782. case VIDIOC_S_FBUF32: cmd = VIDIOC_S_FBUF; break;
  783. case VIDIOC_QBUF32: cmd = VIDIOC_QBUF; break;
  784. case VIDIOC_DQBUF32: cmd = VIDIOC_DQBUF; break;
  785. case VIDIOC_ENUMSTD32: cmd = VIDIOC_ENUMSTD; break;
  786. case VIDIOC_ENUMINPUT32: cmd = VIDIOC_ENUMINPUT; break;
  787. case VIDIOC_TRY_FMT32: cmd = VIDIOC_TRY_FMT; break;
  788. case VIDIOC_G_EXT_CTRLS32: cmd = VIDIOC_G_EXT_CTRLS; break;
  789. case VIDIOC_S_EXT_CTRLS32: cmd = VIDIOC_S_EXT_CTRLS; break;
  790. case VIDIOC_TRY_EXT_CTRLS32: cmd = VIDIOC_TRY_EXT_CTRLS; break;
  791. case VIDIOC_DQEVENT32: cmd = VIDIOC_DQEVENT; break;
  792. case VIDIOC_OVERLAY32: cmd = VIDIOC_OVERLAY; break;
  793. case VIDIOC_STREAMON32: cmd = VIDIOC_STREAMON; break;
  794. case VIDIOC_STREAMOFF32: cmd = VIDIOC_STREAMOFF; break;
  795. case VIDIOC_G_INPUT32: cmd = VIDIOC_G_INPUT; break;
  796. case VIDIOC_S_INPUT32: cmd = VIDIOC_S_INPUT; break;
  797. case VIDIOC_G_OUTPUT32: cmd = VIDIOC_G_OUTPUT; break;
  798. case VIDIOC_S_OUTPUT32: cmd = VIDIOC_S_OUTPUT; break;
  799. case VIDIOC_CREATE_BUFS32: cmd = VIDIOC_CREATE_BUFS; break;
  800. case VIDIOC_PREPARE_BUF32: cmd = VIDIOC_PREPARE_BUF; break;
  801. case VIDIOC_G_EDID32: cmd = VIDIOC_G_EDID; break;
  802. case VIDIOC_S_EDID32: cmd = VIDIOC_S_EDID; break;
  803. }
  804. switch (cmd) {
  805. case VIDIOC_OVERLAY:
  806. case VIDIOC_STREAMON:
  807. case VIDIOC_STREAMOFF:
  808. case VIDIOC_S_INPUT:
  809. case VIDIOC_S_OUTPUT:
  810. err = get_user(karg.vi, (s32 __user *)up);
  811. compatible_arg = 0;
  812. break;
  813. case VIDIOC_G_INPUT:
  814. case VIDIOC_G_OUTPUT:
  815. compatible_arg = 0;
  816. break;
  817. case VIDIOC_G_EDID:
  818. case VIDIOC_S_EDID:
  819. err = get_v4l2_edid32(&karg.v2edid, up);
  820. compatible_arg = 0;
  821. break;
  822. case VIDIOC_G_FMT:
  823. case VIDIOC_S_FMT:
  824. case VIDIOC_TRY_FMT:
  825. err = get_v4l2_format32(&karg.v2f, up);
  826. compatible_arg = 0;
  827. break;
  828. case VIDIOC_CREATE_BUFS:
  829. err = get_v4l2_create32(&karg.v2crt, up);
  830. compatible_arg = 0;
  831. break;
  832. case VIDIOC_PREPARE_BUF:
  833. case VIDIOC_QUERYBUF:
  834. case VIDIOC_QBUF:
  835. case VIDIOC_DQBUF:
  836. err = get_v4l2_buffer32(&karg.v2b, up);
  837. compatible_arg = 0;
  838. break;
  839. case VIDIOC_S_FBUF:
  840. err = get_v4l2_framebuffer32(&karg.v2fb, up);
  841. compatible_arg = 0;
  842. break;
  843. case VIDIOC_G_FBUF:
  844. compatible_arg = 0;
  845. break;
  846. case VIDIOC_ENUMSTD:
  847. err = get_v4l2_standard32(&karg.v2s, up);
  848. compatible_arg = 0;
  849. break;
  850. case VIDIOC_ENUMINPUT:
  851. err = get_v4l2_input32(&karg.v2i, up);
  852. compatible_arg = 0;
  853. break;
  854. case VIDIOC_G_EXT_CTRLS:
  855. case VIDIOC_S_EXT_CTRLS:
  856. case VIDIOC_TRY_EXT_CTRLS:
  857. err = get_v4l2_ext_controls32(&karg.v2ecs, up);
  858. compatible_arg = 0;
  859. break;
  860. case VIDIOC_DQEVENT:
  861. compatible_arg = 0;
  862. break;
  863. }
  864. if (err)
  865. return err;
  866. if (compatible_arg)
  867. err = native_ioctl(file, cmd, (unsigned long)up);
  868. else {
  869. mm_segment_t old_fs = get_fs();
  870. set_fs(KERNEL_DS);
  871. err = native_ioctl(file, cmd, (unsigned long)&karg);
  872. set_fs(old_fs);
  873. }
  874. /* Special case: even after an error we need to put the
  875. results back for these ioctls since the error_idx will
  876. contain information on which control failed. */
  877. switch (cmd) {
  878. case VIDIOC_G_EXT_CTRLS:
  879. case VIDIOC_S_EXT_CTRLS:
  880. case VIDIOC_TRY_EXT_CTRLS:
  881. if (put_v4l2_ext_controls32(&karg.v2ecs, up))
  882. err = -EFAULT;
  883. break;
  884. }
  885. if (err)
  886. return err;
  887. switch (cmd) {
  888. case VIDIOC_S_INPUT:
  889. case VIDIOC_S_OUTPUT:
  890. case VIDIOC_G_INPUT:
  891. case VIDIOC_G_OUTPUT:
  892. err = put_user(((s32)karg.vi), (s32 __user *)up);
  893. break;
  894. case VIDIOC_G_FBUF:
  895. err = put_v4l2_framebuffer32(&karg.v2fb, up);
  896. break;
  897. case VIDIOC_DQEVENT:
  898. err = put_v4l2_event32(&karg.v2ev, up);
  899. break;
  900. case VIDIOC_G_EDID:
  901. case VIDIOC_S_EDID:
  902. err = put_v4l2_edid32(&karg.v2edid, up);
  903. break;
  904. case VIDIOC_G_FMT:
  905. case VIDIOC_S_FMT:
  906. case VIDIOC_TRY_FMT:
  907. err = put_v4l2_format32(&karg.v2f, up);
  908. break;
  909. case VIDIOC_CREATE_BUFS:
  910. err = put_v4l2_create32(&karg.v2crt, up);
  911. break;
  912. case VIDIOC_QUERYBUF:
  913. case VIDIOC_QBUF:
  914. case VIDIOC_DQBUF:
  915. err = put_v4l2_buffer32(&karg.v2b, up);
  916. break;
  917. case VIDIOC_ENUMSTD:
  918. err = put_v4l2_standard32(&karg.v2s, up);
  919. break;
  920. case VIDIOC_ENUMINPUT:
  921. err = put_v4l2_input32(&karg.v2i, up);
  922. break;
  923. }
  924. return err;
  925. }
  926. long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
  927. {
  928. struct video_device *vdev = video_devdata(file);
  929. long ret = -ENOIOCTLCMD;
  930. if (!file->f_op->unlocked_ioctl)
  931. return ret;
  932. if (_IOC_TYPE(cmd) == 'V' && _IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
  933. ret = do_video_ioctl(file, cmd, arg);
  934. else if (vdev->fops->compat_ioctl32)
  935. ret = vdev->fops->compat_ioctl32(file, cmd, arg);
  936. if (ret == -ENOIOCTLCMD)
  937. pr_debug("compat_ioctl32: unknown ioctl '%c', dir=%d, #%d (0x%08x)\n",
  938. _IOC_TYPE(cmd), _IOC_DIR(cmd), _IOC_NR(cmd), cmd);
  939. return ret;
  940. }
  941. EXPORT_SYMBOL_GPL(v4l2_compat_ioctl32);