uart401.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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/spinlock.h>
  27. #include "sound_config.h"
  28. #include "mpu401.h"
  29. typedef struct uart401_devc
  30. {
  31. int base;
  32. int irq;
  33. int *osp;
  34. void (*midi_input_intr) (int dev, unsigned char data);
  35. int opened, disabled;
  36. volatile unsigned char input_byte;
  37. int my_dev;
  38. int share_irq;
  39. spinlock_t lock;
  40. }
  41. uart401_devc;
  42. #define DATAPORT (devc->base)
  43. #define COMDPORT (devc->base+1)
  44. #define STATPORT (devc->base+1)
  45. static int uart401_status(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(uart401_devc * devc, unsigned char cmd)
  52. {
  53. outb((cmd), COMDPORT);
  54. }
  55. static int uart401_read(uart401_devc * devc)
  56. {
  57. return inb(DATAPORT);
  58. }
  59. static void uart401_write(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(uart401_devc * devc);
  69. static void enter_uart_mode(uart401_devc * devc);
  70. static void uart401_input_loop(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. 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. uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
  103. if (devc->opened)
  104. return -EBUSY;
  105. /* Flush the UART */
  106. while (input_avail(devc))
  107. uart401_read(devc);
  108. devc->midi_input_intr = input;
  109. devc->opened = mode;
  110. enter_uart_mode(devc);
  111. devc->disabled = 0;
  112. return 0;
  113. }
  114. static void uart401_close(int dev)
  115. {
  116. uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
  117. reset_uart401(devc);
  118. devc->opened = 0;
  119. }
  120. static int uart401_out(int dev, unsigned char midi_byte)
  121. {
  122. int timeout;
  123. unsigned long flags;
  124. uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
  125. if (devc->disabled)
  126. return 1;
  127. /*
  128. * Test for input since pending input seems to block the output.
  129. */
  130. spin_lock_irqsave(&devc->lock,flags);
  131. if (input_avail(devc))
  132. uart401_input_loop(devc);
  133. spin_unlock_irqrestore(&devc->lock,flags);
  134. /*
  135. * Sometimes it takes about 13000 loops before the output becomes ready
  136. * (After reset). Normally it takes just about 10 loops.
  137. */
  138. for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
  139. if (!output_ready(devc))
  140. {
  141. printk(KERN_WARNING "uart401: Timeout - Device not responding\n");
  142. devc->disabled = 1;
  143. reset_uart401(devc);
  144. enter_uart_mode(devc);
  145. return 1;
  146. }
  147. uart401_write(devc, midi_byte);
  148. return 1;
  149. }
  150. static inline int uart401_start_read(int dev)
  151. {
  152. return 0;
  153. }
  154. static inline int uart401_end_read(int dev)
  155. {
  156. return 0;
  157. }
  158. static inline void uart401_kick(int dev)
  159. {
  160. }
  161. static inline int uart401_buffer_status(int dev)
  162. {
  163. return 0;
  164. }
  165. #define MIDI_SYNTH_NAME "MPU-401 UART"
  166. #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
  167. #include "midi_synth.h"
  168. static const struct midi_operations uart401_operations =
  169. {
  170. .owner = THIS_MODULE,
  171. .info = {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401},
  172. .converter = &std_midi_synth,
  173. .in_info = {0},
  174. .open = uart401_open,
  175. .close = uart401_close,
  176. .outputc = uart401_out,
  177. .start_read = uart401_start_read,
  178. .end_read = uart401_end_read,
  179. .kick = uart401_kick,
  180. .buffer_status = uart401_buffer_status,
  181. };
  182. static void enter_uart_mode(uart401_devc * devc)
  183. {
  184. int ok, timeout;
  185. unsigned long flags;
  186. spin_lock_irqsave(&devc->lock,flags);
  187. for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
  188. devc->input_byte = 0;
  189. uart401_cmd(devc, UART_MODE_ON);
  190. ok = 0;
  191. for (timeout = 50000; timeout > 0 && !ok; timeout--)
  192. if (devc->input_byte == MPU_ACK)
  193. ok = 1;
  194. else if (input_avail(devc))
  195. if (uart401_read(devc) == MPU_ACK)
  196. ok = 1;
  197. spin_unlock_irqrestore(&devc->lock,flags);
  198. }
  199. static int reset_uart401(uart401_devc * devc)
  200. {
  201. int ok, timeout, n;
  202. /*
  203. * Send the RESET command. Try again if no success at the first time.
  204. */
  205. ok = 0;
  206. for (n = 0; n < 2 && !ok; n++)
  207. {
  208. for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
  209. devc->input_byte = 0;
  210. uart401_cmd(devc, MPU_RESET);
  211. /*
  212. * Wait at least 25 msec. This method is not accurate so let's make the
  213. * loop bit longer. Cannot sleep since this is called during boot.
  214. */
  215. for (timeout = 50000; timeout > 0 && !ok; timeout--)
  216. {
  217. if (devc->input_byte == MPU_ACK) /* Interrupt */
  218. ok = 1;
  219. else if (input_avail(devc))
  220. {
  221. if (uart401_read(devc) == MPU_ACK)
  222. ok = 1;
  223. }
  224. }
  225. }
  226. if (ok)
  227. {
  228. DEB(printk("Reset UART401 OK\n"));
  229. }
  230. else
  231. DDB(printk("Reset UART401 failed - No hardware detected.\n"));
  232. if (ok)
  233. uart401_input_loop(devc); /*
  234. * Flush input before enabling interrupts
  235. */
  236. return ok;
  237. }
  238. int probe_uart401(struct address_info *hw_config, struct module *owner)
  239. {
  240. uart401_devc *devc;
  241. char *name = "MPU-401 (UART) MIDI";
  242. int ok = 0;
  243. unsigned long flags;
  244. DDB(printk("Entered probe_uart401()\n"));
  245. /* Default to "not found" */
  246. hw_config->slots[4] = -1;
  247. if (!request_region(hw_config->io_base, 4, "MPU-401 UART")) {
  248. printk(KERN_INFO "uart401: could not request_region(%d, 4)\n", hw_config->io_base);
  249. return 0;
  250. }
  251. devc = kmalloc(sizeof(uart401_devc), GFP_KERNEL);
  252. if (!devc) {
  253. printk(KERN_WARNING "uart401: Can't allocate memory\n");
  254. goto cleanup_region;
  255. }
  256. devc->base = hw_config->io_base;
  257. devc->irq = hw_config->irq;
  258. devc->osp = hw_config->osp;
  259. devc->midi_input_intr = NULL;
  260. devc->opened = 0;
  261. devc->input_byte = 0;
  262. devc->my_dev = 0;
  263. devc->share_irq = 0;
  264. spin_lock_init(&devc->lock);
  265. spin_lock_irqsave(&devc->lock,flags);
  266. ok = reset_uart401(devc);
  267. spin_unlock_irqrestore(&devc->lock,flags);
  268. if (!ok)
  269. goto cleanup_devc;
  270. if (hw_config->name)
  271. name = hw_config->name;
  272. if (devc->irq < 0) {
  273. devc->share_irq = 1;
  274. devc->irq *= -1;
  275. } else
  276. devc->share_irq = 0;
  277. if (!devc->share_irq)
  278. if (request_irq(devc->irq, uart401intr, 0, "MPU-401 UART", devc) < 0) {
  279. printk(KERN_WARNING "uart401: Failed to allocate IRQ%d\n", devc->irq);
  280. devc->share_irq = 1;
  281. }
  282. devc->my_dev = sound_alloc_mididev();
  283. enter_uart_mode(devc);
  284. if (devc->my_dev == -1) {
  285. printk(KERN_INFO "uart401: Too many midi devices detected\n");
  286. goto cleanup_irq;
  287. }
  288. conf_printf(name, hw_config);
  289. midi_devs[devc->my_dev] = kmalloc(sizeof(struct midi_operations), 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. memcpy(midi_devs[devc->my_dev], &uart401_operations, sizeof(struct midi_operations));
  295. if (owner)
  296. midi_devs[devc->my_dev]->owner = owner;
  297. midi_devs[devc->my_dev]->devc = devc;
  298. midi_devs[devc->my_dev]->converter = kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
  299. if (!midi_devs[devc->my_dev]->converter) {
  300. printk(KERN_WARNING "uart401: Failed to allocate memory\n");
  301. goto cleanup_midi_devs;
  302. }
  303. memcpy(midi_devs[devc->my_dev]->converter, &std_midi_synth, sizeof(struct synth_operations));
  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. 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. if (devc)
  342. {
  343. kfree(midi_devs[devc->my_dev]->converter);
  344. kfree(midi_devs[devc->my_dev]);
  345. kfree(devc);
  346. devc = NULL;
  347. }
  348. /* This kills midi_devs[x] */
  349. sound_unload_mididev(hw_config->slots[4]);
  350. }
  351. EXPORT_SYMBOL(probe_uart401);
  352. EXPORT_SYMBOL(unload_uart401);
  353. EXPORT_SYMBOL(uart401intr);
  354. static struct address_info cfg_mpu;
  355. static int io = -1;
  356. static int irq = -1;
  357. module_param(io, int, 0444);
  358. module_param(irq, int, 0444);
  359. static int __init init_uart401(void)
  360. {
  361. cfg_mpu.irq = irq;
  362. cfg_mpu.io_base = io;
  363. /* Can be loaded either for module use or to provide functions
  364. to others */
  365. if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1) {
  366. printk(KERN_INFO "MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
  367. if (!probe_uart401(&cfg_mpu, THIS_MODULE))
  368. return -ENODEV;
  369. }
  370. return 0;
  371. }
  372. static void __exit cleanup_uart401(void)
  373. {
  374. if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1)
  375. unload_uart401(&cfg_mpu);
  376. }
  377. module_init(init_uart401);
  378. module_exit(cleanup_uart401);
  379. #ifndef MODULE
  380. static int __init setup_uart401(char *str)
  381. {
  382. /* io, irq */
  383. int ints[3];
  384. str = get_options(str, ARRAY_SIZE(ints), ints);
  385. io = ints[1];
  386. irq = ints[2];
  387. return 1;
  388. }
  389. __setup("uart401=", setup_uart401);
  390. #endif
  391. MODULE_LICENSE("GPL");