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