digi.c revision 94320
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 94320 2002-04-10 01:12:55Z 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:
30176195Sbrian		if (sc->model == PCIXR)
30276195Sbrian			PCIPORT = FEPRST;
30376195Sbrian		else
30476195Sbrian			outb(sc->port, FEPRST | FEPMEM);
30576195Sbrian
30676195Sbrian		for (i = 0; ((sc->model == PCIXR ? PCIPORT : inb(sc->port)) &
30776195Sbrian		    FEPMASK) != FEPRST; i++) {
30876195Sbrian			if (i > 1000) {
30976195Sbrian				log(LOG_ERR, "digi%d: init reset failed\n",
31076195Sbrian				    sc->res.unit);
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
35176195Sbrian	if (sc->model == PCIXR) {
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
36276195Sbrian	for (i = 0; ((sc->model == PCIXR ? 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);
103178414Sbrian	if ((res = linker_reference_module(modfile, &lf)) != 0)
103276195Sbrian		printf("%s: Failed %d to load module\n", modfile, res);
103376195Sbrian	free(modfile, M_TEMP);
103476195Sbrian	if (res != 0)
103576195Sbrian		return (res);
103676195Sbrian
103778414Sbrian	sym = malloc(modlen + 10, M_TEMP, M_WAITOK);
103878414Sbrian	snprintf(sym, modlen + 10, "digi_mod_%s", sc->module);
103976195Sbrian	if ((symptr = linker_file_lookup_symbol(lf, sym, 0)) == NULL)
104076195Sbrian		printf("digi_%s.ko: Symbol `%s' not found\n", sc->module, sym);
104176195Sbrian	free(sym, M_TEMP);
104276195Sbrian
104376195Sbrian	digi_mod = (struct digi_mod *)symptr;
104476195Sbrian	if (digi_mod->dm_version != DIGI_MOD_VERSION) {
104576195Sbrian		printf("digi_%s.ko: Invalid version %d (need %d)\n",
104676195Sbrian		    sc->module, digi_mod->dm_version, DIGI_MOD_VERSION);
104776195Sbrian		linker_file_unload(lf);
104876195Sbrian		return (EINVAL);
104976195Sbrian	}
105076195Sbrian
105176195Sbrian	sc->bios.size = digi_mod->dm_bios.size;
105276195Sbrian	if (sc->bios.size != 0 && digi_mod->dm_bios.data != NULL) {
105387503Sarr		sc->bios.data = malloc(sc->bios.size, M_TTYS, M_WAITOK);
105476195Sbrian		bcopy(digi_mod->dm_bios.data, sc->bios.data, sc->bios.size);
105576195Sbrian	}
105676195Sbrian
105776195Sbrian	sc->fep.size = digi_mod->dm_fep.size;
105876195Sbrian	if (sc->fep.size != 0 && digi_mod->dm_fep.data != NULL) {
105987503Sarr		sc->fep.data = malloc(sc->fep.size, M_TTYS, M_WAITOK);
106076195Sbrian		bcopy(digi_mod->dm_fep.data, sc->fep.data, sc->fep.size);
106176195Sbrian	}
106276195Sbrian
106376195Sbrian	sc->link.size = digi_mod->dm_link.size;
106476195Sbrian	if (sc->link.size != 0 && digi_mod->dm_link.data != NULL) {
106587503Sarr		sc->link.data = malloc(sc->link.size, M_TTYS, M_WAITOK);
106676195Sbrian		bcopy(digi_mod->dm_link.data, sc->link.data, sc->link.size);
106776195Sbrian	}
106876195Sbrian
106976195Sbrian	linker_file_unload(lf);
107076195Sbrian
107176195Sbrian	return (0);
107276195Sbrian}
107376195Sbrian
107476195Sbrianstatic int
107583366Sjuliandigiioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
107676195Sbrian{
107776195Sbrian	int unit, pnum, mynor, error, s;
107876195Sbrian	struct digi_softc *sc;
107976195Sbrian	struct digi_p *port;
108076195Sbrian	struct tty *tp;
108176195Sbrian#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
108276195Sbrian	int oldcmd;
108376195Sbrian	struct termios term;
108476195Sbrian#endif
108576195Sbrian
108676195Sbrian	mynor = minor(dev);
108776195Sbrian	unit = MINOR_TO_UNIT(mynor);
108876195Sbrian	pnum = MINOR_TO_PORT(mynor);
108976195Sbrian
109076195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
109176195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiioctl\n", unit));
109276195Sbrian
109376195Sbrian	if (sc->status == DIGI_STATUS_DISABLED)
109476195Sbrian		return (ENXIO);
109576195Sbrian
109676195Sbrian	if (mynor & CTRL_DEV) {
109776195Sbrian		switch (cmd) {
109876195Sbrian		case DIGIIO_DEBUG:
109976195Sbrian#ifdef DEBUG
110076195Sbrian			digi_debug = *(int *)data;
110176195Sbrian			return (0);
110276195Sbrian#else
110376195Sbrian			device_printf(sc->dev, "DEBUG not defined\n");
110476195Sbrian			return (ENXIO);
110576195Sbrian#endif
110676195Sbrian		case DIGIIO_REINIT:
110776195Sbrian			digi_loadmoduledata(sc);
110876195Sbrian			error = digi_init(sc);
110976195Sbrian			digi_freemoduledata(sc);
111076195Sbrian			return (error);
111176195Sbrian
111276195Sbrian		case DIGIIO_MODEL:
111376705Sbrian			*(enum digi_model *)data = sc->model;
111476195Sbrian			return (0);
111576195Sbrian
111676195Sbrian		case DIGIIO_IDENT:
111776195Sbrian			return (copyout(sc->name, *(char **)data,
111876195Sbrian			    strlen(sc->name) + 1));
111976195Sbrian		}
112076195Sbrian	}
112176195Sbrian
112276195Sbrian	if (pnum >= sc->numports)
112376195Sbrian		return (ENXIO);
112476195Sbrian
112576195Sbrian	port = sc->ports + pnum;
112676195Sbrian	if (!(port->status & ENABLED))
112776195Sbrian		return (ENXIO);
112876195Sbrian
112976195Sbrian	tp = port->tp;
113076195Sbrian
113176195Sbrian	if (mynor & CONTROL_MASK) {
113276195Sbrian		struct termios *ct;
113376195Sbrian
113476195Sbrian		switch (mynor & CONTROL_MASK) {
113576195Sbrian		case CONTROL_INIT_STATE:
113676195Sbrian			ct = (mynor & CALLOUT_MASK) ?
113776195Sbrian			    &port->it_out : &port->it_in;
113876195Sbrian			break;
113976195Sbrian		case CONTROL_LOCK_STATE:
114076195Sbrian			ct = (mynor & CALLOUT_MASK) ?
114176195Sbrian			    &port->lt_out : &port->lt_in;
114276195Sbrian			break;
114376195Sbrian		default:
114476195Sbrian			return (ENODEV);	/* /dev/nodev */
114576195Sbrian		}
114676195Sbrian
114776195Sbrian		switch (cmd) {
114876195Sbrian		case TIOCSETA:
114993593Sjhb			error = suser(td);
115076195Sbrian			if (error != 0)
115176195Sbrian				return (error);
115276195Sbrian			*ct = *(struct termios *)data;
115378496Sbrian			return (0);
115476195Sbrian
115576195Sbrian		case TIOCGETA:
115676195Sbrian			*(struct termios *)data = *ct;
115778496Sbrian			return (0);
115876195Sbrian
115976195Sbrian		case TIOCGETD:
116076195Sbrian			*(int *)data = TTYDISC;
116176195Sbrian			return (0);
116278496Sbrian
116376195Sbrian		case TIOCGWINSZ:
116476195Sbrian			bzero(data, sizeof(struct winsize));
116576195Sbrian			return (0);
116678496Sbrian
116778496Sbrian		case DIGIIO_GETALTPIN:
116878496Sbrian			switch (mynor & CONTROL_MASK) {
116978496Sbrian			case CONTROL_INIT_STATE:
117078496Sbrian				*(int *)data = port->ialtpin;
117178496Sbrian				break;
117278496Sbrian
117378496Sbrian			case CONTROL_LOCK_STATE:
117478496Sbrian				*(int *)data = port->laltpin;
117578496Sbrian				break;
117678496Sbrian
117778496Sbrian			default:
117878496Sbrian				panic("Confusion when re-testing minor");
117978496Sbrian				return (ENODEV);
118078496Sbrian			}
118178496Sbrian			return (0);
118278496Sbrian
118378496Sbrian		case DIGIIO_SETALTPIN:
118478496Sbrian			switch (mynor & CONTROL_MASK) {
118578496Sbrian			case CONTROL_INIT_STATE:
118678496Sbrian				if (!port->laltpin) {
118778496Sbrian					port->ialtpin = !!*(int *)data;
118878496Sbrian					DLOG(DIGIDB_SET, (sc->dev,
118978496Sbrian					    "port%d: initial ALTPIN %s\n", pnum,
119078496Sbrian					    port->ialtpin ? "set" : "cleared"));
119178496Sbrian				}
119278496Sbrian				break;
119378496Sbrian
119478496Sbrian			case CONTROL_LOCK_STATE:
119578496Sbrian				port->laltpin = !!*(int *)data;
119678496Sbrian				DLOG(DIGIDB_SET, (sc->dev,
119778496Sbrian				    "port%d: ALTPIN %slocked\n",
119878496Sbrian				    pnum, port->laltpin ? "" : "un"));
119978496Sbrian				break;
120078496Sbrian
120178496Sbrian			default:
120278496Sbrian				panic("Confusion when re-testing minor");
120378496Sbrian				return (ENODEV);
120478496Sbrian			}
120578496Sbrian			return (0);
120678496Sbrian
120776195Sbrian		default:
120876195Sbrian			return (ENOTTY);
120976195Sbrian		}
121076195Sbrian	}
121178496Sbrian
121278496Sbrian	switch (cmd) {
121378496Sbrian	case DIGIIO_GETALTPIN:
121478496Sbrian		*(int *)data = !!(port->dsr == sc->csigs->cd);
121578496Sbrian		return (0);
121678496Sbrian
121778496Sbrian	case DIGIIO_SETALTPIN:
121878496Sbrian		if (!port->laltpin) {
121978496Sbrian			if (*(int *)data) {
122078496Sbrian				DLOG(DIGIDB_SET, (sc->dev,
122178496Sbrian				    "port%d: ALTPIN set\n", pnum));
122278496Sbrian				port->cd = sc->csigs->dsr;
122378496Sbrian				port->dsr = sc->csigs->cd;
122478496Sbrian			} else {
122578496Sbrian				DLOG(DIGIDB_SET, (sc->dev,
122678496Sbrian				    "port%d: ALTPIN cleared\n", pnum));
122778496Sbrian				port->cd = sc->csigs->cd;
122878496Sbrian				port->dsr = sc->csigs->dsr;
122978496Sbrian			}
123078496Sbrian		}
123178496Sbrian		return (0);
123278496Sbrian	}
123378496Sbrian
123476195Sbrian	tp = port->tp;
123576195Sbrian#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
123676195Sbrian	term = tp->t_termios;
123776195Sbrian	oldcmd = cmd;
123876195Sbrian	error = ttsetcompat(tp, &cmd, data, &term);
123976195Sbrian	if (error != 0)
124076195Sbrian		return (error);
124176195Sbrian	if (cmd != oldcmd)
124276195Sbrian		data = (caddr_t) & term;
124376195Sbrian#endif
124476195Sbrian	if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
124576195Sbrian		int cc;
124676195Sbrian		struct termios *dt;
124776195Sbrian		struct termios *lt;
124876195Sbrian
124976195Sbrian		dt = (struct termios *)data;
125076195Sbrian		lt = (mynor & CALLOUT_MASK) ? &port->lt_out : &port->lt_in;
125176195Sbrian
125276195Sbrian		dt->c_iflag =
125376195Sbrian		    (tp->t_iflag & lt->c_iflag) | (dt->c_iflag & ~lt->c_iflag);
125476195Sbrian		dt->c_oflag =
125576195Sbrian		    (tp->t_oflag & lt->c_oflag) | (dt->c_oflag & ~lt->c_oflag);
125676195Sbrian		dt->c_cflag =
125776195Sbrian		    (tp->t_cflag & lt->c_cflag) | (dt->c_cflag & ~lt->c_cflag);
125876195Sbrian		dt->c_lflag =
125976195Sbrian		    (tp->t_lflag & lt->c_lflag) | (dt->c_lflag & ~lt->c_lflag);
126076195Sbrian		port->c_iflag = dt->c_iflag & (IXOFF | IXON | IXANY);
126176195Sbrian		dt->c_iflag &= ~(IXOFF | IXON | IXANY);
126276195Sbrian		for (cc = 0; cc < NCCS; ++cc)
126376195Sbrian			if (lt->c_cc[cc] != 0)
126476195Sbrian				dt->c_cc[cc] = tp->t_cc[cc];
126576195Sbrian		if (lt->c_ispeed != 0)
126676195Sbrian			dt->c_ispeed = tp->t_ispeed;
126776195Sbrian		if (lt->c_ospeed != 0)
126876195Sbrian			dt->c_ospeed = tp->t_ospeed;
126976195Sbrian	}
127083366Sjulian	error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, td);
127176195Sbrian	if (error == 0 && cmd == TIOCGETA)
127276195Sbrian		((struct termios *)data)->c_iflag |= port->c_iflag;
127376195Sbrian
127476195Sbrian	if (error >= 0 && error != ENOIOCTL)
127576195Sbrian		return (error);
127676195Sbrian	s = spltty();
127776195Sbrian	error = ttioctl(tp, cmd, data, flag);
127876195Sbrian	if (error == 0 && cmd == TIOCGETA)
127976195Sbrian		((struct termios *)data)->c_iflag |= port->c_iflag;
128076195Sbrian
128176195Sbrian	digi_disc_optim(tp, &tp->t_termios, port);
128276195Sbrian	if (error >= 0 && error != ENOIOCTL) {
128376195Sbrian		splx(s);
128476195Sbrian		return (error);
128576195Sbrian	}
128676195Sbrian	sc->setwin(sc, 0);
128776195Sbrian	switch (cmd) {
128876195Sbrian	case DIGIIO_RING:
128976195Sbrian		port->send_ring = *(u_char *)data;
129076195Sbrian		break;
129176195Sbrian	case TIOCSBRK:
129276195Sbrian		/*
129383861Sbrian		 * now it sends 400 millisecond break because I don't know
129476195Sbrian		 * how to send an infinite break
129576195Sbrian		 */
129683861Sbrian		fepcmd_w(port, SENDBREAK, 400, 10);
129776195Sbrian		break;
129876195Sbrian	case TIOCCBRK:
129976195Sbrian		/* now it's empty */
130076195Sbrian		break;
130176195Sbrian	case TIOCSDTR:
130276195Sbrian		digimctl(port, TIOCM_DTR, DMBIS);
130376195Sbrian		break;
130476195Sbrian	case TIOCCDTR:
130576195Sbrian		digimctl(port, TIOCM_DTR, DMBIC);
130676195Sbrian		break;
130776195Sbrian	case TIOCMSET:
130876195Sbrian		digimctl(port, *(int *)data, DMSET);
130976195Sbrian		break;
131076195Sbrian	case TIOCMBIS:
131176195Sbrian		digimctl(port, *(int *)data, DMBIS);
131276195Sbrian		break;
131376195Sbrian	case TIOCMBIC:
131476195Sbrian		digimctl(port, *(int *)data, DMBIC);
131576195Sbrian		break;
131676195Sbrian	case TIOCMGET:
131776195Sbrian		*(int *)data = digimctl(port, 0, DMGET);
131876195Sbrian		break;
131976195Sbrian	case TIOCMSDTRWAIT:
132093593Sjhb		error = suser(td);
132176195Sbrian		if (error != 0) {
132276195Sbrian			splx(s);
132376195Sbrian			return (error);
132476195Sbrian		}
132576195Sbrian		port->dtr_wait = *(int *)data *hz / 100;
132676195Sbrian
132776195Sbrian		break;
132876195Sbrian	case TIOCMGDTRWAIT:
132976195Sbrian		*(int *)data = port->dtr_wait * 100 / hz;
133076195Sbrian		break;
133176195Sbrian#ifdef DIGI_INTERRUPT
133276195Sbrian	case TIOCTIMESTAMP:
133376195Sbrian		*(struct timeval *)data = sc->intr_timestamp;
133476195Sbrian
133576195Sbrian		break;
133676195Sbrian#endif
133776195Sbrian	default:
133876195Sbrian		splx(s);
133976195Sbrian		return (ENOTTY);
134076195Sbrian	}
134176195Sbrian	splx(s);
134276195Sbrian	return (0);
134376195Sbrian}
134476195Sbrian
134576195Sbrianstatic int
134676195Sbriandigiparam(struct tty *tp, struct termios *t)
134776195Sbrian{
134876195Sbrian	int mynor;
134976195Sbrian	int unit;
135076195Sbrian	int pnum;
135176195Sbrian	struct digi_softc *sc;
135276195Sbrian	struct digi_p *port;
135376195Sbrian	int cflag;
135476195Sbrian	int iflag;
135576195Sbrian	int hflow;
135676195Sbrian	int s;
135776195Sbrian	int window;
135876195Sbrian
135976195Sbrian	mynor = minor(tp->t_dev);
136076195Sbrian	unit = MINOR_TO_UNIT(mynor);
136176195Sbrian	pnum = MINOR_TO_PORT(mynor);
136276195Sbrian
136376195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
136476195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiparam\n", unit));
136576195Sbrian
136676195Sbrian	port = &sc->ports[pnum];
136776195Sbrian
136876195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: setting parameters\n", pnum));
136976195Sbrian
137076195Sbrian	if (t->c_ispeed == 0)
137176195Sbrian		t->c_ispeed = t->c_ospeed;
137276195Sbrian
137376195Sbrian	cflag = ttspeedtab(t->c_ospeed, digispeedtab);
137476195Sbrian
137576195Sbrian	if (cflag < 0 || (cflag > 0 && t->c_ispeed != t->c_ospeed))
137676195Sbrian		return (EINVAL);
137776195Sbrian
137876195Sbrian	s = splclock();
137976195Sbrian
138076195Sbrian	window = sc->window;
138176195Sbrian	sc->setwin(sc, 0);
138276195Sbrian
138376195Sbrian	if (cflag == 0) {				/* hangup */
138476195Sbrian		DLOG(DIGIDB_SET, (sc->dev, "port%d: hangup\n", pnum));
138576195Sbrian		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
138676195Sbrian	} else {
138776195Sbrian		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIS);
138876195Sbrian
138976195Sbrian		DLOG(DIGIDB_SET, (sc->dev, "port%d: CBAUD = %d\n", pnum,
139076195Sbrian		    cflag));
139176195Sbrian
139276195Sbrian#if 0
139376195Sbrian		/* convert flags to sysV-style values */
139476195Sbrian		if (t->c_cflag & PARODD)
139576195Sbrian			cflag |= 0x0200;
139676195Sbrian		if (t->c_cflag & PARENB)
139776195Sbrian			cflag |= 0x0100;
139876195Sbrian		if (t->c_cflag & CSTOPB)
139976195Sbrian			cflag |= 0x0080;
140076195Sbrian#else
140176195Sbrian		/* convert flags to sysV-style values */
140276195Sbrian		if (t->c_cflag & PARODD)
140376195Sbrian			cflag |= FEP_PARODD;
140476195Sbrian		if (t->c_cflag & PARENB)
140576195Sbrian			cflag |= FEP_PARENB;
140676195Sbrian		if (t->c_cflag & CSTOPB)
140776195Sbrian			cflag |= FEP_CSTOPB;
140876195Sbrian		if (t->c_cflag & CLOCAL)
140976195Sbrian			cflag |= FEP_CLOCAL;
141076195Sbrian#endif
141176195Sbrian
141276195Sbrian		cflag |= (t->c_cflag & CSIZE) >> 4;
141376195Sbrian		DLOG(DIGIDB_SET, (sc->dev, "port%d: CFLAG = 0x%x\n", pnum,
141476195Sbrian		    cflag));
141576195Sbrian		fepcmd_w(port, SETCFLAGS, (unsigned)cflag, 0);
141676195Sbrian	}
141776195Sbrian
141876195Sbrian	iflag =
141976195Sbrian	    t->c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP);
142076195Sbrian	if (port->c_iflag & IXON)
142176195Sbrian		iflag |= 0x400;
142276195Sbrian	if (port->c_iflag & IXANY)
142376195Sbrian		iflag |= 0x800;
142476195Sbrian	if (port->c_iflag & IXOFF)
142576195Sbrian		iflag |= 0x1000;
142676195Sbrian
142776195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: set iflag = 0x%x\n", pnum, iflag));
142876195Sbrian	fepcmd_w(port, SETIFLAGS, (unsigned)iflag, 0);
142976195Sbrian
143076195Sbrian	hflow = 0;
143176195Sbrian	if (t->c_cflag & CDTR_IFLOW)
143276195Sbrian		hflow |= sc->csigs->dtr;
143376195Sbrian	if (t->c_cflag & CRTS_IFLOW)
143476195Sbrian		hflow |= sc->csigs->rts;
143576195Sbrian	if (t->c_cflag & CCTS_OFLOW)
143676195Sbrian		hflow |= sc->csigs->cts;
143776195Sbrian	if (t->c_cflag & CDSR_OFLOW)
143878496Sbrian		hflow |= port->dsr;
143976195Sbrian	if (t->c_cflag & CCAR_OFLOW)
144078496Sbrian		hflow |= port->cd;
144176195Sbrian
144276195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: set hflow = 0x%x\n", pnum, hflow));
144376195Sbrian	fepcmd_w(port, SETHFLOW, 0xff00 | (unsigned)hflow, 0);
144476195Sbrian
144576195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: set startc(0x%x), stopc(0x%x)\n",
144676195Sbrian	    pnum, t->c_cc[VSTART], t->c_cc[VSTOP]));
144776195Sbrian	fepcmd_b(port, SONOFFC, t->c_cc[VSTART], t->c_cc[VSTOP], 0);
144876195Sbrian
144976195Sbrian	if (sc->window != 0)
145076195Sbrian		sc->towin(sc, 0);
145176195Sbrian	if (window != 0)
145276195Sbrian		sc->towin(sc, window);
145376195Sbrian	splx(s);
145476195Sbrian
145576195Sbrian	return (0);
145676195Sbrian}
145776195Sbrian
145876195Sbrianstatic void
145976195Sbriandigi_intr(void *vp)
146076195Sbrian{
146176195Sbrian	struct digi_p *port;
146276195Sbrian	char *cxcon;
146376195Sbrian	struct digi_softc *sc;
146476195Sbrian	int ehead, etail;
146576195Sbrian	volatile struct board_chan *bc;
146676195Sbrian	struct tty *tp;
146776195Sbrian	int head, tail;
146876195Sbrian	int wrapmask;
146976195Sbrian	int size, window;
147076195Sbrian	struct event {
147176195Sbrian		u_char pnum;
147276195Sbrian		u_char event;
147376195Sbrian		u_char mstat;
147476195Sbrian		u_char lstat;
147576195Sbrian	} event;
147676195Sbrian
147776195Sbrian	sc = vp;
147876195Sbrian
147976195Sbrian	if (sc->status != DIGI_STATUS_ENABLED) {
148076195Sbrian		DLOG(DIGIDB_IRQ, (sc->dev, "interrupt on disabled board !\n"));
148176195Sbrian		return;
148276195Sbrian	}
148376327Sbrian
148476195Sbrian#ifdef DIGI_INTERRUPT
148576195Sbrian	microtime(&sc->intr_timestamp);
148676195Sbrian#endif
148776195Sbrian
148876195Sbrian	window = sc->window;
148976195Sbrian	sc->setwin(sc, 0);
149076195Sbrian
149176195Sbrian	if (sc->model >= PCXEM && W(sc->vmem + 0xd00)) {
149276195Sbrian		struct con_bios *con = con_bios_list;
149376195Sbrian		register u_char *ptr;
149476195Sbrian
149576195Sbrian		ptr = sc->vmem + W(sc->vmem + 0xd00);
149676195Sbrian		while (con) {
149776195Sbrian			if (ptr[1] && W(ptr + 2) == W(con->bios + 2))
149876195Sbrian				/* Not first block -- exact match */
149976195Sbrian				break;
150076195Sbrian
150176195Sbrian			if (W(ptr + 4) >= W(con->bios + 4) &&
150276195Sbrian			    W(ptr + 4) <= W(con->bios + 6))
150376195Sbrian				/* Initial search concetrator BIOS */
150476195Sbrian				break;
150576195Sbrian		}
150676195Sbrian
150776195Sbrian		if (con == NULL) {
150876195Sbrian			log(LOG_ERR, "digi%d: wanted bios LREV = 0x%04x"
150976195Sbrian			    " not found!\n", sc->res.unit, W(ptr + 4));
151076195Sbrian			W(ptr + 10) = 0;
151176195Sbrian			W(sc->vmem + 0xd00) = 0;
151276195Sbrian			goto eoi;
151376195Sbrian		}
151476195Sbrian		cxcon = con->bios;
151576195Sbrian		W(ptr + 4) = W(cxcon + 4);
151676195Sbrian		W(ptr + 6) = W(cxcon + 6);
151776195Sbrian		if (ptr[1] == 0)
151876195Sbrian			W(ptr + 2) = W(cxcon + 2);
151976195Sbrian		W(ptr + 8) = (ptr[1] << 6) + W(cxcon + 8);
152076195Sbrian		size = W(cxcon + 10) - (ptr[1] << 10);
152176195Sbrian		if (size <= 0) {
152276195Sbrian			W(ptr + 8) = W(cxcon + 8);
152376195Sbrian			W(ptr + 10) = 0;
152476195Sbrian		} else {
152576195Sbrian			if (size > 1024)
152676195Sbrian				size = 1024;
152776195Sbrian			W(ptr + 10) = size;
152876195Sbrian			bcopy(cxcon + (ptr[1] << 10), ptr + 12, size);
152976195Sbrian		}
153076195Sbrian		W(sc->vmem + 0xd00) = 0;
153176195Sbrian		goto eoi;
153276195Sbrian	}
153376195Sbrian
153476195Sbrian	ehead = sc->gdata->ein;
153576195Sbrian	etail = sc->gdata->eout;
153676195Sbrian	if (ehead == etail) {
153776195Sbrian#ifdef DEBUG
153876195Sbrian		sc->intr_count++;
153976195Sbrian		if (sc->intr_count % 6000 == 0) {
154076195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev,
154176195Sbrian			    "6000 useless polls %x %x\n", ehead, etail));
154276195Sbrian			sc->intr_count = 0;
154376195Sbrian		}
154476195Sbrian#endif
154576195Sbrian		goto eoi;
154676195Sbrian	}
154776195Sbrian	while (ehead != etail) {
154876195Sbrian		event = *(volatile struct event *)(sc->memevent + etail);
154976195Sbrian
155076195Sbrian		etail = (etail + 4) & sc->gdata->imax;
155176195Sbrian
155276195Sbrian		if (event.pnum >= sc->numports) {
155376195Sbrian			log(LOG_ERR, "digi%d: port %d: got event"
155476195Sbrian			    " on nonexisting port\n", sc->res.unit,
155576195Sbrian			    event.pnum);
155676195Sbrian			continue;
155776195Sbrian		}
155876195Sbrian		port = &sc->ports[event.pnum];
155976195Sbrian		bc = port->bc;
156076195Sbrian		tp = port->tp;
156176195Sbrian
156276195Sbrian		if (!(tp->t_state & TS_ISOPEN) && !port->wopeners) {
156376195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev,
156476195Sbrian			    "port %d: event 0x%x on closed port\n",
156576195Sbrian			    event.pnum, event.event));
156676195Sbrian			bc->rout = bc->rin;
156776195Sbrian			bc->idata = 0;
156876195Sbrian			bc->iempty = 0;
156976195Sbrian			bc->ilow = 0;
157076195Sbrian			bc->mint = 0;
157176195Sbrian			continue;
157276195Sbrian		}
157376195Sbrian		if (event.event & ~ALL_IND)
157476195Sbrian			log(LOG_ERR, "digi%d: port%d: ? event 0x%x mstat 0x%x"
157576195Sbrian			    " lstat 0x%x\n", sc->res.unit, event.pnum,
157676195Sbrian			    event.event, event.mstat, event.lstat);
157776195Sbrian
157876195Sbrian		if (event.event & DATA_IND) {
157976195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev, "port %d: DATA_IND\n",
158076195Sbrian			    event.pnum));
158176195Sbrian			wrapmask = port->rxbufsize - 1;
158276195Sbrian			head = bc->rin;
158376195Sbrian			tail = bc->rout;
158476195Sbrian
158576195Sbrian			size = 0;
158676195Sbrian			if (!(tp->t_state & TS_ISOPEN)) {
158776195Sbrian				bc->rout = head;
158876195Sbrian				goto end_of_data;
158976195Sbrian			}
159076195Sbrian			while (head != tail) {
159176195Sbrian				int top;
159276195Sbrian
159376195Sbrian				DLOG(DIGIDB_INT, (sc->dev,
159476195Sbrian				    "port %d: p rx head = %d tail = %d\n",
159576195Sbrian				    event.pnum, head, tail));
159676195Sbrian				top = (head > tail) ? head : wrapmask + 1;
159776195Sbrian				sc->towin(sc, port->rxwin);
159876195Sbrian				size = top - tail;
159976195Sbrian				if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
160076195Sbrian					size = b_to_q((char *)port->rxbuf +
160176195Sbrian					    tail, size, &tp->t_rawq);
160276195Sbrian					tail = top - size;
160376195Sbrian					ttwakeup(tp);
160476195Sbrian				} else for (; tail < top;) {
160576195Sbrian					linesw[tp->t_line].
160676195Sbrian					    l_rint(port->rxbuf[tail], tp);
160776195Sbrian					sc->towin(sc, port->rxwin);
160876195Sbrian					size--;
160976195Sbrian					tail++;
161076195Sbrian					if (tp->t_state & TS_TBLOCK)
161176195Sbrian						break;
161276195Sbrian				}
161376195Sbrian				tail &= wrapmask;
161476195Sbrian				sc->setwin(sc, 0);
161576195Sbrian				bc->rout = tail;
161676195Sbrian				head = bc->rin;
161776195Sbrian				if (size)
161876195Sbrian					break;
161976195Sbrian			}
162076195Sbrian
162176195Sbrian			if (bc->orun) {
162276195Sbrian				CE_RECORD(port, CE_OVERRUN);
162376195Sbrian				log(LOG_ERR, "digi%d: port%d: %s\n",
162476195Sbrian				    sc->res.unit, event.pnum,
162576195Sbrian				    digi_errortxt(CE_OVERRUN));
162676195Sbrian				bc->orun = 0;
162776195Sbrian			}
162876195Sbrianend_of_data:
162976195Sbrian			if (size) {
163076195Sbrian				tp->t_state |= TS_TBLOCK;
163176195Sbrian				port->status |= PAUSE_RX;
163276195Sbrian				DLOG(DIGIDB_RX, (sc->dev, "port %d: pause RX\n",
163376195Sbrian				    event.pnum));
163476195Sbrian			} else {
163576195Sbrian				bc->idata = 1;
163676195Sbrian			}
163776195Sbrian		}
163876195Sbrian
163976195Sbrian		if (event.event & MODEMCHG_IND) {
164076195Sbrian			DLOG(DIGIDB_MODEM, (sc->dev, "port %d: MODEMCHG_IND\n",
164176195Sbrian			    event.pnum));
164276195Sbrian
164378496Sbrian			if ((event.mstat ^ event.lstat) & port->cd) {
164476195Sbrian				sc->hidewin(sc);
164576195Sbrian				linesw[tp->t_line].l_modem
164678496Sbrian				    (tp, event.mstat & port->cd);
164776195Sbrian				sc->setwin(sc, 0);
164876195Sbrian				wakeup(TSA_CARR_ON(tp));
164976195Sbrian			}
165076195Sbrian
165176195Sbrian			if (event.mstat & sc->csigs->ri) {
165276195Sbrian				DLOG(DIGIDB_RI, (sc->dev, "port %d: RING\n",
165376195Sbrian				    event.pnum));
165476195Sbrian				if (port->send_ring) {
165576195Sbrian					linesw[tp->t_line].l_rint('R', tp);
165676195Sbrian					linesw[tp->t_line].l_rint('I', tp);
165776195Sbrian					linesw[tp->t_line].l_rint('N', tp);
165876195Sbrian					linesw[tp->t_line].l_rint('G', tp);
165976195Sbrian					linesw[tp->t_line].l_rint('\r', tp);
166076195Sbrian					linesw[tp->t_line].l_rint('\n', tp);
166176195Sbrian				}
166276195Sbrian			}
166376195Sbrian		}
166476195Sbrian		if (event.event & BREAK_IND) {
166576195Sbrian			DLOG(DIGIDB_MODEM, (sc->dev, "port %d: BREAK_IND\n",
166676195Sbrian			    event.pnum));
166776195Sbrian			linesw[tp->t_line].l_rint(TTY_BI, tp);
166876195Sbrian		}
166976195Sbrian		if (event.event & (LOWTX_IND | EMPTYTX_IND)) {
167076195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev, "port %d:%s%s\n",
167176195Sbrian			    event.pnum,
167276195Sbrian			    event.event & LOWTX_IND ? " LOWTX" : "",
167376195Sbrian			    event.event & EMPTYTX_IND ?  " EMPTYTX" : ""));
167476195Sbrian			(*linesw[tp->t_line].l_start)(tp);
167576195Sbrian		}
167676195Sbrian	}
167776195Sbrian	sc->gdata->eout = etail;
167876195Sbrianeoi:
167976195Sbrian	if (sc->window != 0)
168076195Sbrian		sc->towin(sc, 0);
168176195Sbrian	if (window != 0)
168276195Sbrian		sc->towin(sc, window);
168376195Sbrian}
168476195Sbrian
168576195Sbrianstatic void
168676195Sbriandigistart(struct tty *tp)
168776195Sbrian{
168876195Sbrian	int unit;
168976195Sbrian	int pnum;
169076195Sbrian	struct digi_p *port;
169176195Sbrian	struct digi_softc *sc;
169276195Sbrian	volatile struct board_chan *bc;
169376195Sbrian	int head, tail;
169476195Sbrian	int size, ocount, totcnt = 0;
169576195Sbrian	int s;
169676195Sbrian	int wmask;
169776195Sbrian
169876195Sbrian	unit = MINOR_TO_UNIT(minor(tp->t_dev));
169976195Sbrian	pnum = MINOR_TO_PORT(minor(tp->t_dev));
170076195Sbrian
170176195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
170276195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digistart\n", unit));
170376195Sbrian
170476195Sbrian	port = &sc->ports[pnum];
170576195Sbrian	bc = port->bc;
170676195Sbrian
170776195Sbrian	wmask = port->txbufsize - 1;
170876195Sbrian
170976195Sbrian	s = spltty();
171076195Sbrian	port->lcc = tp->t_outq.c_cc;
171176195Sbrian	sc->setwin(sc, 0);
171276195Sbrian	if (!(tp->t_state & TS_TBLOCK)) {
171376195Sbrian		if (port->status & PAUSE_RX) {
171476195Sbrian			DLOG(DIGIDB_RX, (sc->dev, "port %d: resume RX\n",
171576195Sbrian			    pnum));
171676195Sbrian			/*
171776195Sbrian			 * CAREFUL - braces are needed here if the DLOG is
171876195Sbrian			 * optimised out!
171976195Sbrian			 */
172076195Sbrian		}
172176195Sbrian		port->status &= ~PAUSE_RX;
172276195Sbrian		bc->idata = 1;
172376195Sbrian	}
172476195Sbrian	if (!(tp->t_state & TS_TTSTOP) && port->status & PAUSE_TX) {
172576195Sbrian		DLOG(DIGIDB_TX, (sc->dev, "port %d: resume TX\n", pnum));
172676195Sbrian		port->status &= ~PAUSE_TX;
172776195Sbrian		fepcmd_w(port, RESUMETX, 0, 10);
172876195Sbrian	}
172976195Sbrian	if (tp->t_outq.c_cc == 0)
173076195Sbrian		tp->t_state &= ~TS_BUSY;
173176195Sbrian	else
173276195Sbrian		tp->t_state |= TS_BUSY;
173376195Sbrian
173476195Sbrian	head = bc->tin;
173576195Sbrian	while (tp->t_outq.c_cc != 0) {
173676195Sbrian		tail = bc->tout;
173776195Sbrian		DLOG(DIGIDB_INT, (sc->dev, "port%d: s tx head = %d tail = %d\n",
173876195Sbrian		    pnum, head, tail));
173976195Sbrian
174076195Sbrian		if (head < tail)
174176195Sbrian			size = tail - head - 1;
174276195Sbrian		else {
174376195Sbrian			size = port->txbufsize - head;
174476195Sbrian			if (tail == 0)
174576195Sbrian				size--;
174676195Sbrian		}
174776195Sbrian
174876195Sbrian		if (size == 0)
174976195Sbrian			break;
175076195Sbrian		sc->towin(sc, port->txwin);
175176195Sbrian		ocount = q_to_b(&tp->t_outq, port->txbuf + head, size);
175276195Sbrian		totcnt += ocount;
175376195Sbrian		head += ocount;
175476195Sbrian		head &= wmask;
175576195Sbrian		sc->setwin(sc, 0);
175676195Sbrian		bc->tin = head;
175776195Sbrian		bc->iempty = 1;
175876195Sbrian		bc->ilow = 1;
175976195Sbrian	}
176076195Sbrian	port->lostcc = tp->t_outq.c_cc;
176176195Sbrian	tail = bc->tout;
176276195Sbrian	if (head < tail)
176376195Sbrian		size = port->txbufsize - tail + head;
176476195Sbrian	else
176576195Sbrian		size = head - tail;
176676195Sbrian
176776195Sbrian	port->lbuf = size;
176876195Sbrian	DLOG(DIGIDB_INT, (sc->dev, "port%d: s total cnt = %d\n", pnum, totcnt));
176976195Sbrian	ttwwakeup(tp);
177076195Sbrian	splx(s);
177176195Sbrian}
177276195Sbrian
177376195Sbrianstatic void
177476195Sbriandigistop(struct tty *tp, int rw)
177576195Sbrian{
177676195Sbrian	struct digi_softc *sc;
177776195Sbrian	int unit;
177876195Sbrian	int pnum;
177976195Sbrian	struct digi_p *port;
178076195Sbrian
178176195Sbrian	unit = MINOR_TO_UNIT(minor(tp->t_dev));
178276195Sbrian	pnum = MINOR_TO_PORT(minor(tp->t_dev));
178376195Sbrian
178476195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
178576195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digistop\n", unit));
178676195Sbrian	port = sc->ports + pnum;
178776195Sbrian
178876195Sbrian	DLOG(DIGIDB_TX, (sc->dev, "port %d: pause TX\n", pnum));
178976195Sbrian	port->status |= PAUSE_TX;
179076195Sbrian	fepcmd_w(port, PAUSETX, 0, 10);
179176195Sbrian}
179276195Sbrian
179376195Sbrianstatic void
179476195Sbrianfepcmd(struct digi_p *port, int cmd, int op1, int ncmds)
179576195Sbrian{
179676195Sbrian	u_char *mem;
179776195Sbrian	unsigned tail, head;
179876195Sbrian	int count, n;
179976195Sbrian
180076195Sbrian	mem = port->sc->memcmd;
180176195Sbrian
180276195Sbrian	port->sc->setwin(port->sc, 0);
180376195Sbrian
180476195Sbrian	head = port->sc->gdata->cin;
180576195Sbrian	mem[head + 0] = cmd;
180676195Sbrian	mem[head + 1] = port->pnum;
180776195Sbrian	*(ushort *)(mem + head + 2) = op1;
180876195Sbrian
180976195Sbrian	head = (head + 4) & port->sc->gdata->cmax;
181076195Sbrian	port->sc->gdata->cin = head;
181176195Sbrian
181276195Sbrian	for (count = FEPTIMEOUT; count > 0; count--) {
181376195Sbrian		head = port->sc->gdata->cin;
181476195Sbrian		tail = port->sc->gdata->cout;
181576195Sbrian		n = (head - tail) & port->sc->gdata->cmax;
181676195Sbrian
181776195Sbrian		if (n <= ncmds * sizeof(short) * 4)
181876195Sbrian			break;
181976195Sbrian	}
182076195Sbrian	if (count == 0)
182176195Sbrian		log(LOG_ERR, "digi%d: port%d: timeout on FEP command\n",
182276195Sbrian		    port->sc->res.unit, port->pnum);
182376195Sbrian}
182476195Sbrian
182576195Sbrianconst char *
182676195Sbriandigi_errortxt(int id)
182776195Sbrian{
182876195Sbrian	static const char *error_desc[] = {
182976195Sbrian		"silo overflow",
183076195Sbrian		"interrupt-level buffer overflow",
183176195Sbrian		"tty-level buffer overflow",
183276195Sbrian	};
183376195Sbrian
183476195Sbrian	KASSERT(id >= 0 && id < sizeof(error_desc) / sizeof(error_desc[0]),
183576195Sbrian	    ("Unexpected digi error id %d\n", id));
183676195Sbrian
183776195Sbrian	return (error_desc[id]);
183876195Sbrian}
183976195Sbrian
184076195Sbrianint
184176195Sbriandigi_attach(struct digi_softc *sc)
184276195Sbrian{
184376195Sbrian	sc->res.ctldev = make_dev(&digi_sw,
184476195Sbrian	    (sc->res.unit << 16) | CTRL_DEV, UID_ROOT, GID_WHEEL,
184576195Sbrian	    0600, "digi%r.ctl", sc->res.unit);
184676195Sbrian
184776195Sbrian	digi_loadmoduledata(sc);
184876195Sbrian	digi_init(sc);
184976195Sbrian	digi_freemoduledata(sc);
185076195Sbrian
185176195Sbrian	return (0);
185276195Sbrian}
185376195Sbrian
185476195Sbrianstatic int
185576195Sbriandigi_inuse(struct digi_softc *sc)
185676195Sbrian{
185776195Sbrian	int i;
185876195Sbrian
185976195Sbrian	for (i = 0; i < sc->numports; i++)
186076195Sbrian		if (sc->ttys[i].t_state & TS_ISOPEN) {
186176195Sbrian			DLOG(DIGIDB_INIT, (sc->dev, "port%d: busy\n", i));
186276195Sbrian			return (1);
186376195Sbrian		} else if (sc->ports[i].wopeners || sc->ports[i].opencnt) {
186476195Sbrian			DLOG(DIGIDB_INIT, (sc->dev, "port%d: blocked in open\n",
186576195Sbrian			    i));
186676195Sbrian			return (1);
186776195Sbrian		}
186876195Sbrian	return (0);
186976195Sbrian}
187076195Sbrian
187176195Sbrianstatic void
187276195Sbriandigi_free_state(struct digi_softc *sc)
187376195Sbrian{
187476195Sbrian	int d, i;
187576195Sbrian
187676195Sbrian	/* Blow it all away */
187776195Sbrian
187876195Sbrian	for (i = 0; i < sc->numports; i++)
187976195Sbrian		for (d = 0; d < 6; d++)
188076195Sbrian			destroy_dev(sc->ports[i].dev[d]);
188176195Sbrian
188276195Sbrian	untimeout(digi_poll, sc, sc->callout);
188376195Sbrian	callout_handle_init(&sc->callout);
188476195Sbrian	untimeout(digi_int_test, sc, sc->inttest);
188576195Sbrian	callout_handle_init(&sc->inttest);
188676195Sbrian
188776195Sbrian	bus_teardown_intr(sc->dev, sc->res.irq, sc->res.irqHandler);
188876195Sbrian#ifdef DIGI_INTERRUPT
188976195Sbrian	if (sc->res.irq != NULL) {
189076195Sbrian		bus_release_resource(dev, SYS_RES_IRQ, sc->res.irqrid,
189176195Sbrian		    sc->res.irq);
189276195Sbrian		sc->res.irq = NULL;
189376195Sbrian	}
189476195Sbrian#endif
189576195Sbrian	if (sc->numports) {
189676195Sbrian		KASSERT(sc->ports, ("digi%d: Lost my ports ?", sc->res.unit));
189776195Sbrian		KASSERT(sc->ttys, ("digi%d: Lost my ttys ?", sc->res.unit));
189877004Sbrian		free(sc->ports, M_TTYS);
189976195Sbrian		sc->ports = NULL;
190077004Sbrian		free(sc->ttys, M_TTYS);
190176195Sbrian		sc->ttys = NULL;
190276195Sbrian		sc->numports = 0;
190376195Sbrian	}
190476195Sbrian
190576195Sbrian	sc->status = DIGI_STATUS_NOTINIT;
190676195Sbrian}
190776195Sbrian
190876195Sbrianint
190976195Sbriandigi_detach(device_t dev)
191076195Sbrian{
191176195Sbrian	struct digi_softc *sc = device_get_softc(dev);
191276195Sbrian
191376195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "detaching\n"));
191476195Sbrian
191576195Sbrian	/* If we're INIT'd, numports must be 0 */
191676195Sbrian	KASSERT(sc->numports == 0 || sc->status != DIGI_STATUS_NOTINIT,
191776195Sbrian	    ("digi%d: numports(%d) & status(%d) are out of sync",
191876195Sbrian	    sc->res.unit, sc->numports, (int)sc->status));
191976195Sbrian
192076195Sbrian	if (digi_inuse(sc))
192176195Sbrian		return (EBUSY);
192276195Sbrian
192376195Sbrian	digi_free_state(sc);
192476195Sbrian
192576195Sbrian	destroy_dev(makedev(CDEV_MAJOR,
192676195Sbrian	    (sc->res.unit << 16) | CTRL_DEV));
192776195Sbrian
192876195Sbrian	if (sc->res.mem != NULL) {
192976195Sbrian		bus_release_resource(dev, SYS_RES_MEMORY, sc->res.mrid,
193076195Sbrian		    sc->res.mem);
193176195Sbrian		sc->res.mem = NULL;
193276195Sbrian	}
193376195Sbrian	if (sc->res.io != NULL) {
193476195Sbrian		bus_release_resource(dev, SYS_RES_IOPORT, sc->res.iorid,
193576195Sbrian		    sc->res.io);
193676195Sbrian		sc->res.io = NULL;
193776195Sbrian	}
193876195Sbrian
193976195Sbrian	return (0);
194076195Sbrian}
194176195Sbrian
194276195Sbrianint
194376195Sbriandigi_shutdown(device_t dev)
194476195Sbrian{
194576195Sbrian	return (0);
194676195Sbrian}
194794320Sbrian
194894320SbrianMODULE_VERSION(digi, 1);
1949