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