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