Skip to contentSkip to conversation

alsa-driver: Support for ESI Miditerminal 4140 (#2)

PATCH In review Matthias Koenig 2 participants 3 messages Apr 25, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alsa-driver: Support for ESI Miditerminal 4140 (#2)

Add support for the ESI Miditerminal 4140 device in alsa-driver.

update includes:
- Removed all typedef's
- Proper Kernel CodingStyle
- mdelay fix

---

drivers/Kconfig +13 -0
diff -r 4e7af6120ca7 drivers/Kconfig
@@ -43,3 +43,16 @@ config SND_PCSP
 	  You can compile this as a module, which will be called snd-pcsp.
 
 	  You don't need this driver if you only want your computer to beep.
+
+config SND_MTS64
+	tristate "ESI Miditerminal 4140 driver"
+	depends on SND && PARPORT
+	select SND_RAWMIDI
+	help
+	  The ESI Miditerminal 4140 is a 4 In 4 Out MIDI Interface with 
+          additional SMPTE Timecode capabilities for the parallel port.
+
+	  Say 'Y' to include support for this device.
+
+	  To compile this driver as a module, chose 'M' here: the module 
+          will be called snd-mts64.
drivers/Makefile +2 -1
diff -r 4e7af6120ca7 drivers/Makefile
@@ -10,12 +10,13 @@ snd-serialmidi-objs := serialmidi.o
 snd-serialmidi-objs := serialmidi.o
 snd-aloop-objs := aloop.o
 snd-portman2x4-objs := portman2x4.o
+snd-mts64-objs := mts64.o
 
 obj-$(CONFIG_SND) += pcsp/
 
 obj-$(CONFIG_SND_SERIALMIDI) += snd-serialmidi.o
 obj-$(CONFIG_SND_LOOPBACK) += snd-aloop.o
-obj-$(CONFIG_SND_PORTMAN2X4) += snd-portman2x4.o
+obj-$(CONFIG_SND_MTS64) += snd-mts64.o
 
 include $(SND_TOPDIR)/alsa-kernel/drivers/Makefile
 
drivers/mts64.c new file +1122 -0

1,122 lines not on this page · raw message

#1 lore raw

At Tue, 25 Apr 2006 17:37:18 +0200,

Quoted 1 line · Matthias Koenig

+static int mts64_device_init(struct parport *p)

You can add __devinit since this is called only from probe function.

drivers/mts64.c line 223

Quoted 7 lines
drivers/mts64.c new file +7 -0
+{
+	int i;
+	unsigned long flags;
+
+	local_irq_save(flags);	
+	mts64_write_command(p, MTS64_CMD_RESET);
+	local_irq_restore(flags);

Do we need irq_save/restore here?
I see no possible races around this function.

drivers/mts64.c line 307

Quoted 9 lines
drivers/mts64.c new file +9 -0
+static int mts64_probe(struct parport *p)
+{
+	unsigned char c;
+	unsigned long flags;
+
+	local_irq_save(flags);
+	mts64_smpte_stop(p);
+	mts64_write_command(p, MTS64_CMD_PROBE);
+	local_irq_restore(flags);

Ditto. It's also __devinit function.

Quoted 4 lines
drivers/mts64.c new file +4 -0
+static int snd_mts64_ctl_smpte_switch_put(struct snd_kcontrol* kctl,
+					  struct snd_ctl_elem_value *uctl)
+{
+	struct mts64 *mts = snd_kcontrol_chip(kctl);

drivers/mts64.c line 487

Quoted 10 lines
drivers/mts64.c new file +10 -0
+	int changed = 0;
+	unsigned long flags;
+
+	if (mts->smpte_switch == uctl->value.integer.value[0])
+		return changed;
+
+	changed = 1;
+	mts->smpte_switch = uctl->value.integer.value[0];
+	if (mts->smpte_switch) {
+		spin_lock_irqsave(mts->lock, flags);

You need no irqsave in get/put callbacks. Just use spin_lock_irq()/spin_unlock_irq().

Quoted 33 lines
drivers/mts64.c new file +33 -0
+static int __devinit snd_mts64_ctl_create(struct snd_card *card, 
+					  struct mts64 *mts) 
+{
+	int err;
+
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_switch, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_switch\n");
+		return err;
+	}
+
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_time_hours, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_time_hours\n");
+		return err;
+	}
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_time_minutes, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_time_minutes\n");
+		return err;
+	}
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_time_seconds, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_time_seconds\n");
+		return err;
+	}
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_time_frames, mts));
+	if (err < 0) {

drivers/mts64.c line 710

Quoted 10 lines
drivers/mts64.c new file +10 -0
+		snd_printk("Cannot create control: smpte_time_frames\n");
+		return err;
+	}
+
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_fps, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_fps\n");
+		return err;
+	}

I myself prefer initializations using a loop and an array. It'd be more simpler.

drivers/mts64.c line 782

Quoted 12 lines
drivers/mts64.c new file +12 -0
+static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
+					    int up)
+{
+	struct mts64 *mts = substream->rmidi->private_data;
+	unsigned long flags;
+
+	spin_lock_irqsave(mts->lock, flags);	
+	if (up) {
+		mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
+	} else {
+		mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
+	}

Remove braces here.

Quoted 4 lines
drivers/mts64.c new file +4 -0
+static int __devinit snd_mts64_rawmidi_create(struct snd_card *card, 
+					      struct mts64 *chip, 
+					      struct snd_rawmidi **rrmidi)
+{

(snip)

drivers/mts64.c line 853

Quoted 2 lines
drivers/mts64.c new file +2 -0
+	/* controls */
+	snd_mts64_ctl_create(card, chip);

Should return an error (and free the created rmidi).

Quoted 9 lines
drivers/mts64.c new file +9 -0
+static void mts64_interrupt(int irq, void *private, struct pt_regs *r)
+{
+	struct mts64 *mts = ((struct snd_card*)private)->private_data;
+	unsigned short ret;
+	unsigned char status, data;
+	struct snd_rawmidi_substream *substream;
+
+	spin_lock(mts->lock);
+	ret = mts64_read(mts->pardev->port);

drivers/mts64.c line 880

Quoted 10 lines
drivers/mts64.c new file +10 -0
+	data = ret & 0x00ff;
+	status = ret >> 8;
+
+	if (status & MTS64_STAT_PORT) {
+		mts->current_midi_input_port = mts64_map_midi_input(data);
+	} else {
+		substream = mts->midi_input_substream[mts->current_midi_input_port];
+		if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
+			snd_rawmidi_receive(substream, &data, 1);
+	}

You should check the value of mts->current_midi_input_port. It could be -1 until status is updated, and can be out of the array range.

Quoted 15 lines
drivers/mts64.c new file +15 -0
+static int __devinit mts64_probe_port(struct parport *p)
+{
+	struct pardevice *pardev;
+	int res;
+
+	pardev = parport_register_device(p, DRIVER_NAME,
+					 NULL, NULL, NULL,
+					 0, NULL);
+	if (!pardev)
+		goto __err;
+	
+	if (parport_claim(pardev))
+		goto __err1;
+
+	res = mts64_probe(p);

drivers/mts64.c line 908

Quoted 10 lines
drivers/mts64.c new file +10 -0
+
+	parport_release(pardev);
+	parport_unregister_device(pardev);
+
+	return res;
+	
+__err1:
+	parport_unregister_device(pardev);
+__err:
+	return 0;

No error?

Quoted 14 lines
drivers/mts64.c new file +14 -0
+static int __devinit snd_mts64_probe(struct platform_device *pdev)
+{
+	struct pardevice *pardev;
+	struct parport *p;
+	int dev = pdev->id;
+	struct snd_card *card = NULL;
+	struct mts64 *mts = NULL;
+	struct snd_rawmidi *rmidi = NULL;
+	int err;
+	unsigned long flags;
+
+	p = platform_get_drvdata(pdev);
+	platform_set_drvdata(pdev, NULL);
+

drivers/mts64.c line 976

Quoted 10 lines
drivers/mts64.c new file +10 -0
+	if (dev >= SNDRV_CARDS) {
+		return -ENODEV;
+	}
+	if (!enable[dev]) {
+		return -ENOENT;
+	}
+
+	if (!mts64_probe_port(p)) {
+		return -ENODEV;
+	}

Remove braces around the above three if's.

drivers/mts64.c line 1026

Quoted 4 lines
drivers/mts64.c new file +4 -0
+	/* init device */
+	local_irq_save(flags);
+	mts64_device_init(p);
+	local_irq_restore(flags);

Remove irq_save/restore. mts64_device_init() sleeps.

Quoted 7 lines
drivers/mts64.c new file +7 -0
+
+	platform_set_drvdata(pdev, card);
+
+	/* At this point card will be usable */
+	if ((err = snd_card_register(card)) < 0) {
+		snd_printk("Cannot register card\n");
+		goto __err_release;

drivers/mts64.c line 1043

Quoted 10 lines
drivers/mts64.c new file +10 -0
+	}
+
+	snd_printk("ESI Miditerminal 4140 on 0x%lx\n", p->base);
+	return 0;
+
+__err_release:
+	parport_release(pardev);
+	mts->pardev_claimed = 0;
+__err_free_device:
+	parport_unregister_device(pardev);

These can be card->private_free callback so that a single snd_card_free() call does all without messy labels.

Quoted 12 lines
drivers/mts64.c new file +12 -0
+__err_free_card:
+	snd_card_free(card);
+__err:
+	return err;
+
+}
+
+static int snd_mts64_remove(struct platform_device *pdev)
+{
+	struct snd_card *card = platform_get_drvdata(pdev);
+	struct mts64 *mts = card->private_data;
+	struct pardevice *pardev = mts->pardev;

drivers/mts64.c line 1065

Quoted 10 lines
drivers/mts64.c new file +10 -0
+
+	if (card) {
+		snd_card_free(card);
+		snd_mts64_free(mts);
+	}
+	if (pardev) {
+		if (mts->pardev_claimed)
+			parport_release(pardev);
+		parport_unregister_device(pardev);
+	}

snd_mts64_free() and release of pardev stuff should be in card->private_free callback. It will make the code simpler.

Thanks,

Takashi

-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#2 lore raw
Quoted 5 lines · Takashi Iwai

At Tue, 25 Apr 2006 17:37:18 +0200,

Quoted 1 line · Matthias Koenig

+static int mts64_device_init(struct parport *p)

You can add __devinit since this is called only from probe function.

Ok

Quoted 11 lines

drivers/mts64.c line 223

Quoted 8 lines
drivers/mts64.c new file +7 -0
+{
+	int i;
+	unsigned long flags;
+
+	local_irq_save(flags);	
+	mts64_write_command(p, MTS64_CMD_RESET);
+	local_irq_restore(flags);

Do we need irq_save/restore here?
I see no possible races around this function.

Hmm, probably you're right.
I'm wondering if we need disabling of interrupts (incl. kernel preemption) at all at these places.

Quoted 13 lines

drivers/mts64.c line 307

Quoted 11 lines
drivers/mts64.c new file +9 -0
+static int mts64_probe(struct parport *p)
+{
+	unsigned char c;
+	unsigned long flags;
+
+	local_irq_save(flags);
+	mts64_smpte_stop(p);
+	mts64_write_command(p, MTS64_CMD_PROBE);
+	local_irq_restore(flags);

Ditto. It's also __devinit function.

Ok

Quoted 11 lines
Quoted 11 lines
drivers/mts64.c new file +9 -0
+static int snd_mts64_ctl_smpte_switch_put(struct snd_kcontrol* kctl,
+					  struct snd_ctl_elem_value *uctl)
+{
+	struct mts64 *mts = snd_kcontrol_chip(kctl);
+	int changed = 0;
+	unsigned long flags;
+
+	if (mts->smpte_switch == uctl->value.integer.value[0])
+		return changed;
Quoted 8 lines

drivers/mts64.c line 487

Quoted 5 lines
drivers/mts64.c new file +5 -0
+
+	changed = 1;
+	mts->smpte_switch = uctl->value.integer.value[0];
+	if (mts->smpte_switch) {
+		spin_lock_irqsave(mts->lock, flags);

You need no irqsave in get/put callbacks. Just use spin_lock_irq()/spin_unlock_irq().

Ok

Quoted 39 lines
Quoted 39 lines
drivers/mts64.c new file +38 -0
+static int __devinit snd_mts64_ctl_create(struct snd_card *card, 
+					  struct mts64 *mts) 
+{
+	int err;
+
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_switch, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_switch\n");
+		return err;
+	}
+
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_time_hours, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_time_hours\n");
+		return err;
+	}
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_time_minutes, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_time_minutes\n");
+		return err;
+	}
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_time_seconds, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_time_seconds\n");
+		return err;
+	}
+	err = snd_ctl_add(card, 
+			  snd_ctl_new1(&mts64_ctl_smpte_time_frames, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_time_frames\n");
+		return err;
+	}
+
+	err = snd_ctl_add(card, 
Quoted 8 lines

drivers/mts64.c line 710

Quoted 5 lines
drivers/mts64.c new file +5 -0
+			  snd_ctl_new1(&mts64_ctl_smpte_fps, mts));
+	if (err < 0) {
+		snd_printk("Cannot create control: smpte_fps\n");
+		return err;
+	}

I myself prefer initializations using a loop and an array. It'd be more simpler.

Agreed, looks nicer.

Quoted 15 lines

drivers/mts64.c line 782

Quoted 13 lines
drivers/mts64.c new file +12 -0
+static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
+					    int up)
+{
+	struct mts64 *mts = substream->rmidi->private_data;
+	unsigned long flags;
+
+	spin_lock_irqsave(mts->lock, flags);	
+	if (up) {
+		mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
+	} else {
+		mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
+	}

Remove braces here.

Ok

Quoted 10 lines
Quoted 5 lines
drivers/mts64.c new file +4 -0
+static int __devinit snd_mts64_rawmidi_create(struct snd_card *card, 
+					      struct mts64 *chip, 
+					      struct snd_rawmidi **rrmidi)
+{

(snip)

drivers/mts64.c line 853

Quoted 2 lines
drivers/mts64.c new file +2 -0
+	/* controls */
+	snd_mts64_ctl_create(card, chip);

Should return an error (and free the created rmidi).

Ok

Quoted 15 lines
Quoted 15 lines
drivers/mts64.c new file +14 -0
+static void mts64_interrupt(int irq, void *private, struct pt_regs *r)
+{
+	struct mts64 *mts = ((struct snd_card*)private)->private_data;
+	unsigned short ret;
+	unsigned char status, data;
+	struct snd_rawmidi_substream *substream;
+
+	spin_lock(mts->lock);
+	ret = mts64_read(mts->pardev->port);
+	data = ret & 0x00ff;
+	status = ret >> 8;
+
+	if (status & MTS64_STAT_PORT) {
+		mts->current_midi_input_port = mts64_map_midi_input(data);
Quoted 8 lines

drivers/mts64.c line 880

Quoted 5 lines
drivers/mts64.c new file +5 -0
+	} else {
+		substream = mts->midi_input_substream[mts->current_midi_input_port];
+		if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
+			snd_rawmidi_receive(substream, &data, 1);
+	}

You should check the value of mts->current_midi_input_port. It could be -1 until status is updated, and can be out of the array range.

Yes, I assumed that before some Midi data is sent, the device tells on which port. Right, this should be checked, one never knows. If the initial interrupt gets lost we're doomed.

Quoted 20 lines
Quoted 20 lines
drivers/mts64.c new file +19 -0
+static int __devinit mts64_probe_port(struct parport *p)
+{
+	struct pardevice *pardev;
+	int res;
+
+	pardev = parport_register_device(p, DRIVER_NAME,
+					 NULL, NULL, NULL,
+					 0, NULL);
+	if (!pardev)
+		goto __err;
+	
+	if (parport_claim(pardev))
+		goto __err1;
+
+	res = mts64_probe(p);
+
+	parport_release(pardev);
+	parport_unregister_device(pardev);
+
Quoted 8 lines

drivers/mts64.c line 908

Quoted 6 lines
drivers/mts64.c new file +6 -0
+	return res;
+	
+__err1:
+	parport_unregister_device(pardev);
+__err:
+	return 0;

No error?

0 in this context means no device is present.
Maybe I should change this to conform with the usual conventions and return 0 for success, -ENODEV for no device and -EIO for error on pardevice registering.

Quoted 19 lines
Quoted 19 lines
drivers/mts64.c new file +18 -0
+static int __devinit snd_mts64_probe(struct platform_device *pdev)
+{
+	struct pardevice *pardev;
+	struct parport *p;
+	int dev = pdev->id;
+	struct snd_card *card = NULL;
+	struct mts64 *mts = NULL;
+	struct snd_rawmidi *rmidi = NULL;
+	int err;
+	unsigned long flags;
+
+	p = platform_get_drvdata(pdev);
+	platform_set_drvdata(pdev, NULL);
+
+	if (dev >= SNDRV_CARDS) {
+		return -ENODEV;
+	}
+	if (!enable[dev]) {
Quoted 8 lines

drivers/mts64.c line 976

Quoted 6 lines
drivers/mts64.c new file +6 -0
+		return -ENOENT;
+	}
+
+	if (!mts64_probe_port(p)) {
+		return -ENODEV;
+	}

Remove braces around the above three if's.

Ok

Quoted 7 lines

drivers/mts64.c line 1026

Quoted 5 lines
drivers/mts64.c new file +4 -0
+	/* init device */
+	local_irq_save(flags);
+	mts64_device_init(p);
+	local_irq_restore(flags);

Remove irq_save/restore. mts64_device_init() sleeps.

Yes, this is evidently wrong.

Quoted 13 lines
Quoted 13 lines
drivers/mts64.c new file +12 -0
+
+	platform_set_drvdata(pdev, card);
+
+	/* At this point card will be usable */
+	if ((err = snd_card_register(card)) < 0) {
+		snd_printk("Cannot register card\n");
+		goto __err_release;
+	}
+
+	snd_printk("ESI Miditerminal 4140 on 0x%lx\n", p->base);
+	return 0;
+
Quoted 8 lines

drivers/mts64.c line 1043

Quoted 5 lines
drivers/mts64.c new file +5 -0
+__err_release:
+	parport_release(pardev);
+	mts->pardev_claimed = 0;
+__err_free_device:
+	parport_unregister_device(pardev);

These can be card->private_free callback so that a single snd_card_free() call does all without messy labels.

Ok, this would cleanup the code a lot.
Btw, I can't rember reading about private_free for cards in "Writing an ALSA Driver", this should be added.

Quoted 18 lines
Quoted 18 lines
drivers/mts64.c new file +17 -0
+__err_free_card:
+	snd_card_free(card);
+__err:
+	return err;
+
+}
+
+static int snd_mts64_remove(struct platform_device *pdev)
+{
+	struct snd_card *card = platform_get_drvdata(pdev);
+	struct mts64 *mts = card->private_data;
+	struct pardevice *pardev = mts->pardev;
+
+	if (card) {
+		snd_card_free(card);
+		snd_mts64_free(mts);
+	}
Quoted 8 lines

drivers/mts64.c line 1065

Quoted 5 lines
drivers/mts64.c new file +5 -0
+	if (pardev) {
+		if (mts->pardev_claimed)
+			parport_release(pardev);
+		parport_unregister_device(pardev);
+	}

snd_mts64_free() and release of pardev stuff should be in card->private_free callback. It will make the code simpler.

Ok

So, let's proceed next week, since the next days I'm in Karlsruhe at LAC :-)

Regards,
Matthias

-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642