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