uart6850.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * sound/oss/uart6850.c
  3. *
  4. *
  5. * Copyright (C) by Hannu Savolainen 1993-1997
  6. *
  7. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  8. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  9. * for more info.
  10. * Extended by Alan Cox for Red Hat Software. Now a loadable MIDI driver.
  11. * 28/4/97 - (C) Copyright Alan Cox. Released under the GPL version 2.
  12. *
  13. * Alan Cox: Updated for new modular code. Removed snd_* irq handling. Now
  14. * uses native linux resources
  15. * Christoph Hellwig: Adapted to module_init/module_exit
  16. * Jeff Garzik: Made it work again, in theory
  17. * FIXME: If the request_irq() succeeds, the probe succeeds. Ug.
  18. *
  19. * Status: Testing required (no shit -jgarzik)
  20. *
  21. *
  22. */
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/spinlock.h>
  27. /* Mon Nov 22 22:38:35 MET 1993 marco@driq.home.usn.nl:
  28. * added 6850 support, used with COVOX SoundMaster II and custom cards.
  29. */
  30. #include "sound_config.h"
  31. static int uart6850_base = 0x330;
  32. static int *uart6850_osp;
  33. #define DATAPORT (uart6850_base)
  34. #define COMDPORT (uart6850_base+1)
  35. #define STATPORT (uart6850_base+1)
  36. static int uart6850_status(void)
  37. {
  38. return inb(STATPORT);
  39. }
  40. #define input_avail() (uart6850_status()&INPUT_AVAIL)
  41. #define output_ready() (uart6850_status()&OUTPUT_READY)
  42. static void uart6850_cmd(unsigned char cmd)
  43. {
  44. outb(cmd, COMDPORT);
  45. }
  46. static int uart6850_read(void)
  47. {
  48. return inb(DATAPORT);
  49. }
  50. static void uart6850_write(unsigned char byte)
  51. {
  52. outb(byte, DATAPORT);
  53. }
  54. #define OUTPUT_READY 0x02 /* Mask for data ready Bit */
  55. #define INPUT_AVAIL 0x01 /* Mask for Data Send Ready Bit */
  56. #define UART_RESET 0x95
  57. #define UART_MODE_ON 0x03
  58. static int uart6850_opened;
  59. static int uart6850_irq;
  60. static int uart6850_detected;
  61. static int my_dev;
  62. static DEFINE_SPINLOCK(lock);
  63. static void (*midi_input_intr) (int dev, unsigned char data);
  64. static void poll_uart6850(unsigned long dummy);
  65. static DEFINE_TIMER(uart6850_timer, poll_uart6850, 0, 0);
  66. static void uart6850_input_loop(void)
  67. {
  68. int count = 10;
  69. while (count)
  70. {
  71. /*
  72. * Not timed out
  73. */
  74. if (input_avail())
  75. {
  76. unsigned char c = uart6850_read();
  77. count = 100;
  78. if (uart6850_opened & OPEN_READ)
  79. midi_input_intr(my_dev, c);
  80. }
  81. else
  82. {
  83. while (!input_avail() && count)
  84. count--;
  85. }
  86. }
  87. }
  88. static irqreturn_t m6850intr(int irq, void *dev_id)
  89. {
  90. if (input_avail())
  91. uart6850_input_loop();
  92. return IRQ_HANDLED;
  93. }
  94. /*
  95. * It looks like there is no input interrupts in the UART mode. Let's try
  96. * polling.
  97. */
  98. static void poll_uart6850(unsigned long dummy)
  99. {
  100. unsigned long flags;
  101. if (!(uart6850_opened & OPEN_READ))
  102. return; /* Device has been closed */
  103. spin_lock_irqsave(&lock,flags);
  104. if (input_avail())
  105. uart6850_input_loop();
  106. uart6850_timer.expires = 1 + jiffies;
  107. add_timer(&uart6850_timer);
  108. /*
  109. * Come back later
  110. */
  111. spin_unlock_irqrestore(&lock,flags);
  112. }
  113. static int uart6850_open(int dev, int mode,
  114. void (*input) (int dev, unsigned char data),
  115. void (*output) (int dev)
  116. )
  117. {
  118. if (uart6850_opened)
  119. {
  120. /* printk("Midi6850: Midi busy\n");*/
  121. return -EBUSY;
  122. }
  123. uart6850_cmd(UART_RESET);
  124. uart6850_input_loop();
  125. midi_input_intr = input;
  126. uart6850_opened = mode;
  127. poll_uart6850(0); /*
  128. * Enable input polling
  129. */
  130. return 0;
  131. }
  132. static void uart6850_close(int dev)
  133. {
  134. uart6850_cmd(UART_MODE_ON);
  135. del_timer(&uart6850_timer);
  136. uart6850_opened = 0;
  137. }
  138. static int uart6850_out(int dev, unsigned char midi_byte)
  139. {
  140. int timeout;
  141. unsigned long flags;
  142. /*
  143. * Test for input since pending input seems to block the output.
  144. */
  145. spin_lock_irqsave(&lock,flags);
  146. if (input_avail())
  147. uart6850_input_loop();
  148. spin_unlock_irqrestore(&lock,flags);
  149. /*
  150. * Sometimes it takes about 13000 loops before the output becomes ready
  151. * (After reset). Normally it takes just about 10 loops.
  152. */
  153. for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
  154. * Wait
  155. */
  156. if (!output_ready())
  157. {
  158. printk(KERN_WARNING "Midi6850: Timeout\n");
  159. return 0;
  160. }
  161. uart6850_write(midi_byte);
  162. return 1;
  163. }
  164. static inline int uart6850_command(int dev, unsigned char *midi_byte)
  165. {
  166. return 1;
  167. }
  168. static inline int uart6850_start_read(int dev)
  169. {
  170. return 0;
  171. }
  172. static inline int uart6850_end_read(int dev)
  173. {
  174. return 0;
  175. }
  176. static inline void uart6850_kick(int dev)
  177. {
  178. }
  179. static inline int uart6850_buffer_status(int dev)
  180. {
  181. return 0; /*
  182. * No data in buffers
  183. */
  184. }
  185. #define MIDI_SYNTH_NAME "6850 UART Midi"
  186. #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
  187. #include "midi_synth.h"
  188. static struct midi_operations uart6850_operations =
  189. {
  190. .owner = THIS_MODULE,
  191. .info = {"6850 UART", 0, 0, SNDCARD_UART6850},
  192. .converter = &std_midi_synth,
  193. .in_info = {0},
  194. .open = uart6850_open,
  195. .close = uart6850_close,
  196. .outputc = uart6850_out,
  197. .start_read = uart6850_start_read,
  198. .end_read = uart6850_end_read,
  199. .kick = uart6850_kick,
  200. .command = uart6850_command,
  201. .buffer_status = uart6850_buffer_status
  202. };
  203. static void __init attach_uart6850(struct address_info *hw_config)
  204. {
  205. int ok, timeout;
  206. unsigned long flags;
  207. if (!uart6850_detected)
  208. return;
  209. if ((my_dev = sound_alloc_mididev()) == -1)
  210. {
  211. printk(KERN_INFO "uart6850: Too many midi devices detected\n");
  212. return;
  213. }
  214. uart6850_base = hw_config->io_base;
  215. uart6850_osp = hw_config->osp;
  216. uart6850_irq = hw_config->irq;
  217. spin_lock_irqsave(&lock,flags);
  218. for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
  219. * Wait
  220. */
  221. uart6850_cmd(UART_MODE_ON);
  222. ok = 1;
  223. spin_unlock_irqrestore(&lock,flags);
  224. conf_printf("6850 Midi Interface", hw_config);
  225. std_midi_synth.midi_dev = my_dev;
  226. hw_config->slots[4] = my_dev;
  227. midi_devs[my_dev] = &uart6850_operations;
  228. sequencer_init();
  229. }
  230. static inline int reset_uart6850(void)
  231. {
  232. uart6850_read();
  233. return 1; /*
  234. * OK
  235. */
  236. }
  237. static int __init probe_uart6850(struct address_info *hw_config)
  238. {
  239. int ok;
  240. uart6850_osp = hw_config->osp;
  241. uart6850_base = hw_config->io_base;
  242. uart6850_irq = hw_config->irq;
  243. if (request_irq(uart6850_irq, m6850intr, 0, "MIDI6850", NULL) < 0)
  244. return 0;
  245. ok = reset_uart6850();
  246. uart6850_detected = ok;
  247. return ok;
  248. }
  249. static void __exit unload_uart6850(struct address_info *hw_config)
  250. {
  251. free_irq(hw_config->irq, NULL);
  252. sound_unload_mididev(hw_config->slots[4]);
  253. }
  254. static struct address_info cfg_mpu;
  255. static int __initdata io = -1;
  256. static int __initdata irq = -1;
  257. module_param(io, int, 0);
  258. module_param(irq, int, 0);
  259. static int __init init_uart6850(void)
  260. {
  261. cfg_mpu.io_base = io;
  262. cfg_mpu.irq = irq;
  263. if (cfg_mpu.io_base == -1 || cfg_mpu.irq == -1) {
  264. printk(KERN_INFO "uart6850: irq and io must be set.\n");
  265. return -EINVAL;
  266. }
  267. if (probe_uart6850(&cfg_mpu))
  268. return -ENODEV;
  269. attach_uart6850(&cfg_mpu);
  270. return 0;
  271. }
  272. static void __exit cleanup_uart6850(void)
  273. {
  274. unload_uart6850(&cfg_mpu);
  275. }
  276. module_init(init_uart6850);
  277. module_exit(cleanup_uart6850);
  278. #ifndef MODULE
  279. static int __init setup_uart6850(char *str)
  280. {
  281. /* io, irq */
  282. int ints[3];
  283. str = get_options(str, ARRAY_SIZE(ints), ints);
  284. io = ints[1];
  285. irq = ints[2];
  286. return 1;
  287. }
  288. __setup("uart6850=", setup_uart6850);
  289. #endif
  290. MODULE_LICENSE("GPL");