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