2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of SoundBlaster cards - MIDI interface
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Sun May 9 22:54:38 BST 1999 George David Morrison <gdm@gedamo.demon.co.uk>
22 * Fixed typo in snd_sb8dsp_midi_new_device which prevented midi from
25 * Sun May 11 12:34:56 UTC 2003 Clemens Ladisch <clemens@ladisch.de>
26 * Added full duplex UART mode for DSP version 2.0 and later.
29 #include <sound/driver.h>
31 #include <linux/time.h>
32 #include <sound/core.h>
39 irqreturn_t snd_sb8dsp_midi_interrupt(sb_t * chip)
45 if (chip == NULL || (rmidi = chip->rmidi) == NULL) {
46 inb(SBP(chip, DATA_AVAIL)); /* ack interrupt */
50 spin_lock(&chip->midi_input_lock);
51 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
52 byte = inb(SBP(chip, READ));
53 if (chip->open & SB_OPEN_MIDI_INPUT_TRIGGER) {
54 spin_unlock(&chip->midi_input_lock);
55 snd_rawmidi_receive(chip->midi_substream_input, &byte, 1);
57 spin_unlock(&chip->midi_input_lock);
60 spin_unlock(&chip->midi_input_lock);
70 static int snd_sb8dsp_midi_input_open(snd_rawmidi_substream_t * substream)
74 unsigned int valid_open_flags;
76 chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return -ENXIO);
77 valid_open_flags = chip->hardware >= SB_HW_20
78 ? SB_OPEN_MIDI_OUTPUT | SB_OPEN_MIDI_OUTPUT_TRIGGER : 0;
79 spin_lock_irqsave(&chip->open_lock, flags);
80 if (chip->open & ~valid_open_flags) {
81 spin_unlock_irqrestore(&chip->open_lock, flags);
84 chip->open |= SB_OPEN_MIDI_INPUT;
85 chip->midi_substream_input = substream;
86 if (!(chip->open & SB_OPEN_MIDI_OUTPUT)) {
87 spin_unlock_irqrestore(&chip->open_lock, flags);
88 snd_sbdsp_reset(chip); /* reset DSP */
89 if (chip->hardware >= SB_HW_20)
90 snd_sbdsp_command(chip, SB_DSP_MIDI_UART_IRQ);
92 spin_unlock_irqrestore(&chip->open_lock, flags);
97 static int snd_sb8dsp_midi_output_open(snd_rawmidi_substream_t * substream)
101 unsigned int valid_open_flags;
103 chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return -ENXIO);
104 valid_open_flags = chip->hardware >= SB_HW_20
105 ? SB_OPEN_MIDI_INPUT | SB_OPEN_MIDI_INPUT_TRIGGER : 0;
106 spin_lock_irqsave(&chip->open_lock, flags);
107 if (chip->open & ~valid_open_flags) {
108 spin_unlock_irqrestore(&chip->open_lock, flags);
111 chip->open |= SB_OPEN_MIDI_OUTPUT;
112 chip->midi_substream_output = substream;
113 if (!(chip->open & SB_OPEN_MIDI_INPUT)) {
114 spin_unlock_irqrestore(&chip->open_lock, flags);
115 snd_sbdsp_reset(chip); /* reset DSP */
116 if (chip->hardware >= SB_HW_20)
117 snd_sbdsp_command(chip, SB_DSP_MIDI_UART_IRQ);
119 spin_unlock_irqrestore(&chip->open_lock, flags);
124 static int snd_sb8dsp_midi_input_close(snd_rawmidi_substream_t * substream)
129 chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return -ENXIO);
130 spin_lock_irqsave(&chip->open_lock, flags);
131 chip->open &= ~(SB_OPEN_MIDI_INPUT | SB_OPEN_MIDI_INPUT_TRIGGER);
132 chip->midi_substream_input = NULL;
133 if (!(chip->open & SB_OPEN_MIDI_OUTPUT)) {
134 spin_unlock_irqrestore(&chip->open_lock, flags);
135 snd_sbdsp_reset(chip); /* reset DSP */
137 spin_unlock_irqrestore(&chip->open_lock, flags);
142 static int snd_sb8dsp_midi_output_close(snd_rawmidi_substream_t * substream)
147 chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return -ENXIO);
148 spin_lock_irqsave(&chip->open_lock, flags);
149 chip->open &= ~(SB_OPEN_MIDI_OUTPUT | SB_OPEN_MIDI_OUTPUT_TRIGGER);
150 chip->midi_substream_output = NULL;
151 if (!(chip->open & SB_OPEN_MIDI_INPUT)) {
152 spin_unlock_irqrestore(&chip->open_lock, flags);
153 snd_sbdsp_reset(chip); /* reset DSP */
155 spin_unlock_irqrestore(&chip->open_lock, flags);
160 static void snd_sb8dsp_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
165 chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return);
166 spin_lock_irqsave(&chip->open_lock, flags);
168 if (!(chip->open & SB_OPEN_MIDI_INPUT_TRIGGER)) {
169 if (chip->hardware < SB_HW_20)
170 snd_sbdsp_command(chip, SB_DSP_MIDI_INPUT_IRQ);
171 chip->open |= SB_OPEN_MIDI_INPUT_TRIGGER;
174 if (chip->open & SB_OPEN_MIDI_INPUT_TRIGGER) {
175 if (chip->hardware < SB_HW_20)
176 snd_sbdsp_command(chip, SB_DSP_MIDI_INPUT_IRQ);
177 chip->open &= ~SB_OPEN_MIDI_INPUT_TRIGGER;
180 spin_unlock_irqrestore(&chip->open_lock, flags);
183 static void snd_sb8dsp_midi_output_write(snd_rawmidi_substream_t * substream)
190 /* how big is Tx FIFO? */
191 chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return);
193 spin_lock_irqsave(&chip->open_lock, flags);
194 if (snd_rawmidi_transmit_peek(substream, &byte, 1) != 1) {
195 chip->open &= ~SB_OPEN_MIDI_OUTPUT_TRIGGER;
196 del_timer(&chip->midi_timer);
197 spin_unlock_irqrestore(&chip->open_lock, flags);
200 if (chip->hardware >= SB_HW_20) {
202 while ((inb(SBP(chip, STATUS)) & 0x80) != 0 && --timeout > 0)
205 /* Tx FIFO full - try again later */
206 spin_unlock_irqrestore(&chip->open_lock, flags);
209 outb(byte, SBP(chip, WRITE));
211 snd_sbdsp_command(chip, SB_DSP_MIDI_OUTPUT);
212 snd_sbdsp_command(chip, byte);
214 snd_rawmidi_transmit_ack(substream, 1);
215 spin_unlock_irqrestore(&chip->open_lock, flags);
219 static void snd_sb8dsp_midi_output_timer(unsigned long data)
221 snd_rawmidi_substream_t * substream = (snd_rawmidi_substream_t *) data;
222 sb_t * chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return);
225 spin_lock_irqsave(&chip->open_lock, flags);
226 chip->midi_timer.expires = 1 + jiffies;
227 add_timer(&chip->midi_timer);
228 spin_unlock_irqrestore(&chip->open_lock, flags);
229 snd_sb8dsp_midi_output_write(substream);
232 static void snd_sb8dsp_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
237 chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return);
238 spin_lock_irqsave(&chip->open_lock, flags);
240 if (!(chip->open & SB_OPEN_MIDI_OUTPUT_TRIGGER)) {
241 init_timer(&chip->midi_timer);
242 chip->midi_timer.function = snd_sb8dsp_midi_output_timer;
243 chip->midi_timer.data = (unsigned long) substream;
244 chip->midi_timer.expires = 1 + jiffies;
245 add_timer(&chip->midi_timer);
246 chip->open |= SB_OPEN_MIDI_OUTPUT_TRIGGER;
249 if (chip->open & SB_OPEN_MIDI_OUTPUT_TRIGGER) {
250 chip->open &= ~SB_OPEN_MIDI_OUTPUT_TRIGGER;
253 spin_unlock_irqrestore(&chip->open_lock, flags);
256 snd_sb8dsp_midi_output_write(substream);
263 static snd_rawmidi_ops_t snd_sb8dsp_midi_output =
265 .open = snd_sb8dsp_midi_output_open,
266 .close = snd_sb8dsp_midi_output_close,
267 .trigger = snd_sb8dsp_midi_output_trigger,
270 static snd_rawmidi_ops_t snd_sb8dsp_midi_input =
272 .open = snd_sb8dsp_midi_input_open,
273 .close = snd_sb8dsp_midi_input_close,
274 .trigger = snd_sb8dsp_midi_input_trigger,
277 int snd_sb8dsp_midi(sb_t *chip, int device, snd_rawmidi_t ** rrawmidi)
279 snd_rawmidi_t *rmidi;
284 if ((err = snd_rawmidi_new(chip->card, "SB8 MIDI", device, 1, 1, &rmidi)) < 0)
286 strcpy(rmidi->name, "SB8 MIDI");
287 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_sb8dsp_midi_output);
288 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_sb8dsp_midi_input);
289 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT;
290 if (chip->hardware >= SB_HW_20)
291 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
292 rmidi->private_data = chip;