serial-u16550.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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/module.h>
  39. #include <linux/io.h>
  40. #include <sound/core.h>
  41. #include <sound/rawmidi.h>
  42. #include <sound/initval.h>
  43. #include <linux/serial_reg.h>
  44. #include <linux/jiffies.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 bool 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 bool 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. mod_timer(&uart->buffer_timer, jiffies + (HZ + 255) / 256);
  149. uart->timer_running = 1;
  150. }
  151. }
  152. static inline void snd_uart16550_del_timer(struct snd_uart16550 *uart)
  153. {
  154. if (uart->timer_running) {
  155. del_timer(&uart->buffer_timer);
  156. uart->timer_running = 0;
  157. }
  158. }
  159. /* This macro is only used in snd_uart16550_io_loop */
  160. static inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart)
  161. {
  162. unsigned short buff_out = uart->buff_out;
  163. if (uart->buff_in_count > 0) {
  164. outb(uart->tx_buff[buff_out], uart->base + UART_TX);
  165. uart->fifo_count++;
  166. buff_out++;
  167. buff_out &= TX_BUFF_MASK;
  168. uart->buff_out = buff_out;
  169. uart->buff_in_count--;
  170. }
  171. }
  172. /* This loop should be called with interrupts disabled
  173. * We don't want to interrupt this,
  174. * as we're already handling an interrupt
  175. */
  176. static void snd_uart16550_io_loop(struct snd_uart16550 * uart)
  177. {
  178. unsigned char c, status;
  179. int substream;
  180. /* recall previous stream */
  181. substream = uart->prev_in;
  182. /* Read Loop */
  183. while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) {
  184. /* while receive data ready */
  185. c = inb(uart->base + UART_RX);
  186. /* keep track of last status byte */
  187. if (c & 0x80)
  188. uart->rstatus = c;
  189. /* handle stream switch */
  190. if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
  191. if (uart->rstatus == 0xf5) {
  192. if (c <= SNDRV_SERIAL_MAX_INS && c > 0)
  193. substream = c - 1;
  194. if (c != 0xf5)
  195. /* prevent future bytes from being
  196. interpreted as streams */
  197. uart->rstatus = 0;
  198. } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN)
  199. && uart->midi_input[substream])
  200. snd_rawmidi_receive(uart->midi_input[substream],
  201. &c, 1);
  202. } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) &&
  203. uart->midi_input[substream])
  204. snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
  205. if (status & UART_LSR_OE)
  206. snd_printk(KERN_WARNING
  207. "%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 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); /* Interrupt 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); /* Interrupt 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(KERN_WARNING
  562. "%s: Buffer overrun on device at 0x%lx\n",
  563. uart->rmidi->name, uart->base);
  564. return 0;
  565. }
  566. }
  567. return 1;
  568. }
  569. static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
  570. {
  571. unsigned long flags;
  572. unsigned char midi_byte, addr_byte;
  573. struct snd_uart16550 *uart = substream->rmidi->private_data;
  574. char first;
  575. static unsigned long lasttime = 0;
  576. /* Interrupts are disabled during the updating of the tx_buff,
  577. * since it is 'bad' to have two processes updating the same
  578. * variables (ie buff_in & buff_out)
  579. */
  580. spin_lock_irqsave(&uart->open_lock, flags);
  581. if (uart->irq < 0) /* polling */
  582. snd_uart16550_io_loop(uart);
  583. if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
  584. while (1) {
  585. /* buffer full? */
  586. /* in this mode we need two bytes of space */
  587. if (uart->buff_in_count > TX_BUFF_SIZE - 2)
  588. break;
  589. if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
  590. break;
  591. #ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
  592. /* select exactly one of the four ports */
  593. addr_byte = (1 << (substream->number + 4)) | 0x08;
  594. #else
  595. /* select any combination of the four ports */
  596. addr_byte = (substream->number << 4) | 0x08;
  597. /* ...except none */
  598. if (addr_byte == 0x08)
  599. addr_byte = 0xf8;
  600. #endif
  601. snd_uart16550_output_byte(uart, substream, addr_byte);
  602. /* send midi byte */
  603. snd_uart16550_output_byte(uart, substream, midi_byte);
  604. }
  605. } else {
  606. first = 0;
  607. while (snd_rawmidi_transmit_peek(substream, &midi_byte, 1) == 1) {
  608. /* Also send F5 after 3 seconds with no data
  609. * to handle device disconnect */
  610. if (first == 0 &&
  611. (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
  612. uart->adaptor == SNDRV_SERIAL_GENERIC) &&
  613. (uart->prev_out != substream->number ||
  614. time_after(jiffies, lasttime + 3*HZ))) {
  615. if (snd_uart16550_buffer_can_write(uart, 3)) {
  616. /* Roland Soundcanvas part selection */
  617. /* If this substream of the data is
  618. * different previous substream
  619. * in this uart, send the change part
  620. * event
  621. */
  622. uart->prev_out = substream->number;
  623. /* change part */
  624. snd_uart16550_output_byte(uart, substream,
  625. 0xf5);
  626. /* data */
  627. snd_uart16550_output_byte(uart, substream,
  628. uart->prev_out + 1);
  629. /* If midi_byte is a data byte,
  630. * send the previous status byte */
  631. if (midi_byte < 0x80 &&
  632. uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS)
  633. snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
  634. } else if (!uart->drop_on_full)
  635. break;
  636. }
  637. /* send midi byte */
  638. if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
  639. !uart->drop_on_full )
  640. break;
  641. if (midi_byte >= 0x80 && midi_byte < 0xf0)
  642. uart->prev_status[uart->prev_out] = midi_byte;
  643. first = 1;
  644. snd_rawmidi_transmit_ack( substream, 1 );
  645. }
  646. lasttime = jiffies;
  647. }
  648. spin_unlock_irqrestore(&uart->open_lock, flags);
  649. }
  650. static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
  651. int up)
  652. {
  653. unsigned long flags;
  654. struct snd_uart16550 *uart = substream->rmidi->private_data;
  655. spin_lock_irqsave(&uart->open_lock, flags);
  656. if (up)
  657. uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
  658. else
  659. uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
  660. spin_unlock_irqrestore(&uart->open_lock, flags);
  661. if (up)
  662. snd_uart16550_output_write(substream);
  663. }
  664. static struct snd_rawmidi_ops snd_uart16550_output =
  665. {
  666. .open = snd_uart16550_output_open,
  667. .close = snd_uart16550_output_close,
  668. .trigger = snd_uart16550_output_trigger,
  669. };
  670. static struct snd_rawmidi_ops snd_uart16550_input =
  671. {
  672. .open = snd_uart16550_input_open,
  673. .close = snd_uart16550_input_close,
  674. .trigger = snd_uart16550_input_trigger,
  675. };
  676. static int snd_uart16550_free(struct snd_uart16550 *uart)
  677. {
  678. if (uart->irq >= 0)
  679. free_irq(uart->irq, uart);
  680. release_and_free_resource(uart->res_base);
  681. kfree(uart);
  682. return 0;
  683. };
  684. static int snd_uart16550_dev_free(struct snd_device *device)
  685. {
  686. struct snd_uart16550 *uart = device->device_data;
  687. return snd_uart16550_free(uart);
  688. }
  689. static int snd_uart16550_create(struct snd_card *card,
  690. unsigned long iobase,
  691. int irq,
  692. unsigned int speed,
  693. unsigned int base,
  694. int adaptor,
  695. int droponfull,
  696. struct snd_uart16550 **ruart)
  697. {
  698. static struct snd_device_ops ops = {
  699. .dev_free = snd_uart16550_dev_free,
  700. };
  701. struct snd_uart16550 *uart;
  702. int err;
  703. if ((uart = kzalloc(sizeof(*uart), GFP_KERNEL)) == NULL)
  704. return -ENOMEM;
  705. uart->adaptor = adaptor;
  706. uart->card = card;
  707. spin_lock_init(&uart->open_lock);
  708. uart->irq = -1;
  709. uart->base = iobase;
  710. uart->drop_on_full = droponfull;
  711. if ((err = snd_uart16550_detect(uart)) <= 0) {
  712. printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
  713. snd_uart16550_free(uart);
  714. return -ENODEV;
  715. }
  716. if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
  717. if (request_irq(irq, snd_uart16550_interrupt,
  718. 0, "Serial MIDI", uart)) {
  719. snd_printk(KERN_WARNING
  720. "irq %d busy. Using Polling.\n", irq);
  721. } else {
  722. uart->irq = irq;
  723. }
  724. }
  725. uart->divisor = base / speed;
  726. uart->speed = base / (unsigned int)uart->divisor;
  727. uart->speed_base = base;
  728. uart->prev_out = -1;
  729. uart->prev_in = 0;
  730. uart->rstatus = 0;
  731. memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
  732. setup_timer(&uart->buffer_timer, snd_uart16550_buffer_timer,
  733. (unsigned long)uart);
  734. uart->timer_running = 0;
  735. /* Register device */
  736. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops)) < 0) {
  737. snd_uart16550_free(uart);
  738. return err;
  739. }
  740. switch (uart->adaptor) {
  741. case SNDRV_SERIAL_MS124W_SA:
  742. case SNDRV_SERIAL_MS124W_MB:
  743. /* MS-124W can draw power from RTS and DTR if they
  744. are in opposite states. */
  745. outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
  746. break;
  747. case SNDRV_SERIAL_MS124T:
  748. /* MS-124T can draw power from RTS and/or DTR (preferably
  749. both) if they are asserted. */
  750. outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
  751. break;
  752. default:
  753. break;
  754. }
  755. if (ruart)
  756. *ruart = uart;
  757. return 0;
  758. }
  759. static void snd_uart16550_substreams(struct snd_rawmidi_str *stream)
  760. {
  761. struct snd_rawmidi_substream *substream;
  762. list_for_each_entry(substream, &stream->substreams, list) {
  763. sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
  764. }
  765. }
  766. static int snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
  767. int outs, int ins,
  768. struct snd_rawmidi **rmidi)
  769. {
  770. struct snd_rawmidi *rrawmidi;
  771. int err;
  772. err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
  773. outs, ins, &rrawmidi);
  774. if (err < 0)
  775. return err;
  776. snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  777. &snd_uart16550_input);
  778. snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  779. &snd_uart16550_output);
  780. strcpy(rrawmidi->name, "Serial MIDI");
  781. snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
  782. snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
  783. rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  784. SNDRV_RAWMIDI_INFO_INPUT |
  785. SNDRV_RAWMIDI_INFO_DUPLEX;
  786. rrawmidi->private_data = uart;
  787. if (rmidi)
  788. *rmidi = rrawmidi;
  789. return 0;
  790. }
  791. static int snd_serial_probe(struct platform_device *devptr)
  792. {
  793. struct snd_card *card;
  794. struct snd_uart16550 *uart;
  795. int err;
  796. int dev = devptr->id;
  797. switch (adaptor[dev]) {
  798. case SNDRV_SERIAL_SOUNDCANVAS:
  799. ins[dev] = 1;
  800. break;
  801. case SNDRV_SERIAL_MS124T:
  802. case SNDRV_SERIAL_MS124W_SA:
  803. outs[dev] = 1;
  804. ins[dev] = 1;
  805. break;
  806. case SNDRV_SERIAL_MS124W_MB:
  807. outs[dev] = 16;
  808. ins[dev] = 1;
  809. break;
  810. case SNDRV_SERIAL_GENERIC:
  811. break;
  812. default:
  813. snd_printk(KERN_ERR
  814. "Adaptor type is out of range 0-%d (%d)\n",
  815. SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
  816. return -ENODEV;
  817. }
  818. if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
  819. snd_printk(KERN_ERR
  820. "Count of outputs is out of range 1-%d (%d)\n",
  821. SNDRV_SERIAL_MAX_OUTS, outs[dev]);
  822. return -ENODEV;
  823. }
  824. if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
  825. snd_printk(KERN_ERR
  826. "Count of inputs is out of range 1-%d (%d)\n",
  827. SNDRV_SERIAL_MAX_INS, ins[dev]);
  828. return -ENODEV;
  829. }
  830. err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
  831. 0, &card);
  832. if (err < 0)
  833. return err;
  834. strcpy(card->driver, "Serial");
  835. strcpy(card->shortname, "Serial MIDI (UART16550A)");
  836. if ((err = snd_uart16550_create(card,
  837. port[dev],
  838. irq[dev],
  839. speed[dev],
  840. base[dev],
  841. adaptor[dev],
  842. droponfull[dev],
  843. &uart)) < 0)
  844. goto _err;
  845. err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
  846. if (err < 0)
  847. goto _err;
  848. sprintf(card->longname, "%s [%s] at %#lx, irq %d",
  849. card->shortname,
  850. adaptor_names[uart->adaptor],
  851. uart->base,
  852. uart->irq);
  853. if ((err = snd_card_register(card)) < 0)
  854. goto _err;
  855. platform_set_drvdata(devptr, card);
  856. return 0;
  857. _err:
  858. snd_card_free(card);
  859. return err;
  860. }
  861. static int snd_serial_remove(struct platform_device *devptr)
  862. {
  863. snd_card_free(platform_get_drvdata(devptr));
  864. return 0;
  865. }
  866. #define SND_SERIAL_DRIVER "snd_serial_u16550"
  867. static struct platform_driver snd_serial_driver = {
  868. .probe = snd_serial_probe,
  869. .remove = snd_serial_remove,
  870. .driver = {
  871. .name = SND_SERIAL_DRIVER,
  872. },
  873. };
  874. static void snd_serial_unregister_all(void)
  875. {
  876. int i;
  877. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  878. platform_device_unregister(devices[i]);
  879. platform_driver_unregister(&snd_serial_driver);
  880. }
  881. static int __init alsa_card_serial_init(void)
  882. {
  883. int i, cards, err;
  884. if ((err = platform_driver_register(&snd_serial_driver)) < 0)
  885. return err;
  886. cards = 0;
  887. for (i = 0; i < SNDRV_CARDS; i++) {
  888. struct platform_device *device;
  889. if (! enable[i])
  890. continue;
  891. device = platform_device_register_simple(SND_SERIAL_DRIVER,
  892. i, NULL, 0);
  893. if (IS_ERR(device))
  894. continue;
  895. if (!platform_get_drvdata(device)) {
  896. platform_device_unregister(device);
  897. continue;
  898. }
  899. devices[i] = device;
  900. cards++;
  901. }
  902. if (! cards) {
  903. #ifdef MODULE
  904. printk(KERN_ERR "serial midi soundcard not found or device busy\n");
  905. #endif
  906. snd_serial_unregister_all();
  907. return -ENODEV;
  908. }
  909. return 0;
  910. }
  911. static void __exit alsa_card_serial_exit(void)
  912. {
  913. snd_serial_unregister_all();
  914. }
  915. module_init(alsa_card_serial_init)
  916. module_exit(alsa_card_serial_exit)