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