xref: /OK3568_Linux_fs/kernel/sound/isa/wavefront/wavefront_midi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) by Paul Barton-Davis 1998-1999
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun /* The low level driver for the WaveFront ICS2115 MIDI interface(s)
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Note that there is also an MPU-401 emulation (actually, a UART-401
9*4882a593Smuzhiyun  * emulation) on the CS4232 on the Tropez and Tropez Plus. This code
10*4882a593Smuzhiyun  * has nothing to do with that interface at all.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * The interface is essentially just a UART-401, but is has the
13*4882a593Smuzhiyun  * interesting property of supporting what Turtle Beach called
14*4882a593Smuzhiyun  * "Virtual MIDI" mode. In this mode, there are effectively *two*
15*4882a593Smuzhiyun  * MIDI buses accessible via the interface, one that is routed
16*4882a593Smuzhiyun  * solely to/from the external WaveFront synthesizer and the other
17*4882a593Smuzhiyun  * corresponding to the pin/socket connector used to link external
18*4882a593Smuzhiyun  * MIDI devices to the board.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * This driver fully supports this mode, allowing two distinct MIDI
21*4882a593Smuzhiyun  * busses to be used completely independently, giving 32 channels of
22*4882a593Smuzhiyun  * MIDI routing, 16 to the WaveFront synth and 16 to the external MIDI
23*4882a593Smuzhiyun  * bus. The devices are named /dev/snd/midiCnD0 and /dev/snd/midiCnD1,
24*4882a593Smuzhiyun  * where `n' is the card number. Note that the device numbers may be
25*4882a593Smuzhiyun  * something other than 0 and 1 if the CS4232 UART/MPU-401 interface
26*4882a593Smuzhiyun  * is enabled.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * Switching between the two is accomplished externally by the driver
29*4882a593Smuzhiyun  * using the two otherwise unused MIDI bytes. See the code for more details.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * NOTE: VIRTUAL MIDI MODE IS ON BY DEFAULT (see lowlevel/isa/wavefront.c)
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * The main reason to turn off Virtual MIDI mode is when you want to
34*4882a593Smuzhiyun  * tightly couple the WaveFront synth with an external MIDI
35*4882a593Smuzhiyun  * device. You won't be able to distinguish the source of any MIDI
36*4882a593Smuzhiyun  * data except via SysEx ID, but thats probably OK, since for the most
37*4882a593Smuzhiyun  * part, the WaveFront won't be sending any MIDI data at all.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * The main reason to turn on Virtual MIDI Mode is to provide two
40*4882a593Smuzhiyun  * completely independent 16-channel MIDI buses, one to the
41*4882a593Smuzhiyun  * WaveFront and one to any external MIDI devices. Given the 32
42*4882a593Smuzhiyun  * voice nature of the WaveFront, its pretty easy to find a use
43*4882a593Smuzhiyun  * for all 16 channels driving just that synth.
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  */
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #include <linux/io.h>
48*4882a593Smuzhiyun #include <linux/init.h>
49*4882a593Smuzhiyun #include <linux/time.h>
50*4882a593Smuzhiyun #include <linux/wait.h>
51*4882a593Smuzhiyun #include <sound/core.h>
52*4882a593Smuzhiyun #include <sound/snd_wavefront.h>
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun static inline int
wf_mpu_status(snd_wavefront_midi_t * midi)55*4882a593Smuzhiyun wf_mpu_status (snd_wavefront_midi_t *midi)
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	return inb (midi->mpu_status_port);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static inline int
input_avail(snd_wavefront_midi_t * midi)62*4882a593Smuzhiyun input_avail (snd_wavefront_midi_t *midi)
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	return !(wf_mpu_status(midi) & INPUT_AVAIL);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun static inline int
output_ready(snd_wavefront_midi_t * midi)69*4882a593Smuzhiyun output_ready (snd_wavefront_midi_t *midi)
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	return !(wf_mpu_status(midi) & OUTPUT_READY);
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun static inline int
read_data(snd_wavefront_midi_t * midi)76*4882a593Smuzhiyun read_data (snd_wavefront_midi_t *midi)
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	return inb (midi->mpu_data_port);
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun static inline void
write_data(snd_wavefront_midi_t * midi,unsigned char byte)83*4882a593Smuzhiyun write_data (snd_wavefront_midi_t *midi, unsigned char byte)
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	outb (byte, midi->mpu_data_port);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun static snd_wavefront_midi_t *
get_wavefront_midi(struct snd_rawmidi_substream * substream)90*4882a593Smuzhiyun get_wavefront_midi (struct snd_rawmidi_substream *substream)
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	struct snd_card *card;
94*4882a593Smuzhiyun 	snd_wavefront_card_t *acard;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	if (substream == NULL || substream->rmidi == NULL)
97*4882a593Smuzhiyun 	        return NULL;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	card = substream->rmidi->card;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	if (card == NULL)
102*4882a593Smuzhiyun 	        return NULL;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	if (card->private_data == NULL)
105*4882a593Smuzhiyun  	        return NULL;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	acard = card->private_data;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	return &acard->wavefront.midi;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
snd_wavefront_midi_output_write(snd_wavefront_card_t * card)112*4882a593Smuzhiyun static void snd_wavefront_midi_output_write(snd_wavefront_card_t *card)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	snd_wavefront_midi_t *midi = &card->wavefront.midi;
115*4882a593Smuzhiyun 	snd_wavefront_mpu_id  mpu;
116*4882a593Smuzhiyun 	unsigned long flags;
117*4882a593Smuzhiyun 	unsigned char midi_byte;
118*4882a593Smuzhiyun 	int max = 256, mask = 1;
119*4882a593Smuzhiyun 	int timeout;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* Its not OK to try to change the status of "virtuality" of
122*4882a593Smuzhiyun 	   the MIDI interface while we're outputting stuff.  See
123*4882a593Smuzhiyun 	   snd_wavefront_midi_{enable,disable}_virtual () for the
124*4882a593Smuzhiyun 	   other half of this.
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	   The first loop attempts to flush any data from the
127*4882a593Smuzhiyun 	   current output device, and then the second
128*4882a593Smuzhiyun 	   emits the switch byte (if necessary), and starts
129*4882a593Smuzhiyun 	   outputting data for the output device currently in use.
130*4882a593Smuzhiyun 	*/
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	if (midi->substream_output[midi->output_mpu] == NULL) {
133*4882a593Smuzhiyun 		goto __second;
134*4882a593Smuzhiyun 	}
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	while (max > 0) {
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 		/* XXX fix me - no hard timing loops allowed! */
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 		for (timeout = 30000; timeout > 0; timeout--) {
141*4882a593Smuzhiyun 			if (output_ready (midi))
142*4882a593Smuzhiyun 				break;
143*4882a593Smuzhiyun 		}
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 		spin_lock_irqsave (&midi->virtual, flags);
146*4882a593Smuzhiyun 		if ((midi->mode[midi->output_mpu] & MPU401_MODE_OUTPUT) == 0) {
147*4882a593Smuzhiyun 			spin_unlock_irqrestore (&midi->virtual, flags);
148*4882a593Smuzhiyun 			goto __second;
149*4882a593Smuzhiyun 		}
150*4882a593Smuzhiyun 		if (output_ready (midi)) {
151*4882a593Smuzhiyun 			if (snd_rawmidi_transmit(midi->substream_output[midi->output_mpu], &midi_byte, 1) == 1) {
152*4882a593Smuzhiyun 				if (!midi->isvirtual ||
153*4882a593Smuzhiyun 					(midi_byte != WF_INTERNAL_SWITCH &&
154*4882a593Smuzhiyun 					 midi_byte != WF_EXTERNAL_SWITCH))
155*4882a593Smuzhiyun 					write_data(midi, midi_byte);
156*4882a593Smuzhiyun 				max--;
157*4882a593Smuzhiyun 			} else {
158*4882a593Smuzhiyun 				if (midi->istimer) {
159*4882a593Smuzhiyun 					if (--midi->istimer <= 0)
160*4882a593Smuzhiyun 						del_timer(&midi->timer);
161*4882a593Smuzhiyun 				}
162*4882a593Smuzhiyun 				midi->mode[midi->output_mpu] &= ~MPU401_MODE_OUTPUT_TRIGGER;
163*4882a593Smuzhiyun 				spin_unlock_irqrestore (&midi->virtual, flags);
164*4882a593Smuzhiyun 				goto __second;
165*4882a593Smuzhiyun 			}
166*4882a593Smuzhiyun 		} else {
167*4882a593Smuzhiyun 			spin_unlock_irqrestore (&midi->virtual, flags);
168*4882a593Smuzhiyun 			return;
169*4882a593Smuzhiyun 		}
170*4882a593Smuzhiyun 		spin_unlock_irqrestore (&midi->virtual, flags);
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun       __second:
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (midi->substream_output[!midi->output_mpu] == NULL) {
176*4882a593Smuzhiyun 		return;
177*4882a593Smuzhiyun 	}
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	while (max > 0) {
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 		/* XXX fix me - no hard timing loops allowed! */
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		for (timeout = 30000; timeout > 0; timeout--) {
184*4882a593Smuzhiyun 			if (output_ready (midi))
185*4882a593Smuzhiyun 				break;
186*4882a593Smuzhiyun 		}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 		spin_lock_irqsave (&midi->virtual, flags);
189*4882a593Smuzhiyun 		if (!midi->isvirtual)
190*4882a593Smuzhiyun 			mask = 0;
191*4882a593Smuzhiyun 		mpu = midi->output_mpu ^ mask;
192*4882a593Smuzhiyun 		mask = 0;	/* don't invert the value from now */
193*4882a593Smuzhiyun 		if ((midi->mode[mpu] & MPU401_MODE_OUTPUT) == 0) {
194*4882a593Smuzhiyun 			spin_unlock_irqrestore (&midi->virtual, flags);
195*4882a593Smuzhiyun 			return;
196*4882a593Smuzhiyun 		}
197*4882a593Smuzhiyun 		if (snd_rawmidi_transmit_empty(midi->substream_output[mpu]))
198*4882a593Smuzhiyun 			goto __timer;
199*4882a593Smuzhiyun 		if (output_ready (midi)) {
200*4882a593Smuzhiyun 			if (mpu != midi->output_mpu) {
201*4882a593Smuzhiyun 				write_data(midi, mpu == internal_mpu ?
202*4882a593Smuzhiyun 							WF_INTERNAL_SWITCH :
203*4882a593Smuzhiyun 							WF_EXTERNAL_SWITCH);
204*4882a593Smuzhiyun 				midi->output_mpu = mpu;
205*4882a593Smuzhiyun 			} else if (snd_rawmidi_transmit(midi->substream_output[mpu], &midi_byte, 1) == 1) {
206*4882a593Smuzhiyun 				if (!midi->isvirtual ||
207*4882a593Smuzhiyun 					(midi_byte != WF_INTERNAL_SWITCH &&
208*4882a593Smuzhiyun 					 midi_byte != WF_EXTERNAL_SWITCH))
209*4882a593Smuzhiyun 					write_data(midi, midi_byte);
210*4882a593Smuzhiyun 				max--;
211*4882a593Smuzhiyun 			} else {
212*4882a593Smuzhiyun 			      __timer:
213*4882a593Smuzhiyun 				if (midi->istimer) {
214*4882a593Smuzhiyun 					if (--midi->istimer <= 0)
215*4882a593Smuzhiyun 						del_timer(&midi->timer);
216*4882a593Smuzhiyun 				}
217*4882a593Smuzhiyun 				midi->mode[mpu] &= ~MPU401_MODE_OUTPUT_TRIGGER;
218*4882a593Smuzhiyun 				spin_unlock_irqrestore (&midi->virtual, flags);
219*4882a593Smuzhiyun 				return;
220*4882a593Smuzhiyun 			}
221*4882a593Smuzhiyun 		} else {
222*4882a593Smuzhiyun 			spin_unlock_irqrestore (&midi->virtual, flags);
223*4882a593Smuzhiyun 			return;
224*4882a593Smuzhiyun 		}
225*4882a593Smuzhiyun 		spin_unlock_irqrestore (&midi->virtual, flags);
226*4882a593Smuzhiyun 	}
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
snd_wavefront_midi_input_open(struct snd_rawmidi_substream * substream)229*4882a593Smuzhiyun static int snd_wavefront_midi_input_open(struct snd_rawmidi_substream *substream)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	unsigned long flags;
232*4882a593Smuzhiyun 	snd_wavefront_midi_t *midi;
233*4882a593Smuzhiyun 	snd_wavefront_mpu_id mpu;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	if (snd_BUG_ON(!substream || !substream->rmidi))
236*4882a593Smuzhiyun 		return -ENXIO;
237*4882a593Smuzhiyun 	if (snd_BUG_ON(!substream->rmidi->private_data))
238*4882a593Smuzhiyun 		return -ENXIO;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	if ((midi = get_wavefront_midi (substream)) == NULL)
243*4882a593Smuzhiyun 	        return -EIO;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	spin_lock_irqsave (&midi->open, flags);
246*4882a593Smuzhiyun 	midi->mode[mpu] |= MPU401_MODE_INPUT;
247*4882a593Smuzhiyun 	midi->substream_input[mpu] = substream;
248*4882a593Smuzhiyun 	spin_unlock_irqrestore (&midi->open, flags);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	return 0;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
snd_wavefront_midi_output_open(struct snd_rawmidi_substream * substream)253*4882a593Smuzhiyun static int snd_wavefront_midi_output_open(struct snd_rawmidi_substream *substream)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	unsigned long flags;
256*4882a593Smuzhiyun 	snd_wavefront_midi_t *midi;
257*4882a593Smuzhiyun 	snd_wavefront_mpu_id mpu;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	if (snd_BUG_ON(!substream || !substream->rmidi))
260*4882a593Smuzhiyun 		return -ENXIO;
261*4882a593Smuzhiyun 	if (snd_BUG_ON(!substream->rmidi->private_data))
262*4882a593Smuzhiyun 		return -ENXIO;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	if ((midi = get_wavefront_midi (substream)) == NULL)
267*4882a593Smuzhiyun 	        return -EIO;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	spin_lock_irqsave (&midi->open, flags);
270*4882a593Smuzhiyun 	midi->mode[mpu] |= MPU401_MODE_OUTPUT;
271*4882a593Smuzhiyun 	midi->substream_output[mpu] = substream;
272*4882a593Smuzhiyun 	spin_unlock_irqrestore (&midi->open, flags);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	return 0;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun 
snd_wavefront_midi_input_close(struct snd_rawmidi_substream * substream)277*4882a593Smuzhiyun static int snd_wavefront_midi_input_close(struct snd_rawmidi_substream *substream)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	unsigned long flags;
280*4882a593Smuzhiyun 	snd_wavefront_midi_t *midi;
281*4882a593Smuzhiyun 	snd_wavefront_mpu_id mpu;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	if (snd_BUG_ON(!substream || !substream->rmidi))
284*4882a593Smuzhiyun 		return -ENXIO;
285*4882a593Smuzhiyun 	if (snd_BUG_ON(!substream->rmidi->private_data))
286*4882a593Smuzhiyun 		return -ENXIO;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	if ((midi = get_wavefront_midi (substream)) == NULL)
291*4882a593Smuzhiyun 	        return -EIO;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	spin_lock_irqsave (&midi->open, flags);
294*4882a593Smuzhiyun 	midi->mode[mpu] &= ~MPU401_MODE_INPUT;
295*4882a593Smuzhiyun 	spin_unlock_irqrestore (&midi->open, flags);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	return 0;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun 
snd_wavefront_midi_output_close(struct snd_rawmidi_substream * substream)300*4882a593Smuzhiyun static int snd_wavefront_midi_output_close(struct snd_rawmidi_substream *substream)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	unsigned long flags;
303*4882a593Smuzhiyun 	snd_wavefront_midi_t *midi;
304*4882a593Smuzhiyun 	snd_wavefront_mpu_id mpu;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	if (snd_BUG_ON(!substream || !substream->rmidi))
307*4882a593Smuzhiyun 		return -ENXIO;
308*4882a593Smuzhiyun 	if (snd_BUG_ON(!substream->rmidi->private_data))
309*4882a593Smuzhiyun 		return -ENXIO;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	if ((midi = get_wavefront_midi (substream)) == NULL)
314*4882a593Smuzhiyun 	        return -EIO;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	spin_lock_irqsave (&midi->open, flags);
317*4882a593Smuzhiyun 	midi->mode[mpu] &= ~MPU401_MODE_OUTPUT;
318*4882a593Smuzhiyun 	spin_unlock_irqrestore (&midi->open, flags);
319*4882a593Smuzhiyun 	return 0;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun 
snd_wavefront_midi_input_trigger(struct snd_rawmidi_substream * substream,int up)322*4882a593Smuzhiyun static void snd_wavefront_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	unsigned long flags;
325*4882a593Smuzhiyun 	snd_wavefront_midi_t *midi;
326*4882a593Smuzhiyun 	snd_wavefront_mpu_id mpu;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (substream == NULL || substream->rmidi == NULL)
329*4882a593Smuzhiyun 	        return;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	if (substream->rmidi->private_data == NULL)
332*4882a593Smuzhiyun 	        return;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	if ((midi = get_wavefront_midi (substream)) == NULL) {
337*4882a593Smuzhiyun 		return;
338*4882a593Smuzhiyun 	}
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	spin_lock_irqsave (&midi->virtual, flags);
341*4882a593Smuzhiyun 	if (up) {
342*4882a593Smuzhiyun 		midi->mode[mpu] |= MPU401_MODE_INPUT_TRIGGER;
343*4882a593Smuzhiyun 	} else {
344*4882a593Smuzhiyun 		midi->mode[mpu] &= ~MPU401_MODE_INPUT_TRIGGER;
345*4882a593Smuzhiyun 	}
346*4882a593Smuzhiyun 	spin_unlock_irqrestore (&midi->virtual, flags);
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
snd_wavefront_midi_output_timer(struct timer_list * t)349*4882a593Smuzhiyun static void snd_wavefront_midi_output_timer(struct timer_list *t)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun 	snd_wavefront_midi_t *midi = from_timer(midi, t, timer);
352*4882a593Smuzhiyun 	snd_wavefront_card_t *card = midi->timer_card;
353*4882a593Smuzhiyun 	unsigned long flags;
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	spin_lock_irqsave (&midi->virtual, flags);
356*4882a593Smuzhiyun 	mod_timer(&midi->timer, 1 + jiffies);
357*4882a593Smuzhiyun 	spin_unlock_irqrestore (&midi->virtual, flags);
358*4882a593Smuzhiyun 	snd_wavefront_midi_output_write(card);
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun 
snd_wavefront_midi_output_trigger(struct snd_rawmidi_substream * substream,int up)361*4882a593Smuzhiyun static void snd_wavefront_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun 	unsigned long flags;
364*4882a593Smuzhiyun 	snd_wavefront_midi_t *midi;
365*4882a593Smuzhiyun 	snd_wavefront_mpu_id mpu;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	if (substream == NULL || substream->rmidi == NULL)
368*4882a593Smuzhiyun 	        return;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	if (substream->rmidi->private_data == NULL)
371*4882a593Smuzhiyun 	        return;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	if ((midi = get_wavefront_midi (substream)) == NULL) {
376*4882a593Smuzhiyun 		return;
377*4882a593Smuzhiyun 	}
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	spin_lock_irqsave (&midi->virtual, flags);
380*4882a593Smuzhiyun 	if (up) {
381*4882a593Smuzhiyun 		if ((midi->mode[mpu] & MPU401_MODE_OUTPUT_TRIGGER) == 0) {
382*4882a593Smuzhiyun 			if (!midi->istimer) {
383*4882a593Smuzhiyun 				timer_setup(&midi->timer,
384*4882a593Smuzhiyun 					    snd_wavefront_midi_output_timer,
385*4882a593Smuzhiyun 					    0);
386*4882a593Smuzhiyun 				mod_timer(&midi->timer, 1 + jiffies);
387*4882a593Smuzhiyun 			}
388*4882a593Smuzhiyun 			midi->istimer++;
389*4882a593Smuzhiyun 			midi->mode[mpu] |= MPU401_MODE_OUTPUT_TRIGGER;
390*4882a593Smuzhiyun 		}
391*4882a593Smuzhiyun 	} else {
392*4882a593Smuzhiyun 		midi->mode[mpu] &= ~MPU401_MODE_OUTPUT_TRIGGER;
393*4882a593Smuzhiyun 	}
394*4882a593Smuzhiyun 	spin_unlock_irqrestore (&midi->virtual, flags);
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (up)
397*4882a593Smuzhiyun 		snd_wavefront_midi_output_write((snd_wavefront_card_t *)substream->rmidi->card->private_data);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun void
snd_wavefront_midi_interrupt(snd_wavefront_card_t * card)401*4882a593Smuzhiyun snd_wavefront_midi_interrupt (snd_wavefront_card_t *card)
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun 	unsigned long flags;
405*4882a593Smuzhiyun 	snd_wavefront_midi_t *midi;
406*4882a593Smuzhiyun 	static struct snd_rawmidi_substream *substream = NULL;
407*4882a593Smuzhiyun 	static int mpu = external_mpu;
408*4882a593Smuzhiyun 	int max = 128;
409*4882a593Smuzhiyun 	unsigned char byte;
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	midi = &card->wavefront.midi;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	if (!input_avail (midi)) { /* not for us */
414*4882a593Smuzhiyun 		snd_wavefront_midi_output_write(card);
415*4882a593Smuzhiyun 		return;
416*4882a593Smuzhiyun 	}
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	spin_lock_irqsave (&midi->virtual, flags);
419*4882a593Smuzhiyun 	while (--max) {
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 		if (input_avail (midi)) {
422*4882a593Smuzhiyun 			byte = read_data (midi);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 			if (midi->isvirtual) {
425*4882a593Smuzhiyun 				if (byte == WF_EXTERNAL_SWITCH) {
426*4882a593Smuzhiyun 					substream = midi->substream_input[external_mpu];
427*4882a593Smuzhiyun 					mpu = external_mpu;
428*4882a593Smuzhiyun 				} else if (byte == WF_INTERNAL_SWITCH) {
429*4882a593Smuzhiyun 					substream = midi->substream_output[internal_mpu];
430*4882a593Smuzhiyun 					mpu = internal_mpu;
431*4882a593Smuzhiyun 				} /* else just leave it as it is */
432*4882a593Smuzhiyun 			} else {
433*4882a593Smuzhiyun 				substream = midi->substream_input[internal_mpu];
434*4882a593Smuzhiyun 				mpu = internal_mpu;
435*4882a593Smuzhiyun 			}
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 			if (substream == NULL) {
438*4882a593Smuzhiyun 				continue;
439*4882a593Smuzhiyun 			}
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 			if (midi->mode[mpu] & MPU401_MODE_INPUT_TRIGGER) {
442*4882a593Smuzhiyun 				snd_rawmidi_receive(substream, &byte, 1);
443*4882a593Smuzhiyun 			}
444*4882a593Smuzhiyun 		} else {
445*4882a593Smuzhiyun 			break;
446*4882a593Smuzhiyun 		}
447*4882a593Smuzhiyun 	}
448*4882a593Smuzhiyun 	spin_unlock_irqrestore (&midi->virtual, flags);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	snd_wavefront_midi_output_write(card);
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun void
snd_wavefront_midi_enable_virtual(snd_wavefront_card_t * card)454*4882a593Smuzhiyun snd_wavefront_midi_enable_virtual (snd_wavefront_card_t *card)
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	unsigned long flags;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	spin_lock_irqsave (&card->wavefront.midi.virtual, flags);
460*4882a593Smuzhiyun 	card->wavefront.midi.isvirtual = 1;
461*4882a593Smuzhiyun 	card->wavefront.midi.output_mpu = internal_mpu;
462*4882a593Smuzhiyun 	card->wavefront.midi.input_mpu = internal_mpu;
463*4882a593Smuzhiyun 	spin_unlock_irqrestore (&card->wavefront.midi.virtual, flags);
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun void
snd_wavefront_midi_disable_virtual(snd_wavefront_card_t * card)467*4882a593Smuzhiyun snd_wavefront_midi_disable_virtual (snd_wavefront_card_t *card)
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	unsigned long flags;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	spin_lock_irqsave (&card->wavefront.midi.virtual, flags);
473*4882a593Smuzhiyun 	// snd_wavefront_midi_input_close (card->ics2115_external_rmidi);
474*4882a593Smuzhiyun 	// snd_wavefront_midi_output_close (card->ics2115_external_rmidi);
475*4882a593Smuzhiyun 	card->wavefront.midi.isvirtual = 0;
476*4882a593Smuzhiyun 	spin_unlock_irqrestore (&card->wavefront.midi.virtual, flags);
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun int
snd_wavefront_midi_start(snd_wavefront_card_t * card)480*4882a593Smuzhiyun snd_wavefront_midi_start (snd_wavefront_card_t *card)
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	int ok, i;
484*4882a593Smuzhiyun 	unsigned char rbuf[4], wbuf[4];
485*4882a593Smuzhiyun 	snd_wavefront_t *dev;
486*4882a593Smuzhiyun 	snd_wavefront_midi_t *midi;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	dev = &card->wavefront;
489*4882a593Smuzhiyun 	midi = &dev->midi;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	/* The ICS2115 MPU-401 interface doesn't do anything
492*4882a593Smuzhiyun 	   until its set into UART mode.
493*4882a593Smuzhiyun 	*/
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	/* XXX fix me - no hard timing loops allowed! */
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	for (i = 0; i < 30000 && !output_ready (midi); i++);
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	if (!output_ready (midi)) {
500*4882a593Smuzhiyun 		snd_printk ("MIDI interface not ready for command\n");
501*4882a593Smuzhiyun 		return -1;
502*4882a593Smuzhiyun 	}
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	/* Any interrupts received from now on
505*4882a593Smuzhiyun 	   are owned by the MIDI side of things.
506*4882a593Smuzhiyun 	*/
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	dev->interrupts_are_midi = 1;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	outb (UART_MODE_ON, midi->mpu_command_port);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	for (ok = 0, i = 50000; i > 0 && !ok; i--) {
513*4882a593Smuzhiyun 		if (input_avail (midi)) {
514*4882a593Smuzhiyun 			if (read_data (midi) == MPU_ACK) {
515*4882a593Smuzhiyun 				ok = 1;
516*4882a593Smuzhiyun 				break;
517*4882a593Smuzhiyun 			}
518*4882a593Smuzhiyun 		}
519*4882a593Smuzhiyun 	}
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	if (!ok) {
522*4882a593Smuzhiyun 		snd_printk ("cannot set UART mode for MIDI interface");
523*4882a593Smuzhiyun 		dev->interrupts_are_midi = 0;
524*4882a593Smuzhiyun 		return -1;
525*4882a593Smuzhiyun 	}
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	/* Route external MIDI to WaveFront synth (by default) */
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_MISYNTH_ON, rbuf, wbuf)) {
530*4882a593Smuzhiyun 		snd_printk ("can't enable MIDI-IN-2-synth routing.\n");
531*4882a593Smuzhiyun 		/* XXX error ? */
532*4882a593Smuzhiyun 	}
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	/* Turn on Virtual MIDI, but first *always* turn it off,
535*4882a593Smuzhiyun 	   since otherwise consecutive reloads of the driver will
536*4882a593Smuzhiyun 	   never cause the hardware to generate the initial "internal" or
537*4882a593Smuzhiyun 	   "external" source bytes in the MIDI data stream. This
538*4882a593Smuzhiyun 	   is pretty important, since the internal hardware generally will
539*4882a593Smuzhiyun 	   be used to generate none or very little MIDI output, and
540*4882a593Smuzhiyun 	   thus the only source of MIDI data is actually external. Without
541*4882a593Smuzhiyun 	   the switch bytes, the driver will think it all comes from
542*4882a593Smuzhiyun 	   the internal interface. Duh.
543*4882a593Smuzhiyun 	*/
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_VMIDI_OFF, rbuf, wbuf)) {
546*4882a593Smuzhiyun 		snd_printk ("virtual MIDI mode not disabled\n");
547*4882a593Smuzhiyun 		return 0; /* We're OK, but missing the external MIDI dev */
548*4882a593Smuzhiyun 	}
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	snd_wavefront_midi_enable_virtual (card);
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_VMIDI_ON, rbuf, wbuf)) {
553*4882a593Smuzhiyun 		snd_printk ("cannot enable virtual MIDI mode.\n");
554*4882a593Smuzhiyun 		snd_wavefront_midi_disable_virtual (card);
555*4882a593Smuzhiyun 	}
556*4882a593Smuzhiyun 	return 0;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun const struct snd_rawmidi_ops snd_wavefront_midi_output =
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun 	.open =		snd_wavefront_midi_output_open,
562*4882a593Smuzhiyun 	.close =	snd_wavefront_midi_output_close,
563*4882a593Smuzhiyun 	.trigger =	snd_wavefront_midi_output_trigger,
564*4882a593Smuzhiyun };
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun const struct snd_rawmidi_ops snd_wavefront_midi_input =
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun 	.open =		snd_wavefront_midi_input_open,
569*4882a593Smuzhiyun 	.close =	snd_wavefront_midi_input_close,
570*4882a593Smuzhiyun 	.trigger =	snd_wavefront_midi_input_trigger,
571*4882a593Smuzhiyun };
572*4882a593Smuzhiyun 
573