sb8_midi.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Routines for control of SoundBlaster cards - MIDI interface
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  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. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * --
  20. *
  21. * Sun May 9 22:54:38 BST 1999 George David Morrison <gdm@gedamo.demon.co.uk>
  22. * Fixed typo in snd_sb8dsp_midi_new_device which prevented midi from
  23. * working.
  24. *
  25. * Sun May 11 12:34:56 UTC 2003 Clemens Ladisch <clemens@ladisch.de>
  26. * Added full duplex UART mode for DSP version 2.0 and later.
  27. */
  28. #include <linux/io.h>
  29. #include <linux/time.h>
  30. #include <sound/core.h>
  31. #include <sound/sb.h>
  32. irqreturn_t snd_sb8dsp_midi_interrupt(struct snd_sb *chip)
  33. {
  34. struct snd_rawmidi *rmidi;
  35. int max = 64;
  36. char byte;
  37. if (!chip)
  38. return IRQ_NONE;
  39. rmidi = chip->rmidi;
  40. if (!rmidi) {
  41. inb(SBP(chip, DATA_AVAIL)); /* ack interrupt */
  42. return IRQ_NONE;
  43. }
  44. spin_lock(&chip->midi_input_lock);
  45. while (max-- > 0) {
  46. if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
  47. byte = inb(SBP(chip, READ));
  48. if (chip->open & SB_OPEN_MIDI_INPUT_TRIGGER) {
  49. snd_rawmidi_receive(chip->midi_substream_input, &byte, 1);
  50. }
  51. }
  52. }
  53. spin_unlock(&chip->midi_input_lock);
  54. return IRQ_HANDLED;
  55. }
  56. static int snd_sb8dsp_midi_input_open(struct snd_rawmidi_substream *substream)
  57. {
  58. unsigned long flags;
  59. struct snd_sb *chip;
  60. unsigned int valid_open_flags;
  61. chip = substream->rmidi->private_data;
  62. valid_open_flags = chip->hardware >= SB_HW_20
  63. ? SB_OPEN_MIDI_OUTPUT | SB_OPEN_MIDI_OUTPUT_TRIGGER : 0;
  64. spin_lock_irqsave(&chip->open_lock, flags);
  65. if (chip->open & ~valid_open_flags) {
  66. spin_unlock_irqrestore(&chip->open_lock, flags);
  67. return -EAGAIN;
  68. }
  69. chip->open |= SB_OPEN_MIDI_INPUT;
  70. chip->midi_substream_input = substream;
  71. if (!(chip->open & SB_OPEN_MIDI_OUTPUT)) {
  72. spin_unlock_irqrestore(&chip->open_lock, flags);
  73. snd_sbdsp_reset(chip); /* reset DSP */
  74. if (chip->hardware >= SB_HW_20)
  75. snd_sbdsp_command(chip, SB_DSP_MIDI_UART_IRQ);
  76. } else {
  77. spin_unlock_irqrestore(&chip->open_lock, flags);
  78. }
  79. return 0;
  80. }
  81. static int snd_sb8dsp_midi_output_open(struct snd_rawmidi_substream *substream)
  82. {
  83. unsigned long flags;
  84. struct snd_sb *chip;
  85. unsigned int valid_open_flags;
  86. chip = substream->rmidi->private_data;
  87. valid_open_flags = chip->hardware >= SB_HW_20
  88. ? SB_OPEN_MIDI_INPUT | SB_OPEN_MIDI_INPUT_TRIGGER : 0;
  89. spin_lock_irqsave(&chip->open_lock, flags);
  90. if (chip->open & ~valid_open_flags) {
  91. spin_unlock_irqrestore(&chip->open_lock, flags);
  92. return -EAGAIN;
  93. }
  94. chip->open |= SB_OPEN_MIDI_OUTPUT;
  95. chip->midi_substream_output = substream;
  96. if (!(chip->open & SB_OPEN_MIDI_INPUT)) {
  97. spin_unlock_irqrestore(&chip->open_lock, flags);
  98. snd_sbdsp_reset(chip); /* reset DSP */
  99. if (chip->hardware >= SB_HW_20)
  100. snd_sbdsp_command(chip, SB_DSP_MIDI_UART_IRQ);
  101. } else {
  102. spin_unlock_irqrestore(&chip->open_lock, flags);
  103. }
  104. return 0;
  105. }
  106. static int snd_sb8dsp_midi_input_close(struct snd_rawmidi_substream *substream)
  107. {
  108. unsigned long flags;
  109. struct snd_sb *chip;
  110. chip = substream->rmidi->private_data;
  111. spin_lock_irqsave(&chip->open_lock, flags);
  112. chip->open &= ~(SB_OPEN_MIDI_INPUT | SB_OPEN_MIDI_INPUT_TRIGGER);
  113. chip->midi_substream_input = NULL;
  114. if (!(chip->open & SB_OPEN_MIDI_OUTPUT)) {
  115. spin_unlock_irqrestore(&chip->open_lock, flags);
  116. snd_sbdsp_reset(chip); /* reset DSP */
  117. } else {
  118. spin_unlock_irqrestore(&chip->open_lock, flags);
  119. }
  120. return 0;
  121. }
  122. static int snd_sb8dsp_midi_output_close(struct snd_rawmidi_substream *substream)
  123. {
  124. unsigned long flags;
  125. struct snd_sb *chip;
  126. chip = substream->rmidi->private_data;
  127. spin_lock_irqsave(&chip->open_lock, flags);
  128. chip->open &= ~(SB_OPEN_MIDI_OUTPUT | SB_OPEN_MIDI_OUTPUT_TRIGGER);
  129. chip->midi_substream_output = NULL;
  130. if (!(chip->open & SB_OPEN_MIDI_INPUT)) {
  131. spin_unlock_irqrestore(&chip->open_lock, flags);
  132. snd_sbdsp_reset(chip); /* reset DSP */
  133. } else {
  134. spin_unlock_irqrestore(&chip->open_lock, flags);
  135. }
  136. return 0;
  137. }
  138. static void snd_sb8dsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  139. {
  140. unsigned long flags;
  141. struct snd_sb *chip;
  142. chip = substream->rmidi->private_data;
  143. spin_lock_irqsave(&chip->open_lock, flags);
  144. if (up) {
  145. if (!(chip->open & SB_OPEN_MIDI_INPUT_TRIGGER)) {
  146. if (chip->hardware < SB_HW_20)
  147. snd_sbdsp_command(chip, SB_DSP_MIDI_INPUT_IRQ);
  148. chip->open |= SB_OPEN_MIDI_INPUT_TRIGGER;
  149. }
  150. } else {
  151. if (chip->open & SB_OPEN_MIDI_INPUT_TRIGGER) {
  152. if (chip->hardware < SB_HW_20)
  153. snd_sbdsp_command(chip, SB_DSP_MIDI_INPUT_IRQ);
  154. chip->open &= ~SB_OPEN_MIDI_INPUT_TRIGGER;
  155. }
  156. }
  157. spin_unlock_irqrestore(&chip->open_lock, flags);
  158. }
  159. static void snd_sb8dsp_midi_output_write(struct snd_rawmidi_substream *substream)
  160. {
  161. unsigned long flags;
  162. struct snd_sb *chip;
  163. char byte;
  164. int max = 32;
  165. /* how big is Tx FIFO? */
  166. chip = substream->rmidi->private_data;
  167. while (max-- > 0) {
  168. spin_lock_irqsave(&chip->open_lock, flags);
  169. if (snd_rawmidi_transmit_peek(substream, &byte, 1) != 1) {
  170. chip->open &= ~SB_OPEN_MIDI_OUTPUT_TRIGGER;
  171. del_timer(&chip->midi_timer);
  172. spin_unlock_irqrestore(&chip->open_lock, flags);
  173. break;
  174. }
  175. if (chip->hardware >= SB_HW_20) {
  176. int timeout = 8;
  177. while ((inb(SBP(chip, STATUS)) & 0x80) != 0 && --timeout > 0)
  178. ;
  179. if (timeout == 0) {
  180. /* Tx FIFO full - try again later */
  181. spin_unlock_irqrestore(&chip->open_lock, flags);
  182. break;
  183. }
  184. outb(byte, SBP(chip, WRITE));
  185. } else {
  186. snd_sbdsp_command(chip, SB_DSP_MIDI_OUTPUT);
  187. snd_sbdsp_command(chip, byte);
  188. }
  189. snd_rawmidi_transmit_ack(substream, 1);
  190. spin_unlock_irqrestore(&chip->open_lock, flags);
  191. }
  192. }
  193. static void snd_sb8dsp_midi_output_timer(unsigned long data)
  194. {
  195. struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *) data;
  196. struct snd_sb * chip = substream->rmidi->private_data;
  197. unsigned long flags;
  198. spin_lock_irqsave(&chip->open_lock, flags);
  199. mod_timer(&chip->midi_timer, 1 + jiffies);
  200. spin_unlock_irqrestore(&chip->open_lock, flags);
  201. snd_sb8dsp_midi_output_write(substream);
  202. }
  203. static void snd_sb8dsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  204. {
  205. unsigned long flags;
  206. struct snd_sb *chip;
  207. chip = substream->rmidi->private_data;
  208. spin_lock_irqsave(&chip->open_lock, flags);
  209. if (up) {
  210. if (!(chip->open & SB_OPEN_MIDI_OUTPUT_TRIGGER)) {
  211. setup_timer(&chip->midi_timer,
  212. snd_sb8dsp_midi_output_timer,
  213. (unsigned long) substream);
  214. mod_timer(&chip->midi_timer, 1 + jiffies);
  215. chip->open |= SB_OPEN_MIDI_OUTPUT_TRIGGER;
  216. }
  217. } else {
  218. if (chip->open & SB_OPEN_MIDI_OUTPUT_TRIGGER) {
  219. chip->open &= ~SB_OPEN_MIDI_OUTPUT_TRIGGER;
  220. }
  221. }
  222. spin_unlock_irqrestore(&chip->open_lock, flags);
  223. if (up)
  224. snd_sb8dsp_midi_output_write(substream);
  225. }
  226. static struct snd_rawmidi_ops snd_sb8dsp_midi_output =
  227. {
  228. .open = snd_sb8dsp_midi_output_open,
  229. .close = snd_sb8dsp_midi_output_close,
  230. .trigger = snd_sb8dsp_midi_output_trigger,
  231. };
  232. static struct snd_rawmidi_ops snd_sb8dsp_midi_input =
  233. {
  234. .open = snd_sb8dsp_midi_input_open,
  235. .close = snd_sb8dsp_midi_input_close,
  236. .trigger = snd_sb8dsp_midi_input_trigger,
  237. };
  238. int snd_sb8dsp_midi(struct snd_sb *chip, int device)
  239. {
  240. struct snd_rawmidi *rmidi;
  241. int err;
  242. if ((err = snd_rawmidi_new(chip->card, "SB8 MIDI", device, 1, 1, &rmidi)) < 0)
  243. return err;
  244. strcpy(rmidi->name, "SB8 MIDI");
  245. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_sb8dsp_midi_output);
  246. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_sb8dsp_midi_input);
  247. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT;
  248. if (chip->hardware >= SB_HW_20)
  249. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
  250. rmidi->private_data = chip;
  251. chip->rmidi = rmidi;
  252. return 0;
  253. }