setup enviroment for compilation
[linux-2.4.21-pre4.git] / drivers / tc / zs.c
1 /*
2  * decserial.c: Serial port driver for IOASIC DECstations.
3  *
4  * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5  * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
6  *
7  * DECstation changes
8  * Copyright (C) 1998-2000 Harald Koerfgen
9  * Copyright (C) 2000, 2001, 2002  Maciej W. Rozycki <macro@ds2.pg.gda.pl>
10  *
11  * For the rest of the code the original Copyright applies:
12  * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
13  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
14  *
15  *
16  * Note: for IOASIC systems the wiring is as follows:
17  *
18  * mouse/keyboard:
19  * DIN-7 MJ-4  signal        SCC
20  * 2     1     TxD       <-  A.TxD
21  * 3     4     RxD       ->  A.RxD
22  *
23  * EIA-232/EIA-423:
24  * DB-25 MMJ-6 signal        SCC
25  * 2     2     TxD       <-  B.TxD
26  * 3     5     RxD       ->  B.RxD
27  * 4           RTS       <- ~A.RTS
28  * 5           CTS       -> ~B.CTS
29  * 6     6     DSR       -> ~A.SYNC
30  * 8           CD        -> ~B.DCD
31  * 12          DSRS(DCE) -> ~A.CTS  (*)
32  * 15          TxC       ->  B.TxC
33  * 17          RxC       ->  B.RxC
34  * 20    1     DTR       <- ~A.DTR
35  * 22          RI        -> ~A.DCD
36  * 23          DSRS(DTE) <- ~B.RTS
37  *
38  * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
39  *     is shared with DSRS(DTE) at pin 23.
40  */
41
42 #include <linux/config.h>
43 #include <linux/version.h>
44 #include <linux/errno.h>
45 #include <linux/signal.h>
46 #include <linux/sched.h>
47 #include <linux/timer.h>
48 #include <linux/interrupt.h>
49 #include <linux/tty.h>
50 #include <linux/tty_flip.h>
51 #include <linux/major.h>
52 #include <linux/string.h>
53 #include <linux/fcntl.h>
54 #include <linux/mm.h>
55 #include <linux/kernel.h>
56 #include <linux/delay.h>
57 #include <linux/init.h>
58 #include <linux/ioport.h>
59 #ifdef CONFIG_SERIAL_CONSOLE
60 #include <linux/console.h>
61 #endif
62
63 #include <asm/io.h>
64 #include <asm/pgtable.h>
65 #include <asm/irq.h>
66 #include <asm/system.h>
67 #include <asm/segment.h>
68 #include <asm/bitops.h>
69 #include <asm/uaccess.h>
70 #include <asm/bootinfo.h>
71 #ifdef CONFIG_DECSTATION
72 #include <asm/dec/interrupts.h>
73 #include <asm/dec/machtype.h>
74 #include <asm/dec/tc.h>
75 #include <asm/dec/ioasic_addrs.h>
76 #endif
77 #ifdef CONFIG_BAGET_MIPS
78 #include <asm/baget/baget.h>
79 unsigned long system_base;
80 #endif
81 #ifdef CONFIG_KGDB
82 #include <asm/kgdb.h>
83 #endif
84 #ifdef CONFIG_MAGIC_SYSRQ
85 #include <linux/sysrq.h>
86 #endif
87
88 #include "zs.h"
89
90 /*
91  * It would be nice to dynamically allocate everything that
92  * depends on NUM_SERIAL, so we could support any number of
93  * Z8530s, but for now...
94  */
95 #define NUM_SERIAL      2               /* Max number of ZS chips supported */
96 #define NUM_CHANNELS    (NUM_SERIAL * 2)        /* 2 channels per chip */
97 #define CHANNEL_A_NR  (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
98                                         /* Number of channel A in the chip */
99 #define ZS_CHAN_IO_SIZE 8
100 #define ZS_CLOCK        7372800         /* Z8530 RTxC input clock rate */
101
102 #define RECOVERY_DELAY  udelay(2)
103
104 struct zs_parms {
105         unsigned long scc0;
106         unsigned long scc1;
107         int channel_a_offset;
108         int channel_b_offset;
109         int irq0;
110         int irq1;
111         int clock;
112 };
113
114 static struct zs_parms *zs_parms;
115
116 #ifdef CONFIG_DECSTATION
117 static struct zs_parms ds_parms = {
118         scc0 : SCC0,
119         scc1 : SCC1,
120         channel_a_offset : 1,
121         channel_b_offset : 9,
122         irq0 : -1,
123         irq1 : -1,
124         clock : ZS_CLOCK
125 };
126 #endif
127 #ifdef CONFIG_BAGET_MIPS
128 static struct zs_parms baget_parms = {
129         scc0 : UNI_SCC0,
130         scc1 : UNI_SCC1,
131         channel_a_offset : 9,
132         channel_b_offset : 1,
133         irq0 : BAGET_SCC_IRQ,
134         irq1 : BAGET_SCC_IRQ,
135         clock : 14745000
136 };
137 #endif
138
139 #ifdef CONFIG_DECSTATION
140 #define DS_BUS_PRESENT (IOASIC)
141 #else
142 #define DS_BUS_PRESENT 0
143 #endif
144
145 #ifdef CONFIG_BAGET_MIPS
146 #define BAGET_BUS_PRESENT (mips_machtype == MACH_BAGET202)
147 #else
148 #define BAGET_BUS_PRESENT 0
149 #endif
150
151 #define BUS_PRESENT (DS_BUS_PRESENT || BAGET_BUS_PRESENT)
152
153 struct dec_zschannel zs_channels[NUM_CHANNELS];
154 struct dec_serial zs_soft[NUM_CHANNELS];
155 int zs_channels_found;
156 struct dec_serial *zs_chain;    /* list of all channels */
157
158 struct tty_struct zs_ttys[NUM_CHANNELS];
159
160 #ifdef CONFIG_SERIAL_CONSOLE
161 static struct console sercons;
162 #endif
163 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) \
164     && !defined(MODULE)
165 static unsigned long break_pressed; /* break, really ... */
166 #endif
167
168 static unsigned char zs_init_regs[16] __initdata = {
169         0,                           /* write 0 */
170         0,                           /* write 1 */
171         0xf0,                        /* write 2 */
172         (Rx8),                       /* write 3 */
173         (X16CLK | SB1),              /* write 4 */
174         (Tx8),                       /* write 5 */
175         0, 0, 0,                     /* write 6, 7, 8 */
176         (VIS),                       /* write 9 */
177         (NRZ),                       /* write 10 */
178         (TCBR | RCBR),               /* write 11 */
179         0, 0,                        /* BRG time constant, write 12 + 13 */
180         (BRSRC | BRENABL),           /* write 14 */
181         0                            /* write 15 */
182 };
183
184 DECLARE_TASK_QUEUE(tq_zs_serial);
185
186 struct tty_driver serial_driver, callout_driver;
187 static int serial_refcount;
188
189 /* serial subtype definitions */
190 #define SERIAL_TYPE_NORMAL      1
191 #define SERIAL_TYPE_CALLOUT     2
192
193 /* number of characters left in xmit buffer before we ask for more */
194 #define WAKEUP_CHARS 256
195
196 /*
197  * Debugging.
198  */
199 #undef SERIAL_DEBUG_INTR
200 #undef SERIAL_DEBUG_OPEN
201 #undef SERIAL_DEBUG_FLOW
202 #undef SERIAL_DEBUG_THROTTLE
203 #undef SERIAL_PARANOIA_CHECK
204
205 #undef ZS_DEBUG_REGS
206
207 #ifdef SERIAL_DEBUG_THROTTLE
208 #define _tty_name(tty,buf) tty_name(tty,buf)
209 #endif
210
211 #define RS_STROBE_TIME 10
212 #define RS_ISR_PASS_LIMIT 256
213
214 #define _INLINE_ inline
215
216 static void probe_sccs(void);
217 static void change_speed(struct dec_serial *info);
218 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
219
220 static struct tty_struct *serial_table[NUM_CHANNELS];
221 static struct termios *serial_termios[NUM_CHANNELS];
222 static struct termios *serial_termios_locked[NUM_CHANNELS];
223
224 #ifndef MIN
225 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
226 #endif
227
228 /*
229  * tmp_buf is used as a temporary buffer by serial_write.  We need to
230  * lock it in case the copy_from_user blocks while swapping in a page,
231  * and some other program tries to do a serial write at the same time.
232  * Since the lock will only come under contention when the system is
233  * swapping and available memory is low, it makes sense to share one
234  * buffer across all the serial ports, since it significantly saves
235  * memory if large numbers of serial ports are open.
236  */
237 static unsigned char tmp_buf[4096]; /* This is cheating */
238 static DECLARE_MUTEX(tmp_buf_sem);
239
240 static inline int serial_paranoia_check(struct dec_serial *info,
241                                         dev_t device, const char *routine)
242 {
243 #ifdef SERIAL_PARANOIA_CHECK
244         static const char *badmagic =
245                 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
246         static const char *badinfo =
247                 "Warning: null mac_serial for (%d, %d) in %s\n";
248
249         if (!info) {
250                 printk(badinfo, MAJOR(device), MINOR(device), routine);
251                 return 1;
252         }
253         if (info->magic != SERIAL_MAGIC) {
254                 printk(badmagic, MAJOR(device), MINOR(device), routine);
255                 return 1;
256         }
257 #endif
258         return 0;
259 }
260
261 /*
262  * This is used to figure out the divisor speeds and the timeouts
263  */
264 static int baud_table[] = {
265         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
266         9600, 19200, 38400, 57600, 115200, 0 };
267
268 /*
269  * Reading and writing Z8530 registers.
270  */
271 static inline unsigned char read_zsreg(struct dec_zschannel *channel,
272                                        unsigned char reg)
273 {
274         unsigned char retval;
275
276         if (reg != 0) {
277                 *channel->control = reg & 0xf;
278                 fast_iob(); RECOVERY_DELAY;
279         }
280         retval = *channel->control;
281         RECOVERY_DELAY;
282         return retval;
283 }
284
285 static inline void write_zsreg(struct dec_zschannel *channel,
286                                unsigned char reg, unsigned char value)
287 {
288         if (reg != 0) {
289                 *channel->control = reg & 0xf;
290                 fast_iob(); RECOVERY_DELAY;
291         }
292         *channel->control = value;
293         fast_iob(); RECOVERY_DELAY;
294         return;
295 }
296
297 static inline unsigned char read_zsdata(struct dec_zschannel *channel)
298 {
299         unsigned char retval;
300
301         retval = *channel->data;
302         RECOVERY_DELAY;
303         return retval;
304 }
305
306 static inline void write_zsdata(struct dec_zschannel *channel,
307                                 unsigned char value)
308 {
309         *channel->data = value;
310         fast_iob(); RECOVERY_DELAY;
311         return;
312 }
313
314 static inline void load_zsregs(struct dec_zschannel *channel,
315                                unsigned char *regs)
316 {
317 /*      ZS_CLEARERR(channel);
318         ZS_CLEARFIFO(channel); */
319         /* Load 'em up */
320         write_zsreg(channel, R4, regs[R4]);
321         write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
322         write_zsreg(channel, R5, regs[R5] & ~TxENAB);
323         write_zsreg(channel, R9, regs[R9]);
324         write_zsreg(channel, R1, regs[R1]);
325         write_zsreg(channel, R2, regs[R2]);
326         write_zsreg(channel, R10, regs[R10]);
327         write_zsreg(channel, R11, regs[R11]);
328         write_zsreg(channel, R12, regs[R12]);
329         write_zsreg(channel, R13, regs[R13]);
330         write_zsreg(channel, R14, regs[R14]);
331         write_zsreg(channel, R15, regs[R15]);
332         write_zsreg(channel, R3, regs[R3]);
333         write_zsreg(channel, R5, regs[R5]);
334         return;
335 }
336
337 /* Sets or clears DTR/RTS on the requested line */
338 static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
339 {
340         unsigned long flags;
341
342
343         save_flags(flags); cli();
344         if (info->zs_channel != info->zs_chan_a) {
345                 if (set) {
346                         info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
347                 } else {
348                         info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
349                 }
350                 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
351         }
352         restore_flags(flags);
353 }
354
355 /* Utility routines for the Zilog */
356 static inline int get_zsbaud(struct dec_serial *ss)
357 {
358         struct dec_zschannel *channel = ss->zs_channel;
359         int brg;
360
361         /* The baud rate is split up between two 8-bit registers in
362          * what is termed 'BRG time constant' format in my docs for
363          * the chip, it is a function of the clk rate the chip is
364          * receiving which happens to be constant.
365          */
366         brg = (read_zsreg(channel, 13) << 8);
367         brg |= read_zsreg(channel, 12);
368         return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
369 }
370
371 /* On receive, this clears errors and the receiver interrupts */
372 static inline void rs_recv_clear(struct dec_zschannel *zsc)
373 {
374         write_zsreg(zsc, 0, ERR_RES);
375         write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
376 }
377
378 /*
379  * ----------------------------------------------------------------------
380  *
381  * Here starts the interrupt handling routines.  All of the following
382  * subroutines are declared as inline and are folded into
383  * rs_interrupt().  They were separated out for readability's sake.
384  *
385  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
386  * -----------------------------------------------------------------------
387  */
388
389 static int tty_break;   /* Set whenever BREAK condition is detected.  */
390
391 /*
392  * This routine is used by the interrupt handler to schedule
393  * processing in the software interrupt portion of the driver.
394  */
395 static _INLINE_ void rs_sched_event(struct dec_serial *info,
396                                   int event)
397 {
398         info->event |= 1 << event;
399         queue_task(&info->tqueue, &tq_zs_serial);
400         mark_bh(SERIAL_BH);
401 }
402
403 static _INLINE_ void receive_chars(struct dec_serial *info,
404                                    struct pt_regs *regs)
405 {
406         struct tty_struct *tty = info->tty;
407         unsigned char ch, stat, flag;
408
409         while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
410
411                 stat = read_zsreg(info->zs_channel, R1);
412                 ch = read_zsdata(info->zs_channel);
413
414                 if (!tty && (!info->hook || !info->hook->rx_char))
415                         continue;
416
417                 if (tty_break) {
418                         tty_break = 0;
419 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
420                         if (info->line == sercons.index) {
421                                 if (!break_pressed) {
422                                         break_pressed = jiffies;
423                                         goto ignore_char;
424                                 }
425                                 break_pressed = 0;
426                         }
427 #endif
428                         flag = TTY_BREAK;
429                         if (info->flags & ZILOG_SAK)
430                                 do_SAK(tty);
431                 } else {
432                         if (stat & Rx_OVR) {
433                                 flag = TTY_OVERRUN;
434                         } else if (stat & FRM_ERR) {
435                                 flag = TTY_FRAME;
436                         } else if (stat & PAR_ERR) {
437                                 flag = TTY_PARITY;
438                         } else
439                                 flag = 0;
440                         if (flag)
441                                 /* reset the error indication */
442                                 write_zsreg(info->zs_channel, R0, ERR_RES);
443                 }
444
445 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
446                 if (break_pressed && info->line == sercons.index) {
447                         if (ch != 0 &&
448                             time_before(jiffies, break_pressed + HZ*5)) {
449                                 handle_sysrq(ch, regs, NULL, NULL);
450                                 break_pressed = 0;
451                                 goto ignore_char;
452                         }
453                         break_pressed = 0;
454                 }
455 #endif
456
457                 if (info->hook && info->hook->rx_char) {
458                         (*info->hook->rx_char)(ch, flag);
459                         return;
460                 }
461
462                 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
463                         static int flip_buf_ovf;
464                         ++flip_buf_ovf;
465                         continue;
466                 }
467                 tty->flip.count++;
468                 {
469                         static int flip_max_cnt;
470                         if (flip_max_cnt < tty->flip.count)
471                                 flip_max_cnt = tty->flip.count;
472                 }
473
474                 *tty->flip.flag_buf_ptr++ = flag;
475                 *tty->flip.char_buf_ptr++ = ch;
476 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
477         ignore_char:
478 #endif
479         }
480         if (tty)
481                 tty_flip_buffer_push(tty);
482 }
483
484 static void transmit_chars(struct dec_serial *info)
485 {
486         if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
487                 return;
488         info->tx_active = 0;
489
490         if (info->x_char) {
491                 /* Send next char */
492                 write_zsdata(info->zs_channel, info->x_char);
493                 info->x_char = 0;
494                 info->tx_active = 1;
495                 return;
496         }
497
498         if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
499             || info->tx_stopped) {
500                 write_zsreg(info->zs_channel, R0, RES_Tx_P);
501                 return;
502         }
503         /* Send char */
504         write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
505         info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
506         info->xmit_cnt--;
507         info->tx_active = 1;
508
509         if (info->xmit_cnt < WAKEUP_CHARS)
510                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
511 }
512
513 static _INLINE_ void status_handle(struct dec_serial *info)
514 {
515         unsigned char stat;
516
517         /* Get status from Read Register 0 */
518         stat = read_zsreg(info->zs_channel, R0);
519
520         if (stat & BRK_ABRT) {
521 #ifdef SERIAL_DEBUG_INTR
522                 printk("handling break....");
523 #endif
524                 tty_break = 1;
525         }
526
527         if (info->zs_channel != info->zs_chan_a) {
528
529                 /* Check for DCD transitions */
530                 if (info->tty && !C_CLOCAL(info->tty) &&
531                     ((stat ^ info->read_reg_zero) & DCD) != 0 ) {
532                         if (stat & DCD) {
533                                 wake_up_interruptible(&info->open_wait);
534                         } else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) {
535                                 tty_hangup(info->tty);
536                         }
537                 }
538
539                 /* Check for CTS transitions */
540                 if (info->tty && C_CRTSCTS(info->tty)) {
541                         if ((stat & CTS) != 0) {
542                                 if (info->tx_stopped) {
543                                         info->tx_stopped = 0;
544                                         if (!info->tx_active)
545                                                 transmit_chars(info);
546                                 }
547                         } else {
548                                 info->tx_stopped = 1;
549                         }
550                 }
551
552         }
553
554         /* Clear status condition... */
555         write_zsreg(info->zs_channel, R0, RES_EXT_INT);
556         info->read_reg_zero = stat;
557 }
558
559 /*
560  * This is the serial driver's generic interrupt routine
561  */
562 void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
563 {
564         struct dec_serial *info = (struct dec_serial *) dev_id;
565         unsigned char zs_intreg;
566         int shift;
567
568         /* NOTE: The read register 3, which holds the irq status,
569          *       does so for both channels on each chip.  Although
570          *       the status value itself must be read from the A
571          *       channel and is only valid when read from channel A.
572          *       Yes... broken hardware...
573          */
574 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
575
576         if (info->zs_chan_a == info->zs_channel)
577                 shift = 3;      /* Channel A */
578         else
579                 shift = 0;      /* Channel B */
580
581         for (;;) {
582                 zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
583                 if ((zs_intreg & CHAN_IRQMASK) == 0)
584                         break;
585
586                 if (zs_intreg & CHBRxIP) {
587                         receive_chars(info, regs);
588                 }
589                 if (zs_intreg & CHBTxIP) {
590                         transmit_chars(info);
591                 }
592                 if (zs_intreg & CHBEXT) {
593                         status_handle(info);
594                 }
595         }
596
597         /* Why do we need this ? */
598         write_zsreg(info->zs_channel, 0, RES_H_IUS);
599 }
600
601 #ifdef ZS_DEBUG_REGS
602 void zs_dump (void) {
603         int i, j;
604         for (i = 0; i < zs_channels_found; i++) {
605                 struct dec_zschannel *ch = &zs_channels[i];
606                 if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
607                         for (j = 0; j < 15; j++) {
608                                 printk("W%d = 0x%x\t",
609                                        j, (int)ch->curregs[j]);
610                         }
611                         for (j = 0; j < 15; j++) {
612                                 printk("R%d = 0x%x\t",
613                                        j, (int)read_zsreg(ch,j));
614                         }
615                         printk("\n\n");
616                 }
617         }
618 }
619 #endif
620
621 /*
622  * -------------------------------------------------------------------
623  * Here ends the serial interrupt routines.
624  * -------------------------------------------------------------------
625  */
626
627 /*
628  * ------------------------------------------------------------
629  * rs_stop() and rs_start()
630  *
631  * This routines are called before setting or resetting tty->stopped.
632  * ------------------------------------------------------------
633  */
634 static void rs_stop(struct tty_struct *tty)
635 {
636         struct dec_serial *info = (struct dec_serial *)tty->driver_data;
637         unsigned long flags;
638
639         if (serial_paranoia_check(info, tty->device, "rs_stop"))
640                 return;
641
642 #if 1
643         save_flags(flags); cli();
644         if (info->zs_channel->curregs[5] & TxENAB) {
645                 info->zs_channel->curregs[5] &= ~TxENAB;
646                 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
647         }
648         restore_flags(flags);
649 #endif
650 }
651
652 static void rs_start(struct tty_struct *tty)
653 {
654         struct dec_serial *info = (struct dec_serial *)tty->driver_data;
655         unsigned long flags;
656
657         if (serial_paranoia_check(info, tty->device, "rs_start"))
658                 return;
659
660         save_flags(flags); cli();
661 #if 1
662         if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
663                 info->zs_channel->curregs[5] |= TxENAB;
664                 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
665         }
666 #else
667         if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
668                 transmit_chars(info);
669         }
670 #endif
671         restore_flags(flags);
672 }
673
674 /*
675  * This routine is used to handle the "bottom half" processing for the
676  * serial driver, known also the "software interrupt" processing.
677  * This processing is done at the kernel interrupt level, after the
678  * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
679  * is where time-consuming activities which can not be done in the
680  * interrupt driver proper are done; the interrupt driver schedules
681  * them using rs_sched_event(), and they get done here.
682  */
683 static void do_serial_bh(void)
684 {
685         run_task_queue(&tq_zs_serial);
686 }
687
688 static void do_softint(void *private_)
689 {
690         struct dec_serial       *info = (struct dec_serial *) private_;
691         struct tty_struct       *tty;
692
693         tty = info->tty;
694         if (!tty)
695                 return;
696
697         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
698                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
699                     tty->ldisc.write_wakeup)
700                         (tty->ldisc.write_wakeup)(tty);
701                 wake_up_interruptible(&tty->write_wait);
702         }
703 }
704
705 int zs_startup(struct dec_serial * info)
706 {
707         unsigned long flags;
708
709         if (info->flags & ZILOG_INITIALIZED)
710                 return 0;
711
712         if (!info->xmit_buf) {
713                 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
714                 if (!info->xmit_buf)
715                         return -ENOMEM;
716         }
717
718         save_flags(flags); cli();
719
720 #ifdef SERIAL_DEBUG_OPEN
721         printk("starting up ttyS%02d (irq %d)...", info->line, info->irq);
722 #endif
723
724         /*
725          * Clear the receive FIFO.
726          */
727         ZS_CLEARFIFO(info->zs_channel);
728         info->xmit_fifo_size = 1;
729
730         /*
731          * Clear the interrupt registers.
732          */
733         write_zsreg(info->zs_channel, 0, ERR_RES);
734         write_zsreg(info->zs_channel, 0, RES_H_IUS);
735
736         /*
737          * Turn on RTS and DTR.
738          */
739         zs_rtsdtr(info, RTS | DTR, 1);
740
741         /*
742          * Finally, enable sequencing and interrupts
743          */
744         info->zs_channel->curregs[1] = (info->zs_channel->curregs[1] & ~0x18) | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
745         info->zs_channel->curregs[3] |= (RxENABLE | Rx8);
746         info->zs_channel->curregs[5] |= (TxENAB | Tx8);
747         info->zs_channel->curregs[15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
748         info->zs_channel->curregs[9] |= (VIS | MIE);
749         write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
750         write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
751         write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
752         write_zsreg(info->zs_channel, 15, info->zs_channel->curregs[15]);
753         write_zsreg(info->zs_channel, 9, info->zs_channel->curregs[9]);
754
755         /*
756          * And clear the interrupt registers again for luck.
757          */
758         write_zsreg(info->zs_channel, 0, ERR_RES);
759         write_zsreg(info->zs_channel, 0, RES_H_IUS);
760
761         if (info->tty)
762                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
763         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
764
765         /*
766          * Set the speed of the serial port
767          */
768         change_speed(info);
769
770         /* Save the current value of RR0 */
771         info->read_reg_zero = read_zsreg(info->zs_channel, 0);
772
773         info->flags |= ZILOG_INITIALIZED;
774         restore_flags(flags);
775         return 0;
776 }
777
778 /*
779  * This routine will shutdown a serial port; interrupts are disabled, and
780  * DTR is dropped if the hangup on close termio flag is on.
781  */
782 static void shutdown(struct dec_serial * info)
783 {
784         unsigned long   flags;
785
786         if (!(info->flags & ZILOG_INITIALIZED))
787                 return;
788
789 #ifdef SERIAL_DEBUG_OPEN
790         printk("Shutting down serial port %d (irq %d)....", info->line,
791                info->irq);
792 #endif
793
794         save_flags(flags); cli(); /* Disable interrupts */
795
796         if (info->xmit_buf) {
797                 free_page((unsigned long) info->xmit_buf);
798                 info->xmit_buf = 0;
799         }
800
801         info->zs_channel->curregs[1] = 0;
802         write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
803
804         info->zs_channel->curregs[3] &= ~RxENABLE;
805         write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
806
807         info->zs_channel->curregs[5] &= ~TxENAB;
808         write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
809         if (!info->tty || C_HUPCL(info->tty)) {
810                 zs_rtsdtr(info, RTS | DTR, 0);
811         }
812
813         if (info->tty)
814                 set_bit(TTY_IO_ERROR, &info->tty->flags);
815
816         info->flags &= ~ZILOG_INITIALIZED;
817         restore_flags(flags);
818 }
819
820 /*
821  * This routine is called to set the UART divisor registers to match
822  * the specified baud rate for a serial port.
823  */
824 static void change_speed(struct dec_serial *info)
825 {
826         unsigned cflag;
827         int     i;
828         int     brg, bits;
829         unsigned long flags;
830
831         if (!info->hook) {
832                 if (!info->tty || !info->tty->termios)
833                         return;
834                 cflag = info->tty->termios->c_cflag;
835                 if (!info->port)
836                         return;
837         } else {
838                 cflag = info->hook->cflags;
839         }
840
841         i = cflag & CBAUD;
842         if (i & CBAUDEX) {
843                 i &= ~CBAUDEX;
844                 if (i < 1 || i > 2) {
845                         if (!info->hook)
846                                 info->tty->termios->c_cflag &= ~CBAUDEX;
847                         else
848                                 info->hook->cflags &= ~CBAUDEX;
849                 } else
850                         i += 15;
851         }
852
853         save_flags(flags); cli();
854         info->zs_baud = baud_table[i];
855         info->clk_divisor = 16;
856         if (info->zs_baud) {
857                 info->zs_channel->curregs[4] = X16CLK;
858                 brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
859                 info->zs_channel->curregs[12] = (brg & 255);
860                 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
861                 zs_rtsdtr(info, DTR, 1);
862         } else {
863                 zs_rtsdtr(info, RTS | DTR, 0);
864                 return;
865         }
866
867         /* byte size and parity */
868         info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
869         info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
870         switch (cflag & CSIZE) {
871         case CS5:
872                 bits = 7;
873                 info->zs_channel->curregs[3] |= Rx5;
874                 info->zs_channel->curregs[5] |= Tx5;
875                 break;
876         case CS6:
877                 bits = 8;
878                 info->zs_channel->curregs[3] |= Rx6;
879                 info->zs_channel->curregs[5] |= Tx6;
880                 break;
881         case CS7:
882                 bits = 9;
883                 info->zs_channel->curregs[3] |= Rx7;
884                 info->zs_channel->curregs[5] |= Tx7;
885                 break;
886         case CS8:
887         default: /* defaults to 8 bits */
888                 bits = 10;
889                 info->zs_channel->curregs[3] |= Rx8;
890                 info->zs_channel->curregs[5] |= Tx8;
891                 break;
892         }
893
894         info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
895         info->timeout += HZ/50;         /* Add .02 seconds of slop */
896
897         info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
898         if (cflag & CSTOPB) {
899                 info->zs_channel->curregs[4] |= SB2;
900         } else {
901                 info->zs_channel->curregs[4] |= SB1;
902         }
903         if (cflag & PARENB) {
904                 info->zs_channel->curregs[4] |= PAR_ENA;
905         }
906         if (!(cflag & PARODD)) {
907                 info->zs_channel->curregs[4] |= PAR_EVEN;
908         }
909
910         if (!(cflag & CLOCAL)) {
911                 if (!(info->zs_channel->curregs[15] & DCDIE))
912                         info->read_reg_zero = read_zsreg(info->zs_channel, 0);
913                 info->zs_channel->curregs[15] |= DCDIE;
914         } else
915                 info->zs_channel->curregs[15] &= ~DCDIE;
916         if (cflag & CRTSCTS) {
917                 info->zs_channel->curregs[15] |= CTSIE;
918                 if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
919                         info->tx_stopped = 1;
920         } else {
921                 info->zs_channel->curregs[15] &= ~CTSIE;
922                 info->tx_stopped = 0;
923         }
924
925         /* Load up the new values */
926         load_zsregs(info->zs_channel, info->zs_channel->curregs);
927
928         restore_flags(flags);
929 }
930
931 static void rs_flush_chars(struct tty_struct *tty)
932 {
933         struct dec_serial *info = (struct dec_serial *)tty->driver_data;
934         unsigned long flags;
935
936         if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
937                 return;
938
939         if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
940             !info->xmit_buf)
941                 return;
942
943         /* Enable transmitter */
944         save_flags(flags); cli();
945         transmit_chars(info);
946         restore_flags(flags);
947 }
948
949 static int rs_write(struct tty_struct * tty, int from_user,
950                     const unsigned char *buf, int count)
951 {
952         int     c, total = 0;
953         struct dec_serial *info = (struct dec_serial *)tty->driver_data;
954         unsigned long flags;
955
956         if (serial_paranoia_check(info, tty->device, "rs_write"))
957                 return 0;
958
959         if (!tty || !info->xmit_buf)
960                 return 0;
961
962         save_flags(flags);
963         while (1) {
964                 cli();
965                 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
966                                    SERIAL_XMIT_SIZE - info->xmit_head));
967                 if (c <= 0)
968                         break;
969
970                 if (from_user) {
971                         down(&tmp_buf_sem);
972                         copy_from_user(tmp_buf, buf, c);
973                         c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
974                                        SERIAL_XMIT_SIZE - info->xmit_head));
975                         memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
976                         up(&tmp_buf_sem);
977                 } else
978                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
979                 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
980                 info->xmit_cnt += c;
981                 restore_flags(flags);
982                 buf += c;
983                 count -= c;
984                 total += c;
985         }
986
987         if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
988             && !info->tx_active)
989                 transmit_chars(info);
990         restore_flags(flags);
991         return total;
992 }
993
994 static int rs_write_room(struct tty_struct *tty)
995 {
996         struct dec_serial *info = (struct dec_serial *)tty->driver_data;
997         int     ret;
998
999         if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1000                 return 0;
1001         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1002         if (ret < 0)
1003                 ret = 0;
1004         return ret;
1005 }
1006
1007 static int rs_chars_in_buffer(struct tty_struct *tty)
1008 {
1009         struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1010
1011         if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1012                 return 0;
1013         return info->xmit_cnt;
1014 }
1015
1016 static void rs_flush_buffer(struct tty_struct *tty)
1017 {
1018         struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1019
1020         if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1021                 return;
1022         cli();
1023         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1024         sti();
1025         wake_up_interruptible(&tty->write_wait);
1026         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1027             tty->ldisc.write_wakeup)
1028                 (tty->ldisc.write_wakeup)(tty);
1029 }
1030
1031 /*
1032  * ------------------------------------------------------------
1033  * rs_throttle()
1034  *
1035  * This routine is called by the upper-layer tty layer to signal that
1036  * incoming characters should be throttled.
1037  * ------------------------------------------------------------
1038  */
1039 static void rs_throttle(struct tty_struct * tty)
1040 {
1041         struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1042         unsigned long flags;
1043
1044 #ifdef SERIAL_DEBUG_THROTTLE
1045         char    buf[64];
1046
1047         printk("throttle %s: %d....\n", _tty_name(tty, buf),
1048                tty->ldisc.chars_in_buffer(tty));
1049 #endif
1050
1051         if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1052                 return;
1053
1054         if (I_IXOFF(tty)) {
1055                 save_flags(flags); cli();
1056                 info->x_char = STOP_CHAR(tty);
1057                 if (!info->tx_active)
1058                         transmit_chars(info);
1059                 restore_flags(flags);
1060         }
1061
1062         if (C_CRTSCTS(tty)) {
1063                 zs_rtsdtr(info, RTS, 0);
1064         }
1065 }
1066
1067 static void rs_unthrottle(struct tty_struct * tty)
1068 {
1069         struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1070         unsigned long flags;
1071
1072 #ifdef SERIAL_DEBUG_THROTTLE
1073         char    buf[64];
1074
1075         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1076                tty->ldisc.chars_in_buffer(tty));
1077 #endif
1078
1079         if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1080                 return;
1081
1082         if (I_IXOFF(tty)) {
1083                 save_flags(flags); cli();
1084                 if (info->x_char)
1085                         info->x_char = 0;
1086                 else {
1087                         info->x_char = START_CHAR(tty);
1088                         if (!info->tx_active)
1089                                 transmit_chars(info);
1090                 }
1091                 restore_flags(flags);
1092         }
1093
1094         if (C_CRTSCTS(tty)) {
1095                 zs_rtsdtr(info, RTS, 1);
1096         }
1097 }
1098
1099 /*
1100  * ------------------------------------------------------------
1101  * rs_ioctl() and friends
1102  * ------------------------------------------------------------
1103  */
1104
1105 static int get_serial_info(struct dec_serial * info,
1106                            struct serial_struct * retinfo)
1107 {
1108         struct serial_struct tmp;
1109
1110         if (!retinfo)
1111                 return -EFAULT;
1112         memset(&tmp, 0, sizeof(tmp));
1113         tmp.type = info->type;
1114         tmp.line = info->line;
1115         tmp.port = info->port;
1116         tmp.irq = info->irq;
1117         tmp.flags = info->flags;
1118         tmp.baud_base = info->baud_base;
1119         tmp.close_delay = info->close_delay;
1120         tmp.closing_wait = info->closing_wait;
1121         tmp.custom_divisor = info->custom_divisor;
1122         return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
1123 }
1124
1125 static int set_serial_info(struct dec_serial * info,
1126                            struct serial_struct * new_info)
1127 {
1128         struct serial_struct new_serial;
1129         struct dec_serial old_info;
1130         int                     retval = 0;
1131
1132         if (!new_info)
1133                 return -EFAULT;
1134         copy_from_user(&new_serial,new_info,sizeof(new_serial));
1135         old_info = *info;
1136
1137         if (!capable(CAP_SYS_ADMIN)) {
1138                 if ((new_serial.baud_base != info->baud_base) ||
1139                     (new_serial.type != info->type) ||
1140                     (new_serial.close_delay != info->close_delay) ||
1141                     ((new_serial.flags & ~ZILOG_USR_MASK) !=
1142                      (info->flags & ~ZILOG_USR_MASK)))
1143                         return -EPERM;
1144                 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1145                                (new_serial.flags & ZILOG_USR_MASK));
1146                 info->custom_divisor = new_serial.custom_divisor;
1147                 goto check_and_exit;
1148         }
1149
1150         if (info->count > 1)
1151                 return -EBUSY;
1152
1153         /*
1154          * OK, past this point, all the error checking has been done.
1155          * At this point, we start making changes.....
1156          */
1157
1158         info->baud_base = new_serial.baud_base;
1159         info->flags = ((info->flags & ~ZILOG_FLAGS) |
1160                         (new_serial.flags & ZILOG_FLAGS));
1161         info->type = new_serial.type;
1162         info->close_delay = new_serial.close_delay;
1163         info->closing_wait = new_serial.closing_wait;
1164
1165 check_and_exit:
1166         retval = zs_startup(info);
1167         return retval;
1168 }
1169
1170 /*
1171  * get_lsr_info - get line status register info
1172  *
1173  * Purpose: Let user call ioctl() to get info when the UART physically
1174  *          is emptied.  On bus types like RS485, the transmitter must
1175  *          release the bus after transmitting. This must be done when
1176  *          the transmit shift register is empty, not be done when the
1177  *          transmit holding register is empty.  This functionality
1178  *          allows an RS485 driver to be written in user space.
1179  */
1180 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1181 {
1182         unsigned char status;
1183
1184         cli();
1185         status = read_zsreg(info->zs_channel, 0);
1186         sti();
1187         put_user(status,value);
1188         return 0;
1189 }
1190
1191 static int get_modem_info(struct dec_serial *info, unsigned int *value)
1192 {
1193         unsigned char control, status_a, status_b;
1194         unsigned int result;
1195
1196         if (info->zs_channel == info->zs_chan_a)
1197                 result = 0;
1198         else {
1199                 cli();
1200                 control = info->zs_chan_a->curregs[5];
1201                 status_a = read_zsreg(info->zs_chan_a, 0);
1202                 status_b = read_zsreg(info->zs_channel, 0);
1203                 sti();
1204                 result =  ((control  & RTS) ? TIOCM_RTS: 0)
1205                         | ((control  & DTR) ? TIOCM_DTR: 0)
1206                         | ((status_b & DCD) ? TIOCM_CAR: 0)
1207                         | ((status_a & DCD) ? TIOCM_RNG: 0)
1208                         | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1209                         | ((status_b & CTS) ? TIOCM_CTS: 0);
1210         }
1211         put_user(result, value);
1212         return 0;
1213 }
1214
1215 static int set_modem_info(struct dec_serial *info, unsigned int cmd,
1216                           unsigned int *value)
1217 {
1218         int error;
1219         unsigned int arg, bits;
1220
1221         error = verify_area(VERIFY_READ, value, sizeof(int));
1222         if (error)
1223                 return error;
1224
1225         if (info->zs_channel == info->zs_chan_a)
1226                 return 0;
1227
1228         get_user(arg, value);
1229         bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0);
1230         cli();
1231         switch (cmd) {
1232         case TIOCMBIS:
1233                 info->zs_chan_a->curregs[5] |= bits;
1234                 break;
1235         case TIOCMBIC:
1236                 info->zs_chan_a->curregs[5] &= ~bits;
1237                 break;
1238         case TIOCMSET:
1239                 info->zs_chan_a->curregs[5] =
1240                         (info->zs_chan_a->curregs[5] & ~(DTR | RTS)) | bits;
1241                 break;
1242         default:
1243                 sti();
1244                 return -EINVAL;
1245         }
1246         write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1247         sti();
1248         return 0;
1249 }
1250
1251 /*
1252  * rs_break - turn transmit break condition on/off
1253  */
1254 static void rs_break(struct tty_struct *tty, int break_state)
1255 {
1256         struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1257         unsigned long flags;
1258
1259         if (serial_paranoia_check(info, tty->device, "rs_break"))
1260                 return;
1261         if (!info->port)
1262                 return;
1263
1264         save_flags(flags); cli();
1265         if (break_state == -1)
1266                 info->zs_channel->curregs[5] |= SND_BRK;
1267         else
1268                 info->zs_channel->curregs[5] &= ~SND_BRK;
1269         write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1270         restore_flags(flags);
1271 }
1272
1273 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1274                     unsigned int cmd, unsigned long arg)
1275 {
1276         int error;
1277         struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1278
1279         if (info->hook)
1280                 return -ENODEV;
1281
1282         if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1283                 return -ENODEV;
1284
1285         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1286             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1287             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1288                 if (tty->flags & (1 << TTY_IO_ERROR))
1289                     return -EIO;
1290         }
1291
1292         switch (cmd) {
1293                 case TIOCMGET:
1294                         error = verify_area(VERIFY_WRITE, (void *) arg,
1295                                 sizeof(unsigned int));
1296                         if (error)
1297                                 return error;
1298                         return get_modem_info(info, (unsigned int *) arg);
1299                 case TIOCMBIS:
1300                 case TIOCMBIC:
1301                 case TIOCMSET:
1302                         return set_modem_info(info, cmd, (unsigned int *) arg);
1303                 case TIOCGSERIAL:
1304                         error = verify_area(VERIFY_WRITE, (void *) arg,
1305                                                 sizeof(struct serial_struct));
1306                         if (error)
1307                                 return error;
1308                         return get_serial_info(info,
1309                                                (struct serial_struct *) arg);
1310                 case TIOCSSERIAL:
1311                         return set_serial_info(info,
1312                                                (struct serial_struct *) arg);
1313                 case TIOCSERGETLSR: /* Get line status register */
1314                         error = verify_area(VERIFY_WRITE, (void *) arg,
1315                                 sizeof(unsigned int));
1316                         if (error)
1317                                 return error;
1318                         else
1319                             return get_lsr_info(info, (unsigned int *) arg);
1320
1321                 case TIOCSERGSTRUCT:
1322                         error = verify_area(VERIFY_WRITE, (void *) arg,
1323                                                 sizeof(struct dec_serial));
1324                         if (error)
1325                                 return error;
1326                         copy_from_user((struct dec_serial *) arg,
1327                                        info, sizeof(struct dec_serial));
1328                         return 0;
1329
1330                 default:
1331                         return -ENOIOCTLCMD;
1332                 }
1333         return 0;
1334 }
1335
1336 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1337 {
1338         struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1339         int was_stopped;
1340
1341         if (tty->termios->c_cflag == old_termios->c_cflag)
1342                 return;
1343         was_stopped = info->tx_stopped;
1344
1345         change_speed(info);
1346
1347         if (was_stopped && !info->tx_stopped)
1348                 rs_start(tty);
1349 }
1350
1351 /*
1352  * ------------------------------------------------------------
1353  * rs_close()
1354  *
1355  * This routine is called when the serial port gets closed.
1356  * Wait for the last remaining data to be sent.
1357  * ------------------------------------------------------------
1358  */
1359 static void rs_close(struct tty_struct *tty, struct file * filp)
1360 {
1361         struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1362         unsigned long flags;
1363
1364         if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1365                 return;
1366
1367         save_flags(flags); cli();
1368
1369         if (tty_hung_up_p(filp)) {
1370                 restore_flags(flags);
1371                 return;
1372         }
1373
1374 #ifdef SERIAL_DEBUG_OPEN
1375         printk("rs_close ttyS%02d, count = %d\n", info->line, info->count);
1376 #endif
1377         if ((tty->count == 1) && (info->count != 1)) {
1378                 /*
1379                  * Uh, oh.  tty->count is 1, which means that the tty
1380                  * structure will be freed.  Info->count should always
1381                  * be one in these conditions.  If it's greater than
1382                  * one, we've got real problems, since it means the
1383                  * serial port won't be shutdown.
1384                  */
1385                 printk("rs_close: bad serial port count; tty->count is 1, "
1386                        "info->count is %d\n", info->count);
1387                 info->count = 1;
1388         }
1389         if (--info->count < 0) {
1390                 printk("rs_close: bad serial port count for ttyS%02d: %d\n",
1391                        info->line, info->count);
1392                 info->count = 0;
1393         }
1394         if (info->count) {
1395                 restore_flags(flags);
1396                 return;
1397         }
1398         info->flags |= ZILOG_CLOSING;
1399         /*
1400          * Save the termios structure, since this port may have
1401          * separate termios for callout and dialin.
1402          */
1403         if (info->flags & ZILOG_NORMAL_ACTIVE)
1404                 info->normal_termios = *tty->termios;
1405         if (info->flags & ZILOG_CALLOUT_ACTIVE)
1406                 info->callout_termios = *tty->termios;
1407         /*
1408          * Now we wait for the transmit buffer to clear; and we notify
1409          * the line discipline to only process XON/XOFF characters.
1410          */
1411         tty->closing = 1;
1412         if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1413                 tty_wait_until_sent(tty, info->closing_wait);
1414         /*
1415          * At this point we stop accepting input.  To do this, we
1416          * disable the receiver and receive interrupts.
1417          */
1418         info->zs_channel->curregs[3] &= ~RxENABLE;
1419         write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1420         info->zs_channel->curregs[1] = 0;       /* disable any rx ints */
1421         write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1422         ZS_CLEARFIFO(info->zs_channel);
1423         if (info->flags & ZILOG_INITIALIZED) {
1424                 /*
1425                  * Before we drop DTR, make sure the SCC transmitter
1426                  * has completely drained.
1427                  */
1428                 rs_wait_until_sent(tty, info->timeout);
1429         }
1430
1431         shutdown(info);
1432         if (tty->driver.flush_buffer)
1433                 tty->driver.flush_buffer(tty);
1434         if (tty->ldisc.flush_buffer)
1435                 tty->ldisc.flush_buffer(tty);
1436         tty->closing = 0;
1437         info->event = 0;
1438         info->tty = 0;
1439         if (info->blocked_open) {
1440                 if (info->close_delay) {
1441                         current->state = TASK_INTERRUPTIBLE;
1442                         schedule_timeout(info->close_delay);
1443                 }
1444                 wake_up_interruptible(&info->open_wait);
1445         }
1446         info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1447                          ZILOG_CLOSING);
1448         wake_up_interruptible(&info->close_wait);
1449         restore_flags(flags);
1450 }
1451
1452 /*
1453  * rs_wait_until_sent() --- wait until the transmitter is empty
1454  */
1455 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1456 {
1457         struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1458         unsigned long orig_jiffies, char_time;
1459
1460         if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1461                 return;
1462
1463         orig_jiffies = jiffies;
1464         /*
1465          * Set the check interval to be 1/5 of the estimated time to
1466          * send a single character, and make it at least 1.  The check
1467          * interval should also be less than the timeout.
1468          */
1469         char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1470         char_time = char_time / 5;
1471         if (char_time == 0)
1472                 char_time = 1;
1473         if (timeout)
1474                 char_time = MIN(char_time, timeout);
1475         while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1476                 current->state = TASK_INTERRUPTIBLE;
1477                 schedule_timeout(char_time);
1478                 if (signal_pending(current))
1479                         break;
1480                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1481                         break;
1482         }
1483         current->state = TASK_RUNNING;
1484 }
1485
1486 /*
1487  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1488  */
1489 void rs_hangup(struct tty_struct *tty)
1490 {
1491         struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1492
1493         if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1494                 return;
1495
1496         rs_flush_buffer(tty);
1497         shutdown(info);
1498         info->event = 0;
1499         info->count = 0;
1500         info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1501         info->tty = 0;
1502         wake_up_interruptible(&info->open_wait);
1503 }
1504
1505 /*
1506  * ------------------------------------------------------------
1507  * rs_open() and friends
1508  * ------------------------------------------------------------
1509  */
1510 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1511                            struct dec_serial *info)
1512 {
1513         DECLARE_WAITQUEUE(wait, current);
1514         int             retval;
1515         int             do_clocal = 0;
1516
1517         /*
1518          * If the device is in the middle of being closed, then block
1519          * until it's done, and then try again.
1520          */
1521         if (info->flags & ZILOG_CLOSING) {
1522                 interruptible_sleep_on(&info->close_wait);
1523 #ifdef SERIAL_DO_RESTART
1524                 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1525                         -EAGAIN : -ERESTARTSYS);
1526 #else
1527                 return -EAGAIN;
1528 #endif
1529         }
1530
1531         /*
1532          * If this is a callout device, then just make sure the normal
1533          * device isn't being used.
1534          */
1535         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1536                 if (info->flags & ZILOG_NORMAL_ACTIVE)
1537                         return -EBUSY;
1538                 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1539                     (info->flags & ZILOG_SESSION_LOCKOUT) &&
1540                     (info->session != current->session))
1541                     return -EBUSY;
1542                 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1543                     (info->flags & ZILOG_PGRP_LOCKOUT) &&
1544                     (info->pgrp != current->pgrp))
1545                     return -EBUSY;
1546                 info->flags |= ZILOG_CALLOUT_ACTIVE;
1547                 return 0;
1548         }
1549
1550         /*
1551          * If non-blocking mode is set, or the port is not enabled,
1552          * then make the check up front and then exit.
1553          */
1554         if ((filp->f_flags & O_NONBLOCK) ||
1555             (tty->flags & (1 << TTY_IO_ERROR))) {
1556                 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1557                         return -EBUSY;
1558                 info->flags |= ZILOG_NORMAL_ACTIVE;
1559                 return 0;
1560         }
1561
1562         if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1563                 if (info->normal_termios.c_cflag & CLOCAL)
1564                         do_clocal = 1;
1565         } else {
1566                 if (tty->termios->c_cflag & CLOCAL)
1567                         do_clocal = 1;
1568         }
1569
1570         /*
1571          * Block waiting for the carrier detect and the line to become
1572          * free (i.e., not in use by the callout).  While we are in
1573          * this loop, info->count is dropped by one, so that
1574          * rs_close() knows when to free things.  We restore it upon
1575          * exit, either normal or abnormal.
1576          */
1577         retval = 0;
1578         add_wait_queue(&info->open_wait, &wait);
1579 #ifdef SERIAL_DEBUG_OPEN
1580         printk("block_til_ready before block: ttyS%02d, count = %d\n",
1581                info->line, info->count);
1582 #endif
1583         cli();
1584         if (!tty_hung_up_p(filp))
1585                 info->count--;
1586         sti();
1587         info->blocked_open++;
1588         while (1) {
1589                 cli();
1590                 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1591                     (tty->termios->c_cflag & CBAUD))
1592                         zs_rtsdtr(info, RTS | DTR, 1);
1593                 sti();
1594                 set_current_state(TASK_INTERRUPTIBLE);
1595                 if (tty_hung_up_p(filp) ||
1596                     !(info->flags & ZILOG_INITIALIZED)) {
1597 #ifdef SERIAL_DO_RESTART
1598                         if (info->flags & ZILOG_HUP_NOTIFY)
1599                                 retval = -EAGAIN;
1600                         else
1601                                 retval = -ERESTARTSYS;
1602 #else
1603                         retval = -EAGAIN;
1604 #endif
1605                         break;
1606                 }
1607                 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1608                     !(info->flags & ZILOG_CLOSING) &&
1609                     (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1610                         break;
1611                 if (signal_pending(current)) {
1612                         retval = -ERESTARTSYS;
1613                         break;
1614                 }
1615 #ifdef SERIAL_DEBUG_OPEN
1616                 printk("block_til_ready blocking: ttyS%02d, count = %d\n",
1617                        info->line, info->count);
1618 #endif
1619                 schedule();
1620         }
1621         current->state = TASK_RUNNING;
1622         remove_wait_queue(&info->open_wait, &wait);
1623         if (!tty_hung_up_p(filp))
1624                 info->count++;
1625         info->blocked_open--;
1626 #ifdef SERIAL_DEBUG_OPEN
1627         printk("block_til_ready after blocking: ttyS%02d, count = %d\n",
1628                info->line, info->count);
1629 #endif
1630         if (retval)
1631                 return retval;
1632         info->flags |= ZILOG_NORMAL_ACTIVE;
1633         return 0;
1634 }
1635
1636 /*
1637  * This routine is called whenever a serial port is opened.  It
1638  * enables interrupts for a serial port, linking in its ZILOG structure into
1639  * the IRQ chain.   It also performs the serial-specific
1640  * initialization for the tty structure.
1641  */
1642 int rs_open(struct tty_struct *tty, struct file * filp)
1643 {
1644         struct dec_serial       *info;
1645         int                     retval, line;
1646
1647         line = MINOR(tty->device) - tty->driver.minor_start;
1648         if ((line < 0) || (line >= zs_channels_found))
1649                 return -ENODEV;
1650         info = zs_soft + line;
1651
1652         if (info->hook)
1653                 return -ENODEV;
1654
1655         if (serial_paranoia_check(info, tty->device, "rs_open"))
1656                 return -ENODEV;
1657 #ifdef SERIAL_DEBUG_OPEN
1658         printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1659                info->count);
1660 #endif
1661
1662         info->count++;
1663         tty->driver_data = info;
1664         info->tty = tty;
1665
1666         /*
1667          * If the port is the middle of closing, bail out now
1668          */
1669         if (tty_hung_up_p(filp) ||
1670             (info->flags & ZILOG_CLOSING)) {
1671                 if (info->flags & ZILOG_CLOSING)
1672                         interruptible_sleep_on(&info->close_wait);
1673 #ifdef SERIAL_DO_RESTART
1674                 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1675                         -EAGAIN : -ERESTARTSYS);
1676 #else
1677                 return -EAGAIN;
1678 #endif
1679         }
1680
1681         /*
1682          * Start up serial port
1683          */
1684         retval = zs_startup(info);
1685         if (retval)
1686                 return retval;
1687
1688         retval = block_til_ready(tty, filp, info);
1689         if (retval) {
1690 #ifdef SERIAL_DEBUG_OPEN
1691                 printk("rs_open returning after block_til_ready with %d\n",
1692                        retval);
1693 #endif
1694                 return retval;
1695         }
1696
1697         if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1698                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1699                         *tty->termios = info->normal_termios;
1700                 else
1701                         *tty->termios = info->callout_termios;
1702                 change_speed(info);
1703         }
1704 #ifdef CONFIG_SERIAL_CONSOLE
1705         if (sercons.cflag && sercons.index == line) {
1706                 tty->termios->c_cflag = sercons.cflag;
1707                 sercons.cflag = 0;
1708                 change_speed(info);
1709         }
1710 #endif
1711
1712         info->session = current->session;
1713         info->pgrp = current->pgrp;
1714
1715 #ifdef SERIAL_DEBUG_OPEN
1716         printk("rs_open ttyS%02d successful...", info->line);
1717 #endif
1718 /* tty->low_latency = 1; */
1719         return 0;
1720 }
1721
1722 /* Finally, routines used to initialize the serial driver. */
1723
1724 static void __init show_serial_version(void)
1725 {
1726         printk("DECstation Z8530 serial driver version 0.07\n");
1727 }
1728
1729 /*  Initialize Z8530s zs_channels
1730  */
1731
1732 static void __init probe_sccs(void)
1733 {
1734         struct dec_serial **pp;
1735         int i, n, n_chips = 0, n_channels, chip, channel;
1736
1737         /*
1738          * did we get here by accident?
1739          */
1740         if(!BUS_PRESENT) {
1741                 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1742                 return;
1743         }
1744
1745         /*
1746          * When serial console is activated, tc_init has not been called yet
1747          * and system_base is undefined. Unfortunately we have to hardcode
1748          * system_base for this case :-(. HK
1749          */
1750         switch(mips_machtype) {
1751 #ifdef CONFIG_DECSTATION
1752         case MACH_DS5000_2X0:
1753                 system_base = 0xbf800000;
1754                 n_chips = 2;
1755                 zs_parms = &ds_parms;
1756                 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1757                 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1758                 break;
1759         case MACH_DS5000_1XX:
1760                 system_base = 0xbc000000;
1761                 n_chips = 2;
1762                 zs_parms = &ds_parms;
1763                 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1764                 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1765                 break;
1766         case MACH_DS5000_XX:
1767                 system_base = 0xbc000000;
1768                 n_chips = 1;
1769                 zs_parms = &ds_parms;
1770                 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1771                 break;
1772 #endif
1773 #ifdef CONFIG_BAGET_MIPS
1774         case MACH_BAGET202:
1775                 system_base = UNI_IO_BASE;
1776                 n_chips = 2;
1777                 zs_parms = &baget_parms;
1778                 zs_init_regs[2] = 0x8;
1779                 break;
1780 #endif
1781         default:
1782                 panic("zs: unsupported bus");
1783         }
1784         if (!zs_parms)
1785                 panic("zs: uninitialized parms");
1786
1787         pp = &zs_chain;
1788
1789         n_channels = 0;
1790
1791         for (chip = 0; chip < n_chips; chip++) {
1792                 for (channel = 0; channel <= 1; channel++) {
1793                         /*
1794                          * The sccs reside on the high byte of the 16 bit IOBUS
1795                          */
1796                         zs_channels[n_channels].control =
1797                                 (volatile unsigned char *)system_base +
1798                           (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1799                           (0 == channel ? zs_parms->channel_a_offset :
1800                                           zs_parms->channel_b_offset);
1801                         zs_channels[n_channels].data =
1802                                 zs_channels[n_channels].control + 4;
1803
1804 #ifndef CONFIG_SERIAL_CONSOLE
1805                         /*
1806                          * We're called early and memory managment isn't up, yet.
1807                          * Thus check_region would fail.
1808                          */
1809                         if (check_region((unsigned long)
1810                                          zs_channels[n_channels].control,
1811                                          ZS_CHAN_IO_SIZE) < 0) {
1812                                 panic("SCC I/O region is not free");
1813                         }
1814                         request_region((unsigned long)
1815                                        zs_channels[n_channels].control,
1816                                        ZS_CHAN_IO_SIZE, "SCC");
1817 #endif
1818                         zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1819                         /* HACK alert! */
1820                         if (!(chip & 1))
1821                                 zs_soft[n_channels].irq = zs_parms->irq0;
1822                         else
1823                                 zs_soft[n_channels].irq = zs_parms->irq1;
1824
1825                         /*
1826                          *  Identification of channel A. Location of channel A
1827                          *  inside chip depends on mapping of internal address
1828                          *  the chip decodes channels by.
1829                          *  CHANNEL_A_NR returns either 0 (in case of
1830                          *  DECstations) or 1 (in case of Baget).
1831                          */
1832                         if (CHANNEL_A_NR == channel)
1833                                 zs_soft[n_channels].zs_chan_a =
1834                                     &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1835                         else
1836                                 zs_soft[n_channels].zs_chan_a =
1837                                     &zs_channels[n_channels];
1838
1839                         *pp = &zs_soft[n_channels];
1840                         pp = &zs_soft[n_channels].zs_next;
1841                         n_channels++;
1842                 }
1843         }
1844
1845         *pp = 0;
1846         zs_channels_found = n_channels;
1847
1848         for (n = 0; n < zs_channels_found; n++) {
1849                 for (i = 0; i < 16; i++) {
1850                         zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1851                 }
1852         }
1853
1854 /*      save_and_cli(flags);
1855         for (n = 0; n < zs_channels_found; n++) {
1856                 if (((int)zs_channels[n].control & 0xf) == 1) {
1857                         write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1858                         mdelay(10);
1859                         write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1860                 }
1861                 load_zsregs(zs_soft[n].zs_channel, zs_soft[n].zs_channel->curregs);
1862         }
1863         restore_flags(flags); */
1864 }
1865
1866 /* zs_init inits the driver */
1867 int __init zs_init(void)
1868 {
1869         int channel, i;
1870         unsigned long flags;
1871         struct dec_serial *info;
1872
1873         if(!BUS_PRESENT)
1874                 return -ENODEV;
1875
1876         /* Setup base handler, and timer table. */
1877         init_bh(SERIAL_BH, do_serial_bh);
1878
1879         /* Find out how many Z8530 SCCs we have */
1880         if (zs_chain == 0)
1881                 probe_sccs();
1882
1883         show_serial_version();
1884
1885         /* Initialize the tty_driver structure */
1886         /* Not all of this is exactly right for us. */
1887
1888         memset(&serial_driver, 0, sizeof(struct tty_driver));
1889         serial_driver.magic = TTY_DRIVER_MAGIC;
1890 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1891         serial_driver.name = "tts/%d";
1892 #else
1893         serial_driver.name = "ttyS";
1894 #endif
1895         serial_driver.major = TTY_MAJOR;
1896         serial_driver.minor_start = 64;
1897         serial_driver.num = zs_channels_found;
1898         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1899         serial_driver.subtype = SERIAL_TYPE_NORMAL;
1900         serial_driver.init_termios = tty_std_termios;
1901
1902         serial_driver.init_termios.c_cflag =
1903                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1904         serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1905         serial_driver.refcount = &serial_refcount;
1906         serial_driver.table = serial_table;
1907         serial_driver.termios = serial_termios;
1908         serial_driver.termios_locked = serial_termios_locked;
1909
1910         serial_driver.open = rs_open;
1911         serial_driver.close = rs_close;
1912         serial_driver.write = rs_write;
1913         serial_driver.flush_chars = rs_flush_chars;
1914         serial_driver.write_room = rs_write_room;
1915         serial_driver.chars_in_buffer = rs_chars_in_buffer;
1916         serial_driver.flush_buffer = rs_flush_buffer;
1917         serial_driver.ioctl = rs_ioctl;
1918         serial_driver.throttle = rs_throttle;
1919         serial_driver.unthrottle = rs_unthrottle;
1920         serial_driver.set_termios = rs_set_termios;
1921         serial_driver.stop = rs_stop;
1922         serial_driver.start = rs_start;
1923         serial_driver.hangup = rs_hangup;
1924         serial_driver.break_ctl = rs_break;
1925         serial_driver.wait_until_sent = rs_wait_until_sent;
1926
1927         /*
1928          * The callout device is just like normal device except for
1929          * major number and the subtype code.
1930          */
1931         callout_driver = serial_driver;
1932 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1933         callout_driver.name = "cua/%d";
1934 #else
1935         callout_driver.name = "cua";
1936 #endif
1937         callout_driver.major = TTYAUX_MAJOR;
1938         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1939
1940         if (tty_register_driver(&serial_driver))
1941                 panic("Couldn't register serial driver");
1942         if (tty_register_driver(&callout_driver))
1943                 panic("Couldn't register callout driver");
1944
1945         save_flags(flags); cli();
1946
1947         for (channel = 0; channel < zs_channels_found; ++channel) {
1948                 if (request_irq(zs_soft[channel].irq, rs_interrupt, SA_SHIRQ,
1949                                 "scc", &zs_soft[channel]))
1950                         printk(KERN_ERR "decserial: can't get irq %d\n",
1951                                zs_soft[channel].irq);
1952
1953                 zs_soft[channel].clk_divisor = 16;
1954                 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1955         }
1956
1957         for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1958
1959                 /* Needed before interrupts are enabled. */
1960                 info->tty = 0;
1961                 info->x_char = 0;
1962
1963                 if (info->hook && info->hook->init_info)
1964                         continue;
1965
1966                 info->magic = SERIAL_MAGIC;
1967                 info->port = (int) info->zs_channel->control;
1968                 info->line = i;
1969                 info->custom_divisor = 16;
1970                 info->close_delay = 50;
1971                 info->closing_wait = 3000;
1972                 info->event = 0;
1973                 info->count = 0;
1974                 info->blocked_open = 0;
1975                 info->tqueue.routine = do_softint;
1976                 info->tqueue.data = info;
1977                 info->callout_termios = callout_driver.init_termios;
1978                 info->normal_termios = serial_driver.init_termios;
1979                 init_waitqueue_head(&info->open_wait);
1980                 init_waitqueue_head(&info->close_wait);
1981                 printk("ttyS%02d at 0x%08x (irq = %d)", info->line,
1982                        info->port, info->irq);
1983                 printk(" is a Z85C30 SCC\n");
1984                 tty_register_devfs(&serial_driver, 0,
1985                                    serial_driver.minor_start + info->line);
1986                 tty_register_devfs(&callout_driver, 0,
1987                                    callout_driver.minor_start + info->line);
1988
1989         }
1990
1991         restore_flags(flags);
1992
1993         for (channel = 0; channel < zs_channels_found; ++channel) {
1994                 if (zs_soft[channel].hook &&
1995                     zs_soft[channel].hook->init_channel)
1996                         (*zs_soft[channel].hook->init_channel)
1997                                 (&zs_soft[channel]);
1998         }
1999
2000         for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
2001                 if (info->hook && info->hook->init_info)
2002                         (*info->hook->init_info)(info);
2003         }
2004
2005         return 0;
2006 }
2007
2008 /*
2009  * register_serial and unregister_serial allows for serial ports to be
2010  * configured at run-time, to support PCMCIA modems.
2011  */
2012 /* PowerMac: Unused at this time, just here to make things link. */
2013 int register_serial(struct serial_struct *req)
2014 {
2015         return -1;
2016 }
2017
2018 void unregister_serial(int line)
2019 {
2020         return;
2021 }
2022
2023 /*
2024  * polling I/O routines
2025  */
2026 static int
2027 zs_poll_tx_char(struct dec_serial *info, unsigned char ch)
2028 {
2029         struct dec_zschannel *chan = info->zs_channel;
2030         int    ret;
2031
2032         if(chan) {
2033                 int loops = 10000;
2034
2035                 while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
2036                         loops--;
2037
2038                 if (loops) {
2039                         write_zsdata(chan, ch);
2040                         ret = 0;
2041                 } else
2042                         ret = -EAGAIN;
2043
2044                 return ret;
2045         } else
2046                 return -ENODEV;
2047 }
2048
2049 static int
2050 zs_poll_rx_char(struct dec_serial *info)
2051 {
2052         struct dec_zschannel *chan = info->zs_channel;
2053         int    ret;
2054
2055         if(chan) {
2056                 int loops = 10000;
2057
2058                 while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
2059                         loops--;
2060
2061                 if (loops)
2062                         ret = read_zsdata(chan);
2063                 else
2064                         ret = -EAGAIN;
2065
2066                 return ret;
2067         } else
2068                 return -ENODEV;
2069 }
2070
2071 unsigned int register_zs_hook(unsigned int channel, struct zs_hook *hook)
2072 {
2073         struct dec_serial *info = &zs_soft[channel];
2074
2075         if (info->hook) {
2076                 printk(__FUNCTION__": line %d has already a hook registered\n", channel);
2077
2078                 return 0;
2079         } else {
2080                 info->hook = hook;
2081
2082                 if (zs_chain == 0)
2083                         probe_sccs();
2084
2085                 if (!(info->flags & ZILOG_INITIALIZED))
2086                         zs_startup(info);
2087
2088                 hook->poll_rx_char = zs_poll_rx_char;
2089                 hook->poll_tx_char = zs_poll_tx_char;
2090
2091                 return 1;
2092         }
2093 }
2094
2095 unsigned int unregister_zs_hook(unsigned int channel)
2096 {
2097         struct dec_serial *info = &zs_soft[channel];
2098
2099         if (info->hook) {
2100                 info->hook = NULL;
2101                 return 1;
2102         } else {
2103                 printk(__FUNCTION__": trying to unregister hook on line %d,"
2104                        " but none is registered\n", channel);
2105                 return 0;
2106         }
2107 }
2108
2109 /*
2110  * ------------------------------------------------------------
2111  * Serial console driver
2112  * ------------------------------------------------------------
2113  */
2114 #ifdef CONFIG_SERIAL_CONSOLE
2115
2116
2117 /*
2118  *      Print a string to the serial port trying not to disturb
2119  *      any possible real use of the port...
2120  */
2121 static void serial_console_write(struct console *co, const char *s,
2122                                  unsigned count)
2123 {
2124         struct dec_serial *info;
2125         int i;
2126
2127         info = zs_soft + co->index;
2128
2129         for (i = 0; i < count; i++, s++) {
2130                 if(*s == '\n')
2131                         zs_poll_tx_char(info, '\r');
2132                 zs_poll_tx_char(info, *s);
2133         }
2134 }
2135
2136 static kdev_t serial_console_device(struct console *c)
2137 {
2138         return MKDEV(TTY_MAJOR, 64 + c->index);
2139 }
2140
2141 /*
2142  *      Setup initial baud/bits/parity. We do two things here:
2143  *      - construct a cflag setting for the first rs_open()
2144  *      - initialize the serial port
2145  *      Return non-zero if we didn't find a serial port.
2146  */
2147 static int __init serial_console_setup(struct console *co, char *options)
2148 {
2149         struct dec_serial *info;
2150         int     baud = 9600;
2151         int     bits = 8;
2152         int     parity = 'n';
2153         int     cflag = CREAD | HUPCL | CLOCAL;
2154         char    *s;
2155         unsigned long flags;
2156
2157         if(!BUS_PRESENT)
2158                 return -ENODEV;
2159
2160         info = zs_soft + co->index;
2161
2162         if (zs_chain == 0)
2163                 probe_sccs();
2164
2165         info->is_cons = 1;
2166
2167         if (options) {
2168                 baud = simple_strtoul(options, NULL, 10);
2169                 s = options;
2170                 while(*s >= '0' && *s <= '9')
2171                         s++;
2172                 if (*s)
2173                         parity = *s++;
2174                 if (*s)
2175                         bits   = *s - '0';
2176         }
2177
2178         /*
2179          *      Now construct a cflag setting.
2180          */
2181         switch(baud) {
2182         case 1200:
2183                 cflag |= B1200;
2184                 break;
2185         case 2400:
2186                 cflag |= B2400;
2187                 break;
2188         case 4800:
2189                 cflag |= B4800;
2190                 break;
2191         case 19200:
2192                 cflag |= B19200;
2193                 break;
2194         case 38400:
2195                 cflag |= B38400;
2196                 break;
2197         case 57600:
2198                 cflag |= B57600;
2199                 break;
2200         case 115200:
2201                 cflag |= B115200;
2202                 break;
2203         case 9600:
2204         default:
2205                 cflag |= B9600;
2206                 break;
2207         }
2208         switch(bits) {
2209         case 7:
2210                 cflag |= CS7;
2211                 break;
2212         default:
2213         case 8:
2214                 cflag |= CS8;
2215                 break;
2216         }
2217         switch(parity) {
2218         case 'o': case 'O':
2219                 cflag |= PARODD;
2220                 break;
2221         case 'e': case 'E':
2222                 cflag |= PARENB;
2223                 break;
2224         }
2225         co->cflag = cflag;
2226 #if 1
2227         save_and_cli(flags);
2228
2229         /*
2230          * Turn on RTS and DTR.
2231          */
2232         zs_rtsdtr(info, RTS | DTR, 1);
2233
2234         /*
2235          * Finally, enable sequencing
2236          */
2237         info->zs_channel->curregs[3] |= (RxENABLE | Rx8);
2238         info->zs_channel->curregs[5] |= (TxENAB | Tx8);
2239         info->zs_channel->curregs[9] |= (VIS);
2240         write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
2241         write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
2242         write_zsreg(info->zs_channel, 9, info->zs_channel->curregs[9]);
2243
2244         /*
2245          * Clear the interrupt registers.
2246          */
2247         write_zsreg(info->zs_channel, 0, ERR_RES);
2248         write_zsreg(info->zs_channel, 0, RES_H_IUS);
2249
2250         /*
2251          * Set the speed of the serial port
2252          */
2253         change_speed(info);
2254
2255         /* Save the current value of RR0 */
2256         info->read_reg_zero = read_zsreg(info->zs_channel, 0);
2257
2258         zs_soft[co->index].clk_divisor = 16;
2259         zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2260
2261         restore_flags(flags);
2262 #endif
2263         return 0;
2264 }
2265
2266 static struct console sercons = {
2267         name:           "ttyS",
2268         write:          serial_console_write,
2269         device:         serial_console_device,
2270         setup:          serial_console_setup,
2271         flags:          CON_PRINTBUFFER,
2272         index:          -1,
2273 };
2274
2275 /*
2276  *      Register console.
2277  */
2278 void __init zs_serial_console_init(void)
2279 {
2280         register_console(&sercons);
2281 }
2282 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
2283
2284 #ifdef CONFIG_KGDB
2285 struct dec_zschannel *zs_kgdbchan;
2286 static unsigned char scc_inittab[] = {
2287         9,  0x80,       /* reset A side (CHRA) */
2288         13, 0,          /* set baud rate divisor */
2289         12, 1,
2290         14, 1,          /* baud rate gen enable, src=rtxc (BRENABL) */
2291         11, 0x50,       /* clocks = br gen (RCBR | TCBR) */
2292         5,  0x6a,       /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2293         4,  0x44,       /* x16 clock, 1 stop (SB1 | X16CLK)*/
2294         3,  0xc1,       /* rx enable, 8 bits (RxENABLE | Rx8)*/
2295 };
2296
2297 /* These are for receiving and sending characters under the kgdb
2298  * source level kernel debugger.
2299  */
2300 void putDebugChar(char kgdb_char)
2301 {
2302         struct dec_zschannel *chan = zs_kgdbchan;
2303         while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2304                 RECOVERY_DELAY;
2305         write_zsdata(chan, kgdb_char);
2306 }
2307 char getDebugChar(void)
2308 {
2309         struct dec_zschannel *chan = zs_kgdbchan;
2310         while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2311                 eieio(); /*barrier();*/
2312         return read_zsdata(chan);
2313 }
2314 void kgdb_interruptible(int yes)
2315 {
2316         struct dec_zschannel *chan = zs_kgdbchan;
2317         int one, nine;
2318         nine = read_zsreg(chan, 9);
2319         if (yes == 1) {
2320                 one = EXT_INT_ENAB|INT_ALL_Rx;
2321                 nine |= MIE;
2322                 printk("turning serial ints on\n");
2323         } else {
2324                 one = RxINT_DISAB;
2325                 nine &= ~MIE;
2326                 printk("turning serial ints off\n");
2327         }
2328         write_zsreg(chan, 1, one);
2329         write_zsreg(chan, 9, nine);
2330 }
2331
2332 static int kgdbhook_init_channel(struct dec_serial* info)
2333 {
2334         return 0;
2335 }
2336
2337 static void kgdbhook_init_info(struct dec_serial* info)
2338 {
2339 }
2340
2341 static void kgdbhook_rx_char(struct dec_serial* info,
2342                              unsigned char ch, unsigned char stat)
2343 {
2344         if (ch == 0x03 || ch == '$')
2345                 breakpoint();
2346         if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
2347                 write_zsreg(info->zs_channel, 0, ERR_RES);
2348 }
2349
2350 /* This sets up the serial port we're using, and turns on
2351  * interrupts for that channel, so kgdb is usable once we're done.
2352  */
2353 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2354 {
2355         int brg;
2356         int i, x;
2357         volatile char *sccc = ms->control;
2358         brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2359         printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2360         for (i = 20000; i != 0; --i) {
2361                 x = *sccc; eieio();
2362         }
2363         for (i = 0; i < sizeof(scc_inittab); ++i) {
2364                 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2365                 i++;
2366         }
2367 }
2368 /* This is called at boot time to prime the kgdb serial debugging
2369  * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
2370  * for /dev/ttyb which is determined in setup_arch() from the
2371  * boot command line flags.
2372  */
2373 struct zs_hook zs_kgdbhook = {
2374         init_channel : kgdbhook_init_channel,
2375         init_info    : kgdbhook_init_info,
2376         cflags       : B38400|CS8|CLOCAL,
2377         rx_char      : kgdbhook_rx_char,
2378 }
2379
2380 void __init zs_kgdb_hook(int tty_num)
2381 {
2382         /* Find out how many Z8530 SCCs we have */
2383         if (zs_chain == 0)
2384                 probe_sccs();
2385         zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2386         zs_kgdbchan = zs_soft[tty_num].zs_channel;
2387         zs_soft[tty_num].change_needed = 0;
2388         zs_soft[tty_num].clk_divisor = 16;
2389         zs_soft[tty_num].zs_baud = 38400;
2390         zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2391         /* Turn on transmitter/receiver at 8-bits/char */
2392         kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2393         printk("KGDB: on channel %d initialized\n", tty_num);
2394         set_debug_traps(); /* init stub */
2395 }
2396 #endif /* ifdef CONFIG_KGDB */
2397
2398