uart6850.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * sound/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 struct timer_list uart6850_timer =
  66. TIMER_INITIALIZER(poll_uart6850, 0, 0);
  67. static void uart6850_input_loop(void)
  68. {
  69. int count = 10;
  70. while (count)
  71. {
  72. /*
  73. * Not timed out
  74. */
  75. if (input_avail())
  76. {
  77. unsigned char c = uart6850_read();
  78. count = 100;
  79. if (uart6850_opened & OPEN_READ)
  80. midi_input_intr(my_dev, c);
  81. }
  82. else
  83. {
  84. while (!input_avail() && count)
  85. count--;
  86. }
  87. }
  88. }
  89. static irqreturn_t m6850intr(int irq, void *dev_id, struct pt_regs *dummy)
  90. {
  91. if (input_avail())
  92. uart6850_input_loop();
  93. return IRQ_HANDLED;
  94. }
  95. /*
  96. * It looks like there is no input interrupts in the UART mode. Let's try
  97. * polling.
  98. */
  99. static void poll_uart6850(unsigned long dummy)
  100. {
  101. unsigned long flags;
  102. if (!(uart6850_opened & OPEN_READ))
  103. return; /* Device has been closed */
  104. spin_lock_irqsave(&lock,flags);
  105. if (input_avail())
  106. uart6850_input_loop();
  107. uart6850_timer.expires = 1 + jiffies;
  108. add_timer(&uart6850_timer);
  109. /*
  110. * Come back later
  111. */
  112. spin_unlock_irqrestore(&lock,flags);
  113. }
  114. static int uart6850_open(int dev, int mode,
  115. void (*input) (int dev, unsigned char data),
  116. void (*output) (int dev)
  117. )
  118. {
  119. if (uart6850_opened)
  120. {
  121. /* printk("Midi6850: Midi busy\n");*/
  122. return -EBUSY;
  123. };
  124. uart6850_cmd(UART_RESET);
  125. uart6850_input_loop();
  126. midi_input_intr = input;
  127. uart6850_opened = mode;
  128. poll_uart6850(0); /*
  129. * Enable input polling
  130. */
  131. return 0;
  132. }
  133. static void uart6850_close(int dev)
  134. {
  135. uart6850_cmd(UART_MODE_ON);
  136. del_timer(&uart6850_timer);
  137. uart6850_opened = 0;
  138. }
  139. static int uart6850_out(int dev, unsigned char midi_byte)
  140. {
  141. int timeout;
  142. unsigned long flags;
  143. /*
  144. * Test for input since pending input seems to block the output.
  145. */
  146. spin_lock_irqsave(&lock,flags);
  147. if (input_avail())
  148. uart6850_input_loop();
  149. spin_unlock_irqrestore(&lock,flags);
  150. /*
  151. * Sometimes it takes about 13000 loops before the output becomes ready
  152. * (After reset). Normally it takes just about 10 loops.
  153. */
  154. for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
  155. * Wait
  156. */
  157. if (!output_ready())
  158. {
  159. printk(KERN_WARNING "Midi6850: Timeout\n");
  160. return 0;
  161. }
  162. uart6850_write(midi_byte);
  163. return 1;
  164. }
  165. static inline int uart6850_command(int dev, unsigned char *midi_byte)
  166. {
  167. return 1;
  168. }
  169. static inline int uart6850_start_read(int dev)
  170. {
  171. return 0;
  172. }
  173. static inline int uart6850_end_read(int dev)
  174. {
  175. return 0;
  176. }
  177. static inline void uart6850_kick(int dev)
  178. {
  179. }
  180. static inline int uart6850_buffer_status(int dev)
  181. {
  182. return 0; /*
  183. * No data in buffers
  184. */
  185. }
  186. #define MIDI_SYNTH_NAME "6850 UART Midi"
  187. #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
  188. #include "midi_synth.h"
  189. static struct midi_operations uart6850_operations =
  190. {
  191. .owner = THIS_MODULE,
  192. .info = {"6850 UART", 0, 0, SNDCARD_UART6850},
  193. .converter = &std_midi_synth,
  194. .in_info = {0},
  195. .open = uart6850_open,
  196. .close = uart6850_close,
  197. .outputc = uart6850_out,
  198. .start_read = uart6850_start_read,
  199. .end_read = uart6850_end_read,
  200. .kick = uart6850_kick,
  201. .command = uart6850_command,
  202. .buffer_status = uart6850_buffer_status
  203. };
  204. static void __init attach_uart6850(struct address_info *hw_config)
  205. {
  206. int ok, timeout;
  207. unsigned long flags;
  208. if (!uart6850_detected)
  209. return;
  210. if ((my_dev = sound_alloc_mididev()) == -1)
  211. {
  212. printk(KERN_INFO "uart6850: Too many midi devices detected\n");
  213. return;
  214. }
  215. uart6850_base = hw_config->io_base;
  216. uart6850_osp = hw_config->osp;
  217. uart6850_irq = hw_config->irq;
  218. spin_lock_irqsave(&lock,flags);
  219. for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
  220. * Wait
  221. */
  222. uart6850_cmd(UART_MODE_ON);
  223. ok = 1;
  224. spin_unlock_irqrestore(&lock,flags);
  225. conf_printf("6850 Midi Interface", hw_config);
  226. std_midi_synth.midi_dev = my_dev;
  227. hw_config->slots[4] = my_dev;
  228. midi_devs[my_dev] = &uart6850_operations;
  229. sequencer_init();
  230. }
  231. static inline int reset_uart6850(void)
  232. {
  233. uart6850_read();
  234. return 1; /*
  235. * OK
  236. */
  237. }
  238. static int __init probe_uart6850(struct address_info *hw_config)
  239. {
  240. int ok;
  241. uart6850_osp = hw_config->osp;
  242. uart6850_base = hw_config->io_base;
  243. uart6850_irq = hw_config->irq;
  244. if (request_irq(uart6850_irq, m6850intr, 0, "MIDI6850", NULL) < 0)
  245. return 0;
  246. ok = reset_uart6850();
  247. uart6850_detected = ok;
  248. return ok;
  249. }
  250. static void __exit unload_uart6850(struct address_info *hw_config)
  251. {
  252. free_irq(hw_config->irq, NULL);
  253. sound_unload_mididev(hw_config->slots[4]);
  254. }
  255. static struct address_info cfg_mpu;
  256. static int __initdata io = -1;
  257. static int __initdata irq = -1;
  258. module_param(io, int, 0);
  259. module_param(irq, int, 0);
  260. static int __init init_uart6850(void)
  261. {
  262. cfg_mpu.io_base = io;
  263. cfg_mpu.irq = irq;
  264. if (cfg_mpu.io_base == -1 || cfg_mpu.irq == -1) {
  265. printk(KERN_INFO "uart6850: irq and io must be set.\n");
  266. return -EINVAL;
  267. }
  268. if (probe_uart6850(&cfg_mpu))
  269. return -ENODEV;
  270. attach_uart6850(&cfg_mpu);
  271. return 0;
  272. }
  273. static void __exit cleanup_uart6850(void)
  274. {
  275. unload_uart6850(&cfg_mpu);
  276. }
  277. module_init(init_uart6850);
  278. module_exit(cleanup_uart6850);
  279. #ifndef MODULE
  280. static int __init setup_uart6850(char *str)
  281. {
  282. /* io, irq */
  283. int ints[3];
  284. str = get_options(str, ARRAY_SIZE(ints), ints);
  285. io = ints[1];
  286. irq = ints[2];
  287. return 1;
  288. }
  289. __setup("uart6850=", setup_uart6850);
  290. #endif
  291. MODULE_LICENSE("GPL");