Pull one more egcs 1.1.2 workaround.
[linux-2.6/linux-mips.git] / sound / oss / mpu401.c
blob38ae0aaf95ca0d66f4223b851a82107603914306
1 /*
2 * sound/mpu401.c
4 * The low level driver for Roland MPU-401 compatible Midi cards.
5 */
6 /*
7 * Copyright (C) by Hannu Savolainen 1993-1997
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.
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
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/spinlock.h>
23 #define USE_SEQ_MACROS
24 #define USE_SIMPLE_MACROS
26 #include "sound_config.h"
28 #include "coproc.h"
29 #include "mpu401.h"
31 static int timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
33 struct mpu_config
35 int base; /*
36 * I/O base
38 int irq;
39 int opened; /*
40 * Open mode
42 int devno;
43 int synthno;
44 int uart_mode;
45 int initialized;
46 int mode;
47 #define MODE_MIDI 1
48 #define MODE_SYNTH 2
49 unsigned char version, revision;
50 unsigned int capabilities;
51 #define MPU_CAP_INTLG 0x10000000
52 #define MPU_CAP_SYNC 0x00000010
53 #define MPU_CAP_FSK 0x00000020
54 #define MPU_CAP_CLS 0x00000040
55 #define MPU_CAP_SMPTE 0x00000080
56 #define MPU_CAP_2PORT 0x00000001
57 int timer_flag;
59 #define MBUF_MAX 10
60 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
61 {printk( "MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
62 int m_busy;
63 unsigned char m_buf[MBUF_MAX];
64 int m_ptr;
65 int m_state;
66 int m_left;
67 unsigned char last_status;
68 void (*inputintr) (int dev, unsigned char data);
69 int shared_irq;
70 int *osp;
71 spinlock_t lock;
74 #define DATAPORT(base) (base)
75 #define COMDPORT(base) (base+1)
76 #define STATPORT(base) (base+1)
79 static void mpu401_close(int dev);
81 static int mpu401_status(struct mpu_config *devc)
83 return inb(STATPORT(devc->base));
86 #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
87 #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
89 static void write_command(struct mpu_config *devc, unsigned char cmd)
91 outb(cmd, COMDPORT(devc->base));
94 static int read_data(struct mpu_config *devc)
96 return inb(DATAPORT(devc->base));
99 static void write_data(struct mpu_config *devc, unsigned char byte)
101 outb(byte, DATAPORT(devc->base));
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
110 static struct mpu_config dev_conf[MAX_MIDI_DEV] =
115 static int n_mpu_devs = 0;
117 static int reset_mpu401(struct mpu_config *devc);
118 static void set_uart_mode(int dev, struct mpu_config *devc, int arg);
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);
124 static struct synth_info mpu_synth_info_proto = {
125 "MPU-401 MIDI interface",
127 SYNTH_TYPE_MIDI,
128 MIDI_TYPE_MPU401,
129 0, 128,
130 0, 128,
131 SYNTH_CAP_INPUT
134 static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
137 * States for the input scanner
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 */
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 */
150 static unsigned char len_tab[] = /* # of data bytes following a status
153 2, /* 8x */
154 2, /* 9x */
155 2, /* Ax */
156 2, /* Bx */
157 1, /* Cx */
158 1, /* Dx */
159 2, /* Ex */
160 0 /* Fx */
163 #define STORE(cmd) \
165 int len; \
166 unsigned char obuf[8]; \
167 cmd; \
168 seq_input_event(obuf, len); \
171 #define _seqbuf obuf
172 #define _seqbufptr 0
173 #define _SEQ_ADVBUF(x) len=x
175 static int mpu_input_scanner(struct mpu_config *devc, unsigned char midic)
178 switch (devc->m_state)
180 case ST_INIT:
181 switch (midic)
183 case 0xf8:
184 /* Timer overflow */
185 break;
187 case 0xfc:
188 printk("<all end>");
189 break;
191 case 0xfd:
192 if (devc->timer_flag)
193 mpu_timer_interrupt();
194 break;
196 case 0xfe:
197 return MPU_ACK;
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;
210 case 0xf9:
211 printk("<conductor rq>");
212 break;
214 case 0xff:
215 devc->m_state = ST_SYSMSG;
216 break;
218 default:
219 if (midic <= 0xef)
221 /* printk( "mpu time: %d ", midic); */
222 devc->m_state = ST_TIMED;
224 else
225 printk("<MPU: Unknown event %02x> ", midic);
227 break;
229 case ST_TIMED:
231 int msg = ((int) (midic & 0xf0) >> 4);
233 devc->m_state = ST_DATABYTE;
235 if (msg < 8) /* Data byte */
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;
242 devc->m_ptr = 2;
243 devc->m_buf[0] = devc->last_status;
244 devc->m_buf[1] = midic;
246 if (devc->m_left <= 0)
248 devc->m_state = ST_INIT;
249 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
250 devc->m_ptr = 0;
253 else if (msg == 0xf) /* MPU MARK */
255 devc->m_state = ST_INIT;
257 switch (midic)
259 case 0xf8:
260 /* printk( "NOP "); */
261 break;
263 case 0xf9:
264 /* printk( "meas end "); */
265 break;
267 case 0xfc:
268 /* printk( "data end "); */
269 break;
271 default:
272 printk("Unknown MPU mark %02x\n", midic);
275 else
277 devc->last_status = midic;
278 /* printk( "midi msg "); */
279 msg -= 8;
280 devc->m_left = len_tab[msg];
282 devc->m_ptr = 1;
283 devc->m_buf[0] = midic;
285 if (devc->m_left <= 0)
287 devc->m_state = ST_INIT;
288 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
289 devc->m_ptr = 0;
293 break;
295 case ST_SYSMSG:
296 switch (midic)
298 case 0xf0:
299 printk("<SYX>");
300 devc->m_state = ST_SYSEX;
301 break;
303 case 0xf1:
304 devc->m_state = ST_MTC;
305 break;
307 case 0xf2:
308 devc->m_state = ST_SONGPOS;
309 devc->m_ptr = 0;
310 break;
312 case 0xf3:
313 devc->m_state = ST_SONGSEL;
314 break;
316 case 0xf6:
317 /* printk( "tune_request\n"); */
318 devc->m_state = ST_INIT;
321 * Real time messages
323 case 0xf8:
324 /* midi clock */
325 devc->m_state = ST_INIT;
326 timer_ext_event(devc, TMR_CLOCK, 0);
327 break;
329 case 0xfA:
330 devc->m_state = ST_INIT;
331 timer_ext_event(devc, TMR_START, 0);
332 break;
334 case 0xFB:
335 devc->m_state = ST_INIT;
336 timer_ext_event(devc, TMR_CONTINUE, 0);
337 break;
339 case 0xFC:
340 devc->m_state = ST_INIT;
341 timer_ext_event(devc, TMR_STOP, 0);
342 break;
344 case 0xFE:
345 /* active sensing */
346 devc->m_state = ST_INIT;
347 break;
349 case 0xff:
350 /* printk( "midi hard reset"); */
351 devc->m_state = ST_INIT;
352 break;
354 default:
355 printk("unknown MIDI sysmsg %0x\n", midic);
356 devc->m_state = ST_INIT;
358 break;
360 case ST_MTC:
361 devc->m_state = ST_INIT;
362 printk("MTC frame %x02\n", midic);
363 break;
365 case ST_SYSEX:
366 if (midic == 0xf7)
368 printk("<EOX>");
369 devc->m_state = ST_INIT;
371 else
372 printk("%02x ", midic);
373 break;
375 case ST_SONGPOS:
376 BUFTEST(devc);
377 devc->m_buf[devc->m_ptr++] = midic;
378 if (devc->m_ptr == 2)
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));
386 break;
388 case ST_DATABYTE:
389 BUFTEST(devc);
390 devc->m_buf[devc->m_ptr++] = midic;
391 if ((--devc->m_left) <= 0)
393 devc->m_state = ST_INIT;
394 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
395 devc->m_ptr = 0;
397 break;
399 default:
400 printk("Bad state %d ", devc->m_state);
401 devc->m_state = ST_INIT;
403 return 1;
406 static void mpu401_input_loop(struct mpu_config *devc)
408 unsigned long flags;
409 int busy;
410 int n;
412 spin_lock_irqsave(&devc->lock,flags);
413 busy = devc->m_busy;
414 devc->m_busy = 1;
415 spin_unlock_irqrestore(&devc->lock,flags);
417 if (busy) /* Already inside the scanner */
418 return;
420 n = 50;
422 while (input_avail(devc) && n-- > 0)
424 unsigned char c = read_data(devc);
426 if (devc->mode == MODE_SYNTH)
428 mpu_input_scanner(devc, c);
430 else if (devc->opened & OPEN_READ && devc->inputintr != NULL)
431 devc->inputintr(devc->devno, c);
433 devc->m_busy = 0;
436 int intchk_mpu401(void *dev_id)
438 struct mpu_config *devc;
439 int dev = (int) dev_id;
441 devc = &dev_conf[dev];
442 return input_avail(devc);
445 void mpuintr(int irq, void *dev_id, struct pt_regs *dummy)
447 struct mpu_config *devc;
448 int dev = (int) dev_id;
450 devc = &dev_conf[dev];
452 if (input_avail(devc))
454 if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
455 mpu401_input_loop(devc);
456 else
458 /* Dummy read (just to acknowledge the interrupt) */
459 read_data(devc);
464 static int mpu401_open(int dev, int mode,
465 void (*input) (int dev, unsigned char data),
466 void (*output) (int dev)
469 int err;
470 struct mpu_config *devc;
471 struct coproc_operations *coprocessor;
473 if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
474 return -ENXIO;
476 devc = &dev_conf[dev];
478 if (devc->opened)
479 return -EBUSY;
481 * Verify that the device is really running.
482 * Some devices (such as Ensoniq SoundScape don't
483 * work before the on board processor (OBP) is initialized
484 * by downloading its microcode.
487 if (!devc->initialized)
489 if (mpu401_status(devc) == 0xff) /* Bus float */
491 printk(KERN_ERR "mpu401: Device not initialized properly\n");
492 return -EIO;
494 reset_mpu401(devc);
497 if ( (coprocessor = midi_devs[dev]->coproc) != NULL )
499 if (coprocessor->owner)
500 __MOD_INC_USE_COUNT(coprocessor->owner);
502 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
504 printk(KERN_WARNING "MPU-401: Can't access coprocessor device\n");
505 mpu401_close(dev);
506 return err;
510 set_uart_mode(dev, devc, 1);
511 devc->mode = MODE_MIDI;
512 devc->synthno = 0;
514 mpu401_input_loop(devc);
516 devc->inputintr = input;
517 devc->opened = mode;
519 return 0;
522 static void mpu401_close(int dev)
524 struct mpu_config *devc;
525 struct coproc_operations *coprocessor;
527 devc = &dev_conf[dev];
528 if (devc->uart_mode)
529 reset_mpu401(devc); /*
530 * This disables the UART mode
532 devc->mode = 0;
533 devc->inputintr = NULL;
535 coprocessor = midi_devs[dev]->coproc;
536 if (coprocessor) {
537 coprocessor->close(coprocessor->devc, COPR_MIDI);
539 if (coprocessor->owner)
540 __MOD_DEC_USE_COUNT(coprocessor->owner);
542 devc->opened = 0;
545 static int mpu401_out(int dev, unsigned char midi_byte)
547 int timeout;
548 unsigned long flags;
550 struct mpu_config *devc;
552 devc = &dev_conf[dev];
555 * Sometimes it takes about 30000 loops before the output becomes ready
556 * (After reset). Normally it takes just about 10 loops.
559 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
561 spin_lock_irqsave(&devc->lock,flags);
562 if (!output_ready(devc))
564 printk(KERN_WARNING "mpu401: Send data timeout\n");
565 spin_unlock_irqrestore(&devc->lock,flags);
566 return 0;
568 write_data(devc, midi_byte);
569 spin_unlock_irqrestore(&devc->lock,flags);
570 return 1;
573 static int mpu401_command(int dev, mpu_command_rec * cmd)
575 int i, timeout, ok;
576 int ret = 0;
577 unsigned long flags;
578 struct mpu_config *devc;
580 devc = &dev_conf[dev];
582 if (devc->uart_mode) /*
583 * Not possible in UART mode
586 printk(KERN_WARNING "mpu401: commands not possible in the UART mode\n");
587 return -EINVAL;
590 * Test for input since pending input seems to block the output.
592 if (input_avail(devc))
593 mpu401_input_loop(devc);
596 * Sometimes it takes about 50000 loops before the output becomes ready
597 * (After reset). Normally it takes just about 10 loops.
600 timeout = 50000;
601 retry:
602 if (timeout-- <= 0)
604 printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
605 return -EIO;
607 spin_lock_irqsave(&devc->lock,flags);
609 if (!output_ready(devc))
611 spin_unlock_irqrestore(&devc->lock,flags);
612 goto retry;
614 write_command(devc, cmd->cmd);
616 ok = 0;
617 for (timeout = 50000; timeout > 0 && !ok; timeout--)
619 if (input_avail(devc))
621 if (devc->opened && devc->mode == MODE_SYNTH)
623 if (mpu_input_scanner(devc, read_data(devc)) == MPU_ACK)
624 ok = 1;
626 else
628 /* Device is not currently open. Use simpler method */
629 if (read_data(devc) == MPU_ACK)
630 ok = 1;
634 if (!ok)
636 spin_unlock_irqrestore(&devc->lock,flags);
637 return -EIO;
639 if (cmd->nr_args)
641 for (i = 0; i < cmd->nr_args; i++)
643 for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
645 if (!mpu401_out(dev, cmd->data[i]))
647 spin_unlock_irqrestore(&devc->lock,flags);
648 printk(KERN_WARNING "mpu401: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
649 return -EIO;
653 ret = 0;
654 cmd->data[0] = 0;
656 if (cmd->nr_returns)
658 for (i = 0; i < cmd->nr_returns; i++)
660 ok = 0;
661 for (timeout = 5000; timeout > 0 && !ok; timeout--)
662 if (input_avail(devc))
664 cmd->data[i] = read_data(devc);
665 ok = 1;
667 if (!ok)
669 spin_unlock_irqrestore(&devc->lock,flags);
670 return -EIO;
674 spin_unlock_irqrestore(&devc->lock,flags);
675 return ret;
678 static int mpu_cmd(int dev, int cmd, int data)
680 int ret;
682 static mpu_command_rec rec;
684 rec.cmd = cmd & 0xff;
685 rec.nr_args = ((cmd & 0xf0) == 0xE0);
686 rec.nr_returns = ((cmd & 0xf0) == 0xA0);
687 rec.data[0] = data & 0xff;
689 if ((ret = mpu401_command(dev, &rec)) < 0)
690 return ret;
691 return (unsigned char) rec.data[0];
694 static int mpu401_prefix_cmd(int dev, unsigned char status)
696 struct mpu_config *devc = &dev_conf[dev];
698 if (devc->uart_mode)
699 return 1;
701 if (status < 0xf0)
703 if (mpu_cmd(dev, 0xD0, 0) < 0)
704 return 0;
705 return 1;
707 switch (status)
709 case 0xF0:
710 if (mpu_cmd(dev, 0xDF, 0) < 0)
711 return 0;
712 return 1;
714 default:
715 return 0;
719 static int mpu401_start_read(int dev)
721 return 0;
724 static int mpu401_end_read(int dev)
726 return 0;
729 static int mpu401_ioctl(int dev, unsigned cmd, caddr_t arg)
731 struct mpu_config *devc;
732 mpu_command_rec rec;
733 int val, ret;
735 devc = &dev_conf[dev];
736 switch (cmd)
738 case SNDCTL_MIDI_MPUMODE:
739 if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
740 printk(KERN_WARNING "mpu401: Intelligent mode not supported by the HW\n");
741 return -EINVAL;
743 if (get_user(val, (int *)arg))
744 return -EFAULT;
745 set_uart_mode(dev, devc, !val);
746 return 0;
748 case SNDCTL_MIDI_MPUCMD:
749 if (copy_from_user(&rec, arg, sizeof(rec)))
750 return -EFAULT;
751 if ((ret = mpu401_command(dev, &rec)) < 0)
752 return ret;
753 if (copy_to_user(arg, &rec, sizeof(rec)))
754 return -EFAULT;
755 return 0;
757 default:
758 return -EINVAL;
762 static void mpu401_kick(int dev)
766 static int mpu401_buffer_status(int dev)
768 return 0; /*
769 * No data in buffers
773 static int mpu_synth_ioctl(int dev,
774 unsigned int cmd, caddr_t arg)
776 int midi_dev;
777 struct mpu_config *devc;
779 midi_dev = synth_devs[dev]->midi_dev;
781 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
782 return -ENXIO;
784 devc = &dev_conf[midi_dev];
786 switch (cmd)
789 case SNDCTL_SYNTH_INFO:
790 memcpy((&((char *) arg)[0]), (char *) &mpu_synth_info[midi_dev], sizeof(struct synth_info));
791 return 0;
793 case SNDCTL_SYNTH_MEMAVL:
794 return 0x7fffffff;
796 default:
797 return -EINVAL;
801 static int mpu_synth_open(int dev, int mode)
803 int midi_dev, err;
804 struct mpu_config *devc;
805 struct coproc_operations *coprocessor;
807 midi_dev = synth_devs[dev]->midi_dev;
809 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
810 return -ENXIO;
812 devc = &dev_conf[midi_dev];
815 * Verify that the device is really running.
816 * Some devices (such as Ensoniq SoundScape don't
817 * work before the on board processor (OBP) is initialized
818 * by downloading its microcode.
821 if (!devc->initialized)
823 if (mpu401_status(devc) == 0xff) /* Bus float */
825 printk(KERN_ERR "mpu401: Device not initialized properly\n");
826 return -EIO;
828 reset_mpu401(devc);
830 if (devc->opened)
831 return -EBUSY;
832 devc->mode = MODE_SYNTH;
833 devc->synthno = dev;
835 devc->inputintr = NULL;
837 coprocessor = midi_devs[midi_dev]->coproc;
838 if (coprocessor) {
839 if (coprocessor->owner)
840 __MOD_INC_USE_COUNT(coprocessor->owner);
842 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
844 printk(KERN_WARNING "mpu401: Can't access coprocessor device\n");
845 return err;
848 devc->opened = mode;
849 reset_mpu401(devc);
851 if (mode & OPEN_READ)
853 mpu_cmd(midi_dev, 0x8B, 0); /* Enable data in stop mode */
854 mpu_cmd(midi_dev, 0x34, 0); /* Return timing bytes in stop mode */
855 mpu_cmd(midi_dev, 0x87, 0); /* Enable pitch & controller */
857 return 0;
860 static void mpu_synth_close(int dev)
862 int midi_dev;
863 struct mpu_config *devc;
864 struct coproc_operations *coprocessor;
866 midi_dev = synth_devs[dev]->midi_dev;
868 devc = &dev_conf[midi_dev];
869 mpu_cmd(midi_dev, 0x15, 0); /* Stop recording, playback and MIDI */
870 mpu_cmd(midi_dev, 0x8a, 0); /* Disable data in stopped mode */
872 devc->inputintr = NULL;
874 coprocessor = midi_devs[midi_dev]->coproc;
875 if (coprocessor) {
876 coprocessor->close(coprocessor->devc, COPR_MIDI);
878 if (coprocessor->owner)
879 __MOD_DEC_USE_COUNT(coprocessor->owner);
881 devc->opened = 0;
882 devc->mode = 0;
885 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
886 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
887 #include "midi_synth.h"
889 static struct synth_operations mpu401_synth_proto =
891 owner: THIS_MODULE,
892 id: "MPU401",
893 info: NULL,
894 midi_dev: 0,
895 synth_type: SYNTH_TYPE_MIDI,
896 synth_subtype: 0,
897 open: mpu_synth_open,
898 close: mpu_synth_close,
899 ioctl: mpu_synth_ioctl,
900 kill_note: midi_synth_kill_note,
901 start_note: midi_synth_start_note,
902 set_instr: midi_synth_set_instr,
903 reset: midi_synth_reset,
904 hw_control: midi_synth_hw_control,
905 load_patch: midi_synth_load_patch,
906 aftertouch: midi_synth_aftertouch,
907 controller: midi_synth_controller,
908 panning: midi_synth_panning,
909 bender: midi_synth_bender,
910 setup_voice: midi_synth_setup_voice,
911 send_sysex: midi_synth_send_sysex
914 static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
916 static struct midi_operations mpu401_midi_proto =
918 owner: THIS_MODULE,
919 info: {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
920 in_info: {0},
921 open: mpu401_open,
922 close: mpu401_close,
923 ioctl: mpu401_ioctl,
924 outputc: mpu401_out,
925 start_read: mpu401_start_read,
926 end_read: mpu401_end_read,
927 kick: mpu401_kick,
928 buffer_status: mpu401_buffer_status,
929 prefix_cmd: mpu401_prefix_cmd
932 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
934 static void mpu401_chk_version(int n, struct mpu_config *devc)
936 int tmp;
937 unsigned long flags;
939 devc->version = devc->revision = 0;
941 spin_lock_irqsave(&devc->lock,flags);
942 if ((tmp = mpu_cmd(n, 0xAC, 0)) < 0)
944 spin_unlock_irqrestore(&devc->lock,flags);
945 return;
947 if ((tmp & 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
949 spin_unlock_irqrestore(&devc->lock,flags);
950 return;
952 devc->version = tmp;
954 if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0)
956 devc->version = 0;
957 spin_unlock_irqrestore(&devc->lock,flags);
958 return;
960 devc->revision = tmp;
961 spin_unlock_irqrestore(&devc->lock,flags);
964 void attach_mpu401(struct address_info *hw_config, struct module *owner)
966 unsigned long flags;
967 char revision_char;
969 int m;
970 struct mpu_config *devc;
972 hw_config->slots[1] = -1;
973 m = sound_alloc_mididev();
974 if (m == -1)
976 printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
977 return;
979 devc = &dev_conf[m];
980 devc->base = hw_config->io_base;
981 devc->osp = hw_config->osp;
982 devc->irq = hw_config->irq;
983 devc->opened = 0;
984 devc->uart_mode = 0;
985 devc->initialized = 0;
986 devc->version = 0;
987 devc->revision = 0;
988 devc->capabilities = 0;
989 devc->timer_flag = 0;
990 devc->m_busy = 0;
991 devc->m_state = ST_INIT;
992 devc->shared_irq = hw_config->always_detect;
993 devc->irq = hw_config->irq;
994 spin_lock_init(&devc->lock);
996 if (devc->irq < 0)
998 devc->irq *= -1;
999 devc->shared_irq = 1;
1002 if (!hw_config->always_detect)
1004 /* Verify the hardware again */
1005 if (!reset_mpu401(devc))
1007 printk(KERN_WARNING "mpu401: Device didn't respond\n");
1008 sound_unload_mididev(m);
1009 return;
1011 if (!devc->shared_irq)
1013 if (request_irq(devc->irq, mpuintr, 0, "mpu401", (void *)m) < 0)
1015 printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);
1016 sound_unload_mididev(m);
1017 return;
1020 spin_lock_irqsave(&devc->lock,flags);
1021 mpu401_chk_version(m, devc);
1022 if (devc->version == 0)
1023 mpu401_chk_version(m, devc);
1024 spin_unlock_irqrestore(&devc->lock,flags);
1026 request_region(hw_config->io_base, 2, "mpu401");
1028 if (devc->version != 0)
1029 if (mpu_cmd(m, 0xC5, 0) >= 0) /* Set timebase OK */
1030 if (mpu_cmd(m, 0xE0, 120) >= 0) /* Set tempo OK */
1031 devc->capabilities |= MPU_CAP_INTLG; /* Supports intelligent mode */
1034 mpu401_synth_operations[m] = (struct synth_operations *)kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
1036 if (mpu401_synth_operations[m] == NULL)
1038 sound_unload_mididev(m);
1039 printk(KERN_ERR "mpu401: Can't allocate memory\n");
1040 return;
1042 if (!(devc->capabilities & MPU_CAP_INTLG)) /* No intelligent mode */
1044 memcpy((char *) mpu401_synth_operations[m],
1045 (char *) &std_midi_synth,
1046 sizeof(struct synth_operations));
1048 else
1050 memcpy((char *) mpu401_synth_operations[m],
1051 (char *) &mpu401_synth_proto,
1052 sizeof(struct synth_operations));
1054 if (owner)
1055 mpu401_synth_operations[m]->owner = owner;
1057 memcpy((char *) &mpu401_midi_operations[m],
1058 (char *) &mpu401_midi_proto,
1059 sizeof(struct midi_operations));
1061 mpu401_midi_operations[m].converter = mpu401_synth_operations[m];
1063 memcpy((char *) &mpu_synth_info[m],
1064 (char *) &mpu_synth_info_proto,
1065 sizeof(struct synth_info));
1067 n_mpu_devs++;
1069 if (devc->version == 0x20 && devc->revision >= 0x07) /* MusicQuest interface */
1071 int ports = (devc->revision & 0x08) ? 32 : 16;
1073 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1074 MPU_CAP_CLS | MPU_CAP_2PORT;
1076 revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1077 sprintf(mpu_synth_info[m].name, "MQX-%d%c MIDI Interface #%d",
1078 ports,
1079 revision_char,
1080 n_mpu_devs);
1082 else
1084 revision_char = devc->revision ? devc->revision + '@' : ' ';
1085 if ((int) devc->revision > ('Z' - '@'))
1086 revision_char = '+';
1088 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1090 if (hw_config->name)
1091 sprintf(mpu_synth_info[m].name, "%s (MPU401)", hw_config->name);
1092 else
1093 sprintf(mpu_synth_info[m].name,
1094 "MPU-401 %d.%d%c Midi interface #%d",
1095 (int) (devc->version & 0xf0) >> 4,
1096 devc->version & 0x0f,
1097 revision_char,
1098 n_mpu_devs);
1101 strcpy(mpu401_midi_operations[m].info.name,
1102 mpu_synth_info[m].name);
1104 conf_printf(mpu_synth_info[m].name, hw_config);
1106 mpu401_synth_operations[m]->midi_dev = devc->devno = m;
1107 mpu401_synth_operations[devc->devno]->info = &mpu_synth_info[devc->devno];
1109 if (devc->capabilities & MPU_CAP_INTLG) /* Intelligent mode */
1110 hw_config->slots[2] = mpu_timer_init(m);
1112 midi_devs[m] = &mpu401_midi_operations[devc->devno];
1114 if (owner)
1115 midi_devs[m]->owner = owner;
1117 hw_config->slots[1] = m;
1118 sequencer_init();
1121 static int reset_mpu401(struct mpu_config *devc)
1123 unsigned long flags;
1124 int ok, timeout, n;
1125 int timeout_limit;
1128 * Send the RESET command. Try again if no success at the first time.
1129 * (If the device is in the UART mode, it will not ack the reset cmd).
1132 ok = 0;
1134 timeout_limit = devc->initialized ? 30000 : 100000;
1135 devc->initialized = 1;
1137 for (n = 0; n < 2 && !ok; n++)
1139 for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1140 ok = output_ready(devc);
1142 write_command(devc, MPU_RESET); /*
1143 * Send MPU-401 RESET Command
1147 * Wait at least 25 msec. This method is not accurate so let's make the
1148 * loop bit longer. Cannot sleep since this is called during boot.
1151 for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1153 spin_lock_irqsave(&devc->lock,flags);
1154 if (input_avail(devc))
1155 if (read_data(devc) == MPU_ACK)
1156 ok = 1;
1157 spin_unlock_irqrestore(&devc->lock,flags);
1162 devc->m_state = ST_INIT;
1163 devc->m_ptr = 0;
1164 devc->m_left = 0;
1165 devc->last_status = 0;
1166 devc->uart_mode = 0;
1168 return ok;
1171 static void set_uart_mode(int dev, struct mpu_config *devc, int arg)
1173 if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1174 return;
1175 if ((devc->uart_mode == 0) == (arg == 0))
1176 return; /* Already set */
1177 reset_mpu401(devc); /* This exits the uart mode */
1179 if (arg)
1181 if (mpu_cmd(dev, UART_MODE_ON, 0) < 0)
1183 printk(KERN_ERR "mpu401: Can't enter UART mode\n");
1184 devc->uart_mode = 0;
1185 return;
1188 devc->uart_mode = arg;
1192 int probe_mpu401(struct address_info *hw_config)
1194 int ok = 0;
1195 struct mpu_config tmp_devc;
1197 if (check_region(hw_config->io_base, 2))
1199 printk(KERN_ERR "mpu401: I/O port %x already in use\n\n", hw_config->io_base);
1200 return 0;
1202 tmp_devc.base = hw_config->io_base;
1203 tmp_devc.irq = hw_config->irq;
1204 tmp_devc.initialized = 0;
1205 tmp_devc.opened = 0;
1206 tmp_devc.osp = hw_config->osp;
1208 if (hw_config->always_detect)
1209 return 1;
1211 if (inb(hw_config->io_base + 1) == 0xff)
1213 DDB(printk("MPU401: Port %x looks dead.\n", hw_config->io_base));
1214 return 0; /* Just bus float? */
1216 ok = reset_mpu401(&tmp_devc);
1218 if (!ok)
1220 DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
1222 return ok;
1225 void unload_mpu401(struct address_info *hw_config)
1227 void *p;
1228 int n=hw_config->slots[1];
1230 release_region(hw_config->io_base, 2);
1231 if (hw_config->always_detect == 0 && hw_config->irq > 0)
1232 free_irq(hw_config->irq, (void *)n);
1233 p=mpu401_synth_operations[n];
1234 sound_unload_mididev(n);
1235 sound_unload_timerdev(hw_config->slots[2]);
1236 if(p)
1237 kfree(p);
1240 /*****************************************************
1241 * Timer stuff
1242 ****************************************************/
1244 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1245 static volatile int curr_tempo, curr_timebase, hw_timebase;
1246 static int max_timebase = 8; /* 8*24=192 ppqn */
1247 static volatile unsigned long next_event_time;
1248 static volatile unsigned long curr_ticks, curr_clocks;
1249 static unsigned long prev_event_time;
1250 static int metronome_mode;
1252 static unsigned long clocks2ticks(unsigned long clocks)
1255 * The MPU-401 supports just a limited set of possible timebase values.
1256 * Since the applications require more choices, the driver has to
1257 * program the HW to do its best and to convert between the HW and
1258 * actual timebases.
1260 return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1263 static void set_timebase(int midi_dev, int val)
1265 int hw_val;
1267 if (val < 48)
1268 val = 48;
1269 if (val > 1000)
1270 val = 1000;
1272 hw_val = val;
1273 hw_val = (hw_val + 12) / 24;
1274 if (hw_val > max_timebase)
1275 hw_val = max_timebase;
1277 if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1279 printk(KERN_WARNING "mpu401: Can't set HW timebase to %d\n", hw_val * 24);
1280 return;
1282 hw_timebase = hw_val * 24;
1283 curr_timebase = val;
1287 static void tmr_reset(struct mpu_config *devc)
1289 unsigned long flags;
1291 spin_lock_irqsave(&devc->lock,flags);
1292 next_event_time = (unsigned long) -1;
1293 prev_event_time = 0;
1294 curr_ticks = curr_clocks = 0;
1295 spin_unlock_irqrestore(&devc->lock,flags);
1298 static void set_timer_mode(int midi_dev)
1300 if (timer_mode & TMR_MODE_CLS)
1301 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1302 else if (timer_mode & TMR_MODE_SMPTE)
1303 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1305 if (timer_mode & TMR_INTERNAL)
1307 mpu_cmd(midi_dev, 0x80, 0); /* Use MIDI sync */
1309 else
1311 if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1313 mpu_cmd(midi_dev, 0x82, 0); /* Use MIDI sync */
1314 mpu_cmd(midi_dev, 0x91, 0); /* Enable ext MIDI ctrl */
1316 else if (timer_mode & TMR_MODE_FSK)
1317 mpu_cmd(midi_dev, 0x81, 0); /* Use FSK sync */
1321 static void stop_metronome(int midi_dev)
1323 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1326 static void setup_metronome(int midi_dev)
1328 int numerator, denominator;
1329 int clks_per_click, num_32nds_per_beat;
1330 int beats_per_measure;
1332 numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1333 denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1334 clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1335 num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1336 beats_per_measure = (numerator * 4) >> denominator;
1338 if (!metronome_mode)
1339 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1340 else
1342 mpu_cmd(midi_dev, 0xE4, clks_per_click);
1343 mpu_cmd(midi_dev, 0xE6, beats_per_measure);
1344 mpu_cmd(midi_dev, 0x83, 0); /* Enable metronome without accents */
1348 static int mpu_start_timer(int midi_dev)
1350 struct mpu_config *devc= &dev_conf[midi_dev];
1352 tmr_reset(devc);
1353 set_timer_mode(midi_dev);
1355 if (tmr_running)
1356 return TIMER_NOT_ARMED; /* Already running */
1358 if (timer_mode & TMR_INTERNAL)
1360 mpu_cmd(midi_dev, 0x02, 0); /* Send MIDI start */
1361 tmr_running = 1;
1362 return TIMER_NOT_ARMED;
1364 else
1366 mpu_cmd(midi_dev, 0x35, 0); /* Enable mode messages to PC */
1367 mpu_cmd(midi_dev, 0x38, 0); /* Enable sys common messages to PC */
1368 mpu_cmd(midi_dev, 0x39, 0); /* Enable real time messages to PC */
1369 mpu_cmd(midi_dev, 0x97, 0); /* Enable system exclusive messages to PC */
1371 return TIMER_ARMED;
1374 static int mpu_timer_open(int dev, int mode)
1376 int midi_dev = sound_timer_devs[dev]->devlink;
1377 struct mpu_config *devc= &dev_conf[midi_dev];
1379 if (timer_open)
1380 return -EBUSY;
1382 tmr_reset(devc);
1383 curr_tempo = 50;
1384 mpu_cmd(midi_dev, 0xE0, 50);
1385 curr_timebase = hw_timebase = 120;
1386 set_timebase(midi_dev, 120);
1387 timer_open = 1;
1388 metronome_mode = 0;
1389 set_timer_mode(midi_dev);
1391 mpu_cmd(midi_dev, 0xe7, 0x04); /* Send all clocks to host */
1392 mpu_cmd(midi_dev, 0x95, 0); /* Enable clock to host */
1394 return 0;
1397 static void mpu_timer_close(int dev)
1399 int midi_dev = sound_timer_devs[dev]->devlink;
1401 timer_open = tmr_running = 0;
1402 mpu_cmd(midi_dev, 0x15, 0); /* Stop all */
1403 mpu_cmd(midi_dev, 0x94, 0); /* Disable clock to host */
1404 mpu_cmd(midi_dev, 0x8c, 0); /* Disable measure end messages to host */
1405 stop_metronome(midi_dev);
1408 static int mpu_timer_event(int dev, unsigned char *event)
1410 unsigned char command = event[1];
1411 unsigned long parm = *(unsigned int *) &event[4];
1412 int midi_dev = sound_timer_devs[dev]->devlink;
1414 switch (command)
1416 case TMR_WAIT_REL:
1417 parm += prev_event_time;
1418 case TMR_WAIT_ABS:
1419 if (parm > 0)
1421 long time;
1423 if (parm <= curr_ticks) /* It's the time */
1424 return TIMER_NOT_ARMED;
1425 time = parm;
1426 next_event_time = prev_event_time = time;
1428 return TIMER_ARMED;
1430 break;
1432 case TMR_START:
1433 if (tmr_running)
1434 break;
1435 return mpu_start_timer(midi_dev);
1437 case TMR_STOP:
1438 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1439 stop_metronome(midi_dev);
1440 tmr_running = 0;
1441 break;
1443 case TMR_CONTINUE:
1444 if (tmr_running)
1445 break;
1446 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1447 setup_metronome(midi_dev);
1448 tmr_running = 1;
1449 break;
1451 case TMR_TEMPO:
1452 if (parm)
1454 if (parm < 8)
1455 parm = 8;
1456 if (parm > 250)
1457 parm = 250;
1458 if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
1459 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) parm);
1460 curr_tempo = parm;
1462 break;
1464 case TMR_ECHO:
1465 seq_copy_to_input(event, 8);
1466 break;
1468 case TMR_TIMESIG:
1469 if (metronome_mode) /* Metronome enabled */
1471 metronome_mode = parm;
1472 setup_metronome(midi_dev);
1474 break;
1476 default:;
1478 return TIMER_NOT_ARMED;
1481 static unsigned long mpu_timer_get_time(int dev)
1483 if (!timer_open)
1484 return 0;
1486 return curr_ticks;
1489 static int mpu_timer_ioctl(int dev, unsigned int command, caddr_t arg)
1491 int midi_dev = sound_timer_devs[dev]->devlink;
1493 switch (command)
1495 case SNDCTL_TMR_SOURCE:
1497 int parm;
1499 parm = *(int *) arg;
1500 parm &= timer_caps;
1502 if (parm != 0)
1504 timer_mode = parm;
1506 if (timer_mode & TMR_MODE_CLS)
1507 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1508 else if (timer_mode & TMR_MODE_SMPTE)
1509 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1511 return (*(int *) arg = timer_mode);
1513 break;
1515 case SNDCTL_TMR_START:
1516 mpu_start_timer(midi_dev);
1517 return 0;
1519 case SNDCTL_TMR_STOP:
1520 tmr_running = 0;
1521 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1522 stop_metronome(midi_dev);
1523 return 0;
1525 case SNDCTL_TMR_CONTINUE:
1526 if (tmr_running)
1527 return 0;
1528 tmr_running = 1;
1529 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1530 return 0;
1532 case SNDCTL_TMR_TIMEBASE:
1534 int val;
1536 val = *(int *) arg;
1537 if (val)
1538 set_timebase(midi_dev, val);
1539 return (*(int *) arg = curr_timebase);
1541 break;
1543 case SNDCTL_TMR_TEMPO:
1545 int val;
1546 int ret;
1548 val = *(int *) arg;
1550 if (val)
1552 if (val < 8)
1553 val = 8;
1554 if (val > 250)
1555 val = 250;
1556 if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
1558 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
1559 return ret;
1561 curr_tempo = val;
1563 return (*(int *) arg = curr_tempo);
1565 break;
1567 case SNDCTL_SEQ_CTRLRATE:
1569 int val;
1571 val = *(int *) arg;
1572 if (val != 0) /* Can't change */
1573 return -EINVAL;
1574 return (*(int *) arg = ((curr_tempo * curr_timebase) + 30) / 60);
1576 break;
1578 case SNDCTL_SEQ_GETTIME:
1579 return (*(int *) arg = curr_ticks);
1581 case SNDCTL_TMR_METRONOME:
1582 metronome_mode = *(int *) arg;
1583 setup_metronome(midi_dev);
1584 return 0;
1586 default:;
1588 return -EINVAL;
1591 static void mpu_timer_arm(int dev, long time)
1593 if (time < 0)
1594 time = curr_ticks + 1;
1595 else if (time <= curr_ticks) /* It's the time */
1596 return;
1597 next_event_time = prev_event_time = time;
1598 return;
1601 static struct sound_timer_operations mpu_timer =
1603 owner: THIS_MODULE,
1604 info: {"MPU-401 Timer", 0},
1605 priority: 10, /* Priority */
1606 devlink: 0, /* Local device link */
1607 open: mpu_timer_open,
1608 close: mpu_timer_close,
1609 event: mpu_timer_event,
1610 get_time: mpu_timer_get_time,
1611 ioctl: mpu_timer_ioctl,
1612 arm_timer: mpu_timer_arm
1615 static void mpu_timer_interrupt(void)
1617 if (!timer_open)
1618 return;
1620 if (!tmr_running)
1621 return;
1623 curr_clocks++;
1624 curr_ticks = clocks2ticks(curr_clocks);
1626 if (curr_ticks >= next_event_time)
1628 next_event_time = (unsigned long) -1;
1629 sequencer_timer(0);
1633 static void timer_ext_event(struct mpu_config *devc, int event, int parm)
1635 int midi_dev = devc->devno;
1637 if (!devc->timer_flag)
1638 return;
1640 switch (event)
1642 case TMR_CLOCK:
1643 printk("<MIDI clk>");
1644 break;
1646 case TMR_START:
1647 printk("Ext MIDI start\n");
1648 if (!tmr_running)
1650 if (timer_mode & TMR_EXTERNAL)
1652 tmr_running = 1;
1653 setup_metronome(midi_dev);
1654 next_event_time = 0;
1655 STORE(SEQ_START_TIMER());
1658 break;
1660 case TMR_STOP:
1661 printk("Ext MIDI stop\n");
1662 if (timer_mode & TMR_EXTERNAL)
1664 tmr_running = 0;
1665 stop_metronome(midi_dev);
1666 STORE(SEQ_STOP_TIMER());
1668 break;
1670 case TMR_CONTINUE:
1671 printk("Ext MIDI continue\n");
1672 if (timer_mode & TMR_EXTERNAL)
1674 tmr_running = 1;
1675 setup_metronome(midi_dev);
1676 STORE(SEQ_CONTINUE_TIMER());
1678 break;
1680 case TMR_SPP:
1681 printk("Songpos: %d\n", parm);
1682 if (timer_mode & TMR_EXTERNAL)
1684 STORE(SEQ_SONGPOS(parm));
1686 break;
1690 static int mpu_timer_init(int midi_dev)
1692 struct mpu_config *devc;
1693 int n;
1695 devc = &dev_conf[midi_dev];
1697 if (timer_initialized)
1698 return -1; /* There is already a similar timer */
1700 timer_initialized = 1;
1702 mpu_timer.devlink = midi_dev;
1703 dev_conf[midi_dev].timer_flag = 1;
1705 n = sound_alloc_timerdev();
1706 if (n == -1)
1707 n = 0;
1708 sound_timer_devs[n] = &mpu_timer;
1710 if (devc->version < 0x20) /* Original MPU-401 */
1711 timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1712 else
1715 * The version number 2.0 is used (at least) by the
1716 * MusicQuest cards and the Roland Super-MPU.
1718 * MusicQuest has given a special meaning to the bits of the
1719 * revision number. The Super-MPU returns 0.
1722 if (devc->revision)
1723 timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1725 if (devc->revision & 0x02)
1726 timer_caps |= TMR_MODE_CLS;
1729 if (devc->revision & 0x40)
1730 max_timebase = 10; /* Has the 216 and 240 ppqn modes */
1733 timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1734 return n;
1738 EXPORT_SYMBOL(probe_mpu401);
1739 EXPORT_SYMBOL(attach_mpu401);
1740 EXPORT_SYMBOL(unload_mpu401);
1741 EXPORT_SYMBOL(intchk_mpu401);
1742 EXPORT_SYMBOL(mpuintr);
1744 static struct address_info cfg;
1746 static int __initdata io = -1;
1747 static int __initdata irq = -1;
1749 MODULE_PARM(irq, "i");
1750 MODULE_PARM(io, "i");
1752 int __init init_mpu401(void)
1754 /* Can be loaded either for module use or to provide functions
1755 to others */
1756 if (io != -1 && irq != -1) {
1757 cfg.irq = irq;
1758 cfg.io_base = io;
1759 if (probe_mpu401(&cfg) == 0)
1760 return -ENODEV;
1761 attach_mpu401(&cfg, THIS_MODULE);
1764 return 0;
1767 void __exit cleanup_mpu401(void)
1769 if (io != -1 && irq != -1) {
1770 /* Check for use by, for example, sscape driver */
1771 unload_mpu401(&cfg);
1775 module_init(init_mpu401);
1776 module_exit(cleanup_mpu401);
1778 #ifndef MODULE
1779 static int __init setup_mpu401(char *str)
1781 /* io, irq */
1782 int ints[3];
1784 str = get_options(str, ARRAY_SIZE(ints), ints);
1786 io = ints[1];
1787 irq = ints[2];
1789 return 1;
1792 __setup("mpu401=", setup_mpu401);
1793 #endif
1794 MODULE_LICENSE("GPL");