v4l2-compat-ioctl32.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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 __user *up, struct v4l2_plane32 __user *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 __user *up, struct v4l2_plane32 __user *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 = (__force struct v4l2_plane *)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 = (__force struct v4l2_plane __user *)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 {
  484. __u32 width;
  485. __u32 height;
  486. __u32 pixelformat;
  487. __u32 field;
  488. __u32 bytesperline;
  489. __u32 sizeimage;
  490. __u32 colorspace;
  491. __u32 priv;
  492. } fmt;
  493. };
  494. static int get_v4l2_framebuffer32(struct v4l2_framebuffer *kp, struct v4l2_framebuffer32 __user *up)
  495. {
  496. u32 tmp;
  497. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_framebuffer32)) ||
  498. get_user(tmp, &up->base) ||
  499. get_user(kp->capability, &up->capability) ||
  500. get_user(kp->flags, &up->flags) ||
  501. copy_from_user(&kp->fmt, &up->fmt, sizeof(up->fmt)))
  502. return -EFAULT;
  503. kp->base = (__force void *)compat_ptr(tmp);
  504. return 0;
  505. }
  506. static int put_v4l2_framebuffer32(struct v4l2_framebuffer *kp, struct v4l2_framebuffer32 __user *up)
  507. {
  508. u32 tmp = (u32)((unsigned long)kp->base);
  509. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_framebuffer32)) ||
  510. put_user(tmp, &up->base) ||
  511. put_user(kp->capability, &up->capability) ||
  512. put_user(kp->flags, &up->flags) ||
  513. copy_to_user(&up->fmt, &kp->fmt, sizeof(up->fmt)))
  514. return -EFAULT;
  515. return 0;
  516. }
  517. struct v4l2_input32 {
  518. __u32 index; /* Which input */
  519. __u8 name[32]; /* Label */
  520. __u32 type; /* Type of input */
  521. __u32 audioset; /* Associated audios (bitfield) */
  522. __u32 tuner; /* Associated tuner */
  523. v4l2_std_id std;
  524. __u32 status;
  525. __u32 reserved[4];
  526. } __attribute__ ((packed));
  527. /* The 64-bit v4l2_input struct has extra padding at the end of the struct.
  528. Otherwise it is identical to the 32-bit version. */
  529. static inline int get_v4l2_input32(struct v4l2_input *kp, struct v4l2_input32 __user *up)
  530. {
  531. if (copy_from_user(kp, up, sizeof(struct v4l2_input32)))
  532. return -EFAULT;
  533. return 0;
  534. }
  535. static inline int put_v4l2_input32(struct v4l2_input *kp, struct v4l2_input32 __user *up)
  536. {
  537. if (copy_to_user(up, kp, sizeof(struct v4l2_input32)))
  538. return -EFAULT;
  539. return 0;
  540. }
  541. struct v4l2_ext_controls32 {
  542. __u32 ctrl_class;
  543. __u32 count;
  544. __u32 error_idx;
  545. __u32 reserved[2];
  546. compat_caddr_t controls; /* actually struct v4l2_ext_control32 * */
  547. };
  548. struct v4l2_ext_control32 {
  549. __u32 id;
  550. __u32 size;
  551. __u32 reserved2[1];
  552. union {
  553. __s32 value;
  554. __s64 value64;
  555. compat_caddr_t string; /* actually char * */
  556. };
  557. } __attribute__ ((packed));
  558. /* The following function really belong in v4l2-common, but that causes
  559. a circular dependency between modules. We need to think about this, but
  560. for now this will do. */
  561. /* Return non-zero if this control is a pointer type. Currently only
  562. type STRING is a pointer type. */
  563. static inline int ctrl_is_pointer(u32 id)
  564. {
  565. switch (id) {
  566. case V4L2_CID_RDS_TX_PS_NAME:
  567. case V4L2_CID_RDS_TX_RADIO_TEXT:
  568. return 1;
  569. default:
  570. return 0;
  571. }
  572. }
  573. static int get_v4l2_ext_controls32(struct v4l2_ext_controls *kp, struct v4l2_ext_controls32 __user *up)
  574. {
  575. struct v4l2_ext_control32 __user *ucontrols;
  576. struct v4l2_ext_control __user *kcontrols;
  577. int n;
  578. compat_caddr_t p;
  579. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_ext_controls32)) ||
  580. get_user(kp->ctrl_class, &up->ctrl_class) ||
  581. get_user(kp->count, &up->count) ||
  582. get_user(kp->error_idx, &up->error_idx) ||
  583. copy_from_user(kp->reserved, up->reserved, sizeof(kp->reserved)))
  584. return -EFAULT;
  585. n = kp->count;
  586. if (n == 0) {
  587. kp->controls = NULL;
  588. return 0;
  589. }
  590. if (get_user(p, &up->controls))
  591. return -EFAULT;
  592. ucontrols = compat_ptr(p);
  593. if (!access_ok(VERIFY_READ, ucontrols,
  594. n * sizeof(struct v4l2_ext_control32)))
  595. return -EFAULT;
  596. kcontrols = compat_alloc_user_space(n * sizeof(struct v4l2_ext_control));
  597. kp->controls = (__force struct v4l2_ext_control *)kcontrols;
  598. while (--n >= 0) {
  599. u32 id;
  600. if (copy_in_user(kcontrols, ucontrols, sizeof(*ucontrols)))
  601. return -EFAULT;
  602. if (get_user(id, &kcontrols->id))
  603. return -EFAULT;
  604. if (ctrl_is_pointer(id)) {
  605. void __user *s;
  606. if (get_user(p, &ucontrols->string))
  607. return -EFAULT;
  608. s = compat_ptr(p);
  609. if (put_user(s, &kcontrols->string))
  610. return -EFAULT;
  611. }
  612. ucontrols++;
  613. kcontrols++;
  614. }
  615. return 0;
  616. }
  617. static int put_v4l2_ext_controls32(struct v4l2_ext_controls *kp, struct v4l2_ext_controls32 __user *up)
  618. {
  619. struct v4l2_ext_control32 __user *ucontrols;
  620. struct v4l2_ext_control __user *kcontrols =
  621. (__force struct v4l2_ext_control __user *)kp->controls;
  622. int n = kp->count;
  623. compat_caddr_t p;
  624. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_ext_controls32)) ||
  625. put_user(kp->ctrl_class, &up->ctrl_class) ||
  626. put_user(kp->count, &up->count) ||
  627. put_user(kp->error_idx, &up->error_idx) ||
  628. copy_to_user(up->reserved, kp->reserved, sizeof(up->reserved)))
  629. return -EFAULT;
  630. if (!kp->count)
  631. return 0;
  632. if (get_user(p, &up->controls))
  633. return -EFAULT;
  634. ucontrols = compat_ptr(p);
  635. if (!access_ok(VERIFY_WRITE, ucontrols,
  636. n * sizeof(struct v4l2_ext_control32)))
  637. return -EFAULT;
  638. while (--n >= 0) {
  639. unsigned size = sizeof(*ucontrols);
  640. u32 id;
  641. if (get_user(id, &kcontrols->id))
  642. return -EFAULT;
  643. /* Do not modify the pointer when copying a pointer control.
  644. The contents of the pointer was changed, not the pointer
  645. itself. */
  646. if (ctrl_is_pointer(id))
  647. size -= sizeof(ucontrols->value64);
  648. if (copy_in_user(ucontrols, kcontrols, size))
  649. return -EFAULT;
  650. ucontrols++;
  651. kcontrols++;
  652. }
  653. return 0;
  654. }
  655. struct v4l2_event32 {
  656. __u32 type;
  657. union {
  658. __u8 data[64];
  659. } u;
  660. __u32 pending;
  661. __u32 sequence;
  662. struct compat_timespec timestamp;
  663. __u32 id;
  664. __u32 reserved[8];
  665. };
  666. static int put_v4l2_event32(struct v4l2_event *kp, struct v4l2_event32 __user *up)
  667. {
  668. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_event32)) ||
  669. put_user(kp->type, &up->type) ||
  670. copy_to_user(&up->u, &kp->u, sizeof(kp->u)) ||
  671. put_user(kp->pending, &up->pending) ||
  672. put_user(kp->sequence, &up->sequence) ||
  673. compat_put_timespec(&kp->timestamp, &up->timestamp) ||
  674. put_user(kp->id, &up->id) ||
  675. copy_to_user(up->reserved, kp->reserved, 8 * sizeof(__u32)))
  676. return -EFAULT;
  677. return 0;
  678. }
  679. struct v4l2_edid32 {
  680. __u32 pad;
  681. __u32 start_block;
  682. __u32 blocks;
  683. __u32 reserved[5];
  684. compat_caddr_t edid;
  685. };
  686. static int get_v4l2_edid32(struct v4l2_edid *kp, struct v4l2_edid32 __user *up)
  687. {
  688. u32 tmp;
  689. if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_edid32)) ||
  690. get_user(kp->pad, &up->pad) ||
  691. get_user(kp->start_block, &up->start_block) ||
  692. get_user(kp->blocks, &up->blocks) ||
  693. get_user(tmp, &up->edid) ||
  694. copy_from_user(kp->reserved, up->reserved, sizeof(kp->reserved)))
  695. return -EFAULT;
  696. kp->edid = (__force u8 *)compat_ptr(tmp);
  697. return 0;
  698. }
  699. static int put_v4l2_edid32(struct v4l2_edid *kp, struct v4l2_edid32 __user *up)
  700. {
  701. u32 tmp = (u32)((unsigned long)kp->edid);
  702. if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_edid32)) ||
  703. put_user(kp->pad, &up->pad) ||
  704. put_user(kp->start_block, &up->start_block) ||
  705. put_user(kp->blocks, &up->blocks) ||
  706. put_user(tmp, &up->edid) ||
  707. copy_to_user(up->reserved, kp->reserved, sizeof(up->reserved)))
  708. return -EFAULT;
  709. return 0;
  710. }
  711. #define VIDIOC_G_FMT32 _IOWR('V', 4, struct v4l2_format32)
  712. #define VIDIOC_S_FMT32 _IOWR('V', 5, struct v4l2_format32)
  713. #define VIDIOC_QUERYBUF32 _IOWR('V', 9, struct v4l2_buffer32)
  714. #define VIDIOC_G_FBUF32 _IOR ('V', 10, struct v4l2_framebuffer32)
  715. #define VIDIOC_S_FBUF32 _IOW ('V', 11, struct v4l2_framebuffer32)
  716. #define VIDIOC_QBUF32 _IOWR('V', 15, struct v4l2_buffer32)
  717. #define VIDIOC_DQBUF32 _IOWR('V', 17, struct v4l2_buffer32)
  718. #define VIDIOC_ENUMSTD32 _IOWR('V', 25, struct v4l2_standard32)
  719. #define VIDIOC_ENUMINPUT32 _IOWR('V', 26, struct v4l2_input32)
  720. #define VIDIOC_G_EDID32 _IOWR('V', 40, struct v4l2_edid32)
  721. #define VIDIOC_S_EDID32 _IOWR('V', 41, struct v4l2_edid32)
  722. #define VIDIOC_TRY_FMT32 _IOWR('V', 64, struct v4l2_format32)
  723. #define VIDIOC_G_EXT_CTRLS32 _IOWR('V', 71, struct v4l2_ext_controls32)
  724. #define VIDIOC_S_EXT_CTRLS32 _IOWR('V', 72, struct v4l2_ext_controls32)
  725. #define VIDIOC_TRY_EXT_CTRLS32 _IOWR('V', 73, struct v4l2_ext_controls32)
  726. #define VIDIOC_DQEVENT32 _IOR ('V', 89, struct v4l2_event32)
  727. #define VIDIOC_CREATE_BUFS32 _IOWR('V', 92, struct v4l2_create_buffers32)
  728. #define VIDIOC_PREPARE_BUF32 _IOWR('V', 93, struct v4l2_buffer32)
  729. #define VIDIOC_OVERLAY32 _IOW ('V', 14, s32)
  730. #define VIDIOC_STREAMON32 _IOW ('V', 18, s32)
  731. #define VIDIOC_STREAMOFF32 _IOW ('V', 19, s32)
  732. #define VIDIOC_G_INPUT32 _IOR ('V', 38, s32)
  733. #define VIDIOC_S_INPUT32 _IOWR('V', 39, s32)
  734. #define VIDIOC_G_OUTPUT32 _IOR ('V', 46, s32)
  735. #define VIDIOC_S_OUTPUT32 _IOWR('V', 47, s32)
  736. static long do_video_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  737. {
  738. union {
  739. struct v4l2_format v2f;
  740. struct v4l2_buffer v2b;
  741. struct v4l2_framebuffer v2fb;
  742. struct v4l2_input v2i;
  743. struct v4l2_standard v2s;
  744. struct v4l2_ext_controls v2ecs;
  745. struct v4l2_event v2ev;
  746. struct v4l2_create_buffers v2crt;
  747. struct v4l2_edid v2edid;
  748. unsigned long vx;
  749. int vi;
  750. } karg;
  751. void __user *up = compat_ptr(arg);
  752. int compatible_arg = 1;
  753. long err = 0;
  754. /* First, convert the command. */
  755. switch (cmd) {
  756. case VIDIOC_G_FMT32: cmd = VIDIOC_G_FMT; break;
  757. case VIDIOC_S_FMT32: cmd = VIDIOC_S_FMT; break;
  758. case VIDIOC_QUERYBUF32: cmd = VIDIOC_QUERYBUF; break;
  759. case VIDIOC_G_FBUF32: cmd = VIDIOC_G_FBUF; break;
  760. case VIDIOC_S_FBUF32: cmd = VIDIOC_S_FBUF; break;
  761. case VIDIOC_QBUF32: cmd = VIDIOC_QBUF; break;
  762. case VIDIOC_DQBUF32: cmd = VIDIOC_DQBUF; break;
  763. case VIDIOC_ENUMSTD32: cmd = VIDIOC_ENUMSTD; break;
  764. case VIDIOC_ENUMINPUT32: cmd = VIDIOC_ENUMINPUT; break;
  765. case VIDIOC_TRY_FMT32: cmd = VIDIOC_TRY_FMT; break;
  766. case VIDIOC_G_EXT_CTRLS32: cmd = VIDIOC_G_EXT_CTRLS; break;
  767. case VIDIOC_S_EXT_CTRLS32: cmd = VIDIOC_S_EXT_CTRLS; break;
  768. case VIDIOC_TRY_EXT_CTRLS32: cmd = VIDIOC_TRY_EXT_CTRLS; break;
  769. case VIDIOC_DQEVENT32: cmd = VIDIOC_DQEVENT; break;
  770. case VIDIOC_OVERLAY32: cmd = VIDIOC_OVERLAY; break;
  771. case VIDIOC_STREAMON32: cmd = VIDIOC_STREAMON; break;
  772. case VIDIOC_STREAMOFF32: cmd = VIDIOC_STREAMOFF; break;
  773. case VIDIOC_G_INPUT32: cmd = VIDIOC_G_INPUT; break;
  774. case VIDIOC_S_INPUT32: cmd = VIDIOC_S_INPUT; break;
  775. case VIDIOC_G_OUTPUT32: cmd = VIDIOC_G_OUTPUT; break;
  776. case VIDIOC_S_OUTPUT32: cmd = VIDIOC_S_OUTPUT; break;
  777. case VIDIOC_CREATE_BUFS32: cmd = VIDIOC_CREATE_BUFS; break;
  778. case VIDIOC_PREPARE_BUF32: cmd = VIDIOC_PREPARE_BUF; break;
  779. case VIDIOC_G_EDID32: cmd = VIDIOC_G_EDID; break;
  780. case VIDIOC_S_EDID32: cmd = VIDIOC_S_EDID; break;
  781. }
  782. switch (cmd) {
  783. case VIDIOC_OVERLAY:
  784. case VIDIOC_STREAMON:
  785. case VIDIOC_STREAMOFF:
  786. case VIDIOC_S_INPUT:
  787. case VIDIOC_S_OUTPUT:
  788. err = get_user(karg.vi, (s32 __user *)up);
  789. compatible_arg = 0;
  790. break;
  791. case VIDIOC_G_INPUT:
  792. case VIDIOC_G_OUTPUT:
  793. compatible_arg = 0;
  794. break;
  795. case VIDIOC_G_EDID:
  796. case VIDIOC_S_EDID:
  797. err = get_v4l2_edid32(&karg.v2edid, up);
  798. compatible_arg = 0;
  799. break;
  800. case VIDIOC_G_FMT:
  801. case VIDIOC_S_FMT:
  802. case VIDIOC_TRY_FMT:
  803. err = get_v4l2_format32(&karg.v2f, up);
  804. compatible_arg = 0;
  805. break;
  806. case VIDIOC_CREATE_BUFS:
  807. err = get_v4l2_create32(&karg.v2crt, up);
  808. compatible_arg = 0;
  809. break;
  810. case VIDIOC_PREPARE_BUF:
  811. case VIDIOC_QUERYBUF:
  812. case VIDIOC_QBUF:
  813. case VIDIOC_DQBUF:
  814. err = get_v4l2_buffer32(&karg.v2b, up);
  815. compatible_arg = 0;
  816. break;
  817. case VIDIOC_S_FBUF:
  818. err = get_v4l2_framebuffer32(&karg.v2fb, up);
  819. compatible_arg = 0;
  820. break;
  821. case VIDIOC_G_FBUF:
  822. compatible_arg = 0;
  823. break;
  824. case VIDIOC_ENUMSTD:
  825. err = get_v4l2_standard32(&karg.v2s, up);
  826. compatible_arg = 0;
  827. break;
  828. case VIDIOC_ENUMINPUT:
  829. err = get_v4l2_input32(&karg.v2i, up);
  830. compatible_arg = 0;
  831. break;
  832. case VIDIOC_G_EXT_CTRLS:
  833. case VIDIOC_S_EXT_CTRLS:
  834. case VIDIOC_TRY_EXT_CTRLS:
  835. err = get_v4l2_ext_controls32(&karg.v2ecs, up);
  836. compatible_arg = 0;
  837. break;
  838. case VIDIOC_DQEVENT:
  839. compatible_arg = 0;
  840. break;
  841. }
  842. if (err)
  843. return err;
  844. if (compatible_arg)
  845. err = native_ioctl(file, cmd, (unsigned long)up);
  846. else {
  847. mm_segment_t old_fs = get_fs();
  848. set_fs(KERNEL_DS);
  849. err = native_ioctl(file, cmd, (unsigned long)&karg);
  850. set_fs(old_fs);
  851. }
  852. /* Special case: even after an error we need to put the
  853. results back for these ioctls since the error_idx will
  854. contain information on which control failed. */
  855. switch (cmd) {
  856. case VIDIOC_G_EXT_CTRLS:
  857. case VIDIOC_S_EXT_CTRLS:
  858. case VIDIOC_TRY_EXT_CTRLS:
  859. if (put_v4l2_ext_controls32(&karg.v2ecs, up))
  860. err = -EFAULT;
  861. break;
  862. }
  863. if (err)
  864. return err;
  865. switch (cmd) {
  866. case VIDIOC_S_INPUT:
  867. case VIDIOC_S_OUTPUT:
  868. case VIDIOC_G_INPUT:
  869. case VIDIOC_G_OUTPUT:
  870. err = put_user(((s32)karg.vi), (s32 __user *)up);
  871. break;
  872. case VIDIOC_G_FBUF:
  873. err = put_v4l2_framebuffer32(&karg.v2fb, up);
  874. break;
  875. case VIDIOC_DQEVENT:
  876. err = put_v4l2_event32(&karg.v2ev, up);
  877. break;
  878. case VIDIOC_G_EDID:
  879. case VIDIOC_S_EDID:
  880. err = put_v4l2_edid32(&karg.v2edid, up);
  881. break;
  882. case VIDIOC_G_FMT:
  883. case VIDIOC_S_FMT:
  884. case VIDIOC_TRY_FMT:
  885. err = put_v4l2_format32(&karg.v2f, up);
  886. break;
  887. case VIDIOC_CREATE_BUFS:
  888. err = put_v4l2_create32(&karg.v2crt, up);
  889. break;
  890. case VIDIOC_QUERYBUF:
  891. case VIDIOC_QBUF:
  892. case VIDIOC_DQBUF:
  893. err = put_v4l2_buffer32(&karg.v2b, up);
  894. break;
  895. case VIDIOC_ENUMSTD:
  896. err = put_v4l2_standard32(&karg.v2s, up);
  897. break;
  898. case VIDIOC_ENUMINPUT:
  899. err = put_v4l2_input32(&karg.v2i, up);
  900. break;
  901. }
  902. return err;
  903. }
  904. long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
  905. {
  906. struct video_device *vdev = video_devdata(file);
  907. long ret = -ENOIOCTLCMD;
  908. if (!file->f_op->unlocked_ioctl)
  909. return ret;
  910. if (_IOC_TYPE(cmd) == 'V' && _IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
  911. ret = do_video_ioctl(file, cmd, arg);
  912. else if (vdev->fops->compat_ioctl32)
  913. ret = vdev->fops->compat_ioctl32(file, cmd, arg);
  914. if (ret == -ENOIOCTLCMD)
  915. pr_warn("compat_ioctl32: unknown ioctl '%c', dir=%d, #%d (0x%08x)\n",
  916. _IOC_TYPE(cmd), _IOC_DIR(cmd), _IOC_NR(cmd), cmd);
  917. return ret;
  918. }
  919. EXPORT_SYMBOL_GPL(v4l2_compat_ioctl32);