cy.c revision 67584
1/*-
2 * cyclades cyclom-y serial driver
3 *	Andrew Herbert <andrew@werple.apana.org.au>, 17 August 1993
4 *
5 * Copyright (c) 1993 Andrew Herbert.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
18 *
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.
29 *
30 * $FreeBSD: head/sys/dev/cy/cy.c 67584 2000-10-25 21:13:42Z jhb $
31 */
32
33#include "opt_compat.h"
34#include "cy.h"
35
36/*
37 * TODO:
38 * Atomic COR change.
39 * Consoles.
40 */
41
42/*
43 * Temporary compile-time configuration options.
44 */
45#define	RxFifoThreshold	(CD1400_RX_FIFO_SIZE / 2)
46			/* Number of chars in the receiver FIFO before an
47			 * an interrupt is generated.  Should depend on
48			 * line speed.  Needs to be about 6 on a 486DX33
49			 * for 4 active ports at 115200 bps.  Why doesn't
50			 * 10 work?
51			 */
52#define	PollMode	/* Use polling-based irq service routine, not the
53			 * hardware svcack lines.  Must be defined for
54			 * Cyclom-16Y boards.  Less efficient for Cyclom-8Ys,
55			 * and stops 4 * 115200 bps from working.
56			 */
57#undef	Smarts		/* Enable slightly more CD1400 intelligence.  Mainly
58			 * the output CR/LF processing, plus we can avoid a
59			 * few checks usually done in ttyinput().
60			 *
61			 * XXX not fully implemented, and not particularly
62			 * worthwhile.
63			 */
64#undef	CyDebug		/* Include debugging code (not very expensive). */
65
66/* These will go away. */
67#undef	SOFT_CTS_OFLOW
68#define	SOFT_HOTCHAR
69
70#include <sys/param.h>
71#include <sys/systm.h>
72#include <sys/tty.h>
73#include <sys/proc.h>
74#include <sys/conf.h>
75#include <sys/dkstat.h>
76#include <sys/fcntl.h>
77#include <sys/bus.h>
78#include <sys/interrupt.h>
79#include <sys/ipl.h>
80#include <sys/kernel.h>
81#include <sys/malloc.h>
82#include <sys/syslog.h>
83#include <machine/lock.h>
84#include <machine/psl.h>
85
86#include <i386/isa/isa_device.h>
87#include <i386/isa/cyreg.h>
88#include <i386/isa/ic/cd1400.h>
89
90#ifndef COMPAT_OLDISA
91#error "The cy device requires the old isa compatibility shims"
92#endif
93
94/*
95 * Dictionary so that I can name everything *sio* or *com* to compare with
96 * sio.c.  There is also lots of ugly formatting and unnecessary ifdefs to
97 * simplify the comparision.  These will go away.
98 */
99#define	LSR_BI		CD1400_RDSR_BREAK
100#define	LSR_FE		CD1400_RDSR_FE
101#define	LSR_OE		CD1400_RDSR_OE
102#define	LSR_PE		CD1400_RDSR_PE
103#define	MCR_DTR		CD1400_MSVR2_DTR
104#define	MCR_RTS		CD1400_MSVR1_RTS
105#define	MSR_CTS		CD1400_MSVR2_CTS
106#define	MSR_DCD		CD1400_MSVR2_CD
107#define	MSR_DSR		CD1400_MSVR2_DSR
108#define	MSR_RI		CD1400_MSVR2_RI
109#define	NSIO		(NCY * CY_MAX_PORTS)
110#define	comconsole	cyconsole
111#define	comdefaultrate	cydefaultrate
112#define	com_events	cy_events
113#define	comhardclose	cyhardclose
114#define	commctl		cymctl
115#define	comparam	cyparam
116#define	comspeed	cyspeed
117#define	comstart	cystart
118#define	comwakeup	cywakeup
119#define	nsio_tty	ncy_tty
120#define	p_com_addr	p_cy_addr
121#define	sioattach	cyattach
122#define	sioclose	cyclose
123#define	siodriver	cydriver
124#define	siodtrwakeup	cydtrwakeup
125#define	sioinput	cyinput
126#define	siointr		cyintr
127#define	siointr1	cyintr1
128#define	sioioctl	cyioctl
129#define	sioopen		cyopen
130#define	siopoll		cypoll
131#define	sioprobe	cyprobe
132#define	siosettimeout	cysettimeout
133#define	siosetwater	cysetwater
134#define	comstop		cystop
135#define	siowrite	cywrite
136#define	sio_irec	cy_irec
137#define	sio_timeout	cy_timeout
138#define	sio_timeout_handle cy_timeout_handle
139#define	sio_timeouts_until_log	cy_timeouts_until_log
140#define	sio_tty		cy_tty
141
142#define	CY_MAX_PORTS		(CD1400_NO_OF_CHANNELS * CY_MAX_CD1400s)
143
144/* We encode the cyclom unit number (cyu) in spare bits in the IVR's. */
145#define	CD1400_xIVR_CHAN_SHIFT	3
146#define	CD1400_xIVR_CHAN	0x1F
147
148/*
149 * ETC states.  com->etc may also contain a hardware ETC command value,
150 * meaning that execution of that command is pending.
151 */
152#define	ETC_NONE		0	/* we depend on bzero() setting this */
153#define	ETC_BREAK_STARTING	1
154#define	ETC_BREAK_STARTED	2
155#define	ETC_BREAK_ENDING	3
156#define	ETC_BREAK_ENDED		4
157
158#define	LOTS_OF_EVENTS	64	/* helps separate urgent events from input */
159
160#define	CALLOUT_MASK		0x80
161#define	CONTROL_MASK		0x60
162#define	CONTROL_INIT_STATE	0x20
163#define	CONTROL_LOCK_STATE	0x40
164#define	DEV_TO_UNIT(dev)	(MINOR_TO_UNIT(minor(dev)))
165#define	MINOR_MAGIC_MASK	(CALLOUT_MASK | CONTROL_MASK)
166#define	MINOR_TO_UNIT(mynor)	(((mynor) >> 16) * CY_MAX_PORTS \
167				 | (((mynor) & 0xff) & ~MINOR_MAGIC_MASK))
168
169/*
170 * com state bits.
171 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
172 * than the other bits so that they can be tested as a group without masking
173 * off the low bits.
174 *
175 * The following com and tty flags correspond closely:
176 *	CS_BUSY		= TS_BUSY (maintained by comstart(), siopoll() and
177 *				   comstop())
178 *	CS_TTGO		= ~TS_TTSTOP (maintained by comparam() and comstart())
179 *	CS_CTS_OFLOW	= CCTS_OFLOW (maintained by comparam())
180 *	CS_RTS_IFLOW	= CRTS_IFLOW (maintained by comparam())
181 * TS_FLUSH is not used.
182 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
183 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
184 */
185#define	CS_BUSY		0x80	/* output in progress */
186#define	CS_TTGO		0x40	/* output not stopped by XOFF */
187#define	CS_ODEVREADY	0x20	/* external device h/w ready (CTS) */
188#define	CS_CHECKMSR	1	/* check of MSR scheduled */
189#define	CS_CTS_OFLOW	2	/* use CTS output flow control */
190#define	CS_DTR_OFF	0x10	/* DTR held off */
191#define	CS_ODONE	4	/* output completed */
192#define	CS_RTS_IFLOW	8	/* use RTS input flow control */
193#define	CSE_ODONE	1	/* output transmitted */
194
195static	char const * const	error_desc[] = {
196#define	CE_OVERRUN			0
197	"silo overflow",
198#define	CE_INTERRUPT_BUF_OVERFLOW	1
199	"interrupt-level buffer overflow",
200#define	CE_TTY_BUF_OVERFLOW		2
201	"tty-level buffer overflow",
202};
203
204#define	CE_NTYPES			3
205#define	CE_RECORD(com, errnum)		(++(com)->delta_error_counts[errnum])
206
207/* types.  XXX - should be elsewhere */
208typedef u_char	bool_t;		/* boolean */
209typedef u_char volatile *cy_addr;
210
211/* queue of linear buffers */
212struct lbq {
213	u_char	*l_head;	/* next char to process */
214	u_char	*l_tail;	/* one past the last char to process */
215	struct lbq *l_next;	/* next in queue */
216	bool_t	l_queued;	/* nonzero if queued */
217};
218
219/* com device structure */
220struct com_s {
221	u_char	state;		/* miscellaneous flag bits */
222	bool_t  active_out;	/* nonzero if the callout device is open */
223#if 0
224	u_char	cfcr_image;	/* copy of value written to CFCR */
225#endif
226	u_char	etc;		/* pending Embedded Transmit Command */
227	u_char	extra_state;	/* more flag bits, separate for order trick */
228#if 0
229	u_char	fifo_image;	/* copy of value written to FIFO */
230#endif
231	u_char	gfrcr_image;	/* copy of value read from GFRCR */
232#if 0
233	bool_t	hasfifo;	/* nonzero for 16550 UARTs */
234	bool_t	loses_outints;	/* nonzero if device loses output interrupts */
235#endif
236	u_char	mcr_dtr;	/* MCR bit that is wired to DTR */
237	u_char	mcr_image;	/* copy of value written to MCR */
238	u_char	mcr_rts;	/* MCR bit that is wired to RTS */
239#if 0
240#ifdef COM_MULTIPORT
241	bool_t	multiport;	/* is this unit part of a multiport device? */
242#endif /* COM_MULTIPORT */
243	bool_t	no_irq;		/* nonzero if irq is not attached */
244	bool_t	poll;		/* nonzero if polling is required */
245	bool_t	poll_output;	/* nonzero if polling for output is required */
246#endif
247	int	unit;		/* unit	number */
248	int	dtr_wait;	/* time to hold DTR down on close (* 1/hz) */
249#if 0
250	u_int	tx_fifo_size;
251#endif
252	u_int	wopeners;	/* # processes waiting for DCD in open() */
253
254	/*
255	 * The high level of the driver never reads status registers directly
256	 * because there would be too many side effects to handle conveniently.
257	 * Instead, it reads copies of the registers stored here by the
258	 * interrupt handler.
259	 */
260	u_char	last_modem_status;	/* last MSR read by intr handler */
261	u_char	prev_modem_status;	/* last MSR handled by high level */
262
263	u_char	hotchar;	/* ldisc-specific char to be handled ASAP */
264	u_char	*ibuf;		/* start of input buffer */
265	u_char	*ibufend;	/* end of input buffer */
266	u_char	*ibufold;	/* old input buffer, to be freed */
267	u_char	*ihighwater;	/* threshold in input buffer */
268	u_char	*iptr;		/* next free spot in input buffer */
269	int	ibufsize;	/* size of ibuf (not include error bytes) */
270	int	ierroff;	/* offset of error bytes in ibuf */
271
272	struct lbq	obufq;	/* head of queue of output buffers */
273	struct lbq	obufs[2];	/* output buffers */
274
275	int	cy_align;	/* index for register alignment */
276	cy_addr	cy_iobase;	/* base address of this port's cyclom */
277	cy_addr	iobase;		/* base address of this port's cd1400 */
278	int	mcr_rts_reg;	/* cd1400 reg number of reg holding mcr_rts */
279
280	struct tty	*tp;	/* cross reference */
281
282	/* Initial state. */
283	struct termios	it_in;	/* should be in struct tty */
284	struct termios	it_out;
285
286	/* Lock state. */
287	struct termios	lt_in;	/* should be in struct tty */
288	struct termios	lt_out;
289
290	bool_t	do_timestamp;
291	bool_t	do_dcd_timestamp;
292	struct timeval	timestamp;
293	struct timeval	dcd_timestamp;
294
295	u_long	bytes_in;	/* statistics */
296	u_long	bytes_out;
297	u_int	delta_error_counts[CE_NTYPES];
298	u_long	error_counts[CE_NTYPES];
299
300	u_int	recv_exception;	/* exception chars received */
301	u_int	mdm;		/* modem signal changes */
302#ifdef CyDebug
303	u_int	start_count;	/* no. of calls to comstart() */
304	u_int	start_real;	/* no. of calls that did something */
305#endif
306	u_char	car;		/* CD1400 CAR shadow (if first unit in cd) */
307	u_char	channel_control;/* CD1400 CCR control command shadow */
308	u_char	cor[3];		/* CD1400 COR1-3 shadows */
309	u_char	intr_enable;	/* CD1400 SRER shadow */
310
311	/*
312	 * Data area for output buffers.  Someday we should build the output
313	 * buffer queue without copying data.
314	 */
315	u_char	obuf1[256];
316	u_char	obuf2[256];
317};
318
319/* PCI driver entry point. */
320int	cyattach_common		__P((cy_addr cy_iobase, int cy_align));
321ointhand2_t	siointr;
322
323static	int	cy_units	__P((cy_addr cy_iobase, int cy_align));
324static	int	sioattach	__P((struct isa_device *dev));
325static	void	cd1400_channel_cmd __P((struct com_s *com, int cmd));
326static	void	cd1400_channel_cmd_wait __P((struct com_s *com));
327static	void	cd_etc		__P((struct com_s *com, int etc));
328static	int	cd_getreg	__P((struct com_s *com, int reg));
329static	void	cd_setreg	__P((struct com_s *com, int reg, int val));
330static	timeout_t siodtrwakeup;
331static	void	comhardclose	__P((struct com_s *com));
332static	void	sioinput	__P((struct com_s *com));
333#if 0
334static	void	siointr1	__P((struct com_s *com));
335#endif
336static	int	commctl		__P((struct com_s *com, int bits, int how));
337static	int	comparam	__P((struct tty *tp, struct termios *t));
338static	void	siopoll		__P((void *arg));
339static	int	sioprobe	__P((struct isa_device *dev));
340static	void	siosettimeout	__P((void));
341static	int	siosetwater	__P((struct com_s *com, speed_t speed));
342static	int	comspeed	__P((speed_t speed, u_long cy_clock,
343				     int *prescaler_io));
344static	void	comstart	__P((struct tty *tp));
345static	void	comstop		__P((struct tty *tp, int rw));
346static	timeout_t comwakeup;
347static	void	disc_optim	__P((struct tty	*tp, struct termios *t,
348				     struct com_s *com));
349
350#ifdef CyDebug
351void	cystatus	__P((int unit));
352#endif
353
354static char driver_name[] = "cy";
355
356/* table and macro for fast conversion from a unit number to its com struct */
357static	struct com_s	*p_com_addr[NSIO];
358#define	com_addr(unit)	(p_com_addr[unit])
359
360struct isa_driver	siodriver = {
361	INTR_TYPE_TTY | INTR_FAST,
362	sioprobe,
363	sioattach,
364	driver_name
365};
366COMPAT_ISA_DRIVER(cy, cydriver);	/* XXX */
367
368static	d_open_t	sioopen;
369static	d_close_t	sioclose;
370static	d_write_t	siowrite;
371static	d_ioctl_t	sioioctl;
372
373#define	CDEV_MAJOR	48
374static struct cdevsw sio_cdevsw = {
375	/* open */	sioopen,
376	/* close */	sioclose,
377	/* read */	ttyread,
378	/* write */	siowrite,
379	/* ioctl */	sioioctl,
380	/* poll */	ttypoll,
381	/* mmap */	nommap,
382	/* strategy */	nostrategy,
383	/* name */	driver_name,
384	/* maj */	CDEV_MAJOR,
385	/* dump */	nodump,
386	/* psize */	nopsize,
387	/* flags */	D_TTY,
388	/* bmaj */	-1
389};
390
391static	int	comconsole = -1;
392static	speed_t	comdefaultrate = TTYDEF_SPEED;
393static	u_int	com_events;	/* input chars + weighted output completions */
394static	struct	intrhand *sio_ih;
395static	int	sio_timeout;
396static	int	sio_timeouts_until_log;
397static	struct	callout_handle sio_timeout_handle
398    = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle);
399#if 0 /* XXX */
400static struct tty	*sio_tty[NSIO];
401#else
402static struct tty	sio_tty[NSIO];
403#endif
404static	const int	nsio_tty = NSIO;
405
406#ifdef CyDebug
407static	u_int	cd_inbs;
408static	u_int	cy_inbs;
409static	u_int	cd_outbs;
410static	u_int	cy_outbs;
411static	u_int	cy_svrr_probes;
412static	u_int	cy_timeouts;
413#endif
414
415static	int	cy_chip_offset[] = {
416	0x0000, 0x0400, 0x0800, 0x0c00, 0x0200, 0x0600, 0x0a00, 0x0e00,
417};
418static	int	cy_nr_cd1400s[NCY];
419static	int	cy_total_devices;
420#undef	RxFifoThreshold
421static	int	volatile RxFifoThreshold = (CD1400_RX_FIFO_SIZE / 2);
422
423static int
424sioprobe(dev)
425	struct isa_device	*dev;
426{
427	cy_addr	iobase;
428
429	iobase = (cy_addr)dev->id_maddr;
430
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 */
434
435	/* this is needed to get the board out of reset */
436	cy_outb(iobase, CY_CLEAR_INTR, 0, 0);
437	DELAY(500);
438
439	return (cy_units(iobase, 0) == 0 ? 0 : -1);
440}
441
442static int
443cy_units(cy_iobase, cy_align)
444	cy_addr	cy_iobase;
445	int	cy_align;
446{
447	int	cyu;
448	u_char	firmware_version;
449	int	i;
450	cy_addr	iobase;
451
452	for (cyu = 0; cyu < CY_MAX_CD1400s; ++cyu) {
453		iobase = cy_iobase + (cy_chip_offset[cyu] << cy_align);
454
455		/* wait for chip to become ready for new command */
456		for (i = 0; i < 10; i++) {
457			DELAY(50);
458			if (!cd_inb(iobase, CD1400_CCR, cy_align))
459				break;
460		}
461
462		/* clear the GFRCR register */
463		cd_outb(iobase, CD1400_GFRCR, cy_align, 0);
464
465		/* issue a reset command */
466		cd_outb(iobase, CD1400_CCR, cy_align,
467			CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
468
469		/* wait for the CD1400 to initialize itself */
470		for (i = 0; i < 200; i++) {
471			DELAY(50);
472
473			/* retrieve firmware version */
474			firmware_version = cd_inb(iobase, CD1400_GFRCR,
475						  cy_align);
476			if ((firmware_version & 0xf0) == 0x40)
477				break;
478		}
479
480		/*
481		 * Anything in the 0x40-0x4F range is fine.
482		 * If one CD1400 is bad then we don't support higher
483		 * numbered good ones on this board.
484		 */
485		if ((firmware_version & 0xf0) != 0x40)
486			break;
487	}
488	return (cyu);
489}
490
491static int
492sioattach(isdp)
493	struct isa_device	*isdp;
494{
495	int	adapter;
496
497	adapter = cyattach_common((cy_addr) isdp->id_maddr, 0);
498	if (adapter < 0)
499		return (0);
500
501	/*
502	 * XXX
503	 * This kludge is to allow ISA/PCI device specifications in the
504	 * kernel config file to be in any order.
505	 */
506	if (isdp->id_unit != adapter) {
507		printf("cy%d: attached as cy%d\n", isdp->id_unit, adapter);
508		isdp->id_unit = adapter;	/* XXX */
509	}
510	isdp->id_ointr = siointr;
511	/* isdp->id_ri_flags |= RI_FAST; XXX unimplemented - use newbus! */
512	return (1);
513}
514
515int
516cyattach_common(cy_iobase, cy_align)
517	cy_addr	cy_iobase;
518	int	cy_align;
519{
520	int	adapter;
521	int	cyu;
522	u_char	firmware_version;
523	cy_addr	iobase;
524	int	ncyu;
525	int	unit;
526
527	adapter = cy_total_devices;
528	if ((u_int)adapter >= NCY) {
529		printf(
530	"cy%d: can't attach adapter: insufficient cy devices configured\n",
531		       adapter);
532		return (-1);
533	}
534	ncyu = cy_units(cy_iobase, cy_align);
535	if (ncyu == 0)
536		return (-1);
537	cy_nr_cd1400s[adapter] = ncyu;
538	cy_total_devices++;
539
540	unit = adapter * CY_MAX_PORTS;
541	for (cyu = 0; cyu < ncyu; ++cyu) {
542		int	cdu;
543
544		iobase = (cy_addr) (cy_iobase
545				    + (cy_chip_offset[cyu] << cy_align));
546		firmware_version = cd_inb(iobase, CD1400_GFRCR, cy_align);
547
548		/* Set up a receive timeout period of than 1+ ms. */
549		cd_outb(iobase, CD1400_PPR, cy_align,
550			howmany(CY_CLOCK(firmware_version)
551				/ CD1400_PPR_PRESCALER, 1000));
552
553		for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; ++cdu, ++unit) {
554			struct com_s	*com;
555			int		s;
556
557	com = malloc(sizeof *com, M_DEVBUF, M_NOWAIT);
558	if (com == NULL)
559		break;
560	bzero(com, sizeof *com);
561	com->unit = unit;
562			com->gfrcr_image = firmware_version;
563			if (CY_RTS_DTR_SWAPPED(firmware_version)) {
564				com->mcr_dtr = MCR_RTS;
565				com->mcr_rts = MCR_DTR;
566				com->mcr_rts_reg = CD1400_MSVR2;
567			} else {
568				com->mcr_dtr = MCR_DTR;
569				com->mcr_rts = MCR_RTS;
570				com->mcr_rts_reg = CD1400_MSVR1;
571			}
572	com->dtr_wait = 3 * hz;
573	com->obufs[0].l_head = com->obuf1;
574	com->obufs[1].l_head = com->obuf2;
575
576			com->cy_align = cy_align;
577			com->cy_iobase = cy_iobase;
578	com->iobase = iobase;
579			com->car = ~CD1400_CAR_CHAN;
580
581	/*
582	 * We don't use all the flags from <sys/ttydefaults.h> since they
583	 * are only relevant for logins.  It's important to have echo off
584	 * initially so that the line doesn't start blathering before the
585	 * echo flag can be turned off.
586	 */
587	com->it_in.c_iflag = 0;
588	com->it_in.c_oflag = 0;
589	com->it_in.c_cflag = TTYDEF_CFLAG;
590	com->it_in.c_lflag = 0;
591	if (unit == comconsole) {
592		com->it_in.c_iflag = TTYDEF_IFLAG;
593		com->it_in.c_oflag = TTYDEF_OFLAG;
594		com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
595		com->it_in.c_lflag = TTYDEF_LFLAG;
596		com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
597	}
598	if (siosetwater(com, com->it_in.c_ispeed) != 0) {
599		free(com, M_DEVBUF);
600		return (0);
601	}
602	termioschars(&com->it_in);
603	com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
604	com->it_out = com->it_in;
605
606	s = spltty();
607	com_addr(unit) = com;
608	splx(s);
609
610	if (sio_ih == NULL) {
611		cdevsw_add(&sio_cdevsw);
612		sio_ih = sinthand_add("tty:sio", &tty_ithd, siopoll, NULL,
613		    SWI_TTY, 0);
614	}
615	make_dev(&sio_cdevsw, unit,
616		UID_ROOT, GID_WHEEL, 0600, "ttyc%r%r", adapter,
617		unit % CY_MAX_PORTS);
618	make_dev(&sio_cdevsw, unit | CONTROL_INIT_STATE,
619		UID_ROOT, GID_WHEEL, 0600, "ttyic%r%r", adapter,
620		unit % CY_MAX_PORTS);
621	make_dev(&sio_cdevsw, unit | CONTROL_LOCK_STATE,
622		UID_ROOT, GID_WHEEL, 0600, "ttylc%r%r", adapter,
623		unit % CY_MAX_PORTS);
624	make_dev(&sio_cdevsw, unit | CALLOUT_MASK,
625		UID_UUCP, GID_DIALER, 0660, "cuac%r%r", adapter,
626		unit % CY_MAX_PORTS);
627	make_dev(&sio_cdevsw, unit | CALLOUT_MASK | CONTROL_INIT_STATE,
628		UID_UUCP, GID_DIALER, 0660, "cuaic%r%r", adapter,
629		unit % CY_MAX_PORTS);
630	make_dev(&sio_cdevsw, unit | CALLOUT_MASK | CONTROL_LOCK_STATE,
631		UID_UUCP, GID_DIALER, 0660, "cualc%r%r", adapter,
632		unit % CY_MAX_PORTS);
633		}
634	}
635
636	/* ensure an edge for the next interrupt */
637	cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
638
639	return (adapter);
640}
641
642static int
643sioopen(dev, flag, mode, p)
644	dev_t		dev;
645	int		flag;
646	int		mode;
647	struct proc	*p;
648{
649	struct com_s	*com;
650	int		error;
651	int		mynor;
652	int		s;
653	struct tty	*tp;
654	int		unit;
655	int		intrsave;
656
657	mynor = minor(dev);
658	unit = MINOR_TO_UNIT(mynor);
659	if ((u_int) unit >= NSIO || (com = com_addr(unit)) == NULL)
660		return (ENXIO);
661	if (mynor & CONTROL_MASK)
662		return (0);
663#if 0 /* XXX */
664	tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
665#else
666	tp = com->tp = &sio_tty[unit];
667#endif
668	dev->si_tty = tp;
669	s = spltty();
670	/*
671	 * We jump to this label after all non-interrupted sleeps to pick
672	 * up any changes of the device state.
673	 */
674open_top:
675	while (com->state & CS_DTR_OFF) {
676		error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "cydtr", 0);
677		if (error != 0)
678			goto out;
679	}
680	if (tp->t_state & TS_ISOPEN) {
681		/*
682		 * The device is open, so everything has been initialized.
683		 * Handle conflicts.
684		 */
685		if (mynor & CALLOUT_MASK) {
686			if (!com->active_out) {
687				error = EBUSY;
688				goto out;
689			}
690		} else {
691			if (com->active_out) {
692				if (flag & O_NONBLOCK) {
693					error = EBUSY;
694					goto out;
695				}
696				error =	tsleep(&com->active_out,
697					       TTIPRI | PCATCH, "cybi", 0);
698				if (error != 0)
699					goto out;
700				goto open_top;
701			}
702		}
703		if (tp->t_state & TS_XCLUDE &&
704		    suser(p)) {
705			error = EBUSY;
706			goto out;
707		}
708	} else {
709		/*
710		 * The device isn't open, so there are no conflicts.
711		 * Initialize it.  Initialization is done twice in many
712		 * cases: to preempt sleeping callin opens if we are
713		 * callout, and to complete a callin open after DCD rises.
714		 */
715		tp->t_oproc = comstart;
716		tp->t_stop = comstop;
717		tp->t_param = comparam;
718		tp->t_dev = dev;
719		tp->t_termios = mynor & CALLOUT_MASK
720				? com->it_out : com->it_in;
721
722		/* Encode per-board unit in LIVR for access in intr routines. */
723		cd_setreg(com, CD1400_LIVR,
724			  (unit & CD1400_xIVR_CHAN) << CD1400_xIVR_CHAN_SHIFT);
725
726		(void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
727#if 0
728		com->poll = com->no_irq;
729		com->poll_output = com->loses_outints;
730#endif
731		++com->wopeners;
732		error = comparam(tp, &tp->t_termios);
733		--com->wopeners;
734		if (error != 0)
735			goto out;
736#if 0
737		if (com->hasfifo) {
738			/*
739			 * (Re)enable and flush fifos.
740			 *
741			 * Certain SMC chips cause problems if the fifos
742			 * are enabled while input is ready.  Turn off the
743			 * fifo if necessary to clear the input.  We test
744			 * the input ready bit after enabling the fifos
745			 * since we've already enabled them in comparam()
746			 * and to handle races between enabling and fresh
747			 * input.
748			 */
749			while (TRUE) {
750				outb(iobase + com_fifo,
751				     FIFO_RCV_RST | FIFO_XMT_RST
752				     | com->fifo_image);
753				DELAY(100);
754				if (!(inb(com->line_status_port) & LSR_RXRDY))
755					break;
756				outb(iobase + com_fifo, 0);
757				DELAY(100);
758				(void) inb(com->data_port);
759			}
760		}
761
762		intrsave = save_intr();
763		disable_intr();
764		COM_LOCK();
765		(void) inb(com->line_status_port);
766		(void) inb(com->data_port);
767		com->prev_modem_status = com->last_modem_status
768		    = inb(com->modem_status_port);
769		outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS
770				       | IER_EMSC);
771		COM_UNLOCK();
772		restore_intr(intrsave);
773#else /* !0 */
774		/*
775		 * Flush fifos.  This requires a full channel reset which
776		 * also disables the transmitter and receiver.  Recover
777		 * from this.
778		 */
779		cd1400_channel_cmd(com,
780				   CD1400_CCR_CMDRESET | CD1400_CCR_CHANRESET);
781		cd1400_channel_cmd(com, com->channel_control);
782
783		intrsave = save_intr();
784		disable_intr();
785		COM_LOCK();
786		com->prev_modem_status = com->last_modem_status
787		    = cd_getreg(com, CD1400_MSVR2);
788		cd_setreg(com, CD1400_SRER,
789			  com->intr_enable
790			  = CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
791		COM_UNLOCK();
792		restore_intr(intrsave);
793#endif /* 0 */
794		/*
795		 * Handle initial DCD.  Callout devices get a fake initial
796		 * DCD (trapdoor DCD).  If we are callout, then any sleeping
797		 * callin opens get woken up and resume sleeping on "cybi"
798		 * instead of "cydcd".
799		 */
800		/*
801		 * XXX `mynor & CALLOUT_MASK' should be
802		 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
803		 * TRAPDOOR_CARRIER is the default initial state for callout
804		 * devices and SOFT_CARRIER is like CLOCAL except it hides
805		 * the true carrier.
806		 */
807		if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
808			(*linesw[tp->t_line].l_modem)(tp, 1);
809	}
810	/*
811	 * Wait for DCD if necessary.
812	 */
813	if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
814	    && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
815		++com->wopeners;
816		error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "cydcd", 0);
817		--com->wopeners;
818		if (error != 0)
819			goto out;
820		goto open_top;
821	}
822	error =	(*linesw[tp->t_line].l_open)(dev, tp);
823	disc_optim(tp, &tp->t_termios, com);
824	if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
825		com->active_out = TRUE;
826	siosettimeout();
827out:
828	splx(s);
829	if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
830		comhardclose(com);
831	return (error);
832}
833
834static int
835sioclose(dev, flag, mode, p)
836	dev_t		dev;
837	int		flag;
838	int		mode;
839	struct proc	*p;
840{
841	struct com_s	*com;
842	int		mynor;
843	int		s;
844	struct tty	*tp;
845
846	mynor = minor(dev);
847	if (mynor & CONTROL_MASK)
848		return (0);
849	com = com_addr(MINOR_TO_UNIT(mynor));
850	tp = com->tp;
851	s = spltty();
852	cd_etc(com, CD1400_ETC_STOPBREAK);
853	(*linesw[tp->t_line].l_close)(tp, flag);
854	disc_optim(tp, &tp->t_termios, com);
855	comstop(tp, FREAD | FWRITE);
856	comhardclose(com);
857	ttyclose(tp);
858	siosettimeout();
859	splx(s);
860#ifdef broken /* session holds a ref to the tty; can't deallocate */
861	ttyfree(tp);
862	com->tp = sio_tty[unit] = NULL;
863#endif
864	return (0);
865}
866
867static void
868comhardclose(com)
869	struct com_s	*com;
870{
871	cy_addr		iobase;
872	int		s;
873	struct tty	*tp;
874	int		unit;
875	int		intrsave;
876
877	unit = com->unit;
878	iobase = com->iobase;
879	s = spltty();
880#if 0
881	com->poll = FALSE;
882	com->poll_output = FALSE;
883#endif
884	com->do_timestamp = 0;
885#if 0
886	outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
887#else
888	/* XXX */
889	intrsave = save_intr();
890	disable_intr();
891	COM_LOCK();
892	com->etc = ETC_NONE;
893	cd_setreg(com, CD1400_COR2, com->cor[1] &= ~CD1400_COR2_ETC);
894	COM_UNLOCK();
895	restore_intr(intrsave);
896	cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
897#endif
898
899	{
900#if 0
901		outb(iobase + com_ier, 0);
902#else
903		intrsave = save_intr();
904		disable_intr();
905		COM_LOCK();
906		cd_setreg(com, CD1400_SRER, com->intr_enable = 0);
907		COM_UNLOCK();
908		restore_intr(intrsave);
909#endif
910		tp = com->tp;
911		if ((tp->t_cflag & HUPCL)
912		    /*
913		     * XXX we will miss any carrier drop between here and the
914		     * next open.  Perhaps we should watch DCD even when the
915		     * port is closed; it is not sufficient to check it at
916		     * the next open because it might go up and down while
917		     * we're not watching.
918		     */
919		    || (!com->active_out
920		       && !(com->prev_modem_status & MSR_DCD)
921		       && !(com->it_in.c_cflag & CLOCAL))
922		    || !(tp->t_state & TS_ISOPEN)) {
923			(void)commctl(com, TIOCM_DTR, DMBIC);
924
925			/* Disable receiver (leave transmitter enabled). */
926			com->channel_control = CD1400_CCR_CMDCHANCTL
927					       | CD1400_CCR_XMTEN
928					       | CD1400_CCR_RCVDIS;
929			cd1400_channel_cmd(com, com->channel_control);
930
931			if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
932				timeout(siodtrwakeup, com, com->dtr_wait);
933				com->state |= CS_DTR_OFF;
934			}
935		}
936	}
937#if 0
938	if (com->hasfifo) {
939		/*
940		 * Disable fifos so that they are off after controlled
941		 * reboots.  Some BIOSes fail to detect 16550s when the
942		 * fifos are enabled.
943		 */
944		outb(iobase + com_fifo, 0);
945	}
946#endif
947	com->active_out = FALSE;
948	wakeup(&com->active_out);
949	wakeup(TSA_CARR_ON(tp));	/* restart any wopeners */
950	splx(s);
951}
952
953static int
954siowrite(dev, uio, flag)
955	dev_t		dev;
956	struct uio	*uio;
957	int		flag;
958{
959	int		mynor;
960	struct tty	*tp;
961	int		unit;
962
963	mynor = minor(dev);
964	if (mynor & CONTROL_MASK)
965		return (ENODEV);
966
967	unit = MINOR_TO_UNIT(mynor);
968	tp = com_addr(unit)->tp;
969	/*
970	 * (XXX) We disallow virtual consoles if the physical console is
971	 * a serial port.  This is in case there is a display attached that
972	 * is not the console.  In that situation we don't need/want the X
973	 * server taking over the console.
974	 */
975	if (constty != NULL && unit == comconsole)
976		constty = NULL;
977#ifdef Smarts
978	/* XXX duplicate ttwrite(), but without so much output processing on
979	 * CR & LF chars.  Hardly worth the effort, given that high-throughput
980	 * sessions are raw anyhow.
981	 */
982#else
983	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
984#endif
985}
986
987static void
988siodtrwakeup(chan)
989	void	*chan;
990{
991	struct com_s	*com;
992
993	com = (struct com_s *)chan;
994	com->state &= ~CS_DTR_OFF;
995	wakeup(&com->dtr_wait);
996}
997
998/*
999 * This function:
1000 *  a) needs to be called with COM_LOCK() held, and
1001 *  b) needs to return with COM_LOCK() held.
1002 */
1003static void
1004sioinput(com)
1005	struct com_s	*com;
1006{
1007	u_char		*buf;
1008	int		incc;
1009	u_char		line_status;
1010	int		recv_data;
1011	struct tty	*tp;
1012	int		intrsave;
1013
1014	buf = com->ibuf;
1015	tp = com->tp;
1016	if (!(tp->t_state & TS_ISOPEN)) {
1017		com_events -= (com->iptr - com->ibuf);
1018		com->iptr = com->ibuf;
1019		return;
1020	}
1021	if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1022		/*
1023		 * Avoid the grotesquely inefficient lineswitch routine
1024		 * (ttyinput) in "raw" mode.  It usually takes about 450
1025		 * instructions (that's without canonical processing or echo!).
1026		 * slinput is reasonably fast (usually 40 instructions plus
1027		 * call overhead).
1028		 */
1029
1030		do {
1031			/*
1032			 * This may look odd, but it is using save-and-enable
1033			 * semantics instead of the save-and-disable semantics
1034			 * that are used everywhere else.
1035			 */
1036			intrsave = save_intr();
1037			COM_UNLOCK();
1038			enable_intr();
1039			incc = com->iptr - buf;
1040			if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
1041			    && (com->state & CS_RTS_IFLOW
1042				|| tp->t_iflag & IXOFF)
1043			    && !(tp->t_state & TS_TBLOCK))
1044				ttyblock(tp);
1045			com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1046				+= b_to_q((char *)buf, incc, &tp->t_rawq);
1047			buf += incc;
1048			tk_nin += incc;
1049			tk_rawcc += incc;
1050			tp->t_rawcc += incc;
1051			ttwakeup(tp);
1052			if (tp->t_state & TS_TTSTOP
1053			    && (tp->t_iflag & IXANY
1054				|| tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1055				tp->t_state &= ~TS_TTSTOP;
1056				tp->t_lflag &= ~FLUSHO;
1057				comstart(tp);
1058			}
1059			restore_intr(intrsave);
1060			COM_LOCK();
1061		} while (buf < com->iptr);
1062	} else {
1063		do {
1064			/*
1065			 * This may look odd, but it is using save-and-enable
1066			 * semantics instead of the save-and-disable semantics
1067			 * that are used everywhere else.
1068			 */
1069			intrsave = save_intr();
1070			COM_UNLOCK();
1071			enable_intr();
1072			line_status = buf[com->ierroff];
1073			recv_data = *buf++;
1074			if (line_status
1075			    & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
1076				if (line_status & LSR_BI)
1077					recv_data |= TTY_BI;
1078				if (line_status & LSR_FE)
1079					recv_data |= TTY_FE;
1080				if (line_status & LSR_OE)
1081					recv_data |= TTY_OE;
1082				if (line_status & LSR_PE)
1083					recv_data |= TTY_PE;
1084			}
1085			(*linesw[tp->t_line].l_rint)(recv_data, tp);
1086			restore_intr(intrsave);
1087			COM_LOCK();
1088		} while (buf < com->iptr);
1089	}
1090	com_events -= (com->iptr - com->ibuf);
1091	com->iptr = com->ibuf;
1092
1093	/*
1094	 * There is now room for another low-level buffer full of input,
1095	 * so enable RTS if it is now disabled and there is room in the
1096	 * high-level buffer.
1097	 */
1098	if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & com->mcr_rts) &&
1099	    !(tp->t_state & TS_TBLOCK))
1100#if 0
1101		outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1102#else
1103		cd_setreg(com, com->mcr_rts_reg,
1104			  com->mcr_image |= com->mcr_rts);
1105#endif
1106}
1107
1108void
1109siointr(unit)
1110	int	unit;
1111{
1112	int	baseu;
1113	int	cy_align;
1114	cy_addr	cy_iobase;
1115	int	cyu;
1116	cy_addr	iobase;
1117	u_char	status;
1118
1119	COM_LOCK();	/* XXX could this be placed down lower in the loop? */
1120
1121	baseu = unit * CY_MAX_PORTS;
1122	cy_align = com_addr(baseu)->cy_align;
1123	cy_iobase = com_addr(baseu)->cy_iobase;
1124
1125	/* check each CD1400 in turn */
1126	for (cyu = 0; cyu < cy_nr_cd1400s[unit]; ++cyu) {
1127		iobase = (cy_addr) (cy_iobase
1128				    + (cy_chip_offset[cyu] << cy_align));
1129		/* poll to see if it has any work */
1130		status = cd_inb(iobase, CD1400_SVRR, cy_align);
1131		if (status == 0)
1132			continue;
1133#ifdef CyDebug
1134		++cy_svrr_probes;
1135#endif
1136		/* service requests as appropriate, giving priority to RX */
1137		if (status & CD1400_SVRR_RXRDY) {
1138			struct com_s	*com;
1139			u_int		count;
1140			u_char		*ioptr;
1141			u_char		line_status;
1142			u_char		recv_data;
1143			u_char		serv_type;
1144#ifdef PollMode
1145			u_char		save_rir;
1146#endif
1147
1148#ifdef PollMode
1149			save_rir = cd_inb(iobase, CD1400_RIR, cy_align);
1150
1151			/* enter rx service */
1152			cd_outb(iobase, CD1400_CAR, cy_align, save_rir);
1153			com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1154			= save_rir & CD1400_CAR_CHAN;
1155
1156			serv_type = cd_inb(iobase, CD1400_RIVR, cy_align);
1157			com = com_addr(baseu
1158				       + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1159					  & CD1400_xIVR_CHAN));
1160#else
1161			/* ack receive service */
1162			serv_type = cy_inb(iobase, CY8_SVCACKR, cy_align);
1163
1164			com = com_addr(baseu +
1165				       + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1166					  & CD1400_xIVR_CHAN));
1167#endif
1168
1169		if (serv_type & CD1400_RIVR_EXCEPTION) {
1170			++com->recv_exception;
1171			line_status = cd_inb(iobase, CD1400_RDSR, cy_align);
1172			/* break/unnattached error bits or real input? */
1173			recv_data = cd_inb(iobase, CD1400_RDSR, cy_align);
1174#ifndef SOFT_HOTCHAR
1175			if (line_status & CD1400_RDSR_SPECIAL
1176			    && com->hotchar != 0)
1177				sched_swi(sio_ih, SWI_NOSWITCH);
1178
1179#endif
1180#if 1 /* XXX "intelligent" PFO error handling would break O error handling */
1181			if (line_status & (LSR_PE|LSR_FE|LSR_BI)) {
1182				/*
1183				  Don't store PE if IGNPAR and BI if IGNBRK,
1184				  this hack allows "raw" tty optimization
1185				  works even if IGN* is set.
1186				*/
1187				if (   com->tp == NULL
1188				    || !(com->tp->t_state & TS_ISOPEN)
1189				    || ((line_status & (LSR_PE|LSR_FE))
1190				    &&  (com->tp->t_iflag & IGNPAR))
1191				    || ((line_status & LSR_BI)
1192				    &&  (com->tp->t_iflag & IGNBRK)))
1193					goto cont;
1194				if (   (line_status & (LSR_PE|LSR_FE))
1195				    && (com->tp->t_state & TS_CAN_BYPASS_L_RINT)
1196				    && ((line_status & LSR_FE)
1197				    ||  ((line_status & LSR_PE)
1198				    &&  (com->tp->t_iflag & INPCK))))
1199					recv_data = 0;
1200			}
1201#endif /* 1 */
1202			++com->bytes_in;
1203#ifdef SOFT_HOTCHAR
1204			if (com->hotchar != 0 && recv_data == com->hotchar)
1205				sched_swi(sio_ih, SWI_NOSWITCH);
1206#endif
1207			ioptr = com->iptr;
1208			if (ioptr >= com->ibufend)
1209				CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1210			else {
1211				if (com->do_timestamp)
1212					microtime(&com->timestamp);
1213				++com_events;
1214				ioptr[0] = recv_data;
1215				ioptr[com->ierroff] = line_status;
1216				com->iptr = ++ioptr;
1217				if (ioptr == com->ihighwater
1218				    && com->state & CS_RTS_IFLOW)
1219#if 0
1220					outb(com->modem_ctl_port,
1221					     com->mcr_image &= ~MCR_RTS);
1222#else
1223					cd_outb(iobase, com->mcr_rts_reg,
1224						cy_align,
1225						com->mcr_image &=
1226						~com->mcr_rts);
1227#endif
1228				if (line_status & LSR_OE)
1229					CE_RECORD(com, CE_OVERRUN);
1230			}
1231			goto cont;
1232		} else {
1233			int	ifree;
1234
1235			count = cd_inb(iobase, CD1400_RDCR, cy_align);
1236			if (!count)
1237				goto cont;
1238			com->bytes_in += count;
1239			ioptr = com->iptr;
1240			ifree = com->ibufend - ioptr;
1241			if (count > ifree) {
1242				count -= ifree;
1243				com_events += ifree;
1244				if (ifree != 0) {
1245					if (com->do_timestamp)
1246						microtime(&com->timestamp);
1247					do {
1248						recv_data = cd_inb(iobase,
1249								   CD1400_RDSR,
1250								   cy_align);
1251#ifdef SOFT_HOTCHAR
1252						if (com->hotchar != 0
1253						    && recv_data
1254						       == com->hotchar)
1255							sched_swi(sio_ih, SWI_NOSWITCH);
1256#endif
1257						ioptr[0] = recv_data;
1258						ioptr[com->ierroff] = 0;
1259						++ioptr;
1260					} while (--ifree != 0);
1261				}
1262				com->delta_error_counts
1263				    [CE_INTERRUPT_BUF_OVERFLOW] += count;
1264				do {
1265					recv_data = cd_inb(iobase, CD1400_RDSR,
1266							   cy_align);
1267#ifdef SOFT_HOTCHAR
1268					if (com->hotchar != 0
1269					    && recv_data == com->hotchar)
1270						sched_swi(sio_ih, SWI_NOSWITCH);
1271#endif
1272				} while (--count != 0);
1273			} else {
1274				if (com->do_timestamp)
1275					microtime(&com->timestamp);
1276				if (ioptr <= com->ihighwater
1277				    && ioptr + count > com->ihighwater
1278				    && com->state & CS_RTS_IFLOW)
1279#if 0
1280					outb(com->modem_ctl_port,
1281					     com->mcr_image &= ~MCR_RTS);
1282#else
1283					cd_outb(iobase, com->mcr_rts_reg,
1284						cy_align,
1285						com->mcr_image
1286						&= ~com->mcr_rts);
1287#endif
1288				com_events += count;
1289				do {
1290					recv_data = cd_inb(iobase, CD1400_RDSR,
1291							   cy_align);
1292#ifdef SOFT_HOTCHAR
1293					if (com->hotchar != 0
1294					    && recv_data == com->hotchar)
1295						sched_swi(sio_ih, SWI_NOSWITCH);
1296#endif
1297					ioptr[0] = recv_data;
1298					ioptr[com->ierroff] = 0;
1299					++ioptr;
1300				} while (--count != 0);
1301			}
1302			com->iptr = ioptr;
1303		}
1304cont:
1305
1306			/* terminate service context */
1307#ifdef PollMode
1308			cd_outb(iobase, CD1400_RIR, cy_align,
1309				save_rir
1310				& ~(CD1400_RIR_RDIREQ | CD1400_RIR_RBUSY));
1311#else
1312			cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1313#endif
1314		}
1315		if (status & CD1400_SVRR_MDMCH) {
1316			struct com_s	*com;
1317			u_char	modem_status;
1318#ifdef PollMode
1319			u_char	save_mir;
1320#else
1321			u_char	vector;
1322#endif
1323
1324#ifdef PollMode
1325			save_mir = cd_inb(iobase, CD1400_MIR, cy_align);
1326
1327			/* enter modem service */
1328			cd_outb(iobase, CD1400_CAR, cy_align, save_mir);
1329			com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1330			= save_mir & CD1400_CAR_CHAN;
1331
1332			com = com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS
1333				       + (save_mir & CD1400_MIR_CHAN));
1334#else
1335			/* ack modem service */
1336			vector = cy_inb(iobase, CY8_SVCACKM, cy_align);
1337
1338			com = com_addr(baseu
1339				       + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1340					  & CD1400_xIVR_CHAN));
1341#endif
1342			++com->mdm;
1343			modem_status = cd_inb(iobase, CD1400_MSVR2, cy_align);
1344		if (modem_status != com->last_modem_status) {
1345			if (com->do_dcd_timestamp
1346			    && !(com->last_modem_status & MSR_DCD)
1347			    && modem_status & MSR_DCD)
1348				microtime(&com->dcd_timestamp);
1349
1350			/*
1351			 * Schedule high level to handle DCD changes.  Note
1352			 * that we don't use the delta bits anywhere.  Some
1353			 * UARTs mess them up, and it's easy to remember the
1354			 * previous bits and calculate the delta.
1355			 */
1356			com->last_modem_status = modem_status;
1357			if (!(com->state & CS_CHECKMSR)) {
1358				com_events += LOTS_OF_EVENTS;
1359				com->state |= CS_CHECKMSR;
1360				sched_swi(sio_ih, SWI_NOSWITCH);
1361			}
1362
1363#ifdef SOFT_CTS_OFLOW
1364			/* handle CTS change immediately for crisp flow ctl */
1365			if (com->state & CS_CTS_OFLOW) {
1366				if (modem_status & MSR_CTS) {
1367					com->state |= CS_ODEVREADY;
1368					if (com->state >= (CS_BUSY | CS_TTGO
1369							   | CS_ODEVREADY)
1370					    && !(com->intr_enable
1371						 & CD1400_SRER_TXRDY))
1372						cd_outb(iobase, CD1400_SRER,
1373							cy_align,
1374							com->intr_enable
1375							= com->intr_enable
1376							  & ~CD1400_SRER_TXMPTY
1377							  | CD1400_SRER_TXRDY);
1378				} else {
1379					com->state &= ~CS_ODEVREADY;
1380					if (com->intr_enable
1381					    & CD1400_SRER_TXRDY)
1382						cd_outb(iobase, CD1400_SRER,
1383							cy_align,
1384							com->intr_enable
1385							= com->intr_enable
1386							  & ~CD1400_SRER_TXRDY
1387							  | CD1400_SRER_TXMPTY);
1388				}
1389			}
1390#endif
1391		}
1392
1393			/* terminate service context */
1394#ifdef PollMode
1395			cd_outb(iobase, CD1400_MIR, cy_align,
1396				save_mir
1397				& ~(CD1400_MIR_RDIREQ | CD1400_MIR_RBUSY));
1398#else
1399			cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1400#endif
1401		}
1402		if (status & CD1400_SVRR_TXRDY) {
1403			struct com_s	*com;
1404#ifdef PollMode
1405			u_char	save_tir;
1406#else
1407			u_char	vector;
1408#endif
1409
1410#ifdef PollMode
1411			save_tir = cd_inb(iobase, CD1400_TIR, cy_align);
1412
1413			/* enter tx service */
1414			cd_outb(iobase, CD1400_CAR, cy_align, save_tir);
1415			com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1416			= save_tir & CD1400_CAR_CHAN;
1417
1418			com = com_addr(baseu
1419				       + cyu * CD1400_NO_OF_CHANNELS
1420				       + (save_tir & CD1400_TIR_CHAN));
1421#else
1422			/* ack transmit service */
1423			vector = cy_inb(iobase, CY8_SVCACKT, cy_align);
1424
1425			com = com_addr(baseu
1426				       + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1427					  & CD1400_xIVR_CHAN));
1428#endif
1429
1430			if (com->etc != ETC_NONE) {
1431				if (com->intr_enable & CD1400_SRER_TXRDY) {
1432					/*
1433					 * Here due to sloppy SRER_TXRDY
1434					 * enabling.  Ignore.  Come back when
1435					 * tx is empty.
1436					 */
1437					cd_outb(iobase, CD1400_SRER, cy_align,
1438						com->intr_enable
1439						= (com->intr_enable
1440						  & ~CD1400_SRER_TXRDY)
1441						  | CD1400_SRER_TXMPTY);
1442					goto terminate_tx_service;
1443				}
1444				switch (com->etc) {
1445				case CD1400_ETC_SENDBREAK:
1446				case CD1400_ETC_STOPBREAK:
1447					/*
1448					 * Start the command.  Come back on
1449					 * next tx empty interrupt, hopefully
1450					 * after command has been executed.
1451					 */
1452					cd_outb(iobase, CD1400_COR2, cy_align,
1453						com->cor[1] |= CD1400_COR2_ETC);
1454					cd_outb(iobase, CD1400_TDR, cy_align,
1455						CD1400_ETC_CMD);
1456					cd_outb(iobase, CD1400_TDR, cy_align,
1457						com->etc);
1458					if (com->etc == CD1400_ETC_SENDBREAK)
1459						com->etc = ETC_BREAK_STARTING;
1460					else
1461						com->etc = ETC_BREAK_ENDING;
1462					goto terminate_tx_service;
1463				case ETC_BREAK_STARTING:
1464					/*
1465					 * BREAK is now on.  Continue with
1466					 * SRER_TXMPTY processing, hopefully
1467					 * don't come back.
1468					 */
1469					com->etc = ETC_BREAK_STARTED;
1470					break;
1471				case ETC_BREAK_STARTED:
1472					/*
1473					 * Came back due to sloppy SRER_TXMPTY
1474					 * enabling.  Hope again.
1475					 */
1476					break;
1477				case ETC_BREAK_ENDING:
1478					/*
1479					 * BREAK is now off.  Continue with
1480					 * SRER_TXMPTY processing and don't
1481					 * come back.  The SWI handler will
1482					 * restart tx interrupts if necessary.
1483					 */
1484					cd_outb(iobase, CD1400_COR2, cy_align,
1485						com->cor[1]
1486						&= ~CD1400_COR2_ETC);
1487					com->etc = ETC_BREAK_ENDED;
1488					if (!(com->state & CS_ODONE)) {
1489						com_events += LOTS_OF_EVENTS;
1490						com->state |= CS_ODONE;
1491						sched_swi(sio_ih, SWI_NOSWITCH);
1492					}
1493					break;
1494				case ETC_BREAK_ENDED:
1495					/*
1496					 * Shouldn't get here.  Hope again.
1497					 */
1498					break;
1499				}
1500			}
1501			if (com->intr_enable & CD1400_SRER_TXMPTY) {
1502				if (!(com->extra_state & CSE_ODONE)) {
1503					com_events += LOTS_OF_EVENTS;
1504					com->extra_state |= CSE_ODONE;
1505					sched_swi(sio_ih, SWI_NOSWITCH);
1506				}
1507				cd_outb(iobase, CD1400_SRER, cy_align,
1508					com->intr_enable
1509					&= ~CD1400_SRER_TXMPTY);
1510				goto terminate_tx_service;
1511			}
1512		if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1513			u_char	*ioptr;
1514			u_int	ocount;
1515
1516			ioptr = com->obufq.l_head;
1517				ocount = com->obufq.l_tail - ioptr;
1518				if (ocount > CD1400_TX_FIFO_SIZE)
1519					ocount = CD1400_TX_FIFO_SIZE;
1520				com->bytes_out += ocount;
1521				do
1522					cd_outb(iobase, CD1400_TDR, cy_align,
1523						*ioptr++);
1524				while (--ocount != 0);
1525			com->obufq.l_head = ioptr;
1526			if (ioptr >= com->obufq.l_tail) {
1527				struct lbq	*qp;
1528
1529				qp = com->obufq.l_next;
1530				qp->l_queued = FALSE;
1531				qp = qp->l_next;
1532				if (qp != NULL) {
1533					com->obufq.l_head = qp->l_head;
1534					com->obufq.l_tail = qp->l_tail;
1535					com->obufq.l_next = qp;
1536				} else {
1537					/* output just completed */
1538					com->state &= ~CS_BUSY;
1539
1540					/*
1541					 * The setting of CSE_ODONE may be
1542					 * stale here.  We currently only
1543					 * use it when CS_BUSY is set, and
1544					 * fixing it when we clear CS_BUSY
1545					 * is easiest.
1546					 */
1547					if (com->extra_state & CSE_ODONE) {
1548						com_events -= LOTS_OF_EVENTS;
1549						com->extra_state &= ~CSE_ODONE;
1550					}
1551
1552					cd_outb(iobase, CD1400_SRER, cy_align,
1553						com->intr_enable
1554						= (com->intr_enable
1555						  & ~CD1400_SRER_TXRDY)
1556						  | CD1400_SRER_TXMPTY);
1557				}
1558				if (!(com->state & CS_ODONE)) {
1559					com_events += LOTS_OF_EVENTS;
1560					com->state |= CS_ODONE;
1561
1562					/* handle at high level ASAP */
1563					sched_swi(sio_ih, SWI_NOSWITCH);
1564				}
1565			}
1566		}
1567
1568			/* terminate service context */
1569terminate_tx_service:
1570#ifdef PollMode
1571			cd_outb(iobase, CD1400_TIR, cy_align,
1572				save_tir
1573				& ~(CD1400_TIR_RDIREQ | CD1400_TIR_RBUSY));
1574#else
1575			cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1576#endif
1577		}
1578	}
1579
1580	/* ensure an edge for the next interrupt */
1581	cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
1582
1583	sched_swi(sio_ih, SWI_NOSWITCH);
1584
1585	COM_UNLOCK();
1586}
1587
1588#if 0
1589static void
1590siointr1(com)
1591	struct com_s	*com;
1592{
1593}
1594#endif
1595
1596static int
1597sioioctl(dev, cmd, data, flag, p)
1598	dev_t		dev;
1599	u_long		cmd;
1600	caddr_t		data;
1601	int		flag;
1602	struct proc	*p;
1603{
1604	struct com_s	*com;
1605	int		error;
1606	int		mynor;
1607	int		s;
1608	struct tty	*tp;
1609#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1610	int		oldcmd;
1611	struct termios	term;
1612#endif
1613
1614	mynor = minor(dev);
1615	com = com_addr(MINOR_TO_UNIT(mynor));
1616	if (mynor & CONTROL_MASK) {
1617		struct termios	*ct;
1618
1619		switch (mynor & CONTROL_MASK) {
1620		case CONTROL_INIT_STATE:
1621			ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
1622			break;
1623		case CONTROL_LOCK_STATE:
1624			ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
1625			break;
1626		default:
1627			return (ENODEV);	/* /dev/nodev */
1628		}
1629		switch (cmd) {
1630		case TIOCSETA:
1631			error = suser(p);
1632			if (error != 0)
1633				return (error);
1634			*ct = *(struct termios *)data;
1635			return (0);
1636		case TIOCGETA:
1637			*(struct termios *)data = *ct;
1638			return (0);
1639		case TIOCGETD:
1640			*(int *)data = TTYDISC;
1641			return (0);
1642		case TIOCGWINSZ:
1643			bzero(data, sizeof(struct winsize));
1644			return (0);
1645		default:
1646			return (ENOTTY);
1647		}
1648	}
1649	tp = com->tp;
1650#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1651	term = tp->t_termios;
1652	oldcmd = cmd;
1653	error = ttsetcompat(tp, &cmd, data, &term);
1654	if (error != 0)
1655		return (error);
1656	if (cmd != oldcmd)
1657		data = (caddr_t)&term;
1658#endif
1659	if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1660		int	cc;
1661		struct termios *dt = (struct termios *)data;
1662		struct termios *lt = mynor & CALLOUT_MASK
1663				     ? &com->lt_out : &com->lt_in;
1664
1665		dt->c_iflag = (tp->t_iflag & lt->c_iflag)
1666			      | (dt->c_iflag & ~lt->c_iflag);
1667		dt->c_oflag = (tp->t_oflag & lt->c_oflag)
1668			      | (dt->c_oflag & ~lt->c_oflag);
1669		dt->c_cflag = (tp->t_cflag & lt->c_cflag)
1670			      | (dt->c_cflag & ~lt->c_cflag);
1671		dt->c_lflag = (tp->t_lflag & lt->c_lflag)
1672			      | (dt->c_lflag & ~lt->c_lflag);
1673		for (cc = 0; cc < NCCS; ++cc)
1674			if (lt->c_cc[cc] != 0)
1675				dt->c_cc[cc] = tp->t_cc[cc];
1676		if (lt->c_ispeed != 0)
1677			dt->c_ispeed = tp->t_ispeed;
1678		if (lt->c_ospeed != 0)
1679			dt->c_ospeed = tp->t_ospeed;
1680	}
1681	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1682	if (error != ENOIOCTL)
1683		return (error);
1684	s = spltty();
1685	error = ttioctl(tp, cmd, data, flag);
1686	disc_optim(tp, &tp->t_termios, com);
1687	if (error != ENOIOCTL) {
1688		splx(s);
1689		return (error);
1690	}
1691	switch (cmd) {
1692	case TIOCSBRK:
1693#if 0
1694		outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
1695#else
1696		cd_etc(com, CD1400_ETC_SENDBREAK);
1697#endif
1698		break;
1699	case TIOCCBRK:
1700#if 0
1701		outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1702#else
1703		cd_etc(com, CD1400_ETC_STOPBREAK);
1704#endif
1705		break;
1706	case TIOCSDTR:
1707		(void)commctl(com, TIOCM_DTR, DMBIS);
1708		break;
1709	case TIOCCDTR:
1710		(void)commctl(com, TIOCM_DTR, DMBIC);
1711		break;
1712	/*
1713	 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set.  The
1714	 * changes get undone on the next call to comparam().
1715	 */
1716	case TIOCMSET:
1717		(void)commctl(com, *(int *)data, DMSET);
1718		break;
1719	case TIOCMBIS:
1720		(void)commctl(com, *(int *)data, DMBIS);
1721		break;
1722	case TIOCMBIC:
1723		(void)commctl(com, *(int *)data, DMBIC);
1724		break;
1725	case TIOCMGET:
1726		*(int *)data = commctl(com, 0, DMGET);
1727		break;
1728	case TIOCMSDTRWAIT:
1729		/* must be root since the wait applies to following logins */
1730		error = suser(p);
1731		if (error != 0) {
1732			splx(s);
1733			return (error);
1734		}
1735		com->dtr_wait = *(int *)data * hz / 100;
1736		break;
1737	case TIOCMGDTRWAIT:
1738		*(int *)data = com->dtr_wait * 100 / hz;
1739		break;
1740	case TIOCTIMESTAMP:
1741		com->do_timestamp = TRUE;
1742		*(struct timeval *)data = com->timestamp;
1743		break;
1744	case TIOCDCDTIMESTAMP:
1745		com->do_dcd_timestamp = TRUE;
1746		*(struct timeval *)data = com->dcd_timestamp;
1747		break;
1748	default:
1749		splx(s);
1750		return (ENOTTY);
1751	}
1752	splx(s);
1753	return (0);
1754}
1755
1756static void
1757siopoll(void *arg)
1758{
1759	int		unit;
1760	int		intrsave;
1761
1762#ifdef CyDebug
1763	++cy_timeouts;
1764#endif
1765	if (com_events == 0)
1766		return;
1767repeat:
1768	for (unit = 0; unit < NSIO; ++unit) {
1769		struct com_s	*com;
1770		int		incc;
1771		struct tty	*tp;
1772
1773		com = com_addr(unit);
1774		if (com == NULL)
1775			continue;
1776		tp = com->tp;
1777		if (tp == NULL) {
1778			/*
1779			 * XXX forget any events related to closed devices
1780			 * (actually never opened devices) so that we don't
1781			 * loop.
1782			 */
1783			intrsave = save_intr();
1784			disable_intr();
1785			COM_LOCK();
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;
1791			}
1792			com_events -= incc;
1793			COM_UNLOCK();
1794			restore_intr(intrsave);
1795			if (incc != 0)
1796				log(LOG_DEBUG,
1797				    "sio%d: %d events for device with no tp\n",
1798				    unit, incc);
1799			continue;
1800		}
1801		if (com->iptr != com->ibuf) {
1802			intrsave = save_intr();
1803			disable_intr();
1804			COM_LOCK();
1805			sioinput(com);
1806			COM_UNLOCK();
1807			restore_intr(intrsave);
1808		}
1809		if (com->state & CS_CHECKMSR) {
1810			u_char	delta_modem_status;
1811
1812			intrsave = save_intr();
1813			disable_intr();
1814			COM_LOCK();
1815			sioinput(com);
1816			delta_modem_status = com->last_modem_status
1817					     ^ com->prev_modem_status;
1818			com->prev_modem_status = com->last_modem_status;
1819			com_events -= LOTS_OF_EVENTS;
1820			com->state &= ~CS_CHECKMSR;
1821			COM_UNLOCK();
1822			restore_intr(intrsave);
1823			if (delta_modem_status & MSR_DCD)
1824				(*linesw[tp->t_line].l_modem)
1825					(tp, com->prev_modem_status & MSR_DCD);
1826		}
1827		if (com->extra_state & CSE_ODONE) {
1828			intrsave = save_intr();
1829			disable_intr();
1830			COM_LOCK();
1831			com_events -= LOTS_OF_EVENTS;
1832			com->extra_state &= ~CSE_ODONE;
1833			COM_UNLOCK();
1834			restore_intr(intrsave);
1835			if (!(com->state & CS_BUSY)) {
1836				tp->t_state &= ~TS_BUSY;
1837				ttwwakeup(com->tp);
1838			}
1839			if (com->etc != ETC_NONE) {
1840				if (com->etc == ETC_BREAK_ENDED)
1841					com->etc = ETC_NONE;
1842				wakeup(&com->etc);
1843			}
1844		}
1845		if (com->state & CS_ODONE) {
1846			intrsave = save_intr();
1847			disable_intr();
1848			COM_LOCK();
1849			com_events -= LOTS_OF_EVENTS;
1850			com->state &= ~CS_ODONE;
1851			COM_UNLOCK();
1852			restore_intr(intrsave);
1853			(*linesw[tp->t_line].l_start)(tp);
1854		}
1855		if (com_events == 0)
1856			break;
1857	}
1858	if (com_events >= LOTS_OF_EVENTS)
1859		goto repeat;
1860}
1861
1862static int
1863comparam(tp, t)
1864	struct tty	*tp;
1865	struct termios	*t;
1866{
1867	int		bits;
1868	int		cflag;
1869	struct com_s	*com;
1870	u_char		cor_change;
1871	u_long		cy_clock;
1872	int		idivisor;
1873	int		iflag;
1874	int		iprescaler;
1875	int		itimeout;
1876	int		odivisor;
1877	int		oprescaler;
1878	u_char		opt;
1879	int		s;
1880	int		unit;
1881	int		intrsave;
1882
1883	/* do historical conversions */
1884	if (t->c_ispeed == 0)
1885		t->c_ispeed = t->c_ospeed;
1886
1887	unit = DEV_TO_UNIT(tp->t_dev);
1888	com = com_addr(unit);
1889
1890	/* check requested parameters */
1891	cy_clock = CY_CLOCK(com->gfrcr_image);
1892	idivisor = comspeed(t->c_ispeed, cy_clock, &iprescaler);
1893	if (idivisor < 0)
1894		return (EINVAL);
1895	odivisor = comspeed(t->c_ospeed, cy_clock, &oprescaler);
1896	if (odivisor < 0)
1897		return (EINVAL);
1898
1899	/* parameters are OK, convert them to the com struct and the device */
1900	s = spltty();
1901	if (odivisor == 0)
1902		(void)commctl(com, TIOCM_DTR, DMBIC);	/* hang up line */
1903	else
1904		(void)commctl(com, TIOCM_DTR, DMBIS);
1905
1906	(void) siosetwater(com, t->c_ispeed);
1907
1908	/* XXX we don't actually change the speed atomically. */
1909
1910	if (idivisor != 0) {
1911		cd_setreg(com, CD1400_RBPR, idivisor);
1912		cd_setreg(com, CD1400_RCOR, iprescaler);
1913	}
1914	if (odivisor != 0) {
1915		cd_setreg(com, CD1400_TBPR, odivisor);
1916		cd_setreg(com, CD1400_TCOR, oprescaler);
1917	}
1918
1919	/*
1920	 * channel control
1921	 *	receiver enable
1922	 *	transmitter enable (always set)
1923	 */
1924	cflag = t->c_cflag;
1925	opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
1926	      | (cflag & CREAD ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
1927	if (opt != com->channel_control) {
1928		com->channel_control = opt;
1929		cd1400_channel_cmd(com, opt);
1930	}
1931
1932#ifdef Smarts
1933	/* set special chars */
1934	/* XXX if one is _POSIX_VDISABLE, can't use some others */
1935	if (t->c_cc[VSTOP] != _POSIX_VDISABLE)
1936		cd_setreg(com, CD1400_SCHR1, t->c_cc[VSTOP]);
1937	if (t->c_cc[VSTART] != _POSIX_VDISABLE)
1938		cd_setreg(com, CD1400_SCHR2, t->c_cc[VSTART]);
1939	if (t->c_cc[VINTR] != _POSIX_VDISABLE)
1940		cd_setreg(com, CD1400_SCHR3, t->c_cc[VINTR]);
1941	if (t->c_cc[VSUSP] != _POSIX_VDISABLE)
1942		cd_setreg(com, CD1400_SCHR4, t->c_cc[VSUSP]);
1943#endif
1944
1945	/*
1946	 * set channel option register 1 -
1947	 *	parity mode
1948	 *	stop bits
1949	 *	char length
1950	 */
1951	opt = 0;
1952	/* parity */
1953	if (cflag & PARENB) {
1954		if (cflag & PARODD)
1955			opt |= CD1400_COR1_PARODD;
1956		opt |= CD1400_COR1_PARNORMAL;
1957	}
1958	iflag = t->c_iflag;
1959	if (!(iflag & INPCK))
1960		opt |= CD1400_COR1_NOINPCK;
1961	bits = 1 + 1;
1962	/* stop bits */
1963	if (cflag & CSTOPB) {
1964		++bits;
1965		opt |= CD1400_COR1_STOP2;
1966	}
1967	/* char length */
1968	switch (cflag & CSIZE) {
1969	case CS5:
1970		bits += 5;
1971		opt |= CD1400_COR1_CS5;
1972		break;
1973	case CS6:
1974		bits += 6;
1975		opt |= CD1400_COR1_CS6;
1976		break;
1977	case CS7:
1978		bits += 7;
1979		opt |= CD1400_COR1_CS7;
1980		break;
1981	default:
1982		bits += 8;
1983		opt |= CD1400_COR1_CS8;
1984		break;
1985	}
1986	cor_change = 0;
1987	if (opt != com->cor[0]) {
1988		cor_change |= CD1400_CCR_COR1;
1989		cd_setreg(com, CD1400_COR1, com->cor[0] = opt);
1990	}
1991
1992	/*
1993	 * Set receive time-out period, normally to max(one char time, 5 ms).
1994	 */
1995	if (t->c_ispeed == 0)
1996		itimeout = cd_getreg(com, CD1400_RTPR);
1997	else {
1998		itimeout = (1000 * bits + t->c_ispeed - 1) / t->c_ispeed;
1999#ifdef SOFT_HOTCHAR
2000#define	MIN_RTP		1
2001#else
2002#define	MIN_RTP		5
2003#endif
2004		if (itimeout < MIN_RTP)
2005			itimeout = MIN_RTP;
2006	}
2007	if (!(t->c_lflag & ICANON) && t->c_cc[VMIN] != 0 && t->c_cc[VTIME] != 0
2008	    && t->c_cc[VTIME] * 10 > itimeout)
2009		itimeout = t->c_cc[VTIME] * 10;
2010	if (itimeout > 255)
2011		itimeout = 255;
2012	cd_setreg(com, CD1400_RTPR, itimeout);
2013
2014	/*
2015	 * set channel option register 2 -
2016	 *	flow control
2017	 */
2018	opt = 0;
2019#ifdef Smarts
2020	if (iflag & IXANY)
2021		opt |= CD1400_COR2_IXANY;
2022	if (iflag & IXOFF)
2023		opt |= CD1400_COR2_IXOFF;
2024#endif
2025#ifndef SOFT_CTS_OFLOW
2026	if (cflag & CCTS_OFLOW)
2027		opt |= CD1400_COR2_CCTS_OFLOW;
2028#endif
2029	intrsave = save_intr();
2030	disable_intr();
2031	COM_LOCK();
2032	if (opt != com->cor[1]) {
2033		cor_change |= CD1400_CCR_COR2;
2034		cd_setreg(com, CD1400_COR2, com->cor[1] = opt);
2035	}
2036	COM_UNLOCK();
2037	restore_intr(intrsave);
2038
2039	/*
2040	 * set channel option register 3 -
2041	 *	receiver FIFO interrupt threshold
2042	 *	flow control
2043	 */
2044	opt = RxFifoThreshold;
2045#ifdef Smarts
2046	if (t->c_lflag & ICANON)
2047		opt |= CD1400_COR3_SCD34;	/* detect INTR & SUSP chars */
2048	if (iflag & IXOFF)
2049		/* detect and transparently handle START and STOP chars */
2050		opt |= CD1400_COR3_FCT | CD1400_COR3_SCD12;
2051#endif
2052	if (opt != com->cor[2]) {
2053		cor_change |= CD1400_CCR_COR3;
2054		cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2055	}
2056
2057	/* notify the CD1400 if COR1-3 have changed */
2058	if (cor_change)
2059		cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | cor_change);
2060
2061	/*
2062	 * set channel option register 4 -
2063	 *	CR/NL processing
2064	 *	break processing
2065	 *	received exception processing
2066	 */
2067	opt = 0;
2068	if (iflag & IGNCR)
2069		opt |= CD1400_COR4_IGNCR;
2070#ifdef Smarts
2071	/*
2072	 * we need a new ttyinput() for this, as we don't want to
2073	 * have ICRNL && INLCR being done in both layers, or to have
2074	 * synchronisation problems
2075	 */
2076	if (iflag & ICRNL)
2077		opt |= CD1400_COR4_ICRNL;
2078	if (iflag & INLCR)
2079		opt |= CD1400_COR4_INLCR;
2080#endif
2081	if (iflag & IGNBRK)
2082		opt |= CD1400_COR4_IGNBRK | CD1400_COR4_NOBRKINT;
2083	/*
2084	 * The `-ignbrk -brkint parmrk' case is not handled by the hardware,
2085	 * so only tell the hardware about -brkint if -parmrk.
2086	 */
2087	if (!(iflag & (BRKINT | PARMRK)))
2088		opt |= CD1400_COR4_NOBRKINT;
2089#if 0
2090	/* XXX using this "intelligence" breaks reporting of overruns. */
2091	if (iflag & IGNPAR)
2092		opt |= CD1400_COR4_PFO_DISCARD;
2093	else {
2094		if (iflag & PARMRK)
2095			opt |= CD1400_COR4_PFO_ESC;
2096		else
2097			opt |= CD1400_COR4_PFO_NUL;
2098	}
2099#else
2100	opt |= CD1400_COR4_PFO_EXCEPTION;
2101#endif
2102	cd_setreg(com, CD1400_COR4, opt);
2103
2104	/*
2105	 * set channel option register 5 -
2106	 */
2107	opt = 0;
2108	if (iflag & ISTRIP)
2109		opt |= CD1400_COR5_ISTRIP;
2110	if (t->c_iflag & IEXTEN)
2111		/* enable LNEXT (e.g. ctrl-v quoting) handling */
2112		opt |= CD1400_COR5_LNEXT;
2113#ifdef Smarts
2114	if (t->c_oflag & ONLCR)
2115		opt |= CD1400_COR5_ONLCR;
2116	if (t->c_oflag & OCRNL)
2117		opt |= CD1400_COR5_OCRNL;
2118#endif
2119	cd_setreg(com, CD1400_COR5, opt);
2120
2121	/*
2122	 * We always generate modem status change interrupts for CD changes.
2123	 * Among other things, this is necessary to track TS_CARR_ON for
2124	 * pstat to print even when the driver doesn't care.  CD changes
2125	 * should be rare so interrupts for them are not worth extra code to
2126	 * avoid.  We avoid interrupts for other modem status changes (except
2127	 * for CTS changes when SOFT_CTS_OFLOW is configured) since this is
2128	 * simplest and best.
2129	 */
2130
2131	/*
2132	 * set modem change option register 1
2133	 *	generate modem interrupts on which 1 -> 0 input transitions
2134	 *	also controls auto-DTR output flow-control, which we don't use
2135	 */
2136	opt = CD1400_MCOR1_CDzd;
2137#ifdef SOFT_CTS_OFLOW
2138	if (cflag & CCTS_OFLOW)
2139		opt |= CD1400_MCOR1_CTSzd;
2140#endif
2141	cd_setreg(com, CD1400_MCOR1, opt);
2142
2143	/*
2144	 * set modem change option register 2
2145	 *	generate modem interrupts on specific 0 -> 1 input transitions
2146	 */
2147	opt = CD1400_MCOR2_CDod;
2148#ifdef SOFT_CTS_OFLOW
2149	if (cflag & CCTS_OFLOW)
2150		opt |= CD1400_MCOR2_CTSod;
2151#endif
2152	cd_setreg(com, CD1400_MCOR2, opt);
2153
2154	/*
2155	 * XXX should have done this long ago, but there is too much state
2156	 * to change all atomically.
2157	 */
2158	intrsave = save_intr();
2159	disable_intr();
2160	COM_LOCK();
2161
2162	com->state &= ~CS_TTGO;
2163	if (!(tp->t_state & TS_TTSTOP))
2164		com->state |= CS_TTGO;
2165	if (cflag & CRTS_IFLOW) {
2166		com->state |= CS_RTS_IFLOW;
2167		/*
2168		 * If CS_RTS_IFLOW just changed from off to on, the change
2169		 * needs to be propagated to MCR_RTS.  This isn't urgent,
2170		 * so do it later by calling comstart() instead of repeating
2171		 * a lot of code from comstart() here.
2172		 */
2173	} else if (com->state & CS_RTS_IFLOW) {
2174		com->state &= ~CS_RTS_IFLOW;
2175		/*
2176		 * CS_RTS_IFLOW just changed from on to off.  Force MCR_RTS
2177		 * on here, since comstart() won't do it later.
2178		 */
2179#if 0
2180		outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2181#else
2182		cd_setreg(com, com->mcr_rts_reg,
2183			  com->mcr_image |= com->mcr_rts);
2184#endif
2185	}
2186
2187	/*
2188	 * Set up state to handle output flow control.
2189	 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2190	 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2191	 */
2192	com->state |= CS_ODEVREADY;
2193#ifdef SOFT_CTS_OFLOW
2194	com->state &= ~CS_CTS_OFLOW;
2195	if (cflag & CCTS_OFLOW) {
2196		com->state |= CS_CTS_OFLOW;
2197		if (!(com->last_modem_status & MSR_CTS))
2198			com->state &= ~CS_ODEVREADY;
2199	}
2200#endif
2201	/* XXX shouldn't call functions while intrs are disabled. */
2202	disc_optim(tp, t, com);
2203#if 0
2204	/*
2205	 * Recover from fiddling with CS_TTGO.  We used to call siointr1()
2206	 * unconditionally, but that defeated the careful discarding of
2207	 * stale input in sioopen().
2208	 */
2209	if (com->state >= (CS_BUSY | CS_TTGO))
2210		siointr1(com);
2211#endif
2212	if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2213		if (!(com->intr_enable & CD1400_SRER_TXRDY))
2214			cd_setreg(com, CD1400_SRER,
2215				  com->intr_enable
2216				  = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2217				    | CD1400_SRER_TXRDY);
2218	} else {
2219		if (com->intr_enable & CD1400_SRER_TXRDY)
2220			cd_setreg(com, CD1400_SRER,
2221				  com->intr_enable
2222				  = (com->intr_enable & ~CD1400_SRER_TXRDY)
2223				    | CD1400_SRER_TXMPTY);
2224	}
2225
2226	COM_UNLOCK();
2227	restore_intr(intrsave);
2228	splx(s);
2229	comstart(tp);
2230	if (com->ibufold != NULL) {
2231		free(com->ibufold, M_DEVBUF);
2232		com->ibufold = NULL;
2233	}
2234	return (0);
2235}
2236
2237static int
2238siosetwater(com, speed)
2239	struct com_s	*com;
2240	speed_t		speed;
2241{
2242	int		cp4ticks;
2243	u_char		*ibuf;
2244	int		ibufsize;
2245	struct tty	*tp;
2246	int		intrsave;
2247
2248	/*
2249	 * Make the buffer size large enough to handle a softtty interrupt
2250	 * latency of about 2 ticks without loss of throughput or data
2251	 * (about 3 ticks if input flow control is not used or not honoured,
2252	 * but a bit less for CS5-CS7 modes).
2253	 */
2254	cp4ticks = speed / 10 / hz * 4;
2255	for (ibufsize = 128; ibufsize < cp4ticks;)
2256		ibufsize <<= 1;
2257	if (ibufsize == com->ibufsize) {
2258		return (0);
2259	}
2260
2261	/*
2262	 * Allocate input buffer.  The extra factor of 2 in the size is
2263	 * to allow for an error byte for each input byte.
2264	 */
2265	ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT);
2266	if (ibuf == NULL) {
2267		return (ENOMEM);
2268	}
2269
2270	/* Initialize non-critical variables. */
2271	com->ibufold = com->ibuf;
2272	com->ibufsize = ibufsize;
2273	tp = com->tp;
2274	if (tp != NULL) {
2275		tp->t_ififosize = 2 * ibufsize;
2276		tp->t_ispeedwat = (speed_t)-1;
2277		tp->t_ospeedwat = (speed_t)-1;
2278	}
2279
2280	/*
2281	 * Read current input buffer, if any.  Continue with interrupts
2282	 * disabled.
2283	 */
2284	intrsave = save_intr();
2285	disable_intr();
2286	COM_LOCK();
2287	if (com->iptr != com->ibuf)
2288		sioinput(com);
2289
2290	/*-
2291	 * Initialize critical variables, including input buffer watermarks.
2292	 * The external device is asked to stop sending when the buffer
2293	 * exactly reaches high water, or when the high level requests it.
2294	 * The high level is notified immediately (rather than at a later
2295	 * clock tick) when this watermark is reached.
2296	 * The buffer size is chosen so the watermark should almost never
2297	 * be reached.
2298	 * The low watermark is invisibly 0 since the buffer is always
2299	 * emptied all at once.
2300	 */
2301	com->iptr = com->ibuf = ibuf;
2302	com->ibufend = ibuf + ibufsize;
2303	com->ierroff = ibufsize;
2304	com->ihighwater = ibuf + 3 * ibufsize / 4;
2305
2306	COM_UNLOCK();
2307	restore_intr(intrsave);
2308	return (0);
2309}
2310
2311static void
2312comstart(tp)
2313	struct tty	*tp;
2314{
2315	struct com_s	*com;
2316	int		s;
2317#ifdef CyDebug
2318	bool_t		started;
2319#endif
2320	int		unit;
2321	int		intrsave;
2322
2323	unit = DEV_TO_UNIT(tp->t_dev);
2324	com = com_addr(unit);
2325	s = spltty();
2326
2327#ifdef CyDebug
2328	++com->start_count;
2329	started = FALSE;
2330#endif
2331
2332	intrsave = save_intr();
2333	disable_intr();
2334	COM_LOCK();
2335	if (tp->t_state & TS_TTSTOP) {
2336		com->state &= ~CS_TTGO;
2337		if (com->intr_enable & CD1400_SRER_TXRDY)
2338			cd_setreg(com, CD1400_SRER,
2339				  com->intr_enable
2340				  = (com->intr_enable & ~CD1400_SRER_TXRDY)
2341				    | CD1400_SRER_TXMPTY);
2342	} else {
2343		com->state |= CS_TTGO;
2344		if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)
2345		    && !(com->intr_enable & CD1400_SRER_TXRDY))
2346			cd_setreg(com, CD1400_SRER,
2347				  com->intr_enable
2348				  = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2349				    | CD1400_SRER_TXRDY);
2350	}
2351	if (tp->t_state & TS_TBLOCK) {
2352		if (com->mcr_image & com->mcr_rts && com->state & CS_RTS_IFLOW)
2353#if 0
2354			outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2355#else
2356			cd_setreg(com, com->mcr_rts_reg,
2357				  com->mcr_image &= ~com->mcr_rts);
2358#endif
2359	} else {
2360		if (!(com->mcr_image & com->mcr_rts)
2361		    && com->iptr < com->ihighwater
2362		    && com->state & CS_RTS_IFLOW)
2363#if 0
2364			outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2365#else
2366			cd_setreg(com, com->mcr_rts_reg,
2367				  com->mcr_image |= com->mcr_rts);
2368#endif
2369	}
2370	COM_UNLOCK();
2371	restore_intr(intrsave);
2372	if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2373		ttwwakeup(tp);
2374		splx(s);
2375		return;
2376	}
2377	if (tp->t_outq.c_cc != 0) {
2378		struct lbq	*qp;
2379		struct lbq	*next;
2380
2381		if (!com->obufs[0].l_queued) {
2382#ifdef CyDebug
2383			started = TRUE;
2384#endif
2385			com->obufs[0].l_tail
2386			    = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2387						  sizeof com->obuf1);
2388			com->obufs[0].l_next = NULL;
2389			com->obufs[0].l_queued = TRUE;
2390			intrsave = save_intr();
2391			disable_intr();
2392			COM_LOCK();
2393			if (com->state & CS_BUSY) {
2394				qp = com->obufq.l_next;
2395				while ((next = qp->l_next) != NULL)
2396					qp = next;
2397				qp->l_next = &com->obufs[0];
2398			} else {
2399				com->obufq.l_head = com->obufs[0].l_head;
2400				com->obufq.l_tail = com->obufs[0].l_tail;
2401				com->obufq.l_next = &com->obufs[0];
2402				com->state |= CS_BUSY;
2403				if (com->state >= (CS_BUSY | CS_TTGO
2404						   | CS_ODEVREADY))
2405					cd_setreg(com, CD1400_SRER,
2406						  com->intr_enable
2407						  = (com->intr_enable
2408						    & ~CD1400_SRER_TXMPTY)
2409						    | CD1400_SRER_TXRDY);
2410			}
2411			COM_UNLOCK();
2412			restore_intr(intrsave);
2413		}
2414		if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2415#ifdef CyDebug
2416			started = TRUE;
2417#endif
2418			com->obufs[1].l_tail
2419			    = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2420						  sizeof com->obuf2);
2421			com->obufs[1].l_next = NULL;
2422			com->obufs[1].l_queued = TRUE;
2423			intrsave = save_intr();
2424			disable_intr();
2425			COM_LOCK();
2426			if (com->state & CS_BUSY) {
2427				qp = com->obufq.l_next;
2428				while ((next = qp->l_next) != NULL)
2429					qp = next;
2430				qp->l_next = &com->obufs[1];
2431			} else {
2432				com->obufq.l_head = com->obufs[1].l_head;
2433				com->obufq.l_tail = com->obufs[1].l_tail;
2434				com->obufq.l_next = &com->obufs[1];
2435				com->state |= CS_BUSY;
2436				if (com->state >= (CS_BUSY | CS_TTGO
2437						   | CS_ODEVREADY))
2438					cd_setreg(com, CD1400_SRER,
2439						  com->intr_enable
2440						  = (com->intr_enable
2441						    & ~CD1400_SRER_TXMPTY)
2442						    | CD1400_SRER_TXRDY);
2443			}
2444			COM_UNLOCK();
2445			restore_intr(intrsave);
2446		}
2447		tp->t_state |= TS_BUSY;
2448	}
2449#ifdef CyDebug
2450	if (started)
2451		++com->start_real;
2452#endif
2453#if 0
2454	intrsave = save_intr();
2455	disable_intr();
2456	COM_LOCK();
2457	if (com->state >= (CS_BUSY | CS_TTGO))
2458		siointr1(com);	/* fake interrupt to start output */
2459	COM_UNLOCK();
2460	restore_intr(intrsave);
2461#endif
2462	ttwwakeup(tp);
2463	splx(s);
2464}
2465
2466static void
2467comstop(tp, rw)
2468	struct tty	*tp;
2469	int		rw;
2470{
2471	struct com_s	*com;
2472	bool_t		wakeup_etc;
2473	int		intrsave;
2474
2475	com = com_addr(DEV_TO_UNIT(tp->t_dev));
2476	wakeup_etc = FALSE;
2477	intrsave = save_intr();
2478	disable_intr();
2479	COM_LOCK();
2480	if (rw & FWRITE) {
2481		com->obufs[0].l_queued = FALSE;
2482		com->obufs[1].l_queued = FALSE;
2483		if (com->extra_state & CSE_ODONE) {
2484			com_events -= LOTS_OF_EVENTS;
2485			com->extra_state &= ~CSE_ODONE;
2486			if (com->etc != ETC_NONE) {
2487				if (com->etc == ETC_BREAK_ENDED)
2488					com->etc = ETC_NONE;
2489				wakeup_etc = TRUE;
2490			}
2491		}
2492		com->tp->t_state &= ~TS_BUSY;
2493		if (com->state & CS_ODONE)
2494			com_events -= LOTS_OF_EVENTS;
2495		com->state &= ~(CS_ODONE | CS_BUSY);
2496	}
2497	if (rw & FREAD) {
2498		/* XXX no way to reset only input fifo. */
2499		com_events -= (com->iptr - com->ibuf);
2500		com->iptr = com->ibuf;
2501	}
2502	COM_UNLOCK();
2503	restore_intr(intrsave);
2504	if (wakeup_etc)
2505		wakeup(&com->etc);
2506	if (rw & FWRITE && com->etc == ETC_NONE)
2507		cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
2508	comstart(tp);
2509}
2510
2511static int
2512commctl(com, bits, how)
2513	struct com_s	*com;
2514	int		bits;
2515	int		how;
2516{
2517	int	mcr;
2518	int	msr;
2519	int	intrsave;
2520
2521	if (how == DMGET) {
2522		if (com->channel_control & CD1400_CCR_RCVEN)
2523			bits |= TIOCM_LE;
2524		mcr = com->mcr_image;
2525		if (mcr & com->mcr_dtr)
2526			bits |= TIOCM_DTR;
2527		if (mcr & com->mcr_rts)
2528			/* XXX wired on for Cyclom-8Ys */
2529			bits |= TIOCM_RTS;
2530
2531		/*
2532		 * We must read the modem status from the hardware because
2533		 * we don't generate modem status change interrupts for all
2534		 * changes, so com->prev_modem_status is not guaranteed to
2535		 * be up to date.  This is safe, unlike for sio, because
2536		 * reading the status register doesn't clear pending modem
2537		 * status change interrupts.
2538		 */
2539		msr = cd_getreg(com, CD1400_MSVR2);
2540
2541		if (msr & MSR_CTS)
2542			bits |= TIOCM_CTS;
2543		if (msr & MSR_DCD)
2544			bits |= TIOCM_CD;
2545		if (msr & MSR_DSR)
2546			bits |= TIOCM_DSR;
2547		if (msr & MSR_RI)
2548			/* XXX not connected except for Cyclom-16Y? */
2549			bits |= TIOCM_RI;
2550		return (bits);
2551	}
2552	mcr = 0;
2553	if (bits & TIOCM_DTR)
2554		mcr |= com->mcr_dtr;
2555	if (bits & TIOCM_RTS)
2556		mcr |= com->mcr_rts;
2557	intrsave = save_intr();
2558	disable_intr();
2559	COM_LOCK();
2560	switch (how) {
2561	case DMSET:
2562		com->mcr_image = mcr;
2563		cd_setreg(com, CD1400_MSVR1, mcr);
2564		cd_setreg(com, CD1400_MSVR2, mcr);
2565		break;
2566	case DMBIS:
2567		com->mcr_image = mcr = com->mcr_image | mcr;
2568		cd_setreg(com, CD1400_MSVR1, mcr);
2569		cd_setreg(com, CD1400_MSVR2, mcr);
2570		break;
2571	case DMBIC:
2572		com->mcr_image = mcr = com->mcr_image & ~mcr;
2573		cd_setreg(com, CD1400_MSVR1, mcr);
2574		cd_setreg(com, CD1400_MSVR2, mcr);
2575		break;
2576	}
2577	COM_UNLOCK();
2578	restore_intr(intrsave);
2579	return (0);
2580}
2581
2582static void
2583siosettimeout()
2584{
2585	struct com_s	*com;
2586	bool_t		someopen;
2587	int		unit;
2588
2589	/*
2590	 * Set our timeout period to 1 second if no polled devices are open.
2591	 * Otherwise set it to max(1/200, 1/hz).
2592	 * Enable timeouts iff some device is open.
2593	 */
2594	untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
2595	sio_timeout = hz;
2596	someopen = FALSE;
2597	for (unit = 0; unit < NSIO; ++unit) {
2598		com = com_addr(unit);
2599		if (com != NULL && com->tp != NULL
2600		    && com->tp->t_state & TS_ISOPEN) {
2601			someopen = TRUE;
2602#if 0
2603			if (com->poll || com->poll_output) {
2604				sio_timeout = hz > 200 ? hz / 200 : 1;
2605				break;
2606			}
2607#endif
2608		}
2609	}
2610	if (someopen) {
2611		sio_timeouts_until_log = hz / sio_timeout;
2612		sio_timeout_handle = timeout(comwakeup, (void *)NULL,
2613					     sio_timeout);
2614	} else {
2615		/* Flush error messages, if any. */
2616		sio_timeouts_until_log = 1;
2617		comwakeup((void *)NULL);
2618		untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
2619	}
2620}
2621
2622static void
2623comwakeup(chan)
2624	void	*chan;
2625{
2626	struct com_s	*com;
2627	int		unit;
2628
2629	sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout);
2630
2631#if 0
2632	/*
2633	 * Recover from lost output interrupts.
2634	 * Poll any lines that don't use interrupts.
2635	 */
2636	for (unit = 0; unit < NSIO; ++unit) {
2637		com = com_addr(unit);
2638		if (com != NULL
2639		    && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2640			int	intrsave;
2641
2642			intrsave = save_intr();
2643			disable_intr();
2644			COM_LOCK();
2645			siointr1(com);
2646			COM_UNLOCK();
2647			restore_intr(intrsave);
2648		}
2649	}
2650#endif
2651
2652	/*
2653	 * Check for and log errors, but not too often.
2654	 */
2655	if (--sio_timeouts_until_log > 0)
2656		return;
2657	sio_timeouts_until_log = hz / sio_timeout;
2658	for (unit = 0; unit < NSIO; ++unit) {
2659		int	errnum;
2660
2661		com = com_addr(unit);
2662		if (com == NULL)
2663			continue;
2664		for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2665			u_int	delta;
2666			u_long	total;
2667			int	intrsave;
2668
2669			intrsave = save_intr();
2670			disable_intr();
2671			COM_LOCK();
2672			delta = com->delta_error_counts[errnum];
2673			com->delta_error_counts[errnum] = 0;
2674			COM_UNLOCK();
2675			restore_intr(intrsave);
2676			if (delta == 0)
2677				continue;
2678			total = com->error_counts[errnum] += delta;
2679			log(LOG_ERR, "cy%d: %u more %s%s (total %lu)\n",
2680			    unit, delta, error_desc[errnum],
2681			    delta == 1 ? "" : "s", total);
2682		}
2683	}
2684}
2685
2686static void
2687disc_optim(tp, t, com)
2688	struct tty	*tp;
2689	struct termios	*t;
2690	struct com_s	*com;
2691{
2692#ifndef SOFT_HOTCHAR
2693	u_char	opt;
2694#endif
2695
2696	/*
2697	 * XXX can skip a lot more cases if Smarts.  Maybe
2698	 * (IGNCR | ISTRIP | IXON) in c_iflag.  But perhaps we
2699	 * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2700	 */
2701	if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2702	    && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2703	    && (!(t->c_iflag & PARMRK)
2704		|| (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2705	    && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2706	    && linesw[tp->t_line].l_rint == ttyinput)
2707		tp->t_state |= TS_CAN_BYPASS_L_RINT;
2708	else
2709		tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2710	com->hotchar = linesw[tp->t_line].l_hotchar;
2711#ifndef SOFT_HOTCHAR
2712	opt = com->cor[2] & ~CD1400_COR3_SCD34;
2713	if (com->hotchar != 0) {
2714		cd_setreg(com, CD1400_SCHR3, com->hotchar);
2715		cd_setreg(com, CD1400_SCHR4, com->hotchar);
2716		opt |= CD1400_COR3_SCD34;
2717	}
2718	if (opt != com->cor[2]) {
2719		cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2720		cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR3);
2721	}
2722#endif
2723}
2724
2725#ifdef Smarts
2726/* standard line discipline input routine */
2727int
2728cyinput(c, tp)
2729	int		c;
2730	struct tty	*tp;
2731{
2732	/* XXX duplicate ttyinput(), but without the IXOFF/IXON/ISTRIP/IPARMRK
2733	 * bits, as they are done by the CD1400.  Hardly worth the effort,
2734	 * given that high-throughput sessions are raw anyhow.
2735	 */
2736}
2737#endif /* Smarts */
2738
2739static int
2740comspeed(speed, cy_clock, prescaler_io)
2741	speed_t	speed;
2742	u_long	cy_clock;
2743	int	*prescaler_io;
2744{
2745	int	actual;
2746	int	error;
2747	int	divider;
2748	int	prescaler;
2749	int	prescaler_unit;
2750
2751	if (speed == 0)
2752		return (0);
2753	if (speed < 0 || speed > 150000)
2754		return (-1);
2755
2756	/* determine which prescaler to use */
2757	for (prescaler_unit = 4, prescaler = 2048; prescaler_unit;
2758		prescaler_unit--, prescaler >>= 2) {
2759		if (cy_clock / prescaler / speed > 63)
2760			break;
2761	}
2762
2763	divider = (cy_clock / prescaler * 2 / speed + 1) / 2; /* round off */
2764	if (divider > 255)
2765		divider = 255;
2766	actual = cy_clock/prescaler/divider;
2767
2768	/* 10 times error in percent: */
2769	error = ((actual - (long)speed) * 2000 / (long)speed + 1) / 2;
2770
2771	/* 3.0% max error tolerance */
2772	if (error < -30 || error > 30)
2773		return (-1);
2774
2775#if 0
2776	printf("prescaler = %d (%d)\n", prescaler, prescaler_unit);
2777	printf("divider = %d (%x)\n", divider, divider);
2778	printf("actual = %d\n", actual);
2779	printf("error = %d\n", error);
2780#endif
2781
2782	*prescaler_io = prescaler_unit;
2783	return (divider);
2784}
2785
2786static void
2787cd1400_channel_cmd(com, cmd)
2788	struct com_s	*com;
2789	int		cmd;
2790{
2791	cd1400_channel_cmd_wait(com);
2792	cd_setreg(com, CD1400_CCR, cmd);
2793	cd1400_channel_cmd_wait(com);
2794}
2795
2796static void
2797cd1400_channel_cmd_wait(com)
2798	struct com_s	*com;
2799{
2800	struct timeval	start;
2801	struct timeval	tv;
2802	long		usec;
2803
2804	if (cd_getreg(com, CD1400_CCR) == 0)
2805		return;
2806	microtime(&start);
2807	for (;;) {
2808		if (cd_getreg(com, CD1400_CCR) == 0)
2809			return;
2810		microtime(&tv);
2811		usec = 1000000 * (tv.tv_sec - start.tv_sec) +
2812		    tv.tv_usec - start.tv_usec;
2813		if (usec >= 5000) {
2814			log(LOG_ERR,
2815			    "cy%d: channel command timeout (%ld usec)\n",
2816			    com->unit, usec);
2817			return;
2818		}
2819	}
2820}
2821
2822static void
2823cd_etc(com, etc)
2824	struct com_s	*com;
2825	int		etc;
2826{
2827	int		intrsave;
2828
2829	/*
2830	 * We can't change the hardware's ETC state while there are any
2831	 * characters in the tx fifo, since those characters would be
2832	 * interpreted as commands!  Unputting characters from the fifo
2833	 * is difficult, so we wait up to 12 character times for the fifo
2834	 * to drain.  The command will be delayed for up to 2 character
2835	 * times for the tx to become empty.  Unputting characters from
2836	 * the tx holding and shift registers is impossible, so we wait
2837	 * for the tx to become empty so that the command is sure to be
2838	 * executed soon after we issue it.
2839	 */
2840	intrsave = save_intr();
2841	disable_intr();
2842	COM_LOCK();
2843	if (com->etc == etc)
2844		goto wait;
2845	if ((etc == CD1400_ETC_SENDBREAK
2846	    && (com->etc == ETC_BREAK_STARTING
2847		|| com->etc == ETC_BREAK_STARTED))
2848	    || (etc == CD1400_ETC_STOPBREAK
2849	       && (com->etc == ETC_BREAK_ENDING || com->etc == ETC_BREAK_ENDED
2850		   || com->etc == ETC_NONE))) {
2851		COM_UNLOCK();
2852		restore_intr(intrsave);
2853		return;
2854	}
2855	com->etc = etc;
2856	cd_setreg(com, CD1400_SRER,
2857		  com->intr_enable
2858		  = (com->intr_enable & ~CD1400_SRER_TXRDY) | CD1400_SRER_TXMPTY);
2859wait:
2860	COM_UNLOCK();
2861	restore_intr(intrsave);
2862	while (com->etc == etc
2863	       && tsleep(&com->etc, TTIPRI | PCATCH, "cyetc", 0) == 0)
2864		continue;
2865}
2866
2867static int
2868cd_getreg(com, reg)
2869	struct com_s	*com;
2870	int		reg;
2871{
2872	struct com_s	*basecom;
2873	u_char	car;
2874	int	cy_align;
2875	int	intrsave;
2876	cy_addr	iobase;
2877	int	val;
2878
2879	basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2880	car = com->unit & CD1400_CAR_CHAN;
2881	cy_align = com->cy_align;
2882	iobase = com->iobase;
2883	intrsave = save_intr();
2884	disable_intr();
2885	if (intrsave & PSL_I)
2886		COM_LOCK();
2887	if (basecom->car != car)
2888		cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2889	val = cd_inb(iobase, reg, cy_align);
2890	if (intrsave & PSL_I)
2891		COM_UNLOCK();
2892	restore_intr(intrsave);
2893	return (val);
2894}
2895
2896static void
2897cd_setreg(com, reg, val)
2898	struct com_s	*com;
2899	int		reg;
2900	int		val;
2901{
2902	struct com_s	*basecom;
2903	u_char	car;
2904	int	cy_align;
2905	int	intrsave;
2906	cy_addr	iobase;
2907
2908	basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2909	car = com->unit & CD1400_CAR_CHAN;
2910	cy_align = com->cy_align;
2911	iobase = com->iobase;
2912	intrsave = save_intr();
2913	disable_intr();
2914	if (intrsave & PSL_I)
2915		COM_LOCK();
2916	if (basecom->car != car)
2917		cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2918	cd_outb(iobase, reg, cy_align, val);
2919	if (intrsave & PSL_I)
2920		COM_UNLOCK();
2921	restore_intr(intrsave);
2922}
2923
2924#ifdef CyDebug
2925/* useful in ddb */
2926void
2927cystatus(unit)
2928	int	unit;
2929{
2930	struct com_s	*com;
2931	cy_addr		iobase;
2932	u_int		ocount;
2933	struct tty	*tp;
2934
2935	com = com_addr(unit);
2936	printf("info for channel %d\n", unit);
2937	printf("------------------\n");
2938	printf("total cyclom service probes:\t%d\n", cy_svrr_probes);
2939	printf("calls to upper layer:\t\t%d\n", cy_timeouts);
2940	if (com == NULL)
2941		return;
2942	iobase = com->iobase;
2943	printf("\n");
2944	printf("cd1400 base address:\\tt%p\n", iobase);
2945	printf("saved channel_control:\t\t0x%02x\n", com->channel_control);
2946	printf("saved cor1-3:\t\t\t0x%02x 0x%02x 0x%02x\n",
2947	       com->cor[0], com->cor[1], com->cor[2]);
2948	printf("service request enable reg:\t0x%02x (0x%02x cached)\n",
2949	       cd_getreg(com, CD1400_SRER), com->intr_enable);
2950	printf("service request register:\t0x%02x\n",
2951	       cd_inb(iobase, CD1400_SVRR, com->cy_align));
2952	printf("modem status:\t\t\t0x%02x (0x%02x cached)\n",
2953	       cd_getreg(com, CD1400_MSVR2), com->prev_modem_status);
2954	printf("rx/tx/mdm interrupt registers:\t0x%02x 0x%02x 0x%02x\n",
2955	       cd_inb(iobase, CD1400_RIR, com->cy_align),
2956	       cd_inb(iobase, CD1400_TIR, com->cy_align),
2957	       cd_inb(iobase, CD1400_MIR, com->cy_align));
2958	printf("\n");
2959	printf("com state:\t\t\t0x%02x\n", com->state);
2960	printf("calls to comstart():\t\t%d (%d useful)\n",
2961	       com->start_count, com->start_real);
2962	printf("rx buffer chars free:\t\t%d\n", com->iptr - com->ibuf);
2963	ocount = 0;
2964	if (com->obufs[0].l_queued)
2965		ocount += com->obufs[0].l_tail - com->obufs[0].l_head;
2966	if (com->obufs[1].l_queued)
2967		ocount += com->obufs[1].l_tail - com->obufs[1].l_head;
2968	printf("tx buffer chars:\t\t%u\n", ocount);
2969	printf("received chars:\t\t\t%d\n", com->bytes_in);
2970	printf("received exceptions:\t\t%d\n", com->recv_exception);
2971	printf("modem signal deltas:\t\t%d\n", com->mdm);
2972	printf("transmitted chars:\t\t%d\n", com->bytes_out);
2973	printf("\n");
2974	tp = com->tp;
2975	if (tp != NULL) {
2976		printf("tty state:\t\t\t0x%08x\n", tp->t_state);
2977		printf(
2978		"upper layer queue lengths:\t%d raw, %d canon, %d output\n",
2979		       tp->t_rawq.c_cc, tp->t_canq.c_cc, tp->t_outq.c_cc);
2980	} else
2981		printf("tty state:\t\t\tclosed\n");
2982}
2983#endif /* CyDebug */
2984