seccomp: introduce writer locking
[linux/fpc-iii.git] / sound / oss / mpu401.c
blob3bbc3ec5be823f23b91b6bf73a6b578bf43b9539
1 /*
2 * sound/oss/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
18 * Zwane Mwaikambo Changed attach/unload resource freeing
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
29 #include "sound_config.h"
31 #include "coproc.h"
32 #include "mpu401.h"
34 static int timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
36 struct mpu_config
38 int base; /*
39 * I/O base
41 int irq;
42 int opened; /*
43 * Open mode
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;
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;
77 #define DATAPORT(base) (base)
78 #define COMDPORT(base) (base+1)
79 #define STATPORT(base) (base+1)
82 static void mpu401_close(int dev);
84 static inline int mpu401_status(struct mpu_config *devc)
86 return inb(STATPORT(devc->base));
89 #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
90 #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
92 static inline void write_command(struct mpu_config *devc, unsigned char cmd)
94 outb(cmd, COMDPORT(devc->base));
97 static inline int read_data(struct mpu_config *devc)
99 return inb(DATAPORT(devc->base));
102 static inline void write_data(struct mpu_config *devc, unsigned char byte)
104 outb(byte, DATAPORT(devc->base));
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
113 static struct mpu_config dev_conf[MAX_MIDI_DEV];
115 static int n_mpu_devs;
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 static irqreturn_t mpuintr(int irq, void *dev_id)
438 struct mpu_config *devc;
439 int dev = (int)(unsigned long) dev_id;
440 int handled = 0;
442 devc = &dev_conf[dev];
444 if (input_avail(devc))
446 handled = 1;
447 if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
448 mpu401_input_loop(devc);
449 else
451 /* Dummy read (just to acknowledge the interrupt) */
452 read_data(devc);
455 return IRQ_RETVAL(handled);
458 static int mpu401_open(int dev, int mode,
459 void (*input) (int dev, unsigned char data),
460 void (*output) (int dev)
463 int err;
464 struct mpu_config *devc;
465 struct coproc_operations *coprocessor;
467 if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
468 return -ENXIO;
470 devc = &dev_conf[dev];
472 if (devc->opened)
473 return -EBUSY;
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.
481 if (!devc->initialized)
483 if (mpu401_status(devc) == 0xff) /* Bus float */
485 printk(KERN_ERR "mpu401: Device not initialized properly\n");
486 return -EIO;
488 reset_mpu401(devc);
491 if ( (coprocessor = midi_devs[dev]->coproc) != NULL )
493 if (!try_module_get(coprocessor->owner)) {
494 mpu401_close(dev);
495 return -ENODEV;
498 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
500 printk(KERN_WARNING "MPU-401: Can't access coprocessor device\n");
501 mpu401_close(dev);
502 return err;
506 set_uart_mode(dev, devc, 1);
507 devc->mode = MODE_MIDI;
508 devc->synthno = 0;
510 mpu401_input_loop(devc);
512 devc->inputintr = input;
513 devc->opened = mode;
515 return 0;
518 static void mpu401_close(int dev)
520 struct mpu_config *devc;
521 struct coproc_operations *coprocessor;
523 devc = &dev_conf[dev];
524 if (devc->uart_mode)
525 reset_mpu401(devc); /*
526 * This disables the UART mode
528 devc->mode = 0;
529 devc->inputintr = NULL;
531 coprocessor = midi_devs[dev]->coproc;
532 if (coprocessor) {
533 coprocessor->close(coprocessor->devc, COPR_MIDI);
534 module_put(coprocessor->owner);
536 devc->opened = 0;
539 static int mpu401_out(int dev, unsigned char midi_byte)
541 int timeout;
542 unsigned long flags;
544 struct mpu_config *devc;
546 devc = &dev_conf[dev];
549 * Sometimes it takes about 30000 loops before the output becomes ready
550 * (After reset). Normally it takes just about 10 loops.
553 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
555 spin_lock_irqsave(&devc->lock,flags);
556 if (!output_ready(devc))
558 printk(KERN_WARNING "mpu401: Send data timeout\n");
559 spin_unlock_irqrestore(&devc->lock,flags);
560 return 0;
562 write_data(devc, midi_byte);
563 spin_unlock_irqrestore(&devc->lock,flags);
564 return 1;
567 static int mpu401_command(int dev, mpu_command_rec * cmd)
569 int i, timeout, ok;
570 unsigned long flags;
571 struct mpu_config *devc;
573 devc = &dev_conf[dev];
575 if (devc->uart_mode) /*
576 * Not possible in UART mode
579 printk(KERN_WARNING "mpu401: commands not possible in the UART mode\n");
580 return -EINVAL;
583 * Test for input since pending input seems to block the output.
585 if (input_avail(devc))
586 mpu401_input_loop(devc);
589 * Sometimes it takes about 50000 loops before the output becomes ready
590 * (After reset). Normally it takes just about 10 loops.
593 timeout = 50000;
594 retry:
595 if (timeout-- <= 0)
597 printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
598 return -EIO;
600 spin_lock_irqsave(&devc->lock,flags);
602 if (!output_ready(devc))
604 spin_unlock_irqrestore(&devc->lock,flags);
605 goto retry;
607 write_command(devc, cmd->cmd);
609 ok = 0;
610 for (timeout = 50000; timeout > 0 && !ok; timeout--)
612 if (input_avail(devc))
614 if (devc->opened && devc->mode == MODE_SYNTH)
616 if (mpu_input_scanner(devc, read_data(devc)) == MPU_ACK)
617 ok = 1;
619 else
621 /* Device is not currently open. Use simpler method */
622 if (read_data(devc) == MPU_ACK)
623 ok = 1;
627 if (!ok)
629 spin_unlock_irqrestore(&devc->lock,flags);
630 return -EIO;
632 if (cmd->nr_args)
634 for (i = 0; i < cmd->nr_args; i++)
636 for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
638 if (!mpu401_out(dev, cmd->data[i]))
640 spin_unlock_irqrestore(&devc->lock,flags);
641 printk(KERN_WARNING "mpu401: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
642 return -EIO;
646 cmd->data[0] = 0;
648 if (cmd->nr_returns)
650 for (i = 0; i < cmd->nr_returns; i++)
652 ok = 0;
653 for (timeout = 5000; timeout > 0 && !ok; timeout--)
654 if (input_avail(devc))
656 cmd->data[i] = read_data(devc);
657 ok = 1;
659 if (!ok)
661 spin_unlock_irqrestore(&devc->lock,flags);
662 return -EIO;
666 spin_unlock_irqrestore(&devc->lock,flags);
667 return 0;
670 static int mpu_cmd(int dev, int cmd, int data)
672 int ret;
674 static mpu_command_rec rec;
676 rec.cmd = cmd & 0xff;
677 rec.nr_args = ((cmd & 0xf0) == 0xE0);
678 rec.nr_returns = ((cmd & 0xf0) == 0xA0);
679 rec.data[0] = data & 0xff;
681 if ((ret = mpu401_command(dev, &rec)) < 0)
682 return ret;
683 return (unsigned char) rec.data[0];
686 static int mpu401_prefix_cmd(int dev, unsigned char status)
688 struct mpu_config *devc = &dev_conf[dev];
690 if (devc->uart_mode)
691 return 1;
693 if (status < 0xf0)
695 if (mpu_cmd(dev, 0xD0, 0) < 0)
696 return 0;
697 return 1;
699 switch (status)
701 case 0xF0:
702 if (mpu_cmd(dev, 0xDF, 0) < 0)
703 return 0;
704 return 1;
706 default:
707 return 0;
711 static int mpu401_start_read(int dev)
713 return 0;
716 static int mpu401_end_read(int dev)
718 return 0;
721 static int mpu401_ioctl(int dev, unsigned cmd, void __user *arg)
723 struct mpu_config *devc;
724 mpu_command_rec rec;
725 int val, ret;
727 devc = &dev_conf[dev];
728 switch (cmd)
730 case SNDCTL_MIDI_MPUMODE:
731 if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
732 printk(KERN_WARNING "mpu401: Intelligent mode not supported by the HW\n");
733 return -EINVAL;
735 if (get_user(val, (int __user *)arg))
736 return -EFAULT;
737 set_uart_mode(dev, devc, !val);
738 return 0;
740 case SNDCTL_MIDI_MPUCMD:
741 if (copy_from_user(&rec, arg, sizeof(rec)))
742 return -EFAULT;
743 if ((ret = mpu401_command(dev, &rec)) < 0)
744 return ret;
745 if (copy_to_user(arg, &rec, sizeof(rec)))
746 return -EFAULT;
747 return 0;
749 default:
750 return -EINVAL;
754 static void mpu401_kick(int dev)
758 static int mpu401_buffer_status(int dev)
760 return 0; /*
761 * No data in buffers
765 static int mpu_synth_ioctl(int dev, unsigned int cmd, void __user *arg)
767 int midi_dev;
768 struct mpu_config *devc;
770 midi_dev = synth_devs[dev]->midi_dev;
772 if (midi_dev < 0 || midi_dev >= num_midis || midi_devs[midi_dev] == NULL)
773 return -ENXIO;
775 devc = &dev_conf[midi_dev];
777 switch (cmd)
780 case SNDCTL_SYNTH_INFO:
781 if (copy_to_user(arg, &mpu_synth_info[midi_dev],
782 sizeof(struct synth_info)))
783 return -EFAULT;
784 return 0;
786 case SNDCTL_SYNTH_MEMAVL:
787 return 0x7fffffff;
789 default:
790 return -EINVAL;
794 static int mpu_synth_open(int dev, int mode)
796 int midi_dev, err;
797 struct mpu_config *devc;
798 struct coproc_operations *coprocessor;
800 midi_dev = synth_devs[dev]->midi_dev;
802 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
803 return -ENXIO;
805 devc = &dev_conf[midi_dev];
808 * Verify that the device is really running.
809 * Some devices (such as Ensoniq SoundScape don't
810 * work before the on board processor (OBP) is initialized
811 * by downloading its microcode.
814 if (!devc->initialized)
816 if (mpu401_status(devc) == 0xff) /* Bus float */
818 printk(KERN_ERR "mpu401: Device not initialized properly\n");
819 return -EIO;
821 reset_mpu401(devc);
823 if (devc->opened)
824 return -EBUSY;
825 devc->mode = MODE_SYNTH;
826 devc->synthno = dev;
828 devc->inputintr = NULL;
830 coprocessor = midi_devs[midi_dev]->coproc;
831 if (coprocessor) {
832 if (!try_module_get(coprocessor->owner))
833 return -ENODEV;
835 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
837 printk(KERN_WARNING "mpu401: Can't access coprocessor device\n");
838 return err;
841 devc->opened = mode;
842 reset_mpu401(devc);
844 if (mode & OPEN_READ)
846 mpu_cmd(midi_dev, 0x8B, 0); /* Enable data in stop mode */
847 mpu_cmd(midi_dev, 0x34, 0); /* Return timing bytes in stop mode */
848 mpu_cmd(midi_dev, 0x87, 0); /* Enable pitch & controller */
850 return 0;
853 static void mpu_synth_close(int dev)
855 int midi_dev;
856 struct mpu_config *devc;
857 struct coproc_operations *coprocessor;
859 midi_dev = synth_devs[dev]->midi_dev;
861 devc = &dev_conf[midi_dev];
862 mpu_cmd(midi_dev, 0x15, 0); /* Stop recording, playback and MIDI */
863 mpu_cmd(midi_dev, 0x8a, 0); /* Disable data in stopped mode */
865 devc->inputintr = NULL;
867 coprocessor = midi_devs[midi_dev]->coproc;
868 if (coprocessor) {
869 coprocessor->close(coprocessor->devc, COPR_MIDI);
870 module_put(coprocessor->owner);
872 devc->opened = 0;
873 devc->mode = 0;
876 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
877 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
878 #include "midi_synth.h"
880 static struct synth_operations mpu401_synth_proto =
882 .owner = THIS_MODULE,
883 .id = "MPU401",
884 .info = NULL,
885 .midi_dev = 0,
886 .synth_type = SYNTH_TYPE_MIDI,
887 .synth_subtype = 0,
888 .open = mpu_synth_open,
889 .close = mpu_synth_close,
890 .ioctl = mpu_synth_ioctl,
891 .kill_note = midi_synth_kill_note,
892 .start_note = midi_synth_start_note,
893 .set_instr = midi_synth_set_instr,
894 .reset = midi_synth_reset,
895 .hw_control = midi_synth_hw_control,
896 .load_patch = midi_synth_load_patch,
897 .aftertouch = midi_synth_aftertouch,
898 .controller = midi_synth_controller,
899 .panning = midi_synth_panning,
900 .bender = midi_synth_bender,
901 .setup_voice = midi_synth_setup_voice,
902 .send_sysex = midi_synth_send_sysex
905 static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
907 static struct midi_operations mpu401_midi_proto =
909 .owner = THIS_MODULE,
910 .info = {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
911 .in_info = {0},
912 .open = mpu401_open,
913 .close = mpu401_close,
914 .ioctl = mpu401_ioctl,
915 .outputc = mpu401_out,
916 .start_read = mpu401_start_read,
917 .end_read = mpu401_end_read,
918 .kick = mpu401_kick,
919 .buffer_status = mpu401_buffer_status,
920 .prefix_cmd = mpu401_prefix_cmd
923 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
925 static void mpu401_chk_version(int n, struct mpu_config *devc)
927 int tmp;
929 devc->version = devc->revision = 0;
931 tmp = mpu_cmd(n, 0xAC, 0);
932 if (tmp < 0)
933 return;
934 if ((tmp & 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
935 return;
936 devc->version = tmp;
938 if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0) {
939 devc->version = 0;
940 return;
942 devc->revision = tmp;
945 int attach_mpu401(struct address_info *hw_config, struct module *owner)
947 unsigned long flags;
948 char revision_char;
950 int m, ret;
951 struct mpu_config *devc;
953 hw_config->slots[1] = -1;
954 m = sound_alloc_mididev();
955 if (m == -1)
957 printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
958 ret = -ENOMEM;
959 goto out_err;
961 devc = &dev_conf[m];
962 devc->base = hw_config->io_base;
963 devc->osp = hw_config->osp;
964 devc->irq = hw_config->irq;
965 devc->opened = 0;
966 devc->uart_mode = 0;
967 devc->initialized = 0;
968 devc->version = 0;
969 devc->revision = 0;
970 devc->capabilities = 0;
971 devc->timer_flag = 0;
972 devc->m_busy = 0;
973 devc->m_state = ST_INIT;
974 devc->shared_irq = hw_config->always_detect;
975 devc->irq = hw_config->irq;
976 spin_lock_init(&devc->lock);
978 if (devc->irq < 0)
980 devc->irq *= -1;
981 devc->shared_irq = 1;
984 if (!hw_config->always_detect)
986 /* Verify the hardware again */
987 if (!reset_mpu401(devc))
989 printk(KERN_WARNING "mpu401: Device didn't respond\n");
990 ret = -ENODEV;
991 goto out_mididev;
993 if (!devc->shared_irq)
995 if (request_irq(devc->irq, mpuintr, 0, "mpu401",
996 hw_config) < 0)
998 printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);
999 ret = -ENOMEM;
1000 goto out_mididev;
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);
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 */
1016 mpu401_synth_operations[m] = kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
1018 if (mpu401_synth_operations[m] == NULL)
1020 printk(KERN_ERR "mpu401: Can't allocate memory\n");
1021 ret = -ENOMEM;
1022 goto out_irq;
1024 if (!(devc->capabilities & MPU_CAP_INTLG)) /* No intelligent mode */
1026 memcpy((char *) mpu401_synth_operations[m],
1027 (char *) &std_midi_synth,
1028 sizeof(struct synth_operations));
1030 else
1032 memcpy((char *) mpu401_synth_operations[m],
1033 (char *) &mpu401_synth_proto,
1034 sizeof(struct synth_operations));
1036 if (owner)
1037 mpu401_synth_operations[m]->owner = owner;
1039 memcpy((char *) &mpu401_midi_operations[m],
1040 (char *) &mpu401_midi_proto,
1041 sizeof(struct midi_operations));
1043 mpu401_midi_operations[m].converter = mpu401_synth_operations[m];
1045 memcpy((char *) &mpu_synth_info[m],
1046 (char *) &mpu_synth_info_proto,
1047 sizeof(struct synth_info));
1049 n_mpu_devs++;
1051 if (devc->version == 0x20 && devc->revision >= 0x07) /* MusicQuest interface */
1053 int ports = (devc->revision & 0x08) ? 32 : 16;
1055 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1056 MPU_CAP_CLS | MPU_CAP_2PORT;
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);
1064 else
1066 revision_char = devc->revision ? devc->revision + '@' : ' ';
1067 if ((int) devc->revision > ('Z' - '@'))
1068 revision_char = '+';
1070 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
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);
1083 strcpy(mpu401_midi_operations[m].info.name,
1084 mpu_synth_info[m].name);
1086 conf_printf(mpu_synth_info[m].name, hw_config);
1088 mpu401_synth_operations[m]->midi_dev = devc->devno = m;
1089 mpu401_synth_operations[devc->devno]->info = &mpu_synth_info[devc->devno];
1091 if (devc->capabilities & MPU_CAP_INTLG) /* Intelligent mode */
1092 hw_config->slots[2] = mpu_timer_init(m);
1094 midi_devs[m] = &mpu401_midi_operations[devc->devno];
1096 if (owner)
1097 midi_devs[m]->owner = owner;
1099 hw_config->slots[1] = m;
1100 sequencer_init();
1102 return 0;
1104 out_irq:
1105 free_irq(devc->irq, hw_config);
1106 out_mididev:
1107 sound_unload_mididev(m);
1108 out_err:
1109 release_region(hw_config->io_base, 2);
1110 return ret;
1113 static int reset_mpu401(struct mpu_config *devc)
1115 unsigned long flags;
1116 int ok, timeout, n;
1117 int timeout_limit;
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).
1124 ok = 0;
1126 timeout_limit = devc->initialized ? 30000 : 100000;
1127 devc->initialized = 1;
1129 for (n = 0; n < 2 && !ok; n++)
1131 for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1132 ok = output_ready(devc);
1134 write_command(devc, MPU_RESET); /*
1135 * Send MPU-401 RESET Command
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.
1143 for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
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);
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;
1160 return ok;
1163 static void set_uart_mode(int dev, struct mpu_config *devc, int arg)
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 */
1171 if (arg)
1173 if (mpu_cmd(dev, UART_MODE_ON, 0) < 0)
1175 printk(KERN_ERR "mpu401: Can't enter UART mode\n");
1176 devc->uart_mode = 0;
1177 return;
1180 devc->uart_mode = arg;
1184 int probe_mpu401(struct address_info *hw_config, struct resource *ports)
1186 int ok = 0;
1187 struct mpu_config tmp_devc;
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;
1195 if (hw_config->always_detect)
1196 return 1;
1198 if (inb(hw_config->io_base + 1) == 0xff)
1200 DDB(printk("MPU401: Port %x looks dead.\n", hw_config->io_base));
1201 return 0; /* Just bus float? */
1203 ok = reset_mpu401(&tmp_devc);
1205 if (!ok)
1207 DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
1209 return ok;
1212 void unload_mpu401(struct address_info *hw_config)
1214 void *p;
1215 int n=hw_config->slots[1];
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);
1228 /*****************************************************
1229 * Timer stuff
1230 ****************************************************/
1232 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1233 static volatile int curr_tempo, curr_timebase, hw_timebase;
1234 static int max_timebase = 8; /* 8*24=192 ppqn */
1235 static volatile unsigned long next_event_time;
1236 static volatile unsigned long curr_ticks, curr_clocks;
1237 static unsigned long prev_event_time;
1238 static int metronome_mode;
1240 static unsigned long clocks2ticks(unsigned long clocks)
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.
1248 return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1251 static void set_timebase(int midi_dev, int val)
1253 int hw_val;
1255 if (val < 48)
1256 val = 48;
1257 if (val > 1000)
1258 val = 1000;
1260 hw_val = val;
1261 hw_val = (hw_val + 12) / 24;
1262 if (hw_val > max_timebase)
1263 hw_val = max_timebase;
1265 if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1267 printk(KERN_WARNING "mpu401: Can't set HW timebase to %d\n", hw_val * 24);
1268 return;
1270 hw_timebase = hw_val * 24;
1271 curr_timebase = val;
1275 static void tmr_reset(struct mpu_config *devc)
1277 unsigned long flags;
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);
1286 static void set_timer_mode(int midi_dev)
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 */
1293 if (timer_mode & TMR_INTERNAL)
1295 mpu_cmd(midi_dev, 0x80, 0); /* Use MIDI sync */
1297 else
1299 if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1301 mpu_cmd(midi_dev, 0x82, 0); /* Use MIDI sync */
1302 mpu_cmd(midi_dev, 0x91, 0); /* Enable ext MIDI ctrl */
1304 else if (timer_mode & TMR_MODE_FSK)
1305 mpu_cmd(midi_dev, 0x81, 0); /* Use FSK sync */
1309 static void stop_metronome(int midi_dev)
1311 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1314 static void setup_metronome(int midi_dev)
1316 int numerator, denominator;
1317 int clks_per_click, num_32nds_per_beat;
1318 int beats_per_measure;
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;
1326 if (!metronome_mode)
1327 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1328 else
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 */
1336 static int mpu_start_timer(int midi_dev)
1338 struct mpu_config *devc= &dev_conf[midi_dev];
1340 tmr_reset(devc);
1341 set_timer_mode(midi_dev);
1343 if (tmr_running)
1344 return TIMER_NOT_ARMED; /* Already running */
1346 if (timer_mode & TMR_INTERNAL)
1348 mpu_cmd(midi_dev, 0x02, 0); /* Send MIDI start */
1349 tmr_running = 1;
1350 return TIMER_NOT_ARMED;
1352 else
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 */
1359 return TIMER_ARMED;
1362 static int mpu_timer_open(int dev, int mode)
1364 int midi_dev = sound_timer_devs[dev]->devlink;
1365 struct mpu_config *devc= &dev_conf[midi_dev];
1367 if (timer_open)
1368 return -EBUSY;
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);
1379 mpu_cmd(midi_dev, 0xe7, 0x04); /* Send all clocks to host */
1380 mpu_cmd(midi_dev, 0x95, 0); /* Enable clock to host */
1382 return 0;
1385 static void mpu_timer_close(int dev)
1387 int midi_dev = sound_timer_devs[dev]->devlink;
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);
1396 static int mpu_timer_event(int dev, unsigned char *event)
1398 unsigned char command = event[1];
1399 unsigned long parm = *(unsigned int *) &event[4];
1400 int midi_dev = sound_timer_devs[dev]->devlink;
1402 switch (command)
1404 case TMR_WAIT_REL:
1405 parm += prev_event_time;
1406 case TMR_WAIT_ABS:
1407 if (parm > 0)
1409 long time;
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;
1416 return TIMER_ARMED;
1418 break;
1420 case TMR_START:
1421 if (tmr_running)
1422 break;
1423 return mpu_start_timer(midi_dev);
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;
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;
1439 case TMR_TEMPO:
1440 if (parm)
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;
1450 break;
1452 case TMR_ECHO:
1453 seq_copy_to_input(event, 8);
1454 break;
1456 case TMR_TIMESIG:
1457 if (metronome_mode) /* Metronome enabled */
1459 metronome_mode = parm;
1460 setup_metronome(midi_dev);
1462 break;
1464 default:;
1466 return TIMER_NOT_ARMED;
1469 static unsigned long mpu_timer_get_time(int dev)
1471 if (!timer_open)
1472 return 0;
1474 return curr_ticks;
1477 static int mpu_timer_ioctl(int dev, unsigned int command, void __user *arg)
1479 int midi_dev = sound_timer_devs[dev]->devlink;
1480 int __user *p = (int __user *)arg;
1482 switch (command)
1484 case SNDCTL_TMR_SOURCE:
1486 int parm;
1488 if (get_user(parm, p))
1489 return -EFAULT;
1490 parm &= timer_caps;
1492 if (parm != 0)
1494 timer_mode = parm;
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 */
1501 if (put_user(timer_mode, p))
1502 return -EFAULT;
1503 return timer_mode;
1505 break;
1507 case SNDCTL_TMR_START:
1508 mpu_start_timer(midi_dev);
1509 return 0;
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;
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;
1524 case SNDCTL_TMR_TIMEBASE:
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;
1535 break;
1537 case SNDCTL_TMR_TEMPO:
1539 int val;
1540 int ret;
1542 if (get_user(val, p))
1543 return -EFAULT;
1545 if (val)
1547 if (val < 8)
1548 val = 8;
1549 if (val > 250)
1550 val = 250;
1551 if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
1553 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
1554 return ret;
1556 curr_tempo = val;
1558 if (put_user(curr_tempo, p))
1559 return -EFAULT;
1560 return curr_tempo;
1562 break;
1564 case SNDCTL_SEQ_CTRLRATE:
1566 int val;
1567 if (get_user(val, p))
1568 return -EFAULT;
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;
1577 break;
1579 case SNDCTL_SEQ_GETTIME:
1580 if (put_user(curr_ticks, p))
1581 return -EFAULT;
1582 return curr_ticks;
1584 case SNDCTL_TMR_METRONOME:
1585 if (get_user(metronome_mode, p))
1586 return -EFAULT;
1587 setup_metronome(midi_dev);
1588 return 0;
1590 default:;
1592 return -EINVAL;
1595 static void mpu_timer_arm(int dev, long time)
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;
1605 static struct sound_timer_operations mpu_timer =
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
1619 static void mpu_timer_interrupt(void)
1621 if (!timer_open)
1622 return;
1624 if (!tmr_running)
1625 return;
1627 curr_clocks++;
1628 curr_ticks = clocks2ticks(curr_clocks);
1630 if (curr_ticks >= next_event_time)
1632 next_event_time = (unsigned long) -1;
1633 sequencer_timer(0);
1637 static void timer_ext_event(struct mpu_config *devc, int event, int parm)
1639 int midi_dev = devc->devno;
1641 if (!devc->timer_flag)
1642 return;
1644 switch (event)
1646 case TMR_CLOCK:
1647 printk("<MIDI clk>");
1648 break;
1650 case TMR_START:
1651 printk("Ext MIDI start\n");
1652 if (!tmr_running)
1654 if (timer_mode & TMR_EXTERNAL)
1656 tmr_running = 1;
1657 setup_metronome(midi_dev);
1658 next_event_time = 0;
1659 STORE(SEQ_START_TIMER());
1662 break;
1664 case TMR_STOP:
1665 printk("Ext MIDI stop\n");
1666 if (timer_mode & TMR_EXTERNAL)
1668 tmr_running = 0;
1669 stop_metronome(midi_dev);
1670 STORE(SEQ_STOP_TIMER());
1672 break;
1674 case TMR_CONTINUE:
1675 printk("Ext MIDI continue\n");
1676 if (timer_mode & TMR_EXTERNAL)
1678 tmr_running = 1;
1679 setup_metronome(midi_dev);
1680 STORE(SEQ_CONTINUE_TIMER());
1682 break;
1684 case TMR_SPP:
1685 printk("Songpos: %d\n", parm);
1686 if (timer_mode & TMR_EXTERNAL)
1688 STORE(SEQ_SONGPOS(parm));
1690 break;
1694 static int mpu_timer_init(int midi_dev)
1696 struct mpu_config *devc;
1697 int n;
1699 devc = &dev_conf[midi_dev];
1701 if (timer_initialized)
1702 return -1; /* There is already a similar timer */
1704 timer_initialized = 1;
1706 mpu_timer.devlink = midi_dev;
1707 dev_conf[midi_dev].timer_flag = 1;
1709 n = sound_alloc_timerdev();
1710 if (n == -1)
1711 n = 0;
1712 sound_timer_devs[n] = &mpu_timer;
1714 if (devc->version < 0x20) /* Original MPU-401 */
1715 timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1716 else
1719 * The version number 2.0 is used (at least) by the
1720 * MusicQuest cards and the Roland Super-MPU.
1722 * MusicQuest has given a special meaning to the bits of the
1723 * revision number. The Super-MPU returns 0.
1726 if (devc->revision)
1727 timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1729 if (devc->revision & 0x02)
1730 timer_caps |= TMR_MODE_CLS;
1733 if (devc->revision & 0x40)
1734 max_timebase = 10; /* Has the 216 and 240 ppqn modes */
1737 timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1738 return n;
1742 EXPORT_SYMBOL(probe_mpu401);
1743 EXPORT_SYMBOL(attach_mpu401);
1744 EXPORT_SYMBOL(unload_mpu401);
1746 static struct address_info cfg;
1748 static int io = -1;
1749 static int irq = -1;
1751 module_param(irq, int, 0);
1752 module_param(io, int, 0);
1754 static int __init init_mpu401(void)
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;
1770 if ((ret = attach_mpu401(&cfg, THIS_MODULE)))
1771 return ret;
1774 return 0;
1777 static void __exit cleanup_mpu401(void)
1779 if (io != -1 && irq != -1) {
1780 /* Check for use by, for example, sscape driver */
1781 unload_mpu401(&cfg);
1785 module_init(init_mpu401);
1786 module_exit(cleanup_mpu401);
1788 #ifndef MODULE
1789 static int __init setup_mpu401(char *str)
1791 /* io, irq */
1792 int ints[3];
1794 str = get_options(str, ARRAY_SIZE(ints), ints);
1796 io = ints[1];
1797 irq = ints[2];
1799 return 1;
1802 __setup("mpu401=", setup_mpu401);
1803 #endif
1804 MODULE_LICENSE("GPL");