1 /* 2 * Digital Audio (PCM) abstract layer 3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h> 23 #include <linux/init.h> 24 #include <linux/slab.h> 25 #include <linux/time.h> 26 #include <linux/mutex.h> 27 #include <sound/core.h> 28 #include <sound/minors.h> 29 #include <sound/pcm.h> 30 #include <sound/control.h> 31 #include <sound/info.h> 32 33 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>"); 34 MODULE_DESCRIPTION("Midlevel PCM code for ALSA."); 35 MODULE_LICENSE("GPL"); 36 37 static LIST_HEAD(snd_pcm_devices); 38 static LIST_HEAD(snd_pcm_notify_list); 39 static DEFINE_MUTEX(register_mutex); 40 41 static int snd_pcm_free(struct snd_pcm *pcm); 42 static int snd_pcm_dev_free(struct snd_device *device); 43 static int snd_pcm_dev_register(struct snd_device *device); 44 static int snd_pcm_dev_disconnect(struct snd_device *device); 45 static int snd_pcm_dev_unregister(struct snd_device *device); 46 47 static struct snd_pcm *snd_pcm_search(struct snd_card *card, int device) 48 { 49 struct list_head *p; 50 struct snd_pcm *pcm; 51 52 list_for_each(p, &snd_pcm_devices) { 53 pcm = list_entry(p, struct snd_pcm, list); 54 if (pcm->card == card && pcm->device == device) 55 return pcm; 56 } 57 return NULL; 58 } 59 60 static int snd_pcm_control_ioctl(struct snd_card *card, 61 struct snd_ctl_file *control, 62 unsigned int cmd, unsigned long arg) 63 { 64 switch (cmd) { 65 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE: 66 { 67 int device; 68 69 if (get_user(device, (int __user *)arg)) 70 return -EFAULT; 71 mutex_lock(®ister_mutex); 72 device = device < 0 ? 0 : device + 1; 73 while (device < SNDRV_PCM_DEVICES) { 74 if (snd_pcm_search(card, device)) 75 break; 76 device++; 77 } 78 if (device == SNDRV_PCM_DEVICES) 79 device = -1; 80 mutex_unlock(®ister_mutex); 81 if (put_user(device, (int __user *)arg)) 82 return -EFAULT; 83 return 0; 84 } 85 case SNDRV_CTL_IOCTL_PCM_INFO: 86 { 87 struct snd_pcm_info __user *info; 88 unsigned int device, subdevice; 89 int stream; 90 struct snd_pcm *pcm; 91 struct snd_pcm_str *pstr; 92 struct snd_pcm_substream *substream; 93 int err; 94 95 info = (struct snd_pcm_info __user *)arg; 96 if (get_user(device, &info->device)) 97 return -EFAULT; 98 if (get_user(stream, &info->stream)) 99 return -EFAULT; 100 if (stream < 0 || stream > 1) 101 return -EINVAL; 102 if (get_user(subdevice, &info->subdevice)) 103 return -EFAULT; 104 mutex_lock(®ister_mutex); 105 pcm = snd_pcm_search(card, device); 106 if (pcm == NULL) { 107 err = -ENXIO; 108 goto _error; 109 } 110 pstr = &pcm->streams[stream]; 111 if (pstr->substream_count == 0) { 112 err = -ENOENT; 113 goto _error; 114 } 115 if (subdevice >= pstr->substream_count) { 116 err = -ENXIO; 117 goto _error; 118 } 119 for (substream = pstr->substream; substream; 120 substream = substream->next) 121 if (substream->number == (int)subdevice) 122 break; 123 if (substream == NULL) { 124 err = -ENXIO; 125 goto _error; 126 } 127 err = snd_pcm_info_user(substream, info); 128 _error: 129 mutex_unlock(®ister_mutex); 130 return err; 131 } 132 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE: 133 { 134 int val; 135 136 if (get_user(val, (int __user *)arg)) 137 return -EFAULT; 138 control->prefer_pcm_subdevice = val; 139 return 0; 140 } 141 } 142 return -ENOIOCTLCMD; 143 } 144 145 #ifdef CONFIG_SND_VERBOSE_PROCFS 146 147 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v 148 #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v 149 #define READY(v) [SNDRV_PCM_READY_##v] = #v 150 #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v 151 #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v 152 #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v 153 #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v 154 #define START(v) [SNDRV_PCM_START_##v] = #v 155 #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v 156 #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v 157 158 static char *snd_pcm_format_names[] = { 159 FORMAT(S8), 160 FORMAT(U8), 161 FORMAT(S16_LE), 162 FORMAT(S16_BE), 163 FORMAT(U16_LE), 164 FORMAT(U16_BE), 165 FORMAT(S24_LE), 166 FORMAT(S24_BE), 167 FORMAT(U24_LE), 168 FORMAT(U24_BE), 169 FORMAT(S32_LE), 170 FORMAT(S32_BE), 171 FORMAT(U32_LE), 172 FORMAT(U32_BE), 173 FORMAT(FLOAT_LE), 174 FORMAT(FLOAT_BE), 175 FORMAT(FLOAT64_LE), 176 FORMAT(FLOAT64_BE), 177 FORMAT(IEC958_SUBFRAME_LE), 178 FORMAT(IEC958_SUBFRAME_BE), 179 FORMAT(MU_LAW), 180 FORMAT(A_LAW), 181 FORMAT(IMA_ADPCM), 182 FORMAT(MPEG), 183 FORMAT(GSM), 184 FORMAT(SPECIAL), 185 FORMAT(S24_3LE), 186 FORMAT(S24_3BE), 187 FORMAT(U24_3LE), 188 FORMAT(U24_3BE), 189 FORMAT(S20_3LE), 190 FORMAT(S20_3BE), 191 FORMAT(U20_3LE), 192 FORMAT(U20_3BE), 193 FORMAT(S18_3LE), 194 FORMAT(S18_3BE), 195 FORMAT(U18_3LE), 196 FORMAT(U18_3BE), 197 }; 198 199 static const char *snd_pcm_format_name(snd_pcm_format_t format) 200 { 201 return snd_pcm_format_names[format]; 202 } 203 204 static char *snd_pcm_stream_names[] = { 205 STREAM(PLAYBACK), 206 STREAM(CAPTURE), 207 }; 208 209 static char *snd_pcm_state_names[] = { 210 STATE(OPEN), 211 STATE(SETUP), 212 STATE(PREPARED), 213 STATE(RUNNING), 214 STATE(XRUN), 215 STATE(DRAINING), 216 STATE(PAUSED), 217 STATE(SUSPENDED), 218 }; 219 220 static char *snd_pcm_access_names[] = { 221 ACCESS(MMAP_INTERLEAVED), 222 ACCESS(MMAP_NONINTERLEAVED), 223 ACCESS(MMAP_COMPLEX), 224 ACCESS(RW_INTERLEAVED), 225 ACCESS(RW_NONINTERLEAVED), 226 }; 227 228 static char *snd_pcm_subformat_names[] = { 229 SUBFORMAT(STD), 230 }; 231 232 static char *snd_pcm_tstamp_mode_names[] = { 233 TSTAMP(NONE), 234 TSTAMP(MMAP), 235 }; 236 237 static const char *snd_pcm_stream_name(int stream) 238 { 239 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL); 240 return snd_pcm_stream_names[stream]; 241 } 242 243 static const char *snd_pcm_access_name(snd_pcm_access_t access) 244 { 245 return snd_pcm_access_names[access]; 246 } 247 248 static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat) 249 { 250 return snd_pcm_subformat_names[subformat]; 251 } 252 253 static const char *snd_pcm_tstamp_mode_name(int mode) 254 { 255 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL); 256 return snd_pcm_tstamp_mode_names[mode]; 257 } 258 259 static const char *snd_pcm_state_name(snd_pcm_state_t state) 260 { 261 return snd_pcm_state_names[state]; 262 } 263 264 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE) 265 #include <linux/soundcard.h> 266 267 static const char *snd_pcm_oss_format_name(int format) 268 { 269 switch (format) { 270 case AFMT_MU_LAW: 271 return "MU_LAW"; 272 case AFMT_A_LAW: 273 return "A_LAW"; 274 case AFMT_IMA_ADPCM: 275 return "IMA_ADPCM"; 276 case AFMT_U8: 277 return "U8"; 278 case AFMT_S16_LE: 279 return "S16_LE"; 280 case AFMT_S16_BE: 281 return "S16_BE"; 282 case AFMT_S8: 283 return "S8"; 284 case AFMT_U16_LE: 285 return "U16_LE"; 286 case AFMT_U16_BE: 287 return "U16_BE"; 288 case AFMT_MPEG: 289 return "MPEG"; 290 default: 291 return "unknown"; 292 } 293 } 294 #endif 295 296 static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream, 297 struct snd_info_buffer *buffer) 298 { 299 struct snd_pcm_info *info; 300 int err; 301 302 if (! substream) 303 return; 304 305 info = kmalloc(sizeof(*info), GFP_KERNEL); 306 if (! info) { 307 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n"); 308 return; 309 } 310 311 err = snd_pcm_info(substream, info); 312 if (err < 0) { 313 snd_iprintf(buffer, "error %d\n", err); 314 kfree(info); 315 return; 316 } 317 snd_iprintf(buffer, "card: %d\n", info->card); 318 snd_iprintf(buffer, "device: %d\n", info->device); 319 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice); 320 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream)); 321 snd_iprintf(buffer, "id: %s\n", info->id); 322 snd_iprintf(buffer, "name: %s\n", info->name); 323 snd_iprintf(buffer, "subname: %s\n", info->subname); 324 snd_iprintf(buffer, "class: %d\n", info->dev_class); 325 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass); 326 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count); 327 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail); 328 kfree(info); 329 } 330 331 static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry, 332 struct snd_info_buffer *buffer) 333 { 334 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream, 335 buffer); 336 } 337 338 static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry, 339 struct snd_info_buffer *buffer) 340 { 341 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data, 342 buffer); 343 } 344 345 static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry, 346 struct snd_info_buffer *buffer) 347 { 348 struct snd_pcm_substream *substream = entry->private_data; 349 struct snd_pcm_runtime *runtime = substream->runtime; 350 if (!runtime) { 351 snd_iprintf(buffer, "closed\n"); 352 return; 353 } 354 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { 355 snd_iprintf(buffer, "no setup\n"); 356 return; 357 } 358 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access)); 359 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format)); 360 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat)); 361 snd_iprintf(buffer, "channels: %u\n", runtime->channels); 362 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den); 363 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size); 364 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size); 365 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time); 366 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE) 367 if (substream->oss.oss) { 368 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format)); 369 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels); 370 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate); 371 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes); 372 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods); 373 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames); 374 } 375 #endif 376 } 377 378 static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry, 379 struct snd_info_buffer *buffer) 380 { 381 struct snd_pcm_substream *substream = entry->private_data; 382 struct snd_pcm_runtime *runtime = substream->runtime; 383 if (!runtime) { 384 snd_iprintf(buffer, "closed\n"); 385 return; 386 } 387 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { 388 snd_iprintf(buffer, "no setup\n"); 389 return; 390 } 391 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode)); 392 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step); 393 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min); 394 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min); 395 snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align); 396 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold); 397 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold); 398 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold); 399 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size); 400 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary); 401 } 402 403 static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry, 404 struct snd_info_buffer *buffer) 405 { 406 struct snd_pcm_substream *substream = entry->private_data; 407 struct snd_pcm_runtime *runtime = substream->runtime; 408 struct snd_pcm_status status; 409 int err; 410 if (!runtime) { 411 snd_iprintf(buffer, "closed\n"); 412 return; 413 } 414 memset(&status, 0, sizeof(status)); 415 err = snd_pcm_status(substream, &status); 416 if (err < 0) { 417 snd_iprintf(buffer, "error %d\n", err); 418 return; 419 } 420 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state)); 421 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n", 422 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec); 423 snd_iprintf(buffer, "tstamp : %ld.%09ld\n", 424 status.tstamp.tv_sec, status.tstamp.tv_nsec); 425 snd_iprintf(buffer, "delay : %ld\n", status.delay); 426 snd_iprintf(buffer, "avail : %ld\n", status.avail); 427 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max); 428 snd_iprintf(buffer, "-----\n"); 429 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr); 430 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr); 431 } 432 433 #ifdef CONFIG_SND_PCM_XRUN_DEBUG 434 static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry, 435 struct snd_info_buffer *buffer) 436 { 437 struct snd_pcm_str *pstr = entry->private_data; 438 snd_iprintf(buffer, "%d\n", pstr->xrun_debug); 439 } 440 441 static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry, 442 struct snd_info_buffer *buffer) 443 { 444 struct snd_pcm_str *pstr = entry->private_data; 445 char line[64]; 446 if (!snd_info_get_line(buffer, line, sizeof(line))) 447 pstr->xrun_debug = simple_strtoul(line, NULL, 10); 448 } 449 #endif 450 451 static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) 452 { 453 struct snd_pcm *pcm = pstr->pcm; 454 struct snd_info_entry *entry; 455 char name[16]; 456 457 sprintf(name, "pcm%i%c", pcm->device, 458 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c'); 459 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL) 460 return -ENOMEM; 461 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO; 462 if (snd_info_register(entry) < 0) { 463 snd_info_free_entry(entry); 464 return -ENOMEM; 465 } 466 pstr->proc_root = entry; 467 468 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) { 469 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read); 470 if (snd_info_register(entry) < 0) { 471 snd_info_free_entry(entry); 472 entry = NULL; 473 } 474 } 475 pstr->proc_info_entry = entry; 476 477 #ifdef CONFIG_SND_PCM_XRUN_DEBUG 478 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug", 479 pstr->proc_root)) != NULL) { 480 entry->c.text.read = snd_pcm_xrun_debug_read; 481 entry->c.text.write = snd_pcm_xrun_debug_write; 482 entry->mode |= S_IWUSR; 483 entry->private_data = pstr; 484 if (snd_info_register(entry) < 0) { 485 snd_info_free_entry(entry); 486 entry = NULL; 487 } 488 } 489 pstr->proc_xrun_debug_entry = entry; 490 #endif 491 return 0; 492 } 493 494 static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) 495 { 496 #ifdef CONFIG_SND_PCM_XRUN_DEBUG 497 if (pstr->proc_xrun_debug_entry) { 498 snd_info_unregister(pstr->proc_xrun_debug_entry); 499 pstr->proc_xrun_debug_entry = NULL; 500 } 501 #endif 502 if (pstr->proc_info_entry) { 503 snd_info_unregister(pstr->proc_info_entry); 504 pstr->proc_info_entry = NULL; 505 } 506 if (pstr->proc_root) { 507 snd_info_unregister(pstr->proc_root); 508 pstr->proc_root = NULL; 509 } 510 return 0; 511 } 512 513 static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) 514 { 515 struct snd_info_entry *entry; 516 struct snd_card *card; 517 char name[16]; 518 519 card = substream->pcm->card; 520 521 sprintf(name, "sub%i", substream->number); 522 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL) 523 return -ENOMEM; 524 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO; 525 if (snd_info_register(entry) < 0) { 526 snd_info_free_entry(entry); 527 return -ENOMEM; 528 } 529 substream->proc_root = entry; 530 531 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) { 532 snd_info_set_text_ops(entry, substream, 533 snd_pcm_substream_proc_info_read); 534 if (snd_info_register(entry) < 0) { 535 snd_info_free_entry(entry); 536 entry = NULL; 537 } 538 } 539 substream->proc_info_entry = entry; 540 541 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) { 542 snd_info_set_text_ops(entry, substream, 543 snd_pcm_substream_proc_hw_params_read); 544 if (snd_info_register(entry) < 0) { 545 snd_info_free_entry(entry); 546 entry = NULL; 547 } 548 } 549 substream->proc_hw_params_entry = entry; 550 551 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) { 552 snd_info_set_text_ops(entry, substream, 553 snd_pcm_substream_proc_sw_params_read); 554 if (snd_info_register(entry) < 0) { 555 snd_info_free_entry(entry); 556 entry = NULL; 557 } 558 } 559 substream->proc_sw_params_entry = entry; 560 561 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) { 562 snd_info_set_text_ops(entry, substream, 563 snd_pcm_substream_proc_status_read); 564 if (snd_info_register(entry) < 0) { 565 snd_info_free_entry(entry); 566 entry = NULL; 567 } 568 } 569 substream->proc_status_entry = entry; 570 571 return 0; 572 } 573 574 static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) 575 { 576 if (substream->proc_info_entry) { 577 snd_info_unregister(substream->proc_info_entry); 578 substream->proc_info_entry = NULL; 579 } 580 if (substream->proc_hw_params_entry) { 581 snd_info_unregister(substream->proc_hw_params_entry); 582 substream->proc_hw_params_entry = NULL; 583 } 584 if (substream->proc_sw_params_entry) { 585 snd_info_unregister(substream->proc_sw_params_entry); 586 substream->proc_sw_params_entry = NULL; 587 } 588 if (substream->proc_status_entry) { 589 snd_info_unregister(substream->proc_status_entry); 590 substream->proc_status_entry = NULL; 591 } 592 if (substream->proc_root) { 593 snd_info_unregister(substream->proc_root); 594 substream->proc_root = NULL; 595 } 596 return 0; 597 } 598 #else /* !CONFIG_SND_VERBOSE_PROCFS */ 599 static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; } 600 static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; } 601 static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; } 602 static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; } 603 #endif /* CONFIG_SND_VERBOSE_PROCFS */ 604 605 /** 606 * snd_pcm_new_stream - create a new PCM stream 607 * @pcm: the pcm instance 608 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX 609 * @substream_count: the number of substreams 610 * 611 * Creates a new stream for the pcm. 612 * The corresponding stream on the pcm must have been empty before 613 * calling this, i.e. zero must be given to the argument of 614 * snd_pcm_new(). 615 * 616 * Returns zero if successful, or a negative error code on failure. 617 */ 618 int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count) 619 { 620 int idx, err; 621 struct snd_pcm_str *pstr = &pcm->streams[stream]; 622 struct snd_pcm_substream *substream, *prev; 623 624 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE) 625 mutex_init(&pstr->oss.setup_mutex); 626 #endif 627 pstr->stream = stream; 628 pstr->pcm = pcm; 629 pstr->substream_count = substream_count; 630 if (substream_count > 0) { 631 err = snd_pcm_stream_proc_init(pstr); 632 if (err < 0) { 633 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n"); 634 return err; 635 } 636 } 637 prev = NULL; 638 for (idx = 0, prev = NULL; idx < substream_count; idx++) { 639 substream = kzalloc(sizeof(*substream), GFP_KERNEL); 640 if (substream == NULL) { 641 snd_printk(KERN_ERR "Cannot allocate PCM substream\n"); 642 return -ENOMEM; 643 } 644 substream->pcm = pcm; 645 substream->pstr = pstr; 646 substream->number = idx; 647 substream->stream = stream; 648 sprintf(substream->name, "subdevice #%i", idx); 649 substream->buffer_bytes_max = UINT_MAX; 650 if (prev == NULL) 651 pstr->substream = substream; 652 else 653 prev->next = substream; 654 err = snd_pcm_substream_proc_init(substream); 655 if (err < 0) { 656 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n"); 657 kfree(substream); 658 return err; 659 } 660 substream->group = &substream->self_group; 661 spin_lock_init(&substream->self_group.lock); 662 INIT_LIST_HEAD(&substream->self_group.substreams); 663 list_add_tail(&substream->link_list, &substream->self_group.substreams); 664 spin_lock_init(&substream->timer_lock); 665 atomic_set(&substream->mmap_count, 0); 666 prev = substream; 667 } 668 return 0; 669 } 670 671 EXPORT_SYMBOL(snd_pcm_new_stream); 672 673 /** 674 * snd_pcm_new - create a new PCM instance 675 * @card: the card instance 676 * @id: the id string 677 * @device: the device index (zero based) 678 * @playback_count: the number of substreams for playback 679 * @capture_count: the number of substreams for capture 680 * @rpcm: the pointer to store the new pcm instance 681 * 682 * Creates a new PCM instance. 683 * 684 * The pcm operators have to be set afterwards to the new instance 685 * via snd_pcm_set_ops(). 686 * 687 * Returns zero if successful, or a negative error code on failure. 688 */ 689 int snd_pcm_new(struct snd_card *card, char *id, int device, 690 int playback_count, int capture_count, 691 struct snd_pcm ** rpcm) 692 { 693 struct snd_pcm *pcm; 694 int err; 695 static struct snd_device_ops ops = { 696 .dev_free = snd_pcm_dev_free, 697 .dev_register = snd_pcm_dev_register, 698 .dev_disconnect = snd_pcm_dev_disconnect, 699 .dev_unregister = snd_pcm_dev_unregister 700 }; 701 702 snd_assert(rpcm != NULL, return -EINVAL); 703 *rpcm = NULL; 704 snd_assert(card != NULL, return -ENXIO); 705 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL); 706 if (pcm == NULL) { 707 snd_printk(KERN_ERR "Cannot allocate PCM\n"); 708 return -ENOMEM; 709 } 710 pcm->card = card; 711 pcm->device = device; 712 if (id) 713 strlcpy(pcm->id, id, sizeof(pcm->id)); 714 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) { 715 snd_pcm_free(pcm); 716 return err; 717 } 718 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) { 719 snd_pcm_free(pcm); 720 return err; 721 } 722 mutex_init(&pcm->open_mutex); 723 init_waitqueue_head(&pcm->open_wait); 724 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) { 725 snd_pcm_free(pcm); 726 return err; 727 } 728 *rpcm = pcm; 729 return 0; 730 } 731 732 EXPORT_SYMBOL(snd_pcm_new); 733 734 static void snd_pcm_free_stream(struct snd_pcm_str * pstr) 735 { 736 struct snd_pcm_substream *substream, *substream_next; 737 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE) 738 struct snd_pcm_oss_setup *setup, *setupn; 739 #endif 740 substream = pstr->substream; 741 while (substream) { 742 substream_next = substream->next; 743 snd_pcm_substream_proc_done(substream); 744 kfree(substream); 745 substream = substream_next; 746 } 747 snd_pcm_stream_proc_done(pstr); 748 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE) 749 for (setup = pstr->oss.setup_list; setup; setup = setupn) { 750 setupn = setup->next; 751 kfree(setup->task_name); 752 kfree(setup); 753 } 754 #endif 755 } 756 757 static int snd_pcm_free(struct snd_pcm *pcm) 758 { 759 snd_assert(pcm != NULL, return -ENXIO); 760 if (pcm->private_free) 761 pcm->private_free(pcm); 762 snd_pcm_lib_preallocate_free_for_all(pcm); 763 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]); 764 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]); 765 kfree(pcm); 766 return 0; 767 } 768 769 static int snd_pcm_dev_free(struct snd_device *device) 770 { 771 struct snd_pcm *pcm = device->device_data; 772 return snd_pcm_free(pcm); 773 } 774 775 static void snd_pcm_tick_timer_func(unsigned long data) 776 { 777 struct snd_pcm_substream *substream = (struct snd_pcm_substream *) data; 778 snd_pcm_tick_elapsed(substream); 779 } 780 781 int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, 782 struct file *file, 783 struct snd_pcm_substream **rsubstream) 784 { 785 struct snd_pcm_str * pstr; 786 struct snd_pcm_substream *substream; 787 struct snd_pcm_runtime *runtime; 788 struct snd_ctl_file *kctl; 789 struct snd_card *card; 790 struct list_head *list; 791 int prefer_subdevice = -1; 792 size_t size; 793 794 snd_assert(rsubstream != NULL, return -EINVAL); 795 *rsubstream = NULL; 796 snd_assert(pcm != NULL, return -ENXIO); 797 pstr = &pcm->streams[stream]; 798 if (pstr->substream == NULL || pstr->substream_count == 0) 799 return -ENODEV; 800 801 card = pcm->card; 802 down_read(&card->controls_rwsem); 803 list_for_each(list, &card->ctl_files) { 804 kctl = snd_ctl_file(list); 805 if (kctl->pid == current->pid) { 806 prefer_subdevice = kctl->prefer_pcm_subdevice; 807 break; 808 } 809 } 810 up_read(&card->controls_rwsem); 811 812 switch (stream) { 813 case SNDRV_PCM_STREAM_PLAYBACK: 814 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) { 815 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) { 816 if (SUBSTREAM_BUSY(substream)) 817 return -EAGAIN; 818 } 819 } 820 break; 821 case SNDRV_PCM_STREAM_CAPTURE: 822 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) { 823 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) { 824 if (SUBSTREAM_BUSY(substream)) 825 return -EAGAIN; 826 } 827 } 828 break; 829 default: 830 return -EINVAL; 831 } 832 833 if (file->f_flags & O_APPEND) { 834 if (prefer_subdevice < 0) { 835 if (pstr->substream_count > 1) 836 return -EINVAL; /* must be unique */ 837 substream = pstr->substream; 838 } else { 839 for (substream = pstr->substream; substream; 840 substream = substream->next) 841 if (substream->number == prefer_subdevice) 842 break; 843 } 844 if (! substream) 845 return -ENODEV; 846 if (! SUBSTREAM_BUSY(substream)) 847 return -EBADFD; 848 substream->ref_count++; 849 *rsubstream = substream; 850 return 0; 851 } 852 853 if (prefer_subdevice >= 0) { 854 for (substream = pstr->substream; substream; substream = substream->next) 855 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice) 856 goto __ok; 857 } 858 for (substream = pstr->substream; substream; substream = substream->next) 859 if (!SUBSTREAM_BUSY(substream)) 860 break; 861 __ok: 862 if (substream == NULL) 863 return -EAGAIN; 864 865 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); 866 if (runtime == NULL) 867 return -ENOMEM; 868 869 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)); 870 runtime->status = snd_malloc_pages(size, GFP_KERNEL); 871 if (runtime->status == NULL) { 872 kfree(runtime); 873 return -ENOMEM; 874 } 875 memset((void*)runtime->status, 0, size); 876 877 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)); 878 runtime->control = snd_malloc_pages(size, GFP_KERNEL); 879 if (runtime->control == NULL) { 880 snd_free_pages((void*)runtime->status, 881 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))); 882 kfree(runtime); 883 return -ENOMEM; 884 } 885 memset((void*)runtime->control, 0, size); 886 887 init_waitqueue_head(&runtime->sleep); 888 init_timer(&runtime->tick_timer); 889 runtime->tick_timer.function = snd_pcm_tick_timer_func; 890 runtime->tick_timer.data = (unsigned long) substream; 891 892 runtime->status->state = SNDRV_PCM_STATE_OPEN; 893 894 substream->runtime = runtime; 895 substream->private_data = pcm->private_data; 896 substream->ref_count = 1; 897 substream->f_flags = file->f_flags; 898 pstr->substream_opened++; 899 *rsubstream = substream; 900 return 0; 901 } 902 903 void snd_pcm_detach_substream(struct snd_pcm_substream *substream) 904 { 905 struct snd_pcm_runtime *runtime; 906 907 runtime = substream->runtime; 908 snd_assert(runtime != NULL, return); 909 if (runtime->private_free != NULL) 910 runtime->private_free(runtime); 911 snd_free_pages((void*)runtime->status, 912 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))); 913 snd_free_pages((void*)runtime->control, 914 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control))); 915 kfree(runtime->hw_constraints.rules); 916 kfree(runtime); 917 substream->runtime = NULL; 918 substream->pstr->substream_opened--; 919 } 920 921 static int snd_pcm_dev_register(struct snd_device *device) 922 { 923 int cidx, err; 924 struct snd_pcm_substream *substream; 925 struct list_head *list; 926 char str[16]; 927 struct snd_pcm *pcm = device->device_data; 928 929 snd_assert(pcm != NULL && device != NULL, return -ENXIO); 930 mutex_lock(®ister_mutex); 931 if (snd_pcm_search(pcm->card, pcm->device)) { 932 mutex_unlock(®ister_mutex); 933 return -EBUSY; 934 } 935 list_add_tail(&pcm->list, &snd_pcm_devices); 936 for (cidx = 0; cidx < 2; cidx++) { 937 int devtype = -1; 938 if (pcm->streams[cidx].substream == NULL) 939 continue; 940 switch (cidx) { 941 case SNDRV_PCM_STREAM_PLAYBACK: 942 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device); 943 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK; 944 break; 945 case SNDRV_PCM_STREAM_CAPTURE: 946 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device); 947 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE; 948 break; 949 } 950 if ((err = snd_register_device(devtype, pcm->card, 951 pcm->device, 952 &snd_pcm_f_ops[cidx], 953 pcm, str)) < 0) 954 { 955 list_del(&pcm->list); 956 mutex_unlock(®ister_mutex); 957 return err; 958 } 959 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) 960 snd_pcm_timer_init(substream); 961 } 962 list_for_each(list, &snd_pcm_notify_list) { 963 struct snd_pcm_notify *notify; 964 notify = list_entry(list, struct snd_pcm_notify, list); 965 notify->n_register(pcm); 966 } 967 mutex_unlock(®ister_mutex); 968 return 0; 969 } 970 971 static int snd_pcm_dev_disconnect(struct snd_device *device) 972 { 973 struct snd_pcm *pcm = device->device_data; 974 struct list_head *list; 975 struct snd_pcm_substream *substream; 976 int cidx; 977 978 mutex_lock(®ister_mutex); 979 list_del_init(&pcm->list); 980 for (cidx = 0; cidx < 2; cidx++) 981 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) 982 if (substream->runtime) 983 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED; 984 list_for_each(list, &snd_pcm_notify_list) { 985 struct snd_pcm_notify *notify; 986 notify = list_entry(list, struct snd_pcm_notify, list); 987 notify->n_disconnect(pcm); 988 } 989 mutex_unlock(®ister_mutex); 990 return 0; 991 } 992 993 static int snd_pcm_dev_unregister(struct snd_device *device) 994 { 995 int cidx, devtype; 996 struct snd_pcm_substream *substream; 997 struct list_head *list; 998 struct snd_pcm *pcm = device->device_data; 999 1000 snd_assert(pcm != NULL, return -ENXIO); 1001 mutex_lock(®ister_mutex); 1002 list_del(&pcm->list); 1003 for (cidx = 0; cidx < 2; cidx++) { 1004 devtype = -1; 1005 switch (cidx) { 1006 case SNDRV_PCM_STREAM_PLAYBACK: 1007 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK; 1008 break; 1009 case SNDRV_PCM_STREAM_CAPTURE: 1010 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE; 1011 break; 1012 } 1013 snd_unregister_device(devtype, pcm->card, pcm->device); 1014 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) 1015 snd_pcm_timer_done(substream); 1016 } 1017 list_for_each(list, &snd_pcm_notify_list) { 1018 struct snd_pcm_notify *notify; 1019 notify = list_entry(list, struct snd_pcm_notify, list); 1020 notify->n_unregister(pcm); 1021 } 1022 mutex_unlock(®ister_mutex); 1023 return snd_pcm_free(pcm); 1024 } 1025 1026 int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree) 1027 { 1028 struct list_head *p; 1029 1030 snd_assert(notify != NULL && notify->n_register != NULL && notify->n_unregister != NULL, return -EINVAL); 1031 mutex_lock(®ister_mutex); 1032 if (nfree) { 1033 list_del(¬ify->list); 1034 list_for_each(p, &snd_pcm_devices) 1035 notify->n_unregister(list_entry(p, 1036 struct snd_pcm, list)); 1037 } else { 1038 list_add_tail(¬ify->list, &snd_pcm_notify_list); 1039 list_for_each(p, &snd_pcm_devices) 1040 notify->n_register(list_entry(p, struct snd_pcm, list)); 1041 } 1042 mutex_unlock(®ister_mutex); 1043 return 0; 1044 } 1045 1046 EXPORT_SYMBOL(snd_pcm_notify); 1047 1048 #ifdef CONFIG_PROC_FS 1049 /* 1050 * Info interface 1051 */ 1052 1053 static void snd_pcm_proc_read(struct snd_info_entry *entry, 1054 struct snd_info_buffer *buffer) 1055 { 1056 struct list_head *p; 1057 struct snd_pcm *pcm; 1058 1059 mutex_lock(®ister_mutex); 1060 list_for_each(p, &snd_pcm_devices) { 1061 pcm = list_entry(p, struct snd_pcm, list); 1062 snd_iprintf(buffer, "%02i-%02i: %s : %s", 1063 pcm->card->number, pcm->device, pcm->id, pcm->name); 1064 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) 1065 snd_iprintf(buffer, " : playback %i", 1066 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count); 1067 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) 1068 snd_iprintf(buffer, " : capture %i", 1069 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count); 1070 snd_iprintf(buffer, "\n"); 1071 } 1072 mutex_unlock(®ister_mutex); 1073 } 1074 1075 static struct snd_info_entry *snd_pcm_proc_entry; 1076 1077 static void snd_pcm_proc_init(void) 1078 { 1079 struct snd_info_entry *entry; 1080 1081 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) { 1082 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read); 1083 if (snd_info_register(entry) < 0) { 1084 snd_info_free_entry(entry); 1085 entry = NULL; 1086 } 1087 } 1088 snd_pcm_proc_entry = entry; 1089 } 1090 1091 static void snd_pcm_proc_done(void) 1092 { 1093 if (snd_pcm_proc_entry) 1094 snd_info_unregister(snd_pcm_proc_entry); 1095 } 1096 1097 #else /* !CONFIG_PROC_FS */ 1098 #define snd_pcm_proc_init() 1099 #define snd_pcm_proc_done() 1100 #endif /* CONFIG_PROC_FS */ 1101 1102 1103 /* 1104 * ENTRY functions 1105 */ 1106 1107 static int __init alsa_pcm_init(void) 1108 { 1109 snd_ctl_register_ioctl(snd_pcm_control_ioctl); 1110 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl); 1111 snd_pcm_proc_init(); 1112 return 0; 1113 } 1114 1115 static void __exit alsa_pcm_exit(void) 1116 { 1117 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl); 1118 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl); 1119 snd_pcm_proc_done(); 1120 } 1121 1122 module_init(alsa_pcm_init) 1123 module_exit(alsa_pcm_exit) 1124