1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Routines for control of SoundBlaster cards - MIDI interface
8 * Sun May 9 22:54:38 BST 1999 George David Morrison <gdm@gedamo.demon.co.uk>
9 * Fixed typo in snd_sb8dsp_midi_new_device which prevented midi from
12 * Sun May 11 12:34:56 UTC 2003 Clemens Ladisch <clemens@ladisch.de>
13 * Added full duplex UART mode for DSP version 2.0 and later.
17 #include <linux/time.h>
18 #include <sound/core.h>
22 irqreturn_t
snd_sb8dsp_midi_interrupt(struct snd_sb
*chip
)
24 struct snd_rawmidi
*rmidi
;
33 inb(SBP(chip
, DATA_AVAIL
)); /* ack interrupt */
37 spin_lock(&chip
->midi_input_lock
);
39 if (inb(SBP(chip
, DATA_AVAIL
)) & 0x80) {
40 byte
= inb(SBP(chip
, READ
));
41 if (chip
->open
& SB_OPEN_MIDI_INPUT_TRIGGER
) {
42 snd_rawmidi_receive(chip
->midi_substream_input
, &byte
, 1);
46 spin_unlock(&chip
->midi_input_lock
);
50 static int snd_sb8dsp_midi_input_open(struct snd_rawmidi_substream
*substream
)
54 unsigned int valid_open_flags
;
56 chip
= substream
->rmidi
->private_data
;
57 valid_open_flags
= chip
->hardware
>= SB_HW_20
58 ? SB_OPEN_MIDI_OUTPUT
| SB_OPEN_MIDI_OUTPUT_TRIGGER
: 0;
59 spin_lock_irqsave(&chip
->open_lock
, flags
);
60 if (chip
->open
& ~valid_open_flags
) {
61 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
64 chip
->open
|= SB_OPEN_MIDI_INPUT
;
65 chip
->midi_substream_input
= substream
;
66 if (!(chip
->open
& SB_OPEN_MIDI_OUTPUT
)) {
67 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
68 snd_sbdsp_reset(chip
); /* reset DSP */
69 if (chip
->hardware
>= SB_HW_20
)
70 snd_sbdsp_command(chip
, SB_DSP_MIDI_UART_IRQ
);
72 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
77 static int snd_sb8dsp_midi_output_open(struct snd_rawmidi_substream
*substream
)
81 unsigned int valid_open_flags
;
83 chip
= substream
->rmidi
->private_data
;
84 valid_open_flags
= chip
->hardware
>= SB_HW_20
85 ? SB_OPEN_MIDI_INPUT
| SB_OPEN_MIDI_INPUT_TRIGGER
: 0;
86 spin_lock_irqsave(&chip
->open_lock
, flags
);
87 if (chip
->open
& ~valid_open_flags
) {
88 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
91 chip
->open
|= SB_OPEN_MIDI_OUTPUT
;
92 chip
->midi_substream_output
= substream
;
93 if (!(chip
->open
& SB_OPEN_MIDI_INPUT
)) {
94 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
95 snd_sbdsp_reset(chip
); /* reset DSP */
96 if (chip
->hardware
>= SB_HW_20
)
97 snd_sbdsp_command(chip
, SB_DSP_MIDI_UART_IRQ
);
99 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
104 static int snd_sb8dsp_midi_input_close(struct snd_rawmidi_substream
*substream
)
109 chip
= substream
->rmidi
->private_data
;
110 spin_lock_irqsave(&chip
->open_lock
, flags
);
111 chip
->open
&= ~(SB_OPEN_MIDI_INPUT
| SB_OPEN_MIDI_INPUT_TRIGGER
);
112 chip
->midi_substream_input
= NULL
;
113 if (!(chip
->open
& SB_OPEN_MIDI_OUTPUT
)) {
114 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
115 snd_sbdsp_reset(chip
); /* reset DSP */
117 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
122 static int snd_sb8dsp_midi_output_close(struct snd_rawmidi_substream
*substream
)
127 chip
= substream
->rmidi
->private_data
;
128 del_timer_sync(&chip
->midi_timer
);
129 spin_lock_irqsave(&chip
->open_lock
, flags
);
130 chip
->open
&= ~(SB_OPEN_MIDI_OUTPUT
| SB_OPEN_MIDI_OUTPUT_TRIGGER
);
131 chip
->midi_substream_output
= NULL
;
132 if (!(chip
->open
& SB_OPEN_MIDI_INPUT
)) {
133 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
134 snd_sbdsp_reset(chip
); /* reset DSP */
136 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
141 static void snd_sb8dsp_midi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
146 chip
= substream
->rmidi
->private_data
;
147 spin_lock_irqsave(&chip
->open_lock
, flags
);
149 if (!(chip
->open
& SB_OPEN_MIDI_INPUT_TRIGGER
)) {
150 if (chip
->hardware
< SB_HW_20
)
151 snd_sbdsp_command(chip
, SB_DSP_MIDI_INPUT_IRQ
);
152 chip
->open
|= SB_OPEN_MIDI_INPUT_TRIGGER
;
155 if (chip
->open
& SB_OPEN_MIDI_INPUT_TRIGGER
) {
156 if (chip
->hardware
< SB_HW_20
)
157 snd_sbdsp_command(chip
, SB_DSP_MIDI_INPUT_IRQ
);
158 chip
->open
&= ~SB_OPEN_MIDI_INPUT_TRIGGER
;
161 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
164 static void snd_sb8dsp_midi_output_write(struct snd_rawmidi_substream
*substream
)
171 /* how big is Tx FIFO? */
172 chip
= substream
->rmidi
->private_data
;
174 spin_lock_irqsave(&chip
->open_lock
, flags
);
175 if (snd_rawmidi_transmit_peek(substream
, &byte
, 1) != 1) {
176 chip
->open
&= ~SB_OPEN_MIDI_OUTPUT_TRIGGER
;
177 del_timer(&chip
->midi_timer
);
178 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
181 if (chip
->hardware
>= SB_HW_20
) {
183 while ((inb(SBP(chip
, STATUS
)) & 0x80) != 0 && --timeout
> 0)
186 /* Tx FIFO full - try again later */
187 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
190 outb(byte
, SBP(chip
, WRITE
));
192 snd_sbdsp_command(chip
, SB_DSP_MIDI_OUTPUT
);
193 snd_sbdsp_command(chip
, byte
);
195 snd_rawmidi_transmit_ack(substream
, 1);
196 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
200 static void snd_sb8dsp_midi_output_timer(struct timer_list
*t
)
202 struct snd_sb
*chip
= from_timer(chip
, t
, midi_timer
);
203 struct snd_rawmidi_substream
*substream
= chip
->midi_substream_output
;
206 spin_lock_irqsave(&chip
->open_lock
, flags
);
207 mod_timer(&chip
->midi_timer
, 1 + jiffies
);
208 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
209 snd_sb8dsp_midi_output_write(substream
);
212 static void snd_sb8dsp_midi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
217 chip
= substream
->rmidi
->private_data
;
218 spin_lock_irqsave(&chip
->open_lock
, flags
);
220 if (!(chip
->open
& SB_OPEN_MIDI_OUTPUT_TRIGGER
)) {
221 mod_timer(&chip
->midi_timer
, 1 + jiffies
);
222 chip
->open
|= SB_OPEN_MIDI_OUTPUT_TRIGGER
;
225 if (chip
->open
& SB_OPEN_MIDI_OUTPUT_TRIGGER
) {
226 chip
->open
&= ~SB_OPEN_MIDI_OUTPUT_TRIGGER
;
229 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
232 snd_sb8dsp_midi_output_write(substream
);
235 static const struct snd_rawmidi_ops snd_sb8dsp_midi_output
=
237 .open
= snd_sb8dsp_midi_output_open
,
238 .close
= snd_sb8dsp_midi_output_close
,
239 .trigger
= snd_sb8dsp_midi_output_trigger
,
242 static const struct snd_rawmidi_ops snd_sb8dsp_midi_input
=
244 .open
= snd_sb8dsp_midi_input_open
,
245 .close
= snd_sb8dsp_midi_input_close
,
246 .trigger
= snd_sb8dsp_midi_input_trigger
,
249 int snd_sb8dsp_midi(struct snd_sb
*chip
, int device
)
251 struct snd_rawmidi
*rmidi
;
254 if ((err
= snd_rawmidi_new(chip
->card
, "SB8 MIDI", device
, 1, 1, &rmidi
)) < 0)
256 strcpy(rmidi
->name
, "SB8 MIDI");
257 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &snd_sb8dsp_midi_output
);
258 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &snd_sb8dsp_midi_input
);
259 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_OUTPUT
| SNDRV_RAWMIDI_INFO_INPUT
;
260 if (chip
->hardware
>= SB_HW_20
)
261 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_DUPLEX
;
262 rmidi
->private_data
= chip
;
263 timer_setup(&chip
->midi_timer
, snd_sb8dsp_midi_output_timer
, 0);