2 * cyclades cyclom-y serial driver
3 * Andrew Herbert <andrew@werple.apana.org.au>, 17 August 1993
5 * Copyright (c) 1993 Andrew Herbert.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name Andrew Herbert may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
22 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "opt_compat.h"
43 * Temporary compile-time configuration options.
45 #define RxFifoThreshold (CD1400_RX_FIFO_SIZE / 2)
46 /* Number of chars in the receiver FIFO before an
47 * an interrupt is generated. Should depend on
48 * line speed. Needs to be about 6 on a 486DX33
49 * for 4 active ports at 115200 bps. Why doesn't
52 #define PollMode /* Use polling-based irq service routine, not the
53 * hardware svcack lines. Must be defined for
54 * Cyclom-16Y boards. Less efficient for Cyclom-8Ys,
55 * and stops 4 * 115200 bps from working.
57 #undef Smarts /* Enable slightly more CD1400 intelligence. Mainly
58 * the output CR/LF processing, plus we can avoid a
59 * few checks usually done in ttyinput().
61 * XXX not fully implemented, and not particularly
64 #undef CyDebug /* Include debugging code (not very expensive). */
66 /* These will go away. */
70 #include <sys/param.h>
71 #include <sys/systm.h>
74 #include <sys/dkstat.h>
75 #include <sys/fcntl.h>
77 #include <sys/interrupt.h>
79 #include <sys/kernel.h>
80 #include <sys/malloc.h>
81 #include <sys/mutex.h>
82 #include <sys/syslog.h>
83 #include <machine/lock.h>
84 #include <machine/psl.h>
86 #include <i386/isa/isa_device.h>
87 #include <i386/isa/cyreg.h>
88 #include <i386/isa/ic/cd1400.h>
91 #error "The cy device requires the old isa compatibility shims"
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
137 #define sio_irec cy_irec
138 #define sio_lock cy_lock
139 #define sio_inited cy_inited
140 #define sio_timeout cy_timeout
141 #define sio_timeout_handle cy_timeout_handle
142 #define sio_timeouts_until_log cy_timeouts_until_log
143 #define sio_tty cy_tty
145 #define CY_MAX_PORTS (CD1400_NO_OF_CHANNELS * CY_MAX_CD1400s)
147 /* We encode the cyclom unit number (cyu) in spare bits in the IVR's. */
148 #define CD1400_xIVR_CHAN_SHIFT 3
149 #define CD1400_xIVR_CHAN 0x1F
152 * ETC states. com->etc may also contain a hardware ETC command value,
153 * meaning that execution of that command is pending.
155 #define ETC_NONE 0 /* we depend on bzero() setting this */
156 #define ETC_BREAK_STARTING 1
157 #define ETC_BREAK_STARTED 2
158 #define ETC_BREAK_ENDING 3
159 #define ETC_BREAK_ENDED 4
161 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */
163 #define CALLOUT_MASK 0x80
164 #define CONTROL_MASK 0x60
165 #define CONTROL_INIT_STATE 0x20
166 #define CONTROL_LOCK_STATE 0x40
167 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
168 #define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK)
169 #define MINOR_TO_UNIT(mynor) (((mynor) >> 16) * CY_MAX_PORTS \
170 | (((mynor) & 0xff) & ~MINOR_MAGIC_MASK))
174 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
175 * than the other bits so that they can be tested as a group without masking
178 * The following com and tty flags correspond closely:
179 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and
181 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart())
182 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam())
183 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam())
184 * TS_FLUSH is not used.
185 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
186 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
188 #define CS_BUSY 0x80 /* output in progress */
189 #define CS_TTGO 0x40 /* output not stopped by XOFF */
190 #define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */
191 #define CS_CHECKMSR 1 /* check of MSR scheduled */
192 #define CS_CTS_OFLOW 2 /* use CTS output flow control */
193 #define CS_DTR_OFF 0x10 /* DTR held off */
194 #define CS_ODONE 4 /* output completed */
195 #define CS_RTS_IFLOW 8 /* use RTS input flow control */
196 #define CSE_ODONE 1 /* output transmitted */
198 static char const * const error_desc[] = {
201 #define CE_INTERRUPT_BUF_OVERFLOW 1
202 "interrupt-level buffer overflow",
203 #define CE_TTY_BUF_OVERFLOW 2
204 "tty-level buffer overflow",
208 #define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum])
210 /* types. XXX - should be elsewhere */
211 typedef u_char bool_t; /* boolean */
212 typedef u_char volatile *cy_addr;
214 /* queue of linear buffers */
216 u_char *l_head; /* next char to process */
217 u_char *l_tail; /* one past the last char to process */
218 struct lbq *l_next; /* next in queue */
219 bool_t l_queued; /* nonzero if queued */
222 /* com device structure */
224 u_char state; /* miscellaneous flag bits */
225 bool_t active_out; /* nonzero if the callout device is open */
227 u_char cfcr_image; /* copy of value written to CFCR */
229 u_char etc; /* pending Embedded Transmit Command */
230 u_char extra_state; /* more flag bits, separate for order trick */
232 u_char fifo_image; /* copy of value written to FIFO */
234 u_char gfrcr_image; /* copy of value read from GFRCR */
236 bool_t hasfifo; /* nonzero for 16550 UARTs */
237 bool_t loses_outints; /* nonzero if device loses output interrupts */
239 u_char mcr_dtr; /* MCR bit that is wired to DTR */
240 u_char mcr_image; /* copy of value written to MCR */
241 u_char mcr_rts; /* MCR bit that is wired to RTS */
244 bool_t multiport; /* is this unit part of a multiport device? */
245 #endif /* COM_MULTIPORT */
246 bool_t no_irq; /* nonzero if irq is not attached */
247 bool_t poll; /* nonzero if polling is required */
248 bool_t poll_output; /* nonzero if polling for output is required */
250 int unit; /* unit number */
251 int dtr_wait; /* time to hold DTR down on close (* 1/hz) */
255 u_int wopeners; /* # processes waiting for DCD in open() */
258 * The high level of the driver never reads status registers directly
259 * because there would be too many side effects to handle conveniently.
260 * Instead, it reads copies of the registers stored here by the
263 u_char last_modem_status; /* last MSR read by intr handler */
264 u_char prev_modem_status; /* last MSR handled by high level */
266 u_char hotchar; /* ldisc-specific char to be handled ASAP */
267 u_char *ibuf; /* start of input buffer */
268 u_char *ibufend; /* end of input buffer */
269 u_char *ibufold; /* old input buffer, to be freed */
270 u_char *ihighwater; /* threshold in input buffer */
271 u_char *iptr; /* next free spot in input buffer */
272 int ibufsize; /* size of ibuf (not include error bytes) */
273 int ierroff; /* offset of error bytes in ibuf */
275 struct lbq obufq; /* head of queue of output buffers */
276 struct lbq obufs[2]; /* output buffers */
278 int cy_align; /* index for register alignment */
279 cy_addr cy_iobase; /* base address of this port's cyclom */
280 cy_addr iobase; /* base address of this port's cd1400 */
281 int mcr_rts_reg; /* cd1400 reg number of reg holding mcr_rts */
283 struct tty *tp; /* cross reference */
286 struct termios it_in; /* should be in struct tty */
287 struct termios it_out;
290 struct termios lt_in; /* should be in struct tty */
291 struct termios lt_out;
294 bool_t do_dcd_timestamp;
295 struct timeval timestamp;
296 struct timeval dcd_timestamp;
298 u_long bytes_in; /* statistics */
300 u_int delta_error_counts[CE_NTYPES];
301 u_long error_counts[CE_NTYPES];
303 u_int recv_exception; /* exception chars received */
304 u_int mdm; /* modem signal changes */
306 u_int start_count; /* no. of calls to comstart() */
307 u_int start_real; /* no. of calls that did something */
309 u_char car; /* CD1400 CAR shadow (if first unit in cd) */
310 u_char channel_control;/* CD1400 CCR control command shadow */
311 u_char cor[3]; /* CD1400 COR1-3 shadows */
312 u_char intr_enable; /* CD1400 SRER shadow */
315 * Data area for output buffers. Someday we should build the output
316 * buffer queue without copying data.
322 /* PCI driver entry point. */
323 int cyattach_common __P((cy_addr cy_iobase, int cy_align));
326 static int cy_units __P((cy_addr cy_iobase, int cy_align));
327 static int sioattach __P((struct isa_device *dev));
328 static void cd1400_channel_cmd __P((struct com_s *com, int cmd));
329 static void cd1400_channel_cmd_wait __P((struct com_s *com));
330 static void cd_etc __P((struct com_s *com, int etc));
331 static int cd_getreg __P((struct com_s *com, int reg));
332 static void cd_setreg __P((struct com_s *com, int reg, int val));
333 static timeout_t siodtrwakeup;
334 static void comhardclose __P((struct com_s *com));
335 static void sioinput __P((struct com_s *com));
337 static void siointr1 __P((struct com_s *com));
339 static int commctl __P((struct com_s *com, int bits, int how));
340 static int comparam __P((struct tty *tp, struct termios *t));
341 static void siopoll __P((void *arg));
342 static int sioprobe __P((struct isa_device *dev));
343 static void siosettimeout __P((void));
344 static int siosetwater __P((struct com_s *com, speed_t speed));
345 static int comspeed __P((speed_t speed, u_long cy_clock,
347 static void comstart __P((struct tty *tp));
348 static void comstop __P((struct tty *tp, int rw));
349 static timeout_t comwakeup;
350 static void disc_optim __P((struct tty *tp, struct termios *t,
354 void cystatus __P((int unit));
357 static char driver_name[] = "cy";
358 static struct mtx sio_lock;
359 static int sio_inited;
361 /* table and macro for fast conversion from a unit number to its com struct */
362 static struct com_s *p_com_addr[NSIO];
363 #define com_addr(unit) (p_com_addr[unit])
365 struct isa_driver siodriver = {
366 INTR_TYPE_TTY | INTR_FAST,
371 COMPAT_ISA_DRIVER(cy, cydriver); /* XXX */
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 #define CDEV_MAJOR 48
379 static struct cdevsw sio_cdevsw = {
381 /* close */ sioclose,
383 /* write */ siowrite,
384 /* ioctl */ sioioctl,
387 /* strategy */ nostrategy,
388 /* name */ driver_name,
389 /* maj */ CDEV_MAJOR,
396 static int comconsole = -1;
397 static speed_t comdefaultrate = TTYDEF_SPEED;
398 static u_int com_events; /* input chars + weighted output completions */
400 static int sio_timeout;
401 static int sio_timeouts_until_log;
402 static struct callout_handle sio_timeout_handle
403 = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle);
405 static struct tty *sio_tty[NSIO];
407 static struct tty sio_tty[NSIO];
409 static const int nsio_tty = NSIO;
412 static u_int cd_inbs;
413 static u_int cy_inbs;
414 static u_int cd_outbs;
415 static u_int cy_outbs;
416 static u_int cy_svrr_probes;
417 static u_int cy_timeouts;
420 static int cy_chip_offset[] = {
421 0x0000, 0x0400, 0x0800, 0x0c00, 0x0200, 0x0600, 0x0a00, 0x0e00,
423 static int cy_nr_cd1400s[NCY];
424 static int cy_total_devices;
425 #undef RxFifoThreshold
426 static int volatile RxFifoThreshold = (CD1400_RX_FIFO_SIZE / 2);
430 struct isa_device *dev;
434 iobase = (cy_addr)dev->id_maddr;
436 if (atomic_cmpset_int(&sio_inited, 0, 1))
437 mtx_init(&sio_lock, driver_name, MTX_SPIN);
439 /* Cyclom-16Y hardware reset (Cyclom-8Ys don't care) */
440 cy_inb(iobase, CY16_RESET, 0); /* XXX? */
441 DELAY(500); /* wait for the board to get its act together */
443 /* this is needed to get the board out of reset */
444 cy_outb(iobase, CY_CLEAR_INTR, 0, 0);
447 return (cy_units(iobase, 0) == 0 ? 0 : -1);
451 cy_units(cy_iobase, cy_align)
456 u_char firmware_version;
460 for (cyu = 0; cyu < CY_MAX_CD1400s; ++cyu) {
461 iobase = cy_iobase + (cy_chip_offset[cyu] << cy_align);
463 /* wait for chip to become ready for new command */
464 for (i = 0; i < 10; i++) {
466 if (!cd_inb(iobase, CD1400_CCR, cy_align))
470 /* clear the GFRCR register */
471 cd_outb(iobase, CD1400_GFRCR, cy_align, 0);
473 /* issue a reset command */
474 cd_outb(iobase, CD1400_CCR, cy_align,
475 CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
477 /* wait for the CD1400 to initialize itself */
478 for (i = 0; i < 200; i++) {
481 /* retrieve firmware version */
482 firmware_version = cd_inb(iobase, CD1400_GFRCR,
484 if ((firmware_version & 0xf0) == 0x40)
489 * Anything in the 0x40-0x4F range is fine.
490 * If one CD1400 is bad then we don't support higher
491 * numbered good ones on this board.
493 if ((firmware_version & 0xf0) != 0x40)
501 struct isa_device *isdp;
505 adapter = cyattach_common((cy_addr) isdp->id_maddr, 0);
511 * This kludge is to allow ISA/PCI device specifications in the
512 * kernel config file to be in any order.
514 if (isdp->id_unit != adapter) {
515 printf("cy%d: attached as cy%d\n", isdp->id_unit, adapter);
516 isdp->id_unit = adapter; /* XXX */
518 isdp->id_ointr = siointr;
519 /* isdp->id_ri_flags |= RI_FAST; XXX unimplemented - use newbus! */
524 cyattach_common(cy_iobase, cy_align)
530 u_char firmware_version;
535 adapter = cy_total_devices;
536 if ((u_int)adapter >= NCY) {
538 "cy%d: can't attach adapter: insufficient cy devices configured\n",
542 ncyu = cy_units(cy_iobase, cy_align);
545 cy_nr_cd1400s[adapter] = ncyu;
548 unit = adapter * CY_MAX_PORTS;
549 for (cyu = 0; cyu < ncyu; ++cyu) {
552 iobase = (cy_addr) (cy_iobase
553 + (cy_chip_offset[cyu] << cy_align));
554 firmware_version = cd_inb(iobase, CD1400_GFRCR, cy_align);
556 /* Set up a receive timeout period of than 1+ ms. */
557 cd_outb(iobase, CD1400_PPR, cy_align,
558 howmany(CY_CLOCK(firmware_version)
559 / CD1400_PPR_PRESCALER, 1000));
561 for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; ++cdu, ++unit) {
565 com = malloc(sizeof *com, M_DEVBUF, M_NOWAIT | M_ZERO);
569 com->gfrcr_image = firmware_version;
570 if (CY_RTS_DTR_SWAPPED(firmware_version)) {
571 com->mcr_dtr = MCR_RTS;
572 com->mcr_rts = MCR_DTR;
573 com->mcr_rts_reg = CD1400_MSVR2;
575 com->mcr_dtr = MCR_DTR;
576 com->mcr_rts = MCR_RTS;
577 com->mcr_rts_reg = CD1400_MSVR1;
579 com->dtr_wait = 3 * hz;
580 com->obufs[0].l_head = com->obuf1;
581 com->obufs[1].l_head = com->obuf2;
583 com->cy_align = cy_align;
584 com->cy_iobase = cy_iobase;
585 com->iobase = iobase;
586 com->car = ~CD1400_CAR_CHAN;
589 * We don't use all the flags from <sys/ttydefaults.h> since they
590 * are only relevant for logins. It's important to have echo off
591 * initially so that the line doesn't start blathering before the
592 * echo flag can be turned off.
594 com->it_in.c_iflag = 0;
595 com->it_in.c_oflag = 0;
596 com->it_in.c_cflag = TTYDEF_CFLAG;
597 com->it_in.c_lflag = 0;
598 if (unit == comconsole) {
599 com->it_in.c_iflag = TTYDEF_IFLAG;
600 com->it_in.c_oflag = TTYDEF_OFLAG;
601 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
602 com->it_in.c_lflag = TTYDEF_LFLAG;
603 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
605 if (siosetwater(com, com->it_in.c_ispeed) != 0) {
606 mtx_unlock_spin(&sio_lock);
610 mtx_unlock_spin(&sio_lock);
611 termioschars(&com->it_in);
612 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
613 com->it_out = com->it_in;
616 com_addr(unit) = com;
619 if (sio_ih == NULL) {
620 cdevsw_add(&sio_cdevsw);
621 swi_add(&tty_ithd, "tty:cy", siopoll, NULL, SWI_TTY, 0,
624 make_dev(&sio_cdevsw, unit,
625 UID_ROOT, GID_WHEEL, 0600, "ttyc%r%r", adapter,
626 unit % CY_MAX_PORTS);
627 make_dev(&sio_cdevsw, unit | CONTROL_INIT_STATE,
628 UID_ROOT, GID_WHEEL, 0600, "ttyic%r%r", adapter,
629 unit % CY_MAX_PORTS);
630 make_dev(&sio_cdevsw, unit | CONTROL_LOCK_STATE,
631 UID_ROOT, GID_WHEEL, 0600, "ttylc%r%r", adapter,
632 unit % CY_MAX_PORTS);
633 make_dev(&sio_cdevsw, unit | CALLOUT_MASK,
634 UID_UUCP, GID_DIALER, 0660, "cuac%r%r", adapter,
635 unit % CY_MAX_PORTS);
636 make_dev(&sio_cdevsw, unit | CALLOUT_MASK | CONTROL_INIT_STATE,
637 UID_UUCP, GID_DIALER, 0660, "cuaic%r%r", adapter,
638 unit % CY_MAX_PORTS);
639 make_dev(&sio_cdevsw, unit | CALLOUT_MASK | CONTROL_LOCK_STATE,
640 UID_UUCP, GID_DIALER, 0660, "cualc%r%r", adapter,
641 unit % CY_MAX_PORTS);
645 /* ensure an edge for the next interrupt */
646 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
652 sioopen(dev, flag, mode, p)
666 unit = MINOR_TO_UNIT(mynor);
667 if ((u_int) unit >= NSIO || (com = com_addr(unit)) == NULL)
669 if (mynor & CONTROL_MASK)
672 tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
674 tp = com->tp = &sio_tty[unit];
679 * We jump to this label after all non-interrupted sleeps to pick
680 * up any changes of the device state.
683 while (com->state & CS_DTR_OFF) {
684 error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "cydtr", 0);
688 if (tp->t_state & TS_ISOPEN) {
690 * The device is open, so everything has been initialized.
693 if (mynor & CALLOUT_MASK) {
694 if (!com->active_out) {
699 if (com->active_out) {
700 if (flag & O_NONBLOCK) {
704 error = tsleep(&com->active_out,
705 TTIPRI | PCATCH, "cybi", 0);
711 if (tp->t_state & TS_XCLUDE &&
718 * The device isn't open, so there are no conflicts.
719 * Initialize it. Initialization is done twice in many
720 * cases: to preempt sleeping callin opens if we are
721 * callout, and to complete a callin open after DCD rises.
723 tp->t_oproc = comstart;
724 tp->t_stop = comstop;
725 tp->t_param = comparam;
727 tp->t_termios = mynor & CALLOUT_MASK
728 ? com->it_out : com->it_in;
730 /* Encode per-board unit in LIVR for access in intr routines. */
731 cd_setreg(com, CD1400_LIVR,
732 (unit & CD1400_xIVR_CHAN) << CD1400_xIVR_CHAN_SHIFT);
734 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
736 com->poll = com->no_irq;
737 com->poll_output = com->loses_outints;
740 error = comparam(tp, &tp->t_termios);
747 * (Re)enable and flush fifos.
749 * Certain SMC chips cause problems if the fifos
750 * are enabled while input is ready. Turn off the
751 * fifo if necessary to clear the input. We test
752 * the input ready bit after enabling the fifos
753 * since we've already enabled them in comparam()
754 * and to handle races between enabling and fresh
758 outb(iobase + com_fifo,
759 FIFO_RCV_RST | FIFO_XMT_RST
762 if (!(inb(com->line_status_port) & LSR_RXRDY))
764 outb(iobase + com_fifo, 0);
766 (void) inb(com->data_port);
770 mtx_lock_spin(&sio_lock);
771 (void) inb(com->line_status_port);
772 (void) inb(com->data_port);
773 com->prev_modem_status = com->last_modem_status
774 = inb(com->modem_status_port);
775 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS
777 mtx_unlock_spin(&sio_lock);
780 * Flush fifos. This requires a full channel reset which
781 * also disables the transmitter and receiver. Recover
784 cd1400_channel_cmd(com,
785 CD1400_CCR_CMDRESET | CD1400_CCR_CHANRESET);
786 cd1400_channel_cmd(com, com->channel_control);
788 mtx_lock_spin(&sio_lock);
789 com->prev_modem_status = com->last_modem_status
790 = cd_getreg(com, CD1400_MSVR2);
791 cd_setreg(com, CD1400_SRER,
793 = CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
794 mtx_unlock_spin(&sio_lock);
797 * Handle initial DCD. Callout devices get a fake initial
798 * DCD (trapdoor DCD). If we are callout, then any sleeping
799 * callin opens get woken up and resume sleeping on "cybi"
800 * instead of "cydcd".
803 * XXX `mynor & CALLOUT_MASK' should be
804 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
805 * TRAPDOOR_CARRIER is the default initial state for callout
806 * devices and SOFT_CARRIER is like CLOCAL except it hides
809 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
810 (*linesw[tp->t_line].l_modem)(tp, 1);
813 * Wait for DCD if necessary.
815 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
816 && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
818 error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "cydcd", 0);
824 error = (*linesw[tp->t_line].l_open)(dev, tp);
825 disc_optim(tp, &tp->t_termios, com);
826 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
827 com->active_out = TRUE;
831 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
837 sioclose(dev, flag, mode, p)
849 if (mynor & CONTROL_MASK)
851 com = com_addr(MINOR_TO_UNIT(mynor));
854 cd_etc(com, CD1400_ETC_STOPBREAK);
855 (*linesw[tp->t_line].l_close)(tp, flag);
856 disc_optim(tp, &tp->t_termios, com);
857 comstop(tp, FREAD | FWRITE);
862 #ifdef broken /* session holds a ref to the tty; can't deallocate */
864 com->tp = sio_tty[unit] = NULL;
879 iobase = com->iobase;
883 com->poll_output = FALSE;
885 com->do_timestamp = 0;
887 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
890 mtx_lock_spin(&sio_lock);
892 cd_setreg(com, CD1400_COR2, com->cor[1] &= ~CD1400_COR2_ETC);
893 mtx_unlock_spin(&sio_lock);
894 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
899 outb(iobase + com_ier, 0);
901 mtx_lock_spin(&sio_lock);
902 cd_setreg(com, CD1400_SRER, com->intr_enable = 0);
903 mtx_unlock_spin(&sio_lock);
906 if ((tp->t_cflag & HUPCL)
908 * XXX we will miss any carrier drop between here and the
909 * next open. Perhaps we should watch DCD even when the
910 * port is closed; it is not sufficient to check it at
911 * the next open because it might go up and down while
912 * we're not watching.
915 && !(com->prev_modem_status & MSR_DCD)
916 && !(com->it_in.c_cflag & CLOCAL))
917 || !(tp->t_state & TS_ISOPEN)) {
918 (void)commctl(com, TIOCM_DTR, DMBIC);
920 /* Disable receiver (leave transmitter enabled). */
921 com->channel_control = CD1400_CCR_CMDCHANCTL
924 cd1400_channel_cmd(com, com->channel_control);
926 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
927 timeout(siodtrwakeup, com, com->dtr_wait);
928 com->state |= CS_DTR_OFF;
935 * Disable fifos so that they are off after controlled
936 * reboots. Some BIOSes fail to detect 16550s when the
939 outb(iobase + com_fifo, 0);
942 com->active_out = FALSE;
943 wakeup(&com->active_out);
944 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */
949 siowrite(dev, uio, flag)
959 if (mynor & CONTROL_MASK)
962 unit = MINOR_TO_UNIT(mynor);
963 tp = com_addr(unit)->tp;
965 * (XXX) We disallow virtual consoles if the physical console is
966 * a serial port. This is in case there is a display attached that
967 * is not the console. In that situation we don't need/want the X
968 * server taking over the console.
970 if (constty != NULL && unit == comconsole)
973 /* XXX duplicate ttwrite(), but without so much output processing on
974 * CR & LF chars. Hardly worth the effort, given that high-throughput
975 * sessions are raw anyhow.
978 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
988 com = (struct com_s *)chan;
989 com->state &= ~CS_DTR_OFF;
990 wakeup(&com->dtr_wait);
994 * Call this function with the sio_lock mutex held. It will return with the
1009 if (!(tp->t_state & TS_ISOPEN)) {
1010 com_events -= (com->iptr - com->ibuf);
1011 com->iptr = com->ibuf;
1014 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1016 * Avoid the grotesquely inefficient lineswitch routine
1017 * (ttyinput) in "raw" mode. It usually takes about 450
1018 * instructions (that's without canonical processing or echo!).
1019 * slinput is reasonably fast (usually 40 instructions plus
1025 * This may look odd, but it is using save-and-enable
1026 * semantics instead of the save-and-disable semantics
1027 * that are used everywhere else.
1029 mtx_unlock_spin(&sio_lock);
1030 incc = com->iptr - buf;
1031 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
1032 && (com->state & CS_RTS_IFLOW
1033 || tp->t_iflag & IXOFF)
1034 && !(tp->t_state & TS_TBLOCK))
1036 com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1037 += b_to_q((char *)buf, incc, &tp->t_rawq);
1041 tp->t_rawcc += incc;
1043 if (tp->t_state & TS_TTSTOP
1044 && (tp->t_iflag & IXANY
1045 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1046 tp->t_state &= ~TS_TTSTOP;
1047 tp->t_lflag &= ~FLUSHO;
1050 mtx_lock_spin(&sio_lock);
1051 } while (buf < com->iptr);
1055 * This may look odd, but it is using save-and-enable
1056 * semantics instead of the save-and-disable semantics
1057 * that are used everywhere else.
1059 mtx_unlock_spin(&sio_lock);
1060 line_status = buf[com->ierroff];
1063 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
1064 if (line_status & LSR_BI)
1065 recv_data |= TTY_BI;
1066 if (line_status & LSR_FE)
1067 recv_data |= TTY_FE;
1068 if (line_status & LSR_OE)
1069 recv_data |= TTY_OE;
1070 if (line_status & LSR_PE)
1071 recv_data |= TTY_PE;
1073 (*linesw[tp->t_line].l_rint)(recv_data, tp);
1074 mtx_lock_spin(&sio_lock);
1075 } while (buf < com->iptr);
1077 com_events -= (com->iptr - com->ibuf);
1078 com->iptr = com->ibuf;
1081 * There is now room for another low-level buffer full of input,
1082 * so enable RTS if it is now disabled and there is room in the
1083 * high-level buffer.
1085 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & com->mcr_rts) &&
1086 !(tp->t_state & TS_TBLOCK))
1088 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1090 cd_setreg(com, com->mcr_rts_reg,
1091 com->mcr_image |= com->mcr_rts);
1106 mtx_lock_spin(&sio_lock); /* XXX could this be placed down lower in the loop? */
1108 baseu = unit * CY_MAX_PORTS;
1109 cy_align = com_addr(baseu)->cy_align;
1110 cy_iobase = com_addr(baseu)->cy_iobase;
1112 /* check each CD1400 in turn */
1113 for (cyu = 0; cyu < cy_nr_cd1400s[unit]; ++cyu) {
1114 iobase = (cy_addr) (cy_iobase
1115 + (cy_chip_offset[cyu] << cy_align));
1116 /* poll to see if it has any work */
1117 status = cd_inb(iobase, CD1400_SVRR, cy_align);
1123 /* service requests as appropriate, giving priority to RX */
1124 if (status & CD1400_SVRR_RXRDY) {
1136 save_rir = cd_inb(iobase, CD1400_RIR, cy_align);
1138 /* enter rx service */
1139 cd_outb(iobase, CD1400_CAR, cy_align, save_rir);
1140 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1141 = save_rir & CD1400_CAR_CHAN;
1143 serv_type = cd_inb(iobase, CD1400_RIVR, cy_align);
1144 com = com_addr(baseu
1145 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1146 & CD1400_xIVR_CHAN));
1148 /* ack receive service */
1149 serv_type = cy_inb(iobase, CY8_SVCACKR, cy_align);
1151 com = com_addr(baseu +
1152 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1153 & CD1400_xIVR_CHAN));
1156 if (serv_type & CD1400_RIVR_EXCEPTION) {
1157 ++com->recv_exception;
1158 line_status = cd_inb(iobase, CD1400_RDSR, cy_align);
1159 /* break/unnattached error bits or real input? */
1160 recv_data = cd_inb(iobase, CD1400_RDSR, cy_align);
1161 #ifndef SOFT_HOTCHAR
1162 if (line_status & CD1400_RDSR_SPECIAL
1163 && com->hotchar != 0)
1164 swi_sched(sio_ih, SWI_NOSWITCH);
1167 #if 1 /* XXX "intelligent" PFO error handling would break O error handling */
1168 if (line_status & (LSR_PE|LSR_FE|LSR_BI)) {
1170 Don't store PE if IGNPAR and BI if IGNBRK,
1171 this hack allows "raw" tty optimization
1172 works even if IGN* is set.
1174 if ( com->tp == NULL
1175 || !(com->tp->t_state & TS_ISOPEN)
1176 || ((line_status & (LSR_PE|LSR_FE))
1177 && (com->tp->t_iflag & IGNPAR))
1178 || ((line_status & LSR_BI)
1179 && (com->tp->t_iflag & IGNBRK)))
1181 if ( (line_status & (LSR_PE|LSR_FE))
1182 && (com->tp->t_state & TS_CAN_BYPASS_L_RINT)
1183 && ((line_status & LSR_FE)
1184 || ((line_status & LSR_PE)
1185 && (com->tp->t_iflag & INPCK))))
1191 if (com->hotchar != 0 && recv_data == com->hotchar)
1192 swi_sched(sio_ih, SWI_NOSWITCH);
1195 if (ioptr >= com->ibufend)
1196 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1198 if (com->do_timestamp)
1199 microtime(&com->timestamp);
1201 ioptr[0] = recv_data;
1202 ioptr[com->ierroff] = line_status;
1203 com->iptr = ++ioptr;
1204 if (ioptr == com->ihighwater
1205 && com->state & CS_RTS_IFLOW)
1207 outb(com->modem_ctl_port,
1208 com->mcr_image &= ~MCR_RTS);
1210 cd_outb(iobase, com->mcr_rts_reg,
1215 if (line_status & LSR_OE)
1216 CE_RECORD(com, CE_OVERRUN);
1222 count = cd_inb(iobase, CD1400_RDCR, cy_align);
1225 com->bytes_in += count;
1227 ifree = com->ibufend - ioptr;
1228 if (count > ifree) {
1230 com_events += ifree;
1232 if (com->do_timestamp)
1233 microtime(&com->timestamp);
1235 recv_data = cd_inb(iobase,
1239 if (com->hotchar != 0
1242 swi_sched(sio_ih, SWI_NOSWITCH);
1244 ioptr[0] = recv_data;
1245 ioptr[com->ierroff] = 0;
1247 } while (--ifree != 0);
1249 com->delta_error_counts
1250 [CE_INTERRUPT_BUF_OVERFLOW] += count;
1252 recv_data = cd_inb(iobase, CD1400_RDSR,
1255 if (com->hotchar != 0
1256 && recv_data == com->hotchar)
1257 swi_sched(sio_ih, SWI_NOSWITCH);
1259 } while (--count != 0);
1261 if (com->do_timestamp)
1262 microtime(&com->timestamp);
1263 if (ioptr <= com->ihighwater
1264 && ioptr + count > com->ihighwater
1265 && com->state & CS_RTS_IFLOW)
1267 outb(com->modem_ctl_port,
1268 com->mcr_image &= ~MCR_RTS);
1270 cd_outb(iobase, com->mcr_rts_reg,
1275 com_events += count;
1277 recv_data = cd_inb(iobase, CD1400_RDSR,
1280 if (com->hotchar != 0
1281 && recv_data == com->hotchar)
1282 swi_sched(sio_ih, SWI_NOSWITCH);
1284 ioptr[0] = recv_data;
1285 ioptr[com->ierroff] = 0;
1287 } while (--count != 0);
1293 /* terminate service context */
1295 cd_outb(iobase, CD1400_RIR, cy_align,
1297 & ~(CD1400_RIR_RDIREQ | CD1400_RIR_RBUSY));
1299 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1302 if (status & CD1400_SVRR_MDMCH) {
1304 u_char modem_status;
1312 save_mir = cd_inb(iobase, CD1400_MIR, cy_align);
1314 /* enter modem service */
1315 cd_outb(iobase, CD1400_CAR, cy_align, save_mir);
1316 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1317 = save_mir & CD1400_CAR_CHAN;
1319 com = com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS
1320 + (save_mir & CD1400_MIR_CHAN));
1322 /* ack modem service */
1323 vector = cy_inb(iobase, CY8_SVCACKM, cy_align);
1325 com = com_addr(baseu
1326 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1327 & CD1400_xIVR_CHAN));
1330 modem_status = cd_inb(iobase, CD1400_MSVR2, cy_align);
1331 if (modem_status != com->last_modem_status) {
1332 if (com->do_dcd_timestamp
1333 && !(com->last_modem_status & MSR_DCD)
1334 && modem_status & MSR_DCD)
1335 microtime(&com->dcd_timestamp);
1338 * Schedule high level to handle DCD changes. Note
1339 * that we don't use the delta bits anywhere. Some
1340 * UARTs mess them up, and it's easy to remember the
1341 * previous bits and calculate the delta.
1343 com->last_modem_status = modem_status;
1344 if (!(com->state & CS_CHECKMSR)) {
1345 com_events += LOTS_OF_EVENTS;
1346 com->state |= CS_CHECKMSR;
1347 swi_sched(sio_ih, SWI_NOSWITCH);
1350 #ifdef SOFT_CTS_OFLOW
1351 /* handle CTS change immediately for crisp flow ctl */
1352 if (com->state & CS_CTS_OFLOW) {
1353 if (modem_status & MSR_CTS) {
1354 com->state |= CS_ODEVREADY;
1355 if (com->state >= (CS_BUSY | CS_TTGO
1357 && !(com->intr_enable
1358 & CD1400_SRER_TXRDY))
1359 cd_outb(iobase, CD1400_SRER,
1363 & ~CD1400_SRER_TXMPTY
1364 | CD1400_SRER_TXRDY);
1366 com->state &= ~CS_ODEVREADY;
1367 if (com->intr_enable
1368 & CD1400_SRER_TXRDY)
1369 cd_outb(iobase, CD1400_SRER,
1373 & ~CD1400_SRER_TXRDY
1374 | CD1400_SRER_TXMPTY);
1380 /* terminate service context */
1382 cd_outb(iobase, CD1400_MIR, cy_align,
1384 & ~(CD1400_MIR_RDIREQ | CD1400_MIR_RBUSY));
1386 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1389 if (status & CD1400_SVRR_TXRDY) {
1398 save_tir = cd_inb(iobase, CD1400_TIR, cy_align);
1400 /* enter tx service */
1401 cd_outb(iobase, CD1400_CAR, cy_align, save_tir);
1402 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1403 = save_tir & CD1400_CAR_CHAN;
1405 com = com_addr(baseu
1406 + cyu * CD1400_NO_OF_CHANNELS
1407 + (save_tir & CD1400_TIR_CHAN));
1409 /* ack transmit service */
1410 vector = cy_inb(iobase, CY8_SVCACKT, cy_align);
1412 com = com_addr(baseu
1413 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1414 & CD1400_xIVR_CHAN));
1417 if (com->etc != ETC_NONE) {
1418 if (com->intr_enable & CD1400_SRER_TXRDY) {
1420 * Here due to sloppy SRER_TXRDY
1421 * enabling. Ignore. Come back when
1424 cd_outb(iobase, CD1400_SRER, cy_align,
1427 & ~CD1400_SRER_TXRDY)
1428 | CD1400_SRER_TXMPTY);
1429 goto terminate_tx_service;
1432 case CD1400_ETC_SENDBREAK:
1433 case CD1400_ETC_STOPBREAK:
1435 * Start the command. Come back on
1436 * next tx empty interrupt, hopefully
1437 * after command has been executed.
1439 cd_outb(iobase, CD1400_COR2, cy_align,
1440 com->cor[1] |= CD1400_COR2_ETC);
1441 cd_outb(iobase, CD1400_TDR, cy_align,
1443 cd_outb(iobase, CD1400_TDR, cy_align,
1445 if (com->etc == CD1400_ETC_SENDBREAK)
1446 com->etc = ETC_BREAK_STARTING;
1448 com->etc = ETC_BREAK_ENDING;
1449 goto terminate_tx_service;
1450 case ETC_BREAK_STARTING:
1452 * BREAK is now on. Continue with
1453 * SRER_TXMPTY processing, hopefully
1456 com->etc = ETC_BREAK_STARTED;
1458 case ETC_BREAK_STARTED:
1460 * Came back due to sloppy SRER_TXMPTY
1461 * enabling. Hope again.
1464 case ETC_BREAK_ENDING:
1466 * BREAK is now off. Continue with
1467 * SRER_TXMPTY processing and don't
1468 * come back. The SWI handler will
1469 * restart tx interrupts if necessary.
1471 cd_outb(iobase, CD1400_COR2, cy_align,
1473 &= ~CD1400_COR2_ETC);
1474 com->etc = ETC_BREAK_ENDED;
1475 if (!(com->state & CS_ODONE)) {
1476 com_events += LOTS_OF_EVENTS;
1477 com->state |= CS_ODONE;
1478 swi_sched(sio_ih, SWI_NOSWITCH);
1481 case ETC_BREAK_ENDED:
1483 * Shouldn't get here. Hope again.
1488 if (com->intr_enable & CD1400_SRER_TXMPTY) {
1489 if (!(com->extra_state & CSE_ODONE)) {
1490 com_events += LOTS_OF_EVENTS;
1491 com->extra_state |= CSE_ODONE;
1492 swi_sched(sio_ih, SWI_NOSWITCH);
1494 cd_outb(iobase, CD1400_SRER, cy_align,
1496 &= ~CD1400_SRER_TXMPTY);
1497 goto terminate_tx_service;
1499 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1503 ioptr = com->obufq.l_head;
1504 ocount = com->obufq.l_tail - ioptr;
1505 if (ocount > CD1400_TX_FIFO_SIZE)
1506 ocount = CD1400_TX_FIFO_SIZE;
1507 com->bytes_out += ocount;
1509 cd_outb(iobase, CD1400_TDR, cy_align,
1511 while (--ocount != 0);
1512 com->obufq.l_head = ioptr;
1513 if (ioptr >= com->obufq.l_tail) {
1516 qp = com->obufq.l_next;
1517 qp->l_queued = FALSE;
1520 com->obufq.l_head = qp->l_head;
1521 com->obufq.l_tail = qp->l_tail;
1522 com->obufq.l_next = qp;
1524 /* output just completed */
1525 com->state &= ~CS_BUSY;
1528 * The setting of CSE_ODONE may be
1529 * stale here. We currently only
1530 * use it when CS_BUSY is set, and
1531 * fixing it when we clear CS_BUSY
1534 if (com->extra_state & CSE_ODONE) {
1535 com_events -= LOTS_OF_EVENTS;
1536 com->extra_state &= ~CSE_ODONE;
1539 cd_outb(iobase, CD1400_SRER, cy_align,
1542 & ~CD1400_SRER_TXRDY)
1543 | CD1400_SRER_TXMPTY);
1545 if (!(com->state & CS_ODONE)) {
1546 com_events += LOTS_OF_EVENTS;
1547 com->state |= CS_ODONE;
1549 /* handle at high level ASAP */
1550 swi_sched(sio_ih, SWI_NOSWITCH);
1555 /* terminate service context */
1556 terminate_tx_service:
1558 cd_outb(iobase, CD1400_TIR, cy_align,
1560 & ~(CD1400_TIR_RDIREQ | CD1400_TIR_RBUSY));
1562 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1567 /* ensure an edge for the next interrupt */
1568 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
1570 swi_sched(sio_ih, SWI_NOSWITCH);
1572 mtx_unlock_spin(&sio_lock);
1584 sioioctl(dev, cmd, data, flag, p)
1596 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1598 struct termios term;
1602 com = com_addr(MINOR_TO_UNIT(mynor));
1603 if (mynor & CONTROL_MASK) {
1606 switch (mynor & CONTROL_MASK) {
1607 case CONTROL_INIT_STATE:
1608 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
1610 case CONTROL_LOCK_STATE:
1611 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
1614 return (ENODEV); /* /dev/nodev */
1621 *ct = *(struct termios *)data;
1624 *(struct termios *)data = *ct;
1627 *(int *)data = TTYDISC;
1630 bzero(data, sizeof(struct winsize));
1637 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1638 term = tp->t_termios;
1640 error = ttsetcompat(tp, &cmd, data, &term);
1644 data = (caddr_t)&term;
1646 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1648 struct termios *dt = (struct termios *)data;
1649 struct termios *lt = mynor & CALLOUT_MASK
1650 ? &com->lt_out : &com->lt_in;
1652 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
1653 | (dt->c_iflag & ~lt->c_iflag);
1654 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
1655 | (dt->c_oflag & ~lt->c_oflag);
1656 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
1657 | (dt->c_cflag & ~lt->c_cflag);
1658 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
1659 | (dt->c_lflag & ~lt->c_lflag);
1660 for (cc = 0; cc < NCCS; ++cc)
1661 if (lt->c_cc[cc] != 0)
1662 dt->c_cc[cc] = tp->t_cc[cc];
1663 if (lt->c_ispeed != 0)
1664 dt->c_ispeed = tp->t_ispeed;
1665 if (lt->c_ospeed != 0)
1666 dt->c_ospeed = tp->t_ospeed;
1668 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1669 if (error != ENOIOCTL)
1672 error = ttioctl(tp, cmd, data, flag);
1673 disc_optim(tp, &tp->t_termios, com);
1674 if (error != ENOIOCTL) {
1681 outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
1683 cd_etc(com, CD1400_ETC_SENDBREAK);
1688 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1690 cd_etc(com, CD1400_ETC_STOPBREAK);
1694 (void)commctl(com, TIOCM_DTR, DMBIS);
1697 (void)commctl(com, TIOCM_DTR, DMBIC);
1700 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
1701 * changes get undone on the next call to comparam().
1704 (void)commctl(com, *(int *)data, DMSET);
1707 (void)commctl(com, *(int *)data, DMBIS);
1710 (void)commctl(com, *(int *)data, DMBIC);
1713 *(int *)data = commctl(com, 0, DMGET);
1716 /* must be root since the wait applies to following logins */
1722 com->dtr_wait = *(int *)data * hz / 100;
1725 *(int *)data = com->dtr_wait * 100 / hz;
1728 com->do_timestamp = TRUE;
1729 *(struct timeval *)data = com->timestamp;
1731 case TIOCDCDTIMESTAMP:
1732 com->do_dcd_timestamp = TRUE;
1733 *(struct timeval *)data = com->dcd_timestamp;
1751 if (com_events == 0)
1754 for (unit = 0; unit < NSIO; ++unit) {
1759 com = com_addr(unit);
1765 * XXX forget any events related to closed devices
1766 * (actually never opened devices) so that we don't
1769 mtx_lock_spin(&sio_lock);
1770 incc = com->iptr - com->ibuf;
1771 com->iptr = com->ibuf;
1772 if (com->state & CS_CHECKMSR) {
1773 incc += LOTS_OF_EVENTS;
1774 com->state &= ~CS_CHECKMSR;
1777 mtx_unlock_spin(&sio_lock);
1780 "sio%d: %d events for device with no tp\n",
1784 if (com->iptr != com->ibuf) {
1785 mtx_lock_spin(&sio_lock);
1787 mtx_unlock_spin(&sio_lock);
1789 if (com->state & CS_CHECKMSR) {
1790 u_char delta_modem_status;
1792 mtx_lock_spin(&sio_lock);
1794 delta_modem_status = com->last_modem_status
1795 ^ com->prev_modem_status;
1796 com->prev_modem_status = com->last_modem_status;
1797 com_events -= LOTS_OF_EVENTS;
1798 com->state &= ~CS_CHECKMSR;
1799 mtx_unlock_spin(&sio_lock);
1800 if (delta_modem_status & MSR_DCD)
1801 (*linesw[tp->t_line].l_modem)
1802 (tp, com->prev_modem_status & MSR_DCD);
1804 if (com->extra_state & CSE_ODONE) {
1805 mtx_lock_spin(&sio_lock);
1806 com_events -= LOTS_OF_EVENTS;
1807 com->extra_state &= ~CSE_ODONE;
1808 mtx_unlock_spin(&sio_lock);
1809 if (!(com->state & CS_BUSY)) {
1810 tp->t_state &= ~TS_BUSY;
1813 if (com->etc != ETC_NONE) {
1814 if (com->etc == ETC_BREAK_ENDED)
1815 com->etc = ETC_NONE;
1819 if (com->state & CS_ODONE) {
1820 mtx_lock_spin(&sio_lock);
1821 com_events -= LOTS_OF_EVENTS;
1822 com->state &= ~CS_ODONE;
1823 mtx_unlock_spin(&sio_lock);
1824 (*linesw[tp->t_line].l_start)(tp);
1826 if (com_events == 0)
1829 if (com_events >= LOTS_OF_EVENTS)
1853 /* do historical conversions */
1854 if (t->c_ispeed == 0)
1855 t->c_ispeed = t->c_ospeed;
1857 unit = DEV_TO_UNIT(tp->t_dev);
1858 com = com_addr(unit);
1860 /* check requested parameters */
1861 cy_clock = CY_CLOCK(com->gfrcr_image);
1862 idivisor = comspeed(t->c_ispeed, cy_clock, &iprescaler);
1865 odivisor = comspeed(t->c_ospeed, cy_clock, &oprescaler);
1869 /* parameters are OK, convert them to the com struct and the device */
1872 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */
1874 (void)commctl(com, TIOCM_DTR, DMBIS);
1876 (void) siosetwater(com, t->c_ispeed);
1878 /* XXX we don't actually change the speed atomically. */
1880 if (idivisor != 0) {
1881 cd_setreg(com, CD1400_RBPR, idivisor);
1882 cd_setreg(com, CD1400_RCOR, iprescaler);
1884 if (odivisor != 0) {
1885 cd_setreg(com, CD1400_TBPR, odivisor);
1886 cd_setreg(com, CD1400_TCOR, oprescaler);
1892 * transmitter enable (always set)
1895 opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
1896 | (cflag & CREAD ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
1897 if (opt != com->channel_control) {
1898 com->channel_control = opt;
1899 cd1400_channel_cmd(com, opt);
1903 /* set special chars */
1904 /* XXX if one is _POSIX_VDISABLE, can't use some others */
1905 if (t->c_cc[VSTOP] != _POSIX_VDISABLE)
1906 cd_setreg(com, CD1400_SCHR1, t->c_cc[VSTOP]);
1907 if (t->c_cc[VSTART] != _POSIX_VDISABLE)
1908 cd_setreg(com, CD1400_SCHR2, t->c_cc[VSTART]);
1909 if (t->c_cc[VINTR] != _POSIX_VDISABLE)
1910 cd_setreg(com, CD1400_SCHR3, t->c_cc[VINTR]);
1911 if (t->c_cc[VSUSP] != _POSIX_VDISABLE)
1912 cd_setreg(com, CD1400_SCHR4, t->c_cc[VSUSP]);
1916 * set channel option register 1 -
1923 if (cflag & PARENB) {
1925 opt |= CD1400_COR1_PARODD;
1926 opt |= CD1400_COR1_PARNORMAL;
1929 if (!(iflag & INPCK))
1930 opt |= CD1400_COR1_NOINPCK;
1933 if (cflag & CSTOPB) {
1935 opt |= CD1400_COR1_STOP2;
1938 switch (cflag & CSIZE) {
1941 opt |= CD1400_COR1_CS5;
1945 opt |= CD1400_COR1_CS6;
1949 opt |= CD1400_COR1_CS7;
1953 opt |= CD1400_COR1_CS8;
1957 if (opt != com->cor[0]) {
1958 cor_change |= CD1400_CCR_COR1;
1959 cd_setreg(com, CD1400_COR1, com->cor[0] = opt);
1963 * Set receive time-out period, normally to max(one char time, 5 ms).
1965 if (t->c_ispeed == 0)
1966 itimeout = cd_getreg(com, CD1400_RTPR);
1968 itimeout = (1000 * bits + t->c_ispeed - 1) / t->c_ispeed;
1974 if (itimeout < MIN_RTP)
1977 if (!(t->c_lflag & ICANON) && t->c_cc[VMIN] != 0 && t->c_cc[VTIME] != 0
1978 && t->c_cc[VTIME] * 10 > itimeout)
1979 itimeout = t->c_cc[VTIME] * 10;
1982 cd_setreg(com, CD1400_RTPR, itimeout);
1985 * set channel option register 2 -
1991 opt |= CD1400_COR2_IXANY;
1993 opt |= CD1400_COR2_IXOFF;
1995 #ifndef SOFT_CTS_OFLOW
1996 if (cflag & CCTS_OFLOW)
1997 opt |= CD1400_COR2_CCTS_OFLOW;
1999 if (opt != com->cor[1]) {
2000 cor_change |= CD1400_CCR_COR2;
2001 cd_setreg(com, CD1400_COR2, com->cor[1] = opt);
2003 mtx_unlock_spin(&sio_lock);
2006 * set channel option register 3 -
2007 * receiver FIFO interrupt threshold
2010 opt = RxFifoThreshold;
2012 if (t->c_lflag & ICANON)
2013 opt |= CD1400_COR3_SCD34; /* detect INTR & SUSP chars */
2015 /* detect and transparently handle START and STOP chars */
2016 opt |= CD1400_COR3_FCT | CD1400_COR3_SCD12;
2018 if (opt != com->cor[2]) {
2019 cor_change |= CD1400_CCR_COR3;
2020 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2023 /* notify the CD1400 if COR1-3 have changed */
2025 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | cor_change);
2028 * set channel option register 4 -
2031 * received exception processing
2035 opt |= CD1400_COR4_IGNCR;
2038 * we need a new ttyinput() for this, as we don't want to
2039 * have ICRNL && INLCR being done in both layers, or to have
2040 * synchronisation problems
2043 opt |= CD1400_COR4_ICRNL;
2045 opt |= CD1400_COR4_INLCR;
2048 opt |= CD1400_COR4_IGNBRK | CD1400_COR4_NOBRKINT;
2050 * The `-ignbrk -brkint parmrk' case is not handled by the hardware,
2051 * so only tell the hardware about -brkint if -parmrk.
2053 if (!(iflag & (BRKINT | PARMRK)))
2054 opt |= CD1400_COR4_NOBRKINT;
2056 /* XXX using this "intelligence" breaks reporting of overruns. */
2058 opt |= CD1400_COR4_PFO_DISCARD;
2061 opt |= CD1400_COR4_PFO_ESC;
2063 opt |= CD1400_COR4_PFO_NUL;
2066 opt |= CD1400_COR4_PFO_EXCEPTION;
2068 cd_setreg(com, CD1400_COR4, opt);
2071 * set channel option register 5 -
2075 opt |= CD1400_COR5_ISTRIP;
2076 if (t->c_iflag & IEXTEN)
2077 /* enable LNEXT (e.g. ctrl-v quoting) handling */
2078 opt |= CD1400_COR5_LNEXT;
2080 if (t->c_oflag & ONLCR)
2081 opt |= CD1400_COR5_ONLCR;
2082 if (t->c_oflag & OCRNL)
2083 opt |= CD1400_COR5_OCRNL;
2085 cd_setreg(com, CD1400_COR5, opt);
2088 * We always generate modem status change interrupts for CD changes.
2089 * Among other things, this is necessary to track TS_CARR_ON for
2090 * pstat to print even when the driver doesn't care. CD changes
2091 * should be rare so interrupts for them are not worth extra code to
2092 * avoid. We avoid interrupts for other modem status changes (except
2093 * for CTS changes when SOFT_CTS_OFLOW is configured) since this is
2094 * simplest and best.
2098 * set modem change option register 1
2099 * generate modem interrupts on which 1 -> 0 input transitions
2100 * also controls auto-DTR output flow-control, which we don't use
2102 opt = CD1400_MCOR1_CDzd;
2103 #ifdef SOFT_CTS_OFLOW
2104 if (cflag & CCTS_OFLOW)
2105 opt |= CD1400_MCOR1_CTSzd;
2107 cd_setreg(com, CD1400_MCOR1, opt);
2110 * set modem change option register 2
2111 * generate modem interrupts on specific 0 -> 1 input transitions
2113 opt = CD1400_MCOR2_CDod;
2114 #ifdef SOFT_CTS_OFLOW
2115 if (cflag & CCTS_OFLOW)
2116 opt |= CD1400_MCOR2_CTSod;
2118 cd_setreg(com, CD1400_MCOR2, opt);
2121 * XXX should have done this long ago, but there is too much state
2122 * to change all atomically.
2124 mtx_lock_spin(&sio_lock);
2126 com->state &= ~CS_TTGO;
2127 if (!(tp->t_state & TS_TTSTOP))
2128 com->state |= CS_TTGO;
2129 if (cflag & CRTS_IFLOW) {
2130 com->state |= CS_RTS_IFLOW;
2132 * If CS_RTS_IFLOW just changed from off to on, the change
2133 * needs to be propagated to MCR_RTS. This isn't urgent,
2134 * so do it later by calling comstart() instead of repeating
2135 * a lot of code from comstart() here.
2137 } else if (com->state & CS_RTS_IFLOW) {
2138 com->state &= ~CS_RTS_IFLOW;
2140 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS
2141 * on here, since comstart() won't do it later.
2144 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2146 cd_setreg(com, com->mcr_rts_reg,
2147 com->mcr_image |= com->mcr_rts);
2152 * Set up state to handle output flow control.
2153 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2154 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2156 com->state |= CS_ODEVREADY;
2157 #ifdef SOFT_CTS_OFLOW
2158 com->state &= ~CS_CTS_OFLOW;
2159 if (cflag & CCTS_OFLOW) {
2160 com->state |= CS_CTS_OFLOW;
2161 if (!(com->last_modem_status & MSR_CTS))
2162 com->state &= ~CS_ODEVREADY;
2165 /* XXX shouldn't call functions while intrs are disabled. */
2166 disc_optim(tp, t, com);
2169 * Recover from fiddling with CS_TTGO. We used to call siointr1()
2170 * unconditionally, but that defeated the careful discarding of
2171 * stale input in sioopen().
2173 if (com->state >= (CS_BUSY | CS_TTGO))
2176 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2177 if (!(com->intr_enable & CD1400_SRER_TXRDY))
2178 cd_setreg(com, CD1400_SRER,
2180 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2181 | CD1400_SRER_TXRDY);
2183 if (com->intr_enable & CD1400_SRER_TXRDY)
2184 cd_setreg(com, CD1400_SRER,
2186 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2187 | CD1400_SRER_TXMPTY);
2190 mtx_unlock_spin(&sio_lock);
2193 if (com->ibufold != NULL) {
2194 free(com->ibufold, M_DEVBUF);
2195 com->ibufold = NULL;
2201 * This function must be called with the sio_lock mutex released and will
2202 * return with it obtained.
2205 siosetwater(com, speed)
2215 * Make the buffer size large enough to handle a softtty interrupt
2216 * latency of about 2 ticks without loss of throughput or data
2217 * (about 3 ticks if input flow control is not used or not honoured,
2218 * but a bit less for CS5-CS7 modes).
2220 cp4ticks = speed / 10 / hz * 4;
2221 for (ibufsize = 128; ibufsize < cp4ticks;)
2223 if (ibufsize == com->ibufsize) {
2224 mtx_lock_spin(&sio_lock);
2229 * Allocate input buffer. The extra factor of 2 in the size is
2230 * to allow for an error byte for each input byte.
2232 ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT);
2234 mtx_lock_spin(&sio_lock);
2238 /* Initialize non-critical variables. */
2239 com->ibufold = com->ibuf;
2240 com->ibufsize = ibufsize;
2243 tp->t_ififosize = 2 * ibufsize;
2244 tp->t_ispeedwat = (speed_t)-1;
2245 tp->t_ospeedwat = (speed_t)-1;
2249 * Read current input buffer, if any. Continue with interrupts
2252 mtx_lock_spin(&sio_lock);
2253 if (com->iptr != com->ibuf)
2257 * Initialize critical variables, including input buffer watermarks.
2258 * The external device is asked to stop sending when the buffer
2259 * exactly reaches high water, or when the high level requests it.
2260 * The high level is notified immediately (rather than at a later
2261 * clock tick) when this watermark is reached.
2262 * The buffer size is chosen so the watermark should almost never
2264 * The low watermark is invisibly 0 since the buffer is always
2265 * emptied all at once.
2267 com->iptr = com->ibuf = ibuf;
2268 com->ibufend = ibuf + ibufsize;
2269 com->ierroff = ibufsize;
2270 com->ihighwater = ibuf + 3 * ibufsize / 4;
2286 unit = DEV_TO_UNIT(tp->t_dev);
2287 com = com_addr(unit);
2295 mtx_lock_spin(&sio_lock);
2296 if (tp->t_state & TS_TTSTOP) {
2297 com->state &= ~CS_TTGO;
2298 if (com->intr_enable & CD1400_SRER_TXRDY)
2299 cd_setreg(com, CD1400_SRER,
2301 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2302 | CD1400_SRER_TXMPTY);
2304 com->state |= CS_TTGO;
2305 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)
2306 && !(com->intr_enable & CD1400_SRER_TXRDY))
2307 cd_setreg(com, CD1400_SRER,
2309 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2310 | CD1400_SRER_TXRDY);
2312 if (tp->t_state & TS_TBLOCK) {
2313 if (com->mcr_image & com->mcr_rts && com->state & CS_RTS_IFLOW)
2315 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2317 cd_setreg(com, com->mcr_rts_reg,
2318 com->mcr_image &= ~com->mcr_rts);
2321 if (!(com->mcr_image & com->mcr_rts)
2322 && com->iptr < com->ihighwater
2323 && 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 mtx_unlock_spin(&sio_lock);
2332 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2337 if (tp->t_outq.c_cc != 0) {
2341 if (!com->obufs[0].l_queued) {
2345 com->obufs[0].l_tail
2346 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2348 com->obufs[0].l_next = NULL;
2349 com->obufs[0].l_queued = TRUE;
2350 mtx_lock_spin(&sio_lock);
2351 if (com->state & CS_BUSY) {
2352 qp = com->obufq.l_next;
2353 while ((next = qp->l_next) != NULL)
2355 qp->l_next = &com->obufs[0];
2357 com->obufq.l_head = com->obufs[0].l_head;
2358 com->obufq.l_tail = com->obufs[0].l_tail;
2359 com->obufq.l_next = &com->obufs[0];
2360 com->state |= CS_BUSY;
2361 if (com->state >= (CS_BUSY | CS_TTGO
2363 cd_setreg(com, CD1400_SRER,
2366 & ~CD1400_SRER_TXMPTY)
2367 | CD1400_SRER_TXRDY);
2369 mtx_unlock_spin(&sio_lock);
2371 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2375 com->obufs[1].l_tail
2376 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2378 com->obufs[1].l_next = NULL;
2379 com->obufs[1].l_queued = TRUE;
2380 mtx_lock_spin(&sio_lock);
2381 if (com->state & CS_BUSY) {
2382 qp = com->obufq.l_next;
2383 while ((next = qp->l_next) != NULL)
2385 qp->l_next = &com->obufs[1];
2387 com->obufq.l_head = com->obufs[1].l_head;
2388 com->obufq.l_tail = com->obufs[1].l_tail;
2389 com->obufq.l_next = &com->obufs[1];
2390 com->state |= CS_BUSY;
2391 if (com->state >= (CS_BUSY | CS_TTGO
2393 cd_setreg(com, CD1400_SRER,
2396 & ~CD1400_SRER_TXMPTY)
2397 | CD1400_SRER_TXRDY);
2399 mtx_unlock_spin(&sio_lock);
2401 tp->t_state |= TS_BUSY;
2408 mtx_lock_spin(&sio_lock);
2409 if (com->state >= (CS_BUSY | CS_TTGO))
2410 siointr1(com); /* fake interrupt to start output */
2411 mtx_unlock_spin(&sio_lock);
2425 com = com_addr(DEV_TO_UNIT(tp->t_dev));
2427 mtx_lock_spin(&sio_lock);
2429 com->obufs[0].l_queued = FALSE;
2430 com->obufs[1].l_queued = FALSE;
2431 if (com->extra_state & CSE_ODONE) {
2432 com_events -= LOTS_OF_EVENTS;
2433 com->extra_state &= ~CSE_ODONE;
2434 if (com->etc != ETC_NONE) {
2435 if (com->etc == ETC_BREAK_ENDED)
2436 com->etc = ETC_NONE;
2440 com->tp->t_state &= ~TS_BUSY;
2441 if (com->state & CS_ODONE)
2442 com_events -= LOTS_OF_EVENTS;
2443 com->state &= ~(CS_ODONE | CS_BUSY);
2446 /* XXX no way to reset only input fifo. */
2447 com_events -= (com->iptr - com->ibuf);
2448 com->iptr = com->ibuf;
2450 mtx_unlock_spin(&sio_lock);
2453 if (rw & FWRITE && com->etc == ETC_NONE)
2454 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
2459 commctl(com, bits, how)
2468 if (com->channel_control & CD1400_CCR_RCVEN)
2470 mcr = com->mcr_image;
2471 if (mcr & com->mcr_dtr)
2473 if (mcr & com->mcr_rts)
2474 /* XXX wired on for Cyclom-8Ys */
2478 * We must read the modem status from the hardware because
2479 * we don't generate modem status change interrupts for all
2480 * changes, so com->prev_modem_status is not guaranteed to
2481 * be up to date. This is safe, unlike for sio, because
2482 * reading the status register doesn't clear pending modem
2483 * status change interrupts.
2485 msr = cd_getreg(com, CD1400_MSVR2);
2494 /* XXX not connected except for Cyclom-16Y? */
2499 if (bits & TIOCM_DTR)
2500 mcr |= com->mcr_dtr;
2501 if (bits & TIOCM_RTS)
2502 mcr |= com->mcr_rts;
2503 mtx_lock_spin(&sio_lock);
2506 com->mcr_image = mcr;
2507 cd_setreg(com, CD1400_MSVR1, mcr);
2508 cd_setreg(com, CD1400_MSVR2, mcr);
2511 com->mcr_image = mcr = com->mcr_image | mcr;
2512 cd_setreg(com, CD1400_MSVR1, mcr);
2513 cd_setreg(com, CD1400_MSVR2, mcr);
2516 com->mcr_image = mcr = com->mcr_image & ~mcr;
2517 cd_setreg(com, CD1400_MSVR1, mcr);
2518 cd_setreg(com, CD1400_MSVR2, mcr);
2521 mtx_unlock_spin(&sio_lock);
2533 * Set our timeout period to 1 second if no polled devices are open.
2534 * Otherwise set it to max(1/200, 1/hz).
2535 * Enable timeouts iff some device is open.
2537 untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
2540 for (unit = 0; unit < NSIO; ++unit) {
2541 com = com_addr(unit);
2542 if (com != NULL && com->tp != NULL
2543 && com->tp->t_state & TS_ISOPEN) {
2546 if (com->poll || com->poll_output) {
2547 sio_timeout = hz > 200 ? hz / 200 : 1;
2554 sio_timeouts_until_log = hz / sio_timeout;
2555 sio_timeout_handle = timeout(comwakeup, (void *)NULL,
2558 /* Flush error messages, if any. */
2559 sio_timeouts_until_log = 1;
2560 comwakeup((void *)NULL);
2561 untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
2572 sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout);
2576 * Recover from lost output interrupts.
2577 * Poll any lines that don't use interrupts.
2579 for (unit = 0; unit < NSIO; ++unit) {
2580 com = com_addr(unit);
2582 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2583 mtx_lock_spin(&sio_lock);
2585 mtx_unlock_spin(&sio_lock);
2591 * Check for and log errors, but not too often.
2593 if (--sio_timeouts_until_log > 0)
2595 sio_timeouts_until_log = hz / sio_timeout;
2596 for (unit = 0; unit < NSIO; ++unit) {
2599 com = com_addr(unit);
2602 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2606 mtx_lock_spin(&sio_lock);
2607 delta = com->delta_error_counts[errnum];
2608 com->delta_error_counts[errnum] = 0;
2609 mtx_unlock_spin(&sio_lock);
2612 total = com->error_counts[errnum] += delta;
2613 log(LOG_ERR, "cy%d: %u more %s%s (total %lu)\n",
2614 unit, delta, error_desc[errnum],
2615 delta == 1 ? "" : "s", total);
2621 disc_optim(tp, t, com)
2626 #ifndef SOFT_HOTCHAR
2631 * XXX can skip a lot more cases if Smarts. Maybe
2632 * (IGNCR | ISTRIP | IXON) in c_iflag. But perhaps we
2633 * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2635 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2636 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2637 && (!(t->c_iflag & PARMRK)
2638 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2639 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2640 && linesw[tp->t_line].l_rint == ttyinput)
2641 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2643 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2644 com->hotchar = linesw[tp->t_line].l_hotchar;
2645 #ifndef SOFT_HOTCHAR
2646 opt = com->cor[2] & ~CD1400_COR3_SCD34;
2647 if (com->hotchar != 0) {
2648 cd_setreg(com, CD1400_SCHR3, com->hotchar);
2649 cd_setreg(com, CD1400_SCHR4, com->hotchar);
2650 opt |= CD1400_COR3_SCD34;
2652 if (opt != com->cor[2]) {
2653 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2654 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR3);
2660 /* standard line discipline input routine */
2666 /* XXX duplicate ttyinput(), but without the IXOFF/IXON/ISTRIP/IPARMRK
2667 * bits, as they are done by the CD1400. Hardly worth the effort,
2668 * given that high-throughput sessions are raw anyhow.
2674 comspeed(speed, cy_clock, prescaler_io)
2687 if (speed < 0 || speed > 150000)
2690 /* determine which prescaler to use */
2691 for (prescaler_unit = 4, prescaler = 2048; prescaler_unit;
2692 prescaler_unit--, prescaler >>= 2) {
2693 if (cy_clock / prescaler / speed > 63)
2697 divider = (cy_clock / prescaler * 2 / speed + 1) / 2; /* round off */
2700 actual = cy_clock/prescaler/divider;
2702 /* 10 times error in percent: */
2703 error = ((actual - (long)speed) * 2000 / (long)speed + 1) / 2;
2705 /* 3.0% max error tolerance */
2706 if (error < -30 || error > 30)
2710 printf("prescaler = %d (%d)\n", prescaler, prescaler_unit);
2711 printf("divider = %d (%x)\n", divider, divider);
2712 printf("actual = %d\n", actual);
2713 printf("error = %d\n", error);
2716 *prescaler_io = prescaler_unit;
2721 cd1400_channel_cmd(com, cmd)
2725 cd1400_channel_cmd_wait(com);
2726 cd_setreg(com, CD1400_CCR, cmd);
2727 cd1400_channel_cmd_wait(com);
2731 cd1400_channel_cmd_wait(com)
2734 struct timeval start;
2738 if (cd_getreg(com, CD1400_CCR) == 0)
2742 if (cd_getreg(com, CD1400_CCR) == 0)
2745 usec = 1000000 * (tv.tv_sec - start.tv_sec) +
2746 tv.tv_usec - start.tv_usec;
2749 "cy%d: channel command timeout (%ld usec)\n",
2763 * We can't change the hardware's ETC state while there are any
2764 * characters in the tx fifo, since those characters would be
2765 * interpreted as commands! Unputting characters from the fifo
2766 * is difficult, so we wait up to 12 character times for the fifo
2767 * to drain. The command will be delayed for up to 2 character
2768 * times for the tx to become empty. Unputting characters from
2769 * the tx holding and shift registers is impossible, so we wait
2770 * for the tx to become empty so that the command is sure to be
2771 * executed soon after we issue it.
2773 mtx_lock_spin(&sio_lock);
2774 if (com->etc == etc)
2776 if ((etc == CD1400_ETC_SENDBREAK
2777 && (com->etc == ETC_BREAK_STARTING
2778 || com->etc == ETC_BREAK_STARTED))
2779 || (etc == CD1400_ETC_STOPBREAK
2780 && (com->etc == ETC_BREAK_ENDING || com->etc == ETC_BREAK_ENDED
2781 || com->etc == ETC_NONE))) {
2782 mtx_unlock_spin(&sio_lock);
2786 cd_setreg(com, CD1400_SRER,
2788 = (com->intr_enable & ~CD1400_SRER_TXRDY) | CD1400_SRER_TXMPTY);
2790 mtx_unlock_spin(&sio_lock);
2791 while (com->etc == etc
2792 && tsleep(&com->etc, TTIPRI | PCATCH, "cyetc", 0) == 0)
2801 struct com_s *basecom;
2807 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2808 car = com->unit & CD1400_CAR_CHAN;
2809 cy_align = com->cy_align;
2810 iobase = com->iobase;
2811 mtx_lock_spin(&sio_lock);
2812 if (basecom->car != car)
2813 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2814 val = cd_inb(iobase, reg, cy_align);
2815 mtx_unlock_spin(&sio_lock);
2820 cd_setreg(com, reg, val)
2825 struct com_s *basecom;
2830 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2831 car = com->unit & CD1400_CAR_CHAN;
2832 cy_align = com->cy_align;
2833 iobase = com->iobase;
2834 mtx_lock_spin(&sio_lock);
2835 if (basecom->car != car)
2836 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2837 cd_outb(iobase, reg, cy_align, val);
2838 mtx_unlock_spin(&sio_lock);
2852 com = com_addr(unit);
2853 printf("info for channel %d\n", unit);
2854 printf("------------------\n");
2855 printf("total cyclom service probes:\t%d\n", cy_svrr_probes);
2856 printf("calls to upper layer:\t\t%d\n", cy_timeouts);
2859 iobase = com->iobase;
2861 printf("cd1400 base address:\\tt%p\n", iobase);
2862 printf("saved channel_control:\t\t0x%02x\n", com->channel_control);
2863 printf("saved cor1-3:\t\t\t0x%02x 0x%02x 0x%02x\n",
2864 com->cor[0], com->cor[1], com->cor[2]);
2865 printf("service request enable reg:\t0x%02x (0x%02x cached)\n",
2866 cd_getreg(com, CD1400_SRER), com->intr_enable);
2867 printf("service request register:\t0x%02x\n",
2868 cd_inb(iobase, CD1400_SVRR, com->cy_align));
2869 printf("modem status:\t\t\t0x%02x (0x%02x cached)\n",
2870 cd_getreg(com, CD1400_MSVR2), com->prev_modem_status);
2871 printf("rx/tx/mdm interrupt registers:\t0x%02x 0x%02x 0x%02x\n",
2872 cd_inb(iobase, CD1400_RIR, com->cy_align),
2873 cd_inb(iobase, CD1400_TIR, com->cy_align),
2874 cd_inb(iobase, CD1400_MIR, com->cy_align));
2876 printf("com state:\t\t\t0x%02x\n", com->state);
2877 printf("calls to comstart():\t\t%d (%d useful)\n",
2878 com->start_count, com->start_real);
2879 printf("rx buffer chars free:\t\t%d\n", com->iptr - com->ibuf);
2881 if (com->obufs[0].l_queued)
2882 ocount += com->obufs[0].l_tail - com->obufs[0].l_head;
2883 if (com->obufs[1].l_queued)
2884 ocount += com->obufs[1].l_tail - com->obufs[1].l_head;
2885 printf("tx buffer chars:\t\t%u\n", ocount);
2886 printf("received chars:\t\t\t%d\n", com->bytes_in);
2887 printf("received exceptions:\t\t%d\n", com->recv_exception);
2888 printf("modem signal deltas:\t\t%d\n", com->mdm);
2889 printf("transmitted chars:\t\t%d\n", com->bytes_out);
2893 printf("tty state:\t\t\t0x%08x\n", tp->t_state);
2895 "upper layer queue lengths:\t%d raw, %d canon, %d output\n",
2896 tp->t_rawq.c_cc, tp->t_canq.c_cc, tp->t_outq.c_cc);
2898 printf("tty state:\t\t\tclosed\n");
2900 #endif /* CyDebug */