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.27 2007/04/12 18:35:08 swildner 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/i386/isa_device.h>
91 #include <machine_base/isa/ic/cd1400.h>
94 #define disable_intr() com_lock()
95 #define enable_intr() com_unlock()
99 * Dictionary so that I can name everything *sio* or *com* to compare with
100 * sio.c. There is also lots of ugly formatting and unnecessary ifdefs to
101 * simplify the comparision. These will go away.
103 #define LSR_BI CD1400_RDSR_BREAK
104 #define LSR_FE CD1400_RDSR_FE
105 #define LSR_OE CD1400_RDSR_OE
106 #define LSR_PE CD1400_RDSR_PE
107 #define MCR_DTR CD1400_MSVR2_DTR
108 #define MCR_RTS CD1400_MSVR1_RTS
109 #define MSR_CTS CD1400_MSVR2_CTS
110 #define MSR_DCD CD1400_MSVR2_CD
111 #define MSR_DSR CD1400_MSVR2_DSR
112 #define MSR_RI CD1400_MSVR2_RI
113 #define NSIO (NCY * CY_MAX_PORTS)
114 #define comconsole cyconsole
115 #define comdefaultrate cydefaultrate
116 #define com_events cy_events
117 #define comhardclose cyhardclose
118 #define commctl cymctl
119 #define comparam cyparam
120 #define comspeed cyspeed
121 #define comstart cystart
122 #define comwakeup cywakeup
123 #define nsio_tty ncy_tty
124 #define p_com_addr p_cy_addr
125 #define sioattach cyattach
126 #define sioclose cyclose
127 #define siodriver cydriver
128 #define siodtrwakeup cydtrwakeup
129 #define sioinput cyinput
130 #define siointr cyintr
131 #define siointr1 cyintr1
132 #define sioioctl cyioctl
133 #define sioopen cyopen
134 #define siopoll cypoll
135 #define sioprobe cyprobe
136 #define siosettimeout cysettimeout
137 #define siosetwater cysetwater
138 #define comstop cystop
139 #define siowrite cywrite
140 #define sio_registered cy_registered
141 #define sio_timeout cy_timeout
142 #define sio_timeout_handle cy_timeout_handle
143 #define sio_timeouts_until_log cy_timeouts_until_log
144 #define sio_tty cy_tty
146 #define CY_MAX_PORTS (CD1400_NO_OF_CHANNELS * CY_MAX_CD1400s)
148 /* We encode the cyclom unit number (cyu) in spare bits in the IVR's. */
149 #define CD1400_xIVR_CHAN_SHIFT 3
150 #define CD1400_xIVR_CHAN 0x1F
153 * ETC states. com->etc may also contain a hardware ETC command value,
154 * meaning that execution of that command is pending.
156 #define ETC_NONE 0 /* we depend on bzero() setting this */
157 #define ETC_BREAK_STARTING 1
158 #define ETC_BREAK_STARTED 2
159 #define ETC_BREAK_ENDING 3
160 #define ETC_BREAK_ENDED 4
162 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */
164 #define CALLOUT_MASK 0x80
165 #define CONTROL_MASK 0x60
166 #define CONTROL_INIT_STATE 0x20
167 #define CONTROL_LOCK_STATE 0x40
168 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
169 #define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK)
171 * Not all of the magic is parametrized in the following macros. 16 and
172 * 0xff are related to the bitfields in a udev_t. CY_MAX_PORTS must be
173 * ((0xff & ~MINOR_MAGIC_MASK) + 1) for things to work.
175 #define MINOR_TO_UNIT(mynor) (((mynor) >> 16) * CY_MAX_PORTS \
176 | (((mynor) & 0xff) & ~MINOR_MAGIC_MASK))
177 #define UNIT_TO_MINOR(unit) (((unit) / CY_MAX_PORTS) << 16 \
178 | (((unit) & 0xff) & ~MINOR_MAGIC_MASK))
182 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
183 * than the other bits so that they can be tested as a group without masking
186 * The following com and tty flags correspond closely:
187 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and
189 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart())
190 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam())
191 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam())
192 * TS_FLUSH is not used.
193 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
194 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
196 #define CS_BUSY 0x80 /* output in progress */
197 #define CS_TTGO 0x40 /* output not stopped by XOFF */
198 #define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */
199 #define CS_CHECKMSR 1 /* check of MSR scheduled */
200 #define CS_CTS_OFLOW 2 /* use CTS output flow control */
201 #define CS_DTR_OFF 0x10 /* DTR held off */
202 #define CS_ODONE 4 /* output completed */
203 #define CS_RTS_IFLOW 8 /* use RTS input flow control */
204 #define CSE_ODONE 1 /* output transmitted */
206 static char const * const error_desc[] = {
209 #define CE_INTERRUPT_BUF_OVERFLOW 1
210 "interrupt-level buffer overflow",
211 #define CE_TTY_BUF_OVERFLOW 2
212 "tty-level buffer overflow",
216 #define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum])
218 /* types. XXX - should be elsewhere */
219 typedef u_char bool_t; /* boolean */
220 typedef u_char volatile *cy_addr;
222 /* queue of linear buffers */
224 u_char *l_head; /* next char to process */
225 u_char *l_tail; /* one past the last char to process */
226 struct lbq *l_next; /* next in queue */
227 bool_t l_queued; /* nonzero if queued */
230 /* com device structure */
232 u_char state; /* miscellaneous flag bits */
233 bool_t active_out; /* nonzero if the callout device is open */
235 u_char cfcr_image; /* copy of value written to CFCR */
237 u_char etc; /* pending Embedded Transmit Command */
238 u_char extra_state; /* more flag bits, separate for order trick */
240 u_char fifo_image; /* copy of value written to FIFO */
242 u_char gfrcr_image; /* copy of value read from GFRCR */
244 bool_t hasfifo; /* nonzero for 16550 UARTs */
245 bool_t loses_outints; /* nonzero if device loses output interrupts */
247 u_char mcr_dtr; /* MCR bit that is wired to DTR */
248 u_char mcr_image; /* copy of value written to MCR */
249 u_char mcr_rts; /* MCR bit that is wired to RTS */
252 bool_t multiport; /* is this unit part of a multiport device? */
253 #endif /* COM_MULTIPORT */
254 bool_t no_irq; /* nonzero if irq is not attached */
255 bool_t poll; /* nonzero if polling is required */
256 bool_t poll_output; /* nonzero if polling for output is required */
258 int unit; /* unit number */
259 int dtr_wait; /* time to hold DTR down on close (* 1/hz) */
260 struct callout dtr_ch;
264 u_int wopeners; /* # processes waiting for DCD in open() */
267 * The high level of the driver never reads status registers directly
268 * because there would be too many side effects to handle conveniently.
269 * Instead, it reads copies of the registers stored here by the
272 u_char last_modem_status; /* last MSR read by intr handler */
273 u_char prev_modem_status; /* last MSR handled by high level */
275 u_char hotchar; /* ldisc-specific char to be handled ASAP */
276 u_char *ibuf; /* start of input buffer */
277 u_char *ibufend; /* end of input buffer */
278 u_char *ibufold; /* old input buffer, to be freed */
279 u_char *ihighwater; /* threshold in input buffer */
280 u_char *iptr; /* next free spot in input buffer */
281 int ibufsize; /* size of ibuf (not include error bytes) */
282 int ierroff; /* offset of error bytes in ibuf */
284 struct lbq obufq; /* head of queue of output buffers */
285 struct lbq obufs[2]; /* output buffers */
287 int cy_align; /* index for register alignment */
288 cy_addr cy_iobase; /* base address of this port's cyclom */
289 cy_addr iobase; /* base address of this port's cd1400 */
290 int mcr_rts_reg; /* cd1400 reg number of reg holding mcr_rts */
292 struct tty *tp; /* cross reference */
295 struct termios it_in; /* should be in struct tty */
296 struct termios it_out;
299 struct termios lt_in; /* should be in struct tty */
300 struct termios lt_out;
303 bool_t do_dcd_timestamp;
304 struct timeval timestamp;
305 struct timeval dcd_timestamp;
307 u_long bytes_in; /* statistics */
309 u_int delta_error_counts[CE_NTYPES];
310 u_long error_counts[CE_NTYPES];
312 u_int recv_exception; /* exception chars received */
313 u_int mdm; /* modem signal changes */
315 u_int start_count; /* no. of calls to comstart() */
316 u_int start_real; /* no. of calls that did something */
318 u_char car; /* CD1400 CAR shadow (if first unit in cd) */
319 u_char channel_control;/* CD1400 CCR control command shadow */
320 u_char cor[3]; /* CD1400 COR1-3 shadows */
321 u_char intr_enable; /* CD1400 SRER shadow */
324 * Data area for output buffers. Someday we should build the output
325 * buffer queue without copying data.
331 /* PCI driver entry point. */
332 int cyattach_common (cy_addr cy_iobase, int cy_align);
333 void siointr(void *);
335 static int cy_units (cy_addr cy_iobase, int cy_align);
336 static int sioattach (struct isa_device *dev);
337 static void cd1400_channel_cmd (struct com_s *com, int cmd);
338 static void cd1400_channel_cmd_wait (struct com_s *com);
339 static void cd_etc (struct com_s *com, int etc);
340 static int cd_getreg (struct com_s *com, int reg);
341 static void cd_setreg (struct com_s *com, int reg, int val);
342 static timeout_t siodtrwakeup;
343 static void comhardclose (struct com_s *com);
344 static void sioinput (struct com_s *com);
346 static void siointr1 (struct com_s *com);
348 static int commctl (struct com_s *com, int bits, int how);
349 static int comparam (struct tty *tp, struct termios *t);
350 static inthand2_t siopoll;
351 static int sioprobe (struct isa_device *dev);
352 static void siosettimeout (void);
353 static int siosetwater (struct com_s *com, speed_t speed);
354 static int comspeed (speed_t speed, u_long cy_clock,
356 static void comstart (struct tty *tp);
357 static void comstop (struct tty *tp, int rw);
358 static timeout_t comwakeup;
359 static void disc_optim (struct tty *tp, struct termios *t,
363 void cystatus (int unit);
366 static char driver_name[] = "cy";
368 /* table and macro for fast conversion from a unit number to its com struct */
369 static struct com_s *p_com_addr[NSIO];
370 #define com_addr(unit) (p_com_addr[unit])
372 struct isa_driver siodriver = {
373 sioprobe, sioattach, driver_name
376 static d_open_t sioopen;
377 static d_close_t sioclose;
378 static d_write_t siowrite;
379 static d_ioctl_t sioioctl;
381 #define CDEV_MAJOR 48
382 static struct dev_ops sio_ops = {
383 { driver_name, CDEV_MAJOR, D_TTY | D_KQFILTER },
390 .d_kqfilter = ttykqfilter
393 static int comconsole = -1;
394 static speed_t comdefaultrate = TTYDEF_SPEED;
395 static u_int com_events; /* input chars + weighted output completions */
396 static bool_t sio_registered;
397 static int sio_timeout;
398 static int sio_timeouts_until_log;
399 static struct callout sio_timeout_handle;
401 static struct tty *sio_tty[NSIO];
403 static struct tty sio_tty[NSIO];
405 static const int nsio_tty = NSIO;
408 static u_int cd_inbs;
409 static u_int cy_inbs;
410 static u_int cd_outbs;
411 static u_int cy_outbs;
412 static u_int cy_svrr_probes;
413 static u_int cy_timeouts;
416 static int cy_chip_offset[] = {
417 0x0000, 0x0400, 0x0800, 0x0c00, 0x0200, 0x0600, 0x0a00, 0x0e00,
419 static int cy_nr_cd1400s[NCY];
420 static int cy_total_devices;
421 #undef RxFifoThreshold
422 static int volatile RxFifoThreshold = (CD1400_RX_FIFO_SIZE / 2);
425 sioprobe(struct isa_device *dev)
429 iobase = (cy_addr)dev->id_maddr;
431 /* Cyclom-16Y hardware reset (Cyclom-8Ys don't care) */
432 cy_inb(iobase, CY16_RESET, 0); /* XXX? */
433 DELAY(500); /* wait for the board to get its act together */
435 /* this is needed to get the board out of reset */
436 cy_outb(iobase, CY_CLEAR_INTR, 0, 0);
439 return (cy_units(iobase, 0) == 0 ? 0 : -1);
443 cy_units(cy_addr cy_iobase, int cy_align)
446 u_char firmware_version = 0; /* assign to avoid warning */
450 for (cyu = 0; cyu < CY_MAX_CD1400s; ++cyu) {
451 iobase = cy_iobase + (cy_chip_offset[cyu] << cy_align);
453 /* wait for chip to become ready for new command */
454 for (i = 0; i < 10; i++) {
456 if (!cd_inb(iobase, CD1400_CCR, cy_align))
460 /* clear the GFRCR register */
461 cd_outb(iobase, CD1400_GFRCR, cy_align, 0);
463 /* issue a reset command */
464 cd_outb(iobase, CD1400_CCR, cy_align,
465 CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
467 /* wait for the CD1400 to initialize itself */
468 for (i = 0; i < 200; i++) {
471 /* retrieve firmware version */
472 firmware_version = cd_inb(iobase, CD1400_GFRCR,
474 if ((firmware_version & 0xf0) == 0x40)
479 * Anything in the 0x40-0x4F range is fine.
480 * If one CD1400 is bad then we don't support higher
481 * numbered good ones on this board.
483 if ((firmware_version & 0xf0) != 0x40)
490 sioattach(struct isa_device *isdp)
494 adapter = cyattach_common((cy_addr) isdp->id_maddr, 0);
500 * This kludge is to allow ISA/PCI device specifications in the
501 * kernel config file to be in any order.
503 if (isdp->id_unit != adapter) {
504 kprintf("cy%d: attached as cy%d\n", isdp->id_unit, adapter);
505 isdp->id_unit = adapter; /* XXX */
507 isdp->id_intr = (inthand2_t *)siointr;
508 /* isdp->id_ri_flags |= RI_FAST; XXX unimplemented - use newbus! */
513 cyattach_common(cy_addr cy_iobase, int cy_align)
517 u_char firmware_version;
523 adapter = cy_total_devices;
524 if ((u_int)adapter >= NCY) {
526 "cy%d: can't attach adapter: insufficient cy devices configured\n",
530 ncyu = cy_units(cy_iobase, cy_align);
533 cy_nr_cd1400s[adapter] = ncyu;
536 unit = adapter * CY_MAX_PORTS;
537 for (cyu = 0; cyu < ncyu; ++cyu) {
540 iobase = (cy_addr) (cy_iobase
541 + (cy_chip_offset[cyu] << cy_align));
542 firmware_version = cd_inb(iobase, CD1400_GFRCR, cy_align);
544 /* Set up a receive timeout period of than 1+ ms. */
545 cd_outb(iobase, CD1400_PPR, cy_align,
546 howmany(CY_CLOCK(firmware_version)
547 / CD1400_PPR_PRESCALER, 1000));
549 for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; ++cdu, ++unit) {
552 com = kmalloc(sizeof *com, M_DEVBUF, M_WAITOK | M_ZERO);
554 callout_init(&com->dtr_ch);
555 com->gfrcr_image = firmware_version;
556 if (CY_RTS_DTR_SWAPPED(firmware_version)) {
557 com->mcr_dtr = MCR_RTS;
558 com->mcr_rts = MCR_DTR;
559 com->mcr_rts_reg = CD1400_MSVR2;
561 com->mcr_dtr = MCR_DTR;
562 com->mcr_rts = MCR_RTS;
563 com->mcr_rts_reg = CD1400_MSVR1;
565 com->dtr_wait = 3 * hz;
566 com->obufs[0].l_head = com->obuf1;
567 com->obufs[1].l_head = com->obuf2;
569 com->cy_align = cy_align;
570 com->cy_iobase = cy_iobase;
571 com->iobase = iobase;
572 com->car = ~CD1400_CAR_CHAN;
575 * We don't use all the flags from <sys/ttydefaults.h> since they
576 * are only relevant for logins. It's important to have echo off
577 * initially so that the line doesn't start blathering before the
578 * echo flag can be turned off.
580 com->it_in.c_iflag = 0;
581 com->it_in.c_oflag = 0;
582 com->it_in.c_cflag = TTYDEF_CFLAG;
583 com->it_in.c_lflag = 0;
584 if (unit == comconsole) {
585 com->it_in.c_iflag = TTYDEF_IFLAG;
586 com->it_in.c_oflag = TTYDEF_OFLAG;
587 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
588 com->it_in.c_lflag = TTYDEF_LFLAG;
589 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
591 if (siosetwater(com, com->it_in.c_ispeed) != 0) {
593 kfree(com, M_DEVBUF);
597 termioschars(&com->it_in);
598 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
599 com->it_out = com->it_in;
602 com_addr(unit) = com;
605 if (!sio_registered) {
606 callout_init(&sio_timeout_handle);
607 register_swi(SWI_TTY, siopoll, NULL, "cy", NULL);
608 sio_registered = TRUE;
610 minorbase = UNIT_TO_MINOR(unit);
611 dev_ops_add(&sio_ops, UNIT_TO_MINOR(-1), minorbase);
612 make_dev(&sio_ops, minorbase,
613 UID_ROOT, GID_WHEEL, 0600, "ttyc%r%r", adapter,
614 unit % CY_MAX_PORTS);
615 make_dev(&sio_ops, minorbase | CONTROL_INIT_STATE,
616 UID_ROOT, GID_WHEEL, 0600, "ttyic%r%r", adapter,
617 unit % CY_MAX_PORTS);
618 make_dev(&sio_ops, minorbase | CONTROL_LOCK_STATE,
619 UID_ROOT, GID_WHEEL, 0600, "ttylc%r%r", adapter,
620 unit % CY_MAX_PORTS);
621 make_dev(&sio_ops, minorbase | CALLOUT_MASK,
622 UID_UUCP, GID_DIALER, 0660, "cuac%r%r", adapter,
623 unit % CY_MAX_PORTS);
624 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_INIT_STATE,
625 UID_UUCP, GID_DIALER, 0660, "cuaic%r%r", adapter,
626 unit % CY_MAX_PORTS);
627 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE,
628 UID_UUCP, GID_DIALER, 0660, "cualc%r%r", adapter,
629 unit % CY_MAX_PORTS);
631 /* for(cdu...), for(cyu...) terminating blocks */
635 /* ensure an edge for the next interrupt */
636 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
642 sioopen(struct dev_open_args *ap)
644 cdev_t dev = ap->a_head.a_dev;
652 unit = MINOR_TO_UNIT(mynor);
653 if ((u_int) unit >= NSIO || (com = com_addr(unit)) == NULL)
655 if (mynor & CONTROL_MASK)
658 tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
660 tp = com->tp = &sio_tty[unit];
665 * We jump to this label after all non-interrupted sleeps to pick
666 * up any changes of the device state.
669 while (com->state & CS_DTR_OFF) {
670 error = tsleep(&com->dtr_wait, PCATCH, "cydtr", 0);
674 if (tp->t_state & TS_ISOPEN) {
676 * The device is open, so everything has been initialized.
679 if (mynor & CALLOUT_MASK) {
680 if (!com->active_out) {
685 if (com->active_out) {
686 if (ap->a_oflags & O_NONBLOCK) {
690 error = tsleep(&com->active_out,
697 if (tp->t_state & TS_XCLUDE &&
698 suser_cred(ap->a_cred, 0)) {
704 * The device isn't open, so there are no conflicts.
705 * Initialize it. Initialization is done twice in many
706 * cases: to preempt sleeping callin opens if we are
707 * callout, and to complete a callin open after DCD rises.
709 tp->t_oproc = comstart;
710 tp->t_stop = comstop;
711 tp->t_param = comparam;
713 tp->t_termios = mynor & CALLOUT_MASK
714 ? com->it_out : com->it_in;
716 /* Encode per-board unit in LIVR for access in intr routines. */
717 cd_setreg(com, CD1400_LIVR,
718 (unit & CD1400_xIVR_CHAN) << CD1400_xIVR_CHAN_SHIFT);
720 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
722 com->poll = com->no_irq;
723 com->poll_output = com->loses_outints;
726 error = comparam(tp, &tp->t_termios);
733 * (Re)enable and flush fifos.
735 * Certain SMC chips cause problems if the fifos
736 * are enabled while input is ready. Turn off the
737 * fifo if necessary to clear the input. We test
738 * the input ready bit after enabling the fifos
739 * since we've already enabled them in comparam()
740 * and to handle races between enabling and fresh
744 outb(iobase + com_fifo,
745 FIFO_RCV_RST | FIFO_XMT_RST
748 if (!(inb(com->line_status_port) & LSR_RXRDY))
750 outb(iobase + com_fifo, 0);
752 (void) inb(com->data_port);
757 (void) inb(com->line_status_port);
758 (void) inb(com->data_port);
759 com->prev_modem_status = com->last_modem_status
760 = inb(com->modem_status_port);
761 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS
766 * Flush fifos. This requires a full channel reset which
767 * also disables the transmitter and receiver. Recover
770 cd1400_channel_cmd(com,
771 CD1400_CCR_CMDRESET | CD1400_CCR_CHANRESET);
772 cd1400_channel_cmd(com, com->channel_control);
775 com->prev_modem_status = com->last_modem_status
776 = cd_getreg(com, CD1400_MSVR2);
777 cd_setreg(com, CD1400_SRER,
779 = CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
783 * Handle initial DCD. Callout devices get a fake initial
784 * DCD (trapdoor DCD). If we are callout, then any sleeping
785 * callin opens get woken up and resume sleeping on "cybi"
786 * instead of "cydcd".
789 * XXX `mynor & CALLOUT_MASK' should be
790 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
791 * TRAPDOOR_CARRIER is the default initial state for callout
792 * devices and SOFT_CARRIER is like CLOCAL except it hides
795 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
796 (*linesw[tp->t_line].l_modem)(tp, 1);
799 * Wait for DCD if necessary.
801 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
802 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) {
804 error = tsleep(TSA_CARR_ON(tp), PCATCH, "cydcd", 0);
810 error = (*linesw[tp->t_line].l_open)(dev, tp);
811 disc_optim(tp, &tp->t_termios, com);
812 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
813 com->active_out = TRUE;
817 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
823 sioclose(struct dev_close_args *ap)
825 cdev_t dev = ap->a_head.a_dev;
831 if (mynor & CONTROL_MASK)
833 com = com_addr(MINOR_TO_UNIT(mynor));
836 cd_etc(com, CD1400_ETC_STOPBREAK);
837 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
838 disc_optim(tp, &tp->t_termios, com);
839 comstop(tp, FREAD | FWRITE);
844 #ifdef broken /* session holds a ref to the tty; can't deallocate */
846 com->tp = sio_tty[unit] = NULL;
852 comhardclose(struct com_s *com)
859 iobase = com->iobase;
863 com->poll_output = FALSE;
865 com->do_timestamp = 0;
867 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
872 cd_setreg(com, CD1400_COR2, com->cor[1] &= ~CD1400_COR2_ETC);
874 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
879 outb(iobase + com_ier, 0);
882 cd_setreg(com, CD1400_SRER, com->intr_enable = 0);
886 if ((tp->t_cflag & HUPCL)
888 * XXX we will miss any carrier drop between here and the
889 * next open. Perhaps we should watch DCD even when the
890 * port is closed; it is not sufficient to check it at
891 * the next open because it might go up and down while
892 * we're not watching.
895 && !(com->prev_modem_status & MSR_DCD)
896 && !(com->it_in.c_cflag & CLOCAL))
897 || !(tp->t_state & TS_ISOPEN)) {
898 (void)commctl(com, TIOCM_DTR, DMBIC);
900 /* Disable receiver (leave transmitter enabled). */
901 com->channel_control = CD1400_CCR_CMDCHANCTL
904 cd1400_channel_cmd(com, com->channel_control);
906 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
907 callout_reset(&com->dtr_ch, com->dtr_wait,
909 com->state |= CS_DTR_OFF;
916 * Disable fifos so that they are off after controlled
917 * reboots. Some BIOSes fail to detect 16550s when the
920 outb(iobase + com_fifo, 0);
923 com->active_out = FALSE;
924 wakeup(&com->active_out);
925 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */
930 siowrite(struct dev_write_args *ap)
932 cdev_t dev = ap->a_head.a_dev;
933 struct uio *uio = ap->a_uio;
939 if (mynor & CONTROL_MASK)
942 unit = MINOR_TO_UNIT(mynor);
943 tp = com_addr(unit)->tp;
945 * (XXX) We disallow virtual consoles if the physical console is
946 * a serial port. This is in case there is a display attached that
947 * is not the console. In that situation we don't need/want the X
948 * server taking over the console.
950 if (constty != NULL && unit == comconsole)
953 /* XXX duplicate ttwrite(), but without so much output processing on
954 * CR & LF chars. Hardly worth the effort, given that high-throughput
955 * sessions are raw anyhow.
958 return ((*linesw[tp->t_line].l_write)(tp, uio, ap->a_ioflag));
963 siodtrwakeup(void *chan)
967 com = (struct com_s *)chan;
968 com->state &= ~CS_DTR_OFF;
969 wakeup(&com->dtr_wait);
973 sioinput(struct com_s *com)
983 if (!(tp->t_state & TS_ISOPEN)) {
984 com_events -= (com->iptr - com->ibuf);
985 com->iptr = com->ibuf;
988 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
990 * Avoid the grotesquely inefficient lineswitch routine
991 * (ttyinput) in "raw" mode. It usually takes about 450
992 * instructions (that's without canonical processing or echo!).
993 * slinput is reasonably fast (usually 40 instructions plus
998 incc = com->iptr - buf;
999 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
1000 && (com->state & CS_RTS_IFLOW
1001 || tp->t_iflag & IXOFF)
1002 && !(tp->t_state & TS_TBLOCK))
1004 com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1005 += b_to_q((char *)buf, incc, &tp->t_rawq);
1009 tp->t_rawcc += incc;
1011 if (tp->t_state & TS_TTSTOP
1012 && (tp->t_iflag & IXANY
1013 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1014 tp->t_state &= ~TS_TTSTOP;
1015 tp->t_lflag &= ~FLUSHO;
1019 } while (buf < com->iptr);
1023 line_status = buf[com->ierroff];
1026 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
1027 if (line_status & LSR_BI)
1028 recv_data |= TTY_BI;
1029 if (line_status & LSR_FE)
1030 recv_data |= TTY_FE;
1031 if (line_status & LSR_OE)
1032 recv_data |= TTY_OE;
1033 if (line_status & LSR_PE)
1034 recv_data |= TTY_PE;
1036 (*linesw[tp->t_line].l_rint)(recv_data, tp);
1038 } while (buf < com->iptr);
1040 com_events -= (com->iptr - com->ibuf);
1041 com->iptr = com->ibuf;
1044 * There is now room for another low-level buffer full of input,
1045 * so enable RTS if it is now disabled and there is room in the
1046 * high-level buffer.
1048 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & com->mcr_rts) &&
1049 !(tp->t_state & TS_TBLOCK))
1051 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1053 cd_setreg(com, com->mcr_rts_reg,
1054 com->mcr_image |= com->mcr_rts);
1061 int unit = (int)arg;
1069 com_lock(); /* XXX could this be placed down lower in the loop? */
1071 baseu = unit * CY_MAX_PORTS;
1072 cy_align = com_addr(baseu)->cy_align;
1073 cy_iobase = com_addr(baseu)->cy_iobase;
1075 /* check each CD1400 in turn */
1076 for (cyu = 0; cyu < cy_nr_cd1400s[unit]; ++cyu) {
1077 iobase = (cy_addr) (cy_iobase
1078 + (cy_chip_offset[cyu] << cy_align));
1079 /* poll to see if it has any work */
1080 status = cd_inb(iobase, CD1400_SVRR, cy_align);
1086 /* service requests as appropriate, giving priority to RX */
1087 if (status & CD1400_SVRR_RXRDY) {
1099 save_rir = cd_inb(iobase, CD1400_RIR, cy_align);
1101 /* enter rx service */
1102 cd_outb(iobase, CD1400_CAR, cy_align, save_rir);
1103 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1104 = save_rir & CD1400_CAR_CHAN;
1106 serv_type = cd_inb(iobase, CD1400_RIVR, cy_align);
1107 com = com_addr(baseu
1108 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1109 & CD1400_xIVR_CHAN));
1111 /* ack receive service */
1112 serv_type = cy_inb(iobase, CY8_SVCACKR, cy_align);
1114 com = com_addr(baseu +
1115 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1116 & CD1400_xIVR_CHAN));
1119 if (serv_type & CD1400_RIVR_EXCEPTION) {
1120 ++com->recv_exception;
1121 line_status = cd_inb(iobase, CD1400_RDSR, cy_align);
1122 /* break/unnattached error bits or real input? */
1123 recv_data = cd_inb(iobase, CD1400_RDSR, cy_align);
1124 #ifndef SOFT_HOTCHAR
1125 if (line_status & CD1400_RDSR_SPECIAL
1126 && com->hotchar != 0)
1129 #if 1 /* XXX "intelligent" PFO error handling would break O error handling */
1130 if (line_status & (LSR_PE|LSR_FE|LSR_BI)) {
1132 Don't store PE if IGNPAR and BI if IGNBRK,
1133 this hack allows "raw" tty optimization
1134 works even if IGN* is set.
1136 if ( com->tp == NULL
1137 || !(com->tp->t_state & TS_ISOPEN)
1138 || ((line_status & (LSR_PE|LSR_FE))
1139 && (com->tp->t_iflag & IGNPAR))
1140 || ((line_status & LSR_BI)
1141 && (com->tp->t_iflag & IGNBRK)))
1143 if ( (line_status & (LSR_PE|LSR_FE))
1144 && (com->tp->t_state & TS_CAN_BYPASS_L_RINT)
1145 && ((line_status & LSR_FE)
1146 || ((line_status & LSR_PE)
1147 && (com->tp->t_iflag & INPCK))))
1153 if (com->hotchar != 0 && recv_data == com->hotchar)
1157 if (ioptr >= com->ibufend)
1158 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1160 if (com->do_timestamp)
1161 microtime(&com->timestamp);
1163 ioptr[0] = recv_data;
1164 ioptr[com->ierroff] = line_status;
1165 com->iptr = ++ioptr;
1166 if (ioptr == com->ihighwater
1167 && com->state & CS_RTS_IFLOW)
1169 outb(com->modem_ctl_port,
1170 com->mcr_image &= ~MCR_RTS);
1172 cd_outb(iobase, com->mcr_rts_reg,
1177 if (line_status & LSR_OE)
1178 CE_RECORD(com, CE_OVERRUN);
1184 count = cd_inb(iobase, CD1400_RDCR, cy_align);
1187 com->bytes_in += count;
1189 ifree = com->ibufend - ioptr;
1190 if (count > ifree) {
1192 com_events += ifree;
1194 if (com->do_timestamp)
1195 microtime(&com->timestamp);
1197 recv_data = cd_inb(iobase,
1201 if (com->hotchar != 0
1206 ioptr[0] = recv_data;
1207 ioptr[com->ierroff] = 0;
1209 } while (--ifree != 0);
1211 com->delta_error_counts
1212 [CE_INTERRUPT_BUF_OVERFLOW] += count;
1214 recv_data = cd_inb(iobase, CD1400_RDSR,
1217 if (com->hotchar != 0
1218 && recv_data == com->hotchar)
1221 } while (--count != 0);
1223 if (com->do_timestamp)
1224 microtime(&com->timestamp);
1225 if (ioptr <= com->ihighwater
1226 && ioptr + count > com->ihighwater
1227 && com->state & CS_RTS_IFLOW)
1229 outb(com->modem_ctl_port,
1230 com->mcr_image &= ~MCR_RTS);
1232 cd_outb(iobase, com->mcr_rts_reg,
1237 com_events += count;
1239 recv_data = cd_inb(iobase, CD1400_RDSR,
1242 if (com->hotchar != 0
1243 && recv_data == com->hotchar)
1246 ioptr[0] = recv_data;
1247 ioptr[com->ierroff] = 0;
1249 } while (--count != 0);
1255 /* terminate service context */
1257 cd_outb(iobase, CD1400_RIR, cy_align,
1259 & ~(CD1400_RIR_RDIREQ | CD1400_RIR_RBUSY));
1261 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1264 if (status & CD1400_SVRR_MDMCH) {
1266 u_char modem_status;
1274 save_mir = cd_inb(iobase, CD1400_MIR, cy_align);
1276 /* enter modem service */
1277 cd_outb(iobase, CD1400_CAR, cy_align, save_mir);
1278 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1279 = save_mir & CD1400_CAR_CHAN;
1281 com = com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS
1282 + (save_mir & CD1400_MIR_CHAN));
1284 /* ack modem service */
1285 vector = cy_inb(iobase, CY8_SVCACKM, cy_align);
1287 com = com_addr(baseu
1288 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1289 & CD1400_xIVR_CHAN));
1292 modem_status = cd_inb(iobase, CD1400_MSVR2, cy_align);
1293 if (modem_status != com->last_modem_status) {
1294 if (com->do_dcd_timestamp
1295 && !(com->last_modem_status & MSR_DCD)
1296 && modem_status & MSR_DCD)
1297 microtime(&com->dcd_timestamp);
1300 * Schedule high level to handle DCD changes. Note
1301 * that we don't use the delta bits anywhere. Some
1302 * UARTs mess them up, and it's easy to remember the
1303 * previous bits and calculate the delta.
1305 com->last_modem_status = modem_status;
1306 if (!(com->state & CS_CHECKMSR)) {
1307 com_events += LOTS_OF_EVENTS;
1308 com->state |= CS_CHECKMSR;
1312 #ifdef SOFT_CTS_OFLOW
1313 /* handle CTS change immediately for crisp flow ctl */
1314 if (com->state & CS_CTS_OFLOW) {
1315 if (modem_status & MSR_CTS) {
1316 com->state |= CS_ODEVREADY;
1317 if (com->state >= (CS_BUSY | CS_TTGO
1319 && !(com->intr_enable
1320 & CD1400_SRER_TXRDY))
1321 cd_outb(iobase, CD1400_SRER,
1325 & ~CD1400_SRER_TXMPTY
1326 | CD1400_SRER_TXRDY);
1328 com->state &= ~CS_ODEVREADY;
1329 if (com->intr_enable
1330 & CD1400_SRER_TXRDY)
1331 cd_outb(iobase, CD1400_SRER,
1335 & ~CD1400_SRER_TXRDY
1336 | CD1400_SRER_TXMPTY);
1342 /* terminate service context */
1344 cd_outb(iobase, CD1400_MIR, cy_align,
1346 & ~(CD1400_MIR_RDIREQ | CD1400_MIR_RBUSY));
1348 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1351 if (status & CD1400_SVRR_TXRDY) {
1360 save_tir = cd_inb(iobase, CD1400_TIR, cy_align);
1362 /* enter tx service */
1363 cd_outb(iobase, CD1400_CAR, cy_align, save_tir);
1364 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1365 = save_tir & CD1400_CAR_CHAN;
1367 com = com_addr(baseu
1368 + cyu * CD1400_NO_OF_CHANNELS
1369 + (save_tir & CD1400_TIR_CHAN));
1371 /* ack transmit service */
1372 vector = cy_inb(iobase, CY8_SVCACKT, cy_align);
1374 com = com_addr(baseu
1375 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1376 & CD1400_xIVR_CHAN));
1379 if (com->etc != ETC_NONE) {
1380 if (com->intr_enable & CD1400_SRER_TXRDY) {
1382 * Here due to sloppy SRER_TXRDY
1383 * enabling. Ignore. Come back when
1386 cd_outb(iobase, CD1400_SRER, cy_align,
1389 & ~CD1400_SRER_TXRDY)
1390 | CD1400_SRER_TXMPTY);
1391 goto terminate_tx_service;
1394 case CD1400_ETC_SENDBREAK:
1395 case CD1400_ETC_STOPBREAK:
1397 * Start the command. Come back on
1398 * next tx empty interrupt, hopefully
1399 * after command has been executed.
1401 cd_outb(iobase, CD1400_COR2, cy_align,
1402 com->cor[1] |= CD1400_COR2_ETC);
1403 cd_outb(iobase, CD1400_TDR, cy_align,
1405 cd_outb(iobase, CD1400_TDR, cy_align,
1407 if (com->etc == CD1400_ETC_SENDBREAK)
1408 com->etc = ETC_BREAK_STARTING;
1410 com->etc = ETC_BREAK_ENDING;
1411 goto terminate_tx_service;
1412 case ETC_BREAK_STARTING:
1414 * BREAK is now on. Continue with
1415 * SRER_TXMPTY processing, hopefully
1418 com->etc = ETC_BREAK_STARTED;
1420 case ETC_BREAK_STARTED:
1422 * Came back due to sloppy SRER_TXMPTY
1423 * enabling. Hope again.
1426 case ETC_BREAK_ENDING:
1428 * BREAK is now off. Continue with
1429 * SRER_TXMPTY processing and don't
1430 * come back. The SWI handler will
1431 * restart tx interrupts if necessary.
1433 cd_outb(iobase, CD1400_COR2, cy_align,
1435 &= ~CD1400_COR2_ETC);
1436 com->etc = ETC_BREAK_ENDED;
1437 if (!(com->state & CS_ODONE)) {
1438 com_events += LOTS_OF_EVENTS;
1439 com->state |= CS_ODONE;
1443 case ETC_BREAK_ENDED:
1445 * Shouldn't get here. Hope again.
1450 if (com->intr_enable & CD1400_SRER_TXMPTY) {
1451 if (!(com->extra_state & CSE_ODONE)) {
1452 com_events += LOTS_OF_EVENTS;
1453 com->extra_state |= CSE_ODONE;
1456 cd_outb(iobase, CD1400_SRER, cy_align,
1458 &= ~CD1400_SRER_TXMPTY);
1459 goto terminate_tx_service;
1461 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1465 ioptr = com->obufq.l_head;
1466 ocount = com->obufq.l_tail - ioptr;
1467 if (ocount > CD1400_TX_FIFO_SIZE)
1468 ocount = CD1400_TX_FIFO_SIZE;
1469 com->bytes_out += ocount;
1471 cd_outb(iobase, CD1400_TDR, cy_align,
1473 while (--ocount != 0);
1474 com->obufq.l_head = ioptr;
1475 if (ioptr >= com->obufq.l_tail) {
1478 qp = com->obufq.l_next;
1479 qp->l_queued = FALSE;
1482 com->obufq.l_head = qp->l_head;
1483 com->obufq.l_tail = qp->l_tail;
1484 com->obufq.l_next = qp;
1486 /* output just completed */
1487 com->state &= ~CS_BUSY;
1490 * The setting of CSE_ODONE may be
1491 * stale here. We currently only
1492 * use it when CS_BUSY is set, and
1493 * fixing it when we clear CS_BUSY
1496 if (com->extra_state & CSE_ODONE) {
1497 com_events -= LOTS_OF_EVENTS;
1498 com->extra_state &= ~CSE_ODONE;
1501 cd_outb(iobase, CD1400_SRER, cy_align,
1504 & ~CD1400_SRER_TXRDY)
1505 | CD1400_SRER_TXMPTY);
1507 if (!(com->state & CS_ODONE)) {
1508 com_events += LOTS_OF_EVENTS;
1509 com->state |= CS_ODONE;
1511 /* handle at high level ASAP */
1517 /* terminate service context */
1518 terminate_tx_service:
1520 cd_outb(iobase, CD1400_TIR, cy_align,
1522 & ~(CD1400_TIR_RDIREQ | CD1400_TIR_RBUSY));
1524 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1529 /* ensure an edge for the next interrupt */
1530 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
1539 siointr1(struct com_s *com)
1545 sioioctl(struct dev_ioctl_args *ap)
1547 cdev_t dev = ap->a_head.a_dev;
1548 caddr_t data = ap->a_data;
1549 u_long cmd = ap->a_cmd;
1554 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1556 struct termios term;
1560 com = com_addr(MINOR_TO_UNIT(mynor));
1561 if (mynor & CONTROL_MASK) {
1564 switch (mynor & CONTROL_MASK) {
1565 case CONTROL_INIT_STATE:
1566 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
1568 case CONTROL_LOCK_STATE:
1569 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
1572 return (ENODEV); /* /dev/nodev */
1576 error = suser_cred(ap->a_cred, 0);
1579 *ct = *(struct termios *)data;
1582 *(struct termios *)data = *ct;
1585 *(int *)data = TTYDISC;
1588 bzero(data, sizeof(struct winsize));
1595 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1596 term = tp->t_termios;
1598 error = ttsetcompat(tp, &cmd, data, &term);
1602 data = (caddr_t)&term;
1604 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1606 struct termios *dt = (struct termios *)data;
1607 struct termios *lt = mynor & CALLOUT_MASK
1608 ? &com->lt_out : &com->lt_in;
1610 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
1611 | (dt->c_iflag & ~lt->c_iflag);
1612 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
1613 | (dt->c_oflag & ~lt->c_oflag);
1614 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
1615 | (dt->c_cflag & ~lt->c_cflag);
1616 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
1617 | (dt->c_lflag & ~lt->c_lflag);
1618 for (cc = 0; cc < NCCS; ++cc)
1619 if (lt->c_cc[cc] != 0)
1620 dt->c_cc[cc] = tp->t_cc[cc];
1621 if (lt->c_ispeed != 0)
1622 dt->c_ispeed = tp->t_ispeed;
1623 if (lt->c_ospeed != 0)
1624 dt->c_ospeed = tp->t_ospeed;
1626 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data,
1627 ap->a_fflag, ap->a_cred);
1628 if (error != ENOIOCTL)
1631 error = ttioctl(tp, cmd, data, ap->a_fflag);
1632 disc_optim(tp, &tp->t_termios, com);
1633 if (error != ENOIOCTL) {
1640 outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
1642 cd_etc(com, CD1400_ETC_SENDBREAK);
1647 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1649 cd_etc(com, CD1400_ETC_STOPBREAK);
1653 (void)commctl(com, TIOCM_DTR, DMBIS);
1656 (void)commctl(com, TIOCM_DTR, DMBIC);
1659 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
1660 * changes get undone on the next call to comparam().
1663 (void)commctl(com, *(int *)data, DMSET);
1666 (void)commctl(com, *(int *)data, DMBIS);
1669 (void)commctl(com, *(int *)data, DMBIC);
1672 *(int *)data = commctl(com, 0, DMGET);
1675 /* must be root since the wait applies to following logins */
1676 error = suser_cred(ap->a_cred, 0);
1681 com->dtr_wait = *(int *)data * hz / 100;
1684 *(int *)data = com->dtr_wait * 100 / hz;
1687 com->do_timestamp = TRUE;
1688 *(struct timeval *)data = com->timestamp;
1690 case TIOCDCDTIMESTAMP:
1691 com->do_dcd_timestamp = TRUE;
1692 *(struct timeval *)data = com->dcd_timestamp;
1703 siopoll(void *data, void *frame)
1710 if (com_events == 0)
1713 for (unit = 0; unit < NSIO; ++unit) {
1718 com = com_addr(unit);
1724 * XXX forget any events related to closed devices
1725 * (actually never opened devices) so that we don't
1729 incc = com->iptr - com->ibuf;
1730 com->iptr = com->ibuf;
1731 if (com->state & CS_CHECKMSR) {
1732 incc += LOTS_OF_EVENTS;
1733 com->state &= ~CS_CHECKMSR;
1739 "sio%d: %d events for device with no tp\n",
1743 if (com->iptr != com->ibuf) {
1748 if (com->state & CS_CHECKMSR) {
1749 u_char delta_modem_status;
1752 delta_modem_status = com->last_modem_status
1753 ^ com->prev_modem_status;
1754 com->prev_modem_status = com->last_modem_status;
1755 com_events -= LOTS_OF_EVENTS;
1756 com->state &= ~CS_CHECKMSR;
1758 if (delta_modem_status & MSR_DCD)
1759 (*linesw[tp->t_line].l_modem)
1760 (tp, com->prev_modem_status & MSR_DCD);
1762 if (com->extra_state & CSE_ODONE) {
1764 com_events -= LOTS_OF_EVENTS;
1765 com->extra_state &= ~CSE_ODONE;
1767 if (!(com->state & CS_BUSY)) {
1768 tp->t_state &= ~TS_BUSY;
1771 if (com->etc != ETC_NONE) {
1772 if (com->etc == ETC_BREAK_ENDED)
1773 com->etc = ETC_NONE;
1777 if (com->state & CS_ODONE) {
1779 com_events -= LOTS_OF_EVENTS;
1780 com->state &= ~CS_ODONE;
1782 (*linesw[tp->t_line].l_start)(tp);
1784 if (com_events == 0)
1787 if (com_events >= LOTS_OF_EVENTS)
1792 comparam(struct tty *tp, struct termios *t)
1808 /* do historical conversions */
1809 if (t->c_ispeed == 0)
1810 t->c_ispeed = t->c_ospeed;
1812 unit = DEV_TO_UNIT(tp->t_dev);
1813 com = com_addr(unit);
1815 /* check requested parameters */
1816 cy_clock = CY_CLOCK(com->gfrcr_image);
1817 idivisor = comspeed(t->c_ispeed, cy_clock, &iprescaler);
1820 odivisor = comspeed(t->c_ospeed, cy_clock, &oprescaler);
1824 /* parameters are OK, convert them to the com struct and the device */
1827 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */
1829 (void)commctl(com, TIOCM_DTR, DMBIS);
1832 * This returns with interrupts disabled so that we can complete
1833 * the speed change atomically.
1835 (void) siosetwater(com, t->c_ispeed);
1837 /* XXX we don't actually change the speed atomically. */
1840 if (idivisor != 0) {
1841 cd_setreg(com, CD1400_RBPR, idivisor);
1842 cd_setreg(com, CD1400_RCOR, iprescaler);
1844 if (odivisor != 0) {
1845 cd_setreg(com, CD1400_TBPR, odivisor);
1846 cd_setreg(com, CD1400_TCOR, oprescaler);
1852 * transmitter enable (always set)
1855 opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
1856 | (cflag & CREAD ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
1857 if (opt != com->channel_control) {
1858 com->channel_control = opt;
1859 cd1400_channel_cmd(com, opt);
1863 /* set special chars */
1864 /* XXX if one is _POSIX_VDISABLE, can't use some others */
1865 if (t->c_cc[VSTOP] != _POSIX_VDISABLE)
1866 cd_setreg(com, CD1400_SCHR1, t->c_cc[VSTOP]);
1867 if (t->c_cc[VSTART] != _POSIX_VDISABLE)
1868 cd_setreg(com, CD1400_SCHR2, t->c_cc[VSTART]);
1869 if (t->c_cc[VINTR] != _POSIX_VDISABLE)
1870 cd_setreg(com, CD1400_SCHR3, t->c_cc[VINTR]);
1871 if (t->c_cc[VSUSP] != _POSIX_VDISABLE)
1872 cd_setreg(com, CD1400_SCHR4, t->c_cc[VSUSP]);
1876 * set channel option register 1 -
1883 if (cflag & PARENB) {
1885 opt |= CD1400_COR1_PARODD;
1886 opt |= CD1400_COR1_PARNORMAL;
1889 if (!(iflag & INPCK))
1890 opt |= CD1400_COR1_NOINPCK;
1893 if (cflag & CSTOPB) {
1895 opt |= CD1400_COR1_STOP2;
1898 switch (cflag & CSIZE) {
1901 opt |= CD1400_COR1_CS5;
1905 opt |= CD1400_COR1_CS6;
1909 opt |= CD1400_COR1_CS7;
1913 opt |= CD1400_COR1_CS8;
1917 if (opt != com->cor[0]) {
1918 cor_change |= CD1400_CCR_COR1;
1919 cd_setreg(com, CD1400_COR1, com->cor[0] = opt);
1923 * Set receive time-out period, normally to max(one char time, 5 ms).
1925 if (t->c_ispeed == 0)
1926 itimeout = cd_getreg(com, CD1400_RTPR);
1928 itimeout = (1000 * bits + t->c_ispeed - 1) / t->c_ispeed;
1934 if (itimeout < MIN_RTP)
1937 if (!(t->c_lflag & ICANON) && t->c_cc[VMIN] != 0 && t->c_cc[VTIME] != 0
1938 && t->c_cc[VTIME] * 10 > itimeout)
1939 itimeout = t->c_cc[VTIME] * 10;
1942 cd_setreg(com, CD1400_RTPR, itimeout);
1945 * set channel option register 2 -
1951 opt |= CD1400_COR2_IXANY;
1953 opt |= CD1400_COR2_IXOFF;
1955 #ifndef SOFT_CTS_OFLOW
1956 if (cflag & CCTS_OFLOW)
1957 opt |= CD1400_COR2_CCTS_OFLOW;
1960 if (opt != com->cor[1]) {
1961 cor_change |= CD1400_CCR_COR2;
1962 cd_setreg(com, CD1400_COR2, com->cor[1] = opt);
1967 * set channel option register 3 -
1968 * receiver FIFO interrupt threshold
1971 opt = RxFifoThreshold;
1973 if (t->c_lflag & ICANON)
1974 opt |= CD1400_COR3_SCD34; /* detect INTR & SUSP chars */
1976 /* detect and transparently handle START and STOP chars */
1977 opt |= CD1400_COR3_FCT | CD1400_COR3_SCD12;
1979 if (opt != com->cor[2]) {
1980 cor_change |= CD1400_CCR_COR3;
1981 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
1984 /* notify the CD1400 if COR1-3 have changed */
1986 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | cor_change);
1989 * set channel option register 4 -
1992 * received exception processing
1996 opt |= CD1400_COR4_IGNCR;
1999 * we need a new ttyinput() for this, as we don't want to
2000 * have ICRNL && INLCR being done in both layers, or to have
2001 * synchronisation problems
2004 opt |= CD1400_COR4_ICRNL;
2006 opt |= CD1400_COR4_INLCR;
2009 opt |= CD1400_COR4_IGNBRK | CD1400_COR4_NOBRKINT;
2011 * The `-ignbrk -brkint parmrk' case is not handled by the hardware,
2012 * so only tell the hardware about -brkint if -parmrk.
2014 if (!(iflag & (BRKINT | PARMRK)))
2015 opt |= CD1400_COR4_NOBRKINT;
2017 /* XXX using this "intelligence" breaks reporting of overruns. */
2019 opt |= CD1400_COR4_PFO_DISCARD;
2022 opt |= CD1400_COR4_PFO_ESC;
2024 opt |= CD1400_COR4_PFO_NUL;
2027 opt |= CD1400_COR4_PFO_EXCEPTION;
2029 cd_setreg(com, CD1400_COR4, opt);
2032 * set channel option register 5 -
2036 opt |= CD1400_COR5_ISTRIP;
2037 if (t->c_iflag & IEXTEN)
2038 /* enable LNEXT (e.g. ctrl-v quoting) handling */
2039 opt |= CD1400_COR5_LNEXT;
2041 if (t->c_oflag & ONLCR)
2042 opt |= CD1400_COR5_ONLCR;
2043 if (t->c_oflag & OCRNL)
2044 opt |= CD1400_COR5_OCRNL;
2046 cd_setreg(com, CD1400_COR5, opt);
2049 * We always generate modem status change interrupts for CD changes.
2050 * Among other things, this is necessary to track TS_CARR_ON for
2051 * pstat to print even when the driver doesn't care. CD changes
2052 * should be rare so interrupts for them are not worth extra code to
2053 * avoid. We avoid interrupts for other modem status changes (except
2054 * for CTS changes when SOFT_CTS_OFLOW is configured) since this is
2055 * simplest and best.
2059 * set modem change option register 1
2060 * generate modem interrupts on which 1 -> 0 input transitions
2061 * also controls auto-DTR output flow-control, which we don't use
2063 opt = CD1400_MCOR1_CDzd;
2064 #ifdef SOFT_CTS_OFLOW
2065 if (cflag & CCTS_OFLOW)
2066 opt |= CD1400_MCOR1_CTSzd;
2068 cd_setreg(com, CD1400_MCOR1, opt);
2071 * set modem change option register 2
2072 * generate modem interrupts on specific 0 -> 1 input transitions
2074 opt = CD1400_MCOR2_CDod;
2075 #ifdef SOFT_CTS_OFLOW
2076 if (cflag & CCTS_OFLOW)
2077 opt |= CD1400_MCOR2_CTSod;
2079 cd_setreg(com, CD1400_MCOR2, opt);
2082 * XXX should have done this long ago, but there is too much state
2083 * to change all atomically.
2087 com->state &= ~CS_TTGO;
2088 if (!(tp->t_state & TS_TTSTOP))
2089 com->state |= CS_TTGO;
2090 if (cflag & CRTS_IFLOW) {
2091 com->state |= CS_RTS_IFLOW;
2093 * If CS_RTS_IFLOW just changed from off to on, the change
2094 * needs to be propagated to MCR_RTS. This isn't urgent,
2095 * so do it later by calling comstart() instead of repeating
2096 * a lot of code from comstart() here.
2098 } else if (com->state & CS_RTS_IFLOW) {
2099 com->state &= ~CS_RTS_IFLOW;
2101 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS
2102 * on here, since comstart() won't do it later.
2105 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2107 cd_setreg(com, com->mcr_rts_reg,
2108 com->mcr_image |= com->mcr_rts);
2113 * Set up state to handle output flow control.
2114 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2115 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2117 com->state |= CS_ODEVREADY;
2118 #ifdef SOFT_CTS_OFLOW
2119 com->state &= ~CS_CTS_OFLOW;
2120 if (cflag & CCTS_OFLOW) {
2121 com->state |= CS_CTS_OFLOW;
2122 if (!(com->last_modem_status & MSR_CTS))
2123 com->state &= ~CS_ODEVREADY;
2126 /* XXX shouldn't call functions while intrs are disabled. */
2127 disc_optim(tp, t, com);
2130 * Recover from fiddling with CS_TTGO. We used to call siointr1()
2131 * unconditionally, but that defeated the careful discarding of
2132 * stale input in sioopen().
2134 if (com->state >= (CS_BUSY | CS_TTGO))
2137 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2138 if (!(com->intr_enable & CD1400_SRER_TXRDY))
2139 cd_setreg(com, CD1400_SRER,
2141 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2142 | CD1400_SRER_TXRDY);
2144 if (com->intr_enable & CD1400_SRER_TXRDY)
2145 cd_setreg(com, CD1400_SRER,
2147 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2148 | CD1400_SRER_TXMPTY);
2154 if (com->ibufold != NULL) {
2155 kfree(com->ibufold, M_DEVBUF);
2156 com->ibufold = NULL;
2162 siosetwater(struct com_s *com, speed_t speed)
2170 * Make the buffer size large enough to handle a softtty interrupt
2171 * latency of about 2 ticks without loss of throughput or data
2172 * (about 3 ticks if input flow control is not used or not honoured,
2173 * but a bit less for CS5-CS7 modes).
2175 cp4ticks = speed / 10 / hz * 4;
2176 for (ibufsize = 128; ibufsize < cp4ticks;)
2178 if (ibufsize == com->ibufsize) {
2184 * Allocate input buffer. The extra factor of 2 in the size is
2185 * to allow for an error byte for each input byte.
2187 ibuf = kmalloc(2 * ibufsize, M_DEVBUF, M_WAITOK);
2189 /* Initialize non-critical variables. */
2190 com->ibufold = com->ibuf;
2191 com->ibufsize = ibufsize;
2194 tp->t_ififosize = 2 * ibufsize;
2195 tp->t_ispeedwat = (speed_t)-1;
2196 tp->t_ospeedwat = (speed_t)-1;
2200 * Read current input buffer, if any. Continue with interrupts
2204 if (com->iptr != com->ibuf)
2208 * Initialize critical variables, including input buffer watermarks.
2209 * The external device is asked to stop sending when the buffer
2210 * exactly reaches high water, or when the high level requests it.
2211 * The high level is notified immediately (rather than at a later
2212 * clock tick) when this watermark is reached.
2213 * The buffer size is chosen so the watermark should almost never
2215 * The low watermark is invisibly 0 since the buffer is always
2216 * emptied all at once.
2218 com->iptr = com->ibuf = ibuf;
2219 com->ibufend = ibuf + ibufsize;
2220 com->ierroff = ibufsize;
2221 com->ihighwater = ibuf + 3 * ibufsize / 4;
2226 comstart(struct tty *tp)
2234 unit = DEV_TO_UNIT(tp->t_dev);
2235 com = com_addr(unit);
2244 if (tp->t_state & TS_TTSTOP) {
2245 com->state &= ~CS_TTGO;
2246 if (com->intr_enable & CD1400_SRER_TXRDY)
2247 cd_setreg(com, CD1400_SRER,
2249 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2250 | CD1400_SRER_TXMPTY);
2252 com->state |= CS_TTGO;
2253 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)
2254 && !(com->intr_enable & CD1400_SRER_TXRDY))
2255 cd_setreg(com, CD1400_SRER,
2257 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2258 | CD1400_SRER_TXRDY);
2260 if (tp->t_state & TS_TBLOCK) {
2261 if (com->mcr_image & com->mcr_rts && com->state & CS_RTS_IFLOW)
2263 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2265 cd_setreg(com, com->mcr_rts_reg,
2266 com->mcr_image &= ~com->mcr_rts);
2269 if (!(com->mcr_image & com->mcr_rts)
2270 && com->iptr < com->ihighwater
2271 && com->state & CS_RTS_IFLOW)
2273 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2275 cd_setreg(com, com->mcr_rts_reg,
2276 com->mcr_image |= com->mcr_rts);
2280 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2285 if (tp->t_outq.c_cc != 0) {
2289 if (!com->obufs[0].l_queued) {
2293 com->obufs[0].l_tail
2294 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2296 com->obufs[0].l_next = NULL;
2297 com->obufs[0].l_queued = TRUE;
2299 if (com->state & CS_BUSY) {
2300 qp = com->obufq.l_next;
2301 while ((next = qp->l_next) != NULL)
2303 qp->l_next = &com->obufs[0];
2305 com->obufq.l_head = com->obufs[0].l_head;
2306 com->obufq.l_tail = com->obufs[0].l_tail;
2307 com->obufq.l_next = &com->obufs[0];
2308 com->state |= CS_BUSY;
2309 if (com->state >= (CS_BUSY | CS_TTGO
2311 cd_setreg(com, CD1400_SRER,
2314 & ~CD1400_SRER_TXMPTY)
2315 | CD1400_SRER_TXRDY);
2319 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2323 com->obufs[1].l_tail
2324 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2326 com->obufs[1].l_next = NULL;
2327 com->obufs[1].l_queued = TRUE;
2329 if (com->state & CS_BUSY) {
2330 qp = com->obufq.l_next;
2331 while ((next = qp->l_next) != NULL)
2333 qp->l_next = &com->obufs[1];
2335 com->obufq.l_head = com->obufs[1].l_head;
2336 com->obufq.l_tail = com->obufs[1].l_tail;
2337 com->obufq.l_next = &com->obufs[1];
2338 com->state |= CS_BUSY;
2339 if (com->state >= (CS_BUSY | CS_TTGO
2341 cd_setreg(com, CD1400_SRER,
2344 & ~CD1400_SRER_TXMPTY)
2345 | CD1400_SRER_TXRDY);
2349 tp->t_state |= TS_BUSY;
2357 if (com->state >= (CS_BUSY | CS_TTGO))
2358 siointr1(com); /* fake interrupt to start output */
2366 comstop(struct tty *tp, int rw)
2371 com = com_addr(DEV_TO_UNIT(tp->t_dev));
2375 com->obufs[0].l_queued = FALSE;
2376 com->obufs[1].l_queued = FALSE;
2377 if (com->extra_state & CSE_ODONE) {
2378 com_events -= LOTS_OF_EVENTS;
2379 com->extra_state &= ~CSE_ODONE;
2380 if (com->etc != ETC_NONE) {
2381 if (com->etc == ETC_BREAK_ENDED)
2382 com->etc = ETC_NONE;
2386 com->tp->t_state &= ~TS_BUSY;
2387 if (com->state & CS_ODONE)
2388 com_events -= LOTS_OF_EVENTS;
2389 com->state &= ~(CS_ODONE | CS_BUSY);
2392 /* XXX no way to reset only input fifo. */
2393 com_events -= (com->iptr - com->ibuf);
2394 com->iptr = com->ibuf;
2399 if (rw & FWRITE && com->etc == ETC_NONE)
2400 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
2405 commctl(struct com_s *com, int bits, int how)
2411 if (com->channel_control & CD1400_CCR_RCVEN)
2413 mcr = com->mcr_image;
2414 if (mcr & com->mcr_dtr)
2416 if (mcr & com->mcr_rts)
2417 /* XXX wired on for Cyclom-8Ys */
2421 * We must read the modem status from the hardware because
2422 * we don't generate modem status change interrupts for all
2423 * changes, so com->prev_modem_status is not guaranteed to
2424 * be up to date. This is safe, unlike for sio, because
2425 * reading the status register doesn't clear pending modem
2426 * status change interrupts.
2428 msr = cd_getreg(com, CD1400_MSVR2);
2437 /* XXX not connected except for Cyclom-16Y? */
2442 if (bits & TIOCM_DTR)
2443 mcr |= com->mcr_dtr;
2444 if (bits & TIOCM_RTS)
2445 mcr |= com->mcr_rts;
2449 com->mcr_image = mcr;
2450 cd_setreg(com, CD1400_MSVR1, mcr);
2451 cd_setreg(com, CD1400_MSVR2, mcr);
2454 com->mcr_image = mcr = com->mcr_image | mcr;
2455 cd_setreg(com, CD1400_MSVR1, mcr);
2456 cd_setreg(com, CD1400_MSVR2, mcr);
2459 com->mcr_image = mcr = com->mcr_image & ~mcr;
2460 cd_setreg(com, CD1400_MSVR1, mcr);
2461 cd_setreg(com, CD1400_MSVR2, mcr);
2476 * Set our timeout period to 1 second if no polled devices are open.
2477 * Otherwise set it to max(1/200, 1/hz).
2478 * Enable timeouts iff some device is open.
2480 callout_stop(&sio_timeout_handle);
2483 for (unit = 0; unit < NSIO; ++unit) {
2484 com = com_addr(unit);
2485 if (com != NULL && com->tp != NULL
2486 && com->tp->t_state & TS_ISOPEN) {
2489 if (com->poll || com->poll_output) {
2490 sio_timeout = hz > 200 ? hz / 200 : 1;
2497 sio_timeouts_until_log = hz / sio_timeout;
2498 callout_reset(&sio_timeout_handle, sio_timeout,
2501 /* Flush error messages, if any. */
2502 sio_timeouts_until_log = 1;
2503 comwakeup((void *)NULL);
2504 callout_stop(&sio_timeout_handle);
2509 comwakeup(void *chan)
2514 callout_reset(&sio_timeout_handle, sio_timeout, comwakeup, NULL);
2518 * Recover from lost output interrupts.
2519 * Poll any lines that don't use interrupts.
2521 for (unit = 0; unit < NSIO; ++unit) {
2522 com = com_addr(unit);
2524 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2533 * Check for and log errors, but not too often.
2535 if (--sio_timeouts_until_log > 0)
2537 sio_timeouts_until_log = hz / sio_timeout;
2538 for (unit = 0; unit < NSIO; ++unit) {
2541 com = com_addr(unit);
2544 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2549 delta = com->delta_error_counts[errnum];
2550 com->delta_error_counts[errnum] = 0;
2554 total = com->error_counts[errnum] += delta;
2555 log(LOG_ERR, "cy%d: %u more %s%s (total %lu)\n",
2556 unit, delta, error_desc[errnum],
2557 delta == 1 ? "" : "s", total);
2563 disc_optim(struct tty *tp, struct termios *t, struct com_s *com)
2565 #ifndef SOFT_HOTCHAR
2570 * XXX can skip a lot more cases if Smarts. Maybe
2571 * (IGNCR | ISTRIP | IXON) in c_iflag. But perhaps we
2572 * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2574 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2575 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2576 && (!(t->c_iflag & PARMRK)
2577 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2578 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2579 && linesw[tp->t_line].l_rint == ttyinput)
2580 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2582 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2583 com->hotchar = linesw[tp->t_line].l_hotchar;
2584 #ifndef SOFT_HOTCHAR
2585 opt = com->cor[2] & ~CD1400_COR3_SCD34;
2586 if (com->hotchar != 0) {
2587 cd_setreg(com, CD1400_SCHR3, com->hotchar);
2588 cd_setreg(com, CD1400_SCHR4, com->hotchar);
2589 opt |= CD1400_COR3_SCD34;
2591 if (opt != com->cor[2]) {
2592 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2593 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR3);
2599 /* standard line discipline input routine */
2601 cyinput(int c, struct tty *tp)
2603 /* XXX duplicate ttyinput(), but without the IXOFF/IXON/ISTRIP/IPARMRK
2604 * bits, as they are done by the CD1400. Hardly worth the effort,
2605 * given that high-throughput sessions are raw anyhow.
2611 comspeed(speed_t speed, u_long cy_clock, int *prescaler_io)
2621 if (speed < 0 || speed > 150000)
2624 /* determine which prescaler to use */
2625 for (prescaler_unit = 4, prescaler = 2048; prescaler_unit;
2626 prescaler_unit--, prescaler >>= 2) {
2627 if (cy_clock / prescaler / speed > 63)
2631 divider = (cy_clock / prescaler * 2 / speed + 1) / 2; /* round off */
2634 actual = cy_clock/prescaler/divider;
2636 /* 10 times error in percent: */
2637 error = ((actual - (long)speed) * 2000 / (long)speed + 1) / 2;
2639 /* 3.0% max error tolerance */
2640 if (error < -30 || error > 30)
2644 kprintf("prescaler = %d (%d)\n", prescaler, prescaler_unit);
2645 kprintf("divider = %d (%x)\n", divider, divider);
2646 kprintf("actual = %d\n", actual);
2647 kprintf("error = %d\n", error);
2650 *prescaler_io = prescaler_unit;
2655 cd1400_channel_cmd(struct com_s *com, int cmd)
2657 cd1400_channel_cmd_wait(com);
2658 cd_setreg(com, CD1400_CCR, cmd);
2659 cd1400_channel_cmd_wait(com);
2663 cd1400_channel_cmd_wait(struct com_s *com)
2665 struct timeval start;
2669 if (cd_getreg(com, CD1400_CCR) == 0)
2673 if (cd_getreg(com, CD1400_CCR) == 0)
2676 usec = 1000000 * (tv.tv_sec - start.tv_sec) +
2677 tv.tv_usec - start.tv_usec;
2680 "cy%d: channel command timeout (%ld usec)\n",
2688 cd_etc(struct com_s *com, int etc)
2691 * We can't change the hardware's ETC state while there are any
2692 * characters in the tx fifo, since those characters would be
2693 * interpreted as commands! Unputting characters from the fifo
2694 * is difficult, so we wait up to 12 character times for the fifo
2695 * to drain. The command will be delayed for up to 2 character
2696 * times for the tx to become empty. Unputting characters from
2697 * the tx holding and shift registers is impossible, so we wait
2698 * for the tx to become empty so that the command is sure to be
2699 * executed soon after we issue it.
2702 if (com->etc == etc) {
2706 if ((etc == CD1400_ETC_SENDBREAK
2707 && (com->etc == ETC_BREAK_STARTING
2708 || com->etc == ETC_BREAK_STARTED))
2709 || (etc == CD1400_ETC_STOPBREAK
2710 && (com->etc == ETC_BREAK_ENDING || com->etc == ETC_BREAK_ENDED
2711 || com->etc == ETC_NONE))) {
2716 cd_setreg(com, CD1400_SRER,
2718 = (com->intr_enable & ~CD1400_SRER_TXRDY) | CD1400_SRER_TXMPTY);
2721 while (com->etc == etc
2722 && tsleep(&com->etc, PCATCH, "cyetc", 0) == 0)
2727 cd_getreg(struct com_s *com, int reg)
2729 struct com_s *basecom;
2736 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2737 car = com->unit & CD1400_CAR_CHAN;
2738 cy_align = com->cy_align;
2739 iobase = com->iobase;
2743 if (basecom->car != car)
2744 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2745 val = cd_inb(iobase, reg, cy_align);
2752 cd_setreg(struct com_s *com, int reg, int val)
2754 struct com_s *basecom;
2760 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2761 car = com->unit & CD1400_CAR_CHAN;
2762 cy_align = com->cy_align;
2763 iobase = com->iobase;
2767 if (basecom->car != car)
2768 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2769 cd_outb(iobase, reg, cy_align, val);
2784 com = com_addr(unit);
2785 kprintf("info for channel %d\n", unit);
2786 kprintf("------------------\n");
2787 kprintf("total cyclom service probes:\t%d\n", cy_svrr_probes);
2788 kprintf("calls to upper layer:\t\t%d\n", cy_timeouts);
2791 iobase = com->iobase;
2793 kprintf("cd1400 base address:\\tt%p\n", iobase);
2794 kprintf("saved channel_control:\t\t0x%02x\n", com->channel_control);
2795 kprintf("saved cor1-3:\t\t\t0x%02x 0x%02x 0x%02x\n",
2796 com->cor[0], com->cor[1], com->cor[2]);
2797 kprintf("service request enable reg:\t0x%02x (0x%02x cached)\n",
2798 cd_getreg(com, CD1400_SRER), com->intr_enable);
2799 kprintf("service request register:\t0x%02x\n",
2800 cd_inb(iobase, CD1400_SVRR, com->cy_align));
2801 kprintf("modem status:\t\t\t0x%02x (0x%02x cached)\n",
2802 cd_getreg(com, CD1400_MSVR2), com->prev_modem_status);
2803 kprintf("rx/tx/mdm interrupt registers:\t0x%02x 0x%02x 0x%02x\n",
2804 cd_inb(iobase, CD1400_RIR, com->cy_align),
2805 cd_inb(iobase, CD1400_TIR, com->cy_align),
2806 cd_inb(iobase, CD1400_MIR, com->cy_align));
2808 kprintf("com state:\t\t\t0x%02x\n", com->state);
2809 kprintf("calls to comstart():\t\t%d (%d useful)\n",
2810 com->start_count, com->start_real);
2811 kprintf("rx buffer chars free:\t\t%d\n", com->iptr - com->ibuf);
2813 if (com->obufs[0].l_queued)
2814 ocount += com->obufs[0].l_tail - com->obufs[0].l_head;
2815 if (com->obufs[1].l_queued)
2816 ocount += com->obufs[1].l_tail - com->obufs[1].l_head;
2817 kprintf("tx buffer chars:\t\t%u\n", ocount);
2818 kprintf("received chars:\t\t\t%d\n", com->bytes_in);
2819 kprintf("received exceptions:\t\t%d\n", com->recv_exception);
2820 kprintf("modem signal deltas:\t\t%d\n", com->mdm);
2821 kprintf("transmitted chars:\t\t%d\n", com->bytes_out);
2825 kprintf("tty state:\t\t\t0x%08x\n", tp->t_state);
2827 "upper layer queue lengths:\t%d raw, %d canon, %d output\n",
2828 tp->t_rawq.c_cc, tp->t_canq.c_cc, tp->t_outq.c_cc);
2830 kprintf("tty state:\t\t\tclosed\n");
2832 #endif /* CyDebug */