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>
88 #include <bus/isa/isa_device.h>
90 #include <machine_base/isa/ic/cd1400.h>
92 #define disable_intr() com_lock()
93 #define enable_intr() com_unlock()
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.
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
143 #define CY_MAX_PORTS (CD1400_NO_OF_CHANNELS * CY_MAX_CD1400s)
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
150 * ETC states. com->etc may also contain a hardware ETC command value,
151 * meaning that execution of that command is pending.
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
159 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */
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)
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.
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))
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
183 * The following com and tty flags correspond closely:
184 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and
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).
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 */
203 static char const * const error_desc[] = {
206 #define CE_INTERRUPT_BUF_OVERFLOW 1
207 "interrupt-level buffer overflow",
208 #define CE_TTY_BUF_OVERFLOW 2
209 "tty-level buffer overflow",
213 #define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum])
215 /* types. XXX - should be elsewhere */
216 typedef u_char bool_t; /* boolean */
217 typedef u_char volatile *cy_addr;
219 /* queue of linear buffers */
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 */
227 /* com device structure */
229 u_char state; /* miscellaneous flag bits */
230 bool_t active_out; /* nonzero if the callout device is open */
232 u_char cfcr_image; /* copy of value written to CFCR */
234 u_char etc; /* pending Embedded Transmit Command */
235 u_char extra_state; /* more flag bits, separate for order trick */
237 u_char fifo_image; /* copy of value written to FIFO */
239 u_char gfrcr_image; /* copy of value read from GFRCR */
241 bool_t hasfifo; /* nonzero for 16550 UARTs */
242 bool_t loses_outints; /* nonzero if device loses output interrupts */
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 */
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 */
255 int unit; /* unit number */
256 int dtr_wait; /* time to hold DTR down on close (* 1/hz) */
257 struct callout dtr_ch;
261 u_int wopeners; /* # processes waiting for DCD in open() */
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
269 u_char last_modem_status; /* last MSR read by intr handler */
270 u_char prev_modem_status; /* last MSR handled by high level */
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 */
281 struct lbq obufq; /* head of queue of output buffers */
282 struct lbq obufs[2]; /* output buffers */
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 */
289 struct tty *tp; /* cross reference */
292 struct termios it_in; /* should be in struct tty */
293 struct termios it_out;
296 struct termios lt_in; /* should be in struct tty */
297 struct termios lt_out;
300 bool_t do_dcd_timestamp;
301 struct timeval timestamp;
302 struct timeval dcd_timestamp;
304 u_long bytes_in; /* statistics */
306 u_int delta_error_counts[CE_NTYPES];
307 u_long error_counts[CE_NTYPES];
309 u_int recv_exception; /* exception chars received */
310 u_int mdm; /* modem signal changes */
312 u_int start_count; /* no. of calls to comstart() */
313 u_int start_real; /* no. of calls that did something */
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 */
321 * Data area for output buffers. Someday we should build the output
322 * buffer queue without copying data.
328 /* PCI driver entry point. */
329 int cyattach_common (cy_addr cy_iobase, int cy_align);
330 void siointr(void *);
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);
343 static void siointr1 (struct com_s *com);
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,
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,
360 void cystatus (int unit);
363 static char driver_name[] = "cy";
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])
369 struct isa_driver siodriver = {
370 sioprobe, sioattach, driver_name
373 static d_open_t sioopen;
374 static d_close_t sioclose;
375 static d_write_t siowrite;
376 static d_ioctl_t sioioctl;
378 static struct dev_ops sio_ops = {
379 { driver_name, 0, D_TTY },
385 .d_kqfilter = ttykqfilter,
386 .d_revoke = ttyrevoke
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;
397 static struct tty *sio_tty[NSIO];
399 static struct tty sio_tty[NSIO];
401 static const int nsio_tty = NSIO;
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;
412 static int cy_chip_offset[] = {
413 0x0000, 0x0400, 0x0800, 0x0c00, 0x0200, 0x0600, 0x0a00, 0x0e00,
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);
421 sioprobe(struct isa_device *dev)
425 iobase = (cy_addr)dev->id_maddr;
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 */
431 /* this is needed to get the board out of reset */
432 cy_outb(iobase, CY_CLEAR_INTR, 0, 0);
435 return (cy_units(iobase, 0) == 0 ? 0 : -1);
439 cy_units(cy_addr cy_iobase, int cy_align)
442 u_char firmware_version = 0; /* assign to avoid warning */
446 for (cyu = 0; cyu < CY_MAX_CD1400s; ++cyu) {
447 iobase = cy_iobase + (cy_chip_offset[cyu] << cy_align);
449 /* wait for chip to become ready for new command */
450 for (i = 0; i < 10; i++) {
452 if (!cd_inb(iobase, CD1400_CCR, cy_align))
456 /* clear the GFRCR register */
457 cd_outb(iobase, CD1400_GFRCR, cy_align, 0);
459 /* issue a reset command */
460 cd_outb(iobase, CD1400_CCR, cy_align,
461 CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
463 /* wait for the CD1400 to initialize itself */
464 for (i = 0; i < 200; i++) {
467 /* retrieve firmware version */
468 firmware_version = cd_inb(iobase, CD1400_GFRCR,
470 if ((firmware_version & 0xf0) == 0x40)
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.
479 if ((firmware_version & 0xf0) != 0x40)
486 sioattach(struct isa_device *isdp)
490 lwkt_gettoken(&tty_token);
491 adapter = cyattach_common((cy_addr) isdp->id_maddr, 0);
493 lwkt_reltoken(&tty_token);
499 * This kludge is to allow ISA/PCI device specifications in the
500 * kernel config file to be in any order.
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 */
506 isdp->id_intr = (inthand2_t *)siointr;
507 /* isdp->id_ri_flags |= RI_FAST; XXX unimplemented - use newbus! */
508 lwkt_reltoken(&tty_token);
513 * NOTE: Must be called with tty_token held
516 cyattach_common(cy_addr cy_iobase, int cy_align)
520 u_char firmware_version;
526 ASSERT_LWKT_TOKEN_HELD(&tty_token);
528 adapter = cy_total_devices;
529 if ((u_int)adapter >= NCY) {
531 "cy%d: can't attach adapter: insufficient cy devices configured\n",
535 ncyu = cy_units(cy_iobase, cy_align);
538 cy_nr_cd1400s[adapter] = ncyu;
541 unit = adapter * CY_MAX_PORTS;
542 for (cyu = 0; cyu < ncyu; ++cyu) {
545 iobase = (cy_addr) (cy_iobase
546 + (cy_chip_offset[cyu] << cy_align));
547 firmware_version = cd_inb(iobase, CD1400_GFRCR, cy_align);
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));
554 for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; ++cdu, ++unit) {
557 com = kmalloc(sizeof *com, M_DEVBUF, M_WAITOK | M_ZERO);
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;
566 com->mcr_dtr = MCR_DTR;
567 com->mcr_rts = MCR_RTS;
568 com->mcr_rts_reg = CD1400_MSVR1;
570 com->dtr_wait = 3 * hz;
571 com->obufs[0].l_head = com->obuf1;
572 com->obufs[1].l_head = com->obuf2;
574 com->cy_align = cy_align;
575 com->cy_iobase = cy_iobase;
576 com->iobase = iobase;
577 com->car = ~CD1400_CAR_CHAN;
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.
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;
596 if (siosetwater(com, com->it_in.c_ispeed) != 0) {
598 kfree(com, M_DEVBUF);
602 termioschars(&com->it_in);
603 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
604 com->it_out = com->it_in;
607 com_addr(unit) = com;
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;
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);
635 /* for(cdu...), for(cyu...) terminating blocks */
639 /* ensure an edge for the next interrupt */
640 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
646 sioopen(struct dev_open_args *ap)
648 cdev_t dev = ap->a_head.a_dev;
656 unit = MINOR_TO_UNIT(mynor);
657 if ((u_int) unit >= NSIO || (com = com_addr(unit)) == NULL)
659 if (mynor & CONTROL_MASK)
662 lwkt_gettoken(&tty_token);
664 tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
666 tp = com->tp = &sio_tty[unit];
671 * We jump to this label after all non-interrupted sleeps to pick
672 * up any changes of the device state.
675 while (com->state & CS_DTR_OFF) {
676 error = tsleep(&com->dtr_wait, PCATCH, "cydtr", 0);
680 if (tp->t_state & TS_ISOPEN) {
682 * The device is open, so everything has been initialized.
685 if (mynor & CALLOUT_MASK) {
686 if (!com->active_out) {
691 if (com->active_out) {
692 if (ap->a_oflags & O_NONBLOCK) {
696 error = tsleep(&com->active_out,
703 if (tp->t_state & TS_XCLUDE &&
704 priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
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.
715 tp->t_oproc = comstart;
716 tp->t_stop = comstop;
717 tp->t_param = comparam;
719 tp->t_termios = mynor & CALLOUT_MASK
720 ? com->it_out : com->it_in;
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);
726 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
728 com->poll = com->no_irq;
729 com->poll_output = com->loses_outints;
732 error = comparam(tp, &tp->t_termios);
739 * (Re)enable and flush fifos.
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
750 outb(iobase + com_fifo,
751 FIFO_RCV_RST | FIFO_XMT_RST
754 if (!(inb(com->line_status_port) & LSR_RXRDY))
756 outb(iobase + com_fifo, 0);
758 (void) inb(com->data_port);
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
772 * Flush fifos. This requires a full channel reset which
773 * also disables the transmitter and receiver. Recover
776 cd1400_channel_cmd(com,
777 CD1400_CCR_CMDRESET | CD1400_CCR_CHANRESET);
778 cd1400_channel_cmd(com, com->channel_control);
781 com->prev_modem_status = com->last_modem_status
782 = cd_getreg(com, CD1400_MSVR2);
783 cd_setreg(com, CD1400_SRER,
785 = CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
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".
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
801 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
802 (*linesw[tp->t_line].l_modem)(tp, 1);
805 * Wait for DCD if necessary.
807 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
808 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) {
810 error = tsleep(TSA_CARR_ON(tp), PCATCH, "cydcd", 0);
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;
823 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
825 lwkt_reltoken(&tty_token);
830 * NOTE: Must be called with tty_token held
833 sioclose(struct dev_close_args *ap)
835 cdev_t dev = ap->a_head.a_dev;
840 ASSERT_LWKT_TOKEN_HELD(&tty_token);
842 if (mynor & CONTROL_MASK)
844 lwkt_gettoken(&tty_token);
845 com = com_addr(MINOR_TO_UNIT(mynor));
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);
856 #ifdef broken /* session holds a ref to the tty; can't deallocate */
858 com->tp = sio_tty[unit] = NULL;
860 lwkt_reltoken(&tty_token);
865 * NOTE: Must be called with tty_token held
868 comhardclose(struct com_s *com)
874 ASSERT_LWKT_TOKEN_HELD(&tty_token);
877 iobase = com->iobase;
881 com->poll_output = FALSE;
883 com->do_timestamp = 0;
885 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
890 cd_setreg(com, CD1400_COR2, com->cor[1] &= ~CD1400_COR2_ETC);
892 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
897 outb(iobase + com_ier, 0);
900 cd_setreg(com, CD1400_SRER, com->intr_enable = 0);
904 if ((tp->t_cflag & HUPCL)
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.
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);
918 /* Disable receiver (leave transmitter enabled). */
919 com->channel_control = CD1400_CCR_CMDCHANCTL
922 cd1400_channel_cmd(com, com->channel_control);
924 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
925 callout_reset(&com->dtr_ch, com->dtr_wait,
927 com->state |= CS_DTR_OFF;
934 * Disable fifos so that they are off after controlled
935 * reboots. Some BIOSes fail to detect 16550s when the
938 outb(iobase + com_fifo, 0);
941 com->active_out = FALSE;
942 wakeup(&com->active_out);
943 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */
948 siowrite(struct dev_write_args *ap)
950 cdev_t dev = ap->a_head.a_dev;
951 struct uio *uio = ap->a_uio;
957 if (mynor & CONTROL_MASK)
960 lwkt_gettoken(&tty_token);
961 unit = MINOR_TO_UNIT(mynor);
962 tp = com_addr(unit)->tp;
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.
969 if (constty != NULL && unit == comconsole)
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.
977 return ((*linesw[tp->t_line].l_write)(tp, uio, ap->a_ioflag));
979 lwkt_reltoken(&tty_token);
983 siodtrwakeup(void *chan)
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);
995 * NOTE: Must be called with tty_token held
998 sioinput(struct com_s *com)
1006 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1010 if (!(tp->t_state & TS_ISOPEN)) {
1011 com_events -= (com->iptr - com->ibuf);
1012 com->iptr = com->ibuf;
1015 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
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
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))
1031 com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1032 += b_to_q((char *)buf, incc, &tp->t_rawq);
1036 tp->t_rawcc += incc;
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;
1046 } while (buf < com->iptr);
1050 line_status = buf[com->ierroff];
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;
1063 (*linesw[tp->t_line].l_rint)(recv_data, tp);
1065 } while (buf < com->iptr);
1067 com_events -= (com->iptr - com->ibuf);
1068 com->iptr = com->ibuf;
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.
1075 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & com->mcr_rts) &&
1076 !(tp->t_state & TS_TBLOCK))
1078 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1080 cd_setreg(com, com->mcr_rts_reg,
1081 com->mcr_image |= com->mcr_rts);
1088 int unit = (int)arg;
1096 lwkt_gettoken(&tty_token);
1097 com_lock(); /* XXX could this be placed down lower in the loop? */
1099 baseu = unit * CY_MAX_PORTS;
1100 cy_align = com_addr(baseu)->cy_align;
1101 cy_iobase = com_addr(baseu)->cy_iobase;
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);
1114 /* service requests as appropriate, giving priority to RX */
1115 if (status & CD1400_SVRR_RXRDY) {
1127 save_rir = cd_inb(iobase, CD1400_RIR, cy_align);
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;
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));
1139 /* ack receive service */
1140 serv_type = cy_inb(iobase, CY8_SVCACKR, cy_align);
1142 com = com_addr(baseu +
1143 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1144 & CD1400_xIVR_CHAN));
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)
1157 #if 1 /* XXX "intelligent" PFO error handling would break O error handling */
1158 if (line_status & (LSR_PE|LSR_FE|LSR_BI)) {
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.
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)))
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))))
1181 if (com->hotchar != 0 && recv_data == com->hotchar)
1185 if (ioptr >= com->ibufend)
1186 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1188 if (com->do_timestamp)
1189 microtime(&com->timestamp);
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)
1197 outb(com->modem_ctl_port,
1198 com->mcr_image &= ~MCR_RTS);
1200 cd_outb(iobase, com->mcr_rts_reg,
1205 if (line_status & LSR_OE)
1206 CE_RECORD(com, CE_OVERRUN);
1212 count = cd_inb(iobase, CD1400_RDCR, cy_align);
1215 com->bytes_in += count;
1217 ifree = com->ibufend - ioptr;
1218 if (count > ifree) {
1220 com_events += ifree;
1222 if (com->do_timestamp)
1223 microtime(&com->timestamp);
1225 recv_data = cd_inb(iobase,
1229 if (com->hotchar != 0
1234 ioptr[0] = recv_data;
1235 ioptr[com->ierroff] = 0;
1237 } while (--ifree != 0);
1239 com->delta_error_counts
1240 [CE_INTERRUPT_BUF_OVERFLOW] += count;
1242 recv_data = cd_inb(iobase, CD1400_RDSR,
1245 if (com->hotchar != 0
1246 && recv_data == com->hotchar)
1249 } while (--count != 0);
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)
1257 outb(com->modem_ctl_port,
1258 com->mcr_image &= ~MCR_RTS);
1260 cd_outb(iobase, com->mcr_rts_reg,
1265 com_events += count;
1267 recv_data = cd_inb(iobase, CD1400_RDSR,
1270 if (com->hotchar != 0
1271 && recv_data == com->hotchar)
1274 ioptr[0] = recv_data;
1275 ioptr[com->ierroff] = 0;
1277 } while (--count != 0);
1283 /* terminate service context */
1285 cd_outb(iobase, CD1400_RIR, cy_align,
1287 & ~(CD1400_RIR_RDIREQ | CD1400_RIR_RBUSY));
1289 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1292 if (status & CD1400_SVRR_MDMCH) {
1294 u_char modem_status;
1302 save_mir = cd_inb(iobase, CD1400_MIR, cy_align);
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;
1309 com = com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS
1310 + (save_mir & CD1400_MIR_CHAN));
1312 /* ack modem service */
1313 vector = cy_inb(iobase, CY8_SVCACKM, cy_align);
1315 com = com_addr(baseu
1316 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1317 & CD1400_xIVR_CHAN));
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);
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.
1333 com->last_modem_status = modem_status;
1334 if (!(com->state & CS_CHECKMSR)) {
1335 com_events += LOTS_OF_EVENTS;
1336 com->state |= CS_CHECKMSR;
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
1347 && !(com->intr_enable
1348 & CD1400_SRER_TXRDY))
1349 cd_outb(iobase, CD1400_SRER,
1353 & ~CD1400_SRER_TXMPTY
1354 | CD1400_SRER_TXRDY);
1356 com->state &= ~CS_ODEVREADY;
1357 if (com->intr_enable
1358 & CD1400_SRER_TXRDY)
1359 cd_outb(iobase, CD1400_SRER,
1363 & ~CD1400_SRER_TXRDY
1364 | CD1400_SRER_TXMPTY);
1370 /* terminate service context */
1372 cd_outb(iobase, CD1400_MIR, cy_align,
1374 & ~(CD1400_MIR_RDIREQ | CD1400_MIR_RBUSY));
1376 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1379 if (status & CD1400_SVRR_TXRDY) {
1388 save_tir = cd_inb(iobase, CD1400_TIR, cy_align);
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;
1395 com = com_addr(baseu
1396 + cyu * CD1400_NO_OF_CHANNELS
1397 + (save_tir & CD1400_TIR_CHAN));
1399 /* ack transmit service */
1400 vector = cy_inb(iobase, CY8_SVCACKT, cy_align);
1402 com = com_addr(baseu
1403 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1404 & CD1400_xIVR_CHAN));
1407 if (com->etc != ETC_NONE) {
1408 if (com->intr_enable & CD1400_SRER_TXRDY) {
1410 * Here due to sloppy SRER_TXRDY
1411 * enabling. Ignore. Come back when
1414 cd_outb(iobase, CD1400_SRER, cy_align,
1417 & ~CD1400_SRER_TXRDY)
1418 | CD1400_SRER_TXMPTY);
1419 goto terminate_tx_service;
1422 case CD1400_ETC_SENDBREAK:
1423 case CD1400_ETC_STOPBREAK:
1425 * Start the command. Come back on
1426 * next tx empty interrupt, hopefully
1427 * after command has been executed.
1429 cd_outb(iobase, CD1400_COR2, cy_align,
1430 com->cor[1] |= CD1400_COR2_ETC);
1431 cd_outb(iobase, CD1400_TDR, cy_align,
1433 cd_outb(iobase, CD1400_TDR, cy_align,
1435 if (com->etc == CD1400_ETC_SENDBREAK)
1436 com->etc = ETC_BREAK_STARTING;
1438 com->etc = ETC_BREAK_ENDING;
1439 goto terminate_tx_service;
1440 case ETC_BREAK_STARTING:
1442 * BREAK is now on. Continue with
1443 * SRER_TXMPTY processing, hopefully
1446 com->etc = ETC_BREAK_STARTED;
1448 case ETC_BREAK_STARTED:
1450 * Came back due to sloppy SRER_TXMPTY
1451 * enabling. Hope again.
1454 case ETC_BREAK_ENDING:
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.
1461 cd_outb(iobase, CD1400_COR2, cy_align,
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;
1471 case ETC_BREAK_ENDED:
1473 * Shouldn't get here. Hope again.
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;
1484 cd_outb(iobase, CD1400_SRER, cy_align,
1486 &= ~CD1400_SRER_TXMPTY);
1487 goto terminate_tx_service;
1489 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
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;
1499 cd_outb(iobase, CD1400_TDR, cy_align,
1501 while (--ocount != 0);
1502 com->obufq.l_head = ioptr;
1503 if (ioptr >= com->obufq.l_tail) {
1506 qp = com->obufq.l_next;
1507 qp->l_queued = FALSE;
1510 com->obufq.l_head = qp->l_head;
1511 com->obufq.l_tail = qp->l_tail;
1512 com->obufq.l_next = qp;
1514 /* output just completed */
1515 com->state &= ~CS_BUSY;
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
1524 if (com->extra_state & CSE_ODONE) {
1525 com_events -= LOTS_OF_EVENTS;
1526 com->extra_state &= ~CSE_ODONE;
1529 cd_outb(iobase, CD1400_SRER, cy_align,
1532 & ~CD1400_SRER_TXRDY)
1533 | CD1400_SRER_TXMPTY);
1535 if (!(com->state & CS_ODONE)) {
1536 com_events += LOTS_OF_EVENTS;
1537 com->state |= CS_ODONE;
1539 /* handle at high level ASAP */
1545 /* terminate service context */
1546 terminate_tx_service:
1548 cd_outb(iobase, CD1400_TIR, cy_align,
1550 & ~(CD1400_TIR_RDIREQ | CD1400_TIR_RBUSY));
1552 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1557 /* ensure an edge for the next interrupt */
1558 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
1563 lwkt_reltoken(&tty_token);
1568 siointr1(struct com_s *com)
1574 sioioctl(struct dev_ioctl_args *ap)
1576 cdev_t dev = ap->a_head.a_dev;
1577 caddr_t data = ap->a_data;
1578 u_long cmd = ap->a_cmd;
1583 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1585 struct termios term;
1588 lwkt_gettoken(&tty_token);
1590 com = com_addr(MINOR_TO_UNIT(mynor));
1591 if (mynor & CONTROL_MASK) {
1594 switch (mynor & CONTROL_MASK) {
1595 case CONTROL_INIT_STATE:
1596 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
1598 case CONTROL_LOCK_STATE:
1599 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
1602 lwkt_reltoken(&tty_token);
1603 return (ENODEV); /* /dev/nodev */
1607 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1609 lwkt_reltoken(&tty_token);
1612 *ct = *(struct termios *)data;
1613 lwkt_reltoken(&tty_token);
1616 *(struct termios *)data = *ct;
1617 lwkt_reltoken(&tty_token);
1620 *(int *)data = TTYDISC;
1621 lwkt_reltoken(&tty_token);
1624 bzero(data, sizeof(struct winsize));
1625 lwkt_reltoken(&tty_token);
1628 lwkt_reltoken(&tty_token);
1633 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1634 term = tp->t_termios;
1636 error = ttsetcompat(tp, &cmd, data, &term);
1638 lwkt_reltoken(&tty_token);
1642 data = (caddr_t)&term;
1644 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1646 struct termios *dt = (struct termios *)data;
1647 struct termios *lt = mynor & CALLOUT_MASK
1648 ? &com->lt_out : &com->lt_in;
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;
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);
1673 error = ttioctl(tp, cmd, data, ap->a_fflag);
1674 disc_optim(tp, &tp->t_termios, com);
1675 if (error != ENOIOCTL) {
1677 lwkt_reltoken(&tty_token);
1683 outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
1685 cd_etc(com, CD1400_ETC_SENDBREAK);
1690 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1692 cd_etc(com, CD1400_ETC_STOPBREAK);
1696 (void)commctl(com, TIOCM_DTR, DMBIS);
1699 (void)commctl(com, TIOCM_DTR, DMBIC);
1702 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
1703 * changes get undone on the next call to comparam().
1706 (void)commctl(com, *(int *)data, DMSET);
1709 (void)commctl(com, *(int *)data, DMBIS);
1712 (void)commctl(com, *(int *)data, DMBIC);
1715 *(int *)data = commctl(com, 0, DMGET);
1718 /* must be root since the wait applies to following logins */
1719 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1722 lwkt_reltoken(&tty_token);
1725 com->dtr_wait = *(int *)data * hz / 100;
1728 *(int *)data = com->dtr_wait * 100 / hz;
1731 com->do_timestamp = TRUE;
1732 *(struct timeval *)data = com->timestamp;
1734 case TIOCDCDTIMESTAMP:
1735 com->do_dcd_timestamp = TRUE;
1736 *(struct timeval *)data = com->dcd_timestamp;
1740 lwkt_reltoken(&tty_token);
1744 lwkt_reltoken(&tty_token);
1749 siopoll(void *data, void *frame)
1753 lwkt_gettoken(&tty_token);
1757 if (com_events == 0) {
1758 lwkt_reltoken(&tty_token);
1762 for (unit = 0; unit < NSIO; ++unit) {
1767 com = com_addr(unit);
1773 * XXX forget any events related to closed devices
1774 * (actually never opened devices) so that we don't
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;
1788 "sio%d: %d events for device with no tp\n",
1792 if (com->iptr != com->ibuf) {
1797 if (com->state & CS_CHECKMSR) {
1798 u_char delta_modem_status;
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;
1807 if (delta_modem_status & MSR_DCD)
1808 (*linesw[tp->t_line].l_modem)
1809 (tp, com->prev_modem_status & MSR_DCD);
1811 if (com->extra_state & CSE_ODONE) {
1813 com_events -= LOTS_OF_EVENTS;
1814 com->extra_state &= ~CSE_ODONE;
1816 if (!(com->state & CS_BUSY)) {
1817 tp->t_state &= ~TS_BUSY;
1820 if (com->etc != ETC_NONE) {
1821 if (com->etc == ETC_BREAK_ENDED)
1822 com->etc = ETC_NONE;
1826 if (com->state & CS_ODONE) {
1828 com_events -= LOTS_OF_EVENTS;
1829 com->state &= ~CS_ODONE;
1831 (*linesw[tp->t_line].l_start)(tp);
1833 if (com_events == 0)
1836 if (com_events >= LOTS_OF_EVENTS)
1838 lwkt_reltoken(&tty_token);
1842 comparam(struct tty *tp, struct termios *t)
1858 lwkt_gettoken(&tty_token);
1859 /* do historical conversions */
1860 if (t->c_ispeed == 0)
1861 t->c_ispeed = t->c_ospeed;
1863 unit = DEV_TO_UNIT(tp->t_dev);
1864 com = com_addr(unit);
1866 /* check requested parameters */
1867 cy_clock = CY_CLOCK(com->gfrcr_image);
1868 idivisor = comspeed(t->c_ispeed, cy_clock, &iprescaler);
1870 lwkt_reltoken(&tty_token);
1873 odivisor = comspeed(t->c_ospeed, cy_clock, &oprescaler);
1875 lwkt_reltoken(&tty_token);
1879 /* parameters are OK, convert them to the com struct and the device */
1882 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */
1884 (void)commctl(com, TIOCM_DTR, DMBIS);
1887 * This returns with interrupts disabled so that we can complete
1888 * the speed change atomically.
1890 (void) siosetwater(com, t->c_ispeed);
1892 /* XXX we don't actually change the speed atomically. */
1895 if (idivisor != 0) {
1896 cd_setreg(com, CD1400_RBPR, idivisor);
1897 cd_setreg(com, CD1400_RCOR, iprescaler);
1899 if (odivisor != 0) {
1900 cd_setreg(com, CD1400_TBPR, odivisor);
1901 cd_setreg(com, CD1400_TCOR, oprescaler);
1907 * transmitter enable (always set)
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);
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]);
1931 * set channel option register 1 -
1938 if (cflag & PARENB) {
1940 opt |= CD1400_COR1_PARODD;
1941 opt |= CD1400_COR1_PARNORMAL;
1944 if (!(iflag & INPCK))
1945 opt |= CD1400_COR1_NOINPCK;
1948 if (cflag & CSTOPB) {
1950 opt |= CD1400_COR1_STOP2;
1953 switch (cflag & CSIZE) {
1956 opt |= CD1400_COR1_CS5;
1960 opt |= CD1400_COR1_CS6;
1964 opt |= CD1400_COR1_CS7;
1968 opt |= CD1400_COR1_CS8;
1972 if (opt != com->cor[0]) {
1973 cor_change |= CD1400_CCR_COR1;
1974 cd_setreg(com, CD1400_COR1, com->cor[0] = opt);
1978 * Set receive time-out period, normally to max(one char time, 5 ms).
1980 if (t->c_ispeed == 0)
1981 itimeout = cd_getreg(com, CD1400_RTPR);
1983 itimeout = (1000 * bits + t->c_ispeed - 1) / t->c_ispeed;
1989 if (itimeout < MIN_RTP)
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;
1997 cd_setreg(com, CD1400_RTPR, itimeout);
2000 * set channel option register 2 -
2006 opt |= CD1400_COR2_IXANY;
2008 opt |= CD1400_COR2_IXOFF;
2010 #ifndef SOFT_CTS_OFLOW
2011 if (cflag & CCTS_OFLOW)
2012 opt |= CD1400_COR2_CCTS_OFLOW;
2015 if (opt != com->cor[1]) {
2016 cor_change |= CD1400_CCR_COR2;
2017 cd_setreg(com, CD1400_COR2, com->cor[1] = opt);
2022 * set channel option register 3 -
2023 * receiver FIFO interrupt threshold
2026 opt = RxFifoThreshold;
2028 if (t->c_lflag & ICANON)
2029 opt |= CD1400_COR3_SCD34; /* detect INTR & SUSP chars */
2031 /* detect and transparently handle START and STOP chars */
2032 opt |= CD1400_COR3_FCT | CD1400_COR3_SCD12;
2034 if (opt != com->cor[2]) {
2035 cor_change |= CD1400_CCR_COR3;
2036 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2039 /* notify the CD1400 if COR1-3 have changed */
2041 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | cor_change);
2044 * set channel option register 4 -
2047 * received exception processing
2051 opt |= CD1400_COR4_IGNCR;
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
2059 opt |= CD1400_COR4_ICRNL;
2061 opt |= CD1400_COR4_INLCR;
2064 opt |= CD1400_COR4_IGNBRK | CD1400_COR4_NOBRKINT;
2066 * The `-ignbrk -brkint parmrk' case is not handled by the hardware,
2067 * so only tell the hardware about -brkint if -parmrk.
2069 if (!(iflag & (BRKINT | PARMRK)))
2070 opt |= CD1400_COR4_NOBRKINT;
2072 /* XXX using this "intelligence" breaks reporting of overruns. */
2074 opt |= CD1400_COR4_PFO_DISCARD;
2077 opt |= CD1400_COR4_PFO_ESC;
2079 opt |= CD1400_COR4_PFO_NUL;
2082 opt |= CD1400_COR4_PFO_EXCEPTION;
2084 cd_setreg(com, CD1400_COR4, opt);
2087 * set channel option register 5 -
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;
2096 if (t->c_oflag & ONLCR)
2097 opt |= CD1400_COR5_ONLCR;
2098 if (t->c_oflag & OCRNL)
2099 opt |= CD1400_COR5_OCRNL;
2101 cd_setreg(com, CD1400_COR5, opt);
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.
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
2118 opt = CD1400_MCOR1_CDzd;
2119 #ifdef SOFT_CTS_OFLOW
2120 if (cflag & CCTS_OFLOW)
2121 opt |= CD1400_MCOR1_CTSzd;
2123 cd_setreg(com, CD1400_MCOR1, opt);
2126 * set modem change option register 2
2127 * generate modem interrupts on specific 0 -> 1 input transitions
2129 opt = CD1400_MCOR2_CDod;
2130 #ifdef SOFT_CTS_OFLOW
2131 if (cflag & CCTS_OFLOW)
2132 opt |= CD1400_MCOR2_CTSod;
2134 cd_setreg(com, CD1400_MCOR2, opt);
2137 * XXX should have done this long ago, but there is too much state
2138 * to change all atomically.
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;
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.
2153 } else if (com->state & CS_RTS_IFLOW) {
2154 com->state &= ~CS_RTS_IFLOW;
2156 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS
2157 * on here, since comstart() won't do it later.
2160 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2162 cd_setreg(com, com->mcr_rts_reg,
2163 com->mcr_image |= com->mcr_rts);
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.
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;
2181 /* XXX shouldn't call functions while intrs are disabled. */
2182 disc_optim(tp, t, com);
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().
2189 if (com->state >= (CS_BUSY | CS_TTGO))
2192 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2193 if (!(com->intr_enable & CD1400_SRER_TXRDY))
2194 cd_setreg(com, CD1400_SRER,
2196 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2197 | CD1400_SRER_TXRDY);
2199 if (com->intr_enable & CD1400_SRER_TXRDY)
2200 cd_setreg(com, CD1400_SRER,
2202 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2203 | CD1400_SRER_TXMPTY);
2209 if (com->ibufold != NULL) {
2210 kfree(com->ibufold, M_DEVBUF);
2211 com->ibufold = NULL;
2213 lwkt_reltoken(&tty_token);
2218 * NOTE: Must be called with tty_token held
2221 siosetwater(struct com_s *com, speed_t speed)
2228 ASSERT_LWKT_TOKEN_HELD(&tty_token);
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).
2236 cp4ticks = speed / 10 / hz * 4;
2237 for (ibufsize = 128; ibufsize < cp4ticks;)
2239 if (ibufsize == com->ibufsize) {
2245 * Allocate input buffer. The extra factor of 2 in the size is
2246 * to allow for an error byte for each input byte.
2248 ibuf = kmalloc(2 * ibufsize, M_DEVBUF, M_WAITOK);
2250 /* Initialize non-critical variables. */
2251 com->ibufold = com->ibuf;
2252 com->ibufsize = ibufsize;
2255 tp->t_ififosize = 2 * ibufsize;
2256 tp->t_ispeedwat = (speed_t)-1;
2257 tp->t_ospeedwat = (speed_t)-1;
2261 * Read current input buffer, if any. Continue with interrupts
2265 if (com->iptr != com->ibuf)
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
2276 * The low watermark is invisibly 0 since the buffer is always
2277 * emptied all at once.
2279 com->iptr = com->ibuf = ibuf;
2280 com->ibufend = ibuf + ibufsize;
2281 com->ierroff = ibufsize;
2282 com->ihighwater = ibuf + 3 * ibufsize / 4;
2287 comstart(struct tty *tp)
2295 unit = DEV_TO_UNIT(tp->t_dev);
2296 com = com_addr(unit);
2298 lwkt_gettoken(&tty_token);
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,
2311 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2312 | CD1400_SRER_TXMPTY);
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,
2319 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2320 | CD1400_SRER_TXRDY);
2322 if (tp->t_state & TS_TBLOCK) {
2323 if (com->mcr_image & com->mcr_rts && com->state & CS_RTS_IFLOW)
2325 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2327 cd_setreg(com, com->mcr_rts_reg,
2328 com->mcr_image &= ~com->mcr_rts);
2331 if (!(com->mcr_image & com->mcr_rts)
2332 && com->iptr < com->ihighwater
2333 && com->state & CS_RTS_IFLOW)
2335 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2337 cd_setreg(com, com->mcr_rts_reg,
2338 com->mcr_image |= com->mcr_rts);
2342 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2344 lwkt_reltoken(&tty_token);
2348 if (tp->t_outq.c_cc != 0) {
2352 if (!com->obufs[0].l_queued) {
2356 com->obufs[0].l_tail
2357 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2359 com->obufs[0].l_next = NULL;
2360 com->obufs[0].l_queued = TRUE;
2362 if (com->state & CS_BUSY) {
2363 qp = com->obufq.l_next;
2364 while ((next = qp->l_next) != NULL)
2366 qp->l_next = &com->obufs[0];
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
2374 cd_setreg(com, CD1400_SRER,
2377 & ~CD1400_SRER_TXMPTY)
2378 | CD1400_SRER_TXRDY);
2382 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2386 com->obufs[1].l_tail
2387 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2389 com->obufs[1].l_next = NULL;
2390 com->obufs[1].l_queued = TRUE;
2392 if (com->state & CS_BUSY) {
2393 qp = com->obufq.l_next;
2394 while ((next = qp->l_next) != NULL)
2396 qp->l_next = &com->obufs[1];
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
2404 cd_setreg(com, CD1400_SRER,
2407 & ~CD1400_SRER_TXMPTY)
2408 | CD1400_SRER_TXRDY);
2412 tp->t_state |= TS_BUSY;
2420 if (com->state >= (CS_BUSY | CS_TTGO))
2421 siointr1(com); /* fake interrupt to start output */
2425 lwkt_reltoken(&tty_token);
2430 comstop(struct tty *tp, int rw)
2435 lwkt_gettoken(&tty_token);
2436 com = com_addr(DEV_TO_UNIT(tp->t_dev));
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;
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);
2457 /* XXX no way to reset only input fifo. */
2458 com_events -= (com->iptr - com->ibuf);
2459 com->iptr = com->ibuf;
2464 if (rw & FWRITE && com->etc == ETC_NONE)
2465 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
2467 lwkt_reltoken(&tty_token);
2471 * NOTE: Must be called with tty_token held
2474 commctl(struct com_s *com, int bits, int how)
2479 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2482 if (com->channel_control & CD1400_CCR_RCVEN)
2484 mcr = com->mcr_image;
2485 if (mcr & com->mcr_dtr)
2487 if (mcr & com->mcr_rts)
2488 /* XXX wired on for Cyclom-8Ys */
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.
2499 msr = cd_getreg(com, CD1400_MSVR2);
2508 /* XXX not connected except for Cyclom-16Y? */
2513 if (bits & TIOCM_DTR)
2514 mcr |= com->mcr_dtr;
2515 if (bits & TIOCM_RTS)
2516 mcr |= com->mcr_rts;
2520 com->mcr_image = mcr;
2521 cd_setreg(com, CD1400_MSVR1, mcr);
2522 cd_setreg(com, CD1400_MSVR2, mcr);
2525 com->mcr_image = mcr = com->mcr_image | mcr;
2526 cd_setreg(com, CD1400_MSVR1, mcr);
2527 cd_setreg(com, CD1400_MSVR2, mcr);
2530 com->mcr_image = mcr = com->mcr_image & ~mcr;
2531 cd_setreg(com, CD1400_MSVR1, mcr);
2532 cd_setreg(com, CD1400_MSVR2, mcr);
2540 * NOTE: Must be called with tty_token held
2549 ASSERT_LWKT_TOKEN_HELD(&tty_token);
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.
2555 callout_stop(&sio_timeout_handle);
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) {
2564 if (com->poll || com->poll_output) {
2565 sio_timeout = hz > 200 ? hz / 200 : 1;
2572 sio_timeouts_until_log = hz / sio_timeout;
2573 callout_reset(&sio_timeout_handle, sio_timeout,
2576 /* Flush error messages, if any. */
2577 sio_timeouts_until_log = 1;
2579 callout_stop(&sio_timeout_handle);
2584 comwakeup(void *chan)
2589 lwkt_gettoken(&tty_token);
2590 callout_reset(&sio_timeout_handle, sio_timeout, comwakeup, NULL);
2594 * Recover from lost output interrupts.
2595 * Poll any lines that don't use interrupts.
2597 for (unit = 0; unit < NSIO; ++unit) {
2598 com = com_addr(unit);
2600 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2609 * Check for and log errors, but not too often.
2611 if (--sio_timeouts_until_log > 0) {
2612 lwkt_reltoken(&tty_token);
2615 sio_timeouts_until_log = hz / sio_timeout;
2616 for (unit = 0; unit < NSIO; ++unit) {
2619 com = com_addr(unit);
2622 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2627 delta = com->delta_error_counts[errnum];
2628 com->delta_error_counts[errnum] = 0;
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);
2638 lwkt_reltoken(&tty_token);
2642 disc_optim(struct tty *tp, struct termios *t, struct com_s *com)
2644 #ifndef SOFT_HOTCHAR
2648 lwkt_gettoken(&tty_token);
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.
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;
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;
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);
2676 lwkt_reltoken(&tty_token);
2680 /* standard line discipline input routine */
2682 cyinput(int c, struct tty *tp)
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.
2692 comspeed(speed_t speed, u_long cy_clock, int *prescaler_io)
2702 if (speed < 0 || speed > 150000)
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)
2712 divider = (cy_clock / prescaler * 2 / speed + 1) / 2; /* round off */
2715 actual = cy_clock/prescaler/divider;
2717 /* 10 times error in percent: */
2718 error = ((actual - (long)speed) * 2000 / (long)speed + 1) / 2;
2720 /* 3.0% max error tolerance */
2721 if (error < -30 || error > 30)
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);
2731 *prescaler_io = prescaler_unit;
2736 * NOTE: Must be called with tty_token held
2739 cd1400_channel_cmd(struct com_s *com, int cmd)
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);
2748 * NOTE: Must be called with tty_token held
2751 cd1400_channel_cmd_wait(struct com_s *com)
2753 struct timeval start;
2757 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2759 if (cd_getreg(com, CD1400_CCR) == 0)
2763 if (cd_getreg(com, CD1400_CCR) == 0)
2766 usec = 1000000 * (tv.tv_sec - start.tv_sec) +
2767 tv.tv_usec - start.tv_usec;
2770 "cy%d: channel command timeout (%ld usec)\n",
2778 * NOTE: Must be called with tty_token held
2781 cd_etc(struct com_s *com, int etc)
2783 ASSERT_LWKT_TOKEN_HELD(&tty_token);
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.
2796 if (com->etc == etc) {
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))) {
2810 cd_setreg(com, CD1400_SRER,
2812 = (com->intr_enable & ~CD1400_SRER_TXRDY) | CD1400_SRER_TXMPTY);
2815 while (com->etc == etc
2816 && tsleep(&com->etc, PCATCH, "cyetc", 0) == 0)
2821 * NOTE: Must be called with tty_token held
2824 cd_getreg(struct com_s *com, int reg)
2826 struct com_s *basecom;
2833 ASSERT_LWKT_TOKEN_HELD(&tty_token);
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;
2842 if (basecom->car != car)
2843 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2844 val = cd_inb(iobase, reg, cy_align);
2851 * NOTE: Must be called with tty_token held
2854 cd_setreg(struct com_s *com, int reg, int val)
2856 struct com_s *basecom;
2862 ASSERT_LWKT_TOKEN_HELD(&tty_token);
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;
2871 if (basecom->car != car)
2872 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2873 cd_outb(iobase, reg, cy_align, val);
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);
2895 lwkt_gettoken(&tty_token);
2896 iobase = com->iobase;
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));
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);
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);
2930 kprintf("tty state:\t\t\t0x%08x\n", tp->t_state);
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);
2935 kprintf("tty state:\t\t\tclosed\n");
2937 lwkt_reltoken(&tty_token);
2939 #endif /* CyDebug */