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