1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MOTU Midi Timepiece ALSA Main routines
4 * Copyright by Michael T. Mayers (c) Jan 09, 2000
5 * mail: michael@tweakoz.com
6 * Thanks to John Galbraith
8 * This driver is for the 'Mark Of The Unicorn' (MOTU)
9 * MidiTimePiece AV multiport MIDI interface
13 * 8 MIDI Ins and 8 MIDI outs
14 * Video Sync In (BNC), Word Sync Out (BNC),
17 * 2 programmable pedal/footswitch inputs and 4 programmable MIDI controller knobs.
18 * Macintosh RS422 serial port
19 * RS422 "network" port for ganging multiple MTP's
20 * PC Parallel Port ( which this driver currently uses )
24 * Hardware MIDI routing, merging, and filtering
25 * MIDI Synchronization to Video, ADAT, SMPTE and other Clock sources
26 * 128 'scene' memories, recallable from MIDI program change
29 * Jun 11 2001 Takashi Iwai <tiwai@suse.de>
30 * - Recoded & debugged
31 * - Added timer interrupt for midi outputs
32 * - hwports is between 1 and 8, which specifies the number of hardware ports.
33 * The three global ports, computer, adat and broadcast ports, are created
34 * always after h/w and remote ports.
37 #include <linux/init.h>
38 #include <linux/interrupt.h>
39 #include <linux/module.h>
40 #include <linux/err.h>
41 #include <linux/platform_device.h>
42 #include <linux/ioport.h>
44 #include <linux/moduleparam.h>
45 #include <sound/core.h>
46 #include <sound/initval.h>
47 #include <sound/rawmidi.h>
48 #include <linux/delay.h>
53 MODULE_AUTHOR("Michael T. Mayers");
54 MODULE_DESCRIPTION("MOTU MidiTimePiece AV multiport MIDI");
55 MODULE_LICENSE("GPL");
58 #define MTPAV_IOBASE 0x378
60 #define MTPAV_MAX_PORTS 8
62 static int index = SNDRV_DEFAULT_IDX1;
63 static char *id = SNDRV_DEFAULT_STR1;
64 static long port = MTPAV_IOBASE; /* 0x378, 0x278 */
65 static int irq = MTPAV_IRQ; /* 7, 5 */
66 static int hwports = MTPAV_MAX_PORTS; /* use hardware ports 1-8 */
68 module_param(index, int, 0444);
69 MODULE_PARM_DESC(index, "Index value for MotuMTPAV MIDI.");
70 module_param(id, charp, 0444);
71 MODULE_PARM_DESC(id, "ID string for MotuMTPAV MIDI.");
72 module_param_hw(port, long, ioport, 0444);
73 MODULE_PARM_DESC(port, "Parallel port # for MotuMTPAV MIDI.");
74 module_param_hw(irq, int, irq, 0444);
75 MODULE_PARM_DESC(irq, "Parallel IRQ # for MotuMTPAV MIDI.");
76 module_param(hwports, int, 0444);
77 MODULE_PARM_DESC(hwports, "Hardware ports # for MotuMTPAV MIDI.");
79 static struct platform_device *device;
84 //#define USE_FAKE_MTP // don't actually read/write to MTP device (for debugging without an actual unit) (does not work yet)
86 // parallel port usage masks
87 #define SIGS_BYTE 0x08
93 #define SIGC_WRITE 0x04
94 #define SIGC_READ 0x08
95 #define SIGC_INTEN 0x10
102 #define MTPAV_MODE_INPUT_OPENED 0x01
103 #define MTPAV_MODE_OUTPUT_OPENED 0x02
104 #define MTPAV_MODE_INPUT_TRIGGERED 0x04
105 #define MTPAV_MODE_OUTPUT_TRIGGERED 0x08
107 #define NUMPORTS (0x12+1)
118 struct snd_rawmidi_substream *input;
119 struct snd_rawmidi_substream *output;
123 struct snd_card *card;
125 struct resource *res_port;
126 int irq; /* interrupt (for inputs) */
128 int share_irq; /* number of accesses to input interrupts */
129 int istimer; /* number of accesses to timer interrupts */
130 struct timer_list timer; /* timer interrupts for outputs */
131 struct snd_rawmidi *rmidi;
132 int num_ports; /* number of hw ports (1-8) */
133 struct mtpav_port ports[NUMPORTS]; /* all ports including computer, adat and bc */
135 u32 inmidiport; /* selected input midi port */
136 u32 inmidistate; /* during midi command 0xf5 */
138 u32 outmidihwport; /* selected output midi hw port */
143 * possible hardware ports (selected by 0xf5 port message)
145 * 0x01 .. 0x08 this MTP's ports 1..8
146 * 0x09 .. 0x10 networked MTP's ports (9..16)
147 * 0x11 networked MTP's computer port
151 * subdevice 0 - (X-1) ports
152 * X - (2*X-1) networked ports
157 * where X = chip->num_ports
160 #define MTPAV_PIDX_COMPUTER 0
161 #define MTPAV_PIDX_ADAT 1
162 #define MTPAV_PIDX_BROADCAST 2
165 static int translate_subdevice_to_hwport(struct mtpav *chip, int subdev)
168 return 0x01; /* invalid - use port 0 as default */
169 else if (subdev < chip->num_ports)
170 return subdev + 1; /* single mtp port */
171 else if (subdev < chip->num_ports * 2)
172 return subdev - chip->num_ports + 0x09; /* remote port */
173 else if (subdev == chip->num_ports * 2 + MTPAV_PIDX_COMPUTER)
174 return 0x11; /* computer port */
175 else if (subdev == chip->num_ports + MTPAV_PIDX_ADAT)
176 return 0x63; /* ADAT */
177 return 0; /* all ports */
180 static int translate_hwport_to_subdevice(struct mtpav *chip, int hwport)
183 if (hwport <= 0x00) /* all ports */
184 return chip->num_ports + MTPAV_PIDX_BROADCAST;
185 else if (hwport <= 0x08) { /* single port */
187 if (p >= chip->num_ports)
190 } else if (hwport <= 0x10) { /* remote port */
191 p = hwport - 0x09 + chip->num_ports;
192 if (p >= chip->num_ports * 2)
195 } else if (hwport == 0x11) /* computer port */
196 return chip->num_ports + MTPAV_PIDX_COMPUTER;
198 return chip->num_ports + MTPAV_PIDX_ADAT;
205 static u8 snd_mtpav_getreg(struct mtpav *chip, u16 reg)
210 rval = inb(chip->port + SREG);
211 rval = (rval & 0xf8);
212 } else if (reg == CREG) {
213 rval = inb(chip->port + CREG);
214 rval = (rval & 0x1c);
223 static inline void snd_mtpav_mputreg(struct mtpav *chip, u16 reg, u8 val)
225 if (reg == DREG || reg == CREG)
226 outb(val, chip->port + reg);
232 static void snd_mtpav_wait_rfdhi(struct mtpav *chip)
237 sbyte = snd_mtpav_getreg(chip, SREG);
238 while (!(sbyte & SIGS_RFD) && counts--) {
239 sbyte = snd_mtpav_getreg(chip, SREG);
244 static void snd_mtpav_send_byte(struct mtpav *chip, u8 byte)
250 snd_mtpav_wait_rfdhi(chip);
254 tcbyt = snd_mtpav_getreg(chip, CREG);
255 clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
256 setwrite = tcbyt | SIGC_WRITE;
258 snd_mtpav_mputreg(chip, DREG, byte);
259 snd_mtpav_mputreg(chip, CREG, clrwrite); // clear write bit
261 snd_mtpav_mputreg(chip, CREG, setwrite); // set write bit
269 /* call this with spin lock held */
270 static void snd_mtpav_output_port_write(struct mtpav *mtp_card,
271 struct mtpav_port *portp,
272 struct snd_rawmidi_substream *substream)
276 // Get the outbyte first, so we can emulate running status if
278 if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
281 // send port change command if necessary
283 if (portp->hwport != mtp_card->outmidihwport) {
284 mtp_card->outmidihwport = portp->hwport;
286 snd_mtpav_send_byte(mtp_card, 0xf5);
287 snd_mtpav_send_byte(mtp_card, portp->hwport);
289 snd_printk(KERN_DEBUG "new outport: 0x%x\n",
290 (unsigned int) portp->hwport);
292 if (!(outbyte & 0x80) && portp->running_status)
293 snd_mtpav_send_byte(mtp_card, portp->running_status);
300 portp->running_status = outbyte;
302 snd_mtpav_send_byte(mtp_card, outbyte);
303 } while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
306 static void snd_mtpav_output_write(struct snd_rawmidi_substream *substream)
308 struct mtpav *mtp_card = substream->rmidi->private_data;
309 struct mtpav_port *portp = &mtp_card->ports[substream->number];
312 spin_lock_irqsave(&mtp_card->spinlock, flags);
313 snd_mtpav_output_port_write(mtp_card, portp, substream);
314 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
322 static void snd_mtpav_portscan(struct mtpav *chip) // put mtp into smart routing mode
326 for (p = 0; p < 8; p++) {
327 snd_mtpav_send_byte(chip, 0xf5);
328 snd_mtpav_send_byte(chip, p);
329 snd_mtpav_send_byte(chip, 0xfe);
336 static int snd_mtpav_input_open(struct snd_rawmidi_substream *substream)
338 struct mtpav *mtp_card = substream->rmidi->private_data;
339 struct mtpav_port *portp = &mtp_card->ports[substream->number];
342 spin_lock_irqsave(&mtp_card->spinlock, flags);
343 portp->mode |= MTPAV_MODE_INPUT_OPENED;
344 portp->input = substream;
345 if (mtp_card->share_irq++ == 0)
346 snd_mtpav_mputreg(mtp_card, CREG, (SIGC_INTEN | SIGC_WRITE)); // enable pport interrupts
347 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
354 static int snd_mtpav_input_close(struct snd_rawmidi_substream *substream)
356 struct mtpav *mtp_card = substream->rmidi->private_data;
357 struct mtpav_port *portp = &mtp_card->ports[substream->number];
360 spin_lock_irqsave(&mtp_card->spinlock, flags);
361 portp->mode &= ~MTPAV_MODE_INPUT_OPENED;
363 if (--mtp_card->share_irq == 0)
364 snd_mtpav_mputreg(mtp_card, CREG, 0); // disable pport interrupts
365 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
372 static void snd_mtpav_input_trigger(struct snd_rawmidi_substream *substream, int up)
374 struct mtpav *mtp_card = substream->rmidi->private_data;
375 struct mtpav_port *portp = &mtp_card->ports[substream->number];
378 spin_lock_irqsave(&mtp_card->spinlock, flags);
380 portp->mode |= MTPAV_MODE_INPUT_TRIGGERED;
382 portp->mode &= ~MTPAV_MODE_INPUT_TRIGGERED;
383 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
389 * timer interrupt for outputs
392 static void snd_mtpav_output_timer(struct timer_list *t)
395 struct mtpav *chip = from_timer(chip, t, timer);
398 spin_lock_irqsave(&chip->spinlock, flags);
399 /* reprogram timer */
400 mod_timer(&chip->timer, 1 + jiffies);
401 /* process each port */
402 for (p = 0; p <= chip->num_ports * 2 + MTPAV_PIDX_BROADCAST; p++) {
403 struct mtpav_port *portp = &chip->ports[p];
404 if ((portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED) && portp->output)
405 snd_mtpav_output_port_write(chip, portp, portp->output);
407 spin_unlock_irqrestore(&chip->spinlock, flags);
411 static void snd_mtpav_add_output_timer(struct mtpav *chip)
413 mod_timer(&chip->timer, 1 + jiffies);
417 static void snd_mtpav_remove_output_timer(struct mtpav *chip)
419 del_timer(&chip->timer);
425 static int snd_mtpav_output_open(struct snd_rawmidi_substream *substream)
427 struct mtpav *mtp_card = substream->rmidi->private_data;
428 struct mtpav_port *portp = &mtp_card->ports[substream->number];
431 spin_lock_irqsave(&mtp_card->spinlock, flags);
432 portp->mode |= MTPAV_MODE_OUTPUT_OPENED;
433 portp->output = substream;
434 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
441 static int snd_mtpav_output_close(struct snd_rawmidi_substream *substream)
443 struct mtpav *mtp_card = substream->rmidi->private_data;
444 struct mtpav_port *portp = &mtp_card->ports[substream->number];
447 spin_lock_irqsave(&mtp_card->spinlock, flags);
448 portp->mode &= ~MTPAV_MODE_OUTPUT_OPENED;
449 portp->output = NULL;
450 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
457 static void snd_mtpav_output_trigger(struct snd_rawmidi_substream *substream, int up)
459 struct mtpav *mtp_card = substream->rmidi->private_data;
460 struct mtpav_port *portp = &mtp_card->ports[substream->number];
463 spin_lock_irqsave(&mtp_card->spinlock, flags);
465 if (! (portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED)) {
466 if (mtp_card->istimer++ == 0)
467 snd_mtpav_add_output_timer(mtp_card);
468 portp->mode |= MTPAV_MODE_OUTPUT_TRIGGERED;
471 portp->mode &= ~MTPAV_MODE_OUTPUT_TRIGGERED;
472 if (--mtp_card->istimer == 0)
473 snd_mtpav_remove_output_timer(mtp_card);
475 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
478 snd_mtpav_output_write(substream);
482 * midi interrupt for inputs
485 static void snd_mtpav_inmidi_process(struct mtpav *mcrd, u8 inbyte)
487 struct mtpav_port *portp;
489 if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
492 portp = &mcrd->ports[mcrd->inmidiport];
493 if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED)
494 snd_rawmidi_receive(portp->input, &inbyte, 1);
497 static void snd_mtpav_inmidi_h(struct mtpav *mcrd, u8 inbyte)
499 if (inbyte >= 0xf8) {
500 /* real-time midi code */
501 snd_mtpav_inmidi_process(mcrd, inbyte);
505 if (mcrd->inmidistate == 0) { // awaiting command
506 if (inbyte == 0xf5) // MTP port #
507 mcrd->inmidistate = 1;
509 snd_mtpav_inmidi_process(mcrd, inbyte);
510 } else if (mcrd->inmidistate) {
511 mcrd->inmidiport = translate_hwport_to_subdevice(mcrd, inbyte);
512 mcrd->inmidistate = 0;
516 static void snd_mtpav_read_bytes(struct mtpav *mcrd)
523 u8 sbyt = snd_mtpav_getreg(mcrd, SREG);
525 /* printk(KERN_DEBUG "snd_mtpav_read_bytes() sbyt: 0x%x\n", sbyt); */
527 if (!(sbyt & SIGS_BYTE))
530 cbyt = snd_mtpav_getreg(mcrd, CREG);
531 clrread = cbyt & (SIGC_READ ^ 0xff);
532 setread = cbyt | SIGC_READ;
537 for (i = 0; i < 4; i++) {
538 snd_mtpav_mputreg(mcrd, CREG, setread);
539 sr = snd_mtpav_getreg(mcrd, SREG);
540 snd_mtpav_mputreg(mcrd, CREG, clrread);
542 sr &= SIGS_IN0 | SIGS_IN1;
544 mtp_read_byte |= sr << (i * 2);
547 snd_mtpav_inmidi_h(mcrd, mtp_read_byte);
549 sbyt = snd_mtpav_getreg(mcrd, SREG);
551 } while (sbyt & SIGS_BYTE);
554 static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id)
556 struct mtpav *mcard = dev_id;
558 spin_lock(&mcard->spinlock);
559 snd_mtpav_read_bytes(mcard);
560 spin_unlock(&mcard->spinlock);
567 static int snd_mtpav_get_ISA(struct mtpav *mcard)
569 if ((mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI")) == NULL) {
570 snd_printk(KERN_ERR "MTVAP port 0x%lx is busy\n", port);
574 if (request_irq(irq, snd_mtpav_irqh, 0, "MOTU MTPAV", mcard)) {
575 snd_printk(KERN_ERR "MTVAP IRQ %d busy\n", irq);
586 static const struct snd_rawmidi_ops snd_mtpav_output = {
587 .open = snd_mtpav_output_open,
588 .close = snd_mtpav_output_close,
589 .trigger = snd_mtpav_output_trigger,
592 static const struct snd_rawmidi_ops snd_mtpav_input = {
593 .open = snd_mtpav_input_open,
594 .close = snd_mtpav_input_close,
595 .trigger = snd_mtpav_input_trigger,
600 * get RAWMIDI resources
603 static void snd_mtpav_set_name(struct mtpav *chip,
604 struct snd_rawmidi_substream *substream)
606 if (substream->number >= 0 && substream->number < chip->num_ports)
607 sprintf(substream->name, "MTP direct %d", (substream->number % chip->num_ports) + 1);
608 else if (substream->number >= 8 && substream->number < chip->num_ports * 2)
609 sprintf(substream->name, "MTP remote %d", (substream->number % chip->num_ports) + 1);
610 else if (substream->number == chip->num_ports * 2)
611 strcpy(substream->name, "MTP computer");
612 else if (substream->number == chip->num_ports * 2 + 1)
613 strcpy(substream->name, "MTP ADAT");
615 strcpy(substream->name, "MTP broadcast");
618 static int snd_mtpav_get_RAWMIDI(struct mtpav *mcard)
621 struct snd_rawmidi *rawmidi;
622 struct snd_rawmidi_substream *substream;
623 struct list_head *list;
627 else if (hwports > 8)
629 mcard->num_ports = hwports;
631 if ((rval = snd_rawmidi_new(mcard->card, "MotuMIDI", 0,
632 mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
633 mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
636 rawmidi = mcard->rmidi;
637 rawmidi->private_data = mcard;
639 list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
640 substream = list_entry(list, struct snd_rawmidi_substream, list);
641 snd_mtpav_set_name(mcard, substream);
642 substream->ops = &snd_mtpav_input;
644 list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
645 substream = list_entry(list, struct snd_rawmidi_substream, list);
646 snd_mtpav_set_name(mcard, substream);
647 substream->ops = &snd_mtpav_output;
648 mcard->ports[substream->number].hwport = translate_subdevice_to_hwport(mcard, substream->number);
650 rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
651 SNDRV_RAWMIDI_INFO_DUPLEX;
652 sprintf(rawmidi->name, "MTP AV MIDI");
659 static void snd_mtpav_free(struct snd_card *card)
661 struct mtpav *crd = card->private_data;
664 spin_lock_irqsave(&crd->spinlock, flags);
665 if (crd->istimer > 0)
666 snd_mtpav_remove_output_timer(crd);
667 spin_unlock_irqrestore(&crd->spinlock, flags);
669 free_irq(crd->irq, (void *)crd);
670 release_and_free_resource(crd->res_port);
675 static int snd_mtpav_probe(struct platform_device *dev)
677 struct snd_card *card;
679 struct mtpav *mtp_card;
681 err = snd_card_new(&dev->dev, index, id, THIS_MODULE,
682 sizeof(*mtp_card), &card);
686 mtp_card = card->private_data;
687 spin_lock_init(&mtp_card->spinlock);
688 mtp_card->card = card;
690 mtp_card->share_irq = 0;
691 mtp_card->inmidistate = 0;
692 mtp_card->outmidihwport = 0xffffffff;
693 timer_setup(&mtp_card->timer, snd_mtpav_output_timer, 0);
695 card->private_free = snd_mtpav_free;
697 err = snd_mtpav_get_RAWMIDI(mtp_card);
701 mtp_card->inmidiport = mtp_card->num_ports + MTPAV_PIDX_BROADCAST;
703 err = snd_mtpav_get_ISA(mtp_card);
707 strcpy(card->driver, "MTPAV");
708 strcpy(card->shortname, "MTPAV on parallel port");
709 snprintf(card->longname, sizeof(card->longname),
710 "MTPAV on parallel port at 0x%lx", port);
712 snd_mtpav_portscan(mtp_card);
714 err = snd_card_register(mtp_card->card);
718 platform_set_drvdata(dev, card);
719 printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
727 static int snd_mtpav_remove(struct platform_device *devptr)
729 snd_card_free(platform_get_drvdata(devptr));
733 #define SND_MTPAV_DRIVER "snd_mtpav"
735 static struct platform_driver snd_mtpav_driver = {
736 .probe = snd_mtpav_probe,
737 .remove = snd_mtpav_remove,
739 .name = SND_MTPAV_DRIVER,
743 static int __init alsa_card_mtpav_init(void)
747 if ((err = platform_driver_register(&snd_mtpav_driver)) < 0)
750 device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
751 if (!IS_ERR(device)) {
752 if (platform_get_drvdata(device))
754 platform_device_unregister(device);
757 err = PTR_ERR(device);
758 platform_driver_unregister(&snd_mtpav_driver);
762 static void __exit alsa_card_mtpav_exit(void)
764 platform_device_unregister(device);
765 platform_driver_unregister(&snd_mtpav_driver);
768 module_init(alsa_card_mtpav_init)
769 module_exit(alsa_card_mtpav_exit)