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 EXPORT_SYMBOL(snd_cards);
45 DEFINE_RWLOCK(snd_card_rwlock);
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_size = PAGE_SIZE;
73 entry->c.text.read = snd_card_id_read;
74 if (snd_info_register(entry) < 0) {
75 snd_info_free_entry(entry);
78 card->proc_id = entry;
81 #else /* !CONFIG_PROC_FS */
82 #define init_info_for_card(card)
85 static void snd_card_free_thread(void * __card);
88 * snd_card_new - create and initialize a soundcard structure
89 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
90 * @xid: card identification (ASCII string)
91 * @module: top level module for locking
92 * @extra_size: allocate this extra size after the main soundcard structure
94 * Creates and initializes a soundcard structure.
96 * Returns kmallocated snd_card structure. Creates the ALSA control interface
97 * (which is blocked until snd_card_register function is called).
99 struct snd_card *snd_card_new(int idx, const char *xid,
100 struct module *module, int extra_size)
102 struct snd_card *card;
107 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
111 if (!snd_info_check_reserved_words(xid))
113 strlcpy(card->id, xid, sizeof(card->id));
116 write_lock(&snd_card_rwlock);
119 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
120 if (~snd_cards_lock & idx & 1<<idx2) {
122 if (idx >= snd_ecards_limit)
123 snd_ecards_limit = idx + 1;
126 } else if (idx < snd_ecards_limit) {
127 if (snd_cards_lock & (1 << idx))
128 err = -ENODEV; /* invalid */
129 } else if (idx < SNDRV_CARDS)
130 snd_ecards_limit = idx + 1; /* increase the limit */
133 if (idx < 0 || err < 0) {
134 write_unlock(&snd_card_rwlock);
135 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
138 snd_cards_lock |= 1 << idx; /* lock it */
139 write_unlock(&snd_card_rwlock);
141 card->module = module;
142 INIT_LIST_HEAD(&card->devices);
143 init_rwsem(&card->controls_rwsem);
144 rwlock_init(&card->ctl_files_rwlock);
145 INIT_LIST_HEAD(&card->controls);
146 INIT_LIST_HEAD(&card->ctl_files);
147 spin_lock_init(&card->files_lock);
148 init_waitqueue_head(&card->shutdown_sleep);
149 INIT_WORK(&card->free_workq, snd_card_free_thread, card);
151 mutex_init(&card->power_lock);
152 init_waitqueue_head(&card->power_sleep);
154 /* the control interface cannot be accessed from the user space until */
155 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
156 if ((err = snd_ctl_create(card)) < 0) {
157 snd_printd("unable to register control minors\n");
160 if ((err = snd_info_card_create(card)) < 0) {
161 snd_printd("unable to create card info\n");
165 card->private_data = (char *)card + sizeof(struct snd_card);
169 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
175 EXPORT_SYMBOL(snd_card_new);
177 static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
182 static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
183 size_t count, loff_t *offset)
188 static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
189 size_t count, loff_t *offset)
194 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
196 return POLLERR | POLLNVAL;
199 static long snd_disconnect_ioctl(struct file *file,
200 unsigned int cmd, unsigned long arg)
205 static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
210 static int snd_disconnect_fasync(int fd, struct file *file, int on)
216 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
217 * @card: soundcard structure
219 * Disconnects all APIs from the file-operations (user space).
221 * Returns zero, otherwise a negative error code.
223 * Note: The current implementation replaces all active file->f_op with special
224 * dummy file operations (they do nothing except release).
226 int snd_card_disconnect(struct snd_card *card)
228 struct snd_monitor_file *mfile;
230 struct snd_shutdown_f_ops *s_f_ops;
231 struct file_operations *f_ops;
232 const struct file_operations *old_f_ops;
235 spin_lock(&card->files_lock);
236 if (card->shutdown) {
237 spin_unlock(&card->files_lock);
241 spin_unlock(&card->files_lock);
243 /* phase 1: disable fops (user space) operations for ALSA API */
244 write_lock(&snd_card_rwlock);
245 snd_cards[card->number] = NULL;
246 write_unlock(&snd_card_rwlock);
248 /* phase 2: replace file->f_op with special dummy operations */
250 spin_lock(&card->files_lock);
255 /* it's critical part, use endless loop */
256 /* we have no room to fail */
257 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
259 panic("Atomic allocation failed for snd_shutdown_f_ops!");
261 f_ops = &s_f_ops->f_ops;
263 memset(f_ops, 0, sizeof(*f_ops));
264 f_ops->owner = file->f_op->owner;
265 f_ops->release = file->f_op->release;
266 f_ops->llseek = snd_disconnect_llseek;
267 f_ops->read = snd_disconnect_read;
268 f_ops->write = snd_disconnect_write;
269 f_ops->poll = snd_disconnect_poll;
270 f_ops->unlocked_ioctl = snd_disconnect_ioctl;
272 f_ops->compat_ioctl = snd_disconnect_ioctl;
274 f_ops->mmap = snd_disconnect_mmap;
275 f_ops->fasync = snd_disconnect_fasync;
277 s_f_ops->next = card->s_f_ops;
278 card->s_f_ops = s_f_ops;
280 f_ops = fops_get(f_ops);
282 old_f_ops = file->f_op;
283 file->f_op = f_ops; /* must be atomic */
288 spin_unlock(&card->files_lock);
290 /* phase 3: notify all connected devices about disconnection */
291 /* at this point, they cannot respond to any calls except release() */
293 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
294 if (snd_mixer_oss_notify_callback)
295 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
298 /* notify all devices that we are disconnected */
299 err = snd_device_disconnect_all(card);
301 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
306 EXPORT_SYMBOL(snd_card_disconnect);
309 * snd_card_free - frees given soundcard structure
310 * @card: soundcard structure
312 * This function releases the soundcard structure and the all assigned
313 * devices automatically. That is, you don't have to release the devices
316 * Returns zero. Frees all associated devices and frees the control
317 * interface associated to given soundcard.
319 int snd_card_free(struct snd_card *card)
321 struct snd_shutdown_f_ops *s_f_ops;
325 write_lock(&snd_card_rwlock);
326 snd_cards[card->number] = NULL;
327 write_unlock(&snd_card_rwlock);
330 wake_up(&card->power_sleep);
332 /* wait, until all devices are ready for the free operation */
333 wait_event(card->shutdown_sleep, card->files == NULL);
335 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
336 if (snd_mixer_oss_notify_callback)
337 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
339 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
340 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
341 /* Fatal, but this situation should never occur */
343 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
344 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
345 /* Fatal, but this situation should never occur */
347 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
348 snd_printk(KERN_ERR "unable to free all devices (post)\n");
349 /* Fatal, but this situation should never occur */
351 if (card->private_free)
352 card->private_free(card);
353 snd_info_unregister(card->proc_id);
354 if (snd_info_card_free(card) < 0) {
355 snd_printk(KERN_WARNING "unable to free card info\n");
356 /* Not fatal error */
358 while (card->s_f_ops) {
359 s_f_ops = card->s_f_ops;
360 card->s_f_ops = s_f_ops->next;
363 write_lock(&snd_card_rwlock);
364 snd_cards_lock &= ~(1 << card->number);
365 write_unlock(&snd_card_rwlock);
370 EXPORT_SYMBOL(snd_card_free);
372 static void snd_card_free_thread(void * __card)
374 struct snd_card *card = __card;
375 struct module * module = card->module;
377 if (!try_module_get(module)) {
378 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
388 * snd_card_free_in_thread - call snd_card_free() in thread
389 * @card: soundcard structure
391 * This function schedules the call of snd_card_free() function in a
392 * work queue. When all devices are released (non-busy), the work
393 * is woken up and calls snd_card_free().
395 * When a card can be disconnected at any time by hotplug service,
396 * this function should be used in disconnect (or detach) callback
397 * instead of calling snd_card_free() directly.
399 * Returns - zero otherwise a negative error code if the start of thread failed.
401 int snd_card_free_in_thread(struct snd_card *card)
403 if (card->files == NULL) {
408 if (schedule_work(&card->free_workq))
411 snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
412 /* try to free the structure immediately */
417 EXPORT_SYMBOL(snd_card_free_in_thread);
419 static void choose_default_id(struct snd_card *card)
421 int i, len, idx_flag = 0, loops = SNDRV_CARDS;
424 id = spos = card->shortname;
425 while (*id != '\0') {
431 while (*spos != '\0' && !isalnum(*spos))
434 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
435 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
445 strcpy(id, "default");
449 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
450 strcpy(card->id, card->proc_root->name);
453 if (!snd_info_check_reserved_words(id))
455 for (i = 0; i < snd_ecards_limit; i++) {
456 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
464 if (id[len-1] != '9')
468 } else if ((size_t)len <= sizeof(card->id) - 3) {
473 if ((size_t)len <= sizeof(card->id) - 2)
484 * snd_card_register - register the soundcard
485 * @card: soundcard structure
487 * This function registers all the devices assigned to the soundcard.
488 * Until calling this, the ALSA control interface is blocked from the
489 * external accesses. Thus, you should call this function at the end
490 * of the initialization of the card.
492 * Returns zero otherwise a negative error code if the registrain failed.
494 int snd_card_register(struct snd_card *card)
498 snd_assert(card != NULL, return -EINVAL);
499 if ((err = snd_device_register_all(card)) < 0)
501 write_lock(&snd_card_rwlock);
502 if (snd_cards[card->number]) {
503 /* already registered */
504 write_unlock(&snd_card_rwlock);
507 if (card->id[0] == '\0')
508 choose_default_id(card);
509 snd_cards[card->number] = card;
510 write_unlock(&snd_card_rwlock);
511 init_info_for_card(card);
512 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
513 if (snd_mixer_oss_notify_callback)
514 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
519 EXPORT_SYMBOL(snd_card_register);
521 #ifdef CONFIG_PROC_FS
522 static struct snd_info_entry *snd_card_info_entry = NULL;
524 static void snd_card_info_read(struct snd_info_entry *entry,
525 struct snd_info_buffer *buffer)
528 struct snd_card *card;
530 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
531 read_lock(&snd_card_rwlock);
532 if ((card = snd_cards[idx]) != NULL) {
534 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
539 snd_iprintf(buffer, " %s\n",
542 read_unlock(&snd_card_rwlock);
545 snd_iprintf(buffer, "--- no soundcards ---\n");
548 #ifdef CONFIG_SND_OSSEMUL
550 void snd_card_info_read_oss(struct snd_info_buffer *buffer)
553 struct snd_card *card;
555 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
556 read_lock(&snd_card_rwlock);
557 if ((card = snd_cards[idx]) != NULL) {
559 snd_iprintf(buffer, "%s\n", card->longname);
561 read_unlock(&snd_card_rwlock);
564 snd_iprintf(buffer, "--- no soundcards ---\n");
571 static struct snd_info_entry *snd_card_module_info_entry;
572 static void snd_card_module_info_read(struct snd_info_entry *entry,
573 struct snd_info_buffer *buffer)
576 struct snd_card *card;
578 for (idx = 0; idx < SNDRV_CARDS; idx++) {
579 read_lock(&snd_card_rwlock);
580 if ((card = snd_cards[idx]) != NULL)
581 snd_iprintf(buffer, "%2i %s\n",
582 idx, card->module->name);
583 read_unlock(&snd_card_rwlock);
588 int __init snd_card_info_init(void)
590 struct snd_info_entry *entry;
592 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
595 entry->c.text.read_size = PAGE_SIZE;
596 entry->c.text.read = snd_card_info_read;
597 if (snd_info_register(entry) < 0) {
598 snd_info_free_entry(entry);
601 snd_card_info_entry = entry;
604 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
606 entry->c.text.read_size = PAGE_SIZE;
607 entry->c.text.read = snd_card_module_info_read;
608 if (snd_info_register(entry) < 0)
609 snd_info_free_entry(entry);
611 snd_card_module_info_entry = entry;
618 int __exit snd_card_info_done(void)
620 snd_info_unregister(snd_card_info_entry);
622 snd_info_unregister(snd_card_module_info_entry);
627 #endif /* CONFIG_PROC_FS */
630 * snd_component_add - add a component string
631 * @card: soundcard structure
632 * @component: the component id string
634 * This function adds the component id string to the supported list.
635 * The component can be referred from the alsa-lib.
637 * Returns zero otherwise a negative error code.
640 int snd_component_add(struct snd_card *card, const char *component)
643 int len = strlen(component);
645 ptr = strstr(card->components, component);
647 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
650 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
654 if (card->components[0] != '\0')
655 strcat(card->components, " ");
656 strcat(card->components, component);
660 EXPORT_SYMBOL(snd_component_add);
663 * snd_card_file_add - add the file to the file list of the card
664 * @card: soundcard structure
665 * @file: file pointer
667 * This function adds the file to the file linked-list of the card.
668 * This linked-list is used to keep tracking the connection state,
669 * and to avoid the release of busy resources by hotplug.
671 * Returns zero or a negative error code.
673 int snd_card_file_add(struct snd_card *card, struct file *file)
675 struct snd_monitor_file *mfile;
677 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
682 spin_lock(&card->files_lock);
683 if (card->shutdown) {
684 spin_unlock(&card->files_lock);
688 mfile->next = card->files;
690 spin_unlock(&card->files_lock);
694 EXPORT_SYMBOL(snd_card_file_add);
697 * snd_card_file_remove - remove the file from the file list
698 * @card: soundcard structure
699 * @file: file pointer
701 * This function removes the file formerly added to the card via
702 * snd_card_file_add() function.
703 * If all files are removed and the release of the card is
704 * scheduled, it will wake up the the thread to call snd_card_free()
705 * (see snd_card_free_in_thread() function).
707 * Returns zero or a negative error code.
709 int snd_card_file_remove(struct snd_card *card, struct file *file)
711 struct snd_monitor_file *mfile, *pfile = NULL;
713 spin_lock(&card->files_lock);
716 if (mfile->file == file) {
718 pfile->next = mfile->next;
720 card->files = mfile->next;
726 spin_unlock(&card->files_lock);
727 if (card->files == NULL)
728 wake_up(&card->shutdown_sleep);
730 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
737 EXPORT_SYMBOL(snd_card_file_remove);
741 * snd_power_wait - wait until the power-state is changed.
742 * @card: soundcard structure
743 * @power_state: expected power state
745 * Waits until the power-state is changed.
747 * Note: the power lock must be active before call.
749 int snd_power_wait(struct snd_card *card, unsigned int power_state)
755 if (snd_power_get_state(card) == power_state)
757 init_waitqueue_entry(&wait, current);
758 add_wait_queue(&card->power_sleep, &wait);
760 if (card->shutdown) {
764 if (snd_power_get_state(card) == power_state)
766 set_current_state(TASK_UNINTERRUPTIBLE);
767 snd_power_unlock(card);
768 schedule_timeout(30 * HZ);
769 snd_power_lock(card);
771 remove_wait_queue(&card->power_sleep, &wait);
775 EXPORT_SYMBOL(snd_power_wait);
776 #endif /* CONFIG_PM */