serial-u16550.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. timer_delete(&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. guard(spinlock)(&uart->open_lock);
  245. if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  246. return IRQ_NONE;
  247. /* indicate to the UART that the interrupt has been serviced */
  248. inb(uart->base + UART_IIR);
  249. snd_uart16550_io_loop(uart);
  250. return IRQ_HANDLED;
  251. }
  252. /* When the polling mode, this function calls snd_uart16550_io_loop. */
  253. static void snd_uart16550_buffer_timer(struct timer_list *t)
  254. {
  255. struct snd_uart16550 *uart;
  256. uart = timer_container_of(uart, t, buffer_timer);
  257. guard(spinlock_irqsave)(&uart->open_lock);
  258. snd_uart16550_del_timer(uart);
  259. snd_uart16550_io_loop(uart);
  260. }
  261. /*
  262. * this method probes, if an uart sits on given port
  263. * return 0 if found
  264. * return negative error if not found
  265. */
  266. static int snd_uart16550_detect(struct snd_uart16550 *uart)
  267. {
  268. unsigned long io_base = uart->base;
  269. int ok;
  270. unsigned char c;
  271. /* Do some vague tests for the presence of the uart */
  272. if (io_base == 0 || io_base == SNDRV_AUTO_PORT) {
  273. return -ENODEV; /* Not configured */
  274. }
  275. if (!devm_request_region(uart->card->dev, io_base, 8, "Serial MIDI")) {
  276. dev_err(uart->card->dev,
  277. "u16550: can't grab port 0x%lx\n", io_base);
  278. return -EBUSY;
  279. }
  280. /* uart detected unless one of the following tests should fail */
  281. ok = 1;
  282. /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */
  283. outb(UART_LCR_WLEN8, io_base + UART_LCR); /* Line Control Register */
  284. c = inb(io_base + UART_IER);
  285. /* The top four bits of the IER should always == 0 */
  286. if ((c & 0xf0) != 0)
  287. ok = 0; /* failed */
  288. outb(0xaa, io_base + UART_SCR);
  289. /* Write arbitrary data into the scratch reg */
  290. c = inb(io_base + UART_SCR);
  291. /* If it comes back, it's OK */
  292. if (c != 0xaa)
  293. ok = 0; /* failed */
  294. outb(0x55, io_base + UART_SCR);
  295. /* Write arbitrary data into the scratch reg */
  296. c = inb(io_base + UART_SCR);
  297. /* If it comes back, it's OK */
  298. if (c != 0x55)
  299. ok = 0; /* failed */
  300. return ok;
  301. }
  302. static void snd_uart16550_do_open(struct snd_uart16550 * uart)
  303. {
  304. char byte;
  305. /* Initialize basic variables */
  306. uart->buff_in_count = 0;
  307. uart->buff_in = 0;
  308. uart->buff_out = 0;
  309. uart->fifo_limit = 1;
  310. uart->fifo_count = 0;
  311. uart->timer_running = 0;
  312. outb(UART_FCR_ENABLE_FIFO /* Enable FIFO's (if available) */
  313. | UART_FCR_CLEAR_RCVR /* Clear receiver FIFO */
  314. | UART_FCR_CLEAR_XMIT /* Clear transmitter FIFO */
  315. | UART_FCR_TRIGGER_4 /* Set FIFO trigger at 4-bytes */
  316. /* NOTE: interrupt generated after T=(time)4-bytes
  317. * if less than UART_FCR_TRIGGER bytes received
  318. */
  319. ,uart->base + UART_FCR); /* FIFO Control Register */
  320. if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0)
  321. uart->fifo_limit = 16;
  322. if (uart->divisor != 0) {
  323. uart->old_line_ctrl_reg = inb(uart->base + UART_LCR);
  324. outb(UART_LCR_DLAB /* Divisor latch access bit */
  325. ,uart->base + UART_LCR); /* Line Control Register */
  326. uart->old_divisor_lsb = inb(uart->base + UART_DLL);
  327. uart->old_divisor_msb = inb(uart->base + UART_DLM);
  328. outb(uart->divisor
  329. ,uart->base + UART_DLL); /* Divisor Latch Low */
  330. outb(0
  331. ,uart->base + UART_DLM); /* Divisor Latch High */
  332. /* DLAB is reset to 0 in next outb() */
  333. }
  334. /* Set serial parameters (parity off, etc) */
  335. outb(UART_LCR_WLEN8 /* 8 data-bits */
  336. | 0 /* 1 stop-bit */
  337. | 0 /* parity off */
  338. | 0 /* DLAB = 0 */
  339. ,uart->base + UART_LCR); /* Line Control Register */
  340. switch (uart->adaptor) {
  341. default:
  342. outb(UART_MCR_RTS /* Set Request-To-Send line active */
  343. | UART_MCR_DTR /* Set Data-Terminal-Ready line active */
  344. | UART_MCR_OUT2 /* Set OUT2 - not always required, but when
  345. * it is, it is ESSENTIAL for enabling interrupts
  346. */
  347. ,uart->base + UART_MCR); /* Modem Control Register */
  348. break;
  349. case SNDRV_SERIAL_MS124W_SA:
  350. case SNDRV_SERIAL_MS124W_MB:
  351. /* MS-124W can draw power from RTS and DTR if they
  352. are in opposite states. */
  353. outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2,
  354. uart->base + UART_MCR);
  355. break;
  356. case SNDRV_SERIAL_MS124T:
  357. /* MS-124T can draw power from RTS and/or DTR (preferably
  358. both) if they are both asserted. */
  359. outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2,
  360. uart->base + UART_MCR);
  361. break;
  362. }
  363. if (uart->irq < 0) {
  364. byte = (0 & UART_IER_RDI) /* Disable Receiver data interrupt */
  365. |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
  366. ;
  367. } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
  368. byte = UART_IER_RDI /* Enable Receiver data interrupt */
  369. | UART_IER_MSI /* Enable Modem status interrupt */
  370. ;
  371. } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
  372. byte = UART_IER_RDI /* Enable Receiver data interrupt */
  373. | UART_IER_MSI /* Enable Modem status interrupt */
  374. | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
  375. ;
  376. } else {
  377. byte = UART_IER_RDI /* Enable Receiver data interrupt */
  378. | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
  379. ;
  380. }
  381. outb(byte, uart->base + UART_IER); /* Interrupt enable Register */
  382. inb(uart->base + UART_LSR); /* Clear any pre-existing overrun indication */
  383. inb(uart->base + UART_IIR); /* Clear any pre-existing transmit interrupt */
  384. inb(uart->base + UART_RX); /* Clear any pre-existing receive interrupt */
  385. }
  386. static void snd_uart16550_do_close(struct snd_uart16550 * uart)
  387. {
  388. if (uart->irq < 0)
  389. snd_uart16550_del_timer(uart);
  390. /* NOTE: may need to disable interrupts before de-registering out handler.
  391. * For now, the consequences are harmless.
  392. */
  393. outb((0 & UART_IER_RDI) /* Disable Receiver data interrupt */
  394. |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
  395. ,uart->base + UART_IER); /* Interrupt enable Register */
  396. switch (uart->adaptor) {
  397. default:
  398. outb((0 & UART_MCR_RTS) /* Deactivate Request-To-Send line */
  399. |(0 & UART_MCR_DTR) /* Deactivate Data-Terminal-Ready line */
  400. |(0 & UART_MCR_OUT2) /* Deactivate OUT2 */
  401. ,uart->base + UART_MCR); /* Modem Control Register */
  402. break;
  403. case SNDRV_SERIAL_MS124W_SA:
  404. case SNDRV_SERIAL_MS124W_MB:
  405. /* MS-124W can draw power from RTS and DTR if they
  406. are in opposite states; leave it powered. */
  407. outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2),
  408. uart->base + UART_MCR);
  409. break;
  410. case SNDRV_SERIAL_MS124T:
  411. /* MS-124T can draw power from RTS and/or DTR (preferably
  412. both) if they are both asserted; leave it powered. */
  413. outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2),
  414. uart->base + UART_MCR);
  415. break;
  416. }
  417. inb(uart->base + UART_IIR); /* Clear any outstanding interrupts */
  418. /* Restore old divisor */
  419. if (uart->divisor != 0) {
  420. outb(UART_LCR_DLAB /* Divisor latch access bit */
  421. ,uart->base + UART_LCR); /* Line Control Register */
  422. outb(uart->old_divisor_lsb
  423. ,uart->base + UART_DLL); /* Divisor Latch Low */
  424. outb(uart->old_divisor_msb
  425. ,uart->base + UART_DLM); /* Divisor Latch High */
  426. /* Restore old LCR (data bits, stop bits, parity, DLAB) */
  427. outb(uart->old_line_ctrl_reg
  428. ,uart->base + UART_LCR); /* Line Control Register */
  429. }
  430. }
  431. static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
  432. {
  433. struct snd_uart16550 *uart = substream->rmidi->private_data;
  434. guard(spinlock_irqsave)(&uart->open_lock);
  435. if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  436. snd_uart16550_do_open(uart);
  437. uart->filemode |= SERIAL_MODE_INPUT_OPEN;
  438. uart->midi_input[substream->number] = substream;
  439. return 0;
  440. }
  441. static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
  442. {
  443. struct snd_uart16550 *uart = substream->rmidi->private_data;
  444. guard(spinlock_irqsave)(&uart->open_lock);
  445. uart->filemode &= ~SERIAL_MODE_INPUT_OPEN;
  446. uart->midi_input[substream->number] = NULL;
  447. if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  448. snd_uart16550_do_close(uart);
  449. return 0;
  450. }
  451. static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream,
  452. int up)
  453. {
  454. struct snd_uart16550 *uart = substream->rmidi->private_data;
  455. guard(spinlock_irqsave)(&uart->open_lock);
  456. if (up)
  457. uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
  458. else
  459. uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
  460. }
  461. static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
  462. {
  463. struct snd_uart16550 *uart = substream->rmidi->private_data;
  464. guard(spinlock_irqsave)(&uart->open_lock);
  465. if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  466. snd_uart16550_do_open(uart);
  467. uart->filemode |= SERIAL_MODE_OUTPUT_OPEN;
  468. uart->midi_output[substream->number] = substream;
  469. return 0;
  470. };
  471. static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
  472. {
  473. struct snd_uart16550 *uart = substream->rmidi->private_data;
  474. guard(spinlock_irqsave)(&uart->open_lock);
  475. uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN;
  476. uart->midi_output[substream->number] = NULL;
  477. if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  478. snd_uart16550_do_close(uart);
  479. return 0;
  480. };
  481. static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart,
  482. int Num)
  483. {
  484. if (uart->buff_in_count + Num < TX_BUFF_SIZE)
  485. return 1;
  486. else
  487. return 0;
  488. }
  489. static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart,
  490. unsigned char byte)
  491. {
  492. unsigned short buff_in = uart->buff_in;
  493. if (uart->buff_in_count < TX_BUFF_SIZE) {
  494. uart->tx_buff[buff_in] = byte;
  495. buff_in++;
  496. buff_in &= TX_BUFF_MASK;
  497. uart->buff_in = buff_in;
  498. uart->buff_in_count++;
  499. if (uart->irq < 0) /* polling mode */
  500. snd_uart16550_add_timer(uart);
  501. return 1;
  502. } else
  503. return 0;
  504. }
  505. static int snd_uart16550_output_byte(struct snd_uart16550 *uart,
  506. struct snd_rawmidi_substream *substream,
  507. unsigned char midi_byte)
  508. {
  509. if (uart->buff_in_count == 0 /* Buffer empty? */
  510. && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
  511. uart->adaptor != SNDRV_SERIAL_GENERIC) ||
  512. (uart->fifo_count == 0 /* FIFO empty? */
  513. && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) { /* CTS? */
  514. /* Tx Buffer Empty - try to write immediately */
  515. if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
  516. /* Transmitter holding register (and Tx FIFO) empty */
  517. uart->fifo_count = 1;
  518. outb(midi_byte, uart->base + UART_TX);
  519. } else {
  520. if (uart->fifo_count < uart->fifo_limit) {
  521. uart->fifo_count++;
  522. outb(midi_byte, uart->base + UART_TX);
  523. } else {
  524. /* Cannot write (buffer empty) -
  525. * put char in buffer */
  526. snd_uart16550_write_buffer(uart, midi_byte);
  527. }
  528. }
  529. } else {
  530. if (!snd_uart16550_write_buffer(uart, midi_byte)) {
  531. dev_warn(uart->card->dev,
  532. "%s: Buffer overrun on device at 0x%lx\n",
  533. uart->rmidi->name, uart->base);
  534. return 0;
  535. }
  536. }
  537. return 1;
  538. }
  539. static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
  540. {
  541. unsigned char midi_byte, addr_byte;
  542. struct snd_uart16550 *uart = substream->rmidi->private_data;
  543. char first;
  544. static unsigned long lasttime = 0;
  545. /* Interrupts are disabled during the updating of the tx_buff,
  546. * since it is 'bad' to have two processes updating the same
  547. * variables (ie buff_in & buff_out)
  548. */
  549. guard(spinlock_irqsave)(&uart->open_lock);
  550. if (uart->irq < 0) /* polling */
  551. snd_uart16550_io_loop(uart);
  552. if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
  553. while (1) {
  554. /* buffer full? */
  555. /* in this mode we need two bytes of space */
  556. if (uart->buff_in_count > TX_BUFF_SIZE - 2)
  557. break;
  558. if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
  559. break;
  560. #ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
  561. /* select exactly one of the four ports */
  562. addr_byte = (1 << (substream->number + 4)) | 0x08;
  563. #else
  564. /* select any combination of the four ports */
  565. addr_byte = (substream->number << 4) | 0x08;
  566. /* ...except none */
  567. if (addr_byte == 0x08)
  568. addr_byte = 0xf8;
  569. #endif
  570. snd_uart16550_output_byte(uart, substream, addr_byte);
  571. /* send midi byte */
  572. snd_uart16550_output_byte(uart, substream, midi_byte);
  573. }
  574. } else {
  575. first = 0;
  576. while (snd_rawmidi_transmit_peek(substream, &midi_byte, 1) == 1) {
  577. /* Also send F5 after 3 seconds with no data
  578. * to handle device disconnect */
  579. if (first == 0 &&
  580. (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
  581. uart->adaptor == SNDRV_SERIAL_GENERIC) &&
  582. (uart->prev_out != substream->number ||
  583. time_after(jiffies, lasttime + 3*HZ))) {
  584. if (snd_uart16550_buffer_can_write(uart, 3)) {
  585. /* Roland Soundcanvas part selection */
  586. /* If this substream of the data is
  587. * different previous substream
  588. * in this uart, send the change part
  589. * event
  590. */
  591. uart->prev_out = substream->number;
  592. /* change part */
  593. snd_uart16550_output_byte(uart, substream,
  594. 0xf5);
  595. /* data */
  596. snd_uart16550_output_byte(uart, substream,
  597. uart->prev_out + 1);
  598. /* If midi_byte is a data byte,
  599. * send the previous status byte */
  600. if (midi_byte < 0x80 &&
  601. uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS)
  602. snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
  603. } else if (!uart->drop_on_full)
  604. break;
  605. }
  606. /* send midi byte */
  607. if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
  608. !uart->drop_on_full )
  609. break;
  610. if (midi_byte >= 0x80 && midi_byte < 0xf0)
  611. uart->prev_status[uart->prev_out] = midi_byte;
  612. first = 1;
  613. snd_rawmidi_transmit_ack( substream, 1 );
  614. }
  615. lasttime = jiffies;
  616. }
  617. }
  618. static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
  619. int up)
  620. {
  621. struct snd_uart16550 *uart = substream->rmidi->private_data;
  622. scoped_guard(spinlock_irqsave, &uart->open_lock) {
  623. if (up)
  624. uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
  625. else
  626. uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
  627. }
  628. if (up)
  629. snd_uart16550_output_write(substream);
  630. }
  631. static const struct snd_rawmidi_ops snd_uart16550_output =
  632. {
  633. .open = snd_uart16550_output_open,
  634. .close = snd_uart16550_output_close,
  635. .trigger = snd_uart16550_output_trigger,
  636. };
  637. static const struct snd_rawmidi_ops snd_uart16550_input =
  638. {
  639. .open = snd_uart16550_input_open,
  640. .close = snd_uart16550_input_close,
  641. .trigger = snd_uart16550_input_trigger,
  642. };
  643. static int snd_uart16550_create(struct snd_card *card,
  644. unsigned long iobase,
  645. int irq,
  646. unsigned int speed,
  647. unsigned int base,
  648. int adaptor,
  649. int droponfull,
  650. struct snd_uart16550 **ruart)
  651. {
  652. struct snd_uart16550 *uart;
  653. int err;
  654. uart = devm_kzalloc(card->dev, sizeof(*uart), GFP_KERNEL);
  655. if (!uart)
  656. return -ENOMEM;
  657. uart->adaptor = adaptor;
  658. uart->card = card;
  659. spin_lock_init(&uart->open_lock);
  660. uart->irq = -1;
  661. uart->base = iobase;
  662. uart->drop_on_full = droponfull;
  663. err = snd_uart16550_detect(uart);
  664. if (err <= 0) {
  665. dev_err(card->dev, "no UART detected at 0x%lx\n", iobase);
  666. return -ENODEV;
  667. }
  668. if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
  669. if (devm_request_irq(card->dev, irq, snd_uart16550_interrupt,
  670. 0, "Serial MIDI", uart)) {
  671. dev_warn(card->dev,
  672. "irq %d busy. Using Polling.\n", irq);
  673. } else {
  674. uart->irq = irq;
  675. }
  676. }
  677. uart->divisor = base / speed;
  678. uart->speed = base / (unsigned int)uart->divisor;
  679. uart->speed_base = base;
  680. uart->prev_out = -1;
  681. uart->prev_in = 0;
  682. uart->rstatus = 0;
  683. memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
  684. timer_setup(&uart->buffer_timer, snd_uart16550_buffer_timer, 0);
  685. uart->timer_running = 0;
  686. switch (uart->adaptor) {
  687. case SNDRV_SERIAL_MS124W_SA:
  688. case SNDRV_SERIAL_MS124W_MB:
  689. /* MS-124W can draw power from RTS and DTR if they
  690. are in opposite states. */
  691. outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
  692. break;
  693. case SNDRV_SERIAL_MS124T:
  694. /* MS-124T can draw power from RTS and/or DTR (preferably
  695. both) if they are asserted. */
  696. outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
  697. break;
  698. default:
  699. break;
  700. }
  701. if (ruart)
  702. *ruart = uart;
  703. return 0;
  704. }
  705. static void snd_uart16550_substreams(struct snd_rawmidi_str *stream)
  706. {
  707. struct snd_rawmidi_substream *substream;
  708. list_for_each_entry(substream, &stream->substreams, list) {
  709. sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
  710. }
  711. }
  712. static int snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
  713. int outs, int ins,
  714. struct snd_rawmidi **rmidi)
  715. {
  716. struct snd_rawmidi *rrawmidi;
  717. int err;
  718. err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
  719. outs, ins, &rrawmidi);
  720. if (err < 0)
  721. return err;
  722. snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  723. &snd_uart16550_input);
  724. snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  725. &snd_uart16550_output);
  726. strscpy(rrawmidi->name, "Serial MIDI");
  727. snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
  728. snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
  729. rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  730. SNDRV_RAWMIDI_INFO_INPUT |
  731. SNDRV_RAWMIDI_INFO_DUPLEX;
  732. rrawmidi->private_data = uart;
  733. if (rmidi)
  734. *rmidi = rrawmidi;
  735. return 0;
  736. }
  737. static int snd_serial_probe(struct platform_device *devptr)
  738. {
  739. struct snd_card *card;
  740. struct snd_uart16550 *uart;
  741. int err;
  742. int dev = devptr->id;
  743. switch (adaptor[dev]) {
  744. case SNDRV_SERIAL_SOUNDCANVAS:
  745. ins[dev] = 1;
  746. break;
  747. case SNDRV_SERIAL_MS124T:
  748. case SNDRV_SERIAL_MS124W_SA:
  749. outs[dev] = 1;
  750. ins[dev] = 1;
  751. break;
  752. case SNDRV_SERIAL_MS124W_MB:
  753. outs[dev] = 16;
  754. ins[dev] = 1;
  755. break;
  756. case SNDRV_SERIAL_GENERIC:
  757. break;
  758. default:
  759. dev_err(&devptr->dev,
  760. "Adaptor type is out of range 0-%d (%d)\n",
  761. SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
  762. return -ENODEV;
  763. }
  764. if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
  765. dev_err(&devptr->dev,
  766. "Count of outputs is out of range 1-%d (%d)\n",
  767. SNDRV_SERIAL_MAX_OUTS, outs[dev]);
  768. return -ENODEV;
  769. }
  770. if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
  771. dev_err(&devptr->dev,
  772. "Count of inputs is out of range 1-%d (%d)\n",
  773. SNDRV_SERIAL_MAX_INS, ins[dev]);
  774. return -ENODEV;
  775. }
  776. err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
  777. 0, &card);
  778. if (err < 0)
  779. return err;
  780. strscpy(card->driver, "Serial");
  781. strscpy(card->shortname, "Serial MIDI (UART16550A)");
  782. err = snd_uart16550_create(card, port[dev], irq[dev], speed[dev],
  783. base[dev], adaptor[dev], droponfull[dev],
  784. &uart);
  785. if (err < 0)
  786. return err;
  787. err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
  788. if (err < 0)
  789. return err;
  790. sprintf(card->longname, "%s [%s] at %#lx, irq %d",
  791. card->shortname,
  792. adaptor_names[uart->adaptor],
  793. uart->base,
  794. uart->irq);
  795. err = snd_card_register(card);
  796. if (err < 0)
  797. return err;
  798. platform_set_drvdata(devptr, card);
  799. return 0;
  800. }
  801. #define SND_SERIAL_DRIVER "snd_serial_u16550"
  802. static struct platform_driver snd_serial_driver = {
  803. .probe = snd_serial_probe,
  804. .driver = {
  805. .name = SND_SERIAL_DRIVER,
  806. },
  807. };
  808. static void snd_serial_unregister_all(void)
  809. {
  810. int i;
  811. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  812. platform_device_unregister(devices[i]);
  813. platform_driver_unregister(&snd_serial_driver);
  814. }
  815. static int __init alsa_card_serial_init(void)
  816. {
  817. int i, cards, err;
  818. err = platform_driver_register(&snd_serial_driver);
  819. if (err < 0)
  820. return err;
  821. cards = 0;
  822. for (i = 0; i < SNDRV_CARDS; i++) {
  823. struct platform_device *device;
  824. if (! enable[i])
  825. continue;
  826. device = platform_device_register_simple(SND_SERIAL_DRIVER,
  827. i, NULL, 0);
  828. if (IS_ERR(device))
  829. continue;
  830. if (!platform_get_drvdata(device)) {
  831. platform_device_unregister(device);
  832. continue;
  833. }
  834. devices[i] = device;
  835. cards++;
  836. }
  837. if (! cards) {
  838. #ifdef MODULE
  839. pr_err("serial midi soundcard not found or device busy\n");
  840. #endif
  841. snd_serial_unregister_all();
  842. return -ENODEV;
  843. }
  844. return 0;
  845. }
  846. static void __exit alsa_card_serial_exit(void)
  847. {
  848. snd_serial_unregister_all();
  849. }
  850. module_init(alsa_card_serial_init)
  851. module_exit(alsa_card_serial_exit)