serial-u16550.c 29 KB

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