v4l2-compat-ioctl32.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ioctl32.c: Conversion between 32bit and 64bit native ioctls.
  4. * Separated from fs stuff by Arnd Bergmann <arnd@arndb.de>
  5. *
  6. * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
  7. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  8. * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
  9. * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz)
  10. * Copyright (C) 2005 Philippe De Muyter (phdm@macqel.be)
  11. * Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
  12. *
  13. * These routines maintain argument size conversion between 32bit and 64bit
  14. * ioctls.
  15. */
  16. #include <linux/compat.h>
  17. #include <linux/module.h>
  18. #include <linux/videodev2.h>
  19. #include <linux/v4l2-subdev.h>
  20. #include <media/v4l2-dev.h>
  21. #include <media/v4l2-fh.h>
  22. #include <media/v4l2-ctrls.h>
  23. #include <media/v4l2-ioctl.h>
  24. /*
  25. * Per-ioctl data copy handlers.
  26. *
  27. * Those come in pairs, with a get_v4l2_foo() and a put_v4l2_foo() routine,
  28. * where "v4l2_foo" is the name of the V4L2 struct.
  29. *
  30. * They basically get two __user pointers, one with a 32-bits struct that
  31. * came from the userspace call and a 64-bits struct, also allocated as
  32. * userspace, but filled internally by do_video_ioctl().
  33. *
  34. * For ioctls that have pointers inside it, the functions will also
  35. * receive an ancillary buffer with extra space, used to pass extra
  36. * data to the routine.
  37. */
  38. struct v4l2_window32 {
  39. struct v4l2_rect w;
  40. __u32 field; /* enum v4l2_field */
  41. __u32 chromakey;
  42. compat_caddr_t clips; /* always NULL */
  43. __u32 clipcount; /* always 0 */
  44. compat_caddr_t bitmap; /* always NULL */
  45. __u8 global_alpha;
  46. };
  47. static int get_v4l2_window32(struct v4l2_window *p64,
  48. struct v4l2_window32 __user *p32)
  49. {
  50. struct v4l2_window32 w32;
  51. if (copy_from_user(&w32, p32, sizeof(w32)))
  52. return -EFAULT;
  53. *p64 = (struct v4l2_window) {
  54. .w = w32.w,
  55. .field = w32.field,
  56. .chromakey = w32.chromakey,
  57. .clips = NULL,
  58. .clipcount = 0,
  59. .bitmap = NULL,
  60. .global_alpha = w32.global_alpha,
  61. };
  62. return 0;
  63. }
  64. static int put_v4l2_window32(struct v4l2_window *p64,
  65. struct v4l2_window32 __user *p32)
  66. {
  67. struct v4l2_window32 w32;
  68. memset(&w32, 0, sizeof(w32));
  69. w32 = (struct v4l2_window32) {
  70. .w = p64->w,
  71. .field = p64->field,
  72. .chromakey = p64->chromakey,
  73. .clips = 0,
  74. .clipcount = 0,
  75. .bitmap = 0,
  76. .global_alpha = p64->global_alpha,
  77. };
  78. if (copy_to_user(p32, &w32, sizeof(w32)))
  79. return -EFAULT;
  80. return 0;
  81. }
  82. struct v4l2_format32 {
  83. __u32 type; /* enum v4l2_buf_type */
  84. union {
  85. struct v4l2_pix_format pix;
  86. struct v4l2_pix_format_mplane pix_mp;
  87. struct v4l2_window32 win;
  88. struct v4l2_vbi_format vbi;
  89. struct v4l2_sliced_vbi_format sliced;
  90. struct v4l2_sdr_format sdr;
  91. struct v4l2_meta_format meta;
  92. __u8 raw_data[200]; /* user-defined */
  93. } fmt;
  94. };
  95. /**
  96. * struct v4l2_create_buffers32 - VIDIOC_CREATE_BUFS32 argument
  97. * @index: on return, index of the first created buffer
  98. * @count: entry: number of requested buffers,
  99. * return: number of created buffers
  100. * @memory: buffer memory type
  101. * @format: frame format, for which buffers are requested
  102. * @capabilities: capabilities of this buffer type.
  103. * @flags: additional buffer management attributes (ignored unless the
  104. * queue has V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS capability and
  105. * configured for MMAP streaming I/O).
  106. * @max_num_buffers: if V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS capability flag is set
  107. * this field indicate the maximum possible number of buffers
  108. * for this queue.
  109. * @reserved: future extensions
  110. */
  111. struct v4l2_create_buffers32 {
  112. __u32 index;
  113. __u32 count;
  114. __u32 memory; /* enum v4l2_memory */
  115. struct v4l2_format32 format;
  116. __u32 capabilities;
  117. __u32 flags;
  118. __u32 max_num_buffers;
  119. __u32 reserved[5];
  120. };
  121. static int get_v4l2_format32(struct v4l2_format *p64,
  122. struct v4l2_format32 __user *p32)
  123. {
  124. if (get_user(p64->type, &p32->type))
  125. return -EFAULT;
  126. switch (p64->type) {
  127. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  128. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  129. return copy_from_user(&p64->fmt.pix, &p32->fmt.pix,
  130. sizeof(p64->fmt.pix)) ? -EFAULT : 0;
  131. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  132. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  133. return copy_from_user(&p64->fmt.pix_mp, &p32->fmt.pix_mp,
  134. sizeof(p64->fmt.pix_mp)) ? -EFAULT : 0;
  135. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  136. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  137. return get_v4l2_window32(&p64->fmt.win, &p32->fmt.win);
  138. case V4L2_BUF_TYPE_VBI_CAPTURE:
  139. case V4L2_BUF_TYPE_VBI_OUTPUT:
  140. return copy_from_user(&p64->fmt.vbi, &p32->fmt.vbi,
  141. sizeof(p64->fmt.vbi)) ? -EFAULT : 0;
  142. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  143. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  144. return copy_from_user(&p64->fmt.sliced, &p32->fmt.sliced,
  145. sizeof(p64->fmt.sliced)) ? -EFAULT : 0;
  146. case V4L2_BUF_TYPE_SDR_CAPTURE:
  147. case V4L2_BUF_TYPE_SDR_OUTPUT:
  148. return copy_from_user(&p64->fmt.sdr, &p32->fmt.sdr,
  149. sizeof(p64->fmt.sdr)) ? -EFAULT : 0;
  150. case V4L2_BUF_TYPE_META_CAPTURE:
  151. case V4L2_BUF_TYPE_META_OUTPUT:
  152. return copy_from_user(&p64->fmt.meta, &p32->fmt.meta,
  153. sizeof(p64->fmt.meta)) ? -EFAULT : 0;
  154. default:
  155. return -EINVAL;
  156. }
  157. }
  158. static int get_v4l2_create32(struct v4l2_create_buffers *p64,
  159. struct v4l2_create_buffers32 __user *p32)
  160. {
  161. if (copy_from_user(p64, p32,
  162. offsetof(struct v4l2_create_buffers32, format)))
  163. return -EFAULT;
  164. if (copy_from_user(&p64->flags, &p32->flags, sizeof(p32->flags)))
  165. return -EFAULT;
  166. if (copy_from_user(&p64->max_num_buffers, &p32->max_num_buffers,
  167. sizeof(p32->max_num_buffers)))
  168. return -EFAULT;
  169. return get_v4l2_format32(&p64->format, &p32->format);
  170. }
  171. static int put_v4l2_format32(struct v4l2_format *p64,
  172. struct v4l2_format32 __user *p32)
  173. {
  174. switch (p64->type) {
  175. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  176. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  177. return copy_to_user(&p32->fmt.pix, &p64->fmt.pix,
  178. sizeof(p64->fmt.pix)) ? -EFAULT : 0;
  179. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  180. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  181. return copy_to_user(&p32->fmt.pix_mp, &p64->fmt.pix_mp,
  182. sizeof(p64->fmt.pix_mp)) ? -EFAULT : 0;
  183. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  184. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  185. return put_v4l2_window32(&p64->fmt.win, &p32->fmt.win);
  186. case V4L2_BUF_TYPE_VBI_CAPTURE:
  187. case V4L2_BUF_TYPE_VBI_OUTPUT:
  188. return copy_to_user(&p32->fmt.vbi, &p64->fmt.vbi,
  189. sizeof(p64->fmt.vbi)) ? -EFAULT : 0;
  190. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  191. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  192. return copy_to_user(&p32->fmt.sliced, &p64->fmt.sliced,
  193. sizeof(p64->fmt.sliced)) ? -EFAULT : 0;
  194. case V4L2_BUF_TYPE_SDR_CAPTURE:
  195. case V4L2_BUF_TYPE_SDR_OUTPUT:
  196. return copy_to_user(&p32->fmt.sdr, &p64->fmt.sdr,
  197. sizeof(p64->fmt.sdr)) ? -EFAULT : 0;
  198. case V4L2_BUF_TYPE_META_CAPTURE:
  199. case V4L2_BUF_TYPE_META_OUTPUT:
  200. return copy_to_user(&p32->fmt.meta, &p64->fmt.meta,
  201. sizeof(p64->fmt.meta)) ? -EFAULT : 0;
  202. default:
  203. return -EINVAL;
  204. }
  205. }
  206. static int put_v4l2_create32(struct v4l2_create_buffers *p64,
  207. struct v4l2_create_buffers32 __user *p32)
  208. {
  209. if (copy_to_user(p32, p64,
  210. offsetof(struct v4l2_create_buffers32, format)) ||
  211. put_user(p64->capabilities, &p32->capabilities) ||
  212. put_user(p64->flags, &p32->flags) ||
  213. put_user(p64->max_num_buffers, &p32->max_num_buffers) ||
  214. copy_to_user(p32->reserved, p64->reserved, sizeof(p64->reserved)))
  215. return -EFAULT;
  216. return put_v4l2_format32(&p64->format, &p32->format);
  217. }
  218. struct v4l2_standard32 {
  219. __u32 index;
  220. compat_u64 id;
  221. __u8 name[24];
  222. struct v4l2_fract frameperiod; /* Frames, not fields */
  223. __u32 framelines;
  224. __u32 reserved[4];
  225. };
  226. static int get_v4l2_standard32(struct v4l2_standard *p64,
  227. struct v4l2_standard32 __user *p32)
  228. {
  229. /* other fields are not set by the user, nor used by the driver */
  230. return get_user(p64->index, &p32->index);
  231. }
  232. static int put_v4l2_standard32(struct v4l2_standard *p64,
  233. struct v4l2_standard32 __user *p32)
  234. {
  235. if (put_user(p64->index, &p32->index) ||
  236. put_user(p64->id, &p32->id) ||
  237. copy_to_user(p32->name, p64->name, sizeof(p32->name)) ||
  238. copy_to_user(&p32->frameperiod, &p64->frameperiod,
  239. sizeof(p32->frameperiod)) ||
  240. put_user(p64->framelines, &p32->framelines) ||
  241. copy_to_user(p32->reserved, p64->reserved, sizeof(p32->reserved)))
  242. return -EFAULT;
  243. return 0;
  244. }
  245. struct v4l2_plane32 {
  246. __u32 bytesused;
  247. __u32 length;
  248. union {
  249. __u32 mem_offset;
  250. compat_long_t userptr;
  251. __s32 fd;
  252. } m;
  253. __u32 data_offset;
  254. __u32 reserved[11];
  255. };
  256. /*
  257. * This is correct for all architectures including i386, but not x32,
  258. * which has different alignment requirements for timestamp
  259. */
  260. struct v4l2_buffer32 {
  261. __u32 index;
  262. __u32 type; /* enum v4l2_buf_type */
  263. __u32 bytesused;
  264. __u32 flags;
  265. __u32 field; /* enum v4l2_field */
  266. struct {
  267. compat_s64 tv_sec;
  268. compat_s64 tv_usec;
  269. } timestamp;
  270. struct v4l2_timecode timecode;
  271. __u32 sequence;
  272. /* memory location */
  273. __u32 memory; /* enum v4l2_memory */
  274. union {
  275. __u32 offset;
  276. compat_long_t userptr;
  277. compat_caddr_t planes;
  278. __s32 fd;
  279. } m;
  280. __u32 length;
  281. __u32 reserved2;
  282. __s32 request_fd;
  283. };
  284. #ifdef CONFIG_COMPAT_32BIT_TIME
  285. struct v4l2_buffer32_time32 {
  286. __u32 index;
  287. __u32 type; /* enum v4l2_buf_type */
  288. __u32 bytesused;
  289. __u32 flags;
  290. __u32 field; /* enum v4l2_field */
  291. struct old_timeval32 timestamp;
  292. struct v4l2_timecode timecode;
  293. __u32 sequence;
  294. /* memory location */
  295. __u32 memory; /* enum v4l2_memory */
  296. union {
  297. __u32 offset;
  298. compat_long_t userptr;
  299. compat_caddr_t planes;
  300. __s32 fd;
  301. } m;
  302. __u32 length;
  303. __u32 reserved2;
  304. __s32 request_fd;
  305. };
  306. #endif
  307. static int get_v4l2_plane32(struct v4l2_plane *p64,
  308. struct v4l2_plane32 __user *p32,
  309. enum v4l2_memory memory)
  310. {
  311. struct v4l2_plane32 plane32;
  312. typeof(p64->m) m = {};
  313. if (copy_from_user(&plane32, p32, sizeof(plane32)))
  314. return -EFAULT;
  315. switch (memory) {
  316. case V4L2_MEMORY_MMAP:
  317. case V4L2_MEMORY_OVERLAY:
  318. m.mem_offset = plane32.m.mem_offset;
  319. break;
  320. case V4L2_MEMORY_USERPTR:
  321. m.userptr = (unsigned long)compat_ptr(plane32.m.userptr);
  322. break;
  323. case V4L2_MEMORY_DMABUF:
  324. m.fd = plane32.m.fd;
  325. break;
  326. }
  327. memset(p64, 0, sizeof(*p64));
  328. *p64 = (struct v4l2_plane) {
  329. .bytesused = plane32.bytesused,
  330. .length = plane32.length,
  331. .m = m,
  332. .data_offset = plane32.data_offset,
  333. };
  334. return 0;
  335. }
  336. static int put_v4l2_plane32(struct v4l2_plane *p64,
  337. struct v4l2_plane32 __user *p32,
  338. enum v4l2_memory memory)
  339. {
  340. struct v4l2_plane32 plane32;
  341. memset(&plane32, 0, sizeof(plane32));
  342. plane32 = (struct v4l2_plane32) {
  343. .bytesused = p64->bytesused,
  344. .length = p64->length,
  345. .data_offset = p64->data_offset,
  346. };
  347. switch (memory) {
  348. case V4L2_MEMORY_MMAP:
  349. case V4L2_MEMORY_OVERLAY:
  350. plane32.m.mem_offset = p64->m.mem_offset;
  351. break;
  352. case V4L2_MEMORY_USERPTR:
  353. plane32.m.userptr = (uintptr_t)(p64->m.userptr);
  354. break;
  355. case V4L2_MEMORY_DMABUF:
  356. plane32.m.fd = p64->m.fd;
  357. break;
  358. }
  359. if (copy_to_user(p32, &plane32, sizeof(plane32)))
  360. return -EFAULT;
  361. return 0;
  362. }
  363. static int get_v4l2_buffer32(struct v4l2_buffer *vb,
  364. struct v4l2_buffer32 __user *arg)
  365. {
  366. struct v4l2_buffer32 vb32;
  367. if (copy_from_user(&vb32, arg, sizeof(vb32)))
  368. return -EFAULT;
  369. memset(vb, 0, sizeof(*vb));
  370. *vb = (struct v4l2_buffer) {
  371. .index = vb32.index,
  372. .type = vb32.type,
  373. .bytesused = vb32.bytesused,
  374. .flags = vb32.flags,
  375. .field = vb32.field,
  376. .timestamp.tv_sec = vb32.timestamp.tv_sec,
  377. .timestamp.tv_usec = vb32.timestamp.tv_usec,
  378. .timecode = vb32.timecode,
  379. .sequence = vb32.sequence,
  380. .memory = vb32.memory,
  381. .m.offset = vb32.m.offset,
  382. .length = vb32.length,
  383. .request_fd = vb32.request_fd,
  384. };
  385. switch (vb->memory) {
  386. case V4L2_MEMORY_MMAP:
  387. case V4L2_MEMORY_OVERLAY:
  388. vb->m.offset = vb32.m.offset;
  389. break;
  390. case V4L2_MEMORY_USERPTR:
  391. vb->m.userptr = (unsigned long)compat_ptr(vb32.m.userptr);
  392. break;
  393. case V4L2_MEMORY_DMABUF:
  394. vb->m.fd = vb32.m.fd;
  395. break;
  396. }
  397. if (V4L2_TYPE_IS_MULTIPLANAR(vb->type))
  398. vb->m.planes = (void __force *)
  399. compat_ptr(vb32.m.planes);
  400. return 0;
  401. }
  402. #ifdef CONFIG_COMPAT_32BIT_TIME
  403. static int get_v4l2_buffer32_time32(struct v4l2_buffer *vb,
  404. struct v4l2_buffer32_time32 __user *arg)
  405. {
  406. struct v4l2_buffer32_time32 vb32;
  407. if (copy_from_user(&vb32, arg, sizeof(vb32)))
  408. return -EFAULT;
  409. *vb = (struct v4l2_buffer) {
  410. .index = vb32.index,
  411. .type = vb32.type,
  412. .bytesused = vb32.bytesused,
  413. .flags = vb32.flags,
  414. .field = vb32.field,
  415. .timestamp.tv_sec = vb32.timestamp.tv_sec,
  416. .timestamp.tv_usec = vb32.timestamp.tv_usec,
  417. .timecode = vb32.timecode,
  418. .sequence = vb32.sequence,
  419. .memory = vb32.memory,
  420. .m.offset = vb32.m.offset,
  421. .length = vb32.length,
  422. .request_fd = vb32.request_fd,
  423. };
  424. switch (vb->memory) {
  425. case V4L2_MEMORY_MMAP:
  426. case V4L2_MEMORY_OVERLAY:
  427. vb->m.offset = vb32.m.offset;
  428. break;
  429. case V4L2_MEMORY_USERPTR:
  430. vb->m.userptr = (unsigned long)compat_ptr(vb32.m.userptr);
  431. break;
  432. case V4L2_MEMORY_DMABUF:
  433. vb->m.fd = vb32.m.fd;
  434. break;
  435. }
  436. if (V4L2_TYPE_IS_MULTIPLANAR(vb->type))
  437. vb->m.planes = (void __force *)
  438. compat_ptr(vb32.m.planes);
  439. return 0;
  440. }
  441. #endif
  442. static int put_v4l2_buffer32(struct v4l2_buffer *vb,
  443. struct v4l2_buffer32 __user *arg)
  444. {
  445. struct v4l2_buffer32 vb32;
  446. memset(&vb32, 0, sizeof(vb32));
  447. vb32 = (struct v4l2_buffer32) {
  448. .index = vb->index,
  449. .type = vb->type,
  450. .bytesused = vb->bytesused,
  451. .flags = vb->flags,
  452. .field = vb->field,
  453. .timestamp.tv_sec = vb->timestamp.tv_sec,
  454. .timestamp.tv_usec = vb->timestamp.tv_usec,
  455. .timecode = vb->timecode,
  456. .sequence = vb->sequence,
  457. .memory = vb->memory,
  458. .m.offset = vb->m.offset,
  459. .length = vb->length,
  460. .request_fd = vb->request_fd,
  461. };
  462. switch (vb->memory) {
  463. case V4L2_MEMORY_MMAP:
  464. case V4L2_MEMORY_OVERLAY:
  465. vb32.m.offset = vb->m.offset;
  466. break;
  467. case V4L2_MEMORY_USERPTR:
  468. vb32.m.userptr = (uintptr_t)(vb->m.userptr);
  469. break;
  470. case V4L2_MEMORY_DMABUF:
  471. vb32.m.fd = vb->m.fd;
  472. break;
  473. }
  474. if (V4L2_TYPE_IS_MULTIPLANAR(vb->type))
  475. vb32.m.planes = (uintptr_t)vb->m.planes;
  476. if (copy_to_user(arg, &vb32, sizeof(vb32)))
  477. return -EFAULT;
  478. return 0;
  479. }
  480. #ifdef CONFIG_COMPAT_32BIT_TIME
  481. static int put_v4l2_buffer32_time32(struct v4l2_buffer *vb,
  482. struct v4l2_buffer32_time32 __user *arg)
  483. {
  484. struct v4l2_buffer32_time32 vb32;
  485. memset(&vb32, 0, sizeof(vb32));
  486. vb32 = (struct v4l2_buffer32_time32) {
  487. .index = vb->index,
  488. .type = vb->type,
  489. .bytesused = vb->bytesused,
  490. .flags = vb->flags,
  491. .field = vb->field,
  492. .timestamp.tv_sec = vb->timestamp.tv_sec,
  493. .timestamp.tv_usec = vb->timestamp.tv_usec,
  494. .timecode = vb->timecode,
  495. .sequence = vb->sequence,
  496. .memory = vb->memory,
  497. .m.offset = vb->m.offset,
  498. .length = vb->length,
  499. .request_fd = vb->request_fd,
  500. };
  501. switch (vb->memory) {
  502. case V4L2_MEMORY_MMAP:
  503. case V4L2_MEMORY_OVERLAY:
  504. vb32.m.offset = vb->m.offset;
  505. break;
  506. case V4L2_MEMORY_USERPTR:
  507. vb32.m.userptr = (uintptr_t)(vb->m.userptr);
  508. break;
  509. case V4L2_MEMORY_DMABUF:
  510. vb32.m.fd = vb->m.fd;
  511. break;
  512. }
  513. if (V4L2_TYPE_IS_MULTIPLANAR(vb->type))
  514. vb32.m.planes = (uintptr_t)vb->m.planes;
  515. if (copy_to_user(arg, &vb32, sizeof(vb32)))
  516. return -EFAULT;
  517. return 0;
  518. }
  519. #endif
  520. struct v4l2_framebuffer32 {
  521. __u32 capability;
  522. __u32 flags;
  523. compat_caddr_t base;
  524. struct {
  525. __u32 width;
  526. __u32 height;
  527. __u32 pixelformat;
  528. __u32 field;
  529. __u32 bytesperline;
  530. __u32 sizeimage;
  531. __u32 colorspace;
  532. __u32 priv;
  533. } fmt;
  534. };
  535. static int get_v4l2_framebuffer32(struct v4l2_framebuffer *p64,
  536. struct v4l2_framebuffer32 __user *p32)
  537. {
  538. if (get_user(p64->capability, &p32->capability) ||
  539. get_user(p64->flags, &p32->flags) ||
  540. copy_from_user(&p64->fmt, &p32->fmt, sizeof(p64->fmt)))
  541. return -EFAULT;
  542. p64->base = NULL;
  543. return 0;
  544. }
  545. static int put_v4l2_framebuffer32(struct v4l2_framebuffer *p64,
  546. struct v4l2_framebuffer32 __user *p32)
  547. {
  548. if (put_user((uintptr_t)p64->base, &p32->base) ||
  549. put_user(p64->capability, &p32->capability) ||
  550. put_user(p64->flags, &p32->flags) ||
  551. copy_to_user(&p32->fmt, &p64->fmt, sizeof(p64->fmt)))
  552. return -EFAULT;
  553. return 0;
  554. }
  555. struct v4l2_input32 {
  556. __u32 index; /* Which input */
  557. __u8 name[32]; /* Label */
  558. __u32 type; /* Type of input */
  559. __u32 audioset; /* Associated audios (bitfield) */
  560. __u32 tuner; /* Associated tuner */
  561. compat_u64 std;
  562. __u32 status;
  563. __u32 capabilities;
  564. __u32 reserved[3];
  565. };
  566. /*
  567. * The 64-bit v4l2_input struct has extra padding at the end of the struct.
  568. * Otherwise it is identical to the 32-bit version.
  569. */
  570. static inline int get_v4l2_input32(struct v4l2_input *p64,
  571. struct v4l2_input32 __user *p32)
  572. {
  573. if (copy_from_user(p64, p32, sizeof(*p32)))
  574. return -EFAULT;
  575. return 0;
  576. }
  577. static inline int put_v4l2_input32(struct v4l2_input *p64,
  578. struct v4l2_input32 __user *p32)
  579. {
  580. if (copy_to_user(p32, p64, sizeof(*p32)))
  581. return -EFAULT;
  582. return 0;
  583. }
  584. struct v4l2_ext_controls32 {
  585. __u32 which;
  586. __u32 count;
  587. __u32 error_idx;
  588. __s32 request_fd;
  589. __u32 reserved[1];
  590. compat_caddr_t controls; /* actually struct v4l2_ext_control32 * */
  591. };
  592. struct v4l2_ext_control32 {
  593. __u32 id;
  594. __u32 size;
  595. __u32 reserved2[1];
  596. union {
  597. __s32 value;
  598. __s64 value64;
  599. compat_caddr_t string; /* actually char * */
  600. };
  601. } __attribute__ ((packed));
  602. /* Return true if this control is a pointer type. */
  603. static inline bool ctrl_is_pointer(struct file *file, u32 id)
  604. {
  605. struct video_device *vdev = video_devdata(file);
  606. struct v4l2_fh *fh = NULL;
  607. struct v4l2_ctrl_handler *hdl = NULL;
  608. struct v4l2_query_ext_ctrl qec = { id };
  609. const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
  610. if (test_bit(V4L2_FL_USES_V4L2_FH, &vdev->flags))
  611. fh = file->private_data;
  612. if (fh && fh->ctrl_handler)
  613. hdl = fh->ctrl_handler;
  614. else if (vdev->ctrl_handler)
  615. hdl = vdev->ctrl_handler;
  616. if (hdl) {
  617. struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, id);
  618. return ctrl && ctrl->is_ptr;
  619. }
  620. if (!ops || !ops->vidioc_query_ext_ctrl)
  621. return false;
  622. return !ops->vidioc_query_ext_ctrl(file, fh, &qec) &&
  623. (qec.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD);
  624. }
  625. static int get_v4l2_ext_controls32(struct v4l2_ext_controls *p64,
  626. struct v4l2_ext_controls32 __user *p32)
  627. {
  628. struct v4l2_ext_controls32 ec32;
  629. if (copy_from_user(&ec32, p32, sizeof(ec32)))
  630. return -EFAULT;
  631. *p64 = (struct v4l2_ext_controls) {
  632. .which = ec32.which,
  633. .count = ec32.count,
  634. .error_idx = ec32.error_idx,
  635. .request_fd = ec32.request_fd,
  636. .reserved[0] = ec32.reserved[0],
  637. .controls = (void __force *)compat_ptr(ec32.controls),
  638. };
  639. return 0;
  640. }
  641. static int put_v4l2_ext_controls32(struct v4l2_ext_controls *p64,
  642. struct v4l2_ext_controls32 __user *p32)
  643. {
  644. struct v4l2_ext_controls32 ec32;
  645. memset(&ec32, 0, sizeof(ec32));
  646. ec32 = (struct v4l2_ext_controls32) {
  647. .which = p64->which,
  648. .count = p64->count,
  649. .error_idx = p64->error_idx,
  650. .request_fd = p64->request_fd,
  651. .reserved[0] = p64->reserved[0],
  652. .controls = (uintptr_t)p64->controls,
  653. };
  654. if (copy_to_user(p32, &ec32, sizeof(ec32)))
  655. return -EFAULT;
  656. return 0;
  657. }
  658. #ifdef CONFIG_X86_64
  659. /*
  660. * x86 is the only compat architecture with different struct alignment
  661. * between 32-bit and 64-bit tasks.
  662. */
  663. struct v4l2_event32 {
  664. __u32 type;
  665. union {
  666. compat_s64 value64;
  667. __u8 data[64];
  668. } u;
  669. __u32 pending;
  670. __u32 sequence;
  671. struct {
  672. compat_s64 tv_sec;
  673. compat_s64 tv_nsec;
  674. } timestamp;
  675. __u32 id;
  676. __u32 reserved[8];
  677. };
  678. static int put_v4l2_event32(struct v4l2_event *p64,
  679. struct v4l2_event32 __user *p32)
  680. {
  681. if (put_user(p64->type, &p32->type) ||
  682. copy_to_user(&p32->u, &p64->u, sizeof(p64->u)) ||
  683. put_user(p64->pending, &p32->pending) ||
  684. put_user(p64->sequence, &p32->sequence) ||
  685. put_user(p64->timestamp.tv_sec, &p32->timestamp.tv_sec) ||
  686. put_user(p64->timestamp.tv_nsec, &p32->timestamp.tv_nsec) ||
  687. put_user(p64->id, &p32->id) ||
  688. copy_to_user(p32->reserved, p64->reserved, sizeof(p32->reserved)))
  689. return -EFAULT;
  690. return 0;
  691. }
  692. #endif
  693. #ifdef CONFIG_COMPAT_32BIT_TIME
  694. struct v4l2_event32_time32 {
  695. __u32 type;
  696. union {
  697. compat_s64 value64;
  698. __u8 data[64];
  699. } u;
  700. __u32 pending;
  701. __u32 sequence;
  702. struct old_timespec32 timestamp;
  703. __u32 id;
  704. __u32 reserved[8];
  705. };
  706. static int put_v4l2_event32_time32(struct v4l2_event *p64,
  707. struct v4l2_event32_time32 __user *p32)
  708. {
  709. if (put_user(p64->type, &p32->type) ||
  710. copy_to_user(&p32->u, &p64->u, sizeof(p64->u)) ||
  711. put_user(p64->pending, &p32->pending) ||
  712. put_user(p64->sequence, &p32->sequence) ||
  713. put_user(p64->timestamp.tv_sec, &p32->timestamp.tv_sec) ||
  714. put_user(p64->timestamp.tv_nsec, &p32->timestamp.tv_nsec) ||
  715. put_user(p64->id, &p32->id) ||
  716. copy_to_user(p32->reserved, p64->reserved, sizeof(p32->reserved)))
  717. return -EFAULT;
  718. return 0;
  719. }
  720. #endif
  721. struct v4l2_edid32 {
  722. __u32 pad;
  723. __u32 start_block;
  724. __u32 blocks;
  725. __u32 reserved[5];
  726. compat_caddr_t edid;
  727. };
  728. static int get_v4l2_edid32(struct v4l2_edid *p64,
  729. struct v4l2_edid32 __user *p32)
  730. {
  731. compat_uptr_t edid;
  732. if (copy_from_user(p64, p32, offsetof(struct v4l2_edid32, edid)) ||
  733. get_user(edid, &p32->edid))
  734. return -EFAULT;
  735. p64->edid = (void __force *)compat_ptr(edid);
  736. return 0;
  737. }
  738. static int put_v4l2_edid32(struct v4l2_edid *p64,
  739. struct v4l2_edid32 __user *p32)
  740. {
  741. if (copy_to_user(p32, p64, offsetof(struct v4l2_edid32, edid)))
  742. return -EFAULT;
  743. return 0;
  744. }
  745. /*
  746. * List of ioctls that require 32-bits/64-bits conversion
  747. *
  748. * The V4L2 ioctls that aren't listed there don't have pointer arguments
  749. * and the struct size is identical for both 32 and 64 bits versions, so
  750. * they don't need translations.
  751. */
  752. #define VIDIOC_G_FMT32 _IOWR('V', 4, struct v4l2_format32)
  753. #define VIDIOC_S_FMT32 _IOWR('V', 5, struct v4l2_format32)
  754. #define VIDIOC_QUERYBUF32 _IOWR('V', 9, struct v4l2_buffer32)
  755. #define VIDIOC_G_FBUF32 _IOR ('V', 10, struct v4l2_framebuffer32)
  756. #define VIDIOC_S_FBUF32 _IOW ('V', 11, struct v4l2_framebuffer32)
  757. #define VIDIOC_QBUF32 _IOWR('V', 15, struct v4l2_buffer32)
  758. #define VIDIOC_DQBUF32 _IOWR('V', 17, struct v4l2_buffer32)
  759. #define VIDIOC_ENUMSTD32 _IOWR('V', 25, struct v4l2_standard32)
  760. #define VIDIOC_ENUMINPUT32 _IOWR('V', 26, struct v4l2_input32)
  761. #define VIDIOC_G_EDID32 _IOWR('V', 40, struct v4l2_edid32)
  762. #define VIDIOC_S_EDID32 _IOWR('V', 41, struct v4l2_edid32)
  763. #define VIDIOC_TRY_FMT32 _IOWR('V', 64, struct v4l2_format32)
  764. #define VIDIOC_G_EXT_CTRLS32 _IOWR('V', 71, struct v4l2_ext_controls32)
  765. #define VIDIOC_S_EXT_CTRLS32 _IOWR('V', 72, struct v4l2_ext_controls32)
  766. #define VIDIOC_TRY_EXT_CTRLS32 _IOWR('V', 73, struct v4l2_ext_controls32)
  767. #define VIDIOC_DQEVENT32 _IOR ('V', 89, struct v4l2_event32)
  768. #define VIDIOC_CREATE_BUFS32 _IOWR('V', 92, struct v4l2_create_buffers32)
  769. #define VIDIOC_PREPARE_BUF32 _IOWR('V', 93, struct v4l2_buffer32)
  770. #ifdef CONFIG_COMPAT_32BIT_TIME
  771. #define VIDIOC_QUERYBUF32_TIME32 _IOWR('V', 9, struct v4l2_buffer32_time32)
  772. #define VIDIOC_QBUF32_TIME32 _IOWR('V', 15, struct v4l2_buffer32_time32)
  773. #define VIDIOC_DQBUF32_TIME32 _IOWR('V', 17, struct v4l2_buffer32_time32)
  774. #define VIDIOC_DQEVENT32_TIME32 _IOR ('V', 89, struct v4l2_event32_time32)
  775. #define VIDIOC_PREPARE_BUF32_TIME32 _IOWR('V', 93, struct v4l2_buffer32_time32)
  776. #endif
  777. unsigned int v4l2_compat_translate_cmd(unsigned int cmd)
  778. {
  779. switch (cmd) {
  780. case VIDIOC_G_FMT32:
  781. return VIDIOC_G_FMT;
  782. case VIDIOC_S_FMT32:
  783. return VIDIOC_S_FMT;
  784. case VIDIOC_TRY_FMT32:
  785. return VIDIOC_TRY_FMT;
  786. case VIDIOC_G_FBUF32:
  787. return VIDIOC_G_FBUF;
  788. case VIDIOC_S_FBUF32:
  789. return VIDIOC_S_FBUF;
  790. #ifdef CONFIG_COMPAT_32BIT_TIME
  791. case VIDIOC_QUERYBUF32_TIME32:
  792. return VIDIOC_QUERYBUF;
  793. case VIDIOC_QBUF32_TIME32:
  794. return VIDIOC_QBUF;
  795. case VIDIOC_DQBUF32_TIME32:
  796. return VIDIOC_DQBUF;
  797. case VIDIOC_PREPARE_BUF32_TIME32:
  798. return VIDIOC_PREPARE_BUF;
  799. #endif
  800. case VIDIOC_QUERYBUF32:
  801. return VIDIOC_QUERYBUF;
  802. case VIDIOC_QBUF32:
  803. return VIDIOC_QBUF;
  804. case VIDIOC_DQBUF32:
  805. return VIDIOC_DQBUF;
  806. case VIDIOC_CREATE_BUFS32:
  807. return VIDIOC_CREATE_BUFS;
  808. case VIDIOC_G_EXT_CTRLS32:
  809. return VIDIOC_G_EXT_CTRLS;
  810. case VIDIOC_S_EXT_CTRLS32:
  811. return VIDIOC_S_EXT_CTRLS;
  812. case VIDIOC_TRY_EXT_CTRLS32:
  813. return VIDIOC_TRY_EXT_CTRLS;
  814. case VIDIOC_PREPARE_BUF32:
  815. return VIDIOC_PREPARE_BUF;
  816. case VIDIOC_ENUMSTD32:
  817. return VIDIOC_ENUMSTD;
  818. case VIDIOC_ENUMINPUT32:
  819. return VIDIOC_ENUMINPUT;
  820. case VIDIOC_G_EDID32:
  821. return VIDIOC_G_EDID;
  822. case VIDIOC_S_EDID32:
  823. return VIDIOC_S_EDID;
  824. #ifdef CONFIG_X86_64
  825. case VIDIOC_DQEVENT32:
  826. return VIDIOC_DQEVENT;
  827. #endif
  828. #ifdef CONFIG_COMPAT_32BIT_TIME
  829. case VIDIOC_DQEVENT32_TIME32:
  830. return VIDIOC_DQEVENT;
  831. #endif
  832. }
  833. return cmd;
  834. }
  835. int v4l2_compat_get_user(void __user *arg, void *parg, unsigned int cmd)
  836. {
  837. switch (cmd) {
  838. case VIDIOC_G_FMT32:
  839. case VIDIOC_S_FMT32:
  840. case VIDIOC_TRY_FMT32:
  841. return get_v4l2_format32(parg, arg);
  842. case VIDIOC_S_FBUF32:
  843. return get_v4l2_framebuffer32(parg, arg);
  844. #ifdef CONFIG_COMPAT_32BIT_TIME
  845. case VIDIOC_QUERYBUF32_TIME32:
  846. case VIDIOC_QBUF32_TIME32:
  847. case VIDIOC_DQBUF32_TIME32:
  848. case VIDIOC_PREPARE_BUF32_TIME32:
  849. return get_v4l2_buffer32_time32(parg, arg);
  850. #endif
  851. case VIDIOC_QUERYBUF32:
  852. case VIDIOC_QBUF32:
  853. case VIDIOC_DQBUF32:
  854. case VIDIOC_PREPARE_BUF32:
  855. return get_v4l2_buffer32(parg, arg);
  856. case VIDIOC_G_EXT_CTRLS32:
  857. case VIDIOC_S_EXT_CTRLS32:
  858. case VIDIOC_TRY_EXT_CTRLS32:
  859. return get_v4l2_ext_controls32(parg, arg);
  860. case VIDIOC_CREATE_BUFS32:
  861. return get_v4l2_create32(parg, arg);
  862. case VIDIOC_ENUMSTD32:
  863. return get_v4l2_standard32(parg, arg);
  864. case VIDIOC_ENUMINPUT32:
  865. return get_v4l2_input32(parg, arg);
  866. case VIDIOC_G_EDID32:
  867. case VIDIOC_S_EDID32:
  868. return get_v4l2_edid32(parg, arg);
  869. }
  870. return 0;
  871. }
  872. int v4l2_compat_put_user(void __user *arg, void *parg, unsigned int cmd)
  873. {
  874. switch (cmd) {
  875. case VIDIOC_G_FMT32:
  876. case VIDIOC_S_FMT32:
  877. case VIDIOC_TRY_FMT32:
  878. return put_v4l2_format32(parg, arg);
  879. case VIDIOC_G_FBUF32:
  880. return put_v4l2_framebuffer32(parg, arg);
  881. #ifdef CONFIG_COMPAT_32BIT_TIME
  882. case VIDIOC_QUERYBUF32_TIME32:
  883. case VIDIOC_QBUF32_TIME32:
  884. case VIDIOC_DQBUF32_TIME32:
  885. case VIDIOC_PREPARE_BUF32_TIME32:
  886. return put_v4l2_buffer32_time32(parg, arg);
  887. #endif
  888. case VIDIOC_QUERYBUF32:
  889. case VIDIOC_QBUF32:
  890. case VIDIOC_DQBUF32:
  891. case VIDIOC_PREPARE_BUF32:
  892. return put_v4l2_buffer32(parg, arg);
  893. case VIDIOC_G_EXT_CTRLS32:
  894. case VIDIOC_S_EXT_CTRLS32:
  895. case VIDIOC_TRY_EXT_CTRLS32:
  896. return put_v4l2_ext_controls32(parg, arg);
  897. case VIDIOC_CREATE_BUFS32:
  898. return put_v4l2_create32(parg, arg);
  899. case VIDIOC_ENUMSTD32:
  900. return put_v4l2_standard32(parg, arg);
  901. case VIDIOC_ENUMINPUT32:
  902. return put_v4l2_input32(parg, arg);
  903. case VIDIOC_G_EDID32:
  904. case VIDIOC_S_EDID32:
  905. return put_v4l2_edid32(parg, arg);
  906. #ifdef CONFIG_X86_64
  907. case VIDIOC_DQEVENT32:
  908. return put_v4l2_event32(parg, arg);
  909. #endif
  910. #ifdef CONFIG_COMPAT_32BIT_TIME
  911. case VIDIOC_DQEVENT32_TIME32:
  912. return put_v4l2_event32_time32(parg, arg);
  913. #endif
  914. }
  915. return 0;
  916. }
  917. int v4l2_compat_get_array_args(struct file *file, void *mbuf,
  918. void __user *user_ptr, size_t array_size,
  919. unsigned int cmd, void *arg)
  920. {
  921. int err = 0;
  922. memset(mbuf, 0, array_size);
  923. switch (cmd) {
  924. #ifdef CONFIG_COMPAT_32BIT_TIME
  925. case VIDIOC_QUERYBUF32_TIME32:
  926. case VIDIOC_QBUF32_TIME32:
  927. case VIDIOC_DQBUF32_TIME32:
  928. case VIDIOC_PREPARE_BUF32_TIME32:
  929. #endif
  930. case VIDIOC_QUERYBUF32:
  931. case VIDIOC_QBUF32:
  932. case VIDIOC_DQBUF32:
  933. case VIDIOC_PREPARE_BUF32: {
  934. struct v4l2_buffer *b64 = arg;
  935. struct v4l2_plane *p64 = mbuf;
  936. struct v4l2_plane32 __user *p32 = user_ptr;
  937. if (V4L2_TYPE_IS_MULTIPLANAR(b64->type)) {
  938. u32 num_planes = b64->length;
  939. if (num_planes == 0)
  940. return 0;
  941. while (num_planes--) {
  942. err = get_v4l2_plane32(p64, p32, b64->memory);
  943. if (err)
  944. return err;
  945. ++p64;
  946. ++p32;
  947. }
  948. }
  949. break;
  950. }
  951. case VIDIOC_G_EXT_CTRLS32:
  952. case VIDIOC_S_EXT_CTRLS32:
  953. case VIDIOC_TRY_EXT_CTRLS32: {
  954. struct v4l2_ext_controls *ecs64 = arg;
  955. struct v4l2_ext_control *ec64 = mbuf;
  956. struct v4l2_ext_control32 __user *ec32 = user_ptr;
  957. int n;
  958. for (n = 0; n < ecs64->count; n++) {
  959. if (copy_from_user(ec64, ec32, sizeof(*ec32)))
  960. return -EFAULT;
  961. if (ctrl_is_pointer(file, ec64->id)) {
  962. compat_uptr_t p;
  963. if (get_user(p, &ec32->string))
  964. return -EFAULT;
  965. ec64->string = compat_ptr(p);
  966. }
  967. ec32++;
  968. ec64++;
  969. }
  970. break;
  971. }
  972. default:
  973. if (copy_from_user(mbuf, user_ptr, array_size))
  974. err = -EFAULT;
  975. break;
  976. }
  977. return err;
  978. }
  979. int v4l2_compat_put_array_args(struct file *file, void __user *user_ptr,
  980. void *mbuf, size_t array_size,
  981. unsigned int cmd, void *arg)
  982. {
  983. int err = 0;
  984. switch (cmd) {
  985. #ifdef CONFIG_COMPAT_32BIT_TIME
  986. case VIDIOC_QUERYBUF32_TIME32:
  987. case VIDIOC_QBUF32_TIME32:
  988. case VIDIOC_DQBUF32_TIME32:
  989. case VIDIOC_PREPARE_BUF32_TIME32:
  990. #endif
  991. case VIDIOC_QUERYBUF32:
  992. case VIDIOC_QBUF32:
  993. case VIDIOC_DQBUF32:
  994. case VIDIOC_PREPARE_BUF32: {
  995. struct v4l2_buffer *b64 = arg;
  996. struct v4l2_plane *p64 = mbuf;
  997. struct v4l2_plane32 __user *p32 = user_ptr;
  998. if (V4L2_TYPE_IS_MULTIPLANAR(b64->type)) {
  999. u32 num_planes = b64->length;
  1000. if (num_planes == 0)
  1001. return 0;
  1002. while (num_planes--) {
  1003. err = put_v4l2_plane32(p64, p32, b64->memory);
  1004. if (err)
  1005. return err;
  1006. ++p64;
  1007. ++p32;
  1008. }
  1009. }
  1010. break;
  1011. }
  1012. case VIDIOC_G_EXT_CTRLS32:
  1013. case VIDIOC_S_EXT_CTRLS32:
  1014. case VIDIOC_TRY_EXT_CTRLS32: {
  1015. struct v4l2_ext_controls *ecs64 = arg;
  1016. struct v4l2_ext_control *ec64 = mbuf;
  1017. struct v4l2_ext_control32 __user *ec32 = user_ptr;
  1018. int n;
  1019. for (n = 0; n < ecs64->count; n++) {
  1020. unsigned int size = sizeof(*ec32);
  1021. /*
  1022. * Do not modify the pointer when copying a pointer
  1023. * control. The contents of the pointer was changed,
  1024. * not the pointer itself.
  1025. * The structures are otherwise compatible.
  1026. */
  1027. if (ctrl_is_pointer(file, ec64->id))
  1028. size -= sizeof(ec32->value64);
  1029. if (copy_to_user(ec32, ec64, size))
  1030. return -EFAULT;
  1031. ec32++;
  1032. ec64++;
  1033. }
  1034. break;
  1035. }
  1036. default:
  1037. if (copy_to_user(user_ptr, mbuf, array_size))
  1038. err = -EFAULT;
  1039. break;
  1040. }
  1041. return err;
  1042. }
  1043. /**
  1044. * v4l2_compat_ioctl32() - Handles a compat32 ioctl call
  1045. *
  1046. * @file: pointer to &struct file with the file handler
  1047. * @cmd: ioctl to be called
  1048. * @arg: arguments passed from/to the ioctl handler
  1049. *
  1050. * This function is meant to be used as .compat_ioctl fops at v4l2-dev.c
  1051. * in order to deal with 32-bit calls on a 64-bits Kernel.
  1052. *
  1053. * This function calls do_video_ioctl() for non-private V4L2 ioctls.
  1054. * If the function is a private one it calls vdev->fops->compat_ioctl32
  1055. * instead.
  1056. */
  1057. long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
  1058. {
  1059. struct video_device *vdev = video_devdata(file);
  1060. long ret = -ENOIOCTLCMD;
  1061. if (!file->f_op->unlocked_ioctl)
  1062. return ret;
  1063. if (!video_is_registered(vdev))
  1064. return -ENODEV;
  1065. if (_IOC_TYPE(cmd) == 'V' && _IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
  1066. ret = file->f_op->unlocked_ioctl(file, cmd,
  1067. (unsigned long)compat_ptr(arg));
  1068. else if (vdev->fops->compat_ioctl32)
  1069. ret = vdev->fops->compat_ioctl32(file, cmd, arg);
  1070. if (ret == -ENOIOCTLCMD)
  1071. pr_debug("compat_ioctl32: unknown ioctl '%c', dir=%d, #%d (0x%08x)\n",
  1072. _IOC_TYPE(cmd), _IOC_DIR(cmd), _IOC_NR(cmd), cmd);
  1073. return ret;
  1074. }
  1075. EXPORT_SYMBOL_GPL(v4l2_compat_ioctl32);