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