1 /* 2 * Digital Audio (PCM) abstract layer 3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz> 4 * 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 */ 21 22 #include <sound/driver.h> 23 #include <linux/mm.h> 24 #include <linux/file.h> 25 #include <linux/slab.h> 26 #include <linux/time.h> 27 #include <linux/latency.h> 28 #include <linux/uio.h> 29 #include <sound/core.h> 30 #include <sound/control.h> 31 #include <sound/info.h> 32 #include <sound/pcm.h> 33 #include <sound/pcm_params.h> 34 #include <sound/timer.h> 35 #include <sound/minors.h> 36 #include <asm/io.h> 37 38 /* 39 * Compatibility 40 */ 41 42 struct snd_pcm_hw_params_old { 43 unsigned int flags; 44 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT - 45 SNDRV_PCM_HW_PARAM_ACCESS + 1]; 46 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME - 47 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1]; 48 unsigned int rmask; 49 unsigned int cmask; 50 unsigned int info; 51 unsigned int msbits; 52 unsigned int rate_num; 53 unsigned int rate_den; 54 snd_pcm_uframes_t fifo_size; 55 unsigned char reserved[64]; 56 }; 57 58 #ifdef CONFIG_SND_SUPPORT_OLD_API 59 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old) 60 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old) 61 62 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, 63 struct snd_pcm_hw_params_old __user * _oparams); 64 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, 65 struct snd_pcm_hw_params_old __user * _oparams); 66 #endif 67 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream); 68 69 /* 70 * 71 */ 72 73 DEFINE_RWLOCK(snd_pcm_link_rwlock); 74 EXPORT_SYMBOL(snd_pcm_link_rwlock); 75 76 static DECLARE_RWSEM(snd_pcm_link_rwsem); 77 78 static inline mm_segment_t snd_enter_user(void) 79 { 80 mm_segment_t fs = get_fs(); 81 set_fs(get_ds()); 82 return fs; 83 } 84 85 static inline void snd_leave_user(mm_segment_t fs) 86 { 87 set_fs(fs); 88 } 89 90 91 92 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info) 93 { 94 struct snd_pcm_runtime *runtime; 95 struct snd_pcm *pcm = substream->pcm; 96 struct snd_pcm_str *pstr = substream->pstr; 97 98 snd_assert(substream != NULL, return -ENXIO); 99 memset(info, 0, sizeof(*info)); 100 info->card = pcm->card->number; 101 info->device = pcm->device; 102 info->stream = substream->stream; 103 info->subdevice = substream->number; 104 strlcpy(info->id, pcm->id, sizeof(info->id)); 105 strlcpy(info->name, pcm->name, sizeof(info->name)); 106 info->dev_class = pcm->dev_class; 107 info->dev_subclass = pcm->dev_subclass; 108 info->subdevices_count = pstr->substream_count; 109 info->subdevices_avail = pstr->substream_count - pstr->substream_opened; 110 strlcpy(info->subname, substream->name, sizeof(info->subname)); 111 runtime = substream->runtime; 112 /* AB: FIXME!!! This is definitely nonsense */ 113 if (runtime) { 114 info->sync = runtime->sync; 115 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info); 116 } 117 return 0; 118 } 119 120 int snd_pcm_info_user(struct snd_pcm_substream *substream, 121 struct snd_pcm_info __user * _info) 122 { 123 struct snd_pcm_info *info; 124 int err; 125 126 info = kmalloc(sizeof(*info), GFP_KERNEL); 127 if (! info) 128 return -ENOMEM; 129 err = snd_pcm_info(substream, info); 130 if (err >= 0) { 131 if (copy_to_user(_info, info, sizeof(*info))) 132 err = -EFAULT; 133 } 134 kfree(info); 135 return err; 136 } 137 138 #undef RULES_DEBUG 139 140 #ifdef RULES_DEBUG 141 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v 142 char *snd_pcm_hw_param_names[] = { 143 HW_PARAM(ACCESS), 144 HW_PARAM(FORMAT), 145 HW_PARAM(SUBFORMAT), 146 HW_PARAM(SAMPLE_BITS), 147 HW_PARAM(FRAME_BITS), 148 HW_PARAM(CHANNELS), 149 HW_PARAM(RATE), 150 HW_PARAM(PERIOD_TIME), 151 HW_PARAM(PERIOD_SIZE), 152 HW_PARAM(PERIOD_BYTES), 153 HW_PARAM(PERIODS), 154 HW_PARAM(BUFFER_TIME), 155 HW_PARAM(BUFFER_SIZE), 156 HW_PARAM(BUFFER_BYTES), 157 HW_PARAM(TICK_TIME), 158 }; 159 #endif 160 161 int snd_pcm_hw_refine(struct snd_pcm_substream *substream, 162 struct snd_pcm_hw_params *params) 163 { 164 unsigned int k; 165 struct snd_pcm_hardware *hw; 166 struct snd_interval *i = NULL; 167 struct snd_mask *m = NULL; 168 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints; 169 unsigned int rstamps[constrs->rules_num]; 170 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1]; 171 unsigned int stamp = 2; 172 int changed, again; 173 174 params->info = 0; 175 params->fifo_size = 0; 176 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS)) 177 params->msbits = 0; 178 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) { 179 params->rate_num = 0; 180 params->rate_den = 0; 181 } 182 183 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { 184 m = hw_param_mask(params, k); 185 if (snd_mask_empty(m)) 186 return -EINVAL; 187 if (!(params->rmask & (1 << k))) 188 continue; 189 #ifdef RULES_DEBUG 190 printk("%s = ", snd_pcm_hw_param_names[k]); 191 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]); 192 #endif 193 changed = snd_mask_refine(m, constrs_mask(constrs, k)); 194 #ifdef RULES_DEBUG 195 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]); 196 #endif 197 if (changed) 198 params->cmask |= 1 << k; 199 if (changed < 0) 200 return changed; 201 } 202 203 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { 204 i = hw_param_interval(params, k); 205 if (snd_interval_empty(i)) 206 return -EINVAL; 207 if (!(params->rmask & (1 << k))) 208 continue; 209 #ifdef RULES_DEBUG 210 printk("%s = ", snd_pcm_hw_param_names[k]); 211 if (i->empty) 212 printk("empty"); 213 else 214 printk("%c%u %u%c", 215 i->openmin ? '(' : '[', i->min, 216 i->max, i->openmax ? ')' : ']'); 217 printk(" -> "); 218 #endif 219 changed = snd_interval_refine(i, constrs_interval(constrs, k)); 220 #ifdef RULES_DEBUG 221 if (i->empty) 222 printk("empty\n"); 223 else 224 printk("%c%u %u%c\n", 225 i->openmin ? '(' : '[', i->min, 226 i->max, i->openmax ? ')' : ']'); 227 #endif 228 if (changed) 229 params->cmask |= 1 << k; 230 if (changed < 0) 231 return changed; 232 } 233 234 for (k = 0; k < constrs->rules_num; k++) 235 rstamps[k] = 0; 236 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) 237 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0; 238 do { 239 again = 0; 240 for (k = 0; k < constrs->rules_num; k++) { 241 struct snd_pcm_hw_rule *r = &constrs->rules[k]; 242 unsigned int d; 243 int doit = 0; 244 if (r->cond && !(r->cond & params->flags)) 245 continue; 246 for (d = 0; r->deps[d] >= 0; d++) { 247 if (vstamps[r->deps[d]] > rstamps[k]) { 248 doit = 1; 249 break; 250 } 251 } 252 if (!doit) 253 continue; 254 #ifdef RULES_DEBUG 255 printk("Rule %d [%p]: ", k, r->func); 256 if (r->var >= 0) { 257 printk("%s = ", snd_pcm_hw_param_names[r->var]); 258 if (hw_is_mask(r->var)) { 259 m = hw_param_mask(params, r->var); 260 printk("%x", *m->bits); 261 } else { 262 i = hw_param_interval(params, r->var); 263 if (i->empty) 264 printk("empty"); 265 else 266 printk("%c%u %u%c", 267 i->openmin ? '(' : '[', i->min, 268 i->max, i->openmax ? ')' : ']'); 269 } 270 } 271 #endif 272 changed = r->func(params, r); 273 #ifdef RULES_DEBUG 274 if (r->var >= 0) { 275 printk(" -> "); 276 if (hw_is_mask(r->var)) 277 printk("%x", *m->bits); 278 else { 279 if (i->empty) 280 printk("empty"); 281 else 282 printk("%c%u %u%c", 283 i->openmin ? '(' : '[', i->min, 284 i->max, i->openmax ? ')' : ']'); 285 } 286 } 287 printk("\n"); 288 #endif 289 rstamps[k] = stamp; 290 if (changed && r->var >= 0) { 291 params->cmask |= (1 << r->var); 292 vstamps[r->var] = stamp; 293 again = 1; 294 } 295 if (changed < 0) 296 return changed; 297 stamp++; 298 } 299 } while (again); 300 if (!params->msbits) { 301 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); 302 if (snd_interval_single(i)) 303 params->msbits = snd_interval_value(i); 304 } 305 306 if (!params->rate_den) { 307 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 308 if (snd_interval_single(i)) { 309 params->rate_num = snd_interval_value(i); 310 params->rate_den = 1; 311 } 312 } 313 314 hw = &substream->runtime->hw; 315 if (!params->info) 316 params->info = hw->info; 317 if (!params->fifo_size) 318 params->fifo_size = hw->fifo_size; 319 params->rmask = 0; 320 return 0; 321 } 322 323 EXPORT_SYMBOL(snd_pcm_hw_refine); 324 325 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream, 326 struct snd_pcm_hw_params __user * _params) 327 { 328 struct snd_pcm_hw_params *params; 329 int err; 330 331 params = kmalloc(sizeof(*params), GFP_KERNEL); 332 if (!params) { 333 err = -ENOMEM; 334 goto out; 335 } 336 if (copy_from_user(params, _params, sizeof(*params))) { 337 err = -EFAULT; 338 goto out; 339 } 340 err = snd_pcm_hw_refine(substream, params); 341 if (copy_to_user(_params, params, sizeof(*params))) { 342 if (!err) 343 err = -EFAULT; 344 } 345 out: 346 kfree(params); 347 return err; 348 } 349 350 static int period_to_usecs(struct snd_pcm_runtime *runtime) 351 { 352 int usecs; 353 354 if (! runtime->rate) 355 return -1; /* invalid */ 356 357 /* take 75% of period time as the deadline */ 358 usecs = (750000 / runtime->rate) * runtime->period_size; 359 usecs += ((750000 % runtime->rate) * runtime->period_size) / 360 runtime->rate; 361 362 return usecs; 363 } 364 365 static int snd_pcm_hw_params(struct snd_pcm_substream *substream, 366 struct snd_pcm_hw_params *params) 367 { 368 struct snd_pcm_runtime *runtime; 369 int err, usecs; 370 unsigned int bits; 371 snd_pcm_uframes_t frames; 372 373 snd_assert(substream != NULL, return -ENXIO); 374 runtime = substream->runtime; 375 snd_assert(runtime != NULL, return -ENXIO); 376 snd_pcm_stream_lock_irq(substream); 377 switch (runtime->status->state) { 378 case SNDRV_PCM_STATE_OPEN: 379 case SNDRV_PCM_STATE_SETUP: 380 case SNDRV_PCM_STATE_PREPARED: 381 break; 382 default: 383 snd_pcm_stream_unlock_irq(substream); 384 return -EBADFD; 385 } 386 snd_pcm_stream_unlock_irq(substream); 387 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE) 388 if (!substream->oss.oss) 389 #endif 390 if (atomic_read(&substream->mmap_count)) 391 return -EBADFD; 392 393 params->rmask = ~0U; 394 err = snd_pcm_hw_refine(substream, params); 395 if (err < 0) 396 goto _error; 397 398 err = snd_pcm_hw_params_choose(substream, params); 399 if (err < 0) 400 goto _error; 401 402 if (substream->ops->hw_params != NULL) { 403 err = substream->ops->hw_params(substream, params); 404 if (err < 0) 405 goto _error; 406 } 407 408 runtime->access = params_access(params); 409 runtime->format = params_format(params); 410 runtime->subformat = params_subformat(params); 411 runtime->channels = params_channels(params); 412 runtime->rate = params_rate(params); 413 runtime->period_size = params_period_size(params); 414 runtime->periods = params_periods(params); 415 runtime->buffer_size = params_buffer_size(params); 416 runtime->tick_time = params_tick_time(params); 417 runtime->info = params->info; 418 runtime->rate_num = params->rate_num; 419 runtime->rate_den = params->rate_den; 420 421 bits = snd_pcm_format_physical_width(runtime->format); 422 runtime->sample_bits = bits; 423 bits *= runtime->channels; 424 runtime->frame_bits = bits; 425 frames = 1; 426 while (bits % 8 != 0) { 427 bits *= 2; 428 frames *= 2; 429 } 430 runtime->byte_align = bits / 8; 431 runtime->min_align = frames; 432 433 /* Default sw params */ 434 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; 435 runtime->period_step = 1; 436 runtime->sleep_min = 0; 437 runtime->control->avail_min = runtime->period_size; 438 runtime->xfer_align = runtime->period_size; 439 runtime->start_threshold = 1; 440 runtime->stop_threshold = runtime->buffer_size; 441 runtime->silence_threshold = 0; 442 runtime->silence_size = 0; 443 runtime->boundary = runtime->buffer_size; 444 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size) 445 runtime->boundary *= 2; 446 447 snd_pcm_timer_resolution_change(substream); 448 runtime->status->state = SNDRV_PCM_STATE_SETUP; 449 450 remove_acceptable_latency(substream->latency_id); 451 if ((usecs = period_to_usecs(runtime)) >= 0) 452 set_acceptable_latency(substream->latency_id, usecs); 453 return 0; 454 _error: 455 /* hardware might be unuseable from this time, 456 so we force application to retry to set 457 the correct hardware parameter settings */ 458 runtime->status->state = SNDRV_PCM_STATE_OPEN; 459 if (substream->ops->hw_free != NULL) 460 substream->ops->hw_free(substream); 461 return err; 462 } 463 464 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream, 465 struct snd_pcm_hw_params __user * _params) 466 { 467 struct snd_pcm_hw_params *params; 468 int err; 469 470 params = kmalloc(sizeof(*params), GFP_KERNEL); 471 if (!params) { 472 err = -ENOMEM; 473 goto out; 474 } 475 if (copy_from_user(params, _params, sizeof(*params))) { 476 err = -EFAULT; 477 goto out; 478 } 479 err = snd_pcm_hw_params(substream, params); 480 if (copy_to_user(_params, params, sizeof(*params))) { 481 if (!err) 482 err = -EFAULT; 483 } 484 out: 485 kfree(params); 486 return err; 487 } 488 489 static int snd_pcm_hw_free(struct snd_pcm_substream *substream) 490 { 491 struct snd_pcm_runtime *runtime; 492 int result = 0; 493 494 snd_assert(substream != NULL, return -ENXIO); 495 runtime = substream->runtime; 496 snd_assert(runtime != NULL, return -ENXIO); 497 snd_pcm_stream_lock_irq(substream); 498 switch (runtime->status->state) { 499 case SNDRV_PCM_STATE_SETUP: 500 case SNDRV_PCM_STATE_PREPARED: 501 break; 502 default: 503 snd_pcm_stream_unlock_irq(substream); 504 return -EBADFD; 505 } 506 snd_pcm_stream_unlock_irq(substream); 507 if (atomic_read(&substream->mmap_count)) 508 return -EBADFD; 509 if (substream->ops->hw_free) 510 result = substream->ops->hw_free(substream); 511 runtime->status->state = SNDRV_PCM_STATE_OPEN; 512 remove_acceptable_latency(substream->latency_id); 513 return result; 514 } 515 516 static int snd_pcm_sw_params(struct snd_pcm_substream *substream, 517 struct snd_pcm_sw_params *params) 518 { 519 struct snd_pcm_runtime *runtime; 520 521 snd_assert(substream != NULL, return -ENXIO); 522 runtime = substream->runtime; 523 snd_assert(runtime != NULL, return -ENXIO); 524 snd_pcm_stream_lock_irq(substream); 525 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { 526 snd_pcm_stream_unlock_irq(substream); 527 return -EBADFD; 528 } 529 snd_pcm_stream_unlock_irq(substream); 530 531 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST) 532 return -EINVAL; 533 if (params->avail_min == 0) 534 return -EINVAL; 535 if (params->xfer_align == 0 || 536 params->xfer_align % runtime->min_align != 0) 537 return -EINVAL; 538 if (params->silence_size >= runtime->boundary) { 539 if (params->silence_threshold != 0) 540 return -EINVAL; 541 } else { 542 if (params->silence_size > params->silence_threshold) 543 return -EINVAL; 544 if (params->silence_threshold > runtime->buffer_size) 545 return -EINVAL; 546 } 547 snd_pcm_stream_lock_irq(substream); 548 runtime->tstamp_mode = params->tstamp_mode; 549 runtime->sleep_min = params->sleep_min; 550 runtime->period_step = params->period_step; 551 runtime->control->avail_min = params->avail_min; 552 runtime->start_threshold = params->start_threshold; 553 runtime->stop_threshold = params->stop_threshold; 554 runtime->silence_threshold = params->silence_threshold; 555 runtime->silence_size = params->silence_size; 556 runtime->xfer_align = params->xfer_align; 557 params->boundary = runtime->boundary; 558 if (snd_pcm_running(substream)) { 559 if (runtime->sleep_min) 560 snd_pcm_tick_prepare(substream); 561 else 562 snd_pcm_tick_set(substream, 0); 563 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 564 runtime->silence_size > 0) 565 snd_pcm_playback_silence(substream, ULONG_MAX); 566 wake_up(&runtime->sleep); 567 } 568 snd_pcm_stream_unlock_irq(substream); 569 return 0; 570 } 571 572 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream, 573 struct snd_pcm_sw_params __user * _params) 574 { 575 struct snd_pcm_sw_params params; 576 int err; 577 if (copy_from_user(¶ms, _params, sizeof(params))) 578 return -EFAULT; 579 err = snd_pcm_sw_params(substream, ¶ms); 580 if (copy_to_user(_params, ¶ms, sizeof(params))) 581 return -EFAULT; 582 return err; 583 } 584 585 int snd_pcm_status(struct snd_pcm_substream *substream, 586 struct snd_pcm_status *status) 587 { 588 struct snd_pcm_runtime *runtime = substream->runtime; 589 590 snd_pcm_stream_lock_irq(substream); 591 status->state = runtime->status->state; 592 status->suspended_state = runtime->status->suspended_state; 593 if (status->state == SNDRV_PCM_STATE_OPEN) 594 goto _end; 595 status->trigger_tstamp = runtime->trigger_tstamp; 596 if (snd_pcm_running(substream)) { 597 snd_pcm_update_hw_ptr(substream); 598 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP) 599 status->tstamp = runtime->status->tstamp; 600 else 601 getnstimeofday(&status->tstamp); 602 } else 603 getnstimeofday(&status->tstamp); 604 status->appl_ptr = runtime->control->appl_ptr; 605 status->hw_ptr = runtime->status->hw_ptr; 606 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 607 status->avail = snd_pcm_playback_avail(runtime); 608 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING || 609 runtime->status->state == SNDRV_PCM_STATE_DRAINING) 610 status->delay = runtime->buffer_size - status->avail; 611 else 612 status->delay = 0; 613 } else { 614 status->avail = snd_pcm_capture_avail(runtime); 615 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) 616 status->delay = status->avail; 617 else 618 status->delay = 0; 619 } 620 status->avail_max = runtime->avail_max; 621 status->overrange = runtime->overrange; 622 runtime->avail_max = 0; 623 runtime->overrange = 0; 624 _end: 625 snd_pcm_stream_unlock_irq(substream); 626 return 0; 627 } 628 629 static int snd_pcm_status_user(struct snd_pcm_substream *substream, 630 struct snd_pcm_status __user * _status) 631 { 632 struct snd_pcm_status status; 633 struct snd_pcm_runtime *runtime; 634 int res; 635 636 snd_assert(substream != NULL, return -ENXIO); 637 runtime = substream->runtime; 638 memset(&status, 0, sizeof(status)); 639 res = snd_pcm_status(substream, &status); 640 if (res < 0) 641 return res; 642 if (copy_to_user(_status, &status, sizeof(status))) 643 return -EFAULT; 644 return 0; 645 } 646 647 static int snd_pcm_channel_info(struct snd_pcm_substream *substream, 648 struct snd_pcm_channel_info * info) 649 { 650 struct snd_pcm_runtime *runtime; 651 unsigned int channel; 652 653 snd_assert(substream != NULL, return -ENXIO); 654 channel = info->channel; 655 runtime = substream->runtime; 656 snd_pcm_stream_lock_irq(substream); 657 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { 658 snd_pcm_stream_unlock_irq(substream); 659 return -EBADFD; 660 } 661 snd_pcm_stream_unlock_irq(substream); 662 if (channel >= runtime->channels) 663 return -EINVAL; 664 memset(info, 0, sizeof(*info)); 665 info->channel = channel; 666 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info); 667 } 668 669 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream, 670 struct snd_pcm_channel_info __user * _info) 671 { 672 struct snd_pcm_channel_info info; 673 int res; 674 675 if (copy_from_user(&info, _info, sizeof(info))) 676 return -EFAULT; 677 res = snd_pcm_channel_info(substream, &info); 678 if (res < 0) 679 return res; 680 if (copy_to_user(_info, &info, sizeof(info))) 681 return -EFAULT; 682 return 0; 683 } 684 685 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream) 686 { 687 struct snd_pcm_runtime *runtime = substream->runtime; 688 if (runtime->trigger_master == NULL) 689 return; 690 if (runtime->trigger_master == substream) { 691 getnstimeofday(&runtime->trigger_tstamp); 692 } else { 693 snd_pcm_trigger_tstamp(runtime->trigger_master); 694 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp; 695 } 696 runtime->trigger_master = NULL; 697 } 698 699 struct action_ops { 700 int (*pre_action)(struct snd_pcm_substream *substream, int state); 701 int (*do_action)(struct snd_pcm_substream *substream, int state); 702 void (*undo_action)(struct snd_pcm_substream *substream, int state); 703 void (*post_action)(struct snd_pcm_substream *substream, int state); 704 }; 705 706 /* 707 * this functions is core for handling of linked stream 708 * Note: the stream state might be changed also on failure 709 * Note2: call with calling stream lock + link lock 710 */ 711 static int snd_pcm_action_group(struct action_ops *ops, 712 struct snd_pcm_substream *substream, 713 int state, int do_lock) 714 { 715 struct snd_pcm_substream *s = NULL; 716 struct snd_pcm_substream *s1; 717 int res = 0; 718 719 snd_pcm_group_for_each_entry(s, substream) { 720 if (do_lock && s != substream) 721 spin_lock(&s->self_group.lock); 722 res = ops->pre_action(s, state); 723 if (res < 0) 724 goto _unlock; 725 } 726 snd_pcm_group_for_each_entry(s, substream) { 727 res = ops->do_action(s, state); 728 if (res < 0) { 729 if (ops->undo_action) { 730 snd_pcm_group_for_each_entry(s1, substream) { 731 if (s1 == s) /* failed stream */ 732 break; 733 ops->undo_action(s1, state); 734 } 735 } 736 s = NULL; /* unlock all */ 737 goto _unlock; 738 } 739 } 740 snd_pcm_group_for_each_entry(s, substream) { 741 ops->post_action(s, state); 742 } 743 _unlock: 744 if (do_lock) { 745 /* unlock streams */ 746 snd_pcm_group_for_each_entry(s1, substream) { 747 if (s1 != substream) 748 spin_unlock(&s1->self_group.lock); 749 if (s1 == s) /* end */ 750 break; 751 } 752 } 753 return res; 754 } 755 756 /* 757 * Note: call with stream lock 758 */ 759 static int snd_pcm_action_single(struct action_ops *ops, 760 struct snd_pcm_substream *substream, 761 int state) 762 { 763 int res; 764 765 res = ops->pre_action(substream, state); 766 if (res < 0) 767 return res; 768 res = ops->do_action(substream, state); 769 if (res == 0) 770 ops->post_action(substream, state); 771 else if (ops->undo_action) 772 ops->undo_action(substream, state); 773 return res; 774 } 775 776 /* 777 * Note: call with stream lock 778 */ 779 static int snd_pcm_action(struct action_ops *ops, 780 struct snd_pcm_substream *substream, 781 int state) 782 { 783 int res; 784 785 if (snd_pcm_stream_linked(substream)) { 786 if (!spin_trylock(&substream->group->lock)) { 787 spin_unlock(&substream->self_group.lock); 788 spin_lock(&substream->group->lock); 789 spin_lock(&substream->self_group.lock); 790 } 791 res = snd_pcm_action_group(ops, substream, state, 1); 792 spin_unlock(&substream->group->lock); 793 } else { 794 res = snd_pcm_action_single(ops, substream, state); 795 } 796 return res; 797 } 798 799 /* 800 * Note: don't use any locks before 801 */ 802 static int snd_pcm_action_lock_irq(struct action_ops *ops, 803 struct snd_pcm_substream *substream, 804 int state) 805 { 806 int res; 807 808 read_lock_irq(&snd_pcm_link_rwlock); 809 if (snd_pcm_stream_linked(substream)) { 810 spin_lock(&substream->group->lock); 811 spin_lock(&substream->self_group.lock); 812 res = snd_pcm_action_group(ops, substream, state, 1); 813 spin_unlock(&substream->self_group.lock); 814 spin_unlock(&substream->group->lock); 815 } else { 816 spin_lock(&substream->self_group.lock); 817 res = snd_pcm_action_single(ops, substream, state); 818 spin_unlock(&substream->self_group.lock); 819 } 820 read_unlock_irq(&snd_pcm_link_rwlock); 821 return res; 822 } 823 824 /* 825 */ 826 static int snd_pcm_action_nonatomic(struct action_ops *ops, 827 struct snd_pcm_substream *substream, 828 int state) 829 { 830 int res; 831 832 down_read(&snd_pcm_link_rwsem); 833 if (snd_pcm_stream_linked(substream)) 834 res = snd_pcm_action_group(ops, substream, state, 0); 835 else 836 res = snd_pcm_action_single(ops, substream, state); 837 up_read(&snd_pcm_link_rwsem); 838 return res; 839 } 840 841 /* 842 * start callbacks 843 */ 844 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state) 845 { 846 struct snd_pcm_runtime *runtime = substream->runtime; 847 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED) 848 return -EBADFD; 849 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 850 !snd_pcm_playback_data(substream)) 851 return -EPIPE; 852 runtime->trigger_master = substream; 853 return 0; 854 } 855 856 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state) 857 { 858 if (substream->runtime->trigger_master != substream) 859 return 0; 860 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START); 861 } 862 863 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state) 864 { 865 if (substream->runtime->trigger_master == substream) 866 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP); 867 } 868 869 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state) 870 { 871 struct snd_pcm_runtime *runtime = substream->runtime; 872 snd_pcm_trigger_tstamp(substream); 873 runtime->status->state = state; 874 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 875 runtime->silence_size > 0) 876 snd_pcm_playback_silence(substream, ULONG_MAX); 877 if (runtime->sleep_min) 878 snd_pcm_tick_prepare(substream); 879 if (substream->timer) 880 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART, 881 &runtime->trigger_tstamp); 882 } 883 884 static struct action_ops snd_pcm_action_start = { 885 .pre_action = snd_pcm_pre_start, 886 .do_action = snd_pcm_do_start, 887 .undo_action = snd_pcm_undo_start, 888 .post_action = snd_pcm_post_start 889 }; 890 891 /** 892 * snd_pcm_start 893 * @substream: the PCM substream instance 894 * 895 * Start all linked streams. 896 */ 897 int snd_pcm_start(struct snd_pcm_substream *substream) 898 { 899 return snd_pcm_action(&snd_pcm_action_start, substream, 900 SNDRV_PCM_STATE_RUNNING); 901 } 902 903 /* 904 * stop callbacks 905 */ 906 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state) 907 { 908 struct snd_pcm_runtime *runtime = substream->runtime; 909 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 910 return -EBADFD; 911 runtime->trigger_master = substream; 912 return 0; 913 } 914 915 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state) 916 { 917 if (substream->runtime->trigger_master == substream && 918 snd_pcm_running(substream)) 919 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP); 920 return 0; /* unconditonally stop all substreams */ 921 } 922 923 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state) 924 { 925 struct snd_pcm_runtime *runtime = substream->runtime; 926 if (runtime->status->state != state) { 927 snd_pcm_trigger_tstamp(substream); 928 if (substream->timer) 929 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP, 930 &runtime->trigger_tstamp); 931 runtime->status->state = state; 932 snd_pcm_tick_set(substream, 0); 933 } 934 wake_up(&runtime->sleep); 935 } 936 937 static struct action_ops snd_pcm_action_stop = { 938 .pre_action = snd_pcm_pre_stop, 939 .do_action = snd_pcm_do_stop, 940 .post_action = snd_pcm_post_stop 941 }; 942 943 /** 944 * snd_pcm_stop 945 * @substream: the PCM substream instance 946 * @state: PCM state after stopping the stream 947 * 948 * Try to stop all running streams in the substream group. 949 * The state of each stream is changed to the given value after that unconditionally. 950 */ 951 int snd_pcm_stop(struct snd_pcm_substream *substream, int state) 952 { 953 return snd_pcm_action(&snd_pcm_action_stop, substream, state); 954 } 955 956 EXPORT_SYMBOL(snd_pcm_stop); 957 958 /** 959 * snd_pcm_drain_done 960 * @substream: the PCM substream 961 * 962 * Stop the DMA only when the given stream is playback. 963 * The state is changed to SETUP. 964 * Unlike snd_pcm_stop(), this affects only the given stream. 965 */ 966 int snd_pcm_drain_done(struct snd_pcm_substream *substream) 967 { 968 return snd_pcm_action_single(&snd_pcm_action_stop, substream, 969 SNDRV_PCM_STATE_SETUP); 970 } 971 972 /* 973 * pause callbacks 974 */ 975 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push) 976 { 977 struct snd_pcm_runtime *runtime = substream->runtime; 978 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE)) 979 return -ENOSYS; 980 if (push) { 981 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING) 982 return -EBADFD; 983 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED) 984 return -EBADFD; 985 runtime->trigger_master = substream; 986 return 0; 987 } 988 989 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push) 990 { 991 if (substream->runtime->trigger_master != substream) 992 return 0; 993 return substream->ops->trigger(substream, 994 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH : 995 SNDRV_PCM_TRIGGER_PAUSE_RELEASE); 996 } 997 998 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push) 999 { 1000 if (substream->runtime->trigger_master == substream) 1001 substream->ops->trigger(substream, 1002 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE : 1003 SNDRV_PCM_TRIGGER_PAUSE_PUSH); 1004 } 1005 1006 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push) 1007 { 1008 struct snd_pcm_runtime *runtime = substream->runtime; 1009 snd_pcm_trigger_tstamp(substream); 1010 if (push) { 1011 runtime->status->state = SNDRV_PCM_STATE_PAUSED; 1012 if (substream->timer) 1013 snd_timer_notify(substream->timer, 1014 SNDRV_TIMER_EVENT_MPAUSE, 1015 &runtime->trigger_tstamp); 1016 snd_pcm_tick_set(substream, 0); 1017 wake_up(&runtime->sleep); 1018 } else { 1019 runtime->status->state = SNDRV_PCM_STATE_RUNNING; 1020 if (runtime->sleep_min) 1021 snd_pcm_tick_prepare(substream); 1022 if (substream->timer) 1023 snd_timer_notify(substream->timer, 1024 SNDRV_TIMER_EVENT_MCONTINUE, 1025 &runtime->trigger_tstamp); 1026 } 1027 } 1028 1029 static struct action_ops snd_pcm_action_pause = { 1030 .pre_action = snd_pcm_pre_pause, 1031 .do_action = snd_pcm_do_pause, 1032 .undo_action = snd_pcm_undo_pause, 1033 .post_action = snd_pcm_post_pause 1034 }; 1035 1036 /* 1037 * Push/release the pause for all linked streams. 1038 */ 1039 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push) 1040 { 1041 return snd_pcm_action(&snd_pcm_action_pause, substream, push); 1042 } 1043 1044 #ifdef CONFIG_PM 1045 /* suspend */ 1046 1047 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state) 1048 { 1049 struct snd_pcm_runtime *runtime = substream->runtime; 1050 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) 1051 return -EBUSY; 1052 runtime->trigger_master = substream; 1053 return 0; 1054 } 1055 1056 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state) 1057 { 1058 struct snd_pcm_runtime *runtime = substream->runtime; 1059 if (runtime->trigger_master != substream) 1060 return 0; 1061 if (! snd_pcm_running(substream)) 1062 return 0; 1063 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); 1064 return 0; /* suspend unconditionally */ 1065 } 1066 1067 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state) 1068 { 1069 struct snd_pcm_runtime *runtime = substream->runtime; 1070 snd_pcm_trigger_tstamp(substream); 1071 if (substream->timer) 1072 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND, 1073 &runtime->trigger_tstamp); 1074 runtime->status->suspended_state = runtime->status->state; 1075 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED; 1076 snd_pcm_tick_set(substream, 0); 1077 wake_up(&runtime->sleep); 1078 } 1079 1080 static struct action_ops snd_pcm_action_suspend = { 1081 .pre_action = snd_pcm_pre_suspend, 1082 .do_action = snd_pcm_do_suspend, 1083 .post_action = snd_pcm_post_suspend 1084 }; 1085 1086 /** 1087 * snd_pcm_suspend 1088 * @substream: the PCM substream 1089 * 1090 * Trigger SUSPEND to all linked streams. 1091 * After this call, all streams are changed to SUSPENDED state. 1092 */ 1093 int snd_pcm_suspend(struct snd_pcm_substream *substream) 1094 { 1095 int err; 1096 unsigned long flags; 1097 1098 if (! substream) 1099 return 0; 1100 1101 snd_pcm_stream_lock_irqsave(substream, flags); 1102 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0); 1103 snd_pcm_stream_unlock_irqrestore(substream, flags); 1104 return err; 1105 } 1106 1107 EXPORT_SYMBOL(snd_pcm_suspend); 1108 1109 /** 1110 * snd_pcm_suspend_all 1111 * @pcm: the PCM instance 1112 * 1113 * Trigger SUSPEND to all substreams in the given pcm. 1114 * After this call, all streams are changed to SUSPENDED state. 1115 */ 1116 int snd_pcm_suspend_all(struct snd_pcm *pcm) 1117 { 1118 struct snd_pcm_substream *substream; 1119 int stream, err = 0; 1120 1121 if (! pcm) 1122 return 0; 1123 1124 for (stream = 0; stream < 2; stream++) { 1125 for (substream = pcm->streams[stream].substream; 1126 substream; substream = substream->next) { 1127 /* FIXME: the open/close code should lock this as well */ 1128 if (substream->runtime == NULL) 1129 continue; 1130 err = snd_pcm_suspend(substream); 1131 if (err < 0 && err != -EBUSY) 1132 return err; 1133 } 1134 } 1135 return 0; 1136 } 1137 1138 EXPORT_SYMBOL(snd_pcm_suspend_all); 1139 1140 /* resume */ 1141 1142 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state) 1143 { 1144 struct snd_pcm_runtime *runtime = substream->runtime; 1145 if (!(runtime->info & SNDRV_PCM_INFO_RESUME)) 1146 return -ENOSYS; 1147 runtime->trigger_master = substream; 1148 return 0; 1149 } 1150 1151 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state) 1152 { 1153 struct snd_pcm_runtime *runtime = substream->runtime; 1154 if (runtime->trigger_master != substream) 1155 return 0; 1156 /* DMA not running previously? */ 1157 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING && 1158 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING || 1159 substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) 1160 return 0; 1161 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME); 1162 } 1163 1164 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state) 1165 { 1166 if (substream->runtime->trigger_master == substream && 1167 snd_pcm_running(substream)) 1168 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); 1169 } 1170 1171 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state) 1172 { 1173 struct snd_pcm_runtime *runtime = substream->runtime; 1174 snd_pcm_trigger_tstamp(substream); 1175 if (substream->timer) 1176 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME, 1177 &runtime->trigger_tstamp); 1178 runtime->status->state = runtime->status->suspended_state; 1179 if (runtime->sleep_min) 1180 snd_pcm_tick_prepare(substream); 1181 } 1182 1183 static struct action_ops snd_pcm_action_resume = { 1184 .pre_action = snd_pcm_pre_resume, 1185 .do_action = snd_pcm_do_resume, 1186 .undo_action = snd_pcm_undo_resume, 1187 .post_action = snd_pcm_post_resume 1188 }; 1189 1190 static int snd_pcm_resume(struct snd_pcm_substream *substream) 1191 { 1192 struct snd_card *card = substream->pcm->card; 1193 int res; 1194 1195 snd_power_lock(card); 1196 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0) 1197 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0); 1198 snd_power_unlock(card); 1199 return res; 1200 } 1201 1202 #else 1203 1204 static int snd_pcm_resume(struct snd_pcm_substream *substream) 1205 { 1206 return -ENOSYS; 1207 } 1208 1209 #endif /* CONFIG_PM */ 1210 1211 /* 1212 * xrun ioctl 1213 * 1214 * Change the RUNNING stream(s) to XRUN state. 1215 */ 1216 static int snd_pcm_xrun(struct snd_pcm_substream *substream) 1217 { 1218 struct snd_card *card = substream->pcm->card; 1219 struct snd_pcm_runtime *runtime = substream->runtime; 1220 int result; 1221 1222 snd_power_lock(card); 1223 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) { 1224 result = snd_power_wait(card, SNDRV_CTL_POWER_D0); 1225 if (result < 0) 1226 goto _unlock; 1227 } 1228 1229 snd_pcm_stream_lock_irq(substream); 1230 switch (runtime->status->state) { 1231 case SNDRV_PCM_STATE_XRUN: 1232 result = 0; /* already there */ 1233 break; 1234 case SNDRV_PCM_STATE_RUNNING: 1235 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); 1236 break; 1237 default: 1238 result = -EBADFD; 1239 } 1240 snd_pcm_stream_unlock_irq(substream); 1241 _unlock: 1242 snd_power_unlock(card); 1243 return result; 1244 } 1245 1246 /* 1247 * reset ioctl 1248 */ 1249 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state) 1250 { 1251 struct snd_pcm_runtime *runtime = substream->runtime; 1252 switch (runtime->status->state) { 1253 case SNDRV_PCM_STATE_RUNNING: 1254 case SNDRV_PCM_STATE_PREPARED: 1255 case SNDRV_PCM_STATE_PAUSED: 1256 case SNDRV_PCM_STATE_SUSPENDED: 1257 return 0; 1258 default: 1259 return -EBADFD; 1260 } 1261 } 1262 1263 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state) 1264 { 1265 struct snd_pcm_runtime *runtime = substream->runtime; 1266 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL); 1267 if (err < 0) 1268 return err; 1269 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, ); 1270 runtime->hw_ptr_base = 0; 1271 runtime->hw_ptr_interrupt = runtime->status->hw_ptr - 1272 runtime->status->hw_ptr % runtime->period_size; 1273 runtime->silence_start = runtime->status->hw_ptr; 1274 runtime->silence_filled = 0; 1275 return 0; 1276 } 1277 1278 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state) 1279 { 1280 struct snd_pcm_runtime *runtime = substream->runtime; 1281 runtime->control->appl_ptr = runtime->status->hw_ptr; 1282 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 1283 runtime->silence_size > 0) 1284 snd_pcm_playback_silence(substream, ULONG_MAX); 1285 } 1286 1287 static struct action_ops snd_pcm_action_reset = { 1288 .pre_action = snd_pcm_pre_reset, 1289 .do_action = snd_pcm_do_reset, 1290 .post_action = snd_pcm_post_reset 1291 }; 1292 1293 static int snd_pcm_reset(struct snd_pcm_substream *substream) 1294 { 1295 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0); 1296 } 1297 1298 /* 1299 * prepare ioctl 1300 */ 1301 /* we use the second argument for updating f_flags */ 1302 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, 1303 int f_flags) 1304 { 1305 struct snd_pcm_runtime *runtime = substream->runtime; 1306 if (runtime->status->state == SNDRV_PCM_STATE_OPEN || 1307 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED) 1308 return -EBADFD; 1309 if (snd_pcm_running(substream)) 1310 return -EBUSY; 1311 substream->f_flags = f_flags; 1312 return 0; 1313 } 1314 1315 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state) 1316 { 1317 int err; 1318 err = substream->ops->prepare(substream); 1319 if (err < 0) 1320 return err; 1321 return snd_pcm_do_reset(substream, 0); 1322 } 1323 1324 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state) 1325 { 1326 struct snd_pcm_runtime *runtime = substream->runtime; 1327 runtime->control->appl_ptr = runtime->status->hw_ptr; 1328 runtime->status->state = SNDRV_PCM_STATE_PREPARED; 1329 } 1330 1331 static struct action_ops snd_pcm_action_prepare = { 1332 .pre_action = snd_pcm_pre_prepare, 1333 .do_action = snd_pcm_do_prepare, 1334 .post_action = snd_pcm_post_prepare 1335 }; 1336 1337 /** 1338 * snd_pcm_prepare 1339 * @substream: the PCM substream instance 1340 * @file: file to refer f_flags 1341 * 1342 * Prepare the PCM substream to be triggerable. 1343 */ 1344 static int snd_pcm_prepare(struct snd_pcm_substream *substream, 1345 struct file *file) 1346 { 1347 int res; 1348 struct snd_card *card = substream->pcm->card; 1349 int f_flags; 1350 1351 if (file) 1352 f_flags = file->f_flags; 1353 else 1354 f_flags = substream->f_flags; 1355 1356 snd_power_lock(card); 1357 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0) 1358 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, 1359 substream, f_flags); 1360 snd_power_unlock(card); 1361 return res; 1362 } 1363 1364 /* 1365 * drain ioctl 1366 */ 1367 1368 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state) 1369 { 1370 if (substream->f_flags & O_NONBLOCK) 1371 return -EAGAIN; 1372 substream->runtime->trigger_master = substream; 1373 return 0; 1374 } 1375 1376 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state) 1377 { 1378 struct snd_pcm_runtime *runtime = substream->runtime; 1379 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1380 switch (runtime->status->state) { 1381 case SNDRV_PCM_STATE_PREPARED: 1382 /* start playback stream if possible */ 1383 if (! snd_pcm_playback_empty(substream)) { 1384 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING); 1385 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING); 1386 } 1387 break; 1388 case SNDRV_PCM_STATE_RUNNING: 1389 runtime->status->state = SNDRV_PCM_STATE_DRAINING; 1390 break; 1391 default: 1392 break; 1393 } 1394 } else { 1395 /* stop running stream */ 1396 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) { 1397 int state = snd_pcm_capture_avail(runtime) > 0 ? 1398 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP; 1399 snd_pcm_do_stop(substream, state); 1400 snd_pcm_post_stop(substream, state); 1401 } 1402 } 1403 return 0; 1404 } 1405 1406 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state) 1407 { 1408 } 1409 1410 static struct action_ops snd_pcm_action_drain_init = { 1411 .pre_action = snd_pcm_pre_drain_init, 1412 .do_action = snd_pcm_do_drain_init, 1413 .post_action = snd_pcm_post_drain_init 1414 }; 1415 1416 struct drain_rec { 1417 struct snd_pcm_substream *substream; 1418 wait_queue_t wait; 1419 snd_pcm_uframes_t stop_threshold; 1420 }; 1421 1422 static int snd_pcm_drop(struct snd_pcm_substream *substream); 1423 1424 /* 1425 * Drain the stream(s). 1426 * When the substream is linked, sync until the draining of all playback streams 1427 * is finished. 1428 * After this call, all streams are supposed to be either SETUP or DRAINING 1429 * (capture only) state. 1430 */ 1431 static int snd_pcm_drain(struct snd_pcm_substream *substream) 1432 { 1433 struct snd_card *card; 1434 struct snd_pcm_runtime *runtime; 1435 struct snd_pcm_substream *s; 1436 int result = 0; 1437 int i, num_drecs; 1438 struct drain_rec *drec, drec_tmp, *d; 1439 1440 snd_assert(substream != NULL, return -ENXIO); 1441 card = substream->pcm->card; 1442 runtime = substream->runtime; 1443 1444 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 1445 return -EBADFD; 1446 1447 snd_power_lock(card); 1448 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) { 1449 result = snd_power_wait(card, SNDRV_CTL_POWER_D0); 1450 if (result < 0) { 1451 snd_power_unlock(card); 1452 return result; 1453 } 1454 } 1455 1456 /* allocate temporary record for drain sync */ 1457 down_read(&snd_pcm_link_rwsem); 1458 if (snd_pcm_stream_linked(substream)) { 1459 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL); 1460 if (! drec) { 1461 up_read(&snd_pcm_link_rwsem); 1462 snd_power_unlock(card); 1463 return -ENOMEM; 1464 } 1465 } else 1466 drec = &drec_tmp; 1467 1468 /* count only playback streams */ 1469 num_drecs = 0; 1470 snd_pcm_group_for_each_entry(s, substream) { 1471 runtime = s->runtime; 1472 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1473 d = &drec[num_drecs++]; 1474 d->substream = s; 1475 init_waitqueue_entry(&d->wait, current); 1476 add_wait_queue(&runtime->sleep, &d->wait); 1477 /* stop_threshold fixup to avoid endless loop when 1478 * stop_threshold > buffer_size 1479 */ 1480 d->stop_threshold = runtime->stop_threshold; 1481 if (runtime->stop_threshold > runtime->buffer_size) 1482 runtime->stop_threshold = runtime->buffer_size; 1483 } 1484 } 1485 up_read(&snd_pcm_link_rwsem); 1486 1487 snd_pcm_stream_lock_irq(substream); 1488 /* resume pause */ 1489 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED) 1490 snd_pcm_pause(substream, 0); 1491 1492 /* pre-start/stop - all running streams are changed to DRAINING state */ 1493 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0); 1494 if (result < 0) { 1495 snd_pcm_stream_unlock_irq(substream); 1496 goto _error; 1497 } 1498 1499 for (;;) { 1500 long tout; 1501 if (signal_pending(current)) { 1502 result = -ERESTARTSYS; 1503 break; 1504 } 1505 /* all finished? */ 1506 for (i = 0; i < num_drecs; i++) { 1507 runtime = drec[i].substream->runtime; 1508 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) 1509 break; 1510 } 1511 if (i == num_drecs) 1512 break; /* yes, all drained */ 1513 1514 set_current_state(TASK_INTERRUPTIBLE); 1515 snd_pcm_stream_unlock_irq(substream); 1516 snd_power_unlock(card); 1517 tout = schedule_timeout(10 * HZ); 1518 snd_power_lock(card); 1519 snd_pcm_stream_lock_irq(substream); 1520 if (tout == 0) { 1521 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) 1522 result = -ESTRPIPE; 1523 else { 1524 snd_printd("playback drain error (DMA or IRQ trouble?)\n"); 1525 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); 1526 result = -EIO; 1527 } 1528 break; 1529 } 1530 } 1531 1532 snd_pcm_stream_unlock_irq(substream); 1533 1534 _error: 1535 for (i = 0; i < num_drecs; i++) { 1536 d = &drec[i]; 1537 runtime = d->substream->runtime; 1538 remove_wait_queue(&runtime->sleep, &d->wait); 1539 runtime->stop_threshold = d->stop_threshold; 1540 } 1541 1542 if (drec != &drec_tmp) 1543 kfree(drec); 1544 snd_power_unlock(card); 1545 1546 return result; 1547 } 1548 1549 /* 1550 * drop ioctl 1551 * 1552 * Immediately put all linked substreams into SETUP state. 1553 */ 1554 static int snd_pcm_drop(struct snd_pcm_substream *substream) 1555 { 1556 struct snd_pcm_runtime *runtime; 1557 struct snd_card *card; 1558 int result = 0; 1559 1560 snd_assert(substream != NULL, return -ENXIO); 1561 runtime = substream->runtime; 1562 card = substream->pcm->card; 1563 1564 if (runtime->status->state == SNDRV_PCM_STATE_OPEN || 1565 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED) 1566 return -EBADFD; 1567 1568 snd_power_lock(card); 1569 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) { 1570 result = snd_power_wait(card, SNDRV_CTL_POWER_D0); 1571 if (result < 0) 1572 goto _unlock; 1573 } 1574 1575 snd_pcm_stream_lock_irq(substream); 1576 /* resume pause */ 1577 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED) 1578 snd_pcm_pause(substream, 0); 1579 1580 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); 1581 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */ 1582 snd_pcm_stream_unlock_irq(substream); 1583 _unlock: 1584 snd_power_unlock(card); 1585 return result; 1586 } 1587 1588 1589 /* WARNING: Don't forget to fput back the file */ 1590 static struct file *snd_pcm_file_fd(int fd) 1591 { 1592 struct file *file; 1593 struct inode *inode; 1594 unsigned int minor; 1595 1596 file = fget(fd); 1597 if (!file) 1598 return NULL; 1599 inode = file->f_path.dentry->d_inode; 1600 if (!S_ISCHR(inode->i_mode) || 1601 imajor(inode) != snd_major) { 1602 fput(file); 1603 return NULL; 1604 } 1605 minor = iminor(inode); 1606 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) && 1607 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) { 1608 fput(file); 1609 return NULL; 1610 } 1611 return file; 1612 } 1613 1614 /* 1615 * PCM link handling 1616 */ 1617 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd) 1618 { 1619 int res = 0; 1620 struct file *file; 1621 struct snd_pcm_file *pcm_file; 1622 struct snd_pcm_substream *substream1; 1623 1624 file = snd_pcm_file_fd(fd); 1625 if (!file) 1626 return -EBADFD; 1627 pcm_file = file->private_data; 1628 substream1 = pcm_file->substream; 1629 down_write(&snd_pcm_link_rwsem); 1630 write_lock_irq(&snd_pcm_link_rwlock); 1631 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN || 1632 substream->runtime->status->state != substream1->runtime->status->state) { 1633 res = -EBADFD; 1634 goto _end; 1635 } 1636 if (snd_pcm_stream_linked(substream1)) { 1637 res = -EALREADY; 1638 goto _end; 1639 } 1640 if (!snd_pcm_stream_linked(substream)) { 1641 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC); 1642 if (substream->group == NULL) { 1643 res = -ENOMEM; 1644 goto _end; 1645 } 1646 spin_lock_init(&substream->group->lock); 1647 INIT_LIST_HEAD(&substream->group->substreams); 1648 list_add_tail(&substream->link_list, &substream->group->substreams); 1649 substream->group->count = 1; 1650 } 1651 list_add_tail(&substream1->link_list, &substream->group->substreams); 1652 substream->group->count++; 1653 substream1->group = substream->group; 1654 _end: 1655 write_unlock_irq(&snd_pcm_link_rwlock); 1656 up_write(&snd_pcm_link_rwsem); 1657 fput(file); 1658 return res; 1659 } 1660 1661 static void relink_to_local(struct snd_pcm_substream *substream) 1662 { 1663 substream->group = &substream->self_group; 1664 INIT_LIST_HEAD(&substream->self_group.substreams); 1665 list_add_tail(&substream->link_list, &substream->self_group.substreams); 1666 } 1667 1668 static int snd_pcm_unlink(struct snd_pcm_substream *substream) 1669 { 1670 struct snd_pcm_substream *s; 1671 int res = 0; 1672 1673 down_write(&snd_pcm_link_rwsem); 1674 write_lock_irq(&snd_pcm_link_rwlock); 1675 if (!snd_pcm_stream_linked(substream)) { 1676 res = -EALREADY; 1677 goto _end; 1678 } 1679 list_del(&substream->link_list); 1680 substream->group->count--; 1681 if (substream->group->count == 1) { /* detach the last stream, too */ 1682 snd_pcm_group_for_each_entry(s, substream) { 1683 relink_to_local(s); 1684 break; 1685 } 1686 kfree(substream->group); 1687 } 1688 relink_to_local(substream); 1689 _end: 1690 write_unlock_irq(&snd_pcm_link_rwlock); 1691 up_write(&snd_pcm_link_rwsem); 1692 return res; 1693 } 1694 1695 /* 1696 * hw configurator 1697 */ 1698 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params, 1699 struct snd_pcm_hw_rule *rule) 1700 { 1701 struct snd_interval t; 1702 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]), 1703 hw_param_interval_c(params, rule->deps[1]), &t); 1704 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 1705 } 1706 1707 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params, 1708 struct snd_pcm_hw_rule *rule) 1709 { 1710 struct snd_interval t; 1711 snd_interval_div(hw_param_interval_c(params, rule->deps[0]), 1712 hw_param_interval_c(params, rule->deps[1]), &t); 1713 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 1714 } 1715 1716 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params, 1717 struct snd_pcm_hw_rule *rule) 1718 { 1719 struct snd_interval t; 1720 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]), 1721 hw_param_interval_c(params, rule->deps[1]), 1722 (unsigned long) rule->private, &t); 1723 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 1724 } 1725 1726 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params, 1727 struct snd_pcm_hw_rule *rule) 1728 { 1729 struct snd_interval t; 1730 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]), 1731 (unsigned long) rule->private, 1732 hw_param_interval_c(params, rule->deps[1]), &t); 1733 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 1734 } 1735 1736 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params, 1737 struct snd_pcm_hw_rule *rule) 1738 { 1739 unsigned int k; 1740 struct snd_interval *i = hw_param_interval(params, rule->deps[0]); 1741 struct snd_mask m; 1742 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 1743 snd_mask_any(&m); 1744 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) { 1745 int bits; 1746 if (! snd_mask_test(mask, k)) 1747 continue; 1748 bits = snd_pcm_format_physical_width(k); 1749 if (bits <= 0) 1750 continue; /* ignore invalid formats */ 1751 if ((unsigned)bits < i->min || (unsigned)bits > i->max) 1752 snd_mask_reset(&m, k); 1753 } 1754 return snd_mask_refine(mask, &m); 1755 } 1756 1757 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params, 1758 struct snd_pcm_hw_rule *rule) 1759 { 1760 struct snd_interval t; 1761 unsigned int k; 1762 t.min = UINT_MAX; 1763 t.max = 0; 1764 t.openmin = 0; 1765 t.openmax = 0; 1766 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) { 1767 int bits; 1768 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k)) 1769 continue; 1770 bits = snd_pcm_format_physical_width(k); 1771 if (bits <= 0) 1772 continue; /* ignore invalid formats */ 1773 if (t.min > (unsigned)bits) 1774 t.min = bits; 1775 if (t.max < (unsigned)bits) 1776 t.max = bits; 1777 } 1778 t.integer = 1; 1779 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 1780 } 1781 1782 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 1783 #error "Change this table" 1784 #endif 1785 1786 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100, 1787 48000, 64000, 88200, 96000, 176400, 192000 }; 1788 1789 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params, 1790 struct snd_pcm_hw_rule *rule) 1791 { 1792 struct snd_pcm_hardware *hw = rule->private; 1793 return snd_interval_list(hw_param_interval(params, rule->var), 1794 ARRAY_SIZE(rates), rates, hw->rates); 1795 } 1796 1797 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params, 1798 struct snd_pcm_hw_rule *rule) 1799 { 1800 struct snd_interval t; 1801 struct snd_pcm_substream *substream = rule->private; 1802 t.min = 0; 1803 t.max = substream->buffer_bytes_max; 1804 t.openmin = 0; 1805 t.openmax = 0; 1806 t.integer = 1; 1807 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 1808 } 1809 1810 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream) 1811 { 1812 struct snd_pcm_runtime *runtime = substream->runtime; 1813 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; 1814 int k, err; 1815 1816 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { 1817 snd_mask_any(constrs_mask(constrs, k)); 1818 } 1819 1820 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { 1821 snd_interval_any(constrs_interval(constrs, k)); 1822 } 1823 1824 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS)); 1825 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)); 1826 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)); 1827 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)); 1828 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS)); 1829 1830 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, 1831 snd_pcm_hw_rule_format, NULL, 1832 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 1833 if (err < 0) 1834 return err; 1835 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 1836 snd_pcm_hw_rule_sample_bits, NULL, 1837 SNDRV_PCM_HW_PARAM_FORMAT, 1838 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 1839 if (err < 0) 1840 return err; 1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 1842 snd_pcm_hw_rule_div, NULL, 1843 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); 1844 if (err < 0) 1845 return err; 1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 1847 snd_pcm_hw_rule_mul, NULL, 1848 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); 1849 if (err < 0) 1850 return err; 1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 1852 snd_pcm_hw_rule_mulkdiv, (void*) 8, 1853 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); 1854 if (err < 0) 1855 return err; 1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 1857 snd_pcm_hw_rule_mulkdiv, (void*) 8, 1858 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1); 1859 if (err < 0) 1860 return err; 1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 1862 snd_pcm_hw_rule_div, NULL, 1863 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 1864 if (err < 0) 1865 return err; 1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 1867 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 1868 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1); 1869 if (err < 0) 1870 return err; 1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 1872 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 1873 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1); 1874 if (err < 0) 1875 return err; 1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, 1877 snd_pcm_hw_rule_div, NULL, 1878 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); 1879 if (err < 0) 1880 return err; 1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 1882 snd_pcm_hw_rule_div, NULL, 1883 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); 1884 if (err < 0) 1885 return err; 1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 1887 snd_pcm_hw_rule_mulkdiv, (void*) 8, 1888 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 1889 if (err < 0) 1890 return err; 1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 1892 snd_pcm_hw_rule_muldivk, (void*) 1000000, 1893 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); 1894 if (err < 0) 1895 return err; 1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 1897 snd_pcm_hw_rule_mul, NULL, 1898 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); 1899 if (err < 0) 1900 return err; 1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 1902 snd_pcm_hw_rule_mulkdiv, (void*) 8, 1903 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 1904 if (err < 0) 1905 return err; 1906 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 1907 snd_pcm_hw_rule_muldivk, (void*) 1000000, 1908 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); 1909 if (err < 0) 1910 return err; 1911 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 1912 snd_pcm_hw_rule_muldivk, (void*) 8, 1913 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 1914 if (err < 0) 1915 return err; 1916 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 1917 snd_pcm_hw_rule_muldivk, (void*) 8, 1918 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 1919 if (err < 0) 1920 return err; 1921 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1922 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 1923 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); 1924 if (err < 0) 1925 return err; 1926 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 1927 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 1928 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); 1929 if (err < 0) 1930 return err; 1931 return 0; 1932 } 1933 1934 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream) 1935 { 1936 struct snd_pcm_runtime *runtime = substream->runtime; 1937 struct snd_pcm_hardware *hw = &runtime->hw; 1938 int err; 1939 unsigned int mask = 0; 1940 1941 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) 1942 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED; 1943 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) 1944 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED; 1945 if (hw->info & SNDRV_PCM_INFO_MMAP) { 1946 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) 1947 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED; 1948 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) 1949 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED; 1950 if (hw->info & SNDRV_PCM_INFO_COMPLEX) 1951 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX; 1952 } 1953 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask); 1954 snd_assert(err >= 0, return -EINVAL); 1955 1956 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats); 1957 snd_assert(err >= 0, return -EINVAL); 1958 1959 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD); 1960 snd_assert(err >= 0, return -EINVAL); 1961 1962 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 1963 hw->channels_min, hw->channels_max); 1964 snd_assert(err >= 0, return -EINVAL); 1965 1966 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, 1967 hw->rate_min, hw->rate_max); 1968 snd_assert(err >= 0, return -EINVAL); 1969 1970 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 1971 hw->period_bytes_min, hw->period_bytes_max); 1972 snd_assert(err >= 0, return -EINVAL); 1973 1974 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS, 1975 hw->periods_min, hw->periods_max); 1976 snd_assert(err >= 0, return -EINVAL); 1977 1978 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 1979 hw->period_bytes_min, hw->buffer_bytes_max); 1980 snd_assert(err >= 0, return -EINVAL); 1981 1982 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 1983 snd_pcm_hw_rule_buffer_bytes_max, substream, 1984 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1); 1985 if (err < 0) 1986 return err; 1987 1988 /* FIXME: remove */ 1989 if (runtime->dma_bytes) { 1990 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes); 1991 snd_assert(err >= 0, return -EINVAL); 1992 } 1993 1994 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) { 1995 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 1996 snd_pcm_hw_rule_rate, hw, 1997 SNDRV_PCM_HW_PARAM_RATE, -1); 1998 if (err < 0) 1999 return err; 2000 } 2001 2002 /* FIXME: this belong to lowlevel */ 2003 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME, 2004 1000000 / HZ, 1000000 / HZ); 2005 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); 2006 2007 return 0; 2008 } 2009 2010 static void pcm_release_private(struct snd_pcm_substream *substream) 2011 { 2012 snd_pcm_unlink(substream); 2013 } 2014 2015 void snd_pcm_release_substream(struct snd_pcm_substream *substream) 2016 { 2017 substream->ref_count--; 2018 if (substream->ref_count > 0) 2019 return; 2020 2021 snd_pcm_drop(substream); 2022 if (substream->hw_opened) { 2023 if (substream->ops->hw_free != NULL) 2024 substream->ops->hw_free(substream); 2025 substream->ops->close(substream); 2026 substream->hw_opened = 0; 2027 } 2028 if (substream->pcm_release) { 2029 substream->pcm_release(substream); 2030 substream->pcm_release = NULL; 2031 } 2032 snd_pcm_detach_substream(substream); 2033 } 2034 2035 EXPORT_SYMBOL(snd_pcm_release_substream); 2036 2037 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, 2038 struct file *file, 2039 struct snd_pcm_substream **rsubstream) 2040 { 2041 struct snd_pcm_substream *substream; 2042 int err; 2043 2044 err = snd_pcm_attach_substream(pcm, stream, file, &substream); 2045 if (err < 0) 2046 return err; 2047 if (substream->ref_count > 1) { 2048 *rsubstream = substream; 2049 return 0; 2050 } 2051 2052 err = snd_pcm_hw_constraints_init(substream); 2053 if (err < 0) { 2054 snd_printd("snd_pcm_hw_constraints_init failed\n"); 2055 goto error; 2056 } 2057 2058 if ((err = substream->ops->open(substream)) < 0) 2059 goto error; 2060 2061 substream->hw_opened = 1; 2062 2063 err = snd_pcm_hw_constraints_complete(substream); 2064 if (err < 0) { 2065 snd_printd("snd_pcm_hw_constraints_complete failed\n"); 2066 goto error; 2067 } 2068 2069 *rsubstream = substream; 2070 return 0; 2071 2072 error: 2073 snd_pcm_release_substream(substream); 2074 return err; 2075 } 2076 2077 EXPORT_SYMBOL(snd_pcm_open_substream); 2078 2079 static int snd_pcm_open_file(struct file *file, 2080 struct snd_pcm *pcm, 2081 int stream, 2082 struct snd_pcm_file **rpcm_file) 2083 { 2084 struct snd_pcm_file *pcm_file; 2085 struct snd_pcm_substream *substream; 2086 struct snd_pcm_str *str; 2087 int err; 2088 2089 snd_assert(rpcm_file != NULL, return -EINVAL); 2090 *rpcm_file = NULL; 2091 2092 err = snd_pcm_open_substream(pcm, stream, file, &substream); 2093 if (err < 0) 2094 return err; 2095 2096 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL); 2097 if (pcm_file == NULL) { 2098 snd_pcm_release_substream(substream); 2099 return -ENOMEM; 2100 } 2101 pcm_file->substream = substream; 2102 if (substream->ref_count == 1) { 2103 str = substream->pstr; 2104 substream->file = pcm_file; 2105 substream->pcm_release = pcm_release_private; 2106 } 2107 file->private_data = pcm_file; 2108 *rpcm_file = pcm_file; 2109 return 0; 2110 } 2111 2112 static int snd_pcm_playback_open(struct inode *inode, struct file *file) 2113 { 2114 struct snd_pcm *pcm; 2115 2116 pcm = snd_lookup_minor_data(iminor(inode), 2117 SNDRV_DEVICE_TYPE_PCM_PLAYBACK); 2118 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK); 2119 } 2120 2121 static int snd_pcm_capture_open(struct inode *inode, struct file *file) 2122 { 2123 struct snd_pcm *pcm; 2124 2125 pcm = snd_lookup_minor_data(iminor(inode), 2126 SNDRV_DEVICE_TYPE_PCM_CAPTURE); 2127 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE); 2128 } 2129 2130 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream) 2131 { 2132 int err; 2133 struct snd_pcm_file *pcm_file; 2134 wait_queue_t wait; 2135 2136 if (pcm == NULL) { 2137 err = -ENODEV; 2138 goto __error1; 2139 } 2140 err = snd_card_file_add(pcm->card, file); 2141 if (err < 0) 2142 goto __error1; 2143 if (!try_module_get(pcm->card->module)) { 2144 err = -EFAULT; 2145 goto __error2; 2146 } 2147 init_waitqueue_entry(&wait, current); 2148 add_wait_queue(&pcm->open_wait, &wait); 2149 mutex_lock(&pcm->open_mutex); 2150 while (1) { 2151 err = snd_pcm_open_file(file, pcm, stream, &pcm_file); 2152 if (err >= 0) 2153 break; 2154 if (err == -EAGAIN) { 2155 if (file->f_flags & O_NONBLOCK) { 2156 err = -EBUSY; 2157 break; 2158 } 2159 } else 2160 break; 2161 set_current_state(TASK_INTERRUPTIBLE); 2162 mutex_unlock(&pcm->open_mutex); 2163 schedule(); 2164 mutex_lock(&pcm->open_mutex); 2165 if (signal_pending(current)) { 2166 err = -ERESTARTSYS; 2167 break; 2168 } 2169 } 2170 remove_wait_queue(&pcm->open_wait, &wait); 2171 mutex_unlock(&pcm->open_mutex); 2172 if (err < 0) 2173 goto __error; 2174 return err; 2175 2176 __error: 2177 module_put(pcm->card->module); 2178 __error2: 2179 snd_card_file_remove(pcm->card, file); 2180 __error1: 2181 return err; 2182 } 2183 2184 static int snd_pcm_release(struct inode *inode, struct file *file) 2185 { 2186 struct snd_pcm *pcm; 2187 struct snd_pcm_substream *substream; 2188 struct snd_pcm_file *pcm_file; 2189 2190 pcm_file = file->private_data; 2191 substream = pcm_file->substream; 2192 snd_assert(substream != NULL, return -ENXIO); 2193 pcm = substream->pcm; 2194 fasync_helper(-1, file, 0, &substream->runtime->fasync); 2195 mutex_lock(&pcm->open_mutex); 2196 snd_pcm_release_substream(substream); 2197 kfree(pcm_file); 2198 mutex_unlock(&pcm->open_mutex); 2199 wake_up(&pcm->open_wait); 2200 module_put(pcm->card->module); 2201 snd_card_file_remove(pcm->card, file); 2202 return 0; 2203 } 2204 2205 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream, 2206 snd_pcm_uframes_t frames) 2207 { 2208 struct snd_pcm_runtime *runtime = substream->runtime; 2209 snd_pcm_sframes_t appl_ptr; 2210 snd_pcm_sframes_t ret; 2211 snd_pcm_sframes_t hw_avail; 2212 2213 if (frames == 0) 2214 return 0; 2215 2216 snd_pcm_stream_lock_irq(substream); 2217 switch (runtime->status->state) { 2218 case SNDRV_PCM_STATE_PREPARED: 2219 break; 2220 case SNDRV_PCM_STATE_DRAINING: 2221 case SNDRV_PCM_STATE_RUNNING: 2222 if (snd_pcm_update_hw_ptr(substream) >= 0) 2223 break; 2224 /* Fall through */ 2225 case SNDRV_PCM_STATE_XRUN: 2226 ret = -EPIPE; 2227 goto __end; 2228 default: 2229 ret = -EBADFD; 2230 goto __end; 2231 } 2232 2233 hw_avail = snd_pcm_playback_hw_avail(runtime); 2234 if (hw_avail <= 0) { 2235 ret = 0; 2236 goto __end; 2237 } 2238 if (frames > (snd_pcm_uframes_t)hw_avail) 2239 frames = hw_avail; 2240 else 2241 frames -= frames % runtime->xfer_align; 2242 appl_ptr = runtime->control->appl_ptr - frames; 2243 if (appl_ptr < 0) 2244 appl_ptr += runtime->boundary; 2245 runtime->control->appl_ptr = appl_ptr; 2246 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING && 2247 runtime->sleep_min) 2248 snd_pcm_tick_prepare(substream); 2249 ret = frames; 2250 __end: 2251 snd_pcm_stream_unlock_irq(substream); 2252 return ret; 2253 } 2254 2255 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream, 2256 snd_pcm_uframes_t frames) 2257 { 2258 struct snd_pcm_runtime *runtime = substream->runtime; 2259 snd_pcm_sframes_t appl_ptr; 2260 snd_pcm_sframes_t ret; 2261 snd_pcm_sframes_t hw_avail; 2262 2263 if (frames == 0) 2264 return 0; 2265 2266 snd_pcm_stream_lock_irq(substream); 2267 switch (runtime->status->state) { 2268 case SNDRV_PCM_STATE_PREPARED: 2269 case SNDRV_PCM_STATE_DRAINING: 2270 break; 2271 case SNDRV_PCM_STATE_RUNNING: 2272 if (snd_pcm_update_hw_ptr(substream) >= 0) 2273 break; 2274 /* Fall through */ 2275 case SNDRV_PCM_STATE_XRUN: 2276 ret = -EPIPE; 2277 goto __end; 2278 default: 2279 ret = -EBADFD; 2280 goto __end; 2281 } 2282 2283 hw_avail = snd_pcm_capture_hw_avail(runtime); 2284 if (hw_avail <= 0) { 2285 ret = 0; 2286 goto __end; 2287 } 2288 if (frames > (snd_pcm_uframes_t)hw_avail) 2289 frames = hw_avail; 2290 else 2291 frames -= frames % runtime->xfer_align; 2292 appl_ptr = runtime->control->appl_ptr - frames; 2293 if (appl_ptr < 0) 2294 appl_ptr += runtime->boundary; 2295 runtime->control->appl_ptr = appl_ptr; 2296 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING && 2297 runtime->sleep_min) 2298 snd_pcm_tick_prepare(substream); 2299 ret = frames; 2300 __end: 2301 snd_pcm_stream_unlock_irq(substream); 2302 return ret; 2303 } 2304 2305 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream, 2306 snd_pcm_uframes_t frames) 2307 { 2308 struct snd_pcm_runtime *runtime = substream->runtime; 2309 snd_pcm_sframes_t appl_ptr; 2310 snd_pcm_sframes_t ret; 2311 snd_pcm_sframes_t avail; 2312 2313 if (frames == 0) 2314 return 0; 2315 2316 snd_pcm_stream_lock_irq(substream); 2317 switch (runtime->status->state) { 2318 case SNDRV_PCM_STATE_PREPARED: 2319 case SNDRV_PCM_STATE_PAUSED: 2320 break; 2321 case SNDRV_PCM_STATE_DRAINING: 2322 case SNDRV_PCM_STATE_RUNNING: 2323 if (snd_pcm_update_hw_ptr(substream) >= 0) 2324 break; 2325 /* Fall through */ 2326 case SNDRV_PCM_STATE_XRUN: 2327 ret = -EPIPE; 2328 goto __end; 2329 default: 2330 ret = -EBADFD; 2331 goto __end; 2332 } 2333 2334 avail = snd_pcm_playback_avail(runtime); 2335 if (avail <= 0) { 2336 ret = 0; 2337 goto __end; 2338 } 2339 if (frames > (snd_pcm_uframes_t)avail) 2340 frames = avail; 2341 else 2342 frames -= frames % runtime->xfer_align; 2343 appl_ptr = runtime->control->appl_ptr + frames; 2344 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary) 2345 appl_ptr -= runtime->boundary; 2346 runtime->control->appl_ptr = appl_ptr; 2347 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING && 2348 runtime->sleep_min) 2349 snd_pcm_tick_prepare(substream); 2350 ret = frames; 2351 __end: 2352 snd_pcm_stream_unlock_irq(substream); 2353 return ret; 2354 } 2355 2356 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream, 2357 snd_pcm_uframes_t frames) 2358 { 2359 struct snd_pcm_runtime *runtime = substream->runtime; 2360 snd_pcm_sframes_t appl_ptr; 2361 snd_pcm_sframes_t ret; 2362 snd_pcm_sframes_t avail; 2363 2364 if (frames == 0) 2365 return 0; 2366 2367 snd_pcm_stream_lock_irq(substream); 2368 switch (runtime->status->state) { 2369 case SNDRV_PCM_STATE_PREPARED: 2370 case SNDRV_PCM_STATE_DRAINING: 2371 case SNDRV_PCM_STATE_PAUSED: 2372 break; 2373 case SNDRV_PCM_STATE_RUNNING: 2374 if (snd_pcm_update_hw_ptr(substream) >= 0) 2375 break; 2376 /* Fall through */ 2377 case SNDRV_PCM_STATE_XRUN: 2378 ret = -EPIPE; 2379 goto __end; 2380 default: 2381 ret = -EBADFD; 2382 goto __end; 2383 } 2384 2385 avail = snd_pcm_capture_avail(runtime); 2386 if (avail <= 0) { 2387 ret = 0; 2388 goto __end; 2389 } 2390 if (frames > (snd_pcm_uframes_t)avail) 2391 frames = avail; 2392 else 2393 frames -= frames % runtime->xfer_align; 2394 appl_ptr = runtime->control->appl_ptr + frames; 2395 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary) 2396 appl_ptr -= runtime->boundary; 2397 runtime->control->appl_ptr = appl_ptr; 2398 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING && 2399 runtime->sleep_min) 2400 snd_pcm_tick_prepare(substream); 2401 ret = frames; 2402 __end: 2403 snd_pcm_stream_unlock_irq(substream); 2404 return ret; 2405 } 2406 2407 static int snd_pcm_hwsync(struct snd_pcm_substream *substream) 2408 { 2409 struct snd_pcm_runtime *runtime = substream->runtime; 2410 int err; 2411 2412 snd_pcm_stream_lock_irq(substream); 2413 switch (runtime->status->state) { 2414 case SNDRV_PCM_STATE_DRAINING: 2415 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 2416 goto __badfd; 2417 case SNDRV_PCM_STATE_RUNNING: 2418 if ((err = snd_pcm_update_hw_ptr(substream)) < 0) 2419 break; 2420 /* Fall through */ 2421 case SNDRV_PCM_STATE_PREPARED: 2422 case SNDRV_PCM_STATE_SUSPENDED: 2423 err = 0; 2424 break; 2425 case SNDRV_PCM_STATE_XRUN: 2426 err = -EPIPE; 2427 break; 2428 default: 2429 __badfd: 2430 err = -EBADFD; 2431 break; 2432 } 2433 snd_pcm_stream_unlock_irq(substream); 2434 return err; 2435 } 2436 2437 static int snd_pcm_delay(struct snd_pcm_substream *substream, 2438 snd_pcm_sframes_t __user *res) 2439 { 2440 struct snd_pcm_runtime *runtime = substream->runtime; 2441 int err; 2442 snd_pcm_sframes_t n = 0; 2443 2444 snd_pcm_stream_lock_irq(substream); 2445 switch (runtime->status->state) { 2446 case SNDRV_PCM_STATE_DRAINING: 2447 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 2448 goto __badfd; 2449 case SNDRV_PCM_STATE_RUNNING: 2450 if ((err = snd_pcm_update_hw_ptr(substream)) < 0) 2451 break; 2452 /* Fall through */ 2453 case SNDRV_PCM_STATE_PREPARED: 2454 case SNDRV_PCM_STATE_SUSPENDED: 2455 err = 0; 2456 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 2457 n = snd_pcm_playback_hw_avail(runtime); 2458 else 2459 n = snd_pcm_capture_avail(runtime); 2460 break; 2461 case SNDRV_PCM_STATE_XRUN: 2462 err = -EPIPE; 2463 break; 2464 default: 2465 __badfd: 2466 err = -EBADFD; 2467 break; 2468 } 2469 snd_pcm_stream_unlock_irq(substream); 2470 if (!err) 2471 if (put_user(n, res)) 2472 err = -EFAULT; 2473 return err; 2474 } 2475 2476 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream, 2477 struct snd_pcm_sync_ptr __user *_sync_ptr) 2478 { 2479 struct snd_pcm_runtime *runtime = substream->runtime; 2480 struct snd_pcm_sync_ptr sync_ptr; 2481 volatile struct snd_pcm_mmap_status *status; 2482 volatile struct snd_pcm_mmap_control *control; 2483 int err; 2484 2485 memset(&sync_ptr, 0, sizeof(sync_ptr)); 2486 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags))) 2487 return -EFAULT; 2488 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control))) 2489 return -EFAULT; 2490 status = runtime->status; 2491 control = runtime->control; 2492 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) { 2493 err = snd_pcm_hwsync(substream); 2494 if (err < 0) 2495 return err; 2496 } 2497 snd_pcm_stream_lock_irq(substream); 2498 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) 2499 control->appl_ptr = sync_ptr.c.control.appl_ptr; 2500 else 2501 sync_ptr.c.control.appl_ptr = control->appl_ptr; 2502 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) 2503 control->avail_min = sync_ptr.c.control.avail_min; 2504 else 2505 sync_ptr.c.control.avail_min = control->avail_min; 2506 sync_ptr.s.status.state = status->state; 2507 sync_ptr.s.status.hw_ptr = status->hw_ptr; 2508 sync_ptr.s.status.tstamp = status->tstamp; 2509 sync_ptr.s.status.suspended_state = status->suspended_state; 2510 snd_pcm_stream_unlock_irq(substream); 2511 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr))) 2512 return -EFAULT; 2513 return 0; 2514 } 2515 2516 static int snd_pcm_common_ioctl1(struct file *file, 2517 struct snd_pcm_substream *substream, 2518 unsigned int cmd, void __user *arg) 2519 { 2520 snd_assert(substream != NULL, return -ENXIO); 2521 2522 switch (cmd) { 2523 case SNDRV_PCM_IOCTL_PVERSION: 2524 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0; 2525 case SNDRV_PCM_IOCTL_INFO: 2526 return snd_pcm_info_user(substream, arg); 2527 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */ 2528 return 0; 2529 case SNDRV_PCM_IOCTL_HW_REFINE: 2530 return snd_pcm_hw_refine_user(substream, arg); 2531 case SNDRV_PCM_IOCTL_HW_PARAMS: 2532 return snd_pcm_hw_params_user(substream, arg); 2533 case SNDRV_PCM_IOCTL_HW_FREE: 2534 return snd_pcm_hw_free(substream); 2535 case SNDRV_PCM_IOCTL_SW_PARAMS: 2536 return snd_pcm_sw_params_user(substream, arg); 2537 case SNDRV_PCM_IOCTL_STATUS: 2538 return snd_pcm_status_user(substream, arg); 2539 case SNDRV_PCM_IOCTL_CHANNEL_INFO: 2540 return snd_pcm_channel_info_user(substream, arg); 2541 case SNDRV_PCM_IOCTL_PREPARE: 2542 return snd_pcm_prepare(substream, file); 2543 case SNDRV_PCM_IOCTL_RESET: 2544 return snd_pcm_reset(substream); 2545 case SNDRV_PCM_IOCTL_START: 2546 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING); 2547 case SNDRV_PCM_IOCTL_LINK: 2548 return snd_pcm_link(substream, (int)(unsigned long) arg); 2549 case SNDRV_PCM_IOCTL_UNLINK: 2550 return snd_pcm_unlink(substream); 2551 case SNDRV_PCM_IOCTL_RESUME: 2552 return snd_pcm_resume(substream); 2553 case SNDRV_PCM_IOCTL_XRUN: 2554 return snd_pcm_xrun(substream); 2555 case SNDRV_PCM_IOCTL_HWSYNC: 2556 return snd_pcm_hwsync(substream); 2557 case SNDRV_PCM_IOCTL_DELAY: 2558 return snd_pcm_delay(substream, arg); 2559 case SNDRV_PCM_IOCTL_SYNC_PTR: 2560 return snd_pcm_sync_ptr(substream, arg); 2561 #ifdef CONFIG_SND_SUPPORT_OLD_API 2562 case SNDRV_PCM_IOCTL_HW_REFINE_OLD: 2563 return snd_pcm_hw_refine_old_user(substream, arg); 2564 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD: 2565 return snd_pcm_hw_params_old_user(substream, arg); 2566 #endif 2567 case SNDRV_PCM_IOCTL_DRAIN: 2568 return snd_pcm_drain(substream); 2569 case SNDRV_PCM_IOCTL_DROP: 2570 return snd_pcm_drop(substream); 2571 case SNDRV_PCM_IOCTL_PAUSE: 2572 { 2573 int res; 2574 snd_pcm_stream_lock_irq(substream); 2575 res = snd_pcm_pause(substream, (int)(unsigned long)arg); 2576 snd_pcm_stream_unlock_irq(substream); 2577 return res; 2578 } 2579 } 2580 snd_printd("unknown ioctl = 0x%x\n", cmd); 2581 return -ENOTTY; 2582 } 2583 2584 static int snd_pcm_playback_ioctl1(struct file *file, 2585 struct snd_pcm_substream *substream, 2586 unsigned int cmd, void __user *arg) 2587 { 2588 snd_assert(substream != NULL, return -ENXIO); 2589 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL); 2590 switch (cmd) { 2591 case SNDRV_PCM_IOCTL_WRITEI_FRAMES: 2592 { 2593 struct snd_xferi xferi; 2594 struct snd_xferi __user *_xferi = arg; 2595 struct snd_pcm_runtime *runtime = substream->runtime; 2596 snd_pcm_sframes_t result; 2597 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 2598 return -EBADFD; 2599 if (put_user(0, &_xferi->result)) 2600 return -EFAULT; 2601 if (copy_from_user(&xferi, _xferi, sizeof(xferi))) 2602 return -EFAULT; 2603 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames); 2604 __put_user(result, &_xferi->result); 2605 return result < 0 ? result : 0; 2606 } 2607 case SNDRV_PCM_IOCTL_WRITEN_FRAMES: 2608 { 2609 struct snd_xfern xfern; 2610 struct snd_xfern __user *_xfern = arg; 2611 struct snd_pcm_runtime *runtime = substream->runtime; 2612 void __user **bufs; 2613 snd_pcm_sframes_t result; 2614 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 2615 return -EBADFD; 2616 if (runtime->channels > 128) 2617 return -EINVAL; 2618 if (put_user(0, &_xfern->result)) 2619 return -EFAULT; 2620 if (copy_from_user(&xfern, _xfern, sizeof(xfern))) 2621 return -EFAULT; 2622 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL); 2623 if (bufs == NULL) 2624 return -ENOMEM; 2625 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) { 2626 kfree(bufs); 2627 return -EFAULT; 2628 } 2629 result = snd_pcm_lib_writev(substream, bufs, xfern.frames); 2630 kfree(bufs); 2631 __put_user(result, &_xfern->result); 2632 return result < 0 ? result : 0; 2633 } 2634 case SNDRV_PCM_IOCTL_REWIND: 2635 { 2636 snd_pcm_uframes_t frames; 2637 snd_pcm_uframes_t __user *_frames = arg; 2638 snd_pcm_sframes_t result; 2639 if (get_user(frames, _frames)) 2640 return -EFAULT; 2641 if (put_user(0, _frames)) 2642 return -EFAULT; 2643 result = snd_pcm_playback_rewind(substream, frames); 2644 __put_user(result, _frames); 2645 return result < 0 ? result : 0; 2646 } 2647 case SNDRV_PCM_IOCTL_FORWARD: 2648 { 2649 snd_pcm_uframes_t frames; 2650 snd_pcm_uframes_t __user *_frames = arg; 2651 snd_pcm_sframes_t result; 2652 if (get_user(frames, _frames)) 2653 return -EFAULT; 2654 if (put_user(0, _frames)) 2655 return -EFAULT; 2656 result = snd_pcm_playback_forward(substream, frames); 2657 __put_user(result, _frames); 2658 return result < 0 ? result : 0; 2659 } 2660 } 2661 return snd_pcm_common_ioctl1(file, substream, cmd, arg); 2662 } 2663 2664 static int snd_pcm_capture_ioctl1(struct file *file, 2665 struct snd_pcm_substream *substream, 2666 unsigned int cmd, void __user *arg) 2667 { 2668 snd_assert(substream != NULL, return -ENXIO); 2669 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL); 2670 switch (cmd) { 2671 case SNDRV_PCM_IOCTL_READI_FRAMES: 2672 { 2673 struct snd_xferi xferi; 2674 struct snd_xferi __user *_xferi = arg; 2675 struct snd_pcm_runtime *runtime = substream->runtime; 2676 snd_pcm_sframes_t result; 2677 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 2678 return -EBADFD; 2679 if (put_user(0, &_xferi->result)) 2680 return -EFAULT; 2681 if (copy_from_user(&xferi, _xferi, sizeof(xferi))) 2682 return -EFAULT; 2683 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames); 2684 __put_user(result, &_xferi->result); 2685 return result < 0 ? result : 0; 2686 } 2687 case SNDRV_PCM_IOCTL_READN_FRAMES: 2688 { 2689 struct snd_xfern xfern; 2690 struct snd_xfern __user *_xfern = arg; 2691 struct snd_pcm_runtime *runtime = substream->runtime; 2692 void *bufs; 2693 snd_pcm_sframes_t result; 2694 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 2695 return -EBADFD; 2696 if (runtime->channels > 128) 2697 return -EINVAL; 2698 if (put_user(0, &_xfern->result)) 2699 return -EFAULT; 2700 if (copy_from_user(&xfern, _xfern, sizeof(xfern))) 2701 return -EFAULT; 2702 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL); 2703 if (bufs == NULL) 2704 return -ENOMEM; 2705 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) { 2706 kfree(bufs); 2707 return -EFAULT; 2708 } 2709 result = snd_pcm_lib_readv(substream, bufs, xfern.frames); 2710 kfree(bufs); 2711 __put_user(result, &_xfern->result); 2712 return result < 0 ? result : 0; 2713 } 2714 case SNDRV_PCM_IOCTL_REWIND: 2715 { 2716 snd_pcm_uframes_t frames; 2717 snd_pcm_uframes_t __user *_frames = arg; 2718 snd_pcm_sframes_t result; 2719 if (get_user(frames, _frames)) 2720 return -EFAULT; 2721 if (put_user(0, _frames)) 2722 return -EFAULT; 2723 result = snd_pcm_capture_rewind(substream, frames); 2724 __put_user(result, _frames); 2725 return result < 0 ? result : 0; 2726 } 2727 case SNDRV_PCM_IOCTL_FORWARD: 2728 { 2729 snd_pcm_uframes_t frames; 2730 snd_pcm_uframes_t __user *_frames = arg; 2731 snd_pcm_sframes_t result; 2732 if (get_user(frames, _frames)) 2733 return -EFAULT; 2734 if (put_user(0, _frames)) 2735 return -EFAULT; 2736 result = snd_pcm_capture_forward(substream, frames); 2737 __put_user(result, _frames); 2738 return result < 0 ? result : 0; 2739 } 2740 } 2741 return snd_pcm_common_ioctl1(file, substream, cmd, arg); 2742 } 2743 2744 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd, 2745 unsigned long arg) 2746 { 2747 struct snd_pcm_file *pcm_file; 2748 2749 pcm_file = file->private_data; 2750 2751 if (((cmd >> 8) & 0xff) != 'A') 2752 return -ENOTTY; 2753 2754 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd, 2755 (void __user *)arg); 2756 } 2757 2758 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd, 2759 unsigned long arg) 2760 { 2761 struct snd_pcm_file *pcm_file; 2762 2763 pcm_file = file->private_data; 2764 2765 if (((cmd >> 8) & 0xff) != 'A') 2766 return -ENOTTY; 2767 2768 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd, 2769 (void __user *)arg); 2770 } 2771 2772 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, 2773 unsigned int cmd, void *arg) 2774 { 2775 mm_segment_t fs; 2776 int result; 2777 2778 fs = snd_enter_user(); 2779 switch (substream->stream) { 2780 case SNDRV_PCM_STREAM_PLAYBACK: 2781 result = snd_pcm_playback_ioctl1(NULL, substream, cmd, 2782 (void __user *)arg); 2783 break; 2784 case SNDRV_PCM_STREAM_CAPTURE: 2785 result = snd_pcm_capture_ioctl1(NULL, substream, cmd, 2786 (void __user *)arg); 2787 break; 2788 default: 2789 result = -EINVAL; 2790 break; 2791 } 2792 snd_leave_user(fs); 2793 return result; 2794 } 2795 2796 EXPORT_SYMBOL(snd_pcm_kernel_ioctl); 2797 2798 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, 2799 loff_t * offset) 2800 { 2801 struct snd_pcm_file *pcm_file; 2802 struct snd_pcm_substream *substream; 2803 struct snd_pcm_runtime *runtime; 2804 snd_pcm_sframes_t result; 2805 2806 pcm_file = file->private_data; 2807 substream = pcm_file->substream; 2808 snd_assert(substream != NULL, return -ENXIO); 2809 runtime = substream->runtime; 2810 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 2811 return -EBADFD; 2812 if (!frame_aligned(runtime, count)) 2813 return -EINVAL; 2814 count = bytes_to_frames(runtime, count); 2815 result = snd_pcm_lib_read(substream, buf, count); 2816 if (result > 0) 2817 result = frames_to_bytes(runtime, result); 2818 return result; 2819 } 2820 2821 static ssize_t snd_pcm_write(struct file *file, const char __user *buf, 2822 size_t count, loff_t * offset) 2823 { 2824 struct snd_pcm_file *pcm_file; 2825 struct snd_pcm_substream *substream; 2826 struct snd_pcm_runtime *runtime; 2827 snd_pcm_sframes_t result; 2828 2829 pcm_file = file->private_data; 2830 substream = pcm_file->substream; 2831 snd_assert(substream != NULL, result = -ENXIO; goto end); 2832 runtime = substream->runtime; 2833 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { 2834 result = -EBADFD; 2835 goto end; 2836 } 2837 if (!frame_aligned(runtime, count)) { 2838 result = -EINVAL; 2839 goto end; 2840 } 2841 count = bytes_to_frames(runtime, count); 2842 result = snd_pcm_lib_write(substream, buf, count); 2843 if (result > 0) 2844 result = frames_to_bytes(runtime, result); 2845 end: 2846 return result; 2847 } 2848 2849 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov, 2850 unsigned long nr_segs, loff_t pos) 2851 2852 { 2853 struct snd_pcm_file *pcm_file; 2854 struct snd_pcm_substream *substream; 2855 struct snd_pcm_runtime *runtime; 2856 snd_pcm_sframes_t result; 2857 unsigned long i; 2858 void __user **bufs; 2859 snd_pcm_uframes_t frames; 2860 2861 pcm_file = iocb->ki_filp->private_data; 2862 substream = pcm_file->substream; 2863 snd_assert(substream != NULL, return -ENXIO); 2864 runtime = substream->runtime; 2865 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 2866 return -EBADFD; 2867 if (nr_segs > 1024 || nr_segs != runtime->channels) 2868 return -EINVAL; 2869 if (!frame_aligned(runtime, iov->iov_len)) 2870 return -EINVAL; 2871 frames = bytes_to_samples(runtime, iov->iov_len); 2872 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL); 2873 if (bufs == NULL) 2874 return -ENOMEM; 2875 for (i = 0; i < nr_segs; ++i) 2876 bufs[i] = iov[i].iov_base; 2877 result = snd_pcm_lib_readv(substream, bufs, frames); 2878 if (result > 0) 2879 result = frames_to_bytes(runtime, result); 2880 kfree(bufs); 2881 return result; 2882 } 2883 2884 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov, 2885 unsigned long nr_segs, loff_t pos) 2886 { 2887 struct snd_pcm_file *pcm_file; 2888 struct snd_pcm_substream *substream; 2889 struct snd_pcm_runtime *runtime; 2890 snd_pcm_sframes_t result; 2891 unsigned long i; 2892 void __user **bufs; 2893 snd_pcm_uframes_t frames; 2894 2895 pcm_file = iocb->ki_filp->private_data; 2896 substream = pcm_file->substream; 2897 snd_assert(substream != NULL, result = -ENXIO; goto end); 2898 runtime = substream->runtime; 2899 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { 2900 result = -EBADFD; 2901 goto end; 2902 } 2903 if (nr_segs > 128 || nr_segs != runtime->channels || 2904 !frame_aligned(runtime, iov->iov_len)) { 2905 result = -EINVAL; 2906 goto end; 2907 } 2908 frames = bytes_to_samples(runtime, iov->iov_len); 2909 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL); 2910 if (bufs == NULL) 2911 return -ENOMEM; 2912 for (i = 0; i < nr_segs; ++i) 2913 bufs[i] = iov[i].iov_base; 2914 result = snd_pcm_lib_writev(substream, bufs, frames); 2915 if (result > 0) 2916 result = frames_to_bytes(runtime, result); 2917 kfree(bufs); 2918 end: 2919 return result; 2920 } 2921 2922 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait) 2923 { 2924 struct snd_pcm_file *pcm_file; 2925 struct snd_pcm_substream *substream; 2926 struct snd_pcm_runtime *runtime; 2927 unsigned int mask; 2928 snd_pcm_uframes_t avail; 2929 2930 pcm_file = file->private_data; 2931 2932 substream = pcm_file->substream; 2933 snd_assert(substream != NULL, return -ENXIO); 2934 runtime = substream->runtime; 2935 2936 poll_wait(file, &runtime->sleep, wait); 2937 2938 snd_pcm_stream_lock_irq(substream); 2939 avail = snd_pcm_playback_avail(runtime); 2940 switch (runtime->status->state) { 2941 case SNDRV_PCM_STATE_RUNNING: 2942 case SNDRV_PCM_STATE_PREPARED: 2943 case SNDRV_PCM_STATE_PAUSED: 2944 if (avail >= runtime->control->avail_min) { 2945 mask = POLLOUT | POLLWRNORM; 2946 break; 2947 } 2948 /* Fall through */ 2949 case SNDRV_PCM_STATE_DRAINING: 2950 mask = 0; 2951 break; 2952 default: 2953 mask = POLLOUT | POLLWRNORM | POLLERR; 2954 break; 2955 } 2956 snd_pcm_stream_unlock_irq(substream); 2957 return mask; 2958 } 2959 2960 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait) 2961 { 2962 struct snd_pcm_file *pcm_file; 2963 struct snd_pcm_substream *substream; 2964 struct snd_pcm_runtime *runtime; 2965 unsigned int mask; 2966 snd_pcm_uframes_t avail; 2967 2968 pcm_file = file->private_data; 2969 2970 substream = pcm_file->substream; 2971 snd_assert(substream != NULL, return -ENXIO); 2972 runtime = substream->runtime; 2973 2974 poll_wait(file, &runtime->sleep, wait); 2975 2976 snd_pcm_stream_lock_irq(substream); 2977 avail = snd_pcm_capture_avail(runtime); 2978 switch (runtime->status->state) { 2979 case SNDRV_PCM_STATE_RUNNING: 2980 case SNDRV_PCM_STATE_PREPARED: 2981 case SNDRV_PCM_STATE_PAUSED: 2982 if (avail >= runtime->control->avail_min) { 2983 mask = POLLIN | POLLRDNORM; 2984 break; 2985 } 2986 mask = 0; 2987 break; 2988 case SNDRV_PCM_STATE_DRAINING: 2989 if (avail > 0) { 2990 mask = POLLIN | POLLRDNORM; 2991 break; 2992 } 2993 /* Fall through */ 2994 default: 2995 mask = POLLIN | POLLRDNORM | POLLERR; 2996 break; 2997 } 2998 snd_pcm_stream_unlock_irq(substream); 2999 return mask; 3000 } 3001 3002 /* 3003 * mmap support 3004 */ 3005 3006 /* 3007 * Only on coherent architectures, we can mmap the status and the control records 3008 * for effcient data transfer. On others, we have to use HWSYNC ioctl... 3009 */ 3010 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA) 3011 /* 3012 * mmap status record 3013 */ 3014 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area, 3015 unsigned long address, int *type) 3016 { 3017 struct snd_pcm_substream *substream = area->vm_private_data; 3018 struct snd_pcm_runtime *runtime; 3019 struct page * page; 3020 3021 if (substream == NULL) 3022 return NOPAGE_SIGBUS; 3023 runtime = substream->runtime; 3024 page = virt_to_page(runtime->status); 3025 get_page(page); 3026 if (type) 3027 *type = VM_FAULT_MINOR; 3028 return page; 3029 } 3030 3031 static struct vm_operations_struct snd_pcm_vm_ops_status = 3032 { 3033 .nopage = snd_pcm_mmap_status_nopage, 3034 }; 3035 3036 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, 3037 struct vm_area_struct *area) 3038 { 3039 struct snd_pcm_runtime *runtime; 3040 long size; 3041 if (!(area->vm_flags & VM_READ)) 3042 return -EINVAL; 3043 runtime = substream->runtime; 3044 snd_assert(runtime != NULL, return -EAGAIN); 3045 size = area->vm_end - area->vm_start; 3046 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))) 3047 return -EINVAL; 3048 area->vm_ops = &snd_pcm_vm_ops_status; 3049 area->vm_private_data = substream; 3050 area->vm_flags |= VM_RESERVED; 3051 return 0; 3052 } 3053 3054 /* 3055 * mmap control record 3056 */ 3057 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area, 3058 unsigned long address, int *type) 3059 { 3060 struct snd_pcm_substream *substream = area->vm_private_data; 3061 struct snd_pcm_runtime *runtime; 3062 struct page * page; 3063 3064 if (substream == NULL) 3065 return NOPAGE_SIGBUS; 3066 runtime = substream->runtime; 3067 page = virt_to_page(runtime->control); 3068 get_page(page); 3069 if (type) 3070 *type = VM_FAULT_MINOR; 3071 return page; 3072 } 3073 3074 static struct vm_operations_struct snd_pcm_vm_ops_control = 3075 { 3076 .nopage = snd_pcm_mmap_control_nopage, 3077 }; 3078 3079 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, 3080 struct vm_area_struct *area) 3081 { 3082 struct snd_pcm_runtime *runtime; 3083 long size; 3084 if (!(area->vm_flags & VM_READ)) 3085 return -EINVAL; 3086 runtime = substream->runtime; 3087 snd_assert(runtime != NULL, return -EAGAIN); 3088 size = area->vm_end - area->vm_start; 3089 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control))) 3090 return -EINVAL; 3091 area->vm_ops = &snd_pcm_vm_ops_control; 3092 area->vm_private_data = substream; 3093 area->vm_flags |= VM_RESERVED; 3094 return 0; 3095 } 3096 #else /* ! coherent mmap */ 3097 /* 3098 * don't support mmap for status and control records. 3099 */ 3100 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, 3101 struct vm_area_struct *area) 3102 { 3103 return -ENXIO; 3104 } 3105 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, 3106 struct vm_area_struct *area) 3107 { 3108 return -ENXIO; 3109 } 3110 #endif /* coherent mmap */ 3111 3112 /* 3113 * nopage callback for mmapping a RAM page 3114 */ 3115 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area, 3116 unsigned long address, int *type) 3117 { 3118 struct snd_pcm_substream *substream = area->vm_private_data; 3119 struct snd_pcm_runtime *runtime; 3120 unsigned long offset; 3121 struct page * page; 3122 void *vaddr; 3123 size_t dma_bytes; 3124 3125 if (substream == NULL) 3126 return NOPAGE_SIGBUS; 3127 runtime = substream->runtime; 3128 offset = area->vm_pgoff << PAGE_SHIFT; 3129 offset += address - area->vm_start; 3130 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_SIGBUS); 3131 dma_bytes = PAGE_ALIGN(runtime->dma_bytes); 3132 if (offset > dma_bytes - PAGE_SIZE) 3133 return NOPAGE_SIGBUS; 3134 if (substream->ops->page) { 3135 page = substream->ops->page(substream, offset); 3136 if (! page) 3137 return NOPAGE_OOM; /* XXX: is this really due to OOM? */ 3138 } else { 3139 vaddr = runtime->dma_area + offset; 3140 page = virt_to_page(vaddr); 3141 } 3142 get_page(page); 3143 if (type) 3144 *type = VM_FAULT_MINOR; 3145 return page; 3146 } 3147 3148 static struct vm_operations_struct snd_pcm_vm_ops_data = 3149 { 3150 .open = snd_pcm_mmap_data_open, 3151 .close = snd_pcm_mmap_data_close, 3152 .nopage = snd_pcm_mmap_data_nopage, 3153 }; 3154 3155 /* 3156 * mmap the DMA buffer on RAM 3157 */ 3158 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream, 3159 struct vm_area_struct *area) 3160 { 3161 area->vm_ops = &snd_pcm_vm_ops_data; 3162 area->vm_private_data = substream; 3163 area->vm_flags |= VM_RESERVED; 3164 atomic_inc(&substream->mmap_count); 3165 return 0; 3166 } 3167 3168 /* 3169 * mmap the DMA buffer on I/O memory area 3170 */ 3171 #if SNDRV_PCM_INFO_MMAP_IOMEM 3172 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio = 3173 { 3174 .open = snd_pcm_mmap_data_open, 3175 .close = snd_pcm_mmap_data_close, 3176 }; 3177 3178 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, 3179 struct vm_area_struct *area) 3180 { 3181 long size; 3182 unsigned long offset; 3183 3184 #ifdef pgprot_noncached 3185 area->vm_page_prot = pgprot_noncached(area->vm_page_prot); 3186 #endif 3187 area->vm_ops = &snd_pcm_vm_ops_data_mmio; 3188 area->vm_private_data = substream; 3189 area->vm_flags |= VM_IO; 3190 size = area->vm_end - area->vm_start; 3191 offset = area->vm_pgoff << PAGE_SHIFT; 3192 if (io_remap_pfn_range(area, area->vm_start, 3193 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT, 3194 size, area->vm_page_prot)) 3195 return -EAGAIN; 3196 atomic_inc(&substream->mmap_count); 3197 return 0; 3198 } 3199 3200 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem); 3201 #endif /* SNDRV_PCM_INFO_MMAP */ 3202 3203 /* 3204 * mmap DMA buffer 3205 */ 3206 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, 3207 struct vm_area_struct *area) 3208 { 3209 struct snd_pcm_runtime *runtime; 3210 long size; 3211 unsigned long offset; 3212 size_t dma_bytes; 3213 3214 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 3215 if (!(area->vm_flags & (VM_WRITE|VM_READ))) 3216 return -EINVAL; 3217 } else { 3218 if (!(area->vm_flags & VM_READ)) 3219 return -EINVAL; 3220 } 3221 runtime = substream->runtime; 3222 snd_assert(runtime != NULL, return -EAGAIN); 3223 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 3224 return -EBADFD; 3225 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) 3226 return -ENXIO; 3227 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || 3228 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) 3229 return -EINVAL; 3230 size = area->vm_end - area->vm_start; 3231 offset = area->vm_pgoff << PAGE_SHIFT; 3232 dma_bytes = PAGE_ALIGN(runtime->dma_bytes); 3233 if ((size_t)size > dma_bytes) 3234 return -EINVAL; 3235 if (offset > dma_bytes - size) 3236 return -EINVAL; 3237 3238 if (substream->ops->mmap) 3239 return substream->ops->mmap(substream, area); 3240 else 3241 return snd_pcm_default_mmap(substream, area); 3242 } 3243 3244 EXPORT_SYMBOL(snd_pcm_mmap_data); 3245 3246 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area) 3247 { 3248 struct snd_pcm_file * pcm_file; 3249 struct snd_pcm_substream *substream; 3250 unsigned long offset; 3251 3252 pcm_file = file->private_data; 3253 substream = pcm_file->substream; 3254 snd_assert(substream != NULL, return -ENXIO); 3255 3256 offset = area->vm_pgoff << PAGE_SHIFT; 3257 switch (offset) { 3258 case SNDRV_PCM_MMAP_OFFSET_STATUS: 3259 if (pcm_file->no_compat_mmap) 3260 return -ENXIO; 3261 return snd_pcm_mmap_status(substream, file, area); 3262 case SNDRV_PCM_MMAP_OFFSET_CONTROL: 3263 if (pcm_file->no_compat_mmap) 3264 return -ENXIO; 3265 return snd_pcm_mmap_control(substream, file, area); 3266 default: 3267 return snd_pcm_mmap_data(substream, file, area); 3268 } 3269 return 0; 3270 } 3271 3272 static int snd_pcm_fasync(int fd, struct file * file, int on) 3273 { 3274 struct snd_pcm_file * pcm_file; 3275 struct snd_pcm_substream *substream; 3276 struct snd_pcm_runtime *runtime; 3277 int err; 3278 3279 pcm_file = file->private_data; 3280 substream = pcm_file->substream; 3281 snd_assert(substream != NULL, return -ENXIO); 3282 runtime = substream->runtime; 3283 3284 err = fasync_helper(fd, file, on, &runtime->fasync); 3285 if (err < 0) 3286 return err; 3287 return 0; 3288 } 3289 3290 /* 3291 * ioctl32 compat 3292 */ 3293 #ifdef CONFIG_COMPAT 3294 #include "pcm_compat.c" 3295 #else 3296 #define snd_pcm_ioctl_compat NULL 3297 #endif 3298 3299 /* 3300 * To be removed helpers to keep binary compatibility 3301 */ 3302 3303 #ifdef CONFIG_SND_SUPPORT_OLD_API 3304 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5)) 3305 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5)) 3306 3307 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params, 3308 struct snd_pcm_hw_params_old *oparams) 3309 { 3310 unsigned int i; 3311 3312 memset(params, 0, sizeof(*params)); 3313 params->flags = oparams->flags; 3314 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) 3315 params->masks[i].bits[0] = oparams->masks[i]; 3316 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals)); 3317 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask); 3318 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask); 3319 params->info = oparams->info; 3320 params->msbits = oparams->msbits; 3321 params->rate_num = oparams->rate_num; 3322 params->rate_den = oparams->rate_den; 3323 params->fifo_size = oparams->fifo_size; 3324 } 3325 3326 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams, 3327 struct snd_pcm_hw_params *params) 3328 { 3329 unsigned int i; 3330 3331 memset(oparams, 0, sizeof(*oparams)); 3332 oparams->flags = params->flags; 3333 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) 3334 oparams->masks[i] = params->masks[i].bits[0]; 3335 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals)); 3336 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask); 3337 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask); 3338 oparams->info = params->info; 3339 oparams->msbits = params->msbits; 3340 oparams->rate_num = params->rate_num; 3341 oparams->rate_den = params->rate_den; 3342 oparams->fifo_size = params->fifo_size; 3343 } 3344 3345 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, 3346 struct snd_pcm_hw_params_old __user * _oparams) 3347 { 3348 struct snd_pcm_hw_params *params; 3349 struct snd_pcm_hw_params_old *oparams = NULL; 3350 int err; 3351 3352 params = kmalloc(sizeof(*params), GFP_KERNEL); 3353 if (!params) { 3354 err = -ENOMEM; 3355 goto out; 3356 } 3357 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL); 3358 if (!oparams) { 3359 err = -ENOMEM; 3360 goto out; 3361 } 3362 3363 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) { 3364 err = -EFAULT; 3365 goto out; 3366 } 3367 snd_pcm_hw_convert_from_old_params(params, oparams); 3368 err = snd_pcm_hw_refine(substream, params); 3369 snd_pcm_hw_convert_to_old_params(oparams, params); 3370 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) { 3371 if (!err) 3372 err = -EFAULT; 3373 } 3374 out: 3375 kfree(params); 3376 kfree(oparams); 3377 return err; 3378 } 3379 3380 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, 3381 struct snd_pcm_hw_params_old __user * _oparams) 3382 { 3383 struct snd_pcm_hw_params *params; 3384 struct snd_pcm_hw_params_old *oparams = NULL; 3385 int err; 3386 3387 params = kmalloc(sizeof(*params), GFP_KERNEL); 3388 if (!params) { 3389 err = -ENOMEM; 3390 goto out; 3391 } 3392 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL); 3393 if (!oparams) { 3394 err = -ENOMEM; 3395 goto out; 3396 } 3397 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) { 3398 err = -EFAULT; 3399 goto out; 3400 } 3401 snd_pcm_hw_convert_from_old_params(params, oparams); 3402 err = snd_pcm_hw_params(substream, params); 3403 snd_pcm_hw_convert_to_old_params(oparams, params); 3404 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) { 3405 if (!err) 3406 err = -EFAULT; 3407 } 3408 out: 3409 kfree(params); 3410 kfree(oparams); 3411 return err; 3412 } 3413 #endif /* CONFIG_SND_SUPPORT_OLD_API */ 3414 3415 /* 3416 * Register section 3417 */ 3418 3419 const struct file_operations snd_pcm_f_ops[2] = { 3420 { 3421 .owner = THIS_MODULE, 3422 .write = snd_pcm_write, 3423 .aio_write = snd_pcm_aio_write, 3424 .open = snd_pcm_playback_open, 3425 .release = snd_pcm_release, 3426 .poll = snd_pcm_playback_poll, 3427 .unlocked_ioctl = snd_pcm_playback_ioctl, 3428 .compat_ioctl = snd_pcm_ioctl_compat, 3429 .mmap = snd_pcm_mmap, 3430 .fasync = snd_pcm_fasync, 3431 }, 3432 { 3433 .owner = THIS_MODULE, 3434 .read = snd_pcm_read, 3435 .aio_read = snd_pcm_aio_read, 3436 .open = snd_pcm_capture_open, 3437 .release = snd_pcm_release, 3438 .poll = snd_pcm_capture_poll, 3439 .unlocked_ioctl = snd_pcm_capture_ioctl, 3440 .compat_ioctl = snd_pcm_ioctl_compat, 3441 .mmap = snd_pcm_mmap, 3442 .fasync = snd_pcm_fasync, 3443 } 3444 }; 3445