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