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