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 <linux/init.h>
54 #include <linux/interrupt.h>
55 #include <linux/err.h>
56 #include <linux/platform_device.h>
57 #include <linux/slab.h>
58 #include <linux/ioport.h>
59 #include <linux/moduleparam.h>
60 #include <sound/core.h>
61 #include <sound/initval.h>
62 #include <sound/rawmidi.h>
63 #include <linux/delay.h>
70 MODULE_AUTHOR("Michael T. Mayers");
71 MODULE_DESCRIPTION("MOTU MidiTimePiece AV multiport MIDI");
72 MODULE_LICENSE("GPL");
73 MODULE_SUPPORTED_DEVICE("{{MOTU,MidiTimePiece AV multiport MIDI}}");
76 #define MTPAV_IOBASE 0x378
78 #define MTPAV_MAX_PORTS 8
80 static int index = SNDRV_DEFAULT_IDX1;
81 static char *id = SNDRV_DEFAULT_STR1;
82 static long port = MTPAV_IOBASE; /* 0x378, 0x278 */
83 static int irq = MTPAV_IRQ; /* 7, 5 */
84 static int hwports = MTPAV_MAX_PORTS; /* use hardware ports 1-8 */
86 module_param(index, int, 0444);
87 MODULE_PARM_DESC(index, "Index value for MotuMTPAV MIDI.");
88 module_param(id, charp, 0444);
89 MODULE_PARM_DESC(id, "ID string for MotuMTPAV MIDI.");
90 module_param(port, long, 0444);
91 MODULE_PARM_DESC(port, "Parallel port # for MotuMTPAV MIDI.");
92 module_param(irq, int, 0444);
93 MODULE_PARM_DESC(irq, "Parallel IRQ # for MotuMTPAV MIDI.");
94 module_param(hwports, int, 0444);
95 MODULE_PARM_DESC(hwports, "Hardware ports # for MotuMTPAV MIDI.");
97 static struct platform_device *device;
102 //#define USE_FAKE_MTP // don't actually read/write to MTP device (for debugging without an actual unit) (does not work yet)
104 // parallel port usage masks
105 #define SIGS_BYTE 0x08
106 #define SIGS_RFD 0x80
107 #define SIGS_IRQ 0x40
108 #define SIGS_IN0 0x10
109 #define SIGS_IN1 0x20
111 #define SIGC_WRITE 0x04
112 #define SIGC_READ 0x08
113 #define SIGC_INTEN 0x10
120 #define MTPAV_MODE_INPUT_OPENED 0x01
121 #define MTPAV_MODE_OUTPUT_OPENED 0x02
122 #define MTPAV_MODE_INPUT_TRIGGERED 0x04
123 #define MTPAV_MODE_OUTPUT_TRIGGERED 0x08
125 #define NUMPORTS (0x12+1)
136 struct snd_rawmidi_substream *input;
137 struct snd_rawmidi_substream *output;
141 struct snd_card *card;
143 struct resource *res_port;
144 int irq; /* interrupt (for inputs) */
146 int share_irq; /* number of accesses to input interrupts */
147 int istimer; /* number of accesses to timer interrupts */
148 struct timer_list timer; /* timer interrupts for outputs */
149 struct snd_rawmidi *rmidi;
150 int num_ports; /* number of hw ports (1-8) */
151 struct mtpav_port ports[NUMPORTS]; /* all ports including computer, adat and bc */
153 u32 inmidiport; /* selected input midi port */
154 u32 inmidistate; /* during midi command 0xf5 */
156 u32 outmidihwport; /* selected output midi hw port */
161 * possible hardware ports (selected by 0xf5 port message)
163 * 0x01 .. 0x08 this MTP's ports 1..8
164 * 0x09 .. 0x10 networked MTP's ports (9..16)
165 * 0x11 networked MTP's computer port
169 * subdevice 0 - (X-1) ports
170 * X - (2*X-1) networked ports
175 * where X = chip->num_ports
178 #define MTPAV_PIDX_COMPUTER 0
179 #define MTPAV_PIDX_ADAT 1
180 #define MTPAV_PIDX_BROADCAST 2
183 static int translate_subdevice_to_hwport(struct mtpav *chip, int subdev)
186 return 0x01; /* invalid - use port 0 as default */
187 else if (subdev < chip->num_ports)
188 return subdev + 1; /* single mtp port */
189 else if (subdev < chip->num_ports * 2)
190 return subdev - chip->num_ports + 0x09; /* remote port */
191 else if (subdev == chip->num_ports * 2 + MTPAV_PIDX_COMPUTER)
192 return 0x11; /* computer port */
193 else if (subdev == chip->num_ports + MTPAV_PIDX_ADAT)
194 return 0x63; /* ADAT */
195 return 0; /* all ports */
198 static int translate_hwport_to_subdevice(struct mtpav *chip, int hwport)
201 if (hwport <= 0x00) /* all ports */
202 return chip->num_ports + MTPAV_PIDX_BROADCAST;
203 else if (hwport <= 0x08) { /* single port */
205 if (p >= chip->num_ports)
208 } else if (hwport <= 0x10) { /* remote port */
209 p = hwport - 0x09 + chip->num_ports;
210 if (p >= chip->num_ports * 2)
213 } else if (hwport == 0x11) /* computer port */
214 return chip->num_ports + MTPAV_PIDX_COMPUTER;
216 return chip->num_ports + MTPAV_PIDX_ADAT;
223 static u8 snd_mtpav_getreg(struct mtpav *chip, u16 reg)
228 rval = inb(chip->port + SREG);
229 rval = (rval & 0xf8);
230 } else if (reg == CREG) {
231 rval = inb(chip->port + CREG);
232 rval = (rval & 0x1c);
241 static inline void snd_mtpav_mputreg(struct mtpav *chip, u16 reg, u8 val)
243 if (reg == DREG || reg == CREG)
244 outb(val, chip->port + reg);
250 static void snd_mtpav_wait_rfdhi(struct mtpav *chip)
255 sbyte = snd_mtpav_getreg(chip, SREG);
256 while (!(sbyte & SIGS_RFD) && counts--) {
257 sbyte = snd_mtpav_getreg(chip, SREG);
262 static void snd_mtpav_send_byte(struct mtpav *chip, u8 byte)
268 snd_mtpav_wait_rfdhi(chip);
272 tcbyt = snd_mtpav_getreg(chip, CREG);
273 clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
274 setwrite = tcbyt | SIGC_WRITE;
276 snd_mtpav_mputreg(chip, DREG, byte);
277 snd_mtpav_mputreg(chip, CREG, clrwrite); // clear write bit
279 snd_mtpav_mputreg(chip, CREG, setwrite); // set write bit
287 /* call this with spin lock held */
288 static void snd_mtpav_output_port_write(struct mtpav *mtp_card,
289 struct mtpav_port *portp,
290 struct snd_rawmidi_substream *substream)
294 // Get the outbyte first, so we can emulate running status if
296 if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
299 // send port change command if necessary
301 if (portp->hwport != mtp_card->outmidihwport) {
302 mtp_card->outmidihwport = portp->hwport;
304 snd_mtpav_send_byte(mtp_card, 0xf5);
305 snd_mtpav_send_byte(mtp_card, portp->hwport);
306 //snd_printk("new outport: 0x%x\n", (unsigned int) portp->hwport);
308 if (!(outbyte & 0x80) && portp->running_status)
309 snd_mtpav_send_byte(mtp_card, portp->running_status);
316 portp->running_status = outbyte;
318 snd_mtpav_send_byte(mtp_card, outbyte);
319 } while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
322 static void snd_mtpav_output_write(struct snd_rawmidi_substream *substream)
324 struct mtpav *mtp_card = substream->rmidi->private_data;
325 struct mtpav_port *portp = &mtp_card->ports[substream->number];
328 spin_lock_irqsave(&mtp_card->spinlock, flags);
329 snd_mtpav_output_port_write(mtp_card, portp, substream);
330 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
338 static void snd_mtpav_portscan(struct mtpav *chip) // put mtp into smart routing mode
342 for (p = 0; p < 8; p++) {
343 snd_mtpav_send_byte(chip, 0xf5);
344 snd_mtpav_send_byte(chip, p);
345 snd_mtpav_send_byte(chip, 0xfe);
352 static int snd_mtpav_input_open(struct snd_rawmidi_substream *substream)
354 struct mtpav *mtp_card = substream->rmidi->private_data;
355 struct mtpav_port *portp = &mtp_card->ports[substream->number];
358 spin_lock_irqsave(&mtp_card->spinlock, flags);
359 portp->mode |= MTPAV_MODE_INPUT_OPENED;
360 portp->input = substream;
361 if (mtp_card->share_irq++ == 0)
362 snd_mtpav_mputreg(mtp_card, CREG, (SIGC_INTEN | SIGC_WRITE)); // enable pport interrupts
363 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
370 static int snd_mtpav_input_close(struct snd_rawmidi_substream *substream)
372 struct mtpav *mtp_card = substream->rmidi->private_data;
373 struct mtpav_port *portp = &mtp_card->ports[substream->number];
376 spin_lock_irqsave(&mtp_card->spinlock, flags);
377 portp->mode &= ~MTPAV_MODE_INPUT_OPENED;
379 if (--mtp_card->share_irq == 0)
380 snd_mtpav_mputreg(mtp_card, CREG, 0); // disable pport interrupts
381 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
388 static void snd_mtpav_input_trigger(struct snd_rawmidi_substream *substream, int up)
390 struct mtpav *mtp_card = substream->rmidi->private_data;
391 struct mtpav_port *portp = &mtp_card->ports[substream->number];
394 spin_lock_irqsave(&mtp_card->spinlock, flags);
396 portp->mode |= MTPAV_MODE_INPUT_TRIGGERED;
398 portp->mode &= ~MTPAV_MODE_INPUT_TRIGGERED;
399 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
405 * timer interrupt for outputs
408 static void snd_mtpav_output_timer(unsigned long data)
411 struct mtpav *chip = (struct mtpav *)data;
414 spin_lock_irqsave(&chip->spinlock, flags);
415 /* reprogram timer */
416 chip->timer.expires = 1 + jiffies;
417 add_timer(&chip->timer);
418 /* process each port */
419 for (p = 0; p <= chip->num_ports * 2 + MTPAV_PIDX_BROADCAST; p++) {
420 struct mtpav_port *portp = &chip->ports[p];
421 if ((portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED) && portp->output)
422 snd_mtpav_output_port_write(chip, portp, portp->output);
424 spin_unlock_irqrestore(&chip->spinlock, flags);
428 static void snd_mtpav_add_output_timer(struct mtpav *chip)
430 chip->timer.expires = 1 + jiffies;
431 add_timer(&chip->timer);
435 static void snd_mtpav_remove_output_timer(struct mtpav *chip)
437 del_timer(&chip->timer);
443 static int snd_mtpav_output_open(struct snd_rawmidi_substream *substream)
445 struct mtpav *mtp_card = substream->rmidi->private_data;
446 struct mtpav_port *portp = &mtp_card->ports[substream->number];
449 spin_lock_irqsave(&mtp_card->spinlock, flags);
450 portp->mode |= MTPAV_MODE_OUTPUT_OPENED;
451 portp->output = substream;
452 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
459 static int snd_mtpav_output_close(struct snd_rawmidi_substream *substream)
461 struct mtpav *mtp_card = substream->rmidi->private_data;
462 struct mtpav_port *portp = &mtp_card->ports[substream->number];
465 spin_lock_irqsave(&mtp_card->spinlock, flags);
466 portp->mode &= ~MTPAV_MODE_OUTPUT_OPENED;
467 portp->output = NULL;
468 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
475 static void snd_mtpav_output_trigger(struct snd_rawmidi_substream *substream, int up)
477 struct mtpav *mtp_card = substream->rmidi->private_data;
478 struct mtpav_port *portp = &mtp_card->ports[substream->number];
481 spin_lock_irqsave(&mtp_card->spinlock, flags);
483 if (! (portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED)) {
484 if (mtp_card->istimer++ == 0)
485 snd_mtpav_add_output_timer(mtp_card);
486 portp->mode |= MTPAV_MODE_OUTPUT_TRIGGERED;
489 portp->mode &= ~MTPAV_MODE_OUTPUT_TRIGGERED;
490 if (--mtp_card->istimer == 0)
491 snd_mtpav_remove_output_timer(mtp_card);
493 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
496 snd_mtpav_output_write(substream);
500 * midi interrupt for inputs
503 static void snd_mtpav_inmidi_process(struct mtpav *mcrd, u8 inbyte)
505 struct mtpav_port *portp;
507 if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
510 portp = &mcrd->ports[mcrd->inmidiport];
511 if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED)
512 snd_rawmidi_receive(portp->input, &inbyte, 1);
515 static void snd_mtpav_inmidi_h(struct mtpav *mcrd, u8 inbyte)
517 if (inbyte >= 0xf8) {
518 /* real-time midi code */
519 snd_mtpav_inmidi_process(mcrd, inbyte);
523 if (mcrd->inmidistate == 0) { // awaiting command
524 if (inbyte == 0xf5) // MTP port #
525 mcrd->inmidistate = 1;
527 snd_mtpav_inmidi_process(mcrd, inbyte);
528 } else if (mcrd->inmidistate) {
529 mcrd->inmidiport = translate_hwport_to_subdevice(mcrd, inbyte);
530 mcrd->inmidistate = 0;
534 static void snd_mtpav_read_bytes(struct mtpav *mcrd)
541 u8 sbyt = snd_mtpav_getreg(mcrd, SREG);
543 //printk("snd_mtpav_read_bytes() sbyt: 0x%x\n", sbyt);
545 if (!(sbyt & SIGS_BYTE))
548 cbyt = snd_mtpav_getreg(mcrd, CREG);
549 clrread = cbyt & (SIGC_READ ^ 0xff);
550 setread = cbyt | SIGC_READ;
555 for (i = 0; i < 4; i++) {
556 snd_mtpav_mputreg(mcrd, CREG, setread);
557 sr = snd_mtpav_getreg(mcrd, SREG);
558 snd_mtpav_mputreg(mcrd, CREG, clrread);
560 sr &= SIGS_IN0 | SIGS_IN1;
562 mtp_read_byte |= sr << (i * 2);
565 snd_mtpav_inmidi_h(mcrd, mtp_read_byte);
567 sbyt = snd_mtpav_getreg(mcrd, SREG);
569 } while (sbyt & SIGS_BYTE);
572 static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id)
574 struct mtpav *mcard = dev_id;
576 spin_lock(&mcard->spinlock);
577 snd_mtpav_read_bytes(mcard);
578 spin_unlock(&mcard->spinlock);
585 static int __devinit snd_mtpav_get_ISA(struct mtpav * mcard)
587 if ((mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI")) == NULL) {
588 snd_printk("MTVAP port 0x%lx is busy\n", port);
592 if (request_irq(irq, snd_mtpav_irqh, IRQF_DISABLED, "MOTU MTPAV", mcard)) {
593 snd_printk("MTVAP IRQ %d busy\n", irq);
604 static struct snd_rawmidi_ops snd_mtpav_output = {
605 .open = snd_mtpav_output_open,
606 .close = snd_mtpav_output_close,
607 .trigger = snd_mtpav_output_trigger,
610 static struct snd_rawmidi_ops snd_mtpav_input = {
611 .open = snd_mtpav_input_open,
612 .close = snd_mtpav_input_close,
613 .trigger = snd_mtpav_input_trigger,
618 * get RAWMIDI resources
621 static void __devinit snd_mtpav_set_name(struct mtpav *chip,
622 struct snd_rawmidi_substream *substream)
624 if (substream->number >= 0 && substream->number < chip->num_ports)
625 sprintf(substream->name, "MTP direct %d", (substream->number % chip->num_ports) + 1);
626 else if (substream->number >= 8 && substream->number < chip->num_ports * 2)
627 sprintf(substream->name, "MTP remote %d", (substream->number % chip->num_ports) + 1);
628 else if (substream->number == chip->num_ports * 2)
629 strcpy(substream->name, "MTP computer");
630 else if (substream->number == chip->num_ports * 2 + 1)
631 strcpy(substream->name, "MTP ADAT");
633 strcpy(substream->name, "MTP broadcast");
636 static int __devinit snd_mtpav_get_RAWMIDI(struct mtpav *mcard)
639 struct snd_rawmidi *rawmidi;
640 struct snd_rawmidi_substream *substream;
641 struct list_head *list;
645 else if (hwports > 8)
647 mcard->num_ports = hwports;
649 if ((rval = snd_rawmidi_new(mcard->card, "MotuMIDI", 0,
650 mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
651 mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
654 rawmidi = mcard->rmidi;
655 rawmidi->private_data = mcard;
657 list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
658 substream = list_entry(list, struct snd_rawmidi_substream, list);
659 snd_mtpav_set_name(mcard, substream);
660 substream->ops = &snd_mtpav_input;
662 list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
663 substream = list_entry(list, struct snd_rawmidi_substream, list);
664 snd_mtpav_set_name(mcard, substream);
665 substream->ops = &snd_mtpav_output;
666 mcard->ports[substream->number].hwport = translate_subdevice_to_hwport(mcard, substream->number);
668 rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
669 SNDRV_RAWMIDI_INFO_DUPLEX;
670 sprintf(rawmidi->name, "MTP AV MIDI");
677 static void snd_mtpav_free(struct snd_card *card)
679 struct mtpav *crd = card->private_data;
682 spin_lock_irqsave(&crd->spinlock, flags);
683 if (crd->istimer > 0)
684 snd_mtpav_remove_output_timer(crd);
685 spin_unlock_irqrestore(&crd->spinlock, flags);
687 free_irq(crd->irq, (void *)crd);
688 release_and_free_resource(crd->res_port);
693 static int __devinit snd_mtpav_probe(struct platform_device *dev)
695 struct snd_card *card;
697 struct mtpav *mtp_card;
699 card = snd_card_new(index, id, THIS_MODULE, sizeof(*mtp_card));
703 mtp_card = card->private_data;
704 spin_lock_init(&mtp_card->spinlock);
705 init_timer(&mtp_card->timer);
706 mtp_card->card = card;
708 mtp_card->share_irq = 0;
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_RAWMIDI(mtp_card);
721 mtp_card->inmidiport = mtp_card->num_ports + MTPAV_PIDX_BROADCAST;
723 err = snd_mtpav_get_ISA(mtp_card);
727 strcpy(card->driver, "MTPAV");
728 strcpy(card->shortname, "MTPAV on parallel port");
729 snprintf(card->longname, sizeof(card->longname),
730 "MTPAV on parallel port at 0x%lx", port);
732 snd_mtpav_portscan(mtp_card);
734 snd_card_set_dev(card, &dev->dev);
735 err = snd_card_register(mtp_card->card);
739 platform_set_drvdata(dev, card);
740 printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
748 static int __devexit snd_mtpav_remove(struct platform_device *devptr)
750 snd_card_free(platform_get_drvdata(devptr));
751 platform_set_drvdata(devptr, NULL);
755 #define SND_MTPAV_DRIVER "snd_mtpav"
757 static struct platform_driver snd_mtpav_driver = {
758 .probe = snd_mtpav_probe,
759 .remove = __devexit_p(snd_mtpav_remove),
761 .name = SND_MTPAV_DRIVER
765 static int __init alsa_card_mtpav_init(void)
769 if ((err = platform_driver_register(&snd_mtpav_driver)) < 0)
772 device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
773 if (!IS_ERR(device)) {
774 if (platform_get_drvdata(device))
776 platform_device_unregister(device);
779 err = PTR_ERR(device);
780 platform_driver_unregister(&snd_mtpav_driver);
784 static void __exit alsa_card_mtpav_exit(void)
786 platform_device_unregister(device);
787 platform_driver_unregister(&snd_mtpav_driver);
790 module_init(alsa_card_mtpav_init)
791 module_exit(alsa_card_mtpav_exit)