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 * After this call, all streams are changed to SUSPENDED state. 1784 * 1785 * Return: Zero if successful (or @pcm is %NULL), or a negative error code. 1786 */ 1787 int snd_pcm_suspend_all(struct snd_pcm *pcm) 1788 { 1789 struct snd_pcm_substream *substream; 1790 int stream, err = 0; 1791 1792 if (! pcm) 1793 return 0; 1794 1795 for_each_pcm_substream(pcm, stream, substream) { 1796 /* FIXME: the open/close code should lock this as well */ 1797 if (!substream->runtime) 1798 continue; 1799 1800 /* 1801 * Skip BE dai link PCM's that are internal and may 1802 * not have their substream ops set. 1803 */ 1804 if (!substream->ops) 1805 continue; 1806 1807 err = snd_pcm_suspend(substream); 1808 if (err < 0 && err != -EBUSY) 1809 return err; 1810 } 1811 1812 for_each_pcm_substream(pcm, stream, substream) 1813 snd_pcm_sync_stop(substream, false); 1814 1815 return 0; 1816 } 1817 EXPORT_SYMBOL(snd_pcm_suspend_all); 1818 1819 /* resume callbacks: state argument ignored */ 1820 1821 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, 1822 snd_pcm_state_t state) 1823 { 1824 struct snd_pcm_runtime *runtime = substream->runtime; 1825 if (runtime->state != SNDRV_PCM_STATE_SUSPENDED) 1826 return -EBADFD; 1827 if (!(runtime->info & SNDRV_PCM_INFO_RESUME)) 1828 return -ENOSYS; 1829 runtime->trigger_master = substream; 1830 return 0; 1831 } 1832 1833 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, 1834 snd_pcm_state_t state) 1835 { 1836 struct snd_pcm_runtime *runtime = substream->runtime; 1837 if (runtime->trigger_master != substream) 1838 return 0; 1839 /* DMA not running previously? */ 1840 if (runtime->suspended_state != SNDRV_PCM_STATE_RUNNING && 1841 (runtime->suspended_state != SNDRV_PCM_STATE_DRAINING || 1842 substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) 1843 return 0; 1844 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME); 1845 } 1846 1847 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, 1848 snd_pcm_state_t state) 1849 { 1850 if (substream->runtime->trigger_master == substream && 1851 snd_pcm_running(substream)) 1852 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); 1853 } 1854 1855 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, 1856 snd_pcm_state_t state) 1857 { 1858 struct snd_pcm_runtime *runtime = substream->runtime; 1859 snd_pcm_trigger_tstamp(substream); 1860 __snd_pcm_set_state(runtime, runtime->suspended_state); 1861 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME); 1862 } 1863 1864 static const struct action_ops snd_pcm_action_resume = { 1865 .pre_action = snd_pcm_pre_resume, 1866 .do_action = snd_pcm_do_resume, 1867 .undo_action = snd_pcm_undo_resume, 1868 .post_action = snd_pcm_post_resume 1869 }; 1870 1871 static int snd_pcm_resume(struct snd_pcm_substream *substream) 1872 { 1873 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 1874 ACTION_ARG_IGNORE); 1875 } 1876 1877 #else 1878 1879 static int snd_pcm_resume(struct snd_pcm_substream *substream) 1880 { 1881 return -ENOSYS; 1882 } 1883 1884 #endif /* CONFIG_PM */ 1885 1886 /* 1887 * xrun ioctl 1888 * 1889 * Change the RUNNING stream(s) to XRUN state. 1890 */ 1891 static int snd_pcm_xrun(struct snd_pcm_substream *substream) 1892 { 1893 struct snd_pcm_runtime *runtime = substream->runtime; 1894 1895 guard(pcm_stream_lock_irq)(substream); 1896 switch (runtime->state) { 1897 case SNDRV_PCM_STATE_XRUN: 1898 return 0; /* already there */ 1899 case SNDRV_PCM_STATE_RUNNING: 1900 __snd_pcm_xrun(substream); 1901 return 0; 1902 default: 1903 return -EBADFD; 1904 } 1905 } 1906 1907 /* 1908 * reset ioctl 1909 */ 1910 /* reset callbacks: state argument ignored */ 1911 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, 1912 snd_pcm_state_t state) 1913 { 1914 struct snd_pcm_runtime *runtime = substream->runtime; 1915 switch (runtime->state) { 1916 case SNDRV_PCM_STATE_RUNNING: 1917 case SNDRV_PCM_STATE_PREPARED: 1918 case SNDRV_PCM_STATE_PAUSED: 1919 case SNDRV_PCM_STATE_SUSPENDED: 1920 return 0; 1921 default: 1922 return -EBADFD; 1923 } 1924 } 1925 1926 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, 1927 snd_pcm_state_t state) 1928 { 1929 struct snd_pcm_runtime *runtime = substream->runtime; 1930 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL); 1931 if (err < 0) 1932 return err; 1933 guard(pcm_stream_lock_irq)(substream); 1934 runtime->hw_ptr_base = 0; 1935 runtime->hw_ptr_interrupt = runtime->status->hw_ptr - 1936 runtime->status->hw_ptr % runtime->period_size; 1937 runtime->silence_start = runtime->status->hw_ptr; 1938 runtime->silence_filled = 0; 1939 return 0; 1940 } 1941 1942 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, 1943 snd_pcm_state_t state) 1944 { 1945 struct snd_pcm_runtime *runtime = substream->runtime; 1946 guard(pcm_stream_lock_irq)(substream); 1947 runtime->control->appl_ptr = runtime->status->hw_ptr; 1948 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 1949 runtime->silence_size > 0) 1950 snd_pcm_playback_silence(substream, ULONG_MAX); 1951 } 1952 1953 static const struct action_ops snd_pcm_action_reset = { 1954 .pre_action = snd_pcm_pre_reset, 1955 .do_action = snd_pcm_do_reset, 1956 .post_action = snd_pcm_post_reset 1957 }; 1958 1959 static int snd_pcm_reset(struct snd_pcm_substream *substream) 1960 { 1961 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 1962 ACTION_ARG_IGNORE); 1963 } 1964 1965 /* 1966 * prepare ioctl 1967 */ 1968 /* pass f_flags as state argument */ 1969 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, 1970 snd_pcm_state_t state) 1971 { 1972 struct snd_pcm_runtime *runtime = substream->runtime; 1973 int f_flags = (__force int)state; 1974 1975 if (runtime->state == SNDRV_PCM_STATE_OPEN || 1976 runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 1977 return -EBADFD; 1978 if (snd_pcm_running(substream)) 1979 return -EBUSY; 1980 substream->f_flags = f_flags; 1981 return 0; 1982 } 1983 1984 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, 1985 snd_pcm_state_t state) 1986 { 1987 int err; 1988 snd_pcm_sync_stop(substream, true); 1989 err = substream->ops->prepare(substream); 1990 if (err < 0) 1991 return err; 1992 return snd_pcm_do_reset(substream, state); 1993 } 1994 1995 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, 1996 snd_pcm_state_t state) 1997 { 1998 struct snd_pcm_runtime *runtime = substream->runtime; 1999 runtime->control->appl_ptr = runtime->status->hw_ptr; 2000 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED); 2001 } 2002 2003 static const struct action_ops snd_pcm_action_prepare = { 2004 .pre_action = snd_pcm_pre_prepare, 2005 .do_action = snd_pcm_do_prepare, 2006 .post_action = snd_pcm_post_prepare 2007 }; 2008 2009 /** 2010 * snd_pcm_prepare - prepare the PCM substream to be triggerable 2011 * @substream: the PCM substream instance 2012 * @file: file to refer f_flags 2013 * 2014 * Return: Zero if successful, or a negative error code. 2015 */ 2016 static int snd_pcm_prepare(struct snd_pcm_substream *substream, 2017 struct file *file) 2018 { 2019 int f_flags; 2020 2021 if (file) 2022 f_flags = file->f_flags; 2023 else 2024 f_flags = substream->f_flags; 2025 2026 scoped_guard(pcm_stream_lock_irq, substream) { 2027 switch (substream->runtime->state) { 2028 case SNDRV_PCM_STATE_PAUSED: 2029 snd_pcm_pause(substream, false); 2030 fallthrough; 2031 case SNDRV_PCM_STATE_SUSPENDED: 2032 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); 2033 break; 2034 } 2035 } 2036 2037 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare, 2038 substream, 2039 (__force snd_pcm_state_t)f_flags); 2040 } 2041 2042 /* 2043 * drain ioctl 2044 */ 2045 2046 /* drain init callbacks: state argument ignored */ 2047 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, 2048 snd_pcm_state_t state) 2049 { 2050 struct snd_pcm_runtime *runtime = substream->runtime; 2051 switch (runtime->state) { 2052 case SNDRV_PCM_STATE_OPEN: 2053 case SNDRV_PCM_STATE_DISCONNECTED: 2054 case SNDRV_PCM_STATE_SUSPENDED: 2055 return -EBADFD; 2056 } 2057 runtime->trigger_master = substream; 2058 return 0; 2059 } 2060 2061 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, 2062 snd_pcm_state_t state) 2063 { 2064 struct snd_pcm_runtime *runtime = substream->runtime; 2065 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 2066 switch (runtime->state) { 2067 case SNDRV_PCM_STATE_PREPARED: 2068 /* start playback stream if possible */ 2069 if (! snd_pcm_playback_empty(substream)) { 2070 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING); 2071 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING); 2072 } else { 2073 __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_SETUP); 2074 } 2075 break; 2076 case SNDRV_PCM_STATE_RUNNING: 2077 __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_DRAINING); 2078 break; 2079 case SNDRV_PCM_STATE_XRUN: 2080 __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_SETUP); 2081 break; 2082 default: 2083 break; 2084 } 2085 } else { 2086 /* stop running stream */ 2087 if (runtime->state == SNDRV_PCM_STATE_RUNNING) { 2088 snd_pcm_state_t new_state; 2089 2090 new_state = snd_pcm_capture_avail(runtime) > 0 ? 2091 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP; 2092 snd_pcm_do_stop(substream, new_state); 2093 snd_pcm_post_stop(substream, new_state); 2094 } 2095 } 2096 2097 if (runtime->state == SNDRV_PCM_STATE_DRAINING && 2098 runtime->trigger_master == substream && 2099 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER)) 2100 return substream->ops->trigger(substream, 2101 SNDRV_PCM_TRIGGER_DRAIN); 2102 2103 return 0; 2104 } 2105 2106 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, 2107 snd_pcm_state_t state) 2108 { 2109 } 2110 2111 static const struct action_ops snd_pcm_action_drain_init = { 2112 .pre_action = snd_pcm_pre_drain_init, 2113 .do_action = snd_pcm_do_drain_init, 2114 .post_action = snd_pcm_post_drain_init 2115 }; 2116 2117 /* 2118 * Drain the stream(s). 2119 * When the substream is linked, sync until the draining of all playback streams 2120 * is finished. 2121 * After this call, all streams are supposed to be either SETUP or DRAINING 2122 * (capture only) state. 2123 */ 2124 static int snd_pcm_drain(struct snd_pcm_substream *substream, 2125 struct file *file) 2126 { 2127 struct snd_card *card; 2128 struct snd_pcm_runtime *runtime; 2129 struct snd_pcm_substream *s; 2130 struct snd_pcm_group *group; 2131 wait_queue_entry_t wait; 2132 int result = 0; 2133 int nonblock = 0; 2134 2135 card = substream->pcm->card; 2136 runtime = substream->runtime; 2137 2138 if (runtime->state == SNDRV_PCM_STATE_OPEN) 2139 return -EBADFD; 2140 2141 if (file) { 2142 if (file->f_flags & O_NONBLOCK) 2143 nonblock = 1; 2144 } else if (substream->f_flags & O_NONBLOCK) 2145 nonblock = 1; 2146 2147 snd_pcm_stream_lock_irq(substream); 2148 /* resume pause */ 2149 if (runtime->state == SNDRV_PCM_STATE_PAUSED) 2150 snd_pcm_pause(substream, false); 2151 2152 /* pre-start/stop - all running streams are changed to DRAINING state */ 2153 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 2154 ACTION_ARG_IGNORE); 2155 if (result < 0) 2156 goto unlock; 2157 /* in non-blocking, we don't wait in ioctl but let caller poll */ 2158 if (nonblock) { 2159 result = -EAGAIN; 2160 goto unlock; 2161 } 2162 2163 for (;;) { 2164 long tout; 2165 struct snd_pcm_runtime *to_check; 2166 if (signal_pending(current)) { 2167 result = -ERESTARTSYS; 2168 break; 2169 } 2170 /* find a substream to drain */ 2171 to_check = NULL; 2172 group = snd_pcm_stream_group_ref(substream); 2173 snd_pcm_group_for_each_entry(s, substream) { 2174 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK) 2175 continue; 2176 runtime = s->runtime; 2177 if (runtime->state == SNDRV_PCM_STATE_DRAINING) { 2178 to_check = runtime; 2179 break; 2180 } 2181 } 2182 snd_pcm_group_unref(group, substream); 2183 if (!to_check) 2184 break; /* all drained */ 2185 init_waitqueue_entry(&wait, current); 2186 set_current_state(TASK_INTERRUPTIBLE); 2187 add_wait_queue(&to_check->sleep, &wait); 2188 snd_pcm_stream_unlock_irq(substream); 2189 if (runtime->no_period_wakeup) 2190 tout = MAX_SCHEDULE_TIMEOUT; 2191 else { 2192 tout = 100; 2193 if (runtime->rate) { 2194 long t = runtime->buffer_size * 1100 / runtime->rate; 2195 tout = max(t, tout); 2196 } 2197 tout = msecs_to_jiffies(tout); 2198 } 2199 tout = schedule_timeout(tout); 2200 2201 snd_pcm_stream_lock_irq(substream); 2202 group = snd_pcm_stream_group_ref(substream); 2203 snd_pcm_group_for_each_entry(s, substream) { 2204 if (s->runtime == to_check) { 2205 remove_wait_queue(&to_check->sleep, &wait); 2206 break; 2207 } 2208 } 2209 snd_pcm_group_unref(group, substream); 2210 2211 if (card->shutdown) { 2212 result = -ENODEV; 2213 break; 2214 } 2215 if (tout == 0) { 2216 if (substream->runtime->state == SNDRV_PCM_STATE_SUSPENDED) 2217 result = -ESTRPIPE; 2218 else { 2219 dev_dbg(substream->pcm->card->dev, 2220 "playback drain timeout (DMA or IRQ trouble?)\n"); 2221 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); 2222 result = -EIO; 2223 } 2224 break; 2225 } 2226 } 2227 2228 unlock: 2229 snd_pcm_stream_unlock_irq(substream); 2230 2231 return result; 2232 } 2233 2234 /* 2235 * drop ioctl 2236 * 2237 * Immediately put all linked substreams into SETUP state. 2238 */ 2239 static int snd_pcm_drop(struct snd_pcm_substream *substream) 2240 { 2241 struct snd_pcm_runtime *runtime; 2242 int result = 0; 2243 2244 if (PCM_RUNTIME_CHECK(substream)) 2245 return -ENXIO; 2246 runtime = substream->runtime; 2247 2248 if (runtime->state == SNDRV_PCM_STATE_OPEN || 2249 runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 2250 return -EBADFD; 2251 2252 guard(pcm_stream_lock_irq)(substream); 2253 /* resume pause */ 2254 if (runtime->state == SNDRV_PCM_STATE_PAUSED) 2255 snd_pcm_pause(substream, false); 2256 2257 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); 2258 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */ 2259 2260 return result; 2261 } 2262 2263 2264 static bool is_pcm_file(struct file *file) 2265 { 2266 struct inode *inode = file_inode(file); 2267 struct snd_pcm *pcm; 2268 unsigned int minor; 2269 2270 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major) 2271 return false; 2272 minor = iminor(inode); 2273 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK); 2274 if (!pcm) 2275 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE); 2276 if (!pcm) 2277 return false; 2278 snd_card_unref(pcm->card); 2279 return true; 2280 } 2281 2282 /* 2283 * PCM link handling 2284 */ 2285 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd) 2286 { 2287 struct snd_pcm_file *pcm_file; 2288 struct snd_pcm_substream *substream1; 2289 struct snd_pcm_group *target_group; 2290 bool nonatomic = substream->pcm->nonatomic; 2291 CLASS(fd, f)(fd); 2292 2293 if (fd_empty(f)) 2294 return -EBADFD; 2295 if (!is_pcm_file(fd_file(f))) 2296 return -EBADFD; 2297 2298 pcm_file = fd_file(f)->private_data; 2299 substream1 = pcm_file->substream; 2300 2301 if (substream == substream1) 2302 return -EINVAL; 2303 2304 struct snd_pcm_group *group __free(kfree) = 2305 kzalloc(sizeof(*group), GFP_KERNEL); 2306 if (!group) 2307 return -ENOMEM; 2308 snd_pcm_group_init(group); 2309 2310 guard(rwsem_write)(&snd_pcm_link_rwsem); 2311 if (substream->runtime->state == SNDRV_PCM_STATE_OPEN || 2312 substream->runtime->state != substream1->runtime->state || 2313 substream->pcm->nonatomic != substream1->pcm->nonatomic) 2314 return -EBADFD; 2315 if (snd_pcm_stream_linked(substream1)) 2316 return -EALREADY; 2317 2318 scoped_guard(pcm_stream_lock_irq, substream) { 2319 if (!snd_pcm_stream_linked(substream)) { 2320 snd_pcm_group_assign(substream, group); 2321 group = NULL; /* assigned, don't free this one below */ 2322 } 2323 target_group = substream->group; 2324 } 2325 2326 snd_pcm_group_lock_irq(target_group, nonatomic); 2327 snd_pcm_stream_lock_nested(substream1); 2328 snd_pcm_group_assign(substream1, target_group); 2329 refcount_inc(&target_group->refs); 2330 snd_pcm_stream_unlock(substream1); 2331 snd_pcm_group_unlock_irq(target_group, nonatomic); 2332 return 0; 2333 } 2334 2335 static void relink_to_local(struct snd_pcm_substream *substream) 2336 { 2337 snd_pcm_stream_lock_nested(substream); 2338 snd_pcm_group_assign(substream, &substream->self_group); 2339 snd_pcm_stream_unlock(substream); 2340 } 2341 2342 static int snd_pcm_unlink(struct snd_pcm_substream *substream) 2343 { 2344 struct snd_pcm_group *group; 2345 bool nonatomic = substream->pcm->nonatomic; 2346 bool do_free = false; 2347 2348 guard(rwsem_write)(&snd_pcm_link_rwsem); 2349 2350 if (!snd_pcm_stream_linked(substream)) 2351 return -EALREADY; 2352 2353 group = substream->group; 2354 snd_pcm_group_lock_irq(group, nonatomic); 2355 2356 relink_to_local(substream); 2357 refcount_dec(&group->refs); 2358 2359 /* detach the last stream, too */ 2360 if (list_is_singular(&group->substreams)) { 2361 relink_to_local(list_first_entry(&group->substreams, 2362 struct snd_pcm_substream, 2363 link_list)); 2364 do_free = refcount_dec_and_test(&group->refs); 2365 } 2366 2367 snd_pcm_group_unlock_irq(group, nonatomic); 2368 if (do_free) 2369 kfree(group); 2370 return 0; 2371 } 2372 2373 /* 2374 * hw configurator 2375 */ 2376 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params, 2377 struct snd_pcm_hw_rule *rule) 2378 { 2379 struct snd_interval t; 2380 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]), 2381 hw_param_interval_c(params, rule->deps[1]), &t); 2382 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2383 } 2384 2385 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params, 2386 struct snd_pcm_hw_rule *rule) 2387 { 2388 struct snd_interval t; 2389 snd_interval_div(hw_param_interval_c(params, rule->deps[0]), 2390 hw_param_interval_c(params, rule->deps[1]), &t); 2391 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2392 } 2393 2394 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params, 2395 struct snd_pcm_hw_rule *rule) 2396 { 2397 struct snd_interval t; 2398 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]), 2399 hw_param_interval_c(params, rule->deps[1]), 2400 (unsigned long) rule->private, &t); 2401 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2402 } 2403 2404 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params, 2405 struct snd_pcm_hw_rule *rule) 2406 { 2407 struct snd_interval t; 2408 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]), 2409 (unsigned long) rule->private, 2410 hw_param_interval_c(params, rule->deps[1]), &t); 2411 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2412 } 2413 2414 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params, 2415 struct snd_pcm_hw_rule *rule) 2416 { 2417 snd_pcm_format_t k; 2418 const struct snd_interval *i = 2419 hw_param_interval_c(params, rule->deps[0]); 2420 struct snd_mask m; 2421 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 2422 snd_mask_any(&m); 2423 pcm_for_each_format(k) { 2424 int bits; 2425 if (!snd_mask_test_format(mask, k)) 2426 continue; 2427 bits = snd_pcm_format_physical_width(k); 2428 if (bits <= 0) 2429 continue; /* ignore invalid formats */ 2430 if ((unsigned)bits < i->min || (unsigned)bits > i->max) 2431 snd_mask_reset(&m, (__force unsigned)k); 2432 } 2433 return snd_mask_refine(mask, &m); 2434 } 2435 2436 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params, 2437 struct snd_pcm_hw_rule *rule) 2438 { 2439 struct snd_interval t; 2440 snd_pcm_format_t k; 2441 2442 t.min = UINT_MAX; 2443 t.max = 0; 2444 t.openmin = 0; 2445 t.openmax = 0; 2446 pcm_for_each_format(k) { 2447 int bits; 2448 if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k)) 2449 continue; 2450 bits = snd_pcm_format_physical_width(k); 2451 if (bits <= 0) 2452 continue; /* ignore invalid formats */ 2453 if (t.min > (unsigned)bits) 2454 t.min = bits; 2455 if (t.max < (unsigned)bits) 2456 t.max = bits; 2457 } 2458 t.integer = 1; 2459 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2460 } 2461 2462 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 ||\ 2463 SNDRV_PCM_RATE_128000 != 1 << 19 2464 #error "Change this table" 2465 #endif 2466 2467 /* NOTE: the list is unsorted! */ 2468 static const unsigned int rates[] = { 2469 5512, 8000, 11025, 16000, 22050, 32000, 44100, 2470 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000, 705600, 768000, 2471 /* extended */ 2472 12000, 24000, 128000 2473 }; 2474 2475 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = { 2476 .count = ARRAY_SIZE(rates), 2477 .list = rates, 2478 }; 2479 2480 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params, 2481 struct snd_pcm_hw_rule *rule) 2482 { 2483 struct snd_pcm_hardware *hw = rule->private; 2484 return snd_interval_list(hw_param_interval(params, rule->var), 2485 snd_pcm_known_rates.count, 2486 snd_pcm_known_rates.list, hw->rates); 2487 } 2488 2489 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params, 2490 struct snd_pcm_hw_rule *rule) 2491 { 2492 struct snd_interval t; 2493 struct snd_pcm_substream *substream = rule->private; 2494 t.min = 0; 2495 t.max = substream->buffer_bytes_max; 2496 t.openmin = 0; 2497 t.openmax = 0; 2498 t.integer = 1; 2499 return snd_interval_refine(hw_param_interval(params, rule->var), &t); 2500 } 2501 2502 static int snd_pcm_hw_rule_subformats(struct snd_pcm_hw_params *params, 2503 struct snd_pcm_hw_rule *rule) 2504 { 2505 struct snd_mask *sfmask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_SUBFORMAT); 2506 struct snd_mask *fmask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 2507 u32 *subformats = rule->private; 2508 snd_pcm_format_t f; 2509 struct snd_mask m; 2510 2511 snd_mask_none(&m); 2512 /* All PCMs support at least the default STD subformat. */ 2513 snd_mask_set(&m, (__force unsigned)SNDRV_PCM_SUBFORMAT_STD); 2514 2515 pcm_for_each_format(f) { 2516 if (!snd_mask_test(fmask, (__force unsigned)f)) 2517 continue; 2518 2519 if (f == SNDRV_PCM_FORMAT_S32_LE && *subformats) 2520 m.bits[0] |= *subformats; 2521 else if (snd_pcm_format_linear(f)) 2522 snd_mask_set(&m, (__force unsigned)SNDRV_PCM_SUBFORMAT_MSBITS_MAX); 2523 } 2524 2525 return snd_mask_refine(sfmask, &m); 2526 } 2527 2528 static int snd_pcm_hw_constraint_subformats(struct snd_pcm_runtime *runtime, 2529 unsigned int cond, u32 *subformats) 2530 { 2531 return snd_pcm_hw_rule_add(runtime, cond, -1, 2532 snd_pcm_hw_rule_subformats, (void *)subformats, 2533 SNDRV_PCM_HW_PARAM_SUBFORMAT, 2534 SNDRV_PCM_HW_PARAM_FORMAT, -1); 2535 } 2536 2537 static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream) 2538 { 2539 struct snd_pcm_runtime *runtime = substream->runtime; 2540 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; 2541 int k, err; 2542 2543 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { 2544 snd_mask_any(constrs_mask(constrs, k)); 2545 } 2546 2547 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { 2548 snd_interval_any(constrs_interval(constrs, k)); 2549 } 2550 2551 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS)); 2552 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)); 2553 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)); 2554 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)); 2555 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS)); 2556 2557 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, 2558 snd_pcm_hw_rule_format, NULL, 2559 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 2560 if (err < 0) 2561 return err; 2562 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 2563 snd_pcm_hw_rule_sample_bits, NULL, 2564 SNDRV_PCM_HW_PARAM_FORMAT, 2565 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 2566 if (err < 0) 2567 return err; 2568 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 2569 snd_pcm_hw_rule_div, NULL, 2570 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); 2571 if (err < 0) 2572 return err; 2573 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 2574 snd_pcm_hw_rule_mul, NULL, 2575 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); 2576 if (err < 0) 2577 return err; 2578 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 2579 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2580 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); 2581 if (err < 0) 2582 return err; 2583 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 2584 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2585 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1); 2586 if (err < 0) 2587 return err; 2588 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2589 snd_pcm_hw_rule_div, NULL, 2590 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 2591 if (err < 0) 2592 return err; 2593 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 2594 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2595 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1); 2596 if (err < 0) 2597 return err; 2598 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 2599 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2600 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1); 2601 if (err < 0) 2602 return err; 2603 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, 2604 snd_pcm_hw_rule_div, NULL, 2605 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); 2606 if (err < 0) 2607 return err; 2608 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2609 snd_pcm_hw_rule_div, NULL, 2610 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); 2611 if (err < 0) 2612 return err; 2613 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2614 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2615 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2616 if (err < 0) 2617 return err; 2618 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2619 snd_pcm_hw_rule_muldivk, (void*) 1000000, 2620 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); 2621 if (err < 0) 2622 return err; 2623 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 2624 snd_pcm_hw_rule_mul, NULL, 2625 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); 2626 if (err < 0) 2627 return err; 2628 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 2629 snd_pcm_hw_rule_mulkdiv, (void*) 8, 2630 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2631 if (err < 0) 2632 return err; 2633 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 2634 snd_pcm_hw_rule_muldivk, (void*) 1000000, 2635 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); 2636 if (err < 0) 2637 return err; 2638 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 2639 snd_pcm_hw_rule_muldivk, (void*) 8, 2640 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2641 if (err < 0) 2642 return err; 2643 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2644 snd_pcm_hw_rule_muldivk, (void*) 8, 2645 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); 2646 if (err < 0) 2647 return err; 2648 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 2649 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2650 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); 2651 if (err < 0) 2652 return err; 2653 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 2654 snd_pcm_hw_rule_mulkdiv, (void*) 1000000, 2655 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); 2656 if (err < 0) 2657 return err; 2658 return 0; 2659 } 2660 2661 static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream) 2662 { 2663 struct snd_pcm_runtime *runtime = substream->runtime; 2664 struct snd_pcm_hardware *hw = &runtime->hw; 2665 int err; 2666 unsigned int mask = 0; 2667 2668 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) 2669 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED); 2670 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) 2671 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED); 2672 if (hw_support_mmap(substream)) { 2673 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) 2674 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); 2675 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) 2676 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED); 2677 if (hw->info & SNDRV_PCM_INFO_COMPLEX) 2678 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX); 2679 } 2680 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask); 2681 if (err < 0) 2682 return err; 2683 2684 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats); 2685 if (err < 0) 2686 return err; 2687 2688 err = snd_pcm_hw_constraint_subformats(runtime, 0, &hw->subformats); 2689 if (err < 0) 2690 return err; 2691 2692 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2693 hw->channels_min, hw->channels_max); 2694 if (err < 0) 2695 return err; 2696 2697 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, 2698 hw->rate_min, hw->rate_max); 2699 if (err < 0) 2700 return err; 2701 2702 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 2703 hw->period_bytes_min, hw->period_bytes_max); 2704 if (err < 0) 2705 return err; 2706 2707 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS, 2708 hw->periods_min, hw->periods_max); 2709 if (err < 0) 2710 return err; 2711 2712 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2713 hw->period_bytes_min, hw->buffer_bytes_max); 2714 if (err < 0) 2715 return err; 2716 2717 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2718 snd_pcm_hw_rule_buffer_bytes_max, substream, 2719 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1); 2720 if (err < 0) 2721 return err; 2722 2723 /* FIXME: remove */ 2724 if (runtime->dma_bytes) { 2725 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes); 2726 if (err < 0) 2727 return err; 2728 } 2729 2730 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) { 2731 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 2732 snd_pcm_hw_rule_rate, hw, 2733 SNDRV_PCM_HW_PARAM_RATE, -1); 2734 if (err < 0) 2735 return err; 2736 } 2737 2738 /* FIXME: this belong to lowlevel */ 2739 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); 2740 2741 return 0; 2742 } 2743 2744 static void pcm_release_private(struct snd_pcm_substream *substream) 2745 { 2746 if (snd_pcm_stream_linked(substream)) 2747 snd_pcm_unlink(substream); 2748 } 2749 2750 void snd_pcm_release_substream(struct snd_pcm_substream *substream) 2751 { 2752 substream->ref_count--; 2753 if (substream->ref_count > 0) 2754 return; 2755 2756 snd_pcm_drop(substream); 2757 if (substream->hw_opened) { 2758 if (substream->runtime->state != SNDRV_PCM_STATE_OPEN) 2759 do_hw_free(substream); 2760 substream->ops->close(substream); 2761 substream->hw_opened = 0; 2762 } 2763 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req)) 2764 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req); 2765 if (substream->pcm_release) { 2766 substream->pcm_release(substream); 2767 substream->pcm_release = NULL; 2768 } 2769 snd_pcm_detach_substream(substream); 2770 } 2771 EXPORT_SYMBOL(snd_pcm_release_substream); 2772 2773 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, 2774 struct file *file, 2775 struct snd_pcm_substream **rsubstream) 2776 { 2777 struct snd_pcm_substream *substream; 2778 int err; 2779 2780 err = snd_pcm_attach_substream(pcm, stream, file, &substream); 2781 if (err < 0) 2782 return err; 2783 if (substream->ref_count > 1) { 2784 *rsubstream = substream; 2785 return 0; 2786 } 2787 2788 err = snd_pcm_hw_constraints_init(substream); 2789 if (err < 0) { 2790 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n"); 2791 goto error; 2792 } 2793 2794 err = substream->ops->open(substream); 2795 if (err < 0) 2796 goto error; 2797 2798 substream->hw_opened = 1; 2799 2800 err = snd_pcm_hw_constraints_complete(substream); 2801 if (err < 0) { 2802 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n"); 2803 goto error; 2804 } 2805 2806 /* automatically set EXPLICIT_SYNC flag in the managed mode whenever 2807 * the DMA buffer requires it 2808 */ 2809 if (substream->managed_buffer_alloc && 2810 substream->dma_buffer.dev.need_sync) 2811 substream->runtime->hw.info |= SNDRV_PCM_INFO_EXPLICIT_SYNC; 2812 2813 *rsubstream = substream; 2814 return 0; 2815 2816 error: 2817 snd_pcm_release_substream(substream); 2818 return err; 2819 } 2820 EXPORT_SYMBOL(snd_pcm_open_substream); 2821 2822 static int snd_pcm_open_file(struct file *file, 2823 struct snd_pcm *pcm, 2824 int stream) 2825 { 2826 struct snd_pcm_file *pcm_file; 2827 struct snd_pcm_substream *substream; 2828 int err; 2829 2830 err = snd_pcm_open_substream(pcm, stream, file, &substream); 2831 if (err < 0) 2832 return err; 2833 2834 pcm_file = kzalloc_obj(*pcm_file); 2835 if (pcm_file == NULL) { 2836 snd_pcm_release_substream(substream); 2837 return -ENOMEM; 2838 } 2839 pcm_file->substream = substream; 2840 if (substream->ref_count == 1) 2841 substream->pcm_release = pcm_release_private; 2842 file->private_data = pcm_file; 2843 2844 return 0; 2845 } 2846 2847 static int snd_pcm_playback_open(struct inode *inode, struct file *file) 2848 { 2849 struct snd_pcm *pcm; 2850 int err = nonseekable_open(inode, file); 2851 if (err < 0) 2852 return err; 2853 pcm = snd_lookup_minor_data(iminor(inode), 2854 SNDRV_DEVICE_TYPE_PCM_PLAYBACK); 2855 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK); 2856 if (pcm) 2857 snd_card_unref(pcm->card); 2858 return err; 2859 } 2860 2861 static int snd_pcm_capture_open(struct inode *inode, struct file *file) 2862 { 2863 struct snd_pcm *pcm; 2864 int err = nonseekable_open(inode, file); 2865 if (err < 0) 2866 return err; 2867 pcm = snd_lookup_minor_data(iminor(inode), 2868 SNDRV_DEVICE_TYPE_PCM_CAPTURE); 2869 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE); 2870 if (pcm) 2871 snd_card_unref(pcm->card); 2872 return err; 2873 } 2874 2875 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream) 2876 { 2877 int err; 2878 wait_queue_entry_t wait; 2879 2880 if (pcm == NULL) { 2881 err = -ENODEV; 2882 goto __error1; 2883 } 2884 err = snd_card_file_add(pcm->card, file); 2885 if (err < 0) 2886 goto __error1; 2887 if (!try_module_get(pcm->card->module)) { 2888 err = -EFAULT; 2889 goto __error2; 2890 } 2891 init_waitqueue_entry(&wait, current); 2892 add_wait_queue(&pcm->open_wait, &wait); 2893 mutex_lock(&pcm->open_mutex); 2894 while (1) { 2895 err = snd_pcm_open_file(file, pcm, stream); 2896 if (err >= 0) 2897 break; 2898 if (err == -EAGAIN) { 2899 if (file->f_flags & O_NONBLOCK) { 2900 err = -EBUSY; 2901 break; 2902 } 2903 } else 2904 break; 2905 set_current_state(TASK_INTERRUPTIBLE); 2906 mutex_unlock(&pcm->open_mutex); 2907 schedule(); 2908 mutex_lock(&pcm->open_mutex); 2909 if (pcm->card->shutdown) { 2910 err = -ENODEV; 2911 break; 2912 } 2913 if (signal_pending(current)) { 2914 err = -ERESTARTSYS; 2915 break; 2916 } 2917 } 2918 remove_wait_queue(&pcm->open_wait, &wait); 2919 mutex_unlock(&pcm->open_mutex); 2920 if (err < 0) 2921 goto __error; 2922 return err; 2923 2924 __error: 2925 module_put(pcm->card->module); 2926 __error2: 2927 snd_card_file_remove(pcm->card, file); 2928 __error1: 2929 return err; 2930 } 2931 2932 static int snd_pcm_release(struct inode *inode, struct file *file) 2933 { 2934 struct snd_pcm *pcm; 2935 struct snd_pcm_substream *substream; 2936 struct snd_pcm_file *pcm_file; 2937 2938 pcm_file = file->private_data; 2939 substream = pcm_file->substream; 2940 if (snd_BUG_ON(!substream)) 2941 return -ENXIO; 2942 pcm = substream->pcm; 2943 2944 /* block until the device gets woken up as it may touch the hardware */ 2945 snd_power_wait(pcm->card); 2946 2947 scoped_guard(mutex, &pcm->open_mutex) { 2948 snd_pcm_release_substream(substream); 2949 kfree(pcm_file); 2950 } 2951 wake_up(&pcm->open_wait); 2952 module_put(pcm->card->module); 2953 snd_card_file_remove(pcm->card, file); 2954 return 0; 2955 } 2956 2957 /* check and update PCM state; return 0 or a negative error 2958 * call this inside PCM lock 2959 */ 2960 static int do_pcm_hwsync(struct snd_pcm_substream *substream) 2961 { 2962 switch (substream->runtime->state) { 2963 case SNDRV_PCM_STATE_DRAINING: 2964 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 2965 return -EBADFD; 2966 fallthrough; 2967 case SNDRV_PCM_STATE_RUNNING: 2968 return snd_pcm_update_hw_ptr(substream); 2969 case SNDRV_PCM_STATE_PREPARED: 2970 case SNDRV_PCM_STATE_PAUSED: 2971 return 0; 2972 case SNDRV_PCM_STATE_SUSPENDED: 2973 return -ESTRPIPE; 2974 case SNDRV_PCM_STATE_XRUN: 2975 return -EPIPE; 2976 default: 2977 return -EBADFD; 2978 } 2979 } 2980 2981 /* increase the appl_ptr; returns the processed frames or a negative error */ 2982 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream, 2983 snd_pcm_uframes_t frames, 2984 snd_pcm_sframes_t avail) 2985 { 2986 struct snd_pcm_runtime *runtime = substream->runtime; 2987 snd_pcm_sframes_t appl_ptr; 2988 int ret; 2989 2990 if (avail <= 0) 2991 return 0; 2992 if (frames > (snd_pcm_uframes_t)avail) 2993 frames = avail; 2994 appl_ptr = runtime->control->appl_ptr + frames; 2995 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary) 2996 appl_ptr -= runtime->boundary; 2997 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr); 2998 return ret < 0 ? ret : frames; 2999 } 3000 3001 /* decrease the appl_ptr; returns the processed frames or zero for error */ 3002 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream, 3003 snd_pcm_uframes_t frames, 3004 snd_pcm_sframes_t avail) 3005 { 3006 struct snd_pcm_runtime *runtime = substream->runtime; 3007 snd_pcm_sframes_t appl_ptr; 3008 int ret; 3009 3010 if (avail <= 0) 3011 return 0; 3012 if (frames > (snd_pcm_uframes_t)avail) 3013 frames = avail; 3014 appl_ptr = runtime->control->appl_ptr - frames; 3015 if (appl_ptr < 0) 3016 appl_ptr += runtime->boundary; 3017 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr); 3018 /* NOTE: we return zero for errors because PulseAudio gets depressed 3019 * upon receiving an error from rewind ioctl and stops processing 3020 * any longer. Returning zero means that no rewind is done, so 3021 * it's not absolutely wrong to answer like that. 3022 */ 3023 return ret < 0 ? 0 : frames; 3024 } 3025 3026 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream, 3027 snd_pcm_uframes_t frames) 3028 { 3029 snd_pcm_sframes_t ret; 3030 3031 if (frames == 0) 3032 return 0; 3033 3034 scoped_guard(pcm_stream_lock_irq, substream) { 3035 ret = do_pcm_hwsync(substream); 3036 if (!ret) 3037 ret = rewind_appl_ptr(substream, frames, 3038 snd_pcm_hw_avail(substream)); 3039 } 3040 if (ret >= 0) 3041 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 3042 return ret; 3043 } 3044 3045 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream, 3046 snd_pcm_uframes_t frames) 3047 { 3048 snd_pcm_sframes_t ret; 3049 3050 if (frames == 0) 3051 return 0; 3052 3053 scoped_guard(pcm_stream_lock_irq, substream) { 3054 ret = do_pcm_hwsync(substream); 3055 if (!ret) 3056 ret = forward_appl_ptr(substream, frames, 3057 snd_pcm_avail(substream)); 3058 } 3059 if (ret >= 0) 3060 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 3061 return ret; 3062 } 3063 3064 static int snd_pcm_delay(struct snd_pcm_substream *substream, 3065 snd_pcm_sframes_t *delay) 3066 { 3067 int err; 3068 3069 scoped_guard(pcm_stream_lock_irq, substream) { 3070 err = do_pcm_hwsync(substream); 3071 if (delay && !err) 3072 *delay = snd_pcm_calc_delay(substream); 3073 } 3074 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU); 3075 3076 return err; 3077 } 3078 3079 static inline int snd_pcm_hwsync(struct snd_pcm_substream *substream) 3080 { 3081 return snd_pcm_delay(substream, NULL); 3082 } 3083 3084 #define snd_pcm_sync_ptr_get_user(__f, __c, __ptr) ({ \ 3085 __label__ failed, failed_begin; \ 3086 int __err = -EFAULT; \ 3087 typeof(*(__ptr)) __user *__src = (__ptr); \ 3088 \ 3089 if (!user_read_access_begin(__src, sizeof(*__src))) \ 3090 goto failed_begin; \ 3091 unsafe_get_user(__f, &__src->flags, failed); \ 3092 unsafe_get_user(__c.appl_ptr, &__src->c.control.appl_ptr, failed); \ 3093 unsafe_get_user(__c.avail_min, &__src->c.control.avail_min, failed); \ 3094 __err = 0; \ 3095 failed: \ 3096 user_read_access_end(); \ 3097 failed_begin: \ 3098 __err; \ 3099 }) 3100 3101 #define snd_pcm_sync_ptr_put_user(__s, __c, __ptr) ({ \ 3102 __label__ failed, failed_begin; \ 3103 int __err = -EFAULT; \ 3104 typeof(*(__ptr)) __user *__src = (__ptr); \ 3105 \ 3106 if (!user_write_access_begin(__src, sizeof(*__src))) \ 3107 goto failed_begin; \ 3108 unsafe_put_user(__s.state, &__src->s.status.state, failed); \ 3109 unsafe_put_user(__s.hw_ptr, &__src->s.status.hw_ptr, failed); \ 3110 unsafe_put_user(__s.tstamp.tv_sec, &__src->s.status.tstamp.tv_sec, failed); \ 3111 unsafe_put_user(__s.tstamp.tv_nsec, &__src->s.status.tstamp.tv_nsec, failed); \ 3112 unsafe_put_user(__s.suspended_state, &__src->s.status.suspended_state, failed); \ 3113 unsafe_put_user(__s.audio_tstamp.tv_sec, &__src->s.status.audio_tstamp.tv_sec, failed); \ 3114 unsafe_put_user(__s.audio_tstamp.tv_nsec, &__src->s.status.audio_tstamp.tv_nsec, failed);\ 3115 unsafe_put_user(__c.appl_ptr, &__src->c.control.appl_ptr, failed); \ 3116 unsafe_put_user(__c.avail_min, &__src->c.control.avail_min, failed); \ 3117 __err = 0; \ 3118 failed: \ 3119 user_write_access_end(); \ 3120 failed_begin: \ 3121 __err; \ 3122 }) 3123 3124 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream, 3125 struct snd_pcm_sync_ptr __user *_sync_ptr) 3126 { 3127 struct snd_pcm_runtime *runtime = substream->runtime; 3128 volatile struct snd_pcm_mmap_status *status; 3129 volatile struct snd_pcm_mmap_control *control; 3130 u32 sflags; 3131 struct snd_pcm_mmap_control scontrol; 3132 struct snd_pcm_mmap_status sstatus; 3133 int err; 3134 3135 if (snd_pcm_sync_ptr_get_user(sflags, scontrol, _sync_ptr)) 3136 return -EFAULT; 3137 status = runtime->status; 3138 control = runtime->control; 3139 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) { 3140 err = snd_pcm_hwsync(substream); 3141 if (err < 0) 3142 return err; 3143 } 3144 scoped_guard(pcm_stream_lock_irq, substream) { 3145 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) { 3146 err = pcm_lib_apply_appl_ptr(substream, scontrol.appl_ptr); 3147 if (err < 0) 3148 return err; 3149 } else { 3150 scontrol.appl_ptr = control->appl_ptr; 3151 } 3152 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) 3153 control->avail_min = scontrol.avail_min; 3154 else 3155 scontrol.avail_min = control->avail_min; 3156 sstatus.state = status->state; 3157 sstatus.hw_ptr = status->hw_ptr; 3158 sstatus.tstamp = status->tstamp; 3159 sstatus.suspended_state = status->suspended_state; 3160 sstatus.audio_tstamp = status->audio_tstamp; 3161 } 3162 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) 3163 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 3164 if (snd_pcm_sync_ptr_put_user(sstatus, scontrol, _sync_ptr)) 3165 return -EFAULT; 3166 return 0; 3167 } 3168 3169 struct snd_pcm_mmap_status32 { 3170 snd_pcm_state_t state; 3171 s32 pad1; 3172 u32 hw_ptr; 3173 struct __snd_timespec tstamp; 3174 snd_pcm_state_t suspended_state; 3175 struct __snd_timespec audio_tstamp; 3176 } __packed; 3177 3178 struct snd_pcm_mmap_control32 { 3179 u32 appl_ptr; 3180 u32 avail_min; 3181 }; 3182 3183 struct snd_pcm_sync_ptr32 { 3184 u32 flags; 3185 union { 3186 struct snd_pcm_mmap_status32 status; 3187 unsigned char reserved[64]; 3188 } s; 3189 union { 3190 struct snd_pcm_mmap_control32 control; 3191 unsigned char reserved[64]; 3192 } c; 3193 } __packed; 3194 3195 /* recalculate the boundary within 32bit */ 3196 static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime) 3197 { 3198 snd_pcm_uframes_t boundary; 3199 snd_pcm_uframes_t border; 3200 int order; 3201 3202 if (! runtime->buffer_size) 3203 return 0; 3204 3205 border = 0x7fffffffUL - runtime->buffer_size; 3206 if (runtime->buffer_size > border) 3207 return runtime->buffer_size; 3208 3209 order = __fls(border) - __fls(runtime->buffer_size); 3210 boundary = runtime->buffer_size << order; 3211 3212 if (boundary <= border) 3213 return boundary; 3214 else 3215 return boundary / 2; 3216 } 3217 3218 static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream, 3219 struct snd_pcm_sync_ptr32 __user *src) 3220 { 3221 struct snd_pcm_runtime *runtime = substream->runtime; 3222 volatile struct snd_pcm_mmap_status *status; 3223 volatile struct snd_pcm_mmap_control *control; 3224 u32 sflags; 3225 struct snd_pcm_mmap_control scontrol; 3226 struct snd_pcm_mmap_status sstatus; 3227 snd_pcm_uframes_t boundary; 3228 int err; 3229 3230 if (snd_BUG_ON(!runtime)) 3231 return -EINVAL; 3232 3233 if (snd_pcm_sync_ptr_get_user(sflags, scontrol, src)) 3234 return -EFAULT; 3235 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) { 3236 err = snd_pcm_hwsync(substream); 3237 if (err < 0) 3238 return err; 3239 } 3240 status = runtime->status; 3241 control = runtime->control; 3242 boundary = recalculate_boundary(runtime); 3243 if (! boundary) 3244 boundary = 0x7fffffff; 3245 scoped_guard(pcm_stream_lock_irq, substream) { 3246 /* FIXME: we should consider the boundary for the sync from app */ 3247 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) { 3248 err = pcm_lib_apply_appl_ptr(substream, 3249 scontrol.appl_ptr); 3250 if (err < 0) 3251 return err; 3252 } else 3253 scontrol.appl_ptr = control->appl_ptr % boundary; 3254 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) 3255 control->avail_min = scontrol.avail_min; 3256 else 3257 scontrol.avail_min = control->avail_min; 3258 sstatus.state = status->state; 3259 sstatus.hw_ptr = status->hw_ptr % boundary; 3260 sstatus.tstamp = status->tstamp; 3261 sstatus.suspended_state = status->suspended_state; 3262 sstatus.audio_tstamp = status->audio_tstamp; 3263 } 3264 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) 3265 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 3266 if (snd_pcm_sync_ptr_put_user(sstatus, scontrol, src)) 3267 return -EFAULT; 3268 3269 return 0; 3270 } 3271 #define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32) 3272 3273 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg) 3274 { 3275 struct snd_pcm_runtime *runtime = substream->runtime; 3276 int arg; 3277 3278 if (get_user(arg, _arg)) 3279 return -EFAULT; 3280 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST) 3281 return -EINVAL; 3282 runtime->tstamp_type = arg; 3283 return 0; 3284 } 3285 3286 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream, 3287 struct snd_xferi __user *_xferi) 3288 { 3289 struct snd_xferi xferi; 3290 struct snd_pcm_runtime *runtime = substream->runtime; 3291 snd_pcm_sframes_t result; 3292 3293 if (runtime->state == SNDRV_PCM_STATE_OPEN) 3294 return -EBADFD; 3295 if (put_user(0, &_xferi->result)) 3296 return -EFAULT; 3297 if (copy_from_user(&xferi, _xferi, sizeof(xferi))) 3298 return -EFAULT; 3299 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3300 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames); 3301 else 3302 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames); 3303 if (put_user(result, &_xferi->result)) 3304 return -EFAULT; 3305 return result < 0 ? result : 0; 3306 } 3307 3308 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream, 3309 struct snd_xfern __user *_xfern) 3310 { 3311 struct snd_xfern xfern; 3312 struct snd_pcm_runtime *runtime = substream->runtime; 3313 void *bufs __free(kfree) = NULL; 3314 snd_pcm_sframes_t result; 3315 3316 if (runtime->state == SNDRV_PCM_STATE_OPEN) 3317 return -EBADFD; 3318 if (runtime->channels > 128) 3319 return -EINVAL; 3320 if (put_user(0, &_xfern->result)) 3321 return -EFAULT; 3322 if (copy_from_user(&xfern, _xfern, sizeof(xfern))) 3323 return -EFAULT; 3324 3325 bufs = memdup_array_user(xfern.bufs, runtime->channels, sizeof(void *)); 3326 if (IS_ERR(bufs)) 3327 return PTR_ERR(bufs); 3328 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3329 result = snd_pcm_lib_writev(substream, bufs, xfern.frames); 3330 else 3331 result = snd_pcm_lib_readv(substream, bufs, xfern.frames); 3332 if (put_user(result, &_xfern->result)) 3333 return -EFAULT; 3334 return result < 0 ? result : 0; 3335 } 3336 3337 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream, 3338 snd_pcm_uframes_t __user *_frames) 3339 { 3340 snd_pcm_uframes_t frames; 3341 snd_pcm_sframes_t result; 3342 3343 if (get_user(frames, _frames)) 3344 return -EFAULT; 3345 if (put_user(0, _frames)) 3346 return -EFAULT; 3347 result = snd_pcm_rewind(substream, frames); 3348 if (put_user(result, _frames)) 3349 return -EFAULT; 3350 return result < 0 ? result : 0; 3351 } 3352 3353 static int snd_pcm_forward_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_forward(substream, frames); 3364 if (put_user(result, _frames)) 3365 return -EFAULT; 3366 return result < 0 ? result : 0; 3367 } 3368 3369 static int snd_pcm_common_ioctl(struct file *file, 3370 struct snd_pcm_substream *substream, 3371 unsigned int cmd, void __user *arg) 3372 { 3373 struct snd_pcm_file *pcm_file = file->private_data; 3374 int res; 3375 3376 if (PCM_RUNTIME_CHECK(substream)) 3377 return -ENXIO; 3378 3379 if (substream->runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 3380 return -EBADFD; 3381 3382 res = snd_power_wait(substream->pcm->card); 3383 if (res < 0) 3384 return res; 3385 3386 switch (cmd) { 3387 case SNDRV_PCM_IOCTL_PVERSION: 3388 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0; 3389 case SNDRV_PCM_IOCTL_INFO: 3390 return snd_pcm_info_user(substream, arg); 3391 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */ 3392 return 0; 3393 case SNDRV_PCM_IOCTL_TTSTAMP: 3394 return snd_pcm_tstamp(substream, arg); 3395 case SNDRV_PCM_IOCTL_USER_PVERSION: 3396 if (get_user(pcm_file->user_pversion, 3397 (unsigned int __user *)arg)) 3398 return -EFAULT; 3399 return 0; 3400 case SNDRV_PCM_IOCTL_HW_REFINE: 3401 return snd_pcm_hw_refine_user(substream, arg); 3402 case SNDRV_PCM_IOCTL_HW_PARAMS: 3403 return snd_pcm_hw_params_user(substream, arg); 3404 case SNDRV_PCM_IOCTL_HW_FREE: 3405 return snd_pcm_hw_free(substream); 3406 case SNDRV_PCM_IOCTL_SW_PARAMS: 3407 return snd_pcm_sw_params_user(substream, arg); 3408 case SNDRV_PCM_IOCTL_STATUS32: 3409 return snd_pcm_status_user32(substream, arg, false); 3410 case SNDRV_PCM_IOCTL_STATUS_EXT32: 3411 return snd_pcm_status_user32(substream, arg, true); 3412 case SNDRV_PCM_IOCTL_STATUS64: 3413 return snd_pcm_status_user64(substream, arg, false); 3414 case SNDRV_PCM_IOCTL_STATUS_EXT64: 3415 return snd_pcm_status_user64(substream, arg, true); 3416 case SNDRV_PCM_IOCTL_CHANNEL_INFO: 3417 return snd_pcm_channel_info_user(substream, arg); 3418 case SNDRV_PCM_IOCTL_PREPARE: 3419 return snd_pcm_prepare(substream, file); 3420 case SNDRV_PCM_IOCTL_RESET: 3421 return snd_pcm_reset(substream); 3422 case SNDRV_PCM_IOCTL_START: 3423 return snd_pcm_start_lock_irq(substream); 3424 case SNDRV_PCM_IOCTL_LINK: 3425 return snd_pcm_link(substream, (int)(unsigned long) arg); 3426 case SNDRV_PCM_IOCTL_UNLINK: 3427 return snd_pcm_unlink(substream); 3428 case SNDRV_PCM_IOCTL_RESUME: 3429 return snd_pcm_resume(substream); 3430 case SNDRV_PCM_IOCTL_XRUN: 3431 return snd_pcm_xrun(substream); 3432 case SNDRV_PCM_IOCTL_HWSYNC: 3433 return snd_pcm_hwsync(substream); 3434 case SNDRV_PCM_IOCTL_DELAY: 3435 { 3436 snd_pcm_sframes_t delay = 0; 3437 snd_pcm_sframes_t __user *res = arg; 3438 int err; 3439 3440 err = snd_pcm_delay(substream, &delay); 3441 if (err) 3442 return err; 3443 if (put_user(delay, res)) 3444 return -EFAULT; 3445 return 0; 3446 } 3447 case __SNDRV_PCM_IOCTL_SYNC_PTR32: 3448 return snd_pcm_ioctl_sync_ptr_compat(substream, arg); 3449 case __SNDRV_PCM_IOCTL_SYNC_PTR64: 3450 return snd_pcm_sync_ptr(substream, arg); 3451 #ifdef CONFIG_SND_SUPPORT_OLD_API 3452 case SNDRV_PCM_IOCTL_HW_REFINE_OLD: 3453 return snd_pcm_hw_refine_old_user(substream, arg); 3454 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD: 3455 return snd_pcm_hw_params_old_user(substream, arg); 3456 #endif 3457 case SNDRV_PCM_IOCTL_DRAIN: 3458 return snd_pcm_drain(substream, file); 3459 case SNDRV_PCM_IOCTL_DROP: 3460 return snd_pcm_drop(substream); 3461 case SNDRV_PCM_IOCTL_PAUSE: 3462 return snd_pcm_pause_lock_irq(substream, (unsigned long)arg); 3463 case SNDRV_PCM_IOCTL_WRITEI_FRAMES: 3464 case SNDRV_PCM_IOCTL_READI_FRAMES: 3465 return snd_pcm_xferi_frames_ioctl(substream, arg); 3466 case SNDRV_PCM_IOCTL_WRITEN_FRAMES: 3467 case SNDRV_PCM_IOCTL_READN_FRAMES: 3468 return snd_pcm_xfern_frames_ioctl(substream, arg); 3469 case SNDRV_PCM_IOCTL_REWIND: 3470 return snd_pcm_rewind_ioctl(substream, arg); 3471 case SNDRV_PCM_IOCTL_FORWARD: 3472 return snd_pcm_forward_ioctl(substream, arg); 3473 } 3474 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd); 3475 return -ENOTTY; 3476 } 3477 3478 static long snd_pcm_ioctl(struct file *file, unsigned int cmd, 3479 unsigned long arg) 3480 { 3481 struct snd_pcm_file *pcm_file; 3482 3483 pcm_file = file->private_data; 3484 3485 if (((cmd >> 8) & 0xff) != 'A') 3486 return -ENOTTY; 3487 3488 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd, 3489 (void __user *)arg); 3490 } 3491 3492 /** 3493 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space 3494 * @substream: PCM substream 3495 * @cmd: IOCTL cmd 3496 * @arg: IOCTL argument 3497 * 3498 * The function is provided primarily for OSS layer and USB gadget drivers, 3499 * and it allows only the limited set of ioctls (hw_params, sw_params, 3500 * prepare, start, drain, drop, forward). 3501 * 3502 * Return: zero if successful, or a negative error code 3503 */ 3504 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, 3505 unsigned int cmd, void *arg) 3506 { 3507 snd_pcm_uframes_t *frames = arg; 3508 snd_pcm_sframes_t result; 3509 3510 if (substream->runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 3511 return -EBADFD; 3512 3513 switch (cmd) { 3514 case SNDRV_PCM_IOCTL_FORWARD: 3515 { 3516 /* provided only for OSS; capture-only and no value returned */ 3517 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE) 3518 return -EINVAL; 3519 result = snd_pcm_forward(substream, *frames); 3520 return result < 0 ? result : 0; 3521 } 3522 case SNDRV_PCM_IOCTL_HW_PARAMS: 3523 return snd_pcm_hw_params(substream, arg); 3524 case SNDRV_PCM_IOCTL_SW_PARAMS: 3525 return snd_pcm_sw_params(substream, arg); 3526 case SNDRV_PCM_IOCTL_PREPARE: 3527 return snd_pcm_prepare(substream, NULL); 3528 case SNDRV_PCM_IOCTL_START: 3529 return snd_pcm_start_lock_irq(substream); 3530 case SNDRV_PCM_IOCTL_DRAIN: 3531 return snd_pcm_drain(substream, NULL); 3532 case SNDRV_PCM_IOCTL_DROP: 3533 return snd_pcm_drop(substream); 3534 case SNDRV_PCM_IOCTL_DELAY: 3535 return snd_pcm_delay(substream, frames); 3536 default: 3537 return -EINVAL; 3538 } 3539 } 3540 EXPORT_SYMBOL(snd_pcm_kernel_ioctl); 3541 3542 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, 3543 loff_t * offset) 3544 { 3545 struct snd_pcm_file *pcm_file; 3546 struct snd_pcm_substream *substream; 3547 struct snd_pcm_runtime *runtime; 3548 snd_pcm_sframes_t result; 3549 3550 pcm_file = file->private_data; 3551 substream = pcm_file->substream; 3552 if (PCM_RUNTIME_CHECK(substream)) 3553 return -ENXIO; 3554 runtime = substream->runtime; 3555 if (runtime->state == SNDRV_PCM_STATE_OPEN || 3556 runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 3557 return -EBADFD; 3558 if (!frame_aligned(runtime, count)) 3559 return -EINVAL; 3560 count = bytes_to_frames(runtime, count); 3561 result = snd_pcm_lib_read(substream, buf, count); 3562 if (result > 0) 3563 result = frames_to_bytes(runtime, result); 3564 return result; 3565 } 3566 3567 static ssize_t snd_pcm_write(struct file *file, const char __user *buf, 3568 size_t count, loff_t * offset) 3569 { 3570 struct snd_pcm_file *pcm_file; 3571 struct snd_pcm_substream *substream; 3572 struct snd_pcm_runtime *runtime; 3573 snd_pcm_sframes_t result; 3574 3575 pcm_file = file->private_data; 3576 substream = pcm_file->substream; 3577 if (PCM_RUNTIME_CHECK(substream)) 3578 return -ENXIO; 3579 runtime = substream->runtime; 3580 if (runtime->state == SNDRV_PCM_STATE_OPEN || 3581 runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 3582 return -EBADFD; 3583 if (!frame_aligned(runtime, count)) 3584 return -EINVAL; 3585 count = bytes_to_frames(runtime, count); 3586 result = snd_pcm_lib_write(substream, buf, count); 3587 if (result > 0) 3588 result = frames_to_bytes(runtime, result); 3589 return result; 3590 } 3591 3592 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to) 3593 { 3594 struct snd_pcm_file *pcm_file; 3595 struct snd_pcm_substream *substream; 3596 struct snd_pcm_runtime *runtime; 3597 snd_pcm_sframes_t result; 3598 unsigned long i; 3599 snd_pcm_uframes_t frames; 3600 const struct iovec *iov = iter_iov(to); 3601 3602 pcm_file = iocb->ki_filp->private_data; 3603 substream = pcm_file->substream; 3604 if (PCM_RUNTIME_CHECK(substream)) 3605 return -ENXIO; 3606 runtime = substream->runtime; 3607 if (runtime->state == SNDRV_PCM_STATE_OPEN || 3608 runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 3609 return -EBADFD; 3610 if (!user_backed_iter(to)) 3611 return -EINVAL; 3612 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels) 3613 return -EINVAL; 3614 if (!frame_aligned(runtime, iov->iov_len)) 3615 return -EINVAL; 3616 frames = bytes_to_samples(runtime, iov->iov_len); 3617 3618 void __user **bufs __free(kfree) = 3619 kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL); 3620 if (bufs == NULL) 3621 return -ENOMEM; 3622 for (i = 0; i < to->nr_segs; ++i) { 3623 bufs[i] = iov->iov_base; 3624 iov++; 3625 } 3626 result = snd_pcm_lib_readv(substream, bufs, frames); 3627 if (result > 0) 3628 result = frames_to_bytes(runtime, result); 3629 return result; 3630 } 3631 3632 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from) 3633 { 3634 struct snd_pcm_file *pcm_file; 3635 struct snd_pcm_substream *substream; 3636 struct snd_pcm_runtime *runtime; 3637 snd_pcm_sframes_t result; 3638 unsigned long i; 3639 snd_pcm_uframes_t frames; 3640 const struct iovec *iov = iter_iov(from); 3641 3642 pcm_file = iocb->ki_filp->private_data; 3643 substream = pcm_file->substream; 3644 if (PCM_RUNTIME_CHECK(substream)) 3645 return -ENXIO; 3646 runtime = substream->runtime; 3647 if (runtime->state == SNDRV_PCM_STATE_OPEN || 3648 runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 3649 return -EBADFD; 3650 if (!user_backed_iter(from)) 3651 return -EINVAL; 3652 if (from->nr_segs > 128 || from->nr_segs != runtime->channels || 3653 !frame_aligned(runtime, iov->iov_len)) 3654 return -EINVAL; 3655 frames = bytes_to_samples(runtime, iov->iov_len); 3656 3657 void __user **bufs __free(kfree) = 3658 kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL); 3659 if (bufs == NULL) 3660 return -ENOMEM; 3661 for (i = 0; i < from->nr_segs; ++i) { 3662 bufs[i] = iov->iov_base; 3663 iov++; 3664 } 3665 result = snd_pcm_lib_writev(substream, bufs, frames); 3666 if (result > 0) 3667 result = frames_to_bytes(runtime, result); 3668 return result; 3669 } 3670 3671 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait) 3672 { 3673 struct snd_pcm_file *pcm_file; 3674 struct snd_pcm_substream *substream; 3675 struct snd_pcm_runtime *runtime; 3676 __poll_t mask, ok; 3677 snd_pcm_uframes_t avail; 3678 3679 pcm_file = file->private_data; 3680 3681 substream = pcm_file->substream; 3682 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3683 ok = EPOLLOUT | EPOLLWRNORM; 3684 else 3685 ok = EPOLLIN | EPOLLRDNORM; 3686 if (PCM_RUNTIME_CHECK(substream)) 3687 return ok | EPOLLERR; 3688 3689 runtime = substream->runtime; 3690 if (runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 3691 return ok | EPOLLERR; 3692 3693 poll_wait(file, &runtime->sleep, wait); 3694 3695 mask = 0; 3696 guard(pcm_stream_lock_irq)(substream); 3697 avail = snd_pcm_avail(substream); 3698 switch (runtime->state) { 3699 case SNDRV_PCM_STATE_RUNNING: 3700 case SNDRV_PCM_STATE_PREPARED: 3701 case SNDRV_PCM_STATE_PAUSED: 3702 if (avail >= runtime->control->avail_min) 3703 mask = ok; 3704 break; 3705 case SNDRV_PCM_STATE_DRAINING: 3706 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 3707 mask = ok; 3708 if (!avail) 3709 mask |= EPOLLERR; 3710 } 3711 break; 3712 default: 3713 mask = ok | EPOLLERR; 3714 break; 3715 } 3716 return mask; 3717 } 3718 3719 /* 3720 * mmap support 3721 */ 3722 3723 /* 3724 * Only on coherent architectures, we can mmap the status and the control records 3725 * for effcient data transfer. On others, we have to use HWSYNC ioctl... 3726 */ 3727 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA) 3728 /* 3729 * mmap status record 3730 */ 3731 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf) 3732 { 3733 struct snd_pcm_substream *substream = vmf->vma->vm_private_data; 3734 struct snd_pcm_runtime *runtime; 3735 3736 if (substream == NULL) 3737 return VM_FAULT_SIGBUS; 3738 runtime = substream->runtime; 3739 vmf->page = virt_to_page(runtime->status); 3740 get_page(vmf->page); 3741 return 0; 3742 } 3743 3744 static const struct vm_operations_struct snd_pcm_vm_ops_status = 3745 { 3746 .fault = snd_pcm_mmap_status_fault, 3747 }; 3748 3749 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, 3750 struct vm_area_struct *area) 3751 { 3752 long size; 3753 if (!(area->vm_flags & VM_READ)) 3754 return -EINVAL; 3755 size = area->vm_end - area->vm_start; 3756 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))) 3757 return -EINVAL; 3758 area->vm_ops = &snd_pcm_vm_ops_status; 3759 area->vm_private_data = substream; 3760 vm_flags_mod(area, VM_DONTEXPAND | VM_DONTDUMP, 3761 VM_WRITE | VM_MAYWRITE); 3762 3763 return 0; 3764 } 3765 3766 /* 3767 * mmap control record 3768 */ 3769 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf) 3770 { 3771 struct snd_pcm_substream *substream = vmf->vma->vm_private_data; 3772 struct snd_pcm_runtime *runtime; 3773 3774 if (substream == NULL) 3775 return VM_FAULT_SIGBUS; 3776 runtime = substream->runtime; 3777 vmf->page = virt_to_page(runtime->control); 3778 get_page(vmf->page); 3779 return 0; 3780 } 3781 3782 static const struct vm_operations_struct snd_pcm_vm_ops_control = 3783 { 3784 .fault = snd_pcm_mmap_control_fault, 3785 }; 3786 3787 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, 3788 struct vm_area_struct *area) 3789 { 3790 long size; 3791 if (!(area->vm_flags & VM_READ)) 3792 return -EINVAL; 3793 size = area->vm_end - area->vm_start; 3794 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control))) 3795 return -EINVAL; 3796 area->vm_ops = &snd_pcm_vm_ops_control; 3797 area->vm_private_data = substream; 3798 vm_flags_set(area, VM_DONTEXPAND | VM_DONTDUMP); 3799 return 0; 3800 } 3801 3802 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file) 3803 { 3804 /* If drivers require the explicit sync (typically for non-coherent 3805 * pages), we have to disable the mmap of status and control data 3806 * to enforce the control via SYNC_PTR ioctl. 3807 */ 3808 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC) 3809 return false; 3810 /* See pcm_control_mmap_allowed() below. 3811 * Since older alsa-lib requires both status and control mmaps to be 3812 * coupled, we have to disable the status mmap for old alsa-lib, too. 3813 */ 3814 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) && 3815 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)) 3816 return false; 3817 return true; 3818 } 3819 3820 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file) 3821 { 3822 if (pcm_file->no_compat_mmap) 3823 return false; 3824 /* see above */ 3825 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC) 3826 return false; 3827 /* Disallow the control mmap when SYNC_APPLPTR flag is set; 3828 * it enforces the user-space to fall back to snd_pcm_sync_ptr(), 3829 * thus it effectively assures the manual update of appl_ptr. 3830 */ 3831 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR) 3832 return false; 3833 return true; 3834 } 3835 3836 #else /* ! coherent mmap */ 3837 /* 3838 * don't support mmap for status and control records. 3839 */ 3840 #define pcm_status_mmap_allowed(pcm_file) false 3841 #define pcm_control_mmap_allowed(pcm_file) false 3842 3843 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, 3844 struct vm_area_struct *area) 3845 { 3846 return -ENXIO; 3847 } 3848 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, 3849 struct vm_area_struct *area) 3850 { 3851 return -ENXIO; 3852 } 3853 #endif /* coherent mmap */ 3854 3855 /* 3856 * snd_pcm_mmap_data_open - increase the mmap counter 3857 */ 3858 static void snd_pcm_mmap_data_open(struct vm_area_struct *area) 3859 { 3860 struct snd_pcm_substream *substream = area->vm_private_data; 3861 3862 atomic_inc(&substream->mmap_count); 3863 } 3864 3865 /* 3866 * snd_pcm_mmap_data_close - decrease the mmap counter 3867 */ 3868 static void snd_pcm_mmap_data_close(struct vm_area_struct *area) 3869 { 3870 struct snd_pcm_substream *substream = area->vm_private_data; 3871 3872 atomic_dec(&substream->mmap_count); 3873 } 3874 3875 /* 3876 * fault callback for mmapping a RAM page 3877 */ 3878 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf) 3879 { 3880 struct snd_pcm_substream *substream = vmf->vma->vm_private_data; 3881 struct snd_pcm_runtime *runtime; 3882 unsigned long offset; 3883 struct page * page; 3884 size_t dma_bytes; 3885 3886 if (substream == NULL) 3887 return VM_FAULT_SIGBUS; 3888 runtime = substream->runtime; 3889 offset = vmf->pgoff << PAGE_SHIFT; 3890 dma_bytes = PAGE_ALIGN(runtime->dma_bytes); 3891 if (offset > dma_bytes - PAGE_SIZE) 3892 return VM_FAULT_SIGBUS; 3893 if (substream->ops->page) 3894 page = substream->ops->page(substream, offset); 3895 else if (!snd_pcm_get_dma_buf(substream)) { 3896 if (WARN_ON_ONCE(!runtime->dma_area)) 3897 return VM_FAULT_SIGBUS; 3898 page = virt_to_page(runtime->dma_area + offset); 3899 } else 3900 page = snd_sgbuf_get_page(snd_pcm_get_dma_buf(substream), offset); 3901 if (!page) 3902 return VM_FAULT_SIGBUS; 3903 get_page(page); 3904 vmf->page = page; 3905 return 0; 3906 } 3907 3908 static const struct vm_operations_struct snd_pcm_vm_ops_data = { 3909 .open = snd_pcm_mmap_data_open, 3910 .close = snd_pcm_mmap_data_close, 3911 }; 3912 3913 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = { 3914 .open = snd_pcm_mmap_data_open, 3915 .close = snd_pcm_mmap_data_close, 3916 .fault = snd_pcm_mmap_data_fault, 3917 }; 3918 3919 /* 3920 * mmap the DMA buffer on RAM 3921 */ 3922 3923 /** 3924 * snd_pcm_lib_default_mmap - Default PCM data mmap function 3925 * @substream: PCM substream 3926 * @area: VMA 3927 * 3928 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL, 3929 * this function is invoked implicitly. 3930 * 3931 * Return: zero if successful, or a negative error code 3932 */ 3933 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream, 3934 struct vm_area_struct *area) 3935 { 3936 vm_flags_set(area, VM_DONTEXPAND | VM_DONTDUMP); 3937 if (!substream->ops->page && 3938 !snd_dma_buffer_mmap(snd_pcm_get_dma_buf(substream), area)) 3939 return 0; 3940 /* mmap with fault handler */ 3941 area->vm_ops = &snd_pcm_vm_ops_data_fault; 3942 return 0; 3943 } 3944 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap); 3945 3946 /* 3947 * mmap the DMA buffer on I/O memory area 3948 */ 3949 #if SNDRV_PCM_INFO_MMAP_IOMEM 3950 /** 3951 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem 3952 * @substream: PCM substream 3953 * @area: VMA 3954 * 3955 * When your hardware uses the iomapped pages as the hardware buffer and 3956 * wants to mmap it, pass this function as mmap pcm_ops. Note that this 3957 * is supposed to work only on limited architectures. 3958 * 3959 * Return: zero if successful, or a negative error code 3960 */ 3961 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, 3962 struct vm_area_struct *area) 3963 { 3964 struct snd_pcm_runtime *runtime = substream->runtime; 3965 3966 area->vm_page_prot = pgprot_noncached(area->vm_page_prot); 3967 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes); 3968 } 3969 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem); 3970 #endif /* SNDRV_PCM_INFO_MMAP */ 3971 3972 /* 3973 * mmap DMA buffer 3974 */ 3975 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, 3976 struct vm_area_struct *area) 3977 { 3978 struct snd_pcm_runtime *runtime; 3979 long size; 3980 unsigned long offset; 3981 size_t dma_bytes; 3982 int err; 3983 3984 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 3985 if (!(area->vm_flags & (VM_WRITE|VM_READ))) 3986 return -EINVAL; 3987 } else { 3988 if (!(area->vm_flags & VM_READ)) 3989 return -EINVAL; 3990 } 3991 runtime = substream->runtime; 3992 if (runtime->state == SNDRV_PCM_STATE_OPEN) 3993 return -EBADFD; 3994 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) 3995 return -ENXIO; 3996 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || 3997 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) 3998 return -EINVAL; 3999 size = area->vm_end - area->vm_start; 4000 offset = area->vm_pgoff << PAGE_SHIFT; 4001 dma_bytes = PAGE_ALIGN(runtime->dma_bytes); 4002 if ((size_t)size > dma_bytes) 4003 return -EINVAL; 4004 if (offset > dma_bytes - size) 4005 return -EINVAL; 4006 4007 area->vm_ops = &snd_pcm_vm_ops_data; 4008 area->vm_private_data = substream; 4009 if (substream->ops->mmap) 4010 err = substream->ops->mmap(substream, area); 4011 else 4012 err = snd_pcm_lib_default_mmap(substream, area); 4013 if (!err) 4014 atomic_inc(&substream->mmap_count); 4015 return err; 4016 } 4017 EXPORT_SYMBOL(snd_pcm_mmap_data); 4018 4019 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area) 4020 { 4021 struct snd_pcm_file * pcm_file; 4022 struct snd_pcm_substream *substream; 4023 unsigned long offset; 4024 4025 pcm_file = file->private_data; 4026 substream = pcm_file->substream; 4027 if (PCM_RUNTIME_CHECK(substream)) 4028 return -ENXIO; 4029 if (substream->runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 4030 return -EBADFD; 4031 4032 offset = area->vm_pgoff << PAGE_SHIFT; 4033 switch (offset) { 4034 case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD: 4035 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT)) 4036 return -ENXIO; 4037 fallthrough; 4038 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW: 4039 if (!pcm_status_mmap_allowed(pcm_file)) 4040 return -ENXIO; 4041 return snd_pcm_mmap_status(substream, file, area); 4042 case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD: 4043 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT)) 4044 return -ENXIO; 4045 fallthrough; 4046 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW: 4047 if (!pcm_control_mmap_allowed(pcm_file)) 4048 return -ENXIO; 4049 return snd_pcm_mmap_control(substream, file, area); 4050 default: 4051 return snd_pcm_mmap_data(substream, file, area); 4052 } 4053 return 0; 4054 } 4055 4056 static int snd_pcm_fasync(int fd, struct file * file, int on) 4057 { 4058 struct snd_pcm_file * pcm_file; 4059 struct snd_pcm_substream *substream; 4060 struct snd_pcm_runtime *runtime; 4061 4062 pcm_file = file->private_data; 4063 substream = pcm_file->substream; 4064 if (PCM_RUNTIME_CHECK(substream)) 4065 return -ENXIO; 4066 runtime = substream->runtime; 4067 if (runtime->state == SNDRV_PCM_STATE_DISCONNECTED) 4068 return -EBADFD; 4069 return snd_fasync_helper(fd, file, on, &runtime->fasync); 4070 } 4071 4072 /* 4073 * ioctl32 compat 4074 */ 4075 #ifdef CONFIG_COMPAT 4076 #include "pcm_compat.c" 4077 #else 4078 #define snd_pcm_ioctl_compat NULL 4079 #endif 4080 4081 /* 4082 * To be removed helpers to keep binary compatibility 4083 */ 4084 4085 #ifdef CONFIG_SND_SUPPORT_OLD_API 4086 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5)) 4087 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5)) 4088 4089 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params, 4090 struct snd_pcm_hw_params_old *oparams) 4091 { 4092 unsigned int i; 4093 4094 memset(params, 0, sizeof(*params)); 4095 params->flags = oparams->flags; 4096 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) 4097 params->masks[i].bits[0] = oparams->masks[i]; 4098 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals)); 4099 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask); 4100 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask); 4101 params->info = oparams->info; 4102 params->msbits = oparams->msbits; 4103 params->rate_num = oparams->rate_num; 4104 params->rate_den = oparams->rate_den; 4105 params->fifo_size = oparams->fifo_size; 4106 } 4107 4108 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams, 4109 struct snd_pcm_hw_params *params) 4110 { 4111 unsigned int i; 4112 4113 memset(oparams, 0, sizeof(*oparams)); 4114 oparams->flags = params->flags; 4115 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) 4116 oparams->masks[i] = params->masks[i].bits[0]; 4117 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals)); 4118 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask); 4119 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask); 4120 oparams->info = params->info; 4121 oparams->msbits = params->msbits; 4122 oparams->rate_num = params->rate_num; 4123 oparams->rate_den = params->rate_den; 4124 oparams->fifo_size = params->fifo_size; 4125 } 4126 4127 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, 4128 struct snd_pcm_hw_params_old __user * _oparams) 4129 { 4130 int err; 4131 4132 struct snd_pcm_hw_params *params __free(kfree) = 4133 kmalloc_obj(*params); 4134 if (!params) 4135 return -ENOMEM; 4136 4137 struct snd_pcm_hw_params_old *oparams __free(kfree) = 4138 memdup_user(_oparams, sizeof(*oparams)); 4139 if (IS_ERR(oparams)) 4140 return PTR_ERR(oparams); 4141 snd_pcm_hw_convert_from_old_params(params, oparams); 4142 err = snd_pcm_hw_refine(substream, params); 4143 if (err < 0) 4144 return err; 4145 4146 err = fixup_unreferenced_params(substream, params); 4147 if (err < 0) 4148 return err; 4149 4150 snd_pcm_hw_convert_to_old_params(oparams, params); 4151 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) 4152 return -EFAULT; 4153 return 0; 4154 } 4155 4156 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, 4157 struct snd_pcm_hw_params_old __user * _oparams) 4158 { 4159 int err; 4160 4161 struct snd_pcm_hw_params *params __free(kfree) = 4162 kmalloc_obj(*params); 4163 if (!params) 4164 return -ENOMEM; 4165 4166 struct snd_pcm_hw_params_old *oparams __free(kfree) = 4167 memdup_user(_oparams, sizeof(*oparams)); 4168 if (IS_ERR(oparams)) 4169 return PTR_ERR(oparams); 4170 4171 snd_pcm_hw_convert_from_old_params(params, oparams); 4172 err = snd_pcm_hw_params(substream, params); 4173 if (err < 0) 4174 return err; 4175 4176 snd_pcm_hw_convert_to_old_params(oparams, params); 4177 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) 4178 return -EFAULT; 4179 return 0; 4180 } 4181 #endif /* CONFIG_SND_SUPPORT_OLD_API */ 4182 4183 #ifndef CONFIG_MMU 4184 static unsigned long snd_pcm_get_unmapped_area(struct file *file, 4185 unsigned long addr, 4186 unsigned long len, 4187 unsigned long pgoff, 4188 unsigned long flags) 4189 { 4190 struct snd_pcm_file *pcm_file = file->private_data; 4191 struct snd_pcm_substream *substream = pcm_file->substream; 4192 struct snd_pcm_runtime *runtime = substream->runtime; 4193 unsigned long offset = pgoff << PAGE_SHIFT; 4194 4195 switch (offset) { 4196 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW: 4197 return (unsigned long)runtime->status; 4198 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW: 4199 return (unsigned long)runtime->control; 4200 default: 4201 return (unsigned long)runtime->dma_area + offset; 4202 } 4203 } 4204 #else 4205 # define snd_pcm_get_unmapped_area NULL 4206 #endif 4207 4208 /* 4209 * Register section 4210 */ 4211 4212 const struct file_operations snd_pcm_f_ops[2] = { 4213 { 4214 .owner = THIS_MODULE, 4215 .write = snd_pcm_write, 4216 .write_iter = snd_pcm_writev, 4217 .open = snd_pcm_playback_open, 4218 .release = snd_pcm_release, 4219 .poll = snd_pcm_poll, 4220 .unlocked_ioctl = snd_pcm_ioctl, 4221 .compat_ioctl = snd_pcm_ioctl_compat, 4222 .mmap = snd_pcm_mmap, 4223 .fasync = snd_pcm_fasync, 4224 .get_unmapped_area = snd_pcm_get_unmapped_area, 4225 }, 4226 { 4227 .owner = THIS_MODULE, 4228 .read = snd_pcm_read, 4229 .read_iter = snd_pcm_readv, 4230 .open = snd_pcm_capture_open, 4231 .release = snd_pcm_release, 4232 .poll = snd_pcm_poll, 4233 .unlocked_ioctl = snd_pcm_ioctl, 4234 .compat_ioctl = snd_pcm_ioctl_compat, 4235 .mmap = snd_pcm_mmap, 4236 .fasync = snd_pcm_fasync, 4237 .get_unmapped_area = snd_pcm_get_unmapped_area, 4238 } 4239 }; 4240