ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / sb / sb8_midi.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Routines for control of SoundBlaster cards - MIDI interface
4  *
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.
9  *
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.
14  *
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
18  *
19  * --
20  *
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 
23  *   working.
24  *
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.
27  */
28
29 #include <sound/driver.h>
30 #include <asm/io.h>
31 #include <linux/time.h>
32 #include <sound/core.h>
33 #include <sound/sb.h>
34
35 /*
36
37  */
38
39 irqreturn_t snd_sb8dsp_midi_interrupt(sb_t * chip)
40 {
41         snd_rawmidi_t *rmidi;
42         int max = 64;
43         char byte;
44
45         if (chip == NULL || (rmidi = chip->rmidi) == NULL) {
46                 inb(SBP(chip, DATA_AVAIL));     /* ack interrupt */
47                 return IRQ_NONE;
48         }
49         while (max-- > 0) {
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);
56                         } else {
57                                 spin_unlock(&chip->midi_input_lock);
58                         }
59                 } else {
60                         spin_unlock(&chip->midi_input_lock);
61                 }
62         }
63         return IRQ_HANDLED;
64 }
65
66 /*
67
68  */
69
70 static int snd_sb8dsp_midi_input_open(snd_rawmidi_substream_t * substream)
71 {
72         unsigned long flags;
73         sb_t *chip;
74         unsigned int valid_open_flags;
75
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);
82                 return -EAGAIN;
83         }
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);
91         } else {
92                 spin_unlock_irqrestore(&chip->open_lock, flags);
93         }
94         return 0;
95 }
96
97 static int snd_sb8dsp_midi_output_open(snd_rawmidi_substream_t * substream)
98 {
99         unsigned long flags;
100         sb_t *chip;
101         unsigned int valid_open_flags;
102
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);
109                 return -EAGAIN;
110         }
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);
118         } else {
119                 spin_unlock_irqrestore(&chip->open_lock, flags);
120         }
121         return 0;
122 }
123
124 static int snd_sb8dsp_midi_input_close(snd_rawmidi_substream_t * substream)
125 {
126         unsigned long flags;
127         sb_t *chip;
128
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 */
136         } else {
137                 spin_unlock_irqrestore(&chip->open_lock, flags);
138         }
139         return 0;
140 }
141
142 static int snd_sb8dsp_midi_output_close(snd_rawmidi_substream_t * substream)
143 {
144         unsigned long flags;
145         sb_t *chip;
146
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 */
154         } else {
155                 spin_unlock_irqrestore(&chip->open_lock, flags);
156         }
157         return 0;
158 }
159
160 static void snd_sb8dsp_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
161 {
162         unsigned long flags;
163         sb_t *chip;
164
165         chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return);
166         spin_lock_irqsave(&chip->open_lock, flags);
167         if (up) {
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;
172                 }
173         } else {
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;
178                 }
179         }
180         spin_unlock_irqrestore(&chip->open_lock, flags);
181 }
182
183 static void snd_sb8dsp_midi_output_write(snd_rawmidi_substream_t * substream)
184 {
185         unsigned long flags;
186         sb_t *chip;
187         char byte;
188         int max = 32;
189
190         /* how big is Tx FIFO? */
191         chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return);
192         while (max-- > 0) {
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);
198                         break;
199                 }
200                 if (chip->hardware >= SB_HW_20) {
201                         int timeout = 8;
202                         while ((inb(SBP(chip, STATUS)) & 0x80) != 0 && --timeout > 0)
203                                 ;
204                         if (timeout == 0) {
205                                 /* Tx FIFO full - try again later */
206                                 spin_unlock_irqrestore(&chip->open_lock, flags);
207                                 break;
208                         }
209                         outb(byte, SBP(chip, WRITE));
210                 } else {
211                         snd_sbdsp_command(chip, SB_DSP_MIDI_OUTPUT);
212                         snd_sbdsp_command(chip, byte);
213                 }
214                 snd_rawmidi_transmit_ack(substream, 1);
215                 spin_unlock_irqrestore(&chip->open_lock, flags);
216         }
217 }
218
219 static void snd_sb8dsp_midi_output_timer(unsigned long data)
220 {
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);
223         unsigned long flags;
224
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);
230 }
231
232 static void snd_sb8dsp_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
233 {
234         unsigned long flags;
235         sb_t *chip;
236
237         chip = snd_magic_cast(sb_t, substream->rmidi->private_data, return);
238         spin_lock_irqsave(&chip->open_lock, flags);
239         if (up) {
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;
247                 }
248         } else {
249                 if (chip->open & SB_OPEN_MIDI_OUTPUT_TRIGGER) {
250                         chip->open &= ~SB_OPEN_MIDI_OUTPUT_TRIGGER;
251                 }
252         }
253         spin_unlock_irqrestore(&chip->open_lock, flags);
254
255         if (up)
256                 snd_sb8dsp_midi_output_write(substream);
257 }
258
259 /*
260
261  */
262
263 static snd_rawmidi_ops_t snd_sb8dsp_midi_output =
264 {
265         .open =         snd_sb8dsp_midi_output_open,
266         .close =        snd_sb8dsp_midi_output_close,
267         .trigger =      snd_sb8dsp_midi_output_trigger,
268 };
269
270 static snd_rawmidi_ops_t snd_sb8dsp_midi_input =
271 {
272         .open =         snd_sb8dsp_midi_input_open,
273         .close =        snd_sb8dsp_midi_input_close,
274         .trigger =      snd_sb8dsp_midi_input_trigger,
275 };
276
277 int snd_sb8dsp_midi(sb_t *chip, int device, snd_rawmidi_t ** rrawmidi)
278 {
279         snd_rawmidi_t *rmidi;
280         int err;
281
282         if (rrawmidi)
283                 *rrawmidi = NULL;
284         if ((err = snd_rawmidi_new(chip->card, "SB8 MIDI", device, 1, 1, &rmidi)) < 0)
285                 return err;
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;
293         chip->rmidi = rmidi;
294         if (rrawmidi)
295                 *rrawmidi = rmidi;
296         return 0;
297 }