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