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