Import 2.3.18pre1
[davej-history.git] / drivers / sound / sb_midi.c
blob31a299a96b402cb5a1cc7d38559c4876cd403844
1 /*
2 * sound/sb_dsp.c
4 * The low level driver for the Sound Blaster DS chips.
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 #include <linux/config.h>
15 #include "sound_config.h"
17 #ifdef CONFIG_SBDSP
18 #ifdef CONFIG_MIDI
20 #include "sb.h"
21 #undef SB_TEST_IRQ
24 * The DSP channel can be used either for input or output. Variable
25 * 'sb_irq_mode' will be set when the program calls read or write first time
26 * after open. Current version doesn't support mode changes without closing
27 * and reopening the device. Support for this feature may be implemented in a
28 * future version of this driver.
32 static int sb_midi_open(int dev, int mode,
33 void (*input) (int dev, unsigned char data),
34 void (*output) (int dev)
37 sb_devc *devc = midi_devs[dev]->devc;
38 unsigned long flags;
40 if (devc == NULL)
41 return -ENXIO;
43 save_flags(flags);
44 cli();
45 if (devc->opened)
47 restore_flags(flags);
48 return -EBUSY;
50 devc->opened = 1;
51 restore_flags(flags);
53 devc->irq_mode = IMODE_MIDI;
54 devc->midi_broken = 0;
56 sb_dsp_reset(devc);
58 if (!sb_dsp_command(devc, 0x35)) /* Start MIDI UART mode */
60 devc->opened = 0;
61 return -EIO;
63 devc->intr_active = 1;
65 if (mode & OPEN_READ)
67 devc->input_opened = 1;
68 devc->midi_input_intr = input;
70 return 0;
73 static void sb_midi_close(int dev)
75 sb_devc *devc = midi_devs[dev]->devc;
76 unsigned long flags;
78 if (devc == NULL)
79 return;
81 save_flags(flags);
82 cli();
83 sb_dsp_reset(devc);
84 devc->intr_active = 0;
85 devc->input_opened = 0;
86 devc->opened = 0;
87 restore_flags(flags);
90 static int sb_midi_out(int dev, unsigned char midi_byte)
92 sb_devc *devc = midi_devs[dev]->devc;
94 if (devc == NULL)
95 return 1;
97 if (devc->midi_broken)
98 return 1;
100 if (!sb_dsp_command(devc, midi_byte))
102 devc->midi_broken = 1;
103 return 1;
105 return 1;
108 static int sb_midi_start_read(int dev)
110 return 0;
113 static int sb_midi_end_read(int dev)
115 sb_devc *devc = midi_devs[dev]->devc;
117 if (devc == NULL)
118 return -ENXIO;
120 sb_dsp_reset(devc);
121 devc->intr_active = 0;
122 return 0;
125 static int sb_midi_ioctl(int dev, unsigned cmd, caddr_t arg)
127 return -EINVAL;
130 void sb_midi_interrupt(sb_devc * devc)
132 unsigned long flags;
133 unsigned char data;
135 if (devc == NULL)
136 return;
138 save_flags(flags);
139 cli();
141 data = inb(DSP_READ);
142 if (devc->input_opened)
143 devc->midi_input_intr(devc->my_mididev, data);
145 restore_flags(flags);
148 #define MIDI_SYNTH_NAME "Sound Blaster Midi"
149 #define MIDI_SYNTH_CAPS 0
150 #include "midi_synth.h"
152 static struct midi_operations sb_midi_operations =
155 "Sound Blaster", 0, 0, SNDCARD_SB
157 &std_midi_synth,
158 {0},
159 sb_midi_open,
160 sb_midi_close,
161 sb_midi_ioctl,
162 sb_midi_out,
163 sb_midi_start_read,
164 sb_midi_end_read,
165 NULL,
166 NULL,
167 NULL,
168 NULL
171 void sb_dsp_midi_init(sb_devc * devc)
173 int dev;
175 if (devc->model < 2) /* No MIDI support for SB 1.x */
176 return;
178 dev = sound_alloc_mididev();
180 if (dev == -1)
182 printk(KERN_ERR "sb_midi: too many MIDI devices detected\n");
183 return;
185 std_midi_synth.midi_dev = devc->my_mididev = dev;
186 midi_devs[dev] = (struct midi_operations *)kmalloc(sizeof(struct midi_operations), GFP_KERNEL);
187 if (midi_devs[dev] == NULL)
189 printk(KERN_WARNING "Sound Blaster: failed to allocate MIDI memory.\n");
190 sound_unload_mididev(dev);
191 return;
193 memcpy((char *) midi_devs[dev], (char *) &sb_midi_operations,
194 sizeof(struct midi_operations));
196 midi_devs[dev]->devc = devc;
199 midi_devs[dev]->converter = (struct synth_operations *)kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
200 if (midi_devs[dev]->converter == NULL)
202 printk(KERN_WARNING "Sound Blaster: failed to allocate MIDI memory.\n");
203 kfree(midi_devs[dev]);
204 sound_unload_mididev(dev);
205 return;
207 memcpy((char *) midi_devs[dev]->converter, (char *) &std_midi_synth,
208 sizeof(struct synth_operations));
210 midi_devs[dev]->converter->id = "SBMIDI";
211 sequencer_init();
214 #endif
215 #endif