si.c revision 69781
1/*
2 * Device driver for Specialix range (SI/XIO) of serial line multiplexors.
3 *
4 * Copyright (C) 1990, 1992, 1998 Specialix International,
5 * Copyright (C) 1993, Andy Rutter <andy@acronym.co.uk>
6 * Copyright (C) 2000, Peter Wemm <peter@netplex.com.au>
7 *
8 * Originally derived from:	SunOS 4.x version
9 * Ported from BSDI version to FreeBSD by Peter Wemm.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notices, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notices, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by Andy Rutter of
22 *	Advanced Methods and Tools Ltd. based on original information
23 *	from Specialix International.
24 * 4. Neither the name of Advanced Methods and Tools, nor Specialix
25 *    International may be used to endorse or promote products derived from
26 *    this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
31 * NO EVENT SHALL THE AUTHORS BE LIABLE.
32 *
33 * $FreeBSD: head/sys/dev/si/si.c 69781 2000-12-08 21:51:06Z dwmalone $
34 */
35
36#ifndef lint
37static const char si_copyright1[] =  "@(#) Copyright (C) Specialix International, 1990,1992,1998",
38		  si_copyright2[] =  "@(#) Copyright (C) Andy Rutter 1993",
39		  si_copyright3[] =  "@(#) Copyright (C) Peter Wemm 2000";
40#endif	/* not lint */
41
42#include "opt_compat.h"
43#include "opt_debug_si.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
48#include <sys/ioctl_compat.h>
49#endif
50#include <sys/tty.h>
51#include <sys/conf.h>
52#include <sys/fcntl.h>
53#include <sys/dkstat.h>
54#include <sys/kernel.h>
55#include <sys/malloc.h>
56#include <sys/sysctl.h>
57#include <sys/bus.h>
58#include <machine/bus.h>
59#include <sys/rman.h>
60#include <machine/resource.h>
61
62
63#include <vm/vm.h>
64#include <vm/pmap.h>
65
66#include <machine/stdarg.h>
67
68#include <dev/si/sireg.h>
69#include <dev/si/sivar.h>
70#include <dev/si/si.h>
71
72/*
73 * This device driver is designed to interface the Specialix International
74 * SI, XIO and SX range of serial multiplexor cards to FreeBSD on an ISA,
75 * EISA or PCI bus machine.
76 *
77 * The controller is interfaced to the host via dual port RAM
78 * and an interrupt.
79 *
80 * The code for the Host 1 (very old ISA cards) has not been tested.
81 */
82
83#define	POLL		/* turn on poller to scan for lost interrupts */
84#define REALPOLL	/* on each poll, scan for work regardless */
85#define POLLHZ	(hz/10)	/* 10 times per second */
86#define SI_I_HIGH_WATER	(TTYHOG - 2 * SI_BUFFERSIZE)
87#define INT_COUNT 25000		/* max of 125 ints per second */
88#define JET_INT_COUNT 100	/* max of 100 ints per second */
89#define RXINT_COUNT 1	/* one rxint per 10 milliseconds */
90
91enum si_mctl { GET, SET, BIS, BIC };
92
93static void si_command(struct si_port *, int, int);
94static int si_modem(struct si_port *, enum si_mctl, int);
95static void si_write_enable(struct si_port *, int);
96static int si_Sioctl(dev_t, u_long, caddr_t, int, struct proc *);
97static void si_start(struct tty *);
98static void si_stop(struct tty *, int);
99static timeout_t si_lstart;
100static void si_disc_optim(struct tty *tp, struct termios *t,struct si_port *pp);
101static void sihardclose(struct si_port *pp);
102static void sidtrwakeup(void *chan);
103
104#ifdef SI_DEBUG
105static char	*si_mctl2str(enum si_mctl cmd);
106#endif
107
108static int	siparam(struct tty *, struct termios *);
109
110static void	si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip);
111static char *	si_modulename(int host_type, int uart_type);
112
113static	d_open_t	siopen;
114static	d_close_t	siclose;
115static	d_write_t	siwrite;
116static	d_ioctl_t	siioctl;
117
118#define	CDEV_MAJOR	68
119static struct cdevsw si_cdevsw = {
120	/* open */	siopen,
121	/* close */	siclose,
122	/* read */	ttyread,
123	/* write */	siwrite,
124	/* ioctl */	siioctl,
125	/* poll */	ttypoll,
126	/* mmap */	nommap,
127	/* strategy */	nostrategy,
128	/* name */	"si",
129	/* maj */	CDEV_MAJOR,
130	/* dump */	nodump,
131	/* psize */	nopsize,
132	/* flags */	D_TTY,
133	/* bmaj */	-1
134};
135
136static int si_Nports;
137static int si_Nmodules;
138static int si_debug = 0;	/* data, not bss, so it's patchable */
139
140SYSCTL_INT(_machdep, OID_AUTO, si_debug, CTLFLAG_RW, &si_debug, 0, "");
141
142static struct tty *si__tty;
143
144static int si_numunits;
145
146devclass_t si_devclass;
147
148#ifndef B2000	/* not standard, but the hardware knows it. */
149# define B2000 2000
150#endif
151static struct speedtab bdrates[] = {
152	{ B75,		CLK75, },	/* 0x0 */
153	{ B110,		CLK110, },	/* 0x1 */
154	{ B150,		CLK150, },	/* 0x3 */
155	{ B300,		CLK300, },	/* 0x4 */
156	{ B600,		CLK600, },	/* 0x5 */
157	{ B1200,	CLK1200, },	/* 0x6 */
158	{ B2000,	CLK2000, },	/* 0x7 */
159	{ B2400,	CLK2400, },	/* 0x8 */
160	{ B4800,	CLK4800, },	/* 0x9 */
161	{ B9600,	CLK9600, },	/* 0xb */
162	{ B19200,	CLK19200, },	/* 0xc */
163	{ B38400,	CLK38400, },	/* 0x2 (out of order!) */
164	{ B57600,	CLK57600, },	/* 0xd */
165	{ B115200,	CLK110, },	/* 0x1 (dupe!, 110 baud on "si") */
166	{ -1,		-1 },
167};
168
169
170/* populated with approx character/sec rates - translated at card
171 * initialisation time to chars per tick of the clock */
172static int done_chartimes = 0;
173static struct speedtab chartimes[] = {
174	{ B75,		8, },
175	{ B110,		11, },
176	{ B150,		15, },
177	{ B300,		30, },
178	{ B600,		60, },
179	{ B1200,	120, },
180	{ B2000,	200, },
181	{ B2400,	240, },
182	{ B4800,	480, },
183	{ B9600,	960, },
184	{ B19200,	1920, },
185	{ B38400,	3840, },
186	{ B57600,	5760, },
187	{ B115200,	11520, },
188	{ -1,		-1 },
189};
190static volatile int in_intr = 0;	/* Inside interrupt handler? */
191
192#ifdef POLL
193static int si_pollrate;			/* in addition to irq */
194static int si_realpoll = 0;		/* poll HW on timer */
195
196SYSCTL_INT(_machdep, OID_AUTO, si_pollrate, CTLFLAG_RW, &si_pollrate, 0, "");
197SYSCTL_INT(_machdep, OID_AUTO, si_realpoll, CTLFLAG_RW, &si_realpoll, 0, "");
198
199static int init_finished = 0;
200static void si_poll(void *);
201#endif
202
203/*
204 * Array of adapter types and the corresponding RAM size. The order of
205 * entries here MUST match the ordinal of the adapter type.
206 */
207static char *si_type[] = {
208	"EMPTY",
209	"SIHOST",
210	"SIMCA",		/* FreeBSD does not support Microchannel */
211	"SIHOST2",
212	"SIEISA",
213	"SIPCI",
214	"SXPCI",
215	"SXISA",
216};
217
218/*
219 * We have to make an 8 bit version of bcopy, since some cards can't
220 * deal with 32 bit I/O
221 */
222static void __inline
223si_bcopy(const void *src, void *dst, size_t len)
224{
225	while (len--)
226		*(((u_char *)dst)++) = *(((const u_char *)src)++);
227}
228static void __inline
229si_vbcopy(const volatile void *src, void *dst, size_t len)
230{
231	while (len--)
232		*(((u_char *)dst)++) = *(((const volatile u_char *)src)++);
233}
234static void __inline
235si_bcopyv(const void *src, volatile void *dst, size_t len)
236{
237	while (len--)
238		*(((volatile u_char *)dst)++) = *(((const u_char *)src)++);
239}
240
241
242/*
243 * Attach the device.  Initialize the card.
244 */
245int
246siattach(device_t dev)
247{
248	int unit;
249	struct si_softc *sc;
250	struct si_port *pp;
251	volatile struct si_channel *ccbp;
252	volatile struct si_reg *regp;
253	volatile caddr_t maddr;
254	struct si_module *modp;
255	struct tty *tp;
256	struct speedtab *spt;
257	int nmodule, nport, x, y;
258	int uart_type;
259
260	sc = device_get_softc(dev);
261	unit = device_get_unit(dev);
262
263	sc->sc_typename = si_type[sc->sc_type];
264	if (si_numunits < unit + 1)
265		si_numunits = unit + 1;
266
267	DPRINT((0, DBG_AUTOBOOT, "si%d: siattach\n", unit));
268
269#ifdef POLL
270	if (si_pollrate == 0) {
271		si_pollrate = POLLHZ;		/* in addition to irq */
272#ifdef REALPOLL
273		si_realpoll = 1;		/* scan always */
274#endif
275	}
276#endif
277
278	DPRINT((0, DBG_AUTOBOOT, "si%d: type: %s paddr: %x maddr: %x\n", unit,
279		sc->sc_typename, sc->sc_paddr, sc->sc_maddr));
280
281	sc->sc_ports = NULL;			/* mark as uninitialised */
282
283	maddr = sc->sc_maddr;
284
285	/* Stop the CPU first so it won't stomp around while we load */
286
287	switch (sc->sc_type) {
288		case SIEISA:
289			outb(sc->sc_iobase + 2, sc->sc_irq << 4);
290		break;
291		case SIPCI:
292			*(maddr+SIPCIRESET) = 0;
293		break;
294		case SIJETPCI: /* fall through to JET ISA */
295		case SIJETISA:
296			*(maddr+SIJETCONFIG) = 0;
297		break;
298		case SIHOST2:
299			*(maddr+SIPLRESET) = 0;
300		break;
301		case SIHOST:
302			*(maddr+SIRESET) = 0;
303		break;
304		default: /* this should never happen */
305			printf("si%d: unsupported configuration\n", unit);
306			return EINVAL;
307		break;
308	}
309
310	/* OK, now lets download the download code */
311
312	if (SI_ISJET(sc->sc_type)) {
313		DPRINT((0, DBG_DOWNLOAD, "si%d: jet_download: nbytes %d\n",
314			unit, si3_t225_dsize));
315		si_bcopy(si3_t225_download, maddr + si3_t225_downloadaddr,
316			si3_t225_dsize);
317		DPRINT((0, DBG_DOWNLOAD,
318			"si%d: jet_bootstrap: nbytes %d -> %x\n",
319			unit, si3_t225_bsize, si3_t225_bootloadaddr));
320		si_bcopy(si3_t225_bootstrap, maddr + si3_t225_bootloadaddr,
321			si3_t225_bsize);
322	} else {
323		DPRINT((0, DBG_DOWNLOAD, "si%d: si_download: nbytes %d\n",
324			unit, si2_z280_dsize));
325		si_bcopy(si2_z280_download, maddr + si2_z280_downloadaddr,
326			si2_z280_dsize);
327	}
328
329	/* Now start the CPU */
330
331	switch (sc->sc_type) {
332	case SIEISA:
333		/* modify the download code to tell it that it's on an EISA */
334		*(maddr + 0x42) = 1;
335		outb(sc->sc_iobase + 2, (sc->sc_irq << 4) | 4);
336		(void)inb(sc->sc_iobase + 3); /* reset interrupt */
337		break;
338	case SIPCI:
339		/* modify the download code to tell it that it's on a PCI */
340		*(maddr+0x42) = 1;
341		*(maddr+SIPCIRESET) = 1;
342		*(maddr+SIPCIINTCL) = 0;
343		break;
344	case SIJETPCI:
345		*(maddr+SIJETRESET) = 0;
346		*(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN;
347		break;
348	case SIJETISA:
349		*(maddr+SIJETRESET) = 0;
350		switch (sc->sc_irq) {
351		case 9:
352			*(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0x90;
353			break;
354		case 10:
355			*(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xa0;
356			break;
357		case 11:
358			*(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xb0;
359			break;
360		case 12:
361			*(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xc0;
362			break;
363		case 15:
364			*(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xf0;
365			break;
366		}
367		break;
368	case SIHOST:
369		*(maddr+SIRESET_CL) = 0;
370		*(maddr+SIINTCL_CL) = 0;
371		break;
372	case SIHOST2:
373		*(maddr+SIPLRESET) = 0x10;
374		switch (sc->sc_irq) {
375		case 11:
376			*(maddr+SIPLIRQ11) = 0x10;
377			break;
378		case 12:
379			*(maddr+SIPLIRQ12) = 0x10;
380			break;
381		case 15:
382			*(maddr+SIPLIRQ15) = 0x10;
383			break;
384		}
385		*(maddr+SIPLIRQCLR) = 0x10;
386		break;
387	default: /* this should _REALLY_ never happen */
388		printf("si%d: Uh, it was supported a second ago...\n", unit);
389		return EINVAL;
390	}
391
392	DELAY(1000000);			/* wait around for a second */
393
394	regp = (struct si_reg *)maddr;
395	y = 0;
396					/* wait max of 5 sec for init OK */
397	while (regp->initstat == 0 && y++ < 10) {
398		DELAY(500000);
399	}
400	switch (regp->initstat) {
401	case 0:
402		printf("si%d: startup timeout - aborting\n", unit);
403		sc->sc_type = SIEMPTY;
404		return EINVAL;
405	case 1:
406		if (SI_ISJET(sc->sc_type)) {
407			/* set throttle to 100 times per second */
408			regp->int_count = JET_INT_COUNT;
409			/* rx_intr_count is a NOP in Jet */
410		} else {
411			/* set throttle to 125 times per second */
412			regp->int_count = INT_COUNT;
413			/* rx intr max of 25 times per second */
414			regp->rx_int_count = RXINT_COUNT;
415		}
416		regp->int_pending = 0;		/* no intr pending */
417		regp->int_scounter = 0;	/* reset counter */
418		break;
419	case 0xff:
420		/*
421		 * No modules found, so give up on this one.
422		 */
423		printf("si%d: %s - no ports found\n", unit,
424			si_type[sc->sc_type]);
425		return 0;
426	default:
427		printf("si%d: download code version error - initstat %x\n",
428			unit, regp->initstat);
429		return EINVAL;
430	}
431
432	/*
433	 * First time around the ports just count them in order
434	 * to allocate some memory.
435	 */
436	nport = 0;
437	modp = (struct si_module *)(maddr + 0x80);
438	for (;;) {
439		DPRINT((0, DBG_DOWNLOAD, "si%d: ccb addr 0x%x\n", unit, modp));
440		switch (modp->sm_type) {
441		case TA4:
442			DPRINT((0, DBG_DOWNLOAD,
443				"si%d: Found old TA4 module, 4 ports\n",
444				unit));
445			x = 4;
446			break;
447		case TA8:
448			DPRINT((0, DBG_DOWNLOAD,
449				"si%d: Found old TA8 module, 8 ports\n",
450				unit));
451			x = 8;
452			break;
453		case TA4_ASIC:
454			DPRINT((0, DBG_DOWNLOAD,
455				"si%d: Found ASIC TA4 module, 4 ports\n",
456				unit));
457			x = 4;
458			break;
459		case TA8_ASIC:
460			DPRINT((0, DBG_DOWNLOAD,
461				"si%d: Found ASIC TA8 module, 8 ports\n",
462				unit));
463			x = 8;
464			break;
465		case MTA:
466			DPRINT((0, DBG_DOWNLOAD,
467				"si%d: Found CD1400 module, 8 ports\n",
468				unit));
469			x = 8;
470			break;
471		case SXDC:
472			DPRINT((0, DBG_DOWNLOAD,
473				"si%d: Found SXDC module, 8 ports\n",
474				unit));
475			x = 8;
476			break;
477		default:
478			printf("si%d: unknown module type %d\n",
479				unit, modp->sm_type);
480			goto try_next;
481		}
482
483		/* this was limited in firmware and is also a driver issue */
484		if ((nport + x) > SI_MAXPORTPERCARD) {
485			printf("si%d: extra ports ignored\n", unit);
486			goto try_next;
487		}
488
489		nport += x;
490		si_Nports += x;
491		si_Nmodules++;
492
493try_next:
494		if (modp->sm_next == 0)
495			break;
496		modp = (struct si_module *)
497			(maddr + (unsigned)(modp->sm_next & 0x7fff));
498	}
499	sc->sc_ports = (struct si_port *)malloc(sizeof(struct si_port) * nport,
500		M_DEVBUF, M_NOWAIT | M_ZERO);
501	if (sc->sc_ports == 0) {
502mem_fail:
503		printf("si%d: fail to malloc memory for port structs\n",
504			unit);
505		return EINVAL;
506	}
507	sc->sc_nport = nport;
508
509	/*
510	 * allocate tty structures for ports
511	 */
512	tp = (struct tty *)malloc(sizeof(*tp) * nport, M_DEVBUF,
513		M_NOWAIT | M_ZERO);
514	if (tp == 0)
515		goto mem_fail;
516	si__tty = tp;
517
518	/*
519	 * Scan round the ports again, this time initialising.
520	 */
521	pp = sc->sc_ports;
522	nmodule = 0;
523	modp = (struct si_module *)(maddr + 0x80);
524	uart_type = 1000;	/* arbitary, > uchar_max */
525	for (;;) {
526		switch (modp->sm_type) {
527		case TA4:
528			nport = 4;
529			break;
530		case TA8:
531			nport = 8;
532			break;
533		case TA4_ASIC:
534			nport = 4;
535			break;
536		case TA8_ASIC:
537			nport = 8;
538			break;
539		case MTA:
540			nport = 8;
541			break;
542		case SXDC:
543			nport = 8;
544			break;
545		default:
546			goto try_next2;
547		}
548		nmodule++;
549		ccbp = (struct si_channel *)((char *)modp + 0x100);
550		if (uart_type == 1000)
551			uart_type = ccbp->type;
552		else if (uart_type != ccbp->type)
553			printf("si%d: Warning: module %d mismatch! (%d%s != %d%s)\n",
554			    unit, nmodule,
555			    ccbp->type, si_modulename(sc->sc_type, ccbp->type),
556			    uart_type, si_modulename(sc->sc_type, uart_type));
557
558		for (x = 0; x < nport; x++, pp++, ccbp++) {
559			pp->sp_ccb = ccbp;	/* save the address */
560			pp->sp_tty = tp++;
561			pp->sp_pend = IDLE_CLOSE;
562			pp->sp_state = 0;	/* internal flag */
563			pp->sp_dtr_wait = 3 * hz;
564			pp->sp_iin.c_iflag = TTYDEF_IFLAG;
565			pp->sp_iin.c_oflag = TTYDEF_OFLAG;
566			pp->sp_iin.c_cflag = TTYDEF_CFLAG;
567			pp->sp_iin.c_lflag = TTYDEF_LFLAG;
568			termioschars(&pp->sp_iin);
569			pp->sp_iin.c_ispeed = pp->sp_iin.c_ospeed =
570				TTYDEF_SPEED;;
571			pp->sp_iout = pp->sp_iin;
572		}
573try_next2:
574		if (modp->sm_next == 0) {
575			printf("si%d: card: %s, ports: %d, modules: %d, type: %d%s\n",
576				unit,
577				sc->sc_typename,
578				sc->sc_nport,
579				nmodule,
580				uart_type,
581				si_modulename(sc->sc_type, uart_type));
582			break;
583		}
584		modp = (struct si_module *)
585			(maddr + (unsigned)(modp->sm_next & 0x7fff));
586	}
587	if (done_chartimes == 0) {
588		for (spt = chartimes ; spt->sp_speed != -1; spt++) {
589			if ((spt->sp_code /= hz) == 0)
590				spt->sp_code = 1;
591		}
592		done_chartimes = 1;
593	}
594
595/*	path	name	devsw		minor	type   uid gid perm*/
596	for (x = 0; x < sc->sc_nport; x++) {
597		/* sync with the manuals that start at 1 */
598		y = x + 1 + unit * (1 << SI_CARDSHIFT);
599		make_dev(&si_cdevsw, x, 0, 0, 0600, "ttyA%02d", y);
600		make_dev(&si_cdevsw, x + 0x00080, 0, 0, 0600, "cuaA%02d", y);
601		make_dev(&si_cdevsw, x + 0x10000, 0, 0, 0600, "ttyiA%02d", y);
602		make_dev(&si_cdevsw, x + 0x10080, 0, 0, 0600, "cuaiA%02d", y);
603		make_dev(&si_cdevsw, x + 0x20000, 0, 0, 0600, "ttylA%02d", y);
604		make_dev(&si_cdevsw, x + 0x20080, 0, 0, 0600, "cualA%02d", y);
605	}
606	make_dev(&si_cdevsw, 0x40000, 0, 0, 0600, "si_control");
607	return (0);
608}
609
610static	int
611siopen(dev_t dev, int flag, int mode, struct proc *p)
612{
613	int oldspl, error;
614	int card, port;
615	struct si_softc *sc;
616	struct tty *tp;
617	volatile struct si_channel *ccbp;
618	struct si_port *pp;
619	int mynor = minor(dev);
620
621	/* quickly let in /dev/si_control */
622	if (IS_CONTROLDEV(mynor)) {
623		if ((error = suser(p)))
624			return(error);
625		return(0);
626	}
627
628	card = SI_CARD(mynor);
629	sc = devclass_get_softc(si_devclass, card);
630	if (sc == NULL)
631		return (ENXIO);
632
633	if (sc->sc_type == SIEMPTY) {
634		DPRINT((0, DBG_OPEN|DBG_FAIL, "si%d: type %s??\n",
635			card, sc->sc_typename));
636		return(ENXIO);
637	}
638
639	port = SI_PORT(mynor);
640	if (port >= sc->sc_nport) {
641		DPRINT((0, DBG_OPEN|DBG_FAIL, "si%d: nports %d\n",
642			card, sc->sc_nport));
643		return(ENXIO);
644	}
645
646#ifdef	POLL
647	/*
648	 * We've now got a device, so start the poller.
649	 */
650	if (init_finished == 0) {
651		timeout(si_poll, (caddr_t)0L, si_pollrate);
652		init_finished = 1;
653	}
654#endif
655
656	/* initial/lock device */
657	if (IS_STATE(mynor)) {
658		return(0);
659	}
660
661	pp = sc->sc_ports + port;
662	tp = pp->sp_tty;			/* the "real" tty */
663	dev->si_tty = tp;
664	ccbp = pp->sp_ccb;			/* Find control block */
665	DPRINT((pp, DBG_ENTRY|DBG_OPEN, "siopen(%s,%x,%x,%x)\n",
666		devtoname(dev), flag, mode, p));
667
668	oldspl = spltty();			/* Keep others out */
669	error = 0;
670
671open_top:
672	while (pp->sp_state & SS_DTR_OFF) {
673		error = tsleep(&pp->sp_dtr_wait, TTIPRI|PCATCH, "sidtr", 0);
674		if (error != 0)
675			goto out;
676	}
677
678	if (tp->t_state & TS_ISOPEN) {
679		/*
680		 * The device is open, so everything has been initialised.
681		 * handle conflicts.
682		 */
683		if (IS_CALLOUT(mynor)) {
684			if (!pp->sp_active_out) {
685				error = EBUSY;
686				goto out;
687			}
688		} else {
689			if (pp->sp_active_out) {
690				if (flag & O_NONBLOCK) {
691					error = EBUSY;
692					goto out;
693				}
694				error = tsleep(&pp->sp_active_out,
695						TTIPRI|PCATCH, "sibi", 0);
696				if (error != 0)
697					goto out;
698				goto open_top;
699			}
700		}
701		if (tp->t_state & TS_XCLUDE &&
702		    suser(p)) {
703			DPRINT((pp, DBG_OPEN|DBG_FAIL,
704				"already open and EXCLUSIVE set\n"));
705			error = EBUSY;
706			goto out;
707		}
708	} else {
709		/*
710		 * The device isn't open, so there are no conflicts.
711		 * Initialize it. Avoid sleep... :-)
712		 */
713		DPRINT((pp, DBG_OPEN, "first open\n"));
714		tp->t_oproc = si_start;
715		tp->t_stop = si_stop;
716		tp->t_param = siparam;
717		tp->t_dev = dev;
718		tp->t_termios = mynor & SI_CALLOUT_MASK
719				? pp->sp_iout : pp->sp_iin;
720
721		(void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
722
723		++pp->sp_wopeners;	/* in case of sleep in siparam */
724
725		error = siparam(tp, &tp->t_termios);
726
727		--pp->sp_wopeners;
728		if (error != 0)
729			goto out;
730		/* XXX: we should goto_top if siparam slept */
731
732		/* set initial DCD state */
733		pp->sp_last_hi_ip = ccbp->hi_ip;
734		if ((pp->sp_last_hi_ip & IP_DCD) || IS_CALLOUT(mynor)) {
735			(*linesw[tp->t_line].l_modem)(tp, 1);
736		}
737	}
738
739	/* whoops! we beat the close! */
740	if (pp->sp_state & SS_CLOSING) {
741		/* try and stop it from proceeding to bash the hardware */
742		pp->sp_state &= ~SS_CLOSING;
743	}
744
745	/*
746	 * Wait for DCD if necessary
747	 */
748	if (!(tp->t_state & TS_CARR_ON) &&
749	    !IS_CALLOUT(mynor) &&
750	    !(tp->t_cflag & CLOCAL) &&
751	    !(flag & O_NONBLOCK)) {
752		++pp->sp_wopeners;
753		DPRINT((pp, DBG_OPEN, "sleeping for carrier\n"));
754		error = tsleep(TSA_CARR_ON(tp), TTIPRI|PCATCH, "sidcd", 0);
755		--pp->sp_wopeners;
756		if (error != 0)
757			goto out;
758		goto open_top;
759	}
760
761	error = (*linesw[tp->t_line].l_open)(dev, tp);
762	si_disc_optim(tp, &tp->t_termios, pp);
763	if (tp->t_state & TS_ISOPEN && IS_CALLOUT(mynor))
764		pp->sp_active_out = TRUE;
765
766	pp->sp_state |= SS_OPEN;	/* made it! */
767
768out:
769	splx(oldspl);
770
771	DPRINT((pp, DBG_OPEN, "leaving siopen\n"));
772
773	if (!(tp->t_state & TS_ISOPEN) && pp->sp_wopeners == 0)
774		sihardclose(pp);
775
776	return(error);
777}
778
779static	int
780siclose(dev_t dev, int flag, int mode, struct proc *p)
781{
782	struct si_port *pp;
783	struct tty *tp;
784	int oldspl;
785	int error = 0;
786	int mynor = minor(dev);
787
788	if (IS_SPECIAL(mynor))
789		return(0);
790
791	oldspl = spltty();
792
793	pp = MINOR2PP(mynor);
794	tp = pp->sp_tty;
795
796	DPRINT((pp, DBG_ENTRY|DBG_CLOSE, "siclose(%s,%x,%x,%x) sp_state:%x\n",
797		devtoname(dev), flag, mode, p, pp->sp_state));
798
799	/* did we sleep and loose a race? */
800	if (pp->sp_state & SS_CLOSING) {
801		/* error = ESOMETING? */
802		goto out;
803	}
804
805	/* begin race detection.. */
806	pp->sp_state |= SS_CLOSING;
807
808	si_write_enable(pp, 0);		/* block writes for ttywait() */
809
810	/* THIS MAY SLEEP IN TTYWAIT!!! */
811	(*linesw[tp->t_line].l_close)(tp, flag);
812
813	si_write_enable(pp, 1);
814
815	/* did we sleep and somebody started another open? */
816	if (!(pp->sp_state & SS_CLOSING)) {
817		/* error = ESOMETING? */
818		goto out;
819	}
820	/* ok. we are now still on the right track.. nuke the hardware */
821
822	if (pp->sp_state & SS_LSTART) {
823		untimeout(si_lstart, (caddr_t)pp, pp->lstart_ch);
824		pp->sp_state &= ~SS_LSTART;
825	}
826
827	si_stop(tp, FREAD | FWRITE);
828
829	sihardclose(pp);
830	ttyclose(tp);
831	pp->sp_state &= ~SS_OPEN;
832
833out:
834	DPRINT((pp, DBG_CLOSE|DBG_EXIT, "close done, returning\n"));
835	splx(oldspl);
836	return(error);
837}
838
839static void
840sihardclose(struct si_port *pp)
841{
842	int oldspl;
843	struct tty *tp;
844	volatile struct si_channel *ccbp;
845
846	oldspl = spltty();
847
848	tp = pp->sp_tty;
849	ccbp = pp->sp_ccb;			/* Find control block */
850	if (tp->t_cflag & HUPCL ||
851	    (!pp->sp_active_out &&
852	     !(ccbp->hi_ip & IP_DCD) &&
853	     !(pp->sp_iin.c_cflag && CLOCAL)) ||
854	    !(tp->t_state & TS_ISOPEN)) {
855
856		(void) si_modem(pp, BIC, TIOCM_DTR|TIOCM_RTS);
857		(void) si_command(pp, FCLOSE, SI_NOWAIT);
858
859		if (pp->sp_dtr_wait != 0) {
860			timeout(sidtrwakeup, pp, pp->sp_dtr_wait);
861			pp->sp_state |= SS_DTR_OFF;
862		}
863
864	}
865	pp->sp_active_out = FALSE;
866	wakeup((caddr_t)&pp->sp_active_out);
867	wakeup(TSA_CARR_ON(tp));
868
869	splx(oldspl);
870}
871
872
873/*
874 * called at splsoftclock()...
875 */
876static void
877sidtrwakeup(void *chan)
878{
879	struct si_port *pp;
880	int oldspl;
881
882	oldspl = spltty();
883
884	pp = (struct si_port *)chan;
885	pp->sp_state &= ~SS_DTR_OFF;
886	wakeup(&pp->sp_dtr_wait);
887
888	splx(oldspl);
889}
890
891static	int
892siwrite(dev_t dev, struct uio *uio, int flag)
893{
894	struct si_port *pp;
895	struct tty *tp;
896	int error = 0;
897	int mynor = minor(dev);
898	int oldspl;
899
900	if (IS_SPECIAL(mynor)) {
901		DPRINT((0, DBG_ENTRY|DBG_FAIL|DBG_WRITE, "siwrite(CONTROLDEV!!)\n"));
902		return(ENODEV);
903	}
904	pp = MINOR2PP(mynor);
905	tp = pp->sp_tty;
906	DPRINT((pp, DBG_WRITE, "siwrite(%s,%x,%x)\n", devtoname(dev), uio, flag));
907
908	oldspl = spltty();
909	/*
910	 * If writes are currently blocked, wait on the "real" tty
911	 */
912	while (pp->sp_state & SS_BLOCKWRITE) {
913		pp->sp_state |= SS_WAITWRITE;
914		DPRINT((pp, DBG_WRITE, "in siwrite, wait for SS_BLOCKWRITE to clear\n"));
915		if ((error = ttysleep(tp, (caddr_t)pp, TTOPRI|PCATCH,
916				     "siwrite", tp->t_timeout))) {
917			if (error == EWOULDBLOCK)
918				error = EIO;
919			goto out;
920		}
921	}
922
923	error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
924out:
925	splx(oldspl);
926	return (error);
927}
928
929
930static	int
931siioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
932{
933	struct si_port *pp;
934	struct tty *tp;
935	int error;
936	int mynor = minor(dev);
937	int oldspl;
938	int blocked = 0;
939#if defined(COMPAT_43)
940	u_long oldcmd;
941	struct termios term;
942#endif
943
944	if (IS_SI_IOCTL(cmd))
945		return(si_Sioctl(dev, cmd, data, flag, p));
946
947	pp = MINOR2PP(mynor);
948	tp = pp->sp_tty;
949
950	DPRINT((pp, DBG_ENTRY|DBG_IOCTL, "siioctl(%s,%lx,%x,%x)\n",
951		devtoname(dev), cmd, data, flag));
952	if (IS_STATE(mynor)) {
953		struct termios *ct;
954
955		switch (mynor & SI_STATE_MASK) {
956		case SI_INIT_STATE_MASK:
957			ct = IS_CALLOUT(mynor) ? &pp->sp_iout : &pp->sp_iin;
958			break;
959		case SI_LOCK_STATE_MASK:
960			ct = IS_CALLOUT(mynor) ? &pp->sp_lout : &pp->sp_lin;
961			break;
962		default:
963			return (ENODEV);
964		}
965		switch (cmd) {
966		case TIOCSETA:
967			error = suser(p);
968			if (error != 0)
969				return (error);
970			*ct = *(struct termios *)data;
971			return (0);
972		case TIOCGETA:
973			*(struct termios *)data = *ct;
974			return (0);
975		case TIOCGETD:
976			*(int *)data = TTYDISC;
977			return (0);
978		case TIOCGWINSZ:
979			bzero(data, sizeof(struct winsize));
980			return (0);
981		default:
982			return (ENOTTY);
983		}
984	}
985	/*
986	 * Do the old-style ioctl compat routines...
987	 */
988#if defined(COMPAT_43)
989	term = tp->t_termios;
990	oldcmd = cmd;
991	error = ttsetcompat(tp, &cmd, data, &term);
992	if (error != 0)
993		return (error);
994	if (cmd != oldcmd)
995		data = (caddr_t)&term;
996#endif
997	/*
998	 * Do the initial / lock state business
999	 */
1000	if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1001		int     cc;
1002		struct termios *dt = (struct termios *)data;
1003		struct termios *lt = mynor & SI_CALLOUT_MASK
1004				     ? &pp->sp_lout : &pp->sp_lin;
1005
1006		dt->c_iflag = (tp->t_iflag & lt->c_iflag) |
1007			(dt->c_iflag & ~lt->c_iflag);
1008		dt->c_oflag = (tp->t_oflag & lt->c_oflag) |
1009			(dt->c_oflag & ~lt->c_oflag);
1010		dt->c_cflag = (tp->t_cflag & lt->c_cflag) |
1011			(dt->c_cflag & ~lt->c_cflag);
1012		dt->c_lflag = (tp->t_lflag & lt->c_lflag) |
1013			(dt->c_lflag & ~lt->c_lflag);
1014		for (cc = 0; cc < NCCS; ++cc)
1015			if (lt->c_cc[cc] != 0)
1016				dt->c_cc[cc] = tp->t_cc[cc];
1017		if (lt->c_ispeed != 0)
1018			dt->c_ispeed = tp->t_ispeed;
1019		if (lt->c_ospeed != 0)
1020			dt->c_ospeed = tp->t_ospeed;
1021	}
1022
1023	/*
1024	 * Block user-level writes to give the ttywait()
1025	 * a chance to completely drain for commands
1026	 * that require the port to be in a quiescent state.
1027	 */
1028	switch (cmd) {
1029	case TIOCSETAW:
1030	case TIOCSETAF:
1031	case TIOCDRAIN:
1032#ifdef COMPAT_43
1033	case TIOCSETP:
1034#endif
1035		blocked++;	/* block writes for ttywait() and siparam() */
1036		si_write_enable(pp, 0);
1037	}
1038
1039	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1040	if (error != ENOIOCTL)
1041		goto out;
1042
1043	oldspl = spltty();
1044
1045	error = ttioctl(tp, cmd, data, flag);
1046	si_disc_optim(tp, &tp->t_termios, pp);
1047	if (error != ENOIOCTL) {
1048		splx(oldspl);
1049		goto out;
1050	}
1051
1052	error = 0;
1053	switch (cmd) {
1054	case TIOCSBRK:
1055		si_command(pp, SBREAK, SI_WAIT);
1056		break;
1057	case TIOCCBRK:
1058		si_command(pp, EBREAK, SI_WAIT);
1059		break;
1060	case TIOCSDTR:
1061		(void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1062		break;
1063	case TIOCCDTR:
1064		(void) si_modem(pp, SET, 0);
1065		break;
1066	case TIOCMSET:
1067		(void) si_modem(pp, SET, *(int *)data);
1068		break;
1069	case TIOCMBIS:
1070		(void) si_modem(pp, BIS, *(int *)data);
1071		break;
1072	case TIOCMBIC:
1073		(void) si_modem(pp, BIC, *(int *)data);
1074		break;
1075	case TIOCMGET:
1076		*(int *)data = si_modem(pp, GET, 0);
1077		break;
1078	case TIOCMSDTRWAIT:
1079		/* must be root since the wait applies to following logins */
1080		error = suser(p);
1081		if (error == 0)
1082			pp->sp_dtr_wait = *(int *)data * hz / 100;
1083		break;
1084	case TIOCMGDTRWAIT:
1085		*(int *)data = pp->sp_dtr_wait * 100 / hz;
1086		break;
1087	default:
1088		error = ENOTTY;
1089	}
1090	splx(oldspl);
1091
1092out:
1093	DPRINT((pp, DBG_IOCTL|DBG_EXIT, "siioctl ret %d\n", error));
1094	if (blocked)
1095		si_write_enable(pp, 1);
1096	return(error);
1097}
1098
1099/*
1100 * Handle the Specialix ioctls. All MUST be called via the CONTROL device
1101 */
1102static int
1103si_Sioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1104{
1105	struct si_softc *xsc;
1106	struct si_port *xpp;
1107	volatile struct si_reg *regp;
1108	struct si_tcsi *dp;
1109	struct si_pstat *sps;
1110	int *ip, error = 0;
1111	int oldspl;
1112	int card, port;
1113	int mynor = minor(dev);
1114
1115	DPRINT((0, DBG_ENTRY|DBG_IOCTL, "si_Sioctl(%s,%lx,%x,%x)\n",
1116		devtoname(dev), cmd, data, flag));
1117
1118#if 1
1119	DPRINT((0, DBG_IOCTL, "TCSI_PORT=%x\n", TCSI_PORT));
1120	DPRINT((0, DBG_IOCTL, "TCSI_CCB=%x\n", TCSI_CCB));
1121	DPRINT((0, DBG_IOCTL, "TCSI_TTY=%x\n", TCSI_TTY));
1122#endif
1123
1124	if (!IS_CONTROLDEV(mynor)) {
1125		DPRINT((0, DBG_IOCTL|DBG_FAIL, "not called from control device!\n"));
1126		return(ENODEV);
1127	}
1128
1129	oldspl = spltty();	/* better safe than sorry */
1130
1131	ip = (int *)data;
1132
1133#define SUCHECK if ((error = suser(p))) goto out
1134
1135	switch (cmd) {
1136	case TCSIPORTS:
1137		*ip = si_Nports;
1138		goto out;
1139	case TCSIMODULES:
1140		*ip = si_Nmodules;
1141		goto out;
1142	case TCSISDBG_ALL:
1143		SUCHECK;
1144		si_debug = *ip;
1145		goto out;
1146	case TCSIGDBG_ALL:
1147		*ip = si_debug;
1148		goto out;
1149	default:
1150		/*
1151		 * Check that a controller for this port exists
1152		 */
1153
1154		/* may also be a struct si_pstat, a superset of si_tcsi */
1155
1156		dp = (struct si_tcsi *)data;
1157		sps = (struct si_pstat *)data;
1158		card = dp->tc_card;
1159		xsc = devclass_get_softc(si_devclass, card);	/* check.. */
1160		if (xsc == NULL || xsc->sc_type == SIEMPTY) {
1161			error = ENOENT;
1162			goto out;
1163		}
1164		/*
1165		 * And check that a port exists
1166		 */
1167		port = dp->tc_port;
1168		if (port < 0 || port >= xsc->sc_nport) {
1169			error = ENOENT;
1170			goto out;
1171		}
1172		xpp = xsc->sc_ports + port;
1173		regp = (struct si_reg *)xsc->sc_maddr;
1174	}
1175
1176	switch (cmd) {
1177	case TCSIDEBUG:
1178#ifdef	SI_DEBUG
1179		SUCHECK;
1180		if (xpp->sp_debug)
1181			xpp->sp_debug = 0;
1182		else {
1183			xpp->sp_debug = DBG_ALL;
1184			DPRINT((xpp, DBG_IOCTL, "debug toggled %s\n",
1185				(xpp->sp_debug&DBG_ALL)?"ON":"OFF"));
1186		}
1187		break;
1188#else
1189		error = ENODEV;
1190		goto out;
1191#endif
1192	case TCSISDBG_LEVEL:
1193	case TCSIGDBG_LEVEL:
1194#ifdef	SI_DEBUG
1195		if (cmd == TCSIGDBG_LEVEL) {
1196			dp->tc_dbglvl = xpp->sp_debug;
1197		} else {
1198			SUCHECK;
1199			xpp->sp_debug = dp->tc_dbglvl;
1200		}
1201		break;
1202#else
1203		error = ENODEV;
1204		goto out;
1205#endif
1206	case TCSIGRXIT:
1207		dp->tc_int = regp->rx_int_count;
1208		break;
1209	case TCSIRXIT:
1210		SUCHECK;
1211		regp->rx_int_count = dp->tc_int;
1212		break;
1213	case TCSIGIT:
1214		dp->tc_int = regp->int_count;
1215		break;
1216	case TCSIIT:
1217		SUCHECK;
1218		regp->int_count = dp->tc_int;
1219		break;
1220	case TCSISTATE:
1221		dp->tc_int = xpp->sp_ccb->hi_ip;
1222		break;
1223	/* these next three use a different structure */
1224	case TCSI_PORT:
1225		SUCHECK;
1226		si_bcopy(xpp, &sps->tc_siport, sizeof(sps->tc_siport));
1227		break;
1228	case TCSI_CCB:
1229		SUCHECK;
1230		si_vbcopy(xpp->sp_ccb, &sps->tc_ccb, sizeof(sps->tc_ccb));
1231		break;
1232	case TCSI_TTY:
1233		SUCHECK;
1234		si_bcopy(xpp->sp_tty, &sps->tc_tty, sizeof(sps->tc_tty));
1235		break;
1236	default:
1237		error = EINVAL;
1238		goto out;
1239	}
1240out:
1241	splx(oldspl);
1242	return(error);		/* success */
1243}
1244
1245/*
1246 *	siparam()	: Configure line params
1247 *	called at spltty();
1248 *	this may sleep, does not flush, nor wait for drain, nor block writes
1249 *	caller must arrange this if it's important..
1250 */
1251static int
1252siparam(struct tty *tp, struct termios *t)
1253{
1254	struct si_port *pp = TP2PP(tp);
1255	volatile struct si_channel *ccbp;
1256	int oldspl, cflag, iflag, oflag, lflag;
1257	int error = 0;		/* shutup gcc */
1258	int ispeed = 0;		/* shutup gcc */
1259	int ospeed = 0;		/* shutup gcc */
1260	BYTE val;
1261
1262	DPRINT((pp, DBG_ENTRY|DBG_PARAM, "siparam(%x,%x)\n", tp, t));
1263	cflag = t->c_cflag;
1264	iflag = t->c_iflag;
1265	oflag = t->c_oflag;
1266	lflag = t->c_lflag;
1267	DPRINT((pp, DBG_PARAM, "OFLAG 0x%x CFLAG 0x%x IFLAG 0x%x LFLAG 0x%x\n",
1268		oflag, cflag, iflag, lflag));
1269
1270	/* XXX - if Jet host and SXDC module, use extended baud rates */
1271
1272	/* if not hung up.. */
1273	if (t->c_ospeed != 0) {
1274		/* translate baud rate to firmware values */
1275		ospeed = ttspeedtab(t->c_ospeed, bdrates);
1276		ispeed = t->c_ispeed ?
1277			 ttspeedtab(t->c_ispeed, bdrates) : ospeed;
1278
1279		/* enforce legit baud rate */
1280		if (ospeed < 0 || ispeed < 0)
1281			return (EINVAL);
1282	}
1283
1284	oldspl = spltty();
1285
1286	ccbp = pp->sp_ccb;
1287
1288	/* ========== set hi_break ========== */
1289	val = 0;
1290	if (iflag & IGNBRK)		/* Breaks */
1291		val |= BR_IGN;
1292	if (iflag & BRKINT)		/* Interrupt on break? */
1293		val |= BR_INT;
1294	if (iflag & PARMRK)		/* Parity mark? */
1295		val |= BR_PARMRK;
1296	if (iflag & IGNPAR)		/* Ignore chars with parity errors? */
1297		val |= BR_PARIGN;
1298	ccbp->hi_break = val;
1299
1300	/* ========== set hi_csr ========== */
1301	/* if not hung up.. */
1302	if (t->c_ospeed != 0) {
1303		/* Set I/O speeds */
1304		 val = (ispeed << 4) | ospeed;
1305	}
1306	ccbp->hi_csr = val;
1307
1308	/* ========== set hi_mr2 ========== */
1309	val = 0;
1310	if (cflag & CSTOPB)				/* Stop bits */
1311		val |= MR2_2_STOP;
1312	else
1313		val |= MR2_1_STOP;
1314	/*
1315	 * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1316	 * a DCE, hence the reverse sense of RTS and CTS
1317	 */
1318	/* Output Flow - RTS must be raised before data can be sent */
1319	if (cflag & CCTS_OFLOW)
1320		val |= MR2_RTSCONT;
1321
1322	ccbp->hi_mr2 = val;
1323
1324	/* ========== set hi_mr1 ========== */
1325	val = 0;
1326	if (!(cflag & PARENB))				/* Parity */
1327		val |= MR1_NONE;
1328	else
1329		val |= MR1_WITH;
1330	if (cflag & PARODD)
1331		val |= MR1_ODD;
1332
1333	if ((cflag & CS8) == CS8) {			/* 8 data bits? */
1334		val |= MR1_8_BITS;
1335	} else if ((cflag & CS7) == CS7) {		/* 7 data bits? */
1336		val |= MR1_7_BITS;
1337	} else if ((cflag & CS6) == CS6) {		/* 6 data bits? */
1338		val |= MR1_6_BITS;
1339	} else {					/* Must be 5 */
1340		val |= MR1_5_BITS;
1341	}
1342	/*
1343	 * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1344	 * a DCE, hence the reverse sense of RTS and CTS
1345	 */
1346	/* Input Flow - CTS is raised when port is ready to receive data */
1347	if (cflag & CRTS_IFLOW)
1348		val |= MR1_CTSCONT;
1349
1350	ccbp->hi_mr1 = val;
1351
1352	/* ========== set hi_mask ========== */
1353	val = 0xff;
1354	if ((cflag & CS8) == CS8) {			/* 8 data bits? */
1355		val &= 0xFF;
1356	} else if ((cflag & CS7) == CS7) {		/* 7 data bits? */
1357		val &= 0x7F;
1358	} else if ((cflag & CS6) == CS6) {		/* 6 data bits? */
1359		val &= 0x3F;
1360	} else {					/* Must be 5 */
1361		val &= 0x1F;
1362	}
1363	if (iflag & ISTRIP)
1364		val &= 0x7F;
1365
1366	ccbp->hi_mask = val;
1367
1368	/* ========== set hi_prtcl ========== */
1369	val = SP_DCEN;		/* Monitor DCD always, or TIOCMGET misses it */
1370	if (iflag & IXANY)
1371		val |= SP_TANY;
1372	if (iflag & IXON)
1373		val |= SP_TXEN;
1374	if (iflag & IXOFF)
1375		val |= SP_RXEN;
1376	if (iflag & INPCK)
1377		val |= SP_PAEN;
1378
1379	ccbp->hi_prtcl = val;
1380
1381
1382	/* ========== set hi_{rx|tx}{on|off} ========== */
1383	/* XXX: the card TOTALLY shields us from the flow control... */
1384	ccbp->hi_txon = t->c_cc[VSTART];
1385	ccbp->hi_txoff = t->c_cc[VSTOP];
1386
1387	ccbp->hi_rxon = t->c_cc[VSTART];
1388	ccbp->hi_rxoff = t->c_cc[VSTOP];
1389
1390	/* ========== send settings to the card ========== */
1391	/* potential sleep here */
1392	if (ccbp->hi_stat == IDLE_CLOSE)		/* Not yet open */
1393		si_command(pp, LOPEN, SI_WAIT);		/* open it */
1394	else
1395		si_command(pp, CONFIG, SI_WAIT);	/* change params */
1396
1397	/* ========== set DTR etc ========== */
1398	/* Hangup if ospeed == 0 */
1399	if (t->c_ospeed == 0) {
1400		(void) si_modem(pp, BIC, TIOCM_DTR|TIOCM_RTS);
1401	} else {
1402		/*
1403		 * If the previous speed was 0, may need to re-enable
1404		 * the modem signals
1405		 */
1406		(void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1407	}
1408
1409	DPRINT((pp, DBG_PARAM, "siparam, complete: MR1 %x MR2 %x HI_MASK %x PRTCL %x HI_BREAK %x\n",
1410		ccbp->hi_mr1, ccbp->hi_mr2, ccbp->hi_mask, ccbp->hi_prtcl, ccbp->hi_break));
1411
1412	splx(oldspl);
1413	return(error);
1414}
1415
1416/*
1417 * Enable or Disable the writes to this channel...
1418 * "state" ->  enabled = 1; disabled = 0;
1419 */
1420static void
1421si_write_enable(struct si_port *pp, int state)
1422{
1423	int oldspl;
1424
1425	oldspl = spltty();
1426
1427	if (state) {
1428		pp->sp_state &= ~SS_BLOCKWRITE;
1429		if (pp->sp_state & SS_WAITWRITE) {
1430			pp->sp_state &= ~SS_WAITWRITE;
1431			/* thunder away! */
1432			wakeup((caddr_t)pp);
1433		}
1434	} else {
1435		pp->sp_state |= SS_BLOCKWRITE;
1436	}
1437
1438	splx(oldspl);
1439}
1440
1441/*
1442 * Set/Get state of modem control lines.
1443 * Due to DCE-like behaviour of the adapter, some signals need translation:
1444 *	TIOCM_DTR	DSR
1445 *	TIOCM_RTS	CTS
1446 */
1447static int
1448si_modem(struct si_port *pp, enum si_mctl cmd, int bits)
1449{
1450	volatile struct si_channel *ccbp;
1451	int x;
1452
1453	DPRINT((pp, DBG_ENTRY|DBG_MODEM, "si_modem(%x,%s,%x)\n", pp, si_mctl2str(cmd), bits));
1454	ccbp = pp->sp_ccb;		/* Find channel address */
1455	switch (cmd) {
1456	case GET:
1457		x = ccbp->hi_ip;
1458		bits = TIOCM_LE;
1459		if (x & IP_DCD)		bits |= TIOCM_CAR;
1460		if (x & IP_DTR)		bits |= TIOCM_DTR;
1461		if (x & IP_RTS)		bits |= TIOCM_RTS;
1462		if (x & IP_RI)		bits |= TIOCM_RI;
1463		return(bits);
1464	case SET:
1465		ccbp->hi_op &= ~(OP_DSR|OP_CTS);
1466		/* fall through */
1467	case BIS:
1468		x = 0;
1469		if (bits & TIOCM_DTR)
1470			x |= OP_DSR;
1471		if (bits & TIOCM_RTS)
1472			x |= OP_CTS;
1473		ccbp->hi_op |= x;
1474		break;
1475	case BIC:
1476		if (bits & TIOCM_DTR)
1477			ccbp->hi_op &= ~OP_DSR;
1478		if (bits & TIOCM_RTS)
1479			ccbp->hi_op &= ~OP_CTS;
1480	}
1481	return 0;
1482}
1483
1484/*
1485 * Handle change of modem state
1486 */
1487static void
1488si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip)
1489{
1490							/* if a modem dev */
1491	if (hi_ip & IP_DCD) {
1492		if (!(pp->sp_last_hi_ip & IP_DCD)) {
1493			DPRINT((pp, DBG_INTR, "modem carr on t_line %d\n",
1494				tp->t_line));
1495			(void)(*linesw[tp->t_line].l_modem)(tp, 1);
1496		}
1497	} else {
1498		if (pp->sp_last_hi_ip & IP_DCD) {
1499			DPRINT((pp, DBG_INTR, "modem carr off\n"));
1500			if ((*linesw[tp->t_line].l_modem)(tp, 0))
1501				(void) si_modem(pp, SET, 0);
1502		}
1503	}
1504	pp->sp_last_hi_ip = hi_ip;
1505
1506}
1507
1508/*
1509 * Poller to catch missed interrupts.
1510 *
1511 * Note that the SYSV Specialix drivers poll at 100 times per second to get
1512 * better response.  We could really use a "periodic" version timeout(). :-)
1513 */
1514#ifdef POLL
1515static void
1516si_poll(void *nothing)
1517{
1518	struct si_softc *sc;
1519	int i;
1520	volatile struct si_reg *regp;
1521	struct si_port *pp;
1522	int lost, oldspl, port;
1523
1524	DPRINT((0, DBG_POLL, "si_poll()\n"));
1525	oldspl = spltty();
1526	if (in_intr)
1527		goto out;
1528	lost = 0;
1529	for (i = 0; i < si_numunits; i++) {
1530		sc = devclass_get_softc(si_devclass, i);
1531		if (sc == NULL || sc->sc_type == SIEMPTY)
1532			continue;
1533		regp = (struct si_reg *)sc->sc_maddr;
1534
1535		/*
1536		 * See if there has been a pending interrupt for 2 seconds
1537		 * or so. The test (int_scounter >= 200) won't correspond
1538		 * to 2 seconds if int_count gets changed.
1539		 */
1540		if (regp->int_pending != 0) {
1541			if (regp->int_scounter >= 200 &&
1542			    regp->initstat == 1) {
1543				printf("si%d: lost intr\n", i);
1544				lost++;
1545			}
1546		} else {
1547			regp->int_scounter = 0;
1548		}
1549
1550		/*
1551		 * gripe about no input flow control..
1552		 */
1553		pp = sc->sc_ports;
1554		for (port = 0; port < sc->sc_nport; pp++, port++) {
1555			if (pp->sp_delta_overflows > 0) {
1556				printf("si%d: %d tty level buffer overflows\n",
1557					i, pp->sp_delta_overflows);
1558				pp->sp_delta_overflows = 0;
1559			}
1560		}
1561	}
1562	if (lost || si_realpoll)
1563		si_intr(NULL);	/* call intr with fake vector */
1564out:
1565	splx(oldspl);
1566
1567	timeout(si_poll, (caddr_t)0L, si_pollrate);
1568}
1569#endif	/* ifdef POLL */
1570
1571/*
1572 * The interrupt handler polls ALL ports on ALL adapters each time
1573 * it is called.
1574 */
1575
1576static BYTE si_rxbuf[SI_BUFFERSIZE];	/* input staging area */
1577static BYTE si_txbuf[SI_BUFFERSIZE];	/* output staging area */
1578
1579void
1580si_intr(void *arg)
1581{
1582	struct si_softc *sc;
1583	struct si_port *pp;
1584	volatile struct si_channel *ccbp;
1585	struct tty *tp;
1586	volatile caddr_t maddr;
1587	BYTE op, ip;
1588	int x, card, port, n, i, isopen;
1589	volatile BYTE *z;
1590	BYTE c;
1591
1592	sc = arg;
1593
1594	DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "si_intr\n"));
1595	if (in_intr)
1596		return;
1597	in_intr = 1;
1598
1599	/*
1600	 * When we get an int we poll all the channels and do ALL pending
1601	 * work, not just the first one we find. This allows all cards to
1602	 * share the same vector.
1603	 *
1604	 * XXX - But if we're sharing the vector with something that's NOT
1605	 * a SI/XIO/SX card, we may be making more work for ourselves.
1606	 */
1607	for (card = 0; card < si_numunits; card++) {
1608		sc = devclass_get_softc(si_devclass, card);
1609		if (sc == NULL || sc->sc_type == SIEMPTY)
1610			continue;
1611
1612		/*
1613		 * First, clear the interrupt
1614		 */
1615		switch(sc->sc_type) {
1616		case SIHOST:
1617			maddr = sc->sc_maddr;
1618			((volatile struct si_reg *)maddr)->int_pending = 0;
1619							/* flag nothing pending */
1620			*(maddr+SIINTCL) = 0x00;	/* Set IRQ clear */
1621			*(maddr+SIINTCL_CL) = 0x00;	/* Clear IRQ clear */
1622			break;
1623		case SIHOST2:
1624			maddr = sc->sc_maddr;
1625			((volatile struct si_reg *)maddr)->int_pending = 0;
1626			*(maddr+SIPLIRQCLR) = 0x00;
1627			*(maddr+SIPLIRQCLR) = 0x10;
1628			break;
1629		case SIPCI:
1630			maddr = sc->sc_maddr;
1631			((volatile struct si_reg *)maddr)->int_pending = 0;
1632			*(maddr+SIPCIINTCL) = 0x0;
1633			break;
1634		case SIJETPCI:	/* fall through to JETISA case */
1635		case SIJETISA:
1636			maddr = sc->sc_maddr;
1637			((volatile struct si_reg *)maddr)->int_pending = 0;
1638			*(maddr+SIJETINTCL) = 0x0;
1639			break;
1640		case SIEISA:
1641			maddr = sc->sc_maddr;
1642			((volatile struct si_reg *)maddr)->int_pending = 0;
1643			(void)inb(sc->sc_iobase + 3);
1644			break;
1645		case SIEMPTY:
1646		default:
1647			continue;
1648		}
1649		((volatile struct si_reg *)maddr)->int_scounter = 0;
1650
1651		/*
1652		 * check each port
1653		 */
1654		for (pp = sc->sc_ports, port = 0; port < sc->sc_nport;
1655		     pp++, port++) {
1656			ccbp = pp->sp_ccb;
1657			tp = pp->sp_tty;
1658
1659			/*
1660			 * See if a command has completed ?
1661			 */
1662			if (ccbp->hi_stat != pp->sp_pend) {
1663				DPRINT((pp, DBG_INTR,
1664					"si_intr hi_stat = 0x%x, pend = %d\n",
1665					ccbp->hi_stat, pp->sp_pend));
1666				switch(pp->sp_pend) {
1667				case LOPEN:
1668				case MPEND:
1669				case MOPEN:
1670				case CONFIG:
1671				case SBREAK:
1672				case EBREAK:
1673					pp->sp_pend = ccbp->hi_stat;
1674						/* sleeping in si_command */
1675					wakeup(&pp->sp_state);
1676					break;
1677				default:
1678					pp->sp_pend = ccbp->hi_stat;
1679				}
1680			}
1681
1682			/*
1683			 * Continue on if it's closed
1684			 */
1685			if (ccbp->hi_stat == IDLE_CLOSE) {
1686				continue;
1687			}
1688
1689			/*
1690			 * Do modem state change if not a local device
1691			 */
1692			si_modem_state(pp, tp, ccbp->hi_ip);
1693
1694			/*
1695			 * Check to see if we should 'receive' characters.
1696			 */
1697			if (tp->t_state & TS_CONNECTED &&
1698			    tp->t_state & TS_ISOPEN)
1699				isopen = 1;
1700			else
1701				isopen = 0;
1702
1703			/*
1704			 * Do input break processing
1705			 */
1706			if (ccbp->hi_state & ST_BREAK) {
1707				if (isopen) {
1708				    (*linesw[tp->t_line].l_rint)(TTY_BI, tp);
1709				}
1710				ccbp->hi_state &= ~ST_BREAK;   /* A Bit iffy this */
1711				DPRINT((pp, DBG_INTR, "si_intr break\n"));
1712			}
1713
1714			/*
1715			 * Do RX stuff - if not open then dump any characters.
1716			 * XXX: This is VERY messy and needs to be cleaned up.
1717			 *
1718			 * XXX: can we leave data in the host adapter buffer
1719			 * when the clists are full?  That may be dangerous
1720			 * if the user cannot get an interrupt signal through.
1721			 */
1722
1723	more_rx:	/* XXX Sorry. the nesting was driving me bats! :-( */
1724
1725			if (!isopen) {
1726				ccbp->hi_rxopos = ccbp->hi_rxipos;
1727				goto end_rx;
1728			}
1729
1730			/*
1731			 * If the tty input buffers are blocked, stop emptying
1732			 * the incoming buffers and let the auto flow control
1733			 * assert..
1734			 */
1735			if (tp->t_state & TS_TBLOCK) {
1736				goto end_rx;
1737			}
1738
1739			/*
1740			 * Process read characters if not skipped above
1741			 */
1742			op = ccbp->hi_rxopos;
1743			ip = ccbp->hi_rxipos;
1744			c = ip - op;
1745			if (c == 0) {
1746				goto end_rx;
1747			}
1748
1749			n = c & 0xff;
1750			if (n > 250)
1751				n = 250;
1752
1753			DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1754						n, op, ip));
1755
1756			/*
1757			 * Suck characters out of host card buffer into the
1758			 * "input staging buffer" - so that we dont leave the
1759			 * host card in limbo while we're possibly echoing
1760			 * characters and possibly flushing input inside the
1761			 * ldisc l_rint() routine.
1762			 */
1763			if (n <= SI_BUFFERSIZE - op) {
1764
1765				DPRINT((pp, DBG_INTR, "\tsingle copy\n"));
1766				z = ccbp->hi_rxbuf + op;
1767				si_vbcopy(z, si_rxbuf, n);
1768
1769				op += n;
1770			} else {
1771				x = SI_BUFFERSIZE - op;
1772
1773				DPRINT((pp, DBG_INTR, "\tdouble part 1 %d\n", x));
1774				z = ccbp->hi_rxbuf + op;
1775				si_vbcopy(z, si_rxbuf, x);
1776
1777				DPRINT((pp, DBG_INTR, "\tdouble part 2 %d\n",
1778					n - x));
1779				z = ccbp->hi_rxbuf;
1780				si_vbcopy(z, si_rxbuf + x, n - x);
1781
1782				op += n;
1783			}
1784
1785			/* clear collected characters from buffer */
1786			ccbp->hi_rxopos = op;
1787
1788			DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1789						n, op, ip));
1790
1791			/*
1792			 * at this point...
1793			 * n = number of chars placed in si_rxbuf
1794			 */
1795
1796			/*
1797			 * Avoid the grotesquely inefficient lineswitch
1798			 * routine (ttyinput) in "raw" mode. It usually
1799			 * takes about 450 instructions (that's without
1800			 * canonical processing or echo!). slinput is
1801			 * reasonably fast (usually 40 instructions
1802			 * plus call overhead).
1803			 */
1804			if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1805
1806				/* block if the driver supports it */
1807				if (tp->t_rawq.c_cc + n >= SI_I_HIGH_WATER &&
1808				    (tp->t_cflag & CRTS_IFLOW ||
1809				     tp->t_iflag & IXOFF) &&
1810				    !(tp->t_state & TS_TBLOCK))
1811					ttyblock(tp);
1812
1813				tk_nin += n;
1814				tk_rawcc += n;
1815				tp->t_rawcc += n;
1816
1817				pp->sp_delta_overflows +=
1818				    b_to_q((char *)si_rxbuf, n, &tp->t_rawq);
1819
1820				ttwakeup(tp);
1821				if (tp->t_state & TS_TTSTOP &&
1822				    (tp->t_iflag & IXANY ||
1823				     tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1824					tp->t_state &= ~TS_TTSTOP;
1825					tp->t_lflag &= ~FLUSHO;
1826					si_start(tp);
1827				}
1828			} else {
1829				/*
1830				 * It'd be nice to not have to go through the
1831				 * function call overhead for each char here.
1832				 * It'd be nice to block input it, saving a
1833				 * loop here and the call/return overhead.
1834				 */
1835				for(x = 0; x < n; x++) {
1836					i = si_rxbuf[x];
1837					if ((*linesw[tp->t_line].l_rint)(i, tp)
1838					     == -1) {
1839						pp->sp_delta_overflows++;
1840					}
1841				}
1842			}
1843			goto more_rx;	/* try for more until RXbuf is empty */
1844
1845	end_rx:		/* XXX: Again, sorry about the gotos.. :-) */
1846
1847			/*
1848			 * Do TX stuff
1849			 */
1850			(*linesw[tp->t_line].l_start)(tp);
1851
1852		} /* end of for (all ports on this controller) */
1853	} /* end of for (all controllers) */
1854
1855	in_intr = 0;
1856	DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "end si_intr\n"));
1857}
1858
1859/*
1860 * Nudge the transmitter...
1861 *
1862 * XXX: I inherited some funny code here.  It implies the host card only
1863 * interrupts when the transmit buffer reaches the low-water-mark, and does
1864 * not interrupt when it's actually hits empty.  In some cases, we have
1865 * processes waiting for complete drain, and we need to simulate an interrupt
1866 * about when we think the buffer is going to be empty (and retry if not).
1867 * I really am not certain about this...  I *need* the hardware manuals.
1868 */
1869static void
1870si_start(struct tty *tp)
1871{
1872	struct si_port *pp;
1873	volatile struct si_channel *ccbp;
1874	struct clist *qp;
1875	BYTE ipos;
1876	int nchar;
1877	int oldspl, count, n, amount, buffer_full;
1878
1879	oldspl = spltty();
1880
1881	qp = &tp->t_outq;
1882	pp = TP2PP(tp);
1883
1884	DPRINT((pp, DBG_ENTRY|DBG_START,
1885		"si_start(%x) t_state %x sp_state %x t_outq.c_cc %d\n",
1886		tp, tp->t_state, pp->sp_state, qp->c_cc));
1887
1888	if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
1889		goto out;
1890
1891	buffer_full = 0;
1892	ccbp = pp->sp_ccb;
1893
1894	count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1895	DPRINT((pp, DBG_START, "count %d\n", (BYTE)count));
1896
1897	while ((nchar = qp->c_cc) > 0) {
1898		if ((BYTE)count >= 255) {
1899			buffer_full++;
1900			break;
1901		}
1902		amount = min(nchar, (255 - (BYTE)count));
1903		ipos = (unsigned int)ccbp->hi_txipos;
1904		n = q_to_b(&tp->t_outq, si_txbuf, amount);
1905		/* will it fit in one lump? */
1906		if ((SI_BUFFERSIZE - ipos) >= n) {
1907			si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos], n);
1908		} else {
1909			si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos],
1910				SI_BUFFERSIZE - ipos);
1911			si_bcopyv(si_txbuf + (SI_BUFFERSIZE - ipos),
1912				&ccbp->hi_txbuf[0], n - (SI_BUFFERSIZE - ipos));
1913		}
1914		ccbp->hi_txipos += n;
1915		count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1916	}
1917
1918	if (count != 0 && nchar == 0) {
1919		tp->t_state |= TS_BUSY;
1920	} else {
1921		tp->t_state &= ~TS_BUSY;
1922	}
1923
1924	/* wakeup time? */
1925	ttwwakeup(tp);
1926
1927	DPRINT((pp, DBG_START, "count %d, nchar %d, tp->t_state 0x%x\n",
1928		(BYTE)count, nchar, tp->t_state));
1929
1930	if (tp->t_state & TS_BUSY)
1931	{
1932		int time;
1933
1934		time = ttspeedtab(tp->t_ospeed, chartimes);
1935
1936		if (time > 0) {
1937			if (time < nchar)
1938				time = nchar / time;
1939			else
1940				time = 2;
1941		} else {
1942			DPRINT((pp, DBG_START,
1943				"bad char time value! %d\n", time));
1944			time = hz/10;
1945		}
1946
1947		if ((pp->sp_state & (SS_LSTART|SS_INLSTART)) == SS_LSTART) {
1948			untimeout(si_lstart, (caddr_t)pp, pp->lstart_ch);
1949		} else {
1950			pp->sp_state |= SS_LSTART;
1951		}
1952		DPRINT((pp, DBG_START, "arming lstart, time=%d\n", time));
1953		pp->lstart_ch = timeout(si_lstart, (caddr_t)pp, time);
1954	}
1955
1956out:
1957	splx(oldspl);
1958	DPRINT((pp, DBG_EXIT|DBG_START, "leave si_start()\n"));
1959}
1960
1961/*
1962 * Note: called at splsoftclock from the timeout code
1963 * This has to deal with two things...  cause wakeups while waiting for
1964 * tty drains on last process exit, and call l_start at about the right
1965 * time for protocols like ppp.
1966 */
1967static void
1968si_lstart(void *arg)
1969{
1970	struct si_port *pp = arg;
1971	struct tty *tp;
1972	int oldspl;
1973
1974	DPRINT((pp, DBG_ENTRY|DBG_LSTART, "si_lstart(%x) sp_state %x\n",
1975		pp, pp->sp_state));
1976
1977	oldspl = spltty();
1978
1979	if ((pp->sp_state & SS_OPEN) == 0 || (pp->sp_state & SS_LSTART) == 0) {
1980		splx(oldspl);
1981		return;
1982	}
1983	pp->sp_state &= ~SS_LSTART;
1984	pp->sp_state |= SS_INLSTART;
1985
1986	tp = pp->sp_tty;
1987
1988	/* deal with the process exit case */
1989	ttwwakeup(tp);
1990
1991	/* nudge protocols - eg: ppp */
1992	(*linesw[tp->t_line].l_start)(tp);
1993
1994	pp->sp_state &= ~SS_INLSTART;
1995	splx(oldspl);
1996}
1997
1998/*
1999 * Stop output on a line. called at spltty();
2000 */
2001void
2002si_stop(struct tty *tp, int rw)
2003{
2004	volatile struct si_channel *ccbp;
2005	struct si_port *pp;
2006
2007	pp = TP2PP(tp);
2008	ccbp = pp->sp_ccb;
2009
2010	DPRINT((TP2PP(tp), DBG_ENTRY|DBG_STOP, "si_stop(%x,%x)\n", tp, rw));
2011
2012	/* XXX: must check (rw & FWRITE | FREAD) etc flushing... */
2013	if (rw & FWRITE) {
2014		/* what level are we meant to be flushing anyway? */
2015		if (tp->t_state & TS_BUSY) {
2016			si_command(TP2PP(tp), WFLUSH, SI_NOWAIT);
2017			tp->t_state &= ~TS_BUSY;
2018			ttwwakeup(tp);	/* Bruce???? */
2019		}
2020	}
2021#if 1	/* XXX: this doesn't work right yet.. */
2022	/* XXX: this may have been failing because we used to call l_rint()
2023	 * while we were looping based on these two counters. Now, we collect
2024	 * the data and then loop stuffing it into l_rint(), making this
2025	 * useless.  Should we cause this to blow away the staging buffer?
2026	 */
2027	if (rw & FREAD) {
2028		ccbp->hi_rxopos = ccbp->hi_rxipos;
2029	}
2030#endif
2031}
2032
2033/*
2034 * Issue a command to the host card CPU.
2035 */
2036
2037static void
2038si_command(struct si_port *pp, int cmd, int waitflag)
2039{
2040	int oldspl;
2041	volatile struct si_channel *ccbp = pp->sp_ccb;
2042	int x;
2043
2044	DPRINT((pp, DBG_ENTRY|DBG_PARAM, "si_command(%x,%x,%d): hi_stat 0x%x\n",
2045		pp, cmd, waitflag, ccbp->hi_stat));
2046
2047	oldspl = spltty();		/* Keep others out */
2048
2049	/* wait until it's finished what it was doing.. */
2050	/* XXX: sits in IDLE_BREAK until something disturbs it or break
2051	 * is turned off. */
2052	while((x = ccbp->hi_stat) != IDLE_OPEN &&
2053			x != IDLE_CLOSE &&
2054			x != IDLE_BREAK &&
2055			x != cmd) {
2056		if (in_intr) {			/* Prevent sleep in intr */
2057			DPRINT((pp, DBG_PARAM,
2058				"cmd intr collision - completing %d\trequested %d\n",
2059				x, cmd));
2060			splx(oldspl);
2061			return;
2062		} else if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, TTIPRI|PCATCH,
2063				"sicmd1", 1)) {
2064			splx(oldspl);
2065			return;
2066		}
2067	}
2068	/* it should now be in IDLE_{OPEN|CLOSE|BREAK}, or "cmd" */
2069
2070	/* if there was a pending command, cause a state-change wakeup */
2071	switch(pp->sp_pend) {
2072	case LOPEN:
2073	case MPEND:
2074	case MOPEN:
2075	case CONFIG:
2076	case SBREAK:
2077	case EBREAK:
2078		wakeup(&pp->sp_state);
2079		break;
2080	default:
2081		break;
2082	}
2083
2084	pp->sp_pend = cmd;		/* New command pending */
2085	ccbp->hi_stat = cmd;		/* Post it */
2086
2087	if (waitflag) {
2088		if (in_intr) {		/* If in interrupt handler */
2089			DPRINT((pp, DBG_PARAM,
2090				"attempt to sleep in si_intr - cmd req %d\n",
2091				cmd));
2092			splx(oldspl);
2093			return;
2094		} else while(ccbp->hi_stat != IDLE_OPEN &&
2095			     ccbp->hi_stat != IDLE_BREAK) {
2096			if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, TTIPRI|PCATCH,
2097			    "sicmd2", 0))
2098				break;
2099		}
2100	}
2101	splx(oldspl);
2102}
2103
2104static void
2105si_disc_optim(struct tty *tp, struct termios *t, struct si_port *pp)
2106{
2107	/*
2108	 * XXX can skip a lot more cases if Smarts.  Maybe
2109	 * (IGNCR | ISTRIP | IXON) in c_iflag.  But perhaps we
2110	 * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2111	 */
2112	if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) &&
2113	    (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) &&
2114	    (!(t->c_iflag & PARMRK) ||
2115	     (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) &&
2116	    !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) &&
2117	    linesw[tp->t_line].l_rint == ttyinput)
2118		tp->t_state |= TS_CAN_BYPASS_L_RINT;
2119	else
2120		tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2121	pp->sp_hotchar = linesw[tp->t_line].l_hotchar;
2122	DPRINT((pp, DBG_OPTIM, "bypass: %s, hotchar: %x\n",
2123		(tp->t_state & TS_CAN_BYPASS_L_RINT) ? "on" : "off",
2124		pp->sp_hotchar));
2125}
2126
2127
2128#ifdef	SI_DEBUG
2129
2130void
2131si_dprintf(struct si_port *pp, int flags, const char *fmt, ...)
2132{
2133	va_list ap;
2134
2135	if ((pp == NULL && (si_debug&flags)) ||
2136	    (pp != NULL && ((pp->sp_debug&flags) || (si_debug&flags)))) {
2137		if (pp != NULL)
2138			printf("%ci%d(%d): ", 's',
2139				(int)SI_CARD(minor(pp->sp_tty->t_dev)),
2140				(int)SI_PORT(minor(pp->sp_tty->t_dev)));
2141		va_start(ap, fmt);
2142		vprintf(fmt, ap);
2143		va_end(ap);
2144	}
2145}
2146
2147static char *
2148si_mctl2str(enum si_mctl cmd)
2149{
2150	switch (cmd) {
2151	case GET:
2152		return("GET");
2153	case SET:
2154		return("SET");
2155	case BIS:
2156		return("BIS");
2157	case BIC:
2158		return("BIC");
2159	}
2160	return("BAD");
2161}
2162
2163#endif	/* DEBUG */
2164
2165static char *
2166si_modulename(int host_type, int uart_type)
2167{
2168	switch (host_type) {
2169	/* Z280 based cards */
2170	case SIEISA:
2171	case SIHOST2:
2172	case SIHOST:
2173	case SIPCI:
2174		switch (uart_type) {
2175		case 0:
2176			return(" (XIO)");
2177		case 1:
2178			return(" (SI)");
2179		}
2180		break;
2181	/* T225 based hosts */
2182	case SIJETPCI:
2183	case SIJETISA:
2184		switch (uart_type) {
2185		case 0:
2186			return(" (SI)");
2187		case 40:
2188			return(" (XIO)");
2189		case 72:
2190			return(" (SXDC)");
2191		}
2192		break;
2193	}
2194	return("");
2195}
2196