]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/pci/ca0106/ca_midi.c
[ALSA] snd-ca0106: Add midi support.
[linux-2.6-omap-h63xx.git] / sound / pci / ca0106 / ca_midi.c
1 /* 
2  *  Copyright 10/16/2005 Tilman Kranz <tilde@tk-sls.de>
3  *  Creative Audio MIDI, for the CA0106 Driver
4  *  Version: 0.0.1
5  *
6  *  Changelog:
7  *    Implementation is based on mpu401 and emu10k1x and
8  *    tested with ca0106.
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  *
24  *
25  */
26
27 #include <linux/spinlock.h>
28 #include <sound/driver.h>
29 #include <sound/core.h>
30 #include <sound/rawmidi.h>
31
32 #include "ca_midi.h"
33
34 #define ca_midi_write_data(midi, data)  midi->write(midi, data, 0)
35 #define ca_midi_write_cmd(midi, data)   midi->write(midi, data, 1)
36 #define ca_midi_read_data(midi)         midi->read(midi, 0)
37 #define ca_midi_read_stat(midi)         midi->read(midi, 1)
38 #define ca_midi_input_avail(midi)       (!(ca_midi_read_stat(midi) & midi->input_avail))
39 #define ca_midi_output_ready(midi)      (!(ca_midi_read_stat(midi) & midi->output_ready))
40
41 static void ca_midi_clear_rx(ca_midi_t *midi)
42 {
43         int timeout = 100000;
44         for (; timeout > 0 && ca_midi_input_avail(midi); timeout--)
45                 ca_midi_read_data(midi);
46 #ifdef CONFIG_SND_DEBUG
47         if (timeout <= 0)
48                 snd_printk(KERN_ERR "ca_midi_clear_rx: timeout (status = 0x%x)\n", ca_midi_read_stat(midi));
49 #endif
50 }
51
52 void ca_midi_interrupt(ca_midi_t *midi, unsigned int status) {
53         unsigned char byte;
54
55         if (midi->rmidi == NULL) {
56                 midi->interrupt_disable(midi,midi->tx_enable | midi->rx_enable);
57                 return;
58         }
59
60         spin_lock(&midi->input_lock);
61         if ((status & midi->ipr_rx) && ca_midi_input_avail(midi)) {
62                 if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
63                         ca_midi_clear_rx(midi);
64                 } else {
65                         byte = ca_midi_read_data(midi);
66                         if(midi->substream_input)
67                                 snd_rawmidi_receive(midi->substream_input, &byte, 1);
68
69
70                 }
71         }
72         spin_unlock(&midi->input_lock);
73
74         spin_lock(&midi->output_lock);
75         if ((status & midi->ipr_tx) && ca_midi_output_ready(midi)) {
76                 if (midi->substream_output &&
77                     snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
78                         ca_midi_write_data(midi, byte);
79                 } else {
80                         midi->interrupt_disable(midi,midi->tx_enable);
81                 }
82         }
83         spin_unlock(&midi->output_lock);
84
85 }
86
87 static void ca_midi_cmd(ca_midi_t *midi, unsigned char cmd, int ack)
88 {
89         unsigned long flags;
90         int timeout, ok;
91
92         spin_lock_irqsave(&midi->input_lock, flags);
93         ca_midi_write_data(midi, 0x00);
94         /* ca_midi_clear_rx(midi); */
95
96         ca_midi_write_cmd(midi, cmd);
97         if (ack) {
98                 ok = 0;
99                 timeout = 10000;
100                 while (!ok && timeout-- > 0) {
101                         if (ca_midi_input_avail(midi)) {
102                                 if (ca_midi_read_data(midi) == midi->ack)
103                                         ok = 1;
104                         }
105                 }
106                 if (!ok && ca_midi_read_data(midi) == midi->ack)
107                         ok = 1;
108         } else {
109                 ok = 1;
110         }
111         spin_unlock_irqrestore(&midi->input_lock, flags);
112         if (!ok)
113                 snd_printk(KERN_ERR "ca_midi_cmd: 0x%x failed at 0x%x (status = 0x%x, data = 0x%x)!!!\n",
114                            cmd,
115                            midi->get_dev_id_port(midi->dev_id),
116                            ca_midi_read_stat(midi),
117                            ca_midi_read_data(midi));
118 }
119
120 static int ca_midi_input_open(snd_rawmidi_substream_t * substream)
121 {
122         ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
123         unsigned long flags;
124         
125         snd_assert(midi->dev_id, return -ENXIO);
126         spin_lock_irqsave(&midi->open_lock, flags);
127         midi->midi_mode |= CA_MIDI_MODE_INPUT;
128         midi->substream_input = substream;
129         if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
130                 spin_unlock_irqrestore(&midi->open_lock, flags);
131                 ca_midi_cmd(midi, midi->reset, 1);
132                 ca_midi_cmd(midi, midi->enter_uart, 1);
133         } else {
134                 spin_unlock_irqrestore(&midi->open_lock, flags);
135         }
136         return 0;
137 }
138
139 static int ca_midi_output_open(snd_rawmidi_substream_t * substream)
140 {
141         ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
142         unsigned long flags;
143
144         snd_assert(midi->dev_id, return -ENXIO);
145         spin_lock_irqsave(&midi->open_lock, flags);
146         midi->midi_mode |= CA_MIDI_MODE_OUTPUT;
147         midi->substream_output = substream;
148         if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
149                 spin_unlock_irqrestore(&midi->open_lock, flags);
150                 ca_midi_cmd(midi, midi->reset, 1);
151                 ca_midi_cmd(midi, midi->enter_uart, 1);
152         } else {
153                 spin_unlock_irqrestore(&midi->open_lock, flags);
154         }
155         return 0;
156 }
157
158 static int ca_midi_input_close(snd_rawmidi_substream_t * substream)
159 {
160         ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
161         unsigned long flags;
162
163         snd_assert(midi->dev_id, return -ENXIO);
164         spin_lock_irqsave(&midi->open_lock, flags);
165         midi->interrupt_disable(midi,midi->rx_enable);
166         midi->midi_mode &= ~CA_MIDI_MODE_INPUT;
167         midi->substream_input = NULL;
168         if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
169                 spin_unlock_irqrestore(&midi->open_lock, flags);
170                 ca_midi_cmd(midi, midi->reset, 0);
171         } else {
172                 spin_unlock_irqrestore(&midi->open_lock, flags);
173         }
174         return 0;
175 }
176
177 static int ca_midi_output_close(snd_rawmidi_substream_t * substream)
178 {
179         ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
180         unsigned long flags;
181         snd_assert(midi->dev_id, return -ENXIO);
182         
183         spin_lock_irqsave(&midi->open_lock, flags);
184
185         midi->interrupt_disable(midi,midi->tx_enable);
186         midi->midi_mode &= ~CA_MIDI_MODE_OUTPUT;
187         midi->substream_output = NULL;
188         
189         if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
190                 spin_unlock_irqrestore(&midi->open_lock, flags);
191                 ca_midi_cmd(midi, midi->reset, 0);
192         } else {
193                 spin_unlock_irqrestore(&midi->open_lock, flags);
194         }
195         return 0;
196 }
197
198 static void ca_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
199 {
200         ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
201         snd_assert(midi->dev_id, return);
202
203         if (up) {
204                 midi->interrupt_enable(midi,midi->rx_enable);
205         } else {
206                 midi->interrupt_disable(midi, midi->rx_enable);
207         }
208 }
209
210 static void ca_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
211 {
212         ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
213         unsigned long flags;
214
215         snd_assert(midi->dev_id, return);
216
217         if (up) {
218                 int max = 4;
219                 unsigned char byte;
220
221                 spin_lock_irqsave(&midi->output_lock, flags);
222         
223                 /* try to send some amount of bytes here before interrupts */
224                 while (max > 0) {
225                         if (ca_midi_output_ready(midi)) {
226                                 if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT) ||
227                                     snd_rawmidi_transmit(substream, &byte, 1) != 1) {
228                                         /* no more data */
229                                         spin_unlock_irqrestore(&midi->output_lock, flags);
230                                         return;
231                                 }
232                                 ca_midi_write_data(midi, byte);
233                                 max--;
234                         } else {
235                                 break;
236                         }
237                 }
238
239                 spin_unlock_irqrestore(&midi->output_lock, flags);
240                 midi->interrupt_enable(midi,midi->tx_enable);
241
242         } else {
243                 midi->interrupt_disable(midi,midi->tx_enable);
244         }
245 }
246
247 static snd_rawmidi_ops_t ca_midi_output =
248 {
249         .open =         ca_midi_output_open,
250         .close =        ca_midi_output_close,
251         .trigger =      ca_midi_output_trigger,
252 };
253
254 static snd_rawmidi_ops_t ca_midi_input =
255 {
256         .open =         ca_midi_input_open,
257         .close =        ca_midi_input_close,
258         .trigger =      ca_midi_input_trigger,
259 };
260
261 void ca_midi_free(ca_midi_t *midi) {
262         midi->interrupt = NULL;
263         midi->interrupt_enable = NULL;
264         midi->interrupt_disable = NULL;
265         midi->read = NULL;
266         midi->write = NULL;
267         midi->get_dev_id_card = NULL;
268         midi->get_dev_id_port = NULL;
269         midi->rmidi = NULL;
270 }
271
272 static void ca_rmidi_free(snd_rawmidi_t *rmidi)
273 {
274         ca_midi_free((ca_midi_t *)rmidi->private_data);
275 }
276
277 int __devinit ca_midi_init(void *dev_id, ca_midi_t *midi, int device, char *name)
278 {
279         snd_rawmidi_t *rmidi;
280         int err;
281
282         if ((err = snd_rawmidi_new(midi->get_dev_id_card(midi->dev_id), name, device, 1, 1, &rmidi)) < 0)
283                 return err;
284
285         midi->dev_id = dev_id;
286         midi->interrupt = ca_midi_interrupt;
287
288         spin_lock_init(&midi->open_lock);
289         spin_lock_init(&midi->input_lock);
290         spin_lock_init(&midi->output_lock);
291
292         strcpy(rmidi->name, name);
293         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &ca_midi_output);
294         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &ca_midi_input);
295         rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
296                              SNDRV_RAWMIDI_INFO_INPUT |
297                              SNDRV_RAWMIDI_INFO_DUPLEX;
298         rmidi->private_data = midi;
299         rmidi->private_free = ca_rmidi_free;
300         
301         midi->rmidi = rmidi;
302         return 0;
303 }
304