2 * Initialization routines
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/sched.h>
25 #include <linux/file.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/ctype.h>
29 #include <linux/pci.h>
32 #include <sound/core.h>
33 #include <sound/control.h>
34 #include <sound/info.h>
36 struct snd_shutdown_f_ops {
37 struct file_operations f_ops;
38 struct snd_shutdown_f_ops *next;
41 static unsigned int snd_cards_lock; /* locked for registering/using */
42 struct snd_card *snd_cards[SNDRV_CARDS];
43 EXPORT_SYMBOL(snd_cards);
45 static DEFINE_MUTEX(snd_card_mutex);
47 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
48 int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
49 EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
53 static void snd_card_id_read(struct snd_info_entry *entry,
54 struct snd_info_buffer *buffer)
56 snd_iprintf(buffer, "%s\n", entry->card->id);
59 static inline int init_info_for_card(struct snd_card *card)
62 struct snd_info_entry *entry;
64 if ((err = snd_info_card_register(card)) < 0) {
65 snd_printd("unable to create card info\n");
68 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
69 snd_printd("unable to create card entry\n");
72 entry->c.text.read = snd_card_id_read;
73 if (snd_info_register(entry) < 0) {
74 snd_info_free_entry(entry);
77 card->proc_id = entry;
80 #else /* !CONFIG_PROC_FS */
81 #define init_info_for_card(card)
85 * snd_card_new - create and initialize a soundcard structure
86 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
87 * @xid: card identification (ASCII string)
88 * @module: top level module for locking
89 * @extra_size: allocate this extra size after the main soundcard structure
91 * Creates and initializes a soundcard structure.
93 * Returns kmallocated snd_card structure. Creates the ALSA control interface
94 * (which is blocked until snd_card_register function is called).
96 struct snd_card *snd_card_new(int idx, const char *xid,
97 struct module *module, int extra_size)
99 struct snd_card *card;
104 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
108 if (!snd_info_check_reserved_words(xid))
110 strlcpy(card->id, xid, sizeof(card->id));
113 mutex_lock(&snd_card_mutex);
116 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
117 if (~snd_cards_lock & idx & 1<<idx2) {
119 if (idx >= snd_ecards_limit)
120 snd_ecards_limit = idx + 1;
123 } else if (idx < snd_ecards_limit) {
124 if (snd_cards_lock & (1 << idx))
125 err = -ENODEV; /* invalid */
126 } else if (idx < SNDRV_CARDS)
127 snd_ecards_limit = idx + 1; /* increase the limit */
130 if (idx < 0 || err < 0) {
131 mutex_unlock(&snd_card_mutex);
132 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
135 snd_cards_lock |= 1 << idx; /* lock it */
136 mutex_unlock(&snd_card_mutex);
138 card->module = module;
139 INIT_LIST_HEAD(&card->devices);
140 init_rwsem(&card->controls_rwsem);
141 rwlock_init(&card->ctl_files_rwlock);
142 INIT_LIST_HEAD(&card->controls);
143 INIT_LIST_HEAD(&card->ctl_files);
144 spin_lock_init(&card->files_lock);
145 init_waitqueue_head(&card->shutdown_sleep);
147 mutex_init(&card->power_lock);
148 init_waitqueue_head(&card->power_sleep);
150 /* the control interface cannot be accessed from the user space until */
151 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
152 if ((err = snd_ctl_create(card)) < 0) {
153 snd_printd("unable to register control minors\n");
156 if ((err = snd_info_card_create(card)) < 0) {
157 snd_printd("unable to create card info\n");
161 card->private_data = (char *)card + sizeof(struct snd_card);
165 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
171 EXPORT_SYMBOL(snd_card_new);
173 /* return non-zero if a card is already locked */
174 int snd_card_locked(int card)
178 mutex_lock(&snd_card_mutex);
179 locked = snd_cards_lock & (1 << card);
180 mutex_unlock(&snd_card_mutex);
184 static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
189 static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
190 size_t count, loff_t *offset)
195 static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
196 size_t count, loff_t *offset)
201 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
203 return POLLERR | POLLNVAL;
206 static long snd_disconnect_ioctl(struct file *file,
207 unsigned int cmd, unsigned long arg)
212 static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
217 static int snd_disconnect_fasync(int fd, struct file *file, int on)
223 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
224 * @card: soundcard structure
226 * Disconnects all APIs from the file-operations (user space).
228 * Returns zero, otherwise a negative error code.
230 * Note: The current implementation replaces all active file->f_op with special
231 * dummy file operations (they do nothing except release).
233 int snd_card_disconnect(struct snd_card *card)
235 struct snd_monitor_file *mfile;
237 struct snd_shutdown_f_ops *s_f_ops;
238 struct file_operations *f_ops;
239 const struct file_operations *old_f_ops;
242 spin_lock(&card->files_lock);
243 if (card->shutdown) {
244 spin_unlock(&card->files_lock);
248 spin_unlock(&card->files_lock);
250 /* phase 1: disable fops (user space) operations for ALSA API */
251 mutex_lock(&snd_card_mutex);
252 snd_cards[card->number] = NULL;
253 mutex_unlock(&snd_card_mutex);
255 /* phase 2: replace file->f_op with special dummy operations */
257 spin_lock(&card->files_lock);
262 /* it's critical part, use endless loop */
263 /* we have no room to fail */
264 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
266 panic("Atomic allocation failed for snd_shutdown_f_ops!");
268 f_ops = &s_f_ops->f_ops;
270 memset(f_ops, 0, sizeof(*f_ops));
271 f_ops->owner = file->f_op->owner;
272 f_ops->release = file->f_op->release;
273 f_ops->llseek = snd_disconnect_llseek;
274 f_ops->read = snd_disconnect_read;
275 f_ops->write = snd_disconnect_write;
276 f_ops->poll = snd_disconnect_poll;
277 f_ops->unlocked_ioctl = snd_disconnect_ioctl;
279 f_ops->compat_ioctl = snd_disconnect_ioctl;
281 f_ops->mmap = snd_disconnect_mmap;
282 f_ops->fasync = snd_disconnect_fasync;
284 s_f_ops->next = card->s_f_ops;
285 card->s_f_ops = s_f_ops;
287 f_ops = fops_get(f_ops);
289 old_f_ops = file->f_op;
290 file->f_op = f_ops; /* must be atomic */
295 spin_unlock(&card->files_lock);
297 /* phase 3: notify all connected devices about disconnection */
298 /* at this point, they cannot respond to any calls except release() */
300 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
301 if (snd_mixer_oss_notify_callback)
302 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
305 /* notify all devices that we are disconnected */
306 err = snd_device_disconnect_all(card);
308 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
310 snd_info_card_disconnect(card);
314 EXPORT_SYMBOL(snd_card_disconnect);
317 * snd_card_free - frees given soundcard structure
318 * @card: soundcard structure
320 * This function releases the soundcard structure and the all assigned
321 * devices automatically. That is, you don't have to release the devices
324 * Returns zero. Frees all associated devices and frees the control
325 * interface associated to given soundcard.
327 static int snd_card_do_free(struct snd_card *card)
329 struct snd_shutdown_f_ops *s_f_ops;
331 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
332 if (snd_mixer_oss_notify_callback)
333 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
335 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
336 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
337 /* Fatal, but this situation should never occur */
339 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
340 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
341 /* Fatal, but this situation should never occur */
343 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
344 snd_printk(KERN_ERR "unable to free all devices (post)\n");
345 /* Fatal, but this situation should never occur */
347 if (card->private_free)
348 card->private_free(card);
349 snd_info_free_entry(card->proc_id);
350 if (snd_info_card_free(card) < 0) {
351 snd_printk(KERN_WARNING "unable to free card info\n");
352 /* Not fatal error */
354 while (card->s_f_ops) {
355 s_f_ops = card->s_f_ops;
356 card->s_f_ops = s_f_ops->next;
363 static int snd_card_free_prepare(struct snd_card *card)
367 (void) snd_card_disconnect(card);
368 mutex_lock(&snd_card_mutex);
369 snd_cards[card->number] = NULL;
370 snd_cards_lock &= ~(1 << card->number);
371 mutex_unlock(&snd_card_mutex);
373 wake_up(&card->power_sleep);
378 int snd_card_free_when_closed(struct snd_card *card)
381 int ret = snd_card_free_prepare(card);
385 spin_lock(&card->files_lock);
386 if (card->files == NULL)
389 card->free_on_last_close = 1;
390 spin_unlock(&card->files_lock);
393 snd_card_do_free(card);
397 EXPORT_SYMBOL(snd_card_free_when_closed);
399 int snd_card_free(struct snd_card *card)
401 int ret = snd_card_free_prepare(card);
405 /* wait, until all devices are ready for the free operation */
406 wait_event(card->shutdown_sleep, card->files == NULL);
407 snd_card_do_free(card);
411 EXPORT_SYMBOL(snd_card_free);
413 static void choose_default_id(struct snd_card *card)
415 int i, len, idx_flag = 0, loops = SNDRV_CARDS;
418 id = spos = card->shortname;
419 while (*id != '\0') {
425 while (*spos != '\0' && !isalnum(*spos))
428 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
429 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
439 strcpy(id, "default");
443 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
444 strcpy(card->id, card->proc_root->name);
447 if (!snd_info_check_reserved_words(id))
449 for (i = 0; i < snd_ecards_limit; i++) {
450 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
458 if (id[len-1] != '9')
462 } else if ((size_t)len <= sizeof(card->id) - 3) {
467 if ((size_t)len <= sizeof(card->id) - 2)
478 * snd_card_register - register the soundcard
479 * @card: soundcard structure
481 * This function registers all the devices assigned to the soundcard.
482 * Until calling this, the ALSA control interface is blocked from the
483 * external accesses. Thus, you should call this function at the end
484 * of the initialization of the card.
486 * Returns zero otherwise a negative error code if the registrain failed.
488 int snd_card_register(struct snd_card *card)
492 snd_assert(card != NULL, return -EINVAL);
493 if ((err = snd_device_register_all(card)) < 0)
495 mutex_lock(&snd_card_mutex);
496 if (snd_cards[card->number]) {
497 /* already registered */
498 mutex_unlock(&snd_card_mutex);
501 if (card->id[0] == '\0')
502 choose_default_id(card);
503 snd_cards[card->number] = card;
504 mutex_unlock(&snd_card_mutex);
505 init_info_for_card(card);
506 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
507 if (snd_mixer_oss_notify_callback)
508 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
513 EXPORT_SYMBOL(snd_card_register);
515 #ifdef CONFIG_PROC_FS
516 static struct snd_info_entry *snd_card_info_entry;
518 static void snd_card_info_read(struct snd_info_entry *entry,
519 struct snd_info_buffer *buffer)
522 struct snd_card *card;
524 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
525 mutex_lock(&snd_card_mutex);
526 if ((card = snd_cards[idx]) != NULL) {
528 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
533 snd_iprintf(buffer, " %s\n",
536 mutex_unlock(&snd_card_mutex);
539 snd_iprintf(buffer, "--- no soundcards ---\n");
542 #ifdef CONFIG_SND_OSSEMUL
544 void snd_card_info_read_oss(struct snd_info_buffer *buffer)
547 struct snd_card *card;
549 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
550 mutex_lock(&snd_card_mutex);
551 if ((card = snd_cards[idx]) != NULL) {
553 snd_iprintf(buffer, "%s\n", card->longname);
555 mutex_unlock(&snd_card_mutex);
558 snd_iprintf(buffer, "--- no soundcards ---\n");
565 static struct snd_info_entry *snd_card_module_info_entry;
566 static void snd_card_module_info_read(struct snd_info_entry *entry,
567 struct snd_info_buffer *buffer)
570 struct snd_card *card;
572 for (idx = 0; idx < SNDRV_CARDS; idx++) {
573 mutex_lock(&snd_card_mutex);
574 if ((card = snd_cards[idx]) != NULL)
575 snd_iprintf(buffer, "%2i %s\n",
576 idx, card->module->name);
577 mutex_unlock(&snd_card_mutex);
582 int __init snd_card_info_init(void)
584 struct snd_info_entry *entry;
586 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
589 entry->c.text.read = snd_card_info_read;
590 if (snd_info_register(entry) < 0) {
591 snd_info_free_entry(entry);
594 snd_card_info_entry = entry;
597 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
599 entry->c.text.read = snd_card_module_info_read;
600 if (snd_info_register(entry) < 0)
601 snd_info_free_entry(entry);
603 snd_card_module_info_entry = entry;
610 int __exit snd_card_info_done(void)
612 snd_info_free_entry(snd_card_info_entry);
614 snd_info_free_entry(snd_card_module_info_entry);
619 #endif /* CONFIG_PROC_FS */
622 * snd_component_add - add a component string
623 * @card: soundcard structure
624 * @component: the component id string
626 * This function adds the component id string to the supported list.
627 * The component can be referred from the alsa-lib.
629 * Returns zero otherwise a negative error code.
632 int snd_component_add(struct snd_card *card, const char *component)
635 int len = strlen(component);
637 ptr = strstr(card->components, component);
639 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
642 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
646 if (card->components[0] != '\0')
647 strcat(card->components, " ");
648 strcat(card->components, component);
652 EXPORT_SYMBOL(snd_component_add);
655 * snd_card_file_add - add the file to the file list of the card
656 * @card: soundcard structure
657 * @file: file pointer
659 * This function adds the file to the file linked-list of the card.
660 * This linked-list is used to keep tracking the connection state,
661 * and to avoid the release of busy resources by hotplug.
663 * Returns zero or a negative error code.
665 int snd_card_file_add(struct snd_card *card, struct file *file)
667 struct snd_monitor_file *mfile;
669 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
674 spin_lock(&card->files_lock);
675 if (card->shutdown) {
676 spin_unlock(&card->files_lock);
680 mfile->next = card->files;
682 spin_unlock(&card->files_lock);
686 EXPORT_SYMBOL(snd_card_file_add);
689 * snd_card_file_remove - remove the file from the file list
690 * @card: soundcard structure
691 * @file: file pointer
693 * This function removes the file formerly added to the card via
694 * snd_card_file_add() function.
695 * If all files are removed and snd_card_free_when_closed() was
696 * called beforehand, it processes the pending release of
699 * Returns zero or a negative error code.
701 int snd_card_file_remove(struct snd_card *card, struct file *file)
703 struct snd_monitor_file *mfile, *pfile = NULL;
706 spin_lock(&card->files_lock);
709 if (mfile->file == file) {
711 pfile->next = mfile->next;
713 card->files = mfile->next;
719 if (card->files == NULL)
721 spin_unlock(&card->files_lock);
723 wake_up(&card->shutdown_sleep);
724 if (card->free_on_last_close)
725 snd_card_do_free(card);
728 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
735 EXPORT_SYMBOL(snd_card_file_remove);
739 * snd_power_wait - wait until the power-state is changed.
740 * @card: soundcard structure
741 * @power_state: expected power state
743 * Waits until the power-state is changed.
745 * Note: the power lock must be active before call.
747 int snd_power_wait(struct snd_card *card, unsigned int power_state)
753 if (snd_power_get_state(card) == power_state)
755 init_waitqueue_entry(&wait, current);
756 add_wait_queue(&card->power_sleep, &wait);
758 if (card->shutdown) {
762 if (snd_power_get_state(card) == power_state)
764 set_current_state(TASK_UNINTERRUPTIBLE);
765 snd_power_unlock(card);
766 schedule_timeout(30 * HZ);
767 snd_power_lock(card);
769 remove_wait_queue(&card->power_sleep, &wait);
773 EXPORT_SYMBOL(snd_power_wait);
774 #endif /* CONFIG_PM */