uart401.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * sound/oss/uart401.c
  3. *
  4. * MPU-401 UART driver (formerly uart401_midi.c)
  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. * Changes:
  14. * Alan Cox Reformatted, removed sound_mem usage, use normal Linux
  15. * interrupt allocation. Protect against bogus unload
  16. * Fixed to allow IRQ > 15
  17. * Christoph Hellwig Adapted to module_init/module_exit
  18. * Arnaldo C. de Melo got rid of check_region
  19. *
  20. * Status:
  21. * Untested
  22. */
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include "sound_config.h"
  29. #include "mpu401.h"
  30. struct uart401_devc
  31. {
  32. int base;
  33. int irq;
  34. int *osp;
  35. void (*midi_input_intr) (int dev, unsigned char data);
  36. int opened, disabled;
  37. volatile unsigned char input_byte;
  38. int my_dev;
  39. int share_irq;
  40. spinlock_t lock;
  41. };
  42. #define DATAPORT (devc->base)
  43. #define COMDPORT (devc->base+1)
  44. #define STATPORT (devc->base+1)
  45. static int uart401_status(struct uart401_devc *devc)
  46. {
  47. return inb(STATPORT);
  48. }
  49. #define input_avail(devc) (!(uart401_status(devc)&INPUT_AVAIL))
  50. #define output_ready(devc) (!(uart401_status(devc)&OUTPUT_READY))
  51. static void uart401_cmd(struct uart401_devc *devc, unsigned char cmd)
  52. {
  53. outb((cmd), COMDPORT);
  54. }
  55. static int uart401_read(struct uart401_devc *devc)
  56. {
  57. return inb(DATAPORT);
  58. }
  59. static void uart401_write(struct uart401_devc *devc, unsigned char byte)
  60. {
  61. outb((byte), DATAPORT);
  62. }
  63. #define OUTPUT_READY 0x40
  64. #define INPUT_AVAIL 0x80
  65. #define MPU_ACK 0xFE
  66. #define MPU_RESET 0xFF
  67. #define UART_MODE_ON 0x3F
  68. static int reset_uart401(struct uart401_devc *devc);
  69. static void enter_uart_mode(struct uart401_devc *devc);
  70. static void uart401_input_loop(struct uart401_devc *devc)
  71. {
  72. int work_limit=30000;
  73. while (input_avail(devc) && --work_limit)
  74. {
  75. unsigned char c = uart401_read(devc);
  76. if (c == MPU_ACK)
  77. devc->input_byte = c;
  78. else if (devc->opened & OPEN_READ && devc->midi_input_intr)
  79. devc->midi_input_intr(devc->my_dev, c);
  80. }
  81. if(work_limit==0)
  82. printk(KERN_WARNING "Too much work in interrupt on uart401 (0x%X). UART jabbering ??\n", devc->base);
  83. }
  84. irqreturn_t uart401intr(int irq, void *dev_id)
  85. {
  86. struct uart401_devc *devc = dev_id;
  87. if (devc == NULL)
  88. {
  89. printk(KERN_ERR "uart401: bad devc\n");
  90. return IRQ_NONE;
  91. }
  92. if (input_avail(devc))
  93. uart401_input_loop(devc);
  94. return IRQ_HANDLED;
  95. }
  96. static int
  97. uart401_open(int dev, int mode,
  98. void (*input) (int dev, unsigned char data),
  99. void (*output) (int dev)
  100. )
  101. {
  102. struct uart401_devc *devc = (struct uart401_devc *)
  103. midi_devs[dev]->devc;
  104. if (devc->opened)
  105. return -EBUSY;
  106. /* Flush the UART */
  107. while (input_avail(devc))
  108. uart401_read(devc);
  109. devc->midi_input_intr = input;
  110. devc->opened = mode;
  111. enter_uart_mode(devc);
  112. devc->disabled = 0;
  113. return 0;
  114. }
  115. static void uart401_close(int dev)
  116. {
  117. struct uart401_devc *devc = (struct uart401_devc *)
  118. midi_devs[dev]->devc;
  119. reset_uart401(devc);
  120. devc->opened = 0;
  121. }
  122. static int uart401_out(int dev, unsigned char midi_byte)
  123. {
  124. int timeout;
  125. unsigned long flags;
  126. struct uart401_devc *devc = (struct uart401_devc *)
  127. midi_devs[dev]->devc;
  128. if (devc->disabled)
  129. return 1;
  130. /*
  131. * Test for input since pending input seems to block the output.
  132. */
  133. spin_lock_irqsave(&devc->lock,flags);
  134. if (input_avail(devc))
  135. uart401_input_loop(devc);
  136. spin_unlock_irqrestore(&devc->lock,flags);
  137. /*
  138. * Sometimes it takes about 13000 loops before the output becomes ready
  139. * (After reset). Normally it takes just about 10 loops.
  140. */
  141. for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
  142. if (!output_ready(devc))
  143. {
  144. printk(KERN_WARNING "uart401: Timeout - Device not responding\n");
  145. devc->disabled = 1;
  146. reset_uart401(devc);
  147. enter_uart_mode(devc);
  148. return 1;
  149. }
  150. uart401_write(devc, midi_byte);
  151. return 1;
  152. }
  153. static inline int uart401_start_read(int dev)
  154. {
  155. return 0;
  156. }
  157. static inline int uart401_end_read(int dev)
  158. {
  159. return 0;
  160. }
  161. static inline void uart401_kick(int dev)
  162. {
  163. }
  164. static inline int uart401_buffer_status(int dev)
  165. {
  166. return 0;
  167. }
  168. #define MIDI_SYNTH_NAME "MPU-401 UART"
  169. #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
  170. #include "midi_synth.h"
  171. static const struct midi_operations uart401_operations =
  172. {
  173. .owner = THIS_MODULE,
  174. .info = {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401},
  175. .converter = &std_midi_synth,
  176. .in_info = {0},
  177. .open = uart401_open,
  178. .close = uart401_close,
  179. .outputc = uart401_out,
  180. .start_read = uart401_start_read,
  181. .end_read = uart401_end_read,
  182. .kick = uart401_kick,
  183. .buffer_status = uart401_buffer_status,
  184. };
  185. static void enter_uart_mode(struct uart401_devc *devc)
  186. {
  187. int ok, timeout;
  188. unsigned long flags;
  189. spin_lock_irqsave(&devc->lock,flags);
  190. for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
  191. devc->input_byte = 0;
  192. uart401_cmd(devc, UART_MODE_ON);
  193. ok = 0;
  194. for (timeout = 50000; timeout > 0 && !ok; timeout--)
  195. if (devc->input_byte == MPU_ACK)
  196. ok = 1;
  197. else if (input_avail(devc))
  198. if (uart401_read(devc) == MPU_ACK)
  199. ok = 1;
  200. spin_unlock_irqrestore(&devc->lock,flags);
  201. }
  202. static int reset_uart401(struct uart401_devc *devc)
  203. {
  204. int ok, timeout, n;
  205. /*
  206. * Send the RESET command. Try again if no success at the first time.
  207. */
  208. ok = 0;
  209. for (n = 0; n < 2 && !ok; n++)
  210. {
  211. for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
  212. devc->input_byte = 0;
  213. uart401_cmd(devc, MPU_RESET);
  214. /*
  215. * Wait at least 25 msec. This method is not accurate so let's make the
  216. * loop bit longer. Cannot sleep since this is called during boot.
  217. */
  218. for (timeout = 50000; timeout > 0 && !ok; timeout--)
  219. {
  220. if (devc->input_byte == MPU_ACK) /* Interrupt */
  221. ok = 1;
  222. else if (input_avail(devc))
  223. {
  224. if (uart401_read(devc) == MPU_ACK)
  225. ok = 1;
  226. }
  227. }
  228. }
  229. /* Flush input before enabling interrupts */
  230. if (ok)
  231. uart401_input_loop(devc);
  232. else
  233. DDB(printk("Reset UART401 failed - No hardware detected.\n"));
  234. return ok;
  235. }
  236. int probe_uart401(struct address_info *hw_config, struct module *owner)
  237. {
  238. struct uart401_devc *devc;
  239. char *name = "MPU-401 (UART) MIDI";
  240. int ok = 0;
  241. unsigned long flags;
  242. DDB(printk("Entered probe_uart401()\n"));
  243. /* Default to "not found" */
  244. hw_config->slots[4] = -1;
  245. if (!request_region(hw_config->io_base, 4, "MPU-401 UART")) {
  246. printk(KERN_INFO "uart401: could not request_region(%d, 4)\n", hw_config->io_base);
  247. return 0;
  248. }
  249. devc = kmalloc(sizeof(struct uart401_devc), GFP_KERNEL);
  250. if (!devc) {
  251. printk(KERN_WARNING "uart401: Can't allocate memory\n");
  252. goto cleanup_region;
  253. }
  254. devc->base = hw_config->io_base;
  255. devc->irq = hw_config->irq;
  256. devc->osp = hw_config->osp;
  257. devc->midi_input_intr = NULL;
  258. devc->opened = 0;
  259. devc->input_byte = 0;
  260. devc->my_dev = 0;
  261. devc->share_irq = 0;
  262. spin_lock_init(&devc->lock);
  263. spin_lock_irqsave(&devc->lock,flags);
  264. ok = reset_uart401(devc);
  265. spin_unlock_irqrestore(&devc->lock,flags);
  266. if (!ok)
  267. goto cleanup_devc;
  268. if (hw_config->name)
  269. name = hw_config->name;
  270. if (devc->irq < 0) {
  271. devc->share_irq = 1;
  272. devc->irq *= -1;
  273. } else
  274. devc->share_irq = 0;
  275. if (!devc->share_irq)
  276. if (request_irq(devc->irq, uart401intr, 0, "MPU-401 UART", devc) < 0) {
  277. printk(KERN_WARNING "uart401: Failed to allocate IRQ%d\n", devc->irq);
  278. devc->share_irq = 1;
  279. }
  280. devc->my_dev = sound_alloc_mididev();
  281. enter_uart_mode(devc);
  282. if (devc->my_dev == -1) {
  283. printk(KERN_INFO "uart401: Too many midi devices detected\n");
  284. goto cleanup_irq;
  285. }
  286. conf_printf(name, hw_config);
  287. midi_devs[devc->my_dev] = kmemdup(&uart401_operations,
  288. sizeof(struct midi_operations),
  289. GFP_KERNEL);
  290. if (!midi_devs[devc->my_dev]) {
  291. printk(KERN_ERR "uart401: Failed to allocate memory\n");
  292. goto cleanup_unload_mididev;
  293. }
  294. if (owner)
  295. midi_devs[devc->my_dev]->owner = owner;
  296. midi_devs[devc->my_dev]->devc = devc;
  297. midi_devs[devc->my_dev]->converter = kmemdup(&std_midi_synth,
  298. sizeof(struct synth_operations),
  299. GFP_KERNEL);
  300. if (!midi_devs[devc->my_dev]->converter) {
  301. printk(KERN_WARNING "uart401: Failed to allocate memory\n");
  302. goto cleanup_midi_devs;
  303. }
  304. strcpy(midi_devs[devc->my_dev]->info.name, name);
  305. midi_devs[devc->my_dev]->converter->id = "UART401";
  306. midi_devs[devc->my_dev]->converter->midi_dev = devc->my_dev;
  307. if (owner)
  308. midi_devs[devc->my_dev]->converter->owner = owner;
  309. hw_config->slots[4] = devc->my_dev;
  310. sequencer_init();
  311. devc->opened = 0;
  312. return 1;
  313. cleanup_midi_devs:
  314. kfree(midi_devs[devc->my_dev]);
  315. cleanup_unload_mididev:
  316. sound_unload_mididev(devc->my_dev);
  317. cleanup_irq:
  318. if (!devc->share_irq)
  319. free_irq(devc->irq, devc);
  320. cleanup_devc:
  321. kfree(devc);
  322. cleanup_region:
  323. release_region(hw_config->io_base, 4);
  324. return 0;
  325. }
  326. void unload_uart401(struct address_info *hw_config)
  327. {
  328. struct uart401_devc *devc;
  329. int n=hw_config->slots[4];
  330. /* Not set up */
  331. if(n==-1 || midi_devs[n]==NULL)
  332. return;
  333. /* Not allocated (erm ??) */
  334. devc = midi_devs[hw_config->slots[4]]->devc;
  335. if (devc == NULL)
  336. return;
  337. reset_uart401(devc);
  338. release_region(hw_config->io_base, 4);
  339. if (!devc->share_irq)
  340. free_irq(devc->irq, devc);
  341. kfree(midi_devs[devc->my_dev]->converter);
  342. kfree(midi_devs[devc->my_dev]);
  343. kfree(devc);
  344. /* This kills midi_devs[x] */
  345. sound_unload_mididev(hw_config->slots[4]);
  346. }
  347. EXPORT_SYMBOL(probe_uart401);
  348. EXPORT_SYMBOL(unload_uart401);
  349. EXPORT_SYMBOL(uart401intr);
  350. static struct address_info cfg_mpu;
  351. static int io = -1;
  352. static int irq = -1;
  353. module_param_hw(io, int, ioport, 0444);
  354. module_param_hw(irq, int, irq, 0444);
  355. static int __init init_uart401(void)
  356. {
  357. cfg_mpu.irq = irq;
  358. cfg_mpu.io_base = io;
  359. /* Can be loaded either for module use or to provide functions
  360. to others */
  361. if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1) {
  362. printk(KERN_INFO "MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
  363. if (!probe_uart401(&cfg_mpu, THIS_MODULE))
  364. return -ENODEV;
  365. }
  366. return 0;
  367. }
  368. static void __exit cleanup_uart401(void)
  369. {
  370. if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1)
  371. unload_uart401(&cfg_mpu);
  372. }
  373. module_init(init_uart401);
  374. module_exit(cleanup_uart401);
  375. #ifndef MODULE
  376. static int __init setup_uart401(char *str)
  377. {
  378. /* io, irq */
  379. int ints[3];
  380. str = get_options(str, ARRAY_SIZE(ints), ints);
  381. io = ints[1];
  382. irq = ints[2];
  383. return 1;
  384. }
  385. __setup("uart401=", setup_uart401);
  386. #endif
  387. MODULE_LICENSE("GPL");