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