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