^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /****************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Copyright Echo Digital Audio Corporation (c) 1998 - 2004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) All rights reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) www.echoaudio.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) This file is part of Echo Digital Audio's generic driver library.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) Echo Digital Audio's generic driver library is free software;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) you can redistribute it and/or modify it under the terms of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) the GNU General Public License as published by the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) You should have received a copy of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) along with this program; if not, write to the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) Foundation, Inc., 59 Temple Place - Suite 330, Boston,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) MA 02111-1307, USA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) Translation from C++ and adaptation for use in ALSA-Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) were made by Giuliano Pochini <pochini@shiny.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) ****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) MIDI lowlevel code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* Start and stop Midi input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int enable_midi_input(struct echoaudio *chip, char enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) dev_dbg(chip->card->dev, "enable_midi_input(%d)\n", enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (wait_handshake(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) chip->mtc_state = MIDI_IN_STATE_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) chip->comm_page->flags |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) cpu_to_le32(DSP_FLAG_MIDI_INPUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) chip->comm_page->flags &=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ~cpu_to_le32(DSP_FLAG_MIDI_INPUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) clear_handshake(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return send_vector(chip, DSP_VC_UPDATE_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* Send a buffer full of MIDI data to the DSP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) Returns how many actually written or < 0 on error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int write_midi(struct echoaudio *chip, u8 *data, int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (snd_BUG_ON(bytes <= 0 || bytes >= MIDI_OUT_BUFFER_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (wait_handshake(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* HF4 indicates that it is safe to write MIDI output data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (! (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_REG_HF4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) chip->comm_page->midi_output[0] = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) memcpy(&chip->comm_page->midi_output[1], data, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) chip->comm_page->midi_out_free_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) clear_handshake(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) send_vector(chip, DSP_VC_MIDI_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dev_dbg(chip->card->dev, "write_midi: %d\n", bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Run the state machine for MIDI input data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) MIDI time code sync isn't supported by this code right now, but you still need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) this state machine to parse the incoming MIDI data stream. Every time the DSP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) sees a 0xF1 byte come in, it adds the DSP sample position to the MIDI data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) stream. The DSP sample position is represented as a 32 bit unsigned value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) with the high 16 bits first, followed by the low 16 bits. Since these aren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) real MIDI bytes, the following logic is needed to skip them. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static inline int mtc_process_data(struct echoaudio *chip, short midi_byte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) switch (chip->mtc_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case MIDI_IN_STATE_NORMAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (midi_byte == 0xF1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) chip->mtc_state = MIDI_IN_STATE_TS_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) case MIDI_IN_STATE_TS_HIGH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) chip->mtc_state = MIDI_IN_STATE_TS_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return MIDI_IN_SKIP_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case MIDI_IN_STATE_TS_LOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) chip->mtc_state = MIDI_IN_STATE_F1_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return MIDI_IN_SKIP_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) case MIDI_IN_STATE_F1_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) chip->mtc_state = MIDI_IN_STATE_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* This function is called from the IRQ handler and it reads the midi data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) from the DSP's buffer. It returns the number of bytes received. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int midi_service_irq(struct echoaudio *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) short int count, midi_byte, i, received;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* The count is at index 0, followed by actual data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) count = le16_to_cpu(chip->comm_page->midi_input[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (snd_BUG_ON(count >= MIDI_IN_BUFFER_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* Get the MIDI data from the comm page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) received = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) for (i = 1; i <= count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* Get the MIDI byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) midi_byte = le16_to_cpu(chip->comm_page->midi_input[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /* Parse the incoming MIDI stream. The incoming MIDI data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) consists of MIDI bytes and timestamps for the MIDI time code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 0xF1 bytes. mtc_process_data() is a little state machine that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) parses the stream. If you get MIDI_IN_SKIP_DATA back, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) this is a timestamp byte, not a MIDI byte, so don't store it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) in the MIDI input buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (mtc_process_data(chip, midi_byte) == MIDI_IN_SKIP_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) chip->midi_buffer[received++] = (u8)midi_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return received;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) MIDI interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int snd_echo_midi_input_open(struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct echoaudio *chip = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) chip->midi_in = substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static void snd_echo_midi_input_trigger(struct snd_rawmidi_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct echoaudio *chip = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (up != chip->midi_input_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) spin_lock_irq(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) enable_midi_input(chip, up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) spin_unlock_irq(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) chip->midi_input_enabled = up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int snd_echo_midi_input_close(struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct echoaudio *chip = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) chip->midi_in = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int snd_echo_midi_output_open(struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct echoaudio *chip = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) chip->tinuse = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) chip->midi_full = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) chip->midi_out = substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static void snd_echo_midi_output_write(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct echoaudio *chip = from_timer(chip, t, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int bytes, sent, time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned char buf[MIDI_OUT_BUFFER_SIZE - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* No interrupts are involved: we have to check at regular intervals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if the card's output buffer has room for new data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) sent = bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) spin_lock_irqsave(&chip->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) chip->midi_full = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (!snd_rawmidi_transmit_empty(chip->midi_out)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) bytes = snd_rawmidi_transmit_peek(chip->midi_out, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) MIDI_OUT_BUFFER_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) dev_dbg(chip->card->dev, "Try to send %d bytes...\n", bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) sent = write_midi(chip, buf, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (sent < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) dev_err(chip->card->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) "write_midi() error %d\n", sent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /* retry later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) sent = 9000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) chip->midi_full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) } else if (sent > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) dev_dbg(chip->card->dev, "%d bytes sent\n", sent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) snd_rawmidi_transmit_ack(chip->midi_out, sent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /* Buffer is full. DSP's internal buffer is 64 (128 ?)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) bytes long. Let's wait until half of them are sent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dev_dbg(chip->card->dev, "Full\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) sent = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) chip->midi_full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /* We restart the timer only if there is some data left to send */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!snd_rawmidi_transmit_empty(chip->midi_out) && chip->tinuse) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* The timer will expire slightly after the data has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) sent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) time = (sent << 3) / 25 + 1; /* 8/25=0.32ms to send a byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) mod_timer(&chip->timer, jiffies + (time * HZ + 999) / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) dev_dbg(chip->card->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) "Timer armed(%d)\n", ((time * HZ + 999) / 1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) spin_unlock_irqrestore(&chip->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static void snd_echo_midi_output_trigger(struct snd_rawmidi_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct echoaudio *chip = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) dev_dbg(chip->card->dev, "snd_echo_midi_output_trigger(%d)\n", up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) spin_lock_irq(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (up) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (!chip->tinuse) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) timer_setup(&chip->timer, snd_echo_midi_output_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) chip->tinuse = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (chip->tinuse) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) chip->tinuse = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) spin_unlock_irq(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) del_timer_sync(&chip->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) dev_dbg(chip->card->dev, "Timer removed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) spin_unlock_irq(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (up && !chip->midi_full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) snd_echo_midi_output_write(&chip->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int snd_echo_midi_output_close(struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct echoaudio *chip = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) chip->midi_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static const struct snd_rawmidi_ops snd_echo_midi_input = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) .open = snd_echo_midi_input_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) .close = snd_echo_midi_input_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) .trigger = snd_echo_midi_input_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static const struct snd_rawmidi_ops snd_echo_midi_output = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) .open = snd_echo_midi_output_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) .close = snd_echo_midi_output_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) .trigger = snd_echo_midi_output_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /* <--snd_echo_probe() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static int snd_echo_midi_create(struct snd_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct echoaudio *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if ((err = snd_rawmidi_new(card, card->shortname, 0, 1, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) &chip->rmidi)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) strcpy(chip->rmidi->name, card->shortname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) chip->rmidi->private_data = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) snd_rawmidi_set_ops(chip->rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) &snd_echo_midi_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) snd_rawmidi_set_ops(chip->rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) &snd_echo_midi_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) chip->rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }