uart6850.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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/module.h>
  25. /* Mon Nov 22 22:38:35 MET 1993 marco@driq.home.usn.nl:
  26.  *      added 6850 support, used with COVOX SoundMaster II and custom cards.
  27.  */
  28. #include "sound_config.h"
  29. static int uart6850_base = 0x330;
  30. static int *uart6850_osp;
  31. #define DATAPORT   (uart6850_base)
  32. #define COMDPORT   (uart6850_base+1)
  33. #define STATPORT   (uart6850_base+1)
  34. static int uart6850_status(void)
  35. {
  36. return inb(STATPORT);
  37. }
  38. #define input_avail() (uart6850_status()&INPUT_AVAIL)
  39. #define output_ready() (uart6850_status()&OUTPUT_READY)
  40. static void uart6850_cmd(unsigned char cmd)
  41. {
  42. outb(cmd, COMDPORT);
  43. }
  44. static int uart6850_read(void)
  45. {
  46. return inb(DATAPORT);
  47. }
  48. static void uart6850_write(unsigned char byte)
  49. {
  50. outb(byte, DATAPORT);
  51. }
  52. #define OUTPUT_READY 0x02 /* Mask for data ready Bit */
  53. #define INPUT_AVAIL 0x01 /* Mask for Data Send Ready Bit */
  54. #define UART_RESET 0x95
  55. #define UART_MODE_ON 0x03
  56. static int uart6850_opened;
  57. static int uart6850_irq;
  58. static int uart6850_detected;
  59. static int my_dev;
  60. static void (*midi_input_intr) (int dev, unsigned char data);
  61. static void poll_uart6850(unsigned long dummy);
  62. static struct timer_list uart6850_timer = {
  63. function: poll_uart6850
  64. };
  65. static void uart6850_input_loop(void)
  66. {
  67. int count = 10;
  68. while (count)
  69. {
  70. /*
  71.  * Not timed out
  72.  */
  73. if (input_avail())
  74. {
  75. unsigned char c = uart6850_read();
  76. count = 100;
  77. if (uart6850_opened & OPEN_READ)
  78. midi_input_intr(my_dev, c);
  79. }
  80. else
  81. {
  82. while (!input_avail() && count)
  83. count--;
  84. }
  85. }
  86. }
  87. void m6850intr(int irq, void *dev_id, struct pt_regs *dummy)
  88. {
  89. if (input_avail())
  90. uart6850_input_loop();
  91. }
  92. /*
  93.  * It looks like there is no input interrupts in the UART mode. Let's try
  94.  * polling.
  95.  */
  96. static void poll_uart6850(unsigned long dummy)
  97. {
  98. unsigned long flags;
  99. if (!(uart6850_opened & OPEN_READ))
  100. return; /* Device has been closed */
  101. save_flags(flags);
  102. cli();
  103. if (input_avail())
  104. uart6850_input_loop();
  105. uart6850_timer.expires = 1 + jiffies;
  106. add_timer(&uart6850_timer);
  107. /*
  108.  * Come back later
  109.  */
  110. restore_flags(flags);
  111. }
  112. static int uart6850_open(int dev, int mode,
  113.       void            (*input) (int dev, unsigned char data),
  114.       void            (*output) (int dev)
  115. )
  116. {
  117. if (uart6850_opened)
  118. {
  119. /*   printk("Midi6850: Midi busyn");*/
  120.   return -EBUSY;
  121. };
  122. uart6850_cmd(UART_RESET);
  123. uart6850_input_loop();
  124. midi_input_intr = input;
  125. uart6850_opened = mode;
  126. poll_uart6850(0); /*
  127.  * Enable input polling
  128.  */
  129. return 0;
  130. }
  131. static void uart6850_close(int dev)
  132. {
  133. uart6850_cmd(UART_MODE_ON);
  134. del_timer(&uart6850_timer);
  135. uart6850_opened = 0;
  136. }
  137. static int uart6850_out(int dev, unsigned char midi_byte)
  138. {
  139. int timeout;
  140. unsigned long flags;
  141. /*
  142.  * Test for input since pending input seems to block the output.
  143.  */
  144. save_flags(flags);
  145. cli();
  146. if (input_avail())
  147. uart6850_input_loop();
  148. restore_flags(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: Timeoutn");
  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 detectedn");
  212. return;
  213. }
  214. uart6850_base = hw_config->io_base;
  215. uart6850_osp = hw_config->osp;
  216. uart6850_irq = hw_config->irq;
  217. save_flags(flags);
  218. cli();
  219. for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
  220.  * Wait
  221.  */
  222. uart6850_cmd(UART_MODE_ON);
  223. ok = 1;
  224. restore_flags(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_PARM(io,"i");
  259. MODULE_PARM(irq,"i");
  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");