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