Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
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
7 *
8 * This driver is for the 'Mark Of The Unicorn' (MOTU)
9 * MidiTimePiece AV multiport MIDI interface
10 *
11 * IOPORTS
12 * -------
13 * 8 MIDI Ins and 8 MIDI outs
14 * Video Sync In (BNC), Word Sync Out (BNC),
15 * ADAT Sync Out (DB9)
16 * SMPTE in/out (1/4")
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 )
21 *
22 * MISC FEATURES
23 * -------------
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
27 *
28 * ChangeLog
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.
35 */
36
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>
43#include <linux/io.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>
49
50/*
51 * globals
52 */
53MODULE_AUTHOR("Michael T. Mayers");
54MODULE_DESCRIPTION("MOTU MidiTimePiece AV multiport MIDI");
55MODULE_LICENSE("GPL");
56
57// io resources
58#define MTPAV_IOBASE 0x378
59#define MTPAV_IRQ 7
60#define MTPAV_MAX_PORTS 8
61
62static int index = SNDRV_DEFAULT_IDX1;
63static char *id = SNDRV_DEFAULT_STR1;
64static long port = MTPAV_IOBASE; /* 0x378, 0x278 */
65static int irq = MTPAV_IRQ; /* 7, 5 */
66static int hwports = MTPAV_MAX_PORTS; /* use hardware ports 1-8 */
67
68module_param(index, int, 0444);
69MODULE_PARM_DESC(index, "Index value for MotuMTPAV MIDI.");
70module_param(id, charp, 0444);
71MODULE_PARM_DESC(id, "ID string for MotuMTPAV MIDI.");
72module_param_hw(port, long, ioport, 0444);
73MODULE_PARM_DESC(port, "Parallel port # for MotuMTPAV MIDI.");
74module_param_hw(irq, int, irq, 0444);
75MODULE_PARM_DESC(irq, "Parallel IRQ # for MotuMTPAV MIDI.");
76module_param(hwports, int, 0444);
77MODULE_PARM_DESC(hwports, "Hardware ports # for MotuMTPAV MIDI.");
78
79static struct platform_device *device;
80
81/*
82 * defines
83 */
84//#define USE_FAKE_MTP // don't actually read/write to MTP device (for debugging without an actual unit) (does not work yet)
85
86// parallel port usage masks
87#define SIGS_BYTE 0x08
88#define SIGS_RFD 0x80
89#define SIGS_IRQ 0x40
90#define SIGS_IN0 0x10
91#define SIGS_IN1 0x20
92
93#define SIGC_WRITE 0x04
94#define SIGC_READ 0x08
95#define SIGC_INTEN 0x10
96
97#define DREG 0
98#define SREG 1
99#define CREG 2
100
101//
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
106
107#define NUMPORTS (0x12+1)
108
109
110/*
111 */
112
113struct mtpav_port {
114 u8 number;
115 u8 hwport;
116 u8 mode;
117 u8 running_status;
118 struct snd_rawmidi_substream *input;
119 struct snd_rawmidi_substream *output;
120};
121
122struct mtpav {
123 struct snd_card *card;
124 unsigned long port;
125 struct resource *res_port;
126 int irq; /* interrupt (for inputs) */
127 spinlock_t spinlock;
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 */
134
135 u32 inmidiport; /* selected input midi port */
136 u32 inmidistate; /* during midi command 0xf5 */
137
138 u32 outmidihwport; /* selected output midi hw port */
139};
140
141
142/*
143 * possible hardware ports (selected by 0xf5 port message)
144 * 0x00 all ports
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
148 * 0x63 to ADAT
149 *
150 * mappig:
151 * subdevice 0 - (X-1) ports
152 * X - (2*X-1) networked ports
153 * X computer
154 * X+1 ADAT
155 * X+2 all ports
156 *
157 * where X = chip->num_ports
158 */
159
160#define MTPAV_PIDX_COMPUTER 0
161#define MTPAV_PIDX_ADAT 1
162#define MTPAV_PIDX_BROADCAST 2
163
164
165static int translate_subdevice_to_hwport(struct mtpav *chip, int subdev)
166{
167 if (subdev < 0)
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 */
178}
179
180static int translate_hwport_to_subdevice(struct mtpav *chip, int hwport)
181{
182 int p;
183 if (hwport <= 0x00) /* all ports */
184 return chip->num_ports + MTPAV_PIDX_BROADCAST;
185 else if (hwport <= 0x08) { /* single port */
186 p = hwport - 1;
187 if (p >= chip->num_ports)
188 p = 0;
189 return p;
190 } else if (hwport <= 0x10) { /* remote port */
191 p = hwport - 0x09 + chip->num_ports;
192 if (p >= chip->num_ports * 2)
193 p = chip->num_ports;
194 return p;
195 } else if (hwport == 0x11) /* computer port */
196 return chip->num_ports + MTPAV_PIDX_COMPUTER;
197 else /* ADAT */
198 return chip->num_ports + MTPAV_PIDX_ADAT;
199}
200
201
202/*
203 */
204
205static u8 snd_mtpav_getreg(struct mtpav *chip, u16 reg)
206{
207 u8 rval = 0;
208
209 if (reg == SREG) {
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);
215 }
216
217 return rval;
218}
219
220/*
221 */
222
223static inline void snd_mtpav_mputreg(struct mtpav *chip, u16 reg, u8 val)
224{
225 if (reg == DREG || reg == CREG)
226 outb(val, chip->port + reg);
227}
228
229/*
230 */
231
232static void snd_mtpav_wait_rfdhi(struct mtpav *chip)
233{
234 int counts = 10000;
235 u8 sbyte;
236
237 sbyte = snd_mtpav_getreg(chip, SREG);
238 while (!(sbyte & SIGS_RFD) && counts--) {
239 sbyte = snd_mtpav_getreg(chip, SREG);
240 udelay(10);
241 }
242}
243
244static void snd_mtpav_send_byte(struct mtpav *chip, u8 byte)
245{
246 u8 tcbyt;
247 u8 clrwrite;
248 u8 setwrite;
249
250 snd_mtpav_wait_rfdhi(chip);
251
252 /////////////////
253
254 tcbyt = snd_mtpav_getreg(chip, CREG);
255 clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
256 setwrite = tcbyt | SIGC_WRITE;
257
258 snd_mtpav_mputreg(chip, DREG, byte);
259 snd_mtpav_mputreg(chip, CREG, clrwrite); // clear write bit
260
261 snd_mtpav_mputreg(chip, CREG, setwrite); // set write bit
262
263}
264
265
266/*
267 */
268
269/* call this with spin lock held */
270static void snd_mtpav_output_port_write(struct mtpav *mtp_card,
271 struct mtpav_port *portp,
272 struct snd_rawmidi_substream *substream)
273{
274 u8 outbyte;
275
276 // Get the outbyte first, so we can emulate running status if
277 // necessary
278 if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
279 return;
280
281 // send port change command if necessary
282
283 if (portp->hwport != mtp_card->outmidihwport) {
284 mtp_card->outmidihwport = portp->hwport;
285
286 snd_mtpav_send_byte(mtp_card, 0xf5);
287 snd_mtpav_send_byte(mtp_card, portp->hwport);
288 /*
289 snd_printk(KERN_DEBUG "new outport: 0x%x\n",
290 (unsigned int) portp->hwport);
291 */
292 if (!(outbyte & 0x80) && portp->running_status)
293 snd_mtpav_send_byte(mtp_card, portp->running_status);
294 }
295
296 // send data
297
298 do {
299 if (outbyte & 0x80)
300 portp->running_status = outbyte;
301
302 snd_mtpav_send_byte(mtp_card, outbyte);
303 } while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
304}
305
306static void snd_mtpav_output_write(struct snd_rawmidi_substream *substream)
307{
308 struct mtpav *mtp_card = substream->rmidi->private_data;
309 struct mtpav_port *portp = &mtp_card->ports[substream->number];
310 unsigned long flags;
311
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);
315}
316
317
318/*
319 * mtpav control
320 */
321
322static void snd_mtpav_portscan(struct mtpav *chip) // put mtp into smart routing mode
323{
324 u8 p;
325
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);
330 }
331}
332
333/*
334 */
335
336static int snd_mtpav_input_open(struct snd_rawmidi_substream *substream)
337{
338 struct mtpav *mtp_card = substream->rmidi->private_data;
339 struct mtpav_port *portp = &mtp_card->ports[substream->number];
340 unsigned long flags;
341
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);
348 return 0;
349}
350
351/*
352 */
353
354static int snd_mtpav_input_close(struct snd_rawmidi_substream *substream)
355{
356 struct mtpav *mtp_card = substream->rmidi->private_data;
357 struct mtpav_port *portp = &mtp_card->ports[substream->number];
358 unsigned long flags;
359
360 spin_lock_irqsave(&mtp_card->spinlock, flags);
361 portp->mode &= ~MTPAV_MODE_INPUT_OPENED;
362 portp->input = NULL;
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);
366 return 0;
367}
368
369/*
370 */
371
372static void snd_mtpav_input_trigger(struct snd_rawmidi_substream *substream, int up)
373{
374 struct mtpav *mtp_card = substream->rmidi->private_data;
375 struct mtpav_port *portp = &mtp_card->ports[substream->number];
376 unsigned long flags;
377
378 spin_lock_irqsave(&mtp_card->spinlock, flags);
379 if (up)
380 portp->mode |= MTPAV_MODE_INPUT_TRIGGERED;
381 else
382 portp->mode &= ~MTPAV_MODE_INPUT_TRIGGERED;
383 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
384
385}
386
387
388/*
389 * timer interrupt for outputs
390 */
391
392static void snd_mtpav_output_timer(struct timer_list *t)
393{
394 unsigned long flags;
395 struct mtpav *chip = from_timer(chip, t, timer);
396 int p;
397
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);
406 }
407 spin_unlock_irqrestore(&chip->spinlock, flags);
408}
409
410/* spinlock held! */
411static void snd_mtpav_add_output_timer(struct mtpav *chip)
412{
413 mod_timer(&chip->timer, 1 + jiffies);
414}
415
416/* spinlock held! */
417static void snd_mtpav_remove_output_timer(struct mtpav *chip)
418{
419 del_timer(&chip->timer);
420}
421
422/*
423 */
424
425static int snd_mtpav_output_open(struct snd_rawmidi_substream *substream)
426{
427 struct mtpav *mtp_card = substream->rmidi->private_data;
428 struct mtpav_port *portp = &mtp_card->ports[substream->number];
429 unsigned long flags;
430
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);
435 return 0;
436};
437
438/*
439 */
440
441static int snd_mtpav_output_close(struct snd_rawmidi_substream *substream)
442{
443 struct mtpav *mtp_card = substream->rmidi->private_data;
444 struct mtpav_port *portp = &mtp_card->ports[substream->number];
445 unsigned long flags;
446
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);
451 return 0;
452};
453
454/*
455 */
456
457static void snd_mtpav_output_trigger(struct snd_rawmidi_substream *substream, int up)
458{
459 struct mtpav *mtp_card = substream->rmidi->private_data;
460 struct mtpav_port *portp = &mtp_card->ports[substream->number];
461 unsigned long flags;
462
463 spin_lock_irqsave(&mtp_card->spinlock, flags);
464 if (up) {
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;
469 }
470 } else {
471 portp->mode &= ~MTPAV_MODE_OUTPUT_TRIGGERED;
472 if (--mtp_card->istimer == 0)
473 snd_mtpav_remove_output_timer(mtp_card);
474 }
475 spin_unlock_irqrestore(&mtp_card->spinlock, flags);
476
477 if (up)
478 snd_mtpav_output_write(substream);
479}
480
481/*
482 * midi interrupt for inputs
483 */
484
485static void snd_mtpav_inmidi_process(struct mtpav *mcrd, u8 inbyte)
486{
487 struct mtpav_port *portp;
488
489 if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
490 return;
491
492 portp = &mcrd->ports[mcrd->inmidiport];
493 if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED)
494 snd_rawmidi_receive(portp->input, &inbyte, 1);
495}
496
497static void snd_mtpav_inmidi_h(struct mtpav *mcrd, u8 inbyte)
498{
499 if (inbyte >= 0xf8) {
500 /* real-time midi code */
501 snd_mtpav_inmidi_process(mcrd, inbyte);
502 return;
503 }
504
505 if (mcrd->inmidistate == 0) { // awaiting command
506 if (inbyte == 0xf5) // MTP port #
507 mcrd->inmidistate = 1;
508 else
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;
513 }
514}
515
516static void snd_mtpav_read_bytes(struct mtpav *mcrd)
517{
518 u8 clrread, setread;
519 u8 mtp_read_byte;
520 u8 sr, cbyt;
521 int i;
522
523 u8 sbyt = snd_mtpav_getreg(mcrd, SREG);
524
525 /* printk(KERN_DEBUG "snd_mtpav_read_bytes() sbyt: 0x%x\n", sbyt); */
526
527 if (!(sbyt & SIGS_BYTE))
528 return;
529
530 cbyt = snd_mtpav_getreg(mcrd, CREG);
531 clrread = cbyt & (SIGC_READ ^ 0xff);
532 setread = cbyt | SIGC_READ;
533
534 do {
535
536 mtp_read_byte = 0;
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);
541
542 sr &= SIGS_IN0 | SIGS_IN1;
543 sr >>= 4;
544 mtp_read_byte |= sr << (i * 2);
545 }
546
547 snd_mtpav_inmidi_h(mcrd, mtp_read_byte);
548
549 sbyt = snd_mtpav_getreg(mcrd, SREG);
550
551 } while (sbyt & SIGS_BYTE);
552}
553
554static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id)
555{
556 struct mtpav *mcard = dev_id;
557
558 spin_lock(&mcard->spinlock);
559 snd_mtpav_read_bytes(mcard);
560 spin_unlock(&mcard->spinlock);
561 return IRQ_HANDLED;
562}
563
564/*
565 * get ISA resources
566 */
567static int snd_mtpav_get_ISA(struct mtpav *mcard)
568{
569 mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI");
570 if (!mcard->res_port) {
571 snd_printk(KERN_ERR "MTVAP port 0x%lx is busy\n", port);
572 return -EBUSY;
573 }
574 mcard->port = port;
575 if (request_irq(irq, snd_mtpav_irqh, 0, "MOTU MTPAV", mcard)) {
576 snd_printk(KERN_ERR "MTVAP IRQ %d busy\n", irq);
577 return -EBUSY;
578 }
579 mcard->irq = irq;
580 return 0;
581}
582
583
584/*
585 */
586
587static const struct snd_rawmidi_ops snd_mtpav_output = {
588 .open = snd_mtpav_output_open,
589 .close = snd_mtpav_output_close,
590 .trigger = snd_mtpav_output_trigger,
591};
592
593static const struct snd_rawmidi_ops snd_mtpav_input = {
594 .open = snd_mtpav_input_open,
595 .close = snd_mtpav_input_close,
596 .trigger = snd_mtpav_input_trigger,
597};
598
599
600/*
601 * get RAWMIDI resources
602 */
603
604static void snd_mtpav_set_name(struct mtpav *chip,
605 struct snd_rawmidi_substream *substream)
606{
607 if (substream->number >= 0 && substream->number < chip->num_ports)
608 sprintf(substream->name, "MTP direct %d", (substream->number % chip->num_ports) + 1);
609 else if (substream->number >= 8 && substream->number < chip->num_ports * 2)
610 sprintf(substream->name, "MTP remote %d", (substream->number % chip->num_ports) + 1);
611 else if (substream->number == chip->num_ports * 2)
612 strcpy(substream->name, "MTP computer");
613 else if (substream->number == chip->num_ports * 2 + 1)
614 strcpy(substream->name, "MTP ADAT");
615 else
616 strcpy(substream->name, "MTP broadcast");
617}
618
619static int snd_mtpav_get_RAWMIDI(struct mtpav *mcard)
620{
621 int rval;
622 struct snd_rawmidi *rawmidi;
623 struct snd_rawmidi_substream *substream;
624 struct list_head *list;
625
626 if (hwports < 1)
627 hwports = 1;
628 else if (hwports > 8)
629 hwports = 8;
630 mcard->num_ports = hwports;
631
632 rval = snd_rawmidi_new(mcard->card, "MotuMIDI", 0,
633 mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
634 mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
635 &mcard->rmidi);
636 if (rval < 0)
637 return rval;
638 rawmidi = mcard->rmidi;
639 rawmidi->private_data = mcard;
640
641 list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
642 substream = list_entry(list, struct snd_rawmidi_substream, list);
643 snd_mtpav_set_name(mcard, substream);
644 substream->ops = &snd_mtpav_input;
645 }
646 list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
647 substream = list_entry(list, struct snd_rawmidi_substream, list);
648 snd_mtpav_set_name(mcard, substream);
649 substream->ops = &snd_mtpav_output;
650 mcard->ports[substream->number].hwport = translate_subdevice_to_hwport(mcard, substream->number);
651 }
652 rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
653 SNDRV_RAWMIDI_INFO_DUPLEX;
654 sprintf(rawmidi->name, "MTP AV MIDI");
655 return 0;
656}
657
658/*
659 */
660
661static void snd_mtpav_free(struct snd_card *card)
662{
663 struct mtpav *crd = card->private_data;
664 unsigned long flags;
665
666 spin_lock_irqsave(&crd->spinlock, flags);
667 if (crd->istimer > 0)
668 snd_mtpav_remove_output_timer(crd);
669 spin_unlock_irqrestore(&crd->spinlock, flags);
670 if (crd->irq >= 0)
671 free_irq(crd->irq, (void *)crd);
672 release_and_free_resource(crd->res_port);
673}
674
675/*
676 */
677static int snd_mtpav_probe(struct platform_device *dev)
678{
679 struct snd_card *card;
680 int err;
681 struct mtpav *mtp_card;
682
683 err = snd_card_new(&dev->dev, index, id, THIS_MODULE,
684 sizeof(*mtp_card), &card);
685 if (err < 0)
686 return err;
687
688 mtp_card = card->private_data;
689 spin_lock_init(&mtp_card->spinlock);
690 mtp_card->card = card;
691 mtp_card->irq = -1;
692 mtp_card->share_irq = 0;
693 mtp_card->inmidistate = 0;
694 mtp_card->outmidihwport = 0xffffffff;
695 timer_setup(&mtp_card->timer, snd_mtpav_output_timer, 0);
696
697 card->private_free = snd_mtpav_free;
698
699 err = snd_mtpav_get_RAWMIDI(mtp_card);
700 if (err < 0)
701 goto __error;
702
703 mtp_card->inmidiport = mtp_card->num_ports + MTPAV_PIDX_BROADCAST;
704
705 err = snd_mtpav_get_ISA(mtp_card);
706 if (err < 0)
707 goto __error;
708
709 strcpy(card->driver, "MTPAV");
710 strcpy(card->shortname, "MTPAV on parallel port");
711 snprintf(card->longname, sizeof(card->longname),
712 "MTPAV on parallel port at 0x%lx", port);
713
714 snd_mtpav_portscan(mtp_card);
715
716 err = snd_card_register(mtp_card->card);
717 if (err < 0)
718 goto __error;
719
720 platform_set_drvdata(dev, card);
721 printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
722 return 0;
723
724 __error:
725 snd_card_free(card);
726 return err;
727}
728
729static int snd_mtpav_remove(struct platform_device *devptr)
730{
731 snd_card_free(platform_get_drvdata(devptr));
732 return 0;
733}
734
735#define SND_MTPAV_DRIVER "snd_mtpav"
736
737static struct platform_driver snd_mtpav_driver = {
738 .probe = snd_mtpav_probe,
739 .remove = snd_mtpav_remove,
740 .driver = {
741 .name = SND_MTPAV_DRIVER,
742 },
743};
744
745static int __init alsa_card_mtpav_init(void)
746{
747 int err;
748
749 err = platform_driver_register(&snd_mtpav_driver);
750 if (err < 0)
751 return err;
752
753 device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
754 if (!IS_ERR(device)) {
755 if (platform_get_drvdata(device))
756 return 0;
757 platform_device_unregister(device);
758 err = -ENODEV;
759 } else
760 err = PTR_ERR(device);
761 platform_driver_unregister(&snd_mtpav_driver);
762 return err;
763}
764
765static void __exit alsa_card_mtpav_exit(void)
766{
767 platform_device_unregister(device);
768 platform_driver_unregister(&snd_mtpav_driver);
769}
770
771module_init(alsa_card_mtpav_init)
772module_exit(alsa_card_mtpav_exit)
1/*
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
6 *
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.
11 *
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.
16 *
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
20 *
21 *
22 * This driver is for the 'Mark Of The Unicorn' (MOTU)
23 * MidiTimePiece AV multiport MIDI interface
24 *
25 * IOPORTS
26 * -------
27 * 8 MIDI Ins and 8 MIDI outs
28 * Video Sync In (BNC), Word Sync Out (BNC),
29 * ADAT Sync Out (DB9)
30 * SMPTE in/out (1/4")
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 )
35 *
36 * MISC FEATURES
37 * -------------
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
41 *
42 *
43 * ChangeLog
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.
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/ioport.h>
58#include <linux/io.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/*
66 * globals
67 */
68MODULE_AUTHOR("Michael T. Mayers");
69MODULE_DESCRIPTION("MOTU MidiTimePiece AV multiport MIDI");
70MODULE_LICENSE("GPL");
71MODULE_SUPPORTED_DEVICE("{{MOTU,MidiTimePiece AV multiport MIDI}}");
72
73// io resources
74#define MTPAV_IOBASE 0x378
75#define MTPAV_IRQ 7
76#define MTPAV_MAX_PORTS 8
77
78static int index = SNDRV_DEFAULT_IDX1;
79static char *id = SNDRV_DEFAULT_STR1;
80static long port = MTPAV_IOBASE; /* 0x378, 0x278 */
81static int irq = MTPAV_IRQ; /* 7, 5 */
82static int hwports = MTPAV_MAX_PORTS; /* use hardware ports 1-8 */
83
84module_param(index, int, 0444);
85MODULE_PARM_DESC(index, "Index value for MotuMTPAV MIDI.");
86module_param(id, charp, 0444);
87MODULE_PARM_DESC(id, "ID string for MotuMTPAV MIDI.");
88module_param(port, long, 0444);
89MODULE_PARM_DESC(port, "Parallel port # for MotuMTPAV MIDI.");
90module_param(irq, int, 0444);
91MODULE_PARM_DESC(irq, "Parallel IRQ # for MotuMTPAV MIDI.");
92module_param(hwports, int, 0444);
93MODULE_PARM_DESC(hwports, "Hardware ports # for MotuMTPAV MIDI.");
94
95static struct platform_device *device;
96
97/*
98 * defines
99 */
100//#define USE_FAKE_MTP // don't actually read/write to MTP device (for debugging without an actual unit) (does not work yet)
101
102// parallel port usage masks
103#define SIGS_BYTE 0x08
104#define SIGS_RFD 0x80
105#define SIGS_IRQ 0x40
106#define SIGS_IN0 0x10
107#define SIGS_IN1 0x20
108
109#define SIGC_WRITE 0x04
110#define SIGC_READ 0x08
111#define SIGC_INTEN 0x10
112
113#define DREG 0
114#define SREG 1
115#define CREG 2
116
117//
118#define MTPAV_MODE_INPUT_OPENED 0x01
119#define MTPAV_MODE_OUTPUT_OPENED 0x02
120#define MTPAV_MODE_INPUT_TRIGGERED 0x04
121#define MTPAV_MODE_OUTPUT_TRIGGERED 0x08
122
123#define NUMPORTS (0x12+1)
124
125
126/*
127 */
128
129struct mtpav_port {
130 u8 number;
131 u8 hwport;
132 u8 mode;
133 u8 running_status;
134 struct snd_rawmidi_substream *input;
135 struct snd_rawmidi_substream *output;
136};
137
138struct mtpav {
139 struct snd_card *card;
140 unsigned long port;
141 struct resource *res_port;
142 int irq; /* interrupt (for inputs) */
143 spinlock_t spinlock;
144 int share_irq; /* number of accesses to input interrupts */
145 int istimer; /* number of accesses to timer interrupts */
146 struct timer_list timer; /* timer interrupts for outputs */
147 struct snd_rawmidi *rmidi;
148 int num_ports; /* number of hw ports (1-8) */
149 struct mtpav_port ports[NUMPORTS]; /* all ports including computer, adat and bc */
150
151 u32 inmidiport; /* selected input midi port */
152 u32 inmidistate; /* during midi command 0xf5 */
153
154 u32 outmidihwport; /* selected output midi hw port */
155};
156
157
158/*
159 * possible hardware ports (selected by 0xf5 port message)
160 * 0x00 all ports
161 * 0x01 .. 0x08 this MTP's ports 1..8
162 * 0x09 .. 0x10 networked MTP's ports (9..16)
163 * 0x11 networked MTP's computer port
164 * 0x63 to ADAT
165 *
166 * mappig:
167 * subdevice 0 - (X-1) ports
168 * X - (2*X-1) networked ports
169 * X computer
170 * X+1 ADAT
171 * X+2 all ports
172 *
173 * where X = chip->num_ports
174 */
175
176#define MTPAV_PIDX_COMPUTER 0
177#define MTPAV_PIDX_ADAT 1
178#define MTPAV_PIDX_BROADCAST 2
179
180
181static int translate_subdevice_to_hwport(struct mtpav *chip, int subdev)
182{
183 if (subdev < 0)
184 return 0x01; /* invalid - use port 0 as default */
185 else if (subdev < chip->num_ports)
186 return subdev + 1; /* single mtp port */
187 else if (subdev < chip->num_ports * 2)
188 return subdev - chip->num_ports + 0x09; /* remote port */
189 else if (subdev == chip->num_ports * 2 + MTPAV_PIDX_COMPUTER)
190 return 0x11; /* computer port */
191 else if (subdev == chip->num_ports + MTPAV_PIDX_ADAT)
192 return 0x63; /* ADAT */
193 return 0; /* all ports */
194}
195
196static int translate_hwport_to_subdevice(struct mtpav *chip, int hwport)
197{
198 int p;
199 if (hwport <= 0x00) /* all ports */
200 return chip->num_ports + MTPAV_PIDX_BROADCAST;
201 else if (hwport <= 0x08) { /* single port */
202 p = hwport - 1;
203 if (p >= chip->num_ports)
204 p = 0;
205 return p;
206 } else if (hwport <= 0x10) { /* remote port */
207 p = hwport - 0x09 + chip->num_ports;
208 if (p >= chip->num_ports * 2)
209 p = chip->num_ports;
210 return p;
211 } else if (hwport == 0x11) /* computer port */
212 return chip->num_ports + MTPAV_PIDX_COMPUTER;
213 else /* ADAT */
214 return chip->num_ports + MTPAV_PIDX_ADAT;
215}
216
217
218/*
219 */
220
221static u8 snd_mtpav_getreg(struct mtpav *chip, u16 reg)
222{
223 u8 rval = 0;
224
225 if (reg == SREG) {
226 rval = inb(chip->port + SREG);
227 rval = (rval & 0xf8);
228 } else if (reg == CREG) {
229 rval = inb(chip->port + CREG);
230 rval = (rval & 0x1c);
231 }
232
233 return rval;
234}
235
236/*
237 */
238
239static inline void snd_mtpav_mputreg(struct mtpav *chip, u16 reg, u8 val)
240{
241 if (reg == DREG || reg == CREG)
242 outb(val, chip->port + reg);
243}
244
245/*
246 */
247
248static void snd_mtpav_wait_rfdhi(struct mtpav *chip)
249{
250 int counts = 10000;
251 u8 sbyte;
252
253 sbyte = snd_mtpav_getreg(chip, SREG);
254 while (!(sbyte & SIGS_RFD) && counts--) {
255 sbyte = snd_mtpav_getreg(chip, SREG);
256 udelay(10);
257 }
258}
259
260static void snd_mtpav_send_byte(struct mtpav *chip, u8 byte)
261{
262 u8 tcbyt;
263 u8 clrwrite;
264 u8 setwrite;
265
266 snd_mtpav_wait_rfdhi(chip);
267
268 /////////////////
269
270 tcbyt = snd_mtpav_getreg(chip, CREG);
271 clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
272 setwrite = tcbyt | SIGC_WRITE;
273
274 snd_mtpav_mputreg(chip, DREG, byte);
275 snd_mtpav_mputreg(chip, CREG, clrwrite); // clear write bit
276
277 snd_mtpav_mputreg(chip, CREG, setwrite); // set write bit
278
279}
280
281
282/*
283 */
284
285/* call this with spin lock held */
286static void snd_mtpav_output_port_write(struct mtpav *mtp_card,
287 struct mtpav_port *portp,
288 struct snd_rawmidi_substream *substream)
289{
290 u8 outbyte;
291
292 // Get the outbyte first, so we can emulate running status if
293 // necessary
294 if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
295 return;
296
297 // send port change command if necessary
298
299 if (portp->hwport != mtp_card->outmidihwport) {
300 mtp_card->outmidihwport = portp->hwport;
301
302 snd_mtpav_send_byte(mtp_card, 0xf5);
303 snd_mtpav_send_byte(mtp_card, portp->hwport);
304 /*
305 snd_printk(KERN_DEBUG "new outport: 0x%x\n",
306 (unsigned int) portp->hwport);
307 */
308 if (!(outbyte & 0x80) && portp->running_status)
309 snd_mtpav_send_byte(mtp_card, portp->running_status);
310 }
311
312 // send data
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 * mtpav control
336 */
337
338static void snd_mtpav_portscan(struct mtpav *chip) // put mtp into smart routing mode
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)); // enable pport interrupts
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); // disable pport interrupts
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 * timer interrupt for outputs
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 /* 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);
423 }
424 spin_unlock_irqrestore(&chip->spinlock, flags);
425}
426
427/* spinlock held! */
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/* spinlock held! */
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 * midi interrupt for inputs
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 /* real-time midi code */
519 snd_mtpav_inmidi_process(mcrd, inbyte);
520 return;
521 }
522
523 if (mcrd->inmidistate == 0) { // awaiting command
524 if (inbyte == 0xf5) // MTP port #
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 /* printk(KERN_DEBUG "snd_mtpav_read_bytes() sbyt: 0x%x\n", sbyt); */
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 * get ISA resources
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(KERN_ERR "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(KERN_ERR "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 * get RAWMIDI resources
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 err = snd_card_create(index, id, THIS_MODULE, sizeof(*mtp_card), &card);
700 if (err < 0)
701 return err;
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->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;
714
715 card->private_free = snd_mtpav_free;
716
717 err = snd_mtpav_get_RAWMIDI(mtp_card);
718 if (err < 0)
719 goto __error;
720
721 mtp_card->inmidiport = mtp_card->num_ports + MTPAV_PIDX_BROADCAST;
722
723 err = snd_mtpav_get_ISA(mtp_card);
724 if (err < 0)
725 goto __error;
726
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);
731
732 snd_mtpav_portscan(mtp_card);
733
734 snd_card_set_dev(card, &dev->dev);
735 err = snd_card_register(mtp_card->card);
736 if (err < 0)
737 goto __error;
738
739 platform_set_drvdata(dev, card);
740 printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
741 return 0;
742
743 __error:
744 snd_card_free(card);
745 return err;
746}
747
748static int __devexit snd_mtpav_remove(struct platform_device *devptr)
749{
750 snd_card_free(platform_get_drvdata(devptr));
751 platform_set_drvdata(devptr, NULL);
752 return 0;
753}
754
755#define SND_MTPAV_DRIVER "snd_mtpav"
756
757static struct platform_driver snd_mtpav_driver = {
758 .probe = snd_mtpav_probe,
759 .remove = __devexit_p(snd_mtpav_remove),
760 .driver = {
761 .name = SND_MTPAV_DRIVER
762 },
763};
764
765static int __init alsa_card_mtpav_init(void)
766{
767 int err;
768
769 if ((err = platform_driver_register(&snd_mtpav_driver)) < 0)
770 return err;
771
772 device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
773 if (!IS_ERR(device)) {
774 if (platform_get_drvdata(device))
775 return 0;
776 platform_device_unregister(device);
777 err = -ENODEV;
778 } else
779 err = PTR_ERR(device);
780 platform_driver_unregister(&snd_mtpav_driver);
781 return err;
782}
783
784static void __exit alsa_card_mtpav_exit(void)
785{
786 platform_device_unregister(device);
787 platform_driver_unregister(&snd_mtpav_driver);
788}
789
790module_init(alsa_card_mtpav_init)
791module_exit(alsa_card_mtpav_exit)