1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Digital Audio (PCM) abstract layer 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 5 */ 6 7 #include <linux/init.h> 8 #include <linux/slab.h> 9 #include <linux/module.h> 10 #include <linux/time.h> 11 #include <linux/mutex.h> 12 #include <linux/device.h> 13 #include <linux/nospec.h> 14 #include <sound/core.h> 15 #include <sound/minors.h> 16 #include <sound/pcm.h> 17 #include <sound/timer.h> 18 #include <sound/control.h> 19 #include <sound/info.h> 20 21 #include "pcm_local.h" 22 23 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>"); 24 MODULE_DESCRIPTION("Midlevel PCM code for ALSA."); 25 MODULE_LICENSE("GPL"); 26 27 static LIST_HEAD(snd_pcm_devices); 28 static DEFINE_MUTEX(register_mutex); 29 #if IS_ENABLED(CONFIG_SND_PCM_OSS) 30 static LIST_HEAD(snd_pcm_notify_list); 31 #endif 32 33 static int snd_pcm_free(struct snd_pcm *pcm); 34 static int snd_pcm_dev_free(struct snd_device *device); 35 static int snd_pcm_dev_register(struct snd_device *device); 36 static int snd_pcm_dev_disconnect(struct snd_device *device); 37 38 static struct snd_pcm *snd_pcm_get(struct snd_card *card, int device) 39 { 40 struct snd_pcm *pcm; 41 42 list_for_each_entry(pcm, &snd_pcm_devices, list) { 43 if (pcm->card == card && pcm->device == device) 44 return pcm; 45 } 46 return NULL; 47 } 48 49 static int snd_pcm_next(struct snd_card *card, int device) 50 { 51 struct snd_pcm *pcm; 52 53 list_for_each_entry(pcm, &snd_pcm_devices, list) { 54 if (pcm->card == card && pcm->device > device) 55 return pcm->device; 56 else if (pcm->card->number > card->number) 57 return -1; 58 } 59 return -1; 60 } 61 62 static int snd_pcm_add(struct snd_pcm *newpcm) 63 { 64 struct snd_pcm *pcm; 65 66 if (newpcm->internal) 67 return 0; 68 69 list_for_each_entry(pcm, &snd_pcm_devices, list) { 70 if (pcm->card == newpcm->card && pcm->device == newpcm->device) 71 return -EBUSY; 72 if (pcm->card->number > newpcm->card->number || 73 (pcm->card == newpcm->card && 74 pcm->device > newpcm->device)) { 75 list_add(&newpcm->list, pcm->list.prev); 76 return 0; 77 } 78 } 79 list_add_tail(&newpcm->list, &snd_pcm_devices); 80 return 0; 81 } 82 83 static int snd_pcm_control_ioctl(struct snd_card *card, 84 struct snd_ctl_file *control, 85 unsigned int cmd, unsigned long arg) 86 { 87 switch (cmd) { 88 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE: 89 { 90 int device; 91 92 if (get_user(device, (int __user *)arg)) 93 return -EFAULT; 94 mutex_lock(®ister_mutex); 95 device = snd_pcm_next(card, device); 96 mutex_unlock(®ister_mutex); 97 if (put_user(device, (int __user *)arg)) 98 return -EFAULT; 99 return 0; 100 } 101 case SNDRV_CTL_IOCTL_PCM_INFO: 102 { 103 struct snd_pcm_info __user *info; 104 unsigned int device, subdevice; 105 int stream; 106 struct snd_pcm *pcm; 107 struct snd_pcm_str *pstr; 108 struct snd_pcm_substream *substream; 109 int err; 110 111 info = (struct snd_pcm_info __user *)arg; 112 if (get_user(device, &info->device)) 113 return -EFAULT; 114 if (get_user(stream, &info->stream)) 115 return -EFAULT; 116 if (stream < 0 || stream > 1) 117 return -EINVAL; 118 stream = array_index_nospec(stream, 2); 119 if (get_user(subdevice, &info->subdevice)) 120 return -EFAULT; 121 mutex_lock(®ister_mutex); 122 pcm = snd_pcm_get(card, device); 123 if (pcm == NULL) { 124 err = -ENXIO; 125 goto _error; 126 } 127 pstr = &pcm->streams[stream]; 128 if (pstr->substream_count == 0) { 129 err = -ENOENT; 130 goto _error; 131 } 132 if (subdevice >= pstr->substream_count) { 133 err = -ENXIO; 134 goto _error; 135 } 136 for (substream = pstr->substream; substream; 137 substream = substream->next) 138 if (substream->number == (int)subdevice) 139 break; 140 if (substream == NULL) { 141 err = -ENXIO; 142 goto _error; 143 } 144 mutex_lock(&pcm->open_mutex); 145 err = snd_pcm_info_user(substream, info); 146 mutex_unlock(&pcm->open_mutex); 147 _error: 148 mutex_unlock(®ister_mutex); 149 return err; 150 } 151 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE: 152 { 153 int val; 154 155 if (get_user(val, (int __user *)arg)) 156 return -EFAULT; 157 control->preferred_subdevice[SND_CTL_SUBDEV_PCM] = val; 158 return 0; 159 } 160 } 161 return -ENOIOCTLCMD; 162 } 163 164 #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v 165 166 static const char * const snd_pcm_format_names[] = { 167 FORMAT(S8), 168 FORMAT(U8), 169 FORMAT(S16_LE), 170 FORMAT(S16_BE), 171 FORMAT(U16_LE), 172 FORMAT(U16_BE), 173 FORMAT(S24_LE), 174 FORMAT(S24_BE), 175 FORMAT(U24_LE), 176 FORMAT(U24_BE), 177 FORMAT(S32_LE), 178 FORMAT(S32_BE), 179 FORMAT(U32_LE), 180 FORMAT(U32_BE), 181 FORMAT(FLOAT_LE), 182 FORMAT(FLOAT_BE), 183 FORMAT(FLOAT64_LE), 184 FORMAT(FLOAT64_BE), 185 FORMAT(IEC958_SUBFRAME_LE), 186 FORMAT(IEC958_SUBFRAME_BE), 187 FORMAT(MU_LAW), 188 FORMAT(A_LAW), 189 FORMAT(IMA_ADPCM), 190 FORMAT(MPEG), 191 FORMAT(GSM), 192 FORMAT(SPECIAL), 193 FORMAT(S24_3LE), 194 FORMAT(S24_3BE), 195 FORMAT(U24_3LE), 196 FORMAT(U24_3BE), 197 FORMAT(S20_3LE), 198 FORMAT(S20_3BE), 199 FORMAT(U20_3LE), 200 FORMAT(U20_3BE), 201 FORMAT(S18_3LE), 202 FORMAT(S18_3BE), 203 FORMAT(U18_3LE), 204 FORMAT(U18_3BE), 205 FORMAT(G723_24), 206 FORMAT(G723_24_1B), 207 FORMAT(G723_40), 208 FORMAT(G723_40_1B), 209 FORMAT(DSD_U8), 210 FORMAT(DSD_U16_LE), 211 FORMAT(DSD_U32_LE), 212 FORMAT(DSD_U16_BE), 213 FORMAT(DSD_U32_BE), 214 FORMAT(S20_LE), 215 FORMAT(S20_BE), 216 FORMAT(U20_LE), 217 FORMAT(U20_BE), 218 }; 219 220 /** 221 * snd_pcm_format_name - Return a name string for the given PCM format 222 * @format: PCM format 223 * 224 * Return: the format name string 225 */ 226 const char *snd_pcm_format_name(snd_pcm_format_t format) 227 { 228 unsigned int format_num = (__force unsigned int)format; 229 230 if (format_num >= ARRAY_SIZE(snd_pcm_format_names) || !snd_pcm_format_names[format_num]) 231 return "Unknown"; 232 return snd_pcm_format_names[format_num]; 233 } 234 EXPORT_SYMBOL_GPL(snd_pcm_format_name); 235 236 #ifdef CONFIG_SND_VERBOSE_PROCFS 237 238 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v 239 #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v 240 #define READY(v) [SNDRV_PCM_READY_##v] = #v 241 #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v 242 #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v 243 #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v 244 #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v 245 #define START(v) [SNDRV_PCM_START_##v] = #v 246 #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v 247 248 static const char * const snd_pcm_stream_names[] = { 249 STREAM(PLAYBACK), 250 STREAM(CAPTURE), 251 }; 252 253 static const char * const snd_pcm_state_names[] = { 254 STATE(OPEN), 255 STATE(SETUP), 256 STATE(PREPARED), 257 STATE(RUNNING), 258 STATE(XRUN), 259 STATE(DRAINING), 260 STATE(PAUSED), 261 STATE(SUSPENDED), 262 STATE(DISCONNECTED), 263 }; 264 265 static const char * const snd_pcm_access_names[] = { 266 ACCESS(MMAP_INTERLEAVED), 267 ACCESS(MMAP_NONINTERLEAVED), 268 ACCESS(MMAP_COMPLEX), 269 ACCESS(RW_INTERLEAVED), 270 ACCESS(RW_NONINTERLEAVED), 271 }; 272 273 static const char * const snd_pcm_subformat_names[] = { 274 SUBFORMAT(STD), 275 SUBFORMAT(MSBITS_MAX), 276 SUBFORMAT(MSBITS_20), 277 SUBFORMAT(MSBITS_24), 278 }; 279 280 static const char * const snd_pcm_tstamp_mode_names[] = { 281 TSTAMP(NONE), 282 TSTAMP(ENABLE), 283 }; 284 285 static const char *snd_pcm_stream_name(int stream) 286 { 287 return snd_pcm_stream_names[stream]; 288 } 289 290 static const char *snd_pcm_access_name(snd_pcm_access_t access) 291 { 292 return snd_pcm_access_names[(__force int)access]; 293 } 294 295 static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat) 296 { 297 return snd_pcm_subformat_names[(__force int)subformat]; 298 } 299 300 static const char *snd_pcm_tstamp_mode_name(int mode) 301 { 302 return snd_pcm_tstamp_mode_names[mode]; 303 } 304 305 static const char *snd_pcm_state_name(snd_pcm_state_t state) 306 { 307 return snd_pcm_state_names[(__force int)state]; 308 } 309 310 #if IS_ENABLED(CONFIG_SND_PCM_OSS) 311 #include <linux/soundcard.h> 312 313 static const char *snd_pcm_oss_format_name(int format) 314 { 315 switch (format) { 316 case AFMT_MU_LAW: 317 return "MU_LAW"; 318 case AFMT_A_LAW: 319 return "A_LAW"; 320 case AFMT_IMA_ADPCM: 321 return "IMA_ADPCM"; 322 case AFMT_U8: 323 return "U8"; 324 case AFMT_S16_LE: 325 return "S16_LE"; 326 case AFMT_S16_BE: 327 return "S16_BE"; 328 case AFMT_S8: 329 return "S8"; 330 case AFMT_U16_LE: 331 return "U16_LE"; 332 case AFMT_U16_BE: 333 return "U16_BE"; 334 case AFMT_MPEG: 335 return "MPEG"; 336 default: 337 return "unknown"; 338 } 339 } 340 #endif 341 342 static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream, 343 struct snd_info_buffer *buffer) 344 { 345 struct snd_pcm_info *info; 346 int err; 347 348 if (! substream) 349 return; 350 351 info = kmalloc(sizeof(*info), GFP_KERNEL); 352 if (!info) 353 return; 354 355 err = snd_pcm_info(substream, info); 356 if (err < 0) { 357 snd_iprintf(buffer, "error %d\n", err); 358 kfree(info); 359 return; 360 } 361 snd_iprintf(buffer, "card: %d\n", info->card); 362 snd_iprintf(buffer, "device: %d\n", info->device); 363 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice); 364 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream)); 365 snd_iprintf(buffer, "id: %s\n", info->id); 366 snd_iprintf(buffer, "name: %s\n", info->name); 367 snd_iprintf(buffer, "subname: %s\n", info->subname); 368 snd_iprintf(buffer, "class: %d\n", info->dev_class); 369 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass); 370 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count); 371 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail); 372 kfree(info); 373 } 374 375 static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry, 376 struct snd_info_buffer *buffer) 377 { 378 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream, 379 buffer); 380 } 381 382 static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry, 383 struct snd_info_buffer *buffer) 384 { 385 snd_pcm_proc_info_read(entry->private_data, buffer); 386 } 387 388 static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry, 389 struct snd_info_buffer *buffer) 390 { 391 struct snd_pcm_substream *substream = entry->private_data; 392 struct snd_pcm_runtime *runtime; 393 394 mutex_lock(&substream->pcm->open_mutex); 395 runtime = substream->runtime; 396 if (!runtime) { 397 snd_iprintf(buffer, "closed\n"); 398 goto unlock; 399 } 400 if (runtime->state == SNDRV_PCM_STATE_OPEN) { 401 snd_iprintf(buffer, "no setup\n"); 402 goto unlock; 403 } 404 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access)); 405 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format)); 406 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat)); 407 snd_iprintf(buffer, "channels: %u\n", runtime->channels); 408 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den); 409 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size); 410 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size); 411 #if IS_ENABLED(CONFIG_SND_PCM_OSS) 412 if (substream->oss.oss) { 413 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format)); 414 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels); 415 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate); 416 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes); 417 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods); 418 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames); 419 } 420 #endif 421 unlock: 422 mutex_unlock(&substream->pcm->open_mutex); 423 } 424 425 static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry, 426 struct snd_info_buffer *buffer) 427 { 428 struct snd_pcm_substream *substream = entry->private_data; 429 struct snd_pcm_runtime *runtime; 430 431 mutex_lock(&substream->pcm->open_mutex); 432 runtime = substream->runtime; 433 if (!runtime) { 434 snd_iprintf(buffer, "closed\n"); 435 goto unlock; 436 } 437 if (runtime->state == SNDRV_PCM_STATE_OPEN) { 438 snd_iprintf(buffer, "no setup\n"); 439 goto unlock; 440 } 441 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode)); 442 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step); 443 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min); 444 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold); 445 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold); 446 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold); 447 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size); 448 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary); 449 unlock: 450 mutex_unlock(&substream->pcm->open_mutex); 451 } 452 453 static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry, 454 struct snd_info_buffer *buffer) 455 { 456 struct snd_pcm_substream *substream = entry->private_data; 457 struct snd_pcm_runtime *runtime; 458 struct snd_pcm_status64 status; 459 int err; 460 461 mutex_lock(&substream->pcm->open_mutex); 462 runtime = substream->runtime; 463 if (!runtime) { 464 snd_iprintf(buffer, "closed\n"); 465 goto unlock; 466 } 467 memset(&status, 0, sizeof(status)); 468 err = snd_pcm_status64(substream, &status); 469 if (err < 0) { 470 snd_iprintf(buffer, "error %d\n", err); 471 goto unlock; 472 } 473 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state)); 474 snd_iprintf(buffer, "owner_pid : %d\n", pid_vnr(substream->pid)); 475 snd_iprintf(buffer, "trigger_time: %lld.%09lld\n", 476 status.trigger_tstamp_sec, status.trigger_tstamp_nsec); 477 snd_iprintf(buffer, "tstamp : %lld.%09lld\n", 478 status.tstamp_sec, status.tstamp_nsec); 479 snd_iprintf(buffer, "delay : %ld\n", status.delay); 480 snd_iprintf(buffer, "avail : %ld\n", status.avail); 481 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max); 482 snd_iprintf(buffer, "-----\n"); 483 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr); 484 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr); 485 unlock: 486 mutex_unlock(&substream->pcm->open_mutex); 487 } 488 489 #ifdef CONFIG_SND_PCM_XRUN_DEBUG 490 static void snd_pcm_xrun_injection_write(struct snd_info_entry *entry, 491 struct snd_info_buffer *buffer) 492 { 493 struct snd_pcm_substream *substream = entry->private_data; 494 495 snd_pcm_stop_xrun(substream); 496 } 497 498 static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry, 499 struct snd_info_buffer *buffer) 500 { 501 struct snd_pcm_str *pstr = entry->private_data; 502 snd_iprintf(buffer, "%d\n", pstr->xrun_debug); 503 } 504 505 static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry, 506 struct snd_info_buffer *buffer) 507 { 508 struct snd_pcm_str *pstr = entry->private_data; 509 char line[64]; 510 if (!snd_info_get_line(buffer, line, sizeof(line))) 511 pstr->xrun_debug = simple_strtoul(line, NULL, 10); 512 } 513 #endif 514 515 static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) 516 { 517 struct snd_pcm *pcm = pstr->pcm; 518 struct snd_info_entry *entry; 519 char name[16]; 520 521 sprintf(name, "pcm%i%c", pcm->device, 522 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c'); 523 entry = snd_info_create_card_entry(pcm->card, name, 524 pcm->card->proc_root); 525 if (!entry) 526 return -ENOMEM; 527 entry->mode = S_IFDIR | 0555; 528 pstr->proc_root = entry; 529 entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root); 530 if (entry) 531 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read); 532 #ifdef CONFIG_SND_PCM_XRUN_DEBUG 533 entry = snd_info_create_card_entry(pcm->card, "xrun_debug", 534 pstr->proc_root); 535 if (entry) { 536 snd_info_set_text_ops(entry, pstr, snd_pcm_xrun_debug_read); 537 entry->c.text.write = snd_pcm_xrun_debug_write; 538 entry->mode |= 0200; 539 } 540 #endif 541 return 0; 542 } 543 544 static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) 545 { 546 snd_info_free_entry(pstr->proc_root); 547 pstr->proc_root = NULL; 548 return 0; 549 } 550 551 static struct snd_info_entry * 552 create_substream_info_entry(struct snd_pcm_substream *substream, 553 const char *name, 554 void (*read)(struct snd_info_entry *, 555 struct snd_info_buffer *)) 556 { 557 struct snd_info_entry *entry; 558 559 entry = snd_info_create_card_entry(substream->pcm->card, name, 560 substream->proc_root); 561 if (entry) 562 snd_info_set_text_ops(entry, substream, read); 563 return entry; 564 } 565 566 static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) 567 { 568 struct snd_info_entry *entry; 569 struct snd_card *card; 570 char name[16]; 571 572 card = substream->pcm->card; 573 574 sprintf(name, "sub%i", substream->number); 575 entry = snd_info_create_card_entry(card, name, 576 substream->pstr->proc_root); 577 if (!entry) 578 return -ENOMEM; 579 entry->mode = S_IFDIR | 0555; 580 substream->proc_root = entry; 581 582 create_substream_info_entry(substream, "info", 583 snd_pcm_substream_proc_info_read); 584 create_substream_info_entry(substream, "hw_params", 585 snd_pcm_substream_proc_hw_params_read); 586 create_substream_info_entry(substream, "sw_params", 587 snd_pcm_substream_proc_sw_params_read); 588 create_substream_info_entry(substream, "status", 589 snd_pcm_substream_proc_status_read); 590 591 #ifdef CONFIG_SND_PCM_XRUN_DEBUG 592 entry = create_substream_info_entry(substream, "xrun_injection", NULL); 593 if (entry) { 594 entry->c.text.write = snd_pcm_xrun_injection_write; 595 entry->mode = S_IFREG | 0200; 596 } 597 #endif /* CONFIG_SND_PCM_XRUN_DEBUG */ 598 599 return 0; 600 } 601 602 #else /* !CONFIG_SND_VERBOSE_PROCFS */ 603 static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; } 604 static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; } 605 static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; } 606 #endif /* CONFIG_SND_VERBOSE_PROCFS */ 607 608 static const struct attribute_group *pcm_dev_attr_groups[]; 609 610 /* 611 * PM callbacks: we need to deal only with suspend here, as the resume is 612 * triggered either from user-space or the driver's resume callback 613 */ 614 #ifdef CONFIG_PM_SLEEP 615 static int do_pcm_suspend(struct device *dev) 616 { 617 struct snd_pcm_str *pstr = dev_get_drvdata(dev); 618 619 if (!pstr->pcm->no_device_suspend) 620 snd_pcm_suspend_all(pstr->pcm); 621 return 0; 622 } 623 #endif 624 625 static const struct dev_pm_ops pcm_dev_pm_ops = { 626 SET_SYSTEM_SLEEP_PM_OPS(do_pcm_suspend, NULL) 627 }; 628 629 /* device type for PCM -- basically only for passing PM callbacks */ 630 static const struct device_type pcm_dev_type = { 631 .name = "pcm", 632 .pm = &pcm_dev_pm_ops, 633 }; 634 635 /** 636 * snd_pcm_new_stream - create a new PCM stream 637 * @pcm: the pcm instance 638 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX 639 * @substream_count: the number of substreams 640 * 641 * Creates a new stream for the pcm. 642 * The corresponding stream on the pcm must have been empty before 643 * calling this, i.e. zero must be given to the argument of 644 * snd_pcm_new(). 645 * 646 * Return: Zero if successful, or a negative error code on failure. 647 */ 648 int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count) 649 { 650 int idx, err; 651 struct snd_pcm_str *pstr = &pcm->streams[stream]; 652 struct snd_pcm_substream *substream, *prev; 653 654 #if IS_ENABLED(CONFIG_SND_PCM_OSS) 655 mutex_init(&pstr->oss.setup_mutex); 656 #endif 657 pstr->stream = stream; 658 pstr->pcm = pcm; 659 pstr->substream_count = substream_count; 660 if (!substream_count) 661 return 0; 662 663 err = snd_device_alloc(&pstr->dev, pcm->card); 664 if (err < 0) 665 return err; 666 dev_set_name(pstr->dev, "pcmC%iD%i%c", pcm->card->number, pcm->device, 667 stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c'); 668 pstr->dev->groups = pcm_dev_attr_groups; 669 pstr->dev->type = &pcm_dev_type; 670 dev_set_drvdata(pstr->dev, pstr); 671 672 if (!pcm->internal) { 673 err = snd_pcm_stream_proc_init(pstr); 674 if (err < 0) { 675 pcm_err(pcm, "Error in snd_pcm_stream_proc_init\n"); 676 return err; 677 } 678 } 679 prev = NULL; 680 for (idx = 0, prev = NULL; idx < substream_count; idx++) { 681 substream = kzalloc(sizeof(*substream), GFP_KERNEL); 682 if (!substream) 683 return -ENOMEM; 684 substream->pcm = pcm; 685 substream->pstr = pstr; 686 substream->number = idx; 687 substream->stream = stream; 688 sprintf(substream->name, "subdevice #%i", idx); 689 substream->buffer_bytes_max = UINT_MAX; 690 if (prev == NULL) 691 pstr->substream = substream; 692 else 693 prev->next = substream; 694 695 if (!pcm->internal) { 696 err = snd_pcm_substream_proc_init(substream); 697 if (err < 0) { 698 pcm_err(pcm, 699 "Error in snd_pcm_stream_proc_init\n"); 700 if (prev == NULL) 701 pstr->substream = NULL; 702 else 703 prev->next = NULL; 704 kfree(substream); 705 return err; 706 } 707 } 708 substream->group = &substream->self_group; 709 snd_pcm_group_init(&substream->self_group); 710 list_add_tail(&substream->link_list, &substream->self_group.substreams); 711 atomic_set(&substream->mmap_count, 0); 712 prev = substream; 713 } 714 return 0; 715 } 716 EXPORT_SYMBOL(snd_pcm_new_stream); 717 718 static int _snd_pcm_new(struct snd_card *card, const char *id, int device, 719 int playback_count, int capture_count, bool internal, 720 struct snd_pcm **rpcm) 721 { 722 struct snd_pcm *pcm; 723 int err; 724 static const struct snd_device_ops ops = { 725 .dev_free = snd_pcm_dev_free, 726 .dev_register = snd_pcm_dev_register, 727 .dev_disconnect = snd_pcm_dev_disconnect, 728 }; 729 static const struct snd_device_ops internal_ops = { 730 .dev_free = snd_pcm_dev_free, 731 }; 732 733 if (snd_BUG_ON(!card)) 734 return -ENXIO; 735 if (rpcm) 736 *rpcm = NULL; 737 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL); 738 if (!pcm) 739 return -ENOMEM; 740 pcm->card = card; 741 pcm->device = device; 742 pcm->internal = internal; 743 mutex_init(&pcm->open_mutex); 744 init_waitqueue_head(&pcm->open_wait); 745 INIT_LIST_HEAD(&pcm->list); 746 if (id) 747 strscpy(pcm->id, id, sizeof(pcm->id)); 748 749 err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, 750 playback_count); 751 if (err < 0) 752 goto free_pcm; 753 754 err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count); 755 if (err < 0) 756 goto free_pcm; 757 758 err = snd_device_new(card, SNDRV_DEV_PCM, pcm, 759 internal ? &internal_ops : &ops); 760 if (err < 0) 761 goto free_pcm; 762 763 if (rpcm) 764 *rpcm = pcm; 765 return 0; 766 767 free_pcm: 768 snd_pcm_free(pcm); 769 return err; 770 } 771 772 /** 773 * snd_pcm_new - create a new PCM instance 774 * @card: the card instance 775 * @id: the id string 776 * @device: the device index (zero based) 777 * @playback_count: the number of substreams for playback 778 * @capture_count: the number of substreams for capture 779 * @rpcm: the pointer to store the new pcm instance 780 * 781 * Creates a new PCM instance. 782 * 783 * The pcm operators have to be set afterwards to the new instance 784 * via snd_pcm_set_ops(). 785 * 786 * Return: Zero if successful, or a negative error code on failure. 787 */ 788 int snd_pcm_new(struct snd_card *card, const char *id, int device, 789 int playback_count, int capture_count, struct snd_pcm **rpcm) 790 { 791 return _snd_pcm_new(card, id, device, playback_count, capture_count, 792 false, rpcm); 793 } 794 EXPORT_SYMBOL(snd_pcm_new); 795 796 /** 797 * snd_pcm_new_internal - create a new internal PCM instance 798 * @card: the card instance 799 * @id: the id string 800 * @device: the device index (zero based - shared with normal PCMs) 801 * @playback_count: the number of substreams for playback 802 * @capture_count: the number of substreams for capture 803 * @rpcm: the pointer to store the new pcm instance 804 * 805 * Creates a new internal PCM instance with no userspace device or procfs 806 * entries. This is used by ASoC Back End PCMs in order to create a PCM that 807 * will only be used internally by kernel drivers. i.e. it cannot be opened 808 * by userspace. It provides existing ASoC components drivers with a substream 809 * and access to any private data. 810 * 811 * The pcm operators have to be set afterwards to the new instance 812 * via snd_pcm_set_ops(). 813 * 814 * Return: Zero if successful, or a negative error code on failure. 815 */ 816 int snd_pcm_new_internal(struct snd_card *card, const char *id, int device, 817 int playback_count, int capture_count, 818 struct snd_pcm **rpcm) 819 { 820 return _snd_pcm_new(card, id, device, playback_count, capture_count, 821 true, rpcm); 822 } 823 EXPORT_SYMBOL(snd_pcm_new_internal); 824 825 static void free_chmap(struct snd_pcm_str *pstr) 826 { 827 if (pstr->chmap_kctl) { 828 struct snd_card *card = pstr->pcm->card; 829 830 snd_ctl_remove(card, pstr->chmap_kctl); 831 pstr->chmap_kctl = NULL; 832 } 833 } 834 835 static void snd_pcm_free_stream(struct snd_pcm_str * pstr) 836 { 837 struct snd_pcm_substream *substream, *substream_next; 838 #if IS_ENABLED(CONFIG_SND_PCM_OSS) 839 struct snd_pcm_oss_setup *setup, *setupn; 840 #endif 841 842 /* free all proc files under the stream */ 843 snd_pcm_stream_proc_done(pstr); 844 845 substream = pstr->substream; 846 while (substream) { 847 substream_next = substream->next; 848 snd_pcm_timer_done(substream); 849 kfree(substream); 850 substream = substream_next; 851 } 852 #if IS_ENABLED(CONFIG_SND_PCM_OSS) 853 for (setup = pstr->oss.setup_list; setup; setup = setupn) { 854 setupn = setup->next; 855 kfree(setup->task_name); 856 kfree(setup); 857 } 858 #endif 859 free_chmap(pstr); 860 if (pstr->substream_count) 861 put_device(pstr->dev); 862 } 863 864 #if IS_ENABLED(CONFIG_SND_PCM_OSS) 865 #define pcm_call_notify(pcm, call) \ 866 do { \ 867 struct snd_pcm_notify *_notify; \ 868 list_for_each_entry(_notify, &snd_pcm_notify_list, list) \ 869 _notify->call(pcm); \ 870 } while (0) 871 #else 872 #define pcm_call_notify(pcm, call) do {} while (0) 873 #endif 874 875 static int snd_pcm_free(struct snd_pcm *pcm) 876 { 877 if (!pcm) 878 return 0; 879 if (!pcm->internal) 880 pcm_call_notify(pcm, n_unregister); 881 if (pcm->private_free) 882 pcm->private_free(pcm); 883 snd_pcm_lib_preallocate_free_for_all(pcm); 884 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]); 885 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]); 886 kfree(pcm); 887 return 0; 888 } 889 890 static int snd_pcm_dev_free(struct snd_device *device) 891 { 892 struct snd_pcm *pcm = device->device_data; 893 return snd_pcm_free(pcm); 894 } 895 896 int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, 897 struct file *file, 898 struct snd_pcm_substream **rsubstream) 899 { 900 struct snd_pcm_str * pstr; 901 struct snd_pcm_substream *substream; 902 struct snd_pcm_runtime *runtime; 903 struct snd_card *card; 904 int prefer_subdevice; 905 size_t size; 906 907 if (snd_BUG_ON(!pcm || !rsubstream)) 908 return -ENXIO; 909 if (snd_BUG_ON(stream != SNDRV_PCM_STREAM_PLAYBACK && 910 stream != SNDRV_PCM_STREAM_CAPTURE)) 911 return -EINVAL; 912 *rsubstream = NULL; 913 pstr = &pcm->streams[stream]; 914 if (pstr->substream == NULL || pstr->substream_count == 0) 915 return -ENODEV; 916 917 card = pcm->card; 918 prefer_subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_PCM); 919 920 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) { 921 int opposite = !stream; 922 923 for (substream = pcm->streams[opposite].substream; substream; 924 substream = substream->next) { 925 if (SUBSTREAM_BUSY(substream)) 926 return -EAGAIN; 927 } 928 } 929 930 if (file->f_flags & O_APPEND) { 931 if (prefer_subdevice < 0) { 932 if (pstr->substream_count > 1) 933 return -EINVAL; /* must be unique */ 934 substream = pstr->substream; 935 } else { 936 for (substream = pstr->substream; substream; 937 substream = substream->next) 938 if (substream->number == prefer_subdevice) 939 break; 940 } 941 if (! substream) 942 return -ENODEV; 943 if (! SUBSTREAM_BUSY(substream)) 944 return -EBADFD; 945 substream->ref_count++; 946 *rsubstream = substream; 947 return 0; 948 } 949 950 for (substream = pstr->substream; substream; substream = substream->next) { 951 if (!SUBSTREAM_BUSY(substream) && 952 (prefer_subdevice == -1 || 953 substream->number == prefer_subdevice)) 954 break; 955 } 956 if (substream == NULL) 957 return -EAGAIN; 958 959 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); 960 if (runtime == NULL) 961 return -ENOMEM; 962 963 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)); 964 runtime->status = alloc_pages_exact(size, GFP_KERNEL); 965 if (runtime->status == NULL) { 966 kfree(runtime); 967 return -ENOMEM; 968 } 969 memset(runtime->status, 0, size); 970 971 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)); 972 runtime->control = alloc_pages_exact(size, GFP_KERNEL); 973 if (runtime->control == NULL) { 974 free_pages_exact(runtime->status, 975 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))); 976 kfree(runtime); 977 return -ENOMEM; 978 } 979 memset(runtime->control, 0, size); 980 981 init_waitqueue_head(&runtime->sleep); 982 init_waitqueue_head(&runtime->tsleep); 983 984 __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_OPEN); 985 mutex_init(&runtime->buffer_mutex); 986 atomic_set(&runtime->buffer_accessing, 0); 987 988 substream->runtime = runtime; 989 substream->private_data = pcm->private_data; 990 substream->ref_count = 1; 991 substream->f_flags = file->f_flags; 992 substream->pid = get_pid(task_pid(current)); 993 pstr->substream_opened++; 994 *rsubstream = substream; 995 return 0; 996 } 997 998 void snd_pcm_detach_substream(struct snd_pcm_substream *substream) 999 { 1000 struct snd_pcm_runtime *runtime; 1001 1002 if (PCM_RUNTIME_CHECK(substream)) 1003 return; 1004 runtime = substream->runtime; 1005 if (runtime->private_free != NULL) 1006 runtime->private_free(runtime); 1007 free_pages_exact(runtime->status, 1008 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))); 1009 free_pages_exact(runtime->control, 1010 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control))); 1011 kfree(runtime->hw_constraints.rules); 1012 /* Avoid concurrent access to runtime via PCM timer interface */ 1013 if (substream->timer) { 1014 spin_lock_irq(&substream->timer->lock); 1015 substream->runtime = NULL; 1016 spin_unlock_irq(&substream->timer->lock); 1017 } else { 1018 substream->runtime = NULL; 1019 } 1020 mutex_destroy(&runtime->buffer_mutex); 1021 snd_fasync_free(runtime->fasync); 1022 kfree(runtime); 1023 put_pid(substream->pid); 1024 substream->pid = NULL; 1025 substream->pstr->substream_opened--; 1026 } 1027 1028 static ssize_t pcm_class_show(struct device *dev, 1029 struct device_attribute *attr, char *buf) 1030 { 1031 struct snd_pcm_str *pstr = dev_get_drvdata(dev); 1032 struct snd_pcm *pcm = pstr->pcm; 1033 const char *str; 1034 static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = { 1035 [SNDRV_PCM_CLASS_GENERIC] = "generic", 1036 [SNDRV_PCM_CLASS_MULTI] = "multi", 1037 [SNDRV_PCM_CLASS_MODEM] = "modem", 1038 [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer", 1039 }; 1040 1041 if (pcm->dev_class > SNDRV_PCM_CLASS_LAST) 1042 str = "none"; 1043 else 1044 str = strs[pcm->dev_class]; 1045 return sysfs_emit(buf, "%s\n", str); 1046 } 1047 1048 static DEVICE_ATTR_RO(pcm_class); 1049 static struct attribute *pcm_dev_attrs[] = { 1050 &dev_attr_pcm_class.attr, 1051 NULL 1052 }; 1053 1054 static const struct attribute_group pcm_dev_attr_group = { 1055 .attrs = pcm_dev_attrs, 1056 }; 1057 1058 static const struct attribute_group *pcm_dev_attr_groups[] = { 1059 &pcm_dev_attr_group, 1060 NULL 1061 }; 1062 1063 static int snd_pcm_dev_register(struct snd_device *device) 1064 { 1065 int cidx, err; 1066 struct snd_pcm_substream *substream; 1067 struct snd_pcm *pcm; 1068 1069 if (snd_BUG_ON(!device || !device->device_data)) 1070 return -ENXIO; 1071 pcm = device->device_data; 1072 1073 mutex_lock(®ister_mutex); 1074 err = snd_pcm_add(pcm); 1075 if (err) 1076 goto unlock; 1077 for (cidx = 0; cidx < 2; cidx++) { 1078 int devtype = -1; 1079 if (pcm->streams[cidx].substream == NULL) 1080 continue; 1081 switch (cidx) { 1082 case SNDRV_PCM_STREAM_PLAYBACK: 1083 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK; 1084 break; 1085 case SNDRV_PCM_STREAM_CAPTURE: 1086 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE; 1087 break; 1088 } 1089 /* register pcm */ 1090 err = snd_register_device(devtype, pcm->card, pcm->device, 1091 &snd_pcm_f_ops[cidx], pcm, 1092 pcm->streams[cidx].dev); 1093 if (err < 0) { 1094 list_del_init(&pcm->list); 1095 goto unlock; 1096 } 1097 1098 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) 1099 snd_pcm_timer_init(substream); 1100 } 1101 1102 pcm_call_notify(pcm, n_register); 1103 1104 unlock: 1105 mutex_unlock(®ister_mutex); 1106 return err; 1107 } 1108 1109 static int snd_pcm_dev_disconnect(struct snd_device *device) 1110 { 1111 struct snd_pcm *pcm = device->device_data; 1112 struct snd_pcm_substream *substream; 1113 int cidx; 1114 1115 mutex_lock(®ister_mutex); 1116 mutex_lock(&pcm->open_mutex); 1117 wake_up(&pcm->open_wait); 1118 list_del_init(&pcm->list); 1119 1120 for_each_pcm_substream(pcm, cidx, substream) { 1121 snd_pcm_stream_lock_irq(substream); 1122 if (substream->runtime) { 1123 if (snd_pcm_running(substream)) 1124 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED); 1125 /* to be sure, set the state unconditionally */ 1126 __snd_pcm_set_state(substream->runtime, 1127 SNDRV_PCM_STATE_DISCONNECTED); 1128 wake_up(&substream->runtime->sleep); 1129 wake_up(&substream->runtime->tsleep); 1130 } 1131 snd_pcm_stream_unlock_irq(substream); 1132 } 1133 1134 for_each_pcm_substream(pcm, cidx, substream) 1135 snd_pcm_sync_stop(substream, false); 1136 1137 pcm_call_notify(pcm, n_disconnect); 1138 for (cidx = 0; cidx < 2; cidx++) { 1139 if (pcm->streams[cidx].dev) 1140 snd_unregister_device(pcm->streams[cidx].dev); 1141 free_chmap(&pcm->streams[cidx]); 1142 } 1143 mutex_unlock(&pcm->open_mutex); 1144 mutex_unlock(®ister_mutex); 1145 return 0; 1146 } 1147 1148 #if IS_ENABLED(CONFIG_SND_PCM_OSS) 1149 /** 1150 * snd_pcm_notify - Add/remove the notify list 1151 * @notify: PCM notify list 1152 * @nfree: 0 = register, 1 = unregister 1153 * 1154 * This adds the given notifier to the global list so that the callback is 1155 * called for each registered PCM devices. This exists only for PCM OSS 1156 * emulation, so far. 1157 * 1158 * Return: zero if successful, or a negative error code 1159 */ 1160 int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree) 1161 { 1162 struct snd_pcm *pcm; 1163 1164 if (snd_BUG_ON(!notify || 1165 !notify->n_register || 1166 !notify->n_unregister || 1167 !notify->n_disconnect)) 1168 return -EINVAL; 1169 mutex_lock(®ister_mutex); 1170 if (nfree) { 1171 list_del(¬ify->list); 1172 list_for_each_entry(pcm, &snd_pcm_devices, list) 1173 notify->n_unregister(pcm); 1174 } else { 1175 list_add_tail(¬ify->list, &snd_pcm_notify_list); 1176 list_for_each_entry(pcm, &snd_pcm_devices, list) 1177 notify->n_register(pcm); 1178 } 1179 mutex_unlock(®ister_mutex); 1180 return 0; 1181 } 1182 EXPORT_SYMBOL(snd_pcm_notify); 1183 #endif /* CONFIG_SND_PCM_OSS */ 1184 1185 #ifdef CONFIG_SND_PROC_FS 1186 /* 1187 * Info interface 1188 */ 1189 1190 static void snd_pcm_proc_read(struct snd_info_entry *entry, 1191 struct snd_info_buffer *buffer) 1192 { 1193 struct snd_pcm *pcm; 1194 1195 mutex_lock(®ister_mutex); 1196 list_for_each_entry(pcm, &snd_pcm_devices, list) { 1197 snd_iprintf(buffer, "%02i-%02i: %s : %s", 1198 pcm->card->number, pcm->device, pcm->id, pcm->name); 1199 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) 1200 snd_iprintf(buffer, " : playback %i", 1201 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count); 1202 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) 1203 snd_iprintf(buffer, " : capture %i", 1204 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count); 1205 snd_iprintf(buffer, "\n"); 1206 } 1207 mutex_unlock(®ister_mutex); 1208 } 1209 1210 static struct snd_info_entry *snd_pcm_proc_entry; 1211 1212 static void snd_pcm_proc_init(void) 1213 { 1214 struct snd_info_entry *entry; 1215 1216 entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL); 1217 if (entry) { 1218 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read); 1219 if (snd_info_register(entry) < 0) { 1220 snd_info_free_entry(entry); 1221 entry = NULL; 1222 } 1223 } 1224 snd_pcm_proc_entry = entry; 1225 } 1226 1227 static void snd_pcm_proc_done(void) 1228 { 1229 snd_info_free_entry(snd_pcm_proc_entry); 1230 } 1231 1232 #else /* !CONFIG_SND_PROC_FS */ 1233 #define snd_pcm_proc_init() 1234 #define snd_pcm_proc_done() 1235 #endif /* CONFIG_SND_PROC_FS */ 1236 1237 1238 /* 1239 * ENTRY functions 1240 */ 1241 1242 static int __init alsa_pcm_init(void) 1243 { 1244 snd_ctl_register_ioctl(snd_pcm_control_ioctl); 1245 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl); 1246 snd_pcm_proc_init(); 1247 return 0; 1248 } 1249 1250 static void __exit alsa_pcm_exit(void) 1251 { 1252 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl); 1253 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl); 1254 snd_pcm_proc_done(); 1255 } 1256 1257 module_init(alsa_pcm_init) 1258 module_exit(alsa_pcm_exit) 1259