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 *)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 *)arg))
72 case SNDRV_CTL_IOCTL_PCM_INFO:
74 snd_pcm_info_t *info = (snd_pcm_info_t *)arg;
75 unsigned int device, subdevice;
76 snd_pcm_stream_t stream;
79 snd_pcm_substream_t *substream;
80 if (get_user(device, &info->device))
82 if (device >= SNDRV_PCM_DEVICES)
84 pcm = snd_pcm_devices[tmp + device];
87 if (get_user(stream, &info->stream))
89 if (stream < 0 || stream > 1)
91 pstr = &pcm->streams[stream];
92 if (pstr->substream_count == 0)
94 if (get_user(subdevice, &info->subdevice))
96 if (subdevice >= pstr->substream_count)
98 for (substream = pstr->substream; substream; substream = substream->next)
99 if (substream->number == (int)subdevice)
101 if (substream == NULL)
103 return snd_pcm_info_user(substream, info);
105 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
109 if (get_user(val, (int *)arg))
111 control->prefer_pcm_subdevice = val;
117 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
118 #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
119 #define READY(v) [SNDRV_PCM_READY_##v] = #v
120 #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
121 #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
122 #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
123 #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
124 #define START(v) [SNDRV_PCM_START_##v] = #v
125 #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
126 #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
128 char *snd_pcm_stream_names[] = {
133 char *snd_pcm_state_names[] = {
144 char *snd_pcm_access_names[] = {
145 ACCESS(MMAP_INTERLEAVED),
146 ACCESS(MMAP_NONINTERLEAVED),
147 ACCESS(MMAP_COMPLEX),
148 ACCESS(RW_INTERLEAVED),
149 ACCESS(RW_NONINTERLEAVED),
152 char *snd_pcm_format_names[] = {
171 FORMAT(IEC958_SUBFRAME_LE),
172 FORMAT(IEC958_SUBFRAME_BE),
193 char *snd_pcm_subformat_names[] = {
197 char *snd_pcm_tstamp_mode_names[] = {
202 const char *snd_pcm_stream_name(snd_pcm_stream_t stream)
204 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return 0);
205 return snd_pcm_stream_names[stream];
208 const char *snd_pcm_access_name(snd_pcm_access_t access)
210 snd_assert(access <= SNDRV_PCM_ACCESS_LAST, return 0);
211 return snd_pcm_access_names[access];
214 const char *snd_pcm_format_name(snd_pcm_format_t format)
216 snd_assert(format <= SNDRV_PCM_FORMAT_LAST, return 0);
217 return snd_pcm_format_names[format];
220 const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
222 snd_assert(subformat <= SNDRV_PCM_SUBFORMAT_LAST, return 0);
223 return snd_pcm_subformat_names[subformat];
226 const char *snd_pcm_tstamp_mode_name(snd_pcm_tstamp_t mode)
228 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return 0);
229 return snd_pcm_tstamp_mode_names[mode];
232 const char *snd_pcm_state_name(snd_pcm_state_t state)
234 snd_assert(state <= SNDRV_PCM_STATE_LAST, return 0);
235 return snd_pcm_state_names[state];
238 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
239 #include <linux/soundcard.h>
240 const char *snd_pcm_oss_format_name(int format)
270 static void snd_pcm_proc_info_read(snd_pcm_substream_t *substream, snd_info_buffer_t *buffer)
274 snd_runtime_check(substream, return);
275 err = snd_pcm_info(substream, &info);
277 snd_iprintf(buffer, "error %d\n", err);
280 snd_iprintf(buffer, "card: %d\n", info.card);
281 snd_iprintf(buffer, "device: %d\n", info.device);
282 snd_iprintf(buffer, "subdevice: %d\n", info.subdevice);
283 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info.stream));
284 snd_iprintf(buffer, "id: %s\n", info.id);
285 snd_iprintf(buffer, "name: %s\n", info.name);
286 snd_iprintf(buffer, "subname: %s\n", info.subname);
287 snd_iprintf(buffer, "class: %d\n", info.dev_class);
288 snd_iprintf(buffer, "subclass: %d\n", info.dev_subclass);
289 snd_iprintf(buffer, "subdevices_count: %d\n", info.subdevices_count);
290 snd_iprintf(buffer, "subdevices_avail: %d\n", info.subdevices_avail);
293 static void snd_pcm_stream_proc_info_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
295 snd_pcm_proc_info_read(((snd_pcm_str_t *)entry->private_data)->substream, buffer);
298 static void snd_pcm_substream_proc_info_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
300 snd_pcm_proc_info_read((snd_pcm_substream_t *)entry->private_data, buffer);
303 static void snd_pcm_substream_proc_hw_params_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
305 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
306 snd_pcm_runtime_t *runtime = substream->runtime;
308 snd_iprintf(buffer, "closed\n");
311 snd_pcm_stream_lock_irq(substream);
312 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
313 snd_iprintf(buffer, "no setup\n");
314 snd_pcm_stream_unlock_irq(substream);
317 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
318 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
319 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
320 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
321 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
322 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
323 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
324 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
325 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
326 if (substream->oss.oss) {
327 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
328 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
329 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
330 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
331 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
332 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
335 snd_pcm_stream_unlock_irq(substream);
338 static void snd_pcm_substream_proc_sw_params_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
340 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
341 snd_pcm_runtime_t *runtime = substream->runtime;
343 snd_iprintf(buffer, "closed\n");
346 snd_pcm_stream_lock_irq(substream);
347 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
348 snd_iprintf(buffer, "no setup\n");
349 snd_pcm_stream_unlock_irq(substream);
352 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
353 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
354 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
355 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
356 snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
357 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
358 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
359 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
360 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
361 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
362 snd_pcm_stream_unlock_irq(substream);
365 static void snd_pcm_substream_proc_status_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
367 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
368 snd_pcm_runtime_t *runtime = substream->runtime;
369 snd_pcm_status_t status;
372 snd_iprintf(buffer, "closed\n");
375 memset(&status, 0, sizeof(status));
376 err = snd_pcm_status(substream, &status);
378 snd_iprintf(buffer, "error %d\n", err);
381 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
382 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
383 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
384 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
385 status.tstamp.tv_sec, status.tstamp.tv_nsec);
386 snd_iprintf(buffer, "delay : %ld\n", status.delay);
387 snd_iprintf(buffer, "avail : %ld\n", status.avail);
388 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
389 snd_iprintf(buffer, "-----\n");
390 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
391 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
394 #ifdef CONFIG_SND_DEBUG
395 static void snd_pcm_xrun_debug_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
397 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
398 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
401 static void snd_pcm_xrun_debug_write(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
403 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
405 if (!snd_info_get_line(buffer, line, sizeof(line)))
406 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
410 static int snd_pcm_stream_proc_init(snd_pcm_str_t *pstr)
412 snd_pcm_t *pcm = pstr->pcm;
413 snd_info_entry_t *entry;
416 sprintf(name, "pcm%i%c", pcm->device,
417 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
418 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
420 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
421 if (snd_info_register(entry) < 0) {
422 snd_info_free_entry(entry);
425 pstr->proc_root = entry;
427 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
428 snd_info_set_text_ops(entry, pstr, 256, snd_pcm_stream_proc_info_read);
429 if (snd_info_register(entry) < 0) {
430 snd_info_free_entry(entry);
434 pstr->proc_info_entry = entry;
436 #ifdef CONFIG_SND_DEBUG
437 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug", pstr->proc_root)) != NULL) {
438 entry->c.text.read_size = 64;
439 entry->c.text.read = snd_pcm_xrun_debug_read;
440 entry->c.text.write_size = 64;
441 entry->c.text.write = snd_pcm_xrun_debug_write;
442 entry->private_data = pstr;
443 if (snd_info_register(entry) < 0) {
444 snd_info_free_entry(entry);
448 pstr->proc_xrun_debug_entry = entry;
453 static int snd_pcm_stream_proc_done(snd_pcm_str_t *pstr)
455 #ifdef CONFIG_SND_DEBUG
456 if (pstr->proc_xrun_debug_entry) {
457 snd_info_unregister(pstr->proc_xrun_debug_entry);
458 pstr->proc_xrun_debug_entry = NULL;
461 if (pstr->proc_info_entry) {
462 snd_info_unregister(pstr->proc_info_entry);
463 pstr->proc_info_entry = NULL;
465 if (pstr->proc_root) {
466 snd_info_unregister(pstr->proc_root);
467 pstr->proc_root = NULL;
472 static int snd_pcm_substream_proc_init(snd_pcm_substream_t *substream)
474 snd_info_entry_t *entry;
478 card = substream->pcm->card;
480 sprintf(name, "sub%i", substream->number);
481 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
483 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
484 if (snd_info_register(entry) < 0) {
485 snd_info_free_entry(entry);
488 substream->proc_root = entry;
490 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
491 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_info_read);
492 if (snd_info_register(entry) < 0) {
493 snd_info_free_entry(entry);
497 substream->proc_info_entry = entry;
499 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
500 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_hw_params_read);
501 if (snd_info_register(entry) < 0) {
502 snd_info_free_entry(entry);
506 substream->proc_hw_params_entry = entry;
508 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
509 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_sw_params_read);
510 if (snd_info_register(entry) < 0) {
511 snd_info_free_entry(entry);
515 substream->proc_sw_params_entry = entry;
517 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
518 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_status_read);
519 if (snd_info_register(entry) < 0) {
520 snd_info_free_entry(entry);
524 substream->proc_status_entry = entry;
529 static int snd_pcm_substream_proc_done(snd_pcm_substream_t *substream)
531 if (substream->proc_info_entry) {
532 snd_info_unregister(substream->proc_info_entry);
533 substream->proc_info_entry = NULL;
535 if (substream->proc_hw_params_entry) {
536 snd_info_unregister(substream->proc_hw_params_entry);
537 substream->proc_hw_params_entry = NULL;
539 if (substream->proc_sw_params_entry) {
540 snd_info_unregister(substream->proc_sw_params_entry);
541 substream->proc_sw_params_entry = NULL;
543 if (substream->proc_status_entry) {
544 snd_info_unregister(substream->proc_status_entry);
545 substream->proc_status_entry = NULL;
547 if (substream->proc_root) {
548 snd_info_unregister(substream->proc_root);
549 substream->proc_root = NULL;
555 * snd_pcm_new_stream - create a new PCM stream
556 * @pcm: the pcm instance
557 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
558 * @substream_count: the number of substreams
560 * Creates a new stream for the pcm.
561 * The corresponding stream on the pcm must have been empty before
562 * calling this, i.e. zero must be given to the argument of
565 * Returns zero if successful, or a negative error code on failure.
567 int snd_pcm_new_stream(snd_pcm_t *pcm, int stream, int substream_count)
570 snd_pcm_str_t *pstr = &pcm->streams[stream];
571 snd_pcm_substream_t *substream, *prev;
573 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
574 init_MUTEX(&pstr->oss.setup_mutex);
576 pstr->stream = stream;
578 pstr->substream_count = substream_count;
579 pstr->reg = &snd_pcm_reg[stream];
580 if (substream_count > 0) {
581 err = snd_pcm_stream_proc_init(pstr);
586 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
587 substream = snd_magic_kcalloc(snd_pcm_substream_t, 0, GFP_KERNEL);
588 if (substream == NULL)
590 substream->pcm = pcm;
591 substream->pstr = pstr;
592 substream->number = idx;
593 substream->stream = stream;
594 sprintf(substream->name, "subdevice #%i", idx);
595 substream->buffer_bytes_max = UINT_MAX;
597 pstr->substream = substream;
599 prev->next = substream;
600 err = snd_pcm_substream_proc_init(substream);
602 snd_magic_kfree(substream);
605 substream->group = &substream->self_group;
606 spin_lock_init(&substream->self_group.lock);
607 INIT_LIST_HEAD(&substream->self_group.substreams);
608 list_add_tail(&substream->link_list, &substream->self_group.substreams);
609 spin_lock_init(&substream->timer_lock);
616 * snd_pcm_new - create a new PCM instance
617 * @card: the card instance
619 * @device: the device index (zero based)
620 * @playback_count: the number of substreams for playback
621 * @capture_count: the number of substreams for capture
622 * @rpcm: the pointer to store the new pcm instance
624 * Creates a new PCM instance.
626 * The pcm operators have to be set afterwards to the new instance
627 * via snd_pcm_set_ops().
629 * Returns zero if successful, or a negative error code on failure.
631 int snd_pcm_new(snd_card_t * card, char *id, int device,
632 int playback_count, int capture_count,
637 static snd_device_ops_t ops = {
638 .dev_free = snd_pcm_dev_free,
639 .dev_register = snd_pcm_dev_register,
640 .dev_disconnect = snd_pcm_dev_disconnect,
641 .dev_unregister = snd_pcm_dev_unregister
644 snd_assert(rpcm != NULL, return -EINVAL);
646 snd_assert(card != NULL, return -ENXIO);
647 pcm = snd_magic_kcalloc(snd_pcm_t, 0, GFP_KERNEL);
651 pcm->device = device;
653 strlcpy(pcm->id, id, sizeof(pcm->id));
655 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
659 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
663 init_MUTEX(&pcm->open_mutex);
664 init_waitqueue_head(&pcm->open_wait);
665 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
673 static void snd_pcm_free_stream(snd_pcm_str_t * pstr)
675 snd_pcm_substream_t *substream, *substream_next;
676 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
677 snd_pcm_oss_setup_t *setup, *setupn;
679 substream = pstr->substream;
681 substream_next = substream->next;
682 snd_pcm_substream_proc_done(substream);
683 snd_magic_kfree(substream);
684 substream = substream_next;
686 snd_pcm_stream_proc_done(pstr);
687 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
688 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
689 setupn = setup->next;
690 kfree(setup->task_name);
696 static int snd_pcm_free(snd_pcm_t *pcm)
698 snd_assert(pcm != NULL, return -ENXIO);
699 if (pcm->private_free)
700 pcm->private_free(pcm);
701 snd_pcm_lib_preallocate_free_for_all(pcm);
702 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
703 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
704 snd_magic_kfree(pcm);
708 static int snd_pcm_dev_free(snd_device_t *device)
710 snd_pcm_t *pcm = snd_magic_cast(snd_pcm_t, device->device_data, return -ENXIO);
711 return snd_pcm_free(pcm);
714 static void snd_pcm_tick_timer_func(unsigned long data)
716 snd_pcm_substream_t *substream = (snd_pcm_substream_t*) data;
717 snd_pcm_tick_elapsed(substream);
720 int snd_pcm_open_substream(snd_pcm_t *pcm, int stream,
721 snd_pcm_substream_t **rsubstream)
723 snd_pcm_str_t * pstr;
724 snd_pcm_substream_t * substream;
725 snd_pcm_runtime_t * runtime;
726 snd_ctl_file_t *kctl;
728 struct list_head *list;
729 int prefer_subdevice = -1;
732 snd_assert(rsubstream != NULL, return -EINVAL);
734 snd_assert(pcm != NULL, return -ENXIO);
735 pstr = &pcm->streams[stream];
736 if (pstr->substream == NULL)
740 down_read(&card->controls_rwsem);
741 list_for_each(list, &card->ctl_files) {
742 kctl = snd_ctl_file(list);
743 if (kctl->pid == current->pid) {
744 prefer_subdevice = kctl->prefer_pcm_subdevice;
748 up_read(&card->controls_rwsem);
750 if (pstr->substream_count == 0)
753 case SNDRV_PCM_STREAM_PLAYBACK:
754 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
755 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
756 if (SUBSTREAM_BUSY(substream))
761 case SNDRV_PCM_STREAM_CAPTURE:
762 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
763 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
764 if (SUBSTREAM_BUSY(substream))
773 if (prefer_subdevice >= 0) {
774 for (substream = pstr->substream; substream; substream = substream->next)
775 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
778 for (substream = pstr->substream; substream; substream = substream->next)
779 if (!SUBSTREAM_BUSY(substream))
782 if (substream == NULL)
785 runtime = snd_kcalloc(sizeof(snd_pcm_runtime_t), GFP_KERNEL);
789 size = PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t));
790 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
791 if (runtime->status == NULL) {
795 memset((void*)runtime->status, 0, size);
797 size = PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t));
798 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
799 if (runtime->control == NULL) {
800 snd_free_pages((void*)runtime->status, PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)));
804 memset((void*)runtime->control, 0, size);
806 init_waitqueue_head(&runtime->sleep);
807 atomic_set(&runtime->mmap_count, 0);
808 init_timer(&runtime->tick_timer);
809 runtime->tick_timer.function = snd_pcm_tick_timer_func;
810 runtime->tick_timer.data = (unsigned long) substream;
812 runtime->status->state = SNDRV_PCM_STATE_OPEN;
814 substream->runtime = runtime;
815 substream->private_data = pcm->private_data;
816 pstr->substream_opened++;
817 *rsubstream = substream;
821 void snd_pcm_release_substream(snd_pcm_substream_t *substream)
823 snd_pcm_runtime_t * runtime;
824 substream->file = NULL;
825 runtime = substream->runtime;
826 snd_assert(runtime != NULL, return);
827 if (runtime->private_free != NULL)
828 runtime->private_free(runtime);
829 snd_free_pages((void*)runtime->status, PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)));
830 snd_free_pages((void*)runtime->control, PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)));
831 if (runtime->hw_constraints.rules)
832 kfree(runtime->hw_constraints.rules);
834 substream->runtime = NULL;
835 substream->pstr->substream_opened--;
838 static int snd_pcm_dev_register(snd_device_t *device)
841 unsigned short minor;
842 snd_pcm_substream_t *substream;
843 struct list_head *list;
845 snd_pcm_t *pcm = snd_magic_cast(snd_pcm_t, device->device_data, return -ENXIO);
847 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
848 down(®ister_mutex);
849 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
850 if (snd_pcm_devices[idx]) {
854 snd_pcm_devices[idx] = pcm;
855 for (cidx = 0; cidx < 2; cidx++) {
857 if (pcm->streams[cidx].substream == NULL)
860 case SNDRV_PCM_STREAM_PLAYBACK:
861 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
862 minor = SNDRV_MINOR_PCM_PLAYBACK + idx;
863 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
865 case SNDRV_PCM_STREAM_CAPTURE:
866 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
867 minor = SNDRV_MINOR_PCM_CAPTURE + idx;
868 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
871 if ((err = snd_register_device(devtype, pcm->card, pcm->device, pcm->streams[cidx].reg, str)) < 0) {
872 snd_pcm_devices[idx] = NULL;
876 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
877 snd_pcm_timer_init(substream);
879 list_for_each(list, &snd_pcm_notify_list) {
880 snd_pcm_notify_t *notify;
881 notify = list_entry(list, snd_pcm_notify_t, list);
882 notify->n_register(pcm);
888 static int snd_pcm_dev_disconnect(snd_device_t *device)
890 snd_pcm_t *pcm = snd_magic_cast(snd_pcm_t, device->device_data, return -ENXIO);
891 struct list_head *list;
892 snd_pcm_substream_t *substream;
895 down(®ister_mutex);
896 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
897 snd_pcm_devices[idx] = NULL;
898 for (cidx = 0; cidx < 2; cidx++)
899 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
900 if (substream->runtime)
901 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
902 list_for_each(list, &snd_pcm_notify_list) {
903 snd_pcm_notify_t *notify;
904 notify = list_entry(list, snd_pcm_notify_t, list);
905 notify->n_disconnect(pcm);
911 static int snd_pcm_dev_unregister(snd_device_t *device)
913 int idx, cidx, devtype;
914 snd_pcm_substream_t *substream;
915 struct list_head *list;
916 snd_pcm_t *pcm = snd_magic_cast(snd_pcm_t, device->device_data, return -ENXIO);
918 snd_assert(pcm != NULL, return -ENXIO);
919 down(®ister_mutex);
920 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
921 snd_pcm_devices[idx] = NULL;
922 for (cidx = 0; cidx < 2; cidx++) {
925 case SNDRV_PCM_STREAM_PLAYBACK:
926 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
928 case SNDRV_PCM_STREAM_CAPTURE:
929 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
932 snd_unregister_device(devtype, pcm->card, pcm->device);
933 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
934 snd_pcm_timer_done(substream);
936 list_for_each(list, &snd_pcm_notify_list) {
937 snd_pcm_notify_t *notify;
938 notify = list_entry(list, snd_pcm_notify_t, list);
939 notify->n_unregister(pcm);
942 return snd_pcm_free(pcm);
945 int snd_pcm_notify(snd_pcm_notify_t *notify, int nfree)
949 snd_assert(notify != NULL && notify->n_register != NULL && notify->n_unregister != NULL, return -EINVAL);
950 down(®ister_mutex);
952 list_del(¬ify->list);
953 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
954 if (snd_pcm_devices[idx] == NULL)
956 notify->n_unregister(snd_pcm_devices[idx]);
959 list_add_tail(¬ify->list, &snd_pcm_notify_list);
960 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
961 if (snd_pcm_devices[idx] == NULL)
963 notify->n_register(snd_pcm_devices[idx]);
974 static void snd_pcm_proc_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
979 down(®ister_mutex);
980 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
981 pcm = snd_pcm_devices[idx];
984 snd_iprintf(buffer, "%02i-%02i: %s : %s", idx / SNDRV_PCM_DEVICES,
985 idx % SNDRV_PCM_DEVICES, pcm->id, pcm->name);
986 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
987 snd_iprintf(buffer, " : playback %i", pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
988 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
989 snd_iprintf(buffer, " : capture %i", pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
990 snd_iprintf(buffer, "\n");
999 static snd_info_entry_t *snd_pcm_proc_entry = NULL;
1001 static int __init alsa_pcm_init(void)
1003 snd_info_entry_t *entry;
1005 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1006 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
1007 snd_info_set_text_ops(entry, NULL, SNDRV_CARDS * SNDRV_PCM_DEVICES * 128, snd_pcm_proc_read);
1008 if (snd_info_register(entry) < 0) {
1009 snd_info_free_entry(entry);
1013 snd_pcm_proc_entry = entry;
1017 static void __exit alsa_pcm_exit(void)
1019 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1020 if (snd_pcm_proc_entry) {
1021 snd_info_unregister(snd_pcm_proc_entry);
1022 snd_pcm_proc_entry = NULL;
1026 module_init(alsa_pcm_init)
1027 module_exit(alsa_pcm_exit)
1029 EXPORT_SYMBOL(snd_pcm_devices);
1030 EXPORT_SYMBOL(snd_pcm_new);
1031 EXPORT_SYMBOL(snd_pcm_new_stream);
1032 EXPORT_SYMBOL(snd_pcm_notify);
1033 EXPORT_SYMBOL(snd_pcm_open_substream);
1034 EXPORT_SYMBOL(snd_pcm_release_substream);
1035 EXPORT_SYMBOL(snd_pcm_format_name);
1036 EXPORT_SYMBOL(snd_pcm_subformat_name);
1038 EXPORT_SYMBOL(snd_pcm_link_rwlock);
1039 EXPORT_SYMBOL(snd_pcm_start);
1041 EXPORT_SYMBOL(snd_pcm_suspend);
1042 EXPORT_SYMBOL(snd_pcm_suspend_all);
1044 EXPORT_SYMBOL(snd_pcm_kernel_playback_ioctl);
1045 EXPORT_SYMBOL(snd_pcm_kernel_capture_ioctl);
1046 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
1047 EXPORT_SYMBOL(snd_pcm_open);
1048 EXPORT_SYMBOL(snd_pcm_release);
1049 EXPORT_SYMBOL(snd_pcm_playback_poll);
1050 EXPORT_SYMBOL(snd_pcm_capture_poll);
1051 EXPORT_SYMBOL(snd_pcm_mmap_data);
1053 EXPORT_SYMBOL(snd_pcm_format_signed);
1054 EXPORT_SYMBOL(snd_pcm_format_unsigned);
1055 EXPORT_SYMBOL(snd_pcm_format_linear);
1056 EXPORT_SYMBOL(snd_pcm_format_little_endian);
1057 EXPORT_SYMBOL(snd_pcm_format_big_endian);
1058 EXPORT_SYMBOL(snd_pcm_format_width);
1059 EXPORT_SYMBOL(snd_pcm_format_physical_width);
1060 EXPORT_SYMBOL(snd_pcm_format_size);
1061 EXPORT_SYMBOL(snd_pcm_format_silence_64);
1062 EXPORT_SYMBOL(snd_pcm_format_set_silence);
1063 EXPORT_SYMBOL(snd_pcm_build_linear_format);
1064 EXPORT_SYMBOL(snd_pcm_limit_hw_rates);