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