xref: /OK3568_Linux_fs/kernel/drivers/tty/amiserial.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Serial driver for the amiga builtin port.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This code was created by taking serial.c version 4.30 from kernel
6*4882a593Smuzhiyun  * release 2.3.22, replacing all hardware related stuff with the
7*4882a593Smuzhiyun  * corresponding amiga hardware actions, and removing all irrelevant
8*4882a593Smuzhiyun  * code. As a consequence, it uses many of the constants and names
9*4882a593Smuzhiyun  * associated with the registers and bits of 16550 compatible UARTS -
10*4882a593Smuzhiyun  * but only to keep track of status, etc in the state variables. It
11*4882a593Smuzhiyun  * was done this was to make it easier to keep the code in line with
12*4882a593Smuzhiyun  * (non hardware specific) changes to serial.c.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The port is registered with the tty driver as minor device 64, and
15*4882a593Smuzhiyun  * therefore other ports should should only use 65 upwards.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * Richard Lucock 28/12/99
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *  Copyright (C) 1991, 1992  Linus Torvalds
20*4882a593Smuzhiyun  *  Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
21*4882a593Smuzhiyun  * 		1998, 1999  Theodore Ts'o
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <linux/delay.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /* Set of debugging defines */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #undef SERIAL_DEBUG_INTR
30*4882a593Smuzhiyun #undef SERIAL_DEBUG_OPEN
31*4882a593Smuzhiyun #undef SERIAL_DEBUG_FLOW
32*4882a593Smuzhiyun #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /* Sanity checks */
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
37*4882a593Smuzhiyun #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
38*4882a593Smuzhiyun  tty->name, (info->tport.flags), serial_driver->refcount,info->count,tty->count,s)
39*4882a593Smuzhiyun #else
40*4882a593Smuzhiyun #define DBG_CNT(s)
41*4882a593Smuzhiyun #endif
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /*
44*4882a593Smuzhiyun  * End of serial driver configuration section.
45*4882a593Smuzhiyun  */
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #include <linux/module.h>
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #include <linux/types.h>
50*4882a593Smuzhiyun #include <linux/serial.h>
51*4882a593Smuzhiyun #include <linux/serial_reg.h>
52*4882a593Smuzhiyun static char *serial_version = "4.30";
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #include <linux/errno.h>
55*4882a593Smuzhiyun #include <linux/signal.h>
56*4882a593Smuzhiyun #include <linux/sched.h>
57*4882a593Smuzhiyun #include <linux/kernel.h>
58*4882a593Smuzhiyun #include <linux/timer.h>
59*4882a593Smuzhiyun #include <linux/interrupt.h>
60*4882a593Smuzhiyun #include <linux/tty.h>
61*4882a593Smuzhiyun #include <linux/tty_flip.h>
62*4882a593Smuzhiyun #include <linux/circ_buf.h>
63*4882a593Smuzhiyun #include <linux/console.h>
64*4882a593Smuzhiyun #include <linux/major.h>
65*4882a593Smuzhiyun #include <linux/string.h>
66*4882a593Smuzhiyun #include <linux/fcntl.h>
67*4882a593Smuzhiyun #include <linux/ptrace.h>
68*4882a593Smuzhiyun #include <linux/ioport.h>
69*4882a593Smuzhiyun #include <linux/mm.h>
70*4882a593Smuzhiyun #include <linux/seq_file.h>
71*4882a593Smuzhiyun #include <linux/slab.h>
72*4882a593Smuzhiyun #include <linux/init.h>
73*4882a593Smuzhiyun #include <linux/bitops.h>
74*4882a593Smuzhiyun #include <linux/platform_device.h>
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #include <asm/setup.h>
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun #include <asm/irq.h>
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun #include <asm/amigahw.h>
82*4882a593Smuzhiyun #include <asm/amigaints.h>
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun struct serial_state {
85*4882a593Smuzhiyun 	struct tty_port		tport;
86*4882a593Smuzhiyun 	struct circ_buf		xmit;
87*4882a593Smuzhiyun 	struct async_icount	icount;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	unsigned long		port;
90*4882a593Smuzhiyun 	int			baud_base;
91*4882a593Smuzhiyun 	int			xmit_fifo_size;
92*4882a593Smuzhiyun 	int			custom_divisor;
93*4882a593Smuzhiyun 	int			read_status_mask;
94*4882a593Smuzhiyun 	int			ignore_status_mask;
95*4882a593Smuzhiyun 	int			timeout;
96*4882a593Smuzhiyun 	int			quot;
97*4882a593Smuzhiyun 	int			IER; 	/* Interrupt Enable Register */
98*4882a593Smuzhiyun 	int			MCR; 	/* Modem control register */
99*4882a593Smuzhiyun 	int			x_char;	/* xon/xoff character */
100*4882a593Smuzhiyun };
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun #define custom amiga_custom
103*4882a593Smuzhiyun static char *serial_name = "Amiga-builtin serial driver";
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun static struct tty_driver *serial_driver;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /* number of characters left in xmit buffer before we ask for more */
108*4882a593Smuzhiyun #define WAKEUP_CHARS 256
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun static unsigned char current_ctl_bits;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun static void change_speed(struct tty_struct *tty, struct serial_state *info,
113*4882a593Smuzhiyun 		struct ktermios *old);
114*4882a593Smuzhiyun static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun static struct serial_state rs_table[1];
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun #define NR_PORTS ARRAY_SIZE(rs_table)
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun #include <linux/uaccess.h>
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun #define serial_isroot()	(capable(CAP_SYS_ADMIN))
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /* some serial hardware definitions */
126*4882a593Smuzhiyun #define SDR_OVRUN   (1<<15)
127*4882a593Smuzhiyun #define SDR_RBF     (1<<14)
128*4882a593Smuzhiyun #define SDR_TBE     (1<<13)
129*4882a593Smuzhiyun #define SDR_TSRE    (1<<12)
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun #define SERPER_PARENB    (1<<15)
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun #define AC_SETCLR   (1<<15)
134*4882a593Smuzhiyun #define AC_UARTBRK  (1<<11)
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun #define SER_DTR     (1<<7)
137*4882a593Smuzhiyun #define SER_RTS     (1<<6)
138*4882a593Smuzhiyun #define SER_DCD     (1<<5)
139*4882a593Smuzhiyun #define SER_CTS     (1<<4)
140*4882a593Smuzhiyun #define SER_DSR     (1<<3)
141*4882a593Smuzhiyun 
rtsdtr_ctrl(int bits)142*4882a593Smuzhiyun static __inline__ void rtsdtr_ctrl(int bits)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun     ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /*
148*4882a593Smuzhiyun  * ------------------------------------------------------------
149*4882a593Smuzhiyun  * rs_stop() and rs_start()
150*4882a593Smuzhiyun  *
151*4882a593Smuzhiyun  * This routines are called before setting or resetting tty->stopped.
152*4882a593Smuzhiyun  * They enable or disable transmitter interrupts, as necessary.
153*4882a593Smuzhiyun  * ------------------------------------------------------------
154*4882a593Smuzhiyun  */
rs_stop(struct tty_struct * tty)155*4882a593Smuzhiyun static void rs_stop(struct tty_struct *tty)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
158*4882a593Smuzhiyun 	unsigned long flags;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	local_irq_save(flags);
161*4882a593Smuzhiyun 	if (info->IER & UART_IER_THRI) {
162*4882a593Smuzhiyun 		info->IER &= ~UART_IER_THRI;
163*4882a593Smuzhiyun 		/* disable Tx interrupt and remove any pending interrupts */
164*4882a593Smuzhiyun 		custom.intena = IF_TBE;
165*4882a593Smuzhiyun 		mb();
166*4882a593Smuzhiyun 		custom.intreq = IF_TBE;
167*4882a593Smuzhiyun 		mb();
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 	local_irq_restore(flags);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
rs_start(struct tty_struct * tty)172*4882a593Smuzhiyun static void rs_start(struct tty_struct *tty)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
175*4882a593Smuzhiyun 	unsigned long flags;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	local_irq_save(flags);
178*4882a593Smuzhiyun 	if (info->xmit.head != info->xmit.tail
179*4882a593Smuzhiyun 	    && info->xmit.buf
180*4882a593Smuzhiyun 	    && !(info->IER & UART_IER_THRI)) {
181*4882a593Smuzhiyun 		info->IER |= UART_IER_THRI;
182*4882a593Smuzhiyun 		custom.intena = IF_SETCLR | IF_TBE;
183*4882a593Smuzhiyun 		mb();
184*4882a593Smuzhiyun 		/* set a pending Tx Interrupt, transmitter should restart now */
185*4882a593Smuzhiyun 		custom.intreq = IF_SETCLR | IF_TBE;
186*4882a593Smuzhiyun 		mb();
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 	local_irq_restore(flags);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun  * ----------------------------------------------------------------------
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * Here starts the interrupt handling routines.  All of the following
195*4882a593Smuzhiyun  * subroutines are declared as inline and are folded into
196*4882a593Smuzhiyun  * rs_interrupt().  They were separated out for readability's sake.
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * Note: rs_interrupt() is a "fast" interrupt, which means that it
199*4882a593Smuzhiyun  * runs with interrupts turned off.  People who may want to modify
200*4882a593Smuzhiyun  * rs_interrupt() should try to keep the interrupt handler as fast as
201*4882a593Smuzhiyun  * possible.  After you are done making modifications, it is not a bad
202*4882a593Smuzhiyun  * idea to do:
203*4882a593Smuzhiyun  *
204*4882a593Smuzhiyun  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * and look at the resulting assemble code in serial.s.
207*4882a593Smuzhiyun  *
208*4882a593Smuzhiyun  * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
209*4882a593Smuzhiyun  * -----------------------------------------------------------------------
210*4882a593Smuzhiyun  */
211*4882a593Smuzhiyun 
receive_chars(struct serial_state * info)212*4882a593Smuzhiyun static void receive_chars(struct serial_state *info)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun         int status;
215*4882a593Smuzhiyun 	int serdatr;
216*4882a593Smuzhiyun 	unsigned char ch, flag;
217*4882a593Smuzhiyun 	struct	async_icount *icount;
218*4882a593Smuzhiyun 	int oe = 0;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	icount = &info->icount;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	status = UART_LSR_DR; /* We obviously have a character! */
223*4882a593Smuzhiyun 	serdatr = custom.serdatr;
224*4882a593Smuzhiyun 	mb();
225*4882a593Smuzhiyun 	custom.intreq = IF_RBF;
226*4882a593Smuzhiyun 	mb();
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	if((serdatr & 0x1ff) == 0)
229*4882a593Smuzhiyun 	    status |= UART_LSR_BI;
230*4882a593Smuzhiyun 	if(serdatr & SDR_OVRUN)
231*4882a593Smuzhiyun 	    status |= UART_LSR_OE;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	ch = serdatr & 0xff;
234*4882a593Smuzhiyun 	icount->rx++;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_INTR
237*4882a593Smuzhiyun 	printk("DR%02x:%02x...", ch, status);
238*4882a593Smuzhiyun #endif
239*4882a593Smuzhiyun 	flag = TTY_NORMAL;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	/*
242*4882a593Smuzhiyun 	 * We don't handle parity or frame errors - but I have left
243*4882a593Smuzhiyun 	 * the code in, since I'm not sure that the errors can't be
244*4882a593Smuzhiyun 	 * detected.
245*4882a593Smuzhiyun 	 */
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	if (status & (UART_LSR_BI | UART_LSR_PE |
248*4882a593Smuzhiyun 		      UART_LSR_FE | UART_LSR_OE)) {
249*4882a593Smuzhiyun 	  /*
250*4882a593Smuzhiyun 	   * For statistics only
251*4882a593Smuzhiyun 	   */
252*4882a593Smuzhiyun 	  if (status & UART_LSR_BI) {
253*4882a593Smuzhiyun 	    status &= ~(UART_LSR_FE | UART_LSR_PE);
254*4882a593Smuzhiyun 	    icount->brk++;
255*4882a593Smuzhiyun 	  } else if (status & UART_LSR_PE)
256*4882a593Smuzhiyun 	    icount->parity++;
257*4882a593Smuzhiyun 	  else if (status & UART_LSR_FE)
258*4882a593Smuzhiyun 	    icount->frame++;
259*4882a593Smuzhiyun 	  if (status & UART_LSR_OE)
260*4882a593Smuzhiyun 	    icount->overrun++;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	  /*
263*4882a593Smuzhiyun 	   * Now check to see if character should be
264*4882a593Smuzhiyun 	   * ignored, and mask off conditions which
265*4882a593Smuzhiyun 	   * should be ignored.
266*4882a593Smuzhiyun 	   */
267*4882a593Smuzhiyun 	  if (status & info->ignore_status_mask)
268*4882a593Smuzhiyun 	    goto out;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	  status &= info->read_status_mask;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	  if (status & (UART_LSR_BI)) {
273*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_INTR
274*4882a593Smuzhiyun 	    printk("handling break....");
275*4882a593Smuzhiyun #endif
276*4882a593Smuzhiyun 	    flag = TTY_BREAK;
277*4882a593Smuzhiyun 	    if (info->tport.flags & ASYNC_SAK)
278*4882a593Smuzhiyun 	      do_SAK(info->tport.tty);
279*4882a593Smuzhiyun 	  } else if (status & UART_LSR_PE)
280*4882a593Smuzhiyun 	    flag = TTY_PARITY;
281*4882a593Smuzhiyun 	  else if (status & UART_LSR_FE)
282*4882a593Smuzhiyun 	    flag = TTY_FRAME;
283*4882a593Smuzhiyun 	  if (status & UART_LSR_OE) {
284*4882a593Smuzhiyun 	    /*
285*4882a593Smuzhiyun 	     * Overrun is special, since it's
286*4882a593Smuzhiyun 	     * reported immediately, and doesn't
287*4882a593Smuzhiyun 	     * affect the current character
288*4882a593Smuzhiyun 	     */
289*4882a593Smuzhiyun 	     oe = 1;
290*4882a593Smuzhiyun 	  }
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 	tty_insert_flip_char(&info->tport, ch, flag);
293*4882a593Smuzhiyun 	if (oe == 1)
294*4882a593Smuzhiyun 		tty_insert_flip_char(&info->tport, 0, TTY_OVERRUN);
295*4882a593Smuzhiyun 	tty_flip_buffer_push(&info->tport);
296*4882a593Smuzhiyun out:
297*4882a593Smuzhiyun 	return;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun 
transmit_chars(struct serial_state * info)300*4882a593Smuzhiyun static void transmit_chars(struct serial_state *info)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	custom.intreq = IF_TBE;
303*4882a593Smuzhiyun 	mb();
304*4882a593Smuzhiyun 	if (info->x_char) {
305*4882a593Smuzhiyun 	        custom.serdat = info->x_char | 0x100;
306*4882a593Smuzhiyun 		mb();
307*4882a593Smuzhiyun 		info->icount.tx++;
308*4882a593Smuzhiyun 		info->x_char = 0;
309*4882a593Smuzhiyun 		return;
310*4882a593Smuzhiyun 	}
311*4882a593Smuzhiyun 	if (info->xmit.head == info->xmit.tail
312*4882a593Smuzhiyun 	    || info->tport.tty->stopped
313*4882a593Smuzhiyun 	    || info->tport.tty->hw_stopped) {
314*4882a593Smuzhiyun 		info->IER &= ~UART_IER_THRI;
315*4882a593Smuzhiyun 	        custom.intena = IF_TBE;
316*4882a593Smuzhiyun 		mb();
317*4882a593Smuzhiyun 		return;
318*4882a593Smuzhiyun 	}
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
321*4882a593Smuzhiyun 	mb();
322*4882a593Smuzhiyun 	info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
323*4882a593Smuzhiyun 	info->icount.tx++;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (CIRC_CNT(info->xmit.head,
326*4882a593Smuzhiyun 		     info->xmit.tail,
327*4882a593Smuzhiyun 		     SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
328*4882a593Smuzhiyun 		tty_wakeup(info->tport.tty);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_INTR
331*4882a593Smuzhiyun 	printk("THRE...");
332*4882a593Smuzhiyun #endif
333*4882a593Smuzhiyun 	if (info->xmit.head == info->xmit.tail) {
334*4882a593Smuzhiyun 	        custom.intena = IF_TBE;
335*4882a593Smuzhiyun 		mb();
336*4882a593Smuzhiyun 		info->IER &= ~UART_IER_THRI;
337*4882a593Smuzhiyun 	}
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
check_modem_status(struct serial_state * info)340*4882a593Smuzhiyun static void check_modem_status(struct serial_state *info)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun 	struct tty_port *port = &info->tport;
343*4882a593Smuzhiyun 	unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
344*4882a593Smuzhiyun 	unsigned char dstatus;
345*4882a593Smuzhiyun 	struct	async_icount *icount;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	/* Determine bits that have changed */
348*4882a593Smuzhiyun 	dstatus = status ^ current_ctl_bits;
349*4882a593Smuzhiyun 	current_ctl_bits = status;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	if (dstatus) {
352*4882a593Smuzhiyun 		icount = &info->icount;
353*4882a593Smuzhiyun 		/* update input line counters */
354*4882a593Smuzhiyun 		if (dstatus & SER_DSR)
355*4882a593Smuzhiyun 			icount->dsr++;
356*4882a593Smuzhiyun 		if (dstatus & SER_DCD) {
357*4882a593Smuzhiyun 			icount->dcd++;
358*4882a593Smuzhiyun 		}
359*4882a593Smuzhiyun 		if (dstatus & SER_CTS)
360*4882a593Smuzhiyun 			icount->cts++;
361*4882a593Smuzhiyun 		wake_up_interruptible(&port->delta_msr_wait);
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	if (tty_port_check_carrier(port) && (dstatus & SER_DCD)) {
365*4882a593Smuzhiyun #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
366*4882a593Smuzhiyun 		printk("ttyS%d CD now %s...", info->line,
367*4882a593Smuzhiyun 		       (!(status & SER_DCD)) ? "on" : "off");
368*4882a593Smuzhiyun #endif
369*4882a593Smuzhiyun 		if (!(status & SER_DCD))
370*4882a593Smuzhiyun 			wake_up_interruptible(&port->open_wait);
371*4882a593Smuzhiyun 		else {
372*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_OPEN
373*4882a593Smuzhiyun 			printk("doing serial hangup...");
374*4882a593Smuzhiyun #endif
375*4882a593Smuzhiyun 			if (port->tty)
376*4882a593Smuzhiyun 				tty_hangup(port->tty);
377*4882a593Smuzhiyun 		}
378*4882a593Smuzhiyun 	}
379*4882a593Smuzhiyun 	if (tty_port_cts_enabled(port)) {
380*4882a593Smuzhiyun 		if (port->tty->hw_stopped) {
381*4882a593Smuzhiyun 			if (!(status & SER_CTS)) {
382*4882a593Smuzhiyun #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
383*4882a593Smuzhiyun 				printk("CTS tx start...");
384*4882a593Smuzhiyun #endif
385*4882a593Smuzhiyun 				port->tty->hw_stopped = 0;
386*4882a593Smuzhiyun 				info->IER |= UART_IER_THRI;
387*4882a593Smuzhiyun 				custom.intena = IF_SETCLR | IF_TBE;
388*4882a593Smuzhiyun 				mb();
389*4882a593Smuzhiyun 				/* set a pending Tx Interrupt, transmitter should restart now */
390*4882a593Smuzhiyun 				custom.intreq = IF_SETCLR | IF_TBE;
391*4882a593Smuzhiyun 				mb();
392*4882a593Smuzhiyun 				tty_wakeup(port->tty);
393*4882a593Smuzhiyun 				return;
394*4882a593Smuzhiyun 			}
395*4882a593Smuzhiyun 		} else {
396*4882a593Smuzhiyun 			if ((status & SER_CTS)) {
397*4882a593Smuzhiyun #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
398*4882a593Smuzhiyun 				printk("CTS tx stop...");
399*4882a593Smuzhiyun #endif
400*4882a593Smuzhiyun 				port->tty->hw_stopped = 1;
401*4882a593Smuzhiyun 				info->IER &= ~UART_IER_THRI;
402*4882a593Smuzhiyun 				/* disable Tx interrupt and remove any pending interrupts */
403*4882a593Smuzhiyun 				custom.intena = IF_TBE;
404*4882a593Smuzhiyun 				mb();
405*4882a593Smuzhiyun 				custom.intreq = IF_TBE;
406*4882a593Smuzhiyun 				mb();
407*4882a593Smuzhiyun 			}
408*4882a593Smuzhiyun 		}
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
ser_vbl_int(int irq,void * data)412*4882a593Smuzhiyun static irqreturn_t ser_vbl_int( int irq, void *data)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun         /* vbl is just a periodic interrupt we tie into to update modem status */
415*4882a593Smuzhiyun 	struct serial_state *info = data;
416*4882a593Smuzhiyun 	/*
417*4882a593Smuzhiyun 	 * TBD - is it better to unregister from this interrupt or to
418*4882a593Smuzhiyun 	 * ignore it if MSI is clear ?
419*4882a593Smuzhiyun 	 */
420*4882a593Smuzhiyun 	if(info->IER & UART_IER_MSI)
421*4882a593Smuzhiyun 	  check_modem_status(info);
422*4882a593Smuzhiyun 	return IRQ_HANDLED;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
ser_rx_int(int irq,void * dev_id)425*4882a593Smuzhiyun static irqreturn_t ser_rx_int(int irq, void *dev_id)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	struct serial_state *info = dev_id;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_INTR
430*4882a593Smuzhiyun 	printk("ser_rx_int...");
431*4882a593Smuzhiyun #endif
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	if (!info->tport.tty)
434*4882a593Smuzhiyun 		return IRQ_NONE;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	receive_chars(info);
437*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_INTR
438*4882a593Smuzhiyun 	printk("end.\n");
439*4882a593Smuzhiyun #endif
440*4882a593Smuzhiyun 	return IRQ_HANDLED;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun 
ser_tx_int(int irq,void * dev_id)443*4882a593Smuzhiyun static irqreturn_t ser_tx_int(int irq, void *dev_id)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	struct serial_state *info = dev_id;
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	if (custom.serdatr & SDR_TBE) {
448*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_INTR
449*4882a593Smuzhiyun 	  printk("ser_tx_int...");
450*4882a593Smuzhiyun #endif
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	  if (!info->tport.tty)
453*4882a593Smuzhiyun 		return IRQ_NONE;
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	  transmit_chars(info);
456*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_INTR
457*4882a593Smuzhiyun 	  printk("end.\n");
458*4882a593Smuzhiyun #endif
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun 	return IRQ_HANDLED;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun /*
464*4882a593Smuzhiyun  * -------------------------------------------------------------------
465*4882a593Smuzhiyun  * Here ends the serial interrupt routines.
466*4882a593Smuzhiyun  * -------------------------------------------------------------------
467*4882a593Smuzhiyun  */
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun /*
470*4882a593Smuzhiyun  * ---------------------------------------------------------------
471*4882a593Smuzhiyun  * Low level utility subroutines for the serial driver:  routines to
472*4882a593Smuzhiyun  * figure out the appropriate timeout for an interrupt chain, routines
473*4882a593Smuzhiyun  * to initialize and startup a serial port, and routines to shutdown a
474*4882a593Smuzhiyun  * serial port.  Useful stuff like that.
475*4882a593Smuzhiyun  * ---------------------------------------------------------------
476*4882a593Smuzhiyun  */
477*4882a593Smuzhiyun 
startup(struct tty_struct * tty,struct serial_state * info)478*4882a593Smuzhiyun static int startup(struct tty_struct *tty, struct serial_state *info)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun 	struct tty_port *port = &info->tport;
481*4882a593Smuzhiyun 	unsigned long flags;
482*4882a593Smuzhiyun 	int	retval=0;
483*4882a593Smuzhiyun 	unsigned long page;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	page = get_zeroed_page(GFP_KERNEL);
486*4882a593Smuzhiyun 	if (!page)
487*4882a593Smuzhiyun 		return -ENOMEM;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	local_irq_save(flags);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	if (tty_port_initialized(port)) {
492*4882a593Smuzhiyun 		free_page(page);
493*4882a593Smuzhiyun 		goto errout;
494*4882a593Smuzhiyun 	}
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	if (info->xmit.buf)
497*4882a593Smuzhiyun 		free_page(page);
498*4882a593Smuzhiyun 	else
499*4882a593Smuzhiyun 		info->xmit.buf = (unsigned char *) page;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_OPEN
502*4882a593Smuzhiyun 	printk("starting up ttys%d ...", info->line);
503*4882a593Smuzhiyun #endif
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	/* Clear anything in the input buffer */
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	custom.intreq = IF_RBF;
508*4882a593Smuzhiyun 	mb();
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
511*4882a593Smuzhiyun 	if (retval) {
512*4882a593Smuzhiyun 	  if (serial_isroot()) {
513*4882a593Smuzhiyun 	      set_bit(TTY_IO_ERROR, &tty->flags);
514*4882a593Smuzhiyun 	    retval = 0;
515*4882a593Smuzhiyun 	  }
516*4882a593Smuzhiyun 	  goto errout;
517*4882a593Smuzhiyun 	}
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	/* enable both Rx and Tx interrupts */
520*4882a593Smuzhiyun 	custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
521*4882a593Smuzhiyun 	mb();
522*4882a593Smuzhiyun 	info->IER = UART_IER_MSI;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	/* remember current state of the DCD and CTS bits */
525*4882a593Smuzhiyun 	current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	info->MCR = 0;
528*4882a593Smuzhiyun 	if (C_BAUD(tty))
529*4882a593Smuzhiyun 	  info->MCR = SER_DTR | SER_RTS;
530*4882a593Smuzhiyun 	rtsdtr_ctrl(info->MCR);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	clear_bit(TTY_IO_ERROR, &tty->flags);
533*4882a593Smuzhiyun 	info->xmit.head = info->xmit.tail = 0;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	/*
536*4882a593Smuzhiyun 	 * and set the speed of the serial port
537*4882a593Smuzhiyun 	 */
538*4882a593Smuzhiyun 	change_speed(tty, info, NULL);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	tty_port_set_initialized(port, 1);
541*4882a593Smuzhiyun 	local_irq_restore(flags);
542*4882a593Smuzhiyun 	return 0;
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun errout:
545*4882a593Smuzhiyun 	local_irq_restore(flags);
546*4882a593Smuzhiyun 	return retval;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun /*
550*4882a593Smuzhiyun  * This routine will shutdown a serial port; interrupts are disabled, and
551*4882a593Smuzhiyun  * DTR is dropped if the hangup on close termio flag is on.
552*4882a593Smuzhiyun  */
shutdown(struct tty_struct * tty,struct serial_state * info)553*4882a593Smuzhiyun static void shutdown(struct tty_struct *tty, struct serial_state *info)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun 	unsigned long	flags;
556*4882a593Smuzhiyun 	struct serial_state *state;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	if (!tty_port_initialized(&info->tport))
559*4882a593Smuzhiyun 		return;
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	state = info;
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_OPEN
564*4882a593Smuzhiyun 	printk("Shutting down serial port %d ....\n", info->line);
565*4882a593Smuzhiyun #endif
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	local_irq_save(flags); /* Disable interrupts */
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	/*
570*4882a593Smuzhiyun 	 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
571*4882a593Smuzhiyun 	 * here so the queue might never be waken up
572*4882a593Smuzhiyun 	 */
573*4882a593Smuzhiyun 	wake_up_interruptible(&info->tport.delta_msr_wait);
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	/*
576*4882a593Smuzhiyun 	 * Free the IRQ, if necessary
577*4882a593Smuzhiyun 	 */
578*4882a593Smuzhiyun 	free_irq(IRQ_AMIGA_VERTB, info);
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	if (info->xmit.buf) {
581*4882a593Smuzhiyun 		free_page((unsigned long) info->xmit.buf);
582*4882a593Smuzhiyun 		info->xmit.buf = NULL;
583*4882a593Smuzhiyun 	}
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	info->IER = 0;
586*4882a593Smuzhiyun 	custom.intena = IF_RBF | IF_TBE;
587*4882a593Smuzhiyun 	mb();
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	/* disable break condition */
590*4882a593Smuzhiyun 	custom.adkcon = AC_UARTBRK;
591*4882a593Smuzhiyun 	mb();
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	if (C_HUPCL(tty))
594*4882a593Smuzhiyun 		info->MCR &= ~(SER_DTR|SER_RTS);
595*4882a593Smuzhiyun 	rtsdtr_ctrl(info->MCR);
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	set_bit(TTY_IO_ERROR, &tty->flags);
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	tty_port_set_initialized(&info->tport, 0);
600*4882a593Smuzhiyun 	local_irq_restore(flags);
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun /*
605*4882a593Smuzhiyun  * This routine is called to set the UART divisor registers to match
606*4882a593Smuzhiyun  * the specified baud rate for a serial port.
607*4882a593Smuzhiyun  */
change_speed(struct tty_struct * tty,struct serial_state * info,struct ktermios * old_termios)608*4882a593Smuzhiyun static void change_speed(struct tty_struct *tty, struct serial_state *info,
609*4882a593Smuzhiyun 			 struct ktermios *old_termios)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	struct tty_port *port = &info->tport;
612*4882a593Smuzhiyun 	int	quot = 0, baud_base, baud;
613*4882a593Smuzhiyun 	unsigned cflag, cval = 0;
614*4882a593Smuzhiyun 	int	bits;
615*4882a593Smuzhiyun 	unsigned long	flags;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	cflag = tty->termios.c_cflag;
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	/* Byte size is always 8 bits plus parity bit if requested */
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	cval = 3; bits = 10;
622*4882a593Smuzhiyun 	if (cflag & CSTOPB) {
623*4882a593Smuzhiyun 		cval |= 0x04;
624*4882a593Smuzhiyun 		bits++;
625*4882a593Smuzhiyun 	}
626*4882a593Smuzhiyun 	if (cflag & PARENB) {
627*4882a593Smuzhiyun 		cval |= UART_LCR_PARITY;
628*4882a593Smuzhiyun 		bits++;
629*4882a593Smuzhiyun 	}
630*4882a593Smuzhiyun 	if (!(cflag & PARODD))
631*4882a593Smuzhiyun 		cval |= UART_LCR_EPAR;
632*4882a593Smuzhiyun #ifdef CMSPAR
633*4882a593Smuzhiyun 	if (cflag & CMSPAR)
634*4882a593Smuzhiyun 		cval |= UART_LCR_SPAR;
635*4882a593Smuzhiyun #endif
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	/* Determine divisor based on baud rate */
638*4882a593Smuzhiyun 	baud = tty_get_baud_rate(tty);
639*4882a593Smuzhiyun 	if (!baud)
640*4882a593Smuzhiyun 		baud = 9600;	/* B0 transition handled in rs_set_termios */
641*4882a593Smuzhiyun 	baud_base = info->baud_base;
642*4882a593Smuzhiyun 	if (baud == 38400 && (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
643*4882a593Smuzhiyun 		quot = info->custom_divisor;
644*4882a593Smuzhiyun 	else {
645*4882a593Smuzhiyun 		if (baud == 134)
646*4882a593Smuzhiyun 			/* Special case since 134 is really 134.5 */
647*4882a593Smuzhiyun 			quot = (2*baud_base / 269);
648*4882a593Smuzhiyun 		else if (baud)
649*4882a593Smuzhiyun 			quot = baud_base / baud;
650*4882a593Smuzhiyun 	}
651*4882a593Smuzhiyun 	/* If the quotient is zero refuse the change */
652*4882a593Smuzhiyun 	if (!quot && old_termios) {
653*4882a593Smuzhiyun 		/* FIXME: Will need updating for new tty in the end */
654*4882a593Smuzhiyun 		tty->termios.c_cflag &= ~CBAUD;
655*4882a593Smuzhiyun 		tty->termios.c_cflag |= (old_termios->c_cflag & CBAUD);
656*4882a593Smuzhiyun 		baud = tty_get_baud_rate(tty);
657*4882a593Smuzhiyun 		if (!baud)
658*4882a593Smuzhiyun 			baud = 9600;
659*4882a593Smuzhiyun 		if (baud == 38400 &&
660*4882a593Smuzhiyun 		    (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
661*4882a593Smuzhiyun 			quot = info->custom_divisor;
662*4882a593Smuzhiyun 		else {
663*4882a593Smuzhiyun 			if (baud == 134)
664*4882a593Smuzhiyun 				/* Special case since 134 is really 134.5 */
665*4882a593Smuzhiyun 				quot = (2*baud_base / 269);
666*4882a593Smuzhiyun 			else if (baud)
667*4882a593Smuzhiyun 				quot = baud_base / baud;
668*4882a593Smuzhiyun 		}
669*4882a593Smuzhiyun 	}
670*4882a593Smuzhiyun 	/* As a last resort, if the quotient is zero, default to 9600 bps */
671*4882a593Smuzhiyun 	if (!quot)
672*4882a593Smuzhiyun 		quot = baud_base / 9600;
673*4882a593Smuzhiyun 	info->quot = quot;
674*4882a593Smuzhiyun 	info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
675*4882a593Smuzhiyun 	info->timeout += HZ/50;		/* Add .02 seconds of slop */
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	/* CTS flow control flag and modem status interrupts */
678*4882a593Smuzhiyun 	info->IER &= ~UART_IER_MSI;
679*4882a593Smuzhiyun 	if (port->flags & ASYNC_HARDPPS_CD)
680*4882a593Smuzhiyun 		info->IER |= UART_IER_MSI;
681*4882a593Smuzhiyun 	tty_port_set_cts_flow(port, cflag & CRTSCTS);
682*4882a593Smuzhiyun 	if (cflag & CRTSCTS)
683*4882a593Smuzhiyun 		info->IER |= UART_IER_MSI;
684*4882a593Smuzhiyun 	tty_port_set_check_carrier(port, ~cflag & CLOCAL);
685*4882a593Smuzhiyun 	if (~cflag & CLOCAL)
686*4882a593Smuzhiyun 		info->IER |= UART_IER_MSI;
687*4882a593Smuzhiyun 	/* TBD:
688*4882a593Smuzhiyun 	 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
689*4882a593Smuzhiyun 	 */
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	/*
692*4882a593Smuzhiyun 	 * Set up parity check flag
693*4882a593Smuzhiyun 	 */
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
696*4882a593Smuzhiyun 	if (I_INPCK(tty))
697*4882a593Smuzhiyun 		info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
698*4882a593Smuzhiyun 	if (I_BRKINT(tty) || I_PARMRK(tty))
699*4882a593Smuzhiyun 		info->read_status_mask |= UART_LSR_BI;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	/*
702*4882a593Smuzhiyun 	 * Characters to ignore
703*4882a593Smuzhiyun 	 */
704*4882a593Smuzhiyun 	info->ignore_status_mask = 0;
705*4882a593Smuzhiyun 	if (I_IGNPAR(tty))
706*4882a593Smuzhiyun 		info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
707*4882a593Smuzhiyun 	if (I_IGNBRK(tty)) {
708*4882a593Smuzhiyun 		info->ignore_status_mask |= UART_LSR_BI;
709*4882a593Smuzhiyun 		/*
710*4882a593Smuzhiyun 		 * If we're ignore parity and break indicators, ignore
711*4882a593Smuzhiyun 		 * overruns too.  (For real raw support).
712*4882a593Smuzhiyun 		 */
713*4882a593Smuzhiyun 		if (I_IGNPAR(tty))
714*4882a593Smuzhiyun 			info->ignore_status_mask |= UART_LSR_OE;
715*4882a593Smuzhiyun 	}
716*4882a593Smuzhiyun 	/*
717*4882a593Smuzhiyun 	 * !!! ignore all characters if CREAD is not set
718*4882a593Smuzhiyun 	 */
719*4882a593Smuzhiyun 	if ((cflag & CREAD) == 0)
720*4882a593Smuzhiyun 		info->ignore_status_mask |= UART_LSR_DR;
721*4882a593Smuzhiyun 	local_irq_save(flags);
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	{
724*4882a593Smuzhiyun 	  short serper;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	/* Set up the baud rate */
727*4882a593Smuzhiyun 	  serper = quot - 1;
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	/* Enable or disable parity bit */
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	if(cval & UART_LCR_PARITY)
732*4882a593Smuzhiyun 	  serper |= (SERPER_PARENB);
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	custom.serper = serper;
735*4882a593Smuzhiyun 	mb();
736*4882a593Smuzhiyun 	}
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	local_irq_restore(flags);
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun 
rs_put_char(struct tty_struct * tty,unsigned char ch)741*4882a593Smuzhiyun static int rs_put_char(struct tty_struct *tty, unsigned char ch)
742*4882a593Smuzhiyun {
743*4882a593Smuzhiyun 	struct serial_state *info;
744*4882a593Smuzhiyun 	unsigned long flags;
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	info = tty->driver_data;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	if (!info->xmit.buf)
749*4882a593Smuzhiyun 		return 0;
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	local_irq_save(flags);
752*4882a593Smuzhiyun 	if (CIRC_SPACE(info->xmit.head,
753*4882a593Smuzhiyun 		       info->xmit.tail,
754*4882a593Smuzhiyun 		       SERIAL_XMIT_SIZE) == 0) {
755*4882a593Smuzhiyun 		local_irq_restore(flags);
756*4882a593Smuzhiyun 		return 0;
757*4882a593Smuzhiyun 	}
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	info->xmit.buf[info->xmit.head++] = ch;
760*4882a593Smuzhiyun 	info->xmit.head &= SERIAL_XMIT_SIZE-1;
761*4882a593Smuzhiyun 	local_irq_restore(flags);
762*4882a593Smuzhiyun 	return 1;
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun 
rs_flush_chars(struct tty_struct * tty)765*4882a593Smuzhiyun static void rs_flush_chars(struct tty_struct *tty)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
768*4882a593Smuzhiyun 	unsigned long flags;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	if (info->xmit.head == info->xmit.tail
771*4882a593Smuzhiyun 	    || tty->stopped
772*4882a593Smuzhiyun 	    || tty->hw_stopped
773*4882a593Smuzhiyun 	    || !info->xmit.buf)
774*4882a593Smuzhiyun 		return;
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	local_irq_save(flags);
777*4882a593Smuzhiyun 	info->IER |= UART_IER_THRI;
778*4882a593Smuzhiyun 	custom.intena = IF_SETCLR | IF_TBE;
779*4882a593Smuzhiyun 	mb();
780*4882a593Smuzhiyun 	/* set a pending Tx Interrupt, transmitter should restart now */
781*4882a593Smuzhiyun 	custom.intreq = IF_SETCLR | IF_TBE;
782*4882a593Smuzhiyun 	mb();
783*4882a593Smuzhiyun 	local_irq_restore(flags);
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
rs_write(struct tty_struct * tty,const unsigned char * buf,int count)786*4882a593Smuzhiyun static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
787*4882a593Smuzhiyun {
788*4882a593Smuzhiyun 	int	c, ret = 0;
789*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
790*4882a593Smuzhiyun 	unsigned long flags;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	if (!info->xmit.buf)
793*4882a593Smuzhiyun 		return 0;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	local_irq_save(flags);
796*4882a593Smuzhiyun 	while (1) {
797*4882a593Smuzhiyun 		c = CIRC_SPACE_TO_END(info->xmit.head,
798*4882a593Smuzhiyun 				      info->xmit.tail,
799*4882a593Smuzhiyun 				      SERIAL_XMIT_SIZE);
800*4882a593Smuzhiyun 		if (count < c)
801*4882a593Smuzhiyun 			c = count;
802*4882a593Smuzhiyun 		if (c <= 0) {
803*4882a593Smuzhiyun 			break;
804*4882a593Smuzhiyun 		}
805*4882a593Smuzhiyun 		memcpy(info->xmit.buf + info->xmit.head, buf, c);
806*4882a593Smuzhiyun 		info->xmit.head = ((info->xmit.head + c) &
807*4882a593Smuzhiyun 				   (SERIAL_XMIT_SIZE-1));
808*4882a593Smuzhiyun 		buf += c;
809*4882a593Smuzhiyun 		count -= c;
810*4882a593Smuzhiyun 		ret += c;
811*4882a593Smuzhiyun 	}
812*4882a593Smuzhiyun 	local_irq_restore(flags);
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	if (info->xmit.head != info->xmit.tail
815*4882a593Smuzhiyun 	    && !tty->stopped
816*4882a593Smuzhiyun 	    && !tty->hw_stopped
817*4882a593Smuzhiyun 	    && !(info->IER & UART_IER_THRI)) {
818*4882a593Smuzhiyun 		info->IER |= UART_IER_THRI;
819*4882a593Smuzhiyun 		local_irq_disable();
820*4882a593Smuzhiyun 		custom.intena = IF_SETCLR | IF_TBE;
821*4882a593Smuzhiyun 		mb();
822*4882a593Smuzhiyun 		/* set a pending Tx Interrupt, transmitter should restart now */
823*4882a593Smuzhiyun 		custom.intreq = IF_SETCLR | IF_TBE;
824*4882a593Smuzhiyun 		mb();
825*4882a593Smuzhiyun 		local_irq_restore(flags);
826*4882a593Smuzhiyun 	}
827*4882a593Smuzhiyun 	return ret;
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun 
rs_write_room(struct tty_struct * tty)830*4882a593Smuzhiyun static int rs_write_room(struct tty_struct *tty)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun 
rs_chars_in_buffer(struct tty_struct * tty)837*4882a593Smuzhiyun static int rs_chars_in_buffer(struct tty_struct *tty)
838*4882a593Smuzhiyun {
839*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
842*4882a593Smuzhiyun }
843*4882a593Smuzhiyun 
rs_flush_buffer(struct tty_struct * tty)844*4882a593Smuzhiyun static void rs_flush_buffer(struct tty_struct *tty)
845*4882a593Smuzhiyun {
846*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
847*4882a593Smuzhiyun 	unsigned long flags;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	local_irq_save(flags);
850*4882a593Smuzhiyun 	info->xmit.head = info->xmit.tail = 0;
851*4882a593Smuzhiyun 	local_irq_restore(flags);
852*4882a593Smuzhiyun 	tty_wakeup(tty);
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun /*
856*4882a593Smuzhiyun  * This function is used to send a high-priority XON/XOFF character to
857*4882a593Smuzhiyun  * the device
858*4882a593Smuzhiyun  */
rs_send_xchar(struct tty_struct * tty,char ch)859*4882a593Smuzhiyun static void rs_send_xchar(struct tty_struct *tty, char ch)
860*4882a593Smuzhiyun {
861*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
862*4882a593Smuzhiyun         unsigned long flags;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	info->x_char = ch;
865*4882a593Smuzhiyun 	if (ch) {
866*4882a593Smuzhiyun 		/* Make sure transmit interrupts are on */
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	        /* Check this ! */
869*4882a593Smuzhiyun 	        local_irq_save(flags);
870*4882a593Smuzhiyun 		if(!(custom.intenar & IF_TBE)) {
871*4882a593Smuzhiyun 		    custom.intena = IF_SETCLR | IF_TBE;
872*4882a593Smuzhiyun 		    mb();
873*4882a593Smuzhiyun 		    /* set a pending Tx Interrupt, transmitter should restart now */
874*4882a593Smuzhiyun 		    custom.intreq = IF_SETCLR | IF_TBE;
875*4882a593Smuzhiyun 		    mb();
876*4882a593Smuzhiyun 		}
877*4882a593Smuzhiyun 		local_irq_restore(flags);
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 		info->IER |= UART_IER_THRI;
880*4882a593Smuzhiyun 	}
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun /*
884*4882a593Smuzhiyun  * ------------------------------------------------------------
885*4882a593Smuzhiyun  * rs_throttle()
886*4882a593Smuzhiyun  *
887*4882a593Smuzhiyun  * This routine is called by the upper-layer tty layer to signal that
888*4882a593Smuzhiyun  * incoming characters should be throttled.
889*4882a593Smuzhiyun  * ------------------------------------------------------------
890*4882a593Smuzhiyun  */
rs_throttle(struct tty_struct * tty)891*4882a593Smuzhiyun static void rs_throttle(struct tty_struct * tty)
892*4882a593Smuzhiyun {
893*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
894*4882a593Smuzhiyun 	unsigned long flags;
895*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_THROTTLE
896*4882a593Smuzhiyun 	printk("throttle %s ....\n", tty_name(tty));
897*4882a593Smuzhiyun #endif
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	if (I_IXOFF(tty))
900*4882a593Smuzhiyun 		rs_send_xchar(tty, STOP_CHAR(tty));
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	if (C_CRTSCTS(tty))
903*4882a593Smuzhiyun 		info->MCR &= ~SER_RTS;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	local_irq_save(flags);
906*4882a593Smuzhiyun 	rtsdtr_ctrl(info->MCR);
907*4882a593Smuzhiyun 	local_irq_restore(flags);
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun 
rs_unthrottle(struct tty_struct * tty)910*4882a593Smuzhiyun static void rs_unthrottle(struct tty_struct * tty)
911*4882a593Smuzhiyun {
912*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
913*4882a593Smuzhiyun 	unsigned long flags;
914*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_THROTTLE
915*4882a593Smuzhiyun 	printk("unthrottle %s ....\n", tty_name(tty));
916*4882a593Smuzhiyun #endif
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	if (I_IXOFF(tty)) {
919*4882a593Smuzhiyun 		if (info->x_char)
920*4882a593Smuzhiyun 			info->x_char = 0;
921*4882a593Smuzhiyun 		else
922*4882a593Smuzhiyun 			rs_send_xchar(tty, START_CHAR(tty));
923*4882a593Smuzhiyun 	}
924*4882a593Smuzhiyun 	if (C_CRTSCTS(tty))
925*4882a593Smuzhiyun 		info->MCR |= SER_RTS;
926*4882a593Smuzhiyun 	local_irq_save(flags);
927*4882a593Smuzhiyun 	rtsdtr_ctrl(info->MCR);
928*4882a593Smuzhiyun 	local_irq_restore(flags);
929*4882a593Smuzhiyun }
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun /*
932*4882a593Smuzhiyun  * ------------------------------------------------------------
933*4882a593Smuzhiyun  * rs_ioctl() and friends
934*4882a593Smuzhiyun  * ------------------------------------------------------------
935*4882a593Smuzhiyun  */
936*4882a593Smuzhiyun 
get_serial_info(struct tty_struct * tty,struct serial_struct * ss)937*4882a593Smuzhiyun static int get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
938*4882a593Smuzhiyun {
939*4882a593Smuzhiyun 	struct serial_state *state = tty->driver_data;
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	tty_lock(tty);
942*4882a593Smuzhiyun 	ss->line = tty->index;
943*4882a593Smuzhiyun 	ss->port = state->port;
944*4882a593Smuzhiyun 	ss->flags = state->tport.flags;
945*4882a593Smuzhiyun 	ss->xmit_fifo_size = state->xmit_fifo_size;
946*4882a593Smuzhiyun 	ss->baud_base = state->baud_base;
947*4882a593Smuzhiyun 	ss->close_delay = state->tport.close_delay;
948*4882a593Smuzhiyun 	ss->closing_wait = state->tport.closing_wait;
949*4882a593Smuzhiyun 	ss->custom_divisor = state->custom_divisor;
950*4882a593Smuzhiyun 	tty_unlock(tty);
951*4882a593Smuzhiyun 	return 0;
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun 
set_serial_info(struct tty_struct * tty,struct serial_struct * ss)954*4882a593Smuzhiyun static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
955*4882a593Smuzhiyun {
956*4882a593Smuzhiyun 	struct serial_state *state = tty->driver_data;
957*4882a593Smuzhiyun 	struct tty_port *port = &state->tport;
958*4882a593Smuzhiyun 	bool change_spd;
959*4882a593Smuzhiyun 	int 			retval = 0;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	tty_lock(tty);
962*4882a593Smuzhiyun 	change_spd = ((ss->flags ^ port->flags) & ASYNC_SPD_MASK) ||
963*4882a593Smuzhiyun 		ss->custom_divisor != state->custom_divisor;
964*4882a593Smuzhiyun 	if (ss->irq || ss->port != state->port ||
965*4882a593Smuzhiyun 			ss->xmit_fifo_size != state->xmit_fifo_size) {
966*4882a593Smuzhiyun 		tty_unlock(tty);
967*4882a593Smuzhiyun 		return -EINVAL;
968*4882a593Smuzhiyun 	}
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	if (!serial_isroot()) {
971*4882a593Smuzhiyun 		if ((ss->baud_base != state->baud_base) ||
972*4882a593Smuzhiyun 		    (ss->close_delay != port->close_delay) ||
973*4882a593Smuzhiyun 		    (ss->closing_wait != port->closing_wait) ||
974*4882a593Smuzhiyun 		    (ss->xmit_fifo_size != state->xmit_fifo_size) ||
975*4882a593Smuzhiyun 		    ((ss->flags & ~ASYNC_USR_MASK) !=
976*4882a593Smuzhiyun 		     (port->flags & ~ASYNC_USR_MASK))) {
977*4882a593Smuzhiyun 			tty_unlock(tty);
978*4882a593Smuzhiyun 			return -EPERM;
979*4882a593Smuzhiyun 		}
980*4882a593Smuzhiyun 		port->flags = ((port->flags & ~ASYNC_USR_MASK) |
981*4882a593Smuzhiyun 			       (ss->flags & ASYNC_USR_MASK));
982*4882a593Smuzhiyun 		state->custom_divisor = ss->custom_divisor;
983*4882a593Smuzhiyun 		goto check_and_exit;
984*4882a593Smuzhiyun 	}
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 	if (ss->baud_base < 9600) {
987*4882a593Smuzhiyun 		tty_unlock(tty);
988*4882a593Smuzhiyun 		return -EINVAL;
989*4882a593Smuzhiyun 	}
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	/*
992*4882a593Smuzhiyun 	 * OK, past this point, all the error checking has been done.
993*4882a593Smuzhiyun 	 * At this point, we start making changes.....
994*4882a593Smuzhiyun 	 */
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	state->baud_base = ss->baud_base;
997*4882a593Smuzhiyun 	port->flags = ((port->flags & ~ASYNC_FLAGS) |
998*4882a593Smuzhiyun 			(ss->flags & ASYNC_FLAGS));
999*4882a593Smuzhiyun 	state->custom_divisor = ss->custom_divisor;
1000*4882a593Smuzhiyun 	port->close_delay = ss->close_delay * HZ/100;
1001*4882a593Smuzhiyun 	port->closing_wait = ss->closing_wait * HZ/100;
1002*4882a593Smuzhiyun 	port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun check_and_exit:
1005*4882a593Smuzhiyun 	if (tty_port_initialized(port)) {
1006*4882a593Smuzhiyun 		if (change_spd) {
1007*4882a593Smuzhiyun 			/* warn about deprecation unless clearing */
1008*4882a593Smuzhiyun 			if (ss->flags & ASYNC_SPD_MASK)
1009*4882a593Smuzhiyun 				dev_warn_ratelimited(tty->dev, "use of SPD flags is deprecated\n");
1010*4882a593Smuzhiyun 			change_speed(tty, state, NULL);
1011*4882a593Smuzhiyun 		}
1012*4882a593Smuzhiyun 	} else
1013*4882a593Smuzhiyun 		retval = startup(tty, state);
1014*4882a593Smuzhiyun 	tty_unlock(tty);
1015*4882a593Smuzhiyun 	return retval;
1016*4882a593Smuzhiyun }
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun /*
1019*4882a593Smuzhiyun  * get_lsr_info - get line status register info
1020*4882a593Smuzhiyun  *
1021*4882a593Smuzhiyun  * Purpose: Let user call ioctl() to get info when the UART physically
1022*4882a593Smuzhiyun  * 	    is emptied.  On bus types like RS485, the transmitter must
1023*4882a593Smuzhiyun  * 	    release the bus after transmitting. This must be done when
1024*4882a593Smuzhiyun  * 	    the transmit shift register is empty, not be done when the
1025*4882a593Smuzhiyun  * 	    transmit holding register is empty.  This functionality
1026*4882a593Smuzhiyun  * 	    allows an RS485 driver to be written in user space.
1027*4882a593Smuzhiyun  */
get_lsr_info(struct serial_state * info,unsigned int __user * value)1028*4882a593Smuzhiyun static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
1029*4882a593Smuzhiyun {
1030*4882a593Smuzhiyun 	unsigned char status;
1031*4882a593Smuzhiyun 	unsigned int result;
1032*4882a593Smuzhiyun 	unsigned long flags;
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 	local_irq_save(flags);
1035*4882a593Smuzhiyun 	status = custom.serdatr;
1036*4882a593Smuzhiyun 	mb();
1037*4882a593Smuzhiyun 	local_irq_restore(flags);
1038*4882a593Smuzhiyun 	result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1039*4882a593Smuzhiyun 	if (copy_to_user(value, &result, sizeof(int)))
1040*4882a593Smuzhiyun 		return -EFAULT;
1041*4882a593Smuzhiyun 	return 0;
1042*4882a593Smuzhiyun }
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 
rs_tiocmget(struct tty_struct * tty)1045*4882a593Smuzhiyun static int rs_tiocmget(struct tty_struct *tty)
1046*4882a593Smuzhiyun {
1047*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
1048*4882a593Smuzhiyun 	unsigned char control, status;
1049*4882a593Smuzhiyun 	unsigned long flags;
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	if (tty_io_error(tty))
1052*4882a593Smuzhiyun 		return -EIO;
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	control = info->MCR;
1055*4882a593Smuzhiyun 	local_irq_save(flags);
1056*4882a593Smuzhiyun 	status = ciab.pra;
1057*4882a593Smuzhiyun 	local_irq_restore(flags);
1058*4882a593Smuzhiyun 	return    ((control & SER_RTS) ? TIOCM_RTS : 0)
1059*4882a593Smuzhiyun 		| ((control & SER_DTR) ? TIOCM_DTR : 0)
1060*4882a593Smuzhiyun 		| (!(status  & SER_DCD) ? TIOCM_CAR : 0)
1061*4882a593Smuzhiyun 		| (!(status  & SER_DSR) ? TIOCM_DSR : 0)
1062*4882a593Smuzhiyun 		| (!(status  & SER_CTS) ? TIOCM_CTS : 0);
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun 
rs_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)1065*4882a593Smuzhiyun static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1066*4882a593Smuzhiyun 						unsigned int clear)
1067*4882a593Smuzhiyun {
1068*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
1069*4882a593Smuzhiyun 	unsigned long flags;
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	if (tty_io_error(tty))
1072*4882a593Smuzhiyun 		return -EIO;
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 	local_irq_save(flags);
1075*4882a593Smuzhiyun 	if (set & TIOCM_RTS)
1076*4882a593Smuzhiyun 		info->MCR |= SER_RTS;
1077*4882a593Smuzhiyun 	if (set & TIOCM_DTR)
1078*4882a593Smuzhiyun 		info->MCR |= SER_DTR;
1079*4882a593Smuzhiyun 	if (clear & TIOCM_RTS)
1080*4882a593Smuzhiyun 		info->MCR &= ~SER_RTS;
1081*4882a593Smuzhiyun 	if (clear & TIOCM_DTR)
1082*4882a593Smuzhiyun 		info->MCR &= ~SER_DTR;
1083*4882a593Smuzhiyun 	rtsdtr_ctrl(info->MCR);
1084*4882a593Smuzhiyun 	local_irq_restore(flags);
1085*4882a593Smuzhiyun 	return 0;
1086*4882a593Smuzhiyun }
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun /*
1089*4882a593Smuzhiyun  * rs_break() --- routine which turns the break handling on or off
1090*4882a593Smuzhiyun  */
rs_break(struct tty_struct * tty,int break_state)1091*4882a593Smuzhiyun static int rs_break(struct tty_struct *tty, int break_state)
1092*4882a593Smuzhiyun {
1093*4882a593Smuzhiyun 	unsigned long flags;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	local_irq_save(flags);
1096*4882a593Smuzhiyun 	if (break_state == -1)
1097*4882a593Smuzhiyun 	  custom.adkcon = AC_SETCLR | AC_UARTBRK;
1098*4882a593Smuzhiyun 	else
1099*4882a593Smuzhiyun 	  custom.adkcon = AC_UARTBRK;
1100*4882a593Smuzhiyun 	mb();
1101*4882a593Smuzhiyun 	local_irq_restore(flags);
1102*4882a593Smuzhiyun 	return 0;
1103*4882a593Smuzhiyun }
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun /*
1106*4882a593Smuzhiyun  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1107*4882a593Smuzhiyun  * Return: write counters to the user passed counter struct
1108*4882a593Smuzhiyun  * NB: both 1->0 and 0->1 transitions are counted except for
1109*4882a593Smuzhiyun  *     RI where only 0->1 is counted.
1110*4882a593Smuzhiyun  */
rs_get_icount(struct tty_struct * tty,struct serial_icounter_struct * icount)1111*4882a593Smuzhiyun static int rs_get_icount(struct tty_struct *tty,
1112*4882a593Smuzhiyun 				struct serial_icounter_struct *icount)
1113*4882a593Smuzhiyun {
1114*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
1115*4882a593Smuzhiyun 	struct async_icount cnow;
1116*4882a593Smuzhiyun 	unsigned long flags;
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	local_irq_save(flags);
1119*4882a593Smuzhiyun 	cnow = info->icount;
1120*4882a593Smuzhiyun 	local_irq_restore(flags);
1121*4882a593Smuzhiyun 	icount->cts = cnow.cts;
1122*4882a593Smuzhiyun 	icount->dsr = cnow.dsr;
1123*4882a593Smuzhiyun 	icount->rng = cnow.rng;
1124*4882a593Smuzhiyun 	icount->dcd = cnow.dcd;
1125*4882a593Smuzhiyun 	icount->rx = cnow.rx;
1126*4882a593Smuzhiyun 	icount->tx = cnow.tx;
1127*4882a593Smuzhiyun 	icount->frame = cnow.frame;
1128*4882a593Smuzhiyun 	icount->overrun = cnow.overrun;
1129*4882a593Smuzhiyun 	icount->parity = cnow.parity;
1130*4882a593Smuzhiyun 	icount->brk = cnow.brk;
1131*4882a593Smuzhiyun 	icount->buf_overrun = cnow.buf_overrun;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	return 0;
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun 
rs_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)1136*4882a593Smuzhiyun static int rs_ioctl(struct tty_struct *tty,
1137*4882a593Smuzhiyun 		    unsigned int cmd, unsigned long arg)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
1140*4882a593Smuzhiyun 	struct async_icount cprev, cnow;	/* kernel counter temps */
1141*4882a593Smuzhiyun 	void __user *argp = (void __user *)arg;
1142*4882a593Smuzhiyun 	unsigned long flags;
1143*4882a593Smuzhiyun 	DEFINE_WAIT(wait);
1144*4882a593Smuzhiyun 	int ret;
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	if ((cmd != TIOCSERCONFIG) &&
1147*4882a593Smuzhiyun 	    (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1148*4882a593Smuzhiyun 		if (tty_io_error(tty))
1149*4882a593Smuzhiyun 		    return -EIO;
1150*4882a593Smuzhiyun 	}
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	switch (cmd) {
1153*4882a593Smuzhiyun 		case TIOCSERCONFIG:
1154*4882a593Smuzhiyun 			return 0;
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 		case TIOCSERGETLSR: /* Get line status register */
1157*4882a593Smuzhiyun 			return get_lsr_info(info, argp);
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 		/*
1160*4882a593Smuzhiyun 		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1161*4882a593Smuzhiyun 		 * - mask passed in arg for lines of interest
1162*4882a593Smuzhiyun  		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1163*4882a593Smuzhiyun 		 * Caller should use TIOCGICOUNT to see which one it was
1164*4882a593Smuzhiyun 		 */
1165*4882a593Smuzhiyun 		case TIOCMIWAIT:
1166*4882a593Smuzhiyun 			local_irq_save(flags);
1167*4882a593Smuzhiyun 			/* note the counters on entry */
1168*4882a593Smuzhiyun 			cprev = info->icount;
1169*4882a593Smuzhiyun 			local_irq_restore(flags);
1170*4882a593Smuzhiyun 			while (1) {
1171*4882a593Smuzhiyun 				prepare_to_wait(&info->tport.delta_msr_wait,
1172*4882a593Smuzhiyun 						&wait, TASK_INTERRUPTIBLE);
1173*4882a593Smuzhiyun 				local_irq_save(flags);
1174*4882a593Smuzhiyun 				cnow = info->icount; /* atomic copy */
1175*4882a593Smuzhiyun 				local_irq_restore(flags);
1176*4882a593Smuzhiyun 				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1177*4882a593Smuzhiyun 				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
1178*4882a593Smuzhiyun 					ret = -EIO; /* no change => error */
1179*4882a593Smuzhiyun 					break;
1180*4882a593Smuzhiyun 				}
1181*4882a593Smuzhiyun 				if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1182*4882a593Smuzhiyun 				     ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1183*4882a593Smuzhiyun 				     ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1184*4882a593Smuzhiyun 				     ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1185*4882a593Smuzhiyun 					ret = 0;
1186*4882a593Smuzhiyun 					break;
1187*4882a593Smuzhiyun 				}
1188*4882a593Smuzhiyun 				schedule();
1189*4882a593Smuzhiyun 				/* see if a signal did it */
1190*4882a593Smuzhiyun 				if (signal_pending(current)) {
1191*4882a593Smuzhiyun 					ret = -ERESTARTSYS;
1192*4882a593Smuzhiyun 					break;
1193*4882a593Smuzhiyun 				}
1194*4882a593Smuzhiyun 				cprev = cnow;
1195*4882a593Smuzhiyun 			}
1196*4882a593Smuzhiyun 			finish_wait(&info->tport.delta_msr_wait, &wait);
1197*4882a593Smuzhiyun 			return ret;
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 		default:
1200*4882a593Smuzhiyun 			return -ENOIOCTLCMD;
1201*4882a593Smuzhiyun 		}
1202*4882a593Smuzhiyun 	return 0;
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun 
rs_set_termios(struct tty_struct * tty,struct ktermios * old_termios)1205*4882a593Smuzhiyun static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1206*4882a593Smuzhiyun {
1207*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
1208*4882a593Smuzhiyun 	unsigned long flags;
1209*4882a593Smuzhiyun 	unsigned int cflag = tty->termios.c_cflag;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	change_speed(tty, info, old_termios);
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 	/* Handle transition to B0 status */
1214*4882a593Smuzhiyun 	if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
1215*4882a593Smuzhiyun 		info->MCR &= ~(SER_DTR|SER_RTS);
1216*4882a593Smuzhiyun 		local_irq_save(flags);
1217*4882a593Smuzhiyun 		rtsdtr_ctrl(info->MCR);
1218*4882a593Smuzhiyun 		local_irq_restore(flags);
1219*4882a593Smuzhiyun 	}
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun 	/* Handle transition away from B0 status */
1222*4882a593Smuzhiyun 	if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1223*4882a593Smuzhiyun 		info->MCR |= SER_DTR;
1224*4882a593Smuzhiyun 		if (!C_CRTSCTS(tty) || !tty_throttled(tty))
1225*4882a593Smuzhiyun 			info->MCR |= SER_RTS;
1226*4882a593Smuzhiyun 		local_irq_save(flags);
1227*4882a593Smuzhiyun 		rtsdtr_ctrl(info->MCR);
1228*4882a593Smuzhiyun 		local_irq_restore(flags);
1229*4882a593Smuzhiyun 	}
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 	/* Handle turning off CRTSCTS */
1232*4882a593Smuzhiyun 	if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
1233*4882a593Smuzhiyun 		tty->hw_stopped = 0;
1234*4882a593Smuzhiyun 		rs_start(tty);
1235*4882a593Smuzhiyun 	}
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun #if 0
1238*4882a593Smuzhiyun 	/*
1239*4882a593Smuzhiyun 	 * No need to wake up processes in open wait, since they
1240*4882a593Smuzhiyun 	 * sample the CLOCAL flag once, and don't recheck it.
1241*4882a593Smuzhiyun 	 * XXX  It's not clear whether the current behavior is correct
1242*4882a593Smuzhiyun 	 * or not.  Hence, this may change.....
1243*4882a593Smuzhiyun 	 */
1244*4882a593Smuzhiyun 	if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1245*4882a593Smuzhiyun 		wake_up_interruptible(&info->open_wait);
1246*4882a593Smuzhiyun #endif
1247*4882a593Smuzhiyun }
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun /*
1250*4882a593Smuzhiyun  * ------------------------------------------------------------
1251*4882a593Smuzhiyun  * rs_close()
1252*4882a593Smuzhiyun  *
1253*4882a593Smuzhiyun  * This routine is called when the serial port gets closed.  First, we
1254*4882a593Smuzhiyun  * wait for the last remaining data to be sent.  Then, we unlink its
1255*4882a593Smuzhiyun  * async structure from the interrupt chain if necessary, and we free
1256*4882a593Smuzhiyun  * that IRQ if nothing is left in the chain.
1257*4882a593Smuzhiyun  * ------------------------------------------------------------
1258*4882a593Smuzhiyun  */
rs_close(struct tty_struct * tty,struct file * filp)1259*4882a593Smuzhiyun static void rs_close(struct tty_struct *tty, struct file * filp)
1260*4882a593Smuzhiyun {
1261*4882a593Smuzhiyun 	struct serial_state *state = tty->driver_data;
1262*4882a593Smuzhiyun 	struct tty_port *port = &state->tport;
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 	if (tty_port_close_start(port, tty, filp) == 0)
1265*4882a593Smuzhiyun 		return;
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun 	/*
1268*4882a593Smuzhiyun 	 * At this point we stop accepting input.  To do this, we
1269*4882a593Smuzhiyun 	 * disable the receive line status interrupts, and tell the
1270*4882a593Smuzhiyun 	 * interrupt driver to stop checking the data ready bit in the
1271*4882a593Smuzhiyun 	 * line status register.
1272*4882a593Smuzhiyun 	 */
1273*4882a593Smuzhiyun 	state->read_status_mask &= ~UART_LSR_DR;
1274*4882a593Smuzhiyun 	if (tty_port_initialized(port)) {
1275*4882a593Smuzhiyun 	        /* disable receive interrupts */
1276*4882a593Smuzhiyun 	        custom.intena = IF_RBF;
1277*4882a593Smuzhiyun 		mb();
1278*4882a593Smuzhiyun 		/* clear any pending receive interrupt */
1279*4882a593Smuzhiyun 		custom.intreq = IF_RBF;
1280*4882a593Smuzhiyun 		mb();
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 		/*
1283*4882a593Smuzhiyun 		 * Before we drop DTR, make sure the UART transmitter
1284*4882a593Smuzhiyun 		 * has completely drained; this is especially
1285*4882a593Smuzhiyun 		 * important if there is a transmit FIFO!
1286*4882a593Smuzhiyun 		 */
1287*4882a593Smuzhiyun 		rs_wait_until_sent(tty, state->timeout);
1288*4882a593Smuzhiyun 	}
1289*4882a593Smuzhiyun 	shutdown(tty, state);
1290*4882a593Smuzhiyun 	rs_flush_buffer(tty);
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 	tty_ldisc_flush(tty);
1293*4882a593Smuzhiyun 	port->tty = NULL;
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	tty_port_close_end(port, tty);
1296*4882a593Smuzhiyun }
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun /*
1299*4882a593Smuzhiyun  * rs_wait_until_sent() --- wait until the transmitter is empty
1300*4882a593Smuzhiyun  */
rs_wait_until_sent(struct tty_struct * tty,int timeout)1301*4882a593Smuzhiyun static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1302*4882a593Smuzhiyun {
1303*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
1304*4882a593Smuzhiyun 	unsigned long orig_jiffies, char_time;
1305*4882a593Smuzhiyun 	int lsr;
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 	if (info->xmit_fifo_size == 0)
1308*4882a593Smuzhiyun 		return; /* Just in case.... */
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 	orig_jiffies = jiffies;
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	/*
1313*4882a593Smuzhiyun 	 * Set the check interval to be 1/5 of the estimated time to
1314*4882a593Smuzhiyun 	 * send a single character, and make it at least 1.  The check
1315*4882a593Smuzhiyun 	 * interval should also be less than the timeout.
1316*4882a593Smuzhiyun 	 *
1317*4882a593Smuzhiyun 	 * Note: we have to use pretty tight timings here to satisfy
1318*4882a593Smuzhiyun 	 * the NIST-PCTS.
1319*4882a593Smuzhiyun 	 */
1320*4882a593Smuzhiyun 	char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1321*4882a593Smuzhiyun 	char_time = char_time / 5;
1322*4882a593Smuzhiyun 	if (char_time == 0)
1323*4882a593Smuzhiyun 		char_time = 1;
1324*4882a593Smuzhiyun 	if (timeout)
1325*4882a593Smuzhiyun 	  char_time = min_t(unsigned long, char_time, timeout);
1326*4882a593Smuzhiyun 	/*
1327*4882a593Smuzhiyun 	 * If the transmitter hasn't cleared in twice the approximate
1328*4882a593Smuzhiyun 	 * amount of time to send the entire FIFO, it probably won't
1329*4882a593Smuzhiyun 	 * ever clear.  This assumes the UART isn't doing flow
1330*4882a593Smuzhiyun 	 * control, which is currently the case.  Hence, if it ever
1331*4882a593Smuzhiyun 	 * takes longer than info->timeout, this is probably due to a
1332*4882a593Smuzhiyun 	 * UART bug of some kind.  So, we clamp the timeout parameter at
1333*4882a593Smuzhiyun 	 * 2*info->timeout.
1334*4882a593Smuzhiyun 	 */
1335*4882a593Smuzhiyun 	if (!timeout || timeout > 2*info->timeout)
1336*4882a593Smuzhiyun 		timeout = 2*info->timeout;
1337*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1338*4882a593Smuzhiyun 	printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1339*4882a593Smuzhiyun 	printk("jiff=%lu...", jiffies);
1340*4882a593Smuzhiyun #endif
1341*4882a593Smuzhiyun 	while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1342*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1343*4882a593Smuzhiyun 		printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1344*4882a593Smuzhiyun #endif
1345*4882a593Smuzhiyun 		msleep_interruptible(jiffies_to_msecs(char_time));
1346*4882a593Smuzhiyun 		if (signal_pending(current))
1347*4882a593Smuzhiyun 			break;
1348*4882a593Smuzhiyun 		if (timeout && time_after(jiffies, orig_jiffies + timeout))
1349*4882a593Smuzhiyun 			break;
1350*4882a593Smuzhiyun 	}
1351*4882a593Smuzhiyun 	__set_current_state(TASK_RUNNING);
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1354*4882a593Smuzhiyun 	printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1355*4882a593Smuzhiyun #endif
1356*4882a593Smuzhiyun }
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun /*
1359*4882a593Smuzhiyun  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1360*4882a593Smuzhiyun  */
rs_hangup(struct tty_struct * tty)1361*4882a593Smuzhiyun static void rs_hangup(struct tty_struct *tty)
1362*4882a593Smuzhiyun {
1363*4882a593Smuzhiyun 	struct serial_state *info = tty->driver_data;
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	rs_flush_buffer(tty);
1366*4882a593Smuzhiyun 	shutdown(tty, info);
1367*4882a593Smuzhiyun 	info->tport.count = 0;
1368*4882a593Smuzhiyun 	tty_port_set_active(&info->tport, 0);
1369*4882a593Smuzhiyun 	info->tport.tty = NULL;
1370*4882a593Smuzhiyun 	wake_up_interruptible(&info->tport.open_wait);
1371*4882a593Smuzhiyun }
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun /*
1374*4882a593Smuzhiyun  * This routine is called whenever a serial port is opened.  It
1375*4882a593Smuzhiyun  * enables interrupts for a serial port, linking in its async structure into
1376*4882a593Smuzhiyun  * the IRQ chain.   It also performs the serial-specific
1377*4882a593Smuzhiyun  * initialization for the tty structure.
1378*4882a593Smuzhiyun  */
rs_open(struct tty_struct * tty,struct file * filp)1379*4882a593Smuzhiyun static int rs_open(struct tty_struct *tty, struct file * filp)
1380*4882a593Smuzhiyun {
1381*4882a593Smuzhiyun 	struct serial_state *info = rs_table + tty->index;
1382*4882a593Smuzhiyun 	struct tty_port *port = &info->tport;
1383*4882a593Smuzhiyun 	int retval;
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	port->count++;
1386*4882a593Smuzhiyun 	port->tty = tty;
1387*4882a593Smuzhiyun 	tty->driver_data = info;
1388*4882a593Smuzhiyun 	tty->port = port;
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun 	port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	retval = startup(tty, info);
1393*4882a593Smuzhiyun 	if (retval) {
1394*4882a593Smuzhiyun 		return retval;
1395*4882a593Smuzhiyun 	}
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 	return tty_port_block_til_ready(port, tty, filp);
1398*4882a593Smuzhiyun }
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun /*
1401*4882a593Smuzhiyun  * /proc fs routines....
1402*4882a593Smuzhiyun  */
1403*4882a593Smuzhiyun 
line_info(struct seq_file * m,int line,struct serial_state * state)1404*4882a593Smuzhiyun static inline void line_info(struct seq_file *m, int line,
1405*4882a593Smuzhiyun 		struct serial_state *state)
1406*4882a593Smuzhiyun {
1407*4882a593Smuzhiyun 	char	stat_buf[30], control, status;
1408*4882a593Smuzhiyun 	unsigned long flags;
1409*4882a593Smuzhiyun 
1410*4882a593Smuzhiyun 	seq_printf(m, "%d: uart:amiga_builtin", line);
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun 	local_irq_save(flags);
1413*4882a593Smuzhiyun 	status = ciab.pra;
1414*4882a593Smuzhiyun 	control = tty_port_initialized(&state->tport) ? state->MCR : status;
1415*4882a593Smuzhiyun 	local_irq_restore(flags);
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 	stat_buf[0] = 0;
1418*4882a593Smuzhiyun 	stat_buf[1] = 0;
1419*4882a593Smuzhiyun 	if(!(control & SER_RTS))
1420*4882a593Smuzhiyun 		strcat(stat_buf, "|RTS");
1421*4882a593Smuzhiyun 	if(!(status & SER_CTS))
1422*4882a593Smuzhiyun 		strcat(stat_buf, "|CTS");
1423*4882a593Smuzhiyun 	if(!(control & SER_DTR))
1424*4882a593Smuzhiyun 		strcat(stat_buf, "|DTR");
1425*4882a593Smuzhiyun 	if(!(status & SER_DSR))
1426*4882a593Smuzhiyun 		strcat(stat_buf, "|DSR");
1427*4882a593Smuzhiyun 	if(!(status & SER_DCD))
1428*4882a593Smuzhiyun 		strcat(stat_buf, "|CD");
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	if (state->quot)
1431*4882a593Smuzhiyun 		seq_printf(m, " baud:%d", state->baud_base / state->quot);
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun 	if (state->icount.frame)
1436*4882a593Smuzhiyun 		seq_printf(m, " fe:%d", state->icount.frame);
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	if (state->icount.parity)
1439*4882a593Smuzhiyun 		seq_printf(m, " pe:%d", state->icount.parity);
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 	if (state->icount.brk)
1442*4882a593Smuzhiyun 		seq_printf(m, " brk:%d", state->icount.brk);
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	if (state->icount.overrun)
1445*4882a593Smuzhiyun 		seq_printf(m, " oe:%d", state->icount.overrun);
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun 	/*
1448*4882a593Smuzhiyun 	 * Last thing is the RS-232 status lines
1449*4882a593Smuzhiyun 	 */
1450*4882a593Smuzhiyun 	seq_printf(m, " %s\n", stat_buf+1);
1451*4882a593Smuzhiyun }
1452*4882a593Smuzhiyun 
rs_proc_show(struct seq_file * m,void * v)1453*4882a593Smuzhiyun static int rs_proc_show(struct seq_file *m, void *v)
1454*4882a593Smuzhiyun {
1455*4882a593Smuzhiyun 	seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1456*4882a593Smuzhiyun 	line_info(m, 0, &rs_table[0]);
1457*4882a593Smuzhiyun 	return 0;
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun /*
1461*4882a593Smuzhiyun  * ---------------------------------------------------------------------
1462*4882a593Smuzhiyun  * rs_init() and friends
1463*4882a593Smuzhiyun  *
1464*4882a593Smuzhiyun  * rs_init() is called at boot-time to initialize the serial driver.
1465*4882a593Smuzhiyun  * ---------------------------------------------------------------------
1466*4882a593Smuzhiyun  */
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun /*
1469*4882a593Smuzhiyun  * This routine prints out the appropriate serial driver version
1470*4882a593Smuzhiyun  * number, and identifies which options were configured into this
1471*4882a593Smuzhiyun  * driver.
1472*4882a593Smuzhiyun  */
show_serial_version(void)1473*4882a593Smuzhiyun static void show_serial_version(void)
1474*4882a593Smuzhiyun {
1475*4882a593Smuzhiyun  	printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1476*4882a593Smuzhiyun }
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun static const struct tty_operations serial_ops = {
1480*4882a593Smuzhiyun 	.open = rs_open,
1481*4882a593Smuzhiyun 	.close = rs_close,
1482*4882a593Smuzhiyun 	.write = rs_write,
1483*4882a593Smuzhiyun 	.put_char = rs_put_char,
1484*4882a593Smuzhiyun 	.flush_chars = rs_flush_chars,
1485*4882a593Smuzhiyun 	.write_room = rs_write_room,
1486*4882a593Smuzhiyun 	.chars_in_buffer = rs_chars_in_buffer,
1487*4882a593Smuzhiyun 	.flush_buffer = rs_flush_buffer,
1488*4882a593Smuzhiyun 	.ioctl = rs_ioctl,
1489*4882a593Smuzhiyun 	.throttle = rs_throttle,
1490*4882a593Smuzhiyun 	.unthrottle = rs_unthrottle,
1491*4882a593Smuzhiyun 	.set_termios = rs_set_termios,
1492*4882a593Smuzhiyun 	.stop = rs_stop,
1493*4882a593Smuzhiyun 	.start = rs_start,
1494*4882a593Smuzhiyun 	.hangup = rs_hangup,
1495*4882a593Smuzhiyun 	.break_ctl = rs_break,
1496*4882a593Smuzhiyun 	.send_xchar = rs_send_xchar,
1497*4882a593Smuzhiyun 	.wait_until_sent = rs_wait_until_sent,
1498*4882a593Smuzhiyun 	.tiocmget = rs_tiocmget,
1499*4882a593Smuzhiyun 	.tiocmset = rs_tiocmset,
1500*4882a593Smuzhiyun 	.get_icount = rs_get_icount,
1501*4882a593Smuzhiyun 	.set_serial = set_serial_info,
1502*4882a593Smuzhiyun 	.get_serial = get_serial_info,
1503*4882a593Smuzhiyun 	.proc_show = rs_proc_show,
1504*4882a593Smuzhiyun };
1505*4882a593Smuzhiyun 
amiga_carrier_raised(struct tty_port * port)1506*4882a593Smuzhiyun static int amiga_carrier_raised(struct tty_port *port)
1507*4882a593Smuzhiyun {
1508*4882a593Smuzhiyun 	return !(ciab.pra & SER_DCD);
1509*4882a593Smuzhiyun }
1510*4882a593Smuzhiyun 
amiga_dtr_rts(struct tty_port * port,int raise)1511*4882a593Smuzhiyun static void amiga_dtr_rts(struct tty_port *port, int raise)
1512*4882a593Smuzhiyun {
1513*4882a593Smuzhiyun 	struct serial_state *info = container_of(port, struct serial_state,
1514*4882a593Smuzhiyun 			tport);
1515*4882a593Smuzhiyun 	unsigned long flags;
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	if (raise)
1518*4882a593Smuzhiyun 		info->MCR |= SER_DTR|SER_RTS;
1519*4882a593Smuzhiyun 	else
1520*4882a593Smuzhiyun 		info->MCR &= ~(SER_DTR|SER_RTS);
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun 	local_irq_save(flags);
1523*4882a593Smuzhiyun 	rtsdtr_ctrl(info->MCR);
1524*4882a593Smuzhiyun 	local_irq_restore(flags);
1525*4882a593Smuzhiyun }
1526*4882a593Smuzhiyun 
1527*4882a593Smuzhiyun static const struct tty_port_operations amiga_port_ops = {
1528*4882a593Smuzhiyun 	.carrier_raised = amiga_carrier_raised,
1529*4882a593Smuzhiyun 	.dtr_rts = amiga_dtr_rts,
1530*4882a593Smuzhiyun };
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun /*
1533*4882a593Smuzhiyun  * The serial driver boot-time initialization code!
1534*4882a593Smuzhiyun  */
amiga_serial_probe(struct platform_device * pdev)1535*4882a593Smuzhiyun static int __init amiga_serial_probe(struct platform_device *pdev)
1536*4882a593Smuzhiyun {
1537*4882a593Smuzhiyun 	unsigned long flags;
1538*4882a593Smuzhiyun 	struct serial_state * state;
1539*4882a593Smuzhiyun 	int error;
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	serial_driver = alloc_tty_driver(NR_PORTS);
1542*4882a593Smuzhiyun 	if (!serial_driver)
1543*4882a593Smuzhiyun 		return -ENOMEM;
1544*4882a593Smuzhiyun 
1545*4882a593Smuzhiyun 	show_serial_version();
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 	/* Initialize the tty_driver structure */
1548*4882a593Smuzhiyun 
1549*4882a593Smuzhiyun 	serial_driver->driver_name = "amiserial";
1550*4882a593Smuzhiyun 	serial_driver->name = "ttyS";
1551*4882a593Smuzhiyun 	serial_driver->major = TTY_MAJOR;
1552*4882a593Smuzhiyun 	serial_driver->minor_start = 64;
1553*4882a593Smuzhiyun 	serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1554*4882a593Smuzhiyun 	serial_driver->subtype = SERIAL_TYPE_NORMAL;
1555*4882a593Smuzhiyun 	serial_driver->init_termios = tty_std_termios;
1556*4882a593Smuzhiyun 	serial_driver->init_termios.c_cflag =
1557*4882a593Smuzhiyun 		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1558*4882a593Smuzhiyun 	serial_driver->flags = TTY_DRIVER_REAL_RAW;
1559*4882a593Smuzhiyun 	tty_set_operations(serial_driver, &serial_ops);
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun 	state = rs_table;
1562*4882a593Smuzhiyun 	state->port = (int)&custom.serdatr; /* Just to give it a value */
1563*4882a593Smuzhiyun 	state->custom_divisor = 0;
1564*4882a593Smuzhiyun 	state->icount.cts = state->icount.dsr =
1565*4882a593Smuzhiyun 	  state->icount.rng = state->icount.dcd = 0;
1566*4882a593Smuzhiyun 	state->icount.rx = state->icount.tx = 0;
1567*4882a593Smuzhiyun 	state->icount.frame = state->icount.parity = 0;
1568*4882a593Smuzhiyun 	state->icount.overrun = state->icount.brk = 0;
1569*4882a593Smuzhiyun 	tty_port_init(&state->tport);
1570*4882a593Smuzhiyun 	state->tport.ops = &amiga_port_ops;
1571*4882a593Smuzhiyun 	tty_port_link_device(&state->tport, serial_driver, 0);
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun 	error = tty_register_driver(serial_driver);
1574*4882a593Smuzhiyun 	if (error)
1575*4882a593Smuzhiyun 		goto fail_put_tty_driver;
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun 	printk(KERN_INFO "ttyS0 is the amiga builtin serial port\n");
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	/* Hardware set up */
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun 	state->baud_base = amiga_colorclock;
1582*4882a593Smuzhiyun 	state->xmit_fifo_size = 1;
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 	/* set ISRs, and then disable the rx interrupts */
1585*4882a593Smuzhiyun 	error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1586*4882a593Smuzhiyun 	if (error)
1587*4882a593Smuzhiyun 		goto fail_unregister;
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
1590*4882a593Smuzhiyun 			    "serial RX", state);
1591*4882a593Smuzhiyun 	if (error)
1592*4882a593Smuzhiyun 		goto fail_free_irq;
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun 	local_irq_save(flags);
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun 	/* turn off Rx and Tx interrupts */
1597*4882a593Smuzhiyun 	custom.intena = IF_RBF | IF_TBE;
1598*4882a593Smuzhiyun 	mb();
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun 	/* clear any pending interrupt */
1601*4882a593Smuzhiyun 	custom.intreq = IF_RBF | IF_TBE;
1602*4882a593Smuzhiyun 	mb();
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 	local_irq_restore(flags);
1605*4882a593Smuzhiyun 
1606*4882a593Smuzhiyun 	/*
1607*4882a593Smuzhiyun 	 * set the appropriate directions for the modem control flags,
1608*4882a593Smuzhiyun 	 * and clear RTS and DTR
1609*4882a593Smuzhiyun 	 */
1610*4882a593Smuzhiyun 	ciab.ddra |= (SER_DTR | SER_RTS);   /* outputs */
1611*4882a593Smuzhiyun 	ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR);  /* inputs */
1612*4882a593Smuzhiyun 
1613*4882a593Smuzhiyun 	platform_set_drvdata(pdev, state);
1614*4882a593Smuzhiyun 
1615*4882a593Smuzhiyun 	return 0;
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun fail_free_irq:
1618*4882a593Smuzhiyun 	free_irq(IRQ_AMIGA_TBE, state);
1619*4882a593Smuzhiyun fail_unregister:
1620*4882a593Smuzhiyun 	tty_unregister_driver(serial_driver);
1621*4882a593Smuzhiyun fail_put_tty_driver:
1622*4882a593Smuzhiyun 	tty_port_destroy(&state->tport);
1623*4882a593Smuzhiyun 	put_tty_driver(serial_driver);
1624*4882a593Smuzhiyun 	return error;
1625*4882a593Smuzhiyun }
1626*4882a593Smuzhiyun 
amiga_serial_remove(struct platform_device * pdev)1627*4882a593Smuzhiyun static int __exit amiga_serial_remove(struct platform_device *pdev)
1628*4882a593Smuzhiyun {
1629*4882a593Smuzhiyun 	int error;
1630*4882a593Smuzhiyun 	struct serial_state *state = platform_get_drvdata(pdev);
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun 	/* printk("Unloading %s: version %s\n", serial_name, serial_version); */
1633*4882a593Smuzhiyun 	error = tty_unregister_driver(serial_driver);
1634*4882a593Smuzhiyun 	if (error)
1635*4882a593Smuzhiyun 		printk("SERIAL: failed to unregister serial driver (%d)\n",
1636*4882a593Smuzhiyun 		       error);
1637*4882a593Smuzhiyun 	put_tty_driver(serial_driver);
1638*4882a593Smuzhiyun 	tty_port_destroy(&state->tport);
1639*4882a593Smuzhiyun 
1640*4882a593Smuzhiyun 	free_irq(IRQ_AMIGA_TBE, state);
1641*4882a593Smuzhiyun 	free_irq(IRQ_AMIGA_RBF, state);
1642*4882a593Smuzhiyun 
1643*4882a593Smuzhiyun 	return error;
1644*4882a593Smuzhiyun }
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun static struct platform_driver amiga_serial_driver = {
1647*4882a593Smuzhiyun 	.remove = __exit_p(amiga_serial_remove),
1648*4882a593Smuzhiyun 	.driver   = {
1649*4882a593Smuzhiyun 		.name	= "amiga-serial",
1650*4882a593Smuzhiyun 	},
1651*4882a593Smuzhiyun };
1652*4882a593Smuzhiyun 
1653*4882a593Smuzhiyun module_platform_driver_probe(amiga_serial_driver, amiga_serial_probe);
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun /*
1659*4882a593Smuzhiyun  * ------------------------------------------------------------
1660*4882a593Smuzhiyun  * Serial console driver
1661*4882a593Smuzhiyun  * ------------------------------------------------------------
1662*4882a593Smuzhiyun  */
1663*4882a593Smuzhiyun 
amiga_serial_putc(char c)1664*4882a593Smuzhiyun static void amiga_serial_putc(char c)
1665*4882a593Smuzhiyun {
1666*4882a593Smuzhiyun 	custom.serdat = (unsigned char)c | 0x100;
1667*4882a593Smuzhiyun 	while (!(custom.serdatr & 0x2000))
1668*4882a593Smuzhiyun 		barrier();
1669*4882a593Smuzhiyun }
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun /*
1672*4882a593Smuzhiyun  *	Print a string to the serial port trying not to disturb
1673*4882a593Smuzhiyun  *	any possible real use of the port...
1674*4882a593Smuzhiyun  *
1675*4882a593Smuzhiyun  *	The console must be locked when we get here.
1676*4882a593Smuzhiyun  */
serial_console_write(struct console * co,const char * s,unsigned count)1677*4882a593Smuzhiyun static void serial_console_write(struct console *co, const char *s,
1678*4882a593Smuzhiyun 				unsigned count)
1679*4882a593Smuzhiyun {
1680*4882a593Smuzhiyun 	unsigned short intena = custom.intenar;
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun 	custom.intena = IF_TBE;
1683*4882a593Smuzhiyun 
1684*4882a593Smuzhiyun 	while (count--) {
1685*4882a593Smuzhiyun 		if (*s == '\n')
1686*4882a593Smuzhiyun 			amiga_serial_putc('\r');
1687*4882a593Smuzhiyun 		amiga_serial_putc(*s++);
1688*4882a593Smuzhiyun 	}
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	custom.intena = IF_SETCLR | (intena & IF_TBE);
1691*4882a593Smuzhiyun }
1692*4882a593Smuzhiyun 
serial_console_device(struct console * c,int * index)1693*4882a593Smuzhiyun static struct tty_driver *serial_console_device(struct console *c, int *index)
1694*4882a593Smuzhiyun {
1695*4882a593Smuzhiyun 	*index = 0;
1696*4882a593Smuzhiyun 	return serial_driver;
1697*4882a593Smuzhiyun }
1698*4882a593Smuzhiyun 
1699*4882a593Smuzhiyun static struct console sercons = {
1700*4882a593Smuzhiyun 	.name =		"ttyS",
1701*4882a593Smuzhiyun 	.write =	serial_console_write,
1702*4882a593Smuzhiyun 	.device =	serial_console_device,
1703*4882a593Smuzhiyun 	.flags =	CON_PRINTBUFFER,
1704*4882a593Smuzhiyun 	.index =	-1,
1705*4882a593Smuzhiyun };
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun /*
1708*4882a593Smuzhiyun  *	Register console.
1709*4882a593Smuzhiyun  */
amiserial_console_init(void)1710*4882a593Smuzhiyun static int __init amiserial_console_init(void)
1711*4882a593Smuzhiyun {
1712*4882a593Smuzhiyun 	if (!MACH_IS_AMIGA)
1713*4882a593Smuzhiyun 		return -ENODEV;
1714*4882a593Smuzhiyun 
1715*4882a593Smuzhiyun 	register_console(&sercons);
1716*4882a593Smuzhiyun 	return 0;
1717*4882a593Smuzhiyun }
1718*4882a593Smuzhiyun console_initcall(amiserial_console_init);
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
1721*4882a593Smuzhiyun 
1722*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1723*4882a593Smuzhiyun MODULE_ALIAS("platform:amiga-serial");
1724