TTY: amiserial/simserial, use tty_port
[deliverable/linux.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", \
916b7656 48 tty->name, (info->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
588993dd
JS
105static void change_speed(struct tty_struct *tty, struct serial_state *info,
106 struct ktermios *old);
1da177e4
LT
107static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
108
109
110static struct serial_state rs_table[1];
111
fe971071 112#define NR_PORTS ARRAY_SIZE(rs_table)
1da177e4 113
1da177e4
LT
114#include <asm/uaccess.h>
115
116#define serial_isroot() (capable(CAP_SYS_ADMIN))
117
118
916b7656 119static inline int serial_paranoia_check(struct serial_state *info,
1da177e4
LT
120 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{
916b7656 172 struct serial_state *info = tty->driver_data;
1da177e4
LT
173 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{
916b7656 192 struct serial_state *info = tty->driver_data;
1da177e4
LT
193 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
916b7656 233static void receive_chars(struct serial_state *info)
1da177e4
LT
234{
235 int status;
236 int serdatr;
87758791 237 struct tty_struct *tty = info->tport.tty;
33f0f88f 238 unsigned char ch, flag;
1da177e4 239 struct async_icount *icount;
33f0f88f 240 int oe = 0;
1da177e4 241
916b7656 242 icount = &info->icount;
1da177e4
LT
243
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;
1da177e4
LT
256 icount->rx++;
257
258#ifdef SERIAL_DEBUG_INTR
259 printk("DR%02x:%02x...", ch, status);
260#endif
33f0f88f 261 flag = TTY_NORMAL;
1da177e4
LT
262
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)
33f0f88f 290 goto out;
1da177e4
LT
291
292 status &= info->read_status_mask;
293
294 if (status & (UART_LSR_BI)) {
295#ifdef SERIAL_DEBUG_INTR
296 printk("handling break....");
297#endif
33f0f88f 298 flag = TTY_BREAK;
916b7656 299 if (info->flags & ASYNC_SAK)
1da177e4
LT
300 do_SAK(tty);
301 } else if (status & UART_LSR_PE)
33f0f88f 302 flag = TTY_PARITY;
1da177e4 303 else if (status & UART_LSR_FE)
33f0f88f 304 flag = TTY_FRAME;
1da177e4
LT
305 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 */
33f0f88f 311 oe = 1;
1da177e4
LT
312 }
313 }
33f0f88f
AC
314 tty_insert_flip_char(tty, ch, flag);
315 if (oe == 1)
316 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1da177e4 317 tty_flip_buffer_push(tty);
33f0f88f
AC
318out:
319 return;
1da177e4
LT
320}
321
916b7656 322static void transmit_chars(struct serial_state *info)
1da177e4
LT
323{
324 custom.intreq = IF_TBE;
325 mb();
326 if (info->x_char) {
327 custom.serdat = info->x_char | 0x100;
328 mb();
916b7656 329 info->icount.tx++;
1da177e4
LT
330 info->x_char = 0;
331 return;
332 }
333 if (info->xmit.head == info->xmit.tail
87758791
JS
334 || info->tport.tty->stopped
335 || info->tport.tty->hw_stopped) {
1da177e4
LT
336 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);
916b7656 345 info->icount.tx++;
1da177e4
LT
346
347 if (CIRC_CNT(info->xmit.head,
348 info->xmit.tail,
349 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
87758791 350 tty_wakeup(info->tport.tty);
1da177e4
LT
351
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
916b7656 362static void check_modem_status(struct serial_state *info)
1da177e4
LT
363{
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) {
916b7656 373 icount = &info->icount;
1da177e4
LT
374 /* 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
916b7656 380 if ((info->flags & ASYNC_HARDPPS_CD) &&
1da177e4
LT
381 !(status & SER_DCD))
382 hardpps();
383#endif
384 }
385 if (dstatus & SER_CTS)
386 icount->cts++;
87758791 387 wake_up_interruptible(&info->tport.delta_msr_wait);
1da177e4
LT
388 }
389
916b7656 390 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
1da177e4
LT
391#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))
87758791 396 wake_up_interruptible(&info->tport.open_wait);
1da177e4
LT
397 else {
398#ifdef SERIAL_DEBUG_OPEN
399 printk("doing serial hangup...");
400#endif
87758791
JS
401 if (info->tport.tty)
402 tty_hangup(info->tport.tty);
1da177e4
LT
403 }
404 }
916b7656 405 if (info->flags & ASYNC_CTS_FLOW) {
87758791 406 if (info->tport.tty->hw_stopped) {
1da177e4
LT
407 if (!(status & SER_CTS)) {
408#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
409 printk("CTS tx start...");
410#endif
87758791 411 info->tport.tty->hw_stopped = 0;
1da177e4
LT
412 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();
87758791 418 tty_wakeup(info->tport.tty);
1da177e4
LT
419 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
87758791 426 info->tport.tty->hw_stopped = 1;
1da177e4
LT
427 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
7d12e780 438static irqreturn_t ser_vbl_int( int irq, void *data)
1da177e4
LT
439{
440 /* vbl is just a periodic interrupt we tie into to update modem status */
916b7656 441 struct serial_state *info = data;
1da177e4
LT
442 /*
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
7d12e780 451static irqreturn_t ser_rx_int(int irq, void *dev_id)
1da177e4 452{
916b7656 453 struct serial_state *info = dev_id;
1da177e4
LT
454
455#ifdef SERIAL_DEBUG_INTR
456 printk("ser_rx_int...");
457#endif
458
87758791 459 if (!info->tport.tty)
1da177e4
LT
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{
916b7656 471 struct serial_state *info = dev_id;
1da177e4
LT
472
473 if (custom.serdatr & SDR_TBE) {
474#ifdef SERIAL_DEBUG_INTR
475 printk("ser_tx_int...");
476#endif
477
87758791 478 if (!info->tport.tty)
1da177e4
LT
479 return IRQ_NONE;
480
481 transmit_chars(info);
1da177e4
LT
482#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
1da177e4
LT
495/*
496 * ---------------------------------------------------------------
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
588993dd 504static int startup(struct tty_struct *tty, struct serial_state *info)
1da177e4
LT
505{
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
916b7656 516 if (info->flags & ASYNC_INITIALIZED) {
1da177e4
LT
517 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()) {
588993dd 538 set_bit(TTY_IO_ERROR, &tty->flags);
1da177e4
LT
539 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
1da177e4 552 info->MCR = 0;
588993dd 553 if (C_BAUD(tty))
1da177e4
LT
554 info->MCR = SER_DTR | SER_RTS;
555 rtsdtr_ctrl(info->MCR);
556
588993dd 557 clear_bit(TTY_IO_ERROR, &tty->flags);
1da177e4
LT
558 info->xmit.head = info->xmit.tail = 0;
559
560 /*
561 * Set up the tty->alt_speed kludge
562 */
588993dd
JS
563 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;
1da177e4
LT
571
572 /*
573 * and set the speed of the serial port
574 */
588993dd 575 change_speed(tty, info, NULL);
1da177e4 576
916b7656 577 info->flags |= ASYNC_INITIALIZED;
1da177e4
LT
578 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 */
588993dd 590static void shutdown(struct tty_struct *tty, struct serial_state *info)
1da177e4
LT
591{
592 unsigned long flags;
593 struct serial_state *state;
594
916b7656 595 if (!(info->flags & ASYNC_INITIALIZED))
1da177e4
LT
596 return;
597
916b7656 598 state = info;
1da177e4
LT
599
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 */
87758791 610 wake_up_interruptible(&info->tport.delta_msr_wait);
1da177e4 611
1da177e4
LT
612 /*
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
588993dd 630 if (tty->termios->c_cflag & HUPCL)
1da177e4
LT
631 info->MCR &= ~(SER_DTR|SER_RTS);
632 rtsdtr_ctrl(info->MCR);
633
588993dd 634 set_bit(TTY_IO_ERROR, &tty->flags);
1da177e4 635
916b7656 636 info->flags &= ~ASYNC_INITIALIZED;
1da177e4
LT
637 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 */
588993dd 645static void change_speed(struct tty_struct *tty, struct serial_state *info,
606d099c 646 struct ktermios *old_termios)
1da177e4
LT
647{
648 int quot = 0, baud_base, baud;
649 unsigned cflag, cval = 0;
650 int bits;
651 unsigned long flags;
652
588993dd 653 cflag = tty->termios->c_cflag;
1da177e4
LT
654
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 */
588993dd 674 baud = tty_get_baud_rate(tty);
1da177e4
LT
675 if (!baud)
676 baud = 9600; /* B0 transition handled in rs_set_termios */
916b7656 677 baud_base = info->baud_base;
1da177e4 678 if (baud == 38400 &&
916b7656
JS
679 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
680 quot = info->custom_divisor;
1da177e4
LT
681 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) {
db0ef08e 690 /* FIXME: Will need updating for new tty in the end */
588993dd
JS
691 tty->termios->c_cflag &= ~CBAUD;
692 tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
693 baud = tty_get_baud_rate(tty);
1da177e4
LT
694 if (!baud)
695 baud = 9600;
696 if (baud == 38400 &&
916b7656
JS
697 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
698 quot = info->custom_divisor;
1da177e4
LT
699 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;
916b7656 711 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
1da177e4
LT
712 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;
916b7656 716 if (info->flags & ASYNC_HARDPPS_CD)
1da177e4
LT
717 info->IER |= UART_IER_MSI;
718 if (cflag & CRTSCTS) {
916b7656 719 info->flags |= ASYNC_CTS_FLOW;
1da177e4
LT
720 info->IER |= UART_IER_MSI;
721 } else
916b7656 722 info->flags &= ~ASYNC_CTS_FLOW;
1da177e4 723 if (cflag & CLOCAL)
916b7656 724 info->flags &= ~ASYNC_CHECK_CD;
1da177e4 725 else {
916b7656 726 info->flags |= ASYNC_CHECK_CD;
1da177e4
LT
727 info->IER |= UART_IER_MSI;
728 }
729 /* TBD:
4b512d26 730 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
1da177e4
LT
731 */
732
733 /*
734 * Set up parity check flag
735 */
1da177e4
LT
736
737 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
588993dd 738 if (I_INPCK(tty))
1da177e4 739 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
588993dd 740 if (I_BRKINT(tty) || I_PARMRK(tty))
1da177e4
LT
741 info->read_status_mask |= UART_LSR_BI;
742
743 /*
744 * Characters to ignore
745 */
746 info->ignore_status_mask = 0;
588993dd 747 if (I_IGNPAR(tty))
1da177e4 748 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
588993dd 749 if (I_IGNBRK(tty)) {
1da177e4
LT
750 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 */
588993dd 755 if (I_IGNPAR(tty))
1da177e4
LT
756 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
1da177e4
LT
780 local_irq_restore(flags);
781}
782
257afa3c 783static int rs_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4 784{
916b7656 785 struct serial_state *info;
1da177e4
LT
786 unsigned long flags;
787
d9ad7ef1
TJRMI
788 info = tty->driver_data;
789
1da177e4 790 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
257afa3c 791 return 0;
1da177e4 792
d9ad7ef1 793 if (!info->xmit.buf)
257afa3c 794 return 0;
1da177e4
LT
795
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);
257afa3c 801 return 0;
1da177e4
LT
802 }
803
804 info->xmit.buf[info->xmit.head++] = ch;
805 info->xmit.head &= SERIAL_XMIT_SIZE-1;
806 local_irq_restore(flags);
257afa3c 807 return 1;
1da177e4
LT
808}
809
810static void rs_flush_chars(struct tty_struct *tty)
811{
916b7656 812 struct serial_state *info = tty->driver_data;
1da177e4
LT
813 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;
916b7656 837 struct serial_state *info = tty->driver_data;
1da177e4
LT
838 unsigned long flags;
839
840 if (serial_paranoia_check(info, tty->name, "rs_write"))
841 return 0;
842
b3218a79 843 if (!info->xmit.buf)
1da177e4
LT
844 return 0;
845
3abf3bed 846 local_irq_save(flags);
1da177e4
LT
847 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{
916b7656 883 struct serial_state *info = tty->driver_data;
1da177e4
LT
884
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{
916b7656 892 struct serial_state *info = tty->driver_data;
1da177e4
LT
893
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{
916b7656 901 struct serial_state *info = tty->driver_data;
1da177e4
LT
902 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);
1da177e4
LT
909 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{
916b7656 918 struct serial_state *info = tty->driver_data;
1da177e4
LT
919 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{
916b7656 953 struct serial_state *info = tty->driver_data;
1da177e4
LT
954 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{
916b7656 978 struct serial_state *info = tty->driver_data;
1da177e4
LT
979 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
916b7656 1009static int get_serial_info(struct serial_state *state,
ab14caec 1010 struct serial_struct __user * retinfo)
1da177e4
LT
1011{
1012 struct serial_struct tmp;
1da177e4
LT
1013
1014 if (!retinfo)
1015 return -EFAULT;
1016 memset(&tmp, 0, sizeof(tmp));
ec79d605 1017 tty_lock();
1da177e4
LT
1018 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;
1025 tmp.close_delay = state->close_delay;
1026 tmp.closing_wait = state->closing_wait;
1027 tmp.custom_divisor = state->custom_divisor;
ec79d605 1028 tty_unlock();
1da177e4
LT
1029 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1030 return -EFAULT;
1031 return 0;
1032}
1033
588993dd 1034static int set_serial_info(struct tty_struct *tty, struct serial_state *state,
ab14caec 1035 struct serial_struct __user * new_info)
1da177e4
LT
1036{
1037 struct serial_struct new_serial;
0f9b9684 1038 bool change_spd;
1da177e4
LT
1039 int retval = 0;
1040
1041 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1042 return -EFAULT;
e18ce49b 1043
ec79d605 1044 tty_lock();
0f9b9684
JS
1045 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;
e18ce49b 1051 }
1da177e4
LT
1052
1053 if (!serial_isroot()) {
1054 if ((new_serial.baud_base != state->baud_base) ||
1055 (new_serial.close_delay != state->close_delay) ||
1056 (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));
1da177e4
LT
1062 state->custom_divisor = new_serial.custom_divisor;
1063 goto check_and_exit;
1064 }
1065
e18ce49b 1066 if (new_serial.baud_base < 9600) {
ec79d605 1067 tty_unlock();
1da177e4 1068 return -EINVAL;
e18ce49b 1069 }
1da177e4
LT
1070
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));
1da177e4
LT
1079 state->custom_divisor = new_serial.custom_divisor;
1080 state->close_delay = new_serial.close_delay * HZ/100;
1081 state->closing_wait = new_serial.closing_wait * HZ/100;
588993dd 1082 tty->low_latency = (state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1da177e4
LT
1083
1084check_and_exit:
fef21073 1085 if (state->flags & ASYNC_INITIALIZED) {
0f9b9684 1086 if (change_spd) {
1da177e4 1087 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
588993dd 1088 tty->alt_speed = 57600;
1da177e4 1089 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
588993dd 1090 tty->alt_speed = 115200;
1da177e4 1091 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
588993dd 1092 tty->alt_speed = 230400;
1da177e4 1093 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
588993dd
JS
1094 tty->alt_speed = 460800;
1095 change_speed(tty, state, NULL);
1da177e4
LT
1096 }
1097 } else
588993dd 1098 retval = startup(tty, state);
ec79d605 1099 tty_unlock();
1da177e4
LT
1100 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 */
916b7656 1114static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
1da177e4
LT
1115{
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
60b33c13 1131static int rs_tiocmget(struct tty_struct *tty)
1da177e4 1132{
916b7656 1133 struct serial_state *info = tty->driver_data;
1da177e4
LT
1134 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
20b9d177
AC
1153static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1154 unsigned int clear)
1da177e4 1155{
916b7656 1156 struct serial_state *info = tty->driver_data;
1da177e4
LT
1157 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 */
9e98966c 1181static int rs_break(struct tty_struct *tty, int break_state)
1da177e4 1182{
916b7656 1183 struct serial_state *info = tty->driver_data;
1da177e4
LT
1184 unsigned long flags;
1185
1186 if (serial_paranoia_check(info, tty->name, "rs_break"))
970a8a51 1187 return -EINVAL;
1da177e4
LT
1188
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);
9e98966c 1196 return 0;
1da177e4
LT
1197}
1198
0587102c
AC
1199/*
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{
916b7656 1208 struct serial_state *info = tty->driver_data;
0587102c
AC
1209 struct async_icount cnow;
1210 unsigned long flags;
1211
1212 local_irq_save(flags);
916b7656 1213 cnow = info->icount;
0587102c
AC
1214 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}
1da177e4 1229
6caa76b7 1230static int rs_ioctl(struct tty_struct *tty,
1da177e4
LT
1231 unsigned int cmd, unsigned long arg)
1232{
916b7656 1233 struct serial_state *info = tty->driver_data;
1da177e4 1234 struct async_icount cprev, cnow; /* kernel counter temps */
ab14caec 1235 void __user *argp = (void __user *)arg;
1da177e4
LT
1236 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:
ab14caec 1250 return get_serial_info(info, argp);
1da177e4 1251 case TIOCSSERIAL:
588993dd 1252 return set_serial_info(tty, info, argp);
1da177e4
LT
1253 case TIOCSERCONFIG:
1254 return 0;
1255
1256 case TIOCSERGETLSR: /* Get line status register */
ab14caec 1257 return get_lsr_info(info, argp);
1da177e4
LT
1258
1259 case TIOCSERGSTRUCT:
ab14caec 1260 if (copy_to_user(argp,
916b7656 1261 info, sizeof(struct serial_state)))
1da177e4
LT
1262 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 */
916b7656 1274 cprev = info->icount;
1da177e4
LT
1275 local_irq_restore(flags);
1276 while (1) {
87758791 1277 interruptible_sleep_on(&info->tport.delta_msr_wait);
1da177e4
LT
1278 /* see if a signal did it */
1279 if (signal_pending(current))
1280 return -ERESTARTSYS;
1281 local_irq_save(flags);
916b7656 1282 cnow = info->icount; /* atomic copy */
1da177e4
LT
1283 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
1da177e4
LT
1297 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
606d099c 1309static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1da177e4 1310{
916b7656 1311 struct serial_state *info = tty->driver_data;
1da177e4
LT
1312 unsigned long flags;
1313 unsigned int cflag = tty->termios->c_cflag;
1314
588993dd 1315 change_speed(tty, info, old_termios);
1da177e4
LT
1316
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{
916b7656 1371 struct serial_state *state = tty->driver_data;
1da177e4
LT
1372 unsigned long flags;
1373
916b7656 1374 if (!state || serial_paranoia_check(state, tty->name, "rs_close"))
1da177e4
LT
1375 return;
1376
1da177e4
LT
1377 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
916b7656 1386 printk("rs_close ttys%d, count = %d\n", state->line, state->count);
1da177e4
LT
1387#endif
1388 if ((tty->count == 1) && (state->count != 1)) {
1389 /*
1390 * Uh, oh. tty->count is 1, which means that the tty
1391 * structure will be freed. state->count should always
1392 * 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, "
1397 "state->count is %d\n", state->count);
1398 state->count = 1;
1399 }
1400 if (--state->count < 0) {
1401 printk("rs_close: bad serial port count for ttys%d: %d\n",
d8522563 1402 state->line, state->count);
1da177e4
LT
1403 state->count = 0;
1404 }
1405 if (state->count) {
1406 DBG_CNT("before DEC-2");
1407 local_irq_restore(flags);
1408 return;
1409 }
fef21073 1410 state->flags |= ASYNC_CLOSING;
1da177e4
LT
1411 /*
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;
d8522563
JS
1416 if (state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1417 tty_wait_until_sent(tty, state->closing_wait);
1da177e4
LT
1418 /*
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 */
916b7656 1424 state->read_status_mask &= ~UART_LSR_DR;
fef21073 1425 if (state->flags & ASYNC_INITIALIZED) {
1da177e4
LT
1426 /* 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 */
916b7656 1438 rs_wait_until_sent(tty, state->timeout);
1da177e4 1439 }
588993dd 1440 shutdown(tty, state);
978e595f 1441 rs_flush_buffer(tty);
1da177e4
LT
1442
1443 tty_ldisc_flush(tty);
1444 tty->closing = 0;
87758791
JS
1445 state->tport.tty = NULL;
1446 if (state->tport.blocked_open) {
d8522563
JS
1447 if (state->close_delay) {
1448 msleep_interruptible(jiffies_to_msecs(state->close_delay));
1da177e4 1449 }
87758791 1450 wake_up_interruptible(&state->tport.open_wait);
1da177e4 1451 }
fef21073 1452 state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
87758791 1453 wake_up_interruptible(&state->tport.close_wait);
1da177e4
LT
1454 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{
916b7656 1462 struct serial_state *info = tty->driver_data;
1da177e4
LT
1463 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
916b7656 1469 if (info->xmit_fifo_size == 0)
1da177e4
LT
1470 return; /* Just in case.... */
1471
1472 orig_jiffies = jiffies;
978e595f 1473
1da177e4
LT
1474 /*
1475 * 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 */
916b7656 1482 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1da177e4
LT
1483 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 }
cc0a8fbb 1513 __set_current_state(TASK_RUNNING);
eff4b0b9 1514
1da177e4
LT
1515#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{
916b7656 1525 struct serial_state *info = tty->driver_data;
1da177e4
LT
1526
1527 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1528 return;
1529
1da177e4 1530 rs_flush_buffer(tty);
588993dd 1531 shutdown(tty, info);
916b7656
JS
1532 info->count = 0;
1533 info->flags &= ~ASYNC_NORMAL_ACTIVE;
87758791
JS
1534 info->tport.tty = NULL;
1535 wake_up_interruptible(&info->tport.open_wait);
1da177e4
LT
1536}
1537
1538/*
1539 * ------------------------------------------------------------
1540 * rs_open() and friends
1541 * ------------------------------------------------------------
1542 */
1543static int block_til_ready(struct tty_struct *tty, struct file * filp,
916b7656 1544 struct serial_state *info)
1da177e4
LT
1545{
1546#ifdef DECLARE_WAITQUEUE
1547 DECLARE_WAITQUEUE(wait, current);
1548#else
1549 struct wait_queue wait = { current, NULL };
1550#endif
1da177e4
LT
1551 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) ||
916b7656
JS
1560 (info->flags & ASYNC_CLOSING)) {
1561 if (info->flags & ASYNC_CLOSING)
87758791 1562 interruptible_sleep_on(&info->tport.close_wait);
1da177e4 1563#ifdef SERIAL_DO_RESTART
916b7656 1564 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1da177e4
LT
1565 -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))) {
916b7656 1577 info->flags |= ASYNC_NORMAL_ACTIVE;
1da177e4
LT
1578 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
916b7656 1587 * this loop, info->count is dropped by one, so that
1da177e4
LT
1588 * rs_close() knows when to free things. We restore it upon
1589 * exit, either normal or abnormal.
1590 */
1591 retval = 0;
87758791 1592 add_wait_queue(&info->tport.open_wait, &wait);
1da177e4
LT
1593#ifdef SERIAL_DEBUG_OPEN
1594 printk("block_til_ready before block: ttys%d, count = %d\n",
916b7656 1595 info->line, info->count);
1da177e4
LT
1596#endif
1597 local_irq_save(flags);
1598 if (!tty_hung_up_p(filp)) {
1599 extra_count = 1;
916b7656 1600 info->count--;
1da177e4
LT
1601 }
1602 local_irq_restore(flags);
87758791 1603 info->tport.blocked_open++;
1da177e4
LT
1604 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) ||
916b7656 1611 !(info->flags & ASYNC_INITIALIZED)) {
1da177e4 1612#ifdef SERIAL_DO_RESTART
916b7656 1613 if (info->flags & ASYNC_HUP_NOTIFY)
1da177e4
LT
1614 retval = -EAGAIN;
1615 else
1616 retval = -ERESTARTSYS;
1617#else
1618 retval = -EAGAIN;
1619#endif
1620 break;
1621 }
916b7656 1622 if (!(info->flags & ASYNC_CLOSING) &&
1da177e4
LT
1623 (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",
916b7656 1631 info->line, info->count);
1da177e4 1632#endif
e142a31d 1633 tty_unlock();
1da177e4 1634 schedule();
e142a31d 1635 tty_lock();
1da177e4 1636 }
cc0a8fbb 1637 __set_current_state(TASK_RUNNING);
87758791 1638 remove_wait_queue(&info->tport.open_wait, &wait);
1da177e4 1639 if (extra_count)
916b7656 1640 info->count++;
87758791 1641 info->tport.blocked_open--;
1da177e4
LT
1642#ifdef SERIAL_DEBUG_OPEN
1643 printk("block_til_ready after blocking: ttys%d, count = %d\n",
916b7656 1644 info->line, info->count);
1da177e4
LT
1645#endif
1646 if (retval)
1647 return retval;
916b7656 1648 info->flags |= ASYNC_NORMAL_ACTIVE;
1da177e4
LT
1649 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{
916b7656 1660 struct serial_state *info = rs_table + tty->index;
410235fd 1661 int retval;
1da177e4 1662
916b7656 1663 info->count++;
87758791 1664 info->tport.tty = tty;
916b7656 1665 tty->driver_data = info;
87758791 1666 tty->port = &info->tport;
1da177e4
LT
1667 if (serial_paranoia_check(info, tty->name, "rs_open"))
1668 return -ENODEV;
1669
1670#ifdef SERIAL_DEBUG_OPEN
916b7656 1671 printk("rs_open %s, count = %d\n", tty->name, info->count);
1da177e4 1672#endif
916b7656 1673 tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1da177e4 1674
1da177e4
LT
1675 /*
1676 * If the port is the middle of closing, bail out now
1677 */
1678 if (tty_hung_up_p(filp) ||
916b7656
JS
1679 (info->flags & ASYNC_CLOSING)) {
1680 if (info->flags & ASYNC_CLOSING)
87758791 1681 interruptible_sleep_on(&info->tport.close_wait);
1da177e4 1682#ifdef SERIAL_DO_RESTART
916b7656 1683 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1da177e4
LT
1684 -EAGAIN : -ERESTARTSYS);
1685#else
1686 return -EAGAIN;
1687#endif
1688 }
1689
1690 /*
1691 * Start up serial port
1692 */
588993dd 1693 retval = startup(tty, info);
1da177e4
LT
1694 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
d594027d 1717static inline void line_info(struct seq_file *m, struct serial_state *state)
1da177e4 1718{
1da177e4 1719 char stat_buf[30], control, status;
1da177e4
LT
1720 unsigned long flags;
1721
d594027d 1722 seq_printf(m, "%d: uart:amiga_builtin",state->line);
1da177e4 1723
1da177e4
LT
1724 local_irq_save(flags);
1725 status = ciab.pra;
916b7656 1726 control = (state->flags & ASYNC_INITIALIZED) ? state->MCR : status;
1da177e4
LT
1727 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
916b7656
JS
1742 if (state->quot)
1743 seq_printf(m, " baud:%d", state->baud_base / state->quot);
1da177e4 1744
d594027d 1745 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1da177e4
LT
1746
1747 if (state->icount.frame)
d594027d 1748 seq_printf(m, " fe:%d", state->icount.frame);
1da177e4
LT
1749
1750 if (state->icount.parity)
d594027d 1751 seq_printf(m, " pe:%d", state->icount.parity);
1da177e4
LT
1752
1753 if (state->icount.brk)
d594027d 1754 seq_printf(m, " brk:%d", state->icount.brk);
1da177e4
LT
1755
1756 if (state->icount.overrun)
d594027d 1757 seq_printf(m, " oe:%d", state->icount.overrun);
1da177e4
LT
1758
1759 /*
1760 * Last thing is the RS-232 status lines
1761 */
d594027d 1762 seq_printf(m, " %s\n", stat_buf+1);
1da177e4
LT
1763}
1764
d594027d 1765static int rs_proc_show(struct seq_file *m, void *v)
1da177e4 1766{
d594027d
AD
1767 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1768 line_info(m, &rs_table[0]);
1769 return 0;
1da177e4
LT
1770}
1771
d594027d
AD
1772static 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
1da177e4
LT
1785/*
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 */
41c28ff1 1798static void show_serial_version(void)
1da177e4
LT
1799{
1800 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1801}
1802
1803
b68e31d0 1804static const struct tty_operations serial_ops = {
1da177e4
LT
1805 .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,
1da177e4
LT
1823 .tiocmget = rs_tiocmget,
1824 .tiocmset = rs_tiocmset,
0587102c 1825 .get_icount = rs_get_icount,
d594027d 1826 .proc_fops = &rs_proc_fops,
1da177e4
LT
1827};
1828
1829/*
1830 * The serial driver boot-time initialization code!
1831 */
826e8c8c 1832static int __init amiga_serial_probe(struct platform_device *pdev)
1da177e4
LT
1833{
1834 unsigned long flags;
1835 struct serial_state * state;
5edc304f 1836 int error;
1da177e4 1837
410235fd 1838 serial_driver = alloc_tty_driver(NR_PORTS);
1da177e4
LT
1839 if (!serial_driver)
1840 return -ENOMEM;
1841
1da177e4
LT
1842 show_serial_version();
1843
1844 /* Initialize the tty_driver structure */
1845
1da177e4
LT
1846 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
5edc304f
GU
1858 error = tty_register_driver(serial_driver);
1859 if (error)
826e8c8c 1860 goto fail_put_tty_driver;
1da177e4
LT
1861
1862 state = rs_table;
1da177e4
LT
1863 state->port = (int)&custom.serdatr; /* Just to give it a value */
1864 state->line = 0;
1865 state->custom_divisor = 0;
1866 state->close_delay = 5*HZ/10;
1867 state->closing_wait = 30*HZ;
1868 state->icount.cts = state->icount.dsr =
1869 state->icount.rng = state->icount.dcd = 0;
1870 state->icount.rx = state->icount.tx = 0;
1871 state->icount.frame = state->icount.parity = 0;
1872 state->icount.overrun = state->icount.brk = 0;
87758791 1873 tty_port_init(&state->tport);
1da177e4
LT
1874
1875 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
1876 state->line);
1877
1878 /* Hardware set up */
1879
1880 state->baud_base = amiga_colorclock;
1881 state->xmit_fifo_size = 1;
1882
1da177e4 1883 /* set ISRs, and then disable the rx interrupts */
5edc304f
GU
1884 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1885 if (error)
1886 goto fail_unregister;
1887
9cfb5c05 1888 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
5edc304f
GU
1889 "serial RX", state);
1890 if (error)
1891 goto fail_free_irq;
1da177e4 1892
c70c036f
JL
1893 local_irq_save(flags);
1894
1da177e4
LT
1895 /* turn off Rx and Tx interrupts */
1896 custom.intena = IF_RBF | IF_TBE;
1897 mb();
1898
1899 /* clear any pending interrupt */
1900 custom.intreq = IF_RBF | IF_TBE;
1901 mb();
1902
1903 local_irq_restore(flags);
1904
1905 /*
1906 * set the appropriate directions for the modem control flags,
1907 * and clear RTS and DTR
1908 */
1909 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1910 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1911
826e8c8c
GU
1912 platform_set_drvdata(pdev, state);
1913
1da177e4 1914 return 0;
5edc304f
GU
1915
1916fail_free_irq:
1917 free_irq(IRQ_AMIGA_TBE, state);
1918fail_unregister:
1919 tty_unregister_driver(serial_driver);
5edc304f
GU
1920fail_put_tty_driver:
1921 put_tty_driver(serial_driver);
1922 return error;
1da177e4
LT
1923}
1924
826e8c8c 1925static int __exit amiga_serial_remove(struct platform_device *pdev)
1da177e4
LT
1926{
1927 int error;
826e8c8c 1928 struct serial_state *state = platform_get_drvdata(pdev);
1da177e4
LT
1929
1930 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
1da177e4
LT
1931 if ((error = tty_unregister_driver(serial_driver)))
1932 printk("SERIAL: failed to unregister serial driver (%d)\n",
1933 error);
1934 put_tty_driver(serial_driver);
1935
916b7656
JS
1936 free_irq(IRQ_AMIGA_TBE, state);
1937 free_irq(IRQ_AMIGA_RBF, state);
5edc304f 1938
826e8c8c
GU
1939 platform_set_drvdata(pdev, NULL);
1940
1941 return error;
1942}
1943
1944static struct platform_driver amiga_serial_driver = {
1945 .remove = __exit_p(amiga_serial_remove),
1946 .driver = {
1947 .name = "amiga-serial",
1948 .owner = THIS_MODULE,
1949 },
1950};
1951
1952static int __init amiga_serial_init(void)
1953{
1954 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
1955}
1956
1957module_init(amiga_serial_init);
1958
1959static void __exit amiga_serial_exit(void)
1960{
1961 platform_driver_unregister(&amiga_serial_driver);
1da177e4
LT
1962}
1963
826e8c8c 1964module_exit(amiga_serial_exit);
1da177e4
LT
1965
1966
d1a35e4d
GU
1967#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
1968
1da177e4
LT
1969/*
1970 * ------------------------------------------------------------
1971 * Serial console driver
1972 * ------------------------------------------------------------
1973 */
1da177e4
LT
1974
1975static void amiga_serial_putc(char c)
1976{
1977 custom.serdat = (unsigned char)c | 0x100;
1978 while (!(custom.serdatr & 0x2000))
1979 barrier();
1980}
1981
1982/*
1983 * Print a string to the serial port trying not to disturb
1984 * any possible real use of the port...
1985 *
1986 * The console must be locked when we get here.
1987 */
1988static void serial_console_write(struct console *co, const char *s,
1989 unsigned count)
1990{
1991 unsigned short intena = custom.intenar;
1992
1993 custom.intena = IF_TBE;
1994
1995 while (count--) {
1996 if (*s == '\n')
1997 amiga_serial_putc('\r');
1998 amiga_serial_putc(*s++);
1999 }
2000
2001 custom.intena = IF_SETCLR | (intena & IF_TBE);
2002}
2003
2004static struct tty_driver *serial_console_device(struct console *c, int *index)
2005{
2006 *index = 0;
2007 return serial_driver;
2008}
2009
2010static struct console sercons = {
2011 .name = "ttyS",
2012 .write = serial_console_write,
2013 .device = serial_console_device,
2014 .flags = CON_PRINTBUFFER,
2015 .index = -1,
2016};
2017
2018/*
2019 * Register console.
2020 */
2021static int __init amiserial_console_init(void)
2022{
2023 register_console(&sercons);
2024 return 0;
2025}
2026console_initcall(amiserial_console_init);
d1a35e4d
GU
2027
2028#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
1da177e4
LT
2029
2030MODULE_LICENSE("GPL");
826e8c8c 2031MODULE_ALIAS("platform:amiga-serial");
This page took 1.309269 seconds and 5 git commands to generate.