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