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