v4l2-compat-ioctl32.c 25 KB

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