Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / sound / isa / cmi8330.c
index 46776cc..3c1e9fd 100644 (file)
@@ -45,6 +45,8 @@
 
 #include <sound/driver.h>
 #include <linux/init.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/pnp.h>
 #include <linux/moduleparam.h>
@@ -106,6 +108,11 @@ MODULE_PARM_DESC(wssirq, "IRQ # for CMI8330 WSS driver.");
 module_param_array(wssdma, int, NULL, 0444);
 MODULE_PARM_DESC(wssdma, "DMA for CMI8330 WSS driver.");
 
+static struct platform_device *platform_devices[SNDRV_CARDS];
+#ifdef CONFIG_PNP
+static int pnp_registered;
+#endif
+
 #define CMI8330_RMUX3D    16
 #define CMI8330_MUTEMUX   17
 #define CMI8330_OUTPUTVOL 18
@@ -137,27 +144,25 @@ static unsigned char snd_cmi8330_image[((CMI8330_CDINGAIN)-16) + 1] =
        0x0                     /* 26 - cd-in rec gain */
 };
 
-typedef int (*snd_pcm_open_callback_t)(snd_pcm_substream_t *);
+typedef int (*snd_pcm_open_callback_t)(struct snd_pcm_substream *);
 
 struct snd_cmi8330 {
 #ifdef CONFIG_PNP
        struct pnp_dev *cap;
        struct pnp_dev *play;
 #endif
-       snd_card_t *card;
-       ad1848_t *wss;
-       sb_t *sb;
+       struct snd_card *card;
+       struct snd_ad1848 *wss;
+       struct snd_sb *sb;
 
-       snd_pcm_t *pcm;
+       struct snd_pcm *pcm;
        struct snd_cmi8330_stream {
-               snd_pcm_ops_t ops;
+               struct snd_pcm_ops ops;
                snd_pcm_open_callback_t open;
                void *private_data; /* sb or wss */
        } streams[2];
 };
 
-static snd_card_t *snd_cmi8330_legacy[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
-
 #ifdef CONFIG_PNP
 
 static struct pnp_card_device_id snd_cmi8330_pnpids[] = {
@@ -170,7 +175,7 @@ MODULE_DEVICE_TABLE(pnp_card, snd_cmi8330_pnpids);
 #endif
 
 
-static struct ad1848_mix_elem snd_cmi8330_controls[] __initdata = {
+static struct ad1848_mix_elem snd_cmi8330_controls[] __devinitdata = {
 AD1848_DOUBLE("Master Playback Volume", 0, CMI8330_MASTVOL, CMI8330_MASTVOL, 4, 0, 15, 0),
 AD1848_SINGLE("Loud Playback Switch", 0, CMI8330_MUTEMUX, 6, 1, 1),
 AD1848_DOUBLE("PCM Playback Switch", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 7, 7, 1, 1),
@@ -194,12 +199,12 @@ AD1848_DOUBLE("Wavetable Capture Volume", 0, CMI8330_WAVGAIN, CMI8330_WAVGAIN, 4
 AD1848_SINGLE("3D Control - Switch", 0, CMI8330_RMUX3D, 5, 1, 1),
 AD1848_SINGLE("PC Speaker Playback Volume", 0, CMI8330_OUTPUTVOL, 3, 3, 0),
 AD1848_SINGLE("FM Playback Switch", 0, CMI8330_RECMUX, 3, 1, 1),
-AD1848_SINGLE("IEC958 Input Capture Switch", 0, CMI8330_RMUX3D, 7, 1, 1),
-AD1848_SINGLE("IEC958 Input Playback Switch", 0, CMI8330_MUTEMUX, 7, 1, 1),
+AD1848_SINGLE(SNDRV_CTL_NAME_IEC958("Input ",CAPTURE,SWITCH), 0, CMI8330_RMUX3D, 7, 1, 1),
+AD1848_SINGLE(SNDRV_CTL_NAME_IEC958("Input ",PLAYBACK,SWITCH), 0, CMI8330_MUTEMUX, 7, 1, 1),
 };
 
 #ifdef ENABLE_SB_MIXER
-static struct sbmix_elem cmi8330_sb_mixers[] __initdata = {
+static struct sbmix_elem cmi8330_sb_mixers[] __devinitdata = {
 SB_DOUBLE("SB Master Playback Volume", SB_DSP4_MASTER_DEV, (SB_DSP4_MASTER_DEV + 1), 3, 3, 31),
 SB_DOUBLE("Tone Control - Bass", SB_DSP4_BASS_DEV, (SB_DSP4_BASS_DEV + 1), 4, 4, 15),
 SB_DOUBLE("Tone Control - Treble", SB_DSP4_TREBLE_DEV, (SB_DSP4_TREBLE_DEV + 1), 4, 4, 15),
@@ -217,7 +222,7 @@ SB_DOUBLE("SB Playback Volume", SB_DSP4_OGAIN_DEV, (SB_DSP4_OGAIN_DEV + 1), 6, 6
 SB_SINGLE("SB Mic Auto Gain", SB_DSP4_MIC_AGC, 0, 1),
 };
 
-static unsigned char cmi8330_sb_init_values[][2] __initdata = {
+static unsigned char cmi8330_sb_init_values[][2] __devinitdata = {
        { SB_DSP4_MASTER_DEV + 0, 0 },
        { SB_DSP4_MASTER_DEV + 1, 0 },
        { SB_DSP4_PCM_DEV + 0, 0 },
@@ -231,7 +236,7 @@ static unsigned char cmi8330_sb_init_values[][2] __initdata = {
 };
 
 
-static int __devinit cmi8330_add_sb_mixers(sb_t *chip)
+static int __devinit cmi8330_add_sb_mixers(struct snd_sb *chip)
 {
        int idx, err;
        unsigned long flags;
@@ -256,7 +261,7 @@ static int __devinit cmi8330_add_sb_mixers(sb_t *chip)
 }
 #endif
 
-static int __devinit snd_cmi8330_mixer(snd_card_t *card, struct snd_cmi8330 *acard)
+static int __devinit snd_cmi8330_mixer(struct snd_card *card, struct snd_cmi8330 *acard)
 {
        unsigned int idx;
        int err;
@@ -370,7 +375,7 @@ static int __devinit snd_cmi8330_pnp(int dev, struct snd_cmi8330 *acard,
 #define CMI_AD_STREAM  SNDRV_PCM_STREAM_PLAYBACK
 #endif
 
-static int snd_cmi8330_playback_open(snd_pcm_substream_t * substream)
+static int snd_cmi8330_playback_open(struct snd_pcm_substream *substream)
 {
        struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
 
@@ -379,7 +384,7 @@ static int snd_cmi8330_playback_open(snd_pcm_substream_t * substream)
        return chip->streams[SNDRV_PCM_STREAM_PLAYBACK].open(substream);
 }
 
-static int snd_cmi8330_capture_open(snd_pcm_substream_t * substream)
+static int snd_cmi8330_capture_open(struct snd_pcm_substream *substream)
 {
        struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
 
@@ -388,15 +393,10 @@ static int snd_cmi8330_capture_open(snd_pcm_substream_t * substream)
        return chip->streams[SNDRV_PCM_STREAM_CAPTURE].open(substream);
 }
 
-static void snd_cmi8330_pcm_free(snd_pcm_t *pcm)
+static int __devinit snd_cmi8330_pcm(struct snd_card *card, struct snd_cmi8330 *chip)
 {
-       snd_pcm_lib_preallocate_free_for_all(pcm);
-}
-
-static int __devinit snd_cmi8330_pcm(snd_card_t *card, struct snd_cmi8330 *chip)
-{
-       snd_pcm_t *pcm;
-       const snd_pcm_ops_t *ops;
+       struct snd_pcm *pcm;
+       const struct snd_pcm_ops *ops;
        int err;
        static snd_pcm_open_callback_t cmi_open_callbacks[2] = {
                snd_cmi8330_playback_open,
@@ -407,7 +407,6 @@ static int __devinit snd_cmi8330_pcm(snd_card_t *card, struct snd_cmi8330 *chip)
                return err;
        strcpy(pcm->name, "CMI8330");
        pcm->private_data = chip;
-       pcm->private_free = snd_cmi8330_pcm_free;
        
        /* SB16 */
        ops = snd_sb16dsp_get_pcm_ops(CMI_SB_STREAM);
@@ -435,65 +434,75 @@ static int __devinit snd_cmi8330_pcm(snd_card_t *card, struct snd_cmi8330 *chip)
 }
 
 
-/*
- */
+#ifdef CONFIG_PM
+static int snd_cmi8330_suspend(struct snd_card *card)
+{
+       struct snd_cmi8330 *acard = card->private_data;
 
-static int __devinit snd_cmi8330_probe(int dev,
-                                      struct pnp_card_link *pcard,
-                                      const struct pnp_card_device_id *pid)
+       snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
+       snd_pcm_suspend_all(acard->pcm);
+       acard->wss->suspend(acard->wss);
+       snd_sbmixer_suspend(acard->sb);
+       return 0;
+}
+
+static int snd_cmi8330_resume(struct snd_card *card)
 {
-       snd_card_t *card;
-       struct snd_cmi8330 *acard;
-       unsigned long flags;
-       int i, err;
+       struct snd_cmi8330 *acard = card->private_data;
 
-#ifdef CONFIG_PNP
-       if (!isapnp[dev]) {
+       snd_sbdsp_reset(acard->sb);
+       snd_sbmixer_suspend(acard->sb);
+       acard->wss->resume(acard->wss);
+       snd_power_change_state(card, SNDRV_CTL_POWER_D0);
+       return 0;
+}
 #endif
-               if (wssport[dev] == SNDRV_AUTO_PORT) {
-                       snd_printk("specify wssport\n");
-                       return -EINVAL;
-               }
-               if (sbport[dev] == SNDRV_AUTO_PORT) {
-                       snd_printk("specify sbport\n");
-                       return -EINVAL;
-               }
+
+
+/*
+ */
+
 #ifdef CONFIG_PNP
-       }
+#define is_isapnp_selected(dev)                isapnp[dev]
+#else
+#define is_isapnp_selected(dev)                0
 #endif
+
+#define PFX    "cmi8330: "
+
+static struct snd_card *snd_cmi8330_card_new(int dev)
+{
+       struct snd_card *card;
+       struct snd_cmi8330 *acard;
+
        card = snd_card_new(index[dev], id[dev], THIS_MODULE,
                            sizeof(struct snd_cmi8330));
        if (card == NULL) {
-               snd_printk("could not get a new card\n");
-               return -ENOMEM;
+               snd_printk(KERN_ERR PFX "could not get a new card\n");
+               return NULL;
        }
-       acard = (struct snd_cmi8330 *)card->private_data;
+       acard = card->private_data;
        acard->card = card;
+       return card;
+}
 
-#ifdef CONFIG_PNP
-       if (isapnp[dev]) {
-               if ((err = snd_cmi8330_pnp(dev, acard, pcard, pid)) < 0) {
-                       snd_printk("PnP detection failed\n");
-                       snd_card_free(card);
-                       return err;
-               }
-               snd_card_set_dev(card, &pcard->card->dev);
-       }
-#endif
+static int __devinit snd_cmi8330_probe(struct snd_card *card, int dev)
+{
+       struct snd_cmi8330 *acard;
+       int i, err;
 
+       acard = card->private_data;
        if ((err = snd_ad1848_create(card,
                                     wssport[dev] + 4,
                                     wssirq[dev],
                                     wssdma[dev],
                                     AD1848_HW_DETECT,
                                     &acard->wss)) < 0) {
-               snd_printk("(AD1848) device busy??\n");
-               snd_card_free(card);
+               snd_printk(KERN_ERR PFX "(AD1848) device busy??\n");
                return err;
        }
        if (acard->wss->hardware != AD1848_HW_CMI8330) {
-               snd_printk("(AD1848) not found during probe\n");
-               snd_card_free(card);
+               snd_printk(KERN_ERR PFX "(AD1848) not found during probe\n");
                return -ENODEV;
        }
 
@@ -503,31 +512,25 @@ static int __devinit snd_cmi8330_probe(int dev,
                                    sbdma8[dev],
                                    sbdma16[dev],
                                    SB_HW_AUTO, &acard->sb)) < 0) {
-               snd_printk("(SB16) device busy??\n");
-               snd_card_free(card);
+               snd_printk(KERN_ERR PFX "(SB16) device busy??\n");
                return err;
        }
        if (acard->sb->hardware != SB_HW_16) {
-               snd_printk("(SB16) not found during probe\n");
-               snd_card_free(card);
-               return -ENODEV;
+               snd_printk(KERN_ERR PFX "(SB16) not found during probe\n");
+               return err;
        }
 
-       spin_lock_irqsave(&acard->wss->reg_lock, flags);
        snd_ad1848_out(acard->wss, AD1848_MISC_INFO, 0x40); /* switch on MODE2 */
        for (i = CMI8330_RMUX3D; i <= CMI8330_CDINGAIN; i++)
                snd_ad1848_out(acard->wss, i, snd_cmi8330_image[i - CMI8330_RMUX3D]);
-       spin_unlock_irqrestore(&acard->wss->reg_lock, flags);
 
        if ((err = snd_cmi8330_mixer(card, acard)) < 0) {
-               snd_printk("failed to create mixers\n");
-               snd_card_free(card);
+               snd_printk(KERN_ERR PFX "failed to create mixers\n");
                return err;
        }
 
        if ((err = snd_cmi8330_pcm(card, acard)) < 0) {
-               snd_printk("failed to create pcms\n");
-               snd_card_free(card);
+               snd_printk(KERN_ERR PFX "failed to create pcms\n");
                return err;
        }
 
@@ -539,79 +542,186 @@ static int __devinit snd_cmi8330_probe(int dev,
                wssirq[dev],
                wssdma[dev]);
 
-       if ((err = snd_card_register(card)) < 0) {
+       return snd_card_register(card);
+}
+
+static int __devinit snd_cmi8330_nonpnp_probe(struct platform_device *pdev)
+{
+       struct snd_card *card;
+       int err;
+       int dev = pdev->id;
+
+       if (wssport[dev] == SNDRV_AUTO_PORT) {
+               snd_printk(KERN_ERR PFX "specify wssport\n");
+               return -EINVAL;
+       }
+       if (sbport[dev] == SNDRV_AUTO_PORT) {
+               snd_printk(KERN_ERR PFX "specify sbport\n");
+               return -EINVAL;
+       }
+
+       card = snd_cmi8330_card_new(dev);
+       if (! card)
+               return -ENOMEM;
+       snd_card_set_dev(card, &pdev->dev);
+       if ((err = snd_cmi8330_probe(card, dev)) < 0) {
                snd_card_free(card);
                return err;
        }
+       platform_set_drvdata(pdev, card);
+       return 0;
+}
 
-       if (pcard)
-               pnp_set_card_drvdata(pcard, card);
-       else
-               snd_cmi8330_legacy[dev] = card;
+static int snd_cmi8330_nonpnp_remove(struct platform_device *devptr)
+{
+       snd_card_free(platform_get_drvdata(devptr));
+       platform_set_drvdata(devptr, NULL);
        return 0;
 }
 
+#ifdef CONFIG_PM
+static int snd_cmi8330_nonpnp_suspend(struct platform_device *dev, pm_message_t state)
+{
+       return snd_cmi8330_suspend(platform_get_drvdata(dev));
+}
+
+static int snd_cmi8330_nonpnp_resume(struct platform_device *dev)
+{
+       return snd_cmi8330_resume(platform_get_drvdata(dev));
+}
+#endif
+
+#define CMI8330_DRIVER "snd_cmi8330"
+
+static struct platform_driver snd_cmi8330_driver = {
+       .probe          = snd_cmi8330_nonpnp_probe,
+       .remove         = snd_cmi8330_nonpnp_remove,
+#ifdef CONFIG_PM
+       .suspend        = snd_cmi8330_nonpnp_suspend,
+       .resume         = snd_cmi8330_nonpnp_resume,
+#endif
+       .driver         = {
+               .name   = CMI8330_DRIVER
+       },
+};
+
+
 #ifdef CONFIG_PNP
-static int __devinit snd_cmi8330_pnp_detect(struct pnp_card_link *card,
-                                           const struct pnp_card_device_id *id)
+static unsigned int __devinitdata cmi8330_pnp_devices;
+
+static int __devinit snd_cmi8330_pnp_detect(struct pnp_card_link *pcard,
+                                           const struct pnp_card_device_id *pid)
 {
        static int dev;
+       struct snd_card *card;
        int res;
 
        for ( ; dev < SNDRV_CARDS; dev++) {
-               if (!enable[dev] || !isapnp[dev])
-                       continue;
-               res = snd_cmi8330_probe(dev, card, id);
-               if (res < 0)
-                       return res;
-               dev++;
-               return 0;
+               if (enable[dev] && isapnp[dev])
+                       break;
+       }
+       if (dev >= SNDRV_CARDS)
+               return -ENODEV;
+                              
+       card = snd_cmi8330_card_new(dev);
+       if (! card)
+               return -ENOMEM;
+       if ((res = snd_cmi8330_pnp(dev, card->private_data, pcard, pid)) < 0) {
+               snd_printk(KERN_ERR PFX "PnP detection failed\n");
+               snd_card_free(card);
+               return res;
+       }
+       snd_card_set_dev(card, &pcard->card->dev);
+       if ((res = snd_cmi8330_probe(card, dev)) < 0) {
+               snd_card_free(card);
+               return res;
        }
-       return -ENODEV;
+       pnp_set_card_drvdata(pcard, card);
+       dev++;
+       cmi8330_pnp_devices++;
+       return 0;
 }
 
 static void __devexit snd_cmi8330_pnp_remove(struct pnp_card_link * pcard)
 {
-       snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
+       snd_card_free(pnp_get_card_drvdata(pcard));
+       pnp_set_card_drvdata(pcard, NULL);
+}
 
-       snd_card_disconnect(card);
-       snd_card_free_in_thread(card);
+#ifdef CONFIG_PM
+static int snd_cmi8330_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
+{
+       return snd_cmi8330_suspend(pnp_get_card_drvdata(pcard));
 }
 
+static int snd_cmi8330_pnp_resume(struct pnp_card_link *pcard)
+{
+       return snd_cmi8330_resume(pnp_get_card_drvdata(pcard));
+}
+#endif
+
 static struct pnp_card_driver cmi8330_pnpc_driver = {
        .flags = PNP_DRIVER_RES_DISABLE,
        .name = "cmi8330",
        .id_table = snd_cmi8330_pnpids,
        .probe = snd_cmi8330_pnp_detect,
        .remove = __devexit_p(snd_cmi8330_pnp_remove),
+#ifdef CONFIG_PM
+       .suspend        = snd_cmi8330_pnp_suspend,
+       .resume         = snd_cmi8330_pnp_resume,
+#endif
 };
 #endif /* CONFIG_PNP */
 
+static void __init_or_module snd_cmi8330_unregister_all(void)
+{
+       int i;
+
+#ifdef CONFIG_PNP
+       if (pnp_registered)
+               pnp_unregister_card_driver(&cmi8330_pnpc_driver);
+#endif
+       for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
+               platform_device_unregister(platform_devices[i]);
+       platform_driver_unregister(&snd_cmi8330_driver);
+}
+
 static int __init alsa_card_cmi8330_init(void)
 {
-       int dev, cards = 0;
+       int i, err, cards = 0;
+
+       if ((err = platform_driver_register(&snd_cmi8330_driver)) < 0)
+               return err;
 
-       for (dev = 0; dev < SNDRV_CARDS; dev++) {
-               if (!enable[dev])
+       for (i = 0; i < SNDRV_CARDS; i++) {
+               struct platform_device *device;
+               if (! enable[i] || is_isapnp_selected(i))
                        continue;
-#ifdef CONFIG_PNP
-               if (isapnp[dev])
+               device = platform_device_register_simple(CMI8330_DRIVER,
+                                                        i, NULL, 0);
+               if (IS_ERR(device))
                        continue;
-#endif
-               if (snd_cmi8330_probe(dev, NULL, NULL) >= 0)
-                       cards++;
+               if (!platform_get_drvdata(device)) {
+                       platform_device_unregister(device);
+                       continue;
+               }
+               platform_devices[i] = device;
+               cards++;
        }
+
 #ifdef CONFIG_PNP
-       cards += pnp_register_card_driver(&cmi8330_pnpc_driver);
+       err = pnp_register_card_driver(&cmi8330_pnpc_driver);
+       if (!err) {
+               pnp_registered = 1;
+               cards += cmi8330_pnp_devices;
+       }
 #endif
 
        if (!cards) {
-#ifdef CONFIG_PNP
-               pnp_unregister_card_driver(&cmi8330_pnpc_driver);
-#endif
 #ifdef MODULE
                snd_printk(KERN_ERR "CMI8330 not found or device busy\n");
 #endif
+               snd_cmi8330_unregister_all();
                return -ENODEV;
        }
        return 0;
@@ -619,14 +729,7 @@ static int __init alsa_card_cmi8330_init(void)
 
 static void __exit alsa_card_cmi8330_exit(void)
 {
-       int i;
-
-#ifdef CONFIG_PNP
-       /* PnP cards first */
-       pnp_unregister_card_driver(&cmi8330_pnpc_driver);
-#endif
-       for (i = 0; i < SNDRV_CARDS; i++)
-               snd_card_free(snd_cmi8330_legacy[i]);
+       snd_cmi8330_unregister_all();
 }
 
 module_init(alsa_card_cmi8330_init)