serial-u16550.c 31 KB

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