1 /****************************************************************************
3 Copyright Echo Digital Audio Corporation (c) 1998 - 2004
7 This file is part of Echo Digital Audio's generic driver library.
9 Echo Digital Audio's generic driver library is free software;
10 you can redistribute it and/or modify it under the terms of
11 the GNU General Public License as published by the Free Software
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 *************************************************************************
26 Translation from C++ and adaptation for use in ALSA-Driver
27 were made by Giuliano Pochini <pochini@shiny.it>
29 ****************************************************************************/
32 /******************************************************************************
34 ******************************************************************************/
36 /* Start and stop Midi input */
37 static int enable_midi_input(struct echoaudio *chip, char enable)
39 DE_MID(("enable_midi_input(%d)\n", enable));
41 if (wait_handshake(chip))
45 chip->mtc_state = MIDI_IN_STATE_NORMAL;
46 chip->comm_page->flags |=
47 __constant_cpu_to_le32(DSP_FLAG_MIDI_INPUT);
49 chip->comm_page->flags &=
50 ~__constant_cpu_to_le32(DSP_FLAG_MIDI_INPUT);
52 clear_handshake(chip);
53 return send_vector(chip, DSP_VC_UPDATE_FLAGS);
58 /* Send a buffer full of MIDI data to the DSP
59 Returns how many actually written or < 0 on error */
60 static int write_midi(struct echoaudio *chip, u8 *data, int bytes)
62 snd_assert(bytes > 0 && bytes < MIDI_OUT_BUFFER_SIZE, return -EINVAL);
64 if (wait_handshake(chip))
67 /* HF4 indicates that it is safe to write MIDI output data */
68 if (! (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_REG_HF4))
71 chip->comm_page->midi_output[0] = bytes;
72 memcpy(&chip->comm_page->midi_output[1], data, bytes);
73 chip->comm_page->midi_out_free_count = 0;
74 clear_handshake(chip);
75 send_vector(chip, DSP_VC_MIDI_WRITE);
76 DE_MID(("write_midi: %d\n", bytes));
82 /* Run the state machine for MIDI input data
83 MIDI time code sync isn't supported by this code right now, but you still need
84 this state machine to parse the incoming MIDI data stream. Every time the DSP
85 sees a 0xF1 byte come in, it adds the DSP sample position to the MIDI data
86 stream. The DSP sample position is represented as a 32 bit unsigned value,
87 with the high 16 bits first, followed by the low 16 bits. Since these aren't
88 real MIDI bytes, the following logic is needed to skip them. */
89 static inline int mtc_process_data(struct echoaudio *chip, short midi_byte)
91 switch (chip->mtc_state) {
92 case MIDI_IN_STATE_NORMAL:
93 if (midi_byte == 0xF1)
94 chip->mtc_state = MIDI_IN_STATE_TS_HIGH;
96 case MIDI_IN_STATE_TS_HIGH:
97 chip->mtc_state = MIDI_IN_STATE_TS_LOW;
98 return MIDI_IN_SKIP_DATA;
100 case MIDI_IN_STATE_TS_LOW:
101 chip->mtc_state = MIDI_IN_STATE_F1_DATA;
102 return MIDI_IN_SKIP_DATA;
104 case MIDI_IN_STATE_F1_DATA:
105 chip->mtc_state = MIDI_IN_STATE_NORMAL;
113 /* This function is called from the IRQ handler and it reads the midi data
114 from the DSP's buffer. It returns the number of bytes received. */
115 static int midi_service_irq(struct echoaudio *chip)
117 short int count, midi_byte, i, received;
119 /* The count is at index 0, followed by actual data */
120 count = le16_to_cpu(chip->comm_page->midi_input[0]);
122 snd_assert(count < MIDI_IN_BUFFER_SIZE, return 0);
124 /* Get the MIDI data from the comm page */
127 for (i = 1; i <= count; i++) {
128 /* Get the MIDI byte */
129 midi_byte = le16_to_cpu(chip->comm_page->midi_input[i]);
131 /* Parse the incoming MIDI stream. The incoming MIDI data
132 consists of MIDI bytes and timestamps for the MIDI time code
133 0xF1 bytes. mtc_process_data() is a little state machine that
134 parses the stream. If you get MIDI_IN_SKIP_DATA back, then
135 this is a timestamp byte, not a MIDI byte, so don't store it
136 in the MIDI input buffer. */
137 if (mtc_process_data(chip, midi_byte) == MIDI_IN_SKIP_DATA)
140 chip->midi_buffer[received++] = (u8)midi_byte;
149 /******************************************************************************
151 ******************************************************************************/
153 static int snd_echo_midi_input_open(struct snd_rawmidi_substream *substream)
155 struct echoaudio *chip = substream->rmidi->private_data;
157 chip->midi_in = substream;
158 DE_MID(("rawmidi_iopen\n"));
164 static void snd_echo_midi_input_trigger(struct snd_rawmidi_substream *substream,
167 struct echoaudio *chip = substream->rmidi->private_data;
169 if (up != chip->midi_input_enabled) {
170 spin_lock_irq(&chip->lock);
171 enable_midi_input(chip, up);
172 spin_unlock_irq(&chip->lock);
173 chip->midi_input_enabled = up;
179 static int snd_echo_midi_input_close(struct snd_rawmidi_substream *substream)
181 struct echoaudio *chip = substream->rmidi->private_data;
183 chip->midi_in = NULL;
184 DE_MID(("rawmidi_iclose\n"));
190 static int snd_echo_midi_output_open(struct snd_rawmidi_substream *substream)
192 struct echoaudio *chip = substream->rmidi->private_data;
196 chip->midi_out = substream;
197 DE_MID(("rawmidi_oopen\n"));
203 static void snd_echo_midi_output_write(unsigned long data)
205 struct echoaudio *chip = (struct echoaudio *)data;
207 int bytes, sent, time;
208 unsigned char buf[MIDI_OUT_BUFFER_SIZE - 1];
210 DE_MID(("snd_echo_midi_output_write\n"));
211 /* No interrupts are involved: we have to check at regular intervals
212 if the card's output buffer has room for new data. */
214 spin_lock_irqsave(&chip->lock, flags);
216 if (!snd_rawmidi_transmit_empty(chip->midi_out)) {
217 bytes = snd_rawmidi_transmit_peek(chip->midi_out, buf,
218 MIDI_OUT_BUFFER_SIZE - 1);
219 DE_MID(("Try to send %d bytes...\n", bytes));
220 sent = write_midi(chip, buf, bytes);
222 snd_printk(KERN_ERR "write_midi() error %d\n", sent);
226 } else if (sent > 0) {
227 DE_MID(("%d bytes sent\n", sent));
228 snd_rawmidi_transmit_ack(chip->midi_out, sent);
230 /* Buffer is full. DSP's internal buffer is 64 (128 ?)
231 bytes long. Let's wait until half of them are sent */
238 /* We restart the timer only if there is some data left to send */
239 if (!snd_rawmidi_transmit_empty(chip->midi_out) && chip->tinuse) {
240 /* The timer will expire slightly after the data has been
242 time = (sent << 3) / 25 + 1; /* 8/25=0.32ms to send a byte */
243 mod_timer(&chip->timer, jiffies + (time * HZ + 999) / 1000);
244 DE_MID(("Timer armed(%d)\n", ((time * HZ + 999) / 1000)));
246 spin_unlock_irqrestore(&chip->lock, flags);
251 static void snd_echo_midi_output_trigger(struct snd_rawmidi_substream *substream,
254 struct echoaudio *chip = substream->rmidi->private_data;
256 DE_MID(("snd_echo_midi_output_trigger(%d)\n", up));
257 spin_lock_irq(&chip->lock);
260 init_timer(&chip->timer);
261 chip->timer.function = snd_echo_midi_output_write;
262 chip->timer.data = (unsigned long)chip;
268 spin_unlock_irq(&chip->lock);
269 del_timer_sync(&chip->timer);
270 DE_MID(("Timer removed\n"));
274 spin_unlock_irq(&chip->lock);
276 if (up && !chip->midi_full)
277 snd_echo_midi_output_write((unsigned long)chip);
282 static int snd_echo_midi_output_close(struct snd_rawmidi_substream *substream)
284 struct echoaudio *chip = substream->rmidi->private_data;
286 chip->midi_out = NULL;
287 DE_MID(("rawmidi_oclose\n"));
293 static struct snd_rawmidi_ops snd_echo_midi_input = {
294 .open = snd_echo_midi_input_open,
295 .close = snd_echo_midi_input_close,
296 .trigger = snd_echo_midi_input_trigger,
299 static struct snd_rawmidi_ops snd_echo_midi_output = {
300 .open = snd_echo_midi_output_open,
301 .close = snd_echo_midi_output_close,
302 .trigger = snd_echo_midi_output_trigger,
307 /* <--snd_echo_probe() */
308 static int __devinit snd_echo_midi_create(struct snd_card *card,
309 struct echoaudio *chip)
313 if ((err = snd_rawmidi_new(card, card->shortname, 0, 1, 1,
317 strcpy(chip->rmidi->name, card->shortname);
318 chip->rmidi->private_data = chip;
320 snd_rawmidi_set_ops(chip->rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
321 &snd_echo_midi_input);
322 snd_rawmidi_set_ops(chip->rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
323 &snd_echo_midi_output);
325 chip->rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
326 SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
327 DE_INIT(("MIDI ok\n"));