4 * cyclades cyclom-y serial driver
5 * Andrew Herbert <andrew@werple.apana.org.au>, 17 August 1993
7 * Copyright (c) 1993 Andrew Herbert.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 * $FreeBSD: src/sys/i386/isa/cy.c,v 1.97.2.2 2001/08/22 13:04:58 bde Exp $
35 #include "opt_compat.h"
45 * Temporary compile-time configuration options.
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
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.
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().
63 * XXX not fully implemented, and not particularly
66 #undef CyDebug /* Include debugging code (not very expensive). */
68 /* These will go away. */
72 #include <sys/param.h>
73 #include <sys/systm.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>
89 #include <machine_base/isa/ic/cd1400.h>
91 #define disable_intr() com_lock()
92 #define enable_intr() com_unlock()
95 * Dictionary so that I can name everything *sio* or *com* to compare with
96 * sio.c. There is also lots of ugly formatting and unnecessary ifdefs to
97 * simplify the comparision. These will go away.
99 #define LSR_BI CD1400_RDSR_BREAK
100 #define LSR_FE CD1400_RDSR_FE
101 #define LSR_OE CD1400_RDSR_OE
102 #define LSR_PE CD1400_RDSR_PE
103 #define MCR_DTR CD1400_MSVR2_DTR
104 #define MCR_RTS CD1400_MSVR1_RTS
105 #define MSR_CTS CD1400_MSVR2_CTS
106 #define MSR_DCD CD1400_MSVR2_CD
107 #define MSR_DSR CD1400_MSVR2_DSR
108 #define MSR_RI CD1400_MSVR2_RI
109 #define NSIO (NCY * CY_MAX_PORTS)
110 #define comconsole cyconsole
111 #define comdefaultrate cydefaultrate
112 #define com_events cy_events
113 #define comhardclose cyhardclose
114 #define commctl cymctl
115 #define comparam cyparam
116 #define comspeed cyspeed
117 #define comstart cystart
118 #define comwakeup cywakeup
119 #define nsio_tty ncy_tty
120 #define p_com_addr p_cy_addr
121 #define sioattach cyattach
122 #define sioclose cyclose
123 #define siodriver cydriver
124 #define siodtrwakeup cydtrwakeup
125 #define sioinput cyinput
126 #define siointr cyintr
127 #define siointr1 cyintr1
128 #define sioioctl cyioctl
129 #define sioopen cyopen
130 #define siopoll cypoll
131 #define sioprobe cyprobe
132 #define siosettimeout cysettimeout
133 #define siosetwater cysetwater
134 #define comstop cystop
135 #define siowrite cywrite
136 #define sio_registered cy_registered
137 #define sio_timeout cy_timeout
138 #define sio_timeout_handle cy_timeout_handle
139 #define sio_timeouts_until_log cy_timeouts_until_log
140 #define sio_tty cy_tty
142 #define CY_MAX_PORTS (CD1400_NO_OF_CHANNELS * CY_MAX_CD1400s)
144 /* We encode the cyclom unit number (cyu) in spare bits in the IVR's. */
145 #define CD1400_xIVR_CHAN_SHIFT 3
146 #define CD1400_xIVR_CHAN 0x1F
149 * ETC states. com->etc may also contain a hardware ETC command value,
150 * meaning that execution of that command is pending.
152 #define ETC_NONE 0 /* we depend on bzero() setting this */
153 #define ETC_BREAK_STARTING 1
154 #define ETC_BREAK_STARTED 2
155 #define ETC_BREAK_ENDING 3
156 #define ETC_BREAK_ENDED 4
158 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */
160 #define CALLOUT_MASK 0x80
161 #define CONTROL_MASK 0x60
162 #define CONTROL_INIT_STATE 0x20
163 #define CONTROL_LOCK_STATE 0x40
164 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
165 #define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK)
167 * Not all of the magic is parametrized in the following macros. 16 and
168 * 0xff are related to the bitfields in a udev_t. CY_MAX_PORTS must be
169 * ((0xff & ~MINOR_MAGIC_MASK) + 1) for things to work.
171 #define MINOR_TO_UNIT(mynor) (((mynor) >> 16) * CY_MAX_PORTS \
172 | (((mynor) & 0xff) & ~MINOR_MAGIC_MASK))
173 #define UNIT_TO_MINOR(unit) (((unit) / CY_MAX_PORTS) << 16 \
174 | (((unit) & 0xff) & ~MINOR_MAGIC_MASK))
178 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
179 * than the other bits so that they can be tested as a group without masking
182 * The following com and tty flags correspond closely:
183 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and
185 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart())
186 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam())
187 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam())
188 * TS_FLUSH is not used.
189 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
190 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
192 #define CS_BUSY 0x80 /* output in progress */
193 #define CS_TTGO 0x40 /* output not stopped by XOFF */
194 #define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */
195 #define CS_CHECKMSR 1 /* check of MSR scheduled */
196 #define CS_CTS_OFLOW 2 /* use CTS output flow control */
197 #define CS_DTR_OFF 0x10 /* DTR held off */
198 #define CS_ODONE 4 /* output completed */
199 #define CS_RTS_IFLOW 8 /* use RTS input flow control */
200 #define CSE_ODONE 1 /* output transmitted */
202 static char const * const error_desc[] = {
205 #define CE_INTERRUPT_BUF_OVERFLOW 1
206 "interrupt-level buffer overflow",
207 #define CE_TTY_BUF_OVERFLOW 2
208 "tty-level buffer overflow",
212 #define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum])
214 /* types. XXX - should be elsewhere */
215 typedef u_char bool_t; /* boolean */
216 typedef u_char volatile *cy_addr;
218 /* queue of linear buffers */
220 u_char *l_head; /* next char to process */
221 u_char *l_tail; /* one past the last char to process */
222 struct lbq *l_next; /* next in queue */
223 bool_t l_queued; /* nonzero if queued */
226 /* com device structure */
228 u_char state; /* miscellaneous flag bits */
229 bool_t active_out; /* nonzero if the callout device is open */
231 u_char cfcr_image; /* copy of value written to CFCR */
233 u_char etc; /* pending Embedded Transmit Command */
234 u_char extra_state; /* more flag bits, separate for order trick */
236 u_char fifo_image; /* copy of value written to FIFO */
238 u_char gfrcr_image; /* copy of value read from GFRCR */
240 bool_t hasfifo; /* nonzero for 16550 UARTs */
241 bool_t loses_outints; /* nonzero if device loses output interrupts */
243 u_char mcr_dtr; /* MCR bit that is wired to DTR */
244 u_char mcr_image; /* copy of value written to MCR */
245 u_char mcr_rts; /* MCR bit that is wired to RTS */
248 bool_t multiport; /* is this unit part of a multiport device? */
249 #endif /* COM_MULTIPORT */
250 bool_t no_irq; /* nonzero if irq is not attached */
251 bool_t poll; /* nonzero if polling is required */
252 bool_t poll_output; /* nonzero if polling for output is required */
254 int unit; /* unit number */
255 int dtr_wait; /* time to hold DTR down on close (* 1/hz) */
256 struct callout dtr_ch;
260 u_int wopeners; /* # processes waiting for DCD in open() */
263 * The high level of the driver never reads status registers directly
264 * because there would be too many side effects to handle conveniently.
265 * Instead, it reads copies of the registers stored here by the
268 u_char last_modem_status; /* last MSR read by intr handler */
269 u_char prev_modem_status; /* last MSR handled by high level */
271 u_char hotchar; /* ldisc-specific char to be handled ASAP */
272 u_char *ibuf; /* start of input buffer */
273 u_char *ibufend; /* end of input buffer */
274 u_char *ibufold; /* old input buffer, to be freed */
275 u_char *ihighwater; /* threshold in input buffer */
276 u_char *iptr; /* next free spot in input buffer */
277 int ibufsize; /* size of ibuf (not include error bytes) */
278 int ierroff; /* offset of error bytes in ibuf */
280 struct lbq obufq; /* head of queue of output buffers */
281 struct lbq obufs[2]; /* output buffers */
283 int cy_align; /* index for register alignment */
284 cy_addr cy_iobase; /* base address of this port's cyclom */
285 cy_addr iobase; /* base address of this port's cd1400 */
286 int mcr_rts_reg; /* cd1400 reg number of reg holding mcr_rts */
288 struct tty *tp; /* cross reference */
291 struct termios it_in; /* should be in struct tty */
292 struct termios it_out;
295 struct termios lt_in; /* should be in struct tty */
296 struct termios lt_out;
299 bool_t do_dcd_timestamp;
300 struct timeval timestamp;
301 struct timeval dcd_timestamp;
303 u_long bytes_in; /* statistics */
305 u_int delta_error_counts[CE_NTYPES];
306 u_long error_counts[CE_NTYPES];
308 u_int recv_exception; /* exception chars received */
309 u_int mdm; /* modem signal changes */
311 u_int start_count; /* no. of calls to comstart() */
312 u_int start_real; /* no. of calls that did something */
314 u_char car; /* CD1400 CAR shadow (if first unit in cd) */
315 u_char channel_control;/* CD1400 CCR control command shadow */
316 u_char cor[3]; /* CD1400 COR1-3 shadows */
317 u_char intr_enable; /* CD1400 SRER shadow */
320 * Data area for output buffers. Someday we should build the output
321 * buffer queue without copying data.
327 /* PCI driver entry point. */
328 int cyattach_common (cy_addr cy_iobase, int cy_align);
329 void siointr(void *);
331 static int cy_units (cy_addr cy_iobase, int cy_align);
332 static void cd1400_channel_cmd (struct com_s *com, int cmd);
333 static void cd1400_channel_cmd_wait (struct com_s *com);
334 static void cd_etc (struct com_s *com, int etc);
335 static int cd_getreg (struct com_s *com, int reg);
336 static void cd_setreg (struct com_s *com, int reg, int val);
337 static timeout_t siodtrwakeup;
338 static void comhardclose (struct com_s *com);
339 static void sioinput (struct com_s *com);
341 static void siointr1 (struct com_s *com);
343 static int commctl (struct com_s *com, int bits, int how);
344 static int comparam (struct tty *tp, struct termios *t);
345 static inthand2_t siopoll;
346 static void siosettimeout (void);
347 static int siosetwater (struct com_s *com, speed_t speed);
348 static int comspeed (speed_t speed, u_long cy_clock,
350 static void comstart (struct tty *tp);
351 static void comstop (struct tty *tp, int rw);
352 static timeout_t comwakeup;
353 static void disc_optim (struct tty *tp, struct termios *t,
357 void cystatus (int unit);
360 static char driver_name[] = "cy";
362 /* table and macro for fast conversion from a unit number to its com struct */
363 static struct com_s *p_com_addr[NSIO];
364 #define com_addr(unit) (p_com_addr[unit])
366 static d_open_t sioopen;
367 static d_close_t sioclose;
368 static d_write_t siowrite;
369 static d_ioctl_t sioioctl;
371 static struct dev_ops sio_ops = {
372 { driver_name, 0, D_TTY },
378 .d_kqfilter = ttykqfilter,
379 .d_revoke = ttyrevoke
382 static int comconsole = -1;
383 static speed_t comdefaultrate = TTYDEF_SPEED;
384 static u_int com_events; /* input chars + weighted output completions */
385 static bool_t sio_registered;
386 static int sio_timeout;
387 static int sio_timeouts_until_log;
388 static struct callout sio_timeout_handle;
390 static struct tty *sio_tty[NSIO];
392 static struct tty sio_tty[NSIO];
394 static const int nsio_tty = NSIO;
397 static u_int cd_inbs;
398 static u_int cy_inbs;
399 static u_int cd_outbs;
400 static u_int cy_outbs;
401 static u_int cy_svrr_probes;
402 static u_int cy_timeouts;
405 static int cy_chip_offset[] = {
406 0x0000, 0x0400, 0x0800, 0x0c00, 0x0200, 0x0600, 0x0a00, 0x0e00,
408 static int cy_nr_cd1400s[NCY];
409 static int cy_total_devices;
410 #undef RxFifoThreshold
411 static int volatile RxFifoThreshold = (CD1400_RX_FIFO_SIZE / 2);
414 cy_units(cy_addr cy_iobase, int cy_align)
417 u_char firmware_version = 0; /* assign to avoid warning */
421 for (cyu = 0; cyu < CY_MAX_CD1400s; ++cyu) {
422 iobase = cy_iobase + (cy_chip_offset[cyu] << cy_align);
424 /* wait for chip to become ready for new command */
425 for (i = 0; i < 10; i++) {
427 if (!cd_inb(iobase, CD1400_CCR, cy_align))
431 /* clear the GFRCR register */
432 cd_outb(iobase, CD1400_GFRCR, cy_align, 0);
434 /* issue a reset command */
435 cd_outb(iobase, CD1400_CCR, cy_align,
436 CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
438 /* wait for the CD1400 to initialize itself */
439 for (i = 0; i < 200; i++) {
442 /* retrieve firmware version */
443 firmware_version = cd_inb(iobase, CD1400_GFRCR,
445 if ((firmware_version & 0xf0) == 0x40)
450 * Anything in the 0x40-0x4F range is fine.
451 * If one CD1400 is bad then we don't support higher
452 * numbered good ones on this board.
454 if ((firmware_version & 0xf0) != 0x40)
461 * NOTE: Must be called with tty_token held
464 cyattach_common(cy_addr cy_iobase, int cy_align)
468 u_char firmware_version;
474 ASSERT_LWKT_TOKEN_HELD(&tty_token);
476 adapter = cy_total_devices;
477 if ((u_int)adapter >= NCY) {
479 "cy%d: can't attach adapter: insufficient cy devices configured\n",
483 ncyu = cy_units(cy_iobase, cy_align);
486 cy_nr_cd1400s[adapter] = ncyu;
489 unit = adapter * CY_MAX_PORTS;
490 for (cyu = 0; cyu < ncyu; ++cyu) {
493 iobase = (cy_addr) (cy_iobase
494 + (cy_chip_offset[cyu] << cy_align));
495 firmware_version = cd_inb(iobase, CD1400_GFRCR, cy_align);
497 /* Set up a receive timeout period of than 1+ ms. */
498 cd_outb(iobase, CD1400_PPR, cy_align,
499 howmany(CY_CLOCK(firmware_version)
500 / CD1400_PPR_PRESCALER, 1000));
502 for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; ++cdu, ++unit) {
505 com = kmalloc(sizeof *com, M_DEVBUF, M_WAITOK | M_ZERO);
507 callout_init_mp(&com->dtr_ch);
508 com->gfrcr_image = firmware_version;
509 if (CY_RTS_DTR_SWAPPED(firmware_version)) {
510 com->mcr_dtr = MCR_RTS;
511 com->mcr_rts = MCR_DTR;
512 com->mcr_rts_reg = CD1400_MSVR2;
514 com->mcr_dtr = MCR_DTR;
515 com->mcr_rts = MCR_RTS;
516 com->mcr_rts_reg = CD1400_MSVR1;
518 com->dtr_wait = 3 * hz;
519 com->obufs[0].l_head = com->obuf1;
520 com->obufs[1].l_head = com->obuf2;
522 com->cy_align = cy_align;
523 com->cy_iobase = cy_iobase;
524 com->iobase = iobase;
525 com->car = ~CD1400_CAR_CHAN;
528 * We don't use all the flags from <sys/ttydefaults.h> since they
529 * are only relevant for logins. It's important to have echo off
530 * initially so that the line doesn't start blathering before the
531 * echo flag can be turned off.
533 com->it_in.c_iflag = 0;
534 com->it_in.c_oflag = 0;
535 com->it_in.c_cflag = TTYDEF_CFLAG;
536 com->it_in.c_lflag = 0;
537 if (unit == comconsole) {
538 com->it_in.c_iflag = TTYDEF_IFLAG;
539 com->it_in.c_oflag = TTYDEF_OFLAG;
540 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
541 com->it_in.c_lflag = TTYDEF_LFLAG;
542 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
544 if (siosetwater(com, com->it_in.c_ispeed) != 0) {
546 kfree(com, M_DEVBUF);
550 termioschars(&com->it_in);
551 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
552 com->it_out = com->it_in;
555 com_addr(unit) = com;
558 if (!sio_registered) {
559 callout_init_mp(&sio_timeout_handle);
560 register_swi_mp(SWI_TTY, siopoll, NULL, "cy", NULL, -1);
561 sio_registered = TRUE;
563 minorbase = UNIT_TO_MINOR(unit);
564 make_dev(&sio_ops, minorbase,
565 UID_ROOT, GID_WHEEL, 0600, "ttyc%r%r", adapter,
566 unit % CY_MAX_PORTS);
567 make_dev(&sio_ops, minorbase | CONTROL_INIT_STATE,
568 UID_ROOT, GID_WHEEL, 0600, "ttyic%r%r", adapter,
569 unit % CY_MAX_PORTS);
570 make_dev(&sio_ops, minorbase | CONTROL_LOCK_STATE,
571 UID_ROOT, GID_WHEEL, 0600, "ttylc%r%r", adapter,
572 unit % CY_MAX_PORTS);
573 make_dev(&sio_ops, minorbase | CALLOUT_MASK,
574 UID_UUCP, GID_DIALER, 0660, "cuac%r%r", adapter,
575 unit % CY_MAX_PORTS);
576 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_INIT_STATE,
577 UID_UUCP, GID_DIALER, 0660, "cuaic%r%r", adapter,
578 unit % CY_MAX_PORTS);
579 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE,
580 UID_UUCP, GID_DIALER, 0660, "cualc%r%r", adapter,
581 unit % CY_MAX_PORTS);
583 /* for(cdu...), for(cyu...) terminating blocks */
587 /* ensure an edge for the next interrupt */
588 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
594 sioopen(struct dev_open_args *ap)
596 cdev_t dev = ap->a_head.a_dev;
604 unit = MINOR_TO_UNIT(mynor);
605 if ((u_int) unit >= NSIO || (com = com_addr(unit)) == NULL)
607 if (mynor & CONTROL_MASK)
610 lwkt_gettoken(&tty_token);
612 tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
614 tp = com->tp = &sio_tty[unit];
619 * We jump to this label after all non-interrupted sleeps to pick
620 * up any changes of the device state.
623 while (com->state & CS_DTR_OFF) {
624 error = tsleep(&com->dtr_wait, PCATCH, "cydtr", 0);
628 if (tp->t_state & TS_ISOPEN) {
630 * The device is open, so everything has been initialized.
633 if (mynor & CALLOUT_MASK) {
634 if (!com->active_out) {
639 if (com->active_out) {
640 if (ap->a_oflags & O_NONBLOCK) {
644 error = tsleep(&com->active_out,
651 if (tp->t_state & TS_XCLUDE &&
652 priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
658 * The device isn't open, so there are no conflicts.
659 * Initialize it. Initialization is done twice in many
660 * cases: to preempt sleeping callin opens if we are
661 * callout, and to complete a callin open after DCD rises.
663 tp->t_oproc = comstart;
664 tp->t_stop = comstop;
665 tp->t_param = comparam;
667 tp->t_termios = mynor & CALLOUT_MASK
668 ? com->it_out : com->it_in;
670 /* Encode per-board unit in LIVR for access in intr routines. */
671 cd_setreg(com, CD1400_LIVR,
672 (unit & CD1400_xIVR_CHAN) << CD1400_xIVR_CHAN_SHIFT);
674 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
676 com->poll = com->no_irq;
677 com->poll_output = com->loses_outints;
680 error = comparam(tp, &tp->t_termios);
687 * (Re)enable and flush fifos.
689 * Certain SMC chips cause problems if the fifos
690 * are enabled while input is ready. Turn off the
691 * fifo if necessary to clear the input. We test
692 * the input ready bit after enabling the fifos
693 * since we've already enabled them in comparam()
694 * and to handle races between enabling and fresh
698 outb(iobase + com_fifo,
699 FIFO_RCV_RST | FIFO_XMT_RST
702 if (!(inb(com->line_status_port) & LSR_RXRDY))
704 outb(iobase + com_fifo, 0);
706 (void) inb(com->data_port);
711 (void) inb(com->line_status_port);
712 (void) inb(com->data_port);
713 com->prev_modem_status = com->last_modem_status
714 = inb(com->modem_status_port);
715 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS
720 * Flush fifos. This requires a full channel reset which
721 * also disables the transmitter and receiver. Recover
724 cd1400_channel_cmd(com,
725 CD1400_CCR_CMDRESET | CD1400_CCR_CHANRESET);
726 cd1400_channel_cmd(com, com->channel_control);
729 com->prev_modem_status = com->last_modem_status
730 = cd_getreg(com, CD1400_MSVR2);
731 cd_setreg(com, CD1400_SRER,
733 = CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
737 * Handle initial DCD. Callout devices get a fake initial
738 * DCD (trapdoor DCD). If we are callout, then any sleeping
739 * callin opens get woken up and resume sleeping on "cybi"
740 * instead of "cydcd".
743 * XXX `mynor & CALLOUT_MASK' should be
744 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
745 * TRAPDOOR_CARRIER is the default initial state for callout
746 * devices and SOFT_CARRIER is like CLOCAL except it hides
749 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
750 (*linesw[tp->t_line].l_modem)(tp, 1);
753 * Wait for DCD if necessary.
755 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
756 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) {
758 error = tsleep(TSA_CARR_ON(tp), PCATCH, "cydcd", 0);
764 error = (*linesw[tp->t_line].l_open)(dev, tp);
765 disc_optim(tp, &tp->t_termios, com);
766 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
767 com->active_out = TRUE;
771 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
773 lwkt_reltoken(&tty_token);
778 * NOTE: Must be called with tty_token held
781 sioclose(struct dev_close_args *ap)
783 cdev_t dev = ap->a_head.a_dev;
788 ASSERT_LWKT_TOKEN_HELD(&tty_token);
790 if (mynor & CONTROL_MASK)
792 lwkt_gettoken(&tty_token);
793 com = com_addr(MINOR_TO_UNIT(mynor));
796 cd_etc(com, CD1400_ETC_STOPBREAK);
797 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
798 disc_optim(tp, &tp->t_termios, com);
799 comstop(tp, FREAD | FWRITE);
804 #ifdef broken /* session holds a ref to the tty; can't deallocate */
806 com->tp = sio_tty[unit] = NULL;
808 lwkt_reltoken(&tty_token);
813 * NOTE: Must be called with tty_token held
816 comhardclose(struct com_s *com)
820 ASSERT_LWKT_TOKEN_HELD(&tty_token);
825 com->poll_output = FALSE;
827 com->do_timestamp = 0;
829 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
834 cd_setreg(com, CD1400_COR2, com->cor[1] &= ~CD1400_COR2_ETC);
836 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
841 outb(iobase + com_ier, 0);
844 cd_setreg(com, CD1400_SRER, com->intr_enable = 0);
848 if ((tp->t_cflag & HUPCL)
850 * XXX we will miss any carrier drop between here and the
851 * next open. Perhaps we should watch DCD even when the
852 * port is closed; it is not sufficient to check it at
853 * the next open because it might go up and down while
854 * we're not watching.
857 && !(com->prev_modem_status & MSR_DCD)
858 && !(com->it_in.c_cflag & CLOCAL))
859 || !(tp->t_state & TS_ISOPEN)) {
860 (void)commctl(com, TIOCM_DTR, DMBIC);
862 /* Disable receiver (leave transmitter enabled). */
863 com->channel_control = CD1400_CCR_CMDCHANCTL
866 cd1400_channel_cmd(com, com->channel_control);
868 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
869 callout_reset(&com->dtr_ch, com->dtr_wait,
871 com->state |= CS_DTR_OFF;
878 * Disable fifos so that they are off after controlled
879 * reboots. Some BIOSes fail to detect 16550s when the
882 outb(iobase + com_fifo, 0);
885 com->active_out = FALSE;
886 wakeup(&com->active_out);
887 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */
892 siowrite(struct dev_write_args *ap)
894 cdev_t dev = ap->a_head.a_dev;
895 struct uio *uio = ap->a_uio;
901 if (mynor & CONTROL_MASK)
904 lwkt_gettoken(&tty_token);
905 unit = MINOR_TO_UNIT(mynor);
906 tp = com_addr(unit)->tp;
908 * (XXX) We disallow virtual consoles if the physical console is
909 * a serial port. This is in case there is a display attached that
910 * is not the console. In that situation we don't need/want the X
911 * server taking over the console.
913 if (constty != NULL && unit == comconsole)
916 /* XXX duplicate ttwrite(), but without so much output processing on
917 * CR & LF chars. Hardly worth the effort, given that high-throughput
918 * sessions are raw anyhow.
921 return ((*linesw[tp->t_line].l_write)(tp, uio, ap->a_ioflag));
923 lwkt_reltoken(&tty_token);
927 siodtrwakeup(void *chan)
931 lwkt_gettoken(&tty_token);
932 com = (struct com_s *)chan;
933 com->state &= ~CS_DTR_OFF;
934 wakeup(&com->dtr_wait);
935 lwkt_reltoken(&tty_token);
939 * NOTE: Must be called with tty_token held
942 sioinput(struct com_s *com)
950 ASSERT_LWKT_TOKEN_HELD(&tty_token);
954 if (!(tp->t_state & TS_ISOPEN)) {
955 com_events -= (com->iptr - com->ibuf);
956 com->iptr = com->ibuf;
959 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
961 * Avoid the grotesquely inefficient lineswitch routine
962 * (ttyinput) in "raw" mode. It usually takes about 450
963 * instructions (that's without canonical processing or echo!).
964 * slinput is reasonably fast (usually 40 instructions plus
969 incc = com->iptr - buf;
970 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
971 && (com->state & CS_RTS_IFLOW
972 || tp->t_iflag & IXOFF)
973 && !(tp->t_state & TS_TBLOCK))
975 com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
976 += b_to_q((char *)buf, incc, &tp->t_rawq);
982 if (tp->t_state & TS_TTSTOP
983 && (tp->t_iflag & IXANY
984 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
985 tp->t_state &= ~TS_TTSTOP;
986 tp->t_lflag &= ~FLUSHO;
990 } while (buf < com->iptr);
994 line_status = buf[com->ierroff];
997 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
998 if (line_status & LSR_BI)
1000 if (line_status & LSR_FE)
1001 recv_data |= TTY_FE;
1002 if (line_status & LSR_OE)
1003 recv_data |= TTY_OE;
1004 if (line_status & LSR_PE)
1005 recv_data |= TTY_PE;
1007 (*linesw[tp->t_line].l_rint)(recv_data, tp);
1009 } while (buf < com->iptr);
1011 com_events -= (com->iptr - com->ibuf);
1012 com->iptr = com->ibuf;
1015 * There is now room for another low-level buffer full of input,
1016 * so enable RTS if it is now disabled and there is room in the
1017 * high-level buffer.
1019 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & com->mcr_rts) &&
1020 !(tp->t_state & TS_TBLOCK))
1022 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1024 cd_setreg(com, com->mcr_rts_reg,
1025 com->mcr_image |= com->mcr_rts);
1032 int unit = (int)arg;
1040 lwkt_gettoken(&tty_token);
1041 com_lock(); /* XXX could this be placed down lower in the loop? */
1043 baseu = unit * CY_MAX_PORTS;
1044 cy_align = com_addr(baseu)->cy_align;
1045 cy_iobase = com_addr(baseu)->cy_iobase;
1047 /* check each CD1400 in turn */
1048 for (cyu = 0; cyu < cy_nr_cd1400s[unit]; ++cyu) {
1049 iobase = (cy_addr) (cy_iobase
1050 + (cy_chip_offset[cyu] << cy_align));
1051 /* poll to see if it has any work */
1052 status = cd_inb(iobase, CD1400_SVRR, cy_align);
1058 /* service requests as appropriate, giving priority to RX */
1059 if (status & CD1400_SVRR_RXRDY) {
1071 save_rir = cd_inb(iobase, CD1400_RIR, cy_align);
1073 /* enter rx service */
1074 cd_outb(iobase, CD1400_CAR, cy_align, save_rir);
1075 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1076 = save_rir & CD1400_CAR_CHAN;
1078 serv_type = cd_inb(iobase, CD1400_RIVR, cy_align);
1079 com = com_addr(baseu
1080 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1081 & CD1400_xIVR_CHAN));
1083 /* ack receive service */
1084 serv_type = cy_inb(iobase, CY8_SVCACKR, cy_align);
1086 com = com_addr(baseu +
1087 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1088 & CD1400_xIVR_CHAN));
1091 if (serv_type & CD1400_RIVR_EXCEPTION) {
1092 ++com->recv_exception;
1093 line_status = cd_inb(iobase, CD1400_RDSR, cy_align);
1094 /* break/unnattached error bits or real input? */
1095 recv_data = cd_inb(iobase, CD1400_RDSR, cy_align);
1096 #ifndef SOFT_HOTCHAR
1097 if (line_status & CD1400_RDSR_SPECIAL
1098 && com->hotchar != 0)
1101 #if 1 /* XXX "intelligent" PFO error handling would break O error handling */
1102 if (line_status & (LSR_PE|LSR_FE|LSR_BI)) {
1104 Don't store PE if IGNPAR and BI if IGNBRK,
1105 this hack allows "raw" tty optimization
1106 works even if IGN* is set.
1108 if ( com->tp == NULL
1109 || !(com->tp->t_state & TS_ISOPEN)
1110 || ((line_status & (LSR_PE|LSR_FE))
1111 && (com->tp->t_iflag & IGNPAR))
1112 || ((line_status & LSR_BI)
1113 && (com->tp->t_iflag & IGNBRK)))
1115 if ( (line_status & (LSR_PE|LSR_FE))
1116 && (com->tp->t_state & TS_CAN_BYPASS_L_RINT)
1117 && ((line_status & LSR_FE)
1118 || ((line_status & LSR_PE)
1119 && (com->tp->t_iflag & INPCK))))
1125 if (com->hotchar != 0 && recv_data == com->hotchar)
1129 if (ioptr >= com->ibufend)
1130 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1132 if (com->do_timestamp)
1133 microtime(&com->timestamp);
1135 ioptr[0] = recv_data;
1136 ioptr[com->ierroff] = line_status;
1137 com->iptr = ++ioptr;
1138 if (ioptr == com->ihighwater
1139 && com->state & CS_RTS_IFLOW)
1141 outb(com->modem_ctl_port,
1142 com->mcr_image &= ~MCR_RTS);
1144 cd_outb(iobase, com->mcr_rts_reg,
1149 if (line_status & LSR_OE)
1150 CE_RECORD(com, CE_OVERRUN);
1156 count = cd_inb(iobase, CD1400_RDCR, cy_align);
1159 com->bytes_in += count;
1161 ifree = com->ibufend - ioptr;
1162 if (count > ifree) {
1164 com_events += ifree;
1166 if (com->do_timestamp)
1167 microtime(&com->timestamp);
1169 recv_data = cd_inb(iobase,
1173 if (com->hotchar != 0
1178 ioptr[0] = recv_data;
1179 ioptr[com->ierroff] = 0;
1181 } while (--ifree != 0);
1183 com->delta_error_counts
1184 [CE_INTERRUPT_BUF_OVERFLOW] += count;
1186 recv_data = cd_inb(iobase, CD1400_RDSR,
1189 if (com->hotchar != 0
1190 && recv_data == com->hotchar)
1193 } while (--count != 0);
1195 if (com->do_timestamp)
1196 microtime(&com->timestamp);
1197 if (ioptr <= com->ihighwater
1198 && ioptr + count > com->ihighwater
1199 && com->state & CS_RTS_IFLOW)
1201 outb(com->modem_ctl_port,
1202 com->mcr_image &= ~MCR_RTS);
1204 cd_outb(iobase, com->mcr_rts_reg,
1209 com_events += count;
1211 recv_data = cd_inb(iobase, CD1400_RDSR,
1214 if (com->hotchar != 0
1215 && recv_data == com->hotchar)
1218 ioptr[0] = recv_data;
1219 ioptr[com->ierroff] = 0;
1221 } while (--count != 0);
1227 /* terminate service context */
1229 cd_outb(iobase, CD1400_RIR, cy_align,
1231 & ~(CD1400_RIR_RDIREQ | CD1400_RIR_RBUSY));
1233 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1236 if (status & CD1400_SVRR_MDMCH) {
1238 u_char modem_status;
1246 save_mir = cd_inb(iobase, CD1400_MIR, cy_align);
1248 /* enter modem service */
1249 cd_outb(iobase, CD1400_CAR, cy_align, save_mir);
1250 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1251 = save_mir & CD1400_CAR_CHAN;
1253 com = com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS
1254 + (save_mir & CD1400_MIR_CHAN));
1256 /* ack modem service */
1257 vector = cy_inb(iobase, CY8_SVCACKM, cy_align);
1259 com = com_addr(baseu
1260 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1261 & CD1400_xIVR_CHAN));
1264 modem_status = cd_inb(iobase, CD1400_MSVR2, cy_align);
1265 if (modem_status != com->last_modem_status) {
1266 if (com->do_dcd_timestamp
1267 && !(com->last_modem_status & MSR_DCD)
1268 && modem_status & MSR_DCD)
1269 microtime(&com->dcd_timestamp);
1272 * Schedule high level to handle DCD changes. Note
1273 * that we don't use the delta bits anywhere. Some
1274 * UARTs mess them up, and it's easy to remember the
1275 * previous bits and calculate the delta.
1277 com->last_modem_status = modem_status;
1278 if (!(com->state & CS_CHECKMSR)) {
1279 com_events += LOTS_OF_EVENTS;
1280 com->state |= CS_CHECKMSR;
1284 #ifdef SOFT_CTS_OFLOW
1285 /* handle CTS change immediately for crisp flow ctl */
1286 if (com->state & CS_CTS_OFLOW) {
1287 if (modem_status & MSR_CTS) {
1288 com->state |= CS_ODEVREADY;
1289 if (com->state >= (CS_BUSY | CS_TTGO
1291 && !(com->intr_enable
1292 & CD1400_SRER_TXRDY))
1293 cd_outb(iobase, CD1400_SRER,
1297 & ~CD1400_SRER_TXMPTY
1298 | CD1400_SRER_TXRDY);
1300 com->state &= ~CS_ODEVREADY;
1301 if (com->intr_enable
1302 & CD1400_SRER_TXRDY)
1303 cd_outb(iobase, CD1400_SRER,
1307 & ~CD1400_SRER_TXRDY
1308 | CD1400_SRER_TXMPTY);
1314 /* terminate service context */
1316 cd_outb(iobase, CD1400_MIR, cy_align,
1318 & ~(CD1400_MIR_RDIREQ | CD1400_MIR_RBUSY));
1320 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1323 if (status & CD1400_SVRR_TXRDY) {
1332 save_tir = cd_inb(iobase, CD1400_TIR, cy_align);
1334 /* enter tx service */
1335 cd_outb(iobase, CD1400_CAR, cy_align, save_tir);
1336 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1337 = save_tir & CD1400_CAR_CHAN;
1339 com = com_addr(baseu
1340 + cyu * CD1400_NO_OF_CHANNELS
1341 + (save_tir & CD1400_TIR_CHAN));
1343 /* ack transmit service */
1344 vector = cy_inb(iobase, CY8_SVCACKT, cy_align);
1346 com = com_addr(baseu
1347 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1348 & CD1400_xIVR_CHAN));
1351 if (com->etc != ETC_NONE) {
1352 if (com->intr_enable & CD1400_SRER_TXRDY) {
1354 * Here due to sloppy SRER_TXRDY
1355 * enabling. Ignore. Come back when
1358 cd_outb(iobase, CD1400_SRER, cy_align,
1361 & ~CD1400_SRER_TXRDY)
1362 | CD1400_SRER_TXMPTY);
1363 goto terminate_tx_service;
1366 case CD1400_ETC_SENDBREAK:
1367 case CD1400_ETC_STOPBREAK:
1369 * Start the command. Come back on
1370 * next tx empty interrupt, hopefully
1371 * after command has been executed.
1373 cd_outb(iobase, CD1400_COR2, cy_align,
1374 com->cor[1] |= CD1400_COR2_ETC);
1375 cd_outb(iobase, CD1400_TDR, cy_align,
1377 cd_outb(iobase, CD1400_TDR, cy_align,
1379 if (com->etc == CD1400_ETC_SENDBREAK)
1380 com->etc = ETC_BREAK_STARTING;
1382 com->etc = ETC_BREAK_ENDING;
1383 goto terminate_tx_service;
1384 case ETC_BREAK_STARTING:
1386 * BREAK is now on. Continue with
1387 * SRER_TXMPTY processing, hopefully
1390 com->etc = ETC_BREAK_STARTED;
1392 case ETC_BREAK_STARTED:
1394 * Came back due to sloppy SRER_TXMPTY
1395 * enabling. Hope again.
1398 case ETC_BREAK_ENDING:
1400 * BREAK is now off. Continue with
1401 * SRER_TXMPTY processing and don't
1402 * come back. The SWI handler will
1403 * restart tx interrupts if necessary.
1405 cd_outb(iobase, CD1400_COR2, cy_align,
1407 &= ~CD1400_COR2_ETC);
1408 com->etc = ETC_BREAK_ENDED;
1409 if (!(com->state & CS_ODONE)) {
1410 com_events += LOTS_OF_EVENTS;
1411 com->state |= CS_ODONE;
1415 case ETC_BREAK_ENDED:
1417 * Shouldn't get here. Hope again.
1422 if (com->intr_enable & CD1400_SRER_TXMPTY) {
1423 if (!(com->extra_state & CSE_ODONE)) {
1424 com_events += LOTS_OF_EVENTS;
1425 com->extra_state |= CSE_ODONE;
1428 cd_outb(iobase, CD1400_SRER, cy_align,
1430 &= ~CD1400_SRER_TXMPTY);
1431 goto terminate_tx_service;
1433 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1437 ioptr = com->obufq.l_head;
1438 ocount = com->obufq.l_tail - ioptr;
1439 if (ocount > CD1400_TX_FIFO_SIZE)
1440 ocount = CD1400_TX_FIFO_SIZE;
1441 com->bytes_out += ocount;
1443 cd_outb(iobase, CD1400_TDR, cy_align,
1445 while (--ocount != 0);
1446 com->obufq.l_head = ioptr;
1447 if (ioptr >= com->obufq.l_tail) {
1450 qp = com->obufq.l_next;
1451 qp->l_queued = FALSE;
1454 com->obufq.l_head = qp->l_head;
1455 com->obufq.l_tail = qp->l_tail;
1456 com->obufq.l_next = qp;
1458 /* output just completed */
1459 com->state &= ~CS_BUSY;
1462 * The setting of CSE_ODONE may be
1463 * stale here. We currently only
1464 * use it when CS_BUSY is set, and
1465 * fixing it when we clear CS_BUSY
1468 if (com->extra_state & CSE_ODONE) {
1469 com_events -= LOTS_OF_EVENTS;
1470 com->extra_state &= ~CSE_ODONE;
1473 cd_outb(iobase, CD1400_SRER, cy_align,
1476 & ~CD1400_SRER_TXRDY)
1477 | CD1400_SRER_TXMPTY);
1479 if (!(com->state & CS_ODONE)) {
1480 com_events += LOTS_OF_EVENTS;
1481 com->state |= CS_ODONE;
1483 /* handle at high level ASAP */
1489 /* terminate service context */
1490 terminate_tx_service:
1492 cd_outb(iobase, CD1400_TIR, cy_align,
1494 & ~(CD1400_TIR_RDIREQ | CD1400_TIR_RBUSY));
1496 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1501 /* ensure an edge for the next interrupt */
1502 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
1507 lwkt_reltoken(&tty_token);
1512 siointr1(struct com_s *com)
1518 sioioctl(struct dev_ioctl_args *ap)
1520 cdev_t dev = ap->a_head.a_dev;
1521 caddr_t data = ap->a_data;
1522 u_long cmd = ap->a_cmd;
1527 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1529 struct termios term;
1532 lwkt_gettoken(&tty_token);
1534 com = com_addr(MINOR_TO_UNIT(mynor));
1535 if (mynor & CONTROL_MASK) {
1538 switch (mynor & CONTROL_MASK) {
1539 case CONTROL_INIT_STATE:
1540 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
1542 case CONTROL_LOCK_STATE:
1543 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
1546 lwkt_reltoken(&tty_token);
1547 return (ENODEV); /* /dev/nodev */
1551 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1553 lwkt_reltoken(&tty_token);
1556 *ct = *(struct termios *)data;
1557 lwkt_reltoken(&tty_token);
1560 *(struct termios *)data = *ct;
1561 lwkt_reltoken(&tty_token);
1564 *(int *)data = TTYDISC;
1565 lwkt_reltoken(&tty_token);
1568 bzero(data, sizeof(struct winsize));
1569 lwkt_reltoken(&tty_token);
1572 lwkt_reltoken(&tty_token);
1577 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1578 term = tp->t_termios;
1580 error = ttsetcompat(tp, &cmd, data, &term);
1582 lwkt_reltoken(&tty_token);
1586 data = (caddr_t)&term;
1588 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1590 struct termios *dt = (struct termios *)data;
1591 struct termios *lt = mynor & CALLOUT_MASK
1592 ? &com->lt_out : &com->lt_in;
1594 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
1595 | (dt->c_iflag & ~lt->c_iflag);
1596 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
1597 | (dt->c_oflag & ~lt->c_oflag);
1598 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
1599 | (dt->c_cflag & ~lt->c_cflag);
1600 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
1601 | (dt->c_lflag & ~lt->c_lflag);
1602 for (cc = 0; cc < NCCS; ++cc)
1603 if (lt->c_cc[cc] != 0)
1604 dt->c_cc[cc] = tp->t_cc[cc];
1605 if (lt->c_ispeed != 0)
1606 dt->c_ispeed = tp->t_ispeed;
1607 if (lt->c_ospeed != 0)
1608 dt->c_ospeed = tp->t_ospeed;
1610 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data,
1611 ap->a_fflag, ap->a_cred);
1612 if (error != ENOIOCTL) {
1613 lwkt_reltoken(&tty_token);
1617 error = ttioctl(tp, cmd, data, ap->a_fflag);
1618 disc_optim(tp, &tp->t_termios, com);
1619 if (error != ENOIOCTL) {
1621 lwkt_reltoken(&tty_token);
1627 outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
1629 cd_etc(com, CD1400_ETC_SENDBREAK);
1634 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1636 cd_etc(com, CD1400_ETC_STOPBREAK);
1640 (void)commctl(com, TIOCM_DTR, DMBIS);
1643 (void)commctl(com, TIOCM_DTR, DMBIC);
1646 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
1647 * changes get undone on the next call to comparam().
1650 (void)commctl(com, *(int *)data, DMSET);
1653 (void)commctl(com, *(int *)data, DMBIS);
1656 (void)commctl(com, *(int *)data, DMBIC);
1659 *(int *)data = commctl(com, 0, DMGET);
1662 /* must be root since the wait applies to following logins */
1663 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1666 lwkt_reltoken(&tty_token);
1669 com->dtr_wait = *(int *)data * hz / 100;
1672 *(int *)data = com->dtr_wait * 100 / hz;
1675 com->do_timestamp = TRUE;
1676 *(struct timeval *)data = com->timestamp;
1678 case TIOCDCDTIMESTAMP:
1679 com->do_dcd_timestamp = TRUE;
1680 *(struct timeval *)data = com->dcd_timestamp;
1684 lwkt_reltoken(&tty_token);
1688 lwkt_reltoken(&tty_token);
1693 siopoll(void *data, void *frame)
1697 lwkt_gettoken(&tty_token);
1701 if (com_events == 0) {
1702 lwkt_reltoken(&tty_token);
1706 for (unit = 0; unit < NSIO; ++unit) {
1711 com = com_addr(unit);
1717 * XXX forget any events related to closed devices
1718 * (actually never opened devices) so that we don't
1722 incc = com->iptr - com->ibuf;
1723 com->iptr = com->ibuf;
1724 if (com->state & CS_CHECKMSR) {
1725 incc += LOTS_OF_EVENTS;
1726 com->state &= ~CS_CHECKMSR;
1732 "sio%d: %d events for device with no tp\n",
1736 if (com->iptr != com->ibuf) {
1741 if (com->state & CS_CHECKMSR) {
1742 u_char delta_modem_status;
1745 delta_modem_status = com->last_modem_status
1746 ^ com->prev_modem_status;
1747 com->prev_modem_status = com->last_modem_status;
1748 com_events -= LOTS_OF_EVENTS;
1749 com->state &= ~CS_CHECKMSR;
1751 if (delta_modem_status & MSR_DCD)
1752 (*linesw[tp->t_line].l_modem)
1753 (tp, com->prev_modem_status & MSR_DCD);
1755 if (com->extra_state & CSE_ODONE) {
1757 com_events -= LOTS_OF_EVENTS;
1758 com->extra_state &= ~CSE_ODONE;
1760 if (!(com->state & CS_BUSY)) {
1761 tp->t_state &= ~TS_BUSY;
1764 if (com->etc != ETC_NONE) {
1765 if (com->etc == ETC_BREAK_ENDED)
1766 com->etc = ETC_NONE;
1770 if (com->state & CS_ODONE) {
1772 com_events -= LOTS_OF_EVENTS;
1773 com->state &= ~CS_ODONE;
1775 (*linesw[tp->t_line].l_start)(tp);
1777 if (com_events == 0)
1780 if (com_events >= LOTS_OF_EVENTS)
1782 lwkt_reltoken(&tty_token);
1786 comparam(struct tty *tp, struct termios *t)
1802 lwkt_gettoken(&tty_token);
1803 /* do historical conversions */
1804 if (t->c_ispeed == 0)
1805 t->c_ispeed = t->c_ospeed;
1807 unit = DEV_TO_UNIT(tp->t_dev);
1808 com = com_addr(unit);
1810 /* check requested parameters */
1811 cy_clock = CY_CLOCK(com->gfrcr_image);
1812 idivisor = comspeed(t->c_ispeed, cy_clock, &iprescaler);
1814 lwkt_reltoken(&tty_token);
1817 odivisor = comspeed(t->c_ospeed, cy_clock, &oprescaler);
1819 lwkt_reltoken(&tty_token);
1823 /* parameters are OK, convert them to the com struct and the device */
1826 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */
1828 (void)commctl(com, TIOCM_DTR, DMBIS);
1831 * This returns with interrupts disabled so that we can complete
1832 * the speed change atomically.
1834 (void) siosetwater(com, t->c_ispeed);
1836 /* XXX we don't actually change the speed atomically. */
1839 if (idivisor != 0) {
1840 cd_setreg(com, CD1400_RBPR, idivisor);
1841 cd_setreg(com, CD1400_RCOR, iprescaler);
1843 if (odivisor != 0) {
1844 cd_setreg(com, CD1400_TBPR, odivisor);
1845 cd_setreg(com, CD1400_TCOR, oprescaler);
1851 * transmitter enable (always set)
1854 opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
1855 | (cflag & CREAD ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
1856 if (opt != com->channel_control) {
1857 com->channel_control = opt;
1858 cd1400_channel_cmd(com, opt);
1862 /* set special chars */
1863 /* XXX if one is _POSIX_VDISABLE, can't use some others */
1864 if (t->c_cc[VSTOP] != _POSIX_VDISABLE)
1865 cd_setreg(com, CD1400_SCHR1, t->c_cc[VSTOP]);
1866 if (t->c_cc[VSTART] != _POSIX_VDISABLE)
1867 cd_setreg(com, CD1400_SCHR2, t->c_cc[VSTART]);
1868 if (t->c_cc[VINTR] != _POSIX_VDISABLE)
1869 cd_setreg(com, CD1400_SCHR3, t->c_cc[VINTR]);
1870 if (t->c_cc[VSUSP] != _POSIX_VDISABLE)
1871 cd_setreg(com, CD1400_SCHR4, t->c_cc[VSUSP]);
1875 * set channel option register 1 -
1882 if (cflag & PARENB) {
1884 opt |= CD1400_COR1_PARODD;
1885 opt |= CD1400_COR1_PARNORMAL;
1888 if (!(iflag & INPCK))
1889 opt |= CD1400_COR1_NOINPCK;
1892 if (cflag & CSTOPB) {
1894 opt |= CD1400_COR1_STOP2;
1897 switch (cflag & CSIZE) {
1900 opt |= CD1400_COR1_CS5;
1904 opt |= CD1400_COR1_CS6;
1908 opt |= CD1400_COR1_CS7;
1912 opt |= CD1400_COR1_CS8;
1916 if (opt != com->cor[0]) {
1917 cor_change |= CD1400_CCR_COR1;
1918 cd_setreg(com, CD1400_COR1, com->cor[0] = opt);
1922 * Set receive time-out period, normally to max(one char time, 5 ms).
1924 if (t->c_ispeed == 0)
1925 itimeout = cd_getreg(com, CD1400_RTPR);
1927 itimeout = (1000 * bits + t->c_ispeed - 1) / t->c_ispeed;
1933 if (itimeout < MIN_RTP)
1936 if (!(t->c_lflag & ICANON) && t->c_cc[VMIN] != 0 && t->c_cc[VTIME] != 0
1937 && t->c_cc[VTIME] * 10 > itimeout)
1938 itimeout = t->c_cc[VTIME] * 10;
1941 cd_setreg(com, CD1400_RTPR, itimeout);
1944 * set channel option register 2 -
1950 opt |= CD1400_COR2_IXANY;
1952 opt |= CD1400_COR2_IXOFF;
1954 #ifndef SOFT_CTS_OFLOW
1955 if (cflag & CCTS_OFLOW)
1956 opt |= CD1400_COR2_CCTS_OFLOW;
1959 if (opt != com->cor[1]) {
1960 cor_change |= CD1400_CCR_COR2;
1961 cd_setreg(com, CD1400_COR2, com->cor[1] = opt);
1966 * set channel option register 3 -
1967 * receiver FIFO interrupt threshold
1970 opt = RxFifoThreshold;
1972 if (t->c_lflag & ICANON)
1973 opt |= CD1400_COR3_SCD34; /* detect INTR & SUSP chars */
1975 /* detect and transparently handle START and STOP chars */
1976 opt |= CD1400_COR3_FCT | CD1400_COR3_SCD12;
1978 if (opt != com->cor[2]) {
1979 cor_change |= CD1400_CCR_COR3;
1980 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
1983 /* notify the CD1400 if COR1-3 have changed */
1985 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | cor_change);
1988 * set channel option register 4 -
1991 * received exception processing
1995 opt |= CD1400_COR4_IGNCR;
1998 * we need a new ttyinput() for this, as we don't want to
1999 * have ICRNL && INLCR being done in both layers, or to have
2000 * synchronisation problems
2003 opt |= CD1400_COR4_ICRNL;
2005 opt |= CD1400_COR4_INLCR;
2008 opt |= CD1400_COR4_IGNBRK | CD1400_COR4_NOBRKINT;
2010 * The `-ignbrk -brkint parmrk' case is not handled by the hardware,
2011 * so only tell the hardware about -brkint if -parmrk.
2013 if (!(iflag & (BRKINT | PARMRK)))
2014 opt |= CD1400_COR4_NOBRKINT;
2016 /* XXX using this "intelligence" breaks reporting of overruns. */
2018 opt |= CD1400_COR4_PFO_DISCARD;
2021 opt |= CD1400_COR4_PFO_ESC;
2023 opt |= CD1400_COR4_PFO_NUL;
2026 opt |= CD1400_COR4_PFO_EXCEPTION;
2028 cd_setreg(com, CD1400_COR4, opt);
2031 * set channel option register 5 -
2035 opt |= CD1400_COR5_ISTRIP;
2036 if (t->c_iflag & IEXTEN)
2037 /* enable LNEXT (e.g. ctrl-v quoting) handling */
2038 opt |= CD1400_COR5_LNEXT;
2040 if (t->c_oflag & ONLCR)
2041 opt |= CD1400_COR5_ONLCR;
2042 if (t->c_oflag & OCRNL)
2043 opt |= CD1400_COR5_OCRNL;
2045 cd_setreg(com, CD1400_COR5, opt);
2048 * We always generate modem status change interrupts for CD changes.
2049 * Among other things, this is necessary to track TS_CARR_ON for
2050 * pstat to print even when the driver doesn't care. CD changes
2051 * should be rare so interrupts for them are not worth extra code to
2052 * avoid. We avoid interrupts for other modem status changes (except
2053 * for CTS changes when SOFT_CTS_OFLOW is configured) since this is
2054 * simplest and best.
2058 * set modem change option register 1
2059 * generate modem interrupts on which 1 -> 0 input transitions
2060 * also controls auto-DTR output flow-control, which we don't use
2062 opt = CD1400_MCOR1_CDzd;
2063 #ifdef SOFT_CTS_OFLOW
2064 if (cflag & CCTS_OFLOW)
2065 opt |= CD1400_MCOR1_CTSzd;
2067 cd_setreg(com, CD1400_MCOR1, opt);
2070 * set modem change option register 2
2071 * generate modem interrupts on specific 0 -> 1 input transitions
2073 opt = CD1400_MCOR2_CDod;
2074 #ifdef SOFT_CTS_OFLOW
2075 if (cflag & CCTS_OFLOW)
2076 opt |= CD1400_MCOR2_CTSod;
2078 cd_setreg(com, CD1400_MCOR2, opt);
2081 * XXX should have done this long ago, but there is too much state
2082 * to change all atomically.
2086 com->state &= ~CS_TTGO;
2087 if (!(tp->t_state & TS_TTSTOP))
2088 com->state |= CS_TTGO;
2089 if (cflag & CRTS_IFLOW) {
2090 com->state |= CS_RTS_IFLOW;
2092 * If CS_RTS_IFLOW just changed from off to on, the change
2093 * needs to be propagated to MCR_RTS. This isn't urgent,
2094 * so do it later by calling comstart() instead of repeating
2095 * a lot of code from comstart() here.
2097 } else if (com->state & CS_RTS_IFLOW) {
2098 com->state &= ~CS_RTS_IFLOW;
2100 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS
2101 * on here, since comstart() won't do it later.
2104 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2106 cd_setreg(com, com->mcr_rts_reg,
2107 com->mcr_image |= com->mcr_rts);
2112 * Set up state to handle output flow control.
2113 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2114 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2116 com->state |= CS_ODEVREADY;
2117 #ifdef SOFT_CTS_OFLOW
2118 com->state &= ~CS_CTS_OFLOW;
2119 if (cflag & CCTS_OFLOW) {
2120 com->state |= CS_CTS_OFLOW;
2121 if (!(com->last_modem_status & MSR_CTS))
2122 com->state &= ~CS_ODEVREADY;
2125 /* XXX shouldn't call functions while intrs are disabled. */
2126 disc_optim(tp, t, com);
2129 * Recover from fiddling with CS_TTGO. We used to call siointr1()
2130 * unconditionally, but that defeated the careful discarding of
2131 * stale input in sioopen().
2133 if (com->state >= (CS_BUSY | CS_TTGO))
2136 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2137 if (!(com->intr_enable & CD1400_SRER_TXRDY))
2138 cd_setreg(com, CD1400_SRER,
2140 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2141 | CD1400_SRER_TXRDY);
2143 if (com->intr_enable & CD1400_SRER_TXRDY)
2144 cd_setreg(com, CD1400_SRER,
2146 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2147 | CD1400_SRER_TXMPTY);
2153 if (com->ibufold != NULL) {
2154 kfree(com->ibufold, M_DEVBUF);
2155 com->ibufold = NULL;
2157 lwkt_reltoken(&tty_token);
2162 * NOTE: Must be called with tty_token held
2165 siosetwater(struct com_s *com, speed_t speed)
2172 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2175 * Make the buffer size large enough to handle a softtty interrupt
2176 * latency of about 2 ticks without loss of throughput or data
2177 * (about 3 ticks if input flow control is not used or not honoured,
2178 * but a bit less for CS5-CS7 modes).
2180 cp4ticks = speed / 10 / hz * 4;
2181 for (ibufsize = 128; ibufsize < cp4ticks;)
2183 if (ibufsize == com->ibufsize) {
2189 * Allocate input buffer. The extra factor of 2 in the size is
2190 * to allow for an error byte for each input byte.
2192 ibuf = kmalloc(2 * ibufsize, M_DEVBUF, M_WAITOK);
2194 /* Initialize non-critical variables. */
2195 com->ibufold = com->ibuf;
2196 com->ibufsize = ibufsize;
2199 tp->t_ififosize = 2 * ibufsize;
2200 tp->t_ispeedwat = (speed_t)-1;
2201 tp->t_ospeedwat = (speed_t)-1;
2205 * Read current input buffer, if any. Continue with interrupts
2209 if (com->iptr != com->ibuf)
2213 * Initialize critical variables, including input buffer watermarks.
2214 * The external device is asked to stop sending when the buffer
2215 * exactly reaches high water, or when the high level requests it.
2216 * The high level is notified immediately (rather than at a later
2217 * clock tick) when this watermark is reached.
2218 * The buffer size is chosen so the watermark should almost never
2220 * The low watermark is invisibly 0 since the buffer is always
2221 * emptied all at once.
2223 com->iptr = com->ibuf = ibuf;
2224 com->ibufend = ibuf + ibufsize;
2225 com->ierroff = ibufsize;
2226 com->ihighwater = ibuf + 3 * ibufsize / 4;
2231 comstart(struct tty *tp)
2239 unit = DEV_TO_UNIT(tp->t_dev);
2240 com = com_addr(unit);
2242 lwkt_gettoken(&tty_token);
2250 if (tp->t_state & TS_TTSTOP) {
2251 com->state &= ~CS_TTGO;
2252 if (com->intr_enable & CD1400_SRER_TXRDY)
2253 cd_setreg(com, CD1400_SRER,
2255 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2256 | CD1400_SRER_TXMPTY);
2258 com->state |= CS_TTGO;
2259 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)
2260 && !(com->intr_enable & CD1400_SRER_TXRDY))
2261 cd_setreg(com, CD1400_SRER,
2263 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2264 | CD1400_SRER_TXRDY);
2266 if (tp->t_state & TS_TBLOCK) {
2267 if (com->mcr_image & com->mcr_rts && com->state & CS_RTS_IFLOW)
2269 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2271 cd_setreg(com, com->mcr_rts_reg,
2272 com->mcr_image &= ~com->mcr_rts);
2275 if (!(com->mcr_image & com->mcr_rts)
2276 && com->iptr < com->ihighwater
2277 && com->state & CS_RTS_IFLOW)
2279 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2281 cd_setreg(com, com->mcr_rts_reg,
2282 com->mcr_image |= com->mcr_rts);
2286 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2288 lwkt_reltoken(&tty_token);
2292 if (tp->t_outq.c_cc != 0) {
2296 if (!com->obufs[0].l_queued) {
2300 com->obufs[0].l_tail
2301 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2303 com->obufs[0].l_next = NULL;
2304 com->obufs[0].l_queued = TRUE;
2306 if (com->state & CS_BUSY) {
2307 qp = com->obufq.l_next;
2308 while ((next = qp->l_next) != NULL)
2310 qp->l_next = &com->obufs[0];
2312 com->obufq.l_head = com->obufs[0].l_head;
2313 com->obufq.l_tail = com->obufs[0].l_tail;
2314 com->obufq.l_next = &com->obufs[0];
2315 com->state |= CS_BUSY;
2316 if (com->state >= (CS_BUSY | CS_TTGO
2318 cd_setreg(com, CD1400_SRER,
2321 & ~CD1400_SRER_TXMPTY)
2322 | CD1400_SRER_TXRDY);
2326 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2330 com->obufs[1].l_tail
2331 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2333 com->obufs[1].l_next = NULL;
2334 com->obufs[1].l_queued = TRUE;
2336 if (com->state & CS_BUSY) {
2337 qp = com->obufq.l_next;
2338 while ((next = qp->l_next) != NULL)
2340 qp->l_next = &com->obufs[1];
2342 com->obufq.l_head = com->obufs[1].l_head;
2343 com->obufq.l_tail = com->obufs[1].l_tail;
2344 com->obufq.l_next = &com->obufs[1];
2345 com->state |= CS_BUSY;
2346 if (com->state >= (CS_BUSY | CS_TTGO
2348 cd_setreg(com, CD1400_SRER,
2351 & ~CD1400_SRER_TXMPTY)
2352 | CD1400_SRER_TXRDY);
2356 tp->t_state |= TS_BUSY;
2364 if (com->state >= (CS_BUSY | CS_TTGO))
2365 siointr1(com); /* fake interrupt to start output */
2369 lwkt_reltoken(&tty_token);
2374 comstop(struct tty *tp, int rw)
2379 lwkt_gettoken(&tty_token);
2380 com = com_addr(DEV_TO_UNIT(tp->t_dev));
2384 com->obufs[0].l_queued = FALSE;
2385 com->obufs[1].l_queued = FALSE;
2386 if (com->extra_state & CSE_ODONE) {
2387 com_events -= LOTS_OF_EVENTS;
2388 com->extra_state &= ~CSE_ODONE;
2389 if (com->etc != ETC_NONE) {
2390 if (com->etc == ETC_BREAK_ENDED)
2391 com->etc = ETC_NONE;
2395 com->tp->t_state &= ~TS_BUSY;
2396 if (com->state & CS_ODONE)
2397 com_events -= LOTS_OF_EVENTS;
2398 com->state &= ~(CS_ODONE | CS_BUSY);
2401 /* XXX no way to reset only input fifo. */
2402 com_events -= (com->iptr - com->ibuf);
2403 com->iptr = com->ibuf;
2408 if (rw & FWRITE && com->etc == ETC_NONE)
2409 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
2411 lwkt_reltoken(&tty_token);
2415 * NOTE: Must be called with tty_token held
2418 commctl(struct com_s *com, int bits, int how)
2423 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2426 if (com->channel_control & CD1400_CCR_RCVEN)
2428 mcr = com->mcr_image;
2429 if (mcr & com->mcr_dtr)
2431 if (mcr & com->mcr_rts)
2432 /* XXX wired on for Cyclom-8Ys */
2436 * We must read the modem status from the hardware because
2437 * we don't generate modem status change interrupts for all
2438 * changes, so com->prev_modem_status is not guaranteed to
2439 * be up to date. This is safe, unlike for sio, because
2440 * reading the status register doesn't clear pending modem
2441 * status change interrupts.
2443 msr = cd_getreg(com, CD1400_MSVR2);
2452 /* XXX not connected except for Cyclom-16Y? */
2457 if (bits & TIOCM_DTR)
2458 mcr |= com->mcr_dtr;
2459 if (bits & TIOCM_RTS)
2460 mcr |= com->mcr_rts;
2464 com->mcr_image = mcr;
2465 cd_setreg(com, CD1400_MSVR1, mcr);
2466 cd_setreg(com, CD1400_MSVR2, mcr);
2469 com->mcr_image = mcr = com->mcr_image | mcr;
2470 cd_setreg(com, CD1400_MSVR1, mcr);
2471 cd_setreg(com, CD1400_MSVR2, mcr);
2474 com->mcr_image = mcr = com->mcr_image & ~mcr;
2475 cd_setreg(com, CD1400_MSVR1, mcr);
2476 cd_setreg(com, CD1400_MSVR2, mcr);
2484 * NOTE: Must be called with tty_token held
2493 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2495 * Set our timeout period to 1 second if no polled devices are open.
2496 * Otherwise set it to max(1/200, 1/hz).
2497 * Enable timeouts iff some device is open.
2499 callout_stop(&sio_timeout_handle);
2502 for (unit = 0; unit < NSIO; ++unit) {
2503 com = com_addr(unit);
2504 if (com != NULL && com->tp != NULL
2505 && com->tp->t_state & TS_ISOPEN) {
2508 if (com->poll || com->poll_output) {
2509 sio_timeout = hz > 200 ? hz / 200 : 1;
2516 sio_timeouts_until_log = hz / sio_timeout;
2517 callout_reset(&sio_timeout_handle, sio_timeout,
2520 /* Flush error messages, if any. */
2521 sio_timeouts_until_log = 1;
2523 callout_stop(&sio_timeout_handle);
2528 comwakeup(void *chan)
2533 lwkt_gettoken(&tty_token);
2534 callout_reset(&sio_timeout_handle, sio_timeout, comwakeup, NULL);
2538 * Recover from lost output interrupts.
2539 * Poll any lines that don't use interrupts.
2541 for (unit = 0; unit < NSIO; ++unit) {
2542 com = com_addr(unit);
2544 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2553 * Check for and log errors, but not too often.
2555 if (--sio_timeouts_until_log > 0) {
2556 lwkt_reltoken(&tty_token);
2559 sio_timeouts_until_log = hz / sio_timeout;
2560 for (unit = 0; unit < NSIO; ++unit) {
2563 com = com_addr(unit);
2566 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2571 delta = com->delta_error_counts[errnum];
2572 com->delta_error_counts[errnum] = 0;
2576 total = com->error_counts[errnum] += delta;
2577 log(LOG_ERR, "cy%d: %u more %s%s (total %lu)\n",
2578 unit, delta, error_desc[errnum],
2579 delta == 1 ? "" : "s", total);
2582 lwkt_reltoken(&tty_token);
2586 disc_optim(struct tty *tp, struct termios *t, struct com_s *com)
2588 #ifndef SOFT_HOTCHAR
2592 lwkt_gettoken(&tty_token);
2594 * XXX can skip a lot more cases if Smarts. Maybe
2595 * (IGNCR | ISTRIP | IXON) in c_iflag. But perhaps we
2596 * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2598 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2599 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2600 && (!(t->c_iflag & PARMRK)
2601 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2602 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2603 && linesw[tp->t_line].l_rint == ttyinput)
2604 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2606 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2607 com->hotchar = linesw[tp->t_line].l_hotchar;
2608 #ifndef SOFT_HOTCHAR
2609 opt = com->cor[2] & ~CD1400_COR3_SCD34;
2610 if (com->hotchar != 0) {
2611 cd_setreg(com, CD1400_SCHR3, com->hotchar);
2612 cd_setreg(com, CD1400_SCHR4, com->hotchar);
2613 opt |= CD1400_COR3_SCD34;
2615 if (opt != com->cor[2]) {
2616 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2617 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR3);
2620 lwkt_reltoken(&tty_token);
2624 /* standard line discipline input routine */
2626 cyinput(int c, struct tty *tp)
2628 /* XXX duplicate ttyinput(), but without the IXOFF/IXON/ISTRIP/IPARMRK
2629 * bits, as they are done by the CD1400. Hardly worth the effort,
2630 * given that high-throughput sessions are raw anyhow.
2636 comspeed(speed_t speed, u_long cy_clock, int *prescaler_io)
2646 if (speed < 0 || speed > 150000)
2649 /* determine which prescaler to use */
2650 for (prescaler_unit = 4, prescaler = 2048; prescaler_unit;
2651 prescaler_unit--, prescaler >>= 2) {
2652 if (cy_clock / prescaler / speed > 63)
2656 divider = (cy_clock / prescaler * 2 / speed + 1) / 2; /* round off */
2659 actual = cy_clock/prescaler/divider;
2661 /* 10 times error in percent: */
2662 error = ((actual - (long)speed) * 2000 / (long)speed + 1) / 2;
2664 /* 3.0% max error tolerance */
2665 if (error < -30 || error > 30)
2669 kprintf("prescaler = %d (%d)\n", prescaler, prescaler_unit);
2670 kprintf("divider = %d (%x)\n", divider, divider);
2671 kprintf("actual = %d\n", actual);
2672 kprintf("error = %d\n", error);
2675 *prescaler_io = prescaler_unit;
2680 * NOTE: Must be called with tty_token held
2683 cd1400_channel_cmd(struct com_s *com, int cmd)
2685 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2686 cd1400_channel_cmd_wait(com);
2687 cd_setreg(com, CD1400_CCR, cmd);
2688 cd1400_channel_cmd_wait(com);
2692 * NOTE: Must be called with tty_token held
2695 cd1400_channel_cmd_wait(struct com_s *com)
2697 struct timeval start;
2701 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2703 if (cd_getreg(com, CD1400_CCR) == 0)
2707 if (cd_getreg(com, CD1400_CCR) == 0)
2710 usec = 1000000 * (tv.tv_sec - start.tv_sec) +
2711 tv.tv_usec - start.tv_usec;
2714 "cy%d: channel command timeout (%ld usec)\n",
2722 * NOTE: Must be called with tty_token held
2725 cd_etc(struct com_s *com, int etc)
2727 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2729 * We can't change the hardware's ETC state while there are any
2730 * characters in the tx fifo, since those characters would be
2731 * interpreted as commands! Unputting characters from the fifo
2732 * is difficult, so we wait up to 12 character times for the fifo
2733 * to drain. The command will be delayed for up to 2 character
2734 * times for the tx to become empty. Unputting characters from
2735 * the tx holding and shift registers is impossible, so we wait
2736 * for the tx to become empty so that the command is sure to be
2737 * executed soon after we issue it.
2740 if (com->etc == etc) {
2744 if ((etc == CD1400_ETC_SENDBREAK
2745 && (com->etc == ETC_BREAK_STARTING
2746 || com->etc == ETC_BREAK_STARTED))
2747 || (etc == CD1400_ETC_STOPBREAK
2748 && (com->etc == ETC_BREAK_ENDING || com->etc == ETC_BREAK_ENDED
2749 || com->etc == ETC_NONE))) {
2754 cd_setreg(com, CD1400_SRER,
2756 = (com->intr_enable & ~CD1400_SRER_TXRDY) | CD1400_SRER_TXMPTY);
2759 while (com->etc == etc
2760 && tsleep(&com->etc, PCATCH, "cyetc", 0) == 0)
2765 * NOTE: Must be called with tty_token held
2768 cd_getreg(struct com_s *com, int reg)
2770 struct com_s *basecom;
2777 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2779 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2780 car = com->unit & CD1400_CAR_CHAN;
2781 cy_align = com->cy_align;
2782 iobase = com->iobase;
2786 if (basecom->car != car)
2787 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2788 val = cd_inb(iobase, reg, cy_align);
2795 * NOTE: Must be called with tty_token held
2798 cd_setreg(struct com_s *com, int reg, int val)
2800 struct com_s *basecom;
2806 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2808 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2809 car = com->unit & CD1400_CAR_CHAN;
2810 cy_align = com->cy_align;
2811 iobase = com->iobase;
2815 if (basecom->car != car)
2816 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2817 cd_outb(iobase, reg, cy_align, val);
2832 com = com_addr(unit);
2833 kprintf("info for channel %d\n", unit);
2834 kprintf("------------------\n");
2835 kprintf("total cyclom service probes:\t%d\n", cy_svrr_probes);
2836 kprintf("calls to upper layer:\t\t%d\n", cy_timeouts);
2839 lwkt_gettoken(&tty_token);
2840 iobase = com->iobase;
2842 kprintf("cd1400 base address:\\tt%p\n", iobase);
2843 kprintf("saved channel_control:\t\t0x%02x\n", com->channel_control);
2844 kprintf("saved cor1-3:\t\t\t0x%02x 0x%02x 0x%02x\n",
2845 com->cor[0], com->cor[1], com->cor[2]);
2846 kprintf("service request enable reg:\t0x%02x (0x%02x cached)\n",
2847 cd_getreg(com, CD1400_SRER), com->intr_enable);
2848 kprintf("service request register:\t0x%02x\n",
2849 cd_inb(iobase, CD1400_SVRR, com->cy_align));
2850 kprintf("modem status:\t\t\t0x%02x (0x%02x cached)\n",
2851 cd_getreg(com, CD1400_MSVR2), com->prev_modem_status);
2852 kprintf("rx/tx/mdm interrupt registers:\t0x%02x 0x%02x 0x%02x\n",
2853 cd_inb(iobase, CD1400_RIR, com->cy_align),
2854 cd_inb(iobase, CD1400_TIR, com->cy_align),
2855 cd_inb(iobase, CD1400_MIR, com->cy_align));
2857 kprintf("com state:\t\t\t0x%02x\n", com->state);
2858 kprintf("calls to comstart():\t\t%d (%d useful)\n",
2859 com->start_count, com->start_real);
2860 kprintf("rx buffer chars free:\t\t%d\n", com->iptr - com->ibuf);
2862 if (com->obufs[0].l_queued)
2863 ocount += com->obufs[0].l_tail - com->obufs[0].l_head;
2864 if (com->obufs[1].l_queued)
2865 ocount += com->obufs[1].l_tail - com->obufs[1].l_head;
2866 kprintf("tx buffer chars:\t\t%u\n", ocount);
2867 kprintf("received chars:\t\t\t%d\n", com->bytes_in);
2868 kprintf("received exceptions:\t\t%d\n", com->recv_exception);
2869 kprintf("modem signal deltas:\t\t%d\n", com->mdm);
2870 kprintf("transmitted chars:\t\t%d\n", com->bytes_out);
2874 kprintf("tty state:\t\t\t0x%08x\n", tp->t_state);
2876 "upper layer queue lengths:\t%d raw, %d canon, %d output\n",
2877 tp->t_rawq.c_cc, tp->t_canq.c_cc, tp->t_outq.c_cc);
2879 kprintf("tty state:\t\t\tclosed\n");
2881 lwkt_reltoken(&tty_token);
2883 #endif /* CyDebug */