2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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.
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.
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
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>
33 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
34 MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
35 MODULE_LICENSE("GPL");
37 static LIST_HEAD(snd_pcm_devices);
38 static LIST_HEAD(snd_pcm_notify_list);
39 static DEFINE_MUTEX(register_mutex);
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);
46 static struct snd_pcm *snd_pcm_search(struct snd_card *card, int device)
50 list_for_each_entry(pcm, &snd_pcm_devices, list) {
51 if (pcm->card == card && pcm->device == device)
57 static int snd_pcm_control_ioctl(struct snd_card *card,
58 struct snd_ctl_file *control,
59 unsigned int cmd, unsigned long arg)
62 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
66 if (get_user(device, (int __user *)arg))
68 mutex_lock(®ister_mutex);
69 device = device < 0 ? 0 : device + 1;
70 while (device < SNDRV_PCM_DEVICES) {
71 if (snd_pcm_search(card, device))
75 if (device == SNDRV_PCM_DEVICES)
77 mutex_unlock(®ister_mutex);
78 if (put_user(device, (int __user *)arg))
82 case SNDRV_CTL_IOCTL_PCM_INFO:
84 struct snd_pcm_info __user *info;
85 unsigned int device, subdevice;
88 struct snd_pcm_str *pstr;
89 struct snd_pcm_substream *substream;
92 info = (struct snd_pcm_info __user *)arg;
93 if (get_user(device, &info->device))
95 if (get_user(stream, &info->stream))
97 if (stream < 0 || stream > 1)
99 if (get_user(subdevice, &info->subdevice))
101 mutex_lock(®ister_mutex);
102 pcm = snd_pcm_search(card, device);
107 pstr = &pcm->streams[stream];
108 if (pstr->substream_count == 0) {
112 if (subdevice >= pstr->substream_count) {
116 for (substream = pstr->substream; substream;
117 substream = substream->next)
118 if (substream->number == (int)subdevice)
120 if (substream == NULL) {
124 err = snd_pcm_info_user(substream, info);
126 mutex_unlock(®ister_mutex);
129 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
133 if (get_user(val, (int __user *)arg))
135 control->prefer_pcm_subdevice = val;
142 #ifdef CONFIG_SND_VERBOSE_PROCFS
144 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
145 #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
146 #define READY(v) [SNDRV_PCM_READY_##v] = #v
147 #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
148 #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
149 #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
150 #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
151 #define START(v) [SNDRV_PCM_START_##v] = #v
152 #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
153 #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
155 static char *snd_pcm_format_names[] = {
174 FORMAT(IEC958_SUBFRAME_LE),
175 FORMAT(IEC958_SUBFRAME_BE),
196 static const char *snd_pcm_format_name(snd_pcm_format_t format)
198 return snd_pcm_format_names[format];
201 static char *snd_pcm_stream_names[] = {
206 static char *snd_pcm_state_names[] = {
217 static char *snd_pcm_access_names[] = {
218 ACCESS(MMAP_INTERLEAVED),
219 ACCESS(MMAP_NONINTERLEAVED),
220 ACCESS(MMAP_COMPLEX),
221 ACCESS(RW_INTERLEAVED),
222 ACCESS(RW_NONINTERLEAVED),
225 static char *snd_pcm_subformat_names[] = {
229 static char *snd_pcm_tstamp_mode_names[] = {
234 static const char *snd_pcm_stream_name(int stream)
236 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
237 return snd_pcm_stream_names[stream];
240 static const char *snd_pcm_access_name(snd_pcm_access_t access)
242 return snd_pcm_access_names[access];
245 static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
247 return snd_pcm_subformat_names[subformat];
250 static const char *snd_pcm_tstamp_mode_name(int mode)
252 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
253 return snd_pcm_tstamp_mode_names[mode];
256 static const char *snd_pcm_state_name(snd_pcm_state_t state)
258 return snd_pcm_state_names[state];
261 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
262 #include <linux/soundcard.h>
264 static const char *snd_pcm_oss_format_name(int format)
293 static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
294 struct snd_info_buffer *buffer)
296 struct snd_pcm_info *info;
302 info = kmalloc(sizeof(*info), GFP_KERNEL);
304 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
308 err = snd_pcm_info(substream, info);
310 snd_iprintf(buffer, "error %d\n", err);
314 snd_iprintf(buffer, "card: %d\n", info->card);
315 snd_iprintf(buffer, "device: %d\n", info->device);
316 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
317 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
318 snd_iprintf(buffer, "id: %s\n", info->id);
319 snd_iprintf(buffer, "name: %s\n", info->name);
320 snd_iprintf(buffer, "subname: %s\n", info->subname);
321 snd_iprintf(buffer, "class: %d\n", info->dev_class);
322 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
323 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
324 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
328 static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
329 struct snd_info_buffer *buffer)
331 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
335 static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
336 struct snd_info_buffer *buffer)
338 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data,
342 static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
343 struct snd_info_buffer *buffer)
345 struct snd_pcm_substream *substream = entry->private_data;
346 struct snd_pcm_runtime *runtime = substream->runtime;
348 snd_iprintf(buffer, "closed\n");
351 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
352 snd_iprintf(buffer, "no setup\n");
355 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
356 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
357 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
358 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
359 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
360 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
361 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
362 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
363 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
364 if (substream->oss.oss) {
365 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
366 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
367 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
368 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
369 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
370 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
375 static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
376 struct snd_info_buffer *buffer)
378 struct snd_pcm_substream *substream = entry->private_data;
379 struct snd_pcm_runtime *runtime = substream->runtime;
381 snd_iprintf(buffer, "closed\n");
384 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
385 snd_iprintf(buffer, "no setup\n");
388 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
389 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
390 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
391 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
392 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
393 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
394 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
395 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
396 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
399 static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
400 struct snd_info_buffer *buffer)
402 struct snd_pcm_substream *substream = entry->private_data;
403 struct snd_pcm_runtime *runtime = substream->runtime;
404 struct snd_pcm_status status;
407 snd_iprintf(buffer, "closed\n");
410 memset(&status, 0, sizeof(status));
411 err = snd_pcm_status(substream, &status);
413 snd_iprintf(buffer, "error %d\n", err);
416 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
417 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
418 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
419 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
420 status.tstamp.tv_sec, status.tstamp.tv_nsec);
421 snd_iprintf(buffer, "delay : %ld\n", status.delay);
422 snd_iprintf(buffer, "avail : %ld\n", status.avail);
423 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
424 snd_iprintf(buffer, "-----\n");
425 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
426 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
429 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
430 static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
431 struct snd_info_buffer *buffer)
433 struct snd_pcm_str *pstr = entry->private_data;
434 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
437 static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
438 struct snd_info_buffer *buffer)
440 struct snd_pcm_str *pstr = entry->private_data;
442 if (!snd_info_get_line(buffer, line, sizeof(line)))
443 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
447 static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
449 struct snd_pcm *pcm = pstr->pcm;
450 struct snd_info_entry *entry;
453 sprintf(name, "pcm%i%c", pcm->device,
454 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
455 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
457 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
458 if (snd_info_register(entry) < 0) {
459 snd_info_free_entry(entry);
462 pstr->proc_root = entry;
464 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
465 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
466 if (snd_info_register(entry) < 0) {
467 snd_info_free_entry(entry);
471 pstr->proc_info_entry = entry;
473 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
474 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
475 pstr->proc_root)) != NULL) {
476 entry->c.text.read = snd_pcm_xrun_debug_read;
477 entry->c.text.write = snd_pcm_xrun_debug_write;
478 entry->mode |= S_IWUSR;
479 entry->private_data = pstr;
480 if (snd_info_register(entry) < 0) {
481 snd_info_free_entry(entry);
485 pstr->proc_xrun_debug_entry = entry;
490 static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
492 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
493 snd_info_free_entry(pstr->proc_xrun_debug_entry);
494 pstr->proc_xrun_debug_entry = NULL;
496 snd_info_free_entry(pstr->proc_info_entry);
497 pstr->proc_info_entry = NULL;
498 snd_info_free_entry(pstr->proc_root);
499 pstr->proc_root = NULL;
503 static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
505 struct snd_info_entry *entry;
506 struct snd_card *card;
509 card = substream->pcm->card;
511 sprintf(name, "sub%i", substream->number);
512 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
514 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
515 if (snd_info_register(entry) < 0) {
516 snd_info_free_entry(entry);
519 substream->proc_root = entry;
521 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
522 snd_info_set_text_ops(entry, substream,
523 snd_pcm_substream_proc_info_read);
524 if (snd_info_register(entry) < 0) {
525 snd_info_free_entry(entry);
529 substream->proc_info_entry = entry;
531 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
532 snd_info_set_text_ops(entry, substream,
533 snd_pcm_substream_proc_hw_params_read);
534 if (snd_info_register(entry) < 0) {
535 snd_info_free_entry(entry);
539 substream->proc_hw_params_entry = entry;
541 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
542 snd_info_set_text_ops(entry, substream,
543 snd_pcm_substream_proc_sw_params_read);
544 if (snd_info_register(entry) < 0) {
545 snd_info_free_entry(entry);
549 substream->proc_sw_params_entry = entry;
551 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
552 snd_info_set_text_ops(entry, substream,
553 snd_pcm_substream_proc_status_read);
554 if (snd_info_register(entry) < 0) {
555 snd_info_free_entry(entry);
559 substream->proc_status_entry = entry;
564 static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
566 snd_info_free_entry(substream->proc_info_entry);
567 substream->proc_info_entry = NULL;
568 snd_info_free_entry(substream->proc_hw_params_entry);
569 substream->proc_hw_params_entry = NULL;
570 snd_info_free_entry(substream->proc_sw_params_entry);
571 substream->proc_sw_params_entry = NULL;
572 snd_info_free_entry(substream->proc_status_entry);
573 substream->proc_status_entry = NULL;
574 snd_info_free_entry(substream->proc_root);
575 substream->proc_root = NULL;
578 #else /* !CONFIG_SND_VERBOSE_PROCFS */
579 static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
580 static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
581 static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
582 static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
583 #endif /* CONFIG_SND_VERBOSE_PROCFS */
586 * snd_pcm_new_stream - create a new PCM stream
587 * @pcm: the pcm instance
588 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
589 * @substream_count: the number of substreams
591 * Creates a new stream for the pcm.
592 * The corresponding stream on the pcm must have been empty before
593 * calling this, i.e. zero must be given to the argument of
596 * Returns zero if successful, or a negative error code on failure.
598 int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
601 struct snd_pcm_str *pstr = &pcm->streams[stream];
602 struct snd_pcm_substream *substream, *prev;
604 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
605 mutex_init(&pstr->oss.setup_mutex);
607 pstr->stream = stream;
609 pstr->substream_count = substream_count;
610 if (substream_count > 0) {
611 err = snd_pcm_stream_proc_init(pstr);
613 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
618 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
619 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
620 if (substream == NULL) {
621 snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
624 substream->pcm = pcm;
625 substream->pstr = pstr;
626 substream->number = idx;
627 substream->stream = stream;
628 sprintf(substream->name, "subdevice #%i", idx);
629 snprintf(substream->latency_id, sizeof(substream->latency_id),
630 "ALSA-PCM%d-%d%c%d", pcm->card->number, pcm->device,
631 (stream ? 'c' : 'p'), idx);
632 substream->buffer_bytes_max = UINT_MAX;
634 pstr->substream = substream;
636 prev->next = substream;
637 err = snd_pcm_substream_proc_init(substream);
639 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
641 pstr->substream = NULL;
647 substream->group = &substream->self_group;
648 spin_lock_init(&substream->self_group.lock);
649 INIT_LIST_HEAD(&substream->self_group.substreams);
650 list_add_tail(&substream->link_list, &substream->self_group.substreams);
651 spin_lock_init(&substream->timer_lock);
652 atomic_set(&substream->mmap_count, 0);
658 EXPORT_SYMBOL(snd_pcm_new_stream);
661 * snd_pcm_new - create a new PCM instance
662 * @card: the card instance
664 * @device: the device index (zero based)
665 * @playback_count: the number of substreams for playback
666 * @capture_count: the number of substreams for capture
667 * @rpcm: the pointer to store the new pcm instance
669 * Creates a new PCM instance.
671 * The pcm operators have to be set afterwards to the new instance
672 * via snd_pcm_set_ops().
674 * Returns zero if successful, or a negative error code on failure.
676 int snd_pcm_new(struct snd_card *card, char *id, int device,
677 int playback_count, int capture_count,
678 struct snd_pcm ** rpcm)
682 static struct snd_device_ops ops = {
683 .dev_free = snd_pcm_dev_free,
684 .dev_register = snd_pcm_dev_register,
685 .dev_disconnect = snd_pcm_dev_disconnect,
688 snd_assert(rpcm != NULL, return -EINVAL);
690 snd_assert(card != NULL, return -ENXIO);
691 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
693 snd_printk(KERN_ERR "Cannot allocate PCM\n");
697 pcm->device = device;
699 strlcpy(pcm->id, id, sizeof(pcm->id));
700 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
704 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
708 mutex_init(&pcm->open_mutex);
709 init_waitqueue_head(&pcm->open_wait);
710 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
718 EXPORT_SYMBOL(snd_pcm_new);
720 static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
722 struct snd_pcm_substream *substream, *substream_next;
723 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
724 struct snd_pcm_oss_setup *setup, *setupn;
726 substream = pstr->substream;
728 substream_next = substream->next;
729 snd_pcm_timer_done(substream);
730 snd_pcm_substream_proc_done(substream);
732 substream = substream_next;
734 snd_pcm_stream_proc_done(pstr);
735 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
736 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
737 setupn = setup->next;
738 kfree(setup->task_name);
744 static int snd_pcm_free(struct snd_pcm *pcm)
746 struct snd_pcm_notify *notify;
748 snd_assert(pcm != NULL, return -ENXIO);
749 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
750 notify->n_unregister(pcm);
752 if (pcm->private_free)
753 pcm->private_free(pcm);
754 snd_pcm_lib_preallocate_free_for_all(pcm);
755 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
756 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
761 static int snd_pcm_dev_free(struct snd_device *device)
763 struct snd_pcm *pcm = device->device_data;
764 return snd_pcm_free(pcm);
767 static void snd_pcm_tick_timer_func(unsigned long data)
769 struct snd_pcm_substream *substream = (struct snd_pcm_substream *) data;
770 snd_pcm_tick_elapsed(substream);
773 int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
775 struct snd_pcm_substream **rsubstream)
777 struct snd_pcm_str * pstr;
778 struct snd_pcm_substream *substream;
779 struct snd_pcm_runtime *runtime;
780 struct snd_ctl_file *kctl;
781 struct snd_card *card;
782 int prefer_subdevice = -1;
785 snd_assert(rsubstream != NULL, return -EINVAL);
787 snd_assert(pcm != NULL, return -ENXIO);
788 pstr = &pcm->streams[stream];
789 if (pstr->substream == NULL || pstr->substream_count == 0)
793 down_read(&card->controls_rwsem);
794 list_for_each_entry(kctl, &card->ctl_files, list) {
795 if (kctl->pid == current->pid) {
796 prefer_subdevice = kctl->prefer_pcm_subdevice;
797 if (prefer_subdevice != -1)
801 up_read(&card->controls_rwsem);
804 case SNDRV_PCM_STREAM_PLAYBACK:
805 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
806 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
807 if (SUBSTREAM_BUSY(substream))
812 case SNDRV_PCM_STREAM_CAPTURE:
813 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
814 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
815 if (SUBSTREAM_BUSY(substream))
824 if (file->f_flags & O_APPEND) {
825 if (prefer_subdevice < 0) {
826 if (pstr->substream_count > 1)
827 return -EINVAL; /* must be unique */
828 substream = pstr->substream;
830 for (substream = pstr->substream; substream;
831 substream = substream->next)
832 if (substream->number == prefer_subdevice)
837 if (! SUBSTREAM_BUSY(substream))
839 substream->ref_count++;
840 *rsubstream = substream;
844 if (prefer_subdevice >= 0) {
845 for (substream = pstr->substream; substream; substream = substream->next)
846 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
849 for (substream = pstr->substream; substream; substream = substream->next)
850 if (!SUBSTREAM_BUSY(substream))
853 if (substream == NULL)
856 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
860 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
861 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
862 if (runtime->status == NULL) {
866 memset((void*)runtime->status, 0, size);
868 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
869 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
870 if (runtime->control == NULL) {
871 snd_free_pages((void*)runtime->status,
872 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
876 memset((void*)runtime->control, 0, size);
878 init_waitqueue_head(&runtime->sleep);
879 init_timer(&runtime->tick_timer);
880 runtime->tick_timer.function = snd_pcm_tick_timer_func;
881 runtime->tick_timer.data = (unsigned long) substream;
883 runtime->status->state = SNDRV_PCM_STATE_OPEN;
885 substream->runtime = runtime;
886 substream->private_data = pcm->private_data;
887 substream->ref_count = 1;
888 substream->f_flags = file->f_flags;
889 pstr->substream_opened++;
890 *rsubstream = substream;
894 void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
896 struct snd_pcm_runtime *runtime;
898 runtime = substream->runtime;
899 snd_assert(runtime != NULL, return);
900 if (runtime->private_free != NULL)
901 runtime->private_free(runtime);
902 snd_free_pages((void*)runtime->status,
903 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
904 snd_free_pages((void*)runtime->control,
905 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
906 kfree(runtime->hw_constraints.rules);
908 substream->runtime = NULL;
909 substream->pstr->substream_opened--;
912 static ssize_t show_pcm_class(struct device *dev,
913 struct device_attribute *attr, char *buf)
917 static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
918 [SNDRV_PCM_CLASS_GENERIC] = "generic",
919 [SNDRV_PCM_CLASS_MULTI] = "multi",
920 [SNDRV_PCM_CLASS_MODEM] = "modem",
921 [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
924 if (! (pcm = dev_get_drvdata(dev)) ||
925 pcm->dev_class > SNDRV_PCM_CLASS_LAST)
928 str = strs[pcm->dev_class];
929 return snprintf(buf, PAGE_SIZE, "%s\n", str);
932 static struct device_attribute pcm_attrs =
933 __ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
935 static int snd_pcm_dev_register(struct snd_device *device)
938 struct snd_pcm_substream *substream;
939 struct snd_pcm_notify *notify;
941 struct snd_pcm *pcm = device->device_data;
944 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
945 mutex_lock(®ister_mutex);
946 if (snd_pcm_search(pcm->card, pcm->device)) {
947 mutex_unlock(®ister_mutex);
950 list_add_tail(&pcm->list, &snd_pcm_devices);
951 for (cidx = 0; cidx < 2; cidx++) {
953 if (pcm->streams[cidx].substream == NULL)
956 case SNDRV_PCM_STREAM_PLAYBACK:
957 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
958 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
960 case SNDRV_PCM_STREAM_CAPTURE:
961 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
962 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
965 /* device pointer to use, pcm->dev takes precedence if
966 * it is assigned, otherwise fall back to card's device
970 dev = snd_card_get_device_link(pcm->card);
972 err = snd_register_device_for_dev(devtype, pcm->card,
974 &snd_pcm_f_ops[cidx],
977 list_del(&pcm->list);
978 mutex_unlock(®ister_mutex);
981 snd_add_device_sysfs_file(devtype, pcm->card, pcm->device,
983 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
984 snd_pcm_timer_init(substream);
987 list_for_each_entry(notify, &snd_pcm_notify_list, list)
988 notify->n_register(pcm);
990 mutex_unlock(®ister_mutex);
994 static int snd_pcm_dev_disconnect(struct snd_device *device)
996 struct snd_pcm *pcm = device->device_data;
997 struct snd_pcm_notify *notify;
998 struct snd_pcm_substream *substream;
1001 mutex_lock(®ister_mutex);
1002 if (list_empty(&pcm->list))
1005 list_del_init(&pcm->list);
1006 for (cidx = 0; cidx < 2; cidx++)
1007 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1008 if (substream->runtime)
1009 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
1010 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
1011 notify->n_disconnect(pcm);
1013 for (cidx = 0; cidx < 2; cidx++) {
1016 case SNDRV_PCM_STREAM_PLAYBACK:
1017 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1019 case SNDRV_PCM_STREAM_CAPTURE:
1020 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1023 snd_unregister_device(devtype, pcm->card, pcm->device);
1026 mutex_unlock(®ister_mutex);
1030 int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
1032 struct snd_pcm *pcm;
1034 snd_assert(notify != NULL &&
1035 notify->n_register != NULL &&
1036 notify->n_unregister != NULL &&
1037 notify->n_disconnect, return -EINVAL);
1038 mutex_lock(®ister_mutex);
1040 list_del(¬ify->list);
1041 list_for_each_entry(pcm, &snd_pcm_devices, list)
1042 notify->n_unregister(pcm);
1044 list_add_tail(¬ify->list, &snd_pcm_notify_list);
1045 list_for_each_entry(pcm, &snd_pcm_devices, list)
1046 notify->n_register(pcm);
1048 mutex_unlock(®ister_mutex);
1052 EXPORT_SYMBOL(snd_pcm_notify);
1054 #ifdef CONFIG_PROC_FS
1059 static void snd_pcm_proc_read(struct snd_info_entry *entry,
1060 struct snd_info_buffer *buffer)
1062 struct snd_pcm *pcm;
1064 mutex_lock(®ister_mutex);
1065 list_for_each_entry(pcm, &snd_pcm_devices, list) {
1066 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1067 pcm->card->number, pcm->device, pcm->id, pcm->name);
1068 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
1069 snd_iprintf(buffer, " : playback %i",
1070 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
1071 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
1072 snd_iprintf(buffer, " : capture %i",
1073 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
1074 snd_iprintf(buffer, "\n");
1076 mutex_unlock(®ister_mutex);
1079 static struct snd_info_entry *snd_pcm_proc_entry;
1081 static void snd_pcm_proc_init(void)
1083 struct snd_info_entry *entry;
1085 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
1086 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
1087 if (snd_info_register(entry) < 0) {
1088 snd_info_free_entry(entry);
1092 snd_pcm_proc_entry = entry;
1095 static void snd_pcm_proc_done(void)
1097 snd_info_free_entry(snd_pcm_proc_entry);
1100 #else /* !CONFIG_PROC_FS */
1101 #define snd_pcm_proc_init()
1102 #define snd_pcm_proc_done()
1103 #endif /* CONFIG_PROC_FS */
1110 static int __init alsa_pcm_init(void)
1112 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1113 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1114 snd_pcm_proc_init();
1118 static void __exit alsa_pcm_exit(void)
1120 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1121 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1122 snd_pcm_proc_done();
1125 module_init(alsa_pcm_init)
1126 module_exit(alsa_pcm_exit)