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@perex.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/core.h>
31 #include <sound/rawmidi.h>
35 #define ca_midi_write_data(midi, data) midi->write(midi, data, 0)
36 #define ca_midi_write_cmd(midi, data) midi->write(midi, data, 1)
37 #define ca_midi_read_data(midi) midi->read(midi, 0)
38 #define ca_midi_read_stat(midi) midi->read(midi, 1)
39 #define ca_midi_input_avail(midi) (!(ca_midi_read_stat(midi) & midi->input_avail))
40 #define ca_midi_output_ready(midi) (!(ca_midi_read_stat(midi) & midi->output_ready))
42 static void ca_midi_clear_rx(struct snd_ca_midi
*midi
)
45 for (; timeout
> 0 && ca_midi_input_avail(midi
); timeout
--)
46 ca_midi_read_data(midi
);
47 #ifdef CONFIG_SND_DEBUG
49 snd_printk(KERN_ERR
"ca_midi_clear_rx: timeout (status = 0x%x)\n",
50 ca_midi_read_stat(midi
));
54 static void ca_midi_interrupt(struct snd_ca_midi
*midi
, unsigned int status
)
58 if (midi
->rmidi
== NULL
) {
59 midi
->interrupt_disable(midi
,midi
->tx_enable
| midi
->rx_enable
);
63 spin_lock(&midi
->input_lock
);
64 if ((status
& midi
->ipr_rx
) && ca_midi_input_avail(midi
)) {
65 if (!(midi
->midi_mode
& CA_MIDI_MODE_INPUT
)) {
66 ca_midi_clear_rx(midi
);
68 byte
= ca_midi_read_data(midi
);
69 if(midi
->substream_input
)
70 snd_rawmidi_receive(midi
->substream_input
, &byte
, 1);
75 spin_unlock(&midi
->input_lock
);
77 spin_lock(&midi
->output_lock
);
78 if ((status
& midi
->ipr_tx
) && ca_midi_output_ready(midi
)) {
79 if (midi
->substream_output
&&
80 snd_rawmidi_transmit(midi
->substream_output
, &byte
, 1) == 1) {
81 ca_midi_write_data(midi
, byte
);
83 midi
->interrupt_disable(midi
,midi
->tx_enable
);
86 spin_unlock(&midi
->output_lock
);
90 static void ca_midi_cmd(struct snd_ca_midi
*midi
, unsigned char cmd
, int ack
)
95 spin_lock_irqsave(&midi
->input_lock
, flags
);
96 ca_midi_write_data(midi
, 0x00);
97 /* ca_midi_clear_rx(midi); */
99 ca_midi_write_cmd(midi
, cmd
);
103 while (!ok
&& timeout
-- > 0) {
104 if (ca_midi_input_avail(midi
)) {
105 if (ca_midi_read_data(midi
) == midi
->ack
)
109 if (!ok
&& ca_midi_read_data(midi
) == midi
->ack
)
114 spin_unlock_irqrestore(&midi
->input_lock
, flags
);
116 snd_printk(KERN_ERR
"ca_midi_cmd: 0x%x failed at 0x%x (status = 0x%x, data = 0x%x)!!!\n",
118 midi
->get_dev_id_port(midi
->dev_id
),
119 ca_midi_read_stat(midi
),
120 ca_midi_read_data(midi
));
123 static int ca_midi_input_open(struct snd_rawmidi_substream
*substream
)
125 struct snd_ca_midi
*midi
= substream
->rmidi
->private_data
;
128 if (snd_BUG_ON(!midi
->dev_id
))
130 spin_lock_irqsave(&midi
->open_lock
, flags
);
131 midi
->midi_mode
|= CA_MIDI_MODE_INPUT
;
132 midi
->substream_input
= substream
;
133 if (!(midi
->midi_mode
& CA_MIDI_MODE_OUTPUT
)) {
134 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
135 ca_midi_cmd(midi
, midi
->reset
, 1);
136 ca_midi_cmd(midi
, midi
->enter_uart
, 1);
138 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
143 static int ca_midi_output_open(struct snd_rawmidi_substream
*substream
)
145 struct snd_ca_midi
*midi
= substream
->rmidi
->private_data
;
148 if (snd_BUG_ON(!midi
->dev_id
))
150 spin_lock_irqsave(&midi
->open_lock
, flags
);
151 midi
->midi_mode
|= CA_MIDI_MODE_OUTPUT
;
152 midi
->substream_output
= substream
;
153 if (!(midi
->midi_mode
& CA_MIDI_MODE_INPUT
)) {
154 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
155 ca_midi_cmd(midi
, midi
->reset
, 1);
156 ca_midi_cmd(midi
, midi
->enter_uart
, 1);
158 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
163 static int ca_midi_input_close(struct snd_rawmidi_substream
*substream
)
165 struct snd_ca_midi
*midi
= substream
->rmidi
->private_data
;
168 if (snd_BUG_ON(!midi
->dev_id
))
170 spin_lock_irqsave(&midi
->open_lock
, flags
);
171 midi
->interrupt_disable(midi
,midi
->rx_enable
);
172 midi
->midi_mode
&= ~CA_MIDI_MODE_INPUT
;
173 midi
->substream_input
= NULL
;
174 if (!(midi
->midi_mode
& CA_MIDI_MODE_OUTPUT
)) {
175 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
176 ca_midi_cmd(midi
, midi
->reset
, 0);
178 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
183 static int ca_midi_output_close(struct snd_rawmidi_substream
*substream
)
185 struct snd_ca_midi
*midi
= substream
->rmidi
->private_data
;
188 if (snd_BUG_ON(!midi
->dev_id
))
191 spin_lock_irqsave(&midi
->open_lock
, flags
);
193 midi
->interrupt_disable(midi
,midi
->tx_enable
);
194 midi
->midi_mode
&= ~CA_MIDI_MODE_OUTPUT
;
195 midi
->substream_output
= NULL
;
197 if (!(midi
->midi_mode
& CA_MIDI_MODE_INPUT
)) {
198 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
199 ca_midi_cmd(midi
, midi
->reset
, 0);
201 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
206 static void ca_midi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
208 struct snd_ca_midi
*midi
= substream
->rmidi
->private_data
;
210 if (snd_BUG_ON(!midi
->dev_id
))
214 midi
->interrupt_enable(midi
,midi
->rx_enable
);
216 midi
->interrupt_disable(midi
, midi
->rx_enable
);
220 static void ca_midi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
222 struct snd_ca_midi
*midi
= substream
->rmidi
->private_data
;
225 if (snd_BUG_ON(!midi
->dev_id
))
232 spin_lock_irqsave(&midi
->output_lock
, flags
);
234 /* try to send some amount of bytes here before interrupts */
236 if (ca_midi_output_ready(midi
)) {
237 if (!(midi
->midi_mode
& CA_MIDI_MODE_OUTPUT
) ||
238 snd_rawmidi_transmit(substream
, &byte
, 1) != 1) {
240 spin_unlock_irqrestore(&midi
->output_lock
, flags
);
243 ca_midi_write_data(midi
, byte
);
250 spin_unlock_irqrestore(&midi
->output_lock
, flags
);
251 midi
->interrupt_enable(midi
,midi
->tx_enable
);
254 midi
->interrupt_disable(midi
,midi
->tx_enable
);
258 static struct snd_rawmidi_ops ca_midi_output
=
260 .open
= ca_midi_output_open
,
261 .close
= ca_midi_output_close
,
262 .trigger
= ca_midi_output_trigger
,
265 static struct snd_rawmidi_ops ca_midi_input
=
267 .open
= ca_midi_input_open
,
268 .close
= ca_midi_input_close
,
269 .trigger
= ca_midi_input_trigger
,
272 static void ca_midi_free(struct snd_ca_midi
*midi
)
274 midi
->interrupt
= NULL
;
275 midi
->interrupt_enable
= NULL
;
276 midi
->interrupt_disable
= NULL
;
279 midi
->get_dev_id_card
= NULL
;
280 midi
->get_dev_id_port
= NULL
;
284 static void ca_rmidi_free(struct snd_rawmidi
*rmidi
)
286 ca_midi_free(rmidi
->private_data
);
289 int __devinit
ca_midi_init(void *dev_id
, struct snd_ca_midi
*midi
, int device
, char *name
)
291 struct snd_rawmidi
*rmidi
;
294 if ((err
= snd_rawmidi_new(midi
->get_dev_id_card(midi
->dev_id
), name
, device
, 1, 1, &rmidi
)) < 0)
297 midi
->dev_id
= dev_id
;
298 midi
->interrupt
= ca_midi_interrupt
;
300 spin_lock_init(&midi
->open_lock
);
301 spin_lock_init(&midi
->input_lock
);
302 spin_lock_init(&midi
->output_lock
);
304 strcpy(rmidi
->name
, name
);
305 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &ca_midi_output
);
306 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &ca_midi_input
);
307 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_OUTPUT
|
308 SNDRV_RAWMIDI_INFO_INPUT
|
309 SNDRV_RAWMIDI_INFO_DUPLEX
;
310 rmidi
->private_data
= midi
;
311 rmidi
->private_free
= ca_rmidi_free
;