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