1/*
2 *  esp.c - driver for Hayes ESP serial cards
3 *
4 *  --- Notices from serial.c, upon which this driver is based ---
5 *
6 *  Copyright (C) 1991, 1992  Linus Torvalds
7 *
8 *  Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92.  Now
9 *  much more extensible to support other serial cards based on the
10 *  16450/16550A UART's.  Added support for the AST FourPort and the
11 *  Accent Async board.
12 *
13 *  set_serial_info fixed to set the flags, custom divisor, and uart
14 * 	type fields.  Fix suggested by Michael K. Johnson 12/12/92.
15 *
16 *  11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
17 *
18 *  03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
19 *
20 *  rs_set_termios fixed to look also for changes of the input
21 *      flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
22 *                                            Bernd Anh���pl 05/17/96.
23 *
24 * --- End of notices from serial.c ---
25 *
26 * Support for the ESP serial card by Andrew J. Robinson
27 *     <arobinso@nyx.net> (Card detection routine taken from a patch
28 *     by Dennis J. Boylan).  Patches to allow use with 2.1.x contributed
29 *     by Chris Faylor.
30 *
31 * Most recent changes: (Andrew J. Robinson)
32 *   Support for PIO mode.  This allows the driver to work properly with
33 *     multiport cards.
34 *
35 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> -
36 * several cleanups, use module_init/module_exit, etc
37 *
38 * This module exports the following rs232 io functions:
39 *
40 *	int espserial_init(void);
41 */
42
43#include <linux/module.h>
44#include <linux/errno.h>
45#include <linux/signal.h>
46#include <linux/sched.h>
47#include <linux/interrupt.h>
48#include <linux/tty.h>
49#include <linux/tty_flip.h>
50#include <linux/serial.h>
51#include <linux/serialP.h>
52#include <linux/serial_reg.h>
53#include <linux/major.h>
54#include <linux/string.h>
55#include <linux/fcntl.h>
56#include <linux/ptrace.h>
57#include <linux/ioport.h>
58#include <linux/mm.h>
59#include <linux/init.h>
60#include <linux/delay.h>
61
62#include <asm/system.h>
63#include <asm/io.h>
64#include <asm/bitops.h>
65
66#include <asm/dma.h>
67#include <linux/slab.h>
68#include <asm/uaccess.h>
69
70#include <linux/hayesesp.h>
71
72#define NR_PORTS 64	/* maximum number of ports */
73#define NR_PRIMARY 8	/* maximum number of primary ports */
74#define REGION_SIZE 8   /* size of io region to request */
75
76/* The following variables can be set by giving module options */
77static int irq[NR_PRIMARY];	/* IRQ for each base port */
78static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
79static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
80static unsigned int rx_trigger = ESP_RX_TRIGGER;
81static unsigned int tx_trigger = ESP_TX_TRIGGER;
82static unsigned int flow_off = ESP_FLOW_OFF;
83static unsigned int flow_on = ESP_FLOW_ON;
84static unsigned int rx_timeout = ESP_RX_TMOUT;
85static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
86
87MODULE_LICENSE("GPL");
88
89module_param_array(irq, int, NULL, 0);
90module_param_array(divisor, uint, NULL, 0);
91module_param(dma, uint, 0);
92module_param(rx_trigger, uint, 0);
93module_param(tx_trigger, uint, 0);
94module_param(flow_off, uint, 0);
95module_param(flow_on, uint, 0);
96module_param(rx_timeout, uint, 0);
97module_param(pio_threshold, uint, 0);
98
99/* END */
100
101static char *dma_buffer;
102static int dma_bytes;
103static struct esp_pio_buffer *free_pio_buf;
104
105#define DMA_BUFFER_SZ 1024
106
107#define WAKEUP_CHARS 1024
108
109static char serial_name[] __initdata = "ESP serial driver";
110static char serial_version[] __initdata = "2.2";
111
112static struct tty_driver *esp_driver;
113
114/* serial subtype definitions */
115#define SERIAL_TYPE_NORMAL	1
116
117/*
118 * Serial driver configuration section.  Here are the various options:
119 *
120 * SERIAL_PARANOIA_CHECK
121 * 		Check the magic number for the esp_structure where
122 * 		ever possible.
123 */
124
125#undef SERIAL_PARANOIA_CHECK
126#define SERIAL_DO_RESTART
127
128#undef SERIAL_DEBUG_INTR
129#undef SERIAL_DEBUG_OPEN
130#undef SERIAL_DEBUG_FLOW
131
132#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
133#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
134 tty->name, (info->flags), serial_driver.refcount,info->count,tty->count,s)
135#else
136#define DBG_CNT(s)
137#endif
138
139static struct esp_struct *ports;
140
141static void change_speed(struct esp_struct *info);
142static void rs_wait_until_sent(struct tty_struct *, int);
143
144/*
145 * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
146 * times the normal 1.8432 Mhz clock of most serial boards).
147 */
148#define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
149
150/* Standard COM flags (except for COM4, because of the 8514 problem) */
151#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
152
153static inline int serial_paranoia_check(struct esp_struct *info,
154					char *name, const char *routine)
155{
156#ifdef SERIAL_PARANOIA_CHECK
157	static const char badmagic[] = KERN_WARNING
158		"Warning: bad magic number for serial struct (%s) in %s\n";
159	static const char badinfo[] = KERN_WARNING
160		"Warning: null esp_struct for (%s) in %s\n";
161
162	if (!info) {
163		printk(badinfo, name, routine);
164		return 1;
165	}
166	if (info->magic != ESP_MAGIC) {
167		printk(badmagic, name, routine);
168		return 1;
169	}
170#endif
171	return 0;
172}
173
174static inline unsigned int serial_in(struct esp_struct *info, int offset)
175{
176	return inb(info->port + offset);
177}
178
179static inline void serial_out(struct esp_struct *info, int offset,
180			      unsigned char value)
181{
182	outb(value, info->port+offset);
183}
184
185/*
186 * ------------------------------------------------------------
187 * rs_stop() and rs_start()
188 *
189 * This routines are called before setting or resetting tty->stopped.
190 * They enable or disable transmitter interrupts, as necessary.
191 * ------------------------------------------------------------
192 */
193static void rs_stop(struct tty_struct *tty)
194{
195	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
196	unsigned long flags;
197
198	if (serial_paranoia_check(info, tty->name, "rs_stop"))
199		return;
200
201	spin_lock_irqsave(&info->lock, flags);
202	if (info->IER & UART_IER_THRI) {
203		info->IER &= ~UART_IER_THRI;
204		serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
205		serial_out(info, UART_ESI_CMD2, info->IER);
206	}
207	spin_unlock_irqrestore(&info->lock, flags);
208}
209
210static void rs_start(struct tty_struct *tty)
211{
212	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
213	unsigned long flags;
214
215	if (serial_paranoia_check(info, tty->name, "rs_start"))
216		return;
217
218	spin_lock_irqsave(&info->lock, flags);
219	if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
220		info->IER |= UART_IER_THRI;
221		serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
222		serial_out(info, UART_ESI_CMD2, info->IER);
223	}
224	spin_unlock_irqrestore(&info->lock, flags);
225}
226
227/*
228 * ----------------------------------------------------------------------
229 *
230 * Here starts the interrupt handling routines.  All of the following
231 * subroutines are declared as inline and are folded into
232 * rs_interrupt().  They were separated out for readability's sake.
233 *
234 * Note: rs_interrupt() is a "fast" interrupt, which means that it
235 * runs with interrupts turned off.  People who may want to modify
236 * rs_interrupt() should try to keep the interrupt handler as fast as
237 * possible.  After you are done making modifications, it is not a bad
238 * idea to do:
239 *
240 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
241 *
242 * and look at the resulting assemble code in serial.s.
243 *
244 * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
245 * -----------------------------------------------------------------------
246 */
247
248/*
249 * This routine is used by the interrupt handler to schedule
250 * processing in the software interrupt portion of the driver.
251 */
252static inline void rs_sched_event(struct esp_struct *info,
253				  int event)
254{
255	info->event |= 1 << event;
256	schedule_work(&info->tqueue);
257}
258
259static DEFINE_SPINLOCK(pio_lock);
260
261static inline struct esp_pio_buffer *get_pio_buffer(void)
262{
263	struct esp_pio_buffer *buf;
264	unsigned long flags;
265
266	spin_lock_irqsave(&pio_lock, flags);
267	if (free_pio_buf) {
268		buf = free_pio_buf;
269		free_pio_buf = buf->next;
270	} else {
271		buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
272	}
273	spin_unlock_irqrestore(&pio_lock, flags);
274	return buf;
275}
276
277static inline void release_pio_buffer(struct esp_pio_buffer *buf)
278{
279	unsigned long flags;
280	spin_lock_irqsave(&pio_lock, flags);
281	buf->next = free_pio_buf;
282	free_pio_buf = buf;
283	spin_unlock_irqrestore(&pio_lock, flags);
284}
285
286static inline void receive_chars_pio(struct esp_struct *info, int num_bytes)
287{
288	struct tty_struct *tty = info->tty;
289	int i;
290	struct esp_pio_buffer *pio_buf;
291	struct esp_pio_buffer *err_buf;
292	unsigned char status_mask;
293
294	pio_buf = get_pio_buffer();
295
296	if (!pio_buf)
297		return;
298
299	err_buf = get_pio_buffer();
300
301	if (!err_buf) {
302		release_pio_buffer(pio_buf);
303		return;
304	}
305
306	status_mask = (info->read_status_mask >> 2) & 0x07;
307
308	for (i = 0; i < num_bytes - 1; i += 2) {
309		*((unsigned short *)(pio_buf->data + i)) =
310			inw(info->port + UART_ESI_RX);
311		err_buf->data[i] = serial_in(info, UART_ESI_RWS);
312		err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
313		err_buf->data[i] &= status_mask;
314	}
315
316	if (num_bytes & 0x0001) {
317		pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
318		err_buf->data[num_bytes - 1] =
319			(serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
320	}
321
322	/* make sure everything is still ok since interrupts were enabled */
323	tty = info->tty;
324
325	if (!tty) {
326		release_pio_buffer(pio_buf);
327		release_pio_buffer(err_buf);
328		info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
329		return;
330	}
331
332	status_mask = (info->ignore_status_mask >> 2) & 0x07;
333
334	for (i = 0; i < num_bytes; i++) {
335		if (!(err_buf->data[i] & status_mask)) {
336			int flag = 0;
337
338			if (err_buf->data[i] & 0x04) {
339				flag = TTY_BREAK;
340				if (info->flags & ASYNC_SAK)
341					do_SAK(tty);
342			}
343			else if (err_buf->data[i] & 0x02)
344				flag = TTY_FRAME;
345			else if (err_buf->data[i] & 0x01)
346				flag = TTY_PARITY;
347			tty_insert_flip_char(tty, pio_buf->data[i], flag);
348		}
349	}
350
351	tty_schedule_flip(tty);
352
353	info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
354	release_pio_buffer(pio_buf);
355	release_pio_buffer(err_buf);
356}
357
358static inline void receive_chars_dma(struct esp_struct *info, int num_bytes)
359{
360	unsigned long flags;
361	info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
362	dma_bytes = num_bytes;
363	info->stat_flags |= ESP_STAT_DMA_RX;
364
365	flags=claim_dma_lock();
366        disable_dma(dma);
367        clear_dma_ff(dma);
368        set_dma_mode(dma, DMA_MODE_READ);
369        set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
370        set_dma_count(dma, dma_bytes);
371        enable_dma(dma);
372        release_dma_lock(flags);
373
374        serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
375}
376
377static inline void receive_chars_dma_done(struct esp_struct *info,
378					    int status)
379{
380	struct tty_struct *tty = info->tty;
381	int num_bytes;
382	unsigned long flags;
383
384	flags=claim_dma_lock();
385	disable_dma(dma);
386	clear_dma_ff(dma);
387
388	info->stat_flags &= ~ESP_STAT_DMA_RX;
389	num_bytes = dma_bytes - get_dma_residue(dma);
390	release_dma_lock(flags);
391
392	info->icount.rx += num_bytes;
393
394	if (num_bytes > 0) {
395		tty_insert_flip_string(tty, dma_buffer, num_bytes - 1);
396
397		status &= (0x1c & info->read_status_mask);
398
399		/* Is the status significant or do we throw the last byte ? */
400		if (!(status & info->ignore_status_mask)) {
401			int statflag = 0;
402
403			if (status & 0x10) {
404				statflag = TTY_BREAK;
405				(info->icount.brk)++;
406				if (info->flags & ASYNC_SAK)
407					do_SAK(tty);
408			} else if (status & 0x08) {
409				statflag = TTY_FRAME;
410				(info->icount.frame)++;
411			}
412			else if (status & 0x04) {
413				statflag = TTY_PARITY;
414				(info->icount.parity)++;
415			}
416			tty_insert_flip_char(tty, dma_buffer[num_bytes - 1], statflag);
417		}
418		tty_schedule_flip(tty);
419	}
420
421	if (dma_bytes != num_bytes) {
422		num_bytes = dma_bytes - num_bytes;
423		dma_bytes = 0;
424		receive_chars_dma(info, num_bytes);
425	} else
426		dma_bytes = 0;
427}
428
429/* Caller must hold info->lock */
430
431static inline void transmit_chars_pio(struct esp_struct *info,
432					int space_avail)
433{
434	int i;
435	struct esp_pio_buffer *pio_buf;
436
437	pio_buf = get_pio_buffer();
438
439	if (!pio_buf)
440		return;
441
442	while (space_avail && info->xmit_cnt) {
443		if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
444			memcpy(pio_buf->data,
445			       &(info->xmit_buf[info->xmit_tail]),
446			       space_avail);
447		} else {
448			i = ESP_XMIT_SIZE - info->xmit_tail;
449			memcpy(pio_buf->data,
450			       &(info->xmit_buf[info->xmit_tail]), i);
451			memcpy(&(pio_buf->data[i]), info->xmit_buf,
452			       space_avail - i);
453		}
454
455		info->xmit_cnt -= space_avail;
456		info->xmit_tail = (info->xmit_tail + space_avail) &
457			(ESP_XMIT_SIZE - 1);
458
459		for (i = 0; i < space_avail - 1; i += 2) {
460			outw(*((unsigned short *)(pio_buf->data + i)),
461			     info->port + UART_ESI_TX);
462		}
463
464		if (space_avail & 0x0001)
465			serial_out(info, UART_ESI_TX,
466				   pio_buf->data[space_avail - 1]);
467
468		if (info->xmit_cnt) {
469			serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
470			serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
471			space_avail = serial_in(info, UART_ESI_STAT1) << 8;
472			space_avail |= serial_in(info, UART_ESI_STAT2);
473
474			if (space_avail > info->xmit_cnt)
475				space_avail = info->xmit_cnt;
476		}
477	}
478
479	if (info->xmit_cnt < WAKEUP_CHARS) {
480		rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
481
482#ifdef SERIAL_DEBUG_INTR
483		printk("THRE...");
484#endif
485
486		if (info->xmit_cnt <= 0) {
487			info->IER &= ~UART_IER_THRI;
488			serial_out(info, UART_ESI_CMD1,
489				   ESI_SET_SRV_MASK);
490			serial_out(info, UART_ESI_CMD2, info->IER);
491		}
492	}
493
494	release_pio_buffer(pio_buf);
495}
496
497/* Caller must hold info->lock */
498static inline void transmit_chars_dma(struct esp_struct *info, int num_bytes)
499{
500	unsigned long flags;
501
502	dma_bytes = num_bytes;
503
504	if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
505		memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
506		       dma_bytes);
507	} else {
508		int i = ESP_XMIT_SIZE - info->xmit_tail;
509		memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
510			i);
511		memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
512	}
513
514	info->xmit_cnt -= dma_bytes;
515	info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
516
517	if (info->xmit_cnt < WAKEUP_CHARS) {
518		rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
519
520#ifdef SERIAL_DEBUG_INTR
521		printk("THRE...");
522#endif
523
524		if (info->xmit_cnt <= 0) {
525			info->IER &= ~UART_IER_THRI;
526			serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
527			serial_out(info, UART_ESI_CMD2, info->IER);
528		}
529	}
530
531	info->stat_flags |= ESP_STAT_DMA_TX;
532
533	flags=claim_dma_lock();
534        disable_dma(dma);
535        clear_dma_ff(dma);
536        set_dma_mode(dma, DMA_MODE_WRITE);
537        set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
538        set_dma_count(dma, dma_bytes);
539        enable_dma(dma);
540        release_dma_lock(flags);
541
542        serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
543}
544
545static inline void transmit_chars_dma_done(struct esp_struct *info)
546{
547	int num_bytes;
548	unsigned long flags;
549
550
551	flags=claim_dma_lock();
552	disable_dma(dma);
553	clear_dma_ff(dma);
554
555	num_bytes = dma_bytes - get_dma_residue(dma);
556	info->icount.tx += dma_bytes;
557	release_dma_lock(flags);
558
559	if (dma_bytes != num_bytes) {
560		dma_bytes -= num_bytes;
561		memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
562
563		flags=claim_dma_lock();
564        	disable_dma(dma);
565        	clear_dma_ff(dma);
566        	set_dma_mode(dma, DMA_MODE_WRITE);
567        	set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
568        	set_dma_count(dma, dma_bytes);
569        	enable_dma(dma);
570        	release_dma_lock(flags);
571
572        	serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
573	} else {
574		dma_bytes = 0;
575		info->stat_flags &= ~ESP_STAT_DMA_TX;
576	}
577}
578
579static inline void check_modem_status(struct esp_struct *info)
580{
581	int	status;
582
583	serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
584	status = serial_in(info, UART_ESI_STAT2);
585
586	if (status & UART_MSR_ANY_DELTA) {
587		/* update input line counters */
588		if (status & UART_MSR_TERI)
589			info->icount.rng++;
590		if (status & UART_MSR_DDSR)
591			info->icount.dsr++;
592		if (status & UART_MSR_DDCD)
593			info->icount.dcd++;
594		if (status & UART_MSR_DCTS)
595			info->icount.cts++;
596		wake_up_interruptible(&info->delta_msr_wait);
597	}
598
599	if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
600#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
601		printk("ttys%d CD now %s...", info->line,
602		       (status & UART_MSR_DCD) ? "on" : "off");
603#endif
604		if (status & UART_MSR_DCD)
605			wake_up_interruptible(&info->open_wait);
606		else {
607#ifdef SERIAL_DEBUG_OPEN
608			printk("scheduling hangup...");
609#endif
610			schedule_work(&info->tqueue_hangup);
611		}
612	}
613}
614
615/*
616 * This is the serial driver's interrupt routine
617 */
618static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
619{
620	struct esp_struct * info;
621	unsigned err_status;
622	unsigned int scratch;
623
624#ifdef SERIAL_DEBUG_INTR
625	printk("rs_interrupt_single(%d)...", irq);
626#endif
627	info = (struct esp_struct *)dev_id;
628	err_status = 0;
629	scratch = serial_in(info, UART_ESI_SID);
630
631	spin_lock(&info->lock);
632
633	if (!info->tty) {
634		spin_unlock(&info->lock);
635		return IRQ_NONE;
636	}
637
638	if (scratch & 0x04) { /* error */
639		serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
640		err_status = serial_in(info, UART_ESI_STAT1);
641		serial_in(info, UART_ESI_STAT2);
642
643		if (err_status & 0x01)
644			info->stat_flags |= ESP_STAT_RX_TIMEOUT;
645
646		if (err_status & 0x20) /* UART status */
647			check_modem_status(info);
648
649		if (err_status & 0x80) /* Start break */
650			wake_up_interruptible(&info->break_wait);
651	}
652
653	if ((scratch & 0x88) || /* DMA completed or timed out */
654	    (err_status & 0x1c) /* receive error */) {
655		if (info->stat_flags & ESP_STAT_DMA_RX)
656			receive_chars_dma_done(info, err_status);
657		else if (info->stat_flags & ESP_STAT_DMA_TX)
658			transmit_chars_dma_done(info);
659	}
660
661	if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
662	    ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
663	    (info->IER & UART_IER_RDI)) {
664		int num_bytes;
665
666		serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
667		serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
668		num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
669		num_bytes |= serial_in(info, UART_ESI_STAT2);
670
671		num_bytes = tty_buffer_request_room(info->tty, num_bytes);
672
673		if (num_bytes) {
674			if (dma_bytes ||
675			    (info->stat_flags & ESP_STAT_USE_PIO) ||
676			    (num_bytes <= info->config.pio_threshold))
677				receive_chars_pio(info, num_bytes);
678			else
679				receive_chars_dma(info, num_bytes);
680		}
681	}
682
683	if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
684	    (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
685		if ((info->xmit_cnt <= 0) || info->tty->stopped) {
686			info->IER &= ~UART_IER_THRI;
687			serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
688			serial_out(info, UART_ESI_CMD2, info->IER);
689		} else {
690			int num_bytes;
691
692			serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
693			serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
694			num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
695			num_bytes |= serial_in(info, UART_ESI_STAT2);
696
697			if (num_bytes > info->xmit_cnt)
698				num_bytes = info->xmit_cnt;
699
700			if (num_bytes) {
701				if (dma_bytes ||
702				    (info->stat_flags & ESP_STAT_USE_PIO) ||
703				    (num_bytes <= info->config.pio_threshold))
704					transmit_chars_pio(info, num_bytes);
705				else
706					transmit_chars_dma(info, num_bytes);
707			}
708		}
709	}
710
711	info->last_active = jiffies;
712
713#ifdef SERIAL_DEBUG_INTR
714	printk("end.\n");
715#endif
716	spin_unlock(&info->lock);
717	return IRQ_HANDLED;
718}
719
720/*
721 * -------------------------------------------------------------------
722 * Here ends the serial interrupt routines.
723 * -------------------------------------------------------------------
724 */
725
726static void do_softint(struct work_struct *work)
727{
728	struct esp_struct	*info =
729		container_of(work, struct esp_struct, tqueue);
730	struct tty_struct	*tty;
731
732	tty = info->tty;
733	if (!tty)
734		return;
735
736	if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
737		tty_wakeup(tty);
738	}
739}
740
741/*
742 * This routine is called from the scheduler tqueue when the interrupt
743 * routine has signalled that a hangup has occurred.  The path of
744 * hangup processing is:
745 *
746 * 	serial interrupt routine -> (scheduler tqueue) ->
747 * 	do_serial_hangup() -> tty->hangup() -> esp_hangup()
748 *
749 */
750static void do_serial_hangup(struct work_struct *work)
751{
752	struct esp_struct	*info =
753		container_of(work, struct esp_struct, tqueue_hangup);
754	struct tty_struct	*tty;
755
756	tty = info->tty;
757	if (tty)
758		tty_hangup(tty);
759}
760
761/*
762 * ---------------------------------------------------------------
763 * Low level utility subroutines for the serial driver:  routines to
764 * figure out the appropriate timeout for an interrupt chain, routines
765 * to initialize and startup a serial port, and routines to shutdown a
766 * serial port.  Useful stuff like that.
767 *
768 * Caller should hold lock
769 * ---------------------------------------------------------------
770 */
771
772static inline void esp_basic_init(struct esp_struct * info)
773{
774	/* put ESPC in enhanced mode */
775	serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
776
777	if (info->stat_flags & ESP_STAT_NEVER_DMA)
778		serial_out(info, UART_ESI_CMD2, 0x01);
779	else
780		serial_out(info, UART_ESI_CMD2, 0x31);
781
782	/* disable interrupts for now */
783	serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
784	serial_out(info, UART_ESI_CMD2, 0x00);
785
786	/* set interrupt and DMA channel */
787	serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
788
789	if (info->stat_flags & ESP_STAT_NEVER_DMA)
790		serial_out(info, UART_ESI_CMD2, 0x01);
791	else
792		serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
793
794	serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
795
796	if (info->line % 8)	/* secondary port */
797		serial_out(info, UART_ESI_CMD2, 0x0d);	/* shared */
798	else if (info->irq == 9)
799		serial_out(info, UART_ESI_CMD2, 0x02);
800	else
801		serial_out(info, UART_ESI_CMD2, info->irq);
802
803	/* set error status mask (check this) */
804	serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
805
806	if (info->stat_flags & ESP_STAT_NEVER_DMA)
807		serial_out(info, UART_ESI_CMD2, 0xa1);
808	else
809		serial_out(info, UART_ESI_CMD2, 0xbd);
810
811	serial_out(info, UART_ESI_CMD2, 0x00);
812
813	/* set DMA timeout */
814	serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
815	serial_out(info, UART_ESI_CMD2, 0xff);
816
817	/* set FIFO trigger levels */
818	serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
819	serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
820	serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
821	serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
822	serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
823
824	/* Set clock scaling and wait states */
825	serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
826	serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
827
828	/* set reinterrupt pacing */
829	serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
830	serial_out(info, UART_ESI_CMD2, 0xff);
831}
832
833static int startup(struct esp_struct * info)
834{
835	unsigned long flags;
836	int	retval=0;
837        unsigned int num_chars;
838
839        spin_lock_irqsave(&info->lock, flags);
840
841	if (info->flags & ASYNC_INITIALIZED)
842		goto out;
843
844	if (!info->xmit_buf) {
845		info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_ATOMIC);
846		retval = -ENOMEM;
847		if (!info->xmit_buf)
848			goto out;
849	}
850
851#ifdef SERIAL_DEBUG_OPEN
852	printk("starting up ttys%d (irq %d)...", info->line, info->irq);
853#endif
854
855	/* Flush the RX buffer.  Using the ESI flush command may cause */
856	/* wild interrupts, so read all the data instead. */
857
858	serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
859	serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
860	num_chars = serial_in(info, UART_ESI_STAT1) << 8;
861	num_chars |= serial_in(info, UART_ESI_STAT2);
862
863	while (num_chars > 1) {
864		inw(info->port + UART_ESI_RX);
865		num_chars -= 2;
866	}
867
868	if (num_chars)
869		serial_in(info, UART_ESI_RX);
870
871	/* set receive character timeout */
872	serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
873	serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
874
875	/* clear all flags except the "never DMA" flag */
876	info->stat_flags &= ESP_STAT_NEVER_DMA;
877
878	if (info->stat_flags & ESP_STAT_NEVER_DMA)
879		info->stat_flags |= ESP_STAT_USE_PIO;
880
881	spin_unlock_irqrestore(&info->lock, flags);
882
883	/*
884	 * Allocate the IRQ
885	 */
886
887	retval = request_irq(info->irq, rs_interrupt_single, IRQF_SHARED,
888			     "esp serial", info);
889
890	if (retval) {
891		if (capable(CAP_SYS_ADMIN)) {
892			if (info->tty)
893				set_bit(TTY_IO_ERROR,
894					&info->tty->flags);
895			retval = 0;
896		}
897		goto out_unlocked;
898	}
899
900	if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
901		dma_buffer = (char *)__get_dma_pages(
902			GFP_KERNEL, get_order(DMA_BUFFER_SZ));
903
904		/* use PIO mode if DMA buf/chan cannot be allocated */
905		if (!dma_buffer)
906			info->stat_flags |= ESP_STAT_USE_PIO;
907		else if (request_dma(dma, "esp serial")) {
908			free_pages((unsigned long)dma_buffer,
909				   get_order(DMA_BUFFER_SZ));
910			dma_buffer = NULL;
911			info->stat_flags |= ESP_STAT_USE_PIO;
912		}
913
914	}
915
916	info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
917
918	spin_lock_irqsave(&info->lock, flags);
919	serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
920	serial_out(info, UART_ESI_CMD2, UART_MCR);
921	serial_out(info, UART_ESI_CMD2, info->MCR);
922
923	/*
924	 * Finally, enable interrupts
925	 */
926	/* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
927	info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
928			UART_IER_DMA_TC;
929	serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
930	serial_out(info, UART_ESI_CMD2, info->IER);
931
932	if (info->tty)
933		clear_bit(TTY_IO_ERROR, &info->tty->flags);
934	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
935	spin_unlock_irqrestore(&info->lock, flags);
936
937	/*
938	 * Set up the tty->alt_speed kludge
939	 */
940	if (info->tty) {
941		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
942			info->tty->alt_speed = 57600;
943		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
944			info->tty->alt_speed = 115200;
945		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
946			info->tty->alt_speed = 230400;
947		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
948			info->tty->alt_speed = 460800;
949	}
950
951	/*
952	 * set the speed of the serial port
953	 */
954	change_speed(info);
955	info->flags |= ASYNC_INITIALIZED;
956	return 0;
957
958out:
959	spin_unlock_irqrestore(&info->lock, flags);
960out_unlocked:
961	return retval;
962}
963
964/*
965 * This routine will shutdown a serial port; interrupts are disabled, and
966 * DTR is dropped if the hangup on close termio flag is on.
967 */
968static void shutdown(struct esp_struct * info)
969{
970	unsigned long	flags, f;
971
972	if (!(info->flags & ASYNC_INITIALIZED))
973		return;
974
975#ifdef SERIAL_DEBUG_OPEN
976	printk("Shutting down serial port %d (irq %d)....", info->line,
977	       info->irq);
978#endif
979
980	spin_lock_irqsave(&info->lock, flags);
981	/*
982	 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
983	 * here so the queue might never be waken up
984	 */
985	wake_up_interruptible(&info->delta_msr_wait);
986	wake_up_interruptible(&info->break_wait);
987
988	/* stop a DMA transfer on the port being closed */
989	/* DMA lock is higher priority always */
990	if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
991		f=claim_dma_lock();
992		disable_dma(dma);
993		clear_dma_ff(dma);
994		release_dma_lock(f);
995
996		dma_bytes = 0;
997	}
998
999	/*
1000	 * Free the IRQ
1001	 */
1002	free_irq(info->irq, info);
1003
1004	if (dma_buffer) {
1005		struct esp_struct *current_port = ports;
1006
1007		while (current_port) {
1008			if ((current_port != info) &&
1009			    (current_port->flags & ASYNC_INITIALIZED))
1010				break;
1011
1012			current_port = current_port->next_port;
1013		}
1014
1015		if (!current_port) {
1016			free_dma(dma);
1017			free_pages((unsigned long)dma_buffer,
1018				   get_order(DMA_BUFFER_SZ));
1019			dma_buffer = NULL;
1020		}
1021	}
1022
1023	if (info->xmit_buf) {
1024		free_page((unsigned long) info->xmit_buf);
1025		info->xmit_buf = NULL;
1026	}
1027
1028	info->IER = 0;
1029	serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1030	serial_out(info, UART_ESI_CMD2, 0x00);
1031
1032	if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1033		info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1034
1035	info->MCR &= ~UART_MCR_OUT2;
1036	serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1037	serial_out(info, UART_ESI_CMD2, UART_MCR);
1038	serial_out(info, UART_ESI_CMD2, info->MCR);
1039
1040	if (info->tty)
1041		set_bit(TTY_IO_ERROR, &info->tty->flags);
1042
1043	info->flags &= ~ASYNC_INITIALIZED;
1044	spin_unlock_irqrestore(&info->lock, flags);
1045}
1046
1047/*
1048 * This routine is called to set the UART divisor registers to match
1049 * the specified baud rate for a serial port.
1050 */
1051static void change_speed(struct esp_struct *info)
1052{
1053	unsigned short port;
1054	int	quot = 0;
1055	unsigned cflag,cval;
1056	int	baud, bits;
1057	unsigned char flow1 = 0, flow2 = 0;
1058	unsigned long flags;
1059
1060	if (!info->tty || !info->tty->termios)
1061		return;
1062	cflag = info->tty->termios->c_cflag;
1063	port = info->port;
1064
1065	/* byte size and parity */
1066	switch (cflag & CSIZE) {
1067	      case CS5: cval = 0x00; bits = 7; break;
1068	      case CS6: cval = 0x01; bits = 8; break;
1069	      case CS7: cval = 0x02; bits = 9; break;
1070	      case CS8: cval = 0x03; bits = 10; break;
1071	      default:  cval = 0x00; bits = 7; break;
1072	}
1073	if (cflag & CSTOPB) {
1074		cval |= 0x04;
1075		bits++;
1076	}
1077	if (cflag & PARENB) {
1078		cval |= UART_LCR_PARITY;
1079		bits++;
1080	}
1081	if (!(cflag & PARODD))
1082		cval |= UART_LCR_EPAR;
1083#ifdef CMSPAR
1084	if (cflag & CMSPAR)
1085		cval |= UART_LCR_SPAR;
1086#endif
1087
1088	baud = tty_get_baud_rate(info->tty);
1089	if (baud == 38400 &&
1090	    ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1091		quot = info->custom_divisor;
1092	else {
1093		if (baud == 134)
1094			/* Special case since 134 is really 134.5 */
1095			quot = (2*BASE_BAUD / 269);
1096		else if (baud)
1097			quot = BASE_BAUD / baud;
1098	}
1099	/* If the quotient is ever zero, default to 9600 bps */
1100	if (!quot)
1101		quot = BASE_BAUD / 9600;
1102
1103	info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1104
1105	/* CTS flow control flag and modem status interrupts */
1106	/* info->IER &= ~UART_IER_MSI; */
1107	if (cflag & CRTSCTS) {
1108		info->flags |= ASYNC_CTS_FLOW;
1109		/* info->IER |= UART_IER_MSI; */
1110		flow1 = 0x04;
1111		flow2 = 0x10;
1112	} else
1113		info->flags &= ~ASYNC_CTS_FLOW;
1114	if (cflag & CLOCAL)
1115		info->flags &= ~ASYNC_CHECK_CD;
1116	else {
1117		info->flags |= ASYNC_CHECK_CD;
1118		/* info->IER |= UART_IER_MSI; */
1119	}
1120
1121	/*
1122	 * Set up parity check flag
1123	 */
1124#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1125
1126	info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1127	if (I_INPCK(info->tty))
1128		info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1129	if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1130		info->read_status_mask |= UART_LSR_BI;
1131
1132	info->ignore_status_mask = 0;
1133	if (I_IGNBRK(info->tty)) {
1134		info->ignore_status_mask |= UART_LSR_BI;
1135		info->read_status_mask |= UART_LSR_BI;
1136		/*
1137		 * If we're ignore parity and break indicators, ignore
1138		 * overruns too.  (For real raw support).
1139		 */
1140		if (I_IGNPAR(info->tty)) {
1141			info->ignore_status_mask |= UART_LSR_OE | \
1142				UART_LSR_PE | UART_LSR_FE;
1143			info->read_status_mask |= UART_LSR_OE | \
1144				UART_LSR_PE | UART_LSR_FE;
1145		}
1146	}
1147
1148	if (I_IXOFF(info->tty))
1149		flow1 |= 0x81;
1150
1151	spin_lock_irqsave(&info->lock, flags);
1152	/* set baud */
1153	serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1154	serial_out(info, UART_ESI_CMD2, quot >> 8);
1155	serial_out(info, UART_ESI_CMD2, quot & 0xff);
1156
1157	/* set data bits, parity, etc. */
1158	serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1159	serial_out(info, UART_ESI_CMD2, UART_LCR);
1160	serial_out(info, UART_ESI_CMD2, cval);
1161
1162	/* Enable flow control */
1163	serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1164	serial_out(info, UART_ESI_CMD2, flow1);
1165	serial_out(info, UART_ESI_CMD2, flow2);
1166
1167	/* set flow control characters (XON/XOFF only) */
1168	if (I_IXOFF(info->tty)) {
1169		serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1170		serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1171		serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1172		serial_out(info, UART_ESI_CMD2, 0x10);
1173		serial_out(info, UART_ESI_CMD2, 0x21);
1174		switch (cflag & CSIZE) {
1175			case CS5:
1176				serial_out(info, UART_ESI_CMD2, 0x1f);
1177				break;
1178			case CS6:
1179				serial_out(info, UART_ESI_CMD2, 0x3f);
1180				break;
1181			case CS7:
1182			case CS8:
1183				serial_out(info, UART_ESI_CMD2, 0x7f);
1184				break;
1185			default:
1186				serial_out(info, UART_ESI_CMD2, 0xff);
1187				break;
1188		}
1189	}
1190
1191	/* Set high/low water */
1192	serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1193	serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1194	serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1195	serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1196	serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1197
1198	spin_unlock_irqrestore(&info->lock, flags);
1199}
1200
1201static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1202{
1203	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1204	unsigned long flags;
1205
1206	if (serial_paranoia_check(info, tty->name, "rs_put_char"))
1207		return;
1208
1209	if (!info->xmit_buf)
1210		return;
1211
1212	spin_lock_irqsave(&info->lock, flags);
1213	if (info->xmit_cnt < ESP_XMIT_SIZE - 1) {
1214		info->xmit_buf[info->xmit_head++] = ch;
1215		info->xmit_head &= ESP_XMIT_SIZE-1;
1216		info->xmit_cnt++;
1217	}
1218	spin_unlock_irqrestore(&info->lock, flags);
1219}
1220
1221static void rs_flush_chars(struct tty_struct *tty)
1222{
1223	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1224	unsigned long flags;
1225
1226	if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1227		return;
1228
1229	spin_lock_irqsave(&info->lock, flags);
1230
1231	if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1232		goto out;
1233
1234	if (!(info->IER & UART_IER_THRI)) {
1235		info->IER |= UART_IER_THRI;
1236		serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1237		serial_out(info, UART_ESI_CMD2, info->IER);
1238	}
1239out:
1240	spin_unlock_irqrestore(&info->lock, flags);
1241}
1242
1243static int rs_write(struct tty_struct * tty,
1244		    const unsigned char *buf, int count)
1245{
1246	int	c, t, ret = 0;
1247	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1248	unsigned long flags;
1249
1250	if (serial_paranoia_check(info, tty->name, "rs_write"))
1251		return 0;
1252
1253	if (!info->xmit_buf)
1254		return 0;
1255
1256	while (1) {
1257		/* Thanks to R. Wolff for suggesting how to do this with */
1258		/* interrupts enabled */
1259
1260		c = count;
1261		t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1262
1263		if (t < c)
1264			c = t;
1265
1266		t = ESP_XMIT_SIZE - info->xmit_head;
1267
1268		if (t < c)
1269			c = t;
1270
1271		if (c <= 0)
1272			break;
1273
1274		memcpy(info->xmit_buf + info->xmit_head, buf, c);
1275
1276		info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1277		info->xmit_cnt += c;
1278		buf += c;
1279		count -= c;
1280		ret += c;
1281	}
1282
1283	spin_lock_irqsave(&info->lock, flags);
1284
1285	if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1286		info->IER |= UART_IER_THRI;
1287		serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1288		serial_out(info, UART_ESI_CMD2, info->IER);
1289	}
1290
1291	spin_unlock_irqrestore(&info->lock, flags);
1292	return ret;
1293}
1294
1295static int rs_write_room(struct tty_struct *tty)
1296{
1297	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1298	int	ret;
1299	unsigned long flags;
1300
1301	if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1302		return 0;
1303
1304	spin_lock_irqsave(&info->lock, flags);
1305
1306	ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1307	if (ret < 0)
1308		ret = 0;
1309	spin_unlock_irqrestore(&info->lock, flags);
1310	return ret;
1311}
1312
1313static int rs_chars_in_buffer(struct tty_struct *tty)
1314{
1315	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1316
1317	if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1318		return 0;
1319	return info->xmit_cnt;
1320}
1321
1322static void rs_flush_buffer(struct tty_struct *tty)
1323{
1324	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1325	unsigned long flags;
1326
1327	if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1328		return;
1329	spin_lock_irqsave(&info->lock, flags);
1330	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1331	spin_unlock_irqrestore(&info->lock, flags);
1332	tty_wakeup(tty);
1333}
1334
1335/*
1336 * ------------------------------------------------------------
1337 * rs_throttle()
1338 *
1339 * This routine is called by the upper-layer tty layer to signal that
1340 * incoming characters should be throttled.
1341 * ------------------------------------------------------------
1342 */
1343static void rs_throttle(struct tty_struct * tty)
1344{
1345	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1346	unsigned long flags;
1347#ifdef SERIAL_DEBUG_THROTTLE
1348	char	buf[64];
1349
1350	printk("throttle %s: %d....\n", tty_name(tty, buf),
1351	       tty->ldisc.chars_in_buffer(tty));
1352#endif
1353
1354	if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1355		return;
1356
1357	spin_lock_irqsave(&info->lock, flags);
1358	info->IER &= ~UART_IER_RDI;
1359	serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1360	serial_out(info, UART_ESI_CMD2, info->IER);
1361	serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1362	serial_out(info, UART_ESI_CMD2, 0x00);
1363	spin_unlock_irqrestore(&info->lock, flags);
1364}
1365
1366static void rs_unthrottle(struct tty_struct * tty)
1367{
1368	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1369	unsigned long flags;
1370#ifdef SERIAL_DEBUG_THROTTLE
1371	char	buf[64];
1372
1373	printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1374	       tty->ldisc.chars_in_buffer(tty));
1375#endif
1376
1377	if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1378		return;
1379
1380	spin_lock_irqsave(&info->lock, flags);
1381	info->IER |= UART_IER_RDI;
1382	serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1383	serial_out(info, UART_ESI_CMD2, info->IER);
1384	serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1385	serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1386	spin_unlock_irqrestore(&info->lock, flags);
1387}
1388
1389/*
1390 * ------------------------------------------------------------
1391 * rs_ioctl() and friends
1392 * ------------------------------------------------------------
1393 */
1394
1395static int get_serial_info(struct esp_struct * info,
1396			   struct serial_struct __user *retinfo)
1397{
1398	struct serial_struct tmp;
1399
1400	memset(&tmp, 0, sizeof(tmp));
1401	tmp.type = PORT_16550A;
1402	tmp.line = info->line;
1403	tmp.port = info->port;
1404	tmp.irq = info->irq;
1405	tmp.flags = info->flags;
1406	tmp.xmit_fifo_size = 1024;
1407	tmp.baud_base = BASE_BAUD;
1408	tmp.close_delay = info->close_delay;
1409	tmp.closing_wait = info->closing_wait;
1410	tmp.custom_divisor = info->custom_divisor;
1411	tmp.hub6 = 0;
1412	if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1413		return -EFAULT;
1414	return 0;
1415}
1416
1417static int get_esp_config(struct esp_struct * info,
1418			  struct hayes_esp_config __user *retinfo)
1419{
1420	struct hayes_esp_config tmp;
1421
1422	if (!retinfo)
1423		return -EFAULT;
1424
1425	memset(&tmp, 0, sizeof(tmp));
1426	tmp.rx_timeout = info->config.rx_timeout;
1427	tmp.rx_trigger = info->config.rx_trigger;
1428	tmp.tx_trigger = info->config.tx_trigger;
1429	tmp.flow_off = info->config.flow_off;
1430	tmp.flow_on = info->config.flow_on;
1431	tmp.pio_threshold = info->config.pio_threshold;
1432	tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1433
1434	return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1435}
1436
1437static int set_serial_info(struct esp_struct * info,
1438			   struct serial_struct __user *new_info)
1439{
1440	struct serial_struct new_serial;
1441	struct esp_struct old_info;
1442	unsigned int change_irq;
1443	int retval = 0;
1444	struct esp_struct *current_async;
1445
1446	if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1447		return -EFAULT;
1448	old_info = *info;
1449
1450	if ((new_serial.type != PORT_16550A) ||
1451	    (new_serial.hub6) ||
1452	    (info->port != new_serial.port) ||
1453	    (new_serial.baud_base != BASE_BAUD) ||
1454	    (new_serial.irq > 15) ||
1455	    (new_serial.irq < 2) ||
1456	    (new_serial.irq == 6) ||
1457	    (new_serial.irq == 8) ||
1458	    (new_serial.irq == 13))
1459		return -EINVAL;
1460
1461	change_irq = new_serial.irq != info->irq;
1462
1463	if (change_irq && (info->line % 8))
1464		return -EINVAL;
1465
1466	if (!capable(CAP_SYS_ADMIN)) {
1467		if (change_irq ||
1468		    (new_serial.close_delay != info->close_delay) ||
1469		    ((new_serial.flags & ~ASYNC_USR_MASK) !=
1470		     (info->flags & ~ASYNC_USR_MASK)))
1471			return -EPERM;
1472		info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1473			       (new_serial.flags & ASYNC_USR_MASK));
1474		info->custom_divisor = new_serial.custom_divisor;
1475	} else {
1476		if (new_serial.irq == 2)
1477			new_serial.irq = 9;
1478
1479		if (change_irq) {
1480			current_async = ports;
1481
1482			while (current_async) {
1483				if ((current_async->line >= info->line) &&
1484				    (current_async->line < (info->line + 8))) {
1485					if (current_async == info) {
1486						if (current_async->count > 1)
1487							return -EBUSY;
1488					} else if (current_async->count)
1489						return -EBUSY;
1490				}
1491
1492				current_async = current_async->next_port;
1493			}
1494		}
1495
1496		/*
1497		 * OK, past this point, all the error checking has been done.
1498		 * At this point, we start making changes.....
1499		 */
1500
1501		info->flags = ((info->flags & ~ASYNC_FLAGS) |
1502			       (new_serial.flags & ASYNC_FLAGS));
1503		info->custom_divisor = new_serial.custom_divisor;
1504		info->close_delay = new_serial.close_delay * HZ/100;
1505		info->closing_wait = new_serial.closing_wait * HZ/100;
1506
1507		if (change_irq) {
1508			/*
1509			 * We need to shutdown the serial port at the old
1510			 * port/irq combination.
1511			 */
1512			shutdown(info);
1513
1514			current_async = ports;
1515
1516			while (current_async) {
1517				if ((current_async->line >= info->line) &&
1518				    (current_async->line < (info->line + 8)))
1519					current_async->irq = new_serial.irq;
1520
1521				current_async = current_async->next_port;
1522			}
1523
1524			serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1525			if (info->irq == 9)
1526				serial_out(info, UART_ESI_CMD2, 0x02);
1527			else
1528				serial_out(info, UART_ESI_CMD2, info->irq);
1529		}
1530	}
1531
1532	if (info->flags & ASYNC_INITIALIZED) {
1533		if (((old_info.flags & ASYNC_SPD_MASK) !=
1534		     (info->flags & ASYNC_SPD_MASK)) ||
1535		    (old_info.custom_divisor != info->custom_divisor)) {
1536			if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1537				info->tty->alt_speed = 57600;
1538			if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1539				info->tty->alt_speed = 115200;
1540			if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1541				info->tty->alt_speed = 230400;
1542			if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1543				info->tty->alt_speed = 460800;
1544			change_speed(info);
1545		}
1546	} else
1547		retval = startup(info);
1548
1549	return retval;
1550}
1551
1552static int set_esp_config(struct esp_struct * info,
1553			  struct hayes_esp_config __user * new_info)
1554{
1555	struct hayes_esp_config new_config;
1556	unsigned int change_dma;
1557	int retval = 0;
1558	struct esp_struct *current_async;
1559	unsigned long flags;
1560
1561	/* Perhaps a non-sysadmin user should be able to do some of these */
1562	/* operations.  I haven't decided yet. */
1563
1564	if (!capable(CAP_SYS_ADMIN))
1565		return -EPERM;
1566
1567	if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1568		return -EFAULT;
1569
1570	if ((new_config.flow_on >= new_config.flow_off) ||
1571	    (new_config.rx_trigger < 1) ||
1572	    (new_config.tx_trigger < 1) ||
1573	    (new_config.flow_off < 1) ||
1574	    (new_config.flow_on < 1) ||
1575	    (new_config.rx_trigger > 1023) ||
1576	    (new_config.tx_trigger > 1023) ||
1577	    (new_config.flow_off > 1023) ||
1578	    (new_config.flow_on > 1023) ||
1579	    (new_config.pio_threshold < 0) ||
1580	    (new_config.pio_threshold > 1024))
1581		return -EINVAL;
1582
1583	if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1584		new_config.dma_channel = 0;
1585
1586	if (info->stat_flags & ESP_STAT_NEVER_DMA)
1587		change_dma = new_config.dma_channel;
1588	else
1589		change_dma = (new_config.dma_channel != dma);
1590
1591	if (change_dma) {
1592		if (new_config.dma_channel) {
1593			/* PIO mode to DMA mode transition OR */
1594			/* change current DMA channel */
1595
1596			current_async = ports;
1597
1598			while (current_async) {
1599				if (current_async == info) {
1600					if (current_async->count > 1)
1601						return -EBUSY;
1602				} else if (current_async->count)
1603					return -EBUSY;
1604
1605				current_async =
1606					current_async->next_port;
1607			}
1608
1609			shutdown(info);
1610			dma = new_config.dma_channel;
1611			info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1612
1613                        /* all ports must use the same DMA channel */
1614
1615			spin_lock_irqsave(&info->lock, flags);
1616			current_async = ports;
1617
1618			while (current_async) {
1619				esp_basic_init(current_async);
1620				current_async = current_async->next_port;
1621			}
1622			spin_unlock_irqrestore(&info->lock, flags);
1623		} else {
1624			/* DMA mode to PIO mode only */
1625
1626			if (info->count > 1)
1627				return -EBUSY;
1628
1629			shutdown(info);
1630			spin_lock_irqsave(&info->lock, flags);
1631			info->stat_flags |= ESP_STAT_NEVER_DMA;
1632			esp_basic_init(info);
1633			spin_unlock_irqrestore(&info->lock, flags);
1634		}
1635	}
1636
1637	info->config.pio_threshold = new_config.pio_threshold;
1638
1639	if ((new_config.flow_off != info->config.flow_off) ||
1640	    (new_config.flow_on != info->config.flow_on)) {
1641		unsigned long flags;
1642
1643		info->config.flow_off = new_config.flow_off;
1644		info->config.flow_on = new_config.flow_on;
1645
1646		spin_lock_irqsave(&info->lock, flags);
1647		serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1648		serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1649		serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1650		serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1651		serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1652		spin_unlock_irqrestore(&info->lock, flags);
1653	}
1654
1655	if ((new_config.rx_trigger != info->config.rx_trigger) ||
1656	    (new_config.tx_trigger != info->config.tx_trigger)) {
1657		unsigned long flags;
1658
1659		info->config.rx_trigger = new_config.rx_trigger;
1660		info->config.tx_trigger = new_config.tx_trigger;
1661		spin_lock_irqsave(&info->lock, flags);
1662		serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1663		serial_out(info, UART_ESI_CMD2,
1664			   new_config.rx_trigger >> 8);
1665		serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1666		serial_out(info, UART_ESI_CMD2,
1667			   new_config.tx_trigger >> 8);
1668		serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1669		spin_unlock_irqrestore(&info->lock, flags);
1670	}
1671
1672	if (new_config.rx_timeout != info->config.rx_timeout) {
1673		unsigned long flags;
1674
1675		info->config.rx_timeout = new_config.rx_timeout;
1676		spin_lock_irqsave(&info->lock, flags);
1677
1678		if (info->IER & UART_IER_RDI) {
1679			serial_out(info, UART_ESI_CMD1,
1680				   ESI_SET_RX_TIMEOUT);
1681			serial_out(info, UART_ESI_CMD2,
1682				   new_config.rx_timeout);
1683		}
1684
1685		spin_unlock_irqrestore(&info->lock, flags);
1686	}
1687
1688	if (!(info->flags & ASYNC_INITIALIZED))
1689		retval = startup(info);
1690
1691	return retval;
1692}
1693
1694/*
1695 * get_lsr_info - get line status register info
1696 *
1697 * Purpose: Let user call ioctl() to get info when the UART physically
1698 * 	    is emptied.  On bus types like RS485, the transmitter must
1699 * 	    release the bus after transmitting. This must be done when
1700 * 	    the transmit shift register is empty, not be done when the
1701 * 	    transmit holding register is empty.  This functionality
1702 * 	    allows an RS485 driver to be written in user space.
1703 */
1704static int get_lsr_info(struct esp_struct * info, unsigned int __user *value)
1705{
1706	unsigned char status;
1707	unsigned int result;
1708	unsigned long flags;
1709
1710	spin_lock_irqsave(&info->lock, flags);
1711	serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1712	status = serial_in(info, UART_ESI_STAT1);
1713	spin_unlock_irqrestore(&info->lock, flags);
1714	result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1715	return put_user(result,value);
1716}
1717
1718
1719static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1720{
1721	struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1722	unsigned char control, status;
1723	unsigned long flags;
1724
1725	if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1726		return -ENODEV;
1727	if (tty->flags & (1 << TTY_IO_ERROR))
1728		return -EIO;
1729
1730	control = info->MCR;
1731
1732	spin_lock_irqsave(&info->lock, flags);
1733	serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1734	status = serial_in(info, UART_ESI_STAT2);
1735	spin_unlock_irqrestore(&info->lock, flags);
1736
1737	return    ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1738		| ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1739		| ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1740		| ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1741		| ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1742		| ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1743}
1744
1745static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1746			unsigned int set, unsigned int clear)
1747{
1748	struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1749	unsigned long flags;
1750
1751	if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1752		return -ENODEV;
1753	if (tty->flags & (1 << TTY_IO_ERROR))
1754		return -EIO;
1755
1756	spin_lock_irqsave(&info->lock, flags);
1757
1758	if (set & TIOCM_RTS)
1759		info->MCR |= UART_MCR_RTS;
1760	if (set & TIOCM_DTR)
1761		info->MCR |= UART_MCR_DTR;
1762
1763	if (clear & TIOCM_RTS)
1764		info->MCR &= ~UART_MCR_RTS;
1765	if (clear & TIOCM_DTR)
1766		info->MCR &= ~UART_MCR_DTR;
1767
1768	serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1769	serial_out(info, UART_ESI_CMD2, UART_MCR);
1770	serial_out(info, UART_ESI_CMD2, info->MCR);
1771
1772	spin_unlock_irqrestore(&info->lock, flags);
1773	return 0;
1774}
1775
1776/*
1777 * rs_break() --- routine which turns the break handling on or off
1778 */
1779static void esp_break(struct tty_struct *tty, int break_state)
1780{
1781	struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1782	unsigned long flags;
1783
1784	if (serial_paranoia_check(info, tty->name, "esp_break"))
1785		return;
1786
1787	if (break_state == -1) {
1788		spin_lock_irqsave(&info->lock, flags);
1789		serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1790		serial_out(info, UART_ESI_CMD2, 0x01);
1791		spin_unlock_irqrestore(&info->lock, flags);
1792
1793		interruptible_sleep_on(&info->break_wait);
1794	} else {
1795		spin_lock_irqsave(&info->lock, flags);
1796		serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1797		serial_out(info, UART_ESI_CMD2, 0x00);
1798		spin_unlock_irqrestore(&info->lock, flags);
1799	}
1800}
1801
1802static int rs_ioctl(struct tty_struct *tty, struct file * file,
1803		    unsigned int cmd, unsigned long arg)
1804{
1805	struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1806	struct async_icount cprev, cnow;	/* kernel counter temps */
1807	struct serial_icounter_struct __user *p_cuser;	/* user space */
1808	void __user *argp = (void __user *)arg;
1809	unsigned long flags;
1810
1811	if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1812		return -ENODEV;
1813
1814	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1815	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1816	    (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1817	    (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1818	    (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1819		if (tty->flags & (1 << TTY_IO_ERROR))
1820		    return -EIO;
1821	}
1822
1823	switch (cmd) {
1824		case TIOCGSERIAL:
1825			return get_serial_info(info, argp);
1826		case TIOCSSERIAL:
1827			return set_serial_info(info, argp);
1828		case TIOCSERCONFIG:
1829			/* do not reconfigure after initial configuration */
1830			return 0;
1831
1832		case TIOCSERGWILD:
1833			return put_user(0L, (unsigned long __user *)argp);
1834
1835		case TIOCSERGETLSR: /* Get line status register */
1836			    return get_lsr_info(info, argp);
1837
1838		case TIOCSERSWILD:
1839			if (!capable(CAP_SYS_ADMIN))
1840				return -EPERM;
1841			return 0;
1842
1843		/*
1844		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1845		 * - mask passed in arg for lines of interest
1846 		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1847		 * Caller should use TIOCGICOUNT to see which one it was
1848		 */
1849		 case TIOCMIWAIT:
1850			spin_lock_irqsave(&info->lock, flags);
1851			cprev = info->icount;	/* note the counters on entry */
1852			spin_unlock_irqrestore(&info->lock, flags);
1853			while (1) {
1854				interruptible_sleep_on(&info->delta_msr_wait);
1855				/* see if a signal did it */
1856				if (signal_pending(current))
1857					return -ERESTARTSYS;
1858				spin_lock_irqsave(&info->lock, flags);
1859				cnow = info->icount;	/* atomic copy */
1860				spin_unlock_irqrestore(&info->lock, flags);
1861				if (cnow.rng == cprev.rng &&
1862				    cnow.dsr == cprev.dsr &&
1863				    cnow.dcd == cprev.dcd &&
1864				    cnow.cts == cprev.cts)
1865					return -EIO; /* no change => error */
1866				if (((arg & TIOCM_RNG) &&
1867				     (cnow.rng != cprev.rng)) ||
1868				     ((arg & TIOCM_DSR) &&
1869				      (cnow.dsr != cprev.dsr)) ||
1870				     ((arg & TIOCM_CD) &&
1871				      (cnow.dcd != cprev.dcd)) ||
1872				     ((arg & TIOCM_CTS) &&
1873				      (cnow.cts != cprev.cts)) ) {
1874					return 0;
1875				}
1876				cprev = cnow;
1877			}
1878			/* NOTREACHED */
1879
1880		/*
1881		 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1882		 * Return: write counters to the user passed counter struct
1883		 * NB: both 1->0 and 0->1 transitions are counted except for
1884		 *     RI where only 0->1 is counted.
1885		 */
1886		case TIOCGICOUNT:
1887			spin_lock_irqsave(&info->lock, flags);
1888			cnow = info->icount;
1889			spin_unlock_irqrestore(&info->lock, flags);
1890			p_cuser = argp;
1891			if (put_user(cnow.cts, &p_cuser->cts) ||
1892			    put_user(cnow.dsr, &p_cuser->dsr) ||
1893			    put_user(cnow.rng, &p_cuser->rng) ||
1894			    put_user(cnow.dcd, &p_cuser->dcd))
1895				return -EFAULT;
1896
1897			return 0;
1898	case TIOCGHAYESESP:
1899		return get_esp_config(info, argp);
1900	case TIOCSHAYESESP:
1901		return set_esp_config(info, argp);
1902
1903		default:
1904			return -ENOIOCTLCMD;
1905		}
1906	return 0;
1907}
1908
1909static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1910{
1911	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1912	unsigned long flags;
1913
1914	if (   (tty->termios->c_cflag == old_termios->c_cflag)
1915	    && (   RELEVANT_IFLAG(tty->termios->c_iflag)
1916		== RELEVANT_IFLAG(old_termios->c_iflag)))
1917		return;
1918
1919	change_speed(info);
1920
1921	spin_lock_irqsave(&info->lock, flags);
1922
1923	/* Handle transition to B0 status */
1924	if ((old_termios->c_cflag & CBAUD) &&
1925		!(tty->termios->c_cflag & CBAUD)) {
1926		info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1927		serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1928		serial_out(info, UART_ESI_CMD2, UART_MCR);
1929		serial_out(info, UART_ESI_CMD2, info->MCR);
1930	}
1931
1932	/* Handle transition away from B0 status */
1933	if (!(old_termios->c_cflag & CBAUD) &&
1934		(tty->termios->c_cflag & CBAUD)) {
1935		info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1936		serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1937		serial_out(info, UART_ESI_CMD2, UART_MCR);
1938		serial_out(info, UART_ESI_CMD2, info->MCR);
1939	}
1940
1941	spin_unlock_irqrestore(&info->lock, flags);
1942
1943	/* Handle turning of CRTSCTS */
1944	if ((old_termios->c_cflag & CRTSCTS) &&
1945	    !(tty->termios->c_cflag & CRTSCTS)) {
1946		rs_start(tty);
1947	}
1948}
1949
1950/*
1951 * ------------------------------------------------------------
1952 * rs_close()
1953 *
1954 * This routine is called when the serial port gets closed.  First, we
1955 * wait for the last remaining data to be sent.  Then, we unlink its
1956 * async structure from the interrupt chain if necessary, and we free
1957 * that IRQ if nothing is left in the chain.
1958 * ------------------------------------------------------------
1959 */
1960static void rs_close(struct tty_struct *tty, struct file * filp)
1961{
1962	struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1963	unsigned long flags;
1964
1965	if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1966		return;
1967
1968	spin_lock_irqsave(&info->lock, flags);
1969
1970	if (tty_hung_up_p(filp)) {
1971		DBG_CNT("before DEC-hung");
1972		goto out;
1973	}
1974
1975#ifdef SERIAL_DEBUG_OPEN
1976	printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1977#endif
1978	if ((tty->count == 1) && (info->count != 1)) {
1979		/*
1980		 * Uh, oh.  tty->count is 1, which means that the tty
1981		 * structure will be freed.  Info->count should always
1982		 * be one in these conditions.  If it's greater than
1983		 * one, we've got real problems, since it means the
1984		 * serial port won't be shutdown.
1985		 */
1986		printk("rs_close: bad serial port count; tty->count is 1, "
1987		       "info->count is %d\n", info->count);
1988		info->count = 1;
1989	}
1990	if (--info->count < 0) {
1991		printk("rs_close: bad serial port count for ttys%d: %d\n",
1992		       info->line, info->count);
1993		info->count = 0;
1994	}
1995	if (info->count) {
1996		DBG_CNT("before DEC-2");
1997		goto out;
1998	}
1999	info->flags |= ASYNC_CLOSING;
2000
2001	spin_unlock_irqrestore(&info->lock, flags);
2002	/*
2003	 * Now we wait for the transmit buffer to clear; and we notify
2004	 * the line discipline to only process XON/XOFF characters.
2005	 */
2006	tty->closing = 1;
2007	if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2008		tty_wait_until_sent(tty, info->closing_wait);
2009	/*
2010	 * At this point we stop accepting input.  To do this, we
2011	 * disable the receive line status interrupts, and tell the
2012	 * interrupt driver to stop checking the data ready bit in the
2013	 * line status register.
2014	 */
2015	/* info->IER &= ~UART_IER_RLSI; */
2016	info->IER &= ~UART_IER_RDI;
2017	info->read_status_mask &= ~UART_LSR_DR;
2018	if (info->flags & ASYNC_INITIALIZED) {
2019
2020		spin_lock_irqsave(&info->lock, flags);
2021		serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2022		serial_out(info, UART_ESI_CMD2, info->IER);
2023
2024		/* disable receive timeout */
2025		serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2026		serial_out(info, UART_ESI_CMD2, 0x00);
2027
2028		spin_unlock_irqrestore(&info->lock, flags);
2029
2030		/*
2031		 * Before we drop DTR, make sure the UART transmitter
2032		 * has completely drained; this is especially
2033		 * important if there is a transmit FIFO!
2034		 */
2035		rs_wait_until_sent(tty, info->timeout);
2036	}
2037	shutdown(info);
2038	if (tty->driver->flush_buffer)
2039		tty->driver->flush_buffer(tty);
2040	tty_ldisc_flush(tty);
2041	tty->closing = 0;
2042	info->event = 0;
2043	info->tty = NULL;
2044
2045	if (info->blocked_open) {
2046		if (info->close_delay) {
2047			msleep_interruptible(jiffies_to_msecs(info->close_delay));
2048		}
2049		wake_up_interruptible(&info->open_wait);
2050	}
2051	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2052	wake_up_interruptible(&info->close_wait);
2053	return;
2054
2055out:
2056	spin_unlock_irqrestore(&info->lock, flags);
2057}
2058
2059static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2060{
2061	struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2062	unsigned long orig_jiffies, char_time;
2063	unsigned long flags;
2064
2065	if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2066		return;
2067
2068	orig_jiffies = jiffies;
2069	char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2070
2071	if (!char_time)
2072		char_time = 1;
2073
2074	spin_lock_irqsave(&info->lock, flags);
2075	serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2076	serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2077
2078	while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2079		(serial_in(info, UART_ESI_STAT2) != 0xff)) {
2080
2081		spin_unlock_irqrestore(&info->lock, flags);
2082		msleep_interruptible(jiffies_to_msecs(char_time));
2083
2084		if (signal_pending(current))
2085			break;
2086
2087		if (timeout && time_after(jiffies, orig_jiffies + timeout))
2088			break;
2089
2090		spin_lock_irqsave(&info->lock, flags);
2091		serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2092		serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2093	}
2094	spin_unlock_irqrestore(&info->lock, flags);
2095	set_current_state(TASK_RUNNING);
2096}
2097
2098/*
2099 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2100 */
2101static void esp_hangup(struct tty_struct *tty)
2102{
2103	struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2104
2105	if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2106		return;
2107
2108	rs_flush_buffer(tty);
2109	shutdown(info);
2110	info->event = 0;
2111	info->count = 0;
2112	info->flags &= ~ASYNC_NORMAL_ACTIVE;
2113	info->tty = NULL;
2114	wake_up_interruptible(&info->open_wait);
2115}
2116
2117/*
2118 * ------------------------------------------------------------
2119 * esp_open() and friends
2120 * ------------------------------------------------------------
2121 */
2122static int block_til_ready(struct tty_struct *tty, struct file * filp,
2123			   struct esp_struct *info)
2124{
2125	DECLARE_WAITQUEUE(wait, current);
2126	int		retval;
2127	int		do_clocal = 0;
2128	unsigned long	flags;
2129
2130	/*
2131	 * If the device is in the middle of being closed, then block
2132	 * until it's done, and then try again.
2133	 */
2134	if (tty_hung_up_p(filp) ||
2135	    (info->flags & ASYNC_CLOSING)) {
2136		if (info->flags & ASYNC_CLOSING)
2137			interruptible_sleep_on(&info->close_wait);
2138#ifdef SERIAL_DO_RESTART
2139		if (info->flags & ASYNC_HUP_NOTIFY)
2140			return -EAGAIN;
2141		else
2142			return -ERESTARTSYS;
2143#else
2144		return -EAGAIN;
2145#endif
2146	}
2147
2148	/*
2149	 * If non-blocking mode is set, or the port is not enabled,
2150	 * then make the check up front and then exit.
2151	 */
2152	if ((filp->f_flags & O_NONBLOCK) ||
2153	    (tty->flags & (1 << TTY_IO_ERROR))) {
2154		info->flags |= ASYNC_NORMAL_ACTIVE;
2155		return 0;
2156	}
2157
2158	if (tty->termios->c_cflag & CLOCAL)
2159		do_clocal = 1;
2160
2161	/*
2162	 * Block waiting for the carrier detect and the line to become
2163	 * free (i.e., not in use by the callout).  While we are in
2164	 * this loop, info->count is dropped by one, so that
2165	 * rs_close() knows when to free things.  We restore it upon
2166	 * exit, either normal or abnormal.
2167	 */
2168	retval = 0;
2169	add_wait_queue(&info->open_wait, &wait);
2170#ifdef SERIAL_DEBUG_OPEN
2171	printk("block_til_ready before block: ttys%d, count = %d\n",
2172	       info->line, info->count);
2173#endif
2174	spin_lock_irqsave(&info->lock, flags);
2175	if (!tty_hung_up_p(filp))
2176		info->count--;
2177	info->blocked_open++;
2178	while (1) {
2179		if ((tty->termios->c_cflag & CBAUD)) {
2180			unsigned int scratch;
2181
2182			serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2183			serial_out(info, UART_ESI_CMD2, UART_MCR);
2184			scratch = serial_in(info, UART_ESI_STAT1);
2185			serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2186			serial_out(info, UART_ESI_CMD2, UART_MCR);
2187			serial_out(info, UART_ESI_CMD2,
2188				scratch | UART_MCR_DTR | UART_MCR_RTS);
2189		}
2190		set_current_state(TASK_INTERRUPTIBLE);
2191		if (tty_hung_up_p(filp) ||
2192		    !(info->flags & ASYNC_INITIALIZED)) {
2193#ifdef SERIAL_DO_RESTART
2194			if (info->flags & ASYNC_HUP_NOTIFY)
2195				retval = -EAGAIN;
2196			else
2197				retval = -ERESTARTSYS;
2198#else
2199			retval = -EAGAIN;
2200#endif
2201			break;
2202		}
2203
2204		serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2205		if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2206			do_clocal = 1;
2207
2208		if (!(info->flags & ASYNC_CLOSING) &&
2209		    (do_clocal))
2210			break;
2211		if (signal_pending(current)) {
2212			retval = -ERESTARTSYS;
2213			break;
2214		}
2215#ifdef SERIAL_DEBUG_OPEN
2216		printk("block_til_ready blocking: ttys%d, count = %d\n",
2217		       info->line, info->count);
2218#endif
2219		spin_unlock_irqrestore(&info->lock, flags);
2220		schedule();
2221		spin_lock_irqsave(&info->lock, flags);
2222	}
2223	set_current_state(TASK_RUNNING);
2224	remove_wait_queue(&info->open_wait, &wait);
2225	if (!tty_hung_up_p(filp))
2226		info->count++;
2227	info->blocked_open--;
2228	spin_unlock_irqrestore(&info->lock, flags);
2229#ifdef SERIAL_DEBUG_OPEN
2230	printk("block_til_ready after blocking: ttys%d, count = %d\n",
2231	       info->line, info->count);
2232#endif
2233	if (retval)
2234		return retval;
2235	info->flags |= ASYNC_NORMAL_ACTIVE;
2236	return 0;
2237}
2238
2239/*
2240 * This routine is called whenever a serial port is opened.  It
2241 * enables interrupts for a serial port, linking in its async structure into
2242 * the IRQ chain.   It also performs the serial-specific
2243 * initialization for the tty structure.
2244 */
2245static int esp_open(struct tty_struct *tty, struct file * filp)
2246{
2247	struct esp_struct	*info;
2248	int 			retval, line;
2249	unsigned long		flags;
2250
2251	line = tty->index;
2252	if ((line < 0) || (line >= NR_PORTS))
2253		return -ENODEV;
2254
2255	/* find the port in the chain */
2256
2257	info = ports;
2258
2259	while (info && (info->line != line))
2260		info = info->next_port;
2261
2262	if (!info) {
2263		serial_paranoia_check(info, tty->name, "esp_open");
2264		return -ENODEV;
2265	}
2266
2267#ifdef SERIAL_DEBUG_OPEN
2268	printk("esp_open %s, count = %d\n", tty->name, info->count);
2269#endif
2270	spin_lock_irqsave(&info->lock, flags);
2271	info->count++;
2272	tty->driver_data = info;
2273	info->tty = tty;
2274
2275	spin_unlock_irqrestore(&info->lock, flags);
2276
2277	/*
2278	 * Start up serial port
2279	 */
2280	retval = startup(info);
2281	if (retval)
2282		return retval;
2283
2284	retval = block_til_ready(tty, filp, info);
2285	if (retval) {
2286#ifdef SERIAL_DEBUG_OPEN
2287		printk("esp_open returning after block_til_ready with %d\n",
2288		       retval);
2289#endif
2290		return retval;
2291	}
2292
2293#ifdef SERIAL_DEBUG_OPEN
2294	printk("esp_open %s successful...", tty->name);
2295#endif
2296	return 0;
2297}
2298
2299/*
2300 * ---------------------------------------------------------------------
2301 * espserial_init() and friends
2302 *
2303 * espserial_init() is called at boot-time to initialize the serial driver.
2304 * ---------------------------------------------------------------------
2305 */
2306
2307/*
2308 * This routine prints out the appropriate serial driver version
2309 * number, and identifies which options were configured into this
2310 * driver.
2311 */
2312
2313static inline void show_serial_version(void)
2314{
2315 	printk(KERN_INFO "%s version %s (DMA %u)\n",
2316		serial_name, serial_version, dma);
2317}
2318
2319/*
2320 * This routine is called by espserial_init() to initialize a specific serial
2321 * port.
2322 */
2323static inline int autoconfig(struct esp_struct * info)
2324{
2325	int port_detected = 0;
2326	unsigned long flags;
2327
2328	if (!request_region(info->port, REGION_SIZE, "esp serial"))
2329		return -EIO;
2330
2331	spin_lock_irqsave(&info->lock, flags);
2332	/*
2333	 * Check for ESP card
2334	 */
2335
2336	if (serial_in(info, UART_ESI_BASE) == 0xf3) {
2337		serial_out(info, UART_ESI_CMD1, 0x00);
2338		serial_out(info, UART_ESI_CMD1, 0x01);
2339
2340		if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2341			port_detected = 1;
2342
2343			if (!(info->irq)) {
2344				serial_out(info, UART_ESI_CMD1, 0x02);
2345
2346				if (serial_in(info, UART_ESI_STAT1) & 0x01)
2347					info->irq = 3;
2348				else
2349					info->irq = 4;
2350			}
2351
2352
2353			/* put card in enhanced mode */
2354			/* this prevents access through */
2355			/* the "old" IO ports */
2356			esp_basic_init(info);
2357
2358			/* clear out MCR */
2359			serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2360			serial_out(info, UART_ESI_CMD2, UART_MCR);
2361			serial_out(info, UART_ESI_CMD2, 0x00);
2362		}
2363	}
2364	if (!port_detected)
2365		release_region(info->port, REGION_SIZE);
2366
2367	spin_unlock_irqrestore(&info->lock, flags);
2368	return (port_detected);
2369}
2370
2371static const struct tty_operations esp_ops = {
2372	.open = esp_open,
2373	.close = rs_close,
2374	.write = rs_write,
2375	.put_char = rs_put_char,
2376	.flush_chars = rs_flush_chars,
2377	.write_room = rs_write_room,
2378	.chars_in_buffer = rs_chars_in_buffer,
2379	.flush_buffer = rs_flush_buffer,
2380	.ioctl = rs_ioctl,
2381	.throttle = rs_throttle,
2382	.unthrottle = rs_unthrottle,
2383	.set_termios = rs_set_termios,
2384	.stop = rs_stop,
2385	.start = rs_start,
2386	.hangup = esp_hangup,
2387	.break_ctl = esp_break,
2388	.wait_until_sent = rs_wait_until_sent,
2389	.tiocmget = esp_tiocmget,
2390	.tiocmset = esp_tiocmset,
2391};
2392
2393/*
2394 * The serial driver boot-time initialization code!
2395 */
2396static int __init espserial_init(void)
2397{
2398	int i, offset;
2399	struct esp_struct * info;
2400	struct esp_struct *last_primary = NULL;
2401	int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2402
2403	esp_driver = alloc_tty_driver(NR_PORTS);
2404	if (!esp_driver)
2405		return -ENOMEM;
2406
2407	for (i = 0; i < NR_PRIMARY; i++) {
2408		if (irq[i] != 0) {
2409			if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2410			    (irq[i] == 8) || (irq[i] == 13))
2411				irq[i] = 0;
2412			else if (irq[i] == 2)
2413				irq[i] = 9;
2414		}
2415	}
2416
2417	if ((dma != 1) && (dma != 3))
2418		dma = 0;
2419
2420	if ((rx_trigger < 1) || (rx_trigger > 1023))
2421		rx_trigger = 768;
2422
2423	if ((tx_trigger < 1) || (tx_trigger > 1023))
2424		tx_trigger = 768;
2425
2426	if ((flow_off < 1) || (flow_off > 1023))
2427		flow_off = 1016;
2428
2429	if ((flow_on < 1) || (flow_on > 1023))
2430		flow_on = 944;
2431
2432	if ((rx_timeout < 0) || (rx_timeout > 255))
2433		rx_timeout = 128;
2434
2435	if (flow_on >= flow_off)
2436		flow_on = flow_off - 1;
2437
2438	show_serial_version();
2439
2440	/* Initialize the tty_driver structure */
2441
2442	esp_driver->owner = THIS_MODULE;
2443	esp_driver->name = "ttyP";
2444	esp_driver->major = ESP_IN_MAJOR;
2445	esp_driver->minor_start = 0;
2446	esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
2447	esp_driver->subtype = SERIAL_TYPE_NORMAL;
2448	esp_driver->init_termios = tty_std_termios;
2449	esp_driver->init_termios.c_cflag =
2450		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2451	esp_driver->flags = TTY_DRIVER_REAL_RAW;
2452	tty_set_operations(esp_driver, &esp_ops);
2453	if (tty_register_driver(esp_driver))
2454	{
2455		printk(KERN_ERR "Couldn't register esp serial driver");
2456		put_tty_driver(esp_driver);
2457		return 1;
2458	}
2459
2460	info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2461
2462	if (!info)
2463	{
2464		printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2465		tty_unregister_driver(esp_driver);
2466		put_tty_driver(esp_driver);
2467		return 1;
2468	}
2469
2470	memset((void *)info, 0, sizeof(struct esp_struct));
2471	spin_lock_init(&info->lock);
2472	/* rx_trigger, tx_trigger are needed by autoconfig */
2473	info->config.rx_trigger = rx_trigger;
2474	info->config.tx_trigger = tx_trigger;
2475
2476	i = 0;
2477	offset = 0;
2478
2479	do {
2480		info->port = esp[i] + offset;
2481		info->irq = irq[i];
2482		info->line = (i * 8) + (offset / 8);
2483
2484		if (!autoconfig(info)) {
2485			i++;
2486			offset = 0;
2487			continue;
2488		}
2489
2490		info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2491		info->flags = STD_COM_FLAGS;
2492		if (info->custom_divisor)
2493			info->flags |= ASYNC_SPD_CUST;
2494		info->magic = ESP_MAGIC;
2495		info->close_delay = 5*HZ/10;
2496		info->closing_wait = 30*HZ;
2497		INIT_WORK(&info->tqueue, do_softint);
2498		INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
2499		info->config.rx_timeout = rx_timeout;
2500		info->config.flow_on = flow_on;
2501		info->config.flow_off = flow_off;
2502		info->config.pio_threshold = pio_threshold;
2503		info->next_port = ports;
2504		init_waitqueue_head(&info->open_wait);
2505		init_waitqueue_head(&info->close_wait);
2506		init_waitqueue_head(&info->delta_msr_wait);
2507		init_waitqueue_head(&info->break_wait);
2508		ports = info;
2509		printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2510			info->line, info->port, info->irq);
2511
2512		if (info->line % 8) {
2513			printk("secondary port\n");
2514			/* 8 port cards can't do DMA */
2515			info->stat_flags |= ESP_STAT_NEVER_DMA;
2516
2517			if (last_primary)
2518				last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2519		} else {
2520			printk("primary port\n");
2521			last_primary = info;
2522			irq[i] = info->irq;
2523		}
2524
2525		if (!dma)
2526			info->stat_flags |= ESP_STAT_NEVER_DMA;
2527
2528		info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2529		if (!info)
2530		{
2531			printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2532
2533			/* allow use of the already detected ports */
2534			return 0;
2535		}
2536
2537		memset((void *)info, 0, sizeof(struct esp_struct));
2538		/* rx_trigger, tx_trigger are needed by autoconfig */
2539		info->config.rx_trigger = rx_trigger;
2540		info->config.tx_trigger = tx_trigger;
2541
2542		if (offset == 56) {
2543			i++;
2544			offset = 0;
2545		} else {
2546			offset += 8;
2547		}
2548	} while (i < NR_PRIMARY);
2549
2550	/* free the last port memory allocation */
2551	kfree(info);
2552
2553	return 0;
2554}
2555
2556static void __exit espserial_exit(void)
2557{
2558	int e1;
2559	struct esp_struct *temp_async;
2560	struct esp_pio_buffer *pio_buf;
2561
2562	/* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2563	if ((e1 = tty_unregister_driver(esp_driver)))
2564		printk("SERIAL: failed to unregister serial driver (%d)\n",
2565		       e1);
2566	put_tty_driver(esp_driver);
2567
2568	while (ports) {
2569		if (ports->port) {
2570			release_region(ports->port, REGION_SIZE);
2571		}
2572		temp_async = ports->next_port;
2573		kfree(ports);
2574		ports = temp_async;
2575	}
2576
2577	if (dma_buffer)
2578		free_pages((unsigned long)dma_buffer,
2579			get_order(DMA_BUFFER_SZ));
2580
2581	while (free_pio_buf) {
2582		pio_buf = free_pio_buf->next;
2583		kfree(free_pio_buf);
2584		free_pio_buf = pio_buf;
2585	}
2586}
2587
2588module_init(espserial_init);
2589module_exit(espserial_exit);
2590