serial-u16550.c 30 KB

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