Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / sound / mpu401.c
blobe6bb002e030dbc06c75db742a25c9567ab86a49b
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
19 #include <linux/module.h>
20 #include <linux/init.h>
22 #define USE_SEQ_MACROS
23 #define USE_SIMPLE_MACROS
25 #include"sound_config.h"
27 #include"coproc.h"
28 #include"mpu401.h"
30 static int timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
32 struct mpu_config
34 int base;/*
35 * I/O base
37 int irq;
38 int opened;/*
39 * Open mode
41 int devno;
42 int synthno;
43 int uart_mode;
44 int initialized;
45 int mode;
46 #define MODE_MIDI 1
47 #define MODE_SYNTH 2
48 unsigned char version, revision;
49 unsigned int capabilities;
50 #define MPU_CAP_INTLG 0x10000000
51 #define MPU_CAP_SYNC 0x00000010
52 #define MPU_CAP_FSK 0x00000020
53 #define MPU_CAP_CLS 0x00000040
54 #define MPU_CAP_SMPTE 0x00000080
55 #define MPU_CAP_2PORT 0x00000001
56 int timer_flag;
58 #define MBUF_MAX 10
59 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
60 {printk("MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
61 int m_busy;
62 unsigned char m_buf[MBUF_MAX];
63 int m_ptr;
64 int m_state;
65 int m_left;
66 unsigned char last_status;
67 void(*inputintr) (int dev,unsigned char data);
68 int shared_irq;
69 int*osp;
72 #define DATAPORT(base) (base)
73 #define COMDPORT(base) (base+1)
74 #define STATPORT(base) (base+1)
76 static intmpu401_status(struct mpu_config *devc)
78 returninb(STATPORT(devc->base));
81 #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
82 #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
84 static voidwrite_command(struct mpu_config *devc,unsigned char cmd)
86 outb(cmd,COMDPORT(devc->base));
89 static intread_data(struct mpu_config *devc)
91 returninb(DATAPORT(devc->base));
94 static voidwrite_data(struct mpu_config *devc,unsigned char byte)
96 outb(byte,DATAPORT(devc->base));
99 #define OUTPUT_READY 0x40
100 #define INPUT_AVAIL 0x80
101 #define MPU_ACK 0xFE
102 #define MPU_RESET 0xFF
103 #define UART_MODE_ON 0x3F
105 static struct mpu_config dev_conf[MAX_MIDI_DEV] =
110 static int n_mpu_devs =0;
112 static intreset_mpu401(struct mpu_config *devc);
113 static voidset_uart_mode(int dev,struct mpu_config *devc,int arg);
115 static intmpu_timer_init(int midi_dev);
116 static voidmpu_timer_interrupt(void);
117 static voidtimer_ext_event(struct mpu_config *devc,int event,int parm);
119 static struct synth_info mpu_synth_info_proto = {
120 "MPU-401 MIDI interface",
122 SYNTH_TYPE_MIDI,
123 MIDI_TYPE_MPU401,
124 0,128,
125 0,128,
126 SYNTH_CAP_INPUT
129 static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
132 * States for the input scanner
135 #define ST_INIT 0/* Ready for timing byte or msg */
136 #define ST_TIMED 1/* Leading timing byte rcvd */
137 #define ST_DATABYTE 2/* Waiting for (nr_left) data bytes */
139 #define ST_SYSMSG 100/* System message (sysx etc). */
140 #define ST_SYSEX 101/* System exclusive msg */
141 #define ST_MTC 102/* Midi Time Code (MTC) qframe msg */
142 #define ST_SONGSEL 103/* Song select */
143 #define ST_SONGPOS 104/* Song position pointer */
145 static unsigned char len_tab[] =/* # of data bytes following a status
148 2,/* 8x */
149 2,/* 9x */
150 2,/* Ax */
151 2,/* Bx */
152 1,/* Cx */
153 1,/* Dx */
154 2,/* Ex */
155 0/* Fx */
158 #define STORE(cmd) \
160 int len; \
161 unsigned char obuf[8]; \
162 cmd; \
163 seq_input_event(obuf, len); \
166 #define _seqbuf obuf
167 #define _seqbufptr 0
168 #define _SEQ_ADVBUF(x) len=x
170 static intmpu_input_scanner(struct mpu_config *devc,unsigned char midic)
173 switch(devc->m_state)
175 case ST_INIT:
176 switch(midic)
178 case0xf8:
179 /* Timer overflow */
180 break;
182 case0xfc:
183 printk("<all end>");
184 break;
186 case0xfd:
187 if(devc->timer_flag)
188 mpu_timer_interrupt();
189 break;
191 case0xfe:
192 return MPU_ACK;
194 case0xf0:
195 case0xf1:
196 case0xf2:
197 case0xf3:
198 case0xf4:
199 case0xf5:
200 case0xf6:
201 case0xf7:
202 printk("<Trk data rq #%d>", midic &0x0f);
203 break;
205 case0xf9:
206 printk("<conductor rq>");
207 break;
209 case0xff:
210 devc->m_state = ST_SYSMSG;
211 break;
213 default:
214 if(midic <=0xef)
216 /* printk( "mpu time: %d ", midic); */
217 devc->m_state = ST_TIMED;
219 else
220 printk("<MPU: Unknown event %02x> ", midic);
222 break;
224 case ST_TIMED:
226 int msg = ((int) (midic &0xf0) >>4);
228 devc->m_state = ST_DATABYTE;
230 if(msg <8)/* Data byte */
232 /* printk( "midi msg (running status) "); */
233 msg = ((int) (devc->last_status &0xf0) >>4);
234 msg -=8;
235 devc->m_left = len_tab[msg] -1;
237 devc->m_ptr =2;
238 devc->m_buf[0] = devc->last_status;
239 devc->m_buf[1] = midic;
241 if(devc->m_left <=0)
243 devc->m_state = ST_INIT;
244 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
245 devc->m_ptr =0;
248 else if(msg ==0xf)/* MPU MARK */
250 devc->m_state = ST_INIT;
252 switch(midic)
254 case0xf8:
255 /* printk( "NOP "); */
256 break;
258 case0xf9:
259 /* printk( "meas end "); */
260 break;
262 case0xfc:
263 /* printk( "data end "); */
264 break;
266 default:
267 printk("Unknown MPU mark %02x\n", midic);
270 else
272 devc->last_status = midic;
273 /* printk( "midi msg "); */
274 msg -=8;
275 devc->m_left = len_tab[msg];
277 devc->m_ptr =1;
278 devc->m_buf[0] = midic;
280 if(devc->m_left <=0)
282 devc->m_state = ST_INIT;
283 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
284 devc->m_ptr =0;
288 break;
290 case ST_SYSMSG:
291 switch(midic)
293 case0xf0:
294 printk("<SYX>");
295 devc->m_state = ST_SYSEX;
296 break;
298 case0xf1:
299 devc->m_state = ST_MTC;
300 break;
302 case0xf2:
303 devc->m_state = ST_SONGPOS;
304 devc->m_ptr =0;
305 break;
307 case0xf3:
308 devc->m_state = ST_SONGSEL;
309 break;
311 case0xf6:
312 /* printk( "tune_request\n"); */
313 devc->m_state = ST_INIT;
316 * Real time messages
318 case0xf8:
319 /* midi clock */
320 devc->m_state = ST_INIT;
321 timer_ext_event(devc, TMR_CLOCK,0);
322 break;
324 case0xfA:
325 devc->m_state = ST_INIT;
326 timer_ext_event(devc, TMR_START,0);
327 break;
329 case0xFB:
330 devc->m_state = ST_INIT;
331 timer_ext_event(devc, TMR_CONTINUE,0);
332 break;
334 case0xFC:
335 devc->m_state = ST_INIT;
336 timer_ext_event(devc, TMR_STOP,0);
337 break;
339 case0xFE:
340 /* active sensing */
341 devc->m_state = ST_INIT;
342 break;
344 case0xff:
345 /* printk( "midi hard reset"); */
346 devc->m_state = ST_INIT;
347 break;
349 default:
350 printk("unknown MIDI sysmsg %0x\n", midic);
351 devc->m_state = ST_INIT;
353 break;
355 case ST_MTC:
356 devc->m_state = ST_INIT;
357 printk("MTC frame %x02\n", midic);
358 break;
360 case ST_SYSEX:
361 if(midic ==0xf7)
363 printk("<EOX>");
364 devc->m_state = ST_INIT;
366 else
367 printk("%02x ", midic);
368 break;
370 case ST_SONGPOS:
371 BUFTEST(devc);
372 devc->m_buf[devc->m_ptr++] = midic;
373 if(devc->m_ptr ==2)
375 devc->m_state = ST_INIT;
376 devc->m_ptr =0;
377 timer_ext_event(devc, TMR_SPP,
378 ((devc->m_buf[1] &0x7f) <<7) |
379 (devc->m_buf[0] &0x7f));
381 break;
383 case ST_DATABYTE:
384 BUFTEST(devc);
385 devc->m_buf[devc->m_ptr++] = midic;
386 if((--devc->m_left) <=0)
388 devc->m_state = ST_INIT;
389 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
390 devc->m_ptr =0;
392 break;
394 default:
395 printk("Bad state %d ", devc->m_state);
396 devc->m_state = ST_INIT;
398 return1;
401 static voidmpu401_input_loop(struct mpu_config *devc)
403 unsigned long flags;
404 int busy;
405 int n;
407 save_flags(flags);
408 cli();
409 busy = devc->m_busy;
410 devc->m_busy =1;
411 restore_flags(flags);
413 if(busy)/* Already inside the scanner */
414 return;
416 n =50;
418 while(input_avail(devc) && n-- >0)
420 unsigned char c =read_data(devc);
422 if(devc->mode == MODE_SYNTH)
424 mpu_input_scanner(devc, c);
426 else if(devc->opened & OPEN_READ && devc->inputintr != NULL)
427 devc->inputintr(devc->devno, c);
429 devc->m_busy =0;
432 intintchk_mpu401(void*dev_id)
434 struct mpu_config *devc;
435 int dev = (int) dev_id;
437 devc = &dev_conf[dev];
438 returninput_avail(devc);
441 voidmpuintr(int irq,void*dev_id,struct pt_regs *dummy)
443 struct mpu_config *devc;
444 int dev = (int) dev_id;
446 sti();
447 devc = &dev_conf[dev];
449 if(input_avail(devc))
451 if(devc->base !=0&& (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
452 mpu401_input_loop(devc);
453 else
455 /* Dummy read (just to acknowledge the interrupt) */
456 read_data(devc);
461 static intmpu401_open(int dev,int mode,
462 void(*input) (int dev,unsigned char data),
463 void(*output) (int dev)
466 int err;
467 struct mpu_config *devc;
469 if(dev <0|| dev >= num_midis || midi_devs[dev] == NULL)
470 return-ENXIO;
472 devc = &dev_conf[dev];
474 if(devc->opened)
475 return-EBUSY;
477 * Verify that the device is really running.
478 * Some devices (such as Ensoniq SoundScape don't
479 * work before the on board processor (OBP) is initialized
480 * by downloading its microcode.
483 if(!devc->initialized)
485 if(mpu401_status(devc) ==0xff)/* Bus float */
487 printk(KERN_ERR "mpu401: Device not initialized properly\n");
488 return-EIO;
490 reset_mpu401(devc);
493 if(midi_devs[dev]->coproc)
495 if((err = midi_devs[dev]->coproc->
496 open(midi_devs[dev]->coproc->devc, COPR_MIDI)) <0)
498 printk("MPU-401: Can't access coprocessor device\n");
499 return err;
503 set_uart_mode(dev, devc,1);
504 devc->mode = MODE_MIDI;
505 devc->synthno =0;
507 mpu401_input_loop(devc);
509 devc->inputintr = input;
510 devc->opened = mode;
512 return0;
515 static voidmpu401_close(int dev)
517 struct mpu_config *devc;
519 devc = &dev_conf[dev];
520 if(devc->uart_mode)
521 reset_mpu401(devc);/*
522 * This disables the UART mode
524 devc->mode =0;
525 devc->inputintr = NULL;
527 if(midi_devs[dev]->coproc)
528 midi_devs[dev]->coproc->close(midi_devs[dev]->coproc->devc, COPR_MIDI);
529 devc->opened =0;
532 static intmpu401_out(int dev,unsigned char midi_byte)
534 int timeout;
535 unsigned long flags;
537 struct mpu_config *devc;
539 devc = &dev_conf[dev];
542 * Sometimes it takes about 30000 loops before the output becomes ready
543 * (After reset). Normally it takes just about 10 loops.
546 for(timeout =30000; timeout >0&& !output_ready(devc); timeout--);
548 save_flags(flags);
549 cli();
550 if(!output_ready(devc))
552 printk(KERN_WARNING "mpu401: Send data timeout\n");
553 restore_flags(flags);
554 return0;
556 write_data(devc, midi_byte);
557 restore_flags(flags);
558 return1;
561 static intmpu401_command(int dev, mpu_command_rec * cmd)
563 int i, timeout, ok;
564 int ret =0;
565 unsigned long flags;
566 struct mpu_config *devc;
568 devc = &dev_conf[dev];
570 if(devc->uart_mode)/*
571 * Not possible in UART mode
574 printk(KERN_WARNING "mpu401: commands not possible in the UART mode\n");
575 return-EINVAL;
578 * Test for input since pending input seems to block the output.
580 if(input_avail(devc))
581 mpu401_input_loop(devc);
584 * Sometimes it takes about 50000 loops before the output becomes ready
585 * (After reset). Normally it takes just about 10 loops.
588 timeout =50000;
589 retry:
590 if(timeout-- <=0)
592 printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
593 return-EIO;
595 save_flags(flags);
596 cli();
598 if(!output_ready(devc))
600 restore_flags(flags);
601 goto retry;
603 write_command(devc, cmd->cmd);
605 ok =0;
606 for(timeout =50000; timeout >0&& !ok; timeout--)
608 if(input_avail(devc))
610 if(devc->opened && devc->mode == MODE_SYNTH)
612 if(mpu_input_scanner(devc,read_data(devc)) == MPU_ACK)
613 ok =1;
615 else
617 /* Device is not currently open. Use simpler method */
618 if(read_data(devc) == MPU_ACK)
619 ok =1;
623 if(!ok)
625 restore_flags(flags);
626 return-EIO;
628 if(cmd->nr_args)
630 for(i =0; i < cmd->nr_args; i++)
632 for(timeout =3000; timeout >0&& !output_ready(devc); timeout--);
634 if(!mpu401_out(dev, cmd->data[i]))
636 restore_flags(flags);
637 printk(KERN_WARNING "mpu401: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
638 return-EIO;
642 ret =0;
643 cmd->data[0] =0;
645 if(cmd->nr_returns)
647 for(i =0; i < cmd->nr_returns; i++)
649 ok =0;
650 for(timeout =5000; timeout >0&& !ok; timeout--)
651 if(input_avail(devc))
653 cmd->data[i] =read_data(devc);
654 ok =1;
656 if(!ok)
658 restore_flags(flags);
659 return-EIO;
663 restore_flags(flags);
664 return ret;
667 static intmpu_cmd(int dev,int cmd,int data)
669 int ret;
671 static mpu_command_rec rec;
673 rec.cmd = cmd &0xff;
674 rec.nr_args = ((cmd &0xf0) ==0xE0);
675 rec.nr_returns = ((cmd &0xf0) ==0xA0);
676 rec.data[0] = data &0xff;
678 if((ret =mpu401_command(dev, &rec)) <0)
679 return ret;
680 return(unsigned char) rec.data[0];
683 static intmpu401_prefix_cmd(int dev,unsigned char status)
685 struct mpu_config *devc = &dev_conf[dev];
687 if(devc->uart_mode)
688 return1;
690 if(status <0xf0)
692 if(mpu_cmd(dev,0xD0,0) <0)
693 return0;
694 return1;
696 switch(status)
698 case0xF0:
699 if(mpu_cmd(dev,0xDF,0) <0)
700 return0;
701 return1;
703 default:
704 return0;
708 static intmpu401_start_read(int dev)
710 return0;
713 static intmpu401_end_read(int dev)
715 return0;
718 static intmpu401_ioctl(int dev,unsigned cmd, caddr_t arg)
720 struct mpu_config *devc;
721 mpu_command_rec rec;
722 int val, ret;
724 devc = &dev_conf[dev];
725 switch(cmd)
727 case SNDCTL_MIDI_MPUMODE:
728 if(!(devc->capabilities & MPU_CAP_INTLG)) {/* No intelligent mode */
729 printk(KERN_WARNING "mpu401: Intelligent mode not supported by the HW\n");
730 return-EINVAL;
732 if(get_user(val, (int*)arg))
733 return-EFAULT;
734 set_uart_mode(dev, devc, !val);
735 return0;
737 case SNDCTL_MIDI_MPUCMD:
738 if(copy_from_user(&rec, arg,sizeof(rec)))
739 return-EFAULT;
740 if((ret =mpu401_command(dev, &rec)) <0)
741 return ret;
742 if(copy_to_user(arg, &rec,sizeof(rec)))
743 return-EFAULT;
744 return0;
746 default:
747 return-EINVAL;
751 static voidmpu401_kick(int dev)
755 static intmpu401_buffer_status(int dev)
757 return0;/*
758 * No data in buffers
762 static intmpu_synth_ioctl(int dev,
763 unsigned int cmd, caddr_t arg)
765 int midi_dev;
766 struct mpu_config *devc;
768 midi_dev = synth_devs[dev]->midi_dev;
770 if(midi_dev <0|| midi_dev > num_midis || midi_devs[midi_dev] == NULL)
771 return-ENXIO;
773 devc = &dev_conf[midi_dev];
775 switch(cmd)
778 case SNDCTL_SYNTH_INFO:
779 memcpy((&((char*) arg)[0]), (char*) &mpu_synth_info[midi_dev],sizeof(struct synth_info));
780 return0;
782 case SNDCTL_SYNTH_MEMAVL:
783 return0x7fffffff;
785 default:
786 return-EINVAL;
790 static intmpu_synth_open(int dev,int mode)
792 int midi_dev, err;
793 struct mpu_config *devc;
795 midi_dev = synth_devs[dev]->midi_dev;
797 if(midi_dev <0|| midi_dev > num_midis || midi_devs[midi_dev] == NULL)
798 return-ENXIO;
800 devc = &dev_conf[midi_dev];
803 * Verify that the device is really running.
804 * Some devices (such as Ensoniq SoundScape don't
805 * work before the on board processor (OBP) is initialized
806 * by downloading its microcode.
809 if(!devc->initialized)
811 if(mpu401_status(devc) ==0xff)/* Bus float */
813 printk(KERN_ERR "mpu401: Device not initialized properly\n");
814 return-EIO;
816 reset_mpu401(devc);
818 if(devc->opened)
819 return-EBUSY;
820 devc->mode = MODE_SYNTH;
821 devc->synthno = dev;
823 devc->inputintr = NULL;
825 if(midi_devs[midi_dev]->coproc)
826 if((err = midi_devs[midi_dev]->coproc->
827 open(midi_devs[midi_dev]->coproc->devc, COPR_MIDI)) <0)
829 printk(KERN_WARNING "mpu401: Can't access coprocessor device\n");
830 return err;
832 devc->opened = mode;
833 reset_mpu401(devc);
835 if(mode & OPEN_READ)
837 mpu_cmd(midi_dev,0x8B,0);/* Enable data in stop mode */
838 mpu_cmd(midi_dev,0x34,0);/* Return timing bytes in stop mode */
839 mpu_cmd(midi_dev,0x87,0);/* Enable pitch & controller */
841 return0;
844 static voidmpu_synth_close(int dev)
846 int midi_dev;
847 struct mpu_config *devc;
849 midi_dev = synth_devs[dev]->midi_dev;
851 devc = &dev_conf[midi_dev];
852 mpu_cmd(midi_dev,0x15,0);/* Stop recording, playback and MIDI */
853 mpu_cmd(midi_dev,0x8a,0);/* Disable data in stopped mode */
855 devc->inputintr = NULL;
857 if(midi_devs[midi_dev]->coproc)
858 midi_devs[midi_dev]->coproc->close(midi_devs[midi_dev]->coproc->devc, COPR_MIDI);
859 devc->opened =0;
860 devc->mode =0;
863 #define MIDI_SYNTH_NAME"MPU-401 UART Midi"
864 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
865 #include"midi_synth.h"
867 static struct synth_operations mpu401_synth_proto =
869 owner: THIS_MODULE,
870 id:"MPU401",
871 info: NULL,
872 midi_dev:0,
873 synth_type: SYNTH_TYPE_MIDI,
874 synth_subtype:0,
875 open: mpu_synth_open,
876 close: mpu_synth_close,
877 ioctl: mpu_synth_ioctl,
878 kill_note: midi_synth_kill_note,
879 start_note: midi_synth_start_note,
880 set_instr: midi_synth_set_instr,
881 reset: midi_synth_reset,
882 hw_control: midi_synth_hw_control,
883 load_patch: midi_synth_load_patch,
884 aftertouch: midi_synth_aftertouch,
885 controller: midi_synth_controller,
886 panning: midi_synth_panning,
887 bender: midi_synth_bender,
888 setup_voice: midi_synth_setup_voice,
889 send_sysex: midi_synth_send_sysex
892 static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
894 static struct midi_operations mpu401_midi_proto =
896 owner: THIS_MODULE,
897 info: {"MPU-401 Midi",0, MIDI_CAP_MPU401, SNDCARD_MPU401},
898 in_info: {0},
899 open: mpu401_open,
900 close: mpu401_close,
901 ioctl: mpu401_ioctl,
902 outputc: mpu401_out,
903 start_read: mpu401_start_read,
904 end_read: mpu401_end_read,
905 kick: mpu401_kick,
906 buffer_status: mpu401_buffer_status,
907 prefix_cmd: mpu401_prefix_cmd
910 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
912 static voidmpu401_chk_version(int n,struct mpu_config *devc)
914 int tmp;
915 unsigned long flags;
917 devc->version = devc->revision =0;
919 save_flags(flags);
920 cli();
921 if((tmp =mpu_cmd(n,0xAC,0)) <0)
923 restore_flags(flags);
924 return;
926 if((tmp &0xf0) >0x20)/* Why it's larger than 2.x ??? */
928 restore_flags(flags);
929 return;
931 devc->version = tmp;
933 if((tmp =mpu_cmd(n,0xAD,0)) <0)
935 devc->version =0;
936 restore_flags(flags);
937 return;
939 devc->revision = tmp;
940 restore_flags(flags);
943 voidattach_mpu401(struct address_info *hw_config,struct module *owner)
945 unsigned long flags;
946 char revision_char;
948 int m;
949 struct mpu_config *devc;
951 hw_config->slots[1] = -1;
952 m =sound_alloc_mididev();
953 if(m == -1)
955 printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
956 return;
958 devc = &dev_conf[m];
959 devc->base = hw_config->io_base;
960 devc->osp = hw_config->osp;
961 devc->irq = hw_config->irq;
962 devc->opened =0;
963 devc->uart_mode =0;
964 devc->initialized =0;
965 devc->version =0;
966 devc->revision =0;
967 devc->capabilities =0;
968 devc->timer_flag =0;
969 devc->m_busy =0;
970 devc->m_state = ST_INIT;
971 devc->shared_irq = hw_config->always_detect;
972 devc->irq = hw_config->irq;
974 if(devc->irq <0)
976 devc->irq *= -1;
977 devc->shared_irq =1;
980 if(!hw_config->always_detect)
982 /* Verify the hardware again */
983 if(!reset_mpu401(devc))
985 printk(KERN_WARNING "mpu401: Device didn't respond\n");
986 sound_unload_mididev(m);
987 return;
989 if(!devc->shared_irq)
991 if(request_irq(devc->irq, mpuintr,0,"mpu401", (void*)m) <0)
993 printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);
994 sound_unload_mididev(m);
995 return;
998 save_flags(flags);
999 cli();
1000 mpu401_chk_version(m, devc);
1001 if(devc->version ==0)
1002 mpu401_chk_version(m, devc);
1003 restore_flags(flags);
1005 request_region(hw_config->io_base,2,"mpu401");
1007 if(devc->version !=0)
1008 if(mpu_cmd(m,0xC5,0) >=0)/* Set timebase OK */
1009 if(mpu_cmd(m,0xE0,120) >=0)/* Set tempo OK */
1010 devc->capabilities |= MPU_CAP_INTLG;/* Supports intelligent mode */
1013 mpu401_synth_operations[m] = (struct synth_operations *)kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
1015 if(mpu401_synth_operations[m] == NULL)
1017 sound_unload_mididev(m);
1018 printk(KERN_ERR "mpu401: Can't allocate memory\n");
1019 return;
1021 if(!(devc->capabilities & MPU_CAP_INTLG))/* No intelligent mode */
1023 memcpy((char*) mpu401_synth_operations[m],
1024 (char*) &std_midi_synth,
1025 sizeof(struct synth_operations));
1027 else
1029 memcpy((char*) mpu401_synth_operations[m],
1030 (char*) &mpu401_synth_proto,
1031 sizeof(struct synth_operations));
1034 memcpy((char*) &mpu401_midi_operations[m],
1035 (char*) &mpu401_midi_proto,
1036 sizeof(struct midi_operations));
1038 mpu401_midi_operations[m].converter = mpu401_synth_operations[m];
1040 memcpy((char*) &mpu_synth_info[m],
1041 (char*) &mpu_synth_info_proto,
1042 sizeof(struct synth_info));
1044 n_mpu_devs++;
1046 if(devc->version ==0x20&& devc->revision >=0x07)/* MusicQuest interface */
1048 int ports = (devc->revision &0x08) ?32:16;
1050 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1051 MPU_CAP_CLS | MPU_CAP_2PORT;
1053 revision_char = (devc->revision ==0x7f) ?'M':' ';
1054 sprintf(mpu_synth_info[m].name,"MQX-%d%c MIDI Interface #%d",
1055 ports,
1056 revision_char,
1057 n_mpu_devs);
1059 else
1061 revision_char = devc->revision ? devc->revision +'@':' ';
1062 if((int) devc->revision > ('Z'-'@'))
1063 revision_char ='+';
1065 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1067 if(hw_config->name)
1068 sprintf(mpu_synth_info[m].name,"%s (MPU401)", hw_config->name);
1069 else
1070 sprintf(mpu_synth_info[m].name,
1071 "MPU-401 %d.%d%c Midi interface #%d",
1072 (int) (devc->version &0xf0) >>4,
1073 devc->version &0x0f,
1074 revision_char,
1075 n_mpu_devs);
1078 strcpy(mpu401_midi_operations[m].info.name,
1079 mpu_synth_info[m].name);
1081 conf_printf(mpu_synth_info[m].name, hw_config);
1083 mpu401_synth_operations[m]->midi_dev = devc->devno = m;
1084 mpu401_synth_operations[devc->devno]->info = &mpu_synth_info[devc->devno];
1086 if(devc->capabilities & MPU_CAP_INTLG)/* Intelligent mode */
1087 hw_config->slots[2] =mpu_timer_init(m);
1089 midi_devs[m] = &mpu401_midi_operations[devc->devno];
1091 if(owner)
1092 midi_devs[m]->owner = owner;
1094 hw_config->slots[1] = m;
1095 sequencer_init();
1098 static intreset_mpu401(struct mpu_config *devc)
1100 unsigned long flags;
1101 int ok, timeout, n;
1102 int timeout_limit;
1105 * Send the RESET command. Try again if no success at the first time.
1106 * (If the device is in the UART mode, it will not ack the reset cmd).
1109 ok =0;
1111 timeout_limit = devc->initialized ?30000:100000;
1112 devc->initialized =1;
1114 for(n =0; n <2&& !ok; n++)
1116 for(timeout = timeout_limit; timeout >0&& !ok; timeout--)
1117 ok =output_ready(devc);
1119 write_command(devc, MPU_RESET);/*
1120 * Send MPU-401 RESET Command
1124 * Wait at least 25 msec. This method is not accurate so let's make the
1125 * loop bit longer. Cannot sleep since this is called during boot.
1128 for(timeout = timeout_limit *2; timeout >0&& !ok; timeout--)
1130 save_flags(flags);
1131 cli();
1132 if(input_avail(devc))
1133 if(read_data(devc) == MPU_ACK)
1134 ok =1;
1135 restore_flags(flags);
1140 devc->m_state = ST_INIT;
1141 devc->m_ptr =0;
1142 devc->m_left =0;
1143 devc->last_status =0;
1144 devc->uart_mode =0;
1146 return ok;
1149 static voidset_uart_mode(int dev,struct mpu_config *devc,int arg)
1151 if(!arg && (devc->capabilities & MPU_CAP_INTLG))
1152 return;
1153 if((devc->uart_mode ==0) == (arg ==0))
1154 return;/* Already set */
1155 reset_mpu401(devc);/* This exits the uart mode */
1157 if(arg)
1159 if(mpu_cmd(dev, UART_MODE_ON,0) <0)
1161 printk(KERN_ERR "mpu401: Can't enter UART mode\n");
1162 devc->uart_mode =0;
1163 return;
1166 devc->uart_mode = arg;
1170 intprobe_mpu401(struct address_info *hw_config)
1172 int ok =0;
1173 struct mpu_config tmp_devc;
1175 if(check_region(hw_config->io_base,2))
1177 printk(KERN_ERR "mpu401: I/O port %x already in use\n\n", hw_config->io_base);
1178 return0;
1180 tmp_devc.base = hw_config->io_base;
1181 tmp_devc.irq = hw_config->irq;
1182 tmp_devc.initialized =0;
1183 tmp_devc.opened =0;
1184 tmp_devc.osp = hw_config->osp;
1186 if(hw_config->always_detect)
1187 return1;
1189 if(inb(hw_config->io_base +1) ==0xff)
1191 DDB(printk("MPU401: Port %x looks dead.\n", hw_config->io_base));
1192 return0;/* Just bus float? */
1194 ok =reset_mpu401(&tmp_devc);
1196 if(!ok)
1198 DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
1200 return ok;
1203 void __exit unload_mpu401(struct address_info *hw_config)
1205 void*p;
1206 int n=hw_config->slots[1];
1208 release_region(hw_config->io_base,2);
1209 if(hw_config->always_detect ==0&& hw_config->irq >0)
1210 free_irq(hw_config->irq, (void*)n);
1211 p=mpu401_synth_operations[n];
1212 sound_unload_mididev(n);
1213 sound_unload_timerdev(hw_config->slots[2]);
1214 if(p)
1215 kfree(p);
1218 /*****************************************************
1219 * Timer stuff
1220 ****************************************************/
1222 staticvolatileint timer_initialized =0, timer_open =0, tmr_running =0;
1223 staticvolatileint curr_tempo, curr_timebase, hw_timebase;
1224 static int max_timebase =8;/* 8*24=192 ppqn */
1225 staticvolatileunsigned long next_event_time;
1226 staticvolatileunsigned long curr_ticks, curr_clocks;
1227 static unsigned long prev_event_time;
1228 static int metronome_mode;
1230 static unsigned longclocks2ticks(unsigned long clocks)
1233 * The MPU-401 supports just a limited set of possible timebase values.
1234 * Since the applications require more choices, the driver has to
1235 * program the HW to do its best and to convert between the HW and
1236 * actual timebases.
1238 return((clocks * curr_timebase) + (hw_timebase /2)) / hw_timebase;
1241 static voidset_timebase(int midi_dev,int val)
1243 int hw_val;
1245 if(val <48)
1246 val =48;
1247 if(val >1000)
1248 val =1000;
1250 hw_val = val;
1251 hw_val = (hw_val +12) /24;
1252 if(hw_val > max_timebase)
1253 hw_val = max_timebase;
1255 if(mpu_cmd(midi_dev,0xC0| (hw_val &0x0f),0) <0)
1257 printk(KERN_WARNING "mpu401: Can't set HW timebase to %d\n", hw_val *24);
1258 return;
1260 hw_timebase = hw_val *24;
1261 curr_timebase = val;
1265 static voidtmr_reset(void)
1267 unsigned long flags;
1269 save_flags(flags);
1270 cli();
1271 next_event_time = (unsigned long) -1;
1272 prev_event_time =0;
1273 curr_ticks = curr_clocks =0;
1274 restore_flags(flags);
1277 static voidset_timer_mode(int midi_dev)
1279 if(timer_mode & TMR_MODE_CLS)
1280 mpu_cmd(midi_dev,0x3c,0);/* Use CLS sync */
1281 else if(timer_mode & TMR_MODE_SMPTE)
1282 mpu_cmd(midi_dev,0x3d,0);/* Use SMPTE sync */
1284 if(timer_mode & TMR_INTERNAL)
1286 mpu_cmd(midi_dev,0x80,0);/* Use MIDI sync */
1288 else
1290 if(timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1292 mpu_cmd(midi_dev,0x82,0);/* Use MIDI sync */
1293 mpu_cmd(midi_dev,0x91,0);/* Enable ext MIDI ctrl */
1295 else if(timer_mode & TMR_MODE_FSK)
1296 mpu_cmd(midi_dev,0x81,0);/* Use FSK sync */
1300 static voidstop_metronome(int midi_dev)
1302 mpu_cmd(midi_dev,0x84,0);/* Disable metronome */
1305 static voidsetup_metronome(int midi_dev)
1307 int numerator, denominator;
1308 int clks_per_click, num_32nds_per_beat;
1309 int beats_per_measure;
1311 numerator = ((unsigned) metronome_mode >>24) &0xff;
1312 denominator = ((unsigned) metronome_mode >>16) &0xff;
1313 clks_per_click = ((unsigned) metronome_mode >>8) &0xff;
1314 num_32nds_per_beat = (unsigned) metronome_mode &0xff;
1315 beats_per_measure = (numerator *4) >> denominator;
1317 if(!metronome_mode)
1318 mpu_cmd(midi_dev,0x84,0);/* Disable metronome */
1319 else
1321 mpu_cmd(midi_dev,0xE4, clks_per_click);
1322 mpu_cmd(midi_dev,0xE6, beats_per_measure);
1323 mpu_cmd(midi_dev,0x83,0);/* Enable metronome without accents */
1327 static intmpu_start_timer(int midi_dev)
1329 tmr_reset();
1330 set_timer_mode(midi_dev);
1332 if(tmr_running)
1333 return TIMER_NOT_ARMED;/* Already running */
1335 if(timer_mode & TMR_INTERNAL)
1337 mpu_cmd(midi_dev,0x02,0);/* Send MIDI start */
1338 tmr_running =1;
1339 return TIMER_NOT_ARMED;
1341 else
1343 mpu_cmd(midi_dev,0x35,0);/* Enable mode messages to PC */
1344 mpu_cmd(midi_dev,0x38,0);/* Enable sys common messages to PC */
1345 mpu_cmd(midi_dev,0x39,0);/* Enable real time messages to PC */
1346 mpu_cmd(midi_dev,0x97,0);/* Enable system exclusive messages to PC */
1348 return TIMER_ARMED;
1351 static intmpu_timer_open(int dev,int mode)
1353 int midi_dev = sound_timer_devs[dev]->devlink;
1355 if(timer_open)
1356 return-EBUSY;
1358 tmr_reset();
1359 curr_tempo =50;
1360 mpu_cmd(midi_dev,0xE0,50);
1361 curr_timebase = hw_timebase =120;
1362 set_timebase(midi_dev,120);
1363 timer_open =1;
1364 metronome_mode =0;
1365 set_timer_mode(midi_dev);
1367 mpu_cmd(midi_dev,0xe7,0x04);/* Send all clocks to host */
1368 mpu_cmd(midi_dev,0x95,0);/* Enable clock to host */
1370 return0;
1373 static voidmpu_timer_close(int dev)
1375 int midi_dev = sound_timer_devs[dev]->devlink;
1377 timer_open = tmr_running =0;
1378 mpu_cmd(midi_dev,0x15,0);/* Stop all */
1379 mpu_cmd(midi_dev,0x94,0);/* Disable clock to host */
1380 mpu_cmd(midi_dev,0x8c,0);/* Disable measure end messages to host */
1381 stop_metronome(midi_dev);
1384 static intmpu_timer_event(int dev,unsigned char*event)
1386 unsigned char command = event[1];
1387 unsigned long parm = *(unsigned int*) &event[4];
1388 int midi_dev = sound_timer_devs[dev]->devlink;
1390 switch(command)
1392 case TMR_WAIT_REL:
1393 parm += prev_event_time;
1394 case TMR_WAIT_ABS:
1395 if(parm >0)
1397 long time;
1399 if(parm <= curr_ticks)/* It's the time */
1400 return TIMER_NOT_ARMED;
1401 time = parm;
1402 next_event_time = prev_event_time = time;
1404 return TIMER_ARMED;
1406 break;
1408 case TMR_START:
1409 if(tmr_running)
1410 break;
1411 returnmpu_start_timer(midi_dev);
1413 case TMR_STOP:
1414 mpu_cmd(midi_dev,0x01,0);/* Send MIDI stop */
1415 stop_metronome(midi_dev);
1416 tmr_running =0;
1417 break;
1419 case TMR_CONTINUE:
1420 if(tmr_running)
1421 break;
1422 mpu_cmd(midi_dev,0x03,0);/* Send MIDI continue */
1423 setup_metronome(midi_dev);
1424 tmr_running =1;
1425 break;
1427 case TMR_TEMPO:
1428 if(parm)
1430 if(parm <8)
1431 parm =8;
1432 if(parm >250)
1433 parm =250;
1434 if(mpu_cmd(midi_dev,0xE0, parm) <0)
1435 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) parm);
1436 curr_tempo = parm;
1438 break;
1440 case TMR_ECHO:
1441 seq_copy_to_input(event,8);
1442 break;
1444 case TMR_TIMESIG:
1445 if(metronome_mode)/* Metronome enabled */
1447 metronome_mode = parm;
1448 setup_metronome(midi_dev);
1450 break;
1452 default:
1454 return TIMER_NOT_ARMED;
1457 static unsigned longmpu_timer_get_time(int dev)
1459 if(!timer_open)
1460 return0;
1462 return curr_ticks;
1465 static intmpu_timer_ioctl(int dev,unsigned int command, caddr_t arg)
1467 int midi_dev = sound_timer_devs[dev]->devlink;
1469 switch(command)
1471 case SNDCTL_TMR_SOURCE:
1473 int parm;
1475 parm = *(int*) arg;
1476 parm &= timer_caps;
1478 if(parm !=0)
1480 timer_mode = parm;
1482 if(timer_mode & TMR_MODE_CLS)
1483 mpu_cmd(midi_dev,0x3c,0);/* Use CLS sync */
1484 else if(timer_mode & TMR_MODE_SMPTE)
1485 mpu_cmd(midi_dev,0x3d,0);/* Use SMPTE sync */
1487 return(*(int*) arg = timer_mode);
1489 break;
1491 case SNDCTL_TMR_START:
1492 mpu_start_timer(midi_dev);
1493 return0;
1495 case SNDCTL_TMR_STOP:
1496 tmr_running =0;
1497 mpu_cmd(midi_dev,0x01,0);/* Send MIDI stop */
1498 stop_metronome(midi_dev);
1499 return0;
1501 case SNDCTL_TMR_CONTINUE:
1502 if(tmr_running)
1503 return0;
1504 tmr_running =1;
1505 mpu_cmd(midi_dev,0x03,0);/* Send MIDI continue */
1506 return0;
1508 case SNDCTL_TMR_TIMEBASE:
1510 int val;
1512 val = *(int*) arg;
1513 if(val)
1514 set_timebase(midi_dev, val);
1515 return(*(int*) arg = curr_timebase);
1517 break;
1519 case SNDCTL_TMR_TEMPO:
1521 int val;
1522 int ret;
1524 val = *(int*) arg;
1526 if(val)
1528 if(val <8)
1529 val =8;
1530 if(val >250)
1531 val =250;
1532 if((ret =mpu_cmd(midi_dev,0xE0, val)) <0)
1534 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
1535 return ret;
1537 curr_tempo = val;
1539 return(*(int*) arg = curr_tempo);
1541 break;
1543 case SNDCTL_SEQ_CTRLRATE:
1545 int val;
1547 val = *(int*) arg;
1548 if(val !=0)/* Can't change */
1549 return-EINVAL;
1550 return(*(int*) arg = ((curr_tempo * curr_timebase) +30) /60);
1552 break;
1554 case SNDCTL_SEQ_GETTIME:
1555 return(*(int*) arg = curr_ticks);
1557 case SNDCTL_TMR_METRONOME:
1558 metronome_mode = *(int*) arg;
1559 setup_metronome(midi_dev);
1560 return0;
1562 default:
1564 return-EINVAL;
1567 static voidmpu_timer_arm(int dev,long time)
1569 if(time <0)
1570 time = curr_ticks +1;
1571 else if(time <= curr_ticks)/* It's the time */
1572 return;
1573 next_event_time = prev_event_time = time;
1574 return;
1577 static struct sound_timer_operations mpu_timer =
1579 owner: THIS_MODULE,
1580 info: {"MPU-401 Timer",0},
1581 priority:10,/* Priority */
1582 devlink:0,/* Local device link */
1583 open: mpu_timer_open,
1584 close: mpu_timer_close,
1585 event: mpu_timer_event,
1586 get_time: mpu_timer_get_time,
1587 ioctl: mpu_timer_ioctl,
1588 arm_timer: mpu_timer_arm
1591 static voidmpu_timer_interrupt(void)
1593 if(!timer_open)
1594 return;
1596 if(!tmr_running)
1597 return;
1599 curr_clocks++;
1600 curr_ticks =clocks2ticks(curr_clocks);
1602 if(curr_ticks >= next_event_time)
1604 next_event_time = (unsigned long) -1;
1605 sequencer_timer(0);
1609 static voidtimer_ext_event(struct mpu_config *devc,int event,int parm)
1611 int midi_dev = devc->devno;
1613 if(!devc->timer_flag)
1614 return;
1616 switch(event)
1618 case TMR_CLOCK:
1619 printk("<MIDI clk>");
1620 break;
1622 case TMR_START:
1623 printk("Ext MIDI start\n");
1624 if(!tmr_running)
1626 if(timer_mode & TMR_EXTERNAL)
1628 tmr_running =1;
1629 setup_metronome(midi_dev);
1630 next_event_time =0;
1631 STORE(SEQ_START_TIMER());
1634 break;
1636 case TMR_STOP:
1637 printk("Ext MIDI stop\n");
1638 if(timer_mode & TMR_EXTERNAL)
1640 tmr_running =0;
1641 stop_metronome(midi_dev);
1642 STORE(SEQ_STOP_TIMER());
1644 break;
1646 case TMR_CONTINUE:
1647 printk("Ext MIDI continue\n");
1648 if(timer_mode & TMR_EXTERNAL)
1650 tmr_running =1;
1651 setup_metronome(midi_dev);
1652 STORE(SEQ_CONTINUE_TIMER());
1654 break;
1656 case TMR_SPP:
1657 printk("Songpos: %d\n", parm);
1658 if(timer_mode & TMR_EXTERNAL)
1660 STORE(SEQ_SONGPOS(parm));
1662 break;
1666 static intmpu_timer_init(int midi_dev)
1668 struct mpu_config *devc;
1669 int n;
1671 devc = &dev_conf[midi_dev];
1673 if(timer_initialized)
1674 return-1;/* There is already a similar timer */
1676 timer_initialized =1;
1678 mpu_timer.devlink = midi_dev;
1679 dev_conf[midi_dev].timer_flag =1;
1681 n =sound_alloc_timerdev();
1682 if(n == -1)
1683 n =0;
1684 sound_timer_devs[n] = &mpu_timer;
1686 if(devc->version <0x20)/* Original MPU-401 */
1687 timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1688 else
1691 * The version number 2.0 is used (at least) by the
1692 * MusicQuest cards and the Roland Super-MPU.
1694 * MusicQuest has given a special meaning to the bits of the
1695 * revision number. The Super-MPU returns 0.
1698 if(devc->revision)
1699 timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1701 if(devc->revision &0x02)
1702 timer_caps |= TMR_MODE_CLS;
1705 if(devc->revision &0x40)
1706 max_timebase =10;/* Has the 216 and 240 ppqn modes */
1709 timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1710 return n;
1714 EXPORT_SYMBOL(probe_mpu401);
1715 EXPORT_SYMBOL(attach_mpu401);
1716 EXPORT_SYMBOL(unload_mpu401);
1717 EXPORT_SYMBOL(intchk_mpu401);
1718 EXPORT_SYMBOL(mpuintr);
1720 static struct address_info cfg;
1722 static int __initdata io = -1;
1723 static int __initdata irq = -1;
1725 MODULE_PARM(irq,"i");
1726 MODULE_PARM(io,"i");
1728 int __init init_mpu401(void)
1730 /* Can be loaded either for module use or to provide functions
1731 to others */
1732 if(io != -1&& irq != -1) {
1733 cfg.irq = irq;
1734 cfg.io_base = io;
1735 if(probe_mpu401(&cfg) ==0)
1736 return-ENODEV;
1737 attach_mpu401(&cfg, THIS_MODULE);
1740 return0;
1743 void __exit cleanup_mpu401(void)
1745 if(io != -1&& irq != -1) {
1746 /* Check for use by, for example, sscape driver */
1747 unload_mpu401(&cfg);
1751 module_init(init_mpu401);
1752 module_exit(cleanup_mpu401);
1754 #ifndef MODULE
1755 static int __init setup_mpu401(char*str)
1757 /* io, irq */
1758 int ints[3];
1760 str =get_options(str,ARRAY_SIZE(ints), ints);
1762 io = ints[1];
1763 irq = ints[2];
1765 return1;
1768 __setup("mpu401=", setup_mpu401);
1769 #endif
close