v4l2-compat-ioctl32.c 34 KB

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