v4l2-compat-ioctl32.c 35 KB

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