pcm_compat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * 32bit -> 64bit ioctl wrapper for PCM API
  3. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /* This file included from pcm_native.c */
  21. #include <linux/compat.h>
  22. static int snd_pcm_ioctl_delay_compat(snd_pcm_substream_t *substream,
  23. s32 __user *src)
  24. {
  25. snd_pcm_sframes_t delay;
  26. mm_segment_t fs;
  27. int err;
  28. fs = snd_enter_user();
  29. err = snd_pcm_delay(substream, &delay);
  30. snd_leave_user(fs);
  31. if (err < 0)
  32. return err;
  33. if (put_user(delay, src))
  34. return -EFAULT;
  35. return err;
  36. }
  37. static int snd_pcm_ioctl_rewind_compat(snd_pcm_substream_t *substream,
  38. u32 __user *src)
  39. {
  40. snd_pcm_uframes_t frames;
  41. int err;
  42. if (get_user(frames, src))
  43. return -EFAULT;
  44. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  45. err = snd_pcm_playback_rewind(substream, frames);
  46. else
  47. err = snd_pcm_capture_rewind(substream, frames);
  48. if (put_user(err, src))
  49. return -EFAULT;
  50. return err < 0 ? err : 0;
  51. }
  52. static int snd_pcm_ioctl_forward_compat(snd_pcm_substream_t *substream,
  53. u32 __user *src)
  54. {
  55. snd_pcm_uframes_t frames;
  56. int err;
  57. if (get_user(frames, src))
  58. return -EFAULT;
  59. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  60. err = snd_pcm_playback_forward(substream, frames);
  61. else
  62. err = snd_pcm_capture_forward(substream, frames);
  63. if (put_user(err, src))
  64. return -EFAULT;
  65. return err < 0 ? err : 0;
  66. }
  67. struct sndrv_pcm_hw_params32 {
  68. u32 flags;
  69. struct sndrv_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK - SNDRV_PCM_HW_PARAM_FIRST_MASK + 1]; /* this must be identical */
  70. struct sndrv_mask mres[5]; /* reserved masks */
  71. struct sndrv_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
  72. struct sndrv_interval ires[9]; /* reserved intervals */
  73. u32 rmask;
  74. u32 cmask;
  75. u32 info;
  76. u32 msbits;
  77. u32 rate_num;
  78. u32 rate_den;
  79. u32 fifo_size;
  80. unsigned char reserved[64];
  81. };
  82. struct sndrv_pcm_sw_params32 {
  83. s32 tstamp_mode;
  84. u32 period_step;
  85. u32 sleep_min;
  86. u32 avail_min;
  87. u32 xfer_align;
  88. u32 start_threshold;
  89. u32 stop_threshold;
  90. u32 silence_threshold;
  91. u32 silence_size;
  92. u32 boundary;
  93. unsigned char reserved[64];
  94. };
  95. static int snd_pcm_ioctl_sw_params_compat(snd_pcm_substream_t *substream,
  96. struct sndrv_pcm_sw_params32 __user *src)
  97. {
  98. snd_pcm_sw_params_t params;
  99. int err;
  100. memset(&params, 0, sizeof(params));
  101. if (get_user(params.tstamp_mode, &src->tstamp_mode) ||
  102. get_user(params.period_step, &src->period_step) ||
  103. get_user(params.sleep_min, &src->sleep_min) ||
  104. get_user(params.avail_min, &src->avail_min) ||
  105. get_user(params.xfer_align, &src->xfer_align) ||
  106. get_user(params.start_threshold, &src->start_threshold) ||
  107. get_user(params.stop_threshold, &src->stop_threshold) ||
  108. get_user(params.silence_threshold, &src->silence_threshold) ||
  109. get_user(params.silence_size, &src->silence_size))
  110. return -EFAULT;
  111. err = snd_pcm_sw_params(substream, &params);
  112. if (err < 0)
  113. return err;
  114. if (put_user(params.boundary, &src->boundary))
  115. return -EFAULT;
  116. return err;
  117. }
  118. struct sndrv_pcm_channel_info32 {
  119. u32 channel;
  120. u32 offset;
  121. u32 first;
  122. u32 step;
  123. };
  124. static int snd_pcm_ioctl_channel_info_compat(snd_pcm_substream_t *substream,
  125. struct sndrv_pcm_channel_info32 __user *src)
  126. {
  127. snd_pcm_channel_info_t info;
  128. int err;
  129. if (get_user(info.channel, &src->channel) ||
  130. get_user(info.offset, &src->offset) ||
  131. get_user(info.first, &src->first) ||
  132. get_user(info.step, &src->step))
  133. return -EFAULT;
  134. err = snd_pcm_channel_info(substream, &info);
  135. if (err < 0)
  136. return err;
  137. if (put_user(info.channel, &src->channel) ||
  138. put_user(info.offset, &src->offset) ||
  139. put_user(info.first, &src->first) ||
  140. put_user(info.step, &src->step))
  141. return -EFAULT;
  142. return err;
  143. }
  144. struct sndrv_pcm_status32 {
  145. s32 state;
  146. struct compat_timespec trigger_tstamp;
  147. struct compat_timespec tstamp;
  148. u32 appl_ptr;
  149. u32 hw_ptr;
  150. s32 delay;
  151. u32 avail;
  152. u32 avail_max;
  153. u32 overrange;
  154. s32 suspended_state;
  155. unsigned char reserved[60];
  156. } __attribute__((packed));
  157. static int snd_pcm_status_user_compat(snd_pcm_substream_t *substream,
  158. struct sndrv_pcm_status32 __user *src)
  159. {
  160. snd_pcm_status_t status;
  161. int err;
  162. err = snd_pcm_status(substream, &status);
  163. if (err < 0)
  164. return err;
  165. if (put_user(status.state, &src->state) ||
  166. put_user(status.trigger_tstamp.tv_sec, &src->trigger_tstamp.tv_sec) ||
  167. put_user(status.trigger_tstamp.tv_nsec, &src->trigger_tstamp.tv_nsec) ||
  168. put_user(status.tstamp.tv_sec, &src->tstamp.tv_sec) ||
  169. put_user(status.tstamp.tv_nsec, &src->tstamp.tv_nsec) ||
  170. put_user(status.appl_ptr, &src->appl_ptr) ||
  171. put_user(status.hw_ptr, &src->hw_ptr) ||
  172. put_user(status.delay, &src->delay) ||
  173. put_user(status.avail, &src->avail) ||
  174. put_user(status.avail_max, &src->avail_max) ||
  175. put_user(status.overrange, &src->overrange) ||
  176. put_user(status.suspended_state, &src->suspended_state))
  177. return -EFAULT;
  178. return err;
  179. }
  180. /* recalcuate the boundary within 32bit */
  181. static void recalculate_boundary(snd_pcm_runtime_t *runtime)
  182. {
  183. if (! runtime->buffer_size)
  184. return;
  185. runtime->boundary = runtime->buffer_size;
  186. while (runtime->boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
  187. runtime->boundary *= 2;
  188. }
  189. /* both for HW_PARAMS and HW_REFINE */
  190. static int snd_pcm_ioctl_hw_params_compat(snd_pcm_substream_t *substream,
  191. int refine,
  192. struct sndrv_pcm_hw_params32 __user *data32)
  193. {
  194. struct sndrv_pcm_hw_params *data;
  195. snd_pcm_runtime_t *runtime;
  196. int err;
  197. if (! (runtime = substream->runtime))
  198. return -ENOTTY;
  199. data = kmalloc(sizeof(*data), GFP_KERNEL);
  200. if (data == NULL)
  201. return -ENOMEM;
  202. /* only fifo_size is different, so just copy all */
  203. if (copy_from_user(data, data32, sizeof(*data32))) {
  204. err = -EFAULT;
  205. goto error;
  206. }
  207. if (refine)
  208. err = snd_pcm_hw_refine(substream, data);
  209. else
  210. err = snd_pcm_hw_params(substream, data);
  211. if (err < 0)
  212. goto error;
  213. if (copy_to_user(data32, data, sizeof(*data32)) ||
  214. put_user(data->fifo_size, &data32->fifo_size)) {
  215. err = -EFAULT;
  216. goto error;
  217. }
  218. if (! refine)
  219. recalculate_boundary(runtime);
  220. error:
  221. kfree(data);
  222. return err;
  223. }
  224. /*
  225. */
  226. struct sndrv_xferi32 {
  227. s32 result;
  228. u32 buf;
  229. u32 frames;
  230. };
  231. static int snd_pcm_ioctl_xferi_compat(snd_pcm_substream_t *substream,
  232. int dir, struct sndrv_xferi32 __user *data32)
  233. {
  234. compat_caddr_t buf;
  235. u32 frames;
  236. int err;
  237. if (! substream->runtime)
  238. return -ENOTTY;
  239. if (substream->stream != dir)
  240. return -EINVAL;
  241. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
  242. return -EBADFD;
  243. if (get_user(buf, &data32->buf) ||
  244. get_user(frames, &data32->frames))
  245. return -EFAULT;
  246. if (dir == SNDRV_PCM_STREAM_PLAYBACK)
  247. err = snd_pcm_lib_write(substream, compat_ptr(buf), frames);
  248. else
  249. err = snd_pcm_lib_read(substream, compat_ptr(buf), frames);
  250. if (err < 0)
  251. return err;
  252. /* copy the result */
  253. if (put_user(err, &data32->result))
  254. return -EFAULT;
  255. return 0;
  256. }
  257. /* snd_xfern needs remapping of bufs */
  258. struct sndrv_xfern32 {
  259. s32 result;
  260. u32 bufs; /* this is void **; */
  261. u32 frames;
  262. };
  263. /*
  264. * xfern ioctl nees to copy (up to) 128 pointers on stack.
  265. * although we may pass the copied pointers through f_op->ioctl, but the ioctl
  266. * handler there expands again the same 128 pointers on stack, so it is better
  267. * to handle the function (calling pcm_readv/writev) directly in this handler.
  268. */
  269. static int snd_pcm_ioctl_xfern_compat(snd_pcm_substream_t *substream,
  270. int dir, struct sndrv_xfern32 __user *data32)
  271. {
  272. compat_caddr_t buf;
  273. compat_caddr_t __user *bufptr;
  274. u32 frames;
  275. void __user **bufs;
  276. int err, ch, i;
  277. if (! substream->runtime)
  278. return -ENOTTY;
  279. if (substream->stream != dir)
  280. return -EINVAL;
  281. if ((ch = substream->runtime->channels) > 128)
  282. return -EINVAL;
  283. if (get_user(buf, &data32->bufs) ||
  284. get_user(frames, &data32->frames))
  285. return -EFAULT;
  286. bufptr = compat_ptr(buf);
  287. bufs = kmalloc(sizeof(void __user *) * ch, GFP_KERNEL);
  288. if (bufs == NULL)
  289. return -ENOMEM;
  290. for (i = 0; i < ch; i++) {
  291. u32 ptr;
  292. if (get_user(ptr, bufptr)) {
  293. kfree(bufs);
  294. return -EFAULT;
  295. }
  296. bufs[ch] = compat_ptr(ptr);
  297. bufptr++;
  298. }
  299. if (dir == SNDRV_PCM_STREAM_PLAYBACK)
  300. err = snd_pcm_lib_writev(substream, bufs, frames);
  301. else
  302. err = snd_pcm_lib_readv(substream, bufs, frames);
  303. if (err >= 0) {
  304. if (put_user(err, &data32->result))
  305. err = -EFAULT;
  306. }
  307. kfree(bufs);
  308. return err;
  309. }
  310. struct sndrv_pcm_mmap_status32 {
  311. s32 state;
  312. s32 pad1;
  313. u32 hw_ptr;
  314. struct compat_timespec tstamp;
  315. s32 suspended_state;
  316. } __attribute__((packed));
  317. struct sndrv_pcm_mmap_control32 {
  318. u32 appl_ptr;
  319. u32 avail_min;
  320. };
  321. struct sndrv_pcm_sync_ptr32 {
  322. u32 flags;
  323. union {
  324. struct sndrv_pcm_mmap_status32 status;
  325. unsigned char reserved[64];
  326. } s;
  327. union {
  328. struct sndrv_pcm_mmap_control32 control;
  329. unsigned char reserved[64];
  330. } c;
  331. } __attribute__((packed));
  332. static int snd_pcm_ioctl_sync_ptr_compat(snd_pcm_substream_t *substream,
  333. struct sndrv_pcm_sync_ptr32 __user *src)
  334. {
  335. snd_pcm_runtime_t *runtime = substream->runtime;
  336. volatile struct sndrv_pcm_mmap_status *status;
  337. volatile struct sndrv_pcm_mmap_control *control;
  338. u32 sflags;
  339. struct sndrv_pcm_mmap_control scontrol;
  340. struct sndrv_pcm_mmap_status sstatus;
  341. int err;
  342. snd_assert(runtime, return -EINVAL);
  343. if (get_user(sflags, &src->flags) ||
  344. get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
  345. get_user(scontrol.avail_min, &src->c.control.avail_min))
  346. return -EFAULT;
  347. if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
  348. err = snd_pcm_hwsync(substream);
  349. if (err < 0)
  350. return err;
  351. }
  352. status = runtime->status;
  353. control = runtime->control;
  354. snd_pcm_stream_lock_irq(substream);
  355. if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
  356. control->appl_ptr = scontrol.appl_ptr;
  357. else
  358. scontrol.appl_ptr = control->appl_ptr;
  359. if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
  360. control->avail_min = scontrol.avail_min;
  361. else
  362. scontrol.avail_min = control->avail_min;
  363. sstatus.state = status->state;
  364. sstatus.hw_ptr = status->hw_ptr;
  365. sstatus.tstamp = status->tstamp;
  366. sstatus.suspended_state = status->suspended_state;
  367. snd_pcm_stream_unlock_irq(substream);
  368. if (put_user(sstatus.state, &src->s.status.state) ||
  369. put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
  370. put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp.tv_sec) ||
  371. put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp.tv_nsec) ||
  372. put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
  373. put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
  374. put_user(scontrol.avail_min, &src->c.control.avail_min))
  375. return -EFAULT;
  376. return 0;
  377. }
  378. /*
  379. */
  380. enum {
  381. SNDRV_PCM_IOCTL_HW_REFINE32 = _IOWR('A', 0x10, struct sndrv_pcm_hw_params32),
  382. SNDRV_PCM_IOCTL_HW_PARAMS32 = _IOWR('A', 0x11, struct sndrv_pcm_hw_params32),
  383. SNDRV_PCM_IOCTL_SW_PARAMS32 = _IOWR('A', 0x13, struct sndrv_pcm_sw_params32),
  384. SNDRV_PCM_IOCTL_STATUS32 = _IOR('A', 0x20, struct sndrv_pcm_status32),
  385. SNDRV_PCM_IOCTL_DELAY32 = _IOR('A', 0x21, s32),
  386. SNDRV_PCM_IOCTL_CHANNEL_INFO32 = _IOR('A', 0x32, struct sndrv_pcm_channel_info32),
  387. SNDRV_PCM_IOCTL_REWIND32 = _IOW('A', 0x46, u32),
  388. SNDRV_PCM_IOCTL_FORWARD32 = _IOW('A', 0x49, u32),
  389. SNDRV_PCM_IOCTL_WRITEI_FRAMES32 = _IOW('A', 0x50, struct sndrv_xferi32),
  390. SNDRV_PCM_IOCTL_READI_FRAMES32 = _IOR('A', 0x51, struct sndrv_xferi32),
  391. SNDRV_PCM_IOCTL_WRITEN_FRAMES32 = _IOW('A', 0x52, struct sndrv_xfern32),
  392. SNDRV_PCM_IOCTL_READN_FRAMES32 = _IOR('A', 0x53, struct sndrv_xfern32),
  393. SNDRV_PCM_IOCTL_SYNC_PTR32 = _IOWR('A', 0x23, struct sndrv_pcm_sync_ptr32),
  394. };
  395. static long snd_pcm_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  396. {
  397. snd_pcm_file_t *pcm_file;
  398. snd_pcm_substream_t *substream;
  399. void __user *argp = compat_ptr(arg);
  400. pcm_file = file->private_data;
  401. if (! pcm_file)
  402. return -ENOTTY;
  403. substream = pcm_file->substream;
  404. if (! substream)
  405. return -ENOTTY;
  406. /*
  407. * When PCM is used on 32bit mode, we need to disable
  408. * mmap of PCM status/control records because of the size
  409. * incompatibility.
  410. */
  411. substream->no_mmap_ctrl = 1;
  412. switch (cmd) {
  413. case SNDRV_PCM_IOCTL_PVERSION:
  414. case SNDRV_PCM_IOCTL_INFO:
  415. case SNDRV_PCM_IOCTL_TSTAMP:
  416. case SNDRV_PCM_IOCTL_HWSYNC:
  417. case SNDRV_PCM_IOCTL_PREPARE:
  418. case SNDRV_PCM_IOCTL_RESET:
  419. case SNDRV_PCM_IOCTL_START:
  420. case SNDRV_PCM_IOCTL_DROP:
  421. case SNDRV_PCM_IOCTL_DRAIN:
  422. case SNDRV_PCM_IOCTL_PAUSE:
  423. case SNDRV_PCM_IOCTL_HW_FREE:
  424. case SNDRV_PCM_IOCTL_RESUME:
  425. case SNDRV_PCM_IOCTL_XRUN:
  426. case SNDRV_PCM_IOCTL_LINK:
  427. case SNDRV_PCM_IOCTL_UNLINK:
  428. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  429. return snd_pcm_playback_ioctl1(substream, cmd, argp);
  430. else
  431. return snd_pcm_capture_ioctl1(substream, cmd, argp);
  432. case SNDRV_PCM_IOCTL_HW_REFINE32:
  433. return snd_pcm_ioctl_hw_params_compat(substream, 1, argp);
  434. case SNDRV_PCM_IOCTL_HW_PARAMS32:
  435. return snd_pcm_ioctl_hw_params_compat(substream, 0, argp);
  436. case SNDRV_PCM_IOCTL_SW_PARAMS32:
  437. return snd_pcm_ioctl_sw_params_compat(substream, argp);
  438. case SNDRV_PCM_IOCTL_STATUS32:
  439. return snd_pcm_status_user_compat(substream, argp);
  440. case SNDRV_PCM_IOCTL_SYNC_PTR32:
  441. return snd_pcm_ioctl_sync_ptr_compat(substream, argp);
  442. case SNDRV_PCM_IOCTL_CHANNEL_INFO32:
  443. return snd_pcm_ioctl_channel_info_compat(substream, argp);
  444. case SNDRV_PCM_IOCTL_WRITEI_FRAMES32:
  445. return snd_pcm_ioctl_xferi_compat(substream, SNDRV_PCM_STREAM_PLAYBACK, argp);
  446. case SNDRV_PCM_IOCTL_READI_FRAMES32:
  447. return snd_pcm_ioctl_xferi_compat(substream, SNDRV_PCM_STREAM_CAPTURE, argp);
  448. case SNDRV_PCM_IOCTL_WRITEN_FRAMES32:
  449. return snd_pcm_ioctl_xfern_compat(substream, SNDRV_PCM_STREAM_PLAYBACK, argp);
  450. case SNDRV_PCM_IOCTL_READN_FRAMES32:
  451. return snd_pcm_ioctl_xfern_compat(substream, SNDRV_PCM_STREAM_CAPTURE, argp);
  452. case SNDRV_PCM_IOCTL_DELAY32:
  453. return snd_pcm_ioctl_delay_compat(substream, argp);
  454. case SNDRV_PCM_IOCTL_REWIND32:
  455. return snd_pcm_ioctl_rewind_compat(substream, argp);
  456. case SNDRV_PCM_IOCTL_FORWARD32:
  457. return snd_pcm_ioctl_forward_compat(substream, argp);
  458. }
  459. return -ENOIOCTLCMD;
  460. }