2 * MOTU Midi Timepiece ALSA Main routines
3 * Copyright by Michael T. Mayers (c) Jan 09, 2000
4 * mail: michael@tweakoz.com
5 * Thanks to John Galbraith
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * This driver is for the 'Mark Of The Unicorn' (MOTU)
23 * MidiTimePiece AV multiport MIDI interface
27 * 8 MIDI Ins and 8 MIDI outs
28 * Video Sync In (BNC), Word Sync Out (BNC),
31 * 2 programmable pedal/footswitch inputs and 4 programmable MIDI controller knobs.
32 * Macintosh RS422 serial port
33 * RS422 "network" port for ganging multiple MTP's
34 * PC Parallel Port ( which this driver currently uses )
38 * Hardware MIDI routing, merging, and filtering
39 * MIDI Synchronization to Video, ADAT, SMPTE and other Clock sources
40 * 128 'scene' memories, recallable from MIDI program change
44 * Jun 11 2001 Takashi Iwai <tiwai@suse.de>
45 * - Recoded & debugged
46 * - Added timer interrupt for midi outputs
47 * - hwports is between 1 and 8, which specifies the number of hardware ports.
48 * The three global ports, computer, adat and broadcast ports, are created
49 * always after h/w and remote ports.
53 #include <sound/driver.h>
54 #include <linux/init.h>
55 #include <linux/interrupt.h>
56 #include <linux/err.h>
57 #include <linux/platform_device.h>
58 #include <linux/slab.h>
59 #include <linux/ioport.h>
60 #include <linux/moduleparam.h>
61 #include <sound/core.h>
62 #include <sound/initval.h>
63 #include <sound/rawmidi.h>
64 #include <linux/delay.h>
71 MODULE_AUTHOR("Michael T. Mayers");
72 MODULE_DESCRIPTION("MOTU MidiTimePiece AV multiport MIDI");
73 MODULE_LICENSE("GPL");
74 MODULE_SUPPORTED_DEVICE("{{MOTU,MidiTimePiece AV multiport MIDI}}");
77 #define MTPAV_IOBASE 0x378
79 #define MTPAV_MAX_PORTS 8
81 static int index = SNDRV_DEFAULT_IDX1;
82 static char *id = SNDRV_DEFAULT_STR1;
83 static long port = MTPAV_IOBASE; /* 0x378, 0x278 */
84 static int irq = MTPAV_IRQ; /* 7, 5 */
85 static int hwports = MTPAV_MAX_PORTS; /* use hardware ports 1-8 */
87 module_param(index, int, 0444);
88 MODULE_PARM_DESC(index, "Index value for MotuMTPAV MIDI.");
89 module_param(id, charp, 0444);
90 MODULE_PARM_DESC(id, "ID string for MotuMTPAV MIDI.");
91 module_param(port, long, 0444);
92 MODULE_PARM_DESC(port, "Parallel port # for MotuMTPAV MIDI.");
93 module_param(irq, int, 0444);
94 MODULE_PARM_DESC(irq, "Parallel IRQ # for MotuMTPAV MIDI.");
95 module_param(hwports, int, 0444);
96 MODULE_PARM_DESC(hwports, "Hardware ports # for MotuMTPAV MIDI.");
101 //#define USE_FAKE_MTP // don't actually read/write to MTP device (for debugging without an actual unit) (does not work yet)
103 // parallel port usage masks
104 #define SIGS_BYTE 0x08
105 #define SIGS_RFD 0x80
106 #define SIGS_IRQ 0x40
107 #define SIGS_IN0 0x10
108 #define SIGS_IN1 0x20
110 #define SIGC_WRITE 0x04
111 #define SIGC_READ 0x08
112 #define SIGC_INTEN 0x10
119 #define MTPAV_MODE_INPUT_OPENED 0x01
120 #define MTPAV_MODE_OUTPUT_OPENED 0x02
121 #define MTPAV_MODE_INPUT_TRIGGERED 0x04
122 #define MTPAV_MODE_OUTPUT_TRIGGERED 0x08
124 #define NUMPORTS (0x12+1)
135 struct snd_rawmidi_substream *input;
136 struct snd_rawmidi_substream *output;
140 struct snd_card *card;
142 struct resource *res_port;
143 int irq; /* interrupt (for inputs) */
145 int share_irq; /* number of accesses to input interrupts */
146 int istimer; /* number of accesses to timer interrupts */
147 struct timer_list timer; /* timer interrupts for outputs */
148 struct snd_rawmidi *rmidi;
149 int num_ports; /* number of hw ports (1-8) */
150 struct mtpav_port ports[NUMPORTS]; /* all ports including computer, adat and bc */
152 u32 inmidiport; /* selected input midi port */
153 u32 inmidistate; /* during midi command 0xf5 */
155 u32 outmidihwport; /* selected output midi hw port */
160 * possible hardware ports (selected by 0xf5 port message)
162 * 0x01 .. 0x08 this MTP's ports 1..8
163 * 0x09 .. 0x10 networked MTP's ports (9..16)
164 * 0x11 networked MTP's computer port
168 * subdevice 0 - (X-1) ports
169 * X - (2*X-1) networked ports
174 * where X = chip->num_ports
177 #define MTPAV_PIDX_COMPUTER 0
178 #define MTPAV_PIDX_ADAT 1
179 #define MTPAV_PIDX_BROADCAST 2
182 static int translate_subdevice_to_hwport(struct mtpav *chip, int subdev)
185 return 0x01; /* invalid - use port 0 as default */
186 else if (subdev < chip->num_ports)
187 return subdev + 1; /* single mtp port */
188 else if (subdev < chip->num_ports * 2)
189 return subdev - chip->num_ports + 0x09; /* remote port */
190 else if (subdev == chip->num_ports * 2 + MTPAV_PIDX_COMPUTER)
191 return 0x11; /* computer port */
192 else if (subdev == chip->num_ports + MTPAV_PIDX_ADAT)
193 return 0x63; /* ADAT */
194 return 0; /* all ports */
197 static int translate_hwport_to_subdevice(struct mtpav *chip, int hwport)
200 if (hwport <= 0x00) /* all ports */
201 return chip->num_ports + MTPAV_PIDX_BROADCAST;
202 else if (hwport <= 0x08) { /* single port */
204 if (p >= chip->num_ports)
207 } else if (hwport <= 0x10) { /* remote port */
208 p = hwport - 0x09 + chip->num_ports;
209 if (p >= chip->num_ports * 2)
212 } else if (hwport == 0x11) /* computer port */
213 return chip->num_ports + MTPAV_PIDX_COMPUTER;
215 return chip->num_ports + MTPAV_PIDX_ADAT;
222 static u8 snd_mtpav_getreg(struct mtpav *chip, u16 reg)
227 rval = inb(chip->port + SREG);
228 rval = (rval & 0xf8);
229 } else if (reg == CREG) {
230 rval = inb(chip->port + CREG);
231 rval = (rval & 0x1c);
240 static inline void snd_mtpav_mputreg(struct mtpav *chip, u16 reg, u8 val)
242 if (reg == DREG || reg == CREG)
243 outb(val, chip->port + reg);
249 static void snd_mtpav_wait_rfdhi(struct mtpav *chip)
254 sbyte = snd_mtpav_getreg(chip, SREG);
255 while (!(sbyte & SIGS_RFD) && counts--) {
256 sbyte = snd_mtpav_getreg(chip, SREG);
261 static void snd_mtpav_send_byte(struct mtpav *chip, u8 byte)
267 snd_mtpav_wait_rfdhi(chip);
271 tcbyt = snd_mtpav_getreg(chip, CREG);
272 clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
273 setwrite = tcbyt | SIGC_WRITE;
275 snd_mtpav_mputreg(chip, DREG, byte);
276 snd_mtpav_mputreg(chip, CREG, clrwrite); // clear write bit
278 snd_mtpav_mputreg(chip, CREG, setwrite); // set write bit
286 /* call this with spin lock held */
287 static void snd_mtpav_output_port_write(struct mtpav *mtp_card,
288 struct mtpav_port *portp,
289 struct snd_rawmidi_substream *substream)
293 // Get the outbyte first, so we can emulate running status if
295 if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
298 // send port change command if necessary
300 if (portp->hwport != mtp_card->outmidihwport) {
301 mtp_card->outmidihwport = portp->hwport;
303 snd_mtpav_send_byte(mtp_card, 0xf5);
304 snd_mtpav_send_byte(mtp_card, portp->hwport);
305 //snd_printk("new outport: 0x%x\n", (unsigned int) portp->hwport);
307 if (!(outbyte & 0x80) && portp->running_status)
308 snd_mtpav_send_byte(mtp_card, portp->running_status);
315 portp->running_status = outbyte;
317 snd_mtpav_send_byte(mtp_card, outbyte);
318 } while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
321 static void snd_mtpav_output_write(struct snd_rawmidi_substream *substream)
323 struct mtpav *mtp_card = substream->rmidi->private_data;
324 struct mtpav_port *portp = &mtp_card->ports[substream->number];
327 spin_lock_irqsave(&mtp_card->spinlock, flags);
328 snd_mtpav_output_port_write(mtp_card, portp, substream);
329 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
337 static void snd_mtpav_portscan(struct mtpav *chip) // put mtp into smart routing mode
341 for (p = 0; p < 8; p++) {
342 snd_mtpav_send_byte(chip, 0xf5);
343 snd_mtpav_send_byte(chip, p);
344 snd_mtpav_send_byte(chip, 0xfe);
351 static int snd_mtpav_input_open(struct snd_rawmidi_substream *substream)
353 struct mtpav *mtp_card = substream->rmidi->private_data;
354 struct mtpav_port *portp = &mtp_card->ports[substream->number];
357 spin_lock_irqsave(&mtp_card->spinlock, flags);
358 portp->mode |= MTPAV_MODE_INPUT_OPENED;
359 portp->input = substream;
360 if (mtp_card->share_irq++ == 0)
361 snd_mtpav_mputreg(mtp_card, CREG, (SIGC_INTEN | SIGC_WRITE)); // enable pport interrupts
362 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
369 static int snd_mtpav_input_close(struct snd_rawmidi_substream *substream)
371 struct mtpav *mtp_card = substream->rmidi->private_data;
372 struct mtpav_port *portp = &mtp_card->ports[substream->number];
375 spin_lock_irqsave(&mtp_card->spinlock, flags);
376 portp->mode &= ~MTPAV_MODE_INPUT_OPENED;
378 if (--mtp_card->share_irq == 0)
379 snd_mtpav_mputreg(mtp_card, CREG, 0); // disable pport interrupts
380 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
387 static void snd_mtpav_input_trigger(struct snd_rawmidi_substream *substream, int up)
389 struct mtpav *mtp_card = substream->rmidi->private_data;
390 struct mtpav_port *portp = &mtp_card->ports[substream->number];
393 spin_lock_irqsave(&mtp_card->spinlock, flags);
395 portp->mode |= MTPAV_MODE_INPUT_TRIGGERED;
397 portp->mode &= ~MTPAV_MODE_INPUT_TRIGGERED;
398 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
404 * timer interrupt for outputs
407 static void snd_mtpav_output_timer(unsigned long data)
410 struct mtpav *chip = (struct mtpav *)data;
413 spin_lock_irqsave(&chip->spinlock, flags);
414 /* reprogram timer */
415 chip->timer.expires = 1 + jiffies;
416 add_timer(&chip->timer);
417 /* process each port */
418 for (p = 0; p <= chip->num_ports * 2 + MTPAV_PIDX_BROADCAST; p++) {
419 struct mtpav_port *portp = &chip->ports[p];
420 if ((portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED) && portp->output)
421 snd_mtpav_output_port_write(chip, portp, portp->output);
423 spin_unlock_irqrestore(&chip->spinlock, flags);
427 static void snd_mtpav_add_output_timer(struct mtpav *chip)
429 chip->timer.expires = 1 + jiffies;
430 add_timer(&chip->timer);
434 static void snd_mtpav_remove_output_timer(struct mtpav *chip)
436 del_timer(&chip->timer);
442 static int snd_mtpav_output_open(struct snd_rawmidi_substream *substream)
444 struct mtpav *mtp_card = substream->rmidi->private_data;
445 struct mtpav_port *portp = &mtp_card->ports[substream->number];
448 spin_lock_irqsave(&mtp_card->spinlock, flags);
449 portp->mode |= MTPAV_MODE_OUTPUT_OPENED;
450 portp->output = substream;
451 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
458 static int snd_mtpav_output_close(struct snd_rawmidi_substream *substream)
460 struct mtpav *mtp_card = substream->rmidi->private_data;
461 struct mtpav_port *portp = &mtp_card->ports[substream->number];
464 spin_lock_irqsave(&mtp_card->spinlock, flags);
465 portp->mode &= ~MTPAV_MODE_OUTPUT_OPENED;
466 portp->output = NULL;
467 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
474 static void snd_mtpav_output_trigger(struct snd_rawmidi_substream *substream, int up)
476 struct mtpav *mtp_card = substream->rmidi->private_data;
477 struct mtpav_port *portp = &mtp_card->ports[substream->number];
480 spin_lock_irqsave(&mtp_card->spinlock, flags);
482 if (! (portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED)) {
483 if (mtp_card->istimer++ == 0)
484 snd_mtpav_add_output_timer(mtp_card);
485 portp->mode |= MTPAV_MODE_OUTPUT_TRIGGERED;
488 portp->mode &= ~MTPAV_MODE_OUTPUT_TRIGGERED;
489 if (--mtp_card->istimer == 0)
490 snd_mtpav_remove_output_timer(mtp_card);
492 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
495 snd_mtpav_output_write(substream);
499 * midi interrupt for inputs
502 static void snd_mtpav_inmidi_process(struct mtpav *mcrd, u8 inbyte)
504 struct mtpav_port *portp;
506 if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
509 portp = &mcrd->ports[mcrd->inmidiport];
510 if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED)
511 snd_rawmidi_receive(portp->input, &inbyte, 1);
514 static void snd_mtpav_inmidi_h(struct mtpav *mcrd, u8 inbyte)
516 if (inbyte >= 0xf8) {
517 /* real-time midi code */
518 snd_mtpav_inmidi_process(mcrd, inbyte);
522 if (mcrd->inmidistate == 0) { // awaiting command
523 if (inbyte == 0xf5) // MTP port #
524 mcrd->inmidistate = 1;
526 snd_mtpav_inmidi_process(mcrd, inbyte);
527 } else if (mcrd->inmidistate) {
528 mcrd->inmidiport = translate_hwport_to_subdevice(mcrd, inbyte);
529 mcrd->inmidistate = 0;
533 static void snd_mtpav_read_bytes(struct mtpav *mcrd)
540 u8 sbyt = snd_mtpav_getreg(mcrd, SREG);
542 //printk("snd_mtpav_read_bytes() sbyt: 0x%x\n", sbyt);
544 if (!(sbyt & SIGS_BYTE))
547 cbyt = snd_mtpav_getreg(mcrd, CREG);
548 clrread = cbyt & (SIGC_READ ^ 0xff);
549 setread = cbyt | SIGC_READ;
554 for (i = 0; i < 4; i++) {
555 snd_mtpav_mputreg(mcrd, CREG, setread);
556 sr = snd_mtpav_getreg(mcrd, SREG);
557 snd_mtpav_mputreg(mcrd, CREG, clrread);
559 sr &= SIGS_IN0 | SIGS_IN1;
561 mtp_read_byte |= sr << (i * 2);
564 snd_mtpav_inmidi_h(mcrd, mtp_read_byte);
566 sbyt = snd_mtpav_getreg(mcrd, SREG);
568 } while (sbyt & SIGS_BYTE);
571 static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id, struct pt_regs *regs)
573 struct mtpav *mcard = dev_id;
575 spin_lock(&mcard->spinlock);
576 snd_mtpav_read_bytes(mcard);
577 spin_unlock(&mcard->spinlock);
584 static int __init snd_mtpav_get_ISA(struct mtpav * mcard)
586 if ((mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI")) == NULL) {
587 snd_printk("MTVAP port 0x%lx is busy\n", port);
591 if (request_irq(irq, snd_mtpav_irqh, SA_INTERRUPT, "MOTU MTPAV", mcard)) {
592 snd_printk("MTVAP IRQ %d busy\n", irq);
603 static struct snd_rawmidi_ops snd_mtpav_output = {
604 .open = snd_mtpav_output_open,
605 .close = snd_mtpav_output_close,
606 .trigger = snd_mtpav_output_trigger,
609 static struct snd_rawmidi_ops snd_mtpav_input = {
610 .open = snd_mtpav_input_open,
611 .close = snd_mtpav_input_close,
612 .trigger = snd_mtpav_input_trigger,
617 * get RAWMIDI resources
620 static void __init snd_mtpav_set_name(struct mtpav *chip,
621 struct snd_rawmidi_substream *substream)
623 if (substream->number >= 0 && substream->number < chip->num_ports)
624 sprintf(substream->name, "MTP direct %d", (substream->number % chip->num_ports) + 1);
625 else if (substream->number >= 8 && substream->number < chip->num_ports * 2)
626 sprintf(substream->name, "MTP remote %d", (substream->number % chip->num_ports) + 1);
627 else if (substream->number == chip->num_ports * 2)
628 strcpy(substream->name, "MTP computer");
629 else if (substream->number == chip->num_ports * 2 + 1)
630 strcpy(substream->name, "MTP ADAT");
632 strcpy(substream->name, "MTP broadcast");
635 static int __init snd_mtpav_get_RAWMIDI(struct mtpav *mcard)
638 struct snd_rawmidi *rawmidi;
639 struct snd_rawmidi_substream *substream;
640 struct list_head *list;
644 else if (hwports > 8)
646 mcard->num_ports = hwports;
648 if ((rval = snd_rawmidi_new(mcard->card, "MotuMIDI", 0,
649 mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
650 mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
653 rawmidi = mcard->rmidi;
654 rawmidi->private_data = mcard;
656 list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
657 substream = list_entry(list, struct snd_rawmidi_substream, list);
658 snd_mtpav_set_name(mcard, substream);
659 substream->ops = &snd_mtpav_input;
661 list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
662 substream = list_entry(list, struct snd_rawmidi_substream, list);
663 snd_mtpav_set_name(mcard, substream);
664 substream->ops = &snd_mtpav_output;
665 mcard->ports[substream->number].hwport = translate_subdevice_to_hwport(mcard, substream->number);
667 rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
668 SNDRV_RAWMIDI_INFO_DUPLEX;
669 sprintf(rawmidi->name, "MTP AV MIDI");
676 static void snd_mtpav_free(struct snd_card *card)
678 struct mtpav *crd = card->private_data;
681 spin_lock_irqsave(&crd->spinlock, flags);
682 if (crd->istimer > 0)
683 snd_mtpav_remove_output_timer(crd);
684 spin_unlock_irqrestore(&crd->spinlock, flags);
686 free_irq(crd->irq, (void *)crd);
687 release_and_free_resource(crd->res_port);
692 static int __init snd_mtpav_probe(struct platform_device *dev)
694 struct snd_card *card;
696 struct mtpav *mtp_card;
698 card = snd_card_new(index, id, THIS_MODULE, sizeof(*mtp_card));
702 mtp_card = card->private_data;
703 spin_lock_init(&mtp_card->spinlock);
704 init_timer(&mtp_card->timer);
705 mtp_card->card = card;
707 mtp_card->share_irq = 0;
708 mtp_card->inmidiport = 0xffffffff;
709 mtp_card->inmidistate = 0;
710 mtp_card->outmidihwport = 0xffffffff;
711 init_timer(&mtp_card->timer);
712 mtp_card->timer.function = snd_mtpav_output_timer;
713 mtp_card->timer.data = (unsigned long) mtp_card;
715 card->private_free = snd_mtpav_free;
717 err = snd_mtpav_get_ISA(mtp_card);
721 strcpy(card->driver, "MTPAV");
722 strcpy(card->shortname, "MTPAV on parallel port");
723 snprintf(card->longname, sizeof(card->longname),
724 "MTPAV on parallel port at 0x%lx", port);
726 err = snd_mtpav_get_RAWMIDI(mtp_card);
730 snd_mtpav_portscan(mtp_card);
732 snd_card_set_dev(card, &dev->dev);
733 err = snd_card_register(mtp_card->card);
737 platform_set_drvdata(dev, card);
738 printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
746 static int snd_mtpav_remove(struct platform_device *devptr)
748 snd_card_free(platform_get_drvdata(devptr));
749 platform_set_drvdata(devptr, NULL);
753 #define SND_MTPAV_DRIVER "snd_mtpav"
755 static struct platform_driver snd_mtpav_driver = {
756 .probe = snd_mtpav_probe,
757 .remove = snd_mtpav_remove,
759 .name = SND_MTPAV_DRIVER
763 static int __init alsa_card_mtpav_init(void)
766 struct platform_device *device;
768 if ((err = platform_driver_register(&snd_mtpav_driver)) < 0)
771 device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
772 if (IS_ERR(device)) {
773 platform_driver_unregister(&snd_mtpav_driver);
774 return PTR_ERR(device);
779 static void __exit alsa_card_mtpav_exit(void)
781 platform_driver_unregister(&snd_mtpav_driver);
784 module_init(alsa_card_mtpav_init)
785 module_exit(alsa_card_mtpav_exit)