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 = kzalloc(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 #ifdef CONFIG_SND_GENERIC_DRIVER
230 static void snd_generic_device_unregister(snd_card_t *card);
232 #define snd_generic_device_unregister(x) /*NOP*/
236 * snd_card_free - frees given soundcard structure
237 * @card: soundcard structure
239 * This function releases the soundcard structure and the all assigned
240 * devices automatically. That is, you don't have to release the devices
243 * Returns zero. Frees all associated devices and frees the control
244 * interface associated to given soundcard.
246 int snd_card_free(snd_card_t * card)
248 struct snd_shutdown_f_ops *s_f_ops;
252 write_lock(&snd_card_rwlock);
253 snd_cards[card->number] = NULL;
254 write_unlock(&snd_card_rwlock);
257 wake_up(&card->power_sleep);
259 /* wait, until all devices are ready for the free operation */
260 wait_event(card->shutdown_sleep, card->files == NULL);
262 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
263 if (snd_mixer_oss_notify_callback)
264 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
266 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
267 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
268 /* Fatal, but this situation should never occur */
270 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
271 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
272 /* Fatal, but this situation should never occur */
274 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
275 snd_printk(KERN_ERR "unable to free all devices (post)\n");
276 /* Fatal, but this situation should never occur */
278 if (card->private_free)
279 card->private_free(card);
281 snd_info_unregister(card->proc_id);
282 if (snd_info_card_free(card) < 0) {
283 snd_printk(KERN_WARNING "unable to free card info\n");
284 /* Not fatal error */
286 snd_generic_device_unregister(card);
287 while (card->s_f_ops) {
288 s_f_ops = card->s_f_ops;
289 card->s_f_ops = s_f_ops->next;
292 write_lock(&snd_card_rwlock);
293 snd_cards_lock &= ~(1 << card->number);
294 write_unlock(&snd_card_rwlock);
299 static void snd_card_free_thread(void * __card)
301 snd_card_t *card = __card;
302 struct module * module = card->module;
304 if (!try_module_get(module)) {
305 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
315 * snd_card_free_in_thread - call snd_card_free() in thread
316 * @card: soundcard structure
318 * This function schedules the call of snd_card_free() function in a
319 * work queue. When all devices are released (non-busy), the work
320 * is woken up and calls snd_card_free().
322 * When a card can be disconnected at any time by hotplug service,
323 * this function should be used in disconnect (or detach) callback
324 * instead of calling snd_card_free() directly.
326 * Returns - zero otherwise a negative error code if the start of thread failed.
328 int snd_card_free_in_thread(snd_card_t * card)
330 if (card->files == NULL) {
335 if (schedule_work(&card->free_workq))
338 snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
339 /* try to free the structure immediately */
344 static void choose_default_id(snd_card_t * card)
346 int i, len, idx_flag = 0, loops = 8;
349 id = spos = card->shortname;
350 while (*id != '\0') {
356 while (*spos != '\0' && !isalnum(*spos))
359 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
360 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
370 strcpy(id, "default");
374 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
375 strcpy(card->id, card->proc_root->name);
378 if (!snd_info_check_reserved_words(id))
380 for (i = 0; i < snd_ecards_limit; i++) {
381 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
390 else if ((size_t)len <= sizeof(card->id) - 3) {
395 if ((size_t)len <= sizeof(card->id) - 2)
406 * snd_card_register - register the soundcard
407 * @card: soundcard structure
409 * This function registers all the devices assigned to the soundcard.
410 * Until calling this, the ALSA control interface is blocked from the
411 * external accesses. Thus, you should call this function at the end
412 * of the initialization of the card.
414 * Returns zero otherwise a negative error code if the registrain failed.
416 int snd_card_register(snd_card_t * card)
419 snd_info_entry_t *entry;
421 snd_runtime_check(card != NULL, return -EINVAL);
422 if ((err = snd_device_register_all(card)) < 0)
424 write_lock(&snd_card_rwlock);
425 if (snd_cards[card->number]) {
426 /* already registered */
427 write_unlock(&snd_card_rwlock);
430 if (card->id[0] == '\0')
431 choose_default_id(card);
432 snd_cards[card->number] = card;
433 write_unlock(&snd_card_rwlock);
434 if ((err = snd_info_card_register(card)) < 0) {
435 snd_printd("unable to create card info\n");
438 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
439 snd_printd("unable to create card entry\n");
442 entry->c.text.read_size = PAGE_SIZE;
443 entry->c.text.read = snd_card_id_read;
444 if (snd_info_register(entry) < 0) {
445 snd_info_free_entry(entry);
448 card->proc_id = entry;
450 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
451 if (snd_mixer_oss_notify_callback)
452 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
457 static snd_info_entry_t *snd_card_info_entry = NULL;
459 static void snd_card_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
464 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
465 read_lock(&snd_card_rwlock);
466 if ((card = snd_cards[idx]) != NULL) {
468 snd_iprintf(buffer, "%i [%-15s]: %s - %s\n",
473 snd_iprintf(buffer, " %s\n",
476 read_unlock(&snd_card_rwlock);
479 snd_iprintf(buffer, "--- no soundcards ---\n");
482 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
484 void snd_card_info_read_oss(snd_info_buffer_t * buffer)
489 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
490 read_lock(&snd_card_rwlock);
491 if ((card = snd_cards[idx]) != NULL) {
493 snd_iprintf(buffer, "%s\n", card->longname);
495 read_unlock(&snd_card_rwlock);
498 snd_iprintf(buffer, "--- no soundcards ---\n");
505 static snd_info_entry_t *snd_card_module_info_entry;
506 static void snd_card_module_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
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, "%i %s\n", idx, card->module->name);
515 read_unlock(&snd_card_rwlock);
520 int __init snd_card_info_init(void)
522 snd_info_entry_t *entry;
524 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
525 snd_runtime_check(entry != NULL, return -ENOMEM);
526 entry->c.text.read_size = PAGE_SIZE;
527 entry->c.text.read = snd_card_info_read;
528 if (snd_info_register(entry) < 0) {
529 snd_info_free_entry(entry);
532 snd_card_info_entry = entry;
535 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
537 entry->c.text.read_size = PAGE_SIZE;
538 entry->c.text.read = snd_card_module_info_read;
539 if (snd_info_register(entry) < 0)
540 snd_info_free_entry(entry);
542 snd_card_module_info_entry = entry;
549 int __exit snd_card_info_done(void)
551 if (snd_card_info_entry)
552 snd_info_unregister(snd_card_info_entry);
554 if (snd_card_module_info_entry)
555 snd_info_unregister(snd_card_module_info_entry);
561 * snd_component_add - add a component string
562 * @card: soundcard structure
563 * @component: the component id string
565 * This function adds the component id string to the supported list.
566 * The component can be referred from the alsa-lib.
568 * Returns zero otherwise a negative error code.
571 int snd_component_add(snd_card_t *card, const char *component)
574 int len = strlen(component);
576 ptr = strstr(card->components, component);
578 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
581 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
585 if (card->components[0] != '\0')
586 strcat(card->components, " ");
587 strcat(card->components, component);
592 * snd_card_file_add - add the file to the file list of the card
593 * @card: soundcard structure
594 * @file: file pointer
596 * This function adds the file to the file linked-list of the card.
597 * This linked-list is used to keep tracking the connection state,
598 * and to avoid the release of busy resources by hotplug.
600 * Returns zero or a negative error code.
602 int snd_card_file_add(snd_card_t *card, struct file *file)
604 struct snd_monitor_file *mfile;
606 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
611 spin_lock(&card->files_lock);
612 if (card->shutdown) {
613 spin_unlock(&card->files_lock);
617 mfile->next = card->files;
619 spin_unlock(&card->files_lock);
624 * snd_card_file_remove - remove the file from the file list
625 * @card: soundcard structure
626 * @file: file pointer
628 * This function removes the file formerly added to the card via
629 * snd_card_file_add() function.
630 * If all files are removed and the release of the card is
631 * scheduled, it will wake up the the thread to call snd_card_free()
632 * (see snd_card_free_in_thread() function).
634 * Returns zero or a negative error code.
636 int snd_card_file_remove(snd_card_t *card, struct file *file)
638 struct snd_monitor_file *mfile, *pfile = NULL;
640 spin_lock(&card->files_lock);
643 if (mfile->file == file) {
645 pfile->next = mfile->next;
647 card->files = mfile->next;
653 spin_unlock(&card->files_lock);
654 if (card->files == NULL)
655 wake_up(&card->shutdown_sleep);
657 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
664 #ifdef CONFIG_SND_GENERIC_DRIVER
666 * generic device without a proper bus using platform_device
669 struct snd_generic_device {
670 struct platform_device pdev;
674 #define get_snd_generic_card(dev) container_of(to_platform_device(dev), struct snd_generic_device, pdev)->card
676 #define SND_GENERIC_NAME "snd_generic"
679 static int snd_generic_suspend(struct device *dev, pm_message_t state);
680 static int snd_generic_resume(struct device *dev);
683 /* initialized in sound.c */
684 struct device_driver snd_generic_driver = {
685 .name = SND_GENERIC_NAME,
686 .bus = &platform_bus_type,
688 .suspend = snd_generic_suspend,
689 .resume = snd_generic_resume,
693 void snd_generic_device_release(struct device *dev)
697 static int snd_generic_device_register(snd_card_t *card)
699 struct snd_generic_device *dev;
702 if (card->generic_dev)
703 return 0; /* already registered */
705 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
707 snd_printk(KERN_ERR "can't allocate generic_device\n");
711 dev->pdev.name = SND_GENERIC_NAME;
712 dev->pdev.id = card->number;
713 dev->pdev.dev.release = snd_generic_device_release;
715 if ((err = platform_device_register(&dev->pdev)) < 0) {
719 card->generic_dev = dev;
723 static void snd_generic_device_unregister(snd_card_t *card)
725 struct snd_generic_device *dev = card->generic_dev;
727 platform_device_unregister(&dev->pdev);
729 card->generic_dev = NULL;
734 * snd_card_set_generic_dev - assign the generic device to the card
735 * @card: soundcard structure
737 * Assigns a generic device to the card. This function is provided as the
738 * last resort, for devices without any proper bus. Thus this won't override
739 * the device already assigned to the card.
741 * Returns zero if successful, or a negative error code.
743 int snd_card_set_generic_dev(snd_card_t *card)
746 if ((err = snd_generic_device_register(card)) < 0)
749 snd_card_set_dev(card, &card->generic_dev->pdev.dev);
752 #endif /* CONFIG_SND_GENERIC_DRIVER */
756 * snd_power_wait - wait until the power-state is changed.
757 * @card: soundcard structure
758 * @power_state: expected power state
759 * @file: file structure for the O_NONBLOCK check (optional)
761 * Waits until the power-state is changed.
763 * Note: the power lock must be active before call.
765 int snd_power_wait(snd_card_t *card, unsigned int power_state, struct file *file)
771 if (snd_power_get_state(card) == power_state)
773 init_waitqueue_entry(&wait, current);
774 add_wait_queue(&card->power_sleep, &wait);
776 if (card->shutdown) {
780 if (snd_power_get_state(card) == power_state)
782 #if 0 /* block all devices */
783 if (file && (file->f_flags & O_NONBLOCK)) {
788 set_current_state(TASK_UNINTERRUPTIBLE);
789 snd_power_unlock(card);
790 schedule_timeout(30 * HZ);
791 snd_power_lock(card);
793 remove_wait_queue(&card->power_sleep, &wait);
798 * snd_card_set_pm_callback - set the PCI power-management callbacks
799 * @card: soundcard structure
800 * @suspend: suspend callback function
801 * @resume: resume callback function
802 * @private_data: private data to pass to the callback functions
804 * Sets the power-management callback functions of the card.
805 * These callbacks are called from ALSA's common PCI suspend/resume
806 * handler and from the control API.
808 int snd_card_set_pm_callback(snd_card_t *card,
809 int (*suspend)(snd_card_t *, pm_message_t),
810 int (*resume)(snd_card_t *),
813 card->pm_suspend = suspend;
814 card->pm_resume = resume;
815 card->pm_private_data = private_data;
819 #ifdef CONFIG_SND_GENERIC_DRIVER
820 /* suspend/resume callbacks for snd_generic platform device */
821 static int snd_generic_suspend(struct device *dev, pm_message_t state)
825 card = get_snd_generic_card(dev);
826 if (card->power_state == SNDRV_CTL_POWER_D3hot)
828 if (card->pm_suspend)
829 card->pm_suspend(card, PMSG_SUSPEND);
830 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
834 static int snd_generic_resume(struct device *dev)
838 card = get_snd_generic_card(dev);
839 if (card->power_state == SNDRV_CTL_POWER_D0)
841 if (card->pm_suspend)
842 card->pm_resume(card);
843 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
848 * snd_card_set_generic_pm_callback - set the generic power-management callbacks
849 * @card: soundcard structure
850 * @suspend: suspend callback function
851 * @resume: resume callback function
852 * @private_data: private data to pass to the callback functions
854 * Registers the power-management and sets the lowlevel callbacks for
855 * the given card. These callbacks are called from the ALSA's common
856 * PM handler and from the control API.
858 int snd_card_set_generic_pm_callback(snd_card_t *card,
859 int (*suspend)(snd_card_t *, pm_message_t),
860 int (*resume)(snd_card_t *),
864 if ((err = snd_generic_device_register(card)) < 0)
866 return snd_card_set_pm_callback(card, suspend, resume, private_data);
868 #endif /* CONFIG_SND_GENERIC_DRIVER */
871 int snd_card_pci_suspend(struct pci_dev *dev, pm_message_t state)
873 snd_card_t *card = pci_get_drvdata(dev);
875 if (! card || ! card->pm_suspend)
877 if (card->power_state == SNDRV_CTL_POWER_D3hot)
879 err = card->pm_suspend(card, PMSG_SUSPEND);
881 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
885 int snd_card_pci_resume(struct pci_dev *dev)
887 snd_card_t *card = pci_get_drvdata(dev);
888 if (! card || ! card->pm_resume)
890 if (card->power_state == SNDRV_CTL_POWER_D0)
892 /* restore the PCI config space */
893 pci_restore_state(dev);
894 card->pm_resume(card);
895 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
900 #endif /* CONFIG_PM */