serial-u16550.c 30 KB

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