pas2_midi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * sound/oss/pas2_midi.c
  3. *
  4. * The low level driver for the PAS Midi Interface.
  5. */
  6. /*
  7. * Copyright (C) by Hannu Savolainen 1993-1997
  8. *
  9. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  10. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  11. * for more info.
  12. *
  13. * Bartlomiej Zolnierkiewicz : Added __init to pas_init_mixer()
  14. */
  15. #include <linux/init.h>
  16. #include <linux/spinlock.h>
  17. #include "sound_config.h"
  18. #include "pas2.h"
  19. extern spinlock_t pas_lock;
  20. static int midi_busy, input_opened;
  21. static int my_dev;
  22. int pas2_mididev=-1;
  23. static unsigned char tmp_queue[256];
  24. static volatile int qlen;
  25. static volatile unsigned char qhead, qtail;
  26. static void (*midi_input_intr) (int dev, unsigned char data);
  27. static int pas_midi_open(int dev, int mode,
  28. void (*input) (int dev, unsigned char data),
  29. void (*output) (int dev)
  30. )
  31. {
  32. int err;
  33. unsigned long flags;
  34. unsigned char ctrl;
  35. if (midi_busy)
  36. return -EBUSY;
  37. /*
  38. * Reset input and output FIFO pointers
  39. */
  40. pas_write(0x20 | 0x40,
  41. 0x178b);
  42. spin_lock_irqsave(&pas_lock, flags);
  43. if ((err = pas_set_intr(0x10)) < 0)
  44. {
  45. spin_unlock_irqrestore(&pas_lock, flags);
  46. return err;
  47. }
  48. /*
  49. * Enable input available and output FIFO empty interrupts
  50. */
  51. ctrl = 0;
  52. input_opened = 0;
  53. midi_input_intr = input;
  54. if (mode == OPEN_READ || mode == OPEN_READWRITE)
  55. {
  56. ctrl |= 0x04; /* Enable input */
  57. input_opened = 1;
  58. }
  59. if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
  60. {
  61. ctrl |= 0x08 | 0x10; /* Enable output */
  62. }
  63. pas_write(ctrl, 0x178b);
  64. /*
  65. * Acknowledge any pending interrupts
  66. */
  67. pas_write(0xff, 0x1B88);
  68. spin_unlock_irqrestore(&pas_lock, flags);
  69. midi_busy = 1;
  70. qlen = qhead = qtail = 0;
  71. return 0;
  72. }
  73. static void pas_midi_close(int dev)
  74. {
  75. /*
  76. * Reset FIFO pointers, disable intrs
  77. */
  78. pas_write(0x20 | 0x40, 0x178b);
  79. pas_remove_intr(0x10);
  80. midi_busy = 0;
  81. }
  82. static int dump_to_midi(unsigned char midi_byte)
  83. {
  84. int fifo_space, x;
  85. fifo_space = ((x = pas_read(0x1B89)) >> 4) & 0x0f;
  86. /*
  87. * The MIDI FIFO space register and it's documentation is nonunderstandable.
  88. * There seem to be no way to differentiate between buffer full and buffer
  89. * empty situations. For this reason we don't never write the buffer
  90. * completely full. In this way we can assume that 0 (or is it 15)
  91. * means that the buffer is empty.
  92. */
  93. if (fifo_space < 2 && fifo_space != 0) /* Full (almost) */
  94. return 0; /* Ask upper layers to retry after some time */
  95. pas_write(midi_byte, 0x178A);
  96. return 1;
  97. }
  98. static int pas_midi_out(int dev, unsigned char midi_byte)
  99. {
  100. unsigned long flags;
  101. /*
  102. * Drain the local queue first
  103. */
  104. spin_lock_irqsave(&pas_lock, flags);
  105. while (qlen && dump_to_midi(tmp_queue[qhead]))
  106. {
  107. qlen--;
  108. qhead++;
  109. }
  110. spin_unlock_irqrestore(&pas_lock, flags);
  111. /*
  112. * Output the byte if the local queue is empty.
  113. */
  114. if (!qlen)
  115. if (dump_to_midi(midi_byte))
  116. return 1;
  117. /*
  118. * Put to the local queue
  119. */
  120. if (qlen >= 256)
  121. return 0; /* Local queue full */
  122. spin_lock_irqsave(&pas_lock, flags);
  123. tmp_queue[qtail] = midi_byte;
  124. qlen++;
  125. qtail++;
  126. spin_unlock_irqrestore(&pas_lock, flags);
  127. return 1;
  128. }
  129. static int pas_midi_start_read(int dev)
  130. {
  131. return 0;
  132. }
  133. static int pas_midi_end_read(int dev)
  134. {
  135. return 0;
  136. }
  137. static void pas_midi_kick(int dev)
  138. {
  139. }
  140. static int pas_buffer_status(int dev)
  141. {
  142. return qlen;
  143. }
  144. #define MIDI_SYNTH_NAME "Pro Audio Spectrum Midi"
  145. #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
  146. #include "midi_synth.h"
  147. static struct midi_operations pas_midi_operations =
  148. {
  149. .owner = THIS_MODULE,
  150. .info = {"Pro Audio Spectrum", 0, 0, SNDCARD_PAS},
  151. .converter = &std_midi_synth,
  152. .in_info = {0},
  153. .open = pas_midi_open,
  154. .close = pas_midi_close,
  155. .outputc = pas_midi_out,
  156. .start_read = pas_midi_start_read,
  157. .end_read = pas_midi_end_read,
  158. .kick = pas_midi_kick,
  159. .buffer_status = pas_buffer_status,
  160. };
  161. void __init pas_midi_init(void)
  162. {
  163. int dev = sound_alloc_mididev();
  164. if (dev == -1)
  165. {
  166. printk(KERN_WARNING "pas_midi_init: Too many midi devices detected\n");
  167. return;
  168. }
  169. std_midi_synth.midi_dev = my_dev = dev;
  170. midi_devs[dev] = &pas_midi_operations;
  171. pas2_mididev = dev;
  172. sequencer_init();
  173. }
  174. void pas_midi_interrupt(void)
  175. {
  176. unsigned char stat;
  177. int i, incount;
  178. stat = pas_read(0x1B88);
  179. if (stat & 0x04) /* Input data available */
  180. {
  181. incount = pas_read(0x1B89) & 0x0f; /* Input FIFO size */
  182. if (!incount)
  183. incount = 16;
  184. for (i = 0; i < incount; i++)
  185. if (input_opened)
  186. {
  187. midi_input_intr(my_dev, pas_read(0x178A));
  188. } else
  189. pas_read(0x178A); /* Flush */
  190. }
  191. if (stat & (0x08 | 0x10))
  192. {
  193. spin_lock(&pas_lock);/* called in irq context */
  194. while (qlen && dump_to_midi(tmp_queue[qhead]))
  195. {
  196. qlen--;
  197. qhead++;
  198. }
  199. spin_unlock(&pas_lock);
  200. }
  201. if (stat & 0x40)
  202. {
  203. printk(KERN_WARNING "MIDI output overrun %x,%x\n", pas_read(0x1B89), stat);
  204. }
  205. pas_write(stat, 0x1B88); /* Acknowledge interrupts */
  206. }