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