1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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>
64
65#include <asm/io.h>
66
67
68
69
70MODULE_AUTHOR("Michael T. Mayers");
71MODULE_DESCRIPTION("MOTU MidiTimePiece AV multiport MIDI");
72MODULE_LICENSE("GPL");
73MODULE_SUPPORTED_DEVICE("{{MOTU,MidiTimePiece AV multiport MIDI}}");
74
75
76#define MTPAV_IOBASE 0x378
77#define MTPAV_IRQ 7
78#define MTPAV_MAX_PORTS 8
79
80static int index = SNDRV_DEFAULT_IDX1;
81static char *id = SNDRV_DEFAULT_STR1;
82static long port = MTPAV_IOBASE;
83static int irq = MTPAV_IRQ;
84static int hwports = MTPAV_MAX_PORTS;
85
86module_param(index, int, 0444);
87MODULE_PARM_DESC(index, "Index value for MotuMTPAV MIDI.");
88module_param(id, charp, 0444);
89MODULE_PARM_DESC(id, "ID string for MotuMTPAV MIDI.");
90module_param(port, long, 0444);
91MODULE_PARM_DESC(port, "Parallel port # for MotuMTPAV MIDI.");
92module_param(irq, int, 0444);
93MODULE_PARM_DESC(irq, "Parallel IRQ # for MotuMTPAV MIDI.");
94module_param(hwports, int, 0444);
95MODULE_PARM_DESC(hwports, "Hardware ports # for MotuMTPAV MIDI.");
96
97static struct platform_device *device;
98
99
100
101
102
103
104
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
110
111#define SIGC_WRITE 0x04
112#define SIGC_READ 0x08
113#define SIGC_INTEN 0x10
114
115#define DREG 0
116#define SREG 1
117#define CREG 2
118
119
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
124
125#define NUMPORTS (0x12+1)
126
127
128
129
130
131struct mtpav_port {
132 u8 number;
133 u8 hwport;
134 u8 mode;
135 u8 running_status;
136 struct snd_rawmidi_substream *input;
137 struct snd_rawmidi_substream *output;
138};
139
140struct mtpav {
141 struct snd_card *card;
142 unsigned long port;
143 struct resource *res_port;
144 int irq;
145 spinlock_t spinlock;
146 int share_irq;
147 int istimer;
148 struct timer_list timer;
149 struct snd_rawmidi *rmidi;
150 int num_ports;
151 struct mtpav_port ports[NUMPORTS];
152
153 u32 inmidiport;
154 u32 inmidistate;
155
156 u32 outmidihwport;
157};
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178#define MTPAV_PIDX_COMPUTER 0
179#define MTPAV_PIDX_ADAT 1
180#define MTPAV_PIDX_BROADCAST 2
181
182
183static int translate_subdevice_to_hwport(struct mtpav *chip, int subdev)
184{
185 if (subdev < 0)
186 return 0x01;
187 else if (subdev < chip->num_ports)
188 return subdev + 1;
189 else if (subdev < chip->num_ports * 2)
190 return subdev - chip->num_ports + 0x09;
191 else if (subdev == chip->num_ports * 2 + MTPAV_PIDX_COMPUTER)
192 return 0x11;
193 else if (subdev == chip->num_ports + MTPAV_PIDX_ADAT)
194 return 0x63;
195 return 0;
196}
197
198static int translate_hwport_to_subdevice(struct mtpav *chip, int hwport)
199{
200 int p;
201 if (hwport <= 0x00)
202 return chip->num_ports + MTPAV_PIDX_BROADCAST;
203 else if (hwport <= 0x08) {
204 p = hwport - 1;
205 if (p >= chip->num_ports)
206 p = 0;
207 return p;
208 } else if (hwport <= 0x10) {
209 p = hwport - 0x09 + chip->num_ports;
210 if (p >= chip->num_ports * 2)
211 p = chip->num_ports;
212 return p;
213 } else if (hwport == 0x11)
214 return chip->num_ports + MTPAV_PIDX_COMPUTER;
215 else
216 return chip->num_ports + MTPAV_PIDX_ADAT;
217}
218
219
220
221
222
223static u8 snd_mtpav_getreg(struct mtpav *chip, u16 reg)
224{
225 u8 rval = 0;
226
227 if (reg == SREG) {
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);
233 }
234
235 return rval;
236}
237
238
239
240
241static inline void snd_mtpav_mputreg(struct mtpav *chip, u16 reg, u8 val)
242{
243 if (reg == DREG || reg == CREG)
244 outb(val, chip->port + reg);
245}
246
247
248
249
250static void snd_mtpav_wait_rfdhi(struct mtpav *chip)
251{
252 int counts = 10000;
253 u8 sbyte;
254
255 sbyte = snd_mtpav_getreg(chip, SREG);
256 while (!(sbyte & SIGS_RFD) && counts--) {
257 sbyte = snd_mtpav_getreg(chip, SREG);
258 udelay(10);
259 }
260}
261
262static void snd_mtpav_send_byte(struct mtpav *chip, u8 byte)
263{
264 u8 tcbyt;
265 u8 clrwrite;
266 u8 setwrite;
267
268 snd_mtpav_wait_rfdhi(chip);
269
270
271
272 tcbyt = snd_mtpav_getreg(chip, CREG);
273 clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
274 setwrite = tcbyt | SIGC_WRITE;
275
276 snd_mtpav_mputreg(chip, DREG, byte);
277 snd_mtpav_mputreg(chip, CREG, clrwrite);
278
279 snd_mtpav_mputreg(chip, CREG, setwrite);
280
281}
282
283
284
285
286
287
288static void snd_mtpav_output_port_write(struct mtpav *mtp_card,
289 struct mtpav_port *portp,
290 struct snd_rawmidi_substream *substream)
291{
292 u8 outbyte;
293
294
295
296 if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
297 return;
298
299
300
301 if (portp->hwport != mtp_card->outmidihwport) {
302 mtp_card->outmidihwport = portp->hwport;
303
304 snd_mtpav_send_byte(mtp_card, 0xf5);
305 snd_mtpav_send_byte(mtp_card, portp->hwport);
306
307
308 if (!(outbyte & 0x80) && portp->running_status)
309 snd_mtpav_send_byte(mtp_card, portp->running_status);
310 }
311
312
313
314 do {
315 if (outbyte & 0x80)
316 portp->running_status = outbyte;
317
318 snd_mtpav_send_byte(mtp_card, outbyte);
319 } while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
320}
321
322static void snd_mtpav_output_write(struct snd_rawmidi_substream *substream)
323{
324 struct mtpav *mtp_card = substream->rmidi->private_data;
325 struct mtpav_port *portp = &mtp_card->ports[substream->number];
326 unsigned long flags;
327
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);
331}
332
333
334
335
336
337
338static void snd_mtpav_portscan(struct mtpav *chip)
339{
340 u8 p;
341
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);
346 }
347}
348
349
350
351
352static int snd_mtpav_input_open(struct snd_rawmidi_substream *substream)
353{
354 struct mtpav *mtp_card = substream->rmidi->private_data;
355 struct mtpav_port *portp = &mtp_card->ports[substream->number];
356 unsigned long flags;
357
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));
363 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
364 return 0;
365}
366
367
368
369
370static int snd_mtpav_input_close(struct snd_rawmidi_substream *substream)
371{
372 struct mtpav *mtp_card = substream->rmidi->private_data;
373 struct mtpav_port *portp = &mtp_card->ports[substream->number];
374 unsigned long flags;
375
376 spin_lock_irqsave(&mtp_card->spinlock, flags);
377 portp->mode &= ~MTPAV_MODE_INPUT_OPENED;
378 portp->input = NULL;
379 if (--mtp_card->share_irq == 0)
380 snd_mtpav_mputreg(mtp_card, CREG, 0);
381 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
382 return 0;
383}
384
385
386
387
388static void snd_mtpav_input_trigger(struct snd_rawmidi_substream *substream, int up)
389{
390 struct mtpav *mtp_card = substream->rmidi->private_data;
391 struct mtpav_port *portp = &mtp_card->ports[substream->number];
392 unsigned long flags;
393
394 spin_lock_irqsave(&mtp_card->spinlock, flags);
395 if (up)
396 portp->mode |= MTPAV_MODE_INPUT_TRIGGERED;
397 else
398 portp->mode &= ~MTPAV_MODE_INPUT_TRIGGERED;
399 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
400
401}
402
403
404
405
406
407
408static void snd_mtpav_output_timer(unsigned long data)
409{
410 unsigned long flags;
411 struct mtpav *chip = (struct mtpav *)data;
412 int p;
413
414 spin_lock_irqsave(&chip->spinlock, flags);
415
416 chip->timer.expires = 1 + jiffies;
417 add_timer(&chip->timer);
418
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);
423 }
424 spin_unlock_irqrestore(&chip->spinlock, flags);
425}
426
427
428static void snd_mtpav_add_output_timer(struct mtpav *chip)
429{
430 chip->timer.expires = 1 + jiffies;
431 add_timer(&chip->timer);
432}
433
434
435static void snd_mtpav_remove_output_timer(struct mtpav *chip)
436{
437 del_timer(&chip->timer);
438}
439
440
441
442
443static int snd_mtpav_output_open(struct snd_rawmidi_substream *substream)
444{
445 struct mtpav *mtp_card = substream->rmidi->private_data;
446 struct mtpav_port *portp = &mtp_card->ports[substream->number];
447 unsigned long flags;
448
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);
453 return 0;
454};
455
456
457
458
459static int snd_mtpav_output_close(struct snd_rawmidi_substream *substream)
460{
461 struct mtpav *mtp_card = substream->rmidi->private_data;
462 struct mtpav_port *portp = &mtp_card->ports[substream->number];
463 unsigned long flags;
464
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);
469 return 0;
470};
471
472
473
474
475static void snd_mtpav_output_trigger(struct snd_rawmidi_substream *substream, int up)
476{
477 struct mtpav *mtp_card = substream->rmidi->private_data;
478 struct mtpav_port *portp = &mtp_card->ports[substream->number];
479 unsigned long flags;
480
481 spin_lock_irqsave(&mtp_card->spinlock, flags);
482 if (up) {
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;
487 }
488 } else {
489 portp->mode &= ~MTPAV_MODE_OUTPUT_TRIGGERED;
490 if (--mtp_card->istimer == 0)
491 snd_mtpav_remove_output_timer(mtp_card);
492 }
493 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
494
495 if (up)
496 snd_mtpav_output_write(substream);
497}
498
499
500
501
502
503static void snd_mtpav_inmidi_process(struct mtpav *mcrd, u8 inbyte)
504{
505 struct mtpav_port *portp;
506
507 if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
508 return;
509
510 portp = &mcrd->ports[mcrd->inmidiport];
511 if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED)
512 snd_rawmidi_receive(portp->input, &inbyte, 1);
513}
514
515static void snd_mtpav_inmidi_h(struct mtpav *mcrd, u8 inbyte)
516{
517 if (inbyte >= 0xf8) {
518
519 snd_mtpav_inmidi_process(mcrd, inbyte);
520 return;
521 }
522
523 if (mcrd->inmidistate == 0) {
524 if (inbyte == 0xf5)
525 mcrd->inmidistate = 1;
526 else
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;
531 }
532}
533
534static void snd_mtpav_read_bytes(struct mtpav *mcrd)
535{
536 u8 clrread, setread;
537 u8 mtp_read_byte;
538 u8 sr, cbyt;
539 int i;
540
541 u8 sbyt = snd_mtpav_getreg(mcrd, SREG);
542
543
544
545 if (!(sbyt & SIGS_BYTE))
546 return;
547
548 cbyt = snd_mtpav_getreg(mcrd, CREG);
549 clrread = cbyt & (SIGC_READ ^ 0xff);
550 setread = cbyt | SIGC_READ;
551
552 do {
553
554 mtp_read_byte = 0;
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);
559
560 sr &= SIGS_IN0 | SIGS_IN1;
561 sr >>= 4;
562 mtp_read_byte |= sr << (i * 2);
563 }
564
565 snd_mtpav_inmidi_h(mcrd, mtp_read_byte);
566
567 sbyt = snd_mtpav_getreg(mcrd, SREG);
568
569 } while (sbyt & SIGS_BYTE);
570}
571
572static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id)
573{
574 struct mtpav *mcard = dev_id;
575
576 spin_lock(&mcard->spinlock);
577 snd_mtpav_read_bytes(mcard);
578 spin_unlock(&mcard->spinlock);
579 return IRQ_HANDLED;
580}
581
582
583
584
585static int __devinit snd_mtpav_get_ISA(struct mtpav * mcard)
586{
587 if ((mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI")) == NULL) {
588 snd_printk("MTVAP port 0x%lx is busy\n", port);
589 return -EBUSY;
590 }
591 mcard->port = port;
592 if (request_irq(irq, snd_mtpav_irqh, IRQF_DISABLED, "MOTU MTPAV", mcard)) {
593 snd_printk("MTVAP IRQ %d busy\n", irq);
594 return -EBUSY;
595 }
596 mcard->irq = irq;
597 return 0;
598}
599
600
601
602
603
604static 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,
608};
609
610static 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,
614};
615
616
617
618
619
620
621static void __devinit snd_mtpav_set_name(struct mtpav *chip,
622 struct snd_rawmidi_substream *substream)
623{
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");
632 else
633 strcpy(substream->name, "MTP broadcast");
634}
635
636static int __devinit snd_mtpav_get_RAWMIDI(struct mtpav *mcard)
637{
638 int rval;
639 struct snd_rawmidi *rawmidi;
640 struct snd_rawmidi_substream *substream;
641 struct list_head *list;
642
643 if (hwports < 1)
644 hwports = 1;
645 else if (hwports > 8)
646 hwports = 8;
647 mcard->num_ports = hwports;
648
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,
652 &mcard->rmidi)) < 0)
653 return rval;
654 rawmidi = mcard->rmidi;
655 rawmidi->private_data = mcard;
656
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;
661 }
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);
667 }
668 rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
669 SNDRV_RAWMIDI_INFO_DUPLEX;
670 sprintf(rawmidi->name, "MTP AV MIDI");
671 return 0;
672}
673
674
675
676
677static void snd_mtpav_free(struct snd_card *card)
678{
679 struct mtpav *crd = card->private_data;
680 unsigned long flags;
681
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);
686 if (crd->irq >= 0)
687 free_irq(crd->irq, (void *)crd);
688 release_and_free_resource(crd->res_port);
689}
690
691
692
693static int __devinit snd_mtpav_probe(struct platform_device *dev)
694{
695 struct snd_card *card;
696 int err;
697 struct mtpav *mtp_card;
698
699 card = snd_card_new(index, id, THIS_MODULE, sizeof(*mtp_card));
700 if (! card)
701 return -ENOMEM;
702
703 mtp_card = card->private_data;
704 spin_lock_init(&mtp_card->spinlock);
705 init_timer(&mtp_card->timer);
706 mtp_card->card = card;
707 mtp_card->irq = -1;
708 mtp_card->share_irq = 0;
709 mtp_card->inmidiport = 0xffffffff;
710 mtp_card->inmidistate = 0;
711 mtp_card->outmidihwport = 0xffffffff;
712 init_timer(&mtp_card->timer);
713 mtp_card->timer.function = snd_mtpav_output_timer;
714 mtp_card->timer.data = (unsigned long) mtp_card;
715
716 card->private_free = snd_mtpav_free;
717
718 err = snd_mtpav_get_RAWMIDI(mtp_card);
719 if (err < 0)
720 goto __error;
721
722 err = snd_mtpav_get_ISA(mtp_card);
723 if (err < 0)
724 goto __error;
725
726 strcpy(card->driver, "MTPAV");
727 strcpy(card->shortname, "MTPAV on parallel port");
728 snprintf(card->longname, sizeof(card->longname),
729 "MTPAV on parallel port at 0x%lx", port);
730
731 snd_mtpav_portscan(mtp_card);
732
733 snd_card_set_dev(card, &dev->dev);
734 err = snd_card_register(mtp_card->card);
735 if (err < 0)
736 goto __error;
737
738 platform_set_drvdata(dev, card);
739 printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
740 return 0;
741
742 __error:
743 snd_card_free(card);
744 return err;
745}
746
747static int __devexit snd_mtpav_remove(struct platform_device *devptr)
748{
749 snd_card_free(platform_get_drvdata(devptr));
750 platform_set_drvdata(devptr, NULL);
751 return 0;
752}
753
754#define SND_MTPAV_DRIVER "snd_mtpav"
755
756static struct platform_driver snd_mtpav_driver = {
757 .probe = snd_mtpav_probe,
758 .remove = __devexit_p(snd_mtpav_remove),
759 .driver = {
760 .name = SND_MTPAV_DRIVER
761 },
762};
763
764static int __init alsa_card_mtpav_init(void)
765{
766 int err;
767
768 if ((err = platform_driver_register(&snd_mtpav_driver)) < 0)
769 return err;
770
771 device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
772 if (!IS_ERR(device)) {
773 if (platform_get_drvdata(device))
774 return 0;
775 platform_device_unregister(device);
776 err = -ENODEV;
777 } else
778 err = PTR_ERR(device);
779 platform_driver_unregister(&snd_mtpav_driver);
780 return err;
781}
782
783static void __exit alsa_card_mtpav_exit(void)
784{
785 platform_device_unregister(device);
786 platform_driver_unregister(&snd_mtpav_driver);
787}
788
789module_init(alsa_card_mtpav_init)
790module_exit(alsa_card_mtpav_exit)
791