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