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