blob: b182bccf3eab681b1f0f26e65ef02be62eab3fd4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Serial driver for the amiga builtin port.
3 *
4 * This code was created by taking serial.c version 4.30 from kernel
5 * release 2.3.22, replacing all hardware related stuff with the
6 * corresponding amiga hardware actions, and removing all irrelevant
7 * code. As a consequence, it uses many of the constants and names
8 * associated with the registers and bits of 16550 compatible UARTS -
9 * but only to keep track of status, etc in the state variables. It
10 * was done this was to make it easier to keep the code in line with
11 * (non hardware specific) changes to serial.c.
12 *
13 * The port is registered with the tty driver as minor device 64, and
14 * therefore other ports should should only use 65 upwards.
15 *
16 * Richard Lucock 28/12/99
17 *
18 * Copyright (C) 1991, 1992 Linus Torvalds
19 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
20 * 1998, 1999 Theodore Ts'o
21 *
22 */
23
24/*
25 * Serial driver configuration section. Here are the various options:
26 *
27 * SERIAL_PARANOIA_CHECK
28 * Check the magic number for the async_structure where
29 * ever possible.
30 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/delay.h>
33
34#undef SERIAL_PARANOIA_CHECK
35#define SERIAL_DO_RESTART
36
37/* Set of debugging defines */
38
39#undef SERIAL_DEBUG_INTR
40#undef SERIAL_DEBUG_OPEN
41#undef SERIAL_DEBUG_FLOW
42#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
43
44/* Sanity checks */
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
47#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
Jiri Slaby01bd7302012-03-05 14:52:27 +010048 tty->name, (info->tport.flags), serial_driver->refcount,info->count,tty->count,s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#else
50#define DBG_CNT(s)
51#endif
52
53/*
54 * End of serial driver configuration section.
55 */
56
57#include <linux/module.h>
58
59#include <linux/types.h>
60#include <linux/serial.h>
61#include <linux/serialP.h>
62#include <linux/serial_reg.h>
63static char *serial_version = "4.30";
64
65#include <linux/errno.h>
66#include <linux/signal.h>
67#include <linux/sched.h>
68#include <linux/kernel.h>
69#include <linux/timer.h>
70#include <linux/interrupt.h>
71#include <linux/tty.h>
72#include <linux/tty_flip.h>
73#include <linux/console.h>
74#include <linux/major.h>
75#include <linux/string.h>
76#include <linux/fcntl.h>
77#include <linux/ptrace.h>
78#include <linux/ioport.h>
79#include <linux/mm.h>
Alexey Dobriyand5940272009-03-31 15:19:23 -070080#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/slab.h>
82#include <linux/init.h>
83#include <linux/bitops.h>
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +020084#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#include <asm/setup.h>
87
88#include <asm/system.h>
89
90#include <asm/irq.h>
91
92#include <asm/amigahw.h>
93#include <asm/amigaints.h>
94
Al Virob4290a22006-01-12 01:06:12 -080095#define custom amiga_custom
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static char *serial_name = "Amiga-builtin serial driver";
97
98static struct tty_driver *serial_driver;
99
100/* number of characters left in xmit buffer before we ask for more */
101#define WAKEUP_CHARS 256
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static unsigned char current_ctl_bits;
104
Jiri Slaby588993d2012-03-05 14:52:22 +0100105static void change_speed(struct tty_struct *tty, struct serial_state *info,
106 struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
108
109
110static struct serial_state rs_table[1];
111
Tobias Klauserfe971072006-01-09 20:54:02 -0800112#define NR_PORTS ARRAY_SIZE(rs_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <asm/uaccess.h>
115
116#define serial_isroot() (capable(CAP_SYS_ADMIN))
117
118
Jiri Slaby916b7652012-03-05 14:52:20 +0100119static inline int serial_paranoia_check(struct serial_state *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 char *name, const char *routine)
121{
122#ifdef SERIAL_PARANOIA_CHECK
123 static const char *badmagic =
124 "Warning: bad magic number for serial struct (%s) in %s\n";
125 static const char *badinfo =
126 "Warning: null async_struct for (%s) in %s\n";
127
128 if (!info) {
129 printk(badinfo, name, routine);
130 return 1;
131 }
132 if (info->magic != SERIAL_MAGIC) {
133 printk(badmagic, name, routine);
134 return 1;
135 }
136#endif
137 return 0;
138}
139
140/* some serial hardware definitions */
141#define SDR_OVRUN (1<<15)
142#define SDR_RBF (1<<14)
143#define SDR_TBE (1<<13)
144#define SDR_TSRE (1<<12)
145
146#define SERPER_PARENB (1<<15)
147
148#define AC_SETCLR (1<<15)
149#define AC_UARTBRK (1<<11)
150
151#define SER_DTR (1<<7)
152#define SER_RTS (1<<6)
153#define SER_DCD (1<<5)
154#define SER_CTS (1<<4)
155#define SER_DSR (1<<3)
156
157static __inline__ void rtsdtr_ctrl(int bits)
158{
159 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
160}
161
162/*
163 * ------------------------------------------------------------
164 * rs_stop() and rs_start()
165 *
166 * This routines are called before setting or resetting tty->stopped.
167 * They enable or disable transmitter interrupts, as necessary.
168 * ------------------------------------------------------------
169 */
170static void rs_stop(struct tty_struct *tty)
171{
Jiri Slaby916b7652012-03-05 14:52:20 +0100172 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned long flags;
174
175 if (serial_paranoia_check(info, tty->name, "rs_stop"))
176 return;
177
178 local_irq_save(flags);
179 if (info->IER & UART_IER_THRI) {
180 info->IER &= ~UART_IER_THRI;
181 /* disable Tx interrupt and remove any pending interrupts */
182 custom.intena = IF_TBE;
183 mb();
184 custom.intreq = IF_TBE;
185 mb();
186 }
187 local_irq_restore(flags);
188}
189
190static void rs_start(struct tty_struct *tty)
191{
Jiri Slaby916b7652012-03-05 14:52:20 +0100192 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unsigned long flags;
194
195 if (serial_paranoia_check(info, tty->name, "rs_start"))
196 return;
197
198 local_irq_save(flags);
199 if (info->xmit.head != info->xmit.tail
200 && info->xmit.buf
201 && !(info->IER & UART_IER_THRI)) {
202 info->IER |= UART_IER_THRI;
203 custom.intena = IF_SETCLR | IF_TBE;
204 mb();
205 /* set a pending Tx Interrupt, transmitter should restart now */
206 custom.intreq = IF_SETCLR | IF_TBE;
207 mb();
208 }
209 local_irq_restore(flags);
210}
211
212/*
213 * ----------------------------------------------------------------------
214 *
215 * Here starts the interrupt handling routines. All of the following
216 * subroutines are declared as inline and are folded into
217 * rs_interrupt(). They were separated out for readability's sake.
218 *
219 * Note: rs_interrupt() is a "fast" interrupt, which means that it
220 * runs with interrupts turned off. People who may want to modify
221 * rs_interrupt() should try to keep the interrupt handler as fast as
222 * possible. After you are done making modifications, it is not a bad
223 * idea to do:
224 *
225 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
226 *
227 * and look at the resulting assemble code in serial.s.
228 *
229 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
230 * -----------------------------------------------------------------------
231 */
232
Jiri Slaby916b7652012-03-05 14:52:20 +0100233static void receive_chars(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int status;
236 int serdatr;
Jiri Slaby87758792012-03-05 14:52:24 +0100237 struct tty_struct *tty = info->tport.tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800238 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 struct async_icount *icount;
Alan Cox33f0f882006-01-09 20:54:13 -0800240 int oe = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jiri Slaby916b7652012-03-05 14:52:20 +0100242 icount = &info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 status = UART_LSR_DR; /* We obviously have a character! */
245 serdatr = custom.serdatr;
246 mb();
247 custom.intreq = IF_RBF;
248 mb();
249
250 if((serdatr & 0x1ff) == 0)
251 status |= UART_LSR_BI;
252 if(serdatr & SDR_OVRUN)
253 status |= UART_LSR_OE;
254
255 ch = serdatr & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 icount->rx++;
257
258#ifdef SERIAL_DEBUG_INTR
259 printk("DR%02x:%02x...", ch, status);
260#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800261 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /*
264 * We don't handle parity or frame errors - but I have left
265 * the code in, since I'm not sure that the errors can't be
266 * detected.
267 */
268
269 if (status & (UART_LSR_BI | UART_LSR_PE |
270 UART_LSR_FE | UART_LSR_OE)) {
271 /*
272 * For statistics only
273 */
274 if (status & UART_LSR_BI) {
275 status &= ~(UART_LSR_FE | UART_LSR_PE);
276 icount->brk++;
277 } else if (status & UART_LSR_PE)
278 icount->parity++;
279 else if (status & UART_LSR_FE)
280 icount->frame++;
281 if (status & UART_LSR_OE)
282 icount->overrun++;
283
284 /*
285 * Now check to see if character should be
286 * ignored, and mask off conditions which
287 * should be ignored.
288 */
289 if (status & info->ignore_status_mask)
Alan Cox33f0f882006-01-09 20:54:13 -0800290 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 status &= info->read_status_mask;
293
294 if (status & (UART_LSR_BI)) {
295#ifdef SERIAL_DEBUG_INTR
296 printk("handling break....");
297#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800298 flag = TTY_BREAK;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100299 if (info->tport.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 do_SAK(tty);
301 } else if (status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800302 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 else if (status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800304 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (status & UART_LSR_OE) {
306 /*
307 * Overrun is special, since it's
308 * reported immediately, and doesn't
309 * affect the current character
310 */
Alan Cox33f0f882006-01-09 20:54:13 -0800311 oe = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 }
Alan Cox33f0f882006-01-09 20:54:13 -0800314 tty_insert_flip_char(tty, ch, flag);
315 if (oe == 1)
316 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 tty_flip_buffer_push(tty);
Alan Cox33f0f882006-01-09 20:54:13 -0800318out:
319 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Jiri Slaby916b7652012-03-05 14:52:20 +0100322static void transmit_chars(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 custom.intreq = IF_TBE;
325 mb();
326 if (info->x_char) {
327 custom.serdat = info->x_char | 0x100;
328 mb();
Jiri Slaby916b7652012-03-05 14:52:20 +0100329 info->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 info->x_char = 0;
331 return;
332 }
333 if (info->xmit.head == info->xmit.tail
Jiri Slaby87758792012-03-05 14:52:24 +0100334 || info->tport.tty->stopped
335 || info->tport.tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 info->IER &= ~UART_IER_THRI;
337 custom.intena = IF_TBE;
338 mb();
339 return;
340 }
341
342 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
343 mb();
344 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
Jiri Slaby916b7652012-03-05 14:52:20 +0100345 info->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (CIRC_CNT(info->xmit.head,
348 info->xmit.tail,
349 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
Jiri Slaby87758792012-03-05 14:52:24 +0100350 tty_wakeup(info->tport.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352#ifdef SERIAL_DEBUG_INTR
353 printk("THRE...");
354#endif
355 if (info->xmit.head == info->xmit.tail) {
356 custom.intena = IF_TBE;
357 mb();
358 info->IER &= ~UART_IER_THRI;
359 }
360}
361
Jiri Slaby916b7652012-03-05 14:52:20 +0100362static void check_modem_status(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Jiri Slaby7188dc22012-03-05 14:52:43 +0100364 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
366 unsigned char dstatus;
367 struct async_icount *icount;
368
369 /* Determine bits that have changed */
370 dstatus = status ^ current_ctl_bits;
371 current_ctl_bits = status;
372
373 if (dstatus) {
Jiri Slaby916b7652012-03-05 14:52:20 +0100374 icount = &info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* update input line counters */
376 if (dstatus & SER_DSR)
377 icount->dsr++;
378 if (dstatus & SER_DCD) {
379 icount->dcd++;
380#ifdef CONFIG_HARD_PPS
Jiri Slaby7188dc22012-03-05 14:52:43 +0100381 if ((port->flags & ASYNC_HARDPPS_CD) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 !(status & SER_DCD))
383 hardpps();
384#endif
385 }
386 if (dstatus & SER_CTS)
387 icount->cts++;
Jiri Slaby7188dc22012-03-05 14:52:43 +0100388 wake_up_interruptible(&port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
Jiri Slaby7188dc22012-03-05 14:52:43 +0100391 if ((port->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
393 printk("ttyS%d CD now %s...", info->line,
394 (!(status & SER_DCD)) ? "on" : "off");
395#endif
396 if (!(status & SER_DCD))
Jiri Slaby7188dc22012-03-05 14:52:43 +0100397 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 else {
399#ifdef SERIAL_DEBUG_OPEN
400 printk("doing serial hangup...");
401#endif
Jiri Slaby7188dc22012-03-05 14:52:43 +0100402 if (port->tty)
403 tty_hangup(port->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 }
Jiri Slaby7188dc22012-03-05 14:52:43 +0100406 if (port->flags & ASYNC_CTS_FLOW) {
407 if (port->tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (!(status & SER_CTS)) {
409#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
410 printk("CTS tx start...");
411#endif
Jiri Slaby7188dc22012-03-05 14:52:43 +0100412 port->tty->hw_stopped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 info->IER |= UART_IER_THRI;
414 custom.intena = IF_SETCLR | IF_TBE;
415 mb();
416 /* set a pending Tx Interrupt, transmitter should restart now */
417 custom.intreq = IF_SETCLR | IF_TBE;
418 mb();
Jiri Slaby7188dc22012-03-05 14:52:43 +0100419 tty_wakeup(port->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return;
421 }
422 } else {
423 if ((status & SER_CTS)) {
424#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
425 printk("CTS tx stop...");
426#endif
Jiri Slaby7188dc22012-03-05 14:52:43 +0100427 port->tty->hw_stopped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 info->IER &= ~UART_IER_THRI;
429 /* disable Tx interrupt and remove any pending interrupts */
430 custom.intena = IF_TBE;
431 mb();
432 custom.intreq = IF_TBE;
433 mb();
434 }
435 }
436 }
437}
438
David Howells7d12e782006-10-05 14:55:46 +0100439static irqreturn_t ser_vbl_int( int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 /* vbl is just a periodic interrupt we tie into to update modem status */
Jiri Slaby916b7652012-03-05 14:52:20 +0100442 struct serial_state *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
444 * TBD - is it better to unregister from this interrupt or to
445 * ignore it if MSI is clear ?
446 */
447 if(info->IER & UART_IER_MSI)
448 check_modem_status(info);
449 return IRQ_HANDLED;
450}
451
David Howells7d12e782006-10-05 14:55:46 +0100452static irqreturn_t ser_rx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Jiri Slaby916b7652012-03-05 14:52:20 +0100454 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456#ifdef SERIAL_DEBUG_INTR
457 printk("ser_rx_int...");
458#endif
459
Jiri Slaby87758792012-03-05 14:52:24 +0100460 if (!info->tport.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return IRQ_NONE;
462
463 receive_chars(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464#ifdef SERIAL_DEBUG_INTR
465 printk("end.\n");
466#endif
467 return IRQ_HANDLED;
468}
469
David Howells7d12e782006-10-05 14:55:46 +0100470static irqreturn_t ser_tx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Jiri Slaby916b7652012-03-05 14:52:20 +0100472 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 if (custom.serdatr & SDR_TBE) {
475#ifdef SERIAL_DEBUG_INTR
476 printk("ser_tx_int...");
477#endif
478
Jiri Slaby87758792012-03-05 14:52:24 +0100479 if (!info->tport.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return IRQ_NONE;
481
482 transmit_chars(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#ifdef SERIAL_DEBUG_INTR
484 printk("end.\n");
485#endif
486 }
487 return IRQ_HANDLED;
488}
489
490/*
491 * -------------------------------------------------------------------
492 * Here ends the serial interrupt routines.
493 * -------------------------------------------------------------------
494 */
495
496/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * ---------------------------------------------------------------
498 * Low level utility subroutines for the serial driver: routines to
499 * figure out the appropriate timeout for an interrupt chain, routines
500 * to initialize and startup a serial port, and routines to shutdown a
501 * serial port. Useful stuff like that.
502 * ---------------------------------------------------------------
503 */
504
Jiri Slaby588993d2012-03-05 14:52:22 +0100505static int startup(struct tty_struct *tty, struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Jiri Slaby01bd7302012-03-05 14:52:27 +0100507 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 unsigned long flags;
509 int retval=0;
510 unsigned long page;
511
512 page = get_zeroed_page(GFP_KERNEL);
513 if (!page)
514 return -ENOMEM;
515
516 local_irq_save(flags);
517
Jiri Slaby01bd7302012-03-05 14:52:27 +0100518 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 free_page(page);
520 goto errout;
521 }
522
523 if (info->xmit.buf)
524 free_page(page);
525 else
526 info->xmit.buf = (unsigned char *) page;
527
528#ifdef SERIAL_DEBUG_OPEN
529 printk("starting up ttys%d ...", info->line);
530#endif
531
532 /* Clear anything in the input buffer */
533
534 custom.intreq = IF_RBF;
535 mb();
536
537 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
538 if (retval) {
539 if (serial_isroot()) {
Jiri Slaby588993d2012-03-05 14:52:22 +0100540 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 retval = 0;
542 }
543 goto errout;
544 }
545
546 /* enable both Rx and Tx interrupts */
547 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
548 mb();
549 info->IER = UART_IER_MSI;
550
551 /* remember current state of the DCD and CTS bits */
552 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 info->MCR = 0;
Jiri Slaby588993d2012-03-05 14:52:22 +0100555 if (C_BAUD(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 info->MCR = SER_DTR | SER_RTS;
557 rtsdtr_ctrl(info->MCR);
558
Jiri Slaby588993d2012-03-05 14:52:22 +0100559 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 info->xmit.head = info->xmit.tail = 0;
561
562 /*
563 * Set up the tty->alt_speed kludge
564 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100565 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby588993d2012-03-05 14:52:22 +0100566 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100567 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby588993d2012-03-05 14:52:22 +0100568 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100569 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby588993d2012-03-05 14:52:22 +0100570 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100571 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby588993d2012-03-05 14:52:22 +0100572 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /*
575 * and set the speed of the serial port
576 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100577 change_speed(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Jiri Slaby01bd7302012-03-05 14:52:27 +0100579 port->flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 local_irq_restore(flags);
581 return 0;
582
583errout:
584 local_irq_restore(flags);
585 return retval;
586}
587
588/*
589 * This routine will shutdown a serial port; interrupts are disabled, and
590 * DTR is dropped if the hangup on close termio flag is on.
591 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100592static void shutdown(struct tty_struct *tty, struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 unsigned long flags;
595 struct serial_state *state;
596
Jiri Slaby01bd7302012-03-05 14:52:27 +0100597 if (!(info->tport.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return;
599
Jiri Slaby916b7652012-03-05 14:52:20 +0100600 state = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602#ifdef SERIAL_DEBUG_OPEN
603 printk("Shutting down serial port %d ....\n", info->line);
604#endif
605
606 local_irq_save(flags); /* Disable interrupts */
607
608 /*
609 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
610 * here so the queue might never be waken up
611 */
Jiri Slaby87758792012-03-05 14:52:24 +0100612 wake_up_interruptible(&info->tport.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * Free the IRQ, if necessary
616 */
617 free_irq(IRQ_AMIGA_VERTB, info);
618
619 if (info->xmit.buf) {
620 free_page((unsigned long) info->xmit.buf);
621 info->xmit.buf = NULL;
622 }
623
624 info->IER = 0;
625 custom.intena = IF_RBF | IF_TBE;
626 mb();
627
628 /* disable break condition */
629 custom.adkcon = AC_UARTBRK;
630 mb();
631
Jiri Slaby588993d2012-03-05 14:52:22 +0100632 if (tty->termios->c_cflag & HUPCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 info->MCR &= ~(SER_DTR|SER_RTS);
634 rtsdtr_ctrl(info->MCR);
635
Jiri Slaby588993d2012-03-05 14:52:22 +0100636 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Jiri Slaby01bd7302012-03-05 14:52:27 +0100638 info->tport.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 local_irq_restore(flags);
640}
641
642
643/*
644 * This routine is called to set the UART divisor registers to match
645 * the specified baud rate for a serial port.
646 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100647static void change_speed(struct tty_struct *tty, struct serial_state *info,
Alan Cox606d0992006-12-08 02:38:45 -0800648 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Jiri Slaby01bd7302012-03-05 14:52:27 +0100650 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int quot = 0, baud_base, baud;
652 unsigned cflag, cval = 0;
653 int bits;
654 unsigned long flags;
655
Jiri Slaby588993d2012-03-05 14:52:22 +0100656 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* Byte size is always 8 bits plus parity bit if requested */
659
660 cval = 3; bits = 10;
661 if (cflag & CSTOPB) {
662 cval |= 0x04;
663 bits++;
664 }
665 if (cflag & PARENB) {
666 cval |= UART_LCR_PARITY;
667 bits++;
668 }
669 if (!(cflag & PARODD))
670 cval |= UART_LCR_EPAR;
671#ifdef CMSPAR
672 if (cflag & CMSPAR)
673 cval |= UART_LCR_SPAR;
674#endif
675
676 /* Determine divisor based on baud rate */
Jiri Slaby588993d2012-03-05 14:52:22 +0100677 baud = tty_get_baud_rate(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!baud)
679 baud = 9600; /* B0 transition handled in rs_set_termios */
Jiri Slaby916b7652012-03-05 14:52:20 +0100680 baud_base = info->baud_base;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100681 if (baud == 38400 && (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
Jiri Slaby916b7652012-03-05 14:52:20 +0100682 quot = info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 else {
684 if (baud == 134)
685 /* Special case since 134 is really 134.5 */
686 quot = (2*baud_base / 269);
687 else if (baud)
688 quot = baud_base / baud;
689 }
690 /* If the quotient is zero refuse the change */
691 if (!quot && old_termios) {
Alan Coxdb0ef082007-07-15 23:41:47 -0700692 /* FIXME: Will need updating for new tty in the end */
Jiri Slaby588993d2012-03-05 14:52:22 +0100693 tty->termios->c_cflag &= ~CBAUD;
694 tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
695 baud = tty_get_baud_rate(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!baud)
697 baud = 9600;
698 if (baud == 38400 &&
Jiri Slaby01bd7302012-03-05 14:52:27 +0100699 (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
Jiri Slaby916b7652012-03-05 14:52:20 +0100700 quot = info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 else {
702 if (baud == 134)
703 /* Special case since 134 is really 134.5 */
704 quot = (2*baud_base / 269);
705 else if (baud)
706 quot = baud_base / baud;
707 }
708 }
709 /* As a last resort, if the quotient is zero, default to 9600 bps */
710 if (!quot)
711 quot = baud_base / 9600;
712 info->quot = quot;
Jiri Slaby916b7652012-03-05 14:52:20 +0100713 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 info->timeout += HZ/50; /* Add .02 seconds of slop */
715
716 /* CTS flow control flag and modem status interrupts */
717 info->IER &= ~UART_IER_MSI;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100718 if (port->flags & ASYNC_HARDPPS_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 info->IER |= UART_IER_MSI;
720 if (cflag & CRTSCTS) {
Jiri Slaby01bd7302012-03-05 14:52:27 +0100721 port->flags |= ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 info->IER |= UART_IER_MSI;
723 } else
Jiri Slaby01bd7302012-03-05 14:52:27 +0100724 port->flags &= ~ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (cflag & CLOCAL)
Jiri Slaby01bd7302012-03-05 14:52:27 +0100726 port->flags &= ~ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 else {
Jiri Slaby01bd7302012-03-05 14:52:27 +0100728 port->flags |= ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 info->IER |= UART_IER_MSI;
730 }
731 /* TBD:
Thadeu Lima de Souza Cascardo4b512d22009-04-14 23:14:10 -0300732 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
734
735 /*
736 * Set up parity check flag
737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
Jiri Slaby588993d2012-03-05 14:52:22 +0100740 if (I_INPCK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Jiri Slaby588993d2012-03-05 14:52:22 +0100742 if (I_BRKINT(tty) || I_PARMRK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 info->read_status_mask |= UART_LSR_BI;
744
745 /*
746 * Characters to ignore
747 */
748 info->ignore_status_mask = 0;
Jiri Slaby588993d2012-03-05 14:52:22 +0100749 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
Jiri Slaby588993d2012-03-05 14:52:22 +0100751 if (I_IGNBRK(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 info->ignore_status_mask |= UART_LSR_BI;
753 /*
754 * If we're ignore parity and break indicators, ignore
755 * overruns too. (For real raw support).
756 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100757 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 info->ignore_status_mask |= UART_LSR_OE;
759 }
760 /*
761 * !!! ignore all characters if CREAD is not set
762 */
763 if ((cflag & CREAD) == 0)
764 info->ignore_status_mask |= UART_LSR_DR;
765 local_irq_save(flags);
766
767 {
768 short serper;
769
770 /* Set up the baud rate */
771 serper = quot - 1;
772
773 /* Enable or disable parity bit */
774
775 if(cval & UART_LCR_PARITY)
776 serper |= (SERPER_PARENB);
777
778 custom.serper = serper;
779 mb();
780 }
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 local_irq_restore(flags);
783}
784
Alan Cox257afa32008-04-30 00:54:02 -0700785static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Jiri Slaby916b7652012-03-05 14:52:20 +0100787 struct serial_state *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 unsigned long flags;
789
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700790 info = tty->driver_data;
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
Alan Cox257afa32008-04-30 00:54:02 -0700793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700795 if (!info->xmit.buf)
Alan Cox257afa32008-04-30 00:54:02 -0700796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 local_irq_save(flags);
799 if (CIRC_SPACE(info->xmit.head,
800 info->xmit.tail,
801 SERIAL_XMIT_SIZE) == 0) {
802 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700803 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805
806 info->xmit.buf[info->xmit.head++] = ch;
807 info->xmit.head &= SERIAL_XMIT_SIZE-1;
808 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700809 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812static void rs_flush_chars(struct tty_struct *tty)
813{
Jiri Slaby916b7652012-03-05 14:52:20 +0100814 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 unsigned long flags;
816
817 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
818 return;
819
820 if (info->xmit.head == info->xmit.tail
821 || tty->stopped
822 || tty->hw_stopped
823 || !info->xmit.buf)
824 return;
825
826 local_irq_save(flags);
827 info->IER |= UART_IER_THRI;
828 custom.intena = IF_SETCLR | IF_TBE;
829 mb();
830 /* set a pending Tx Interrupt, transmitter should restart now */
831 custom.intreq = IF_SETCLR | IF_TBE;
832 mb();
833 local_irq_restore(flags);
834}
835
836static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
837{
838 int c, ret = 0;
Jiri Slaby916b7652012-03-05 14:52:20 +0100839 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 unsigned long flags;
841
842 if (serial_paranoia_check(info, tty->name, "rs_write"))
843 return 0;
844
Jiri Slabyb3218a72006-10-04 02:15:27 -0700845 if (!info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return 0;
847
Jiri Kosina3abf3be2007-02-10 01:46:48 -0800848 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 while (1) {
850 c = CIRC_SPACE_TO_END(info->xmit.head,
851 info->xmit.tail,
852 SERIAL_XMIT_SIZE);
853 if (count < c)
854 c = count;
855 if (c <= 0) {
856 break;
857 }
858 memcpy(info->xmit.buf + info->xmit.head, buf, c);
859 info->xmit.head = ((info->xmit.head + c) &
860 (SERIAL_XMIT_SIZE-1));
861 buf += c;
862 count -= c;
863 ret += c;
864 }
865 local_irq_restore(flags);
866
867 if (info->xmit.head != info->xmit.tail
868 && !tty->stopped
869 && !tty->hw_stopped
870 && !(info->IER & UART_IER_THRI)) {
871 info->IER |= UART_IER_THRI;
872 local_irq_disable();
873 custom.intena = IF_SETCLR | IF_TBE;
874 mb();
875 /* set a pending Tx Interrupt, transmitter should restart now */
876 custom.intreq = IF_SETCLR | IF_TBE;
877 mb();
878 local_irq_restore(flags);
879 }
880 return ret;
881}
882
883static int rs_write_room(struct tty_struct *tty)
884{
Jiri Slaby916b7652012-03-05 14:52:20 +0100885 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
888 return 0;
889 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
890}
891
892static int rs_chars_in_buffer(struct tty_struct *tty)
893{
Jiri Slaby916b7652012-03-05 14:52:20 +0100894 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
897 return 0;
898 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
899}
900
901static void rs_flush_buffer(struct tty_struct *tty)
902{
Jiri Slaby916b7652012-03-05 14:52:20 +0100903 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 unsigned long flags;
905
906 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
907 return;
908 local_irq_save(flags);
909 info->xmit.head = info->xmit.tail = 0;
910 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 tty_wakeup(tty);
912}
913
914/*
915 * This function is used to send a high-priority XON/XOFF character to
916 * the device
917 */
918static void rs_send_xchar(struct tty_struct *tty, char ch)
919{
Jiri Slaby916b7652012-03-05 14:52:20 +0100920 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 unsigned long flags;
922
923 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
924 return;
925
926 info->x_char = ch;
927 if (ch) {
928 /* Make sure transmit interrupts are on */
929
930 /* Check this ! */
931 local_irq_save(flags);
932 if(!(custom.intenar & IF_TBE)) {
933 custom.intena = IF_SETCLR | IF_TBE;
934 mb();
935 /* set a pending Tx Interrupt, transmitter should restart now */
936 custom.intreq = IF_SETCLR | IF_TBE;
937 mb();
938 }
939 local_irq_restore(flags);
940
941 info->IER |= UART_IER_THRI;
942 }
943}
944
945/*
946 * ------------------------------------------------------------
947 * rs_throttle()
948 *
949 * This routine is called by the upper-layer tty layer to signal that
950 * incoming characters should be throttled.
951 * ------------------------------------------------------------
952 */
953static void rs_throttle(struct tty_struct * tty)
954{
Jiri Slaby916b7652012-03-05 14:52:20 +0100955 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 unsigned long flags;
957#ifdef SERIAL_DEBUG_THROTTLE
958 char buf[64];
959
960 printk("throttle %s: %d....\n", tty_name(tty, buf),
961 tty->ldisc.chars_in_buffer(tty));
962#endif
963
964 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
965 return;
966
967 if (I_IXOFF(tty))
968 rs_send_xchar(tty, STOP_CHAR(tty));
969
970 if (tty->termios->c_cflag & CRTSCTS)
971 info->MCR &= ~SER_RTS;
972
973 local_irq_save(flags);
974 rtsdtr_ctrl(info->MCR);
975 local_irq_restore(flags);
976}
977
978static void rs_unthrottle(struct tty_struct * tty)
979{
Jiri Slaby916b7652012-03-05 14:52:20 +0100980 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 unsigned long flags;
982#ifdef SERIAL_DEBUG_THROTTLE
983 char buf[64];
984
985 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
986 tty->ldisc.chars_in_buffer(tty));
987#endif
988
989 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
990 return;
991
992 if (I_IXOFF(tty)) {
993 if (info->x_char)
994 info->x_char = 0;
995 else
996 rs_send_xchar(tty, START_CHAR(tty));
997 }
998 if (tty->termios->c_cflag & CRTSCTS)
999 info->MCR |= SER_RTS;
1000 local_irq_save(flags);
1001 rtsdtr_ctrl(info->MCR);
1002 local_irq_restore(flags);
1003}
1004
1005/*
1006 * ------------------------------------------------------------
1007 * rs_ioctl() and friends
1008 * ------------------------------------------------------------
1009 */
1010
Jiri Slabyff169e52012-03-05 14:52:44 +01001011static int get_serial_info(struct tty_struct *tty, struct serial_state *state,
Al Viroab14cae2006-01-12 01:06:30 -08001012 struct serial_struct __user * retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct serial_struct tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (!retinfo)
1017 return -EFAULT;
1018 memset(&tmp, 0, sizeof(tmp));
Arnd Bergmannec79d602010-06-01 22:53:01 +02001019 tty_lock();
Jiri Slabyff169e52012-03-05 14:52:44 +01001020 tmp.line = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 tmp.port = state->port;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001022 tmp.flags = state->tport.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 tmp.xmit_fifo_size = state->xmit_fifo_size;
1024 tmp.baud_base = state->baud_base;
Jiri Slaby799be6f2012-03-05 14:52:25 +01001025 tmp.close_delay = state->tport.close_delay;
1026 tmp.closing_wait = state->tport.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 tmp.custom_divisor = state->custom_divisor;
Arnd Bergmannec79d602010-06-01 22:53:01 +02001028 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1030 return -EFAULT;
1031 return 0;
1032}
1033
Jiri Slaby588993d2012-03-05 14:52:22 +01001034static int set_serial_info(struct tty_struct *tty, struct serial_state *state,
Al Viroab14cae2006-01-12 01:06:30 -08001035 struct serial_struct __user * new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Jiri Slaby01bd7302012-03-05 14:52:27 +01001037 struct tty_port *port = &state->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct serial_struct new_serial;
Jiri Slaby0f9b9682012-03-05 14:52:21 +01001039 bool change_spd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int retval = 0;
1041
1042 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1043 return -EFAULT;
Alan Coxe18ce492008-04-30 00:53:16 -07001044
Arnd Bergmannec79d602010-06-01 22:53:01 +02001045 tty_lock();
Jiri Slaby01bd7302012-03-05 14:52:27 +01001046 change_spd = ((new_serial.flags ^ port->flags) & ASYNC_SPD_MASK) ||
Jiri Slaby0f9b9682012-03-05 14:52:21 +01001047 new_serial.custom_divisor != state->custom_divisor;
Jiri Slabyff169e52012-03-05 14:52:44 +01001048 if (new_serial.irq || new_serial.port != state->port ||
Jiri Slaby0f9b9682012-03-05 14:52:21 +01001049 new_serial.xmit_fifo_size != state->xmit_fifo_size) {
1050 tty_unlock();
1051 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (!serial_isroot()) {
1055 if ((new_serial.baud_base != state->baud_base) ||
Jiri Slaby01bd7302012-03-05 14:52:27 +01001056 (new_serial.close_delay != port->close_delay) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1058 ((new_serial.flags & ~ASYNC_USR_MASK) !=
Jiri Slaby01bd7302012-03-05 14:52:27 +01001059 (port->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return -EPERM;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001061 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 state->custom_divisor = new_serial.custom_divisor;
1064 goto check_and_exit;
1065 }
1066
Alan Coxe18ce492008-04-30 00:53:16 -07001067 if (new_serial.baud_base < 9600) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001068 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /*
1073 * OK, past this point, all the error checking has been done.
1074 * At this point, we start making changes.....
1075 */
1076
1077 state->baud_base = new_serial.baud_base;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001078 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 state->custom_divisor = new_serial.custom_divisor;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001081 port->close_delay = new_serial.close_delay * HZ/100;
1082 port->closing_wait = new_serial.closing_wait * HZ/100;
1083 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085check_and_exit:
Jiri Slaby01bd7302012-03-05 14:52:27 +01001086 if (port->flags & ASYNC_INITIALIZED) {
Jiri Slaby0f9b9682012-03-05 14:52:21 +01001087 if (change_spd) {
Jiri Slaby01bd7302012-03-05 14:52:27 +01001088 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby588993d2012-03-05 14:52:22 +01001089 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001090 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby588993d2012-03-05 14:52:22 +01001091 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001092 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby588993d2012-03-05 14:52:22 +01001093 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001094 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby588993d2012-03-05 14:52:22 +01001095 tty->alt_speed = 460800;
1096 change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 } else
Jiri Slaby588993d2012-03-05 14:52:22 +01001099 retval = startup(tty, state);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001100 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return retval;
1102}
1103
1104
1105/*
1106 * get_lsr_info - get line status register info
1107 *
1108 * Purpose: Let user call ioctl() to get info when the UART physically
1109 * is emptied. On bus types like RS485, the transmitter must
1110 * release the bus after transmitting. This must be done when
1111 * the transmit shift register is empty, not be done when the
1112 * transmit holding register is empty. This functionality
1113 * allows an RS485 driver to be written in user space.
1114 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001115static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 unsigned char status;
1118 unsigned int result;
1119 unsigned long flags;
1120
1121 local_irq_save(flags);
1122 status = custom.serdatr;
1123 mb();
1124 local_irq_restore(flags);
1125 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1126 if (copy_to_user(value, &result, sizeof(int)))
1127 return -EFAULT;
1128 return 0;
1129}
1130
1131
Alan Cox60b33c12011-02-14 16:26:14 +00001132static int rs_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Jiri Slaby916b7652012-03-05 14:52:20 +01001134 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 unsigned char control, status;
1136 unsigned long flags;
1137
1138 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1139 return -ENODEV;
1140 if (tty->flags & (1 << TTY_IO_ERROR))
1141 return -EIO;
1142
1143 control = info->MCR;
1144 local_irq_save(flags);
1145 status = ciab.pra;
1146 local_irq_restore(flags);
1147 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1148 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1149 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1150 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1151 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1152}
1153
Alan Cox20b9d172011-02-14 16:26:50 +00001154static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1155 unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Jiri Slaby916b7652012-03-05 14:52:20 +01001157 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 unsigned long flags;
1159
1160 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1161 return -ENODEV;
1162 if (tty->flags & (1 << TTY_IO_ERROR))
1163 return -EIO;
1164
1165 local_irq_save(flags);
1166 if (set & TIOCM_RTS)
1167 info->MCR |= SER_RTS;
1168 if (set & TIOCM_DTR)
1169 info->MCR |= SER_DTR;
1170 if (clear & TIOCM_RTS)
1171 info->MCR &= ~SER_RTS;
1172 if (clear & TIOCM_DTR)
1173 info->MCR &= ~SER_DTR;
1174 rtsdtr_ctrl(info->MCR);
1175 local_irq_restore(flags);
1176 return 0;
1177}
1178
1179/*
1180 * rs_break() --- routine which turns the break handling on or off
1181 */
Alan Cox9e989662008-07-22 11:18:03 +01001182static int rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Jiri Slaby916b7652012-03-05 14:52:20 +01001184 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 unsigned long flags;
1186
1187 if (serial_paranoia_check(info, tty->name, "rs_break"))
Geert Uytterhoeven970a8a52008-08-06 22:19:39 +02001188 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 local_irq_save(flags);
1191 if (break_state == -1)
1192 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1193 else
1194 custom.adkcon = AC_UARTBRK;
1195 mb();
1196 local_irq_restore(flags);
Alan Cox9e989662008-07-22 11:18:03 +01001197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Alan Cox05871022010-09-16 18:21:52 +01001200/*
1201 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1202 * Return: write counters to the user passed counter struct
1203 * NB: both 1->0 and 0->1 transitions are counted except for
1204 * RI where only 0->1 is counted.
1205 */
1206static int rs_get_icount(struct tty_struct *tty,
1207 struct serial_icounter_struct *icount)
1208{
Jiri Slaby916b7652012-03-05 14:52:20 +01001209 struct serial_state *info = tty->driver_data;
Alan Cox05871022010-09-16 18:21:52 +01001210 struct async_icount cnow;
1211 unsigned long flags;
1212
1213 local_irq_save(flags);
Jiri Slaby916b7652012-03-05 14:52:20 +01001214 cnow = info->icount;
Alan Cox05871022010-09-16 18:21:52 +01001215 local_irq_restore(flags);
1216 icount->cts = cnow.cts;
1217 icount->dsr = cnow.dsr;
1218 icount->rng = cnow.rng;
1219 icount->dcd = cnow.dcd;
1220 icount->rx = cnow.rx;
1221 icount->tx = cnow.tx;
1222 icount->frame = cnow.frame;
1223 icount->overrun = cnow.overrun;
1224 icount->parity = cnow.parity;
1225 icount->brk = cnow.brk;
1226 icount->buf_overrun = cnow.buf_overrun;
1227
1228 return 0;
1229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Alan Cox6caa76b2011-02-14 16:27:22 +00001231static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 unsigned int cmd, unsigned long arg)
1233{
Jiri Slaby916b7652012-03-05 14:52:20 +01001234 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 struct async_icount cprev, cnow; /* kernel counter temps */
Al Viroab14cae2006-01-12 01:06:30 -08001236 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 unsigned long flags;
1238
1239 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1240 return -ENODEV;
1241
1242 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1243 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1244 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1245 if (tty->flags & (1 << TTY_IO_ERROR))
1246 return -EIO;
1247 }
1248
1249 switch (cmd) {
1250 case TIOCGSERIAL:
Jiri Slabyff169e52012-03-05 14:52:44 +01001251 return get_serial_info(tty, info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 case TIOCSSERIAL:
Jiri Slaby588993d2012-03-05 14:52:22 +01001253 return set_serial_info(tty, info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 case TIOCSERCONFIG:
1255 return 0;
1256
1257 case TIOCSERGETLSR: /* Get line status register */
Al Viroab14cae2006-01-12 01:06:30 -08001258 return get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 case TIOCSERGSTRUCT:
Al Viroab14cae2006-01-12 01:06:30 -08001261 if (copy_to_user(argp,
Jiri Slaby916b7652012-03-05 14:52:20 +01001262 info, sizeof(struct serial_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return -EFAULT;
1264 return 0;
1265
1266 /*
1267 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1268 * - mask passed in arg for lines of interest
1269 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1270 * Caller should use TIOCGICOUNT to see which one it was
1271 */
1272 case TIOCMIWAIT:
1273 local_irq_save(flags);
1274 /* note the counters on entry */
Jiri Slaby916b7652012-03-05 14:52:20 +01001275 cprev = info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 local_irq_restore(flags);
1277 while (1) {
Jiri Slaby87758792012-03-05 14:52:24 +01001278 interruptible_sleep_on(&info->tport.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /* see if a signal did it */
1280 if (signal_pending(current))
1281 return -ERESTARTSYS;
1282 local_irq_save(flags);
Jiri Slaby916b7652012-03-05 14:52:20 +01001283 cnow = info->icount; /* atomic copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 local_irq_restore(flags);
1285 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1286 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1287 return -EIO; /* no change => error */
1288 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1289 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1290 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1291 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1292 return 0;
1293 }
1294 cprev = cnow;
1295 }
1296 /* NOTREACHED */
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 case TIOCSERGWILD:
1299 case TIOCSERSWILD:
1300 /* "setserial -W" is called in Debian boot */
1301 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1302 return 0;
1303
1304 default:
1305 return -ENOIOCTLCMD;
1306 }
1307 return 0;
1308}
1309
Alan Cox606d0992006-12-08 02:38:45 -08001310static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Jiri Slaby916b7652012-03-05 14:52:20 +01001312 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 unsigned long flags;
1314 unsigned int cflag = tty->termios->c_cflag;
1315
Jiri Slaby588993d2012-03-05 14:52:22 +01001316 change_speed(tty, info, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /* Handle transition to B0 status */
1319 if ((old_termios->c_cflag & CBAUD) &&
1320 !(cflag & CBAUD)) {
1321 info->MCR &= ~(SER_DTR|SER_RTS);
1322 local_irq_save(flags);
1323 rtsdtr_ctrl(info->MCR);
1324 local_irq_restore(flags);
1325 }
1326
1327 /* Handle transition away from B0 status */
1328 if (!(old_termios->c_cflag & CBAUD) &&
1329 (cflag & CBAUD)) {
1330 info->MCR |= SER_DTR;
1331 if (!(tty->termios->c_cflag & CRTSCTS) ||
1332 !test_bit(TTY_THROTTLED, &tty->flags)) {
1333 info->MCR |= SER_RTS;
1334 }
1335 local_irq_save(flags);
1336 rtsdtr_ctrl(info->MCR);
1337 local_irq_restore(flags);
1338 }
1339
1340 /* Handle turning off CRTSCTS */
1341 if ((old_termios->c_cflag & CRTSCTS) &&
1342 !(tty->termios->c_cflag & CRTSCTS)) {
1343 tty->hw_stopped = 0;
1344 rs_start(tty);
1345 }
1346
1347#if 0
1348 /*
1349 * No need to wake up processes in open wait, since they
1350 * sample the CLOCAL flag once, and don't recheck it.
1351 * XXX It's not clear whether the current behavior is correct
1352 * or not. Hence, this may change.....
1353 */
1354 if (!(old_termios->c_cflag & CLOCAL) &&
1355 (tty->termios->c_cflag & CLOCAL))
1356 wake_up_interruptible(&info->open_wait);
1357#endif
1358}
1359
1360/*
1361 * ------------------------------------------------------------
1362 * rs_close()
1363 *
1364 * This routine is called when the serial port gets closed. First, we
1365 * wait for the last remaining data to be sent. Then, we unlink its
1366 * async structure from the interrupt chain if necessary, and we free
1367 * that IRQ if nothing is left in the chain.
1368 * ------------------------------------------------------------
1369 */
1370static void rs_close(struct tty_struct *tty, struct file * filp)
1371{
Jiri Slaby916b7652012-03-05 14:52:20 +01001372 struct serial_state *state = tty->driver_data;
Jiri Slaby7188dc22012-03-05 14:52:43 +01001373 struct tty_port *port = &state->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 unsigned long flags;
1375
Jiri Slaby916b7652012-03-05 14:52:20 +01001376 if (!state || serial_paranoia_check(state, tty->name, "rs_close"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return;
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 local_irq_save(flags);
1380
1381 if (tty_hung_up_p(filp)) {
1382 DBG_CNT("before DEC-hung");
1383 local_irq_restore(flags);
1384 return;
1385 }
1386
1387#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby7188dc22012-03-05 14:52:43 +01001388 printk("rs_close ttys%d, count = %d\n", state->line, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389#endif
Jiri Slaby7188dc22012-03-05 14:52:43 +01001390 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /*
1392 * Uh, oh. tty->count is 1, which means that the tty
Jiri Slaby7188dc22012-03-05 14:52:43 +01001393 * structure will be freed. port->count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * be one in these conditions. If it's greater than
1395 * one, we've got real problems, since it means the
1396 * serial port won't be shutdown.
1397 */
1398 printk("rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby7188dc22012-03-05 14:52:43 +01001399 "port->count is %d\n", state->tport.count);
1400 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
Jiri Slaby7188dc22012-03-05 14:52:43 +01001402 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 printk("rs_close: bad serial port count for ttys%d: %d\n",
Jiri Slabyff169e52012-03-05 14:52:44 +01001404 tty->index, port->count);
Jiri Slaby7188dc22012-03-05 14:52:43 +01001405 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
Jiri Slaby7188dc22012-03-05 14:52:43 +01001407 if (port->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 DBG_CNT("before DEC-2");
1409 local_irq_restore(flags);
1410 return;
1411 }
Jiri Slaby7188dc22012-03-05 14:52:43 +01001412 port->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /*
1414 * Now we wait for the transmit buffer to clear; and we notify
1415 * the line discipline to only process XON/XOFF characters.
1416 */
1417 tty->closing = 1;
Jiri Slaby7188dc22012-03-05 14:52:43 +01001418 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1419 tty_wait_until_sent(tty, port->closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /*
1421 * At this point we stop accepting input. To do this, we
1422 * disable the receive line status interrupts, and tell the
1423 * interrupt driver to stop checking the data ready bit in the
1424 * line status register.
1425 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001426 state->read_status_mask &= ~UART_LSR_DR;
Jiri Slaby7188dc22012-03-05 14:52:43 +01001427 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /* disable receive interrupts */
1429 custom.intena = IF_RBF;
1430 mb();
1431 /* clear any pending receive interrupt */
1432 custom.intreq = IF_RBF;
1433 mb();
1434
1435 /*
1436 * Before we drop DTR, make sure the UART transmitter
1437 * has completely drained; this is especially
1438 * important if there is a transmit FIFO!
1439 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001440 rs_wait_until_sent(tty, state->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
Jiri Slaby588993d2012-03-05 14:52:22 +01001442 shutdown(tty, state);
Alan Cox978e5952008-04-30 00:53:59 -07001443 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 tty_ldisc_flush(tty);
1446 tty->closing = 0;
Jiri Slaby7188dc22012-03-05 14:52:43 +01001447 port->tty = NULL;
1448 if (port->blocked_open) {
1449 if (port->close_delay) {
1450 msleep_interruptible(jiffies_to_msecs(port->close_delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
Jiri Slaby7188dc22012-03-05 14:52:43 +01001452 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
Jiri Slaby7188dc22012-03-05 14:52:43 +01001454 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1455 wake_up_interruptible(&port->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 local_irq_restore(flags);
1457}
1458
1459/*
1460 * rs_wait_until_sent() --- wait until the transmitter is empty
1461 */
1462static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1463{
Jiri Slaby916b7652012-03-05 14:52:20 +01001464 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 unsigned long orig_jiffies, char_time;
1466 int lsr;
1467
1468 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1469 return;
1470
Jiri Slaby916b7652012-03-05 14:52:20 +01001471 if (info->xmit_fifo_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return; /* Just in case.... */
1473
1474 orig_jiffies = jiffies;
Alan Cox978e5952008-04-30 00:53:59 -07001475
Arnd Bergmann20365212010-06-01 22:53:07 +02001476 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 * Set the check interval to be 1/5 of the estimated time to
1478 * send a single character, and make it at least 1. The check
1479 * interval should also be less than the timeout.
1480 *
1481 * Note: we have to use pretty tight timings here to satisfy
1482 * the NIST-PCTS.
1483 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001484 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 char_time = char_time / 5;
1486 if (char_time == 0)
1487 char_time = 1;
1488 if (timeout)
1489 char_time = min_t(unsigned long, char_time, timeout);
1490 /*
1491 * If the transmitter hasn't cleared in twice the approximate
1492 * amount of time to send the entire FIFO, it probably won't
1493 * ever clear. This assumes the UART isn't doing flow
1494 * control, which is currently the case. Hence, if it ever
1495 * takes longer than info->timeout, this is probably due to a
1496 * UART bug of some kind. So, we clamp the timeout parameter at
1497 * 2*info->timeout.
1498 */
1499 if (!timeout || timeout > 2*info->timeout)
1500 timeout = 2*info->timeout;
1501#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1502 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1503 printk("jiff=%lu...", jiffies);
1504#endif
1505 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1506#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1507 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1508#endif
1509 msleep_interruptible(jiffies_to_msecs(char_time));
1510 if (signal_pending(current))
1511 break;
1512 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1513 break;
1514 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001515 __set_current_state(TASK_RUNNING);
Jiri Slabyeff4b0b2011-07-14 14:35:13 +02001516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1518 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1519#endif
1520}
1521
1522/*
1523 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1524 */
1525static void rs_hangup(struct tty_struct *tty)
1526{
Jiri Slaby916b7652012-03-05 14:52:20 +01001527 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1530 return;
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 rs_flush_buffer(tty);
Jiri Slaby588993d2012-03-05 14:52:22 +01001533 shutdown(tty, info);
Jiri Slaby12c80352012-03-05 14:52:26 +01001534 info->tport.count = 0;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001535 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby87758792012-03-05 14:52:24 +01001536 info->tport.tty = NULL;
1537 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540/*
1541 * ------------------------------------------------------------
1542 * rs_open() and friends
1543 * ------------------------------------------------------------
1544 */
1545static int block_til_ready(struct tty_struct *tty, struct file * filp,
Jiri Slaby916b7652012-03-05 14:52:20 +01001546 struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548#ifdef DECLARE_WAITQUEUE
1549 DECLARE_WAITQUEUE(wait, current);
1550#else
1551 struct wait_queue wait = { current, NULL };
1552#endif
Jiri Slaby01bd7302012-03-05 14:52:27 +01001553 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int retval;
1555 int do_clocal = 0, extra_count = 0;
1556 unsigned long flags;
1557
1558 /*
1559 * If the device is in the middle of being closed, then block
1560 * until it's done, and then try again.
1561 */
1562 if (tty_hung_up_p(filp) ||
Jiri Slaby01bd7302012-03-05 14:52:27 +01001563 (port->flags & ASYNC_CLOSING)) {
1564 if (port->flags & ASYNC_CLOSING)
1565 interruptible_sleep_on(&port->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566#ifdef SERIAL_DO_RESTART
Jiri Slaby01bd7302012-03-05 14:52:27 +01001567 return ((port->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 -EAGAIN : -ERESTARTSYS);
1569#else
1570 return -EAGAIN;
1571#endif
1572 }
1573
1574 /*
1575 * If non-blocking mode is set, or the port is not enabled,
1576 * then make the check up front and then exit.
1577 */
1578 if ((filp->f_flags & O_NONBLOCK) ||
1579 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slaby01bd7302012-03-05 14:52:27 +01001580 port->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return 0;
1582 }
1583
1584 if (tty->termios->c_cflag & CLOCAL)
1585 do_clocal = 1;
1586
1587 /*
1588 * Block waiting for the carrier detect and the line to become
1589 * free (i.e., not in use by the callout). While we are in
Jiri Slaby01bd7302012-03-05 14:52:27 +01001590 * this loop, port->count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 * rs_close() knows when to free things. We restore it upon
1592 * exit, either normal or abnormal.
1593 */
1594 retval = 0;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001595 add_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596#ifdef SERIAL_DEBUG_OPEN
1597 printk("block_til_ready before block: ttys%d, count = %d\n",
Jiri Slaby01bd7302012-03-05 14:52:27 +01001598 info->line, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599#endif
1600 local_irq_save(flags);
1601 if (!tty_hung_up_p(filp)) {
1602 extra_count = 1;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001603 port->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605 local_irq_restore(flags);
Jiri Slaby01bd7302012-03-05 14:52:27 +01001606 port->blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 while (1) {
1608 local_irq_save(flags);
1609 if (tty->termios->c_cflag & CBAUD)
1610 rtsdtr_ctrl(SER_DTR|SER_RTS);
1611 local_irq_restore(flags);
1612 set_current_state(TASK_INTERRUPTIBLE);
1613 if (tty_hung_up_p(filp) ||
Jiri Slaby01bd7302012-03-05 14:52:27 +01001614 !(port->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615#ifdef SERIAL_DO_RESTART
Jiri Slaby01bd7302012-03-05 14:52:27 +01001616 if (port->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 retval = -EAGAIN;
1618 else
1619 retval = -ERESTARTSYS;
1620#else
1621 retval = -EAGAIN;
1622#endif
1623 break;
1624 }
Jiri Slaby01bd7302012-03-05 14:52:27 +01001625 if (!(port->flags & ASYNC_CLOSING) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1627 break;
1628 if (signal_pending(current)) {
1629 retval = -ERESTARTSYS;
1630 break;
1631 }
1632#ifdef SERIAL_DEBUG_OPEN
1633 printk("block_til_ready blocking: ttys%d, count = %d\n",
Jiri Slaby01bd7302012-03-05 14:52:27 +01001634 info->line, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635#endif
Arnd Bergmanne142a312010-06-01 22:53:10 +02001636 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001638 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001640 __set_current_state(TASK_RUNNING);
Jiri Slaby01bd7302012-03-05 14:52:27 +01001641 remove_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (extra_count)
Jiri Slaby01bd7302012-03-05 14:52:27 +01001643 port->count++;
1644 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645#ifdef SERIAL_DEBUG_OPEN
1646 printk("block_til_ready after blocking: ttys%d, count = %d\n",
Jiri Slaby01bd7302012-03-05 14:52:27 +01001647 info->line, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648#endif
1649 if (retval)
1650 return retval;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001651 port->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return 0;
1653}
1654
1655/*
1656 * This routine is called whenever a serial port is opened. It
1657 * enables interrupts for a serial port, linking in its async structure into
1658 * the IRQ chain. It also performs the serial-specific
1659 * initialization for the tty structure.
1660 */
1661static int rs_open(struct tty_struct *tty, struct file * filp)
1662{
Jiri Slaby916b7652012-03-05 14:52:20 +01001663 struct serial_state *info = rs_table + tty->index;
Jiri Slaby7188dc22012-03-05 14:52:43 +01001664 struct tty_port *port = &info->tport;
Jiri Slaby410235f2012-03-05 14:52:01 +01001665 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Jiri Slaby7188dc22012-03-05 14:52:43 +01001667 port->count++;
1668 port->tty = tty;
Jiri Slaby916b7652012-03-05 14:52:20 +01001669 tty->driver_data = info;
Jiri Slaby7188dc22012-03-05 14:52:43 +01001670 tty->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (serial_paranoia_check(info, tty->name, "rs_open"))
1672 return -ENODEV;
1673
1674#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby916b7652012-03-05 14:52:20 +01001675 printk("rs_open %s, count = %d\n", tty->name, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676#endif
Jiri Slaby7188dc22012-03-05 14:52:43 +01001677 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /*
1680 * If the port is the middle of closing, bail out now
1681 */
1682 if (tty_hung_up_p(filp) ||
Jiri Slaby7188dc22012-03-05 14:52:43 +01001683 (port->flags & ASYNC_CLOSING)) {
1684 if (port->flags & ASYNC_CLOSING)
1685 interruptible_sleep_on(&port->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686#ifdef SERIAL_DO_RESTART
Jiri Slaby7188dc22012-03-05 14:52:43 +01001687 return ((port->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 -EAGAIN : -ERESTARTSYS);
1689#else
1690 return -EAGAIN;
1691#endif
1692 }
1693
1694 /*
1695 * Start up serial port
1696 */
Jiri Slaby588993d2012-03-05 14:52:22 +01001697 retval = startup(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (retval) {
1699 return retval;
1700 }
1701
1702 retval = block_til_ready(tty, filp, info);
1703 if (retval) {
1704#ifdef SERIAL_DEBUG_OPEN
1705 printk("rs_open returning after block_til_ready with %d\n",
1706 retval);
1707#endif
1708 return retval;
1709 }
1710
1711#ifdef SERIAL_DEBUG_OPEN
1712 printk("rs_open %s successful...", tty->name);
1713#endif
1714 return 0;
1715}
1716
1717/*
1718 * /proc fs routines....
1719 */
1720
Jiri Slabyff169e52012-03-05 14:52:44 +01001721static inline void line_info(struct seq_file *m, int line,
1722 struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 char stat_buf[30], control, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 unsigned long flags;
1726
Jiri Slabyff169e52012-03-05 14:52:44 +01001727 seq_printf(m, "%d: uart:amiga_builtin", line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 local_irq_save(flags);
1730 status = ciab.pra;
Jiri Slaby01bd7302012-03-05 14:52:27 +01001731 control = (state->tport.flags & ASYNC_INITIALIZED) ? state->MCR : status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 local_irq_restore(flags);
1733
1734 stat_buf[0] = 0;
1735 stat_buf[1] = 0;
1736 if(!(control & SER_RTS))
1737 strcat(stat_buf, "|RTS");
1738 if(!(status & SER_CTS))
1739 strcat(stat_buf, "|CTS");
1740 if(!(control & SER_DTR))
1741 strcat(stat_buf, "|DTR");
1742 if(!(status & SER_DSR))
1743 strcat(stat_buf, "|DSR");
1744 if(!(status & SER_DCD))
1745 strcat(stat_buf, "|CD");
1746
Jiri Slaby916b7652012-03-05 14:52:20 +01001747 if (state->quot)
1748 seq_printf(m, " baud:%d", state->baud_base / state->quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Alexey Dobriyand5940272009-03-31 15:19:23 -07001750 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 if (state->icount.frame)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001753 seq_printf(m, " fe:%d", state->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 if (state->icount.parity)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001756 seq_printf(m, " pe:%d", state->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 if (state->icount.brk)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001759 seq_printf(m, " brk:%d", state->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 if (state->icount.overrun)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001762 seq_printf(m, " oe:%d", state->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 /*
1765 * Last thing is the RS-232 status lines
1766 */
Alexey Dobriyand5940272009-03-31 15:19:23 -07001767 seq_printf(m, " %s\n", stat_buf+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Alexey Dobriyand5940272009-03-31 15:19:23 -07001770static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Alexey Dobriyand5940272009-03-31 15:19:23 -07001772 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
Jiri Slabyff169e52012-03-05 14:52:44 +01001773 line_info(m, 0, &rs_table[0]);
Alexey Dobriyand5940272009-03-31 15:19:23 -07001774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Alexey Dobriyand5940272009-03-31 15:19:23 -07001777static int rs_proc_open(struct inode *inode, struct file *file)
1778{
1779 return single_open(file, rs_proc_show, NULL);
1780}
1781
1782static const struct file_operations rs_proc_fops = {
1783 .owner = THIS_MODULE,
1784 .open = rs_proc_open,
1785 .read = seq_read,
1786 .llseek = seq_lseek,
1787 .release = single_release,
1788};
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790/*
1791 * ---------------------------------------------------------------------
1792 * rs_init() and friends
1793 *
1794 * rs_init() is called at boot-time to initialize the serial driver.
1795 * ---------------------------------------------------------------------
1796 */
1797
1798/*
1799 * This routine prints out the appropriate serial driver version
1800 * number, and identifies which options were configured into this
1801 * driver.
1802 */
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001803static void show_serial_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
1805 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1806}
1807
1808
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001809static const struct tty_operations serial_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 .open = rs_open,
1811 .close = rs_close,
1812 .write = rs_write,
1813 .put_char = rs_put_char,
1814 .flush_chars = rs_flush_chars,
1815 .write_room = rs_write_room,
1816 .chars_in_buffer = rs_chars_in_buffer,
1817 .flush_buffer = rs_flush_buffer,
1818 .ioctl = rs_ioctl,
1819 .throttle = rs_throttle,
1820 .unthrottle = rs_unthrottle,
1821 .set_termios = rs_set_termios,
1822 .stop = rs_stop,
1823 .start = rs_start,
1824 .hangup = rs_hangup,
1825 .break_ctl = rs_break,
1826 .send_xchar = rs_send_xchar,
1827 .wait_until_sent = rs_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 .tiocmget = rs_tiocmget,
1829 .tiocmset = rs_tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01001830 .get_icount = rs_get_icount,
Alexey Dobriyand5940272009-03-31 15:19:23 -07001831 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832};
1833
1834/*
1835 * The serial driver boot-time initialization code!
1836 */
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001837static int __init amiga_serial_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 unsigned long flags;
1840 struct serial_state * state;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001841 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Jiri Slaby410235f2012-03-05 14:52:01 +01001843 serial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (!serial_driver)
1845 return -ENOMEM;
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 show_serial_version();
1848
1849 /* Initialize the tty_driver structure */
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 serial_driver->driver_name = "amiserial";
1852 serial_driver->name = "ttyS";
1853 serial_driver->major = TTY_MAJOR;
1854 serial_driver->minor_start = 64;
1855 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1856 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1857 serial_driver->init_termios = tty_std_termios;
1858 serial_driver->init_termios.c_cflag =
1859 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1860 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1861 tty_set_operations(serial_driver, &serial_ops);
1862
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001863 error = tty_register_driver(serial_driver);
1864 if (error)
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001865 goto fail_put_tty_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 state = rs_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 state->port = (int)&custom.serdatr; /* Just to give it a value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 state->custom_divisor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 state->icount.cts = state->icount.dsr =
1871 state->icount.rng = state->icount.dcd = 0;
1872 state->icount.rx = state->icount.tx = 0;
1873 state->icount.frame = state->icount.parity = 0;
1874 state->icount.overrun = state->icount.brk = 0;
Jiri Slaby87758792012-03-05 14:52:24 +01001875 tty_port_init(&state->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Jiri Slabyff169e52012-03-05 14:52:44 +01001877 printk(KERN_INFO "ttyS0 is the amiga builtin serial port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 /* Hardware set up */
1880
1881 state->baud_base = amiga_colorclock;
1882 state->xmit_fifo_size = 1;
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /* set ISRs, and then disable the rx interrupts */
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001885 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1886 if (error)
1887 goto fail_unregister;
1888
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001889 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001890 "serial RX", state);
1891 if (error)
1892 goto fail_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Julia Lawallc70c0362010-04-06 14:34:46 -07001894 local_irq_save(flags);
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /* turn off Rx and Tx interrupts */
1897 custom.intena = IF_RBF | IF_TBE;
1898 mb();
1899
1900 /* clear any pending interrupt */
1901 custom.intreq = IF_RBF | IF_TBE;
1902 mb();
1903
1904 local_irq_restore(flags);
1905
1906 /*
1907 * set the appropriate directions for the modem control flags,
1908 * and clear RTS and DTR
1909 */
1910 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1911 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1912
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001913 platform_set_drvdata(pdev, state);
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return 0;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001916
1917fail_free_irq:
1918 free_irq(IRQ_AMIGA_TBE, state);
1919fail_unregister:
1920 tty_unregister_driver(serial_driver);
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001921fail_put_tty_driver:
1922 put_tty_driver(serial_driver);
1923 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001926static int __exit amiga_serial_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 int error;
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001929 struct serial_state *state = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if ((error = tty_unregister_driver(serial_driver)))
1933 printk("SERIAL: failed to unregister serial driver (%d)\n",
1934 error);
1935 put_tty_driver(serial_driver);
1936
Jiri Slaby916b7652012-03-05 14:52:20 +01001937 free_irq(IRQ_AMIGA_TBE, state);
1938 free_irq(IRQ_AMIGA_RBF, state);
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001939
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001940 platform_set_drvdata(pdev, NULL);
1941
1942 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001945static struct platform_driver amiga_serial_driver = {
1946 .remove = __exit_p(amiga_serial_remove),
1947 .driver = {
1948 .name = "amiga-serial",
1949 .owner = THIS_MODULE,
1950 },
1951};
1952
1953static int __init amiga_serial_init(void)
1954{
1955 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
1956}
1957
1958module_init(amiga_serial_init);
1959
1960static void __exit amiga_serial_exit(void)
1961{
1962 platform_driver_unregister(&amiga_serial_driver);
1963}
1964
1965module_exit(amiga_serial_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01001968#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970/*
1971 * ------------------------------------------------------------
1972 * Serial console driver
1973 * ------------------------------------------------------------
1974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976static void amiga_serial_putc(char c)
1977{
1978 custom.serdat = (unsigned char)c | 0x100;
1979 while (!(custom.serdatr & 0x2000))
1980 barrier();
1981}
1982
1983/*
1984 * Print a string to the serial port trying not to disturb
1985 * any possible real use of the port...
1986 *
1987 * The console must be locked when we get here.
1988 */
1989static void serial_console_write(struct console *co, const char *s,
1990 unsigned count)
1991{
1992 unsigned short intena = custom.intenar;
1993
1994 custom.intena = IF_TBE;
1995
1996 while (count--) {
1997 if (*s == '\n')
1998 amiga_serial_putc('\r');
1999 amiga_serial_putc(*s++);
2000 }
2001
2002 custom.intena = IF_SETCLR | (intena & IF_TBE);
2003}
2004
2005static struct tty_driver *serial_console_device(struct console *c, int *index)
2006{
2007 *index = 0;
2008 return serial_driver;
2009}
2010
2011static struct console sercons = {
2012 .name = "ttyS",
2013 .write = serial_console_write,
2014 .device = serial_console_device,
2015 .flags = CON_PRINTBUFFER,
2016 .index = -1,
2017};
2018
2019/*
2020 * Register console.
2021 */
2022static int __init amiserial_console_init(void)
2023{
2024 register_console(&sercons);
2025 return 0;
2026}
2027console_initcall(amiserial_console_init);
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01002028
2029#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031MODULE_LICENSE("GPL");
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02002032MODULE_ALIAS("platform:amiga-serial");