sio.c revision 117167
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/sys/dev/sio/sio.c 117167 2003-07-02 16:09:02Z jhb $
34 *	from: @(#)com.c	7.5 (Berkeley) 5/16/91
35 *	from: i386/isa sio.c,v 1.234
36 */
37
38#include "opt_comconsole.h"
39#include "opt_compat.h"
40#include "opt_ddb.h"
41#include "opt_sio.h"
42
43/*
44 * Serial driver, based on 386BSD-0.1 com driver.
45 * Mostly rewritten to use pseudo-DMA.
46 * Works for National Semiconductor NS8250-NS16550AF UARTs.
47 * COM driver, based on HP dca driver.
48 *
49 * Changes for PC-Card integration:
50 *	- Added PC-Card driver table and handlers
51 */
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/bus.h>
55#include <sys/conf.h>
56#include <sys/fcntl.h>
57#include <sys/interrupt.h>
58#include <sys/kernel.h>
59#include <sys/limits.h>
60#include <sys/lock.h>
61#include <sys/malloc.h>
62#include <sys/module.h>
63#include <sys/mutex.h>
64#include <sys/proc.h>
65#include <sys/reboot.h>
66#include <sys/sysctl.h>
67#include <sys/syslog.h>
68#include <sys/tty.h>
69#include <machine/bus_pio.h>
70#include <machine/bus.h>
71#include <sys/rman.h>
72#include <sys/timepps.h>
73#include <sys/uio.h>
74
75#include <isa/isavar.h>
76
77#include <machine/resource.h>
78
79#include <dev/sio/sioreg.h>
80#include <dev/sio/siovar.h>
81
82#ifdef COM_ESP
83#include <dev/ic/esp.h>
84#endif
85#include <dev/ic/ns16550.h>
86
87#define	LOTS_OF_EVENTS	64	/* helps separate urgent events from input */
88
89#define	CALLOUT_MASK		0x80
90#define	CONTROL_MASK		0x60
91#define	CONTROL_INIT_STATE	0x20
92#define	CONTROL_LOCK_STATE	0x40
93#define	DEV_TO_UNIT(dev)	(MINOR_TO_UNIT(minor(dev)))
94#define	MINOR_TO_UNIT(mynor)	((((mynor) & ~0xffffU) >> (8 + 3)) \
95				 | ((mynor) & 0x1f))
96#define	UNIT_TO_MINOR(unit)	((((unit) & ~0x1fU) << (8 + 3)) \
97				 | ((unit) & 0x1f))
98
99#ifdef COM_MULTIPORT
100/* checks in flags for multiport and which is multiport "master chip"
101 * for a given card
102 */
103#define	COM_ISMULTIPORT(flags)	((flags) & 0x01)
104#define	COM_MPMASTER(flags)	(((flags) >> 8) & 0x0ff)
105#define	COM_NOTAST4(flags)	((flags) & 0x04)
106#else
107#define	COM_ISMULTIPORT(flags)	(0)
108#endif /* COM_MULTIPORT */
109
110#define	COM_CONSOLE(flags)	((flags) & 0x10)
111#define	COM_FORCECONSOLE(flags)	((flags) & 0x20)
112#define	COM_LLCONSOLE(flags)	((flags) & 0x40)
113#define	COM_DEBUGGER(flags)	((flags) & 0x80)
114#define	COM_LOSESOUTINTS(flags)	((flags) & 0x08)
115#define	COM_NOFIFO(flags)		((flags) & 0x02)
116#define	COM_PPSCTS(flags)	((flags) & 0x10000)
117#define COM_ST16650A(flags)	((flags) & 0x20000)
118#define COM_C_NOPROBE		(0x40000)
119#define COM_NOPROBE(flags)	((flags) & COM_C_NOPROBE)
120#define COM_C_IIR_TXRDYBUG	(0x80000)
121#define COM_IIR_TXRDYBUG(flags)	((flags) & COM_C_IIR_TXRDYBUG)
122#define COM_NOSCR(flags)	((flags) & 0x100000)
123#define	COM_TI16754(flags)	((flags) & 0x200000)
124#define	COM_FIFOSIZE(flags)	(((flags) & 0xff000000) >> 24)
125
126#define	sio_getreg(com, off) \
127	(bus_space_read_1((com)->bst, (com)->bsh, (off)))
128#define	sio_setreg(com, off, value) \
129	(bus_space_write_1((com)->bst, (com)->bsh, (off), (value)))
130
131/*
132 * com state bits.
133 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
134 * than the other bits so that they can be tested as a group without masking
135 * off the low bits.
136 *
137 * The following com and tty flags correspond closely:
138 *	CS_BUSY		= TS_BUSY (maintained by comstart(), siopoll() and
139 *				   comstop())
140 *	CS_TTGO		= ~TS_TTSTOP (maintained by comparam() and comstart())
141 *	CS_CTS_OFLOW	= CCTS_OFLOW (maintained by comparam())
142 *	CS_RTS_IFLOW	= CRTS_IFLOW (maintained by comparam())
143 * TS_FLUSH is not used.
144 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
145 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
146 */
147#define	CS_BUSY		0x80	/* output in progress */
148#define	CS_TTGO		0x40	/* output not stopped by XOFF */
149#define	CS_ODEVREADY	0x20	/* external device h/w ready (CTS) */
150#define	CS_CHECKMSR	1	/* check of MSR scheduled */
151#define	CS_CTS_OFLOW	2	/* use CTS output flow control */
152#define	CS_DTR_OFF	0x10	/* DTR held off */
153#define	CS_ODONE	4	/* output completed */
154#define	CS_RTS_IFLOW	8	/* use RTS input flow control */
155#define	CSE_BUSYCHECK	1	/* siobusycheck() scheduled */
156
157static	char const * const	error_desc[] = {
158#define	CE_OVERRUN			0
159	"silo overflow",
160#define	CE_INTERRUPT_BUF_OVERFLOW	1
161	"interrupt-level buffer overflow",
162#define	CE_TTY_BUF_OVERFLOW		2
163	"tty-level buffer overflow",
164};
165
166#define	CE_NTYPES			3
167#define	CE_RECORD(com, errnum)		(++(com)->delta_error_counts[errnum])
168
169/* types.  XXX - should be elsewhere */
170typedef u_int	Port_t;		/* hardware port */
171typedef u_char	bool_t;		/* boolean */
172
173/* queue of linear buffers */
174struct lbq {
175	u_char	*l_head;	/* next char to process */
176	u_char	*l_tail;	/* one past the last char to process */
177	struct lbq *l_next;	/* next in queue */
178	bool_t	l_queued;	/* nonzero if queued */
179};
180
181/* com device structure */
182struct com_s {
183	u_int	flags;		/* Copy isa device flags */
184	u_char	state;		/* miscellaneous flag bits */
185	bool_t  active_out;	/* nonzero if the callout device is open */
186	u_char	cfcr_image;	/* copy of value written to CFCR */
187#ifdef COM_ESP
188	bool_t	esp;		/* is this unit a hayes esp board? */
189#endif
190	u_char	extra_state;	/* more flag bits, separate for order trick */
191	u_char	fifo_image;	/* copy of value written to FIFO */
192	bool_t	hasfifo;	/* nonzero for 16550 UARTs */
193	bool_t	st16650a;	/* Is a Startech 16650A or RTS/CTS compat */
194	bool_t	loses_outints;	/* nonzero if device loses output interrupts */
195	u_char	mcr_image;	/* copy of value written to MCR */
196#ifdef COM_MULTIPORT
197	bool_t	multiport;	/* is this unit part of a multiport device? */
198#endif /* COM_MULTIPORT */
199	bool_t	no_irq;		/* nonzero if irq is not attached */
200	bool_t  gone;		/* hardware disappeared */
201	bool_t	poll;		/* nonzero if polling is required */
202	bool_t	poll_output;	/* nonzero if polling for output is required */
203	int	unit;		/* unit	number */
204	int	dtr_wait;	/* time to hold DTR down on close (* 1/hz) */
205	u_int	tx_fifo_size;
206	u_int	wopeners;	/* # processes waiting for DCD in open() */
207
208	/*
209	 * The high level of the driver never reads status registers directly
210	 * because there would be too many side effects to handle conveniently.
211	 * Instead, it reads copies of the registers stored here by the
212	 * interrupt handler.
213	 */
214	u_char	last_modem_status;	/* last MSR read by intr handler */
215	u_char	prev_modem_status;	/* last MSR handled by high level */
216
217	u_char	hotchar;	/* ldisc-specific char to be handled ASAP */
218	u_char	*ibuf;		/* start of input buffer */
219	u_char	*ibufend;	/* end of input buffer */
220	u_char	*ibufold;	/* old input buffer, to be freed */
221	u_char	*ihighwater;	/* threshold in input buffer */
222	u_char	*iptr;		/* next free spot in input buffer */
223	int	ibufsize;	/* size of ibuf (not include error bytes) */
224	int	ierroff;	/* offset of error bytes in ibuf */
225
226	struct lbq	obufq;	/* head of queue of output buffers */
227	struct lbq	obufs[2];	/* output buffers */
228
229	bus_space_tag_t		bst;
230	bus_space_handle_t	bsh;
231
232	Port_t	data_port;	/* i/o ports */
233#ifdef COM_ESP
234	Port_t	esp_port;
235#endif
236	Port_t	int_id_port;
237	Port_t	modem_ctl_port;
238	Port_t	line_status_port;
239	Port_t	modem_status_port;
240	Port_t	intr_ctl_port;	/* Ports of IIR register */
241
242	struct tty	*tp;	/* cross reference */
243
244	/* Initial state. */
245	struct termios	it_in;	/* should be in struct tty */
246	struct termios	it_out;
247
248	/* Lock state. */
249	struct termios	lt_in;	/* should be in struct tty */
250	struct termios	lt_out;
251
252	bool_t	do_timestamp;
253	bool_t	do_dcd_timestamp;
254	struct timeval	timestamp;
255	struct timeval	dcd_timestamp;
256	struct	pps_state pps;
257	int	pps_bit;
258
259	u_long	bytes_in;	/* statistics */
260	u_long	bytes_out;
261	u_int	delta_error_counts[CE_NTYPES];
262	u_long	error_counts[CE_NTYPES];
263
264	u_long	rclk;
265
266	struct resource *irqres;
267	struct resource *ioportres;
268	int	ioportrid;
269	void	*cookie;
270	dev_t	devs[6];
271
272	/*
273	 * Data area for output buffers.  Someday we should build the output
274	 * buffer queue without copying data.
275	 */
276	u_char	obuf1[256];
277	u_char	obuf2[256];
278};
279
280#ifdef COM_ESP
281static	int	espattach(struct com_s *com, Port_t esp_port);
282#endif
283
284static	timeout_t siobusycheck;
285static	u_int	siodivisor(u_long rclk, speed_t speed);
286static	timeout_t siodtrwakeup;
287static	void	comhardclose(struct com_s *com);
288static	void	sioinput(struct com_s *com);
289static	void	siointr1(struct com_s *com);
290static	void	siointr(void *arg);
291static	int	commctl(struct com_s *com, int bits, int how);
292static	int	comparam(struct tty *tp, struct termios *t);
293static	void	siopoll(void *);
294static	void	siosettimeout(void);
295static	int	siosetwater(struct com_s *com, speed_t speed);
296static	void	comstart(struct tty *tp);
297static	void	comstop(struct tty *tp, int rw);
298static	timeout_t comwakeup;
299static	void	disc_optim(struct tty *tp, struct termios *t,
300		    struct com_s *com);
301
302char		sio_driver_name[] = "sio";
303static struct	mtx sio_lock;
304static int	sio_inited;
305
306/* table and macro for fast conversion from a unit number to its com struct */
307devclass_t	sio_devclass;
308#define	com_addr(unit)	((struct com_s *) \
309			 devclass_get_softc(sio_devclass, unit)) /* XXX */
310
311static	d_open_t	sioopen;
312static	d_close_t	sioclose;
313static	d_read_t	sioread;
314static	d_write_t	siowrite;
315static	d_ioctl_t	sioioctl;
316
317#define	CDEV_MAJOR	28
318static struct cdevsw sio_cdevsw = {
319	.d_open =	sioopen,
320	.d_close =	sioclose,
321	.d_read =	sioread,
322	.d_write =	siowrite,
323	.d_ioctl =	sioioctl,
324	.d_poll =	ttypoll,
325	.d_name =	sio_driver_name,
326	.d_maj =	CDEV_MAJOR,
327	.d_flags =	D_TTY,
328	.d_kqfilter =	ttykqfilter,
329};
330
331int	comconsole = -1;
332static	volatile speed_t	comdefaultrate = CONSPEED;
333static	u_long			comdefaultrclk = DEFAULT_RCLK;
334SYSCTL_ULONG(_machdep, OID_AUTO, conrclk, CTLFLAG_RW, &comdefaultrclk, 0, "");
335static	speed_t			gdbdefaultrate = GDBSPEED;
336SYSCTL_UINT(_machdep, OID_AUTO, gdbspeed, CTLFLAG_RW,
337	    &gdbdefaultrate, GDBSPEED, "");
338static	u_int	com_events;	/* input chars + weighted output completions */
339static	Port_t	siocniobase;
340static	int	siocnunit = -1;
341static	Port_t	siogdbiobase;
342static	int	siogdbunit = -1;
343static	void	*sio_slow_ih;
344static	void	*sio_fast_ih;
345static	int	sio_timeout;
346static	int	sio_timeouts_until_log;
347static	struct	callout_handle sio_timeout_handle
348    = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle);
349static	int	sio_numunits;
350
351#ifdef COM_ESP
352/* XXX configure this properly. */
353/* XXX quite broken for new-bus. */
354static	Port_t	likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, };
355static	Port_t	likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 };
356#endif
357
358/*
359 * handle sysctl read/write requests for console speed
360 *
361 * In addition to setting comdefaultrate for I/O through /dev/console,
362 * also set the initial and lock values for the /dev/ttyXX device
363 * if there is one associated with the console.  Finally, if the /dev/tty
364 * device has already been open, change the speed on the open running port
365 * itself.
366 */
367
368static int
369sysctl_machdep_comdefaultrate(SYSCTL_HANDLER_ARGS)
370{
371	int error, s;
372	speed_t newspeed;
373	struct com_s *com;
374	struct tty *tp;
375
376	newspeed = comdefaultrate;
377
378	error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req);
379	if (error || !req->newptr)
380		return (error);
381
382	comdefaultrate = newspeed;
383
384	if (comconsole < 0)		/* serial console not selected? */
385		return (0);
386
387	com = com_addr(comconsole);
388	if (com == NULL)
389		return (ENXIO);
390
391	/*
392	 * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX
393	 * (note, the lock rates really are boolean -- if non-zero, disallow
394	 *  speed changes)
395	 */
396	com->it_in.c_ispeed  = com->it_in.c_ospeed =
397	com->lt_in.c_ispeed  = com->lt_in.c_ospeed =
398	com->it_out.c_ispeed = com->it_out.c_ospeed =
399	com->lt_out.c_ispeed = com->lt_out.c_ospeed = comdefaultrate;
400
401	/*
402	 * if we're open, change the running rate too
403	 */
404	tp = com->tp;
405	if (tp && (tp->t_state & TS_ISOPEN)) {
406		tp->t_termios.c_ispeed =
407		tp->t_termios.c_ospeed = comdefaultrate;
408		s = spltty();
409		error = comparam(tp, &tp->t_termios);
410		splx(s);
411	}
412	return error;
413}
414
415SYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW,
416	    0, 0, sysctl_machdep_comdefaultrate, "I", "");
417/* TUNABLE_INT("machdep.conspeed", &comdefaultrate); */
418
419#define SET_FLAG(dev, bit) device_set_flags(dev, device_get_flags(dev) | (bit))
420#define CLR_FLAG(dev, bit) device_set_flags(dev, device_get_flags(dev) & ~(bit))
421
422/*
423 *	Unload the driver and clear the table.
424 *	XXX this is mostly wrong.
425 *	XXX TODO:
426 *	This is usually called when the card is ejected, but
427 *	can be caused by a kldunload of a controller driver.
428 *	The idea is to reset the driver's view of the device
429 *	and ensure that any driver entry points such as
430 *	read and write do not hang.
431 */
432int
433siodetach(dev)
434	device_t	dev;
435{
436	struct com_s	*com;
437	int i;
438
439	com = (struct com_s *) device_get_softc(dev);
440	if (com == NULL) {
441		device_printf(dev, "NULL com in siounload\n");
442		return (0);
443	}
444	com->gone = 1;
445	for (i = 0 ; i < 6; i++)
446		destroy_dev(com->devs[i]);
447	if (com->irqres) {
448		bus_teardown_intr(dev, com->irqres, com->cookie);
449		bus_release_resource(dev, SYS_RES_IRQ, 0, com->irqres);
450	}
451	if (com->ioportres)
452		bus_release_resource(dev, SYS_RES_IOPORT, com->ioportrid,
453				     com->ioportres);
454	if (com->tp && (com->tp->t_state & TS_ISOPEN)) {
455		device_printf(dev, "still open, forcing close\n");
456		(*linesw[com->tp->t_line].l_close)(com->tp, 0);
457		com->tp->t_gen++;
458		ttyclose(com->tp);
459		ttwakeup(com->tp);
460		ttwwakeup(com->tp);
461	} else {
462		if (com->ibuf != NULL)
463			free(com->ibuf, M_DEVBUF);
464		device_set_softc(dev, NULL);
465		free(com, M_DEVBUF);
466	}
467	return (0);
468}
469
470int
471sioprobe(dev, xrid, rclk, noprobe)
472	device_t	dev;
473	int		xrid;
474	u_long		rclk;
475	int		noprobe;
476{
477#if 0
478	static bool_t	already_init;
479	device_t	xdev;
480#endif
481	struct com_s	*com;
482	u_int		divisor;
483	bool_t		failures[10];
484	int		fn;
485	device_t	idev;
486	Port_t		iobase;
487	intrmask_t	irqmap[4];
488	intrmask_t	irqs;
489	u_char		mcr_image;
490	int		result;
491	u_long		xirq;
492	u_int		flags = device_get_flags(dev);
493	int		rid;
494	struct resource *port;
495
496	rid = xrid;
497	port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
498				  0, ~0, IO_COMSIZE, RF_ACTIVE);
499	if (!port)
500		return (ENXIO);
501
502	com = malloc(sizeof(*com), M_DEVBUF, M_NOWAIT | M_ZERO);
503	if (com == NULL)
504		return (ENOMEM);
505	device_set_softc(dev, com);
506	com->bst = rman_get_bustag(port);
507	com->bsh = rman_get_bushandle(port);
508	if (rclk == 0)
509		rclk = DEFAULT_RCLK;
510	com->rclk = rclk;
511
512	while (sio_inited != 2)
513		if (atomic_cmpset_int(&sio_inited, 0, 1)) {
514			mtx_init(&sio_lock, sio_driver_name, NULL,
515			    (comconsole != -1) ?
516			    MTX_SPIN | MTX_QUIET : MTX_SPIN);
517			atomic_store_rel_int(&sio_inited, 2);
518		}
519
520#if 0
521	/*
522	 * XXX this is broken - when we are first called, there are no
523	 * previously configured IO ports.  We could hard code
524	 * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse.
525	 * This code has been doing nothing since the conversion since
526	 * "count" is zero the first time around.
527	 */
528	if (!already_init) {
529		/*
530		 * Turn off MCR_IENABLE for all likely serial ports.  An unused
531		 * port with its MCR_IENABLE gate open will inhibit interrupts
532		 * from any used port that shares the interrupt vector.
533		 * XXX the gate enable is elsewhere for some multiports.
534		 */
535		device_t *devs;
536		int count, i, xioport;
537
538		devclass_get_devices(sio_devclass, &devs, &count);
539		for (i = 0; i < count; i++) {
540			xdev = devs[i];
541			if (device_is_enabled(xdev) &&
542			    bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport,
543					     NULL) == 0)
544				outb(xioport + com_mcr, 0);
545		}
546		free(devs, M_TEMP);
547		already_init = TRUE;
548	}
549#endif
550
551	if (COM_LLCONSOLE(flags)) {
552		printf("sio%d: reserved for low-level i/o\n",
553		       device_get_unit(dev));
554		bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
555		device_set_softc(dev, NULL);
556		free(com, M_DEVBUF);
557		return (ENXIO);
558	}
559
560	/*
561	 * If the device is on a multiport card and has an AST/4
562	 * compatible interrupt control register, initialize this
563	 * register and prepare to leave MCR_IENABLE clear in the mcr.
564	 * Otherwise, prepare to set MCR_IENABLE in the mcr.
565	 * Point idev to the device struct giving the correct id_irq.
566	 * This is the struct for the master device if there is one.
567	 */
568	idev = dev;
569	mcr_image = MCR_IENABLE;
570#ifdef COM_MULTIPORT
571	if (COM_ISMULTIPORT(flags)) {
572		Port_t xiobase;
573		u_long io;
574
575		idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags));
576		if (idev == NULL) {
577			printf("sio%d: master device %d not configured\n",
578			       device_get_unit(dev), COM_MPMASTER(flags));
579			idev = dev;
580		}
581		if (!COM_NOTAST4(flags)) {
582			if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io,
583					     NULL) == 0) {
584				xiobase = io;
585				if (bus_get_resource(idev, SYS_RES_IRQ, 0,
586				    NULL, NULL) == 0)
587					outb(xiobase + com_scr, 0x80);
588				else
589					outb(xiobase + com_scr, 0);
590			}
591			mcr_image = 0;
592		}
593	}
594#endif /* COM_MULTIPORT */
595	if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0)
596		mcr_image = 0;
597
598	bzero(failures, sizeof failures);
599	iobase = rman_get_start(port);
600
601	/*
602	 * We don't want to get actual interrupts, just masked ones.
603	 * Interrupts from this line should already be masked in the ICU,
604	 * but mask them in the processor as well in case there are some
605	 * (misconfigured) shared interrupts.
606	 */
607	mtx_lock_spin(&sio_lock);
608/* EXTRA DELAY? */
609
610	/*
611	 * For the TI16754 chips, set prescaler to 1 (4 is often the
612	 * default after-reset value) as otherwise it's impossible to
613	 * get highest baudrates.
614	 */
615	if (COM_TI16754(flags)) {
616		u_char cfcr, efr;
617
618		cfcr = sio_getreg(com, com_cfcr);
619		sio_setreg(com, com_cfcr, CFCR_EFR_ENABLE);
620		efr = sio_getreg(com, com_efr);
621		/* Unlock extended features to turn off prescaler. */
622		sio_setreg(com, com_efr, efr | EFR_EFE);
623		/* Disable EFR. */
624		sio_setreg(com, com_cfcr, (cfcr != CFCR_EFR_ENABLE) ? cfcr : 0);
625		/* Turn off prescaler. */
626		sio_setreg(com, com_mcr,
627			   sio_getreg(com, com_mcr) & ~MCR_PRESCALE);
628		sio_setreg(com, com_cfcr, CFCR_EFR_ENABLE);
629		sio_setreg(com, com_efr, efr);
630		sio_setreg(com, com_cfcr, cfcr);
631	}
632
633	/*
634	 * Initialize the speed and the word size and wait long enough to
635	 * drain the maximum of 16 bytes of junk in device output queues.
636	 * The speed is undefined after a master reset and must be set
637	 * before relying on anything related to output.  There may be
638	 * junk after a (very fast) soft reboot and (apparently) after
639	 * master reset.
640	 * XXX what about the UART bug avoided by waiting in comparam()?
641	 * We don't want to to wait long enough to drain at 2 bps.
642	 */
643	if (iobase == siocniobase)
644		DELAY((16 + 1) * 1000000 / (comdefaultrate / 10));
645	else {
646		sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS);
647		divisor = siodivisor(rclk, SIO_TEST_SPEED);
648		sio_setreg(com, com_dlbl, divisor & 0xff);
649		sio_setreg(com, com_dlbh, divisor >> 8);
650		sio_setreg(com, com_cfcr, CFCR_8BITS);
651		DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10));
652	}
653
654	/*
655	 * Enable the interrupt gate and disable device interupts.  This
656	 * should leave the device driving the interrupt line low and
657	 * guarantee an edge trigger if an interrupt can be generated.
658	 */
659/* EXTRA DELAY? */
660	sio_setreg(com, com_mcr, mcr_image);
661	sio_setreg(com, com_ier, 0);
662	DELAY(1000);		/* XXX */
663	irqmap[0] = isa_irq_pending();
664
665	/*
666	 * Attempt to set loopback mode so that we can send a null byte
667	 * without annoying any external device.
668	 */
669/* EXTRA DELAY? */
670	sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK);
671
672	/*
673	 * Attempt to generate an output interrupt.  On 8250's, setting
674	 * IER_ETXRDY generates an interrupt independent of the current
675	 * setting and independent of whether the THR is empty.  On 16450's,
676	 * setting IER_ETXRDY generates an interrupt independent of the
677	 * current setting.  On 16550A's, setting IER_ETXRDY only
678	 * generates an interrupt when IER_ETXRDY is not already set.
679	 */
680	sio_setreg(com, com_ier, IER_ETXRDY);
681
682	/*
683	 * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate
684	 * an interrupt.  They'd better generate one for actually doing
685	 * output.  Loopback may be broken on the same incompatibles but
686	 * it's unlikely to do more than allow the null byte out.
687	 */
688	sio_setreg(com, com_data, 0);
689	DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10));
690
691	/*
692	 * Turn off loopback mode so that the interrupt gate works again
693	 * (MCR_IENABLE was hidden).  This should leave the device driving
694	 * an interrupt line high.  It doesn't matter if the interrupt
695	 * line oscillates while we are not looking at it, since interrupts
696	 * are disabled.
697	 */
698/* EXTRA DELAY? */
699	sio_setreg(com, com_mcr, mcr_image);
700
701	/*
702	 * It seems my Xircom CBEM56G Cardbus modem wants to be reset
703	 * to 8 bits *again*, or else probe test 0 will fail.
704	 * gwk@sgi.com, 4/19/2001
705	 */
706	sio_setreg(com, com_cfcr, CFCR_8BITS);
707
708	/*
709	 * Some pcmcia cards have the "TXRDY bug", so we check everyone
710	 * for IIR_TXRDY implementation ( Palido 321s, DC-1S... )
711	 */
712	if (noprobe) {
713		/* Reading IIR register twice */
714		for (fn = 0; fn < 2; fn ++) {
715			DELAY(10000);
716			failures[6] = sio_getreg(com, com_iir);
717		}
718		/* Check IIR_TXRDY clear ? */
719		result = 0;
720		if (failures[6] & IIR_TXRDY) {
721			/* No, Double check with clearing IER */
722			sio_setreg(com, com_ier, 0);
723			if (sio_getreg(com, com_iir) & IIR_NOPEND) {
724				/* Ok. We discovered TXRDY bug! */
725				SET_FLAG(dev, COM_C_IIR_TXRDYBUG);
726			} else {
727				/* Unknown, Just omit this chip.. XXX */
728				result = ENXIO;
729				sio_setreg(com, com_mcr, 0);
730			}
731		} else {
732			/* OK. this is well-known guys */
733			CLR_FLAG(dev, COM_C_IIR_TXRDYBUG);
734		}
735		sio_setreg(com, com_ier, 0);
736		sio_setreg(com, com_cfcr, CFCR_8BITS);
737		mtx_unlock_spin(&sio_lock);
738		bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
739		if (iobase == siocniobase)
740			result = 0;
741		if (result != 0) {
742			device_set_softc(dev, NULL);
743			free(com, M_DEVBUF);
744		}
745		return (result);
746	}
747
748	/*
749	 * Check that
750	 *	o the CFCR, IER and MCR in UART hold the values written to them
751	 *	  (the values happen to be all distinct - this is good for
752	 *	  avoiding false positive tests from bus echoes).
753	 *	o an output interrupt is generated and its vector is correct.
754	 *	o the interrupt goes away when the IIR in the UART is read.
755	 */
756/* EXTRA DELAY? */
757	failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS;
758	failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY;
759	failures[2] = sio_getreg(com, com_mcr) - mcr_image;
760	DELAY(10000);		/* Some internal modems need this time */
761	irqmap[1] = isa_irq_pending();
762	failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY;
763	DELAY(1000);		/* XXX */
764	irqmap[2] = isa_irq_pending();
765	failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND;
766
767	/*
768	 * Turn off all device interrupts and check that they go off properly.
769	 * Leave MCR_IENABLE alone.  For ports without a master port, it gates
770	 * the OUT2 output of the UART to
771	 * the ICU input.  Closing the gate would give a floating ICU input
772	 * (unless there is another device driving it) and spurious interrupts.
773	 * (On the system that this was first tested on, the input floats high
774	 * and gives a (masked) interrupt as soon as the gate is closed.)
775	 */
776	sio_setreg(com, com_ier, 0);
777	sio_setreg(com, com_cfcr, CFCR_8BITS);	/* dummy to avoid bus echo */
778	failures[7] = sio_getreg(com, com_ier);
779	DELAY(1000);		/* XXX */
780	irqmap[3] = isa_irq_pending();
781	failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND;
782
783	mtx_unlock_spin(&sio_lock);
784
785	irqs = irqmap[1] & ~irqmap[0];
786	if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 &&
787	    ((1 << xirq) & irqs) == 0) {
788		printf(
789		"sio%d: configured irq %ld not in bitmap of probed irqs %#x\n",
790		    device_get_unit(dev), xirq, irqs);
791		printf(
792		"sio%d: port may not be enabled\n",
793		    device_get_unit(dev));
794	}
795	if (bootverbose)
796		printf("sio%d: irq maps: %#x %#x %#x %#x\n",
797		    device_get_unit(dev),
798		    irqmap[0], irqmap[1], irqmap[2], irqmap[3]);
799
800	result = 0;
801	for (fn = 0; fn < sizeof failures; ++fn)
802		if (failures[fn]) {
803			sio_setreg(com, com_mcr, 0);
804			result = ENXIO;
805			if (bootverbose) {
806				printf("sio%d: probe failed test(s):",
807				    device_get_unit(dev));
808				for (fn = 0; fn < sizeof failures; ++fn)
809					if (failures[fn])
810						printf(" %d", fn);
811				printf("\n");
812			}
813			break;
814		}
815	bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
816	if (iobase == siocniobase)
817		result = 0;
818	if (result != 0) {
819		device_set_softc(dev, NULL);
820		free(com, M_DEVBUF);
821	}
822	return (result);
823}
824
825#ifdef COM_ESP
826static int
827espattach(com, esp_port)
828	struct com_s		*com;
829	Port_t			esp_port;
830{
831	u_char	dips;
832	u_char	val;
833
834	/*
835	 * Check the ESP-specific I/O port to see if we're an ESP
836	 * card.  If not, return failure immediately.
837	 */
838	if ((inb(esp_port) & 0xf3) == 0) {
839		printf(" port 0x%x is not an ESP board?\n", esp_port);
840		return (0);
841	}
842
843	/*
844	 * We've got something that claims to be a Hayes ESP card.
845	 * Let's hope so.
846	 */
847
848	/* Get the dip-switch configuration */
849	outb(esp_port + ESP_CMD1, ESP_GETDIPS);
850	dips = inb(esp_port + ESP_STATUS1);
851
852	/*
853	 * Bits 0,1 of dips say which COM port we are.
854	 */
855	if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03])
856		printf(" : ESP");
857	else {
858		printf(" esp_port has com %d\n", dips & 0x03);
859		return (0);
860	}
861
862	/*
863	 * Check for ESP version 2.0 or later:  bits 4,5,6 = 010.
864	 */
865	outb(esp_port + ESP_CMD1, ESP_GETTEST);
866	val = inb(esp_port + ESP_STATUS1);	/* clear reg 1 */
867	val = inb(esp_port + ESP_STATUS2);
868	if ((val & 0x70) < 0x20) {
869		printf("-old (%o)", val & 0x70);
870		return (0);
871	}
872
873	/*
874	 * Check for ability to emulate 16550:  bit 7 == 1
875	 */
876	if ((dips & 0x80) == 0) {
877		printf(" slave");
878		return (0);
879	}
880
881	/*
882	 * Okay, we seem to be a Hayes ESP card.  Whee.
883	 */
884	com->esp = TRUE;
885	com->esp_port = esp_port;
886	return (1);
887}
888#endif /* COM_ESP */
889
890int
891sioattach(dev, xrid, rclk)
892	device_t	dev;
893	int		xrid;
894	u_long		rclk;
895{
896	struct com_s	*com;
897#ifdef COM_ESP
898	Port_t		*espp;
899#endif
900	Port_t		iobase;
901	int		minorbase;
902	int		unit;
903	u_int		flags;
904	int		rid;
905	struct resource *port;
906	int		ret;
907
908	rid = xrid;
909	port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
910				  0, ~0, IO_COMSIZE, RF_ACTIVE);
911	if (!port)
912		return (ENXIO);
913
914	iobase = rman_get_start(port);
915	unit = device_get_unit(dev);
916	com = device_get_softc(dev);
917	flags = device_get_flags(dev);
918
919	if (unit >= sio_numunits)
920		sio_numunits = unit + 1;
921	/*
922	 * sioprobe() has initialized the device registers as follows:
923	 *	o cfcr = CFCR_8BITS.
924	 *	  It is most important that CFCR_DLAB is off, so that the
925	 *	  data port is not hidden when we enable interrupts.
926	 *	o ier = 0.
927	 *	  Interrupts are only enabled when the line is open.
928	 *	o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible
929	 *	  interrupt control register or the config specifies no irq.
930	 *	  Keeping MCR_DTR and MCR_RTS off might stop the external
931	 *	  device from sending before we are ready.
932	 */
933	bzero(com, sizeof *com);
934	com->unit = unit;
935	com->ioportres = port;
936	com->ioportrid = rid;
937	com->bst = rman_get_bustag(port);
938	com->bsh = rman_get_bushandle(port);
939	com->cfcr_image = CFCR_8BITS;
940	com->dtr_wait = 3 * hz;
941	com->loses_outints = COM_LOSESOUTINTS(flags) != 0;
942	com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0;
943	com->tx_fifo_size = 1;
944	com->obufs[0].l_head = com->obuf1;
945	com->obufs[1].l_head = com->obuf2;
946
947	com->data_port = iobase + com_data;
948	com->int_id_port = iobase + com_iir;
949	com->modem_ctl_port = iobase + com_mcr;
950	com->mcr_image = inb(com->modem_ctl_port);
951	com->line_status_port = iobase + com_lsr;
952	com->modem_status_port = iobase + com_msr;
953	com->intr_ctl_port = iobase + com_ier;
954
955	if (rclk == 0)
956		rclk = DEFAULT_RCLK;
957	com->rclk = rclk;
958
959	/*
960	 * We don't use all the flags from <sys/ttydefaults.h> since they
961	 * are only relevant for logins.  It's important to have echo off
962	 * initially so that the line doesn't start blathering before the
963	 * echo flag can be turned off.
964	 */
965	com->it_in.c_iflag = 0;
966	com->it_in.c_oflag = 0;
967	com->it_in.c_cflag = TTYDEF_CFLAG;
968	com->it_in.c_lflag = 0;
969	if (unit == comconsole) {
970		com->it_in.c_iflag = TTYDEF_IFLAG;
971		com->it_in.c_oflag = TTYDEF_OFLAG;
972		com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
973		com->it_in.c_lflag = TTYDEF_LFLAG;
974		com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
975		com->lt_out.c_ispeed = com->lt_out.c_ospeed =
976		com->lt_in.c_ispeed = com->lt_in.c_ospeed =
977		com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
978	} else
979		com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED;
980	if (siosetwater(com, com->it_in.c_ispeed) != 0) {
981		mtx_unlock_spin(&sio_lock);
982		/*
983		 * Leave i/o resources allocated if this is a `cn'-level
984		 * console, so that other devices can't snarf them.
985		 */
986		if (iobase != siocniobase)
987			bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
988		return (ENOMEM);
989	}
990	mtx_unlock_spin(&sio_lock);
991	termioschars(&com->it_in);
992	com->it_out = com->it_in;
993
994	/* attempt to determine UART type */
995	printf("sio%d: type", unit);
996
997
998	if (!COM_ISMULTIPORT(flags) &&
999	    !COM_IIR_TXRDYBUG(flags) && !COM_NOSCR(flags)) {
1000		u_char	scr;
1001		u_char	scr1;
1002		u_char	scr2;
1003
1004		scr = sio_getreg(com, com_scr);
1005		sio_setreg(com, com_scr, 0xa5);
1006		scr1 = sio_getreg(com, com_scr);
1007		sio_setreg(com, com_scr, 0x5a);
1008		scr2 = sio_getreg(com, com_scr);
1009		sio_setreg(com, com_scr, scr);
1010		if (scr1 != 0xa5 || scr2 != 0x5a) {
1011			printf(" 8250 or not responding");
1012			goto determined_type;
1013		}
1014	}
1015	sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH);
1016	DELAY(100);
1017	com->st16650a = 0;
1018	switch (inb(com->int_id_port) & IIR_FIFO_MASK) {
1019	case FIFO_RX_LOW:
1020		printf(" 16450");
1021		break;
1022	case FIFO_RX_MEDL:
1023		printf(" 16450?");
1024		break;
1025	case FIFO_RX_MEDH:
1026		printf(" 16550?");
1027		break;
1028	case FIFO_RX_HIGH:
1029		if (COM_NOFIFO(flags)) {
1030			printf(" 16550A fifo disabled");
1031		} else {
1032			com->hasfifo = TRUE;
1033			if (COM_ST16650A(flags)) {
1034				com->st16650a = 1;
1035				com->tx_fifo_size = 32;
1036				printf(" ST16650A");
1037			} else if (COM_TI16754(flags)) {
1038				com->tx_fifo_size = 64;
1039				printf(" TI16754");
1040			} else {
1041				com->tx_fifo_size = COM_FIFOSIZE(flags);
1042				printf(" 16550A");
1043			}
1044		}
1045#ifdef COM_ESP
1046		for (espp = likely_esp_ports; *espp != 0; espp++)
1047			if (espattach(com, *espp)) {
1048				com->tx_fifo_size = 1024;
1049				break;
1050			}
1051#endif
1052		if (!com->st16650a && !COM_TI16754(flags)) {
1053			if (!com->tx_fifo_size)
1054				com->tx_fifo_size = 16;
1055			else
1056				printf(" lookalike with %d bytes FIFO",
1057				    com->tx_fifo_size);
1058		}
1059
1060		break;
1061	}
1062
1063#ifdef COM_ESP
1064	if (com->esp) {
1065		/*
1066		 * Set 16550 compatibility mode.
1067		 * We don't use the ESP_MODE_SCALE bit to increase the
1068		 * fifo trigger levels because we can't handle large
1069		 * bursts of input.
1070		 * XXX flow control should be set in comparam(), not here.
1071		 */
1072		outb(com->esp_port + ESP_CMD1, ESP_SETMODE);
1073		outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO);
1074
1075		/* Set RTS/CTS flow control. */
1076		outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE);
1077		outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS);
1078		outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS);
1079
1080		/* Set flow-control levels. */
1081		outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW);
1082		outb(com->esp_port + ESP_CMD2, HIBYTE(768));
1083		outb(com->esp_port + ESP_CMD2, LOBYTE(768));
1084		outb(com->esp_port + ESP_CMD2, HIBYTE(512));
1085		outb(com->esp_port + ESP_CMD2, LOBYTE(512));
1086	}
1087#endif /* COM_ESP */
1088	sio_setreg(com, com_fifo, 0);
1089determined_type: ;
1090
1091#ifdef COM_MULTIPORT
1092	if (COM_ISMULTIPORT(flags)) {
1093		device_t masterdev;
1094
1095		com->multiport = TRUE;
1096		printf(" (multiport");
1097		if (unit == COM_MPMASTER(flags))
1098			printf(" master");
1099		printf(")");
1100		masterdev = devclass_get_device(sio_devclass,
1101		    COM_MPMASTER(flags));
1102		com->no_irq = (masterdev == NULL || bus_get_resource(masterdev,
1103		    SYS_RES_IRQ, 0, NULL, NULL) != 0);
1104	 }
1105#endif /* COM_MULTIPORT */
1106	if (unit == comconsole)
1107		printf(", console");
1108	if (COM_IIR_TXRDYBUG(flags))
1109		printf(" with a bogus IIR_TXRDY register");
1110	printf("\n");
1111
1112	if (sio_fast_ih == NULL) {
1113		swi_add(&tty_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0,
1114		    &sio_fast_ih);
1115		swi_add(&clk_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0,
1116		    &sio_slow_ih);
1117	}
1118	minorbase = UNIT_TO_MINOR(unit);
1119	com->devs[0] = make_dev(&sio_cdevsw, minorbase,
1120	    UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit);
1121	com->devs[1] = make_dev(&sio_cdevsw, minorbase | CONTROL_INIT_STATE,
1122	    UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit);
1123	com->devs[2] = make_dev(&sio_cdevsw, minorbase | CONTROL_LOCK_STATE,
1124	    UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit);
1125	com->devs[3] = make_dev(&sio_cdevsw, minorbase | CALLOUT_MASK,
1126	    UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit);
1127	com->devs[4] = make_dev(&sio_cdevsw,
1128	    minorbase | CALLOUT_MASK | CONTROL_INIT_STATE,
1129	    UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit);
1130	com->devs[5] = make_dev(&sio_cdevsw,
1131	    minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE,
1132	    UID_UUCP, GID_DIALER, 0660, "cuala%r", unit);
1133	for (rid = 0; rid < 6; rid++)
1134		com->devs[rid]->si_drv1 = com;
1135	com->flags = flags;
1136	com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
1137
1138	if (COM_PPSCTS(flags))
1139		com->pps_bit = MSR_CTS;
1140	else
1141		com->pps_bit = MSR_DCD;
1142	pps_init(&com->pps);
1143
1144	rid = 0;
1145	com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1,
1146	    RF_ACTIVE);
1147	if (com->irqres) {
1148		ret = BUS_SETUP_INTR(device_get_parent(dev), dev, com->irqres,
1149				     INTR_TYPE_TTY | INTR_FAST,
1150				     siointr, com, &com->cookie);
1151		if (ret) {
1152			ret = BUS_SETUP_INTR(device_get_parent(dev), dev,
1153					     com->irqres, INTR_TYPE_TTY,
1154					     siointr, com, &com->cookie);
1155			if (ret == 0)
1156				device_printf(dev, "unable to activate interrupt in fast mode - using normal mode\n");
1157		}
1158		if (ret)
1159			device_printf(dev, "could not activate interrupt\n");
1160#if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \
1161    defined(ALT_BREAK_TO_DEBUGGER))
1162		/*
1163		 * Enable interrupts for early break-to-debugger support
1164		 * on the console.
1165		 */
1166		if (ret == 0 && unit == comconsole)
1167			outb(siocniobase + com_ier, IER_ERXRDY | IER_ERLS |
1168			    IER_EMSC);
1169#endif
1170	}
1171
1172	return (0);
1173}
1174
1175static int
1176sioopen(dev, flag, mode, td)
1177	dev_t		dev;
1178	int		flag;
1179	int		mode;
1180	struct thread	*td;
1181{
1182	struct com_s	*com;
1183	int		error;
1184	int		mynor;
1185	int		s;
1186	struct tty	*tp;
1187	int		unit;
1188
1189	mynor = minor(dev);
1190	unit = MINOR_TO_UNIT(mynor);
1191	com = com_addr(unit);
1192	if (com == NULL)
1193		return (ENXIO);
1194	if (com->gone)
1195		return (ENXIO);
1196	if (mynor & CONTROL_MASK)
1197		return (0);
1198	tp = dev->si_tty = com->tp = ttymalloc(com->tp);
1199	s = spltty();
1200	/*
1201	 * We jump to this label after all non-interrupted sleeps to pick
1202	 * up any changes of the device state.
1203	 */
1204open_top:
1205	while (com->state & CS_DTR_OFF) {
1206		error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "siodtr", 0);
1207		if (com_addr(unit) == NULL)
1208			return (ENXIO);
1209		if (error != 0 || com->gone)
1210			goto out;
1211	}
1212	if (tp->t_state & TS_ISOPEN) {
1213		/*
1214		 * The device is open, so everything has been initialized.
1215		 * Handle conflicts.
1216		 */
1217		if (mynor & CALLOUT_MASK) {
1218			if (!com->active_out) {
1219				error = EBUSY;
1220				goto out;
1221			}
1222		} else {
1223			if (com->active_out) {
1224				if (flag & O_NONBLOCK) {
1225					error = EBUSY;
1226					goto out;
1227				}
1228				error =	tsleep(&com->active_out,
1229					       TTIPRI | PCATCH, "siobi", 0);
1230				if (com_addr(unit) == NULL)
1231					return (ENXIO);
1232				if (error != 0 || com->gone)
1233					goto out;
1234				goto open_top;
1235			}
1236		}
1237		if (tp->t_state & TS_XCLUDE &&
1238		    suser(td)) {
1239			error = EBUSY;
1240			goto out;
1241		}
1242	} else {
1243		/*
1244		 * The device isn't open, so there are no conflicts.
1245		 * Initialize it.  Initialization is done twice in many
1246		 * cases: to preempt sleeping callin opens if we are
1247		 * callout, and to complete a callin open after DCD rises.
1248		 */
1249		tp->t_oproc = comstart;
1250		tp->t_param = comparam;
1251		tp->t_stop = comstop;
1252		tp->t_dev = dev;
1253		tp->t_termios = mynor & CALLOUT_MASK
1254				? com->it_out : com->it_in;
1255		(void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
1256		com->poll = com->no_irq;
1257		com->poll_output = com->loses_outints;
1258		++com->wopeners;
1259		error = comparam(tp, &tp->t_termios);
1260		--com->wopeners;
1261		if (error != 0)
1262			goto out;
1263		/*
1264		 * XXX we should goto open_top if comparam() slept.
1265		 */
1266		if (com->hasfifo) {
1267			int i;
1268			/*
1269			 * (Re)enable and drain fifos.
1270			 *
1271			 * Certain SMC chips cause problems if the fifos
1272			 * are enabled while input is ready.  Turn off the
1273			 * fifo if necessary to clear the input.  We test
1274			 * the input ready bit after enabling the fifos
1275			 * since we've already enabled them in comparam()
1276			 * and to handle races between enabling and fresh
1277			 * input.
1278			 */
1279			for (i = 0; i < 500; i++) {
1280				sio_setreg(com, com_fifo,
1281					   FIFO_RCV_RST | FIFO_XMT_RST
1282					   | com->fifo_image);
1283				/*
1284				 * XXX the delays are for superstitious
1285				 * historical reasons.  It must be less than
1286				 * the character time at the maximum
1287				 * supported speed (87 usec at 115200 bps
1288				 * 8N1).  Otherwise we might loop endlessly
1289				 * if data is streaming in.  We used to use
1290				 * delays of 100.  That usually worked
1291				 * because DELAY(100) used to usually delay
1292				 * for about 85 usec instead of 100.
1293				 */
1294				DELAY(50);
1295				if (!(inb(com->line_status_port) & LSR_RXRDY))
1296					break;
1297				sio_setreg(com, com_fifo, 0);
1298				DELAY(50);
1299				(void) inb(com->data_port);
1300			}
1301			if (i == 500) {
1302				error = EIO;
1303				goto out;
1304			}
1305		}
1306
1307		mtx_lock_spin(&sio_lock);
1308		(void) inb(com->line_status_port);
1309		(void) inb(com->data_port);
1310		com->prev_modem_status = com->last_modem_status
1311		    = inb(com->modem_status_port);
1312		if (COM_IIR_TXRDYBUG(com->flags)) {
1313			outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS
1314						| IER_EMSC);
1315		} else {
1316			outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY
1317						| IER_ERLS | IER_EMSC);
1318		}
1319		mtx_unlock_spin(&sio_lock);
1320		/*
1321		 * Handle initial DCD.  Callout devices get a fake initial
1322		 * DCD (trapdoor DCD).  If we are callout, then any sleeping
1323		 * callin opens get woken up and resume sleeping on "siobi"
1324		 * instead of "siodcd".
1325		 */
1326		/*
1327		 * XXX `mynor & CALLOUT_MASK' should be
1328		 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
1329		 * TRAPDOOR_CARRIER is the default initial state for callout
1330		 * devices and SOFT_CARRIER is like CLOCAL except it hides
1331		 * the true carrier.
1332		 */
1333		if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
1334			(*linesw[tp->t_line].l_modem)(tp, 1);
1335	}
1336	/*
1337	 * Wait for DCD if necessary.
1338	 */
1339	if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
1340	    && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
1341		++com->wopeners;
1342		error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "siodcd", 0);
1343		if (com_addr(unit) == NULL)
1344			return (ENXIO);
1345		--com->wopeners;
1346		if (error != 0 || com->gone)
1347			goto out;
1348		goto open_top;
1349	}
1350	error =	(*linesw[tp->t_line].l_open)(dev, tp);
1351	disc_optim(tp, &tp->t_termios, com);
1352	if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
1353		com->active_out = TRUE;
1354	siosettimeout();
1355out:
1356	splx(s);
1357	if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
1358		comhardclose(com);
1359	return (error);
1360}
1361
1362static int
1363sioclose(dev, flag, mode, td)
1364	dev_t		dev;
1365	int		flag;
1366	int		mode;
1367	struct thread	*td;
1368{
1369	struct com_s	*com;
1370	int		mynor;
1371	int		s;
1372	struct tty	*tp;
1373
1374	mynor = minor(dev);
1375	if (mynor & CONTROL_MASK)
1376		return (0);
1377	com = com_addr(MINOR_TO_UNIT(mynor));
1378	if (com == NULL)
1379		return (ENODEV);
1380	tp = com->tp;
1381	s = spltty();
1382	(*linesw[tp->t_line].l_close)(tp, flag);
1383	disc_optim(tp, &tp->t_termios, com);
1384	comstop(tp, FREAD | FWRITE);
1385	comhardclose(com);
1386	ttyclose(tp);
1387	siosettimeout();
1388	splx(s);
1389	if (com->gone) {
1390		printf("sio%d: gone\n", com->unit);
1391		s = spltty();
1392		if (com->ibuf != NULL)
1393			free(com->ibuf, M_DEVBUF);
1394		bzero(tp, sizeof *tp);
1395		splx(s);
1396	}
1397	return (0);
1398}
1399
1400static void
1401comhardclose(com)
1402	struct com_s	*com;
1403{
1404	int		s;
1405	struct tty	*tp;
1406
1407	s = spltty();
1408	com->poll = FALSE;
1409	com->poll_output = FALSE;
1410	com->do_timestamp = FALSE;
1411	com->do_dcd_timestamp = FALSE;
1412	com->pps.ppsparam.mode = 0;
1413	sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1414	tp = com->tp;
1415
1416#if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \
1417    defined(ALT_BREAK_TO_DEBUGGER))
1418	/*
1419	 * Leave interrupts enabled and don't clear DTR if this is the
1420	 * console. This allows us to detect break-to-debugger events
1421	 * while the console device is closed.
1422	 */
1423	if (com->unit != comconsole)
1424#endif
1425	{
1426		sio_setreg(com, com_ier, 0);
1427		if (tp->t_cflag & HUPCL
1428		    /*
1429		     * XXX we will miss any carrier drop between here and the
1430		     * next open.  Perhaps we should watch DCD even when the
1431		     * port is closed; it is not sufficient to check it at
1432		     * the next open because it might go up and down while
1433		     * we're not watching.
1434		     */
1435		    || (!com->active_out
1436		        && !(com->prev_modem_status & MSR_DCD)
1437		        && !(com->it_in.c_cflag & CLOCAL))
1438		    || !(tp->t_state & TS_ISOPEN)) {
1439			(void)commctl(com, TIOCM_DTR, DMBIC);
1440			if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
1441				timeout(siodtrwakeup, com, com->dtr_wait);
1442				com->state |= CS_DTR_OFF;
1443			}
1444		}
1445	}
1446	if (com->hasfifo) {
1447		/*
1448		 * Disable fifos so that they are off after controlled
1449		 * reboots.  Some BIOSes fail to detect 16550s when the
1450		 * fifos are enabled.
1451		 */
1452		sio_setreg(com, com_fifo, 0);
1453	}
1454	com->active_out = FALSE;
1455	wakeup(&com->active_out);
1456	wakeup(TSA_CARR_ON(tp));	/* restart any wopeners */
1457	splx(s);
1458}
1459
1460static int
1461sioread(dev, uio, flag)
1462	dev_t		dev;
1463	struct uio	*uio;
1464	int		flag;
1465{
1466	int		mynor;
1467	struct com_s	*com;
1468
1469	mynor = minor(dev);
1470	if (mynor & CONTROL_MASK)
1471		return (ENODEV);
1472	com = com_addr(MINOR_TO_UNIT(mynor));
1473	if (com == NULL || com->gone)
1474		return (ENODEV);
1475	return ((*linesw[com->tp->t_line].l_read)(com->tp, uio, flag));
1476}
1477
1478static int
1479siowrite(dev, uio, flag)
1480	dev_t		dev;
1481	struct uio	*uio;
1482	int		flag;
1483{
1484	int		mynor;
1485	struct com_s	*com;
1486	int		unit;
1487
1488	mynor = minor(dev);
1489	if (mynor & CONTROL_MASK)
1490		return (ENODEV);
1491
1492	unit = MINOR_TO_UNIT(mynor);
1493	com = com_addr(unit);
1494	if (com == NULL || com->gone)
1495		return (ENODEV);
1496	/*
1497	 * (XXX) We disallow virtual consoles if the physical console is
1498	 * a serial port.  This is in case there is a display attached that
1499	 * is not the console.  In that situation we don't need/want the X
1500	 * server taking over the console.
1501	 */
1502	if (constty != NULL && unit == comconsole)
1503		constty = NULL;
1504	return ((*linesw[com->tp->t_line].l_write)(com->tp, uio, flag));
1505}
1506
1507static void
1508siobusycheck(chan)
1509	void	*chan;
1510{
1511	struct com_s	*com;
1512	int		s;
1513
1514	com = (struct com_s *)chan;
1515
1516	/*
1517	 * Clear TS_BUSY if low-level output is complete.
1518	 * spl locking is sufficient because siointr1() does not set CS_BUSY.
1519	 * If siointr1() clears CS_BUSY after we look at it, then we'll get
1520	 * called again.  Reading the line status port outside of siointr1()
1521	 * is safe because CS_BUSY is clear so there are no output interrupts
1522	 * to lose.
1523	 */
1524	s = spltty();
1525	if (com->state & CS_BUSY)
1526		com->extra_state &= ~CSE_BUSYCHECK;	/* False alarm. */
1527	else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY))
1528	    == (LSR_TSRE | LSR_TXRDY)) {
1529		com->tp->t_state &= ~TS_BUSY;
1530		ttwwakeup(com->tp);
1531		com->extra_state &= ~CSE_BUSYCHECK;
1532	} else
1533		timeout(siobusycheck, com, hz / 100);
1534	splx(s);
1535}
1536
1537static u_int
1538siodivisor(rclk, speed)
1539	u_long	rclk;
1540	speed_t	speed;
1541{
1542	long	actual_speed;
1543	u_int	divisor;
1544	int	error;
1545
1546	if (speed == 0)
1547		return (0);
1548#if UINT_MAX > (ULONG_MAX - 1) / 8
1549	if (speed > (ULONG_MAX - 1) / 8)
1550		return (0);
1551#endif
1552	divisor = (rclk / (8UL * speed) + 1) / 2;
1553	if (divisor == 0 || divisor >= 65536)
1554		return (0);
1555	actual_speed = rclk / (16UL * divisor);
1556
1557	/* 10 times error in percent: */
1558	error = ((actual_speed - (long)speed) * 2000 / (long)speed + 1) / 2;
1559
1560	/* 3.0% maximum error tolerance: */
1561	if (error < -30 || error > 30)
1562		return (0);
1563
1564	return (divisor);
1565}
1566
1567static void
1568siodtrwakeup(chan)
1569	void	*chan;
1570{
1571	struct com_s	*com;
1572
1573	com = (struct com_s *)chan;
1574	com->state &= ~CS_DTR_OFF;
1575	wakeup(&com->dtr_wait);
1576}
1577
1578/*
1579 * Call this function with the sio_lock mutex held.  It will return with the
1580 * lock still held.
1581 */
1582static void
1583sioinput(com)
1584	struct com_s	*com;
1585{
1586	u_char		*buf;
1587	int		incc;
1588	u_char		line_status;
1589	int		recv_data;
1590	struct tty	*tp;
1591
1592	buf = com->ibuf;
1593	tp = com->tp;
1594	if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) {
1595		com_events -= (com->iptr - com->ibuf);
1596		com->iptr = com->ibuf;
1597		return;
1598	}
1599	if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1600		/*
1601		 * Avoid the grotesquely inefficient lineswitch routine
1602		 * (ttyinput) in "raw" mode.  It usually takes about 450
1603		 * instructions (that's without canonical processing or echo!).
1604		 * slinput is reasonably fast (usually 40 instructions plus
1605		 * call overhead).
1606		 */
1607		do {
1608			/*
1609			 * This may look odd, but it is using save-and-enable
1610			 * semantics instead of the save-and-disable semantics
1611			 * that are used everywhere else.
1612			 */
1613			mtx_unlock_spin(&sio_lock);
1614			incc = com->iptr - buf;
1615			if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
1616			    && (com->state & CS_RTS_IFLOW
1617				|| tp->t_iflag & IXOFF)
1618			    && !(tp->t_state & TS_TBLOCK))
1619				ttyblock(tp);
1620			com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1621				+= b_to_q((char *)buf, incc, &tp->t_rawq);
1622			buf += incc;
1623			tk_nin += incc;
1624			tk_rawcc += incc;
1625			tp->t_rawcc += incc;
1626			ttwakeup(tp);
1627			if (tp->t_state & TS_TTSTOP
1628			    && (tp->t_iflag & IXANY
1629				|| tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1630				tp->t_state &= ~TS_TTSTOP;
1631				tp->t_lflag &= ~FLUSHO;
1632				comstart(tp);
1633			}
1634			mtx_lock_spin(&sio_lock);
1635		} while (buf < com->iptr);
1636	} else {
1637		do {
1638			/*
1639			 * This may look odd, but it is using save-and-enable
1640			 * semantics instead of the save-and-disable semantics
1641			 * that are used everywhere else.
1642			 */
1643			mtx_unlock_spin(&sio_lock);
1644			line_status = buf[com->ierroff];
1645			recv_data = *buf++;
1646			if (line_status
1647			    & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
1648				if (line_status & LSR_BI)
1649					recv_data |= TTY_BI;
1650				if (line_status & LSR_FE)
1651					recv_data |= TTY_FE;
1652				if (line_status & LSR_OE)
1653					recv_data |= TTY_OE;
1654				if (line_status & LSR_PE)
1655					recv_data |= TTY_PE;
1656			}
1657			(*linesw[tp->t_line].l_rint)(recv_data, tp);
1658			mtx_lock_spin(&sio_lock);
1659		} while (buf < com->iptr);
1660	}
1661	com_events -= (com->iptr - com->ibuf);
1662	com->iptr = com->ibuf;
1663
1664	/*
1665	 * There is now room for another low-level buffer full of input,
1666	 * so enable RTS if it is now disabled and there is room in the
1667	 * high-level buffer.
1668	 */
1669	if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) &&
1670	    !(tp->t_state & TS_TBLOCK))
1671		outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1672}
1673
1674static void
1675siointr(arg)
1676	void		*arg;
1677{
1678	struct com_s	*com;
1679
1680#ifndef COM_MULTIPORT
1681	com = (struct com_s *)arg;
1682
1683	mtx_lock_spin(&sio_lock);
1684	siointr1(com);
1685	mtx_unlock_spin(&sio_lock);
1686#else /* COM_MULTIPORT */
1687	bool_t		possibly_more_intrs;
1688	int		unit;
1689
1690	/*
1691	 * Loop until there is no activity on any port.  This is necessary
1692	 * to get an interrupt edge more than to avoid another interrupt.
1693	 * If the IRQ signal is just an OR of the IRQ signals from several
1694	 * devices, then the edge from one may be lost because another is
1695	 * on.
1696	 */
1697	mtx_lock_spin(&sio_lock);
1698	do {
1699		possibly_more_intrs = FALSE;
1700		for (unit = 0; unit < sio_numunits; ++unit) {
1701			com = com_addr(unit);
1702			/*
1703			 * XXX COM_LOCK();
1704			 * would it work here, or be counter-productive?
1705			 */
1706			if (com != NULL
1707			    && !com->gone
1708			    && (inb(com->int_id_port) & IIR_IMASK)
1709			       != IIR_NOPEND) {
1710				siointr1(com);
1711				possibly_more_intrs = TRUE;
1712			}
1713			/* XXX COM_UNLOCK(); */
1714		}
1715	} while (possibly_more_intrs);
1716	mtx_unlock_spin(&sio_lock);
1717#endif /* COM_MULTIPORT */
1718}
1719
1720static struct timespec siots[8192];
1721static int siotso;
1722static int volatile siotsunit = -1;
1723
1724static int
1725sysctl_siots(SYSCTL_HANDLER_ARGS)
1726{
1727	char buf[128];
1728	long long delta;
1729	size_t len;
1730	int error, i;
1731
1732	for (i = 1; i < siotso; i++) {
1733		delta = (long long)(siots[i].tv_sec - siots[i - 1].tv_sec) *
1734		    1000000000 +
1735		    (siots[i].tv_nsec - siots[i - 1].tv_nsec);
1736		len = sprintf(buf, "%lld\n", delta);
1737		if (delta >= 110000)
1738			len += sprintf(buf + len - 1, ": *** %ld.%09ld\n",
1739			    (long)siots[i].tv_sec, siots[i].tv_nsec);
1740		if (i == siotso - 1)
1741			buf[len - 1] = '\0';
1742		error = SYSCTL_OUT(req, buf, len);
1743		if (error != 0)
1744			return (error);
1745		uio_yield();
1746	}
1747	return (0);
1748}
1749
1750SYSCTL_PROC(_machdep, OID_AUTO, siots, CTLTYPE_STRING | CTLFLAG_RD,
1751    0, 0, sysctl_siots, "A", "sio timestamps");
1752
1753static void
1754siointr1(com)
1755	struct com_s	*com;
1756{
1757	u_char	line_status;
1758	u_char	modem_status;
1759	u_char	*ioptr;
1760	u_char	recv_data;
1761	u_char	int_ctl;
1762	u_char	int_ctl_new;
1763
1764	int_ctl = inb(com->intr_ctl_port);
1765	int_ctl_new = int_ctl;
1766
1767	while (!com->gone) {
1768		if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) {
1769			modem_status = inb(com->modem_status_port);
1770		        if ((modem_status ^ com->last_modem_status) &
1771			    com->pps_bit) {
1772				pps_capture(&com->pps);
1773				pps_event(&com->pps,
1774				    (modem_status & com->pps_bit) ?
1775				    PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
1776			}
1777		}
1778		line_status = inb(com->line_status_port);
1779
1780		/* input event? (check first to help avoid overruns) */
1781		while (line_status & LSR_RCV_MASK) {
1782			/* break/unnattached error bits or real input? */
1783			if (!(line_status & LSR_RXRDY))
1784				recv_data = 0;
1785			else
1786				recv_data = inb(com->data_port);
1787#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER)
1788			/*
1789			 * Solaris implements a new BREAK which is initiated
1790			 * by a character sequence CR ~ ^b which is similar
1791			 * to a familiar pattern used on Sun servers by the
1792			 * Remote Console.
1793			 */
1794#define	KEY_CRTLB	2	/* ^B */
1795#define	KEY_CR		13	/* CR '\r' */
1796#define	KEY_TILDE	126	/* ~ */
1797
1798			if (com->unit == comconsole) {
1799				static int brk_state1 = 0, brk_state2 = 0;
1800				if (recv_data == KEY_CR) {
1801					brk_state1 = recv_data;
1802					brk_state2 = 0;
1803				} else if (brk_state1 == KEY_CR
1804					   && (recv_data == KEY_TILDE
1805					       || recv_data == KEY_CRTLB)) {
1806					if (recv_data == KEY_TILDE)
1807						brk_state2 = recv_data;
1808					else if (brk_state2 == KEY_TILDE
1809						 && recv_data == KEY_CRTLB) {
1810							breakpoint();
1811							brk_state1 = 0;
1812							brk_state2 = 0;
1813							goto cont;
1814					} else
1815						brk_state2 = 0;
1816				} else
1817					brk_state1 = 0;
1818			}
1819#endif
1820			if (line_status & (LSR_BI | LSR_FE | LSR_PE)) {
1821				/*
1822				 * Don't store BI if IGNBRK or FE/PE if IGNPAR.
1823				 * Otherwise, push the work to a higher level
1824				 * (to handle PARMRK) if we're bypassing.
1825				 * Otherwise, convert BI/FE and PE+INPCK to 0.
1826				 *
1827				 * This makes bypassing work right in the
1828				 * usual "raw" case (IGNBRK set, and IGNPAR
1829				 * and INPCK clear).
1830				 *
1831				 * Note: BI together with FE/PE means just BI.
1832				 */
1833				if (line_status & LSR_BI) {
1834#if defined(DDB) && defined(BREAK_TO_DEBUGGER)
1835					if (com->unit == comconsole) {
1836						breakpoint();
1837						goto cont;
1838					}
1839#endif
1840					if (com->tp == NULL
1841					    || com->tp->t_iflag & IGNBRK)
1842						goto cont;
1843				} else {
1844					if (com->tp == NULL
1845					    || com->tp->t_iflag & IGNPAR)
1846						goto cont;
1847				}
1848				if (com->tp->t_state & TS_CAN_BYPASS_L_RINT
1849				    && (line_status & (LSR_BI | LSR_FE)
1850					|| com->tp->t_iflag & INPCK))
1851					recv_data = 0;
1852			}
1853			++com->bytes_in;
1854			if (com->hotchar != 0 && recv_data == com->hotchar)
1855				swi_sched(sio_fast_ih, 0);
1856			ioptr = com->iptr;
1857			if (ioptr >= com->ibufend)
1858				CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1859			else {
1860				if (com->do_timestamp)
1861					microtime(&com->timestamp);
1862				++com_events;
1863				swi_sched(sio_slow_ih, SWI_DELAY);
1864#if 0 /* for testing input latency vs efficiency */
1865if (com->iptr - com->ibuf == 8)
1866	swi_sched(sio_fast_ih, 0);
1867#endif
1868				ioptr[0] = recv_data;
1869				ioptr[com->ierroff] = line_status;
1870				com->iptr = ++ioptr;
1871				if (ioptr == com->ihighwater
1872				    && com->state & CS_RTS_IFLOW)
1873					outb(com->modem_ctl_port,
1874					     com->mcr_image &= ~MCR_RTS);
1875				if (line_status & LSR_OE)
1876					CE_RECORD(com, CE_OVERRUN);
1877			}
1878cont:
1879			/*
1880			 * "& 0x7F" is to avoid the gcc-1.40 generating a slow
1881			 * jump from the top of the loop to here
1882			 */
1883			line_status = inb(com->line_status_port) & 0x7F;
1884		}
1885
1886		/* modem status change? (always check before doing output) */
1887		modem_status = inb(com->modem_status_port);
1888		if (modem_status != com->last_modem_status) {
1889			if (com->do_dcd_timestamp
1890			    && !(com->last_modem_status & MSR_DCD)
1891			    && modem_status & MSR_DCD)
1892				microtime(&com->dcd_timestamp);
1893
1894			/*
1895			 * Schedule high level to handle DCD changes.  Note
1896			 * that we don't use the delta bits anywhere.  Some
1897			 * UARTs mess them up, and it's easy to remember the
1898			 * previous bits and calculate the delta.
1899			 */
1900			com->last_modem_status = modem_status;
1901			if (!(com->state & CS_CHECKMSR)) {
1902				com_events += LOTS_OF_EVENTS;
1903				com->state |= CS_CHECKMSR;
1904				swi_sched(sio_fast_ih, 0);
1905			}
1906
1907			/* handle CTS change immediately for crisp flow ctl */
1908			if (com->state & CS_CTS_OFLOW) {
1909				if (modem_status & MSR_CTS)
1910					com->state |= CS_ODEVREADY;
1911				else
1912					com->state &= ~CS_ODEVREADY;
1913			}
1914		}
1915
1916		/* output queued and everything ready? */
1917		if (line_status & LSR_TXRDY
1918		    && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1919			ioptr = com->obufq.l_head;
1920			if (com->tx_fifo_size > 1 && com->unit != siotsunit) {
1921				u_int	ocount;
1922
1923				ocount = com->obufq.l_tail - ioptr;
1924				if (ocount > com->tx_fifo_size)
1925					ocount = com->tx_fifo_size;
1926				com->bytes_out += ocount;
1927				do
1928					outb(com->data_port, *ioptr++);
1929				while (--ocount != 0);
1930			} else {
1931				outb(com->data_port, *ioptr++);
1932				++com->bytes_out;
1933				if (com->unit == siotsunit) {
1934					nanouptime(&siots[siotso]);
1935					siotso = (siotso + 1) %
1936					    (sizeof siots / sizeof siots[0]);
1937				}
1938			}
1939			com->obufq.l_head = ioptr;
1940			if (COM_IIR_TXRDYBUG(com->flags)) {
1941				int_ctl_new = int_ctl | IER_ETXRDY;
1942			}
1943			if (ioptr >= com->obufq.l_tail) {
1944				struct lbq	*qp;
1945
1946				qp = com->obufq.l_next;
1947				qp->l_queued = FALSE;
1948				qp = qp->l_next;
1949				if (qp != NULL) {
1950					com->obufq.l_head = qp->l_head;
1951					com->obufq.l_tail = qp->l_tail;
1952					com->obufq.l_next = qp;
1953				} else {
1954					/* output just completed */
1955					if (COM_IIR_TXRDYBUG(com->flags)) {
1956						int_ctl_new = int_ctl & ~IER_ETXRDY;
1957					}
1958					com->state &= ~CS_BUSY;
1959				}
1960				if (!(com->state & CS_ODONE)) {
1961					com_events += LOTS_OF_EVENTS;
1962					com->state |= CS_ODONE;
1963					/* handle at high level ASAP */
1964					swi_sched(sio_fast_ih, 0);
1965				}
1966			}
1967			if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) {
1968				outb(com->intr_ctl_port, int_ctl_new);
1969			}
1970		}
1971
1972		/* finished? */
1973#ifndef COM_MULTIPORT
1974		if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND)
1975#endif /* COM_MULTIPORT */
1976			return;
1977	}
1978}
1979
1980static int
1981sioioctl(dev, cmd, data, flag, td)
1982	dev_t		dev;
1983	u_long		cmd;
1984	caddr_t		data;
1985	int		flag;
1986	struct thread	*td;
1987{
1988	struct com_s	*com;
1989	int		error;
1990	int		mynor;
1991	int		s;
1992	struct tty	*tp;
1993#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1994	u_long		oldcmd;
1995	struct termios	term;
1996#endif
1997
1998	mynor = minor(dev);
1999	com = com_addr(MINOR_TO_UNIT(mynor));
2000	if (com == NULL || com->gone)
2001		return (ENODEV);
2002	if (mynor & CONTROL_MASK) {
2003		struct termios	*ct;
2004
2005		switch (mynor & CONTROL_MASK) {
2006		case CONTROL_INIT_STATE:
2007			ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
2008			break;
2009		case CONTROL_LOCK_STATE:
2010			ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
2011			break;
2012		default:
2013			return (ENODEV);	/* /dev/nodev */
2014		}
2015		switch (cmd) {
2016		case TIOCSETA:
2017			error = suser(td);
2018			if (error != 0)
2019				return (error);
2020			*ct = *(struct termios *)data;
2021			return (0);
2022		case TIOCGETA:
2023			*(struct termios *)data = *ct;
2024			return (0);
2025		case TIOCGETD:
2026			*(int *)data = TTYDISC;
2027			return (0);
2028		case TIOCGWINSZ:
2029			bzero(data, sizeof(struct winsize));
2030			return (0);
2031		default:
2032			return (ENOTTY);
2033		}
2034	}
2035	tp = com->tp;
2036#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2037	term = tp->t_termios;
2038	oldcmd = cmd;
2039	error = ttsetcompat(tp, &cmd, data, &term);
2040	if (error != 0)
2041		return (error);
2042	if (cmd != oldcmd)
2043		data = (caddr_t)&term;
2044#endif
2045	if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
2046		int	cc;
2047		struct termios *dt = (struct termios *)data;
2048		struct termios *lt = mynor & CALLOUT_MASK
2049				     ? &com->lt_out : &com->lt_in;
2050
2051		dt->c_iflag = (tp->t_iflag & lt->c_iflag)
2052			      | (dt->c_iflag & ~lt->c_iflag);
2053		dt->c_oflag = (tp->t_oflag & lt->c_oflag)
2054			      | (dt->c_oflag & ~lt->c_oflag);
2055		dt->c_cflag = (tp->t_cflag & lt->c_cflag)
2056			      | (dt->c_cflag & ~lt->c_cflag);
2057		dt->c_lflag = (tp->t_lflag & lt->c_lflag)
2058			      | (dt->c_lflag & ~lt->c_lflag);
2059		for (cc = 0; cc < NCCS; ++cc)
2060			if (lt->c_cc[cc] != 0)
2061				dt->c_cc[cc] = tp->t_cc[cc];
2062		if (lt->c_ispeed != 0)
2063			dt->c_ispeed = tp->t_ispeed;
2064		if (lt->c_ospeed != 0)
2065			dt->c_ospeed = tp->t_ospeed;
2066	}
2067	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
2068	if (error != ENOIOCTL)
2069		return (error);
2070	s = spltty();
2071	error = ttioctl(tp, cmd, data, flag);
2072	disc_optim(tp, &tp->t_termios, com);
2073	if (error != ENOIOCTL) {
2074		splx(s);
2075		return (error);
2076	}
2077	switch (cmd) {
2078	case TIOCSBRK:
2079		sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK);
2080		break;
2081	case TIOCCBRK:
2082		sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
2083		break;
2084	case TIOCSDTR:
2085		(void)commctl(com, TIOCM_DTR, DMBIS);
2086		break;
2087	case TIOCCDTR:
2088		(void)commctl(com, TIOCM_DTR, DMBIC);
2089		break;
2090	/*
2091	 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set.  The
2092	 * changes get undone on the next call to comparam().
2093	 */
2094	case TIOCMSET:
2095		(void)commctl(com, *(int *)data, DMSET);
2096		break;
2097	case TIOCMBIS:
2098		(void)commctl(com, *(int *)data, DMBIS);
2099		break;
2100	case TIOCMBIC:
2101		(void)commctl(com, *(int *)data, DMBIC);
2102		break;
2103	case TIOCMGET:
2104		*(int *)data = commctl(com, 0, DMGET);
2105		break;
2106	case TIOCMSDTRWAIT:
2107		/* must be root since the wait applies to following logins */
2108		error = suser(td);
2109		if (error != 0) {
2110			splx(s);
2111			return (error);
2112		}
2113		com->dtr_wait = *(int *)data * hz / 100;
2114		break;
2115	case TIOCMGDTRWAIT:
2116		*(int *)data = com->dtr_wait * 100 / hz;
2117		break;
2118	case TIOCTIMESTAMP:
2119		com->do_timestamp = TRUE;
2120		*(struct timeval *)data = com->timestamp;
2121		break;
2122	case TIOCDCDTIMESTAMP:
2123		com->do_dcd_timestamp = TRUE;
2124		*(struct timeval *)data = com->dcd_timestamp;
2125		break;
2126	default:
2127		splx(s);
2128		error = pps_ioctl(cmd, data, &com->pps);
2129		if (error == ENODEV)
2130			error = ENOTTY;
2131		return (error);
2132	}
2133	splx(s);
2134	return (0);
2135}
2136
2137/* software interrupt handler for SWI_TTY */
2138static void
2139siopoll(void *dummy)
2140{
2141	int		unit;
2142
2143	if (com_events == 0)
2144		return;
2145repeat:
2146	for (unit = 0; unit < sio_numunits; ++unit) {
2147		struct com_s	*com;
2148		int		incc;
2149		struct tty	*tp;
2150
2151		com = com_addr(unit);
2152		if (com == NULL)
2153			continue;
2154		tp = com->tp;
2155		if (tp == NULL || com->gone) {
2156			/*
2157			 * Discard any events related to never-opened or
2158			 * going-away devices.
2159			 */
2160			mtx_lock_spin(&sio_lock);
2161			incc = com->iptr - com->ibuf;
2162			com->iptr = com->ibuf;
2163			if (com->state & CS_CHECKMSR) {
2164				incc += LOTS_OF_EVENTS;
2165				com->state &= ~CS_CHECKMSR;
2166			}
2167			com_events -= incc;
2168			mtx_unlock_spin(&sio_lock);
2169			continue;
2170		}
2171		if (com->iptr != com->ibuf) {
2172			mtx_lock_spin(&sio_lock);
2173			sioinput(com);
2174			mtx_unlock_spin(&sio_lock);
2175		}
2176		if (com->state & CS_CHECKMSR) {
2177			u_char	delta_modem_status;
2178
2179			mtx_lock_spin(&sio_lock);
2180			delta_modem_status = com->last_modem_status
2181					     ^ com->prev_modem_status;
2182			com->prev_modem_status = com->last_modem_status;
2183			com_events -= LOTS_OF_EVENTS;
2184			com->state &= ~CS_CHECKMSR;
2185			mtx_unlock_spin(&sio_lock);
2186			if (delta_modem_status & MSR_DCD)
2187				(*linesw[tp->t_line].l_modem)
2188					(tp, com->prev_modem_status & MSR_DCD);
2189		}
2190		if (com->state & CS_ODONE) {
2191			mtx_lock_spin(&sio_lock);
2192			com_events -= LOTS_OF_EVENTS;
2193			com->state &= ~CS_ODONE;
2194			mtx_unlock_spin(&sio_lock);
2195			if (!(com->state & CS_BUSY)
2196			    && !(com->extra_state & CSE_BUSYCHECK)) {
2197				timeout(siobusycheck, com, hz / 100);
2198				com->extra_state |= CSE_BUSYCHECK;
2199			}
2200			(*linesw[tp->t_line].l_start)(tp);
2201		}
2202		if (com_events == 0)
2203			break;
2204	}
2205	if (com_events >= LOTS_OF_EVENTS)
2206		goto repeat;
2207}
2208
2209static int
2210comparam(tp, t)
2211	struct tty	*tp;
2212	struct termios	*t;
2213{
2214	u_int		cfcr;
2215	int		cflag;
2216	struct com_s	*com;
2217	u_int		divisor;
2218	u_char		dlbh;
2219	u_char		dlbl;
2220	int		s;
2221	int		unit;
2222
2223	unit = DEV_TO_UNIT(tp->t_dev);
2224	com = com_addr(unit);
2225	if (com == NULL)
2226		return (ENODEV);
2227
2228	/* do historical conversions */
2229	if (t->c_ispeed == 0)
2230		t->c_ispeed = t->c_ospeed;
2231
2232	/* check requested parameters */
2233	if (t->c_ospeed == 0)
2234		divisor = 0;
2235	else {
2236		if (t->c_ispeed != t->c_ospeed)
2237			return (EINVAL);
2238		divisor = siodivisor(com->rclk, t->c_ispeed);
2239		if (divisor == 0)
2240			return (EINVAL);
2241	}
2242
2243	/* parameters are OK, convert them to the com struct and the device */
2244	s = spltty();
2245	if (divisor == 0)
2246		(void)commctl(com, TIOCM_DTR, DMBIC);	/* hang up line */
2247	else
2248		(void)commctl(com, TIOCM_DTR, DMBIS);
2249	cflag = t->c_cflag;
2250	switch (cflag & CSIZE) {
2251	case CS5:
2252		cfcr = CFCR_5BITS;
2253		break;
2254	case CS6:
2255		cfcr = CFCR_6BITS;
2256		break;
2257	case CS7:
2258		cfcr = CFCR_7BITS;
2259		break;
2260	default:
2261		cfcr = CFCR_8BITS;
2262		break;
2263	}
2264	if (cflag & PARENB) {
2265		cfcr |= CFCR_PENAB;
2266		if (!(cflag & PARODD))
2267			cfcr |= CFCR_PEVEN;
2268	}
2269	if (cflag & CSTOPB)
2270		cfcr |= CFCR_STOPB;
2271
2272	if (com->hasfifo && divisor != 0) {
2273		/*
2274		 * Use a fifo trigger level low enough so that the input
2275		 * latency from the fifo is less than about 16 msec and
2276		 * the total latency is less than about 30 msec.  These
2277		 * latencies are reasonable for humans.  Serial comms
2278		 * protocols shouldn't expect anything better since modem
2279		 * latencies are larger.
2280		 *
2281		 * The fifo trigger level cannot be set at RX_HIGH for high
2282		 * speed connections without further work on reducing
2283		 * interrupt disablement times in other parts of the system,
2284		 * without producing silo overflow errors.
2285		 */
2286		com->fifo_image = com->unit == siotsunit ? 0
2287				  : t->c_ospeed <= 4800
2288				  ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_MEDH;
2289#ifdef COM_ESP
2290		/*
2291		 * The Hayes ESP card needs the fifo DMA mode bit set
2292		 * in compatibility mode.  If not, it will interrupt
2293		 * for each character received.
2294		 */
2295		if (com->esp)
2296			com->fifo_image |= FIFO_DMA_MODE;
2297#endif
2298		sio_setreg(com, com_fifo, com->fifo_image);
2299	}
2300
2301	/*
2302	 * This returns with interrupts disabled so that we can complete
2303	 * the speed change atomically.  Keeping interrupts disabled is
2304	 * especially important while com_data is hidden.
2305	 */
2306	(void) siosetwater(com, t->c_ispeed);
2307
2308	if (divisor != 0) {
2309		sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB);
2310		/*
2311		 * Only set the divisor registers if they would change,
2312		 * since on some 16550 incompatibles (UMC8669F), setting
2313		 * them while input is arriving them loses sync until
2314		 * data stops arriving.
2315		 */
2316		dlbl = divisor & 0xFF;
2317		if (sio_getreg(com, com_dlbl) != dlbl)
2318			sio_setreg(com, com_dlbl, dlbl);
2319		dlbh = divisor >> 8;
2320		if (sio_getreg(com, com_dlbh) != dlbh)
2321			sio_setreg(com, com_dlbh, dlbh);
2322	}
2323
2324	sio_setreg(com, com_cfcr, com->cfcr_image = cfcr);
2325
2326	if (!(tp->t_state & TS_TTSTOP))
2327		com->state |= CS_TTGO;
2328
2329	if (cflag & CRTS_IFLOW) {
2330		if (com->st16650a) {
2331			sio_setreg(com, com_cfcr, 0xbf);
2332			sio_setreg(com, com_fifo,
2333				   sio_getreg(com, com_fifo) | 0x40);
2334		}
2335		com->state |= CS_RTS_IFLOW;
2336		/*
2337		 * If CS_RTS_IFLOW just changed from off to on, the change
2338		 * needs to be propagated to MCR_RTS.  This isn't urgent,
2339		 * so do it later by calling comstart() instead of repeating
2340		 * a lot of code from comstart() here.
2341		 */
2342	} else if (com->state & CS_RTS_IFLOW) {
2343		com->state &= ~CS_RTS_IFLOW;
2344		/*
2345		 * CS_RTS_IFLOW just changed from on to off.  Force MCR_RTS
2346		 * on here, since comstart() won't do it later.
2347		 */
2348		outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2349		if (com->st16650a) {
2350			sio_setreg(com, com_cfcr, 0xbf);
2351			sio_setreg(com, com_fifo,
2352				   sio_getreg(com, com_fifo) & ~0x40);
2353		}
2354	}
2355
2356
2357	/*
2358	 * Set up state to handle output flow control.
2359	 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2360	 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2361	 */
2362	com->state |= CS_ODEVREADY;
2363	com->state &= ~CS_CTS_OFLOW;
2364	if (cflag & CCTS_OFLOW) {
2365		com->state |= CS_CTS_OFLOW;
2366		if (!(com->last_modem_status & MSR_CTS))
2367			com->state &= ~CS_ODEVREADY;
2368		if (com->st16650a) {
2369			sio_setreg(com, com_cfcr, 0xbf);
2370			sio_setreg(com, com_fifo,
2371				   sio_getreg(com, com_fifo) | 0x80);
2372		}
2373	} else {
2374		if (com->st16650a) {
2375			sio_setreg(com, com_cfcr, 0xbf);
2376			sio_setreg(com, com_fifo,
2377				   sio_getreg(com, com_fifo) & ~0x80);
2378		}
2379	}
2380
2381	sio_setreg(com, com_cfcr, com->cfcr_image);
2382
2383	/* XXX shouldn't call functions while intrs are disabled. */
2384	disc_optim(tp, t, com);
2385	/*
2386	 * Recover from fiddling with CS_TTGO.  We used to call siointr1()
2387	 * unconditionally, but that defeated the careful discarding of
2388	 * stale input in sioopen().
2389	 */
2390	if (com->state >= (CS_BUSY | CS_TTGO))
2391		siointr1(com);
2392
2393	mtx_unlock_spin(&sio_lock);
2394	splx(s);
2395	comstart(tp);
2396	if (com->ibufold != NULL) {
2397		free(com->ibufold, M_DEVBUF);
2398		com->ibufold = NULL;
2399	}
2400	return (0);
2401}
2402
2403/*
2404 * This function must be called with the sio_lock mutex released and will
2405 * return with it obtained.
2406 */
2407static int
2408siosetwater(com, speed)
2409	struct com_s	*com;
2410	speed_t		speed;
2411{
2412	int		cp4ticks;
2413	u_char		*ibuf;
2414	int		ibufsize;
2415	struct tty	*tp;
2416
2417	/*
2418	 * Make the buffer size large enough to handle a softtty interrupt
2419	 * latency of about 2 ticks without loss of throughput or data
2420	 * (about 3 ticks if input flow control is not used or not honoured,
2421	 * but a bit less for CS5-CS7 modes).
2422	 */
2423	cp4ticks = speed / 10 / hz * 4;
2424	for (ibufsize = 128; ibufsize < cp4ticks;)
2425		ibufsize <<= 1;
2426	if (ibufsize == com->ibufsize) {
2427		mtx_lock_spin(&sio_lock);
2428		return (0);
2429	}
2430
2431	/*
2432	 * Allocate input buffer.  The extra factor of 2 in the size is
2433	 * to allow for an error byte for each input byte.
2434	 */
2435	ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT);
2436	if (ibuf == NULL) {
2437		mtx_lock_spin(&sio_lock);
2438		return (ENOMEM);
2439	}
2440
2441	/* Initialize non-critical variables. */
2442	com->ibufold = com->ibuf;
2443	com->ibufsize = ibufsize;
2444	tp = com->tp;
2445	if (tp != NULL) {
2446		tp->t_ififosize = 2 * ibufsize;
2447		tp->t_ispeedwat = (speed_t)-1;
2448		tp->t_ospeedwat = (speed_t)-1;
2449	}
2450
2451	/*
2452	 * Read current input buffer, if any.  Continue with interrupts
2453	 * disabled.
2454	 */
2455	mtx_lock_spin(&sio_lock);
2456	if (com->iptr != com->ibuf)
2457		sioinput(com);
2458
2459	/*-
2460	 * Initialize critical variables, including input buffer watermarks.
2461	 * The external device is asked to stop sending when the buffer
2462	 * exactly reaches high water, or when the high level requests it.
2463	 * The high level is notified immediately (rather than at a later
2464	 * clock tick) when this watermark is reached.
2465	 * The buffer size is chosen so the watermark should almost never
2466	 * be reached.
2467	 * The low watermark is invisibly 0 since the buffer is always
2468	 * emptied all at once.
2469	 */
2470	com->iptr = com->ibuf = ibuf;
2471	com->ibufend = ibuf + ibufsize;
2472	com->ierroff = ibufsize;
2473	com->ihighwater = ibuf + 3 * ibufsize / 4;
2474	return (0);
2475}
2476
2477static void
2478comstart(tp)
2479	struct tty	*tp;
2480{
2481	struct com_s	*com;
2482	int		s;
2483	int		unit;
2484
2485	unit = DEV_TO_UNIT(tp->t_dev);
2486	com = com_addr(unit);
2487	if (com == NULL)
2488		return;
2489	s = spltty();
2490	mtx_lock_spin(&sio_lock);
2491	if (tp->t_state & TS_TTSTOP)
2492		com->state &= ~CS_TTGO;
2493	else
2494		com->state |= CS_TTGO;
2495	if (tp->t_state & TS_TBLOCK) {
2496		if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW)
2497			outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2498	} else {
2499		if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater
2500		    && com->state & CS_RTS_IFLOW)
2501			outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2502	}
2503	mtx_unlock_spin(&sio_lock);
2504	if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2505		ttwwakeup(tp);
2506		splx(s);
2507		return;
2508	}
2509	if (tp->t_outq.c_cc != 0) {
2510		struct lbq	*qp;
2511		struct lbq	*next;
2512
2513		if (!com->obufs[0].l_queued) {
2514			com->obufs[0].l_tail
2515			    = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2516						  sizeof com->obuf1);
2517			com->obufs[0].l_next = NULL;
2518			com->obufs[0].l_queued = TRUE;
2519			mtx_lock_spin(&sio_lock);
2520			if (com->state & CS_BUSY) {
2521				qp = com->obufq.l_next;
2522				while ((next = qp->l_next) != NULL)
2523					qp = next;
2524				qp->l_next = &com->obufs[0];
2525			} else {
2526				com->obufq.l_head = com->obufs[0].l_head;
2527				com->obufq.l_tail = com->obufs[0].l_tail;
2528				com->obufq.l_next = &com->obufs[0];
2529				com->state |= CS_BUSY;
2530			}
2531			mtx_unlock_spin(&sio_lock);
2532		}
2533		if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2534			com->obufs[1].l_tail
2535			    = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2536						  sizeof com->obuf2);
2537			com->obufs[1].l_next = NULL;
2538			com->obufs[1].l_queued = TRUE;
2539			mtx_lock_spin(&sio_lock);
2540			if (com->state & CS_BUSY) {
2541				qp = com->obufq.l_next;
2542				while ((next = qp->l_next) != NULL)
2543					qp = next;
2544				qp->l_next = &com->obufs[1];
2545			} else {
2546				com->obufq.l_head = com->obufs[1].l_head;
2547				com->obufq.l_tail = com->obufs[1].l_tail;
2548				com->obufq.l_next = &com->obufs[1];
2549				com->state |= CS_BUSY;
2550			}
2551			mtx_unlock_spin(&sio_lock);
2552		}
2553		tp->t_state |= TS_BUSY;
2554	}
2555	mtx_lock_spin(&sio_lock);
2556	if (com->state >= (CS_BUSY | CS_TTGO))
2557		siointr1(com);	/* fake interrupt to start output */
2558	mtx_unlock_spin(&sio_lock);
2559	ttwwakeup(tp);
2560	splx(s);
2561}
2562
2563static void
2564comstop(tp, rw)
2565	struct tty	*tp;
2566	int		rw;
2567{
2568	struct com_s	*com;
2569
2570	com = com_addr(DEV_TO_UNIT(tp->t_dev));
2571	if (com == NULL || com->gone)
2572		return;
2573	mtx_lock_spin(&sio_lock);
2574	if (rw & FWRITE) {
2575		if (com->hasfifo)
2576#ifdef COM_ESP
2577		    /* XXX avoid h/w bug. */
2578		    if (!com->esp)
2579#endif
2580			sio_setreg(com, com_fifo,
2581				   FIFO_XMT_RST | com->fifo_image);
2582		com->obufs[0].l_queued = FALSE;
2583		com->obufs[1].l_queued = FALSE;
2584		if (com->state & CS_ODONE)
2585			com_events -= LOTS_OF_EVENTS;
2586		com->state &= ~(CS_ODONE | CS_BUSY);
2587		com->tp->t_state &= ~TS_BUSY;
2588	}
2589	if (rw & FREAD) {
2590		if (com->hasfifo)
2591#ifdef COM_ESP
2592		    /* XXX avoid h/w bug. */
2593		    if (!com->esp)
2594#endif
2595			sio_setreg(com, com_fifo,
2596				   FIFO_RCV_RST | com->fifo_image);
2597		com_events -= (com->iptr - com->ibuf);
2598		com->iptr = com->ibuf;
2599	}
2600	mtx_unlock_spin(&sio_lock);
2601	comstart(tp);
2602}
2603
2604static int
2605commctl(com, bits, how)
2606	struct com_s	*com;
2607	int		bits;
2608	int		how;
2609{
2610	int	mcr;
2611	int	msr;
2612
2613	if (how == DMGET) {
2614		bits = TIOCM_LE;	/* XXX - always enabled while open */
2615		mcr = com->mcr_image;
2616		if (mcr & MCR_DTR)
2617			bits |= TIOCM_DTR;
2618		if (mcr & MCR_RTS)
2619			bits |= TIOCM_RTS;
2620		msr = com->prev_modem_status;
2621		if (msr & MSR_CTS)
2622			bits |= TIOCM_CTS;
2623		if (msr & MSR_DCD)
2624			bits |= TIOCM_CD;
2625		if (msr & MSR_DSR)
2626			bits |= TIOCM_DSR;
2627		/*
2628		 * XXX - MSR_RI is naturally volatile, and we make MSR_TERI
2629		 * more volatile by reading the modem status a lot.  Perhaps
2630		 * we should latch both bits until the status is read here.
2631		 */
2632		if (msr & (MSR_RI | MSR_TERI))
2633			bits |= TIOCM_RI;
2634		return (bits);
2635	}
2636	mcr = 0;
2637	if (bits & TIOCM_DTR)
2638		mcr |= MCR_DTR;
2639	if (bits & TIOCM_RTS)
2640		mcr |= MCR_RTS;
2641	if (com->gone)
2642		return(0);
2643	mtx_lock_spin(&sio_lock);
2644	switch (how) {
2645	case DMSET:
2646		outb(com->modem_ctl_port,
2647		     com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE));
2648		break;
2649	case DMBIS:
2650		outb(com->modem_ctl_port, com->mcr_image |= mcr);
2651		break;
2652	case DMBIC:
2653		outb(com->modem_ctl_port, com->mcr_image &= ~mcr);
2654		break;
2655	}
2656	mtx_unlock_spin(&sio_lock);
2657	return (0);
2658}
2659
2660static void
2661siosettimeout()
2662{
2663	struct com_s	*com;
2664	bool_t		someopen;
2665	int		unit;
2666
2667	/*
2668	 * Set our timeout period to 1 second if no polled devices are open.
2669	 * Otherwise set it to max(1/200, 1/hz).
2670	 * Enable timeouts iff some device is open.
2671	 */
2672	untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
2673	sio_timeout = hz;
2674	someopen = FALSE;
2675	for (unit = 0; unit < sio_numunits; ++unit) {
2676		com = com_addr(unit);
2677		if (com != NULL && com->tp != NULL
2678		    && com->tp->t_state & TS_ISOPEN && !com->gone) {
2679			someopen = TRUE;
2680			if (com->poll || com->poll_output) {
2681				sio_timeout = hz > 200 ? hz / 200 : 1;
2682				break;
2683			}
2684		}
2685	}
2686	if (someopen) {
2687		sio_timeouts_until_log = hz / sio_timeout;
2688		sio_timeout_handle = timeout(comwakeup, (void *)NULL,
2689					     sio_timeout);
2690	} else {
2691		/* Flush error messages, if any. */
2692		sio_timeouts_until_log = 1;
2693		comwakeup((void *)NULL);
2694		untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
2695	}
2696}
2697
2698static void
2699comwakeup(chan)
2700	void	*chan;
2701{
2702	struct com_s	*com;
2703	int		unit;
2704
2705	sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout);
2706
2707	/*
2708	 * Recover from lost output interrupts.
2709	 * Poll any lines that don't use interrupts.
2710	 */
2711	for (unit = 0; unit < sio_numunits; ++unit) {
2712		com = com_addr(unit);
2713		if (com != NULL && !com->gone
2714		    && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2715			mtx_lock_spin(&sio_lock);
2716			siointr1(com);
2717			mtx_unlock_spin(&sio_lock);
2718		}
2719	}
2720
2721	/*
2722	 * Check for and log errors, but not too often.
2723	 */
2724	if (--sio_timeouts_until_log > 0)
2725		return;
2726	sio_timeouts_until_log = hz / sio_timeout;
2727	for (unit = 0; unit < sio_numunits; ++unit) {
2728		int	errnum;
2729
2730		com = com_addr(unit);
2731		if (com == NULL)
2732			continue;
2733		if (com->gone)
2734			continue;
2735		for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2736			u_int	delta;
2737			u_long	total;
2738
2739			mtx_lock_spin(&sio_lock);
2740			delta = com->delta_error_counts[errnum];
2741			com->delta_error_counts[errnum] = 0;
2742			mtx_unlock_spin(&sio_lock);
2743			if (delta == 0)
2744				continue;
2745			total = com->error_counts[errnum] += delta;
2746			log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n",
2747			    unit, delta, error_desc[errnum],
2748			    delta == 1 ? "" : "s", total);
2749		}
2750	}
2751}
2752
2753static void
2754disc_optim(tp, t, com)
2755	struct tty	*tp;
2756	struct termios	*t;
2757	struct com_s	*com;
2758{
2759	if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2760	    && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2761	    && (!(t->c_iflag & PARMRK)
2762		|| (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2763	    && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2764	    && linesw[tp->t_line].l_rint == ttyinput)
2765		tp->t_state |= TS_CAN_BYPASS_L_RINT;
2766	else
2767		tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2768	com->hotchar = linesw[tp->t_line].l_hotchar;
2769}
2770
2771/*
2772 * Following are all routines needed for SIO to act as console
2773 */
2774#include <sys/cons.h>
2775
2776struct siocnstate {
2777	u_char	dlbl;
2778	u_char	dlbh;
2779	u_char	ier;
2780	u_char	cfcr;
2781	u_char	mcr;
2782};
2783
2784#ifndef __alpha__
2785static speed_t siocngetspeed(Port_t, u_long rclk);
2786#endif
2787static void siocnclose(struct siocnstate *sp, Port_t iobase);
2788static void siocnopen(struct siocnstate *sp, Port_t iobase, int speed);
2789static void siocntxwait(Port_t iobase);
2790
2791#ifdef __alpha__
2792int siocnattach(int port, int speed);
2793int siogdbattach(int port, int speed);
2794int siogdbgetc(void);
2795void siogdbputc(int c);
2796#else
2797static cn_probe_t siocnprobe;
2798static cn_init_t siocninit;
2799static cn_term_t siocnterm;
2800#endif
2801static cn_checkc_t siocncheckc;
2802static cn_getc_t siocngetc;
2803static cn_putc_t siocnputc;
2804
2805#ifndef __alpha__
2806CONS_DRIVER(sio, siocnprobe, siocninit, siocnterm, siocngetc, siocncheckc,
2807	    siocnputc, NULL);
2808#endif
2809
2810/* To get the GDB related variables */
2811#if DDB > 0
2812#include <ddb/ddb.h>
2813static struct consdev gdbconsdev;
2814
2815#endif
2816
2817static void
2818siocntxwait(iobase)
2819	Port_t	iobase;
2820{
2821	int	timo;
2822
2823	/*
2824	 * Wait for any pending transmission to finish.  Required to avoid
2825	 * the UART lockup bug when the speed is changed, and for normal
2826	 * transmits.
2827	 */
2828	timo = 100000;
2829	while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY))
2830	       != (LSR_TSRE | LSR_TXRDY) && --timo != 0)
2831		;
2832}
2833
2834#ifndef __alpha__
2835
2836/*
2837 * Read the serial port specified and try to figure out what speed
2838 * it's currently running at.  We're assuming the serial port has
2839 * been initialized and is basicly idle.  This routine is only intended
2840 * to be run at system startup.
2841 *
2842 * If the value read from the serial port doesn't make sense, return 0.
2843 */
2844
2845static speed_t
2846siocngetspeed(iobase, rclk)
2847	Port_t	iobase;
2848	u_long	rclk;
2849{
2850	u_int	divisor;
2851	u_char	dlbh;
2852	u_char	dlbl;
2853	u_char  cfcr;
2854
2855	cfcr = inb(iobase + com_cfcr);
2856	outb(iobase + com_cfcr, CFCR_DLAB | cfcr);
2857
2858	dlbl = inb(iobase + com_dlbl);
2859	dlbh = inb(iobase + com_dlbh);
2860
2861	outb(iobase + com_cfcr, cfcr);
2862
2863	divisor = dlbh << 8 | dlbl;
2864
2865	/* XXX there should be more sanity checking. */
2866	if (divisor == 0)
2867		return (CONSPEED);
2868	return (rclk / (16UL * divisor));
2869}
2870
2871#endif
2872
2873static void
2874siocnopen(sp, iobase, speed)
2875	struct siocnstate	*sp;
2876	Port_t			iobase;
2877	int			speed;
2878{
2879	u_int	divisor;
2880	u_char	dlbh;
2881	u_char	dlbl;
2882
2883	/*
2884	 * Save all the device control registers except the fifo register
2885	 * and set our default ones (cs8 -parenb speed=comdefaultrate).
2886	 * We can't save the fifo register since it is read-only.
2887	 */
2888	sp->ier = inb(iobase + com_ier);
2889	outb(iobase + com_ier, 0);	/* spltty() doesn't stop siointr() */
2890	siocntxwait(iobase);
2891	sp->cfcr = inb(iobase + com_cfcr);
2892	outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
2893	sp->dlbl = inb(iobase + com_dlbl);
2894	sp->dlbh = inb(iobase + com_dlbh);
2895	/*
2896	 * Only set the divisor registers if they would change, since on
2897	 * some 16550 incompatibles (Startech), setting them clears the
2898	 * data input register.  This also reduces the effects of the
2899	 * UMC8669F bug.
2900	 */
2901	divisor = siodivisor(comdefaultrclk, speed);
2902	dlbl = divisor & 0xFF;
2903	if (sp->dlbl != dlbl)
2904		outb(iobase + com_dlbl, dlbl);
2905	dlbh = divisor >> 8;
2906	if (sp->dlbh != dlbh)
2907		outb(iobase + com_dlbh, dlbh);
2908	outb(iobase + com_cfcr, CFCR_8BITS);
2909	sp->mcr = inb(iobase + com_mcr);
2910	/*
2911	 * We don't want interrupts, but must be careful not to "disable"
2912	 * them by clearing the MCR_IENABLE bit, since that might cause
2913	 * an interrupt by floating the IRQ line.
2914	 */
2915	outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS);
2916}
2917
2918static void
2919siocnclose(sp, iobase)
2920	struct siocnstate	*sp;
2921	Port_t			iobase;
2922{
2923	/*
2924	 * Restore the device control registers.
2925	 */
2926	siocntxwait(iobase);
2927	outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
2928	if (sp->dlbl != inb(iobase + com_dlbl))
2929		outb(iobase + com_dlbl, sp->dlbl);
2930	if (sp->dlbh != inb(iobase + com_dlbh))
2931		outb(iobase + com_dlbh, sp->dlbh);
2932	outb(iobase + com_cfcr, sp->cfcr);
2933	/*
2934	 * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them.
2935	 */
2936	outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS);
2937	outb(iobase + com_ier, sp->ier);
2938}
2939
2940#ifndef __alpha__
2941
2942static void
2943siocnprobe(cp)
2944	struct consdev	*cp;
2945{
2946	speed_t			boot_speed;
2947	u_char			cfcr;
2948	u_int			divisor;
2949	int			s, unit;
2950	struct siocnstate	sp;
2951
2952	/*
2953	 * Find our first enabled console, if any.  If it is a high-level
2954	 * console device, then initialize it and return successfully.
2955	 * If it is a low-level console device, then initialize it and
2956	 * return unsuccessfully.  It must be initialized in both cases
2957	 * for early use by console drivers and debuggers.  Initializing
2958	 * the hardware is not necessary in all cases, since the i/o
2959	 * routines initialize it on the fly, but it is necessary if
2960	 * input might arrive while the hardware is switched back to an
2961	 * uninitialized state.  We can't handle multiple console devices
2962	 * yet because our low-level routines don't take a device arg.
2963	 * We trust the user to set the console flags properly so that we
2964	 * don't need to probe.
2965	 */
2966	cp->cn_pri = CN_DEAD;
2967
2968	for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */
2969		int flags;
2970
2971		if (resource_disabled("sio", unit))
2972			continue;
2973		if (resource_int_value("sio", unit, "flags", &flags))
2974			continue;
2975		if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) {
2976			int port;
2977			Port_t iobase;
2978
2979			if (resource_int_value("sio", unit, "port", &port))
2980				continue;
2981			iobase = port;
2982			s = spltty();
2983			if (boothowto & RB_SERIAL) {
2984				boot_speed =
2985				    siocngetspeed(iobase, comdefaultrclk);
2986				if (boot_speed)
2987					comdefaultrate = boot_speed;
2988			}
2989
2990			/*
2991			 * Initialize the divisor latch.  We can't rely on
2992			 * siocnopen() to do this the first time, since it
2993			 * avoids writing to the latch if the latch appears
2994			 * to have the correct value.  Also, if we didn't
2995			 * just read the speed from the hardware, then we
2996			 * need to set the speed in hardware so that
2997			 * switching it later is null.
2998			 */
2999			cfcr = inb(iobase + com_cfcr);
3000			outb(iobase + com_cfcr, CFCR_DLAB | cfcr);
3001			divisor = siodivisor(comdefaultrclk, comdefaultrate);
3002			outb(iobase + com_dlbl, divisor & 0xff);
3003			outb(iobase + com_dlbh, divisor >> 8);
3004			outb(iobase + com_cfcr, cfcr);
3005
3006			siocnopen(&sp, iobase, comdefaultrate);
3007
3008			splx(s);
3009			if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) {
3010				cp->cn_dev = makedev(CDEV_MAJOR, unit);
3011				cp->cn_pri = COM_FORCECONSOLE(flags)
3012					     || boothowto & RB_SERIAL
3013					     ? CN_REMOTE : CN_NORMAL;
3014				siocniobase = iobase;
3015				siocnunit = unit;
3016			}
3017			if (COM_DEBUGGER(flags)) {
3018				printf("sio%d: gdb debugging port\n", unit);
3019				siogdbiobase = iobase;
3020				siogdbunit = unit;
3021#if DDB > 0
3022				gdbconsdev.cn_dev = makedev(CDEV_MAJOR, unit);
3023				gdb_arg = &gdbconsdev;
3024				gdb_getc = siocngetc;
3025				gdb_putc = siocnputc;
3026#endif
3027			}
3028		}
3029	}
3030#ifdef	__i386__
3031#if DDB > 0
3032	/*
3033	 * XXX Ugly Compatability.
3034	 * If no gdb port has been specified, set it to be the console
3035	 * as some configuration files don't specify the gdb port.
3036	 */
3037	if (gdb_arg == NULL && (boothowto & RB_GDB)) {
3038		printf("Warning: no GDB port specified. Defaulting to sio%d.\n",
3039			siocnunit);
3040		printf("Set flag 0x80 on desired GDB port in your\n");
3041		printf("configuration file (currently sio only).\n");
3042		siogdbiobase = siocniobase;
3043		siogdbunit = siocnunit;
3044		gdbconsdev.cn_dev = makedev(CDEV_MAJOR, siocnunit);
3045		gdb_arg = &gdbconsdev;
3046		gdb_getc = siocngetc;
3047		gdb_putc = siocnputc;
3048	}
3049#endif
3050#endif
3051}
3052
3053static void
3054siocninit(cp)
3055	struct consdev	*cp;
3056{
3057	comconsole = DEV_TO_UNIT(cp->cn_dev);
3058}
3059
3060static void
3061siocnterm(cp)
3062	struct consdev	*cp;
3063{
3064	comconsole = -1;
3065}
3066
3067#endif
3068
3069#ifdef __alpha__
3070
3071CONS_DRIVER(sio, NULL, NULL, NULL, siocngetc, siocncheckc, siocnputc, NULL);
3072
3073int
3074siocnattach(port, speed)
3075	int port;
3076	int speed;
3077{
3078	int			s;
3079	u_char			cfcr;
3080	u_int			divisor;
3081	struct siocnstate	sp;
3082	int			unit = 0;	/* XXX random value! */
3083
3084	siocniobase = port;
3085	siocnunit = unit;
3086	comdefaultrate = speed;
3087	sio_consdev.cn_pri = CN_NORMAL;
3088	sio_consdev.cn_dev = makedev(CDEV_MAJOR, unit);
3089
3090	s = spltty();
3091
3092	/*
3093	 * Initialize the divisor latch.  We can't rely on
3094	 * siocnopen() to do this the first time, since it
3095	 * avoids writing to the latch if the latch appears
3096	 * to have the correct value.  Also, if we didn't
3097	 * just read the speed from the hardware, then we
3098	 * need to set the speed in hardware so that
3099	 * switching it later is null.
3100	 */
3101	cfcr = inb(siocniobase + com_cfcr);
3102	outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr);
3103	divisor = siodivisor(comdefaultrclk, comdefaultrate);
3104	outb(siocniobase + com_dlbl, divisor & 0xff);
3105	outb(siocniobase + com_dlbh, divisor >> 8);
3106	outb(siocniobase + com_cfcr, cfcr);
3107
3108	siocnopen(&sp, siocniobase, comdefaultrate);
3109	splx(s);
3110
3111	cnadd(&sio_consdev);
3112	return (0);
3113}
3114
3115int
3116siogdbattach(port, speed)
3117	int port;
3118	int speed;
3119{
3120	int			s;
3121	u_char			cfcr;
3122	u_int			divisor;
3123	struct siocnstate	sp;
3124	int			unit = 1;	/* XXX random value! */
3125
3126	siogdbiobase = port;
3127	gdbdefaultrate = speed;
3128
3129	printf("sio%d: gdb debugging port\n", unit);
3130	siogdbunit = unit;
3131#if DDB > 0
3132	gdbconsdev.cn_dev = makedev(CDEV_MAJOR, unit);
3133	gdb_arg = &gdbconsdev;
3134	gdb_getc = siocngetc;
3135	gdb_putc = siocnputc;
3136#endif
3137
3138	s = spltty();
3139
3140	/*
3141	 * Initialize the divisor latch.  We can't rely on
3142	 * siocnopen() to do this the first time, since it
3143	 * avoids writing to the latch if the latch appears
3144	 * to have the correct value.  Also, if we didn't
3145	 * just read the speed from the hardware, then we
3146	 * need to set the speed in hardware so that
3147	 * switching it later is null.
3148	 */
3149	cfcr = inb(siogdbiobase + com_cfcr);
3150	outb(siogdbiobase + com_cfcr, CFCR_DLAB | cfcr);
3151	divisor = siodivisor(comdefaultrclk, gdbdefaultrate);
3152	outb(siogdbiobase + com_dlbl, divisor & 0xff);
3153	outb(siogdbiobase + com_dlbh, divisor >> 8);
3154	outb(siogdbiobase + com_cfcr, cfcr);
3155
3156	siocnopen(&sp, siogdbiobase, gdbdefaultrate);
3157	splx(s);
3158
3159	return (0);
3160}
3161
3162#endif
3163
3164static int
3165siocncheckc(struct consdev *cd)
3166{
3167	int	c;
3168	dev_t	dev;
3169	Port_t	iobase;
3170	int	s;
3171	struct siocnstate	sp;
3172	speed_t	speed;
3173
3174	dev = cd->cn_dev;
3175	if (minor(dev) == siocnunit) {
3176		iobase = siocniobase;
3177		speed = comdefaultrate;
3178	} else {
3179		iobase = siogdbiobase;
3180		speed = gdbdefaultrate;
3181	}
3182	s = spltty();
3183	siocnopen(&sp, iobase, speed);
3184	if (inb(iobase + com_lsr) & LSR_RXRDY)
3185		c = inb(iobase + com_data);
3186	else
3187		c = -1;
3188	siocnclose(&sp, iobase);
3189	splx(s);
3190	return (c);
3191}
3192
3193
3194static int
3195siocngetc(struct consdev *cd)
3196{
3197	int	c;
3198	dev_t	dev;
3199	Port_t	iobase;
3200	int	s;
3201	struct siocnstate	sp;
3202	speed_t	speed;
3203
3204	dev = cd->cn_dev;
3205	if (minor(dev) == siocnunit) {
3206		iobase = siocniobase;
3207		speed = comdefaultrate;
3208	} else {
3209		iobase = siogdbiobase;
3210		speed = gdbdefaultrate;
3211	}
3212	s = spltty();
3213	siocnopen(&sp, iobase, speed);
3214	while (!(inb(iobase + com_lsr) & LSR_RXRDY))
3215		;
3216	c = inb(iobase + com_data);
3217	siocnclose(&sp, iobase);
3218	splx(s);
3219	return (c);
3220}
3221
3222static void
3223siocnputc(struct consdev *cd, int c)
3224{
3225	int	need_unlock;
3226	int	s;
3227	dev_t	dev;
3228	struct siocnstate	sp;
3229	Port_t	iobase;
3230	speed_t	speed;
3231
3232	dev = cd->cn_dev;
3233	if (minor(dev) == siocnunit) {
3234		iobase = siocniobase;
3235		speed = comdefaultrate;
3236	} else {
3237		iobase = siogdbiobase;
3238		speed = gdbdefaultrate;
3239	}
3240	s = spltty();
3241	need_unlock = 0;
3242	if (sio_inited == 2 && !mtx_owned(&sio_lock)) {
3243		mtx_lock_spin(&sio_lock);
3244		need_unlock = 1;
3245	}
3246	siocnopen(&sp, iobase, speed);
3247	siocntxwait(iobase);
3248	outb(iobase + com_data, c);
3249	siocnclose(&sp, iobase);
3250	if (need_unlock)
3251		mtx_unlock_spin(&sio_lock);
3252	splx(s);
3253}
3254
3255#ifdef __alpha__
3256int
3257siogdbgetc()
3258{
3259	int	c;
3260	Port_t	iobase;
3261	speed_t	speed;
3262	int	s;
3263	struct siocnstate	sp;
3264
3265	if (siogdbunit == siocnunit) {
3266		iobase = siocniobase;
3267		speed = comdefaultrate;
3268	} else {
3269		iobase = siogdbiobase;
3270		speed = gdbdefaultrate;
3271	}
3272
3273	s = spltty();
3274	siocnopen(&sp, iobase, speed);
3275	while (!(inb(iobase + com_lsr) & LSR_RXRDY))
3276		;
3277	c = inb(iobase + com_data);
3278	siocnclose(&sp, iobase);
3279	splx(s);
3280	return (c);
3281}
3282
3283void
3284siogdbputc(c)
3285	int	c;
3286{
3287	Port_t	iobase;
3288	speed_t	speed;
3289	int	s;
3290	struct siocnstate	sp;
3291
3292	if (siogdbunit == siocnunit) {
3293		iobase = siocniobase;
3294		speed = comdefaultrate;
3295	} else {
3296		iobase = siogdbiobase;
3297		speed = gdbdefaultrate;
3298	}
3299
3300	s = spltty();
3301	siocnopen(&sp, iobase, speed);
3302	siocntxwait(siogdbiobase);
3303	outb(siogdbiobase + com_data, c);
3304	siocnclose(&sp, siogdbiobase);
3305	splx(s);
3306}
3307#endif
3308