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_group *group; 2371 bool nonatomic = substream->pcm->nonatomic; 2372 bool do_free = false; 2373 2374 guard(rwsem_write)(&snd_pcm_link_rwsem); 2375 2376 if (!snd_pcm_stream_linked(substream)) 2377 return -EALREADY; 2378 2379 group = substream->group; 2380 snd_pcm_group_lock_irq(group, nonatomic); 2381 2382 relink_to_local(substream); 2383 refcount_dec(&group->refs); 2384 2385 /* detach the last stream, too */ 2386 if (list_is_singular(&group->substreams)) { 2387 relink_to_local(list_first_entry(&group->substreams, 2388 struct snd_pcm_substream, 2389 link_list)); 2390 do_free = refcount_dec_and_test(&group->refs); 2391 } 2392 2393 snd_pcm_group_unlock_irq(group, nonatomic); 2394 if (do_free) 2395 kfree(group); 2396 return 0; 2397 } 2398 2399 /* 2400 * hw configurator 2401 */ 2402 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params, 2403 struct snd_pcm_hw_rule *rule) 2404 { 2405 struct snd_interval t; 2406 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]), 2407 hw_param_interval_c(params, rule->deps[1]), &t); 2408 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2409 } 2410 2411 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params, 2412 struct snd_pcm_hw_rule *rule) 2413 { 2414 struct snd_interval t; 2415 snd_interval_div(hw_param_interval_c(params, rule->deps[0]), 2416 hw_param_interval_c(params, rule->deps[1]), &t); 2417 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2418 } 2419 2420 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params, 2421 struct snd_pcm_hw_rule *rule) 2422 { 2423 struct snd_interval t; 2424 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]), 2425 hw_param_interval_c(params, rule->deps[1]), 2426 (unsigned long) rule->private, &t); 2427 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2428 } 2429 2430 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params, 2431 struct snd_pcm_hw_rule *rule) 2432 { 2433 struct snd_interval t; 2434 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]), 2435 (unsigned long) rule->private, 2436 hw_param_interval_c(params, rule->deps[1]), &t); 2437 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2438 } 2439 2440 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params, 2441 struct snd_pcm_hw_rule *rule) 2442 { 2443 snd_pcm_format_t k; 2444 const struct snd_interval *i = 2445 hw_param_interval_c(params, rule->deps[0]); 2446 struct snd_mask m; 2447 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 2448 snd_mask_any(&m); 2449 pcm_for_each_format(k) { 2450 int bits; 2451 if (!snd_mask_test_format(mask, k)) 2452 continue; 2453 bits = snd_pcm_format_physical_width(k); 2454 if (bits <= 0) 2455 continue; /* ignore invalid formats */ 2456 if ((unsigned)bits < i->min || (unsigned)bits > i->max) 2457 snd_mask_reset(&m, (__force unsigned)k); 2458 } 2459 return snd_mask_refine(mask, &m); 2460 } 2461 2462 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params, 2463 struct snd_pcm_hw_rule *rule) 2464 { 2465 struct snd_interval t; 2466 snd_pcm_format_t k; 2467 2468 t.min = UINT_MAX; 2469 t.max = 0; 2470 t.openmin = 0; 2471 t.openmax = 0; 2472 pcm_for_each_format(k) { 2473 int bits; 2474 if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k)) 2475 continue; 2476 bits = snd_pcm_format_physical_width(k); 2477 if (bits <= 0) 2478 continue; /* ignore invalid formats */ 2479 if (t.min > (unsigned)bits) 2480 t.min = bits; 2481 if (t.max < (unsigned)bits) 2482 t.max = bits; 2483 } 2484 t.integer = 1; 2485 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2486 } 2487 2488 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 ||\ 2489 SNDRV_PCM_RATE_128000 != 1 << 19 2490 #error "Change this table" 2491 #endif 2492 2493 /* NOTE: the list is unsorted! */ 2494 static const unsigned int rates[] = { 2495 5512, 8000, 11025, 16000, 22050, 32000, 44100, 2496 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000, 705600, 768000, 2497 /* extended */ 2498 12000, 24000, 128000 2499 }; 2500 2501 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = { 2502 .count = ARRAY_SIZE(rates), 2503 .list = rates, 2504 }; 2505 2506 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params, 2507 struct snd_pcm_hw_rule *rule) 2508 { 2509 struct snd_pcm_hardware *hw = rule->private; 2510 return snd_interval_list(hw_param_interval(params, rule->var), 2511 snd_pcm_known_rates.count, 2512 snd_pcm_known_rates.list, hw->rates); 2513 } 2514 2515 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params, 2516 struct snd_pcm_hw_rule *rule) 2517 { 2518 struct snd_interval t; 2519 struct snd_pcm_substream *substream = rule->private; 2520 t.min = 0; 2521 t.max = substream->buffer_bytes_max; 2522 t.openmin = 0; 2523 t.openmax = 0; 2524 t.integer = 1; 2525 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2526 } 2527 2528 static int snd_pcm_hw_rule_subformats(struct snd_pcm_hw_params *params, 2529 struct snd_pcm_hw_rule *rule) 2530 { 2531 struct snd_mask *sfmask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_SUBFORMAT); 2532 struct snd_mask *fmask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 2533 u32 *subformats = rule->private; 2534 snd_pcm_format_t f; 2535 struct snd_mask m; 2536 2537 snd_mask_none(&m); 2538 /* All PCMs support at least the default STD subformat. */ 2539 snd_mask_set(&m, (__force unsigned)SNDRV_PCM_SUBFORMAT_STD); 2540 2541 pcm_for_each_format(f) { 2542 if (!snd_mask_test(fmask, (__force unsigned)f)) 2543 continue; 2544 2545 if (f == SNDRV_PCM_FORMAT_S32_LE && *subformats) 2546 m.bits[0] |= *subformats; 2547 else if (snd_pcm_format_linear(f)) 2548 snd_mask_set(&m, (__force unsigned)SNDRV_PCM_SUBFORMAT_MSBITS_MAX); 2549 } 2550 2551 return snd_mask_refine(sfmask, &m); 2552 } 2553 2554 static int snd_pcm_hw_constraint_subformats(struct snd_pcm_runtime *runtime, 2555 unsigned int cond, u32 *subformats) 2556 { 2557 return snd_pcm_hw_rule_add(runtime, cond, -1, 2558 snd_pcm_hw_rule_subformats, (void *)subformats, 2559 SNDRV_PCM_HW_PARAM_SUBFORMAT, 2560 SNDRV_PCM_HW_PARAM_FORMAT, -1); 2561 } 2562 2563 static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream) 2564 { 2565 struct snd_pcm_runtime *runtime = substream->runtime; 2566 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; 2567 int k, err; 2568 2569 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { 2570 snd_mask_any(constrs_mask(constrs, k)); 2571 } 2572 2573 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { 2574 snd_interval_any(constrs_interval(constrs, k)); 2575 } 2576 2577 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS)); 2578 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)); 2579 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)); 2580 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)); 2581 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS)); 2582 2583 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, 2584 snd_pcm_hw_rule_format, NULL, 2585 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 2586 if (err < 0) 2587 return err; 2588 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 2589 snd_pcm_hw_rule_sample_bits, NULL, 2590 SNDRV_PCM_HW_PARAM_FORMAT, 2591 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 2592 if (err < 0) 2593 return err; 2594 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 2595 snd_pcm_hw_rule_div, NULL, 2596 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); 2597 if (err < 0) 2598 return err; 2599 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 2600 snd_pcm_hw_rule_mul, NULL, 2601 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); 2602 if (err < 0) 2603 return err; 2604 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 2605 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2606 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); 2607 if (err < 0) 2608 return err; 2609 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 2610 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2611 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1); 2612 if (err < 0) 2613 return err; 2614 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2615 snd_pcm_hw_rule_div, NULL, 2616 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 2617 if (err < 0) 2618 return err; 2619 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 2620 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2621 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1); 2622 if (err < 0) 2623 return err; 2624 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 2625 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2626 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1); 2627 if (err < 0) 2628 return err; 2629 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, 2630 snd_pcm_hw_rule_div, NULL, 2631 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); 2632 if (err < 0) 2633 return err; 2634 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2635 snd_pcm_hw_rule_div, NULL, 2636 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); 2637 if (err < 0) 2638 return err; 2639 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2640 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2641 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2642 if (err < 0) 2643 return err; 2644 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2645 snd_pcm_hw_rule_muldivk, (void*) 1000000, 2646 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); 2647 if (err < 0) 2648 return err; 2649 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 2650 snd_pcm_hw_rule_mul, NULL, 2651 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); 2652 if (err < 0) 2653 return err; 2654 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 2655 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2656 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2657 if (err < 0) 2658 return err; 2659 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 2660 snd_pcm_hw_rule_muldivk, (void*) 1000000, 2661 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); 2662 if (err < 0) 2663 return err; 2664 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 2665 snd_pcm_hw_rule_muldivk, (void*) 8, 2666 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2667 if (err < 0) 2668 return err; 2669 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2670 snd_pcm_hw_rule_muldivk, (void*) 8, 2671 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2672 if (err < 0) 2673 return err; 2674 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 2675 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2676 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); 2677 if (err < 0) 2678 return err; 2679 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 2680 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2681 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); 2682 if (err < 0) 2683 return err; 2684 return 0; 2685 } 2686 2687 static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream) 2688 { 2689 struct snd_pcm_runtime *runtime = substream->runtime; 2690 struct snd_pcm_hardware *hw = &runtime->hw; 2691 int err; 2692 unsigned int mask = 0; 2693 2694 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) 2695 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED); 2696 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) 2697 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED); 2698 if (hw_support_mmap(substream)) { 2699 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) 2700 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); 2701 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) 2702 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED); 2703 if (hw->info & SNDRV_PCM_INFO_COMPLEX) 2704 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX); 2705 } 2706 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask); 2707 if (err < 0) 2708 return err; 2709 2710 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats); 2711 if (err < 0) 2712 return err; 2713 2714 err = snd_pcm_hw_constraint_subformats(runtime, 0, &hw->subformats); 2715 if (err < 0) 2716 return err; 2717 2718 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2719 hw->channels_min, hw->channels_max); 2720 if (err < 0) 2721 return err; 2722 2723 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, 2724 hw->rate_min, hw->rate_max); 2725 if (err < 0) 2726 return err; 2727 2728 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 2729 hw->period_bytes_min, hw->period_bytes_max); 2730 if (err < 0) 2731 return err; 2732 2733 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS, 2734 hw->periods_min, hw->periods_max); 2735 if (err < 0) 2736 return err; 2737 2738 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2739 hw->period_bytes_min, hw->buffer_bytes_max); 2740 if (err < 0) 2741 return err; 2742 2743 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2744 snd_pcm_hw_rule_buffer_bytes_max, substream, 2745 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1); 2746 if (err < 0) 2747 return err; 2748 2749 /* FIXME: remove */ 2750 if (runtime->dma_bytes) { 2751 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes); 2752 if (err < 0) 2753 return err; 2754 } 2755 2756 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) { 2757 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 2758 snd_pcm_hw_rule_rate, hw, 2759 SNDRV_PCM_HW_PARAM_RATE, -1); 2760 if (err < 0) 2761 return err; 2762 } 2763 2764 /* FIXME: this belong to lowlevel */ 2765 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); 2766 2767 return 0; 2768 } 2769 2770 static void pcm_release_private(struct snd_pcm_substream *substream) 2771 { 2772 if (snd_pcm_stream_linked(substream)) 2773 snd_pcm_unlink(substream); 2774 } 2775 2776 void snd_pcm_release_substream(struct snd_pcm_substream *substream) 2777 { 2778 substream->ref_count--; 2779 if (substream->ref_count > 0) 2780 return; 2781 2782 snd_pcm_drop(substream); 2783 if (substream->hw_opened) { 2784 if (substream->runtime->state != SNDRV_PCM_STATE_OPEN) 2785 do_hw_free(substream); 2786 substream->ops->close(substream); 2787 substream->hw_opened = 0; 2788 } 2789 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req)) 2790 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req); 2791 if (substream->pcm_release) { 2792 substream->pcm_release(substream); 2793 substream->pcm_release = NULL; 2794 } 2795 snd_pcm_detach_substream(substream); 2796 } 2797 EXPORT_SYMBOL(snd_pcm_release_substream); 2798 2799 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, 2800 struct file *file, 2801 struct snd_pcm_substream **rsubstream) 2802 { 2803 struct snd_pcm_substream *substream; 2804 int err; 2805 2806 err = snd_pcm_attach_substream(pcm, stream, file, &substream); 2807 if (err < 0) 2808 return err; 2809 if (substream->ref_count > 1) { 2810 *rsubstream = substream; 2811 return 0; 2812 } 2813 2814 err = snd_pcm_hw_constraints_init(substream); 2815 if (err < 0) { 2816 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n"); 2817 goto error; 2818 } 2819 2820 err = substream->ops->open(substream); 2821 if (err < 0) 2822 goto error; 2823 2824 substream->hw_opened = 1; 2825 2826 err = snd_pcm_hw_constraints_complete(substream); 2827 if (err < 0) { 2828 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n"); 2829 goto error; 2830 } 2831 2832 /* automatically set EXPLICIT_SYNC flag in the managed mode whenever 2833 * the DMA buffer requires it 2834 */ 2835 if (substream->managed_buffer_alloc && 2836 substream->dma_buffer.dev.need_sync) 2837 substream->runtime->hw.info |= SNDRV_PCM_INFO_EXPLICIT_SYNC; 2838 2839 *rsubstream = substream; 2840 return 0; 2841 2842 error: 2843 snd_pcm_release_substream(substream); 2844 return err; 2845 } 2846 EXPORT_SYMBOL(snd_pcm_open_substream); 2847 2848 static int snd_pcm_open_file(struct file *file, 2849 struct snd_pcm *pcm, 2850 int stream) 2851 { 2852 struct snd_pcm_file *pcm_file; 2853 struct snd_pcm_substream *substream; 2854 int err; 2855 2856 err = snd_pcm_open_substream(pcm, stream, file, &substream); 2857 if (err < 0) 2858 return err; 2859 2860 pcm_file = kzalloc_obj(*pcm_file); 2861 if (pcm_file == NULL) { 2862 snd_pcm_release_substream(substream); 2863 return -ENOMEM; 2864 } 2865 pcm_file->substream = substream; 2866 if (substream->ref_count == 1) 2867 substream->pcm_release = pcm_release_private; 2868 file->private_data = pcm_file; 2869 2870 return 0; 2871 } 2872 2873 static int snd_pcm_playback_open(struct inode *inode, struct file *file) 2874 { 2875 struct snd_pcm *pcm; 2876 int err = nonseekable_open(inode, file); 2877 if (err < 0) 2878 return err; 2879 pcm = snd_lookup_minor_data(iminor(inode), 2880 SNDRV_DEVICE_TYPE_PCM_PLAYBACK); 2881 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK); 2882 if (pcm) 2883 snd_card_unref(pcm->card); 2884 return err; 2885 } 2886 2887 static int snd_pcm_capture_open(struct inode *inode, struct file *file) 2888 { 2889 struct snd_pcm *pcm; 2890 int err = nonseekable_open(inode, file); 2891 if (err < 0) 2892 return err; 2893 pcm = snd_lookup_minor_data(iminor(inode), 2894 SNDRV_DEVICE_TYPE_PCM_CAPTURE); 2895 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE); 2896 if (pcm) 2897 snd_card_unref(pcm->card); 2898 return err; 2899 } 2900 2901 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream) 2902 { 2903 int err; 2904 wait_queue_entry_t wait; 2905 2906 if (pcm == NULL) { 2907 err = -ENODEV; 2908 goto __error1; 2909 } 2910 err = snd_card_file_add(pcm->card, file); 2911 if (err < 0) 2912 goto __error1; 2913 if (!try_module_get(pcm->card->module)) { 2914 err = -EFAULT; 2915 goto __error2; 2916 } 2917 init_waitqueue_entry(&wait, current); 2918 add_wait_queue(&pcm->open_wait, &wait); 2919 mutex_lock(&pcm->open_mutex); 2920 while (1) { 2921 err = snd_pcm_open_file(file, pcm, stream); 2922 if (err >= 0) 2923 break; 2924 if (err == -EAGAIN) { 2925 if (file->f_flags & O_NONBLOCK) { 2926 err = -EBUSY; 2927 break; 2928 } 2929 } else 2930 break; 2931 set_current_state(TASK_INTERRUPTIBLE); 2932 mutex_unlock(&pcm->open_mutex); 2933 schedule(); 2934 mutex_lock(&pcm->open_mutex); 2935 if (pcm->card->shutdown) { 2936 err = -ENODEV; 2937 break; 2938 } 2939 if (signal_pending(current)) { 2940 err = -ERESTARTSYS; 2941 break; 2942 } 2943 } 2944 remove_wait_queue(&pcm->open_wait, &wait); 2945 mutex_unlock(&pcm->open_mutex); 2946 if (err < 0) 2947 goto __error; 2948 return err; 2949 2950 __error: 2951 module_put(pcm->card->module); 2952 __error2: 2953 snd_card_file_remove(pcm->card, file); 2954 __error1: 2955 return err; 2956 } 2957 2958 static int snd_pcm_release(struct inode *inode, struct file *file) 2959 { 2960 struct snd_pcm *pcm; 2961 struct snd_pcm_substream *substream; 2962 struct snd_pcm_file *pcm_file; 2963 2964 pcm_file = file->private_data; 2965 substream = pcm_file->substream; 2966 if (snd_BUG_ON(!substream)) 2967 return -ENXIO; 2968 pcm = substream->pcm; 2969 2970 /* block until the device gets woken up as it may touch the hardware */ 2971 snd_power_wait(pcm->card); 2972 2973 scoped_guard(mutex, &pcm->open_mutex) { 2974 snd_pcm_release_substream(substream); 2975 kfree(pcm_file); 2976 } 2977 wake_up(&pcm->open_wait); 2978 module_put(pcm->card->module); 2979 snd_card_file_remove(pcm->card, file); 2980 return 0; 2981 } 2982 2983 /* check and update PCM state; return 0 or a negative error 2984 * call this inside PCM lock 2985 */ 2986 static int do_pcm_hwsync(struct snd_pcm_substream *substream) 2987 { 2988 switch (substream->runtime->state) { 2989 case SNDRV_PCM_STATE_DRAINING: 2990 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 2991 return -EBADFD; 2992 fallthrough; 2993 case SNDRV_PCM_STATE_RUNNING: 2994 return snd_pcm_update_hw_ptr(substream); 2995 case SNDRV_PCM_STATE_PREPARED: 2996 case SNDRV_PCM_STATE_PAUSED: 2997 return 0; 2998 case SNDRV_PCM_STATE_SUSPENDED: 2999 return -ESTRPIPE; 3000 case SNDRV_PCM_STATE_XRUN: 3001 return -EPIPE; 3002 default: 3003 return -EBADFD; 3004 } 3005 } 3006 3007 /* increase the appl_ptr; returns the processed frames or a negative error */ 3008 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream, 3009 snd_pcm_uframes_t frames, 3010 snd_pcm_sframes_t avail) 3011 { 3012 struct snd_pcm_runtime *runtime = substream->runtime; 3013 snd_pcm_sframes_t appl_ptr; 3014 int ret; 3015 3016 if (avail <= 0) 3017 return 0; 3018 if (frames > (snd_pcm_uframes_t)avail) 3019 frames = avail; 3020 appl_ptr = runtime->control->appl_ptr + frames; 3021 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary) 3022 appl_ptr -= runtime->boundary; 3023 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr); 3024 return ret < 0 ? ret : frames; 3025 } 3026 3027 /* decrease the appl_ptr; returns the processed frames or zero for error */ 3028 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream, 3029 snd_pcm_uframes_t frames, 3030 snd_pcm_sframes_t avail) 3031 { 3032 struct snd_pcm_runtime *runtime = substream->runtime; 3033 snd_pcm_sframes_t appl_ptr; 3034 int ret; 3035 3036 if (avail <= 0) 3037 return 0; 3038 if (frames > (snd_pcm_uframes_t)avail) 3039 frames = avail; 3040 appl_ptr = runtime->control->appl_ptr - frames; 3041 if (appl_ptr < 0) 3042 appl_ptr += runtime->boundary; 3043 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr); 3044 /* NOTE: we return zero for errors because PulseAudio gets depressed 3045 * upon receiving an error from rewind ioctl and stops processing 3046 * any longer. Returning zero means that no rewind is done, so 3047 * it's not absolutely wrong to answer like that. 3048 */ 3049 return ret < 0 ? 0 : frames; 3050 } 3051 3052 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream, 3053 snd_pcm_uframes_t frames) 3054 { 3055 snd_pcm_sframes_t ret; 3056 3057 if (frames == 0) 3058 return 0; 3059 3060 scoped_guard(pcm_stream_lock_irq, substream) { 3061 ret = do_pcm_hwsync(substream); 3062 if (!ret) 3063 ret = rewind_appl_ptr(substream, frames, 3064 snd_pcm_hw_avail(substream)); 3065 } 3066 if (ret >= 0) 3067 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 3068 return ret; 3069 } 3070 3071 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream, 3072 snd_pcm_uframes_t frames) 3073 { 3074 snd_pcm_sframes_t ret; 3075 3076 if (frames == 0) 3077 return 0; 3078 3079 scoped_guard(pcm_stream_lock_irq, substream) { 3080 ret = do_pcm_hwsync(substream); 3081 if (!ret) 3082 ret = forward_appl_ptr(substream, frames, 3083 snd_pcm_avail(substream)); 3084 } 3085 if (ret >= 0) 3086 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 3087 return ret; 3088 } 3089 3090 static int snd_pcm_delay(struct snd_pcm_substream *substream, 3091 snd_pcm_sframes_t *delay) 3092 { 3093 int err; 3094 3095 scoped_guard(pcm_stream_lock_irq, substream) { 3096 err = do_pcm_hwsync(substream); 3097 if (delay && !err) 3098 *delay = snd_pcm_calc_delay(substream); 3099 } 3100 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU); 3101 3102 return err; 3103 } 3104 3105 static inline int snd_pcm_hwsync(struct snd_pcm_substream *substream) 3106 { 3107 return snd_pcm_delay(substream, NULL); 3108 } 3109 3110 #define snd_pcm_sync_ptr_get_user(__f, __c, __ptr) ({ \ 3111 __label__ failed, failed_begin; \ 3112 int __err = -EFAULT; \ 3113 typeof(*(__ptr)) __user *__src = (__ptr); \ 3114 \ 3115 if (!user_read_access_begin(__src, sizeof(*__src))) \ 3116 goto failed_begin; \ 3117 unsafe_get_user(__f, &__src->flags, failed); \ 3118 unsafe_get_user(__c.appl_ptr, &__src->c.control.appl_ptr, failed); \ 3119 unsafe_get_user(__c.avail_min, &__src->c.control.avail_min, failed); \ 3120 __err = 0; \ 3121 failed: \ 3122 user_read_access_end(); \ 3123 failed_begin: \ 3124 __err; \ 3125 }) 3126 3127 #define snd_pcm_sync_ptr_put_user(__s, __c, __ptr) ({ \ 3128 __label__ failed, failed_begin; \ 3129 int __err = -EFAULT; \ 3130 typeof(*(__ptr)) __user *__src = (__ptr); \ 3131 \ 3132 if (!user_write_access_begin(__src, sizeof(*__src))) \ 3133 goto failed_begin; \ 3134 unsafe_put_user(__s.state, &__src->s.status.state, failed); \ 3135 unsafe_put_user(__s.hw_ptr, &__src->s.status.hw_ptr, failed); \ 3136 unsafe_put_user(__s.tstamp.tv_sec, &__src->s.status.tstamp.tv_sec, failed); \ 3137 unsafe_put_user(__s.tstamp.tv_nsec, &__src->s.status.tstamp.tv_nsec, failed); \ 3138 unsafe_put_user(__s.suspended_state, &__src->s.status.suspended_state, failed); \ 3139 unsafe_put_user(__s.audio_tstamp.tv_sec, &__src->s.status.audio_tstamp.tv_sec, failed); \ 3140 unsafe_put_user(__s.audio_tstamp.tv_nsec, &__src->s.status.audio_tstamp.tv_nsec, failed);\ 3141 unsafe_put_user(__c.appl_ptr, &__src->c.control.appl_ptr, failed); \ 3142 unsafe_put_user(__c.avail_min, &__src->c.control.avail_min, failed); \ 3143 __err = 0; \ 3144 failed: \ 3145 user_write_access_end(); \ 3146 failed_begin: \ 3147 __err; \ 3148 }) 3149 3150 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream, 3151 struct snd_pcm_sync_ptr __user *_sync_ptr) 3152 { 3153 struct snd_pcm_runtime *runtime = substream->runtime; 3154 volatile struct snd_pcm_mmap_status *status; 3155 volatile struct snd_pcm_mmap_control *control; 3156 u32 sflags; 3157 struct snd_pcm_mmap_control scontrol; 3158 struct snd_pcm_mmap_status sstatus; 3159 int err; 3160 3161 if (snd_pcm_sync_ptr_get_user(sflags, scontrol, _sync_ptr)) 3162 return -EFAULT; 3163 status = runtime->status; 3164 control = runtime->control; 3165 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) { 3166 err = snd_pcm_hwsync(substream); 3167 if (err < 0) 3168 return err; 3169 } 3170 scoped_guard(pcm_stream_lock_irq, substream) { 3171 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) { 3172 err = pcm_lib_apply_appl_ptr(substream, scontrol.appl_ptr); 3173 if (err < 0) 3174 return err; 3175 } else { 3176 scontrol.appl_ptr = control->appl_ptr; 3177 } 3178 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) 3179 control->avail_min = scontrol.avail_min; 3180 else 3181 scontrol.avail_min = control->avail_min; 3182 sstatus.state = status->state; 3183 sstatus.hw_ptr = status->hw_ptr; 3184 sstatus.tstamp = status->tstamp; 3185 sstatus.suspended_state = status->suspended_state; 3186 sstatus.audio_tstamp = status->audio_tstamp; 3187 } 3188 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) 3189 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 3190 if (snd_pcm_sync_ptr_put_user(sstatus, scontrol, _sync_ptr)) 3191 return -EFAULT; 3192 return 0; 3193 } 3194 3195 struct snd_pcm_mmap_status32 { 3196 snd_pcm_state_t state; 3197 s32 pad1; 3198 u32 hw_ptr; 3199 struct __snd_timespec tstamp; 3200 snd_pcm_state_t suspended_state; 3201 struct __snd_timespec audio_tstamp; 3202 } __packed; 3203 3204 struct snd_pcm_mmap_control32 { 3205 u32 appl_ptr; 3206 u32 avail_min; 3207 }; 3208 3209 struct snd_pcm_sync_ptr32 { 3210 u32 flags; 3211 union { 3212 struct snd_pcm_mmap_status32 status; 3213 unsigned char reserved[64]; 3214 } s; 3215 union { 3216 struct snd_pcm_mmap_control32 control; 3217 unsigned char reserved[64]; 3218 } c; 3219 } __packed; 3220 3221 /* recalculate the boundary within 32bit */ 3222 static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime) 3223 { 3224 snd_pcm_uframes_t boundary; 3225 snd_pcm_uframes_t border; 3226 int order; 3227 3228 if (! runtime->buffer_size) 3229 return 0; 3230 3231 border = 0x7fffffffUL - runtime->buffer_size; 3232 if (runtime->buffer_size > border) 3233 return runtime->buffer_size; 3234 3235 order = __fls(border) - __fls(runtime->buffer_size); 3236 boundary = runtime->buffer_size << order; 3237 3238 if (boundary <= border) 3239 return boundary; 3240 else 3241 return boundary / 2; 3242 } 3243 3244 static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream, 3245 struct snd_pcm_sync_ptr32 __user *src) 3246 { 3247 struct snd_pcm_runtime *runtime = substream->runtime; 3248 volatile struct snd_pcm_mmap_status *status; 3249 volatile struct snd_pcm_mmap_control *control; 3250 u32 sflags; 3251 struct snd_pcm_mmap_control scontrol; 3252 struct snd_pcm_mmap_status sstatus; 3253 snd_pcm_uframes_t boundary; 3254 int err; 3255 3256 if (snd_BUG_ON(!runtime)) 3257 return -EINVAL; 3258 3259 if (snd_pcm_sync_ptr_get_user(sflags, scontrol, src)) 3260 return -EFAULT; 3261 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) { 3262 err = snd_pcm_hwsync(substream); 3263 if (err < 0) 3264 return err; 3265 } 3266 status = runtime->status; 3267 control = runtime->control; 3268 boundary = recalculate_boundary(runtime); 3269 if (! boundary) 3270 boundary = 0x7fffffff; 3271 scoped_guard(pcm_stream_lock_irq, substream) { 3272 /* FIXME: we should consider the boundary for the sync from app */ 3273 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) { 3274 err = pcm_lib_apply_appl_ptr(substream, 3275 scontrol.appl_ptr); 3276 if (err < 0) 3277 return err; 3278 } else 3279 scontrol.appl_ptr = control->appl_ptr % boundary; 3280 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) 3281 control->avail_min = scontrol.avail_min; 3282 else 3283 scontrol.avail_min = control->avail_min; 3284 sstatus.state = status->state; 3285 sstatus.hw_ptr = status->hw_ptr % boundary; 3286 sstatus.tstamp = status->tstamp; 3287 sstatus.suspended_state = status->suspended_state; 3288 sstatus.audio_tstamp = status->audio_tstamp; 3289 } 3290 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) 3291 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 3292 if (snd_pcm_sync_ptr_put_user(sstatus, scontrol, src)) 3293 return -EFAULT; 3294 3295 return 0; 3296 } 3297 #define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32) 3298 3299 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg) 3300 { 3301 struct snd_pcm_runtime *runtime = substream->runtime; 3302 int arg; 3303 3304 if (get_user(arg, _arg)) 3305 return -EFAULT; 3306 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST) 3307 return -EINVAL; 3308 runtime->tstamp_type = arg; 3309 return 0; 3310 } 3311 3312 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream, 3313 struct snd_xferi __user *_xferi) 3314 { 3315 struct snd_xferi xferi; 3316 snd_pcm_sframes_t result; 3317 3318 if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN) 3319 return -EBADFD; 3320 if (put_user(0, &_xferi->result)) 3321 return -EFAULT; 3322 if (copy_from_user(&xferi, _xferi, sizeof(xferi))) 3323 return -EFAULT; 3324 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3325 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames); 3326 else 3327 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames); 3328 if (put_user(result, &_xferi->result)) 3329 return -EFAULT; 3330 return result < 0 ? result : 0; 3331 } 3332 3333 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream, 3334 struct snd_xfern __user *_xfern) 3335 { 3336 struct snd_xfern xfern; 3337 struct snd_pcm_runtime *runtime = substream->runtime; 3338 void *bufs __free(kfree) = NULL; 3339 snd_pcm_sframes_t result; 3340 3341 if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN) 3342 return -EBADFD; 3343 if (runtime->channels > 128) 3344 return -EINVAL; 3345 if (put_user(0, &_xfern->result)) 3346 return -EFAULT; 3347 if (copy_from_user(&xfern, _xfern, sizeof(xfern))) 3348 return -EFAULT; 3349 3350 bufs = memdup_array_user(xfern.bufs, runtime->channels, sizeof(void *)); 3351 if (IS_ERR(bufs)) 3352 return PTR_ERR(bufs); 3353 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3354 result = snd_pcm_lib_writev(substream, bufs, xfern.frames); 3355 else 3356 result = snd_pcm_lib_readv(substream, bufs, xfern.frames); 3357 if (put_user(result, &_xfern->result)) 3358 return -EFAULT; 3359 return result < 0 ? result : 0; 3360 } 3361 3362 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream, 3363 snd_pcm_uframes_t __user *_frames) 3364 { 3365 snd_pcm_uframes_t frames; 3366 snd_pcm_sframes_t result; 3367 3368 if (get_user(frames, _frames)) 3369 return -EFAULT; 3370 if (put_user(0, _frames)) 3371 return -EFAULT; 3372 result = snd_pcm_rewind(substream, frames); 3373 if (put_user(result, _frames)) 3374 return -EFAULT; 3375 return result < 0 ? result : 0; 3376 } 3377 3378 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream, 3379 snd_pcm_uframes_t __user *_frames) 3380 { 3381 snd_pcm_uframes_t frames; 3382 snd_pcm_sframes_t result; 3383 3384 if (get_user(frames, _frames)) 3385 return -EFAULT; 3386 if (put_user(0, _frames)) 3387 return -EFAULT; 3388 result = snd_pcm_forward(substream, frames); 3389 if (put_user(result, _frames)) 3390 return -EFAULT; 3391 return result < 0 ? result : 0; 3392 } 3393 3394 static int snd_pcm_common_ioctl(struct file *file, 3395 struct snd_pcm_substream *substream, 3396 unsigned int cmd, void __user *arg) 3397 { 3398 struct snd_pcm_file *pcm_file = file->private_data; 3399 int res; 3400 3401 if (PCM_RUNTIME_CHECK(substream)) 3402 return -ENXIO; 3403 3404 if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_DISCONNECTED) 3405 return -EBADFD; 3406 3407 res = snd_power_wait(substream->pcm->card); 3408 if (res < 0) 3409 return res; 3410 3411 switch (cmd) { 3412 case SNDRV_PCM_IOCTL_PVERSION: 3413 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0; 3414 case SNDRV_PCM_IOCTL_INFO: 3415 return snd_pcm_info_user(substream, arg); 3416 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */ 3417 return 0; 3418 case SNDRV_PCM_IOCTL_TTSTAMP: 3419 return snd_pcm_tstamp(substream, arg); 3420 case SNDRV_PCM_IOCTL_USER_PVERSION: 3421 if (get_user(pcm_file->user_pversion, 3422 (unsigned int __user *)arg)) 3423 return -EFAULT; 3424 return 0; 3425 case SNDRV_PCM_IOCTL_HW_REFINE: 3426 return snd_pcm_hw_refine_user(substream, arg); 3427 case SNDRV_PCM_IOCTL_HW_PARAMS: 3428 return snd_pcm_hw_params_user(substream, arg); 3429 case SNDRV_PCM_IOCTL_HW_FREE: 3430 return snd_pcm_hw_free(substream); 3431 case SNDRV_PCM_IOCTL_SW_PARAMS: 3432 return snd_pcm_sw_params_user(substream, arg); 3433 case SNDRV_PCM_IOCTL_STATUS32: 3434 return snd_pcm_status_user32(substream, arg, false); 3435 case SNDRV_PCM_IOCTL_STATUS_EXT32: 3436 return snd_pcm_status_user32(substream, arg, true); 3437 case SNDRV_PCM_IOCTL_STATUS64: 3438 return snd_pcm_status_user64(substream, arg, false); 3439 case SNDRV_PCM_IOCTL_STATUS_EXT64: 3440 return snd_pcm_status_user64(substream, arg, true); 3441 case SNDRV_PCM_IOCTL_CHANNEL_INFO: 3442 return snd_pcm_channel_info_user(substream, arg); 3443 case SNDRV_PCM_IOCTL_PREPARE: 3444 return snd_pcm_prepare(substream, file); 3445 case SNDRV_PCM_IOCTL_RESET: 3446 return snd_pcm_reset(substream); 3447 case SNDRV_PCM_IOCTL_START: 3448 return snd_pcm_start_lock_irq(substream); 3449 case SNDRV_PCM_IOCTL_LINK: 3450 return snd_pcm_link(substream, (int)(unsigned long) arg); 3451 case SNDRV_PCM_IOCTL_UNLINK: 3452 return snd_pcm_unlink(substream); 3453 case SNDRV_PCM_IOCTL_RESUME: 3454 return snd_pcm_resume(substream); 3455 case SNDRV_PCM_IOCTL_XRUN: 3456 return snd_pcm_xrun(substream); 3457 case SNDRV_PCM_IOCTL_HWSYNC: 3458 return snd_pcm_hwsync(substream); 3459 case SNDRV_PCM_IOCTL_DELAY: 3460 { 3461 snd_pcm_sframes_t delay = 0; 3462 snd_pcm_sframes_t __user *res = arg; 3463 int err; 3464 3465 err = snd_pcm_delay(substream, &delay); 3466 if (err) 3467 return err; 3468 if (put_user(delay, res)) 3469 return -EFAULT; 3470 return 0; 3471 } 3472 case __SNDRV_PCM_IOCTL_SYNC_PTR32: 3473 return snd_pcm_ioctl_sync_ptr_compat(substream, arg); 3474 case __SNDRV_PCM_IOCTL_SYNC_PTR64: 3475 return snd_pcm_sync_ptr(substream, arg); 3476 #ifdef CONFIG_SND_SUPPORT_OLD_API 3477 case SNDRV_PCM_IOCTL_HW_REFINE_OLD: 3478 return snd_pcm_hw_refine_old_user(substream, arg); 3479 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD: 3480 return snd_pcm_hw_params_old_user(substream, arg); 3481 #endif 3482 case SNDRV_PCM_IOCTL_DRAIN: 3483 return snd_pcm_drain(substream, file); 3484 case SNDRV_PCM_IOCTL_DROP: 3485 return snd_pcm_drop(substream); 3486 case SNDRV_PCM_IOCTL_PAUSE: 3487 return snd_pcm_pause_lock_irq(substream, (unsigned long)arg); 3488 case SNDRV_PCM_IOCTL_WRITEI_FRAMES: 3489 case SNDRV_PCM_IOCTL_READI_FRAMES: 3490 return snd_pcm_xferi_frames_ioctl(substream, arg); 3491 case SNDRV_PCM_IOCTL_WRITEN_FRAMES: 3492 case SNDRV_PCM_IOCTL_READN_FRAMES: 3493 return snd_pcm_xfern_frames_ioctl(substream, arg); 3494 case SNDRV_PCM_IOCTL_REWIND: 3495 return snd_pcm_rewind_ioctl(substream, arg); 3496 case SNDRV_PCM_IOCTL_FORWARD: 3497 return snd_pcm_forward_ioctl(substream, arg); 3498 } 3499 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd); 3500 return -ENOTTY; 3501 } 3502 3503 static long snd_pcm_ioctl(struct file *file, unsigned int cmd, 3504 unsigned long arg) 3505 { 3506 struct snd_pcm_file *pcm_file; 3507 3508 pcm_file = file->private_data; 3509 3510 if (((cmd >> 8) & 0xff) != 'A') 3511 return -ENOTTY; 3512 3513 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd, 3514 (void __user *)arg); 3515 } 3516 3517 /** 3518 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space 3519 * @substream: PCM substream 3520 * @cmd: IOCTL cmd 3521 * @arg: IOCTL argument 3522 * 3523 * The function is provided primarily for OSS layer and USB gadget drivers, 3524 * and it allows only the limited set of ioctls (hw_params, sw_params, 3525 * prepare, start, drain, drop, forward). 3526 * 3527 * Return: zero if successful, or a negative error code 3528 */ 3529 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, 3530 unsigned int cmd, void *arg) 3531 { 3532 snd_pcm_uframes_t *frames = arg; 3533 snd_pcm_sframes_t result; 3534 3535 if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_DISCONNECTED) 3536 return -EBADFD; 3537 3538 switch (cmd) { 3539 case SNDRV_PCM_IOCTL_FORWARD: 3540 { 3541 /* provided only for OSS; capture-only and no value returned */ 3542 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE) 3543 return -EINVAL; 3544 result = snd_pcm_forward(substream, *frames); 3545 return result < 0 ? result : 0; 3546 } 3547 case SNDRV_PCM_IOCTL_HW_PARAMS: 3548 return snd_pcm_hw_params(substream, arg); 3549 case SNDRV_PCM_IOCTL_SW_PARAMS: 3550 return snd_pcm_sw_params(substream, arg); 3551 case SNDRV_PCM_IOCTL_PREPARE: 3552 return snd_pcm_prepare(substream, NULL); 3553 case SNDRV_PCM_IOCTL_START: 3554 return snd_pcm_start_lock_irq(substream); 3555 case SNDRV_PCM_IOCTL_DRAIN: 3556 return snd_pcm_drain(substream, NULL); 3557 case SNDRV_PCM_IOCTL_DROP: 3558 return snd_pcm_drop(substream); 3559 case SNDRV_PCM_IOCTL_DELAY: 3560 return snd_pcm_delay(substream, frames); 3561 default: 3562 return -EINVAL; 3563 } 3564 } 3565 EXPORT_SYMBOL(snd_pcm_kernel_ioctl); 3566 3567 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, 3568 loff_t * offset) 3569 { 3570 struct snd_pcm_file *pcm_file; 3571 struct snd_pcm_substream *substream; 3572 struct snd_pcm_runtime *runtime; 3573 snd_pcm_sframes_t result; 3574 3575 pcm_file = file->private_data; 3576 substream = pcm_file->substream; 3577 if (PCM_RUNTIME_CHECK(substream)) 3578 return -ENXIO; 3579 runtime = substream->runtime; 3580 if (snd_pcm_state_open_or_disconnected(substream)) 3581 return -EBADFD; 3582 if (!frame_aligned(runtime, count)) 3583 return -EINVAL; 3584 count = bytes_to_frames(runtime, count); 3585 result = snd_pcm_lib_read(substream, buf, count); 3586 if (result > 0) 3587 result = frames_to_bytes(runtime, result); 3588 return result; 3589 } 3590 3591 static ssize_t snd_pcm_write(struct file *file, const char __user *buf, 3592 size_t count, loff_t * offset) 3593 { 3594 struct snd_pcm_file *pcm_file; 3595 struct snd_pcm_substream *substream; 3596 struct snd_pcm_runtime *runtime; 3597 snd_pcm_sframes_t result; 3598 3599 pcm_file = file->private_data; 3600 substream = pcm_file->substream; 3601 if (PCM_RUNTIME_CHECK(substream)) 3602 return -ENXIO; 3603 runtime = substream->runtime; 3604 if (snd_pcm_state_open_or_disconnected(substream)) 3605 return -EBADFD; 3606 if (!frame_aligned(runtime, count)) 3607 return -EINVAL; 3608 count = bytes_to_frames(runtime, count); 3609 result = snd_pcm_lib_write(substream, buf, count); 3610 if (result > 0) 3611 result = frames_to_bytes(runtime, result); 3612 return result; 3613 } 3614 3615 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to) 3616 { 3617 struct snd_pcm_file *pcm_file; 3618 struct snd_pcm_substream *substream; 3619 struct snd_pcm_runtime *runtime; 3620 snd_pcm_sframes_t result; 3621 unsigned long i; 3622 snd_pcm_uframes_t frames; 3623 const struct iovec *iov = iter_iov(to); 3624 3625 pcm_file = iocb->ki_filp->private_data; 3626 substream = pcm_file->substream; 3627 if (PCM_RUNTIME_CHECK(substream)) 3628 return -ENXIO; 3629 runtime = substream->runtime; 3630 if (snd_pcm_state_open_or_disconnected(substream)) 3631 return -EBADFD; 3632 if (!user_backed_iter(to)) 3633 return -EINVAL; 3634 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels) 3635 return -EINVAL; 3636 if (!frame_aligned(runtime, iov->iov_len)) 3637 return -EINVAL; 3638 frames = bytes_to_samples(runtime, iov->iov_len); 3639 3640 void __user **bufs __free(kfree) = 3641 kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL); 3642 if (bufs == NULL) 3643 return -ENOMEM; 3644 for (i = 0; i < to->nr_segs; ++i) { 3645 bufs[i] = iov->iov_base; 3646 iov++; 3647 } 3648 result = snd_pcm_lib_readv(substream, bufs, frames); 3649 if (result > 0) 3650 result = frames_to_bytes(runtime, result); 3651 return result; 3652 } 3653 3654 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from) 3655 { 3656 struct snd_pcm_file *pcm_file; 3657 struct snd_pcm_substream *substream; 3658 struct snd_pcm_runtime *runtime; 3659 snd_pcm_sframes_t result; 3660 unsigned long i; 3661 snd_pcm_uframes_t frames; 3662 const struct iovec *iov = iter_iov(from); 3663 3664 pcm_file = iocb->ki_filp->private_data; 3665 substream = pcm_file->substream; 3666 if (PCM_RUNTIME_CHECK(substream)) 3667 return -ENXIO; 3668 runtime = substream->runtime; 3669 if (snd_pcm_state_open_or_disconnected(substream)) 3670 return -EBADFD; 3671 if (!user_backed_iter(from)) 3672 return -EINVAL; 3673 if (from->nr_segs > 128 || from->nr_segs != runtime->channels || 3674 !frame_aligned(runtime, iov->iov_len)) 3675 return -EINVAL; 3676 frames = bytes_to_samples(runtime, iov->iov_len); 3677 3678 void __user **bufs __free(kfree) = 3679 kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL); 3680 if (bufs == NULL) 3681 return -ENOMEM; 3682 for (i = 0; i < from->nr_segs; ++i) { 3683 bufs[i] = iov->iov_base; 3684 iov++; 3685 } 3686 result = snd_pcm_lib_writev(substream, bufs, frames); 3687 if (result > 0) 3688 result = frames_to_bytes(runtime, result); 3689 return result; 3690 } 3691 3692 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait) 3693 { 3694 struct snd_pcm_file *pcm_file; 3695 struct snd_pcm_substream *substream; 3696 struct snd_pcm_runtime *runtime; 3697 __poll_t mask, ok; 3698 snd_pcm_uframes_t avail; 3699 3700 pcm_file = file->private_data; 3701 3702 substream = pcm_file->substream; 3703 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3704 ok = EPOLLOUT | EPOLLWRNORM; 3705 else 3706 ok = EPOLLIN | EPOLLRDNORM; 3707 if (PCM_RUNTIME_CHECK(substream)) 3708 return ok | EPOLLERR; 3709 3710 runtime = substream->runtime; 3711 if (runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 3712 return ok | EPOLLERR; 3713 3714 poll_wait(file, &runtime->sleep, wait); 3715 3716 mask = 0; 3717 guard(pcm_stream_lock_irq)(substream); 3718 avail = snd_pcm_avail(substream); 3719 switch (runtime->state) { 3720 case SNDRV_PCM_STATE_RUNNING: 3721 case SNDRV_PCM_STATE_PREPARED: 3722 case SNDRV_PCM_STATE_PAUSED: 3723 if (avail >= runtime->control->avail_min) 3724 mask = ok; 3725 break; 3726 case SNDRV_PCM_STATE_DRAINING: 3727 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 3728 mask = ok; 3729 if (!avail) 3730 mask |= EPOLLERR; 3731 } 3732 break; 3733 default: 3734 mask = ok | EPOLLERR; 3735 break; 3736 } 3737 return mask; 3738 } 3739 3740 /* 3741 * mmap support 3742 */ 3743 3744 /* 3745 * Only on coherent architectures, we can mmap the status and the control records 3746 * for effcient data transfer. On others, we have to use HWSYNC ioctl... 3747 */ 3748 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA) 3749 /* 3750 * mmap status record 3751 */ 3752 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf) 3753 { 3754 struct snd_pcm_substream *substream = vmf->vma->vm_private_data; 3755 struct snd_pcm_runtime *runtime; 3756 3757 if (substream == NULL) 3758 return VM_FAULT_SIGBUS; 3759 runtime = substream->runtime; 3760 vmf->page = virt_to_page(runtime->status); 3761 get_page(vmf->page); 3762 return 0; 3763 } 3764 3765 static const struct vm_operations_struct snd_pcm_vm_ops_status = 3766 { 3767 .fault = snd_pcm_mmap_status_fault, 3768 }; 3769 3770 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, 3771 struct vm_area_struct *area) 3772 { 3773 long size; 3774 if (!(area->vm_flags & VM_READ)) 3775 return -EINVAL; 3776 size = area->vm_end - area->vm_start; 3777 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))) 3778 return -EINVAL; 3779 area->vm_ops = &snd_pcm_vm_ops_status; 3780 area->vm_private_data = substream; 3781 vm_flags_mod(area, VM_DONTEXPAND | VM_DONTDUMP, 3782 VM_WRITE | VM_MAYWRITE); 3783 3784 return 0; 3785 } 3786 3787 /* 3788 * mmap control record 3789 */ 3790 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf) 3791 { 3792 struct snd_pcm_substream *substream = vmf->vma->vm_private_data; 3793 struct snd_pcm_runtime *runtime; 3794 3795 if (substream == NULL) 3796 return VM_FAULT_SIGBUS; 3797 runtime = substream->runtime; 3798 vmf->page = virt_to_page(runtime->control); 3799 get_page(vmf->page); 3800 return 0; 3801 } 3802 3803 static const struct vm_operations_struct snd_pcm_vm_ops_control = 3804 { 3805 .fault = snd_pcm_mmap_control_fault, 3806 }; 3807 3808 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, 3809 struct vm_area_struct *area) 3810 { 3811 long size; 3812 if (!(area->vm_flags & VM_READ)) 3813 return -EINVAL; 3814 size = area->vm_end - area->vm_start; 3815 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control))) 3816 return -EINVAL; 3817 area->vm_ops = &snd_pcm_vm_ops_control; 3818 area->vm_private_data = substream; 3819 vm_flags_set(area, VM_DONTEXPAND | VM_DONTDUMP); 3820 return 0; 3821 } 3822 3823 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file) 3824 { 3825 /* If drivers require the explicit sync (typically for non-coherent 3826 * pages), we have to disable the mmap of status and control data 3827 * to enforce the control via SYNC_PTR ioctl. 3828 */ 3829 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC) 3830 return false; 3831 /* See pcm_control_mmap_allowed() below. 3832 * Since older alsa-lib requires both status and control mmaps to be 3833 * coupled, we have to disable the status mmap for old alsa-lib, too. 3834 */ 3835 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) && 3836 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)) 3837 return false; 3838 return true; 3839 } 3840 3841 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file) 3842 { 3843 if (pcm_file->no_compat_mmap) 3844 return false; 3845 /* see above */ 3846 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC) 3847 return false; 3848 /* Disallow the control mmap when SYNC_APPLPTR flag is set; 3849 * it enforces the user-space to fall back to snd_pcm_sync_ptr(), 3850 * thus it effectively assures the manual update of appl_ptr. 3851 */ 3852 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR) 3853 return false; 3854 return true; 3855 } 3856 3857 #else /* ! coherent mmap */ 3858 /* 3859 * don't support mmap for status and control records. 3860 */ 3861 #define pcm_status_mmap_allowed(pcm_file) false 3862 #define pcm_control_mmap_allowed(pcm_file) false 3863 3864 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, 3865 struct vm_area_struct *area) 3866 { 3867 return -ENXIO; 3868 } 3869 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, 3870 struct vm_area_struct *area) 3871 { 3872 return -ENXIO; 3873 } 3874 #endif /* coherent mmap */ 3875 3876 /* 3877 * snd_pcm_mmap_data_open - increase the mmap counter 3878 */ 3879 static void snd_pcm_mmap_data_open(struct vm_area_struct *area) 3880 { 3881 struct snd_pcm_substream *substream = area->vm_private_data; 3882 3883 atomic_inc(&substream->mmap_count); 3884 } 3885 3886 /* 3887 * snd_pcm_mmap_data_close - decrease the mmap counter 3888 */ 3889 static void snd_pcm_mmap_data_close(struct vm_area_struct *area) 3890 { 3891 struct snd_pcm_substream *substream = area->vm_private_data; 3892 3893 atomic_dec(&substream->mmap_count); 3894 } 3895 3896 /* 3897 * fault callback for mmapping a RAM page 3898 */ 3899 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf) 3900 { 3901 struct snd_pcm_substream *substream = vmf->vma->vm_private_data; 3902 struct snd_pcm_runtime *runtime; 3903 unsigned long offset; 3904 struct page * page; 3905 size_t dma_bytes; 3906 3907 if (substream == NULL) 3908 return VM_FAULT_SIGBUS; 3909 runtime = substream->runtime; 3910 offset = vmf->pgoff << PAGE_SHIFT; 3911 dma_bytes = PAGE_ALIGN(runtime->dma_bytes); 3912 if (offset > dma_bytes - PAGE_SIZE) 3913 return VM_FAULT_SIGBUS; 3914 if (substream->ops->page) 3915 page = substream->ops->page(substream, offset); 3916 else if (!snd_pcm_get_dma_buf(substream)) { 3917 if (WARN_ON_ONCE(!runtime->dma_area)) 3918 return VM_FAULT_SIGBUS; 3919 page = virt_to_page(runtime->dma_area + offset); 3920 } else 3921 page = snd_sgbuf_get_page(snd_pcm_get_dma_buf(substream), offset); 3922 if (!page) 3923 return VM_FAULT_SIGBUS; 3924 get_page(page); 3925 vmf->page = page; 3926 return 0; 3927 } 3928 3929 static const struct vm_operations_struct snd_pcm_vm_ops_data = { 3930 .open = snd_pcm_mmap_data_open, 3931 .close = snd_pcm_mmap_data_close, 3932 }; 3933 3934 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = { 3935 .open = snd_pcm_mmap_data_open, 3936 .close = snd_pcm_mmap_data_close, 3937 .fault = snd_pcm_mmap_data_fault, 3938 }; 3939 3940 /* 3941 * mmap the DMA buffer on RAM 3942 */ 3943 3944 /** 3945 * snd_pcm_lib_default_mmap - Default PCM data mmap function 3946 * @substream: PCM substream 3947 * @area: VMA 3948 * 3949 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL, 3950 * this function is invoked implicitly. 3951 * 3952 * Return: zero if successful, or a negative error code 3953 */ 3954 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream, 3955 struct vm_area_struct *area) 3956 { 3957 vm_flags_set(area, VM_DONTEXPAND | VM_DONTDUMP); 3958 if (!substream->ops->page && 3959 !snd_dma_buffer_mmap(snd_pcm_get_dma_buf(substream), area)) 3960 return 0; 3961 /* mmap with fault handler */ 3962 area->vm_ops = &snd_pcm_vm_ops_data_fault; 3963 return 0; 3964 } 3965 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap); 3966 3967 /* 3968 * mmap the DMA buffer on I/O memory area 3969 */ 3970 #if SNDRV_PCM_INFO_MMAP_IOMEM 3971 /** 3972 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem 3973 * @substream: PCM substream 3974 * @area: VMA 3975 * 3976 * When your hardware uses the iomapped pages as the hardware buffer and 3977 * wants to mmap it, pass this function as mmap pcm_ops. Note that this 3978 * is supposed to work only on limited architectures. 3979 * 3980 * Return: zero if successful, or a negative error code 3981 */ 3982 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, 3983 struct vm_area_struct *area) 3984 { 3985 struct snd_pcm_runtime *runtime = substream->runtime; 3986 3987 area->vm_page_prot = pgprot_noncached(area->vm_page_prot); 3988 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes); 3989 } 3990 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem); 3991 #endif /* SNDRV_PCM_INFO_MMAP */ 3992 3993 /* 3994 * mmap DMA buffer 3995 */ 3996 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, 3997 struct vm_area_struct *area) 3998 { 3999 struct snd_pcm_runtime *runtime; 4000 long size; 4001 unsigned long offset; 4002 size_t dma_bytes; 4003 int err; 4004 4005 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 4006 if (!(area->vm_flags & (VM_WRITE|VM_READ))) 4007 return -EINVAL; 4008 } else { 4009 if (!(area->vm_flags & VM_READ)) 4010 return -EINVAL; 4011 } 4012 runtime = substream->runtime; 4013 if (runtime->state == SNDRV_PCM_STATE_OPEN) 4014 return -EBADFD; 4015 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) 4016 return -ENXIO; 4017 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || 4018 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) 4019 return -EINVAL; 4020 size = area->vm_end - area->vm_start; 4021 offset = area->vm_pgoff << PAGE_SHIFT; 4022 dma_bytes = PAGE_ALIGN(runtime->dma_bytes); 4023 if ((size_t)size > dma_bytes) 4024 return -EINVAL; 4025 if (offset > dma_bytes - size) 4026 return -EINVAL; 4027 4028 area->vm_ops = &snd_pcm_vm_ops_data; 4029 area->vm_private_data = substream; 4030 if (substream->ops->mmap) 4031 err = substream->ops->mmap(substream, area); 4032 else 4033 err = snd_pcm_lib_default_mmap(substream, area); 4034 if (!err) 4035 atomic_inc(&substream->mmap_count); 4036 return err; 4037 } 4038 EXPORT_SYMBOL(snd_pcm_mmap_data); 4039 4040 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area) 4041 { 4042 struct snd_pcm_file * pcm_file; 4043 struct snd_pcm_substream *substream; 4044 unsigned long offset; 4045 4046 pcm_file = file->private_data; 4047 substream = pcm_file->substream; 4048 if (PCM_RUNTIME_CHECK(substream)) 4049 return -ENXIO; 4050 if (substream->runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 4051 return -EBADFD; 4052 4053 offset = area->vm_pgoff << PAGE_SHIFT; 4054 switch (offset) { 4055 case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD: 4056 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT)) 4057 return -ENXIO; 4058 fallthrough; 4059 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW: 4060 if (!pcm_status_mmap_allowed(pcm_file)) 4061 return -ENXIO; 4062 return snd_pcm_mmap_status(substream, file, area); 4063 case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD: 4064 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT)) 4065 return -ENXIO; 4066 fallthrough; 4067 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW: 4068 if (!pcm_control_mmap_allowed(pcm_file)) 4069 return -ENXIO; 4070 return snd_pcm_mmap_control(substream, file, area); 4071 default: 4072 return snd_pcm_mmap_data(substream, file, area); 4073 } 4074 return 0; 4075 } 4076 4077 static int snd_pcm_fasync(int fd, struct file * file, int on) 4078 { 4079 struct snd_pcm_file * pcm_file; 4080 struct snd_pcm_substream *substream; 4081 struct snd_pcm_runtime *runtime; 4082 4083 pcm_file = file->private_data; 4084 substream = pcm_file->substream; 4085 if (PCM_RUNTIME_CHECK(substream)) 4086 return -ENXIO; 4087 runtime = substream->runtime; 4088 if (runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 4089 return -EBADFD; 4090 return snd_fasync_helper(fd, file, on, &runtime->fasync); 4091 } 4092 4093 /* 4094 * ioctl32 compat 4095 */ 4096 #ifdef CONFIG_COMPAT 4097 #include "pcm_compat.c" 4098 #else 4099 #define snd_pcm_ioctl_compat NULL 4100 #endif 4101 4102 /* 4103 * To be removed helpers to keep binary compatibility 4104 */ 4105 4106 #ifdef CONFIG_SND_SUPPORT_OLD_API 4107 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5)) 4108 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5)) 4109 4110 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params, 4111 struct snd_pcm_hw_params_old *oparams) 4112 { 4113 unsigned int i; 4114 4115 memset(params, 0, sizeof(*params)); 4116 params->flags = oparams->flags; 4117 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) 4118 params->masks[i].bits[0] = oparams->masks[i]; 4119 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals)); 4120 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask); 4121 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask); 4122 params->info = oparams->info; 4123 params->msbits = oparams->msbits; 4124 params->rate_num = oparams->rate_num; 4125 params->rate_den = oparams->rate_den; 4126 params->fifo_size = oparams->fifo_size; 4127 } 4128 4129 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams, 4130 struct snd_pcm_hw_params *params) 4131 { 4132 unsigned int i; 4133 4134 memset(oparams, 0, sizeof(*oparams)); 4135 oparams->flags = params->flags; 4136 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) 4137 oparams->masks[i] = params->masks[i].bits[0]; 4138 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals)); 4139 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask); 4140 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask); 4141 oparams->info = params->info; 4142 oparams->msbits = params->msbits; 4143 oparams->rate_num = params->rate_num; 4144 oparams->rate_den = params->rate_den; 4145 oparams->fifo_size = params->fifo_size; 4146 } 4147 4148 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, 4149 struct snd_pcm_hw_params_old __user * _oparams) 4150 { 4151 int err; 4152 4153 struct snd_pcm_hw_params *params __free(kfree) = 4154 kmalloc_obj(*params); 4155 if (!params) 4156 return -ENOMEM; 4157 4158 struct snd_pcm_hw_params_old *oparams __free(kfree) = 4159 memdup_user(_oparams, sizeof(*oparams)); 4160 if (IS_ERR(oparams)) 4161 return PTR_ERR(oparams); 4162 snd_pcm_hw_convert_from_old_params(params, oparams); 4163 err = snd_pcm_hw_refine(substream, params); 4164 if (err < 0) 4165 return err; 4166 4167 err = fixup_unreferenced_params(substream, params); 4168 if (err < 0) 4169 return err; 4170 4171 snd_pcm_hw_convert_to_old_params(oparams, params); 4172 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) 4173 return -EFAULT; 4174 return 0; 4175 } 4176 4177 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, 4178 struct snd_pcm_hw_params_old __user * _oparams) 4179 { 4180 int err; 4181 4182 struct snd_pcm_hw_params *params __free(kfree) = 4183 kmalloc_obj(*params); 4184 if (!params) 4185 return -ENOMEM; 4186 4187 struct snd_pcm_hw_params_old *oparams __free(kfree) = 4188 memdup_user(_oparams, sizeof(*oparams)); 4189 if (IS_ERR(oparams)) 4190 return PTR_ERR(oparams); 4191 4192 snd_pcm_hw_convert_from_old_params(params, oparams); 4193 err = snd_pcm_hw_params(substream, params); 4194 if (err < 0) 4195 return err; 4196 4197 snd_pcm_hw_convert_to_old_params(oparams, params); 4198 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) 4199 return -EFAULT; 4200 return 0; 4201 } 4202 #endif /* CONFIG_SND_SUPPORT_OLD_API */ 4203 4204 #ifndef CONFIG_MMU 4205 static unsigned long snd_pcm_get_unmapped_area(struct file *file, 4206 unsigned long addr, 4207 unsigned long len, 4208 unsigned long pgoff, 4209 unsigned long flags) 4210 { 4211 struct snd_pcm_file *pcm_file = file->private_data; 4212 struct snd_pcm_substream *substream = pcm_file->substream; 4213 struct snd_pcm_runtime *runtime = substream->runtime; 4214 unsigned long offset = pgoff << PAGE_SHIFT; 4215 4216 switch (offset) { 4217 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW: 4218 return (unsigned long)runtime->status; 4219 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW: 4220 return (unsigned long)runtime->control; 4221 default: 4222 return (unsigned long)runtime->dma_area + offset; 4223 } 4224 } 4225 #else 4226 # define snd_pcm_get_unmapped_area NULL 4227 #endif 4228 4229 /* 4230 * Register section 4231 */ 4232 4233 const struct file_operations snd_pcm_f_ops[2] = { 4234 { 4235 .owner = THIS_MODULE, 4236 .write = snd_pcm_write, 4237 .write_iter = snd_pcm_writev, 4238 .open = snd_pcm_playback_open, 4239 .release = snd_pcm_release, 4240 .poll = snd_pcm_poll, 4241 .unlocked_ioctl = snd_pcm_ioctl, 4242 .compat_ioctl = snd_pcm_ioctl_compat, 4243 .mmap = snd_pcm_mmap, 4244 .fasync = snd_pcm_fasync, 4245 .get_unmapped_area = snd_pcm_get_unmapped_area, 4246 }, 4247 { 4248 .owner = THIS_MODULE, 4249 .read = snd_pcm_read, 4250 .read_iter = snd_pcm_readv, 4251 .open = snd_pcm_capture_open, 4252 .release = snd_pcm_release, 4253 .poll = snd_pcm_poll, 4254 .unlocked_ioctl = snd_pcm_ioctl, 4255 .compat_ioctl = snd_pcm_ioctl_compat, 4256 .mmap = snd_pcm_mmap, 4257 .fasync = snd_pcm_fasync, 4258 .get_unmapped_area = snd_pcm_get_unmapped_area, 4259 } 4260 }; 4261