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