Back to home page

OSCL-LXR

 
 

    


0001 /****************************************************************************
0002 
0003    Copyright Echo Digital Audio Corporation (c) 1998 - 2004
0004    All rights reserved
0005    www.echoaudio.com
0006 
0007    This file is part of Echo Digital Audio's generic driver library.
0008 
0009    Echo Digital Audio's generic driver library is free software;
0010    you can redistribute it and/or modify it under the terms of
0011    the GNU General Public License as published by the Free Software
0012    Foundation.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program; if not, write to the Free Software
0021    Foundation, Inc., 59 Temple Place - Suite 330, Boston,
0022    MA  02111-1307, USA.
0023 
0024    *************************************************************************
0025 
0026  Translation from C++ and adaptation for use in ALSA-Driver
0027  were made by Giuliano Pochini <pochini@shiny.it>
0028 
0029 ****************************************************************************/
0030 
0031 
0032 /******************************************************************************
0033     MIDI lowlevel code
0034 ******************************************************************************/
0035 
0036 /* Start and stop Midi input */
0037 static int enable_midi_input(struct echoaudio *chip, char enable)
0038 {
0039     dev_dbg(chip->card->dev, "enable_midi_input(%d)\n", enable);
0040 
0041     if (wait_handshake(chip))
0042         return -EIO;
0043 
0044     if (enable) {
0045         chip->mtc_state = MIDI_IN_STATE_NORMAL;
0046         chip->comm_page->flags |=
0047             cpu_to_le32(DSP_FLAG_MIDI_INPUT);
0048     } else
0049         chip->comm_page->flags &=
0050             ~cpu_to_le32(DSP_FLAG_MIDI_INPUT);
0051 
0052     clear_handshake(chip);
0053     return send_vector(chip, DSP_VC_UPDATE_FLAGS);
0054 }
0055 
0056 
0057 
0058 /* Send a buffer full of MIDI data to the DSP
0059 Returns how many actually written or < 0 on error */
0060 static int write_midi(struct echoaudio *chip, u8 *data, int bytes)
0061 {
0062     if (snd_BUG_ON(bytes <= 0 || bytes >= MIDI_OUT_BUFFER_SIZE))
0063         return -EINVAL;
0064 
0065     if (wait_handshake(chip))
0066         return -EIO;
0067 
0068     /* HF4 indicates that it is safe to write MIDI output data */
0069     if (! (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_REG_HF4))
0070         return 0;
0071 
0072     chip->comm_page->midi_output[0] = bytes;
0073     memcpy(&chip->comm_page->midi_output[1], data, bytes);
0074     chip->comm_page->midi_out_free_count = 0;
0075     clear_handshake(chip);
0076     send_vector(chip, DSP_VC_MIDI_WRITE);
0077     dev_dbg(chip->card->dev, "write_midi: %d\n", bytes);
0078     return bytes;
0079 }
0080 
0081 
0082 
0083 /* Run the state machine for MIDI input data
0084 MIDI time code sync isn't supported by this code right now, but you still need
0085 this state machine to parse the incoming MIDI data stream.  Every time the DSP
0086 sees a 0xF1 byte come in, it adds the DSP sample position to the MIDI data
0087 stream. The DSP sample position is represented as a 32 bit unsigned value,
0088 with the high 16 bits first, followed by the low 16 bits. Since these aren't
0089 real MIDI bytes, the following logic is needed to skip them. */
0090 static inline int mtc_process_data(struct echoaudio *chip, short midi_byte)
0091 {
0092     switch (chip->mtc_state) {
0093     case MIDI_IN_STATE_NORMAL:
0094         if (midi_byte == 0xF1)
0095             chip->mtc_state = MIDI_IN_STATE_TS_HIGH;
0096         break;
0097     case MIDI_IN_STATE_TS_HIGH:
0098         chip->mtc_state = MIDI_IN_STATE_TS_LOW;
0099         return MIDI_IN_SKIP_DATA;
0100         break;
0101     case MIDI_IN_STATE_TS_LOW:
0102         chip->mtc_state = MIDI_IN_STATE_F1_DATA;
0103         return MIDI_IN_SKIP_DATA;
0104         break;
0105     case MIDI_IN_STATE_F1_DATA:
0106         chip->mtc_state = MIDI_IN_STATE_NORMAL;
0107         break;
0108     }
0109     return 0;
0110 }
0111 
0112 
0113 
0114 /* This function is called from the IRQ handler and it reads the midi data
0115 from the DSP's buffer.  It returns the number of bytes received. */
0116 static int midi_service_irq(struct echoaudio *chip)
0117 {
0118     short int count, midi_byte, i, received;
0119 
0120     /* The count is at index 0, followed by actual data */
0121     count = le16_to_cpu(chip->comm_page->midi_input[0]);
0122 
0123     if (snd_BUG_ON(count >= MIDI_IN_BUFFER_SIZE))
0124         return 0;
0125 
0126     /* Get the MIDI data from the comm page */
0127     received = 0;
0128     for (i = 1; i <= count; i++) {
0129         /* Get the MIDI byte */
0130         midi_byte = le16_to_cpu(chip->comm_page->midi_input[i]);
0131 
0132         /* Parse the incoming MIDI stream. The incoming MIDI data
0133         consists of MIDI bytes and timestamps for the MIDI time code
0134         0xF1 bytes. mtc_process_data() is a little state machine that
0135         parses the stream. If you get MIDI_IN_SKIP_DATA back, then
0136         this is a timestamp byte, not a MIDI byte, so don't store it
0137         in the MIDI input buffer. */
0138         if (mtc_process_data(chip, midi_byte) == MIDI_IN_SKIP_DATA)
0139             continue;
0140 
0141         chip->midi_buffer[received++] = (u8)midi_byte;
0142     }
0143 
0144     return received;
0145 }
0146 
0147 
0148 
0149 
0150 /******************************************************************************
0151     MIDI interface
0152 ******************************************************************************/
0153 
0154 static int snd_echo_midi_input_open(struct snd_rawmidi_substream *substream)
0155 {
0156     struct echoaudio *chip = substream->rmidi->private_data;
0157 
0158     chip->midi_in = substream;
0159     return 0;
0160 }
0161 
0162 
0163 
0164 static void snd_echo_midi_input_trigger(struct snd_rawmidi_substream *substream,
0165                     int up)
0166 {
0167     struct echoaudio *chip = substream->rmidi->private_data;
0168 
0169     if (up != chip->midi_input_enabled) {
0170         spin_lock_irq(&chip->lock);
0171         enable_midi_input(chip, up);
0172         spin_unlock_irq(&chip->lock);
0173         chip->midi_input_enabled = up;
0174     }
0175 }
0176 
0177 
0178 
0179 static int snd_echo_midi_input_close(struct snd_rawmidi_substream *substream)
0180 {
0181     struct echoaudio *chip = substream->rmidi->private_data;
0182 
0183     chip->midi_in = NULL;
0184     return 0;
0185 }
0186 
0187 
0188 
0189 static int snd_echo_midi_output_open(struct snd_rawmidi_substream *substream)
0190 {
0191     struct echoaudio *chip = substream->rmidi->private_data;
0192 
0193     chip->tinuse = 0;
0194     chip->midi_full = 0;
0195     chip->midi_out = substream;
0196     return 0;
0197 }
0198 
0199 
0200 
0201 static void snd_echo_midi_output_write(struct timer_list *t)
0202 {
0203     struct echoaudio *chip = from_timer(chip, t, timer);
0204     unsigned long flags;
0205     int bytes, sent, time;
0206     unsigned char buf[MIDI_OUT_BUFFER_SIZE - 1];
0207 
0208     /* No interrupts are involved: we have to check at regular intervals
0209     if the card's output buffer has room for new data. */
0210     sent = 0;
0211     spin_lock_irqsave(&chip->lock, flags);
0212     chip->midi_full = 0;
0213     if (!snd_rawmidi_transmit_empty(chip->midi_out)) {
0214         bytes = snd_rawmidi_transmit_peek(chip->midi_out, buf,
0215                           MIDI_OUT_BUFFER_SIZE - 1);
0216         dev_dbg(chip->card->dev, "Try to send %d bytes...\n", bytes);
0217         sent = write_midi(chip, buf, bytes);
0218         if (sent < 0) {
0219             dev_err(chip->card->dev,
0220                 "write_midi() error %d\n", sent);
0221             /* retry later */
0222             sent = 9000;
0223             chip->midi_full = 1;
0224         } else if (sent > 0) {
0225             dev_dbg(chip->card->dev, "%d bytes sent\n", sent);
0226             snd_rawmidi_transmit_ack(chip->midi_out, sent);
0227         } else {
0228             /* Buffer is full. DSP's internal buffer is 64 (128 ?)
0229             bytes long. Let's wait until half of them are sent */
0230             dev_dbg(chip->card->dev, "Full\n");
0231             sent = 32;
0232             chip->midi_full = 1;
0233         }
0234     }
0235 
0236     /* We restart the timer only if there is some data left to send */
0237     if (!snd_rawmidi_transmit_empty(chip->midi_out) && chip->tinuse) {
0238         /* The timer will expire slightly after the data has been
0239            sent */
0240         time = (sent << 3) / 25 + 1;    /* 8/25=0.32ms to send a byte */
0241         mod_timer(&chip->timer, jiffies + (time * HZ + 999) / 1000);
0242         dev_dbg(chip->card->dev,
0243             "Timer armed(%d)\n", ((time * HZ + 999) / 1000));
0244     }
0245     spin_unlock_irqrestore(&chip->lock, flags);
0246 }
0247 
0248 
0249 
0250 static void snd_echo_midi_output_trigger(struct snd_rawmidi_substream *substream,
0251                      int up)
0252 {
0253     struct echoaudio *chip = substream->rmidi->private_data;
0254 
0255     dev_dbg(chip->card->dev, "snd_echo_midi_output_trigger(%d)\n", up);
0256     spin_lock_irq(&chip->lock);
0257     if (up) {
0258         if (!chip->tinuse) {
0259             timer_setup(&chip->timer, snd_echo_midi_output_write,
0260                     0);
0261             chip->tinuse = 1;
0262         }
0263     } else {
0264         if (chip->tinuse) {
0265             chip->tinuse = 0;
0266             spin_unlock_irq(&chip->lock);
0267             del_timer_sync(&chip->timer);
0268             dev_dbg(chip->card->dev, "Timer removed\n");
0269             return;
0270         }
0271     }
0272     spin_unlock_irq(&chip->lock);
0273 
0274     if (up && !chip->midi_full)
0275         snd_echo_midi_output_write(&chip->timer);
0276 }
0277 
0278 
0279 
0280 static int snd_echo_midi_output_close(struct snd_rawmidi_substream *substream)
0281 {
0282     struct echoaudio *chip = substream->rmidi->private_data;
0283 
0284     chip->midi_out = NULL;
0285     return 0;
0286 }
0287 
0288 
0289 
0290 static const struct snd_rawmidi_ops snd_echo_midi_input = {
0291     .open = snd_echo_midi_input_open,
0292     .close = snd_echo_midi_input_close,
0293     .trigger = snd_echo_midi_input_trigger,
0294 };
0295 
0296 static const struct snd_rawmidi_ops snd_echo_midi_output = {
0297     .open = snd_echo_midi_output_open,
0298     .close = snd_echo_midi_output_close,
0299     .trigger = snd_echo_midi_output_trigger,
0300 };
0301 
0302 
0303 
0304 /* <--snd_echo_probe() */
0305 static int snd_echo_midi_create(struct snd_card *card,
0306                 struct echoaudio *chip)
0307 {
0308     int err;
0309 
0310     err = snd_rawmidi_new(card, card->shortname, 0, 1, 1, &chip->rmidi);
0311     if (err < 0)
0312         return err;
0313 
0314     strcpy(chip->rmidi->name, card->shortname);
0315     chip->rmidi->private_data = chip;
0316 
0317     snd_rawmidi_set_ops(chip->rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
0318                 &snd_echo_midi_input);
0319     snd_rawmidi_set_ops(chip->rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
0320                 &snd_echo_midi_output);
0321 
0322     chip->rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
0323         SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
0324     return 0;
0325 }