serial-u16550.c 31 KB

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