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