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>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
35 struct snd_shutdown_f_ops {
36 struct file_operations f_ops;
37 struct snd_shutdown_f_ops *next;
40 unsigned int snd_cards_lock = 0; /* locked for registering/using */
41 snd_card_t *snd_cards[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = NULL};
42 rwlock_t snd_card_rwlock = RW_LOCK_UNLOCKED;
44 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
45 int (*snd_mixer_oss_notify_callback)(snd_card_t *card, int free_flag);
48 static void snd_card_id_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
50 snd_iprintf(buffer, "%s\n", entry->card->id);
53 static void snd_card_free_thread(void * __card);
56 * snd_card_new - create and initialize a soundcard structure
57 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
58 * @xid: card identification (ASCII string)
59 * @module: top level module for locking
60 * @extra_size: allocate this extra size after the main soundcard structure
62 * Creates and initializes a soundcard structure.
64 * Returns kmallocated snd_card_t structure. Creates the ALSA control interface
65 * (which is blocked until snd_card_register function is called).
67 snd_card_t *snd_card_new(int idx, const char *xid,
68 struct module *module, int extra_size)
75 card = kcalloc(1, sizeof(*card) + extra_size, GFP_KERNEL);
79 if (!snd_info_check_reserved_words(xid))
81 strlcpy(card->id, xid, sizeof(card->id));
84 write_lock(&snd_card_rwlock);
87 for (idx2 = 0; idx2 < snd_ecards_limit; idx2++)
88 if (!(snd_cards_lock & (1 << idx2))) {
92 if (idx < 0 && snd_ecards_limit < SNDRV_CARDS)
93 /* for dynamically additional devices like hotplug:
94 * increment the limit if still free slot exists.
96 idx = snd_ecards_limit++;
97 } else if (idx < snd_ecards_limit) {
98 if (snd_cards_lock & (1 << idx))
99 err = -ENODEV; /* invalid */
100 } else if (idx < SNDRV_CARDS)
101 snd_ecards_limit = idx + 1; /* increase the limit */
104 if (idx < 0 || err < 0) {
105 write_unlock(&snd_card_rwlock);
106 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
109 snd_cards_lock |= 1 << idx; /* lock it */
110 write_unlock(&snd_card_rwlock);
112 card->module = module;
113 INIT_LIST_HEAD(&card->devices);
114 init_rwsem(&card->controls_rwsem);
115 rwlock_init(&card->ctl_files_rwlock);
116 INIT_LIST_HEAD(&card->controls);
117 INIT_LIST_HEAD(&card->ctl_files);
118 spin_lock_init(&card->files_lock);
119 init_waitqueue_head(&card->shutdown_sleep);
120 INIT_WORK(&card->free_workq, snd_card_free_thread, card);
122 init_MUTEX(&card->power_lock);
123 init_waitqueue_head(&card->power_sleep);
125 /* the control interface cannot be accessed from the user space until */
126 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
127 if ((err = snd_ctl_create(card)) < 0) {
128 snd_printd("unable to register control minors\n");
131 if ((err = snd_info_card_create(card)) < 0) {
132 snd_printd("unable to create card info\n");
136 card->private_data = (char *)card + sizeof(snd_card_t);
140 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
146 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
148 return POLLERR | POLLNVAL;
152 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
153 * @card: soundcard structure
155 * Disconnects all APIs from the file-operations (user space).
157 * Returns zero, otherwise a negative error code.
159 * Note: The current implementation replaces all active file->f_op with special
160 * dummy file operations (they do nothing except release).
162 int snd_card_disconnect(snd_card_t * card)
164 struct snd_monitor_file *mfile;
166 struct snd_shutdown_f_ops *s_f_ops;
167 struct file_operations *f_ops, *old_f_ops;
170 spin_lock(&card->files_lock);
171 if (card->shutdown) {
172 spin_unlock(&card->files_lock);
176 spin_unlock(&card->files_lock);
178 /* phase 1: disable fops (user space) operations for ALSA API */
179 write_lock(&snd_card_rwlock);
180 snd_cards[card->number] = NULL;
181 write_unlock(&snd_card_rwlock);
183 /* phase 2: replace file->f_op with special dummy operations */
185 spin_lock(&card->files_lock);
190 /* it's critical part, use endless loop */
191 /* we have no room to fail */
192 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
194 panic("Atomic allocation failed for snd_shutdown_f_ops!");
196 f_ops = &s_f_ops->f_ops;
198 memset(f_ops, 0, sizeof(*f_ops));
199 f_ops->owner = file->f_op->owner;
200 f_ops->release = file->f_op->release;
201 f_ops->poll = snd_disconnect_poll;
203 s_f_ops->next = card->s_f_ops;
204 card->s_f_ops = s_f_ops;
206 f_ops = fops_get(f_ops);
208 old_f_ops = file->f_op;
209 file->f_op = f_ops; /* must be atomic */
214 spin_unlock(&card->files_lock);
216 /* phase 3: notify all connected devices about disconnection */
217 /* at this point, they cannot respond to any calls except release() */
219 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
220 if (snd_mixer_oss_notify_callback)
221 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
224 /* notify all devices that we are disconnected */
225 err = snd_device_disconnect_all(card);
227 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
233 * snd_card_free - frees given soundcard structure
234 * @card: soundcard structure
236 * This function releases the soundcard structure and the all assigned
237 * devices automatically. That is, you don't have to release the devices
240 * Returns zero. Frees all associated devices and frees the control
241 * interface associated to given soundcard.
243 int snd_card_free(snd_card_t * card)
245 struct snd_shutdown_f_ops *s_f_ops;
249 write_lock(&snd_card_rwlock);
250 snd_cards[card->number] = NULL;
251 write_unlock(&snd_card_rwlock);
254 wake_up(&card->power_sleep);
257 pm_unregister(card->pm_dev);
263 /* wait, until all devices are ready for the free operation */
264 wait_event(card->shutdown_sleep, card->files == NULL);
266 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
267 if (snd_mixer_oss_notify_callback)
268 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
270 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
271 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
272 /* Fatal, but this situation should never occur */
274 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
275 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
276 /* Fatal, but this situation should never occur */
278 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
279 snd_printk(KERN_ERR "unable to free all devices (post)\n");
280 /* Fatal, but this situation should never occur */
282 if (card->private_free)
283 card->private_free(card);
285 snd_info_unregister(card->proc_id);
286 if (snd_info_card_free(card) < 0) {
287 snd_printk(KERN_WARNING "unable to free card info\n");
288 /* Not fatal error */
290 while (card->s_f_ops) {
291 s_f_ops = card->s_f_ops;
292 card->s_f_ops = s_f_ops->next;
295 write_lock(&snd_card_rwlock);
296 snd_cards_lock &= ~(1 << card->number);
297 write_unlock(&snd_card_rwlock);
302 static void snd_card_free_thread(void * __card)
304 snd_card_t *card = __card;
305 struct module * module = card->module;
307 if (!try_module_get(module)) {
308 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
318 * snd_card_free_in_thread - call snd_card_free() in thread
319 * @card: soundcard structure
321 * This function schedules the call of snd_card_free() function in a
322 * work queue. When all devices are released (non-busy), the work
323 * is woken up and calls snd_card_free().
325 * When a card can be disconnected at any time by hotplug service,
326 * this function should be used in disconnect (or detach) callback
327 * instead of calling snd_card_free() directly.
329 * Returns - zero otherwise a negative error code if the start of thread failed.
331 int snd_card_free_in_thread(snd_card_t * card)
333 if (card->files == NULL) {
338 if (schedule_work(&card->free_workq))
341 snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
342 /* try to free the structure immediately */
347 static void choose_default_id(snd_card_t * card)
349 int i, len, idx_flag = 0, loops = 8;
352 id = spos = card->shortname;
353 while (*id != '\0') {
359 while (*spos != '\0' && !isalnum(*spos))
362 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
363 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
373 strcpy(id, "default");
377 snd_printk(KERN_ERR "unable to choose default card id (%s)", id);
378 strcpy(card->id, card->proc_root->name);
381 if (!snd_info_check_reserved_words(id))
383 for (i = 0; i < snd_ecards_limit; i++) {
384 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
393 else if ((size_t)len <= sizeof(card->id) - 3) {
398 if ((size_t)len <= sizeof(card->id) - 2)
409 * snd_card_register - register the soundcard
410 * @card: soundcard structure
412 * This function registers all the devices assigned to the soundcard.
413 * Until calling this, the ALSA control interface is blocked from the
414 * external accesses. Thus, you should call this function at the end
415 * of the initialization of the card.
417 * Returns zero otherwise a negative error code if the registrain failed.
419 int snd_card_register(snd_card_t * card)
422 snd_info_entry_t *entry;
424 snd_runtime_check(card != NULL, return -EINVAL);
425 if ((err = snd_device_register_all(card)) < 0)
427 write_lock(&snd_card_rwlock);
428 if (snd_cards[card->number]) {
429 /* already registered */
430 write_unlock(&snd_card_rwlock);
433 if (card->id[0] == '\0')
434 choose_default_id(card);
435 snd_cards[card->number] = card;
436 write_unlock(&snd_card_rwlock);
437 if ((err = snd_info_card_register(card)) < 0) {
438 snd_printd("unable to create card info\n");
441 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
442 snd_printd("unable to create card entry\n");
445 entry->c.text.read_size = PAGE_SIZE;
446 entry->c.text.read = snd_card_id_read;
447 if (snd_info_register(entry) < 0) {
448 snd_info_free_entry(entry);
451 card->proc_id = entry;
453 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
454 if (snd_mixer_oss_notify_callback)
455 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
460 static snd_info_entry_t *snd_card_info_entry = NULL;
462 static void snd_card_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
467 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
468 read_lock(&snd_card_rwlock);
469 if ((card = snd_cards[idx]) != NULL) {
471 snd_iprintf(buffer, "%i [%-15s]: %s - %s\n",
476 snd_iprintf(buffer, " %s\n",
479 read_unlock(&snd_card_rwlock);
482 snd_iprintf(buffer, "--- no soundcards ---\n");
485 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
487 void snd_card_info_read_oss(snd_info_buffer_t * buffer)
492 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
493 read_lock(&snd_card_rwlock);
494 if ((card = snd_cards[idx]) != NULL) {
496 snd_iprintf(buffer, "%s\n", card->longname);
498 read_unlock(&snd_card_rwlock);
501 snd_iprintf(buffer, "--- no soundcards ---\n");
508 static snd_info_entry_t *snd_card_module_info_entry;
509 static void snd_card_module_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
514 for (idx = 0; idx < SNDRV_CARDS; idx++) {
515 read_lock(&snd_card_rwlock);
516 if ((card = snd_cards[idx]) != NULL)
517 snd_iprintf(buffer, "%i %s\n", idx, card->module->name);
518 read_unlock(&snd_card_rwlock);
523 int __init snd_card_info_init(void)
525 snd_info_entry_t *entry;
527 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
528 snd_runtime_check(entry != NULL, return -ENOMEM);
529 entry->c.text.read_size = PAGE_SIZE;
530 entry->c.text.read = snd_card_info_read;
531 if (snd_info_register(entry) < 0) {
532 snd_info_free_entry(entry);
535 snd_card_info_entry = entry;
538 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
540 entry->c.text.read_size = PAGE_SIZE;
541 entry->c.text.read = snd_card_module_info_read;
542 if (snd_info_register(entry) < 0)
543 snd_info_free_entry(entry);
545 snd_card_module_info_entry = entry;
552 int __exit snd_card_info_done(void)
554 if (snd_card_info_entry)
555 snd_info_unregister(snd_card_info_entry);
557 if (snd_card_module_info_entry)
558 snd_info_unregister(snd_card_module_info_entry);
564 * snd_component_add - add a component string
565 * @card: soundcard structure
566 * @component: the component id string
568 * This function adds the component id string to the supported list.
569 * The component can be referred from the alsa-lib.
571 * Returns zero otherwise a negative error code.
574 int snd_component_add(snd_card_t *card, const char *component)
577 int len = strlen(component);
579 ptr = strstr(card->components, component);
581 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
584 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
588 if (card->components[0] != '\0')
589 strcat(card->components, " ");
590 strcat(card->components, component);
595 * snd_card_file_add - add the file to the file list of the card
596 * @card: soundcard structure
597 * @file: file pointer
599 * This function adds the file to the file linked-list of the card.
600 * This linked-list is used to keep tracking the connection state,
601 * and to avoid the release of busy resources by hotplug.
603 * Returns zero or a negative error code.
605 int snd_card_file_add(snd_card_t *card, struct file *file)
607 struct snd_monitor_file *mfile;
609 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
614 spin_lock(&card->files_lock);
615 if (card->shutdown) {
616 spin_unlock(&card->files_lock);
620 mfile->next = card->files;
622 spin_unlock(&card->files_lock);
627 * snd_card_file_remove - remove the file from the file list
628 * @card: soundcard structure
629 * @file: file pointer
631 * This function removes the file formerly added to the card via
632 * snd_card_file_add() function.
633 * If all files are removed and the release of the card is
634 * scheduled, it will wake up the the thread to call snd_card_free()
635 * (see snd_card_free_in_thread() function).
637 * Returns zero or a negative error code.
639 int snd_card_file_remove(snd_card_t *card, struct file *file)
641 struct snd_monitor_file *mfile, *pfile = NULL;
643 spin_lock(&card->files_lock);
646 if (mfile->file == file) {
648 pfile->next = mfile->next;
650 card->files = mfile->next;
656 spin_unlock(&card->files_lock);
657 if (card->files == NULL)
658 wake_up(&card->shutdown_sleep);
660 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
669 * snd_power_wait - wait until the power-state is changed.
670 * @card: soundcard structure
671 * @power_state: expected power state
672 * @file: file structure for the O_NONBLOCK check (optional)
674 * Waits until the power-state is changed.
676 * Note: the power lock must be active before call.
678 int snd_power_wait(snd_card_t *card, unsigned int power_state, struct file *file)
684 if (snd_power_get_state(card) == power_state)
686 init_waitqueue_entry(&wait, current);
687 add_wait_queue(&card->power_sleep, &wait);
689 if (card->shutdown) {
693 if (snd_power_get_state(card) == power_state)
695 #if 0 /* block all devices */
696 if (file && (file->f_flags & O_NONBLOCK)) {
701 set_current_state(TASK_UNINTERRUPTIBLE);
702 snd_power_unlock(card);
703 schedule_timeout(30 * HZ);
704 snd_power_lock(card);
706 remove_wait_queue(&card->power_sleep, &wait);
711 * snd_card_set_pm_callback - set the PCI power-management callbacks
712 * @card: soundcard structure
713 * @suspend: suspend callback function
714 * @resume: resume callback function
715 * @private_data: private data to pass to the callback functions
717 * Sets the power-management callback functions of the card.
718 * These callbacks are called from ALSA's common PCI suspend/resume
719 * handler and from the control API.
721 int snd_card_set_pm_callback(snd_card_t *card,
722 int (*suspend)(snd_card_t *, unsigned int),
723 int (*resume)(snd_card_t *, unsigned int),
726 card->pm_suspend = suspend;
727 card->pm_resume = resume;
728 card->pm_private_data = private_data;
732 static int snd_generic_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
734 snd_card_t *card = dev->data;
738 if (card->power_state == SNDRV_CTL_POWER_D3hot)
740 /* FIXME: the correct state value? */
741 card->pm_suspend(card, 0);
742 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
745 if (card->power_state == SNDRV_CTL_POWER_D0)
747 /* FIXME: the correct state value? */
748 card->pm_resume(card, 0);
749 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
756 * snd_card_set_dev_pm_callback - set the generic power-management callbacks
757 * @card: soundcard structure
758 * @type: PM device type (PM_XXX)
759 * @suspend: suspend callback function
760 * @resume: resume callback function
761 * @private_data: private data to pass to the callback functions
763 * Registers the power-management and sets the lowlevel callbacks for
764 * the given card with the given PM type. These callbacks are called
765 * from the ALSA's common PM handler and from the control API.
767 int snd_card_set_dev_pm_callback(snd_card_t *card, int type,
768 int (*suspend)(snd_card_t *, unsigned int),
769 int (*resume)(snd_card_t *, unsigned int),
772 card->pm_dev = pm_register(type, 0, snd_generic_pm_callback);
775 card->pm_dev->data = card;
776 snd_card_set_pm_callback(card, suspend, resume, private_data);
781 int snd_card_pci_suspend(struct pci_dev *dev, u32 state)
783 snd_card_t *card = pci_get_drvdata(dev);
785 if (! card || ! card->pm_suspend)
787 if (card->power_state == SNDRV_CTL_POWER_D3hot)
789 /* FIXME: correct state value? */
790 err = card->pm_suspend(card, 0);
792 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
796 int snd_card_pci_resume(struct pci_dev *dev)
798 snd_card_t *card = pci_get_drvdata(dev);
799 if (! card || ! card->pm_resume)
801 if (card->power_state == SNDRV_CTL_POWER_D0)
803 /* restore the PCI config space */
804 pci_restore_state(dev);
805 /* FIXME: correct state value? */
806 card->pm_resume(card, 0);
807 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
812 #endif /* CONFIG_PM */