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