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