digi.c revision 94323
176195Sbrian/*-
276195Sbrian * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
376358Sbrian *   based on work by Slawa Olhovchenkov
476358Sbrian *                    John Prince <johnp@knight-trosoft.com>
576358Sbrian *                    Eric Hernes
676195Sbrian * All rights reserved.
776195Sbrian *
876195Sbrian * Redistribution and use in source and binary forms, with or without
976195Sbrian * modification, are permitted provided that the following conditions
1076195Sbrian * are met:
1176195Sbrian * 1. Redistributions of source code must retain the above copyright
1276195Sbrian *    notice, this list of conditions and the following disclaimer.
1376195Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1476195Sbrian *    notice, this list of conditions and the following disclaimer in the
1576195Sbrian *    documentation and/or other materials provided with the distribution.
1676195Sbrian *
1776195Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1876195Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1976195Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2076195Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2176195Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2276195Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2376195Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2476195Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2576195Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2676195Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2776195Sbrian * SUCH DAMAGE.
2876195Sbrian *
2976195Sbrian * $FreeBSD: head/sys/dev/digi/digi.c 94323 2002-04-10 01:15:23Z brian $
3076195Sbrian */
3176195Sbrian
3276195Sbrian/*-
3376195Sbrian * TODO:
3476195Sbrian *	Figure out what the con bios stuff is supposed to do
3576195Sbrian *	Test with *LOTS* more cards - I only have a PCI8r and an ISA Xem.
3676195Sbrian */
3776195Sbrian
3890684Sbde#include "opt_compat.h"
3990684Sbde
4076195Sbrian#include <sys/param.h>
4176195Sbrian#include <sys/systm.h>
4276195Sbrian#include <sys/proc.h>
4376195Sbrian#include <sys/conf.h>
4476195Sbrian#include <sys/linker.h>
4576195Sbrian#include <sys/kernel.h>
4676195Sbrian#include <sys/mbuf.h>
4776195Sbrian#include <sys/malloc.h>
4876195Sbrian#include <sys/tty.h>
4976195Sbrian#include <sys/syslog.h>
5076195Sbrian#include <sys/fcntl.h>
5176195Sbrian#include <sys/bus.h>
5276195Sbrian#include <sys/bus.h>
5376195Sbrian#include <machine/resource.h>
5476195Sbrian
5576848Sbrian#include <sys/digiio.h>
5676853Sbrian#include <dev/digi/digireg.h>
5776853Sbrian#include <dev/digi/digi.h>
5876853Sbrian#include <dev/digi/digi_mod.h>
5976853Sbrian#include <dev/digi/digi_pci.h>
6076195Sbrian
6176195Sbrian#define	CDEV_MAJOR	162
6276195Sbrian
6376195Sbrian#define	CTRL_DEV		0x800000
6476195Sbrian#define	CALLOUT_MASK		0x400000
6576195Sbrian#define	CONTROL_INIT_STATE	0x100000
6676195Sbrian#define	CONTROL_LOCK_STATE	0x200000
6776195Sbrian#define	CONTROL_MASK		(CTRL_DEV|CONTROL_INIT_STATE|CONTROL_LOCK_STATE)
6876195Sbrian#define UNIT_MASK		0x030000
6976195Sbrian#define PORT_MASK		0x0000FF
7076195Sbrian#define	DEV_TO_UNIT(dev)	(MINOR_TO_UNIT(minor(dev)))
7176195Sbrian#define	MINOR_MAGIC_MASK	(CALLOUT_MASK | CONTROL_MASK)
7276195Sbrian#define	MINOR_TO_UNIT(mynor)	(((mynor) & UNIT_MASK)>>16)
7376195Sbrian#define MINOR_TO_PORT(mynor)	((mynor) & PORT_MASK)
7476195Sbrian
7576195Sbrianstatic d_open_t		digiopen;
7676195Sbrianstatic d_close_t	digiclose;
7776195Sbrianstatic d_read_t		digiread;
7876195Sbrianstatic d_write_t	digiwrite;
7976195Sbrianstatic d_ioctl_t	digiioctl;
8076195Sbrian
8176195Sbrianstatic void	digistop(struct tty *tp, int rw);
8276195Sbrianstatic int	digimctl(struct digi_p *port, int bits, int how);
8376195Sbrianstatic void	digi_poll(void *ptr);
8476195Sbrianstatic void	digi_freemoduledata(struct digi_softc *);
8576195Sbrianstatic void	fepcmd(struct digi_p *port, int cmd, int op, int ncmds);
8676195Sbrianstatic void	digistart(struct tty *tp);
8776195Sbrianstatic int	digiparam(struct tty *tp, struct termios *t);
8876195Sbrianstatic void	digihardclose(struct digi_p *port);
8976195Sbrianstatic void	digi_intr(void *);
9076195Sbrianstatic int	digi_init(struct digi_softc *_sc);
9176195Sbrianstatic int	digi_loadmoduledata(struct digi_softc *);
9276195Sbrianstatic int	digi_inuse(struct digi_softc *);
9376195Sbrianstatic void	digi_free_state(struct digi_softc *);
9476195Sbrian
9576195Sbrian#define	fepcmd_b(port, cmd, op1, op2, ncmds) \
9676195Sbrian	fepcmd(port, cmd, (op2 << 8) | op1, ncmds)
9776195Sbrian#define	fepcmd_w	fepcmd
9876195Sbrian
9976195Sbrian
10076195Sbrianstatic speed_t digidefaultrate = TTYDEF_SPEED;
10176195Sbrian
10276195Sbrianstruct con_bios {
10376195Sbrian	struct con_bios *next;
10476195Sbrian	u_char *bios;
10576195Sbrian	size_t size;
10676195Sbrian};
10776195Sbrian
10889062Smsmithstatic struct con_bios *con_bios_list;
10991445Speterdevclass_t	 digi_devclass;
11076195Sbrianstatic char	 driver_name[] = "digi";
11176195Sbrianunsigned 	 digi_debug = 0;
11276195Sbrian
11376195Sbrianstatic struct speedtab digispeedtab[] = {
11476195Sbrian	{ 0,		0},			/* old (sysV-like) Bx codes */
11576195Sbrian	{ 50,		1},
11676195Sbrian	{ 75,		2},
11776195Sbrian	{ 110,		3},
11876195Sbrian	{ 134,		4},
11976195Sbrian	{ 150,		5},
12076195Sbrian	{ 200,		6},
12176195Sbrian	{ 300,		7},
12276195Sbrian	{ 600,		8},
12376195Sbrian	{ 1200,		9},
12476195Sbrian	{ 1800,		10},
12576195Sbrian	{ 2400,		11},
12676195Sbrian	{ 4800,		12},
12776195Sbrian	{ 9600,		13},
12876195Sbrian	{ 19200,	14},
12976195Sbrian	{ 38400,	15},
13076195Sbrian	{ 57600,	(02000 | 1)},
13176195Sbrian	{ 76800,	(02000 | 2)},
13276195Sbrian	{ 115200,	(02000 | 3)},
13376195Sbrian	{ 230400,	(02000 | 6)},
13476195Sbrian	{ -1,		-1}
13576195Sbrian};
13676195Sbrian
13776195Sbrianconst struct digi_control_signals digi_xixe_signals = {
13876195Sbrian	0x02, 0x08, 0x10, 0x20, 0x40, 0x80
13976195Sbrian};
14076195Sbrian
14176195Sbrianconst struct digi_control_signals digi_normal_signals = {
14276195Sbrian	0x02, 0x80, 0x20, 0x10, 0x40, 0x01
14376195Sbrian};
14476195Sbrian
14576195Sbrianstatic struct cdevsw digi_sw = {
14676195Sbrian	digiopen,		/* open */
14776195Sbrian	digiclose,		/* close */
14876195Sbrian	digiread,		/* read */
14976195Sbrian	digiwrite,		/* write */
15076195Sbrian	digiioctl,		/* ioctl */
15176195Sbrian	ttypoll,		/* poll */
15276195Sbrian	nommap,			/* mmap */
15376195Sbrian	nostrategy,		/* strategy */
15476195Sbrian	driver_name,		/* name */
15576195Sbrian	CDEV_MAJOR,		/* maj */
15676195Sbrian	nodump,			/* dump */
15776195Sbrian	nopsize,		/* psize */
15876195Sbrian	D_TTY | D_KQFILTER,	/* flags */
15976195Sbrian	ttykqfilter		/* bmaj */
16076195Sbrian};
16176195Sbrian
16276195Sbrianstatic void
16376195Sbriandigi_poll(void *ptr)
16476195Sbrian{
16576195Sbrian	struct digi_softc *sc;
16676195Sbrian
16776195Sbrian	sc = (struct digi_softc *)ptr;
16876195Sbrian	callout_handle_init(&sc->callout);
16976195Sbrian	digi_intr(sc);
17076195Sbrian	sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
17176195Sbrian}
17276195Sbrian
17376195Sbrianstatic void
17476195Sbriandigi_int_test(void *v)
17576195Sbrian{
17676195Sbrian	struct digi_softc *sc = v;
17776195Sbrian
17876195Sbrian	callout_handle_init(&sc->inttest);
17976195Sbrian#ifdef DIGI_INTERRUPT
18076195Sbrian	if (sc->intr_timestamp.tv_sec || sc->intr_timestamp.tv_usec) {
18176195Sbrian		/* interrupt OK! */
18276195Sbrian		return;
18376195Sbrian	}
18476195Sbrian	log(LOG_ERR, "digi%d: Interrupt didn't work, use polled mode\n", unit);
18576195Sbrian#endif
18676195Sbrian	sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
18776195Sbrian}
18876195Sbrian
18976195Sbrianstatic void
19076195Sbriandigi_freemoduledata(struct digi_softc *sc)
19176195Sbrian{
19276195Sbrian	if (sc->fep.data != NULL) {
19376195Sbrian		free(sc->fep.data, M_TTYS);
19476195Sbrian		sc->fep.data = NULL;
19576195Sbrian	}
19676195Sbrian	if (sc->link.data != NULL) {
19776195Sbrian		free(sc->link.data, M_TTYS);
19876195Sbrian		sc->link.data = NULL;
19976195Sbrian	}
20076195Sbrian	if (sc->bios.data != NULL) {
20176195Sbrian		free(sc->bios.data, M_TTYS);
20276195Sbrian		sc->bios.data = NULL;
20376195Sbrian	}
20476195Sbrian}
20576195Sbrian
20676195Sbrianstatic int
20776195Sbriandigi_bcopy(const void *vfrom, void *vto, size_t sz)
20876195Sbrian{
20976195Sbrian	volatile const char *from = (volatile const char *)vfrom;
21076195Sbrian	volatile char *to = (volatile char *)vto;
21176195Sbrian	size_t i;
21276195Sbrian
21376195Sbrian	for (i = 0; i < sz; i++)
21476195Sbrian		*to++ = *from++;
21576195Sbrian
21676195Sbrian	from = (const volatile char *)vfrom;
21776195Sbrian	to = (volatile char *)vto;
21876195Sbrian	for (i = 0; i < sz; i++)
21976195Sbrian		if (*to++ != *from++)
22076195Sbrian			return (0);
22176195Sbrian	return (1);
22276195Sbrian}
22376195Sbrian
22476195Sbrianstatic int
22576195Sbriandigi_init(struct digi_softc *sc)
22676195Sbrian{
22776195Sbrian	int i, cnt, resp;
22876195Sbrian	u_char *ptr;
22976195Sbrian	int lowwater;
23076195Sbrian	struct digi_p *port;
23176195Sbrian	volatile struct board_chan *bc;
23276195Sbrian
23376195Sbrian	ptr = NULL;
23476195Sbrian
23576195Sbrian	if (sc->status == DIGI_STATUS_DISABLED) {
23676195Sbrian		log(LOG_ERR, "digi%d: Cannot init a disabled card\n",
23776195Sbrian		    sc->res.unit);
23876195Sbrian		return (EIO);
23976195Sbrian	}
24076195Sbrian	if (sc->bios.data == NULL) {
24176195Sbrian		log(LOG_ERR, "digi%d: Cannot init without BIOS\n",
24276195Sbrian		    sc->res.unit);
24376195Sbrian		return (EIO);
24476195Sbrian	}
24576195Sbrian#if 0
24676195Sbrian	if (sc->link.data == NULL && sc->model >= PCCX) {
24776195Sbrian		log(LOG_ERR, "digi%d: Cannot init without link info\n",
24876195Sbrian		    sc->res.unit);
24976195Sbrian		return (EIO);
25076195Sbrian	}
25176195Sbrian#endif
25276195Sbrian	if (sc->fep.data == NULL) {
25376195Sbrian		log(LOG_ERR, "digi%d: Cannot init without fep code\n",
25476195Sbrian		    sc->res.unit);
25576195Sbrian		return (EIO);
25676195Sbrian	}
25776195Sbrian	sc->status = DIGI_STATUS_NOTINIT;
25876195Sbrian
25976195Sbrian	if (sc->numports) {
26076195Sbrian		/*
26176195Sbrian		 * We're re-initialising - maybe because someone's attached
26276195Sbrian		 * another port module.  For now, we just re-initialise
26376195Sbrian		 * everything.
26476195Sbrian		 */
26576195Sbrian		if (digi_inuse(sc))
26676195Sbrian			return (EBUSY);
26776195Sbrian
26876195Sbrian		digi_free_state(sc);
26976195Sbrian	}
27076195Sbrian
27176195Sbrian	ptr = sc->setwin(sc, MISCGLOBAL);
27276195Sbrian	for (i = 0; i < 16; i += 2)
27376195Sbrian		vW(ptr + i) = 0;
27476195Sbrian
27576195Sbrian	switch (sc->model) {
27676195Sbrian	case PCXEVE:
27776195Sbrian		outb(sc->wport, 0xff);		/* window 7 */
27876195Sbrian		ptr = sc->vmem + (BIOSCODE & 0x1fff);
27976195Sbrian
28076195Sbrian		if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
28176195Sbrian			device_printf(sc->dev, "BIOS upload failed\n");
28276195Sbrian			return (EIO);
28376195Sbrian		}
28476195Sbrian
28576195Sbrian		outb(sc->port, FEPCLR);
28676195Sbrian		break;
28776195Sbrian
28876195Sbrian	case PCXE:
28976195Sbrian	case PCXI:
29076195Sbrian	case PCCX:
29176195Sbrian		ptr = sc->setwin(sc, BIOSCODE + ((0xf000 - sc->mem_seg) << 4));
29276195Sbrian		if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
29376195Sbrian			device_printf(sc->dev, "BIOS upload failed\n");
29476195Sbrian			return (EIO);
29576195Sbrian		}
29676195Sbrian		break;
29776195Sbrian
29876195Sbrian	case PCXEM:
29976195Sbrian	case PCIEPCX:
30076195Sbrian	case PCIXR:
30194323Sbrian		if (sc->pcibus)
30276195Sbrian			PCIPORT = FEPRST;
30376195Sbrian		else
30476195Sbrian			outb(sc->port, FEPRST | FEPMEM);
30576195Sbrian
30694323Sbrian		for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) &
30776195Sbrian		    FEPMASK) != FEPRST; i++) {
30876195Sbrian			if (i > 1000) {
30994323Sbrian				log(LOG_ERR, "digi%d: %s init reset failed\n",
31094323Sbrian				    sc->res.unit, sc->name);
31176195Sbrian				return (EIO);
31276195Sbrian			}
31376195Sbrian			tsleep(sc, PUSER | PCATCH, "digiinit0", 1);
31476195Sbrian		}
31576195Sbrian		DLOG(DIGIDB_INIT, (sc->dev, "Got init reset after %d us\n", i));
31676195Sbrian
31776195Sbrian		/* Now upload the BIOS */
31876195Sbrian		cnt = (sc->bios.size < sc->win_size - BIOSOFFSET) ?
31976195Sbrian		    sc->bios.size : sc->win_size - BIOSOFFSET;
32076195Sbrian
32176195Sbrian		ptr = sc->setwin(sc, BIOSOFFSET);
32276195Sbrian		if (!digi_bcopy(sc->bios.data, ptr, cnt)) {
32376195Sbrian			device_printf(sc->dev, "BIOS upload (1) failed\n");
32476195Sbrian			return (EIO);
32576195Sbrian		}
32676195Sbrian
32776195Sbrian		if (cnt != sc->bios.size) {
32876195Sbrian			/* and the second part */
32976195Sbrian			ptr = sc->setwin(sc, sc->win_size);
33076195Sbrian			if (!digi_bcopy(sc->bios.data + cnt, ptr,
33176195Sbrian			    sc->bios.size - cnt)) {
33276195Sbrian				device_printf(sc->dev, "BIOS upload failed\n");
33376195Sbrian				return (EIO);
33476195Sbrian			}
33576195Sbrian		}
33676195Sbrian
33776195Sbrian		ptr = sc->setwin(sc, 0);
33876195Sbrian		vW(ptr + 0) = 0x0401;
33976195Sbrian		vW(ptr + 2) = 0x0bf0;
34076195Sbrian		vW(ptr + 4) = 0x0000;
34176195Sbrian		vW(ptr + 6) = 0x0000;
34276195Sbrian
34376195Sbrian		break;
34476195Sbrian	}
34576195Sbrian
34676195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "BIOS uploaded\n"));
34776195Sbrian
34876195Sbrian	ptr = sc->setwin(sc, MISCGLOBAL);
34976195Sbrian	W(ptr) = 0;
35076195Sbrian
35194323Sbrian	if (sc->pcibus) {
35276195Sbrian		PCIPORT = FEPCLR;
35376195Sbrian		resp = FEPRST;
35476195Sbrian	} else if (sc->model == PCXEVE) {
35576195Sbrian		outb(sc->port, FEPCLR);
35676195Sbrian		resp = FEPRST;
35776195Sbrian	} else {
35876195Sbrian		outb(sc->port, FEPCLR | FEPMEM);
35976195Sbrian		resp = FEPRST | FEPMEM;
36076195Sbrian	}
36176195Sbrian
36294323Sbrian	for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) & FEPMASK)
36376195Sbrian	    == resp; i++) {
36476195Sbrian		if (i > 1000) {
36576195Sbrian			log(LOG_ERR, "digi%d: BIOS start failed\n",
36676195Sbrian			    sc->res.unit);
36776195Sbrian			return (EIO);
36876195Sbrian		}
36976195Sbrian		tsleep(sc, PUSER | PCATCH, "digibios0", 1);
37076195Sbrian	}
37176195Sbrian
37276195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "BIOS started after %d us\n", i));
37376195Sbrian
37476195Sbrian	for (i = 0; vW(ptr) != *(u_short *)"GD"; i++) {
37576195Sbrian		if (i > 2000) {
37676195Sbrian			log(LOG_ERR, "digi%d: BIOS boot failed "
37776195Sbrian			    "(0x%02x != 0x%02x)\n",
37876195Sbrian			    sc->res.unit, vW(ptr), *(u_short *)"GD");
37976195Sbrian			return (EIO);
38076195Sbrian		}
38176195Sbrian		tsleep(sc, PUSER | PCATCH, "digibios1", 1);
38276195Sbrian	}
38376195Sbrian
38476195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "BIOS booted after %d iterations\n", i));
38576195Sbrian
38676195Sbrian	if (sc->link.data != NULL) {
38776195Sbrian		DLOG(DIGIDB_INIT, (sc->dev, "Loading link data\n"));
38876195Sbrian		ptr = sc->setwin(sc, 0xcd0);
38976195Sbrian		digi_bcopy(sc->link.data, ptr, 21);	/* XXX 21 ? */
39076195Sbrian	}
39176195Sbrian
39276195Sbrian	/* load FEP/OS */
39376195Sbrian
39476195Sbrian	switch (sc->model) {
39576195Sbrian	case PCXE:
39676195Sbrian	case PCXEVE:
39776195Sbrian	case PCXI:
39876195Sbrian		ptr = sc->setwin(sc, sc->model == PCXI ? 0x2000 : 0x0);
39976195Sbrian		digi_bcopy(sc->fep.data, ptr, sc->fep.size);
40076195Sbrian
40176195Sbrian		/* A BIOS request to move our data to 0x2000 */
40276195Sbrian		ptr = sc->setwin(sc, MBOX);
40376195Sbrian		vW(ptr + 0) = 2;
40476195Sbrian		vW(ptr + 2) = sc->mem_seg + FEPCODESEG;
40576195Sbrian		vW(ptr + 4) = 0;
40676195Sbrian		vW(ptr + 6) = FEPCODESEG;
40776195Sbrian		vW(ptr + 8) = 0;
40876195Sbrian		vW(ptr + 10) = sc->fep.size;
40976195Sbrian
41076195Sbrian		/* Run the BIOS request */
41176195Sbrian		outb(sc->port, FEPREQ | FEPMEM);
41276195Sbrian		outb(sc->port, FEPCLR | FEPMEM);
41376195Sbrian
41476195Sbrian		for (i = 0; W(ptr); i++) {
41576195Sbrian			if (i > 10) {
41676195Sbrian				log(LOG_ERR, "digi%d: FEP/OS move failed\n",
41776195Sbrian				    sc->res.unit);
41876195Sbrian				sc->hidewin(sc);
41976195Sbrian				return (EIO);
42076195Sbrian			}
42176195Sbrian			tsleep(sc, PUSER | PCATCH, "digifep0", 1);
42276195Sbrian		}
42376195Sbrian		DLOG(DIGIDB_INIT,
42476195Sbrian		    (sc->dev, "FEP/OS moved after %d iterations\n", i));
42576195Sbrian
42676195Sbrian		/* Clear the confirm word */
42776195Sbrian		ptr = sc->setwin(sc, FEPSTAT);
42876195Sbrian		vW(ptr + 0) = 0;
42976195Sbrian
43076195Sbrian		/* A BIOS request to execute the FEP/OS */
43176195Sbrian		ptr = sc->setwin(sc, MBOX);
43276195Sbrian		vW(ptr + 0) = 0x01;
43376195Sbrian		vW(ptr + 2) = FEPCODESEG;
43476195Sbrian		vW(ptr + 4) = 0x04;
43576195Sbrian
43676195Sbrian		/* Run the BIOS request */
43776195Sbrian		outb(sc->port, FEPREQ);
43876195Sbrian		outb(sc->port, FEPCLR);
43976195Sbrian
44076195Sbrian		ptr = sc->setwin(sc, FEPSTAT);
44176195Sbrian
44276195Sbrian		break;
44376195Sbrian
44476195Sbrian	case PCXEM:
44576195Sbrian	case PCIEPCX:
44676195Sbrian	case PCIXR:
44776195Sbrian		DLOG(DIGIDB_INIT, (sc->dev, "Loading FEP/OS\n"));
44876195Sbrian
44976195Sbrian		cnt = (sc->fep.size < sc->win_size - BIOSOFFSET) ?
45076195Sbrian		    sc->fep.size : sc->win_size - BIOSOFFSET;
45176195Sbrian
45276195Sbrian		ptr = sc->setwin(sc, BIOSOFFSET);
45376195Sbrian		digi_bcopy(sc->fep.data, ptr, cnt);
45476195Sbrian
45576195Sbrian		if (cnt != sc->fep.size) {
45676195Sbrian			ptr = sc->setwin(sc, BIOSOFFSET + cnt);
45776195Sbrian			digi_bcopy(sc->fep.data + cnt, ptr,
45876195Sbrian			    sc->fep.size - cnt);
45976195Sbrian		}
46076195Sbrian
46176195Sbrian		DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS loaded\n"));
46276195Sbrian
46376195Sbrian		ptr = sc->setwin(sc, 0xc30);
46476195Sbrian		W(ptr + 4) = 0x1004;
46576195Sbrian		W(ptr + 6) = 0xbfc0;
46676195Sbrian		W(ptr + 0) = 0x03;
46776195Sbrian		W(ptr + 2) = 0x00;
46876195Sbrian
46976195Sbrian		/* Clear the confirm word */
47076195Sbrian		ptr = sc->setwin(sc, FEPSTAT);
47176195Sbrian		W(ptr + 0) = 0;
47276195Sbrian
47376195Sbrian		if (sc->port)
47476195Sbrian			outb(sc->port, 0);		/* XXX necessary ? */
47576195Sbrian
47676195Sbrian		break;
47776195Sbrian
47876195Sbrian	case PCCX:
47976195Sbrian		ptr = sc->setwin(sc, 0xd000);
48076195Sbrian		digi_bcopy(sc->fep.data, ptr, sc->fep.size);
48176195Sbrian
48276195Sbrian		/* A BIOS request to execute the FEP/OS */
48376195Sbrian		ptr = sc->setwin(sc, 0xc40);
48476195Sbrian		W(ptr + 0) = 1;
48576195Sbrian		W(ptr + 2) = FEPCODE >> 4;
48676195Sbrian		W(ptr + 4) = 4;
48776195Sbrian
48876195Sbrian		/* Clear the confirm word */
48976195Sbrian		ptr = sc->setwin(sc, FEPSTAT);
49076195Sbrian		W(ptr + 0) = 0;
49176195Sbrian
49276195Sbrian		/* Run the BIOS request */
49376195Sbrian		outb(sc->port, FEPREQ | FEPMEM); /* send interrupt to BIOS */
49476195Sbrian		outb(sc->port, FEPCLR | FEPMEM);
49576195Sbrian		break;
49676195Sbrian	}
49776195Sbrian
49876195Sbrian	/* Now wait 'till the FEP/OS has booted */
49976195Sbrian	for (i = 0; vW(ptr) != *(u_short *)"OS"; i++) {
50076195Sbrian		if (i > 2000) {
50176195Sbrian			log(LOG_ERR, "digi%d: FEP/OS start failed "
50276195Sbrian			    "(0x%02x != 0x%02x)\n",
50376195Sbrian			    sc->res.unit, vW(ptr), *(u_short *)"OS");
50476195Sbrian			sc->hidewin(sc);
50576195Sbrian			return (EIO);
50676195Sbrian		}
50776195Sbrian		tsleep(sc, PUSER | PCATCH, "digifep1", 1);
50876195Sbrian	}
50976195Sbrian
51076195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS started after %d iterations\n", i));
51176195Sbrian
51276195Sbrian	if (sc->model >= PCXEM) {
51376195Sbrian		ptr = sc->setwin(sc, 0xe04);
51476195Sbrian		vW(ptr) = 2;
51576195Sbrian		ptr = sc->setwin(sc, 0xc02);
51676195Sbrian		sc->numports = vW(ptr);
51776195Sbrian	} else {
51876195Sbrian		ptr = sc->setwin(sc, 0xc22);
51976195Sbrian		sc->numports = vW(ptr);
52076195Sbrian	}
52176195Sbrian
52276195Sbrian	if (sc->numports == 0) {
52376195Sbrian		device_printf(sc->dev, "%s, 0 ports found\n", sc->name);
52476195Sbrian		sc->hidewin(sc);
52576195Sbrian		return (0);
52676195Sbrian	}
52776195Sbrian
52876195Sbrian	if (sc->numports > 256) {
52976195Sbrian		/* Our minor numbering scheme is broken for more than 256 */
53076195Sbrian		device_printf(sc->dev, "%s, 256 ports (%d ports found)\n",
53176195Sbrian		    sc->name, sc->numports);
53276195Sbrian		sc->numports = 256;
53376195Sbrian	} else
53476195Sbrian		device_printf(sc->dev, "%s, %d ports found\n", sc->name,
53576195Sbrian		    sc->numports);
53676195Sbrian
53776195Sbrian	if (sc->ports)
53876195Sbrian		free(sc->ports, M_TTYS);
53977004Sbrian	sc->ports = malloc(sizeof(struct digi_p) * sc->numports,
54087503Sarr	    M_TTYS, M_WAITOK | M_ZERO);
54176195Sbrian
54276195Sbrian	if (sc->ttys)
54376195Sbrian		free(sc->ttys, M_TTYS);
54477004Sbrian	sc->ttys = malloc(sizeof(struct tty) * sc->numports,
54587503Sarr	    M_TTYS, M_WAITOK | M_ZERO);
54676195Sbrian
54776195Sbrian	/*
54876195Sbrian	 * XXX Should read port 0xc90 for an array of 2byte values, 1 per
54976195Sbrian	 * port.  If the value is 0, the port is broken....
55076195Sbrian	 */
55176195Sbrian
55276195Sbrian	ptr = sc->setwin(sc, 0);
55376195Sbrian
55476195Sbrian	/* We should now init per-port structures */
55576195Sbrian	bc = (volatile struct board_chan *)(ptr + CHANSTRUCT);
55676195Sbrian	sc->gdata = (volatile struct global_data *)(ptr + FEP_GLOBAL);
55776195Sbrian
55876195Sbrian	sc->memcmd = ptr + sc->gdata->cstart;
55976195Sbrian	sc->memevent = ptr + sc->gdata->istart;
56076195Sbrian
56176195Sbrian	for (i = 0; i < sc->numports; i++, bc++) {
56276195Sbrian		port = sc->ports + i;
56376195Sbrian		port->pnum = i;
56476195Sbrian		port->sc = sc;
56576195Sbrian		port->status = ENABLED;
56676195Sbrian		port->tp = sc->ttys + i;
56776195Sbrian		port->bc = bc;
56876195Sbrian
56976195Sbrian		if (sc->model == PCXEVE) {
57076195Sbrian			port->txbuf = ptr +
57176195Sbrian			    (((bc->tseg - sc->mem_seg) << 4) & 0x1fff);
57276195Sbrian			port->rxbuf = ptr +
57376195Sbrian			    (((bc->rseg - sc->mem_seg) << 4) & 0x1fff);
57476195Sbrian			port->txwin = FEPWIN | ((bc->tseg - sc->mem_seg) >> 9);
57576195Sbrian			port->rxwin = FEPWIN | ((bc->rseg - sc->mem_seg) >> 9);
57676195Sbrian		} else if (sc->model == PCXI || sc->model == PCXE) {
57776195Sbrian			port->txbuf = ptr + ((bc->tseg - sc->mem_seg) << 4);
57876195Sbrian			port->rxbuf = ptr + ((bc->rseg - sc->mem_seg) << 4);
57976195Sbrian			port->txwin = port->rxwin = 0;
58076195Sbrian		} else {
58176195Sbrian			port->txbuf = ptr +
58276195Sbrian			    (((bc->tseg - sc->mem_seg) << 4) % sc->win_size);
58376195Sbrian			port->rxbuf = ptr +
58476195Sbrian			    (((bc->rseg - sc->mem_seg) << 4) % sc->win_size);
58576195Sbrian			port->txwin = FEPWIN |
58676195Sbrian			    (((bc->tseg - sc->mem_seg) << 4) / sc->win_size);
58776195Sbrian			port->rxwin = FEPWIN |
58876195Sbrian			    (((bc->rseg - sc->mem_seg) << 4) / sc->win_size);
58976195Sbrian		}
59076195Sbrian		port->txbufsize = bc->tmax + 1;
59176195Sbrian		port->rxbufsize = bc->rmax + 1;
59276195Sbrian
59376195Sbrian		lowwater = port->txbufsize >> 2;
59476195Sbrian		if (lowwater > 1024)
59576195Sbrian			lowwater = 1024;
59676195Sbrian		sc->setwin(sc, 0);
59776195Sbrian		fepcmd_w(port, STXLWATER, lowwater, 10);
59876195Sbrian		fepcmd_w(port, SRXLWATER, port->rxbufsize >> 2, 10);
59976195Sbrian		fepcmd_w(port, SRXHWATER, (3 * port->rxbufsize) >> 2, 10);
60076195Sbrian
60176195Sbrian		bc->edelay = 100;
60276195Sbrian		port->dtr_wait = 3 * hz;
60376195Sbrian
60476195Sbrian		/*
60576195Sbrian		 * We don't use all the flags from <sys/ttydefaults.h> since
60676195Sbrian		 * they are only relevant for logins.  It's important to have
60776195Sbrian		 * echo off initially so that the line doesn't start blathering
60876195Sbrian		 * before the echo flag can be turned off.
60976195Sbrian		 */
61076195Sbrian		port->it_in.c_iflag = 0;
61176195Sbrian		port->it_in.c_oflag = 0;
61276195Sbrian		port->it_in.c_cflag = TTYDEF_CFLAG;
61376195Sbrian		port->it_in.c_lflag = 0;
61476195Sbrian		termioschars(&port->it_in);
61576195Sbrian		port->it_in.c_ispeed = port->it_in.c_ospeed = digidefaultrate;
61676195Sbrian		port->it_out = port->it_in;
61776195Sbrian		port->send_ring = 1;	/* Default action on signal RI */
61876195Sbrian
61976195Sbrian		port->dev[0] = make_dev(&digi_sw, (sc->res.unit << 16) + i,
62076195Sbrian		    UID_ROOT, GID_WHEEL, 0600, "ttyD%d.%d", sc->res.unit, i);
62176195Sbrian		port->dev[1] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
62276195Sbrian		    CONTROL_INIT_STATE, UID_ROOT, GID_WHEEL,
62376195Sbrian		    0600, "ttyiD%d.%d", sc->res.unit, i);
62476195Sbrian		port->dev[2] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
62576195Sbrian		    CONTROL_LOCK_STATE, UID_ROOT, GID_WHEEL,
62676195Sbrian		    0600, "ttylD%d.%d", sc->res.unit, i);
62776195Sbrian		port->dev[3] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
62876195Sbrian		    CALLOUT_MASK, UID_UUCP, GID_DIALER,
62976195Sbrian		    0660, "cuaD%d.%d", sc->res.unit, i);
63076195Sbrian		port->dev[4] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
63176195Sbrian		    CALLOUT_MASK | CONTROL_INIT_STATE, UID_UUCP, GID_DIALER,
63276195Sbrian		    0660, "cuaiD%d.%d", sc->res.unit, i);
63376195Sbrian		port->dev[5] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
63476195Sbrian		    CALLOUT_MASK | CONTROL_LOCK_STATE, UID_UUCP, GID_DIALER,
63576195Sbrian		    0660, "cualD%d.%d", sc->res.unit, i);
63676195Sbrian	}
63776195Sbrian
63876195Sbrian	sc->hidewin(sc);
63976195Sbrian	sc->inttest = timeout(digi_int_test, sc, hz);
64076195Sbrian	/* fepcmd_w(&sc->ports[0], 0xff, 0, 0); */
64176195Sbrian	sc->status = DIGI_STATUS_ENABLED;
64276195Sbrian
64376195Sbrian	return (0);
64476195Sbrian}
64576195Sbrian
64676195Sbrianstatic int
64776195Sbriandigimctl(struct digi_p *port, int bits, int how)
64876195Sbrian{
64976195Sbrian	int mstat;
65076195Sbrian
65176195Sbrian	if (how == DMGET) {
65276195Sbrian		port->sc->setwin(port->sc, 0);
65376195Sbrian		mstat = port->bc->mstat;
65476195Sbrian		port->sc->hidewin(port->sc);
65576195Sbrian		bits = TIOCM_LE;
65676195Sbrian		if (mstat & port->sc->csigs->rts)
65776195Sbrian			bits |= TIOCM_RTS;
65878496Sbrian		if (mstat & port->cd)
65976195Sbrian			bits |= TIOCM_CD;
66078496Sbrian		if (mstat & port->dsr)
66176195Sbrian			bits |= TIOCM_DSR;
66276195Sbrian		if (mstat & port->sc->csigs->cts)
66376195Sbrian			bits |= TIOCM_CTS;
66476195Sbrian		if (mstat & port->sc->csigs->ri)
66576195Sbrian			bits |= TIOCM_RI;
66676195Sbrian		if (mstat & port->sc->csigs->dtr)
66776195Sbrian			bits |= TIOCM_DTR;
66876195Sbrian		return (bits);
66976195Sbrian	}
67076195Sbrian
67176195Sbrian	/* Only DTR and RTS may be set */
67276195Sbrian	mstat = 0;
67376195Sbrian	if (bits & TIOCM_DTR)
67476195Sbrian		mstat |= port->sc->csigs->dtr;
67576195Sbrian	if (bits & TIOCM_RTS)
67676195Sbrian		mstat |= port->sc->csigs->rts;
67776195Sbrian
67876195Sbrian	switch (how) {
67976195Sbrian	case DMSET:
68076195Sbrian		fepcmd_b(port, SETMODEM, mstat, ~mstat, 0);
68176195Sbrian		break;
68276195Sbrian	case DMBIS:
68376195Sbrian		fepcmd_b(port, SETMODEM, mstat, 0, 0);
68476195Sbrian		break;
68576195Sbrian	case DMBIC:
68676195Sbrian		fepcmd_b(port, SETMODEM, 0, mstat, 0);
68776195Sbrian		break;
68876195Sbrian	}
68976195Sbrian
69076195Sbrian	return (0);
69176195Sbrian}
69276195Sbrian
69376195Sbrianstatic void
69476195Sbriandigi_disc_optim(struct tty *tp, struct termios *t, struct digi_p *port)
69576195Sbrian{
69676195Sbrian	if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP)) &&
69776195Sbrian	    (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) &&
69876195Sbrian	    (!(t->c_iflag & PARMRK) ||
69976195Sbrian	    (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) &&
70076195Sbrian	    !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) &&
70176195Sbrian	    linesw[tp->t_line].l_rint == ttyinput)
70276195Sbrian		tp->t_state |= TS_CAN_BYPASS_L_RINT;
70376195Sbrian	else
70476195Sbrian		tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
70576195Sbrian}
70676195Sbrian
70776195Sbrianint
70883366Sjuliandigiopen(dev_t dev, int flag, int mode, struct thread *td)
70976195Sbrian{
71076195Sbrian	struct digi_softc *sc;
71176195Sbrian	struct tty *tp;
71276195Sbrian	int unit;
71376195Sbrian	int pnum;
71476195Sbrian	struct digi_p *port;
71576195Sbrian	int s;
71676195Sbrian	int error, mynor;
71776195Sbrian	volatile struct board_chan *bc;
71876195Sbrian
71976195Sbrian	error = 0;
72076195Sbrian	mynor = minor(dev);
72176195Sbrian	unit = MINOR_TO_UNIT(minor(dev));
72276195Sbrian	pnum = MINOR_TO_PORT(minor(dev));
72376195Sbrian
72476195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
72576195Sbrian	if (!sc)
72676195Sbrian		return (ENXIO);
72776195Sbrian
72876195Sbrian	if (sc->status != DIGI_STATUS_ENABLED) {
72976195Sbrian		DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
73076195Sbrian		return (ENXIO);
73176195Sbrian	}
73276195Sbrian	if (pnum >= sc->numports) {
73376195Sbrian		DLOG(DIGIDB_OPEN, (sc->dev, "port%d: Doesn't exist\n", pnum));
73476195Sbrian		return (ENXIO);
73576195Sbrian	}
73676195Sbrian	if (mynor & (CTRL_DEV | CONTROL_MASK)) {
73776195Sbrian		sc->opencnt++;
73876195Sbrian		return (0);
73976195Sbrian	}
74076195Sbrian	port = &sc->ports[pnum];
74176195Sbrian	tp = dev->si_tty = port->tp;
74276195Sbrian	bc = port->bc;
74376195Sbrian
74476195Sbrian	s = spltty();
74576195Sbrian
74676195Sbrianopen_top:
74776195Sbrian	while (port->status & DIGI_DTR_OFF) {
74876195Sbrian		port->wopeners++;
74976195Sbrian		error = tsleep(&port->dtr_wait, TTIPRI | PCATCH, "digidtr", 0);
75076195Sbrian		port->wopeners--;
75176195Sbrian		if (error)
75276195Sbrian			goto out;
75376195Sbrian	}
75476195Sbrian
75576195Sbrian	if (tp->t_state & TS_ISOPEN) {
75676195Sbrian		/*
75776195Sbrian		 * The device is open, so everything has been initialized.
75876195Sbrian		 * Handle conflicts.
75976195Sbrian		 */
76076195Sbrian		if (mynor & CALLOUT_MASK) {
76176195Sbrian			if (!port->active_out) {
76276195Sbrian				error = EBUSY;
76376195Sbrian				DLOG(DIGIDB_OPEN, (sc->dev, "port %d:"
76476195Sbrian				    " BUSY error = %d\n", pnum, error));
76576195Sbrian				goto out;
76676195Sbrian			}
76776195Sbrian		} else if (port->active_out) {
76876195Sbrian			if (flag & O_NONBLOCK) {
76976195Sbrian				error = EBUSY;
77076195Sbrian				DLOG(DIGIDB_OPEN, (sc->dev,
77176195Sbrian				    "port %d: BUSY error = %d\n", pnum, error));
77276195Sbrian				goto out;
77376195Sbrian			}
77476195Sbrian			port->wopeners++;
77576195Sbrian			error = tsleep(&port->active_out, TTIPRI | PCATCH,
77676195Sbrian			    "digibi", 0);
77776195Sbrian			port->wopeners--;
77876195Sbrian			if (error != 0) {
77976195Sbrian				DLOG(DIGIDB_OPEN, (sc->dev,
78076195Sbrian				    "port %d: tsleep(digibi) error = %d\n",
78176195Sbrian				    pnum, error));
78276195Sbrian				goto out;
78376195Sbrian			}
78476195Sbrian			goto open_top;
78576195Sbrian		}
78693593Sjhb		if (tp->t_state & TS_XCLUDE && suser(td) != 0) {
78776195Sbrian			error = EBUSY;
78876195Sbrian			goto out;
78976195Sbrian		}
79076195Sbrian	} else {
79176195Sbrian		/*
79276195Sbrian		 * The device isn't open, so there are no conflicts.
79376195Sbrian		 * Initialize it.  Initialization is done twice in many
79476195Sbrian		 * cases: to preempt sleeping callin opens if we are callout,
79576195Sbrian		 * and to complete a callin open after DCD rises.
79676195Sbrian		 */
79776195Sbrian		tp->t_oproc = digistart;
79876195Sbrian		tp->t_param = digiparam;
79976195Sbrian		tp->t_stop = digistop;
80076195Sbrian		tp->t_dev = dev;
80176195Sbrian		tp->t_termios = (mynor & CALLOUT_MASK) ?
80276195Sbrian		    port->it_out : port->it_in;
80376195Sbrian		sc->setwin(sc, 0);
80476195Sbrian
80576195Sbrian		bc->rout = bc->rin;	/* clear input queue */
80676195Sbrian		bc->idata = 1;
80776195Sbrian		bc->iempty = 1;
80876195Sbrian		bc->ilow = 1;
80978496Sbrian		bc->mint = port->cd | port->sc->csigs->ri;
81076195Sbrian		bc->tin = bc->tout;
81178496Sbrian		if (port->ialtpin) {
81278496Sbrian			port->cd = sc->csigs->dsr;
81378496Sbrian			port->dsr = sc->csigs->cd;
81478496Sbrian		} else {
81578496Sbrian			port->cd = sc->csigs->cd;
81678496Sbrian			port->dsr = sc->csigs->dsr;
81778496Sbrian		}
81876195Sbrian		port->wopeners++;			/* XXX required ? */
81976195Sbrian		error = digiparam(tp, &tp->t_termios);
82076195Sbrian		port->wopeners--;
82176195Sbrian
82276195Sbrian		if (error != 0) {
82376195Sbrian			DLOG(DIGIDB_OPEN, (sc->dev,
82476195Sbrian			    "port %d: cxpparam error = %d\n", pnum, error));
82576195Sbrian			goto out;
82676195Sbrian		}
82776195Sbrian		ttsetwater(tp);
82876195Sbrian
82976195Sbrian		/* handle fake and initial DCD for callout devices */
83076195Sbrian
83178496Sbrian		if (bc->mstat & port->cd || mynor & CALLOUT_MASK)
83276195Sbrian			linesw[tp->t_line].l_modem(tp, 1);
83376195Sbrian	}
83476195Sbrian
83576195Sbrian	/* Wait for DCD if necessary */
83676195Sbrian	if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) &&
83776195Sbrian	    !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
83876195Sbrian		port->wopeners++;
83976195Sbrian		error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "digidcd", 0);
84076195Sbrian		port->wopeners--;
84176195Sbrian		if (error != 0) {
84276195Sbrian			DLOG(DIGIDB_OPEN, (sc->dev,
84376195Sbrian			    "port %d: tsleep(digidcd) error = %d\n",
84476195Sbrian			    pnum, error));
84576195Sbrian			goto out;
84676195Sbrian		}
84776195Sbrian		goto open_top;
84876195Sbrian	}
84976195Sbrian	error = linesw[tp->t_line].l_open(dev, tp);
85076195Sbrian	DLOG(DIGIDB_OPEN, (sc->dev, "port %d: l_open error = %d\n",
85176195Sbrian	    pnum, error));
85276195Sbrian
85376195Sbrian	digi_disc_optim(tp, &tp->t_termios, port);
85476195Sbrian
85576195Sbrian	if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
85676195Sbrian		port->active_out = TRUE;
85776195Sbrian
85876195Sbrian	if (tp->t_state & TS_ISOPEN)
85976195Sbrian		sc->opencnt++;
86076195Sbrianout:
86176195Sbrian	splx(s);
86276195Sbrian
86376195Sbrian	if (!(tp->t_state & TS_ISOPEN))
86476195Sbrian		digihardclose(port);
86576195Sbrian
86676195Sbrian	DLOG(DIGIDB_OPEN, (sc->dev, "port %d: open() returns %d\n",
86776195Sbrian	    pnum, error));
86876195Sbrian
86976195Sbrian	return (error);
87076195Sbrian}
87176195Sbrian
87276195Sbrianint
87383366Sjuliandigiclose(dev_t dev, int flag, int mode, struct thread *td)
87476195Sbrian{
87576195Sbrian	int mynor;
87676195Sbrian	struct tty *tp;
87776195Sbrian	int unit, pnum;
87876195Sbrian	struct digi_softc *sc;
87976195Sbrian	struct digi_p *port;
88076195Sbrian	int s;
88176195Sbrian
88276195Sbrian	mynor = minor(dev);
88376195Sbrian	unit = MINOR_TO_UNIT(mynor);
88476195Sbrian	pnum = MINOR_TO_PORT(mynor);
88576195Sbrian
88676195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
88776195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
88876195Sbrian
88976195Sbrian	if (mynor & (CTRL_DEV | CONTROL_MASK)) {
89076195Sbrian		sc->opencnt--;
89176195Sbrian		return (0);
89276195Sbrian	}
89376195Sbrian
89476195Sbrian	port = sc->ports + pnum;
89576195Sbrian	tp = port->tp;
89676195Sbrian
89776195Sbrian	DLOG(DIGIDB_CLOSE, (sc->dev, "port %d: closing\n", pnum));
89876195Sbrian
89976195Sbrian	s = spltty();
90076195Sbrian	linesw[tp->t_line].l_close(tp, flag);
90176195Sbrian	digi_disc_optim(tp, &tp->t_termios, port);
90276195Sbrian	digistop(tp, FREAD | FWRITE);
90376195Sbrian	digihardclose(port);
90476195Sbrian	ttyclose(tp);
90576195Sbrian	if (--sc->opencnt == 0)
90676195Sbrian		splx(s);
90776195Sbrian	return (0);
90876195Sbrian}
90976195Sbrian
91076195Sbrianstatic void
91176195Sbriandigidtrwakeup(void *chan)
91276195Sbrian{
91376195Sbrian	struct digi_p *port = chan;
91476195Sbrian
91576195Sbrian	port->status &= ~DIGI_DTR_OFF;
91676195Sbrian	wakeup(&port->dtr_wait);
91776195Sbrian	port->wopeners--;
91876195Sbrian}
91976195Sbrian
92076195Sbrianstatic void
92176195Sbriandigihardclose(struct digi_p *port)
92276195Sbrian{
92376195Sbrian	volatile struct board_chan *bc;
92476195Sbrian	int s;
92576195Sbrian
92676195Sbrian	bc = port->bc;
92776195Sbrian
92876195Sbrian	s = spltty();
92976195Sbrian	port->sc->setwin(port->sc, 0);
93076195Sbrian	bc->idata = 0;
93176195Sbrian	bc->iempty = 0;
93276195Sbrian	bc->ilow = 0;
93376195Sbrian	bc->mint = 0;
93476195Sbrian	if ((port->tp->t_cflag & HUPCL) ||
93578496Sbrian	    (!port->active_out && !(bc->mstat & port->cd) &&
93676195Sbrian	    !(port->it_in.c_cflag & CLOCAL)) ||
93776195Sbrian	    !(port->tp->t_state & TS_ISOPEN)) {
93876195Sbrian		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
93976195Sbrian		if (port->dtr_wait != 0) {
94076195Sbrian			/* Schedule a wakeup of any callin devices */
94176195Sbrian			port->wopeners++;
94276195Sbrian			timeout(&digidtrwakeup, port, port->dtr_wait);
94376195Sbrian			port->status |= DIGI_DTR_OFF;
94476195Sbrian		}
94576195Sbrian	}
94676195Sbrian	port->active_out = FALSE;
94776195Sbrian	wakeup(&port->active_out);
94876195Sbrian	wakeup(TSA_CARR_ON(port->tp));
94976195Sbrian	splx(s);
95076195Sbrian}
95176195Sbrian
95276195Sbrianint
95376195Sbriandigiread(dev_t dev, struct uio *uio, int flag)
95476195Sbrian{
95576195Sbrian	int mynor;
95676195Sbrian	struct tty *tp;
95776195Sbrian	int error, unit, pnum;
95876195Sbrian	struct digi_softc *sc;
95976195Sbrian
96076195Sbrian	mynor = minor(dev);
96176195Sbrian	if (mynor & CONTROL_MASK)
96276195Sbrian		return (ENODEV);
96376195Sbrian
96476195Sbrian	unit = MINOR_TO_UNIT(mynor);
96576195Sbrian	pnum = MINOR_TO_PORT(mynor);
96676195Sbrian
96776195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
96876195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
96976195Sbrian	tp = &sc->ttys[pnum];
97076195Sbrian
97176195Sbrian	error = linesw[tp->t_line].l_read(tp, uio, flag);
97276195Sbrian	DLOG(DIGIDB_READ, (sc->dev, "port %d: read() returns %d\n",
97376195Sbrian	    pnum, error));
97476195Sbrian
97576195Sbrian	return (error);
97676195Sbrian}
97776195Sbrian
97876195Sbrianint
97976195Sbriandigiwrite(dev_t dev, struct uio *uio, int flag)
98076195Sbrian{
98176195Sbrian	int mynor;
98276195Sbrian	struct tty *tp;
98376195Sbrian	int error, unit, pnum;
98476195Sbrian	struct digi_softc *sc;
98576195Sbrian
98676195Sbrian	mynor = minor(dev);
98776195Sbrian	if (mynor & CONTROL_MASK)
98876195Sbrian		return (ENODEV);
98976195Sbrian
99076195Sbrian	unit = MINOR_TO_UNIT(mynor);
99176195Sbrian	pnum = MINOR_TO_PORT(mynor);
99276195Sbrian
99376195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
99476195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
99576195Sbrian	tp = &sc->ttys[pnum];
99676195Sbrian
99776195Sbrian	error = linesw[tp->t_line].l_write(tp, uio, flag);
99876195Sbrian	DLOG(DIGIDB_WRITE, (sc->dev, "port %d: write() returns %d\n",
99976195Sbrian	    pnum, error));
100076195Sbrian
100176195Sbrian	return (error);
100276195Sbrian}
100376195Sbrian
100476195Sbrian/*
100576195Sbrian * Load module "digi_<mod>.ko" and look for a symbol called digi_mod_<mod>.
100676195Sbrian *
100776195Sbrian * Populate sc->bios, sc->fep, and sc->link from this data.
100876195Sbrian *
100976195Sbrian * sc->fep.data, sc->bios.data and sc->link.data are malloc()d according
101076195Sbrian * to their respective sizes.
101176195Sbrian *
101276195Sbrian * The module is unloaded when we're done.
101376195Sbrian */
101476195Sbrianstatic int
101576195Sbriandigi_loadmoduledata(struct digi_softc *sc)
101676195Sbrian{
101776195Sbrian	struct digi_mod *digi_mod;
101876195Sbrian	linker_file_t lf;
101976195Sbrian	char *modfile, *sym;
102076195Sbrian	caddr_t symptr;
102178414Sbrian	int modlen, res;
102276195Sbrian
102376195Sbrian	KASSERT(sc->bios.data == NULL, ("Uninitialised BIOS variable"));
102476195Sbrian	KASSERT(sc->fep.data == NULL, ("Uninitialised FEP variable"));
102576195Sbrian	KASSERT(sc->link.data == NULL, ("Uninitialised LINK variable"));
102676195Sbrian	KASSERT(sc->module != NULL, ("Uninitialised module name"));
102776195Sbrian
102878414Sbrian	modlen = strlen(sc->module);
102978414Sbrian	modfile = malloc(modlen + 6, M_TEMP, M_WAITOK);
103078414Sbrian	snprintf(modfile, modlen + 6, "digi_%s", sc->module);
103194321Sbrian	if ((res = linker_reference_module(modfile, NULL, &lf)) != 0) {
103294321Sbrian		if (res == ENOENT && rootdev == NODEV)
103394321Sbrian			printf("%s: Failed to autoload module: No filesystem\n",
103494321Sbrian			    modfile);
103594321Sbrian		else
103694321Sbrian			printf("%s: Failed %d to autoload module\n", modfile,
103794321Sbrian			    res);
103894321Sbrian	}
103976195Sbrian	free(modfile, M_TEMP);
104076195Sbrian	if (res != 0)
104176195Sbrian		return (res);
104276195Sbrian
104378414Sbrian	sym = malloc(modlen + 10, M_TEMP, M_WAITOK);
104478414Sbrian	snprintf(sym, modlen + 10, "digi_mod_%s", sc->module);
104576195Sbrian	if ((symptr = linker_file_lookup_symbol(lf, sym, 0)) == NULL)
104676195Sbrian		printf("digi_%s.ko: Symbol `%s' not found\n", sc->module, sym);
104776195Sbrian	free(sym, M_TEMP);
104876195Sbrian
104976195Sbrian	digi_mod = (struct digi_mod *)symptr;
105076195Sbrian	if (digi_mod->dm_version != DIGI_MOD_VERSION) {
105176195Sbrian		printf("digi_%s.ko: Invalid version %d (need %d)\n",
105276195Sbrian		    sc->module, digi_mod->dm_version, DIGI_MOD_VERSION);
105376195Sbrian		linker_file_unload(lf);
105476195Sbrian		return (EINVAL);
105576195Sbrian	}
105676195Sbrian
105776195Sbrian	sc->bios.size = digi_mod->dm_bios.size;
105876195Sbrian	if (sc->bios.size != 0 && digi_mod->dm_bios.data != NULL) {
105987503Sarr		sc->bios.data = malloc(sc->bios.size, M_TTYS, M_WAITOK);
106076195Sbrian		bcopy(digi_mod->dm_bios.data, sc->bios.data, sc->bios.size);
106176195Sbrian	}
106276195Sbrian
106376195Sbrian	sc->fep.size = digi_mod->dm_fep.size;
106476195Sbrian	if (sc->fep.size != 0 && digi_mod->dm_fep.data != NULL) {
106587503Sarr		sc->fep.data = malloc(sc->fep.size, M_TTYS, M_WAITOK);
106676195Sbrian		bcopy(digi_mod->dm_fep.data, sc->fep.data, sc->fep.size);
106776195Sbrian	}
106876195Sbrian
106976195Sbrian	sc->link.size = digi_mod->dm_link.size;
107076195Sbrian	if (sc->link.size != 0 && digi_mod->dm_link.data != NULL) {
107187503Sarr		sc->link.data = malloc(sc->link.size, M_TTYS, M_WAITOK);
107276195Sbrian		bcopy(digi_mod->dm_link.data, sc->link.data, sc->link.size);
107376195Sbrian	}
107476195Sbrian
107576195Sbrian	linker_file_unload(lf);
107676195Sbrian
107776195Sbrian	return (0);
107876195Sbrian}
107976195Sbrian
108076195Sbrianstatic int
108183366Sjuliandigiioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
108276195Sbrian{
108376195Sbrian	int unit, pnum, mynor, error, s;
108476195Sbrian	struct digi_softc *sc;
108576195Sbrian	struct digi_p *port;
108676195Sbrian	struct tty *tp;
108776195Sbrian#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
108876195Sbrian	int oldcmd;
108976195Sbrian	struct termios term;
109076195Sbrian#endif
109176195Sbrian
109276195Sbrian	mynor = minor(dev);
109376195Sbrian	unit = MINOR_TO_UNIT(mynor);
109476195Sbrian	pnum = MINOR_TO_PORT(mynor);
109576195Sbrian
109676195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
109776195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiioctl\n", unit));
109876195Sbrian
109976195Sbrian	if (sc->status == DIGI_STATUS_DISABLED)
110076195Sbrian		return (ENXIO);
110176195Sbrian
110276195Sbrian	if (mynor & CTRL_DEV) {
110376195Sbrian		switch (cmd) {
110476195Sbrian		case DIGIIO_DEBUG:
110576195Sbrian#ifdef DEBUG
110676195Sbrian			digi_debug = *(int *)data;
110776195Sbrian			return (0);
110876195Sbrian#else
110976195Sbrian			device_printf(sc->dev, "DEBUG not defined\n");
111076195Sbrian			return (ENXIO);
111176195Sbrian#endif
111276195Sbrian		case DIGIIO_REINIT:
111376195Sbrian			digi_loadmoduledata(sc);
111476195Sbrian			error = digi_init(sc);
111576195Sbrian			digi_freemoduledata(sc);
111676195Sbrian			return (error);
111776195Sbrian
111876195Sbrian		case DIGIIO_MODEL:
111976705Sbrian			*(enum digi_model *)data = sc->model;
112076195Sbrian			return (0);
112176195Sbrian
112276195Sbrian		case DIGIIO_IDENT:
112376195Sbrian			return (copyout(sc->name, *(char **)data,
112476195Sbrian			    strlen(sc->name) + 1));
112576195Sbrian		}
112676195Sbrian	}
112776195Sbrian
112876195Sbrian	if (pnum >= sc->numports)
112976195Sbrian		return (ENXIO);
113076195Sbrian
113176195Sbrian	port = sc->ports + pnum;
113276195Sbrian	if (!(port->status & ENABLED))
113376195Sbrian		return (ENXIO);
113476195Sbrian
113576195Sbrian	tp = port->tp;
113676195Sbrian
113776195Sbrian	if (mynor & CONTROL_MASK) {
113876195Sbrian		struct termios *ct;
113976195Sbrian
114076195Sbrian		switch (mynor & CONTROL_MASK) {
114176195Sbrian		case CONTROL_INIT_STATE:
114276195Sbrian			ct = (mynor & CALLOUT_MASK) ?
114376195Sbrian			    &port->it_out : &port->it_in;
114476195Sbrian			break;
114576195Sbrian		case CONTROL_LOCK_STATE:
114676195Sbrian			ct = (mynor & CALLOUT_MASK) ?
114776195Sbrian			    &port->lt_out : &port->lt_in;
114876195Sbrian			break;
114976195Sbrian		default:
115076195Sbrian			return (ENODEV);	/* /dev/nodev */
115176195Sbrian		}
115276195Sbrian
115376195Sbrian		switch (cmd) {
115476195Sbrian		case TIOCSETA:
115593593Sjhb			error = suser(td);
115676195Sbrian			if (error != 0)
115776195Sbrian				return (error);
115876195Sbrian			*ct = *(struct termios *)data;
115978496Sbrian			return (0);
116076195Sbrian
116176195Sbrian		case TIOCGETA:
116276195Sbrian			*(struct termios *)data = *ct;
116378496Sbrian			return (0);
116476195Sbrian
116576195Sbrian		case TIOCGETD:
116676195Sbrian			*(int *)data = TTYDISC;
116776195Sbrian			return (0);
116878496Sbrian
116976195Sbrian		case TIOCGWINSZ:
117076195Sbrian			bzero(data, sizeof(struct winsize));
117176195Sbrian			return (0);
117278496Sbrian
117378496Sbrian		case DIGIIO_GETALTPIN:
117478496Sbrian			switch (mynor & CONTROL_MASK) {
117578496Sbrian			case CONTROL_INIT_STATE:
117678496Sbrian				*(int *)data = port->ialtpin;
117778496Sbrian				break;
117878496Sbrian
117978496Sbrian			case CONTROL_LOCK_STATE:
118078496Sbrian				*(int *)data = port->laltpin;
118178496Sbrian				break;
118278496Sbrian
118378496Sbrian			default:
118478496Sbrian				panic("Confusion when re-testing minor");
118578496Sbrian				return (ENODEV);
118678496Sbrian			}
118778496Sbrian			return (0);
118878496Sbrian
118978496Sbrian		case DIGIIO_SETALTPIN:
119078496Sbrian			switch (mynor & CONTROL_MASK) {
119178496Sbrian			case CONTROL_INIT_STATE:
119278496Sbrian				if (!port->laltpin) {
119378496Sbrian					port->ialtpin = !!*(int *)data;
119478496Sbrian					DLOG(DIGIDB_SET, (sc->dev,
119578496Sbrian					    "port%d: initial ALTPIN %s\n", pnum,
119678496Sbrian					    port->ialtpin ? "set" : "cleared"));
119778496Sbrian				}
119878496Sbrian				break;
119978496Sbrian
120078496Sbrian			case CONTROL_LOCK_STATE:
120178496Sbrian				port->laltpin = !!*(int *)data;
120278496Sbrian				DLOG(DIGIDB_SET, (sc->dev,
120378496Sbrian				    "port%d: ALTPIN %slocked\n",
120478496Sbrian				    pnum, port->laltpin ? "" : "un"));
120578496Sbrian				break;
120678496Sbrian
120778496Sbrian			default:
120878496Sbrian				panic("Confusion when re-testing minor");
120978496Sbrian				return (ENODEV);
121078496Sbrian			}
121178496Sbrian			return (0);
121278496Sbrian
121376195Sbrian		default:
121476195Sbrian			return (ENOTTY);
121576195Sbrian		}
121676195Sbrian	}
121778496Sbrian
121878496Sbrian	switch (cmd) {
121978496Sbrian	case DIGIIO_GETALTPIN:
122078496Sbrian		*(int *)data = !!(port->dsr == sc->csigs->cd);
122178496Sbrian		return (0);
122278496Sbrian
122378496Sbrian	case DIGIIO_SETALTPIN:
122478496Sbrian		if (!port->laltpin) {
122578496Sbrian			if (*(int *)data) {
122678496Sbrian				DLOG(DIGIDB_SET, (sc->dev,
122778496Sbrian				    "port%d: ALTPIN set\n", pnum));
122878496Sbrian				port->cd = sc->csigs->dsr;
122978496Sbrian				port->dsr = sc->csigs->cd;
123078496Sbrian			} else {
123178496Sbrian				DLOG(DIGIDB_SET, (sc->dev,
123278496Sbrian				    "port%d: ALTPIN cleared\n", pnum));
123378496Sbrian				port->cd = sc->csigs->cd;
123478496Sbrian				port->dsr = sc->csigs->dsr;
123578496Sbrian			}
123678496Sbrian		}
123778496Sbrian		return (0);
123878496Sbrian	}
123978496Sbrian
124076195Sbrian	tp = port->tp;
124176195Sbrian#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
124276195Sbrian	term = tp->t_termios;
124376195Sbrian	oldcmd = cmd;
124476195Sbrian	error = ttsetcompat(tp, &cmd, data, &term);
124576195Sbrian	if (error != 0)
124676195Sbrian		return (error);
124776195Sbrian	if (cmd != oldcmd)
124876195Sbrian		data = (caddr_t) & term;
124976195Sbrian#endif
125076195Sbrian	if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
125176195Sbrian		int cc;
125276195Sbrian		struct termios *dt;
125376195Sbrian		struct termios *lt;
125476195Sbrian
125576195Sbrian		dt = (struct termios *)data;
125676195Sbrian		lt = (mynor & CALLOUT_MASK) ? &port->lt_out : &port->lt_in;
125776195Sbrian
125876195Sbrian		dt->c_iflag =
125976195Sbrian		    (tp->t_iflag & lt->c_iflag) | (dt->c_iflag & ~lt->c_iflag);
126076195Sbrian		dt->c_oflag =
126176195Sbrian		    (tp->t_oflag & lt->c_oflag) | (dt->c_oflag & ~lt->c_oflag);
126276195Sbrian		dt->c_cflag =
126376195Sbrian		    (tp->t_cflag & lt->c_cflag) | (dt->c_cflag & ~lt->c_cflag);
126476195Sbrian		dt->c_lflag =
126576195Sbrian		    (tp->t_lflag & lt->c_lflag) | (dt->c_lflag & ~lt->c_lflag);
126676195Sbrian		port->c_iflag = dt->c_iflag & (IXOFF | IXON | IXANY);
126776195Sbrian		dt->c_iflag &= ~(IXOFF | IXON | IXANY);
126876195Sbrian		for (cc = 0; cc < NCCS; ++cc)
126976195Sbrian			if (lt->c_cc[cc] != 0)
127076195Sbrian				dt->c_cc[cc] = tp->t_cc[cc];
127176195Sbrian		if (lt->c_ispeed != 0)
127276195Sbrian			dt->c_ispeed = tp->t_ispeed;
127376195Sbrian		if (lt->c_ospeed != 0)
127476195Sbrian			dt->c_ospeed = tp->t_ospeed;
127576195Sbrian	}
127683366Sjulian	error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, td);
127776195Sbrian	if (error == 0 && cmd == TIOCGETA)
127876195Sbrian		((struct termios *)data)->c_iflag |= port->c_iflag;
127976195Sbrian
128076195Sbrian	if (error >= 0 && error != ENOIOCTL)
128176195Sbrian		return (error);
128276195Sbrian	s = spltty();
128376195Sbrian	error = ttioctl(tp, cmd, data, flag);
128476195Sbrian	if (error == 0 && cmd == TIOCGETA)
128576195Sbrian		((struct termios *)data)->c_iflag |= port->c_iflag;
128676195Sbrian
128776195Sbrian	digi_disc_optim(tp, &tp->t_termios, port);
128876195Sbrian	if (error >= 0 && error != ENOIOCTL) {
128976195Sbrian		splx(s);
129076195Sbrian		return (error);
129176195Sbrian	}
129276195Sbrian	sc->setwin(sc, 0);
129376195Sbrian	switch (cmd) {
129476195Sbrian	case DIGIIO_RING:
129576195Sbrian		port->send_ring = *(u_char *)data;
129676195Sbrian		break;
129776195Sbrian	case TIOCSBRK:
129876195Sbrian		/*
129983861Sbrian		 * now it sends 400 millisecond break because I don't know
130076195Sbrian		 * how to send an infinite break
130176195Sbrian		 */
130283861Sbrian		fepcmd_w(port, SENDBREAK, 400, 10);
130376195Sbrian		break;
130476195Sbrian	case TIOCCBRK:
130576195Sbrian		/* now it's empty */
130676195Sbrian		break;
130776195Sbrian	case TIOCSDTR:
130876195Sbrian		digimctl(port, TIOCM_DTR, DMBIS);
130976195Sbrian		break;
131076195Sbrian	case TIOCCDTR:
131176195Sbrian		digimctl(port, TIOCM_DTR, DMBIC);
131276195Sbrian		break;
131376195Sbrian	case TIOCMSET:
131476195Sbrian		digimctl(port, *(int *)data, DMSET);
131576195Sbrian		break;
131676195Sbrian	case TIOCMBIS:
131776195Sbrian		digimctl(port, *(int *)data, DMBIS);
131876195Sbrian		break;
131976195Sbrian	case TIOCMBIC:
132076195Sbrian		digimctl(port, *(int *)data, DMBIC);
132176195Sbrian		break;
132276195Sbrian	case TIOCMGET:
132376195Sbrian		*(int *)data = digimctl(port, 0, DMGET);
132476195Sbrian		break;
132576195Sbrian	case TIOCMSDTRWAIT:
132693593Sjhb		error = suser(td);
132776195Sbrian		if (error != 0) {
132876195Sbrian			splx(s);
132976195Sbrian			return (error);
133076195Sbrian		}
133176195Sbrian		port->dtr_wait = *(int *)data *hz / 100;
133276195Sbrian
133376195Sbrian		break;
133476195Sbrian	case TIOCMGDTRWAIT:
133576195Sbrian		*(int *)data = port->dtr_wait * 100 / hz;
133676195Sbrian		break;
133776195Sbrian#ifdef DIGI_INTERRUPT
133876195Sbrian	case TIOCTIMESTAMP:
133976195Sbrian		*(struct timeval *)data = sc->intr_timestamp;
134076195Sbrian
134176195Sbrian		break;
134276195Sbrian#endif
134376195Sbrian	default:
134476195Sbrian		splx(s);
134576195Sbrian		return (ENOTTY);
134676195Sbrian	}
134776195Sbrian	splx(s);
134876195Sbrian	return (0);
134976195Sbrian}
135076195Sbrian
135176195Sbrianstatic int
135276195Sbriandigiparam(struct tty *tp, struct termios *t)
135376195Sbrian{
135476195Sbrian	int mynor;
135576195Sbrian	int unit;
135676195Sbrian	int pnum;
135776195Sbrian	struct digi_softc *sc;
135876195Sbrian	struct digi_p *port;
135976195Sbrian	int cflag;
136076195Sbrian	int iflag;
136176195Sbrian	int hflow;
136276195Sbrian	int s;
136376195Sbrian	int window;
136476195Sbrian
136576195Sbrian	mynor = minor(tp->t_dev);
136676195Sbrian	unit = MINOR_TO_UNIT(mynor);
136776195Sbrian	pnum = MINOR_TO_PORT(mynor);
136876195Sbrian
136976195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
137076195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiparam\n", unit));
137176195Sbrian
137276195Sbrian	port = &sc->ports[pnum];
137376195Sbrian
137476195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: setting parameters\n", pnum));
137576195Sbrian
137676195Sbrian	if (t->c_ispeed == 0)
137776195Sbrian		t->c_ispeed = t->c_ospeed;
137876195Sbrian
137976195Sbrian	cflag = ttspeedtab(t->c_ospeed, digispeedtab);
138076195Sbrian
138176195Sbrian	if (cflag < 0 || (cflag > 0 && t->c_ispeed != t->c_ospeed))
138276195Sbrian		return (EINVAL);
138376195Sbrian
138476195Sbrian	s = splclock();
138576195Sbrian
138676195Sbrian	window = sc->window;
138776195Sbrian	sc->setwin(sc, 0);
138876195Sbrian
138976195Sbrian	if (cflag == 0) {				/* hangup */
139076195Sbrian		DLOG(DIGIDB_SET, (sc->dev, "port%d: hangup\n", pnum));
139176195Sbrian		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
139276195Sbrian	} else {
139376195Sbrian		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIS);
139476195Sbrian
139576195Sbrian		DLOG(DIGIDB_SET, (sc->dev, "port%d: CBAUD = %d\n", pnum,
139676195Sbrian		    cflag));
139776195Sbrian
139876195Sbrian#if 0
139976195Sbrian		/* convert flags to sysV-style values */
140076195Sbrian		if (t->c_cflag & PARODD)
140176195Sbrian			cflag |= 0x0200;
140276195Sbrian		if (t->c_cflag & PARENB)
140376195Sbrian			cflag |= 0x0100;
140476195Sbrian		if (t->c_cflag & CSTOPB)
140576195Sbrian			cflag |= 0x0080;
140676195Sbrian#else
140776195Sbrian		/* convert flags to sysV-style values */
140876195Sbrian		if (t->c_cflag & PARODD)
140976195Sbrian			cflag |= FEP_PARODD;
141076195Sbrian		if (t->c_cflag & PARENB)
141176195Sbrian			cflag |= FEP_PARENB;
141276195Sbrian		if (t->c_cflag & CSTOPB)
141376195Sbrian			cflag |= FEP_CSTOPB;
141476195Sbrian		if (t->c_cflag & CLOCAL)
141576195Sbrian			cflag |= FEP_CLOCAL;
141676195Sbrian#endif
141776195Sbrian
141876195Sbrian		cflag |= (t->c_cflag & CSIZE) >> 4;
141976195Sbrian		DLOG(DIGIDB_SET, (sc->dev, "port%d: CFLAG = 0x%x\n", pnum,
142076195Sbrian		    cflag));
142176195Sbrian		fepcmd_w(port, SETCFLAGS, (unsigned)cflag, 0);
142276195Sbrian	}
142376195Sbrian
142476195Sbrian	iflag =
142576195Sbrian	    t->c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP);
142676195Sbrian	if (port->c_iflag & IXON)
142776195Sbrian		iflag |= 0x400;
142876195Sbrian	if (port->c_iflag & IXANY)
142976195Sbrian		iflag |= 0x800;
143076195Sbrian	if (port->c_iflag & IXOFF)
143176195Sbrian		iflag |= 0x1000;
143276195Sbrian
143376195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: set iflag = 0x%x\n", pnum, iflag));
143476195Sbrian	fepcmd_w(port, SETIFLAGS, (unsigned)iflag, 0);
143576195Sbrian
143676195Sbrian	hflow = 0;
143776195Sbrian	if (t->c_cflag & CDTR_IFLOW)
143876195Sbrian		hflow |= sc->csigs->dtr;
143976195Sbrian	if (t->c_cflag & CRTS_IFLOW)
144076195Sbrian		hflow |= sc->csigs->rts;
144176195Sbrian	if (t->c_cflag & CCTS_OFLOW)
144276195Sbrian		hflow |= sc->csigs->cts;
144376195Sbrian	if (t->c_cflag & CDSR_OFLOW)
144478496Sbrian		hflow |= port->dsr;
144576195Sbrian	if (t->c_cflag & CCAR_OFLOW)
144678496Sbrian		hflow |= port->cd;
144776195Sbrian
144876195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: set hflow = 0x%x\n", pnum, hflow));
144976195Sbrian	fepcmd_w(port, SETHFLOW, 0xff00 | (unsigned)hflow, 0);
145076195Sbrian
145176195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: set startc(0x%x), stopc(0x%x)\n",
145276195Sbrian	    pnum, t->c_cc[VSTART], t->c_cc[VSTOP]));
145376195Sbrian	fepcmd_b(port, SONOFFC, t->c_cc[VSTART], t->c_cc[VSTOP], 0);
145476195Sbrian
145576195Sbrian	if (sc->window != 0)
145676195Sbrian		sc->towin(sc, 0);
145776195Sbrian	if (window != 0)
145876195Sbrian		sc->towin(sc, window);
145976195Sbrian	splx(s);
146076195Sbrian
146176195Sbrian	return (0);
146276195Sbrian}
146376195Sbrian
146476195Sbrianstatic void
146576195Sbriandigi_intr(void *vp)
146676195Sbrian{
146776195Sbrian	struct digi_p *port;
146876195Sbrian	char *cxcon;
146976195Sbrian	struct digi_softc *sc;
147076195Sbrian	int ehead, etail;
147176195Sbrian	volatile struct board_chan *bc;
147276195Sbrian	struct tty *tp;
147376195Sbrian	int head, tail;
147476195Sbrian	int wrapmask;
147576195Sbrian	int size, window;
147676195Sbrian	struct event {
147776195Sbrian		u_char pnum;
147876195Sbrian		u_char event;
147976195Sbrian		u_char mstat;
148076195Sbrian		u_char lstat;
148176195Sbrian	} event;
148276195Sbrian
148376195Sbrian	sc = vp;
148476195Sbrian
148576195Sbrian	if (sc->status != DIGI_STATUS_ENABLED) {
148676195Sbrian		DLOG(DIGIDB_IRQ, (sc->dev, "interrupt on disabled board !\n"));
148776195Sbrian		return;
148876195Sbrian	}
148976327Sbrian
149076195Sbrian#ifdef DIGI_INTERRUPT
149176195Sbrian	microtime(&sc->intr_timestamp);
149276195Sbrian#endif
149376195Sbrian
149476195Sbrian	window = sc->window;
149576195Sbrian	sc->setwin(sc, 0);
149676195Sbrian
149776195Sbrian	if (sc->model >= PCXEM && W(sc->vmem + 0xd00)) {
149876195Sbrian		struct con_bios *con = con_bios_list;
149976195Sbrian		register u_char *ptr;
150076195Sbrian
150176195Sbrian		ptr = sc->vmem + W(sc->vmem + 0xd00);
150276195Sbrian		while (con) {
150376195Sbrian			if (ptr[1] && W(ptr + 2) == W(con->bios + 2))
150476195Sbrian				/* Not first block -- exact match */
150576195Sbrian				break;
150676195Sbrian
150776195Sbrian			if (W(ptr + 4) >= W(con->bios + 4) &&
150876195Sbrian			    W(ptr + 4) <= W(con->bios + 6))
150976195Sbrian				/* Initial search concetrator BIOS */
151076195Sbrian				break;
151176195Sbrian		}
151276195Sbrian
151376195Sbrian		if (con == NULL) {
151476195Sbrian			log(LOG_ERR, "digi%d: wanted bios LREV = 0x%04x"
151576195Sbrian			    " not found!\n", sc->res.unit, W(ptr + 4));
151676195Sbrian			W(ptr + 10) = 0;
151776195Sbrian			W(sc->vmem + 0xd00) = 0;
151876195Sbrian			goto eoi;
151976195Sbrian		}
152076195Sbrian		cxcon = con->bios;
152176195Sbrian		W(ptr + 4) = W(cxcon + 4);
152276195Sbrian		W(ptr + 6) = W(cxcon + 6);
152376195Sbrian		if (ptr[1] == 0)
152476195Sbrian			W(ptr + 2) = W(cxcon + 2);
152576195Sbrian		W(ptr + 8) = (ptr[1] << 6) + W(cxcon + 8);
152676195Sbrian		size = W(cxcon + 10) - (ptr[1] << 10);
152776195Sbrian		if (size <= 0) {
152876195Sbrian			W(ptr + 8) = W(cxcon + 8);
152976195Sbrian			W(ptr + 10) = 0;
153076195Sbrian		} else {
153176195Sbrian			if (size > 1024)
153276195Sbrian				size = 1024;
153376195Sbrian			W(ptr + 10) = size;
153476195Sbrian			bcopy(cxcon + (ptr[1] << 10), ptr + 12, size);
153576195Sbrian		}
153676195Sbrian		W(sc->vmem + 0xd00) = 0;
153776195Sbrian		goto eoi;
153876195Sbrian	}
153976195Sbrian
154076195Sbrian	ehead = sc->gdata->ein;
154176195Sbrian	etail = sc->gdata->eout;
154276195Sbrian	if (ehead == etail) {
154376195Sbrian#ifdef DEBUG
154476195Sbrian		sc->intr_count++;
154576195Sbrian		if (sc->intr_count % 6000 == 0) {
154676195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev,
154776195Sbrian			    "6000 useless polls %x %x\n", ehead, etail));
154876195Sbrian			sc->intr_count = 0;
154976195Sbrian		}
155076195Sbrian#endif
155176195Sbrian		goto eoi;
155276195Sbrian	}
155376195Sbrian	while (ehead != etail) {
155476195Sbrian		event = *(volatile struct event *)(sc->memevent + etail);
155576195Sbrian
155676195Sbrian		etail = (etail + 4) & sc->gdata->imax;
155776195Sbrian
155876195Sbrian		if (event.pnum >= sc->numports) {
155976195Sbrian			log(LOG_ERR, "digi%d: port %d: got event"
156076195Sbrian			    " on nonexisting port\n", sc->res.unit,
156176195Sbrian			    event.pnum);
156276195Sbrian			continue;
156376195Sbrian		}
156476195Sbrian		port = &sc->ports[event.pnum];
156576195Sbrian		bc = port->bc;
156676195Sbrian		tp = port->tp;
156776195Sbrian
156876195Sbrian		if (!(tp->t_state & TS_ISOPEN) && !port->wopeners) {
156976195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev,
157076195Sbrian			    "port %d: event 0x%x on closed port\n",
157176195Sbrian			    event.pnum, event.event));
157276195Sbrian			bc->rout = bc->rin;
157376195Sbrian			bc->idata = 0;
157476195Sbrian			bc->iempty = 0;
157576195Sbrian			bc->ilow = 0;
157676195Sbrian			bc->mint = 0;
157776195Sbrian			continue;
157876195Sbrian		}
157976195Sbrian		if (event.event & ~ALL_IND)
158076195Sbrian			log(LOG_ERR, "digi%d: port%d: ? event 0x%x mstat 0x%x"
158176195Sbrian			    " lstat 0x%x\n", sc->res.unit, event.pnum,
158276195Sbrian			    event.event, event.mstat, event.lstat);
158376195Sbrian
158476195Sbrian		if (event.event & DATA_IND) {
158576195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev, "port %d: DATA_IND\n",
158676195Sbrian			    event.pnum));
158776195Sbrian			wrapmask = port->rxbufsize - 1;
158876195Sbrian			head = bc->rin;
158976195Sbrian			tail = bc->rout;
159076195Sbrian
159176195Sbrian			size = 0;
159276195Sbrian			if (!(tp->t_state & TS_ISOPEN)) {
159376195Sbrian				bc->rout = head;
159476195Sbrian				goto end_of_data;
159576195Sbrian			}
159676195Sbrian			while (head != tail) {
159776195Sbrian				int top;
159876195Sbrian
159976195Sbrian				DLOG(DIGIDB_INT, (sc->dev,
160076195Sbrian				    "port %d: p rx head = %d tail = %d\n",
160176195Sbrian				    event.pnum, head, tail));
160276195Sbrian				top = (head > tail) ? head : wrapmask + 1;
160376195Sbrian				sc->towin(sc, port->rxwin);
160476195Sbrian				size = top - tail;
160576195Sbrian				if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
160676195Sbrian					size = b_to_q((char *)port->rxbuf +
160776195Sbrian					    tail, size, &tp->t_rawq);
160876195Sbrian					tail = top - size;
160976195Sbrian					ttwakeup(tp);
161076195Sbrian				} else for (; tail < top;) {
161176195Sbrian					linesw[tp->t_line].
161276195Sbrian					    l_rint(port->rxbuf[tail], tp);
161376195Sbrian					sc->towin(sc, port->rxwin);
161476195Sbrian					size--;
161576195Sbrian					tail++;
161676195Sbrian					if (tp->t_state & TS_TBLOCK)
161776195Sbrian						break;
161876195Sbrian				}
161976195Sbrian				tail &= wrapmask;
162076195Sbrian				sc->setwin(sc, 0);
162176195Sbrian				bc->rout = tail;
162276195Sbrian				head = bc->rin;
162376195Sbrian				if (size)
162476195Sbrian					break;
162576195Sbrian			}
162676195Sbrian
162776195Sbrian			if (bc->orun) {
162876195Sbrian				CE_RECORD(port, CE_OVERRUN);
162976195Sbrian				log(LOG_ERR, "digi%d: port%d: %s\n",
163076195Sbrian				    sc->res.unit, event.pnum,
163176195Sbrian				    digi_errortxt(CE_OVERRUN));
163276195Sbrian				bc->orun = 0;
163376195Sbrian			}
163476195Sbrianend_of_data:
163576195Sbrian			if (size) {
163676195Sbrian				tp->t_state |= TS_TBLOCK;
163776195Sbrian				port->status |= PAUSE_RX;
163876195Sbrian				DLOG(DIGIDB_RX, (sc->dev, "port %d: pause RX\n",
163976195Sbrian				    event.pnum));
164076195Sbrian			} else {
164176195Sbrian				bc->idata = 1;
164276195Sbrian			}
164376195Sbrian		}
164476195Sbrian
164576195Sbrian		if (event.event & MODEMCHG_IND) {
164676195Sbrian			DLOG(DIGIDB_MODEM, (sc->dev, "port %d: MODEMCHG_IND\n",
164776195Sbrian			    event.pnum));
164876195Sbrian
164978496Sbrian			if ((event.mstat ^ event.lstat) & port->cd) {
165076195Sbrian				sc->hidewin(sc);
165176195Sbrian				linesw[tp->t_line].l_modem
165278496Sbrian				    (tp, event.mstat & port->cd);
165376195Sbrian				sc->setwin(sc, 0);
165476195Sbrian				wakeup(TSA_CARR_ON(tp));
165576195Sbrian			}
165676195Sbrian
165776195Sbrian			if (event.mstat & sc->csigs->ri) {
165876195Sbrian				DLOG(DIGIDB_RI, (sc->dev, "port %d: RING\n",
165976195Sbrian				    event.pnum));
166076195Sbrian				if (port->send_ring) {
166176195Sbrian					linesw[tp->t_line].l_rint('R', tp);
166276195Sbrian					linesw[tp->t_line].l_rint('I', tp);
166376195Sbrian					linesw[tp->t_line].l_rint('N', tp);
166476195Sbrian					linesw[tp->t_line].l_rint('G', tp);
166576195Sbrian					linesw[tp->t_line].l_rint('\r', tp);
166676195Sbrian					linesw[tp->t_line].l_rint('\n', tp);
166776195Sbrian				}
166876195Sbrian			}
166976195Sbrian		}
167076195Sbrian		if (event.event & BREAK_IND) {
167176195Sbrian			DLOG(DIGIDB_MODEM, (sc->dev, "port %d: BREAK_IND\n",
167276195Sbrian			    event.pnum));
167376195Sbrian			linesw[tp->t_line].l_rint(TTY_BI, tp);
167476195Sbrian		}
167576195Sbrian		if (event.event & (LOWTX_IND | EMPTYTX_IND)) {
167676195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev, "port %d:%s%s\n",
167776195Sbrian			    event.pnum,
167876195Sbrian			    event.event & LOWTX_IND ? " LOWTX" : "",
167976195Sbrian			    event.event & EMPTYTX_IND ?  " EMPTYTX" : ""));
168076195Sbrian			(*linesw[tp->t_line].l_start)(tp);
168176195Sbrian		}
168276195Sbrian	}
168376195Sbrian	sc->gdata->eout = etail;
168476195Sbrianeoi:
168576195Sbrian	if (sc->window != 0)
168676195Sbrian		sc->towin(sc, 0);
168776195Sbrian	if (window != 0)
168876195Sbrian		sc->towin(sc, window);
168976195Sbrian}
169076195Sbrian
169176195Sbrianstatic void
169276195Sbriandigistart(struct tty *tp)
169376195Sbrian{
169476195Sbrian	int unit;
169576195Sbrian	int pnum;
169676195Sbrian	struct digi_p *port;
169776195Sbrian	struct digi_softc *sc;
169876195Sbrian	volatile struct board_chan *bc;
169976195Sbrian	int head, tail;
170076195Sbrian	int size, ocount, totcnt = 0;
170176195Sbrian	int s;
170276195Sbrian	int wmask;
170376195Sbrian
170476195Sbrian	unit = MINOR_TO_UNIT(minor(tp->t_dev));
170576195Sbrian	pnum = MINOR_TO_PORT(minor(tp->t_dev));
170676195Sbrian
170776195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
170876195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digistart\n", unit));
170976195Sbrian
171076195Sbrian	port = &sc->ports[pnum];
171176195Sbrian	bc = port->bc;
171276195Sbrian
171376195Sbrian	wmask = port->txbufsize - 1;
171476195Sbrian
171576195Sbrian	s = spltty();
171676195Sbrian	port->lcc = tp->t_outq.c_cc;
171776195Sbrian	sc->setwin(sc, 0);
171876195Sbrian	if (!(tp->t_state & TS_TBLOCK)) {
171976195Sbrian		if (port->status & PAUSE_RX) {
172076195Sbrian			DLOG(DIGIDB_RX, (sc->dev, "port %d: resume RX\n",
172176195Sbrian			    pnum));
172276195Sbrian			/*
172376195Sbrian			 * CAREFUL - braces are needed here if the DLOG is
172476195Sbrian			 * optimised out!
172576195Sbrian			 */
172676195Sbrian		}
172776195Sbrian		port->status &= ~PAUSE_RX;
172876195Sbrian		bc->idata = 1;
172976195Sbrian	}
173076195Sbrian	if (!(tp->t_state & TS_TTSTOP) && port->status & PAUSE_TX) {
173176195Sbrian		DLOG(DIGIDB_TX, (sc->dev, "port %d: resume TX\n", pnum));
173276195Sbrian		port->status &= ~PAUSE_TX;
173376195Sbrian		fepcmd_w(port, RESUMETX, 0, 10);
173476195Sbrian	}
173576195Sbrian	if (tp->t_outq.c_cc == 0)
173676195Sbrian		tp->t_state &= ~TS_BUSY;
173776195Sbrian	else
173876195Sbrian		tp->t_state |= TS_BUSY;
173976195Sbrian
174076195Sbrian	head = bc->tin;
174176195Sbrian	while (tp->t_outq.c_cc != 0) {
174276195Sbrian		tail = bc->tout;
174376195Sbrian		DLOG(DIGIDB_INT, (sc->dev, "port%d: s tx head = %d tail = %d\n",
174476195Sbrian		    pnum, head, tail));
174576195Sbrian
174676195Sbrian		if (head < tail)
174776195Sbrian			size = tail - head - 1;
174876195Sbrian		else {
174976195Sbrian			size = port->txbufsize - head;
175076195Sbrian			if (tail == 0)
175176195Sbrian				size--;
175276195Sbrian		}
175376195Sbrian
175476195Sbrian		if (size == 0)
175576195Sbrian			break;
175676195Sbrian		sc->towin(sc, port->txwin);
175776195Sbrian		ocount = q_to_b(&tp->t_outq, port->txbuf + head, size);
175876195Sbrian		totcnt += ocount;
175976195Sbrian		head += ocount;
176076195Sbrian		head &= wmask;
176176195Sbrian		sc->setwin(sc, 0);
176276195Sbrian		bc->tin = head;
176376195Sbrian		bc->iempty = 1;
176476195Sbrian		bc->ilow = 1;
176576195Sbrian	}
176676195Sbrian	port->lostcc = tp->t_outq.c_cc;
176776195Sbrian	tail = bc->tout;
176876195Sbrian	if (head < tail)
176976195Sbrian		size = port->txbufsize - tail + head;
177076195Sbrian	else
177176195Sbrian		size = head - tail;
177276195Sbrian
177376195Sbrian	port->lbuf = size;
177476195Sbrian	DLOG(DIGIDB_INT, (sc->dev, "port%d: s total cnt = %d\n", pnum, totcnt));
177576195Sbrian	ttwwakeup(tp);
177676195Sbrian	splx(s);
177776195Sbrian}
177876195Sbrian
177976195Sbrianstatic void
178076195Sbriandigistop(struct tty *tp, int rw)
178176195Sbrian{
178276195Sbrian	struct digi_softc *sc;
178376195Sbrian	int unit;
178476195Sbrian	int pnum;
178576195Sbrian	struct digi_p *port;
178676195Sbrian
178776195Sbrian	unit = MINOR_TO_UNIT(minor(tp->t_dev));
178876195Sbrian	pnum = MINOR_TO_PORT(minor(tp->t_dev));
178976195Sbrian
179076195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
179176195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digistop\n", unit));
179276195Sbrian	port = sc->ports + pnum;
179376195Sbrian
179476195Sbrian	DLOG(DIGIDB_TX, (sc->dev, "port %d: pause TX\n", pnum));
179576195Sbrian	port->status |= PAUSE_TX;
179676195Sbrian	fepcmd_w(port, PAUSETX, 0, 10);
179776195Sbrian}
179876195Sbrian
179976195Sbrianstatic void
180076195Sbrianfepcmd(struct digi_p *port, int cmd, int op1, int ncmds)
180176195Sbrian{
180276195Sbrian	u_char *mem;
180376195Sbrian	unsigned tail, head;
180476195Sbrian	int count, n;
180576195Sbrian
180676195Sbrian	mem = port->sc->memcmd;
180776195Sbrian
180876195Sbrian	port->sc->setwin(port->sc, 0);
180976195Sbrian
181076195Sbrian	head = port->sc->gdata->cin;
181176195Sbrian	mem[head + 0] = cmd;
181276195Sbrian	mem[head + 1] = port->pnum;
181376195Sbrian	*(ushort *)(mem + head + 2) = op1;
181476195Sbrian
181576195Sbrian	head = (head + 4) & port->sc->gdata->cmax;
181676195Sbrian	port->sc->gdata->cin = head;
181776195Sbrian
181876195Sbrian	for (count = FEPTIMEOUT; count > 0; count--) {
181976195Sbrian		head = port->sc->gdata->cin;
182076195Sbrian		tail = port->sc->gdata->cout;
182176195Sbrian		n = (head - tail) & port->sc->gdata->cmax;
182276195Sbrian
182376195Sbrian		if (n <= ncmds * sizeof(short) * 4)
182476195Sbrian			break;
182576195Sbrian	}
182676195Sbrian	if (count == 0)
182776195Sbrian		log(LOG_ERR, "digi%d: port%d: timeout on FEP command\n",
182876195Sbrian		    port->sc->res.unit, port->pnum);
182976195Sbrian}
183076195Sbrian
183176195Sbrianconst char *
183276195Sbriandigi_errortxt(int id)
183376195Sbrian{
183476195Sbrian	static const char *error_desc[] = {
183576195Sbrian		"silo overflow",
183676195Sbrian		"interrupt-level buffer overflow",
183776195Sbrian		"tty-level buffer overflow",
183876195Sbrian	};
183976195Sbrian
184076195Sbrian	KASSERT(id >= 0 && id < sizeof(error_desc) / sizeof(error_desc[0]),
184176195Sbrian	    ("Unexpected digi error id %d\n", id));
184276195Sbrian
184376195Sbrian	return (error_desc[id]);
184476195Sbrian}
184576195Sbrian
184676195Sbrianint
184776195Sbriandigi_attach(struct digi_softc *sc)
184876195Sbrian{
184976195Sbrian	sc->res.ctldev = make_dev(&digi_sw,
185076195Sbrian	    (sc->res.unit << 16) | CTRL_DEV, UID_ROOT, GID_WHEEL,
185176195Sbrian	    0600, "digi%r.ctl", sc->res.unit);
185276195Sbrian
185376195Sbrian	digi_loadmoduledata(sc);
185476195Sbrian	digi_init(sc);
185576195Sbrian	digi_freemoduledata(sc);
185676195Sbrian
185776195Sbrian	return (0);
185876195Sbrian}
185976195Sbrian
186076195Sbrianstatic int
186176195Sbriandigi_inuse(struct digi_softc *sc)
186276195Sbrian{
186376195Sbrian	int i;
186476195Sbrian
186576195Sbrian	for (i = 0; i < sc->numports; i++)
186676195Sbrian		if (sc->ttys[i].t_state & TS_ISOPEN) {
186776195Sbrian			DLOG(DIGIDB_INIT, (sc->dev, "port%d: busy\n", i));
186876195Sbrian			return (1);
186976195Sbrian		} else if (sc->ports[i].wopeners || sc->ports[i].opencnt) {
187076195Sbrian			DLOG(DIGIDB_INIT, (sc->dev, "port%d: blocked in open\n",
187176195Sbrian			    i));
187276195Sbrian			return (1);
187376195Sbrian		}
187476195Sbrian	return (0);
187576195Sbrian}
187676195Sbrian
187776195Sbrianstatic void
187876195Sbriandigi_free_state(struct digi_softc *sc)
187976195Sbrian{
188076195Sbrian	int d, i;
188176195Sbrian
188276195Sbrian	/* Blow it all away */
188376195Sbrian
188476195Sbrian	for (i = 0; i < sc->numports; i++)
188576195Sbrian		for (d = 0; d < 6; d++)
188676195Sbrian			destroy_dev(sc->ports[i].dev[d]);
188776195Sbrian
188876195Sbrian	untimeout(digi_poll, sc, sc->callout);
188976195Sbrian	callout_handle_init(&sc->callout);
189076195Sbrian	untimeout(digi_int_test, sc, sc->inttest);
189176195Sbrian	callout_handle_init(&sc->inttest);
189276195Sbrian
189376195Sbrian	bus_teardown_intr(sc->dev, sc->res.irq, sc->res.irqHandler);
189476195Sbrian#ifdef DIGI_INTERRUPT
189576195Sbrian	if (sc->res.irq != NULL) {
189676195Sbrian		bus_release_resource(dev, SYS_RES_IRQ, sc->res.irqrid,
189776195Sbrian		    sc->res.irq);
189876195Sbrian		sc->res.irq = NULL;
189976195Sbrian	}
190076195Sbrian#endif
190176195Sbrian	if (sc->numports) {
190276195Sbrian		KASSERT(sc->ports, ("digi%d: Lost my ports ?", sc->res.unit));
190376195Sbrian		KASSERT(sc->ttys, ("digi%d: Lost my ttys ?", sc->res.unit));
190477004Sbrian		free(sc->ports, M_TTYS);
190576195Sbrian		sc->ports = NULL;
190677004Sbrian		free(sc->ttys, M_TTYS);
190776195Sbrian		sc->ttys = NULL;
190876195Sbrian		sc->numports = 0;
190976195Sbrian	}
191076195Sbrian
191176195Sbrian	sc->status = DIGI_STATUS_NOTINIT;
191276195Sbrian}
191376195Sbrian
191476195Sbrianint
191576195Sbriandigi_detach(device_t dev)
191676195Sbrian{
191776195Sbrian	struct digi_softc *sc = device_get_softc(dev);
191876195Sbrian
191976195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "detaching\n"));
192076195Sbrian
192176195Sbrian	/* If we're INIT'd, numports must be 0 */
192276195Sbrian	KASSERT(sc->numports == 0 || sc->status != DIGI_STATUS_NOTINIT,
192376195Sbrian	    ("digi%d: numports(%d) & status(%d) are out of sync",
192476195Sbrian	    sc->res.unit, sc->numports, (int)sc->status));
192576195Sbrian
192676195Sbrian	if (digi_inuse(sc))
192776195Sbrian		return (EBUSY);
192876195Sbrian
192976195Sbrian	digi_free_state(sc);
193076195Sbrian
193176195Sbrian	destroy_dev(makedev(CDEV_MAJOR,
193276195Sbrian	    (sc->res.unit << 16) | CTRL_DEV));
193376195Sbrian
193476195Sbrian	if (sc->res.mem != NULL) {
193576195Sbrian		bus_release_resource(dev, SYS_RES_MEMORY, sc->res.mrid,
193676195Sbrian		    sc->res.mem);
193776195Sbrian		sc->res.mem = NULL;
193876195Sbrian	}
193976195Sbrian	if (sc->res.io != NULL) {
194076195Sbrian		bus_release_resource(dev, SYS_RES_IOPORT, sc->res.iorid,
194176195Sbrian		    sc->res.io);
194276195Sbrian		sc->res.io = NULL;
194376195Sbrian	}
194476195Sbrian
194576195Sbrian	return (0);
194676195Sbrian}
194776195Sbrian
194876195Sbrianint
194976195Sbriandigi_shutdown(device_t dev)
195076195Sbrian{
195176195Sbrian	return (0);
195276195Sbrian}
195394320Sbrian
195494320SbrianMODULE_VERSION(digi, 1);
1955