xref: /openbmc/linux/drivers/tty/amiserial.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a6afd9f3SGreg Kroah-Hartman /*
3a6afd9f3SGreg Kroah-Hartman  * Serial driver for the amiga builtin port.
4a6afd9f3SGreg Kroah-Hartman  *
5a6afd9f3SGreg Kroah-Hartman  * This code was created by taking serial.c version 4.30 from kernel
6a6afd9f3SGreg Kroah-Hartman  * release 2.3.22, replacing all hardware related stuff with the
7a6afd9f3SGreg Kroah-Hartman  * corresponding amiga hardware actions, and removing all irrelevant
8a6afd9f3SGreg Kroah-Hartman  * code. As a consequence, it uses many of the constants and names
9a6afd9f3SGreg Kroah-Hartman  * associated with the registers and bits of 16550 compatible UARTS -
10a6afd9f3SGreg Kroah-Hartman  * but only to keep track of status, etc in the state variables. It
11a6afd9f3SGreg Kroah-Hartman  * was done this was to make it easier to keep the code in line with
12a6afd9f3SGreg Kroah-Hartman  * (non hardware specific) changes to serial.c.
13a6afd9f3SGreg Kroah-Hartman  *
14a6afd9f3SGreg Kroah-Hartman  * The port is registered with the tty driver as minor device 64, and
15014482b6SJason Wang  * therefore other ports should only use 65 upwards.
16a6afd9f3SGreg Kroah-Hartman  *
17a6afd9f3SGreg Kroah-Hartman  * Richard Lucock 28/12/99
18a6afd9f3SGreg Kroah-Hartman  *
19a6afd9f3SGreg Kroah-Hartman  *  Copyright (C) 1991, 1992  Linus Torvalds
20a6afd9f3SGreg Kroah-Hartman  *  Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
21a6afd9f3SGreg Kroah-Hartman  * 		1998, 1999  Theodore Ts'o
22a6afd9f3SGreg Kroah-Hartman  *
23a6afd9f3SGreg Kroah-Hartman  */
24a6afd9f3SGreg Kroah-Hartman 
25a6afd9f3SGreg Kroah-Hartman /* Set of debugging defines */
26a6afd9f3SGreg Kroah-Hartman 
27a6afd9f3SGreg Kroah-Hartman #undef SERIAL_DEBUG_INTR
28a6afd9f3SGreg Kroah-Hartman #undef SERIAL_DEBUG_OPEN
29a6afd9f3SGreg Kroah-Hartman #undef SERIAL_DEBUG_FLOW
30a6afd9f3SGreg Kroah-Hartman #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
31a6afd9f3SGreg Kroah-Hartman 
32a6afd9f3SGreg Kroah-Hartman /*
33a6afd9f3SGreg Kroah-Hartman  * End of serial driver configuration section.
34a6afd9f3SGreg Kroah-Hartman  */
35a6afd9f3SGreg Kroah-Hartman 
36f3d788b4SJiri Slaby #include <linux/bitops.h>
376fe18d26SJiri Slaby #include <linux/circ_buf.h>
38a6afd9f3SGreg Kroah-Hartman #include <linux/console.h>
39f3d788b4SJiri Slaby #include <linux/delay.h>
40f3d788b4SJiri Slaby #include <linux/errno.h>
41a6afd9f3SGreg Kroah-Hartman #include <linux/fcntl.h>
42a6afd9f3SGreg Kroah-Hartman #include <linux/init.h>
43f3d788b4SJiri Slaby #include <linux/interrupt.h>
44f3d788b4SJiri Slaby #include <linux/ioport.h>
45f3d788b4SJiri Slaby #include <linux/kernel.h>
46f3d788b4SJiri Slaby #include <linux/major.h>
47f3d788b4SJiri Slaby #include <linux/mm.h>
48f3d788b4SJiri Slaby #include <linux/module.h>
49a6afd9f3SGreg Kroah-Hartman #include <linux/platform_device.h>
50f3d788b4SJiri Slaby #include <linux/ptrace.h>
51f3d788b4SJiri Slaby #include <linux/seq_file.h>
52f3d788b4SJiri Slaby #include <linux/serial.h>
53f3d788b4SJiri Slaby #include <linux/serial_reg.h>
54eb47b59aSIlpo Järvinen #include <linux/serial_core.h>
55f3d788b4SJiri Slaby #include <linux/sched.h>
56f3d788b4SJiri Slaby #include <linux/signal.h>
57f3d788b4SJiri Slaby #include <linux/slab.h>
58f3d788b4SJiri Slaby #include <linux/string.h>
59f3d788b4SJiri Slaby #include <linux/timer.h>
60f3d788b4SJiri Slaby #include <linux/tty_flip.h>
61f3d788b4SJiri Slaby #include <linux/tty.h>
62f3d788b4SJiri Slaby #include <linux/types.h>
63f3d788b4SJiri Slaby #include <linux/uaccess.h>
64a6afd9f3SGreg Kroah-Hartman 
65a6afd9f3SGreg Kroah-Hartman #include <asm/amigahw.h>
66a6afd9f3SGreg Kroah-Hartman #include <asm/amigaints.h>
67f3d788b4SJiri Slaby #include <asm/irq.h>
68f3d788b4SJiri Slaby #include <asm/setup.h>
69a6afd9f3SGreg Kroah-Hartman 
706fe18d26SJiri Slaby struct serial_state {
716fe18d26SJiri Slaby 	struct tty_port		tport;
726fe18d26SJiri Slaby 	struct circ_buf		xmit;
736fe18d26SJiri Slaby 	struct async_icount	icount;
746fe18d26SJiri Slaby 
756fe18d26SJiri Slaby 	unsigned long		port;
766fe18d26SJiri Slaby 	int			baud_base;
776fe18d26SJiri Slaby 	int			custom_divisor;
786fe18d26SJiri Slaby 	int			read_status_mask;
796fe18d26SJiri Slaby 	int			ignore_status_mask;
806fe18d26SJiri Slaby 	int			timeout;
816fe18d26SJiri Slaby 	int			quot;
826fe18d26SJiri Slaby 	int			IER; 	/* Interrupt Enable Register */
836fe18d26SJiri Slaby 	int			MCR; 	/* Modem control register */
846fe18d26SJiri Slaby 	int			x_char;	/* xon/xoff character */
856fe18d26SJiri Slaby };
866fe18d26SJiri Slaby 
87a6afd9f3SGreg Kroah-Hartman static struct tty_driver *serial_driver;
88a6afd9f3SGreg Kroah-Hartman 
89a6afd9f3SGreg Kroah-Hartman /* number of characters left in xmit buffer before we ask for more */
90a6afd9f3SGreg Kroah-Hartman #define WAKEUP_CHARS 256
91a6afd9f3SGreg Kroah-Hartman 
925a7c7a6bSJiri Slaby #define XMIT_FIFO_SIZE 1
935a7c7a6bSJiri Slaby 
94a6afd9f3SGreg Kroah-Hartman static unsigned char current_ctl_bits;
95a6afd9f3SGreg Kroah-Hartman 
96588993ddSJiri Slaby static void change_speed(struct tty_struct *tty, struct serial_state *info,
97a8c11c15SIlpo Järvinen 			 const struct ktermios *old);
98a6afd9f3SGreg Kroah-Hartman static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
99a6afd9f3SGreg Kroah-Hartman 
100a6afd9f3SGreg Kroah-Hartman 
1016cc7bda1SJiri Slaby static struct serial_state serial_state;
102a6afd9f3SGreg Kroah-Hartman 
103a6afd9f3SGreg Kroah-Hartman /* some serial hardware definitions */
104a6afd9f3SGreg Kroah-Hartman #define SDR_OVRUN   (1<<15)
105a6afd9f3SGreg Kroah-Hartman #define SDR_RBF     (1<<14)
106a6afd9f3SGreg Kroah-Hartman #define SDR_TBE     (1<<13)
107a6afd9f3SGreg Kroah-Hartman #define SDR_TSRE    (1<<12)
108a6afd9f3SGreg Kroah-Hartman 
109a6afd9f3SGreg Kroah-Hartman #define SERPER_PARENB    (1<<15)
110a6afd9f3SGreg Kroah-Hartman 
111a6afd9f3SGreg Kroah-Hartman #define AC_SETCLR   (1<<15)
112a6afd9f3SGreg Kroah-Hartman #define AC_UARTBRK  (1<<11)
113a6afd9f3SGreg Kroah-Hartman 
114a6afd9f3SGreg Kroah-Hartman #define SER_DTR     (1<<7)
115a6afd9f3SGreg Kroah-Hartman #define SER_RTS     (1<<6)
116a6afd9f3SGreg Kroah-Hartman #define SER_DCD     (1<<5)
117a6afd9f3SGreg Kroah-Hartman #define SER_CTS     (1<<4)
118a6afd9f3SGreg Kroah-Hartman #define SER_DSR     (1<<3)
119a6afd9f3SGreg Kroah-Hartman 
rtsdtr_ctrl(int bits)120a6afd9f3SGreg Kroah-Hartman static __inline__ void rtsdtr_ctrl(int bits)
121a6afd9f3SGreg Kroah-Hartman {
122a6afd9f3SGreg Kroah-Hartman     ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
123a6afd9f3SGreg Kroah-Hartman }
124a6afd9f3SGreg Kroah-Hartman 
125a6afd9f3SGreg Kroah-Hartman /*
126a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
127a6afd9f3SGreg Kroah-Hartman  * rs_stop() and rs_start()
128a6afd9f3SGreg Kroah-Hartman  *
1296e94dbc7SJiri Slaby  * This routines are called before setting or resetting tty->flow.stopped.
130a6afd9f3SGreg Kroah-Hartman  * They enable or disable transmitter interrupts, as necessary.
131a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
132a6afd9f3SGreg Kroah-Hartman  */
rs_stop(struct tty_struct * tty)133a6afd9f3SGreg Kroah-Hartman static void rs_stop(struct tty_struct *tty)
134a6afd9f3SGreg Kroah-Hartman {
135916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
136a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
137a6afd9f3SGreg Kroah-Hartman 
138a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
139a6afd9f3SGreg Kroah-Hartman 	if (info->IER & UART_IER_THRI) {
140a6afd9f3SGreg Kroah-Hartman 		info->IER &= ~UART_IER_THRI;
141a6afd9f3SGreg Kroah-Hartman 		/* disable Tx interrupt and remove any pending interrupts */
14281680702SJiri Slaby 		amiga_custom.intena = IF_TBE;
143a6afd9f3SGreg Kroah-Hartman 		mb();
14481680702SJiri Slaby 		amiga_custom.intreq = IF_TBE;
145a6afd9f3SGreg Kroah-Hartman 		mb();
146a6afd9f3SGreg Kroah-Hartman 	}
147a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
148a6afd9f3SGreg Kroah-Hartman }
149a6afd9f3SGreg Kroah-Hartman 
rs_start(struct tty_struct * tty)150a6afd9f3SGreg Kroah-Hartman static void rs_start(struct tty_struct *tty)
151a6afd9f3SGreg Kroah-Hartman {
152916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
153a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
154a6afd9f3SGreg Kroah-Hartman 
155a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
156a6afd9f3SGreg Kroah-Hartman 	if (info->xmit.head != info->xmit.tail
157a6afd9f3SGreg Kroah-Hartman 	    && info->xmit.buf
158a6afd9f3SGreg Kroah-Hartman 	    && !(info->IER & UART_IER_THRI)) {
159a6afd9f3SGreg Kroah-Hartman 		info->IER |= UART_IER_THRI;
16081680702SJiri Slaby 		amiga_custom.intena = IF_SETCLR | IF_TBE;
161a6afd9f3SGreg Kroah-Hartman 		mb();
162a6afd9f3SGreg Kroah-Hartman 		/* set a pending Tx Interrupt, transmitter should restart now */
16381680702SJiri Slaby 		amiga_custom.intreq = IF_SETCLR | IF_TBE;
164a6afd9f3SGreg Kroah-Hartman 		mb();
165a6afd9f3SGreg Kroah-Hartman 	}
166a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
167a6afd9f3SGreg Kroah-Hartman }
168a6afd9f3SGreg Kroah-Hartman 
169a6afd9f3SGreg Kroah-Hartman /*
170a6afd9f3SGreg Kroah-Hartman  * ----------------------------------------------------------------------
171a6afd9f3SGreg Kroah-Hartman  *
172b4420693SJiri Slaby  * Here start the interrupt handling routines.
173a6afd9f3SGreg Kroah-Hartman  *
174a6afd9f3SGreg Kroah-Hartman  * -----------------------------------------------------------------------
175a6afd9f3SGreg Kroah-Hartman  */
176a6afd9f3SGreg Kroah-Hartman 
receive_chars(struct serial_state * info)177916b7656SJiri Slaby static void receive_chars(struct serial_state *info)
178a6afd9f3SGreg Kroah-Hartman {
179a6afd9f3SGreg Kroah-Hartman         int status;
180a6afd9f3SGreg Kroah-Hartman 	int serdatr;
181a6afd9f3SGreg Kroah-Hartman 	unsigned char ch, flag;
182a6afd9f3SGreg Kroah-Hartman 	struct	async_icount *icount;
183a6afd9f3SGreg Kroah-Hartman 	int oe = 0;
184a6afd9f3SGreg Kroah-Hartman 
185916b7656SJiri Slaby 	icount = &info->icount;
186a6afd9f3SGreg Kroah-Hartman 
187a6afd9f3SGreg Kroah-Hartman 	status = UART_LSR_DR; /* We obviously have a character! */
18881680702SJiri Slaby 	serdatr = amiga_custom.serdatr;
189a6afd9f3SGreg Kroah-Hartman 	mb();
19081680702SJiri Slaby 	amiga_custom.intreq = IF_RBF;
191a6afd9f3SGreg Kroah-Hartman 	mb();
192a6afd9f3SGreg Kroah-Hartman 
193a6afd9f3SGreg Kroah-Hartman 	if((serdatr & 0x1ff) == 0)
194a6afd9f3SGreg Kroah-Hartman 	    status |= UART_LSR_BI;
195a6afd9f3SGreg Kroah-Hartman 	if(serdatr & SDR_OVRUN)
196a6afd9f3SGreg Kroah-Hartman 	    status |= UART_LSR_OE;
197a6afd9f3SGreg Kroah-Hartman 
198a6afd9f3SGreg Kroah-Hartman 	ch = serdatr & 0xff;
199a6afd9f3SGreg Kroah-Hartman 	icount->rx++;
200a6afd9f3SGreg Kroah-Hartman 
201a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_INTR
202a6afd9f3SGreg Kroah-Hartman 	printk("DR%02x:%02x...", ch, status);
203a6afd9f3SGreg Kroah-Hartman #endif
204a6afd9f3SGreg Kroah-Hartman 	flag = TTY_NORMAL;
205a6afd9f3SGreg Kroah-Hartman 
206a6afd9f3SGreg Kroah-Hartman 	/*
207a6afd9f3SGreg Kroah-Hartman 	 * We don't handle parity or frame errors - but I have left
208a6afd9f3SGreg Kroah-Hartman 	 * the code in, since I'm not sure that the errors can't be
209a6afd9f3SGreg Kroah-Hartman 	 * detected.
210a6afd9f3SGreg Kroah-Hartman 	 */
211a6afd9f3SGreg Kroah-Hartman 
212a6afd9f3SGreg Kroah-Hartman 	if (status & (UART_LSR_BI | UART_LSR_PE |
213a6afd9f3SGreg Kroah-Hartman 		      UART_LSR_FE | UART_LSR_OE)) {
214a6afd9f3SGreg Kroah-Hartman 	  /*
215a6afd9f3SGreg Kroah-Hartman 	   * For statistics only
216a6afd9f3SGreg Kroah-Hartman 	   */
217a6afd9f3SGreg Kroah-Hartman 	  if (status & UART_LSR_BI) {
218a6afd9f3SGreg Kroah-Hartman 	    status &= ~(UART_LSR_FE | UART_LSR_PE);
219a6afd9f3SGreg Kroah-Hartman 	    icount->brk++;
220a6afd9f3SGreg Kroah-Hartman 	  } else if (status & UART_LSR_PE)
221a6afd9f3SGreg Kroah-Hartman 	    icount->parity++;
222a6afd9f3SGreg Kroah-Hartman 	  else if (status & UART_LSR_FE)
223a6afd9f3SGreg Kroah-Hartman 	    icount->frame++;
224a6afd9f3SGreg Kroah-Hartman 	  if (status & UART_LSR_OE)
225a6afd9f3SGreg Kroah-Hartman 	    icount->overrun++;
226a6afd9f3SGreg Kroah-Hartman 
227a6afd9f3SGreg Kroah-Hartman 	  /*
228a6afd9f3SGreg Kroah-Hartman 	   * Now check to see if character should be
229a6afd9f3SGreg Kroah-Hartman 	   * ignored, and mask off conditions which
230a6afd9f3SGreg Kroah-Hartman 	   * should be ignored.
231a6afd9f3SGreg Kroah-Hartman 	   */
232a6afd9f3SGreg Kroah-Hartman 	  if (status & info->ignore_status_mask)
233a6afd9f3SGreg Kroah-Hartman 	    goto out;
234a6afd9f3SGreg Kroah-Hartman 
235a6afd9f3SGreg Kroah-Hartman 	  status &= info->read_status_mask;
236a6afd9f3SGreg Kroah-Hartman 
237a6afd9f3SGreg Kroah-Hartman 	  if (status & (UART_LSR_BI)) {
238a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_INTR
239a6afd9f3SGreg Kroah-Hartman 	    printk("handling break....");
240a6afd9f3SGreg Kroah-Hartman #endif
241a6afd9f3SGreg Kroah-Hartman 	    flag = TTY_BREAK;
24201bd730dSJiri Slaby 	    if (info->tport.flags & ASYNC_SAK)
2432e124b4aSJiri Slaby 	      do_SAK(info->tport.tty);
244a6afd9f3SGreg Kroah-Hartman 	  } else if (status & UART_LSR_PE)
245a6afd9f3SGreg Kroah-Hartman 	    flag = TTY_PARITY;
246a6afd9f3SGreg Kroah-Hartman 	  else if (status & UART_LSR_FE)
247a6afd9f3SGreg Kroah-Hartman 	    flag = TTY_FRAME;
248a6afd9f3SGreg Kroah-Hartman 	  if (status & UART_LSR_OE) {
249a6afd9f3SGreg Kroah-Hartman 	    /*
250a6afd9f3SGreg Kroah-Hartman 	     * Overrun is special, since it's
251a6afd9f3SGreg Kroah-Hartman 	     * reported immediately, and doesn't
252a6afd9f3SGreg Kroah-Hartman 	     * affect the current character
253a6afd9f3SGreg Kroah-Hartman 	     */
254a6afd9f3SGreg Kroah-Hartman 	     oe = 1;
255a6afd9f3SGreg Kroah-Hartman 	  }
256a6afd9f3SGreg Kroah-Hartman 	}
25792a19f9cSJiri Slaby 	tty_insert_flip_char(&info->tport, ch, flag);
258a6afd9f3SGreg Kroah-Hartman 	if (oe == 1)
25992a19f9cSJiri Slaby 		tty_insert_flip_char(&info->tport, 0, TTY_OVERRUN);
2602e124b4aSJiri Slaby 	tty_flip_buffer_push(&info->tport);
261a6afd9f3SGreg Kroah-Hartman out:
262a6afd9f3SGreg Kroah-Hartman 	return;
263a6afd9f3SGreg Kroah-Hartman }
264a6afd9f3SGreg Kroah-Hartman 
transmit_chars(struct serial_state * info)265916b7656SJiri Slaby static void transmit_chars(struct serial_state *info)
266a6afd9f3SGreg Kroah-Hartman {
26781680702SJiri Slaby 	amiga_custom.intreq = IF_TBE;
268a6afd9f3SGreg Kroah-Hartman 	mb();
269a6afd9f3SGreg Kroah-Hartman 	if (info->x_char) {
27081680702SJiri Slaby 	        amiga_custom.serdat = info->x_char | 0x100;
271a6afd9f3SGreg Kroah-Hartman 		mb();
272916b7656SJiri Slaby 		info->icount.tx++;
273a6afd9f3SGreg Kroah-Hartman 		info->x_char = 0;
274a6afd9f3SGreg Kroah-Hartman 		return;
275a6afd9f3SGreg Kroah-Hartman 	}
276a6afd9f3SGreg Kroah-Hartman 	if (info->xmit.head == info->xmit.tail
2776e94dbc7SJiri Slaby 	    || info->tport.tty->flow.stopped
27887758791SJiri Slaby 	    || info->tport.tty->hw_stopped) {
279a6afd9f3SGreg Kroah-Hartman 		info->IER &= ~UART_IER_THRI;
28081680702SJiri Slaby 	        amiga_custom.intena = IF_TBE;
281a6afd9f3SGreg Kroah-Hartman 		mb();
282a6afd9f3SGreg Kroah-Hartman 		return;
283a6afd9f3SGreg Kroah-Hartman 	}
284a6afd9f3SGreg Kroah-Hartman 
28581680702SJiri Slaby 	amiga_custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
286a6afd9f3SGreg Kroah-Hartman 	mb();
287eb47b59aSIlpo Järvinen 	info->xmit.tail = info->xmit.tail & (UART_XMIT_SIZE - 1);
288916b7656SJiri Slaby 	info->icount.tx++;
289a6afd9f3SGreg Kroah-Hartman 
290a6afd9f3SGreg Kroah-Hartman 	if (CIRC_CNT(info->xmit.head,
291a6afd9f3SGreg Kroah-Hartman 		     info->xmit.tail,
292eb47b59aSIlpo Järvinen 		     UART_XMIT_SIZE) < WAKEUP_CHARS)
29387758791SJiri Slaby 		tty_wakeup(info->tport.tty);
294a6afd9f3SGreg Kroah-Hartman 
295a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_INTR
296a6afd9f3SGreg Kroah-Hartman 	printk("THRE...");
297a6afd9f3SGreg Kroah-Hartman #endif
298a6afd9f3SGreg Kroah-Hartman 	if (info->xmit.head == info->xmit.tail) {
29981680702SJiri Slaby 	        amiga_custom.intena = IF_TBE;
300a6afd9f3SGreg Kroah-Hartman 		mb();
301a6afd9f3SGreg Kroah-Hartman 		info->IER &= ~UART_IER_THRI;
302a6afd9f3SGreg Kroah-Hartman 	}
303a6afd9f3SGreg Kroah-Hartman }
304a6afd9f3SGreg Kroah-Hartman 
check_modem_status(struct serial_state * info)305916b7656SJiri Slaby static void check_modem_status(struct serial_state *info)
306a6afd9f3SGreg Kroah-Hartman {
3077188dc20SJiri Slaby 	struct tty_port *port = &info->tport;
308a6afd9f3SGreg Kroah-Hartman 	unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
309a6afd9f3SGreg Kroah-Hartman 	unsigned char dstatus;
310a6afd9f3SGreg Kroah-Hartman 	struct	async_icount *icount;
311a6afd9f3SGreg Kroah-Hartman 
312a6afd9f3SGreg Kroah-Hartman 	/* Determine bits that have changed */
313a6afd9f3SGreg Kroah-Hartman 	dstatus = status ^ current_ctl_bits;
314a6afd9f3SGreg Kroah-Hartman 	current_ctl_bits = status;
315a6afd9f3SGreg Kroah-Hartman 
316a6afd9f3SGreg Kroah-Hartman 	if (dstatus) {
317916b7656SJiri Slaby 		icount = &info->icount;
318a6afd9f3SGreg Kroah-Hartman 		/* update input line counters */
319a6afd9f3SGreg Kroah-Hartman 		if (dstatus & SER_DSR)
320a6afd9f3SGreg Kroah-Hartman 			icount->dsr++;
321a6afd9f3SGreg Kroah-Hartman 		if (dstatus & SER_DCD) {
322a6afd9f3SGreg Kroah-Hartman 			icount->dcd++;
323a6afd9f3SGreg Kroah-Hartman 		}
324a6afd9f3SGreg Kroah-Hartman 		if (dstatus & SER_CTS)
325a6afd9f3SGreg Kroah-Hartman 			icount->cts++;
3267188dc20SJiri Slaby 		wake_up_interruptible(&port->delta_msr_wait);
327a6afd9f3SGreg Kroah-Hartman 	}
328a6afd9f3SGreg Kroah-Hartman 
3292d68655dSPeter Hurley 	if (tty_port_check_carrier(port) && (dstatus & SER_DCD)) {
330a6afd9f3SGreg Kroah-Hartman #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
331a6afd9f3SGreg Kroah-Hartman 		printk("ttyS%d CD now %s...", info->line,
332a6afd9f3SGreg Kroah-Hartman 		       (!(status & SER_DCD)) ? "on" : "off");
333a6afd9f3SGreg Kroah-Hartman #endif
334a6afd9f3SGreg Kroah-Hartman 		if (!(status & SER_DCD))
3357188dc20SJiri Slaby 			wake_up_interruptible(&port->open_wait);
336a6afd9f3SGreg Kroah-Hartman 		else {
337a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_OPEN
338a6afd9f3SGreg Kroah-Hartman 			printk("doing serial hangup...");
339a6afd9f3SGreg Kroah-Hartman #endif
3407188dc20SJiri Slaby 			if (port->tty)
3417188dc20SJiri Slaby 				tty_hangup(port->tty);
342a6afd9f3SGreg Kroah-Hartman 		}
343a6afd9f3SGreg Kroah-Hartman 	}
344f21ec3d2SHuang Shijie 	if (tty_port_cts_enabled(port)) {
3457188dc20SJiri Slaby 		if (port->tty->hw_stopped) {
346a6afd9f3SGreg Kroah-Hartman 			if (!(status & SER_CTS)) {
347a6afd9f3SGreg Kroah-Hartman #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
348a6afd9f3SGreg Kroah-Hartman 				printk("CTS tx start...");
349a6afd9f3SGreg Kroah-Hartman #endif
350035173c9SIlpo Järvinen 				port->tty->hw_stopped = false;
351a6afd9f3SGreg Kroah-Hartman 				info->IER |= UART_IER_THRI;
35281680702SJiri Slaby 				amiga_custom.intena = IF_SETCLR | IF_TBE;
353a6afd9f3SGreg Kroah-Hartman 				mb();
354a6afd9f3SGreg Kroah-Hartman 				/* set a pending Tx Interrupt, transmitter should restart now */
35581680702SJiri Slaby 				amiga_custom.intreq = IF_SETCLR | IF_TBE;
356a6afd9f3SGreg Kroah-Hartman 				mb();
3577188dc20SJiri Slaby 				tty_wakeup(port->tty);
358a6afd9f3SGreg Kroah-Hartman 				return;
359a6afd9f3SGreg Kroah-Hartman 			}
360a6afd9f3SGreg Kroah-Hartman 		} else {
361a6afd9f3SGreg Kroah-Hartman 			if ((status & SER_CTS)) {
362a6afd9f3SGreg Kroah-Hartman #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
363a6afd9f3SGreg Kroah-Hartman 				printk("CTS tx stop...");
364a6afd9f3SGreg Kroah-Hartman #endif
365035173c9SIlpo Järvinen 				port->tty->hw_stopped = true;
366a6afd9f3SGreg Kroah-Hartman 				info->IER &= ~UART_IER_THRI;
367a6afd9f3SGreg Kroah-Hartman 				/* disable Tx interrupt and remove any pending interrupts */
36881680702SJiri Slaby 				amiga_custom.intena = IF_TBE;
369a6afd9f3SGreg Kroah-Hartman 				mb();
37081680702SJiri Slaby 				amiga_custom.intreq = IF_TBE;
371a6afd9f3SGreg Kroah-Hartman 				mb();
372a6afd9f3SGreg Kroah-Hartman 			}
373a6afd9f3SGreg Kroah-Hartman 		}
374a6afd9f3SGreg Kroah-Hartman 	}
375a6afd9f3SGreg Kroah-Hartman }
376a6afd9f3SGreg Kroah-Hartman 
ser_vbl_int(int irq,void * data)377a6afd9f3SGreg Kroah-Hartman static irqreturn_t ser_vbl_int( int irq, void *data)
378a6afd9f3SGreg Kroah-Hartman {
379a6afd9f3SGreg Kroah-Hartman         /* vbl is just a periodic interrupt we tie into to update modem status */
380916b7656SJiri Slaby 	struct serial_state *info = data;
381a6afd9f3SGreg Kroah-Hartman 	/*
382a6afd9f3SGreg Kroah-Hartman 	 * TBD - is it better to unregister from this interrupt or to
383a6afd9f3SGreg Kroah-Hartman 	 * ignore it if MSI is clear ?
384a6afd9f3SGreg Kroah-Hartman 	 */
385a6afd9f3SGreg Kroah-Hartman 	if(info->IER & UART_IER_MSI)
386a6afd9f3SGreg Kroah-Hartman 	  check_modem_status(info);
387a6afd9f3SGreg Kroah-Hartman 	return IRQ_HANDLED;
388a6afd9f3SGreg Kroah-Hartman }
389a6afd9f3SGreg Kroah-Hartman 
ser_rx_int(int irq,void * dev_id)390a6afd9f3SGreg Kroah-Hartman static irqreturn_t ser_rx_int(int irq, void *dev_id)
391a6afd9f3SGreg Kroah-Hartman {
392916b7656SJiri Slaby 	struct serial_state *info = dev_id;
393a6afd9f3SGreg Kroah-Hartman 
394a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_INTR
395a6afd9f3SGreg Kroah-Hartman 	printk("ser_rx_int...");
396a6afd9f3SGreg Kroah-Hartman #endif
397a6afd9f3SGreg Kroah-Hartman 
39887758791SJiri Slaby 	if (!info->tport.tty)
399a6afd9f3SGreg Kroah-Hartman 		return IRQ_NONE;
400a6afd9f3SGreg Kroah-Hartman 
401a6afd9f3SGreg Kroah-Hartman 	receive_chars(info);
402a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_INTR
403a6afd9f3SGreg Kroah-Hartman 	printk("end.\n");
404a6afd9f3SGreg Kroah-Hartman #endif
405a6afd9f3SGreg Kroah-Hartman 	return IRQ_HANDLED;
406a6afd9f3SGreg Kroah-Hartman }
407a6afd9f3SGreg Kroah-Hartman 
ser_tx_int(int irq,void * dev_id)408a6afd9f3SGreg Kroah-Hartman static irqreturn_t ser_tx_int(int irq, void *dev_id)
409a6afd9f3SGreg Kroah-Hartman {
410916b7656SJiri Slaby 	struct serial_state *info = dev_id;
411a6afd9f3SGreg Kroah-Hartman 
41281680702SJiri Slaby 	if (amiga_custom.serdatr & SDR_TBE) {
413a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_INTR
414a6afd9f3SGreg Kroah-Hartman 	  printk("ser_tx_int...");
415a6afd9f3SGreg Kroah-Hartman #endif
416a6afd9f3SGreg Kroah-Hartman 
41787758791SJiri Slaby 	  if (!info->tport.tty)
418a6afd9f3SGreg Kroah-Hartman 		return IRQ_NONE;
419a6afd9f3SGreg Kroah-Hartman 
420a6afd9f3SGreg Kroah-Hartman 	  transmit_chars(info);
421a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_INTR
422a6afd9f3SGreg Kroah-Hartman 	  printk("end.\n");
423a6afd9f3SGreg Kroah-Hartman #endif
424a6afd9f3SGreg Kroah-Hartman 	}
425a6afd9f3SGreg Kroah-Hartman 	return IRQ_HANDLED;
426a6afd9f3SGreg Kroah-Hartman }
427a6afd9f3SGreg Kroah-Hartman 
428a6afd9f3SGreg Kroah-Hartman /*
429a6afd9f3SGreg Kroah-Hartman  * -------------------------------------------------------------------
430a6afd9f3SGreg Kroah-Hartman  * Here ends the serial interrupt routines.
431a6afd9f3SGreg Kroah-Hartman  * -------------------------------------------------------------------
432a6afd9f3SGreg Kroah-Hartman  */
433a6afd9f3SGreg Kroah-Hartman 
434a6afd9f3SGreg Kroah-Hartman /*
435a6afd9f3SGreg Kroah-Hartman  * ---------------------------------------------------------------
436a6afd9f3SGreg Kroah-Hartman  * Low level utility subroutines for the serial driver:  routines to
437a6afd9f3SGreg Kroah-Hartman  * figure out the appropriate timeout for an interrupt chain, routines
438a6afd9f3SGreg Kroah-Hartman  * to initialize and startup a serial port, and routines to shutdown a
439a6afd9f3SGreg Kroah-Hartman  * serial port.  Useful stuff like that.
440a6afd9f3SGreg Kroah-Hartman  * ---------------------------------------------------------------
441a6afd9f3SGreg Kroah-Hartman  */
442a6afd9f3SGreg Kroah-Hartman 
startup(struct tty_struct * tty,struct serial_state * info)443588993ddSJiri Slaby static int startup(struct tty_struct *tty, struct serial_state *info)
444a6afd9f3SGreg Kroah-Hartman {
44501bd730dSJiri Slaby 	struct tty_port *port = &info->tport;
446a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
447a6afd9f3SGreg Kroah-Hartman 	int	retval=0;
448a6afd9f3SGreg Kroah-Hartman 	unsigned long page;
449a6afd9f3SGreg Kroah-Hartman 
450a6afd9f3SGreg Kroah-Hartman 	page = get_zeroed_page(GFP_KERNEL);
451a6afd9f3SGreg Kroah-Hartman 	if (!page)
452a6afd9f3SGreg Kroah-Hartman 		return -ENOMEM;
453a6afd9f3SGreg Kroah-Hartman 
454a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
455a6afd9f3SGreg Kroah-Hartman 
456d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
457a6afd9f3SGreg Kroah-Hartman 		free_page(page);
458a6afd9f3SGreg Kroah-Hartman 		goto errout;
459a6afd9f3SGreg Kroah-Hartman 	}
460a6afd9f3SGreg Kroah-Hartman 
461a6afd9f3SGreg Kroah-Hartman 	if (info->xmit.buf)
462a6afd9f3SGreg Kroah-Hartman 		free_page(page);
463a6afd9f3SGreg Kroah-Hartman 	else
464a6afd9f3SGreg Kroah-Hartman 		info->xmit.buf = (unsigned char *) page;
465a6afd9f3SGreg Kroah-Hartman 
466a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_OPEN
467a6afd9f3SGreg Kroah-Hartman 	printk("starting up ttys%d ...", info->line);
468a6afd9f3SGreg Kroah-Hartman #endif
469a6afd9f3SGreg Kroah-Hartman 
470a6afd9f3SGreg Kroah-Hartman 	/* Clear anything in the input buffer */
471a6afd9f3SGreg Kroah-Hartman 
47281680702SJiri Slaby 	amiga_custom.intreq = IF_RBF;
473a6afd9f3SGreg Kroah-Hartman 	mb();
474a6afd9f3SGreg Kroah-Hartman 
475a6afd9f3SGreg Kroah-Hartman 	retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
476a6afd9f3SGreg Kroah-Hartman 	if (retval) {
47793525619SJiri Slaby 		if (capable(CAP_SYS_ADMIN)) {
478588993ddSJiri Slaby 			set_bit(TTY_IO_ERROR, &tty->flags);
479a6afd9f3SGreg Kroah-Hartman 			retval = 0;
480a6afd9f3SGreg Kroah-Hartman 		}
481a6afd9f3SGreg Kroah-Hartman 		goto errout;
482a6afd9f3SGreg Kroah-Hartman 	}
483a6afd9f3SGreg Kroah-Hartman 
484a6afd9f3SGreg Kroah-Hartman 	/* enable both Rx and Tx interrupts */
48581680702SJiri Slaby 	amiga_custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
486a6afd9f3SGreg Kroah-Hartman 	mb();
487a6afd9f3SGreg Kroah-Hartman 	info->IER = UART_IER_MSI;
488a6afd9f3SGreg Kroah-Hartman 
489a6afd9f3SGreg Kroah-Hartman 	/* remember current state of the DCD and CTS bits */
490a6afd9f3SGreg Kroah-Hartman 	current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
491a6afd9f3SGreg Kroah-Hartman 
492a6afd9f3SGreg Kroah-Hartman 	info->MCR = 0;
493588993ddSJiri Slaby 	if (C_BAUD(tty))
494a6afd9f3SGreg Kroah-Hartman 	  info->MCR = SER_DTR | SER_RTS;
495a6afd9f3SGreg Kroah-Hartman 	rtsdtr_ctrl(info->MCR);
496a6afd9f3SGreg Kroah-Hartman 
497588993ddSJiri Slaby 	clear_bit(TTY_IO_ERROR, &tty->flags);
498a6afd9f3SGreg Kroah-Hartman 	info->xmit.head = info->xmit.tail = 0;
499a6afd9f3SGreg Kroah-Hartman 
500a6afd9f3SGreg Kroah-Hartman 	/*
501a6afd9f3SGreg Kroah-Hartman 	 * and set the speed of the serial port
502a6afd9f3SGreg Kroah-Hartman 	 */
503588993ddSJiri Slaby 	change_speed(tty, info, NULL);
504a6afd9f3SGreg Kroah-Hartman 
505515be7baSIlpo Järvinen 	tty_port_set_initialized(port, true);
506a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
507a6afd9f3SGreg Kroah-Hartman 	return 0;
508a6afd9f3SGreg Kroah-Hartman 
509a6afd9f3SGreg Kroah-Hartman errout:
510a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
511a6afd9f3SGreg Kroah-Hartman 	return retval;
512a6afd9f3SGreg Kroah-Hartman }
513a6afd9f3SGreg Kroah-Hartman 
514a6afd9f3SGreg Kroah-Hartman /*
515a6afd9f3SGreg Kroah-Hartman  * This routine will shutdown a serial port; interrupts are disabled, and
516a6afd9f3SGreg Kroah-Hartman  * DTR is dropped if the hangup on close termio flag is on.
517a6afd9f3SGreg Kroah-Hartman  */
shutdown(struct tty_struct * tty,struct serial_state * info)518588993ddSJiri Slaby static void shutdown(struct tty_struct *tty, struct serial_state *info)
519a6afd9f3SGreg Kroah-Hartman {
520a6afd9f3SGreg Kroah-Hartman 	unsigned long	flags;
521a6afd9f3SGreg Kroah-Hartman 
522d41861caSPeter Hurley 	if (!tty_port_initialized(&info->tport))
523a6afd9f3SGreg Kroah-Hartman 		return;
524a6afd9f3SGreg Kroah-Hartman 
525a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_OPEN
526a6afd9f3SGreg Kroah-Hartman 	printk("Shutting down serial port %d ....\n", info->line);
527a6afd9f3SGreg Kroah-Hartman #endif
528a6afd9f3SGreg Kroah-Hartman 
529a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags); /* Disable interrupts */
530a6afd9f3SGreg Kroah-Hartman 
531a6afd9f3SGreg Kroah-Hartman 	/*
532a6afd9f3SGreg Kroah-Hartman 	 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
533a6afd9f3SGreg Kroah-Hartman 	 * here so the queue might never be waken up
534a6afd9f3SGreg Kroah-Hartman 	 */
53587758791SJiri Slaby 	wake_up_interruptible(&info->tport.delta_msr_wait);
536a6afd9f3SGreg Kroah-Hartman 
537a6afd9f3SGreg Kroah-Hartman 	/*
538a6afd9f3SGreg Kroah-Hartman 	 * Free the IRQ, if necessary
539a6afd9f3SGreg Kroah-Hartman 	 */
540a6afd9f3SGreg Kroah-Hartman 	free_irq(IRQ_AMIGA_VERTB, info);
541a6afd9f3SGreg Kroah-Hartman 
542a6afd9f3SGreg Kroah-Hartman 	free_page((unsigned long)info->xmit.buf);
543a6afd9f3SGreg Kroah-Hartman 	info->xmit.buf = NULL;
544a6afd9f3SGreg Kroah-Hartman 
545a6afd9f3SGreg Kroah-Hartman 	info->IER = 0;
54681680702SJiri Slaby 	amiga_custom.intena = IF_RBF | IF_TBE;
547a6afd9f3SGreg Kroah-Hartman 	mb();
548a6afd9f3SGreg Kroah-Hartman 
549a6afd9f3SGreg Kroah-Hartman 	/* disable break condition */
55081680702SJiri Slaby 	amiga_custom.adkcon = AC_UARTBRK;
551a6afd9f3SGreg Kroah-Hartman 	mb();
552a6afd9f3SGreg Kroah-Hartman 
5539db276f8SPeter Hurley 	if (C_HUPCL(tty))
554a6afd9f3SGreg Kroah-Hartman 		info->MCR &= ~(SER_DTR|SER_RTS);
555a6afd9f3SGreg Kroah-Hartman 	rtsdtr_ctrl(info->MCR);
556a6afd9f3SGreg Kroah-Hartman 
557588993ddSJiri Slaby 	set_bit(TTY_IO_ERROR, &tty->flags);
558a6afd9f3SGreg Kroah-Hartman 
559515be7baSIlpo Järvinen 	tty_port_set_initialized(&info->tport, false);
560a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
561a6afd9f3SGreg Kroah-Hartman }
562a6afd9f3SGreg Kroah-Hartman 
563a6afd9f3SGreg Kroah-Hartman 
564a6afd9f3SGreg Kroah-Hartman /*
565a6afd9f3SGreg Kroah-Hartman  * This routine is called to set the UART divisor registers to match
566a6afd9f3SGreg Kroah-Hartman  * the specified baud rate for a serial port.
567a6afd9f3SGreg Kroah-Hartman  */
change_speed(struct tty_struct * tty,struct serial_state * info,const struct ktermios * old_termios)568588993ddSJiri Slaby static void change_speed(struct tty_struct *tty, struct serial_state *info,
569a8c11c15SIlpo Järvinen 			 const struct ktermios *old_termios)
570a6afd9f3SGreg Kroah-Hartman {
57101bd730dSJiri Slaby 	struct tty_port *port = &info->tport;
572a6afd9f3SGreg Kroah-Hartman 	int	quot = 0, baud_base, baud;
573a6afd9f3SGreg Kroah-Hartman 	unsigned cflag, cval = 0;
574a6afd9f3SGreg Kroah-Hartman 	int	bits;
575a6afd9f3SGreg Kroah-Hartman 	unsigned long	flags;
576a6afd9f3SGreg Kroah-Hartman 
577adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
578a6afd9f3SGreg Kroah-Hartman 
579a6afd9f3SGreg Kroah-Hartman 	/* Byte size is always 8 bits plus parity bit if requested */
580a6afd9f3SGreg Kroah-Hartman 
581a6afd9f3SGreg Kroah-Hartman 	cval = 3; bits = 10;
582a6afd9f3SGreg Kroah-Hartman 	if (cflag & CSTOPB) {
583a6afd9f3SGreg Kroah-Hartman 		cval |= 0x04;
584a6afd9f3SGreg Kroah-Hartman 		bits++;
585a6afd9f3SGreg Kroah-Hartman 	}
586a6afd9f3SGreg Kroah-Hartman 	if (cflag & PARENB) {
587a6afd9f3SGreg Kroah-Hartman 		cval |= UART_LCR_PARITY;
588a6afd9f3SGreg Kroah-Hartman 		bits++;
589a6afd9f3SGreg Kroah-Hartman 	}
590a6afd9f3SGreg Kroah-Hartman 	if (!(cflag & PARODD))
591a6afd9f3SGreg Kroah-Hartman 		cval |= UART_LCR_EPAR;
592a6afd9f3SGreg Kroah-Hartman 	if (cflag & CMSPAR)
593a6afd9f3SGreg Kroah-Hartman 		cval |= UART_LCR_SPAR;
594a6afd9f3SGreg Kroah-Hartman 
595a6afd9f3SGreg Kroah-Hartman 	/* Determine divisor based on baud rate */
596588993ddSJiri Slaby 	baud = tty_get_baud_rate(tty);
597a6afd9f3SGreg Kroah-Hartman 	if (!baud)
598a6afd9f3SGreg Kroah-Hartman 		baud = 9600;	/* B0 transition handled in rs_set_termios */
599916b7656SJiri Slaby 	baud_base = info->baud_base;
60001bd730dSJiri Slaby 	if (baud == 38400 && (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
601916b7656SJiri Slaby 		quot = info->custom_divisor;
602a6afd9f3SGreg Kroah-Hartman 	else {
603a6afd9f3SGreg Kroah-Hartman 		if (baud == 134)
604a6afd9f3SGreg Kroah-Hartman 			/* Special case since 134 is really 134.5 */
605a6afd9f3SGreg Kroah-Hartman 			quot = (2*baud_base / 269);
606a6afd9f3SGreg Kroah-Hartman 		else if (baud)
607a6afd9f3SGreg Kroah-Hartman 			quot = baud_base / baud;
608a6afd9f3SGreg Kroah-Hartman 	}
609a6afd9f3SGreg Kroah-Hartman 	/* If the quotient is zero refuse the change */
610a6afd9f3SGreg Kroah-Hartman 	if (!quot && old_termios) {
611a6afd9f3SGreg Kroah-Hartman 		/* FIXME: Will need updating for new tty in the end */
612adc8d746SAlan Cox 		tty->termios.c_cflag &= ~CBAUD;
613adc8d746SAlan Cox 		tty->termios.c_cflag |= (old_termios->c_cflag & CBAUD);
614588993ddSJiri Slaby 		baud = tty_get_baud_rate(tty);
615a6afd9f3SGreg Kroah-Hartman 		if (!baud)
616a6afd9f3SGreg Kroah-Hartman 			baud = 9600;
617a6afd9f3SGreg Kroah-Hartman 		if (baud == 38400 &&
61801bd730dSJiri Slaby 		    (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
619916b7656SJiri Slaby 			quot = info->custom_divisor;
620a6afd9f3SGreg Kroah-Hartman 		else {
621a6afd9f3SGreg Kroah-Hartman 			if (baud == 134)
622a6afd9f3SGreg Kroah-Hartman 				/* Special case since 134 is really 134.5 */
623a6afd9f3SGreg Kroah-Hartman 				quot = (2*baud_base / 269);
624a6afd9f3SGreg Kroah-Hartman 			else if (baud)
625a6afd9f3SGreg Kroah-Hartman 				quot = baud_base / baud;
626a6afd9f3SGreg Kroah-Hartman 		}
627a6afd9f3SGreg Kroah-Hartman 	}
628a6afd9f3SGreg Kroah-Hartman 	/* As a last resort, if the quotient is zero, default to 9600 bps */
629a6afd9f3SGreg Kroah-Hartman 	if (!quot)
630a6afd9f3SGreg Kroah-Hartman 		quot = baud_base / 9600;
631a6afd9f3SGreg Kroah-Hartman 	info->quot = quot;
6325a7c7a6bSJiri Slaby 	info->timeout = (XMIT_FIFO_SIZE*HZ*bits*quot) / baud_base;
633a6afd9f3SGreg Kroah-Hartman 	info->timeout += HZ/50;		/* Add .02 seconds of slop */
634a6afd9f3SGreg Kroah-Hartman 
635a6afd9f3SGreg Kroah-Hartman 	/* CTS flow control flag and modem status interrupts */
636a6afd9f3SGreg Kroah-Hartman 	info->IER &= ~UART_IER_MSI;
63701bd730dSJiri Slaby 	if (port->flags & ASYNC_HARDPPS_CD)
638a6afd9f3SGreg Kroah-Hartman 		info->IER |= UART_IER_MSI;
6395604a98eSPeter Hurley 	tty_port_set_cts_flow(port, cflag & CRTSCTS);
6405604a98eSPeter Hurley 	if (cflag & CRTSCTS)
641a6afd9f3SGreg Kroah-Hartman 		info->IER |= UART_IER_MSI;
6422d68655dSPeter Hurley 	tty_port_set_check_carrier(port, ~cflag & CLOCAL);
6432d68655dSPeter Hurley 	if (~cflag & CLOCAL)
644a6afd9f3SGreg Kroah-Hartman 		info->IER |= UART_IER_MSI;
645a6afd9f3SGreg Kroah-Hartman 	/* TBD:
646a6afd9f3SGreg Kroah-Hartman 	 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
647a6afd9f3SGreg Kroah-Hartman 	 */
648a6afd9f3SGreg Kroah-Hartman 
649a6afd9f3SGreg Kroah-Hartman 	/*
650a6afd9f3SGreg Kroah-Hartman 	 * Set up parity check flag
651a6afd9f3SGreg Kroah-Hartman 	 */
652a6afd9f3SGreg Kroah-Hartman 
653a6afd9f3SGreg Kroah-Hartman 	info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
654588993ddSJiri Slaby 	if (I_INPCK(tty))
655a6afd9f3SGreg Kroah-Hartman 		info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
656588993ddSJiri Slaby 	if (I_BRKINT(tty) || I_PARMRK(tty))
657a6afd9f3SGreg Kroah-Hartman 		info->read_status_mask |= UART_LSR_BI;
658a6afd9f3SGreg Kroah-Hartman 
659a6afd9f3SGreg Kroah-Hartman 	/*
660a6afd9f3SGreg Kroah-Hartman 	 * Characters to ignore
661a6afd9f3SGreg Kroah-Hartman 	 */
662a6afd9f3SGreg Kroah-Hartman 	info->ignore_status_mask = 0;
663588993ddSJiri Slaby 	if (I_IGNPAR(tty))
664a6afd9f3SGreg Kroah-Hartman 		info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
665588993ddSJiri Slaby 	if (I_IGNBRK(tty)) {
666a6afd9f3SGreg Kroah-Hartman 		info->ignore_status_mask |= UART_LSR_BI;
667a6afd9f3SGreg Kroah-Hartman 		/*
668a6afd9f3SGreg Kroah-Hartman 		 * If we're ignore parity and break indicators, ignore
669a6afd9f3SGreg Kroah-Hartman 		 * overruns too.  (For real raw support).
670a6afd9f3SGreg Kroah-Hartman 		 */
671588993ddSJiri Slaby 		if (I_IGNPAR(tty))
672a6afd9f3SGreg Kroah-Hartman 			info->ignore_status_mask |= UART_LSR_OE;
673a6afd9f3SGreg Kroah-Hartman 	}
674a6afd9f3SGreg Kroah-Hartman 	/*
675a6afd9f3SGreg Kroah-Hartman 	 * !!! ignore all characters if CREAD is not set
676a6afd9f3SGreg Kroah-Hartman 	 */
677a6afd9f3SGreg Kroah-Hartman 	if ((cflag & CREAD) == 0)
678a6afd9f3SGreg Kroah-Hartman 		info->ignore_status_mask |= UART_LSR_DR;
679a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
680a6afd9f3SGreg Kroah-Hartman 
681a6afd9f3SGreg Kroah-Hartman 	{
682a6afd9f3SGreg Kroah-Hartman 	  short serper;
683a6afd9f3SGreg Kroah-Hartman 
684a6afd9f3SGreg Kroah-Hartman 	/* Set up the baud rate */
685a6afd9f3SGreg Kroah-Hartman 	  serper = quot - 1;
686a6afd9f3SGreg Kroah-Hartman 
687a6afd9f3SGreg Kroah-Hartman 	/* Enable or disable parity bit */
688a6afd9f3SGreg Kroah-Hartman 
689a6afd9f3SGreg Kroah-Hartman 	if(cval & UART_LCR_PARITY)
690a6afd9f3SGreg Kroah-Hartman 	  serper |= (SERPER_PARENB);
691a6afd9f3SGreg Kroah-Hartman 
69281680702SJiri Slaby 	amiga_custom.serper = serper;
693a6afd9f3SGreg Kroah-Hartman 	mb();
694a6afd9f3SGreg Kroah-Hartman 	}
695a6afd9f3SGreg Kroah-Hartman 
696a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
697a6afd9f3SGreg Kroah-Hartman }
698a6afd9f3SGreg Kroah-Hartman 
rs_put_char(struct tty_struct * tty,u8 ch)699dcaafbe6SJiri Slaby (SUSE) static int rs_put_char(struct tty_struct *tty, u8 ch)
700a6afd9f3SGreg Kroah-Hartman {
701916b7656SJiri Slaby 	struct serial_state *info;
702a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
703a6afd9f3SGreg Kroah-Hartman 
704a6afd9f3SGreg Kroah-Hartman 	info = tty->driver_data;
705a6afd9f3SGreg Kroah-Hartman 
706a6afd9f3SGreg Kroah-Hartman 	if (!info->xmit.buf)
707a6afd9f3SGreg Kroah-Hartman 		return 0;
708a6afd9f3SGreg Kroah-Hartman 
709a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
710a6afd9f3SGreg Kroah-Hartman 	if (CIRC_SPACE(info->xmit.head,
711a6afd9f3SGreg Kroah-Hartman 		       info->xmit.tail,
712eb47b59aSIlpo Järvinen 		       UART_XMIT_SIZE) == 0) {
713a6afd9f3SGreg Kroah-Hartman 		local_irq_restore(flags);
714a6afd9f3SGreg Kroah-Hartman 		return 0;
715a6afd9f3SGreg Kroah-Hartman 	}
716a6afd9f3SGreg Kroah-Hartman 
717a6afd9f3SGreg Kroah-Hartman 	info->xmit.buf[info->xmit.head++] = ch;
718eb47b59aSIlpo Järvinen 	info->xmit.head &= UART_XMIT_SIZE - 1;
719a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
720a6afd9f3SGreg Kroah-Hartman 	return 1;
721a6afd9f3SGreg Kroah-Hartman }
722a6afd9f3SGreg Kroah-Hartman 
rs_flush_chars(struct tty_struct * tty)723a6afd9f3SGreg Kroah-Hartman static void rs_flush_chars(struct tty_struct *tty)
724a6afd9f3SGreg Kroah-Hartman {
725916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
726a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
727a6afd9f3SGreg Kroah-Hartman 
728a6afd9f3SGreg Kroah-Hartman 	if (info->xmit.head == info->xmit.tail
7296e94dbc7SJiri Slaby 	    || tty->flow.stopped
730a6afd9f3SGreg Kroah-Hartman 	    || tty->hw_stopped
731a6afd9f3SGreg Kroah-Hartman 	    || !info->xmit.buf)
732a6afd9f3SGreg Kroah-Hartman 		return;
733a6afd9f3SGreg Kroah-Hartman 
734a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
735a6afd9f3SGreg Kroah-Hartman 	info->IER |= UART_IER_THRI;
73681680702SJiri Slaby 	amiga_custom.intena = IF_SETCLR | IF_TBE;
737a6afd9f3SGreg Kroah-Hartman 	mb();
738a6afd9f3SGreg Kroah-Hartman 	/* set a pending Tx Interrupt, transmitter should restart now */
73981680702SJiri Slaby 	amiga_custom.intreq = IF_SETCLR | IF_TBE;
740a6afd9f3SGreg Kroah-Hartman 	mb();
741a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
742a6afd9f3SGreg Kroah-Hartman }
743a6afd9f3SGreg Kroah-Hartman 
rs_write(struct tty_struct * tty,const u8 * buf,size_t count)744*95713967SJiri Slaby (SUSE) static ssize_t rs_write(struct tty_struct * tty, const u8 *buf, size_t count)
745a6afd9f3SGreg Kroah-Hartman {
746a6afd9f3SGreg Kroah-Hartman 	int	c, ret = 0;
747916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
748a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
749a6afd9f3SGreg Kroah-Hartman 
750a6afd9f3SGreg Kroah-Hartman 	if (!info->xmit.buf)
751a6afd9f3SGreg Kroah-Hartman 		return 0;
752a6afd9f3SGreg Kroah-Hartman 
753a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
754a6afd9f3SGreg Kroah-Hartman 	while (1) {
755a6afd9f3SGreg Kroah-Hartman 		c = CIRC_SPACE_TO_END(info->xmit.head,
756a6afd9f3SGreg Kroah-Hartman 				      info->xmit.tail,
757eb47b59aSIlpo Järvinen 				      UART_XMIT_SIZE);
758a6afd9f3SGreg Kroah-Hartman 		if (count < c)
759a6afd9f3SGreg Kroah-Hartman 			c = count;
760a6afd9f3SGreg Kroah-Hartman 		if (c <= 0) {
761a6afd9f3SGreg Kroah-Hartman 			break;
762a6afd9f3SGreg Kroah-Hartman 		}
763a6afd9f3SGreg Kroah-Hartman 		memcpy(info->xmit.buf + info->xmit.head, buf, c);
764eb47b59aSIlpo Järvinen 		info->xmit.head = (info->xmit.head + c) & (UART_XMIT_SIZE - 1);
765a6afd9f3SGreg Kroah-Hartman 		buf += c;
766a6afd9f3SGreg Kroah-Hartman 		count -= c;
767a6afd9f3SGreg Kroah-Hartman 		ret += c;
768a6afd9f3SGreg Kroah-Hartman 	}
769a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
770a6afd9f3SGreg Kroah-Hartman 
771a6afd9f3SGreg Kroah-Hartman 	if (info->xmit.head != info->xmit.tail
7726e94dbc7SJiri Slaby 	    && !tty->flow.stopped
773a6afd9f3SGreg Kroah-Hartman 	    && !tty->hw_stopped
774a6afd9f3SGreg Kroah-Hartman 	    && !(info->IER & UART_IER_THRI)) {
775a6afd9f3SGreg Kroah-Hartman 		info->IER |= UART_IER_THRI;
776a6afd9f3SGreg Kroah-Hartman 		local_irq_disable();
77781680702SJiri Slaby 		amiga_custom.intena = IF_SETCLR | IF_TBE;
778a6afd9f3SGreg Kroah-Hartman 		mb();
779a6afd9f3SGreg Kroah-Hartman 		/* set a pending Tx Interrupt, transmitter should restart now */
78081680702SJiri Slaby 		amiga_custom.intreq = IF_SETCLR | IF_TBE;
781a6afd9f3SGreg Kroah-Hartman 		mb();
782a6afd9f3SGreg Kroah-Hartman 		local_irq_restore(flags);
783a6afd9f3SGreg Kroah-Hartman 	}
784a6afd9f3SGreg Kroah-Hartman 	return ret;
785a6afd9f3SGreg Kroah-Hartman }
786a6afd9f3SGreg Kroah-Hartman 
rs_write_room(struct tty_struct * tty)78703b3b1a2SJiri Slaby static unsigned int rs_write_room(struct tty_struct *tty)
788a6afd9f3SGreg Kroah-Hartman {
789916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
790a6afd9f3SGreg Kroah-Hartman 
791eb47b59aSIlpo Järvinen 	return CIRC_SPACE(info->xmit.head, info->xmit.tail, UART_XMIT_SIZE);
792a6afd9f3SGreg Kroah-Hartman }
793a6afd9f3SGreg Kroah-Hartman 
rs_chars_in_buffer(struct tty_struct * tty)794fff4ef17SJiri Slaby static unsigned int rs_chars_in_buffer(struct tty_struct *tty)
795a6afd9f3SGreg Kroah-Hartman {
796916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
797a6afd9f3SGreg Kroah-Hartman 
798eb47b59aSIlpo Järvinen 	return CIRC_CNT(info->xmit.head, info->xmit.tail, UART_XMIT_SIZE);
799a6afd9f3SGreg Kroah-Hartman }
800a6afd9f3SGreg Kroah-Hartman 
rs_flush_buffer(struct tty_struct * tty)801a6afd9f3SGreg Kroah-Hartman static void rs_flush_buffer(struct tty_struct *tty)
802a6afd9f3SGreg Kroah-Hartman {
803916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
804a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
805a6afd9f3SGreg Kroah-Hartman 
806a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
807a6afd9f3SGreg Kroah-Hartman 	info->xmit.head = info->xmit.tail = 0;
808a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
809a6afd9f3SGreg Kroah-Hartman 	tty_wakeup(tty);
810a6afd9f3SGreg Kroah-Hartman }
811a6afd9f3SGreg Kroah-Hartman 
812a6afd9f3SGreg Kroah-Hartman /*
813a6afd9f3SGreg Kroah-Hartman  * This function is used to send a high-priority XON/XOFF character to
814a6afd9f3SGreg Kroah-Hartman  * the device
815a6afd9f3SGreg Kroah-Hartman  */
rs_send_xchar(struct tty_struct * tty,char ch)816a6afd9f3SGreg Kroah-Hartman static void rs_send_xchar(struct tty_struct *tty, char ch)
817a6afd9f3SGreg Kroah-Hartman {
818916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
819a6afd9f3SGreg Kroah-Hartman         unsigned long flags;
820a6afd9f3SGreg Kroah-Hartman 
821a6afd9f3SGreg Kroah-Hartman 	info->x_char = ch;
822a6afd9f3SGreg Kroah-Hartman 	if (ch) {
823a6afd9f3SGreg Kroah-Hartman 		/* Make sure transmit interrupts are on */
824a6afd9f3SGreg Kroah-Hartman 
825a6afd9f3SGreg Kroah-Hartman 	        /* Check this ! */
826a6afd9f3SGreg Kroah-Hartman 	        local_irq_save(flags);
82781680702SJiri Slaby 		if(!(amiga_custom.intenar & IF_TBE)) {
82881680702SJiri Slaby 		    amiga_custom.intena = IF_SETCLR | IF_TBE;
829a6afd9f3SGreg Kroah-Hartman 		    mb();
830a6afd9f3SGreg Kroah-Hartman 		    /* set a pending Tx Interrupt, transmitter should restart now */
83181680702SJiri Slaby 		    amiga_custom.intreq = IF_SETCLR | IF_TBE;
832a6afd9f3SGreg Kroah-Hartman 		    mb();
833a6afd9f3SGreg Kroah-Hartman 		}
834a6afd9f3SGreg Kroah-Hartman 		local_irq_restore(flags);
835a6afd9f3SGreg Kroah-Hartman 
836a6afd9f3SGreg Kroah-Hartman 		info->IER |= UART_IER_THRI;
837a6afd9f3SGreg Kroah-Hartman 	}
838a6afd9f3SGreg Kroah-Hartman }
839a6afd9f3SGreg Kroah-Hartman 
840a6afd9f3SGreg Kroah-Hartman /*
841a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
842a6afd9f3SGreg Kroah-Hartman  * rs_throttle()
843a6afd9f3SGreg Kroah-Hartman  *
844a6afd9f3SGreg Kroah-Hartman  * This routine is called by the upper-layer tty layer to signal that
845a6afd9f3SGreg Kroah-Hartman  * incoming characters should be throttled.
846a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
847a6afd9f3SGreg Kroah-Hartman  */
rs_throttle(struct tty_struct * tty)848a6afd9f3SGreg Kroah-Hartman static void rs_throttle(struct tty_struct * tty)
849a6afd9f3SGreg Kroah-Hartman {
850916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
851a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
852a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_THROTTLE
853fdfb719eSPeter Hurley 	printk("throttle %s ....\n", tty_name(tty));
854a6afd9f3SGreg Kroah-Hartman #endif
855a6afd9f3SGreg Kroah-Hartman 
856a6afd9f3SGreg Kroah-Hartman 	if (I_IXOFF(tty))
857a6afd9f3SGreg Kroah-Hartman 		rs_send_xchar(tty, STOP_CHAR(tty));
858a6afd9f3SGreg Kroah-Hartman 
8599db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
860a6afd9f3SGreg Kroah-Hartman 		info->MCR &= ~SER_RTS;
861a6afd9f3SGreg Kroah-Hartman 
862a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
863a6afd9f3SGreg Kroah-Hartman 	rtsdtr_ctrl(info->MCR);
864a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
865a6afd9f3SGreg Kroah-Hartman }
866a6afd9f3SGreg Kroah-Hartman 
rs_unthrottle(struct tty_struct * tty)867a6afd9f3SGreg Kroah-Hartman static void rs_unthrottle(struct tty_struct * tty)
868a6afd9f3SGreg Kroah-Hartman {
869916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
870a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
871a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_THROTTLE
872fdfb719eSPeter Hurley 	printk("unthrottle %s ....\n", tty_name(tty));
873a6afd9f3SGreg Kroah-Hartman #endif
874a6afd9f3SGreg Kroah-Hartman 
875a6afd9f3SGreg Kroah-Hartman 	if (I_IXOFF(tty)) {
876a6afd9f3SGreg Kroah-Hartman 		if (info->x_char)
877a6afd9f3SGreg Kroah-Hartman 			info->x_char = 0;
878a6afd9f3SGreg Kroah-Hartman 		else
879a6afd9f3SGreg Kroah-Hartman 			rs_send_xchar(tty, START_CHAR(tty));
880a6afd9f3SGreg Kroah-Hartman 	}
8819db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
882a6afd9f3SGreg Kroah-Hartman 		info->MCR |= SER_RTS;
883a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
884a6afd9f3SGreg Kroah-Hartman 	rtsdtr_ctrl(info->MCR);
885a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
886a6afd9f3SGreg Kroah-Hartman }
887a6afd9f3SGreg Kroah-Hartman 
888a6afd9f3SGreg Kroah-Hartman /*
889a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
890a6afd9f3SGreg Kroah-Hartman  * rs_ioctl() and friends
891a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
892a6afd9f3SGreg Kroah-Hartman  */
893a6afd9f3SGreg Kroah-Hartman 
get_serial_info(struct tty_struct * tty,struct serial_struct * ss)894b129cbc9SAl Viro static int get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
895a6afd9f3SGreg Kroah-Hartman {
896b129cbc9SAl Viro 	struct serial_state *state = tty->driver_data;
897c33a63e8SJohan Hovold 	unsigned int close_delay, closing_wait;
898a6afd9f3SGreg Kroah-Hartman 
89989c8d91eSAlan Cox 	tty_lock(tty);
900c33a63e8SJohan Hovold 	close_delay = jiffies_to_msecs(state->tport.close_delay) / 10;
901c33a63e8SJohan Hovold 	closing_wait = state->tport.closing_wait;
902c33a63e8SJohan Hovold 	if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
903c33a63e8SJohan Hovold 		closing_wait = jiffies_to_msecs(closing_wait) / 10;
904c33a63e8SJohan Hovold 
905b129cbc9SAl Viro 	ss->line = tty->index;
906b129cbc9SAl Viro 	ss->port = state->port;
907b129cbc9SAl Viro 	ss->flags = state->tport.flags;
9085a7c7a6bSJiri Slaby 	ss->xmit_fifo_size = XMIT_FIFO_SIZE;
909b129cbc9SAl Viro 	ss->baud_base = state->baud_base;
910c33a63e8SJohan Hovold 	ss->close_delay = close_delay;
911c33a63e8SJohan Hovold 	ss->closing_wait = closing_wait;
912b129cbc9SAl Viro 	ss->custom_divisor = state->custom_divisor;
91389c8d91eSAlan Cox 	tty_unlock(tty);
914a6afd9f3SGreg Kroah-Hartman 	return 0;
915a6afd9f3SGreg Kroah-Hartman }
916a6afd9f3SGreg Kroah-Hartman 
set_serial_info(struct tty_struct * tty,struct serial_struct * ss)917b129cbc9SAl Viro static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
918a6afd9f3SGreg Kroah-Hartman {
919b129cbc9SAl Viro 	struct serial_state *state = tty->driver_data;
92001bd730dSJiri Slaby 	struct tty_port *port = &state->tport;
9210f9b9684SJiri Slaby 	bool change_spd;
922a6afd9f3SGreg Kroah-Hartman 	int 			retval = 0;
923c33a63e8SJohan Hovold 	unsigned int close_delay, closing_wait;
924a6afd9f3SGreg Kroah-Hartman 
92589c8d91eSAlan Cox 	tty_lock(tty);
926b129cbc9SAl Viro 	change_spd = ((ss->flags ^ port->flags) & ASYNC_SPD_MASK) ||
927b129cbc9SAl Viro 		ss->custom_divisor != state->custom_divisor;
928b129cbc9SAl Viro 	if (ss->irq || ss->port != state->port ||
9295a7c7a6bSJiri Slaby 			ss->xmit_fifo_size != XMIT_FIFO_SIZE) {
93089c8d91eSAlan Cox 		tty_unlock(tty);
931a6afd9f3SGreg Kroah-Hartman 		return -EINVAL;
932a6afd9f3SGreg Kroah-Hartman 	}
933a6afd9f3SGreg Kroah-Hartman 
934c33a63e8SJohan Hovold 	close_delay = msecs_to_jiffies(ss->close_delay * 10);
935c33a63e8SJohan Hovold 	closing_wait = ss->closing_wait;
936c33a63e8SJohan Hovold 	if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
937c33a63e8SJohan Hovold 		closing_wait = msecs_to_jiffies(closing_wait * 10);
938c33a63e8SJohan Hovold 
93993525619SJiri Slaby 	if (!capable(CAP_SYS_ADMIN)) {
940b129cbc9SAl Viro 		if ((ss->baud_base != state->baud_base) ||
941c33a63e8SJohan Hovold 		    (close_delay != port->close_delay) ||
942c33a63e8SJohan Hovold 		    (closing_wait != port->closing_wait) ||
943b129cbc9SAl Viro 		    ((ss->flags & ~ASYNC_USR_MASK) !=
944d3a7b83fSJulia Lawall 		     (port->flags & ~ASYNC_USR_MASK))) {
94589c8d91eSAlan Cox 			tty_unlock(tty);
946a6afd9f3SGreg Kroah-Hartman 			return -EPERM;
947d3a7b83fSJulia Lawall 		}
94801bd730dSJiri Slaby 		port->flags = ((port->flags & ~ASYNC_USR_MASK) |
949b129cbc9SAl Viro 			       (ss->flags & ASYNC_USR_MASK));
950b129cbc9SAl Viro 		state->custom_divisor = ss->custom_divisor;
951a6afd9f3SGreg Kroah-Hartman 		goto check_and_exit;
952a6afd9f3SGreg Kroah-Hartman 	}
953a6afd9f3SGreg Kroah-Hartman 
954b129cbc9SAl Viro 	if (ss->baud_base < 9600) {
95589c8d91eSAlan Cox 		tty_unlock(tty);
956a6afd9f3SGreg Kroah-Hartman 		return -EINVAL;
957a6afd9f3SGreg Kroah-Hartman 	}
958a6afd9f3SGreg Kroah-Hartman 
959a6afd9f3SGreg Kroah-Hartman 	/*
960a6afd9f3SGreg Kroah-Hartman 	 * OK, past this point, all the error checking has been done.
961a6afd9f3SGreg Kroah-Hartman 	 * At this point, we start making changes.....
962a6afd9f3SGreg Kroah-Hartman 	 */
963a6afd9f3SGreg Kroah-Hartman 
964b129cbc9SAl Viro 	state->baud_base = ss->baud_base;
96501bd730dSJiri Slaby 	port->flags = ((port->flags & ~ASYNC_FLAGS) |
966b129cbc9SAl Viro 			(ss->flags & ASYNC_FLAGS));
967b129cbc9SAl Viro 	state->custom_divisor = ss->custom_divisor;
968c33a63e8SJohan Hovold 	port->close_delay = close_delay;
969c33a63e8SJohan Hovold 	port->closing_wait = closing_wait;
970a6afd9f3SGreg Kroah-Hartman 
971a6afd9f3SGreg Kroah-Hartman check_and_exit:
972d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
9730f9b9684SJiri Slaby 		if (change_spd) {
9746086380aSJohan Hovold 			/* warn about deprecation unless clearing */
975b129cbc9SAl Viro 			if (ss->flags & ASYNC_SPD_MASK)
9766086380aSJohan Hovold 				dev_warn_ratelimited(tty->dev, "use of SPD flags is deprecated\n");
977588993ddSJiri Slaby 			change_speed(tty, state, NULL);
978a6afd9f3SGreg Kroah-Hartman 		}
979a6afd9f3SGreg Kroah-Hartman 	} else
980588993ddSJiri Slaby 		retval = startup(tty, state);
98189c8d91eSAlan Cox 	tty_unlock(tty);
982a6afd9f3SGreg Kroah-Hartman 	return retval;
983a6afd9f3SGreg Kroah-Hartman }
984a6afd9f3SGreg Kroah-Hartman 
985a6afd9f3SGreg Kroah-Hartman /*
986a6afd9f3SGreg Kroah-Hartman  * get_lsr_info - get line status register info
987a6afd9f3SGreg Kroah-Hartman  *
988a6afd9f3SGreg Kroah-Hartman  * Purpose: Let user call ioctl() to get info when the UART physically
989a6afd9f3SGreg Kroah-Hartman  * 	    is emptied.  On bus types like RS485, the transmitter must
990a6afd9f3SGreg Kroah-Hartman  * 	    release the bus after transmitting. This must be done when
991a6afd9f3SGreg Kroah-Hartman  * 	    the transmit shift register is empty, not be done when the
992a6afd9f3SGreg Kroah-Hartman  * 	    transmit holding register is empty.  This functionality
993a6afd9f3SGreg Kroah-Hartman  * 	    allows an RS485 driver to be written in user space.
994a6afd9f3SGreg Kroah-Hartman  */
get_lsr_info(struct serial_state * info,unsigned int __user * value)995916b7656SJiri Slaby static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
996a6afd9f3SGreg Kroah-Hartman {
997a6afd9f3SGreg Kroah-Hartman 	unsigned char status;
998a6afd9f3SGreg Kroah-Hartman 	unsigned int result;
999a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1000a6afd9f3SGreg Kroah-Hartman 
1001a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
100281680702SJiri Slaby 	status = amiga_custom.serdatr;
1003a6afd9f3SGreg Kroah-Hartman 	mb();
1004a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
1005a6afd9f3SGreg Kroah-Hartman 	result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1006a6afd9f3SGreg Kroah-Hartman 	if (copy_to_user(value, &result, sizeof(int)))
1007a6afd9f3SGreg Kroah-Hartman 		return -EFAULT;
1008a6afd9f3SGreg Kroah-Hartman 	return 0;
1009a6afd9f3SGreg Kroah-Hartman }
1010a6afd9f3SGreg Kroah-Hartman 
1011a6afd9f3SGreg Kroah-Hartman 
rs_tiocmget(struct tty_struct * tty)1012a6afd9f3SGreg Kroah-Hartman static int rs_tiocmget(struct tty_struct *tty)
1013a6afd9f3SGreg Kroah-Hartman {
1014916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
1015a6afd9f3SGreg Kroah-Hartman 	unsigned char control, status;
1016a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1017a6afd9f3SGreg Kroah-Hartman 
101818900ca6SPeter Hurley 	if (tty_io_error(tty))
1019a6afd9f3SGreg Kroah-Hartman 		return -EIO;
1020a6afd9f3SGreg Kroah-Hartman 
1021a6afd9f3SGreg Kroah-Hartman 	control = info->MCR;
1022a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
1023a6afd9f3SGreg Kroah-Hartman 	status = ciab.pra;
1024a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
1025a6afd9f3SGreg Kroah-Hartman 	return    ((control & SER_RTS) ? TIOCM_RTS : 0)
1026a6afd9f3SGreg Kroah-Hartman 		| ((control & SER_DTR) ? TIOCM_DTR : 0)
1027a6afd9f3SGreg Kroah-Hartman 		| (!(status  & SER_DCD) ? TIOCM_CAR : 0)
1028a6afd9f3SGreg Kroah-Hartman 		| (!(status  & SER_DSR) ? TIOCM_DSR : 0)
1029a6afd9f3SGreg Kroah-Hartman 		| (!(status  & SER_CTS) ? TIOCM_CTS : 0);
1030a6afd9f3SGreg Kroah-Hartman }
1031a6afd9f3SGreg Kroah-Hartman 
rs_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)1032a6afd9f3SGreg Kroah-Hartman static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1033a6afd9f3SGreg Kroah-Hartman 						unsigned int clear)
1034a6afd9f3SGreg Kroah-Hartman {
1035916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
1036a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1037a6afd9f3SGreg Kroah-Hartman 
103818900ca6SPeter Hurley 	if (tty_io_error(tty))
1039a6afd9f3SGreg Kroah-Hartman 		return -EIO;
1040a6afd9f3SGreg Kroah-Hartman 
1041a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
1042a6afd9f3SGreg Kroah-Hartman 	if (set & TIOCM_RTS)
1043a6afd9f3SGreg Kroah-Hartman 		info->MCR |= SER_RTS;
1044a6afd9f3SGreg Kroah-Hartman 	if (set & TIOCM_DTR)
1045a6afd9f3SGreg Kroah-Hartman 		info->MCR |= SER_DTR;
1046a6afd9f3SGreg Kroah-Hartman 	if (clear & TIOCM_RTS)
1047a6afd9f3SGreg Kroah-Hartman 		info->MCR &= ~SER_RTS;
1048a6afd9f3SGreg Kroah-Hartman 	if (clear & TIOCM_DTR)
1049a6afd9f3SGreg Kroah-Hartman 		info->MCR &= ~SER_DTR;
1050a6afd9f3SGreg Kroah-Hartman 	rtsdtr_ctrl(info->MCR);
1051a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
1052a6afd9f3SGreg Kroah-Hartman 	return 0;
1053a6afd9f3SGreg Kroah-Hartman }
1054a6afd9f3SGreg Kroah-Hartman 
1055a6afd9f3SGreg Kroah-Hartman /*
1056a6afd9f3SGreg Kroah-Hartman  * rs_break() --- routine which turns the break handling on or off
1057a6afd9f3SGreg Kroah-Hartman  */
rs_break(struct tty_struct * tty,int break_state)1058a6afd9f3SGreg Kroah-Hartman static int rs_break(struct tty_struct *tty, int break_state)
1059a6afd9f3SGreg Kroah-Hartman {
1060a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1061a6afd9f3SGreg Kroah-Hartman 
1062a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
1063a6afd9f3SGreg Kroah-Hartman 	if (break_state == -1)
106481680702SJiri Slaby 	  amiga_custom.adkcon = AC_SETCLR | AC_UARTBRK;
1065a6afd9f3SGreg Kroah-Hartman 	else
106681680702SJiri Slaby 	  amiga_custom.adkcon = AC_UARTBRK;
1067a6afd9f3SGreg Kroah-Hartman 	mb();
1068a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
1069a6afd9f3SGreg Kroah-Hartman 	return 0;
1070a6afd9f3SGreg Kroah-Hartman }
1071a6afd9f3SGreg Kroah-Hartman 
1072a6afd9f3SGreg Kroah-Hartman /*
1073a6afd9f3SGreg Kroah-Hartman  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1074a6afd9f3SGreg Kroah-Hartman  * Return: write counters to the user passed counter struct
1075a6afd9f3SGreg Kroah-Hartman  * NB: both 1->0 and 0->1 transitions are counted except for
1076a6afd9f3SGreg Kroah-Hartman  *     RI where only 0->1 is counted.
1077a6afd9f3SGreg Kroah-Hartman  */
rs_get_icount(struct tty_struct * tty,struct serial_icounter_struct * icount)1078a6afd9f3SGreg Kroah-Hartman static int rs_get_icount(struct tty_struct *tty,
1079a6afd9f3SGreg Kroah-Hartman 				struct serial_icounter_struct *icount)
1080a6afd9f3SGreg Kroah-Hartman {
1081916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
1082a6afd9f3SGreg Kroah-Hartman 	struct async_icount cnow;
1083a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1084a6afd9f3SGreg Kroah-Hartman 
1085a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
1086916b7656SJiri Slaby 	cnow = info->icount;
1087a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
1088a6afd9f3SGreg Kroah-Hartman 	icount->cts = cnow.cts;
1089a6afd9f3SGreg Kroah-Hartman 	icount->dsr = cnow.dsr;
1090a6afd9f3SGreg Kroah-Hartman 	icount->rng = cnow.rng;
1091a6afd9f3SGreg Kroah-Hartman 	icount->dcd = cnow.dcd;
1092a6afd9f3SGreg Kroah-Hartman 	icount->rx = cnow.rx;
1093a6afd9f3SGreg Kroah-Hartman 	icount->tx = cnow.tx;
1094a6afd9f3SGreg Kroah-Hartman 	icount->frame = cnow.frame;
1095a6afd9f3SGreg Kroah-Hartman 	icount->overrun = cnow.overrun;
1096a6afd9f3SGreg Kroah-Hartman 	icount->parity = cnow.parity;
1097a6afd9f3SGreg Kroah-Hartman 	icount->brk = cnow.brk;
1098a6afd9f3SGreg Kroah-Hartman 	icount->buf_overrun = cnow.buf_overrun;
1099a6afd9f3SGreg Kroah-Hartman 
1100a6afd9f3SGreg Kroah-Hartman 	return 0;
1101a6afd9f3SGreg Kroah-Hartman }
1102a6afd9f3SGreg Kroah-Hartman 
rs_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)1103a6afd9f3SGreg Kroah-Hartman static int rs_ioctl(struct tty_struct *tty,
1104a6afd9f3SGreg Kroah-Hartman 		    unsigned int cmd, unsigned long arg)
1105a6afd9f3SGreg Kroah-Hartman {
1106916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
1107a6afd9f3SGreg Kroah-Hartman 	struct async_icount cprev, cnow;	/* kernel counter temps */
1108a6afd9f3SGreg Kroah-Hartman 	void __user *argp = (void __user *)arg;
1109a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1110591cee0aSArnd Bergmann 	DEFINE_WAIT(wait);
1111591cee0aSArnd Bergmann 	int ret;
1112a6afd9f3SGreg Kroah-Hartman 
1113ce5a9831SAl Viro 	if ((cmd != TIOCSERCONFIG) &&
1114a6afd9f3SGreg Kroah-Hartman 	    (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
111518900ca6SPeter Hurley 		if (tty_io_error(tty))
1116a6afd9f3SGreg Kroah-Hartman 		    return -EIO;
1117a6afd9f3SGreg Kroah-Hartman 	}
1118a6afd9f3SGreg Kroah-Hartman 
1119a6afd9f3SGreg Kroah-Hartman 	switch (cmd) {
1120a6afd9f3SGreg Kroah-Hartman 		case TIOCSERCONFIG:
1121a6afd9f3SGreg Kroah-Hartman 			return 0;
1122a6afd9f3SGreg Kroah-Hartman 
1123a6afd9f3SGreg Kroah-Hartman 		case TIOCSERGETLSR: /* Get line status register */
1124a6afd9f3SGreg Kroah-Hartman 			return get_lsr_info(info, argp);
1125a6afd9f3SGreg Kroah-Hartman 
1126a6afd9f3SGreg Kroah-Hartman 		/*
1127a6afd9f3SGreg Kroah-Hartman 		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1128a6afd9f3SGreg Kroah-Hartman 		 * - mask passed in arg for lines of interest
1129a6afd9f3SGreg Kroah-Hartman  		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1130a6afd9f3SGreg Kroah-Hartman 		 * Caller should use TIOCGICOUNT to see which one it was
1131a6afd9f3SGreg Kroah-Hartman 		 */
1132a6afd9f3SGreg Kroah-Hartman 		case TIOCMIWAIT:
1133a6afd9f3SGreg Kroah-Hartman 			local_irq_save(flags);
1134a6afd9f3SGreg Kroah-Hartman 			/* note the counters on entry */
1135916b7656SJiri Slaby 			cprev = info->icount;
1136a6afd9f3SGreg Kroah-Hartman 			local_irq_restore(flags);
1137a6afd9f3SGreg Kroah-Hartman 			while (1) {
1138591cee0aSArnd Bergmann 				prepare_to_wait(&info->tport.delta_msr_wait,
1139591cee0aSArnd Bergmann 						&wait, TASK_INTERRUPTIBLE);
1140a6afd9f3SGreg Kroah-Hartman 				local_irq_save(flags);
1141916b7656SJiri Slaby 				cnow = info->icount; /* atomic copy */
1142a6afd9f3SGreg Kroah-Hartman 				local_irq_restore(flags);
1143a6afd9f3SGreg Kroah-Hartman 				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1144591cee0aSArnd Bergmann 				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
1145591cee0aSArnd Bergmann 					ret = -EIO; /* no change => error */
1146591cee0aSArnd Bergmann 					break;
1147591cee0aSArnd Bergmann 				}
1148a6afd9f3SGreg Kroah-Hartman 				if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1149a6afd9f3SGreg Kroah-Hartman 				     ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1150a6afd9f3SGreg Kroah-Hartman 				     ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1151a6afd9f3SGreg Kroah-Hartman 				     ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1152591cee0aSArnd Bergmann 					ret = 0;
1153591cee0aSArnd Bergmann 					break;
1154591cee0aSArnd Bergmann 				}
1155591cee0aSArnd Bergmann 				schedule();
1156591cee0aSArnd Bergmann 				/* see if a signal did it */
1157591cee0aSArnd Bergmann 				if (signal_pending(current)) {
1158591cee0aSArnd Bergmann 					ret = -ERESTARTSYS;
1159591cee0aSArnd Bergmann 					break;
1160a6afd9f3SGreg Kroah-Hartman 				}
1161a6afd9f3SGreg Kroah-Hartman 				cprev = cnow;
1162a6afd9f3SGreg Kroah-Hartman 			}
1163591cee0aSArnd Bergmann 			finish_wait(&info->tport.delta_msr_wait, &wait);
1164591cee0aSArnd Bergmann 			return ret;
1165a6afd9f3SGreg Kroah-Hartman 
1166a6afd9f3SGreg Kroah-Hartman 		default:
1167a6afd9f3SGreg Kroah-Hartman 			return -ENOIOCTLCMD;
1168a6afd9f3SGreg Kroah-Hartman 		}
1169a6afd9f3SGreg Kroah-Hartman 	return 0;
1170a6afd9f3SGreg Kroah-Hartman }
1171a6afd9f3SGreg Kroah-Hartman 
rs_set_termios(struct tty_struct * tty,const struct ktermios * old_termios)1172a8c11c15SIlpo Järvinen static void rs_set_termios(struct tty_struct *tty, const struct ktermios *old_termios)
1173a6afd9f3SGreg Kroah-Hartman {
1174916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
1175a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1176adc8d746SAlan Cox 	unsigned int cflag = tty->termios.c_cflag;
1177a6afd9f3SGreg Kroah-Hartman 
1178588993ddSJiri Slaby 	change_speed(tty, info, old_termios);
1179a6afd9f3SGreg Kroah-Hartman 
1180a6afd9f3SGreg Kroah-Hartman 	/* Handle transition to B0 status */
11819db276f8SPeter Hurley 	if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
1182a6afd9f3SGreg Kroah-Hartman 		info->MCR &= ~(SER_DTR|SER_RTS);
1183a6afd9f3SGreg Kroah-Hartman 		local_irq_save(flags);
1184a6afd9f3SGreg Kroah-Hartman 		rtsdtr_ctrl(info->MCR);
1185a6afd9f3SGreg Kroah-Hartman 		local_irq_restore(flags);
1186a6afd9f3SGreg Kroah-Hartman 	}
1187a6afd9f3SGreg Kroah-Hartman 
1188a6afd9f3SGreg Kroah-Hartman 	/* Handle transition away from B0 status */
11899db276f8SPeter Hurley 	if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1190a6afd9f3SGreg Kroah-Hartman 		info->MCR |= SER_DTR;
119197ef38b8SPeter Hurley 		if (!C_CRTSCTS(tty) || !tty_throttled(tty))
1192a6afd9f3SGreg Kroah-Hartman 			info->MCR |= SER_RTS;
1193a6afd9f3SGreg Kroah-Hartman 		local_irq_save(flags);
1194a6afd9f3SGreg Kroah-Hartman 		rtsdtr_ctrl(info->MCR);
1195a6afd9f3SGreg Kroah-Hartman 		local_irq_restore(flags);
1196a6afd9f3SGreg Kroah-Hartman 	}
1197a6afd9f3SGreg Kroah-Hartman 
1198a6afd9f3SGreg Kroah-Hartman 	/* Handle turning off CRTSCTS */
11999db276f8SPeter Hurley 	if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
1200035173c9SIlpo Järvinen 		tty->hw_stopped = false;
1201a6afd9f3SGreg Kroah-Hartman 		rs_start(tty);
1202a6afd9f3SGreg Kroah-Hartman 	}
1203a6afd9f3SGreg Kroah-Hartman 
1204a6afd9f3SGreg Kroah-Hartman #if 0
1205a6afd9f3SGreg Kroah-Hartman 	/*
1206a6afd9f3SGreg Kroah-Hartman 	 * No need to wake up processes in open wait, since they
1207a6afd9f3SGreg Kroah-Hartman 	 * sample the CLOCAL flag once, and don't recheck it.
1208a6afd9f3SGreg Kroah-Hartman 	 * XXX  It's not clear whether the current behavior is correct
1209a6afd9f3SGreg Kroah-Hartman 	 * or not.  Hence, this may change.....
1210a6afd9f3SGreg Kroah-Hartman 	 */
12119db276f8SPeter Hurley 	if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1212a6afd9f3SGreg Kroah-Hartman 		wake_up_interruptible(&info->open_wait);
1213a6afd9f3SGreg Kroah-Hartman #endif
1214a6afd9f3SGreg Kroah-Hartman }
1215a6afd9f3SGreg Kroah-Hartman 
1216a6afd9f3SGreg Kroah-Hartman /*
1217a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
1218a6afd9f3SGreg Kroah-Hartman  * rs_close()
1219a6afd9f3SGreg Kroah-Hartman  *
1220a6afd9f3SGreg Kroah-Hartman  * This routine is called when the serial port gets closed.  First, we
1221a6afd9f3SGreg Kroah-Hartman  * wait for the last remaining data to be sent.  Then, we unlink its
1222a6afd9f3SGreg Kroah-Hartman  * async structure from the interrupt chain if necessary, and we free
1223a6afd9f3SGreg Kroah-Hartman  * that IRQ if nothing is left in the chain.
1224a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
1225a6afd9f3SGreg Kroah-Hartman  */
rs_close(struct tty_struct * tty,struct file * filp)1226a6afd9f3SGreg Kroah-Hartman static void rs_close(struct tty_struct *tty, struct file * filp)
1227a6afd9f3SGreg Kroah-Hartman {
1228916b7656SJiri Slaby 	struct serial_state *state = tty->driver_data;
12297188dc20SJiri Slaby 	struct tty_port *port = &state->tport;
1230a6afd9f3SGreg Kroah-Hartman 
12319b937421SJiri Slaby 	if (tty_port_close_start(port, tty, filp) == 0)
1232a6afd9f3SGreg Kroah-Hartman 		return;
1233a6afd9f3SGreg Kroah-Hartman 
1234a6afd9f3SGreg Kroah-Hartman 	/*
1235a6afd9f3SGreg Kroah-Hartman 	 * At this point we stop accepting input.  To do this, we
1236a6afd9f3SGreg Kroah-Hartman 	 * disable the receive line status interrupts, and tell the
1237a6afd9f3SGreg Kroah-Hartman 	 * interrupt driver to stop checking the data ready bit in the
1238a6afd9f3SGreg Kroah-Hartman 	 * line status register.
1239a6afd9f3SGreg Kroah-Hartman 	 */
1240916b7656SJiri Slaby 	state->read_status_mask &= ~UART_LSR_DR;
1241d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
1242a6afd9f3SGreg Kroah-Hartman 	        /* disable receive interrupts */
124381680702SJiri Slaby 	        amiga_custom.intena = IF_RBF;
1244a6afd9f3SGreg Kroah-Hartman 		mb();
1245a6afd9f3SGreg Kroah-Hartman 		/* clear any pending receive interrupt */
124681680702SJiri Slaby 		amiga_custom.intreq = IF_RBF;
1247a6afd9f3SGreg Kroah-Hartman 		mb();
1248a6afd9f3SGreg Kroah-Hartman 
1249a6afd9f3SGreg Kroah-Hartman 		/*
1250a6afd9f3SGreg Kroah-Hartman 		 * Before we drop DTR, make sure the UART transmitter
1251a6afd9f3SGreg Kroah-Hartman 		 * has completely drained; this is especially
1252a6afd9f3SGreg Kroah-Hartman 		 * important if there is a transmit FIFO!
1253a6afd9f3SGreg Kroah-Hartman 		 */
1254916b7656SJiri Slaby 		rs_wait_until_sent(tty, state->timeout);
1255a6afd9f3SGreg Kroah-Hartman 	}
1256588993ddSJiri Slaby 	shutdown(tty, state);
1257a6afd9f3SGreg Kroah-Hartman 	rs_flush_buffer(tty);
1258a6afd9f3SGreg Kroah-Hartman 
1259a6afd9f3SGreg Kroah-Hartman 	tty_ldisc_flush(tty);
12607188dc20SJiri Slaby 	port->tty = NULL;
1261b8edebe4SJiri Slaby 
1262b8edebe4SJiri Slaby 	tty_port_close_end(port, tty);
1263a6afd9f3SGreg Kroah-Hartman }
1264a6afd9f3SGreg Kroah-Hartman 
1265a6afd9f3SGreg Kroah-Hartman /*
1266a6afd9f3SGreg Kroah-Hartman  * rs_wait_until_sent() --- wait until the transmitter is empty
1267a6afd9f3SGreg Kroah-Hartman  */
rs_wait_until_sent(struct tty_struct * tty,int timeout)1268a6afd9f3SGreg Kroah-Hartman static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1269a6afd9f3SGreg Kroah-Hartman {
1270916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
1271a6afd9f3SGreg Kroah-Hartman 	unsigned long orig_jiffies, char_time;
1272a6afd9f3SGreg Kroah-Hartman 	int lsr;
1273a6afd9f3SGreg Kroah-Hartman 
1274a6afd9f3SGreg Kroah-Hartman 	orig_jiffies = jiffies;
1275a6afd9f3SGreg Kroah-Hartman 
1276a6afd9f3SGreg Kroah-Hartman 	/*
1277a6afd9f3SGreg Kroah-Hartman 	 * Set the check interval to be 1/5 of the estimated time to
1278a6afd9f3SGreg Kroah-Hartman 	 * send a single character, and make it at least 1.  The check
1279a6afd9f3SGreg Kroah-Hartman 	 * interval should also be less than the timeout.
1280a6afd9f3SGreg Kroah-Hartman 	 *
1281a6afd9f3SGreg Kroah-Hartman 	 * Note: we have to use pretty tight timings here to satisfy
1282a6afd9f3SGreg Kroah-Hartman 	 * the NIST-PCTS.
1283a6afd9f3SGreg Kroah-Hartman 	 */
12845a7c7a6bSJiri Slaby 	char_time = (info->timeout - HZ/50) / XMIT_FIFO_SIZE;
1285a6afd9f3SGreg Kroah-Hartman 	char_time = char_time / 5;
1286a6afd9f3SGreg Kroah-Hartman 	if (char_time == 0)
1287a6afd9f3SGreg Kroah-Hartman 		char_time = 1;
1288a6afd9f3SGreg Kroah-Hartman 	if (timeout)
1289a6afd9f3SGreg Kroah-Hartman 	  char_time = min_t(unsigned long, char_time, timeout);
1290a6afd9f3SGreg Kroah-Hartman 	/*
1291a6afd9f3SGreg Kroah-Hartman 	 * If the transmitter hasn't cleared in twice the approximate
1292a6afd9f3SGreg Kroah-Hartman 	 * amount of time to send the entire FIFO, it probably won't
1293a6afd9f3SGreg Kroah-Hartman 	 * ever clear.  This assumes the UART isn't doing flow
1294a6afd9f3SGreg Kroah-Hartman 	 * control, which is currently the case.  Hence, if it ever
1295a6afd9f3SGreg Kroah-Hartman 	 * takes longer than info->timeout, this is probably due to a
1296a6afd9f3SGreg Kroah-Hartman 	 * UART bug of some kind.  So, we clamp the timeout parameter at
1297a6afd9f3SGreg Kroah-Hartman 	 * 2*info->timeout.
1298a6afd9f3SGreg Kroah-Hartman 	 */
1299a6afd9f3SGreg Kroah-Hartman 	if (!timeout || timeout > 2*info->timeout)
1300a6afd9f3SGreg Kroah-Hartman 		timeout = 2*info->timeout;
1301a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1302a6afd9f3SGreg Kroah-Hartman 	printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1303a6afd9f3SGreg Kroah-Hartman 	printk("jiff=%lu...", jiffies);
1304a6afd9f3SGreg Kroah-Hartman #endif
130581680702SJiri Slaby 	while(!((lsr = amiga_custom.serdatr) & SDR_TSRE)) {
1306a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1307a6afd9f3SGreg Kroah-Hartman 		printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1308a6afd9f3SGreg Kroah-Hartman #endif
1309a6afd9f3SGreg Kroah-Hartman 		msleep_interruptible(jiffies_to_msecs(char_time));
1310a6afd9f3SGreg Kroah-Hartman 		if (signal_pending(current))
1311a6afd9f3SGreg Kroah-Hartman 			break;
1312a6afd9f3SGreg Kroah-Hartman 		if (timeout && time_after(jiffies, orig_jiffies + timeout))
1313a6afd9f3SGreg Kroah-Hartman 			break;
1314a6afd9f3SGreg Kroah-Hartman 	}
1315a6afd9f3SGreg Kroah-Hartman 	__set_current_state(TASK_RUNNING);
1316eff4b0b9SJiri Slaby 
1317a6afd9f3SGreg Kroah-Hartman #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1318a6afd9f3SGreg Kroah-Hartman 	printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1319a6afd9f3SGreg Kroah-Hartman #endif
1320a6afd9f3SGreg Kroah-Hartman }
1321a6afd9f3SGreg Kroah-Hartman 
1322a6afd9f3SGreg Kroah-Hartman /*
1323a6afd9f3SGreg Kroah-Hartman  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1324a6afd9f3SGreg Kroah-Hartman  */
rs_hangup(struct tty_struct * tty)1325a6afd9f3SGreg Kroah-Hartman static void rs_hangup(struct tty_struct *tty)
1326a6afd9f3SGreg Kroah-Hartman {
1327916b7656SJiri Slaby 	struct serial_state *info = tty->driver_data;
1328a6afd9f3SGreg Kroah-Hartman 
1329a6afd9f3SGreg Kroah-Hartman 	rs_flush_buffer(tty);
1330588993ddSJiri Slaby 	shutdown(tty, info);
133112c80354SJiri Slaby 	info->tport.count = 0;
13329b5aa549SIlpo Järvinen 	tty_port_set_active(&info->tport, false);
133387758791SJiri Slaby 	info->tport.tty = NULL;
133487758791SJiri Slaby 	wake_up_interruptible(&info->tport.open_wait);
1335a6afd9f3SGreg Kroah-Hartman }
1336a6afd9f3SGreg Kroah-Hartman 
1337a6afd9f3SGreg Kroah-Hartman /*
1338a6afd9f3SGreg Kroah-Hartman  * This routine is called whenever a serial port is opened.  It
1339a6afd9f3SGreg Kroah-Hartman  * enables interrupts for a serial port, linking in its async structure into
1340a6afd9f3SGreg Kroah-Hartman  * the IRQ chain.   It also performs the serial-specific
1341a6afd9f3SGreg Kroah-Hartman  * initialization for the tty structure.
1342a6afd9f3SGreg Kroah-Hartman  */
rs_open(struct tty_struct * tty,struct file * filp)1343a6afd9f3SGreg Kroah-Hartman static int rs_open(struct tty_struct *tty, struct file * filp)
1344a6afd9f3SGreg Kroah-Hartman {
13457ec3114eSJiri Slaby 	struct tty_port *port = tty->port;
13467ec3114eSJiri Slaby 	struct serial_state *info = container_of(port, struct serial_state,
13477ec3114eSJiri Slaby 			tport);
1348410235fdSJiri Slaby 	int retval;
1349a6afd9f3SGreg Kroah-Hartman 
13507188dc20SJiri Slaby 	port->count++;
13517188dc20SJiri Slaby 	port->tty = tty;
1352916b7656SJiri Slaby 	tty->driver_data = info;
1353a6afd9f3SGreg Kroah-Hartman 
1354588993ddSJiri Slaby 	retval = startup(tty, info);
1355a6afd9f3SGreg Kroah-Hartman 	if (retval) {
1356a6afd9f3SGreg Kroah-Hartman 		return retval;
1357a6afd9f3SGreg Kroah-Hartman 	}
1358a6afd9f3SGreg Kroah-Hartman 
13596e1aeb03SJiri Slaby 	return tty_port_block_til_ready(port, tty, filp);
1360a6afd9f3SGreg Kroah-Hartman }
1361a6afd9f3SGreg Kroah-Hartman 
1362a6afd9f3SGreg Kroah-Hartman /*
1363a6afd9f3SGreg Kroah-Hartman  * /proc fs routines....
1364a6afd9f3SGreg Kroah-Hartman  */
1365a6afd9f3SGreg Kroah-Hartman 
line_info(struct seq_file * m,int line,struct serial_state * state)1366ff169e5cSJiri Slaby static inline void line_info(struct seq_file *m, int line,
1367ff169e5cSJiri Slaby 		struct serial_state *state)
1368a6afd9f3SGreg Kroah-Hartman {
1369a6afd9f3SGreg Kroah-Hartman 	char	stat_buf[30], control, status;
1370a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1371a6afd9f3SGreg Kroah-Hartman 
1372ff169e5cSJiri Slaby 	seq_printf(m, "%d: uart:amiga_builtin", line);
1373a6afd9f3SGreg Kroah-Hartman 
1374a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
1375a6afd9f3SGreg Kroah-Hartman 	status = ciab.pra;
1376d41861caSPeter Hurley 	control = tty_port_initialized(&state->tport) ? state->MCR : status;
1377a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
1378a6afd9f3SGreg Kroah-Hartman 
1379a6afd9f3SGreg Kroah-Hartman 	stat_buf[0] = 0;
1380a6afd9f3SGreg Kroah-Hartman 	stat_buf[1] = 0;
1381a6afd9f3SGreg Kroah-Hartman 	if(!(control & SER_RTS))
1382a6afd9f3SGreg Kroah-Hartman 		strcat(stat_buf, "|RTS");
1383a6afd9f3SGreg Kroah-Hartman 	if(!(status & SER_CTS))
1384a6afd9f3SGreg Kroah-Hartman 		strcat(stat_buf, "|CTS");
1385a6afd9f3SGreg Kroah-Hartman 	if(!(control & SER_DTR))
1386a6afd9f3SGreg Kroah-Hartman 		strcat(stat_buf, "|DTR");
1387a6afd9f3SGreg Kroah-Hartman 	if(!(status & SER_DSR))
1388a6afd9f3SGreg Kroah-Hartman 		strcat(stat_buf, "|DSR");
1389a6afd9f3SGreg Kroah-Hartman 	if(!(status & SER_DCD))
1390a6afd9f3SGreg Kroah-Hartman 		strcat(stat_buf, "|CD");
1391a6afd9f3SGreg Kroah-Hartman 
1392916b7656SJiri Slaby 	if (state->quot)
1393916b7656SJiri Slaby 		seq_printf(m, " baud:%d", state->baud_base / state->quot);
1394a6afd9f3SGreg Kroah-Hartman 
1395a6afd9f3SGreg Kroah-Hartman 	seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1396a6afd9f3SGreg Kroah-Hartman 
1397a6afd9f3SGreg Kroah-Hartman 	if (state->icount.frame)
1398a6afd9f3SGreg Kroah-Hartman 		seq_printf(m, " fe:%d", state->icount.frame);
1399a6afd9f3SGreg Kroah-Hartman 
1400a6afd9f3SGreg Kroah-Hartman 	if (state->icount.parity)
1401a6afd9f3SGreg Kroah-Hartman 		seq_printf(m, " pe:%d", state->icount.parity);
1402a6afd9f3SGreg Kroah-Hartman 
1403a6afd9f3SGreg Kroah-Hartman 	if (state->icount.brk)
1404a6afd9f3SGreg Kroah-Hartman 		seq_printf(m, " brk:%d", state->icount.brk);
1405a6afd9f3SGreg Kroah-Hartman 
1406a6afd9f3SGreg Kroah-Hartman 	if (state->icount.overrun)
1407a6afd9f3SGreg Kroah-Hartman 		seq_printf(m, " oe:%d", state->icount.overrun);
1408a6afd9f3SGreg Kroah-Hartman 
1409a6afd9f3SGreg Kroah-Hartman 	/*
1410a6afd9f3SGreg Kroah-Hartman 	 * Last thing is the RS-232 status lines
1411a6afd9f3SGreg Kroah-Hartman 	 */
1412a6afd9f3SGreg Kroah-Hartman 	seq_printf(m, " %s\n", stat_buf+1);
1413a6afd9f3SGreg Kroah-Hartman }
1414a6afd9f3SGreg Kroah-Hartman 
rs_proc_show(struct seq_file * m,void * v)1415a6afd9f3SGreg Kroah-Hartman static int rs_proc_show(struct seq_file *m, void *v)
1416a6afd9f3SGreg Kroah-Hartman {
14171cd25475SJiri Slaby 	seq_printf(m, "serinfo:1.0 driver:4.30\n");
14186cc7bda1SJiri Slaby 	line_info(m, 0, &serial_state);
1419a6afd9f3SGreg Kroah-Hartman 	return 0;
1420a6afd9f3SGreg Kroah-Hartman }
1421a6afd9f3SGreg Kroah-Hartman 
1422a6afd9f3SGreg Kroah-Hartman /*
1423a6afd9f3SGreg Kroah-Hartman  * ---------------------------------------------------------------------
1424a6afd9f3SGreg Kroah-Hartman  * rs_init() and friends
1425a6afd9f3SGreg Kroah-Hartman  *
1426a6afd9f3SGreg Kroah-Hartman  * rs_init() is called at boot-time to initialize the serial driver.
1427a6afd9f3SGreg Kroah-Hartman  * ---------------------------------------------------------------------
1428a6afd9f3SGreg Kroah-Hartman  */
1429a6afd9f3SGreg Kroah-Hartman 
1430a6afd9f3SGreg Kroah-Hartman static const struct tty_operations serial_ops = {
1431a6afd9f3SGreg Kroah-Hartman 	.open = rs_open,
1432a6afd9f3SGreg Kroah-Hartman 	.close = rs_close,
1433a6afd9f3SGreg Kroah-Hartman 	.write = rs_write,
1434a6afd9f3SGreg Kroah-Hartman 	.put_char = rs_put_char,
1435a6afd9f3SGreg Kroah-Hartman 	.flush_chars = rs_flush_chars,
1436a6afd9f3SGreg Kroah-Hartman 	.write_room = rs_write_room,
1437a6afd9f3SGreg Kroah-Hartman 	.chars_in_buffer = rs_chars_in_buffer,
1438a6afd9f3SGreg Kroah-Hartman 	.flush_buffer = rs_flush_buffer,
1439a6afd9f3SGreg Kroah-Hartman 	.ioctl = rs_ioctl,
1440a6afd9f3SGreg Kroah-Hartman 	.throttle = rs_throttle,
1441a6afd9f3SGreg Kroah-Hartman 	.unthrottle = rs_unthrottle,
1442a6afd9f3SGreg Kroah-Hartman 	.set_termios = rs_set_termios,
1443a6afd9f3SGreg Kroah-Hartman 	.stop = rs_stop,
1444a6afd9f3SGreg Kroah-Hartman 	.start = rs_start,
1445a6afd9f3SGreg Kroah-Hartman 	.hangup = rs_hangup,
1446a6afd9f3SGreg Kroah-Hartman 	.break_ctl = rs_break,
1447a6afd9f3SGreg Kroah-Hartman 	.send_xchar = rs_send_xchar,
1448a6afd9f3SGreg Kroah-Hartman 	.wait_until_sent = rs_wait_until_sent,
1449a6afd9f3SGreg Kroah-Hartman 	.tiocmget = rs_tiocmget,
1450a6afd9f3SGreg Kroah-Hartman 	.tiocmset = rs_tiocmset,
1451a6afd9f3SGreg Kroah-Hartman 	.get_icount = rs_get_icount,
1452b129cbc9SAl Viro 	.set_serial = set_serial_info,
1453b129cbc9SAl Viro 	.get_serial = get_serial_info,
14548a8dcabfSChristoph Hellwig 	.proc_show = rs_proc_show,
1455a6afd9f3SGreg Kroah-Hartman };
1456a6afd9f3SGreg Kroah-Hartman 
amiga_carrier_raised(struct tty_port * port)1457b300fb26SIlpo Järvinen static bool amiga_carrier_raised(struct tty_port *port)
1458f1166604SJiri Slaby {
1459f1166604SJiri Slaby 	return !(ciab.pra & SER_DCD);
1460f1166604SJiri Slaby }
1461f1166604SJiri Slaby 
amiga_dtr_rts(struct tty_port * port,bool active)14625701cb8bSIlpo Järvinen static void amiga_dtr_rts(struct tty_port *port, bool active)
1463f1166604SJiri Slaby {
1464f1166604SJiri Slaby 	struct serial_state *info = container_of(port, struct serial_state,
1465f1166604SJiri Slaby 			tport);
1466f1166604SJiri Slaby 	unsigned long flags;
1467f1166604SJiri Slaby 
14685701cb8bSIlpo Järvinen 	if (active)
1469f1166604SJiri Slaby 		info->MCR |= SER_DTR|SER_RTS;
1470f1166604SJiri Slaby 	else
1471f1166604SJiri Slaby 		info->MCR &= ~(SER_DTR|SER_RTS);
1472f1166604SJiri Slaby 
1473f1166604SJiri Slaby 	local_irq_save(flags);
1474f1166604SJiri Slaby 	rtsdtr_ctrl(info->MCR);
1475f1166604SJiri Slaby 	local_irq_restore(flags);
1476f1166604SJiri Slaby }
1477f1166604SJiri Slaby 
1478f1166604SJiri Slaby static const struct tty_port_operations amiga_port_ops = {
1479f1166604SJiri Slaby 	.carrier_raised = amiga_carrier_raised,
1480f1166604SJiri Slaby 	.dtr_rts = amiga_dtr_rts,
1481f1166604SJiri Slaby };
1482f1166604SJiri Slaby 
1483a6afd9f3SGreg Kroah-Hartman /*
1484a6afd9f3SGreg Kroah-Hartman  * The serial driver boot-time initialization code!
1485a6afd9f3SGreg Kroah-Hartman  */
amiga_serial_probe(struct platform_device * pdev)1486a6afd9f3SGreg Kroah-Hartman static int __init amiga_serial_probe(struct platform_device *pdev)
1487a6afd9f3SGreg Kroah-Hartman {
14886cc7bda1SJiri Slaby 	struct serial_state *state = &serial_state;
14890524513aSJiri Slaby 	struct tty_driver *driver;
1490a6afd9f3SGreg Kroah-Hartman 	unsigned long flags;
1491a6afd9f3SGreg Kroah-Hartman 	int error;
1492a6afd9f3SGreg Kroah-Hartman 
149339b7b42bSJiri Slaby 	driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
149439b7b42bSJiri Slaby 	if (IS_ERR(driver))
149539b7b42bSJiri Slaby 		return PTR_ERR(driver);
1496a6afd9f3SGreg Kroah-Hartman 
1497a6afd9f3SGreg Kroah-Hartman 	/* Initialize the tty_driver structure */
1498a6afd9f3SGreg Kroah-Hartman 
14990524513aSJiri Slaby 	driver->driver_name = "amiserial";
15000524513aSJiri Slaby 	driver->name = "ttyS";
15010524513aSJiri Slaby 	driver->major = TTY_MAJOR;
15020524513aSJiri Slaby 	driver->minor_start = 64;
15030524513aSJiri Slaby 	driver->type = TTY_DRIVER_TYPE_SERIAL;
15040524513aSJiri Slaby 	driver->subtype = SERIAL_TYPE_NORMAL;
15050524513aSJiri Slaby 	driver->init_termios = tty_std_termios;
15060524513aSJiri Slaby 	driver->init_termios.c_cflag =
1507a6afd9f3SGreg Kroah-Hartman 		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
15080524513aSJiri Slaby 	tty_set_operations(driver, &serial_ops);
1509a6afd9f3SGreg Kroah-Hartman 
15105d4317abSJiri Slaby 	memset(state, 0, sizeof(*state));
151181680702SJiri Slaby 	state->port = (int)&amiga_custom.serdatr; /* Just to give it a value */
151287758791SJiri Slaby 	tty_port_init(&state->tport);
1513f1166604SJiri Slaby 	state->tport.ops = &amiga_port_ops;
15140524513aSJiri Slaby 	tty_port_link_device(&state->tport, driver, 0);
1515b19e2ca7SJiri Slaby 
15160524513aSJiri Slaby 	error = tty_register_driver(driver);
1517b19e2ca7SJiri Slaby 	if (error)
15189f90a4ddSJiri Slaby 		goto fail_tty_driver_kref_put;
1519a6afd9f3SGreg Kroah-Hartman 
1520ff169e5cSJiri Slaby 	printk(KERN_INFO "ttyS0 is the amiga builtin serial port\n");
1521a6afd9f3SGreg Kroah-Hartman 
1522a6afd9f3SGreg Kroah-Hartman 	/* Hardware set up */
1523a6afd9f3SGreg Kroah-Hartman 
1524a6afd9f3SGreg Kroah-Hartman 	state->baud_base = amiga_colorclock;
1525a6afd9f3SGreg Kroah-Hartman 
1526a6afd9f3SGreg Kroah-Hartman 	/* set ISRs, and then disable the rx interrupts */
1527a6afd9f3SGreg Kroah-Hartman 	error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1528a6afd9f3SGreg Kroah-Hartman 	if (error)
1529a6afd9f3SGreg Kroah-Hartman 		goto fail_unregister;
1530a6afd9f3SGreg Kroah-Hartman 
15319cfb5c05SYong Zhang 	error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
1532a6afd9f3SGreg Kroah-Hartman 			    "serial RX", state);
1533a6afd9f3SGreg Kroah-Hartman 	if (error)
1534a6afd9f3SGreg Kroah-Hartman 		goto fail_free_irq;
1535a6afd9f3SGreg Kroah-Hartman 
1536a6afd9f3SGreg Kroah-Hartman 	local_irq_save(flags);
1537a6afd9f3SGreg Kroah-Hartman 
1538a6afd9f3SGreg Kroah-Hartman 	/* turn off Rx and Tx interrupts */
153981680702SJiri Slaby 	amiga_custom.intena = IF_RBF | IF_TBE;
1540a6afd9f3SGreg Kroah-Hartman 	mb();
1541a6afd9f3SGreg Kroah-Hartman 
1542a6afd9f3SGreg Kroah-Hartman 	/* clear any pending interrupt */
154381680702SJiri Slaby 	amiga_custom.intreq = IF_RBF | IF_TBE;
1544a6afd9f3SGreg Kroah-Hartman 	mb();
1545a6afd9f3SGreg Kroah-Hartman 
1546a6afd9f3SGreg Kroah-Hartman 	local_irq_restore(flags);
1547a6afd9f3SGreg Kroah-Hartman 
1548a6afd9f3SGreg Kroah-Hartman 	/*
1549a6afd9f3SGreg Kroah-Hartman 	 * set the appropriate directions for the modem control flags,
1550a6afd9f3SGreg Kroah-Hartman 	 * and clear RTS and DTR
1551a6afd9f3SGreg Kroah-Hartman 	 */
1552a6afd9f3SGreg Kroah-Hartman 	ciab.ddra |= (SER_DTR | SER_RTS);   /* outputs */
1553a6afd9f3SGreg Kroah-Hartman 	ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR);  /* inputs */
1554a6afd9f3SGreg Kroah-Hartman 
1555a6afd9f3SGreg Kroah-Hartman 	platform_set_drvdata(pdev, state);
1556a6afd9f3SGreg Kroah-Hartman 
15570524513aSJiri Slaby 	serial_driver = driver;
15580524513aSJiri Slaby 
1559a6afd9f3SGreg Kroah-Hartman 	return 0;
1560a6afd9f3SGreg Kroah-Hartman 
1561a6afd9f3SGreg Kroah-Hartman fail_free_irq:
1562a6afd9f3SGreg Kroah-Hartman 	free_irq(IRQ_AMIGA_TBE, state);
1563a6afd9f3SGreg Kroah-Hartman fail_unregister:
15640524513aSJiri Slaby 	tty_unregister_driver(driver);
15659f90a4ddSJiri Slaby fail_tty_driver_kref_put:
1566191c5f10SJiri Slaby 	tty_port_destroy(&state->tport);
15679f90a4ddSJiri Slaby 	tty_driver_kref_put(driver);
1568a6afd9f3SGreg Kroah-Hartman 	return error;
1569a6afd9f3SGreg Kroah-Hartman }
1570a6afd9f3SGreg Kroah-Hartman 
amiga_serial_remove(struct platform_device * pdev)1571a6afd9f3SGreg Kroah-Hartman static int __exit amiga_serial_remove(struct platform_device *pdev)
1572a6afd9f3SGreg Kroah-Hartman {
1573a6afd9f3SGreg Kroah-Hartman 	struct serial_state *state = platform_get_drvdata(pdev);
1574a6afd9f3SGreg Kroah-Hartman 
15756c2e6317SJiri Slaby 	tty_unregister_driver(serial_driver);
15769f90a4ddSJiri Slaby 	tty_driver_kref_put(serial_driver);
1577191c5f10SJiri Slaby 	tty_port_destroy(&state->tport);
1578a6afd9f3SGreg Kroah-Hartman 
1579916b7656SJiri Slaby 	free_irq(IRQ_AMIGA_TBE, state);
1580916b7656SJiri Slaby 	free_irq(IRQ_AMIGA_RBF, state);
1581a6afd9f3SGreg Kroah-Hartman 
15826c2e6317SJiri Slaby 	return 0;
1583a6afd9f3SGreg Kroah-Hartman }
1584a6afd9f3SGreg Kroah-Hartman 
1585a6afd9f3SGreg Kroah-Hartman static struct platform_driver amiga_serial_driver = {
1586a6afd9f3SGreg Kroah-Hartman 	.remove = __exit_p(amiga_serial_remove),
1587a6afd9f3SGreg Kroah-Hartman 	.driver   = {
1588a6afd9f3SGreg Kroah-Hartman 		.name	= "amiga-serial",
1589a6afd9f3SGreg Kroah-Hartman 	},
1590a6afd9f3SGreg Kroah-Hartman };
1591a6afd9f3SGreg Kroah-Hartman 
1592fec6bee3SJingoo Han module_platform_driver_probe(amiga_serial_driver, amiga_serial_probe);
1593a6afd9f3SGreg Kroah-Hartman 
1594a6afd9f3SGreg Kroah-Hartman 
1595a6afd9f3SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
1596a6afd9f3SGreg Kroah-Hartman 
1597a6afd9f3SGreg Kroah-Hartman /*
1598a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
1599a6afd9f3SGreg Kroah-Hartman  * Serial console driver
1600a6afd9f3SGreg Kroah-Hartman  * ------------------------------------------------------------
1601a6afd9f3SGreg Kroah-Hartman  */
1602a6afd9f3SGreg Kroah-Hartman 
amiga_serial_putc(char c)1603a6afd9f3SGreg Kroah-Hartman static void amiga_serial_putc(char c)
1604a6afd9f3SGreg Kroah-Hartman {
160581680702SJiri Slaby 	amiga_custom.serdat = (unsigned char)c | 0x100;
160681680702SJiri Slaby 	while (!(amiga_custom.serdatr & 0x2000))
1607a6afd9f3SGreg Kroah-Hartman 		barrier();
1608a6afd9f3SGreg Kroah-Hartman }
1609a6afd9f3SGreg Kroah-Hartman 
1610a6afd9f3SGreg Kroah-Hartman /*
1611a6afd9f3SGreg Kroah-Hartman  *	Print a string to the serial port trying not to disturb
1612a6afd9f3SGreg Kroah-Hartman  *	any possible real use of the port...
1613a6afd9f3SGreg Kroah-Hartman  *
1614a6afd9f3SGreg Kroah-Hartman  *	The console must be locked when we get here.
1615a6afd9f3SGreg Kroah-Hartman  */
serial_console_write(struct console * co,const char * s,unsigned count)1616a6afd9f3SGreg Kroah-Hartman static void serial_console_write(struct console *co, const char *s,
1617a6afd9f3SGreg Kroah-Hartman 				unsigned count)
1618a6afd9f3SGreg Kroah-Hartman {
161981680702SJiri Slaby 	unsigned short intena = amiga_custom.intenar;
1620a6afd9f3SGreg Kroah-Hartman 
162181680702SJiri Slaby 	amiga_custom.intena = IF_TBE;
1622a6afd9f3SGreg Kroah-Hartman 
1623a6afd9f3SGreg Kroah-Hartman 	while (count--) {
1624a6afd9f3SGreg Kroah-Hartman 		if (*s == '\n')
1625a6afd9f3SGreg Kroah-Hartman 			amiga_serial_putc('\r');
1626a6afd9f3SGreg Kroah-Hartman 		amiga_serial_putc(*s++);
1627a6afd9f3SGreg Kroah-Hartman 	}
1628a6afd9f3SGreg Kroah-Hartman 
162981680702SJiri Slaby 	amiga_custom.intena = IF_SETCLR | (intena & IF_TBE);
1630a6afd9f3SGreg Kroah-Hartman }
1631a6afd9f3SGreg Kroah-Hartman 
serial_console_device(struct console * c,int * index)1632a6afd9f3SGreg Kroah-Hartman static struct tty_driver *serial_console_device(struct console *c, int *index)
1633a6afd9f3SGreg Kroah-Hartman {
1634a6afd9f3SGreg Kroah-Hartman 	*index = 0;
1635a6afd9f3SGreg Kroah-Hartman 	return serial_driver;
1636a6afd9f3SGreg Kroah-Hartman }
1637a6afd9f3SGreg Kroah-Hartman 
1638a6afd9f3SGreg Kroah-Hartman static struct console sercons = {
1639a6afd9f3SGreg Kroah-Hartman 	.name =		"ttyS",
1640a6afd9f3SGreg Kroah-Hartman 	.write =	serial_console_write,
1641a6afd9f3SGreg Kroah-Hartman 	.device =	serial_console_device,
1642a6afd9f3SGreg Kroah-Hartman 	.flags =	CON_PRINTBUFFER,
1643a6afd9f3SGreg Kroah-Hartman 	.index =	-1,
1644a6afd9f3SGreg Kroah-Hartman };
1645a6afd9f3SGreg Kroah-Hartman 
1646a6afd9f3SGreg Kroah-Hartman /*
1647a6afd9f3SGreg Kroah-Hartman  *	Register console.
1648a6afd9f3SGreg Kroah-Hartman  */
amiserial_console_init(void)1649a6afd9f3SGreg Kroah-Hartman static int __init amiserial_console_init(void)
1650a6afd9f3SGreg Kroah-Hartman {
16513dcf344bSGeert Uytterhoeven 	if (!MACH_IS_AMIGA)
16523dcf344bSGeert Uytterhoeven 		return -ENODEV;
16533dcf344bSGeert Uytterhoeven 
1654a6afd9f3SGreg Kroah-Hartman 	register_console(&sercons);
1655a6afd9f3SGreg Kroah-Hartman 	return 0;
1656a6afd9f3SGreg Kroah-Hartman }
1657a6afd9f3SGreg Kroah-Hartman console_initcall(amiserial_console_init);
1658a6afd9f3SGreg Kroah-Hartman 
1659a6afd9f3SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
1660a6afd9f3SGreg Kroah-Hartman 
1661a6afd9f3SGreg Kroah-Hartman MODULE_LICENSE("GPL");
1662a6afd9f3SGreg Kroah-Hartman MODULE_ALIAS("platform:amiga-serial");
1663