si.c revision 130057
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 130057 2004-06-04 08:02:37Z 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 = ttyioctl(dev, cmd, data, flag, td);
1020	si_disc_optim(tp, &tp->t_termios, pp);
1021	if (error != ENOTTY)
1022		goto out;
1023
1024	oldspl = spltty();
1025
1026	error = 0;
1027	switch (cmd) {
1028	case TIOCSBRK:
1029		si_command(pp, SBREAK, SI_WAIT);
1030		break;
1031	case TIOCCBRK:
1032		si_command(pp, EBREAK, SI_WAIT);
1033		break;
1034	case TIOCSDTR:
1035		(void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1036		break;
1037	case TIOCCDTR:
1038		(void) si_modem(pp, SET, 0);
1039		break;
1040	case TIOCMSET:
1041		(void) si_modem(pp, SET, *(int *)data);
1042		break;
1043	case TIOCMBIS:
1044		(void) si_modem(pp, BIS, *(int *)data);
1045		break;
1046	case TIOCMBIC:
1047		(void) si_modem(pp, BIC, *(int *)data);
1048		break;
1049	case TIOCMGET:
1050		*(int *)data = si_modem(pp, GET, 0);
1051		break;
1052	case TIOCMSDTRWAIT:
1053		/* must be root since the wait applies to following logins */
1054		error = suser(td);
1055		if (error == 0)
1056			pp->sp_dtr_wait = *(int *)data * hz / 100;
1057		break;
1058	case TIOCMGDTRWAIT:
1059		*(int *)data = pp->sp_dtr_wait * 100 / hz;
1060		break;
1061	default:
1062		error = ENOTTY;
1063	}
1064	splx(oldspl);
1065
1066out:
1067	DPRINT((pp, DBG_IOCTL|DBG_EXIT, "siioctl ret %d\n", error));
1068	if (blocked)
1069		si_write_enable(pp, 1);
1070	return(error);
1071}
1072
1073/*
1074 * Handle the Specialix ioctls. All MUST be called via the CONTROL device
1075 */
1076static int
1077si_Sioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1078{
1079	struct si_softc *xsc;
1080	struct si_port *xpp;
1081	volatile struct si_reg *regp;
1082	struct si_tcsi *dp;
1083	struct si_pstat *sps;
1084	int *ip, error = 0;
1085	int oldspl;
1086	int card, port;
1087	int mynor = minor(dev);
1088
1089	DPRINT((0, DBG_ENTRY|DBG_IOCTL, "si_Sioctl(%s,%lx,%x,%x)\n",
1090		devtoname(dev), cmd, data, flag));
1091
1092#if 1
1093	DPRINT((0, DBG_IOCTL, "TCSI_PORT=%x\n", TCSI_PORT));
1094	DPRINT((0, DBG_IOCTL, "TCSI_CCB=%x\n", TCSI_CCB));
1095	DPRINT((0, DBG_IOCTL, "TCSI_TTY=%x\n", TCSI_TTY));
1096#endif
1097
1098	if (!IS_CONTROLDEV(mynor)) {
1099		DPRINT((0, DBG_IOCTL|DBG_FAIL, "not called from control device!\n"));
1100		return(ENODEV);
1101	}
1102
1103	oldspl = spltty();	/* better safe than sorry */
1104
1105	ip = (int *)data;
1106
1107#define SUCHECK if ((error = suser(td))) goto out
1108
1109	switch (cmd) {
1110	case TCSIPORTS:
1111		*ip = si_Nports;
1112		goto out;
1113	case TCSIMODULES:
1114		*ip = si_Nmodules;
1115		goto out;
1116	case TCSISDBG_ALL:
1117		SUCHECK;
1118		si_debug = *ip;
1119		goto out;
1120	case TCSIGDBG_ALL:
1121		*ip = si_debug;
1122		goto out;
1123	default:
1124		/*
1125		 * Check that a controller for this port exists
1126		 */
1127
1128		/* may also be a struct si_pstat, a superset of si_tcsi */
1129
1130		dp = (struct si_tcsi *)data;
1131		sps = (struct si_pstat *)data;
1132		card = dp->tc_card;
1133		xsc = devclass_get_softc(si_devclass, card);	/* check.. */
1134		if (xsc == NULL || xsc->sc_type == SIEMPTY) {
1135			error = ENOENT;
1136			goto out;
1137		}
1138		/*
1139		 * And check that a port exists
1140		 */
1141		port = dp->tc_port;
1142		if (port < 0 || port >= xsc->sc_nport) {
1143			error = ENOENT;
1144			goto out;
1145		}
1146		xpp = xsc->sc_ports + port;
1147		regp = (struct si_reg *)xsc->sc_maddr;
1148	}
1149
1150	switch (cmd) {
1151	case TCSIDEBUG:
1152#ifdef	SI_DEBUG
1153		SUCHECK;
1154		if (xpp->sp_debug)
1155			xpp->sp_debug = 0;
1156		else {
1157			xpp->sp_debug = DBG_ALL;
1158			DPRINT((xpp, DBG_IOCTL, "debug toggled %s\n",
1159				(xpp->sp_debug&DBG_ALL)?"ON":"OFF"));
1160		}
1161		break;
1162#else
1163		error = ENODEV;
1164		goto out;
1165#endif
1166	case TCSISDBG_LEVEL:
1167	case TCSIGDBG_LEVEL:
1168#ifdef	SI_DEBUG
1169		if (cmd == TCSIGDBG_LEVEL) {
1170			dp->tc_dbglvl = xpp->sp_debug;
1171		} else {
1172			SUCHECK;
1173			xpp->sp_debug = dp->tc_dbglvl;
1174		}
1175		break;
1176#else
1177		error = ENODEV;
1178		goto out;
1179#endif
1180	case TCSIGRXIT:
1181		dp->tc_int = regp->rx_int_count;
1182		break;
1183	case TCSIRXIT:
1184		SUCHECK;
1185		regp->rx_int_count = dp->tc_int;
1186		break;
1187	case TCSIGIT:
1188		dp->tc_int = regp->int_count;
1189		break;
1190	case TCSIIT:
1191		SUCHECK;
1192		regp->int_count = dp->tc_int;
1193		break;
1194	case TCSISTATE:
1195		dp->tc_int = xpp->sp_ccb->hi_ip;
1196		break;
1197	/* these next three use a different structure */
1198	case TCSI_PORT:
1199		SUCHECK;
1200		si_bcopy(xpp, &sps->tc_siport, sizeof(sps->tc_siport));
1201		break;
1202	case TCSI_CCB:
1203		SUCHECK;
1204		si_vbcopy(xpp->sp_ccb, &sps->tc_ccb, sizeof(sps->tc_ccb));
1205		break;
1206	case TCSI_TTY:
1207		SUCHECK;
1208		si_bcopy(xpp->sp_tty, &sps->tc_tty, sizeof(sps->tc_tty));
1209		break;
1210	default:
1211		error = EINVAL;
1212		goto out;
1213	}
1214out:
1215	splx(oldspl);
1216	return(error);		/* success */
1217}
1218
1219/*
1220 *	siparam()	: Configure line params
1221 *	called at spltty();
1222 *	this may sleep, does not flush, nor wait for drain, nor block writes
1223 *	caller must arrange this if it's important..
1224 */
1225static int
1226siparam(struct tty *tp, struct termios *t)
1227{
1228	struct si_port *pp = TP2PP(tp);
1229	volatile struct si_channel *ccbp;
1230	int oldspl, cflag, iflag, oflag, lflag;
1231	int error = 0;		/* shutup gcc */
1232	int ispeed = 0;		/* shutup gcc */
1233	int ospeed = 0;		/* shutup gcc */
1234	BYTE val;
1235
1236	DPRINT((pp, DBG_ENTRY|DBG_PARAM, "siparam(%x,%x)\n", tp, t));
1237	cflag = t->c_cflag;
1238	iflag = t->c_iflag;
1239	oflag = t->c_oflag;
1240	lflag = t->c_lflag;
1241	DPRINT((pp, DBG_PARAM, "OFLAG 0x%x CFLAG 0x%x IFLAG 0x%x LFLAG 0x%x\n",
1242		oflag, cflag, iflag, lflag));
1243
1244	/* XXX - if Jet host and SXDC module, use extended baud rates */
1245
1246	/* if not hung up.. */
1247	if (t->c_ospeed != 0) {
1248		/* translate baud rate to firmware values */
1249		ospeed = ttspeedtab(t->c_ospeed, bdrates);
1250		ispeed = t->c_ispeed ?
1251			 ttspeedtab(t->c_ispeed, bdrates) : ospeed;
1252
1253		/* enforce legit baud rate */
1254		if (ospeed < 0 || ispeed < 0)
1255			return (EINVAL);
1256	}
1257
1258	oldspl = spltty();
1259
1260	ccbp = pp->sp_ccb;
1261
1262	/* ========== set hi_break ========== */
1263	val = 0;
1264	if (iflag & IGNBRK)		/* Breaks */
1265		val |= BR_IGN;
1266	if (iflag & BRKINT)		/* Interrupt on break? */
1267		val |= BR_INT;
1268	if (iflag & PARMRK)		/* Parity mark? */
1269		val |= BR_PARMRK;
1270	if (iflag & IGNPAR)		/* Ignore chars with parity errors? */
1271		val |= BR_PARIGN;
1272	ccbp->hi_break = val;
1273
1274	/* ========== set hi_csr ========== */
1275	/* if not hung up.. */
1276	if (t->c_ospeed != 0) {
1277		/* Set I/O speeds */
1278		 val = (ispeed << 4) | ospeed;
1279	}
1280	ccbp->hi_csr = val;
1281
1282	/* ========== set hi_mr2 ========== */
1283	val = 0;
1284	if (cflag & CSTOPB)				/* Stop bits */
1285		val |= MR2_2_STOP;
1286	else
1287		val |= MR2_1_STOP;
1288	/*
1289	 * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1290	 * a DCE, hence the reverse sense of RTS and CTS
1291	 */
1292	/* Output Flow - RTS must be raised before data can be sent */
1293	if (cflag & CCTS_OFLOW)
1294		val |= MR2_RTSCONT;
1295
1296	ccbp->hi_mr2 = val;
1297
1298	/* ========== set hi_mr1 ========== */
1299	val = 0;
1300	if (!(cflag & PARENB))				/* Parity */
1301		val |= MR1_NONE;
1302	else
1303		val |= MR1_WITH;
1304	if (cflag & PARODD)
1305		val |= MR1_ODD;
1306
1307	if ((cflag & CS8) == CS8) {			/* 8 data bits? */
1308		val |= MR1_8_BITS;
1309	} else if ((cflag & CS7) == CS7) {		/* 7 data bits? */
1310		val |= MR1_7_BITS;
1311	} else if ((cflag & CS6) == CS6) {		/* 6 data bits? */
1312		val |= MR1_6_BITS;
1313	} else {					/* Must be 5 */
1314		val |= MR1_5_BITS;
1315	}
1316	/*
1317	 * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1318	 * a DCE, hence the reverse sense of RTS and CTS
1319	 */
1320	/* Input Flow - CTS is raised when port is ready to receive data */
1321	if (cflag & CRTS_IFLOW)
1322		val |= MR1_CTSCONT;
1323
1324	ccbp->hi_mr1 = val;
1325
1326	/* ========== set hi_mask ========== */
1327	val = 0xff;
1328	if ((cflag & CS8) == CS8) {			/* 8 data bits? */
1329		val &= 0xFF;
1330	} else if ((cflag & CS7) == CS7) {		/* 7 data bits? */
1331		val &= 0x7F;
1332	} else if ((cflag & CS6) == CS6) {		/* 6 data bits? */
1333		val &= 0x3F;
1334	} else {					/* Must be 5 */
1335		val &= 0x1F;
1336	}
1337	if (iflag & ISTRIP)
1338		val &= 0x7F;
1339
1340	ccbp->hi_mask = val;
1341
1342	/* ========== set hi_prtcl ========== */
1343	val = SP_DCEN;		/* Monitor DCD always, or TIOCMGET misses it */
1344	if (iflag & IXANY)
1345		val |= SP_TANY;
1346	if (iflag & IXON)
1347		val |= SP_TXEN;
1348	if (iflag & IXOFF)
1349		val |= SP_RXEN;
1350	if (iflag & INPCK)
1351		val |= SP_PAEN;
1352
1353	ccbp->hi_prtcl = val;
1354
1355
1356	/* ========== set hi_{rx|tx}{on|off} ========== */
1357	/* XXX: the card TOTALLY shields us from the flow control... */
1358	ccbp->hi_txon = t->c_cc[VSTART];
1359	ccbp->hi_txoff = t->c_cc[VSTOP];
1360
1361	ccbp->hi_rxon = t->c_cc[VSTART];
1362	ccbp->hi_rxoff = t->c_cc[VSTOP];
1363
1364	/* ========== send settings to the card ========== */
1365	/* potential sleep here */
1366	if (ccbp->hi_stat == IDLE_CLOSE)		/* Not yet open */
1367		si_command(pp, LOPEN, SI_WAIT);		/* open it */
1368	else
1369		si_command(pp, CONFIG, SI_WAIT);	/* change params */
1370
1371	/* ========== set DTR etc ========== */
1372	/* Hangup if ospeed == 0 */
1373	if (t->c_ospeed == 0) {
1374		(void) si_modem(pp, BIC, TIOCM_DTR|TIOCM_RTS);
1375	} else {
1376		/*
1377		 * If the previous speed was 0, may need to re-enable
1378		 * the modem signals
1379		 */
1380		(void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1381	}
1382
1383	DPRINT((pp, DBG_PARAM, "siparam, complete: MR1 %x MR2 %x HI_MASK %x PRTCL %x HI_BREAK %x\n",
1384		ccbp->hi_mr1, ccbp->hi_mr2, ccbp->hi_mask, ccbp->hi_prtcl, ccbp->hi_break));
1385
1386	splx(oldspl);
1387	return(error);
1388}
1389
1390/*
1391 * Enable or Disable the writes to this channel...
1392 * "state" ->  enabled = 1; disabled = 0;
1393 */
1394static void
1395si_write_enable(struct si_port *pp, int state)
1396{
1397	int oldspl;
1398
1399	oldspl = spltty();
1400
1401	if (state) {
1402		pp->sp_state &= ~SS_BLOCKWRITE;
1403		if (pp->sp_state & SS_WAITWRITE) {
1404			pp->sp_state &= ~SS_WAITWRITE;
1405			/* thunder away! */
1406			wakeup(pp);
1407		}
1408	} else {
1409		pp->sp_state |= SS_BLOCKWRITE;
1410	}
1411
1412	splx(oldspl);
1413}
1414
1415/*
1416 * Set/Get state of modem control lines.
1417 * Due to DCE-like behaviour of the adapter, some signals need translation:
1418 *	TIOCM_DTR	DSR
1419 *	TIOCM_RTS	CTS
1420 */
1421static int
1422si_modem(struct si_port *pp, enum si_mctl cmd, int bits)
1423{
1424	volatile struct si_channel *ccbp;
1425	int x;
1426
1427	DPRINT((pp, DBG_ENTRY|DBG_MODEM, "si_modem(%x,%s,%x)\n", pp, si_mctl2str(cmd), bits));
1428	ccbp = pp->sp_ccb;		/* Find channel address */
1429	switch (cmd) {
1430	case GET:
1431		x = ccbp->hi_ip;
1432		bits = TIOCM_LE;
1433		if (x & IP_DCD)		bits |= TIOCM_CAR;
1434		if (x & IP_DTR)		bits |= TIOCM_DTR;
1435		if (x & IP_RTS)		bits |= TIOCM_RTS;
1436		if (x & IP_RI)		bits |= TIOCM_RI;
1437		return(bits);
1438	case SET:
1439		ccbp->hi_op &= ~(OP_DSR|OP_CTS);
1440		/* fall through */
1441	case BIS:
1442		x = 0;
1443		if (bits & TIOCM_DTR)
1444			x |= OP_DSR;
1445		if (bits & TIOCM_RTS)
1446			x |= OP_CTS;
1447		ccbp->hi_op |= x;
1448		break;
1449	case BIC:
1450		if (bits & TIOCM_DTR)
1451			ccbp->hi_op &= ~OP_DSR;
1452		if (bits & TIOCM_RTS)
1453			ccbp->hi_op &= ~OP_CTS;
1454	}
1455	return 0;
1456}
1457
1458/*
1459 * Handle change of modem state
1460 */
1461static void
1462si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip)
1463{
1464							/* if a modem dev */
1465	if (hi_ip & IP_DCD) {
1466		if (!(pp->sp_last_hi_ip & IP_DCD)) {
1467			DPRINT((pp, DBG_INTR, "modem carr on t_line %d\n",
1468				tp->t_line));
1469			(void)(*linesw[tp->t_line].l_modem)(tp, 1);
1470		}
1471	} else {
1472		if (pp->sp_last_hi_ip & IP_DCD) {
1473			DPRINT((pp, DBG_INTR, "modem carr off\n"));
1474			if ((*linesw[tp->t_line].l_modem)(tp, 0))
1475				(void) si_modem(pp, SET, 0);
1476		}
1477	}
1478	pp->sp_last_hi_ip = hi_ip;
1479
1480}
1481
1482/*
1483 * Poller to catch missed interrupts.
1484 *
1485 * Note that the SYSV Specialix drivers poll at 100 times per second to get
1486 * better response.  We could really use a "periodic" version timeout(). :-)
1487 */
1488#ifdef POLL
1489static void
1490si_poll(void *nothing)
1491{
1492	struct si_softc *sc;
1493	int i;
1494	volatile struct si_reg *regp;
1495	struct si_port *pp;
1496	int lost, oldspl, port;
1497
1498	DPRINT((0, DBG_POLL, "si_poll()\n"));
1499	oldspl = spltty();
1500	if (in_intr)
1501		goto out;
1502	lost = 0;
1503	for (i = 0; i < si_numunits; i++) {
1504		sc = devclass_get_softc(si_devclass, i);
1505		if (sc == NULL || sc->sc_type == SIEMPTY)
1506			continue;
1507		regp = (struct si_reg *)sc->sc_maddr;
1508
1509		/*
1510		 * See if there has been a pending interrupt for 2 seconds
1511		 * or so. The test (int_scounter >= 200) won't correspond
1512		 * to 2 seconds if int_count gets changed.
1513		 */
1514		if (regp->int_pending != 0) {
1515			if (regp->int_scounter >= 200 &&
1516			    regp->initstat == 1) {
1517				printf("si%d: lost intr\n", i);
1518				lost++;
1519			}
1520		} else {
1521			regp->int_scounter = 0;
1522		}
1523
1524		/*
1525		 * gripe about no input flow control..
1526		 */
1527		pp = sc->sc_ports;
1528		for (port = 0; port < sc->sc_nport; pp++, port++) {
1529			if (pp->sp_delta_overflows > 0) {
1530				printf("si%d: %d tty level buffer overflows\n",
1531					i, pp->sp_delta_overflows);
1532				pp->sp_delta_overflows = 0;
1533			}
1534		}
1535	}
1536	if (lost || si_realpoll)
1537		si_intr(NULL);	/* call intr with fake vector */
1538out:
1539	splx(oldspl);
1540
1541	timeout(si_poll, (caddr_t)0L, si_pollrate);
1542}
1543#endif	/* ifdef POLL */
1544
1545/*
1546 * The interrupt handler polls ALL ports on ALL adapters each time
1547 * it is called.
1548 */
1549
1550static BYTE si_rxbuf[SI_BUFFERSIZE];	/* input staging area */
1551static BYTE si_txbuf[SI_BUFFERSIZE];	/* output staging area */
1552
1553void
1554si_intr(void *arg)
1555{
1556	struct si_softc *sc;
1557	struct si_port *pp;
1558	volatile struct si_channel *ccbp;
1559	struct tty *tp;
1560	volatile caddr_t maddr;
1561	BYTE op, ip;
1562	int x, card, port, n, i, isopen;
1563	volatile BYTE *z;
1564	BYTE c;
1565
1566	sc = arg;
1567
1568	DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "si_intr\n"));
1569	if (in_intr)
1570		return;
1571	in_intr = 1;
1572
1573	/*
1574	 * When we get an int we poll all the channels and do ALL pending
1575	 * work, not just the first one we find. This allows all cards to
1576	 * share the same vector.
1577	 *
1578	 * XXX - But if we're sharing the vector with something that's NOT
1579	 * a SI/XIO/SX card, we may be making more work for ourselves.
1580	 */
1581	for (card = 0; card < si_numunits; card++) {
1582		sc = devclass_get_softc(si_devclass, card);
1583		if (sc == NULL || sc->sc_type == SIEMPTY)
1584			continue;
1585
1586		/*
1587		 * First, clear the interrupt
1588		 */
1589		switch(sc->sc_type) {
1590		case SIHOST:
1591			maddr = sc->sc_maddr;
1592			((volatile struct si_reg *)maddr)->int_pending = 0;
1593							/* flag nothing pending */
1594			*(maddr+SIINTCL) = 0x00;	/* Set IRQ clear */
1595			*(maddr+SIINTCL_CL) = 0x00;	/* Clear IRQ clear */
1596			break;
1597		case SIHOST2:
1598			maddr = sc->sc_maddr;
1599			((volatile struct si_reg *)maddr)->int_pending = 0;
1600			*(maddr+SIPLIRQCLR) = 0x00;
1601			*(maddr+SIPLIRQCLR) = 0x10;
1602			break;
1603		case SIPCI:
1604			maddr = sc->sc_maddr;
1605			((volatile struct si_reg *)maddr)->int_pending = 0;
1606			*(maddr+SIPCIINTCL) = 0x0;
1607			break;
1608		case SIJETPCI:	/* fall through to JETISA case */
1609		case SIJETISA:
1610			maddr = sc->sc_maddr;
1611			((volatile struct si_reg *)maddr)->int_pending = 0;
1612			*(maddr+SIJETINTCL) = 0x0;
1613			break;
1614		case SIEISA:
1615			maddr = sc->sc_maddr;
1616			((volatile struct si_reg *)maddr)->int_pending = 0;
1617			(void)inb(sc->sc_iobase + 3);
1618			break;
1619		case SIEMPTY:
1620		default:
1621			continue;
1622		}
1623		((volatile struct si_reg *)maddr)->int_scounter = 0;
1624
1625		/*
1626		 * check each port
1627		 */
1628		for (pp = sc->sc_ports, port = 0; port < sc->sc_nport;
1629		     pp++, port++) {
1630			ccbp = pp->sp_ccb;
1631			tp = pp->sp_tty;
1632
1633			/*
1634			 * See if a command has completed ?
1635			 */
1636			if (ccbp->hi_stat != pp->sp_pend) {
1637				DPRINT((pp, DBG_INTR,
1638					"si_intr hi_stat = 0x%x, pend = %d\n",
1639					ccbp->hi_stat, pp->sp_pend));
1640				switch(pp->sp_pend) {
1641				case LOPEN:
1642				case MPEND:
1643				case MOPEN:
1644				case CONFIG:
1645				case SBREAK:
1646				case EBREAK:
1647					pp->sp_pend = ccbp->hi_stat;
1648						/* sleeping in si_command */
1649					wakeup(&pp->sp_state);
1650					break;
1651				default:
1652					pp->sp_pend = ccbp->hi_stat;
1653				}
1654			}
1655
1656			/*
1657			 * Continue on if it's closed
1658			 */
1659			if (ccbp->hi_stat == IDLE_CLOSE) {
1660				continue;
1661			}
1662
1663			/*
1664			 * Do modem state change if not a local device
1665			 */
1666			si_modem_state(pp, tp, ccbp->hi_ip);
1667
1668			/*
1669			 * Check to see if we should 'receive' characters.
1670			 */
1671			if (tp->t_state & TS_CONNECTED &&
1672			    tp->t_state & TS_ISOPEN)
1673				isopen = 1;
1674			else
1675				isopen = 0;
1676
1677			/*
1678			 * Do input break processing
1679			 */
1680			if (ccbp->hi_state & ST_BREAK) {
1681				if (isopen) {
1682				    (*linesw[tp->t_line].l_rint)(TTY_BI, tp);
1683				}
1684				ccbp->hi_state &= ~ST_BREAK;   /* A Bit iffy this */
1685				DPRINT((pp, DBG_INTR, "si_intr break\n"));
1686			}
1687
1688			/*
1689			 * Do RX stuff - if not open then dump any characters.
1690			 * XXX: This is VERY messy and needs to be cleaned up.
1691			 *
1692			 * XXX: can we leave data in the host adapter buffer
1693			 * when the clists are full?  That may be dangerous
1694			 * if the user cannot get an interrupt signal through.
1695			 */
1696
1697	more_rx:	/* XXX Sorry. the nesting was driving me bats! :-( */
1698
1699			if (!isopen) {
1700				ccbp->hi_rxopos = ccbp->hi_rxipos;
1701				goto end_rx;
1702			}
1703
1704			/*
1705			 * If the tty input buffers are blocked, stop emptying
1706			 * the incoming buffers and let the auto flow control
1707			 * assert..
1708			 */
1709			if (tp->t_state & TS_TBLOCK) {
1710				goto end_rx;
1711			}
1712
1713			/*
1714			 * Process read characters if not skipped above
1715			 */
1716			op = ccbp->hi_rxopos;
1717			ip = ccbp->hi_rxipos;
1718			c = ip - op;
1719			if (c == 0) {
1720				goto end_rx;
1721			}
1722
1723			n = c & 0xff;
1724			if (n > 250)
1725				n = 250;
1726
1727			DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1728						n, op, ip));
1729
1730			/*
1731			 * Suck characters out of host card buffer into the
1732			 * "input staging buffer" - so that we dont leave the
1733			 * host card in limbo while we're possibly echoing
1734			 * characters and possibly flushing input inside the
1735			 * ldisc l_rint() routine.
1736			 */
1737			if (n <= SI_BUFFERSIZE - op) {
1738
1739				DPRINT((pp, DBG_INTR, "\tsingle copy\n"));
1740				z = ccbp->hi_rxbuf + op;
1741				si_vbcopy(z, si_rxbuf, n);
1742
1743				op += n;
1744			} else {
1745				x = SI_BUFFERSIZE - op;
1746
1747				DPRINT((pp, DBG_INTR, "\tdouble part 1 %d\n", x));
1748				z = ccbp->hi_rxbuf + op;
1749				si_vbcopy(z, si_rxbuf, x);
1750
1751				DPRINT((pp, DBG_INTR, "\tdouble part 2 %d\n",
1752					n - x));
1753				z = ccbp->hi_rxbuf;
1754				si_vbcopy(z, si_rxbuf + x, n - x);
1755
1756				op += n;
1757			}
1758
1759			/* clear collected characters from buffer */
1760			ccbp->hi_rxopos = op;
1761
1762			DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1763						n, op, ip));
1764
1765			/*
1766			 * at this point...
1767			 * n = number of chars placed in si_rxbuf
1768			 */
1769
1770			/*
1771			 * Avoid the grotesquely inefficient lineswitch
1772			 * routine (ttyinput) in "raw" mode. It usually
1773			 * takes about 450 instructions (that's without
1774			 * canonical processing or echo!). slinput is
1775			 * reasonably fast (usually 40 instructions
1776			 * plus call overhead).
1777			 */
1778			if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1779
1780				/* block if the driver supports it */
1781				if (tp->t_rawq.c_cc + n >= SI_I_HIGH_WATER &&
1782				    (tp->t_cflag & CRTS_IFLOW ||
1783				     tp->t_iflag & IXOFF) &&
1784				    !(tp->t_state & TS_TBLOCK))
1785					ttyblock(tp);
1786
1787				tk_nin += n;
1788				tk_rawcc += n;
1789				tp->t_rawcc += n;
1790
1791				pp->sp_delta_overflows +=
1792				    b_to_q((char *)si_rxbuf, n, &tp->t_rawq);
1793
1794				ttwakeup(tp);
1795				if (tp->t_state & TS_TTSTOP &&
1796				    (tp->t_iflag & IXANY ||
1797				     tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1798					tp->t_state &= ~TS_TTSTOP;
1799					tp->t_lflag &= ~FLUSHO;
1800					si_start(tp);
1801				}
1802			} else {
1803				/*
1804				 * It'd be nice to not have to go through the
1805				 * function call overhead for each char here.
1806				 * It'd be nice to block input it, saving a
1807				 * loop here and the call/return overhead.
1808				 */
1809				for(x = 0; x < n; x++) {
1810					i = si_rxbuf[x];
1811					if ((*linesw[tp->t_line].l_rint)(i, tp)
1812					     == -1) {
1813						pp->sp_delta_overflows++;
1814					}
1815				}
1816			}
1817			goto more_rx;	/* try for more until RXbuf is empty */
1818
1819	end_rx:		/* XXX: Again, sorry about the gotos.. :-) */
1820
1821			/*
1822			 * Do TX stuff
1823			 */
1824			(*linesw[tp->t_line].l_start)(tp);
1825
1826		} /* end of for (all ports on this controller) */
1827	} /* end of for (all controllers) */
1828
1829	in_intr = 0;
1830	DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "end si_intr\n"));
1831}
1832
1833/*
1834 * Nudge the transmitter...
1835 *
1836 * XXX: I inherited some funny code here.  It implies the host card only
1837 * interrupts when the transmit buffer reaches the low-water-mark, and does
1838 * not interrupt when it's actually hits empty.  In some cases, we have
1839 * processes waiting for complete drain, and we need to simulate an interrupt
1840 * about when we think the buffer is going to be empty (and retry if not).
1841 * I really am not certain about this...  I *need* the hardware manuals.
1842 */
1843static void
1844si_start(struct tty *tp)
1845{
1846	struct si_port *pp;
1847	volatile struct si_channel *ccbp;
1848	struct clist *qp;
1849	BYTE ipos;
1850	int nchar;
1851	int oldspl, count, n, amount, buffer_full;
1852
1853	oldspl = spltty();
1854
1855	qp = &tp->t_outq;
1856	pp = TP2PP(tp);
1857
1858	DPRINT((pp, DBG_ENTRY|DBG_START,
1859		"si_start(%x) t_state %x sp_state %x t_outq.c_cc %d\n",
1860		tp, tp->t_state, pp->sp_state, qp->c_cc));
1861
1862	if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
1863		goto out;
1864
1865	buffer_full = 0;
1866	ccbp = pp->sp_ccb;
1867
1868	count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1869	DPRINT((pp, DBG_START, "count %d\n", (BYTE)count));
1870
1871	while ((nchar = qp->c_cc) > 0) {
1872		if ((BYTE)count >= 255) {
1873			buffer_full++;
1874			break;
1875		}
1876		amount = min(nchar, (255 - (BYTE)count));
1877		ipos = (unsigned int)ccbp->hi_txipos;
1878		n = q_to_b(&tp->t_outq, si_txbuf, amount);
1879		/* will it fit in one lump? */
1880		if ((SI_BUFFERSIZE - ipos) >= n) {
1881			si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos], n);
1882		} else {
1883			si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos],
1884				SI_BUFFERSIZE - ipos);
1885			si_bcopyv(si_txbuf + (SI_BUFFERSIZE - ipos),
1886				&ccbp->hi_txbuf[0], n - (SI_BUFFERSIZE - ipos));
1887		}
1888		ccbp->hi_txipos += n;
1889		count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1890	}
1891
1892	if (count != 0 && nchar == 0) {
1893		tp->t_state |= TS_BUSY;
1894	} else {
1895		tp->t_state &= ~TS_BUSY;
1896	}
1897
1898	/* wakeup time? */
1899	ttwwakeup(tp);
1900
1901	DPRINT((pp, DBG_START, "count %d, nchar %d, tp->t_state 0x%x\n",
1902		(BYTE)count, nchar, tp->t_state));
1903
1904	if (tp->t_state & TS_BUSY)
1905	{
1906		int time;
1907
1908		time = ttspeedtab(tp->t_ospeed, chartimes);
1909
1910		if (time > 0) {
1911			if (time < nchar)
1912				time = nchar / time;
1913			else
1914				time = 2;
1915		} else {
1916			DPRINT((pp, DBG_START,
1917				"bad char time value! %d\n", time));
1918			time = hz/10;
1919		}
1920
1921		if ((pp->sp_state & (SS_LSTART|SS_INLSTART)) == SS_LSTART) {
1922			untimeout(si_lstart, (caddr_t)pp, pp->lstart_ch);
1923		} else {
1924			pp->sp_state |= SS_LSTART;
1925		}
1926		DPRINT((pp, DBG_START, "arming lstart, time=%d\n", time));
1927		pp->lstart_ch = timeout(si_lstart, (caddr_t)pp, time);
1928	}
1929
1930out:
1931	splx(oldspl);
1932	DPRINT((pp, DBG_EXIT|DBG_START, "leave si_start()\n"));
1933}
1934
1935/*
1936 * Note: called at splsoftclock from the timeout code
1937 * This has to deal with two things...  cause wakeups while waiting for
1938 * tty drains on last process exit, and call l_start at about the right
1939 * time for protocols like ppp.
1940 */
1941static void
1942si_lstart(void *arg)
1943{
1944	struct si_port *pp = arg;
1945	struct tty *tp;
1946	int oldspl;
1947
1948	DPRINT((pp, DBG_ENTRY|DBG_LSTART, "si_lstart(%x) sp_state %x\n",
1949		pp, pp->sp_state));
1950
1951	oldspl = spltty();
1952
1953	if ((pp->sp_state & SS_OPEN) == 0 || (pp->sp_state & SS_LSTART) == 0) {
1954		splx(oldspl);
1955		return;
1956	}
1957	pp->sp_state &= ~SS_LSTART;
1958	pp->sp_state |= SS_INLSTART;
1959
1960	tp = pp->sp_tty;
1961
1962	/* deal with the process exit case */
1963	ttwwakeup(tp);
1964
1965	/* nudge protocols - eg: ppp */
1966	(*linesw[tp->t_line].l_start)(tp);
1967
1968	pp->sp_state &= ~SS_INLSTART;
1969	splx(oldspl);
1970}
1971
1972/*
1973 * Stop output on a line. called at spltty();
1974 */
1975static void
1976si_stop(struct tty *tp, int rw)
1977{
1978	volatile struct si_channel *ccbp;
1979	struct si_port *pp;
1980
1981	pp = TP2PP(tp);
1982	ccbp = pp->sp_ccb;
1983
1984	DPRINT((TP2PP(tp), DBG_ENTRY|DBG_STOP, "si_stop(%x,%x)\n", tp, rw));
1985
1986	/* XXX: must check (rw & FWRITE | FREAD) etc flushing... */
1987	if (rw & FWRITE) {
1988		/* what level are we meant to be flushing anyway? */
1989		if (tp->t_state & TS_BUSY) {
1990			si_command(TP2PP(tp), WFLUSH, SI_NOWAIT);
1991			tp->t_state &= ~TS_BUSY;
1992			ttwwakeup(tp);	/* Bruce???? */
1993		}
1994	}
1995#if 1	/* XXX: this doesn't work right yet.. */
1996	/* XXX: this may have been failing because we used to call l_rint()
1997	 * while we were looping based on these two counters. Now, we collect
1998	 * the data and then loop stuffing it into l_rint(), making this
1999	 * useless.  Should we cause this to blow away the staging buffer?
2000	 */
2001	if (rw & FREAD) {
2002		ccbp->hi_rxopos = ccbp->hi_rxipos;
2003	}
2004#endif
2005}
2006
2007/*
2008 * Issue a command to the host card CPU.
2009 */
2010
2011static void
2012si_command(struct si_port *pp, int cmd, int waitflag)
2013{
2014	int oldspl;
2015	volatile struct si_channel *ccbp = pp->sp_ccb;
2016	int x;
2017
2018	DPRINT((pp, DBG_ENTRY|DBG_PARAM, "si_command(%x,%x,%d): hi_stat 0x%x\n",
2019		pp, cmd, waitflag, ccbp->hi_stat));
2020
2021	oldspl = spltty();		/* Keep others out */
2022
2023	/* wait until it's finished what it was doing.. */
2024	/* XXX: sits in IDLE_BREAK until something disturbs it or break
2025	 * is turned off. */
2026	while((x = ccbp->hi_stat) != IDLE_OPEN &&
2027			x != IDLE_CLOSE &&
2028			x != IDLE_BREAK &&
2029			x != cmd) {
2030		if (in_intr) {			/* Prevent sleep in intr */
2031			DPRINT((pp, DBG_PARAM,
2032				"cmd intr collision - completing %d\trequested %d\n",
2033				x, cmd));
2034			splx(oldspl);
2035			return;
2036		} else if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, TTIPRI|PCATCH,
2037				"sicmd1", 1)) {
2038			splx(oldspl);
2039			return;
2040		}
2041	}
2042	/* it should now be in IDLE_{OPEN|CLOSE|BREAK}, or "cmd" */
2043
2044	/* if there was a pending command, cause a state-change wakeup */
2045	switch(pp->sp_pend) {
2046	case LOPEN:
2047	case MPEND:
2048	case MOPEN:
2049	case CONFIG:
2050	case SBREAK:
2051	case EBREAK:
2052		wakeup(&pp->sp_state);
2053		break;
2054	default:
2055		break;
2056	}
2057
2058	pp->sp_pend = cmd;		/* New command pending */
2059	ccbp->hi_stat = cmd;		/* Post it */
2060
2061	if (waitflag) {
2062		if (in_intr) {		/* If in interrupt handler */
2063			DPRINT((pp, DBG_PARAM,
2064				"attempt to sleep in si_intr - cmd req %d\n",
2065				cmd));
2066			splx(oldspl);
2067			return;
2068		} else while(ccbp->hi_stat != IDLE_OPEN &&
2069			     ccbp->hi_stat != IDLE_BREAK) {
2070			if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, TTIPRI|PCATCH,
2071			    "sicmd2", 0))
2072				break;
2073		}
2074	}
2075	splx(oldspl);
2076}
2077
2078static void
2079si_disc_optim(struct tty *tp, struct termios *t, struct si_port *pp)
2080{
2081	/*
2082	 * XXX can skip a lot more cases if Smarts.  Maybe
2083	 * (IGNCR | ISTRIP | IXON) in c_iflag.  But perhaps we
2084	 * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2085	 */
2086	if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) &&
2087	    (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) &&
2088	    (!(t->c_iflag & PARMRK) ||
2089	     (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) &&
2090	    !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) &&
2091	    linesw[tp->t_line].l_rint == ttyinput)
2092		tp->t_state |= TS_CAN_BYPASS_L_RINT;
2093	else
2094		tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2095	pp->sp_hotchar = linesw[tp->t_line].l_hotchar;
2096	DPRINT((pp, DBG_OPTIM, "bypass: %s, hotchar: %x\n",
2097		(tp->t_state & TS_CAN_BYPASS_L_RINT) ? "on" : "off",
2098		pp->sp_hotchar));
2099}
2100
2101
2102#ifdef	SI_DEBUG
2103
2104void
2105si_dprintf(struct si_port *pp, int flags, const char *fmt, ...)
2106{
2107	va_list ap;
2108
2109	if ((pp == NULL && (si_debug&flags)) ||
2110	    (pp != NULL && ((pp->sp_debug&flags) || (si_debug&flags)))) {
2111		if (pp != NULL)
2112			printf("%ci%d(%d): ", 's',
2113				(int)SI_CARD(minor(pp->sp_tty->t_dev)),
2114				(int)SI_PORT(minor(pp->sp_tty->t_dev)));
2115		va_start(ap, fmt);
2116		vprintf(fmt, ap);
2117		va_end(ap);
2118	}
2119}
2120
2121static char *
2122si_mctl2str(enum si_mctl cmd)
2123{
2124	switch (cmd) {
2125	case GET:
2126		return("GET");
2127	case SET:
2128		return("SET");
2129	case BIS:
2130		return("BIS");
2131	case BIC:
2132		return("BIC");
2133	}
2134	return("BAD");
2135}
2136
2137#endif	/* DEBUG */
2138
2139static char *
2140si_modulename(int host_type, int uart_type)
2141{
2142	switch (host_type) {
2143	/* Z280 based cards */
2144	case SIEISA:
2145	case SIHOST2:
2146	case SIHOST:
2147	case SIPCI:
2148		switch (uart_type) {
2149		case 0:
2150			return(" (XIO)");
2151		case 1:
2152			return(" (SI)");
2153		}
2154		break;
2155	/* T225 based hosts */
2156	case SIJETPCI:
2157	case SIJETISA:
2158		switch (uart_type) {
2159		case 0:
2160			return(" (SI)");
2161		case 40:
2162			return(" (XIO)");
2163		case 72:
2164			return(" (SXDC)");
2165		}
2166		break;
2167	}
2168	return("");
2169}
2170