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