uart401.c 10 KB

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