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