pcm_compat.c 15 KB

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