blob: 8ad64a0f12513ae0e3dcafbc94850b738da1e4e8 [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 Slaby916b7652012-03-05 14:52:20 +010048 tty->name, (info->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 Slaby916b7652012-03-05 14:52:20 +0100299 if (info->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{
364 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
365 unsigned char dstatus;
366 struct async_icount *icount;
367
368 /* Determine bits that have changed */
369 dstatus = status ^ current_ctl_bits;
370 current_ctl_bits = status;
371
372 if (dstatus) {
Jiri Slaby916b7652012-03-05 14:52:20 +0100373 icount = &info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* update input line counters */
375 if (dstatus & SER_DSR)
376 icount->dsr++;
377 if (dstatus & SER_DCD) {
378 icount->dcd++;
379#ifdef CONFIG_HARD_PPS
Jiri Slaby916b7652012-03-05 14:52:20 +0100380 if ((info->flags & ASYNC_HARDPPS_CD) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 !(status & SER_DCD))
382 hardpps();
383#endif
384 }
385 if (dstatus & SER_CTS)
386 icount->cts++;
Jiri Slaby87758792012-03-05 14:52:24 +0100387 wake_up_interruptible(&info->tport.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Jiri Slaby916b7652012-03-05 14:52:20 +0100390 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
392 printk("ttyS%d CD now %s...", info->line,
393 (!(status & SER_DCD)) ? "on" : "off");
394#endif
395 if (!(status & SER_DCD))
Jiri Slaby87758792012-03-05 14:52:24 +0100396 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 else {
398#ifdef SERIAL_DEBUG_OPEN
399 printk("doing serial hangup...");
400#endif
Jiri Slaby87758792012-03-05 14:52:24 +0100401 if (info->tport.tty)
402 tty_hangup(info->tport.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 }
Jiri Slaby916b7652012-03-05 14:52:20 +0100405 if (info->flags & ASYNC_CTS_FLOW) {
Jiri Slaby87758792012-03-05 14:52:24 +0100406 if (info->tport.tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!(status & SER_CTS)) {
408#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
409 printk("CTS tx start...");
410#endif
Jiri Slaby87758792012-03-05 14:52:24 +0100411 info->tport.tty->hw_stopped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 info->IER |= UART_IER_THRI;
413 custom.intena = IF_SETCLR | IF_TBE;
414 mb();
415 /* set a pending Tx Interrupt, transmitter should restart now */
416 custom.intreq = IF_SETCLR | IF_TBE;
417 mb();
Jiri Slaby87758792012-03-05 14:52:24 +0100418 tty_wakeup(info->tport.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return;
420 }
421 } else {
422 if ((status & SER_CTS)) {
423#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
424 printk("CTS tx stop...");
425#endif
Jiri Slaby87758792012-03-05 14:52:24 +0100426 info->tport.tty->hw_stopped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 info->IER &= ~UART_IER_THRI;
428 /* disable Tx interrupt and remove any pending interrupts */
429 custom.intena = IF_TBE;
430 mb();
431 custom.intreq = IF_TBE;
432 mb();
433 }
434 }
435 }
436}
437
David Howells7d12e782006-10-05 14:55:46 +0100438static irqreturn_t ser_vbl_int( int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 /* vbl is just a periodic interrupt we tie into to update modem status */
Jiri Slaby916b7652012-03-05 14:52:20 +0100441 struct serial_state *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /*
443 * TBD - is it better to unregister from this interrupt or to
444 * ignore it if MSI is clear ?
445 */
446 if(info->IER & UART_IER_MSI)
447 check_modem_status(info);
448 return IRQ_HANDLED;
449}
450
David Howells7d12e782006-10-05 14:55:46 +0100451static irqreturn_t ser_rx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Jiri Slaby916b7652012-03-05 14:52:20 +0100453 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455#ifdef SERIAL_DEBUG_INTR
456 printk("ser_rx_int...");
457#endif
458
Jiri Slaby87758792012-03-05 14:52:24 +0100459 if (!info->tport.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return IRQ_NONE;
461
462 receive_chars(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#ifdef SERIAL_DEBUG_INTR
464 printk("end.\n");
465#endif
466 return IRQ_HANDLED;
467}
468
David Howells7d12e782006-10-05 14:55:46 +0100469static irqreturn_t ser_tx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Jiri Slaby916b7652012-03-05 14:52:20 +0100471 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (custom.serdatr & SDR_TBE) {
474#ifdef SERIAL_DEBUG_INTR
475 printk("ser_tx_int...");
476#endif
477
Jiri Slaby87758792012-03-05 14:52:24 +0100478 if (!info->tport.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return IRQ_NONE;
480
481 transmit_chars(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482#ifdef SERIAL_DEBUG_INTR
483 printk("end.\n");
484#endif
485 }
486 return IRQ_HANDLED;
487}
488
489/*
490 * -------------------------------------------------------------------
491 * Here ends the serial interrupt routines.
492 * -------------------------------------------------------------------
493 */
494
495/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * ---------------------------------------------------------------
497 * Low level utility subroutines for the serial driver: routines to
498 * figure out the appropriate timeout for an interrupt chain, routines
499 * to initialize and startup a serial port, and routines to shutdown a
500 * serial port. Useful stuff like that.
501 * ---------------------------------------------------------------
502 */
503
Jiri Slaby588993d2012-03-05 14:52:22 +0100504static int startup(struct tty_struct *tty, struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 unsigned long flags;
507 int retval=0;
508 unsigned long page;
509
510 page = get_zeroed_page(GFP_KERNEL);
511 if (!page)
512 return -ENOMEM;
513
514 local_irq_save(flags);
515
Jiri Slaby916b7652012-03-05 14:52:20 +0100516 if (info->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 free_page(page);
518 goto errout;
519 }
520
521 if (info->xmit.buf)
522 free_page(page);
523 else
524 info->xmit.buf = (unsigned char *) page;
525
526#ifdef SERIAL_DEBUG_OPEN
527 printk("starting up ttys%d ...", info->line);
528#endif
529
530 /* Clear anything in the input buffer */
531
532 custom.intreq = IF_RBF;
533 mb();
534
535 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
536 if (retval) {
537 if (serial_isroot()) {
Jiri Slaby588993d2012-03-05 14:52:22 +0100538 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 retval = 0;
540 }
541 goto errout;
542 }
543
544 /* enable both Rx and Tx interrupts */
545 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
546 mb();
547 info->IER = UART_IER_MSI;
548
549 /* remember current state of the DCD and CTS bits */
550 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 info->MCR = 0;
Jiri Slaby588993d2012-03-05 14:52:22 +0100553 if (C_BAUD(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 info->MCR = SER_DTR | SER_RTS;
555 rtsdtr_ctrl(info->MCR);
556
Jiri Slaby588993d2012-03-05 14:52:22 +0100557 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 info->xmit.head = info->xmit.tail = 0;
559
560 /*
561 * Set up the tty->alt_speed kludge
562 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100563 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
564 tty->alt_speed = 57600;
565 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
566 tty->alt_speed = 115200;
567 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
568 tty->alt_speed = 230400;
569 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
570 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /*
573 * and set the speed of the serial port
574 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100575 change_speed(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Jiri Slaby916b7652012-03-05 14:52:20 +0100577 info->flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 local_irq_restore(flags);
579 return 0;
580
581errout:
582 local_irq_restore(flags);
583 return retval;
584}
585
586/*
587 * This routine will shutdown a serial port; interrupts are disabled, and
588 * DTR is dropped if the hangup on close termio flag is on.
589 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100590static void shutdown(struct tty_struct *tty, struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 unsigned long flags;
593 struct serial_state *state;
594
Jiri Slaby916b7652012-03-05 14:52:20 +0100595 if (!(info->flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return;
597
Jiri Slaby916b7652012-03-05 14:52:20 +0100598 state = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600#ifdef SERIAL_DEBUG_OPEN
601 printk("Shutting down serial port %d ....\n", info->line);
602#endif
603
604 local_irq_save(flags); /* Disable interrupts */
605
606 /*
607 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
608 * here so the queue might never be waken up
609 */
Jiri Slaby87758792012-03-05 14:52:24 +0100610 wake_up_interruptible(&info->tport.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
613 * Free the IRQ, if necessary
614 */
615 free_irq(IRQ_AMIGA_VERTB, info);
616
617 if (info->xmit.buf) {
618 free_page((unsigned long) info->xmit.buf);
619 info->xmit.buf = NULL;
620 }
621
622 info->IER = 0;
623 custom.intena = IF_RBF | IF_TBE;
624 mb();
625
626 /* disable break condition */
627 custom.adkcon = AC_UARTBRK;
628 mb();
629
Jiri Slaby588993d2012-03-05 14:52:22 +0100630 if (tty->termios->c_cflag & HUPCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 info->MCR &= ~(SER_DTR|SER_RTS);
632 rtsdtr_ctrl(info->MCR);
633
Jiri Slaby588993d2012-03-05 14:52:22 +0100634 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Jiri Slaby916b7652012-03-05 14:52:20 +0100636 info->flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 local_irq_restore(flags);
638}
639
640
641/*
642 * This routine is called to set the UART divisor registers to match
643 * the specified baud rate for a serial port.
644 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100645static void change_speed(struct tty_struct *tty, struct serial_state *info,
Alan Cox606d0992006-12-08 02:38:45 -0800646 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 int quot = 0, baud_base, baud;
649 unsigned cflag, cval = 0;
650 int bits;
651 unsigned long flags;
652
Jiri Slaby588993d2012-03-05 14:52:22 +0100653 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* Byte size is always 8 bits plus parity bit if requested */
656
657 cval = 3; bits = 10;
658 if (cflag & CSTOPB) {
659 cval |= 0x04;
660 bits++;
661 }
662 if (cflag & PARENB) {
663 cval |= UART_LCR_PARITY;
664 bits++;
665 }
666 if (!(cflag & PARODD))
667 cval |= UART_LCR_EPAR;
668#ifdef CMSPAR
669 if (cflag & CMSPAR)
670 cval |= UART_LCR_SPAR;
671#endif
672
673 /* Determine divisor based on baud rate */
Jiri Slaby588993d2012-03-05 14:52:22 +0100674 baud = tty_get_baud_rate(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!baud)
676 baud = 9600; /* B0 transition handled in rs_set_termios */
Jiri Slaby916b7652012-03-05 14:52:20 +0100677 baud_base = info->baud_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (baud == 38400 &&
Jiri Slaby916b7652012-03-05 14:52:20 +0100679 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
680 quot = info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 else {
682 if (baud == 134)
683 /* Special case since 134 is really 134.5 */
684 quot = (2*baud_base / 269);
685 else if (baud)
686 quot = baud_base / baud;
687 }
688 /* If the quotient is zero refuse the change */
689 if (!quot && old_termios) {
Alan Coxdb0ef082007-07-15 23:41:47 -0700690 /* FIXME: Will need updating for new tty in the end */
Jiri Slaby588993d2012-03-05 14:52:22 +0100691 tty->termios->c_cflag &= ~CBAUD;
692 tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
693 baud = tty_get_baud_rate(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!baud)
695 baud = 9600;
696 if (baud == 38400 &&
Jiri Slaby916b7652012-03-05 14:52:20 +0100697 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
698 quot = info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 else {
700 if (baud == 134)
701 /* Special case since 134 is really 134.5 */
702 quot = (2*baud_base / 269);
703 else if (baud)
704 quot = baud_base / baud;
705 }
706 }
707 /* As a last resort, if the quotient is zero, default to 9600 bps */
708 if (!quot)
709 quot = baud_base / 9600;
710 info->quot = quot;
Jiri Slaby916b7652012-03-05 14:52:20 +0100711 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 info->timeout += HZ/50; /* Add .02 seconds of slop */
713
714 /* CTS flow control flag and modem status interrupts */
715 info->IER &= ~UART_IER_MSI;
Jiri Slaby916b7652012-03-05 14:52:20 +0100716 if (info->flags & ASYNC_HARDPPS_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 info->IER |= UART_IER_MSI;
718 if (cflag & CRTSCTS) {
Jiri Slaby916b7652012-03-05 14:52:20 +0100719 info->flags |= ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 info->IER |= UART_IER_MSI;
721 } else
Jiri Slaby916b7652012-03-05 14:52:20 +0100722 info->flags &= ~ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (cflag & CLOCAL)
Jiri Slaby916b7652012-03-05 14:52:20 +0100724 info->flags &= ~ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 else {
Jiri Slaby916b7652012-03-05 14:52:20 +0100726 info->flags |= ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 info->IER |= UART_IER_MSI;
728 }
729 /* TBD:
Thadeu Lima de Souza Cascardo4b512d22009-04-14 23:14:10 -0300730 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
732
733 /*
734 * Set up parity check flag
735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
Jiri Slaby588993d2012-03-05 14:52:22 +0100738 if (I_INPCK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Jiri Slaby588993d2012-03-05 14:52:22 +0100740 if (I_BRKINT(tty) || I_PARMRK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 info->read_status_mask |= UART_LSR_BI;
742
743 /*
744 * Characters to ignore
745 */
746 info->ignore_status_mask = 0;
Jiri Slaby588993d2012-03-05 14:52:22 +0100747 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
Jiri Slaby588993d2012-03-05 14:52:22 +0100749 if (I_IGNBRK(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 info->ignore_status_mask |= UART_LSR_BI;
751 /*
752 * If we're ignore parity and break indicators, ignore
753 * overruns too. (For real raw support).
754 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100755 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 info->ignore_status_mask |= UART_LSR_OE;
757 }
758 /*
759 * !!! ignore all characters if CREAD is not set
760 */
761 if ((cflag & CREAD) == 0)
762 info->ignore_status_mask |= UART_LSR_DR;
763 local_irq_save(flags);
764
765 {
766 short serper;
767
768 /* Set up the baud rate */
769 serper = quot - 1;
770
771 /* Enable or disable parity bit */
772
773 if(cval & UART_LCR_PARITY)
774 serper |= (SERPER_PARENB);
775
776 custom.serper = serper;
777 mb();
778 }
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 local_irq_restore(flags);
781}
782
Alan Cox257afa32008-04-30 00:54:02 -0700783static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Jiri Slaby916b7652012-03-05 14:52:20 +0100785 struct serial_state *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 unsigned long flags;
787
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700788 info = tty->driver_data;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
Alan Cox257afa32008-04-30 00:54:02 -0700791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700793 if (!info->xmit.buf)
Alan Cox257afa32008-04-30 00:54:02 -0700794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 local_irq_save(flags);
797 if (CIRC_SPACE(info->xmit.head,
798 info->xmit.tail,
799 SERIAL_XMIT_SIZE) == 0) {
800 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
804 info->xmit.buf[info->xmit.head++] = ch;
805 info->xmit.head &= SERIAL_XMIT_SIZE-1;
806 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700807 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810static void rs_flush_chars(struct tty_struct *tty)
811{
Jiri Slaby916b7652012-03-05 14:52:20 +0100812 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 unsigned long flags;
814
815 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
816 return;
817
818 if (info->xmit.head == info->xmit.tail
819 || tty->stopped
820 || tty->hw_stopped
821 || !info->xmit.buf)
822 return;
823
824 local_irq_save(flags);
825 info->IER |= UART_IER_THRI;
826 custom.intena = IF_SETCLR | IF_TBE;
827 mb();
828 /* set a pending Tx Interrupt, transmitter should restart now */
829 custom.intreq = IF_SETCLR | IF_TBE;
830 mb();
831 local_irq_restore(flags);
832}
833
834static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
835{
836 int c, ret = 0;
Jiri Slaby916b7652012-03-05 14:52:20 +0100837 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 unsigned long flags;
839
840 if (serial_paranoia_check(info, tty->name, "rs_write"))
841 return 0;
842
Jiri Slabyb3218a72006-10-04 02:15:27 -0700843 if (!info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 0;
845
Jiri Kosina3abf3be2007-02-10 01:46:48 -0800846 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 while (1) {
848 c = CIRC_SPACE_TO_END(info->xmit.head,
849 info->xmit.tail,
850 SERIAL_XMIT_SIZE);
851 if (count < c)
852 c = count;
853 if (c <= 0) {
854 break;
855 }
856 memcpy(info->xmit.buf + info->xmit.head, buf, c);
857 info->xmit.head = ((info->xmit.head + c) &
858 (SERIAL_XMIT_SIZE-1));
859 buf += c;
860 count -= c;
861 ret += c;
862 }
863 local_irq_restore(flags);
864
865 if (info->xmit.head != info->xmit.tail
866 && !tty->stopped
867 && !tty->hw_stopped
868 && !(info->IER & UART_IER_THRI)) {
869 info->IER |= UART_IER_THRI;
870 local_irq_disable();
871 custom.intena = IF_SETCLR | IF_TBE;
872 mb();
873 /* set a pending Tx Interrupt, transmitter should restart now */
874 custom.intreq = IF_SETCLR | IF_TBE;
875 mb();
876 local_irq_restore(flags);
877 }
878 return ret;
879}
880
881static int rs_write_room(struct tty_struct *tty)
882{
Jiri Slaby916b7652012-03-05 14:52:20 +0100883 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
886 return 0;
887 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
888}
889
890static int rs_chars_in_buffer(struct tty_struct *tty)
891{
Jiri Slaby916b7652012-03-05 14:52:20 +0100892 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
895 return 0;
896 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
897}
898
899static void rs_flush_buffer(struct tty_struct *tty)
900{
Jiri Slaby916b7652012-03-05 14:52:20 +0100901 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 unsigned long flags;
903
904 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
905 return;
906 local_irq_save(flags);
907 info->xmit.head = info->xmit.tail = 0;
908 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 tty_wakeup(tty);
910}
911
912/*
913 * This function is used to send a high-priority XON/XOFF character to
914 * the device
915 */
916static void rs_send_xchar(struct tty_struct *tty, char ch)
917{
Jiri Slaby916b7652012-03-05 14:52:20 +0100918 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 unsigned long flags;
920
921 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
922 return;
923
924 info->x_char = ch;
925 if (ch) {
926 /* Make sure transmit interrupts are on */
927
928 /* Check this ! */
929 local_irq_save(flags);
930 if(!(custom.intenar & IF_TBE)) {
931 custom.intena = IF_SETCLR | IF_TBE;
932 mb();
933 /* set a pending Tx Interrupt, transmitter should restart now */
934 custom.intreq = IF_SETCLR | IF_TBE;
935 mb();
936 }
937 local_irq_restore(flags);
938
939 info->IER |= UART_IER_THRI;
940 }
941}
942
943/*
944 * ------------------------------------------------------------
945 * rs_throttle()
946 *
947 * This routine is called by the upper-layer tty layer to signal that
948 * incoming characters should be throttled.
949 * ------------------------------------------------------------
950 */
951static void rs_throttle(struct tty_struct * tty)
952{
Jiri Slaby916b7652012-03-05 14:52:20 +0100953 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 unsigned long flags;
955#ifdef SERIAL_DEBUG_THROTTLE
956 char buf[64];
957
958 printk("throttle %s: %d....\n", tty_name(tty, buf),
959 tty->ldisc.chars_in_buffer(tty));
960#endif
961
962 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
963 return;
964
965 if (I_IXOFF(tty))
966 rs_send_xchar(tty, STOP_CHAR(tty));
967
968 if (tty->termios->c_cflag & CRTSCTS)
969 info->MCR &= ~SER_RTS;
970
971 local_irq_save(flags);
972 rtsdtr_ctrl(info->MCR);
973 local_irq_restore(flags);
974}
975
976static void rs_unthrottle(struct tty_struct * tty)
977{
Jiri Slaby916b7652012-03-05 14:52:20 +0100978 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 unsigned long flags;
980#ifdef SERIAL_DEBUG_THROTTLE
981 char buf[64];
982
983 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
984 tty->ldisc.chars_in_buffer(tty));
985#endif
986
987 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
988 return;
989
990 if (I_IXOFF(tty)) {
991 if (info->x_char)
992 info->x_char = 0;
993 else
994 rs_send_xchar(tty, START_CHAR(tty));
995 }
996 if (tty->termios->c_cflag & CRTSCTS)
997 info->MCR |= SER_RTS;
998 local_irq_save(flags);
999 rtsdtr_ctrl(info->MCR);
1000 local_irq_restore(flags);
1001}
1002
1003/*
1004 * ------------------------------------------------------------
1005 * rs_ioctl() and friends
1006 * ------------------------------------------------------------
1007 */
1008
Jiri Slaby916b7652012-03-05 14:52:20 +01001009static int get_serial_info(struct serial_state *state,
Al Viroab14cae2006-01-12 01:06:30 -08001010 struct serial_struct __user * retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 struct serial_struct tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 if (!retinfo)
1015 return -EFAULT;
1016 memset(&tmp, 0, sizeof(tmp));
Arnd Bergmannec79d602010-06-01 22:53:01 +02001017 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 tmp.type = state->type;
1019 tmp.line = state->line;
1020 tmp.port = state->port;
1021 tmp.irq = state->irq;
1022 tmp.flags = state->flags;
1023 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{
1037 struct serial_struct new_serial;
Jiri Slaby0f9b9682012-03-05 14:52:21 +01001038 bool change_spd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 int retval = 0;
1040
1041 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1042 return -EFAULT;
Alan Coxe18ce492008-04-30 00:53:16 -07001043
Arnd Bergmannec79d602010-06-01 22:53:01 +02001044 tty_lock();
Jiri Slaby0f9b9682012-03-05 14:52:21 +01001045 change_spd = ((new_serial.flags ^ state->flags) & ASYNC_SPD_MASK) ||
1046 new_serial.custom_divisor != state->custom_divisor;
1047 if (new_serial.irq != state->irq || new_serial.port != state->port ||
1048 new_serial.xmit_fifo_size != state->xmit_fifo_size) {
1049 tty_unlock();
1050 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 if (!serial_isroot()) {
1054 if ((new_serial.baud_base != state->baud_base) ||
Jiri Slaby799be6f2012-03-05 14:52:25 +01001055 (new_serial.close_delay != state->tport.close_delay) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1057 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1058 (state->flags & ~ASYNC_USR_MASK)))
1059 return -EPERM;
1060 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1061 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 state->custom_divisor = new_serial.custom_divisor;
1063 goto check_and_exit;
1064 }
1065
Alan Coxe18ce492008-04-30 00:53:16 -07001066 if (new_serial.baud_base < 9600) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001067 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /*
1072 * OK, past this point, all the error checking has been done.
1073 * At this point, we start making changes.....
1074 */
1075
1076 state->baud_base = new_serial.baud_base;
1077 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1078 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 state->custom_divisor = new_serial.custom_divisor;
Jiri Slaby799be6f2012-03-05 14:52:25 +01001080 state->tport.close_delay = new_serial.close_delay * HZ/100;
1081 state->tport.closing_wait = new_serial.closing_wait * HZ/100;
Jiri Slaby588993d2012-03-05 14:52:22 +01001082 tty->low_latency = (state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084check_and_exit:
Jiri Slabyfef21072012-03-05 14:52:14 +01001085 if (state->flags & ASYNC_INITIALIZED) {
Jiri Slaby0f9b9682012-03-05 14:52:21 +01001086 if (change_spd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby588993d2012-03-05 14:52:22 +01001088 tty->alt_speed = 57600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby588993d2012-03-05 14:52:22 +01001090 tty->alt_speed = 115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby588993d2012-03-05 14:52:22 +01001092 tty->alt_speed = 230400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby588993d2012-03-05 14:52:22 +01001094 tty->alt_speed = 460800;
1095 change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 } else
Jiri Slaby588993d2012-03-05 14:52:22 +01001098 retval = startup(tty, state);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001099 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return retval;
1101}
1102
1103
1104/*
1105 * get_lsr_info - get line status register info
1106 *
1107 * Purpose: Let user call ioctl() to get info when the UART physically
1108 * is emptied. On bus types like RS485, the transmitter must
1109 * release the bus after transmitting. This must be done when
1110 * the transmit shift register is empty, not be done when the
1111 * transmit holding register is empty. This functionality
1112 * allows an RS485 driver to be written in user space.
1113 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001114static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 unsigned char status;
1117 unsigned int result;
1118 unsigned long flags;
1119
1120 local_irq_save(flags);
1121 status = custom.serdatr;
1122 mb();
1123 local_irq_restore(flags);
1124 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1125 if (copy_to_user(value, &result, sizeof(int)))
1126 return -EFAULT;
1127 return 0;
1128}
1129
1130
Alan Cox60b33c12011-02-14 16:26:14 +00001131static int rs_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Jiri Slaby916b7652012-03-05 14:52:20 +01001133 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 unsigned char control, status;
1135 unsigned long flags;
1136
1137 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1138 return -ENODEV;
1139 if (tty->flags & (1 << TTY_IO_ERROR))
1140 return -EIO;
1141
1142 control = info->MCR;
1143 local_irq_save(flags);
1144 status = ciab.pra;
1145 local_irq_restore(flags);
1146 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1147 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1148 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1149 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1150 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1151}
1152
Alan Cox20b9d172011-02-14 16:26:50 +00001153static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1154 unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Jiri Slaby916b7652012-03-05 14:52:20 +01001156 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 unsigned long flags;
1158
1159 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1160 return -ENODEV;
1161 if (tty->flags & (1 << TTY_IO_ERROR))
1162 return -EIO;
1163
1164 local_irq_save(flags);
1165 if (set & TIOCM_RTS)
1166 info->MCR |= SER_RTS;
1167 if (set & TIOCM_DTR)
1168 info->MCR |= SER_DTR;
1169 if (clear & TIOCM_RTS)
1170 info->MCR &= ~SER_RTS;
1171 if (clear & TIOCM_DTR)
1172 info->MCR &= ~SER_DTR;
1173 rtsdtr_ctrl(info->MCR);
1174 local_irq_restore(flags);
1175 return 0;
1176}
1177
1178/*
1179 * rs_break() --- routine which turns the break handling on or off
1180 */
Alan Cox9e989662008-07-22 11:18:03 +01001181static int rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Jiri Slaby916b7652012-03-05 14:52:20 +01001183 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 unsigned long flags;
1185
1186 if (serial_paranoia_check(info, tty->name, "rs_break"))
Geert Uytterhoeven970a8a52008-08-06 22:19:39 +02001187 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 local_irq_save(flags);
1190 if (break_state == -1)
1191 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1192 else
1193 custom.adkcon = AC_UARTBRK;
1194 mb();
1195 local_irq_restore(flags);
Alan Cox9e989662008-07-22 11:18:03 +01001196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Alan Cox05871022010-09-16 18:21:52 +01001199/*
1200 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1201 * Return: write counters to the user passed counter struct
1202 * NB: both 1->0 and 0->1 transitions are counted except for
1203 * RI where only 0->1 is counted.
1204 */
1205static int rs_get_icount(struct tty_struct *tty,
1206 struct serial_icounter_struct *icount)
1207{
Jiri Slaby916b7652012-03-05 14:52:20 +01001208 struct serial_state *info = tty->driver_data;
Alan Cox05871022010-09-16 18:21:52 +01001209 struct async_icount cnow;
1210 unsigned long flags;
1211
1212 local_irq_save(flags);
Jiri Slaby916b7652012-03-05 14:52:20 +01001213 cnow = info->icount;
Alan Cox05871022010-09-16 18:21:52 +01001214 local_irq_restore(flags);
1215 icount->cts = cnow.cts;
1216 icount->dsr = cnow.dsr;
1217 icount->rng = cnow.rng;
1218 icount->dcd = cnow.dcd;
1219 icount->rx = cnow.rx;
1220 icount->tx = cnow.tx;
1221 icount->frame = cnow.frame;
1222 icount->overrun = cnow.overrun;
1223 icount->parity = cnow.parity;
1224 icount->brk = cnow.brk;
1225 icount->buf_overrun = cnow.buf_overrun;
1226
1227 return 0;
1228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Alan Cox6caa76b2011-02-14 16:27:22 +00001230static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 unsigned int cmd, unsigned long arg)
1232{
Jiri Slaby916b7652012-03-05 14:52:20 +01001233 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct async_icount cprev, cnow; /* kernel counter temps */
Al Viroab14cae2006-01-12 01:06:30 -08001235 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 unsigned long flags;
1237
1238 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1239 return -ENODEV;
1240
1241 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1242 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1243 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1244 if (tty->flags & (1 << TTY_IO_ERROR))
1245 return -EIO;
1246 }
1247
1248 switch (cmd) {
1249 case TIOCGSERIAL:
Al Viroab14cae2006-01-12 01:06:30 -08001250 return get_serial_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 case TIOCSSERIAL:
Jiri Slaby588993d2012-03-05 14:52:22 +01001252 return set_serial_info(tty, info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 case TIOCSERCONFIG:
1254 return 0;
1255
1256 case TIOCSERGETLSR: /* Get line status register */
Al Viroab14cae2006-01-12 01:06:30 -08001257 return get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 case TIOCSERGSTRUCT:
Al Viroab14cae2006-01-12 01:06:30 -08001260 if (copy_to_user(argp,
Jiri Slaby916b7652012-03-05 14:52:20 +01001261 info, sizeof(struct serial_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return -EFAULT;
1263 return 0;
1264
1265 /*
1266 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1267 * - mask passed in arg for lines of interest
1268 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1269 * Caller should use TIOCGICOUNT to see which one it was
1270 */
1271 case TIOCMIWAIT:
1272 local_irq_save(flags);
1273 /* note the counters on entry */
Jiri Slaby916b7652012-03-05 14:52:20 +01001274 cprev = info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 local_irq_restore(flags);
1276 while (1) {
Jiri Slaby87758792012-03-05 14:52:24 +01001277 interruptible_sleep_on(&info->tport.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /* see if a signal did it */
1279 if (signal_pending(current))
1280 return -ERESTARTSYS;
1281 local_irq_save(flags);
Jiri Slaby916b7652012-03-05 14:52:20 +01001282 cnow = info->icount; /* atomic copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 local_irq_restore(flags);
1284 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1285 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1286 return -EIO; /* no change => error */
1287 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1288 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1289 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1290 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1291 return 0;
1292 }
1293 cprev = cnow;
1294 }
1295 /* NOTREACHED */
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 case TIOCSERGWILD:
1298 case TIOCSERSWILD:
1299 /* "setserial -W" is called in Debian boot */
1300 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1301 return 0;
1302
1303 default:
1304 return -ENOIOCTLCMD;
1305 }
1306 return 0;
1307}
1308
Alan Cox606d0992006-12-08 02:38:45 -08001309static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Jiri Slaby916b7652012-03-05 14:52:20 +01001311 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 unsigned long flags;
1313 unsigned int cflag = tty->termios->c_cflag;
1314
Jiri Slaby588993d2012-03-05 14:52:22 +01001315 change_speed(tty, info, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /* Handle transition to B0 status */
1318 if ((old_termios->c_cflag & CBAUD) &&
1319 !(cflag & CBAUD)) {
1320 info->MCR &= ~(SER_DTR|SER_RTS);
1321 local_irq_save(flags);
1322 rtsdtr_ctrl(info->MCR);
1323 local_irq_restore(flags);
1324 }
1325
1326 /* Handle transition away from B0 status */
1327 if (!(old_termios->c_cflag & CBAUD) &&
1328 (cflag & CBAUD)) {
1329 info->MCR |= SER_DTR;
1330 if (!(tty->termios->c_cflag & CRTSCTS) ||
1331 !test_bit(TTY_THROTTLED, &tty->flags)) {
1332 info->MCR |= SER_RTS;
1333 }
1334 local_irq_save(flags);
1335 rtsdtr_ctrl(info->MCR);
1336 local_irq_restore(flags);
1337 }
1338
1339 /* Handle turning off CRTSCTS */
1340 if ((old_termios->c_cflag & CRTSCTS) &&
1341 !(tty->termios->c_cflag & CRTSCTS)) {
1342 tty->hw_stopped = 0;
1343 rs_start(tty);
1344 }
1345
1346#if 0
1347 /*
1348 * No need to wake up processes in open wait, since they
1349 * sample the CLOCAL flag once, and don't recheck it.
1350 * XXX It's not clear whether the current behavior is correct
1351 * or not. Hence, this may change.....
1352 */
1353 if (!(old_termios->c_cflag & CLOCAL) &&
1354 (tty->termios->c_cflag & CLOCAL))
1355 wake_up_interruptible(&info->open_wait);
1356#endif
1357}
1358
1359/*
1360 * ------------------------------------------------------------
1361 * rs_close()
1362 *
1363 * This routine is called when the serial port gets closed. First, we
1364 * wait for the last remaining data to be sent. Then, we unlink its
1365 * async structure from the interrupt chain if necessary, and we free
1366 * that IRQ if nothing is left in the chain.
1367 * ------------------------------------------------------------
1368 */
1369static void rs_close(struct tty_struct *tty, struct file * filp)
1370{
Jiri Slaby916b7652012-03-05 14:52:20 +01001371 struct serial_state *state = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 unsigned long flags;
1373
Jiri Slaby916b7652012-03-05 14:52:20 +01001374 if (!state || serial_paranoia_check(state, tty->name, "rs_close"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 local_irq_save(flags);
1378
1379 if (tty_hung_up_p(filp)) {
1380 DBG_CNT("before DEC-hung");
1381 local_irq_restore(flags);
1382 return;
1383 }
1384
1385#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby12c80352012-03-05 14:52:26 +01001386 printk("rs_close ttys%d, count = %d\n", state->line, state->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387#endif
Jiri Slaby12c80352012-03-05 14:52:26 +01001388 if ((tty->count == 1) && (state->tport.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 /*
1390 * Uh, oh. tty->count is 1, which means that the tty
Jiri Slaby12c80352012-03-05 14:52:26 +01001391 * structure will be freed. state->tport.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * be one in these conditions. If it's greater than
1393 * one, we've got real problems, since it means the
1394 * serial port won't be shutdown.
1395 */
1396 printk("rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby12c80352012-03-05 14:52:26 +01001397 "state->tport.count is %d\n", state->tport.count);
1398 state->tport.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Jiri Slaby12c80352012-03-05 14:52:26 +01001400 if (--state->tport.count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 printk("rs_close: bad serial port count for ttys%d: %d\n",
Jiri Slaby12c80352012-03-05 14:52:26 +01001402 state->line, state->tport.count);
1403 state->tport.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
Jiri Slaby12c80352012-03-05 14:52:26 +01001405 if (state->tport.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 DBG_CNT("before DEC-2");
1407 local_irq_restore(flags);
1408 return;
1409 }
Jiri Slabyfef21072012-03-05 14:52:14 +01001410 state->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /*
1412 * Now we wait for the transmit buffer to clear; and we notify
1413 * the line discipline to only process XON/XOFF characters.
1414 */
1415 tty->closing = 1;
Jiri Slaby799be6f2012-03-05 14:52:25 +01001416 if (state->tport.closing_wait != ASYNC_CLOSING_WAIT_NONE)
1417 tty_wait_until_sent(tty, state->tport.closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /*
1419 * At this point we stop accepting input. To do this, we
1420 * disable the receive line status interrupts, and tell the
1421 * interrupt driver to stop checking the data ready bit in the
1422 * line status register.
1423 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001424 state->read_status_mask &= ~UART_LSR_DR;
Jiri Slabyfef21072012-03-05 14:52:14 +01001425 if (state->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /* disable receive interrupts */
1427 custom.intena = IF_RBF;
1428 mb();
1429 /* clear any pending receive interrupt */
1430 custom.intreq = IF_RBF;
1431 mb();
1432
1433 /*
1434 * Before we drop DTR, make sure the UART transmitter
1435 * has completely drained; this is especially
1436 * important if there is a transmit FIFO!
1437 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001438 rs_wait_until_sent(tty, state->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
Jiri Slaby588993d2012-03-05 14:52:22 +01001440 shutdown(tty, state);
Alan Cox978e5952008-04-30 00:53:59 -07001441 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 tty_ldisc_flush(tty);
1444 tty->closing = 0;
Jiri Slaby87758792012-03-05 14:52:24 +01001445 state->tport.tty = NULL;
1446 if (state->tport.blocked_open) {
Jiri Slaby799be6f2012-03-05 14:52:25 +01001447 if (state->tport.close_delay) {
1448 msleep_interruptible(jiffies_to_msecs(state->tport.close_delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
Jiri Slaby87758792012-03-05 14:52:24 +01001450 wake_up_interruptible(&state->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
Jiri Slabyfef21072012-03-05 14:52:14 +01001452 state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slaby87758792012-03-05 14:52:24 +01001453 wake_up_interruptible(&state->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 local_irq_restore(flags);
1455}
1456
1457/*
1458 * rs_wait_until_sent() --- wait until the transmitter is empty
1459 */
1460static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1461{
Jiri Slaby916b7652012-03-05 14:52:20 +01001462 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 unsigned long orig_jiffies, char_time;
1464 int lsr;
1465
1466 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1467 return;
1468
Jiri Slaby916b7652012-03-05 14:52:20 +01001469 if (info->xmit_fifo_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return; /* Just in case.... */
1471
1472 orig_jiffies = jiffies;
Alan Cox978e5952008-04-30 00:53:59 -07001473
Arnd Bergmann20365212010-06-01 22:53:07 +02001474 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * Set the check interval to be 1/5 of the estimated time to
1476 * send a single character, and make it at least 1. The check
1477 * interval should also be less than the timeout.
1478 *
1479 * Note: we have to use pretty tight timings here to satisfy
1480 * the NIST-PCTS.
1481 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001482 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 char_time = char_time / 5;
1484 if (char_time == 0)
1485 char_time = 1;
1486 if (timeout)
1487 char_time = min_t(unsigned long, char_time, timeout);
1488 /*
1489 * If the transmitter hasn't cleared in twice the approximate
1490 * amount of time to send the entire FIFO, it probably won't
1491 * ever clear. This assumes the UART isn't doing flow
1492 * control, which is currently the case. Hence, if it ever
1493 * takes longer than info->timeout, this is probably due to a
1494 * UART bug of some kind. So, we clamp the timeout parameter at
1495 * 2*info->timeout.
1496 */
1497 if (!timeout || timeout > 2*info->timeout)
1498 timeout = 2*info->timeout;
1499#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1500 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1501 printk("jiff=%lu...", jiffies);
1502#endif
1503 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1504#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1505 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1506#endif
1507 msleep_interruptible(jiffies_to_msecs(char_time));
1508 if (signal_pending(current))
1509 break;
1510 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1511 break;
1512 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001513 __set_current_state(TASK_RUNNING);
Jiri Slabyeff4b0b2011-07-14 14:35:13 +02001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1516 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1517#endif
1518}
1519
1520/*
1521 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1522 */
1523static void rs_hangup(struct tty_struct *tty)
1524{
Jiri Slaby916b7652012-03-05 14:52:20 +01001525 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1528 return;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 rs_flush_buffer(tty);
Jiri Slaby588993d2012-03-05 14:52:22 +01001531 shutdown(tty, info);
Jiri Slaby12c80352012-03-05 14:52:26 +01001532 info->tport.count = 0;
Jiri Slaby916b7652012-03-05 14:52:20 +01001533 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby87758792012-03-05 14:52:24 +01001534 info->tport.tty = NULL;
1535 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
1538/*
1539 * ------------------------------------------------------------
1540 * rs_open() and friends
1541 * ------------------------------------------------------------
1542 */
1543static int block_til_ready(struct tty_struct *tty, struct file * filp,
Jiri Slaby916b7652012-03-05 14:52:20 +01001544 struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546#ifdef DECLARE_WAITQUEUE
1547 DECLARE_WAITQUEUE(wait, current);
1548#else
1549 struct wait_queue wait = { current, NULL };
1550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 int retval;
1552 int do_clocal = 0, extra_count = 0;
1553 unsigned long flags;
1554
1555 /*
1556 * If the device is in the middle of being closed, then block
1557 * until it's done, and then try again.
1558 */
1559 if (tty_hung_up_p(filp) ||
Jiri Slaby916b7652012-03-05 14:52:20 +01001560 (info->flags & ASYNC_CLOSING)) {
1561 if (info->flags & ASYNC_CLOSING)
Jiri Slaby87758792012-03-05 14:52:24 +01001562 interruptible_sleep_on(&info->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563#ifdef SERIAL_DO_RESTART
Jiri Slaby916b7652012-03-05 14:52:20 +01001564 return ((info->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 -EAGAIN : -ERESTARTSYS);
1566#else
1567 return -EAGAIN;
1568#endif
1569 }
1570
1571 /*
1572 * If non-blocking mode is set, or the port is not enabled,
1573 * then make the check up front and then exit.
1574 */
1575 if ((filp->f_flags & O_NONBLOCK) ||
1576 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slaby916b7652012-03-05 14:52:20 +01001577 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579 }
1580
1581 if (tty->termios->c_cflag & CLOCAL)
1582 do_clocal = 1;
1583
1584 /*
1585 * Block waiting for the carrier detect and the line to become
1586 * free (i.e., not in use by the callout). While we are in
Jiri Slaby12c80352012-03-05 14:52:26 +01001587 * this loop, info->tport.count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 * rs_close() knows when to free things. We restore it upon
1589 * exit, either normal or abnormal.
1590 */
1591 retval = 0;
Jiri Slaby87758792012-03-05 14:52:24 +01001592 add_wait_queue(&info->tport.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593#ifdef SERIAL_DEBUG_OPEN
1594 printk("block_til_ready before block: ttys%d, count = %d\n",
Jiri Slaby12c80352012-03-05 14:52:26 +01001595 info->line, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596#endif
1597 local_irq_save(flags);
1598 if (!tty_hung_up_p(filp)) {
1599 extra_count = 1;
Jiri Slaby12c80352012-03-05 14:52:26 +01001600 info->tport.count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602 local_irq_restore(flags);
Jiri Slaby87758792012-03-05 14:52:24 +01001603 info->tport.blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 while (1) {
1605 local_irq_save(flags);
1606 if (tty->termios->c_cflag & CBAUD)
1607 rtsdtr_ctrl(SER_DTR|SER_RTS);
1608 local_irq_restore(flags);
1609 set_current_state(TASK_INTERRUPTIBLE);
1610 if (tty_hung_up_p(filp) ||
Jiri Slaby916b7652012-03-05 14:52:20 +01001611 !(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612#ifdef SERIAL_DO_RESTART
Jiri Slaby916b7652012-03-05 14:52:20 +01001613 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 retval = -EAGAIN;
1615 else
1616 retval = -ERESTARTSYS;
1617#else
1618 retval = -EAGAIN;
1619#endif
1620 break;
1621 }
Jiri Slaby916b7652012-03-05 14:52:20 +01001622 if (!(info->flags & ASYNC_CLOSING) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1624 break;
1625 if (signal_pending(current)) {
1626 retval = -ERESTARTSYS;
1627 break;
1628 }
1629#ifdef SERIAL_DEBUG_OPEN
1630 printk("block_til_ready blocking: ttys%d, count = %d\n",
Jiri Slaby12c80352012-03-05 14:52:26 +01001631 info->line, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632#endif
Arnd Bergmanne142a312010-06-01 22:53:10 +02001633 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001635 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001637 __set_current_state(TASK_RUNNING);
Jiri Slaby87758792012-03-05 14:52:24 +01001638 remove_wait_queue(&info->tport.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (extra_count)
Jiri Slaby12c80352012-03-05 14:52:26 +01001640 info->tport.count++;
Jiri Slaby87758792012-03-05 14:52:24 +01001641 info->tport.blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642#ifdef SERIAL_DEBUG_OPEN
1643 printk("block_til_ready after blocking: ttys%d, count = %d\n",
Jiri Slaby12c80352012-03-05 14:52:26 +01001644 info->line, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645#endif
1646 if (retval)
1647 return retval;
Jiri Slaby916b7652012-03-05 14:52:20 +01001648 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return 0;
1650}
1651
1652/*
1653 * This routine is called whenever a serial port is opened. It
1654 * enables interrupts for a serial port, linking in its async structure into
1655 * the IRQ chain. It also performs the serial-specific
1656 * initialization for the tty structure.
1657 */
1658static int rs_open(struct tty_struct *tty, struct file * filp)
1659{
Jiri Slaby916b7652012-03-05 14:52:20 +01001660 struct serial_state *info = rs_table + tty->index;
Jiri Slaby410235f2012-03-05 14:52:01 +01001661 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Jiri Slaby12c80352012-03-05 14:52:26 +01001663 info->tport.count++;
Jiri Slaby87758792012-03-05 14:52:24 +01001664 info->tport.tty = tty;
Jiri Slaby916b7652012-03-05 14:52:20 +01001665 tty->driver_data = info;
Jiri Slaby87758792012-03-05 14:52:24 +01001666 tty->port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (serial_paranoia_check(info, tty->name, "rs_open"))
1668 return -ENODEV;
1669
1670#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby916b7652012-03-05 14:52:20 +01001671 printk("rs_open %s, count = %d\n", tty->name, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672#endif
Jiri Slaby916b7652012-03-05 14:52:20 +01001673 tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /*
1676 * If the port is the middle of closing, bail out now
1677 */
1678 if (tty_hung_up_p(filp) ||
Jiri Slaby916b7652012-03-05 14:52:20 +01001679 (info->flags & ASYNC_CLOSING)) {
1680 if (info->flags & ASYNC_CLOSING)
Jiri Slaby87758792012-03-05 14:52:24 +01001681 interruptible_sleep_on(&info->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682#ifdef SERIAL_DO_RESTART
Jiri Slaby916b7652012-03-05 14:52:20 +01001683 return ((info->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 -EAGAIN : -ERESTARTSYS);
1685#else
1686 return -EAGAIN;
1687#endif
1688 }
1689
1690 /*
1691 * Start up serial port
1692 */
Jiri Slaby588993d2012-03-05 14:52:22 +01001693 retval = startup(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (retval) {
1695 return retval;
1696 }
1697
1698 retval = block_til_ready(tty, filp, info);
1699 if (retval) {
1700#ifdef SERIAL_DEBUG_OPEN
1701 printk("rs_open returning after block_til_ready with %d\n",
1702 retval);
1703#endif
1704 return retval;
1705 }
1706
1707#ifdef SERIAL_DEBUG_OPEN
1708 printk("rs_open %s successful...", tty->name);
1709#endif
1710 return 0;
1711}
1712
1713/*
1714 * /proc fs routines....
1715 */
1716
Alexey Dobriyand5940272009-03-31 15:19:23 -07001717static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 char stat_buf[30], control, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 unsigned long flags;
1721
Alexey Dobriyand5940272009-03-31 15:19:23 -07001722 seq_printf(m, "%d: uart:amiga_builtin",state->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 local_irq_save(flags);
1725 status = ciab.pra;
Jiri Slaby916b7652012-03-05 14:52:20 +01001726 control = (state->flags & ASYNC_INITIALIZED) ? state->MCR : status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 local_irq_restore(flags);
1728
1729 stat_buf[0] = 0;
1730 stat_buf[1] = 0;
1731 if(!(control & SER_RTS))
1732 strcat(stat_buf, "|RTS");
1733 if(!(status & SER_CTS))
1734 strcat(stat_buf, "|CTS");
1735 if(!(control & SER_DTR))
1736 strcat(stat_buf, "|DTR");
1737 if(!(status & SER_DSR))
1738 strcat(stat_buf, "|DSR");
1739 if(!(status & SER_DCD))
1740 strcat(stat_buf, "|CD");
1741
Jiri Slaby916b7652012-03-05 14:52:20 +01001742 if (state->quot)
1743 seq_printf(m, " baud:%d", state->baud_base / state->quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Alexey Dobriyand5940272009-03-31 15:19:23 -07001745 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 if (state->icount.frame)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001748 seq_printf(m, " fe:%d", state->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 if (state->icount.parity)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001751 seq_printf(m, " pe:%d", state->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 if (state->icount.brk)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001754 seq_printf(m, " brk:%d", state->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 if (state->icount.overrun)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001757 seq_printf(m, " oe:%d", state->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 /*
1760 * Last thing is the RS-232 status lines
1761 */
Alexey Dobriyand5940272009-03-31 15:19:23 -07001762 seq_printf(m, " %s\n", stat_buf+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Alexey Dobriyand5940272009-03-31 15:19:23 -07001765static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Alexey Dobriyand5940272009-03-31 15:19:23 -07001767 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1768 line_info(m, &rs_table[0]);
1769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Alexey Dobriyand5940272009-03-31 15:19:23 -07001772static int rs_proc_open(struct inode *inode, struct file *file)
1773{
1774 return single_open(file, rs_proc_show, NULL);
1775}
1776
1777static const struct file_operations rs_proc_fops = {
1778 .owner = THIS_MODULE,
1779 .open = rs_proc_open,
1780 .read = seq_read,
1781 .llseek = seq_lseek,
1782 .release = single_release,
1783};
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785/*
1786 * ---------------------------------------------------------------------
1787 * rs_init() and friends
1788 *
1789 * rs_init() is called at boot-time to initialize the serial driver.
1790 * ---------------------------------------------------------------------
1791 */
1792
1793/*
1794 * This routine prints out the appropriate serial driver version
1795 * number, and identifies which options were configured into this
1796 * driver.
1797 */
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001798static void show_serial_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1801}
1802
1803
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001804static const struct tty_operations serial_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 .open = rs_open,
1806 .close = rs_close,
1807 .write = rs_write,
1808 .put_char = rs_put_char,
1809 .flush_chars = rs_flush_chars,
1810 .write_room = rs_write_room,
1811 .chars_in_buffer = rs_chars_in_buffer,
1812 .flush_buffer = rs_flush_buffer,
1813 .ioctl = rs_ioctl,
1814 .throttle = rs_throttle,
1815 .unthrottle = rs_unthrottle,
1816 .set_termios = rs_set_termios,
1817 .stop = rs_stop,
1818 .start = rs_start,
1819 .hangup = rs_hangup,
1820 .break_ctl = rs_break,
1821 .send_xchar = rs_send_xchar,
1822 .wait_until_sent = rs_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 .tiocmget = rs_tiocmget,
1824 .tiocmset = rs_tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01001825 .get_icount = rs_get_icount,
Alexey Dobriyand5940272009-03-31 15:19:23 -07001826 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827};
1828
1829/*
1830 * The serial driver boot-time initialization code!
1831 */
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001832static int __init amiga_serial_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 unsigned long flags;
1835 struct serial_state * state;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001836 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Jiri Slaby410235f2012-03-05 14:52:01 +01001838 serial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (!serial_driver)
1840 return -ENOMEM;
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 show_serial_version();
1843
1844 /* Initialize the tty_driver structure */
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 serial_driver->driver_name = "amiserial";
1847 serial_driver->name = "ttyS";
1848 serial_driver->major = TTY_MAJOR;
1849 serial_driver->minor_start = 64;
1850 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1851 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1852 serial_driver->init_termios = tty_std_termios;
1853 serial_driver->init_termios.c_cflag =
1854 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1855 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1856 tty_set_operations(serial_driver, &serial_ops);
1857
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001858 error = tty_register_driver(serial_driver);
1859 if (error)
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001860 goto fail_put_tty_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 state = rs_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 state->port = (int)&custom.serdatr; /* Just to give it a value */
1864 state->line = 0;
1865 state->custom_divisor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 state->icount.cts = state->icount.dsr =
1867 state->icount.rng = state->icount.dcd = 0;
1868 state->icount.rx = state->icount.tx = 0;
1869 state->icount.frame = state->icount.parity = 0;
1870 state->icount.overrun = state->icount.brk = 0;
Jiri Slaby87758792012-03-05 14:52:24 +01001871 tty_port_init(&state->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
1874 state->line);
1875
1876 /* Hardware set up */
1877
1878 state->baud_base = amiga_colorclock;
1879 state->xmit_fifo_size = 1;
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /* set ISRs, and then disable the rx interrupts */
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001882 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1883 if (error)
1884 goto fail_unregister;
1885
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001886 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001887 "serial RX", state);
1888 if (error)
1889 goto fail_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Julia Lawallc70c0362010-04-06 14:34:46 -07001891 local_irq_save(flags);
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 /* turn off Rx and Tx interrupts */
1894 custom.intena = IF_RBF | IF_TBE;
1895 mb();
1896
1897 /* clear any pending interrupt */
1898 custom.intreq = IF_RBF | IF_TBE;
1899 mb();
1900
1901 local_irq_restore(flags);
1902
1903 /*
1904 * set the appropriate directions for the modem control flags,
1905 * and clear RTS and DTR
1906 */
1907 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1908 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1909
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001910 platform_set_drvdata(pdev, state);
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return 0;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001913
1914fail_free_irq:
1915 free_irq(IRQ_AMIGA_TBE, state);
1916fail_unregister:
1917 tty_unregister_driver(serial_driver);
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001918fail_put_tty_driver:
1919 put_tty_driver(serial_driver);
1920 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001923static int __exit amiga_serial_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 int error;
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001926 struct serial_state *state = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if ((error = tty_unregister_driver(serial_driver)))
1930 printk("SERIAL: failed to unregister serial driver (%d)\n",
1931 error);
1932 put_tty_driver(serial_driver);
1933
Jiri Slaby916b7652012-03-05 14:52:20 +01001934 free_irq(IRQ_AMIGA_TBE, state);
1935 free_irq(IRQ_AMIGA_RBF, state);
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001936
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001937 platform_set_drvdata(pdev, NULL);
1938
1939 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001942static struct platform_driver amiga_serial_driver = {
1943 .remove = __exit_p(amiga_serial_remove),
1944 .driver = {
1945 .name = "amiga-serial",
1946 .owner = THIS_MODULE,
1947 },
1948};
1949
1950static int __init amiga_serial_init(void)
1951{
1952 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
1953}
1954
1955module_init(amiga_serial_init);
1956
1957static void __exit amiga_serial_exit(void)
1958{
1959 platform_driver_unregister(&amiga_serial_driver);
1960}
1961
1962module_exit(amiga_serial_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01001965#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967/*
1968 * ------------------------------------------------------------
1969 * Serial console driver
1970 * ------------------------------------------------------------
1971 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973static void amiga_serial_putc(char c)
1974{
1975 custom.serdat = (unsigned char)c | 0x100;
1976 while (!(custom.serdatr & 0x2000))
1977 barrier();
1978}
1979
1980/*
1981 * Print a string to the serial port trying not to disturb
1982 * any possible real use of the port...
1983 *
1984 * The console must be locked when we get here.
1985 */
1986static void serial_console_write(struct console *co, const char *s,
1987 unsigned count)
1988{
1989 unsigned short intena = custom.intenar;
1990
1991 custom.intena = IF_TBE;
1992
1993 while (count--) {
1994 if (*s == '\n')
1995 amiga_serial_putc('\r');
1996 amiga_serial_putc(*s++);
1997 }
1998
1999 custom.intena = IF_SETCLR | (intena & IF_TBE);
2000}
2001
2002static struct tty_driver *serial_console_device(struct console *c, int *index)
2003{
2004 *index = 0;
2005 return serial_driver;
2006}
2007
2008static struct console sercons = {
2009 .name = "ttyS",
2010 .write = serial_console_write,
2011 .device = serial_console_device,
2012 .flags = CON_PRINTBUFFER,
2013 .index = -1,
2014};
2015
2016/*
2017 * Register console.
2018 */
2019static int __init amiserial_console_init(void)
2020{
2021 register_console(&sercons);
2022 return 0;
2023}
2024console_initcall(amiserial_console_init);
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01002025
2026#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028MODULE_LICENSE("GPL");
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02002029MODULE_ALIAS("platform:amiga-serial");