gus_uart.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Routines for the GF1 MIDI interface - like UART 6850
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/time.h>
  24. #include <sound/core.h>
  25. #include <sound/gus.h>
  26. static void snd_gf1_interrupt_midi_in(struct snd_gus_card * gus)
  27. {
  28. int count;
  29. unsigned char stat, data, byte;
  30. unsigned long flags;
  31. count = 10;
  32. while (count) {
  33. spin_lock_irqsave(&gus->uart_cmd_lock, flags);
  34. stat = snd_gf1_uart_stat(gus);
  35. if (!(stat & 0x01)) { /* data in Rx FIFO? */
  36. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  37. count--;
  38. continue;
  39. }
  40. count = 100; /* arm counter to new value */
  41. data = snd_gf1_uart_get(gus);
  42. if (!(gus->gf1.uart_cmd & 0x80)) {
  43. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  44. continue;
  45. }
  46. if (stat & 0x10) { /* framing error */
  47. gus->gf1.uart_framing++;
  48. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  49. continue;
  50. }
  51. byte = snd_gf1_uart_get(gus);
  52. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  53. snd_rawmidi_receive(gus->midi_substream_input, &byte, 1);
  54. if (stat & 0x20) {
  55. gus->gf1.uart_overrun++;
  56. }
  57. }
  58. }
  59. static void snd_gf1_interrupt_midi_out(struct snd_gus_card * gus)
  60. {
  61. char byte;
  62. unsigned long flags;
  63. /* try unlock output */
  64. if (snd_gf1_uart_stat(gus) & 0x01)
  65. snd_gf1_interrupt_midi_in(gus);
  66. spin_lock_irqsave(&gus->uart_cmd_lock, flags);
  67. if (snd_gf1_uart_stat(gus) & 0x02) { /* Tx FIFO free? */
  68. if (snd_rawmidi_transmit(gus->midi_substream_output, &byte, 1) != 1) { /* no other bytes or error */
  69. snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd & ~0x20); /* disable Tx interrupt */
  70. } else {
  71. snd_gf1_uart_put(gus, byte);
  72. }
  73. }
  74. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  75. }
  76. static void snd_gf1_uart_reset(struct snd_gus_card * gus, int close)
  77. {
  78. snd_gf1_uart_cmd(gus, 0x03); /* reset */
  79. if (!close && gus->uart_enable) {
  80. udelay(160);
  81. snd_gf1_uart_cmd(gus, 0x00); /* normal operations */
  82. }
  83. }
  84. static int snd_gf1_uart_output_open(struct snd_rawmidi_substream *substream)
  85. {
  86. unsigned long flags;
  87. struct snd_gus_card *gus;
  88. gus = substream->rmidi->private_data;
  89. spin_lock_irqsave(&gus->uart_cmd_lock, flags);
  90. if (!(gus->gf1.uart_cmd & 0x80)) { /* input active? */
  91. snd_gf1_uart_reset(gus, 0);
  92. }
  93. gus->gf1.interrupt_handler_midi_out = snd_gf1_interrupt_midi_out;
  94. gus->midi_substream_output = substream;
  95. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  96. #if 0
  97. snd_printk(KERN_DEBUG "write init - cmd = 0x%x, stat = 0x%x\n", gus->gf1.uart_cmd, snd_gf1_uart_stat(gus));
  98. #endif
  99. return 0;
  100. }
  101. static int snd_gf1_uart_input_open(struct snd_rawmidi_substream *substream)
  102. {
  103. unsigned long flags;
  104. struct snd_gus_card *gus;
  105. int i;
  106. gus = substream->rmidi->private_data;
  107. spin_lock_irqsave(&gus->uart_cmd_lock, flags);
  108. if (gus->gf1.interrupt_handler_midi_out != snd_gf1_interrupt_midi_out) {
  109. snd_gf1_uart_reset(gus, 0);
  110. }
  111. gus->gf1.interrupt_handler_midi_in = snd_gf1_interrupt_midi_in;
  112. gus->midi_substream_input = substream;
  113. if (gus->uart_enable) {
  114. for (i = 0; i < 1000 && (snd_gf1_uart_stat(gus) & 0x01); i++)
  115. snd_gf1_uart_get(gus); /* clean Rx */
  116. if (i >= 1000)
  117. snd_printk(KERN_ERR "gus midi uart init read - cleanup error\n");
  118. }
  119. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  120. #if 0
  121. snd_printk(KERN_DEBUG
  122. "read init - enable = %i, cmd = 0x%x, stat = 0x%x\n",
  123. gus->uart_enable, gus->gf1.uart_cmd, snd_gf1_uart_stat(gus));
  124. snd_printk(KERN_DEBUG
  125. "[0x%x] reg (ctrl/status) = 0x%x, reg (data) = 0x%x "
  126. "(page = 0x%x)\n",
  127. gus->gf1.port + 0x100, inb(gus->gf1.port + 0x100),
  128. inb(gus->gf1.port + 0x101), inb(gus->gf1.port + 0x102));
  129. #endif
  130. return 0;
  131. }
  132. static int snd_gf1_uart_output_close(struct snd_rawmidi_substream *substream)
  133. {
  134. unsigned long flags;
  135. struct snd_gus_card *gus;
  136. gus = substream->rmidi->private_data;
  137. spin_lock_irqsave(&gus->uart_cmd_lock, flags);
  138. if (gus->gf1.interrupt_handler_midi_in != snd_gf1_interrupt_midi_in)
  139. snd_gf1_uart_reset(gus, 1);
  140. snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_MIDI_OUT);
  141. gus->midi_substream_output = NULL;
  142. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  143. return 0;
  144. }
  145. static int snd_gf1_uart_input_close(struct snd_rawmidi_substream *substream)
  146. {
  147. unsigned long flags;
  148. struct snd_gus_card *gus;
  149. gus = substream->rmidi->private_data;
  150. spin_lock_irqsave(&gus->uart_cmd_lock, flags);
  151. if (gus->gf1.interrupt_handler_midi_out != snd_gf1_interrupt_midi_out)
  152. snd_gf1_uart_reset(gus, 1);
  153. snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_MIDI_IN);
  154. gus->midi_substream_input = NULL;
  155. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  156. return 0;
  157. }
  158. static void snd_gf1_uart_input_trigger(struct snd_rawmidi_substream *substream, int up)
  159. {
  160. struct snd_gus_card *gus;
  161. unsigned long flags;
  162. gus = substream->rmidi->private_data;
  163. spin_lock_irqsave(&gus->uart_cmd_lock, flags);
  164. if (up) {
  165. if ((gus->gf1.uart_cmd & 0x80) == 0)
  166. snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd | 0x80); /* enable Rx interrupts */
  167. } else {
  168. if (gus->gf1.uart_cmd & 0x80)
  169. snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd & ~0x80); /* disable Rx interrupts */
  170. }
  171. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  172. }
  173. static void snd_gf1_uart_output_trigger(struct snd_rawmidi_substream *substream, int up)
  174. {
  175. unsigned long flags;
  176. struct snd_gus_card *gus;
  177. char byte;
  178. int timeout;
  179. gus = substream->rmidi->private_data;
  180. spin_lock_irqsave(&gus->uart_cmd_lock, flags);
  181. if (up) {
  182. if ((gus->gf1.uart_cmd & 0x20) == 0) {
  183. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  184. /* wait for empty Rx - Tx is probably unlocked */
  185. timeout = 10000;
  186. while (timeout-- > 0 && snd_gf1_uart_stat(gus) & 0x01);
  187. /* Tx FIFO free? */
  188. spin_lock_irqsave(&gus->uart_cmd_lock, flags);
  189. if (gus->gf1.uart_cmd & 0x20) {
  190. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  191. return;
  192. }
  193. if (snd_gf1_uart_stat(gus) & 0x02) {
  194. if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  195. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  196. return;
  197. }
  198. snd_gf1_uart_put(gus, byte);
  199. }
  200. snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd | 0x20); /* enable Tx interrupt */
  201. }
  202. } else {
  203. if (gus->gf1.uart_cmd & 0x20)
  204. snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd & ~0x20);
  205. }
  206. spin_unlock_irqrestore(&gus->uart_cmd_lock, flags);
  207. }
  208. static const struct snd_rawmidi_ops snd_gf1_uart_output =
  209. {
  210. .open = snd_gf1_uart_output_open,
  211. .close = snd_gf1_uart_output_close,
  212. .trigger = snd_gf1_uart_output_trigger,
  213. };
  214. static const struct snd_rawmidi_ops snd_gf1_uart_input =
  215. {
  216. .open = snd_gf1_uart_input_open,
  217. .close = snd_gf1_uart_input_close,
  218. .trigger = snd_gf1_uart_input_trigger,
  219. };
  220. int snd_gf1_rawmidi_new(struct snd_gus_card *gus, int device)
  221. {
  222. struct snd_rawmidi *rmidi;
  223. int err;
  224. if ((err = snd_rawmidi_new(gus->card, "GF1", device, 1, 1, &rmidi)) < 0)
  225. return err;
  226. strcpy(rmidi->name, gus->interwave ? "AMD InterWave" : "GF1");
  227. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_gf1_uart_output);
  228. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_gf1_uart_input);
  229. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
  230. rmidi->private_data = gus;
  231. gus->midi_uart = rmidi;
  232. return err;
  233. }