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/slab.h>
57 #include <linux/ioport.h>
58 #include <linux/moduleparam.h>
59 #include <sound/core.h>
60 #include <sound/initval.h>
61 #include <sound/rawmidi.h>
62 #include <linux/delay.h>
69 MODULE_AUTHOR("Michael T. Mayers");
70 MODULE_DESCRIPTION("MOTU MidiTimePiece AV multiport MIDI");
71 MODULE_LICENSE("GPL");
72 MODULE_SUPPORTED_DEVICE("{{MOTU,MidiTimePiece AV multiport MIDI}}");
75 #define MTPAV_IOBASE 0x378
77 #define MTPAV_MAX_PORTS 8
79 static int index = SNDRV_DEFAULT_IDX1;
80 static char *id = SNDRV_DEFAULT_STR1;
81 static long port = MTPAV_IOBASE; /* 0x378, 0x278 */
82 static int irq = MTPAV_IRQ; /* 7, 5 */
83 static int hwports = MTPAV_MAX_PORTS; /* use hardware ports 1-8 */
85 module_param(index, int, 0444);
86 MODULE_PARM_DESC(index, "Index value for MotuMTPAV MIDI.");
87 module_param(id, charp, 0444);
88 MODULE_PARM_DESC(id, "ID string for MotuMTPAV MIDI.");
89 module_param(port, long, 0444);
90 MODULE_PARM_DESC(port, "Parallel port # for MotuMTPAV MIDI.");
91 module_param(irq, int, 0444);
92 MODULE_PARM_DESC(irq, "Parallel IRQ # for MotuMTPAV MIDI.");
93 module_param(hwports, int, 0444);
94 MODULE_PARM_DESC(hwports, "Hardware ports # for MotuMTPAV MIDI.");
99 //#define USE_FAKE_MTP // don't actually read/write to MTP device (for debugging without an actual unit) (does not work yet)
101 // parallel port usage masks
102 #define SIGS_BYTE 0x08
103 #define SIGS_RFD 0x80
104 #define SIGS_IRQ 0x40
105 #define SIGS_IN0 0x10
106 #define SIGS_IN1 0x20
108 #define SIGC_WRITE 0x04
109 #define SIGC_READ 0x08
110 #define SIGC_INTEN 0x10
117 #define MTPAV_MODE_INPUT_OPENED 0x01
118 #define MTPAV_MODE_OUTPUT_OPENED 0x02
119 #define MTPAV_MODE_INPUT_TRIGGERED 0x04
120 #define MTPAV_MODE_OUTPUT_TRIGGERED 0x08
122 #define NUMPORTS (0x12+1)
128 typedef struct mtpav_port {
133 snd_rawmidi_substream_t *input;
134 snd_rawmidi_substream_t *output;
137 typedef struct mtpav {
140 struct resource *res_port;
141 int irq; /* interrupt (for inputs) */
143 int share_irq; /* number of accesses to input interrupts */
144 int istimer; /* number of accesses to timer interrupts */
145 struct timer_list timer; /* timer interrupts for outputs */
146 snd_rawmidi_t *rmidi;
147 int num_ports; /* number of hw ports (1-8) */
148 mtpav_port_t ports[NUMPORTS]; /* all ports including computer, adat and bc */
150 u32 inmidiport; /* selected input midi port */
151 u32 inmidistate; /* during midi command 0xf5 */
153 u32 outmidihwport; /* selected output midi hw port */
159 * hey, we handle at most only one card..
161 static mtpav_t *mtp_card;
164 * possible hardware ports (selected by 0xf5 port message)
166 * 0x01 .. 0x08 this MTP's ports 1..8
167 * 0x09 .. 0x10 networked MTP's ports (9..16)
168 * 0x11 networked MTP's computer port
172 * subdevice 0 - (X-1) ports
173 * X - (2*X-1) networked ports
178 * where X = chip->num_ports
181 #define MTPAV_PIDX_COMPUTER 0
182 #define MTPAV_PIDX_ADAT 1
183 #define MTPAV_PIDX_BROADCAST 2
186 static int translate_subdevice_to_hwport(mtpav_t *chip, int subdev)
189 return 0x01; /* invalid - use port 0 as default */
190 else if (subdev < chip->num_ports)
191 return subdev + 1; /* single mtp port */
192 else if (subdev < chip->num_ports * 2)
193 return subdev - chip->num_ports + 0x09; /* remote port */
194 else if (subdev == chip->num_ports * 2 + MTPAV_PIDX_COMPUTER)
195 return 0x11; /* computer port */
196 else if (subdev == chip->num_ports + MTPAV_PIDX_ADAT)
197 return 0x63; /* ADAT */
198 return 0; /* all ports */
201 static int translate_hwport_to_subdevice(mtpav_t *chip, int hwport)
204 if (hwport <= 0x00) /* all ports */
205 return chip->num_ports + MTPAV_PIDX_BROADCAST;
206 else if (hwport <= 0x08) { /* single port */
208 if (p >= chip->num_ports)
211 } else if (hwport <= 0x10) { /* remote port */
212 p = hwport - 0x09 + chip->num_ports;
213 if (p >= chip->num_ports * 2)
216 } else if (hwport == 0x11) /* computer port */
217 return chip->num_ports + MTPAV_PIDX_COMPUTER;
219 return chip->num_ports + MTPAV_PIDX_ADAT;
226 static u8 snd_mtpav_getreg(mtpav_t *chip, u16 reg)
231 rval = inb(chip->port + SREG);
232 rval = (rval & 0xf8);
233 } else if (reg == CREG) {
234 rval = inb(chip->port + CREG);
235 rval = (rval & 0x1c);
244 static void snd_mtpav_mputreg(mtpav_t *chip, u16 reg, u8 val)
247 outb(val, chip->port + DREG);
248 } else if (reg == CREG) {
249 outb(val, chip->port + CREG);
256 static void snd_mtpav_wait_rfdhi(mtpav_t *chip)
261 sbyte = snd_mtpav_getreg(chip, SREG);
262 while (!(sbyte & SIGS_RFD) && counts--) {
263 sbyte = snd_mtpav_getreg(chip, SREG);
268 static void snd_mtpav_send_byte(mtpav_t *chip, u8 byte)
274 snd_mtpav_wait_rfdhi(chip);
278 tcbyt = snd_mtpav_getreg(chip, CREG);
279 clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
280 setwrite = tcbyt | SIGC_WRITE;
282 snd_mtpav_mputreg(chip, DREG, byte);
283 snd_mtpav_mputreg(chip, CREG, clrwrite); // clear write bit
285 snd_mtpav_mputreg(chip, CREG, setwrite); // set write bit
293 /* call this with spin lock held */
294 static void snd_mtpav_output_port_write(mtpav_port_t *port,
295 snd_rawmidi_substream_t *substream)
299 // Get the outbyte first, so we can emulate running status if
301 if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
304 // send port change command if necessary
306 if (port->hwport != mtp_card->outmidihwport) {
307 mtp_card->outmidihwport = port->hwport;
309 snd_mtpav_send_byte(mtp_card, 0xf5);
310 snd_mtpav_send_byte(mtp_card, port->hwport);
311 //snd_printk("new outport: 0x%x\n", (unsigned int) port->hwport);
313 if (!(outbyte & 0x80) && port->running_status)
314 snd_mtpav_send_byte(mtp_card, port->running_status);
321 port->running_status = outbyte;
323 snd_mtpav_send_byte(mtp_card, outbyte);
324 } while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
327 static void snd_mtpav_output_write(snd_rawmidi_substream_t * substream)
329 mtpav_port_t *port = &mtp_card->ports[substream->number];
332 spin_lock_irqsave(&mtp_card->spinlock, flags);
333 snd_mtpav_output_port_write(port, substream);
334 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
342 static void snd_mtpav_portscan(mtpav_t *chip) // put mtp into smart routing mode
346 for (p = 0; p < 8; p++) {
347 snd_mtpav_send_byte(chip, 0xf5);
348 snd_mtpav_send_byte(chip, p);
349 snd_mtpav_send_byte(chip, 0xfe);
356 static int snd_mtpav_input_open(snd_rawmidi_substream_t * substream)
359 mtpav_port_t *portp = &mtp_card->ports[substream->number];
361 //printk("mtpav port: %d opened\n", (int) substream->number);
362 spin_lock_irqsave(&mtp_card->spinlock, flags);
363 portp->mode |= MTPAV_MODE_INPUT_OPENED;
364 portp->input = substream;
365 if (mtp_card->share_irq++ == 0)
366 snd_mtpav_mputreg(mtp_card, CREG, (SIGC_INTEN | SIGC_WRITE)); // enable pport interrupts
367 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
374 static int snd_mtpav_input_close(snd_rawmidi_substream_t *substream)
377 mtpav_port_t *portp = &mtp_card->ports[substream->number];
379 //printk("mtpav port: %d closed\n", (int) portp);
381 spin_lock_irqsave(&mtp_card->spinlock, flags);
383 portp->mode &= (~MTPAV_MODE_INPUT_OPENED);
385 if (--mtp_card->share_irq == 0)
386 snd_mtpav_mputreg(mtp_card, CREG, 0); // disable pport interrupts
388 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
395 static void snd_mtpav_input_trigger(snd_rawmidi_substream_t * substream, int up)
398 mtpav_port_t *portp = &mtp_card->ports[substream->number];
400 spin_lock_irqsave(&mtp_card->spinlock, flags);
402 portp->mode |= MTPAV_MODE_INPUT_TRIGGERED;
404 portp->mode &= ~MTPAV_MODE_INPUT_TRIGGERED;
405 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
411 * timer interrupt for outputs
414 static void snd_mtpav_output_timer(unsigned long data)
417 mtpav_t *chip = (mtpav_t *)data;
420 spin_lock_irqsave(&chip->spinlock, flags);
421 /* reprogram timer */
422 chip->timer.expires = 1 + jiffies;
423 add_timer(&chip->timer);
424 /* process each port */
425 for (p = 0; p <= chip->num_ports * 2 + MTPAV_PIDX_BROADCAST; p++) {
426 mtpav_port_t *portp = &mtp_card->ports[p];
427 if ((portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED) && portp->output)
428 snd_mtpav_output_port_write(portp, portp->output);
430 spin_unlock_irqrestore(&chip->spinlock, flags);
434 static void snd_mtpav_add_output_timer(mtpav_t *chip)
436 init_timer(&chip->timer);
437 chip->timer.function = snd_mtpav_output_timer;
438 chip->timer.data = (unsigned long) mtp_card;
439 chip->timer.expires = 1 + jiffies;
440 add_timer(&chip->timer);
444 static void snd_mtpav_remove_output_timer(mtpav_t *chip)
446 del_timer(&chip->timer);
452 static int snd_mtpav_output_open(snd_rawmidi_substream_t * substream)
455 mtpav_port_t *portp = &mtp_card->ports[substream->number];
457 spin_lock_irqsave(&mtp_card->spinlock, flags);
458 portp->mode |= MTPAV_MODE_OUTPUT_OPENED;
459 portp->output = substream;
460 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
467 static int snd_mtpav_output_close(snd_rawmidi_substream_t * substream)
470 mtpav_port_t *portp = &mtp_card->ports[substream->number];
472 spin_lock_irqsave(&mtp_card->spinlock, flags);
473 portp->mode &= (~MTPAV_MODE_OUTPUT_OPENED);
474 portp->output = NULL;
475 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
482 static void snd_mtpav_output_trigger(snd_rawmidi_substream_t * substream, int up)
485 mtpav_port_t *portp = &mtp_card->ports[substream->number];
487 spin_lock_irqsave(&mtp_card->spinlock, flags);
489 if (! (portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED)) {
490 if (mtp_card->istimer++ == 0)
491 snd_mtpav_add_output_timer(mtp_card);
492 portp->mode |= MTPAV_MODE_OUTPUT_TRIGGERED;
495 portp->mode &= ~MTPAV_MODE_OUTPUT_TRIGGERED;
496 if (--mtp_card->istimer == 0)
497 snd_mtpav_remove_output_timer(mtp_card);
499 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
502 snd_mtpav_output_write(substream);
506 * midi interrupt for inputs
509 static void snd_mtpav_inmidi_process(mtpav_t *mcrd, u8 inbyte)
513 if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
516 portp = &mcrd->ports[mcrd->inmidiport];
517 if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED) {
518 snd_rawmidi_receive(portp->input, &inbyte, 1);
522 static void snd_mtpav_inmidi_h(mtpav_t * mcrd, u8 inbyte)
524 snd_assert(mcrd, return);
526 if (inbyte >= 0xf8) {
527 /* real-time midi code */
528 snd_mtpav_inmidi_process(mcrd, inbyte);
532 if (mcrd->inmidistate == 0) { // awaiting command
533 if (inbyte == 0xf5) // MTP port #
534 mcrd->inmidistate = 1;
536 snd_mtpav_inmidi_process(mcrd, inbyte);
537 } else if (mcrd->inmidistate) {
538 mcrd->inmidiport = translate_hwport_to_subdevice(mcrd, inbyte);
539 mcrd->inmidistate = 0;
543 static void snd_mtpav_read_bytes(mtpav_t * mcrd)
550 u8 sbyt = snd_mtpav_getreg(mcrd, SREG);
552 //printk("snd_mtpav_read_bytes() sbyt: 0x%x\n", sbyt);
554 if (!(sbyt & SIGS_BYTE))
557 cbyt = snd_mtpav_getreg(mcrd, CREG);
558 clrread = cbyt & (SIGC_READ ^ 0xff);
559 setread = cbyt | SIGC_READ;
564 for (i = 0; i < 4; i++) {
565 snd_mtpav_mputreg(mcrd, CREG, setread);
566 sr = snd_mtpav_getreg(mcrd, SREG);
567 snd_mtpav_mputreg(mcrd, CREG, clrread);
569 sr &= SIGS_IN0 | SIGS_IN1;
571 mtp_read_byte |= sr << (i * 2);
574 snd_mtpav_inmidi_h(mcrd, mtp_read_byte);
576 sbyt = snd_mtpav_getreg(mcrd, SREG);
578 } while (sbyt & SIGS_BYTE);
581 static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id, struct pt_regs *regs)
583 mtpav_t *mcard = dev_id;
585 //printk("irqh()\n");
586 spin_lock(&mcard->spinlock);
587 snd_mtpav_read_bytes(mcard);
588 spin_unlock(&mcard->spinlock);
595 static int snd_mtpav_get_ISA(mtpav_t * mcard)
597 if ((mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI")) == NULL) {
598 snd_printk("MTVAP port 0x%lx is busy\n", port);
602 if (request_irq(irq, snd_mtpav_irqh, SA_INTERRUPT, "MOTU MTPAV", (void *)mcard)) {
603 snd_printk("MTVAP IRQ %d busy\n", irq);
614 static snd_rawmidi_ops_t snd_mtpav_output = {
615 .open = snd_mtpav_output_open,
616 .close = snd_mtpav_output_close,
617 .trigger = snd_mtpav_output_trigger,
620 static snd_rawmidi_ops_t snd_mtpav_input = {
621 .open = snd_mtpav_input_open,
622 .close = snd_mtpav_input_close,
623 .trigger = snd_mtpav_input_trigger,
628 * get RAWMIDI resources
631 static void snd_mtpav_set_name(mtpav_t *chip, snd_rawmidi_substream_t *substream)
633 if (substream->number >= 0 && substream->number < chip->num_ports)
634 sprintf(substream->name, "MTP direct %d", (substream->number % chip->num_ports) + 1);
635 else if (substream->number >= 8 && substream->number < chip->num_ports * 2)
636 sprintf(substream->name, "MTP remote %d", (substream->number % chip->num_ports) + 1);
637 else if (substream->number == chip->num_ports * 2)
638 strcpy(substream->name, "MTP computer");
639 else if (substream->number == chip->num_ports * 2 + 1)
640 strcpy(substream->name, "MTP ADAT");
642 strcpy(substream->name, "MTP broadcast");
645 static int snd_mtpav_get_RAWMIDI(mtpav_t * mcard)
648 snd_rawmidi_t *rawmidi;
649 snd_rawmidi_substream_t *substream;
650 struct list_head *list;
652 //printk("entering snd_mtpav_get_RAWMIDI\n");
655 mcard->num_ports = 1;
656 else if (hwports > 8)
657 mcard->num_ports = 8;
659 mcard->num_ports = hwports;
661 if ((rval = snd_rawmidi_new(mcard->card, "MotuMIDI", 0,
662 mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
663 mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
666 rawmidi = mcard->rmidi;
668 list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
669 substream = list_entry(list, snd_rawmidi_substream_t, list);
670 snd_mtpav_set_name(mcard, substream);
671 substream->ops = &snd_mtpav_input;
673 list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
674 substream = list_entry(list, snd_rawmidi_substream_t, list);
675 snd_mtpav_set_name(mcard, substream);
676 substream->ops = &snd_mtpav_output;
677 mcard->ports[substream->number].hwport = translate_subdevice_to_hwport(mcard, substream->number);
679 rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
680 SNDRV_RAWMIDI_INFO_DUPLEX;
681 sprintf(rawmidi->name, "MTP AV MIDI");
682 //printk("exiting snd_mtpav_get_RAWMIDI() \n");
689 static mtpav_t *new_mtpav(void)
691 mtpav_t *ncrd = kzalloc(sizeof(*ncrd), GFP_KERNEL);
693 spin_lock_init(&ncrd->spinlock);
695 init_timer(&ncrd->timer);
700 ncrd->inmidiport = 0xffffffff;
701 ncrd->inmidistate = 0;
702 ncrd->outmidihwport = 0xffffffff;
710 static void free_mtpav(mtpav_t * crd)
714 spin_lock_irqsave(&crd->spinlock, flags);
715 if (crd->istimer > 0)
716 snd_mtpav_remove_output_timer(crd);
717 spin_unlock_irqrestore(&crd->spinlock, flags);
719 free_irq(crd->irq, (void *)crd);
720 release_and_free_resource(crd->res_port);
727 static int __init alsa_card_mtpav_init(void)
730 char longname_buffer[80];
732 mtp_card = new_mtpav();
733 if (mtp_card == NULL)
736 mtp_card->card = snd_card_new(index, id, THIS_MODULE, 0);
737 if (mtp_card->card == NULL) {
738 free_mtpav(mtp_card);
742 err = snd_mtpav_get_ISA(mtp_card);
743 //printk("snd_mtpav_get_ISA returned: %d\n", err);
747 strcpy(mtp_card->card->driver, "MTPAV");
748 strcpy(mtp_card->card->shortname, "MTPAV on parallel port");
749 memset(longname_buffer, 0, sizeof(longname_buffer));
750 sprintf(longname_buffer, "MTPAV on parallel port at");
752 err = snd_mtpav_get_RAWMIDI(mtp_card);
753 //snd_printk("snd_mtapv_get_RAWMIDI returned: %d\n", err);
757 if ((err = snd_card_set_generic_dev(mtp_card->card)) < 0)
760 err = snd_card_register(mtp_card->card); // don't snd_card_register until AFTER all cards reources done!
762 //printk("snd_card_register returned %d\n", err);
767 snd_mtpav_portscan(mtp_card);
769 printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
774 snd_card_free(mtp_card->card);
775 free_mtpav(mtp_card);
782 static void __exit alsa_card_mtpav_exit(void)
784 if (mtp_card == NULL)
787 snd_card_free(mtp_card->card);
788 free_mtpav(mtp_card);
794 module_init(alsa_card_mtpav_init)
795 module_exit(alsa_card_mtpav_exit)