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