forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpu401.c
1806 lines (1512 loc) · 37.6 KB
/
mpu401.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* sound/oss/mpu401.c
*
* The low level driver for Roland MPU-401 compatible Midi cards.
*/
/*
* Copyright (C) by Hannu Savolainen 1993-1997
*
* OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
* Version 2 (June 1991). See the "COPYING" file distributed with this software
* for more info.
*
*
* Thomas Sailer ioctl code reworked (vmalloc/vfree removed)
* Alan Cox modularisation, use normal request_irq, use dev_id
* Bartlomiej Zolnierkiewicz removed some __init to allow using many drivers
* Chris Rankin Update the module-usage counter for the coprocessor
* Zwane Mwaikambo Changed attach/unload resource freeing
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#define USE_SEQ_MACROS
#define USE_SIMPLE_MACROS
#include "sound_config.h"
#include "coproc.h"
#include "mpu401.h"
static int timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
struct mpu_config
{
int base; /*
* I/O base
*/
int irq;
int opened; /*
* Open mode
*/
int devno;
int synthno;
int uart_mode;
int initialized;
int mode;
#define MODE_MIDI 1
#define MODE_SYNTH 2
unsigned char version, revision;
unsigned int capabilities;
#define MPU_CAP_INTLG 0x10000000
#define MPU_CAP_SYNC 0x00000010
#define MPU_CAP_FSK 0x00000020
#define MPU_CAP_CLS 0x00000040
#define MPU_CAP_SMPTE 0x00000080
#define MPU_CAP_2PORT 0x00000001
int timer_flag;
#define MBUF_MAX 10
#define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
{printk( "MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
int m_busy;
unsigned char m_buf[MBUF_MAX];
int m_ptr;
int m_state;
int m_left;
unsigned char last_status;
void (*inputintr) (int dev, unsigned char data);
int shared_irq;
int *osp;
spinlock_t lock;
};
#define DATAPORT(base) (base)
#define COMDPORT(base) (base+1)
#define STATPORT(base) (base+1)
static void mpu401_close(int dev);
static inline int mpu401_status(struct mpu_config *devc)
{
return inb(STATPORT(devc->base));
}
#define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
#define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
static inline void write_command(struct mpu_config *devc, unsigned char cmd)
{
outb(cmd, COMDPORT(devc->base));
}
static inline int read_data(struct mpu_config *devc)
{
return inb(DATAPORT(devc->base));
}
static inline void write_data(struct mpu_config *devc, unsigned char byte)
{
outb(byte, DATAPORT(devc->base));
}
#define OUTPUT_READY 0x40
#define INPUT_AVAIL 0x80
#define MPU_ACK 0xFE
#define MPU_RESET 0xFF
#define UART_MODE_ON 0x3F
static struct mpu_config dev_conf[MAX_MIDI_DEV];
static int n_mpu_devs;
static int reset_mpu401(struct mpu_config *devc);
static void set_uart_mode(int dev, struct mpu_config *devc, int arg);
static int mpu_timer_init(int midi_dev);
static void mpu_timer_interrupt(void);
static void timer_ext_event(struct mpu_config *devc, int event, int parm);
static struct synth_info mpu_synth_info_proto = {
"MPU-401 MIDI interface",
0,
SYNTH_TYPE_MIDI,
MIDI_TYPE_MPU401,
0, 128,
0, 128,
SYNTH_CAP_INPUT
};
static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
/*
* States for the input scanner
*/
#define ST_INIT 0 /* Ready for timing byte or msg */
#define ST_TIMED 1 /* Leading timing byte rcvd */
#define ST_DATABYTE 2 /* Waiting for (nr_left) data bytes */
#define ST_SYSMSG 100 /* System message (sysx etc). */
#define ST_SYSEX 101 /* System exclusive msg */
#define ST_MTC 102 /* Midi Time Code (MTC) qframe msg */
#define ST_SONGSEL 103 /* Song select */
#define ST_SONGPOS 104 /* Song position pointer */
static unsigned char len_tab[] = /* # of data bytes following a status
*/
{
2, /* 8x */
2, /* 9x */
2, /* Ax */
2, /* Bx */
1, /* Cx */
1, /* Dx */
2, /* Ex */
0 /* Fx */
};
#define STORE(cmd) \
{ \
int len; \
unsigned char obuf[8]; \
cmd; \
seq_input_event(obuf, len); \
}
#define _seqbuf obuf
#define _seqbufptr 0
#define _SEQ_ADVBUF(x) len=x
static int mpu_input_scanner(struct mpu_config *devc, unsigned char midic)
{
switch (devc->m_state)
{
case ST_INIT:
switch (midic)
{
case 0xf8:
/* Timer overflow */
break;
case 0xfc:
printk("<all end>");
break;
case 0xfd:
if (devc->timer_flag)
mpu_timer_interrupt();
break;
case 0xfe:
return MPU_ACK;
case 0xf0:
case 0xf1:
case 0xf2:
case 0xf3:
case 0xf4:
case 0xf5:
case 0xf6:
case 0xf7:
printk("<Trk data rq #%d>", midic & 0x0f);
break;
case 0xf9:
printk("<conductor rq>");
break;
case 0xff:
devc->m_state = ST_SYSMSG;
break;
default:
if (midic <= 0xef)
{
/* printk( "mpu time: %d ", midic); */
devc->m_state = ST_TIMED;
}
else
printk("<MPU: Unknown event %02x> ", midic);
}
break;
case ST_TIMED:
{
int msg = ((int) (midic & 0xf0) >> 4);
devc->m_state = ST_DATABYTE;
if (msg < 8) /* Data byte */
{
/* printk( "midi msg (running status) "); */
msg = ((int) (devc->last_status & 0xf0) >> 4);
msg -= 8;
devc->m_left = len_tab[msg] - 1;
devc->m_ptr = 2;
devc->m_buf[0] = devc->last_status;
devc->m_buf[1] = midic;
if (devc->m_left <= 0)
{
devc->m_state = ST_INIT;
do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
devc->m_ptr = 0;
}
}
else if (msg == 0xf) /* MPU MARK */
{
devc->m_state = ST_INIT;
switch (midic)
{
case 0xf8:
/* printk( "NOP "); */
break;
case 0xf9:
/* printk( "meas end "); */
break;
case 0xfc:
/* printk( "data end "); */
break;
default:
printk("Unknown MPU mark %02x\n", midic);
}
}
else
{
devc->last_status = midic;
/* printk( "midi msg "); */
msg -= 8;
devc->m_left = len_tab[msg];
devc->m_ptr = 1;
devc->m_buf[0] = midic;
if (devc->m_left <= 0)
{
devc->m_state = ST_INIT;
do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
devc->m_ptr = 0;
}
}
}
break;
case ST_SYSMSG:
switch (midic)
{
case 0xf0:
printk("<SYX>");
devc->m_state = ST_SYSEX;
break;
case 0xf1:
devc->m_state = ST_MTC;
break;
case 0xf2:
devc->m_state = ST_SONGPOS;
devc->m_ptr = 0;
break;
case 0xf3:
devc->m_state = ST_SONGSEL;
break;
case 0xf6:
/* printk( "tune_request\n"); */
devc->m_state = ST_INIT;
/*
* Real time messages
*/
case 0xf8:
/* midi clock */
devc->m_state = ST_INIT;
timer_ext_event(devc, TMR_CLOCK, 0);
break;
case 0xfA:
devc->m_state = ST_INIT;
timer_ext_event(devc, TMR_START, 0);
break;
case 0xFB:
devc->m_state = ST_INIT;
timer_ext_event(devc, TMR_CONTINUE, 0);
break;
case 0xFC:
devc->m_state = ST_INIT;
timer_ext_event(devc, TMR_STOP, 0);
break;
case 0xFE:
/* active sensing */
devc->m_state = ST_INIT;
break;
case 0xff:
/* printk( "midi hard reset"); */
devc->m_state = ST_INIT;
break;
default:
printk("unknown MIDI sysmsg %0x\n", midic);
devc->m_state = ST_INIT;
}
break;
case ST_MTC:
devc->m_state = ST_INIT;
printk("MTC frame %x02\n", midic);
break;
case ST_SYSEX:
if (midic == 0xf7)
{
printk("<EOX>");
devc->m_state = ST_INIT;
}
else
printk("%02x ", midic);
break;
case ST_SONGPOS:
BUFTEST(devc);
devc->m_buf[devc->m_ptr++] = midic;
if (devc->m_ptr == 2)
{
devc->m_state = ST_INIT;
devc->m_ptr = 0;
timer_ext_event(devc, TMR_SPP,
((devc->m_buf[1] & 0x7f) << 7) |
(devc->m_buf[0] & 0x7f));
}
break;
case ST_DATABYTE:
BUFTEST(devc);
devc->m_buf[devc->m_ptr++] = midic;
if ((--devc->m_left) <= 0)
{
devc->m_state = ST_INIT;
do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
devc->m_ptr = 0;
}
break;
default:
printk("Bad state %d ", devc->m_state);
devc->m_state = ST_INIT;
}
return 1;
}
static void mpu401_input_loop(struct mpu_config *devc)
{
unsigned long flags;
int busy;
int n;
spin_lock_irqsave(&devc->lock,flags);
busy = devc->m_busy;
devc->m_busy = 1;
spin_unlock_irqrestore(&devc->lock,flags);
if (busy) /* Already inside the scanner */
return;
n = 50;
while (input_avail(devc) && n-- > 0)
{
unsigned char c = read_data(devc);
if (devc->mode == MODE_SYNTH)
{
mpu_input_scanner(devc, c);
}
else if (devc->opened & OPEN_READ && devc->inputintr != NULL)
devc->inputintr(devc->devno, c);
}
devc->m_busy = 0;
}
static irqreturn_t mpuintr(int irq, void *dev_id)
{
struct mpu_config *devc;
int dev = (int)(unsigned long) dev_id;
int handled = 0;
devc = &dev_conf[dev];
if (input_avail(devc))
{
handled = 1;
if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
mpu401_input_loop(devc);
else
{
/* Dummy read (just to acknowledge the interrupt) */
read_data(devc);
}
}
return IRQ_RETVAL(handled);
}
static int mpu401_open(int dev, int mode,
void (*input) (int dev, unsigned char data),
void (*output) (int dev)
)
{
int err;
struct mpu_config *devc;
struct coproc_operations *coprocessor;
if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
return -ENXIO;
devc = &dev_conf[dev];
if (devc->opened)
return -EBUSY;
/*
* Verify that the device is really running.
* Some devices (such as Ensoniq SoundScape don't
* work before the on board processor (OBP) is initialized
* by downloading its microcode.
*/
if (!devc->initialized)
{
if (mpu401_status(devc) == 0xff) /* Bus float */
{
printk(KERN_ERR "mpu401: Device not initialized properly\n");
return -EIO;
}
reset_mpu401(devc);
}
if ( (coprocessor = midi_devs[dev]->coproc) != NULL )
{
if (!try_module_get(coprocessor->owner)) {
mpu401_close(dev);
return -ENODEV;
}
if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
{
printk(KERN_WARNING "MPU-401: Can't access coprocessor device\n");
mpu401_close(dev);
return err;
}
}
set_uart_mode(dev, devc, 1);
devc->mode = MODE_MIDI;
devc->synthno = 0;
mpu401_input_loop(devc);
devc->inputintr = input;
devc->opened = mode;
return 0;
}
static void mpu401_close(int dev)
{
struct mpu_config *devc;
struct coproc_operations *coprocessor;
devc = &dev_conf[dev];
if (devc->uart_mode)
reset_mpu401(devc); /*
* This disables the UART mode
*/
devc->mode = 0;
devc->inputintr = NULL;
coprocessor = midi_devs[dev]->coproc;
if (coprocessor) {
coprocessor->close(coprocessor->devc, COPR_MIDI);
module_put(coprocessor->owner);
}
devc->opened = 0;
}
static int mpu401_out(int dev, unsigned char midi_byte)
{
int timeout;
unsigned long flags;
struct mpu_config *devc;
devc = &dev_conf[dev];
/*
* Sometimes it takes about 30000 loops before the output becomes ready
* (After reset). Normally it takes just about 10 loops.
*/
for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
spin_lock_irqsave(&devc->lock,flags);
if (!output_ready(devc))
{
printk(KERN_WARNING "mpu401: Send data timeout\n");
spin_unlock_irqrestore(&devc->lock,flags);
return 0;
}
write_data(devc, midi_byte);
spin_unlock_irqrestore(&devc->lock,flags);
return 1;
}
static int mpu401_command(int dev, mpu_command_rec * cmd)
{
int i, timeout, ok;
int ret = 0;
unsigned long flags;
struct mpu_config *devc;
devc = &dev_conf[dev];
if (devc->uart_mode) /*
* Not possible in UART mode
*/
{
printk(KERN_WARNING "mpu401: commands not possible in the UART mode\n");
return -EINVAL;
}
/*
* Test for input since pending input seems to block the output.
*/
if (input_avail(devc))
mpu401_input_loop(devc);
/*
* Sometimes it takes about 50000 loops before the output becomes ready
* (After reset). Normally it takes just about 10 loops.
*/
timeout = 50000;
retry:
if (timeout-- <= 0)
{
printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
return -EIO;
}
spin_lock_irqsave(&devc->lock,flags);
if (!output_ready(devc))
{
spin_unlock_irqrestore(&devc->lock,flags);
goto retry;
}
write_command(devc, cmd->cmd);
ok = 0;
for (timeout = 50000; timeout > 0 && !ok; timeout--)
{
if (input_avail(devc))
{
if (devc->opened && devc->mode == MODE_SYNTH)
{
if (mpu_input_scanner(devc, read_data(devc)) == MPU_ACK)
ok = 1;
}
else
{
/* Device is not currently open. Use simpler method */
if (read_data(devc) == MPU_ACK)
ok = 1;
}
}
}
if (!ok)
{
spin_unlock_irqrestore(&devc->lock,flags);
return -EIO;
}
if (cmd->nr_args)
{
for (i = 0; i < cmd->nr_args; i++)
{
for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
if (!mpu401_out(dev, cmd->data[i]))
{
spin_unlock_irqrestore(&devc->lock,flags);
printk(KERN_WARNING "mpu401: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
return -EIO;
}
}
}
ret = 0;
cmd->data[0] = 0;
if (cmd->nr_returns)
{
for (i = 0; i < cmd->nr_returns; i++)
{
ok = 0;
for (timeout = 5000; timeout > 0 && !ok; timeout--)
if (input_avail(devc))
{
cmd->data[i] = read_data(devc);
ok = 1;
}
if (!ok)
{
spin_unlock_irqrestore(&devc->lock,flags);
return -EIO;
}
}
}
spin_unlock_irqrestore(&devc->lock,flags);
return ret;
}
static int mpu_cmd(int dev, int cmd, int data)
{
int ret;
static mpu_command_rec rec;
rec.cmd = cmd & 0xff;
rec.nr_args = ((cmd & 0xf0) == 0xE0);
rec.nr_returns = ((cmd & 0xf0) == 0xA0);
rec.data[0] = data & 0xff;
if ((ret = mpu401_command(dev, &rec)) < 0)
return ret;
return (unsigned char) rec.data[0];
}
static int mpu401_prefix_cmd(int dev, unsigned char status)
{
struct mpu_config *devc = &dev_conf[dev];
if (devc->uart_mode)
return 1;
if (status < 0xf0)
{
if (mpu_cmd(dev, 0xD0, 0) < 0)
return 0;
return 1;
}
switch (status)
{
case 0xF0:
if (mpu_cmd(dev, 0xDF, 0) < 0)
return 0;
return 1;
default:
return 0;
}
}
static int mpu401_start_read(int dev)
{
return 0;
}
static int mpu401_end_read(int dev)
{
return 0;
}
static int mpu401_ioctl(int dev, unsigned cmd, void __user *arg)
{
struct mpu_config *devc;
mpu_command_rec rec;
int val, ret;
devc = &dev_conf[dev];
switch (cmd)
{
case SNDCTL_MIDI_MPUMODE:
if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
printk(KERN_WARNING "mpu401: Intelligent mode not supported by the HW\n");
return -EINVAL;
}
if (get_user(val, (int __user *)arg))
return -EFAULT;
set_uart_mode(dev, devc, !val);
return 0;
case SNDCTL_MIDI_MPUCMD:
if (copy_from_user(&rec, arg, sizeof(rec)))
return -EFAULT;
if ((ret = mpu401_command(dev, &rec)) < 0)
return ret;
if (copy_to_user(arg, &rec, sizeof(rec)))
return -EFAULT;
return 0;
default:
return -EINVAL;
}
}
static void mpu401_kick(int dev)
{
}
static int mpu401_buffer_status(int dev)
{
return 0; /*
* No data in buffers
*/
}
static int mpu_synth_ioctl(int dev, unsigned int cmd, void __user *arg)
{
int midi_dev;
struct mpu_config *devc;
midi_dev = synth_devs[dev]->midi_dev;
if (midi_dev < 0 || midi_dev >= num_midis || midi_devs[midi_dev] == NULL)
return -ENXIO;
devc = &dev_conf[midi_dev];
switch (cmd)
{
case SNDCTL_SYNTH_INFO:
if (copy_to_user(arg, &mpu_synth_info[midi_dev],
sizeof(struct synth_info)))
return -EFAULT;
return 0;
case SNDCTL_SYNTH_MEMAVL:
return 0x7fffffff;
default:
return -EINVAL;
}
}
static int mpu_synth_open(int dev, int mode)
{
int midi_dev, err;
struct mpu_config *devc;
struct coproc_operations *coprocessor;
midi_dev = synth_devs[dev]->midi_dev;
if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
return -ENXIO;
devc = &dev_conf[midi_dev];
/*
* Verify that the device is really running.
* Some devices (such as Ensoniq SoundScape don't
* work before the on board processor (OBP) is initialized
* by downloading its microcode.
*/
if (!devc->initialized)
{
if (mpu401_status(devc) == 0xff) /* Bus float */
{
printk(KERN_ERR "mpu401: Device not initialized properly\n");
return -EIO;
}
reset_mpu401(devc);
}
if (devc->opened)
return -EBUSY;
devc->mode = MODE_SYNTH;
devc->synthno = dev;
devc->inputintr = NULL;
coprocessor = midi_devs[midi_dev]->coproc;
if (coprocessor) {
if (!try_module_get(coprocessor->owner))
return -ENODEV;
if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
{
printk(KERN_WARNING "mpu401: Can't access coprocessor device\n");
return err;
}
}
devc->opened = mode;
reset_mpu401(devc);
if (mode & OPEN_READ)
{
mpu_cmd(midi_dev, 0x8B, 0); /* Enable data in stop mode */
mpu_cmd(midi_dev, 0x34, 0); /* Return timing bytes in stop mode */
mpu_cmd(midi_dev, 0x87, 0); /* Enable pitch & controller */
}
return 0;
}
static void mpu_synth_close(int dev)
{
int midi_dev;
struct mpu_config *devc;
struct coproc_operations *coprocessor;
midi_dev = synth_devs[dev]->midi_dev;
devc = &dev_conf[midi_dev];
mpu_cmd(midi_dev, 0x15, 0); /* Stop recording, playback and MIDI */
mpu_cmd(midi_dev, 0x8a, 0); /* Disable data in stopped mode */
devc->inputintr = NULL;
coprocessor = midi_devs[midi_dev]->coproc;
if (coprocessor) {
coprocessor->close(coprocessor->devc, COPR_MIDI);
module_put(coprocessor->owner);
}
devc->opened = 0;
devc->mode = 0;
}
#define MIDI_SYNTH_NAME "MPU-401 UART Midi"
#define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
#include "midi_synth.h"
static struct synth_operations mpu401_synth_proto =
{
.owner = THIS_MODULE,
.id = "MPU401",
.info = NULL,
.midi_dev = 0,
.synth_type = SYNTH_TYPE_MIDI,
.synth_subtype = 0,
.open = mpu_synth_open,
.close = mpu_synth_close,
.ioctl = mpu_synth_ioctl,
.kill_note = midi_synth_kill_note,
.start_note = midi_synth_start_note,
.set_instr = midi_synth_set_instr,
.reset = midi_synth_reset,
.hw_control = midi_synth_hw_control,
.load_patch = midi_synth_load_patch,
.aftertouch = midi_synth_aftertouch,
.controller = midi_synth_controller,
.panning = midi_synth_panning,
.bender = midi_synth_bender,
.setup_voice = midi_synth_setup_voice,
.send_sysex = midi_synth_send_sysex
};
static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
static struct midi_operations mpu401_midi_proto =
{
.owner = THIS_MODULE,
.info = {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
.in_info = {0},
.open = mpu401_open,
.close = mpu401_close,
.ioctl = mpu401_ioctl,
.outputc = mpu401_out,
.start_read = mpu401_start_read,
.end_read = mpu401_end_read,
.kick = mpu401_kick,
.buffer_status = mpu401_buffer_status,
.prefix_cmd = mpu401_prefix_cmd
};
static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
static void mpu401_chk_version(int n, struct mpu_config *devc)
{
int tmp;
devc->version = devc->revision = 0;
tmp = mpu_cmd(n, 0xAC, 0);
if (tmp < 0)
return;
if ((tmp & 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
return;
devc->version = tmp;
if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0) {
devc->version = 0;
return;
}
devc->revision = tmp;
}
int attach_mpu401(struct address_info *hw_config, struct module *owner)
{
unsigned long flags;
char revision_char;
int m, ret;
struct mpu_config *devc;
hw_config->slots[1] = -1;
m = sound_alloc_mididev();
if (m == -1)
{
printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
ret = -ENOMEM;
goto out_err;
}
devc = &dev_conf[m];
devc->base = hw_config->io_base;
devc->osp = hw_config->osp;
devc->irq = hw_config->irq;
devc->opened = 0;
devc->uart_mode = 0;
devc->initialized = 0;
devc->version = 0;
devc->revision = 0;
devc->capabilities = 0;
devc->timer_flag = 0;
devc->m_busy = 0;
devc->m_state = ST_INIT;
devc->shared_irq = hw_config->always_detect;
devc->irq = hw_config->irq;
spin_lock_init(&devc->lock);
if (devc->irq < 0)
{
devc->irq *= -1;
devc->shared_irq = 1;
}
if (!hw_config->always_detect)
{
/* Verify the hardware again */
if (!reset_mpu401(devc))
{
printk(KERN_WARNING "mpu401: Device didn't respond\n");
ret = -ENODEV;
goto out_mididev;
}
if (!devc->shared_irq)
{
if (request_irq(devc->irq, mpuintr, 0, "mpu401",
hw_config) < 0)
{
printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);