sb8_midi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
  29. #include <asm/io.h>
  30. #include <linux/time.h>
  31. #include <sound/core.h>
  32. #include <sound/sb.h>
  33. /*
  34. */
  35. irqreturn_t snd_sb8dsp_midi_interrupt(struct snd_sb * chip)
  36. {
  37. struct snd_rawmidi *rmidi;
  38. int max = 64;
  39. char byte;
  40. if (chip == NULL || (rmidi = chip->rmidi) == NULL) {
  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. /*
  57. */
  58. static int snd_sb8dsp_midi_input_open(struct snd_rawmidi_substream *substream)
  59. {
  60. unsigned long flags;
  61. struct snd_sb *chip;
  62. unsigned int valid_open_flags;
  63. chip = substream->rmidi->private_data;
  64. valid_open_flags = chip->hardware >= SB_HW_20
  65. ? SB_OPEN_MIDI_OUTPUT | SB_OPEN_MIDI_OUTPUT_TRIGGER : 0;
  66. spin_lock_irqsave(&chip->open_lock, flags);
  67. if (chip->open & ~valid_open_flags) {
  68. spin_unlock_irqrestore(&chip->open_lock, flags);
  69. return -EAGAIN;
  70. }
  71. chip->open |= SB_OPEN_MIDI_INPUT;
  72. chip->midi_substream_input = substream;
  73. if (!(chip->open & SB_OPEN_MIDI_OUTPUT)) {
  74. spin_unlock_irqrestore(&chip->open_lock, flags);
  75. snd_sbdsp_reset(chip); /* reset DSP */
  76. if (chip->hardware >= SB_HW_20)
  77. snd_sbdsp_command(chip, SB_DSP_MIDI_UART_IRQ);
  78. } else {
  79. spin_unlock_irqrestore(&chip->open_lock, flags);
  80. }
  81. return 0;
  82. }
  83. static int snd_sb8dsp_midi_output_open(struct snd_rawmidi_substream *substream)
  84. {
  85. unsigned long flags;
  86. struct snd_sb *chip;
  87. unsigned int valid_open_flags;
  88. chip = substream->rmidi->private_data;
  89. valid_open_flags = chip->hardware >= SB_HW_20
  90. ? SB_OPEN_MIDI_INPUT | SB_OPEN_MIDI_INPUT_TRIGGER : 0;
  91. spin_lock_irqsave(&chip->open_lock, flags);
  92. if (chip->open & ~valid_open_flags) {
  93. spin_unlock_irqrestore(&chip->open_lock, flags);
  94. return -EAGAIN;
  95. }
  96. chip->open |= SB_OPEN_MIDI_OUTPUT;
  97. chip->midi_substream_output = substream;
  98. if (!(chip->open & SB_OPEN_MIDI_INPUT)) {
  99. spin_unlock_irqrestore(&chip->open_lock, flags);
  100. snd_sbdsp_reset(chip); /* reset DSP */
  101. if (chip->hardware >= SB_HW_20)
  102. snd_sbdsp_command(chip, SB_DSP_MIDI_UART_IRQ);
  103. } else {
  104. spin_unlock_irqrestore(&chip->open_lock, flags);
  105. }
  106. return 0;
  107. }
  108. static int snd_sb8dsp_midi_input_close(struct snd_rawmidi_substream *substream)
  109. {
  110. unsigned long flags;
  111. struct snd_sb *chip;
  112. chip = substream->rmidi->private_data;
  113. spin_lock_irqsave(&chip->open_lock, flags);
  114. chip->open &= ~(SB_OPEN_MIDI_INPUT | SB_OPEN_MIDI_INPUT_TRIGGER);
  115. chip->midi_substream_input = NULL;
  116. if (!(chip->open & SB_OPEN_MIDI_OUTPUT)) {
  117. spin_unlock_irqrestore(&chip->open_lock, flags);
  118. snd_sbdsp_reset(chip); /* reset DSP */
  119. } else {
  120. spin_unlock_irqrestore(&chip->open_lock, flags);
  121. }
  122. return 0;
  123. }
  124. static int snd_sb8dsp_midi_output_close(struct snd_rawmidi_substream *substream)
  125. {
  126. unsigned long flags;
  127. struct snd_sb *chip;
  128. chip = substream->rmidi->private_data;
  129. spin_lock_irqsave(&chip->open_lock, flags);
  130. chip->open &= ~(SB_OPEN_MIDI_OUTPUT | SB_OPEN_MIDI_OUTPUT_TRIGGER);
  131. chip->midi_substream_output = NULL;
  132. if (!(chip->open & SB_OPEN_MIDI_INPUT)) {
  133. spin_unlock_irqrestore(&chip->open_lock, flags);
  134. snd_sbdsp_reset(chip); /* reset DSP */
  135. } else {
  136. spin_unlock_irqrestore(&chip->open_lock, flags);
  137. }
  138. return 0;
  139. }
  140. static void snd_sb8dsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  141. {
  142. unsigned long flags;
  143. struct snd_sb *chip;
  144. chip = substream->rmidi->private_data;
  145. spin_lock_irqsave(&chip->open_lock, flags);
  146. if (up) {
  147. if (!(chip->open & SB_OPEN_MIDI_INPUT_TRIGGER)) {
  148. if (chip->hardware < SB_HW_20)
  149. snd_sbdsp_command(chip, SB_DSP_MIDI_INPUT_IRQ);
  150. chip->open |= SB_OPEN_MIDI_INPUT_TRIGGER;
  151. }
  152. } else {
  153. if (chip->open & SB_OPEN_MIDI_INPUT_TRIGGER) {
  154. if (chip->hardware < SB_HW_20)
  155. snd_sbdsp_command(chip, SB_DSP_MIDI_INPUT_IRQ);
  156. chip->open &= ~SB_OPEN_MIDI_INPUT_TRIGGER;
  157. }
  158. }
  159. spin_unlock_irqrestore(&chip->open_lock, flags);
  160. }
  161. static void snd_sb8dsp_midi_output_write(struct snd_rawmidi_substream *substream)
  162. {
  163. unsigned long flags;
  164. struct snd_sb *chip;
  165. char byte;
  166. int max = 32;
  167. /* how big is Tx FIFO? */
  168. chip = substream->rmidi->private_data;
  169. while (max-- > 0) {
  170. spin_lock_irqsave(&chip->open_lock, flags);
  171. if (snd_rawmidi_transmit_peek(substream, &byte, 1) != 1) {
  172. chip->open &= ~SB_OPEN_MIDI_OUTPUT_TRIGGER;
  173. del_timer(&chip->midi_timer);
  174. spin_unlock_irqrestore(&chip->open_lock, flags);
  175. break;
  176. }
  177. if (chip->hardware >= SB_HW_20) {
  178. int timeout = 8;
  179. while ((inb(SBP(chip, STATUS)) & 0x80) != 0 && --timeout > 0)
  180. ;
  181. if (timeout == 0) {
  182. /* Tx FIFO full - try again later */
  183. spin_unlock_irqrestore(&chip->open_lock, flags);
  184. break;
  185. }
  186. outb(byte, SBP(chip, WRITE));
  187. } else {
  188. snd_sbdsp_command(chip, SB_DSP_MIDI_OUTPUT);
  189. snd_sbdsp_command(chip, byte);
  190. }
  191. snd_rawmidi_transmit_ack(substream, 1);
  192. spin_unlock_irqrestore(&chip->open_lock, flags);
  193. }
  194. }
  195. static void snd_sb8dsp_midi_output_timer(unsigned long data)
  196. {
  197. struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *) data;
  198. struct snd_sb * chip = substream->rmidi->private_data;
  199. unsigned long flags;
  200. spin_lock_irqsave(&chip->open_lock, flags);
  201. chip->midi_timer.expires = 1 + jiffies;
  202. add_timer(&chip->midi_timer);
  203. spin_unlock_irqrestore(&chip->open_lock, flags);
  204. snd_sb8dsp_midi_output_write(substream);
  205. }
  206. static void snd_sb8dsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  207. {
  208. unsigned long flags;
  209. struct snd_sb *chip;
  210. chip = substream->rmidi->private_data;
  211. spin_lock_irqsave(&chip->open_lock, flags);
  212. if (up) {
  213. if (!(chip->open & SB_OPEN_MIDI_OUTPUT_TRIGGER)) {
  214. init_timer(&chip->midi_timer);
  215. chip->midi_timer.function = snd_sb8dsp_midi_output_timer;
  216. chip->midi_timer.data = (unsigned long) substream;
  217. chip->midi_timer.expires = 1 + jiffies;
  218. add_timer(&chip->midi_timer);
  219. chip->open |= SB_OPEN_MIDI_OUTPUT_TRIGGER;
  220. }
  221. } else {
  222. if (chip->open & SB_OPEN_MIDI_OUTPUT_TRIGGER) {
  223. chip->open &= ~SB_OPEN_MIDI_OUTPUT_TRIGGER;
  224. }
  225. }
  226. spin_unlock_irqrestore(&chip->open_lock, flags);
  227. if (up)
  228. snd_sb8dsp_midi_output_write(substream);
  229. }
  230. /*
  231. */
  232. static struct snd_rawmidi_ops snd_sb8dsp_midi_output =
  233. {
  234. .open = snd_sb8dsp_midi_output_open,
  235. .close = snd_sb8dsp_midi_output_close,
  236. .trigger = snd_sb8dsp_midi_output_trigger,
  237. };
  238. static struct snd_rawmidi_ops snd_sb8dsp_midi_input =
  239. {
  240. .open = snd_sb8dsp_midi_input_open,
  241. .close = snd_sb8dsp_midi_input_close,
  242. .trigger = snd_sb8dsp_midi_input_trigger,
  243. };
  244. int snd_sb8dsp_midi(struct snd_sb *chip, int device, struct snd_rawmidi ** rrawmidi)
  245. {
  246. struct snd_rawmidi *rmidi;
  247. int err;
  248. if (rrawmidi)
  249. *rrawmidi = NULL;
  250. if ((err = snd_rawmidi_new(chip->card, "SB8 MIDI", device, 1, 1, &rmidi)) < 0)
  251. return err;
  252. strcpy(rmidi->name, "SB8 MIDI");
  253. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_sb8dsp_midi_output);
  254. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_sb8dsp_midi_input);
  255. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT;
  256. if (chip->hardware >= SB_HW_20)
  257. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
  258. rmidi->private_data = chip;
  259. chip->rmidi = rmidi;
  260. if (rrawmidi)
  261. *rrawmidi = rmidi;
  262. return 0;
  263. }