pcm_compat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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(struct snd_pcm_substream *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(struct snd_pcm_substream *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(struct snd_pcm_substream *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 snd_pcm_hw_params32 {
  68. u32 flags;
  69. struct snd_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK - SNDRV_PCM_HW_PARAM_FIRST_MASK + 1]; /* this must be identical */
  70. struct snd_mask mres[5]; /* reserved masks */
  71. struct snd_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
  72. struct snd_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 snd_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. /* recalcuate the boundary within 32bit */
  96. static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
  97. {
  98. snd_pcm_uframes_t boundary;
  99. if (! runtime->buffer_size)
  100. return 0;
  101. boundary = runtime->buffer_size;
  102. while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
  103. boundary *= 2;
  104. return boundary;
  105. }
  106. static int snd_pcm_ioctl_sw_params_compat(struct snd_pcm_substream *substream,
  107. struct snd_pcm_sw_params32 __user *src)
  108. {
  109. struct snd_pcm_sw_params params;
  110. snd_pcm_uframes_t boundary;
  111. int err;
  112. memset(&params, 0, sizeof(params));
  113. if (get_user(params.tstamp_mode, &src->tstamp_mode) ||
  114. get_user(params.period_step, &src->period_step) ||
  115. get_user(params.sleep_min, &src->sleep_min) ||
  116. get_user(params.avail_min, &src->avail_min) ||
  117. get_user(params.xfer_align, &src->xfer_align) ||
  118. get_user(params.start_threshold, &src->start_threshold) ||
  119. get_user(params.stop_threshold, &src->stop_threshold) ||
  120. get_user(params.silence_threshold, &src->silence_threshold) ||
  121. get_user(params.silence_size, &src->silence_size))
  122. return -EFAULT;
  123. /*
  124. * Check silent_size parameter. Since we have 64bit boundary,
  125. * silence_size must be compared with the 32bit boundary.
  126. */
  127. boundary = recalculate_boundary(substream->runtime);
  128. if (boundary && params.silence_size >= boundary)
  129. params.silence_size = substream->runtime->boundary;
  130. err = snd_pcm_sw_params(substream, &params);
  131. if (err < 0)
  132. return err;
  133. if (boundary && put_user(boundary, &src->boundary))
  134. return -EFAULT;
  135. return err;
  136. }
  137. struct snd_pcm_channel_info32 {
  138. u32 channel;
  139. u32 offset;
  140. u32 first;
  141. u32 step;
  142. };
  143. static int snd_pcm_ioctl_channel_info_compat(struct snd_pcm_substream *substream,
  144. struct snd_pcm_channel_info32 __user *src)
  145. {
  146. struct snd_pcm_channel_info info;
  147. int err;
  148. if (get_user(info.channel, &src->channel) ||
  149. get_user(info.offset, &src->offset) ||
  150. get_user(info.first, &src->first) ||
  151. get_user(info.step, &src->step))
  152. return -EFAULT;
  153. err = snd_pcm_channel_info(substream, &info);
  154. if (err < 0)
  155. return err;
  156. if (put_user(info.channel, &src->channel) ||
  157. put_user(info.offset, &src->offset) ||
  158. put_user(info.first, &src->first) ||
  159. put_user(info.step, &src->step))
  160. return -EFAULT;
  161. return err;
  162. }
  163. struct snd_pcm_status32 {
  164. s32 state;
  165. struct compat_timespec trigger_tstamp;
  166. struct compat_timespec tstamp;
  167. u32 appl_ptr;
  168. u32 hw_ptr;
  169. s32 delay;
  170. u32 avail;
  171. u32 avail_max;
  172. u32 overrange;
  173. s32 suspended_state;
  174. unsigned char reserved[60];
  175. } __attribute__((packed));
  176. static int snd_pcm_status_user_compat(struct snd_pcm_substream *substream,
  177. struct snd_pcm_status32 __user *src)
  178. {
  179. struct snd_pcm_status status;
  180. int err;
  181. err = snd_pcm_status(substream, &status);
  182. if (err < 0)
  183. return err;
  184. if (put_user(status.state, &src->state) ||
  185. put_user(status.trigger_tstamp.tv_sec, &src->trigger_tstamp.tv_sec) ||
  186. put_user(status.trigger_tstamp.tv_nsec, &src->trigger_tstamp.tv_nsec) ||
  187. put_user(status.tstamp.tv_sec, &src->tstamp.tv_sec) ||
  188. put_user(status.tstamp.tv_nsec, &src->tstamp.tv_nsec) ||
  189. put_user(status.appl_ptr, &src->appl_ptr) ||
  190. put_user(status.hw_ptr, &src->hw_ptr) ||
  191. put_user(status.delay, &src->delay) ||
  192. put_user(status.avail, &src->avail) ||
  193. put_user(status.avail_max, &src->avail_max) ||
  194. put_user(status.overrange, &src->overrange) ||
  195. put_user(status.suspended_state, &src->suspended_state))
  196. return -EFAULT;
  197. return err;
  198. }
  199. /* both for HW_PARAMS and HW_REFINE */
  200. static int snd_pcm_ioctl_hw_params_compat(struct snd_pcm_substream *substream,
  201. int refine,
  202. struct snd_pcm_hw_params32 __user *data32)
  203. {
  204. struct snd_pcm_hw_params *data;
  205. struct snd_pcm_runtime *runtime;
  206. int err;
  207. if (! (runtime = substream->runtime))
  208. return -ENOTTY;
  209. data = kmalloc(sizeof(*data), GFP_KERNEL);
  210. if (data == NULL)
  211. return -ENOMEM;
  212. /* only fifo_size is different, so just copy all */
  213. if (copy_from_user(data, data32, sizeof(*data32))) {
  214. err = -EFAULT;
  215. goto error;
  216. }
  217. if (refine)
  218. err = snd_pcm_hw_refine(substream, data);
  219. else
  220. err = snd_pcm_hw_params(substream, data);
  221. if (err < 0)
  222. goto error;
  223. if (copy_to_user(data32, data, sizeof(*data32)) ||
  224. put_user(data->fifo_size, &data32->fifo_size)) {
  225. err = -EFAULT;
  226. goto error;
  227. }
  228. if (! refine) {
  229. unsigned int new_boundary = recalculate_boundary(runtime);
  230. if (new_boundary)
  231. runtime->boundary = new_boundary;
  232. }
  233. error:
  234. kfree(data);
  235. return err;
  236. }
  237. /*
  238. */
  239. struct snd_xferi32 {
  240. s32 result;
  241. u32 buf;
  242. u32 frames;
  243. };
  244. static int snd_pcm_ioctl_xferi_compat(struct snd_pcm_substream *substream,
  245. int dir, struct snd_xferi32 __user *data32)
  246. {
  247. compat_caddr_t buf;
  248. u32 frames;
  249. int err;
  250. if (! substream->runtime)
  251. return -ENOTTY;
  252. if (substream->stream != dir)
  253. return -EINVAL;
  254. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
  255. return -EBADFD;
  256. if (get_user(buf, &data32->buf) ||
  257. get_user(frames, &data32->frames))
  258. return -EFAULT;
  259. if (dir == SNDRV_PCM_STREAM_PLAYBACK)
  260. err = snd_pcm_lib_write(substream, compat_ptr(buf), frames);
  261. else
  262. err = snd_pcm_lib_read(substream, compat_ptr(buf), frames);
  263. if (err < 0)
  264. return err;
  265. /* copy the result */
  266. if (put_user(err, &data32->result))
  267. return -EFAULT;
  268. return 0;
  269. }
  270. /* snd_xfern needs remapping of bufs */
  271. struct snd_xfern32 {
  272. s32 result;
  273. u32 bufs; /* this is void **; */
  274. u32 frames;
  275. };
  276. /*
  277. * xfern ioctl nees to copy (up to) 128 pointers on stack.
  278. * although we may pass the copied pointers through f_op->ioctl, but the ioctl
  279. * handler there expands again the same 128 pointers on stack, so it is better
  280. * to handle the function (calling pcm_readv/writev) directly in this handler.
  281. */
  282. static int snd_pcm_ioctl_xfern_compat(struct snd_pcm_substream *substream,
  283. int dir, struct snd_xfern32 __user *data32)
  284. {
  285. compat_caddr_t buf;
  286. compat_caddr_t __user *bufptr;
  287. u32 frames;
  288. void __user **bufs;
  289. int err, ch, i;
  290. if (! substream->runtime)
  291. return -ENOTTY;
  292. if (substream->stream != dir)
  293. return -EINVAL;
  294. if ((ch = substream->runtime->channels) > 128)
  295. return -EINVAL;
  296. if (get_user(buf, &data32->bufs) ||
  297. get_user(frames, &data32->frames))
  298. return -EFAULT;
  299. bufptr = compat_ptr(buf);
  300. bufs = kmalloc(sizeof(void __user *) * ch, GFP_KERNEL);
  301. if (bufs == NULL)
  302. return -ENOMEM;
  303. for (i = 0; i < ch; i++) {
  304. u32 ptr;
  305. if (get_user(ptr, bufptr)) {
  306. kfree(bufs);
  307. return -EFAULT;
  308. }
  309. bufs[ch] = compat_ptr(ptr);
  310. bufptr++;
  311. }
  312. if (dir == SNDRV_PCM_STREAM_PLAYBACK)
  313. err = snd_pcm_lib_writev(substream, bufs, frames);
  314. else
  315. err = snd_pcm_lib_readv(substream, bufs, frames);
  316. if (err >= 0) {
  317. if (put_user(err, &data32->result))
  318. err = -EFAULT;
  319. }
  320. kfree(bufs);
  321. return err;
  322. }
  323. struct snd_pcm_mmap_status32 {
  324. s32 state;
  325. s32 pad1;
  326. u32 hw_ptr;
  327. struct compat_timespec tstamp;
  328. s32 suspended_state;
  329. } __attribute__((packed));
  330. struct snd_pcm_mmap_control32 {
  331. u32 appl_ptr;
  332. u32 avail_min;
  333. };
  334. struct snd_pcm_sync_ptr32 {
  335. u32 flags;
  336. union {
  337. struct snd_pcm_mmap_status32 status;
  338. unsigned char reserved[64];
  339. } s;
  340. union {
  341. struct snd_pcm_mmap_control32 control;
  342. unsigned char reserved[64];
  343. } c;
  344. } __attribute__((packed));
  345. static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
  346. struct snd_pcm_sync_ptr32 __user *src)
  347. {
  348. struct snd_pcm_runtime *runtime = substream->runtime;
  349. volatile struct snd_pcm_mmap_status *status;
  350. volatile struct snd_pcm_mmap_control *control;
  351. u32 sflags;
  352. struct snd_pcm_mmap_control scontrol;
  353. struct snd_pcm_mmap_status sstatus;
  354. snd_pcm_uframes_t boundary;
  355. int err;
  356. snd_assert(runtime, 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_HWSYNC:
  435. case SNDRV_PCM_IOCTL_PREPARE:
  436. case SNDRV_PCM_IOCTL_RESET:
  437. case SNDRV_PCM_IOCTL_START:
  438. case SNDRV_PCM_IOCTL_DROP:
  439. case SNDRV_PCM_IOCTL_DRAIN:
  440. case SNDRV_PCM_IOCTL_PAUSE:
  441. case SNDRV_PCM_IOCTL_HW_FREE:
  442. case SNDRV_PCM_IOCTL_RESUME:
  443. case SNDRV_PCM_IOCTL_XRUN:
  444. case SNDRV_PCM_IOCTL_LINK:
  445. case SNDRV_PCM_IOCTL_UNLINK:
  446. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  447. return snd_pcm_playback_ioctl1(file, substream, cmd, argp);
  448. else
  449. return snd_pcm_capture_ioctl1(file, substream, cmd, argp);
  450. case SNDRV_PCM_IOCTL_HW_REFINE32:
  451. return snd_pcm_ioctl_hw_params_compat(substream, 1, argp);
  452. case SNDRV_PCM_IOCTL_HW_PARAMS32:
  453. return snd_pcm_ioctl_hw_params_compat(substream, 0, argp);
  454. case SNDRV_PCM_IOCTL_SW_PARAMS32:
  455. return snd_pcm_ioctl_sw_params_compat(substream, argp);
  456. case SNDRV_PCM_IOCTL_STATUS32:
  457. return snd_pcm_status_user_compat(substream, argp);
  458. case SNDRV_PCM_IOCTL_SYNC_PTR32:
  459. return snd_pcm_ioctl_sync_ptr_compat(substream, argp);
  460. case SNDRV_PCM_IOCTL_CHANNEL_INFO32:
  461. return snd_pcm_ioctl_channel_info_compat(substream, argp);
  462. case SNDRV_PCM_IOCTL_WRITEI_FRAMES32:
  463. return snd_pcm_ioctl_xferi_compat(substream, SNDRV_PCM_STREAM_PLAYBACK, argp);
  464. case SNDRV_PCM_IOCTL_READI_FRAMES32:
  465. return snd_pcm_ioctl_xferi_compat(substream, SNDRV_PCM_STREAM_CAPTURE, argp);
  466. case SNDRV_PCM_IOCTL_WRITEN_FRAMES32:
  467. return snd_pcm_ioctl_xfern_compat(substream, SNDRV_PCM_STREAM_PLAYBACK, argp);
  468. case SNDRV_PCM_IOCTL_READN_FRAMES32:
  469. return snd_pcm_ioctl_xfern_compat(substream, SNDRV_PCM_STREAM_CAPTURE, argp);
  470. case SNDRV_PCM_IOCTL_DELAY32:
  471. return snd_pcm_ioctl_delay_compat(substream, argp);
  472. case SNDRV_PCM_IOCTL_REWIND32:
  473. return snd_pcm_ioctl_rewind_compat(substream, argp);
  474. case SNDRV_PCM_IOCTL_FORWARD32:
  475. return snd_pcm_ioctl_forward_compat(substream, argp);
  476. }
  477. return -ENOIOCTLCMD;
  478. }