added mtd driver
[linux-2.4.git] / drivers / sound / mpu401.c
1 /*
2  * sound/mpu401.c
3  *
4  * The low level driver for Roland MPU-401 compatible Midi cards.
5  */
6 /*
7  * Copyright (C) by Hannu Savolainen 1993-1997
8  *
9  * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10  * Version 2 (June 1991). See the "COPYING" file distributed with this software
11  * for more info.
12  *
13  *
14  * Thomas Sailer        ioctl code reworked (vmalloc/vfree removed)
15  * Alan Cox             modularisation, use normal request_irq, use dev_id
16  * Bartlomiej Zolnierkiewicz    removed some __init to allow using many drivers
17  * Chris Rankin         Update the module-usage counter for the coprocessor
18  * Zwane Mwaikambo      Changed attach/unload resource freeing
19  */
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23
24 #define USE_SEQ_MACROS
25 #define USE_SIMPLE_MACROS
26
27 #include "sound_config.h"
28
29 #include "coproc.h"
30 #include "mpu401.h"
31
32 static int      timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
33
34 struct mpu_config
35 {
36         int             base;   /*
37                                  * I/O base
38                                  */
39         int             irq;
40         int             opened; /*
41                                  * Open mode
42                                  */
43         int             devno;
44         int             synthno;
45         int             uart_mode;
46         int             initialized;
47         int             mode;
48 #define MODE_MIDI       1
49 #define MODE_SYNTH      2
50         unsigned char   version, revision;
51         unsigned int    capabilities;
52 #define MPU_CAP_INTLG   0x10000000
53 #define MPU_CAP_SYNC    0x00000010
54 #define MPU_CAP_FSK     0x00000020
55 #define MPU_CAP_CLS     0x00000040
56 #define MPU_CAP_SMPTE   0x00000080
57 #define MPU_CAP_2PORT   0x00000001
58         int             timer_flag;
59
60 #define MBUF_MAX        10
61 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
62         {printk( "MPU: Invalid buffer pointer %d/%d, s=%d\n",  dc->m_ptr,  dc->m_left,  dc->m_state);dc->m_ptr--;}
63           int             m_busy;
64           unsigned char   m_buf[MBUF_MAX];
65           int             m_ptr;
66           int             m_state;
67           int             m_left;
68           unsigned char   last_status;
69           void            (*inputintr) (int dev, unsigned char data);
70           int             shared_irq;
71           int            *osp;
72   };
73
74 #define DATAPORT(base)   (base)
75 #define COMDPORT(base)   (base+1)
76 #define STATPORT(base)   (base+1)
77
78
79 static void mpu401_close(int dev);
80
81 static inline int mpu401_status(struct mpu_config *devc)
82 {
83         return inb(STATPORT(devc->base));
84 }
85
86 #define input_avail(devc)               (!(mpu401_status(devc)&INPUT_AVAIL))
87 #define output_ready(devc)              (!(mpu401_status(devc)&OUTPUT_READY))
88
89 static inline void write_command(struct mpu_config *devc, unsigned char cmd)
90 {
91         outb(cmd, COMDPORT(devc->base));
92 }
93
94 static inline int read_data(struct mpu_config *devc)
95 {
96         return inb(DATAPORT(devc->base));
97 }
98
99 static inline void write_data(struct mpu_config *devc, unsigned char byte)
100 {
101         outb(byte, DATAPORT(devc->base));
102 }
103
104 #define OUTPUT_READY    0x40
105 #define INPUT_AVAIL     0x80
106 #define MPU_ACK         0xFE
107 #define MPU_RESET       0xFF
108 #define UART_MODE_ON    0x3F
109
110 static struct mpu_config dev_conf[MAX_MIDI_DEV] =
111 {
112         {0}
113 };
114
115 static int n_mpu_devs = 0;
116
117 static int reset_mpu401(struct mpu_config *devc);
118 static void set_uart_mode(int dev, struct mpu_config *devc, int arg);
119
120 static int mpu_timer_init(int midi_dev);
121 static void mpu_timer_interrupt(void);
122 static void timer_ext_event(struct mpu_config *devc, int event, int parm);
123
124 static struct synth_info mpu_synth_info_proto = {
125         "MPU-401 MIDI interface", 
126         0, 
127         SYNTH_TYPE_MIDI, 
128         MIDI_TYPE_MPU401, 
129         0, 128, 
130         0, 128, 
131         SYNTH_CAP_INPUT
132 };
133
134 static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
135
136 /*
137  * States for the input scanner
138  */
139
140 #define ST_INIT                 0       /* Ready for timing byte or msg */
141 #define ST_TIMED                1       /* Leading timing byte rcvd */
142 #define ST_DATABYTE             2       /* Waiting for (nr_left) data bytes */
143
144 #define ST_SYSMSG               100     /* System message (sysx etc). */
145 #define ST_SYSEX                101     /* System exclusive msg */
146 #define ST_MTC                  102     /* Midi Time Code (MTC) qframe msg */
147 #define ST_SONGSEL              103     /* Song select */
148 #define ST_SONGPOS              104     /* Song position pointer */
149
150 static unsigned char len_tab[] =        /* # of data bytes following a status
151                                          */
152 {
153         2,                      /* 8x */
154         2,                      /* 9x */
155         2,                      /* Ax */
156         2,                      /* Bx */
157         1,                      /* Cx */
158         1,                      /* Dx */
159         2,                      /* Ex */
160         0                       /* Fx */
161 };
162
163 #define STORE(cmd) \
164 { \
165         int len; \
166         unsigned char obuf[8]; \
167         cmd; \
168         seq_input_event(obuf, len); \
169 }
170
171 #define _seqbuf obuf
172 #define _seqbufptr 0
173 #define _SEQ_ADVBUF(x) len=x
174
175 static int mpu_input_scanner(struct mpu_config *devc, unsigned char midic)
176 {
177
178         switch (devc->m_state)
179         {
180                 case ST_INIT:
181                         switch (midic)
182                         {
183                                 case 0xf8:
184                                 /* Timer overflow */
185                                         break;
186
187                                 case 0xfc:
188                                         printk("<all end>");
189                                         break;
190
191                                 case 0xfd:
192                                         if (devc->timer_flag)
193                                                 mpu_timer_interrupt();
194                                         break;
195
196                                 case 0xfe:
197                                         return MPU_ACK;
198
199                                 case 0xf0:
200                                 case 0xf1:
201                                 case 0xf2:
202                                 case 0xf3:
203                                 case 0xf4:
204                                 case 0xf5:
205                                 case 0xf6:
206                                 case 0xf7:
207                                         printk("<Trk data rq #%d>", midic & 0x0f);
208                                         break;
209
210                                 case 0xf9:
211                                         printk("<conductor rq>");
212                                         break;
213
214                                 case 0xff:
215                                         devc->m_state = ST_SYSMSG;
216                                         break;
217
218                                 default:
219                                         if (midic <= 0xef)
220                                         {
221                                                 /* printk( "mpu time: %d ",  midic); */
222                                                 devc->m_state = ST_TIMED;
223                                         }
224                                         else
225                                                 printk("<MPU: Unknown event %02x> ", midic);
226                         }
227                         break;
228
229                 case ST_TIMED:
230                         {
231                                 int msg = ((int) (midic & 0xf0) >> 4);
232
233                                 devc->m_state = ST_DATABYTE;
234
235                                 if (msg < 8)    /* Data byte */
236                                 {
237                                         /* printk( "midi msg (running status) "); */
238                                         msg = ((int) (devc->last_status & 0xf0) >> 4);
239                                         msg -= 8;
240                                         devc->m_left = len_tab[msg] - 1;
241
242                                         devc->m_ptr = 2;
243                                         devc->m_buf[0] = devc->last_status;
244                                         devc->m_buf[1] = midic;
245
246                                         if (devc->m_left <= 0)
247                                         {
248                                                 devc->m_state = ST_INIT;
249                                                 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
250                                                 devc->m_ptr = 0;
251                                         }
252                                 }
253                                 else if (msg == 0xf)    /* MPU MARK */
254                                 {
255                                         devc->m_state = ST_INIT;
256
257                                         switch (midic)
258                                         {
259                                                 case 0xf8:
260                                                         /* printk( "NOP "); */
261                                                         break;
262
263                                                 case 0xf9:
264                                                         /* printk( "meas end "); */
265                                                         break;
266
267                                                 case 0xfc:
268                                                         /* printk( "data end "); */
269                                                         break;
270
271                                                 default:
272                                                         printk("Unknown MPU mark %02x\n", midic);
273                                         }
274                                 }
275                                 else
276                                 {
277                                         devc->last_status = midic;
278                                         /* printk( "midi msg "); */
279                                         msg -= 8;
280                                         devc->m_left = len_tab[msg];
281
282                                         devc->m_ptr = 1;
283                                         devc->m_buf[0] = midic;
284
285                                         if (devc->m_left <= 0)
286                                         {
287                                                 devc->m_state = ST_INIT;
288                                                 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
289                                                 devc->m_ptr = 0;
290                                         }
291                                 }
292                         }
293                         break;
294
295                 case ST_SYSMSG:
296                         switch (midic)
297                         {
298                                 case 0xf0:
299                                         printk("<SYX>");
300                                         devc->m_state = ST_SYSEX;
301                                         break;
302
303                                 case 0xf1:
304                                         devc->m_state = ST_MTC;
305                                         break;
306
307                                 case 0xf2:
308                                         devc->m_state = ST_SONGPOS;
309                                         devc->m_ptr = 0;
310                                         break;
311
312                                 case 0xf3:
313                                         devc->m_state = ST_SONGSEL;
314                                         break;
315
316                                 case 0xf6:
317                                         /* printk( "tune_request\n"); */
318                                         devc->m_state = ST_INIT;
319
320                                         /*
321                                          *    Real time messages
322                                          */
323                                 case 0xf8:
324                                         /* midi clock */
325                                         devc->m_state = ST_INIT;
326                                         timer_ext_event(devc, TMR_CLOCK, 0);
327                                         break;
328
329                                 case 0xfA:
330                                         devc->m_state = ST_INIT;
331                                         timer_ext_event(devc, TMR_START, 0);
332                                         break;
333
334                                 case 0xFB:
335                                         devc->m_state = ST_INIT;
336                                         timer_ext_event(devc, TMR_CONTINUE, 0);
337                                         break;
338
339                                 case 0xFC:
340                                         devc->m_state = ST_INIT;
341                                         timer_ext_event(devc, TMR_STOP, 0);
342                                         break;
343
344                                 case 0xFE:
345                                         /* active sensing */
346                                         devc->m_state = ST_INIT;
347                                         break;
348
349                                 case 0xff:
350                                         /* printk( "midi hard reset"); */
351                                         devc->m_state = ST_INIT;
352                                         break;
353
354                                 default:
355                                         printk("unknown MIDI sysmsg %0x\n", midic);
356                                         devc->m_state = ST_INIT;
357                         }
358                         break;
359
360                 case ST_MTC:
361                         devc->m_state = ST_INIT;
362                         printk("MTC frame %x02\n", midic);
363                         break;
364
365                 case ST_SYSEX:
366                         if (midic == 0xf7)
367                         {
368                                 printk("<EOX>");
369                                 devc->m_state = ST_INIT;
370                         }
371                         else
372                                 printk("%02x ", midic);
373                         break;
374
375                 case ST_SONGPOS:
376                         BUFTEST(devc);
377                         devc->m_buf[devc->m_ptr++] = midic;
378                         if (devc->m_ptr == 2)
379                         {
380                                 devc->m_state = ST_INIT;
381                                 devc->m_ptr = 0;
382                                 timer_ext_event(devc, TMR_SPP,
383                                         ((devc->m_buf[1] & 0x7f) << 7) |
384                                         (devc->m_buf[0] & 0x7f));
385                         }
386                         break;
387
388                 case ST_DATABYTE:
389                         BUFTEST(devc);
390                         devc->m_buf[devc->m_ptr++] = midic;
391                         if ((--devc->m_left) <= 0)
392                         {
393                                 devc->m_state = ST_INIT;
394                                 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
395                                 devc->m_ptr = 0;
396                         }
397                         break;
398
399                 default:
400                         printk("Bad state %d ", devc->m_state);
401                         devc->m_state = ST_INIT;
402         }
403         return 1;
404 }
405
406 static void mpu401_input_loop(struct mpu_config *devc)
407 {
408         unsigned long flags;
409         int busy;
410         int n;
411
412         save_flags(flags);
413         cli();
414         busy = devc->m_busy;
415         devc->m_busy = 1;
416         restore_flags(flags);
417
418         if (busy)               /* Already inside the scanner */
419                 return;
420
421         n = 50;
422
423         while (input_avail(devc) && n-- > 0)
424         {
425                 unsigned char c = read_data(devc);
426
427                 if (devc->mode == MODE_SYNTH)
428                 {
429                         mpu_input_scanner(devc, c);
430                 }
431                 else if (devc->opened & OPEN_READ && devc->inputintr != NULL)
432                         devc->inputintr(devc->devno, c);
433         }
434         devc->m_busy = 0;
435 }
436
437 int intchk_mpu401(void *dev_id)
438 {
439         struct mpu_config *devc;
440         int dev = (int) dev_id;
441
442         devc = &dev_conf[dev];
443         return input_avail(devc);
444 }
445
446 void mpuintr(int irq, void *dev_id, struct pt_regs *dummy)
447 {
448         struct mpu_config *devc;
449         int dev = (int) dev_id;
450
451         sti();
452         devc = &dev_conf[dev];
453
454         if (input_avail(devc))
455         {
456                 if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
457                         mpu401_input_loop(devc);
458                 else
459                 {
460                         /* Dummy read (just to acknowledge the interrupt) */
461                         read_data(devc);
462                 }
463         }
464 }
465
466 static int mpu401_open(int dev, int mode,
467             void            (*input) (int dev, unsigned char data),
468             void            (*output) (int dev)
469 )
470 {
471         int err;
472         struct mpu_config *devc;
473         struct coproc_operations *coprocessor;
474
475         if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
476                 return -ENXIO;
477
478         devc = &dev_conf[dev];
479
480         if (devc->opened)
481                   return -EBUSY;
482         /*
483          *  Verify that the device is really running.
484          *  Some devices (such as Ensoniq SoundScape don't
485          *  work before the on board processor (OBP) is initialized
486          *  by downloading its microcode.
487          */
488
489         if (!devc->initialized)
490         {
491                 if (mpu401_status(devc) == 0xff)        /* Bus float */
492                 {
493                         printk(KERN_ERR "mpu401: Device not initialized properly\n");
494                         return -EIO;
495                 }
496                 reset_mpu401(devc);
497         }
498
499         if ( (coprocessor = midi_devs[dev]->coproc) != NULL )
500         {
501                 if (coprocessor->owner)
502                         __MOD_INC_USE_COUNT(coprocessor->owner);
503
504                 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
505                 {
506                         printk(KERN_WARNING "MPU-401: Can't access coprocessor device\n");
507                         mpu401_close(dev);
508                         return err;
509                 }
510         }
511         
512         set_uart_mode(dev, devc, 1);
513         devc->mode = MODE_MIDI;
514         devc->synthno = 0;
515
516         mpu401_input_loop(devc);
517
518         devc->inputintr = input;
519         devc->opened = mode;
520
521         return 0;
522 }
523
524 static void mpu401_close(int dev)
525 {
526         struct mpu_config *devc;
527         struct coproc_operations *coprocessor;
528
529         devc = &dev_conf[dev];
530         if (devc->uart_mode)
531                 reset_mpu401(devc);     /*
532                                          * This disables the UART mode
533                                          */
534         devc->mode = 0;
535         devc->inputintr = NULL;
536
537         coprocessor = midi_devs[dev]->coproc;
538         if (coprocessor) {
539                 coprocessor->close(coprocessor->devc, COPR_MIDI);
540
541                 if (coprocessor->owner)
542                         __MOD_DEC_USE_COUNT(coprocessor->owner);
543         }
544         devc->opened = 0;
545 }
546
547 static int mpu401_out(int dev, unsigned char midi_byte)
548 {
549         int timeout;
550         unsigned long flags;
551
552         struct mpu_config *devc;
553
554         devc = &dev_conf[dev];
555
556         /*
557          * Sometimes it takes about 30000 loops before the output becomes ready
558          * (After reset). Normally it takes just about 10 loops.
559          */
560
561         for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
562
563         save_flags(flags);
564         cli();
565         if (!output_ready(devc))
566         {
567                 printk(KERN_WARNING "mpu401: Send data timeout\n");
568                 restore_flags(flags);
569                 return 0;
570         }
571         write_data(devc, midi_byte);
572         restore_flags(flags);
573         return 1;
574 }
575
576 static int mpu401_command(int dev, mpu_command_rec * cmd)
577 {
578         int i, timeout, ok;
579         int ret = 0;
580         unsigned long   flags;
581         struct mpu_config *devc;
582
583         devc = &dev_conf[dev];
584
585         if (devc->uart_mode)    /*
586                                  * Not possible in UART mode
587                                  */
588         {
589                 printk(KERN_WARNING "mpu401: commands not possible in the UART mode\n");
590                 return -EINVAL;
591         }
592         /*
593          * Test for input since pending input seems to block the output.
594          */
595         if (input_avail(devc))
596                 mpu401_input_loop(devc);
597
598         /*
599          * Sometimes it takes about 50000 loops before the output becomes ready
600          * (After reset). Normally it takes just about 10 loops.
601          */
602
603         timeout = 50000;
604 retry:
605         if (timeout-- <= 0)
606         {
607                 printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
608                 return -EIO;
609         }
610         save_flags(flags);
611         cli();
612
613         if (!output_ready(devc))
614         {
615                   restore_flags(flags);
616                   goto retry;
617         }
618         write_command(devc, cmd->cmd);
619
620         ok = 0;
621         for (timeout = 50000; timeout > 0 && !ok; timeout--)
622         {
623                 if (input_avail(devc))
624                 {
625                         if (devc->opened && devc->mode == MODE_SYNTH)
626                         {
627                                 if (mpu_input_scanner(devc, read_data(devc)) == MPU_ACK)
628                                         ok = 1;
629                         }
630                         else
631                         {
632                                 /* Device is not currently open. Use simpler method */
633                                 if (read_data(devc) == MPU_ACK)
634                                         ok = 1;
635                         }
636                 }
637         }
638         if (!ok)
639         {
640                 restore_flags(flags);
641                 return -EIO;
642         }
643         if (cmd->nr_args)
644         {
645                 for (i = 0; i < cmd->nr_args; i++)
646                 {
647                         for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
648
649                         if (!mpu401_out(dev, cmd->data[i]))
650                         {
651                                 restore_flags(flags);
652                                 printk(KERN_WARNING "mpu401: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
653                                 return -EIO;
654                         }
655                 }
656         }
657         ret = 0;
658         cmd->data[0] = 0;
659
660         if (cmd->nr_returns)
661         {
662                 for (i = 0; i < cmd->nr_returns; i++)
663                 {
664                         ok = 0;
665                         for (timeout = 5000; timeout > 0 && !ok; timeout--)
666                                 if (input_avail(devc))
667                                 {
668                                         cmd->data[i] = read_data(devc);
669                                         ok = 1;
670                                 }
671                         if (!ok)
672                         {
673                                 restore_flags(flags);
674                                 return -EIO;
675                         }
676                 }
677         }
678         restore_flags(flags);
679         return ret;
680 }
681
682 static int mpu_cmd(int dev, int cmd, int data)
683 {
684         int ret;
685
686         static mpu_command_rec rec;
687
688         rec.cmd = cmd & 0xff;
689         rec.nr_args = ((cmd & 0xf0) == 0xE0);
690         rec.nr_returns = ((cmd & 0xf0) == 0xA0);
691         rec.data[0] = data & 0xff;
692
693         if ((ret = mpu401_command(dev, &rec)) < 0)
694                 return ret;
695         return (unsigned char) rec.data[0];
696 }
697
698 static int mpu401_prefix_cmd(int dev, unsigned char status)
699 {
700         struct mpu_config *devc = &dev_conf[dev];
701
702         if (devc->uart_mode)
703                 return 1;
704
705         if (status < 0xf0)
706         {
707                 if (mpu_cmd(dev, 0xD0, 0) < 0)
708                         return 0;
709                 return 1;
710         }
711         switch (status)
712         {
713                 case 0xF0:
714                         if (mpu_cmd(dev, 0xDF, 0) < 0)
715                                 return 0;
716                         return 1;
717
718                 default:
719                         return 0;
720         }
721 }
722
723 static int mpu401_start_read(int dev)
724 {
725         return 0;
726 }
727
728 static int mpu401_end_read(int dev)
729 {
730         return 0;
731 }
732
733 static int mpu401_ioctl(int dev, unsigned cmd, caddr_t arg)
734 {
735         struct mpu_config *devc;
736         mpu_command_rec rec;
737         int val, ret;
738
739         devc = &dev_conf[dev];
740         switch (cmd) 
741         {
742                 case SNDCTL_MIDI_MPUMODE:
743                         if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
744                                 printk(KERN_WARNING "mpu401: Intelligent mode not supported by the HW\n");
745                                 return -EINVAL;
746                         }
747                         if (get_user(val, (int *)arg))
748                                 return -EFAULT;
749                         set_uart_mode(dev, devc, !val);
750                         return 0;
751
752                 case SNDCTL_MIDI_MPUCMD:
753                         if (copy_from_user(&rec, arg, sizeof(rec)))
754                                 return -EFAULT;
755                         if ((ret = mpu401_command(dev, &rec)) < 0)
756                                 return ret;
757                         if (copy_to_user(arg, &rec, sizeof(rec)))
758                                 return -EFAULT;
759                         return 0;
760
761                 default:
762                         return -EINVAL;
763         }
764 }
765
766 static void mpu401_kick(int dev)
767 {
768 }
769
770 static int mpu401_buffer_status(int dev)
771 {
772         return 0;               /*
773                                  * No data in buffers
774                                  */
775 }
776
777 static int mpu_synth_ioctl(int dev,
778                 unsigned int cmd, caddr_t arg)
779 {
780         int midi_dev;
781         struct mpu_config *devc;
782
783         midi_dev = synth_devs[dev]->midi_dev;
784
785         if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
786                 return -ENXIO;
787
788         devc = &dev_conf[midi_dev];
789
790         switch (cmd)
791         {
792
793                 case SNDCTL_SYNTH_INFO:
794                         if(copy_to_user((&((char *) arg)[0]), (char *) &mpu_synth_info[midi_dev], sizeof(struct synth_info)))
795                                 return -EFAULT;
796                         return 0;
797
798                 case SNDCTL_SYNTH_MEMAVL:
799                         return 0x7fffffff;
800
801                 default:
802                         return -EINVAL;
803         }
804 }
805
806 static int mpu_synth_open(int dev, int mode)
807 {
808         int midi_dev, err;
809         struct mpu_config *devc;
810         struct coproc_operations *coprocessor;
811
812         midi_dev = synth_devs[dev]->midi_dev;
813
814         if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
815                 return -ENXIO;
816
817         devc = &dev_conf[midi_dev];
818
819         /*
820          *  Verify that the device is really running.
821          *  Some devices (such as Ensoniq SoundScape don't
822          *  work before the on board processor (OBP) is initialized
823          *  by downloading its microcode.
824          */
825
826         if (!devc->initialized)
827         {
828                 if (mpu401_status(devc) == 0xff)        /* Bus float */
829                 {
830                         printk(KERN_ERR "mpu401: Device not initialized properly\n");
831                         return -EIO;
832                 }
833                 reset_mpu401(devc);
834         }
835         if (devc->opened)
836                 return -EBUSY;
837         devc->mode = MODE_SYNTH;
838         devc->synthno = dev;
839
840         devc->inputintr = NULL;
841
842         coprocessor = midi_devs[midi_dev]->coproc;
843         if (coprocessor) {
844                 if (coprocessor->owner)
845                         __MOD_INC_USE_COUNT(coprocessor->owner);
846
847                 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
848                 {
849                         printk(KERN_WARNING "mpu401: Can't access coprocessor device\n");
850                         return err;
851                 }
852         }
853         devc->opened = mode;
854         reset_mpu401(devc);
855
856         if (mode & OPEN_READ)
857         {
858                 mpu_cmd(midi_dev, 0x8B, 0);     /* Enable data in stop mode */
859                 mpu_cmd(midi_dev, 0x34, 0);     /* Return timing bytes in stop mode */
860                 mpu_cmd(midi_dev, 0x87, 0);     /* Enable pitch & controller */
861         }
862         return 0;
863 }
864
865 static void mpu_synth_close(int dev)
866
867         int midi_dev;
868         struct mpu_config *devc;
869         struct coproc_operations *coprocessor;
870
871         midi_dev = synth_devs[dev]->midi_dev;
872
873         devc = &dev_conf[midi_dev];
874         mpu_cmd(midi_dev, 0x15, 0);     /* Stop recording, playback and MIDI */
875         mpu_cmd(midi_dev, 0x8a, 0);     /* Disable data in stopped mode */
876
877         devc->inputintr = NULL;
878
879         coprocessor = midi_devs[midi_dev]->coproc;
880         if (coprocessor) {
881                 coprocessor->close(coprocessor->devc, COPR_MIDI);
882
883                 if (coprocessor->owner)
884                         __MOD_DEC_USE_COUNT(coprocessor->owner);
885         }
886         devc->opened = 0;
887         devc->mode = 0;
888 }
889
890 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
891 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
892 #include "midi_synth.h"
893
894 static struct synth_operations mpu401_synth_proto =
895 {
896         owner:          THIS_MODULE,
897         id:             "MPU401",
898         info:           NULL,
899         midi_dev:       0,
900         synth_type:     SYNTH_TYPE_MIDI,
901         synth_subtype:  0,
902         open:           mpu_synth_open,
903         close:          mpu_synth_close,
904         ioctl:          mpu_synth_ioctl,
905         kill_note:      midi_synth_kill_note,
906         start_note:     midi_synth_start_note,
907         set_instr:      midi_synth_set_instr,
908         reset:          midi_synth_reset,
909         hw_control:     midi_synth_hw_control,
910         load_patch:     midi_synth_load_patch,
911         aftertouch:     midi_synth_aftertouch,
912         controller:     midi_synth_controller,
913         panning:        midi_synth_panning,
914         bender:         midi_synth_bender,
915         setup_voice:    midi_synth_setup_voice,
916         send_sysex:     midi_synth_send_sysex
917 };
918
919 static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
920
921 static struct midi_operations mpu401_midi_proto =
922 {
923         owner:          THIS_MODULE,
924         info:           {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
925         in_info:        {0},
926         open:           mpu401_open,
927         close:          mpu401_close,
928         ioctl:          mpu401_ioctl,
929         outputc:        mpu401_out,
930         start_read:     mpu401_start_read,
931         end_read:       mpu401_end_read,
932         kick:           mpu401_kick,
933         buffer_status:  mpu401_buffer_status,
934         prefix_cmd:     mpu401_prefix_cmd
935 };
936
937 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
938
939 static void mpu401_chk_version(int n, struct mpu_config *devc)
940 {
941         int tmp;
942         unsigned long flags;
943
944         devc->version = devc->revision = 0;
945
946         save_flags(flags);
947         cli();
948         if ((tmp = mpu_cmd(n, 0xAC, 0)) < 0)
949         {
950                 restore_flags(flags);
951                 return;
952         }
953         if ((tmp & 0xf0) > 0x20)        /* Why it's larger than 2.x ??? */
954         {
955                 restore_flags(flags);
956                 return;
957         }
958         devc->version = tmp;
959
960         if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0)
961         {
962                 devc->version = 0;
963                 restore_flags(flags);
964                 return;
965         }
966         devc->revision = tmp;
967         restore_flags(flags);
968 }
969
970 int attach_mpu401(struct address_info *hw_config, struct module *owner)
971 {
972         unsigned long flags;
973         char revision_char;
974
975         int m, ret;
976         struct mpu_config *devc;
977
978         hw_config->slots[1] = -1;
979         m = sound_alloc_mididev();
980         if (m == -1)
981         {
982                 printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
983                 ret = -ENOMEM;
984                 goto out_err;
985         }
986         devc = &dev_conf[m];
987         devc->base = hw_config->io_base;
988         devc->osp = hw_config->osp;
989         devc->irq = hw_config->irq;
990         devc->opened = 0;
991         devc->uart_mode = 0;
992         devc->initialized = 0;
993         devc->version = 0;
994         devc->revision = 0;
995         devc->capabilities = 0;
996         devc->timer_flag = 0;
997         devc->m_busy = 0;
998         devc->m_state = ST_INIT;
999         devc->shared_irq = hw_config->always_detect;
1000         devc->irq = hw_config->irq;
1001
1002         if (devc->irq < 0)
1003         {
1004                 devc->irq *= -1;
1005                 devc->shared_irq = 1;
1006         }
1007
1008         if (!hw_config->always_detect)
1009         {
1010                 /* Verify the hardware again */
1011                 if (!reset_mpu401(devc))
1012                 {
1013                         printk(KERN_WARNING "mpu401: Device didn't respond\n");
1014                         ret = -ENODEV;
1015                         goto out_mididev;
1016                 }
1017                 if (!devc->shared_irq)
1018                 {
1019                         if (request_irq(devc->irq, mpuintr, 0, "mpu401", (void *)m) < 0)
1020                         {
1021                                 printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);
1022                                 ret = -ENOMEM;
1023                                 goto out_mididev;
1024                         }
1025                 }
1026                 save_flags(flags);
1027                 cli();
1028                 mpu401_chk_version(m, devc);
1029                 if (devc->version == 0)
1030                         mpu401_chk_version(m, devc);
1031                 restore_flags(flags);
1032         }
1033
1034         if (!request_region(hw_config->io_base, 2, "mpu401"))
1035         {
1036                 ret = -ENOMEM;
1037                 goto out_irq;
1038         }       
1039
1040         if (devc->version != 0)
1041                 if (mpu_cmd(m, 0xC5, 0) >= 0)   /* Set timebase OK */
1042                         if (mpu_cmd(m, 0xE0, 120) >= 0)         /* Set tempo OK */
1043                                 devc->capabilities |= MPU_CAP_INTLG;    /* Supports intelligent mode */
1044
1045
1046         mpu401_synth_operations[m] = (struct synth_operations *)kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
1047
1048         if (mpu401_synth_operations[m] == NULL)
1049         {
1050                 printk(KERN_ERR "mpu401: Can't allocate memory\n");
1051                 ret = -ENOMEM;
1052                 goto out_resource;
1053         }
1054         if (!(devc->capabilities & MPU_CAP_INTLG))      /* No intelligent mode */
1055         {
1056                 memcpy((char *) mpu401_synth_operations[m],
1057                         (char *) &std_midi_synth,
1058                          sizeof(struct synth_operations));
1059         }
1060         else
1061         {
1062                 memcpy((char *) mpu401_synth_operations[m],
1063                         (char *) &mpu401_synth_proto,
1064                          sizeof(struct synth_operations));
1065         }
1066         if (owner)
1067                 mpu401_synth_operations[m]->owner = owner;
1068
1069         memcpy((char *) &mpu401_midi_operations[m],
1070                (char *) &mpu401_midi_proto,
1071                sizeof(struct midi_operations));
1072
1073         mpu401_midi_operations[m].converter = mpu401_synth_operations[m];
1074
1075         memcpy((char *) &mpu_synth_info[m],
1076                (char *) &mpu_synth_info_proto,
1077                sizeof(struct synth_info));
1078
1079         n_mpu_devs++;
1080
1081         if (devc->version == 0x20 && devc->revision >= 0x07)    /* MusicQuest interface */
1082         {
1083                 int ports = (devc->revision & 0x08) ? 32 : 16;
1084
1085                 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1086                                 MPU_CAP_CLS | MPU_CAP_2PORT;
1087
1088                 revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1089                 sprintf(mpu_synth_info[m].name, "MQX-%d%c MIDI Interface #%d",
1090                                 ports,
1091                                 revision_char,
1092                                 n_mpu_devs);
1093         }
1094         else
1095         {
1096                 revision_char = devc->revision ? devc->revision + '@' : ' ';
1097                 if ((int) devc->revision > ('Z' - '@'))
1098                         revision_char = '+';
1099
1100                 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1101
1102                 if (hw_config->name)
1103                         sprintf(mpu_synth_info[m].name, "%s (MPU401)", hw_config->name);
1104                 else
1105                         sprintf(mpu_synth_info[m].name,
1106                                 "MPU-401 %d.%d%c Midi interface #%d",
1107                                 (int) (devc->version & 0xf0) >> 4,
1108                                 devc->version & 0x0f,
1109                                 revision_char,
1110                                 n_mpu_devs);
1111         }
1112
1113         strcpy(mpu401_midi_operations[m].info.name,
1114                mpu_synth_info[m].name);
1115
1116         conf_printf(mpu_synth_info[m].name, hw_config);
1117
1118         mpu401_synth_operations[m]->midi_dev = devc->devno = m;
1119         mpu401_synth_operations[devc->devno]->info = &mpu_synth_info[devc->devno];
1120
1121         if (devc->capabilities & MPU_CAP_INTLG)         /* Intelligent mode */
1122                 hw_config->slots[2] = mpu_timer_init(m);
1123
1124         midi_devs[m] = &mpu401_midi_operations[devc->devno];
1125         
1126         if (owner)
1127                 midi_devs[m]->owner = owner;
1128
1129         hw_config->slots[1] = m;
1130         sequencer_init();
1131         
1132         return 0;
1133
1134 out_resource:
1135         release_region(hw_config->io_base, 2);
1136 out_irq:
1137         free_irq(devc->irq, (void *)m);
1138 out_mididev:
1139         sound_unload_mididev(m);
1140 out_err:
1141         return ret;
1142 }
1143
1144 static int reset_mpu401(struct mpu_config *devc)
1145 {
1146         unsigned long flags;
1147         int ok, timeout, n;
1148         int timeout_limit;
1149
1150         /*
1151          * Send the RESET command. Try again if no success at the first time.
1152          * (If the device is in the UART mode, it will not ack the reset cmd).
1153          */
1154
1155         ok = 0;
1156
1157         timeout_limit = devc->initialized ? 30000 : 100000;
1158         devc->initialized = 1;
1159
1160         for (n = 0; n < 2 && !ok; n++)
1161         {
1162                 for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1163                           ok = output_ready(devc);
1164
1165                 write_command(devc, MPU_RESET); /*
1166                                                            * Send MPU-401 RESET Command
1167                                                          */
1168
1169                 /*
1170                  * Wait at least 25 msec. This method is not accurate so let's make the
1171                  * loop bit longer. Cannot sleep since this is called during boot.
1172                  */
1173
1174                 for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1175                 {
1176                         save_flags(flags);
1177                         cli();
1178                         if (input_avail(devc))
1179                                 if (read_data(devc) == MPU_ACK)
1180                                         ok = 1;
1181                         restore_flags(flags);
1182                 }
1183
1184         }
1185
1186         devc->m_state = ST_INIT;
1187         devc->m_ptr = 0;
1188         devc->m_left = 0;
1189         devc->last_status = 0;
1190         devc->uart_mode = 0;
1191
1192         return ok;
1193 }
1194
1195 static void set_uart_mode(int dev, struct mpu_config *devc, int arg)
1196 {
1197         if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1198                 return;
1199         if ((devc->uart_mode == 0) == (arg == 0))
1200                 return;         /* Already set */
1201         reset_mpu401(devc);     /* This exits the uart mode */
1202
1203         if (arg)
1204         {
1205                 if (mpu_cmd(dev, UART_MODE_ON, 0) < 0)
1206                 {
1207                         printk(KERN_ERR "mpu401: Can't enter UART mode\n");
1208                         devc->uart_mode = 0;
1209                         return;
1210                 }
1211         }
1212         devc->uart_mode = arg;
1213
1214 }
1215
1216 int probe_mpu401(struct address_info *hw_config)
1217 {
1218         int ok = 0;
1219         struct mpu_config tmp_devc;
1220
1221         if (check_region(hw_config->io_base, 2))
1222         {
1223                 printk(KERN_ERR "mpu401: I/O port %x already in use\n\n", hw_config->io_base);
1224                 return 0;
1225         }
1226         tmp_devc.base = hw_config->io_base;
1227         tmp_devc.irq = hw_config->irq;
1228         tmp_devc.initialized = 0;
1229         tmp_devc.opened = 0;
1230         tmp_devc.osp = hw_config->osp;
1231
1232         if (hw_config->always_detect)
1233                 return 1;
1234
1235         if (inb(hw_config->io_base + 1) == 0xff)
1236         {
1237                 DDB(printk("MPU401: Port %x looks dead.\n", hw_config->io_base));
1238                 return 0;       /* Just bus float? */
1239         }
1240         ok = reset_mpu401(&tmp_devc);
1241
1242         if (!ok)
1243         {
1244                 DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
1245         }
1246         return ok;
1247 }
1248
1249 void unload_mpu401(struct address_info *hw_config)
1250 {
1251         void *p;
1252         int n=hw_config->slots[1];
1253                 
1254         if (n != -1) {
1255                 release_region(hw_config->io_base, 2);
1256                 if (hw_config->always_detect == 0 && hw_config->irq > 0)
1257                         free_irq(hw_config->irq, (void *)n);
1258                 p=mpu401_synth_operations[n];
1259                 sound_unload_mididev(n);
1260                 sound_unload_timerdev(hw_config->slots[2]);
1261                 if(p)
1262                         kfree(p);
1263         }
1264 }
1265
1266 /*****************************************************
1267  *      Timer stuff
1268  ****************************************************/
1269
1270 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1271 static volatile int curr_tempo, curr_timebase, hw_timebase;
1272 static int      max_timebase = 8;       /* 8*24=192 ppqn */
1273 static volatile unsigned long next_event_time;
1274 static volatile unsigned long curr_ticks, curr_clocks;
1275 static unsigned long prev_event_time;
1276 static int      metronome_mode;
1277
1278 static unsigned long clocks2ticks(unsigned long clocks)
1279 {
1280         /*
1281          * The MPU-401 supports just a limited set of possible timebase values.
1282          * Since the applications require more choices, the driver has to
1283          * program the HW to do its best and to convert between the HW and
1284          * actual timebases.
1285          */
1286         return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1287 }
1288
1289 static void set_timebase(int midi_dev, int val)
1290 {
1291         int hw_val;
1292
1293         if (val < 48)
1294                 val = 48;
1295         if (val > 1000)
1296                 val = 1000;
1297
1298         hw_val = val;
1299         hw_val = (hw_val + 12) / 24;
1300         if (hw_val > max_timebase)
1301                 hw_val = max_timebase;
1302
1303         if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1304         {
1305                 printk(KERN_WARNING "mpu401: Can't set HW timebase to %d\n", hw_val * 24);
1306                 return;
1307         }
1308         hw_timebase = hw_val * 24;
1309         curr_timebase = val;
1310
1311 }
1312
1313 static void tmr_reset(void)
1314 {
1315         unsigned long flags;
1316
1317         save_flags(flags);
1318         cli();
1319         next_event_time = (unsigned long) -1;
1320         prev_event_time = 0;
1321         curr_ticks = curr_clocks = 0;
1322         restore_flags(flags);
1323 }
1324
1325 static void set_timer_mode(int midi_dev)
1326 {
1327         if (timer_mode & TMR_MODE_CLS)
1328                 mpu_cmd(midi_dev, 0x3c, 0);     /* Use CLS sync */
1329         else if (timer_mode & TMR_MODE_SMPTE)
1330                 mpu_cmd(midi_dev, 0x3d, 0);     /* Use SMPTE sync */
1331
1332         if (timer_mode & TMR_INTERNAL)
1333         {
1334                   mpu_cmd(midi_dev, 0x80, 0);   /* Use MIDI sync */
1335         }
1336         else
1337         {
1338                 if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1339                 {
1340                         mpu_cmd(midi_dev, 0x82, 0);             /* Use MIDI sync */
1341                         mpu_cmd(midi_dev, 0x91, 0);             /* Enable ext MIDI ctrl */
1342                 }
1343                 else if (timer_mode & TMR_MODE_FSK)
1344                         mpu_cmd(midi_dev, 0x81, 0);     /* Use FSK sync */
1345         }
1346 }
1347
1348 static void stop_metronome(int midi_dev)
1349 {
1350         mpu_cmd(midi_dev, 0x84, 0);     /* Disable metronome */
1351 }
1352
1353 static void setup_metronome(int midi_dev)
1354 {
1355         int numerator, denominator;
1356         int clks_per_click, num_32nds_per_beat;
1357         int beats_per_measure;
1358
1359         numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1360         denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1361         clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1362         num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1363         beats_per_measure = (numerator * 4) >> denominator;
1364
1365         if (!metronome_mode)
1366                 mpu_cmd(midi_dev, 0x84, 0);     /* Disable metronome */
1367         else
1368         {
1369                 mpu_cmd(midi_dev, 0xE4, clks_per_click);
1370                 mpu_cmd(midi_dev, 0xE6, beats_per_measure);
1371                 mpu_cmd(midi_dev, 0x83, 0);     /* Enable metronome without accents */
1372         }
1373 }
1374
1375 static int mpu_start_timer(int midi_dev)
1376 {
1377         tmr_reset();
1378         set_timer_mode(midi_dev);
1379
1380         if (tmr_running)
1381                 return TIMER_NOT_ARMED;         /* Already running */
1382
1383         if (timer_mode & TMR_INTERNAL)
1384         {
1385                 mpu_cmd(midi_dev, 0x02, 0);     /* Send MIDI start */
1386                 tmr_running = 1;
1387                 return TIMER_NOT_ARMED;
1388         }
1389         else
1390         {
1391                 mpu_cmd(midi_dev, 0x35, 0);     /* Enable mode messages to PC */
1392                 mpu_cmd(midi_dev, 0x38, 0);     /* Enable sys common messages to PC */
1393                 mpu_cmd(midi_dev, 0x39, 0);     /* Enable real time messages to PC */
1394                 mpu_cmd(midi_dev, 0x97, 0);     /* Enable system exclusive messages to PC */
1395         }
1396         return TIMER_ARMED;
1397 }
1398
1399 static int mpu_timer_open(int dev, int mode)
1400 {
1401         int midi_dev = sound_timer_devs[dev]->devlink;
1402
1403         if (timer_open)
1404                 return -EBUSY;
1405
1406         tmr_reset();
1407         curr_tempo = 50;
1408         mpu_cmd(midi_dev, 0xE0, 50);
1409         curr_timebase = hw_timebase = 120;
1410         set_timebase(midi_dev, 120);
1411         timer_open = 1;
1412         metronome_mode = 0;
1413         set_timer_mode(midi_dev);
1414
1415         mpu_cmd(midi_dev, 0xe7, 0x04);  /* Send all clocks to host */
1416         mpu_cmd(midi_dev, 0x95, 0);     /* Enable clock to host */
1417
1418         return 0;
1419 }
1420
1421 static void mpu_timer_close(int dev)
1422 {
1423         int midi_dev = sound_timer_devs[dev]->devlink;
1424
1425         timer_open = tmr_running = 0;
1426         mpu_cmd(midi_dev, 0x15, 0);     /* Stop all */
1427         mpu_cmd(midi_dev, 0x94, 0);     /* Disable clock to host */
1428         mpu_cmd(midi_dev, 0x8c, 0);     /* Disable measure end messages to host */
1429         stop_metronome(midi_dev);
1430 }
1431
1432 static int mpu_timer_event(int dev, unsigned char *event)
1433 {
1434         unsigned char command = event[1];
1435         unsigned long parm = *(unsigned int *) &event[4];
1436         int midi_dev = sound_timer_devs[dev]->devlink;
1437
1438         switch (command)
1439         {
1440                 case TMR_WAIT_REL:
1441                         parm += prev_event_time;
1442                 case TMR_WAIT_ABS:
1443                         if (parm > 0)
1444                         {
1445                                 long time;
1446
1447                                 if (parm <= curr_ticks) /* It's the time */
1448                                         return TIMER_NOT_ARMED;
1449                                 time = parm;
1450                                 next_event_time = prev_event_time = time;
1451
1452                                 return TIMER_ARMED;
1453                         }
1454                         break;
1455
1456                 case TMR_START:
1457                         if (tmr_running)
1458                                 break;
1459                         return mpu_start_timer(midi_dev);
1460
1461                 case TMR_STOP:
1462                         mpu_cmd(midi_dev, 0x01, 0);     /* Send MIDI stop */
1463                         stop_metronome(midi_dev);
1464                         tmr_running = 0;
1465                         break;
1466
1467                 case TMR_CONTINUE:
1468                         if (tmr_running)
1469                                 break;
1470                         mpu_cmd(midi_dev, 0x03, 0);     /* Send MIDI continue */
1471                         setup_metronome(midi_dev);
1472                         tmr_running = 1;
1473                         break;
1474
1475                 case TMR_TEMPO:
1476                         if (parm)
1477                         {
1478                                 if (parm < 8)
1479                                         parm = 8;
1480                                 if (parm > 250)
1481                                         parm = 250;
1482                                 if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
1483                                         printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) parm);
1484                                 curr_tempo = parm;
1485                         }
1486                         break;
1487
1488                 case TMR_ECHO:
1489                         seq_copy_to_input(event, 8);
1490                         break;
1491
1492                 case TMR_TIMESIG:
1493                         if (metronome_mode)     /* Metronome enabled */
1494                         {
1495                                 metronome_mode = parm;
1496                                 setup_metronome(midi_dev);
1497                         }
1498                         break;
1499
1500                 default:;
1501         }
1502         return TIMER_NOT_ARMED;
1503 }
1504
1505 static unsigned long mpu_timer_get_time(int dev)
1506 {
1507         if (!timer_open)
1508                 return 0;
1509
1510         return curr_ticks;
1511 }
1512
1513 static int mpu_timer_ioctl(int dev, unsigned int command, caddr_t arg)
1514 {
1515         int midi_dev = sound_timer_devs[dev]->devlink;
1516         int *p = (int *)arg;
1517
1518         switch (command)
1519         {
1520                 case SNDCTL_TMR_SOURCE:
1521                         {
1522                                 int parm;
1523
1524                                 if (get_user(parm, p))
1525                                         return -EFAULT;
1526                                 parm &= timer_caps;
1527
1528                                 if (parm != 0)
1529                                 {
1530                                         timer_mode = parm;
1531         
1532                                         if (timer_mode & TMR_MODE_CLS)
1533                                                 mpu_cmd(midi_dev, 0x3c, 0);             /* Use CLS sync */
1534                                         else if (timer_mode & TMR_MODE_SMPTE)
1535                                                 mpu_cmd(midi_dev, 0x3d, 0);             /* Use SMPTE sync */
1536                                 }
1537                                 if (put_user(timer_mode, p))
1538                                         return -EFAULT;
1539                                 return timer_mode;
1540                         }
1541                         break;
1542
1543                 case SNDCTL_TMR_START:
1544                         mpu_start_timer(midi_dev);
1545                         return 0;
1546
1547                 case SNDCTL_TMR_STOP:
1548                         tmr_running = 0;
1549                         mpu_cmd(midi_dev, 0x01, 0);     /* Send MIDI stop */
1550                         stop_metronome(midi_dev);
1551                         return 0;
1552
1553                 case SNDCTL_TMR_CONTINUE:
1554                         if (tmr_running)
1555                                 return 0;
1556                         tmr_running = 1;
1557                         mpu_cmd(midi_dev, 0x03, 0);     /* Send MIDI continue */
1558                         return 0;
1559
1560                 case SNDCTL_TMR_TIMEBASE:
1561                         {
1562                                 int val;
1563
1564                                 if (get_user(val, p))
1565                                         return -EFAULT;
1566                                 if (val)
1567                                         set_timebase(midi_dev, val);
1568                                 if (put_user(curr_timebase, p))
1569                                         return -EFAULT;
1570                                 return curr_timebase;
1571                         }
1572                         break;
1573
1574                 case SNDCTL_TMR_TEMPO:
1575                         {
1576                                 int val;
1577                                 int ret;
1578
1579                                 if (get_user(val, p))
1580                                         return -EFAULT;
1581
1582                                 if (val)
1583                                 {
1584                                         if (val < 8)
1585                                                 val = 8;
1586                                         if (val > 250)
1587                                                 val = 250;
1588                                         if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
1589                                         {
1590                                                 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
1591                                                 return ret;
1592                                         }
1593                                         curr_tempo = val;
1594                                 }
1595                                 if (put_user(curr_tempo, p))
1596                                         return -EFAULT;
1597                                 return curr_tempo;
1598                         }
1599                         break;
1600
1601                 case SNDCTL_SEQ_CTRLRATE:
1602                         {
1603                                 int val;
1604
1605                                 if (get_user(val, p))
1606                                         return -EFAULT;
1607                                 if (val != 0)           /* Can't change */
1608                                         return -EINVAL;
1609                                 val = (curr_tempo * curr_timebase + 30) / 60;
1610                                 if (put_user(val, p))
1611                                         return -EFAULT;
1612                                 return val;
1613                         }
1614                         break;
1615
1616                 case SNDCTL_SEQ_GETTIME:
1617                         if (put_user(curr_ticks, p))
1618                                 return -EFAULT;
1619                         return curr_ticks;
1620
1621                 case SNDCTL_TMR_METRONOME:
1622                         if (get_user(metronome_mode, p))
1623                                 return -EFAULT;
1624                         setup_metronome(midi_dev);
1625                         return 0;
1626
1627                 default:;
1628         }
1629         return -EINVAL;
1630 }
1631
1632 static void mpu_timer_arm(int dev, long time)
1633 {
1634         if (time < 0)
1635                 time = curr_ticks + 1;
1636         else if (time <= curr_ticks)    /* It's the time */
1637                 return;
1638         next_event_time = prev_event_time = time;
1639         return;
1640 }
1641
1642 static struct sound_timer_operations mpu_timer =
1643 {
1644         owner:          THIS_MODULE,
1645         info:           {"MPU-401 Timer", 0},
1646         priority:       10,     /* Priority */
1647         devlink:        0,      /* Local device link */
1648         open:           mpu_timer_open,
1649         close:          mpu_timer_close,
1650         event:          mpu_timer_event,
1651         get_time:       mpu_timer_get_time,
1652         ioctl:          mpu_timer_ioctl,
1653         arm_timer:      mpu_timer_arm
1654 };
1655
1656 static void mpu_timer_interrupt(void)
1657 {
1658         if (!timer_open)
1659                 return;
1660
1661         if (!tmr_running)
1662                 return;
1663
1664         curr_clocks++;
1665         curr_ticks = clocks2ticks(curr_clocks);
1666
1667         if (curr_ticks >= next_event_time)
1668         {
1669                 next_event_time = (unsigned long) -1;
1670                 sequencer_timer(0);
1671         }
1672 }
1673
1674 static void timer_ext_event(struct mpu_config *devc, int event, int parm)
1675 {
1676         int midi_dev = devc->devno;
1677
1678         if (!devc->timer_flag)
1679                 return;
1680
1681         switch (event)
1682         {
1683                 case TMR_CLOCK:
1684                         printk("<MIDI clk>");
1685                         break;
1686
1687                 case TMR_START:
1688                         printk("Ext MIDI start\n");
1689                         if (!tmr_running)
1690                         {
1691                                 if (timer_mode & TMR_EXTERNAL)
1692                                 {
1693                                         tmr_running = 1;
1694                                         setup_metronome(midi_dev);
1695                                         next_event_time = 0;
1696                                         STORE(SEQ_START_TIMER());
1697                                 }
1698                         }
1699                         break;
1700
1701                 case TMR_STOP:
1702                         printk("Ext MIDI stop\n");
1703                         if (timer_mode & TMR_EXTERNAL)
1704                         {
1705                                 tmr_running = 0;
1706                                 stop_metronome(midi_dev);
1707                                 STORE(SEQ_STOP_TIMER());
1708                         }
1709                         break;
1710
1711                 case TMR_CONTINUE:
1712                         printk("Ext MIDI continue\n");
1713                         if (timer_mode & TMR_EXTERNAL)
1714                         {
1715                                 tmr_running = 1;
1716                                 setup_metronome(midi_dev);
1717                                 STORE(SEQ_CONTINUE_TIMER());
1718                         }
1719                         break;
1720
1721                 case TMR_SPP:
1722                         printk("Songpos: %d\n", parm);
1723                         if (timer_mode & TMR_EXTERNAL)
1724                         {
1725                                 STORE(SEQ_SONGPOS(parm));
1726                         }
1727                         break;
1728         }
1729 }
1730
1731 static int mpu_timer_init(int midi_dev)
1732 {
1733         struct mpu_config *devc;
1734         int n;
1735
1736         devc = &dev_conf[midi_dev];
1737
1738         if (timer_initialized)
1739                 return -1;      /* There is already a similar timer */
1740
1741         timer_initialized = 1;
1742
1743         mpu_timer.devlink = midi_dev;
1744         dev_conf[midi_dev].timer_flag = 1;
1745
1746         n = sound_alloc_timerdev();
1747         if (n == -1)
1748                 n = 0;
1749         sound_timer_devs[n] = &mpu_timer;
1750
1751         if (devc->version < 0x20)       /* Original MPU-401 */
1752                 timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1753         else
1754         {
1755                 /*
1756                  * The version number 2.0 is used (at least) by the
1757                  * MusicQuest cards and the Roland Super-MPU.
1758                  *
1759                  * MusicQuest has given a special meaning to the bits of the
1760                  * revision number. The Super-MPU returns 0.
1761                  */
1762
1763                 if (devc->revision)
1764                         timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1765
1766                 if (devc->revision & 0x02)
1767                         timer_caps |= TMR_MODE_CLS;
1768
1769
1770                 if (devc->revision & 0x40)
1771                         max_timebase = 10;      /* Has the 216 and 240 ppqn modes */
1772         }
1773
1774         timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1775         return n;
1776
1777 }
1778
1779 EXPORT_SYMBOL(probe_mpu401);
1780 EXPORT_SYMBOL(attach_mpu401);
1781 EXPORT_SYMBOL(unload_mpu401);
1782 EXPORT_SYMBOL(intchk_mpu401);
1783 EXPORT_SYMBOL(mpuintr);
1784
1785 static struct address_info cfg;
1786
1787 static int io = -1;
1788 static int irq = -1;
1789
1790 MODULE_PARM(irq, "i");
1791 MODULE_PARM(io, "i");
1792
1793 int __init init_mpu401(void)
1794 {
1795         int ret;
1796         /* Can be loaded either for module use or to provide functions
1797            to others */
1798         if (io != -1 && irq != -1) {
1799                 cfg.irq = irq;
1800                 cfg.io_base = io;
1801                 if (probe_mpu401(&cfg) == 0)
1802                         return -ENODEV;
1803                 if ((ret = attach_mpu401(&cfg, THIS_MODULE)))
1804                         return ret;
1805         }
1806         
1807         return 0;
1808 }
1809
1810 void __exit cleanup_mpu401(void)
1811 {
1812         if (io != -1 && irq != -1) {
1813                 /* Check for use by, for example, sscape driver */
1814                 unload_mpu401(&cfg);
1815         }
1816 }
1817
1818 module_init(init_mpu401);
1819 module_exit(cleanup_mpu401);
1820
1821 #ifndef MODULE
1822 static int __init setup_mpu401(char *str)
1823 {
1824         /* io, irq */
1825         int ints[3];
1826         
1827         str = get_options(str, ARRAY_SIZE(ints), ints);
1828         
1829         io = ints[1];
1830         irq = ints[2];
1831
1832         return 1;
1833 }
1834
1835 __setup("mpu401=", setup_mpu401);
1836 #endif
1837 MODULE_LICENSE("GPL");