v4l2-compat-ioctl32.c 34 KB

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