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 <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>
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");
36 static LIST_HEAD(snd_pcm_devices);
37 static LIST_HEAD(snd_pcm_notify_list);
38 static DEFINE_MUTEX(register_mutex);
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);
45 static struct snd_pcm *snd_pcm_search(struct snd_card *card, int device)
49 list_for_each_entry(pcm, &snd_pcm_devices, list) {
50 if (pcm->card == card && pcm->device == device)
56 static int snd_pcm_control_ioctl(struct snd_card *card,
57 struct snd_ctl_file *control,
58 unsigned int cmd, unsigned long arg)
61 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
65 if (get_user(device, (int __user *)arg))
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))
74 if (device == SNDRV_PCM_DEVICES)
76 mutex_unlock(®ister_mutex);
77 if (put_user(device, (int __user *)arg))
81 case SNDRV_CTL_IOCTL_PCM_INFO:
83 struct snd_pcm_info __user *info;
84 unsigned int device, subdevice;
87 struct snd_pcm_str *pstr;
88 struct snd_pcm_substream *substream;
91 info = (struct snd_pcm_info __user *)arg;
92 if (get_user(device, &info->device))
94 if (get_user(stream, &info->stream))
96 if (stream < 0 || stream > 1)
98 if (get_user(subdevice, &info->subdevice))
100 mutex_lock(®ister_mutex);
101 pcm = snd_pcm_search(card, device);
106 pstr = &pcm->streams[stream];
107 if (pstr->substream_count == 0) {
111 if (subdevice >= pstr->substream_count) {
115 for (substream = pstr->substream; substream;
116 substream = substream->next)
117 if (substream->number == (int)subdevice)
119 if (substream == NULL) {
123 err = snd_pcm_info_user(substream, info);
125 mutex_unlock(®ister_mutex);
128 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
132 if (get_user(val, (int __user *)arg))
134 control->prefer_pcm_subdevice = val;
141 #ifdef CONFIG_SND_VERBOSE_PROCFS
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
154 static char *snd_pcm_format_names[] = {
173 FORMAT(IEC958_SUBFRAME_LE),
174 FORMAT(IEC958_SUBFRAME_BE),
195 static const char *snd_pcm_format_name(snd_pcm_format_t format)
197 return snd_pcm_format_names[format];
200 static char *snd_pcm_stream_names[] = {
205 static char *snd_pcm_state_names[] = {
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),
224 static char *snd_pcm_subformat_names[] = {
228 static char *snd_pcm_tstamp_mode_names[] = {
233 static const char *snd_pcm_stream_name(int stream)
235 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
236 return snd_pcm_stream_names[stream];
239 static const char *snd_pcm_access_name(snd_pcm_access_t access)
241 return snd_pcm_access_names[access];
244 static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
246 return snd_pcm_subformat_names[subformat];
249 static const char *snd_pcm_tstamp_mode_name(int mode)
251 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
252 return snd_pcm_tstamp_mode_names[mode];
255 static const char *snd_pcm_state_name(snd_pcm_state_t state)
257 return snd_pcm_state_names[state];
260 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
261 #include <linux/soundcard.h>
263 static const char *snd_pcm_oss_format_name(int format)
292 static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
293 struct snd_info_buffer *buffer)
295 struct snd_pcm_info *info;
301 info = kmalloc(sizeof(*info), GFP_KERNEL);
303 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
307 err = snd_pcm_info(substream, info);
309 snd_iprintf(buffer, "error %d\n", err);
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);
327 static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
328 struct snd_info_buffer *buffer)
330 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
334 static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
335 struct snd_info_buffer *buffer)
337 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data,
341 static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
342 struct snd_info_buffer *buffer)
344 struct snd_pcm_substream *substream = entry->private_data;
345 struct snd_pcm_runtime *runtime = substream->runtime;
347 snd_iprintf(buffer, "closed\n");
350 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
351 snd_iprintf(buffer, "no setup\n");
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);
373 static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
374 struct snd_info_buffer *buffer)
376 struct snd_pcm_substream *substream = entry->private_data;
377 struct snd_pcm_runtime *runtime = substream->runtime;
379 snd_iprintf(buffer, "closed\n");
382 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
383 snd_iprintf(buffer, "no setup\n");
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);
396 static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
397 struct snd_info_buffer *buffer)
399 struct snd_pcm_substream *substream = entry->private_data;
400 struct snd_pcm_runtime *runtime = substream->runtime;
401 struct snd_pcm_status status;
404 snd_iprintf(buffer, "closed\n");
407 memset(&status, 0, sizeof(status));
408 err = snd_pcm_status(substream, &status);
410 snd_iprintf(buffer, "error %d\n", err);
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);
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)
430 struct snd_pcm_str *pstr = entry->private_data;
431 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
434 static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
435 struct snd_info_buffer *buffer)
437 struct snd_pcm_str *pstr = entry->private_data;
439 if (!snd_info_get_line(buffer, line, sizeof(line)))
440 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
444 static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
446 struct snd_pcm *pcm = pstr->pcm;
447 struct snd_info_entry *entry;
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)
454 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
455 if (snd_info_register(entry) < 0) {
456 snd_info_free_entry(entry);
459 pstr->proc_root = entry;
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);
468 pstr->proc_info_entry = entry;
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);
482 pstr->proc_xrun_debug_entry = entry;
487 static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
489 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
490 snd_info_free_entry(pstr->proc_xrun_debug_entry);
491 pstr->proc_xrun_debug_entry = NULL;
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;
500 static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
502 struct snd_info_entry *entry;
503 struct snd_card *card;
506 card = substream->pcm->card;
508 sprintf(name, "sub%i", substream->number);
509 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
511 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
512 if (snd_info_register(entry) < 0) {
513 snd_info_free_entry(entry);
516 substream->proc_root = entry;
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);
526 substream->proc_info_entry = entry;
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);
536 substream->proc_hw_params_entry = entry;
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);
546 substream->proc_sw_params_entry = entry;
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);
556 substream->proc_status_entry = entry;
561 static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
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;
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 */
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
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
593 * Returns zero if successful, or a negative error code on failure.
595 int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
598 struct snd_pcm_str *pstr = &pcm->streams[stream];
599 struct snd_pcm_substream *substream, *prev;
601 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
602 mutex_init(&pstr->oss.setup_mutex);
604 pstr->stream = stream;
606 pstr->substream_count = substream_count;
607 if (substream_count > 0) {
608 err = snd_pcm_stream_proc_init(pstr);
610 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
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");
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;
631 pstr->substream = substream;
633 prev->next = substream;
634 err = snd_pcm_substream_proc_init(substream);
636 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
638 pstr->substream = NULL;
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);
655 EXPORT_SYMBOL(snd_pcm_new_stream);
658 * snd_pcm_new - create a new PCM instance
659 * @card: the card instance
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
666 * Creates a new PCM instance.
668 * The pcm operators have to be set afterwards to the new instance
669 * via snd_pcm_set_ops().
671 * Returns zero if successful, or a negative error code on failure.
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)
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,
685 snd_assert(rpcm != NULL, return -EINVAL);
687 snd_assert(card != NULL, return -ENXIO);
688 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
690 snd_printk(KERN_ERR "Cannot allocate PCM\n");
694 pcm->device = device;
696 strlcpy(pcm->id, id, sizeof(pcm->id));
697 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
701 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
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) {
715 EXPORT_SYMBOL(snd_pcm_new);
717 static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
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;
723 substream = pstr->substream;
725 substream_next = substream->next;
726 snd_pcm_timer_done(substream);
727 snd_pcm_substream_proc_done(substream);
729 substream = substream_next;
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);
741 static int snd_pcm_free(struct snd_pcm *pcm)
743 struct snd_pcm_notify *notify;
745 snd_assert(pcm != NULL, return -ENXIO);
746 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
747 notify->n_unregister(pcm);
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]);
758 static int snd_pcm_dev_free(struct snd_device *device)
760 struct snd_pcm *pcm = device->device_data;
761 return snd_pcm_free(pcm);
764 int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
766 struct snd_pcm_substream **rsubstream)
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;
776 snd_assert(rsubstream != NULL, return -EINVAL);
778 snd_assert(pcm != NULL, return -ENXIO);
779 pstr = &pcm->streams[stream];
780 if (pstr->substream == NULL || pstr->substream_count == 0)
784 read_lock(&card->ctl_files_rwlock);
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)
792 read_unlock(&card->ctl_files_rwlock);
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))
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))
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;
821 for (substream = pstr->substream; substream;
822 substream = substream->next)
823 if (substream->number == prefer_subdevice)
828 if (! SUBSTREAM_BUSY(substream))
830 substream->ref_count++;
831 *rsubstream = substream;
835 if (prefer_subdevice >= 0) {
836 for (substream = pstr->substream; substream; substream = substream->next)
837 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
840 for (substream = pstr->substream; substream; substream = substream->next)
841 if (!SUBSTREAM_BUSY(substream))
844 if (substream == NULL)
847 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
851 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
852 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
853 if (runtime->status == NULL) {
857 memset((void*)runtime->status, 0, size);
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)));
867 memset((void*)runtime->control, 0, size);
869 init_waitqueue_head(&runtime->sleep);
871 runtime->status->state = SNDRV_PCM_STATE_OPEN;
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;
882 void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
884 struct snd_pcm_runtime *runtime;
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);
896 substream->runtime = NULL;
897 substream->pstr->substream_opened--;
900 static ssize_t show_pcm_class(struct device *dev,
901 struct device_attribute *attr, char *buf)
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",
912 if (! (pcm = dev_get_drvdata(dev)) ||
913 pcm->dev_class > SNDRV_PCM_CLASS_LAST)
916 str = strs[pcm->dev_class];
917 return snprintf(buf, PAGE_SIZE, "%s\n", str);
920 static struct device_attribute pcm_attrs =
921 __ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
923 static int snd_pcm_dev_register(struct snd_device *device)
926 struct snd_pcm_substream *substream;
927 struct snd_pcm_notify *notify;
929 struct snd_pcm *pcm = device->device_data;
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);
938 list_add_tail(&pcm->list, &snd_pcm_devices);
939 for (cidx = 0; cidx < 2; cidx++) {
941 if (pcm->streams[cidx].substream == NULL)
944 case SNDRV_PCM_STREAM_PLAYBACK:
945 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
946 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
948 case SNDRV_PCM_STREAM_CAPTURE:
949 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
950 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
953 /* device pointer to use, pcm->dev takes precedence if
954 * it is assigned, otherwise fall back to card's device
958 dev = snd_card_get_device_link(pcm->card);
960 err = snd_register_device_for_dev(devtype, pcm->card,
962 &snd_pcm_f_ops[cidx],
965 list_del(&pcm->list);
966 mutex_unlock(®ister_mutex);
969 snd_add_device_sysfs_file(devtype, pcm->card, pcm->device,
971 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
972 snd_pcm_timer_init(substream);
975 list_for_each_entry(notify, &snd_pcm_notify_list, list)
976 notify->n_register(pcm);
978 mutex_unlock(®ister_mutex);
982 static int snd_pcm_dev_disconnect(struct snd_device *device)
984 struct snd_pcm *pcm = device->device_data;
985 struct snd_pcm_notify *notify;
986 struct snd_pcm_substream *substream;
989 mutex_lock(®ister_mutex);
990 if (list_empty(&pcm->list))
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);
1001 for (cidx = 0; cidx < 2; cidx++) {
1004 case SNDRV_PCM_STREAM_PLAYBACK:
1005 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1007 case SNDRV_PCM_STREAM_CAPTURE:
1008 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1011 snd_unregister_device(devtype, pcm->card, pcm->device);
1014 mutex_unlock(®ister_mutex);
1018 int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
1020 struct snd_pcm *pcm;
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);
1028 list_del(¬ify->list);
1029 list_for_each_entry(pcm, &snd_pcm_devices, list)
1030 notify->n_unregister(pcm);
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);
1036 mutex_unlock(®ister_mutex);
1040 EXPORT_SYMBOL(snd_pcm_notify);
1042 #ifdef CONFIG_PROC_FS
1047 static void snd_pcm_proc_read(struct snd_info_entry *entry,
1048 struct snd_info_buffer *buffer)
1050 struct snd_pcm *pcm;
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");
1064 mutex_unlock(®ister_mutex);
1067 static struct snd_info_entry *snd_pcm_proc_entry;
1069 static void snd_pcm_proc_init(void)
1071 struct snd_info_entry *entry;
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);
1080 snd_pcm_proc_entry = entry;
1083 static void snd_pcm_proc_done(void)
1085 snd_info_free_entry(snd_pcm_proc_entry);
1088 #else /* !CONFIG_PROC_FS */
1089 #define snd_pcm_proc_init()
1090 #define snd_pcm_proc_done()
1091 #endif /* CONFIG_PROC_FS */
1098 static int __init alsa_pcm_init(void)
1100 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1101 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1102 snd_pcm_proc_init();
1106 static void __exit alsa_pcm_exit(void)
1108 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1109 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1110 snd_pcm_proc_done();
1113 module_init(alsa_pcm_init)
1114 module_exit(alsa_pcm_exit)