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.
30 * $FreeBSD: src/sys/i386/isa/cy.c,v 1.97.2.2 2001/08/22 13:04:58 bde Exp $
31 * $DragonFly: src/sys/dev/serial/cy/cy.c,v 1.30 2008/10/20 13:16:58 sephe Exp $
34 #include "opt_compat.h"
44 * Temporary compile-time configuration options.
46 #define RxFifoThreshold (CD1400_RX_FIFO_SIZE / 2)
47 /* Number of chars in the receiver FIFO before an
48 * an interrupt is generated. Should depend on
49 * line speed. Needs to be about 6 on a 486DX33
50 * for 4 active ports at 115200 bps. Why doesn't
53 #define PollMode /* Use polling-based irq service routine, not the
54 * hardware svcack lines. Must be defined for
55 * Cyclom-16Y boards. Less efficient for Cyclom-8Ys,
56 * and stops 4 * 115200 bps from working.
58 #undef Smarts /* Enable slightly more CD1400 intelligence. Mainly
59 * the output CR/LF processing, plus we can avoid a
60 * few checks usually done in ttyinput().
62 * XXX not fully implemented, and not particularly
65 #undef CyDebug /* Include debugging code (not very expensive). */
67 /* These will go away. */
71 #include <sys/param.h>
72 #include <sys/systm.h>
76 #include <sys/dkstat.h>
77 #include <sys/fcntl.h>
78 #include <sys/interrupt.h>
79 #include <sys/kernel.h>
80 #include <sys/malloc.h>
81 #include <sys/syslog.h>
82 #include <sys/thread2.h>
83 #include <machine/clock.h>
85 #include <machine/lock.h>
87 #include <machine/psl.h>
89 #include <bus/isa/isa_device.h>
91 #include <machine_base/isa/ic/cd1400.h>
94 #define disable_intr() com_lock()
95 #define enable_intr() com_unlock()
97 #define disable_intr() ((void)0)
98 #define enable_intr() ((void)0)
102 * Dictionary so that I can name everything *sio* or *com* to compare with
103 * sio.c. There is also lots of ugly formatting and unnecessary ifdefs to
104 * simplify the comparision. These will go away.
106 #define LSR_BI CD1400_RDSR_BREAK
107 #define LSR_FE CD1400_RDSR_FE
108 #define LSR_OE CD1400_RDSR_OE
109 #define LSR_PE CD1400_RDSR_PE
110 #define MCR_DTR CD1400_MSVR2_DTR
111 #define MCR_RTS CD1400_MSVR1_RTS
112 #define MSR_CTS CD1400_MSVR2_CTS
113 #define MSR_DCD CD1400_MSVR2_CD
114 #define MSR_DSR CD1400_MSVR2_DSR
115 #define MSR_RI CD1400_MSVR2_RI
116 #define NSIO (NCY * CY_MAX_PORTS)
117 #define comconsole cyconsole
118 #define comdefaultrate cydefaultrate
119 #define com_events cy_events
120 #define comhardclose cyhardclose
121 #define commctl cymctl
122 #define comparam cyparam
123 #define comspeed cyspeed
124 #define comstart cystart
125 #define comwakeup cywakeup
126 #define nsio_tty ncy_tty
127 #define p_com_addr p_cy_addr
128 #define sioattach cyattach
129 #define sioclose cyclose
130 #define siodriver cydriver
131 #define siodtrwakeup cydtrwakeup
132 #define sioinput cyinput
133 #define siointr cyintr
134 #define siointr1 cyintr1
135 #define sioioctl cyioctl
136 #define sioopen cyopen
137 #define siopoll cypoll
138 #define sioprobe cyprobe
139 #define siosettimeout cysettimeout
140 #define siosetwater cysetwater
141 #define comstop cystop
142 #define siowrite cywrite
143 #define sio_registered cy_registered
144 #define sio_timeout cy_timeout
145 #define sio_timeout_handle cy_timeout_handle
146 #define sio_timeouts_until_log cy_timeouts_until_log
147 #define sio_tty cy_tty
149 #define CY_MAX_PORTS (CD1400_NO_OF_CHANNELS * CY_MAX_CD1400s)
151 /* We encode the cyclom unit number (cyu) in spare bits in the IVR's. */
152 #define CD1400_xIVR_CHAN_SHIFT 3
153 #define CD1400_xIVR_CHAN 0x1F
156 * ETC states. com->etc may also contain a hardware ETC command value,
157 * meaning that execution of that command is pending.
159 #define ETC_NONE 0 /* we depend on bzero() setting this */
160 #define ETC_BREAK_STARTING 1
161 #define ETC_BREAK_STARTED 2
162 #define ETC_BREAK_ENDING 3
163 #define ETC_BREAK_ENDED 4
165 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */
167 #define CALLOUT_MASK 0x80
168 #define CONTROL_MASK 0x60
169 #define CONTROL_INIT_STATE 0x20
170 #define CONTROL_LOCK_STATE 0x40
171 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
172 #define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK)
174 * Not all of the magic is parametrized in the following macros. 16 and
175 * 0xff are related to the bitfields in a udev_t. CY_MAX_PORTS must be
176 * ((0xff & ~MINOR_MAGIC_MASK) + 1) for things to work.
178 #define MINOR_TO_UNIT(mynor) (((mynor) >> 16) * CY_MAX_PORTS \
179 | (((mynor) & 0xff) & ~MINOR_MAGIC_MASK))
180 #define UNIT_TO_MINOR(unit) (((unit) / CY_MAX_PORTS) << 16 \
181 | (((unit) & 0xff) & ~MINOR_MAGIC_MASK))
185 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
186 * than the other bits so that they can be tested as a group without masking
189 * The following com and tty flags correspond closely:
190 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and
192 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart())
193 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam())
194 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam())
195 * TS_FLUSH is not used.
196 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
197 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
199 #define CS_BUSY 0x80 /* output in progress */
200 #define CS_TTGO 0x40 /* output not stopped by XOFF */
201 #define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */
202 #define CS_CHECKMSR 1 /* check of MSR scheduled */
203 #define CS_CTS_OFLOW 2 /* use CTS output flow control */
204 #define CS_DTR_OFF 0x10 /* DTR held off */
205 #define CS_ODONE 4 /* output completed */
206 #define CS_RTS_IFLOW 8 /* use RTS input flow control */
207 #define CSE_ODONE 1 /* output transmitted */
209 static char const * const error_desc[] = {
212 #define CE_INTERRUPT_BUF_OVERFLOW 1
213 "interrupt-level buffer overflow",
214 #define CE_TTY_BUF_OVERFLOW 2
215 "tty-level buffer overflow",
219 #define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum])
221 /* types. XXX - should be elsewhere */
222 typedef u_char bool_t; /* boolean */
223 typedef u_char volatile *cy_addr;
225 /* queue of linear buffers */
227 u_char *l_head; /* next char to process */
228 u_char *l_tail; /* one past the last char to process */
229 struct lbq *l_next; /* next in queue */
230 bool_t l_queued; /* nonzero if queued */
233 /* com device structure */
235 u_char state; /* miscellaneous flag bits */
236 bool_t active_out; /* nonzero if the callout device is open */
238 u_char cfcr_image; /* copy of value written to CFCR */
240 u_char etc; /* pending Embedded Transmit Command */
241 u_char extra_state; /* more flag bits, separate for order trick */
243 u_char fifo_image; /* copy of value written to FIFO */
245 u_char gfrcr_image; /* copy of value read from GFRCR */
247 bool_t hasfifo; /* nonzero for 16550 UARTs */
248 bool_t loses_outints; /* nonzero if device loses output interrupts */
250 u_char mcr_dtr; /* MCR bit that is wired to DTR */
251 u_char mcr_image; /* copy of value written to MCR */
252 u_char mcr_rts; /* MCR bit that is wired to RTS */
255 bool_t multiport; /* is this unit part of a multiport device? */
256 #endif /* COM_MULTIPORT */
257 bool_t no_irq; /* nonzero if irq is not attached */
258 bool_t poll; /* nonzero if polling is required */
259 bool_t poll_output; /* nonzero if polling for output is required */
261 int unit; /* unit number */
262 int dtr_wait; /* time to hold DTR down on close (* 1/hz) */
263 struct callout dtr_ch;
267 u_int wopeners; /* # processes waiting for DCD in open() */
270 * The high level of the driver never reads status registers directly
271 * because there would be too many side effects to handle conveniently.
272 * Instead, it reads copies of the registers stored here by the
275 u_char last_modem_status; /* last MSR read by intr handler */
276 u_char prev_modem_status; /* last MSR handled by high level */
278 u_char hotchar; /* ldisc-specific char to be handled ASAP */
279 u_char *ibuf; /* start of input buffer */
280 u_char *ibufend; /* end of input buffer */
281 u_char *ibufold; /* old input buffer, to be freed */
282 u_char *ihighwater; /* threshold in input buffer */
283 u_char *iptr; /* next free spot in input buffer */
284 int ibufsize; /* size of ibuf (not include error bytes) */
285 int ierroff; /* offset of error bytes in ibuf */
287 struct lbq obufq; /* head of queue of output buffers */
288 struct lbq obufs[2]; /* output buffers */
290 int cy_align; /* index for register alignment */
291 cy_addr cy_iobase; /* base address of this port's cyclom */
292 cy_addr iobase; /* base address of this port's cd1400 */
293 int mcr_rts_reg; /* cd1400 reg number of reg holding mcr_rts */
295 struct tty *tp; /* cross reference */
298 struct termios it_in; /* should be in struct tty */
299 struct termios it_out;
302 struct termios lt_in; /* should be in struct tty */
303 struct termios lt_out;
306 bool_t do_dcd_timestamp;
307 struct timeval timestamp;
308 struct timeval dcd_timestamp;
310 u_long bytes_in; /* statistics */
312 u_int delta_error_counts[CE_NTYPES];
313 u_long error_counts[CE_NTYPES];
315 u_int recv_exception; /* exception chars received */
316 u_int mdm; /* modem signal changes */
318 u_int start_count; /* no. of calls to comstart() */
319 u_int start_real; /* no. of calls that did something */
321 u_char car; /* CD1400 CAR shadow (if first unit in cd) */
322 u_char channel_control;/* CD1400 CCR control command shadow */
323 u_char cor[3]; /* CD1400 COR1-3 shadows */
324 u_char intr_enable; /* CD1400 SRER shadow */
327 * Data area for output buffers. Someday we should build the output
328 * buffer queue without copying data.
334 /* PCI driver entry point. */
335 int cyattach_common (cy_addr cy_iobase, int cy_align);
336 void siointr(void *);
338 static int cy_units (cy_addr cy_iobase, int cy_align);
339 static int sioattach (struct isa_device *dev);
340 static void cd1400_channel_cmd (struct com_s *com, int cmd);
341 static void cd1400_channel_cmd_wait (struct com_s *com);
342 static void cd_etc (struct com_s *com, int etc);
343 static int cd_getreg (struct com_s *com, int reg);
344 static void cd_setreg (struct com_s *com, int reg, int val);
345 static timeout_t siodtrwakeup;
346 static void comhardclose (struct com_s *com);
347 static void sioinput (struct com_s *com);
349 static void siointr1 (struct com_s *com);
351 static int commctl (struct com_s *com, int bits, int how);
352 static int comparam (struct tty *tp, struct termios *t);
353 static inthand2_t siopoll;
354 static int sioprobe (struct isa_device *dev);
355 static void siosettimeout (void);
356 static int siosetwater (struct com_s *com, speed_t speed);
357 static int comspeed (speed_t speed, u_long cy_clock,
359 static void comstart (struct tty *tp);
360 static void comstop (struct tty *tp, int rw);
361 static timeout_t comwakeup;
362 static void disc_optim (struct tty *tp, struct termios *t,
366 void cystatus (int unit);
369 static char driver_name[] = "cy";
371 /* table and macro for fast conversion from a unit number to its com struct */
372 static struct com_s *p_com_addr[NSIO];
373 #define com_addr(unit) (p_com_addr[unit])
375 struct isa_driver siodriver = {
376 sioprobe, sioattach, driver_name
379 static d_open_t sioopen;
380 static d_close_t sioclose;
381 static d_write_t siowrite;
382 static d_ioctl_t sioioctl;
384 #define CDEV_MAJOR 48
385 static struct dev_ops sio_ops = {
386 { driver_name, CDEV_MAJOR, D_TTY | D_KQFILTER },
393 .d_kqfilter = ttykqfilter
396 static int comconsole = -1;
397 static speed_t comdefaultrate = TTYDEF_SPEED;
398 static u_int com_events; /* input chars + weighted output completions */
399 static bool_t sio_registered;
400 static int sio_timeout;
401 static int sio_timeouts_until_log;
402 static struct callout sio_timeout_handle;
404 static struct tty *sio_tty[NSIO];
406 static struct tty sio_tty[NSIO];
408 static const int nsio_tty = NSIO;
411 static u_int cd_inbs;
412 static u_int cy_inbs;
413 static u_int cd_outbs;
414 static u_int cy_outbs;
415 static u_int cy_svrr_probes;
416 static u_int cy_timeouts;
419 static int cy_chip_offset[] = {
420 0x0000, 0x0400, 0x0800, 0x0c00, 0x0200, 0x0600, 0x0a00, 0x0e00,
422 static int cy_nr_cd1400s[NCY];
423 static int cy_total_devices;
424 #undef RxFifoThreshold
425 static int volatile RxFifoThreshold = (CD1400_RX_FIFO_SIZE / 2);
428 sioprobe(struct isa_device *dev)
432 iobase = (cy_addr)dev->id_maddr;
434 /* Cyclom-16Y hardware reset (Cyclom-8Ys don't care) */
435 cy_inb(iobase, CY16_RESET, 0); /* XXX? */
436 DELAY(500); /* wait for the board to get its act together */
438 /* this is needed to get the board out of reset */
439 cy_outb(iobase, CY_CLEAR_INTR, 0, 0);
442 return (cy_units(iobase, 0) == 0 ? 0 : -1);
446 cy_units(cy_addr cy_iobase, int cy_align)
449 u_char firmware_version = 0; /* assign to avoid warning */
453 for (cyu = 0; cyu < CY_MAX_CD1400s; ++cyu) {
454 iobase = cy_iobase + (cy_chip_offset[cyu] << cy_align);
456 /* wait for chip to become ready for new command */
457 for (i = 0; i < 10; i++) {
459 if (!cd_inb(iobase, CD1400_CCR, cy_align))
463 /* clear the GFRCR register */
464 cd_outb(iobase, CD1400_GFRCR, cy_align, 0);
466 /* issue a reset command */
467 cd_outb(iobase, CD1400_CCR, cy_align,
468 CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
470 /* wait for the CD1400 to initialize itself */
471 for (i = 0; i < 200; i++) {
474 /* retrieve firmware version */
475 firmware_version = cd_inb(iobase, CD1400_GFRCR,
477 if ((firmware_version & 0xf0) == 0x40)
482 * Anything in the 0x40-0x4F range is fine.
483 * If one CD1400 is bad then we don't support higher
484 * numbered good ones on this board.
486 if ((firmware_version & 0xf0) != 0x40)
493 sioattach(struct isa_device *isdp)
497 adapter = cyattach_common((cy_addr) isdp->id_maddr, 0);
503 * This kludge is to allow ISA/PCI device specifications in the
504 * kernel config file to be in any order.
506 if (isdp->id_unit != adapter) {
507 kprintf("cy%d: attached as cy%d\n", isdp->id_unit, adapter);
508 isdp->id_unit = adapter; /* XXX */
510 isdp->id_intr = (inthand2_t *)siointr;
511 /* isdp->id_ri_flags |= RI_FAST; XXX unimplemented - use newbus! */
516 cyattach_common(cy_addr cy_iobase, int cy_align)
520 u_char firmware_version;
526 adapter = cy_total_devices;
527 if ((u_int)adapter >= NCY) {
529 "cy%d: can't attach adapter: insufficient cy devices configured\n",
533 ncyu = cy_units(cy_iobase, cy_align);
536 cy_nr_cd1400s[adapter] = ncyu;
539 unit = adapter * CY_MAX_PORTS;
540 for (cyu = 0; cyu < ncyu; ++cyu) {
543 iobase = (cy_addr) (cy_iobase
544 + (cy_chip_offset[cyu] << cy_align));
545 firmware_version = cd_inb(iobase, CD1400_GFRCR, cy_align);
547 /* Set up a receive timeout period of than 1+ ms. */
548 cd_outb(iobase, CD1400_PPR, cy_align,
549 howmany(CY_CLOCK(firmware_version)
550 / CD1400_PPR_PRESCALER, 1000));
552 for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; ++cdu, ++unit) {
555 com = kmalloc(sizeof *com, M_DEVBUF, M_WAITOK | M_ZERO);
557 callout_init(&com->dtr_ch);
558 com->gfrcr_image = firmware_version;
559 if (CY_RTS_DTR_SWAPPED(firmware_version)) {
560 com->mcr_dtr = MCR_RTS;
561 com->mcr_rts = MCR_DTR;
562 com->mcr_rts_reg = CD1400_MSVR2;
564 com->mcr_dtr = MCR_DTR;
565 com->mcr_rts = MCR_RTS;
566 com->mcr_rts_reg = CD1400_MSVR1;
568 com->dtr_wait = 3 * hz;
569 com->obufs[0].l_head = com->obuf1;
570 com->obufs[1].l_head = com->obuf2;
572 com->cy_align = cy_align;
573 com->cy_iobase = cy_iobase;
574 com->iobase = iobase;
575 com->car = ~CD1400_CAR_CHAN;
578 * We don't use all the flags from <sys/ttydefaults.h> since they
579 * are only relevant for logins. It's important to have echo off
580 * initially so that the line doesn't start blathering before the
581 * echo flag can be turned off.
583 com->it_in.c_iflag = 0;
584 com->it_in.c_oflag = 0;
585 com->it_in.c_cflag = TTYDEF_CFLAG;
586 com->it_in.c_lflag = 0;
587 if (unit == comconsole) {
588 com->it_in.c_iflag = TTYDEF_IFLAG;
589 com->it_in.c_oflag = TTYDEF_OFLAG;
590 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
591 com->it_in.c_lflag = TTYDEF_LFLAG;
592 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
594 if (siosetwater(com, com->it_in.c_ispeed) != 0) {
596 kfree(com, M_DEVBUF);
600 termioschars(&com->it_in);
601 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
602 com->it_out = com->it_in;
605 com_addr(unit) = com;
608 if (!sio_registered) {
609 callout_init(&sio_timeout_handle);
610 register_swi(SWI_TTY, siopoll, NULL, "cy", NULL);
611 sio_registered = TRUE;
613 minorbase = UNIT_TO_MINOR(unit);
614 dev_ops_add(&sio_ops, UNIT_TO_MINOR(-1), minorbase);
615 make_dev(&sio_ops, minorbase,
616 UID_ROOT, GID_WHEEL, 0600, "ttyc%r%r", adapter,
617 unit % CY_MAX_PORTS);
618 make_dev(&sio_ops, minorbase | CONTROL_INIT_STATE,
619 UID_ROOT, GID_WHEEL, 0600, "ttyic%r%r", adapter,
620 unit % CY_MAX_PORTS);
621 make_dev(&sio_ops, minorbase | CONTROL_LOCK_STATE,
622 UID_ROOT, GID_WHEEL, 0600, "ttylc%r%r", adapter,
623 unit % CY_MAX_PORTS);
624 make_dev(&sio_ops, minorbase | CALLOUT_MASK,
625 UID_UUCP, GID_DIALER, 0660, "cuac%r%r", adapter,
626 unit % CY_MAX_PORTS);
627 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_INIT_STATE,
628 UID_UUCP, GID_DIALER, 0660, "cuaic%r%r", adapter,
629 unit % CY_MAX_PORTS);
630 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE,
631 UID_UUCP, GID_DIALER, 0660, "cualc%r%r", adapter,
632 unit % CY_MAX_PORTS);
634 /* for(cdu...), for(cyu...) terminating blocks */
638 /* ensure an edge for the next interrupt */
639 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
645 sioopen(struct dev_open_args *ap)
647 cdev_t dev = ap->a_head.a_dev;
655 unit = MINOR_TO_UNIT(mynor);
656 if ((u_int) unit >= NSIO || (com = com_addr(unit)) == NULL)
658 if (mynor & CONTROL_MASK)
661 tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
663 tp = com->tp = &sio_tty[unit];
668 * We jump to this label after all non-interrupted sleeps to pick
669 * up any changes of the device state.
672 while (com->state & CS_DTR_OFF) {
673 error = tsleep(&com->dtr_wait, PCATCH, "cydtr", 0);
677 if (tp->t_state & TS_ISOPEN) {
679 * The device is open, so everything has been initialized.
682 if (mynor & CALLOUT_MASK) {
683 if (!com->active_out) {
688 if (com->active_out) {
689 if (ap->a_oflags & O_NONBLOCK) {
693 error = tsleep(&com->active_out,
700 if (tp->t_state & TS_XCLUDE &&
701 suser_cred(ap->a_cred, 0)) {
707 * The device isn't open, so there are no conflicts.
708 * Initialize it. Initialization is done twice in many
709 * cases: to preempt sleeping callin opens if we are
710 * callout, and to complete a callin open after DCD rises.
712 tp->t_oproc = comstart;
713 tp->t_stop = comstop;
714 tp->t_param = comparam;
716 tp->t_termios = mynor & CALLOUT_MASK
717 ? com->it_out : com->it_in;
719 /* Encode per-board unit in LIVR for access in intr routines. */
720 cd_setreg(com, CD1400_LIVR,
721 (unit & CD1400_xIVR_CHAN) << CD1400_xIVR_CHAN_SHIFT);
723 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
725 com->poll = com->no_irq;
726 com->poll_output = com->loses_outints;
729 error = comparam(tp, &tp->t_termios);
736 * (Re)enable and flush fifos.
738 * Certain SMC chips cause problems if the fifos
739 * are enabled while input is ready. Turn off the
740 * fifo if necessary to clear the input. We test
741 * the input ready bit after enabling the fifos
742 * since we've already enabled them in comparam()
743 * and to handle races between enabling and fresh
747 outb(iobase + com_fifo,
748 FIFO_RCV_RST | FIFO_XMT_RST
751 if (!(inb(com->line_status_port) & LSR_RXRDY))
753 outb(iobase + com_fifo, 0);
755 (void) inb(com->data_port);
760 (void) inb(com->line_status_port);
761 (void) inb(com->data_port);
762 com->prev_modem_status = com->last_modem_status
763 = inb(com->modem_status_port);
764 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS
769 * Flush fifos. This requires a full channel reset which
770 * also disables the transmitter and receiver. Recover
773 cd1400_channel_cmd(com,
774 CD1400_CCR_CMDRESET | CD1400_CCR_CHANRESET);
775 cd1400_channel_cmd(com, com->channel_control);
778 com->prev_modem_status = com->last_modem_status
779 = cd_getreg(com, CD1400_MSVR2);
780 cd_setreg(com, CD1400_SRER,
782 = CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
786 * Handle initial DCD. Callout devices get a fake initial
787 * DCD (trapdoor DCD). If we are callout, then any sleeping
788 * callin opens get woken up and resume sleeping on "cybi"
789 * instead of "cydcd".
792 * XXX `mynor & CALLOUT_MASK' should be
793 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
794 * TRAPDOOR_CARRIER is the default initial state for callout
795 * devices and SOFT_CARRIER is like CLOCAL except it hides
798 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
799 (*linesw[tp->t_line].l_modem)(tp, 1);
802 * Wait for DCD if necessary.
804 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
805 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) {
807 error = tsleep(TSA_CARR_ON(tp), PCATCH, "cydcd", 0);
813 error = (*linesw[tp->t_line].l_open)(dev, tp);
814 disc_optim(tp, &tp->t_termios, com);
815 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
816 com->active_out = TRUE;
820 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
826 sioclose(struct dev_close_args *ap)
828 cdev_t dev = ap->a_head.a_dev;
834 if (mynor & CONTROL_MASK)
836 com = com_addr(MINOR_TO_UNIT(mynor));
839 cd_etc(com, CD1400_ETC_STOPBREAK);
840 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
841 disc_optim(tp, &tp->t_termios, com);
842 comstop(tp, FREAD | FWRITE);
847 #ifdef broken /* session holds a ref to the tty; can't deallocate */
849 com->tp = sio_tty[unit] = NULL;
855 comhardclose(struct com_s *com)
862 iobase = com->iobase;
866 com->poll_output = FALSE;
868 com->do_timestamp = 0;
870 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
875 cd_setreg(com, CD1400_COR2, com->cor[1] &= ~CD1400_COR2_ETC);
877 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
882 outb(iobase + com_ier, 0);
885 cd_setreg(com, CD1400_SRER, com->intr_enable = 0);
889 if ((tp->t_cflag & HUPCL)
891 * XXX we will miss any carrier drop between here and the
892 * next open. Perhaps we should watch DCD even when the
893 * port is closed; it is not sufficient to check it at
894 * the next open because it might go up and down while
895 * we're not watching.
898 && !(com->prev_modem_status & MSR_DCD)
899 && !(com->it_in.c_cflag & CLOCAL))
900 || !(tp->t_state & TS_ISOPEN)) {
901 (void)commctl(com, TIOCM_DTR, DMBIC);
903 /* Disable receiver (leave transmitter enabled). */
904 com->channel_control = CD1400_CCR_CMDCHANCTL
907 cd1400_channel_cmd(com, com->channel_control);
909 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
910 callout_reset(&com->dtr_ch, com->dtr_wait,
912 com->state |= CS_DTR_OFF;
919 * Disable fifos so that they are off after controlled
920 * reboots. Some BIOSes fail to detect 16550s when the
923 outb(iobase + com_fifo, 0);
926 com->active_out = FALSE;
927 wakeup(&com->active_out);
928 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */
933 siowrite(struct dev_write_args *ap)
935 cdev_t dev = ap->a_head.a_dev;
936 struct uio *uio = ap->a_uio;
942 if (mynor & CONTROL_MASK)
945 unit = MINOR_TO_UNIT(mynor);
946 tp = com_addr(unit)->tp;
948 * (XXX) We disallow virtual consoles if the physical console is
949 * a serial port. This is in case there is a display attached that
950 * is not the console. In that situation we don't need/want the X
951 * server taking over the console.
953 if (constty != NULL && unit == comconsole)
956 /* XXX duplicate ttwrite(), but without so much output processing on
957 * CR & LF chars. Hardly worth the effort, given that high-throughput
958 * sessions are raw anyhow.
961 return ((*linesw[tp->t_line].l_write)(tp, uio, ap->a_ioflag));
966 siodtrwakeup(void *chan)
970 com = (struct com_s *)chan;
971 com->state &= ~CS_DTR_OFF;
972 wakeup(&com->dtr_wait);
976 sioinput(struct com_s *com)
986 if (!(tp->t_state & TS_ISOPEN)) {
987 com_events -= (com->iptr - com->ibuf);
988 com->iptr = com->ibuf;
991 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
993 * Avoid the grotesquely inefficient lineswitch routine
994 * (ttyinput) in "raw" mode. It usually takes about 450
995 * instructions (that's without canonical processing or echo!).
996 * slinput is reasonably fast (usually 40 instructions plus
1001 incc = com->iptr - buf;
1002 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
1003 && (com->state & CS_RTS_IFLOW
1004 || tp->t_iflag & IXOFF)
1005 && !(tp->t_state & TS_TBLOCK))
1007 com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1008 += b_to_q((char *)buf, incc, &tp->t_rawq);
1012 tp->t_rawcc += incc;
1014 if (tp->t_state & TS_TTSTOP
1015 && (tp->t_iflag & IXANY
1016 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1017 tp->t_state &= ~TS_TTSTOP;
1018 tp->t_lflag &= ~FLUSHO;
1022 } while (buf < com->iptr);
1026 line_status = buf[com->ierroff];
1029 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
1030 if (line_status & LSR_BI)
1031 recv_data |= TTY_BI;
1032 if (line_status & LSR_FE)
1033 recv_data |= TTY_FE;
1034 if (line_status & LSR_OE)
1035 recv_data |= TTY_OE;
1036 if (line_status & LSR_PE)
1037 recv_data |= TTY_PE;
1039 (*linesw[tp->t_line].l_rint)(recv_data, tp);
1041 } while (buf < com->iptr);
1043 com_events -= (com->iptr - com->ibuf);
1044 com->iptr = com->ibuf;
1047 * There is now room for another low-level buffer full of input,
1048 * so enable RTS if it is now disabled and there is room in the
1049 * high-level buffer.
1051 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & com->mcr_rts) &&
1052 !(tp->t_state & TS_TBLOCK))
1054 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1056 cd_setreg(com, com->mcr_rts_reg,
1057 com->mcr_image |= com->mcr_rts);
1064 int unit = (int)arg;
1072 com_lock(); /* XXX could this be placed down lower in the loop? */
1074 baseu = unit * CY_MAX_PORTS;
1075 cy_align = com_addr(baseu)->cy_align;
1076 cy_iobase = com_addr(baseu)->cy_iobase;
1078 /* check each CD1400 in turn */
1079 for (cyu = 0; cyu < cy_nr_cd1400s[unit]; ++cyu) {
1080 iobase = (cy_addr) (cy_iobase
1081 + (cy_chip_offset[cyu] << cy_align));
1082 /* poll to see if it has any work */
1083 status = cd_inb(iobase, CD1400_SVRR, cy_align);
1089 /* service requests as appropriate, giving priority to RX */
1090 if (status & CD1400_SVRR_RXRDY) {
1102 save_rir = cd_inb(iobase, CD1400_RIR, cy_align);
1104 /* enter rx service */
1105 cd_outb(iobase, CD1400_CAR, cy_align, save_rir);
1106 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1107 = save_rir & CD1400_CAR_CHAN;
1109 serv_type = cd_inb(iobase, CD1400_RIVR, cy_align);
1110 com = com_addr(baseu
1111 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1112 & CD1400_xIVR_CHAN));
1114 /* ack receive service */
1115 serv_type = cy_inb(iobase, CY8_SVCACKR, cy_align);
1117 com = com_addr(baseu +
1118 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1119 & CD1400_xIVR_CHAN));
1122 if (serv_type & CD1400_RIVR_EXCEPTION) {
1123 ++com->recv_exception;
1124 line_status = cd_inb(iobase, CD1400_RDSR, cy_align);
1125 /* break/unnattached error bits or real input? */
1126 recv_data = cd_inb(iobase, CD1400_RDSR, cy_align);
1127 #ifndef SOFT_HOTCHAR
1128 if (line_status & CD1400_RDSR_SPECIAL
1129 && com->hotchar != 0)
1132 #if 1 /* XXX "intelligent" PFO error handling would break O error handling */
1133 if (line_status & (LSR_PE|LSR_FE|LSR_BI)) {
1135 Don't store PE if IGNPAR and BI if IGNBRK,
1136 this hack allows "raw" tty optimization
1137 works even if IGN* is set.
1139 if ( com->tp == NULL
1140 || !(com->tp->t_state & TS_ISOPEN)
1141 || ((line_status & (LSR_PE|LSR_FE))
1142 && (com->tp->t_iflag & IGNPAR))
1143 || ((line_status & LSR_BI)
1144 && (com->tp->t_iflag & IGNBRK)))
1146 if ( (line_status & (LSR_PE|LSR_FE))
1147 && (com->tp->t_state & TS_CAN_BYPASS_L_RINT)
1148 && ((line_status & LSR_FE)
1149 || ((line_status & LSR_PE)
1150 && (com->tp->t_iflag & INPCK))))
1156 if (com->hotchar != 0 && recv_data == com->hotchar)
1160 if (ioptr >= com->ibufend)
1161 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1163 if (com->do_timestamp)
1164 microtime(&com->timestamp);
1166 ioptr[0] = recv_data;
1167 ioptr[com->ierroff] = line_status;
1168 com->iptr = ++ioptr;
1169 if (ioptr == com->ihighwater
1170 && com->state & CS_RTS_IFLOW)
1172 outb(com->modem_ctl_port,
1173 com->mcr_image &= ~MCR_RTS);
1175 cd_outb(iobase, com->mcr_rts_reg,
1180 if (line_status & LSR_OE)
1181 CE_RECORD(com, CE_OVERRUN);
1187 count = cd_inb(iobase, CD1400_RDCR, cy_align);
1190 com->bytes_in += count;
1192 ifree = com->ibufend - ioptr;
1193 if (count > ifree) {
1195 com_events += ifree;
1197 if (com->do_timestamp)
1198 microtime(&com->timestamp);
1200 recv_data = cd_inb(iobase,
1204 if (com->hotchar != 0
1209 ioptr[0] = recv_data;
1210 ioptr[com->ierroff] = 0;
1212 } while (--ifree != 0);
1214 com->delta_error_counts
1215 [CE_INTERRUPT_BUF_OVERFLOW] += count;
1217 recv_data = cd_inb(iobase, CD1400_RDSR,
1220 if (com->hotchar != 0
1221 && recv_data == com->hotchar)
1224 } while (--count != 0);
1226 if (com->do_timestamp)
1227 microtime(&com->timestamp);
1228 if (ioptr <= com->ihighwater
1229 && ioptr + count > com->ihighwater
1230 && com->state & CS_RTS_IFLOW)
1232 outb(com->modem_ctl_port,
1233 com->mcr_image &= ~MCR_RTS);
1235 cd_outb(iobase, com->mcr_rts_reg,
1240 com_events += count;
1242 recv_data = cd_inb(iobase, CD1400_RDSR,
1245 if (com->hotchar != 0
1246 && recv_data == com->hotchar)
1249 ioptr[0] = recv_data;
1250 ioptr[com->ierroff] = 0;
1252 } while (--count != 0);
1258 /* terminate service context */
1260 cd_outb(iobase, CD1400_RIR, cy_align,
1262 & ~(CD1400_RIR_RDIREQ | CD1400_RIR_RBUSY));
1264 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1267 if (status & CD1400_SVRR_MDMCH) {
1269 u_char modem_status;
1277 save_mir = cd_inb(iobase, CD1400_MIR, cy_align);
1279 /* enter modem service */
1280 cd_outb(iobase, CD1400_CAR, cy_align, save_mir);
1281 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1282 = save_mir & CD1400_CAR_CHAN;
1284 com = com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS
1285 + (save_mir & CD1400_MIR_CHAN));
1287 /* ack modem service */
1288 vector = cy_inb(iobase, CY8_SVCACKM, cy_align);
1290 com = com_addr(baseu
1291 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1292 & CD1400_xIVR_CHAN));
1295 modem_status = cd_inb(iobase, CD1400_MSVR2, cy_align);
1296 if (modem_status != com->last_modem_status) {
1297 if (com->do_dcd_timestamp
1298 && !(com->last_modem_status & MSR_DCD)
1299 && modem_status & MSR_DCD)
1300 microtime(&com->dcd_timestamp);
1303 * Schedule high level to handle DCD changes. Note
1304 * that we don't use the delta bits anywhere. Some
1305 * UARTs mess them up, and it's easy to remember the
1306 * previous bits and calculate the delta.
1308 com->last_modem_status = modem_status;
1309 if (!(com->state & CS_CHECKMSR)) {
1310 com_events += LOTS_OF_EVENTS;
1311 com->state |= CS_CHECKMSR;
1315 #ifdef SOFT_CTS_OFLOW
1316 /* handle CTS change immediately for crisp flow ctl */
1317 if (com->state & CS_CTS_OFLOW) {
1318 if (modem_status & MSR_CTS) {
1319 com->state |= CS_ODEVREADY;
1320 if (com->state >= (CS_BUSY | CS_TTGO
1322 && !(com->intr_enable
1323 & CD1400_SRER_TXRDY))
1324 cd_outb(iobase, CD1400_SRER,
1328 & ~CD1400_SRER_TXMPTY
1329 | CD1400_SRER_TXRDY);
1331 com->state &= ~CS_ODEVREADY;
1332 if (com->intr_enable
1333 & CD1400_SRER_TXRDY)
1334 cd_outb(iobase, CD1400_SRER,
1338 & ~CD1400_SRER_TXRDY
1339 | CD1400_SRER_TXMPTY);
1345 /* terminate service context */
1347 cd_outb(iobase, CD1400_MIR, cy_align,
1349 & ~(CD1400_MIR_RDIREQ | CD1400_MIR_RBUSY));
1351 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1354 if (status & CD1400_SVRR_TXRDY) {
1363 save_tir = cd_inb(iobase, CD1400_TIR, cy_align);
1365 /* enter tx service */
1366 cd_outb(iobase, CD1400_CAR, cy_align, save_tir);
1367 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1368 = save_tir & CD1400_CAR_CHAN;
1370 com = com_addr(baseu
1371 + cyu * CD1400_NO_OF_CHANNELS
1372 + (save_tir & CD1400_TIR_CHAN));
1374 /* ack transmit service */
1375 vector = cy_inb(iobase, CY8_SVCACKT, cy_align);
1377 com = com_addr(baseu
1378 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1379 & CD1400_xIVR_CHAN));
1382 if (com->etc != ETC_NONE) {
1383 if (com->intr_enable & CD1400_SRER_TXRDY) {
1385 * Here due to sloppy SRER_TXRDY
1386 * enabling. Ignore. Come back when
1389 cd_outb(iobase, CD1400_SRER, cy_align,
1392 & ~CD1400_SRER_TXRDY)
1393 | CD1400_SRER_TXMPTY);
1394 goto terminate_tx_service;
1397 case CD1400_ETC_SENDBREAK:
1398 case CD1400_ETC_STOPBREAK:
1400 * Start the command. Come back on
1401 * next tx empty interrupt, hopefully
1402 * after command has been executed.
1404 cd_outb(iobase, CD1400_COR2, cy_align,
1405 com->cor[1] |= CD1400_COR2_ETC);
1406 cd_outb(iobase, CD1400_TDR, cy_align,
1408 cd_outb(iobase, CD1400_TDR, cy_align,
1410 if (com->etc == CD1400_ETC_SENDBREAK)
1411 com->etc = ETC_BREAK_STARTING;
1413 com->etc = ETC_BREAK_ENDING;
1414 goto terminate_tx_service;
1415 case ETC_BREAK_STARTING:
1417 * BREAK is now on. Continue with
1418 * SRER_TXMPTY processing, hopefully
1421 com->etc = ETC_BREAK_STARTED;
1423 case ETC_BREAK_STARTED:
1425 * Came back due to sloppy SRER_TXMPTY
1426 * enabling. Hope again.
1429 case ETC_BREAK_ENDING:
1431 * BREAK is now off. Continue with
1432 * SRER_TXMPTY processing and don't
1433 * come back. The SWI handler will
1434 * restart tx interrupts if necessary.
1436 cd_outb(iobase, CD1400_COR2, cy_align,
1438 &= ~CD1400_COR2_ETC);
1439 com->etc = ETC_BREAK_ENDED;
1440 if (!(com->state & CS_ODONE)) {
1441 com_events += LOTS_OF_EVENTS;
1442 com->state |= CS_ODONE;
1446 case ETC_BREAK_ENDED:
1448 * Shouldn't get here. Hope again.
1453 if (com->intr_enable & CD1400_SRER_TXMPTY) {
1454 if (!(com->extra_state & CSE_ODONE)) {
1455 com_events += LOTS_OF_EVENTS;
1456 com->extra_state |= CSE_ODONE;
1459 cd_outb(iobase, CD1400_SRER, cy_align,
1461 &= ~CD1400_SRER_TXMPTY);
1462 goto terminate_tx_service;
1464 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1468 ioptr = com->obufq.l_head;
1469 ocount = com->obufq.l_tail - ioptr;
1470 if (ocount > CD1400_TX_FIFO_SIZE)
1471 ocount = CD1400_TX_FIFO_SIZE;
1472 com->bytes_out += ocount;
1474 cd_outb(iobase, CD1400_TDR, cy_align,
1476 while (--ocount != 0);
1477 com->obufq.l_head = ioptr;
1478 if (ioptr >= com->obufq.l_tail) {
1481 qp = com->obufq.l_next;
1482 qp->l_queued = FALSE;
1485 com->obufq.l_head = qp->l_head;
1486 com->obufq.l_tail = qp->l_tail;
1487 com->obufq.l_next = qp;
1489 /* output just completed */
1490 com->state &= ~CS_BUSY;
1493 * The setting of CSE_ODONE may be
1494 * stale here. We currently only
1495 * use it when CS_BUSY is set, and
1496 * fixing it when we clear CS_BUSY
1499 if (com->extra_state & CSE_ODONE) {
1500 com_events -= LOTS_OF_EVENTS;
1501 com->extra_state &= ~CSE_ODONE;
1504 cd_outb(iobase, CD1400_SRER, cy_align,
1507 & ~CD1400_SRER_TXRDY)
1508 | CD1400_SRER_TXMPTY);
1510 if (!(com->state & CS_ODONE)) {
1511 com_events += LOTS_OF_EVENTS;
1512 com->state |= CS_ODONE;
1514 /* handle at high level ASAP */
1520 /* terminate service context */
1521 terminate_tx_service:
1523 cd_outb(iobase, CD1400_TIR, cy_align,
1525 & ~(CD1400_TIR_RDIREQ | CD1400_TIR_RBUSY));
1527 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1532 /* ensure an edge for the next interrupt */
1533 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
1542 siointr1(struct com_s *com)
1548 sioioctl(struct dev_ioctl_args *ap)
1550 cdev_t dev = ap->a_head.a_dev;
1551 caddr_t data = ap->a_data;
1552 u_long cmd = ap->a_cmd;
1557 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1559 struct termios term;
1563 com = com_addr(MINOR_TO_UNIT(mynor));
1564 if (mynor & CONTROL_MASK) {
1567 switch (mynor & CONTROL_MASK) {
1568 case CONTROL_INIT_STATE:
1569 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
1571 case CONTROL_LOCK_STATE:
1572 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
1575 return (ENODEV); /* /dev/nodev */
1579 error = suser_cred(ap->a_cred, 0);
1582 *ct = *(struct termios *)data;
1585 *(struct termios *)data = *ct;
1588 *(int *)data = TTYDISC;
1591 bzero(data, sizeof(struct winsize));
1598 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1599 term = tp->t_termios;
1601 error = ttsetcompat(tp, &cmd, data, &term);
1605 data = (caddr_t)&term;
1607 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1609 struct termios *dt = (struct termios *)data;
1610 struct termios *lt = mynor & CALLOUT_MASK
1611 ? &com->lt_out : &com->lt_in;
1613 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
1614 | (dt->c_iflag & ~lt->c_iflag);
1615 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
1616 | (dt->c_oflag & ~lt->c_oflag);
1617 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
1618 | (dt->c_cflag & ~lt->c_cflag);
1619 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
1620 | (dt->c_lflag & ~lt->c_lflag);
1621 for (cc = 0; cc < NCCS; ++cc)
1622 if (lt->c_cc[cc] != 0)
1623 dt->c_cc[cc] = tp->t_cc[cc];
1624 if (lt->c_ispeed != 0)
1625 dt->c_ispeed = tp->t_ispeed;
1626 if (lt->c_ospeed != 0)
1627 dt->c_ospeed = tp->t_ospeed;
1629 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data,
1630 ap->a_fflag, ap->a_cred);
1631 if (error != ENOIOCTL)
1634 error = ttioctl(tp, cmd, data, ap->a_fflag);
1635 disc_optim(tp, &tp->t_termios, com);
1636 if (error != ENOIOCTL) {
1643 outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
1645 cd_etc(com, CD1400_ETC_SENDBREAK);
1650 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1652 cd_etc(com, CD1400_ETC_STOPBREAK);
1656 (void)commctl(com, TIOCM_DTR, DMBIS);
1659 (void)commctl(com, TIOCM_DTR, DMBIC);
1662 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
1663 * changes get undone on the next call to comparam().
1666 (void)commctl(com, *(int *)data, DMSET);
1669 (void)commctl(com, *(int *)data, DMBIS);
1672 (void)commctl(com, *(int *)data, DMBIC);
1675 *(int *)data = commctl(com, 0, DMGET);
1678 /* must be root since the wait applies to following logins */
1679 error = suser_cred(ap->a_cred, 0);
1684 com->dtr_wait = *(int *)data * hz / 100;
1687 *(int *)data = com->dtr_wait * 100 / hz;
1690 com->do_timestamp = TRUE;
1691 *(struct timeval *)data = com->timestamp;
1693 case TIOCDCDTIMESTAMP:
1694 com->do_dcd_timestamp = TRUE;
1695 *(struct timeval *)data = com->dcd_timestamp;
1706 siopoll(void *data, void *frame)
1713 if (com_events == 0)
1716 for (unit = 0; unit < NSIO; ++unit) {
1721 com = com_addr(unit);
1727 * XXX forget any events related to closed devices
1728 * (actually never opened devices) so that we don't
1732 incc = com->iptr - com->ibuf;
1733 com->iptr = com->ibuf;
1734 if (com->state & CS_CHECKMSR) {
1735 incc += LOTS_OF_EVENTS;
1736 com->state &= ~CS_CHECKMSR;
1742 "sio%d: %d events for device with no tp\n",
1746 if (com->iptr != com->ibuf) {
1751 if (com->state & CS_CHECKMSR) {
1752 u_char delta_modem_status;
1755 delta_modem_status = com->last_modem_status
1756 ^ com->prev_modem_status;
1757 com->prev_modem_status = com->last_modem_status;
1758 com_events -= LOTS_OF_EVENTS;
1759 com->state &= ~CS_CHECKMSR;
1761 if (delta_modem_status & MSR_DCD)
1762 (*linesw[tp->t_line].l_modem)
1763 (tp, com->prev_modem_status & MSR_DCD);
1765 if (com->extra_state & CSE_ODONE) {
1767 com_events -= LOTS_OF_EVENTS;
1768 com->extra_state &= ~CSE_ODONE;
1770 if (!(com->state & CS_BUSY)) {
1771 tp->t_state &= ~TS_BUSY;
1774 if (com->etc != ETC_NONE) {
1775 if (com->etc == ETC_BREAK_ENDED)
1776 com->etc = ETC_NONE;
1780 if (com->state & CS_ODONE) {
1782 com_events -= LOTS_OF_EVENTS;
1783 com->state &= ~CS_ODONE;
1785 (*linesw[tp->t_line].l_start)(tp);
1787 if (com_events == 0)
1790 if (com_events >= LOTS_OF_EVENTS)
1795 comparam(struct tty *tp, struct termios *t)
1811 /* do historical conversions */
1812 if (t->c_ispeed == 0)
1813 t->c_ispeed = t->c_ospeed;
1815 unit = DEV_TO_UNIT(tp->t_dev);
1816 com = com_addr(unit);
1818 /* check requested parameters */
1819 cy_clock = CY_CLOCK(com->gfrcr_image);
1820 idivisor = comspeed(t->c_ispeed, cy_clock, &iprescaler);
1823 odivisor = comspeed(t->c_ospeed, cy_clock, &oprescaler);
1827 /* parameters are OK, convert them to the com struct and the device */
1830 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */
1832 (void)commctl(com, TIOCM_DTR, DMBIS);
1835 * This returns with interrupts disabled so that we can complete
1836 * the speed change atomically.
1838 (void) siosetwater(com, t->c_ispeed);
1840 /* XXX we don't actually change the speed atomically. */
1843 if (idivisor != 0) {
1844 cd_setreg(com, CD1400_RBPR, idivisor);
1845 cd_setreg(com, CD1400_RCOR, iprescaler);
1847 if (odivisor != 0) {
1848 cd_setreg(com, CD1400_TBPR, odivisor);
1849 cd_setreg(com, CD1400_TCOR, oprescaler);
1855 * transmitter enable (always set)
1858 opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
1859 | (cflag & CREAD ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
1860 if (opt != com->channel_control) {
1861 com->channel_control = opt;
1862 cd1400_channel_cmd(com, opt);
1866 /* set special chars */
1867 /* XXX if one is _POSIX_VDISABLE, can't use some others */
1868 if (t->c_cc[VSTOP] != _POSIX_VDISABLE)
1869 cd_setreg(com, CD1400_SCHR1, t->c_cc[VSTOP]);
1870 if (t->c_cc[VSTART] != _POSIX_VDISABLE)
1871 cd_setreg(com, CD1400_SCHR2, t->c_cc[VSTART]);
1872 if (t->c_cc[VINTR] != _POSIX_VDISABLE)
1873 cd_setreg(com, CD1400_SCHR3, t->c_cc[VINTR]);
1874 if (t->c_cc[VSUSP] != _POSIX_VDISABLE)
1875 cd_setreg(com, CD1400_SCHR4, t->c_cc[VSUSP]);
1879 * set channel option register 1 -
1886 if (cflag & PARENB) {
1888 opt |= CD1400_COR1_PARODD;
1889 opt |= CD1400_COR1_PARNORMAL;
1892 if (!(iflag & INPCK))
1893 opt |= CD1400_COR1_NOINPCK;
1896 if (cflag & CSTOPB) {
1898 opt |= CD1400_COR1_STOP2;
1901 switch (cflag & CSIZE) {
1904 opt |= CD1400_COR1_CS5;
1908 opt |= CD1400_COR1_CS6;
1912 opt |= CD1400_COR1_CS7;
1916 opt |= CD1400_COR1_CS8;
1920 if (opt != com->cor[0]) {
1921 cor_change |= CD1400_CCR_COR1;
1922 cd_setreg(com, CD1400_COR1, com->cor[0] = opt);
1926 * Set receive time-out period, normally to max(one char time, 5 ms).
1928 if (t->c_ispeed == 0)
1929 itimeout = cd_getreg(com, CD1400_RTPR);
1931 itimeout = (1000 * bits + t->c_ispeed - 1) / t->c_ispeed;
1937 if (itimeout < MIN_RTP)
1940 if (!(t->c_lflag & ICANON) && t->c_cc[VMIN] != 0 && t->c_cc[VTIME] != 0
1941 && t->c_cc[VTIME] * 10 > itimeout)
1942 itimeout = t->c_cc[VTIME] * 10;
1945 cd_setreg(com, CD1400_RTPR, itimeout);
1948 * set channel option register 2 -
1954 opt |= CD1400_COR2_IXANY;
1956 opt |= CD1400_COR2_IXOFF;
1958 #ifndef SOFT_CTS_OFLOW
1959 if (cflag & CCTS_OFLOW)
1960 opt |= CD1400_COR2_CCTS_OFLOW;
1963 if (opt != com->cor[1]) {
1964 cor_change |= CD1400_CCR_COR2;
1965 cd_setreg(com, CD1400_COR2, com->cor[1] = opt);
1970 * set channel option register 3 -
1971 * receiver FIFO interrupt threshold
1974 opt = RxFifoThreshold;
1976 if (t->c_lflag & ICANON)
1977 opt |= CD1400_COR3_SCD34; /* detect INTR & SUSP chars */
1979 /* detect and transparently handle START and STOP chars */
1980 opt |= CD1400_COR3_FCT | CD1400_COR3_SCD12;
1982 if (opt != com->cor[2]) {
1983 cor_change |= CD1400_CCR_COR3;
1984 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
1987 /* notify the CD1400 if COR1-3 have changed */
1989 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | cor_change);
1992 * set channel option register 4 -
1995 * received exception processing
1999 opt |= CD1400_COR4_IGNCR;
2002 * we need a new ttyinput() for this, as we don't want to
2003 * have ICRNL && INLCR being done in both layers, or to have
2004 * synchronisation problems
2007 opt |= CD1400_COR4_ICRNL;
2009 opt |= CD1400_COR4_INLCR;
2012 opt |= CD1400_COR4_IGNBRK | CD1400_COR4_NOBRKINT;
2014 * The `-ignbrk -brkint parmrk' case is not handled by the hardware,
2015 * so only tell the hardware about -brkint if -parmrk.
2017 if (!(iflag & (BRKINT | PARMRK)))
2018 opt |= CD1400_COR4_NOBRKINT;
2020 /* XXX using this "intelligence" breaks reporting of overruns. */
2022 opt |= CD1400_COR4_PFO_DISCARD;
2025 opt |= CD1400_COR4_PFO_ESC;
2027 opt |= CD1400_COR4_PFO_NUL;
2030 opt |= CD1400_COR4_PFO_EXCEPTION;
2032 cd_setreg(com, CD1400_COR4, opt);
2035 * set channel option register 5 -
2039 opt |= CD1400_COR5_ISTRIP;
2040 if (t->c_iflag & IEXTEN)
2041 /* enable LNEXT (e.g. ctrl-v quoting) handling */
2042 opt |= CD1400_COR5_LNEXT;
2044 if (t->c_oflag & ONLCR)
2045 opt |= CD1400_COR5_ONLCR;
2046 if (t->c_oflag & OCRNL)
2047 opt |= CD1400_COR5_OCRNL;
2049 cd_setreg(com, CD1400_COR5, opt);
2052 * We always generate modem status change interrupts for CD changes.
2053 * Among other things, this is necessary to track TS_CARR_ON for
2054 * pstat to print even when the driver doesn't care. CD changes
2055 * should be rare so interrupts for them are not worth extra code to
2056 * avoid. We avoid interrupts for other modem status changes (except
2057 * for CTS changes when SOFT_CTS_OFLOW is configured) since this is
2058 * simplest and best.
2062 * set modem change option register 1
2063 * generate modem interrupts on which 1 -> 0 input transitions
2064 * also controls auto-DTR output flow-control, which we don't use
2066 opt = CD1400_MCOR1_CDzd;
2067 #ifdef SOFT_CTS_OFLOW
2068 if (cflag & CCTS_OFLOW)
2069 opt |= CD1400_MCOR1_CTSzd;
2071 cd_setreg(com, CD1400_MCOR1, opt);
2074 * set modem change option register 2
2075 * generate modem interrupts on specific 0 -> 1 input transitions
2077 opt = CD1400_MCOR2_CDod;
2078 #ifdef SOFT_CTS_OFLOW
2079 if (cflag & CCTS_OFLOW)
2080 opt |= CD1400_MCOR2_CTSod;
2082 cd_setreg(com, CD1400_MCOR2, opt);
2085 * XXX should have done this long ago, but there is too much state
2086 * to change all atomically.
2090 com->state &= ~CS_TTGO;
2091 if (!(tp->t_state & TS_TTSTOP))
2092 com->state |= CS_TTGO;
2093 if (cflag & CRTS_IFLOW) {
2094 com->state |= CS_RTS_IFLOW;
2096 * If CS_RTS_IFLOW just changed from off to on, the change
2097 * needs to be propagated to MCR_RTS. This isn't urgent,
2098 * so do it later by calling comstart() instead of repeating
2099 * a lot of code from comstart() here.
2101 } else if (com->state & CS_RTS_IFLOW) {
2102 com->state &= ~CS_RTS_IFLOW;
2104 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS
2105 * on here, since comstart() won't do it later.
2108 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2110 cd_setreg(com, com->mcr_rts_reg,
2111 com->mcr_image |= com->mcr_rts);
2116 * Set up state to handle output flow control.
2117 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2118 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2120 com->state |= CS_ODEVREADY;
2121 #ifdef SOFT_CTS_OFLOW
2122 com->state &= ~CS_CTS_OFLOW;
2123 if (cflag & CCTS_OFLOW) {
2124 com->state |= CS_CTS_OFLOW;
2125 if (!(com->last_modem_status & MSR_CTS))
2126 com->state &= ~CS_ODEVREADY;
2129 /* XXX shouldn't call functions while intrs are disabled. */
2130 disc_optim(tp, t, com);
2133 * Recover from fiddling with CS_TTGO. We used to call siointr1()
2134 * unconditionally, but that defeated the careful discarding of
2135 * stale input in sioopen().
2137 if (com->state >= (CS_BUSY | CS_TTGO))
2140 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2141 if (!(com->intr_enable & CD1400_SRER_TXRDY))
2142 cd_setreg(com, CD1400_SRER,
2144 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2145 | CD1400_SRER_TXRDY);
2147 if (com->intr_enable & CD1400_SRER_TXRDY)
2148 cd_setreg(com, CD1400_SRER,
2150 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2151 | CD1400_SRER_TXMPTY);
2157 if (com->ibufold != NULL) {
2158 kfree(com->ibufold, M_DEVBUF);
2159 com->ibufold = NULL;
2165 siosetwater(struct com_s *com, speed_t speed)
2173 * Make the buffer size large enough to handle a softtty interrupt
2174 * latency of about 2 ticks without loss of throughput or data
2175 * (about 3 ticks if input flow control is not used or not honoured,
2176 * but a bit less for CS5-CS7 modes).
2178 cp4ticks = speed / 10 / hz * 4;
2179 for (ibufsize = 128; ibufsize < cp4ticks;)
2181 if (ibufsize == com->ibufsize) {
2187 * Allocate input buffer. The extra factor of 2 in the size is
2188 * to allow for an error byte for each input byte.
2190 ibuf = kmalloc(2 * ibufsize, M_DEVBUF, M_WAITOK);
2192 /* Initialize non-critical variables. */
2193 com->ibufold = com->ibuf;
2194 com->ibufsize = ibufsize;
2197 tp->t_ififosize = 2 * ibufsize;
2198 tp->t_ispeedwat = (speed_t)-1;
2199 tp->t_ospeedwat = (speed_t)-1;
2203 * Read current input buffer, if any. Continue with interrupts
2207 if (com->iptr != com->ibuf)
2211 * Initialize critical variables, including input buffer watermarks.
2212 * The external device is asked to stop sending when the buffer
2213 * exactly reaches high water, or when the high level requests it.
2214 * The high level is notified immediately (rather than at a later
2215 * clock tick) when this watermark is reached.
2216 * The buffer size is chosen so the watermark should almost never
2218 * The low watermark is invisibly 0 since the buffer is always
2219 * emptied all at once.
2221 com->iptr = com->ibuf = ibuf;
2222 com->ibufend = ibuf + ibufsize;
2223 com->ierroff = ibufsize;
2224 com->ihighwater = ibuf + 3 * ibufsize / 4;
2229 comstart(struct tty *tp)
2237 unit = DEV_TO_UNIT(tp->t_dev);
2238 com = com_addr(unit);
2247 if (tp->t_state & TS_TTSTOP) {
2248 com->state &= ~CS_TTGO;
2249 if (com->intr_enable & CD1400_SRER_TXRDY)
2250 cd_setreg(com, CD1400_SRER,
2252 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2253 | CD1400_SRER_TXMPTY);
2255 com->state |= CS_TTGO;
2256 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)
2257 && !(com->intr_enable & CD1400_SRER_TXRDY))
2258 cd_setreg(com, CD1400_SRER,
2260 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2261 | CD1400_SRER_TXRDY);
2263 if (tp->t_state & TS_TBLOCK) {
2264 if (com->mcr_image & com->mcr_rts && com->state & CS_RTS_IFLOW)
2266 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2268 cd_setreg(com, com->mcr_rts_reg,
2269 com->mcr_image &= ~com->mcr_rts);
2272 if (!(com->mcr_image & com->mcr_rts)
2273 && com->iptr < com->ihighwater
2274 && com->state & CS_RTS_IFLOW)
2276 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2278 cd_setreg(com, com->mcr_rts_reg,
2279 com->mcr_image |= com->mcr_rts);
2283 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2288 if (tp->t_outq.c_cc != 0) {
2292 if (!com->obufs[0].l_queued) {
2296 com->obufs[0].l_tail
2297 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2299 com->obufs[0].l_next = NULL;
2300 com->obufs[0].l_queued = TRUE;
2302 if (com->state & CS_BUSY) {
2303 qp = com->obufq.l_next;
2304 while ((next = qp->l_next) != NULL)
2306 qp->l_next = &com->obufs[0];
2308 com->obufq.l_head = com->obufs[0].l_head;
2309 com->obufq.l_tail = com->obufs[0].l_tail;
2310 com->obufq.l_next = &com->obufs[0];
2311 com->state |= CS_BUSY;
2312 if (com->state >= (CS_BUSY | CS_TTGO
2314 cd_setreg(com, CD1400_SRER,
2317 & ~CD1400_SRER_TXMPTY)
2318 | CD1400_SRER_TXRDY);
2322 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2326 com->obufs[1].l_tail
2327 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2329 com->obufs[1].l_next = NULL;
2330 com->obufs[1].l_queued = TRUE;
2332 if (com->state & CS_BUSY) {
2333 qp = com->obufq.l_next;
2334 while ((next = qp->l_next) != NULL)
2336 qp->l_next = &com->obufs[1];
2338 com->obufq.l_head = com->obufs[1].l_head;
2339 com->obufq.l_tail = com->obufs[1].l_tail;
2340 com->obufq.l_next = &com->obufs[1];
2341 com->state |= CS_BUSY;
2342 if (com->state >= (CS_BUSY | CS_TTGO
2344 cd_setreg(com, CD1400_SRER,
2347 & ~CD1400_SRER_TXMPTY)
2348 | CD1400_SRER_TXRDY);
2352 tp->t_state |= TS_BUSY;
2360 if (com->state >= (CS_BUSY | CS_TTGO))
2361 siointr1(com); /* fake interrupt to start output */
2369 comstop(struct tty *tp, int rw)
2374 com = com_addr(DEV_TO_UNIT(tp->t_dev));
2378 com->obufs[0].l_queued = FALSE;
2379 com->obufs[1].l_queued = FALSE;
2380 if (com->extra_state & CSE_ODONE) {
2381 com_events -= LOTS_OF_EVENTS;
2382 com->extra_state &= ~CSE_ODONE;
2383 if (com->etc != ETC_NONE) {
2384 if (com->etc == ETC_BREAK_ENDED)
2385 com->etc = ETC_NONE;
2389 com->tp->t_state &= ~TS_BUSY;
2390 if (com->state & CS_ODONE)
2391 com_events -= LOTS_OF_EVENTS;
2392 com->state &= ~(CS_ODONE | CS_BUSY);
2395 /* XXX no way to reset only input fifo. */
2396 com_events -= (com->iptr - com->ibuf);
2397 com->iptr = com->ibuf;
2402 if (rw & FWRITE && com->etc == ETC_NONE)
2403 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
2408 commctl(struct com_s *com, int bits, int how)
2414 if (com->channel_control & CD1400_CCR_RCVEN)
2416 mcr = com->mcr_image;
2417 if (mcr & com->mcr_dtr)
2419 if (mcr & com->mcr_rts)
2420 /* XXX wired on for Cyclom-8Ys */
2424 * We must read the modem status from the hardware because
2425 * we don't generate modem status change interrupts for all
2426 * changes, so com->prev_modem_status is not guaranteed to
2427 * be up to date. This is safe, unlike for sio, because
2428 * reading the status register doesn't clear pending modem
2429 * status change interrupts.
2431 msr = cd_getreg(com, CD1400_MSVR2);
2440 /* XXX not connected except for Cyclom-16Y? */
2445 if (bits & TIOCM_DTR)
2446 mcr |= com->mcr_dtr;
2447 if (bits & TIOCM_RTS)
2448 mcr |= com->mcr_rts;
2452 com->mcr_image = mcr;
2453 cd_setreg(com, CD1400_MSVR1, mcr);
2454 cd_setreg(com, CD1400_MSVR2, mcr);
2457 com->mcr_image = mcr = com->mcr_image | mcr;
2458 cd_setreg(com, CD1400_MSVR1, mcr);
2459 cd_setreg(com, CD1400_MSVR2, mcr);
2462 com->mcr_image = mcr = com->mcr_image & ~mcr;
2463 cd_setreg(com, CD1400_MSVR1, mcr);
2464 cd_setreg(com, CD1400_MSVR2, mcr);
2479 * Set our timeout period to 1 second if no polled devices are open.
2480 * Otherwise set it to max(1/200, 1/hz).
2481 * Enable timeouts iff some device is open.
2483 callout_stop(&sio_timeout_handle);
2486 for (unit = 0; unit < NSIO; ++unit) {
2487 com = com_addr(unit);
2488 if (com != NULL && com->tp != NULL
2489 && com->tp->t_state & TS_ISOPEN) {
2492 if (com->poll || com->poll_output) {
2493 sio_timeout = hz > 200 ? hz / 200 : 1;
2500 sio_timeouts_until_log = hz / sio_timeout;
2501 callout_reset(&sio_timeout_handle, sio_timeout,
2504 /* Flush error messages, if any. */
2505 sio_timeouts_until_log = 1;
2507 callout_stop(&sio_timeout_handle);
2512 comwakeup(void *chan)
2517 callout_reset(&sio_timeout_handle, sio_timeout, comwakeup, NULL);
2521 * Recover from lost output interrupts.
2522 * Poll any lines that don't use interrupts.
2524 for (unit = 0; unit < NSIO; ++unit) {
2525 com = com_addr(unit);
2527 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2536 * Check for and log errors, but not too often.
2538 if (--sio_timeouts_until_log > 0)
2540 sio_timeouts_until_log = hz / sio_timeout;
2541 for (unit = 0; unit < NSIO; ++unit) {
2544 com = com_addr(unit);
2547 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2552 delta = com->delta_error_counts[errnum];
2553 com->delta_error_counts[errnum] = 0;
2557 total = com->error_counts[errnum] += delta;
2558 log(LOG_ERR, "cy%d: %u more %s%s (total %lu)\n",
2559 unit, delta, error_desc[errnum],
2560 delta == 1 ? "" : "s", total);
2566 disc_optim(struct tty *tp, struct termios *t, struct com_s *com)
2568 #ifndef SOFT_HOTCHAR
2573 * XXX can skip a lot more cases if Smarts. Maybe
2574 * (IGNCR | ISTRIP | IXON) in c_iflag. But perhaps we
2575 * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2577 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2578 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2579 && (!(t->c_iflag & PARMRK)
2580 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2581 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2582 && linesw[tp->t_line].l_rint == ttyinput)
2583 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2585 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2586 com->hotchar = linesw[tp->t_line].l_hotchar;
2587 #ifndef SOFT_HOTCHAR
2588 opt = com->cor[2] & ~CD1400_COR3_SCD34;
2589 if (com->hotchar != 0) {
2590 cd_setreg(com, CD1400_SCHR3, com->hotchar);
2591 cd_setreg(com, CD1400_SCHR4, com->hotchar);
2592 opt |= CD1400_COR3_SCD34;
2594 if (opt != com->cor[2]) {
2595 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2596 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR3);
2602 /* standard line discipline input routine */
2604 cyinput(int c, struct tty *tp)
2606 /* XXX duplicate ttyinput(), but without the IXOFF/IXON/ISTRIP/IPARMRK
2607 * bits, as they are done by the CD1400. Hardly worth the effort,
2608 * given that high-throughput sessions are raw anyhow.
2614 comspeed(speed_t speed, u_long cy_clock, int *prescaler_io)
2624 if (speed < 0 || speed > 150000)
2627 /* determine which prescaler to use */
2628 for (prescaler_unit = 4, prescaler = 2048; prescaler_unit;
2629 prescaler_unit--, prescaler >>= 2) {
2630 if (cy_clock / prescaler / speed > 63)
2634 divider = (cy_clock / prescaler * 2 / speed + 1) / 2; /* round off */
2637 actual = cy_clock/prescaler/divider;
2639 /* 10 times error in percent: */
2640 error = ((actual - (long)speed) * 2000 / (long)speed + 1) / 2;
2642 /* 3.0% max error tolerance */
2643 if (error < -30 || error > 30)
2647 kprintf("prescaler = %d (%d)\n", prescaler, prescaler_unit);
2648 kprintf("divider = %d (%x)\n", divider, divider);
2649 kprintf("actual = %d\n", actual);
2650 kprintf("error = %d\n", error);
2653 *prescaler_io = prescaler_unit;
2658 cd1400_channel_cmd(struct com_s *com, int cmd)
2660 cd1400_channel_cmd_wait(com);
2661 cd_setreg(com, CD1400_CCR, cmd);
2662 cd1400_channel_cmd_wait(com);
2666 cd1400_channel_cmd_wait(struct com_s *com)
2668 struct timeval start;
2672 if (cd_getreg(com, CD1400_CCR) == 0)
2676 if (cd_getreg(com, CD1400_CCR) == 0)
2679 usec = 1000000 * (tv.tv_sec - start.tv_sec) +
2680 tv.tv_usec - start.tv_usec;
2683 "cy%d: channel command timeout (%ld usec)\n",
2691 cd_etc(struct com_s *com, int etc)
2694 * We can't change the hardware's ETC state while there are any
2695 * characters in the tx fifo, since those characters would be
2696 * interpreted as commands! Unputting characters from the fifo
2697 * is difficult, so we wait up to 12 character times for the fifo
2698 * to drain. The command will be delayed for up to 2 character
2699 * times for the tx to become empty. Unputting characters from
2700 * the tx holding and shift registers is impossible, so we wait
2701 * for the tx to become empty so that the command is sure to be
2702 * executed soon after we issue it.
2705 if (com->etc == etc) {
2709 if ((etc == CD1400_ETC_SENDBREAK
2710 && (com->etc == ETC_BREAK_STARTING
2711 || com->etc == ETC_BREAK_STARTED))
2712 || (etc == CD1400_ETC_STOPBREAK
2713 && (com->etc == ETC_BREAK_ENDING || com->etc == ETC_BREAK_ENDED
2714 || com->etc == ETC_NONE))) {
2719 cd_setreg(com, CD1400_SRER,
2721 = (com->intr_enable & ~CD1400_SRER_TXRDY) | CD1400_SRER_TXMPTY);
2724 while (com->etc == etc
2725 && tsleep(&com->etc, PCATCH, "cyetc", 0) == 0)
2730 cd_getreg(struct com_s *com, int reg)
2732 struct com_s *basecom;
2739 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2740 car = com->unit & CD1400_CAR_CHAN;
2741 cy_align = com->cy_align;
2742 iobase = com->iobase;
2746 if (basecom->car != car)
2747 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2748 val = cd_inb(iobase, reg, cy_align);
2755 cd_setreg(struct com_s *com, int reg, int val)
2757 struct com_s *basecom;
2763 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2764 car = com->unit & CD1400_CAR_CHAN;
2765 cy_align = com->cy_align;
2766 iobase = com->iobase;
2770 if (basecom->car != car)
2771 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2772 cd_outb(iobase, reg, cy_align, val);
2787 com = com_addr(unit);
2788 kprintf("info for channel %d\n", unit);
2789 kprintf("------------------\n");
2790 kprintf("total cyclom service probes:\t%d\n", cy_svrr_probes);
2791 kprintf("calls to upper layer:\t\t%d\n", cy_timeouts);
2794 iobase = com->iobase;
2796 kprintf("cd1400 base address:\\tt%p\n", iobase);
2797 kprintf("saved channel_control:\t\t0x%02x\n", com->channel_control);
2798 kprintf("saved cor1-3:\t\t\t0x%02x 0x%02x 0x%02x\n",
2799 com->cor[0], com->cor[1], com->cor[2]);
2800 kprintf("service request enable reg:\t0x%02x (0x%02x cached)\n",
2801 cd_getreg(com, CD1400_SRER), com->intr_enable);
2802 kprintf("service request register:\t0x%02x\n",
2803 cd_inb(iobase, CD1400_SVRR, com->cy_align));
2804 kprintf("modem status:\t\t\t0x%02x (0x%02x cached)\n",
2805 cd_getreg(com, CD1400_MSVR2), com->prev_modem_status);
2806 kprintf("rx/tx/mdm interrupt registers:\t0x%02x 0x%02x 0x%02x\n",
2807 cd_inb(iobase, CD1400_RIR, com->cy_align),
2808 cd_inb(iobase, CD1400_TIR, com->cy_align),
2809 cd_inb(iobase, CD1400_MIR, com->cy_align));
2811 kprintf("com state:\t\t\t0x%02x\n", com->state);
2812 kprintf("calls to comstart():\t\t%d (%d useful)\n",
2813 com->start_count, com->start_real);
2814 kprintf("rx buffer chars free:\t\t%d\n", com->iptr - com->ibuf);
2816 if (com->obufs[0].l_queued)
2817 ocount += com->obufs[0].l_tail - com->obufs[0].l_head;
2818 if (com->obufs[1].l_queued)
2819 ocount += com->obufs[1].l_tail - com->obufs[1].l_head;
2820 kprintf("tx buffer chars:\t\t%u\n", ocount);
2821 kprintf("received chars:\t\t\t%d\n", com->bytes_in);
2822 kprintf("received exceptions:\t\t%d\n", com->recv_exception);
2823 kprintf("modem signal deltas:\t\t%d\n", com->mdm);
2824 kprintf("transmitted chars:\t\t%d\n", com->bytes_out);
2828 kprintf("tty state:\t\t\t0x%08x\n", tp->t_state);
2830 "upper layer queue lengths:\t%d raw, %d canon, %d output\n",
2831 tp->t_rawq.c_cc, tp->t_canq.c_cc, tp->t_outq.c_cc);
2833 kprintf("tty state:\t\t\tclosed\n");
2835 #endif /* CyDebug */