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