serial-u16550.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /*
  2. * serial.c
  3. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
  4. * Isaku Yamahata <yamahata@private.email.ne.jp>,
  5. * George Hansper <ghansper@apana.org.au>,
  6. * Hannu Savolainen
  7. *
  8. * This code is based on the code from ALSA 0.5.9, but heavily rewritten.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Sat Mar 31 17:27:57 PST 2001 tim.mann@compaq.com
  25. * Added support for the Midiator MS-124T and for the MS-124W in
  26. * Single Addressed (S/A) or Multiple Burst (M/B) mode, with
  27. * power derived either parasitically from the serial port or
  28. * from a separate power supply.
  29. *
  30. * More documentation can be found in serial-u16550.txt.
  31. */
  32. #include <sound/driver.h>
  33. #include <linux/init.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/slab.h>
  36. #include <linux/ioport.h>
  37. #include <linux/moduleparam.h>
  38. #include <sound/core.h>
  39. #include <sound/rawmidi.h>
  40. #include <sound/initval.h>
  41. #include <linux/serial_reg.h>
  42. #include <asm/io.h>
  43. MODULE_DESCRIPTION("MIDI serial u16550");
  44. MODULE_LICENSE("GPL");
  45. MODULE_SUPPORTED_DEVICE("{{ALSA, MIDI serial u16550}}");
  46. #define SNDRV_SERIAL_SOUNDCANVAS 0 /* Roland Soundcanvas; F5 NN selects part */
  47. #define SNDRV_SERIAL_MS124T 1 /* Midiator MS-124T */
  48. #define SNDRV_SERIAL_MS124W_SA 2 /* Midiator MS-124W in S/A mode */
  49. #define SNDRV_SERIAL_MS124W_MB 3 /* Midiator MS-124W in M/B mode */
  50. #define SNDRV_SERIAL_GENERIC 4 /* Generic Interface */
  51. #define SNDRV_SERIAL_MAX_ADAPTOR SNDRV_SERIAL_GENERIC
  52. static char *adaptor_names[] = {
  53. "Soundcanvas",
  54. "MS-124T",
  55. "MS-124W S/A",
  56. "MS-124W M/B",
  57. "Generic"
  58. };
  59. #define SNDRV_SERIAL_NORMALBUFF 0 /* Normal blocking buffer operation */
  60. #define SNDRV_SERIAL_DROPBUFF 1 /* Non-blocking discard operation */
  61. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  62. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  63. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  64. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x3f8,0x2f8,0x3e8,0x2e8 */
  65. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,4,5,7,9,10,11,14,15 */
  66. static int speed[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 38400}; /* 9600,19200,38400,57600,115200 */
  67. static int base[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 115200}; /* baud base */
  68. static int outs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */
  69. static int ins[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */
  70. static int adaptor[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = SNDRV_SERIAL_SOUNDCANVAS};
  71. static int droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF };
  72. module_param_array(index, int, NULL, 0444);
  73. MODULE_PARM_DESC(index, "Index value for Serial MIDI.");
  74. module_param_array(id, charp, NULL, 0444);
  75. MODULE_PARM_DESC(id, "ID string for Serial MIDI.");
  76. module_param_array(enable, bool, NULL, 0444);
  77. MODULE_PARM_DESC(enable, "Enable UART16550A chip.");
  78. module_param_array(port, long, NULL, 0444);
  79. MODULE_PARM_DESC(port, "Port # for UART16550A chip.");
  80. module_param_array(irq, int, NULL, 0444);
  81. MODULE_PARM_DESC(irq, "IRQ # for UART16550A chip.");
  82. module_param_array(speed, int, NULL, 0444);
  83. MODULE_PARM_DESC(speed, "Speed in bauds.");
  84. module_param_array(base, int, NULL, 0444);
  85. MODULE_PARM_DESC(base, "Base for divisor in bauds.");
  86. module_param_array(outs, int, NULL, 0444);
  87. MODULE_PARM_DESC(outs, "Number of MIDI outputs.");
  88. module_param_array(ins, int, NULL, 0444);
  89. MODULE_PARM_DESC(ins, "Number of MIDI inputs.");
  90. module_param_array(droponfull, bool, NULL, 0444);
  91. MODULE_PARM_DESC(droponfull, "Flag to enable drop-on-full buffer mode");
  92. module_param_array(adaptor, int, NULL, 0444);
  93. MODULE_PARM_DESC(adaptor, "Type of adaptor.");
  94. /*#define SNDRV_SERIAL_MS124W_MB_NOCOMBO 1*/ /* Address outs as 0-3 instead of bitmap */
  95. #define SNDRV_SERIAL_MAX_OUTS 16 /* max 64, min 16 */
  96. #define SNDRV_SERIAL_MAX_INS 16 /* max 64, min 16 */
  97. #define TX_BUFF_SIZE (1<<15) /* Must be 2^n */
  98. #define TX_BUFF_MASK (TX_BUFF_SIZE - 1)
  99. #define SERIAL_MODE_NOT_OPENED (0)
  100. #define SERIAL_MODE_INPUT_OPEN (1 << 0)
  101. #define SERIAL_MODE_OUTPUT_OPEN (1 << 1)
  102. #define SERIAL_MODE_INPUT_TRIGGERED (1 << 2)
  103. #define SERIAL_MODE_OUTPUT_TRIGGERED (1 << 3)
  104. typedef struct _snd_uart16550 {
  105. snd_card_t *card;
  106. snd_rawmidi_t *rmidi;
  107. snd_rawmidi_substream_t *midi_output[SNDRV_SERIAL_MAX_OUTS];
  108. snd_rawmidi_substream_t *midi_input[SNDRV_SERIAL_MAX_INS];
  109. int filemode; //open status of file
  110. spinlock_t open_lock;
  111. int irq;
  112. unsigned long base;
  113. struct resource *res_base;
  114. unsigned int speed;
  115. unsigned int speed_base;
  116. unsigned char divisor;
  117. unsigned char old_divisor_lsb;
  118. unsigned char old_divisor_msb;
  119. unsigned char old_line_ctrl_reg;
  120. // parameter for using of write loop
  121. short int fifo_limit; //used in uart16550
  122. short int fifo_count; //used in uart16550
  123. // type of adaptor
  124. int adaptor;
  125. // inputs
  126. int prev_in;
  127. unsigned char rstatus;
  128. // outputs
  129. int prev_out;
  130. unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS];
  131. // write buffer and its writing/reading position
  132. unsigned char tx_buff[TX_BUFF_SIZE];
  133. int buff_in_count;
  134. int buff_in;
  135. int buff_out;
  136. int drop_on_full;
  137. // wait timer
  138. unsigned int timer_running:1;
  139. struct timer_list buffer_timer;
  140. } snd_uart16550_t;
  141. static snd_card_t *snd_serial_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
  142. static inline void snd_uart16550_add_timer(snd_uart16550_t *uart)
  143. {
  144. if (! uart->timer_running) {
  145. /* timer 38600bps * 10bit * 16byte */
  146. uart->buffer_timer.expires = jiffies + (HZ+255)/256;
  147. uart->timer_running = 1;
  148. add_timer(&uart->buffer_timer);
  149. }
  150. }
  151. static inline void snd_uart16550_del_timer(snd_uart16550_t *uart)
  152. {
  153. if (uart->timer_running) {
  154. del_timer(&uart->buffer_timer);
  155. uart->timer_running = 0;
  156. }
  157. }
  158. /* This macro is only used in snd_uart16550_io_loop */
  159. static inline void snd_uart16550_buffer_output(snd_uart16550_t *uart)
  160. {
  161. unsigned short buff_out = uart->buff_out;
  162. if( uart->buff_in_count > 0 ) {
  163. outb(uart->tx_buff[buff_out], uart->base + UART_TX);
  164. uart->fifo_count++;
  165. buff_out++;
  166. buff_out &= TX_BUFF_MASK;
  167. uart->buff_out = buff_out;
  168. uart->buff_in_count--;
  169. }
  170. }
  171. /* This loop should be called with interrupts disabled
  172. * We don't want to interrupt this,
  173. * as we're already handling an interrupt
  174. */
  175. static void snd_uart16550_io_loop(snd_uart16550_t * uart)
  176. {
  177. unsigned char c, status;
  178. int substream;
  179. /* recall previous stream */
  180. substream = uart->prev_in;
  181. /* Read Loop */
  182. while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) {
  183. /* while receive data ready */
  184. c = inb(uart->base + UART_RX);
  185. /* keep track of last status byte */
  186. if (c & 0x80) {
  187. uart->rstatus = c;
  188. }
  189. /* handle stream switch */
  190. if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
  191. if (uart->rstatus == 0xf5) {
  192. if (c <= SNDRV_SERIAL_MAX_INS && c > 0)
  193. substream = c - 1;
  194. if (c != 0xf5)
  195. uart->rstatus = 0; /* prevent future bytes from being interpreted as streams */
  196. }
  197. else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) && (uart->midi_input[substream] != NULL)) {
  198. snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
  199. }
  200. } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) && (uart->midi_input[substream] != NULL)) {
  201. snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
  202. }
  203. if (status & UART_LSR_OE)
  204. snd_printk("%s: Overrun on device at 0x%lx\n",
  205. uart->rmidi->name, uart->base);
  206. }
  207. /* remember the last stream */
  208. uart->prev_in = substream;
  209. /* no need of check SERIAL_MODE_OUTPUT_OPEN because if not,
  210. buffer is never filled. */
  211. /* Check write status */
  212. if (status & UART_LSR_THRE) {
  213. uart->fifo_count = 0;
  214. }
  215. if (uart->adaptor == SNDRV_SERIAL_MS124W_SA
  216. || uart->adaptor == SNDRV_SERIAL_GENERIC) {
  217. /* Can't use FIFO, must send only when CTS is true */
  218. status = inb(uart->base + UART_MSR);
  219. while( (uart->fifo_count == 0) && (status & UART_MSR_CTS) &&
  220. (uart->buff_in_count > 0) ) {
  221. snd_uart16550_buffer_output(uart);
  222. status = inb( uart->base + UART_MSR );
  223. }
  224. } else {
  225. /* Write loop */
  226. while (uart->fifo_count < uart->fifo_limit /* Can we write ? */
  227. && uart->buff_in_count > 0) /* Do we want to? */
  228. snd_uart16550_buffer_output(uart);
  229. }
  230. if (uart->irq < 0 && uart->buff_in_count > 0)
  231. snd_uart16550_add_timer(uart);
  232. }
  233. /* NOTES ON SERVICING INTERUPTS
  234. * ---------------------------
  235. * After receiving a interrupt, it is important to indicate to the UART that
  236. * this has been done.
  237. * For a Rx interrupt, this is done by reading the received byte.
  238. * For a Tx interrupt this is done by either:
  239. * a) Writing a byte
  240. * b) Reading the IIR
  241. * It is particularly important to read the IIR if a Tx interrupt is received
  242. * when there is no data in tx_buff[], as in this case there no other
  243. * indication that the interrupt has been serviced, and it remains outstanding
  244. * indefinitely. This has the curious side effect that and no further interrupts
  245. * will be generated from this device AT ALL!!.
  246. * It is also desirable to clear outstanding interrupts when the device is
  247. * opened/closed.
  248. *
  249. *
  250. * Note that some devices need OUT2 to be set before they will generate
  251. * interrupts at all. (Possibly tied to an internal pull-up on CTS?)
  252. */
  253. static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  254. {
  255. snd_uart16550_t *uart;
  256. uart = (snd_uart16550_t *) dev_id;
  257. spin_lock(&uart->open_lock);
  258. if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
  259. spin_unlock(&uart->open_lock);
  260. return IRQ_NONE;
  261. }
  262. inb(uart->base + UART_IIR); /* indicate to the UART that the interrupt has been serviced */
  263. snd_uart16550_io_loop(uart);
  264. spin_unlock(&uart->open_lock);
  265. return IRQ_HANDLED;
  266. }
  267. /* When the polling mode, this function calls snd_uart16550_io_loop. */
  268. static void snd_uart16550_buffer_timer(unsigned long data)
  269. {
  270. unsigned long flags;
  271. snd_uart16550_t *uart;
  272. uart = (snd_uart16550_t *)data;
  273. spin_lock_irqsave(&uart->open_lock, flags);
  274. snd_uart16550_del_timer(uart);
  275. snd_uart16550_io_loop(uart);
  276. spin_unlock_irqrestore(&uart->open_lock, flags);
  277. }
  278. /*
  279. * this method probes, if an uart sits on given port
  280. * return 0 if found
  281. * return negative error if not found
  282. */
  283. static int __init snd_uart16550_detect(snd_uart16550_t *uart)
  284. {
  285. unsigned long io_base = uart->base;
  286. int ok;
  287. unsigned char c;
  288. /* Do some vague tests for the presence of the uart */
  289. if (io_base == 0 || io_base == SNDRV_AUTO_PORT) {
  290. return -ENODEV; /* Not configured */
  291. }
  292. uart->res_base = request_region(io_base, 8, "Serial MIDI");
  293. if (uart->res_base == NULL) {
  294. snd_printk(KERN_ERR "u16550: can't grab port 0x%lx\n", io_base);
  295. return -EBUSY;
  296. }
  297. ok = 1; /* uart detected unless one of the following tests should fail */
  298. /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */
  299. outb(UART_LCR_WLEN8, io_base + UART_LCR); /* Line Control Register */
  300. c = inb(io_base + UART_IER);
  301. /* The top four bits of the IER should always == 0 */
  302. if ((c & 0xf0) != 0)
  303. ok = 0; /* failed */
  304. outb(0xaa, io_base + UART_SCR);
  305. /* Write arbitrary data into the scratch reg */
  306. c = inb(io_base + UART_SCR);
  307. /* If it comes back, it's OK */
  308. if (c != 0xaa)
  309. ok = 0; /* failed */
  310. outb(0x55, io_base + UART_SCR);
  311. /* Write arbitrary data into the scratch reg */
  312. c = inb(io_base + UART_SCR);
  313. /* If it comes back, it's OK */
  314. if (c != 0x55)
  315. ok = 0; /* failed */
  316. return ok;
  317. }
  318. static void snd_uart16550_do_open(snd_uart16550_t * uart)
  319. {
  320. char byte;
  321. /* Initialize basic variables */
  322. uart->buff_in_count = 0;
  323. uart->buff_in = 0;
  324. uart->buff_out = 0;
  325. uart->fifo_limit = 1;
  326. uart->fifo_count = 0;
  327. uart->timer_running = 0;
  328. outb(UART_FCR_ENABLE_FIFO /* Enable FIFO's (if available) */
  329. | UART_FCR_CLEAR_RCVR /* Clear receiver FIFO */
  330. | UART_FCR_CLEAR_XMIT /* Clear transmitter FIFO */
  331. | UART_FCR_TRIGGER_4 /* Set FIFO trigger at 4-bytes */
  332. /* NOTE: interrupt generated after T=(time)4-bytes
  333. * if less than UART_FCR_TRIGGER bytes received
  334. */
  335. ,uart->base + UART_FCR); /* FIFO Control Register */
  336. if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0)
  337. uart->fifo_limit = 16;
  338. if (uart->divisor != 0) {
  339. uart->old_line_ctrl_reg = inb(uart->base + UART_LCR);
  340. outb(UART_LCR_DLAB /* Divisor latch access bit */
  341. ,uart->base + UART_LCR); /* Line Control Register */
  342. uart->old_divisor_lsb = inb(uart->base + UART_DLL);
  343. uart->old_divisor_msb = inb(uart->base + UART_DLM);
  344. outb(uart->divisor
  345. ,uart->base + UART_DLL); /* Divisor Latch Low */
  346. outb(0
  347. ,uart->base + UART_DLM); /* Divisor Latch High */
  348. /* DLAB is reset to 0 in next outb() */
  349. }
  350. /* Set serial parameters (parity off, etc) */
  351. outb(UART_LCR_WLEN8 /* 8 data-bits */
  352. | 0 /* 1 stop-bit */
  353. | 0 /* parity off */
  354. | 0 /* DLAB = 0 */
  355. ,uart->base + UART_LCR); /* Line Control Register */
  356. switch (uart->adaptor) {
  357. default:
  358. outb(UART_MCR_RTS /* Set Request-To-Send line active */
  359. | UART_MCR_DTR /* Set Data-Terminal-Ready line active */
  360. | UART_MCR_OUT2 /* Set OUT2 - not always required, but when
  361. * it is, it is ESSENTIAL for enabling interrupts
  362. */
  363. ,uart->base + UART_MCR); /* Modem Control Register */
  364. break;
  365. case SNDRV_SERIAL_MS124W_SA:
  366. case SNDRV_SERIAL_MS124W_MB:
  367. /* MS-124W can draw power from RTS and DTR if they
  368. are in opposite states. */
  369. outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2,
  370. uart->base + UART_MCR);
  371. break;
  372. case SNDRV_SERIAL_MS124T:
  373. /* MS-124T can draw power from RTS and/or DTR (preferably
  374. both) if they are both asserted. */
  375. outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2,
  376. uart->base + UART_MCR);
  377. break;
  378. }
  379. if (uart->irq < 0) {
  380. byte = (0 & UART_IER_RDI) /* Disable Receiver data interrupt */
  381. |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
  382. ;
  383. } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
  384. byte = UART_IER_RDI /* Enable Receiver data interrupt */
  385. | UART_IER_MSI /* Enable Modem status interrupt */
  386. ;
  387. } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
  388. byte = UART_IER_RDI /* Enable Receiver data interrupt */
  389. | UART_IER_MSI /* Enable Modem status interrupt */
  390. | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
  391. ;
  392. } else {
  393. byte = UART_IER_RDI /* Enable Receiver data interrupt */
  394. | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
  395. ;
  396. }
  397. outb(byte, uart->base + UART_IER); /* Interupt enable Register */
  398. inb(uart->base + UART_LSR); /* Clear any pre-existing overrun indication */
  399. inb(uart->base + UART_IIR); /* Clear any pre-existing transmit interrupt */
  400. inb(uart->base + UART_RX); /* Clear any pre-existing receive interrupt */
  401. }
  402. static void snd_uart16550_do_close(snd_uart16550_t * uart)
  403. {
  404. if (uart->irq < 0)
  405. snd_uart16550_del_timer(uart);
  406. /* NOTE: may need to disable interrupts before de-registering out handler.
  407. * For now, the consequences are harmless.
  408. */
  409. outb((0 & UART_IER_RDI) /* Disable Receiver data interrupt */
  410. |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
  411. ,uart->base + UART_IER); /* Interupt enable Register */
  412. switch (uart->adaptor) {
  413. default:
  414. outb((0 & UART_MCR_RTS) /* Deactivate Request-To-Send line */
  415. |(0 & UART_MCR_DTR) /* Deactivate Data-Terminal-Ready line */
  416. |(0 & UART_MCR_OUT2) /* Deactivate OUT2 */
  417. ,uart->base + UART_MCR); /* Modem Control Register */
  418. break;
  419. case SNDRV_SERIAL_MS124W_SA:
  420. case SNDRV_SERIAL_MS124W_MB:
  421. /* MS-124W can draw power from RTS and DTR if they
  422. are in opposite states; leave it powered. */
  423. outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2),
  424. uart->base + UART_MCR);
  425. break;
  426. case SNDRV_SERIAL_MS124T:
  427. /* MS-124T can draw power from RTS and/or DTR (preferably
  428. both) if they are both asserted; leave it powered. */
  429. outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2),
  430. uart->base + UART_MCR);
  431. break;
  432. }
  433. inb(uart->base + UART_IIR); /* Clear any outstanding interrupts */
  434. /* Restore old divisor */
  435. if (uart->divisor != 0) {
  436. outb(UART_LCR_DLAB /* Divisor latch access bit */
  437. ,uart->base + UART_LCR); /* Line Control Register */
  438. outb(uart->old_divisor_lsb
  439. ,uart->base + UART_DLL); /* Divisor Latch Low */
  440. outb(uart->old_divisor_msb
  441. ,uart->base + UART_DLM); /* Divisor Latch High */
  442. /* Restore old LCR (data bits, stop bits, parity, DLAB) */
  443. outb(uart->old_line_ctrl_reg
  444. ,uart->base + UART_LCR); /* Line Control Register */
  445. }
  446. }
  447. static int snd_uart16550_input_open(snd_rawmidi_substream_t * substream)
  448. {
  449. unsigned long flags;
  450. snd_uart16550_t *uart = substream->rmidi->private_data;
  451. spin_lock_irqsave(&uart->open_lock, flags);
  452. if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  453. snd_uart16550_do_open(uart);
  454. uart->filemode |= SERIAL_MODE_INPUT_OPEN;
  455. uart->midi_input[substream->number] = substream;
  456. spin_unlock_irqrestore(&uart->open_lock, flags);
  457. return 0;
  458. }
  459. static int snd_uart16550_input_close(snd_rawmidi_substream_t * substream)
  460. {
  461. unsigned long flags;
  462. snd_uart16550_t *uart = substream->rmidi->private_data;
  463. spin_lock_irqsave(&uart->open_lock, flags);
  464. uart->filemode &= ~SERIAL_MODE_INPUT_OPEN;
  465. uart->midi_input[substream->number] = NULL;
  466. if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  467. snd_uart16550_do_close(uart);
  468. spin_unlock_irqrestore(&uart->open_lock, flags);
  469. return 0;
  470. }
  471. static void snd_uart16550_input_trigger(snd_rawmidi_substream_t * substream, int up)
  472. {
  473. unsigned long flags;
  474. snd_uart16550_t *uart = substream->rmidi->private_data;
  475. spin_lock_irqsave(&uart->open_lock, flags);
  476. if (up) {
  477. uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
  478. } else {
  479. uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
  480. }
  481. spin_unlock_irqrestore(&uart->open_lock, flags);
  482. }
  483. static int snd_uart16550_output_open(snd_rawmidi_substream_t * substream)
  484. {
  485. unsigned long flags;
  486. snd_uart16550_t *uart = substream->rmidi->private_data;
  487. spin_lock_irqsave(&uart->open_lock, flags);
  488. if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  489. snd_uart16550_do_open(uart);
  490. uart->filemode |= SERIAL_MODE_OUTPUT_OPEN;
  491. uart->midi_output[substream->number] = substream;
  492. spin_unlock_irqrestore(&uart->open_lock, flags);
  493. return 0;
  494. };
  495. static int snd_uart16550_output_close(snd_rawmidi_substream_t * substream)
  496. {
  497. unsigned long flags;
  498. snd_uart16550_t *uart = substream->rmidi->private_data;
  499. spin_lock_irqsave(&uart->open_lock, flags);
  500. uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN;
  501. uart->midi_output[substream->number] = NULL;
  502. if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  503. snd_uart16550_do_close(uart);
  504. spin_unlock_irqrestore(&uart->open_lock, flags);
  505. return 0;
  506. };
  507. static inline int snd_uart16550_buffer_can_write( snd_uart16550_t *uart, int Num )
  508. {
  509. if( uart->buff_in_count + Num < TX_BUFF_SIZE )
  510. return 1;
  511. else
  512. return 0;
  513. }
  514. static inline int snd_uart16550_write_buffer(snd_uart16550_t *uart, unsigned char byte)
  515. {
  516. unsigned short buff_in = uart->buff_in;
  517. if( uart->buff_in_count < TX_BUFF_SIZE ) {
  518. uart->tx_buff[buff_in] = byte;
  519. buff_in++;
  520. buff_in &= TX_BUFF_MASK;
  521. uart->buff_in = buff_in;
  522. uart->buff_in_count++;
  523. if (uart->irq < 0) /* polling mode */
  524. snd_uart16550_add_timer(uart);
  525. return 1;
  526. } else
  527. return 0;
  528. }
  529. static int snd_uart16550_output_byte(snd_uart16550_t *uart, snd_rawmidi_substream_t * substream, unsigned char midi_byte)
  530. {
  531. if (uart->buff_in_count == 0 /* Buffer empty? */
  532. && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
  533. uart->adaptor != SNDRV_SERIAL_GENERIC) ||
  534. (uart->fifo_count == 0 /* FIFO empty? */
  535. && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) { /* CTS? */
  536. /* Tx Buffer Empty - try to write immediately */
  537. if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
  538. /* Transmitter holding register (and Tx FIFO) empty */
  539. uart->fifo_count = 1;
  540. outb(midi_byte, uart->base + UART_TX);
  541. } else {
  542. if (uart->fifo_count < uart->fifo_limit) {
  543. uart->fifo_count++;
  544. outb(midi_byte, uart->base + UART_TX);
  545. } else {
  546. /* Cannot write (buffer empty) - put char in buffer */
  547. snd_uart16550_write_buffer(uart, midi_byte);
  548. }
  549. }
  550. } else {
  551. if( !snd_uart16550_write_buffer(uart, midi_byte) ) {
  552. snd_printk("%s: Buffer overrun on device at 0x%lx\n",
  553. uart->rmidi->name, uart->base);
  554. return 0;
  555. }
  556. }
  557. return 1;
  558. }
  559. static void snd_uart16550_output_write(snd_rawmidi_substream_t * substream)
  560. {
  561. unsigned long flags;
  562. unsigned char midi_byte, addr_byte;
  563. snd_uart16550_t *uart = substream->rmidi->private_data;
  564. char first;
  565. static unsigned long lasttime=0;
  566. /* Interupts are disabled during the updating of the tx_buff,
  567. * since it is 'bad' to have two processes updating the same
  568. * variables (ie buff_in & buff_out)
  569. */
  570. spin_lock_irqsave(&uart->open_lock, flags);
  571. if (uart->irq < 0) //polling
  572. snd_uart16550_io_loop(uart);
  573. if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
  574. while (1) {
  575. /* buffer full? */
  576. /* in this mode we need two bytes of space */
  577. if (uart->buff_in_count > TX_BUFF_SIZE - 2)
  578. break;
  579. if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
  580. break;
  581. #ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
  582. /* select exactly one of the four ports */
  583. addr_byte = (1 << (substream->number + 4)) | 0x08;
  584. #else
  585. /* select any combination of the four ports */
  586. addr_byte = (substream->number << 4) | 0x08;
  587. /* ...except none */
  588. if (addr_byte == 0x08) addr_byte = 0xf8;
  589. #endif
  590. snd_uart16550_output_byte(uart, substream, addr_byte);
  591. /* send midi byte */
  592. snd_uart16550_output_byte(uart, substream, midi_byte);
  593. }
  594. } else {
  595. first = 0;
  596. while( 1 == snd_rawmidi_transmit_peek(substream, &midi_byte, 1) ) {
  597. /* Also send F5 after 3 seconds with no data to handle device disconnect */
  598. if (first == 0 && (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
  599. uart->adaptor == SNDRV_SERIAL_GENERIC) &&
  600. (uart->prev_out != substream->number || jiffies-lasttime > 3*HZ)) {
  601. if( snd_uart16550_buffer_can_write( uart, 3 ) ) {
  602. /* Roland Soundcanvas part selection */
  603. /* If this substream of the data is different previous
  604. substream in this uart, send the change part event */
  605. uart->prev_out = substream->number;
  606. /* change part */
  607. snd_uart16550_output_byte(uart, substream, 0xf5);
  608. /* data */
  609. snd_uart16550_output_byte(uart, substream, uart->prev_out + 1);
  610. /* If midi_byte is a data byte, send the previous status byte */
  611. if ((midi_byte < 0x80) && (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS))
  612. snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
  613. } else if( !uart->drop_on_full )
  614. break;
  615. }
  616. /* send midi byte */
  617. if( !snd_uart16550_output_byte(uart, substream, midi_byte) && !uart->drop_on_full )
  618. break;
  619. if (midi_byte >= 0x80 && midi_byte < 0xf0)
  620. uart->prev_status[uart->prev_out] = midi_byte;
  621. first = 1;
  622. snd_rawmidi_transmit_ack( substream, 1 );
  623. }
  624. lasttime = jiffies;
  625. }
  626. spin_unlock_irqrestore(&uart->open_lock, flags);
  627. }
  628. static void snd_uart16550_output_trigger(snd_rawmidi_substream_t * substream, int up)
  629. {
  630. unsigned long flags;
  631. snd_uart16550_t *uart = substream->rmidi->private_data;
  632. spin_lock_irqsave(&uart->open_lock, flags);
  633. if (up) {
  634. uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
  635. } else {
  636. uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
  637. }
  638. spin_unlock_irqrestore(&uart->open_lock, flags);
  639. if (up)
  640. snd_uart16550_output_write(substream);
  641. }
  642. static snd_rawmidi_ops_t snd_uart16550_output =
  643. {
  644. .open = snd_uart16550_output_open,
  645. .close = snd_uart16550_output_close,
  646. .trigger = snd_uart16550_output_trigger,
  647. };
  648. static snd_rawmidi_ops_t snd_uart16550_input =
  649. {
  650. .open = snd_uart16550_input_open,
  651. .close = snd_uart16550_input_close,
  652. .trigger = snd_uart16550_input_trigger,
  653. };
  654. static int snd_uart16550_free(snd_uart16550_t *uart)
  655. {
  656. if (uart->irq >= 0)
  657. free_irq(uart->irq, (void *)uart);
  658. if (uart->res_base) {
  659. release_resource(uart->res_base);
  660. kfree_nocheck(uart->res_base);
  661. }
  662. kfree(uart);
  663. return 0;
  664. };
  665. static int snd_uart16550_dev_free(snd_device_t *device)
  666. {
  667. snd_uart16550_t *uart = device->device_data;
  668. return snd_uart16550_free(uart);
  669. }
  670. static int __init snd_uart16550_create(snd_card_t * card,
  671. unsigned long iobase,
  672. int irq,
  673. unsigned int speed,
  674. unsigned int base,
  675. int adaptor,
  676. int droponfull,
  677. snd_uart16550_t **ruart)
  678. {
  679. static snd_device_ops_t ops = {
  680. .dev_free = snd_uart16550_dev_free,
  681. };
  682. snd_uart16550_t *uart;
  683. int err;
  684. if ((uart = kcalloc(1, sizeof(*uart), GFP_KERNEL)) == NULL)
  685. return -ENOMEM;
  686. uart->adaptor = adaptor;
  687. uart->card = card;
  688. spin_lock_init(&uart->open_lock);
  689. uart->irq = -1;
  690. uart->base = iobase;
  691. uart->drop_on_full = droponfull;
  692. if ((err = snd_uart16550_detect(uart)) <= 0) {
  693. printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
  694. return err;
  695. }
  696. if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
  697. if (request_irq(irq, snd_uart16550_interrupt,
  698. SA_INTERRUPT, "Serial MIDI", (void *) uart)) {
  699. snd_printk("irq %d busy. Using Polling.\n", irq);
  700. } else {
  701. uart->irq = irq;
  702. }
  703. }
  704. uart->divisor = base / speed;
  705. uart->speed = base / (unsigned int)uart->divisor;
  706. uart->speed_base = base;
  707. uart->prev_out = -1;
  708. uart->prev_in = 0;
  709. uart->rstatus = 0;
  710. memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
  711. init_timer(&uart->buffer_timer);
  712. uart->buffer_timer.function = snd_uart16550_buffer_timer;
  713. uart->buffer_timer.data = (unsigned long)uart;
  714. uart->timer_running = 0;
  715. /* Register device */
  716. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops)) < 0) {
  717. snd_uart16550_free(uart);
  718. return err;
  719. }
  720. switch (uart->adaptor) {
  721. case SNDRV_SERIAL_MS124W_SA:
  722. case SNDRV_SERIAL_MS124W_MB:
  723. /* MS-124W can draw power from RTS and DTR if they
  724. are in opposite states. */
  725. outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
  726. break;
  727. case SNDRV_SERIAL_MS124T:
  728. /* MS-124T can draw power from RTS and/or DTR (preferably
  729. both) if they are asserted. */
  730. outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
  731. break;
  732. default:
  733. break;
  734. }
  735. if (ruart)
  736. *ruart = uart;
  737. return 0;
  738. }
  739. static void __init snd_uart16550_substreams(snd_rawmidi_str_t *stream)
  740. {
  741. struct list_head *list;
  742. list_for_each(list, &stream->substreams) {
  743. snd_rawmidi_substream_t *substream = list_entry(list, snd_rawmidi_substream_t, list);
  744. sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
  745. }
  746. }
  747. static int __init snd_uart16550_rmidi(snd_uart16550_t *uart, int device, int outs, int ins, snd_rawmidi_t **rmidi)
  748. {
  749. snd_rawmidi_t *rrawmidi;
  750. int err;
  751. if ((err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device, outs, ins, &rrawmidi)) < 0)
  752. return err;
  753. snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_uart16550_input);
  754. snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_uart16550_output);
  755. strcpy(rrawmidi->name, "Serial MIDI");
  756. snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
  757. snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
  758. rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  759. SNDRV_RAWMIDI_INFO_INPUT |
  760. SNDRV_RAWMIDI_INFO_DUPLEX;
  761. rrawmidi->private_data = uart;
  762. if (rmidi)
  763. *rmidi = rrawmidi;
  764. return 0;
  765. }
  766. static int __init snd_serial_probe(int dev)
  767. {
  768. snd_card_t *card;
  769. snd_uart16550_t *uart;
  770. int err;
  771. if (!enable[dev])
  772. return -ENOENT;
  773. switch (adaptor[dev]) {
  774. case SNDRV_SERIAL_SOUNDCANVAS:
  775. ins[dev] = 1;
  776. break;
  777. case SNDRV_SERIAL_MS124T:
  778. case SNDRV_SERIAL_MS124W_SA:
  779. outs[dev] = 1;
  780. ins[dev] = 1;
  781. break;
  782. case SNDRV_SERIAL_MS124W_MB:
  783. outs[dev] = 16;
  784. ins[dev] = 1;
  785. break;
  786. case SNDRV_SERIAL_GENERIC:
  787. break;
  788. default:
  789. snd_printk("Adaptor type is out of range 0-%d (%d)\n",
  790. SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
  791. return -ENODEV;
  792. }
  793. if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
  794. snd_printk("Count of outputs is out of range 1-%d (%d)\n",
  795. SNDRV_SERIAL_MAX_OUTS, outs[dev]);
  796. return -ENODEV;
  797. }
  798. if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
  799. snd_printk("Count of inputs is out of range 1-%d (%d)\n",
  800. SNDRV_SERIAL_MAX_INS, ins[dev]);
  801. return -ENODEV;
  802. }
  803. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  804. if (card == NULL)
  805. return -ENOMEM;
  806. strcpy(card->driver, "Serial");
  807. strcpy(card->shortname, "Serial MIDI (UART16550A)");
  808. if ((err = snd_uart16550_create(card,
  809. port[dev],
  810. irq[dev],
  811. speed[dev],
  812. base[dev],
  813. adaptor[dev],
  814. droponfull[dev],
  815. &uart)) < 0) {
  816. snd_card_free(card);
  817. return err;
  818. }
  819. if ((err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi)) < 0) {
  820. snd_card_free(card);
  821. return err;
  822. }
  823. sprintf(card->longname, "%s at 0x%lx, irq %d speed %d div %d outs %d ins %d adaptor %s droponfull %d",
  824. card->shortname,
  825. uart->base,
  826. uart->irq,
  827. uart->speed,
  828. (int)uart->divisor,
  829. outs[dev],
  830. ins[dev],
  831. adaptor_names[uart->adaptor],
  832. uart->drop_on_full);
  833. if ((err = snd_card_register(card)) < 0) {
  834. snd_card_free(card);
  835. return err;
  836. }
  837. snd_serial_cards[dev] = card;
  838. return 0;
  839. }
  840. static int __init alsa_card_serial_init(void)
  841. {
  842. int dev = 0;
  843. int cards = 0;
  844. for (dev = 0; dev < SNDRV_CARDS; dev++) {
  845. if (snd_serial_probe(dev) == 0)
  846. cards++;
  847. }
  848. if (cards == 0) {
  849. #ifdef MODULE
  850. printk(KERN_ERR "serial midi soundcard not found or device busy\n");
  851. #endif
  852. return -ENODEV;
  853. }
  854. return 0;
  855. }
  856. static void __exit alsa_card_serial_exit(void)
  857. {
  858. int dev;
  859. for (dev = 0; dev < SNDRV_CARDS; dev++) {
  860. if (snd_serial_cards[dev] != NULL)
  861. snd_card_free(snd_serial_cards[dev]);
  862. }
  863. }
  864. module_init(alsa_card_serial_init)
  865. module_exit(alsa_card_serial_exit)