v4l2-compat-ioctl32.c 28 KB

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