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