v4l2-compat-ioctl32.c 30 KB

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