4 * cyclades cyclom-y serial driver
5 * Andrew Herbert <andrew@werple.apana.org.au>, 17 August 1993
7 * Copyright (c) 1993 Andrew Herbert.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name Andrew Herbert may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
24 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/sys/i386/isa/cy.c,v 1.97.2.2 2001/08/22 13:04:58 bde Exp $
35 #include "opt_compat.h"
45 * Temporary compile-time configuration options.
47 #define RxFifoThreshold (CD1400_RX_FIFO_SIZE / 2)
48 /* Number of chars in the receiver FIFO before an
49 * an interrupt is generated. Should depend on
50 * line speed. Needs to be about 6 on a 486DX33
51 * for 4 active ports at 115200 bps. Why doesn't
54 #define PollMode /* Use polling-based irq service routine, not the
55 * hardware svcack lines. Must be defined for
56 * Cyclom-16Y boards. Less efficient for Cyclom-8Ys,
57 * and stops 4 * 115200 bps from working.
59 #undef Smarts /* Enable slightly more CD1400 intelligence. Mainly
60 * the output CR/LF processing, plus we can avoid a
61 * few checks usually done in ttyinput().
63 * XXX not fully implemented, and not particularly
66 #undef CyDebug /* Include debugging code (not very expensive). */
68 /* These will go away. */
72 #include <sys/param.h>
73 #include <sys/systm.h>
78 #include <sys/dkstat.h>
79 #include <sys/fcntl.h>
80 #include <sys/interrupt.h>
81 #include <sys/kernel.h>
82 #include <sys/malloc.h>
83 #include <sys/syslog.h>
84 #include <sys/thread2.h>
85 #include <machine/clock.h>
87 #include <machine/lock.h>
89 #include <machine/psl.h>
91 #include <bus/isa/isa_device.h>
93 #include <machine_base/isa/ic/cd1400.h>
96 #define disable_intr() com_lock()
97 #define enable_intr() com_unlock()
99 #define disable_intr() ((void)0)
100 #define enable_intr() ((void)0)
104 * Dictionary so that I can name everything *sio* or *com* to compare with
105 * sio.c. There is also lots of ugly formatting and unnecessary ifdefs to
106 * simplify the comparision. These will go away.
108 #define LSR_BI CD1400_RDSR_BREAK
109 #define LSR_FE CD1400_RDSR_FE
110 #define LSR_OE CD1400_RDSR_OE
111 #define LSR_PE CD1400_RDSR_PE
112 #define MCR_DTR CD1400_MSVR2_DTR
113 #define MCR_RTS CD1400_MSVR1_RTS
114 #define MSR_CTS CD1400_MSVR2_CTS
115 #define MSR_DCD CD1400_MSVR2_CD
116 #define MSR_DSR CD1400_MSVR2_DSR
117 #define MSR_RI CD1400_MSVR2_RI
118 #define NSIO (NCY * CY_MAX_PORTS)
119 #define comconsole cyconsole
120 #define comdefaultrate cydefaultrate
121 #define com_events cy_events
122 #define comhardclose cyhardclose
123 #define commctl cymctl
124 #define comparam cyparam
125 #define comspeed cyspeed
126 #define comstart cystart
127 #define comwakeup cywakeup
128 #define nsio_tty ncy_tty
129 #define p_com_addr p_cy_addr
130 #define sioattach cyattach
131 #define sioclose cyclose
132 #define siodriver cydriver
133 #define siodtrwakeup cydtrwakeup
134 #define sioinput cyinput
135 #define siointr cyintr
136 #define siointr1 cyintr1
137 #define sioioctl cyioctl
138 #define sioopen cyopen
139 #define siopoll cypoll
140 #define sioprobe cyprobe
141 #define siosettimeout cysettimeout
142 #define siosetwater cysetwater
143 #define comstop cystop
144 #define siowrite cywrite
145 #define sio_registered cy_registered
146 #define sio_timeout cy_timeout
147 #define sio_timeout_handle cy_timeout_handle
148 #define sio_timeouts_until_log cy_timeouts_until_log
149 #define sio_tty cy_tty
151 #define CY_MAX_PORTS (CD1400_NO_OF_CHANNELS * CY_MAX_CD1400s)
153 /* We encode the cyclom unit number (cyu) in spare bits in the IVR's. */
154 #define CD1400_xIVR_CHAN_SHIFT 3
155 #define CD1400_xIVR_CHAN 0x1F
158 * ETC states. com->etc may also contain a hardware ETC command value,
159 * meaning that execution of that command is pending.
161 #define ETC_NONE 0 /* we depend on bzero() setting this */
162 #define ETC_BREAK_STARTING 1
163 #define ETC_BREAK_STARTED 2
164 #define ETC_BREAK_ENDING 3
165 #define ETC_BREAK_ENDED 4
167 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */
169 #define CALLOUT_MASK 0x80
170 #define CONTROL_MASK 0x60
171 #define CONTROL_INIT_STATE 0x20
172 #define CONTROL_LOCK_STATE 0x40
173 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
174 #define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK)
176 * Not all of the magic is parametrized in the following macros. 16 and
177 * 0xff are related to the bitfields in a udev_t. CY_MAX_PORTS must be
178 * ((0xff & ~MINOR_MAGIC_MASK) + 1) for things to work.
180 #define MINOR_TO_UNIT(mynor) (((mynor) >> 16) * CY_MAX_PORTS \
181 | (((mynor) & 0xff) & ~MINOR_MAGIC_MASK))
182 #define UNIT_TO_MINOR(unit) (((unit) / CY_MAX_PORTS) << 16 \
183 | (((unit) & 0xff) & ~MINOR_MAGIC_MASK))
187 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
188 * than the other bits so that they can be tested as a group without masking
191 * The following com and tty flags correspond closely:
192 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and
194 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart())
195 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam())
196 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam())
197 * TS_FLUSH is not used.
198 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
199 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
201 #define CS_BUSY 0x80 /* output in progress */
202 #define CS_TTGO 0x40 /* output not stopped by XOFF */
203 #define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */
204 #define CS_CHECKMSR 1 /* check of MSR scheduled */
205 #define CS_CTS_OFLOW 2 /* use CTS output flow control */
206 #define CS_DTR_OFF 0x10 /* DTR held off */
207 #define CS_ODONE 4 /* output completed */
208 #define CS_RTS_IFLOW 8 /* use RTS input flow control */
209 #define CSE_ODONE 1 /* output transmitted */
211 static char const * const error_desc[] = {
214 #define CE_INTERRUPT_BUF_OVERFLOW 1
215 "interrupt-level buffer overflow",
216 #define CE_TTY_BUF_OVERFLOW 2
217 "tty-level buffer overflow",
221 #define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum])
223 /* types. XXX - should be elsewhere */
224 typedef u_char bool_t; /* boolean */
225 typedef u_char volatile *cy_addr;
227 /* queue of linear buffers */
229 u_char *l_head; /* next char to process */
230 u_char *l_tail; /* one past the last char to process */
231 struct lbq *l_next; /* next in queue */
232 bool_t l_queued; /* nonzero if queued */
235 /* com device structure */
237 u_char state; /* miscellaneous flag bits */
238 bool_t active_out; /* nonzero if the callout device is open */
240 u_char cfcr_image; /* copy of value written to CFCR */
242 u_char etc; /* pending Embedded Transmit Command */
243 u_char extra_state; /* more flag bits, separate for order trick */
245 u_char fifo_image; /* copy of value written to FIFO */
247 u_char gfrcr_image; /* copy of value read from GFRCR */
249 bool_t hasfifo; /* nonzero for 16550 UARTs */
250 bool_t loses_outints; /* nonzero if device loses output interrupts */
252 u_char mcr_dtr; /* MCR bit that is wired to DTR */
253 u_char mcr_image; /* copy of value written to MCR */
254 u_char mcr_rts; /* MCR bit that is wired to RTS */
257 bool_t multiport; /* is this unit part of a multiport device? */
258 #endif /* COM_MULTIPORT */
259 bool_t no_irq; /* nonzero if irq is not attached */
260 bool_t poll; /* nonzero if polling is required */
261 bool_t poll_output; /* nonzero if polling for output is required */
263 int unit; /* unit number */
264 int dtr_wait; /* time to hold DTR down on close (* 1/hz) */
265 struct callout dtr_ch;
269 u_int wopeners; /* # processes waiting for DCD in open() */
272 * The high level of the driver never reads status registers directly
273 * because there would be too many side effects to handle conveniently.
274 * Instead, it reads copies of the registers stored here by the
277 u_char last_modem_status; /* last MSR read by intr handler */
278 u_char prev_modem_status; /* last MSR handled by high level */
280 u_char hotchar; /* ldisc-specific char to be handled ASAP */
281 u_char *ibuf; /* start of input buffer */
282 u_char *ibufend; /* end of input buffer */
283 u_char *ibufold; /* old input buffer, to be freed */
284 u_char *ihighwater; /* threshold in input buffer */
285 u_char *iptr; /* next free spot in input buffer */
286 int ibufsize; /* size of ibuf (not include error bytes) */
287 int ierroff; /* offset of error bytes in ibuf */
289 struct lbq obufq; /* head of queue of output buffers */
290 struct lbq obufs[2]; /* output buffers */
292 int cy_align; /* index for register alignment */
293 cy_addr cy_iobase; /* base address of this port's cyclom */
294 cy_addr iobase; /* base address of this port's cd1400 */
295 int mcr_rts_reg; /* cd1400 reg number of reg holding mcr_rts */
297 struct tty *tp; /* cross reference */
300 struct termios it_in; /* should be in struct tty */
301 struct termios it_out;
304 struct termios lt_in; /* should be in struct tty */
305 struct termios lt_out;
308 bool_t do_dcd_timestamp;
309 struct timeval timestamp;
310 struct timeval dcd_timestamp;
312 u_long bytes_in; /* statistics */
314 u_int delta_error_counts[CE_NTYPES];
315 u_long error_counts[CE_NTYPES];
317 u_int recv_exception; /* exception chars received */
318 u_int mdm; /* modem signal changes */
320 u_int start_count; /* no. of calls to comstart() */
321 u_int start_real; /* no. of calls that did something */
323 u_char car; /* CD1400 CAR shadow (if first unit in cd) */
324 u_char channel_control;/* CD1400 CCR control command shadow */
325 u_char cor[3]; /* CD1400 COR1-3 shadows */
326 u_char intr_enable; /* CD1400 SRER shadow */
329 * Data area for output buffers. Someday we should build the output
330 * buffer queue without copying data.
336 /* PCI driver entry point. */
337 int cyattach_common (cy_addr cy_iobase, int cy_align);
338 void siointr(void *);
340 static int cy_units (cy_addr cy_iobase, int cy_align);
341 static int sioattach (struct isa_device *dev);
342 static void cd1400_channel_cmd (struct com_s *com, int cmd);
343 static void cd1400_channel_cmd_wait (struct com_s *com);
344 static void cd_etc (struct com_s *com, int etc);
345 static int cd_getreg (struct com_s *com, int reg);
346 static void cd_setreg (struct com_s *com, int reg, int val);
347 static timeout_t siodtrwakeup;
348 static void comhardclose (struct com_s *com);
349 static void sioinput (struct com_s *com);
351 static void siointr1 (struct com_s *com);
353 static int commctl (struct com_s *com, int bits, int how);
354 static int comparam (struct tty *tp, struct termios *t);
355 static inthand2_t siopoll;
356 static int sioprobe (struct isa_device *dev);
357 static void siosettimeout (void);
358 static int siosetwater (struct com_s *com, speed_t speed);
359 static int comspeed (speed_t speed, u_long cy_clock,
361 static void comstart (struct tty *tp);
362 static void comstop (struct tty *tp, int rw);
363 static timeout_t comwakeup;
364 static void disc_optim (struct tty *tp, struct termios *t,
368 void cystatus (int unit);
371 static char driver_name[] = "cy";
373 /* table and macro for fast conversion from a unit number to its com struct */
374 static struct com_s *p_com_addr[NSIO];
375 #define com_addr(unit) (p_com_addr[unit])
377 struct isa_driver siodriver = {
378 sioprobe, sioattach, driver_name
381 static d_open_t sioopen;
382 static d_close_t sioclose;
383 static d_write_t siowrite;
384 static d_ioctl_t sioioctl;
386 static struct dev_ops sio_ops = {
387 { driver_name, 0, D_TTY },
393 .d_kqfilter = ttykqfilter,
394 .d_revoke = ttyrevoke
397 static int comconsole = -1;
398 static speed_t comdefaultrate = TTYDEF_SPEED;
399 static u_int com_events; /* input chars + weighted output completions */
400 static bool_t sio_registered;
401 static int sio_timeout;
402 static int sio_timeouts_until_log;
403 static struct callout sio_timeout_handle;
405 static struct tty *sio_tty[NSIO];
407 static struct tty sio_tty[NSIO];
409 static const int nsio_tty = NSIO;
412 static u_int cd_inbs;
413 static u_int cy_inbs;
414 static u_int cd_outbs;
415 static u_int cy_outbs;
416 static u_int cy_svrr_probes;
417 static u_int cy_timeouts;
420 static int cy_chip_offset[] = {
421 0x0000, 0x0400, 0x0800, 0x0c00, 0x0200, 0x0600, 0x0a00, 0x0e00,
423 static int cy_nr_cd1400s[NCY];
424 static int cy_total_devices;
425 #undef RxFifoThreshold
426 static int volatile RxFifoThreshold = (CD1400_RX_FIFO_SIZE / 2);
429 sioprobe(struct isa_device *dev)
433 iobase = (cy_addr)dev->id_maddr;
435 /* Cyclom-16Y hardware reset (Cyclom-8Ys don't care) */
436 cy_inb(iobase, CY16_RESET, 0); /* XXX? */
437 DELAY(500); /* wait for the board to get its act together */
439 /* this is needed to get the board out of reset */
440 cy_outb(iobase, CY_CLEAR_INTR, 0, 0);
443 return (cy_units(iobase, 0) == 0 ? 0 : -1);
447 cy_units(cy_addr cy_iobase, int cy_align)
450 u_char firmware_version = 0; /* assign to avoid warning */
454 for (cyu = 0; cyu < CY_MAX_CD1400s; ++cyu) {
455 iobase = cy_iobase + (cy_chip_offset[cyu] << cy_align);
457 /* wait for chip to become ready for new command */
458 for (i = 0; i < 10; i++) {
460 if (!cd_inb(iobase, CD1400_CCR, cy_align))
464 /* clear the GFRCR register */
465 cd_outb(iobase, CD1400_GFRCR, cy_align, 0);
467 /* issue a reset command */
468 cd_outb(iobase, CD1400_CCR, cy_align,
469 CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
471 /* wait for the CD1400 to initialize itself */
472 for (i = 0; i < 200; i++) {
475 /* retrieve firmware version */
476 firmware_version = cd_inb(iobase, CD1400_GFRCR,
478 if ((firmware_version & 0xf0) == 0x40)
483 * Anything in the 0x40-0x4F range is fine.
484 * If one CD1400 is bad then we don't support higher
485 * numbered good ones on this board.
487 if ((firmware_version & 0xf0) != 0x40)
494 sioattach(struct isa_device *isdp)
498 lwkt_gettoken(&tty_token);
499 adapter = cyattach_common((cy_addr) isdp->id_maddr, 0);
501 lwkt_reltoken(&tty_token);
507 * This kludge is to allow ISA/PCI device specifications in the
508 * kernel config file to be in any order.
510 if (isdp->id_unit != adapter) {
511 kprintf("cy%d: attached as cy%d\n", isdp->id_unit, adapter);
512 isdp->id_unit = adapter; /* XXX */
514 isdp->id_intr = (inthand2_t *)siointr;
515 /* isdp->id_ri_flags |= RI_FAST; XXX unimplemented - use newbus! */
516 lwkt_reltoken(&tty_token);
521 * NOTE: Must be called with tty_token held
524 cyattach_common(cy_addr cy_iobase, int cy_align)
528 u_char firmware_version;
534 ASSERT_LWKT_TOKEN_HELD(&tty_token);
536 adapter = cy_total_devices;
537 if ((u_int)adapter >= NCY) {
539 "cy%d: can't attach adapter: insufficient cy devices configured\n",
543 ncyu = cy_units(cy_iobase, cy_align);
546 cy_nr_cd1400s[adapter] = ncyu;
549 unit = adapter * CY_MAX_PORTS;
550 for (cyu = 0; cyu < ncyu; ++cyu) {
553 iobase = (cy_addr) (cy_iobase
554 + (cy_chip_offset[cyu] << cy_align));
555 firmware_version = cd_inb(iobase, CD1400_GFRCR, cy_align);
557 /* Set up a receive timeout period of than 1+ ms. */
558 cd_outb(iobase, CD1400_PPR, cy_align,
559 howmany(CY_CLOCK(firmware_version)
560 / CD1400_PPR_PRESCALER, 1000));
562 for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; ++cdu, ++unit) {
565 com = kmalloc(sizeof *com, M_DEVBUF, M_WAITOK | M_ZERO);
567 callout_init_mp(&com->dtr_ch);
568 com->gfrcr_image = firmware_version;
569 if (CY_RTS_DTR_SWAPPED(firmware_version)) {
570 com->mcr_dtr = MCR_RTS;
571 com->mcr_rts = MCR_DTR;
572 com->mcr_rts_reg = CD1400_MSVR2;
574 com->mcr_dtr = MCR_DTR;
575 com->mcr_rts = MCR_RTS;
576 com->mcr_rts_reg = CD1400_MSVR1;
578 com->dtr_wait = 3 * hz;
579 com->obufs[0].l_head = com->obuf1;
580 com->obufs[1].l_head = com->obuf2;
582 com->cy_align = cy_align;
583 com->cy_iobase = cy_iobase;
584 com->iobase = iobase;
585 com->car = ~CD1400_CAR_CHAN;
588 * We don't use all the flags from <sys/ttydefaults.h> since they
589 * are only relevant for logins. It's important to have echo off
590 * initially so that the line doesn't start blathering before the
591 * echo flag can be turned off.
593 com->it_in.c_iflag = 0;
594 com->it_in.c_oflag = 0;
595 com->it_in.c_cflag = TTYDEF_CFLAG;
596 com->it_in.c_lflag = 0;
597 if (unit == comconsole) {
598 com->it_in.c_iflag = TTYDEF_IFLAG;
599 com->it_in.c_oflag = TTYDEF_OFLAG;
600 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
601 com->it_in.c_lflag = TTYDEF_LFLAG;
602 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
604 if (siosetwater(com, com->it_in.c_ispeed) != 0) {
606 kfree(com, M_DEVBUF);
610 termioschars(&com->it_in);
611 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
612 com->it_out = com->it_in;
615 com_addr(unit) = com;
618 if (!sio_registered) {
619 callout_init_mp(&sio_timeout_handle);
620 register_swi_mp(SWI_TTY, siopoll, NULL, "cy", NULL, -1);
621 sio_registered = TRUE;
623 minorbase = UNIT_TO_MINOR(unit);
624 make_dev(&sio_ops, minorbase,
625 UID_ROOT, GID_WHEEL, 0600, "ttyc%r%r", adapter,
626 unit % CY_MAX_PORTS);
627 make_dev(&sio_ops, minorbase | CONTROL_INIT_STATE,
628 UID_ROOT, GID_WHEEL, 0600, "ttyic%r%r", adapter,
629 unit % CY_MAX_PORTS);
630 make_dev(&sio_ops, minorbase | CONTROL_LOCK_STATE,
631 UID_ROOT, GID_WHEEL, 0600, "ttylc%r%r", adapter,
632 unit % CY_MAX_PORTS);
633 make_dev(&sio_ops, minorbase | CALLOUT_MASK,
634 UID_UUCP, GID_DIALER, 0660, "cuac%r%r", adapter,
635 unit % CY_MAX_PORTS);
636 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_INIT_STATE,
637 UID_UUCP, GID_DIALER, 0660, "cuaic%r%r", adapter,
638 unit % CY_MAX_PORTS);
639 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE,
640 UID_UUCP, GID_DIALER, 0660, "cualc%r%r", adapter,
641 unit % CY_MAX_PORTS);
643 /* for(cdu...), for(cyu...) terminating blocks */
647 /* ensure an edge for the next interrupt */
648 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
654 sioopen(struct dev_open_args *ap)
656 cdev_t dev = ap->a_head.a_dev;
664 unit = MINOR_TO_UNIT(mynor);
665 if ((u_int) unit >= NSIO || (com = com_addr(unit)) == NULL)
667 if (mynor & CONTROL_MASK)
670 lwkt_gettoken(&tty_token);
672 tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
674 tp = com->tp = &sio_tty[unit];
679 * We jump to this label after all non-interrupted sleeps to pick
680 * up any changes of the device state.
683 while (com->state & CS_DTR_OFF) {
684 error = tsleep(&com->dtr_wait, PCATCH, "cydtr", 0);
688 if (tp->t_state & TS_ISOPEN) {
690 * The device is open, so everything has been initialized.
693 if (mynor & CALLOUT_MASK) {
694 if (!com->active_out) {
699 if (com->active_out) {
700 if (ap->a_oflags & O_NONBLOCK) {
704 error = tsleep(&com->active_out,
711 if (tp->t_state & TS_XCLUDE &&
712 priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
718 * The device isn't open, so there are no conflicts.
719 * Initialize it. Initialization is done twice in many
720 * cases: to preempt sleeping callin opens if we are
721 * callout, and to complete a callin open after DCD rises.
723 tp->t_oproc = comstart;
724 tp->t_stop = comstop;
725 tp->t_param = comparam;
727 tp->t_termios = mynor & CALLOUT_MASK
728 ? com->it_out : com->it_in;
730 /* Encode per-board unit in LIVR for access in intr routines. */
731 cd_setreg(com, CD1400_LIVR,
732 (unit & CD1400_xIVR_CHAN) << CD1400_xIVR_CHAN_SHIFT);
734 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
736 com->poll = com->no_irq;
737 com->poll_output = com->loses_outints;
740 error = comparam(tp, &tp->t_termios);
747 * (Re)enable and flush fifos.
749 * Certain SMC chips cause problems if the fifos
750 * are enabled while input is ready. Turn off the
751 * fifo if necessary to clear the input. We test
752 * the input ready bit after enabling the fifos
753 * since we've already enabled them in comparam()
754 * and to handle races between enabling and fresh
758 outb(iobase + com_fifo,
759 FIFO_RCV_RST | FIFO_XMT_RST
762 if (!(inb(com->line_status_port) & LSR_RXRDY))
764 outb(iobase + com_fifo, 0);
766 (void) inb(com->data_port);
771 (void) inb(com->line_status_port);
772 (void) inb(com->data_port);
773 com->prev_modem_status = com->last_modem_status
774 = inb(com->modem_status_port);
775 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS
780 * Flush fifos. This requires a full channel reset which
781 * also disables the transmitter and receiver. Recover
784 cd1400_channel_cmd(com,
785 CD1400_CCR_CMDRESET | CD1400_CCR_CHANRESET);
786 cd1400_channel_cmd(com, com->channel_control);
789 com->prev_modem_status = com->last_modem_status
790 = cd_getreg(com, CD1400_MSVR2);
791 cd_setreg(com, CD1400_SRER,
793 = CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
797 * Handle initial DCD. Callout devices get a fake initial
798 * DCD (trapdoor DCD). If we are callout, then any sleeping
799 * callin opens get woken up and resume sleeping on "cybi"
800 * instead of "cydcd".
803 * XXX `mynor & CALLOUT_MASK' should be
804 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
805 * TRAPDOOR_CARRIER is the default initial state for callout
806 * devices and SOFT_CARRIER is like CLOCAL except it hides
809 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
810 (*linesw[tp->t_line].l_modem)(tp, 1);
813 * Wait for DCD if necessary.
815 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
816 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) {
818 error = tsleep(TSA_CARR_ON(tp), PCATCH, "cydcd", 0);
824 error = (*linesw[tp->t_line].l_open)(dev, tp);
825 disc_optim(tp, &tp->t_termios, com);
826 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
827 com->active_out = TRUE;
831 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
833 lwkt_reltoken(&tty_token);
838 * NOTE: Must be called with tty_token held
841 sioclose(struct dev_close_args *ap)
843 cdev_t dev = ap->a_head.a_dev;
848 ASSERT_LWKT_TOKEN_HELD(&tty_token);
850 if (mynor & CONTROL_MASK)
852 lwkt_gettoken(&tty_token);
853 com = com_addr(MINOR_TO_UNIT(mynor));
856 cd_etc(com, CD1400_ETC_STOPBREAK);
857 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
858 disc_optim(tp, &tp->t_termios, com);
859 comstop(tp, FREAD | FWRITE);
864 #ifdef broken /* session holds a ref to the tty; can't deallocate */
866 com->tp = sio_tty[unit] = NULL;
868 lwkt_reltoken(&tty_token);
873 * NOTE: Must be called with tty_token held
876 comhardclose(struct com_s *com)
882 ASSERT_LWKT_TOKEN_HELD(&tty_token);
885 iobase = com->iobase;
889 com->poll_output = FALSE;
891 com->do_timestamp = 0;
893 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
898 cd_setreg(com, CD1400_COR2, com->cor[1] &= ~CD1400_COR2_ETC);
900 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
905 outb(iobase + com_ier, 0);
908 cd_setreg(com, CD1400_SRER, com->intr_enable = 0);
912 if ((tp->t_cflag & HUPCL)
914 * XXX we will miss any carrier drop between here and the
915 * next open. Perhaps we should watch DCD even when the
916 * port is closed; it is not sufficient to check it at
917 * the next open because it might go up and down while
918 * we're not watching.
921 && !(com->prev_modem_status & MSR_DCD)
922 && !(com->it_in.c_cflag & CLOCAL))
923 || !(tp->t_state & TS_ISOPEN)) {
924 (void)commctl(com, TIOCM_DTR, DMBIC);
926 /* Disable receiver (leave transmitter enabled). */
927 com->channel_control = CD1400_CCR_CMDCHANCTL
930 cd1400_channel_cmd(com, com->channel_control);
932 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
933 callout_reset(&com->dtr_ch, com->dtr_wait,
935 com->state |= CS_DTR_OFF;
942 * Disable fifos so that they are off after controlled
943 * reboots. Some BIOSes fail to detect 16550s when the
946 outb(iobase + com_fifo, 0);
949 com->active_out = FALSE;
950 wakeup(&com->active_out);
951 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */
956 siowrite(struct dev_write_args *ap)
958 cdev_t dev = ap->a_head.a_dev;
959 struct uio *uio = ap->a_uio;
965 if (mynor & CONTROL_MASK)
968 lwkt_gettoken(&tty_token);
969 unit = MINOR_TO_UNIT(mynor);
970 tp = com_addr(unit)->tp;
972 * (XXX) We disallow virtual consoles if the physical console is
973 * a serial port. This is in case there is a display attached that
974 * is not the console. In that situation we don't need/want the X
975 * server taking over the console.
977 if (constty != NULL && unit == comconsole)
980 /* XXX duplicate ttwrite(), but without so much output processing on
981 * CR & LF chars. Hardly worth the effort, given that high-throughput
982 * sessions are raw anyhow.
985 return ((*linesw[tp->t_line].l_write)(tp, uio, ap->a_ioflag));
987 lwkt_reltoken(&tty_token);
991 siodtrwakeup(void *chan)
995 lwkt_gettoken(&tty_token);
996 com = (struct com_s *)chan;
997 com->state &= ~CS_DTR_OFF;
998 wakeup(&com->dtr_wait);
999 lwkt_reltoken(&tty_token);
1003 * NOTE: Must be called with tty_token held
1006 sioinput(struct com_s *com)
1014 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1018 if (!(tp->t_state & TS_ISOPEN)) {
1019 com_events -= (com->iptr - com->ibuf);
1020 com->iptr = com->ibuf;
1023 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1025 * Avoid the grotesquely inefficient lineswitch routine
1026 * (ttyinput) in "raw" mode. It usually takes about 450
1027 * instructions (that's without canonical processing or echo!).
1028 * slinput is reasonably fast (usually 40 instructions plus
1033 incc = com->iptr - buf;
1034 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
1035 && (com->state & CS_RTS_IFLOW
1036 || tp->t_iflag & IXOFF)
1037 && !(tp->t_state & TS_TBLOCK))
1039 com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1040 += b_to_q((char *)buf, incc, &tp->t_rawq);
1044 tp->t_rawcc += incc;
1046 if (tp->t_state & TS_TTSTOP
1047 && (tp->t_iflag & IXANY
1048 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1049 tp->t_state &= ~TS_TTSTOP;
1050 tp->t_lflag &= ~FLUSHO;
1054 } while (buf < com->iptr);
1058 line_status = buf[com->ierroff];
1061 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
1062 if (line_status & LSR_BI)
1063 recv_data |= TTY_BI;
1064 if (line_status & LSR_FE)
1065 recv_data |= TTY_FE;
1066 if (line_status & LSR_OE)
1067 recv_data |= TTY_OE;
1068 if (line_status & LSR_PE)
1069 recv_data |= TTY_PE;
1071 (*linesw[tp->t_line].l_rint)(recv_data, tp);
1073 } while (buf < com->iptr);
1075 com_events -= (com->iptr - com->ibuf);
1076 com->iptr = com->ibuf;
1079 * There is now room for another low-level buffer full of input,
1080 * so enable RTS if it is now disabled and there is room in the
1081 * high-level buffer.
1083 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & com->mcr_rts) &&
1084 !(tp->t_state & TS_TBLOCK))
1086 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1088 cd_setreg(com, com->mcr_rts_reg,
1089 com->mcr_image |= com->mcr_rts);
1096 int unit = (int)arg;
1104 lwkt_gettoken(&tty_token);
1105 com_lock(); /* XXX could this be placed down lower in the loop? */
1107 baseu = unit * CY_MAX_PORTS;
1108 cy_align = com_addr(baseu)->cy_align;
1109 cy_iobase = com_addr(baseu)->cy_iobase;
1111 /* check each CD1400 in turn */
1112 for (cyu = 0; cyu < cy_nr_cd1400s[unit]; ++cyu) {
1113 iobase = (cy_addr) (cy_iobase
1114 + (cy_chip_offset[cyu] << cy_align));
1115 /* poll to see if it has any work */
1116 status = cd_inb(iobase, CD1400_SVRR, cy_align);
1122 /* service requests as appropriate, giving priority to RX */
1123 if (status & CD1400_SVRR_RXRDY) {
1135 save_rir = cd_inb(iobase, CD1400_RIR, cy_align);
1137 /* enter rx service */
1138 cd_outb(iobase, CD1400_CAR, cy_align, save_rir);
1139 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1140 = save_rir & CD1400_CAR_CHAN;
1142 serv_type = cd_inb(iobase, CD1400_RIVR, cy_align);
1143 com = com_addr(baseu
1144 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1145 & CD1400_xIVR_CHAN));
1147 /* ack receive service */
1148 serv_type = cy_inb(iobase, CY8_SVCACKR, cy_align);
1150 com = com_addr(baseu +
1151 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1152 & CD1400_xIVR_CHAN));
1155 if (serv_type & CD1400_RIVR_EXCEPTION) {
1156 ++com->recv_exception;
1157 line_status = cd_inb(iobase, CD1400_RDSR, cy_align);
1158 /* break/unnattached error bits or real input? */
1159 recv_data = cd_inb(iobase, CD1400_RDSR, cy_align);
1160 #ifndef SOFT_HOTCHAR
1161 if (line_status & CD1400_RDSR_SPECIAL
1162 && com->hotchar != 0)
1165 #if 1 /* XXX "intelligent" PFO error handling would break O error handling */
1166 if (line_status & (LSR_PE|LSR_FE|LSR_BI)) {
1168 Don't store PE if IGNPAR and BI if IGNBRK,
1169 this hack allows "raw" tty optimization
1170 works even if IGN* is set.
1172 if ( com->tp == NULL
1173 || !(com->tp->t_state & TS_ISOPEN)
1174 || ((line_status & (LSR_PE|LSR_FE))
1175 && (com->tp->t_iflag & IGNPAR))
1176 || ((line_status & LSR_BI)
1177 && (com->tp->t_iflag & IGNBRK)))
1179 if ( (line_status & (LSR_PE|LSR_FE))
1180 && (com->tp->t_state & TS_CAN_BYPASS_L_RINT)
1181 && ((line_status & LSR_FE)
1182 || ((line_status & LSR_PE)
1183 && (com->tp->t_iflag & INPCK))))
1189 if (com->hotchar != 0 && recv_data == com->hotchar)
1193 if (ioptr >= com->ibufend)
1194 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1196 if (com->do_timestamp)
1197 microtime(&com->timestamp);
1199 ioptr[0] = recv_data;
1200 ioptr[com->ierroff] = line_status;
1201 com->iptr = ++ioptr;
1202 if (ioptr == com->ihighwater
1203 && com->state & CS_RTS_IFLOW)
1205 outb(com->modem_ctl_port,
1206 com->mcr_image &= ~MCR_RTS);
1208 cd_outb(iobase, com->mcr_rts_reg,
1213 if (line_status & LSR_OE)
1214 CE_RECORD(com, CE_OVERRUN);
1220 count = cd_inb(iobase, CD1400_RDCR, cy_align);
1223 com->bytes_in += count;
1225 ifree = com->ibufend - ioptr;
1226 if (count > ifree) {
1228 com_events += ifree;
1230 if (com->do_timestamp)
1231 microtime(&com->timestamp);
1233 recv_data = cd_inb(iobase,
1237 if (com->hotchar != 0
1242 ioptr[0] = recv_data;
1243 ioptr[com->ierroff] = 0;
1245 } while (--ifree != 0);
1247 com->delta_error_counts
1248 [CE_INTERRUPT_BUF_OVERFLOW] += count;
1250 recv_data = cd_inb(iobase, CD1400_RDSR,
1253 if (com->hotchar != 0
1254 && recv_data == com->hotchar)
1257 } while (--count != 0);
1259 if (com->do_timestamp)
1260 microtime(&com->timestamp);
1261 if (ioptr <= com->ihighwater
1262 && ioptr + count > com->ihighwater
1263 && com->state & CS_RTS_IFLOW)
1265 outb(com->modem_ctl_port,
1266 com->mcr_image &= ~MCR_RTS);
1268 cd_outb(iobase, com->mcr_rts_reg,
1273 com_events += count;
1275 recv_data = cd_inb(iobase, CD1400_RDSR,
1278 if (com->hotchar != 0
1279 && recv_data == com->hotchar)
1282 ioptr[0] = recv_data;
1283 ioptr[com->ierroff] = 0;
1285 } while (--count != 0);
1291 /* terminate service context */
1293 cd_outb(iobase, CD1400_RIR, cy_align,
1295 & ~(CD1400_RIR_RDIREQ | CD1400_RIR_RBUSY));
1297 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1300 if (status & CD1400_SVRR_MDMCH) {
1302 u_char modem_status;
1310 save_mir = cd_inb(iobase, CD1400_MIR, cy_align);
1312 /* enter modem service */
1313 cd_outb(iobase, CD1400_CAR, cy_align, save_mir);
1314 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1315 = save_mir & CD1400_CAR_CHAN;
1317 com = com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS
1318 + (save_mir & CD1400_MIR_CHAN));
1320 /* ack modem service */
1321 vector = cy_inb(iobase, CY8_SVCACKM, cy_align);
1323 com = com_addr(baseu
1324 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1325 & CD1400_xIVR_CHAN));
1328 modem_status = cd_inb(iobase, CD1400_MSVR2, cy_align);
1329 if (modem_status != com->last_modem_status) {
1330 if (com->do_dcd_timestamp
1331 && !(com->last_modem_status & MSR_DCD)
1332 && modem_status & MSR_DCD)
1333 microtime(&com->dcd_timestamp);
1336 * Schedule high level to handle DCD changes. Note
1337 * that we don't use the delta bits anywhere. Some
1338 * UARTs mess them up, and it's easy to remember the
1339 * previous bits and calculate the delta.
1341 com->last_modem_status = modem_status;
1342 if (!(com->state & CS_CHECKMSR)) {
1343 com_events += LOTS_OF_EVENTS;
1344 com->state |= CS_CHECKMSR;
1348 #ifdef SOFT_CTS_OFLOW
1349 /* handle CTS change immediately for crisp flow ctl */
1350 if (com->state & CS_CTS_OFLOW) {
1351 if (modem_status & MSR_CTS) {
1352 com->state |= CS_ODEVREADY;
1353 if (com->state >= (CS_BUSY | CS_TTGO
1355 && !(com->intr_enable
1356 & CD1400_SRER_TXRDY))
1357 cd_outb(iobase, CD1400_SRER,
1361 & ~CD1400_SRER_TXMPTY
1362 | CD1400_SRER_TXRDY);
1364 com->state &= ~CS_ODEVREADY;
1365 if (com->intr_enable
1366 & CD1400_SRER_TXRDY)
1367 cd_outb(iobase, CD1400_SRER,
1371 & ~CD1400_SRER_TXRDY
1372 | CD1400_SRER_TXMPTY);
1378 /* terminate service context */
1380 cd_outb(iobase, CD1400_MIR, cy_align,
1382 & ~(CD1400_MIR_RDIREQ | CD1400_MIR_RBUSY));
1384 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1387 if (status & CD1400_SVRR_TXRDY) {
1396 save_tir = cd_inb(iobase, CD1400_TIR, cy_align);
1398 /* enter tx service */
1399 cd_outb(iobase, CD1400_CAR, cy_align, save_tir);
1400 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1401 = save_tir & CD1400_CAR_CHAN;
1403 com = com_addr(baseu
1404 + cyu * CD1400_NO_OF_CHANNELS
1405 + (save_tir & CD1400_TIR_CHAN));
1407 /* ack transmit service */
1408 vector = cy_inb(iobase, CY8_SVCACKT, cy_align);
1410 com = com_addr(baseu
1411 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1412 & CD1400_xIVR_CHAN));
1415 if (com->etc != ETC_NONE) {
1416 if (com->intr_enable & CD1400_SRER_TXRDY) {
1418 * Here due to sloppy SRER_TXRDY
1419 * enabling. Ignore. Come back when
1422 cd_outb(iobase, CD1400_SRER, cy_align,
1425 & ~CD1400_SRER_TXRDY)
1426 | CD1400_SRER_TXMPTY);
1427 goto terminate_tx_service;
1430 case CD1400_ETC_SENDBREAK:
1431 case CD1400_ETC_STOPBREAK:
1433 * Start the command. Come back on
1434 * next tx empty interrupt, hopefully
1435 * after command has been executed.
1437 cd_outb(iobase, CD1400_COR2, cy_align,
1438 com->cor[1] |= CD1400_COR2_ETC);
1439 cd_outb(iobase, CD1400_TDR, cy_align,
1441 cd_outb(iobase, CD1400_TDR, cy_align,
1443 if (com->etc == CD1400_ETC_SENDBREAK)
1444 com->etc = ETC_BREAK_STARTING;
1446 com->etc = ETC_BREAK_ENDING;
1447 goto terminate_tx_service;
1448 case ETC_BREAK_STARTING:
1450 * BREAK is now on. Continue with
1451 * SRER_TXMPTY processing, hopefully
1454 com->etc = ETC_BREAK_STARTED;
1456 case ETC_BREAK_STARTED:
1458 * Came back due to sloppy SRER_TXMPTY
1459 * enabling. Hope again.
1462 case ETC_BREAK_ENDING:
1464 * BREAK is now off. Continue with
1465 * SRER_TXMPTY processing and don't
1466 * come back. The SWI handler will
1467 * restart tx interrupts if necessary.
1469 cd_outb(iobase, CD1400_COR2, cy_align,
1471 &= ~CD1400_COR2_ETC);
1472 com->etc = ETC_BREAK_ENDED;
1473 if (!(com->state & CS_ODONE)) {
1474 com_events += LOTS_OF_EVENTS;
1475 com->state |= CS_ODONE;
1479 case ETC_BREAK_ENDED:
1481 * Shouldn't get here. Hope again.
1486 if (com->intr_enable & CD1400_SRER_TXMPTY) {
1487 if (!(com->extra_state & CSE_ODONE)) {
1488 com_events += LOTS_OF_EVENTS;
1489 com->extra_state |= CSE_ODONE;
1492 cd_outb(iobase, CD1400_SRER, cy_align,
1494 &= ~CD1400_SRER_TXMPTY);
1495 goto terminate_tx_service;
1497 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1501 ioptr = com->obufq.l_head;
1502 ocount = com->obufq.l_tail - ioptr;
1503 if (ocount > CD1400_TX_FIFO_SIZE)
1504 ocount = CD1400_TX_FIFO_SIZE;
1505 com->bytes_out += ocount;
1507 cd_outb(iobase, CD1400_TDR, cy_align,
1509 while (--ocount != 0);
1510 com->obufq.l_head = ioptr;
1511 if (ioptr >= com->obufq.l_tail) {
1514 qp = com->obufq.l_next;
1515 qp->l_queued = FALSE;
1518 com->obufq.l_head = qp->l_head;
1519 com->obufq.l_tail = qp->l_tail;
1520 com->obufq.l_next = qp;
1522 /* output just completed */
1523 com->state &= ~CS_BUSY;
1526 * The setting of CSE_ODONE may be
1527 * stale here. We currently only
1528 * use it when CS_BUSY is set, and
1529 * fixing it when we clear CS_BUSY
1532 if (com->extra_state & CSE_ODONE) {
1533 com_events -= LOTS_OF_EVENTS;
1534 com->extra_state &= ~CSE_ODONE;
1537 cd_outb(iobase, CD1400_SRER, cy_align,
1540 & ~CD1400_SRER_TXRDY)
1541 | CD1400_SRER_TXMPTY);
1543 if (!(com->state & CS_ODONE)) {
1544 com_events += LOTS_OF_EVENTS;
1545 com->state |= CS_ODONE;
1547 /* handle at high level ASAP */
1553 /* terminate service context */
1554 terminate_tx_service:
1556 cd_outb(iobase, CD1400_TIR, cy_align,
1558 & ~(CD1400_TIR_RDIREQ | CD1400_TIR_RBUSY));
1560 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1565 /* ensure an edge for the next interrupt */
1566 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
1571 lwkt_reltoken(&tty_token);
1576 siointr1(struct com_s *com)
1582 sioioctl(struct dev_ioctl_args *ap)
1584 cdev_t dev = ap->a_head.a_dev;
1585 caddr_t data = ap->a_data;
1586 u_long cmd = ap->a_cmd;
1591 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1593 struct termios term;
1596 lwkt_gettoken(&tty_token);
1598 com = com_addr(MINOR_TO_UNIT(mynor));
1599 if (mynor & CONTROL_MASK) {
1602 switch (mynor & CONTROL_MASK) {
1603 case CONTROL_INIT_STATE:
1604 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
1606 case CONTROL_LOCK_STATE:
1607 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
1610 lwkt_reltoken(&tty_token);
1611 return (ENODEV); /* /dev/nodev */
1615 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1617 lwkt_reltoken(&tty_token);
1620 *ct = *(struct termios *)data;
1621 lwkt_reltoken(&tty_token);
1624 *(struct termios *)data = *ct;
1625 lwkt_reltoken(&tty_token);
1628 *(int *)data = TTYDISC;
1629 lwkt_reltoken(&tty_token);
1632 bzero(data, sizeof(struct winsize));
1633 lwkt_reltoken(&tty_token);
1636 lwkt_reltoken(&tty_token);
1641 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1642 term = tp->t_termios;
1644 error = ttsetcompat(tp, &cmd, data, &term);
1646 lwkt_reltoken(&tty_token);
1650 data = (caddr_t)&term;
1652 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1654 struct termios *dt = (struct termios *)data;
1655 struct termios *lt = mynor & CALLOUT_MASK
1656 ? &com->lt_out : &com->lt_in;
1658 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
1659 | (dt->c_iflag & ~lt->c_iflag);
1660 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
1661 | (dt->c_oflag & ~lt->c_oflag);
1662 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
1663 | (dt->c_cflag & ~lt->c_cflag);
1664 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
1665 | (dt->c_lflag & ~lt->c_lflag);
1666 for (cc = 0; cc < NCCS; ++cc)
1667 if (lt->c_cc[cc] != 0)
1668 dt->c_cc[cc] = tp->t_cc[cc];
1669 if (lt->c_ispeed != 0)
1670 dt->c_ispeed = tp->t_ispeed;
1671 if (lt->c_ospeed != 0)
1672 dt->c_ospeed = tp->t_ospeed;
1674 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data,
1675 ap->a_fflag, ap->a_cred);
1676 if (error != ENOIOCTL) {
1677 lwkt_reltoken(&tty_token);
1681 error = ttioctl(tp, cmd, data, ap->a_fflag);
1682 disc_optim(tp, &tp->t_termios, com);
1683 if (error != ENOIOCTL) {
1685 lwkt_reltoken(&tty_token);
1691 outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
1693 cd_etc(com, CD1400_ETC_SENDBREAK);
1698 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1700 cd_etc(com, CD1400_ETC_STOPBREAK);
1704 (void)commctl(com, TIOCM_DTR, DMBIS);
1707 (void)commctl(com, TIOCM_DTR, DMBIC);
1710 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
1711 * changes get undone on the next call to comparam().
1714 (void)commctl(com, *(int *)data, DMSET);
1717 (void)commctl(com, *(int *)data, DMBIS);
1720 (void)commctl(com, *(int *)data, DMBIC);
1723 *(int *)data = commctl(com, 0, DMGET);
1726 /* must be root since the wait applies to following logins */
1727 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1730 lwkt_reltoken(&tty_token);
1733 com->dtr_wait = *(int *)data * hz / 100;
1736 *(int *)data = com->dtr_wait * 100 / hz;
1739 com->do_timestamp = TRUE;
1740 *(struct timeval *)data = com->timestamp;
1742 case TIOCDCDTIMESTAMP:
1743 com->do_dcd_timestamp = TRUE;
1744 *(struct timeval *)data = com->dcd_timestamp;
1748 lwkt_reltoken(&tty_token);
1752 lwkt_reltoken(&tty_token);
1757 siopoll(void *data, void *frame)
1761 lwkt_gettoken(&tty_token);
1765 if (com_events == 0) {
1766 lwkt_reltoken(&tty_token);
1770 for (unit = 0; unit < NSIO; ++unit) {
1775 com = com_addr(unit);
1781 * XXX forget any events related to closed devices
1782 * (actually never opened devices) so that we don't
1786 incc = com->iptr - com->ibuf;
1787 com->iptr = com->ibuf;
1788 if (com->state & CS_CHECKMSR) {
1789 incc += LOTS_OF_EVENTS;
1790 com->state &= ~CS_CHECKMSR;
1796 "sio%d: %d events for device with no tp\n",
1800 if (com->iptr != com->ibuf) {
1805 if (com->state & CS_CHECKMSR) {
1806 u_char delta_modem_status;
1809 delta_modem_status = com->last_modem_status
1810 ^ com->prev_modem_status;
1811 com->prev_modem_status = com->last_modem_status;
1812 com_events -= LOTS_OF_EVENTS;
1813 com->state &= ~CS_CHECKMSR;
1815 if (delta_modem_status & MSR_DCD)
1816 (*linesw[tp->t_line].l_modem)
1817 (tp, com->prev_modem_status & MSR_DCD);
1819 if (com->extra_state & CSE_ODONE) {
1821 com_events -= LOTS_OF_EVENTS;
1822 com->extra_state &= ~CSE_ODONE;
1824 if (!(com->state & CS_BUSY)) {
1825 tp->t_state &= ~TS_BUSY;
1828 if (com->etc != ETC_NONE) {
1829 if (com->etc == ETC_BREAK_ENDED)
1830 com->etc = ETC_NONE;
1834 if (com->state & CS_ODONE) {
1836 com_events -= LOTS_OF_EVENTS;
1837 com->state &= ~CS_ODONE;
1839 (*linesw[tp->t_line].l_start)(tp);
1841 if (com_events == 0)
1844 if (com_events >= LOTS_OF_EVENTS)
1846 lwkt_reltoken(&tty_token);
1850 comparam(struct tty *tp, struct termios *t)
1866 lwkt_gettoken(&tty_token);
1867 /* do historical conversions */
1868 if (t->c_ispeed == 0)
1869 t->c_ispeed = t->c_ospeed;
1871 unit = DEV_TO_UNIT(tp->t_dev);
1872 com = com_addr(unit);
1874 /* check requested parameters */
1875 cy_clock = CY_CLOCK(com->gfrcr_image);
1876 idivisor = comspeed(t->c_ispeed, cy_clock, &iprescaler);
1878 lwkt_reltoken(&tty_token);
1881 odivisor = comspeed(t->c_ospeed, cy_clock, &oprescaler);
1883 lwkt_reltoken(&tty_token);
1887 /* parameters are OK, convert them to the com struct and the device */
1890 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */
1892 (void)commctl(com, TIOCM_DTR, DMBIS);
1895 * This returns with interrupts disabled so that we can complete
1896 * the speed change atomically.
1898 (void) siosetwater(com, t->c_ispeed);
1900 /* XXX we don't actually change the speed atomically. */
1903 if (idivisor != 0) {
1904 cd_setreg(com, CD1400_RBPR, idivisor);
1905 cd_setreg(com, CD1400_RCOR, iprescaler);
1907 if (odivisor != 0) {
1908 cd_setreg(com, CD1400_TBPR, odivisor);
1909 cd_setreg(com, CD1400_TCOR, oprescaler);
1915 * transmitter enable (always set)
1918 opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
1919 | (cflag & CREAD ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
1920 if (opt != com->channel_control) {
1921 com->channel_control = opt;
1922 cd1400_channel_cmd(com, opt);
1926 /* set special chars */
1927 /* XXX if one is _POSIX_VDISABLE, can't use some others */
1928 if (t->c_cc[VSTOP] != _POSIX_VDISABLE)
1929 cd_setreg(com, CD1400_SCHR1, t->c_cc[VSTOP]);
1930 if (t->c_cc[VSTART] != _POSIX_VDISABLE)
1931 cd_setreg(com, CD1400_SCHR2, t->c_cc[VSTART]);
1932 if (t->c_cc[VINTR] != _POSIX_VDISABLE)
1933 cd_setreg(com, CD1400_SCHR3, t->c_cc[VINTR]);
1934 if (t->c_cc[VSUSP] != _POSIX_VDISABLE)
1935 cd_setreg(com, CD1400_SCHR4, t->c_cc[VSUSP]);
1939 * set channel option register 1 -
1946 if (cflag & PARENB) {
1948 opt |= CD1400_COR1_PARODD;
1949 opt |= CD1400_COR1_PARNORMAL;
1952 if (!(iflag & INPCK))
1953 opt |= CD1400_COR1_NOINPCK;
1956 if (cflag & CSTOPB) {
1958 opt |= CD1400_COR1_STOP2;
1961 switch (cflag & CSIZE) {
1964 opt |= CD1400_COR1_CS5;
1968 opt |= CD1400_COR1_CS6;
1972 opt |= CD1400_COR1_CS7;
1976 opt |= CD1400_COR1_CS8;
1980 if (opt != com->cor[0]) {
1981 cor_change |= CD1400_CCR_COR1;
1982 cd_setreg(com, CD1400_COR1, com->cor[0] = opt);
1986 * Set receive time-out period, normally to max(one char time, 5 ms).
1988 if (t->c_ispeed == 0)
1989 itimeout = cd_getreg(com, CD1400_RTPR);
1991 itimeout = (1000 * bits + t->c_ispeed - 1) / t->c_ispeed;
1997 if (itimeout < MIN_RTP)
2000 if (!(t->c_lflag & ICANON) && t->c_cc[VMIN] != 0 && t->c_cc[VTIME] != 0
2001 && t->c_cc[VTIME] * 10 > itimeout)
2002 itimeout = t->c_cc[VTIME] * 10;
2005 cd_setreg(com, CD1400_RTPR, itimeout);
2008 * set channel option register 2 -
2014 opt |= CD1400_COR2_IXANY;
2016 opt |= CD1400_COR2_IXOFF;
2018 #ifndef SOFT_CTS_OFLOW
2019 if (cflag & CCTS_OFLOW)
2020 opt |= CD1400_COR2_CCTS_OFLOW;
2023 if (opt != com->cor[1]) {
2024 cor_change |= CD1400_CCR_COR2;
2025 cd_setreg(com, CD1400_COR2, com->cor[1] = opt);
2030 * set channel option register 3 -
2031 * receiver FIFO interrupt threshold
2034 opt = RxFifoThreshold;
2036 if (t->c_lflag & ICANON)
2037 opt |= CD1400_COR3_SCD34; /* detect INTR & SUSP chars */
2039 /* detect and transparently handle START and STOP chars */
2040 opt |= CD1400_COR3_FCT | CD1400_COR3_SCD12;
2042 if (opt != com->cor[2]) {
2043 cor_change |= CD1400_CCR_COR3;
2044 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2047 /* notify the CD1400 if COR1-3 have changed */
2049 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | cor_change);
2052 * set channel option register 4 -
2055 * received exception processing
2059 opt |= CD1400_COR4_IGNCR;
2062 * we need a new ttyinput() for this, as we don't want to
2063 * have ICRNL && INLCR being done in both layers, or to have
2064 * synchronisation problems
2067 opt |= CD1400_COR4_ICRNL;
2069 opt |= CD1400_COR4_INLCR;
2072 opt |= CD1400_COR4_IGNBRK | CD1400_COR4_NOBRKINT;
2074 * The `-ignbrk -brkint parmrk' case is not handled by the hardware,
2075 * so only tell the hardware about -brkint if -parmrk.
2077 if (!(iflag & (BRKINT | PARMRK)))
2078 opt |= CD1400_COR4_NOBRKINT;
2080 /* XXX using this "intelligence" breaks reporting of overruns. */
2082 opt |= CD1400_COR4_PFO_DISCARD;
2085 opt |= CD1400_COR4_PFO_ESC;
2087 opt |= CD1400_COR4_PFO_NUL;
2090 opt |= CD1400_COR4_PFO_EXCEPTION;
2092 cd_setreg(com, CD1400_COR4, opt);
2095 * set channel option register 5 -
2099 opt |= CD1400_COR5_ISTRIP;
2100 if (t->c_iflag & IEXTEN)
2101 /* enable LNEXT (e.g. ctrl-v quoting) handling */
2102 opt |= CD1400_COR5_LNEXT;
2104 if (t->c_oflag & ONLCR)
2105 opt |= CD1400_COR5_ONLCR;
2106 if (t->c_oflag & OCRNL)
2107 opt |= CD1400_COR5_OCRNL;
2109 cd_setreg(com, CD1400_COR5, opt);
2112 * We always generate modem status change interrupts for CD changes.
2113 * Among other things, this is necessary to track TS_CARR_ON for
2114 * pstat to print even when the driver doesn't care. CD changes
2115 * should be rare so interrupts for them are not worth extra code to
2116 * avoid. We avoid interrupts for other modem status changes (except
2117 * for CTS changes when SOFT_CTS_OFLOW is configured) since this is
2118 * simplest and best.
2122 * set modem change option register 1
2123 * generate modem interrupts on which 1 -> 0 input transitions
2124 * also controls auto-DTR output flow-control, which we don't use
2126 opt = CD1400_MCOR1_CDzd;
2127 #ifdef SOFT_CTS_OFLOW
2128 if (cflag & CCTS_OFLOW)
2129 opt |= CD1400_MCOR1_CTSzd;
2131 cd_setreg(com, CD1400_MCOR1, opt);
2134 * set modem change option register 2
2135 * generate modem interrupts on specific 0 -> 1 input transitions
2137 opt = CD1400_MCOR2_CDod;
2138 #ifdef SOFT_CTS_OFLOW
2139 if (cflag & CCTS_OFLOW)
2140 opt |= CD1400_MCOR2_CTSod;
2142 cd_setreg(com, CD1400_MCOR2, opt);
2145 * XXX should have done this long ago, but there is too much state
2146 * to change all atomically.
2150 com->state &= ~CS_TTGO;
2151 if (!(tp->t_state & TS_TTSTOP))
2152 com->state |= CS_TTGO;
2153 if (cflag & CRTS_IFLOW) {
2154 com->state |= CS_RTS_IFLOW;
2156 * If CS_RTS_IFLOW just changed from off to on, the change
2157 * needs to be propagated to MCR_RTS. This isn't urgent,
2158 * so do it later by calling comstart() instead of repeating
2159 * a lot of code from comstart() here.
2161 } else if (com->state & CS_RTS_IFLOW) {
2162 com->state &= ~CS_RTS_IFLOW;
2164 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS
2165 * on here, since comstart() won't do it later.
2168 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2170 cd_setreg(com, com->mcr_rts_reg,
2171 com->mcr_image |= com->mcr_rts);
2176 * Set up state to handle output flow control.
2177 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2178 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2180 com->state |= CS_ODEVREADY;
2181 #ifdef SOFT_CTS_OFLOW
2182 com->state &= ~CS_CTS_OFLOW;
2183 if (cflag & CCTS_OFLOW) {
2184 com->state |= CS_CTS_OFLOW;
2185 if (!(com->last_modem_status & MSR_CTS))
2186 com->state &= ~CS_ODEVREADY;
2189 /* XXX shouldn't call functions while intrs are disabled. */
2190 disc_optim(tp, t, com);
2193 * Recover from fiddling with CS_TTGO. We used to call siointr1()
2194 * unconditionally, but that defeated the careful discarding of
2195 * stale input in sioopen().
2197 if (com->state >= (CS_BUSY | CS_TTGO))
2200 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2201 if (!(com->intr_enable & CD1400_SRER_TXRDY))
2202 cd_setreg(com, CD1400_SRER,
2204 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2205 | CD1400_SRER_TXRDY);
2207 if (com->intr_enable & CD1400_SRER_TXRDY)
2208 cd_setreg(com, CD1400_SRER,
2210 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2211 | CD1400_SRER_TXMPTY);
2217 if (com->ibufold != NULL) {
2218 kfree(com->ibufold, M_DEVBUF);
2219 com->ibufold = NULL;
2221 lwkt_reltoken(&tty_token);
2226 * NOTE: Must be called with tty_token held
2229 siosetwater(struct com_s *com, speed_t speed)
2236 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2239 * Make the buffer size large enough to handle a softtty interrupt
2240 * latency of about 2 ticks without loss of throughput or data
2241 * (about 3 ticks if input flow control is not used or not honoured,
2242 * but a bit less for CS5-CS7 modes).
2244 cp4ticks = speed / 10 / hz * 4;
2245 for (ibufsize = 128; ibufsize < cp4ticks;)
2247 if (ibufsize == com->ibufsize) {
2253 * Allocate input buffer. The extra factor of 2 in the size is
2254 * to allow for an error byte for each input byte.
2256 ibuf = kmalloc(2 * ibufsize, M_DEVBUF, M_WAITOK);
2258 /* Initialize non-critical variables. */
2259 com->ibufold = com->ibuf;
2260 com->ibufsize = ibufsize;
2263 tp->t_ififosize = 2 * ibufsize;
2264 tp->t_ispeedwat = (speed_t)-1;
2265 tp->t_ospeedwat = (speed_t)-1;
2269 * Read current input buffer, if any. Continue with interrupts
2273 if (com->iptr != com->ibuf)
2277 * Initialize critical variables, including input buffer watermarks.
2278 * The external device is asked to stop sending when the buffer
2279 * exactly reaches high water, or when the high level requests it.
2280 * The high level is notified immediately (rather than at a later
2281 * clock tick) when this watermark is reached.
2282 * The buffer size is chosen so the watermark should almost never
2284 * The low watermark is invisibly 0 since the buffer is always
2285 * emptied all at once.
2287 com->iptr = com->ibuf = ibuf;
2288 com->ibufend = ibuf + ibufsize;
2289 com->ierroff = ibufsize;
2290 com->ihighwater = ibuf + 3 * ibufsize / 4;
2295 comstart(struct tty *tp)
2303 unit = DEV_TO_UNIT(tp->t_dev);
2304 com = com_addr(unit);
2306 lwkt_gettoken(&tty_token);
2314 if (tp->t_state & TS_TTSTOP) {
2315 com->state &= ~CS_TTGO;
2316 if (com->intr_enable & CD1400_SRER_TXRDY)
2317 cd_setreg(com, CD1400_SRER,
2319 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2320 | CD1400_SRER_TXMPTY);
2322 com->state |= CS_TTGO;
2323 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)
2324 && !(com->intr_enable & CD1400_SRER_TXRDY))
2325 cd_setreg(com, CD1400_SRER,
2327 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2328 | CD1400_SRER_TXRDY);
2330 if (tp->t_state & TS_TBLOCK) {
2331 if (com->mcr_image & com->mcr_rts && com->state & CS_RTS_IFLOW)
2333 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2335 cd_setreg(com, com->mcr_rts_reg,
2336 com->mcr_image &= ~com->mcr_rts);
2339 if (!(com->mcr_image & com->mcr_rts)
2340 && com->iptr < com->ihighwater
2341 && com->state & CS_RTS_IFLOW)
2343 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2345 cd_setreg(com, com->mcr_rts_reg,
2346 com->mcr_image |= com->mcr_rts);
2350 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2352 lwkt_reltoken(&tty_token);
2356 if (tp->t_outq.c_cc != 0) {
2360 if (!com->obufs[0].l_queued) {
2364 com->obufs[0].l_tail
2365 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2367 com->obufs[0].l_next = NULL;
2368 com->obufs[0].l_queued = TRUE;
2370 if (com->state & CS_BUSY) {
2371 qp = com->obufq.l_next;
2372 while ((next = qp->l_next) != NULL)
2374 qp->l_next = &com->obufs[0];
2376 com->obufq.l_head = com->obufs[0].l_head;
2377 com->obufq.l_tail = com->obufs[0].l_tail;
2378 com->obufq.l_next = &com->obufs[0];
2379 com->state |= CS_BUSY;
2380 if (com->state >= (CS_BUSY | CS_TTGO
2382 cd_setreg(com, CD1400_SRER,
2385 & ~CD1400_SRER_TXMPTY)
2386 | CD1400_SRER_TXRDY);
2390 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2394 com->obufs[1].l_tail
2395 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2397 com->obufs[1].l_next = NULL;
2398 com->obufs[1].l_queued = TRUE;
2400 if (com->state & CS_BUSY) {
2401 qp = com->obufq.l_next;
2402 while ((next = qp->l_next) != NULL)
2404 qp->l_next = &com->obufs[1];
2406 com->obufq.l_head = com->obufs[1].l_head;
2407 com->obufq.l_tail = com->obufs[1].l_tail;
2408 com->obufq.l_next = &com->obufs[1];
2409 com->state |= CS_BUSY;
2410 if (com->state >= (CS_BUSY | CS_TTGO
2412 cd_setreg(com, CD1400_SRER,
2415 & ~CD1400_SRER_TXMPTY)
2416 | CD1400_SRER_TXRDY);
2420 tp->t_state |= TS_BUSY;
2428 if (com->state >= (CS_BUSY | CS_TTGO))
2429 siointr1(com); /* fake interrupt to start output */
2433 lwkt_reltoken(&tty_token);
2438 comstop(struct tty *tp, int rw)
2443 lwkt_gettoken(&tty_token);
2444 com = com_addr(DEV_TO_UNIT(tp->t_dev));
2448 com->obufs[0].l_queued = FALSE;
2449 com->obufs[1].l_queued = FALSE;
2450 if (com->extra_state & CSE_ODONE) {
2451 com_events -= LOTS_OF_EVENTS;
2452 com->extra_state &= ~CSE_ODONE;
2453 if (com->etc != ETC_NONE) {
2454 if (com->etc == ETC_BREAK_ENDED)
2455 com->etc = ETC_NONE;
2459 com->tp->t_state &= ~TS_BUSY;
2460 if (com->state & CS_ODONE)
2461 com_events -= LOTS_OF_EVENTS;
2462 com->state &= ~(CS_ODONE | CS_BUSY);
2465 /* XXX no way to reset only input fifo. */
2466 com_events -= (com->iptr - com->ibuf);
2467 com->iptr = com->ibuf;
2472 if (rw & FWRITE && com->etc == ETC_NONE)
2473 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
2475 lwkt_reltoken(&tty_token);
2479 * NOTE: Must be called with tty_token held
2482 commctl(struct com_s *com, int bits, int how)
2487 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2490 if (com->channel_control & CD1400_CCR_RCVEN)
2492 mcr = com->mcr_image;
2493 if (mcr & com->mcr_dtr)
2495 if (mcr & com->mcr_rts)
2496 /* XXX wired on for Cyclom-8Ys */
2500 * We must read the modem status from the hardware because
2501 * we don't generate modem status change interrupts for all
2502 * changes, so com->prev_modem_status is not guaranteed to
2503 * be up to date. This is safe, unlike for sio, because
2504 * reading the status register doesn't clear pending modem
2505 * status change interrupts.
2507 msr = cd_getreg(com, CD1400_MSVR2);
2516 /* XXX not connected except for Cyclom-16Y? */
2521 if (bits & TIOCM_DTR)
2522 mcr |= com->mcr_dtr;
2523 if (bits & TIOCM_RTS)
2524 mcr |= com->mcr_rts;
2528 com->mcr_image = mcr;
2529 cd_setreg(com, CD1400_MSVR1, mcr);
2530 cd_setreg(com, CD1400_MSVR2, mcr);
2533 com->mcr_image = mcr = com->mcr_image | mcr;
2534 cd_setreg(com, CD1400_MSVR1, mcr);
2535 cd_setreg(com, CD1400_MSVR2, mcr);
2538 com->mcr_image = mcr = com->mcr_image & ~mcr;
2539 cd_setreg(com, CD1400_MSVR1, mcr);
2540 cd_setreg(com, CD1400_MSVR2, mcr);
2548 * NOTE: Must be called with tty_token held
2557 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2559 * Set our timeout period to 1 second if no polled devices are open.
2560 * Otherwise set it to max(1/200, 1/hz).
2561 * Enable timeouts iff some device is open.
2563 callout_stop(&sio_timeout_handle);
2566 for (unit = 0; unit < NSIO; ++unit) {
2567 com = com_addr(unit);
2568 if (com != NULL && com->tp != NULL
2569 && com->tp->t_state & TS_ISOPEN) {
2572 if (com->poll || com->poll_output) {
2573 sio_timeout = hz > 200 ? hz / 200 : 1;
2580 sio_timeouts_until_log = hz / sio_timeout;
2581 callout_reset(&sio_timeout_handle, sio_timeout,
2584 /* Flush error messages, if any. */
2585 sio_timeouts_until_log = 1;
2587 callout_stop(&sio_timeout_handle);
2592 comwakeup(void *chan)
2597 lwkt_gettoken(&tty_token);
2598 callout_reset(&sio_timeout_handle, sio_timeout, comwakeup, NULL);
2602 * Recover from lost output interrupts.
2603 * Poll any lines that don't use interrupts.
2605 for (unit = 0; unit < NSIO; ++unit) {
2606 com = com_addr(unit);
2608 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2617 * Check for and log errors, but not too often.
2619 if (--sio_timeouts_until_log > 0) {
2620 lwkt_reltoken(&tty_token);
2623 sio_timeouts_until_log = hz / sio_timeout;
2624 for (unit = 0; unit < NSIO; ++unit) {
2627 com = com_addr(unit);
2630 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2635 delta = com->delta_error_counts[errnum];
2636 com->delta_error_counts[errnum] = 0;
2640 total = com->error_counts[errnum] += delta;
2641 log(LOG_ERR, "cy%d: %u more %s%s (total %lu)\n",
2642 unit, delta, error_desc[errnum],
2643 delta == 1 ? "" : "s", total);
2646 lwkt_reltoken(&tty_token);
2650 disc_optim(struct tty *tp, struct termios *t, struct com_s *com)
2652 #ifndef SOFT_HOTCHAR
2656 lwkt_gettoken(&tty_token);
2658 * XXX can skip a lot more cases if Smarts. Maybe
2659 * (IGNCR | ISTRIP | IXON) in c_iflag. But perhaps we
2660 * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2662 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2663 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2664 && (!(t->c_iflag & PARMRK)
2665 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2666 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2667 && linesw[tp->t_line].l_rint == ttyinput)
2668 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2670 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2671 com->hotchar = linesw[tp->t_line].l_hotchar;
2672 #ifndef SOFT_HOTCHAR
2673 opt = com->cor[2] & ~CD1400_COR3_SCD34;
2674 if (com->hotchar != 0) {
2675 cd_setreg(com, CD1400_SCHR3, com->hotchar);
2676 cd_setreg(com, CD1400_SCHR4, com->hotchar);
2677 opt |= CD1400_COR3_SCD34;
2679 if (opt != com->cor[2]) {
2680 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2681 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR3);
2684 lwkt_reltoken(&tty_token);
2688 /* standard line discipline input routine */
2690 cyinput(int c, struct tty *tp)
2692 /* XXX duplicate ttyinput(), but without the IXOFF/IXON/ISTRIP/IPARMRK
2693 * bits, as they are done by the CD1400. Hardly worth the effort,
2694 * given that high-throughput sessions are raw anyhow.
2700 comspeed(speed_t speed, u_long cy_clock, int *prescaler_io)
2710 if (speed < 0 || speed > 150000)
2713 /* determine which prescaler to use */
2714 for (prescaler_unit = 4, prescaler = 2048; prescaler_unit;
2715 prescaler_unit--, prescaler >>= 2) {
2716 if (cy_clock / prescaler / speed > 63)
2720 divider = (cy_clock / prescaler * 2 / speed + 1) / 2; /* round off */
2723 actual = cy_clock/prescaler/divider;
2725 /* 10 times error in percent: */
2726 error = ((actual - (long)speed) * 2000 / (long)speed + 1) / 2;
2728 /* 3.0% max error tolerance */
2729 if (error < -30 || error > 30)
2733 kprintf("prescaler = %d (%d)\n", prescaler, prescaler_unit);
2734 kprintf("divider = %d (%x)\n", divider, divider);
2735 kprintf("actual = %d\n", actual);
2736 kprintf("error = %d\n", error);
2739 *prescaler_io = prescaler_unit;
2744 * NOTE: Must be called with tty_token held
2747 cd1400_channel_cmd(struct com_s *com, int cmd)
2749 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2750 cd1400_channel_cmd_wait(com);
2751 cd_setreg(com, CD1400_CCR, cmd);
2752 cd1400_channel_cmd_wait(com);
2756 * NOTE: Must be called with tty_token held
2759 cd1400_channel_cmd_wait(struct com_s *com)
2761 struct timeval start;
2765 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2767 if (cd_getreg(com, CD1400_CCR) == 0)
2771 if (cd_getreg(com, CD1400_CCR) == 0)
2774 usec = 1000000 * (tv.tv_sec - start.tv_sec) +
2775 tv.tv_usec - start.tv_usec;
2778 "cy%d: channel command timeout (%ld usec)\n",
2786 * NOTE: Must be called with tty_token held
2789 cd_etc(struct com_s *com, int etc)
2791 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2793 * We can't change the hardware's ETC state while there are any
2794 * characters in the tx fifo, since those characters would be
2795 * interpreted as commands! Unputting characters from the fifo
2796 * is difficult, so we wait up to 12 character times for the fifo
2797 * to drain. The command will be delayed for up to 2 character
2798 * times for the tx to become empty. Unputting characters from
2799 * the tx holding and shift registers is impossible, so we wait
2800 * for the tx to become empty so that the command is sure to be
2801 * executed soon after we issue it.
2804 if (com->etc == etc) {
2808 if ((etc == CD1400_ETC_SENDBREAK
2809 && (com->etc == ETC_BREAK_STARTING
2810 || com->etc == ETC_BREAK_STARTED))
2811 || (etc == CD1400_ETC_STOPBREAK
2812 && (com->etc == ETC_BREAK_ENDING || com->etc == ETC_BREAK_ENDED
2813 || com->etc == ETC_NONE))) {
2818 cd_setreg(com, CD1400_SRER,
2820 = (com->intr_enable & ~CD1400_SRER_TXRDY) | CD1400_SRER_TXMPTY);
2823 while (com->etc == etc
2824 && tsleep(&com->etc, PCATCH, "cyetc", 0) == 0)
2829 * NOTE: Must be called with tty_token held
2832 cd_getreg(struct com_s *com, int reg)
2834 struct com_s *basecom;
2841 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2843 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2844 car = com->unit & CD1400_CAR_CHAN;
2845 cy_align = com->cy_align;
2846 iobase = com->iobase;
2850 if (basecom->car != car)
2851 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2852 val = cd_inb(iobase, reg, cy_align);
2859 * NOTE: Must be called with tty_token held
2862 cd_setreg(struct com_s *com, int reg, int val)
2864 struct com_s *basecom;
2870 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2872 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2873 car = com->unit & CD1400_CAR_CHAN;
2874 cy_align = com->cy_align;
2875 iobase = com->iobase;
2879 if (basecom->car != car)
2880 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2881 cd_outb(iobase, reg, cy_align, val);
2896 com = com_addr(unit);
2897 kprintf("info for channel %d\n", unit);
2898 kprintf("------------------\n");
2899 kprintf("total cyclom service probes:\t%d\n", cy_svrr_probes);
2900 kprintf("calls to upper layer:\t\t%d\n", cy_timeouts);
2903 lwkt_gettoken(&tty_token);
2904 iobase = com->iobase;
2906 kprintf("cd1400 base address:\\tt%p\n", iobase);
2907 kprintf("saved channel_control:\t\t0x%02x\n", com->channel_control);
2908 kprintf("saved cor1-3:\t\t\t0x%02x 0x%02x 0x%02x\n",
2909 com->cor[0], com->cor[1], com->cor[2]);
2910 kprintf("service request enable reg:\t0x%02x (0x%02x cached)\n",
2911 cd_getreg(com, CD1400_SRER), com->intr_enable);
2912 kprintf("service request register:\t0x%02x\n",
2913 cd_inb(iobase, CD1400_SVRR, com->cy_align));
2914 kprintf("modem status:\t\t\t0x%02x (0x%02x cached)\n",
2915 cd_getreg(com, CD1400_MSVR2), com->prev_modem_status);
2916 kprintf("rx/tx/mdm interrupt registers:\t0x%02x 0x%02x 0x%02x\n",
2917 cd_inb(iobase, CD1400_RIR, com->cy_align),
2918 cd_inb(iobase, CD1400_TIR, com->cy_align),
2919 cd_inb(iobase, CD1400_MIR, com->cy_align));
2921 kprintf("com state:\t\t\t0x%02x\n", com->state);
2922 kprintf("calls to comstart():\t\t%d (%d useful)\n",
2923 com->start_count, com->start_real);
2924 kprintf("rx buffer chars free:\t\t%d\n", com->iptr - com->ibuf);
2926 if (com->obufs[0].l_queued)
2927 ocount += com->obufs[0].l_tail - com->obufs[0].l_head;
2928 if (com->obufs[1].l_queued)
2929 ocount += com->obufs[1].l_tail - com->obufs[1].l_head;
2930 kprintf("tx buffer chars:\t\t%u\n", ocount);
2931 kprintf("received chars:\t\t\t%d\n", com->bytes_in);
2932 kprintf("received exceptions:\t\t%d\n", com->recv_exception);
2933 kprintf("modem signal deltas:\t\t%d\n", com->mdm);
2934 kprintf("transmitted chars:\t\t%d\n", com->bytes_out);
2938 kprintf("tty state:\t\t\t0x%08x\n", tp->t_state);
2940 "upper layer queue lengths:\t%d raw, %d canon, %d output\n",
2941 tp->t_rawq.c_cc, tp->t_canq.c_cc, tp->t_outq.c_cc);
2943 kprintf("tty state:\t\t\tclosed\n");
2945 lwkt_reltoken(&tty_token);
2947 #endif /* CyDebug */