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