TTY: amiserial, remove IRQ_ports
[linux-2.6-block.git] / drivers / tty / amiserial.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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
1da177e4
LT
32#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
1da177e4
LT
46#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
47#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
fef21073 48 tty->name, (info->state->flags), serial_driver->refcount,info->count,tty->count,s)
1da177e4
LT
49#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>
d594027d 80#include <linux/seq_file.h>
1da177e4
LT
81#include <linux/slab.h>
82#include <linux/init.h>
83#include <linux/bitops.h>
826e8c8c 84#include <linux/platform_device.h>
1da177e4
LT
85
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
b4290a23 95#define custom amiga_custom
1da177e4
LT
96static 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
1da177e4
LT
103static unsigned char current_ctl_bits;
104
606d099c 105static void change_speed(struct async_struct *info, struct ktermios *old);
1da177e4
LT
106static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
107
108
109static struct serial_state rs_table[1];
110
fe971071 111#define NR_PORTS ARRAY_SIZE(rs_table)
1da177e4 112
1da177e4
LT
113#include <asm/uaccess.h>
114
115#define serial_isroot() (capable(CAP_SYS_ADMIN))
116
117
118static inline int serial_paranoia_check(struct async_struct *info,
119 char *name, const char *routine)
120{
121#ifdef SERIAL_PARANOIA_CHECK
122 static const char *badmagic =
123 "Warning: bad magic number for serial struct (%s) in %s\n";
124 static const char *badinfo =
125 "Warning: null async_struct for (%s) in %s\n";
126
127 if (!info) {
128 printk(badinfo, name, routine);
129 return 1;
130 }
131 if (info->magic != SERIAL_MAGIC) {
132 printk(badmagic, name, routine);
133 return 1;
134 }
135#endif
136 return 0;
137}
138
139/* some serial hardware definitions */
140#define SDR_OVRUN (1<<15)
141#define SDR_RBF (1<<14)
142#define SDR_TBE (1<<13)
143#define SDR_TSRE (1<<12)
144
145#define SERPER_PARENB (1<<15)
146
147#define AC_SETCLR (1<<15)
148#define AC_UARTBRK (1<<11)
149
150#define SER_DTR (1<<7)
151#define SER_RTS (1<<6)
152#define SER_DCD (1<<5)
153#define SER_CTS (1<<4)
154#define SER_DSR (1<<3)
155
156static __inline__ void rtsdtr_ctrl(int bits)
157{
158 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
159}
160
161/*
162 * ------------------------------------------------------------
163 * rs_stop() and rs_start()
164 *
165 * This routines are called before setting or resetting tty->stopped.
166 * They enable or disable transmitter interrupts, as necessary.
167 * ------------------------------------------------------------
168 */
169static void rs_stop(struct tty_struct *tty)
170{
c9f19e96 171 struct async_struct *info = tty->driver_data;
1da177e4
LT
172 unsigned long flags;
173
174 if (serial_paranoia_check(info, tty->name, "rs_stop"))
175 return;
176
177 local_irq_save(flags);
178 if (info->IER & UART_IER_THRI) {
179 info->IER &= ~UART_IER_THRI;
180 /* disable Tx interrupt and remove any pending interrupts */
181 custom.intena = IF_TBE;
182 mb();
183 custom.intreq = IF_TBE;
184 mb();
185 }
186 local_irq_restore(flags);
187}
188
189static void rs_start(struct tty_struct *tty)
190{
c9f19e96 191 struct async_struct *info = tty->driver_data;
1da177e4
LT
192 unsigned long flags;
193
194 if (serial_paranoia_check(info, tty->name, "rs_start"))
195 return;
196
197 local_irq_save(flags);
198 if (info->xmit.head != info->xmit.tail
199 && info->xmit.buf
200 && !(info->IER & UART_IER_THRI)) {
201 info->IER |= UART_IER_THRI;
202 custom.intena = IF_SETCLR | IF_TBE;
203 mb();
204 /* set a pending Tx Interrupt, transmitter should restart now */
205 custom.intreq = IF_SETCLR | IF_TBE;
206 mb();
207 }
208 local_irq_restore(flags);
209}
210
211/*
212 * ----------------------------------------------------------------------
213 *
214 * Here starts the interrupt handling routines. All of the following
215 * subroutines are declared as inline and are folded into
216 * rs_interrupt(). They were separated out for readability's sake.
217 *
218 * Note: rs_interrupt() is a "fast" interrupt, which means that it
219 * runs with interrupts turned off. People who may want to modify
220 * rs_interrupt() should try to keep the interrupt handler as fast as
221 * possible. After you are done making modifications, it is not a bad
222 * idea to do:
223 *
224 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
225 *
226 * and look at the resulting assemble code in serial.s.
227 *
228 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
229 * -----------------------------------------------------------------------
230 */
231
41c28ff1 232static void receive_chars(struct async_struct *info)
1da177e4
LT
233{
234 int status;
235 int serdatr;
236 struct tty_struct *tty = info->tty;
33f0f88f 237 unsigned char ch, flag;
1da177e4 238 struct async_icount *icount;
33f0f88f 239 int oe = 0;
1da177e4
LT
240
241 icount = &info->state->icount;
242
243 status = UART_LSR_DR; /* We obviously have a character! */
244 serdatr = custom.serdatr;
245 mb();
246 custom.intreq = IF_RBF;
247 mb();
248
249 if((serdatr & 0x1ff) == 0)
250 status |= UART_LSR_BI;
251 if(serdatr & SDR_OVRUN)
252 status |= UART_LSR_OE;
253
254 ch = serdatr & 0xff;
1da177e4
LT
255 icount->rx++;
256
257#ifdef SERIAL_DEBUG_INTR
258 printk("DR%02x:%02x...", ch, status);
259#endif
33f0f88f 260 flag = TTY_NORMAL;
1da177e4
LT
261
262 /*
263 * We don't handle parity or frame errors - but I have left
264 * the code in, since I'm not sure that the errors can't be
265 * detected.
266 */
267
268 if (status & (UART_LSR_BI | UART_LSR_PE |
269 UART_LSR_FE | UART_LSR_OE)) {
270 /*
271 * For statistics only
272 */
273 if (status & UART_LSR_BI) {
274 status &= ~(UART_LSR_FE | UART_LSR_PE);
275 icount->brk++;
276 } else if (status & UART_LSR_PE)
277 icount->parity++;
278 else if (status & UART_LSR_FE)
279 icount->frame++;
280 if (status & UART_LSR_OE)
281 icount->overrun++;
282
283 /*
284 * Now check to see if character should be
285 * ignored, and mask off conditions which
286 * should be ignored.
287 */
288 if (status & info->ignore_status_mask)
33f0f88f 289 goto out;
1da177e4
LT
290
291 status &= info->read_status_mask;
292
293 if (status & (UART_LSR_BI)) {
294#ifdef SERIAL_DEBUG_INTR
295 printk("handling break....");
296#endif
33f0f88f 297 flag = TTY_BREAK;
fef21073 298 if (info->state->flags & ASYNC_SAK)
1da177e4
LT
299 do_SAK(tty);
300 } else if (status & UART_LSR_PE)
33f0f88f 301 flag = TTY_PARITY;
1da177e4 302 else if (status & UART_LSR_FE)
33f0f88f 303 flag = TTY_FRAME;
1da177e4
LT
304 if (status & UART_LSR_OE) {
305 /*
306 * Overrun is special, since it's
307 * reported immediately, and doesn't
308 * affect the current character
309 */
33f0f88f 310 oe = 1;
1da177e4
LT
311 }
312 }
33f0f88f
AC
313 tty_insert_flip_char(tty, ch, flag);
314 if (oe == 1)
315 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1da177e4 316 tty_flip_buffer_push(tty);
33f0f88f
AC
317out:
318 return;
1da177e4
LT
319}
320
41c28ff1 321static void transmit_chars(struct async_struct *info)
1da177e4
LT
322{
323 custom.intreq = IF_TBE;
324 mb();
325 if (info->x_char) {
326 custom.serdat = info->x_char | 0x100;
327 mb();
328 info->state->icount.tx++;
329 info->x_char = 0;
330 return;
331 }
332 if (info->xmit.head == info->xmit.tail
333 || info->tty->stopped
334 || info->tty->hw_stopped) {
335 info->IER &= ~UART_IER_THRI;
336 custom.intena = IF_TBE;
337 mb();
338 return;
339 }
340
341 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
342 mb();
343 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
344 info->state->icount.tx++;
345
346 if (CIRC_CNT(info->xmit.head,
347 info->xmit.tail,
348 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
c5f0508b 349 tty_wakeup(info->tty);
1da177e4
LT
350
351#ifdef SERIAL_DEBUG_INTR
352 printk("THRE...");
353#endif
354 if (info->xmit.head == info->xmit.tail) {
355 custom.intena = IF_TBE;
356 mb();
357 info->IER &= ~UART_IER_THRI;
358 }
359}
360
41c28ff1 361static void check_modem_status(struct async_struct *info)
1da177e4
LT
362{
363 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
364 unsigned char dstatus;
365 struct async_icount *icount;
366
367 /* Determine bits that have changed */
368 dstatus = status ^ current_ctl_bits;
369 current_ctl_bits = status;
370
371 if (dstatus) {
372 icount = &info->state->icount;
373 /* update input line counters */
374 if (dstatus & SER_DSR)
375 icount->dsr++;
376 if (dstatus & SER_DCD) {
377 icount->dcd++;
378#ifdef CONFIG_HARD_PPS
fef21073 379 if ((info->state->flags & ASYNC_HARDPPS_CD) &&
1da177e4
LT
380 !(status & SER_DCD))
381 hardpps();
382#endif
383 }
384 if (dstatus & SER_CTS)
385 icount->cts++;
386 wake_up_interruptible(&info->delta_msr_wait);
387 }
388
fef21073 389 if ((info->state->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
1da177e4
LT
390#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
391 printk("ttyS%d CD now %s...", info->line,
392 (!(status & SER_DCD)) ? "on" : "off");
393#endif
394 if (!(status & SER_DCD))
395 wake_up_interruptible(&info->open_wait);
396 else {
397#ifdef SERIAL_DEBUG_OPEN
398 printk("doing serial hangup...");
399#endif
400 if (info->tty)
401 tty_hangup(info->tty);
402 }
403 }
fef21073 404 if (info->state->flags & ASYNC_CTS_FLOW) {
1da177e4
LT
405 if (info->tty->hw_stopped) {
406 if (!(status & SER_CTS)) {
407#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
408 printk("CTS tx start...");
409#endif
410 info->tty->hw_stopped = 0;
411 info->IER |= UART_IER_THRI;
412 custom.intena = IF_SETCLR | IF_TBE;
413 mb();
414 /* set a pending Tx Interrupt, transmitter should restart now */
415 custom.intreq = IF_SETCLR | IF_TBE;
416 mb();
c5f0508b 417 tty_wakeup(info->tty);
1da177e4
LT
418 return;
419 }
420 } else {
421 if ((status & SER_CTS)) {
422#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
423 printk("CTS tx stop...");
424#endif
425 info->tty->hw_stopped = 1;
426 info->IER &= ~UART_IER_THRI;
427 /* disable Tx interrupt and remove any pending interrupts */
428 custom.intena = IF_TBE;
429 mb();
430 custom.intreq = IF_TBE;
431 mb();
432 }
433 }
434 }
435}
436
7d12e780 437static irqreturn_t ser_vbl_int( int irq, void *data)
1da177e4
LT
438{
439 /* vbl is just a periodic interrupt we tie into to update modem status */
13c90621 440 struct async_struct *info = data;
1da177e4
LT
441 /*
442 * TBD - is it better to unregister from this interrupt or to
443 * ignore it if MSI is clear ?
444 */
445 if(info->IER & UART_IER_MSI)
446 check_modem_status(info);
447 return IRQ_HANDLED;
448}
449
7d12e780 450static irqreturn_t ser_rx_int(int irq, void *dev_id)
1da177e4 451{
13c90621
JS
452 struct serial_state *state = dev_id;
453 struct async_struct *info = state->info;
1da177e4
LT
454
455#ifdef SERIAL_DEBUG_INTR
456 printk("ser_rx_int...");
457#endif
458
1da177e4
LT
459 if (!info || !info->tty)
460 return IRQ_NONE;
461
462 receive_chars(info);
1da177e4
LT
463#ifdef SERIAL_DEBUG_INTR
464 printk("end.\n");
465#endif
466 return IRQ_HANDLED;
467}
468
7d12e780 469static irqreturn_t ser_tx_int(int irq, void *dev_id)
1da177e4 470{
13c90621
JS
471 struct serial_state *state = dev_id;
472 struct async_struct *info = state->info;
1da177e4
LT
473
474 if (custom.serdatr & SDR_TBE) {
475#ifdef SERIAL_DEBUG_INTR
476 printk("ser_tx_int...");
477#endif
478
1da177e4
LT
479 if (!info || !info->tty)
480 return IRQ_NONE;
481
482 transmit_chars(info);
1da177e4
LT
483#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
1da177e4
LT
496/*
497 * ---------------------------------------------------------------
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
505static int startup(struct async_struct * info)
506{
507 unsigned long flags;
508 int retval=0;
509 unsigned long page;
510
511 page = get_zeroed_page(GFP_KERNEL);
512 if (!page)
513 return -ENOMEM;
514
515 local_irq_save(flags);
516
fef21073 517 if (info->state->flags & ASYNC_INITIALIZED) {
1da177e4
LT
518 free_page(page);
519 goto errout;
520 }
521
522 if (info->xmit.buf)
523 free_page(page);
524 else
525 info->xmit.buf = (unsigned char *) page;
526
527#ifdef SERIAL_DEBUG_OPEN
528 printk("starting up ttys%d ...", info->line);
529#endif
530
531 /* Clear anything in the input buffer */
532
533 custom.intreq = IF_RBF;
534 mb();
535
536 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
537 if (retval) {
538 if (serial_isroot()) {
539 if (info->tty)
540 set_bit(TTY_IO_ERROR,
541 &info->tty->flags);
542 retval = 0;
543 }
544 goto errout;
545 }
546
547 /* enable both Rx and Tx interrupts */
548 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
549 mb();
550 info->IER = UART_IER_MSI;
551
552 /* remember current state of the DCD and CTS bits */
553 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
554
1da177e4
LT
555 info->MCR = 0;
556 if (info->tty->termios->c_cflag & CBAUD)
557 info->MCR = SER_DTR | SER_RTS;
558 rtsdtr_ctrl(info->MCR);
559
560 if (info->tty)
561 clear_bit(TTY_IO_ERROR, &info->tty->flags);
562 info->xmit.head = info->xmit.tail = 0;
563
564 /*
565 * Set up the tty->alt_speed kludge
566 */
567 if (info->tty) {
fef21073 568 if ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1da177e4 569 info->tty->alt_speed = 57600;
fef21073 570 if ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1da177e4 571 info->tty->alt_speed = 115200;
fef21073 572 if ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1da177e4 573 info->tty->alt_speed = 230400;
fef21073 574 if ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1da177e4
LT
575 info->tty->alt_speed = 460800;
576 }
577
578 /*
579 * and set the speed of the serial port
580 */
581 change_speed(info, NULL);
582
fef21073 583 info->state->flags |= ASYNC_INITIALIZED;
1da177e4
LT
584 local_irq_restore(flags);
585 return 0;
586
587errout:
588 local_irq_restore(flags);
589 return retval;
590}
591
592/*
593 * This routine will shutdown a serial port; interrupts are disabled, and
594 * DTR is dropped if the hangup on close termio flag is on.
595 */
596static void shutdown(struct async_struct * info)
597{
598 unsigned long flags;
599 struct serial_state *state;
600
fef21073 601 if (!(info->state->flags & ASYNC_INITIALIZED))
1da177e4
LT
602 return;
603
604 state = info->state;
605
606#ifdef SERIAL_DEBUG_OPEN
607 printk("Shutting down serial port %d ....\n", info->line);
608#endif
609
610 local_irq_save(flags); /* Disable interrupts */
611
612 /*
613 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
614 * here so the queue might never be waken up
615 */
616 wake_up_interruptible(&info->delta_msr_wait);
617
1da177e4
LT
618 /*
619 * Free the IRQ, if necessary
620 */
621 free_irq(IRQ_AMIGA_VERTB, info);
622
623 if (info->xmit.buf) {
624 free_page((unsigned long) info->xmit.buf);
625 info->xmit.buf = NULL;
626 }
627
628 info->IER = 0;
629 custom.intena = IF_RBF | IF_TBE;
630 mb();
631
632 /* disable break condition */
633 custom.adkcon = AC_UARTBRK;
634 mb();
635
636 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
637 info->MCR &= ~(SER_DTR|SER_RTS);
638 rtsdtr_ctrl(info->MCR);
639
640 if (info->tty)
641 set_bit(TTY_IO_ERROR, &info->tty->flags);
642
fef21073 643 info->state->flags &= ~ASYNC_INITIALIZED;
1da177e4
LT
644 local_irq_restore(flags);
645}
646
647
648/*
649 * This routine is called to set the UART divisor registers to match
650 * the specified baud rate for a serial port.
651 */
652static void change_speed(struct async_struct *info,
606d099c 653 struct ktermios *old_termios)
1da177e4
LT
654{
655 int quot = 0, baud_base, baud;
656 unsigned cflag, cval = 0;
657 int bits;
658 unsigned long flags;
659
660 if (!info->tty || !info->tty->termios)
661 return;
662 cflag = info->tty->termios->c_cflag;
663
664 /* Byte size is always 8 bits plus parity bit if requested */
665
666 cval = 3; bits = 10;
667 if (cflag & CSTOPB) {
668 cval |= 0x04;
669 bits++;
670 }
671 if (cflag & PARENB) {
672 cval |= UART_LCR_PARITY;
673 bits++;
674 }
675 if (!(cflag & PARODD))
676 cval |= UART_LCR_EPAR;
677#ifdef CMSPAR
678 if (cflag & CMSPAR)
679 cval |= UART_LCR_SPAR;
680#endif
681
682 /* Determine divisor based on baud rate */
683 baud = tty_get_baud_rate(info->tty);
684 if (!baud)
685 baud = 9600; /* B0 transition handled in rs_set_termios */
686 baud_base = info->state->baud_base;
687 if (baud == 38400 &&
fef21073 688 ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1da177e4
LT
689 quot = info->state->custom_divisor;
690 else {
691 if (baud == 134)
692 /* Special case since 134 is really 134.5 */
693 quot = (2*baud_base / 269);
694 else if (baud)
695 quot = baud_base / baud;
696 }
697 /* If the quotient is zero refuse the change */
698 if (!quot && old_termios) {
db0ef08e 699 /* FIXME: Will need updating for new tty in the end */
1da177e4
LT
700 info->tty->termios->c_cflag &= ~CBAUD;
701 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
702 baud = tty_get_baud_rate(info->tty);
703 if (!baud)
704 baud = 9600;
705 if (baud == 38400 &&
fef21073 706 ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1da177e4
LT
707 quot = info->state->custom_divisor;
708 else {
709 if (baud == 134)
710 /* Special case since 134 is really 134.5 */
711 quot = (2*baud_base / 269);
712 else if (baud)
713 quot = baud_base / baud;
714 }
715 }
716 /* As a last resort, if the quotient is zero, default to 9600 bps */
717 if (!quot)
718 quot = baud_base / 9600;
719 info->quot = quot;
d8522563 720 info->timeout = ((info->state->xmit_fifo_size*HZ*bits*quot) / baud_base);
1da177e4
LT
721 info->timeout += HZ/50; /* Add .02 seconds of slop */
722
723 /* CTS flow control flag and modem status interrupts */
724 info->IER &= ~UART_IER_MSI;
fef21073 725 if (info->state->flags & ASYNC_HARDPPS_CD)
1da177e4
LT
726 info->IER |= UART_IER_MSI;
727 if (cflag & CRTSCTS) {
fef21073 728 info->state->flags |= ASYNC_CTS_FLOW;
1da177e4
LT
729 info->IER |= UART_IER_MSI;
730 } else
fef21073 731 info->state->flags &= ~ASYNC_CTS_FLOW;
1da177e4 732 if (cflag & CLOCAL)
fef21073 733 info->state->flags &= ~ASYNC_CHECK_CD;
1da177e4 734 else {
fef21073 735 info->state->flags |= ASYNC_CHECK_CD;
1da177e4
LT
736 info->IER |= UART_IER_MSI;
737 }
738 /* TBD:
4b512d26 739 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
1da177e4
LT
740 */
741
742 /*
743 * Set up parity check flag
744 */
1da177e4
LT
745
746 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
747 if (I_INPCK(info->tty))
748 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
749 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
750 info->read_status_mask |= UART_LSR_BI;
751
752 /*
753 * Characters to ignore
754 */
755 info->ignore_status_mask = 0;
756 if (I_IGNPAR(info->tty))
757 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
758 if (I_IGNBRK(info->tty)) {
759 info->ignore_status_mask |= UART_LSR_BI;
760 /*
761 * If we're ignore parity and break indicators, ignore
762 * overruns too. (For real raw support).
763 */
764 if (I_IGNPAR(info->tty))
765 info->ignore_status_mask |= UART_LSR_OE;
766 }
767 /*
768 * !!! ignore all characters if CREAD is not set
769 */
770 if ((cflag & CREAD) == 0)
771 info->ignore_status_mask |= UART_LSR_DR;
772 local_irq_save(flags);
773
774 {
775 short serper;
776
777 /* Set up the baud rate */
778 serper = quot - 1;
779
780 /* Enable or disable parity bit */
781
782 if(cval & UART_LCR_PARITY)
783 serper |= (SERPER_PARENB);
784
785 custom.serper = serper;
786 mb();
787 }
788
1da177e4
LT
789 local_irq_restore(flags);
790}
791
257afa3c 792static int rs_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4 793{
d9ad7ef1 794 struct async_struct *info;
1da177e4
LT
795 unsigned long flags;
796
d9ad7ef1
TJRMI
797 info = tty->driver_data;
798
1da177e4 799 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
257afa3c 800 return 0;
1da177e4 801
d9ad7ef1 802 if (!info->xmit.buf)
257afa3c 803 return 0;
1da177e4
LT
804
805 local_irq_save(flags);
806 if (CIRC_SPACE(info->xmit.head,
807 info->xmit.tail,
808 SERIAL_XMIT_SIZE) == 0) {
809 local_irq_restore(flags);
257afa3c 810 return 0;
1da177e4
LT
811 }
812
813 info->xmit.buf[info->xmit.head++] = ch;
814 info->xmit.head &= SERIAL_XMIT_SIZE-1;
815 local_irq_restore(flags);
257afa3c 816 return 1;
1da177e4
LT
817}
818
819static void rs_flush_chars(struct tty_struct *tty)
820{
c9f19e96 821 struct async_struct *info = tty->driver_data;
1da177e4
LT
822 unsigned long flags;
823
824 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
825 return;
826
827 if (info->xmit.head == info->xmit.tail
828 || tty->stopped
829 || tty->hw_stopped
830 || !info->xmit.buf)
831 return;
832
833 local_irq_save(flags);
834 info->IER |= UART_IER_THRI;
835 custom.intena = IF_SETCLR | IF_TBE;
836 mb();
837 /* set a pending Tx Interrupt, transmitter should restart now */
838 custom.intreq = IF_SETCLR | IF_TBE;
839 mb();
840 local_irq_restore(flags);
841}
842
843static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
844{
845 int c, ret = 0;
d9ad7ef1 846 struct async_struct *info;
1da177e4
LT
847 unsigned long flags;
848
d9ad7ef1
TJRMI
849 info = tty->driver_data;
850
1da177e4
LT
851 if (serial_paranoia_check(info, tty->name, "rs_write"))
852 return 0;
853
b3218a79 854 if (!info->xmit.buf)
1da177e4
LT
855 return 0;
856
3abf3bed 857 local_irq_save(flags);
1da177e4
LT
858 while (1) {
859 c = CIRC_SPACE_TO_END(info->xmit.head,
860 info->xmit.tail,
861 SERIAL_XMIT_SIZE);
862 if (count < c)
863 c = count;
864 if (c <= 0) {
865 break;
866 }
867 memcpy(info->xmit.buf + info->xmit.head, buf, c);
868 info->xmit.head = ((info->xmit.head + c) &
869 (SERIAL_XMIT_SIZE-1));
870 buf += c;
871 count -= c;
872 ret += c;
873 }
874 local_irq_restore(flags);
875
876 if (info->xmit.head != info->xmit.tail
877 && !tty->stopped
878 && !tty->hw_stopped
879 && !(info->IER & UART_IER_THRI)) {
880 info->IER |= UART_IER_THRI;
881 local_irq_disable();
882 custom.intena = IF_SETCLR | IF_TBE;
883 mb();
884 /* set a pending Tx Interrupt, transmitter should restart now */
885 custom.intreq = IF_SETCLR | IF_TBE;
886 mb();
887 local_irq_restore(flags);
888 }
889 return ret;
890}
891
892static int rs_write_room(struct tty_struct *tty)
893{
c9f19e96 894 struct async_struct *info = tty->driver_data;
1da177e4
LT
895
896 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
897 return 0;
898 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
899}
900
901static int rs_chars_in_buffer(struct tty_struct *tty)
902{
c9f19e96 903 struct async_struct *info = tty->driver_data;
1da177e4
LT
904
905 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
906 return 0;
907 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
908}
909
910static void rs_flush_buffer(struct tty_struct *tty)
911{
c9f19e96 912 struct async_struct *info = tty->driver_data;
1da177e4
LT
913 unsigned long flags;
914
915 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
916 return;
917 local_irq_save(flags);
918 info->xmit.head = info->xmit.tail = 0;
919 local_irq_restore(flags);
1da177e4
LT
920 tty_wakeup(tty);
921}
922
923/*
924 * This function is used to send a high-priority XON/XOFF character to
925 * the device
926 */
927static void rs_send_xchar(struct tty_struct *tty, char ch)
928{
c9f19e96 929 struct async_struct *info = tty->driver_data;
1da177e4
LT
930 unsigned long flags;
931
932 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
933 return;
934
935 info->x_char = ch;
936 if (ch) {
937 /* Make sure transmit interrupts are on */
938
939 /* Check this ! */
940 local_irq_save(flags);
941 if(!(custom.intenar & IF_TBE)) {
942 custom.intena = IF_SETCLR | IF_TBE;
943 mb();
944 /* set a pending Tx Interrupt, transmitter should restart now */
945 custom.intreq = IF_SETCLR | IF_TBE;
946 mb();
947 }
948 local_irq_restore(flags);
949
950 info->IER |= UART_IER_THRI;
951 }
952}
953
954/*
955 * ------------------------------------------------------------
956 * rs_throttle()
957 *
958 * This routine is called by the upper-layer tty layer to signal that
959 * incoming characters should be throttled.
960 * ------------------------------------------------------------
961 */
962static void rs_throttle(struct tty_struct * tty)
963{
c9f19e96 964 struct async_struct *info = tty->driver_data;
1da177e4
LT
965 unsigned long flags;
966#ifdef SERIAL_DEBUG_THROTTLE
967 char buf[64];
968
969 printk("throttle %s: %d....\n", tty_name(tty, buf),
970 tty->ldisc.chars_in_buffer(tty));
971#endif
972
973 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
974 return;
975
976 if (I_IXOFF(tty))
977 rs_send_xchar(tty, STOP_CHAR(tty));
978
979 if (tty->termios->c_cflag & CRTSCTS)
980 info->MCR &= ~SER_RTS;
981
982 local_irq_save(flags);
983 rtsdtr_ctrl(info->MCR);
984 local_irq_restore(flags);
985}
986
987static void rs_unthrottle(struct tty_struct * tty)
988{
c9f19e96 989 struct async_struct *info = tty->driver_data;
1da177e4
LT
990 unsigned long flags;
991#ifdef SERIAL_DEBUG_THROTTLE
992 char buf[64];
993
994 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
995 tty->ldisc.chars_in_buffer(tty));
996#endif
997
998 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
999 return;
1000
1001 if (I_IXOFF(tty)) {
1002 if (info->x_char)
1003 info->x_char = 0;
1004 else
1005 rs_send_xchar(tty, START_CHAR(tty));
1006 }
1007 if (tty->termios->c_cflag & CRTSCTS)
1008 info->MCR |= SER_RTS;
1009 local_irq_save(flags);
1010 rtsdtr_ctrl(info->MCR);
1011 local_irq_restore(flags);
1012}
1013
1014/*
1015 * ------------------------------------------------------------
1016 * rs_ioctl() and friends
1017 * ------------------------------------------------------------
1018 */
1019
1020static int get_serial_info(struct async_struct * info,
ab14caec 1021 struct serial_struct __user * retinfo)
1da177e4
LT
1022{
1023 struct serial_struct tmp;
1024 struct serial_state *state = info->state;
1025
1026 if (!retinfo)
1027 return -EFAULT;
1028 memset(&tmp, 0, sizeof(tmp));
ec79d605 1029 tty_lock();
1da177e4
LT
1030 tmp.type = state->type;
1031 tmp.line = state->line;
1032 tmp.port = state->port;
1033 tmp.irq = state->irq;
1034 tmp.flags = state->flags;
1035 tmp.xmit_fifo_size = state->xmit_fifo_size;
1036 tmp.baud_base = state->baud_base;
1037 tmp.close_delay = state->close_delay;
1038 tmp.closing_wait = state->closing_wait;
1039 tmp.custom_divisor = state->custom_divisor;
ec79d605 1040 tty_unlock();
1da177e4
LT
1041 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1042 return -EFAULT;
1043 return 0;
1044}
1045
1046static int set_serial_info(struct async_struct * info,
ab14caec 1047 struct serial_struct __user * new_info)
1da177e4
LT
1048{
1049 struct serial_struct new_serial;
1050 struct serial_state old_state, *state;
1051 unsigned int change_irq,change_port;
1052 int retval = 0;
1053
1054 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1055 return -EFAULT;
e18ce49b 1056
ec79d605 1057 tty_lock();
1da177e4
LT
1058 state = info->state;
1059 old_state = *state;
1060
1061 change_irq = new_serial.irq != state->irq;
1062 change_port = (new_serial.port != state->port);
e18ce49b 1063 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
ec79d605 1064 tty_unlock();
1da177e4 1065 return -EINVAL;
e18ce49b 1066 }
1da177e4
LT
1067
1068 if (!serial_isroot()) {
1069 if ((new_serial.baud_base != state->baud_base) ||
1070 (new_serial.close_delay != state->close_delay) ||
1071 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1072 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1073 (state->flags & ~ASYNC_USR_MASK)))
1074 return -EPERM;
1075 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1076 (new_serial.flags & ASYNC_USR_MASK));
1da177e4
LT
1077 state->custom_divisor = new_serial.custom_divisor;
1078 goto check_and_exit;
1079 }
1080
e18ce49b 1081 if (new_serial.baud_base < 9600) {
ec79d605 1082 tty_unlock();
1da177e4 1083 return -EINVAL;
e18ce49b 1084 }
1da177e4
LT
1085
1086 /*
1087 * OK, past this point, all the error checking has been done.
1088 * At this point, we start making changes.....
1089 */
1090
1091 state->baud_base = new_serial.baud_base;
1092 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1093 (new_serial.flags & ASYNC_FLAGS));
1da177e4
LT
1094 state->custom_divisor = new_serial.custom_divisor;
1095 state->close_delay = new_serial.close_delay * HZ/100;
1096 state->closing_wait = new_serial.closing_wait * HZ/100;
fef21073 1097 info->tty->low_latency = (state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1da177e4
LT
1098
1099check_and_exit:
fef21073 1100 if (state->flags & ASYNC_INITIALIZED) {
1da177e4
LT
1101 if (((old_state.flags & ASYNC_SPD_MASK) !=
1102 (state->flags & ASYNC_SPD_MASK)) ||
1103 (old_state.custom_divisor != state->custom_divisor)) {
1104 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1105 info->tty->alt_speed = 57600;
1106 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1107 info->tty->alt_speed = 115200;
1108 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1109 info->tty->alt_speed = 230400;
1110 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1111 info->tty->alt_speed = 460800;
1112 change_speed(info, NULL);
1113 }
1114 } else
1115 retval = startup(info);
ec79d605 1116 tty_unlock();
1da177e4
LT
1117 return retval;
1118}
1119
1120
1121/*
1122 * get_lsr_info - get line status register info
1123 *
1124 * Purpose: Let user call ioctl() to get info when the UART physically
1125 * is emptied. On bus types like RS485, the transmitter must
1126 * release the bus after transmitting. This must be done when
1127 * the transmit shift register is empty, not be done when the
1128 * transmit holding register is empty. This functionality
1129 * allows an RS485 driver to be written in user space.
1130 */
ab14caec 1131static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
1da177e4
LT
1132{
1133 unsigned char status;
1134 unsigned int result;
1135 unsigned long flags;
1136
1137 local_irq_save(flags);
1138 status = custom.serdatr;
1139 mb();
1140 local_irq_restore(flags);
1141 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1142 if (copy_to_user(value, &result, sizeof(int)))
1143 return -EFAULT;
1144 return 0;
1145}
1146
1147
60b33c13 1148static int rs_tiocmget(struct tty_struct *tty)
1da177e4 1149{
c9f19e96 1150 struct async_struct * info = tty->driver_data;
1da177e4
LT
1151 unsigned char control, status;
1152 unsigned long flags;
1153
1154 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1155 return -ENODEV;
1156 if (tty->flags & (1 << TTY_IO_ERROR))
1157 return -EIO;
1158
1159 control = info->MCR;
1160 local_irq_save(flags);
1161 status = ciab.pra;
1162 local_irq_restore(flags);
1163 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1164 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1165 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1166 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1167 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1168}
1169
20b9d177
AC
1170static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1171 unsigned int clear)
1da177e4 1172{
c9f19e96 1173 struct async_struct * info = tty->driver_data;
1da177e4
LT
1174 unsigned long flags;
1175
1176 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1177 return -ENODEV;
1178 if (tty->flags & (1 << TTY_IO_ERROR))
1179 return -EIO;
1180
1181 local_irq_save(flags);
1182 if (set & TIOCM_RTS)
1183 info->MCR |= SER_RTS;
1184 if (set & TIOCM_DTR)
1185 info->MCR |= SER_DTR;
1186 if (clear & TIOCM_RTS)
1187 info->MCR &= ~SER_RTS;
1188 if (clear & TIOCM_DTR)
1189 info->MCR &= ~SER_DTR;
1190 rtsdtr_ctrl(info->MCR);
1191 local_irq_restore(flags);
1192 return 0;
1193}
1194
1195/*
1196 * rs_break() --- routine which turns the break handling on or off
1197 */
9e98966c 1198static int rs_break(struct tty_struct *tty, int break_state)
1da177e4 1199{
c9f19e96 1200 struct async_struct * info = tty->driver_data;
1da177e4
LT
1201 unsigned long flags;
1202
1203 if (serial_paranoia_check(info, tty->name, "rs_break"))
970a8a51 1204 return -EINVAL;
1da177e4
LT
1205
1206 local_irq_save(flags);
1207 if (break_state == -1)
1208 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1209 else
1210 custom.adkcon = AC_UARTBRK;
1211 mb();
1212 local_irq_restore(flags);
9e98966c 1213 return 0;
1da177e4
LT
1214}
1215
0587102c
AC
1216/*
1217 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1218 * Return: write counters to the user passed counter struct
1219 * NB: both 1->0 and 0->1 transitions are counted except for
1220 * RI where only 0->1 is counted.
1221 */
1222static int rs_get_icount(struct tty_struct *tty,
1223 struct serial_icounter_struct *icount)
1224{
1225 struct async_struct *info = tty->driver_data;
1226 struct async_icount cnow;
1227 unsigned long flags;
1228
1229 local_irq_save(flags);
1230 cnow = info->state->icount;
1231 local_irq_restore(flags);
1232 icount->cts = cnow.cts;
1233 icount->dsr = cnow.dsr;
1234 icount->rng = cnow.rng;
1235 icount->dcd = cnow.dcd;
1236 icount->rx = cnow.rx;
1237 icount->tx = cnow.tx;
1238 icount->frame = cnow.frame;
1239 icount->overrun = cnow.overrun;
1240 icount->parity = cnow.parity;
1241 icount->brk = cnow.brk;
1242 icount->buf_overrun = cnow.buf_overrun;
1243
1244 return 0;
1245}
1da177e4 1246
6caa76b7 1247static int rs_ioctl(struct tty_struct *tty,
1da177e4
LT
1248 unsigned int cmd, unsigned long arg)
1249{
c9f19e96 1250 struct async_struct * info = tty->driver_data;
1da177e4 1251 struct async_icount cprev, cnow; /* kernel counter temps */
ab14caec 1252 void __user *argp = (void __user *)arg;
1da177e4
LT
1253 unsigned long flags;
1254
1255 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1256 return -ENODEV;
1257
1258 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1259 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1260 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1261 if (tty->flags & (1 << TTY_IO_ERROR))
1262 return -EIO;
1263 }
1264
1265 switch (cmd) {
1266 case TIOCGSERIAL:
ab14caec 1267 return get_serial_info(info, argp);
1da177e4 1268 case TIOCSSERIAL:
ab14caec 1269 return set_serial_info(info, argp);
1da177e4
LT
1270 case TIOCSERCONFIG:
1271 return 0;
1272
1273 case TIOCSERGETLSR: /* Get line status register */
ab14caec 1274 return get_lsr_info(info, argp);
1da177e4
LT
1275
1276 case TIOCSERGSTRUCT:
ab14caec 1277 if (copy_to_user(argp,
1da177e4
LT
1278 info, sizeof(struct async_struct)))
1279 return -EFAULT;
1280 return 0;
1281
1282 /*
1283 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1284 * - mask passed in arg for lines of interest
1285 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1286 * Caller should use TIOCGICOUNT to see which one it was
1287 */
1288 case TIOCMIWAIT:
1289 local_irq_save(flags);
1290 /* note the counters on entry */
1291 cprev = info->state->icount;
1292 local_irq_restore(flags);
1293 while (1) {
1294 interruptible_sleep_on(&info->delta_msr_wait);
1295 /* see if a signal did it */
1296 if (signal_pending(current))
1297 return -ERESTARTSYS;
1298 local_irq_save(flags);
1299 cnow = info->state->icount; /* atomic copy */
1300 local_irq_restore(flags);
1301 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1302 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1303 return -EIO; /* no change => error */
1304 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1305 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1306 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1307 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1308 return 0;
1309 }
1310 cprev = cnow;
1311 }
1312 /* NOTREACHED */
1313
1da177e4
LT
1314 case TIOCSERGWILD:
1315 case TIOCSERSWILD:
1316 /* "setserial -W" is called in Debian boot */
1317 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1318 return 0;
1319
1320 default:
1321 return -ENOIOCTLCMD;
1322 }
1323 return 0;
1324}
1325
606d099c 1326static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1da177e4 1327{
c9f19e96 1328 struct async_struct *info = tty->driver_data;
1da177e4
LT
1329 unsigned long flags;
1330 unsigned int cflag = tty->termios->c_cflag;
1331
1da177e4
LT
1332 change_speed(info, old_termios);
1333
1334 /* Handle transition to B0 status */
1335 if ((old_termios->c_cflag & CBAUD) &&
1336 !(cflag & CBAUD)) {
1337 info->MCR &= ~(SER_DTR|SER_RTS);
1338 local_irq_save(flags);
1339 rtsdtr_ctrl(info->MCR);
1340 local_irq_restore(flags);
1341 }
1342
1343 /* Handle transition away from B0 status */
1344 if (!(old_termios->c_cflag & CBAUD) &&
1345 (cflag & CBAUD)) {
1346 info->MCR |= SER_DTR;
1347 if (!(tty->termios->c_cflag & CRTSCTS) ||
1348 !test_bit(TTY_THROTTLED, &tty->flags)) {
1349 info->MCR |= SER_RTS;
1350 }
1351 local_irq_save(flags);
1352 rtsdtr_ctrl(info->MCR);
1353 local_irq_restore(flags);
1354 }
1355
1356 /* Handle turning off CRTSCTS */
1357 if ((old_termios->c_cflag & CRTSCTS) &&
1358 !(tty->termios->c_cflag & CRTSCTS)) {
1359 tty->hw_stopped = 0;
1360 rs_start(tty);
1361 }
1362
1363#if 0
1364 /*
1365 * No need to wake up processes in open wait, since they
1366 * sample the CLOCAL flag once, and don't recheck it.
1367 * XXX It's not clear whether the current behavior is correct
1368 * or not. Hence, this may change.....
1369 */
1370 if (!(old_termios->c_cflag & CLOCAL) &&
1371 (tty->termios->c_cflag & CLOCAL))
1372 wake_up_interruptible(&info->open_wait);
1373#endif
1374}
1375
1376/*
1377 * ------------------------------------------------------------
1378 * rs_close()
1379 *
1380 * This routine is called when the serial port gets closed. First, we
1381 * wait for the last remaining data to be sent. Then, we unlink its
1382 * async structure from the interrupt chain if necessary, and we free
1383 * that IRQ if nothing is left in the chain.
1384 * ------------------------------------------------------------
1385 */
1386static void rs_close(struct tty_struct *tty, struct file * filp)
1387{
c9f19e96 1388 struct async_struct * info = tty->driver_data;
1da177e4
LT
1389 struct serial_state *state;
1390 unsigned long flags;
1391
1392 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1393 return;
1394
1395 state = info->state;
1396
1397 local_irq_save(flags);
1398
1399 if (tty_hung_up_p(filp)) {
1400 DBG_CNT("before DEC-hung");
1401 local_irq_restore(flags);
1402 return;
1403 }
1404
1405#ifdef SERIAL_DEBUG_OPEN
1406 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1407#endif
1408 if ((tty->count == 1) && (state->count != 1)) {
1409 /*
1410 * Uh, oh. tty->count is 1, which means that the tty
1411 * structure will be freed. state->count should always
1412 * be one in these conditions. If it's greater than
1413 * one, we've got real problems, since it means the
1414 * serial port won't be shutdown.
1415 */
1416 printk("rs_close: bad serial port count; tty->count is 1, "
1417 "state->count is %d\n", state->count);
1418 state->count = 1;
1419 }
1420 if (--state->count < 0) {
1421 printk("rs_close: bad serial port count for ttys%d: %d\n",
d8522563 1422 state->line, state->count);
1da177e4
LT
1423 state->count = 0;
1424 }
1425 if (state->count) {
1426 DBG_CNT("before DEC-2");
1427 local_irq_restore(flags);
1428 return;
1429 }
fef21073 1430 state->flags |= ASYNC_CLOSING;
1da177e4
LT
1431 /*
1432 * Now we wait for the transmit buffer to clear; and we notify
1433 * the line discipline to only process XON/XOFF characters.
1434 */
1435 tty->closing = 1;
d8522563
JS
1436 if (state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1437 tty_wait_until_sent(tty, state->closing_wait);
1da177e4
LT
1438 /*
1439 * At this point we stop accepting input. To do this, we
1440 * disable the receive line status interrupts, and tell the
1441 * interrupt driver to stop checking the data ready bit in the
1442 * line status register.
1443 */
1444 info->read_status_mask &= ~UART_LSR_DR;
fef21073 1445 if (state->flags & ASYNC_INITIALIZED) {
1da177e4
LT
1446 /* disable receive interrupts */
1447 custom.intena = IF_RBF;
1448 mb();
1449 /* clear any pending receive interrupt */
1450 custom.intreq = IF_RBF;
1451 mb();
1452
1453 /*
1454 * Before we drop DTR, make sure the UART transmitter
1455 * has completely drained; this is especially
1456 * important if there is a transmit FIFO!
1457 */
1458 rs_wait_until_sent(tty, info->timeout);
1459 }
1460 shutdown(info);
978e595f 1461 rs_flush_buffer(tty);
1da177e4
LT
1462
1463 tty_ldisc_flush(tty);
1464 tty->closing = 0;
1da177e4
LT
1465 info->tty = NULL;
1466 if (info->blocked_open) {
d8522563
JS
1467 if (state->close_delay) {
1468 msleep_interruptible(jiffies_to_msecs(state->close_delay));
1da177e4
LT
1469 }
1470 wake_up_interruptible(&info->open_wait);
1471 }
fef21073 1472 state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1da177e4
LT
1473 wake_up_interruptible(&info->close_wait);
1474 local_irq_restore(flags);
1475}
1476
1477/*
1478 * rs_wait_until_sent() --- wait until the transmitter is empty
1479 */
1480static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1481{
c9f19e96 1482 struct async_struct * info = tty->driver_data;
1da177e4
LT
1483 unsigned long orig_jiffies, char_time;
1484 int lsr;
1485
1486 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1487 return;
1488
d8522563 1489 if (info->state->xmit_fifo_size == 0)
1da177e4
LT
1490 return; /* Just in case.... */
1491
1492 orig_jiffies = jiffies;
978e595f 1493
1da177e4
LT
1494 /*
1495 * Set the check interval to be 1/5 of the estimated time to
1496 * send a single character, and make it at least 1. The check
1497 * interval should also be less than the timeout.
1498 *
1499 * Note: we have to use pretty tight timings here to satisfy
1500 * the NIST-PCTS.
1501 */
d8522563 1502 char_time = (info->timeout - HZ/50) / info->state->xmit_fifo_size;
1da177e4
LT
1503 char_time = char_time / 5;
1504 if (char_time == 0)
1505 char_time = 1;
1506 if (timeout)
1507 char_time = min_t(unsigned long, char_time, timeout);
1508 /*
1509 * If the transmitter hasn't cleared in twice the approximate
1510 * amount of time to send the entire FIFO, it probably won't
1511 * ever clear. This assumes the UART isn't doing flow
1512 * control, which is currently the case. Hence, if it ever
1513 * takes longer than info->timeout, this is probably due to a
1514 * UART bug of some kind. So, we clamp the timeout parameter at
1515 * 2*info->timeout.
1516 */
1517 if (!timeout || timeout > 2*info->timeout)
1518 timeout = 2*info->timeout;
1519#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1520 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1521 printk("jiff=%lu...", jiffies);
1522#endif
1523 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1524#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1525 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1526#endif
1527 msleep_interruptible(jiffies_to_msecs(char_time));
1528 if (signal_pending(current))
1529 break;
1530 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1531 break;
1532 }
cc0a8fbb 1533 __set_current_state(TASK_RUNNING);
eff4b0b9 1534
1da177e4
LT
1535#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1536 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1537#endif
1538}
1539
1540/*
1541 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1542 */
1543static void rs_hangup(struct tty_struct *tty)
1544{
c9f19e96 1545 struct async_struct * info = tty->driver_data;
1da177e4
LT
1546 struct serial_state *state = info->state;
1547
1548 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1549 return;
1550
1551 state = info->state;
1552
1553 rs_flush_buffer(tty);
1554 shutdown(info);
1da177e4 1555 state->count = 0;
fef21073 1556 state->flags &= ~ASYNC_NORMAL_ACTIVE;
1da177e4
LT
1557 info->tty = NULL;
1558 wake_up_interruptible(&info->open_wait);
1559}
1560
1561/*
1562 * ------------------------------------------------------------
1563 * rs_open() and friends
1564 * ------------------------------------------------------------
1565 */
1566static int block_til_ready(struct tty_struct *tty, struct file * filp,
1567 struct async_struct *info)
1568{
1569#ifdef DECLARE_WAITQUEUE
1570 DECLARE_WAITQUEUE(wait, current);
1571#else
1572 struct wait_queue wait = { current, NULL };
1573#endif
1574 struct serial_state *state = info->state;
1575 int retval;
1576 int do_clocal = 0, extra_count = 0;
1577 unsigned long flags;
1578
1579 /*
1580 * If the device is in the middle of being closed, then block
1581 * until it's done, and then try again.
1582 */
1583 if (tty_hung_up_p(filp) ||
fef21073
JS
1584 (state->flags & ASYNC_CLOSING)) {
1585 if (state->flags & ASYNC_CLOSING)
1da177e4
LT
1586 interruptible_sleep_on(&info->close_wait);
1587#ifdef SERIAL_DO_RESTART
fef21073 1588 return ((state->flags & ASYNC_HUP_NOTIFY) ?
1da177e4
LT
1589 -EAGAIN : -ERESTARTSYS);
1590#else
1591 return -EAGAIN;
1592#endif
1593 }
1594
1595 /*
1596 * If non-blocking mode is set, or the port is not enabled,
1597 * then make the check up front and then exit.
1598 */
1599 if ((filp->f_flags & O_NONBLOCK) ||
1600 (tty->flags & (1 << TTY_IO_ERROR))) {
fef21073 1601 state->flags |= ASYNC_NORMAL_ACTIVE;
1da177e4
LT
1602 return 0;
1603 }
1604
1605 if (tty->termios->c_cflag & CLOCAL)
1606 do_clocal = 1;
1607
1608 /*
1609 * Block waiting for the carrier detect and the line to become
1610 * free (i.e., not in use by the callout). While we are in
1611 * this loop, state->count is dropped by one, so that
1612 * rs_close() knows when to free things. We restore it upon
1613 * exit, either normal or abnormal.
1614 */
1615 retval = 0;
1616 add_wait_queue(&info->open_wait, &wait);
1617#ifdef SERIAL_DEBUG_OPEN
1618 printk("block_til_ready before block: ttys%d, count = %d\n",
1619 state->line, state->count);
1620#endif
1621 local_irq_save(flags);
1622 if (!tty_hung_up_p(filp)) {
1623 extra_count = 1;
1624 state->count--;
1625 }
1626 local_irq_restore(flags);
1627 info->blocked_open++;
1628 while (1) {
1629 local_irq_save(flags);
1630 if (tty->termios->c_cflag & CBAUD)
1631 rtsdtr_ctrl(SER_DTR|SER_RTS);
1632 local_irq_restore(flags);
1633 set_current_state(TASK_INTERRUPTIBLE);
1634 if (tty_hung_up_p(filp) ||
fef21073 1635 !(state->flags & ASYNC_INITIALIZED)) {
1da177e4 1636#ifdef SERIAL_DO_RESTART
fef21073 1637 if (state->flags & ASYNC_HUP_NOTIFY)
1da177e4
LT
1638 retval = -EAGAIN;
1639 else
1640 retval = -ERESTARTSYS;
1641#else
1642 retval = -EAGAIN;
1643#endif
1644 break;
1645 }
fef21073 1646 if (!(state->flags & ASYNC_CLOSING) &&
1da177e4
LT
1647 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1648 break;
1649 if (signal_pending(current)) {
1650 retval = -ERESTARTSYS;
1651 break;
1652 }
1653#ifdef SERIAL_DEBUG_OPEN
1654 printk("block_til_ready blocking: ttys%d, count = %d\n",
1655 info->line, state->count);
1656#endif
e142a31d 1657 tty_unlock();
1da177e4 1658 schedule();
e142a31d 1659 tty_lock();
1da177e4 1660 }
cc0a8fbb 1661 __set_current_state(TASK_RUNNING);
1da177e4
LT
1662 remove_wait_queue(&info->open_wait, &wait);
1663 if (extra_count)
1664 state->count++;
1665 info->blocked_open--;
1666#ifdef SERIAL_DEBUG_OPEN
1667 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1668 info->line, state->count);
1669#endif
1670 if (retval)
1671 return retval;
fef21073 1672 state->flags |= ASYNC_NORMAL_ACTIVE;
1da177e4
LT
1673 return 0;
1674}
1675
1676static int get_async_struct(int line, struct async_struct **ret_info)
1677{
1678 struct async_struct *info;
1679 struct serial_state *sstate;
1680
1681 sstate = rs_table + line;
1682 sstate->count++;
1683 if (sstate->info) {
1684 *ret_info = sstate->info;
1685 return 0;
1686 }
dd00cc48 1687 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
1da177e4
LT
1688 if (!info) {
1689 sstate->count--;
1690 return -ENOMEM;
1691 }
1da177e4
LT
1692#ifdef DECLARE_WAITQUEUE
1693 init_waitqueue_head(&info->open_wait);
1694 init_waitqueue_head(&info->close_wait);
1695 init_waitqueue_head(&info->delta_msr_wait);
1696#endif
1da177e4
LT
1697 info->state = sstate;
1698 if (sstate->info) {
1699 kfree(info);
1700 *ret_info = sstate->info;
1701 return 0;
1702 }
1703 *ret_info = sstate->info = info;
1704 return 0;
1705}
1706
1707/*
1708 * This routine is called whenever a serial port is opened. It
1709 * enables interrupts for a serial port, linking in its async structure into
1710 * the IRQ chain. It also performs the serial-specific
1711 * initialization for the tty structure.
1712 */
1713static int rs_open(struct tty_struct *tty, struct file * filp)
1714{
1715 struct async_struct *info;
410235fd 1716 int retval;
1da177e4 1717
410235fd 1718 retval = get_async_struct(tty->index, &info);
1da177e4
LT
1719 if (retval) {
1720 return retval;
1721 }
1722 tty->driver_data = info;
1723 info->tty = tty;
1724 if (serial_paranoia_check(info, tty->name, "rs_open"))
1725 return -ENODEV;
1726
1727#ifdef SERIAL_DEBUG_OPEN
1728 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1729#endif
fef21073 1730 info->tty->low_latency = (info->state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1da177e4 1731
1da177e4
LT
1732 /*
1733 * If the port is the middle of closing, bail out now
1734 */
1735 if (tty_hung_up_p(filp) ||
fef21073
JS
1736 (info->state->flags & ASYNC_CLOSING)) {
1737 if (info->state->flags & ASYNC_CLOSING)
1da177e4
LT
1738 interruptible_sleep_on(&info->close_wait);
1739#ifdef SERIAL_DO_RESTART
fef21073 1740 return ((info->state->flags & ASYNC_HUP_NOTIFY) ?
1da177e4
LT
1741 -EAGAIN : -ERESTARTSYS);
1742#else
1743 return -EAGAIN;
1744#endif
1745 }
1746
1747 /*
1748 * Start up serial port
1749 */
1750 retval = startup(info);
1751 if (retval) {
1752 return retval;
1753 }
1754
1755 retval = block_til_ready(tty, filp, info);
1756 if (retval) {
1757#ifdef SERIAL_DEBUG_OPEN
1758 printk("rs_open returning after block_til_ready with %d\n",
1759 retval);
1760#endif
1761 return retval;
1762 }
1763
1764#ifdef SERIAL_DEBUG_OPEN
1765 printk("rs_open %s successful...", tty->name);
1766#endif
1767 return 0;
1768}
1769
1770/*
1771 * /proc fs routines....
1772 */
1773
d594027d 1774static inline void line_info(struct seq_file *m, struct serial_state *state)
1da177e4
LT
1775{
1776 struct async_struct *info = state->info, scr_info;
1777 char stat_buf[30], control, status;
1da177e4
LT
1778 unsigned long flags;
1779
d594027d 1780 seq_printf(m, "%d: uart:amiga_builtin",state->line);
1da177e4
LT
1781
1782 /*
1783 * Figure out the current RS-232 lines
1784 */
1785 if (!info) {
1786 info = &scr_info; /* This is just for serial_{in,out} */
1787
1da177e4
LT
1788 info->quot = 0;
1789 info->tty = NULL;
1790 }
1791 local_irq_save(flags);
1792 status = ciab.pra;
1793 control = info ? info->MCR : status;
1794 local_irq_restore(flags);
1795
1796 stat_buf[0] = 0;
1797 stat_buf[1] = 0;
1798 if(!(control & SER_RTS))
1799 strcat(stat_buf, "|RTS");
1800 if(!(status & SER_CTS))
1801 strcat(stat_buf, "|CTS");
1802 if(!(control & SER_DTR))
1803 strcat(stat_buf, "|DTR");
1804 if(!(status & SER_DSR))
1805 strcat(stat_buf, "|DSR");
1806 if(!(status & SER_DCD))
1807 strcat(stat_buf, "|CD");
1808
1809 if (info->quot) {
d594027d 1810 seq_printf(m, " baud:%d", state->baud_base / info->quot);
1da177e4
LT
1811 }
1812
d594027d 1813 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1da177e4
LT
1814
1815 if (state->icount.frame)
d594027d 1816 seq_printf(m, " fe:%d", state->icount.frame);
1da177e4
LT
1817
1818 if (state->icount.parity)
d594027d 1819 seq_printf(m, " pe:%d", state->icount.parity);
1da177e4
LT
1820
1821 if (state->icount.brk)
d594027d 1822 seq_printf(m, " brk:%d", state->icount.brk);
1da177e4
LT
1823
1824 if (state->icount.overrun)
d594027d 1825 seq_printf(m, " oe:%d", state->icount.overrun);
1da177e4
LT
1826
1827 /*
1828 * Last thing is the RS-232 status lines
1829 */
d594027d 1830 seq_printf(m, " %s\n", stat_buf+1);
1da177e4
LT
1831}
1832
d594027d 1833static int rs_proc_show(struct seq_file *m, void *v)
1da177e4 1834{
d594027d
AD
1835 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1836 line_info(m, &rs_table[0]);
1837 return 0;
1da177e4
LT
1838}
1839
d594027d
AD
1840static int rs_proc_open(struct inode *inode, struct file *file)
1841{
1842 return single_open(file, rs_proc_show, NULL);
1843}
1844
1845static const struct file_operations rs_proc_fops = {
1846 .owner = THIS_MODULE,
1847 .open = rs_proc_open,
1848 .read = seq_read,
1849 .llseek = seq_lseek,
1850 .release = single_release,
1851};
1852
1da177e4
LT
1853/*
1854 * ---------------------------------------------------------------------
1855 * rs_init() and friends
1856 *
1857 * rs_init() is called at boot-time to initialize the serial driver.
1858 * ---------------------------------------------------------------------
1859 */
1860
1861/*
1862 * This routine prints out the appropriate serial driver version
1863 * number, and identifies which options were configured into this
1864 * driver.
1865 */
41c28ff1 1866static void show_serial_version(void)
1da177e4
LT
1867{
1868 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1869}
1870
1871
b68e31d0 1872static const struct tty_operations serial_ops = {
1da177e4
LT
1873 .open = rs_open,
1874 .close = rs_close,
1875 .write = rs_write,
1876 .put_char = rs_put_char,
1877 .flush_chars = rs_flush_chars,
1878 .write_room = rs_write_room,
1879 .chars_in_buffer = rs_chars_in_buffer,
1880 .flush_buffer = rs_flush_buffer,
1881 .ioctl = rs_ioctl,
1882 .throttle = rs_throttle,
1883 .unthrottle = rs_unthrottle,
1884 .set_termios = rs_set_termios,
1885 .stop = rs_stop,
1886 .start = rs_start,
1887 .hangup = rs_hangup,
1888 .break_ctl = rs_break,
1889 .send_xchar = rs_send_xchar,
1890 .wait_until_sent = rs_wait_until_sent,
1da177e4
LT
1891 .tiocmget = rs_tiocmget,
1892 .tiocmset = rs_tiocmset,
0587102c 1893 .get_icount = rs_get_icount,
d594027d 1894 .proc_fops = &rs_proc_fops,
1da177e4
LT
1895};
1896
1897/*
1898 * The serial driver boot-time initialization code!
1899 */
826e8c8c 1900static int __init amiga_serial_probe(struct platform_device *pdev)
1da177e4
LT
1901{
1902 unsigned long flags;
1903 struct serial_state * state;
5edc304f 1904 int error;
1da177e4 1905
410235fd 1906 serial_driver = alloc_tty_driver(NR_PORTS);
1da177e4
LT
1907 if (!serial_driver)
1908 return -ENOMEM;
1909
1da177e4
LT
1910 show_serial_version();
1911
1912 /* Initialize the tty_driver structure */
1913
1da177e4
LT
1914 serial_driver->driver_name = "amiserial";
1915 serial_driver->name = "ttyS";
1916 serial_driver->major = TTY_MAJOR;
1917 serial_driver->minor_start = 64;
1918 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1919 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1920 serial_driver->init_termios = tty_std_termios;
1921 serial_driver->init_termios.c_cflag =
1922 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1923 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1924 tty_set_operations(serial_driver, &serial_ops);
1925
5edc304f
GU
1926 error = tty_register_driver(serial_driver);
1927 if (error)
826e8c8c 1928 goto fail_put_tty_driver;
1da177e4
LT
1929
1930 state = rs_table;
1da177e4
LT
1931 state->port = (int)&custom.serdatr; /* Just to give it a value */
1932 state->line = 0;
1933 state->custom_divisor = 0;
1934 state->close_delay = 5*HZ/10;
1935 state->closing_wait = 30*HZ;
1936 state->icount.cts = state->icount.dsr =
1937 state->icount.rng = state->icount.dcd = 0;
1938 state->icount.rx = state->icount.tx = 0;
1939 state->icount.frame = state->icount.parity = 0;
1940 state->icount.overrun = state->icount.brk = 0;
1da177e4
LT
1941
1942 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
1943 state->line);
1944
1945 /* Hardware set up */
1946
1947 state->baud_base = amiga_colorclock;
1948 state->xmit_fifo_size = 1;
1949
1da177e4 1950 /* set ISRs, and then disable the rx interrupts */
5edc304f
GU
1951 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1952 if (error)
1953 goto fail_unregister;
1954
9cfb5c05 1955 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
5edc304f
GU
1956 "serial RX", state);
1957 if (error)
1958 goto fail_free_irq;
1da177e4 1959
c70c036f
JL
1960 local_irq_save(flags);
1961
1da177e4
LT
1962 /* turn off Rx and Tx interrupts */
1963 custom.intena = IF_RBF | IF_TBE;
1964 mb();
1965
1966 /* clear any pending interrupt */
1967 custom.intreq = IF_RBF | IF_TBE;
1968 mb();
1969
1970 local_irq_restore(flags);
1971
1972 /*
1973 * set the appropriate directions for the modem control flags,
1974 * and clear RTS and DTR
1975 */
1976 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1977 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1978
826e8c8c
GU
1979 platform_set_drvdata(pdev, state);
1980
1da177e4 1981 return 0;
5edc304f
GU
1982
1983fail_free_irq:
1984 free_irq(IRQ_AMIGA_TBE, state);
1985fail_unregister:
1986 tty_unregister_driver(serial_driver);
5edc304f
GU
1987fail_put_tty_driver:
1988 put_tty_driver(serial_driver);
1989 return error;
1da177e4
LT
1990}
1991
826e8c8c 1992static int __exit amiga_serial_remove(struct platform_device *pdev)
1da177e4
LT
1993{
1994 int error;
826e8c8c
GU
1995 struct serial_state *state = platform_get_drvdata(pdev);
1996 struct async_struct *info = state->info;
1da177e4
LT
1997
1998 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
1da177e4
LT
1999 if ((error = tty_unregister_driver(serial_driver)))
2000 printk("SERIAL: failed to unregister serial driver (%d)\n",
2001 error);
2002 put_tty_driver(serial_driver);
2003
826e8c8c
GU
2004 rs_table[0].info = NULL;
2005 kfree(info);
1da177e4 2006
5edc304f
GU
2007 free_irq(IRQ_AMIGA_TBE, rs_table);
2008 free_irq(IRQ_AMIGA_RBF, rs_table);
2009
826e8c8c
GU
2010 platform_set_drvdata(pdev, NULL);
2011
2012 return error;
2013}
2014
2015static struct platform_driver amiga_serial_driver = {
2016 .remove = __exit_p(amiga_serial_remove),
2017 .driver = {
2018 .name = "amiga-serial",
2019 .owner = THIS_MODULE,
2020 },
2021};
2022
2023static int __init amiga_serial_init(void)
2024{
2025 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
2026}
2027
2028module_init(amiga_serial_init);
2029
2030static void __exit amiga_serial_exit(void)
2031{
2032 platform_driver_unregister(&amiga_serial_driver);
1da177e4
LT
2033}
2034
826e8c8c 2035module_exit(amiga_serial_exit);
1da177e4
LT
2036
2037
d1a35e4d
GU
2038#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
2039
1da177e4
LT
2040/*
2041 * ------------------------------------------------------------
2042 * Serial console driver
2043 * ------------------------------------------------------------
2044 */
1da177e4
LT
2045
2046static void amiga_serial_putc(char c)
2047{
2048 custom.serdat = (unsigned char)c | 0x100;
2049 while (!(custom.serdatr & 0x2000))
2050 barrier();
2051}
2052
2053/*
2054 * Print a string to the serial port trying not to disturb
2055 * any possible real use of the port...
2056 *
2057 * The console must be locked when we get here.
2058 */
2059static void serial_console_write(struct console *co, const char *s,
2060 unsigned count)
2061{
2062 unsigned short intena = custom.intenar;
2063
2064 custom.intena = IF_TBE;
2065
2066 while (count--) {
2067 if (*s == '\n')
2068 amiga_serial_putc('\r');
2069 amiga_serial_putc(*s++);
2070 }
2071
2072 custom.intena = IF_SETCLR | (intena & IF_TBE);
2073}
2074
2075static struct tty_driver *serial_console_device(struct console *c, int *index)
2076{
2077 *index = 0;
2078 return serial_driver;
2079}
2080
2081static struct console sercons = {
2082 .name = "ttyS",
2083 .write = serial_console_write,
2084 .device = serial_console_device,
2085 .flags = CON_PRINTBUFFER,
2086 .index = -1,
2087};
2088
2089/*
2090 * Register console.
2091 */
2092static int __init amiserial_console_init(void)
2093{
2094 register_console(&sercons);
2095 return 0;
2096}
2097console_initcall(amiserial_console_init);
d1a35e4d
GU
2098
2099#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
1da177e4
LT
2100
2101MODULE_LICENSE("GPL");
826e8c8c 2102MODULE_ALIAS("platform:amiga-serial");