pcm_compat.c 15 KB

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