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