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. 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. /* Flush input before enabling interrupts */
  228. if (ok)
  229. uart401_input_loop(devc);
  230. else
  231. DDB(printk("Reset UART401 failed - No hardware detected.\n"));
  232. return ok;
  233. }
  234. int probe_uart401(struct address_info *hw_config, struct module *owner)
  235. {
  236. uart401_devc *devc;
  237. char *name = "MPU-401 (UART) MIDI";
  238. int ok = 0;
  239. unsigned long flags;
  240. DDB(printk("Entered probe_uart401()\n"));
  241. /* Default to "not found" */
  242. hw_config->slots[4] = -1;
  243. if (!request_region(hw_config->io_base, 4, "MPU-401 UART")) {
  244. printk(KERN_INFO "uart401: could not request_region(%d, 4)\n", hw_config->io_base);
  245. return 0;
  246. }
  247. devc = kmalloc(sizeof(uart401_devc), GFP_KERNEL);
  248. if (!devc) {
  249. printk(KERN_WARNING "uart401: Can't allocate memory\n");
  250. goto cleanup_region;
  251. }
  252. devc->base = hw_config->io_base;
  253. devc->irq = hw_config->irq;
  254. devc->osp = hw_config->osp;
  255. devc->midi_input_intr = NULL;
  256. devc->opened = 0;
  257. devc->input_byte = 0;
  258. devc->my_dev = 0;
  259. devc->share_irq = 0;
  260. spin_lock_init(&devc->lock);
  261. spin_lock_irqsave(&devc->lock,flags);
  262. ok = reset_uart401(devc);
  263. spin_unlock_irqrestore(&devc->lock,flags);
  264. if (!ok)
  265. goto cleanup_devc;
  266. if (hw_config->name)
  267. name = hw_config->name;
  268. if (devc->irq < 0) {
  269. devc->share_irq = 1;
  270. devc->irq *= -1;
  271. } else
  272. devc->share_irq = 0;
  273. if (!devc->share_irq)
  274. if (request_irq(devc->irq, uart401intr, 0, "MPU-401 UART", devc) < 0) {
  275. printk(KERN_WARNING "uart401: Failed to allocate IRQ%d\n", devc->irq);
  276. devc->share_irq = 1;
  277. }
  278. devc->my_dev = sound_alloc_mididev();
  279. enter_uart_mode(devc);
  280. if (devc->my_dev == -1) {
  281. printk(KERN_INFO "uart401: Too many midi devices detected\n");
  282. goto cleanup_irq;
  283. }
  284. conf_printf(name, hw_config);
  285. midi_devs[devc->my_dev] = kmemdup(&uart401_operations,
  286. sizeof(struct midi_operations),
  287. GFP_KERNEL);
  288. if (!midi_devs[devc->my_dev]) {
  289. printk(KERN_ERR "uart401: Failed to allocate memory\n");
  290. goto cleanup_unload_mididev;
  291. }
  292. if (owner)
  293. midi_devs[devc->my_dev]->owner = owner;
  294. midi_devs[devc->my_dev]->devc = devc;
  295. midi_devs[devc->my_dev]->converter = kmemdup(&std_midi_synth,
  296. sizeof(struct synth_operations),
  297. GFP_KERNEL);
  298. if (!midi_devs[devc->my_dev]->converter) {
  299. printk(KERN_WARNING "uart401: Failed to allocate memory\n");
  300. goto cleanup_midi_devs;
  301. }
  302. strcpy(midi_devs[devc->my_dev]->info.name, name);
  303. midi_devs[devc->my_dev]->converter->id = "UART401";
  304. midi_devs[devc->my_dev]->converter->midi_dev = devc->my_dev;
  305. if (owner)
  306. midi_devs[devc->my_dev]->converter->owner = owner;
  307. hw_config->slots[4] = devc->my_dev;
  308. sequencer_init();
  309. devc->opened = 0;
  310. return 1;
  311. cleanup_midi_devs:
  312. kfree(midi_devs[devc->my_dev]);
  313. cleanup_unload_mididev:
  314. sound_unload_mididev(devc->my_dev);
  315. cleanup_irq:
  316. if (!devc->share_irq)
  317. free_irq(devc->irq, devc);
  318. cleanup_devc:
  319. kfree(devc);
  320. cleanup_region:
  321. release_region(hw_config->io_base, 4);
  322. return 0;
  323. }
  324. void unload_uart401(struct address_info *hw_config)
  325. {
  326. uart401_devc *devc;
  327. int n=hw_config->slots[4];
  328. /* Not set up */
  329. if(n==-1 || midi_devs[n]==NULL)
  330. return;
  331. /* Not allocated (erm ??) */
  332. devc = midi_devs[hw_config->slots[4]]->devc;
  333. if (devc == NULL)
  334. return;
  335. reset_uart401(devc);
  336. release_region(hw_config->io_base, 4);
  337. if (!devc->share_irq)
  338. free_irq(devc->irq, devc);
  339. if (devc)
  340. {
  341. kfree(midi_devs[devc->my_dev]->converter);
  342. kfree(midi_devs[devc->my_dev]);
  343. kfree(devc);
  344. devc = NULL;
  345. }
  346. /* This kills midi_devs[x] */
  347. sound_unload_mididev(hw_config->slots[4]);
  348. }
  349. EXPORT_SYMBOL(probe_uart401);
  350. EXPORT_SYMBOL(unload_uart401);
  351. EXPORT_SYMBOL(uart401intr);
  352. static struct address_info cfg_mpu;
  353. static int io = -1;
  354. static int irq = -1;
  355. module_param(io, int, 0444);
  356. module_param(irq, int, 0444);
  357. static int __init init_uart401(void)
  358. {
  359. cfg_mpu.irq = irq;
  360. cfg_mpu.io_base = io;
  361. /* Can be loaded either for module use or to provide functions
  362. to others */
  363. if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1) {
  364. printk(KERN_INFO "MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
  365. if (!probe_uart401(&cfg_mpu, THIS_MODULE))
  366. return -ENODEV;
  367. }
  368. return 0;
  369. }
  370. static void __exit cleanup_uart401(void)
  371. {
  372. if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1)
  373. unload_uart401(&cfg_mpu);
  374. }
  375. module_init(init_uart401);
  376. module_exit(cleanup_uart401);
  377. #ifndef MODULE
  378. static int __init setup_uart401(char *str)
  379. {
  380. /* io, irq */
  381. int ints[3];
  382. str = get_options(str, ARRAY_SIZE(ints), ints);
  383. io = ints[1];
  384. irq = ints[2];
  385. return 1;
  386. }
  387. __setup("uart401=", setup_uart401);
  388. #endif
  389. MODULE_LICENSE("GPL");