[ALSA] mtpav - Use platform_device

Modules: Generic drivers

Rewrite the probe/remove code using platform_device.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
diff --git a/sound/drivers/mtpav.c b/sound/drivers/mtpav.c
index d32d5f6..d9c4e22 100644
--- a/sound/drivers/mtpav.c
+++ b/sound/drivers/mtpav.c
@@ -53,6 +53,8 @@
 #include <sound/driver.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/ioport.h>
 #include <linux/moduleparam.h>
@@ -125,16 +127,16 @@
 /*
  */
 
-typedef struct mtpav_port {
+struct mtpav_port {
 	u8 number;
 	u8 hwport;
 	u8 mode;
 	u8 running_status;
 	struct snd_rawmidi_substream *input;
 	struct snd_rawmidi_substream *output;
-} mtpav_port_t;
+};
 
-typedef struct mtpav {
+struct mtpav {
 	struct snd_card *card;
 	unsigned long port;
 	struct resource *res_port;
@@ -145,22 +147,16 @@
 	struct timer_list timer;	/* timer interrupts for outputs */
 	struct snd_rawmidi *rmidi;
 	int num_ports;		/* number of hw ports (1-8) */
-	mtpav_port_t ports[NUMPORTS];	/* all ports including computer, adat and bc */
+	struct mtpav_port ports[NUMPORTS];	/* all ports including computer, adat and bc */
 
 	u32 inmidiport;		/* selected input midi port */
 	u32 inmidistate;	/* during midi command 0xf5 */
 
 	u32 outmidihwport;	/* selected output midi hw port */
-} mtpav_t;
+};
 
 
 /*
- * global instance
- * hey, we handle at most only one card..
- */
-static mtpav_t *mtp_card;
-
-/*
  * possible hardware ports (selected by 0xf5 port message)
  *      0x00		all ports
  *      0x01 .. 0x08    this MTP's ports 1..8
@@ -183,7 +179,7 @@
 #define MTPAV_PIDX_BROADCAST	2
 
 
-static int translate_subdevice_to_hwport(mtpav_t *chip, int subdev)
+static int translate_subdevice_to_hwport(struct mtpav *chip, int subdev)
 {
 	if (subdev < 0)
 		return 0x01; /* invalid - use port 0 as default */
@@ -198,7 +194,7 @@
 	return 0; /* all ports */
 }
 
-static int translate_hwport_to_subdevice(mtpav_t *chip, int hwport)
+static int translate_hwport_to_subdevice(struct mtpav *chip, int hwport)
 {
 	int p;
 	if (hwport <= 0x00) /* all ports */
@@ -223,7 +219,7 @@
 /*
  */
 
-static u8 snd_mtpav_getreg(mtpav_t *chip, u16 reg)
+static u8 snd_mtpav_getreg(struct mtpav *chip, u16 reg)
 {
 	u8 rval = 0;
 
@@ -241,19 +237,16 @@
 /*
  */
 
-static void snd_mtpav_mputreg(mtpav_t *chip, u16 reg, u8 val)
+static inline void snd_mtpav_mputreg(struct mtpav *chip, u16 reg, u8 val)
 {
-	if (reg == DREG) {
-		outb(val, chip->port + DREG);
-	} else if (reg == CREG) {
-		outb(val, chip->port + CREG);
-	}
+	if (reg == DREG || reg == CREG)
+		outb(val, chip->port + reg);
 }
 
 /*
  */
 
-static void snd_mtpav_wait_rfdhi(mtpav_t *chip)
+static void snd_mtpav_wait_rfdhi(struct mtpav *chip)
 {
 	int counts = 10000;
 	u8 sbyte;
@@ -265,7 +258,7 @@
 	}
 }
 
-static void snd_mtpav_send_byte(mtpav_t *chip, u8 byte)
+static void snd_mtpav_send_byte(struct mtpav *chip, u8 byte)
 {
 	u8 tcbyt;
 	u8 clrwrite;
@@ -291,7 +284,8 @@
  */
 
 /* call this with spin lock held */
-static void snd_mtpav_output_port_write(mtpav_port_t *port,
+static void snd_mtpav_output_port_write(struct mtpav *mtp_card,
+					struct mtpav_port *portp,
 					struct snd_rawmidi_substream *substream)
 {
 	u8 outbyte;
@@ -303,22 +297,22 @@
 
 	// send port change command if necessary
 
-	if (port->hwport != mtp_card->outmidihwport) {
-		mtp_card->outmidihwport = port->hwport;
+	if (portp->hwport != mtp_card->outmidihwport) {
+		mtp_card->outmidihwport = portp->hwport;
 
 		snd_mtpav_send_byte(mtp_card, 0xf5);
-		snd_mtpav_send_byte(mtp_card, port->hwport);
-		//snd_printk("new outport: 0x%x\n", (unsigned int) port->hwport);
+		snd_mtpav_send_byte(mtp_card, portp->hwport);
+		//snd_printk("new outport: 0x%x\n", (unsigned int) portp->hwport);
 
-		if (!(outbyte & 0x80) && port->running_status)
-			snd_mtpav_send_byte(mtp_card, port->running_status);
+		if (!(outbyte & 0x80) && portp->running_status)
+			snd_mtpav_send_byte(mtp_card, portp->running_status);
 	}
 
 	// send data
 
 	do {
 		if (outbyte & 0x80)
-			port->running_status = outbyte;
+			portp->running_status = outbyte;
 		
 		snd_mtpav_send_byte(mtp_card, outbyte);
 	} while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
@@ -326,11 +320,12 @@
 
 static void snd_mtpav_output_write(struct snd_rawmidi_substream *substream)
 {
-	mtpav_port_t *port = &mtp_card->ports[substream->number];
+	struct mtpav *mtp_card = substream->rmidi->private_data;
+	struct mtpav_port *portp = &mtp_card->ports[substream->number];
 	unsigned long flags;
 
 	spin_lock_irqsave(&mtp_card->spinlock, flags);
-	snd_mtpav_output_port_write(port, substream);
+	snd_mtpav_output_port_write(mtp_card, portp, substream);
 	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
 }
 
@@ -339,7 +334,7 @@
  *      mtpav control
  */
 
-static void snd_mtpav_portscan(mtpav_t *chip)	// put mtp into smart routing mode
+static void snd_mtpav_portscan(struct mtpav *chip)	// put mtp into smart routing mode
 {
 	u8 p;
 
@@ -355,10 +350,10 @@
 
 static int snd_mtpav_input_open(struct snd_rawmidi_substream *substream)
 {
+	struct mtpav *mtp_card = substream->rmidi->private_data;
+	struct mtpav_port *portp = &mtp_card->ports[substream->number];
 	unsigned long flags;
-	mtpav_port_t *portp = &mtp_card->ports[substream->number];
 
-	//printk("mtpav port: %d opened\n", (int) substream->number);
 	spin_lock_irqsave(&mtp_card->spinlock, flags);
 	portp->mode |= MTPAV_MODE_INPUT_OPENED;
 	portp->input = substream;
@@ -373,18 +368,15 @@
 
 static int snd_mtpav_input_close(struct snd_rawmidi_substream *substream)
 {
+	struct mtpav *mtp_card = substream->rmidi->private_data;
+	struct mtpav_port *portp = &mtp_card->ports[substream->number];
 	unsigned long flags;
-	mtpav_port_t *portp = &mtp_card->ports[substream->number];
-
-	//printk("mtpav port: %d closed\n", (int) portp);
 
 	spin_lock_irqsave(&mtp_card->spinlock, flags);
-
-	portp->mode &= (~MTPAV_MODE_INPUT_OPENED);
+	portp->mode &= ~MTPAV_MODE_INPUT_OPENED;
 	portp->input = NULL;
 	if (--mtp_card->share_irq == 0)
 		snd_mtpav_mputreg(mtp_card, CREG, 0);	// disable pport interrupts
-
 	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
 	return 0;
 }
@@ -394,8 +386,9 @@
 
 static void snd_mtpav_input_trigger(struct snd_rawmidi_substream *substream, int up)
 {
+	struct mtpav *mtp_card = substream->rmidi->private_data;
+	struct mtpav_port *portp = &mtp_card->ports[substream->number];
 	unsigned long flags;
-	mtpav_port_t *portp = &mtp_card->ports[substream->number];
 
 	spin_lock_irqsave(&mtp_card->spinlock, flags);
 	if (up)
@@ -414,7 +407,7 @@
 static void snd_mtpav_output_timer(unsigned long data)
 {
 	unsigned long flags;
-	mtpav_t *chip = (mtpav_t *)data;
+	struct mtpav *chip = (struct mtpav *)data;
 	int p;
 
 	spin_lock_irqsave(&chip->spinlock, flags);
@@ -423,25 +416,22 @@
 	add_timer(&chip->timer);
 	/* process each port */
 	for (p = 0; p <= chip->num_ports * 2 + MTPAV_PIDX_BROADCAST; p++) {
-		mtpav_port_t *portp = &mtp_card->ports[p];
+		struct mtpav_port *portp = &chip->ports[p];
 		if ((portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED) && portp->output)
-			snd_mtpav_output_port_write(portp, portp->output);
+			snd_mtpav_output_port_write(chip, portp, portp->output);
 	}
 	spin_unlock_irqrestore(&chip->spinlock, flags);
 }
 
 /* spinlock held! */
-static void snd_mtpav_add_output_timer(mtpav_t *chip)
+static void snd_mtpav_add_output_timer(struct mtpav *chip)
 {
-	init_timer(&chip->timer);
-	chip->timer.function = snd_mtpav_output_timer;
-	chip->timer.data = (unsigned long) mtp_card;
 	chip->timer.expires = 1 + jiffies;
 	add_timer(&chip->timer);
 }
 
 /* spinlock held! */
-static void snd_mtpav_remove_output_timer(mtpav_t *chip)
+static void snd_mtpav_remove_output_timer(struct mtpav *chip)
 {
 	del_timer(&chip->timer);
 }
@@ -451,8 +441,9 @@
 
 static int snd_mtpav_output_open(struct snd_rawmidi_substream *substream)
 {
+	struct mtpav *mtp_card = substream->rmidi->private_data;
+	struct mtpav_port *portp = &mtp_card->ports[substream->number];
 	unsigned long flags;
-	mtpav_port_t *portp = &mtp_card->ports[substream->number];
 
 	spin_lock_irqsave(&mtp_card->spinlock, flags);
 	portp->mode |= MTPAV_MODE_OUTPUT_OPENED;
@@ -466,11 +457,12 @@
 
 static int snd_mtpav_output_close(struct snd_rawmidi_substream *substream)
 {
+	struct mtpav *mtp_card = substream->rmidi->private_data;
+	struct mtpav_port *portp = &mtp_card->ports[substream->number];
 	unsigned long flags;
-	mtpav_port_t *portp = &mtp_card->ports[substream->number];
 
 	spin_lock_irqsave(&mtp_card->spinlock, flags);
-	portp->mode &= (~MTPAV_MODE_OUTPUT_OPENED);
+	portp->mode &= ~MTPAV_MODE_OUTPUT_OPENED;
 	portp->output = NULL;
 	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
 	return 0;
@@ -481,12 +473,13 @@
 
 static void snd_mtpav_output_trigger(struct snd_rawmidi_substream *substream, int up)
 {
+	struct mtpav *mtp_card = substream->rmidi->private_data;
+	struct mtpav_port *portp = &mtp_card->ports[substream->number];
 	unsigned long flags;
-	mtpav_port_t *portp = &mtp_card->ports[substream->number];
 
 	spin_lock_irqsave(&mtp_card->spinlock, flags);
 	if (up) {
-		if (! (portp->mode  & MTPAV_MODE_OUTPUT_TRIGGERED)) {
+		if (! (portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED)) {
 			if (mtp_card->istimer++ == 0)
 				snd_mtpav_add_output_timer(mtp_card);
 			portp->mode |= MTPAV_MODE_OUTPUT_TRIGGERED;
@@ -506,23 +499,20 @@
  * midi interrupt for inputs
  */
 
-static void snd_mtpav_inmidi_process(mtpav_t *mcrd, u8 inbyte)
+static void snd_mtpav_inmidi_process(struct mtpav *mcrd, u8 inbyte)
 {
-	mtpav_port_t *portp;
+	struct mtpav_port *portp;
 
 	if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
 		return;
 
 	portp = &mcrd->ports[mcrd->inmidiport];
-	if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED) {
+	if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED)
 		snd_rawmidi_receive(portp->input, &inbyte, 1);
-	}
 }
 
-static void snd_mtpav_inmidi_h(mtpav_t * mcrd, u8 inbyte)
+static void snd_mtpav_inmidi_h(struct mtpav *mcrd, u8 inbyte)
 {
-	snd_assert(mcrd, return);
-
 	if (inbyte >= 0xf8) {
 		/* real-time midi code */
 		snd_mtpav_inmidi_process(mcrd, inbyte);
@@ -540,7 +530,7 @@
 	}
 }
 
-static void snd_mtpav_read_bytes(mtpav_t * mcrd)
+static void snd_mtpav_read_bytes(struct mtpav *mcrd)
 {
 	u8 clrread, setread;
 	u8 mtp_read_byte;
@@ -580,9 +570,8 @@
 
 static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id, struct pt_regs *regs)
 {
-	mtpav_t *mcard = dev_id;
+	struct mtpav *mcard = dev_id;
 
-	//printk("irqh()\n");
 	spin_lock(&mcard->spinlock);
 	snd_mtpav_read_bytes(mcard);
 	spin_unlock(&mcard->spinlock);
@@ -592,14 +581,14 @@
 /*
  * get ISA resources
  */
-static int snd_mtpav_get_ISA(mtpav_t * mcard)
+static int __init snd_mtpav_get_ISA(struct mtpav * mcard)
 {
 	if ((mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI")) == NULL) {
 		snd_printk("MTVAP port 0x%lx is busy\n", port);
 		return -EBUSY;
 	}
 	mcard->port = port;
-	if (request_irq(irq, snd_mtpav_irqh, SA_INTERRUPT, "MOTU MTPAV", (void *)mcard)) {
+	if (request_irq(irq, snd_mtpav_irqh, SA_INTERRUPT, "MOTU MTPAV", mcard)) {
 		snd_printk("MTVAP IRQ %d busy\n", irq);
 		return -EBUSY;
 	}
@@ -628,7 +617,8 @@
  * get RAWMIDI resources
  */
 
-static void snd_mtpav_set_name(mtpav_t *chip, struct snd_rawmidi_substream *substream)
+static void __init snd_mtpav_set_name(struct mtpav *chip,
+				      struct snd_rawmidi_substream *substream)
 {
 	if (substream->number >= 0 && substream->number < chip->num_ports)
 		sprintf(substream->name, "MTP direct %d", (substream->number % chip->num_ports) + 1);
@@ -642,21 +632,18 @@
 		strcpy(substream->name, "MTP broadcast");
 }
 
-static int snd_mtpav_get_RAWMIDI(mtpav_t * mcard)
+static int __init snd_mtpav_get_RAWMIDI(struct mtpav *mcard)
 {
-	int rval = 0;
+	int rval;
 	struct snd_rawmidi *rawmidi;
 	struct snd_rawmidi_substream *substream;
 	struct list_head *list;
 
-	//printk("entering snd_mtpav_get_RAWMIDI\n");
-
 	if (hwports < 1)
-		mcard->num_ports = 1;
+		hwports = 1;
 	else if (hwports > 8)
-		mcard->num_ports = 8;
-	else
-		mcard->num_ports = hwports;
+		hwports = 8;
+	mcard->num_ports = hwports;
 
 	if ((rval = snd_rawmidi_new(mcard->card, "MotuMIDI", 0,
 				    mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
@@ -664,6 +651,7 @@
 				    &mcard->rmidi)) < 0)
 		return rval;
 	rawmidi = mcard->rmidi;
+	rawmidi->private_data = mcard;
 
 	list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
 		substream = list_entry(list, struct snd_rawmidi_substream, list);
@@ -679,36 +667,15 @@
 	rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
 			       SNDRV_RAWMIDI_INFO_DUPLEX;
 	sprintf(rawmidi->name, "MTP AV MIDI");
-	//printk("exiting snd_mtpav_get_RAWMIDI() \n");
 	return 0;
 }
 
 /*
  */
 
-static mtpav_t *new_mtpav(void)
+static void snd_mtpav_free(struct snd_card *card)
 {
-	mtpav_t *ncrd = kzalloc(sizeof(*ncrd), GFP_KERNEL);
-	if (ncrd != NULL) {
-		spin_lock_init(&ncrd->spinlock);
-
-		init_timer(&ncrd->timer);
-		ncrd->card = NULL;
-		ncrd->irq = -1;
-		ncrd->share_irq = 0;
-
-		ncrd->inmidiport = 0xffffffff;
-		ncrd->inmidistate = 0;
-		ncrd->outmidihwport = 0xffffffff;
-	}
-	return ncrd;
-}
-
-/*
- */
-
-static void free_mtpav(mtpav_t * crd)
-{
+	struct mtpav *crd = card->private_data;
 	unsigned long flags;
 
 	spin_lock_irqsave(&crd->spinlock, flags);
@@ -718,78 +685,101 @@
 	if (crd->irq >= 0)
 		free_irq(crd->irq, (void *)crd);
 	release_and_free_resource(crd->res_port);
-	kfree(crd);
 }
 
 /*
  */
-
-static int __init alsa_card_mtpav_init(void)
+static int __init snd_mtpav_probe(struct platform_device *dev)
 {
-	int err = 0;
-	char longname_buffer[80];
+	struct snd_card *card;
+	int err;
+	struct mtpav *mtp_card;
 
-	mtp_card = new_mtpav();
-	if (mtp_card == NULL)
+	card = snd_card_new(index, id, THIS_MODULE, sizeof(*mtp_card));
+	if (! card)
 		return -ENOMEM;
 
-	mtp_card->card = snd_card_new(index, id, THIS_MODULE, 0);
-	if (mtp_card->card == NULL) {
-		free_mtpav(mtp_card);
-		return -ENOMEM;
-	}
+	mtp_card = card->private_data;
+	spin_lock_init(&mtp_card->spinlock);
+	init_timer(&mtp_card->timer);
+	mtp_card->card = card;
+	mtp_card->irq = -1;
+	mtp_card->share_irq = 0;
+	mtp_card->inmidiport = 0xffffffff;
+	mtp_card->inmidistate = 0;
+	mtp_card->outmidihwport = 0xffffffff;
+	init_timer(&mtp_card->timer);
+	mtp_card->timer.function = snd_mtpav_output_timer;
+	mtp_card->timer.data = (unsigned long) mtp_card;
+
+	card->private_free = snd_mtpav_free;
 
 	err = snd_mtpav_get_ISA(mtp_card);
-	//printk("snd_mtpav_get_ISA returned: %d\n", err);
 	if (err < 0)
 		goto __error;
 
-	strcpy(mtp_card->card->driver, "MTPAV");
-	strcpy(mtp_card->card->shortname, "MTPAV on parallel port");
-	memset(longname_buffer, 0, sizeof(longname_buffer));
-	sprintf(longname_buffer, "MTPAV on parallel port at");
+	strcpy(card->driver, "MTPAV");
+	strcpy(card->shortname, "MTPAV on parallel port");
+	snprintf(card->longname, sizeof(card->longname),
+		 "MTPAV on parallel port at 0x%lx", port);
 
 	err = snd_mtpav_get_RAWMIDI(mtp_card);
-	//snd_printk("snd_mtapv_get_RAWMIDI returned: %d\n", err);
 	if (err < 0)
 		goto __error;
 
-	if ((err = snd_card_set_generic_dev(mtp_card->card)) < 0)
-		goto __error;
-
-	err = snd_card_register(mtp_card->card);	// don't snd_card_register until AFTER all cards reources done!
-
-	//printk("snd_card_register returned %d\n", err);
-	if (err < 0)
-		goto __error;
-
-
 	snd_mtpav_portscan(mtp_card);
 
-	printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
+	snd_card_set_dev(card, &dev->dev);
+	err = snd_card_register(mtp_card->card);
+	if (err < 0)
+		goto __error;
 
+	platform_set_drvdata(dev, card);
+	printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
 	return 0;
 
-      __error:
-	snd_card_free(mtp_card->card);
-	free_mtpav(mtp_card);
+ __error:
+	snd_card_free(card);
 	return err;
 }
 
-/*
- */
+static int snd_mtpav_remove(struct platform_device *devptr)
+{
+	snd_card_free(platform_get_drvdata(devptr));
+	platform_set_drvdata(devptr, NULL);
+	return 0;
+}
+
+#define SND_MTPAV_DRIVER	"snd_mtpav"
+
+static struct platform_driver snd_mtpav_driver = {
+	.probe		= snd_mtpav_probe,
+	.remove		= snd_mtpav_remove,
+	.driver		= {
+		.name	= SND_MTPAV_DRIVER
+	},
+};
+
+static int __init alsa_card_mtpav_init(void)
+{
+	int err;
+	struct platform_device *device;
+
+	if ((err = platform_driver_register(&snd_mtpav_driver)) < 0)
+		return err;
+
+	device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
+	if (IS_ERR(device)) {
+		platform_driver_unregister(&snd_mtpav_driver);
+		return PTR_ERR(device);
+	}
+	return 0;
+}
 
 static void __exit alsa_card_mtpav_exit(void)
 {
-	if (mtp_card == NULL)
-		return;
-	if (mtp_card->card)
-		snd_card_free(mtp_card->card);
-	free_mtpav(mtp_card);
+	platform_driver_unregister(&snd_mtpav_driver);
 }
 
-/*
- */
-
 module_init(alsa_card_mtpav_init)
 module_exit(alsa_card_mtpav_exit)