v4l2-compat-ioctl32.c 28 KB

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