serial-u16550.c 31 KB

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