[ALSA] mpu401 - Use platform_device

Modules: MPU401 UART

Rewrite the probe/remove code using platform_device.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
diff --git a/sound/drivers/mpu401/mpu401.c b/sound/drivers/mpu401/mpu401.c
index b1242ee..ec817a8 100644
--- a/sound/drivers/mpu401/mpu401.c
+++ b/sound/drivers/mpu401/mpu401.c
@@ -23,6 +23,8 @@
 #include <sound/driver.h>
 #include <linux/init.h>
 #include <linux/pnp.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
 #include <linux/moduleparam.h>
 #include <sound/core.h>
 #include <sound/mpu401.h>
@@ -56,7 +58,6 @@
 module_param_array(irq, int, NULL, 0444);
 MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
 
-static struct snd_card *snd_mpu401_legacy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
 static int pnp_registered = 0;
 
 static int snd_mpu401_create(int dev, struct snd_card **rcard)
@@ -85,12 +86,6 @@
 		goto _err;
 	}
 
-	if ((err = snd_card_set_generic_dev(card)) < 0)
-		goto _err;
-
-	if ((err = snd_card_register(card)) < 0)
-		goto _err;
-
 	*rcard = card;
 	return 0;
 
@@ -99,8 +94,12 @@
 	return err;
 }
 
-static int __devinit snd_mpu401_probe(int dev)
+static int __devinit snd_mpu401_probe(struct platform_device *devptr)
 {
+	int dev = devptr->id;
+	int err;
+	struct snd_card *card;
+
 	if (port[dev] == SNDRV_AUTO_PORT) {
 		snd_printk(KERN_ERR "specify port\n");
 		return -EINVAL;
@@ -109,9 +108,36 @@
 		snd_printk(KERN_ERR "specify or disable IRQ\n");
 		return -EINVAL;
 	}
-	return snd_mpu401_create(dev, &snd_mpu401_legacy_cards[dev]);
+	err = snd_mpu401_create(dev, &card);
+	if (err < 0)
+		return err;
+	snd_card_set_dev(card, &devptr->dev);
+	if ((err = snd_card_register(card)) < 0) {
+		snd_card_free(card);
+		return err;
+	}
+	platform_set_drvdata(devptr, card);
+	return 0;
 }
 
+static int __devexit snd_mpu401_remove(struct platform_device *devptr)
+{
+	snd_card_free(platform_get_drvdata(devptr));
+	platform_set_drvdata(devptr, NULL);
+	return 0;
+}
+
+#define SND_MPU401_DRIVER	"snd_mpu401"
+
+static struct platform_driver snd_mpu401_driver = {
+	.probe		= snd_mpu401_probe,
+	.remove		= __devexit_p(snd_mpu401_remove),
+	.driver		= {
+		.name	= SND_MPU401_DRIVER
+	},
+};
+
+
 #ifdef CONFIG_PNP
 
 #define IO_EXTENT 2
@@ -164,6 +190,10 @@
 		err = snd_mpu401_create(dev, &card);
 		if (err < 0)
 			return err;
+		if ((err = snd_card_register(card)) < 0) {
+			snd_card_free(card);
+			return err;
+		}
 		snd_card_set_dev(card, &pnp_dev->dev);
 		pnp_set_drvdata(pnp_dev, card);
 		++dev;
@@ -192,18 +222,25 @@
 
 static int __init alsa_card_mpu401_init(void)
 {
-	int dev, devices = 0;
-	int err;
+	int i, err, devices;
 
-	for (dev = 0; dev < SNDRV_CARDS; dev++) {
-		if (!enable[dev])
-			continue;
+	if ((err = platform_driver_register(&snd_mpu401_driver)) < 0)
+		return err;
+
+	devices = 0;
+	for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
+		struct platform_device *device;
 #ifdef CONFIG_PNP
-		if (pnp[dev])
+		if (pnp[i])
 			continue;
 #endif
-		if (snd_mpu401_probe(dev) >= 0)
-			devices++;
+		device = platform_device_register_simple(SND_MPU401_DRIVER,
+							 i, NULL, 0);
+		if (IS_ERR(device)) {
+			err = PTR_ERR(device);
+			goto errout;
+		}
+		devices++;
 	}
 	if ((err = pnp_register_driver(&snd_mpu401_pnp_driver)) >= 0) {
 		pnp_registered = 1;
@@ -214,21 +251,23 @@
 #ifdef MODULE
 		printk(KERN_ERR "MPU-401 device not found or device busy\n");
 #endif
-		if (pnp_registered)
-			pnp_unregister_driver(&snd_mpu401_pnp_driver);
-		return -ENODEV;
+		err = -ENODEV;
+		goto errout;
 	}
 	return 0;
+
+ errout:
+	if (pnp_registered)
+		pnp_unregister_driver(&snd_mpu401_pnp_driver);
+	platform_driver_unregister(&snd_mpu401_driver);
+	return err;
 }
 
 static void __exit alsa_card_mpu401_exit(void)
 {
-	int idx;
-
 	if (pnp_registered)
 		pnp_unregister_driver(&snd_mpu401_pnp_driver);
-	for (idx = 0; idx < SNDRV_CARDS; idx++)
-		snd_card_free(snd_mpu401_legacy_cards[idx]);
+	platform_driver_unregister(&snd_mpu401_driver);
 }
 
 module_init(alsa_card_mpu401_init)