import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / sbus / char / sab82532.c
1 /* $Id: sab82532.c,v 1.1.1.1 2005/04/11 02:50:34 jack Exp $
2  * sab82532.c: ASYNC Driver for the SIEMENS SAB82532 DUSCC.
3  *
4  * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be)
5  *
6  * Rewrote buffer handling to use CIRC(Circular Buffer) macros.
7  *   Maxim Krasnyanskiy <maxk@qualcomm.com>
8  *
9  * Fixed to use tty_get_baud_rate, and to allow for arbitrary baud
10  * rates to be programmed into the UART.  Also eliminated a lot of
11  * duplicated code in the console setup.
12  *   Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
13  */
14
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/signal.h>
19 #include <linux/sched.h>
20 #include <linux/timer.h>
21 #include <linux/interrupt.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
24 #include <linux/serial.h>
25 #include <linux/serialP.h>
26 #include <linux/serial_reg.h>
27 #include <linux/console.h>
28 #include <linux/major.h>
29 #include <linux/string.h>
30 #include <linux/fcntl.h>
31 #include <linux/ptrace.h>
32 #include <linux/ioport.h>
33 #include <linux/mm.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37
38 #include <asm/sab82532.h>
39 #include <asm/uaccess.h>
40 #include <asm/ebus.h>
41 #include <asm/irq.h>
42
43 #include "sunserial.h"
44
45 static DECLARE_TASK_QUEUE(tq_serial);
46
47 /* This is (one of many) a special gross hack to allow SU and
48  * SAB serials to co-exist on the same machine. -DaveM
49  */
50 #undef SERIAL_BH
51 #define SERIAL_BH       AURORA_BH
52
53 static struct tty_driver serial_driver, callout_driver;
54 static int sab82532_refcount;
55
56 /* number of characters left in xmit buffer before we ask for more */
57 #define WAKEUP_CHARS 256
58
59 #undef SERIAL_PARANOIA_CHECK
60 #define SERIAL_DO_RESTART
61
62 /* Set of debugging defines */
63 #undef SERIAL_DEBUG_OPEN
64 #undef SERIAL_DEBUG_FLOW
65 #undef SERIAL_DEBUG_MODEM
66 #undef SERIAL_DEBUG_WAIT_UNTIL_SENT
67 #undef SERIAL_DEBUG_SEND_BREAK
68 #undef SERIAL_DEBUG_INTR
69 #undef SERIAL_DEBUG_FIFO
70 #define SERIAL_DEBUG_OVERFLOW 1
71
72 /* Trace things on serial device, useful for console debugging: */
73 #undef SERIAL_LOG_DEVICE
74
75 #ifdef SERIAL_LOG_DEVICE
76 static void dprint_init(int tty);
77 #endif
78
79 static void change_speed(struct sab82532 *info);
80 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout);
81
82 /*
83  * This assumes you have a 29.4912 MHz clock for your UART.
84  */
85 #define BASE_BAUD ( 29491200 / 16 )
86
87 static struct sab82532 *sab82532_chain = 0;
88 static struct tty_struct *sab82532_table[NR_PORTS];
89 static struct termios *sab82532_termios[NR_PORTS];
90 static struct termios *sab82532_termios_locked[NR_PORTS];
91
92 #ifdef MODULE
93 #undef CONFIG_SERIAL_CONSOLE
94 #endif
95
96 #ifdef CONFIG_SERIAL_CONSOLE
97 extern int serial_console;
98 static struct console sab82532_console;
99 static int sab82532_console_init(void);
100 static void batten_down_hatches(struct sab82532 *info);
101 #endif
102
103 #ifndef MIN
104 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
105 #endif
106
107 static char *sab82532_version[16] = {
108         "V1.0", "V2.0", "V3.2", "V(0x03)",
109         "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
110         "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
111         "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
112 };
113 static char serial_version[16];
114
115 /*
116  * tmp_buf is used as a temporary buffer by sab82532_write.  We need to
117  * lock it in case the copy_from_user blocks while swapping in a page,
118  * and some other program tries to do a serial write at the same time.
119  * Since the lock will only come under contention when the system is
120  * swapping and available memory is low, it makes sense to share one
121  * buffer across all the serial ports, since it significantly saves
122  * memory if large numbers of serial ports are open.
123  */
124 static unsigned char *tmp_buf = 0;
125 static DECLARE_MUTEX(tmp_buf_sem);
126
127 static inline int serial_paranoia_check(struct sab82532 *info,
128                                         kdev_t device, const char *routine)
129 {
130 #ifdef SERIAL_PARANOIA_CHECK
131         static const char *badmagic =
132                 "Warning: bad magic number for serial struct (%s) in %s\n";
133         static const char *badinfo =
134                 "Warning: null sab82532 for (%s) in %s\n";
135
136         if (!info) {
137                 printk(badinfo, kdevname(device), routine);
138                 return 1;
139         }
140         if (info->magic != SERIAL_MAGIC) {
141                 printk(badmagic, kdevname(device), routine);
142                 return 1;
143         }
144 #endif
145         return 0;
146 }
147
148 /*
149  * This is used to figure out the divisor speeds.
150  *
151  * The formula is:    Baud = BASE_BAUD / ((N + 1) * (1 << M)),
152  *
153  * with               0 <= N < 64 and 0 <= M < 16
154  * 
155  * 12-Oct-2001 - Replaced table driven approach with code written by
156  * Theodore Ts'o <tytso@alum.mit.edu> which exactly replicates the
157  * table.  (Modulo bugs for the 307200 and 61440 baud rates, which
158  * were clearly incorrectly calculated in the original table.  This is
159  * why tables filled with magic constants are evil.)
160  */
161
162 static void calc_ebrg(int baud, int *n_ret, int *m_ret)
163 {
164         int     n, m;
165
166         if (baud == 0) {
167                 *n_ret = 0;
168                 *m_ret = 0;
169                 return;
170         }
171      
172         /*
173          * We scale numbers by 10 so that we get better accuracy
174          * without having to use floating point.  Here we increment m
175          * until n is within the valid range.
176          */
177         n = (BASE_BAUD*10) / baud;
178         m = 0;
179         while (n >= 640) {
180                 n = n / 2;
181                 m++;
182         }
183         n = (n+5) / 10;
184         /*
185          * We try very hard to avoid speeds with M == 0 since they may
186          * not work correctly for XTAL frequences above 10 MHz.
187          */
188         if ((m == 0) && ((n & 1) == 0)) {
189                 n = n / 2;
190                 m++;
191         }
192         *n_ret = n - 1;
193         *m_ret = m;
194 }
195
196 #define SAB82532_MAX_TEC_TIMEOUT 200000 /* 1 character time (at 50 baud) */
197 #define SAB82532_MAX_CEC_TIMEOUT  50000 /* 2.5 TX CLKs (at 50 baud) */
198
199 static __inline__ void sab82532_tec_wait(struct sab82532 *info)
200 {
201         int timeout = info->tec_timeout;
202
203         while ((readb(&info->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
204                 udelay(1);
205 }
206
207 static __inline__ void sab82532_cec_wait(struct sab82532 *info)
208 {
209         int timeout = info->cec_timeout;
210
211         while ((readb(&info->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
212                 udelay(1);
213 }
214
215 static __inline__ void sab82532_start_tx(struct sab82532 *info)
216 {
217         unsigned long flags;
218         int i;
219
220         save_flags(flags); cli();
221
222         if (info->xmit.head == info->xmit.tail)
223                 goto out;
224
225         if (!test_bit(SAB82532_XPR, &info->irqflags))
226                 goto out;
227
228         info->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS);
229         writeb(info->interrupt_mask1, &info->regs->w.imr1);
230         clear_bit(SAB82532_ALLS, &info->irqflags);
231
232         clear_bit(SAB82532_XPR, &info->irqflags);
233         for (i = 0; i < info->xmit_fifo_size; i++) {
234                 writeb(info->xmit.buf[info->xmit.tail],
235                        &info->regs->w.xfifo[i]);
236                 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
237                 info->icount.tx++;
238                 if (info->xmit.head == info->xmit.tail)
239                         break;
240         }
241
242         /* Issue a Transmit Frame command. */
243         sab82532_cec_wait(info);
244         writeb(SAB82532_CMDR_XF, &info->regs->w.cmdr);
245
246 out:
247         restore_flags(flags);
248 }
249
250
251 /*
252  * ------------------------------------------------------------
253  * sab82532_stop() and sab82532_start()
254  *
255  * This routines are called before setting or resetting tty->stopped.
256  * They enable or disable transmitter interrupts, as necessary.
257  * ------------------------------------------------------------
258  */
259 static void sab82532_stop(struct tty_struct *tty)
260 {
261         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
262         unsigned long flags;
263
264         if (serial_paranoia_check(info, tty->device, "sab82532_stop"))
265                 return;
266
267         save_flags(flags); cli();
268         info->interrupt_mask1 |= SAB82532_IMR1_XPR;
269         writeb(info->interrupt_mask1, &info->regs->w.imr1);
270         restore_flags(flags);
271 }
272
273 static void sab82532_start(struct tty_struct *tty)
274 {
275         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
276         unsigned long flags;
277         
278         if (serial_paranoia_check(info, tty->device, "sab82532_start"))
279                 return;
280
281         save_flags(flags); cli();
282         info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
283         writeb(info->interrupt_mask1, &info->regs->w.imr1);
284         sab82532_start_tx(info);
285         restore_flags(flags);
286 }
287
288 /*
289  * ----------------------------------------------------------------------
290  *
291  * Here starts the interrupt handling routines.  All of the following
292  * subroutines are declared as inline and are folded into
293  * sab82532_interrupt().  They were separated out for readability's sake.
294  *
295  * Note: sab82532_interrupt() is a "fast" interrupt, which means that it
296  * runs with interrupts turned off.  People who may want to modify
297  * sab82532_interrupt() should try to keep the interrupt handler as fast as
298  * possible.  After you are done making modifications, it is not a bad
299  * idea to do:
300  * 
301  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
302  *
303  * and look at the resulting assemble code in serial.s.
304  *
305  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
306  * -----------------------------------------------------------------------
307  */
308
309 /*
310  * This routine is used by the interrupt handler to schedule
311  * processing in the software interrupt portion of the driver.
312  */
313 static void sab82532_sched_event(struct sab82532 *info, int event)
314 {
315         info->event |= 1 << event;
316         queue_task(&info->tqueue, &tq_serial);
317         mark_bh(SERIAL_BH);
318 }
319
320 static void receive_chars(struct sab82532 *info,
321                           union sab82532_irq_status *stat)
322 {
323         struct tty_struct *tty = info->tty;
324         unsigned char buf[32];
325         unsigned char status;
326         int free_fifo = 0;
327         int i, count = 0;
328
329         /* Read number of BYTES (Character + Status) available. */
330         if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
331                 count = info->recv_fifo_size;
332                 free_fifo++;
333         }
334
335         if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
336                 count = readb(&info->regs->r.rbcl) & (info->recv_fifo_size - 1);
337                 free_fifo++;
338         }
339
340         /* Issue a FIFO read command in case we where idle. */
341         if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
342                 sab82532_cec_wait(info);
343                 writeb(SAB82532_CMDR_RFRD, &info->regs->w.cmdr);
344                 return;
345         }
346
347         if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
348 #ifdef SERIAL_DEBUG_OVERFLOW
349                 printk("sab82532: receive_chars: RFO");
350 #endif
351                 free_fifo++;
352         }
353
354         /* Read the FIFO. */
355         for (i = 0; i < count; i++)
356                 buf[i] = readb(&info->regs->r.rfifo[i]);
357
358         /* Issue Receive Message Complete command. */
359         if (free_fifo) {
360                 sab82532_cec_wait(info);
361                 writeb(SAB82532_CMDR_RMC, &info->regs->w.cmdr);
362         }
363
364         if (!tty)
365                 return;
366
367         for (i = 0; i < count; ) {
368                 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
369 #ifdef SERIAL_DEBUG_OVERFLOW
370                         printk("sab82532: receive_chars: tty overrun\n");
371 #endif
372                         info->icount.buf_overrun++;
373                         break;
374                 }
375
376                 tty->flip.count++;
377                 *tty->flip.char_buf_ptr++ = buf[i++];
378                 status = buf[i++];
379                 info->icount.rx++;
380
381 #ifdef SERIAL_DEBUG_INTR
382                 printk("DR%02x:%02x...", (unsigned char)*(tty->flip.char_buf_ptr - 1), status);
383 #endif
384
385                 if (status & SAB82532_RSTAT_PE) {
386                         *tty->flip.flag_buf_ptr++ = TTY_PARITY;
387                         info->icount.parity++;
388                 } else if (status & SAB82532_RSTAT_FE) {
389                         *tty->flip.flag_buf_ptr++ = TTY_FRAME;
390                         info->icount.frame++;
391                 }
392                 else
393                         *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
394         }
395
396         queue_task(&tty->flip.tqueue, &tq_timer);
397 }
398
399 static void transmit_chars(struct sab82532 *info,
400                            union sab82532_irq_status *stat)
401 {
402         int i;
403
404         if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
405                 info->interrupt_mask1 |= SAB82532_IMR1_ALLS;
406                 writeb(info->interrupt_mask1, &info->regs->w.imr1);
407                 set_bit(SAB82532_ALLS, &info->irqflags);
408         }
409
410         if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
411                 return;
412
413         if (!(readb(&info->regs->r.star) & SAB82532_STAR_XFW)) {
414 #ifdef SERIAL_DEBUG_FIFO
415                 printk("%s: XPR, but no XFW (?)\n", __FUNCTION__);
416 #endif
417                 return;
418         }
419
420         set_bit(SAB82532_XPR, &info->irqflags);
421
422         if (!info->tty) {
423                 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
424                 writeb(info->interrupt_mask1, &info->regs->w.imr1);
425                 return;
426         }
427
428         if ((info->xmit.head == info->xmit.tail) ||
429             info->tty->stopped || info->tty->hw_stopped) {
430                 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
431                 writeb(info->interrupt_mask1, &info->regs->w.imr1);
432                 return;
433         }
434
435         info->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS);
436         writeb(info->interrupt_mask1, &info->regs->w.imr1);
437         clear_bit(SAB82532_ALLS, &info->irqflags);
438
439         /* Stuff 32 bytes into Transmit FIFO. */
440         clear_bit(SAB82532_XPR, &info->irqflags);
441         for (i = 0; i < info->xmit_fifo_size; i++) {
442                 writeb(info->xmit.buf[info->xmit.tail],
443                        &info->regs->w.xfifo[i]);
444                 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
445                 info->icount.tx++;
446                 if (info->xmit.head == info->xmit.tail)
447                         break;
448         }
449
450         /* Issue a Transmit Frame command. */
451         sab82532_cec_wait(info);
452         writeb(SAB82532_CMDR_XF, &info->regs->w.cmdr);
453
454         if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
455                 sab82532_sched_event(info, RS_EVENT_WRITE_WAKEUP);
456
457 #ifdef SERIAL_DEBUG_INTR
458         printk("THRE...");
459 #endif
460 }
461
462 static void check_status(struct sab82532 *info,
463                          union sab82532_irq_status *stat)
464 {
465         struct tty_struct *tty = info->tty;
466         int modem_change = 0;
467
468         if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
469 #ifdef CONFIG_SERIAL_CONSOLE
470                 if (info->is_console) {
471                         batten_down_hatches(info);
472                         return;
473                 }
474 #endif
475                 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
476                         info->icount.buf_overrun++;
477                         goto check_modem;
478                 }
479                 tty->flip.count++;
480                 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
481                 *tty->flip.char_buf_ptr++ = 0;
482                 info->icount.brk++;
483         }
484
485         if (!tty)
486                 return;
487
488         if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
489                 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
490                         info->icount.buf_overrun++;
491                         goto check_modem;
492                 }
493                 tty->flip.count++;
494                 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
495                 *tty->flip.char_buf_ptr++ = 0;
496                 info->icount.overrun++;
497         }
498
499 check_modem:
500         if (stat->sreg.isr0 & SAB82532_ISR0_CDSC) {
501                 info->dcd = (readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : 1;
502                 info->icount.dcd++;
503                 modem_change++;
504 #ifdef SERIAL_DEBUG_MODEM
505                 printk("DCD change: %d\n", info->icount.dcd);
506 #endif
507         }
508         if (stat->sreg.isr1 & SAB82532_ISR1_CSC) {
509                 info->cts = readb(&info->regs->r.star) & SAB82532_STAR_CTS;
510                 info->icount.cts++;
511                 modem_change++;
512 #ifdef SERIAL_DEBUG_MODEM
513                 printk("CTS change: %d, CTS %s\n", info->icount.cts, info->cts ? "on" : "off");
514 #endif
515         }
516         if ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ^ info->dsr) {
517                 info->dsr = (readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : 1;
518                 info->icount.dsr++;
519                 modem_change++;
520 #ifdef SERIAL_DEBUG_MODEM
521                 printk("DSR change: %d\n", info->icount.dsr);
522 #endif
523         }
524         if (modem_change)
525                 wake_up_interruptible(&info->delta_msr_wait);
526
527         if ((info->flags & ASYNC_CHECK_CD) &&
528             (stat->sreg.isr0 & SAB82532_ISR0_CDSC)) {
529
530 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
531                 printk("ttys%d CD now %s...", info->line,
532                        (info->dcd) ? "on" : "off");
533 #endif          
534
535                 if (info->dcd)
536                         wake_up_interruptible(&info->open_wait);
537                 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
538                            (info->flags & ASYNC_CALLOUT_NOHUP))) {
539
540 #ifdef SERIAL_DEBUG_OPEN
541                         printk("scheduling hangup...");
542 #endif
543                         MOD_INC_USE_COUNT;
544                         if (schedule_task(&info->tqueue_hangup) == 0)
545                                 MOD_DEC_USE_COUNT;
546                 }
547         }
548
549         if (info->flags & ASYNC_CTS_FLOW) {
550                 if (info->tty->hw_stopped) {
551                         if (info->cts) {
552
553 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
554                                 printk("CTS tx start...");
555 #endif
556                                 info->tty->hw_stopped = 0;
557                                 sab82532_sched_event(info,
558                                                      RS_EVENT_WRITE_WAKEUP);
559                                 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
560                                 writeb(info->interrupt_mask1, &info->regs->w.imr1);
561                                 sab82532_start_tx(info);
562                         }
563                 } else {
564                         if (!(info->cts)) {
565
566 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
567                                 printk("CTS tx stop...");
568 #endif
569                                 info->tty->hw_stopped = 1;
570                         }
571                 }
572         }
573 }
574
575 /*
576  * This is the serial driver's generic interrupt routine
577  */
578 static void sab82532_interrupt(int irq, void *dev_id, struct pt_regs *regs)
579 {
580         struct sab82532 *info = dev_id;
581         union sab82532_irq_status status;
582
583 #ifdef SERIAL_DEBUG_INTR
584         printk("sab82532_interrupt(%d)...", irq);
585 #endif
586
587         status.stat = 0;
588         if (readb(&info->regs->r.gis) & SAB82532_GIS_ISA0)
589                 status.sreg.isr0 = readb(&info->regs->r.isr0);
590         if (readb(&info->regs->r.gis) & SAB82532_GIS_ISA1)
591                 status.sreg.isr1 = readb(&info->regs->r.isr1);
592
593 #ifdef SERIAL_DEBUG_INTR
594         printk("%d<%02x.%02x>", info->line,
595                status.sreg.isr0, status.sreg.isr1);
596 #endif
597
598         if (!status.stat)
599                 goto next;
600
601         if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
602                                 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
603                 receive_chars(info, &status);
604         if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
605             (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
606                 check_status(info, &status);
607         if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
608                 transmit_chars(info, &status);
609
610 next:
611         info = info->next;
612         status.stat = 0;
613         if (readb(&info->regs->r.gis) & SAB82532_GIS_ISB0)
614                 status.sreg.isr0 = readb(&info->regs->r.isr0);
615         if (readb(&info->regs->r.gis) & SAB82532_GIS_ISB1)
616                 status.sreg.isr1 = readb(&info->regs->r.isr1);
617
618 #ifdef SERIAL_DEBUG_INTR
619         printk("%d<%02x.%02x>", info->line,
620                status.sreg.isr0, status.sreg.isr1);
621 #endif
622
623         if (!status.stat)
624                 goto done;
625
626         if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
627                                 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
628                 receive_chars(info, &status);
629         if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
630             (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
631                 check_status(info, &status);
632         if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
633                 transmit_chars(info, &status);
634
635 done:
636         ;
637 #ifdef SERIAL_DEBUG_INTR
638         printk("end.\n");
639 #endif
640 }
641
642 /*
643  * -------------------------------------------------------------------
644  * Here ends the serial interrupt routines.
645  * -------------------------------------------------------------------
646  */
647
648 /*
649  * This routine is used to handle the "bottom half" processing for the
650  * serial driver, known also the "software interrupt" processing.
651  * This processing is done at the kernel interrupt level, after the
652  * sab82532_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
653  * is where time-consuming activities which can not be done in the
654  * interrupt driver proper are done; the interrupt driver schedules
655  * them using sab82532_sched_event(), and they get done here.
656  */
657 static void do_serial_bh(void)
658 {
659         run_task_queue(&tq_serial);
660 }
661
662 static void do_softint(void *private_)
663 {
664         struct sab82532 *info = (struct sab82532 *)private_;
665         struct tty_struct *tty;
666
667         tty = info->tty;
668         if (!tty)
669                 return;
670
671         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
672                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
673                     tty->ldisc.write_wakeup)
674                         (tty->ldisc.write_wakeup)(tty);
675                 wake_up_interruptible(&tty->write_wait);
676         }
677 }
678
679 /*
680  * This routine is called from the scheduler tqueue when the interrupt
681  * routine has signalled that a hangup has occurred.  The path of
682  * hangup processing is:
683  *
684  *      serial interrupt routine -> (scheduler tqueue) ->
685  *      do_serial_hangup() -> tty->hangup() -> sab82532_hangup()
686  * 
687  */
688 static void do_serial_hangup(void *private_)
689 {
690         struct sab82532 *info = (struct sab82532 *) private_;
691         struct tty_struct *tty;
692
693         tty = info->tty;
694         if (tty)
695                 tty_hangup(tty);
696         MOD_DEC_USE_COUNT;
697 }
698
699 static void
700 sab82532_init_line(struct sab82532 *info)
701 {
702         unsigned char stat, tmp;
703
704         /*
705          * Wait for any commands or immediate characters
706          */
707         sab82532_cec_wait(info);
708         sab82532_tec_wait(info);
709
710         /*
711          * Clear the FIFO buffers.
712          */
713         writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
714         sab82532_cec_wait(info);
715         writeb(SAB82532_CMDR_XRES, &info->regs->w.cmdr);
716
717         /*
718          * Clear the interrupt registers.
719          */
720         stat = readb(&info->regs->r.isr0);
721         stat = readb(&info->regs->r.isr1);
722
723         /*
724          * Now, initialize the UART 
725          */
726         writeb(0, &info->regs->w.ccr0);                         /* power-down */
727         writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
728                SAB82532_CCR0_SM_ASYNC, &info->regs->w.ccr0);
729         writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &info->regs->w.ccr1);
730         writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
731                SAB82532_CCR2_TOE, &info->regs->w.ccr2);
732         writeb(0, &info->regs->w.ccr3);
733         writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &info->regs->w.ccr4);
734         writeb(SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
735                SAB82532_MODE_RAC, &info->regs->w.mode);
736         writeb(SAB82532_RFC_DPS | SAB82532_RFC_RFDF, &info->regs->w.rfc);
737         switch (info->recv_fifo_size) {
738                 case 1:
739                         tmp = readb(&info->regs->w.rfc);
740                         tmp |= SAB82532_RFC_RFTH_1;
741                         writeb(tmp, &info->regs->w.rfc);
742                         break;
743                 case 4:
744                         tmp = readb(&info->regs->w.rfc);
745                         tmp |= SAB82532_RFC_RFTH_4;
746                         writeb(tmp, &info->regs->w.rfc);
747                         break;
748                 case 16:
749                         tmp = readb(&info->regs->w.rfc);
750                         tmp |= SAB82532_RFC_RFTH_16;
751                         writeb(tmp, &info->regs->w.rfc);
752                         break;
753                 default:
754                         info->recv_fifo_size = 32;
755                         /* fall through */
756                 case 32:
757                         tmp = readb(&info->regs->w.rfc);
758                         tmp |= SAB82532_RFC_RFTH_32;
759                         writeb(tmp, &info->regs->w.rfc);
760                         break;
761         }
762         tmp = readb(&info->regs->rw.ccr0);
763         tmp |= SAB82532_CCR0_PU;        /* power-up */
764         writeb(tmp, &info->regs->rw.ccr0);
765 }
766
767 static int startup(struct sab82532 *info)
768 {
769         unsigned long flags;
770         unsigned long page;
771         int retval = 0;
772
773         page = get_free_page(GFP_KERNEL);
774         if (!page)
775                 return -ENOMEM;
776
777         save_flags(flags); cli();
778
779         if (info->flags & ASYNC_INITIALIZED) {
780                 free_page(page);
781                 goto errout;
782         }
783
784         if (!info->regs) {
785                 if (info->tty)
786                         set_bit(TTY_IO_ERROR, &info->tty->flags);
787                 free_page(page);
788                 retval = -ENODEV;
789                 goto errout;
790         }
791         if (info->xmit.buf)
792                 free_page(page);
793         else
794                 info->xmit.buf = (unsigned char *)page;
795
796 #ifdef SERIAL_DEBUG_OPEN
797         printk("starting up serial port %d...", info->line);
798 #endif
799
800         /*
801          * Initialize the Hardware
802          */
803         sab82532_init_line(info);
804
805         if (info->tty->termios->c_cflag & CBAUD) {
806                 u8 tmp;
807
808                 tmp = readb(&info->regs->rw.mode);
809                 tmp &= ~(SAB82532_MODE_FRTS);
810                 tmp |= SAB82532_MODE_RTS;
811                 writeb(tmp, &info->regs->rw.mode);
812
813                 tmp = readb(&info->regs->rw.pvr);
814                 tmp &= ~(info->pvr_dtr_bit);
815                 writeb(tmp, &info->regs->rw.pvr);
816         }
817
818         /*
819          * Finally, enable interrupts
820          */
821         info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
822                                 SAB82532_IMR0_PLLA;
823         writeb(info->interrupt_mask0, &info->regs->w.imr0);
824         info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
825                                 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
826                                 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
827                                 SAB82532_IMR1_XPR;
828         writeb(info->interrupt_mask1, &info->regs->w.imr1);
829         set_bit(SAB82532_ALLS, &info->irqflags);
830
831         if (info->tty)
832                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
833         info->xmit.head = info->xmit.tail = 0;
834
835         set_bit(SAB82532_XPR, &info->irqflags);
836
837         /*
838          * and set the speed of the serial port
839          */
840         change_speed(info);
841
842         info->flags |= ASYNC_INITIALIZED;
843         restore_flags(flags);
844         return 0;
845         
846 errout:
847         restore_flags(flags);
848         return retval;
849 }
850
851 /*
852  * This routine will shutdown a serial port; interrupts are disabled, and
853  * DTR is dropped if the hangup on close termio flag is on.
854  */
855 static void shutdown(struct sab82532 *info)
856 {
857         unsigned long flags;
858         u8 tmp;
859
860         if (!(info->flags & ASYNC_INITIALIZED))
861                 return;
862
863 #ifdef SERIAL_DEBUG_OPEN
864         printk("Shutting down serial port %d...", info->line);
865 #endif
866
867         save_flags(flags); cli(); /* Disable interrupts */
868
869         /*
870          * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
871          * here so the queue might never be waken up
872          */
873         wake_up_interruptible(&info->delta_msr_wait);
874
875         if (info->xmit.buf) {
876                 free_page((unsigned long)info->xmit.buf);
877                 info->xmit.buf = 0;
878         }
879
880 #ifdef CONFIG_SERIAL_CONSOLE
881         if (info->is_console) {
882                 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
883                                         SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
884                 writeb(info->interrupt_mask0, &info->regs->w.imr0);
885                 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
886                                         SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
887                                         SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
888                                         SAB82532_IMR1_XPR;
889                 writeb(info->interrupt_mask1, &info->regs->w.imr1);
890                 if (info->tty)
891                         set_bit(TTY_IO_ERROR, &info->tty->flags);
892                 info->flags &= ~ASYNC_INITIALIZED;
893                 restore_flags(flags);
894                 return;
895         }
896 #endif
897
898         /* Disable Interrupts */
899         info->interrupt_mask0 = 0xff;
900         writeb(info->interrupt_mask0, &info->regs->w.imr0);
901         info->interrupt_mask1 = 0xff;
902         writeb(info->interrupt_mask1, &info->regs->w.imr1);
903
904         if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
905                 tmp = readb(&info->regs->r.mode);
906                 tmp |= (SAB82532_MODE_FRTS | SAB82532_MODE_RTS);
907                 writeb(tmp, &info->regs->rw.mode);
908                 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit,
909                        &info->regs->rw.pvr);
910         }
911
912         /* Disable break condition */
913         tmp = readb(&info->regs->rw.dafo);
914         tmp &= ~(SAB82532_DAFO_XBRK);
915         writeb(tmp, &info->regs->rw.dafo);
916
917         /* Disable Receiver */  
918         tmp = readb(&info->regs->rw.mode);
919         tmp &= ~(SAB82532_MODE_RAC);
920         writeb(tmp, &info->regs->rw.mode);
921
922         /* Power Down */        
923         tmp = readb(&info->regs->rw.ccr0);
924         tmp &= ~(SAB82532_CCR0_PU);
925         writeb(tmp, &info->regs->rw.ccr0);
926
927         if (info->tty)
928                 set_bit(TTY_IO_ERROR, &info->tty->flags);
929
930         info->flags &= ~ASYNC_INITIALIZED;
931         restore_flags(flags);
932 }
933
934 /*
935  * This routine is called to set the UART divisor registers to match
936  * the specified baud rate for a serial port.
937  */
938 static void change_speed(struct sab82532 *info)
939 {
940         unsigned long   flags;
941         unsigned int    ebrg;
942         tcflag_t        cflag;
943         unsigned char   dafo;
944         int             bits, n, m;
945
946         if (!info->tty || !info->tty->termios)
947                 return;
948         cflag = info->tty->termios->c_cflag;
949
950         /* Byte size and parity */
951         switch (cflag & CSIZE) {
952               case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
953               case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
954               case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
955               case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
956               /* Never happens, but GCC is too dumb to figure it out */
957               default:  dafo = SAB82532_DAFO_CHL5; bits = 7; break;
958         }
959
960         if (cflag & CSTOPB) {
961                 dafo |= SAB82532_DAFO_STOP;
962                 bits++;
963         }
964
965         if (cflag & PARENB) {
966                 dafo |= SAB82532_DAFO_PARE;
967                 bits++;
968         }
969
970         if (cflag & PARODD) {
971 #ifdef CMSPAR
972                 if (cflag & CMSPAR)
973                         dafo |= SAB82532_DAFO_PAR_MARK;
974                 else
975 #endif
976                         dafo |= SAB82532_DAFO_PAR_ODD;
977         } else {
978 #ifdef CMSPAR
979                 if (cflag & CMSPAR)
980                         dafo |= SAB82532_DAFO_PAR_SPACE;
981                 else
982 #endif
983                         dafo |= SAB82532_DAFO_PAR_EVEN;
984         }
985
986         /* Determine EBRG values based on baud rate */
987         info->baud = tty_get_baud_rate(info->tty);
988         calc_ebrg(info->baud, &n, &m);
989         
990         ebrg = n | (m << 6);
991
992         if (info->baud) {
993                 info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
994                 info->tec_timeout = (10 * 1000000) / info->baud;
995                 info->cec_timeout = info->tec_timeout >> 2;
996         } else {
997                 info->timeout = 0;
998                 info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
999                 info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
1000         }
1001         info->timeout += HZ / 50;               /* Add .02 seconds of slop */
1002
1003         /* CTS flow control flags */
1004         if (cflag & CRTSCTS)
1005                 info->flags |= ASYNC_CTS_FLOW;
1006         else
1007                 info->flags &= ~(ASYNC_CTS_FLOW);
1008
1009         if (cflag & CLOCAL)
1010                 info->flags &= ~(ASYNC_CHECK_CD);
1011         else
1012                 info->flags |= ASYNC_CHECK_CD;
1013         if (info->tty)
1014                 info->tty->hw_stopped = 0;
1015
1016         /*
1017          * Set up parity check flag
1018          * XXX: not implemented, yet.
1019          */
1020 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1021
1022         /*
1023          * Characters to ignore
1024          * XXX: not implemented, yet.
1025          */
1026
1027         /*
1028          * !!! ignore all characters if CREAD is not set
1029          * XXX: not implemented, yet.
1030          */
1031         if ((cflag & CREAD) == 0)
1032                 info->ignore_status_mask |= SAB82532_ISR0_RPF |
1033                                             SAB82532_ISR0_TCD |
1034                                             SAB82532_ISR0_TIME;
1035
1036         save_flags(flags); cli();
1037         sab82532_cec_wait(info);
1038         sab82532_tec_wait(info);
1039         writeb(dafo, &info->regs->w.dafo);
1040         writeb(ebrg & 0xff, &info->regs->w.bgr);
1041         writeb(readb(&info->regs->rw.ccr2) & ~(0xc0), &info->regs->rw.ccr2);
1042         writeb(readb(&info->regs->rw.ccr2) | ((ebrg >> 2) & 0xc0), &info->regs->rw.ccr2);
1043         if (info->flags & ASYNC_CTS_FLOW) {
1044                 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1045                 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1046                 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FCTS), &info->regs->rw.mode);
1047                 info->interrupt_mask1 &= ~(SAB82532_IMR1_CSC);
1048                 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1049         } else {
1050                 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1051                 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1052                 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FCTS, &info->regs->rw.mode);
1053                 info->interrupt_mask1 |= SAB82532_IMR1_CSC;
1054                 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1055         }
1056         writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RAC, &info->regs->rw.mode);
1057         restore_flags(flags);
1058 }
1059
1060 static void sab82532_put_char(struct tty_struct *tty, unsigned char ch)
1061 {
1062         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1063         unsigned long flags;
1064
1065         if (serial_paranoia_check(info, tty->device, "sab82532_put_char"))
1066                 return;
1067
1068         if (!tty || !info->xmit.buf)
1069                 return;
1070
1071         save_flags(flags); cli();
1072         if (!CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)) {
1073                 restore_flags(flags);
1074                 return;
1075         }
1076
1077         info->xmit.buf[info->xmit.head] = ch;
1078         info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
1079         restore_flags(flags);
1080 }
1081
1082 static void sab82532_flush_chars(struct tty_struct *tty)
1083 {
1084         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1085         unsigned long flags;
1086
1087         if (serial_paranoia_check(info, tty->device, "sab82532_flush_chars"))
1088                 return;
1089
1090         if ((info->xmit.head == info->xmit.tail) ||
1091             tty->stopped || tty->hw_stopped || !info->xmit.buf)
1092                 return;
1093
1094         save_flags(flags); cli();
1095         info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1096         writeb(info->interrupt_mask1, &info->regs->w.imr1);
1097         sab82532_start_tx(info);
1098         restore_flags(flags);
1099 }
1100
1101 static int sab82532_write(struct tty_struct * tty, int from_user,
1102                           const unsigned char *buf, int count)
1103 {
1104         int c, ret = 0;
1105         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1106         unsigned long flags;
1107
1108         if (serial_paranoia_check(info, tty->device, "sab82532_write"))
1109                 return 0;
1110
1111         if (!tty || !info->xmit.buf || !tmp_buf)
1112                 return 0;
1113             
1114         save_flags(flags);
1115         if (from_user) {
1116                 down(&tmp_buf_sem);
1117                 while (1) {
1118                         int c1;
1119                         c = CIRC_SPACE_TO_END(info->xmit.head,
1120                                               info->xmit.tail,
1121                                               SERIAL_XMIT_SIZE);
1122                         if (count < c)
1123                                 c = count;
1124                         if (c <= 0)
1125                                 break;
1126
1127                         c -= copy_from_user(tmp_buf, buf, c);
1128                         if (!c) {
1129                                 if (!ret)
1130                                         ret = -EFAULT;
1131                                 break;
1132                         }
1133                         cli();
1134                         c1 = CIRC_SPACE_TO_END(info->xmit.head,
1135                                                info->xmit.tail,
1136                                                SERIAL_XMIT_SIZE);
1137                         if (c1 < c)
1138                                 c = c1;
1139                         memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
1140                         info->xmit.head = (info->xmit.head + c) & (SERIAL_XMIT_SIZE-1);
1141                         restore_flags(flags);
1142                         buf += c;
1143                         count -= c;
1144                         ret += c;
1145                 }
1146                 up(&tmp_buf_sem);
1147         } else {
1148                 cli();
1149                 while (1) {
1150                         c = CIRC_SPACE_TO_END(info->xmit.head,
1151                                               info->xmit.tail,
1152                                               SERIAL_XMIT_SIZE);
1153                         if (count < c)
1154                                 c = count;
1155                         if (c <= 0)
1156                                 break;
1157                         memcpy(info->xmit.buf + info->xmit.head, buf, c);
1158                         info->xmit.head = (info->xmit.head + c) & (SERIAL_XMIT_SIZE-1);
1159                         buf += c;
1160                         count -= c;
1161                         ret += c;
1162                 }
1163                 restore_flags(flags);
1164         }
1165
1166         if ((info->xmit.head != info->xmit.tail) &&
1167             !tty->stopped && !tty->hw_stopped) {
1168                 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1169                 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1170                 sab82532_start_tx(info);
1171         }
1172
1173         restore_flags(flags);
1174         return ret;
1175 }
1176
1177 static int sab82532_write_room(struct tty_struct *tty)
1178 {
1179         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1180
1181         if (serial_paranoia_check(info, tty->device, "sab82532_write_room"))
1182                 return 0;
1183
1184         return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1185 }
1186
1187 static int sab82532_chars_in_buffer(struct tty_struct *tty)
1188 {
1189         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1190                                 
1191         if (serial_paranoia_check(info, tty->device, "sab82532_chars_in_buffer"))
1192                 return 0;
1193
1194         return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1195 }
1196
1197 static void sab82532_flush_buffer(struct tty_struct *tty)
1198 {
1199         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1200         unsigned long flags;
1201
1202         if (serial_paranoia_check(info, tty->device, "sab82532_flush_buffer"))
1203                 return;
1204
1205         save_flags(flags); cli();
1206         info->xmit.head = info->xmit.tail = 0;
1207         restore_flags(flags);
1208
1209         wake_up_interruptible(&tty->write_wait);
1210         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1211             tty->ldisc.write_wakeup)
1212                 (tty->ldisc.write_wakeup)(tty);
1213 }
1214
1215 /*
1216  * This function is used to send a high-priority XON/XOFF character to
1217  * the device
1218  */
1219 static void sab82532_send_xchar(struct tty_struct *tty, char ch)
1220 {
1221         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1222         unsigned long flags;
1223
1224         if (serial_paranoia_check(info, tty->device, "sab82532_send_xchar"))
1225                 return;
1226
1227         save_flags(flags); cli();
1228         sab82532_tec_wait(info);
1229         writeb(ch, &info->regs->w.tic);
1230         restore_flags(flags);
1231 }
1232
1233 /*
1234  * ------------------------------------------------------------
1235  * sab82532_throttle()
1236  * 
1237  * This routine is called by the upper-layer tty layer to signal that
1238  * incoming characters should be throttled.
1239  * ------------------------------------------------------------
1240  */
1241 static void sab82532_throttle(struct tty_struct * tty)
1242 {
1243         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1244 #ifdef SERIAL_DEBUG_THROTTLE
1245         char    buf[64];
1246         
1247         printk("throttle %s: %d....\n", _tty_name(tty, buf),
1248                tty->ldisc.chars_in_buffer(tty));
1249 #endif
1250
1251         if (serial_paranoia_check(info, tty->device, "sab82532_throttle"))
1252                 return;
1253         
1254         if (I_IXOFF(tty))
1255                 sab82532_send_xchar(tty, STOP_CHAR(tty));
1256
1257         if (tty->termios->c_cflag & CRTSCTS) {
1258                 u8 mode = readb(&info->regs->r.mode);
1259                 mode &= ~(SAB82532_MODE_FRTS | SAB82532_MODE_RTS);
1260                 writeb(mode, &info->regs->w.mode);
1261         }
1262 }
1263
1264 static void sab82532_unthrottle(struct tty_struct * tty)
1265 {
1266         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1267 #ifdef SERIAL_DEBUG_THROTTLE
1268         char    buf[64];
1269         
1270         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1271                tty->ldisc.chars_in_buffer(tty));
1272 #endif
1273
1274         if (serial_paranoia_check(info, tty->device, "sab82532_unthrottle"))
1275                 return;
1276         
1277         if (I_IXOFF(tty)) {
1278                 if (info->x_char)
1279                         info->x_char = 0;
1280                 else
1281                         sab82532_send_xchar(tty, START_CHAR(tty));
1282         }
1283
1284         if (tty->termios->c_cflag & CRTSCTS) {
1285                 u8 mode = readb(&info->regs->r.mode);
1286                 mode &= ~(SAB82532_MODE_RTS);
1287                 mode |= SAB82532_MODE_FRTS;
1288                 writeb(mode, &info->regs->w.mode);
1289         }
1290 }
1291
1292 /*
1293  * ------------------------------------------------------------
1294  * sab82532_ioctl() and friends
1295  * ------------------------------------------------------------
1296  */
1297
1298 static int get_serial_info(struct sab82532 *info,
1299                            struct serial_struct *retinfo)
1300 {
1301         struct serial_struct tmp;
1302    
1303         if (!retinfo)
1304                 return -EFAULT;
1305         memset(&tmp, 0, sizeof(tmp));
1306         tmp.type = info->type;
1307         tmp.line = info->line;
1308         tmp.port = (unsigned long)info->regs;
1309         tmp.irq = info->irq;
1310         tmp.flags = info->flags;
1311         tmp.xmit_fifo_size = info->xmit_fifo_size;
1312         tmp.baud_base = info->baud_base;
1313         tmp.close_delay = info->close_delay;
1314         tmp.closing_wait = info->closing_wait;
1315         tmp.custom_divisor = info->custom_divisor;
1316         tmp.hub6 = 0;
1317         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1318                 return -EFAULT;
1319         return 0;
1320 }
1321
1322 static int set_serial_info(struct sab82532 *info,
1323                            struct serial_struct *new_info)
1324 {
1325         return 0;
1326 }
1327
1328
1329 /*
1330  * get_lsr_info - get line status register info
1331  *
1332  * Purpose: Let user call ioctl() to get info when the UART physically
1333  *          is emptied.  On bus types like RS485, the transmitter must
1334  *          release the bus after transmitting. This must be done when
1335  *          the transmit shift register is empty, not be done when the
1336  *          transmit holding register is empty.  This functionality
1337  *          allows an RS485 driver to be written in user space. 
1338  */
1339 static int get_lsr_info(struct sab82532 * info, unsigned int *value)
1340 {
1341         unsigned int result;
1342
1343         result = (!info->xmit.buf && test_bit(SAB82532_ALLS, &info->irqflags))
1344                                                         ? TIOCSER_TEMT : 0;
1345         return put_user(result, value);
1346 }
1347
1348
1349 static int get_modem_info(struct sab82532 * info, unsigned int *value)
1350 {
1351         unsigned int result;
1352
1353         result =  ((readb(&info->regs->r.mode) & SAB82532_MODE_RTS) ? 
1354                     ((readb(&info->regs->r.mode) & SAB82532_MODE_FRTS) ? 0 : TIOCM_RTS)
1355                                                             : TIOCM_RTS)
1356                 | ((readb(&info->regs->r.pvr) & info->pvr_dtr_bit) ? 0 : TIOCM_DTR)
1357                 | ((readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR)
1358                 | ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : TIOCM_DSR)
1359                 | ((readb(&info->regs->r.star) & SAB82532_STAR_CTS) ? TIOCM_CTS : 0);
1360         return put_user(result,value);
1361 }
1362
1363 static int set_modem_info(struct sab82532 * info, unsigned int cmd,
1364                           unsigned int *value)
1365 {
1366         unsigned int arg;
1367
1368         if (get_user(arg, value))
1369                 return -EFAULT;
1370         switch (cmd) {
1371         case TIOCMBIS: 
1372                 if (arg & TIOCM_RTS) {
1373                         writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1374                         writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1375                 }
1376                 if (arg & TIOCM_DTR) {
1377                         writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1378                 }
1379                 break;
1380         case TIOCMBIC:
1381                 if (arg & TIOCM_RTS) {
1382                         writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1383                         writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1384                 }
1385                 if (arg & TIOCM_DTR) {
1386                         writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1387                 }
1388                 break;
1389         case TIOCMSET:
1390                 if (arg & TIOCM_RTS) {
1391                         writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1392                         writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1393                 } else {
1394                         writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1395                         writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1396                 }
1397                 if (arg & TIOCM_DTR) {
1398                         writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1399                 } else {
1400                         writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1401                 }
1402                 break;
1403         default:
1404                 return -EINVAL;
1405         }
1406         return 0;
1407 }
1408
1409 /*
1410  * This routine sends a break character out the serial port.
1411  */
1412 static void sab82532_break(struct tty_struct *tty, int break_state)
1413 {
1414         struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1415         unsigned long flags;
1416
1417         if (serial_paranoia_check(info, tty->device, "sab82532_break"))
1418                 return;
1419
1420         if (!info->regs)
1421                 return;
1422
1423 #ifdef SERIAL_DEBUG_SEND_BREAK
1424         printk("sab82532_break(%d) jiff=%lu...", break_state, jiffies);
1425 #endif
1426         save_flags(flags); cli();
1427         if (break_state == -1)
1428                 writeb(readb(&info->regs->rw.dafo) | SAB82532_DAFO_XBRK, &info->regs->rw.dafo);
1429         else
1430                 writeb(readb(&info->regs->rw.dafo) & ~(SAB82532_DAFO_XBRK), &info->regs->rw.dafo);
1431         restore_flags(flags);
1432 }
1433
1434
1435 static int sab82532_ioctl(struct tty_struct *tty, struct file * file,
1436                     unsigned int cmd, unsigned long arg)
1437 {
1438         struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1439         struct async_icount cprev, cnow;        /* kernel counter temps */
1440         struct serial_icounter_struct *p_cuser; /* user space */
1441
1442         if (serial_paranoia_check(info, tty->device, "sab82532_ioctl"))
1443                 return -ENODEV;
1444
1445         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1446             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1447             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1448             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1449                 if (tty->flags & (1 << TTY_IO_ERROR))
1450                     return -EIO;
1451         }
1452         
1453         switch (cmd) {
1454                 case TIOCGSOFTCAR:
1455                         return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1456                 case TIOCSSOFTCAR:
1457                         if (get_user(arg, (unsigned int *) arg))
1458                                 return -EFAULT;
1459                         tty->termios->c_cflag =
1460                                 ((tty->termios->c_cflag & ~CLOCAL) |
1461                                  (arg ? CLOCAL : 0));
1462                         return 0;
1463                 case TIOCMGET:
1464                         return get_modem_info(info, (unsigned int *) arg);
1465                 case TIOCMBIS:
1466                 case TIOCMBIC:
1467                 case TIOCMSET:
1468                         return set_modem_info(info, cmd, (unsigned int *) arg);
1469                 case TIOCGSERIAL:
1470                         return get_serial_info(info,
1471                                                (struct serial_struct *) arg);
1472                 case TIOCSSERIAL:
1473                         return set_serial_info(info,
1474                                                (struct serial_struct *) arg);
1475
1476                 case TIOCSERGETLSR: /* Get line status register */
1477                         return get_lsr_info(info, (unsigned int *) arg);
1478
1479                 case TIOCSERGSTRUCT:
1480                         if (copy_to_user((struct sab82532 *) arg,
1481                                          info, sizeof(struct sab82532)))
1482                                 return -EFAULT;
1483                         return 0;
1484                                 
1485                 /*
1486                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1487                  * - mask passed in arg for lines of interest
1488                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1489                  * Caller should use TIOCGICOUNT to see which one it was
1490                  */
1491                  case TIOCMIWAIT:
1492                         cli();
1493                         /* note the counters on entry */
1494                         cprev = info->icount;
1495                         sti();
1496                         while (1) {
1497                                 interruptible_sleep_on(&info->delta_msr_wait);
1498                                 /* see if a signal did it */
1499                                 if (signal_pending(current))
1500                                         return -ERESTARTSYS;
1501                                 cli();
1502                                 cnow = info->icount; /* atomic copy */
1503                                 sti();
1504                                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 
1505                                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1506                                         return -EIO; /* no change => error */
1507                                 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1508                                      ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1509                                      ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1510                                      ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1511                                         return 0;
1512                                 }
1513                                 cprev = cnow;
1514                         }
1515                         /* NOTREACHED */
1516
1517                 /* 
1518                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1519                  * Return: write counters to the user passed counter struct
1520                  * NB: both 1->0 and 0->1 transitions are counted except for
1521                  *     RI where only 0->1 is counted.
1522                  */
1523                 case TIOCGICOUNT:
1524                         cli();
1525                         cnow = info->icount;
1526                         sti();
1527                         p_cuser = (struct serial_icounter_struct *) arg;
1528                         if (put_user(cnow.cts, &p_cuser->cts) ||
1529                             put_user(cnow.dsr, &p_cuser->dsr) ||
1530                             put_user(cnow.rng, &p_cuser->rng) ||
1531                             put_user(cnow.dcd, &p_cuser->dcd))
1532                                 return -EFAULT;
1533                         return 0;
1534
1535                 default:
1536                         return -ENOIOCTLCMD;
1537                 }
1538         return 0;
1539 }
1540
1541 static void sab82532_set_termios(struct tty_struct *tty,
1542                                  struct termios *old_termios)
1543 {
1544         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1545
1546         if (   (tty->termios->c_cflag == old_termios->c_cflag)
1547             && (   RELEVANT_IFLAG(tty->termios->c_iflag) 
1548                 == RELEVANT_IFLAG(old_termios->c_iflag)))
1549           return;
1550
1551         change_speed(info);
1552
1553         /* Handle transition to B0 status */
1554         if ((old_termios->c_cflag & CBAUD) &&
1555             !(tty->termios->c_cflag & CBAUD)) {
1556                 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1557                 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1558                 writeb(readb(&info->regs->w.pvr) | info->pvr_dtr_bit, &info->regs->w.pvr);
1559         }
1560         
1561         /* Handle transition away from B0 status */
1562         if (!(old_termios->c_cflag & CBAUD) &&
1563             (tty->termios->c_cflag & CBAUD)) {
1564                 writeb(readb(&info->regs->w.pvr) & ~(info->pvr_dtr_bit), &info->regs->w.pvr);
1565                 if (tty->termios->c_cflag & CRTSCTS) {
1566                         writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_RTS), &info->regs->w.mode);
1567                         writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1568                 } else if (test_bit(TTY_THROTTLED, &tty->flags)) {
1569                         writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS | SAB82532_MODE_RTS), &info->regs->w.mode);
1570                 } else {
1571                         writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS), &info->regs->w.mode);
1572                         writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1573                 }
1574         }
1575         
1576         /* Handle turning off CRTSCTS */
1577         if ((old_termios->c_cflag & CRTSCTS) &&
1578             !(tty->termios->c_cflag & CRTSCTS)) {
1579                 tty->hw_stopped = 0;
1580                 sab82532_start(tty);
1581         }
1582 }
1583
1584 /*
1585  * ------------------------------------------------------------
1586  * sab82532_close()
1587  * 
1588  * This routine is called when the serial port gets closed.  First, we
1589  * wait for the last remaining data to be sent.  Then, we unlink its
1590  * async structure from the interrupt chain if necessary, and we free
1591  * that IRQ if nothing is left in the chain.
1592  * ------------------------------------------------------------
1593  */
1594 static void sab82532_close(struct tty_struct *tty, struct file * filp)
1595 {
1596         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1597         unsigned long flags;
1598
1599         if (!info || serial_paranoia_check(info, tty->device, "sab82532_close"))
1600                 return;
1601
1602         save_flags(flags); cli();
1603
1604         if (tty_hung_up_p(filp)) {
1605                 MOD_DEC_USE_COUNT;
1606                 restore_flags(flags);
1607                 return;
1608         }
1609
1610 #ifdef SERIAL_DEBUG_OPEN
1611         printk("sab82532_close ttys%d, count = %d\n", info->line, info->count);
1612 #endif
1613         if ((tty->count == 1) && (info->count != 1)) {
1614                 /*
1615                  * Uh, oh.  tty->count is 1, which means that the tty
1616                  * structure will be freed.  info->count should always
1617                  * be one in these conditions.  If it's greater than
1618                  * one, we've got real problems, since it means the
1619                  * serial port won't be shutdown.
1620                  */
1621                 printk("sab82532_close: bad serial port count; tty->count is 1,"
1622                        " info->count is %d\n", info->count);
1623                 info->count = 1;
1624         }
1625         if (--info->count < 0) {
1626                 printk("sab82532_close: bad serial port count for ttys%d: %d\n",
1627                        info->line, info->count);
1628                 info->count = 0;
1629         }
1630         if (info->count) {
1631                 MOD_DEC_USE_COUNT;
1632                 restore_flags(flags);
1633                 return;
1634         }
1635         info->flags |= ASYNC_CLOSING;
1636         /*
1637          * Save the termios structure, since this port may have
1638          * separate termios for callout and dialin.
1639          */
1640         if (info->flags & ASYNC_NORMAL_ACTIVE)
1641                 info->normal_termios = *tty->termios;
1642         if (info->flags & ASYNC_CALLOUT_ACTIVE)
1643                 info->callout_termios = *tty->termios;
1644         /*
1645          * Now we wait for the transmit buffer to clear; and we notify 
1646          * the line discipline to only process XON/XOFF characters.
1647          */
1648         tty->closing = 1;
1649         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1650                 tty_wait_until_sent(tty, info->closing_wait);
1651
1652         /*
1653          * At this point we stop accepting input.  To do this, we
1654          * disable the receive line status interrupts, and turn off
1655          * the receiver.
1656          */
1657         info->interrupt_mask0 |= SAB82532_IMR0_TCD;
1658         writeb(info->interrupt_mask0, &info->regs->w.imr0);
1659         if (info->flags & ASYNC_INITIALIZED) {
1660                 /*
1661                  * Before we drop DTR, make sure the UART transmitter
1662                  * has completely drained; this is especially
1663                  * important if there is a transmit FIFO!
1664                  */
1665                 sab82532_wait_until_sent(tty, info->timeout);
1666         }
1667         shutdown(info);
1668         if (tty->driver.flush_buffer)
1669                 tty->driver.flush_buffer(tty);
1670         if (tty->ldisc.flush_buffer)
1671                 tty->ldisc.flush_buffer(tty);
1672         tty->closing = 0;
1673         info->event = 0;
1674         info->tty = 0;
1675         if (info->blocked_open) {
1676                 if (info->close_delay) {
1677                         current->state = TASK_INTERRUPTIBLE;
1678                         schedule_timeout(info->close_delay);
1679                 }
1680                 wake_up_interruptible(&info->open_wait);
1681         }
1682         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1683                          ASYNC_CLOSING);
1684         wake_up_interruptible(&info->close_wait);
1685         MOD_DEC_USE_COUNT;
1686         restore_flags(flags);
1687 }
1688
1689 /*
1690  * sab82532_wait_until_sent() --- wait until the transmitter is empty
1691  */
1692 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout)
1693 {
1694         struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1695         unsigned long orig_jiffies, char_time;
1696
1697         if (serial_paranoia_check(info,tty->device,"sab82532_wait_until_sent"))
1698                 return;
1699
1700         /*
1701          * Set the check interval to be 1/5 of the estimated time to
1702          * send a single character, and make it at least 1.  The check
1703          * interval should also be less than the timeout.
1704          * 
1705          * Note: we have to use pretty tight timings here to satisfy
1706          * the NIST-PCTS.
1707          */
1708         char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1709         char_time = char_time / 5;
1710         if (char_time == 0)
1711                 char_time = 1;
1712         if (timeout)
1713           char_time = MIN(char_time, timeout);
1714 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1715         printk("In sab82532_wait_until_sent(%d) check=%lu "
1716                "xmit_cnt = %ld, alls = %d (jiff=%lu)...\n",
1717                timeout, char_time,
1718                CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
1719                test_bit(SAB82532_ALLS, &info->irqflags), jiffies);
1720 #endif
1721         orig_jiffies = jiffies;
1722         while ((info->xmit.head != info->xmit.tail) ||
1723                !test_bit(SAB82532_ALLS, &info->irqflags)) {
1724                 current->state = TASK_INTERRUPTIBLE;
1725                 schedule_timeout(char_time);
1726                 if (signal_pending(current))
1727                         break;
1728                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1729                         break;
1730         }
1731 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1732         printk("xmit_cnt = %ld, alls = %d (jiff=%lu)...done\n",
1733                CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
1734                test_bit(SAB82532_ALLS, &info->irqflags), jiffies);
1735 #endif
1736 }
1737
1738 /*
1739  * sab82532_hangup() --- called by tty_hangup() when a hangup is signaled.
1740  */
1741 static void sab82532_hangup(struct tty_struct *tty)
1742 {
1743         struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1744
1745         if (serial_paranoia_check(info, tty->device, "sab82532_hangup"))
1746                 return;
1747
1748 #ifdef CONFIG_SERIAL_CONSOLE
1749         if (info->is_console)
1750                 return;
1751 #endif
1752
1753         sab82532_flush_buffer(tty);
1754         shutdown(info);
1755         info->event = 0;
1756         info->count = 0;
1757         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1758         info->tty = 0;
1759         wake_up_interruptible(&info->open_wait);
1760 }
1761
1762 /*
1763  * ------------------------------------------------------------
1764  * sab82532_open() and friends
1765  * ------------------------------------------------------------
1766  */
1767 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1768                            struct sab82532 *info)
1769 {
1770         DECLARE_WAITQUEUE(wait, current);
1771         int retval;
1772         int do_clocal = 0;
1773
1774         /*
1775          * If the device is in the middle of being closed, then block
1776          * until it's done, and then try again.
1777          */
1778         if (tty_hung_up_p(filp) ||
1779             (info->flags & ASYNC_CLOSING)) {
1780                 if (info->flags & ASYNC_CLOSING)
1781                         interruptible_sleep_on(&info->close_wait);
1782 #ifdef SERIAL_DO_RESTART
1783                 if (info->flags & ASYNC_HUP_NOTIFY)
1784                         return -EAGAIN;
1785                 else
1786                         return -ERESTARTSYS;
1787 #else
1788                 return -EAGAIN;
1789 #endif
1790         }
1791
1792         /*
1793          * If this is a callout device, then just make sure the normal
1794          * device isn't being used.
1795          */
1796         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1797                 if (info->flags & ASYNC_NORMAL_ACTIVE)
1798                         return -EBUSY;
1799                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1800                     (info->flags & ASYNC_SESSION_LOCKOUT) &&
1801                     (info->session != current->session))
1802                     return -EBUSY;
1803                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1804                     (info->flags & ASYNC_PGRP_LOCKOUT) &&
1805                     (info->pgrp != current->pgrp))
1806                     return -EBUSY;
1807                 info->flags |= ASYNC_CALLOUT_ACTIVE;
1808                 return 0;
1809         }
1810         
1811         /*
1812          * If non-blocking mode is set, or the port is not enabled,
1813          * then make the check up front and then exit.
1814          */
1815         if ((filp->f_flags & O_NONBLOCK) ||
1816             (tty->flags & (1 << TTY_IO_ERROR))) {
1817                 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1818                         return -EBUSY;
1819                 info->flags |= ASYNC_NORMAL_ACTIVE;
1820                 return 0;
1821         }
1822
1823         if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1824                 if (info->normal_termios.c_cflag & CLOCAL)
1825                         do_clocal = 1;
1826         } else {
1827                 if (tty->termios->c_cflag & CLOCAL)
1828                         do_clocal = 1;
1829         }
1830         
1831         /*
1832          * Block waiting for the carrier detect and the line to become
1833          * free (i.e., not in use by the callout).  While we are in
1834          * this loop, info->count is dropped by one, so that
1835          * sab82532_close() knows when to free things.  We restore it upon
1836          * exit, either normal or abnormal.
1837          */
1838         retval = 0;
1839         add_wait_queue(&info->open_wait, &wait);
1840 #ifdef SERIAL_DEBUG_OPEN
1841         printk("block_til_ready before block: ttyS%d, count = %d\n",
1842                info->line, info->count);
1843 #endif
1844         cli();
1845         if (!tty_hung_up_p(filp)) 
1846                 info->count--;
1847         sti();
1848         info->blocked_open++;
1849         while (1) {
1850                 cli();
1851                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1852                     (tty->termios->c_cflag & CBAUD)) {
1853                         writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1854                         writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1855                         writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1856                 }
1857                 sti();
1858                 set_current_state(TASK_INTERRUPTIBLE);
1859                 if (tty_hung_up_p(filp) ||
1860                     !(info->flags & ASYNC_INITIALIZED)) {
1861 #ifdef SERIAL_DO_RESTART
1862                         if (info->flags & ASYNC_HUP_NOTIFY)
1863                                 retval = -EAGAIN;
1864                         else
1865                                 retval = -ERESTARTSYS;  
1866 #else
1867                         retval = -EAGAIN;
1868 #endif
1869                         break;
1870                 }
1871                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1872                     !(info->flags & ASYNC_CLOSING) &&
1873                     (do_clocal || !(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD)))
1874                         break;
1875                 if (signal_pending(current)) {
1876                         retval = -ERESTARTSYS;
1877                         break;
1878                 }
1879 #ifdef SERIAL_DEBUG_OPEN
1880                 printk("block_til_ready blocking: ttyS%d, count = %d, flags = %x, clocal = %d, vstr = %02x\n",
1881                        info->line, info->count, info->flags, do_clocal, readb(&info->regs->r.vstr));
1882 #endif
1883                 schedule();
1884         }
1885         current->state = TASK_RUNNING;
1886         remove_wait_queue(&info->open_wait, &wait);
1887         if (!tty_hung_up_p(filp))
1888                 info->count++;
1889         info->blocked_open--;
1890 #ifdef SERIAL_DEBUG_OPEN
1891         printk("block_til_ready after blocking: ttys%d, count = %d\n",
1892                info->line, info->count);
1893 #endif
1894         if (retval)
1895                 return retval;
1896         info->flags |= ASYNC_NORMAL_ACTIVE;
1897         return 0;
1898 }
1899
1900 /*
1901  * This routine is called whenever a serial port is opened.  It
1902  * enables interrupts for a serial port, linking in its async structure into
1903  * the IRQ chain.   It also performs the serial-specific
1904  * initialization for the tty structure.
1905  */
1906 static int sab82532_open(struct tty_struct *tty, struct file * filp)
1907 {
1908         struct sab82532 *info = sab82532_chain;
1909         int retval, line;
1910         unsigned long page;
1911
1912 #ifdef SERIAL_DEBUG_OPEN
1913         printk("sab82532_open: count = %d\n", info->count);
1914 #endif
1915
1916         line = MINOR(tty->device) - tty->driver.minor_start;
1917         if ((line < 0) || (line >= NR_PORTS))
1918                 return -ENODEV;
1919
1920         while (info) {
1921                 if (info->line == line)
1922                         break;
1923                 info = info->next;
1924         }
1925         if (!info) {
1926                 printk("sab82532_open: can't find info for line %d\n",
1927                        line);
1928                 return -ENODEV;
1929         }
1930
1931         if (serial_paranoia_check(info, tty->device, "sab82532_open"))
1932                 return -ENODEV;
1933
1934 #ifdef SERIAL_DEBUG_OPEN
1935         printk("sab82532_open %s%d, count = %d\n", tty->driver.name, info->line,
1936                info->count);
1937 #endif
1938
1939         if (!tmp_buf) {
1940                 page = get_free_page(GFP_KERNEL);
1941                 if (!page)
1942                         return -ENOMEM;
1943                 if (tmp_buf)
1944                         free_page(page);
1945                 else
1946                         tmp_buf = (unsigned char *) page;
1947         }
1948
1949         info->count++;
1950         tty->driver_data = info;
1951         info->tty = tty;
1952
1953         /*
1954          * If the port is in the middle of closing, bail out now.
1955          */
1956         if (tty_hung_up_p(filp) ||
1957             (info->flags & ASYNC_CLOSING)) {
1958                 if (info->flags & ASYNC_CLOSING)
1959                         interruptible_sleep_on(&info->close_wait);
1960 #ifdef SERIAL_DO_RESTART
1961                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1962                         -EAGAIN : -ERESTARTSYS);
1963 #else
1964                 return -EAGAIN;
1965 #endif
1966         }
1967
1968         /*
1969          * Start up serial port
1970          */
1971         retval = startup(info);
1972         if (retval)
1973                 return retval;
1974
1975         MOD_INC_USE_COUNT;
1976         retval = block_til_ready(tty, filp, info);
1977         if (retval) {
1978 #ifdef SERIAL_DEBUG_OPEN
1979                 printk("sab82532_open returning after block_til_ready with %d\n",
1980                        retval);
1981 #endif
1982                 return retval;
1983         }
1984
1985         if ((info->count == 1) &&
1986             (info->flags & ASYNC_SPLIT_TERMIOS)) {
1987                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1988                         *tty->termios = info->normal_termios;
1989                 else 
1990                         *tty->termios = info->callout_termios;
1991                 change_speed(info);
1992         }
1993
1994 #ifdef CONFIG_SERIAL_CONSOLE
1995         if (sab82532_console.cflag && sab82532_console.index == line) {
1996                 tty->termios->c_cflag = sab82532_console.cflag;
1997                 sab82532_console.cflag = 0;
1998                 change_speed(info);
1999         }
2000 #endif
2001
2002         info->session = current->session;
2003         info->pgrp = current->pgrp;
2004
2005 #ifdef SERIAL_DEBUG_OPEN
2006         printk("sab82532_open ttys%d successful... count %d", info->line, info->count);
2007 #endif
2008         return 0;
2009 }
2010
2011 /*
2012  * /proc fs routines....
2013  */
2014
2015 static __inline__ int
2016 line_info(char *buf, struct sab82532 *info)
2017 {
2018         unsigned long flags;
2019         char stat_buf[30];
2020         int ret;
2021
2022         ret = sprintf(buf, "%u: uart:SAB82532 ", info->line);
2023         switch (info->type) {
2024                 case 0:
2025                         ret += sprintf(buf+ret, "V1.0 ");
2026                         break;
2027                 case 1:
2028                         ret += sprintf(buf+ret, "V2.0 ");
2029                         break;
2030                 case 2:
2031                         ret += sprintf(buf+ret, "V3.2 ");
2032                         break;
2033                 default:
2034                         ret += sprintf(buf+ret, "V?.? ");
2035                         break;
2036         }
2037         ret += sprintf(buf+ret, "port:%lX irq:%s",
2038                        (unsigned long)info->regs, __irq_itoa(info->irq));
2039
2040         if (!info->regs) {
2041                 ret += sprintf(buf+ret, "\n");
2042                 return ret;
2043         }
2044
2045         /*
2046          * Figure out the current RS-232 lines
2047          */
2048         stat_buf[0] = 0;
2049         stat_buf[1] = 0;
2050         save_flags(flags); cli();
2051         if (readb(&info->regs->r.mode) & SAB82532_MODE_RTS) {
2052                 if (!(readb(&info->regs->r.mode) & SAB82532_MODE_FRTS))
2053                         strcat(stat_buf, "|RTS");
2054         } else {
2055                 strcat(stat_buf, "|RTS");
2056         }
2057         if (readb(&info->regs->r.star) & SAB82532_STAR_CTS)
2058                 strcat(stat_buf, "|CTS");
2059         if (!(readb(&info->regs->r.pvr) & info->pvr_dtr_bit))
2060                 strcat(stat_buf, "|DTR");
2061         if (!(readb(&info->regs->r.pvr) & info->pvr_dsr_bit))
2062                 strcat(stat_buf, "|DSR");
2063         if (!(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD))
2064                 strcat(stat_buf, "|CD");
2065         restore_flags(flags);
2066
2067         if (info->baud)
2068                 ret += sprintf(buf+ret, " baud:%u", info->baud);
2069
2070         ret += sprintf(buf+ret, " tx:%u rx:%u",
2071                        info->icount.tx, info->icount.rx);
2072
2073         if (info->icount.frame)
2074                 ret += sprintf(buf+ret, " fe:%u", info->icount.frame);
2075
2076         if (info->icount.parity)
2077                 ret += sprintf(buf+ret, " pe:%u", info->icount.parity);
2078
2079         if (info->icount.brk)
2080                 ret += sprintf(buf+ret, " brk:%u", info->icount.brk);
2081
2082         if (info->icount.overrun)
2083                 ret += sprintf(buf+ret, " oe:%u", info->icount.overrun);
2084
2085         /*
2086          * Last thing is the RS-232 status lines.
2087          */
2088         ret += sprintf(buf+ret, " %s\n", stat_buf + 1);
2089         return ret;
2090 }
2091
2092 int sab82532_read_proc(char *page, char **start, off_t off, int count,
2093                        int *eof, void *data)
2094 {
2095         struct sab82532 *info = sab82532_chain;
2096         off_t begin = 0;
2097         int len = 0;
2098
2099         len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2100         for (info = sab82532_chain; info && len < 4000; info = info->next) {
2101                 len += line_info(page + len, info);
2102                 if (len+begin > off+count)
2103                         goto done;
2104                 if (len+begin < off) {
2105                         begin += len;
2106                         len = 0;
2107                 }
2108         }
2109         *eof = 1;
2110 done:
2111         if (off >= len+begin)
2112                 return 0;
2113         *start = page + (off-begin);
2114         return ((count < begin+len-off) ? count : begin+len-off);
2115 }
2116
2117 /*
2118  * ---------------------------------------------------------------------
2119  * sab82532_init() and friends
2120  *
2121  * sab82532_init() is called at boot-time to initialize the serial driver.
2122  * ---------------------------------------------------------------------
2123  */
2124 static int __init get_sab82532(unsigned long *memory_start)
2125 {
2126         struct linux_ebus *ebus;
2127         struct linux_ebus_device *edev = 0;
2128         struct sab82532 *sab;
2129         unsigned long regs, offset;
2130         int i;
2131
2132         for_each_ebus(ebus) {
2133                 for_each_ebusdev(edev, ebus) {
2134                         if (!strcmp(edev->prom_name, "se"))
2135                                 goto ebus_done;
2136
2137                         if (!strcmp(edev->prom_name, "serial")) {
2138                                 char compat[32];
2139                                 int clen;
2140
2141                                 /* On RIO this can be an SE, check it.  We could
2142                                  * just check ebus->is_rio, but this is more portable.
2143                                  */
2144                                 clen = prom_getproperty(edev->prom_node, "compatible",
2145                                                         compat, sizeof(compat));
2146                                 if (clen > 0) {
2147                                         if (strncmp(compat, "sab82532", 8) == 0) {
2148                                                 /* Yep. */
2149                                                 goto ebus_done;
2150                                         }
2151                                 }
2152                         }
2153                 }
2154         }
2155 ebus_done:
2156         if (!edev)
2157                 return -ENODEV;
2158
2159         regs = edev->resource[0].start;
2160         offset = sizeof(union sab82532_async_regs);
2161
2162         for (i = 0; i < 2; i++) {
2163                 if (memory_start) {
2164                         *memory_start = (*memory_start + 7) & ~(7);
2165                         sab = (struct sab82532 *)*memory_start;
2166                         *memory_start += sizeof(struct sab82532);
2167                 } else {
2168                         sab = (struct sab82532 *)kmalloc(sizeof(struct sab82532),
2169                                                          GFP_KERNEL);
2170                         if (!sab) {
2171                                 printk("sab82532: can't alloc sab struct\n");
2172                                 break;
2173                         }
2174                 }
2175                 memset(sab, 0, sizeof(struct sab82532));
2176
2177                 sab->regs = ioremap(regs + offset, sizeof(union sab82532_async_regs));
2178                 sab->irq = edev->irqs[0];
2179                 sab->line = 1 - i;
2180                 sab->xmit_fifo_size = 32;
2181                 sab->recv_fifo_size = 32;
2182
2183                 writeb(SAB82532_IPC_IC_ACT_LOW, &sab->regs->w.ipc);
2184
2185                 sab->next = sab82532_chain;
2186                 sab82532_chain = sab;
2187
2188                 offset -= sizeof(union sab82532_async_regs);
2189         }
2190         return 0;
2191 }
2192
2193 #ifndef MODULE
2194 static void __init sab82532_kgdb_hook(int line)
2195 {
2196         prom_printf("sab82532: kgdb support is not implemented, yet\n");
2197         prom_halt();
2198 }
2199 #endif
2200
2201 static inline void __init show_serial_version(void)
2202 {
2203         char *revision = "$Revision: 1.1.1.1 $";
2204         char *version, *p;
2205
2206         version = strchr(revision, ' ');
2207         strcpy(serial_version, ++version);
2208         p = strchr(serial_version, ' ');
2209         *p = '\0';
2210         printk("SAB82532 serial driver version %s\n", serial_version);
2211 }
2212
2213 extern int su_num_ports;
2214
2215 /*
2216  * The serial driver boot-time initialization code!
2217  */
2218 int __init sab82532_init(void)
2219 {
2220         struct sab82532 *info;
2221         int i;
2222
2223         if (!sab82532_chain)
2224                 get_sab82532(0);
2225         if (!sab82532_chain)
2226                 return -ENODEV;
2227
2228         init_bh(SERIAL_BH, do_serial_bh);
2229
2230         show_serial_version();
2231
2232         /* Initialize the tty_driver structure */
2233         memset(&serial_driver, 0, sizeof(struct tty_driver));
2234         serial_driver.magic = TTY_DRIVER_MAGIC;
2235         serial_driver.driver_name = "serial";
2236 #ifdef CONFIG_DEVFS_FS
2237         serial_driver.name = "tts/%d";
2238 #else
2239         serial_driver.name = "ttyS";
2240 #endif
2241         serial_driver.major = TTY_MAJOR;
2242         serial_driver.minor_start = 64 + su_num_ports;
2243         serial_driver.num = NR_PORTS;
2244         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2245         serial_driver.subtype = SERIAL_TYPE_NORMAL;
2246         serial_driver.init_termios = tty_std_termios;
2247         serial_driver.init_termios.c_cflag =
2248                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2249         serial_driver.flags = TTY_DRIVER_REAL_RAW;
2250         serial_driver.refcount = &sab82532_refcount;
2251         serial_driver.table = sab82532_table;
2252         serial_driver.termios = sab82532_termios;
2253         serial_driver.termios_locked = sab82532_termios_locked;
2254
2255         serial_driver.open = sab82532_open;
2256         serial_driver.close = sab82532_close;
2257         serial_driver.write = sab82532_write;
2258         serial_driver.put_char = sab82532_put_char;
2259         serial_driver.flush_chars = sab82532_flush_chars;
2260         serial_driver.write_room = sab82532_write_room;
2261         serial_driver.chars_in_buffer = sab82532_chars_in_buffer;
2262         serial_driver.flush_buffer = sab82532_flush_buffer;
2263         serial_driver.ioctl = sab82532_ioctl;
2264         serial_driver.throttle = sab82532_throttle;
2265         serial_driver.unthrottle = sab82532_unthrottle;
2266         serial_driver.send_xchar = sab82532_send_xchar;
2267         serial_driver.set_termios = sab82532_set_termios;
2268         serial_driver.stop = sab82532_stop;
2269         serial_driver.start = sab82532_start;
2270         serial_driver.hangup = sab82532_hangup;
2271         serial_driver.break_ctl = sab82532_break;
2272         serial_driver.wait_until_sent = sab82532_wait_until_sent;
2273         serial_driver.read_proc = sab82532_read_proc;
2274
2275         /*
2276          * The callout device is just like normal device except for
2277          * major number and the subtype code.
2278          */
2279         callout_driver = serial_driver;
2280 #ifdef CONFIG_DEVFS_FS
2281         callout_driver.name = "cua/%d";
2282 #else
2283         callout_driver.name = "cua";
2284 #endif
2285         callout_driver.major = TTYAUX_MAJOR;
2286         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2287         callout_driver.read_proc = 0;
2288         callout_driver.proc_entry = 0;
2289
2290         if (tty_register_driver(&serial_driver))
2291                 panic("Couldn't register serial driver\n");
2292         if (tty_register_driver(&callout_driver))
2293                 panic("Couldn't register callout driver\n");
2294
2295         for (info = sab82532_chain, i = 0; info; info = info->next, i++) {
2296                 info->magic = SERIAL_MAGIC;
2297
2298                 info->type = readb(&info->regs->r.vstr) & 0x0f;
2299                 writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &info->regs->w.pcr);
2300                 writeb(0xff, &info->regs->w.pim);
2301                 if (info->line == 0) {
2302                         info->pvr_dsr_bit = (1 << 0);
2303                         info->pvr_dtr_bit = (1 << 1);
2304                 } else {
2305                         info->pvr_dsr_bit = (1 << 3);
2306                         info->pvr_dtr_bit = (1 << 2);
2307                 }
2308                 writeb((1 << 1) | (1 << 2) | (1 << 4), &info->regs->w.pvr);
2309                 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
2310                 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
2311
2312                 info->custom_divisor = 16;
2313                 info->close_delay = 5*HZ/10;
2314                 info->closing_wait = 30*HZ;
2315                 info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
2316                 info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
2317                 info->x_char = 0;
2318                 info->event = 0;        
2319                 info->blocked_open = 0;
2320                 info->tqueue.routine = do_softint;
2321                 info->tqueue.data = info;
2322                 info->tqueue_hangup.routine = do_serial_hangup;
2323                 info->tqueue_hangup.data = info;
2324                 info->callout_termios = callout_driver.init_termios;
2325                 info->normal_termios = serial_driver.init_termios;
2326                 init_waitqueue_head(&info->open_wait);
2327                 init_waitqueue_head(&info->close_wait);
2328                 init_waitqueue_head(&info->delta_msr_wait);
2329                 info->icount.cts = info->icount.dsr = 
2330                         info->icount.rng = info->icount.dcd = 0;
2331                 info->icount.rx = info->icount.tx = 0;
2332                 info->icount.frame = info->icount.parity = 0;
2333                 info->icount.overrun = info->icount.brk = 0;
2334
2335                 if (!(info->line & 0x01)) {
2336                         if (request_irq(info->irq, sab82532_interrupt, SA_SHIRQ,
2337                                         "serial(sab82532)", info)) {
2338                                 printk("sab82532: can't get IRQ %x\n",
2339                                        info->irq);
2340                                 panic("sab82532 initialization failed");
2341                         }
2342                 }
2343         
2344                 printk(KERN_INFO
2345                        "ttyS%02d at 0x%lx (irq = %s) is a SAB82532 %s\n",
2346                        info->line + su_num_ports, (unsigned long)info->regs,
2347                        __irq_itoa(info->irq), sab82532_version[info->type]);
2348         }
2349
2350 #ifdef SERIAL_LOG_DEVICE
2351         dprint_init(SERIAL_LOG_DEVICE);
2352 #endif
2353         return 0;
2354 }
2355
2356 int __init sab82532_probe(void)
2357 {
2358         int node, enode, snode;
2359         char model[32];
2360         int len;
2361
2362         node = prom_getchild(prom_root_node);
2363         node = prom_searchsiblings(node, "pci");
2364
2365         /*
2366          * Check for SUNW,sabre on Ultra 5/10/AXi.
2367          */
2368         len = prom_getproperty(node, "model", model, sizeof(model));
2369         if ((len > 0) && !strncmp(model, "SUNW,sabre", len)) {
2370                 node = prom_getchild(node);
2371                 node = prom_searchsiblings(node, "pci");
2372         }
2373
2374         /*
2375          * For each PCI bus...
2376          */
2377         while (node) {
2378                 enode = prom_getchild(node);
2379                 enode = prom_searchsiblings(enode, "ebus");
2380
2381                 /*
2382                  * For each EBus on this PCI...
2383                  */
2384                 while (enode) {
2385                         int child;
2386
2387                         child = prom_getchild(enode);
2388                         snode = prom_searchsiblings(child, "se");
2389                         if (snode)
2390                                 goto found;
2391
2392                         snode = prom_searchsiblings(child, "serial");
2393                         if (snode) {
2394                                 char compat[32];
2395                                 int clen;
2396
2397                                 clen = prom_getproperty(snode, "compatible",
2398                                                         compat, sizeof(compat));
2399                                 if (clen > 0) {
2400                                         if (strncmp(compat, "sab82532", 8) == 0)
2401                                                 goto found;
2402                                 }
2403                         }
2404
2405                         enode = prom_getsibling(enode);
2406                         enode = prom_searchsiblings(enode, "ebus");
2407                 }
2408                 node = prom_getsibling(node);
2409                 node = prom_searchsiblings(node, "pci");
2410         }
2411         return -ENODEV;
2412
2413 found:
2414 #ifdef CONFIG_SERIAL_CONSOLE
2415         sunserial_setinitfunc(sab82532_console_init);
2416 #endif
2417 #ifndef MODULE
2418         sunserial_setinitfunc(sab82532_init);
2419         rs_ops.rs_kgdb_hook = sab82532_kgdb_hook;
2420 #endif
2421         return 0;
2422 }
2423
2424 #ifdef MODULE
2425 MODULE_LICENSE("GPL");
2426
2427 int init_module(void)
2428 {
2429         if (get_sab82532(0))
2430                 return -ENODEV;
2431
2432         return sab82532_init();
2433 }
2434
2435 void cleanup_module(void) 
2436 {
2437         struct sab82532 *sab;
2438         unsigned long flags;
2439         int e1, e2;
2440
2441         /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2442         save_flags(flags);
2443         cli();
2444         remove_bh(SERIAL_BH);
2445         if ((e1 = tty_unregister_driver(&serial_driver)))
2446                 printk("SERIAL: failed to unregister serial driver (%d)\n",
2447                        e1);
2448         if ((e2 = tty_unregister_driver(&callout_driver)))
2449                 printk("SERIAL: failed to unregister callout driver (%d)\n", 
2450                        e2);
2451         restore_flags(flags);
2452
2453         if (tmp_buf) {
2454                 free_page((unsigned long) tmp_buf);
2455                 tmp_buf = NULL;
2456         }
2457         for (sab = sab82532_chain; sab; sab = sab->next) {
2458                 if (!(sab->line & 0x01))
2459                         free_irq(sab->irq, sab);
2460                 iounmap(sab->regs);
2461         }
2462 }
2463 #endif /* MODULE */
2464
2465 #ifdef CONFIG_SERIAL_CONSOLE
2466 static void
2467 batten_down_hatches(struct sab82532 *info)
2468 {
2469         unsigned char saved_rfc, tmp;
2470
2471         if (!stop_a_enabled)
2472                 return;
2473
2474         /* If we are doing kadb, we call the debugger
2475          * else we just drop into the boot monitor.
2476          * Note that we must flush the user windows
2477          * first before giving up control.
2478          */
2479         printk("\n");
2480         flush_user_windows();
2481
2482         /*
2483          * Set FIFO to single character mode.
2484          */
2485         saved_rfc = readb(&info->regs->r.rfc);
2486         tmp = readb(&info->regs->rw.rfc);
2487         tmp &= ~(SAB82532_RFC_RFDF);
2488         writeb(tmp, &info->regs->rw.rfc);
2489         sab82532_cec_wait(info);
2490         writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2491
2492 #ifndef __sparc_v9__
2493         if ((((unsigned long)linux_dbvec) >= DEBUG_FIRSTVADDR) &&
2494             (((unsigned long)linux_dbvec) <= DEBUG_LASTVADDR))
2495                 sp_enter_debugger();
2496         else
2497 #endif
2498                 prom_cmdline();
2499
2500         /*
2501          * Reset FIFO to character + status mode.
2502          */
2503         writeb(saved_rfc, &info->regs->w.rfc);
2504         sab82532_cec_wait(info);
2505         writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2506 }
2507
2508 static __inline__ void
2509 sab82532_console_putchar(struct sab82532 *info, char c)
2510 {
2511         unsigned long flags;
2512
2513         save_flags(flags); cli();
2514         sab82532_tec_wait(info);
2515         writeb(c, &info->regs->w.tic);
2516         restore_flags(flags);
2517 }
2518
2519 static void
2520 sab82532_console_write(struct console *con, const char *s, unsigned n)
2521 {
2522         struct sab82532 *info;
2523         int i;
2524
2525         info = sab82532_chain;
2526         for (i = con->index; i; i--) {
2527                 info = info->next;
2528                 if (!info)
2529                         return;
2530         }
2531
2532         for (i = 0; i < n; i++) {
2533                 if (*s == '\n')
2534                         sab82532_console_putchar(info, '\r');
2535                 sab82532_console_putchar(info, *s++);
2536         }
2537         sab82532_tec_wait(info);
2538 }
2539
2540 static kdev_t
2541 sab82532_console_device(struct console *con)
2542 {
2543         return MKDEV(TTY_MAJOR, 64 + con->index);
2544 }
2545
2546 static int
2547 sab82532_console_setup(struct console *con, char *options)
2548 {
2549         static struct tty_struct c_tty;
2550         static struct termios c_termios;
2551         struct sab82532 *info;
2552         tcflag_t        cflag;
2553         int             i;
2554
2555         info = sab82532_chain;
2556         for (i = con->index; i; i--) {
2557                 info = info->next;
2558                 if (!info)
2559                         return -ENODEV;
2560         }
2561         info->is_console = 1;
2562
2563         /*
2564          * Initialize the hardware
2565          */
2566         sab82532_init_line(info);
2567
2568         /*
2569          * Finally, enable interrupts
2570          */
2571         info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
2572                                 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
2573         writeb(info->interrupt_mask0, &info->regs->w.imr0);
2574         info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
2575                                 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
2576                                 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
2577                                 SAB82532_IMR1_XPR;
2578         writeb(info->interrupt_mask1, &info->regs->w.imr1);
2579
2580         printk("Console: ttyS%d (SAB82532)\n", info->line);
2581
2582         sunserial_console_termios(con);
2583         cflag = con->cflag;
2584
2585         /*
2586          * Fake up the tty and tty->termios structures so we can use
2587          * change_speed (and eliminate a lot of duplicate code).
2588          */
2589         if (!info->tty) {
2590                 memset(&c_tty, 0, sizeof(c_tty));
2591                 info->tty = &c_tty;
2592         }
2593         if (!info->tty->termios) {
2594                 memset(&c_termios, 0, sizeof(c_termios));
2595                 info->tty->termios = &c_termios;
2596         }
2597         info->tty->termios->c_cflag = con->cflag;
2598
2599         change_speed(info);
2600
2601         /* Now take out the pointers to static structures if necessary */
2602         if (info->tty->termios == &c_termios)
2603                 info->tty->termios = 0;
2604         if (info->tty == &c_tty)
2605                 info->tty = 0;
2606         
2607         return 0;
2608 }
2609
2610 static struct console sab82532_console = {
2611         name:           "ttyS",
2612         write:          sab82532_console_write,
2613         device:         sab82532_console_device,
2614         setup:          sab82532_console_setup,
2615         flags:          CON_PRINTBUFFER,
2616         index:          -1,
2617 };
2618
2619 int __init sab82532_console_init(void)
2620 {
2621         extern int con_is_present(void);
2622         extern int su_console_registered;
2623
2624         if (con_is_present() || su_console_registered)
2625                 return 0;
2626
2627         if (!sab82532_chain) {
2628                 prom_printf("sab82532_console_setup: can't get SAB82532 chain");
2629                 prom_halt();
2630         }
2631
2632         sab82532_console.index = serial_console - 1;
2633         register_console(&sab82532_console);
2634         return 0;
2635 }
2636
2637 #ifdef SERIAL_LOG_DEVICE
2638
2639 static int serial_log_device = 0;
2640
2641 static void
2642 dprint_init(int tty)
2643 {
2644         serial_console = tty + 1;
2645         sab82532_console.index = tty;
2646         sab82532_console_setup(&sab82532_console, "");
2647         serial_console = 0;
2648         serial_log_device = tty + 1;
2649 }
2650
2651 int
2652 dprintf(const char *fmt, ...)
2653 {
2654         static char buffer[4096];
2655         va_list args;
2656         int i;
2657
2658         if (!serial_log_device)
2659                 return 0;
2660
2661         va_start(args, fmt);
2662         i = vsprintf(buffer, fmt, args);
2663         va_end(args);
2664         sab82532_console.write(&sab82532_console, buffer, i);
2665         return i;
2666 }
2667 #endif /* SERIAL_LOG_DEVICE */
2668 #endif /* CONFIG_SERIAL_CONSOLE */