[ALSA] unregister platform device again if probe was unsuccessful
[linux-3.10.git] / sound / drivers / dummy.c
index 9c827b15445875e6d347d0afb99073b86a8e2182..ffeafaf2eccae64956ac2bfd8034257fbb10b92c 100644 (file)
@@ -20,6 +20,8 @@
 
 #include <sound/driver.h>
 #include <linux/init.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
 #include <linux/jiffies.h>
 #include <linux/slab.h>
 #include <linux/time.h>
@@ -90,11 +92,27 @@ static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
 #define USE_RATE_MAX           48000
 #endif
 
+#if 0 /* CA0106 */
+#define USE_FORMATS            SNDRV_PCM_FMTBIT_S16_LE
+#define USE_CHANNELS_MIN       2
+#define USE_CHANNELS_MAX       2
+#define USE_RATE               (SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000) 
+#define USE_RATE_MIN           48000 
+#define USE_RATE_MAX           192000
+#define MAX_BUFFER_SIZE                ((65536-64)*8)
+#define MAX_PERIOD_SIZE                (65536-64)
+#define USE_PERIODS_MIN                2
+#define USE_PERIODS_MAX                8
+#endif
+
 
 /* defaults */
 #ifndef MAX_BUFFER_SIZE
 #define MAX_BUFFER_SIZE                (64*1024)
 #endif
+#ifndef MAX_PERIOD_SIZE
+#define MAX_PERIOD_SIZE                MAX_BUFFER_SIZE
+#endif
 #ifndef USE_FORMATS
 #define USE_FORMATS            (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
 #endif
@@ -142,6 +160,8 @@ MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
 //module_param_array(midi_devs, int, NULL, 0444);
 //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
 
+static struct platform_device *devices[SNDRV_CARDS];
+
 #define MIXER_ADDR_MASTER      0
 #define MIXER_ADDR_LINE                1
 #define MIXER_ADDR_MIC         2
@@ -151,6 +171,7 @@ MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
 
 struct snd_dummy {
        struct snd_card *card;
+       struct snd_pcm *pcm;
        spinlock_t mixer_lock;
        int mixer_volume[MIXER_ADDR_LAST+1][2];
        int capture_source[MIXER_ADDR_LAST+1][2];
@@ -169,8 +190,6 @@ struct snd_dummy_pcm {
        struct snd_pcm_substream *substream;
 };
 
-static struct snd_card *snd_dummy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
-
 
 static inline void snd_card_dummy_pcm_timer_start(struct snd_dummy_pcm *dpcm)
 {
@@ -190,15 +209,21 @@ static int snd_card_dummy_pcm_trigger(struct snd_pcm_substream *substream, int c
        int err = 0;
 
        spin_lock(&dpcm->lock);
-       if (cmd == SNDRV_PCM_TRIGGER_START) {
+       switch (cmd) {
+       case SNDRV_PCM_TRIGGER_START:
+       case SNDRV_PCM_TRIGGER_RESUME:
                snd_card_dummy_pcm_timer_start(dpcm);
-       } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
+               break;
+       case SNDRV_PCM_TRIGGER_STOP:
+       case SNDRV_PCM_TRIGGER_SUSPEND:
                snd_card_dummy_pcm_timer_stop(dpcm);
-       } else {
+               break;
+       default:
                err = -EINVAL;
+               break;
        }
        spin_unlock(&dpcm->lock);
-       return err;
+       return 0;
 }
 
 static int snd_card_dummy_pcm_prepare(struct snd_pcm_substream *substream)
@@ -224,8 +249,9 @@ static int snd_card_dummy_pcm_prepare(struct snd_pcm_substream *substream)
 static void snd_card_dummy_pcm_timer_function(unsigned long data)
 {
        struct snd_dummy_pcm *dpcm = (struct snd_dummy_pcm *)data;
+       unsigned long flags;
        
-       spin_lock(&dpcm->lock);
+       spin_lock_irqsave(&dpcm->lock, flags);
        dpcm->timer.expires = 1 + jiffies;
        add_timer(&dpcm->timer);
        dpcm->pcm_irq_pos += dpcm->pcm_jiffie;
@@ -233,11 +259,10 @@ static void snd_card_dummy_pcm_timer_function(unsigned long data)
        dpcm->pcm_buf_pos %= dpcm->pcm_size;
        if (dpcm->pcm_irq_pos >= dpcm->pcm_count) {
                dpcm->pcm_irq_pos %= dpcm->pcm_count;
-               spin_unlock(&dpcm->lock);
+               spin_unlock_irqrestore(&dpcm->lock, flags);
                snd_pcm_period_elapsed(dpcm->substream);
-               spin_lock(&dpcm->lock);
-       }
-       spin_unlock(&dpcm->lock);
+       } else
+               spin_unlock_irqrestore(&dpcm->lock, flags);
 }
 
 static snd_pcm_uframes_t snd_card_dummy_pcm_pointer(struct snd_pcm_substream *substream)
@@ -251,7 +276,7 @@ static snd_pcm_uframes_t snd_card_dummy_pcm_pointer(struct snd_pcm_substream *su
 static struct snd_pcm_hardware snd_card_dummy_playback =
 {
        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
-                                SNDRV_PCM_INFO_MMAP_VALID),
+                                SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
        .formats =              USE_FORMATS,
        .rates =                USE_RATE,
        .rate_min =             USE_RATE_MIN,
@@ -269,7 +294,7 @@ static struct snd_pcm_hardware snd_card_dummy_playback =
 static struct snd_pcm_hardware snd_card_dummy_capture =
 {
        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
-                                SNDRV_PCM_INFO_MMAP_VALID),
+                                SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
        .formats =              USE_FORMATS,
        .rates =                USE_RATE,
        .rate_min =             USE_RATE_MIN,
@@ -278,7 +303,7 @@ static struct snd_pcm_hardware snd_card_dummy_capture =
        .channels_max =         USE_CHANNELS_MAX,
        .buffer_bytes_max =     MAX_BUFFER_SIZE,
        .period_bytes_min =     64,
-       .period_bytes_max =     MAX_BUFFER_SIZE,
+       .period_bytes_max =     MAX_PERIOD_SIZE,
        .periods_min =          USE_PERIODS_MIN,
        .periods_max =          USE_PERIODS_MAX,
        .fifo_size =            0,
@@ -405,6 +430,7 @@ static int __init snd_card_dummy_pcm(struct snd_dummy *dummy, int device, int su
        if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device,
                               substreams, substreams, &pcm)) < 0)
                return err;
+       dummy->pcm = pcm;
        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops);
        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops);
        pcm->private_data = dummy;
@@ -547,14 +573,13 @@ static int __init snd_card_dummy_new_mixer(struct snd_dummy *dummy)
        return 0;
 }
 
-static int __init snd_card_dummy_probe(int dev)
+static int __init snd_dummy_probe(struct platform_device *devptr)
 {
        struct snd_card *card;
        struct snd_dummy *dummy;
        int idx, err;
+       int dev = devptr->id;
 
-       if (!enable[dev])
-               return -ENODEV;
        card = snd_card_new(index[dev], id[dev], THIS_MODULE,
                            sizeof(struct snd_dummy));
        if (card == NULL)
@@ -575,11 +600,10 @@ static int __init snd_card_dummy_probe(int dev)
        strcpy(card->shortname, "Dummy");
        sprintf(card->longname, "Dummy %i", dev + 1);
 
-       if ((err = snd_card_set_generic_dev(card)) < 0)
-               goto __nodev;
+       snd_card_set_dev(card, &devptr->dev);
 
        if ((err = snd_card_register(card)) == 0) {
-               snd_dummy_cards[dev] = card;
+               platform_set_drvdata(devptr, card);
                return 0;
        }
       __nodev:
@@ -587,23 +611,84 @@ static int __init snd_card_dummy_probe(int dev)
        return err;
 }
 
-static int __init alsa_card_dummy_init(void)
+static int snd_dummy_remove(struct platform_device *devptr)
 {
-       int dev, cards;
+       snd_card_free(platform_get_drvdata(devptr));
+       platform_set_drvdata(devptr, NULL);
+       return 0;
+}
 
-       for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
-               if (snd_card_dummy_probe(dev) < 0) {
-#ifdef MODULE
-                       printk(KERN_ERR "Dummy soundcard #%i not found or device busy\n", dev + 1);
+#ifdef CONFIG_PM
+static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
+{
+       struct snd_card *card = platform_get_drvdata(pdev);
+       struct snd_dummy *dummy = card->private_data;
+
+       snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
+       snd_pcm_suspend_all(dummy->pcm);
+       return 0;
+}
+       
+static int snd_dummy_resume(struct platform_device *pdev)
+{
+       struct snd_card *card = platform_get_drvdata(pdev);
+
+       snd_power_change_state(card, SNDRV_CTL_POWER_D0);
+       return 0;
+}
+#endif
+
+#define SND_DUMMY_DRIVER       "snd_dummy"
+
+static struct platform_driver snd_dummy_driver = {
+       .probe          = snd_dummy_probe,
+       .remove         = snd_dummy_remove,
+#ifdef CONFIG_PM
+       .suspend        = snd_dummy_suspend,
+       .resume         = snd_dummy_resume,
 #endif
-                       break;
+       .driver         = {
+               .name   = SND_DUMMY_DRIVER
+       },
+};
+
+static void __init_or_module snd_dummy_unregister_all(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(devices); ++i)
+               platform_device_unregister(devices[i]);
+       platform_driver_unregister(&snd_dummy_driver);
+}
+
+static int __init alsa_card_dummy_init(void)
+{
+       int i, cards, err;
+
+       if ((err = platform_driver_register(&snd_dummy_driver)) < 0)
+               return err;
+
+       cards = 0;
+       for (i = 0; i < SNDRV_CARDS; i++) {
+               struct platform_device *device;
+               if (! enable[i])
+                       continue;
+               device = platform_device_register_simple(SND_DUMMY_DRIVER,
+                                                        i, NULL, 0);
+               if (IS_ERR(device))
+                       continue;
+               if (!platform_get_drvdata(device)) {
+                       platform_device_unregister(device);
+                       continue;
                }
+               devices[i] = device;
                cards++;
        }
        if (!cards) {
 #ifdef MODULE
                printk(KERN_ERR "Dummy soundcard not found or device busy\n");
 #endif
+               snd_dummy_unregister_all();
                return -ENODEV;
        }
        return 0;
@@ -611,10 +696,7 @@ static int __init alsa_card_dummy_init(void)
 
 static void __exit alsa_card_dummy_exit(void)
 {
-       int idx;
-
-       for (idx = 0; idx < SNDRV_CARDS; idx++)
-               snd_card_free(snd_dummy_cards[idx]);
+       snd_dummy_unregister_all();
 }
 
 module_init(alsa_card_dummy_init)