2 * Copyright 10/16/2005 Tilman Kranz <tilde@tk-sls.de>
3 * Creative Audio MIDI, for the CA0106 Driver
7 * Implementation is based on mpu401 and emu10k1x and
9 * mpu401: Copyright (c) by Jaroslav Kysela <perex@suse.cz>
10 * emu10k1x: Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <linux/spinlock.h>
30 #include <sound/driver.h>
31 #include <sound/core.h>
32 #include <sound/rawmidi.h>
36 #define ca_midi_write_data(midi, data) midi->write(midi, data, 0)
37 #define ca_midi_write_cmd(midi, data) midi->write(midi, data, 1)
38 #define ca_midi_read_data(midi) midi->read(midi, 0)
39 #define ca_midi_read_stat(midi) midi->read(midi, 1)
40 #define ca_midi_input_avail(midi) (!(ca_midi_read_stat(midi) & midi->input_avail))
41 #define ca_midi_output_ready(midi) (!(ca_midi_read_stat(midi) & midi->output_ready))
43 static void ca_midi_clear_rx(ca_midi_t *midi)
46 for (; timeout > 0 && ca_midi_input_avail(midi); timeout--)
47 ca_midi_read_data(midi);
48 #ifdef CONFIG_SND_DEBUG
50 snd_printk(KERN_ERR "ca_midi_clear_rx: timeout (status = 0x%x)\n", ca_midi_read_stat(midi));
54 static void ca_midi_interrupt(ca_midi_t *midi, unsigned int status) {
57 if (midi->rmidi == NULL) {
58 midi->interrupt_disable(midi,midi->tx_enable | midi->rx_enable);
62 spin_lock(&midi->input_lock);
63 if ((status & midi->ipr_rx) && ca_midi_input_avail(midi)) {
64 if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
65 ca_midi_clear_rx(midi);
67 byte = ca_midi_read_data(midi);
68 if(midi->substream_input)
69 snd_rawmidi_receive(midi->substream_input, &byte, 1);
74 spin_unlock(&midi->input_lock);
76 spin_lock(&midi->output_lock);
77 if ((status & midi->ipr_tx) && ca_midi_output_ready(midi)) {
78 if (midi->substream_output &&
79 snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
80 ca_midi_write_data(midi, byte);
82 midi->interrupt_disable(midi,midi->tx_enable);
85 spin_unlock(&midi->output_lock);
89 static void ca_midi_cmd(ca_midi_t *midi, unsigned char cmd, int ack)
94 spin_lock_irqsave(&midi->input_lock, flags);
95 ca_midi_write_data(midi, 0x00);
96 /* ca_midi_clear_rx(midi); */
98 ca_midi_write_cmd(midi, cmd);
102 while (!ok && timeout-- > 0) {
103 if (ca_midi_input_avail(midi)) {
104 if (ca_midi_read_data(midi) == midi->ack)
108 if (!ok && ca_midi_read_data(midi) == midi->ack)
113 spin_unlock_irqrestore(&midi->input_lock, flags);
115 snd_printk(KERN_ERR "ca_midi_cmd: 0x%x failed at 0x%x (status = 0x%x, data = 0x%x)!!!\n",
117 midi->get_dev_id_port(midi->dev_id),
118 ca_midi_read_stat(midi),
119 ca_midi_read_data(midi));
122 static int ca_midi_input_open(snd_rawmidi_substream_t * substream)
124 ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
127 snd_assert(midi->dev_id, return -ENXIO);
128 spin_lock_irqsave(&midi->open_lock, flags);
129 midi->midi_mode |= CA_MIDI_MODE_INPUT;
130 midi->substream_input = substream;
131 if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
132 spin_unlock_irqrestore(&midi->open_lock, flags);
133 ca_midi_cmd(midi, midi->reset, 1);
134 ca_midi_cmd(midi, midi->enter_uart, 1);
136 spin_unlock_irqrestore(&midi->open_lock, flags);
141 static int ca_midi_output_open(snd_rawmidi_substream_t * substream)
143 ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
146 snd_assert(midi->dev_id, return -ENXIO);
147 spin_lock_irqsave(&midi->open_lock, flags);
148 midi->midi_mode |= CA_MIDI_MODE_OUTPUT;
149 midi->substream_output = substream;
150 if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
151 spin_unlock_irqrestore(&midi->open_lock, flags);
152 ca_midi_cmd(midi, midi->reset, 1);
153 ca_midi_cmd(midi, midi->enter_uart, 1);
155 spin_unlock_irqrestore(&midi->open_lock, flags);
160 static int ca_midi_input_close(snd_rawmidi_substream_t * substream)
162 ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
165 snd_assert(midi->dev_id, return -ENXIO);
166 spin_lock_irqsave(&midi->open_lock, flags);
167 midi->interrupt_disable(midi,midi->rx_enable);
168 midi->midi_mode &= ~CA_MIDI_MODE_INPUT;
169 midi->substream_input = NULL;
170 if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
171 spin_unlock_irqrestore(&midi->open_lock, flags);
172 ca_midi_cmd(midi, midi->reset, 0);
174 spin_unlock_irqrestore(&midi->open_lock, flags);
179 static int ca_midi_output_close(snd_rawmidi_substream_t * substream)
181 ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
183 snd_assert(midi->dev_id, return -ENXIO);
185 spin_lock_irqsave(&midi->open_lock, flags);
187 midi->interrupt_disable(midi,midi->tx_enable);
188 midi->midi_mode &= ~CA_MIDI_MODE_OUTPUT;
189 midi->substream_output = NULL;
191 if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
192 spin_unlock_irqrestore(&midi->open_lock, flags);
193 ca_midi_cmd(midi, midi->reset, 0);
195 spin_unlock_irqrestore(&midi->open_lock, flags);
200 static void ca_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
202 ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
203 snd_assert(midi->dev_id, return);
206 midi->interrupt_enable(midi,midi->rx_enable);
208 midi->interrupt_disable(midi, midi->rx_enable);
212 static void ca_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
214 ca_midi_t *midi = (ca_midi_t *)substream->rmidi->private_data;
217 snd_assert(midi->dev_id, return);
223 spin_lock_irqsave(&midi->output_lock, flags);
225 /* try to send some amount of bytes here before interrupts */
227 if (ca_midi_output_ready(midi)) {
228 if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT) ||
229 snd_rawmidi_transmit(substream, &byte, 1) != 1) {
231 spin_unlock_irqrestore(&midi->output_lock, flags);
234 ca_midi_write_data(midi, byte);
241 spin_unlock_irqrestore(&midi->output_lock, flags);
242 midi->interrupt_enable(midi,midi->tx_enable);
245 midi->interrupt_disable(midi,midi->tx_enable);
249 static snd_rawmidi_ops_t ca_midi_output =
251 .open = ca_midi_output_open,
252 .close = ca_midi_output_close,
253 .trigger = ca_midi_output_trigger,
256 static snd_rawmidi_ops_t ca_midi_input =
258 .open = ca_midi_input_open,
259 .close = ca_midi_input_close,
260 .trigger = ca_midi_input_trigger,
263 static void ca_midi_free(ca_midi_t *midi) {
264 midi->interrupt = NULL;
265 midi->interrupt_enable = NULL;
266 midi->interrupt_disable = NULL;
269 midi->get_dev_id_card = NULL;
270 midi->get_dev_id_port = NULL;
274 static void ca_rmidi_free(snd_rawmidi_t *rmidi)
276 ca_midi_free((ca_midi_t *)rmidi->private_data);
279 int __devinit ca_midi_init(void *dev_id, ca_midi_t *midi, int device, char *name)
281 snd_rawmidi_t *rmidi;
284 if ((err = snd_rawmidi_new(midi->get_dev_id_card(midi->dev_id), name, device, 1, 1, &rmidi)) < 0)
287 midi->dev_id = dev_id;
288 midi->interrupt = ca_midi_interrupt;
290 spin_lock_init(&midi->open_lock);
291 spin_lock_init(&midi->input_lock);
292 spin_lock_init(&midi->output_lock);
294 strcpy(rmidi->name, name);
295 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &ca_midi_output);
296 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &ca_midi_input);
297 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
298 SNDRV_RAWMIDI_INFO_INPUT |
299 SNDRV_RAWMIDI_INFO_DUPLEX;
300 rmidi->private_data = midi;
301 rmidi->private_free = ca_rmidi_free;