import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / sbus / char / su.c
1 /* $Id: su.c,v 1.1.1.1 2005/04/11 02:50:34 jack Exp $
2  * su.c: Small serial driver for keyboard/mouse interface on sparc32/PCI
3  *
4  * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be)
5  * Copyright (C) 1998-1999  Pete Zaitcev   (zaitcev@yahoo.com)
6  *
7  * This is mainly a variation of drivers/char/serial.c,
8  * credits go to authors mentioned therein.
9  *
10  * Fixed to use tty_get_baud_rate().
11  *   Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
12  */
13
14 /*
15  * Configuration section.
16  */
17 #undef SERIAL_PARANOIA_CHECK
18 #define CONFIG_SERIAL_NOPAUSE_IO        /* Unused on sparc */
19 #define SERIAL_DO_RESTART
20
21 /* Set of debugging defines */
22
23 #undef SERIAL_DEBUG_INTR
24 #undef SERIAL_DEBUG_OPEN
25 #undef SERIAL_DEBUG_FLOW
26 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
27 #undef SERIAL_DEBUG_THROTTLE
28
29 #define RS_ISR_PASS_LIMIT 256
30
31 /*
32  * 0x20 is sun4m thing, Dave Redman heritage.
33  * See arch/sparc/kernel/irq.c.
34  */
35 #define IRQ_4M(n)       ((n)|0x20)
36
37 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
38 #define DBG_CNT(s)                                                      \
39 do {                                                                    \
40         printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n",          \
41                kdevname(tty->device), (info->flags), serial_refcount,   \
42                info->count,tty->count,s);                               \
43 } while (0)
44 #else
45 #define DBG_CNT(s)
46 #endif
47
48 /*
49  * End of serial driver configuration section.
50  */
51 #include <linux/config.h>
52 #include <linux/module.h>
53 #include <linux/errno.h>
54 #include <linux/signal.h>
55 #include <linux/sched.h>
56 #include <linux/interrupt.h>
57 #include <linux/tty.h>
58 #include <linux/tty_flip.h>
59 #include <linux/serial.h>
60 #include <linux/serialP.h>
61 #include <linux/serial_reg.h>
62 #include <linux/string.h>
63 #include <linux/fcntl.h>
64 #include <linux/ptrace.h>
65 #include <linux/ioport.h>
66 #include <linux/mm.h>
67 #include <linux/slab.h>
68 #include <linux/init.h>
69 #include <linux/bootmem.h>
70 #include <linux/delay.h>
71 #ifdef CONFIG_SERIAL_CONSOLE
72 #include <linux/console.h>
73 #include <linux/major.h>
74 #endif
75 #include <linux/sysrq.h>
76
77 #include <asm/system.h>
78 #include <asm/oplib.h>
79 #include <asm/io.h>
80 #include <asm/ebus.h>
81 #ifdef CONFIG_SPARC64
82 #include <asm/isa.h>
83 #endif
84 #include <asm/irq.h>
85 #include <asm/uaccess.h>
86 #include <asm/bitops.h>
87
88 #include "sunserial.h"
89 #include "sunkbd.h"
90 #include "sunmouse.h"
91
92 /* We are on a NS PC87303 clocked with 24.0 MHz, which results
93  * in a UART clock of 1.8462 MHz.
94  */
95 #define BAUD_BASE       (1846200 / 16)
96
97 #ifdef CONFIG_SERIAL_CONSOLE
98 extern int serial_console;
99 static struct console sercons;
100 int su_serial_console_init(void);
101 #endif
102
103 enum su_type { SU_PORT_NONE, SU_PORT_MS, SU_PORT_KBD, SU_PORT_PORT };
104 static char *su_typev[] = { "???", "mouse", "kbd", "serial" };
105
106 #define SU_PROPSIZE     128
107
108 /*
109  * serial.c saves memory when it allocates async_info upon first open.
110  * We have parts of state structure together because we do call startup
111  * for keyboard and mouse.
112  */
113 struct su_struct {
114         int              magic;
115         unsigned long    port;
116         int              baud_base;
117         int              type;          /* Hardware type: e.g. 16550 */
118         int              irq;
119         int              flags;
120         int              line;
121         int              cflag;
122
123         enum su_type     port_type;     /* Hookup type: e.g. mouse */
124         int              is_console;
125         int              port_node;
126
127         char             name[16];
128
129         int              xmit_fifo_size;
130         int              custom_divisor;
131         unsigned short   close_delay;
132         unsigned short   closing_wait;  /* time to wait before closing */
133
134         struct tty_struct       *tty;
135         int                     read_status_mask;
136         int                     ignore_status_mask;
137         int                     timeout;
138         int                     quot;
139         int                     x_char; /* xon/xoff character */
140         int                     IER;    /* Interrupt Enable Register */
141         int                     MCR;    /* Modem control register */
142         unsigned long           event;
143         int                     blocked_open; /* # of blocked opens */
144         long                    session; /* Session of opening process */
145         long                    pgrp; /* pgrp of opening process */
146         unsigned char           *xmit_buf;
147         int                     xmit_head;
148         int                     xmit_tail;
149         int                     xmit_cnt;
150         struct tq_struct        tqueue;
151         wait_queue_head_t       open_wait;
152         wait_queue_head_t       close_wait;
153         wait_queue_head_t       delta_msr_wait;
154
155         int                     count;
156         struct async_icount     icount;
157         struct termios          normal_termios, callout_termios;
158         unsigned long           last_active;    /* For async_struct, to be */
159 };
160
161 /*
162  * Scan status structure.
163  * "prop" is a local variable but it eats stack to keep it in each
164  * stack frame of a recursive procedure.
165  */
166 struct su_probe_scan {
167         int msnode, kbnode;     /* PROM nodes for mouse and keyboard */
168         int msx, kbx;           /* minors for mouse and keyboard */
169         int devices;            /* scan index */
170         char prop[SU_PROPSIZE];
171 };
172
173 static char *serial_name = "PCIO serial driver";
174 static char serial_version[16];
175
176 static DECLARE_TASK_QUEUE(tq_serial);
177
178 static struct tty_driver serial_driver, callout_driver;
179 static int serial_refcount;
180
181 /* number of characters left in xmit buffer before we ask for more */
182 #define WAKEUP_CHARS 256
183
184 static void autoconfig(struct su_struct *info);
185 static void change_speed(struct su_struct *info, struct termios *old);
186 static void su_wait_until_sent(struct tty_struct *tty, int timeout);
187
188 /*
189  * Here we define the default xmit fifo size used for each type of
190  * UART
191  */
192 static struct serial_uart_config uart_config[] = {
193         { "unknown", 1, 0 }, 
194         { "8250", 1, 0 }, 
195         { "16450", 1, 0 }, 
196         { "16550", 1, 0 }, 
197         { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO }, 
198         { "cirrus", 1, 0 }, 
199         { "ST16650", 1, UART_CLEAR_FIFO |UART_STARTECH }, 
200         { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
201                   UART_STARTECH }, 
202         { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
203         { 0, 0}
204 };
205
206
207 #define NR_PORTS        4
208
209 static struct su_struct su_table[NR_PORTS];
210 static struct tty_struct *serial_table[NR_PORTS];
211 static struct termios *serial_termios[NR_PORTS];
212 static struct termios *serial_termios_locked[NR_PORTS];
213
214 #ifndef MIN
215 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
216 #endif
217
218 /*
219  * tmp_buf is used as a temporary buffer by serial_write.  We need to
220  * lock it in case the copy_from_user blocks while swapping in a page,
221  * and some other program tries to do a serial write at the same time.
222  * Since the lock will only come under contention when the system is
223  * swapping and available memory is low, it makes sense to share one
224  * buffer across all the serial ports, since it significantly saves
225  * memory if large numbers of serial ports are open.
226  */
227 static unsigned char *tmp_buf;
228 static DECLARE_MUTEX(tmp_buf_sem);
229
230 static inline int serial_paranoia_check(struct su_struct *info,
231                                         kdev_t device, const char *routine)
232 {
233 #ifdef SERIAL_PARANOIA_CHECK
234         static const char *badmagic = KERN_WARNING
235                 "Warning: bad magic number for serial struct (%s) in %s\n";
236         static const char *badinfo = KERN_WARNING
237                 "Warning: null su_struct for (%s) in %s\n";
238
239         if (!info) {
240                 printk(badinfo, kdevname(device), routine);
241                 return 1;
242         }
243         if (info->magic != SERIAL_MAGIC) {
244                 printk(badmagic, kdevname(device), routine);
245                 return 1;
246         }
247 #endif
248         return 0;
249 }
250
251 static inline
252 unsigned int su_inb(struct su_struct *info, unsigned long offset)
253 {
254         return inb(info->port + offset);
255 }
256
257 static inline void
258 su_outb(struct su_struct *info, unsigned long offset, int value)
259 {
260 #ifndef __sparc_v9__
261         /*
262          * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
263          * connected with a gate then go to SlavIO. When IRQ4 goes tristated
264          * gate outputs a logical one. Since we use level triggered interrupts
265          * we have lockup and watchdog reset. We cannot mask IRQ because
266          * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
267          * This problem is similar to what Alpha people suffer, see serial.c.
268          */
269         if (offset == UART_MCR) value |= UART_MCR_OUT2;
270 #endif
271         outb(value, info->port + offset);
272 }
273
274 #define serial_in(info, off)            su_inb(info, off)
275 #define serial_inp(info, off)           su_inb(info, off)
276 #define serial_out(info, off, val)      su_outb(info, off, val)
277 #define serial_outp(info, off, val)     su_outb(info, off, val)
278
279 /*
280  * ------------------------------------------------------------
281  * su_stop() and su_start()
282  *
283  * This routines are called before setting or resetting tty->stopped.
284  * They enable or disable transmitter interrupts, as necessary.
285  * ------------------------------------------------------------
286  */
287 static void su_stop(struct tty_struct *tty)
288 {
289         struct su_struct *info = (struct su_struct *)tty->driver_data;
290         unsigned long flags;
291
292         if (serial_paranoia_check(info, tty->device, "su_stop"))
293                 return;
294
295         save_flags(flags); cli();
296         if (info->IER & UART_IER_THRI) {
297                 info->IER &= ~UART_IER_THRI;
298                 serial_out(info, UART_IER, info->IER);
299         }
300         restore_flags(flags);
301 }
302
303 static void su_start(struct tty_struct *tty)
304 {
305         struct su_struct *info = (struct su_struct *)tty->driver_data;
306         unsigned long flags;
307         
308         if (serial_paranoia_check(info, tty->device, "su_start"))
309                 return;
310
311         save_flags(flags); cli();
312         if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
313                 info->IER |= UART_IER_THRI;
314                 serial_out(info, UART_IER, info->IER);
315         }
316         restore_flags(flags);
317 }
318
319 /*
320  * ----------------------------------------------------------------------
321  *
322  * Here starts the interrupt handling routines.  All of the following
323  * subroutines are declared as inline and are folded into
324  * su_interrupt().  They were separated out for readability's sake.
325  *
326  * Note: rs_interrupt() is a "fast" interrupt, which means that it
327  * runs with interrupts turned off.  People who may want to modify
328  * rs_interrupt() should try to keep the interrupt handler as fast as
329  * possible.  After you are done making modifications, it is not a bad
330  * idea to do:
331  * 
332  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
333  *
334  * and look at the resulting assemble code in serial.s.
335  *
336  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
337  * -----------------------------------------------------------------------
338  */
339
340 /*
341  * This routine is used by the interrupt handler to schedule
342  * processing in the software interrupt portion of the driver.
343  */
344 static void
345 su_sched_event(struct su_struct *info, int event)
346 {
347         info->event |= 1 << event;
348         queue_task(&info->tqueue, &tq_serial);
349         mark_bh(SERIAL_BH);
350 }
351
352 static void
353 receive_kbd_ms_chars(struct su_struct *info, struct pt_regs *regs, int is_brk)
354 {
355         unsigned char status = 0;
356         unsigned char ch;
357
358         do {
359                 ch = serial_inp(info, UART_RX);
360                 if (info->port_type == SU_PORT_KBD) {
361                         if (ch == SUNKBD_RESET) {
362                                 l1a_state.kbd_id = 1;
363                                 l1a_state.l1_down = 0;
364                         } else if (l1a_state.kbd_id) {
365                                 l1a_state.kbd_id = 0;
366                         } else if (ch == SUNKBD_L1) {
367                                 l1a_state.l1_down = 1;
368                         } else if (ch == (SUNKBD_L1|SUNKBD_UP)) {
369                                 l1a_state.l1_down = 0;
370                         } else if (ch == SUNKBD_A && l1a_state.l1_down) {
371                                 /* whee... */
372                                 batten_down_hatches();
373                                 /* Continue execution... */
374                                 l1a_state.l1_down = 0;
375                                 l1a_state.kbd_id = 0;
376                                 return;
377                         }
378                         sunkbd_inchar(ch, regs);
379                 } else {
380                         sun_mouse_inbyte(ch, is_brk);
381                 }
382
383                 status = su_inb(info, UART_LSR);
384         } while (status & UART_LSR_DR);
385 }
386
387 static void
388 receive_serial_chars(struct su_struct *info, int *status, struct pt_regs *regs)
389 {
390         struct tty_struct *tty = info->tty;
391         unsigned char ch;
392         int ignored = 0, saw_console_brk = 0;
393         struct  async_icount *icount;
394
395         icount = &info->icount;
396         do {
397                 ch = serial_inp(info, UART_RX);
398                 if (info->is_console &&
399                     (ch == 0 || (*status &UART_LSR_BI)))
400                         saw_console_brk = 1;
401                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
402                         break;
403                 *tty->flip.char_buf_ptr = ch;
404                 icount->rx++;
405
406 #ifdef SERIAL_DEBUG_INTR
407                 printk("D%02x:%02x.", ch, *status);
408 #endif
409                 *tty->flip.flag_buf_ptr = 0;
410                 if (*status & (UART_LSR_BI | UART_LSR_PE |
411                                UART_LSR_FE | UART_LSR_OE)) {
412                         /*
413                          * For statistics only
414                          */
415                         if (*status & UART_LSR_BI) {
416                                 *status &= ~(UART_LSR_FE | UART_LSR_PE);
417                                 icount->brk++;
418                         } else if (*status & UART_LSR_PE)
419                                 icount->parity++;
420                         else if (*status & UART_LSR_FE)
421                                 icount->frame++;
422                         if (*status & UART_LSR_OE)
423                                 icount->overrun++;
424
425                         /*
426                          * Now check to see if character should be
427                          * ignored, and mask off conditions which
428                          * should be ignored.
429                          */
430                         if (*status & info->ignore_status_mask) {
431                                 if (++ignored > 100) {
432 #ifdef SERIAL_DEBUG_INTR
433                                         printk("ign100..");
434 #endif
435                                         break;
436                                 }
437                                 goto ignore_char;
438                         }
439                         *status &= info->read_status_mask;
440
441                         if (*status & (UART_LSR_BI)) {
442 #ifdef SERIAL_DEBUG_INTR
443                                 printk("handling break....");
444 #endif
445                                 *tty->flip.flag_buf_ptr = TTY_BREAK;
446                                 if (info->flags & ASYNC_SAK)
447                                         do_SAK(tty);
448                         } else if (*status & UART_LSR_PE)
449                                 *tty->flip.flag_buf_ptr = TTY_PARITY;
450                         else if (*status & UART_LSR_FE)
451                                 *tty->flip.flag_buf_ptr = TTY_FRAME;
452                         if (*status & UART_LSR_OE) {
453                                 /*
454                                  * Overrun is special, since it's
455                                  * reported immediately, and doesn't
456                                  * affect the current character
457                                  */
458                                 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
459                                         tty->flip.count++;
460                                         tty->flip.flag_buf_ptr++;
461                                         tty->flip.char_buf_ptr++;
462                                         *tty->flip.flag_buf_ptr = TTY_OVERRUN;
463                                 }
464                         }
465                 }
466                 tty->flip.flag_buf_ptr++;
467                 tty->flip.char_buf_ptr++;
468                 tty->flip.count++;
469         ignore_char:
470                 *status = serial_inp(info, UART_LSR);
471         } while (*status & UART_LSR_DR);
472 #ifdef SERIAL_DEBUG_INTR
473         printk("E%02x.R%d", *status, tty->flip.count);
474 #endif
475         tty_flip_buffer_push(tty);
476         if (saw_console_brk != 0)
477                 batten_down_hatches();
478 }
479
480 static void
481 transmit_chars(struct su_struct *info, int *intr_done)
482 {
483         int count;
484
485         if (info->x_char) {
486                 serial_outp(info, UART_TX, info->x_char);
487                 info->icount.tx++;
488                 info->x_char = 0;
489                 if (intr_done)
490                         *intr_done = 0;
491                 return;
492         }
493         if ((info->xmit_cnt <= 0) || info->tty->stopped ||
494             info->tty->hw_stopped) {
495                 info->IER &= ~UART_IER_THRI;
496                 serial_out(info, UART_IER, info->IER);
497                 return;
498         }
499
500         count = info->xmit_fifo_size;
501         do {
502                 serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
503                 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
504                 info->icount.tx++;
505                 if (--info->xmit_cnt <= 0)
506                         break;
507         } while (--count > 0);
508         
509         if (info->xmit_cnt < WAKEUP_CHARS)
510                 su_sched_event(info, RS_EVENT_WRITE_WAKEUP);
511
512 #ifdef SERIAL_DEBUG_INTR
513         printk("T%d...", info->xmit_cnt);
514 #endif
515         if (intr_done)
516                 *intr_done = 0;
517
518         if (info->xmit_cnt <= 0) {
519                 info->IER &= ~UART_IER_THRI;
520                 serial_out(info, UART_IER, info->IER);
521         }
522 }
523
524 static void
525 check_modem_status(struct su_struct *info)
526 {
527         int     status;
528         struct  async_icount *icount;
529
530         status = serial_in(info, UART_MSR);
531
532         if (status & UART_MSR_ANY_DELTA) {
533                 icount = &info->icount;
534                 /* update input line counters */
535                 if (status & UART_MSR_TERI)
536                         icount->rng++;
537                 if (status & UART_MSR_DDSR)
538                         icount->dsr++;
539                 if (status & UART_MSR_DDCD) {
540                         icount->dcd++;
541 #ifdef CONFIG_HARD_PPS
542                         if ((info->flags & ASYNC_HARDPPS_CD) &&
543                             (status & UART_MSR_DCD))
544                                 hardpps();
545 #endif
546                 }
547                 if (status & UART_MSR_DCTS)
548                         icount->cts++;
549                 wake_up_interruptible(&info->delta_msr_wait);
550         }
551
552         if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
553 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
554                 printk("ttys%d CD now %s...", info->line,
555                        (status & UART_MSR_DCD) ? "on" : "off");
556 #endif          
557                 if (status & UART_MSR_DCD)
558                         wake_up_interruptible(&info->open_wait);
559                 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
560                            (info->flags & ASYNC_CALLOUT_NOHUP))) {
561 #ifdef SERIAL_DEBUG_OPEN
562                         printk("doing serial hangup...");
563 #endif
564                         if (info->tty)
565                                 tty_hangup(info->tty);
566         }
567         }
568         if (info->flags & ASYNC_CTS_FLOW) {
569                 if (info->tty->hw_stopped) {
570                         if (status & UART_MSR_CTS) {
571 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
572                                 printk("CTS tx start...");
573 #endif
574                                 info->tty->hw_stopped = 0;
575                                 info->IER |= UART_IER_THRI;
576                                 serial_out(info, UART_IER, info->IER);
577                                 su_sched_event(info, RS_EVENT_WRITE_WAKEUP);
578                                 return;
579                         }
580                 } else {
581                         if (!(status & UART_MSR_CTS)) {
582 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
583                                 printk("CTS tx stop...");
584 #endif
585                                 info->tty->hw_stopped = 1;
586                                 info->IER &= ~UART_IER_THRI;
587                                 serial_out(info, UART_IER, info->IER);
588                         }
589                 }
590         }
591 }
592
593 /*
594  * This is the kbd/mouse serial driver's interrupt routine
595  */
596 static void
597 su_kbd_ms_interrupt(int irq, void *dev_id, struct pt_regs * regs)
598 {
599         struct su_struct *info = (struct su_struct *)dev_id;
600         unsigned char status;
601
602 #ifdef SERIAL_DEBUG_INTR
603         printk("su_kbd_ms_interrupt(%s)...", __irq_itoa(irq));
604 #endif
605         if (!info)
606                 return;
607
608         if (serial_in(info, UART_IIR) & UART_IIR_NO_INT)
609                 return;
610
611         status = serial_inp(info, UART_LSR);
612 #ifdef SERIAL_DEBUG_INTR
613         printk("status = %x...", status);
614 #endif
615         if ((status & UART_LSR_DR) || (status & UART_LSR_BI))
616                 receive_kbd_ms_chars(info, regs,
617                                      (status & UART_LSR_BI) != 0);
618
619 #ifdef SERIAL_DEBUG_INTR
620         printk("end.\n");
621 #endif
622 }
623
624 /*
625  * This is the serial driver's generic interrupt routine
626  */
627 static void
628 su_serial_interrupt(int irq, void *dev_id, struct pt_regs * regs)
629 {
630         int status;
631         struct su_struct *info;
632         int pass_counter = 0;
633
634 #ifdef SERIAL_DEBUG_INTR
635         printk("su_serial_interrupt(%s)...", __irq_itoa(irq));
636 #endif
637         info = (struct su_struct *)dev_id;
638         if (!info || !info->tty) {
639 #ifdef SERIAL_DEBUG_INTR
640                 printk("strain\n");
641 #endif
642                 return;
643         }
644
645         do {
646                 status = serial_inp(info, UART_LSR);
647 #ifdef SERIAL_DEBUG_INTR
648                 printk("status = %x...", status);
649 #endif
650                 if (status & UART_LSR_DR)
651                         receive_serial_chars(info, &status, regs);
652                 check_modem_status(info);
653                 if (status & UART_LSR_THRE)
654                         transmit_chars(info, 0);
655
656                 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
657 #ifdef SERIAL_DEBUG_INTR
658                         printk("rs loop break");
659 #endif
660                         break;  /* Prevent infinite loops */
661                 }
662         } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
663
664         info->last_active = jiffies;
665
666 #ifdef SERIAL_DEBUG_INTR
667         printk("end.\n");
668 #endif
669 }
670
671 /*
672  * -------------------------------------------------------------------
673  * Here ends the serial interrupt routines.
674  * -------------------------------------------------------------------
675  */
676
677 /*
678  * This routine is used to handle the "bottom half" processing for the
679  * serial driver, known also the "software interrupt" processing.
680  * This processing is done at the kernel interrupt level, after the
681  * su_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
682  * is where time-consuming activities which can not be done in the
683  * interrupt driver proper are done; the interrupt driver schedules
684  * them using su_sched_event(), and they get done here.
685  */
686 static void do_serial_bh(void)
687 {
688         run_task_queue(&tq_serial);
689 }
690
691 static void do_softint(void *private_)
692 {
693         struct su_struct        *info = (struct su_struct *) private_;
694         struct tty_struct       *tty;
695
696         tty = info->tty;
697         if (!tty)
698                 return;
699
700         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
701                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
702                     tty->ldisc.write_wakeup)
703                         (tty->ldisc.write_wakeup)(tty);
704                 wake_up_interruptible(&tty->write_wait);
705         }
706 }
707
708 /*
709  * ---------------------------------------------------------------
710  * Low level utility subroutines for the serial driver:  routines to
711  * figure out the appropriate timeout for an interrupt chain, routines
712  * to initialize and startup a serial port, and routines to shutdown a
713  * serial port.  Useful stuff like that.
714  * ---------------------------------------------------------------
715  */
716
717 static int
718 startup(struct su_struct *info)
719 {
720         unsigned long flags;
721         int     retval=0;
722         unsigned long page;
723
724         save_flags(flags);
725         if (info->tty) {
726                 page = get_free_page(GFP_KERNEL);
727                 if (!page)
728                         return -ENOMEM;
729
730                 cli();
731
732                 if (info->flags & ASYNC_INITIALIZED) {
733                         free_page(page);
734                         goto errout;
735                 }
736
737                 if (info->port == 0 || info->type == PORT_UNKNOWN) {
738                         set_bit(TTY_IO_ERROR, &info->tty->flags);
739                         free_page(page);
740                         goto errout;
741                 }
742                 if (info->xmit_buf)
743                         free_page(page);
744                 else
745                         info->xmit_buf = (unsigned char *) page;
746         }
747         cli();
748
749 #ifdef SERIAL_DEBUG_OPEN
750         printk("starting up ttys%d (irq %s)...", info->line,
751                __irq_itoa(info->irq));
752 #endif
753
754         if (uart_config[info->type].flags & UART_STARTECH) {
755                 /* Wake up UART */
756                 serial_outp(info, UART_LCR, 0xBF);
757                 serial_outp(info, UART_EFR, UART_EFR_ECB);
758                 serial_outp(info, UART_IER, 0);
759                 serial_outp(info, UART_EFR, 0);
760                 serial_outp(info, UART_LCR, 0);
761         }
762
763         if (info->type == PORT_16750) {
764                 /* Wake up UART */
765                 serial_outp(info, UART_IER, 0);
766         }
767
768         /*
769          * Clear the FIFO buffers and disable them
770          * (they will be reenabled in change_speed())
771          */
772         if (uart_config[info->type].flags & UART_CLEAR_FIFO)
773                 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
774                                              UART_FCR_CLEAR_XMIT));
775
776         /*
777          * At this point there's no way the LSR could still be 0xFF;
778          * if it is, then bail out, because there's likely no UART
779          * here.
780          */
781         if (serial_inp(info, UART_LSR) == 0xff) {
782                 if (capable(CAP_SYS_ADMIN)) {
783                         if (info->tty)
784                                 set_bit(TTY_IO_ERROR, &info->tty->flags);
785                 } else
786                         retval = -ENODEV;
787                 goto errout;
788         }
789
790         /*
791          * Allocate the IRQ if necessary
792          */
793         if (info->port_type != SU_PORT_PORT) {
794                 retval = request_irq(info->irq, su_kbd_ms_interrupt,
795                                      SA_SHIRQ, info->name, info);
796         } else {
797                 retval = request_irq(info->irq, su_serial_interrupt,
798                                      SA_SHIRQ, info->name, info);
799         }
800         if (retval) {
801                 if (capable(CAP_SYS_ADMIN)) {
802                         if (info->tty)
803                                 set_bit(TTY_IO_ERROR, &info->tty->flags);
804                         retval = 0;
805                 }
806                 goto errout;
807         }
808
809         /*
810          * Clear the interrupt registers.
811          */
812         (void) serial_inp(info, UART_RX);
813         (void) serial_inp(info, UART_IIR);
814         (void) serial_inp(info, UART_MSR);
815
816         /*
817          * Now, initialize the UART 
818          */
819         serial_outp(info, UART_LCR, UART_LCR_WLEN8);    /* reset DLAB */
820
821         info->MCR = 0;
822         if (info->tty && info->tty->termios->c_cflag & CBAUD)
823                 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
824         if (info->irq != 0)
825                 info->MCR |= UART_MCR_OUT2;
826         serial_outp(info, UART_MCR, info->MCR);
827
828         /*
829          * Finally, enable interrupts
830          */
831         info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
832         serial_outp(info, UART_IER, info->IER); /* enable interrupts */
833
834         /*
835          * And clear the interrupt registers again for luck.
836          */
837         (void)serial_inp(info, UART_LSR);
838         (void)serial_inp(info, UART_RX);
839         (void)serial_inp(info, UART_IIR);
840         (void)serial_inp(info, UART_MSR);
841
842         if (info->tty)
843                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
844         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
845
846         /*
847          * Set up the tty->alt_speed kludge
848          */
849         if (info->tty) {
850                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
851                         info->tty->alt_speed = 57600;
852                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
853                         info->tty->alt_speed = 115200;
854                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
855                         info->tty->alt_speed = 230400;
856                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
857                         info->tty->alt_speed = 460800;
858         }
859
860         /*
861          * and set the speed of the serial port
862          */
863         change_speed(info, 0);
864
865         info->flags |= ASYNC_INITIALIZED;
866         restore_flags(flags);
867         return 0;
868
869 errout:
870         restore_flags(flags);
871         return retval;
872 }
873
874 /*
875  * This routine will shutdown a serial port; interrupts are disabled, and
876  * DTR is dropped if the hangup on close termio flag is on.
877  */
878 static void
879 shutdown(struct su_struct *info)
880 {
881         unsigned long   flags;
882
883         if (!(info->flags & ASYNC_INITIALIZED))
884                 return;
885
886         save_flags(flags); cli(); /* Disable interrupts */
887
888         /*
889          * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
890          * here so the queue might never be waken up
891          */
892         wake_up_interruptible(&info->delta_msr_wait);
893         
894         /*
895          * Free the IRQ, if necessary
896          */
897         free_irq(info->irq, info);
898
899         if (info->xmit_buf) {
900                 free_page((unsigned long) info->xmit_buf);
901                 info->xmit_buf = 0;
902         }
903
904         info->IER = 0;
905         serial_outp(info, UART_IER, 0x00);      /* disable all intrs */
906         info->MCR &= ~UART_MCR_OUT2;
907
908         /* disable break condition */
909         serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
910
911         if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
912                 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
913         serial_outp(info, UART_MCR, info->MCR);
914
915         /* disable FIFO's */    
916         serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
917                                      UART_FCR_CLEAR_XMIT));
918         (void)serial_in(info, UART_RX);    /* read data port to reset things */
919
920         if (info->tty)
921                 set_bit(TTY_IO_ERROR, &info->tty->flags);
922
923         if (uart_config[info->type].flags & UART_STARTECH) {
924                 /* Arrange to enter sleep mode */
925                 serial_outp(info, UART_LCR, 0xBF);
926                 serial_outp(info, UART_EFR, UART_EFR_ECB);
927                 serial_outp(info, UART_IER, UART_IERX_SLEEP);
928                 serial_outp(info, UART_LCR, 0);
929         }
930         if (info->type == PORT_16750) {
931                 /* Arrange to enter sleep mode */
932                 serial_outp(info, UART_IER, UART_IERX_SLEEP);
933         }
934         info->flags &= ~ASYNC_INITIALIZED;
935         restore_flags(flags);
936 }
937
938 static int
939 su_get_baud_rate(struct su_struct *info)
940 {
941         static struct tty_struct c_tty;
942         static struct termios c_termios;
943
944         if (info->tty)
945                 return tty_get_baud_rate(info->tty);
946
947         memset(&c_tty, 0, sizeof(c_tty));
948         memset(&c_termios, 0, sizeof(c_termios));
949         c_tty.termios = &c_termios;
950         c_termios.c_cflag = info->cflag;
951
952         return tty_get_baud_rate(&c_tty);
953 }
954
955 /*
956  * This routine is called to set the UART divisor registers to match
957  * the specified baud rate for a serial port.
958  */
959 static void
960 change_speed(struct su_struct *info,
961              struct termios *old_termios)
962 {
963         int             quot = 0, baud;
964         unsigned int    cval, fcr = 0;
965         int             bits;
966         unsigned long   flags;
967
968         if (info->port_type == SU_PORT_PORT) {
969                 if (!info->tty || !info->tty->termios)
970                         return;
971                 if (!info->port)
972                         return;
973                 info->cflag = info->tty->termios->c_cflag;
974         }
975
976         /* byte size and parity */
977         switch (info->cflag & CSIZE) {
978               case CS5: cval = 0x00; bits = 7; break;
979               case CS6: cval = 0x01; bits = 8; break;
980               case CS7: cval = 0x02; bits = 9; break;
981               case CS8: cval = 0x03; bits = 10; break;
982                 /* Never happens, but GCC is too dumb to figure it out */
983               default:  cval = 0x00; bits = 7; break;
984         }
985         if (info->cflag & CSTOPB) {
986                 cval |= 0x04;
987                 bits++;
988         }
989         if (info->cflag & PARENB) {
990                 cval |= UART_LCR_PARITY;
991                 bits++;
992         }
993         if (!(info->cflag & PARODD))
994                 cval |= UART_LCR_EPAR;
995 #ifdef CMSPAR
996         if (info->cflag & CMSPAR)
997                 cval |= UART_LCR_SPAR;
998 #endif
999
1000         /* Determine divisor based on baud rate */
1001         baud = su_get_baud_rate(info);
1002         if (baud == 38400 &&
1003             ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1004                 quot = info->custom_divisor;
1005         else {
1006                 if (baud == 134)
1007                         /* Special case since 134 is really 134.5 */
1008                         quot = (2 * info->baud_base / 269);
1009                 else if (baud)
1010                         quot = info->baud_base / baud;
1011         }
1012         /* If the quotient is zero refuse the change */
1013         if (!quot && old_termios) {
1014                 info->tty->termios->c_cflag &= ~CBAUD;
1015                 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1016                 baud = tty_get_baud_rate(info->tty);
1017                 if (!baud)
1018                         baud = 9600;
1019                 if (baud == 38400 &&
1020                     ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1021                         quot = info->custom_divisor;
1022                 else {
1023                         if (baud == 134)
1024                                 /* Special case since 134 is really 134.5 */
1025                                 quot = (2*info->baud_base / 269);
1026                         else if (baud)
1027                                 quot = info->baud_base / baud;
1028                 }
1029         }
1030         /* As a last resort, if the quotient is zero, default to 9600 bps */
1031         if (!quot)
1032                 quot = info->baud_base / 9600;
1033         info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / info->baud_base);
1034         info->timeout += HZ/50;         /* Add .02 seconds of slop */
1035
1036         /* Set up FIFO's */
1037         if (uart_config[info->type].flags & UART_USE_FIFO) {
1038                 if ((info->baud_base / quot) < 9600)
1039                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1040                 else
1041                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1042         }
1043         if (info->type == PORT_16750)
1044                 fcr |= UART_FCR7_64BYTE;
1045
1046         /* CTS flow control flag and modem status interrupts */
1047         info->IER &= ~UART_IER_MSI;
1048         if (info->flags & ASYNC_HARDPPS_CD)
1049                 info->IER |= UART_IER_MSI;
1050         if (info->cflag & CRTSCTS) {
1051                 info->flags |= ASYNC_CTS_FLOW;
1052                 info->IER |= UART_IER_MSI;
1053         } else
1054                 info->flags &= ~ASYNC_CTS_FLOW;
1055         if (info->cflag & CLOCAL)
1056                 info->flags &= ~ASYNC_CHECK_CD;
1057         else {
1058                 info->flags |= ASYNC_CHECK_CD;
1059                 info->IER |= UART_IER_MSI;
1060         }
1061         serial_out(info, UART_IER, info->IER);
1062
1063         /*
1064          * Set up parity check flag
1065          */
1066         if (info->tty) {
1067 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1068
1069                 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE |
1070                                          UART_LSR_DR;
1071                 if (I_INPCK(info->tty))
1072                         info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1073                 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1074                         info->read_status_mask |= UART_LSR_BI;
1075
1076                 /*
1077                  * Characters to ignore
1078                  */
1079                 info->ignore_status_mask = 0;
1080                 if (I_IGNPAR(info->tty))
1081                         info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1082                 if (I_IGNBRK(info->tty)) {
1083                         info->ignore_status_mask |= UART_LSR_BI;
1084                         /*
1085                          * If we're ignore parity and break indicators, ignore 
1086                          * overruns too.  (For real raw support).
1087                          */
1088                         if (I_IGNPAR(info->tty))
1089                                 info->ignore_status_mask |= UART_LSR_OE;
1090                 }
1091                 /*
1092                  * !!! ignore all characters if CREAD is not set
1093                  */
1094                 if ((info->cflag & CREAD) == 0)
1095                         info->ignore_status_mask |= UART_LSR_DR;
1096         }
1097
1098         save_flags(flags); cli();
1099         if (uart_config[info->type].flags & UART_STARTECH) {
1100                 serial_outp(info, UART_LCR, 0xBF);
1101                 serial_outp(info, UART_EFR,
1102                             (info->cflag & CRTSCTS) ? UART_EFR_CTS : 0);
1103         }
1104         serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);      /* set DLAB */
1105         serial_outp(info, UART_DLL, quot & 0xff);       /* LS of divisor */
1106         serial_outp(info, UART_DLM, quot >> 8);         /* MS of divisor */
1107         if (info->type == PORT_16750)
1108                 serial_outp(info, UART_FCR, fcr);       /* set fcr */
1109         serial_outp(info, UART_LCR, cval);              /* reset DLAB */
1110         if (info->type != PORT_16750)
1111                 serial_outp(info, UART_FCR, fcr);       /* set fcr */
1112         restore_flags(flags);
1113         info->quot = quot;
1114 }
1115
1116 static void
1117 su_put_char(struct tty_struct *tty, unsigned char ch)
1118 {
1119         struct su_struct *info = (struct su_struct *)tty->driver_data;
1120         unsigned long flags;
1121
1122         if (serial_paranoia_check(info, tty->device, "su_put_char"))
1123                 return;
1124
1125         if (!tty || !info->xmit_buf)
1126                 return;
1127
1128         save_flags(flags); cli();
1129         if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1130                 restore_flags(flags);
1131                 return;
1132         }
1133
1134         info->xmit_buf[info->xmit_head++] = ch;
1135         info->xmit_head &= SERIAL_XMIT_SIZE-1;
1136         info->xmit_cnt++;
1137         restore_flags(flags);
1138 }
1139
1140 static void su_put_char_kbd(unsigned char c)
1141 {
1142         struct su_struct *info = su_table;
1143         int lsr;
1144
1145         if (info->port_type != SU_PORT_KBD)
1146                 ++info;
1147         if (info->port_type != SU_PORT_KBD)
1148                 return;
1149
1150         do {
1151                 lsr = serial_in(info, UART_LSR);
1152         } while (!(lsr & UART_LSR_THRE));
1153
1154         /* Send the character out. */
1155         su_outb(info, UART_TX, c);
1156 }
1157
1158 static void
1159 su_change_mouse_baud(int baud)
1160 {
1161         struct su_struct *info = su_table;
1162
1163         if (info->port_type != SU_PORT_MS)
1164                 ++info;
1165         if (info->port_type != SU_PORT_MS)
1166                 return;
1167
1168         info->cflag &= ~CBAUD;
1169         switch (baud) {
1170                 case 1200:
1171                         info->cflag |= B1200;
1172                         break;
1173                 case 2400:
1174                         info->cflag |= B2400;
1175                         break;
1176                 case 4800:
1177                         info->cflag |= B4800;
1178                         break;
1179                 case 9600:
1180                         info->cflag |= B9600;
1181                         break;
1182                 default:
1183                         printk("su_change_mouse_baud: unknown baud rate %d, "
1184                                "defaulting to 1200\n", baud);
1185                         info->cflag |= 1200;
1186                         break;
1187         }
1188         change_speed(info, 0);
1189 }
1190
1191 static void
1192 su_flush_chars(struct tty_struct *tty)
1193 {
1194         struct su_struct *info = (struct su_struct *)tty->driver_data;
1195         unsigned long flags;
1196                                 
1197         if (serial_paranoia_check(info, tty->device, "su_flush_chars"))
1198                 return;
1199
1200         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1201             !info->xmit_buf)
1202                 return;
1203
1204         save_flags(flags); cli();
1205         info->IER |= UART_IER_THRI;
1206         serial_out(info, UART_IER, info->IER);
1207         restore_flags(flags);
1208 }
1209
1210 static int
1211 su_write(struct tty_struct * tty, int from_user,
1212                     const unsigned char *buf, int count)
1213 {
1214         int     c, ret = 0;
1215         struct su_struct *info = (struct su_struct *)tty->driver_data;
1216         unsigned long flags;
1217
1218         if (serial_paranoia_check(info, tty->device, "su_write"))
1219                 return 0;
1220
1221         if (!tty || !info->xmit_buf || !tmp_buf)
1222                 return 0;
1223
1224         save_flags(flags);
1225         if (from_user) {
1226                 down(&tmp_buf_sem);
1227                 while (1) {
1228                         c = MIN(count,
1229                                 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1230                                     SERIAL_XMIT_SIZE - info->xmit_head));
1231                         if (c <= 0)
1232                                 break;
1233
1234                         c -= copy_from_user(tmp_buf, buf, c);
1235                         if (!c) {
1236                                 if (!ret)
1237                                         ret = -EFAULT;
1238                                 break;
1239                         }
1240                         cli();
1241                         c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1242                                        SERIAL_XMIT_SIZE - info->xmit_head));
1243                         memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1244                         info->xmit_head = ((info->xmit_head + c) &
1245                                            (SERIAL_XMIT_SIZE-1));
1246                         info->xmit_cnt += c;
1247                         restore_flags(flags);
1248                         buf += c;
1249                         count -= c;
1250                         ret += c;
1251                 }
1252                 up(&tmp_buf_sem);
1253         } else {
1254                 while (1) {
1255                         cli();          
1256                         c = MIN(count,
1257                                 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1258                                     SERIAL_XMIT_SIZE - info->xmit_head));
1259                         if (c <= 0) {
1260                                 restore_flags(flags);
1261                                 break;
1262                         }
1263                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
1264                         info->xmit_head = ((info->xmit_head + c) &
1265                                            (SERIAL_XMIT_SIZE-1));
1266                         info->xmit_cnt += c;
1267                         restore_flags(flags);
1268                         buf += c;
1269                         count -= c;
1270                         ret += c;
1271                 }
1272         }
1273         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1274             !(info->IER & UART_IER_THRI)) {
1275                 info->IER |= UART_IER_THRI;
1276                 serial_out(info, UART_IER, info->IER);
1277         }
1278         return ret;
1279 }
1280
1281 static int
1282 su_write_room(struct tty_struct *tty)
1283 {
1284         struct su_struct *info = (struct su_struct *)tty->driver_data;
1285         int     ret;
1286
1287         if (serial_paranoia_check(info, tty->device, "su_write_room"))
1288                 return 0;
1289         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1290         if (ret < 0)
1291                 ret = 0;
1292         return ret;
1293 }
1294
1295 static int
1296 su_chars_in_buffer(struct tty_struct *tty)
1297 {
1298         struct su_struct *info = (struct su_struct *)tty->driver_data;
1299                                 
1300         if (serial_paranoia_check(info, tty->device, "su_chars_in_buffer"))
1301                 return 0;
1302         return info->xmit_cnt;
1303 }
1304
1305 static void
1306 su_flush_buffer(struct tty_struct *tty)
1307 {
1308         struct su_struct *info = (struct su_struct *)tty->driver_data;
1309         unsigned long flags;
1310
1311         if (serial_paranoia_check(info, tty->device, "su_flush_buffer"))
1312                 return;
1313         save_flags(flags); cli();
1314         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1315         restore_flags(flags);
1316         wake_up_interruptible(&tty->write_wait);
1317         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1318             tty->ldisc.write_wakeup)
1319                 (tty->ldisc.write_wakeup)(tty);
1320 }
1321
1322 /*
1323  * This function is used to send a high-priority XON/XOFF character to
1324  * the device
1325  */
1326 static void
1327 su_send_xchar(struct tty_struct *tty, char ch)
1328 {
1329         struct su_struct *info = (struct su_struct *)tty->driver_data;
1330
1331         if (serial_paranoia_check(info, tty->device, "su_send_char"))
1332                 return;
1333
1334         if (!(info->flags & ASYNC_INITIALIZED))
1335                 return;
1336
1337         info->x_char = ch;
1338         if (ch) {
1339                 /* Make sure transmit interrupts are on */
1340                 info->IER |= UART_IER_THRI;
1341                 serial_out(info, UART_IER, info->IER);
1342         }
1343 }
1344
1345 /*
1346  * ------------------------------------------------------------
1347  * su_throttle()
1348  * 
1349  * This routine is called by the upper-layer tty layer to signal that
1350  * incoming characters should be throttled.
1351  * ------------------------------------------------------------
1352  */
1353 static void
1354 su_throttle(struct tty_struct * tty)
1355 {
1356         struct su_struct *info = (struct su_struct *)tty->driver_data;
1357         unsigned long flags;
1358 #ifdef SERIAL_DEBUG_THROTTLE
1359         char    buf[64];
1360
1361         printk("throttle %s: %d....\n", tty_name(tty, buf),
1362                tty->ldisc.chars_in_buffer(tty));
1363 #endif
1364
1365         if (serial_paranoia_check(info, tty->device, "su_throttle"))
1366                 return;
1367
1368         if (I_IXOFF(tty))
1369                 su_send_xchar(tty, STOP_CHAR(tty));
1370
1371         if (tty->termios->c_cflag & CRTSCTS)
1372                 info->MCR &= ~UART_MCR_RTS;
1373
1374         save_flags(flags); cli();
1375         serial_out(info, UART_MCR, info->MCR);
1376         restore_flags(flags);
1377 }
1378
1379 static void
1380 su_unthrottle(struct tty_struct * tty)
1381 {
1382         struct su_struct *info = (struct su_struct *)tty->driver_data;
1383         unsigned long flags;
1384 #ifdef SERIAL_DEBUG_THROTTLE
1385         char    buf[64];
1386
1387         printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1388                tty->ldisc.chars_in_buffer(tty));
1389 #endif
1390
1391         if (serial_paranoia_check(info, tty->device, "su_unthrottle"))
1392                 return;
1393
1394         if (I_IXOFF(tty)) {
1395                 if (info->x_char)
1396                         info->x_char = 0;
1397                 else
1398                         su_send_xchar(tty, START_CHAR(tty));
1399         }
1400         if (tty->termios->c_cflag & CRTSCTS)
1401                 info->MCR |= UART_MCR_RTS;
1402         save_flags(flags); cli();
1403         serial_out(info, UART_MCR, info->MCR);
1404         restore_flags(flags);
1405 }
1406
1407 /*
1408  * ------------------------------------------------------------
1409  * su_ioctl() and friends
1410  * ------------------------------------------------------------
1411  */
1412
1413 /*
1414  * get_serial_info - handle TIOCGSERIAL ioctl()
1415  *
1416  * Purpose: Return standard serial struct information about
1417  *          a serial port handled by this driver.
1418  *
1419  * Added:   11-May-2001 Lars Kellogg-Stedman <lars@larsshack.org>
1420  */
1421 static int get_serial_info(struct su_struct * info,
1422                            struct serial_struct * retinfo)
1423 {
1424         struct serial_struct    tmp;
1425
1426         if (!retinfo)
1427                 return -EFAULT;
1428         memset(&tmp, 0, sizeof(tmp));
1429
1430         tmp.type                = info->type;
1431         tmp.line                = info->line;
1432         tmp.port                = info->port;
1433         tmp.irq                 = info->irq;
1434         tmp.flags               = info->flags;
1435         tmp.xmit_fifo_size      = info->xmit_fifo_size;
1436         tmp.baud_base           = info->baud_base;
1437         tmp.close_delay         = info->close_delay;
1438         tmp.closing_wait        = info->closing_wait;
1439         tmp.custom_divisor      = info->custom_divisor;
1440         tmp.hub6                = 0;
1441
1442         if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1443                 return -EFAULT;
1444
1445         return 0;
1446 }
1447
1448 /*
1449  * get_lsr_info - get line status register info
1450  *
1451  * Purpose: Let user call ioctl() to get info when the UART physically
1452  *          is emptied.  On bus types like RS485, the transmitter must
1453  *          release the bus after transmitting. This must be done when
1454  *          the transmit shift register is empty, not be done when the
1455  *          transmit holding register is empty.  This functionality
1456  *          allows an RS485 driver to be written in user space. 
1457  */
1458 static int
1459 get_lsr_info(struct su_struct * info, unsigned int *value)
1460 {
1461         unsigned char status;
1462         unsigned int result;
1463         unsigned long flags;
1464
1465         save_flags(flags); cli();
1466         status = serial_in(info, UART_LSR);
1467         restore_flags(flags);
1468         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1469         return put_user(result,value);
1470 }
1471
1472
1473 static int
1474 get_modem_info(struct su_struct * info, unsigned int *value)
1475 {
1476         unsigned char control, status;
1477         unsigned int result;
1478         unsigned long flags;
1479
1480         control = info->MCR;
1481         save_flags(flags); cli();
1482         status = serial_in(info, UART_MSR);
1483         restore_flags(flags);
1484         result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1485                 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1486 #ifdef TIOCM_OUT1
1487                 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1488                 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1489 #endif
1490                 | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1491                 | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1492                 | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1493                 | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1494         return put_user(result,value);
1495 }
1496
1497 static int
1498 set_modem_info(struct su_struct * info, unsigned int cmd, unsigned int *value)
1499 {
1500         unsigned int arg;
1501         unsigned long flags;
1502
1503         if (get_user(arg, value))
1504                 return -EFAULT;
1505         switch (cmd) {
1506         case TIOCMBIS: 
1507                 if (arg & TIOCM_RTS)
1508                         info->MCR |= UART_MCR_RTS;
1509                 if (arg & TIOCM_DTR)
1510                         info->MCR |= UART_MCR_DTR;
1511 #ifdef TIOCM_OUT1
1512                 if (arg & TIOCM_OUT1)
1513                         info->MCR |= UART_MCR_OUT1;
1514                 if (arg & TIOCM_OUT2)
1515                         info->MCR |= UART_MCR_OUT2;
1516 #endif
1517                 break;
1518         case TIOCMBIC:
1519                 if (arg & TIOCM_RTS)
1520                         info->MCR &= ~UART_MCR_RTS;
1521                 if (arg & TIOCM_DTR)
1522                         info->MCR &= ~UART_MCR_DTR;
1523 #ifdef TIOCM_OUT1
1524                 if (arg & TIOCM_OUT1)
1525                         info->MCR &= ~UART_MCR_OUT1;
1526                 if (arg & TIOCM_OUT2)
1527                         info->MCR &= ~UART_MCR_OUT2;
1528 #endif
1529                 break;
1530         case TIOCMSET:
1531                 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1532 #ifdef TIOCM_OUT1
1533                                             UART_MCR_OUT1 |
1534                                             UART_MCR_OUT2 |
1535 #endif
1536                                             UART_MCR_DTR))
1537                              | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1538 #ifdef TIOCM_OUT1
1539                              | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1540                              | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1541 #endif
1542                              | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1543                 break;
1544         default:
1545                 return -EINVAL;
1546         }
1547         save_flags(flags); cli();
1548         serial_out(info, UART_MCR, info->MCR);
1549         restore_flags(flags);
1550         return 0;
1551 }
1552
1553 /*
1554  * su_break() --- routine which turns the break handling on or off
1555  */
1556 static void
1557 su_break(struct tty_struct *tty, int break_state)
1558 {
1559         struct su_struct * info = (struct su_struct *)tty->driver_data;
1560         unsigned long flags;
1561         
1562         if (serial_paranoia_check(info, tty->device, "su_break"))
1563                 return;
1564
1565         if (!info->port)
1566                 return;
1567         save_flags(flags); cli();
1568         if (break_state == -1)
1569                 serial_out(info, UART_LCR,
1570                            serial_inp(info, UART_LCR) | UART_LCR_SBC);
1571         else
1572                 serial_out(info, UART_LCR,
1573                            serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1574         restore_flags(flags);
1575 }
1576
1577 static int
1578 su_ioctl(struct tty_struct *tty, struct file * file,
1579                     unsigned int cmd, unsigned long arg)
1580 {
1581         struct su_struct * info = (struct su_struct *)tty->driver_data;
1582         struct async_icount cprev, cnow;        /* kernel counter temps */
1583         struct serial_icounter_struct *p_cuser; /* user space */
1584
1585         if (serial_paranoia_check(info, tty->device, "su_ioctl"))
1586                 return -ENODEV;
1587
1588         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1589             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1590             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1591                 if (tty->flags & (1 << TTY_IO_ERROR))
1592                     return -EIO;
1593         }
1594
1595         switch (cmd) {
1596                 case TIOCMGET:
1597                         return get_modem_info(info, (unsigned int *) arg);
1598                 case TIOCMBIS:
1599                 case TIOCMBIC:
1600                 case TIOCMSET:
1601                         return set_modem_info(info, cmd, (unsigned int *) arg);
1602
1603                 case TIOCGSERIAL:
1604                         return get_serial_info(info, (struct serial_struct *)arg);
1605
1606                 case TIOCSERGETLSR: /* Get line status register */
1607                         return get_lsr_info(info, (unsigned int *) arg);
1608
1609 #if 0
1610                 case TIOCSERGSTRUCT:
1611                         if (copy_to_user((struct async_struct *) arg,
1612                                          info, sizeof(struct async_struct)))
1613                                 return -EFAULT;
1614                         return 0;
1615 #endif
1616                                 
1617                 /*
1618                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1619                  * - mask passed in arg for lines of interest
1620                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1621                  * Caller should use TIOCGICOUNT to see which one it was
1622                  */
1623                 case TIOCMIWAIT:
1624                         cli();
1625                         /* note the counters on entry */
1626                         cprev = info->icount;
1627                         sti();
1628                         while (1) {
1629                                 interruptible_sleep_on(&info->delta_msr_wait);
1630                                 /* see if a signal did it */
1631                                 if (signal_pending(current))
1632                                         return -ERESTARTSYS;
1633                                 cli();
1634                                 cnow = info->icount; /* atomic copy */
1635                                 sti();
1636                                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 
1637                                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1638                                         return -EIO; /* no change => error */
1639                                 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1640                                      ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1641                                      ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1642                                      ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1643                                         return 0;
1644                                 }
1645                                 cprev = cnow;
1646                         }
1647                         /* NOTREACHED */
1648
1649                 /* 
1650                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1651                  * Return: write counters to the user passed counter struct
1652                  * NB: both 1->0 and 0->1 transitions are counted except for
1653                  *     RI where only 0->1 is counted.
1654                  */
1655                 case TIOCGICOUNT:
1656                         cli();
1657                         cnow = info->icount;
1658                         sti();
1659                         p_cuser = (struct serial_icounter_struct *) arg;
1660                         if (put_user(cnow.cts, &p_cuser->cts) ||
1661                             put_user(cnow.dsr, &p_cuser->dsr) ||
1662                             put_user(cnow.rng, &p_cuser->rng) ||
1663                             put_user(cnow.dcd, &p_cuser->dcd))
1664                                 return -EFAULT;
1665                         return 0;
1666
1667                 default:
1668                         return -ENOIOCTLCMD;
1669                 }
1670         /* return 0; */ /* Trigger warnings if fall through by a chance. */
1671 }
1672
1673 static void
1674 su_set_termios(struct tty_struct *tty, struct termios *old_termios)
1675 {
1676         struct su_struct *info = (struct su_struct *)tty->driver_data;
1677         unsigned long flags;
1678
1679         if (   (tty->termios->c_cflag == old_termios->c_cflag)
1680             && (   RELEVANT_IFLAG(tty->termios->c_iflag) 
1681                 == RELEVANT_IFLAG(old_termios->c_iflag)))
1682           return;
1683
1684         change_speed(info, old_termios);
1685
1686         /* Handle transition to B0 status */
1687         if ((old_termios->c_cflag & CBAUD) &&
1688             !(tty->termios->c_cflag & CBAUD)) {
1689                 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1690                 save_flags(flags); cli();
1691                 serial_out(info, UART_MCR, info->MCR);
1692                 restore_flags(flags);
1693         }
1694
1695         /* Handle transition away from B0 status */
1696         if (!(old_termios->c_cflag & CBAUD) &&
1697             (tty->termios->c_cflag & CBAUD)) {
1698                 info->MCR |= UART_MCR_DTR;
1699                 if (!(tty->termios->c_cflag & CRTSCTS) || 
1700                     !test_bit(TTY_THROTTLED, &tty->flags)) {
1701                         info->MCR |= UART_MCR_RTS;
1702                 }
1703                 save_flags(flags); cli();
1704                 serial_out(info, UART_MCR, info->MCR);
1705                 restore_flags(flags);
1706         }
1707         
1708         /* Handle turning off CRTSCTS */
1709         if ((old_termios->c_cflag & CRTSCTS) &&
1710             !(tty->termios->c_cflag & CRTSCTS)) {
1711                 tty->hw_stopped = 0;
1712                 su_start(tty);
1713         }
1714
1715 #if 0
1716         /*
1717          * No need to wake up processes in open wait, since they
1718          * sample the CLOCAL flag once, and don't recheck it.
1719          * XXX  It's not clear whether the current behavior is correct
1720          * or not.  Hence, this may change.....
1721          */
1722         if (!(old_termios->c_cflag & CLOCAL) &&
1723             (tty->termios->c_cflag & CLOCAL))
1724                 wake_up_interruptible(&info->open_wait);
1725 #endif
1726 }
1727
1728 /*
1729  * ------------------------------------------------------------
1730  * su_close()
1731  * 
1732  * This routine is called when the serial port gets closed.  First, we
1733  * wait for the last remaining data to be sent.  Then, we unlink its
1734  * async structure from the interrupt chain if necessary, and we free
1735  * that IRQ if nothing is left in the chain.
1736  * ------------------------------------------------------------
1737  */
1738 static void
1739 su_close(struct tty_struct *tty, struct file * filp)
1740 {
1741         struct su_struct *info = (struct su_struct *)tty->driver_data;
1742         unsigned long flags;
1743
1744         if (!info || serial_paranoia_check(info, tty->device, "su_close"))
1745                 return;
1746
1747         save_flags(flags); cli();
1748         
1749         if (tty_hung_up_p(filp)) {
1750                 DBG_CNT("before DEC-hung");
1751                 MOD_DEC_USE_COUNT;
1752                 restore_flags(flags);
1753                 return;
1754         }
1755         
1756 #ifdef SERIAL_DEBUG_OPEN
1757         printk("su_close ttys%d, count = %d\n", info->line, info->count);
1758 #endif
1759         if ((tty->count == 1) && (info->count != 1)) {
1760                 /*
1761                  * Uh, oh.  tty->count is 1, which means that the tty
1762                  * structure will be freed.  info->count should always
1763                  * be one in these conditions.  If it's greater than
1764                  * one, we've got real problems, since it means the
1765                  * serial port won't be shutdown.
1766                  */
1767                 printk("su_close: bad serial port count; tty->count is 1, "
1768                        "info->count is %d\n", info->count);
1769                 info->count = 1;
1770         }
1771         if (--info->count < 0) {
1772                 printk("su_close: bad serial port count for ttys%d: %d\n",
1773                        info->line, info->count);
1774                 info->count = 0;
1775         }
1776         if (info->count) {
1777                 DBG_CNT("before DEC-2");
1778                 MOD_DEC_USE_COUNT;
1779                 restore_flags(flags);
1780                 return;
1781         }
1782         info->flags |= ASYNC_CLOSING;
1783         /*
1784          * Save the termios structure, since this port may have
1785          * separate termios for callout and dialin.
1786          */
1787         if (info->flags & ASYNC_NORMAL_ACTIVE)
1788                 info->normal_termios = *tty->termios;
1789         if (info->flags & ASYNC_CALLOUT_ACTIVE)
1790                 info->callout_termios = *tty->termios;
1791         /*
1792          * Now we wait for the transmit buffer to clear; and we notify 
1793          * the line discipline to only process XON/XOFF characters.
1794          */
1795         tty->closing = 1;
1796         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1797                 tty_wait_until_sent(tty, info->closing_wait);
1798         /*
1799          * At this point we stop accepting input.  To do this, we
1800          * disable the receive line status interrupts, and tell the
1801          * interrupt driver to stop checking the data ready bit in the
1802          * line status register.
1803          */
1804         info->IER &= ~UART_IER_RLSI;
1805         info->read_status_mask &= ~UART_LSR_DR;
1806         if (info->flags & ASYNC_INITIALIZED) {
1807                 serial_out(info, UART_IER, info->IER);
1808                 /*
1809                  * Before we drop DTR, make sure the UART transmitter
1810                  * has completely drained; this is especially
1811                  * important if there is a transmit FIFO!
1812                  */
1813                 su_wait_until_sent(tty, info->timeout);
1814         }
1815         shutdown(info);
1816         if (tty->driver.flush_buffer)
1817                 tty->driver.flush_buffer(tty);
1818         if (tty->ldisc.flush_buffer)
1819                 tty->ldisc.flush_buffer(tty);
1820         tty->closing = 0;
1821         info->event = 0;
1822         info->tty = 0;
1823         if (info->blocked_open) {
1824                 if (info->close_delay) {
1825                         current->state = TASK_INTERRUPTIBLE;
1826                         schedule_timeout(info->close_delay);
1827                 }
1828                 wake_up_interruptible(&info->open_wait);
1829         }
1830         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1831                          ASYNC_CLOSING);
1832         wake_up_interruptible(&info->close_wait);
1833         MOD_DEC_USE_COUNT;
1834         restore_flags(flags);
1835 }
1836
1837 /*
1838  * su_wait_until_sent() --- wait until the transmitter is empty
1839  */
1840 static void
1841 su_wait_until_sent(struct tty_struct *tty, int timeout)
1842 {
1843         struct su_struct * info = (struct su_struct *)tty->driver_data;
1844         unsigned long orig_jiffies, char_time;
1845         int lsr;
1846
1847         if (serial_paranoia_check(info, tty->device, "su_wait_until_sent"))
1848                 return;
1849
1850         if (info->type == PORT_UNKNOWN)
1851                 return;
1852
1853         if (info->xmit_fifo_size == 0)
1854                 return; /* Just in case ... */
1855
1856         orig_jiffies = jiffies;
1857         /*
1858          * Set the check interval to be 1/5 of the estimated time to
1859          * send a single character, and make it at least 1.  The check
1860          * interval should also be less than the timeout.
1861          * 
1862          * Note: we have to use pretty tight timings here to satisfy
1863          * the NIST-PCTS.
1864          */
1865         char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1866         char_time = char_time / 5;
1867         if (char_time == 0)
1868                 char_time = 1;
1869         if (timeout)
1870           char_time = MIN(char_time, timeout);
1871 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1872         printk("In su_wait_until_sent(%d) check=%lu...", timeout, char_time);
1873         printk("jiff=%lu...", jiffies);
1874 #endif
1875         while (!((lsr = serial_inp(info, UART_LSR)) & UART_LSR_TEMT)) {
1876 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1877                 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1878 #endif
1879                 current->state = TASK_INTERRUPTIBLE;
1880                 schedule_timeout(char_time);
1881                 if (signal_pending(current))
1882                         break;
1883                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1884                         break;
1885         }
1886 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1887         printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1888 #endif
1889 }
1890
1891 /*
1892  * su_hangup() --- called by tty_hangup() when a hangup is signaled.
1893  */
1894 static void
1895 su_hangup(struct tty_struct *tty)
1896 {
1897         struct su_struct * info = (struct su_struct *)tty->driver_data;
1898
1899         if (serial_paranoia_check(info, tty->device, "su_hangup"))
1900                 return;
1901
1902         su_flush_buffer(tty);
1903         shutdown(info);
1904         info->event = 0;
1905         info->count = 0;
1906         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1907         info->tty = 0;
1908         wake_up_interruptible(&info->open_wait);
1909 }
1910
1911 /*
1912  * ------------------------------------------------------------
1913  * su_open() and friends
1914  * ------------------------------------------------------------
1915  */
1916 static int
1917 block_til_ready(struct tty_struct *tty, struct file * filp,
1918                 struct su_struct *info)
1919 {
1920         DECLARE_WAITQUEUE(wait, current);
1921         int               retval;
1922         int               do_clocal = 0, extra_count = 0;
1923         unsigned long     flags;
1924
1925         /*
1926          * If the device is in the middle of being closed, then block
1927          * until it's done, and then try again.
1928          */
1929         if (tty_hung_up_p(filp) ||
1930             (info->flags & ASYNC_CLOSING)) {
1931                 if (info->flags & ASYNC_CLOSING)
1932                         interruptible_sleep_on(&info->close_wait);
1933 #ifdef SERIAL_DO_RESTART
1934                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1935                         -EAGAIN : -ERESTARTSYS);
1936 #else
1937                 return -EAGAIN;
1938 #endif
1939         }
1940
1941         /*
1942          * If this is a callout device, then just make sure the normal
1943          * device isn't being used.
1944          */
1945         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1946                 if (info->flags & ASYNC_NORMAL_ACTIVE)
1947                         return -EBUSY;
1948                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1949                     (info->flags & ASYNC_SESSION_LOCKOUT) &&
1950                     (info->session != current->session))
1951                     return -EBUSY;
1952                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1953                     (info->flags & ASYNC_PGRP_LOCKOUT) &&
1954                     (info->pgrp != current->pgrp))
1955                     return -EBUSY;
1956                 info->flags |= ASYNC_CALLOUT_ACTIVE;
1957                 return 0;
1958         }
1959
1960         /*
1961          * If non-blocking mode is set, or the port is not enabled,
1962          * then make the check up front and then exit.
1963          */
1964         if ((filp->f_flags & O_NONBLOCK) ||
1965             (tty->flags & (1 << TTY_IO_ERROR))) {
1966                 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1967                         return -EBUSY;
1968                 info->flags |= ASYNC_NORMAL_ACTIVE;
1969                 return 0;
1970         }
1971
1972         if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1973                 if (info->normal_termios.c_cflag & CLOCAL)
1974                         do_clocal = 1;
1975         } else {
1976                 if (tty->termios->c_cflag & CLOCAL)
1977                         do_clocal = 1;
1978         }
1979         
1980         /*
1981          * Block waiting for the carrier detect and the line to become
1982          * free (i.e., not in use by the callout).  While we are in
1983          * this loop, info->count is dropped by one, so that
1984          * su_close() knows when to free things.  We restore it upon
1985          * exit, either normal or abnormal.
1986          */
1987         retval = 0;
1988         add_wait_queue(&info->open_wait, &wait);
1989 #ifdef SERIAL_DEBUG_OPEN
1990         printk("block_til_ready before block: ttys%d, count = %d\n",
1991                info->line, info->count);
1992 #endif
1993         save_flags(flags); cli();
1994         if (!tty_hung_up_p(filp)) {
1995                 extra_count = 1;
1996                 info->count--;
1997         }
1998         restore_flags(flags);
1999         info->blocked_open++;
2000         while (1) {
2001                 save_flags(flags); cli();
2002                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2003                     (tty->termios->c_cflag & CBAUD))
2004                         serial_out(info, UART_MCR,
2005                                    serial_inp(info, UART_MCR) |
2006                                    (UART_MCR_DTR | UART_MCR_RTS));
2007                 restore_flags(flags);
2008                 set_current_state(TASK_INTERRUPTIBLE);
2009                 if (tty_hung_up_p(filp) ||
2010                     !(info->flags & ASYNC_INITIALIZED)) {
2011 #ifdef SERIAL_DO_RESTART
2012                         if (info->flags & ASYNC_HUP_NOTIFY)
2013                                 retval = -EAGAIN;
2014                         else
2015                                 retval = -ERESTARTSYS;  
2016 #else
2017                         retval = -EAGAIN;
2018 #endif
2019                         break;
2020                 }
2021                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2022                     !(info->flags & ASYNC_CLOSING) &&
2023                     (do_clocal || (serial_in(info, UART_MSR) &
2024                                    UART_MSR_DCD)))
2025                         break;
2026                 if (signal_pending(current)) {
2027                         retval = -ERESTARTSYS;
2028                         break;
2029                 }
2030 #ifdef SERIAL_DEBUG_OPEN
2031                 printk("block_til_ready blocking: ttys%d, count = %d\n",
2032                        info->line, info->count);
2033 #endif
2034                 schedule();
2035         }
2036         current->state = TASK_RUNNING;
2037         remove_wait_queue(&info->open_wait, &wait);
2038         if (extra_count)
2039                 info->count++;
2040         info->blocked_open--;
2041 #ifdef SERIAL_DEBUG_OPEN
2042         printk("block_til_ready after blocking: ttys%d, count = %d\n",
2043                info->line, info->count);
2044 #endif
2045         if (retval)
2046                 return retval;
2047         info->flags |= ASYNC_NORMAL_ACTIVE;
2048         return 0;
2049 }
2050
2051 /*
2052  * This routine is called whenever a serial port is opened.  It
2053  * enables interrupts for a serial port, linking in its async structure into
2054  * the IRQ chain.   It also performs the serial-specific
2055  * initialization for the tty structure.
2056  */
2057 static int
2058 su_open(struct tty_struct *tty, struct file * filp)
2059 {
2060         struct su_struct        *info;
2061         int                     retval, line;
2062         unsigned long           page;
2063
2064         line = MINOR(tty->device) - tty->driver.minor_start;
2065         if ((line < 0) || (line >= NR_PORTS))
2066                 return -ENODEV;
2067         info = su_table + line;
2068         info->count++;
2069         tty->driver_data = info;
2070         info->tty = tty;
2071
2072         if (serial_paranoia_check(info, tty->device, "su_open")) {
2073                 info->count--;
2074                 return -ENODEV;
2075         }
2076
2077 #ifdef SERIAL_DEBUG_OPEN
2078         printk("su_open %s%d, count = %d\n", tty->driver.name, info->line,
2079                info->count);
2080 #endif
2081         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2082
2083         if (!tmp_buf) {
2084                 page = get_free_page(GFP_KERNEL);
2085                 if (!page)
2086                         return -ENOMEM;
2087                 if (tmp_buf)
2088                         free_page(page);
2089                 else
2090                         tmp_buf = (unsigned char *) page;
2091         }
2092
2093         /*
2094          * If the port is the middle of closing, bail out now
2095          */
2096         if (tty_hung_up_p(filp) ||
2097             (info->flags & ASYNC_CLOSING)) {
2098                 if (info->flags & ASYNC_CLOSING)
2099                         interruptible_sleep_on(&info->close_wait);
2100 #ifdef SERIAL_DO_RESTART
2101                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2102                         -EAGAIN : -ERESTARTSYS);
2103 #else
2104                 return -EAGAIN;
2105 #endif
2106         }
2107         
2108         /*
2109          * Start up serial port
2110          */
2111         retval = startup(info);
2112         if (retval)
2113                 return retval;
2114
2115         MOD_INC_USE_COUNT;
2116         retval = block_til_ready(tty, filp, info);
2117         if (retval) {
2118 #ifdef SERIAL_DEBUG_OPEN
2119                 printk("su_open returning after block_til_ready with %d\n",
2120                        retval);
2121 #endif
2122                 return retval;
2123         }
2124
2125         if ((info->count == 1) &&
2126             (info->flags & ASYNC_SPLIT_TERMIOS)) {
2127                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2128                         *tty->termios = info->normal_termios;
2129                 else 
2130                         *tty->termios = info->callout_termios;
2131                 change_speed(info, 0);
2132         }
2133 #ifdef CONFIG_SERIAL_CONSOLE
2134         if (sercons.cflag && sercons.index == line) {
2135                 tty->termios->c_cflag = sercons.cflag;
2136                 sercons.cflag = 0;
2137                 change_speed(info, 0);
2138         }
2139 #endif
2140         info->session = current->session;
2141         info->pgrp = current->pgrp;
2142
2143 #ifdef SERIAL_DEBUG_OPEN
2144         printk("su_open ttys%d successful...", info->line);
2145 #endif
2146         return 0;
2147 }
2148
2149 /*
2150  * /proc fs routines....
2151  */
2152 static int
2153 line_info(char *buf, struct su_struct *info)
2154 {
2155         char            stat_buf[30], control, status;
2156         int             ret;
2157         unsigned long   flags;
2158
2159         if (info->port == 0 || info->type == PORT_UNKNOWN)
2160                 return 0;
2161
2162         ret = sprintf(buf, "%u: uart:%s port:%lX irq:%s",
2163                       info->line, uart_config[info->type].name, 
2164                       (unsigned long)info->port, __irq_itoa(info->irq));
2165
2166         /*
2167          * Figure out the current RS-232 lines
2168          */
2169         save_flags(flags); cli();
2170         status = serial_in(info, UART_MSR);
2171         control = info ? info->MCR : serial_in(info, UART_MCR);
2172         restore_flags(flags);
2173
2174         stat_buf[0] = 0;
2175         stat_buf[1] = 0;
2176         if (control & UART_MCR_RTS)
2177                 strcat(stat_buf, "|RTS");
2178         if (status & UART_MSR_CTS)
2179                 strcat(stat_buf, "|CTS");
2180         if (control & UART_MCR_DTR)
2181                 strcat(stat_buf, "|DTR");
2182         if (status & UART_MSR_DSR)
2183                 strcat(stat_buf, "|DSR");
2184         if (status & UART_MSR_DCD)
2185                 strcat(stat_buf, "|CD");
2186         if (status & UART_MSR_RI)
2187                 strcat(stat_buf, "|RI");
2188
2189         if (info->quot) {
2190                 ret += sprintf(buf+ret, " baud:%u",
2191                                info->baud_base / info->quot);
2192         }
2193
2194         ret += sprintf(buf+ret, " tx:%u rx:%u",
2195                        info->icount.tx, info->icount.rx);
2196
2197         if (info->icount.frame)
2198                 ret += sprintf(buf+ret, " fe:%u", info->icount.frame);
2199
2200         if (info->icount.parity)
2201                 ret += sprintf(buf+ret, " pe:%u", info->icount.parity);
2202
2203         if (info->icount.brk)
2204                 ret += sprintf(buf+ret, " brk:%u", info->icount.brk);   
2205
2206         if (info->icount.overrun)
2207                 ret += sprintf(buf+ret, " oe:%u", info->icount.overrun);
2208
2209         /*
2210          * Last thing is the RS-232 status lines
2211          */
2212         ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2213         return ret;
2214 }
2215
2216 int su_read_proc(char *page, char **start, off_t off, int count,
2217                  int *eof, void *data)
2218 {
2219         int i, len = 0;
2220         off_t   begin = 0;
2221
2222         len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2223         for (i = 0; i < NR_PORTS && len < 4000; i++) {
2224                 len += line_info(page + len, &su_table[i]);
2225                 if (len+begin > off+count)
2226                         goto done;
2227                 if (len+begin < off) {
2228                         begin += len;
2229                         len = 0;
2230                 }
2231         }
2232         *eof = 1;
2233 done:
2234         if (off >= len+begin)
2235                 return 0;
2236         *start = page + (off-begin);
2237         return ((count < begin+len-off) ? count : begin+len-off);
2238 }
2239
2240 /*
2241  * ---------------------------------------------------------------------
2242  * su_XXX_init() and friends
2243  *
2244  * su_XXX_init() is called at boot-time to initialize the serial driver.
2245  * ---------------------------------------------------------------------
2246  */
2247
2248 /*
2249  * This routine prints out the appropriate serial driver version
2250  * number, and identifies which options were configured into this
2251  * driver.
2252  */
2253 static __inline__ void __init show_su_version(void)
2254 {
2255         char *revision = "$Revision: 1.1.1.1 $";
2256         char *version, *p;
2257
2258         version = strchr(revision, ' ');
2259         strcpy(serial_version, ++version);
2260         p = strchr(serial_version, ' ');
2261         *p = '\0';
2262         printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2263 }
2264
2265 /*
2266  * This routine is called by su_{serial|kbd_ms}_init() to initialize a specific
2267  * serial port.  It determines what type of UART chip this serial port is
2268  * using: 8250, 16450, 16550, 16550A.  The important question is
2269  * whether or not this UART is a 16550A, since this will determine
2270  * whether or not we can use its FIFO features.
2271  */
2272 static void
2273 autoconfig(struct su_struct *info)
2274 {
2275         unsigned char status1, status2, scratch, scratch2;
2276         struct linux_ebus_device *dev = 0;
2277         struct linux_ebus *ebus;
2278 #ifdef CONFIG_SPARC64
2279         struct isa_bridge *isa_br;
2280         struct isa_device *isa_dev;
2281 #endif
2282 #ifndef __sparc_v9__
2283         struct linux_prom_registers reg0;
2284 #endif
2285         unsigned long flags;
2286
2287         if (!info->port_node || !info->port_type)
2288                 return;
2289
2290         /*
2291          * First we look for Ebus-bases su's
2292          */
2293         for_each_ebus(ebus) {
2294                 for_each_ebusdev(dev, ebus) {
2295                         if (dev->prom_node == info->port_node) {
2296                                 info->port = dev->resource[0].start;
2297                                 info->irq = dev->irqs[0];
2298                                 goto ebus_done;
2299                         }
2300                 }
2301         }
2302
2303 #ifdef CONFIG_SPARC64
2304         for_each_isa(isa_br) {
2305                 for_each_isadev(isa_dev, isa_br) {
2306                         if (isa_dev->prom_node == info->port_node) {
2307                                 info->port = isa_dev->resource.start;
2308                                 info->irq = isa_dev->irq;
2309                                 goto ebus_done;
2310                         }
2311                 }
2312         }
2313 #endif
2314
2315 #ifdef __sparc_v9__
2316         /*
2317          * Not on Ebus, bailing.
2318          */
2319         return;
2320 #else
2321         /*
2322          * Not on Ebus, must be OBIO.
2323          */
2324         if (prom_getproperty(info->port_node, "reg",
2325             (char *)&reg0, sizeof(reg0)) == -1) {
2326                 prom_printf("su: no \"reg\" property\n");
2327                 return;
2328         }
2329         prom_apply_obio_ranges(&reg0, 1);
2330         if (reg0.which_io != 0) {       /* Just in case... */
2331                 prom_printf("su: bus number nonzero: 0x%x:%x\n",
2332                     reg0.which_io, reg0.phys_addr);
2333                 return;
2334         }
2335         if ((info->port = (unsigned long) ioremap(reg0.phys_addr,
2336             reg0.reg_size)) == 0) {
2337                 prom_printf("su: cannot map\n");
2338                 return;
2339         }
2340
2341         /*
2342          * There is no intr property on MrCoffee, so hardwire it.
2343          */
2344         info->irq = IRQ_4M(13);
2345 #endif
2346
2347 ebus_done:
2348
2349 #ifdef SERIAL_DEBUG_OPEN
2350         printk("Found 'su' at %016lx IRQ %s\n", info->port,
2351                 __irq_itoa(info->irq));
2352 #endif
2353
2354         info->magic = SERIAL_MAGIC;
2355
2356         save_flags(flags); cli();
2357
2358         /*
2359          * Do a simple existence test first; if we fail this, there's
2360          * no point trying anything else.
2361          *
2362          * 0x80 is used as a nonsense port to prevent against false
2363          * positives due to ISA bus float.  The assumption is that
2364          * 0x80 is a non-existent port; which should be safe since
2365          * include/asm/io.h also makes this assumption.
2366          */
2367         scratch = serial_inp(info, UART_IER);
2368         serial_outp(info, UART_IER, 0);
2369         scratch2 = serial_inp(info, UART_IER);
2370         serial_outp(info, UART_IER, scratch);
2371         if (scratch2) {
2372                 restore_flags(flags);
2373                 return;         /* We failed; there's nothing here */
2374         }
2375
2376         scratch = serial_inp(info, UART_MCR);
2377         serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2378         serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2379         status1 = serial_inp(info, UART_MSR) & 0xF0;
2380         serial_outp(info, UART_MCR, scratch);
2381         if (status1 != 0x90) {
2382                 /*
2383                  * This code fragment used to fail, now it fixed itself.
2384                  * We keep the printout for a case.
2385                  */
2386                 printk("su: loopback returned status 0x%02x\n", status1);
2387                 restore_flags(flags);
2388                 return;
2389         } 
2390
2391         scratch2 = serial_in(info, UART_LCR);
2392         serial_outp(info, UART_LCR, 0xBF);      /* set up for StarTech test */
2393         serial_outp(info, UART_EFR, 0);         /* EFR is the same as FCR */
2394         serial_outp(info, UART_LCR, 0);
2395         serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2396         scratch = serial_in(info, UART_IIR) >> 6;
2397         switch (scratch) {
2398                 case 0:
2399                         info->type = PORT_16450;
2400                         break;
2401                 case 1:
2402                         info->type = PORT_UNKNOWN;
2403                         break;
2404                 case 2:
2405                         info->type = PORT_16550;
2406                         break;
2407                 case 3:
2408                         info->type = PORT_16550A;
2409                         break;
2410         }
2411         if (info->type == PORT_16550A) {
2412                 /* Check for Startech UART's */
2413                 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2414                 if (serial_in(info, UART_EFR) == 0) {
2415                         info->type = PORT_16650;
2416                 } else {
2417                         serial_outp(info, UART_LCR, 0xBF);
2418                         if (serial_in(info, UART_EFR) == 0)
2419                                 info->type = PORT_16650V2;
2420                 }
2421         }
2422         if (info->type == PORT_16550A) {
2423                 /* Check for TI 16750 */
2424                 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2425                 serial_outp(info, UART_FCR,
2426                             UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
2427                 scratch = serial_in(info, UART_IIR) >> 5;
2428                 if (scratch == 7) {
2429                         serial_outp(info, UART_LCR, 0);
2430                         serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2431                         scratch = serial_in(info, UART_IIR) >> 5;
2432                         if (scratch == 6)
2433                                 info->type = PORT_16750;
2434                 }
2435                 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2436         }
2437         serial_outp(info, UART_LCR, scratch2);
2438         if (info->type == PORT_16450) {
2439                 scratch = serial_in(info, UART_SCR);
2440                 serial_outp(info, UART_SCR, 0xa5);
2441                 status1 = serial_in(info, UART_SCR);
2442                 serial_outp(info, UART_SCR, 0x5a);
2443                 status2 = serial_in(info, UART_SCR);
2444                 serial_outp(info, UART_SCR, scratch);
2445
2446                 if ((status1 != 0xa5) || (status2 != 0x5a))
2447                         info->type = PORT_8250;
2448         }
2449         info->xmit_fifo_size = uart_config[info->type].dfl_xmit_fifo_size;
2450
2451         if (info->type == PORT_UNKNOWN) {
2452                 restore_flags(flags);
2453                 return;
2454         }
2455
2456         sprintf(info->name, "su(%s)", su_typev[info->port_type]);
2457
2458         /*
2459          * Reset the UART.
2460          */
2461         serial_outp(info, UART_MCR, 0x00);
2462         serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT));
2463         (void)serial_in(info, UART_RX);
2464         serial_outp(info, UART_IER, 0x00);
2465
2466         restore_flags(flags);
2467 }
2468
2469 /* This is used by the SAB driver to adjust where its minor
2470  * numbers start, we always are probed for first.
2471  */
2472 int su_num_ports = 0;
2473 EXPORT_SYMBOL(su_num_ports);
2474
2475 /*
2476  * The serial driver boot-time initialization code!
2477  */
2478 int __init su_serial_init(void)
2479 {
2480         int i;
2481         struct su_struct *info;
2482
2483         init_bh(SERIAL_BH, do_serial_bh);
2484         show_su_version();
2485
2486         /* Initialize the tty_driver structure */
2487
2488         memset(&serial_driver, 0, sizeof(struct tty_driver));
2489         serial_driver.magic = TTY_DRIVER_MAGIC;
2490         serial_driver.driver_name = "su";
2491 #ifdef CONFIG_DEVFS_FS
2492         serial_driver.name = "tts/%d";
2493 #else
2494         serial_driver.name = "ttyS";
2495 #endif
2496         serial_driver.major = TTY_MAJOR;
2497         serial_driver.minor_start = 64;
2498         serial_driver.num = NR_PORTS;
2499         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2500         serial_driver.subtype = SERIAL_TYPE_NORMAL;
2501         serial_driver.init_termios = tty_std_termios;
2502         serial_driver.init_termios.c_cflag =
2503                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2504         serial_driver.flags = TTY_DRIVER_REAL_RAW;
2505         serial_driver.refcount = &serial_refcount;
2506         serial_driver.table = serial_table;
2507         serial_driver.termios = serial_termios;
2508         serial_driver.termios_locked = serial_termios_locked;
2509
2510         serial_driver.open = su_open;
2511         serial_driver.close = su_close;
2512         serial_driver.write = su_write;
2513         serial_driver.put_char = su_put_char;
2514         serial_driver.flush_chars = su_flush_chars;
2515         serial_driver.write_room = su_write_room;
2516         serial_driver.chars_in_buffer = su_chars_in_buffer;
2517         serial_driver.flush_buffer = su_flush_buffer;
2518         serial_driver.ioctl = su_ioctl;
2519         serial_driver.throttle = su_throttle;
2520         serial_driver.unthrottle = su_unthrottle;
2521         serial_driver.send_xchar = su_send_xchar;
2522         serial_driver.set_termios = su_set_termios;
2523         serial_driver.stop = su_stop;
2524         serial_driver.start = su_start;
2525         serial_driver.hangup = su_hangup;
2526         serial_driver.break_ctl = su_break;
2527         serial_driver.wait_until_sent = su_wait_until_sent;
2528         serial_driver.read_proc = su_read_proc;
2529
2530         /*
2531          * The callout device is just like normal device except for
2532          * major number and the subtype code.
2533          */
2534         callout_driver = serial_driver;
2535 #ifdef CONFIG_DEVFS_FS
2536         callout_driver.name = "cua/%d";
2537 #else
2538         callout_driver.name = "cua";
2539 #endif
2540         callout_driver.major = TTYAUX_MAJOR;
2541         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2542         callout_driver.read_proc = 0;
2543         callout_driver.proc_entry = 0;
2544
2545         if (tty_register_driver(&serial_driver))
2546                 panic("Couldn't register regular su\n");
2547         if (tty_register_driver(&callout_driver))
2548                 panic("Couldn't register callout su\n");
2549
2550         for (i = 0, info = su_table; i < NR_PORTS; i++, info++) {
2551                 info->line = i;
2552                 info->type = PORT_UNKNOWN;
2553                 info->baud_base = BAUD_BASE;
2554                 /* info->flags = 0; */
2555                 info->custom_divisor = 0;
2556                 info->close_delay = 5*HZ/10;
2557                 info->closing_wait = 30*HZ;
2558                 info->callout_termios = callout_driver.init_termios;
2559                 info->normal_termios = serial_driver.init_termios;
2560                 info->icount.cts = info->icount.dsr = 
2561                         info->icount.rng = info->icount.dcd = 0;
2562                 info->icount.rx = info->icount.tx = 0;
2563                 info->icount.frame = info->icount.parity = 0;
2564                 info->icount.overrun = info->icount.brk = 0;
2565                 info->tqueue.routine = do_softint;
2566                 info->tqueue.data = info;
2567                 info->cflag = serial_driver.init_termios.c_cflag;
2568                 init_waitqueue_head(&info->open_wait);
2569                 init_waitqueue_head(&info->close_wait);
2570                 init_waitqueue_head(&info->delta_msr_wait);
2571
2572                 autoconfig(info);
2573                 if (info->type == PORT_UNKNOWN)
2574                         continue;
2575
2576                 printk(KERN_INFO "%s at 0x%lx (tty %d irq %s) is a %s\n",
2577                        info->name, (long)info->port, i, __irq_itoa(info->irq),
2578                        uart_config[info->type].name);
2579         }
2580
2581         for (i = 0, info = su_table; i < NR_PORTS; i++, info++)
2582                 if (info->type == PORT_UNKNOWN)
2583                         break;
2584
2585         su_num_ports = i;
2586         serial_driver.num = callout_driver.num = i;
2587
2588         return 0;
2589 }
2590
2591 int __init su_kbd_ms_init(void)
2592 {
2593         int i;
2594         struct su_struct *info;
2595
2596         show_su_version();
2597
2598         for (i = 0, info = su_table; i < 2; i++, info++) {
2599                 info->line = i;
2600                 info->type = PORT_UNKNOWN;
2601                 info->baud_base = BAUD_BASE;
2602
2603                 if (info->port_type == SU_PORT_KBD)
2604                         info->cflag = B1200 | CS8 | CLOCAL | CREAD;
2605                 else
2606                         info->cflag = B4800 | CS8 | CLOCAL | CREAD;
2607
2608                 init_waitqueue_head(&info->open_wait);
2609                 init_waitqueue_head(&info->close_wait);
2610                 init_waitqueue_head(&info->delta_msr_wait);
2611
2612                 autoconfig(info);
2613                 if (info->type == PORT_UNKNOWN)
2614                         continue;
2615
2616                 printk(KERN_INFO "%s at 0x%lx (irq = %s) is a %s\n",
2617                        info->name, info->port, __irq_itoa(info->irq),
2618                        uart_config[info->type].name);
2619
2620                 startup(info);
2621                 if (info->port_type == SU_PORT_KBD)
2622                         keyboard_zsinit(su_put_char_kbd);
2623                 else
2624                         sun_mouse_zsinit();
2625         }
2626         return 0;
2627 }
2628
2629 static int su_node_ok(int node, char *name, int namelen)
2630 {
2631         if (strncmp(name, "su", namelen) == 0 ||
2632             strncmp(name, "su_pnp", namelen) == 0)
2633                 return 1;
2634
2635         if (strncmp(name, "serial", namelen) == 0) {
2636                 char compat[32];
2637                 int clen;
2638
2639                 /* Is it _really_ a 'su' device? */
2640                 clen = prom_getproperty(node, "compatible", compat, sizeof(compat));
2641                 if (clen > 0) {
2642                         if (strncmp(compat, "sab82532", 8) == 0) {
2643                                 /* Nope, Siemens serial, not for us. */
2644                                 return 0;
2645                         }
2646                 }
2647                 return 1;
2648         }
2649
2650         return 0;
2651 }
2652
2653 /*
2654  * We got several platforms which present 'su' in different parts
2655  * of device tree. 'su' may be found under obio, ebus, isa and pci.
2656  * We walk over the tree and find them wherever PROM hides them.
2657  */
2658 void __init su_probe_any(struct su_probe_scan *t, int sunode)
2659 {
2660         struct su_struct *info;
2661         int len;
2662
2663         if (t->devices >= NR_PORTS) return;
2664
2665         for (; sunode != 0; sunode = prom_getsibling(sunode)) {
2666                 len = prom_getproperty(sunode, "name", t->prop, SU_PROPSIZE);
2667                 if (len <= 1) continue;         /* Broken PROM node */
2668                 if (su_node_ok(sunode, t->prop, len)) {
2669                         info = &su_table[t->devices];
2670                         if (t->kbnode != 0 && sunode == t->kbnode) {
2671                                 t->kbx = t->devices;
2672                                 info->port_type = SU_PORT_KBD;
2673                         } else if (t->msnode != 0 && sunode == t->msnode) {
2674                                 t->msx = t->devices;
2675                                 info->port_type = SU_PORT_MS;
2676                         } else {
2677 #ifdef __sparc_v9__
2678                                 /*
2679                                  * Do not attempt to use the truncated
2680                                  * keyboard/mouse ports as serial ports
2681                                  * on Ultras with PC keyboard attached.
2682                                  */
2683                                 if (prom_getbool(sunode, "mouse"))
2684                                         continue;
2685                                 if (prom_getbool(sunode, "keyboard"))
2686                                         continue;
2687 #endif
2688                                 info->port_type = SU_PORT_PORT;
2689                         }
2690                         info->is_console = 0;
2691                         info->port_node = sunode;
2692                         ++t->devices;
2693                 } else {
2694                         su_probe_any(t, prom_getchild(sunode));
2695                 }
2696         }
2697 }
2698
2699 int __init su_probe(void)
2700 {
2701         int node;
2702         int len;
2703         struct su_probe_scan scan;
2704
2705         /*
2706          * First, we scan the tree.
2707          */
2708         scan.devices = 0;
2709         scan.msx = -1;
2710         scan.kbx = -1;
2711         scan.kbnode = 0;
2712         scan.msnode = 0;
2713
2714         /*
2715          * Get the nodes for keyboard and mouse from 'aliases'...
2716          */
2717         node = prom_getchild(prom_root_node);
2718         node = prom_searchsiblings(node, "aliases");
2719         if (node != 0) {
2720
2721                 len = prom_getproperty(node, "keyboard", scan.prop,SU_PROPSIZE);
2722                 if (len > 0) {
2723                         scan.prop[len] = 0;
2724                         scan.kbnode = prom_finddevice(scan.prop);
2725                 }
2726
2727                 len = prom_getproperty(node, "mouse", scan.prop, SU_PROPSIZE);
2728                 if (len > 0) {
2729                         scan.prop[len] = 0;
2730                         scan.msnode = prom_finddevice(scan.prop);
2731                 }
2732         }
2733
2734         su_probe_any(&scan, prom_getchild(prom_root_node));
2735
2736         /*
2737          * Second, we process the special case of keyboard and mouse.
2738          *
2739          * Currently if we got keyboard and mouse hooked to "su" ports
2740          * we do not use any possible remaining "su" as a serial port.
2741          * Thus, we ignore values of .msx and .kbx, then compact ports.
2742          * Those who want to address this issue need to merge
2743          * su_serial_init() and su_ms_kbd_init().
2744          */
2745         if (scan.msx != -1 && scan.kbx != -1) {
2746                 su_table[0].port_type = SU_PORT_MS;
2747                 su_table[0].is_console = 0;
2748                 su_table[0].port_node = scan.msnode;
2749                 su_table[1].port_type = SU_PORT_KBD;
2750                 su_table[1].is_console = 0;
2751                 su_table[1].port_node = scan.kbnode;
2752
2753                 sunserial_setinitfunc(su_kbd_ms_init);
2754                 rs_ops.rs_change_mouse_baud = su_change_mouse_baud;
2755                 sunkbd_setinitfunc(sun_kbd_init);
2756                 kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2757                 kbd_ops.setledstate = sun_setledstate;
2758                 kbd_ops.getledstate = sun_getledstate;
2759                 kbd_ops.setkeycode = sun_setkeycode;
2760                 kbd_ops.getkeycode = sun_getkeycode;
2761 #ifdef CONFIG_PCI
2762                 sunkbd_install_keymaps(sun_key_maps,
2763                     sun_keymap_count, sun_func_buf, sun_func_table,
2764                     sun_funcbufsize, sun_funcbufleft,
2765                     sun_accent_table, sun_accent_table_size);
2766 #endif
2767                 return 0;
2768         }
2769         if (scan.msx != -1 || scan.kbx != -1) {
2770                 printk("su_probe: cannot match keyboard and mouse, confused\n");
2771                 return -ENODEV;
2772         }
2773
2774         if (scan.devices == 0)
2775                 return -ENODEV;
2776
2777 #ifdef CONFIG_SERIAL_CONSOLE
2778         /*
2779          * Console must be initiated after the generic initialization.
2780          * sunserial_setinitfunc inverts order, so call this before next one.
2781          */
2782         sunserial_setinitfunc(su_serial_console_init);
2783 #endif
2784         sunserial_setinitfunc(su_serial_init);
2785         return 0;
2786 }
2787
2788 /*
2789  * ------------------------------------------------------------
2790  * Serial console driver
2791  * ------------------------------------------------------------
2792  */
2793 #ifdef CONFIG_SERIAL_CONSOLE
2794
2795 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2796
2797 /*
2798  *      Wait for transmitter & holding register to empty
2799  */
2800 static __inline__ void
2801 wait_for_xmitr(struct su_struct *info)
2802 {
2803         int lsr;
2804         unsigned int tmout = 1000000;
2805
2806         do {
2807                 lsr = su_inb(info, UART_LSR);
2808                 if (--tmout == 0)
2809                         break;
2810         } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY);
2811 }
2812
2813 /*
2814  *      Print a string to the serial port trying not to disturb
2815  *      any possible real use of the port...
2816  */
2817 static void
2818 serial_console_write(struct console *co, const char *s,
2819                                 unsigned count)
2820 {
2821         struct su_struct *info;
2822         int ier;
2823         unsigned i;
2824
2825         info = su_table + co->index;
2826         /*
2827          *      First save the IER then disable the interrupts
2828          */
2829         ier = su_inb(info, UART_IER);
2830         su_outb(info, UART_IER, 0x00);
2831
2832         /*
2833          *      Now, do each character
2834          */
2835         for (i = 0; i < count; i++, s++) {
2836                 wait_for_xmitr(info);
2837
2838                 /*
2839                  *      Send the character out.
2840                  *      If a LF, also do CR...
2841                  */
2842                 su_outb(info, UART_TX, *s);
2843                 if (*s == 10) {
2844                         wait_for_xmitr(info);
2845                         su_outb(info, UART_TX, 13);
2846                 }
2847         }
2848
2849         /*
2850          *      Finally, Wait for transmitter & holding register to empty
2851          *      and restore the IER
2852          */
2853         wait_for_xmitr(info);
2854         su_outb(info, UART_IER, ier);
2855 }
2856
2857 static kdev_t
2858 serial_console_device(struct console *c)
2859 {
2860         return MKDEV(TTY_MAJOR, 64 + c->index);
2861 }
2862
2863 /*
2864  *      Setup initial baud/bits/parity. We do two things here:
2865  *      - construct a cflag setting for the first su_open()
2866  *      - initialize the serial port
2867  *      Return non-zero if we didn't find a serial port.
2868  */
2869 static int __init serial_console_setup(struct console *co, char *options)
2870 {
2871         struct su_struct *info;
2872         unsigned cval;
2873         int     baud = 9600;
2874         int     bits = 8;
2875         int     parity = 'n';
2876         int     cflag = CREAD | HUPCL | CLOCAL;
2877         int     quot = 0;
2878         char    *s;
2879
2880         if (options) {
2881                 baud = simple_strtoul(options, NULL, 10);
2882                 s = options;
2883                 while (*s >= '0' && *s <= '9')
2884                         s++;
2885                 if (*s) parity = *s++;
2886                 if (*s) bits   = *s - '0';
2887         }
2888
2889         /*
2890          *      Now construct a cflag setting.
2891          */
2892         switch (baud) {
2893                 case 1200:
2894                         cflag |= B1200;
2895                         break;
2896                 case 2400:
2897                         cflag |= B2400;
2898                         break;
2899                 case 4800:
2900                         cflag |= B4800;
2901                         break;
2902                 case 19200:
2903                         cflag |= B19200;
2904                         break;
2905                 case 38400:
2906                         cflag |= B38400;
2907                         break;
2908                 case 57600:
2909                         cflag |= B57600;
2910                         break;
2911                 case 115200:
2912                         cflag |= B115200;
2913                         break;
2914                 case 9600:
2915                 default:
2916                         cflag |= B9600;
2917                         baud = 9600;
2918                         break;
2919         }
2920         switch (bits) {
2921                 case 7:
2922                         cflag |= CS7;
2923                         break;
2924                 default:
2925                 case 8:
2926                         cflag |= CS8;
2927                         break;
2928         }
2929         switch (parity) {
2930                 case 'o': case 'O':
2931                         cflag |= PARODD;
2932                         break;
2933                 case 'e': case 'E':
2934                         cflag |= PARENB;
2935                         break;
2936         }
2937         co->cflag = cflag;
2938
2939         /*
2940          *      Divisor, bytesize and parity
2941          */
2942         info = su_table + co->index;
2943         quot = BAUD_BASE / baud;
2944         cval = cflag & (CSIZE | CSTOPB);
2945 #if defined(__powerpc__) || defined(__alpha__)
2946         cval >>= 8;
2947 #else /* !__powerpc__ && !__alpha__ */
2948         cval >>= 4;
2949 #endif /* !__powerpc__ && !__alpha__ */
2950         if (cflag & PARENB)
2951                 cval |= UART_LCR_PARITY;
2952         if (!(cflag & PARODD))
2953                 cval |= UART_LCR_EPAR;
2954
2955         /*
2956          *      Disable UART interrupts, set DTR and RTS high
2957          *      and set speed.
2958          */
2959         su_outb(info, UART_IER, 0);
2960         su_outb(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2961         su_outb(info, UART_LCR, cval | UART_LCR_DLAB);  /* set DLAB */
2962         su_outb(info, UART_DLL, quot & 0xff);           /* LS of divisor */
2963         su_outb(info, UART_DLM, quot >> 8);             /* MS of divisor */
2964         su_outb(info, UART_LCR, cval);                  /* reset DLAB */
2965         info->quot = quot;
2966
2967         /*
2968          *      If we read 0xff from the LSR, there is no UART here.
2969          */
2970         if (su_inb(info, UART_LSR) == 0xff)
2971                 return -1;
2972
2973         info->is_console = 1;
2974
2975         return 0;
2976 }
2977
2978 static struct console sercons = {
2979         name:           "ttyS",
2980         write:          serial_console_write,
2981         device:         serial_console_device,
2982         setup:          serial_console_setup,
2983         flags:          CON_PRINTBUFFER,
2984         index:          -1,
2985 };
2986
2987 int su_console_registered = 0;
2988
2989 /*
2990  *      Register console.
2991  */
2992 int __init su_serial_console_init(void)
2993 {
2994         extern int con_is_present(void);
2995         int index;
2996
2997         if (con_is_present())
2998                 return 0;
2999         if (serial_console == 0)
3000                 return 0;
3001         index = serial_console - 1;
3002         if (su_table[index].port == 0 || su_table[index].port_node == 0)
3003                 return 0;
3004         sercons.index = index;
3005         register_console(&sercons);
3006         su_console_registered = 1;
3007         return 0;
3008 }
3009
3010 #endif /* CONFIG_SERIAL_CONSOLE */