#include <linux/slab.h>
#include <linux/time.h>
#include <linux/ctype.h>
+#include <linux/pci.h>
+#include <linux/pm.h>
#include <sound/core.h>
#include <sound/control.h>
#include <sound/info.h>
struct snd_shutdown_f_ops *next;
};
-int snd_cards_count = 0;
unsigned int snd_cards_lock = 0; /* locked for registering/using */
snd_card_t *snd_cards[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = NULL};
-rwlock_t snd_card_rwlock = RW_LOCK_UNLOCKED;
+DEFINE_RWLOCK(snd_card_rwlock);
#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
int (*snd_mixer_oss_notify_callback)(snd_card_t *card, int free_flag);
if (extra_size < 0)
extra_size = 0;
- card = (snd_card_t *) snd_kcalloc(sizeof(snd_card_t) + extra_size, GFP_KERNEL);
+ card = kcalloc(1, sizeof(*card) + extra_size, GFP_KERNEL);
if (card == NULL)
return NULL;
if (xid) {
write_lock(&snd_card_rwlock);
if (idx < 0) {
int idx2;
- for (idx2 = 0; idx2 < snd_ecards_limit; idx2++)
- if (!(snd_cards_lock & (1 << idx2))) {
+ for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
+ if (~snd_cards_lock & idx & 1<<idx2) {
idx = idx2;
+ if (idx >= snd_ecards_limit)
+ snd_ecards_limit = idx + 1;
break;
}
- if (idx < 0 && snd_ecards_limit < SNDRV_CARDS)
- /* for dynamically additional devices like hotplug:
- * increment the limit if still free slot exists.
- */
- idx = snd_ecards_limit++;
} else if (idx < snd_ecards_limit) {
if (snd_cards_lock & (1 << idx))
err = -ENODEV; /* invalid */
#endif
/* the control interface cannot be accessed from the user space until */
/* snd_cards_bitmask and snd_cards are set with snd_card_register */
- if ((err = snd_ctl_register(card)) < 0) {
+ if ((err = snd_ctl_create(card)) < 0) {
snd_printd("unable to register control minors\n");
goto __error;
}
return card;
__error_ctl:
- snd_ctl_unregister(card);
+ snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
__error:
kfree(card);
return NULL;
/* phase 3: notify all connected devices about disconnection */
/* at this point, they cannot respond to any calls except release() */
- snd_ctl_disconnect(card);
-
#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
if (snd_mixer_oss_notify_callback)
snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
return 0;
}
+#if defined(CONFIG_PM) && defined(CONFIG_SND_GENERIC_PM)
+static void snd_generic_device_unregister(struct snd_generic_device *dev);
+#endif
+
/**
* snd_card_free - frees given soundcard structure
* @card: soundcard structure
return -EINVAL;
write_lock(&snd_card_rwlock);
snd_cards[card->number] = NULL;
- snd_cards_count--;
write_unlock(&snd_card_rwlock);
#ifdef CONFIG_PM
wake_up(&card->power_sleep);
+#ifdef CONFIG_SND_GENERIC_PM
+ if (card->pm_dev) {
+ snd_generic_device_unregister(card->pm_dev);
+ card->pm_dev = NULL;
+ }
+#endif
#endif
/* wait, until all devices are ready for the free operation */
snd_printk(KERN_ERR "unable to free all devices (normal)\n");
/* Fatal, but this situation should never occur */
}
- if (snd_ctl_unregister(card) < 0) {
- snd_printk(KERN_ERR "unable to unregister control minors\n");
- /* Not fatal error */
- }
if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
snd_printk(KERN_ERR "unable to free all devices (post)\n");
/* Fatal, but this situation should never occur */
while (1) {
if (loops-- == 0) {
- snd_printk(KERN_ERR "unable to choose default card id (%s)", id);
+ snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
strcpy(card->id, card->proc_root->name);
return;
}
if (card->id[0] == '\0')
choose_default_id(card);
snd_cards[card->number] = card;
- snd_cards_count++;
write_unlock(&snd_card_rwlock);
if ((err = snd_info_card_register(card)) < 0) {
snd_printd("unable to create card info\n");
spin_unlock(&card->files_lock);
if (card->files == NULL)
wake_up(&card->shutdown_sleep);
- if (mfile) {
- kfree(mfile);
- } else {
+ if (!mfile) {
snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
return -ENOENT;
}
+ kfree(mfile);
return 0;
}
remove_wait_queue(&card->power_sleep, &wait);
return result;
}
+
+/**
+ * snd_card_set_pm_callback - set the PCI power-management callbacks
+ * @card: soundcard structure
+ * @suspend: suspend callback function
+ * @resume: resume callback function
+ * @private_data: private data to pass to the callback functions
+ *
+ * Sets the power-management callback functions of the card.
+ * These callbacks are called from ALSA's common PCI suspend/resume
+ * handler and from the control API.
+ */
+int snd_card_set_pm_callback(snd_card_t *card,
+ int (*suspend)(snd_card_t *, pm_message_t),
+ int (*resume)(snd_card_t *),
+ void *private_data)
+{
+ card->pm_suspend = suspend;
+ card->pm_resume = resume;
+ card->pm_private_data = private_data;
+ return 0;
+}
+
+#ifdef CONFIG_SND_GENERIC_PM
+/*
+ * use platform_device for generic power-management without a proper bus
+ * (e.g. ISA)
+ */
+struct snd_generic_device {
+ struct platform_device pdev;
+ snd_card_t *card;
+};
+
+#define get_snd_generic_card(dev) container_of(to_platform_device(dev), struct snd_generic_device, pdev)->card
+
+#define SND_GENERIC_NAME "snd_generic_pm"
+
+static int snd_generic_suspend(struct device *dev, pm_message_t state, u32 level);
+static int snd_generic_resume(struct device *dev, u32 level);
+
+static struct device_driver snd_generic_driver = {
+ .name = SND_GENERIC_NAME,
+ .bus = &platform_bus_type,
+ .suspend = snd_generic_suspend,
+ .resume = snd_generic_resume,
+};
+
+static int generic_driver_registered;
+
+static void generic_driver_unregister(void)
+{
+ if (generic_driver_registered) {
+ generic_driver_registered--;
+ if (! generic_driver_registered)
+ driver_unregister(&snd_generic_driver);
+ }
+}
+
+static struct snd_generic_device *snd_generic_device_register(snd_card_t *card)
+{
+ struct snd_generic_device *dev;
+
+ if (! generic_driver_registered) {
+ if (driver_register(&snd_generic_driver) < 0)
+ return NULL;
+ }
+ generic_driver_registered++;
+
+ dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
+ if (! dev) {
+ generic_driver_unregister();
+ return NULL;
+ }
+
+ dev->pdev.name = SND_GENERIC_NAME;
+ dev->pdev.id = card->number;
+ dev->card = card;
+ if (platform_device_register(&dev->pdev) < 0) {
+ kfree(dev);
+ generic_driver_unregister();
+ return NULL;
+ }
+ return dev;
+}
+
+static void snd_generic_device_unregister(struct snd_generic_device *dev)
+{
+ platform_device_unregister(&dev->pdev);
+ kfree(dev);
+ generic_driver_unregister();
+}
+
+/* suspend/resume callbacks for snd_generic platform device */
+static int snd_generic_suspend(struct device *dev, pm_message_t state, u32 level)
+{
+ snd_card_t *card;
+
+ if (level != SUSPEND_DISABLE)
+ return 0;
+
+ card = get_snd_generic_card(dev);
+ if (card->power_state == SNDRV_CTL_POWER_D3hot)
+ return 0;
+ card->pm_suspend(card, PMSG_SUSPEND);
+ snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
+ return 0;
+}
+
+static int snd_generic_resume(struct device *dev, u32 level)
+{
+ snd_card_t *card;
+
+ if (level != RESUME_ENABLE)
+ return 0;
+
+ card = get_snd_generic_card(dev);
+ if (card->power_state == SNDRV_CTL_POWER_D0)
+ return 0;
+ card->pm_resume(card);
+ snd_power_change_state(card, SNDRV_CTL_POWER_D0);
+ return 0;
+}
+
+/**
+ * snd_card_set_generic_pm_callback - set the generic power-management callbacks
+ * @card: soundcard structure
+ * @suspend: suspend callback function
+ * @resume: resume callback function
+ * @private_data: private data to pass to the callback functions
+ *
+ * Registers the power-management and sets the lowlevel callbacks for
+ * the given card. These callbacks are called from the ALSA's common
+ * PM handler and from the control API.
+ */
+int snd_card_set_generic_pm_callback(snd_card_t *card,
+ int (*suspend)(snd_card_t *, pm_message_t),
+ int (*resume)(snd_card_t *),
+ void *private_data)
+{
+ card->pm_dev = snd_generic_device_register(card);
+ if (! card->pm_dev)
+ return -ENOMEM;
+ snd_card_set_pm_callback(card, suspend, resume, private_data);
+ return 0;
+}
+#endif /* CONFIG_SND_GENERIC_PM */
+
+#ifdef CONFIG_PCI
+int snd_card_pci_suspend(struct pci_dev *dev, pm_message_t state)
+{
+ snd_card_t *card = pci_get_drvdata(dev);
+ int err;
+ if (! card || ! card->pm_suspend)
+ return 0;
+ if (card->power_state == SNDRV_CTL_POWER_D3hot)
+ return 0;
+ err = card->pm_suspend(card, PMSG_SUSPEND);
+ pci_save_state(dev);
+ snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
+ return err;
+}
+
+int snd_card_pci_resume(struct pci_dev *dev)
+{
+ snd_card_t *card = pci_get_drvdata(dev);
+ if (! card || ! card->pm_resume)
+ return 0;
+ if (card->power_state == SNDRV_CTL_POWER_D0)
+ return 0;
+ /* restore the PCI config space */
+ pci_restore_state(dev);
+ card->pm_resume(card);
+ snd_power_change_state(card, SNDRV_CTL_POWER_D0);
+ return 0;
+}
+#endif
+
#endif /* CONFIG_PM */