0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
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
0059
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
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
0084
0085
0086
0087
0088
0089
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
0115
0116 static int midi_service_irq(struct echoaudio *chip)
0117 {
0118 short int count, midi_byte, i, received;
0119
0120
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
0127 received = 0;
0128 for (i = 1; i <= count; i++) {
0129
0130 midi_byte = le16_to_cpu(chip->comm_page->midi_input[i]);
0131
0132
0133
0134
0135
0136
0137
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
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
0209
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
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
0229
0230 dev_dbg(chip->card->dev, "Full\n");
0231 sent = 32;
0232 chip->midi_full = 1;
0233 }
0234 }
0235
0236
0237 if (!snd_rawmidi_transmit_empty(chip->midi_out) && chip->tinuse) {
0238
0239
0240 time = (sent << 3) / 25 + 1;
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
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 }