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 <linux/platform_device.h>
33 #include <sound/core.h>
34 #include <sound/control.h>
35 #include <sound/info.h>
37 struct snd_shutdown_f_ops {
38 struct file_operations f_ops;
39 struct snd_shutdown_f_ops *next;
42 unsigned int snd_cards_lock = 0; /* locked for registering/using */
43 struct snd_card *snd_cards[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = NULL};
44 DEFINE_RWLOCK(snd_card_rwlock);
46 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
47 int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
50 static void snd_card_id_read(struct snd_info_entry *entry,
51 struct snd_info_buffer *buffer)
53 snd_iprintf(buffer, "%s\n", entry->card->id);
56 static void snd_card_free_thread(void * __card);
59 * snd_card_new - create and initialize a soundcard structure
60 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
61 * @xid: card identification (ASCII string)
62 * @module: top level module for locking
63 * @extra_size: allocate this extra size after the main soundcard structure
65 * Creates and initializes a soundcard structure.
67 * Returns kmallocated snd_card structure. Creates the ALSA control interface
68 * (which is blocked until snd_card_register function is called).
70 struct snd_card *snd_card_new(int idx, const char *xid,
71 struct module *module, int extra_size)
73 struct snd_card *card;
78 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
82 if (!snd_info_check_reserved_words(xid))
84 strlcpy(card->id, xid, sizeof(card->id));
87 write_lock(&snd_card_rwlock);
90 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
91 if (~snd_cards_lock & idx & 1<<idx2) {
93 if (idx >= snd_ecards_limit)
94 snd_ecards_limit = idx + 1;
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(struct snd_card);
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(struct snd_card *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);
232 #ifdef CONFIG_SND_GENERIC_DRIVER
233 static void snd_generic_device_unregister(struct snd_card *card);
235 #define snd_generic_device_unregister(x) /*NOP*/
239 * snd_card_free - frees given soundcard structure
240 * @card: soundcard structure
242 * This function releases the soundcard structure and the all assigned
243 * devices automatically. That is, you don't have to release the devices
246 * Returns zero. Frees all associated devices and frees the control
247 * interface associated to given soundcard.
249 int snd_card_free(struct snd_card *card)
251 struct snd_shutdown_f_ops *s_f_ops;
255 write_lock(&snd_card_rwlock);
256 snd_cards[card->number] = NULL;
257 write_unlock(&snd_card_rwlock);
260 wake_up(&card->power_sleep);
262 /* wait, until all devices are ready for the free operation */
263 wait_event(card->shutdown_sleep, card->files == NULL);
265 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
266 if (snd_mixer_oss_notify_callback)
267 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
269 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
270 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
271 /* Fatal, but this situation should never occur */
273 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
274 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
275 /* Fatal, but this situation should never occur */
277 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
278 snd_printk(KERN_ERR "unable to free all devices (post)\n");
279 /* Fatal, but this situation should never occur */
281 if (card->private_free)
282 card->private_free(card);
284 snd_info_unregister(card->proc_id);
285 if (snd_info_card_free(card) < 0) {
286 snd_printk(KERN_WARNING "unable to free card info\n");
287 /* Not fatal error */
289 snd_generic_device_unregister(card);
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 struct snd_card *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(struct snd_card *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(struct snd_card *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)\n", 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(struct snd_card *card)
422 struct snd_info_entry *entry;
424 snd_assert(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 struct snd_info_entry *snd_card_info_entry = NULL;
462 static void snd_card_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
465 struct snd_card *card;
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(struct snd_info_buffer *buffer)
490 struct snd_card *card;
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 struct snd_info_entry *snd_card_module_info_entry;
509 static void snd_card_module_info_read(struct snd_info_entry *entry,
510 struct snd_info_buffer *buffer)
513 struct snd_card *card;
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 struct snd_info_entry *entry;
528 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
531 entry->c.text.read_size = PAGE_SIZE;
532 entry->c.text.read = snd_card_info_read;
533 if (snd_info_register(entry) < 0) {
534 snd_info_free_entry(entry);
537 snd_card_info_entry = entry;
540 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
542 entry->c.text.read_size = PAGE_SIZE;
543 entry->c.text.read = snd_card_module_info_read;
544 if (snd_info_register(entry) < 0)
545 snd_info_free_entry(entry);
547 snd_card_module_info_entry = entry;
554 int __exit snd_card_info_done(void)
556 if (snd_card_info_entry)
557 snd_info_unregister(snd_card_info_entry);
559 if (snd_card_module_info_entry)
560 snd_info_unregister(snd_card_module_info_entry);
566 * snd_component_add - add a component string
567 * @card: soundcard structure
568 * @component: the component id string
570 * This function adds the component id string to the supported list.
571 * The component can be referred from the alsa-lib.
573 * Returns zero otherwise a negative error code.
576 int snd_component_add(struct snd_card *card, const char *component)
579 int len = strlen(component);
581 ptr = strstr(card->components, component);
583 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
586 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
590 if (card->components[0] != '\0')
591 strcat(card->components, " ");
592 strcat(card->components, component);
597 * snd_card_file_add - add the file to the file list of the card
598 * @card: soundcard structure
599 * @file: file pointer
601 * This function adds the file to the file linked-list of the card.
602 * This linked-list is used to keep tracking the connection state,
603 * and to avoid the release of busy resources by hotplug.
605 * Returns zero or a negative error code.
607 int snd_card_file_add(struct snd_card *card, struct file *file)
609 struct snd_monitor_file *mfile;
611 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
616 spin_lock(&card->files_lock);
617 if (card->shutdown) {
618 spin_unlock(&card->files_lock);
622 mfile->next = card->files;
624 spin_unlock(&card->files_lock);
629 * snd_card_file_remove - remove the file from the file list
630 * @card: soundcard structure
631 * @file: file pointer
633 * This function removes the file formerly added to the card via
634 * snd_card_file_add() function.
635 * If all files are removed and the release of the card is
636 * scheduled, it will wake up the the thread to call snd_card_free()
637 * (see snd_card_free_in_thread() function).
639 * Returns zero or a negative error code.
641 int snd_card_file_remove(struct snd_card *card, struct file *file)
643 struct snd_monitor_file *mfile, *pfile = NULL;
645 spin_lock(&card->files_lock);
648 if (mfile->file == file) {
650 pfile->next = mfile->next;
652 card->files = mfile->next;
658 spin_unlock(&card->files_lock);
659 if (card->files == NULL)
660 wake_up(&card->shutdown_sleep);
662 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
669 #ifdef CONFIG_SND_GENERIC_DRIVER
671 * generic device without a proper bus using platform_device
674 struct snd_generic_device {
675 struct platform_device pdev;
676 struct snd_card *card;
679 #define get_snd_generic_card(dev) container_of(dev, struct snd_generic_device, pdev)->card
681 #define SND_GENERIC_NAME "snd_generic"
684 static int snd_generic_suspend(struct platform_device *dev, pm_message_t state);
685 static int snd_generic_resume(struct platform_device *dev);
688 /* initialized in sound.c */
689 struct platform_driver snd_generic_driver = {
691 .suspend = snd_generic_suspend,
692 .resume = snd_generic_resume,
695 .name = SND_GENERIC_NAME,
699 void snd_generic_device_release(struct device *dev)
703 static int snd_generic_device_register(struct snd_card *card)
705 struct snd_generic_device *dev;
708 if (card->generic_dev)
709 return 0; /* already registered */
711 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
713 snd_printk(KERN_ERR "can't allocate generic_device\n");
717 dev->pdev.name = SND_GENERIC_NAME;
718 dev->pdev.id = card->number;
719 dev->pdev.dev.release = snd_generic_device_release;
721 if ((err = platform_device_register(&dev->pdev)) < 0) {
725 card->generic_dev = dev;
729 static void snd_generic_device_unregister(struct snd_card *card)
731 struct snd_generic_device *dev = card->generic_dev;
733 platform_device_unregister(&dev->pdev);
735 card->generic_dev = NULL;
740 * snd_card_set_generic_dev - assign the generic device to the card
741 * @card: soundcard structure
743 * Assigns a generic device to the card. This function is provided as the
744 * last resort, for devices without any proper bus. Thus this won't override
745 * the device already assigned to the card.
747 * Returns zero if successful, or a negative error code.
749 int snd_card_set_generic_dev(struct snd_card *card)
752 if ((err = snd_generic_device_register(card)) < 0)
755 snd_card_set_dev(card, &card->generic_dev->pdev.dev);
758 #endif /* CONFIG_SND_GENERIC_DRIVER */
762 * snd_power_wait - wait until the power-state is changed.
763 * @card: soundcard structure
764 * @power_state: expected power state
765 * @file: file structure for the O_NONBLOCK check (optional)
767 * Waits until the power-state is changed.
769 * Note: the power lock must be active before call.
771 int snd_power_wait(struct snd_card *card, unsigned int power_state, struct file *file)
777 if (snd_power_get_state(card) == power_state)
779 init_waitqueue_entry(&wait, current);
780 add_wait_queue(&card->power_sleep, &wait);
782 if (card->shutdown) {
786 if (snd_power_get_state(card) == power_state)
788 #if 0 /* block all devices */
789 if (file && (file->f_flags & O_NONBLOCK)) {
794 set_current_state(TASK_UNINTERRUPTIBLE);
795 snd_power_unlock(card);
796 schedule_timeout(30 * HZ);
797 snd_power_lock(card);
799 remove_wait_queue(&card->power_sleep, &wait);
804 * snd_card_set_pm_callback - set the PCI power-management callbacks
805 * @card: soundcard structure
806 * @suspend: suspend callback function
807 * @resume: resume callback function
808 * @private_data: private data to pass to the callback functions
810 * Sets the power-management callback functions of the card.
811 * These callbacks are called from ALSA's common PCI suspend/resume
812 * handler and from the control API.
814 int snd_card_set_pm_callback(struct snd_card *card,
815 int (*suspend)(struct snd_card *, pm_message_t),
816 int (*resume)(struct snd_card *),
819 card->pm_suspend = suspend;
820 card->pm_resume = resume;
821 card->pm_private_data = private_data;
825 #ifdef CONFIG_SND_GENERIC_DRIVER
826 /* suspend/resume callbacks for snd_generic platform device */
827 static int snd_generic_suspend(struct platform_device *dev, pm_message_t state)
829 struct snd_card *card;
831 card = get_snd_generic_card(dev);
832 if (card->power_state == SNDRV_CTL_POWER_D3hot)
834 if (card->pm_suspend)
835 card->pm_suspend(card, PMSG_SUSPEND);
836 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
840 static int snd_generic_resume(struct platform_device *dev)
842 struct snd_card *card;
844 card = get_snd_generic_card(dev);
845 if (card->power_state == SNDRV_CTL_POWER_D0)
848 card->pm_resume(card);
849 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
854 * snd_card_set_generic_pm_callback - set the generic power-management callbacks
855 * @card: soundcard structure
856 * @suspend: suspend callback function
857 * @resume: resume callback function
858 * @private_data: private data to pass to the callback functions
860 * Registers the power-management and sets the lowlevel callbacks for
861 * the given card. These callbacks are called from the ALSA's common
862 * PM handler and from the control API.
864 int snd_card_set_generic_pm_callback(struct snd_card *card,
865 int (*suspend)(struct snd_card *, pm_message_t),
866 int (*resume)(struct snd_card *),
870 if ((err = snd_generic_device_register(card)) < 0)
872 return snd_card_set_pm_callback(card, suspend, resume, private_data);
874 #endif /* CONFIG_SND_GENERIC_DRIVER */
877 int snd_card_pci_suspend(struct pci_dev *dev, pm_message_t state)
879 struct snd_card *card = pci_get_drvdata(dev);
881 if (! card || ! card->pm_suspend)
883 if (card->power_state == SNDRV_CTL_POWER_D3hot)
885 err = card->pm_suspend(card, PMSG_SUSPEND);
887 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
891 int snd_card_pci_resume(struct pci_dev *dev)
893 struct snd_card *card = pci_get_drvdata(dev);
894 if (! card || ! card->pm_resume)
896 if (card->power_state == SNDRV_CTL_POWER_D0)
898 /* restore the PCI config space */
899 pci_restore_state(dev);
900 card->pm_resume(card);
901 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
906 #endif /* CONFIG_PM */