2 * Copyright (c) by Jaroslav Kysela <perex@perex.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.
30 #include <linux/time.h>
31 #include <sound/core.h>
35 irqreturn_t
snd_sb8dsp_midi_interrupt(struct snd_sb
*chip
)
37 struct snd_rawmidi
*rmidi
;
46 inb(SBP(chip
, DATA_AVAIL
)); /* ack interrupt */
50 spin_lock(&chip
->midi_input_lock
);
52 if (inb(SBP(chip
, DATA_AVAIL
)) & 0x80) {
53 byte
= inb(SBP(chip
, READ
));
54 if (chip
->open
& SB_OPEN_MIDI_INPUT_TRIGGER
) {
55 snd_rawmidi_receive(chip
->midi_substream_input
, &byte
, 1);
59 spin_unlock(&chip
->midi_input_lock
);
63 static int snd_sb8dsp_midi_input_open(struct snd_rawmidi_substream
*substream
)
67 unsigned int valid_open_flags
;
69 chip
= substream
->rmidi
->private_data
;
70 valid_open_flags
= chip
->hardware
>= SB_HW_20
71 ? SB_OPEN_MIDI_OUTPUT
| SB_OPEN_MIDI_OUTPUT_TRIGGER
: 0;
72 spin_lock_irqsave(&chip
->open_lock
, flags
);
73 if (chip
->open
& ~valid_open_flags
) {
74 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
77 chip
->open
|= SB_OPEN_MIDI_INPUT
;
78 chip
->midi_substream_input
= substream
;
79 if (!(chip
->open
& SB_OPEN_MIDI_OUTPUT
)) {
80 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
81 snd_sbdsp_reset(chip
); /* reset DSP */
82 if (chip
->hardware
>= SB_HW_20
)
83 snd_sbdsp_command(chip
, SB_DSP_MIDI_UART_IRQ
);
85 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
90 static int snd_sb8dsp_midi_output_open(struct snd_rawmidi_substream
*substream
)
94 unsigned int valid_open_flags
;
96 chip
= substream
->rmidi
->private_data
;
97 valid_open_flags
= chip
->hardware
>= SB_HW_20
98 ? SB_OPEN_MIDI_INPUT
| SB_OPEN_MIDI_INPUT_TRIGGER
: 0;
99 spin_lock_irqsave(&chip
->open_lock
, flags
);
100 if (chip
->open
& ~valid_open_flags
) {
101 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
104 chip
->open
|= SB_OPEN_MIDI_OUTPUT
;
105 chip
->midi_substream_output
= substream
;
106 if (!(chip
->open
& SB_OPEN_MIDI_INPUT
)) {
107 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
108 snd_sbdsp_reset(chip
); /* reset DSP */
109 if (chip
->hardware
>= SB_HW_20
)
110 snd_sbdsp_command(chip
, SB_DSP_MIDI_UART_IRQ
);
112 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
117 static int snd_sb8dsp_midi_input_close(struct snd_rawmidi_substream
*substream
)
122 chip
= substream
->rmidi
->private_data
;
123 spin_lock_irqsave(&chip
->open_lock
, flags
);
124 chip
->open
&= ~(SB_OPEN_MIDI_INPUT
| SB_OPEN_MIDI_INPUT_TRIGGER
);
125 chip
->midi_substream_input
= NULL
;
126 if (!(chip
->open
& SB_OPEN_MIDI_OUTPUT
)) {
127 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
128 snd_sbdsp_reset(chip
); /* reset DSP */
130 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
135 static int snd_sb8dsp_midi_output_close(struct snd_rawmidi_substream
*substream
)
140 chip
= substream
->rmidi
->private_data
;
141 spin_lock_irqsave(&chip
->open_lock
, flags
);
142 chip
->open
&= ~(SB_OPEN_MIDI_OUTPUT
| SB_OPEN_MIDI_OUTPUT_TRIGGER
);
143 chip
->midi_substream_output
= NULL
;
144 if (!(chip
->open
& SB_OPEN_MIDI_INPUT
)) {
145 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
146 snd_sbdsp_reset(chip
); /* reset DSP */
148 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
153 static void snd_sb8dsp_midi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
158 chip
= substream
->rmidi
->private_data
;
159 spin_lock_irqsave(&chip
->open_lock
, flags
);
161 if (!(chip
->open
& SB_OPEN_MIDI_INPUT_TRIGGER
)) {
162 if (chip
->hardware
< SB_HW_20
)
163 snd_sbdsp_command(chip
, SB_DSP_MIDI_INPUT_IRQ
);
164 chip
->open
|= SB_OPEN_MIDI_INPUT_TRIGGER
;
167 if (chip
->open
& SB_OPEN_MIDI_INPUT_TRIGGER
) {
168 if (chip
->hardware
< SB_HW_20
)
169 snd_sbdsp_command(chip
, SB_DSP_MIDI_INPUT_IRQ
);
170 chip
->open
&= ~SB_OPEN_MIDI_INPUT_TRIGGER
;
173 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
176 static void snd_sb8dsp_midi_output_write(struct snd_rawmidi_substream
*substream
)
183 /* how big is Tx FIFO? */
184 chip
= substream
->rmidi
->private_data
;
186 spin_lock_irqsave(&chip
->open_lock
, flags
);
187 if (snd_rawmidi_transmit_peek(substream
, &byte
, 1) != 1) {
188 chip
->open
&= ~SB_OPEN_MIDI_OUTPUT_TRIGGER
;
189 del_timer(&chip
->midi_timer
);
190 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
193 if (chip
->hardware
>= SB_HW_20
) {
195 while ((inb(SBP(chip
, STATUS
)) & 0x80) != 0 && --timeout
> 0)
198 /* Tx FIFO full - try again later */
199 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
202 outb(byte
, SBP(chip
, WRITE
));
204 snd_sbdsp_command(chip
, SB_DSP_MIDI_OUTPUT
);
205 snd_sbdsp_command(chip
, byte
);
207 snd_rawmidi_transmit_ack(substream
, 1);
208 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
212 static void snd_sb8dsp_midi_output_timer(unsigned long data
)
214 struct snd_rawmidi_substream
*substream
= (struct snd_rawmidi_substream
*) data
;
215 struct snd_sb
* chip
= substream
->rmidi
->private_data
;
218 spin_lock_irqsave(&chip
->open_lock
, flags
);
219 chip
->midi_timer
.expires
= 1 + jiffies
;
220 add_timer(&chip
->midi_timer
);
221 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
222 snd_sb8dsp_midi_output_write(substream
);
225 static void snd_sb8dsp_midi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
230 chip
= substream
->rmidi
->private_data
;
231 spin_lock_irqsave(&chip
->open_lock
, flags
);
233 if (!(chip
->open
& SB_OPEN_MIDI_OUTPUT_TRIGGER
)) {
234 init_timer(&chip
->midi_timer
);
235 chip
->midi_timer
.function
= snd_sb8dsp_midi_output_timer
;
236 chip
->midi_timer
.data
= (unsigned long) substream
;
237 chip
->midi_timer
.expires
= 1 + jiffies
;
238 add_timer(&chip
->midi_timer
);
239 chip
->open
|= SB_OPEN_MIDI_OUTPUT_TRIGGER
;
242 if (chip
->open
& SB_OPEN_MIDI_OUTPUT_TRIGGER
) {
243 chip
->open
&= ~SB_OPEN_MIDI_OUTPUT_TRIGGER
;
246 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
249 snd_sb8dsp_midi_output_write(substream
);
252 static struct snd_rawmidi_ops snd_sb8dsp_midi_output
=
254 .open
= snd_sb8dsp_midi_output_open
,
255 .close
= snd_sb8dsp_midi_output_close
,
256 .trigger
= snd_sb8dsp_midi_output_trigger
,
259 static struct snd_rawmidi_ops snd_sb8dsp_midi_input
=
261 .open
= snd_sb8dsp_midi_input_open
,
262 .close
= snd_sb8dsp_midi_input_close
,
263 .trigger
= snd_sb8dsp_midi_input_trigger
,
266 int snd_sb8dsp_midi(struct snd_sb
*chip
, int device
, struct snd_rawmidi
** rrawmidi
)
268 struct snd_rawmidi
*rmidi
;
273 if ((err
= snd_rawmidi_new(chip
->card
, "SB8 MIDI", device
, 1, 1, &rmidi
)) < 0)
275 strcpy(rmidi
->name
, "SB8 MIDI");
276 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &snd_sb8dsp_midi_output
);
277 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &snd_sb8dsp_midi_input
);
278 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_OUTPUT
| SNDRV_RAWMIDI_INFO_INPUT
;
279 if (chip
->hardware
>= SB_HW_20
)
280 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_DUPLEX
;
281 rmidi
->private_data
= chip
;