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