sb8_midi.c 7.5 KB

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