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 DEFINE_RWLOCK(snd_card_rwlock);
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 < SNDRV_CARDS; idx2++)
88 if (~snd_cards_lock & idx & 1<<idx2) {
90 if (idx >= snd_ecards_limit)
91 snd_ecards_limit = idx + 1;
94 } else if (idx < snd_ecards_limit) {
95 if (snd_cards_lock & (1 << idx))
96 err = -ENODEV; /* invalid */
97 } else if (idx < SNDRV_CARDS)
98 snd_ecards_limit = idx + 1; /* increase the limit */
101 if (idx < 0 || err < 0) {
102 write_unlock(&snd_card_rwlock);
103 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
106 snd_cards_lock |= 1 << idx; /* lock it */
107 write_unlock(&snd_card_rwlock);
109 card->module = module;
110 INIT_LIST_HEAD(&card->devices);
111 init_rwsem(&card->controls_rwsem);
112 rwlock_init(&card->ctl_files_rwlock);
113 INIT_LIST_HEAD(&card->controls);
114 INIT_LIST_HEAD(&card->ctl_files);
115 spin_lock_init(&card->files_lock);
116 init_waitqueue_head(&card->shutdown_sleep);
117 INIT_WORK(&card->free_workq, snd_card_free_thread, card);
119 init_MUTEX(&card->power_lock);
120 init_waitqueue_head(&card->power_sleep);
122 /* the control interface cannot be accessed from the user space until */
123 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
124 if ((err = snd_ctl_create(card)) < 0) {
125 snd_printd("unable to register control minors\n");
128 if ((err = snd_info_card_create(card)) < 0) {
129 snd_printd("unable to create card info\n");
133 card->private_data = (char *)card + sizeof(snd_card_t);
137 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
143 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
145 return POLLERR | POLLNVAL;
149 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
150 * @card: soundcard structure
152 * Disconnects all APIs from the file-operations (user space).
154 * Returns zero, otherwise a negative error code.
156 * Note: The current implementation replaces all active file->f_op with special
157 * dummy file operations (they do nothing except release).
159 int snd_card_disconnect(snd_card_t * card)
161 struct snd_monitor_file *mfile;
163 struct snd_shutdown_f_ops *s_f_ops;
164 struct file_operations *f_ops, *old_f_ops;
167 spin_lock(&card->files_lock);
168 if (card->shutdown) {
169 spin_unlock(&card->files_lock);
173 spin_unlock(&card->files_lock);
175 /* phase 1: disable fops (user space) operations for ALSA API */
176 write_lock(&snd_card_rwlock);
177 snd_cards[card->number] = NULL;
178 write_unlock(&snd_card_rwlock);
180 /* phase 2: replace file->f_op with special dummy operations */
182 spin_lock(&card->files_lock);
187 /* it's critical part, use endless loop */
188 /* we have no room to fail */
189 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
191 panic("Atomic allocation failed for snd_shutdown_f_ops!");
193 f_ops = &s_f_ops->f_ops;
195 memset(f_ops, 0, sizeof(*f_ops));
196 f_ops->owner = file->f_op->owner;
197 f_ops->release = file->f_op->release;
198 f_ops->poll = snd_disconnect_poll;
200 s_f_ops->next = card->s_f_ops;
201 card->s_f_ops = s_f_ops;
203 f_ops = fops_get(f_ops);
205 old_f_ops = file->f_op;
206 file->f_op = f_ops; /* must be atomic */
211 spin_unlock(&card->files_lock);
213 /* phase 3: notify all connected devices about disconnection */
214 /* at this point, they cannot respond to any calls except release() */
216 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
217 if (snd_mixer_oss_notify_callback)
218 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
221 /* notify all devices that we are disconnected */
222 err = snd_device_disconnect_all(card);
224 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
229 #if defined(CONFIG_PM) && defined(CONFIG_SND_GENERIC_PM)
230 static void snd_generic_device_unregister(struct snd_generic_device *dev);
234 * snd_card_free - frees given soundcard structure
235 * @card: soundcard structure
237 * This function releases the soundcard structure and the all assigned
238 * devices automatically. That is, you don't have to release the devices
241 * Returns zero. Frees all associated devices and frees the control
242 * interface associated to given soundcard.
244 int snd_card_free(snd_card_t * card)
246 struct snd_shutdown_f_ops *s_f_ops;
250 write_lock(&snd_card_rwlock);
251 snd_cards[card->number] = NULL;
252 write_unlock(&snd_card_rwlock);
255 wake_up(&card->power_sleep);
256 #ifdef CONFIG_SND_GENERIC_PM
258 snd_generic_device_unregister(card->pm_dev);
264 /* wait, until all devices are ready for the free operation */
265 wait_event(card->shutdown_sleep, card->files == NULL);
267 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
268 if (snd_mixer_oss_notify_callback)
269 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
271 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
272 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
273 /* Fatal, but this situation should never occur */
275 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
276 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
277 /* Fatal, but this situation should never occur */
279 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
280 snd_printk(KERN_ERR "unable to free all devices (post)\n");
281 /* Fatal, but this situation should never occur */
283 if (card->private_free)
284 card->private_free(card);
286 snd_info_unregister(card->proc_id);
287 if (snd_info_card_free(card) < 0) {
288 snd_printk(KERN_WARNING "unable to free card info\n");
289 /* Not fatal error */
291 while (card->s_f_ops) {
292 s_f_ops = card->s_f_ops;
293 card->s_f_ops = s_f_ops->next;
296 write_lock(&snd_card_rwlock);
297 snd_cards_lock &= ~(1 << card->number);
298 write_unlock(&snd_card_rwlock);
303 static void snd_card_free_thread(void * __card)
305 snd_card_t *card = __card;
306 struct module * module = card->module;
308 if (!try_module_get(module)) {
309 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
319 * snd_card_free_in_thread - call snd_card_free() in thread
320 * @card: soundcard structure
322 * This function schedules the call of snd_card_free() function in a
323 * work queue. When all devices are released (non-busy), the work
324 * is woken up and calls snd_card_free().
326 * When a card can be disconnected at any time by hotplug service,
327 * this function should be used in disconnect (or detach) callback
328 * instead of calling snd_card_free() directly.
330 * Returns - zero otherwise a negative error code if the start of thread failed.
332 int snd_card_free_in_thread(snd_card_t * card)
334 if (card->files == NULL) {
339 if (schedule_work(&card->free_workq))
342 snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
343 /* try to free the structure immediately */
348 static void choose_default_id(snd_card_t * card)
350 int i, len, idx_flag = 0, loops = 8;
353 id = spos = card->shortname;
354 while (*id != '\0') {
360 while (*spos != '\0' && !isalnum(*spos))
363 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
364 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
374 strcpy(id, "default");
378 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
379 strcpy(card->id, card->proc_root->name);
382 if (!snd_info_check_reserved_words(id))
384 for (i = 0; i < snd_ecards_limit; i++) {
385 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
394 else if ((size_t)len <= sizeof(card->id) - 3) {
399 if ((size_t)len <= sizeof(card->id) - 2)
410 * snd_card_register - register the soundcard
411 * @card: soundcard structure
413 * This function registers all the devices assigned to the soundcard.
414 * Until calling this, the ALSA control interface is blocked from the
415 * external accesses. Thus, you should call this function at the end
416 * of the initialization of the card.
418 * Returns zero otherwise a negative error code if the registrain failed.
420 int snd_card_register(snd_card_t * card)
423 snd_info_entry_t *entry;
425 snd_runtime_check(card != NULL, return -EINVAL);
426 if ((err = snd_device_register_all(card)) < 0)
428 write_lock(&snd_card_rwlock);
429 if (snd_cards[card->number]) {
430 /* already registered */
431 write_unlock(&snd_card_rwlock);
434 if (card->id[0] == '\0')
435 choose_default_id(card);
436 snd_cards[card->number] = card;
437 write_unlock(&snd_card_rwlock);
438 if ((err = snd_info_card_register(card)) < 0) {
439 snd_printd("unable to create card info\n");
442 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
443 snd_printd("unable to create card entry\n");
446 entry->c.text.read_size = PAGE_SIZE;
447 entry->c.text.read = snd_card_id_read;
448 if (snd_info_register(entry) < 0) {
449 snd_info_free_entry(entry);
452 card->proc_id = entry;
454 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
455 if (snd_mixer_oss_notify_callback)
456 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
461 static snd_info_entry_t *snd_card_info_entry = NULL;
463 static void snd_card_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
468 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
469 read_lock(&snd_card_rwlock);
470 if ((card = snd_cards[idx]) != NULL) {
472 snd_iprintf(buffer, "%i [%-15s]: %s - %s\n",
477 snd_iprintf(buffer, " %s\n",
480 read_unlock(&snd_card_rwlock);
483 snd_iprintf(buffer, "--- no soundcards ---\n");
486 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
488 void snd_card_info_read_oss(snd_info_buffer_t * buffer)
493 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
494 read_lock(&snd_card_rwlock);
495 if ((card = snd_cards[idx]) != NULL) {
497 snd_iprintf(buffer, "%s\n", card->longname);
499 read_unlock(&snd_card_rwlock);
502 snd_iprintf(buffer, "--- no soundcards ---\n");
509 static snd_info_entry_t *snd_card_module_info_entry;
510 static void snd_card_module_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
515 for (idx = 0; idx < SNDRV_CARDS; idx++) {
516 read_lock(&snd_card_rwlock);
517 if ((card = snd_cards[idx]) != NULL)
518 snd_iprintf(buffer, "%i %s\n", idx, card->module->name);
519 read_unlock(&snd_card_rwlock);
524 int __init snd_card_info_init(void)
526 snd_info_entry_t *entry;
528 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
529 snd_runtime_check(entry != NULL, return -ENOMEM);
530 entry->c.text.read_size = PAGE_SIZE;
531 entry->c.text.read = snd_card_info_read;
532 if (snd_info_register(entry) < 0) {
533 snd_info_free_entry(entry);
536 snd_card_info_entry = entry;
539 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
541 entry->c.text.read_size = PAGE_SIZE;
542 entry->c.text.read = snd_card_module_info_read;
543 if (snd_info_register(entry) < 0)
544 snd_info_free_entry(entry);
546 snd_card_module_info_entry = entry;
553 int __exit snd_card_info_done(void)
555 if (snd_card_info_entry)
556 snd_info_unregister(snd_card_info_entry);
558 if (snd_card_module_info_entry)
559 snd_info_unregister(snd_card_module_info_entry);
565 * snd_component_add - add a component string
566 * @card: soundcard structure
567 * @component: the component id string
569 * This function adds the component id string to the supported list.
570 * The component can be referred from the alsa-lib.
572 * Returns zero otherwise a negative error code.
575 int snd_component_add(snd_card_t *card, const char *component)
578 int len = strlen(component);
580 ptr = strstr(card->components, component);
582 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
585 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
589 if (card->components[0] != '\0')
590 strcat(card->components, " ");
591 strcat(card->components, component);
596 * snd_card_file_add - add the file to the file list of the card
597 * @card: soundcard structure
598 * @file: file pointer
600 * This function adds the file to the file linked-list of the card.
601 * This linked-list is used to keep tracking the connection state,
602 * and to avoid the release of busy resources by hotplug.
604 * Returns zero or a negative error code.
606 int snd_card_file_add(snd_card_t *card, struct file *file)
608 struct snd_monitor_file *mfile;
610 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
615 spin_lock(&card->files_lock);
616 if (card->shutdown) {
617 spin_unlock(&card->files_lock);
621 mfile->next = card->files;
623 spin_unlock(&card->files_lock);
628 * snd_card_file_remove - remove the file from the file list
629 * @card: soundcard structure
630 * @file: file pointer
632 * This function removes the file formerly added to the card via
633 * snd_card_file_add() function.
634 * If all files are removed and the release of the card is
635 * scheduled, it will wake up the the thread to call snd_card_free()
636 * (see snd_card_free_in_thread() function).
638 * Returns zero or a negative error code.
640 int snd_card_file_remove(snd_card_t *card, struct file *file)
642 struct snd_monitor_file *mfile, *pfile = NULL;
644 spin_lock(&card->files_lock);
647 if (mfile->file == file) {
649 pfile->next = mfile->next;
651 card->files = mfile->next;
657 spin_unlock(&card->files_lock);
658 if (card->files == NULL)
659 wake_up(&card->shutdown_sleep);
661 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
670 * snd_power_wait - wait until the power-state is changed.
671 * @card: soundcard structure
672 * @power_state: expected power state
673 * @file: file structure for the O_NONBLOCK check (optional)
675 * Waits until the power-state is changed.
677 * Note: the power lock must be active before call.
679 int snd_power_wait(snd_card_t *card, unsigned int power_state, struct file *file)
685 if (snd_power_get_state(card) == power_state)
687 init_waitqueue_entry(&wait, current);
688 add_wait_queue(&card->power_sleep, &wait);
690 if (card->shutdown) {
694 if (snd_power_get_state(card) == power_state)
696 #if 0 /* block all devices */
697 if (file && (file->f_flags & O_NONBLOCK)) {
702 set_current_state(TASK_UNINTERRUPTIBLE);
703 snd_power_unlock(card);
704 schedule_timeout(30 * HZ);
705 snd_power_lock(card);
707 remove_wait_queue(&card->power_sleep, &wait);
712 * snd_card_set_pm_callback - set the PCI power-management callbacks
713 * @card: soundcard structure
714 * @suspend: suspend callback function
715 * @resume: resume callback function
716 * @private_data: private data to pass to the callback functions
718 * Sets the power-management callback functions of the card.
719 * These callbacks are called from ALSA's common PCI suspend/resume
720 * handler and from the control API.
722 int snd_card_set_pm_callback(snd_card_t *card,
723 int (*suspend)(snd_card_t *, pm_message_t),
724 int (*resume)(snd_card_t *),
727 card->pm_suspend = suspend;
728 card->pm_resume = resume;
729 card->pm_private_data = private_data;
733 #ifdef CONFIG_SND_GENERIC_PM
735 * use platform_device for generic power-management without a proper bus
738 struct snd_generic_device {
739 struct platform_device pdev;
743 #define get_snd_generic_card(dev) container_of(to_platform_device(dev), struct snd_generic_device, pdev)->card
745 #define SND_GENERIC_NAME "snd_generic_pm"
747 static int snd_generic_suspend(struct device *dev, pm_message_t state, u32 level);
748 static int snd_generic_resume(struct device *dev, u32 level);
750 static struct device_driver snd_generic_driver = {
751 .name = SND_GENERIC_NAME,
752 .bus = &platform_bus_type,
753 .suspend = snd_generic_suspend,
754 .resume = snd_generic_resume,
757 static int generic_driver_registered;
759 static void generic_driver_unregister(void)
761 if (generic_driver_registered) {
762 generic_driver_registered--;
763 if (! generic_driver_registered)
764 driver_unregister(&snd_generic_driver);
768 static struct snd_generic_device *snd_generic_device_register(snd_card_t *card)
770 struct snd_generic_device *dev;
772 if (! generic_driver_registered) {
773 if (driver_register(&snd_generic_driver) < 0)
776 generic_driver_registered++;
778 dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
780 generic_driver_unregister();
784 dev->pdev.name = SND_GENERIC_NAME;
785 dev->pdev.id = card->number;
787 if (platform_device_register(&dev->pdev) < 0) {
789 generic_driver_unregister();
795 static void snd_generic_device_unregister(struct snd_generic_device *dev)
797 platform_device_unregister(&dev->pdev);
799 generic_driver_unregister();
802 /* suspend/resume callbacks for snd_generic platform device */
803 static int snd_generic_suspend(struct device *dev, pm_message_t state, u32 level)
807 if (level != SUSPEND_DISABLE)
810 card = get_snd_generic_card(dev);
811 if (card->power_state == SNDRV_CTL_POWER_D3hot)
813 card->pm_suspend(card, PMSG_SUSPEND);
814 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
818 static int snd_generic_resume(struct device *dev, u32 level)
822 if (level != RESUME_ENABLE)
825 card = get_snd_generic_card(dev);
826 if (card->power_state == SNDRV_CTL_POWER_D0)
828 card->pm_resume(card);
829 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
834 * snd_card_set_generic_pm_callback - set the generic power-management callbacks
835 * @card: soundcard structure
836 * @suspend: suspend callback function
837 * @resume: resume callback function
838 * @private_data: private data to pass to the callback functions
840 * Registers the power-management and sets the lowlevel callbacks for
841 * the given card. These callbacks are called from the ALSA's common
842 * PM handler and from the control API.
844 int snd_card_set_generic_pm_callback(snd_card_t *card,
845 int (*suspend)(snd_card_t *, pm_message_t),
846 int (*resume)(snd_card_t *),
849 card->pm_dev = snd_generic_device_register(card);
852 snd_card_set_pm_callback(card, suspend, resume, private_data);
855 #endif /* CONFIG_SND_GENERIC_PM */
858 int snd_card_pci_suspend(struct pci_dev *dev, pm_message_t state)
860 snd_card_t *card = pci_get_drvdata(dev);
862 if (! card || ! card->pm_suspend)
864 if (card->power_state == SNDRV_CTL_POWER_D3hot)
866 err = card->pm_suspend(card, PMSG_SUSPEND);
868 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
872 int snd_card_pci_resume(struct pci_dev *dev)
874 snd_card_t *card = pci_get_drvdata(dev);
875 if (! card || ! card->pm_resume)
877 if (card->power_state == SNDRV_CTL_POWER_D0)
879 /* restore the PCI config space */
880 pci_restore_state(dev);
881 card->pm_resume(card);
882 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
887 #endif /* CONFIG_PM */