sb_midi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * sound/oss/sb_midi.c
  3. *
  4. * The low level driver for the Sound Blaster DS chips.
  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. #include <linux/spinlock.h>
  14. #include <linux/slab.h>
  15. #include "sound_config.h"
  16. #include "sb.h"
  17. #undef SB_TEST_IRQ
  18. /*
  19. * The DSP channel can be used either for input or output. Variable
  20. * 'sb_irq_mode' will be set when the program calls read or write first time
  21. * after open. Current version doesn't support mode changes without closing
  22. * and reopening the device. Support for this feature may be implemented in a
  23. * future version of this driver.
  24. */
  25. static int sb_midi_open(int dev, int mode,
  26. void (*input) (int dev, unsigned char data),
  27. void (*output) (int dev)
  28. )
  29. {
  30. sb_devc *devc = midi_devs[dev]->devc;
  31. unsigned long flags;
  32. if (devc == NULL)
  33. return -ENXIO;
  34. spin_lock_irqsave(&devc->lock, flags);
  35. if (devc->opened)
  36. {
  37. spin_unlock_irqrestore(&devc->lock, flags);
  38. return -EBUSY;
  39. }
  40. devc->opened = 1;
  41. spin_unlock_irqrestore(&devc->lock, flags);
  42. devc->irq_mode = IMODE_MIDI;
  43. devc->midi_broken = 0;
  44. sb_dsp_reset(devc);
  45. if (!sb_dsp_command(devc, 0x35)) /* Start MIDI UART mode */
  46. {
  47. devc->opened = 0;
  48. return -EIO;
  49. }
  50. devc->intr_active = 1;
  51. if (mode & OPEN_READ)
  52. {
  53. devc->input_opened = 1;
  54. devc->midi_input_intr = input;
  55. }
  56. return 0;
  57. }
  58. static void sb_midi_close(int dev)
  59. {
  60. sb_devc *devc = midi_devs[dev]->devc;
  61. unsigned long flags;
  62. if (devc == NULL)
  63. return;
  64. spin_lock_irqsave(&devc->lock, flags);
  65. sb_dsp_reset(devc);
  66. devc->intr_active = 0;
  67. devc->input_opened = 0;
  68. devc->opened = 0;
  69. spin_unlock_irqrestore(&devc->lock, flags);
  70. }
  71. static int sb_midi_out(int dev, unsigned char midi_byte)
  72. {
  73. sb_devc *devc = midi_devs[dev]->devc;
  74. if (devc == NULL)
  75. return 1;
  76. if (devc->midi_broken)
  77. return 1;
  78. if (!sb_dsp_command(devc, midi_byte))
  79. {
  80. devc->midi_broken = 1;
  81. return 1;
  82. }
  83. return 1;
  84. }
  85. static int sb_midi_start_read(int dev)
  86. {
  87. return 0;
  88. }
  89. static int sb_midi_end_read(int dev)
  90. {
  91. sb_devc *devc = midi_devs[dev]->devc;
  92. if (devc == NULL)
  93. return -ENXIO;
  94. sb_dsp_reset(devc);
  95. devc->intr_active = 0;
  96. return 0;
  97. }
  98. static int sb_midi_ioctl(int dev, unsigned cmd, void __user *arg)
  99. {
  100. return -EINVAL;
  101. }
  102. void sb_midi_interrupt(sb_devc * devc)
  103. {
  104. unsigned long flags;
  105. unsigned char data;
  106. if (devc == NULL)
  107. return;
  108. spin_lock_irqsave(&devc->lock, flags);
  109. data = inb(DSP_READ);
  110. if (devc->input_opened)
  111. devc->midi_input_intr(devc->my_mididev, data);
  112. spin_unlock_irqrestore(&devc->lock, flags);
  113. }
  114. #define MIDI_SYNTH_NAME "Sound Blaster Midi"
  115. #define MIDI_SYNTH_CAPS 0
  116. #include "midi_synth.h"
  117. static struct midi_operations sb_midi_operations =
  118. {
  119. .owner = THIS_MODULE,
  120. .info = {"Sound Blaster", 0, 0, SNDCARD_SB},
  121. .converter = &std_midi_synth,
  122. .in_info = {0},
  123. .open = sb_midi_open,
  124. .close = sb_midi_close,
  125. .ioctl = sb_midi_ioctl,
  126. .outputc = sb_midi_out,
  127. .start_read = sb_midi_start_read,
  128. .end_read = sb_midi_end_read,
  129. };
  130. void sb_dsp_midi_init(sb_devc * devc, struct module *owner)
  131. {
  132. int dev;
  133. if (devc->model < 2) /* No MIDI support for SB 1.x */
  134. return;
  135. dev = sound_alloc_mididev();
  136. if (dev == -1)
  137. {
  138. printk(KERN_ERR "sb_midi: too many MIDI devices detected\n");
  139. return;
  140. }
  141. std_midi_synth.midi_dev = devc->my_mididev = dev;
  142. midi_devs[dev] = kmalloc(sizeof(struct midi_operations), GFP_KERNEL);
  143. if (midi_devs[dev] == NULL)
  144. {
  145. printk(KERN_WARNING "Sound Blaster: failed to allocate MIDI memory.\n");
  146. sound_unload_mididev(dev);
  147. return;
  148. }
  149. memcpy((char *) midi_devs[dev], (char *) &sb_midi_operations,
  150. sizeof(struct midi_operations));
  151. if (owner)
  152. midi_devs[dev]->owner = owner;
  153. midi_devs[dev]->devc = devc;
  154. midi_devs[dev]->converter = kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
  155. if (midi_devs[dev]->converter == NULL)
  156. {
  157. printk(KERN_WARNING "Sound Blaster: failed to allocate MIDI memory.\n");
  158. kfree(midi_devs[dev]);
  159. sound_unload_mididev(dev);
  160. return;
  161. }
  162. memcpy((char *) midi_devs[dev]->converter, (char *) &std_midi_synth,
  163. sizeof(struct synth_operations));
  164. midi_devs[dev]->converter->id = "SBMIDI";
  165. sequencer_init();
  166. }