v4l2-compat-ioctl32.c 34 KB

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