digi.c revision 94340
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 94340 2002-04-10 03:13:28Z 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
22494340Sbrianstatic void
22594340Sbriandigi_delay(struct digi_softc *sc, const char *txt)
22694340Sbrian{
22794340Sbrian	if (cold)
22894340Sbrian		DELAY(5000);
22994340Sbrian	else
23094340Sbrian		tsleep(sc, PUSER | PCATCH, txt, 5);
23194340Sbrian}
23294340Sbrian
23376195Sbrianstatic int
23476195Sbriandigi_init(struct digi_softc *sc)
23576195Sbrian{
23676195Sbrian	int i, cnt, resp;
23776195Sbrian	u_char *ptr;
23876195Sbrian	int lowwater;
23976195Sbrian	struct digi_p *port;
24076195Sbrian	volatile struct board_chan *bc;
24176195Sbrian
24276195Sbrian	ptr = NULL;
24376195Sbrian
24476195Sbrian	if (sc->status == DIGI_STATUS_DISABLED) {
24576195Sbrian		log(LOG_ERR, "digi%d: Cannot init a disabled card\n",
24676195Sbrian		    sc->res.unit);
24776195Sbrian		return (EIO);
24876195Sbrian	}
24976195Sbrian	if (sc->bios.data == NULL) {
25076195Sbrian		log(LOG_ERR, "digi%d: Cannot init without BIOS\n",
25176195Sbrian		    sc->res.unit);
25276195Sbrian		return (EIO);
25376195Sbrian	}
25476195Sbrian#if 0
25576195Sbrian	if (sc->link.data == NULL && sc->model >= PCCX) {
25676195Sbrian		log(LOG_ERR, "digi%d: Cannot init without link info\n",
25776195Sbrian		    sc->res.unit);
25876195Sbrian		return (EIO);
25976195Sbrian	}
26076195Sbrian#endif
26176195Sbrian	if (sc->fep.data == NULL) {
26276195Sbrian		log(LOG_ERR, "digi%d: Cannot init without fep code\n",
26376195Sbrian		    sc->res.unit);
26476195Sbrian		return (EIO);
26576195Sbrian	}
26676195Sbrian	sc->status = DIGI_STATUS_NOTINIT;
26776195Sbrian
26876195Sbrian	if (sc->numports) {
26976195Sbrian		/*
27076195Sbrian		 * We're re-initialising - maybe because someone's attached
27176195Sbrian		 * another port module.  For now, we just re-initialise
27276195Sbrian		 * everything.
27376195Sbrian		 */
27476195Sbrian		if (digi_inuse(sc))
27576195Sbrian			return (EBUSY);
27676195Sbrian
27776195Sbrian		digi_free_state(sc);
27876195Sbrian	}
27976195Sbrian
28076195Sbrian	ptr = sc->setwin(sc, MISCGLOBAL);
28176195Sbrian	for (i = 0; i < 16; i += 2)
28276195Sbrian		vW(ptr + i) = 0;
28376195Sbrian
28476195Sbrian	switch (sc->model) {
28576195Sbrian	case PCXEVE:
28676195Sbrian		outb(sc->wport, 0xff);		/* window 7 */
28776195Sbrian		ptr = sc->vmem + (BIOSCODE & 0x1fff);
28876195Sbrian
28976195Sbrian		if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
29076195Sbrian			device_printf(sc->dev, "BIOS upload failed\n");
29176195Sbrian			return (EIO);
29276195Sbrian		}
29376195Sbrian
29476195Sbrian		outb(sc->port, FEPCLR);
29576195Sbrian		break;
29676195Sbrian
29776195Sbrian	case PCXE:
29876195Sbrian	case PCXI:
29976195Sbrian	case PCCX:
30076195Sbrian		ptr = sc->setwin(sc, BIOSCODE + ((0xf000 - sc->mem_seg) << 4));
30176195Sbrian		if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
30276195Sbrian			device_printf(sc->dev, "BIOS upload failed\n");
30376195Sbrian			return (EIO);
30476195Sbrian		}
30576195Sbrian		break;
30676195Sbrian
30776195Sbrian	case PCXEM:
30876195Sbrian	case PCIEPCX:
30976195Sbrian	case PCIXR:
31094323Sbrian		if (sc->pcibus)
31176195Sbrian			PCIPORT = FEPRST;
31276195Sbrian		else
31376195Sbrian			outb(sc->port, FEPRST | FEPMEM);
31476195Sbrian
31594323Sbrian		for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) &
31676195Sbrian		    FEPMASK) != FEPRST; i++) {
31776195Sbrian			if (i > 1000) {
31894323Sbrian				log(LOG_ERR, "digi%d: %s init reset failed\n",
31994323Sbrian				    sc->res.unit, sc->name);
32076195Sbrian				return (EIO);
32176195Sbrian			}
32294340Sbrian			digi_delay(sc, "digiinit0");
32376195Sbrian		}
32476195Sbrian		DLOG(DIGIDB_INIT, (sc->dev, "Got init reset after %d us\n", i));
32576195Sbrian
32676195Sbrian		/* Now upload the BIOS */
32776195Sbrian		cnt = (sc->bios.size < sc->win_size - BIOSOFFSET) ?
32876195Sbrian		    sc->bios.size : sc->win_size - BIOSOFFSET;
32976195Sbrian
33076195Sbrian		ptr = sc->setwin(sc, BIOSOFFSET);
33176195Sbrian		if (!digi_bcopy(sc->bios.data, ptr, cnt)) {
33276195Sbrian			device_printf(sc->dev, "BIOS upload (1) failed\n");
33376195Sbrian			return (EIO);
33476195Sbrian		}
33576195Sbrian
33676195Sbrian		if (cnt != sc->bios.size) {
33776195Sbrian			/* and the second part */
33876195Sbrian			ptr = sc->setwin(sc, sc->win_size);
33976195Sbrian			if (!digi_bcopy(sc->bios.data + cnt, ptr,
34076195Sbrian			    sc->bios.size - cnt)) {
34176195Sbrian				device_printf(sc->dev, "BIOS upload failed\n");
34276195Sbrian				return (EIO);
34376195Sbrian			}
34476195Sbrian		}
34576195Sbrian
34676195Sbrian		ptr = sc->setwin(sc, 0);
34776195Sbrian		vW(ptr + 0) = 0x0401;
34876195Sbrian		vW(ptr + 2) = 0x0bf0;
34976195Sbrian		vW(ptr + 4) = 0x0000;
35076195Sbrian		vW(ptr + 6) = 0x0000;
35176195Sbrian
35276195Sbrian		break;
35376195Sbrian	}
35476195Sbrian
35576195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "BIOS uploaded\n"));
35676195Sbrian
35776195Sbrian	ptr = sc->setwin(sc, MISCGLOBAL);
35876195Sbrian	W(ptr) = 0;
35976195Sbrian
36094323Sbrian	if (sc->pcibus) {
36176195Sbrian		PCIPORT = FEPCLR;
36276195Sbrian		resp = FEPRST;
36376195Sbrian	} else if (sc->model == PCXEVE) {
36476195Sbrian		outb(sc->port, FEPCLR);
36576195Sbrian		resp = FEPRST;
36676195Sbrian	} else {
36776195Sbrian		outb(sc->port, FEPCLR | FEPMEM);
36876195Sbrian		resp = FEPRST | FEPMEM;
36976195Sbrian	}
37076195Sbrian
37194323Sbrian	for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) & FEPMASK)
37276195Sbrian	    == resp; i++) {
37376195Sbrian		if (i > 1000) {
37476195Sbrian			log(LOG_ERR, "digi%d: BIOS start failed\n",
37576195Sbrian			    sc->res.unit);
37676195Sbrian			return (EIO);
37776195Sbrian		}
37894340Sbrian		digi_delay(sc, "digibios0");
37976195Sbrian	}
38076195Sbrian
38176195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "BIOS started after %d us\n", i));
38276195Sbrian
38376195Sbrian	for (i = 0; vW(ptr) != *(u_short *)"GD"; i++) {
38476195Sbrian		if (i > 2000) {
38576195Sbrian			log(LOG_ERR, "digi%d: BIOS boot failed "
38676195Sbrian			    "(0x%02x != 0x%02x)\n",
38776195Sbrian			    sc->res.unit, vW(ptr), *(u_short *)"GD");
38876195Sbrian			return (EIO);
38976195Sbrian		}
39094340Sbrian		digi_delay(sc, "digibios1");
39176195Sbrian	}
39276195Sbrian
39376195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "BIOS booted after %d iterations\n", i));
39476195Sbrian
39576195Sbrian	if (sc->link.data != NULL) {
39676195Sbrian		DLOG(DIGIDB_INIT, (sc->dev, "Loading link data\n"));
39776195Sbrian		ptr = sc->setwin(sc, 0xcd0);
39876195Sbrian		digi_bcopy(sc->link.data, ptr, 21);	/* XXX 21 ? */
39976195Sbrian	}
40076195Sbrian
40176195Sbrian	/* load FEP/OS */
40276195Sbrian
40376195Sbrian	switch (sc->model) {
40476195Sbrian	case PCXE:
40576195Sbrian	case PCXEVE:
40676195Sbrian	case PCXI:
40776195Sbrian		ptr = sc->setwin(sc, sc->model == PCXI ? 0x2000 : 0x0);
40876195Sbrian		digi_bcopy(sc->fep.data, ptr, sc->fep.size);
40976195Sbrian
41076195Sbrian		/* A BIOS request to move our data to 0x2000 */
41176195Sbrian		ptr = sc->setwin(sc, MBOX);
41276195Sbrian		vW(ptr + 0) = 2;
41376195Sbrian		vW(ptr + 2) = sc->mem_seg + FEPCODESEG;
41476195Sbrian		vW(ptr + 4) = 0;
41576195Sbrian		vW(ptr + 6) = FEPCODESEG;
41676195Sbrian		vW(ptr + 8) = 0;
41776195Sbrian		vW(ptr + 10) = sc->fep.size;
41876195Sbrian
41976195Sbrian		/* Run the BIOS request */
42076195Sbrian		outb(sc->port, FEPREQ | FEPMEM);
42176195Sbrian		outb(sc->port, FEPCLR | FEPMEM);
42276195Sbrian
42376195Sbrian		for (i = 0; W(ptr); i++) {
42476195Sbrian			if (i > 10) {
42576195Sbrian				log(LOG_ERR, "digi%d: FEP/OS move failed\n",
42676195Sbrian				    sc->res.unit);
42776195Sbrian				sc->hidewin(sc);
42876195Sbrian				return (EIO);
42976195Sbrian			}
43094340Sbrian			digi_delay(sc, "digifep0");
43176195Sbrian		}
43276195Sbrian		DLOG(DIGIDB_INIT,
43376195Sbrian		    (sc->dev, "FEP/OS moved after %d iterations\n", i));
43476195Sbrian
43576195Sbrian		/* Clear the confirm word */
43676195Sbrian		ptr = sc->setwin(sc, FEPSTAT);
43776195Sbrian		vW(ptr + 0) = 0;
43876195Sbrian
43976195Sbrian		/* A BIOS request to execute the FEP/OS */
44076195Sbrian		ptr = sc->setwin(sc, MBOX);
44176195Sbrian		vW(ptr + 0) = 0x01;
44276195Sbrian		vW(ptr + 2) = FEPCODESEG;
44376195Sbrian		vW(ptr + 4) = 0x04;
44476195Sbrian
44576195Sbrian		/* Run the BIOS request */
44676195Sbrian		outb(sc->port, FEPREQ);
44776195Sbrian		outb(sc->port, FEPCLR);
44876195Sbrian
44976195Sbrian		ptr = sc->setwin(sc, FEPSTAT);
45076195Sbrian
45176195Sbrian		break;
45276195Sbrian
45376195Sbrian	case PCXEM:
45476195Sbrian	case PCIEPCX:
45576195Sbrian	case PCIXR:
45676195Sbrian		DLOG(DIGIDB_INIT, (sc->dev, "Loading FEP/OS\n"));
45776195Sbrian
45876195Sbrian		cnt = (sc->fep.size < sc->win_size - BIOSOFFSET) ?
45976195Sbrian		    sc->fep.size : sc->win_size - BIOSOFFSET;
46076195Sbrian
46176195Sbrian		ptr = sc->setwin(sc, BIOSOFFSET);
46276195Sbrian		digi_bcopy(sc->fep.data, ptr, cnt);
46376195Sbrian
46476195Sbrian		if (cnt != sc->fep.size) {
46576195Sbrian			ptr = sc->setwin(sc, BIOSOFFSET + cnt);
46676195Sbrian			digi_bcopy(sc->fep.data + cnt, ptr,
46776195Sbrian			    sc->fep.size - cnt);
46876195Sbrian		}
46976195Sbrian
47076195Sbrian		DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS loaded\n"));
47176195Sbrian
47276195Sbrian		ptr = sc->setwin(sc, 0xc30);
47376195Sbrian		W(ptr + 4) = 0x1004;
47476195Sbrian		W(ptr + 6) = 0xbfc0;
47576195Sbrian		W(ptr + 0) = 0x03;
47676195Sbrian		W(ptr + 2) = 0x00;
47776195Sbrian
47876195Sbrian		/* Clear the confirm word */
47976195Sbrian		ptr = sc->setwin(sc, FEPSTAT);
48076195Sbrian		W(ptr + 0) = 0;
48176195Sbrian
48276195Sbrian		if (sc->port)
48376195Sbrian			outb(sc->port, 0);		/* XXX necessary ? */
48476195Sbrian
48576195Sbrian		break;
48676195Sbrian
48776195Sbrian	case PCCX:
48876195Sbrian		ptr = sc->setwin(sc, 0xd000);
48976195Sbrian		digi_bcopy(sc->fep.data, ptr, sc->fep.size);
49076195Sbrian
49176195Sbrian		/* A BIOS request to execute the FEP/OS */
49276195Sbrian		ptr = sc->setwin(sc, 0xc40);
49376195Sbrian		W(ptr + 0) = 1;
49476195Sbrian		W(ptr + 2) = FEPCODE >> 4;
49576195Sbrian		W(ptr + 4) = 4;
49676195Sbrian
49776195Sbrian		/* Clear the confirm word */
49876195Sbrian		ptr = sc->setwin(sc, FEPSTAT);
49976195Sbrian		W(ptr + 0) = 0;
50076195Sbrian
50176195Sbrian		/* Run the BIOS request */
50276195Sbrian		outb(sc->port, FEPREQ | FEPMEM); /* send interrupt to BIOS */
50376195Sbrian		outb(sc->port, FEPCLR | FEPMEM);
50476195Sbrian		break;
50576195Sbrian	}
50676195Sbrian
50776195Sbrian	/* Now wait 'till the FEP/OS has booted */
50876195Sbrian	for (i = 0; vW(ptr) != *(u_short *)"OS"; i++) {
50976195Sbrian		if (i > 2000) {
51076195Sbrian			log(LOG_ERR, "digi%d: FEP/OS start failed "
51176195Sbrian			    "(0x%02x != 0x%02x)\n",
51276195Sbrian			    sc->res.unit, vW(ptr), *(u_short *)"OS");
51376195Sbrian			sc->hidewin(sc);
51476195Sbrian			return (EIO);
51576195Sbrian		}
51694340Sbrian		digi_delay(sc, "digifep1");
51776195Sbrian	}
51876195Sbrian
51976195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS started after %d iterations\n", i));
52076195Sbrian
52176195Sbrian	if (sc->model >= PCXEM) {
52276195Sbrian		ptr = sc->setwin(sc, 0xe04);
52376195Sbrian		vW(ptr) = 2;
52476195Sbrian		ptr = sc->setwin(sc, 0xc02);
52576195Sbrian		sc->numports = vW(ptr);
52676195Sbrian	} else {
52776195Sbrian		ptr = sc->setwin(sc, 0xc22);
52876195Sbrian		sc->numports = vW(ptr);
52976195Sbrian	}
53076195Sbrian
53176195Sbrian	if (sc->numports == 0) {
53276195Sbrian		device_printf(sc->dev, "%s, 0 ports found\n", sc->name);
53376195Sbrian		sc->hidewin(sc);
53476195Sbrian		return (0);
53576195Sbrian	}
53676195Sbrian
53776195Sbrian	if (sc->numports > 256) {
53876195Sbrian		/* Our minor numbering scheme is broken for more than 256 */
53976195Sbrian		device_printf(sc->dev, "%s, 256 ports (%d ports found)\n",
54076195Sbrian		    sc->name, sc->numports);
54176195Sbrian		sc->numports = 256;
54276195Sbrian	} else
54376195Sbrian		device_printf(sc->dev, "%s, %d ports found\n", sc->name,
54476195Sbrian		    sc->numports);
54576195Sbrian
54676195Sbrian	if (sc->ports)
54776195Sbrian		free(sc->ports, M_TTYS);
54877004Sbrian	sc->ports = malloc(sizeof(struct digi_p) * sc->numports,
54987503Sarr	    M_TTYS, M_WAITOK | M_ZERO);
55076195Sbrian
55176195Sbrian	if (sc->ttys)
55276195Sbrian		free(sc->ttys, M_TTYS);
55377004Sbrian	sc->ttys = malloc(sizeof(struct tty) * sc->numports,
55487503Sarr	    M_TTYS, M_WAITOK | M_ZERO);
55576195Sbrian
55676195Sbrian	/*
55776195Sbrian	 * XXX Should read port 0xc90 for an array of 2byte values, 1 per
55876195Sbrian	 * port.  If the value is 0, the port is broken....
55976195Sbrian	 */
56076195Sbrian
56176195Sbrian	ptr = sc->setwin(sc, 0);
56276195Sbrian
56376195Sbrian	/* We should now init per-port structures */
56476195Sbrian	bc = (volatile struct board_chan *)(ptr + CHANSTRUCT);
56576195Sbrian	sc->gdata = (volatile struct global_data *)(ptr + FEP_GLOBAL);
56676195Sbrian
56776195Sbrian	sc->memcmd = ptr + sc->gdata->cstart;
56876195Sbrian	sc->memevent = ptr + sc->gdata->istart;
56976195Sbrian
57076195Sbrian	for (i = 0; i < sc->numports; i++, bc++) {
57176195Sbrian		port = sc->ports + i;
57276195Sbrian		port->pnum = i;
57376195Sbrian		port->sc = sc;
57476195Sbrian		port->status = ENABLED;
57576195Sbrian		port->tp = sc->ttys + i;
57676195Sbrian		port->bc = bc;
57776195Sbrian
57876195Sbrian		if (sc->model == PCXEVE) {
57976195Sbrian			port->txbuf = ptr +
58076195Sbrian			    (((bc->tseg - sc->mem_seg) << 4) & 0x1fff);
58176195Sbrian			port->rxbuf = ptr +
58276195Sbrian			    (((bc->rseg - sc->mem_seg) << 4) & 0x1fff);
58376195Sbrian			port->txwin = FEPWIN | ((bc->tseg - sc->mem_seg) >> 9);
58476195Sbrian			port->rxwin = FEPWIN | ((bc->rseg - sc->mem_seg) >> 9);
58576195Sbrian		} else if (sc->model == PCXI || sc->model == PCXE) {
58676195Sbrian			port->txbuf = ptr + ((bc->tseg - sc->mem_seg) << 4);
58776195Sbrian			port->rxbuf = ptr + ((bc->rseg - sc->mem_seg) << 4);
58876195Sbrian			port->txwin = port->rxwin = 0;
58976195Sbrian		} else {
59076195Sbrian			port->txbuf = ptr +
59176195Sbrian			    (((bc->tseg - sc->mem_seg) << 4) % sc->win_size);
59276195Sbrian			port->rxbuf = ptr +
59376195Sbrian			    (((bc->rseg - sc->mem_seg) << 4) % sc->win_size);
59476195Sbrian			port->txwin = FEPWIN |
59576195Sbrian			    (((bc->tseg - sc->mem_seg) << 4) / sc->win_size);
59676195Sbrian			port->rxwin = FEPWIN |
59776195Sbrian			    (((bc->rseg - sc->mem_seg) << 4) / sc->win_size);
59876195Sbrian		}
59976195Sbrian		port->txbufsize = bc->tmax + 1;
60076195Sbrian		port->rxbufsize = bc->rmax + 1;
60176195Sbrian
60276195Sbrian		lowwater = port->txbufsize >> 2;
60376195Sbrian		if (lowwater > 1024)
60476195Sbrian			lowwater = 1024;
60576195Sbrian		sc->setwin(sc, 0);
60676195Sbrian		fepcmd_w(port, STXLWATER, lowwater, 10);
60776195Sbrian		fepcmd_w(port, SRXLWATER, port->rxbufsize >> 2, 10);
60876195Sbrian		fepcmd_w(port, SRXHWATER, (3 * port->rxbufsize) >> 2, 10);
60976195Sbrian
61076195Sbrian		bc->edelay = 100;
61176195Sbrian		port->dtr_wait = 3 * hz;
61276195Sbrian
61376195Sbrian		/*
61476195Sbrian		 * We don't use all the flags from <sys/ttydefaults.h> since
61576195Sbrian		 * they are only relevant for logins.  It's important to have
61676195Sbrian		 * echo off initially so that the line doesn't start blathering
61776195Sbrian		 * before the echo flag can be turned off.
61876195Sbrian		 */
61976195Sbrian		port->it_in.c_iflag = 0;
62076195Sbrian		port->it_in.c_oflag = 0;
62176195Sbrian		port->it_in.c_cflag = TTYDEF_CFLAG;
62276195Sbrian		port->it_in.c_lflag = 0;
62376195Sbrian		termioschars(&port->it_in);
62476195Sbrian		port->it_in.c_ispeed = port->it_in.c_ospeed = digidefaultrate;
62576195Sbrian		port->it_out = port->it_in;
62676195Sbrian		port->send_ring = 1;	/* Default action on signal RI */
62776195Sbrian
62876195Sbrian		port->dev[0] = make_dev(&digi_sw, (sc->res.unit << 16) + i,
62976195Sbrian		    UID_ROOT, GID_WHEEL, 0600, "ttyD%d.%d", sc->res.unit, i);
63076195Sbrian		port->dev[1] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
63176195Sbrian		    CONTROL_INIT_STATE, UID_ROOT, GID_WHEEL,
63276195Sbrian		    0600, "ttyiD%d.%d", sc->res.unit, i);
63376195Sbrian		port->dev[2] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
63476195Sbrian		    CONTROL_LOCK_STATE, UID_ROOT, GID_WHEEL,
63576195Sbrian		    0600, "ttylD%d.%d", sc->res.unit, i);
63676195Sbrian		port->dev[3] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
63776195Sbrian		    CALLOUT_MASK, UID_UUCP, GID_DIALER,
63876195Sbrian		    0660, "cuaD%d.%d", sc->res.unit, i);
63976195Sbrian		port->dev[4] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
64076195Sbrian		    CALLOUT_MASK | CONTROL_INIT_STATE, UID_UUCP, GID_DIALER,
64176195Sbrian		    0660, "cuaiD%d.%d", sc->res.unit, i);
64276195Sbrian		port->dev[5] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
64376195Sbrian		    CALLOUT_MASK | CONTROL_LOCK_STATE, UID_UUCP, GID_DIALER,
64476195Sbrian		    0660, "cualD%d.%d", sc->res.unit, i);
64576195Sbrian	}
64676195Sbrian
64776195Sbrian	sc->hidewin(sc);
64876195Sbrian	sc->inttest = timeout(digi_int_test, sc, hz);
64976195Sbrian	/* fepcmd_w(&sc->ports[0], 0xff, 0, 0); */
65076195Sbrian	sc->status = DIGI_STATUS_ENABLED;
65176195Sbrian
65276195Sbrian	return (0);
65376195Sbrian}
65476195Sbrian
65576195Sbrianstatic int
65676195Sbriandigimctl(struct digi_p *port, int bits, int how)
65776195Sbrian{
65876195Sbrian	int mstat;
65976195Sbrian
66076195Sbrian	if (how == DMGET) {
66176195Sbrian		port->sc->setwin(port->sc, 0);
66276195Sbrian		mstat = port->bc->mstat;
66376195Sbrian		port->sc->hidewin(port->sc);
66476195Sbrian		bits = TIOCM_LE;
66576195Sbrian		if (mstat & port->sc->csigs->rts)
66676195Sbrian			bits |= TIOCM_RTS;
66778496Sbrian		if (mstat & port->cd)
66876195Sbrian			bits |= TIOCM_CD;
66978496Sbrian		if (mstat & port->dsr)
67076195Sbrian			bits |= TIOCM_DSR;
67176195Sbrian		if (mstat & port->sc->csigs->cts)
67276195Sbrian			bits |= TIOCM_CTS;
67376195Sbrian		if (mstat & port->sc->csigs->ri)
67476195Sbrian			bits |= TIOCM_RI;
67576195Sbrian		if (mstat & port->sc->csigs->dtr)
67676195Sbrian			bits |= TIOCM_DTR;
67776195Sbrian		return (bits);
67876195Sbrian	}
67976195Sbrian
68076195Sbrian	/* Only DTR and RTS may be set */
68176195Sbrian	mstat = 0;
68276195Sbrian	if (bits & TIOCM_DTR)
68376195Sbrian		mstat |= port->sc->csigs->dtr;
68476195Sbrian	if (bits & TIOCM_RTS)
68576195Sbrian		mstat |= port->sc->csigs->rts;
68676195Sbrian
68776195Sbrian	switch (how) {
68876195Sbrian	case DMSET:
68976195Sbrian		fepcmd_b(port, SETMODEM, mstat, ~mstat, 0);
69076195Sbrian		break;
69176195Sbrian	case DMBIS:
69276195Sbrian		fepcmd_b(port, SETMODEM, mstat, 0, 0);
69376195Sbrian		break;
69476195Sbrian	case DMBIC:
69576195Sbrian		fepcmd_b(port, SETMODEM, 0, mstat, 0);
69676195Sbrian		break;
69776195Sbrian	}
69876195Sbrian
69976195Sbrian	return (0);
70076195Sbrian}
70176195Sbrian
70276195Sbrianstatic void
70376195Sbriandigi_disc_optim(struct tty *tp, struct termios *t, struct digi_p *port)
70476195Sbrian{
70576195Sbrian	if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP)) &&
70676195Sbrian	    (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) &&
70776195Sbrian	    (!(t->c_iflag & PARMRK) ||
70876195Sbrian	    (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) &&
70976195Sbrian	    !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) &&
71076195Sbrian	    linesw[tp->t_line].l_rint == ttyinput)
71176195Sbrian		tp->t_state |= TS_CAN_BYPASS_L_RINT;
71276195Sbrian	else
71376195Sbrian		tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
71476195Sbrian}
71576195Sbrian
71676195Sbrianint
71783366Sjuliandigiopen(dev_t dev, int flag, int mode, struct thread *td)
71876195Sbrian{
71976195Sbrian	struct digi_softc *sc;
72076195Sbrian	struct tty *tp;
72176195Sbrian	int unit;
72276195Sbrian	int pnum;
72376195Sbrian	struct digi_p *port;
72476195Sbrian	int s;
72576195Sbrian	int error, mynor;
72676195Sbrian	volatile struct board_chan *bc;
72776195Sbrian
72876195Sbrian	error = 0;
72976195Sbrian	mynor = minor(dev);
73076195Sbrian	unit = MINOR_TO_UNIT(minor(dev));
73176195Sbrian	pnum = MINOR_TO_PORT(minor(dev));
73276195Sbrian
73376195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
73476195Sbrian	if (!sc)
73576195Sbrian		return (ENXIO);
73676195Sbrian
73776195Sbrian	if (sc->status != DIGI_STATUS_ENABLED) {
73876195Sbrian		DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
73976195Sbrian		return (ENXIO);
74076195Sbrian	}
74176195Sbrian	if (pnum >= sc->numports) {
74276195Sbrian		DLOG(DIGIDB_OPEN, (sc->dev, "port%d: Doesn't exist\n", pnum));
74376195Sbrian		return (ENXIO);
74476195Sbrian	}
74576195Sbrian	if (mynor & (CTRL_DEV | CONTROL_MASK)) {
74676195Sbrian		sc->opencnt++;
74776195Sbrian		return (0);
74876195Sbrian	}
74976195Sbrian	port = &sc->ports[pnum];
75076195Sbrian	tp = dev->si_tty = port->tp;
75176195Sbrian	bc = port->bc;
75276195Sbrian
75376195Sbrian	s = spltty();
75476195Sbrian
75576195Sbrianopen_top:
75676195Sbrian	while (port->status & DIGI_DTR_OFF) {
75776195Sbrian		port->wopeners++;
75876195Sbrian		error = tsleep(&port->dtr_wait, TTIPRI | PCATCH, "digidtr", 0);
75976195Sbrian		port->wopeners--;
76076195Sbrian		if (error)
76176195Sbrian			goto out;
76276195Sbrian	}
76376195Sbrian
76476195Sbrian	if (tp->t_state & TS_ISOPEN) {
76576195Sbrian		/*
76676195Sbrian		 * The device is open, so everything has been initialized.
76776195Sbrian		 * Handle conflicts.
76876195Sbrian		 */
76976195Sbrian		if (mynor & CALLOUT_MASK) {
77076195Sbrian			if (!port->active_out) {
77176195Sbrian				error = EBUSY;
77276195Sbrian				DLOG(DIGIDB_OPEN, (sc->dev, "port %d:"
77376195Sbrian				    " BUSY error = %d\n", pnum, error));
77476195Sbrian				goto out;
77576195Sbrian			}
77676195Sbrian		} else if (port->active_out) {
77776195Sbrian			if (flag & O_NONBLOCK) {
77876195Sbrian				error = EBUSY;
77976195Sbrian				DLOG(DIGIDB_OPEN, (sc->dev,
78076195Sbrian				    "port %d: BUSY error = %d\n", pnum, error));
78176195Sbrian				goto out;
78276195Sbrian			}
78376195Sbrian			port->wopeners++;
78476195Sbrian			error = tsleep(&port->active_out, TTIPRI | PCATCH,
78576195Sbrian			    "digibi", 0);
78676195Sbrian			port->wopeners--;
78776195Sbrian			if (error != 0) {
78876195Sbrian				DLOG(DIGIDB_OPEN, (sc->dev,
78976195Sbrian				    "port %d: tsleep(digibi) error = %d\n",
79076195Sbrian				    pnum, error));
79176195Sbrian				goto out;
79276195Sbrian			}
79376195Sbrian			goto open_top;
79476195Sbrian		}
79593593Sjhb		if (tp->t_state & TS_XCLUDE && suser(td) != 0) {
79676195Sbrian			error = EBUSY;
79776195Sbrian			goto out;
79876195Sbrian		}
79976195Sbrian	} else {
80076195Sbrian		/*
80176195Sbrian		 * The device isn't open, so there are no conflicts.
80276195Sbrian		 * Initialize it.  Initialization is done twice in many
80376195Sbrian		 * cases: to preempt sleeping callin opens if we are callout,
80476195Sbrian		 * and to complete a callin open after DCD rises.
80576195Sbrian		 */
80676195Sbrian		tp->t_oproc = digistart;
80776195Sbrian		tp->t_param = digiparam;
80876195Sbrian		tp->t_stop = digistop;
80976195Sbrian		tp->t_dev = dev;
81076195Sbrian		tp->t_termios = (mynor & CALLOUT_MASK) ?
81176195Sbrian		    port->it_out : port->it_in;
81276195Sbrian		sc->setwin(sc, 0);
81376195Sbrian
81476195Sbrian		bc->rout = bc->rin;	/* clear input queue */
81576195Sbrian		bc->idata = 1;
81676195Sbrian		bc->iempty = 1;
81776195Sbrian		bc->ilow = 1;
81878496Sbrian		bc->mint = port->cd | port->sc->csigs->ri;
81976195Sbrian		bc->tin = bc->tout;
82078496Sbrian		if (port->ialtpin) {
82178496Sbrian			port->cd = sc->csigs->dsr;
82278496Sbrian			port->dsr = sc->csigs->cd;
82378496Sbrian		} else {
82478496Sbrian			port->cd = sc->csigs->cd;
82578496Sbrian			port->dsr = sc->csigs->dsr;
82678496Sbrian		}
82776195Sbrian		port->wopeners++;			/* XXX required ? */
82876195Sbrian		error = digiparam(tp, &tp->t_termios);
82976195Sbrian		port->wopeners--;
83076195Sbrian
83176195Sbrian		if (error != 0) {
83276195Sbrian			DLOG(DIGIDB_OPEN, (sc->dev,
83376195Sbrian			    "port %d: cxpparam error = %d\n", pnum, error));
83476195Sbrian			goto out;
83576195Sbrian		}
83676195Sbrian		ttsetwater(tp);
83776195Sbrian
83876195Sbrian		/* handle fake and initial DCD for callout devices */
83976195Sbrian
84078496Sbrian		if (bc->mstat & port->cd || mynor & CALLOUT_MASK)
84176195Sbrian			linesw[tp->t_line].l_modem(tp, 1);
84276195Sbrian	}
84376195Sbrian
84476195Sbrian	/* Wait for DCD if necessary */
84576195Sbrian	if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) &&
84676195Sbrian	    !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
84776195Sbrian		port->wopeners++;
84876195Sbrian		error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "digidcd", 0);
84976195Sbrian		port->wopeners--;
85076195Sbrian		if (error != 0) {
85176195Sbrian			DLOG(DIGIDB_OPEN, (sc->dev,
85276195Sbrian			    "port %d: tsleep(digidcd) error = %d\n",
85376195Sbrian			    pnum, error));
85476195Sbrian			goto out;
85576195Sbrian		}
85676195Sbrian		goto open_top;
85776195Sbrian	}
85876195Sbrian	error = linesw[tp->t_line].l_open(dev, tp);
85976195Sbrian	DLOG(DIGIDB_OPEN, (sc->dev, "port %d: l_open error = %d\n",
86076195Sbrian	    pnum, error));
86176195Sbrian
86276195Sbrian	digi_disc_optim(tp, &tp->t_termios, port);
86376195Sbrian
86476195Sbrian	if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
86576195Sbrian		port->active_out = TRUE;
86676195Sbrian
86776195Sbrian	if (tp->t_state & TS_ISOPEN)
86876195Sbrian		sc->opencnt++;
86976195Sbrianout:
87076195Sbrian	splx(s);
87176195Sbrian
87276195Sbrian	if (!(tp->t_state & TS_ISOPEN))
87376195Sbrian		digihardclose(port);
87476195Sbrian
87576195Sbrian	DLOG(DIGIDB_OPEN, (sc->dev, "port %d: open() returns %d\n",
87676195Sbrian	    pnum, error));
87776195Sbrian
87876195Sbrian	return (error);
87976195Sbrian}
88076195Sbrian
88176195Sbrianint
88283366Sjuliandigiclose(dev_t dev, int flag, int mode, struct thread *td)
88376195Sbrian{
88476195Sbrian	int mynor;
88576195Sbrian	struct tty *tp;
88676195Sbrian	int unit, pnum;
88776195Sbrian	struct digi_softc *sc;
88876195Sbrian	struct digi_p *port;
88976195Sbrian	int s;
89076195Sbrian
89176195Sbrian	mynor = minor(dev);
89276195Sbrian	unit = MINOR_TO_UNIT(mynor);
89376195Sbrian	pnum = MINOR_TO_PORT(mynor);
89476195Sbrian
89576195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
89676195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
89776195Sbrian
89876195Sbrian	if (mynor & (CTRL_DEV | CONTROL_MASK)) {
89976195Sbrian		sc->opencnt--;
90076195Sbrian		return (0);
90176195Sbrian	}
90276195Sbrian
90376195Sbrian	port = sc->ports + pnum;
90476195Sbrian	tp = port->tp;
90576195Sbrian
90676195Sbrian	DLOG(DIGIDB_CLOSE, (sc->dev, "port %d: closing\n", pnum));
90776195Sbrian
90876195Sbrian	s = spltty();
90976195Sbrian	linesw[tp->t_line].l_close(tp, flag);
91076195Sbrian	digi_disc_optim(tp, &tp->t_termios, port);
91176195Sbrian	digistop(tp, FREAD | FWRITE);
91276195Sbrian	digihardclose(port);
91376195Sbrian	ttyclose(tp);
91476195Sbrian	if (--sc->opencnt == 0)
91576195Sbrian		splx(s);
91676195Sbrian	return (0);
91776195Sbrian}
91876195Sbrian
91976195Sbrianstatic void
92076195Sbriandigidtrwakeup(void *chan)
92176195Sbrian{
92276195Sbrian	struct digi_p *port = chan;
92376195Sbrian
92476195Sbrian	port->status &= ~DIGI_DTR_OFF;
92576195Sbrian	wakeup(&port->dtr_wait);
92676195Sbrian	port->wopeners--;
92776195Sbrian}
92876195Sbrian
92976195Sbrianstatic void
93076195Sbriandigihardclose(struct digi_p *port)
93176195Sbrian{
93276195Sbrian	volatile struct board_chan *bc;
93376195Sbrian	int s;
93476195Sbrian
93576195Sbrian	bc = port->bc;
93676195Sbrian
93776195Sbrian	s = spltty();
93876195Sbrian	port->sc->setwin(port->sc, 0);
93976195Sbrian	bc->idata = 0;
94076195Sbrian	bc->iempty = 0;
94176195Sbrian	bc->ilow = 0;
94276195Sbrian	bc->mint = 0;
94376195Sbrian	if ((port->tp->t_cflag & HUPCL) ||
94478496Sbrian	    (!port->active_out && !(bc->mstat & port->cd) &&
94576195Sbrian	    !(port->it_in.c_cflag & CLOCAL)) ||
94676195Sbrian	    !(port->tp->t_state & TS_ISOPEN)) {
94776195Sbrian		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
94876195Sbrian		if (port->dtr_wait != 0) {
94976195Sbrian			/* Schedule a wakeup of any callin devices */
95076195Sbrian			port->wopeners++;
95176195Sbrian			timeout(&digidtrwakeup, port, port->dtr_wait);
95276195Sbrian			port->status |= DIGI_DTR_OFF;
95376195Sbrian		}
95476195Sbrian	}
95576195Sbrian	port->active_out = FALSE;
95676195Sbrian	wakeup(&port->active_out);
95776195Sbrian	wakeup(TSA_CARR_ON(port->tp));
95876195Sbrian	splx(s);
95976195Sbrian}
96076195Sbrian
96176195Sbrianint
96276195Sbriandigiread(dev_t dev, struct uio *uio, int flag)
96376195Sbrian{
96476195Sbrian	int mynor;
96576195Sbrian	struct tty *tp;
96676195Sbrian	int error, unit, pnum;
96776195Sbrian	struct digi_softc *sc;
96876195Sbrian
96976195Sbrian	mynor = minor(dev);
97076195Sbrian	if (mynor & CONTROL_MASK)
97176195Sbrian		return (ENODEV);
97276195Sbrian
97376195Sbrian	unit = MINOR_TO_UNIT(mynor);
97476195Sbrian	pnum = MINOR_TO_PORT(mynor);
97576195Sbrian
97676195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
97776195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
97876195Sbrian	tp = &sc->ttys[pnum];
97976195Sbrian
98076195Sbrian	error = linesw[tp->t_line].l_read(tp, uio, flag);
98176195Sbrian	DLOG(DIGIDB_READ, (sc->dev, "port %d: read() returns %d\n",
98276195Sbrian	    pnum, error));
98376195Sbrian
98476195Sbrian	return (error);
98576195Sbrian}
98676195Sbrian
98776195Sbrianint
98876195Sbriandigiwrite(dev_t dev, struct uio *uio, int flag)
98976195Sbrian{
99076195Sbrian	int mynor;
99176195Sbrian	struct tty *tp;
99276195Sbrian	int error, unit, pnum;
99376195Sbrian	struct digi_softc *sc;
99476195Sbrian
99576195Sbrian	mynor = minor(dev);
99676195Sbrian	if (mynor & CONTROL_MASK)
99776195Sbrian		return (ENODEV);
99876195Sbrian
99976195Sbrian	unit = MINOR_TO_UNIT(mynor);
100076195Sbrian	pnum = MINOR_TO_PORT(mynor);
100176195Sbrian
100276195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
100376195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
100476195Sbrian	tp = &sc->ttys[pnum];
100576195Sbrian
100676195Sbrian	error = linesw[tp->t_line].l_write(tp, uio, flag);
100776195Sbrian	DLOG(DIGIDB_WRITE, (sc->dev, "port %d: write() returns %d\n",
100876195Sbrian	    pnum, error));
100976195Sbrian
101076195Sbrian	return (error);
101176195Sbrian}
101276195Sbrian
101376195Sbrian/*
101476195Sbrian * Load module "digi_<mod>.ko" and look for a symbol called digi_mod_<mod>.
101576195Sbrian *
101676195Sbrian * Populate sc->bios, sc->fep, and sc->link from this data.
101776195Sbrian *
101876195Sbrian * sc->fep.data, sc->bios.data and sc->link.data are malloc()d according
101976195Sbrian * to their respective sizes.
102076195Sbrian *
102176195Sbrian * The module is unloaded when we're done.
102276195Sbrian */
102376195Sbrianstatic int
102476195Sbriandigi_loadmoduledata(struct digi_softc *sc)
102576195Sbrian{
102676195Sbrian	struct digi_mod *digi_mod;
102776195Sbrian	linker_file_t lf;
102876195Sbrian	char *modfile, *sym;
102976195Sbrian	caddr_t symptr;
103078414Sbrian	int modlen, res;
103176195Sbrian
103276195Sbrian	KASSERT(sc->bios.data == NULL, ("Uninitialised BIOS variable"));
103376195Sbrian	KASSERT(sc->fep.data == NULL, ("Uninitialised FEP variable"));
103476195Sbrian	KASSERT(sc->link.data == NULL, ("Uninitialised LINK variable"));
103576195Sbrian	KASSERT(sc->module != NULL, ("Uninitialised module name"));
103676195Sbrian
103778414Sbrian	modlen = strlen(sc->module);
103878414Sbrian	modfile = malloc(modlen + 6, M_TEMP, M_WAITOK);
103978414Sbrian	snprintf(modfile, modlen + 6, "digi_%s", sc->module);
104094321Sbrian	if ((res = linker_reference_module(modfile, NULL, &lf)) != 0) {
104194321Sbrian		if (res == ENOENT && rootdev == NODEV)
104294321Sbrian			printf("%s: Failed to autoload module: No filesystem\n",
104394321Sbrian			    modfile);
104494321Sbrian		else
104594321Sbrian			printf("%s: Failed %d to autoload module\n", modfile,
104694321Sbrian			    res);
104794321Sbrian	}
104876195Sbrian	free(modfile, M_TEMP);
104976195Sbrian	if (res != 0)
105076195Sbrian		return (res);
105176195Sbrian
105278414Sbrian	sym = malloc(modlen + 10, M_TEMP, M_WAITOK);
105378414Sbrian	snprintf(sym, modlen + 10, "digi_mod_%s", sc->module);
105476195Sbrian	if ((symptr = linker_file_lookup_symbol(lf, sym, 0)) == NULL)
105576195Sbrian		printf("digi_%s.ko: Symbol `%s' not found\n", sc->module, sym);
105676195Sbrian	free(sym, M_TEMP);
105776195Sbrian
105876195Sbrian	digi_mod = (struct digi_mod *)symptr;
105976195Sbrian	if (digi_mod->dm_version != DIGI_MOD_VERSION) {
106076195Sbrian		printf("digi_%s.ko: Invalid version %d (need %d)\n",
106176195Sbrian		    sc->module, digi_mod->dm_version, DIGI_MOD_VERSION);
106276195Sbrian		linker_file_unload(lf);
106376195Sbrian		return (EINVAL);
106476195Sbrian	}
106576195Sbrian
106676195Sbrian	sc->bios.size = digi_mod->dm_bios.size;
106776195Sbrian	if (sc->bios.size != 0 && digi_mod->dm_bios.data != NULL) {
106887503Sarr		sc->bios.data = malloc(sc->bios.size, M_TTYS, M_WAITOK);
106976195Sbrian		bcopy(digi_mod->dm_bios.data, sc->bios.data, sc->bios.size);
107076195Sbrian	}
107176195Sbrian
107276195Sbrian	sc->fep.size = digi_mod->dm_fep.size;
107376195Sbrian	if (sc->fep.size != 0 && digi_mod->dm_fep.data != NULL) {
107487503Sarr		sc->fep.data = malloc(sc->fep.size, M_TTYS, M_WAITOK);
107576195Sbrian		bcopy(digi_mod->dm_fep.data, sc->fep.data, sc->fep.size);
107676195Sbrian	}
107776195Sbrian
107876195Sbrian	sc->link.size = digi_mod->dm_link.size;
107976195Sbrian	if (sc->link.size != 0 && digi_mod->dm_link.data != NULL) {
108087503Sarr		sc->link.data = malloc(sc->link.size, M_TTYS, M_WAITOK);
108176195Sbrian		bcopy(digi_mod->dm_link.data, sc->link.data, sc->link.size);
108276195Sbrian	}
108376195Sbrian
108476195Sbrian	linker_file_unload(lf);
108576195Sbrian
108676195Sbrian	return (0);
108776195Sbrian}
108876195Sbrian
108976195Sbrianstatic int
109083366Sjuliandigiioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
109176195Sbrian{
109276195Sbrian	int unit, pnum, mynor, error, s;
109376195Sbrian	struct digi_softc *sc;
109476195Sbrian	struct digi_p *port;
109576195Sbrian	struct tty *tp;
109676195Sbrian#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
109776195Sbrian	int oldcmd;
109876195Sbrian	struct termios term;
109976195Sbrian#endif
110076195Sbrian
110176195Sbrian	mynor = minor(dev);
110276195Sbrian	unit = MINOR_TO_UNIT(mynor);
110376195Sbrian	pnum = MINOR_TO_PORT(mynor);
110476195Sbrian
110576195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
110676195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiioctl\n", unit));
110776195Sbrian
110876195Sbrian	if (sc->status == DIGI_STATUS_DISABLED)
110976195Sbrian		return (ENXIO);
111076195Sbrian
111176195Sbrian	if (mynor & CTRL_DEV) {
111276195Sbrian		switch (cmd) {
111376195Sbrian		case DIGIIO_DEBUG:
111476195Sbrian#ifdef DEBUG
111576195Sbrian			digi_debug = *(int *)data;
111676195Sbrian			return (0);
111776195Sbrian#else
111876195Sbrian			device_printf(sc->dev, "DEBUG not defined\n");
111976195Sbrian			return (ENXIO);
112076195Sbrian#endif
112176195Sbrian		case DIGIIO_REINIT:
112276195Sbrian			digi_loadmoduledata(sc);
112376195Sbrian			error = digi_init(sc);
112476195Sbrian			digi_freemoduledata(sc);
112576195Sbrian			return (error);
112676195Sbrian
112776195Sbrian		case DIGIIO_MODEL:
112876705Sbrian			*(enum digi_model *)data = sc->model;
112976195Sbrian			return (0);
113076195Sbrian
113176195Sbrian		case DIGIIO_IDENT:
113276195Sbrian			return (copyout(sc->name, *(char **)data,
113376195Sbrian			    strlen(sc->name) + 1));
113476195Sbrian		}
113576195Sbrian	}
113676195Sbrian
113776195Sbrian	if (pnum >= sc->numports)
113876195Sbrian		return (ENXIO);
113976195Sbrian
114076195Sbrian	port = sc->ports + pnum;
114176195Sbrian	if (!(port->status & ENABLED))
114276195Sbrian		return (ENXIO);
114376195Sbrian
114476195Sbrian	tp = port->tp;
114576195Sbrian
114676195Sbrian	if (mynor & CONTROL_MASK) {
114776195Sbrian		struct termios *ct;
114876195Sbrian
114976195Sbrian		switch (mynor & CONTROL_MASK) {
115076195Sbrian		case CONTROL_INIT_STATE:
115176195Sbrian			ct = (mynor & CALLOUT_MASK) ?
115276195Sbrian			    &port->it_out : &port->it_in;
115376195Sbrian			break;
115476195Sbrian		case CONTROL_LOCK_STATE:
115576195Sbrian			ct = (mynor & CALLOUT_MASK) ?
115676195Sbrian			    &port->lt_out : &port->lt_in;
115776195Sbrian			break;
115876195Sbrian		default:
115976195Sbrian			return (ENODEV);	/* /dev/nodev */
116076195Sbrian		}
116176195Sbrian
116276195Sbrian		switch (cmd) {
116376195Sbrian		case TIOCSETA:
116493593Sjhb			error = suser(td);
116576195Sbrian			if (error != 0)
116676195Sbrian				return (error);
116776195Sbrian			*ct = *(struct termios *)data;
116878496Sbrian			return (0);
116976195Sbrian
117076195Sbrian		case TIOCGETA:
117176195Sbrian			*(struct termios *)data = *ct;
117278496Sbrian			return (0);
117376195Sbrian
117476195Sbrian		case TIOCGETD:
117576195Sbrian			*(int *)data = TTYDISC;
117676195Sbrian			return (0);
117778496Sbrian
117876195Sbrian		case TIOCGWINSZ:
117976195Sbrian			bzero(data, sizeof(struct winsize));
118076195Sbrian			return (0);
118178496Sbrian
118278496Sbrian		case DIGIIO_GETALTPIN:
118378496Sbrian			switch (mynor & CONTROL_MASK) {
118478496Sbrian			case CONTROL_INIT_STATE:
118578496Sbrian				*(int *)data = port->ialtpin;
118678496Sbrian				break;
118778496Sbrian
118878496Sbrian			case CONTROL_LOCK_STATE:
118978496Sbrian				*(int *)data = port->laltpin;
119078496Sbrian				break;
119178496Sbrian
119278496Sbrian			default:
119378496Sbrian				panic("Confusion when re-testing minor");
119478496Sbrian				return (ENODEV);
119578496Sbrian			}
119678496Sbrian			return (0);
119778496Sbrian
119878496Sbrian		case DIGIIO_SETALTPIN:
119978496Sbrian			switch (mynor & CONTROL_MASK) {
120078496Sbrian			case CONTROL_INIT_STATE:
120178496Sbrian				if (!port->laltpin) {
120278496Sbrian					port->ialtpin = !!*(int *)data;
120378496Sbrian					DLOG(DIGIDB_SET, (sc->dev,
120478496Sbrian					    "port%d: initial ALTPIN %s\n", pnum,
120578496Sbrian					    port->ialtpin ? "set" : "cleared"));
120678496Sbrian				}
120778496Sbrian				break;
120878496Sbrian
120978496Sbrian			case CONTROL_LOCK_STATE:
121078496Sbrian				port->laltpin = !!*(int *)data;
121178496Sbrian				DLOG(DIGIDB_SET, (sc->dev,
121278496Sbrian				    "port%d: ALTPIN %slocked\n",
121378496Sbrian				    pnum, port->laltpin ? "" : "un"));
121478496Sbrian				break;
121578496Sbrian
121678496Sbrian			default:
121778496Sbrian				panic("Confusion when re-testing minor");
121878496Sbrian				return (ENODEV);
121978496Sbrian			}
122078496Sbrian			return (0);
122178496Sbrian
122276195Sbrian		default:
122376195Sbrian			return (ENOTTY);
122476195Sbrian		}
122576195Sbrian	}
122678496Sbrian
122778496Sbrian	switch (cmd) {
122878496Sbrian	case DIGIIO_GETALTPIN:
122978496Sbrian		*(int *)data = !!(port->dsr == sc->csigs->cd);
123078496Sbrian		return (0);
123178496Sbrian
123278496Sbrian	case DIGIIO_SETALTPIN:
123378496Sbrian		if (!port->laltpin) {
123478496Sbrian			if (*(int *)data) {
123578496Sbrian				DLOG(DIGIDB_SET, (sc->dev,
123678496Sbrian				    "port%d: ALTPIN set\n", pnum));
123778496Sbrian				port->cd = sc->csigs->dsr;
123878496Sbrian				port->dsr = sc->csigs->cd;
123978496Sbrian			} else {
124078496Sbrian				DLOG(DIGIDB_SET, (sc->dev,
124178496Sbrian				    "port%d: ALTPIN cleared\n", pnum));
124278496Sbrian				port->cd = sc->csigs->cd;
124378496Sbrian				port->dsr = sc->csigs->dsr;
124478496Sbrian			}
124578496Sbrian		}
124678496Sbrian		return (0);
124778496Sbrian	}
124878496Sbrian
124976195Sbrian	tp = port->tp;
125076195Sbrian#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
125176195Sbrian	term = tp->t_termios;
125276195Sbrian	oldcmd = cmd;
125376195Sbrian	error = ttsetcompat(tp, &cmd, data, &term);
125476195Sbrian	if (error != 0)
125576195Sbrian		return (error);
125676195Sbrian	if (cmd != oldcmd)
125776195Sbrian		data = (caddr_t) & term;
125876195Sbrian#endif
125976195Sbrian	if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
126076195Sbrian		int cc;
126176195Sbrian		struct termios *dt;
126276195Sbrian		struct termios *lt;
126376195Sbrian
126476195Sbrian		dt = (struct termios *)data;
126576195Sbrian		lt = (mynor & CALLOUT_MASK) ? &port->lt_out : &port->lt_in;
126676195Sbrian
126776195Sbrian		dt->c_iflag =
126876195Sbrian		    (tp->t_iflag & lt->c_iflag) | (dt->c_iflag & ~lt->c_iflag);
126976195Sbrian		dt->c_oflag =
127076195Sbrian		    (tp->t_oflag & lt->c_oflag) | (dt->c_oflag & ~lt->c_oflag);
127176195Sbrian		dt->c_cflag =
127276195Sbrian		    (tp->t_cflag & lt->c_cflag) | (dt->c_cflag & ~lt->c_cflag);
127376195Sbrian		dt->c_lflag =
127476195Sbrian		    (tp->t_lflag & lt->c_lflag) | (dt->c_lflag & ~lt->c_lflag);
127576195Sbrian		port->c_iflag = dt->c_iflag & (IXOFF | IXON | IXANY);
127676195Sbrian		dt->c_iflag &= ~(IXOFF | IXON | IXANY);
127776195Sbrian		for (cc = 0; cc < NCCS; ++cc)
127876195Sbrian			if (lt->c_cc[cc] != 0)
127976195Sbrian				dt->c_cc[cc] = tp->t_cc[cc];
128076195Sbrian		if (lt->c_ispeed != 0)
128176195Sbrian			dt->c_ispeed = tp->t_ispeed;
128276195Sbrian		if (lt->c_ospeed != 0)
128376195Sbrian			dt->c_ospeed = tp->t_ospeed;
128476195Sbrian	}
128583366Sjulian	error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, td);
128676195Sbrian	if (error == 0 && cmd == TIOCGETA)
128776195Sbrian		((struct termios *)data)->c_iflag |= port->c_iflag;
128876195Sbrian
128976195Sbrian	if (error >= 0 && error != ENOIOCTL)
129076195Sbrian		return (error);
129176195Sbrian	s = spltty();
129276195Sbrian	error = ttioctl(tp, cmd, data, flag);
129376195Sbrian	if (error == 0 && cmd == TIOCGETA)
129476195Sbrian		((struct termios *)data)->c_iflag |= port->c_iflag;
129576195Sbrian
129676195Sbrian	digi_disc_optim(tp, &tp->t_termios, port);
129776195Sbrian	if (error >= 0 && error != ENOIOCTL) {
129876195Sbrian		splx(s);
129976195Sbrian		return (error);
130076195Sbrian	}
130176195Sbrian	sc->setwin(sc, 0);
130276195Sbrian	switch (cmd) {
130376195Sbrian	case DIGIIO_RING:
130476195Sbrian		port->send_ring = *(u_char *)data;
130576195Sbrian		break;
130676195Sbrian	case TIOCSBRK:
130776195Sbrian		/*
130883861Sbrian		 * now it sends 400 millisecond break because I don't know
130976195Sbrian		 * how to send an infinite break
131076195Sbrian		 */
131183861Sbrian		fepcmd_w(port, SENDBREAK, 400, 10);
131276195Sbrian		break;
131376195Sbrian	case TIOCCBRK:
131476195Sbrian		/* now it's empty */
131576195Sbrian		break;
131676195Sbrian	case TIOCSDTR:
131776195Sbrian		digimctl(port, TIOCM_DTR, DMBIS);
131876195Sbrian		break;
131976195Sbrian	case TIOCCDTR:
132076195Sbrian		digimctl(port, TIOCM_DTR, DMBIC);
132176195Sbrian		break;
132276195Sbrian	case TIOCMSET:
132376195Sbrian		digimctl(port, *(int *)data, DMSET);
132476195Sbrian		break;
132576195Sbrian	case TIOCMBIS:
132676195Sbrian		digimctl(port, *(int *)data, DMBIS);
132776195Sbrian		break;
132876195Sbrian	case TIOCMBIC:
132976195Sbrian		digimctl(port, *(int *)data, DMBIC);
133076195Sbrian		break;
133176195Sbrian	case TIOCMGET:
133276195Sbrian		*(int *)data = digimctl(port, 0, DMGET);
133376195Sbrian		break;
133476195Sbrian	case TIOCMSDTRWAIT:
133593593Sjhb		error = suser(td);
133676195Sbrian		if (error != 0) {
133776195Sbrian			splx(s);
133876195Sbrian			return (error);
133976195Sbrian		}
134076195Sbrian		port->dtr_wait = *(int *)data *hz / 100;
134176195Sbrian
134276195Sbrian		break;
134376195Sbrian	case TIOCMGDTRWAIT:
134476195Sbrian		*(int *)data = port->dtr_wait * 100 / hz;
134576195Sbrian		break;
134676195Sbrian#ifdef DIGI_INTERRUPT
134776195Sbrian	case TIOCTIMESTAMP:
134876195Sbrian		*(struct timeval *)data = sc->intr_timestamp;
134976195Sbrian
135076195Sbrian		break;
135176195Sbrian#endif
135276195Sbrian	default:
135376195Sbrian		splx(s);
135476195Sbrian		return (ENOTTY);
135576195Sbrian	}
135676195Sbrian	splx(s);
135776195Sbrian	return (0);
135876195Sbrian}
135976195Sbrian
136076195Sbrianstatic int
136176195Sbriandigiparam(struct tty *tp, struct termios *t)
136276195Sbrian{
136376195Sbrian	int mynor;
136476195Sbrian	int unit;
136576195Sbrian	int pnum;
136676195Sbrian	struct digi_softc *sc;
136776195Sbrian	struct digi_p *port;
136876195Sbrian	int cflag;
136976195Sbrian	int iflag;
137076195Sbrian	int hflow;
137176195Sbrian	int s;
137276195Sbrian	int window;
137376195Sbrian
137476195Sbrian	mynor = minor(tp->t_dev);
137576195Sbrian	unit = MINOR_TO_UNIT(mynor);
137676195Sbrian	pnum = MINOR_TO_PORT(mynor);
137776195Sbrian
137876195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
137976195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digiparam\n", unit));
138076195Sbrian
138176195Sbrian	port = &sc->ports[pnum];
138276195Sbrian
138376195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: setting parameters\n", pnum));
138476195Sbrian
138576195Sbrian	if (t->c_ispeed == 0)
138676195Sbrian		t->c_ispeed = t->c_ospeed;
138776195Sbrian
138876195Sbrian	cflag = ttspeedtab(t->c_ospeed, digispeedtab);
138976195Sbrian
139076195Sbrian	if (cflag < 0 || (cflag > 0 && t->c_ispeed != t->c_ospeed))
139176195Sbrian		return (EINVAL);
139276195Sbrian
139376195Sbrian	s = splclock();
139476195Sbrian
139576195Sbrian	window = sc->window;
139676195Sbrian	sc->setwin(sc, 0);
139776195Sbrian
139876195Sbrian	if (cflag == 0) {				/* hangup */
139976195Sbrian		DLOG(DIGIDB_SET, (sc->dev, "port%d: hangup\n", pnum));
140076195Sbrian		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
140176195Sbrian	} else {
140276195Sbrian		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIS);
140376195Sbrian
140476195Sbrian		DLOG(DIGIDB_SET, (sc->dev, "port%d: CBAUD = %d\n", pnum,
140576195Sbrian		    cflag));
140676195Sbrian
140776195Sbrian#if 0
140876195Sbrian		/* convert flags to sysV-style values */
140976195Sbrian		if (t->c_cflag & PARODD)
141076195Sbrian			cflag |= 0x0200;
141176195Sbrian		if (t->c_cflag & PARENB)
141276195Sbrian			cflag |= 0x0100;
141376195Sbrian		if (t->c_cflag & CSTOPB)
141476195Sbrian			cflag |= 0x0080;
141576195Sbrian#else
141676195Sbrian		/* convert flags to sysV-style values */
141776195Sbrian		if (t->c_cflag & PARODD)
141876195Sbrian			cflag |= FEP_PARODD;
141976195Sbrian		if (t->c_cflag & PARENB)
142076195Sbrian			cflag |= FEP_PARENB;
142176195Sbrian		if (t->c_cflag & CSTOPB)
142276195Sbrian			cflag |= FEP_CSTOPB;
142376195Sbrian		if (t->c_cflag & CLOCAL)
142476195Sbrian			cflag |= FEP_CLOCAL;
142576195Sbrian#endif
142676195Sbrian
142776195Sbrian		cflag |= (t->c_cflag & CSIZE) >> 4;
142876195Sbrian		DLOG(DIGIDB_SET, (sc->dev, "port%d: CFLAG = 0x%x\n", pnum,
142976195Sbrian		    cflag));
143076195Sbrian		fepcmd_w(port, SETCFLAGS, (unsigned)cflag, 0);
143176195Sbrian	}
143276195Sbrian
143376195Sbrian	iflag =
143476195Sbrian	    t->c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP);
143576195Sbrian	if (port->c_iflag & IXON)
143676195Sbrian		iflag |= 0x400;
143776195Sbrian	if (port->c_iflag & IXANY)
143876195Sbrian		iflag |= 0x800;
143976195Sbrian	if (port->c_iflag & IXOFF)
144076195Sbrian		iflag |= 0x1000;
144176195Sbrian
144276195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: set iflag = 0x%x\n", pnum, iflag));
144376195Sbrian	fepcmd_w(port, SETIFLAGS, (unsigned)iflag, 0);
144476195Sbrian
144576195Sbrian	hflow = 0;
144676195Sbrian	if (t->c_cflag & CDTR_IFLOW)
144776195Sbrian		hflow |= sc->csigs->dtr;
144876195Sbrian	if (t->c_cflag & CRTS_IFLOW)
144976195Sbrian		hflow |= sc->csigs->rts;
145076195Sbrian	if (t->c_cflag & CCTS_OFLOW)
145176195Sbrian		hflow |= sc->csigs->cts;
145276195Sbrian	if (t->c_cflag & CDSR_OFLOW)
145378496Sbrian		hflow |= port->dsr;
145476195Sbrian	if (t->c_cflag & CCAR_OFLOW)
145578496Sbrian		hflow |= port->cd;
145676195Sbrian
145776195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: set hflow = 0x%x\n", pnum, hflow));
145876195Sbrian	fepcmd_w(port, SETHFLOW, 0xff00 | (unsigned)hflow, 0);
145976195Sbrian
146076195Sbrian	DLOG(DIGIDB_SET, (sc->dev, "port%d: set startc(0x%x), stopc(0x%x)\n",
146176195Sbrian	    pnum, t->c_cc[VSTART], t->c_cc[VSTOP]));
146276195Sbrian	fepcmd_b(port, SONOFFC, t->c_cc[VSTART], t->c_cc[VSTOP], 0);
146376195Sbrian
146476195Sbrian	if (sc->window != 0)
146576195Sbrian		sc->towin(sc, 0);
146676195Sbrian	if (window != 0)
146776195Sbrian		sc->towin(sc, window);
146876195Sbrian	splx(s);
146976195Sbrian
147076195Sbrian	return (0);
147176195Sbrian}
147276195Sbrian
147376195Sbrianstatic void
147476195Sbriandigi_intr(void *vp)
147576195Sbrian{
147676195Sbrian	struct digi_p *port;
147776195Sbrian	char *cxcon;
147876195Sbrian	struct digi_softc *sc;
147976195Sbrian	int ehead, etail;
148076195Sbrian	volatile struct board_chan *bc;
148176195Sbrian	struct tty *tp;
148276195Sbrian	int head, tail;
148376195Sbrian	int wrapmask;
148476195Sbrian	int size, window;
148576195Sbrian	struct event {
148676195Sbrian		u_char pnum;
148776195Sbrian		u_char event;
148876195Sbrian		u_char mstat;
148976195Sbrian		u_char lstat;
149076195Sbrian	} event;
149176195Sbrian
149276195Sbrian	sc = vp;
149376195Sbrian
149476195Sbrian	if (sc->status != DIGI_STATUS_ENABLED) {
149576195Sbrian		DLOG(DIGIDB_IRQ, (sc->dev, "interrupt on disabled board !\n"));
149676195Sbrian		return;
149776195Sbrian	}
149876327Sbrian
149976195Sbrian#ifdef DIGI_INTERRUPT
150076195Sbrian	microtime(&sc->intr_timestamp);
150176195Sbrian#endif
150276195Sbrian
150376195Sbrian	window = sc->window;
150476195Sbrian	sc->setwin(sc, 0);
150576195Sbrian
150676195Sbrian	if (sc->model >= PCXEM && W(sc->vmem + 0xd00)) {
150776195Sbrian		struct con_bios *con = con_bios_list;
150876195Sbrian		register u_char *ptr;
150976195Sbrian
151076195Sbrian		ptr = sc->vmem + W(sc->vmem + 0xd00);
151176195Sbrian		while (con) {
151276195Sbrian			if (ptr[1] && W(ptr + 2) == W(con->bios + 2))
151376195Sbrian				/* Not first block -- exact match */
151476195Sbrian				break;
151576195Sbrian
151676195Sbrian			if (W(ptr + 4) >= W(con->bios + 4) &&
151776195Sbrian			    W(ptr + 4) <= W(con->bios + 6))
151876195Sbrian				/* Initial search concetrator BIOS */
151976195Sbrian				break;
152076195Sbrian		}
152176195Sbrian
152276195Sbrian		if (con == NULL) {
152376195Sbrian			log(LOG_ERR, "digi%d: wanted bios LREV = 0x%04x"
152476195Sbrian			    " not found!\n", sc->res.unit, W(ptr + 4));
152576195Sbrian			W(ptr + 10) = 0;
152676195Sbrian			W(sc->vmem + 0xd00) = 0;
152776195Sbrian			goto eoi;
152876195Sbrian		}
152976195Sbrian		cxcon = con->bios;
153076195Sbrian		W(ptr + 4) = W(cxcon + 4);
153176195Sbrian		W(ptr + 6) = W(cxcon + 6);
153276195Sbrian		if (ptr[1] == 0)
153376195Sbrian			W(ptr + 2) = W(cxcon + 2);
153476195Sbrian		W(ptr + 8) = (ptr[1] << 6) + W(cxcon + 8);
153576195Sbrian		size = W(cxcon + 10) - (ptr[1] << 10);
153676195Sbrian		if (size <= 0) {
153776195Sbrian			W(ptr + 8) = W(cxcon + 8);
153876195Sbrian			W(ptr + 10) = 0;
153976195Sbrian		} else {
154076195Sbrian			if (size > 1024)
154176195Sbrian				size = 1024;
154276195Sbrian			W(ptr + 10) = size;
154376195Sbrian			bcopy(cxcon + (ptr[1] << 10), ptr + 12, size);
154476195Sbrian		}
154576195Sbrian		W(sc->vmem + 0xd00) = 0;
154676195Sbrian		goto eoi;
154776195Sbrian	}
154876195Sbrian
154976195Sbrian	ehead = sc->gdata->ein;
155076195Sbrian	etail = sc->gdata->eout;
155176195Sbrian	if (ehead == etail) {
155276195Sbrian#ifdef DEBUG
155376195Sbrian		sc->intr_count++;
155476195Sbrian		if (sc->intr_count % 6000 == 0) {
155576195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev,
155676195Sbrian			    "6000 useless polls %x %x\n", ehead, etail));
155776195Sbrian			sc->intr_count = 0;
155876195Sbrian		}
155976195Sbrian#endif
156076195Sbrian		goto eoi;
156176195Sbrian	}
156276195Sbrian	while (ehead != etail) {
156376195Sbrian		event = *(volatile struct event *)(sc->memevent + etail);
156476195Sbrian
156576195Sbrian		etail = (etail + 4) & sc->gdata->imax;
156676195Sbrian
156776195Sbrian		if (event.pnum >= sc->numports) {
156876195Sbrian			log(LOG_ERR, "digi%d: port %d: got event"
156976195Sbrian			    " on nonexisting port\n", sc->res.unit,
157076195Sbrian			    event.pnum);
157176195Sbrian			continue;
157276195Sbrian		}
157376195Sbrian		port = &sc->ports[event.pnum];
157476195Sbrian		bc = port->bc;
157576195Sbrian		tp = port->tp;
157676195Sbrian
157776195Sbrian		if (!(tp->t_state & TS_ISOPEN) && !port->wopeners) {
157876195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev,
157976195Sbrian			    "port %d: event 0x%x on closed port\n",
158076195Sbrian			    event.pnum, event.event));
158176195Sbrian			bc->rout = bc->rin;
158276195Sbrian			bc->idata = 0;
158376195Sbrian			bc->iempty = 0;
158476195Sbrian			bc->ilow = 0;
158576195Sbrian			bc->mint = 0;
158676195Sbrian			continue;
158776195Sbrian		}
158876195Sbrian		if (event.event & ~ALL_IND)
158976195Sbrian			log(LOG_ERR, "digi%d: port%d: ? event 0x%x mstat 0x%x"
159076195Sbrian			    " lstat 0x%x\n", sc->res.unit, event.pnum,
159176195Sbrian			    event.event, event.mstat, event.lstat);
159276195Sbrian
159376195Sbrian		if (event.event & DATA_IND) {
159476195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev, "port %d: DATA_IND\n",
159576195Sbrian			    event.pnum));
159676195Sbrian			wrapmask = port->rxbufsize - 1;
159776195Sbrian			head = bc->rin;
159876195Sbrian			tail = bc->rout;
159976195Sbrian
160076195Sbrian			size = 0;
160176195Sbrian			if (!(tp->t_state & TS_ISOPEN)) {
160276195Sbrian				bc->rout = head;
160376195Sbrian				goto end_of_data;
160476195Sbrian			}
160576195Sbrian			while (head != tail) {
160676195Sbrian				int top;
160776195Sbrian
160876195Sbrian				DLOG(DIGIDB_INT, (sc->dev,
160976195Sbrian				    "port %d: p rx head = %d tail = %d\n",
161076195Sbrian				    event.pnum, head, tail));
161176195Sbrian				top = (head > tail) ? head : wrapmask + 1;
161276195Sbrian				sc->towin(sc, port->rxwin);
161376195Sbrian				size = top - tail;
161476195Sbrian				if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
161576195Sbrian					size = b_to_q((char *)port->rxbuf +
161676195Sbrian					    tail, size, &tp->t_rawq);
161776195Sbrian					tail = top - size;
161876195Sbrian					ttwakeup(tp);
161976195Sbrian				} else for (; tail < top;) {
162076195Sbrian					linesw[tp->t_line].
162176195Sbrian					    l_rint(port->rxbuf[tail], tp);
162276195Sbrian					sc->towin(sc, port->rxwin);
162376195Sbrian					size--;
162476195Sbrian					tail++;
162576195Sbrian					if (tp->t_state & TS_TBLOCK)
162676195Sbrian						break;
162776195Sbrian				}
162876195Sbrian				tail &= wrapmask;
162976195Sbrian				sc->setwin(sc, 0);
163076195Sbrian				bc->rout = tail;
163176195Sbrian				head = bc->rin;
163276195Sbrian				if (size)
163376195Sbrian					break;
163476195Sbrian			}
163576195Sbrian
163676195Sbrian			if (bc->orun) {
163776195Sbrian				CE_RECORD(port, CE_OVERRUN);
163876195Sbrian				log(LOG_ERR, "digi%d: port%d: %s\n",
163976195Sbrian				    sc->res.unit, event.pnum,
164076195Sbrian				    digi_errortxt(CE_OVERRUN));
164176195Sbrian				bc->orun = 0;
164276195Sbrian			}
164376195Sbrianend_of_data:
164476195Sbrian			if (size) {
164576195Sbrian				tp->t_state |= TS_TBLOCK;
164676195Sbrian				port->status |= PAUSE_RX;
164776195Sbrian				DLOG(DIGIDB_RX, (sc->dev, "port %d: pause RX\n",
164876195Sbrian				    event.pnum));
164976195Sbrian			} else {
165076195Sbrian				bc->idata = 1;
165176195Sbrian			}
165276195Sbrian		}
165376195Sbrian
165476195Sbrian		if (event.event & MODEMCHG_IND) {
165576195Sbrian			DLOG(DIGIDB_MODEM, (sc->dev, "port %d: MODEMCHG_IND\n",
165676195Sbrian			    event.pnum));
165776195Sbrian
165878496Sbrian			if ((event.mstat ^ event.lstat) & port->cd) {
165976195Sbrian				sc->hidewin(sc);
166076195Sbrian				linesw[tp->t_line].l_modem
166178496Sbrian				    (tp, event.mstat & port->cd);
166276195Sbrian				sc->setwin(sc, 0);
166376195Sbrian				wakeup(TSA_CARR_ON(tp));
166476195Sbrian			}
166576195Sbrian
166676195Sbrian			if (event.mstat & sc->csigs->ri) {
166776195Sbrian				DLOG(DIGIDB_RI, (sc->dev, "port %d: RING\n",
166876195Sbrian				    event.pnum));
166976195Sbrian				if (port->send_ring) {
167076195Sbrian					linesw[tp->t_line].l_rint('R', tp);
167176195Sbrian					linesw[tp->t_line].l_rint('I', tp);
167276195Sbrian					linesw[tp->t_line].l_rint('N', tp);
167376195Sbrian					linesw[tp->t_line].l_rint('G', tp);
167476195Sbrian					linesw[tp->t_line].l_rint('\r', tp);
167576195Sbrian					linesw[tp->t_line].l_rint('\n', tp);
167676195Sbrian				}
167776195Sbrian			}
167876195Sbrian		}
167976195Sbrian		if (event.event & BREAK_IND) {
168076195Sbrian			DLOG(DIGIDB_MODEM, (sc->dev, "port %d: BREAK_IND\n",
168176195Sbrian			    event.pnum));
168276195Sbrian			linesw[tp->t_line].l_rint(TTY_BI, tp);
168376195Sbrian		}
168476195Sbrian		if (event.event & (LOWTX_IND | EMPTYTX_IND)) {
168576195Sbrian			DLOG(DIGIDB_IRQ, (sc->dev, "port %d:%s%s\n",
168676195Sbrian			    event.pnum,
168776195Sbrian			    event.event & LOWTX_IND ? " LOWTX" : "",
168876195Sbrian			    event.event & EMPTYTX_IND ?  " EMPTYTX" : ""));
168976195Sbrian			(*linesw[tp->t_line].l_start)(tp);
169076195Sbrian		}
169176195Sbrian	}
169276195Sbrian	sc->gdata->eout = etail;
169376195Sbrianeoi:
169476195Sbrian	if (sc->window != 0)
169576195Sbrian		sc->towin(sc, 0);
169676195Sbrian	if (window != 0)
169776195Sbrian		sc->towin(sc, window);
169876195Sbrian}
169976195Sbrian
170076195Sbrianstatic void
170176195Sbriandigistart(struct tty *tp)
170276195Sbrian{
170376195Sbrian	int unit;
170476195Sbrian	int pnum;
170576195Sbrian	struct digi_p *port;
170676195Sbrian	struct digi_softc *sc;
170776195Sbrian	volatile struct board_chan *bc;
170876195Sbrian	int head, tail;
170976195Sbrian	int size, ocount, totcnt = 0;
171076195Sbrian	int s;
171176195Sbrian	int wmask;
171276195Sbrian
171376195Sbrian	unit = MINOR_TO_UNIT(minor(tp->t_dev));
171476195Sbrian	pnum = MINOR_TO_PORT(minor(tp->t_dev));
171576195Sbrian
171676195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
171776195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digistart\n", unit));
171876195Sbrian
171976195Sbrian	port = &sc->ports[pnum];
172076195Sbrian	bc = port->bc;
172176195Sbrian
172276195Sbrian	wmask = port->txbufsize - 1;
172376195Sbrian
172476195Sbrian	s = spltty();
172576195Sbrian	port->lcc = tp->t_outq.c_cc;
172676195Sbrian	sc->setwin(sc, 0);
172776195Sbrian	if (!(tp->t_state & TS_TBLOCK)) {
172876195Sbrian		if (port->status & PAUSE_RX) {
172976195Sbrian			DLOG(DIGIDB_RX, (sc->dev, "port %d: resume RX\n",
173076195Sbrian			    pnum));
173176195Sbrian			/*
173276195Sbrian			 * CAREFUL - braces are needed here if the DLOG is
173376195Sbrian			 * optimised out!
173476195Sbrian			 */
173576195Sbrian		}
173676195Sbrian		port->status &= ~PAUSE_RX;
173776195Sbrian		bc->idata = 1;
173876195Sbrian	}
173976195Sbrian	if (!(tp->t_state & TS_TTSTOP) && port->status & PAUSE_TX) {
174076195Sbrian		DLOG(DIGIDB_TX, (sc->dev, "port %d: resume TX\n", pnum));
174176195Sbrian		port->status &= ~PAUSE_TX;
174276195Sbrian		fepcmd_w(port, RESUMETX, 0, 10);
174376195Sbrian	}
174476195Sbrian	if (tp->t_outq.c_cc == 0)
174576195Sbrian		tp->t_state &= ~TS_BUSY;
174676195Sbrian	else
174776195Sbrian		tp->t_state |= TS_BUSY;
174876195Sbrian
174976195Sbrian	head = bc->tin;
175076195Sbrian	while (tp->t_outq.c_cc != 0) {
175176195Sbrian		tail = bc->tout;
175276195Sbrian		DLOG(DIGIDB_INT, (sc->dev, "port%d: s tx head = %d tail = %d\n",
175376195Sbrian		    pnum, head, tail));
175476195Sbrian
175576195Sbrian		if (head < tail)
175676195Sbrian			size = tail - head - 1;
175776195Sbrian		else {
175876195Sbrian			size = port->txbufsize - head;
175976195Sbrian			if (tail == 0)
176076195Sbrian				size--;
176176195Sbrian		}
176276195Sbrian
176376195Sbrian		if (size == 0)
176476195Sbrian			break;
176576195Sbrian		sc->towin(sc, port->txwin);
176676195Sbrian		ocount = q_to_b(&tp->t_outq, port->txbuf + head, size);
176776195Sbrian		totcnt += ocount;
176876195Sbrian		head += ocount;
176976195Sbrian		head &= wmask;
177076195Sbrian		sc->setwin(sc, 0);
177176195Sbrian		bc->tin = head;
177276195Sbrian		bc->iempty = 1;
177376195Sbrian		bc->ilow = 1;
177476195Sbrian	}
177576195Sbrian	port->lostcc = tp->t_outq.c_cc;
177676195Sbrian	tail = bc->tout;
177776195Sbrian	if (head < tail)
177876195Sbrian		size = port->txbufsize - tail + head;
177976195Sbrian	else
178076195Sbrian		size = head - tail;
178176195Sbrian
178276195Sbrian	port->lbuf = size;
178376195Sbrian	DLOG(DIGIDB_INT, (sc->dev, "port%d: s total cnt = %d\n", pnum, totcnt));
178476195Sbrian	ttwwakeup(tp);
178576195Sbrian	splx(s);
178676195Sbrian}
178776195Sbrian
178876195Sbrianstatic void
178976195Sbriandigistop(struct tty *tp, int rw)
179076195Sbrian{
179176195Sbrian	struct digi_softc *sc;
179276195Sbrian	int unit;
179376195Sbrian	int pnum;
179476195Sbrian	struct digi_p *port;
179576195Sbrian
179676195Sbrian	unit = MINOR_TO_UNIT(minor(tp->t_dev));
179776195Sbrian	pnum = MINOR_TO_PORT(minor(tp->t_dev));
179876195Sbrian
179976195Sbrian	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
180076195Sbrian	KASSERT(sc, ("digi%d: softc not allocated in digistop\n", unit));
180176195Sbrian	port = sc->ports + pnum;
180276195Sbrian
180376195Sbrian	DLOG(DIGIDB_TX, (sc->dev, "port %d: pause TX\n", pnum));
180476195Sbrian	port->status |= PAUSE_TX;
180576195Sbrian	fepcmd_w(port, PAUSETX, 0, 10);
180676195Sbrian}
180776195Sbrian
180876195Sbrianstatic void
180976195Sbrianfepcmd(struct digi_p *port, int cmd, int op1, int ncmds)
181076195Sbrian{
181176195Sbrian	u_char *mem;
181276195Sbrian	unsigned tail, head;
181376195Sbrian	int count, n;
181476195Sbrian
181576195Sbrian	mem = port->sc->memcmd;
181676195Sbrian
181776195Sbrian	port->sc->setwin(port->sc, 0);
181876195Sbrian
181976195Sbrian	head = port->sc->gdata->cin;
182076195Sbrian	mem[head + 0] = cmd;
182176195Sbrian	mem[head + 1] = port->pnum;
182276195Sbrian	*(ushort *)(mem + head + 2) = op1;
182376195Sbrian
182476195Sbrian	head = (head + 4) & port->sc->gdata->cmax;
182576195Sbrian	port->sc->gdata->cin = head;
182676195Sbrian
182776195Sbrian	for (count = FEPTIMEOUT; count > 0; count--) {
182876195Sbrian		head = port->sc->gdata->cin;
182976195Sbrian		tail = port->sc->gdata->cout;
183076195Sbrian		n = (head - tail) & port->sc->gdata->cmax;
183176195Sbrian
183276195Sbrian		if (n <= ncmds * sizeof(short) * 4)
183376195Sbrian			break;
183476195Sbrian	}
183576195Sbrian	if (count == 0)
183676195Sbrian		log(LOG_ERR, "digi%d: port%d: timeout on FEP command\n",
183776195Sbrian		    port->sc->res.unit, port->pnum);
183876195Sbrian}
183976195Sbrian
184076195Sbrianconst char *
184176195Sbriandigi_errortxt(int id)
184276195Sbrian{
184376195Sbrian	static const char *error_desc[] = {
184476195Sbrian		"silo overflow",
184576195Sbrian		"interrupt-level buffer overflow",
184676195Sbrian		"tty-level buffer overflow",
184776195Sbrian	};
184876195Sbrian
184976195Sbrian	KASSERT(id >= 0 && id < sizeof(error_desc) / sizeof(error_desc[0]),
185076195Sbrian	    ("Unexpected digi error id %d\n", id));
185176195Sbrian
185276195Sbrian	return (error_desc[id]);
185376195Sbrian}
185476195Sbrian
185576195Sbrianint
185676195Sbriandigi_attach(struct digi_softc *sc)
185776195Sbrian{
185876195Sbrian	sc->res.ctldev = make_dev(&digi_sw,
185976195Sbrian	    (sc->res.unit << 16) | CTRL_DEV, UID_ROOT, GID_WHEEL,
186076195Sbrian	    0600, "digi%r.ctl", sc->res.unit);
186176195Sbrian
186276195Sbrian	digi_loadmoduledata(sc);
186376195Sbrian	digi_init(sc);
186476195Sbrian	digi_freemoduledata(sc);
186576195Sbrian
186676195Sbrian	return (0);
186776195Sbrian}
186876195Sbrian
186976195Sbrianstatic int
187076195Sbriandigi_inuse(struct digi_softc *sc)
187176195Sbrian{
187276195Sbrian	int i;
187376195Sbrian
187476195Sbrian	for (i = 0; i < sc->numports; i++)
187576195Sbrian		if (sc->ttys[i].t_state & TS_ISOPEN) {
187676195Sbrian			DLOG(DIGIDB_INIT, (sc->dev, "port%d: busy\n", i));
187776195Sbrian			return (1);
187876195Sbrian		} else if (sc->ports[i].wopeners || sc->ports[i].opencnt) {
187976195Sbrian			DLOG(DIGIDB_INIT, (sc->dev, "port%d: blocked in open\n",
188076195Sbrian			    i));
188176195Sbrian			return (1);
188276195Sbrian		}
188376195Sbrian	return (0);
188476195Sbrian}
188576195Sbrian
188676195Sbrianstatic void
188776195Sbriandigi_free_state(struct digi_softc *sc)
188876195Sbrian{
188976195Sbrian	int d, i;
189076195Sbrian
189176195Sbrian	/* Blow it all away */
189276195Sbrian
189376195Sbrian	for (i = 0; i < sc->numports; i++)
189476195Sbrian		for (d = 0; d < 6; d++)
189576195Sbrian			destroy_dev(sc->ports[i].dev[d]);
189676195Sbrian
189776195Sbrian	untimeout(digi_poll, sc, sc->callout);
189876195Sbrian	callout_handle_init(&sc->callout);
189976195Sbrian	untimeout(digi_int_test, sc, sc->inttest);
190076195Sbrian	callout_handle_init(&sc->inttest);
190176195Sbrian
190276195Sbrian	bus_teardown_intr(sc->dev, sc->res.irq, sc->res.irqHandler);
190376195Sbrian#ifdef DIGI_INTERRUPT
190476195Sbrian	if (sc->res.irq != NULL) {
190576195Sbrian		bus_release_resource(dev, SYS_RES_IRQ, sc->res.irqrid,
190676195Sbrian		    sc->res.irq);
190776195Sbrian		sc->res.irq = NULL;
190876195Sbrian	}
190976195Sbrian#endif
191076195Sbrian	if (sc->numports) {
191176195Sbrian		KASSERT(sc->ports, ("digi%d: Lost my ports ?", sc->res.unit));
191276195Sbrian		KASSERT(sc->ttys, ("digi%d: Lost my ttys ?", sc->res.unit));
191377004Sbrian		free(sc->ports, M_TTYS);
191476195Sbrian		sc->ports = NULL;
191577004Sbrian		free(sc->ttys, M_TTYS);
191676195Sbrian		sc->ttys = NULL;
191776195Sbrian		sc->numports = 0;
191876195Sbrian	}
191976195Sbrian
192076195Sbrian	sc->status = DIGI_STATUS_NOTINIT;
192176195Sbrian}
192276195Sbrian
192376195Sbrianint
192476195Sbriandigi_detach(device_t dev)
192576195Sbrian{
192676195Sbrian	struct digi_softc *sc = device_get_softc(dev);
192776195Sbrian
192876195Sbrian	DLOG(DIGIDB_INIT, (sc->dev, "detaching\n"));
192976195Sbrian
193076195Sbrian	/* If we're INIT'd, numports must be 0 */
193176195Sbrian	KASSERT(sc->numports == 0 || sc->status != DIGI_STATUS_NOTINIT,
193276195Sbrian	    ("digi%d: numports(%d) & status(%d) are out of sync",
193376195Sbrian	    sc->res.unit, sc->numports, (int)sc->status));
193476195Sbrian
193576195Sbrian	if (digi_inuse(sc))
193676195Sbrian		return (EBUSY);
193776195Sbrian
193876195Sbrian	digi_free_state(sc);
193976195Sbrian
194076195Sbrian	destroy_dev(makedev(CDEV_MAJOR,
194176195Sbrian	    (sc->res.unit << 16) | CTRL_DEV));
194276195Sbrian
194376195Sbrian	if (sc->res.mem != NULL) {
194476195Sbrian		bus_release_resource(dev, SYS_RES_MEMORY, sc->res.mrid,
194576195Sbrian		    sc->res.mem);
194676195Sbrian		sc->res.mem = NULL;
194776195Sbrian	}
194876195Sbrian	if (sc->res.io != NULL) {
194976195Sbrian		bus_release_resource(dev, SYS_RES_IOPORT, sc->res.iorid,
195076195Sbrian		    sc->res.io);
195176195Sbrian		sc->res.io = NULL;
195276195Sbrian	}
195376195Sbrian
195476195Sbrian	return (0);
195576195Sbrian}
195676195Sbrian
195776195Sbrianint
195876195Sbriandigi_shutdown(device_t dev)
195976195Sbrian{
196076195Sbrian	return (0);
196176195Sbrian}
196294320Sbrian
196394320SbrianMODULE_VERSION(digi, 1);
1964