serial-u16550.c 31 KB

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