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