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