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 snd_pcm_t *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(snd_pcm_t *pcm);
41 static int snd_pcm_dev_free(snd_device_t *device);
42 static int snd_pcm_dev_register(snd_device_t *device);
43 static int snd_pcm_dev_disconnect(snd_device_t *device);
44 static int snd_pcm_dev_unregister(snd_device_t *device);
46 static int snd_pcm_control_ioctl(snd_card_t * card,
47 snd_ctl_file_t * 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 snd_pcm_info_t __user *info;
75 unsigned int device, subdevice;
76 snd_pcm_stream_t stream;
79 snd_pcm_substream_t *substream;
80 info = (snd_pcm_info_t __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 char *snd_pcm_stream_names[] = {
134 char *snd_pcm_state_names[] = {
145 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 char *snd_pcm_format_names[] = {
172 FORMAT(IEC958_SUBFRAME_LE),
173 FORMAT(IEC958_SUBFRAME_BE),
194 char *snd_pcm_subformat_names[] = {
198 char *snd_pcm_tstamp_mode_names[] = {
203 const char *snd_pcm_stream_name(snd_pcm_stream_t stream)
205 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
206 return snd_pcm_stream_names[stream];
209 const char *snd_pcm_access_name(snd_pcm_access_t access)
211 snd_assert(access <= SNDRV_PCM_ACCESS_LAST, return NULL);
212 return snd_pcm_access_names[access];
215 const char *snd_pcm_format_name(snd_pcm_format_t format)
217 snd_assert(format <= SNDRV_PCM_FORMAT_LAST, return NULL);
218 return snd_pcm_format_names[format];
221 const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
223 snd_assert(subformat <= SNDRV_PCM_SUBFORMAT_LAST, return NULL);
224 return snd_pcm_subformat_names[subformat];
227 const char *snd_pcm_tstamp_mode_name(snd_pcm_tstamp_t mode)
229 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
230 return snd_pcm_tstamp_mode_names[mode];
233 const char *snd_pcm_state_name(snd_pcm_state_t state)
235 snd_assert(state <= SNDRV_PCM_STATE_LAST, return NULL);
236 return snd_pcm_state_names[state];
239 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
240 #include <linux/soundcard.h>
241 const char *snd_pcm_oss_format_name(int format)
271 static void snd_pcm_proc_info_read(snd_pcm_substream_t *substream, snd_info_buffer_t *buffer)
275 snd_runtime_check(substream, return);
276 err = snd_pcm_info(substream, &info);
278 snd_iprintf(buffer, "error %d\n", err);
281 snd_iprintf(buffer, "card: %d\n", info.card);
282 snd_iprintf(buffer, "device: %d\n", info.device);
283 snd_iprintf(buffer, "subdevice: %d\n", info.subdevice);
284 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info.stream));
285 snd_iprintf(buffer, "id: %s\n", info.id);
286 snd_iprintf(buffer, "name: %s\n", info.name);
287 snd_iprintf(buffer, "subname: %s\n", info.subname);
288 snd_iprintf(buffer, "class: %d\n", info.dev_class);
289 snd_iprintf(buffer, "subclass: %d\n", info.dev_subclass);
290 snd_iprintf(buffer, "subdevices_count: %d\n", info.subdevices_count);
291 snd_iprintf(buffer, "subdevices_avail: %d\n", info.subdevices_avail);
294 static void snd_pcm_stream_proc_info_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
296 snd_pcm_proc_info_read(((snd_pcm_str_t *)entry->private_data)->substream, buffer);
299 static void snd_pcm_substream_proc_info_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
301 snd_pcm_proc_info_read((snd_pcm_substream_t *)entry->private_data, buffer);
304 static void snd_pcm_substream_proc_hw_params_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
306 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
307 snd_pcm_runtime_t *runtime = substream->runtime;
309 snd_iprintf(buffer, "closed\n");
312 snd_pcm_stream_lock_irq(substream);
313 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
314 snd_iprintf(buffer, "no setup\n");
315 snd_pcm_stream_unlock_irq(substream);
318 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
319 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
320 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
321 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
322 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
323 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
324 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
325 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
326 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
327 if (substream->oss.oss) {
328 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
329 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
330 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
331 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
332 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
333 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
336 snd_pcm_stream_unlock_irq(substream);
339 static void snd_pcm_substream_proc_sw_params_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
341 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
342 snd_pcm_runtime_t *runtime = substream->runtime;
344 snd_iprintf(buffer, "closed\n");
347 snd_pcm_stream_lock_irq(substream);
348 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
349 snd_iprintf(buffer, "no setup\n");
350 snd_pcm_stream_unlock_irq(substream);
353 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
354 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
355 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
356 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
357 snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
358 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
359 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
360 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
361 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
362 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
363 snd_pcm_stream_unlock_irq(substream);
366 static void snd_pcm_substream_proc_status_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
368 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
369 snd_pcm_runtime_t *runtime = substream->runtime;
370 snd_pcm_status_t status;
373 snd_iprintf(buffer, "closed\n");
376 memset(&status, 0, sizeof(status));
377 err = snd_pcm_status(substream, &status);
379 snd_iprintf(buffer, "error %d\n", err);
382 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
383 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
384 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
385 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
386 status.tstamp.tv_sec, status.tstamp.tv_nsec);
387 snd_iprintf(buffer, "delay : %ld\n", status.delay);
388 snd_iprintf(buffer, "avail : %ld\n", status.avail);
389 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
390 snd_iprintf(buffer, "-----\n");
391 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
392 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
395 #ifdef CONFIG_SND_DEBUG
396 static void snd_pcm_xrun_debug_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
398 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
399 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
402 static void snd_pcm_xrun_debug_write(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
404 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
406 if (!snd_info_get_line(buffer, line, sizeof(line)))
407 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
411 static int snd_pcm_stream_proc_init(snd_pcm_str_t *pstr)
413 snd_pcm_t *pcm = pstr->pcm;
414 snd_info_entry_t *entry;
417 sprintf(name, "pcm%i%c", pcm->device,
418 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
419 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
421 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
422 if (snd_info_register(entry) < 0) {
423 snd_info_free_entry(entry);
426 pstr->proc_root = entry;
428 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
429 snd_info_set_text_ops(entry, pstr, 256, snd_pcm_stream_proc_info_read);
430 if (snd_info_register(entry) < 0) {
431 snd_info_free_entry(entry);
435 pstr->proc_info_entry = entry;
437 #ifdef CONFIG_SND_DEBUG
438 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug", pstr->proc_root)) != NULL) {
439 entry->c.text.read_size = 64;
440 entry->c.text.read = snd_pcm_xrun_debug_read;
441 entry->c.text.write_size = 64;
442 entry->c.text.write = snd_pcm_xrun_debug_write;
443 entry->private_data = pstr;
444 if (snd_info_register(entry) < 0) {
445 snd_info_free_entry(entry);
449 pstr->proc_xrun_debug_entry = entry;
454 static int snd_pcm_stream_proc_done(snd_pcm_str_t *pstr)
456 #ifdef CONFIG_SND_DEBUG
457 if (pstr->proc_xrun_debug_entry) {
458 snd_info_unregister(pstr->proc_xrun_debug_entry);
459 pstr->proc_xrun_debug_entry = NULL;
462 if (pstr->proc_info_entry) {
463 snd_info_unregister(pstr->proc_info_entry);
464 pstr->proc_info_entry = NULL;
466 if (pstr->proc_root) {
467 snd_info_unregister(pstr->proc_root);
468 pstr->proc_root = NULL;
473 static int snd_pcm_substream_proc_init(snd_pcm_substream_t *substream)
475 snd_info_entry_t *entry;
479 card = substream->pcm->card;
481 sprintf(name, "sub%i", substream->number);
482 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
484 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
485 if (snd_info_register(entry) < 0) {
486 snd_info_free_entry(entry);
489 substream->proc_root = entry;
491 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
492 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_info_read);
493 if (snd_info_register(entry) < 0) {
494 snd_info_free_entry(entry);
498 substream->proc_info_entry = entry;
500 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
501 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_hw_params_read);
502 if (snd_info_register(entry) < 0) {
503 snd_info_free_entry(entry);
507 substream->proc_hw_params_entry = entry;
509 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
510 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_sw_params_read);
511 if (snd_info_register(entry) < 0) {
512 snd_info_free_entry(entry);
516 substream->proc_sw_params_entry = entry;
518 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
519 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_status_read);
520 if (snd_info_register(entry) < 0) {
521 snd_info_free_entry(entry);
525 substream->proc_status_entry = entry;
530 static int snd_pcm_substream_proc_done(snd_pcm_substream_t *substream)
532 if (substream->proc_info_entry) {
533 snd_info_unregister(substream->proc_info_entry);
534 substream->proc_info_entry = NULL;
536 if (substream->proc_hw_params_entry) {
537 snd_info_unregister(substream->proc_hw_params_entry);
538 substream->proc_hw_params_entry = NULL;
540 if (substream->proc_sw_params_entry) {
541 snd_info_unregister(substream->proc_sw_params_entry);
542 substream->proc_sw_params_entry = NULL;
544 if (substream->proc_status_entry) {
545 snd_info_unregister(substream->proc_status_entry);
546 substream->proc_status_entry = NULL;
548 if (substream->proc_root) {
549 snd_info_unregister(substream->proc_root);
550 substream->proc_root = NULL;
556 * snd_pcm_new_stream - create a new PCM stream
557 * @pcm: the pcm instance
558 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
559 * @substream_count: the number of substreams
561 * Creates a new stream for the pcm.
562 * The corresponding stream on the pcm must have been empty before
563 * calling this, i.e. zero must be given to the argument of
566 * Returns zero if successful, or a negative error code on failure.
568 int snd_pcm_new_stream(snd_pcm_t *pcm, int stream, int substream_count)
571 snd_pcm_str_t *pstr = &pcm->streams[stream];
572 snd_pcm_substream_t *substream, *prev;
574 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
575 init_MUTEX(&pstr->oss.setup_mutex);
577 pstr->stream = stream;
579 pstr->substream_count = substream_count;
580 pstr->reg = &snd_pcm_reg[stream];
581 if (substream_count > 0) {
582 err = snd_pcm_stream_proc_init(pstr);
587 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
588 substream = kcalloc(1, sizeof(*substream), GFP_KERNEL);
589 if (substream == NULL)
591 substream->pcm = pcm;
592 substream->pstr = pstr;
593 substream->number = idx;
594 substream->stream = stream;
595 sprintf(substream->name, "subdevice #%i", idx);
596 substream->buffer_bytes_max = UINT_MAX;
598 pstr->substream = substream;
600 prev->next = substream;
601 err = snd_pcm_substream_proc_init(substream);
606 substream->group = &substream->self_group;
607 spin_lock_init(&substream->self_group.lock);
608 INIT_LIST_HEAD(&substream->self_group.substreams);
609 list_add_tail(&substream->link_list, &substream->self_group.substreams);
610 spin_lock_init(&substream->timer_lock);
617 * snd_pcm_new - create a new PCM instance
618 * @card: the card instance
620 * @device: the device index (zero based)
621 * @playback_count: the number of substreams for playback
622 * @capture_count: the number of substreams for capture
623 * @rpcm: the pointer to store the new pcm instance
625 * Creates a new PCM instance.
627 * The pcm operators have to be set afterwards to the new instance
628 * via snd_pcm_set_ops().
630 * Returns zero if successful, or a negative error code on failure.
632 int snd_pcm_new(snd_card_t * card, char *id, int device,
633 int playback_count, int capture_count,
638 static snd_device_ops_t ops = {
639 .dev_free = snd_pcm_dev_free,
640 .dev_register = snd_pcm_dev_register,
641 .dev_disconnect = snd_pcm_dev_disconnect,
642 .dev_unregister = snd_pcm_dev_unregister
645 snd_assert(rpcm != NULL, return -EINVAL);
647 snd_assert(card != NULL, return -ENXIO);
648 pcm = kcalloc(1, sizeof(*pcm), GFP_KERNEL);
652 pcm->device = device;
654 strlcpy(pcm->id, id, sizeof(pcm->id));
656 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
660 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
664 init_MUTEX(&pcm->open_mutex);
665 init_waitqueue_head(&pcm->open_wait);
666 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
674 static void snd_pcm_free_stream(snd_pcm_str_t * pstr)
676 snd_pcm_substream_t *substream, *substream_next;
677 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
678 snd_pcm_oss_setup_t *setup, *setupn;
680 substream = pstr->substream;
682 substream_next = substream->next;
683 snd_pcm_substream_proc_done(substream);
685 substream = substream_next;
687 snd_pcm_stream_proc_done(pstr);
688 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
689 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
690 setupn = setup->next;
691 kfree(setup->task_name);
697 static int snd_pcm_free(snd_pcm_t *pcm)
699 snd_assert(pcm != NULL, return -ENXIO);
700 if (pcm->private_free)
701 pcm->private_free(pcm);
702 snd_pcm_lib_preallocate_free_for_all(pcm);
703 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
704 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
709 static int snd_pcm_dev_free(snd_device_t *device)
711 snd_pcm_t *pcm = device->device_data;
712 return snd_pcm_free(pcm);
715 static void snd_pcm_tick_timer_func(unsigned long data)
717 snd_pcm_substream_t *substream = (snd_pcm_substream_t*) data;
718 snd_pcm_tick_elapsed(substream);
721 int snd_pcm_open_substream(snd_pcm_t *pcm, int stream,
722 snd_pcm_substream_t **rsubstream)
724 snd_pcm_str_t * pstr;
725 snd_pcm_substream_t * substream;
726 snd_pcm_runtime_t * runtime;
727 snd_ctl_file_t *kctl;
729 struct list_head *list;
730 int prefer_subdevice = -1;
733 snd_assert(rsubstream != NULL, return -EINVAL);
735 snd_assert(pcm != NULL, return -ENXIO);
736 pstr = &pcm->streams[stream];
737 if (pstr->substream == NULL)
741 down_read(&card->controls_rwsem);
742 list_for_each(list, &card->ctl_files) {
743 kctl = snd_ctl_file(list);
744 if (kctl->pid == current->pid) {
745 prefer_subdevice = kctl->prefer_pcm_subdevice;
749 up_read(&card->controls_rwsem);
751 if (pstr->substream_count == 0)
754 case SNDRV_PCM_STREAM_PLAYBACK:
755 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
756 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
757 if (SUBSTREAM_BUSY(substream))
762 case SNDRV_PCM_STREAM_CAPTURE:
763 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
764 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
765 if (SUBSTREAM_BUSY(substream))
774 if (prefer_subdevice >= 0) {
775 for (substream = pstr->substream; substream; substream = substream->next)
776 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
779 for (substream = pstr->substream; substream; substream = substream->next)
780 if (!SUBSTREAM_BUSY(substream))
783 if (substream == NULL)
786 runtime = kcalloc(1, sizeof(*runtime), GFP_KERNEL);
790 size = PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t));
791 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
792 if (runtime->status == NULL) {
796 memset((void*)runtime->status, 0, size);
798 size = PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t));
799 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
800 if (runtime->control == NULL) {
801 snd_free_pages((void*)runtime->status, PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)));
805 memset((void*)runtime->control, 0, size);
807 init_waitqueue_head(&runtime->sleep);
808 atomic_set(&runtime->mmap_count, 0);
809 init_timer(&runtime->tick_timer);
810 runtime->tick_timer.function = snd_pcm_tick_timer_func;
811 runtime->tick_timer.data = (unsigned long) substream;
813 runtime->status->state = SNDRV_PCM_STATE_OPEN;
815 substream->runtime = runtime;
816 substream->private_data = pcm->private_data;
817 pstr->substream_opened++;
818 *rsubstream = substream;
822 void snd_pcm_release_substream(snd_pcm_substream_t *substream)
824 snd_pcm_runtime_t * runtime;
825 substream->file = NULL;
826 runtime = substream->runtime;
827 snd_assert(runtime != NULL, return);
828 if (runtime->private_free != NULL)
829 runtime->private_free(runtime);
830 snd_free_pages((void*)runtime->status, PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)));
831 snd_free_pages((void*)runtime->control, PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)));
832 if (runtime->hw_constraints.rules)
833 kfree(runtime->hw_constraints.rules);
835 substream->runtime = NULL;
836 substream->pstr->substream_opened--;
839 static int snd_pcm_dev_register(snd_device_t *device)
842 unsigned short minor;
843 snd_pcm_substream_t *substream;
844 struct list_head *list;
846 snd_pcm_t *pcm = device->device_data;
848 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
849 down(®ister_mutex);
850 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
851 if (snd_pcm_devices[idx]) {
855 snd_pcm_devices[idx] = pcm;
856 for (cidx = 0; cidx < 2; cidx++) {
858 if (pcm->streams[cidx].substream == NULL)
861 case SNDRV_PCM_STREAM_PLAYBACK:
862 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
863 minor = SNDRV_MINOR_PCM_PLAYBACK + idx;
864 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
866 case SNDRV_PCM_STREAM_CAPTURE:
867 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
868 minor = SNDRV_MINOR_PCM_CAPTURE + idx;
869 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
872 if ((err = snd_register_device(devtype, pcm->card, pcm->device, pcm->streams[cidx].reg, str)) < 0) {
873 snd_pcm_devices[idx] = NULL;
877 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
878 snd_pcm_timer_init(substream);
880 list_for_each(list, &snd_pcm_notify_list) {
881 snd_pcm_notify_t *notify;
882 notify = list_entry(list, snd_pcm_notify_t, list);
883 notify->n_register(pcm);
889 static int snd_pcm_dev_disconnect(snd_device_t *device)
891 snd_pcm_t *pcm = device->device_data;
892 struct list_head *list;
893 snd_pcm_substream_t *substream;
896 down(®ister_mutex);
897 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
898 snd_pcm_devices[idx] = NULL;
899 for (cidx = 0; cidx < 2; cidx++)
900 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
901 if (substream->runtime)
902 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
903 list_for_each(list, &snd_pcm_notify_list) {
904 snd_pcm_notify_t *notify;
905 notify = list_entry(list, snd_pcm_notify_t, list);
906 notify->n_disconnect(pcm);
912 static int snd_pcm_dev_unregister(snd_device_t *device)
914 int idx, cidx, devtype;
915 snd_pcm_substream_t *substream;
916 struct list_head *list;
917 snd_pcm_t *pcm = device->device_data;
919 snd_assert(pcm != NULL, return -ENXIO);
920 down(®ister_mutex);
921 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
922 snd_pcm_devices[idx] = NULL;
923 for (cidx = 0; cidx < 2; cidx++) {
926 case SNDRV_PCM_STREAM_PLAYBACK:
927 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
929 case SNDRV_PCM_STREAM_CAPTURE:
930 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
933 snd_unregister_device(devtype, pcm->card, pcm->device);
934 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
935 snd_pcm_timer_done(substream);
937 list_for_each(list, &snd_pcm_notify_list) {
938 snd_pcm_notify_t *notify;
939 notify = list_entry(list, snd_pcm_notify_t, list);
940 notify->n_unregister(pcm);
943 return snd_pcm_free(pcm);
946 int snd_pcm_notify(snd_pcm_notify_t *notify, int nfree)
950 snd_assert(notify != NULL && notify->n_register != NULL && notify->n_unregister != NULL, return -EINVAL);
951 down(®ister_mutex);
953 list_del(¬ify->list);
954 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
955 if (snd_pcm_devices[idx] == NULL)
957 notify->n_unregister(snd_pcm_devices[idx]);
960 list_add_tail(¬ify->list, &snd_pcm_notify_list);
961 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
962 if (snd_pcm_devices[idx] == NULL)
964 notify->n_register(snd_pcm_devices[idx]);
975 static void snd_pcm_proc_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
980 down(®ister_mutex);
981 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
982 pcm = snd_pcm_devices[idx];
985 snd_iprintf(buffer, "%02i-%02i: %s : %s", idx / SNDRV_PCM_DEVICES,
986 idx % SNDRV_PCM_DEVICES, pcm->id, pcm->name);
987 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
988 snd_iprintf(buffer, " : playback %i", pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
989 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
990 snd_iprintf(buffer, " : capture %i", pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
991 snd_iprintf(buffer, "\n");
1000 static snd_info_entry_t *snd_pcm_proc_entry = NULL;
1002 static int __init alsa_pcm_init(void)
1004 snd_info_entry_t *entry;
1006 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1007 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
1008 snd_info_set_text_ops(entry, NULL, SNDRV_CARDS * SNDRV_PCM_DEVICES * 128, snd_pcm_proc_read);
1009 if (snd_info_register(entry) < 0) {
1010 snd_info_free_entry(entry);
1014 snd_pcm_proc_entry = entry;
1018 static void __exit alsa_pcm_exit(void)
1020 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1021 if (snd_pcm_proc_entry) {
1022 snd_info_unregister(snd_pcm_proc_entry);
1023 snd_pcm_proc_entry = NULL;
1027 module_init(alsa_pcm_init)
1028 module_exit(alsa_pcm_exit)
1030 EXPORT_SYMBOL(snd_pcm_devices);
1031 EXPORT_SYMBOL(snd_pcm_new);
1032 EXPORT_SYMBOL(snd_pcm_new_stream);
1033 EXPORT_SYMBOL(snd_pcm_notify);
1034 EXPORT_SYMBOL(snd_pcm_open_substream);
1035 EXPORT_SYMBOL(snd_pcm_release_substream);
1036 EXPORT_SYMBOL(snd_pcm_format_name);
1037 EXPORT_SYMBOL(snd_pcm_subformat_name);
1039 EXPORT_SYMBOL(snd_pcm_link_rwlock);
1040 EXPORT_SYMBOL(snd_pcm_start);
1042 EXPORT_SYMBOL(snd_pcm_suspend);
1043 EXPORT_SYMBOL(snd_pcm_suspend_all);
1045 EXPORT_SYMBOL(snd_pcm_kernel_playback_ioctl);
1046 EXPORT_SYMBOL(snd_pcm_kernel_capture_ioctl);
1047 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
1048 EXPORT_SYMBOL(snd_pcm_open);
1049 EXPORT_SYMBOL(snd_pcm_release);
1050 EXPORT_SYMBOL(snd_pcm_playback_poll);
1051 EXPORT_SYMBOL(snd_pcm_capture_poll);
1052 EXPORT_SYMBOL(snd_pcm_mmap_data);
1053 #if SNDRV_PCM_INFO_MMAP_IOMEM
1054 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
1057 EXPORT_SYMBOL(snd_pcm_format_signed);
1058 EXPORT_SYMBOL(snd_pcm_format_unsigned);
1059 EXPORT_SYMBOL(snd_pcm_format_linear);
1060 EXPORT_SYMBOL(snd_pcm_format_little_endian);
1061 EXPORT_SYMBOL(snd_pcm_format_big_endian);
1062 EXPORT_SYMBOL(snd_pcm_format_width);
1063 EXPORT_SYMBOL(snd_pcm_format_physical_width);
1064 EXPORT_SYMBOL(snd_pcm_format_size);
1065 EXPORT_SYMBOL(snd_pcm_format_silence_64);
1066 EXPORT_SYMBOL(snd_pcm_format_set_silence);
1067 EXPORT_SYMBOL(snd_pcm_build_linear_format);
1068 EXPORT_SYMBOL(snd_pcm_limit_hw_rates);