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 unsigned int snd_cards_lock = 0; /* locked for registering/using */
42 struct snd_card *snd_cards[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = NULL};
43 DEFINE_RWLOCK(snd_card_rwlock);
45 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
46 int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
49 static void snd_card_id_read(struct snd_info_entry *entry,
50 struct snd_info_buffer *buffer)
52 snd_iprintf(buffer, "%s\n", entry->card->id);
55 static void snd_card_free_thread(void * __card);
58 * snd_card_new - create and initialize a soundcard structure
59 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
60 * @xid: card identification (ASCII string)
61 * @module: top level module for locking
62 * @extra_size: allocate this extra size after the main soundcard structure
64 * Creates and initializes a soundcard structure.
66 * Returns kmallocated snd_card structure. Creates the ALSA control interface
67 * (which is blocked until snd_card_register function is called).
69 struct snd_card *snd_card_new(int idx, const char *xid,
70 struct module *module, int extra_size)
72 struct snd_card *card;
77 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
81 if (!snd_info_check_reserved_words(xid))
83 strlcpy(card->id, xid, sizeof(card->id));
86 write_lock(&snd_card_rwlock);
89 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
90 if (~snd_cards_lock & idx & 1<<idx2) {
92 if (idx >= snd_ecards_limit)
93 snd_ecards_limit = idx + 1;
96 } else if (idx < snd_ecards_limit) {
97 if (snd_cards_lock & (1 << idx))
98 err = -ENODEV; /* invalid */
99 } else if (idx < SNDRV_CARDS)
100 snd_ecards_limit = idx + 1; /* increase the limit */
103 if (idx < 0 || err < 0) {
104 write_unlock(&snd_card_rwlock);
105 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
108 snd_cards_lock |= 1 << idx; /* lock it */
109 write_unlock(&snd_card_rwlock);
111 card->module = module;
112 INIT_LIST_HEAD(&card->devices);
113 init_rwsem(&card->controls_rwsem);
114 rwlock_init(&card->ctl_files_rwlock);
115 INIT_LIST_HEAD(&card->controls);
116 INIT_LIST_HEAD(&card->ctl_files);
117 spin_lock_init(&card->files_lock);
118 init_waitqueue_head(&card->shutdown_sleep);
119 INIT_WORK(&card->free_workq, snd_card_free_thread, card);
121 init_MUTEX(&card->power_lock);
122 init_waitqueue_head(&card->power_sleep);
124 /* the control interface cannot be accessed from the user space until */
125 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
126 if ((err = snd_ctl_create(card)) < 0) {
127 snd_printd("unable to register control minors\n");
130 if ((err = snd_info_card_create(card)) < 0) {
131 snd_printd("unable to create card info\n");
135 card->private_data = (char *)card + sizeof(struct snd_card);
139 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
145 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
147 return POLLERR | POLLNVAL;
151 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
152 * @card: soundcard structure
154 * Disconnects all APIs from the file-operations (user space).
156 * Returns zero, otherwise a negative error code.
158 * Note: The current implementation replaces all active file->f_op with special
159 * dummy file operations (they do nothing except release).
161 int snd_card_disconnect(struct snd_card *card)
163 struct snd_monitor_file *mfile;
165 struct snd_shutdown_f_ops *s_f_ops;
166 struct file_operations *f_ops, *old_f_ops;
169 spin_lock(&card->files_lock);
170 if (card->shutdown) {
171 spin_unlock(&card->files_lock);
175 spin_unlock(&card->files_lock);
177 /* phase 1: disable fops (user space) operations for ALSA API */
178 write_lock(&snd_card_rwlock);
179 snd_cards[card->number] = NULL;
180 write_unlock(&snd_card_rwlock);
182 /* phase 2: replace file->f_op with special dummy operations */
184 spin_lock(&card->files_lock);
189 /* it's critical part, use endless loop */
190 /* we have no room to fail */
191 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
193 panic("Atomic allocation failed for snd_shutdown_f_ops!");
195 f_ops = &s_f_ops->f_ops;
197 memset(f_ops, 0, sizeof(*f_ops));
198 f_ops->owner = file->f_op->owner;
199 f_ops->release = file->f_op->release;
200 f_ops->poll = snd_disconnect_poll;
202 s_f_ops->next = card->s_f_ops;
203 card->s_f_ops = s_f_ops;
205 f_ops = fops_get(f_ops);
207 old_f_ops = file->f_op;
208 file->f_op = f_ops; /* must be atomic */
213 spin_unlock(&card->files_lock);
215 /* phase 3: notify all connected devices about disconnection */
216 /* at this point, they cannot respond to any calls except release() */
218 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
219 if (snd_mixer_oss_notify_callback)
220 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
223 /* notify all devices that we are disconnected */
224 err = snd_device_disconnect_all(card);
226 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
232 * snd_card_free - frees given soundcard structure
233 * @card: soundcard structure
235 * This function releases the soundcard structure and the all assigned
236 * devices automatically. That is, you don't have to release the devices
239 * Returns zero. Frees all associated devices and frees the control
240 * interface associated to given soundcard.
242 int snd_card_free(struct snd_card *card)
244 struct snd_shutdown_f_ops *s_f_ops;
248 write_lock(&snd_card_rwlock);
249 snd_cards[card->number] = NULL;
250 write_unlock(&snd_card_rwlock);
253 wake_up(&card->power_sleep);
255 /* wait, until all devices are ready for the free operation */
256 wait_event(card->shutdown_sleep, card->files == NULL);
258 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
259 if (snd_mixer_oss_notify_callback)
260 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
262 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
263 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
264 /* Fatal, but this situation should never occur */
266 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
267 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
268 /* Fatal, but this situation should never occur */
270 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
271 snd_printk(KERN_ERR "unable to free all devices (post)\n");
272 /* Fatal, but this situation should never occur */
274 if (card->private_free)
275 card->private_free(card);
277 snd_info_unregister(card->proc_id);
278 if (snd_info_card_free(card) < 0) {
279 snd_printk(KERN_WARNING "unable to free card info\n");
280 /* Not fatal error */
282 while (card->s_f_ops) {
283 s_f_ops = card->s_f_ops;
284 card->s_f_ops = s_f_ops->next;
287 write_lock(&snd_card_rwlock);
288 snd_cards_lock &= ~(1 << card->number);
289 write_unlock(&snd_card_rwlock);
294 static void snd_card_free_thread(void * __card)
296 struct snd_card *card = __card;
297 struct module * module = card->module;
299 if (!try_module_get(module)) {
300 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
310 * snd_card_free_in_thread - call snd_card_free() in thread
311 * @card: soundcard structure
313 * This function schedules the call of snd_card_free() function in a
314 * work queue. When all devices are released (non-busy), the work
315 * is woken up and calls snd_card_free().
317 * When a card can be disconnected at any time by hotplug service,
318 * this function should be used in disconnect (or detach) callback
319 * instead of calling snd_card_free() directly.
321 * Returns - zero otherwise a negative error code if the start of thread failed.
323 int snd_card_free_in_thread(struct snd_card *card)
325 if (card->files == NULL) {
330 if (schedule_work(&card->free_workq))
333 snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
334 /* try to free the structure immediately */
339 static void choose_default_id(struct snd_card *card)
341 int i, len, idx_flag = 0, loops = SNDRV_CARDS;
344 id = spos = card->shortname;
345 while (*id != '\0') {
351 while (*spos != '\0' && !isalnum(*spos))
354 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
355 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
365 strcpy(id, "default");
369 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
370 strcpy(card->id, card->proc_root->name);
373 if (!snd_info_check_reserved_words(id))
375 for (i = 0; i < snd_ecards_limit; i++) {
376 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
384 if (id[len-1] != '9')
388 } else if ((size_t)len <= sizeof(card->id) - 3) {
393 if ((size_t)len <= sizeof(card->id) - 2)
404 * snd_card_register - register the soundcard
405 * @card: soundcard structure
407 * This function registers all the devices assigned to the soundcard.
408 * Until calling this, the ALSA control interface is blocked from the
409 * external accesses. Thus, you should call this function at the end
410 * of the initialization of the card.
412 * Returns zero otherwise a negative error code if the registrain failed.
414 int snd_card_register(struct snd_card *card)
417 struct snd_info_entry *entry;
419 snd_assert(card != NULL, return -EINVAL);
420 if ((err = snd_device_register_all(card)) < 0)
422 write_lock(&snd_card_rwlock);
423 if (snd_cards[card->number]) {
424 /* already registered */
425 write_unlock(&snd_card_rwlock);
428 if (card->id[0] == '\0')
429 choose_default_id(card);
430 snd_cards[card->number] = card;
431 write_unlock(&snd_card_rwlock);
432 if ((err = snd_info_card_register(card)) < 0) {
433 snd_printd("unable to create card info\n");
436 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
437 snd_printd("unable to create card entry\n");
440 entry->c.text.read_size = PAGE_SIZE;
441 entry->c.text.read = snd_card_id_read;
442 if (snd_info_register(entry) < 0) {
443 snd_info_free_entry(entry);
446 card->proc_id = entry;
448 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
449 if (snd_mixer_oss_notify_callback)
450 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
455 static struct snd_info_entry *snd_card_info_entry = NULL;
457 static void snd_card_info_read(struct snd_info_entry *entry,
458 struct snd_info_buffer *buffer)
461 struct snd_card *card;
463 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
464 read_lock(&snd_card_rwlock);
465 if ((card = snd_cards[idx]) != NULL) {
467 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
472 snd_iprintf(buffer, " %s\n",
475 read_unlock(&snd_card_rwlock);
478 snd_iprintf(buffer, "--- no soundcards ---\n");
481 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
483 void snd_card_info_read_oss(struct snd_info_buffer *buffer)
486 struct snd_card *card;
488 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
489 read_lock(&snd_card_rwlock);
490 if ((card = snd_cards[idx]) != NULL) {
492 snd_iprintf(buffer, "%s\n", card->longname);
494 read_unlock(&snd_card_rwlock);
497 snd_iprintf(buffer, "--- no soundcards ---\n");
504 static struct snd_info_entry *snd_card_module_info_entry;
505 static void snd_card_module_info_read(struct snd_info_entry *entry,
506 struct snd_info_buffer *buffer)
509 struct snd_card *card;
511 for (idx = 0; idx < SNDRV_CARDS; idx++) {
512 read_lock(&snd_card_rwlock);
513 if ((card = snd_cards[idx]) != NULL)
514 snd_iprintf(buffer, "%2i %s\n",
515 idx, card->module->name);
516 read_unlock(&snd_card_rwlock);
521 int __init snd_card_info_init(void)
523 struct snd_info_entry *entry;
525 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
528 entry->c.text.read_size = PAGE_SIZE;
529 entry->c.text.read = snd_card_info_read;
530 if (snd_info_register(entry) < 0) {
531 snd_info_free_entry(entry);
534 snd_card_info_entry = entry;
537 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
539 entry->c.text.read_size = PAGE_SIZE;
540 entry->c.text.read = snd_card_module_info_read;
541 if (snd_info_register(entry) < 0)
542 snd_info_free_entry(entry);
544 snd_card_module_info_entry = entry;
551 int __exit snd_card_info_done(void)
553 if (snd_card_info_entry)
554 snd_info_unregister(snd_card_info_entry);
556 if (snd_card_module_info_entry)
557 snd_info_unregister(snd_card_module_info_entry);
563 * snd_component_add - add a component string
564 * @card: soundcard structure
565 * @component: the component id string
567 * This function adds the component id string to the supported list.
568 * The component can be referred from the alsa-lib.
570 * Returns zero otherwise a negative error code.
573 int snd_component_add(struct snd_card *card, const char *component)
576 int len = strlen(component);
578 ptr = strstr(card->components, component);
580 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
583 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
587 if (card->components[0] != '\0')
588 strcat(card->components, " ");
589 strcat(card->components, component);
594 * snd_card_file_add - add the file to the file list of the card
595 * @card: soundcard structure
596 * @file: file pointer
598 * This function adds the file to the file linked-list of the card.
599 * This linked-list is used to keep tracking the connection state,
600 * and to avoid the release of busy resources by hotplug.
602 * Returns zero or a negative error code.
604 int snd_card_file_add(struct snd_card *card, struct file *file)
606 struct snd_monitor_file *mfile;
608 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
613 spin_lock(&card->files_lock);
614 if (card->shutdown) {
615 spin_unlock(&card->files_lock);
619 mfile->next = card->files;
621 spin_unlock(&card->files_lock);
626 * snd_card_file_remove - remove the file from the file list
627 * @card: soundcard structure
628 * @file: file pointer
630 * This function removes the file formerly added to the card via
631 * snd_card_file_add() function.
632 * If all files are removed and the release of the card is
633 * scheduled, it will wake up the the thread to call snd_card_free()
634 * (see snd_card_free_in_thread() function).
636 * Returns zero or a negative error code.
638 int snd_card_file_remove(struct snd_card *card, struct file *file)
640 struct snd_monitor_file *mfile, *pfile = NULL;
642 spin_lock(&card->files_lock);
645 if (mfile->file == file) {
647 pfile->next = mfile->next;
649 card->files = mfile->next;
655 spin_unlock(&card->files_lock);
656 if (card->files == NULL)
657 wake_up(&card->shutdown_sleep);
659 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
668 * snd_power_wait - wait until the power-state is changed.
669 * @card: soundcard structure
670 * @power_state: expected power state
671 * @file: file structure for the O_NONBLOCK check (optional)
673 * Waits until the power-state is changed.
675 * Note: the power lock must be active before call.
677 int snd_power_wait(struct snd_card *card, unsigned int power_state, struct file *file)
683 if (snd_power_get_state(card) == power_state)
685 init_waitqueue_entry(&wait, current);
686 add_wait_queue(&card->power_sleep, &wait);
688 if (card->shutdown) {
692 if (snd_power_get_state(card) == power_state)
694 #if 0 /* block all devices */
695 if (file && (file->f_flags & O_NONBLOCK)) {
700 set_current_state(TASK_UNINTERRUPTIBLE);
701 snd_power_unlock(card);
702 schedule_timeout(30 * HZ);
703 snd_power_lock(card);
705 remove_wait_queue(&card->power_sleep, &wait);
709 #endif /* CONFIG_PM */