Blame view

sound/drivers/serial-u16550.c 30.9 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
  /*
   *   serial.c
c1017a4cd   Jaroslav Kysela   [ALSA] Changed Ja...
3
   *   Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   *                    Isaku Yamahata <yamahata@private.email.ne.jp>,
   *		      George Hansper <ghansper@apana.org.au>,
   *		      Hannu Savolainen
   *
   *   This code is based on the code from ALSA 0.5.9, but heavily rewritten.
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   * Sat Mar 31 17:27:57 PST 2001 tim.mann@compaq.com 
   *      Added support for the Midiator MS-124T and for the MS-124W in
   *      Single Addressed (S/A) or Multiple Burst (M/B) mode, with
   *      power derived either parasitically from the serial port or
   *      from a separate power supply.
   *
   *      More documentation can be found in serial-u16550.txt.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
  #include <linux/init.h>
  #include <linux/interrupt.h>
9caf6b590   Takashi Iwai   [ALSA] serial-u16...
34
35
  #include <linux/err.h>
  #include <linux/platform_device.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
  #include <linux/slab.h>
  #include <linux/ioport.h>
65a772172   Paul Gortmaker   sound: fix driver...
38
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
41
42
43
  #include <sound/core.h>
  #include <sound/rawmidi.h>
  #include <sound/initval.h>
  
  #include <linux/serial_reg.h>
f11b79928   Julia Lawall   [ALSA] sound: Use...
44
  #include <linux/jiffies.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  
  #include <asm/io.h>
  
  MODULE_DESCRIPTION("MIDI serial u16550");
  MODULE_LICENSE("GPL");
  MODULE_SUPPORTED_DEVICE("{{ALSA, MIDI serial u16550}}");
  
  #define SNDRV_SERIAL_SOUNDCANVAS 0 /* Roland Soundcanvas; F5 NN selects part */
  #define SNDRV_SERIAL_MS124T 1      /* Midiator MS-124T */
  #define SNDRV_SERIAL_MS124W_SA 2   /* Midiator MS-124W in S/A mode */
  #define SNDRV_SERIAL_MS124W_MB 3   /* Midiator MS-124W in M/B mode */
  #define SNDRV_SERIAL_GENERIC 4     /* Generic Interface */
  #define SNDRV_SERIAL_MAX_ADAPTOR SNDRV_SERIAL_GENERIC
  static char *adaptor_names[] = {
  	"Soundcanvas",
          "MS-124T",
  	"MS-124W S/A",
  	"MS-124W M/B",
  	"Generic"
  };
  
  #define SNDRV_SERIAL_NORMALBUFF 0 /* Normal blocking buffer operation */
  #define SNDRV_SERIAL_DROPBUFF   1 /* Non-blocking discard operation */
  
  static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
  static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
a67ff6a54   Rusty Russell   ALSA: module_para...
71
  static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
74
75
76
77
78
  static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x3f8,0x2f8,0x3e8,0x2e8 */
  static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; 	/* 3,4,5,7,9,10,11,14,15 */
  static int speed[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 38400}; /* 9600,19200,38400,57600,115200 */
  static int base[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 115200}; /* baud base */
  static int outs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};	 /* 1 to 16 */
  static int ins[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};	/* 1 to 16 */
  static int adaptor[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = SNDRV_SERIAL_SOUNDCANVAS};
a67ff6a54   Rusty Russell   ALSA: module_para...
79
  static bool droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  
  module_param_array(index, int, NULL, 0444);
  MODULE_PARM_DESC(index, "Index value for Serial MIDI.");
  module_param_array(id, charp, NULL, 0444);
  MODULE_PARM_DESC(id, "ID string for Serial MIDI.");
  module_param_array(enable, bool, NULL, 0444);
  MODULE_PARM_DESC(enable, "Enable UART16550A chip.");
  module_param_array(port, long, NULL, 0444);
  MODULE_PARM_DESC(port, "Port # for UART16550A chip.");
  module_param_array(irq, int, NULL, 0444);
  MODULE_PARM_DESC(irq, "IRQ # for UART16550A chip.");
  module_param_array(speed, int, NULL, 0444);
  MODULE_PARM_DESC(speed, "Speed in bauds.");
  module_param_array(base, int, NULL, 0444);
  MODULE_PARM_DESC(base, "Base for divisor in bauds.");
  module_param_array(outs, int, NULL, 0444);
  MODULE_PARM_DESC(outs, "Number of MIDI outputs.");
  module_param_array(ins, int, NULL, 0444);
  MODULE_PARM_DESC(ins, "Number of MIDI inputs.");
  module_param_array(droponfull, bool, NULL, 0444);
  MODULE_PARM_DESC(droponfull, "Flag to enable drop-on-full buffer mode");
  
  module_param_array(adaptor, int, NULL, 0444);
  MODULE_PARM_DESC(adaptor, "Type of adaptor.");
  
  /*#define SNDRV_SERIAL_MS124W_MB_NOCOMBO 1*/  /* Address outs as 0-3 instead of bitmap */
  
  #define SNDRV_SERIAL_MAX_OUTS	16		/* max 64, min 16 */
  #define SNDRV_SERIAL_MAX_INS	16		/* max 64, min 16 */
  
  #define TX_BUFF_SIZE		(1<<15)		/* Must be 2^n */
  #define TX_BUFF_MASK		(TX_BUFF_SIZE - 1)
  
  #define SERIAL_MODE_NOT_OPENED 		(0)
  #define SERIAL_MODE_INPUT_OPEN		(1 << 0)
  #define SERIAL_MODE_OUTPUT_OPEN		(1 << 1)
  #define SERIAL_MODE_INPUT_TRIGGERED	(1 << 2)
  #define SERIAL_MODE_OUTPUT_TRIGGERED	(1 << 3)
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
118
  struct snd_uart16550 {
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
119
120
121
122
  	struct snd_card *card;
  	struct snd_rawmidi *rmidi;
  	struct snd_rawmidi_substream *midi_output[SNDRV_SERIAL_MAX_OUTS];
  	struct snd_rawmidi_substream *midi_input[SNDRV_SERIAL_MAX_INS];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123

0b830bac3   Takashi Iwai   [ALSA] Clean up s...
124
  	int filemode;		/* open status of file */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  
  	spinlock_t open_lock;
  
  	int irq;
  
  	unsigned long base;
  	struct resource *res_base;
  
  	unsigned int speed;
  	unsigned int speed_base;
  	unsigned char divisor;
  
  	unsigned char old_divisor_lsb;
  	unsigned char old_divisor_msb;
  	unsigned char old_line_ctrl_reg;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
140
141
142
  	/* parameter for using of write loop */
  	short int fifo_limit;	/* used in uart16550 */
          short int fifo_count;	/* used in uart16550 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143

0b830bac3   Takashi Iwai   [ALSA] Clean up s...
144
  	/* type of adaptor */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
  	int adaptor;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
146
  	/* inputs */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
  	int prev_in;
  	unsigned char rstatus;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
149
  	/* outputs */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
  	int prev_out;
  	unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS];
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
152
  	/* write buffer and its writing/reading position */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
154
155
156
157
  	unsigned char tx_buff[TX_BUFF_SIZE];
  	int buff_in_count;
          int buff_in;
          int buff_out;
          int drop_on_full;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
158
  	/* wait timer */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
159
160
  	unsigned int timer_running:1;
  	struct timer_list buffer_timer;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
161
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162

f7a9275d9   Clemens Ladisch   [ALSA] unregister...
163
  static struct platform_device *devices[SNDRV_CARDS];
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
164
  static inline void snd_uart16550_add_timer(struct snd_uart16550 *uart)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
  {
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
166
  	if (!uart->timer_running) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
170
171
172
  		/* timer 38600bps * 10bit * 16byte */
  		uart->buffer_timer.expires = jiffies + (HZ+255)/256;
  		uart->timer_running = 1;
  		add_timer(&uart->buffer_timer);
  	}
  }
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
173
  static inline void snd_uart16550_del_timer(struct snd_uart16550 *uart)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
176
177
178
179
180
181
  {
  	if (uart->timer_running) {
  		del_timer(&uart->buffer_timer);
  		uart->timer_running = 0;
  	}
  }
  
  /* This macro is only used in snd_uart16550_io_loop */
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
182
  static inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
  {
  	unsigned short buff_out = uart->buff_out;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
185
  	if (uart->buff_in_count > 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
187
188
189
190
191
192
193
194
195
196
197
198
  		outb(uart->tx_buff[buff_out], uart->base + UART_TX);
  		uart->fifo_count++;
  		buff_out++;
  		buff_out &= TX_BUFF_MASK;
  		uart->buff_out = buff_out;
  		uart->buff_in_count--;
  	}
  }
  
  /* This loop should be called with interrupts disabled
   * We don't want to interrupt this, 
   * as we're already handling an interrupt 
   */
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
199
  static void snd_uart16550_io_loop(struct snd_uart16550 * uart)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
203
204
205
206
207
208
209
210
211
212
  {
  	unsigned char c, status;
  	int substream;
  
  	/* recall previous stream */
  	substream = uart->prev_in;
  
  	/* Read Loop */
  	while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) {
  		/* while receive data ready */
  		c = inb(uart->base + UART_RX);
  
  		/* keep track of last status byte */
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
213
  		if (c & 0x80)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
  			uart->rstatus = c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
216
217
218
219
220
221
  
  		/* handle stream switch */
  		if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
  			if (uart->rstatus == 0xf5) {
  				if (c <= SNDRV_SERIAL_MAX_INS && c > 0)
  					substream = c - 1;
  				if (c != 0xf5)
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
222
223
224
225
226
227
228
229
230
  					/* prevent future bytes from being
  					   interpreted as streams */
  					uart->rstatus = 0;
  			} else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN)
  				   && uart->midi_input[substream])
  				snd_rawmidi_receive(uart->midi_input[substream],
  						    &c, 1);
  		} else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) &&
  			   uart->midi_input[substream])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231
  			snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
  
  		if (status & UART_LSR_OE)
45203832d   Takashi Iwai   ALSA: Add missing...
234
235
236
  			snd_printk(KERN_WARNING
  				   "%s: Overrun on device at 0x%lx
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
239
240
241
242
243
244
245
  			       uart->rmidi->name, uart->base);
  	}
  
  	/* remember the last stream */
  	uart->prev_in = substream;
  
  	/* no need of check SERIAL_MODE_OUTPUT_OPEN because if not,
  	   buffer is never filled. */
  	/* Check write status */
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
246
  	if (status & UART_LSR_THRE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
  		uart->fifo_count = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
248
249
250
251
  	if (uart->adaptor == SNDRV_SERIAL_MS124W_SA
  	   || uart->adaptor == SNDRV_SERIAL_GENERIC) {
  		/* Can't use FIFO, must send only when CTS is true */
  		status = inb(uart->base + UART_MSR);
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
252
253
  		while (uart->fifo_count == 0 && (status & UART_MSR_CTS) &&
  		       uart->buff_in_count > 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
  		       snd_uart16550_buffer_output(uart);
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
255
  		       status = inb(uart->base + UART_MSR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
257
258
  		}
  	} else {
  		/* Write loop */
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
259
  		while (uart->fifo_count < uart->fifo_limit /* Can we write ? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  		       && uart->buff_in_count > 0)	/* Do we want to? */
  			snd_uart16550_buffer_output(uart);
  	}
  	if (uart->irq < 0 && uart->buff_in_count > 0)
  		snd_uart16550_add_timer(uart);
  }
  
  /* NOTES ON SERVICING INTERUPTS
   * ---------------------------
   * After receiving a interrupt, it is important to indicate to the UART that
   * this has been done. 
   * For a Rx interrupt, this is done by reading the received byte.
   * For a Tx interrupt this is done by either:
   * a) Writing a byte
   * b) Reading the IIR
   * It is particularly important to read the IIR if a Tx interrupt is received
   * when there is no data in tx_buff[], as in this case there no other
   * indication that the interrupt has been serviced, and it remains outstanding
   * indefinitely. This has the curious side effect that and no further interrupts
   * will be generated from this device AT ALL!!.
   * It is also desirable to clear outstanding interrupts when the device is
   * opened/closed.
   *
   *
   * Note that some devices need OUT2 to be set before they will generate
   * interrupts at all. (Possibly tied to an internal pull-up on CTS?)
   */
7d12e780e   David Howells   IRQ: Maintain reg...
287
  static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
  {
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
289
  	struct snd_uart16550 *uart;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290

0b830bac3   Takashi Iwai   [ALSA] Clean up s...
291
  	uart = dev_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
293
294
295
296
  	spin_lock(&uart->open_lock);
  	if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
  		spin_unlock(&uart->open_lock);
  		return IRQ_NONE;
  	}
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
297
298
  	/* indicate to the UART that the interrupt has been serviced */
  	inb(uart->base + UART_IIR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
301
302
303
304
305
306
307
  	snd_uart16550_io_loop(uart);
  	spin_unlock(&uart->open_lock);
  	return IRQ_HANDLED;
  }
  
  /* When the polling mode, this function calls snd_uart16550_io_loop. */
  static void snd_uart16550_buffer_timer(unsigned long data)
  {
  	unsigned long flags;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
308
  	struct snd_uart16550 *uart;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309

0b830bac3   Takashi Iwai   [ALSA] Clean up s...
310
  	uart = (struct snd_uart16550 *)data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
312
313
314
315
316
317
318
319
320
321
  	spin_lock_irqsave(&uart->open_lock, flags);
  	snd_uart16550_del_timer(uart);
  	snd_uart16550_io_loop(uart);
  	spin_unlock_irqrestore(&uart->open_lock, flags);
  }
  
  /*
   *  this method probes, if an uart sits on given port
   *  return 0 if found
   *  return negative error if not found
   */
788c60433   Prarit Bhargava   [ALSA] Fix __devi...
322
  static int __devinit snd_uart16550_detect(struct snd_uart16550 *uart)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  {
  	unsigned long io_base = uart->base;
  	int ok;
  	unsigned char c;
  
  	/* Do some vague tests for the presence of the uart */
  	if (io_base == 0 || io_base == SNDRV_AUTO_PORT) {
  		return -ENODEV;	/* Not configured */
  	}
  
  	uart->res_base = request_region(io_base, 8, "Serial MIDI");
  	if (uart->res_base == NULL) {
  		snd_printk(KERN_ERR "u16550: can't grab port 0x%lx
  ", io_base);
  		return -EBUSY;
  	}
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
339
340
  	/* uart detected unless one of the following tests should fail */
  	ok = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
  	/* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */
  	outb(UART_LCR_WLEN8, io_base + UART_LCR); /* Line Control Register */
  	c = inb(io_base + UART_IER);
  	/* The top four bits of the IER should always == 0 */
  	if ((c & 0xf0) != 0)
  		ok = 0;		/* failed */
  
  	outb(0xaa, io_base + UART_SCR);
  	/* Write arbitrary data into the scratch reg */
  	c = inb(io_base + UART_SCR);
  	/* If it comes back, it's OK */
  	if (c != 0xaa)
  		ok = 0;		/* failed */
  
  	outb(0x55, io_base + UART_SCR);
  	/* Write arbitrary data into the scratch reg */
  	c = inb(io_base + UART_SCR);
  	/* If it comes back, it's OK */
  	if (c != 0x55)
  		ok = 0;		/* failed */
  
  	return ok;
  }
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
364
  static void snd_uart16550_do_open(struct snd_uart16550 * uart)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  {
  	char byte;
  
  	/* Initialize basic variables */
  	uart->buff_in_count = 0;
  	uart->buff_in = 0;
  	uart->buff_out = 0;
  	uart->fifo_limit = 1;
  	uart->fifo_count = 0;
  	uart->timer_running = 0;
  
  	outb(UART_FCR_ENABLE_FIFO	/* Enable FIFO's (if available) */
  	     | UART_FCR_CLEAR_RCVR	/* Clear receiver FIFO */
  	     | UART_FCR_CLEAR_XMIT	/* Clear transmitter FIFO */
  	     | UART_FCR_TRIGGER_4	/* Set FIFO trigger at 4-bytes */
  	/* NOTE: interrupt generated after T=(time)4-bytes
  	 * if less than UART_FCR_TRIGGER bytes received
  	 */
  	     ,uart->base + UART_FCR);	/* FIFO Control Register */
  
  	if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0)
  		uart->fifo_limit = 16;
  	if (uart->divisor != 0) {
  		uart->old_line_ctrl_reg = inb(uart->base + UART_LCR);
  		outb(UART_LCR_DLAB	/* Divisor latch access bit */
  		     ,uart->base + UART_LCR);	/* Line Control Register */
  		uart->old_divisor_lsb = inb(uart->base + UART_DLL);
  		uart->old_divisor_msb = inb(uart->base + UART_DLM);
  
  		outb(uart->divisor
  		     ,uart->base + UART_DLL);	/* Divisor Latch Low */
  		outb(0
  		     ,uart->base + UART_DLM);	/* Divisor Latch High */
  		/* DLAB is reset to 0 in next outb() */
  	}
  	/* Set serial parameters (parity off, etc) */
  	outb(UART_LCR_WLEN8	/* 8 data-bits */
  	     | 0		/* 1 stop-bit */
  	     | 0		/* parity off */
  	     | 0		/* DLAB = 0 */
  	     ,uart->base + UART_LCR);	/* Line Control Register */
  
  	switch (uart->adaptor) {
  	default:
  		outb(UART_MCR_RTS	/* Set Request-To-Send line active */
  		     | UART_MCR_DTR	/* Set Data-Terminal-Ready line active */
  		     | UART_MCR_OUT2	/* Set OUT2 - not always required, but when
  					 * it is, it is ESSENTIAL for enabling interrupts
  				 */
  		     ,uart->base + UART_MCR);	/* Modem Control Register */
  		break;
  	case SNDRV_SERIAL_MS124W_SA:
  	case SNDRV_SERIAL_MS124W_MB:
  		/* MS-124W can draw power from RTS and DTR if they
  		   are in opposite states. */ 
  		outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2,
  		     uart->base + UART_MCR);
  		break;
  	case SNDRV_SERIAL_MS124T:
  		/* MS-124T can draw power from RTS and/or DTR (preferably
  		   both) if they are both asserted. */
  		outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2,
  		     uart->base + UART_MCR);
  		break;
  	}
  
  	if (uart->irq < 0) {
  		byte = (0 & UART_IER_RDI)	/* Disable Receiver data interrupt */
  		    |(0 & UART_IER_THRI)	/* Disable Transmitter holding register empty interrupt */
  		    ;
  	} else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
  		byte = UART_IER_RDI	/* Enable Receiver data interrupt */
  		    | UART_IER_MSI	/* Enable Modem status interrupt */
  		    ;
  	} else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
  		byte = UART_IER_RDI	/* Enable Receiver data interrupt */
  		    | UART_IER_MSI	/* Enable Modem status interrupt */
  		    | UART_IER_THRI	/* Enable Transmitter holding register empty interrupt */
  		    ;
  	} else {
  		byte = UART_IER_RDI	/* Enable Receiver data interrupt */
  		    | UART_IER_THRI	/* Enable Transmitter holding register empty interrupt */
  		    ;
  	}
561de31a2   Joe Perches   [ALSA] sound/: Sp...
449
  	outb(byte, uart->base + UART_IER);	/* Interrupt enable Register */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
452
453
454
  
  	inb(uart->base + UART_LSR);	/* Clear any pre-existing overrun indication */
  	inb(uart->base + UART_IIR);	/* Clear any pre-existing transmit interrupt */
  	inb(uart->base + UART_RX);	/* Clear any pre-existing receive interrupt */
  }
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
455
  static void snd_uart16550_do_close(struct snd_uart16550 * uart)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
456
457
458
459
460
461
462
463
464
465
  {
  	if (uart->irq < 0)
  		snd_uart16550_del_timer(uart);
  
  	/* NOTE: may need to disable interrupts before de-registering out handler.
  	 * For now, the consequences are harmless.
  	 */
  
  	outb((0 & UART_IER_RDI)		/* Disable Receiver data interrupt */
  	     |(0 & UART_IER_THRI)	/* Disable Transmitter holding register empty interrupt */
561de31a2   Joe Perches   [ALSA] sound/: Sp...
466
  	     ,uart->base + UART_IER);	/* Interrupt enable Register */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
  
  	switch (uart->adaptor) {
  	default:
  		outb((0 & UART_MCR_RTS)		/* Deactivate Request-To-Send line  */
  		     |(0 & UART_MCR_DTR)	/* Deactivate Data-Terminal-Ready line */
  		     |(0 & UART_MCR_OUT2)	/* Deactivate OUT2 */
  		     ,uart->base + UART_MCR);	/* Modem Control Register */
  	  break;
  	case SNDRV_SERIAL_MS124W_SA:
  	case SNDRV_SERIAL_MS124W_MB:
  		/* MS-124W can draw power from RTS and DTR if they
  		   are in opposite states; leave it powered. */ 
  		outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2),
  		     uart->base + UART_MCR);
  		break;
  	case SNDRV_SERIAL_MS124T:
  		/* MS-124T can draw power from RTS and/or DTR (preferably
  		   both) if they are both asserted; leave it powered. */
  		outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2),
  		     uart->base + UART_MCR);
  		break;
  	}
  
  	inb(uart->base + UART_IIR);	/* Clear any outstanding interrupts */
  
  	/* Restore old divisor */
  	if (uart->divisor != 0) {
  		outb(UART_LCR_DLAB		/* Divisor latch access bit */
  		     ,uart->base + UART_LCR);	/* Line Control Register */
  		outb(uart->old_divisor_lsb
  		     ,uart->base + UART_DLL);	/* Divisor Latch Low */
  		outb(uart->old_divisor_msb
  		     ,uart->base + UART_DLM);	/* Divisor Latch High */
  		/* Restore old LCR (data bits, stop bits, parity, DLAB) */
  		outb(uart->old_line_ctrl_reg
  		     ,uart->base + UART_LCR);	/* Line Control Register */
  	}
  }
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
505
  static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
507
  {
  	unsigned long flags;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
508
  	struct snd_uart16550 *uart = substream->rmidi->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
509
510
511
512
513
514
515
516
517
  
  	spin_lock_irqsave(&uart->open_lock, flags);
  	if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  		snd_uart16550_do_open(uart);
  	uart->filemode |= SERIAL_MODE_INPUT_OPEN;
  	uart->midi_input[substream->number] = substream;
  	spin_unlock_irqrestore(&uart->open_lock, flags);
  	return 0;
  }
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
518
  static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
520
  {
  	unsigned long flags;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
521
  	struct snd_uart16550 *uart = substream->rmidi->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
522
523
524
525
526
527
528
529
530
  
  	spin_lock_irqsave(&uart->open_lock, flags);
  	uart->filemode &= ~SERIAL_MODE_INPUT_OPEN;
  	uart->midi_input[substream->number] = NULL;
  	if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  		snd_uart16550_do_close(uart);
  	spin_unlock_irqrestore(&uart->open_lock, flags);
  	return 0;
  }
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
531
532
  static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream,
  					int up)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
533
534
  {
  	unsigned long flags;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
535
  	struct snd_uart16550 *uart = substream->rmidi->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
536
537
  
  	spin_lock_irqsave(&uart->open_lock, flags);
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
538
  	if (up)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
  		uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
540
  	else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
  		uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
542
543
  	spin_unlock_irqrestore(&uart->open_lock, flags);
  }
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
544
  static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
545
546
  {
  	unsigned long flags;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
547
  	struct snd_uart16550 *uart = substream->rmidi->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548
549
550
551
552
553
554
555
556
  
  	spin_lock_irqsave(&uart->open_lock, flags);
  	if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  		snd_uart16550_do_open(uart);
  	uart->filemode |= SERIAL_MODE_OUTPUT_OPEN;
  	uart->midi_output[substream->number] = substream;
  	spin_unlock_irqrestore(&uart->open_lock, flags);
  	return 0;
  };
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
557
  static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
558
559
  {
  	unsigned long flags;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
560
  	struct snd_uart16550 *uart = substream->rmidi->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
561
562
563
564
565
566
567
568
569
  
  	spin_lock_irqsave(&uart->open_lock, flags);
  	uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN;
  	uart->midi_output[substream->number] = NULL;
  	if (uart->filemode == SERIAL_MODE_NOT_OPENED)
  		snd_uart16550_do_close(uart);
  	spin_unlock_irqrestore(&uart->open_lock, flags);
  	return 0;
  };
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
570
571
  static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart,
  						 int Num)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
572
  {
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
573
  	if (uart->buff_in_count + Num < TX_BUFF_SIZE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
575
576
577
  		return 1;
  	else
  		return 0;
  }
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
578
579
  static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart,
  					     unsigned char byte)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
580
581
  {
  	unsigned short buff_in = uart->buff_in;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
582
  	if (uart->buff_in_count < TX_BUFF_SIZE) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
583
584
585
586
587
588
589
590
591
592
593
  		uart->tx_buff[buff_in] = byte;
  		buff_in++;
  		buff_in &= TX_BUFF_MASK;
  		uart->buff_in = buff_in;
  		uart->buff_in_count++;
  		if (uart->irq < 0) /* polling mode */
  			snd_uart16550_add_timer(uart);
  		return 1;
  	} else
  		return 0;
  }
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
594
595
596
  static int snd_uart16550_output_byte(struct snd_uart16550 *uart,
  				     struct snd_rawmidi_substream *substream,
  				     unsigned char midi_byte)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597
  {
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
598
  	if (uart->buff_in_count == 0                    /* Buffer empty? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
599
600
  	    && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
  	    uart->adaptor != SNDRV_SERIAL_GENERIC) ||
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
601
  		(uart->fifo_count == 0                  /* FIFO empty? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
602
603
604
605
606
607
608
609
610
611
612
613
  		 && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) { /* CTS? */
  
  	        /* Tx Buffer Empty - try to write immediately */
  		if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
  		        /* Transmitter holding register (and Tx FIFO) empty */
  		        uart->fifo_count = 1;
  			outb(midi_byte, uart->base + UART_TX);
  		} else {
  		        if (uart->fifo_count < uart->fifo_limit) {
  			        uart->fifo_count++;
  				outb(midi_byte, uart->base + UART_TX);
  			} else {
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
614
615
  			        /* Cannot write (buffer empty) -
  				 * put char in buffer */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
616
617
618
619
  				snd_uart16550_write_buffer(uart, midi_byte);
  			}
  		}
  	} else {
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
620
  		if (!snd_uart16550_write_buffer(uart, midi_byte)) {
45203832d   Takashi Iwai   ALSA: Add missing...
621
622
623
  			snd_printk(KERN_WARNING
  				   "%s: Buffer overrun on device at 0x%lx
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
624
625
626
627
628
629
630
  				   uart->rmidi->name, uart->base);
  			return 0;
  		}
  	}
  
  	return 1;
  }
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
631
  static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
632
633
634
  {
  	unsigned long flags;
  	unsigned char midi_byte, addr_byte;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
635
  	struct snd_uart16550 *uart = substream->rmidi->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
636
  	char first;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
637
  	static unsigned long lasttime = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
638
  	
561de31a2   Joe Perches   [ALSA] sound/: Sp...
639
  	/* Interrupts are disabled during the updating of the tx_buff,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
640
641
642
643
644
  	 * since it is 'bad' to have two processes updating the same
  	 * variables (ie buff_in & buff_out)
  	 */
  
  	spin_lock_irqsave(&uart->open_lock, flags);
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
645
  	if (uart->irq < 0)	/* polling */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
  		snd_uart16550_io_loop(uart);
  
  	if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
  		while (1) {
  			/* buffer full? */
  			/* in this mode we need two bytes of space */
  			if (uart->buff_in_count > TX_BUFF_SIZE - 2)
  				break;
  			if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
  				break;
  #ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
  			/* select exactly one of the four ports */
  			addr_byte = (1 << (substream->number + 4)) | 0x08;
  #else
  			/* select any combination of the four ports */
  			addr_byte = (substream->number << 4) | 0x08;
  			/* ...except none */
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
663
664
  			if (addr_byte == 0x08)
  				addr_byte = 0xf8;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
665
666
667
668
669
670
671
  #endif
  			snd_uart16550_output_byte(uart, substream, addr_byte);
  			/* send midi byte */
  			snd_uart16550_output_byte(uart, substream, midi_byte);
  		}
  	} else {
  		first = 0;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
672
673
674
675
676
677
678
  		while (snd_rawmidi_transmit_peek(substream, &midi_byte, 1) == 1) {
  			/* Also send F5 after 3 seconds with no data
  			 * to handle device disconnect */
  			if (first == 0 &&
  			    (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
  			     uart->adaptor == SNDRV_SERIAL_GENERIC) &&
  			    (uart->prev_out != substream->number ||
f11b79928   Julia Lawall   [ALSA] sound: Use...
679
  			     time_after(jiffies, lasttime + 3*HZ))) {
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
680
681
  
  				if (snd_uart16550_buffer_can_write(uart, 3)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
682
  					/* Roland Soundcanvas part selection */
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
683
684
685
686
687
  					/* If this substream of the data is
  					 * different previous substream
  					 * in this uart, send the change part
  					 * event
  					 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
  					uart->prev_out = substream->number;
  					/* change part */
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
690
691
  					snd_uart16550_output_byte(uart, substream,
  								  0xf5);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692
  					/* data */
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
693
694
695
696
697
698
  					snd_uart16550_output_byte(uart, substream,
  								  uart->prev_out + 1);
  					/* If midi_byte is a data byte,
  					 * send the previous status byte */
  					if (midi_byte < 0x80 &&
  					    uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
699
  						snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
700
  				} else if (!uart->drop_on_full)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
702
703
704
705
  					break;
  
  			}
  
  			/* send midi byte */
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
706
707
  			if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
  			    !uart->drop_on_full )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708
709
710
711
712
713
714
715
716
717
718
719
  				break;
  
  			if (midi_byte >= 0x80 && midi_byte < 0xf0)
  				uart->prev_status[uart->prev_out] = midi_byte;
  			first = 1;
  
  			snd_rawmidi_transmit_ack( substream, 1 );
  		}
  		lasttime = jiffies;
  	}
  	spin_unlock_irqrestore(&uart->open_lock, flags);
  }
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
720
721
  static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
  					 int up)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
722
723
  {
  	unsigned long flags;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
724
  	struct snd_uart16550 *uart = substream->rmidi->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
726
  
  	spin_lock_irqsave(&uart->open_lock, flags);
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
727
  	if (up)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
728
  		uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
729
  	else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
730
  		uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731
732
733
734
  	spin_unlock_irqrestore(&uart->open_lock, flags);
  	if (up)
  		snd_uart16550_output_write(substream);
  }
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
735
  static struct snd_rawmidi_ops snd_uart16550_output =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
738
739
740
  {
  	.open =		snd_uart16550_output_open,
  	.close =	snd_uart16550_output_close,
  	.trigger =	snd_uart16550_output_trigger,
  };
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
741
  static struct snd_rawmidi_ops snd_uart16550_input =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
742
743
744
745
746
  {
  	.open =		snd_uart16550_input_open,
  	.close =	snd_uart16550_input_close,
  	.trigger =	snd_uart16550_input_trigger,
  };
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
747
  static int snd_uart16550_free(struct snd_uart16550 *uart)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
748
749
  {
  	if (uart->irq >= 0)
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
750
  		free_irq(uart->irq, uart);
b1d5776d8   Takashi Iwai   [ALSA] Remove vma...
751
  	release_and_free_resource(uart->res_base);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
752
753
754
  	kfree(uart);
  	return 0;
  };
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
755
  static int snd_uart16550_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
756
  {
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
757
  	struct snd_uart16550 *uart = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
758
759
  	return snd_uart16550_free(uart);
  }
788c60433   Prarit Bhargava   [ALSA] Fix __devi...
760
  static int __devinit snd_uart16550_create(struct snd_card *card,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
762
763
764
765
766
  				       unsigned long iobase,
  				       int irq,
  				       unsigned int speed,
  				       unsigned int base,
  				       int adaptor,
  				       int droponfull,
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
767
  				       struct snd_uart16550 **ruart)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
768
  {
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
769
  	static struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
770
771
  		.dev_free =	snd_uart16550_dev_free,
  	};
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
772
  	struct snd_uart16550 *uart;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
773
  	int err;
561b220a4   Takashi Iwai   [ALSA] Replace wi...
774
  	if ((uart = kzalloc(sizeof(*uart), GFP_KERNEL)) == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
775
776
777
778
779
780
781
782
783
784
785
  		return -ENOMEM;
  	uart->adaptor = adaptor;
  	uart->card = card;
  	spin_lock_init(&uart->open_lock);
  	uart->irq = -1;
  	uart->base = iobase;
  	uart->drop_on_full = droponfull;
  
  	if ((err = snd_uart16550_detect(uart)) <= 0) {
  		printk(KERN_ERR "no UART detected at 0x%lx
  ", iobase);
202728d78   Adrian Bunk   [ALSA] fix some m...
786
  		snd_uart16550_free(uart);
68b8bc052   Takashi Iwai   [ALSA] serial-uar...
787
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
789
790
791
  	}
  
  	if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
  		if (request_irq(irq, snd_uart16550_interrupt,
88e24c3a4   Yong Zhang   sound: irq: Remov...
792
  				0, "Serial MIDI", uart)) {
45203832d   Takashi Iwai   ALSA: Add missing...
793
794
795
  			snd_printk(KERN_WARNING
  				   "irq %d busy. Using Polling.
  ", irq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
  		} else {
  			uart->irq = irq;
  		}
  	}
  	uart->divisor = base / speed;
  	uart->speed = base / (unsigned int)uart->divisor;
  	uart->speed_base = base;
  	uart->prev_out = -1;
  	uart->prev_in = 0;
  	uart->rstatus = 0;
  	memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
  	init_timer(&uart->buffer_timer);
  	uart->buffer_timer.function = snd_uart16550_buffer_timer;
  	uart->buffer_timer.data = (unsigned long)uart;
  	uart->timer_running = 0;
  
  	/* Register device */
  	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops)) < 0) {
  		snd_uart16550_free(uart);
  		return err;
  	}
  
  	switch (uart->adaptor) {
  	case SNDRV_SERIAL_MS124W_SA:
  	case SNDRV_SERIAL_MS124W_MB:
  		/* MS-124W can draw power from RTS and DTR if they
  		   are in opposite states. */ 
  		outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
  		break;
  	case SNDRV_SERIAL_MS124T:
  		/* MS-124T can draw power from RTS and/or DTR (preferably
  		   both) if they are asserted. */
  		outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
  		break;
  	default:
  		break;
  	}
  
  	if (ruart)
  		*ruart = uart;
  
  	return 0;
  }
788c60433   Prarit Bhargava   [ALSA] Fix __devi...
839
  static void __devinit snd_uart16550_substreams(struct snd_rawmidi_str *stream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
840
  {
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
841
  	struct snd_rawmidi_substream *substream;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842

0b830bac3   Takashi Iwai   [ALSA] Clean up s...
843
  	list_for_each_entry(substream, &stream->substreams, list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
844
845
846
  		sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
  	}
  }
788c60433   Prarit Bhargava   [ALSA] Fix __devi...
847
  static int __devinit snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
848
849
  				      int outs, int ins,
  				      struct snd_rawmidi **rmidi)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
  {
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
851
  	struct snd_rawmidi *rrawmidi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
852
  	int err;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
853
854
855
  	err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
  			      outs, ins, &rrawmidi);
  	if (err < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
856
  		return err;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
857
858
859
860
  	snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  			    &snd_uart16550_input);
  	snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  			    &snd_uart16550_output);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
861
862
863
864
865
866
867
868
869
870
871
  	strcpy(rrawmidi->name, "Serial MIDI");
  	snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
  	snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
  	rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  			       SNDRV_RAWMIDI_INFO_INPUT |
  			       SNDRV_RAWMIDI_INFO_DUPLEX;
  	rrawmidi->private_data = uart;
  	if (rmidi)
  		*rmidi = rrawmidi;
  	return 0;
  }
788c60433   Prarit Bhargava   [ALSA] Fix __devi...
872
  static int __devinit snd_serial_probe(struct platform_device *devptr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
873
  {
4a4d2cfd8   Takashi Iwai   [ALSA] Remove xxx...
874
  	struct snd_card *card;
0b830bac3   Takashi Iwai   [ALSA] Clean up s...
875
  	struct snd_uart16550 *uart;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
876
  	int err;
9caf6b590   Takashi Iwai   [ALSA] serial-u16...
877
  	int dev = devptr->id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
  
  	switch (adaptor[dev]) {
  	case SNDRV_SERIAL_SOUNDCANVAS:
  		ins[dev] = 1;
  		break;
  	case SNDRV_SERIAL_MS124T:
  	case SNDRV_SERIAL_MS124W_SA:
  		outs[dev] = 1;
  		ins[dev] = 1;
  		break;
  	case SNDRV_SERIAL_MS124W_MB:
  		outs[dev] = 16;
  		ins[dev] = 1;
  		break;
  	case SNDRV_SERIAL_GENERIC:
  		break;
  	default:
45203832d   Takashi Iwai   ALSA: Add missing...
895
896
897
  		snd_printk(KERN_ERR
  			   "Adaptor type is out of range 0-%d (%d)
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
898
899
900
901
902
  			   SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
  		return -ENODEV;
  	}
  
  	if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
45203832d   Takashi Iwai   ALSA: Add missing...
903
904
905
  		snd_printk(KERN_ERR
  			   "Count of outputs is out of range 1-%d (%d)
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
906
907
908
909
910
  			   SNDRV_SERIAL_MAX_OUTS, outs[dev]);
  		return -ENODEV;
  	}
  
  	if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
45203832d   Takashi Iwai   ALSA: Add missing...
911
912
913
  		snd_printk(KERN_ERR
  			   "Count of inputs is out of range 1-%d (%d)
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
914
915
916
  			   SNDRV_SERIAL_MAX_INS, ins[dev]);
  		return -ENODEV;
  	}
bd7dd77c2   Takashi Iwai   ALSA: Convert to ...
917
918
919
  	err  = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  	if (err < 0)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
921
922
923
924
925
926
927
928
929
930
  
  	strcpy(card->driver, "Serial");
  	strcpy(card->shortname, "Serial MIDI (UART16550A)");
  
  	if ((err = snd_uart16550_create(card,
  					port[dev],
  					irq[dev],
  					speed[dev],
  					base[dev],
  					adaptor[dev],
  					droponfull[dev],
16dab54b8   Takashi Iwai   [ALSA] Add snd_ca...
931
932
  					&uart)) < 0)
  		goto _err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
933

0b830bac3   Takashi Iwai   [ALSA] Clean up s...
934
935
  	err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
  	if (err < 0)
16dab54b8   Takashi Iwai   [ALSA] Add snd_ca...
936
  		goto _err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
937

bd0185cea   Clemens Ladisch   sound: serial-u16...
938
  	sprintf(card->longname, "%s [%s] at %#lx, irq %d",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939
  		card->shortname,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
940
  		adaptor_names[uart->adaptor],
bd0185cea   Clemens Ladisch   sound: serial-u16...
941
942
  		uart->base,
  		uart->irq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
943

9caf6b590   Takashi Iwai   [ALSA] serial-u16...
944
  	snd_card_set_dev(card, &devptr->dev);
16dab54b8   Takashi Iwai   [ALSA] Add snd_ca...
945
946
947
  
  	if ((err = snd_card_register(card)) < 0)
  		goto _err;
9caf6b590   Takashi Iwai   [ALSA] serial-u16...
948
  	platform_set_drvdata(devptr, card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
949
  	return 0;
16dab54b8   Takashi Iwai   [ALSA] Add snd_ca...
950
951
952
953
  
   _err:
  	snd_card_free(card);
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
954
  }
788c60433   Prarit Bhargava   [ALSA] Fix __devi...
955
  static int __devexit snd_serial_remove(struct platform_device *devptr)
9caf6b590   Takashi Iwai   [ALSA] serial-u16...
956
957
958
959
960
961
962
963
964
965
  {
  	snd_card_free(platform_get_drvdata(devptr));
  	platform_set_drvdata(devptr, NULL);
  	return 0;
  }
  
  #define SND_SERIAL_DRIVER	"snd_serial_u16550"
  
  static struct platform_driver snd_serial_driver = {
  	.probe		= snd_serial_probe,
788c60433   Prarit Bhargava   [ALSA] Fix __devi...
966
  	.remove		= __devexit_p( snd_serial_remove),
9caf6b590   Takashi Iwai   [ALSA] serial-u16...
967
968
969
970
  	.driver		= {
  		.name	= SND_SERIAL_DRIVER
  	},
  };
bdec0c728   Randy Dunlap   [ALSA] fix sectio...
971
  static void snd_serial_unregister_all(void)
f7a9275d9   Clemens Ladisch   [ALSA] unregister...
972
973
974
975
976
977
978
  {
  	int i;
  
  	for (i = 0; i < ARRAY_SIZE(devices); ++i)
  		platform_device_unregister(devices[i]);
  	platform_driver_unregister(&snd_serial_driver);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
979
980
  static int __init alsa_card_serial_init(void)
  {
9caf6b590   Takashi Iwai   [ALSA] serial-u16...
981
  	int i, cards, err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982

9caf6b590   Takashi Iwai   [ALSA] serial-u16...
983
984
  	if ((err = platform_driver_register(&snd_serial_driver)) < 0)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
985

9caf6b590   Takashi Iwai   [ALSA] serial-u16...
986
  	cards = 0;
8278ca8fe   Takashi Iwai   [ALSA] Fix check ...
987
  	for (i = 0; i < SNDRV_CARDS; i++) {
9caf6b590   Takashi Iwai   [ALSA] serial-u16...
988
  		struct platform_device *device;
8278ca8fe   Takashi Iwai   [ALSA] Fix check ...
989
990
  		if (! enable[i])
  			continue;
9caf6b590   Takashi Iwai   [ALSA] serial-u16...
991
992
  		device = platform_device_register_simple(SND_SERIAL_DRIVER,
  							 i, NULL, 0);
a182ee987   Rene Herman   [ALSA] continue o...
993
994
  		if (IS_ERR(device))
  			continue;
7152447df   Rene Herman   [ALSA] unregister...
995
996
997
998
  		if (!platform_get_drvdata(device)) {
  			platform_device_unregister(device);
  			continue;
  		}
f7a9275d9   Clemens Ladisch   [ALSA] unregister...
999
  		devices[i] = device;
9caf6b590   Takashi Iwai   [ALSA] serial-u16...
1000
1001
1002
  		cards++;
  	}
  	if (! cards) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1003
1004
1005
1006
  #ifdef MODULE
  		printk(KERN_ERR "serial midi soundcard not found or device busy
  ");
  #endif
a182ee987   Rene Herman   [ALSA] continue o...
1007
1008
  		snd_serial_unregister_all();
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1009
1010
1011
1012
1013
1014
  	}
  	return 0;
  }
  
  static void __exit alsa_card_serial_exit(void)
  {
f7a9275d9   Clemens Ladisch   [ALSA] unregister...
1015
  	snd_serial_unregister_all();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1016
1017
1018
1019
  }
  
  module_init(alsa_card_serial_init)
  module_exit(alsa_card_serial_exit)