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