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