2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <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@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
33 MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
34 MODULE_LICENSE("GPL");
36 struct snd_pcm *snd_pcm_devices[SNDRV_CARDS * SNDRV_PCM_DEVICES];
37 static LIST_HEAD(snd_pcm_notify_list);
38 static DECLARE_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);
44 static int snd_pcm_dev_unregister(struct snd_device *device);
46 static int snd_pcm_control_ioctl(struct snd_card *card,
47 struct snd_ctl_file *control,
48 unsigned int cmd, unsigned long arg)
52 tmp = card->number * SNDRV_PCM_DEVICES;
54 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
58 if (get_user(device, (int __user *)arg))
60 device = device < 0 ? 0 : device + 1;
61 while (device < SNDRV_PCM_DEVICES) {
62 if (snd_pcm_devices[tmp + device])
66 if (device == SNDRV_PCM_DEVICES)
68 if (put_user(device, (int __user *)arg))
72 case SNDRV_CTL_IOCTL_PCM_INFO:
74 struct snd_pcm_info __user *info;
75 unsigned int device, subdevice;
78 struct snd_pcm_str *pstr;
79 struct snd_pcm_substream *substream;
80 info = (struct snd_pcm_info __user *)arg;
81 if (get_user(device, &info->device))
83 if (device >= SNDRV_PCM_DEVICES)
85 pcm = snd_pcm_devices[tmp + device];
88 if (get_user(stream, &info->stream))
90 if (stream < 0 || stream > 1)
92 pstr = &pcm->streams[stream];
93 if (pstr->substream_count == 0)
95 if (get_user(subdevice, &info->subdevice))
97 if (subdevice >= pstr->substream_count)
99 for (substream = pstr->substream; substream; substream = substream->next)
100 if (substream->number == (int)subdevice)
102 if (substream == NULL)
104 return snd_pcm_info_user(substream, info);
106 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
110 if (get_user(val, (int __user *)arg))
112 control->prefer_pcm_subdevice = val;
118 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
119 #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
120 #define READY(v) [SNDRV_PCM_READY_##v] = #v
121 #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
122 #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
123 #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
124 #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
125 #define START(v) [SNDRV_PCM_START_##v] = #v
126 #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
127 #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
129 static char *snd_pcm_stream_names[] = {
134 static char *snd_pcm_state_names[] = {
145 static char *snd_pcm_access_names[] = {
146 ACCESS(MMAP_INTERLEAVED),
147 ACCESS(MMAP_NONINTERLEAVED),
148 ACCESS(MMAP_COMPLEX),
149 ACCESS(RW_INTERLEAVED),
150 ACCESS(RW_NONINTERLEAVED),
153 static char *snd_pcm_format_names[] = {
172 FORMAT(IEC958_SUBFRAME_LE),
173 FORMAT(IEC958_SUBFRAME_BE),
194 static char *snd_pcm_subformat_names[] = {
198 static char *snd_pcm_tstamp_mode_names[] = {
203 static const char *snd_pcm_stream_name(int stream)
205 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
206 return snd_pcm_stream_names[stream];
209 static const char *snd_pcm_access_name(snd_pcm_access_t access)
211 return snd_pcm_access_names[access];
214 const char *snd_pcm_format_name(snd_pcm_format_t format)
216 return snd_pcm_format_names[format];
219 static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
221 return snd_pcm_subformat_names[subformat];
224 static const char *snd_pcm_tstamp_mode_name(int mode)
226 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
227 return snd_pcm_tstamp_mode_names[mode];
230 static const char *snd_pcm_state_name(snd_pcm_state_t state)
232 return snd_pcm_state_names[state];
235 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
236 #include <linux/soundcard.h>
237 static const char *snd_pcm_oss_format_name(int format)
266 #ifdef CONFIG_PROC_FS
267 static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
268 struct snd_info_buffer *buffer)
270 struct snd_pcm_info *info;
276 info = kmalloc(sizeof(*info), GFP_KERNEL);
278 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
282 err = snd_pcm_info(substream, info);
284 snd_iprintf(buffer, "error %d\n", err);
288 snd_iprintf(buffer, "card: %d\n", info->card);
289 snd_iprintf(buffer, "device: %d\n", info->device);
290 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
291 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
292 snd_iprintf(buffer, "id: %s\n", info->id);
293 snd_iprintf(buffer, "name: %s\n", info->name);
294 snd_iprintf(buffer, "subname: %s\n", info->subname);
295 snd_iprintf(buffer, "class: %d\n", info->dev_class);
296 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
297 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
298 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
302 static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
303 struct snd_info_buffer *buffer)
305 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
309 static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
310 struct snd_info_buffer *buffer)
312 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data,
316 static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
317 struct snd_info_buffer *buffer)
319 struct snd_pcm_substream *substream = entry->private_data;
320 struct snd_pcm_runtime *runtime = substream->runtime;
322 snd_iprintf(buffer, "closed\n");
325 snd_pcm_stream_lock_irq(substream);
326 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
327 snd_iprintf(buffer, "no setup\n");
328 snd_pcm_stream_unlock_irq(substream);
331 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
332 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
333 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
334 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
335 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
336 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
337 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
338 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
339 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
340 if (substream->oss.oss) {
341 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
342 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
343 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
344 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
345 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
346 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
349 snd_pcm_stream_unlock_irq(substream);
352 static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
353 struct snd_info_buffer *buffer)
355 struct snd_pcm_substream *substream = entry->private_data;
356 struct snd_pcm_runtime *runtime = substream->runtime;
358 snd_iprintf(buffer, "closed\n");
361 snd_pcm_stream_lock_irq(substream);
362 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
363 snd_iprintf(buffer, "no setup\n");
364 snd_pcm_stream_unlock_irq(substream);
367 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
368 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
369 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
370 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
371 snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
372 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
373 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
374 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
375 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
376 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
377 snd_pcm_stream_unlock_irq(substream);
380 static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
381 struct snd_info_buffer *buffer)
383 struct snd_pcm_substream *substream = entry->private_data;
384 struct snd_pcm_runtime *runtime = substream->runtime;
385 struct snd_pcm_status status;
388 snd_iprintf(buffer, "closed\n");
391 memset(&status, 0, sizeof(status));
392 err = snd_pcm_status(substream, &status);
394 snd_iprintf(buffer, "error %d\n", err);
397 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
398 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
399 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
400 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
401 status.tstamp.tv_sec, status.tstamp.tv_nsec);
402 snd_iprintf(buffer, "delay : %ld\n", status.delay);
403 snd_iprintf(buffer, "avail : %ld\n", status.avail);
404 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
405 snd_iprintf(buffer, "-----\n");
406 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
407 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
411 #ifdef CONFIG_SND_DEBUG
412 static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
413 struct snd_info_buffer *buffer)
415 struct snd_pcm_str *pstr = entry->private_data;
416 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
419 static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
420 struct snd_info_buffer *buffer)
422 struct snd_pcm_str *pstr = entry->private_data;
424 if (!snd_info_get_line(buffer, line, sizeof(line)))
425 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
429 static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
431 struct snd_pcm *pcm = pstr->pcm;
432 struct snd_info_entry *entry;
435 sprintf(name, "pcm%i%c", pcm->device,
436 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
437 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
439 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
440 if (snd_info_register(entry) < 0) {
441 snd_info_free_entry(entry);
444 pstr->proc_root = entry;
446 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
447 snd_info_set_text_ops(entry, pstr, 256, snd_pcm_stream_proc_info_read);
448 if (snd_info_register(entry) < 0) {
449 snd_info_free_entry(entry);
453 pstr->proc_info_entry = entry;
455 #ifdef CONFIG_SND_DEBUG
456 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
457 pstr->proc_root)) != NULL) {
458 entry->c.text.read_size = 64;
459 entry->c.text.read = snd_pcm_xrun_debug_read;
460 entry->c.text.write_size = 64;
461 entry->c.text.write = snd_pcm_xrun_debug_write;
462 entry->mode |= S_IWUSR;
463 entry->private_data = pstr;
464 if (snd_info_register(entry) < 0) {
465 snd_info_free_entry(entry);
469 pstr->proc_xrun_debug_entry = entry;
474 static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
476 #ifdef CONFIG_SND_DEBUG
477 if (pstr->proc_xrun_debug_entry) {
478 snd_info_unregister(pstr->proc_xrun_debug_entry);
479 pstr->proc_xrun_debug_entry = NULL;
482 if (pstr->proc_info_entry) {
483 snd_info_unregister(pstr->proc_info_entry);
484 pstr->proc_info_entry = NULL;
486 if (pstr->proc_root) {
487 snd_info_unregister(pstr->proc_root);
488 pstr->proc_root = NULL;
493 static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
495 struct snd_info_entry *entry;
496 struct snd_card *card;
499 card = substream->pcm->card;
501 sprintf(name, "sub%i", substream->number);
502 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
504 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
505 if (snd_info_register(entry) < 0) {
506 snd_info_free_entry(entry);
509 substream->proc_root = entry;
511 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
512 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_info_read);
513 if (snd_info_register(entry) < 0) {
514 snd_info_free_entry(entry);
518 substream->proc_info_entry = entry;
520 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
521 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_hw_params_read);
522 if (snd_info_register(entry) < 0) {
523 snd_info_free_entry(entry);
527 substream->proc_hw_params_entry = entry;
529 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
530 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_sw_params_read);
531 if (snd_info_register(entry) < 0) {
532 snd_info_free_entry(entry);
536 substream->proc_sw_params_entry = entry;
538 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
539 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_status_read);
540 if (snd_info_register(entry) < 0) {
541 snd_info_free_entry(entry);
545 substream->proc_status_entry = entry;
550 static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
552 if (substream->proc_info_entry) {
553 snd_info_unregister(substream->proc_info_entry);
554 substream->proc_info_entry = NULL;
556 if (substream->proc_hw_params_entry) {
557 snd_info_unregister(substream->proc_hw_params_entry);
558 substream->proc_hw_params_entry = NULL;
560 if (substream->proc_sw_params_entry) {
561 snd_info_unregister(substream->proc_sw_params_entry);
562 substream->proc_sw_params_entry = NULL;
564 if (substream->proc_status_entry) {
565 snd_info_unregister(substream->proc_status_entry);
566 substream->proc_status_entry = NULL;
568 if (substream->proc_root) {
569 snd_info_unregister(substream->proc_root);
570 substream->proc_root = NULL;
576 * snd_pcm_new_stream - create a new PCM stream
577 * @pcm: the pcm instance
578 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
579 * @substream_count: the number of substreams
581 * Creates a new stream for the pcm.
582 * The corresponding stream on the pcm must have been empty before
583 * calling this, i.e. zero must be given to the argument of
586 * Returns zero if successful, or a negative error code on failure.
588 int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
591 struct snd_pcm_str *pstr = &pcm->streams[stream];
592 struct snd_pcm_substream *substream, *prev;
594 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
595 init_MUTEX(&pstr->oss.setup_mutex);
597 pstr->stream = stream;
599 pstr->substream_count = substream_count;
600 pstr->reg = &snd_pcm_reg[stream];
601 if (substream_count > 0) {
602 err = snd_pcm_stream_proc_init(pstr);
604 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
609 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
610 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
611 if (substream == NULL) {
612 snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
615 substream->pcm = pcm;
616 substream->pstr = pstr;
617 substream->number = idx;
618 substream->stream = stream;
619 sprintf(substream->name, "subdevice #%i", idx);
620 substream->buffer_bytes_max = UINT_MAX;
622 pstr->substream = substream;
624 prev->next = substream;
625 err = snd_pcm_substream_proc_init(substream);
627 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
631 substream->group = &substream->self_group;
632 spin_lock_init(&substream->self_group.lock);
633 INIT_LIST_HEAD(&substream->self_group.substreams);
634 list_add_tail(&substream->link_list, &substream->self_group.substreams);
635 spin_lock_init(&substream->timer_lock);
642 * snd_pcm_new - create a new PCM instance
643 * @card: the card instance
645 * @device: the device index (zero based)
646 * @playback_count: the number of substreams for playback
647 * @capture_count: the number of substreams for capture
648 * @rpcm: the pointer to store the new pcm instance
650 * Creates a new PCM instance.
652 * The pcm operators have to be set afterwards to the new instance
653 * via snd_pcm_set_ops().
655 * Returns zero if successful, or a negative error code on failure.
657 int snd_pcm_new(struct snd_card *card, char *id, int device,
658 int playback_count, int capture_count,
659 struct snd_pcm ** rpcm)
663 static struct snd_device_ops ops = {
664 .dev_free = snd_pcm_dev_free,
665 .dev_register = snd_pcm_dev_register,
666 .dev_disconnect = snd_pcm_dev_disconnect,
667 .dev_unregister = snd_pcm_dev_unregister
670 snd_assert(rpcm != NULL, return -EINVAL);
672 snd_assert(card != NULL, return -ENXIO);
673 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
675 snd_printk(KERN_ERR "Cannot allocate PCM\n");
679 pcm->device = device;
681 strlcpy(pcm->id, id, sizeof(pcm->id));
682 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
686 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
690 init_MUTEX(&pcm->open_mutex);
691 init_waitqueue_head(&pcm->open_wait);
692 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
700 static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
702 struct snd_pcm_substream *substream, *substream_next;
703 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
704 struct snd_pcm_oss_setup *setup, *setupn;
706 substream = pstr->substream;
708 substream_next = substream->next;
709 snd_pcm_substream_proc_done(substream);
711 substream = substream_next;
713 snd_pcm_stream_proc_done(pstr);
714 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
715 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
716 setupn = setup->next;
717 kfree(setup->task_name);
723 static int snd_pcm_free(struct snd_pcm *pcm)
725 snd_assert(pcm != NULL, return -ENXIO);
726 if (pcm->private_free)
727 pcm->private_free(pcm);
728 snd_pcm_lib_preallocate_free_for_all(pcm);
729 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
730 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
735 static int snd_pcm_dev_free(struct snd_device *device)
737 struct snd_pcm *pcm = device->device_data;
738 return snd_pcm_free(pcm);
741 static void snd_pcm_tick_timer_func(unsigned long data)
743 struct snd_pcm_substream *substream = (struct snd_pcm_substream *) data;
744 snd_pcm_tick_elapsed(substream);
747 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
748 struct snd_pcm_substream **rsubstream)
750 struct snd_pcm_str * pstr;
751 struct snd_pcm_substream *substream;
752 struct snd_pcm_runtime *runtime;
753 struct snd_ctl_file *kctl;
754 struct snd_card *card;
755 struct list_head *list;
756 int prefer_subdevice = -1;
759 snd_assert(rsubstream != NULL, return -EINVAL);
761 snd_assert(pcm != NULL, return -ENXIO);
762 pstr = &pcm->streams[stream];
763 if (pstr->substream == NULL)
767 down_read(&card->controls_rwsem);
768 list_for_each(list, &card->ctl_files) {
769 kctl = snd_ctl_file(list);
770 if (kctl->pid == current->pid) {
771 prefer_subdevice = kctl->prefer_pcm_subdevice;
775 up_read(&card->controls_rwsem);
777 if (pstr->substream_count == 0)
780 case SNDRV_PCM_STREAM_PLAYBACK:
781 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
782 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
783 if (SUBSTREAM_BUSY(substream))
788 case SNDRV_PCM_STREAM_CAPTURE:
789 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
790 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
791 if (SUBSTREAM_BUSY(substream))
800 if (prefer_subdevice >= 0) {
801 for (substream = pstr->substream; substream; substream = substream->next)
802 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
805 for (substream = pstr->substream; substream; substream = substream->next)
806 if (!SUBSTREAM_BUSY(substream))
809 if (substream == NULL)
812 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
816 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
817 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
818 if (runtime->status == NULL) {
822 memset((void*)runtime->status, 0, size);
824 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
825 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
826 if (runtime->control == NULL) {
827 snd_free_pages((void*)runtime->status,
828 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
832 memset((void*)runtime->control, 0, size);
834 init_waitqueue_head(&runtime->sleep);
835 atomic_set(&runtime->mmap_count, 0);
836 init_timer(&runtime->tick_timer);
837 runtime->tick_timer.function = snd_pcm_tick_timer_func;
838 runtime->tick_timer.data = (unsigned long) substream;
840 runtime->status->state = SNDRV_PCM_STATE_OPEN;
842 substream->runtime = runtime;
843 substream->private_data = pcm->private_data;
844 pstr->substream_opened++;
845 *rsubstream = substream;
849 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
851 struct snd_pcm_runtime *runtime;
852 substream->file = NULL;
853 runtime = substream->runtime;
854 snd_assert(runtime != NULL, return);
855 if (runtime->private_free != NULL)
856 runtime->private_free(runtime);
857 snd_free_pages((void*)runtime->status,
858 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
859 snd_free_pages((void*)runtime->control,
860 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
861 kfree(runtime->hw_constraints.rules);
863 substream->runtime = NULL;
864 substream->pstr->substream_opened--;
867 static int snd_pcm_dev_register(struct snd_device *device)
870 unsigned short minor;
871 struct snd_pcm_substream *substream;
872 struct list_head *list;
874 struct snd_pcm *pcm = device->device_data;
876 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
877 down(®ister_mutex);
878 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
879 if (snd_pcm_devices[idx]) {
883 snd_pcm_devices[idx] = pcm;
884 for (cidx = 0; cidx < 2; cidx++) {
886 if (pcm->streams[cidx].substream == NULL)
889 case SNDRV_PCM_STREAM_PLAYBACK:
890 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
891 minor = SNDRV_MINOR_PCM_PLAYBACK + idx;
892 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
894 case SNDRV_PCM_STREAM_CAPTURE:
895 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
896 minor = SNDRV_MINOR_PCM_CAPTURE + idx;
897 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
900 if ((err = snd_register_device(devtype, pcm->card, pcm->device, pcm->streams[cidx].reg, str)) < 0) {
901 snd_pcm_devices[idx] = NULL;
905 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
906 snd_pcm_timer_init(substream);
908 list_for_each(list, &snd_pcm_notify_list) {
909 struct snd_pcm_notify *notify;
910 notify = list_entry(list, struct snd_pcm_notify, list);
911 notify->n_register(pcm);
917 static int snd_pcm_dev_disconnect(struct snd_device *device)
919 struct snd_pcm *pcm = device->device_data;
920 struct list_head *list;
921 struct snd_pcm_substream *substream;
924 down(®ister_mutex);
925 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
926 snd_pcm_devices[idx] = NULL;
927 for (cidx = 0; cidx < 2; cidx++)
928 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
929 if (substream->runtime)
930 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
931 list_for_each(list, &snd_pcm_notify_list) {
932 struct snd_pcm_notify *notify;
933 notify = list_entry(list, struct snd_pcm_notify, list);
934 notify->n_disconnect(pcm);
940 static int snd_pcm_dev_unregister(struct snd_device *device)
942 int idx, cidx, devtype;
943 struct snd_pcm_substream *substream;
944 struct list_head *list;
945 struct snd_pcm *pcm = device->device_data;
947 snd_assert(pcm != NULL, return -ENXIO);
948 down(®ister_mutex);
949 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
950 snd_pcm_devices[idx] = NULL;
951 for (cidx = 0; cidx < 2; cidx++) {
954 case SNDRV_PCM_STREAM_PLAYBACK:
955 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
957 case SNDRV_PCM_STREAM_CAPTURE:
958 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
961 snd_unregister_device(devtype, pcm->card, pcm->device);
962 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
963 snd_pcm_timer_done(substream);
965 list_for_each(list, &snd_pcm_notify_list) {
966 struct snd_pcm_notify *notify;
967 notify = list_entry(list, struct snd_pcm_notify, list);
968 notify->n_unregister(pcm);
971 return snd_pcm_free(pcm);
974 int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
978 snd_assert(notify != NULL && notify->n_register != NULL && notify->n_unregister != NULL, return -EINVAL);
979 down(®ister_mutex);
981 list_del(¬ify->list);
982 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
983 if (snd_pcm_devices[idx] == NULL)
985 notify->n_unregister(snd_pcm_devices[idx]);
988 list_add_tail(¬ify->list, &snd_pcm_notify_list);
989 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
990 if (snd_pcm_devices[idx] == NULL)
992 notify->n_register(snd_pcm_devices[idx]);
1003 static void snd_pcm_proc_read(struct snd_info_entry *entry,
1004 struct snd_info_buffer *buffer)
1007 struct snd_pcm *pcm;
1009 down(®ister_mutex);
1010 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
1011 pcm = snd_pcm_devices[idx];
1014 snd_iprintf(buffer, "%02i-%02i: %s : %s", idx / SNDRV_PCM_DEVICES,
1015 idx % SNDRV_PCM_DEVICES, pcm->id, pcm->name);
1016 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
1017 snd_iprintf(buffer, " : playback %i",
1018 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
1019 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
1020 snd_iprintf(buffer, " : capture %i",
1021 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
1022 snd_iprintf(buffer, "\n");
1024 up(®ister_mutex);
1031 static struct snd_info_entry *snd_pcm_proc_entry = NULL;
1033 static int __init alsa_pcm_init(void)
1035 struct snd_info_entry *entry;
1037 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1038 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1039 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
1040 snd_info_set_text_ops(entry, NULL, SNDRV_CARDS * SNDRV_PCM_DEVICES * 128,
1042 if (snd_info_register(entry) < 0) {
1043 snd_info_free_entry(entry);
1047 snd_pcm_proc_entry = entry;
1051 static void __exit alsa_pcm_exit(void)
1053 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1054 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1055 if (snd_pcm_proc_entry) {
1056 snd_info_unregister(snd_pcm_proc_entry);
1057 snd_pcm_proc_entry = NULL;
1061 module_init(alsa_pcm_init)
1062 module_exit(alsa_pcm_exit)
1064 EXPORT_SYMBOL(snd_pcm_devices);
1065 EXPORT_SYMBOL(snd_pcm_new);
1066 EXPORT_SYMBOL(snd_pcm_new_stream);
1067 EXPORT_SYMBOL(snd_pcm_notify);
1068 EXPORT_SYMBOL(snd_pcm_open_substream);
1069 EXPORT_SYMBOL(snd_pcm_release_substream);
1070 EXPORT_SYMBOL(snd_pcm_format_name);
1072 EXPORT_SYMBOL(snd_pcm_link_rwlock);
1074 EXPORT_SYMBOL(snd_pcm_suspend);
1075 EXPORT_SYMBOL(snd_pcm_suspend_all);
1077 EXPORT_SYMBOL(snd_pcm_kernel_playback_ioctl);
1078 EXPORT_SYMBOL(snd_pcm_kernel_capture_ioctl);
1079 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
1080 EXPORT_SYMBOL(snd_pcm_mmap_data);
1081 #if SNDRV_PCM_INFO_MMAP_IOMEM
1082 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
1085 EXPORT_SYMBOL(snd_pcm_format_signed);
1086 EXPORT_SYMBOL(snd_pcm_format_unsigned);
1087 EXPORT_SYMBOL(snd_pcm_format_linear);
1088 EXPORT_SYMBOL(snd_pcm_format_little_endian);
1089 EXPORT_SYMBOL(snd_pcm_format_big_endian);
1090 EXPORT_SYMBOL(snd_pcm_format_width);
1091 EXPORT_SYMBOL(snd_pcm_format_physical_width);
1092 EXPORT_SYMBOL(snd_pcm_format_size);
1093 EXPORT_SYMBOL(snd_pcm_format_silence_64);
1094 EXPORT_SYMBOL(snd_pcm_format_set_silence);
1095 EXPORT_SYMBOL(snd_pcm_build_linear_format);
1096 EXPORT_SYMBOL(snd_pcm_limit_hw_rates);