]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/pc98/pc98/sio.c
Sync with follwing files:
[FreeBSD/FreeBSD.git] / sys / pc98 / pc98 / sio.c
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      from: @(#)com.c 7.5 (Berkeley) 5/16/91
34  *      $Id: sio.c,v 1.85 1999/04/03 15:51:14 kato Exp $
35  */
36
37 #include "opt_comconsole.h"
38 #include "opt_compat.h"
39 #include "opt_ddb.h"
40 #include "opt_devfs.h"
41 /* #include "opt_sio.h" */
42 #include "sio.h"
43 /* #include "pnp.h" */
44 #define NPNP 0
45
46 /*
47  * Serial driver, based on 386BSD-0.1 com driver.
48  * Mostly rewritten to use pseudo-DMA.
49  * Works for National Semiconductor NS8250-NS16550AF UARTs.
50  * COM driver, based on HP dca driver.
51  *
52  * Changes for PC-Card integration:
53  *      - Added PC-Card driver table and handlers
54  */
55 /*===============================================================
56  * 386BSD(98),FreeBSD-1.1x(98) com driver.
57  * -----
58  * modified for PC9801 by M.Ishii 
59  *                      Kyoto University Microcomputer Club (KMC)
60  * Chou "TEFUTEFU" Hirotomi
61  *                      Kyoto Univ.  the faculty of medicine
62  *===============================================================
63  * FreeBSD-2.0.1(98) sio driver.
64  * -----
65  * modified for pc98 Internal i8251 and MICRO CORE MC16550II
66  *                      T.Koike(hfc01340@niftyserve.or.jp)
67  * implement kernel device configuration
68  *                      aizu@orient.center.nitech.ac.jp
69  *
70  * Notes.
71  * -----
72  *  PC98 localization based on 386BSD(98) com driver. Using its PC98 local
73  *  functions.
74  *  This driver is under debugging,has bugs.
75  *
76  * 1) config
77  *  options COM_MULTIPORT  #if using MC16550II
78  *  device sio0 at nec? port 0x30  tty irq 4             #internal
79  *  device sio1 at nec? port 0xd2  tty irq 5 flags 0x101 #mc1
80  *  device sio2 at nec? port 0x8d2 tty flags 0x101       #mc2
81  *                         # ~~~~~iobase        ~~multi port flag
82  *                         #                   ~  master device is sio1
83  * 2) device
84  *  cd /dev; MAKEDEV ttyd0 ttyd1 ..
85  * 3) /etc/rc.serial
86  *  57600bps is too fast for sio0(internal8251)
87  *  my ex.
88  *    #set default speed 9600
89  *    modem()
90  *       :
91  *      stty </dev/ttyid$i crtscts 9600
92  *       :                 #       ~~~~ default speed(can change after init.)
93  *    modem 0 1 2
94  * 4) COMCONSOLE
95  *  not changed.
96  * 5) PC9861K,PIO9032B,B98_01
97  *  not tested.
98  */
99 /*
100  * modified for AIWA B98-01
101  * by T.Hatanou <hatanou@yasuda.comm.waseda.ac.jp>  last update: 15 Sep.1995 
102  *
103  * How to configure...
104  *   # options COM_MULTIPORT         # support for MICROCORE MC16550II
105  *      ... comment-out this line, which will conflict with B98_01.
106  *   options "B98_01"                # support for AIWA B98-01
107  *   device  sio1 at nec? port 0x00d1 tty irq ?
108  *   device  sio2 at nec? port 0x00d5 tty irq ?
109  *      ... you can leave these lines `irq ?', irq will be autodetected.
110  */
111 /*
112  * Modified by Y.Takahashi of Kogakuin University.
113  */
114
115 #ifdef PC98
116 #define COM_IF_INTERNAL         0x00
117 #define COM_IF_PC9861K_1        0x01
118 #define COM_IF_PC9861K_2        0x02
119 #define COM_IF_IND_SS_1         0x03
120 #define COM_IF_IND_SS_2         0x04
121 #define COM_IF_PIO9032B_1       0x05
122 #define COM_IF_PIO9032B_2       0x06
123 #define COM_IF_B98_01_1         0x07
124 #define COM_IF_B98_01_2         0x08
125 #define COM_IF_END1             COM_IF_B98_01_2
126 #define COM_IF_RSA98            0x10    /* same as COM_IF_NS16550 */
127 #define COM_IF_NS16550          0x11
128 #define COM_IF_SECOND_CCU       0x12    /* same as COM_IF_NS16550 */
129 #define COM_IF_MC16550II        0x13
130 #define COM_IF_MCRS98           0x14    /* same as COM_IF_MC16550II */
131 #define COM_IF_RSB3000          0x15
132 #define COM_IF_RSB384           0x16
133 #define COM_IF_MODEM_CARD       0x17    /* same as COM_IF_NS16550 */
134 #define COM_IF_RSA98III         0x18
135 #define COM_IF_ESP98            0x19
136 #define COM_IF_END2             COM_IF_ESP98
137 #endif /* PC98 */
138
139 #include <sys/param.h>
140 #include <sys/systm.h>
141 #include <sys/reboot.h>
142 #include <sys/malloc.h>
143 #include <sys/tty.h>
144 #include <sys/proc.h>
145 #include <sys/module.h>
146 #include <sys/conf.h>
147 #include <sys/dkstat.h>
148 #include <sys/fcntl.h>
149 #include <sys/interrupt.h>
150 #include <sys/kernel.h>
151 #include <sys/syslog.h>
152 #include <sys/sysctl.h>
153 #include <sys/bus.h>
154 #include <machine/bus.h>
155 #include <sys/rman.h>
156 #ifdef DEVFS
157 #include <sys/devfsext.h>
158 #endif
159 #include <sys/timepps.h>
160
161 #ifdef PC98
162 #include <pc98/pc98/pc98.h>
163 #include <pc98/pc98/pc98_machdep.h>
164 #include <i386/isa/ic/i8251.h>
165 #else
166 #include <isa/isareg.h>
167 #endif
168 #include <isa/isavar.h>
169 #include <machine/lock.h>
170
171 #include <machine/clock.h>
172 #include <machine/ipl.h>
173 #ifndef SMP
174 #include <machine/lock.h>
175 #endif
176 #include <machine/resource.h>
177
178 #include <isa/sioreg.h>
179
180 #ifdef COM_ESP
181 #include <i386/isa/ic/esp.h>
182 #endif
183 #include <i386/isa/ic/ns16550.h>
184 #ifdef PC98
185 #include <i386/isa/ic/rsa.h>
186 #endif
187
188 #if 0
189
190 #include "card.h"
191 #if NCARD > 0
192 #include <sys/module.h>
193 #include <pccard/cardinfo.h>
194 #include <pccard/slot.h>
195 #endif
196
197 #if NPNP > 0
198 #include <i386/isa/pnp.h>
199 #endif
200
201 #endif
202
203 #ifndef __i386__
204 #define disable_intr()  0
205 #define enable_intr()   0
206 #endif
207
208 #ifdef SMP
209 #define disable_intr()  COM_DISABLE_INTR()
210 #define enable_intr()   COM_ENABLE_INTR()
211 #endif /* SMP */
212
213 #ifndef EXTRA_SIO
214 #if NPNP > 0
215 #define EXTRA_SIO MAX_PNP_CARDS
216 #else
217 #define EXTRA_SIO 0
218 #endif
219 #endif
220
221 #define NSIOTOT (NSIO + EXTRA_SIO)
222
223 #define LOTS_OF_EVENTS  64      /* helps separate urgent events from input */
224
225 #define CALLOUT_MASK            0x80
226 #define CONTROL_MASK            0x60
227 #define CONTROL_INIT_STATE      0x20
228 #define CONTROL_LOCK_STATE      0x40
229 #define DEV_TO_UNIT(dev)        (MINOR_TO_UNIT(minor(dev)))
230 #define MINOR_MAGIC_MASK        (CALLOUT_MASK | CONTROL_MASK)
231 #define MINOR_TO_UNIT(mynor)    ((mynor) & ~MINOR_MAGIC_MASK)
232
233 #ifdef COM_MULTIPORT
234 /* checks in flags for multiport and which is multiport "master chip"
235  * for a given card
236  */
237 #define COM_ISMULTIPORT(flags)  ((flags) & 0x01)
238 #define COM_MPMASTER(flags)     (((flags) >> 8) & 0x0ff)
239 #define COM_NOTAST4(flags)      ((flags) & 0x04)
240 #endif /* COM_MULTIPORT */
241
242 #define COM_CONSOLE(flags)      ((flags) & 0x10)
243 #define COM_FORCECONSOLE(flags) ((flags) & 0x20)
244 #define COM_LLCONSOLE(flags)    ((flags) & 0x40)
245 #define COM_LOSESOUTINTS(flags) ((flags) & 0x08)
246 #define COM_NOFIFO(flags)               ((flags) & 0x02)
247 #define COM_ST16650A(flags)     ((flags) & 0x20000)
248 #define COM_C_NOPROBE           (0x40000)
249 #define COM_NOPROBE(flags)      ((flags) & COM_C_NOPROBE)
250 #define COM_C_IIR_TXRDYBUG      (0x80000)
251 #define COM_IIR_TXRDYBUG(flags) ((flags) & COM_C_IIR_TXRDYBUG)
252 #define COM_FIFOSIZE(flags)     (((flags) & 0xff000000) >> 24)
253
254 #ifdef PC98
255 #define com_emr         com_msr /* Extension mode register for RSB-2000/3000 */
256 #else
257 #define com_scr         7       /* scratch register for 16450-16550 (R/W) */
258 #endif
259
260 /*
261  * com state bits.
262  * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
263  * than the other bits so that they can be tested as a group without masking
264  * off the low bits.
265  *
266  * The following com and tty flags correspond closely:
267  *      CS_BUSY         = TS_BUSY (maintained by comstart(), siopoll() and
268  *                                 siostop())
269  *      CS_TTGO         = ~TS_TTSTOP (maintained by comparam() and comstart())
270  *      CS_CTS_OFLOW    = CCTS_OFLOW (maintained by comparam())
271  *      CS_RTS_IFLOW    = CRTS_IFLOW (maintained by comparam())
272  * TS_FLUSH is not used.
273  * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
274  * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
275  */
276 #define CS_BUSY         0x80    /* output in progress */
277 #define CS_TTGO         0x40    /* output not stopped by XOFF */
278 #define CS_ODEVREADY    0x20    /* external device h/w ready (CTS) */
279 #define CS_CHECKMSR     1       /* check of MSR scheduled */
280 #define CS_CTS_OFLOW    2       /* use CTS output flow control */
281 #define CS_DTR_OFF      0x10    /* DTR held off */
282 #define CS_ODONE        4       /* output completed */
283 #define CS_RTS_IFLOW    8       /* use RTS input flow control */
284 #define CSE_BUSYCHECK   1       /* siobusycheck() scheduled */
285
286 static  char const * const      error_desc[] = {
287 #define CE_OVERRUN                      0
288         "silo overflow",
289 #define CE_INTERRUPT_BUF_OVERFLOW       1
290         "interrupt-level buffer overflow",
291 #define CE_TTY_BUF_OVERFLOW             2
292         "tty-level buffer overflow",
293 };
294
295 #define CE_NTYPES                       3
296 #define CE_RECORD(com, errnum)          (++(com)->delta_error_counts[errnum])
297
298 /* types.  XXX - should be elsewhere */
299 typedef u_int   Port_t;         /* hardware port */
300 typedef u_char  bool_t;         /* boolean */
301
302 /* queue of linear buffers */
303 struct lbq {
304         u_char  *l_head;        /* next char to process */
305         u_char  *l_tail;        /* one past the last char to process */
306         struct lbq *l_next;     /* next in queue */
307         bool_t  l_queued;       /* nonzero if queued */
308 };
309
310 /* com device structure */
311 struct com_s {
312         u_int   flags;          /* Copy isa device flags */
313         u_char  state;          /* miscellaneous flag bits */
314         bool_t  active_out;     /* nonzero if the callout device is open */
315         u_char  cfcr_image;     /* copy of value written to CFCR */
316 #ifdef COM_ESP
317         bool_t  esp;            /* is this unit a hayes esp board? */
318 #endif
319         u_char  extra_state;    /* more flag bits, separate for order trick */
320         u_char  fifo_image;     /* copy of value written to FIFO */
321         bool_t  hasfifo;        /* nonzero for 16550 UARTs */
322         bool_t  st16650a;       /* Is a Startech 16650A or RTS/CTS compat */
323         bool_t  loses_outints;  /* nonzero if device loses output interrupts */
324         u_char  mcr_image;      /* copy of value written to MCR */
325 #ifdef COM_MULTIPORT
326         bool_t  multiport;      /* is this unit part of a multiport device? */
327 #endif /* COM_MULTIPORT */
328         bool_t  no_irq;         /* nonzero if irq is not attached */
329         bool_t  gone;           /* hardware disappeared */
330         bool_t  poll;           /* nonzero if polling is required */
331         bool_t  poll_output;    /* nonzero if polling for output is required */
332         int     unit;           /* unit number */
333         int     dtr_wait;       /* time to hold DTR down on close (* 1/hz) */
334         u_int   tx_fifo_size;
335         u_int   wopeners;       /* # processes waiting for DCD in open() */
336
337         /*
338          * The high level of the driver never reads status registers directly
339          * because there would be too many side effects to handle conveniently.
340          * Instead, it reads copies of the registers stored here by the
341          * interrupt handler.
342          */
343         u_char  last_modem_status;      /* last MSR read by intr handler */
344         u_char  prev_modem_status;      /* last MSR handled by high level */
345
346         u_char  hotchar;        /* ldisc-specific char to be handled ASAP */
347         u_char  *ibuf;          /* start of input buffer */
348         u_char  *ibufend;       /* end of input buffer */
349         u_char  *ibufold;       /* old input buffer, to be freed */
350         u_char  *ihighwater;    /* threshold in input buffer */
351         u_char  *iptr;          /* next free spot in input buffer */
352         int     ibufsize;       /* size of ibuf (not include error bytes) */
353         int     ierroff;        /* offset of error bytes in ibuf */
354
355         struct lbq      obufq;  /* head of queue of output buffers */
356         struct lbq      obufs[2];       /* output buffers */
357
358 #ifdef PC98
359         Port_t  cmd_port;
360         Port_t  sts_port;
361         Port_t  in_modem_port;
362         Port_t  intr_ctrl_port;
363         int     intr_enable;
364         int     pc98_prev_modem_status;
365         int     pc98_modem_delta;
366         int     modem_car_chg_timer;
367         int     pc98_prev_siocmd;
368         int     pc98_prev_siomod;
369         int     modem_checking;
370         int     pc98_if_type;
371 #endif /* PC98 */
372         Port_t  data_port;      /* i/o ports */
373 #ifdef COM_ESP
374         Port_t  esp_port;
375 #endif
376         Port_t  int_id_port;
377         Port_t  iobase;
378 #ifdef PC98
379         Port_t  rsabase;        /* iobase address of a I/O-DATA RSA board */
380 #endif
381         Port_t  modem_ctl_port;
382         Port_t  line_status_port;
383         Port_t  modem_status_port;
384         Port_t  intr_ctl_port;  /* Ports of IIR register */
385
386         struct tty      *tp;    /* cross reference */
387
388         /* Initial state. */
389         struct termios  it_in;  /* should be in struct tty */
390         struct termios  it_out;
391
392         /* Lock state. */
393         struct termios  lt_in;  /* should be in struct tty */
394         struct termios  lt_out;
395
396         bool_t  do_timestamp;
397         bool_t  do_dcd_timestamp;
398         struct timeval  timestamp;
399         struct timeval  dcd_timestamp;
400         struct  pps_state pps;
401
402         u_long  bytes_in;       /* statistics */
403         u_long  bytes_out;
404         u_int   delta_error_counts[CE_NTYPES];
405         u_long  error_counts[CE_NTYPES];
406
407         /*
408          * Data area for output buffers.  Someday we should build the output
409          * buffer queue without copying data.
410          */
411 #ifdef PC98
412         int     obufsize;
413         u_char  *obuf1;
414         u_char  *obuf2;
415 #else
416         u_char  obuf1[256];
417         u_char  obuf2[256];
418 #endif
419 #ifdef DEVFS
420         void    *devfs_token_ttyd;
421         void    *devfs_token_ttyl;
422         void    *devfs_token_ttyi;
423         void    *devfs_token_cuaa;
424         void    *devfs_token_cual;
425         void    *devfs_token_cuai;
426 #endif
427 };
428
429 #ifdef COM_ESP
430 static  int     espattach       __P((struct isa_device *isdp, struct com_s *com,
431                                      Port_t esp_port));
432 #endif
433 static  int     sioattach       __P((device_t dev));
434
435 static  timeout_t siobusycheck;
436 static  timeout_t siodtrwakeup;
437 static  void    comhardclose    __P((struct com_s *com));
438 static  void    sioinput        __P((struct com_s *com));
439 static  void    siointr1        __P((struct com_s *com));
440 static  void    siointr         __P((void *arg));
441 static  int     commctl         __P((struct com_s *com, int bits, int how));
442 static  int     comparam        __P((struct tty *tp, struct termios *t));
443 static  swihand_t siopoll;
444 static  int     sioprobe        __P((device_t dev));
445 static  void    siosettimeout   __P((void));
446 static  int     siosetwater     __P((struct com_s *com, speed_t speed));
447 static  void    comstart        __P((struct tty *tp));
448 static  timeout_t comwakeup;
449 static  void    disc_optim      __P((struct tty *tp, struct termios *t,
450                                      struct com_s *com));
451
452
453 static char driver_name[] = "sio";
454
455 /* table and macro for fast conversion from a unit number to its com struct */
456 static  devclass_t      sio_devclass;
457 #define com_addr(unit)  ((struct com_s *) \
458                          devclass_get_softc(sio_devclass, unit))
459
460 static device_method_t sio_methods[] = {
461         /* Device interface */
462         DEVMETHOD(device_probe,         sioprobe),
463         DEVMETHOD(device_attach,        sioattach),
464
465         { 0, 0 }
466 };
467
468 static driver_t sio_driver = {
469         driver_name,
470         sio_methods,
471         DRIVER_TYPE_TTY,
472         sizeof(struct com_s),
473 };
474
475 static  d_open_t        sioopen;
476 static  d_close_t       sioclose;
477 static  d_read_t        sioread;
478 static  d_write_t       siowrite;
479 static  d_ioctl_t       sioioctl;
480 static  d_stop_t        siostop;
481 static  d_devtotty_t    siodevtotty;
482
483 #define CDEV_MAJOR      28
484 static  struct cdevsw   sio_cdevsw = {
485         sioopen,        sioclose,       sioread,        siowrite,
486         sioioctl,       siostop,        noreset,        siodevtotty,
487         ttpoll,         nommap,         NULL,           driver_name,
488         NULL,           -1,             nodump,         nopsize,
489         D_TTY,
490 };
491
492 int     comconsole = -1;
493 static  volatile speed_t        comdefaultrate = CONSPEED;
494 static  volatile speed_t        gdbdefaultrate = CONSPEED;
495 static  u_int   com_events;     /* input chars + weighted output completions */
496 static  Port_t  siocniobase;
497 static  Port_t  siogdbiobase;
498 static  bool_t  sio_registered;
499 static  int     sio_timeout;
500 static  int     sio_timeouts_until_log;
501 static  struct  callout_handle sio_timeout_handle
502     = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle);
503 #if 0 /* XXX */
504 static struct tty       *sio_tty[NSIOTOT];
505 #else
506 static struct tty       sio_tty[NSIOTOT];
507 #endif
508 static  const int       nsio_tty = NSIOTOT;
509
510 #ifdef PC98
511 struct  siodev  {
512         short   if_type;
513         short   irq;
514         Port_t  cmd, sts, ctrl, mod;
515 };
516 static  int     sysclock;
517
518 #define COM_INT_DISABLE         {int previpri; previpri=spltty();
519 #define COM_INT_ENABLE          splx(previpri);}
520 #define IEN_TxFLAG              IEN_Tx
521
522 #define COM_CARRIER_DETECT_EMULATE      0
523 #define PC98_CHECK_MODEM_INTERVAL       (hz/10)
524 #define DCD_OFF_TOLERANCE               2
525 #define DCD_ON_RECOGNITION              2
526 #define IS_8251(if_type)                (!(if_type & 0x10))
527 #define COM1_EXT_CLOCK                  0x40000
528
529 static  void    commint         __P((dev_t dev));
530 static  void    com_tiocm_set   __P((struct com_s *com, int msr));
531 static  void    com_tiocm_bis   __P((struct com_s *com, int msr));
532 static  void    com_tiocm_bic   __P((struct com_s *com, int msr));
533 static  int     com_tiocm_get   __P((struct com_s *com));
534 static  int     com_tiocm_get_delta     __P((struct com_s *com));
535 static  void    pc98_msrint_start       __P((dev_t dev));
536 static  void    com_cflag_and_speed_set __P((struct com_s *com, int cflag, int speed));
537 static  int     pc98_ttspeedtab         __P((struct com_s *com, int speed));
538 static  int     pc98_get_modem_status   __P((struct com_s *com));
539 static  timeout_t       pc98_check_msr;
540 static  void    pc98_set_baud_rate      __P((struct com_s *com, int count));
541 static  void    pc98_i8251_reset        __P((struct com_s *com, int mode, int command));
542 static  void    pc98_disable_i8251_interrupt    __P((struct com_s *com, int mod));
543 static  void    pc98_enable_i8251_interrupt     __P((struct com_s *com, int mod));
544 static  int     pc98_check_i8251_interrupt      __P((struct com_s *com));
545 static  int     pc98_i8251_get_cmd      __P((struct com_s *com));
546 static  int     pc98_i8251_get_mod      __P((struct com_s *com));
547 static  void    pc98_i8251_set_cmd      __P((struct com_s *com, int x));
548 static  void    pc98_i8251_or_cmd       __P((struct com_s *com, int x));
549 static  void    pc98_i8251_clear_cmd    __P((struct com_s *com, int x));
550 static  void    pc98_i8251_clear_or_cmd __P((struct com_s *com, int clr, int x));
551 static  int     pc98_check_if_type      __P((device_t dev, struct siodev *iod));
552 static  void    pc98_check_sysclock     __P((void));
553 static  int     pc98_set_ioport         __P((struct com_s *com, int id_flags));
554
555 #define com_int_Tx_disable(com) \
556                 pc98_disable_i8251_interrupt(com,IEN_Tx|IEN_TxEMP)
557 #define com_int_Tx_enable(com) \
558                 pc98_enable_i8251_interrupt(com,IEN_TxFLAG)
559 #define com_int_Rx_disable(com) \
560                 pc98_disable_i8251_interrupt(com,IEN_Rx)
561 #define com_int_Rx_enable(com) \
562                 pc98_enable_i8251_interrupt(com,IEN_Rx)
563 #define com_int_TxRx_disable(com) \
564                 pc98_disable_i8251_interrupt(com,IEN_Tx|IEN_TxEMP|IEN_Rx)
565 #define com_int_TxRx_enable(com) \
566                 pc98_enable_i8251_interrupt(com,IEN_TxFLAG|IEN_Rx)
567 #define com_send_break_on(com) \
568                 pc98_i8251_or_cmd(com,CMD8251_SBRK)
569 #define com_send_break_off(com) \
570                 pc98_i8251_clear_cmd(com,CMD8251_SBRK)
571
572 static struct speedtab pc98speedtab[] = {       /* internal RS232C interface */
573         { 0,            0, },
574         { 50,           50, },
575         { 75,           75, },
576         { 150,          150, },
577         { 200,          200, },
578         { 300,          300, },
579         { 600,          600, },
580         { 1200,         1200, },
581         { 2400,         2400, },
582         { 4800,         4800, },
583         { 9600,         9600, },
584         { 19200,        19200, },
585         { 38400,        38400, },
586         { 51200,        51200, },
587         { 76800,        76800, },
588         { 20800,        20800, },
589         { 31200,        31200, },
590         { 41600,        41600, },
591         { 62400,        62400, },
592         { -1,           -1 }
593 };
594 static struct speedtab pc98fast_speedtab[] = {
595         { 9600,         0x80 | COMBRD(9600), },
596         { 19200,        0x80 | COMBRD(19200), },
597         { 38400,        0x80 | COMBRD(38400), },
598         { 57600,        0x80 | COMBRD(57600), },
599         { 115200,       0x80 | COMBRD(115200), },
600         { -1,           -1 }
601 };
602 static struct speedtab comspeedtab_pio9032b[] = {
603         { 300,          6, },
604         { 600,          5, },
605         { 1200,         4, },
606         { 2400,         3, },
607         { 4800,         2, },
608         { 9600,         1, },
609         { 19200,        0, },
610         { 38400,        7, },
611         { -1,           -1 }
612 };
613 static struct speedtab comspeedtab_b98_01[] = {
614         { 75,           11, },
615         { 150,          10, },
616         { 300,          9, },
617         { 600,          8, },
618         { 1200,         7, },
619         { 2400,         6, },
620         { 4800,         5, },
621         { 9600,         4, },
622         { 19200,        3, },
623         { 38400,        2, },
624         { 76800,        1, },
625         { 153600,       0, },
626         { -1,           -1 }
627 };
628 static struct speedtab comspeedtab_mc16550[] = {
629         { 300,          1536, },
630         { 600,          768, },
631         { 1200,         384, },
632         { 2400,         192, },
633         { 4800,         96, },
634         { 9600,         48, },
635         { 19200,        24, },
636         { 38400,        12, },
637         { 57600,        8, },
638         { 115200,       4, },
639         { 153600,       3, },
640         { 230400,       2, },
641         { 460800,       1, },
642         { -1,           -1 }
643 };
644 static struct speedtab comspeedtab_rsb384[] = {
645         { 300,          3840, },
646         { 600,          1920, },
647         { 1200,         960, },
648         { 2400,         480, },
649         { 4800,         240, },
650         { 9600,         120, },
651         { 19200,        60, },
652         { 38400,        30, },
653         { 57600,        20, },
654         { 115200,       10, },
655         { 128000,       9, },
656         { 144000,       8, },
657         { 192000,       6, },
658         { 230400,       5, },
659         { 288000,       4, },
660         { 384000,       3, },
661         { 576000,       2, },
662         { 1152000,      1, },
663         { -1,           -1 }
664 };
665 static  struct speedtab comspeedtab_rsa[] = {
666         { 0,            0 },
667         { 50,           COMBRD_RSA(50) },
668         { 75,           COMBRD_RSA(75) },
669         { 110,          COMBRD_RSA(110) },
670         { 134,          COMBRD_RSA(134) },
671         { 150,          COMBRD_RSA(150) },
672         { 200,          COMBRD_RSA(200) },
673         { 300,          COMBRD_RSA(300) },
674         { 600,          COMBRD_RSA(600) },
675         { 1200,         COMBRD_RSA(1200) },
676         { 1800,         COMBRD_RSA(1800) },
677         { 2400,         COMBRD_RSA(2400) },
678         { 4800,         COMBRD_RSA(4800) },
679         { 9600,         COMBRD_RSA(9600) },
680         { 19200,        COMBRD_RSA(19200) },
681         { 38400,        COMBRD_RSA(38400) },
682         { 57600,        COMBRD_RSA(57600) },
683         { 115200,       COMBRD_RSA(115200) },
684         { 230400,       COMBRD_RSA(230400) },
685         { 460800,       COMBRD_RSA(460800) },
686         { 921600,       COMBRD_RSA(921600) },
687         { -1,           -1 }
688 };
689 #endif /* PC98 */
690
691 static  struct speedtab comspeedtab[] = {
692         { 0,            0 },
693         { 50,           COMBRD(50) },
694         { 75,           COMBRD(75) },
695         { 110,          COMBRD(110) },
696         { 134,          COMBRD(134) },
697         { 150,          COMBRD(150) },
698         { 200,          COMBRD(200) },
699         { 300,          COMBRD(300) },
700         { 600,          COMBRD(600) },
701         { 1200,         COMBRD(1200) },
702         { 1800,         COMBRD(1800) },
703         { 2400,         COMBRD(2400) },
704         { 4800,         COMBRD(4800) },
705         { 9600,         COMBRD(9600) },
706         { 19200,        COMBRD(19200) },
707         { 38400,        COMBRD(38400) },
708         { 57600,        COMBRD(57600) },
709         { 115200,       COMBRD(115200) },
710         { -1,           -1 }
711 };
712
713 #ifdef PC98
714 struct {
715         char    *name;
716         short   port_table[7];
717         short   irr_mask;
718         struct speedtab *speedtab;
719         short   check_irq;
720 } if_8251_type[] = {
721         /* COM_IF_INTERNAL */
722         { " (internal)", {0x30, 0x32, 0x32, 0x33, 0x35, -1, -1},
723              -1, pc98speedtab, 1 },
724         /* COM_IF_PC9861K_1 */
725         { " (PC9861K)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, -1, -1},
726              3, NULL, 1 },
727         /* COM_IF_PC9861K_2 */
728         { " (PC9861K)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, -1, -1},
729               3, NULL, 1 },
730         /* COM_IF_IND_SS_1 */
731         { " (IND-SS)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, 0xb3, -1},
732              3, comspeedtab_mc16550, 1 },
733         /* COM_IF_IND_SS_2 */
734         { " (IND-SS)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, 0xbb, -1},
735              3, comspeedtab_mc16550, 1 },
736         /* COM_IF_PIO9032B_1 */
737         { " (PIO9032B)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, 0xb8, -1},
738               7, comspeedtab_pio9032b, 1 },
739         /* COM_IF_PIO9032B_2 */
740         { " (PIO9032B)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, 0xba, -1},
741               7, comspeedtab_pio9032b, 1 },
742         /* COM_IF_B98_01_1 */
743         { " (B98-01)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, 0xd1, 0xd3},
744               7, comspeedtab_b98_01, 0 },
745         /* COM_IF_B98_01_2 */
746         { " (B98-01)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, 0xd5, 0xd7},
747              7, comspeedtab_b98_01, 0 },
748 };
749 #define PC98SIO_data_port(type)         (if_8251_type[type].port_table[0])
750 #define PC98SIO_cmd_port(type)          (if_8251_type[type].port_table[1])
751 #define PC98SIO_sts_port(type)          (if_8251_type[type].port_table[2])
752 #define PC98SIO_in_modem_port(type)     (if_8251_type[type].port_table[3])
753 #define PC98SIO_intr_ctrl_port(type)    (if_8251_type[type].port_table[4])
754 #define PC98SIO_baud_rate_port(type)    (if_8251_type[type].port_table[5])
755 #define PC98SIO_func_port(type)         (if_8251_type[type].port_table[6])
756
757 struct {
758         char    *name;
759         short   irr_read;
760         short   irr_write;
761         short   port_shift;
762         short   io_size;
763         struct speedtab *speedtab;
764 } if_16550a_type[] = {
765         /* COM_IF_RSA98 */
766         { " (RSA-98)", -1, -1, 0, IO_COMSIZE, comspeedtab },
767         /* COM_IF_NS16550 */
768         { "", -1, -1, 0, IO_COMSIZE, comspeedtab },
769         /* COM_IF_SECOND_CCU */
770         { "", -1, -1, 0, IO_COMSIZE, comspeedtab },
771         /* COM_IF_MC16550II */
772         { " (MC16550II)", -1, 0x1000, 8, 1, comspeedtab_mc16550 },
773         /* COM_IF_MCRS98 */
774         { " (MC-RS98)", -1, 0x1000, 8, 1, comspeedtab_mc16550 },
775         /* COM_IF_RSB3000 */
776         { " (RSB-3000)", 0xbf, -1, 1, 1, comspeedtab_rsb384 },
777         /* COM_IF_RSB384 */
778         { " (RSB-384)", 0xbf, -1, 1, 1, comspeedtab_rsb384 },
779         /* COM_IF_MODEM_CARD */
780         { "", -1, -1, 0, IO_COMSIZE, comspeedtab },
781         /* COM_IF_RSA98III */
782         { " (RSA-98III)", -1, -1, 0, 16, comspeedtab_rsa },
783         /* COM_IF_ESP98 */
784         { " (ESP98)", -1, -1, 1, 1, comspeedtab_mc16550 },
785 };
786 #endif /* PC98 */
787
788 #ifdef COM_ESP
789 #ifdef PC98
790
791 /* XXX configure this properly. */
792 static  Port_t  likely_com_ports[] = { 0, 0xb0, 0xb1, 0 };
793 static  Port_t  likely_esp_ports[] = { 0xc0d0, 0 };
794
795 #define ESP98_CMD1      (ESP_CMD1 * 0x100)
796 #define ESP98_CMD2      (ESP_CMD2 * 0x100)
797 #define ESP98_STATUS1   (ESP_STATUS1 * 0x100)
798 #define ESP98_STATUS2   (ESP_STATUS2 * 0x100)
799
800 #else /* PC98 */
801
802 /* XXX configure this properly. */
803 static  Port_t  likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, };
804 static  Port_t  likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 };
805
806 #endif /* PC98 */
807 #endif
808
809 /*
810  * handle sysctl read/write requests for console speed
811  * 
812  * In addition to setting comdefaultrate for I/O through /dev/console,
813  * also set the initial and lock values for the /dev/ttyXX device
814  * if there is one associated with the console.  Finally, if the /dev/tty
815  * device has already been open, change the speed on the open running port
816  * itself.
817  */
818
819 static int
820 sysctl_machdep_comdefaultrate SYSCTL_HANDLER_ARGS
821 {
822         int error, s;
823         speed_t newspeed;
824         struct com_s *com;
825         struct tty *tp;
826
827         newspeed = comdefaultrate;
828
829         error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req);
830         if (error || !req->newptr)
831                 return (error);
832
833         comdefaultrate = newspeed;
834
835         if (comconsole < 0)             /* serial console not selected? */
836                 return (0);
837
838         com = com_addr(comconsole);
839         if (!com)
840                 return (ENXIO);
841
842         /*
843          * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX
844          * (note, the lock rates really are boolean -- if non-zero, disallow
845          *  speed changes)
846          */
847         com->it_in.c_ispeed  = com->it_in.c_ospeed =
848         com->lt_in.c_ispeed  = com->lt_in.c_ospeed =
849         com->it_out.c_ispeed = com->it_out.c_ospeed =
850         com->lt_out.c_ispeed = com->lt_out.c_ospeed = comdefaultrate;
851
852         /*
853          * if we're open, change the running rate too
854          */
855         tp = com->tp;
856         if (tp && (tp->t_state & TS_ISOPEN)) {
857                 tp->t_termios.c_ispeed =
858                 tp->t_termios.c_ospeed = comdefaultrate;
859                 s = spltty();
860                 error = comparam(tp, &tp->t_termios);
861                 splx(s);
862         }
863         return error;
864 }
865
866 SYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW,
867             0, 0, sysctl_machdep_comdefaultrate, "I", "");
868
869 #if NCARD > 0
870 /*
871  *      PC-Card (PCMCIA) specific code.
872  */
873 static int      sioinit         __P((struct pccard_devinfo *));
874 static void     siounload       __P((struct pccard_devinfo *));
875 static int      card_intr       __P((struct pccard_devinfo *));
876
877 PCCARD_MODULE(sio, sioinit, siounload, card_intr, 0, tty_imask);
878
879 /*
880  *      Initialize the device - called from Slot manager.
881  */
882 int
883 sioinit(struct pccard_devinfo *devi)
884 {
885
886         /* validate unit number. */
887         if (devi->isahd.id_unit >= (NSIOTOT))
888                 return(ENODEV);
889         /* Make sure it isn't already probed. */
890         if (com_addr(devi->isahd.id_unit))
891                 return(EBUSY);
892
893         /* It's already probed as serial by Upper */
894         devi->isahd.id_flags |= COM_C_NOPROBE; 
895
896         /*
897          * Probe the device. If a value is returned, the
898          * device was found at the location.
899          */
900         if (sioprobe(&devi->isahd) == 0)
901                 return(ENXIO);
902         if (sioattach(&devi->isahd) == 0)
903                 return(ENXIO);
904
905         return(0);
906 }
907
908 /*
909  *      siounload - unload the driver and clear the table.
910  *      XXX TODO:
911  *      This is usually called when the card is ejected, but
912  *      can be caused by a modunload of a controller driver.
913  *      The idea is to reset the driver's view of the device
914  *      and ensure that any driver entry points such as
915  *      read and write do not hang.
916  */
917 static void
918 siounload(struct pccard_devinfo *devi)
919 {
920         struct com_s    *com;
921
922         if (!devi) {
923                 printf("NULL devi in siounload\n");
924                 return;
925         }
926         com = com_addr(devi->isahd.id_unit);
927         if (!com) {
928                 printf("NULL com in siounload\n");
929                 return;
930         }
931         if (!com->iobase) {
932                 printf("sio%d already unloaded!\n",devi->isahd.id_unit);
933                 return;
934         }
935         if (com->tp && (com->tp->t_state & TS_ISOPEN)) {
936                 com->gone = 1;
937                 printf("sio%d: unload\n", devi->isahd.id_unit);
938                 com->tp->t_gen++;
939                 ttyclose(com->tp);
940                 ttwakeup(com->tp);
941                 ttwwakeup(com->tp);
942         } else {
943                 com_addr(com->unit) = NULL;
944                 if (com->ibuf != NULL)
945                         free(com->ibuf, M_DEVBUF);
946                 free(com, M_DEVBUF);
947                 printf("sio%d: unload,gone\n", devi->isahd.id_unit);
948         }
949 }
950
951 /*
952  *      card_intr - Shared interrupt called from
953  *      front end of PC-Card handler.
954  */
955 static int
956 card_intr(struct pccard_devinfo *devi)
957 {
958         struct com_s    *com;
959
960         COM_LOCK();
961         com = com_addr(devi->isahd.id_unit);
962         if (com && !com->gone)
963                 siointr1(com_addr(devi->isahd.id_unit));
964         COM_UNLOCK();
965         return(1);
966 }
967 #endif /* NCARD > 0 */
968
969 #define SET_FLAG(dev, bit)      isa_set_flags(dev, isa_get_flags(dev) | (bit))
970 #define CLR_FLAG(dev, bit)      isa_set_flags(dev, isa_get_flags(dev) & ~(bit))
971
972 static int
973 sioprobe(dev)
974         device_t        dev;
975 {
976         static bool_t   already_init;
977         bool_t          failures[10];
978         int             fn;
979         device_t        idev;
980         Port_t          iobase;
981         intrmask_t      irqmap[4];
982         intrmask_t      irqs;
983         u_char          mcr_image;
984         int             result;
985         device_t        xdev;
986         u_int           flags = isa_get_flags(dev);
987 #ifdef PC98
988         int             irqout=0;
989         int             ret = 0;
990         int             tmp;
991         int             port_shift = 0;
992         struct siodev   iod;
993         Port_t          rsabase = NULL;
994 #endif
995
996         if (!already_init) {
997                 /*
998                  * Turn off MCR_IENABLE for all likely serial ports.  An unused
999                  * port with its MCR_IENABLE gate open will inhibit interrupts
1000                  * from any used port that shares the interrupt vector.
1001                  * XXX the gate enable is elsewhere for some multiports.
1002                  */
1003                 device_t *devs;
1004                 int count, i;
1005
1006                 devclass_get_devices(sio_devclass, &devs, &count);
1007 #ifdef PC98
1008                 for (i = 0; i < count; i++) {
1009                         xdev = devs[i];
1010                         tmp = (flags >> 24) & 0xff;
1011                         if (IS_8251(tmp))
1012                             outb((isa_get_port(xdev) & 0xff00) | PC98SIO_cmd_port(tmp & 0x0f), 0xf2);
1013                         else
1014                             if (tmp == COM_IF_RSA98III) {
1015                                 rsabase = isa_get_port(xdev) & 0xfff0;
1016                                 outb(isa_get_port(xdev) + 8 + (com_mcr << if_16550a_type[tmp & 0x0f].port_shift), 0);
1017                             } else
1018                                 outb(isa_get_port(xdev) + (com_mcr << if_16550a_type[tmp & 0x0f].port_shift), 0);
1019                 }
1020 #else
1021                 for (i = 0; i < count; i++) {
1022                         xdev = devs[i];
1023                         outb(isa_get_port(xdev) + com_mcr, 0);
1024                 }
1025 #endif
1026                 free(devs, M_TEMP);
1027                 already_init = TRUE;
1028         }
1029
1030         if (COM_LLCONSOLE(flags)) {
1031                 printf("sio%d: reserved for low-level i/o\n",
1032                        device_get_unit(dev));
1033                 return (ENXIO);
1034         }
1035
1036 #ifdef PC98
1037         DELAY(10);
1038
1039         /*
1040          * If the port is i8251 UART (internal, B98_01)
1041          */
1042         if (pc98_check_if_type(dev, &iod) == -1)
1043                 return 0;
1044         if (iod.irq > 0)
1045                 isa_set_irq(dev, iod.irq);
1046         if (IS_8251(iod.if_type)) {
1047                 outb(iod.cmd, 0);
1048                 DELAY(10);
1049                 outb(iod.cmd, 0);
1050                 DELAY(10);
1051                 outb(iod.cmd, 0);
1052                 DELAY(10);
1053                 outb(iod.cmd, CMD8251_RESET);
1054                 DELAY(1000);            /* for a while...*/
1055                 outb(iod.cmd, 0xf2);    /* MODE (dummy) */
1056                 DELAY(10);
1057                 outb(iod.cmd, 0x01);    /* CMD (dummy) */
1058                 DELAY(1000);            /* for a while...*/
1059                 if (( inb(iod.sts) & STS8251_TxEMP ) == 0 ) {
1060                         ret = 0;
1061                 }
1062                 if (if_8251_type[iod.if_type & 0x0f].check_irq) {
1063                     COM_INT_DISABLE
1064                     tmp = ( inb( iod.ctrl ) & ~(IEN_Rx|IEN_TxEMP|IEN_Tx));
1065                     outb( iod.ctrl, tmp|IEN_TxEMP );
1066                     DELAY(10);
1067                     ret = isa_irq_pending() ? 4 : 0;
1068                     outb( iod.ctrl, tmp );
1069                     COM_INT_ENABLE
1070                 } else {
1071                     /*
1072                      * B98_01 doesn't activate TxEMP interrupt line
1073                      * when being reset, so we can't check irq pending.
1074                      */
1075                     ret = 4;
1076                 }
1077                 if (epson_machine_id==0x20) {   /* XXX */
1078                         ret = 4;
1079                 }
1080                 return ret;
1081         }
1082 #endif /* PC98 */
1083         /*
1084          * If the device is on a multiport card and has an AST/4
1085          * compatible interrupt control register, initialize this
1086          * register and prepare to leave MCR_IENABLE clear in the mcr.
1087          * Otherwise, prepare to set MCR_IENABLE in the mcr.
1088          * Point idev to the device struct giving the correct id_irq.
1089          * This is the struct for the master device if there is one.
1090          */
1091         idev = dev;
1092         mcr_image = MCR_IENABLE;
1093 #ifdef PC98
1094         if (iod.if_type == COM_IF_RSA98III) {
1095                 mcr_image = 0;
1096                 rsabase = isa_get_port(idev) & 0xfff0;
1097                 if (rsabase != isa_get_port(idev))
1098                         return(0);
1099                 outb(rsabase + rsa_msr,   0x04);
1100                 outb(rsabase + rsa_frr,   0x00);
1101                 if ((inb(rsabase + rsa_srr) & 0x36) != 0x36)
1102                         return (0);
1103                 outb(rsabase + rsa_ier,   0x00);
1104                 outb(rsabase + rsa_frr,   0x00);
1105                 outb(rsabase + rsa_tivsr, 0x00);
1106                 outb(rsabase + rsa_tcr,   0x00);
1107         }
1108 #endif /* PC98 */
1109 #ifdef COM_MULTIPORT
1110         if (COM_ISMULTIPORT(flags)) {
1111                 idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags));
1112                 if (idev == NULL) {
1113                         printf("sio%d: master device %d not configured\n",
1114                                device_get_unit(dev), COM_MPMASTER(flags));
1115                         isa_set_irq(dev, 0);
1116                         idev = dev;
1117                 }
1118 #ifndef PC98
1119                 if (!COM_NOTAST4(flags)) {
1120                         outb(isa_get_port(idev) + com_scr,
1121                              isa_get_irq(idev) >= 0 ? 0x80 : 0);
1122                         mcr_image = 0;
1123                 }
1124 #endif /* !PC98 */
1125         }
1126 #endif /* COM_MULTIPORT */
1127         if (isa_get_irq(idev) < 0)
1128                 mcr_image = 0;
1129
1130 #ifdef PC98
1131         tmp = if_16550a_type[iod.if_type & 0x0f].irr_write;
1132         if (tmp != -1) {
1133             /* MC16550II */
1134             switch (isa_get_irq(idev)) {
1135             case 3: irqout = 4; break;
1136             case 5: irqout = 5; break;
1137             case 6: irqout = 6; break;
1138             case 12: irqout = 7; break;
1139             default:
1140                 printf("sio%d: irq configuration error\n",
1141                        device_get_unit(dev));
1142                 return (0);
1143             }
1144             outb((isa_get_port(dev) & 0x00ff) | tmp, irqout);
1145         }
1146         port_shift = if_16550a_type[iod.if_type & 0x0f].port_shift;
1147 #endif
1148         bzero(failures, sizeof failures);
1149         iobase = isa_get_port(dev);
1150 #ifdef PC98
1151         if (iod.if_type == COM_IF_RSA98III)
1152                 iobase += 8;
1153 #endif
1154
1155         /*
1156          * We don't want to get actual interrupts, just masked ones.
1157          * Interrupts from this line should already be masked in the ICU,
1158          * but mask them in the processor as well in case there are some
1159          * (misconfigured) shared interrupts.
1160          */
1161         disable_intr();
1162 /* EXTRA DELAY? */
1163
1164         /*
1165          * Initialize the speed and the word size and wait long enough to
1166          * drain the maximum of 16 bytes of junk in device output queues.
1167          * The speed is undefined after a master reset and must be set
1168          * before relying on anything related to output.  There may be
1169          * junk after a (very fast) soft reboot and (apparently) after
1170          * master reset.
1171          * XXX what about the UART bug avoided by waiting in comparam()?
1172          * We don't want to to wait long enough to drain at 2 bps.
1173          */
1174         if (iobase == siocniobase)
1175                 DELAY((16 + 1) * 1000000 / (comdefaultrate / 10));
1176         else {
1177 #ifdef PC98
1178                 tmp = ttspeedtab(SIO_TEST_SPEED,
1179                                  if_16550a_type[iod.if_type & 0x0f].speedtab);
1180                 outb(iobase + (com_cfcr << port_shift), CFCR_DLAB|CFCR_8BITS);
1181                 outb(iobase + (com_dlbl << port_shift), tmp & 0xff);
1182                 outb(iobase + (com_dlbh << port_shift), (tmp >> 8) & 0xff);
1183                 outb(iobase + (com_cfcr << port_shift), CFCR_8BITS);
1184 #else
1185                 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
1186                 outb(iobase + com_dlbl, COMBRD(SIO_TEST_SPEED) & 0xff);
1187                 outb(iobase + com_dlbh, (u_int) COMBRD(SIO_TEST_SPEED) >> 8);
1188                 outb(iobase + com_cfcr, CFCR_8BITS);
1189 #endif
1190                 DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10));
1191         }
1192
1193         /*
1194          * Enable the interrupt gate and disable device interupts.  This
1195          * should leave the device driving the interrupt line low and
1196          * guarantee an edge trigger if an interrupt can be generated.
1197          */
1198 /* EXTRA DELAY? */
1199 #ifdef PC98
1200         outb(iobase + (com_mcr << port_shift), mcr_image);
1201         outb(iobase + (com_ier << port_shift), 0);
1202 #else
1203         outb(iobase + com_mcr, mcr_image);
1204         outb(iobase + com_ier, 0);
1205 #endif
1206         DELAY(1000);            /* XXX */
1207         irqmap[0] = isa_irq_pending();
1208
1209         /*
1210          * Attempt to set loopback mode so that we can send a null byte
1211          * without annoying any external device.
1212          */
1213 /* EXTRA DELAY? */
1214 #ifdef PC98
1215         outb(iobase + (com_mcr << port_shift), mcr_image | MCR_LOOPBACK);
1216 #else
1217         outb(iobase + com_mcr, mcr_image | MCR_LOOPBACK);
1218 #endif
1219
1220         /*
1221          * Attempt to generate an output interrupt.  On 8250's, setting
1222          * IER_ETXRDY generates an interrupt independent of the current
1223          * setting and independent of whether the THR is empty.  On 16450's,
1224          * setting IER_ETXRDY generates an interrupt independent of the
1225          * current setting.  On 16550A's, setting IER_ETXRDY only
1226          * generates an interrupt when IER_ETXRDY is not already set.
1227          */
1228 #ifdef PC98
1229         outb(iobase + (com_ier << port_shift), IER_ETXRDY);
1230         if (iod.if_type == COM_IF_RSA98III)
1231                 outb(rsabase + rsa_ier,   0x04);
1232 #else
1233         outb(iobase + com_ier, IER_ETXRDY);
1234 #endif /* PC98 */
1235
1236         /*
1237          * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate
1238          * an interrupt.  They'd better generate one for actually doing
1239          * output.  Loopback may be broken on the same incompatibles but
1240          * it's unlikely to do more than allow the null byte out.
1241          */
1242 #ifdef PC98
1243         outb(iobase + (com_data << port_shift), 0);
1244 #else
1245         outb(iobase + com_data, 0);
1246 #endif
1247         DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10));
1248
1249         /*
1250          * Turn off loopback mode so that the interrupt gate works again
1251          * (MCR_IENABLE was hidden).  This should leave the device driving
1252          * an interrupt line high.  It doesn't matter if the interrupt
1253          * line oscillates while we are not looking at it, since interrupts
1254          * are disabled.
1255          */
1256 /* EXTRA DELAY? */
1257 #ifdef PC98
1258         outb(iobase + (com_mcr << port_shift), mcr_image);
1259 #else
1260         outb(iobase + com_mcr, mcr_image);
1261 #endif /* PC98 */
1262
1263     /*
1264          * It's a definitly Serial PCMCIA(16550A), but still be required
1265          * for IIR_TXRDY implementation ( Palido 321s, DC-1S... )
1266          */
1267         if ( COM_NOPROBE(flags) ) {
1268                 /* Reading IIR register twice */
1269                 for ( fn = 0; fn < 2; fn ++ ) {
1270                         DELAY(10000);
1271 #ifdef PC98
1272                         failures[6] = inb(iobase + (com_iir << port_shift));
1273 #else
1274                         failures[6] = inb(iobase + com_iir);
1275 #endif
1276                 }
1277                 /* Check IIR_TXRDY clear ? */
1278 #ifdef PC98
1279                 isa_set_portsize(dev,
1280                         if_16550a_type[iod.if_type & 0x0f].io_size);
1281 #else
1282                 isa_set_portsize(dev, IO_COMSIZE);
1283 #endif
1284                 result = 0;
1285                 if ( failures[6] & IIR_TXRDY ) {
1286                         /* Nop, Double check with clearing IER */
1287 #ifdef PC98
1288                         outb(iobase + (com_ier << port_shift), 0);
1289                         if (inb(iobase +
1290                                 (com_iir << port_shift)) & IIR_NOPEND) {
1291 #else
1292                         outb(iobase + com_ier, 0);
1293                         if ( inb(iobase + com_iir) & IIR_NOPEND ) {
1294 #endif
1295                                 /* Ok. we're familia this gang */
1296                                 SET_FLAG(dev, COM_C_IIR_TXRDYBUG); /* Set IIR_TXRDYBUG */
1297                         } else {
1298                                 /* Unknow, Just omit this chip.. XXX*/
1299                                 result = ENXIO;
1300                         }
1301                 } else {
1302                         /* OK. this is well-known guys */
1303                         CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); /*Clear IIR_TXRDYBUG*/
1304                 }
1305 #ifdef PC98
1306                 outb(iobase + (com_cfcr << port_shift), CFCR_8BITS);
1307 #else
1308                 outb(iobase + com_cfcr, CFCR_8BITS);
1309 #endif
1310                 enable_intr();
1311                 return (iobase == siocniobase ? 0 : result);
1312         }
1313
1314         /*
1315          * Check that
1316          *      o the CFCR, IER and MCR in UART hold the values written to them
1317          *        (the values happen to be all distinct - this is good for
1318          *        avoiding false positive tests from bus echoes).
1319          *      o an output interrupt is generated and its vector is correct.
1320          *      o the interrupt goes away when the IIR in the UART is read.
1321          */
1322 /* EXTRA DELAY? */
1323 #ifdef PC98
1324         failures[0] = inb(iobase + (com_cfcr << port_shift)) - CFCR_8BITS;
1325         failures[1] = inb(iobase + (com_ier << port_shift)) - IER_ETXRDY;
1326         failures[2] = inb(iobase + (com_mcr << port_shift)) - mcr_image;
1327 #else
1328         failures[0] = inb(iobase + com_cfcr) - CFCR_8BITS;
1329         failures[1] = inb(iobase + com_ier) - IER_ETXRDY;
1330         failures[2] = inb(iobase + com_mcr) - mcr_image;
1331 #endif
1332         DELAY(10000);           /* Some internal modems need this time */
1333         irqmap[1] = isa_irq_pending();
1334 #ifdef PC98
1335         failures[4] = (inb(iobase + (com_iir << port_shift)) & IIR_IMASK)
1336             - IIR_TXRDY;
1337         if (iod.if_type == COM_IF_RSA98III)
1338                 inb(rsabase + rsa_srr);
1339 #else
1340         failures[4] = (inb(iobase + com_iir) & IIR_IMASK) - IIR_TXRDY;
1341 #endif
1342         DELAY(1000);            /* XXX */
1343         irqmap[2] = isa_irq_pending();
1344 #ifdef PC98
1345         failures[6] = (inb(iobase + (com_iir << port_shift)) & IIR_IMASK)
1346             - IIR_NOPEND;
1347         if (iod.if_type == COM_IF_RSA98III)
1348                 inb(rsabase + rsa_srr);
1349 #else
1350         failures[6] = (inb(iobase + com_iir) & IIR_IMASK) - IIR_NOPEND;
1351 #endif
1352
1353         /*
1354          * Turn off all device interrupts and check that they go off properly.
1355          * Leave MCR_IENABLE alone.  For ports without a master port, it gates
1356          * the OUT2 output of the UART to
1357          * the ICU input.  Closing the gate would give a floating ICU input
1358          * (unless there is another device driving at) and spurious interrupts.
1359          * (On the system that this was first tested on, the input floats high
1360          * and gives a (masked) interrupt as soon as the gate is closed.)
1361          */
1362 #ifdef PC98
1363         outb(iobase + (com_ier << port_shift), 0);
1364         outb(iobase + (com_cfcr << port_shift), CFCR_8BITS);
1365         failures[7] = inb(iobase + (com_ier << port_shift));
1366         if (iod.if_type == COM_IF_RSA98III)
1367                 outb(rsabase + rsa_ier,   0x00);
1368 #else
1369         outb(iobase + com_ier, 0);
1370         outb(iobase + com_cfcr, CFCR_8BITS);    /* dummy to avoid bus echo */
1371         failures[7] = inb(iobase + com_ier);
1372 #endif
1373         DELAY(1000);            /* XXX */
1374         irqmap[3] = isa_irq_pending();
1375 #ifdef PC98
1376         failures[9] = (inb(iobase + (com_iir << port_shift)) & IIR_IMASK)
1377             - IIR_NOPEND;
1378         if (iod.if_type == COM_IF_RSA98III) {
1379                 inb(rsabase + rsa_srr);
1380                 outb(rsabase + rsa_frr, 0x00);
1381         }
1382 #else
1383         failures[9] = (inb(iobase + com_iir) & IIR_IMASK) - IIR_NOPEND;
1384 #endif
1385
1386         enable_intr();
1387
1388         irqs = irqmap[1] & ~irqmap[0];
1389         if (isa_get_irq(idev) >= 0 && ((1 << isa_get_irq(idev)) & irqs) == 0)
1390                 printf(
1391                 "sio%d: configured irq %d not in bitmap of probed irqs %#x\n",
1392                     device_get_unit(dev), isa_get_irq(idev), irqs);
1393         if (bootverbose)
1394                 printf("sio%d: irq maps: %#x %#x %#x %#x\n",
1395                     device_get_unit(dev),
1396                     irqmap[0], irqmap[1], irqmap[2], irqmap[3]);
1397
1398 #ifdef PC98
1399         isa_set_portsize(dev, if_16550a_type[iod.if_type & 0x0f].io_size);
1400 #else
1401         isa_set_portsize(dev, IO_COMSIZE);
1402 #endif
1403         result = 0;
1404         for (fn = 0; fn < sizeof failures; ++fn)
1405                 if (failures[fn]) {
1406 #ifdef PC98
1407                         outb(iobase + (com_mcr << port_shift), 0);
1408 #else
1409                         outb(iobase + com_mcr, 0);
1410 #endif
1411                         result = ENXIO;
1412                         if (bootverbose) {
1413                                 printf("sio%d: probe failed test(s):",
1414                                     device_get_unit(dev));
1415                                 for (fn = 0; fn < sizeof failures; ++fn)
1416                                         if (failures[fn])
1417                                                 printf(" %d", fn);
1418                                 printf("\n");
1419                         }
1420                         break;
1421                 }
1422         return (iobase == siocniobase ? 0 : result);
1423 }
1424
1425 #ifdef COM_ESP
1426 static int
1427 espattach(isdp, com, esp_port)
1428         struct isa_device       *isdp;
1429         struct com_s            *com;
1430         Port_t                  esp_port;
1431 {
1432         u_char  dips;
1433         u_char  val;
1434
1435         /*
1436          * Check the ESP-specific I/O port to see if we're an ESP
1437          * card.  If not, return failure immediately.
1438          */
1439         if ((inb(esp_port) & 0xf3) == 0) {
1440                 printf(" port 0x%x is not an ESP board?\n", esp_port);
1441                 return (0);
1442         }
1443
1444         /*
1445          * We've got something that claims to be a Hayes ESP card.
1446          * Let's hope so.
1447          */
1448
1449         /* Get the dip-switch configuration */
1450 #ifdef PC98
1451         outb(esp_port + ESP98_CMD1, ESP_GETDIPS);
1452         dips = inb(esp_port + ESP98_STATUS1);
1453 #else
1454         outb(esp_port + ESP_CMD1, ESP_GETDIPS);
1455         dips = inb(esp_port + ESP_STATUS1);
1456 #endif
1457
1458         /*
1459          * Bits 0,1 of dips say which COM port we are.
1460          */
1461 #ifdef PC98
1462         if ((com->iobase & 0xff) == likely_com_ports[dips & 0x03])
1463 #else
1464         if (com->iobase == likely_com_ports[dips & 0x03])
1465 #endif
1466                 printf(" : ESP");
1467         else {
1468                 printf(" esp_port has com %d\n", dips & 0x03);
1469                 return (0);
1470         }
1471
1472         /*
1473          * Check for ESP version 2.0 or later:  bits 4,5,6 = 010.
1474          */
1475 #ifdef PC98
1476         outb(esp_port + ESP98_CMD1, ESP_GETTEST);
1477         val = inb(esp_port + ESP98_STATUS1);    /* clear reg 1 */
1478         val = inb(esp_port + ESP98_STATUS2);
1479 #else
1480         outb(esp_port + ESP_CMD1, ESP_GETTEST);
1481         val = inb(esp_port + ESP_STATUS1);      /* clear reg 1 */
1482         val = inb(esp_port + ESP_STATUS2);
1483 #endif
1484         if ((val & 0x70) < 0x20) {
1485                 printf("-old (%o)", val & 0x70);
1486                 return (0);
1487         }
1488
1489         /*
1490          * Check for ability to emulate 16550:  bit 7 == 1
1491          */
1492         if ((dips & 0x80) == 0) {
1493                 printf(" slave");
1494                 return (0);
1495         }
1496
1497         /*
1498          * Okay, we seem to be a Hayes ESP card.  Whee.
1499          */
1500         com->esp = TRUE;
1501         com->esp_port = esp_port;
1502         return (1);
1503 }
1504 #endif /* COM_ESP */
1505
1506 static int
1507 sioattach(dev)
1508         device_t        dev;
1509 {
1510         struct com_s    *com;
1511 #ifdef COM_ESP
1512         Port_t          *espp;
1513 #endif
1514         Port_t          iobase;
1515         int             s;
1516         int             unit;
1517         void            *ih;
1518         struct resource *res;
1519         int             zero = 0;
1520         u_int           flags = isa_get_flags(dev);
1521 #ifdef PC98
1522         int             port_shift = 0;
1523         u_char          *obuf;
1524         u_long          obufsize;
1525 #endif
1526
1527 #if 0
1528         isdp->id_ri_flags |= RI_FAST;
1529 #endif
1530         iobase = isa_get_port(dev);
1531 #ifdef PC98
1532         if (((flags >> 24) & 0xff) == COM_IF_RSA98III)
1533                 iobase += 8;
1534 #endif
1535         unit = device_get_unit(dev);
1536         com = device_get_softc(dev);
1537 #ifdef PC98
1538         obufsize = 256;
1539         if (((flags >> 24) & 0xff) == COM_IF_RSA98III)
1540                 obufsize = 2048;
1541         if ((obuf = malloc(obufsize * 2, M_DEVBUF, M_NOWAIT)) == NULL)
1542                 return (0);
1543         bzero(obuf, obufsize * 2);
1544 #endif
1545
1546         /*
1547          * sioprobe() has initialized the device registers as follows:
1548          *      o cfcr = CFCR_8BITS.
1549          *        It is most important that CFCR_DLAB is off, so that the
1550          *        data port is not hidden when we enable interrupts.
1551          *      o ier = 0.
1552          *        Interrupts are only enabled when the line is open.
1553          *      o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible
1554          *        interrupt control register or the config specifies no irq.
1555          *        Keeping MCR_DTR and MCR_RTS off might stop the external
1556          *        device from sending before we are ready.
1557          */
1558         bzero(com, sizeof *com);
1559 #ifdef PC98
1560         com->obufsize = obufsize;
1561         com->obuf1 = obuf;
1562         com->obuf2 = obuf + obufsize;
1563 #endif
1564         com->unit = unit;
1565         com->cfcr_image = CFCR_8BITS;
1566         com->dtr_wait = 3 * hz;
1567         com->loses_outints = COM_LOSESOUTINTS(flags) != 0;
1568         com->no_irq = isa_get_irq(dev) < 0;
1569         com->tx_fifo_size = 1;
1570         com->obufs[0].l_head = com->obuf1;
1571         com->obufs[1].l_head = com->obuf2;
1572
1573         com->iobase = iobase;
1574 #ifdef PC98
1575         if (pc98_set_ioport(com, isa_get_flags(dev)) == -1) {
1576             com->pc98_if_type = (isa_get_flags(dev) >> 24) & 0xff;
1577             port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
1578             com->data_port = iobase + (com_data << port_shift);
1579             com->int_id_port = iobase + (com_iir << port_shift);
1580             com->modem_ctl_port = iobase + (com_mcr << port_shift);
1581             com->mcr_image = inb(com->modem_ctl_port);
1582             com->line_status_port = iobase + (com_lsr << port_shift);
1583             com->modem_status_port = iobase + (com_msr << port_shift);
1584             com->intr_ctl_port = iobase + (com_ier << port_shift);
1585         }
1586 #else /* not PC98 */
1587         com->data_port = iobase + com_data;
1588         com->int_id_port = iobase + com_iir;
1589         com->modem_ctl_port = iobase + com_mcr;
1590         com->mcr_image = inb(com->modem_ctl_port);
1591         com->line_status_port = iobase + com_lsr;
1592         com->modem_status_port = iobase + com_msr;
1593         com->intr_ctl_port = iobase + com_ier;
1594 #endif
1595
1596         /*
1597          * We don't use all the flags from <sys/ttydefaults.h> since they
1598          * are only relevant for logins.  It's important to have echo off
1599          * initially so that the line doesn't start blathering before the
1600          * echo flag can be turned off.
1601          */
1602         com->it_in.c_iflag = 0;
1603         com->it_in.c_oflag = 0;
1604         com->it_in.c_cflag = TTYDEF_CFLAG;
1605         com->it_in.c_lflag = 0;
1606         if (unit == comconsole) {
1607 #ifdef PC98
1608                 if (IS_8251(com->pc98_if_type))
1609                         DELAY(100000);
1610 #endif
1611                 com->it_in.c_iflag = TTYDEF_IFLAG;
1612                 com->it_in.c_oflag = TTYDEF_OFLAG;
1613                 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
1614                 com->it_in.c_lflag = TTYDEF_LFLAG;
1615                 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
1616                 com->lt_out.c_ispeed = com->lt_out.c_ospeed =
1617                 com->lt_in.c_ispeed = com->lt_in.c_ospeed =
1618                 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
1619         } else
1620                 com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED;
1621         if (siosetwater(com, com->it_in.c_ispeed) != 0) {
1622                 enable_intr();
1623                 free(com, M_DEVBUF);
1624                 return (0);
1625         }
1626         enable_intr();
1627         termioschars(&com->it_in);
1628         com->it_out = com->it_in;
1629
1630         /* attempt to determine UART type */
1631         printf("sio%d: type", unit);
1632
1633
1634 #ifndef PC98
1635 #ifdef COM_MULTIPORT
1636         if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags))
1637 #else
1638         if (!COM_IIR_TXRDYBUG(flags))
1639 #endif
1640         {
1641                 u_char  scr;
1642                 u_char  scr1;
1643                 u_char  scr2;
1644
1645                 scr = inb(iobase + com_scr);
1646                 outb(iobase + com_scr, 0xa5);
1647                 scr1 = inb(iobase + com_scr);
1648                 outb(iobase + com_scr, 0x5a);
1649                 scr2 = inb(iobase + com_scr);
1650                 outb(iobase + com_scr, scr);
1651                 if (scr1 != 0xa5 || scr2 != 0x5a) {
1652                         printf(" 8250");
1653                         goto determined_type;
1654                 }
1655         }
1656 #endif /* !PC98 */
1657 #ifdef PC98
1658         if (IS_8251(com->pc98_if_type)) {
1659             com_int_TxRx_disable( com );
1660             com_cflag_and_speed_set( com, com->it_in.c_cflag, comdefaultrate );
1661             com_tiocm_bic( com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE );
1662             com_send_break_off( com );
1663             printf(" 8251%s", if_8251_type[com->pc98_if_type & 0x0f].name);
1664         } else {
1665         outb(iobase + (com_fifo << port_shift), FIFO_ENABLE | FIFO_RX_HIGH);
1666 #else
1667         outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RX_HIGH);
1668 #endif /* PC98 */
1669         DELAY(100);
1670         com->st16650a = 0;
1671         switch (inb(com->int_id_port) & IIR_FIFO_MASK) {
1672         case FIFO_RX_LOW:
1673                 printf(" 16450");
1674                 break;
1675         case FIFO_RX_MEDL:
1676                 printf(" 16450?");
1677                 break;
1678         case FIFO_RX_MEDH:
1679                 printf(" 16550?");
1680                 break;
1681         case FIFO_RX_HIGH:
1682                 if (COM_NOFIFO(flags)) {
1683                         printf(" 16550A fifo disabled");
1684                 } else {
1685                         com->hasfifo = TRUE;
1686 #ifdef PC98
1687                         com->tx_fifo_size = 0;  /* XXX flag conflicts. */
1688                         printf(" 16550A");
1689 #else
1690                         if (COM_ST16650A(flags)) {
1691                                 com->st16650a = 1;
1692                                 com->tx_fifo_size = 32;
1693                                 printf(" ST16650A");
1694                         } else {
1695                                 com->tx_fifo_size = COM_FIFOSIZE(flags);
1696                                 printf(" 16550A");
1697                         }
1698 #endif
1699                 }
1700 #ifdef PC98
1701                 if (com->pc98_if_type == COM_IF_RSA98III) {
1702                         com->tx_fifo_size = 2048;
1703                         com->rsabase = isa_get_port(dev);
1704                         outb(com->rsabase + rsa_ier, 0x00);
1705                         outb(com->rsabase + rsa_frr, 0x00);
1706                 }
1707 #endif
1708
1709 #ifdef COM_ESP
1710 #ifdef PC98
1711                 if (com->pc98_if_type == COM_IF_ESP98)
1712 #endif
1713                 for (espp = likely_esp_ports; *espp != 0; espp++)
1714                         if (espattach(dev, com, *espp)) {
1715                                 com->tx_fifo_size = 1024;
1716                                 break;
1717                         }
1718 #endif
1719                 if (!com->st16650a) {
1720                         if (!com->tx_fifo_size)
1721                                 com->tx_fifo_size = 16;
1722                         else
1723                                 printf(" lookalike with %d bytes FIFO",
1724                                     com->tx_fifo_size);
1725                 }
1726
1727                 break;
1728         }
1729         
1730 #ifdef PC98
1731         if (com->pc98_if_type == COM_IF_RSB3000) {
1732             /* Set RSB-2000/3000 Extended Buffer mode. */
1733             u_char lcr;
1734             lcr = inb(iobase + (com_cfcr << port_shift));
1735             outb(iobase + (com_cfcr << port_shift), lcr | CFCR_DLAB);
1736             outb(iobase + (com_emr << port_shift), EMR_EXBUFF | EMR_EFMODE);
1737             outb(iobase + (com_cfcr << port_shift), lcr);
1738         }
1739 #endif
1740
1741 #ifdef COM_ESP
1742         if (com->esp) {
1743                 /*
1744                  * Set 16550 compatibility mode.
1745                  * We don't use the ESP_MODE_SCALE bit to increase the
1746                  * fifo trigger levels because we can't handle large
1747                  * bursts of input.
1748                  * XXX flow control should be set in comparam(), not here.
1749                  */
1750 #ifdef PC98
1751                 outb(com->esp_port + ESP98_CMD1, ESP_SETMODE);
1752                 outb(com->esp_port + ESP98_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO);
1753 #else
1754                 outb(com->esp_port + ESP_CMD1, ESP_SETMODE);
1755                 outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO);
1756 #endif
1757
1758                 /* Set RTS/CTS flow control. */
1759 #ifdef PC98
1760                 outb(com->esp_port + ESP98_CMD1, ESP_SETFLOWTYPE);
1761                 outb(com->esp_port + ESP98_CMD2, ESP_FLOW_RTS);
1762                 outb(com->esp_port + ESP98_CMD2, ESP_FLOW_CTS);
1763 #else
1764                 outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE);
1765                 outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS);
1766                 outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS);
1767 #endif
1768
1769                 /* Set flow-control levels. */
1770 #ifdef PC98
1771                 outb(com->esp_port + ESP98_CMD1, ESP_SETRXFLOW);
1772                 outb(com->esp_port + ESP98_CMD2, HIBYTE(768));
1773                 outb(com->esp_port + ESP98_CMD2, LOBYTE(768));
1774                 outb(com->esp_port + ESP98_CMD2, HIBYTE(512));
1775                 outb(com->esp_port + ESP98_CMD2, LOBYTE(512));
1776 #else
1777                 outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW);
1778                 outb(com->esp_port + ESP_CMD2, HIBYTE(768));
1779                 outb(com->esp_port + ESP_CMD2, LOBYTE(768));
1780                 outb(com->esp_port + ESP_CMD2, HIBYTE(512));
1781                 outb(com->esp_port + ESP_CMD2, LOBYTE(512));
1782 #endif
1783
1784 #ifdef PC98
1785                 /* Set UART clock prescaler. */
1786                 outb(com->esp_port + ESP98_CMD1, ESP_SETCLOCK);
1787                 outb(com->esp_port + ESP98_CMD2, 2);    /* 4 times */
1788 #endif
1789         }
1790 #endif /* COM_ESP */
1791 #ifdef PC98
1792         printf("%s", if_16550a_type[com->pc98_if_type & 0x0f].name);
1793         outb(iobase + (com_fifo << port_shift), 0);
1794 #else
1795         outb(iobase + com_fifo, 0);
1796 determined_type: ;
1797 #endif
1798
1799 #ifdef COM_MULTIPORT
1800         if (COM_ISMULTIPORT(flags)) {
1801                 com->multiport = TRUE;
1802                 printf(" (multiport");
1803                 if (unit == COM_MPMASTER(flags))
1804                         printf(" master");
1805                 printf(")");
1806                 com->no_irq =
1807                         isa_get_irq(devclass_get_device
1808                                     (sio_devclass, COM_MPMASTER(flags))) < 0;
1809          }
1810 #endif /* COM_MULTIPORT */
1811 #ifdef PC98
1812         }
1813 #endif
1814         if (unit == comconsole)
1815                 printf(", console");
1816         if ( COM_IIR_TXRDYBUG(flags) )
1817                 printf(" with a bogus IIR_TXRDY register");
1818         printf("\n");
1819
1820         if (!sio_registered) {
1821                 register_swi(SWI_TTY, siopoll);
1822                 sio_registered = TRUE;
1823         }
1824 #ifdef DEVFS
1825         com->devfs_token_ttyd = devfs_add_devswf(&sio_cdevsw,
1826                 unit, DV_CHR,
1827                 UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit);
1828         com->devfs_token_ttyi = devfs_add_devswf(&sio_cdevsw,
1829                 unit | CONTROL_INIT_STATE, DV_CHR,
1830                 UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit);
1831         com->devfs_token_ttyl = devfs_add_devswf(&sio_cdevsw,
1832                 unit | CONTROL_LOCK_STATE, DV_CHR,
1833                 UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit);
1834         com->devfs_token_cuaa = devfs_add_devswf(&sio_cdevsw,
1835                 unit | CALLOUT_MASK, DV_CHR,
1836                 UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit);
1837         com->devfs_token_cuai = devfs_add_devswf(&sio_cdevsw,
1838                 unit | CALLOUT_MASK | CONTROL_INIT_STATE, DV_CHR,
1839                 UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit);
1840         com->devfs_token_cual = devfs_add_devswf(&sio_cdevsw,
1841                 unit | CALLOUT_MASK | CONTROL_LOCK_STATE, DV_CHR,
1842                 UID_UUCP, GID_DIALER, 0660, "cuala%r", unit);
1843 #endif
1844         com->flags = isa_get_flags(dev); /* Heritate id_flags for later */
1845         com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
1846         pps_init(&com->pps);
1847
1848         res = bus_alloc_resource(dev, SYS_RES_IRQ, &zero, 0ul, ~0ul, 1,
1849                                  RF_SHAREABLE | RF_ACTIVE);
1850         BUS_SETUP_INTR(device_get_parent(dev), dev, res, siointr, com,
1851                        &ih);
1852
1853         return (0);
1854 }
1855
1856 static int
1857 sioopen(dev, flag, mode, p)
1858         dev_t           dev;
1859         int             flag;
1860         int             mode;
1861         struct proc     *p;
1862 {
1863         struct com_s    *com;
1864         int             error;
1865         Port_t          iobase;
1866         int             mynor;
1867         int             s;
1868         struct tty      *tp;
1869         int             unit;
1870 #ifdef PC98
1871         int             port_shift = 0;
1872 #endif
1873
1874         mynor = minor(dev);
1875         unit = MINOR_TO_UNIT(mynor);
1876         if ((u_int) unit >= NSIOTOT || (com = com_addr(unit)) == NULL)
1877                 return (ENXIO);
1878         if (com->gone)
1879                 return (ENXIO);
1880         if (mynor & CONTROL_MASK)
1881                 return (0);
1882 #if 0 /* XXX */
1883         tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
1884 #else
1885         tp = com->tp = &sio_tty[unit];
1886 #endif
1887         s = spltty();
1888
1889 #ifdef PC98
1890         if (!IS_8251(com->pc98_if_type))
1891             port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
1892 #endif
1893         /*
1894          * We jump to this label after all non-interrupted sleeps to pick
1895          * up any changes of the device state.
1896          */
1897 open_top:
1898         while (com->state & CS_DTR_OFF) {
1899                 error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "siodtr", 0);
1900                 if (com_addr(unit) == NULL)
1901                         return (ENXIO);
1902                 if (error != 0 || com->gone)
1903                         goto out;
1904         }
1905         if (tp->t_state & TS_ISOPEN) {
1906                 /*
1907                  * The device is open, so everything has been initialized.
1908                  * Handle conflicts.
1909                  */
1910                 if (mynor & CALLOUT_MASK) {
1911                         if (!com->active_out) {
1912                                 error = EBUSY;
1913                                 goto out;
1914                         }
1915                 } else {
1916                         if (com->active_out) {
1917                                 if (flag & O_NONBLOCK) {
1918                                         error = EBUSY;
1919                                         goto out;
1920                                 }
1921                                 error = tsleep(&com->active_out,
1922                                                TTIPRI | PCATCH, "siobi", 0);
1923                                 if (com_addr(unit) == NULL)
1924                                         return (ENXIO);
1925                                 if (error != 0 || com->gone)
1926                                         goto out;
1927                                 goto open_top;
1928                         }
1929                 }
1930                 if (tp->t_state & TS_XCLUDE &&
1931                     suser(p->p_ucred, &p->p_acflag)) {
1932                         error = EBUSY;
1933                         goto out;
1934                 }
1935         } else {
1936                 /*
1937                  * The device isn't open, so there are no conflicts.
1938                  * Initialize it.  Initialization is done twice in many
1939                  * cases: to preempt sleeping callin opens if we are
1940                  * callout, and to complete a callin open after DCD rises.
1941                  */
1942                 tp->t_oproc = comstart;
1943                 tp->t_param = comparam;
1944                 tp->t_dev = dev;
1945                 tp->t_termios = mynor & CALLOUT_MASK
1946                                 ? com->it_out : com->it_in;
1947 #ifdef PC98
1948                 if (!IS_8251(com->pc98_if_type))
1949 #endif
1950                 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
1951                 com->poll = com->no_irq;
1952                 com->poll_output = com->loses_outints;
1953                 ++com->wopeners;
1954                 error = comparam(tp, &tp->t_termios);
1955                 --com->wopeners;
1956                 if (error != 0)
1957                         goto out;
1958 #ifdef PC98
1959                 if (IS_8251(com->pc98_if_type)) {
1960                         com_tiocm_bis(com, TIOCM_DTR|TIOCM_RTS);
1961                         pc98_msrint_start(dev);
1962                 }
1963 #endif
1964                 /*
1965                  * XXX we should goto open_top if comparam() slept.
1966                  */
1967                 iobase = com->iobase;
1968                 if (com->hasfifo) {
1969                         /*
1970                          * (Re)enable and drain fifos.
1971                          *
1972                          * Certain SMC chips cause problems if the fifos
1973                          * are enabled while input is ready.  Turn off the
1974                          * fifo if necessary to clear the input.  We test
1975                          * the input ready bit after enabling the fifos
1976                          * since we've already enabled them in comparam()
1977                          * and to handle races between enabling and fresh
1978                          * input.
1979                          */
1980                         while (TRUE) {
1981 #ifdef PC98
1982                                 outb(iobase + (com_fifo << port_shift),
1983                                      FIFO_RCV_RST | FIFO_XMT_RST
1984                                      | com->fifo_image);
1985                                 if (com->pc98_if_type == COM_IF_RSA98III)
1986                                   outb(com->rsabase + rsa_frr , 0x00);
1987 #else
1988                                 outb(iobase + com_fifo,
1989                                      FIFO_RCV_RST | FIFO_XMT_RST
1990                                      | com->fifo_image);
1991 #endif
1992                                 /*
1993                                  * XXX the delays are for superstitious
1994                                  * historical reasons.  It must be less than
1995                                  * the character time at the maximum
1996                                  * supported speed (87 usec at 115200 bps
1997                                  * 8N1).  Otherwise we might loop endlessly
1998                                  * if data is streaming in.  We used to use
1999                                  * delays of 100.  That usually worked
2000                                  * because DELAY(100) used to usually delay
2001                                  * for about 85 usec instead of 100.
2002                                  */
2003                                 DELAY(50);
2004 #ifndef PC98
2005                                 if (!(inb(com->line_status_port) & LSR_RXRDY))
2006 #else
2007                                 if (com->pc98_if_type == COM_IF_RSA98III
2008                                     ? !(inb(com->rsabase + rsa_srr) & 0x08)
2009                                     : !(inb(com->line_status_port) & LSR_RXRDY))
2010 #endif
2011                                         break;
2012 #ifdef PC98
2013                                 outb(iobase + (com_fifo << port_shift), 0);
2014 #else
2015                                 outb(iobase + com_fifo, 0);
2016 #endif
2017                                 DELAY(50);
2018                                 (void) inb(com->data_port);
2019                         }
2020                 }
2021
2022                 disable_intr();
2023 #ifdef PC98
2024                 if (IS_8251(com->pc98_if_type)) {
2025                     com_tiocm_bis(com, TIOCM_LE);
2026                     com->pc98_prev_modem_status = pc98_get_modem_status(com);
2027                     com_int_Rx_enable(com);
2028                 } else {
2029 #endif
2030                 (void) inb(com->line_status_port);
2031                 (void) inb(com->data_port);
2032                 com->prev_modem_status = com->last_modem_status
2033                     = inb(com->modem_status_port);
2034                 if (COM_IIR_TXRDYBUG(com->flags)) {
2035                         outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS
2036                                                 | IER_EMSC);
2037                 } else {
2038                         outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY
2039                                                 | IER_ERLS | IER_EMSC);
2040                 }
2041 #ifdef PC98
2042                 if (com->pc98_if_type == COM_IF_RSA98III) {
2043                         outb(com->rsabase + rsa_ier, 0x1d);
2044                         outb(com->intr_ctl_port, IER_ERLS | IER_EMSC);
2045                 }
2046 #endif
2047 #ifdef PC98
2048                 }
2049 #endif
2050                 enable_intr();
2051                 /*
2052                  * Handle initial DCD.  Callout devices get a fake initial
2053                  * DCD (trapdoor DCD).  If we are callout, then any sleeping
2054                  * callin opens get woken up and resume sleeping on "siobi"
2055                  * instead of "siodcd".
2056                  */
2057                 /*
2058                  * XXX `mynor & CALLOUT_MASK' should be
2059                  * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
2060                  * TRAPDOOR_CARRIER is the default initial state for callout
2061                  * devices and SOFT_CARRIER is like CLOCAL except it hides
2062                  * the true carrier.
2063                  */
2064 #ifdef PC98
2065                 if ((IS_8251(com->pc98_if_type) &&
2066                         (pc98_get_modem_status(com) & TIOCM_CAR)) ||
2067                     (!IS_8251(com->pc98_if_type) &&
2068                         (com->prev_modem_status & MSR_DCD)) ||
2069                     mynor & CALLOUT_MASK)
2070 #else
2071                 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
2072 #endif
2073                         (*linesw[tp->t_line].l_modem)(tp, 1);
2074         }
2075         /*
2076          * Wait for DCD if necessary.
2077          */
2078         if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
2079             && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
2080                 ++com->wopeners;
2081                 error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "siodcd", 0);
2082                 if (com_addr(unit) == NULL)
2083                         return (ENXIO);
2084                 --com->wopeners;
2085                 if (error != 0 || com->gone)
2086                         goto out;
2087                 goto open_top;
2088         }
2089         error = (*linesw[tp->t_line].l_open)(dev, tp);
2090         disc_optim(tp, &tp->t_termios, com);
2091         if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
2092                 com->active_out = TRUE;
2093         siosettimeout();
2094 out:
2095         splx(s);
2096         if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
2097                 comhardclose(com);
2098         return (error);
2099 }
2100
2101 static int
2102 sioclose(dev, flag, mode, p)
2103         dev_t           dev;
2104         int             flag;
2105         int             mode;
2106         struct proc     *p;
2107 {
2108         struct com_s    *com;
2109         int             mynor;
2110         int             s;
2111         struct tty      *tp;
2112
2113         mynor = minor(dev);
2114         if (mynor & CONTROL_MASK)
2115                 return (0);
2116         com = com_addr(MINOR_TO_UNIT(mynor));
2117         tp = com->tp;
2118         s = spltty();
2119         (*linesw[tp->t_line].l_close)(tp, flag);
2120 #ifdef PC98
2121         com->modem_checking = 0;
2122 #endif
2123         disc_optim(tp, &tp->t_termios, com);
2124         siostop(tp, FREAD | FWRITE);
2125         comhardclose(com);
2126         ttyclose(tp);
2127         siosettimeout();
2128         splx(s);
2129         if (com->gone) {
2130                 printf("sio%d: gone\n", com->unit);
2131                 s = spltty();
2132                 if (com->ibuf != NULL)
2133                         free(com->ibuf, M_DEVBUF);
2134                 bzero(tp,sizeof *tp);
2135                 free(com, M_DEVBUF);
2136                 splx(s);
2137         }
2138         return (0);
2139 }
2140
2141 static void
2142 comhardclose(com)
2143         struct com_s    *com;
2144 {
2145         Port_t          iobase;
2146         int             s;
2147         struct tty      *tp;
2148         int             unit;
2149 #ifdef PC98
2150         int             port_shift = 0;
2151 #endif
2152
2153         unit = com->unit;
2154         iobase = com->iobase;
2155         s = spltty();
2156         com->poll = FALSE;
2157         com->poll_output = FALSE;
2158         com->do_timestamp = FALSE;
2159         com->do_dcd_timestamp = FALSE;
2160         com->pps.ppsparam.mode = 0;
2161 #ifdef PC98
2162         if (IS_8251(com->pc98_if_type))
2163             com_send_break_off(com);
2164         else {
2165             port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
2166             outb(iobase + (com_cfcr << port_shift),
2167                  com->cfcr_image &= ~CFCR_SBREAK);
2168         }
2169 #else
2170         outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
2171 #endif
2172         {
2173 #ifdef PC98
2174                 int tmp;
2175                 if (IS_8251(com->pc98_if_type))
2176                         com_int_TxRx_disable(com);
2177                 else
2178                         outb(iobase + (com_ier << port_shift), 0);
2179                 if (com->pc98_if_type == COM_IF_RSA98III) {
2180                         outb(com->rsabase + rsa_ier, 0x00);
2181                 }
2182 #else
2183                 outb(iobase + com_ier, 0);
2184 #endif
2185                 tp = com->tp;
2186 #ifdef PC98
2187                 if (IS_8251(com->pc98_if_type))
2188                         tmp = pc98_get_modem_status(com) & TIOCM_CAR;
2189                 else
2190                         tmp = com->prev_modem_status & MSR_DCD;
2191 #endif
2192                 if (tp->t_cflag & HUPCL
2193                     /*
2194                      * XXX we will miss any carrier drop between here and the
2195                      * next open.  Perhaps we should watch DCD even when the
2196                      * port is closed; it is not sufficient to check it at
2197                      * the next open because it might go up and down while
2198                      * we're not watching.
2199                      */
2200                     || !com->active_out
2201 #ifdef PC98
2202                        && !(tmp)
2203 #else
2204                        && !(com->prev_modem_status & MSR_DCD)
2205 #endif
2206                        && !(com->it_in.c_cflag & CLOCAL)
2207                     || !(tp->t_state & TS_ISOPEN)) {
2208 #ifdef PC98
2209                         if (IS_8251(com->pc98_if_type))
2210                             com_tiocm_bic(com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE);
2211                         else
2212 #endif
2213                         (void)commctl(com, TIOCM_DTR, DMBIC);
2214                         if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
2215                                 timeout(siodtrwakeup, com, com->dtr_wait);
2216                                 com->state |= CS_DTR_OFF;
2217                         }
2218                 }
2219 #ifdef PC98
2220                 else {
2221                         if (IS_8251(com->pc98_if_type))
2222                                 com_tiocm_bic(com, TIOCM_LE );
2223                 }
2224 #endif
2225         }
2226         if (com->hasfifo) {
2227                 /*
2228                  * Disable fifos so that they are off after controlled
2229                  * reboots.  Some BIOSes fail to detect 16550s when the
2230                  * fifos are enabled.
2231                  */
2232 #ifdef PC98
2233                 outb(iobase + (com_fifo << port_shift), 0);
2234 #else
2235                 outb(iobase + com_fifo, 0);
2236 #endif
2237         }
2238         com->active_out = FALSE;
2239         wakeup(&com->active_out);
2240         wakeup(TSA_CARR_ON(tp));        /* restart any wopeners */
2241         splx(s);
2242 }
2243
2244 static int
2245 sioread(dev, uio, flag)
2246         dev_t           dev;
2247         struct uio      *uio;
2248         int             flag;
2249 {
2250         int             mynor;
2251         int             unit;
2252         struct tty      *tp;
2253
2254         mynor = minor(dev);
2255         if (mynor & CONTROL_MASK)
2256                 return (ENODEV);
2257         unit = MINOR_TO_UNIT(mynor);
2258         if (com_addr(unit)->gone)
2259                 return (ENODEV);
2260         tp = com_addr(unit)->tp;
2261         return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
2262 }
2263
2264 static int
2265 siowrite(dev, uio, flag)
2266         dev_t           dev;
2267         struct uio      *uio;
2268         int             flag;
2269 {
2270         int             mynor;
2271         struct tty      *tp;
2272         int             unit;
2273
2274         mynor = minor(dev);
2275         if (mynor & CONTROL_MASK)
2276                 return (ENODEV);
2277
2278         unit = MINOR_TO_UNIT(mynor);
2279         if (com_addr(unit)->gone)
2280                 return (ENODEV);
2281         tp = com_addr(unit)->tp;
2282         /*
2283          * (XXX) We disallow virtual consoles if the physical console is
2284          * a serial port.  This is in case there is a display attached that
2285          * is not the console.  In that situation we don't need/want the X
2286          * server taking over the console.
2287          */
2288         if (constty != NULL && unit == comconsole)
2289                 constty = NULL;
2290         return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2291 }
2292
2293 static void
2294 siobusycheck(chan)
2295         void    *chan;
2296 {
2297         struct com_s    *com;
2298         int             s;
2299
2300         com = (struct com_s *)chan;
2301
2302         /*
2303          * Clear TS_BUSY if low-level output is complete.
2304          * spl locking is sufficient because siointr1() does not set CS_BUSY.
2305          * If siointr1() clears CS_BUSY after we look at it, then we'll get
2306          * called again.  Reading the line status port outside of siointr1()
2307          * is safe because CS_BUSY is clear so there are no output interrupts
2308          * to lose.
2309          */
2310         s = spltty();
2311         if (com->state & CS_BUSY)
2312                 com->extra_state &= ~CSE_BUSYCHECK;     /* False alarm. */
2313 #ifdef  PC98
2314         else if ((IS_8251(com->pc98_if_type) &&
2315                  (inb(com->sts_port) & (STS8251_TxRDY | STS8251_TxEMP))
2316                  == (STS8251_TxRDY | STS8251_TxEMP)) ||
2317                  (inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY))
2318                  == (LSR_TSRE | LSR_TXRDY)) {
2319 #else
2320         else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY))
2321             == (LSR_TSRE | LSR_TXRDY)) {
2322 #endif
2323                 com->tp->t_state &= ~TS_BUSY;
2324                 ttwwakeup(com->tp);
2325                 com->extra_state &= ~CSE_BUSYCHECK;
2326         } else
2327                 timeout(siobusycheck, com, hz / 100);
2328         splx(s);
2329 }
2330
2331 static void
2332 siodtrwakeup(chan)
2333         void    *chan;
2334 {
2335         struct com_s    *com;
2336
2337         com = (struct com_s *)chan;
2338         com->state &= ~CS_DTR_OFF;
2339         wakeup(&com->dtr_wait);
2340 }
2341
2342 static void
2343 sioinput(com)
2344         struct com_s    *com;
2345 {
2346         u_char          *buf;
2347         int             incc;
2348         u_char          line_status;
2349         int             recv_data;
2350         struct tty      *tp;
2351 #ifdef PC98
2352         u_char          tmp;
2353 #endif
2354
2355         buf = com->ibuf;
2356         tp = com->tp;
2357         if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) {
2358                 com_events -= (com->iptr - com->ibuf);
2359                 com->iptr = com->ibuf;
2360                 return;
2361         }
2362         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
2363                 /*
2364                  * Avoid the grotesquely inefficient lineswitch routine
2365                  * (ttyinput) in "raw" mode.  It usually takes about 450
2366                  * instructions (that's without canonical processing or echo!).
2367                  * slinput is reasonably fast (usually 40 instructions plus
2368                  * call overhead).
2369                  */
2370                 do {
2371                         enable_intr();
2372                         incc = com->iptr - buf;
2373                         if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
2374                             && (com->state & CS_RTS_IFLOW
2375                                 || tp->t_iflag & IXOFF)
2376                             && !(tp->t_state & TS_TBLOCK))
2377                                 ttyblock(tp);
2378                         com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
2379                                 += b_to_q((char *)buf, incc, &tp->t_rawq);
2380                         buf += incc;
2381                         tk_nin += incc;
2382                         tk_rawcc += incc;
2383                         tp->t_rawcc += incc;
2384                         ttwakeup(tp);
2385                         if (tp->t_state & TS_TTSTOP
2386                             && (tp->t_iflag & IXANY
2387                                 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
2388                                 tp->t_state &= ~TS_TTSTOP;
2389                                 tp->t_lflag &= ~FLUSHO;
2390                                 comstart(tp);
2391                         }
2392                         disable_intr();
2393                 } while (buf < com->iptr);
2394         } else {
2395                 do {
2396                         enable_intr();
2397                         line_status = buf[com->ierroff];
2398                         recv_data = *buf++;
2399                         if (line_status
2400                             & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
2401                                 if (line_status & LSR_BI)
2402                                         recv_data |= TTY_BI;
2403                                 if (line_status & LSR_FE)
2404                                         recv_data |= TTY_FE;
2405                                 if (line_status & LSR_OE)
2406                                         recv_data |= TTY_OE;
2407                                 if (line_status & LSR_PE)
2408                                         recv_data |= TTY_PE;
2409                         }
2410                         (*linesw[tp->t_line].l_rint)(recv_data, tp);
2411                         disable_intr();
2412                 } while (buf < com->iptr);
2413         }
2414         com_events -= (com->iptr - com->ibuf);
2415         com->iptr = com->ibuf;
2416
2417         /*
2418          * There is now room for another low-level buffer full of input,
2419          * so enable RTS if it is now disabled and there is room in the
2420          * high-level buffer.
2421          */
2422 #ifdef PC98
2423         if (IS_8251(com->pc98_if_type))
2424                 tmp = com_tiocm_get(com) & TIOCM_RTS;
2425         else
2426                 tmp = com->mcr_image & MCR_RTS;
2427         if ((com->state & CS_RTS_IFLOW) && !(tmp) &&
2428             !(tp->t_state & TS_TBLOCK))
2429                 if (IS_8251(com->pc98_if_type))
2430                         com_tiocm_bis(com, TIOCM_RTS);
2431                 else
2432                         outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2433 #else
2434         if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) &&
2435             !(tp->t_state & TS_TBLOCK))
2436                 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2437 #endif
2438 }
2439
2440 void
2441 siointr(arg)
2442         void            *arg;
2443 {
2444 #ifndef COM_MULTIPORT
2445         COM_LOCK();
2446         siointr1((struct com_s *) arg);
2447         COM_UNLOCK();
2448 #else /* COM_MULTIPORT */
2449         bool_t          possibly_more_intrs;
2450 #ifdef PC98
2451         u_char          rsa_buf_status;
2452 #endif
2453
2454         /*
2455          * Loop until there is no activity on any port.  This is necessary
2456          * to get an interrupt edge more than to avoid another interrupt.
2457          * If the IRQ signal is just an OR of the IRQ signals from several
2458          * devices, then the edge from one may be lost because another is
2459          * on.
2460          */
2461         COM_LOCK();
2462         do {
2463                 possibly_more_intrs = FALSE;
2464                 for (unit = 0; unit < NSIOTOT; ++unit) {
2465                         com = com_addr(unit);
2466                         /*
2467                          * XXX COM_LOCK();
2468                          * would it work here, or be counter-productive?
2469                          */
2470 #ifdef PC98
2471                         if (com != NULL 
2472                             && !com->gone
2473                             && IS_8251(com->pc98_if_type)){
2474                                 siointr1(com);
2475                         } else
2476 #endif /* PC98 */
2477 #ifdef PC98
2478                         if (com != NULL 
2479                             && !com->gone
2480                             && com->pc98_if_type == COM_IF_RSA98III) {
2481                           rsa_buf_status = inb(com->rsabase + rsa_srr) & 0xc9;
2482                           if ((rsa_buf_status & 0xc8)
2483                               || !(rsa_buf_status & 0x01)) {
2484                             siointr1(com);
2485                             if(rsa_buf_status
2486                                != (inb(com->rsabase + rsa_srr) & 0xc9))
2487                               possibly_more_intrs = TRUE;
2488                           }
2489                         } else
2490 #endif
2491                         if (com != NULL 
2492                             && !com->gone
2493                             && (inb(com->int_id_port) & IIR_IMASK)
2494                                != IIR_NOPEND) {
2495                                 siointr1(com);
2496                                 possibly_more_intrs = TRUE;
2497                         }
2498                         /* XXX COM_UNLOCK(); */
2499                 }
2500         } while (possibly_more_intrs);
2501         COM_UNLOCK();
2502 #endif /* COM_MULTIPORT */
2503 }
2504
2505 static void
2506 siointr1(com)
2507         struct com_s    *com;
2508 {
2509         u_char  line_status;
2510         u_char  modem_status;
2511         u_char  *ioptr;
2512         u_char  recv_data;
2513         u_char  int_ctl;
2514         u_char  int_ctl_new;
2515         struct  timecounter *tc;
2516         u_int   count;
2517
2518 #ifdef PC98
2519         u_char  tmp=0;
2520         u_char  rsa_buf_status = 0;
2521         int     rsa_tx_fifo_size=0;
2522         recv_data=0;
2523 #endif /* PC98 */
2524
2525         int_ctl = inb(com->intr_ctl_port);
2526         int_ctl_new = int_ctl;
2527
2528         while (!com->gone) {
2529 #ifdef PC98
2530 status_read:;
2531                 if (IS_8251(com->pc98_if_type)) {
2532                         tmp = inb(com->sts_port);
2533 more_intr:
2534                         line_status = 0;
2535                         if (tmp & STS8251_TxRDY) line_status |= LSR_TXRDY;
2536                         if (tmp & STS8251_RxRDY) line_status |= LSR_RXRDY;
2537                         if (tmp & STS8251_TxEMP) line_status |= LSR_TSRE;
2538                         if (tmp & STS8251_PE)    line_status |= LSR_PE;
2539                         if (tmp & STS8251_OE)    line_status |= LSR_OE;
2540                         if (tmp & STS8251_FE)    line_status |= LSR_FE;
2541                         if (tmp & STS8251_BD_SD) line_status |= LSR_BI;
2542                 } else
2543 #endif /* PC98 */
2544                 if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) {
2545                         modem_status = inb(com->modem_status_port);
2546                         if ((modem_status ^ com->last_modem_status) & MSR_DCD) {
2547                                 tc = timecounter;
2548                                 count = tc->tc_get_timecount(tc);
2549                                 pps_event(&com->pps, tc, count, 
2550                                     (modem_status & MSR_DCD) ? 
2551                                     PPS_CAPTURECLEAR : PPS_CAPTUREASSERT);
2552                         }
2553                 }
2554                 line_status = inb(com->line_status_port);
2555 #ifdef PC98
2556                 if (com->pc98_if_type == COM_IF_RSA98III)
2557                         rsa_buf_status = inb(com->rsabase + rsa_srr);
2558 #endif /* PC98 */
2559
2560                 /* input event? (check first to help avoid overruns) */
2561 #ifndef PC98
2562                 while (line_status & LSR_RCV_MASK) {
2563 #else
2564                 while ((line_status & LSR_RCV_MASK)
2565                        || (com->pc98_if_type == COM_IF_RSA98III
2566                            && (rsa_buf_status & 0x08))) {
2567 #endif /* PC98 */
2568                         /* break/unnattached error bits or real input? */
2569 #ifdef PC98
2570                         if (IS_8251(com->pc98_if_type)) {
2571                                 recv_data = inb(com->data_port);
2572                                 if (tmp & 0x78) {
2573                                         pc98_i8251_or_cmd(com,CMD8251_ER);
2574                                         recv_data = 0;
2575                                 }
2576                         } else {
2577 #endif /* PC98 */
2578 #ifdef PC98
2579                         if (com->pc98_if_type == COM_IF_RSA98III) {
2580                           if (!(rsa_buf_status & 0x08))
2581                             recv_data = 0;
2582                           else {
2583                             recv_data = inb(com->data_port);
2584                           }
2585                         } else
2586 #endif
2587                         if (!(line_status & LSR_RXRDY))
2588                                 recv_data = 0;
2589                         else
2590                                 recv_data = inb(com->data_port);
2591 #ifdef PC98
2592                         }
2593 #endif
2594                         if (line_status & (LSR_BI | LSR_FE | LSR_PE)) {
2595                                 /*
2596                                  * Don't store BI if IGNBRK or FE/PE if IGNPAR.
2597                                  * Otherwise, push the work to a higher level
2598                                  * (to handle PARMRK) if we're bypassing.
2599                                  * Otherwise, convert BI/FE and PE+INPCK to 0.
2600                                  *
2601                                  * This makes bypassing work right in the
2602                                  * usual "raw" case (IGNBRK set, and IGNPAR
2603                                  * and INPCK clear).
2604                                  *
2605                                  * Note: BI together with FE/PE means just BI.
2606                                  */
2607                                 if (line_status & LSR_BI) {
2608 #if defined(DDB) && defined(BREAK_TO_DEBUGGER)
2609                                         if (com->unit == comconsole) {
2610                                                 breakpoint();
2611                                                 goto cont;
2612                                         }
2613 #endif
2614                                         if (com->tp == NULL
2615                                             || com->tp->t_iflag & IGNBRK)
2616                                                 goto cont;
2617                                 } else {
2618                                         if (com->tp == NULL
2619                                             || com->tp->t_iflag & IGNPAR)
2620                                                 goto cont;
2621                                 }
2622                                 if (com->tp->t_state & TS_CAN_BYPASS_L_RINT
2623                                     && (line_status & (LSR_BI | LSR_FE)
2624                                         || com->tp->t_iflag & INPCK))
2625                                         recv_data = 0;
2626                         }
2627                         ++com->bytes_in;
2628                         if (com->hotchar != 0 && recv_data == com->hotchar)
2629                                 setsofttty();
2630                         ioptr = com->iptr;
2631                         if (ioptr >= com->ibufend)
2632                                 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
2633                         else {
2634                                 if (com->do_timestamp)
2635                                         microtime(&com->timestamp);
2636                                 ++com_events;
2637                                 schedsofttty();
2638 #if 0 /* for testing input latency vs efficiency */
2639 if (com->iptr - com->ibuf == 8)
2640         setsofttty();
2641 #endif
2642                                 ioptr[0] = recv_data;
2643                                 ioptr[com->ierroff] = line_status;
2644                                 com->iptr = ++ioptr;
2645                                 if (ioptr == com->ihighwater
2646                                     && com->state & CS_RTS_IFLOW)
2647 #ifdef PC98
2648                                         if (IS_8251(com->pc98_if_type))
2649                                                 com_tiocm_bic(com, TIOCM_RTS);
2650                                         else
2651 #endif
2652                                         outb(com->modem_ctl_port,
2653                                              com->mcr_image &= ~MCR_RTS);
2654                                 if (line_status & LSR_OE)
2655                                         CE_RECORD(com, CE_OVERRUN);
2656                         }
2657 cont:
2658                         /*
2659                          * "& 0x7F" is to avoid the gcc-1.40 generating a slow
2660                          * jump from the top of the loop to here
2661                          */
2662 #ifdef PC98
2663                         if (IS_8251(com->pc98_if_type))
2664                                 goto status_read;
2665                         else
2666 #endif
2667                         line_status = inb(com->line_status_port) & 0x7F;
2668 #ifdef PC98
2669                         if (com->pc98_if_type == COM_IF_RSA98III)
2670                                 rsa_buf_status = inb(com->rsabase + rsa_srr);
2671 #endif /* PC98 */
2672                 }
2673
2674                 /* modem status change? (always check before doing output) */
2675 #ifdef PC98
2676                 if (!IS_8251(com->pc98_if_type)) {
2677 #endif
2678                 modem_status = inb(com->modem_status_port);
2679                 if (modem_status != com->last_modem_status) {
2680                         if (com->do_dcd_timestamp
2681                             && !(com->last_modem_status & MSR_DCD)
2682                             && modem_status & MSR_DCD)
2683                                 microtime(&com->dcd_timestamp);
2684
2685                         /*
2686                          * Schedule high level to handle DCD changes.  Note
2687                          * that we don't use the delta bits anywhere.  Some
2688                          * UARTs mess them up, and it's easy to remember the
2689                          * previous bits and calculate the delta.
2690                          */
2691                         com->last_modem_status = modem_status;
2692                         if (!(com->state & CS_CHECKMSR)) {
2693                                 com_events += LOTS_OF_EVENTS;
2694                                 com->state |= CS_CHECKMSR;
2695                                 setsofttty();
2696                         }
2697
2698                         /* handle CTS change immediately for crisp flow ctl */
2699                         if (com->state & CS_CTS_OFLOW) {
2700                                 if (modem_status & MSR_CTS)
2701                                         com->state |= CS_ODEVREADY;
2702                                 else
2703                                         com->state &= ~CS_ODEVREADY;
2704                         }
2705                 }
2706 #ifdef PC98
2707                 }
2708 #endif
2709
2710                 /* output queued and everything ready? */
2711 #ifndef PC98
2712                 if (line_status & LSR_TXRDY
2713                     && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2714 #else
2715                 if (((com->pc98_if_type == COM_IF_RSA98III)
2716                      ? (rsa_buf_status & 0x02)
2717                      : (line_status & LSR_TXRDY))
2718                     && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2719 #endif
2720                         ioptr = com->obufq.l_head;
2721                         if (com->tx_fifo_size > 1) {
2722                                 u_int   ocount;
2723
2724                                 ocount = com->obufq.l_tail - ioptr;
2725 #ifdef PC98
2726                                 if (com->pc98_if_type == COM_IF_RSA98III) {
2727                                   rsa_buf_status = inb(com->rsabase + rsa_srr);
2728                                   rsa_tx_fifo_size = 1024;
2729                                   if (!(rsa_buf_status & 0x01))
2730                                     rsa_tx_fifo_size = 2048;
2731                                   if (ocount > rsa_tx_fifo_size)
2732                                     ocount = rsa_tx_fifo_size;
2733                                 } else
2734 #endif
2735                                 if (ocount > com->tx_fifo_size)
2736                                         ocount = com->tx_fifo_size;
2737                                 com->bytes_out += ocount;
2738                                 do
2739                                         outb(com->data_port, *ioptr++);
2740                                 while (--ocount != 0);
2741                         } else {
2742                                 outb(com->data_port, *ioptr++);
2743                                 ++com->bytes_out;
2744                         }
2745 #ifdef PC98
2746                         if (IS_8251(com->pc98_if_type))
2747                             if (!(pc98_check_i8251_interrupt(com) & IEN_TxFLAG))
2748                                         com_int_Tx_enable(com);
2749 #endif
2750                         com->obufq.l_head = ioptr;
2751                         if (COM_IIR_TXRDYBUG(com->flags)) {
2752                                 int_ctl_new = int_ctl | IER_ETXRDY;
2753                         }
2754                         if (ioptr >= com->obufq.l_tail) {
2755                                 struct lbq      *qp;
2756
2757                                 qp = com->obufq.l_next;
2758                                 qp->l_queued = FALSE;
2759                                 qp = qp->l_next;
2760                                 if (qp != NULL) {
2761                                         com->obufq.l_head = qp->l_head;
2762                                         com->obufq.l_tail = qp->l_tail;
2763                                         com->obufq.l_next = qp;
2764                                 } else {
2765                                         /* output just completed */
2766                                         if ( COM_IIR_TXRDYBUG(com->flags) ) {
2767                                                 int_ctl_new = int_ctl & ~IER_ETXRDY;
2768                                         }
2769                                         com->state &= ~CS_BUSY;
2770 #if defined(PC98)
2771                                         if (IS_8251(com->pc98_if_type))
2772                                             if ( pc98_check_i8251_interrupt(com) & IEN_TxFLAG )
2773                                                 com_int_Tx_disable(com);
2774 #endif
2775                                 }
2776                                 if (!(com->state & CS_ODONE)) {
2777                                         com_events += LOTS_OF_EVENTS;
2778                                         com->state |= CS_ODONE;
2779                                         setsofttty();   /* handle at high level ASAP */
2780                                 }
2781                         }
2782                         if ( COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) {
2783 #ifdef PC98
2784                                 if (com->pc98_if_type == COM_IF_RSA98III) {
2785                                   int_ctl_new &= ~(IER_ETXRDY | IER_ERXRDY);
2786                                   outb(com->intr_ctl_port, int_ctl_new);
2787                                   outb(com->rsabase + rsa_ier, 0x1d);
2788                                 } else
2789 #endif
2790                                 outb(com->intr_ctl_port, int_ctl_new);
2791                         }
2792                 }
2793 #ifdef PC98
2794                 else if (line_status & LSR_TXRDY) {
2795                     if (IS_8251(com->pc98_if_type))
2796                         if ( pc98_check_i8251_interrupt(com) & IEN_TxFLAG )
2797                             com_int_Tx_disable(com);
2798                 }
2799                 if (IS_8251(com->pc98_if_type))
2800                     if ((tmp = inb(com->sts_port)) & STS8251_RxRDY)
2801                         goto more_intr;
2802 #endif
2803
2804                 /* finished? */
2805 #ifndef COM_MULTIPORT
2806 #ifdef PC98
2807                 if (IS_8251(com->pc98_if_type))
2808                         return;
2809 #endif
2810                 if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND)
2811 #endif /* COM_MULTIPORT */
2812                         return;
2813         }
2814 }
2815
2816 static int
2817 sioioctl(dev, cmd, data, flag, p)
2818         dev_t           dev;
2819         u_long          cmd;
2820         caddr_t         data;
2821         int             flag;
2822         struct proc     *p;
2823 {
2824         struct com_s    *com;
2825         int             error;
2826         Port_t          iobase;
2827         int             mynor;
2828         int             s;
2829         struct tty      *tp;
2830 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2831         u_long          oldcmd;
2832         struct termios  term;
2833 #endif
2834
2835         mynor = minor(dev);
2836         com = com_addr(MINOR_TO_UNIT(mynor));
2837         if (com->gone)
2838                 return (ENODEV);
2839         iobase = com->iobase;
2840         if (mynor & CONTROL_MASK) {
2841                 struct termios  *ct;
2842
2843                 switch (mynor & CONTROL_MASK) {
2844                 case CONTROL_INIT_STATE:
2845                         ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
2846                         break;
2847                 case CONTROL_LOCK_STATE:
2848                         ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
2849                         break;
2850                 default:
2851                         return (ENODEV);        /* /dev/nodev */
2852                 }
2853                 switch (cmd) {
2854                 case TIOCSETA:
2855                         error = suser(p->p_ucred, &p->p_acflag);
2856                         if (error != 0)
2857                                 return (error);
2858                         *ct = *(struct termios *)data;
2859                         return (0);
2860                 case TIOCGETA:
2861                         *(struct termios *)data = *ct;
2862                         return (0);
2863                 case TIOCGETD:
2864                         *(int *)data = TTYDISC;
2865                         return (0);
2866                 case TIOCGWINSZ:
2867                         bzero(data, sizeof(struct winsize));
2868                         return (0);
2869                 default:
2870                         return (ENOTTY);
2871                 }
2872         }
2873         tp = com->tp;
2874 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2875         term = tp->t_termios;
2876         oldcmd = cmd;
2877         error = ttsetcompat(tp, &cmd, data, &term);
2878         if (error != 0)
2879                 return (error);
2880         if (cmd != oldcmd)
2881                 data = (caddr_t)&term;
2882 #endif
2883         if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
2884                 int     cc;
2885                 struct termios *dt = (struct termios *)data;
2886                 struct termios *lt = mynor & CALLOUT_MASK
2887                                      ? &com->lt_out : &com->lt_in;
2888
2889                 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
2890                               | (dt->c_iflag & ~lt->c_iflag);
2891                 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
2892                               | (dt->c_oflag & ~lt->c_oflag);
2893                 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
2894                               | (dt->c_cflag & ~lt->c_cflag);
2895                 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
2896                               | (dt->c_lflag & ~lt->c_lflag);
2897                 for (cc = 0; cc < NCCS; ++cc)
2898                         if (lt->c_cc[cc] != 0)
2899                                 dt->c_cc[cc] = tp->t_cc[cc];
2900                 if (lt->c_ispeed != 0)
2901                         dt->c_ispeed = tp->t_ispeed;
2902                 if (lt->c_ospeed != 0)
2903                         dt->c_ospeed = tp->t_ospeed;
2904         }
2905         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
2906         if (error != ENOIOCTL)
2907                 return (error);
2908         s = spltty();
2909         error = ttioctl(tp, cmd, data, flag);
2910         disc_optim(tp, &tp->t_termios, com);
2911         if (error != ENOIOCTL) {
2912                 splx(s);
2913                 return (error);
2914         }
2915 #ifdef PC98
2916         if (IS_8251(com->pc98_if_type)) {
2917             switch (cmd) {
2918             case TIOCSBRK:
2919                 com_send_break_on( com );
2920                 break;
2921             case TIOCCBRK:
2922                 com_send_break_off( com );
2923                 break;
2924             case TIOCSDTR:
2925                 com_tiocm_bis(com, TIOCM_DTR | TIOCM_RTS );
2926                 break;
2927             case TIOCCDTR:
2928                 com_tiocm_bic(com, TIOCM_DTR);
2929                 break;
2930         /*
2931          * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set.  The
2932          * changes get undone on the next call to comparam().
2933          */
2934             case TIOCMSET:
2935                 com_tiocm_set( com, *(int *)data );
2936                 break;
2937             case TIOCMBIS:
2938                 com_tiocm_bis( com, *(int *)data );
2939                 break;
2940             case TIOCMBIC:
2941                 com_tiocm_bic( com, *(int *)data );
2942                 break;
2943             case TIOCMGET:
2944                 *(int *)data = com_tiocm_get(com);
2945                 break;
2946             case TIOCMSDTRWAIT:
2947                 /* must be root since the wait applies to following logins */
2948                 error = suser(p->p_ucred, &p->p_acflag);
2949                 if (error != 0) {
2950                         splx(s);
2951                         return (error);
2952                 }
2953                 com->dtr_wait = *(int *)data * hz / 100;
2954                 break;
2955             case TIOCMGDTRWAIT:
2956                 *(int *)data = com->dtr_wait * 100 / hz;
2957                 break;
2958             case TIOCTIMESTAMP:
2959                 com->do_timestamp = TRUE;
2960                 *(struct timeval *)data = com->timestamp;
2961                 break;
2962             case TIOCDCDTIMESTAMP:
2963                 com->do_dcd_timestamp = TRUE;
2964                 *(struct timeval *)data = com->dcd_timestamp;
2965                 break;
2966             default:
2967                 splx(s);
2968                 return (ENOTTY);
2969             }
2970         } else {
2971             int port_shift;
2972             port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
2973 #endif
2974         switch (cmd) {
2975         case TIOCSBRK:
2976 #ifdef PC98
2977                 outb(iobase + (com_cfcr << port_shift),
2978                      com->cfcr_image |= CFCR_SBREAK);
2979 #else
2980                 outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
2981 #endif
2982                 break;
2983         case TIOCCBRK:
2984 #ifdef PC98
2985                 outb(iobase + (com_cfcr << port_shift),
2986                      com->cfcr_image &= ~CFCR_SBREAK);
2987 #else
2988                 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
2989 #endif
2990                 break;
2991         case TIOCSDTR:
2992                 (void)commctl(com, TIOCM_DTR, DMBIS);
2993                 break;
2994         case TIOCCDTR:
2995                 (void)commctl(com, TIOCM_DTR, DMBIC);
2996                 break;
2997         /*
2998          * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set.  The
2999          * changes get undone on the next call to comparam().
3000          */
3001         case TIOCMSET:
3002                 (void)commctl(com, *(int *)data, DMSET);
3003                 break;
3004         case TIOCMBIS:
3005                 (void)commctl(com, *(int *)data, DMBIS);
3006                 break;
3007         case TIOCMBIC:
3008                 (void)commctl(com, *(int *)data, DMBIC);
3009                 break;
3010         case TIOCMGET:
3011                 *(int *)data = commctl(com, 0, DMGET);
3012                 break;
3013         case TIOCMSDTRWAIT:
3014                 /* must be root since the wait applies to following logins */
3015                 error = suser(p->p_ucred, &p->p_acflag);
3016                 if (error != 0) {
3017                         splx(s);
3018                         return (error);
3019                 }
3020                 com->dtr_wait = *(int *)data * hz / 100;
3021                 break;
3022         case TIOCMGDTRWAIT:
3023                 *(int *)data = com->dtr_wait * 100 / hz;
3024                 break;
3025         case TIOCTIMESTAMP:
3026                 com->do_timestamp = TRUE;
3027                 *(struct timeval *)data = com->timestamp;
3028                 break;
3029         case TIOCDCDTIMESTAMP:
3030                 com->do_dcd_timestamp = TRUE;
3031                 *(struct timeval *)data = com->dcd_timestamp;
3032                 break;
3033         default:
3034                 splx(s);
3035                 error = pps_ioctl(cmd, data, &com->pps);
3036                 if (error == ENODEV)
3037                         error = ENOTTY;
3038                 return (error);
3039         }
3040 #ifdef PC98
3041         }
3042 #endif
3043         splx(s);
3044         return (0);
3045 }
3046
3047 static void
3048 siopoll()
3049 {
3050         int             unit;
3051
3052         if (com_events == 0)
3053                 return;
3054 repeat:
3055         for (unit = 0; unit < NSIOTOT; ++unit) {
3056                 struct com_s    *com;
3057                 int             incc;
3058                 struct tty      *tp;
3059
3060                 com = com_addr(unit);
3061                 if (com == NULL)
3062                         continue;
3063                 tp = com->tp;
3064                 if (tp == NULL || com->gone) {
3065                         /*
3066                          * Discard any events related to never-opened or
3067                          * going-away devices.
3068                          */
3069                         disable_intr();
3070                         incc = com->iptr - com->ibuf;
3071                         com->iptr = com->ibuf;
3072                         if (com->state & CS_CHECKMSR) {
3073                                 incc += LOTS_OF_EVENTS;
3074                                 com->state &= ~CS_CHECKMSR;
3075                         }
3076                         com_events -= incc;
3077                         enable_intr();
3078                         continue;
3079                 }
3080                 if (com->iptr != com->ibuf) {
3081                         disable_intr();
3082                         sioinput(com);
3083                         enable_intr();
3084                 }
3085                 if (com->state & CS_CHECKMSR) {
3086                         u_char  delta_modem_status;
3087
3088 #ifdef PC98
3089                         if (!IS_8251(com->pc98_if_type)) {
3090 #endif
3091                         disable_intr();
3092                         delta_modem_status = com->last_modem_status
3093                                              ^ com->prev_modem_status;
3094                         com->prev_modem_status = com->last_modem_status;
3095                         com_events -= LOTS_OF_EVENTS;
3096                         com->state &= ~CS_CHECKMSR;
3097                         enable_intr();
3098                         if (delta_modem_status & MSR_DCD)
3099                                 (*linesw[tp->t_line].l_modem)
3100                                         (tp, com->prev_modem_status & MSR_DCD);
3101 #ifdef PC98
3102                         }
3103 #endif
3104                 }
3105                 if (com->state & CS_ODONE) {
3106                         disable_intr();
3107                         com_events -= LOTS_OF_EVENTS;
3108                         com->state &= ~CS_ODONE;
3109                         enable_intr();
3110                         if (!(com->state & CS_BUSY)
3111                             && !(com->extra_state & CSE_BUSYCHECK)) {
3112                                 timeout(siobusycheck, com, hz / 100);
3113                                 com->extra_state |= CSE_BUSYCHECK;
3114                         }
3115                         (*linesw[tp->t_line].l_start)(tp);
3116                 }
3117                 if (com_events == 0)
3118                         break;
3119         }
3120         if (com_events >= LOTS_OF_EVENTS)
3121                 goto repeat;
3122 }
3123
3124 static int
3125 comparam(tp, t)
3126         struct tty      *tp;
3127         struct termios  *t;
3128 {
3129         u_int           cfcr;
3130         int             cflag;
3131         struct com_s    *com;
3132         int             divisor;
3133         u_char          dlbh;
3134         u_char          dlbl;
3135         Port_t          iobase;
3136         int             s;
3137         int             unit;
3138 #ifdef PC98
3139         int             port_shift = 0;
3140         u_char          param = 0;
3141 #endif
3142
3143 #ifdef PC98
3144         cfcr = 0;
3145         unit = DEV_TO_UNIT(tp->t_dev);
3146         com = com_addr(unit);
3147         iobase = com->iobase;
3148         if (IS_8251(com->pc98_if_type)) {
3149             divisor = pc98_ttspeedtab(com, t->c_ospeed);
3150         } else {
3151             port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
3152
3153             /* do historical conversions */
3154             if (t->c_ispeed == 0)
3155                 t->c_ispeed = t->c_ospeed;
3156
3157             /* check requested parameters */
3158             divisor = ttspeedtab(t->c_ospeed,
3159                         if_16550a_type[com->pc98_if_type & 0x0f].speedtab);
3160         }
3161 #else
3162         /* do historical conversions */
3163         if (t->c_ispeed == 0)
3164                 t->c_ispeed = t->c_ospeed;
3165
3166         /* check requested parameters */
3167         divisor = ttspeedtab(t->c_ospeed, comspeedtab);
3168 #endif
3169         if (divisor < 0 || divisor > 0 && t->c_ispeed != t->c_ospeed)
3170                 return (EINVAL);
3171
3172         /* parameters are OK, convert them to the com struct and the device */
3173 #ifndef PC98
3174         unit = DEV_TO_UNIT(tp->t_dev);
3175         com = com_addr(unit);
3176         iobase = com->iobase;
3177 #endif
3178         s = spltty();
3179 #ifdef PC98
3180         if (IS_8251(com->pc98_if_type)) {
3181                 if (divisor == 0)
3182                         com_tiocm_bic( com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE );
3183                 else
3184                         com_tiocm_bis( com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE );
3185         } else {
3186 #endif
3187         if (divisor == 0)
3188                 (void)commctl(com, TIOCM_DTR, DMBIC);   /* hang up line */
3189         else
3190                 (void)commctl(com, TIOCM_DTR, DMBIS);
3191 #ifdef PC98
3192         }
3193 #endif
3194         cflag = t->c_cflag;
3195 #ifdef PC98
3196         if (!IS_8251(com->pc98_if_type)) {
3197 #endif
3198         switch (cflag & CSIZE) {
3199         case CS5:
3200                 cfcr = CFCR_5BITS;
3201                 break;
3202         case CS6:
3203                 cfcr = CFCR_6BITS;
3204                 break;
3205         case CS7:
3206                 cfcr = CFCR_7BITS;
3207                 break;
3208         default:
3209                 cfcr = CFCR_8BITS;
3210                 break;
3211         }
3212         if (cflag & PARENB) {
3213                 cfcr |= CFCR_PENAB;
3214                 if (!(cflag & PARODD))
3215                         cfcr |= CFCR_PEVEN;
3216         }
3217         if (cflag & CSTOPB)
3218                 cfcr |= CFCR_STOPB;
3219
3220         if (com->hasfifo && divisor != 0) {
3221                 /*
3222                  * Use a fifo trigger level low enough so that the input
3223                  * latency from the fifo is less than about 16 msec and
3224                  * the total latency is less than about 30 msec.  These
3225                  * latencies are reasonable for humans.  Serial comms
3226                  * protocols shouldn't expect anything better since modem
3227                  * latencies are larger.
3228                  */
3229                 com->fifo_image = t->c_ospeed <= 4800
3230                                   ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_HIGH;
3231 #ifdef COM_ESP
3232                 /*
3233                  * The Hayes ESP card needs the fifo DMA mode bit set
3234                  * in compatibility mode.  If not, it will interrupt
3235                  * for each character received.
3236                  */
3237                 if (com->esp)
3238                         com->fifo_image |= FIFO_DMA_MODE;
3239 #endif
3240 #ifdef PC98
3241                 outb(iobase + (com_fifo << port_shift), com->fifo_image);
3242 #else
3243                 outb(iobase + com_fifo, com->fifo_image);
3244 #endif
3245         }
3246 #ifdef PC98
3247         }
3248 #endif
3249
3250         /*
3251          * This returns with interrupts disabled so that we can complete
3252          * the speed change atomically.  Keeping interrupts disabled is
3253          * especially important while com_data is hidden.
3254          */
3255         (void) siosetwater(com, t->c_ispeed);
3256
3257 #ifdef PC98
3258         if (IS_8251(com->pc98_if_type))
3259             com_cflag_and_speed_set(com, cflag, t->c_ospeed);
3260         else {
3261 #endif
3262         if (divisor != 0) {
3263 #ifdef PC98
3264                 outb(iobase + (com_cfcr << port_shift), cfcr | CFCR_DLAB);
3265 #else
3266                 outb(iobase + com_cfcr, cfcr | CFCR_DLAB);
3267 #endif
3268                 /*
3269                  * Only set the divisor registers if they would change,
3270                  * since on some 16550 incompatibles (UMC8669F), setting
3271                  * them while input is arriving them loses sync until
3272                  * data stops arriving.
3273                  */
3274                 dlbl = divisor & 0xFF;
3275 #ifdef PC98
3276                 if (inb(iobase + (com_dlbl << port_shift)) != dlbl)
3277                         outb(iobase + (com_dlbl << port_shift), dlbl);
3278                 dlbh = (u_int) divisor >> 8;
3279                 if (inb(iobase + (com_dlbh << port_shift)) != dlbh)
3280                         outb(iobase + (com_dlbh << port_shift), dlbh);
3281 #else
3282                 if (inb(iobase + com_dlbl) != dlbl)
3283                         outb(iobase + com_dlbl, dlbl);
3284                 dlbh = (u_int) divisor >> 8;
3285                 if (inb(iobase + com_dlbh) != dlbh)
3286                         outb(iobase + com_dlbh, dlbh);
3287 #endif
3288         }
3289
3290
3291 #ifdef PC98
3292         }
3293         outb(iobase + (com_cfcr << port_shift), com->cfcr_image = cfcr);
3294 #else
3295         outb(iobase + com_cfcr, com->cfcr_image = cfcr);
3296 #endif
3297
3298         if (!(tp->t_state & TS_TTSTOP))
3299                 com->state |= CS_TTGO;
3300
3301         if (cflag & CRTS_IFLOW) {
3302                 if (com->st16650a) {
3303                         outb(iobase + com_cfcr, 0xbf);
3304                         outb(iobase + com_fifo, inb(iobase + com_fifo) | 0x40);
3305                 }
3306                 com->state |= CS_RTS_IFLOW;
3307                 /*
3308                  * If CS_RTS_IFLOW just changed from off to on, the change
3309                  * needs to be propagated to MCR_RTS.  This isn't urgent,
3310                  * so do it later by calling comstart() instead of repeating
3311                  * a lot of code from comstart() here.
3312                  */
3313         } else if (com->state & CS_RTS_IFLOW) {
3314                 com->state &= ~CS_RTS_IFLOW;
3315                 /*
3316                  * CS_RTS_IFLOW just changed from on to off.  Force MCR_RTS
3317                  * on here, since comstart() won't do it later.
3318                  */
3319 #ifdef PC98
3320                 if (IS_8251(com->pc98_if_type))
3321                         com_tiocm_bis(com, TIOCM_RTS);
3322                 else
3323 #endif
3324                 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
3325                 if (com->st16650a) {
3326                         outb(iobase + com_cfcr, 0xbf);
3327                         outb(iobase + com_fifo, inb(iobase + com_fifo) & ~0x40);
3328                 }
3329         }
3330
3331
3332         /*
3333          * Set up state to handle output flow control.
3334          * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
3335          * Now has 10+ msec latency, while CTS flow has 50- usec latency.
3336          */
3337         com->state |= CS_ODEVREADY;
3338         com->state &= ~CS_CTS_OFLOW;
3339 #ifdef PC98
3340         if (com->pc98_if_type == COM_IF_RSA98III) {
3341                 param = inb(com->rsabase + rsa_msr);
3342                 outb(com->rsabase + rsa_msr, param & 0x14);
3343         }
3344 #endif
3345         if (cflag & CCTS_OFLOW) {
3346                 com->state |= CS_CTS_OFLOW;
3347 #ifdef PC98
3348                 if (IS_8251(com->pc98_if_type)) {
3349                         if (!(pc98_get_modem_status(com) & TIOCM_CTS))
3350                                 com->state &= ~CS_ODEVREADY;
3351                 } else {
3352 #endif
3353 #ifdef PC98
3354                 if (com->pc98_if_type == COM_IF_RSA98III) {
3355                         /* Set automatic flow control mode */
3356                         outb(com->rsabase + rsa_msr, param | 0x08);
3357                 } else
3358 #endif
3359                 if (!(com->last_modem_status & MSR_CTS))
3360                         com->state &= ~CS_ODEVREADY;
3361                 if (com->st16650a) {
3362                         outb(iobase + com_cfcr, 0xbf);
3363                         outb(iobase + com_fifo, inb(iobase + com_fifo) | 0x80);
3364                 }
3365 #ifdef PC98
3366                 }
3367 #endif
3368         } else {
3369                 if (com->st16650a) {
3370                         outb(iobase + com_cfcr, 0xbf);
3371                         outb(iobase + com_fifo, inb(iobase + com_fifo) & ~0x80);
3372                 }
3373         }
3374
3375
3376 #ifdef PC98
3377         outb(iobase + (com_cfcr << port_shift), com->cfcr_image);
3378 #else
3379         outb(iobase + com_cfcr, com->cfcr_image);
3380 #endif
3381
3382
3383         /* XXX shouldn't call functions while intrs are disabled. */
3384         disc_optim(tp, t, com);
3385         /*
3386          * Recover from fiddling with CS_TTGO.  We used to call siointr1()
3387          * unconditionally, but that defeated the careful discarding of
3388          * stale input in sioopen().
3389          */
3390         if (com->state >= (CS_BUSY | CS_TTGO))
3391                 siointr1(com);
3392
3393         enable_intr();
3394         splx(s);
3395         comstart(tp);
3396         if (com->ibufold != NULL) {
3397                 free(com->ibufold, M_DEVBUF);
3398                 com->ibufold = NULL;
3399         }
3400         return (0);
3401 }
3402
3403 static int
3404 siosetwater(com, speed)
3405         struct com_s    *com;
3406         speed_t         speed;
3407 {
3408         int             cp4ticks;
3409         u_char          *ibuf;
3410         int             ibufsize;
3411         struct tty      *tp;
3412
3413         /*
3414          * Make the buffer size large enough to handle a softtty interrupt
3415          * latency of about 2 ticks without loss of throughput or data
3416          * (about 3 ticks if input flow control is not used or not honoured,
3417          * but a bit less for CS5-CS7 modes).
3418          */
3419         cp4ticks = speed / 10 / hz * 4;
3420         for (ibufsize = 128; ibufsize < cp4ticks;)
3421                 ibufsize <<= 1;
3422 #ifdef PC98
3423         if (com->pc98_if_type == COM_IF_RSA98III)
3424                 ibufsize = 2048;
3425 #endif
3426         if (ibufsize == com->ibufsize) {
3427                 disable_intr();
3428                 return (0);
3429         }
3430
3431         /*
3432          * Allocate input buffer.  The extra factor of 2 in the size is
3433          * to allow for an error byte for each input byte.
3434          */
3435         ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT);
3436         if (ibuf == NULL) {
3437                 disable_intr();
3438                 return (ENOMEM);
3439         }
3440
3441         /* Initialize non-critical variables. */
3442         com->ibufold = com->ibuf;
3443         com->ibufsize = ibufsize;
3444         tp = com->tp;
3445         if (tp != NULL) {
3446                 tp->t_ififosize = 2 * ibufsize;
3447                 tp->t_ispeedwat = (speed_t)-1;
3448                 tp->t_ospeedwat = (speed_t)-1;
3449         }
3450
3451         /*
3452          * Read current input buffer, if any.  Continue with interrupts
3453          * disabled.
3454          */
3455         disable_intr();
3456         if (com->iptr != com->ibuf)
3457                 sioinput(com);
3458
3459         /*-
3460          * Initialize critical variables, including input buffer watermarks.
3461          * The external device is asked to stop sending when the buffer
3462          * exactly reaches high water, or when the high level requests it.
3463          * The high level is notified immediately (rather than at a later
3464          * clock tick) when this watermark is reached.
3465          * The buffer size is chosen so the watermark should almost never
3466          * be reached.
3467          * The low watermark is invisibly 0 since the buffer is always
3468          * emptied all at once.
3469          */
3470         com->iptr = com->ibuf = ibuf;
3471         com->ibufend = ibuf + ibufsize;
3472         com->ierroff = ibufsize;
3473         com->ihighwater = ibuf + 3 * ibufsize / 4;
3474         return (0);
3475 }
3476
3477 static void
3478 comstart(tp)
3479         struct tty      *tp;
3480 {
3481         struct com_s    *com;
3482         int             s;
3483         int             unit;
3484 #ifdef PC98
3485         int             tmp;
3486 #endif
3487
3488         unit = DEV_TO_UNIT(tp->t_dev);
3489         com = com_addr(unit);
3490         s = spltty();
3491         disable_intr();
3492         if (tp->t_state & TS_TTSTOP)
3493                 com->state &= ~CS_TTGO;
3494         else
3495                 com->state |= CS_TTGO;
3496         if (tp->t_state & TS_TBLOCK) {
3497 #ifdef PC98
3498                 if (IS_8251(com->pc98_if_type))
3499                         tmp = com_tiocm_get(com) & TIOCM_RTS;
3500                 else
3501                         tmp = com->mcr_image & MCR_RTS;
3502                 if (tmp && (com->state & CS_RTS_IFLOW))
3503 #else
3504                 if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW)
3505 #endif
3506 #ifdef PC98
3507                         if (IS_8251(com->pc98_if_type))
3508                                 com_tiocm_bic(com, TIOCM_RTS);
3509                         else
3510 #endif
3511                         outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
3512         } else {
3513 #ifdef PC98
3514                 if (IS_8251(com->pc98_if_type))
3515                         tmp = com_tiocm_get(com) & TIOCM_RTS;
3516                 else
3517                         tmp = com->mcr_image & MCR_RTS;
3518                 if (!(tmp) && com->iptr < com->ihighwater
3519                         && com->state & CS_RTS_IFLOW)
3520 #else
3521                 if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater
3522                     && com->state & CS_RTS_IFLOW)
3523 #endif
3524 #ifdef PC98
3525                         if (IS_8251(com->pc98_if_type))
3526                                 com_tiocm_bis(com, TIOCM_RTS);
3527                         else
3528 #endif
3529                         outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
3530         }
3531         enable_intr();
3532         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
3533                 ttwwakeup(tp);
3534 #ifdef PC98
3535 /*              if(IS_8251(com->pc98_if_type))
3536                         com_int_Tx_enable(com); */
3537 #endif
3538                 splx(s);
3539                 return;
3540         }
3541         if (tp->t_outq.c_cc != 0) {
3542                 struct lbq      *qp;
3543                 struct lbq      *next;
3544
3545                 if (!com->obufs[0].l_queued) {
3546                         com->obufs[0].l_tail
3547                             = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
3548 #ifndef PC98
3549                                                   sizeof com->obuf1);
3550 #else
3551                                                   com->obufsize);
3552 #endif
3553                         com->obufs[0].l_next = NULL;
3554                         com->obufs[0].l_queued = TRUE;
3555                         disable_intr();
3556                         if (com->state & CS_BUSY) {
3557                                 qp = com->obufq.l_next;
3558                                 while ((next = qp->l_next) != NULL)
3559                                         qp = next;
3560                                 qp->l_next = &com->obufs[0];
3561                         } else {
3562                                 com->obufq.l_head = com->obufs[0].l_head;
3563                                 com->obufq.l_tail = com->obufs[0].l_tail;
3564                                 com->obufq.l_next = &com->obufs[0];
3565                                 com->state |= CS_BUSY;
3566                         }
3567                         enable_intr();
3568                 }
3569                 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
3570                         com->obufs[1].l_tail
3571                             = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
3572 #ifndef PC98
3573                                                   sizeof com->obuf2);
3574 #else
3575                                                   com->obufsize);
3576 #endif
3577                         com->obufs[1].l_next = NULL;
3578                         com->obufs[1].l_queued = TRUE;
3579                         disable_intr();
3580                         if (com->state & CS_BUSY) {
3581                                 qp = com->obufq.l_next;
3582                                 while ((next = qp->l_next) != NULL)
3583                                         qp = next;
3584                                 qp->l_next = &com->obufs[1];
3585                         } else {
3586                                 com->obufq.l_head = com->obufs[1].l_head;
3587                                 com->obufq.l_tail = com->obufs[1].l_tail;
3588                                 com->obufq.l_next = &com->obufs[1];
3589                                 com->state |= CS_BUSY;
3590                         }
3591                         enable_intr();
3592                 }
3593                 tp->t_state |= TS_BUSY;
3594         }
3595         disable_intr();
3596         if (com->state >= (CS_BUSY | CS_TTGO))
3597                 siointr1(com);  /* fake interrupt to start output */
3598         enable_intr();
3599 #ifdef PC98
3600 /*              if(IS_8251(com->pc98_if_type))
3601                         com_int_Tx_enable(com); */
3602 #endif
3603         ttwwakeup(tp);
3604         splx(s);
3605 }
3606
3607 static void
3608 siostop(tp, rw)
3609         struct tty      *tp;
3610         int             rw;
3611 {
3612         struct com_s    *com;
3613 #ifdef PC98
3614         int             port_shift = 0;
3615         int             rsa98_tmp  = 0;
3616 #endif
3617
3618         com = com_addr(DEV_TO_UNIT(tp->t_dev));
3619         if (com->gone)
3620                 return;
3621 #ifdef PC98
3622         if (IS_8251(com->pc98_if_type))
3623             port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
3624 #endif
3625         disable_intr();
3626         if (rw & FWRITE) {
3627                 if (com->hasfifo)
3628 #ifdef COM_ESP
3629                     /* XXX avoid h/w bug. */
3630                     if (!com->esp)
3631 #endif
3632 #ifdef PC98
3633                         outb(com->iobase + (com_fifo << port_shift),
3634                              FIFO_XMT_RST | com->fifo_image);
3635                         if (com->pc98_if_type == COM_IF_RSA98III)
3636                             for(rsa98_tmp = 0; rsa98_tmp < 2048; rsa98_tmp++)
3637                                 outb(com->iobase + (com_fifo << port_shift),
3638                                      FIFO_XMT_RST | com->fifo_image);
3639 #else
3640                         outb(com->iobase + com_fifo,
3641                              FIFO_XMT_RST | com->fifo_image);
3642 #endif
3643                 com->obufs[0].l_queued = FALSE;
3644                 com->obufs[1].l_queued = FALSE;
3645                 if (com->state & CS_ODONE)
3646                         com_events -= LOTS_OF_EVENTS;
3647                 com->state &= ~(CS_ODONE | CS_BUSY);
3648                 com->tp->t_state &= ~TS_BUSY;
3649         }
3650         if (rw & FREAD) {
3651                 if (com->hasfifo)
3652 #ifdef COM_ESP
3653                     /* XXX avoid h/w bug. */
3654                     if (!com->esp)
3655 #endif
3656 #ifdef PC98
3657                         if (com->pc98_if_type == COM_IF_RSA98III) {
3658                             for(rsa98_tmp = 0; rsa98_tmp < 2048; rsa98_tmp++)
3659                                 inb(com->data_port);
3660                         }
3661                         outb(com->iobase + (com_fifo << port_shift),
3662                              FIFO_RCV_RST | com->fifo_image);
3663 #else
3664                         outb(com->iobase + com_fifo,
3665                              FIFO_RCV_RST | com->fifo_image);
3666 #endif
3667                 com_events -= (com->iptr - com->ibuf);
3668                 com->iptr = com->ibuf;
3669         }
3670         enable_intr();
3671         comstart(tp);
3672 }
3673
3674 static struct tty *
3675 siodevtotty(dev)
3676         dev_t   dev;
3677 {
3678         int     mynor;
3679         int     unit;
3680
3681         mynor = minor(dev);
3682         if (mynor & CONTROL_MASK)
3683                 return (NULL);
3684         unit = MINOR_TO_UNIT(mynor);
3685         if ((u_int) unit >= NSIOTOT)
3686                 return (NULL);
3687         return (&sio_tty[unit]);
3688 }
3689
3690 static int
3691 commctl(com, bits, how)
3692         struct com_s    *com;
3693         int             bits;
3694         int             how;
3695 {
3696         int     mcr;
3697         int     msr;
3698
3699         if (how == DMGET) {
3700                 bits = TIOCM_LE;        /* XXX - always enabled while open */
3701                 mcr = com->mcr_image;
3702                 if (mcr & MCR_DTR)
3703                         bits |= TIOCM_DTR;
3704                 if (mcr & MCR_RTS)
3705                         bits |= TIOCM_RTS;
3706                 msr = com->prev_modem_status;
3707                 if (msr & MSR_CTS)
3708                         bits |= TIOCM_CTS;
3709                 if (msr & MSR_DCD)
3710                         bits |= TIOCM_CD;
3711                 if (msr & MSR_DSR)
3712                         bits |= TIOCM_DSR;
3713                 /*
3714                  * XXX - MSR_RI is naturally volatile, and we make MSR_TERI
3715                  * more volatile by reading the modem status a lot.  Perhaps
3716                  * we should latch both bits until the status is read here.
3717                  */
3718                 if (msr & (MSR_RI | MSR_TERI))
3719                         bits |= TIOCM_RI;
3720                 return (bits);
3721         }
3722         mcr = 0;
3723         if (bits & TIOCM_DTR)
3724                 mcr |= MCR_DTR;
3725         if (bits & TIOCM_RTS)
3726                 mcr |= MCR_RTS;
3727         if (com->gone)
3728                 return(0);
3729         disable_intr();
3730         switch (how) {
3731         case DMSET:
3732                 outb(com->modem_ctl_port,
3733                      com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE));
3734                 break;
3735         case DMBIS:
3736                 outb(com->modem_ctl_port, com->mcr_image |= mcr);
3737                 break;
3738         case DMBIC:
3739                 outb(com->modem_ctl_port, com->mcr_image &= ~mcr);
3740                 break;
3741         }
3742         enable_intr();
3743         return (0);
3744 }
3745
3746 static void
3747 siosettimeout()
3748 {
3749         struct com_s    *com;
3750         bool_t          someopen;
3751         int             unit;
3752
3753         /*
3754          * Set our timeout period to 1 second if no polled devices are open.
3755          * Otherwise set it to max(1/200, 1/hz).
3756          * Enable timeouts iff some device is open.
3757          */
3758         untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
3759         sio_timeout = hz;
3760         someopen = FALSE;
3761         for (unit = 0; unit < NSIOTOT; ++unit) {
3762                 com = com_addr(unit);
3763                 if (com != NULL && com->tp != NULL
3764                     && com->tp->t_state & TS_ISOPEN && !com->gone) {
3765                         someopen = TRUE;
3766                         if (com->poll || com->poll_output) {
3767                                 sio_timeout = hz > 200 ? hz / 200 : 1;
3768                                 break;
3769                         }
3770                 }
3771         }
3772         if (someopen) {
3773                 sio_timeouts_until_log = hz / sio_timeout;
3774                 sio_timeout_handle = timeout(comwakeup, (void *)NULL,
3775                                              sio_timeout);
3776         } else {
3777                 /* Flush error messages, if any. */
3778                 sio_timeouts_until_log = 1;
3779                 comwakeup((void *)NULL);
3780                 untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
3781         }
3782 }
3783
3784 static void
3785 comwakeup(chan)
3786         void    *chan;
3787 {
3788         struct com_s    *com;
3789         int             unit;
3790
3791         sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout);
3792
3793         /*
3794          * Recover from lost output interrupts.
3795          * Poll any lines that don't use interrupts.
3796          */
3797         for (unit = 0; unit < NSIOTOT; ++unit) {
3798                 com = com_addr(unit);
3799                 if (com != NULL && !com->gone
3800                     && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
3801                         disable_intr();
3802                         siointr1(com);
3803                         enable_intr();
3804                 }
3805         }
3806
3807         /*
3808          * Check for and log errors, but not too often.
3809          */
3810         if (--sio_timeouts_until_log > 0)
3811                 return;
3812         sio_timeouts_until_log = hz / sio_timeout;
3813         for (unit = 0; unit < NSIOTOT; ++unit) {
3814                 int     errnum;
3815
3816                 com = com_addr(unit);
3817                 if (com == NULL)
3818                         continue;
3819                 if (com->gone)
3820                         continue;
3821                 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
3822                         u_int   delta;
3823                         u_long  total;
3824
3825                         disable_intr();
3826                         delta = com->delta_error_counts[errnum];
3827                         com->delta_error_counts[errnum] = 0;
3828                         enable_intr();
3829                         if (delta == 0)
3830                                 continue;
3831                         total = com->error_counts[errnum] += delta;
3832                         log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n",
3833                             unit, delta, error_desc[errnum],
3834                             delta == 1 ? "" : "s", total);
3835                 }
3836         }
3837 }
3838
3839 #ifdef PC98
3840 /* commint is called when modem control line changes */
3841 static void
3842 commint(dev_t dev)
3843 {
3844         register struct tty *tp;
3845         int     stat,delta;
3846         struct com_s *com;
3847         int     mynor,unit;
3848
3849         mynor = minor(dev);
3850         unit = MINOR_TO_UNIT(mynor);
3851         com = com_addr(unit);
3852         tp = com->tp;
3853
3854         stat = com_tiocm_get(com);
3855         delta = com_tiocm_get_delta(com);
3856
3857         if (com->state & CS_CTS_OFLOW) {
3858                 if (stat & TIOCM_CTS)
3859                         com->state |= CS_ODEVREADY;
3860                 else
3861                         com->state &= ~CS_ODEVREADY;
3862         }
3863         if ((delta & TIOCM_CAR) && (mynor & CALLOUT_MASK) == 0) {
3864             if (stat & TIOCM_CAR )
3865                 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
3866             else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
3867                 /* negate DTR, RTS */
3868                 com_tiocm_bic(com, (tp->t_cflag & HUPCL) ?
3869                                 TIOCM_DTR|TIOCM_RTS|TIOCM_LE : TIOCM_LE );
3870                 /* disable IENABLE */
3871                 com_int_TxRx_disable( com );
3872             }
3873         }
3874 }
3875 #endif
3876
3877 static void
3878 disc_optim(tp, t, com)
3879         struct tty      *tp;
3880         struct termios  *t;
3881         struct com_s    *com;
3882 {
3883         if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
3884             && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
3885             && (!(t->c_iflag & PARMRK)
3886                 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
3887             && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
3888             && linesw[tp->t_line].l_rint == ttyinput)
3889                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
3890         else
3891                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
3892         com->hotchar = linesw[tp->t_line].l_hotchar;
3893 }
3894
3895 /*
3896  * Following are all routines needed for SIO to act as console
3897  */
3898 #include <machine/cons.h>
3899
3900 struct siocnstate {
3901         u_char  dlbl;
3902         u_char  dlbh;
3903         u_char  ier;
3904         u_char  cfcr;
3905         u_char  mcr;
3906 };
3907
3908 static speed_t siocngetspeed __P((Port_t, struct speedtab *));
3909 static void siocnclose  __P((struct siocnstate *sp, Port_t iobase));
3910 static void siocnopen   __P((struct siocnstate *sp, Port_t iobase, int speed));
3911 static void siocntxwait __P((Port_t iobase));
3912
3913 #ifdef __i386__
3914 /*
3915  * XXX: sciocnget() and sciocnputc() are not declared static, as they are
3916  * referred to from i386/i386/i386-gdbstub.c.
3917  */
3918 static cn_probe_t siocnprobe;
3919 static cn_init_t siocninit;
3920 static cn_checkc_t siocncheckc;
3921        cn_getc_t siocngetc;
3922        cn_putc_t siocnputc;
3923
3924 CONS_DRIVER(sio, siocnprobe, siocninit, siocngetc, siocncheckc, siocnputc);
3925
3926 #endif
3927
3928 static void
3929 siocntxwait(iobase)
3930         Port_t  iobase;
3931 {
3932         int     timo;
3933
3934         /*
3935          * Wait for any pending transmission to finish.  Required to avoid
3936          * the UART lockup bug when the speed is changed, and for normal
3937          * transmits.
3938          */
3939         timo = 100000;
3940         while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY))
3941                != (LSR_TSRE | LSR_TXRDY) && --timo != 0)
3942                 ;
3943 }
3944
3945 /*
3946  * Read the serial port specified and try to figure out what speed
3947  * it's currently running at.  We're assuming the serial port has
3948  * been initialized and is basicly idle.  This routine is only intended
3949  * to be run at system startup.
3950  *
3951  * If the value read from the serial port doesn't make sense, return 0.
3952  */
3953
3954 static speed_t
3955 siocngetspeed(iobase, table)
3956         Port_t iobase;
3957         struct speedtab *table;
3958 {
3959         int     code;
3960         u_char  dlbh;
3961         u_char  dlbl;
3962         u_char  cfcr;
3963
3964         cfcr = inb(iobase + com_cfcr);
3965         outb(iobase + com_cfcr, CFCR_DLAB | cfcr);
3966
3967         dlbl = inb(iobase + com_dlbl);
3968         dlbh = inb(iobase + com_dlbh);
3969
3970         outb(iobase + com_cfcr, cfcr);
3971
3972         code = dlbh << 8 | dlbl;
3973
3974         for ( ; table->sp_speed != -1; table++)
3975                 if (table->sp_code == code)
3976                         return (table->sp_speed);
3977
3978         return 0;       /* didn't match anything sane */
3979 }
3980
3981 static void
3982 siocnopen(sp, iobase, speed)
3983         struct siocnstate       *sp;
3984         Port_t                  iobase;
3985         int                     speed;
3986 {
3987         int     divisor;
3988         u_char  dlbh;
3989         u_char  dlbl;
3990
3991         /*
3992          * Save all the device control registers except the fifo register
3993          * and set our default ones (cs8 -parenb speed=comdefaultrate).
3994          * We can't save the fifo register since it is read-only.
3995          */
3996         sp->ier = inb(iobase + com_ier);
3997         outb(iobase + com_ier, 0);      /* spltty() doesn't stop siointr() */
3998         siocntxwait(iobase);
3999         sp->cfcr = inb(iobase + com_cfcr);
4000         outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
4001         sp->dlbl = inb(iobase + com_dlbl);
4002         sp->dlbh = inb(iobase + com_dlbh);
4003         /*
4004          * Only set the divisor registers if they would change, since on
4005          * some 16550 incompatibles (Startech), setting them clears the
4006          * data input register.  This also reduces the effects of the
4007          * UMC8669F bug.
4008          */
4009         divisor = ttspeedtab(speed, comspeedtab);
4010         dlbl = divisor & 0xFF;
4011         if (sp->dlbl != dlbl)
4012                 outb(iobase + com_dlbl, dlbl);
4013         dlbh = (u_int) divisor >> 8;
4014         if (sp->dlbh != dlbh)
4015                 outb(iobase + com_dlbh, dlbh);
4016         outb(iobase + com_cfcr, CFCR_8BITS);
4017         sp->mcr = inb(iobase + com_mcr);
4018         /*
4019          * We don't want interrupts, but must be careful not to "disable"
4020          * them by clearing the MCR_IENABLE bit, since that might cause
4021          * an interrupt by floating the IRQ line.
4022          */
4023         outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS);
4024 }
4025
4026 static void
4027 siocnclose(sp, iobase)
4028         struct siocnstate       *sp;
4029         Port_t                  iobase;
4030 {
4031         /*
4032          * Restore the device control registers.
4033          */
4034         siocntxwait(iobase);
4035         outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
4036         if (sp->dlbl != inb(iobase + com_dlbl))
4037                 outb(iobase + com_dlbl, sp->dlbl);
4038         if (sp->dlbh != inb(iobase + com_dlbh))
4039                 outb(iobase + com_dlbh, sp->dlbh);
4040         outb(iobase + com_cfcr, sp->cfcr);
4041         /*
4042          * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them.
4043          */
4044         outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS);
4045         outb(iobase + com_ier, sp->ier);
4046 }
4047
4048 #ifdef __i386__
4049 static
4050 #endif
4051 void
4052 siocnprobe(cp)
4053         struct consdev  *cp;
4054 {
4055         speed_t                 boot_speed;
4056         u_char                  cfcr;
4057         int                     s, unit;
4058         struct siocnstate       sp;
4059
4060         /*
4061          * Find our first enabled console, if any.  If it is a high-level
4062          * console device, then initialize it and return successfully.
4063          * If it is a low-level console device, then initialize it and
4064          * return unsuccessfully.  It must be initialized in both cases
4065          * for early use by console drivers and debuggers.  Initializing
4066          * the hardware is not necessary in all cases, since the i/o
4067          * routines initialize it on the fly, but it is necessary if
4068          * input might arrive while the hardware is switched back to an
4069          * uninitialized state.  We can't handle multiple console devices
4070          * yet because our low-level routines don't take a device arg.
4071          * We trust the user to set the console flags properly so that we
4072          * don't need to probe.
4073          */
4074         cp->cn_pri = CN_DEAD;
4075
4076         for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */
4077                 int flags;
4078                 if (resource_int_value("sio", unit, "flags", &flags))
4079                         continue;
4080                 if (COM_CONSOLE(flags)) {
4081                         int port;
4082                         if (resource_int_value("sio", unit, "port", &port))
4083                                 continue;
4084                         siocniobase = port;
4085                         s = spltty();
4086                         if (boothowto & RB_SERIAL) {
4087                                 boot_speed = siocngetspeed(siocniobase,
4088                                                            comspeedtab);
4089                                 if (boot_speed)
4090                                         comdefaultrate = boot_speed;
4091                         }
4092
4093                         /*
4094                          * Initialize the divisor latch.  We can't rely on
4095                          * siocnopen() to do this the first time, since it 
4096                          * avoids writing to the latch if the latch appears
4097                          * to have the correct value.  Also, if we didn't
4098                          * just read the speed from the hardware, then we
4099                          * need to set the speed in hardware so that
4100                          * switching it later is null.
4101                          */
4102                         cfcr = inb(siocniobase + com_cfcr);
4103                         outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr);
4104                         outb(siocniobase + com_dlbl,
4105                              COMBRD(comdefaultrate) & 0xff);
4106                         outb(siocniobase + com_dlbh,
4107                              (u_int) COMBRD(comdefaultrate) >> 8);
4108                         outb(siocniobase + com_cfcr, cfcr);
4109
4110                         siocnopen(&sp, siocniobase, comdefaultrate);
4111                         splx(s);
4112                         if (!COM_LLCONSOLE(flags)) {
4113                                 cp->cn_dev = makedev(CDEV_MAJOR, unit);
4114                                 cp->cn_pri = COM_FORCECONSOLE(flags)
4115                                              || boothowto & RB_SERIAL
4116                                              ? CN_REMOTE : CN_NORMAL;
4117                         }
4118                         break;
4119                 }
4120         }
4121 }
4122
4123 #ifdef __alpha__
4124
4125 struct consdev siocons = {
4126         NULL, NULL, siocngetc, siocncheckc, siocnputc,
4127         NULL, makedev(CDEV_MAJOR, 0), CN_NORMAL,
4128 };
4129
4130 extern struct consdev *cn_tab;
4131
4132 int
4133 siocnattach(port, speed)
4134         int port;
4135         int speed;
4136 {
4137         int                     s;
4138         u_char                  cfcr;
4139         struct siocnstate       sp;
4140
4141         siocniobase = port;
4142         comdefaultrate = speed;
4143
4144         s = spltty();
4145
4146         /*
4147          * Initialize the divisor latch.  We can't rely on
4148          * siocnopen() to do this the first time, since it 
4149          * avoids writing to the latch if the latch appears
4150          * to have the correct value.  Also, if we didn't
4151          * just read the speed from the hardware, then we
4152          * need to set the speed in hardware so that
4153          * switching it later is null.
4154          */
4155         cfcr = inb(siocniobase + com_cfcr);
4156         outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr);
4157         outb(siocniobase + com_dlbl,
4158              COMBRD(comdefaultrate) & 0xff);
4159         outb(siocniobase + com_dlbh,
4160              (u_int) COMBRD(comdefaultrate) >> 8);
4161         outb(siocniobase + com_cfcr, cfcr);
4162
4163         siocnopen(&sp, siocniobase, comdefaultrate);
4164         splx(s);
4165
4166         cn_tab = &siocons;
4167         return 0;
4168 }
4169
4170 int
4171 siogdbattach(port, speed)
4172         int port;
4173         int speed;
4174 {
4175         int                     s;
4176         u_char                  cfcr;
4177         struct siocnstate       sp;
4178
4179         siogdbiobase = port;
4180         gdbdefaultrate = speed;
4181
4182         s = spltty();
4183
4184         /*
4185          * Initialize the divisor latch.  We can't rely on
4186          * siocnopen() to do this the first time, since it 
4187          * avoids writing to the latch if the latch appears
4188          * to have the correct value.  Also, if we didn't
4189          * just read the speed from the hardware, then we
4190          * need to set the speed in hardware so that
4191          * switching it later is null.
4192          */
4193         cfcr = inb(siogdbiobase + com_cfcr);
4194         outb(siogdbiobase + com_cfcr, CFCR_DLAB | cfcr);
4195         outb(siogdbiobase + com_dlbl,
4196              COMBRD(gdbdefaultrate) & 0xff);
4197         outb(siogdbiobase + com_dlbh,
4198              (u_int) COMBRD(gdbdefaultrate) >> 8);
4199         outb(siogdbiobase + com_cfcr, cfcr);
4200
4201         siocnopen(&sp, siogdbiobase, gdbdefaultrate);
4202         splx(s);
4203
4204         return 0;
4205 }
4206
4207 #endif
4208
4209 #ifdef __i386__
4210 static
4211 #endif
4212 void
4213 siocninit(cp)
4214         struct consdev  *cp;
4215 {
4216         comconsole = DEV_TO_UNIT(cp->cn_dev);
4217 }
4218
4219 #ifdef __i386__
4220 static
4221 #endif
4222 int
4223 siocncheckc(dev)
4224         dev_t   dev;
4225 {
4226         int     c;
4227         Port_t  iobase;
4228         int     s;
4229         struct siocnstate       sp;
4230
4231         iobase = siocniobase;
4232         s = spltty();
4233         siocnopen(&sp, iobase, comdefaultrate);
4234         if (inb(iobase + com_lsr) & LSR_RXRDY)
4235                 c = inb(iobase + com_data);
4236         else
4237                 c = -1;
4238         siocnclose(&sp, iobase);
4239         splx(s);
4240         return (c);
4241 }
4242
4243
4244 int
4245 siocngetc(dev)
4246         dev_t   dev;
4247 {
4248         int     c;
4249         Port_t  iobase;
4250         int     s;
4251         struct siocnstate       sp;
4252
4253         iobase = siocniobase;
4254         s = spltty();
4255         siocnopen(&sp, iobase, comdefaultrate);
4256         while (!(inb(iobase + com_lsr) & LSR_RXRDY))
4257                 ;
4258         c = inb(iobase + com_data);
4259         siocnclose(&sp, iobase);
4260         splx(s);
4261         return (c);
4262 }
4263
4264 void
4265 siocnputc(dev, c)
4266         dev_t   dev;
4267         int     c;
4268 {
4269         int     s;
4270         struct siocnstate       sp;
4271
4272         s = spltty();
4273         siocnopen(&sp, siocniobase, comdefaultrate);
4274         siocntxwait(siocniobase);
4275         outb(siocniobase + com_data, c);
4276         siocnclose(&sp, siocniobase);
4277         splx(s);
4278 }
4279
4280 int
4281 siogdbgetc()
4282 {
4283         int     c;
4284         Port_t  iobase;
4285         int     s;
4286         struct siocnstate       sp;
4287
4288         iobase = siogdbiobase;
4289         s = spltty();
4290         siocnopen(&sp, iobase, gdbdefaultrate);
4291         while (!(inb(iobase + com_lsr) & LSR_RXRDY))
4292                 ;
4293         c = inb(iobase + com_data);
4294         siocnclose(&sp, iobase);
4295         splx(s);
4296         return (c);
4297 }
4298
4299 void
4300 siogdbputc(c)
4301         int     c;
4302 {
4303         int     s;
4304         struct siocnstate       sp;
4305
4306         s = spltty();
4307         siocnopen(&sp, siogdbiobase, gdbdefaultrate);
4308         siocntxwait(siogdbiobase);
4309         outb(siogdbiobase + com_data, c);
4310         siocnclose(&sp, siogdbiobase);
4311         splx(s);
4312 }
4313
4314
4315 /*
4316  * support PnP cards if we are using 'em
4317  */
4318
4319 #if NPNP > 0
4320
4321 static pnpid_t siopnp_ids[] = {
4322         { 0x5015f435, "MOT1550"},
4323         { 0x8113b04e, "Supra1381"},
4324         { 0x9012b04e, "Supra1290"},
4325         { 0x7121b04e, "SupraExpress 56i Sp"},
4326         { 0x11007256, "USR0011"},
4327         { 0x30207256, "USR2030"},
4328         { 0x31307256, "USR3031"},
4329         { 0x90307256, "USR3090"},
4330         { 0x0100440e, "Cardinal MVP288IV"},
4331         { 0 }
4332 };
4333
4334 static char *siopnp_probe(u_long csn, u_long vend_id);
4335 static void siopnp_attach(u_long csn, u_long vend_id, char *name,
4336         struct isa_device *dev);
4337 static u_long nsiopnp = NSIO;
4338
4339 static struct pnp_device siopnp = {
4340         "siopnp",
4341         siopnp_probe,
4342         siopnp_attach,
4343         &nsiopnp,
4344         &tty_imask
4345 };
4346 DATA_SET (pnpdevice_set, siopnp);
4347
4348 static char *
4349 siopnp_probe(u_long csn, u_long vend_id)
4350 {
4351         pnpid_t *id;
4352         char *s = NULL;
4353
4354         for(id = siopnp_ids; id->vend_id != 0; id++) {
4355                 if (vend_id == id->vend_id) {
4356                         s = id->id_str;
4357                         break;
4358                 }
4359         }
4360
4361         if (s) {
4362                 struct pnp_cinfo d;
4363                 read_pnp_parms(&d, 0);
4364                 if (d.enable == 0 || d.flags & 1) {
4365                         printf("CSN %lu is disabled.\n", csn);
4366                         return (NULL);
4367                 }
4368
4369         }
4370
4371         return (s);
4372 }
4373
4374 static void
4375 siopnp_attach(u_long csn, u_long vend_id, char *name, struct isa_device *dev)
4376 {
4377         struct pnp_cinfo d;
4378
4379         if (dev->id_unit >= NSIOTOT)
4380                 return;
4381
4382         if (read_pnp_parms(&d, 0) == 0) {
4383                 printf("failed to read pnp parms\n");
4384                 return;
4385         }
4386
4387         write_pnp_parms(&d, 0);
4388
4389         enable_pnp_card();
4390
4391         dev->id_iobase = d.port[0];
4392         dev->id_irq = (1 << d.irq[0]);
4393         dev->id_ointr = siointr;
4394         dev->id_ri_flags = RI_FAST;
4395         dev->id_drq = -1;
4396
4397         if (dev->id_driver == NULL) {
4398                 dev->id_driver = &siodriver;
4399                 dev->id_id = isa_compat_nextid();
4400         }
4401
4402         if ((dev->id_alive = sioprobe(dev)) != 0)
4403                 sioattach(dev);
4404         else
4405                 printf("sio%d: probe failed\n", dev->id_unit);
4406 }
4407 #endif
4408
4409 CDEV_DRIVER_MODULE(sio, isa, sio_driver, sio_devclass,
4410                    CDEV_MAJOR, sio_cdevsw, 0, 0);
4411
4412 #ifdef PC98
4413 /*
4414  *  pc98 local function
4415  */
4416
4417 static void
4418 com_tiocm_set(struct com_s *com, int msr)
4419 {
4420         int     s;
4421         int     tmp = 0;
4422         int     mask = CMD8251_TxEN|CMD8251_RxEN|CMD8251_DTR|CMD8251_RTS;
4423
4424         s=spltty();
4425         com->pc98_prev_modem_status = ( msr & (TIOCM_LE|TIOCM_DTR|TIOCM_RTS) )
4426            | ( com->pc98_prev_modem_status & ~(TIOCM_LE|TIOCM_DTR|TIOCM_RTS) );
4427         tmp |= (CMD8251_TxEN|CMD8251_RxEN);
4428         if ( msr & TIOCM_DTR ) tmp |= CMD8251_DTR;
4429         if ( msr & TIOCM_RTS ) tmp |= CMD8251_RTS;
4430         pc98_i8251_clear_or_cmd( com, mask, tmp );
4431         splx(s);
4432 }
4433
4434 static void
4435 com_tiocm_bis(struct com_s *com, int msr)
4436 {
4437         int     s;
4438         int     tmp = 0;
4439
4440         s=spltty();
4441         com->pc98_prev_modem_status |= ( msr & (TIOCM_LE|TIOCM_DTR|TIOCM_RTS) );
4442         tmp |= CMD8251_TxEN|CMD8251_RxEN;
4443         if ( msr & TIOCM_DTR ) tmp |= CMD8251_DTR;
4444         if ( msr & TIOCM_RTS ) tmp |= CMD8251_RTS;
4445
4446         pc98_i8251_or_cmd( com, tmp );
4447         splx(s);
4448 }
4449
4450 static void
4451 com_tiocm_bic(struct com_s *com, int msr)
4452 {
4453         int     s;
4454         int     tmp = msr;
4455
4456         s=spltty();
4457         com->pc98_prev_modem_status &= ~( msr & (TIOCM_LE|TIOCM_DTR|TIOCM_RTS) );
4458         if ( msr & TIOCM_DTR ) tmp |= CMD8251_DTR;
4459         if ( msr & TIOCM_RTS ) tmp |= CMD8251_RTS;
4460
4461         pc98_i8251_clear_cmd( com, tmp );
4462         splx(s);
4463 }
4464
4465 static int
4466 com_tiocm_get(struct com_s *com)
4467 {
4468         return( com->pc98_prev_modem_status );
4469 }
4470
4471 static int
4472 com_tiocm_get_delta(struct com_s *com)
4473 {
4474         int     tmp;
4475
4476         tmp = com->pc98_modem_delta;
4477         com->pc98_modem_delta = 0;
4478         return( tmp );
4479 }
4480
4481 /* convert to TIOCM_?? ( ioctl.h ) */
4482 static int
4483 pc98_get_modem_status(struct com_s *com)
4484 {
4485         int     stat, stat2;
4486         register int    msr;
4487
4488         stat  = inb(com->sts_port);
4489         stat2 = inb(com->in_modem_port);
4490         msr = com->pc98_prev_modem_status
4491                         & ~(TIOCM_CAR|TIOCM_RI|TIOCM_DSR|TIOCM_CTS);
4492         if ( !(stat2 & CICSCD_CD) ) msr |= TIOCM_CAR;
4493         if ( !(stat2 & CICSCD_CI) ) msr |= TIOCM_RI;
4494         if (   stat & STS8251_DSR ) msr |= TIOCM_DSR;
4495         if ( !(stat2 & CICSCD_CS) ) msr |= TIOCM_CTS;
4496 #if COM_CARRIER_DETECT_EMULATE
4497         if ( msr & (TIOCM_DSR|TIOCM_CTS) ) {
4498                 msr |= TIOCM_CAR;
4499         }
4500 #endif
4501         return(msr);
4502 }
4503
4504 static void
4505 pc98_check_msr(void* chan)
4506 {
4507         int     msr, delta;
4508         int     s;
4509         register struct tty *tp;
4510         struct  com_s *com;
4511         int     mynor;
4512         int     unit;
4513         dev_t   dev;
4514
4515         dev=(dev_t)chan;
4516         mynor = minor(dev);
4517         unit = MINOR_TO_UNIT(mynor);
4518         com = com_addr(unit);
4519         tp = com->tp;
4520
4521         s = spltty();
4522         msr = pc98_get_modem_status(com);
4523         /* make change flag */
4524         delta = msr ^ com->pc98_prev_modem_status;
4525         if ( delta & TIOCM_CAR ) {
4526             if ( com->modem_car_chg_timer ) {
4527                 if ( -- com->modem_car_chg_timer )
4528                     msr ^= TIOCM_CAR;
4529             } else {
4530                 if ((com->modem_car_chg_timer = (msr & TIOCM_CAR) ?
4531                      DCD_ON_RECOGNITION : DCD_OFF_TOLERANCE) != 0)
4532                     msr ^= TIOCM_CAR;
4533             }
4534         } else
4535             com->modem_car_chg_timer = 0;
4536         delta = ( msr ^ com->pc98_prev_modem_status ) &
4537                         (TIOCM_CAR|TIOCM_RI|TIOCM_DSR|TIOCM_CTS);
4538         com->pc98_prev_modem_status = msr;
4539         delta = ( com->pc98_modem_delta |= delta );
4540         splx(s);
4541         if ( com->modem_checking || (tp->t_state & (TS_ISOPEN)) ) {
4542                 if ( delta ) {
4543                         commint(dev);
4544                 }
4545                 timeout(pc98_check_msr, (caddr_t)dev,
4546                                         PC98_CHECK_MODEM_INTERVAL);
4547         } else {
4548                 com->modem_checking = 0;
4549         }
4550 }
4551
4552 static void
4553 pc98_msrint_start(dev_t dev)
4554 {
4555         struct  com_s *com;
4556         int     mynor;
4557         int     unit;
4558         int     s = spltty();
4559
4560         mynor = minor(dev);
4561         unit = MINOR_TO_UNIT(mynor);
4562         com = com_addr(unit);
4563         /* modem control line check routine envoke interval is 1/10 sec */
4564         if ( com->modem_checking == 0 ) {
4565                 com->pc98_prev_modem_status = pc98_get_modem_status(com);
4566                 com->pc98_modem_delta = 0;
4567                 timeout(pc98_check_msr, (caddr_t)dev,
4568                                         PC98_CHECK_MODEM_INTERVAL);
4569                 com->modem_checking = 1;
4570         }
4571         splx(s);
4572 }
4573
4574 static void
4575 pc98_disable_i8251_interrupt(struct com_s *com, int mod)
4576 {
4577         /* disable interrupt */
4578         register int    tmp;
4579
4580         mod |= ~(IEN_Tx|IEN_TxEMP|IEN_Rx);
4581         COM_INT_DISABLE
4582         tmp = inb( com->intr_ctrl_port ) & ~(IEN_Tx|IEN_TxEMP|IEN_Rx);
4583         outb( com->intr_ctrl_port, (com->intr_enable&=~mod) | tmp );
4584         COM_INT_ENABLE
4585 }
4586
4587 static void
4588 pc98_enable_i8251_interrupt(struct com_s *com, int mod)
4589 {
4590         register int    tmp;
4591
4592         COM_INT_DISABLE
4593         tmp = inb( com->intr_ctrl_port ) & ~(IEN_Tx|IEN_TxEMP|IEN_Rx);
4594         outb( com->intr_ctrl_port, (com->intr_enable|=mod) | tmp );
4595         COM_INT_ENABLE
4596 }
4597
4598 static int
4599 pc98_check_i8251_interrupt(struct com_s *com)
4600 {
4601         return ( com->intr_enable & 0x07 );
4602 }
4603
4604 static void
4605 pc98_i8251_clear_cmd(struct com_s *com, int x)
4606 {
4607         int     tmp;
4608
4609         COM_INT_DISABLE
4610         tmp = com->pc98_prev_siocmd & ~(x);
4611         outb(com->cmd_port, tmp);
4612         com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH);
4613         COM_INT_ENABLE
4614 }
4615
4616 static void
4617 pc98_i8251_or_cmd(struct com_s *com, int x)
4618 {
4619         int     tmp;
4620
4621         COM_INT_DISABLE
4622         tmp = com->pc98_prev_siocmd | (x);
4623         outb(com->cmd_port, tmp);
4624         com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH);
4625         COM_INT_ENABLE
4626 }
4627
4628 static void
4629 pc98_i8251_set_cmd(struct com_s *com, int x)
4630 {
4631         int     tmp;
4632
4633         COM_INT_DISABLE
4634         tmp = (x);
4635         outb(com->cmd_port, tmp);
4636         com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH);
4637         COM_INT_ENABLE
4638 }
4639
4640 static void
4641 pc98_i8251_clear_or_cmd(struct com_s *com, int clr, int x)
4642 {
4643         int     tmp;
4644         COM_INT_DISABLE
4645         tmp = com->pc98_prev_siocmd & ~(clr);
4646         tmp |= (x);
4647         outb(com->cmd_port, tmp);
4648         com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH);
4649         COM_INT_ENABLE
4650 }
4651
4652 static int
4653 pc98_i8251_get_cmd(struct com_s *com)
4654 {
4655         return com->pc98_prev_siocmd;
4656 }
4657
4658 static int
4659 pc98_i8251_get_mod(struct com_s *com)
4660 {
4661         return com->pc98_prev_siomod;
4662 }
4663
4664 static void
4665 pc98_i8251_reset(struct com_s *com, int mode, int command)
4666 {
4667         outb(com->cmd_port, 0); /* dummy */
4668         DELAY(2);
4669         outb(com->cmd_port, 0); /* dummy */
4670         DELAY(2);
4671         outb(com->cmd_port, 0); /* dummy */
4672         DELAY(2);
4673         outb(com->cmd_port, CMD8251_RESET);     /* internal reset */
4674         DELAY(2);
4675         outb(com->cmd_port, mode );     /* mode register */
4676         com->pc98_prev_siomod = mode;
4677         DELAY(2);
4678         pc98_i8251_set_cmd( com, (command|CMD8251_ER) );
4679 }
4680
4681 static void
4682 pc98_check_sysclock(void)
4683 {
4684         /* get system clock from port */
4685         if ( pc98_machine_type & M_8M ) {
4686         /* 8 MHz system & H98 */
4687                 sysclock = 8;
4688         } else {
4689         /* 5 MHz system */
4690                 sysclock = 5;
4691         }
4692 }
4693
4694 static void
4695 com_cflag_and_speed_set( struct com_s *com, int cflag, int speed)
4696 {
4697         int     cfcr=0, count;
4698         int     previnterrupt;
4699
4700         count = pc98_ttspeedtab( com, speed );
4701         if ( count < 0 ) return;
4702
4703         previnterrupt = pc98_check_i8251_interrupt(com);
4704         pc98_disable_i8251_interrupt( com, IEN_Tx|IEN_TxEMP|IEN_Rx );
4705
4706         switch ( cflag&CSIZE ) {
4707           case CS5:
4708                 cfcr = MOD8251_5BITS; break;
4709           case CS6:
4710                 cfcr = MOD8251_6BITS; break;
4711           case CS7:
4712                 cfcr = MOD8251_7BITS; break;
4713           case CS8:
4714                 cfcr = MOD8251_8BITS; break;
4715         }
4716         if ( cflag&PARENB ) {
4717             if ( cflag&PARODD )
4718                 cfcr |= MOD8251_PODD;
4719             else
4720                 cfcr |= MOD8251_PEVEN;
4721         } else
4722                 cfcr |= MOD8251_PDISAB;
4723
4724         if ( cflag&CSTOPB )
4725                 cfcr |= MOD8251_STOP2;
4726         else
4727                 cfcr |= MOD8251_STOP1;
4728
4729         if ( count & 0x10000 )
4730                 cfcr |= MOD8251_CLKX1;
4731         else
4732                 cfcr |= MOD8251_CLKX16;
4733
4734         if (epson_machine_id != 0x20) { /* XXX */
4735                 int     tmp;
4736                 while (!((tmp = inb(com->sts_port)) & STS8251_TxEMP))
4737                         ;
4738         }
4739         /* set baud rate from ospeed */
4740         pc98_set_baud_rate( com, count );
4741
4742         if ( cfcr != pc98_i8251_get_mod(com) )
4743                 pc98_i8251_reset(com, cfcr, pc98_i8251_get_cmd(com) );
4744
4745         pc98_enable_i8251_interrupt( com, previnterrupt );
4746 }
4747
4748 static int
4749 pc98_ttspeedtab(struct com_s *com, int speed)
4750 {
4751         int     if_type, effect_sp, count = -1, mod;
4752
4753         if_type = com->pc98_if_type & 0x0f;
4754
4755         switch (com->pc98_if_type) {
4756         case COM_IF_INTERNAL:
4757             if (PC98SIO_baud_rate_port(if_type) != -1) {
4758                 count = ttspeedtab(speed, if_8251_type[if_type].speedtab);
4759                 if (count > 0) {
4760                     count |= COM1_EXT_CLOCK;
4761                     break;
4762                 }
4763             }
4764
4765             /* for *1CLK asynchronous! mode, TEFUTEFU */
4766             mod = (sysclock == 5) ? 2457600 : 1996800;
4767             effect_sp = ttspeedtab( speed, pc98speedtab );
4768             if ( effect_sp < 0 )        /* XXX */
4769                 effect_sp = ttspeedtab( (speed - 1), pc98speedtab );
4770             if ( effect_sp <= 0 )
4771                 return effect_sp;
4772             if ( effect_sp == speed )
4773                 mod /= 16;
4774             if ( mod % effect_sp )
4775                 return(-1);
4776             count = mod / effect_sp;
4777             if ( count > 65535 )
4778                 return(-1);
4779             if ( effect_sp != speed )
4780                 count |= 0x10000;
4781             break;
4782         case COM_IF_PC9861K_1:
4783         case COM_IF_PC9861K_2:
4784             count = 1;
4785             break;
4786         case COM_IF_IND_SS_1:
4787         case COM_IF_IND_SS_2:
4788         case COM_IF_PIO9032B_1:
4789         case COM_IF_PIO9032B_2:
4790             if ( speed == 0 ) return 0;
4791             count = ttspeedtab( speed, if_8251_type[if_type].speedtab );
4792             break;
4793         case COM_IF_B98_01_1:
4794         case COM_IF_B98_01_2:
4795             if ( speed == 0 ) return 0;
4796             count = ttspeedtab( speed, if_8251_type[if_type].speedtab );
4797 #ifdef B98_01_OLD
4798             if (count == 0 || count == 1) {
4799                 count += 4;
4800                 count |= 0x20000;  /* x1 mode for 76800 and 153600 */
4801             }
4802 #endif
4803             break;
4804         }
4805
4806         return count;
4807 }
4808
4809 static void
4810 pc98_set_baud_rate( struct com_s *com, int count )
4811 {
4812         int     if_type, io, s;
4813
4814         if_type = com->pc98_if_type & 0x0f;
4815         io = com->iobase & 0xff00;
4816
4817         switch (com->pc98_if_type) {
4818         case COM_IF_INTERNAL:
4819             if (PC98SIO_baud_rate_port(if_type) != -1) {
4820                 if (count & COM1_EXT_CLOCK) {
4821                     outb((Port_t)PC98SIO_baud_rate_port(if_type), count & 0xff);
4822                     break;
4823                 } else {
4824                     outb((Port_t)PC98SIO_baud_rate_port(if_type), 0x09);
4825                 }
4826             }
4827
4828             if ( count < 0 ) {
4829                 printf( "[ Illegal count : %d ]", count );
4830                 return;
4831             } else if ( count == 0 )
4832                 return;
4833             /* set i8253 */
4834             s = splclock();
4835             if (count != 3)
4836                 outb( 0x77, 0xb6 );
4837             else
4838                 outb( 0x77, 0xb4 );
4839             outb( 0x5f, 0);
4840             outb( 0x75, count & 0xff );
4841             outb( 0x5f, 0);
4842             outb( 0x75, (count >> 8) & 0xff );
4843             splx(s);
4844             break;
4845         case COM_IF_IND_SS_1:
4846         case COM_IF_IND_SS_2:
4847             outb(io | PC98SIO_intr_ctrl_port(if_type), 0);
4848             outb(io | PC98SIO_baud_rate_port(if_type), 0);
4849             outb(io | PC98SIO_baud_rate_port(if_type), 0xc0);
4850             outb(io | PC98SIO_baud_rate_port(if_type), (count >> 8) | 0x80);
4851             outb(io | PC98SIO_baud_rate_port(if_type), count & 0xff);
4852             break;
4853         case COM_IF_PIO9032B_1:
4854         case COM_IF_PIO9032B_2:
4855             outb(io | PC98SIO_baud_rate_port(if_type), count);
4856             break;
4857         case COM_IF_B98_01_1:
4858         case COM_IF_B98_01_2:
4859             outb(io | PC98SIO_baud_rate_port(if_type), count & 0x0f);
4860 #ifdef B98_01_OLD
4861             /*
4862              * Some old B98_01 board should be controlled
4863              * in different way, but this hasn't been tested yet.
4864              */
4865             outb(io | PC98SIO_func_port(if_type),
4866                  (count & 0x20000) ? 0xf0 : 0xf2);
4867 #endif
4868             break;
4869         }
4870 }
4871 static int
4872 pc98_check_if_type(device_t dev, struct siodev *iod)
4873 {
4874         int     irr, io, if_type, tmp;
4875         static  short   irq_tab[2][8] = {
4876                 {  3,  5,  6,  9, 10, 12, 13, -1},
4877                 {  3, 10, 12, 13,  5,  6,  9, -1}
4878         };
4879
4880         iod->if_type = if_type = (isa_get_flags(dev) >> 24) & 0xff;
4881         if ((if_type < 0 || if_type > COM_IF_END1) &&
4882             (if_type < 0x10 || if_type > COM_IF_END2))
4883             return(-1);
4884         if_type &= 0x0f;
4885         iod->irq = 0;
4886         io = isa_get_port(dev) & 0xff00;
4887
4888         if (IS_8251(iod->if_type)) {
4889             if (PC98SIO_func_port(if_type) != -1) {
4890                 outb(io | PC98SIO_func_port(if_type), 0xf2);
4891                 tmp = ttspeedtab(9600, if_8251_type[if_type].speedtab);
4892                 if (tmp != -1 && PC98SIO_baud_rate_port(if_type) != -1)
4893                     outb(io | PC98SIO_baud_rate_port(if_type), tmp);
4894             }
4895
4896             iod->cmd  = io | PC98SIO_cmd_port(if_type);
4897             iod->sts  = io | PC98SIO_sts_port(if_type);
4898             iod->mod  = io | PC98SIO_in_modem_port(if_type);
4899             iod->ctrl = io | PC98SIO_intr_ctrl_port(if_type);
4900
4901             if (iod->if_type == COM_IF_INTERNAL) {
4902                 iod->irq = 4;
4903
4904                 /* XXX check new internal port. */
4905                 outb(0x138, 0);
4906                 DELAY(10);
4907                 for (tmp = 0; tmp < 100; tmp++) {
4908                     if ((inb(0x138) & 1) == 0) {
4909                         PC98SIO_baud_rate_port(if_type) = 0x13a;
4910                         if_8251_type[if_type].name = " (internal fast)";
4911                         if_8251_type[if_type].speedtab = pc98fast_speedtab;
4912                         break;
4913                     }
4914                     DELAY(1);
4915                 }
4916             } else {
4917                 tmp = inb( iod->mod ) & if_8251_type[if_type].irr_mask;
4918                 if ((isa_get_port(dev) & 0xff) == IO_COM2)
4919                     iod->irq = irq_tab[0][tmp];
4920                 else
4921                     iod->irq = irq_tab[1][tmp];
4922             }
4923         } else {
4924             irr = if_16550a_type[if_type].irr_read;
4925 #ifdef COM_MULTIPORT
4926             if (!COM_ISMULTIPORT(dev) || dev->id_unit == COM_MPMASTER(dev))
4927 #endif
4928             if (irr != -1) {
4929                 tmp = inb(io | irr);
4930                 if (isa_get_port(dev) & 0x01)   /* XXX depend on RSB-384 */
4931                     iod->irq = irq_tab[1][tmp >> 3];
4932                 else
4933                     iod->irq = irq_tab[0][tmp & 0x07];
4934             }
4935         }
4936         if ( iod->irq == -1 ) return -1;
4937
4938         return 0;
4939 }
4940 static int
4941 pc98_set_ioport( struct com_s *com, int id_flags )
4942 {
4943         int     io, if_type;
4944
4945         if_type = (id_flags >> 24) & 0xff;
4946         if (IS_8251(if_type)) {
4947             pc98_check_sysclock();
4948             io = com->iobase & 0xff00;
4949             com->pc98_if_type   = if_type;
4950             if_type &= 0x0f;
4951             com->data_port      = io | PC98SIO_data_port(if_type);
4952             com->cmd_port       = io | PC98SIO_cmd_port(if_type);
4953             com->sts_port       = io | PC98SIO_sts_port(if_type);
4954             com->in_modem_port  = io | PC98SIO_in_modem_port(if_type);
4955             com->intr_ctrl_port = io | PC98SIO_intr_ctrl_port(if_type);
4956             return 0;
4957         }
4958
4959         return -1;
4960 }
4961 #endif /* PC98 defined */