v4l2-compat-ioctl32.c 29 KB

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