~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/drivers/serial-u16550.c

Version: ~ [ linux-5.4-rc7 ] ~ [ linux-5.3.11 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.84 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.154 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.201 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.201 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.77 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp