]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/cy.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / sys / i386 / isa / cy.c
1 /*-
2  * cyclades cyclom-y serial driver
3  *      Andrew Herbert <andrew@werple.apana.org.au>, 17 August 1993
4  *
5  * Copyright (c) 1993 Andrew Herbert.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name Andrew Herbert may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
22  * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32
33 #include "opt_compat.h"
34 #include "cy.h"
35
36 /*
37  * TODO:
38  * Atomic COR change.
39  * Consoles.
40  */
41
42 /*
43  * Temporary compile-time configuration options.
44  */
45 #define RxFifoThreshold (CD1400_RX_FIFO_SIZE / 2)
46                         /* Number of chars in the receiver FIFO before an
47                          * an interrupt is generated.  Should depend on
48                          * line speed.  Needs to be about 6 on a 486DX33
49                          * for 4 active ports at 115200 bps.  Why doesn't
50                          * 10 work?
51                          */
52 #define PollMode        /* Use polling-based irq service routine, not the
53                          * hardware svcack lines.  Must be defined for
54                          * Cyclom-16Y boards.  Less efficient for Cyclom-8Ys,
55                          * and stops 4 * 115200 bps from working.
56                          */
57 #undef  Smarts          /* Enable slightly more CD1400 intelligence.  Mainly
58                          * the output CR/LF processing, plus we can avoid a
59                          * few checks usually done in ttyinput().
60                          *
61                          * XXX not fully implemented, and not particularly
62                          * worthwhile.
63                          */
64 #undef  CyDebug         /* Include debugging code (not very expensive). */
65
66 /* These will go away. */
67 #undef  SOFT_CTS_OFLOW
68 #define SOFT_HOTCHAR
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/tty.h>
73 #include <sys/proc.h>
74 #include <sys/conf.h>
75 #include <sys/dkstat.h>
76 #include <sys/fcntl.h>
77 #include <sys/interrupt.h>
78 #include <sys/kernel.h>
79 #include <sys/malloc.h>
80 #include <sys/syslog.h>
81 #include <machine/clock.h>
82 #include <machine/ipl.h>
83 #ifndef SMP
84 #include <machine/lock.h>
85 #endif
86 #include <machine/psl.h>
87
88 #include <i386/isa/isa_device.h>
89 #include <i386/isa/cyreg.h>
90 #include <i386/isa/ic/cd1400.h>
91
92 #ifdef SMP
93 #define disable_intr()  COM_DISABLE_INTR()
94 #define enable_intr()   COM_ENABLE_INTR()
95 #endif /* SMP */
96
97 /*
98  * Dictionary so that I can name everything *sio* or *com* to compare with
99  * sio.c.  There is also lots of ugly formatting and unnecessary ifdefs to
100  * simplify the comparision.  These will go away.
101  */
102 #define LSR_BI          CD1400_RDSR_BREAK
103 #define LSR_FE          CD1400_RDSR_FE
104 #define LSR_OE          CD1400_RDSR_OE
105 #define LSR_PE          CD1400_RDSR_PE
106 #define MCR_DTR         CD1400_MSVR2_DTR
107 #define MCR_RTS         CD1400_MSVR1_RTS
108 #define MSR_CTS         CD1400_MSVR2_CTS
109 #define MSR_DCD         CD1400_MSVR2_CD
110 #define MSR_DSR         CD1400_MSVR2_DSR
111 #define MSR_RI          CD1400_MSVR2_RI
112 #define NSIO            (NCY * CY_MAX_PORTS)
113 #define comconsole      cyconsole
114 #define comdefaultrate  cydefaultrate
115 #define com_events      cy_events
116 #define comhardclose    cyhardclose
117 #define commctl         cymctl
118 #define comparam        cyparam
119 #define comspeed        cyspeed
120 #define comstart        cystart
121 #define comwakeup       cywakeup
122 #define nsio_tty        ncy_tty
123 #define p_com_addr      p_cy_addr
124 #define sioattach       cyattach
125 #define sioclose        cyclose
126 #define siodriver       cydriver
127 #define siodtrwakeup    cydtrwakeup
128 #define sioinput        cyinput
129 #define siointr         cyintr
130 #define siointr1        cyintr1
131 #define sioioctl        cyioctl
132 #define sioopen         cyopen
133 #define siopoll         cypoll
134 #define sioprobe        cyprobe
135 #define siosettimeout   cysettimeout
136 #define siosetwater     cysetwater
137 #define comstop         cystop
138 #define siowrite        cywrite
139 #define sio_registered  cy_registered
140 #define sio_timeout     cy_timeout
141 #define sio_timeout_handle cy_timeout_handle
142 #define sio_timeouts_until_log  cy_timeouts_until_log
143 #define sio_tty         cy_tty
144
145 #define CY_MAX_PORTS            (CD1400_NO_OF_CHANNELS * CY_MAX_CD1400s)
146
147 /* We encode the cyclom unit number (cyu) in spare bits in the IVR's. */
148 #define CD1400_xIVR_CHAN_SHIFT  3
149 #define CD1400_xIVR_CHAN        0x1F
150
151 /*
152  * ETC states.  com->etc may also contain a hardware ETC command value,
153  * meaning that execution of that command is pending.
154  */
155 #define ETC_NONE                0       /* we depend on bzero() setting this */
156 #define ETC_BREAK_STARTING      1
157 #define ETC_BREAK_STARTED       2
158 #define ETC_BREAK_ENDING        3
159 #define ETC_BREAK_ENDED         4
160
161 #define LOTS_OF_EVENTS  64      /* helps separate urgent events from input */
162
163 #define CALLOUT_MASK            0x80
164 #define CONTROL_MASK            0x60
165 #define CONTROL_INIT_STATE      0x20
166 #define CONTROL_LOCK_STATE      0x40
167 #define DEV_TO_UNIT(dev)        (MINOR_TO_UNIT(minor(dev)))
168 #define MINOR_MAGIC_MASK        (CALLOUT_MASK | CONTROL_MASK)
169 #define MINOR_TO_UNIT(mynor)    (((mynor) >> 16) * CY_MAX_PORTS \
170                                  | (((mynor) & 0xff) & ~MINOR_MAGIC_MASK))
171
172 /*
173  * com state bits.
174  * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
175  * than the other bits so that they can be tested as a group without masking
176  * off the low bits.
177  *
178  * The following com and tty flags correspond closely:
179  *      CS_BUSY         = TS_BUSY (maintained by comstart(), siopoll() and
180  *                                 comstop())
181  *      CS_TTGO         = ~TS_TTSTOP (maintained by comparam() and comstart())
182  *      CS_CTS_OFLOW    = CCTS_OFLOW (maintained by comparam())
183  *      CS_RTS_IFLOW    = CRTS_IFLOW (maintained by comparam())
184  * TS_FLUSH is not used.
185  * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
186  * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
187  */
188 #define CS_BUSY         0x80    /* output in progress */
189 #define CS_TTGO         0x40    /* output not stopped by XOFF */
190 #define CS_ODEVREADY    0x20    /* external device h/w ready (CTS) */
191 #define CS_CHECKMSR     1       /* check of MSR scheduled */
192 #define CS_CTS_OFLOW    2       /* use CTS output flow control */
193 #define CS_DTR_OFF      0x10    /* DTR held off */
194 #define CS_ODONE        4       /* output completed */
195 #define CS_RTS_IFLOW    8       /* use RTS input flow control */
196 #define CSE_ODONE       1       /* output transmitted */
197
198 static  char const * const      error_desc[] = {
199 #define CE_OVERRUN                      0
200         "silo overflow",
201 #define CE_INTERRUPT_BUF_OVERFLOW       1
202         "interrupt-level buffer overflow",
203 #define CE_TTY_BUF_OVERFLOW             2
204         "tty-level buffer overflow",
205 };
206
207 #define CE_NTYPES                       3
208 #define CE_RECORD(com, errnum)          (++(com)->delta_error_counts[errnum])
209
210 /* types.  XXX - should be elsewhere */
211 typedef u_char  bool_t;         /* boolean */
212 typedef u_char volatile *cy_addr;
213
214 /* queue of linear buffers */
215 struct lbq {
216         u_char  *l_head;        /* next char to process */
217         u_char  *l_tail;        /* one past the last char to process */
218         struct lbq *l_next;     /* next in queue */
219         bool_t  l_queued;       /* nonzero if queued */
220 };
221
222 /* com device structure */
223 struct com_s {
224         u_char  state;          /* miscellaneous flag bits */
225         bool_t  active_out;     /* nonzero if the callout device is open */
226 #if 0
227         u_char  cfcr_image;     /* copy of value written to CFCR */
228 #endif
229         u_char  etc;            /* pending Embedded Transmit Command */
230         u_char  extra_state;    /* more flag bits, separate for order trick */
231 #if 0
232         u_char  fifo_image;     /* copy of value written to FIFO */
233 #endif
234         u_char  gfrcr_image;    /* copy of value read from GFRCR */
235 #if 0
236         bool_t  hasfifo;        /* nonzero for 16550 UARTs */
237         bool_t  loses_outints;  /* nonzero if device loses output interrupts */
238 #endif
239         u_char  mcr_dtr;        /* MCR bit that is wired to DTR */
240         u_char  mcr_image;      /* copy of value written to MCR */
241         u_char  mcr_rts;        /* MCR bit that is wired to RTS */
242 #if 0
243 #ifdef COM_MULTIPORT
244         bool_t  multiport;      /* is this unit part of a multiport device? */
245 #endif /* COM_MULTIPORT */
246         bool_t  no_irq;         /* nonzero if irq is not attached */
247         bool_t  poll;           /* nonzero if polling is required */
248         bool_t  poll_output;    /* nonzero if polling for output is required */
249 #endif
250         int     unit;           /* unit number */
251         int     dtr_wait;       /* time to hold DTR down on close (* 1/hz) */
252 #if 0
253         u_int   tx_fifo_size;
254 #endif
255         u_int   wopeners;       /* # processes waiting for DCD in open() */
256
257         /*
258          * The high level of the driver never reads status registers directly
259          * because there would be too many side effects to handle conveniently.
260          * Instead, it reads copies of the registers stored here by the
261          * interrupt handler.
262          */
263         u_char  last_modem_status;      /* last MSR read by intr handler */
264         u_char  prev_modem_status;      /* last MSR handled by high level */
265
266         u_char  hotchar;        /* ldisc-specific char to be handled ASAP */
267         u_char  *ibuf;          /* start of input buffer */
268         u_char  *ibufend;       /* end of input buffer */
269         u_char  *ibufold;       /* old input buffer, to be freed */
270         u_char  *ihighwater;    /* threshold in input buffer */
271         u_char  *iptr;          /* next free spot in input buffer */
272         int     ibufsize;       /* size of ibuf (not include error bytes) */
273         int     ierroff;        /* offset of error bytes in ibuf */
274
275         struct lbq      obufq;  /* head of queue of output buffers */
276         struct lbq      obufs[2];       /* output buffers */
277
278         int     cy_align;       /* index for register alignment */
279         cy_addr cy_iobase;      /* base address of this port's cyclom */
280         cy_addr iobase;         /* base address of this port's cd1400 */
281         int     mcr_rts_reg;    /* cd1400 reg number of reg holding mcr_rts */
282
283         struct tty      *tp;    /* cross reference */
284
285         /* Initial state. */
286         struct termios  it_in;  /* should be in struct tty */
287         struct termios  it_out;
288
289         /* Lock state. */
290         struct termios  lt_in;  /* should be in struct tty */
291         struct termios  lt_out;
292
293         bool_t  do_timestamp;
294         bool_t  do_dcd_timestamp;
295         struct timeval  timestamp;
296         struct timeval  dcd_timestamp;
297
298         u_long  bytes_in;       /* statistics */
299         u_long  bytes_out;
300         u_int   delta_error_counts[CE_NTYPES];
301         u_long  error_counts[CE_NTYPES];
302
303         u_int   recv_exception; /* exception chars received */
304         u_int   mdm;            /* modem signal changes */
305 #ifdef CyDebug
306         u_int   start_count;    /* no. of calls to comstart() */
307         u_int   start_real;     /* no. of calls that did something */
308 #endif
309         u_char  car;            /* CD1400 CAR shadow (if first unit in cd) */
310         u_char  channel_control;/* CD1400 CCR control command shadow */
311         u_char  cor[3];         /* CD1400 COR1-3 shadows */
312         u_char  intr_enable;    /* CD1400 SRER shadow */
313
314         /*
315          * Data area for output buffers.  Someday we should build the output
316          * buffer queue without copying data.
317          */
318         u_char  obuf1[256];
319         u_char  obuf2[256];
320 };
321
322 /* PCI driver entry point. */
323 int     cyattach_common         __P((cy_addr cy_iobase, int cy_align));
324 ointhand2_t     siointr;
325
326 static  int     cy_units        __P((cy_addr cy_iobase, int cy_align));
327 static  int     sioattach       __P((struct isa_device *dev));
328 static  void    cd1400_channel_cmd __P((struct com_s *com, int cmd));
329 static  void    cd1400_channel_cmd_wait __P((struct com_s *com));
330 static  void    cd_etc          __P((struct com_s *com, int etc));
331 static  int     cd_getreg       __P((struct com_s *com, int reg));
332 static  void    cd_setreg       __P((struct com_s *com, int reg, int val));
333 static  timeout_t siodtrwakeup;
334 static  void    comhardclose    __P((struct com_s *com));
335 static  void    sioinput        __P((struct com_s *com));
336 #if 0
337 static  void    siointr1        __P((struct com_s *com));
338 #endif
339 static  int     commctl         __P((struct com_s *com, int bits, int how));
340 static  int     comparam        __P((struct tty *tp, struct termios *t));
341 static  swihand_t siopoll;
342 static  int     sioprobe        __P((struct isa_device *dev));
343 static  void    siosettimeout   __P((void));
344 static  int     siosetwater     __P((struct com_s *com, speed_t speed));
345 static  int     comspeed        __P((speed_t speed, u_long cy_clock,
346                                      int *prescaler_io));
347 static  void    comstart        __P((struct tty *tp));
348 static  void    comstop         __P((struct tty *tp, int rw));
349 static  timeout_t comwakeup;
350 static  void    disc_optim      __P((struct tty *tp, struct termios *t,
351                                      struct com_s *com));
352
353 #ifdef CyDebug
354 void    cystatus        __P((int unit));
355 #endif
356
357 static char driver_name[] = "cy";
358
359 /* table and macro for fast conversion from a unit number to its com struct */
360 static  struct com_s    *p_com_addr[NSIO];
361 #define com_addr(unit)  (p_com_addr[unit])
362
363 struct isa_driver       siodriver = {
364         sioprobe, sioattach, driver_name
365 };
366
367 static  d_open_t        sioopen;
368 static  d_close_t       sioclose;
369 static  d_write_t       siowrite;
370 static  d_ioctl_t       sioioctl;
371
372 #define CDEV_MAJOR      48
373 static struct cdevsw sio_cdevsw = {
374         /* open */      sioopen,
375         /* close */     sioclose,
376         /* read */      ttyread,
377         /* write */     siowrite,
378         /* ioctl */     sioioctl,
379         /* poll */      ttypoll,
380         /* mmap */      nommap,
381         /* strategy */  nostrategy,
382         /* name */      driver_name,
383         /* maj */       CDEV_MAJOR,
384         /* dump */      nodump,
385         /* psize */     nopsize,
386         /* flags */     D_TTY,
387         /* bmaj */      -1
388 };
389
390 static  int     comconsole = -1;
391 static  speed_t comdefaultrate = TTYDEF_SPEED;
392 static  u_int   com_events;     /* input chars + weighted output completions */
393 static  bool_t  sio_registered;
394 static  int     sio_timeout;
395 static  int     sio_timeouts_until_log;
396 static  struct  callout_handle sio_timeout_handle
397     = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle);
398 #if 0 /* XXX */
399 static struct tty       *sio_tty[NSIO];
400 #else
401 static struct tty       sio_tty[NSIO];
402 #endif
403 static  const int       nsio_tty = NSIO;
404
405 #ifdef CyDebug
406 static  u_int   cd_inbs;
407 static  u_int   cy_inbs;
408 static  u_int   cd_outbs;
409 static  u_int   cy_outbs;
410 static  u_int   cy_svrr_probes;
411 static  u_int   cy_timeouts;
412 #endif
413
414 static  int     cy_chip_offset[] = {
415         0x0000, 0x0400, 0x0800, 0x0c00, 0x0200, 0x0600, 0x0a00, 0x0e00,
416 };
417 static  int     cy_nr_cd1400s[NCY];
418 static  int     cy_total_devices;
419 #undef  RxFifoThreshold
420 static  int     volatile RxFifoThreshold = (CD1400_RX_FIFO_SIZE / 2);
421
422 static int
423 sioprobe(dev)
424         struct isa_device       *dev;
425 {
426         cy_addr iobase;
427
428         iobase = (cy_addr)dev->id_maddr;
429
430         /* Cyclom-16Y hardware reset (Cyclom-8Ys don't care) */
431         cy_inb(iobase, CY16_RESET, 0);  /* XXX? */
432         DELAY(500);     /* wait for the board to get its act together */
433
434         /* this is needed to get the board out of reset */
435         cy_outb(iobase, CY_CLEAR_INTR, 0, 0);
436         DELAY(500);
437
438         return (cy_units(iobase, 0) == 0 ? 0 : -1);
439 }
440
441 static int
442 cy_units(cy_iobase, cy_align)
443         cy_addr cy_iobase;
444         int     cy_align;
445 {
446         int     cyu;
447         u_char  firmware_version;
448         int     i;
449         cy_addr iobase;
450
451         for (cyu = 0; cyu < CY_MAX_CD1400s; ++cyu) {
452                 iobase = cy_iobase + (cy_chip_offset[cyu] << cy_align);
453
454                 /* wait for chip to become ready for new command */
455                 for (i = 0; i < 10; i++) {
456                         DELAY(50);
457                         if (!cd_inb(iobase, CD1400_CCR, cy_align))
458                                 break;
459                 }
460
461                 /* clear the GFRCR register */
462                 cd_outb(iobase, CD1400_GFRCR, cy_align, 0);
463
464                 /* issue a reset command */
465                 cd_outb(iobase, CD1400_CCR, cy_align,
466                         CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
467
468                 /* wait for the CD1400 to initialize itself */
469                 for (i = 0; i < 200; i++) {
470                         DELAY(50);
471
472                         /* retrieve firmware version */
473                         firmware_version = cd_inb(iobase, CD1400_GFRCR,
474                                                   cy_align);
475                         if ((firmware_version & 0xf0) == 0x40)
476                                 break;
477                 }
478
479                 /*
480                  * Anything in the 0x40-0x4F range is fine.
481                  * If one CD1400 is bad then we don't support higher
482                  * numbered good ones on this board.
483                  */
484                 if ((firmware_version & 0xf0) != 0x40)
485                         break;
486         }
487         return (cyu);
488 }
489
490 static int
491 sioattach(isdp)
492         struct isa_device       *isdp;
493 {
494         int     adapter;
495
496         adapter = cyattach_common((cy_addr) isdp->id_maddr, 0);
497         if (adapter < 0)
498                 return (0);
499
500         /*
501          * XXX
502          * This kludge is to allow ISA/PCI device specifications in the
503          * kernel config file to be in any order.
504          */
505         if (isdp->id_unit != adapter) {
506                 printf("cy%d: attached as cy%d\n", isdp->id_unit, adapter);
507                 isdp->id_unit = adapter;        /* XXX */
508         }
509         isdp->id_ointr = siointr;
510         /* isdp->id_ri_flags |= RI_FAST; XXX unimplemented - use newbus! */
511         return (1);
512 }
513
514 int
515 cyattach_common(cy_iobase, cy_align)
516         cy_addr cy_iobase;
517         int     cy_align;
518 {
519         int     adapter;
520         int     cyu;
521         u_char  firmware_version;
522         cy_addr iobase;
523         int     ncyu;
524         int     unit;
525
526         adapter = cy_total_devices;
527         if ((u_int)adapter >= NCY) {
528                 printf(
529         "cy%d: can't attach adapter: insufficient cy devices configured\n",
530                        adapter);
531                 return (-1);
532         }
533         ncyu = cy_units(cy_iobase, cy_align);
534         if (ncyu == 0)
535                 return (-1);
536         cy_nr_cd1400s[adapter] = ncyu;
537         cy_total_devices++;
538
539         unit = adapter * CY_MAX_PORTS;
540         for (cyu = 0; cyu < ncyu; ++cyu) {
541                 int     cdu;
542
543                 iobase = (cy_addr) (cy_iobase
544                                     + (cy_chip_offset[cyu] << cy_align));
545                 firmware_version = cd_inb(iobase, CD1400_GFRCR, cy_align);
546
547                 /* Set up a receive timeout period of than 1+ ms. */
548                 cd_outb(iobase, CD1400_PPR, cy_align,
549                         howmany(CY_CLOCK(firmware_version)
550                                 / CD1400_PPR_PRESCALER, 1000));
551
552                 for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; ++cdu, ++unit) {
553                         struct com_s    *com;
554                         int             s;
555
556         com = malloc(sizeof *com, M_DEVBUF, M_NOWAIT);
557         if (com == NULL)
558                 break;
559         bzero(com, sizeof *com);
560         com->unit = unit;
561                         com->gfrcr_image = firmware_version;
562                         if (CY_RTS_DTR_SWAPPED(firmware_version)) {
563                                 com->mcr_dtr = MCR_RTS;
564                                 com->mcr_rts = MCR_DTR;
565                                 com->mcr_rts_reg = CD1400_MSVR2;
566                         } else {
567                                 com->mcr_dtr = MCR_DTR;
568                                 com->mcr_rts = MCR_RTS;
569                                 com->mcr_rts_reg = CD1400_MSVR1;
570                         }
571         com->dtr_wait = 3 * hz;
572         com->obufs[0].l_head = com->obuf1;
573         com->obufs[1].l_head = com->obuf2;
574
575                         com->cy_align = cy_align;
576                         com->cy_iobase = cy_iobase;
577         com->iobase = iobase;
578                         com->car = ~CD1400_CAR_CHAN;
579
580         /*
581          * We don't use all the flags from <sys/ttydefaults.h> since they
582          * are only relevant for logins.  It's important to have echo off
583          * initially so that the line doesn't start blathering before the
584          * echo flag can be turned off.
585          */
586         com->it_in.c_iflag = 0;
587         com->it_in.c_oflag = 0;
588         com->it_in.c_cflag = TTYDEF_CFLAG;
589         com->it_in.c_lflag = 0;
590         if (unit == comconsole) {
591                 com->it_in.c_iflag = TTYDEF_IFLAG;
592                 com->it_in.c_oflag = TTYDEF_OFLAG;
593                 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
594                 com->it_in.c_lflag = TTYDEF_LFLAG;
595                 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
596         }
597         if (siosetwater(com, com->it_in.c_ispeed) != 0) {
598                 enable_intr();
599                 free(com, M_DEVBUF);
600                 return (0);
601         }
602         enable_intr();
603         termioschars(&com->it_in);
604         com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
605         com->it_out = com->it_in;
606
607         s = spltty();
608         com_addr(unit) = com;
609         splx(s);
610
611         if (!sio_registered) {
612                 cdevsw_add(&sio_cdevsw);
613                 register_swi(SWI_TTY, siopoll);
614                 sio_registered = TRUE;
615         }
616         make_dev(&sio_cdevsw, unit,
617                 UID_ROOT, GID_WHEEL, 0600, "ttyc%r%r", adapter,
618                 unit % CY_MAX_PORTS);
619         make_dev(&sio_cdevsw, unit | CONTROL_INIT_STATE,
620                 UID_ROOT, GID_WHEEL, 0600, "ttyic%r%r", adapter,
621                 unit % CY_MAX_PORTS);
622         make_dev(&sio_cdevsw, unit | CONTROL_LOCK_STATE,
623                 UID_ROOT, GID_WHEEL, 0600, "ttylc%r%r", adapter,
624                 unit % CY_MAX_PORTS);
625         make_dev(&sio_cdevsw, unit | CALLOUT_MASK,
626                 UID_UUCP, GID_DIALER, 0660, "cuac%r%r", adapter,
627                 unit % CY_MAX_PORTS);
628         make_dev(&sio_cdevsw, unit | CALLOUT_MASK | CONTROL_INIT_STATE,
629                 UID_UUCP, GID_DIALER, 0660, "cuaic%r%r", adapter,
630                 unit % CY_MAX_PORTS);
631         make_dev(&sio_cdevsw, unit | CALLOUT_MASK | CONTROL_LOCK_STATE,
632                 UID_UUCP, GID_DIALER, 0660, "cualc%r%r", adapter,
633                 unit % CY_MAX_PORTS);
634                 }
635         }
636
637         /* ensure an edge for the next interrupt */
638         cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
639
640         return (adapter);
641 }
642
643 static int
644 sioopen(dev, flag, mode, p)
645         dev_t           dev;
646         int             flag;
647         int             mode;
648         struct proc     *p;
649 {
650         struct com_s    *com;
651         int             error;
652         int             mynor;
653         int             s;
654         struct tty      *tp;
655         int             unit;
656
657         mynor = minor(dev);
658         unit = MINOR_TO_UNIT(mynor);
659         if ((u_int) unit >= NSIO || (com = com_addr(unit)) == NULL)
660                 return (ENXIO);
661         if (mynor & CONTROL_MASK)
662                 return (0);
663 #if 0 /* XXX */
664         tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
665 #else
666         tp = com->tp = &sio_tty[unit];
667 #endif
668         dev->si_tty = tp;
669         s = spltty();
670         /*
671          * We jump to this label after all non-interrupted sleeps to pick
672          * up any changes of the device state.
673          */
674 open_top:
675         while (com->state & CS_DTR_OFF) {
676                 error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "cydtr", 0);
677                 if (error != 0)
678                         goto out;
679         }
680         if (tp->t_state & TS_ISOPEN) {
681                 /*
682                  * The device is open, so everything has been initialized.
683                  * Handle conflicts.
684                  */
685                 if (mynor & CALLOUT_MASK) {
686                         if (!com->active_out) {
687                                 error = EBUSY;
688                                 goto out;
689                         }
690                 } else {
691                         if (com->active_out) {
692                                 if (flag & O_NONBLOCK) {
693                                         error = EBUSY;
694                                         goto out;
695                                 }
696                                 error = tsleep(&com->active_out,
697                                                TTIPRI | PCATCH, "cybi", 0);
698                                 if (error != 0)
699                                         goto out;
700                                 goto open_top;
701                         }
702                 }
703                 if (tp->t_state & TS_XCLUDE &&
704                     suser(p)) {
705                         error = EBUSY;
706                         goto out;
707                 }
708         } else {
709                 /*
710                  * The device isn't open, so there are no conflicts.
711                  * Initialize it.  Initialization is done twice in many
712                  * cases: to preempt sleeping callin opens if we are
713                  * callout, and to complete a callin open after DCD rises.
714                  */
715                 tp->t_oproc = comstart;
716                 tp->t_stop = comstop;
717                 tp->t_param = comparam;
718                 tp->t_dev = dev;
719                 tp->t_termios = mynor & CALLOUT_MASK
720                                 ? com->it_out : com->it_in;
721
722                 /* Encode per-board unit in LIVR for access in intr routines. */
723                 cd_setreg(com, CD1400_LIVR,
724                           (unit & CD1400_xIVR_CHAN) << CD1400_xIVR_CHAN_SHIFT);
725
726                 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
727 #if 0
728                 com->poll = com->no_irq;
729                 com->poll_output = com->loses_outints;
730 #endif
731                 ++com->wopeners;
732                 error = comparam(tp, &tp->t_termios);
733                 --com->wopeners;
734                 if (error != 0)
735                         goto out;
736 #if 0
737                 if (com->hasfifo) {
738                         /*
739                          * (Re)enable and flush fifos.
740                          *
741                          * Certain SMC chips cause problems if the fifos
742                          * are enabled while input is ready.  Turn off the
743                          * fifo if necessary to clear the input.  We test
744                          * the input ready bit after enabling the fifos
745                          * since we've already enabled them in comparam()
746                          * and to handle races between enabling and fresh
747                          * input.
748                          */
749                         while (TRUE) {
750                                 outb(iobase + com_fifo,
751                                      FIFO_RCV_RST | FIFO_XMT_RST
752                                      | com->fifo_image);
753                                 DELAY(100);
754                                 if (!(inb(com->line_status_port) & LSR_RXRDY))
755                                         break;
756                                 outb(iobase + com_fifo, 0);
757                                 DELAY(100);
758                                 (void) inb(com->data_port);
759                         }
760                 }
761
762                 disable_intr();
763                 (void) inb(com->line_status_port);
764                 (void) inb(com->data_port);
765                 com->prev_modem_status = com->last_modem_status
766                     = inb(com->modem_status_port);
767                 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS
768                                        | IER_EMSC);
769                 enable_intr();
770 #else /* !0 */
771                 /*
772                  * Flush fifos.  This requires a full channel reset which
773                  * also disables the transmitter and receiver.  Recover
774                  * from this.
775                  */
776                 cd1400_channel_cmd(com,
777                                    CD1400_CCR_CMDRESET | CD1400_CCR_CHANRESET);
778                 cd1400_channel_cmd(com, com->channel_control);
779
780                 disable_intr();
781                 com->prev_modem_status = com->last_modem_status
782                     = cd_getreg(com, CD1400_MSVR2);
783                 cd_setreg(com, CD1400_SRER,
784                           com->intr_enable
785                           = CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
786                 enable_intr();
787 #endif /* 0 */
788                 /*
789                  * Handle initial DCD.  Callout devices get a fake initial
790                  * DCD (trapdoor DCD).  If we are callout, then any sleeping
791                  * callin opens get woken up and resume sleeping on "cybi"
792                  * instead of "cydcd".
793                  */
794                 /*
795                  * XXX `mynor & CALLOUT_MASK' should be
796                  * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
797                  * TRAPDOOR_CARRIER is the default initial state for callout
798                  * devices and SOFT_CARRIER is like CLOCAL except it hides
799                  * the true carrier.
800                  */
801                 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
802                         (*linesw[tp->t_line].l_modem)(tp, 1);
803         }
804         /*
805          * Wait for DCD if necessary.
806          */
807         if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
808             && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
809                 ++com->wopeners;
810                 error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "cydcd", 0);
811                 --com->wopeners;
812                 if (error != 0)
813                         goto out;
814                 goto open_top;
815         }
816         error = (*linesw[tp->t_line].l_open)(dev, tp);
817         disc_optim(tp, &tp->t_termios, com);
818         if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
819                 com->active_out = TRUE;
820         siosettimeout();
821 out:
822         splx(s);
823         if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
824                 comhardclose(com);
825         return (error);
826 }
827
828 static int
829 sioclose(dev, flag, mode, p)
830         dev_t           dev;
831         int             flag;
832         int             mode;
833         struct proc     *p;
834 {
835         struct com_s    *com;
836         int             mynor;
837         int             s;
838         struct tty      *tp;
839
840         mynor = minor(dev);
841         if (mynor & CONTROL_MASK)
842                 return (0);
843         com = com_addr(MINOR_TO_UNIT(mynor));
844         tp = com->tp;
845         s = spltty();
846         cd_etc(com, CD1400_ETC_STOPBREAK);
847         (*linesw[tp->t_line].l_close)(tp, flag);
848         disc_optim(tp, &tp->t_termios, com);
849         comstop(tp, FREAD | FWRITE);
850         comhardclose(com);
851         ttyclose(tp);
852         siosettimeout();
853         splx(s);
854 #ifdef broken /* session holds a ref to the tty; can't deallocate */
855         ttyfree(tp);
856         com->tp = sio_tty[unit] = NULL;
857 #endif
858         return (0);
859 }
860
861 static void
862 comhardclose(com)
863         struct com_s    *com;
864 {
865         cy_addr         iobase;
866         int             s;
867         struct tty      *tp;
868         int             unit;
869
870         unit = com->unit;
871         iobase = com->iobase;
872         s = spltty();
873 #if 0
874         com->poll = FALSE;
875         com->poll_output = FALSE;
876 #endif
877         com->do_timestamp = 0;
878 #if 0
879         outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
880 #else
881         /* XXX */
882         disable_intr();
883         com->etc = ETC_NONE;
884         cd_setreg(com, CD1400_COR2, com->cor[1] &= ~CD1400_COR2_ETC);
885         enable_intr();
886         cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
887 #endif
888
889         {
890 #if 0
891                 outb(iobase + com_ier, 0);
892 #else
893                 disable_intr();
894                 cd_setreg(com, CD1400_SRER, com->intr_enable = 0);
895                 enable_intr();
896 #endif
897                 tp = com->tp;
898                 if ((tp->t_cflag & HUPCL)
899                     /*
900                      * XXX we will miss any carrier drop between here and the
901                      * next open.  Perhaps we should watch DCD even when the
902                      * port is closed; it is not sufficient to check it at
903                      * the next open because it might go up and down while
904                      * we're not watching.
905                      */
906                     || (!com->active_out
907                        && !(com->prev_modem_status & MSR_DCD)
908                        && !(com->it_in.c_cflag & CLOCAL))
909                     || !(tp->t_state & TS_ISOPEN)) {
910                         (void)commctl(com, TIOCM_DTR, DMBIC);
911
912                         /* Disable receiver (leave transmitter enabled). */
913                         com->channel_control = CD1400_CCR_CMDCHANCTL
914                                                | CD1400_CCR_XMTEN
915                                                | CD1400_CCR_RCVDIS;
916                         cd1400_channel_cmd(com, com->channel_control);
917
918                         if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
919                                 timeout(siodtrwakeup, com, com->dtr_wait);
920                                 com->state |= CS_DTR_OFF;
921                         }
922                 }
923         }
924 #if 0
925         if (com->hasfifo) {
926                 /*
927                  * Disable fifos so that they are off after controlled
928                  * reboots.  Some BIOSes fail to detect 16550s when the
929                  * fifos are enabled.
930                  */
931                 outb(iobase + com_fifo, 0);
932         }
933 #endif
934         com->active_out = FALSE;
935         wakeup(&com->active_out);
936         wakeup(TSA_CARR_ON(tp));        /* restart any wopeners */
937         splx(s);
938 }
939
940 static int
941 siowrite(dev, uio, flag)
942         dev_t           dev;
943         struct uio      *uio;
944         int             flag;
945 {
946         int             mynor;
947         struct tty      *tp;
948         int             unit;
949
950         mynor = minor(dev);
951         if (mynor & CONTROL_MASK)
952                 return (ENODEV);
953
954         unit = MINOR_TO_UNIT(mynor);
955         tp = com_addr(unit)->tp;
956         /*
957          * (XXX) We disallow virtual consoles if the physical console is
958          * a serial port.  This is in case there is a display attached that
959          * is not the console.  In that situation we don't need/want the X
960          * server taking over the console.
961          */
962         if (constty != NULL && unit == comconsole)
963                 constty = NULL;
964 #ifdef Smarts
965         /* XXX duplicate ttwrite(), but without so much output processing on
966          * CR & LF chars.  Hardly worth the effort, given that high-throughput
967          * sessions are raw anyhow.
968          */
969 #else
970         return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
971 #endif
972 }
973
974 static void
975 siodtrwakeup(chan)
976         void    *chan;
977 {
978         struct com_s    *com;
979
980         com = (struct com_s *)chan;
981         com->state &= ~CS_DTR_OFF;
982         wakeup(&com->dtr_wait);
983 }
984
985 static void
986 sioinput(com)
987         struct com_s    *com;
988 {
989         u_char          *buf;
990         int             incc;
991         u_char          line_status;
992         int             recv_data;
993         struct tty      *tp;
994
995         buf = com->ibuf;
996         tp = com->tp;
997         if (!(tp->t_state & TS_ISOPEN)) {
998                 com_events -= (com->iptr - com->ibuf);
999                 com->iptr = com->ibuf;
1000                 return;
1001         }
1002         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1003                 /*
1004                  * Avoid the grotesquely inefficient lineswitch routine
1005                  * (ttyinput) in "raw" mode.  It usually takes about 450
1006                  * instructions (that's without canonical processing or echo!).
1007                  * slinput is reasonably fast (usually 40 instructions plus
1008                  * call overhead).
1009                  */
1010                 do {
1011                         enable_intr();
1012                         incc = com->iptr - buf;
1013                         if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
1014                             && (com->state & CS_RTS_IFLOW
1015                                 || tp->t_iflag & IXOFF)
1016                             && !(tp->t_state & TS_TBLOCK))
1017                                 ttyblock(tp);
1018                         com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1019                                 += b_to_q((char *)buf, incc, &tp->t_rawq);
1020                         buf += incc;
1021                         tk_nin += incc;
1022                         tk_rawcc += incc;
1023                         tp->t_rawcc += incc;
1024                         ttwakeup(tp);
1025                         if (tp->t_state & TS_TTSTOP
1026                             && (tp->t_iflag & IXANY
1027                                 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1028                                 tp->t_state &= ~TS_TTSTOP;
1029                                 tp->t_lflag &= ~FLUSHO;
1030                                 comstart(tp);
1031                         }
1032                         disable_intr();
1033                 } while (buf < com->iptr);
1034         } else {
1035                 do {
1036                         enable_intr();
1037                         line_status = buf[com->ierroff];
1038                         recv_data = *buf++;
1039                         if (line_status
1040                             & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
1041                                 if (line_status & LSR_BI)
1042                                         recv_data |= TTY_BI;
1043                                 if (line_status & LSR_FE)
1044                                         recv_data |= TTY_FE;
1045                                 if (line_status & LSR_OE)
1046                                         recv_data |= TTY_OE;
1047                                 if (line_status & LSR_PE)
1048                                         recv_data |= TTY_PE;
1049                         }
1050                         (*linesw[tp->t_line].l_rint)(recv_data, tp);
1051                         disable_intr();
1052                 } while (buf < com->iptr);
1053         }
1054         com_events -= (com->iptr - com->ibuf);
1055         com->iptr = com->ibuf;
1056
1057         /*
1058          * There is now room for another low-level buffer full of input,
1059          * so enable RTS if it is now disabled and there is room in the
1060          * high-level buffer.
1061          */
1062         if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & com->mcr_rts) &&
1063             !(tp->t_state & TS_TBLOCK))
1064 #if 0
1065                 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1066 #else
1067                 cd_setreg(com, com->mcr_rts_reg,
1068                           com->mcr_image |= com->mcr_rts);
1069 #endif
1070 }
1071
1072 void
1073 siointr(unit)
1074         int     unit;
1075 {
1076         int     baseu;
1077         int     cy_align;
1078         cy_addr cy_iobase;
1079         int     cyu;
1080         cy_addr iobase;
1081         u_char  status;
1082
1083         COM_LOCK();     /* XXX could this be placed down lower in the loop? */
1084
1085         baseu = unit * CY_MAX_PORTS;
1086         cy_align = com_addr(baseu)->cy_align;
1087         cy_iobase = com_addr(baseu)->cy_iobase;
1088
1089         /* check each CD1400 in turn */
1090         for (cyu = 0; cyu < cy_nr_cd1400s[unit]; ++cyu) {
1091                 iobase = (cy_addr) (cy_iobase
1092                                     + (cy_chip_offset[cyu] << cy_align));
1093                 /* poll to see if it has any work */
1094                 status = cd_inb(iobase, CD1400_SVRR, cy_align);
1095                 if (status == 0)
1096                         continue;
1097 #ifdef CyDebug
1098                 ++cy_svrr_probes;
1099 #endif
1100                 /* service requests as appropriate, giving priority to RX */
1101                 if (status & CD1400_SVRR_RXRDY) {
1102                         struct com_s    *com;
1103                         u_int           count;
1104                         u_char          *ioptr;
1105                         u_char          line_status;
1106                         u_char          recv_data;
1107                         u_char          serv_type;
1108 #ifdef PollMode
1109                         u_char          save_rir;
1110 #endif
1111
1112 #ifdef PollMode
1113                         save_rir = cd_inb(iobase, CD1400_RIR, cy_align);
1114
1115                         /* enter rx service */
1116                         cd_outb(iobase, CD1400_CAR, cy_align, save_rir);
1117                         com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1118                         = save_rir & CD1400_CAR_CHAN;
1119
1120                         serv_type = cd_inb(iobase, CD1400_RIVR, cy_align);
1121                         com = com_addr(baseu
1122                                        + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1123                                           & CD1400_xIVR_CHAN));
1124 #else
1125                         /* ack receive service */
1126                         serv_type = cy_inb(iobase, CY8_SVCACKR, cy_align);
1127
1128                         com = com_addr(baseu +
1129                                        + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1130                                           & CD1400_xIVR_CHAN));
1131 #endif
1132
1133                 if (serv_type & CD1400_RIVR_EXCEPTION) {
1134                         ++com->recv_exception;
1135                         line_status = cd_inb(iobase, CD1400_RDSR, cy_align);
1136                         /* break/unnattached error bits or real input? */
1137                         recv_data = cd_inb(iobase, CD1400_RDSR, cy_align);
1138 #ifndef SOFT_HOTCHAR
1139                         if (line_status & CD1400_RDSR_SPECIAL
1140                             && com->hotchar != 0)
1141                                 setsofttty();
1142 #endif
1143 #if 1 /* XXX "intelligent" PFO error handling would break O error handling */
1144                         if (line_status & (LSR_PE|LSR_FE|LSR_BI)) {
1145                                 /*
1146                                   Don't store PE if IGNPAR and BI if IGNBRK,
1147                                   this hack allows "raw" tty optimization
1148                                   works even if IGN* is set.
1149                                 */
1150                                 if (   com->tp == NULL
1151                                     || !(com->tp->t_state & TS_ISOPEN)
1152                                     || ((line_status & (LSR_PE|LSR_FE))
1153                                     &&  (com->tp->t_iflag & IGNPAR))
1154                                     || ((line_status & LSR_BI)
1155                                     &&  (com->tp->t_iflag & IGNBRK)))
1156                                         goto cont;
1157                                 if (   (line_status & (LSR_PE|LSR_FE))
1158                                     && (com->tp->t_state & TS_CAN_BYPASS_L_RINT)
1159                                     && ((line_status & LSR_FE)
1160                                     ||  ((line_status & LSR_PE)
1161                                     &&  (com->tp->t_iflag & INPCK))))
1162                                         recv_data = 0;
1163                         }
1164 #endif /* 1 */
1165                         ++com->bytes_in;
1166 #ifdef SOFT_HOTCHAR
1167                         if (com->hotchar != 0 && recv_data == com->hotchar)
1168                                 setsofttty();
1169 #endif
1170                         ioptr = com->iptr;
1171                         if (ioptr >= com->ibufend)
1172                                 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1173                         else {
1174                                 if (com->do_timestamp)
1175                                         microtime(&com->timestamp);
1176                                 ++com_events;
1177                                 ioptr[0] = recv_data;
1178                                 ioptr[com->ierroff] = line_status;
1179                                 com->iptr = ++ioptr;
1180                                 if (ioptr == com->ihighwater
1181                                     && com->state & CS_RTS_IFLOW)
1182 #if 0
1183                                         outb(com->modem_ctl_port,
1184                                              com->mcr_image &= ~MCR_RTS);
1185 #else
1186                                         cd_outb(iobase, com->mcr_rts_reg,
1187                                                 cy_align,
1188                                                 com->mcr_image &=
1189                                                 ~com->mcr_rts);
1190 #endif
1191                                 if (line_status & LSR_OE)
1192                                         CE_RECORD(com, CE_OVERRUN);
1193                         }
1194                         goto cont;
1195                 } else {
1196                         int     ifree;
1197
1198                         count = cd_inb(iobase, CD1400_RDCR, cy_align);
1199                         if (!count)
1200                                 goto cont;
1201                         com->bytes_in += count;
1202                         ioptr = com->iptr;
1203                         ifree = com->ibufend - ioptr;
1204                         if (count > ifree) {
1205                                 count -= ifree;
1206                                 com_events += ifree;
1207                                 if (ifree != 0) {
1208                                         if (com->do_timestamp)
1209                                                 microtime(&com->timestamp);
1210                                         do {
1211                                                 recv_data = cd_inb(iobase,
1212                                                                    CD1400_RDSR,
1213                                                                    cy_align);
1214 #ifdef SOFT_HOTCHAR
1215                                                 if (com->hotchar != 0
1216                                                     && recv_data
1217                                                        == com->hotchar)
1218                                                         setsofttty();
1219 #endif
1220                                                 ioptr[0] = recv_data;
1221                                                 ioptr[com->ierroff] = 0;
1222                                                 ++ioptr;
1223                                         } while (--ifree != 0);
1224                                 }
1225                                 com->delta_error_counts
1226                                     [CE_INTERRUPT_BUF_OVERFLOW] += count;
1227                                 do {
1228                                         recv_data = cd_inb(iobase, CD1400_RDSR,
1229                                                            cy_align);
1230 #ifdef SOFT_HOTCHAR
1231                                         if (com->hotchar != 0
1232                                             && recv_data == com->hotchar)
1233                                                 setsofttty();
1234 #endif
1235                                 } while (--count != 0);
1236                         } else {
1237                                 if (com->do_timestamp)
1238                                         microtime(&com->timestamp);
1239                                 if (ioptr <= com->ihighwater
1240                                     && ioptr + count > com->ihighwater
1241                                     && com->state & CS_RTS_IFLOW)
1242 #if 0
1243                                         outb(com->modem_ctl_port,
1244                                              com->mcr_image &= ~MCR_RTS);
1245 #else
1246                                         cd_outb(iobase, com->mcr_rts_reg,
1247                                                 cy_align,
1248                                                 com->mcr_image
1249                                                 &= ~com->mcr_rts);
1250 #endif
1251                                 com_events += count;
1252                                 do {
1253                                         recv_data = cd_inb(iobase, CD1400_RDSR,
1254                                                            cy_align);
1255 #ifdef SOFT_HOTCHAR
1256                                         if (com->hotchar != 0
1257                                             && recv_data == com->hotchar)
1258                                                 setsofttty();
1259 #endif
1260                                         ioptr[0] = recv_data;
1261                                         ioptr[com->ierroff] = 0;
1262                                         ++ioptr;
1263                                 } while (--count != 0);
1264                         }
1265                         com->iptr = ioptr;
1266                 }
1267 cont:
1268
1269                         /* terminate service context */
1270 #ifdef PollMode
1271                         cd_outb(iobase, CD1400_RIR, cy_align,
1272                                 save_rir
1273                                 & ~(CD1400_RIR_RDIREQ | CD1400_RIR_RBUSY));
1274 #else
1275                         cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1276 #endif
1277                 }
1278                 if (status & CD1400_SVRR_MDMCH) {
1279                         struct com_s    *com;
1280                         u_char  modem_status;
1281 #ifdef PollMode
1282                         u_char  save_mir;
1283 #else
1284                         u_char  vector;
1285 #endif
1286
1287 #ifdef PollMode
1288                         save_mir = cd_inb(iobase, CD1400_MIR, cy_align);
1289
1290                         /* enter modem service */
1291                         cd_outb(iobase, CD1400_CAR, cy_align, save_mir);
1292                         com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1293                         = save_mir & CD1400_CAR_CHAN;
1294
1295                         com = com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS
1296                                        + (save_mir & CD1400_MIR_CHAN));
1297 #else
1298                         /* ack modem service */
1299                         vector = cy_inb(iobase, CY8_SVCACKM, cy_align);
1300
1301                         com = com_addr(baseu
1302                                        + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1303                                           & CD1400_xIVR_CHAN));
1304 #endif
1305                         ++com->mdm;
1306                         modem_status = cd_inb(iobase, CD1400_MSVR2, cy_align);
1307                 if (modem_status != com->last_modem_status) {
1308                         if (com->do_dcd_timestamp
1309                             && !(com->last_modem_status & MSR_DCD)
1310                             && modem_status & MSR_DCD)
1311                                 microtime(&com->dcd_timestamp);
1312
1313                         /*
1314                          * Schedule high level to handle DCD changes.  Note
1315                          * that we don't use the delta bits anywhere.  Some
1316                          * UARTs mess them up, and it's easy to remember the
1317                          * previous bits and calculate the delta.
1318                          */
1319                         com->last_modem_status = modem_status;
1320                         if (!(com->state & CS_CHECKMSR)) {
1321                                 com_events += LOTS_OF_EVENTS;
1322                                 com->state |= CS_CHECKMSR;
1323                                 setsofttty();
1324                         }
1325
1326 #ifdef SOFT_CTS_OFLOW
1327                         /* handle CTS change immediately for crisp flow ctl */
1328                         if (com->state & CS_CTS_OFLOW) {
1329                                 if (modem_status & MSR_CTS) {
1330                                         com->state |= CS_ODEVREADY;
1331                                         if (com->state >= (CS_BUSY | CS_TTGO
1332                                                            | CS_ODEVREADY)
1333                                             && !(com->intr_enable
1334                                                  & CD1400_SRER_TXRDY))
1335                                                 cd_outb(iobase, CD1400_SRER,
1336                                                         cy_align,
1337                                                         com->intr_enable
1338                                                         = com->intr_enable
1339                                                           & ~CD1400_SRER_TXMPTY
1340                                                           | CD1400_SRER_TXRDY);
1341                                 } else {
1342                                         com->state &= ~CS_ODEVREADY;
1343                                         if (com->intr_enable
1344                                             & CD1400_SRER_TXRDY)
1345                                                 cd_outb(iobase, CD1400_SRER,
1346                                                         cy_align,
1347                                                         com->intr_enable
1348                                                         = com->intr_enable
1349                                                           & ~CD1400_SRER_TXRDY
1350                                                           | CD1400_SRER_TXMPTY);
1351                                 }
1352                         }
1353 #endif
1354                 }
1355
1356                         /* terminate service context */
1357 #ifdef PollMode
1358                         cd_outb(iobase, CD1400_MIR, cy_align,
1359                                 save_mir
1360                                 & ~(CD1400_MIR_RDIREQ | CD1400_MIR_RBUSY));
1361 #else
1362                         cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1363 #endif
1364                 }
1365                 if (status & CD1400_SVRR_TXRDY) {
1366                         struct com_s    *com;
1367 #ifdef PollMode
1368                         u_char  save_tir;
1369 #else
1370                         u_char  vector;
1371 #endif
1372
1373 #ifdef PollMode
1374                         save_tir = cd_inb(iobase, CD1400_TIR, cy_align);
1375
1376                         /* enter tx service */
1377                         cd_outb(iobase, CD1400_CAR, cy_align, save_tir);
1378                         com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1379                         = save_tir & CD1400_CAR_CHAN;
1380
1381                         com = com_addr(baseu
1382                                        + cyu * CD1400_NO_OF_CHANNELS
1383                                        + (save_tir & CD1400_TIR_CHAN));
1384 #else
1385                         /* ack transmit service */
1386                         vector = cy_inb(iobase, CY8_SVCACKT, cy_align);
1387
1388                         com = com_addr(baseu
1389                                        + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1390                                           & CD1400_xIVR_CHAN));
1391 #endif
1392
1393                         if (com->etc != ETC_NONE) {
1394                                 if (com->intr_enable & CD1400_SRER_TXRDY) {
1395                                         /*
1396                                          * Here due to sloppy SRER_TXRDY
1397                                          * enabling.  Ignore.  Come back when
1398                                          * tx is empty.
1399                                          */
1400                                         cd_outb(iobase, CD1400_SRER, cy_align,
1401                                                 com->intr_enable
1402                                                 = (com->intr_enable
1403                                                   & ~CD1400_SRER_TXRDY)
1404                                                   | CD1400_SRER_TXMPTY);
1405                                         goto terminate_tx_service;
1406                                 }
1407                                 switch (com->etc) {
1408                                 case CD1400_ETC_SENDBREAK:
1409                                 case CD1400_ETC_STOPBREAK:
1410                                         /*
1411                                          * Start the command.  Come back on
1412                                          * next tx empty interrupt, hopefully
1413                                          * after command has been executed.
1414                                          */
1415                                         cd_outb(iobase, CD1400_COR2, cy_align,
1416                                                 com->cor[1] |= CD1400_COR2_ETC);
1417                                         cd_outb(iobase, CD1400_TDR, cy_align,
1418                                                 CD1400_ETC_CMD);
1419                                         cd_outb(iobase, CD1400_TDR, cy_align,
1420                                                 com->etc);
1421                                         if (com->etc == CD1400_ETC_SENDBREAK)
1422                                                 com->etc = ETC_BREAK_STARTING;
1423                                         else
1424                                                 com->etc = ETC_BREAK_ENDING;
1425                                         goto terminate_tx_service;
1426                                 case ETC_BREAK_STARTING:
1427                                         /*
1428                                          * BREAK is now on.  Continue with
1429                                          * SRER_TXMPTY processing, hopefully
1430                                          * don't come back.
1431                                          */
1432                                         com->etc = ETC_BREAK_STARTED;
1433                                         break;
1434                                 case ETC_BREAK_STARTED:
1435                                         /*
1436                                          * Came back due to sloppy SRER_TXMPTY
1437                                          * enabling.  Hope again.
1438                                          */
1439                                         break;
1440                                 case ETC_BREAK_ENDING:
1441                                         /*
1442                                          * BREAK is now off.  Continue with
1443                                          * SRER_TXMPTY processing and don't
1444                                          * come back.  The SWI handler will
1445                                          * restart tx interrupts if necessary.
1446                                          */
1447                                         cd_outb(iobase, CD1400_COR2, cy_align,
1448                                                 com->cor[1]
1449                                                 &= ~CD1400_COR2_ETC);
1450                                         com->etc = ETC_BREAK_ENDED;
1451                                         if (!(com->state & CS_ODONE)) {
1452                                                 com_events += LOTS_OF_EVENTS;
1453                                                 com->state |= CS_ODONE;
1454                                                 setsofttty();
1455                                         }
1456                                         break;
1457                                 case ETC_BREAK_ENDED:
1458                                         /*
1459                                          * Shouldn't get here.  Hope again.
1460                                          */
1461                                         break;
1462                                 }
1463                         }
1464                         if (com->intr_enable & CD1400_SRER_TXMPTY) {
1465                                 if (!(com->extra_state & CSE_ODONE)) {
1466                                         com_events += LOTS_OF_EVENTS;
1467                                         com->extra_state |= CSE_ODONE;
1468                                         setsofttty();
1469                                 }
1470                                 cd_outb(iobase, CD1400_SRER, cy_align,
1471                                         com->intr_enable
1472                                         &= ~CD1400_SRER_TXMPTY);
1473                                 goto terminate_tx_service;
1474                         }
1475                 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1476                         u_char  *ioptr;
1477                         u_int   ocount;
1478
1479                         ioptr = com->obufq.l_head;
1480                                 ocount = com->obufq.l_tail - ioptr;
1481                                 if (ocount > CD1400_TX_FIFO_SIZE)
1482                                         ocount = CD1400_TX_FIFO_SIZE;
1483                                 com->bytes_out += ocount;
1484                                 do
1485                                         cd_outb(iobase, CD1400_TDR, cy_align,
1486                                                 *ioptr++);
1487                                 while (--ocount != 0);
1488                         com->obufq.l_head = ioptr;
1489                         if (ioptr >= com->obufq.l_tail) {
1490                                 struct lbq      *qp;
1491
1492                                 qp = com->obufq.l_next;
1493                                 qp->l_queued = FALSE;
1494                                 qp = qp->l_next;
1495                                 if (qp != NULL) {
1496                                         com->obufq.l_head = qp->l_head;
1497                                         com->obufq.l_tail = qp->l_tail;
1498                                         com->obufq.l_next = qp;
1499                                 } else {
1500                                         /* output just completed */
1501                                         com->state &= ~CS_BUSY;
1502
1503                                         /*
1504                                          * The setting of CSE_ODONE may be
1505                                          * stale here.  We currently only
1506                                          * use it when CS_BUSY is set, and
1507                                          * fixing it when we clear CS_BUSY
1508                                          * is easiest.
1509                                          */
1510                                         if (com->extra_state & CSE_ODONE) {
1511                                                 com_events -= LOTS_OF_EVENTS;
1512                                                 com->extra_state &= ~CSE_ODONE;
1513                                         }
1514
1515                                         cd_outb(iobase, CD1400_SRER, cy_align,
1516                                                 com->intr_enable
1517                                                 = (com->intr_enable
1518                                                   & ~CD1400_SRER_TXRDY)
1519                                                   | CD1400_SRER_TXMPTY);
1520                                 }
1521                                 if (!(com->state & CS_ODONE)) {
1522                                         com_events += LOTS_OF_EVENTS;
1523                                         com->state |= CS_ODONE;
1524
1525                                         /* handle at high level ASAP */
1526                                         setsofttty();
1527                                 }
1528                         }
1529                 }
1530
1531                         /* terminate service context */
1532 terminate_tx_service:
1533 #ifdef PollMode
1534                         cd_outb(iobase, CD1400_TIR, cy_align,
1535                                 save_tir
1536                                 & ~(CD1400_TIR_RDIREQ | CD1400_TIR_RBUSY));
1537 #else
1538                         cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1539 #endif
1540                 }
1541         }
1542
1543         /* ensure an edge for the next interrupt */
1544         cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
1545
1546         schedsofttty();
1547
1548         COM_UNLOCK();
1549 }
1550
1551 #if 0
1552 static void
1553 siointr1(com)
1554         struct com_s    *com;
1555 {
1556 }
1557 #endif
1558
1559 static int
1560 sioioctl(dev, cmd, data, flag, p)
1561         dev_t           dev;
1562         u_long          cmd;
1563         caddr_t         data;
1564         int             flag;
1565         struct proc     *p;
1566 {
1567         struct com_s    *com;
1568         int             error;
1569         int             mynor;
1570         int             s;
1571         struct tty      *tp;
1572 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1573         int             oldcmd;
1574         struct termios  term;
1575 #endif
1576
1577         mynor = minor(dev);
1578         com = com_addr(MINOR_TO_UNIT(mynor));
1579         if (mynor & CONTROL_MASK) {
1580                 struct termios  *ct;
1581
1582                 switch (mynor & CONTROL_MASK) {
1583                 case CONTROL_INIT_STATE:
1584                         ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
1585                         break;
1586                 case CONTROL_LOCK_STATE:
1587                         ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
1588                         break;
1589                 default:
1590                         return (ENODEV);        /* /dev/nodev */
1591                 }
1592                 switch (cmd) {
1593                 case TIOCSETA:
1594                         error = suser(p);
1595                         if (error != 0)
1596                                 return (error);
1597                         *ct = *(struct termios *)data;
1598                         return (0);
1599                 case TIOCGETA:
1600                         *(struct termios *)data = *ct;
1601                         return (0);
1602                 case TIOCGETD:
1603                         *(int *)data = TTYDISC;
1604                         return (0);
1605                 case TIOCGWINSZ:
1606                         bzero(data, sizeof(struct winsize));
1607                         return (0);
1608                 default:
1609                         return (ENOTTY);
1610                 }
1611         }
1612         tp = com->tp;
1613 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1614         term = tp->t_termios;
1615         oldcmd = cmd;
1616         error = ttsetcompat(tp, &cmd, data, &term);
1617         if (error != 0)
1618                 return (error);
1619         if (cmd != oldcmd)
1620                 data = (caddr_t)&term;
1621 #endif
1622         if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1623                 int     cc;
1624                 struct termios *dt = (struct termios *)data;
1625                 struct termios *lt = mynor & CALLOUT_MASK
1626                                      ? &com->lt_out : &com->lt_in;
1627
1628                 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
1629                               | (dt->c_iflag & ~lt->c_iflag);
1630                 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
1631                               | (dt->c_oflag & ~lt->c_oflag);
1632                 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
1633                               | (dt->c_cflag & ~lt->c_cflag);
1634                 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
1635                               | (dt->c_lflag & ~lt->c_lflag);
1636                 for (cc = 0; cc < NCCS; ++cc)
1637                         if (lt->c_cc[cc] != 0)
1638                                 dt->c_cc[cc] = tp->t_cc[cc];
1639                 if (lt->c_ispeed != 0)
1640                         dt->c_ispeed = tp->t_ispeed;
1641                 if (lt->c_ospeed != 0)
1642                         dt->c_ospeed = tp->t_ospeed;
1643         }
1644         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1645         if (error != ENOIOCTL)
1646                 return (error);
1647         s = spltty();
1648         error = ttioctl(tp, cmd, data, flag);
1649         disc_optim(tp, &tp->t_termios, com);
1650         if (error != ENOIOCTL) {
1651                 splx(s);
1652                 return (error);
1653         }
1654         switch (cmd) {
1655         case TIOCSBRK:
1656 #if 0
1657                 outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
1658 #else
1659                 cd_etc(com, CD1400_ETC_SENDBREAK);
1660 #endif
1661                 break;
1662         case TIOCCBRK:
1663 #if 0
1664                 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1665 #else
1666                 cd_etc(com, CD1400_ETC_STOPBREAK);
1667 #endif
1668                 break;
1669         case TIOCSDTR:
1670                 (void)commctl(com, TIOCM_DTR, DMBIS);
1671                 break;
1672         case TIOCCDTR:
1673                 (void)commctl(com, TIOCM_DTR, DMBIC);
1674                 break;
1675         /*
1676          * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set.  The
1677          * changes get undone on the next call to comparam().
1678          */
1679         case TIOCMSET:
1680                 (void)commctl(com, *(int *)data, DMSET);
1681                 break;
1682         case TIOCMBIS:
1683                 (void)commctl(com, *(int *)data, DMBIS);
1684                 break;
1685         case TIOCMBIC:
1686                 (void)commctl(com, *(int *)data, DMBIC);
1687                 break;
1688         case TIOCMGET:
1689                 *(int *)data = commctl(com, 0, DMGET);
1690                 break;
1691         case TIOCMSDTRWAIT:
1692                 /* must be root since the wait applies to following logins */
1693                 error = suser(p);
1694                 if (error != 0) {
1695                         splx(s);
1696                         return (error);
1697                 }
1698                 com->dtr_wait = *(int *)data * hz / 100;
1699                 break;
1700         case TIOCMGDTRWAIT:
1701                 *(int *)data = com->dtr_wait * 100 / hz;
1702                 break;
1703         case TIOCTIMESTAMP:
1704                 com->do_timestamp = TRUE;
1705                 *(struct timeval *)data = com->timestamp;
1706                 break;
1707         case TIOCDCDTIMESTAMP:
1708                 com->do_dcd_timestamp = TRUE;
1709                 *(struct timeval *)data = com->dcd_timestamp;
1710                 break;
1711         default:
1712                 splx(s);
1713                 return (ENOTTY);
1714         }
1715         splx(s);
1716         return (0);
1717 }
1718
1719 static void
1720 siopoll()
1721 {
1722         int             unit;
1723
1724 #ifdef CyDebug
1725         ++cy_timeouts;
1726 #endif
1727         if (com_events == 0)
1728                 return;
1729 repeat:
1730         for (unit = 0; unit < NSIO; ++unit) {
1731                 struct com_s    *com;
1732                 int             incc;
1733                 struct tty      *tp;
1734
1735                 com = com_addr(unit);
1736                 if (com == NULL)
1737                         continue;
1738                 tp = com->tp;
1739                 if (tp == NULL) {
1740                         /*
1741                          * XXX forget any events related to closed devices
1742                          * (actually never opened devices) so that we don't
1743                          * loop.
1744                          */
1745                         disable_intr();
1746                         incc = com->iptr - com->ibuf;
1747                         com->iptr = com->ibuf;
1748                         if (com->state & CS_CHECKMSR) {
1749                                 incc += LOTS_OF_EVENTS;
1750                                 com->state &= ~CS_CHECKMSR;
1751                         }
1752                         com_events -= incc;
1753                         enable_intr();
1754                         if (incc != 0)
1755                                 log(LOG_DEBUG,
1756                                     "sio%d: %d events for device with no tp\n",
1757                                     unit, incc);
1758                         continue;
1759                 }
1760                 if (com->iptr != com->ibuf) {
1761                         disable_intr();
1762                         sioinput(com);
1763                         enable_intr();
1764                 }
1765                 if (com->state & CS_CHECKMSR) {
1766                         u_char  delta_modem_status;
1767
1768                         disable_intr();
1769                         delta_modem_status = com->last_modem_status
1770                                              ^ com->prev_modem_status;
1771                         com->prev_modem_status = com->last_modem_status;
1772                         com_events -= LOTS_OF_EVENTS;
1773                         com->state &= ~CS_CHECKMSR;
1774                         enable_intr();
1775                         if (delta_modem_status & MSR_DCD)
1776                                 (*linesw[tp->t_line].l_modem)
1777                                         (tp, com->prev_modem_status & MSR_DCD);
1778                 }
1779                 if (com->extra_state & CSE_ODONE) {
1780                         disable_intr();
1781                         com_events -= LOTS_OF_EVENTS;
1782                         com->extra_state &= ~CSE_ODONE;
1783                         enable_intr();
1784                         if (!(com->state & CS_BUSY)) {
1785                                 tp->t_state &= ~TS_BUSY;
1786                                 ttwwakeup(com->tp);
1787                         }
1788                         if (com->etc != ETC_NONE) {
1789                                 if (com->etc == ETC_BREAK_ENDED)
1790                                         com->etc = ETC_NONE;
1791                                 wakeup(&com->etc);
1792                         }
1793                 }
1794                 if (com->state & CS_ODONE) {
1795                         disable_intr();
1796                         com_events -= LOTS_OF_EVENTS;
1797                         com->state &= ~CS_ODONE;
1798                         enable_intr();
1799                         (*linesw[tp->t_line].l_start)(tp);
1800                 }
1801                 if (com_events == 0)
1802                         break;
1803         }
1804         if (com_events >= LOTS_OF_EVENTS)
1805                 goto repeat;
1806 }
1807
1808 static int
1809 comparam(tp, t)
1810         struct tty      *tp;
1811         struct termios  *t;
1812 {
1813         int             bits;
1814         int             cflag;
1815         struct com_s    *com;
1816         u_char          cor_change;
1817         u_long          cy_clock;
1818         int             idivisor;
1819         int             iflag;
1820         int             iprescaler;
1821         int             itimeout;
1822         int             odivisor;
1823         int             oprescaler;
1824         u_char          opt;
1825         int             s;
1826         int             unit;
1827
1828         /* do historical conversions */
1829         if (t->c_ispeed == 0)
1830                 t->c_ispeed = t->c_ospeed;
1831
1832         unit = DEV_TO_UNIT(tp->t_dev);
1833         com = com_addr(unit);
1834
1835         /* check requested parameters */
1836         cy_clock = CY_CLOCK(com->gfrcr_image);
1837         idivisor = comspeed(t->c_ispeed, cy_clock, &iprescaler);
1838         if (idivisor < 0)
1839                 return (EINVAL);
1840         odivisor = comspeed(t->c_ospeed, cy_clock, &oprescaler);
1841         if (odivisor < 0)
1842                 return (EINVAL);
1843
1844         /* parameters are OK, convert them to the com struct and the device */
1845         s = spltty();
1846         if (odivisor == 0)
1847                 (void)commctl(com, TIOCM_DTR, DMBIC);   /* hang up line */
1848         else
1849                 (void)commctl(com, TIOCM_DTR, DMBIS);
1850
1851         /*
1852          * This returns with interrupts disabled so that we can complete
1853          * the speed change atomically.
1854          */
1855         (void) siosetwater(com, t->c_ispeed);
1856
1857         /* XXX we don't actually change the speed atomically. */
1858         enable_intr();
1859
1860         if (idivisor != 0) {
1861                 cd_setreg(com, CD1400_RBPR, idivisor);
1862                 cd_setreg(com, CD1400_RCOR, iprescaler);
1863         }
1864         if (odivisor != 0) {
1865                 cd_setreg(com, CD1400_TBPR, odivisor);
1866                 cd_setreg(com, CD1400_TCOR, oprescaler);
1867         }
1868
1869         /*
1870          * channel control
1871          *      receiver enable
1872          *      transmitter enable (always set)
1873          */
1874         cflag = t->c_cflag;
1875         opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
1876               | (cflag & CREAD ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
1877         if (opt != com->channel_control) {
1878                 com->channel_control = opt;
1879                 cd1400_channel_cmd(com, opt);
1880         }
1881
1882 #ifdef Smarts
1883         /* set special chars */
1884         /* XXX if one is _POSIX_VDISABLE, can't use some others */
1885         if (t->c_cc[VSTOP] != _POSIX_VDISABLE)
1886                 cd_setreg(com, CD1400_SCHR1, t->c_cc[VSTOP]);
1887         if (t->c_cc[VSTART] != _POSIX_VDISABLE)
1888                 cd_setreg(com, CD1400_SCHR2, t->c_cc[VSTART]);
1889         if (t->c_cc[VINTR] != _POSIX_VDISABLE)
1890                 cd_setreg(com, CD1400_SCHR3, t->c_cc[VINTR]);
1891         if (t->c_cc[VSUSP] != _POSIX_VDISABLE)
1892                 cd_setreg(com, CD1400_SCHR4, t->c_cc[VSUSP]);
1893 #endif
1894
1895         /*
1896          * set channel option register 1 -
1897          *      parity mode
1898          *      stop bits
1899          *      char length
1900          */
1901         opt = 0;
1902         /* parity */
1903         if (cflag & PARENB) {
1904                 if (cflag & PARODD)
1905                         opt |= CD1400_COR1_PARODD;
1906                 opt |= CD1400_COR1_PARNORMAL;
1907         }
1908         iflag = t->c_iflag;
1909         if (!(iflag & INPCK))
1910                 opt |= CD1400_COR1_NOINPCK;
1911         bits = 1 + 1;
1912         /* stop bits */
1913         if (cflag & CSTOPB) {
1914                 ++bits;
1915                 opt |= CD1400_COR1_STOP2;
1916         }
1917         /* char length */
1918         switch (cflag & CSIZE) {
1919         case CS5:
1920                 bits += 5;
1921                 opt |= CD1400_COR1_CS5;
1922                 break;
1923         case CS6:
1924                 bits += 6;
1925                 opt |= CD1400_COR1_CS6;
1926                 break;
1927         case CS7:
1928                 bits += 7;
1929                 opt |= CD1400_COR1_CS7;
1930                 break;
1931         default:
1932                 bits += 8;
1933                 opt |= CD1400_COR1_CS8;
1934                 break;
1935         }
1936         cor_change = 0;
1937         if (opt != com->cor[0]) {
1938                 cor_change |= CD1400_CCR_COR1;
1939                 cd_setreg(com, CD1400_COR1, com->cor[0] = opt);
1940         }
1941
1942         /*
1943          * Set receive time-out period, normally to max(one char time, 5 ms).
1944          */
1945         if (t->c_ispeed == 0)
1946                 itimeout = cd_getreg(com, CD1400_RTPR);
1947         else {
1948                 itimeout = (1000 * bits + t->c_ispeed - 1) / t->c_ispeed;
1949 #ifdef SOFT_HOTCHAR
1950 #define MIN_RTP         1
1951 #else
1952 #define MIN_RTP         5
1953 #endif
1954                 if (itimeout < MIN_RTP)
1955                         itimeout = MIN_RTP;
1956         }
1957         if (!(t->c_lflag & ICANON) && t->c_cc[VMIN] != 0 && t->c_cc[VTIME] != 0
1958             && t->c_cc[VTIME] * 10 > itimeout)
1959                 itimeout = t->c_cc[VTIME] * 10;
1960         if (itimeout > 255)
1961                 itimeout = 255;
1962         cd_setreg(com, CD1400_RTPR, itimeout);
1963
1964         /*
1965          * set channel option register 2 -
1966          *      flow control
1967          */
1968         opt = 0;
1969 #ifdef Smarts
1970         if (iflag & IXANY)
1971                 opt |= CD1400_COR2_IXANY;
1972         if (iflag & IXOFF)
1973                 opt |= CD1400_COR2_IXOFF;
1974 #endif
1975 #ifndef SOFT_CTS_OFLOW
1976         if (cflag & CCTS_OFLOW)
1977                 opt |= CD1400_COR2_CCTS_OFLOW;
1978 #endif
1979         disable_intr();
1980         if (opt != com->cor[1]) {
1981                 cor_change |= CD1400_CCR_COR2;
1982                 cd_setreg(com, CD1400_COR2, com->cor[1] = opt);
1983         }
1984         enable_intr();
1985
1986         /*
1987          * set channel option register 3 -
1988          *      receiver FIFO interrupt threshold
1989          *      flow control
1990          */
1991         opt = RxFifoThreshold;
1992 #ifdef Smarts
1993         if (t->c_lflag & ICANON)
1994                 opt |= CD1400_COR3_SCD34;       /* detect INTR & SUSP chars */
1995         if (iflag & IXOFF)
1996                 /* detect and transparently handle START and STOP chars */
1997                 opt |= CD1400_COR3_FCT | CD1400_COR3_SCD12;
1998 #endif
1999         if (opt != com->cor[2]) {
2000                 cor_change |= CD1400_CCR_COR3;
2001                 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2002         }
2003
2004         /* notify the CD1400 if COR1-3 have changed */
2005         if (cor_change)
2006                 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | cor_change);
2007
2008         /*
2009          * set channel option register 4 -
2010          *      CR/NL processing
2011          *      break processing
2012          *      received exception processing
2013          */
2014         opt = 0;
2015         if (iflag & IGNCR)
2016                 opt |= CD1400_COR4_IGNCR;
2017 #ifdef Smarts
2018         /*
2019          * we need a new ttyinput() for this, as we don't want to
2020          * have ICRNL && INLCR being done in both layers, or to have
2021          * synchronisation problems
2022          */
2023         if (iflag & ICRNL)
2024                 opt |= CD1400_COR4_ICRNL;
2025         if (iflag & INLCR)
2026                 opt |= CD1400_COR4_INLCR;
2027 #endif
2028         if (iflag & IGNBRK)
2029                 opt |= CD1400_COR4_IGNBRK | CD1400_COR4_NOBRKINT;
2030         /*
2031          * The `-ignbrk -brkint parmrk' case is not handled by the hardware,
2032          * so only tell the hardware about -brkint if -parmrk.
2033          */
2034         if (!(iflag & (BRKINT | PARMRK)))
2035                 opt |= CD1400_COR4_NOBRKINT;
2036 #if 0
2037         /* XXX using this "intelligence" breaks reporting of overruns. */
2038         if (iflag & IGNPAR)
2039                 opt |= CD1400_COR4_PFO_DISCARD;
2040         else {
2041                 if (iflag & PARMRK)
2042                         opt |= CD1400_COR4_PFO_ESC;
2043                 else
2044                         opt |= CD1400_COR4_PFO_NUL;
2045         }
2046 #else
2047         opt |= CD1400_COR4_PFO_EXCEPTION;
2048 #endif
2049         cd_setreg(com, CD1400_COR4, opt);
2050
2051         /*
2052          * set channel option register 5 -
2053          */
2054         opt = 0;
2055         if (iflag & ISTRIP)
2056                 opt |= CD1400_COR5_ISTRIP;
2057         if (t->c_iflag & IEXTEN)
2058                 /* enable LNEXT (e.g. ctrl-v quoting) handling */
2059                 opt |= CD1400_COR5_LNEXT;
2060 #ifdef Smarts
2061         if (t->c_oflag & ONLCR)
2062                 opt |= CD1400_COR5_ONLCR;
2063         if (t->c_oflag & OCRNL)
2064                 opt |= CD1400_COR5_OCRNL;
2065 #endif
2066         cd_setreg(com, CD1400_COR5, opt);
2067
2068         /*
2069          * We always generate modem status change interrupts for CD changes.
2070          * Among other things, this is necessary to track TS_CARR_ON for
2071          * pstat to print even when the driver doesn't care.  CD changes
2072          * should be rare so interrupts for them are not worth extra code to
2073          * avoid.  We avoid interrupts for other modem status changes (except
2074          * for CTS changes when SOFT_CTS_OFLOW is configured) since this is
2075          * simplest and best.
2076          */
2077
2078         /*
2079          * set modem change option register 1
2080          *      generate modem interrupts on which 1 -> 0 input transitions
2081          *      also controls auto-DTR output flow-control, which we don't use
2082          */
2083         opt = CD1400_MCOR1_CDzd;
2084 #ifdef SOFT_CTS_OFLOW
2085         if (cflag & CCTS_OFLOW)
2086                 opt |= CD1400_MCOR1_CTSzd;
2087 #endif
2088         cd_setreg(com, CD1400_MCOR1, opt);
2089
2090         /*
2091          * set modem change option register 2
2092          *      generate modem interrupts on specific 0 -> 1 input transitions
2093          */
2094         opt = CD1400_MCOR2_CDod;
2095 #ifdef SOFT_CTS_OFLOW
2096         if (cflag & CCTS_OFLOW)
2097                 opt |= CD1400_MCOR2_CTSod;
2098 #endif
2099         cd_setreg(com, CD1400_MCOR2, opt);
2100
2101         /*
2102          * XXX should have done this long ago, but there is too much state
2103          * to change all atomically.
2104          */
2105         disable_intr();
2106
2107         com->state &= ~CS_TTGO;
2108         if (!(tp->t_state & TS_TTSTOP))
2109                 com->state |= CS_TTGO;
2110         if (cflag & CRTS_IFLOW) {
2111                 com->state |= CS_RTS_IFLOW;
2112                 /*
2113                  * If CS_RTS_IFLOW just changed from off to on, the change
2114                  * needs to be propagated to MCR_RTS.  This isn't urgent,
2115                  * so do it later by calling comstart() instead of repeating
2116                  * a lot of code from comstart() here.
2117                  */
2118         } else if (com->state & CS_RTS_IFLOW) {
2119                 com->state &= ~CS_RTS_IFLOW;
2120                 /*
2121                  * CS_RTS_IFLOW just changed from on to off.  Force MCR_RTS
2122                  * on here, since comstart() won't do it later.
2123                  */
2124 #if 0
2125                 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2126 #else
2127                 cd_setreg(com, com->mcr_rts_reg,
2128                           com->mcr_image |= com->mcr_rts);
2129 #endif
2130         }
2131
2132         /*
2133          * Set up state to handle output flow control.
2134          * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2135          * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2136          */
2137         com->state |= CS_ODEVREADY;
2138 #ifdef SOFT_CTS_OFLOW
2139         com->state &= ~CS_CTS_OFLOW;
2140         if (cflag & CCTS_OFLOW) {
2141                 com->state |= CS_CTS_OFLOW;
2142                 if (!(com->last_modem_status & MSR_CTS))
2143                         com->state &= ~CS_ODEVREADY;
2144         }
2145 #endif
2146         /* XXX shouldn't call functions while intrs are disabled. */
2147         disc_optim(tp, t, com);
2148 #if 0
2149         /*
2150          * Recover from fiddling with CS_TTGO.  We used to call siointr1()
2151          * unconditionally, but that defeated the careful discarding of
2152          * stale input in sioopen().
2153          */
2154         if (com->state >= (CS_BUSY | CS_TTGO))
2155                 siointr1(com);
2156 #endif
2157         if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2158                 if (!(com->intr_enable & CD1400_SRER_TXRDY))
2159                         cd_setreg(com, CD1400_SRER,
2160                                   com->intr_enable
2161                                   = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2162                                     | CD1400_SRER_TXRDY);
2163         } else {
2164                 if (com->intr_enable & CD1400_SRER_TXRDY)
2165                         cd_setreg(com, CD1400_SRER,
2166                                   com->intr_enable
2167                                   = (com->intr_enable & ~CD1400_SRER_TXRDY)
2168                                     | CD1400_SRER_TXMPTY);
2169         }
2170
2171         enable_intr();
2172         splx(s);
2173         comstart(tp);
2174         if (com->ibufold != NULL) {
2175                 free(com->ibufold, M_DEVBUF);
2176                 com->ibufold = NULL;
2177         }
2178         return (0);
2179 }
2180
2181 static int
2182 siosetwater(com, speed)
2183         struct com_s    *com;
2184         speed_t         speed;
2185 {
2186         int             cp4ticks;
2187         u_char          *ibuf;
2188         int             ibufsize;
2189         struct tty      *tp;
2190
2191         /*
2192          * Make the buffer size large enough to handle a softtty interrupt
2193          * latency of about 2 ticks without loss of throughput or data
2194          * (about 3 ticks if input flow control is not used or not honoured,
2195          * but a bit less for CS5-CS7 modes).
2196          */
2197         cp4ticks = speed / 10 / hz * 4;
2198         for (ibufsize = 128; ibufsize < cp4ticks;)
2199                 ibufsize <<= 1;
2200         if (ibufsize == com->ibufsize) {
2201                 disable_intr();
2202                 return (0);
2203         }
2204
2205         /*
2206          * Allocate input buffer.  The extra factor of 2 in the size is
2207          * to allow for an error byte for each input byte.
2208          */
2209         ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT);
2210         if (ibuf == NULL) {
2211                 disable_intr();
2212                 return (ENOMEM);
2213         }
2214
2215         /* Initialize non-critical variables. */
2216         com->ibufold = com->ibuf;
2217         com->ibufsize = ibufsize;
2218         tp = com->tp;
2219         if (tp != NULL) {
2220                 tp->t_ififosize = 2 * ibufsize;
2221                 tp->t_ispeedwat = (speed_t)-1;
2222                 tp->t_ospeedwat = (speed_t)-1;
2223         }
2224
2225         /*
2226          * Read current input buffer, if any.  Continue with interrupts
2227          * disabled.
2228          */
2229         disable_intr();
2230         if (com->iptr != com->ibuf)
2231                 sioinput(com);
2232
2233         /*-
2234          * Initialize critical variables, including input buffer watermarks.
2235          * The external device is asked to stop sending when the buffer
2236          * exactly reaches high water, or when the high level requests it.
2237          * The high level is notified immediately (rather than at a later
2238          * clock tick) when this watermark is reached.
2239          * The buffer size is chosen so the watermark should almost never
2240          * be reached.
2241          * The low watermark is invisibly 0 since the buffer is always
2242          * emptied all at once.
2243          */
2244         com->iptr = com->ibuf = ibuf;
2245         com->ibufend = ibuf + ibufsize;
2246         com->ierroff = ibufsize;
2247         com->ihighwater = ibuf + 3 * ibufsize / 4;
2248         return (0);
2249 }
2250
2251 static void
2252 comstart(tp)
2253         struct tty      *tp;
2254 {
2255         struct com_s    *com;
2256         int             s;
2257 #ifdef CyDebug
2258         bool_t          started;
2259 #endif
2260         int             unit;
2261
2262         unit = DEV_TO_UNIT(tp->t_dev);
2263         com = com_addr(unit);
2264         s = spltty();
2265
2266 #ifdef CyDebug
2267         ++com->start_count;
2268         started = FALSE;
2269 #endif
2270
2271         disable_intr();
2272         if (tp->t_state & TS_TTSTOP) {
2273                 com->state &= ~CS_TTGO;
2274                 if (com->intr_enable & CD1400_SRER_TXRDY)
2275                         cd_setreg(com, CD1400_SRER,
2276                                   com->intr_enable
2277                                   = (com->intr_enable & ~CD1400_SRER_TXRDY)
2278                                     | CD1400_SRER_TXMPTY);
2279         } else {
2280                 com->state |= CS_TTGO;
2281                 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)
2282                     && !(com->intr_enable & CD1400_SRER_TXRDY))
2283                         cd_setreg(com, CD1400_SRER,
2284                                   com->intr_enable
2285                                   = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2286                                     | CD1400_SRER_TXRDY);
2287         }
2288         if (tp->t_state & TS_TBLOCK) {
2289                 if (com->mcr_image & com->mcr_rts && com->state & CS_RTS_IFLOW)
2290 #if 0
2291                         outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2292 #else
2293                         cd_setreg(com, com->mcr_rts_reg,
2294                                   com->mcr_image &= ~com->mcr_rts);
2295 #endif
2296         } else {
2297                 if (!(com->mcr_image & com->mcr_rts)
2298                     && com->iptr < com->ihighwater
2299                     && com->state & CS_RTS_IFLOW)
2300 #if 0
2301                         outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2302 #else
2303                         cd_setreg(com, com->mcr_rts_reg,
2304                                   com->mcr_image |= com->mcr_rts);
2305 #endif
2306         }
2307         enable_intr();
2308         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2309                 ttwwakeup(tp);
2310                 splx(s);
2311                 return;
2312         }
2313         if (tp->t_outq.c_cc != 0) {
2314                 struct lbq      *qp;
2315                 struct lbq      *next;
2316
2317                 if (!com->obufs[0].l_queued) {
2318 #ifdef CyDebug
2319                         started = TRUE;
2320 #endif
2321                         com->obufs[0].l_tail
2322                             = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2323                                                   sizeof com->obuf1);
2324                         com->obufs[0].l_next = NULL;
2325                         com->obufs[0].l_queued = TRUE;
2326                         disable_intr();
2327                         if (com->state & CS_BUSY) {
2328                                 qp = com->obufq.l_next;
2329                                 while ((next = qp->l_next) != NULL)
2330                                         qp = next;
2331                                 qp->l_next = &com->obufs[0];
2332                         } else {
2333                                 com->obufq.l_head = com->obufs[0].l_head;
2334                                 com->obufq.l_tail = com->obufs[0].l_tail;
2335                                 com->obufq.l_next = &com->obufs[0];
2336                                 com->state |= CS_BUSY;
2337                                 if (com->state >= (CS_BUSY | CS_TTGO
2338                                                    | CS_ODEVREADY))
2339                                         cd_setreg(com, CD1400_SRER,
2340                                                   com->intr_enable
2341                                                   = (com->intr_enable
2342                                                     & ~CD1400_SRER_TXMPTY)
2343                                                     | CD1400_SRER_TXRDY);
2344                         }
2345                         enable_intr();
2346                 }
2347                 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2348 #ifdef CyDebug
2349                         started = TRUE;
2350 #endif
2351                         com->obufs[1].l_tail
2352                             = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2353                                                   sizeof com->obuf2);
2354                         com->obufs[1].l_next = NULL;
2355                         com->obufs[1].l_queued = TRUE;
2356                         disable_intr();
2357                         if (com->state & CS_BUSY) {
2358                                 qp = com->obufq.l_next;
2359                                 while ((next = qp->l_next) != NULL)
2360                                         qp = next;
2361                                 qp->l_next = &com->obufs[1];
2362                         } else {
2363                                 com->obufq.l_head = com->obufs[1].l_head;
2364                                 com->obufq.l_tail = com->obufs[1].l_tail;
2365                                 com->obufq.l_next = &com->obufs[1];
2366                                 com->state |= CS_BUSY;
2367                                 if (com->state >= (CS_BUSY | CS_TTGO
2368                                                    | CS_ODEVREADY))
2369                                         cd_setreg(com, CD1400_SRER,
2370                                                   com->intr_enable
2371                                                   = (com->intr_enable
2372                                                     & ~CD1400_SRER_TXMPTY)
2373                                                     | CD1400_SRER_TXRDY);
2374                         }
2375                         enable_intr();
2376                 }
2377                 tp->t_state |= TS_BUSY;
2378         }
2379 #ifdef CyDebug
2380         if (started)
2381                 ++com->start_real;
2382 #endif
2383 #if 0
2384         disable_intr();
2385         if (com->state >= (CS_BUSY | CS_TTGO))
2386                 siointr1(com);  /* fake interrupt to start output */
2387         enable_intr();
2388 #endif
2389         ttwwakeup(tp);
2390         splx(s);
2391 }
2392
2393 static void
2394 comstop(tp, rw)
2395         struct tty      *tp;
2396         int             rw;
2397 {
2398         struct com_s    *com;
2399         bool_t          wakeup_etc;
2400
2401         com = com_addr(DEV_TO_UNIT(tp->t_dev));
2402         wakeup_etc = FALSE;
2403         disable_intr();
2404         if (rw & FWRITE) {
2405                 com->obufs[0].l_queued = FALSE;
2406                 com->obufs[1].l_queued = FALSE;
2407                 if (com->extra_state & CSE_ODONE) {
2408                         com_events -= LOTS_OF_EVENTS;
2409                         com->extra_state &= ~CSE_ODONE;
2410                         if (com->etc != ETC_NONE) {
2411                                 if (com->etc == ETC_BREAK_ENDED)
2412                                         com->etc = ETC_NONE;
2413                                 wakeup_etc = TRUE;
2414                         }
2415                 }
2416                 com->tp->t_state &= ~TS_BUSY;
2417                 if (com->state & CS_ODONE)
2418                         com_events -= LOTS_OF_EVENTS;
2419                 com->state &= ~(CS_ODONE | CS_BUSY);
2420         }
2421         if (rw & FREAD) {
2422                 /* XXX no way to reset only input fifo. */
2423                 com_events -= (com->iptr - com->ibuf);
2424                 com->iptr = com->ibuf;
2425         }
2426         enable_intr();
2427         if (wakeup_etc)
2428                 wakeup(&com->etc);
2429         if (rw & FWRITE && com->etc == ETC_NONE)
2430                 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
2431         comstart(tp);
2432 }
2433
2434 static int
2435 commctl(com, bits, how)
2436         struct com_s    *com;
2437         int             bits;
2438         int             how;
2439 {
2440         int     mcr;
2441         int     msr;
2442
2443         if (how == DMGET) {
2444                 if (com->channel_control & CD1400_CCR_RCVEN)
2445                         bits |= TIOCM_LE;
2446                 mcr = com->mcr_image;
2447                 if (mcr & com->mcr_dtr)
2448                         bits |= TIOCM_DTR;
2449                 if (mcr & com->mcr_rts)
2450                         /* XXX wired on for Cyclom-8Ys */
2451                         bits |= TIOCM_RTS;
2452
2453                 /*
2454                  * We must read the modem status from the hardware because
2455                  * we don't generate modem status change interrupts for all
2456                  * changes, so com->prev_modem_status is not guaranteed to
2457                  * be up to date.  This is safe, unlike for sio, because
2458                  * reading the status register doesn't clear pending modem
2459                  * status change interrupts.
2460                  */
2461                 msr = cd_getreg(com, CD1400_MSVR2);
2462
2463                 if (msr & MSR_CTS)
2464                         bits |= TIOCM_CTS;
2465                 if (msr & MSR_DCD)
2466                         bits |= TIOCM_CD;
2467                 if (msr & MSR_DSR)
2468                         bits |= TIOCM_DSR;
2469                 if (msr & MSR_RI)
2470                         /* XXX not connected except for Cyclom-16Y? */
2471                         bits |= TIOCM_RI;
2472                 return (bits);
2473         }
2474         mcr = 0;
2475         if (bits & TIOCM_DTR)
2476                 mcr |= com->mcr_dtr;
2477         if (bits & TIOCM_RTS)
2478                 mcr |= com->mcr_rts;
2479         disable_intr();
2480         switch (how) {
2481         case DMSET:
2482                 com->mcr_image = mcr;
2483                 cd_setreg(com, CD1400_MSVR1, mcr);
2484                 cd_setreg(com, CD1400_MSVR2, mcr);
2485                 break;
2486         case DMBIS:
2487                 com->mcr_image = mcr = com->mcr_image | mcr;
2488                 cd_setreg(com, CD1400_MSVR1, mcr);
2489                 cd_setreg(com, CD1400_MSVR2, mcr);
2490                 break;
2491         case DMBIC:
2492                 com->mcr_image = mcr = com->mcr_image & ~mcr;
2493                 cd_setreg(com, CD1400_MSVR1, mcr);
2494                 cd_setreg(com, CD1400_MSVR2, mcr);
2495                 break;
2496         }
2497         enable_intr();
2498         return (0);
2499 }
2500
2501 static void
2502 siosettimeout()
2503 {
2504         struct com_s    *com;
2505         bool_t          someopen;
2506         int             unit;
2507
2508         /*
2509          * Set our timeout period to 1 second if no polled devices are open.
2510          * Otherwise set it to max(1/200, 1/hz).
2511          * Enable timeouts iff some device is open.
2512          */
2513         untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
2514         sio_timeout = hz;
2515         someopen = FALSE;
2516         for (unit = 0; unit < NSIO; ++unit) {
2517                 com = com_addr(unit);
2518                 if (com != NULL && com->tp != NULL
2519                     && com->tp->t_state & TS_ISOPEN) {
2520                         someopen = TRUE;
2521 #if 0
2522                         if (com->poll || com->poll_output) {
2523                                 sio_timeout = hz > 200 ? hz / 200 : 1;
2524                                 break;
2525                         }
2526 #endif
2527                 }
2528         }
2529         if (someopen) {
2530                 sio_timeouts_until_log = hz / sio_timeout;
2531                 sio_timeout_handle = timeout(comwakeup, (void *)NULL,
2532                                              sio_timeout);
2533         } else {
2534                 /* Flush error messages, if any. */
2535                 sio_timeouts_until_log = 1;
2536                 comwakeup((void *)NULL);
2537                 untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
2538         }
2539 }
2540
2541 static void
2542 comwakeup(chan)
2543         void    *chan;
2544 {
2545         struct com_s    *com;
2546         int             unit;
2547
2548         sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout);
2549
2550 #if 0
2551         /*
2552          * Recover from lost output interrupts.
2553          * Poll any lines that don't use interrupts.
2554          */
2555         for (unit = 0; unit < NSIO; ++unit) {
2556                 com = com_addr(unit);
2557                 if (com != NULL
2558                     && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2559                         disable_intr();
2560                         siointr1(com);
2561                         enable_intr();
2562                 }
2563         }
2564 #endif
2565
2566         /*
2567          * Check for and log errors, but not too often.
2568          */
2569         if (--sio_timeouts_until_log > 0)
2570                 return;
2571         sio_timeouts_until_log = hz / sio_timeout;
2572         for (unit = 0; unit < NSIO; ++unit) {
2573                 int     errnum;
2574
2575                 com = com_addr(unit);
2576                 if (com == NULL)
2577                         continue;
2578                 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2579                         u_int   delta;
2580                         u_long  total;
2581
2582                         disable_intr();
2583                         delta = com->delta_error_counts[errnum];
2584                         com->delta_error_counts[errnum] = 0;
2585                         enable_intr();
2586                         if (delta == 0)
2587                                 continue;
2588                         total = com->error_counts[errnum] += delta;
2589                         log(LOG_ERR, "cy%d: %u more %s%s (total %lu)\n",
2590                             unit, delta, error_desc[errnum],
2591                             delta == 1 ? "" : "s", total);
2592                 }
2593         }
2594 }
2595
2596 static void
2597 disc_optim(tp, t, com)
2598         struct tty      *tp;
2599         struct termios  *t;
2600         struct com_s    *com;
2601 {
2602 #ifndef SOFT_HOTCHAR
2603         u_char  opt;
2604 #endif
2605
2606         /*
2607          * XXX can skip a lot more cases if Smarts.  Maybe
2608          * (IGNCR | ISTRIP | IXON) in c_iflag.  But perhaps we
2609          * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2610          */
2611         if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2612             && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2613             && (!(t->c_iflag & PARMRK)
2614                 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2615             && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2616             && linesw[tp->t_line].l_rint == ttyinput)
2617                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2618         else
2619                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2620         com->hotchar = linesw[tp->t_line].l_hotchar;
2621 #ifndef SOFT_HOTCHAR
2622         opt = com->cor[2] & ~CD1400_COR3_SCD34;
2623         if (com->hotchar != 0) {
2624                 cd_setreg(com, CD1400_SCHR3, com->hotchar);
2625                 cd_setreg(com, CD1400_SCHR4, com->hotchar);
2626                 opt |= CD1400_COR3_SCD34;
2627         }
2628         if (opt != com->cor[2]) {
2629                 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2630                 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR3);
2631         }
2632 #endif
2633 }
2634
2635 #ifdef Smarts
2636 /* standard line discipline input routine */
2637 int
2638 cyinput(c, tp)
2639         int             c;
2640         struct tty      *tp;
2641 {
2642         /* XXX duplicate ttyinput(), but without the IXOFF/IXON/ISTRIP/IPARMRK
2643          * bits, as they are done by the CD1400.  Hardly worth the effort,
2644          * given that high-throughput sessions are raw anyhow.
2645          */
2646 }
2647 #endif /* Smarts */
2648
2649 static int
2650 comspeed(speed, cy_clock, prescaler_io)
2651         speed_t speed;
2652         u_long  cy_clock;
2653         int     *prescaler_io;
2654 {
2655         int     actual;
2656         int     error;
2657         int     divider;
2658         int     prescaler;
2659         int     prescaler_unit;
2660
2661         if (speed == 0)
2662                 return (0);
2663         if (speed < 0 || speed > 150000)
2664                 return (-1);
2665
2666         /* determine which prescaler to use */
2667         for (prescaler_unit = 4, prescaler = 2048; prescaler_unit;
2668                 prescaler_unit--, prescaler >>= 2) {
2669                 if (cy_clock / prescaler / speed > 63)
2670                         break;
2671         }
2672
2673         divider = (cy_clock / prescaler * 2 / speed + 1) / 2; /* round off */
2674         if (divider > 255)
2675                 divider = 255;
2676         actual = cy_clock/prescaler/divider;
2677
2678         /* 10 times error in percent: */
2679         error = ((actual - (long)speed) * 2000 / (long)speed + 1) / 2;
2680
2681         /* 3.0% max error tolerance */
2682         if (error < -30 || error > 30)
2683                 return (-1);
2684
2685 #if 0
2686         printf("prescaler = %d (%d)\n", prescaler, prescaler_unit);
2687         printf("divider = %d (%x)\n", divider, divider);
2688         printf("actual = %d\n", actual);
2689         printf("error = %d\n", error);
2690 #endif
2691
2692         *prescaler_io = prescaler_unit;
2693         return (divider);
2694 }
2695
2696 static void
2697 cd1400_channel_cmd(com, cmd)
2698         struct com_s    *com;
2699         int             cmd;
2700 {
2701         cd1400_channel_cmd_wait(com);
2702         cd_setreg(com, CD1400_CCR, cmd);
2703         cd1400_channel_cmd_wait(com);
2704 }
2705
2706 static void
2707 cd1400_channel_cmd_wait(com)
2708         struct com_s    *com;
2709 {
2710         struct timeval  start;
2711         struct timeval  tv;
2712         long            usec;
2713
2714         if (cd_getreg(com, CD1400_CCR) == 0)
2715                 return;
2716         microtime(&start);
2717         for (;;) {
2718                 if (cd_getreg(com, CD1400_CCR) == 0)
2719                         return;
2720                 microtime(&tv);
2721                 usec = 1000000 * (tv.tv_sec - start.tv_sec) +
2722                     tv.tv_usec - start.tv_usec;
2723                 if (usec >= 5000) {
2724                         log(LOG_ERR,
2725                             "cy%d: channel command timeout (%ld usec)\n",
2726                             com->unit, usec);
2727                         return;
2728                 }
2729         }
2730 }
2731
2732 static void
2733 cd_etc(com, etc)
2734         struct com_s    *com;
2735         int             etc;
2736 {
2737         /*
2738          * We can't change the hardware's ETC state while there are any
2739          * characters in the tx fifo, since those characters would be
2740          * interpreted as commands!  Unputting characters from the fifo
2741          * is difficult, so we wait up to 12 character times for the fifo
2742          * to drain.  The command will be delayed for up to 2 character
2743          * times for the tx to become empty.  Unputting characters from
2744          * the tx holding and shift registers is impossible, so we wait
2745          * for the tx to become empty so that the command is sure to be
2746          * executed soon after we issue it.
2747          */
2748         disable_intr();
2749         if (com->etc == etc) {
2750                 enable_intr();
2751                 goto wait;
2752         }
2753         if ((etc == CD1400_ETC_SENDBREAK
2754             && (com->etc == ETC_BREAK_STARTING
2755                 || com->etc == ETC_BREAK_STARTED))
2756             || (etc == CD1400_ETC_STOPBREAK
2757                && (com->etc == ETC_BREAK_ENDING || com->etc == ETC_BREAK_ENDED
2758                    || com->etc == ETC_NONE))) {
2759                 enable_intr();
2760                 return;
2761         }
2762         com->etc = etc;
2763         cd_setreg(com, CD1400_SRER,
2764                   com->intr_enable
2765                   = (com->intr_enable & ~CD1400_SRER_TXRDY) | CD1400_SRER_TXMPTY);
2766         enable_intr();
2767 wait:
2768         while (com->etc == etc
2769                && tsleep(&com->etc, TTIPRI | PCATCH, "cyetc", 0) == 0)
2770                 continue;
2771 }
2772
2773 static int
2774 cd_getreg(com, reg)
2775         struct com_s    *com;
2776         int             reg;
2777 {
2778         struct com_s    *basecom;
2779         u_char  car;
2780         int     cy_align;
2781         u_long  ef;
2782         cy_addr iobase;
2783         int     val;
2784
2785         basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2786         car = com->unit & CD1400_CAR_CHAN;
2787         cy_align = com->cy_align;
2788         iobase = com->iobase;
2789         ef = read_eflags();
2790         if (ef & PSL_I)
2791                 disable_intr();
2792         if (basecom->car != car)
2793                 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2794         val = cd_inb(iobase, reg, cy_align);
2795         if (ef & PSL_I)
2796                 enable_intr();
2797         return (val);
2798 }
2799
2800 static void
2801 cd_setreg(com, reg, val)
2802         struct com_s    *com;
2803         int             reg;
2804         int             val;
2805 {
2806         struct com_s    *basecom;
2807         u_char  car;
2808         int     cy_align;
2809         u_long  ef;
2810         cy_addr iobase;
2811
2812         basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2813         car = com->unit & CD1400_CAR_CHAN;
2814         cy_align = com->cy_align;
2815         iobase = com->iobase;
2816         ef = read_eflags();
2817         if (ef & PSL_I)
2818                 disable_intr();
2819         if (basecom->car != car)
2820                 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2821         cd_outb(iobase, reg, cy_align, val);
2822         if (ef & PSL_I)
2823                 enable_intr();
2824 }
2825
2826 #ifdef CyDebug
2827 /* useful in ddb */
2828 void
2829 cystatus(unit)
2830         int     unit;
2831 {
2832         struct com_s    *com;
2833         cy_addr         iobase;
2834         u_int           ocount;
2835         struct tty      *tp;
2836
2837         com = com_addr(unit);
2838         printf("info for channel %d\n", unit);
2839         printf("------------------\n");
2840         printf("total cyclom service probes:\t%d\n", cy_svrr_probes);
2841         printf("calls to upper layer:\t\t%d\n", cy_timeouts);
2842         if (com == NULL)
2843                 return;
2844         iobase = com->iobase;
2845         printf("\n");
2846         printf("cd1400 base address:\\tt%p\n", iobase);
2847         printf("saved channel_control:\t\t0x%02x\n", com->channel_control);
2848         printf("saved cor1-3:\t\t\t0x%02x 0x%02x 0x%02x\n",
2849                com->cor[0], com->cor[1], com->cor[2]);
2850         printf("service request enable reg:\t0x%02x (0x%02x cached)\n",
2851                cd_getreg(com, CD1400_SRER), com->intr_enable);
2852         printf("service request register:\t0x%02x\n",
2853                cd_inb(iobase, CD1400_SVRR, com->cy_align));
2854         printf("modem status:\t\t\t0x%02x (0x%02x cached)\n",
2855                cd_getreg(com, CD1400_MSVR2), com->prev_modem_status);
2856         printf("rx/tx/mdm interrupt registers:\t0x%02x 0x%02x 0x%02x\n",
2857                cd_inb(iobase, CD1400_RIR, com->cy_align),
2858                cd_inb(iobase, CD1400_TIR, com->cy_align),
2859                cd_inb(iobase, CD1400_MIR, com->cy_align));
2860         printf("\n");
2861         printf("com state:\t\t\t0x%02x\n", com->state);
2862         printf("calls to comstart():\t\t%d (%d useful)\n",
2863                com->start_count, com->start_real);
2864         printf("rx buffer chars free:\t\t%d\n", com->iptr - com->ibuf);
2865         ocount = 0;
2866         if (com->obufs[0].l_queued)
2867                 ocount += com->obufs[0].l_tail - com->obufs[0].l_head;
2868         if (com->obufs[1].l_queued)
2869                 ocount += com->obufs[1].l_tail - com->obufs[1].l_head;
2870         printf("tx buffer chars:\t\t%u\n", ocount);
2871         printf("received chars:\t\t\t%d\n", com->bytes_in);
2872         printf("received exceptions:\t\t%d\n", com->recv_exception);
2873         printf("modem signal deltas:\t\t%d\n", com->mdm);
2874         printf("transmitted chars:\t\t%d\n", com->bytes_out);
2875         printf("\n");
2876         tp = com->tp;
2877         if (tp != NULL) {
2878                 printf("tty state:\t\t\t0x%08x\n", tp->t_state);
2879                 printf(
2880                 "upper layer queue lengths:\t%d raw, %d canon, %d output\n",
2881                        tp->t_rawq.c_cc, tp->t_canq.c_cc, tp->t_outq.c_cc);
2882         } else
2883                 printf("tty state:\t\t\tclosed\n");
2884 }
2885 #endif /* CyDebug */