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