1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Digital Audio (PCM) abstract layer 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 5 */ 6 7 #include <linux/compat.h> 8 #include <linux/mm.h> 9 #include <linux/module.h> 10 #include <linux/file.h> 11 #include <linux/slab.h> 12 #include <linux/sched/signal.h> 13 #include <linux/time.h> 14 #include <linux/pm_qos.h> 15 #include <linux/io.h> 16 #include <linux/dma-mapping.h> 17 #include <linux/vmalloc.h> 18 #include <sound/core.h> 19 #include <sound/control.h> 20 #include <sound/info.h> 21 #include <sound/pcm.h> 22 #include <sound/pcm_params.h> 23 #include <sound/timer.h> 24 #include <sound/minors.h> 25 #include <linux/uio.h> 26 #include <linux/delay.h> 27 28 #include "pcm_local.h" 29 30 #ifdef CONFIG_SND_DEBUG 31 #define CREATE_TRACE_POINTS 32 #include "pcm_param_trace.h" 33 #else 34 #define trace_hw_mask_param_enabled() 0 35 #define trace_hw_interval_param_enabled() 0 36 #define trace_hw_mask_param(substream, type, index, prev, curr) 37 #define trace_hw_interval_param(substream, type, index, prev, curr) 38 #endif 39 40 /* 41 * Compatibility 42 */ 43 44 struct snd_pcm_hw_params_old { 45 unsigned int flags; 46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT - 47 SNDRV_PCM_HW_PARAM_ACCESS + 1]; 48 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME - 49 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1]; 50 unsigned int rmask; 51 unsigned int cmask; 52 unsigned int info; 53 unsigned int msbits; 54 unsigned int rate_num; 55 unsigned int rate_den; 56 snd_pcm_uframes_t fifo_size; 57 unsigned char reserved[64]; 58 }; 59 60 #ifdef CONFIG_SND_SUPPORT_OLD_API 61 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old) 62 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old) 63 64 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, 65 struct snd_pcm_hw_params_old __user * _oparams); 66 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, 67 struct snd_pcm_hw_params_old __user * _oparams); 68 #endif 69 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream); 70 71 /* 72 * 73 */ 74 75 static DECLARE_RWSEM(snd_pcm_link_rwsem); 76 77 void snd_pcm_group_init(struct snd_pcm_group *group) 78 { 79 spin_lock_init(&group->lock); 80 mutex_init(&group->mutex); 81 INIT_LIST_HEAD(&group->substreams); 82 refcount_set(&group->refs, 1); 83 } 84 85 /* define group lock helpers */ 86 #define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \ 87 static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \ 88 { \ 89 if (nonatomic) \ 90 mutex_ ## mutex_action(&group->mutex); \ 91 else \ 92 spin_ ## action(&group->lock); \ 93 } 94 95 DEFINE_PCM_GROUP_LOCK(lock, lock); 96 DEFINE_PCM_GROUP_LOCK(unlock, unlock); 97 DEFINE_PCM_GROUP_LOCK(lock_irq, lock); 98 DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock); 99 100 /** 101 * snd_pcm_stream_lock - Lock the PCM stream 102 * @substream: PCM substream 103 * 104 * This locks the PCM stream's spinlock or mutex depending on the nonatomic 105 * flag of the given substream. This also takes the global link rw lock 106 * (or rw sem), too, for avoiding the race with linked streams. 107 */ 108 void snd_pcm_stream_lock(struct snd_pcm_substream *substream) 109 { 110 snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic); 111 } 112 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock); 113 114 /** 115 * snd_pcm_stream_lock - Unlock the PCM stream 116 * @substream: PCM substream 117 * 118 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock(). 119 */ 120 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream) 121 { 122 snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic); 123 } 124 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock); 125 126 /** 127 * snd_pcm_stream_lock_irq - Lock the PCM stream 128 * @substream: PCM substream 129 * 130 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local 131 * IRQ (only when nonatomic is false). In nonatomic case, this is identical 132 * as snd_pcm_stream_lock(). 133 */ 134 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream) 135 { 136 snd_pcm_group_lock_irq(&substream->self_group, 137 substream->pcm->nonatomic); 138 } 139 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq); 140 141 /** 142 * snd_pcm_stream_unlock_irq - Unlock the PCM stream 143 * @substream: PCM substream 144 * 145 * This is a counter-part of snd_pcm_stream_lock_irq(). 146 */ 147 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream) 148 { 149 snd_pcm_group_unlock_irq(&substream->self_group, 150 substream->pcm->nonatomic); 151 } 152 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq); 153 154 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream) 155 { 156 unsigned long flags = 0; 157 if (substream->pcm->nonatomic) 158 mutex_lock(&substream->self_group.mutex); 159 else 160 spin_lock_irqsave(&substream->self_group.lock, flags); 161 return flags; 162 } 163 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave); 164 165 /** 166 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream 167 * @substream: PCM substream 168 * @flags: irq flags 169 * 170 * This is a counter-part of snd_pcm_stream_lock_irqsave(). 171 */ 172 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream, 173 unsigned long flags) 174 { 175 if (substream->pcm->nonatomic) 176 mutex_unlock(&substream->self_group.mutex); 177 else 178 spin_unlock_irqrestore(&substream->self_group.lock, flags); 179 } 180 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore); 181 182 /* Run PCM ioctl ops */ 183 static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream, 184 unsigned cmd, void *arg) 185 { 186 if (substream->ops->ioctl) 187 return substream->ops->ioctl(substream, cmd, arg); 188 else 189 return snd_pcm_lib_ioctl(substream, cmd, arg); 190 } 191 192 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info) 193 { 194 struct snd_pcm *pcm = substream->pcm; 195 struct snd_pcm_str *pstr = substream->pstr; 196 197 memset(info, 0, sizeof(*info)); 198 info->card = pcm->card->number; 199 info->device = pcm->device; 200 info->stream = substream->stream; 201 info->subdevice = substream->number; 202 strlcpy(info->id, pcm->id, sizeof(info->id)); 203 strlcpy(info->name, pcm->name, sizeof(info->name)); 204 info->dev_class = pcm->dev_class; 205 info->dev_subclass = pcm->dev_subclass; 206 info->subdevices_count = pstr->substream_count; 207 info->subdevices_avail = pstr->substream_count - pstr->substream_opened; 208 strlcpy(info->subname, substream->name, sizeof(info->subname)); 209 210 return 0; 211 } 212 213 int snd_pcm_info_user(struct snd_pcm_substream *substream, 214 struct snd_pcm_info __user * _info) 215 { 216 struct snd_pcm_info *info; 217 int err; 218 219 info = kmalloc(sizeof(*info), GFP_KERNEL); 220 if (! info) 221 return -ENOMEM; 222 err = snd_pcm_info(substream, info); 223 if (err >= 0) { 224 if (copy_to_user(_info, info, sizeof(*info))) 225 err = -EFAULT; 226 } 227 kfree(info); 228 return err; 229 } 230 231 static bool hw_support_mmap(struct snd_pcm_substream *substream) 232 { 233 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP)) 234 return false; 235 236 if (substream->ops->mmap || 237 (substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV && 238 substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV_UC)) 239 return true; 240 241 return dma_can_mmap(substream->dma_buffer.dev.dev); 242 } 243 244 static int constrain_mask_params(struct snd_pcm_substream *substream, 245 struct snd_pcm_hw_params *params) 246 { 247 struct snd_pcm_hw_constraints *constrs = 248 &substream->runtime->hw_constraints; 249 struct snd_mask *m; 250 unsigned int k; 251 struct snd_mask old_mask; 252 int changed; 253 254 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { 255 m = hw_param_mask(params, k); 256 if (snd_mask_empty(m)) 257 return -EINVAL; 258 259 /* This parameter is not requested to change by a caller. */ 260 if (!(params->rmask & (1 << k))) 261 continue; 262 263 if (trace_hw_mask_param_enabled()) 264 old_mask = *m; 265 266 changed = snd_mask_refine(m, constrs_mask(constrs, k)); 267 if (changed < 0) 268 return changed; 269 if (changed == 0) 270 continue; 271 272 /* Set corresponding flag so that the caller gets it. */ 273 trace_hw_mask_param(substream, k, 0, &old_mask, m); 274 params->cmask |= 1 << k; 275 } 276 277 return 0; 278 } 279 280 static int constrain_interval_params(struct snd_pcm_substream *substream, 281 struct snd_pcm_hw_params *params) 282 { 283 struct snd_pcm_hw_constraints *constrs = 284 &substream->runtime->hw_constraints; 285 struct snd_interval *i; 286 unsigned int k; 287 struct snd_interval old_interval; 288 int changed; 289 290 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { 291 i = hw_param_interval(params, k); 292 if (snd_interval_empty(i)) 293 return -EINVAL; 294 295 /* This parameter is not requested to change by a caller. */ 296 if (!(params->rmask & (1 << k))) 297 continue; 298 299 if (trace_hw_interval_param_enabled()) 300 old_interval = *i; 301 302 changed = snd_interval_refine(i, constrs_interval(constrs, k)); 303 if (changed < 0) 304 return changed; 305 if (changed == 0) 306 continue; 307 308 /* Set corresponding flag so that the caller gets it. */ 309 trace_hw_interval_param(substream, k, 0, &old_interval, i); 310 params->cmask |= 1 << k; 311 } 312 313 return 0; 314 } 315 316 static int constrain_params_by_rules(struct snd_pcm_substream *substream, 317 struct snd_pcm_hw_params *params) 318 { 319 struct snd_pcm_hw_constraints *constrs = 320 &substream->runtime->hw_constraints; 321 unsigned int k; 322 unsigned int *rstamps; 323 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1]; 324 unsigned int stamp; 325 struct snd_pcm_hw_rule *r; 326 unsigned int d; 327 struct snd_mask old_mask; 328 struct snd_interval old_interval; 329 bool again; 330 int changed, err = 0; 331 332 /* 333 * Each application of rule has own sequence number. 334 * 335 * Each member of 'rstamps' array represents the sequence number of 336 * recent application of corresponding rule. 337 */ 338 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL); 339 if (!rstamps) 340 return -ENOMEM; 341 342 /* 343 * Each member of 'vstamps' array represents the sequence number of 344 * recent application of rule in which corresponding parameters were 345 * changed. 346 * 347 * In initial state, elements corresponding to parameters requested by 348 * a caller is 1. For unrequested parameters, corresponding members 349 * have 0 so that the parameters are never changed anymore. 350 */ 351 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) 352 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0; 353 354 /* Due to the above design, actual sequence number starts at 2. */ 355 stamp = 2; 356 retry: 357 /* Apply all rules in order. */ 358 again = false; 359 for (k = 0; k < constrs->rules_num; k++) { 360 r = &constrs->rules[k]; 361 362 /* 363 * Check condition bits of this rule. When the rule has 364 * some condition bits, parameter without the bits is 365 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP 366 * is an example of the condition bits. 367 */ 368 if (r->cond && !(r->cond & params->flags)) 369 continue; 370 371 /* 372 * The 'deps' array includes maximum three dependencies 373 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth 374 * member of this array is a sentinel and should be 375 * negative value. 376 * 377 * This rule should be processed in this time when dependent 378 * parameters were changed at former applications of the other 379 * rules. 380 */ 381 for (d = 0; r->deps[d] >= 0; d++) { 382 if (vstamps[r->deps[d]] > rstamps[k]) 383 break; 384 } 385 if (r->deps[d] < 0) 386 continue; 387 388 if (trace_hw_mask_param_enabled()) { 389 if (hw_is_mask(r->var)) 390 old_mask = *hw_param_mask(params, r->var); 391 } 392 if (trace_hw_interval_param_enabled()) { 393 if (hw_is_interval(r->var)) 394 old_interval = *hw_param_interval(params, r->var); 395 } 396 397 changed = r->func(params, r); 398 if (changed < 0) { 399 err = changed; 400 goto out; 401 } 402 403 /* 404 * When the parameter is changed, notify it to the caller 405 * by corresponding returned bit, then preparing for next 406 * iteration. 407 */ 408 if (changed && r->var >= 0) { 409 if (hw_is_mask(r->var)) { 410 trace_hw_mask_param(substream, r->var, 411 k + 1, &old_mask, 412 hw_param_mask(params, r->var)); 413 } 414 if (hw_is_interval(r->var)) { 415 trace_hw_interval_param(substream, r->var, 416 k + 1, &old_interval, 417 hw_param_interval(params, r->var)); 418 } 419 420 params->cmask |= (1 << r->var); 421 vstamps[r->var] = stamp; 422 again = true; 423 } 424 425 rstamps[k] = stamp++; 426 } 427 428 /* Iterate to evaluate all rules till no parameters are changed. */ 429 if (again) 430 goto retry; 431 432 out: 433 kfree(rstamps); 434 return err; 435 } 436 437 static int fixup_unreferenced_params(struct snd_pcm_substream *substream, 438 struct snd_pcm_hw_params *params) 439 { 440 const struct snd_interval *i; 441 const struct snd_mask *m; 442 int err; 443 444 if (!params->msbits) { 445 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); 446 if (snd_interval_single(i)) 447 params->msbits = snd_interval_value(i); 448 } 449 450 if (!params->rate_den) { 451 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); 452 if (snd_interval_single(i)) { 453 params->rate_num = snd_interval_value(i); 454 params->rate_den = 1; 455 } 456 } 457 458 if (!params->fifo_size) { 459 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT); 460 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS); 461 if (snd_mask_single(m) && snd_interval_single(i)) { 462 err = snd_pcm_ops_ioctl(substream, 463 SNDRV_PCM_IOCTL1_FIFO_SIZE, 464 params); 465 if (err < 0) 466 return err; 467 } 468 } 469 470 if (!params->info) { 471 params->info = substream->runtime->hw.info; 472 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES | 473 SNDRV_PCM_INFO_DRAIN_TRIGGER); 474 if (!hw_support_mmap(substream)) 475 params->info &= ~(SNDRV_PCM_INFO_MMAP | 476 SNDRV_PCM_INFO_MMAP_VALID); 477 } 478 479 return 0; 480 } 481 482 int snd_pcm_hw_refine(struct snd_pcm_substream *substream, 483 struct snd_pcm_hw_params *params) 484 { 485 int err; 486 487 params->info = 0; 488 params->fifo_size = 0; 489 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS)) 490 params->msbits = 0; 491 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) { 492 params->rate_num = 0; 493 params->rate_den = 0; 494 } 495 496 err = constrain_mask_params(substream, params); 497 if (err < 0) 498 return err; 499 500 err = constrain_interval_params(substream, params); 501 if (err < 0) 502 return err; 503 504 err = constrain_params_by_rules(substream, params); 505 if (err < 0) 506 return err; 507 508 params->rmask = 0; 509 510 return 0; 511 } 512 EXPORT_SYMBOL(snd_pcm_hw_refine); 513 514 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream, 515 struct snd_pcm_hw_params __user * _params) 516 { 517 struct snd_pcm_hw_params *params; 518 int err; 519 520 params = memdup_user(_params, sizeof(*params)); 521 if (IS_ERR(params)) 522 return PTR_ERR(params); 523 524 err = snd_pcm_hw_refine(substream, params); 525 if (err < 0) 526 goto end; 527 528 err = fixup_unreferenced_params(substream, params); 529 if (err < 0) 530 goto end; 531 532 if (copy_to_user(_params, params, sizeof(*params))) 533 err = -EFAULT; 534 end: 535 kfree(params); 536 return err; 537 } 538 539 static int period_to_usecs(struct snd_pcm_runtime *runtime) 540 { 541 int usecs; 542 543 if (! runtime->rate) 544 return -1; /* invalid */ 545 546 /* take 75% of period time as the deadline */ 547 usecs = (750000 / runtime->rate) * runtime->period_size; 548 usecs += ((750000 % runtime->rate) * runtime->period_size) / 549 runtime->rate; 550 551 return usecs; 552 } 553 554 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state) 555 { 556 snd_pcm_stream_lock_irq(substream); 557 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED) 558 substream->runtime->status->state = state; 559 snd_pcm_stream_unlock_irq(substream); 560 } 561 562 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream, 563 int event) 564 { 565 #ifdef CONFIG_SND_PCM_TIMER 566 if (substream->timer) 567 snd_timer_notify(substream->timer, event, 568 &substream->runtime->trigger_tstamp); 569 #endif 570 } 571 572 static void snd_pcm_sync_stop(struct snd_pcm_substream *substream) 573 { 574 if (substream->runtime->stop_operating) { 575 substream->runtime->stop_operating = false; 576 if (substream->ops->sync_stop) 577 substream->ops->sync_stop(substream); 578 else if (substream->pcm->card->sync_irq > 0) 579 synchronize_irq(substream->pcm->card->sync_irq); 580 } 581 } 582 583 /** 584 * snd_pcm_hw_param_choose - choose a configuration defined by @params 585 * @pcm: PCM instance 586 * @params: the hw_params instance 587 * 588 * Choose one configuration from configuration space defined by @params. 589 * The configuration chosen is that obtained fixing in this order: 590 * first access, first format, first subformat, min channels, 591 * min rate, min period time, max buffer size, min tick time 592 * 593 * Return: Zero if successful, or a negative error code on failure. 594 */ 595 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, 596 struct snd_pcm_hw_params *params) 597 { 598 static const int vars[] = { 599 SNDRV_PCM_HW_PARAM_ACCESS, 600 SNDRV_PCM_HW_PARAM_FORMAT, 601 SNDRV_PCM_HW_PARAM_SUBFORMAT, 602 SNDRV_PCM_HW_PARAM_CHANNELS, 603 SNDRV_PCM_HW_PARAM_RATE, 604 SNDRV_PCM_HW_PARAM_PERIOD_TIME, 605 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 606 SNDRV_PCM_HW_PARAM_TICK_TIME, 607 -1 608 }; 609 const int *v; 610 struct snd_mask old_mask; 611 struct snd_interval old_interval; 612 int changed; 613 614 for (v = vars; *v != -1; v++) { 615 /* Keep old parameter to trace. */ 616 if (trace_hw_mask_param_enabled()) { 617 if (hw_is_mask(*v)) 618 old_mask = *hw_param_mask(params, *v); 619 } 620 if (trace_hw_interval_param_enabled()) { 621 if (hw_is_interval(*v)) 622 old_interval = *hw_param_interval(params, *v); 623 } 624 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE) 625 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL); 626 else 627 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL); 628 if (changed < 0) 629 return changed; 630 if (changed == 0) 631 continue; 632 633 /* Trace the changed parameter. */ 634 if (hw_is_mask(*v)) { 635 trace_hw_mask_param(pcm, *v, 0, &old_mask, 636 hw_param_mask(params, *v)); 637 } 638 if (hw_is_interval(*v)) { 639 trace_hw_interval_param(pcm, *v, 0, &old_interval, 640 hw_param_interval(params, *v)); 641 } 642 } 643 644 return 0; 645 } 646 647 static int snd_pcm_hw_params(struct snd_pcm_substream *substream, 648 struct snd_pcm_hw_params *params) 649 { 650 struct snd_pcm_runtime *runtime; 651 int err, usecs; 652 unsigned int bits; 653 snd_pcm_uframes_t frames; 654 655 if (PCM_RUNTIME_CHECK(substream)) 656 return -ENXIO; 657 runtime = substream->runtime; 658 snd_pcm_stream_lock_irq(substream); 659 switch (runtime->status->state) { 660 case SNDRV_PCM_STATE_OPEN: 661 case SNDRV_PCM_STATE_SETUP: 662 case SNDRV_PCM_STATE_PREPARED: 663 break; 664 default: 665 snd_pcm_stream_unlock_irq(substream); 666 return -EBADFD; 667 } 668 snd_pcm_stream_unlock_irq(substream); 669 #if IS_ENABLED(CONFIG_SND_PCM_OSS) 670 if (!substream->oss.oss) 671 #endif 672 if (atomic_read(&substream->mmap_count)) 673 return -EBADFD; 674 675 snd_pcm_sync_stop(substream); 676 677 params->rmask = ~0U; 678 err = snd_pcm_hw_refine(substream, params); 679 if (err < 0) 680 goto _error; 681 682 err = snd_pcm_hw_params_choose(substream, params); 683 if (err < 0) 684 goto _error; 685 686 err = fixup_unreferenced_params(substream, params); 687 if (err < 0) 688 goto _error; 689 690 if (substream->managed_buffer_alloc) { 691 err = snd_pcm_lib_malloc_pages(substream, 692 params_buffer_bytes(params)); 693 if (err < 0) 694 goto _error; 695 runtime->buffer_changed = err > 0; 696 } 697 698 if (substream->ops->hw_params != NULL) { 699 err = substream->ops->hw_params(substream, params); 700 if (err < 0) 701 goto _error; 702 } 703 704 runtime->access = params_access(params); 705 runtime->format = params_format(params); 706 runtime->subformat = params_subformat(params); 707 runtime->channels = params_channels(params); 708 runtime->rate = params_rate(params); 709 runtime->period_size = params_period_size(params); 710 runtime->periods = params_periods(params); 711 runtime->buffer_size = params_buffer_size(params); 712 runtime->info = params->info; 713 runtime->rate_num = params->rate_num; 714 runtime->rate_den = params->rate_den; 715 runtime->no_period_wakeup = 716 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) && 717 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP); 718 719 bits = snd_pcm_format_physical_width(runtime->format); 720 runtime->sample_bits = bits; 721 bits *= runtime->channels; 722 runtime->frame_bits = bits; 723 frames = 1; 724 while (bits % 8 != 0) { 725 bits *= 2; 726 frames *= 2; 727 } 728 runtime->byte_align = bits / 8; 729 runtime->min_align = frames; 730 731 /* Default sw params */ 732 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; 733 runtime->period_step = 1; 734 runtime->control->avail_min = runtime->period_size; 735 runtime->start_threshold = 1; 736 runtime->stop_threshold = runtime->buffer_size; 737 runtime->silence_threshold = 0; 738 runtime->silence_size = 0; 739 runtime->boundary = runtime->buffer_size; 740 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size) 741 runtime->boundary *= 2; 742 743 /* clear the buffer for avoiding possible kernel info leaks */ 744 if (runtime->dma_area && !substream->ops->copy_user) 745 memset(runtime->dma_area, 0, runtime->dma_bytes); 746 747 snd_pcm_timer_resolution_change(substream); 748 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP); 749 750 if (pm_qos_request_active(&substream->latency_pm_qos_req)) 751 pm_qos_remove_request(&substream->latency_pm_qos_req); 752 if ((usecs = period_to_usecs(runtime)) >= 0) 753 pm_qos_add_request(&substream->latency_pm_qos_req, 754 PM_QOS_CPU_DMA_LATENCY, usecs); 755 return 0; 756 _error: 757 /* hardware might be unusable from this time, 758 so we force application to retry to set 759 the correct hardware parameter settings */ 760 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); 761 if (substream->ops->hw_free != NULL) 762 substream->ops->hw_free(substream); 763 if (substream->managed_buffer_alloc) 764 snd_pcm_lib_free_pages(substream); 765 return err; 766 } 767 768 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream, 769 struct snd_pcm_hw_params __user * _params) 770 { 771 struct snd_pcm_hw_params *params; 772 int err; 773 774 params = memdup_user(_params, sizeof(*params)); 775 if (IS_ERR(params)) 776 return PTR_ERR(params); 777 778 err = snd_pcm_hw_params(substream, params); 779 if (err < 0) 780 goto end; 781 782 if (copy_to_user(_params, params, sizeof(*params))) 783 err = -EFAULT; 784 end: 785 kfree(params); 786 return err; 787 } 788 789 static int snd_pcm_hw_free(struct snd_pcm_substream *substream) 790 { 791 struct snd_pcm_runtime *runtime; 792 int result = 0; 793 794 if (PCM_RUNTIME_CHECK(substream)) 795 return -ENXIO; 796 runtime = substream->runtime; 797 snd_pcm_stream_lock_irq(substream); 798 switch (runtime->status->state) { 799 case SNDRV_PCM_STATE_SETUP: 800 case SNDRV_PCM_STATE_PREPARED: 801 break; 802 default: 803 snd_pcm_stream_unlock_irq(substream); 804 return -EBADFD; 805 } 806 snd_pcm_stream_unlock_irq(substream); 807 if (atomic_read(&substream->mmap_count)) 808 return -EBADFD; 809 snd_pcm_sync_stop(substream); 810 if (substream->ops->hw_free) 811 result = substream->ops->hw_free(substream); 812 if (substream->managed_buffer_alloc) 813 snd_pcm_lib_free_pages(substream); 814 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); 815 pm_qos_remove_request(&substream->latency_pm_qos_req); 816 return result; 817 } 818 819 static int snd_pcm_sw_params(struct snd_pcm_substream *substream, 820 struct snd_pcm_sw_params *params) 821 { 822 struct snd_pcm_runtime *runtime; 823 int err; 824 825 if (PCM_RUNTIME_CHECK(substream)) 826 return -ENXIO; 827 runtime = substream->runtime; 828 snd_pcm_stream_lock_irq(substream); 829 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { 830 snd_pcm_stream_unlock_irq(substream); 831 return -EBADFD; 832 } 833 snd_pcm_stream_unlock_irq(substream); 834 835 if (params->tstamp_mode < 0 || 836 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST) 837 return -EINVAL; 838 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) && 839 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST) 840 return -EINVAL; 841 if (params->avail_min == 0) 842 return -EINVAL; 843 if (params->silence_size >= runtime->boundary) { 844 if (params->silence_threshold != 0) 845 return -EINVAL; 846 } else { 847 if (params->silence_size > params->silence_threshold) 848 return -EINVAL; 849 if (params->silence_threshold > runtime->buffer_size) 850 return -EINVAL; 851 } 852 err = 0; 853 snd_pcm_stream_lock_irq(substream); 854 runtime->tstamp_mode = params->tstamp_mode; 855 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12)) 856 runtime->tstamp_type = params->tstamp_type; 857 runtime->period_step = params->period_step; 858 runtime->control->avail_min = params->avail_min; 859 runtime->start_threshold = params->start_threshold; 860 runtime->stop_threshold = params->stop_threshold; 861 runtime->silence_threshold = params->silence_threshold; 862 runtime->silence_size = params->silence_size; 863 params->boundary = runtime->boundary; 864 if (snd_pcm_running(substream)) { 865 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 866 runtime->silence_size > 0) 867 snd_pcm_playback_silence(substream, ULONG_MAX); 868 err = snd_pcm_update_state(substream, runtime); 869 } 870 snd_pcm_stream_unlock_irq(substream); 871 return err; 872 } 873 874 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream, 875 struct snd_pcm_sw_params __user * _params) 876 { 877 struct snd_pcm_sw_params params; 878 int err; 879 if (copy_from_user(¶ms, _params, sizeof(params))) 880 return -EFAULT; 881 err = snd_pcm_sw_params(substream, ¶ms); 882 if (copy_to_user(_params, ¶ms, sizeof(params))) 883 return -EFAULT; 884 return err; 885 } 886 887 static inline snd_pcm_uframes_t 888 snd_pcm_calc_delay(struct snd_pcm_substream *substream) 889 { 890 snd_pcm_uframes_t delay; 891 892 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 893 delay = snd_pcm_playback_hw_avail(substream->runtime); 894 else 895 delay = snd_pcm_capture_avail(substream->runtime); 896 return delay + substream->runtime->delay; 897 } 898 899 int snd_pcm_status64(struct snd_pcm_substream *substream, 900 struct snd_pcm_status64 *status) 901 { 902 struct snd_pcm_runtime *runtime = substream->runtime; 903 904 snd_pcm_stream_lock_irq(substream); 905 906 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data, 907 &runtime->audio_tstamp_config); 908 909 /* backwards compatible behavior */ 910 if (runtime->audio_tstamp_config.type_requested == 911 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) { 912 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK) 913 runtime->audio_tstamp_config.type_requested = 914 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK; 915 else 916 runtime->audio_tstamp_config.type_requested = 917 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT; 918 runtime->audio_tstamp_report.valid = 0; 919 } else 920 runtime->audio_tstamp_report.valid = 1; 921 922 status->state = runtime->status->state; 923 status->suspended_state = runtime->status->suspended_state; 924 if (status->state == SNDRV_PCM_STATE_OPEN) 925 goto _end; 926 status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec; 927 status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec; 928 if (snd_pcm_running(substream)) { 929 snd_pcm_update_hw_ptr(substream); 930 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { 931 status->tstamp_sec = runtime->status->tstamp.tv_sec; 932 status->tstamp_nsec = 933 runtime->status->tstamp.tv_nsec; 934 status->driver_tstamp_sec = 935 runtime->driver_tstamp.tv_sec; 936 status->driver_tstamp_nsec = 937 runtime->driver_tstamp.tv_nsec; 938 status->audio_tstamp_sec = 939 runtime->status->audio_tstamp.tv_sec; 940 status->audio_tstamp_nsec = 941 runtime->status->audio_tstamp.tv_nsec; 942 if (runtime->audio_tstamp_report.valid == 1) 943 /* backwards compatibility, no report provided in COMPAT mode */ 944 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data, 945 &status->audio_tstamp_accuracy, 946 &runtime->audio_tstamp_report); 947 948 goto _tstamp_end; 949 } 950 } else { 951 /* get tstamp only in fallback mode and only if enabled */ 952 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { 953 struct timespec64 tstamp; 954 955 snd_pcm_gettime(runtime, &tstamp); 956 status->tstamp_sec = tstamp.tv_sec; 957 status->tstamp_nsec = tstamp.tv_nsec; 958 } 959 } 960 _tstamp_end: 961 status->appl_ptr = runtime->control->appl_ptr; 962 status->hw_ptr = runtime->status->hw_ptr; 963 status->avail = snd_pcm_avail(substream); 964 status->delay = snd_pcm_running(substream) ? 965 snd_pcm_calc_delay(substream) : 0; 966 status->avail_max = runtime->avail_max; 967 status->overrange = runtime->overrange; 968 runtime->avail_max = 0; 969 runtime->overrange = 0; 970 _end: 971 snd_pcm_stream_unlock_irq(substream); 972 return 0; 973 } 974 975 static int snd_pcm_status_user64(struct snd_pcm_substream *substream, 976 struct snd_pcm_status64 __user * _status, 977 bool ext) 978 { 979 struct snd_pcm_status64 status; 980 int res; 981 982 memset(&status, 0, sizeof(status)); 983 /* 984 * with extension, parameters are read/write, 985 * get audio_tstamp_data from user, 986 * ignore rest of status structure 987 */ 988 if (ext && get_user(status.audio_tstamp_data, 989 (u32 __user *)(&_status->audio_tstamp_data))) 990 return -EFAULT; 991 res = snd_pcm_status64(substream, &status); 992 if (res < 0) 993 return res; 994 if (copy_to_user(_status, &status, sizeof(status))) 995 return -EFAULT; 996 return 0; 997 } 998 999 static int snd_pcm_status_user32(struct snd_pcm_substream *substream, 1000 struct snd_pcm_status32 __user * _status, 1001 bool ext) 1002 { 1003 struct snd_pcm_status64 status64; 1004 struct snd_pcm_status32 status32; 1005 int res; 1006 1007 memset(&status64, 0, sizeof(status64)); 1008 memset(&status32, 0, sizeof(status32)); 1009 /* 1010 * with extension, parameters are read/write, 1011 * get audio_tstamp_data from user, 1012 * ignore rest of status structure 1013 */ 1014 if (ext && get_user(status64.audio_tstamp_data, 1015 (u32 __user *)(&_status->audio_tstamp_data))) 1016 return -EFAULT; 1017 res = snd_pcm_status64(substream, &status64); 1018 if (res < 0) 1019 return res; 1020 1021 status32 = (struct snd_pcm_status32) { 1022 .state = status64.state, 1023 .trigger_tstamp_sec = status64.trigger_tstamp_sec, 1024 .trigger_tstamp_nsec = status64.trigger_tstamp_nsec, 1025 .tstamp_sec = status64.tstamp_sec, 1026 .tstamp_nsec = status64.tstamp_nsec, 1027 .appl_ptr = status64.appl_ptr, 1028 .hw_ptr = status64.hw_ptr, 1029 .delay = status64.delay, 1030 .avail = status64.avail, 1031 .avail_max = status64.avail_max, 1032 .overrange = status64.overrange, 1033 .suspended_state = status64.suspended_state, 1034 .audio_tstamp_data = status64.audio_tstamp_data, 1035 .audio_tstamp_sec = status64.audio_tstamp_sec, 1036 .audio_tstamp_nsec = status64.audio_tstamp_nsec, 1037 .driver_tstamp_sec = status64.audio_tstamp_sec, 1038 .driver_tstamp_nsec = status64.audio_tstamp_nsec, 1039 .audio_tstamp_accuracy = status64.audio_tstamp_accuracy, 1040 }; 1041 1042 if (copy_to_user(_status, &status32, sizeof(status32))) 1043 return -EFAULT; 1044 1045 return 0; 1046 } 1047 1048 static int snd_pcm_channel_info(struct snd_pcm_substream *substream, 1049 struct snd_pcm_channel_info * info) 1050 { 1051 struct snd_pcm_runtime *runtime; 1052 unsigned int channel; 1053 1054 channel = info->channel; 1055 runtime = substream->runtime; 1056 snd_pcm_stream_lock_irq(substream); 1057 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { 1058 snd_pcm_stream_unlock_irq(substream); 1059 return -EBADFD; 1060 } 1061 snd_pcm_stream_unlock_irq(substream); 1062 if (channel >= runtime->channels) 1063 return -EINVAL; 1064 memset(info, 0, sizeof(*info)); 1065 info->channel = channel; 1066 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info); 1067 } 1068 1069 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream, 1070 struct snd_pcm_channel_info __user * _info) 1071 { 1072 struct snd_pcm_channel_info info; 1073 int res; 1074 1075 if (copy_from_user(&info, _info, sizeof(info))) 1076 return -EFAULT; 1077 res = snd_pcm_channel_info(substream, &info); 1078 if (res < 0) 1079 return res; 1080 if (copy_to_user(_info, &info, sizeof(info))) 1081 return -EFAULT; 1082 return 0; 1083 } 1084 1085 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream) 1086 { 1087 struct snd_pcm_runtime *runtime = substream->runtime; 1088 if (runtime->trigger_master == NULL) 1089 return; 1090 if (runtime->trigger_master == substream) { 1091 if (!runtime->trigger_tstamp_latched) 1092 snd_pcm_gettime(runtime, &runtime->trigger_tstamp); 1093 } else { 1094 snd_pcm_trigger_tstamp(runtime->trigger_master); 1095 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp; 1096 } 1097 runtime->trigger_master = NULL; 1098 } 1099 1100 struct action_ops { 1101 int (*pre_action)(struct snd_pcm_substream *substream, int state); 1102 int (*do_action)(struct snd_pcm_substream *substream, int state); 1103 void (*undo_action)(struct snd_pcm_substream *substream, int state); 1104 void (*post_action)(struct snd_pcm_substream *substream, int state); 1105 }; 1106 1107 /* 1108 * this functions is core for handling of linked stream 1109 * Note: the stream state might be changed also on failure 1110 * Note2: call with calling stream lock + link lock 1111 */ 1112 static int snd_pcm_action_group(const struct action_ops *ops, 1113 struct snd_pcm_substream *substream, 1114 int state, int do_lock) 1115 { 1116 struct snd_pcm_substream *s = NULL; 1117 struct snd_pcm_substream *s1; 1118 int res = 0, depth = 1; 1119 1120 snd_pcm_group_for_each_entry(s, substream) { 1121 if (do_lock && s != substream) { 1122 if (s->pcm->nonatomic) 1123 mutex_lock_nested(&s->self_group.mutex, depth); 1124 else 1125 spin_lock_nested(&s->self_group.lock, depth); 1126 depth++; 1127 } 1128 res = ops->pre_action(s, state); 1129 if (res < 0) 1130 goto _unlock; 1131 } 1132 snd_pcm_group_for_each_entry(s, substream) { 1133 res = ops->do_action(s, state); 1134 if (res < 0) { 1135 if (ops->undo_action) { 1136 snd_pcm_group_for_each_entry(s1, substream) { 1137 if (s1 == s) /* failed stream */ 1138 break; 1139 ops->undo_action(s1, state); 1140 } 1141 } 1142 s = NULL; /* unlock all */ 1143 goto _unlock; 1144 } 1145 } 1146 snd_pcm_group_for_each_entry(s, substream) { 1147 ops->post_action(s, state); 1148 } 1149 _unlock: 1150 if (do_lock) { 1151 /* unlock streams */ 1152 snd_pcm_group_for_each_entry(s1, substream) { 1153 if (s1 != substream) { 1154 if (s1->pcm->nonatomic) 1155 mutex_unlock(&s1->self_group.mutex); 1156 else 1157 spin_unlock(&s1->self_group.lock); 1158 } 1159 if (s1 == s) /* end */ 1160 break; 1161 } 1162 } 1163 return res; 1164 } 1165 1166 /* 1167 * Note: call with stream lock 1168 */ 1169 static int snd_pcm_action_single(const struct action_ops *ops, 1170 struct snd_pcm_substream *substream, 1171 int state) 1172 { 1173 int res; 1174 1175 res = ops->pre_action(substream, state); 1176 if (res < 0) 1177 return res; 1178 res = ops->do_action(substream, state); 1179 if (res == 0) 1180 ops->post_action(substream, state); 1181 else if (ops->undo_action) 1182 ops->undo_action(substream, state); 1183 return res; 1184 } 1185 1186 static void snd_pcm_group_assign(struct snd_pcm_substream *substream, 1187 struct snd_pcm_group *new_group) 1188 { 1189 substream->group = new_group; 1190 list_move(&substream->link_list, &new_group->substreams); 1191 } 1192 1193 /* 1194 * Unref and unlock the group, but keep the stream lock; 1195 * when the group becomes empty and no longer referred, destroy itself 1196 */ 1197 static void snd_pcm_group_unref(struct snd_pcm_group *group, 1198 struct snd_pcm_substream *substream) 1199 { 1200 bool do_free; 1201 1202 if (!group) 1203 return; 1204 do_free = refcount_dec_and_test(&group->refs); 1205 snd_pcm_group_unlock(group, substream->pcm->nonatomic); 1206 if (do_free) 1207 kfree(group); 1208 } 1209 1210 /* 1211 * Lock the group inside a stream lock and reference it; 1212 * return the locked group object, or NULL if not linked 1213 */ 1214 static struct snd_pcm_group * 1215 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream) 1216 { 1217 bool nonatomic = substream->pcm->nonatomic; 1218 struct snd_pcm_group *group; 1219 bool trylock; 1220 1221 for (;;) { 1222 if (!snd_pcm_stream_linked(substream)) 1223 return NULL; 1224 group = substream->group; 1225 /* block freeing the group object */ 1226 refcount_inc(&group->refs); 1227 1228 trylock = nonatomic ? mutex_trylock(&group->mutex) : 1229 spin_trylock(&group->lock); 1230 if (trylock) 1231 break; /* OK */ 1232 1233 /* re-lock for avoiding ABBA deadlock */ 1234 snd_pcm_stream_unlock(substream); 1235 snd_pcm_group_lock(group, nonatomic); 1236 snd_pcm_stream_lock(substream); 1237 1238 /* check the group again; the above opens a small race window */ 1239 if (substream->group == group) 1240 break; /* OK */ 1241 /* group changed, try again */ 1242 snd_pcm_group_unref(group, substream); 1243 } 1244 return group; 1245 } 1246 1247 /* 1248 * Note: call with stream lock 1249 */ 1250 static int snd_pcm_action(const struct action_ops *ops, 1251 struct snd_pcm_substream *substream, 1252 int state) 1253 { 1254 struct snd_pcm_group *group; 1255 int res; 1256 1257 group = snd_pcm_stream_group_ref(substream); 1258 if (group) 1259 res = snd_pcm_action_group(ops, substream, state, 1); 1260 else 1261 res = snd_pcm_action_single(ops, substream, state); 1262 snd_pcm_group_unref(group, substream); 1263 return res; 1264 } 1265 1266 /* 1267 * Note: don't use any locks before 1268 */ 1269 static int snd_pcm_action_lock_irq(const struct action_ops *ops, 1270 struct snd_pcm_substream *substream, 1271 int state) 1272 { 1273 int res; 1274 1275 snd_pcm_stream_lock_irq(substream); 1276 res = snd_pcm_action(ops, substream, state); 1277 snd_pcm_stream_unlock_irq(substream); 1278 return res; 1279 } 1280 1281 /* 1282 */ 1283 static int snd_pcm_action_nonatomic(const struct action_ops *ops, 1284 struct snd_pcm_substream *substream, 1285 int state) 1286 { 1287 int res; 1288 1289 /* Guarantee the group members won't change during non-atomic action */ 1290 down_read(&snd_pcm_link_rwsem); 1291 if (snd_pcm_stream_linked(substream)) 1292 res = snd_pcm_action_group(ops, substream, state, 0); 1293 else 1294 res = snd_pcm_action_single(ops, substream, state); 1295 up_read(&snd_pcm_link_rwsem); 1296 return res; 1297 } 1298 1299 /* 1300 * start callbacks 1301 */ 1302 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state) 1303 { 1304 struct snd_pcm_runtime *runtime = substream->runtime; 1305 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED) 1306 return -EBADFD; 1307 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 1308 !snd_pcm_playback_data(substream)) 1309 return -EPIPE; 1310 runtime->trigger_tstamp_latched = false; 1311 runtime->trigger_master = substream; 1312 return 0; 1313 } 1314 1315 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state) 1316 { 1317 if (substream->runtime->trigger_master != substream) 1318 return 0; 1319 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START); 1320 } 1321 1322 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state) 1323 { 1324 if (substream->runtime->trigger_master == substream) 1325 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP); 1326 } 1327 1328 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state) 1329 { 1330 struct snd_pcm_runtime *runtime = substream->runtime; 1331 snd_pcm_trigger_tstamp(substream); 1332 runtime->hw_ptr_jiffies = jiffies; 1333 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) / 1334 runtime->rate; 1335 runtime->status->state = state; 1336 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 1337 runtime->silence_size > 0) 1338 snd_pcm_playback_silence(substream, ULONG_MAX); 1339 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART); 1340 } 1341 1342 static const struct action_ops snd_pcm_action_start = { 1343 .pre_action = snd_pcm_pre_start, 1344 .do_action = snd_pcm_do_start, 1345 .undo_action = snd_pcm_undo_start, 1346 .post_action = snd_pcm_post_start 1347 }; 1348 1349 /** 1350 * snd_pcm_start - start all linked streams 1351 * @substream: the PCM substream instance 1352 * 1353 * Return: Zero if successful, or a negative error code. 1354 * The stream lock must be acquired before calling this function. 1355 */ 1356 int snd_pcm_start(struct snd_pcm_substream *substream) 1357 { 1358 return snd_pcm_action(&snd_pcm_action_start, substream, 1359 SNDRV_PCM_STATE_RUNNING); 1360 } 1361 1362 /* take the stream lock and start the streams */ 1363 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream) 1364 { 1365 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, 1366 SNDRV_PCM_STATE_RUNNING); 1367 } 1368 1369 /* 1370 * stop callbacks 1371 */ 1372 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state) 1373 { 1374 struct snd_pcm_runtime *runtime = substream->runtime; 1375 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 1376 return -EBADFD; 1377 runtime->trigger_master = substream; 1378 return 0; 1379 } 1380 1381 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state) 1382 { 1383 if (substream->runtime->trigger_master == substream && 1384 snd_pcm_running(substream)) 1385 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP); 1386 return 0; /* unconditonally stop all substreams */ 1387 } 1388 1389 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state) 1390 { 1391 struct snd_pcm_runtime *runtime = substream->runtime; 1392 if (runtime->status->state != state) { 1393 snd_pcm_trigger_tstamp(substream); 1394 runtime->status->state = state; 1395 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP); 1396 } 1397 runtime->stop_operating = true; 1398 wake_up(&runtime->sleep); 1399 wake_up(&runtime->tsleep); 1400 } 1401 1402 static const struct action_ops snd_pcm_action_stop = { 1403 .pre_action = snd_pcm_pre_stop, 1404 .do_action = snd_pcm_do_stop, 1405 .post_action = snd_pcm_post_stop 1406 }; 1407 1408 /** 1409 * snd_pcm_stop - try to stop all running streams in the substream group 1410 * @substream: the PCM substream instance 1411 * @state: PCM state after stopping the stream 1412 * 1413 * The state of each stream is then changed to the given state unconditionally. 1414 * 1415 * Return: Zero if successful, or a negative error code. 1416 */ 1417 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state) 1418 { 1419 return snd_pcm_action(&snd_pcm_action_stop, substream, state); 1420 } 1421 EXPORT_SYMBOL(snd_pcm_stop); 1422 1423 /** 1424 * snd_pcm_drain_done - stop the DMA only when the given stream is playback 1425 * @substream: the PCM substream 1426 * 1427 * After stopping, the state is changed to SETUP. 1428 * Unlike snd_pcm_stop(), this affects only the given stream. 1429 * 1430 * Return: Zero if succesful, or a negative error code. 1431 */ 1432 int snd_pcm_drain_done(struct snd_pcm_substream *substream) 1433 { 1434 return snd_pcm_action_single(&snd_pcm_action_stop, substream, 1435 SNDRV_PCM_STATE_SETUP); 1436 } 1437 1438 /** 1439 * snd_pcm_stop_xrun - stop the running streams as XRUN 1440 * @substream: the PCM substream instance 1441 * 1442 * This stops the given running substream (and all linked substreams) as XRUN. 1443 * Unlike snd_pcm_stop(), this function takes the substream lock by itself. 1444 * 1445 * Return: Zero if successful, or a negative error code. 1446 */ 1447 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream) 1448 { 1449 unsigned long flags; 1450 1451 snd_pcm_stream_lock_irqsave(substream, flags); 1452 if (substream->runtime && snd_pcm_running(substream)) 1453 __snd_pcm_xrun(substream); 1454 snd_pcm_stream_unlock_irqrestore(substream, flags); 1455 return 0; 1456 } 1457 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun); 1458 1459 /* 1460 * pause callbacks 1461 */ 1462 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push) 1463 { 1464 struct snd_pcm_runtime *runtime = substream->runtime; 1465 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE)) 1466 return -ENOSYS; 1467 if (push) { 1468 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING) 1469 return -EBADFD; 1470 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED) 1471 return -EBADFD; 1472 runtime->trigger_master = substream; 1473 return 0; 1474 } 1475 1476 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push) 1477 { 1478 if (substream->runtime->trigger_master != substream) 1479 return 0; 1480 /* some drivers might use hw_ptr to recover from the pause - 1481 update the hw_ptr now */ 1482 if (push) 1483 snd_pcm_update_hw_ptr(substream); 1484 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by 1485 * a delta between the current jiffies, this gives a large enough 1486 * delta, effectively to skip the check once. 1487 */ 1488 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000; 1489 return substream->ops->trigger(substream, 1490 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH : 1491 SNDRV_PCM_TRIGGER_PAUSE_RELEASE); 1492 } 1493 1494 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push) 1495 { 1496 if (substream->runtime->trigger_master == substream) 1497 substream->ops->trigger(substream, 1498 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE : 1499 SNDRV_PCM_TRIGGER_PAUSE_PUSH); 1500 } 1501 1502 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push) 1503 { 1504 struct snd_pcm_runtime *runtime = substream->runtime; 1505 snd_pcm_trigger_tstamp(substream); 1506 if (push) { 1507 runtime->status->state = SNDRV_PCM_STATE_PAUSED; 1508 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE); 1509 wake_up(&runtime->sleep); 1510 wake_up(&runtime->tsleep); 1511 } else { 1512 runtime->status->state = SNDRV_PCM_STATE_RUNNING; 1513 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE); 1514 } 1515 } 1516 1517 static const struct action_ops snd_pcm_action_pause = { 1518 .pre_action = snd_pcm_pre_pause, 1519 .do_action = snd_pcm_do_pause, 1520 .undo_action = snd_pcm_undo_pause, 1521 .post_action = snd_pcm_post_pause 1522 }; 1523 1524 /* 1525 * Push/release the pause for all linked streams. 1526 */ 1527 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push) 1528 { 1529 return snd_pcm_action(&snd_pcm_action_pause, substream, push); 1530 } 1531 1532 #ifdef CONFIG_PM 1533 /* suspend */ 1534 1535 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state) 1536 { 1537 struct snd_pcm_runtime *runtime = substream->runtime; 1538 switch (runtime->status->state) { 1539 case SNDRV_PCM_STATE_SUSPENDED: 1540 return -EBUSY; 1541 /* unresumable PCM state; return -EBUSY for skipping suspend */ 1542 case SNDRV_PCM_STATE_OPEN: 1543 case SNDRV_PCM_STATE_SETUP: 1544 case SNDRV_PCM_STATE_DISCONNECTED: 1545 return -EBUSY; 1546 } 1547 runtime->trigger_master = substream; 1548 return 0; 1549 } 1550 1551 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state) 1552 { 1553 struct snd_pcm_runtime *runtime = substream->runtime; 1554 if (runtime->trigger_master != substream) 1555 return 0; 1556 if (! snd_pcm_running(substream)) 1557 return 0; 1558 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); 1559 return 0; /* suspend unconditionally */ 1560 } 1561 1562 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state) 1563 { 1564 struct snd_pcm_runtime *runtime = substream->runtime; 1565 snd_pcm_trigger_tstamp(substream); 1566 runtime->status->suspended_state = runtime->status->state; 1567 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED; 1568 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND); 1569 wake_up(&runtime->sleep); 1570 wake_up(&runtime->tsleep); 1571 } 1572 1573 static const struct action_ops snd_pcm_action_suspend = { 1574 .pre_action = snd_pcm_pre_suspend, 1575 .do_action = snd_pcm_do_suspend, 1576 .post_action = snd_pcm_post_suspend 1577 }; 1578 1579 /* 1580 * snd_pcm_suspend - trigger SUSPEND to all linked streams 1581 * @substream: the PCM substream 1582 * 1583 * After this call, all streams are changed to SUSPENDED state. 1584 * 1585 * Return: Zero if successful, or a negative error code. 1586 */ 1587 static int snd_pcm_suspend(struct snd_pcm_substream *substream) 1588 { 1589 int err; 1590 unsigned long flags; 1591 1592 snd_pcm_stream_lock_irqsave(substream, flags); 1593 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0); 1594 snd_pcm_stream_unlock_irqrestore(substream, flags); 1595 return err; 1596 } 1597 1598 /** 1599 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm 1600 * @pcm: the PCM instance 1601 * 1602 * After this call, all streams are changed to SUSPENDED state. 1603 * 1604 * Return: Zero if successful (or @pcm is %NULL), or a negative error code. 1605 */ 1606 int snd_pcm_suspend_all(struct snd_pcm *pcm) 1607 { 1608 struct snd_pcm_substream *substream; 1609 int stream, err = 0; 1610 1611 if (! pcm) 1612 return 0; 1613 1614 for (stream = 0; stream < 2; stream++) { 1615 for (substream = pcm->streams[stream].substream; 1616 substream; substream = substream->next) { 1617 /* FIXME: the open/close code should lock this as well */ 1618 if (substream->runtime == NULL) 1619 continue; 1620 1621 /* 1622 * Skip BE dai link PCM's that are internal and may 1623 * not have their substream ops set. 1624 */ 1625 if (!substream->ops) 1626 continue; 1627 1628 err = snd_pcm_suspend(substream); 1629 if (err < 0 && err != -EBUSY) 1630 return err; 1631 } 1632 } 1633 return 0; 1634 } 1635 EXPORT_SYMBOL(snd_pcm_suspend_all); 1636 1637 /* resume */ 1638 1639 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state) 1640 { 1641 struct snd_pcm_runtime *runtime = substream->runtime; 1642 if (!(runtime->info & SNDRV_PCM_INFO_RESUME)) 1643 return -ENOSYS; 1644 runtime->trigger_master = substream; 1645 return 0; 1646 } 1647 1648 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state) 1649 { 1650 struct snd_pcm_runtime *runtime = substream->runtime; 1651 if (runtime->trigger_master != substream) 1652 return 0; 1653 /* DMA not running previously? */ 1654 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING && 1655 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING || 1656 substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) 1657 return 0; 1658 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME); 1659 } 1660 1661 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state) 1662 { 1663 if (substream->runtime->trigger_master == substream && 1664 snd_pcm_running(substream)) 1665 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); 1666 } 1667 1668 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state) 1669 { 1670 struct snd_pcm_runtime *runtime = substream->runtime; 1671 snd_pcm_trigger_tstamp(substream); 1672 runtime->status->state = runtime->status->suspended_state; 1673 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME); 1674 snd_pcm_sync_stop(substream); 1675 } 1676 1677 static const struct action_ops snd_pcm_action_resume = { 1678 .pre_action = snd_pcm_pre_resume, 1679 .do_action = snd_pcm_do_resume, 1680 .undo_action = snd_pcm_undo_resume, 1681 .post_action = snd_pcm_post_resume 1682 }; 1683 1684 static int snd_pcm_resume(struct snd_pcm_substream *substream) 1685 { 1686 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0); 1687 } 1688 1689 #else 1690 1691 static int snd_pcm_resume(struct snd_pcm_substream *substream) 1692 { 1693 return -ENOSYS; 1694 } 1695 1696 #endif /* CONFIG_PM */ 1697 1698 /* 1699 * xrun ioctl 1700 * 1701 * Change the RUNNING stream(s) to XRUN state. 1702 */ 1703 static int snd_pcm_xrun(struct snd_pcm_substream *substream) 1704 { 1705 struct snd_pcm_runtime *runtime = substream->runtime; 1706 int result; 1707 1708 snd_pcm_stream_lock_irq(substream); 1709 switch (runtime->status->state) { 1710 case SNDRV_PCM_STATE_XRUN: 1711 result = 0; /* already there */ 1712 break; 1713 case SNDRV_PCM_STATE_RUNNING: 1714 __snd_pcm_xrun(substream); 1715 result = 0; 1716 break; 1717 default: 1718 result = -EBADFD; 1719 } 1720 snd_pcm_stream_unlock_irq(substream); 1721 return result; 1722 } 1723 1724 /* 1725 * reset ioctl 1726 */ 1727 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state) 1728 { 1729 struct snd_pcm_runtime *runtime = substream->runtime; 1730 switch (runtime->status->state) { 1731 case SNDRV_PCM_STATE_RUNNING: 1732 case SNDRV_PCM_STATE_PREPARED: 1733 case SNDRV_PCM_STATE_PAUSED: 1734 case SNDRV_PCM_STATE_SUSPENDED: 1735 return 0; 1736 default: 1737 return -EBADFD; 1738 } 1739 } 1740 1741 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state) 1742 { 1743 struct snd_pcm_runtime *runtime = substream->runtime; 1744 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL); 1745 if (err < 0) 1746 return err; 1747 runtime->hw_ptr_base = 0; 1748 runtime->hw_ptr_interrupt = runtime->status->hw_ptr - 1749 runtime->status->hw_ptr % runtime->period_size; 1750 runtime->silence_start = runtime->status->hw_ptr; 1751 runtime->silence_filled = 0; 1752 return 0; 1753 } 1754 1755 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state) 1756 { 1757 struct snd_pcm_runtime *runtime = substream->runtime; 1758 runtime->control->appl_ptr = runtime->status->hw_ptr; 1759 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 1760 runtime->silence_size > 0) 1761 snd_pcm_playback_silence(substream, ULONG_MAX); 1762 } 1763 1764 static const struct action_ops snd_pcm_action_reset = { 1765 .pre_action = snd_pcm_pre_reset, 1766 .do_action = snd_pcm_do_reset, 1767 .post_action = snd_pcm_post_reset 1768 }; 1769 1770 static int snd_pcm_reset(struct snd_pcm_substream *substream) 1771 { 1772 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0); 1773 } 1774 1775 /* 1776 * prepare ioctl 1777 */ 1778 /* we use the second argument for updating f_flags */ 1779 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, 1780 int f_flags) 1781 { 1782 struct snd_pcm_runtime *runtime = substream->runtime; 1783 if (runtime->status->state == SNDRV_PCM_STATE_OPEN || 1784 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED) 1785 return -EBADFD; 1786 if (snd_pcm_running(substream)) 1787 return -EBUSY; 1788 substream->f_flags = f_flags; 1789 return 0; 1790 } 1791 1792 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state) 1793 { 1794 int err; 1795 snd_pcm_sync_stop(substream); 1796 err = substream->ops->prepare(substream); 1797 if (err < 0) 1798 return err; 1799 return snd_pcm_do_reset(substream, 0); 1800 } 1801 1802 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state) 1803 { 1804 struct snd_pcm_runtime *runtime = substream->runtime; 1805 runtime->control->appl_ptr = runtime->status->hw_ptr; 1806 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED); 1807 } 1808 1809 static const struct action_ops snd_pcm_action_prepare = { 1810 .pre_action = snd_pcm_pre_prepare, 1811 .do_action = snd_pcm_do_prepare, 1812 .post_action = snd_pcm_post_prepare 1813 }; 1814 1815 /** 1816 * snd_pcm_prepare - prepare the PCM substream to be triggerable 1817 * @substream: the PCM substream instance 1818 * @file: file to refer f_flags 1819 * 1820 * Return: Zero if successful, or a negative error code. 1821 */ 1822 static int snd_pcm_prepare(struct snd_pcm_substream *substream, 1823 struct file *file) 1824 { 1825 int f_flags; 1826 1827 if (file) 1828 f_flags = file->f_flags; 1829 else 1830 f_flags = substream->f_flags; 1831 1832 snd_pcm_stream_lock_irq(substream); 1833 switch (substream->runtime->status->state) { 1834 case SNDRV_PCM_STATE_PAUSED: 1835 snd_pcm_pause(substream, 0); 1836 /* fallthru */ 1837 case SNDRV_PCM_STATE_SUSPENDED: 1838 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); 1839 break; 1840 } 1841 snd_pcm_stream_unlock_irq(substream); 1842 1843 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare, 1844 substream, f_flags); 1845 } 1846 1847 /* 1848 * drain ioctl 1849 */ 1850 1851 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state) 1852 { 1853 struct snd_pcm_runtime *runtime = substream->runtime; 1854 switch (runtime->status->state) { 1855 case SNDRV_PCM_STATE_OPEN: 1856 case SNDRV_PCM_STATE_DISCONNECTED: 1857 case SNDRV_PCM_STATE_SUSPENDED: 1858 return -EBADFD; 1859 } 1860 runtime->trigger_master = substream; 1861 return 0; 1862 } 1863 1864 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state) 1865 { 1866 struct snd_pcm_runtime *runtime = substream->runtime; 1867 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1868 switch (runtime->status->state) { 1869 case SNDRV_PCM_STATE_PREPARED: 1870 /* start playback stream if possible */ 1871 if (! snd_pcm_playback_empty(substream)) { 1872 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING); 1873 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING); 1874 } else { 1875 runtime->status->state = SNDRV_PCM_STATE_SETUP; 1876 } 1877 break; 1878 case SNDRV_PCM_STATE_RUNNING: 1879 runtime->status->state = SNDRV_PCM_STATE_DRAINING; 1880 break; 1881 case SNDRV_PCM_STATE_XRUN: 1882 runtime->status->state = SNDRV_PCM_STATE_SETUP; 1883 break; 1884 default: 1885 break; 1886 } 1887 } else { 1888 /* stop running stream */ 1889 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) { 1890 int new_state = snd_pcm_capture_avail(runtime) > 0 ? 1891 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP; 1892 snd_pcm_do_stop(substream, new_state); 1893 snd_pcm_post_stop(substream, new_state); 1894 } 1895 } 1896 1897 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING && 1898 runtime->trigger_master == substream && 1899 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER)) 1900 return substream->ops->trigger(substream, 1901 SNDRV_PCM_TRIGGER_DRAIN); 1902 1903 return 0; 1904 } 1905 1906 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state) 1907 { 1908 } 1909 1910 static const struct action_ops snd_pcm_action_drain_init = { 1911 .pre_action = snd_pcm_pre_drain_init, 1912 .do_action = snd_pcm_do_drain_init, 1913 .post_action = snd_pcm_post_drain_init 1914 }; 1915 1916 /* 1917 * Drain the stream(s). 1918 * When the substream is linked, sync until the draining of all playback streams 1919 * is finished. 1920 * After this call, all streams are supposed to be either SETUP or DRAINING 1921 * (capture only) state. 1922 */ 1923 static int snd_pcm_drain(struct snd_pcm_substream *substream, 1924 struct file *file) 1925 { 1926 struct snd_card *card; 1927 struct snd_pcm_runtime *runtime; 1928 struct snd_pcm_substream *s; 1929 struct snd_pcm_group *group; 1930 wait_queue_entry_t wait; 1931 int result = 0; 1932 int nonblock = 0; 1933 1934 card = substream->pcm->card; 1935 runtime = substream->runtime; 1936 1937 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 1938 return -EBADFD; 1939 1940 if (file) { 1941 if (file->f_flags & O_NONBLOCK) 1942 nonblock = 1; 1943 } else if (substream->f_flags & O_NONBLOCK) 1944 nonblock = 1; 1945 1946 snd_pcm_stream_lock_irq(substream); 1947 /* resume pause */ 1948 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED) 1949 snd_pcm_pause(substream, 0); 1950 1951 /* pre-start/stop - all running streams are changed to DRAINING state */ 1952 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0); 1953 if (result < 0) 1954 goto unlock; 1955 /* in non-blocking, we don't wait in ioctl but let caller poll */ 1956 if (nonblock) { 1957 result = -EAGAIN; 1958 goto unlock; 1959 } 1960 1961 for (;;) { 1962 long tout; 1963 struct snd_pcm_runtime *to_check; 1964 if (signal_pending(current)) { 1965 result = -ERESTARTSYS; 1966 break; 1967 } 1968 /* find a substream to drain */ 1969 to_check = NULL; 1970 group = snd_pcm_stream_group_ref(substream); 1971 snd_pcm_group_for_each_entry(s, substream) { 1972 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK) 1973 continue; 1974 runtime = s->runtime; 1975 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) { 1976 to_check = runtime; 1977 break; 1978 } 1979 } 1980 snd_pcm_group_unref(group, substream); 1981 if (!to_check) 1982 break; /* all drained */ 1983 init_waitqueue_entry(&wait, current); 1984 set_current_state(TASK_INTERRUPTIBLE); 1985 add_wait_queue(&to_check->sleep, &wait); 1986 snd_pcm_stream_unlock_irq(substream); 1987 if (runtime->no_period_wakeup) 1988 tout = MAX_SCHEDULE_TIMEOUT; 1989 else { 1990 tout = 10; 1991 if (runtime->rate) { 1992 long t = runtime->period_size * 2 / runtime->rate; 1993 tout = max(t, tout); 1994 } 1995 tout = msecs_to_jiffies(tout * 1000); 1996 } 1997 tout = schedule_timeout(tout); 1998 1999 snd_pcm_stream_lock_irq(substream); 2000 group = snd_pcm_stream_group_ref(substream); 2001 snd_pcm_group_for_each_entry(s, substream) { 2002 if (s->runtime == to_check) { 2003 remove_wait_queue(&to_check->sleep, &wait); 2004 break; 2005 } 2006 } 2007 snd_pcm_group_unref(group, substream); 2008 2009 if (card->shutdown) { 2010 result = -ENODEV; 2011 break; 2012 } 2013 if (tout == 0) { 2014 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) 2015 result = -ESTRPIPE; 2016 else { 2017 dev_dbg(substream->pcm->card->dev, 2018 "playback drain error (DMA or IRQ trouble?)\n"); 2019 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); 2020 result = -EIO; 2021 } 2022 break; 2023 } 2024 } 2025 2026 unlock: 2027 snd_pcm_stream_unlock_irq(substream); 2028 2029 return result; 2030 } 2031 2032 /* 2033 * drop ioctl 2034 * 2035 * Immediately put all linked substreams into SETUP state. 2036 */ 2037 static int snd_pcm_drop(struct snd_pcm_substream *substream) 2038 { 2039 struct snd_pcm_runtime *runtime; 2040 int result = 0; 2041 2042 if (PCM_RUNTIME_CHECK(substream)) 2043 return -ENXIO; 2044 runtime = substream->runtime; 2045 2046 if (runtime->status->state == SNDRV_PCM_STATE_OPEN || 2047 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED) 2048 return -EBADFD; 2049 2050 snd_pcm_stream_lock_irq(substream); 2051 /* resume pause */ 2052 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED) 2053 snd_pcm_pause(substream, 0); 2054 2055 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); 2056 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */ 2057 snd_pcm_stream_unlock_irq(substream); 2058 2059 return result; 2060 } 2061 2062 2063 static bool is_pcm_file(struct file *file) 2064 { 2065 struct inode *inode = file_inode(file); 2066 struct snd_pcm *pcm; 2067 unsigned int minor; 2068 2069 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major) 2070 return false; 2071 minor = iminor(inode); 2072 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK); 2073 if (!pcm) 2074 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE); 2075 if (!pcm) 2076 return false; 2077 snd_card_unref(pcm->card); 2078 return true; 2079 } 2080 2081 /* 2082 * PCM link handling 2083 */ 2084 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd) 2085 { 2086 int res = 0; 2087 struct snd_pcm_file *pcm_file; 2088 struct snd_pcm_substream *substream1; 2089 struct snd_pcm_group *group, *target_group; 2090 bool nonatomic = substream->pcm->nonatomic; 2091 struct fd f = fdget(fd); 2092 2093 if (!f.file) 2094 return -EBADFD; 2095 if (!is_pcm_file(f.file)) { 2096 res = -EBADFD; 2097 goto _badf; 2098 } 2099 pcm_file = f.file->private_data; 2100 substream1 = pcm_file->substream; 2101 group = kzalloc(sizeof(*group), GFP_KERNEL); 2102 if (!group) { 2103 res = -ENOMEM; 2104 goto _nolock; 2105 } 2106 snd_pcm_group_init(group); 2107 2108 down_write(&snd_pcm_link_rwsem); 2109 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN || 2110 substream->runtime->status->state != substream1->runtime->status->state || 2111 substream->pcm->nonatomic != substream1->pcm->nonatomic) { 2112 res = -EBADFD; 2113 goto _end; 2114 } 2115 if (snd_pcm_stream_linked(substream1)) { 2116 res = -EALREADY; 2117 goto _end; 2118 } 2119 2120 snd_pcm_stream_lock_irq(substream); 2121 if (!snd_pcm_stream_linked(substream)) { 2122 snd_pcm_group_assign(substream, group); 2123 group = NULL; /* assigned, don't free this one below */ 2124 } 2125 target_group = substream->group; 2126 snd_pcm_stream_unlock_irq(substream); 2127 2128 snd_pcm_group_lock_irq(target_group, nonatomic); 2129 snd_pcm_stream_lock(substream1); 2130 snd_pcm_group_assign(substream1, target_group); 2131 refcount_inc(&target_group->refs); 2132 snd_pcm_stream_unlock(substream1); 2133 snd_pcm_group_unlock_irq(target_group, nonatomic); 2134 _end: 2135 up_write(&snd_pcm_link_rwsem); 2136 _nolock: 2137 kfree(group); 2138 _badf: 2139 fdput(f); 2140 return res; 2141 } 2142 2143 static void relink_to_local(struct snd_pcm_substream *substream) 2144 { 2145 snd_pcm_stream_lock(substream); 2146 snd_pcm_group_assign(substream, &substream->self_group); 2147 snd_pcm_stream_unlock(substream); 2148 } 2149 2150 static int snd_pcm_unlink(struct snd_pcm_substream *substream) 2151 { 2152 struct snd_pcm_group *group; 2153 bool nonatomic = substream->pcm->nonatomic; 2154 bool do_free = false; 2155 int res = 0; 2156 2157 down_write(&snd_pcm_link_rwsem); 2158 2159 if (!snd_pcm_stream_linked(substream)) { 2160 res = -EALREADY; 2161 goto _end; 2162 } 2163 2164 group = substream->group; 2165 snd_pcm_group_lock_irq(group, nonatomic); 2166 2167 relink_to_local(substream); 2168 refcount_dec(&group->refs); 2169 2170 /* detach the last stream, too */ 2171 if (list_is_singular(&group->substreams)) { 2172 relink_to_local(list_first_entry(&group->substreams, 2173 struct snd_pcm_substream, 2174 link_list)); 2175 do_free = refcount_dec_and_test(&group->refs); 2176 } 2177 2178 snd_pcm_group_unlock_irq(group, nonatomic); 2179 if (do_free) 2180 kfree(group); 2181 2182 _end: 2183 up_write(&snd_pcm_link_rwsem); 2184 return res; 2185 } 2186 2187 /* 2188 * hw configurator 2189 */ 2190 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params, 2191 struct snd_pcm_hw_rule *rule) 2192 { 2193 struct snd_interval t; 2194 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]), 2195 hw_param_interval_c(params, rule->deps[1]), &t); 2196 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2197 } 2198 2199 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params, 2200 struct snd_pcm_hw_rule *rule) 2201 { 2202 struct snd_interval t; 2203 snd_interval_div(hw_param_interval_c(params, rule->deps[0]), 2204 hw_param_interval_c(params, rule->deps[1]), &t); 2205 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2206 } 2207 2208 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params, 2209 struct snd_pcm_hw_rule *rule) 2210 { 2211 struct snd_interval t; 2212 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]), 2213 hw_param_interval_c(params, rule->deps[1]), 2214 (unsigned long) rule->private, &t); 2215 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2216 } 2217 2218 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params, 2219 struct snd_pcm_hw_rule *rule) 2220 { 2221 struct snd_interval t; 2222 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]), 2223 (unsigned long) rule->private, 2224 hw_param_interval_c(params, rule->deps[1]), &t); 2225 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2226 } 2227 2228 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params, 2229 struct snd_pcm_hw_rule *rule) 2230 { 2231 unsigned int k; 2232 const struct snd_interval *i = 2233 hw_param_interval_c(params, rule->deps[0]); 2234 struct snd_mask m; 2235 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 2236 snd_mask_any(&m); 2237 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) { 2238 int bits; 2239 if (! snd_mask_test(mask, k)) 2240 continue; 2241 bits = snd_pcm_format_physical_width(k); 2242 if (bits <= 0) 2243 continue; /* ignore invalid formats */ 2244 if ((unsigned)bits < i->min || (unsigned)bits > i->max) 2245 snd_mask_reset(&m, k); 2246 } 2247 return snd_mask_refine(mask, &m); 2248 } 2249 2250 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params, 2251 struct snd_pcm_hw_rule *rule) 2252 { 2253 struct snd_interval t; 2254 unsigned int k; 2255 t.min = UINT_MAX; 2256 t.max = 0; 2257 t.openmin = 0; 2258 t.openmax = 0; 2259 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) { 2260 int bits; 2261 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k)) 2262 continue; 2263 bits = snd_pcm_format_physical_width(k); 2264 if (bits <= 0) 2265 continue; /* ignore invalid formats */ 2266 if (t.min > (unsigned)bits) 2267 t.min = bits; 2268 if (t.max < (unsigned)bits) 2269 t.max = bits; 2270 } 2271 t.integer = 1; 2272 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2273 } 2274 2275 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 2276 #error "Change this table" 2277 #endif 2278 2279 static const unsigned int rates[] = { 2280 5512, 8000, 11025, 16000, 22050, 32000, 44100, 2281 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000 2282 }; 2283 2284 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = { 2285 .count = ARRAY_SIZE(rates), 2286 .list = rates, 2287 }; 2288 2289 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params, 2290 struct snd_pcm_hw_rule *rule) 2291 { 2292 struct snd_pcm_hardware *hw = rule->private; 2293 return snd_interval_list(hw_param_interval(params, rule->var), 2294 snd_pcm_known_rates.count, 2295 snd_pcm_known_rates.list, hw->rates); 2296 } 2297 2298 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params, 2299 struct snd_pcm_hw_rule *rule) 2300 { 2301 struct snd_interval t; 2302 struct snd_pcm_substream *substream = rule->private; 2303 t.min = 0; 2304 t.max = substream->buffer_bytes_max; 2305 t.openmin = 0; 2306 t.openmax = 0; 2307 t.integer = 1; 2308 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2309 } 2310 2311 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream) 2312 { 2313 struct snd_pcm_runtime *runtime = substream->runtime; 2314 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; 2315 int k, err; 2316 2317 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { 2318 snd_mask_any(constrs_mask(constrs, k)); 2319 } 2320 2321 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { 2322 snd_interval_any(constrs_interval(constrs, k)); 2323 } 2324 2325 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS)); 2326 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)); 2327 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)); 2328 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)); 2329 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS)); 2330 2331 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, 2332 snd_pcm_hw_rule_format, NULL, 2333 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 2334 if (err < 0) 2335 return err; 2336 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 2337 snd_pcm_hw_rule_sample_bits, NULL, 2338 SNDRV_PCM_HW_PARAM_FORMAT, 2339 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 2340 if (err < 0) 2341 return err; 2342 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 2343 snd_pcm_hw_rule_div, NULL, 2344 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); 2345 if (err < 0) 2346 return err; 2347 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 2348 snd_pcm_hw_rule_mul, NULL, 2349 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); 2350 if (err < 0) 2351 return err; 2352 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 2353 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2354 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); 2355 if (err < 0) 2356 return err; 2357 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 2358 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2359 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1); 2360 if (err < 0) 2361 return err; 2362 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2363 snd_pcm_hw_rule_div, NULL, 2364 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 2365 if (err < 0) 2366 return err; 2367 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 2368 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2369 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1); 2370 if (err < 0) 2371 return err; 2372 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 2373 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2374 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1); 2375 if (err < 0) 2376 return err; 2377 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, 2378 snd_pcm_hw_rule_div, NULL, 2379 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); 2380 if (err < 0) 2381 return err; 2382 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2383 snd_pcm_hw_rule_div, NULL, 2384 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); 2385 if (err < 0) 2386 return err; 2387 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2388 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2389 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2390 if (err < 0) 2391 return err; 2392 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2393 snd_pcm_hw_rule_muldivk, (void*) 1000000, 2394 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); 2395 if (err < 0) 2396 return err; 2397 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 2398 snd_pcm_hw_rule_mul, NULL, 2399 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); 2400 if (err < 0) 2401 return err; 2402 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 2403 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2404 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2405 if (err < 0) 2406 return err; 2407 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 2408 snd_pcm_hw_rule_muldivk, (void*) 1000000, 2409 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); 2410 if (err < 0) 2411 return err; 2412 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 2413 snd_pcm_hw_rule_muldivk, (void*) 8, 2414 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2415 if (err < 0) 2416 return err; 2417 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2418 snd_pcm_hw_rule_muldivk, (void*) 8, 2419 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2420 if (err < 0) 2421 return err; 2422 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 2423 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2424 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); 2425 if (err < 0) 2426 return err; 2427 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 2428 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2429 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); 2430 if (err < 0) 2431 return err; 2432 return 0; 2433 } 2434 2435 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream) 2436 { 2437 struct snd_pcm_runtime *runtime = substream->runtime; 2438 struct snd_pcm_hardware *hw = &runtime->hw; 2439 int err; 2440 unsigned int mask = 0; 2441 2442 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) 2443 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED; 2444 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) 2445 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED; 2446 if (hw_support_mmap(substream)) { 2447 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) 2448 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED; 2449 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) 2450 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED; 2451 if (hw->info & SNDRV_PCM_INFO_COMPLEX) 2452 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX; 2453 } 2454 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask); 2455 if (err < 0) 2456 return err; 2457 2458 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats); 2459 if (err < 0) 2460 return err; 2461 2462 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD); 2463 if (err < 0) 2464 return err; 2465 2466 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2467 hw->channels_min, hw->channels_max); 2468 if (err < 0) 2469 return err; 2470 2471 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, 2472 hw->rate_min, hw->rate_max); 2473 if (err < 0) 2474 return err; 2475 2476 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 2477 hw->period_bytes_min, hw->period_bytes_max); 2478 if (err < 0) 2479 return err; 2480 2481 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS, 2482 hw->periods_min, hw->periods_max); 2483 if (err < 0) 2484 return err; 2485 2486 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2487 hw->period_bytes_min, hw->buffer_bytes_max); 2488 if (err < 0) 2489 return err; 2490 2491 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2492 snd_pcm_hw_rule_buffer_bytes_max, substream, 2493 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1); 2494 if (err < 0) 2495 return err; 2496 2497 /* FIXME: remove */ 2498 if (runtime->dma_bytes) { 2499 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes); 2500 if (err < 0) 2501 return err; 2502 } 2503 2504 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) { 2505 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 2506 snd_pcm_hw_rule_rate, hw, 2507 SNDRV_PCM_HW_PARAM_RATE, -1); 2508 if (err < 0) 2509 return err; 2510 } 2511 2512 /* FIXME: this belong to lowlevel */ 2513 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); 2514 2515 return 0; 2516 } 2517 2518 static void pcm_release_private(struct snd_pcm_substream *substream) 2519 { 2520 if (snd_pcm_stream_linked(substream)) 2521 snd_pcm_unlink(substream); 2522 } 2523 2524 void snd_pcm_release_substream(struct snd_pcm_substream *substream) 2525 { 2526 substream->ref_count--; 2527 if (substream->ref_count > 0) 2528 return; 2529 2530 snd_pcm_drop(substream); 2531 if (substream->hw_opened) { 2532 if (substream->ops->hw_free && 2533 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) 2534 substream->ops->hw_free(substream); 2535 substream->ops->close(substream); 2536 substream->hw_opened = 0; 2537 } 2538 if (pm_qos_request_active(&substream->latency_pm_qos_req)) 2539 pm_qos_remove_request(&substream->latency_pm_qos_req); 2540 if (substream->pcm_release) { 2541 substream->pcm_release(substream); 2542 substream->pcm_release = NULL; 2543 } 2544 snd_pcm_detach_substream(substream); 2545 } 2546 EXPORT_SYMBOL(snd_pcm_release_substream); 2547 2548 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, 2549 struct file *file, 2550 struct snd_pcm_substream **rsubstream) 2551 { 2552 struct snd_pcm_substream *substream; 2553 int err; 2554 2555 err = snd_pcm_attach_substream(pcm, stream, file, &substream); 2556 if (err < 0) 2557 return err; 2558 if (substream->ref_count > 1) { 2559 *rsubstream = substream; 2560 return 0; 2561 } 2562 2563 err = snd_pcm_hw_constraints_init(substream); 2564 if (err < 0) { 2565 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n"); 2566 goto error; 2567 } 2568 2569 if ((err = substream->ops->open(substream)) < 0) 2570 goto error; 2571 2572 substream->hw_opened = 1; 2573 2574 err = snd_pcm_hw_constraints_complete(substream); 2575 if (err < 0) { 2576 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n"); 2577 goto error; 2578 } 2579 2580 *rsubstream = substream; 2581 return 0; 2582 2583 error: 2584 snd_pcm_release_substream(substream); 2585 return err; 2586 } 2587 EXPORT_SYMBOL(snd_pcm_open_substream); 2588 2589 static int snd_pcm_open_file(struct file *file, 2590 struct snd_pcm *pcm, 2591 int stream) 2592 { 2593 struct snd_pcm_file *pcm_file; 2594 struct snd_pcm_substream *substream; 2595 int err; 2596 2597 err = snd_pcm_open_substream(pcm, stream, file, &substream); 2598 if (err < 0) 2599 return err; 2600 2601 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL); 2602 if (pcm_file == NULL) { 2603 snd_pcm_release_substream(substream); 2604 return -ENOMEM; 2605 } 2606 pcm_file->substream = substream; 2607 if (substream->ref_count == 1) 2608 substream->pcm_release = pcm_release_private; 2609 file->private_data = pcm_file; 2610 2611 return 0; 2612 } 2613 2614 static int snd_pcm_playback_open(struct inode *inode, struct file *file) 2615 { 2616 struct snd_pcm *pcm; 2617 int err = nonseekable_open(inode, file); 2618 if (err < 0) 2619 return err; 2620 pcm = snd_lookup_minor_data(iminor(inode), 2621 SNDRV_DEVICE_TYPE_PCM_PLAYBACK); 2622 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK); 2623 if (pcm) 2624 snd_card_unref(pcm->card); 2625 return err; 2626 } 2627 2628 static int snd_pcm_capture_open(struct inode *inode, struct file *file) 2629 { 2630 struct snd_pcm *pcm; 2631 int err = nonseekable_open(inode, file); 2632 if (err < 0) 2633 return err; 2634 pcm = snd_lookup_minor_data(iminor(inode), 2635 SNDRV_DEVICE_TYPE_PCM_CAPTURE); 2636 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE); 2637 if (pcm) 2638 snd_card_unref(pcm->card); 2639 return err; 2640 } 2641 2642 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream) 2643 { 2644 int err; 2645 wait_queue_entry_t wait; 2646 2647 if (pcm == NULL) { 2648 err = -ENODEV; 2649 goto __error1; 2650 } 2651 err = snd_card_file_add(pcm->card, file); 2652 if (err < 0) 2653 goto __error1; 2654 if (!try_module_get(pcm->card->module)) { 2655 err = -EFAULT; 2656 goto __error2; 2657 } 2658 init_waitqueue_entry(&wait, current); 2659 add_wait_queue(&pcm->open_wait, &wait); 2660 mutex_lock(&pcm->open_mutex); 2661 while (1) { 2662 err = snd_pcm_open_file(file, pcm, stream); 2663 if (err >= 0) 2664 break; 2665 if (err == -EAGAIN) { 2666 if (file->f_flags & O_NONBLOCK) { 2667 err = -EBUSY; 2668 break; 2669 } 2670 } else 2671 break; 2672 set_current_state(TASK_INTERRUPTIBLE); 2673 mutex_unlock(&pcm->open_mutex); 2674 schedule(); 2675 mutex_lock(&pcm->open_mutex); 2676 if (pcm->card->shutdown) { 2677 err = -ENODEV; 2678 break; 2679 } 2680 if (signal_pending(current)) { 2681 err = -ERESTARTSYS; 2682 break; 2683 } 2684 } 2685 remove_wait_queue(&pcm->open_wait, &wait); 2686 mutex_unlock(&pcm->open_mutex); 2687 if (err < 0) 2688 goto __error; 2689 return err; 2690 2691 __error: 2692 module_put(pcm->card->module); 2693 __error2: 2694 snd_card_file_remove(pcm->card, file); 2695 __error1: 2696 return err; 2697 } 2698 2699 static int snd_pcm_release(struct inode *inode, struct file *file) 2700 { 2701 struct snd_pcm *pcm; 2702 struct snd_pcm_substream *substream; 2703 struct snd_pcm_file *pcm_file; 2704 2705 pcm_file = file->private_data; 2706 substream = pcm_file->substream; 2707 if (snd_BUG_ON(!substream)) 2708 return -ENXIO; 2709 pcm = substream->pcm; 2710 mutex_lock(&pcm->open_mutex); 2711 snd_pcm_release_substream(substream); 2712 kfree(pcm_file); 2713 mutex_unlock(&pcm->open_mutex); 2714 wake_up(&pcm->open_wait); 2715 module_put(pcm->card->module); 2716 snd_card_file_remove(pcm->card, file); 2717 return 0; 2718 } 2719 2720 /* check and update PCM state; return 0 or a negative error 2721 * call this inside PCM lock 2722 */ 2723 static int do_pcm_hwsync(struct snd_pcm_substream *substream) 2724 { 2725 switch (substream->runtime->status->state) { 2726 case SNDRV_PCM_STATE_DRAINING: 2727 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 2728 return -EBADFD; 2729 /* Fall through */ 2730 case SNDRV_PCM_STATE_RUNNING: 2731 return snd_pcm_update_hw_ptr(substream); 2732 case SNDRV_PCM_STATE_PREPARED: 2733 case SNDRV_PCM_STATE_PAUSED: 2734 return 0; 2735 case SNDRV_PCM_STATE_SUSPENDED: 2736 return -ESTRPIPE; 2737 case SNDRV_PCM_STATE_XRUN: 2738 return -EPIPE; 2739 default: 2740 return -EBADFD; 2741 } 2742 } 2743 2744 /* increase the appl_ptr; returns the processed frames or a negative error */ 2745 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream, 2746 snd_pcm_uframes_t frames, 2747 snd_pcm_sframes_t avail) 2748 { 2749 struct snd_pcm_runtime *runtime = substream->runtime; 2750 snd_pcm_sframes_t appl_ptr; 2751 int ret; 2752 2753 if (avail <= 0) 2754 return 0; 2755 if (frames > (snd_pcm_uframes_t)avail) 2756 frames = avail; 2757 appl_ptr = runtime->control->appl_ptr + frames; 2758 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary) 2759 appl_ptr -= runtime->boundary; 2760 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr); 2761 return ret < 0 ? ret : frames; 2762 } 2763 2764 /* decrease the appl_ptr; returns the processed frames or zero for error */ 2765 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream, 2766 snd_pcm_uframes_t frames, 2767 snd_pcm_sframes_t avail) 2768 { 2769 struct snd_pcm_runtime *runtime = substream->runtime; 2770 snd_pcm_sframes_t appl_ptr; 2771 int ret; 2772 2773 if (avail <= 0) 2774 return 0; 2775 if (frames > (snd_pcm_uframes_t)avail) 2776 frames = avail; 2777 appl_ptr = runtime->control->appl_ptr - frames; 2778 if (appl_ptr < 0) 2779 appl_ptr += runtime->boundary; 2780 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr); 2781 /* NOTE: we return zero for errors because PulseAudio gets depressed 2782 * upon receiving an error from rewind ioctl and stops processing 2783 * any longer. Returning zero means that no rewind is done, so 2784 * it's not absolutely wrong to answer like that. 2785 */ 2786 return ret < 0 ? 0 : frames; 2787 } 2788 2789 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream, 2790 snd_pcm_uframes_t frames) 2791 { 2792 snd_pcm_sframes_t ret; 2793 2794 if (frames == 0) 2795 return 0; 2796 2797 snd_pcm_stream_lock_irq(substream); 2798 ret = do_pcm_hwsync(substream); 2799 if (!ret) 2800 ret = rewind_appl_ptr(substream, frames, 2801 snd_pcm_hw_avail(substream)); 2802 snd_pcm_stream_unlock_irq(substream); 2803 return ret; 2804 } 2805 2806 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream, 2807 snd_pcm_uframes_t frames) 2808 { 2809 snd_pcm_sframes_t ret; 2810 2811 if (frames == 0) 2812 return 0; 2813 2814 snd_pcm_stream_lock_irq(substream); 2815 ret = do_pcm_hwsync(substream); 2816 if (!ret) 2817 ret = forward_appl_ptr(substream, frames, 2818 snd_pcm_avail(substream)); 2819 snd_pcm_stream_unlock_irq(substream); 2820 return ret; 2821 } 2822 2823 static int snd_pcm_hwsync(struct snd_pcm_substream *substream) 2824 { 2825 int err; 2826 2827 snd_pcm_stream_lock_irq(substream); 2828 err = do_pcm_hwsync(substream); 2829 snd_pcm_stream_unlock_irq(substream); 2830 return err; 2831 } 2832 2833 static int snd_pcm_delay(struct snd_pcm_substream *substream, 2834 snd_pcm_sframes_t *delay) 2835 { 2836 int err; 2837 snd_pcm_sframes_t n = 0; 2838 2839 snd_pcm_stream_lock_irq(substream); 2840 err = do_pcm_hwsync(substream); 2841 if (!err) 2842 n = snd_pcm_calc_delay(substream); 2843 snd_pcm_stream_unlock_irq(substream); 2844 if (!err) 2845 *delay = n; 2846 return err; 2847 } 2848 2849 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream, 2850 struct snd_pcm_sync_ptr __user *_sync_ptr) 2851 { 2852 struct snd_pcm_runtime *runtime = substream->runtime; 2853 struct snd_pcm_sync_ptr sync_ptr; 2854 volatile struct snd_pcm_mmap_status *status; 2855 volatile struct snd_pcm_mmap_control *control; 2856 int err; 2857 2858 memset(&sync_ptr, 0, sizeof(sync_ptr)); 2859 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags))) 2860 return -EFAULT; 2861 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control))) 2862 return -EFAULT; 2863 status = runtime->status; 2864 control = runtime->control; 2865 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) { 2866 err = snd_pcm_hwsync(substream); 2867 if (err < 0) 2868 return err; 2869 } 2870 snd_pcm_stream_lock_irq(substream); 2871 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) { 2872 err = pcm_lib_apply_appl_ptr(substream, 2873 sync_ptr.c.control.appl_ptr); 2874 if (err < 0) { 2875 snd_pcm_stream_unlock_irq(substream); 2876 return err; 2877 } 2878 } else { 2879 sync_ptr.c.control.appl_ptr = control->appl_ptr; 2880 } 2881 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) 2882 control->avail_min = sync_ptr.c.control.avail_min; 2883 else 2884 sync_ptr.c.control.avail_min = control->avail_min; 2885 sync_ptr.s.status.state = status->state; 2886 sync_ptr.s.status.hw_ptr = status->hw_ptr; 2887 sync_ptr.s.status.tstamp = status->tstamp; 2888 sync_ptr.s.status.suspended_state = status->suspended_state; 2889 sync_ptr.s.status.audio_tstamp = status->audio_tstamp; 2890 snd_pcm_stream_unlock_irq(substream); 2891 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr))) 2892 return -EFAULT; 2893 return 0; 2894 } 2895 2896 struct snd_pcm_mmap_status32 { 2897 s32 state; 2898 s32 pad1; 2899 u32 hw_ptr; 2900 s32 tstamp_sec; 2901 s32 tstamp_nsec; 2902 s32 suspended_state; 2903 s32 audio_tstamp_sec; 2904 s32 audio_tstamp_nsec; 2905 } __attribute__((packed)); 2906 2907 struct snd_pcm_mmap_control32 { 2908 u32 appl_ptr; 2909 u32 avail_min; 2910 }; 2911 2912 struct snd_pcm_sync_ptr32 { 2913 u32 flags; 2914 union { 2915 struct snd_pcm_mmap_status32 status; 2916 unsigned char reserved[64]; 2917 } s; 2918 union { 2919 struct snd_pcm_mmap_control32 control; 2920 unsigned char reserved[64]; 2921 } c; 2922 } __attribute__((packed)); 2923 2924 /* recalcuate the boundary within 32bit */ 2925 static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime) 2926 { 2927 snd_pcm_uframes_t boundary; 2928 2929 if (! runtime->buffer_size) 2930 return 0; 2931 boundary = runtime->buffer_size; 2932 while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size) 2933 boundary *= 2; 2934 return boundary; 2935 } 2936 2937 static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream, 2938 struct snd_pcm_sync_ptr32 __user *src) 2939 { 2940 struct snd_pcm_runtime *runtime = substream->runtime; 2941 volatile struct snd_pcm_mmap_status *status; 2942 volatile struct snd_pcm_mmap_control *control; 2943 u32 sflags; 2944 struct snd_pcm_mmap_control scontrol; 2945 struct snd_pcm_mmap_status sstatus; 2946 snd_pcm_uframes_t boundary; 2947 int err; 2948 2949 if (snd_BUG_ON(!runtime)) 2950 return -EINVAL; 2951 2952 if (get_user(sflags, &src->flags) || 2953 get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) || 2954 get_user(scontrol.avail_min, &src->c.control.avail_min)) 2955 return -EFAULT; 2956 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) { 2957 err = snd_pcm_hwsync(substream); 2958 if (err < 0) 2959 return err; 2960 } 2961 status = runtime->status; 2962 control = runtime->control; 2963 boundary = recalculate_boundary(runtime); 2964 if (! boundary) 2965 boundary = 0x7fffffff; 2966 snd_pcm_stream_lock_irq(substream); 2967 /* FIXME: we should consider the boundary for the sync from app */ 2968 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) 2969 control->appl_ptr = scontrol.appl_ptr; 2970 else 2971 scontrol.appl_ptr = control->appl_ptr % boundary; 2972 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) 2973 control->avail_min = scontrol.avail_min; 2974 else 2975 scontrol.avail_min = control->avail_min; 2976 sstatus.state = status->state; 2977 sstatus.hw_ptr = status->hw_ptr % boundary; 2978 sstatus.tstamp = status->tstamp; 2979 sstatus.suspended_state = status->suspended_state; 2980 sstatus.audio_tstamp = status->audio_tstamp; 2981 snd_pcm_stream_unlock_irq(substream); 2982 if (put_user(sstatus.state, &src->s.status.state) || 2983 put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) || 2984 put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) || 2985 put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) || 2986 put_user(sstatus.suspended_state, &src->s.status.suspended_state) || 2987 put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec) || 2988 put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_nsec) || 2989 put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) || 2990 put_user(scontrol.avail_min, &src->c.control.avail_min)) 2991 return -EFAULT; 2992 2993 return 0; 2994 } 2995 #define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32) 2996 2997 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg) 2998 { 2999 struct snd_pcm_runtime *runtime = substream->runtime; 3000 int arg; 3001 3002 if (get_user(arg, _arg)) 3003 return -EFAULT; 3004 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST) 3005 return -EINVAL; 3006 runtime->tstamp_type = arg; 3007 return 0; 3008 } 3009 3010 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream, 3011 struct snd_xferi __user *_xferi) 3012 { 3013 struct snd_xferi xferi; 3014 struct snd_pcm_runtime *runtime = substream->runtime; 3015 snd_pcm_sframes_t result; 3016 3017 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 3018 return -EBADFD; 3019 if (put_user(0, &_xferi->result)) 3020 return -EFAULT; 3021 if (copy_from_user(&xferi, _xferi, sizeof(xferi))) 3022 return -EFAULT; 3023 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3024 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames); 3025 else 3026 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames); 3027 __put_user(result, &_xferi->result); 3028 return result < 0 ? result : 0; 3029 } 3030 3031 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream, 3032 struct snd_xfern __user *_xfern) 3033 { 3034 struct snd_xfern xfern; 3035 struct snd_pcm_runtime *runtime = substream->runtime; 3036 void *bufs; 3037 snd_pcm_sframes_t result; 3038 3039 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 3040 return -EBADFD; 3041 if (runtime->channels > 128) 3042 return -EINVAL; 3043 if (put_user(0, &_xfern->result)) 3044 return -EFAULT; 3045 if (copy_from_user(&xfern, _xfern, sizeof(xfern))) 3046 return -EFAULT; 3047 3048 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels); 3049 if (IS_ERR(bufs)) 3050 return PTR_ERR(bufs); 3051 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3052 result = snd_pcm_lib_writev(substream, bufs, xfern.frames); 3053 else 3054 result = snd_pcm_lib_readv(substream, bufs, xfern.frames); 3055 kfree(bufs); 3056 __put_user(result, &_xfern->result); 3057 return result < 0 ? result : 0; 3058 } 3059 3060 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream, 3061 snd_pcm_uframes_t __user *_frames) 3062 { 3063 snd_pcm_uframes_t frames; 3064 snd_pcm_sframes_t result; 3065 3066 if (get_user(frames, _frames)) 3067 return -EFAULT; 3068 if (put_user(0, _frames)) 3069 return -EFAULT; 3070 result = snd_pcm_rewind(substream, frames); 3071 __put_user(result, _frames); 3072 return result < 0 ? result : 0; 3073 } 3074 3075 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream, 3076 snd_pcm_uframes_t __user *_frames) 3077 { 3078 snd_pcm_uframes_t frames; 3079 snd_pcm_sframes_t result; 3080 3081 if (get_user(frames, _frames)) 3082 return -EFAULT; 3083 if (put_user(0, _frames)) 3084 return -EFAULT; 3085 result = snd_pcm_forward(substream, frames); 3086 __put_user(result, _frames); 3087 return result < 0 ? result : 0; 3088 } 3089 3090 static int snd_pcm_common_ioctl(struct file *file, 3091 struct snd_pcm_substream *substream, 3092 unsigned int cmd, void __user *arg) 3093 { 3094 struct snd_pcm_file *pcm_file = file->private_data; 3095 int res; 3096 3097 if (PCM_RUNTIME_CHECK(substream)) 3098 return -ENXIO; 3099 3100 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0); 3101 if (res < 0) 3102 return res; 3103 3104 switch (cmd) { 3105 case SNDRV_PCM_IOCTL_PVERSION: 3106 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0; 3107 case SNDRV_PCM_IOCTL_INFO: 3108 return snd_pcm_info_user(substream, arg); 3109 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */ 3110 return 0; 3111 case SNDRV_PCM_IOCTL_TTSTAMP: 3112 return snd_pcm_tstamp(substream, arg); 3113 case SNDRV_PCM_IOCTL_USER_PVERSION: 3114 if (get_user(pcm_file->user_pversion, 3115 (unsigned int __user *)arg)) 3116 return -EFAULT; 3117 return 0; 3118 case SNDRV_PCM_IOCTL_HW_REFINE: 3119 return snd_pcm_hw_refine_user(substream, arg); 3120 case SNDRV_PCM_IOCTL_HW_PARAMS: 3121 return snd_pcm_hw_params_user(substream, arg); 3122 case SNDRV_PCM_IOCTL_HW_FREE: 3123 return snd_pcm_hw_free(substream); 3124 case SNDRV_PCM_IOCTL_SW_PARAMS: 3125 return snd_pcm_sw_params_user(substream, arg); 3126 case SNDRV_PCM_IOCTL_STATUS32: 3127 return snd_pcm_status_user32(substream, arg, false); 3128 case SNDRV_PCM_IOCTL_STATUS_EXT32: 3129 return snd_pcm_status_user32(substream, arg, true); 3130 case SNDRV_PCM_IOCTL_STATUS64: 3131 return snd_pcm_status_user64(substream, arg, false); 3132 case SNDRV_PCM_IOCTL_STATUS_EXT64: 3133 return snd_pcm_status_user64(substream, arg, true); 3134 case SNDRV_PCM_IOCTL_CHANNEL_INFO: 3135 return snd_pcm_channel_info_user(substream, arg); 3136 case SNDRV_PCM_IOCTL_PREPARE: 3137 return snd_pcm_prepare(substream, file); 3138 case SNDRV_PCM_IOCTL_RESET: 3139 return snd_pcm_reset(substream); 3140 case SNDRV_PCM_IOCTL_START: 3141 return snd_pcm_start_lock_irq(substream); 3142 case SNDRV_PCM_IOCTL_LINK: 3143 return snd_pcm_link(substream, (int)(unsigned long) arg); 3144 case SNDRV_PCM_IOCTL_UNLINK: 3145 return snd_pcm_unlink(substream); 3146 case SNDRV_PCM_IOCTL_RESUME: 3147 return snd_pcm_resume(substream); 3148 case SNDRV_PCM_IOCTL_XRUN: 3149 return snd_pcm_xrun(substream); 3150 case SNDRV_PCM_IOCTL_HWSYNC: 3151 return snd_pcm_hwsync(substream); 3152 case SNDRV_PCM_IOCTL_DELAY: 3153 { 3154 snd_pcm_sframes_t delay; 3155 snd_pcm_sframes_t __user *res = arg; 3156 int err; 3157 3158 err = snd_pcm_delay(substream, &delay); 3159 if (err) 3160 return err; 3161 if (put_user(delay, res)) 3162 return -EFAULT; 3163 return 0; 3164 } 3165 case __SNDRV_PCM_IOCTL_SYNC_PTR32: 3166 return snd_pcm_ioctl_sync_ptr_compat(substream, arg); 3167 case __SNDRV_PCM_IOCTL_SYNC_PTR64: 3168 return snd_pcm_sync_ptr(substream, arg); 3169 #ifdef CONFIG_SND_SUPPORT_OLD_API 3170 case SNDRV_PCM_IOCTL_HW_REFINE_OLD: 3171 return snd_pcm_hw_refine_old_user(substream, arg); 3172 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD: 3173 return snd_pcm_hw_params_old_user(substream, arg); 3174 #endif 3175 case SNDRV_PCM_IOCTL_DRAIN: 3176 return snd_pcm_drain(substream, file); 3177 case SNDRV_PCM_IOCTL_DROP: 3178 return snd_pcm_drop(substream); 3179 case SNDRV_PCM_IOCTL_PAUSE: 3180 return snd_pcm_action_lock_irq(&snd_pcm_action_pause, 3181 substream, 3182 (int)(unsigned long)arg); 3183 case SNDRV_PCM_IOCTL_WRITEI_FRAMES: 3184 case SNDRV_PCM_IOCTL_READI_FRAMES: 3185 return snd_pcm_xferi_frames_ioctl(substream, arg); 3186 case SNDRV_PCM_IOCTL_WRITEN_FRAMES: 3187 case SNDRV_PCM_IOCTL_READN_FRAMES: 3188 return snd_pcm_xfern_frames_ioctl(substream, arg); 3189 case SNDRV_PCM_IOCTL_REWIND: 3190 return snd_pcm_rewind_ioctl(substream, arg); 3191 case SNDRV_PCM_IOCTL_FORWARD: 3192 return snd_pcm_forward_ioctl(substream, arg); 3193 } 3194 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd); 3195 return -ENOTTY; 3196 } 3197 3198 static long snd_pcm_ioctl(struct file *file, unsigned int cmd, 3199 unsigned long arg) 3200 { 3201 struct snd_pcm_file *pcm_file; 3202 3203 pcm_file = file->private_data; 3204 3205 if (((cmd >> 8) & 0xff) != 'A') 3206 return -ENOTTY; 3207 3208 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd, 3209 (void __user *)arg); 3210 } 3211 3212 /** 3213 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space 3214 * @substream: PCM substream 3215 * @cmd: IOCTL cmd 3216 * @arg: IOCTL argument 3217 * 3218 * The function is provided primarily for OSS layer and USB gadget drivers, 3219 * and it allows only the limited set of ioctls (hw_params, sw_params, 3220 * prepare, start, drain, drop, forward). 3221 */ 3222 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, 3223 unsigned int cmd, void *arg) 3224 { 3225 snd_pcm_uframes_t *frames = arg; 3226 snd_pcm_sframes_t result; 3227 3228 switch (cmd) { 3229 case SNDRV_PCM_IOCTL_FORWARD: 3230 { 3231 /* provided only for OSS; capture-only and no value returned */ 3232 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE) 3233 return -EINVAL; 3234 result = snd_pcm_forward(substream, *frames); 3235 return result < 0 ? result : 0; 3236 } 3237 case SNDRV_PCM_IOCTL_HW_PARAMS: 3238 return snd_pcm_hw_params(substream, arg); 3239 case SNDRV_PCM_IOCTL_SW_PARAMS: 3240 return snd_pcm_sw_params(substream, arg); 3241 case SNDRV_PCM_IOCTL_PREPARE: 3242 return snd_pcm_prepare(substream, NULL); 3243 case SNDRV_PCM_IOCTL_START: 3244 return snd_pcm_start_lock_irq(substream); 3245 case SNDRV_PCM_IOCTL_DRAIN: 3246 return snd_pcm_drain(substream, NULL); 3247 case SNDRV_PCM_IOCTL_DROP: 3248 return snd_pcm_drop(substream); 3249 case SNDRV_PCM_IOCTL_DELAY: 3250 return snd_pcm_delay(substream, frames); 3251 default: 3252 return -EINVAL; 3253 } 3254 } 3255 EXPORT_SYMBOL(snd_pcm_kernel_ioctl); 3256 3257 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, 3258 loff_t * offset) 3259 { 3260 struct snd_pcm_file *pcm_file; 3261 struct snd_pcm_substream *substream; 3262 struct snd_pcm_runtime *runtime; 3263 snd_pcm_sframes_t result; 3264 3265 pcm_file = file->private_data; 3266 substream = pcm_file->substream; 3267 if (PCM_RUNTIME_CHECK(substream)) 3268 return -ENXIO; 3269 runtime = substream->runtime; 3270 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 3271 return -EBADFD; 3272 if (!frame_aligned(runtime, count)) 3273 return -EINVAL; 3274 count = bytes_to_frames(runtime, count); 3275 result = snd_pcm_lib_read(substream, buf, count); 3276 if (result > 0) 3277 result = frames_to_bytes(runtime, result); 3278 return result; 3279 } 3280 3281 static ssize_t snd_pcm_write(struct file *file, const char __user *buf, 3282 size_t count, loff_t * offset) 3283 { 3284 struct snd_pcm_file *pcm_file; 3285 struct snd_pcm_substream *substream; 3286 struct snd_pcm_runtime *runtime; 3287 snd_pcm_sframes_t result; 3288 3289 pcm_file = file->private_data; 3290 substream = pcm_file->substream; 3291 if (PCM_RUNTIME_CHECK(substream)) 3292 return -ENXIO; 3293 runtime = substream->runtime; 3294 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 3295 return -EBADFD; 3296 if (!frame_aligned(runtime, count)) 3297 return -EINVAL; 3298 count = bytes_to_frames(runtime, count); 3299 result = snd_pcm_lib_write(substream, buf, count); 3300 if (result > 0) 3301 result = frames_to_bytes(runtime, result); 3302 return result; 3303 } 3304 3305 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to) 3306 { 3307 struct snd_pcm_file *pcm_file; 3308 struct snd_pcm_substream *substream; 3309 struct snd_pcm_runtime *runtime; 3310 snd_pcm_sframes_t result; 3311 unsigned long i; 3312 void __user **bufs; 3313 snd_pcm_uframes_t frames; 3314 3315 pcm_file = iocb->ki_filp->private_data; 3316 substream = pcm_file->substream; 3317 if (PCM_RUNTIME_CHECK(substream)) 3318 return -ENXIO; 3319 runtime = substream->runtime; 3320 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 3321 return -EBADFD; 3322 if (!iter_is_iovec(to)) 3323 return -EINVAL; 3324 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels) 3325 return -EINVAL; 3326 if (!frame_aligned(runtime, to->iov->iov_len)) 3327 return -EINVAL; 3328 frames = bytes_to_samples(runtime, to->iov->iov_len); 3329 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL); 3330 if (bufs == NULL) 3331 return -ENOMEM; 3332 for (i = 0; i < to->nr_segs; ++i) 3333 bufs[i] = to->iov[i].iov_base; 3334 result = snd_pcm_lib_readv(substream, bufs, frames); 3335 if (result > 0) 3336 result = frames_to_bytes(runtime, result); 3337 kfree(bufs); 3338 return result; 3339 } 3340 3341 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from) 3342 { 3343 struct snd_pcm_file *pcm_file; 3344 struct snd_pcm_substream *substream; 3345 struct snd_pcm_runtime *runtime; 3346 snd_pcm_sframes_t result; 3347 unsigned long i; 3348 void __user **bufs; 3349 snd_pcm_uframes_t frames; 3350 3351 pcm_file = iocb->ki_filp->private_data; 3352 substream = pcm_file->substream; 3353 if (PCM_RUNTIME_CHECK(substream)) 3354 return -ENXIO; 3355 runtime = substream->runtime; 3356 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 3357 return -EBADFD; 3358 if (!iter_is_iovec(from)) 3359 return -EINVAL; 3360 if (from->nr_segs > 128 || from->nr_segs != runtime->channels || 3361 !frame_aligned(runtime, from->iov->iov_len)) 3362 return -EINVAL; 3363 frames = bytes_to_samples(runtime, from->iov->iov_len); 3364 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL); 3365 if (bufs == NULL) 3366 return -ENOMEM; 3367 for (i = 0; i < from->nr_segs; ++i) 3368 bufs[i] = from->iov[i].iov_base; 3369 result = snd_pcm_lib_writev(substream, bufs, frames); 3370 if (result > 0) 3371 result = frames_to_bytes(runtime, result); 3372 kfree(bufs); 3373 return result; 3374 } 3375 3376 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait) 3377 { 3378 struct snd_pcm_file *pcm_file; 3379 struct snd_pcm_substream *substream; 3380 struct snd_pcm_runtime *runtime; 3381 __poll_t mask, ok; 3382 snd_pcm_uframes_t avail; 3383 3384 pcm_file = file->private_data; 3385 3386 substream = pcm_file->substream; 3387 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3388 ok = EPOLLOUT | EPOLLWRNORM; 3389 else 3390 ok = EPOLLIN | EPOLLRDNORM; 3391 if (PCM_RUNTIME_CHECK(substream)) 3392 return ok | EPOLLERR; 3393 3394 runtime = substream->runtime; 3395 poll_wait(file, &runtime->sleep, wait); 3396 3397 mask = 0; 3398 snd_pcm_stream_lock_irq(substream); 3399 avail = snd_pcm_avail(substream); 3400 switch (runtime->status->state) { 3401 case SNDRV_PCM_STATE_RUNNING: 3402 case SNDRV_PCM_STATE_PREPARED: 3403 case SNDRV_PCM_STATE_PAUSED: 3404 if (avail >= runtime->control->avail_min) 3405 mask = ok; 3406 break; 3407 case SNDRV_PCM_STATE_DRAINING: 3408 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 3409 mask = ok; 3410 if (!avail) 3411 mask |= EPOLLERR; 3412 } 3413 break; 3414 default: 3415 mask = ok | EPOLLERR; 3416 break; 3417 } 3418 snd_pcm_stream_unlock_irq(substream); 3419 return mask; 3420 } 3421 3422 /* 3423 * mmap support 3424 */ 3425 3426 /* 3427 * Only on coherent architectures, we can mmap the status and the control records 3428 * for effcient data transfer. On others, we have to use HWSYNC ioctl... 3429 */ 3430 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA) 3431 /* 3432 * mmap status record 3433 */ 3434 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf) 3435 { 3436 struct snd_pcm_substream *substream = vmf->vma->vm_private_data; 3437 struct snd_pcm_runtime *runtime; 3438 3439 if (substream == NULL) 3440 return VM_FAULT_SIGBUS; 3441 runtime = substream->runtime; 3442 vmf->page = virt_to_page(runtime->status); 3443 get_page(vmf->page); 3444 return 0; 3445 } 3446 3447 static const struct vm_operations_struct snd_pcm_vm_ops_status = 3448 { 3449 .fault = snd_pcm_mmap_status_fault, 3450 }; 3451 3452 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, 3453 struct vm_area_struct *area) 3454 { 3455 long size; 3456 if (!(area->vm_flags & VM_READ)) 3457 return -EINVAL; 3458 size = area->vm_end - area->vm_start; 3459 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))) 3460 return -EINVAL; 3461 area->vm_ops = &snd_pcm_vm_ops_status; 3462 area->vm_private_data = substream; 3463 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 3464 return 0; 3465 } 3466 3467 /* 3468 * mmap control record 3469 */ 3470 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf) 3471 { 3472 struct snd_pcm_substream *substream = vmf->vma->vm_private_data; 3473 struct snd_pcm_runtime *runtime; 3474 3475 if (substream == NULL) 3476 return VM_FAULT_SIGBUS; 3477 runtime = substream->runtime; 3478 vmf->page = virt_to_page(runtime->control); 3479 get_page(vmf->page); 3480 return 0; 3481 } 3482 3483 static const struct vm_operations_struct snd_pcm_vm_ops_control = 3484 { 3485 .fault = snd_pcm_mmap_control_fault, 3486 }; 3487 3488 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, 3489 struct vm_area_struct *area) 3490 { 3491 long size; 3492 if (!(area->vm_flags & VM_READ)) 3493 return -EINVAL; 3494 size = area->vm_end - area->vm_start; 3495 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control))) 3496 return -EINVAL; 3497 area->vm_ops = &snd_pcm_vm_ops_control; 3498 area->vm_private_data = substream; 3499 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 3500 return 0; 3501 } 3502 3503 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file) 3504 { 3505 /* See pcm_control_mmap_allowed() below. 3506 * Since older alsa-lib requires both status and control mmaps to be 3507 * coupled, we have to disable the status mmap for old alsa-lib, too. 3508 */ 3509 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) && 3510 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)) 3511 return false; 3512 return true; 3513 } 3514 3515 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file) 3516 { 3517 if (pcm_file->no_compat_mmap) 3518 return false; 3519 /* Disallow the control mmap when SYNC_APPLPTR flag is set; 3520 * it enforces the user-space to fall back to snd_pcm_sync_ptr(), 3521 * thus it effectively assures the manual update of appl_ptr. 3522 */ 3523 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR) 3524 return false; 3525 return true; 3526 } 3527 3528 #else /* ! coherent mmap */ 3529 /* 3530 * don't support mmap for status and control records. 3531 */ 3532 #define pcm_status_mmap_allowed(pcm_file) false 3533 #define pcm_control_mmap_allowed(pcm_file) false 3534 3535 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, 3536 struct vm_area_struct *area) 3537 { 3538 return -ENXIO; 3539 } 3540 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, 3541 struct vm_area_struct *area) 3542 { 3543 return -ENXIO; 3544 } 3545 #endif /* coherent mmap */ 3546 3547 static inline struct page * 3548 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs) 3549 { 3550 void *vaddr = substream->runtime->dma_area + ofs; 3551 3552 switch (substream->dma_buffer.dev.type) { 3553 #ifdef CONFIG_SND_DMA_SGBUF 3554 case SNDRV_DMA_TYPE_DEV_SG: 3555 case SNDRV_DMA_TYPE_DEV_UC_SG: 3556 return snd_pcm_sgbuf_ops_page(substream, ofs); 3557 #endif /* CONFIG_SND_DMA_SGBUF */ 3558 case SNDRV_DMA_TYPE_VMALLOC: 3559 return vmalloc_to_page(vaddr); 3560 default: 3561 return virt_to_page(vaddr); 3562 } 3563 } 3564 3565 /* 3566 * fault callback for mmapping a RAM page 3567 */ 3568 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf) 3569 { 3570 struct snd_pcm_substream *substream = vmf->vma->vm_private_data; 3571 struct snd_pcm_runtime *runtime; 3572 unsigned long offset; 3573 struct page * page; 3574 size_t dma_bytes; 3575 3576 if (substream == NULL) 3577 return VM_FAULT_SIGBUS; 3578 runtime = substream->runtime; 3579 offset = vmf->pgoff << PAGE_SHIFT; 3580 dma_bytes = PAGE_ALIGN(runtime->dma_bytes); 3581 if (offset > dma_bytes - PAGE_SIZE) 3582 return VM_FAULT_SIGBUS; 3583 if (substream->ops->page) 3584 page = substream->ops->page(substream, offset); 3585 else 3586 page = snd_pcm_default_page_ops(substream, offset); 3587 if (!page) 3588 return VM_FAULT_SIGBUS; 3589 get_page(page); 3590 vmf->page = page; 3591 return 0; 3592 } 3593 3594 static const struct vm_operations_struct snd_pcm_vm_ops_data = { 3595 .open = snd_pcm_mmap_data_open, 3596 .close = snd_pcm_mmap_data_close, 3597 }; 3598 3599 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = { 3600 .open = snd_pcm_mmap_data_open, 3601 .close = snd_pcm_mmap_data_close, 3602 .fault = snd_pcm_mmap_data_fault, 3603 }; 3604 3605 /* 3606 * mmap the DMA buffer on RAM 3607 */ 3608 3609 /** 3610 * snd_pcm_lib_default_mmap - Default PCM data mmap function 3611 * @substream: PCM substream 3612 * @area: VMA 3613 * 3614 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL, 3615 * this function is invoked implicitly. 3616 */ 3617 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream, 3618 struct vm_area_struct *area) 3619 { 3620 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 3621 #ifdef CONFIG_GENERIC_ALLOCATOR 3622 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) { 3623 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot); 3624 return remap_pfn_range(area, area->vm_start, 3625 substream->dma_buffer.addr >> PAGE_SHIFT, 3626 area->vm_end - area->vm_start, area->vm_page_prot); 3627 } 3628 #endif /* CONFIG_GENERIC_ALLOCATOR */ 3629 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */ 3630 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page && 3631 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV || 3632 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC)) 3633 return dma_mmap_coherent(substream->dma_buffer.dev.dev, 3634 area, 3635 substream->runtime->dma_area, 3636 substream->runtime->dma_addr, 3637 substream->runtime->dma_bytes); 3638 #endif /* CONFIG_X86 */ 3639 /* mmap with fault handler */ 3640 area->vm_ops = &snd_pcm_vm_ops_data_fault; 3641 return 0; 3642 } 3643 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap); 3644 3645 /* 3646 * mmap the DMA buffer on I/O memory area 3647 */ 3648 #if SNDRV_PCM_INFO_MMAP_IOMEM 3649 /** 3650 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem 3651 * @substream: PCM substream 3652 * @area: VMA 3653 * 3654 * When your hardware uses the iomapped pages as the hardware buffer and 3655 * wants to mmap it, pass this function as mmap pcm_ops. Note that this 3656 * is supposed to work only on limited architectures. 3657 */ 3658 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, 3659 struct vm_area_struct *area) 3660 { 3661 struct snd_pcm_runtime *runtime = substream->runtime; 3662 3663 area->vm_page_prot = pgprot_noncached(area->vm_page_prot); 3664 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes); 3665 } 3666 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem); 3667 #endif /* SNDRV_PCM_INFO_MMAP */ 3668 3669 /* 3670 * mmap DMA buffer 3671 */ 3672 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, 3673 struct vm_area_struct *area) 3674 { 3675 struct snd_pcm_runtime *runtime; 3676 long size; 3677 unsigned long offset; 3678 size_t dma_bytes; 3679 int err; 3680 3681 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 3682 if (!(area->vm_flags & (VM_WRITE|VM_READ))) 3683 return -EINVAL; 3684 } else { 3685 if (!(area->vm_flags & VM_READ)) 3686 return -EINVAL; 3687 } 3688 runtime = substream->runtime; 3689 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) 3690 return -EBADFD; 3691 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) 3692 return -ENXIO; 3693 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || 3694 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) 3695 return -EINVAL; 3696 size = area->vm_end - area->vm_start; 3697 offset = area->vm_pgoff << PAGE_SHIFT; 3698 dma_bytes = PAGE_ALIGN(runtime->dma_bytes); 3699 if ((size_t)size > dma_bytes) 3700 return -EINVAL; 3701 if (offset > dma_bytes - size) 3702 return -EINVAL; 3703 3704 area->vm_ops = &snd_pcm_vm_ops_data; 3705 area->vm_private_data = substream; 3706 if (substream->ops->mmap) 3707 err = substream->ops->mmap(substream, area); 3708 else 3709 err = snd_pcm_lib_default_mmap(substream, area); 3710 if (!err) 3711 atomic_inc(&substream->mmap_count); 3712 return err; 3713 } 3714 EXPORT_SYMBOL(snd_pcm_mmap_data); 3715 3716 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area) 3717 { 3718 struct snd_pcm_file * pcm_file; 3719 struct snd_pcm_substream *substream; 3720 unsigned long offset; 3721 3722 pcm_file = file->private_data; 3723 substream = pcm_file->substream; 3724 if (PCM_RUNTIME_CHECK(substream)) 3725 return -ENXIO; 3726 3727 offset = area->vm_pgoff << PAGE_SHIFT; 3728 switch (offset) { 3729 case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD: 3730 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT)) 3731 return -ENXIO; 3732 /* fallthrough */ 3733 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW: 3734 if (!pcm_status_mmap_allowed(pcm_file)) 3735 return -ENXIO; 3736 return snd_pcm_mmap_status(substream, file, area); 3737 case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD: 3738 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT)) 3739 return -ENXIO; 3740 /* fallthrough */ 3741 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW: 3742 if (!pcm_control_mmap_allowed(pcm_file)) 3743 return -ENXIO; 3744 return snd_pcm_mmap_control(substream, file, area); 3745 default: 3746 return snd_pcm_mmap_data(substream, file, area); 3747 } 3748 return 0; 3749 } 3750 3751 static int snd_pcm_fasync(int fd, struct file * file, int on) 3752 { 3753 struct snd_pcm_file * pcm_file; 3754 struct snd_pcm_substream *substream; 3755 struct snd_pcm_runtime *runtime; 3756 3757 pcm_file = file->private_data; 3758 substream = pcm_file->substream; 3759 if (PCM_RUNTIME_CHECK(substream)) 3760 return -ENXIO; 3761 runtime = substream->runtime; 3762 return fasync_helper(fd, file, on, &runtime->fasync); 3763 } 3764 3765 /* 3766 * ioctl32 compat 3767 */ 3768 #ifdef CONFIG_COMPAT 3769 #include "pcm_compat.c" 3770 #else 3771 #define snd_pcm_ioctl_compat NULL 3772 #endif 3773 3774 /* 3775 * To be removed helpers to keep binary compatibility 3776 */ 3777 3778 #ifdef CONFIG_SND_SUPPORT_OLD_API 3779 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5)) 3780 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5)) 3781 3782 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params, 3783 struct snd_pcm_hw_params_old *oparams) 3784 { 3785 unsigned int i; 3786 3787 memset(params, 0, sizeof(*params)); 3788 params->flags = oparams->flags; 3789 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) 3790 params->masks[i].bits[0] = oparams->masks[i]; 3791 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals)); 3792 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask); 3793 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask); 3794 params->info = oparams->info; 3795 params->msbits = oparams->msbits; 3796 params->rate_num = oparams->rate_num; 3797 params->rate_den = oparams->rate_den; 3798 params->fifo_size = oparams->fifo_size; 3799 } 3800 3801 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams, 3802 struct snd_pcm_hw_params *params) 3803 { 3804 unsigned int i; 3805 3806 memset(oparams, 0, sizeof(*oparams)); 3807 oparams->flags = params->flags; 3808 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) 3809 oparams->masks[i] = params->masks[i].bits[0]; 3810 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals)); 3811 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask); 3812 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask); 3813 oparams->info = params->info; 3814 oparams->msbits = params->msbits; 3815 oparams->rate_num = params->rate_num; 3816 oparams->rate_den = params->rate_den; 3817 oparams->fifo_size = params->fifo_size; 3818 } 3819 3820 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, 3821 struct snd_pcm_hw_params_old __user * _oparams) 3822 { 3823 struct snd_pcm_hw_params *params; 3824 struct snd_pcm_hw_params_old *oparams = NULL; 3825 int err; 3826 3827 params = kmalloc(sizeof(*params), GFP_KERNEL); 3828 if (!params) 3829 return -ENOMEM; 3830 3831 oparams = memdup_user(_oparams, sizeof(*oparams)); 3832 if (IS_ERR(oparams)) { 3833 err = PTR_ERR(oparams); 3834 goto out; 3835 } 3836 snd_pcm_hw_convert_from_old_params(params, oparams); 3837 err = snd_pcm_hw_refine(substream, params); 3838 if (err < 0) 3839 goto out_old; 3840 3841 err = fixup_unreferenced_params(substream, params); 3842 if (err < 0) 3843 goto out_old; 3844 3845 snd_pcm_hw_convert_to_old_params(oparams, params); 3846 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) 3847 err = -EFAULT; 3848 out_old: 3849 kfree(oparams); 3850 out: 3851 kfree(params); 3852 return err; 3853 } 3854 3855 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, 3856 struct snd_pcm_hw_params_old __user * _oparams) 3857 { 3858 struct snd_pcm_hw_params *params; 3859 struct snd_pcm_hw_params_old *oparams = NULL; 3860 int err; 3861 3862 params = kmalloc(sizeof(*params), GFP_KERNEL); 3863 if (!params) 3864 return -ENOMEM; 3865 3866 oparams = memdup_user(_oparams, sizeof(*oparams)); 3867 if (IS_ERR(oparams)) { 3868 err = PTR_ERR(oparams); 3869 goto out; 3870 } 3871 3872 snd_pcm_hw_convert_from_old_params(params, oparams); 3873 err = snd_pcm_hw_params(substream, params); 3874 if (err < 0) 3875 goto out_old; 3876 3877 snd_pcm_hw_convert_to_old_params(oparams, params); 3878 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) 3879 err = -EFAULT; 3880 out_old: 3881 kfree(oparams); 3882 out: 3883 kfree(params); 3884 return err; 3885 } 3886 #endif /* CONFIG_SND_SUPPORT_OLD_API */ 3887 3888 #ifndef CONFIG_MMU 3889 static unsigned long snd_pcm_get_unmapped_area(struct file *file, 3890 unsigned long addr, 3891 unsigned long len, 3892 unsigned long pgoff, 3893 unsigned long flags) 3894 { 3895 struct snd_pcm_file *pcm_file = file->private_data; 3896 struct snd_pcm_substream *substream = pcm_file->substream; 3897 struct snd_pcm_runtime *runtime = substream->runtime; 3898 unsigned long offset = pgoff << PAGE_SHIFT; 3899 3900 switch (offset) { 3901 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW: 3902 return (unsigned long)runtime->status; 3903 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW: 3904 return (unsigned long)runtime->control; 3905 default: 3906 return (unsigned long)runtime->dma_area + offset; 3907 } 3908 } 3909 #else 3910 # define snd_pcm_get_unmapped_area NULL 3911 #endif 3912 3913 /* 3914 * Register section 3915 */ 3916 3917 const struct file_operations snd_pcm_f_ops[2] = { 3918 { 3919 .owner = THIS_MODULE, 3920 .write = snd_pcm_write, 3921 .write_iter = snd_pcm_writev, 3922 .open = snd_pcm_playback_open, 3923 .release = snd_pcm_release, 3924 .llseek = no_llseek, 3925 .poll = snd_pcm_poll, 3926 .unlocked_ioctl = snd_pcm_ioctl, 3927 .compat_ioctl = snd_pcm_ioctl_compat, 3928 .mmap = snd_pcm_mmap, 3929 .fasync = snd_pcm_fasync, 3930 .get_unmapped_area = snd_pcm_get_unmapped_area, 3931 }, 3932 { 3933 .owner = THIS_MODULE, 3934 .read = snd_pcm_read, 3935 .read_iter = snd_pcm_readv, 3936 .open = snd_pcm_capture_open, 3937 .release = snd_pcm_release, 3938 .llseek = no_llseek, 3939 .poll = snd_pcm_poll, 3940 .unlocked_ioctl = snd_pcm_ioctl, 3941 .compat_ioctl = snd_pcm_ioctl_compat, 3942 .mmap = snd_pcm_mmap, 3943 .fasync = snd_pcm_fasync, 3944 .get_unmapped_area = snd_pcm_get_unmapped_area, 3945 } 3946 }; 3947