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