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
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.
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.
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
27 #include <linux/spinlock.h>
28 #include <sound/driver.h>
29 #include <sound/core.h>
30 #include <sound/rawmidi.h>
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))
41 static void ca_midi_clear_rx(ca_midi_t
*midi
)
44 for (; timeout
> 0 && ca_midi_input_avail(midi
); timeout
--)
45 ca_midi_read_data(midi
);
46 #ifdef CONFIG_SND_DEBUG
48 snd_printk(KERN_ERR
"ca_midi_clear_rx: timeout (status = 0x%x)\n", ca_midi_read_stat(midi
));
52 static void ca_midi_interrupt(ca_midi_t
*midi
, unsigned int status
) {
55 if (midi
->rmidi
== NULL
) {
56 midi
->interrupt_disable(midi
,midi
->tx_enable
| midi
->rx_enable
);
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
);
65 byte
= ca_midi_read_data(midi
);
66 if(midi
->substream_input
)
67 snd_rawmidi_receive(midi
->substream_input
, &byte
, 1);
72 spin_unlock(&midi
->input_lock
);
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
);
80 midi
->interrupt_disable(midi
,midi
->tx_enable
);
83 spin_unlock(&midi
->output_lock
);
87 static void ca_midi_cmd(ca_midi_t
*midi
, unsigned char cmd
, int ack
)
92 spin_lock_irqsave(&midi
->input_lock
, flags
);
93 ca_midi_write_data(midi
, 0x00);
94 /* ca_midi_clear_rx(midi); */
96 ca_midi_write_cmd(midi
, cmd
);
100 while (!ok
&& timeout
-- > 0) {
101 if (ca_midi_input_avail(midi
)) {
102 if (ca_midi_read_data(midi
) == midi
->ack
)
106 if (!ok
&& ca_midi_read_data(midi
) == midi
->ack
)
111 spin_unlock_irqrestore(&midi
->input_lock
, flags
);
113 snd_printk(KERN_ERR
"ca_midi_cmd: 0x%x failed at 0x%x (status = 0x%x, data = 0x%x)!!!\n",
115 midi
->get_dev_id_port(midi
->dev_id
),
116 ca_midi_read_stat(midi
),
117 ca_midi_read_data(midi
));
120 static int ca_midi_input_open(snd_rawmidi_substream_t
* substream
)
122 ca_midi_t
*midi
= (ca_midi_t
*)substream
->rmidi
->private_data
;
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);
134 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
139 static int ca_midi_output_open(snd_rawmidi_substream_t
* substream
)
141 ca_midi_t
*midi
= (ca_midi_t
*)substream
->rmidi
->private_data
;
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);
153 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
158 static int ca_midi_input_close(snd_rawmidi_substream_t
* substream
)
160 ca_midi_t
*midi
= (ca_midi_t
*)substream
->rmidi
->private_data
;
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);
172 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
177 static int ca_midi_output_close(snd_rawmidi_substream_t
* substream
)
179 ca_midi_t
*midi
= (ca_midi_t
*)substream
->rmidi
->private_data
;
181 snd_assert(midi
->dev_id
, return -ENXIO
);
183 spin_lock_irqsave(&midi
->open_lock
, flags
);
185 midi
->interrupt_disable(midi
,midi
->tx_enable
);
186 midi
->midi_mode
&= ~CA_MIDI_MODE_OUTPUT
;
187 midi
->substream_output
= NULL
;
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);
193 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
198 static void ca_midi_input_trigger(snd_rawmidi_substream_t
* substream
, int up
)
200 ca_midi_t
*midi
= (ca_midi_t
*)substream
->rmidi
->private_data
;
201 snd_assert(midi
->dev_id
, return);
204 midi
->interrupt_enable(midi
,midi
->rx_enable
);
206 midi
->interrupt_disable(midi
, midi
->rx_enable
);
210 static void ca_midi_output_trigger(snd_rawmidi_substream_t
* substream
, int up
)
212 ca_midi_t
*midi
= (ca_midi_t
*)substream
->rmidi
->private_data
;
215 snd_assert(midi
->dev_id
, return);
221 spin_lock_irqsave(&midi
->output_lock
, flags
);
223 /* try to send some amount of bytes here before interrupts */
225 if (ca_midi_output_ready(midi
)) {
226 if (!(midi
->midi_mode
& CA_MIDI_MODE_OUTPUT
) ||
227 snd_rawmidi_transmit(substream
, &byte
, 1) != 1) {
229 spin_unlock_irqrestore(&midi
->output_lock
, flags
);
232 ca_midi_write_data(midi
, byte
);
239 spin_unlock_irqrestore(&midi
->output_lock
, flags
);
240 midi
->interrupt_enable(midi
,midi
->tx_enable
);
243 midi
->interrupt_disable(midi
,midi
->tx_enable
);
247 static snd_rawmidi_ops_t ca_midi_output
=
249 .open
= ca_midi_output_open
,
250 .close
= ca_midi_output_close
,
251 .trigger
= ca_midi_output_trigger
,
254 static snd_rawmidi_ops_t ca_midi_input
=
256 .open
= ca_midi_input_open
,
257 .close
= ca_midi_input_close
,
258 .trigger
= ca_midi_input_trigger
,
261 static void ca_midi_free(ca_midi_t
*midi
) {
262 midi
->interrupt
= NULL
;
263 midi
->interrupt_enable
= NULL
;
264 midi
->interrupt_disable
= NULL
;
267 midi
->get_dev_id_card
= NULL
;
268 midi
->get_dev_id_port
= NULL
;
272 static void ca_rmidi_free(snd_rawmidi_t
*rmidi
)
274 ca_midi_free((ca_midi_t
*)rmidi
->private_data
);
277 int __devinit
ca_midi_init(void *dev_id
, ca_midi_t
*midi
, int device
, char *name
)
279 snd_rawmidi_t
*rmidi
;
282 if ((err
= snd_rawmidi_new(midi
->get_dev_id_card(midi
->dev_id
), name
, device
, 1, 1, &rmidi
)) < 0)
285 midi
->dev_id
= dev_id
;
286 midi
->interrupt
= ca_midi_interrupt
;
288 spin_lock_init(&midi
->open_lock
);
289 spin_lock_init(&midi
->input_lock
);
290 spin_lock_init(&midi
->output_lock
);
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
;