midi.c 9.1 KB

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