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