Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * serial.c |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz>, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/init.h> |
| 34 | #include <linux/interrupt.h> |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 35 | #include <linux/err.h> |
| 36 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/slab.h> |
| 38 | #include <linux/ioport.h> |
Paul Gortmaker | 65a7721 | 2011-07-15 13:13:37 -0400 | [diff] [blame] | 39 | #include <linux/module.h> |
Takashi Iwai | 6cbbfe1 | 2015-01-28 16:49:33 +0100 | [diff] [blame] | 40 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <sound/core.h> |
| 42 | #include <sound/rawmidi.h> |
| 43 | #include <sound/initval.h> |
| 44 | |
| 45 | #include <linux/serial_reg.h> |
Julia Lawall | f11b799 | 2008-01-07 13:33:45 +0100 | [diff] [blame] | 46 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | MODULE_DESCRIPTION("MIDI serial u16550"); |
| 49 | MODULE_LICENSE("GPL"); |
| 50 | MODULE_SUPPORTED_DEVICE("{{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 | #define SNDRV_SERIAL_NORMALBUFF 0 /* Normal blocking buffer operation */ |
| 67 | #define SNDRV_SERIAL_DROPBUFF 1 /* Non-blocking discard operation */ |
| 68 | |
| 69 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ |
| 70 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
Rusty Russell | a67ff6a | 2011-12-15 13:49:36 +1030 | [diff] [blame] | 71 | static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x3f8,0x2f8,0x3e8,0x2e8 */ |
| 73 | static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,4,5,7,9,10,11,14,15 */ |
| 74 | static int speed[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 38400}; /* 9600,19200,38400,57600,115200 */ |
| 75 | static int base[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 115200}; /* baud base */ |
| 76 | static int outs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */ |
| 77 | static int ins[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */ |
| 78 | static int adaptor[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = SNDRV_SERIAL_SOUNDCANVAS}; |
Rusty Russell | a67ff6a | 2011-12-15 13:49:36 +1030 | [diff] [blame] | 79 | static bool droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | module_param_array(index, int, NULL, 0444); |
| 82 | MODULE_PARM_DESC(index, "Index value for Serial MIDI."); |
| 83 | module_param_array(id, charp, NULL, 0444); |
| 84 | MODULE_PARM_DESC(id, "ID string for Serial MIDI."); |
| 85 | module_param_array(enable, bool, NULL, 0444); |
| 86 | MODULE_PARM_DESC(enable, "Enable UART16550A chip."); |
| 87 | module_param_array(port, long, NULL, 0444); |
| 88 | MODULE_PARM_DESC(port, "Port # for UART16550A chip."); |
| 89 | module_param_array(irq, int, NULL, 0444); |
| 90 | MODULE_PARM_DESC(irq, "IRQ # for UART16550A chip."); |
| 91 | module_param_array(speed, int, NULL, 0444); |
| 92 | MODULE_PARM_DESC(speed, "Speed in bauds."); |
| 93 | module_param_array(base, int, NULL, 0444); |
| 94 | MODULE_PARM_DESC(base, "Base for divisor in bauds."); |
| 95 | module_param_array(outs, int, NULL, 0444); |
| 96 | MODULE_PARM_DESC(outs, "Number of MIDI outputs."); |
| 97 | module_param_array(ins, int, NULL, 0444); |
| 98 | MODULE_PARM_DESC(ins, "Number of MIDI inputs."); |
| 99 | module_param_array(droponfull, bool, NULL, 0444); |
| 100 | MODULE_PARM_DESC(droponfull, "Flag to enable drop-on-full buffer mode"); |
| 101 | |
| 102 | module_param_array(adaptor, int, NULL, 0444); |
| 103 | MODULE_PARM_DESC(adaptor, "Type of adaptor."); |
| 104 | |
| 105 | /*#define SNDRV_SERIAL_MS124W_MB_NOCOMBO 1*/ /* Address outs as 0-3 instead of bitmap */ |
| 106 | |
| 107 | #define SNDRV_SERIAL_MAX_OUTS 16 /* max 64, min 16 */ |
| 108 | #define SNDRV_SERIAL_MAX_INS 16 /* max 64, min 16 */ |
| 109 | |
| 110 | #define TX_BUFF_SIZE (1<<15) /* Must be 2^n */ |
| 111 | #define TX_BUFF_MASK (TX_BUFF_SIZE - 1) |
| 112 | |
| 113 | #define SERIAL_MODE_NOT_OPENED (0) |
| 114 | #define SERIAL_MODE_INPUT_OPEN (1 << 0) |
| 115 | #define SERIAL_MODE_OUTPUT_OPEN (1 << 1) |
| 116 | #define SERIAL_MODE_INPUT_TRIGGERED (1 << 2) |
| 117 | #define SERIAL_MODE_OUTPUT_TRIGGERED (1 << 3) |
| 118 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 119 | struct snd_uart16550 { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 120 | struct snd_card *card; |
| 121 | struct snd_rawmidi *rmidi; |
| 122 | struct snd_rawmidi_substream *midi_output[SNDRV_SERIAL_MAX_OUTS]; |
| 123 | struct snd_rawmidi_substream *midi_input[SNDRV_SERIAL_MAX_INS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 125 | int filemode; /* open status of file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | spinlock_t open_lock; |
| 128 | |
| 129 | int irq; |
| 130 | |
| 131 | unsigned long base; |
| 132 | struct resource *res_base; |
| 133 | |
| 134 | unsigned int speed; |
| 135 | unsigned int speed_base; |
| 136 | unsigned char divisor; |
| 137 | |
| 138 | unsigned char old_divisor_lsb; |
| 139 | unsigned char old_divisor_msb; |
| 140 | unsigned char old_line_ctrl_reg; |
| 141 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 142 | /* parameter for using of write loop */ |
| 143 | short int fifo_limit; /* used in uart16550 */ |
| 144 | short int fifo_count; /* used in uart16550 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 146 | /* type of adaptor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | int adaptor; |
| 148 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 149 | /* inputs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | int prev_in; |
| 151 | unsigned char rstatus; |
| 152 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 153 | /* outputs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | int prev_out; |
| 155 | unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS]; |
| 156 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 157 | /* write buffer and its writing/reading position */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | unsigned char tx_buff[TX_BUFF_SIZE]; |
| 159 | int buff_in_count; |
| 160 | int buff_in; |
| 161 | int buff_out; |
| 162 | int drop_on_full; |
| 163 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 164 | /* wait timer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | unsigned int timer_running:1; |
| 166 | struct timer_list buffer_timer; |
| 167 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 168 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 170 | static struct platform_device *devices[SNDRV_CARDS]; |
| 171 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 172 | static inline void snd_uart16550_add_timer(struct snd_uart16550 *uart) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 174 | if (!uart->timer_running) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /* timer 38600bps * 10bit * 16byte */ |
Takashi Iwai | d80d05f | 2015-01-19 11:29:09 +0100 | [diff] [blame] | 176 | mod_timer(&uart->buffer_timer, jiffies + (HZ + 255) / 256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | uart->timer_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 181 | static inline void snd_uart16550_del_timer(struct snd_uart16550 *uart) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | if (uart->timer_running) { |
| 184 | del_timer(&uart->buffer_timer); |
| 185 | uart->timer_running = 0; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* This macro is only used in snd_uart16550_io_loop */ |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 190 | static inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | unsigned short buff_out = uart->buff_out; |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 193 | if (uart->buff_in_count > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | outb(uart->tx_buff[buff_out], uart->base + UART_TX); |
| 195 | uart->fifo_count++; |
| 196 | buff_out++; |
| 197 | buff_out &= TX_BUFF_MASK; |
| 198 | uart->buff_out = buff_out; |
| 199 | uart->buff_in_count--; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* This loop should be called with interrupts disabled |
| 204 | * We don't want to interrupt this, |
| 205 | * as we're already handling an interrupt |
| 206 | */ |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 207 | static void snd_uart16550_io_loop(struct snd_uart16550 * uart) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
| 209 | unsigned char c, status; |
| 210 | int substream; |
| 211 | |
| 212 | /* recall previous stream */ |
| 213 | substream = uart->prev_in; |
| 214 | |
| 215 | /* Read Loop */ |
| 216 | while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) { |
| 217 | /* while receive data ready */ |
| 218 | c = inb(uart->base + UART_RX); |
| 219 | |
| 220 | /* keep track of last status byte */ |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 221 | if (c & 0x80) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | uart->rstatus = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | /* handle stream switch */ |
| 225 | if (uart->adaptor == SNDRV_SERIAL_GENERIC) { |
| 226 | if (uart->rstatus == 0xf5) { |
| 227 | if (c <= SNDRV_SERIAL_MAX_INS && c > 0) |
| 228 | substream = c - 1; |
| 229 | if (c != 0xf5) |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 230 | /* prevent future bytes from being |
| 231 | interpreted as streams */ |
| 232 | uart->rstatus = 0; |
| 233 | } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) |
| 234 | && uart->midi_input[substream]) |
| 235 | snd_rawmidi_receive(uart->midi_input[substream], |
| 236 | &c, 1); |
| 237 | } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) && |
| 238 | uart->midi_input[substream]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | snd_rawmidi_receive(uart->midi_input[substream], &c, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | if (status & UART_LSR_OE) |
Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 242 | snd_printk(KERN_WARNING |
| 243 | "%s: Overrun on device at 0x%lx\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 253 | if (status & UART_LSR_THRE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | uart->fifo_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | if (uart->adaptor == SNDRV_SERIAL_MS124W_SA |
| 256 | || uart->adaptor == SNDRV_SERIAL_GENERIC) { |
| 257 | /* Can't use FIFO, must send only when CTS is true */ |
| 258 | status = inb(uart->base + UART_MSR); |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 259 | while (uart->fifo_count == 0 && (status & UART_MSR_CTS) && |
| 260 | uart->buff_in_count > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | snd_uart16550_buffer_output(uart); |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 262 | status = inb(uart->base + UART_MSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | } else { |
| 265 | /* Write loop */ |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 266 | while (uart->fifo_count < uart->fifo_limit /* Can we write ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | && uart->buff_in_count > 0) /* Do we want to? */ |
| 268 | snd_uart16550_buffer_output(uart); |
| 269 | } |
| 270 | if (uart->irq < 0 && uart->buff_in_count > 0) |
| 271 | snd_uart16550_add_timer(uart); |
| 272 | } |
| 273 | |
| 274 | /* NOTES ON SERVICING INTERUPTS |
| 275 | * --------------------------- |
| 276 | * After receiving a interrupt, it is important to indicate to the UART that |
| 277 | * this has been done. |
| 278 | * For a Rx interrupt, this is done by reading the received byte. |
| 279 | * For a Tx interrupt this is done by either: |
| 280 | * a) Writing a byte |
| 281 | * b) Reading the IIR |
| 282 | * It is particularly important to read the IIR if a Tx interrupt is received |
| 283 | * when there is no data in tx_buff[], as in this case there no other |
| 284 | * indication that the interrupt has been serviced, and it remains outstanding |
| 285 | * indefinitely. This has the curious side effect that and no further interrupts |
| 286 | * will be generated from this device AT ALL!!. |
| 287 | * It is also desirable to clear outstanding interrupts when the device is |
| 288 | * opened/closed. |
| 289 | * |
| 290 | * |
| 291 | * Note that some devices need OUT2 to be set before they will generate |
| 292 | * interrupts at all. (Possibly tied to an internal pull-up on CTS?) |
| 293 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 294 | static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | { |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 296 | struct snd_uart16550 *uart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 298 | uart = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | spin_lock(&uart->open_lock); |
| 300 | if (uart->filemode == SERIAL_MODE_NOT_OPENED) { |
| 301 | spin_unlock(&uart->open_lock); |
| 302 | return IRQ_NONE; |
| 303 | } |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 304 | /* indicate to the UART that the interrupt has been serviced */ |
| 305 | inb(uart->base + UART_IIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | snd_uart16550_io_loop(uart); |
| 307 | spin_unlock(&uart->open_lock); |
| 308 | return IRQ_HANDLED; |
| 309 | } |
| 310 | |
| 311 | /* When the polling mode, this function calls snd_uart16550_io_loop. */ |
| 312 | static void snd_uart16550_buffer_timer(unsigned long data) |
| 313 | { |
| 314 | unsigned long flags; |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 315 | struct snd_uart16550 *uart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 317 | uart = (struct snd_uart16550 *)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | spin_lock_irqsave(&uart->open_lock, flags); |
| 319 | snd_uart16550_del_timer(uart); |
| 320 | snd_uart16550_io_loop(uart); |
| 321 | spin_unlock_irqrestore(&uart->open_lock, flags); |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * this method probes, if an uart sits on given port |
| 326 | * return 0 if found |
| 327 | * return negative error if not found |
| 328 | */ |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 329 | static int snd_uart16550_detect(struct snd_uart16550 *uart) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { |
| 331 | unsigned long io_base = uart->base; |
| 332 | int ok; |
| 333 | unsigned char c; |
| 334 | |
| 335 | /* Do some vague tests for the presence of the uart */ |
| 336 | if (io_base == 0 || io_base == SNDRV_AUTO_PORT) { |
| 337 | return -ENODEV; /* Not configured */ |
| 338 | } |
| 339 | |
| 340 | uart->res_base = request_region(io_base, 8, "Serial MIDI"); |
| 341 | if (uart->res_base == NULL) { |
| 342 | snd_printk(KERN_ERR "u16550: can't grab port 0x%lx\n", io_base); |
| 343 | return -EBUSY; |
| 344 | } |
| 345 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 346 | /* uart detected unless one of the following tests should fail */ |
| 347 | ok = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */ |
| 349 | outb(UART_LCR_WLEN8, io_base + UART_LCR); /* Line Control Register */ |
| 350 | c = inb(io_base + UART_IER); |
| 351 | /* The top four bits of the IER should always == 0 */ |
| 352 | if ((c & 0xf0) != 0) |
| 353 | ok = 0; /* failed */ |
| 354 | |
| 355 | outb(0xaa, io_base + UART_SCR); |
| 356 | /* Write arbitrary data into the scratch reg */ |
| 357 | c = inb(io_base + UART_SCR); |
| 358 | /* If it comes back, it's OK */ |
| 359 | if (c != 0xaa) |
| 360 | ok = 0; /* failed */ |
| 361 | |
| 362 | outb(0x55, io_base + UART_SCR); |
| 363 | /* Write arbitrary data into the scratch reg */ |
| 364 | c = inb(io_base + UART_SCR); |
| 365 | /* If it comes back, it's OK */ |
| 366 | if (c != 0x55) |
| 367 | ok = 0; /* failed */ |
| 368 | |
| 369 | return ok; |
| 370 | } |
| 371 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 372 | static void snd_uart16550_do_open(struct snd_uart16550 * uart) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
| 374 | char byte; |
| 375 | |
| 376 | /* Initialize basic variables */ |
| 377 | uart->buff_in_count = 0; |
| 378 | uart->buff_in = 0; |
| 379 | uart->buff_out = 0; |
| 380 | uart->fifo_limit = 1; |
| 381 | uart->fifo_count = 0; |
| 382 | uart->timer_running = 0; |
| 383 | |
| 384 | outb(UART_FCR_ENABLE_FIFO /* Enable FIFO's (if available) */ |
| 385 | | UART_FCR_CLEAR_RCVR /* Clear receiver FIFO */ |
| 386 | | UART_FCR_CLEAR_XMIT /* Clear transmitter FIFO */ |
| 387 | | UART_FCR_TRIGGER_4 /* Set FIFO trigger at 4-bytes */ |
| 388 | /* NOTE: interrupt generated after T=(time)4-bytes |
| 389 | * if less than UART_FCR_TRIGGER bytes received |
| 390 | */ |
| 391 | ,uart->base + UART_FCR); /* FIFO Control Register */ |
| 392 | |
| 393 | if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0) |
| 394 | uart->fifo_limit = 16; |
| 395 | if (uart->divisor != 0) { |
| 396 | uart->old_line_ctrl_reg = inb(uart->base + UART_LCR); |
| 397 | outb(UART_LCR_DLAB /* Divisor latch access bit */ |
| 398 | ,uart->base + UART_LCR); /* Line Control Register */ |
| 399 | uart->old_divisor_lsb = inb(uart->base + UART_DLL); |
| 400 | uart->old_divisor_msb = inb(uart->base + UART_DLM); |
| 401 | |
| 402 | outb(uart->divisor |
| 403 | ,uart->base + UART_DLL); /* Divisor Latch Low */ |
| 404 | outb(0 |
| 405 | ,uart->base + UART_DLM); /* Divisor Latch High */ |
| 406 | /* DLAB is reset to 0 in next outb() */ |
| 407 | } |
| 408 | /* Set serial parameters (parity off, etc) */ |
| 409 | outb(UART_LCR_WLEN8 /* 8 data-bits */ |
| 410 | | 0 /* 1 stop-bit */ |
| 411 | | 0 /* parity off */ |
| 412 | | 0 /* DLAB = 0 */ |
| 413 | ,uart->base + UART_LCR); /* Line Control Register */ |
| 414 | |
| 415 | switch (uart->adaptor) { |
| 416 | default: |
| 417 | outb(UART_MCR_RTS /* Set Request-To-Send line active */ |
| 418 | | UART_MCR_DTR /* Set Data-Terminal-Ready line active */ |
| 419 | | UART_MCR_OUT2 /* Set OUT2 - not always required, but when |
| 420 | * it is, it is ESSENTIAL for enabling interrupts |
| 421 | */ |
| 422 | ,uart->base + UART_MCR); /* Modem Control Register */ |
| 423 | break; |
| 424 | case SNDRV_SERIAL_MS124W_SA: |
| 425 | case SNDRV_SERIAL_MS124W_MB: |
| 426 | /* MS-124W can draw power from RTS and DTR if they |
| 427 | are in opposite states. */ |
| 428 | outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2, |
| 429 | uart->base + UART_MCR); |
| 430 | break; |
| 431 | case SNDRV_SERIAL_MS124T: |
| 432 | /* MS-124T can draw power from RTS and/or DTR (preferably |
| 433 | both) if they are both asserted. */ |
| 434 | outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2, |
| 435 | uart->base + UART_MCR); |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | if (uart->irq < 0) { |
| 440 | byte = (0 & UART_IER_RDI) /* Disable Receiver data interrupt */ |
| 441 | |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */ |
| 442 | ; |
| 443 | } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) { |
| 444 | byte = UART_IER_RDI /* Enable Receiver data interrupt */ |
| 445 | | UART_IER_MSI /* Enable Modem status interrupt */ |
| 446 | ; |
| 447 | } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) { |
| 448 | byte = UART_IER_RDI /* Enable Receiver data interrupt */ |
| 449 | | UART_IER_MSI /* Enable Modem status interrupt */ |
| 450 | | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */ |
| 451 | ; |
| 452 | } else { |
| 453 | byte = UART_IER_RDI /* Enable Receiver data interrupt */ |
| 454 | | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */ |
| 455 | ; |
| 456 | } |
Joe Perches | 561de31 | 2007-12-18 13:13:47 +0100 | [diff] [blame] | 457 | outb(byte, uart->base + UART_IER); /* Interrupt enable Register */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | inb(uart->base + UART_LSR); /* Clear any pre-existing overrun indication */ |
| 460 | inb(uart->base + UART_IIR); /* Clear any pre-existing transmit interrupt */ |
| 461 | inb(uart->base + UART_RX); /* Clear any pre-existing receive interrupt */ |
| 462 | } |
| 463 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 464 | static void snd_uart16550_do_close(struct snd_uart16550 * uart) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | { |
| 466 | if (uart->irq < 0) |
| 467 | snd_uart16550_del_timer(uart); |
| 468 | |
| 469 | /* NOTE: may need to disable interrupts before de-registering out handler. |
| 470 | * For now, the consequences are harmless. |
| 471 | */ |
| 472 | |
| 473 | outb((0 & UART_IER_RDI) /* Disable Receiver data interrupt */ |
| 474 | |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */ |
Joe Perches | 561de31 | 2007-12-18 13:13:47 +0100 | [diff] [blame] | 475 | ,uart->base + UART_IER); /* Interrupt enable Register */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | switch (uart->adaptor) { |
| 478 | default: |
| 479 | outb((0 & UART_MCR_RTS) /* Deactivate Request-To-Send line */ |
| 480 | |(0 & UART_MCR_DTR) /* Deactivate Data-Terminal-Ready line */ |
| 481 | |(0 & UART_MCR_OUT2) /* Deactivate OUT2 */ |
| 482 | ,uart->base + UART_MCR); /* Modem Control Register */ |
| 483 | break; |
| 484 | case SNDRV_SERIAL_MS124W_SA: |
| 485 | case SNDRV_SERIAL_MS124W_MB: |
| 486 | /* MS-124W can draw power from RTS and DTR if they |
| 487 | are in opposite states; leave it powered. */ |
| 488 | outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2), |
| 489 | uart->base + UART_MCR); |
| 490 | break; |
| 491 | case SNDRV_SERIAL_MS124T: |
| 492 | /* MS-124T can draw power from RTS and/or DTR (preferably |
| 493 | both) if they are both asserted; leave it powered. */ |
| 494 | outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2), |
| 495 | uart->base + UART_MCR); |
| 496 | break; |
| 497 | } |
| 498 | |
| 499 | inb(uart->base + UART_IIR); /* Clear any outstanding interrupts */ |
| 500 | |
| 501 | /* Restore old divisor */ |
| 502 | if (uart->divisor != 0) { |
| 503 | outb(UART_LCR_DLAB /* Divisor latch access bit */ |
| 504 | ,uart->base + UART_LCR); /* Line Control Register */ |
| 505 | outb(uart->old_divisor_lsb |
| 506 | ,uart->base + UART_DLL); /* Divisor Latch Low */ |
| 507 | outb(uart->old_divisor_msb |
| 508 | ,uart->base + UART_DLM); /* Divisor Latch High */ |
| 509 | /* Restore old LCR (data bits, stop bits, parity, DLAB) */ |
| 510 | outb(uart->old_line_ctrl_reg |
| 511 | ,uart->base + UART_LCR); /* Line Control Register */ |
| 512 | } |
| 513 | } |
| 514 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 515 | static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
| 517 | unsigned long flags; |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 518 | struct snd_uart16550 *uart = substream->rmidi->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | spin_lock_irqsave(&uart->open_lock, flags); |
| 521 | if (uart->filemode == SERIAL_MODE_NOT_OPENED) |
| 522 | snd_uart16550_do_open(uart); |
| 523 | uart->filemode |= SERIAL_MODE_INPUT_OPEN; |
| 524 | uart->midi_input[substream->number] = substream; |
| 525 | spin_unlock_irqrestore(&uart->open_lock, flags); |
| 526 | return 0; |
| 527 | } |
| 528 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 529 | static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
| 531 | unsigned long flags; |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 532 | struct snd_uart16550 *uart = substream->rmidi->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
| 534 | spin_lock_irqsave(&uart->open_lock, flags); |
| 535 | uart->filemode &= ~SERIAL_MODE_INPUT_OPEN; |
| 536 | uart->midi_input[substream->number] = NULL; |
| 537 | if (uart->filemode == SERIAL_MODE_NOT_OPENED) |
| 538 | snd_uart16550_do_close(uart); |
| 539 | spin_unlock_irqrestore(&uart->open_lock, flags); |
| 540 | return 0; |
| 541 | } |
| 542 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 543 | static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream, |
| 544 | int up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
| 546 | unsigned long flags; |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 547 | struct snd_uart16550 *uart = substream->rmidi->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
| 549 | spin_lock_irqsave(&uart->open_lock, flags); |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 550 | if (up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED; |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 552 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | spin_unlock_irqrestore(&uart->open_lock, flags); |
| 555 | } |
| 556 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 557 | static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
| 559 | unsigned long flags; |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 560 | struct snd_uart16550 *uart = substream->rmidi->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
| 562 | spin_lock_irqsave(&uart->open_lock, flags); |
| 563 | if (uart->filemode == SERIAL_MODE_NOT_OPENED) |
| 564 | snd_uart16550_do_open(uart); |
| 565 | uart->filemode |= SERIAL_MODE_OUTPUT_OPEN; |
| 566 | uart->midi_output[substream->number] = substream; |
| 567 | spin_unlock_irqrestore(&uart->open_lock, flags); |
| 568 | return 0; |
| 569 | }; |
| 570 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 571 | static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
| 573 | unsigned long flags; |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 574 | struct snd_uart16550 *uart = substream->rmidi->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
| 576 | spin_lock_irqsave(&uart->open_lock, flags); |
| 577 | uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN; |
| 578 | uart->midi_output[substream->number] = NULL; |
| 579 | if (uart->filemode == SERIAL_MODE_NOT_OPENED) |
| 580 | snd_uart16550_do_close(uart); |
| 581 | spin_unlock_irqrestore(&uart->open_lock, flags); |
| 582 | return 0; |
| 583 | }; |
| 584 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 585 | static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart, |
| 586 | int Num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | { |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 588 | if (uart->buff_in_count + Num < TX_BUFF_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | return 1; |
| 590 | else |
| 591 | return 0; |
| 592 | } |
| 593 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 594 | static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart, |
| 595 | unsigned char byte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
| 597 | unsigned short buff_in = uart->buff_in; |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 598 | if (uart->buff_in_count < TX_BUFF_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | uart->tx_buff[buff_in] = byte; |
| 600 | buff_in++; |
| 601 | buff_in &= TX_BUFF_MASK; |
| 602 | uart->buff_in = buff_in; |
| 603 | uart->buff_in_count++; |
| 604 | if (uart->irq < 0) /* polling mode */ |
| 605 | snd_uart16550_add_timer(uart); |
| 606 | return 1; |
| 607 | } else |
| 608 | return 0; |
| 609 | } |
| 610 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 611 | static int snd_uart16550_output_byte(struct snd_uart16550 *uart, |
| 612 | struct snd_rawmidi_substream *substream, |
| 613 | unsigned char midi_byte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | { |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 615 | if (uart->buff_in_count == 0 /* Buffer empty? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA && |
| 617 | uart->adaptor != SNDRV_SERIAL_GENERIC) || |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 618 | (uart->fifo_count == 0 /* FIFO empty? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) { /* CTS? */ |
| 620 | |
| 621 | /* Tx Buffer Empty - try to write immediately */ |
| 622 | if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) { |
| 623 | /* Transmitter holding register (and Tx FIFO) empty */ |
| 624 | uart->fifo_count = 1; |
| 625 | outb(midi_byte, uart->base + UART_TX); |
| 626 | } else { |
| 627 | if (uart->fifo_count < uart->fifo_limit) { |
| 628 | uart->fifo_count++; |
| 629 | outb(midi_byte, uart->base + UART_TX); |
| 630 | } else { |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 631 | /* Cannot write (buffer empty) - |
| 632 | * put char in buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | snd_uart16550_write_buffer(uart, midi_byte); |
| 634 | } |
| 635 | } |
| 636 | } else { |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 637 | if (!snd_uart16550_write_buffer(uart, midi_byte)) { |
Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 638 | snd_printk(KERN_WARNING |
| 639 | "%s: Buffer overrun on device at 0x%lx\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Joe Perches | 561de31 | 2007-12-18 13:13:47 +0100 | [diff] [blame] | 656 | /* Interrupts are disabled during the updating of the tx_buff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 || |
Julia Lawall | f11b799 | 2008-01-07 13:33:45 +0100 | [diff] [blame] | 697 | time_after(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 | c36f486 | 2017-01-05 17:28:39 +0100 | [diff] [blame] | 755 | static const 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 | c36f486 | 2017-01-05 17:28:39 +0100 | [diff] [blame] | 762 | static const 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 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 784 | static int snd_uart16550_create(struct snd_card *card, |
| 785 | unsigned long iobase, |
| 786 | int irq, |
| 787 | unsigned int speed, |
| 788 | unsigned int base, |
| 789 | int adaptor, |
| 790 | int droponfull, |
| 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, |
Yong Zhang | 88e24c3 | 2011-09-22 16:59:20 +0800 | [diff] [blame] | 817 | 0, "Serial MIDI", uart)) { |
Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 818 | snd_printk(KERN_WARNING |
| 819 | "irq %d busy. Using Polling.\n", irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } else { |
| 821 | uart->irq = irq; |
| 822 | } |
| 823 | } |
| 824 | uart->divisor = base / speed; |
| 825 | uart->speed = base / (unsigned int)uart->divisor; |
| 826 | uart->speed_base = base; |
| 827 | uart->prev_out = -1; |
| 828 | uart->prev_in = 0; |
| 829 | uart->rstatus = 0; |
| 830 | memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS); |
Takashi Iwai | d80d05f | 2015-01-19 11:29:09 +0100 | [diff] [blame] | 831 | setup_timer(&uart->buffer_timer, snd_uart16550_buffer_timer, |
| 832 | (unsigned long)uart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 863 | static void 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 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 872 | static int snd_uart16550_rmidi(struct snd_uart16550 *uart, int device, |
| 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 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 899 | static int 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: |
Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 922 | snd_printk(KERN_ERR |
| 923 | "Adaptor type is out of range 0-%d (%d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]); |
| 925 | return -ENODEV; |
| 926 | } |
| 927 | |
| 928 | if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) { |
Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 929 | snd_printk(KERN_ERR |
| 930 | "Count of outputs is out of range 1-%d (%d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | SNDRV_SERIAL_MAX_OUTS, outs[dev]); |
| 932 | return -ENODEV; |
| 933 | } |
| 934 | |
| 935 | if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) { |
Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 936 | snd_printk(KERN_ERR |
| 937 | "Count of inputs is out of range 1-%d (%d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | SNDRV_SERIAL_MAX_INS, ins[dev]); |
| 939 | return -ENODEV; |
| 940 | } |
| 941 | |
Takashi Iwai | 5872f3f | 2014-01-29 12:59:08 +0100 | [diff] [blame] | 942 | err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE, |
| 943 | 0, &card); |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 944 | if (err < 0) |
| 945 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
| 947 | strcpy(card->driver, "Serial"); |
| 948 | strcpy(card->shortname, "Serial MIDI (UART16550A)"); |
| 949 | |
| 950 | if ((err = snd_uart16550_create(card, |
| 951 | port[dev], |
| 952 | irq[dev], |
| 953 | speed[dev], |
| 954 | base[dev], |
| 955 | adaptor[dev], |
| 956 | droponfull[dev], |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 957 | &uart)) < 0) |
| 958 | goto _err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
Takashi Iwai | 0b830bac | 2006-11-24 16:13:57 +0100 | [diff] [blame] | 960 | err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi); |
| 961 | if (err < 0) |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 962 | goto _err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | |
Clemens Ladisch | bd0185c | 2009-05-05 15:39:39 +0200 | [diff] [blame] | 964 | sprintf(card->longname, "%s [%s] at %#lx, irq %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | card->shortname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | adaptor_names[uart->adaptor], |
Clemens Ladisch | bd0185c | 2009-05-05 15:39:39 +0200 | [diff] [blame] | 967 | uart->base, |
| 968 | uart->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 970 | if ((err = snd_card_register(card)) < 0) |
| 971 | goto _err; |
| 972 | |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 973 | platform_set_drvdata(devptr, card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | return 0; |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 975 | |
| 976 | _err: |
| 977 | snd_card_free(card); |
| 978 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 981 | static int snd_serial_remove(struct platform_device *devptr) |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 982 | { |
| 983 | snd_card_free(platform_get_drvdata(devptr)); |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | #define SND_SERIAL_DRIVER "snd_serial_u16550" |
| 988 | |
| 989 | static struct platform_driver snd_serial_driver = { |
| 990 | .probe = snd_serial_probe, |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 991 | .remove = snd_serial_remove, |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 992 | .driver = { |
Takashi Iwai | 8bf01d8 | 2012-07-02 10:50:24 +0200 | [diff] [blame] | 993 | .name = SND_SERIAL_DRIVER, |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 994 | }, |
| 995 | }; |
| 996 | |
Randy Dunlap | bdec0c7 | 2007-06-25 12:07:38 +0200 | [diff] [blame] | 997 | static void snd_serial_unregister_all(void) |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 998 | { |
| 999 | int i; |
| 1000 | |
| 1001 | for (i = 0; i < ARRAY_SIZE(devices); ++i) |
| 1002 | platform_device_unregister(devices[i]); |
| 1003 | platform_driver_unregister(&snd_serial_driver); |
| 1004 | } |
| 1005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | static int __init alsa_card_serial_init(void) |
| 1007 | { |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 1008 | int i, cards, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 1010 | if ((err = platform_driver_register(&snd_serial_driver)) < 0) |
| 1011 | return err; |
| 1012 | |
| 1013 | cards = 0; |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 1014 | for (i = 0; i < SNDRV_CARDS; i++) { |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 1015 | struct platform_device *device; |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 1016 | if (! enable[i]) |
| 1017 | continue; |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 1018 | device = platform_device_register_simple(SND_SERIAL_DRIVER, |
| 1019 | i, NULL, 0); |
Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 1020 | if (IS_ERR(device)) |
| 1021 | continue; |
Rene Herman | 7152447 | 2006-04-13 12:58:06 +0200 | [diff] [blame] | 1022 | if (!platform_get_drvdata(device)) { |
| 1023 | platform_device_unregister(device); |
| 1024 | continue; |
| 1025 | } |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 1026 | devices[i] = device; |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 1027 | cards++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
Takashi Iwai | 9caf6b5 | 2005-11-17 16:02:30 +0100 | [diff] [blame] | 1029 | if (! cards) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | #ifdef MODULE |
| 1031 | printk(KERN_ERR "serial midi soundcard not found or device busy\n"); |
| 1032 | #endif |
Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 1033 | snd_serial_unregister_all(); |
| 1034 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | static void __exit alsa_card_serial_exit(void) |
| 1040 | { |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 1041 | snd_serial_unregister_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | module_init(alsa_card_serial_init) |
| 1045 | module_exit(alsa_card_serial_exit) |