pcm_compat.c 15 KB

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