digi.c revision 150132
1/*-
2 * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3 *   based on work by Slawa Olhovchenkov
4 *                    John Prince <johnp@knight-trosoft.com>
5 *                    Eric Hernes
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/dev/digi/digi.c 150132 2005-09-14 15:18:12Z andre $
30 */
31
32/*-
33 * TODO:
34 *	Figure out what the con bios stuff is supposed to do
35 *	Test with *LOTS* more cards - I only have a PCI8r and an ISA Xem.
36 */
37
38#include "opt_compat.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/proc.h>
43#include <sys/conf.h>
44#include <sys/linker.h>
45#include <sys/kernel.h>
46#include <sys/mbuf.h>
47#include <sys/malloc.h>
48#include <sys/module.h>
49#include <sys/tty.h>
50#include <sys/syslog.h>
51#include <sys/fcntl.h>
52#include <sys/serial.h>
53#include <sys/bus.h>
54#include <machine/resource.h>
55
56#include <sys/digiio.h>
57#include <dev/digi/digireg.h>
58#include <dev/digi/digi.h>
59#include <dev/digi/digi_mod.h>
60#include <dev/digi/digi_pci.h>
61
62static t_open_t		digiopen;
63static d_open_t		digicopen;
64static d_close_t	digicclose;
65static t_ioctl_t	digiioctl;
66static d_ioctl_t	digisioctl;
67static d_ioctl_t	digicioctl;
68
69static void	digistop(struct tty *tp, int rw);
70static void	digibreak(struct tty *tp, int brk);
71static int	digimodem(struct tty *tp, int sigon, int sigoff);
72static void	digi_poll(void *ptr);
73static void	digi_freemoduledata(struct digi_softc *);
74static void	fepcmd(struct digi_p *port, int cmd, int op, int ncmds);
75static void	digistart(struct tty *tp);
76static int	digiparam(struct tty *tp, struct termios *t);
77static void	digiclose(struct tty *tp);
78static void	digi_intr(void *);
79static int	digi_init(struct digi_softc *_sc);
80static int	digi_loadmoduledata(struct digi_softc *);
81static int	digi_inuse(struct digi_softc *);
82static void	digi_free_state(struct digi_softc *);
83
84#define	fepcmd_b(port, cmd, op1, op2, ncmds) \
85	fepcmd(port, cmd, (op2 << 8) | op1, ncmds)
86#define	fepcmd_w	fepcmd
87
88struct con_bios {
89	struct con_bios *next;
90	u_char *bios;
91	size_t size;
92};
93
94static struct con_bios *con_bios_list;
95devclass_t	 digi_devclass;
96static char	 driver_name[] = "digi";
97unsigned 	 digi_debug = 0;
98
99static struct speedtab digispeedtab[] = {
100	{ 0,		0},			/* old (sysV-like) Bx codes */
101	{ 50,		1},
102	{ 75,		2},
103	{ 110,		3},
104	{ 134,		4},
105	{ 150,		5},
106	{ 200,		6},
107	{ 300,		7},
108	{ 600,		8},
109	{ 1200,		9},
110	{ 1800,		10},
111	{ 2400,		11},
112	{ 4800,		12},
113	{ 9600,		13},
114	{ 19200,	14},
115	{ 38400,	15},
116	{ 57600,	(02000 | 1)},
117	{ 76800,	(02000 | 2)},
118	{ 115200,	(02000 | 3)},
119	{ 230400,	(02000 | 6)},
120	{ -1,		-1}
121};
122
123const struct digi_control_signals digi_xixe_signals = {
124	0x02, 0x08, 0x10, 0x20, 0x40, 0x80
125};
126
127const struct digi_control_signals digi_normal_signals = {
128	0x02, 0x80, 0x20, 0x10, 0x40, 0x01
129};
130
131static struct cdevsw digi_csw = {
132	.d_version =	D_VERSION,
133	.d_open =	digicopen,
134	.d_close =	digicclose,
135	.d_ioctl =	digicioctl,
136	.d_name =	driver_name,
137	.d_flags =	D_TTY | D_NEEDGIANT,
138};
139
140static void
141digi_poll(void *ptr)
142{
143	struct digi_softc *sc;
144
145	sc = (struct digi_softc *)ptr;
146	callout_handle_init(&sc->callout);
147	digi_intr(sc);
148	sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
149}
150
151static void
152digi_int_test(void *v)
153{
154	struct digi_softc *sc = v;
155
156	callout_handle_init(&sc->inttest);
157#ifdef DIGI_INTERRUPT
158	if (sc->intr_timestamp.tv_sec || sc->intr_timestamp.tv_usec) {
159		/* interrupt OK! */
160		return;
161	}
162	log(LOG_ERR, "digi%d: Interrupt didn't work, use polled mode\n", unit);
163#endif
164	sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
165}
166
167static void
168digi_freemoduledata(struct digi_softc *sc)
169{
170	if (sc->fep.data != NULL) {
171		free(sc->fep.data, M_TTYS);
172		sc->fep.data = NULL;
173	}
174	if (sc->link.data != NULL) {
175		free(sc->link.data, M_TTYS);
176		sc->link.data = NULL;
177	}
178	if (sc->bios.data != NULL) {
179		free(sc->bios.data, M_TTYS);
180		sc->bios.data = NULL;
181	}
182}
183
184static int
185digi_bcopy(const void *vfrom, void *vto, size_t sz)
186{
187	volatile const char *from = (volatile const char *)vfrom;
188	volatile char *to = (volatile char *)vto;
189	size_t i;
190
191	for (i = 0; i < sz; i++)
192		*to++ = *from++;
193
194	from = (const volatile char *)vfrom;
195	to = (volatile char *)vto;
196	for (i = 0; i < sz; i++)
197		if (*to++ != *from++)
198			return (0);
199	return (1);
200}
201
202void
203digi_delay(struct digi_softc *sc, const char *txt, u_long timo)
204{
205	if (cold)
206		DELAY(timo * 1000000 / hz);
207	else
208		tsleep(sc, PUSER | PCATCH, txt, timo);
209}
210
211static int
212digi_init(struct digi_softc *sc)
213{
214	int i, cnt, resp;
215	u_char *ptr;
216	int lowwater;
217	struct digi_p *port;
218	volatile struct board_chan *bc;
219	struct tty *tp;
220
221	ptr = NULL;
222
223	if (sc->status == DIGI_STATUS_DISABLED) {
224		log(LOG_ERR, "digi%d: Cannot init a disabled card\n",
225		    sc->res.unit);
226		return (EIO);
227	}
228	if (sc->bios.data == NULL) {
229		log(LOG_ERR, "digi%d: Cannot init without BIOS\n",
230		    sc->res.unit);
231		return (EIO);
232	}
233#if 0
234	if (sc->link.data == NULL && sc->model >= PCCX) {
235		log(LOG_ERR, "digi%d: Cannot init without link info\n",
236		    sc->res.unit);
237		return (EIO);
238	}
239#endif
240	if (sc->fep.data == NULL) {
241		log(LOG_ERR, "digi%d: Cannot init without fep code\n",
242		    sc->res.unit);
243		return (EIO);
244	}
245	sc->status = DIGI_STATUS_NOTINIT;
246
247	if (sc->numports) {
248		/*
249		 * We're re-initialising - maybe because someone's attached
250		 * another port module.  For now, we just re-initialise
251		 * everything.
252		 */
253		if (digi_inuse(sc))
254			return (EBUSY);
255
256		digi_free_state(sc);
257	}
258
259	ptr = sc->setwin(sc, MISCGLOBAL);
260	for (i = 0; i < 16; i += 2)
261		vW(ptr + i) = 0;
262
263	switch (sc->model) {
264	case PCXEVE:
265		outb(sc->wport, 0xff);		/* window 7 */
266		ptr = sc->vmem + (BIOSCODE & 0x1fff);
267
268		if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
269			device_printf(sc->dev, "BIOS upload failed\n");
270			return (EIO);
271		}
272
273		outb(sc->port, FEPCLR);
274		break;
275
276	case PCXE:
277	case PCXI:
278	case PCCX:
279		ptr = sc->setwin(sc, BIOSCODE + ((0xf000 - sc->mem_seg) << 4));
280		if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
281			device_printf(sc->dev, "BIOS upload failed\n");
282			return (EIO);
283		}
284		break;
285
286	case PCXEM:
287	case PCIEPCX:
288	case PCIXR:
289		if (sc->pcibus)
290			PCIPORT = FEPRST;
291		else
292			outb(sc->port, FEPRST | FEPMEM);
293
294		for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) &
295		    FEPMASK) != FEPRST; i++) {
296			if (i > hz) {
297				log(LOG_ERR, "digi%d: %s init reset failed\n",
298				    sc->res.unit, sc->name);
299				return (EIO);
300			}
301			digi_delay(sc, "digiinit0", 5);
302		}
303		DLOG(DIGIDB_INIT, (sc->dev, "Got init reset after %d us\n", i));
304
305		/* Now upload the BIOS */
306		cnt = (sc->bios.size < sc->win_size - BIOSOFFSET) ?
307		    sc->bios.size : sc->win_size - BIOSOFFSET;
308
309		ptr = sc->setwin(sc, BIOSOFFSET);
310		if (!digi_bcopy(sc->bios.data, ptr, cnt)) {
311			device_printf(sc->dev, "BIOS upload (1) failed\n");
312			return (EIO);
313		}
314
315		if (cnt != sc->bios.size) {
316			/* and the second part */
317			ptr = sc->setwin(sc, sc->win_size);
318			if (!digi_bcopy(sc->bios.data + cnt, ptr,
319			    sc->bios.size - cnt)) {
320				device_printf(sc->dev, "BIOS upload failed\n");
321				return (EIO);
322			}
323		}
324
325		ptr = sc->setwin(sc, 0);
326		vW(ptr + 0) = 0x0401;
327		vW(ptr + 2) = 0x0bf0;
328		vW(ptr + 4) = 0x0000;
329		vW(ptr + 6) = 0x0000;
330
331		break;
332	}
333
334	DLOG(DIGIDB_INIT, (sc->dev, "BIOS uploaded\n"));
335
336	ptr = sc->setwin(sc, MISCGLOBAL);
337	W(ptr) = 0;
338
339	if (sc->pcibus) {
340		PCIPORT = FEPCLR;
341		resp = FEPRST;
342	} else if (sc->model == PCXEVE) {
343		outb(sc->port, FEPCLR);
344		resp = FEPRST;
345	} else {
346		outb(sc->port, FEPCLR | FEPMEM);
347		resp = FEPRST | FEPMEM;
348	}
349
350	for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) & FEPMASK)
351	    == resp; i++) {
352		if (i > hz) {
353			log(LOG_ERR, "digi%d: BIOS start failed\n",
354			    sc->res.unit);
355			return (EIO);
356		}
357		digi_delay(sc, "digibios0", 5);
358	}
359
360	DLOG(DIGIDB_INIT, (sc->dev, "BIOS started after %d us\n", i));
361
362	for (i = 0; vW(ptr) != *(u_short *)"GD"; i++) {
363		if (i > 5*hz) {
364			log(LOG_ERR, "digi%d: BIOS boot failed "
365			    "(0x%02x != 0x%02x)\n",
366			    sc->res.unit, vW(ptr), *(u_short *)"GD");
367			return (EIO);
368		}
369		digi_delay(sc, "digibios1", 5);
370	}
371
372	DLOG(DIGIDB_INIT, (sc->dev, "BIOS booted after %d iterations\n", i));
373
374	if (sc->link.data != NULL) {
375		DLOG(DIGIDB_INIT, (sc->dev, "Loading link data\n"));
376		ptr = sc->setwin(sc, 0xcd0);
377		digi_bcopy(sc->link.data, ptr, 21);	/* XXX 21 ? */
378	}
379
380	/* load FEP/OS */
381
382	switch (sc->model) {
383	case PCXE:
384	case PCXEVE:
385	case PCXI:
386		ptr = sc->setwin(sc, sc->model == PCXI ? 0x2000 : 0x0);
387		digi_bcopy(sc->fep.data, ptr, sc->fep.size);
388
389		/* A BIOS request to move our data to 0x2000 */
390		ptr = sc->setwin(sc, MBOX);
391		vW(ptr + 0) = 2;
392		vW(ptr + 2) = sc->mem_seg + FEPCODESEG;
393		vW(ptr + 4) = 0;
394		vW(ptr + 6) = FEPCODESEG;
395		vW(ptr + 8) = 0;
396		vW(ptr + 10) = sc->fep.size;
397
398		/* Run the BIOS request */
399		outb(sc->port, FEPREQ | FEPMEM);
400		outb(sc->port, FEPCLR | FEPMEM);
401
402		for (i = 0; W(ptr); i++) {
403			if (i > hz) {
404				log(LOG_ERR, "digi%d: FEP/OS move failed\n",
405				    sc->res.unit);
406				sc->hidewin(sc);
407				return (EIO);
408			}
409			digi_delay(sc, "digifep0", 5);
410		}
411		DLOG(DIGIDB_INIT,
412		    (sc->dev, "FEP/OS moved after %d iterations\n", i));
413
414		/* Clear the confirm word */
415		ptr = sc->setwin(sc, FEPSTAT);
416		vW(ptr + 0) = 0;
417
418		/* A BIOS request to execute the FEP/OS */
419		ptr = sc->setwin(sc, MBOX);
420		vW(ptr + 0) = 0x01;
421		vW(ptr + 2) = FEPCODESEG;
422		vW(ptr + 4) = 0x04;
423
424		/* Run the BIOS request */
425		outb(sc->port, FEPREQ);
426		outb(sc->port, FEPCLR);
427
428		ptr = sc->setwin(sc, FEPSTAT);
429
430		break;
431
432	case PCXEM:
433	case PCIEPCX:
434	case PCIXR:
435		DLOG(DIGIDB_INIT, (sc->dev, "Loading FEP/OS\n"));
436
437		cnt = (sc->fep.size < sc->win_size - BIOSOFFSET) ?
438		    sc->fep.size : sc->win_size - BIOSOFFSET;
439
440		ptr = sc->setwin(sc, BIOSOFFSET);
441		digi_bcopy(sc->fep.data, ptr, cnt);
442
443		if (cnt != sc->fep.size) {
444			ptr = sc->setwin(sc, BIOSOFFSET + cnt);
445			digi_bcopy(sc->fep.data + cnt, ptr,
446			    sc->fep.size - cnt);
447		}
448
449		DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS loaded\n"));
450
451		ptr = sc->setwin(sc, 0xc30);
452		W(ptr + 4) = 0x1004;
453		W(ptr + 6) = 0xbfc0;
454		W(ptr + 0) = 0x03;
455		W(ptr + 2) = 0x00;
456
457		/* Clear the confirm word */
458		ptr = sc->setwin(sc, FEPSTAT);
459		W(ptr + 0) = 0;
460
461		if (sc->port)
462			outb(sc->port, 0);		/* XXX necessary ? */
463
464		break;
465
466	case PCCX:
467		ptr = sc->setwin(sc, 0xd000);
468		digi_bcopy(sc->fep.data, ptr, sc->fep.size);
469
470		/* A BIOS request to execute the FEP/OS */
471		ptr = sc->setwin(sc, 0xc40);
472		W(ptr + 0) = 1;
473		W(ptr + 2) = FEPCODE >> 4;
474		W(ptr + 4) = 4;
475
476		/* Clear the confirm word */
477		ptr = sc->setwin(sc, FEPSTAT);
478		W(ptr + 0) = 0;
479
480		/* Run the BIOS request */
481		outb(sc->port, FEPREQ | FEPMEM); /* send interrupt to BIOS */
482		outb(sc->port, FEPCLR | FEPMEM);
483		break;
484	}
485
486	/* Now wait 'till the FEP/OS has booted */
487	for (i = 0; vW(ptr) != *(u_short *)"OS"; i++) {
488		if (i > 2*hz) {
489			log(LOG_ERR, "digi%d: FEP/OS start failed "
490			    "(0x%02x != 0x%02x)\n",
491			    sc->res.unit, vW(ptr), *(u_short *)"OS");
492			sc->hidewin(sc);
493			return (EIO);
494		}
495		digi_delay(sc, "digifep1", 5);
496	}
497
498	DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS started after %d iterations\n", i));
499
500	if (sc->model >= PCXEM) {
501		ptr = sc->setwin(sc, 0xe04);
502		vW(ptr) = 2;
503		ptr = sc->setwin(sc, 0xc02);
504		sc->numports = vW(ptr);
505	} else {
506		ptr = sc->setwin(sc, 0xc22);
507		sc->numports = vW(ptr);
508	}
509
510	if (sc->numports == 0) {
511		device_printf(sc->dev, "%s, 0 ports found\n", sc->name);
512		sc->hidewin(sc);
513		return (0);
514	}
515
516	device_printf(sc->dev, "%s, %d ports found\n", sc->name, sc->numports);
517
518	if (sc->ports)
519		free(sc->ports, M_TTYS);
520	sc->ports = malloc(sizeof(struct digi_p) * sc->numports,
521	    M_TTYS, M_WAITOK | M_ZERO);
522
523	/*
524	 * XXX Should read port 0xc90 for an array of 2byte values, 1 per
525	 * port.  If the value is 0, the port is broken....
526	 */
527
528	ptr = sc->setwin(sc, 0);
529
530	/* We should now init per-port structures */
531	bc = (volatile struct board_chan *)(ptr + CHANSTRUCT);
532	sc->gdata = (volatile struct global_data *)(ptr + FEP_GLOBAL);
533
534	sc->memcmd = ptr + sc->gdata->cstart;
535	sc->memevent = ptr + sc->gdata->istart;
536
537	for (i = 0; i < sc->numports; i++, bc++) {
538		port = sc->ports + i;
539		port->pnum = i;
540		port->sc = sc;
541		port->status = ENABLED;
542		port->bc = bc;
543		tp = port->tp = ttyalloc();
544		tp->t_oproc = digistart;
545		tp->t_param = digiparam;
546		tp->t_modem = digimodem;
547		tp->t_break = digibreak;
548		tp->t_stop = digistop;
549		tp->t_cioctl = digisioctl;
550		tp->t_ioctl = digiioctl;
551		tp->t_open = digiopen;
552		tp->t_close = digiclose;
553		tp->t_sc = port;
554
555		if (sc->model == PCXEVE) {
556			port->txbuf = ptr +
557			    (((bc->tseg - sc->mem_seg) << 4) & 0x1fff);
558			port->rxbuf = ptr +
559			    (((bc->rseg - sc->mem_seg) << 4) & 0x1fff);
560			port->txwin = FEPWIN | ((bc->tseg - sc->mem_seg) >> 9);
561			port->rxwin = FEPWIN | ((bc->rseg - sc->mem_seg) >> 9);
562		} else if (sc->model == PCXI || sc->model == PCXE) {
563			port->txbuf = ptr + ((bc->tseg - sc->mem_seg) << 4);
564			port->rxbuf = ptr + ((bc->rseg - sc->mem_seg) << 4);
565			port->txwin = port->rxwin = 0;
566		} else {
567			port->txbuf = ptr +
568			    (((bc->tseg - sc->mem_seg) << 4) % sc->win_size);
569			port->rxbuf = ptr +
570			    (((bc->rseg - sc->mem_seg) << 4) % sc->win_size);
571			port->txwin = FEPWIN |
572			    (((bc->tseg - sc->mem_seg) << 4) / sc->win_size);
573			port->rxwin = FEPWIN |
574			    (((bc->rseg - sc->mem_seg) << 4) / sc->win_size);
575		}
576		port->txbufsize = bc->tmax + 1;
577		port->rxbufsize = bc->rmax + 1;
578
579		lowwater = port->txbufsize >> 2;
580		if (lowwater > 1024)
581			lowwater = 1024;
582		sc->setwin(sc, 0);
583		fepcmd_w(port, STXLWATER, lowwater, 10);
584		fepcmd_w(port, SRXLWATER, port->rxbufsize >> 2, 10);
585		fepcmd_w(port, SRXHWATER, (3 * port->rxbufsize) >> 2, 10);
586
587		bc->edelay = 100;
588
589		ttyinitmode(tp, 0, 0);
590		port->send_ring = 1;	/* Default action on signal RI */
591		ttycreate(tp, NULL, 0, MINOR_CALLOUT, "D%r%r", sc->res.unit, i);
592	}
593
594	sc->hidewin(sc);
595	sc->inttest = timeout(digi_int_test, sc, hz);
596	/* fepcmd_w(&sc->ports[0], 0xff, 0, 0); */
597	sc->status = DIGI_STATUS_ENABLED;
598
599	return (0);
600}
601
602static int
603digimodem(struct tty *tp, int sigon, int sigoff)
604{
605	struct digi_softc *sc;
606	struct digi_p *port;
607	int bitand, bitor, mstat;
608
609	port = tp->t_sc;
610	sc = port->sc;
611
612	if (sigon == 0 && sigoff == 0) {
613		port->sc->setwin(port->sc, 0);
614		mstat = port->bc->mstat;
615		port->sc->hidewin(port->sc);
616		if (mstat & port->sc->csigs->rts)
617			sigon |= SER_RTS;
618		if (mstat & port->cd)
619			sigon |= SER_DCD;
620		if (mstat & port->dsr)
621			sigon |= SER_DSR;
622		if (mstat & port->sc->csigs->cts)
623			sigon |= SER_CTS;
624		if (mstat & port->sc->csigs->ri)
625			sigon |= SER_RI;
626		if (mstat & port->sc->csigs->dtr)
627			sigon |= SER_DTR;
628		return (sigon);
629	}
630
631	bitand = 0;
632	bitor = 0;
633
634	if (sigoff & SER_DTR)
635		bitand |= port->sc->csigs->dtr;
636	if (sigoff & SER_RTS)
637		bitand |= port->sc->csigs->rts;
638	if (sigon & SER_DTR)
639		bitor |= port->sc->csigs->dtr;
640	if (sigon & SER_RTS)
641		bitor |= port->sc->csigs->rts;
642	fepcmd_b(port, SETMODEM, bitor, ~bitand, 0);
643	return (0);
644}
645
646static int
647digicopen(struct cdev *dev, int flag, int mode, struct thread *td)
648{
649	struct digi_softc *sc;
650
651	sc = dev->si_drv1;
652	if (sc->status != DIGI_STATUS_ENABLED) {
653		DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
654		return (ENXIO);
655	}
656	sc->opencnt++;
657	return (0);
658}
659
660static int
661digiopen(struct tty *tp, struct cdev *dev)
662{
663	int error;
664	struct digi_softc *sc;
665	struct digi_p *port;
666	volatile struct board_chan *bc;
667
668	port = tp->t_sc;
669	sc = port->sc;
670
671	if (sc->status != DIGI_STATUS_ENABLED) {
672		DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
673		return (ENXIO);
674	}
675	bc = port->bc;
676
677	/*
678	 * The device isn't open, so there are no conflicts.
679	 * Initialize it.  Initialization is done twice in many
680	 * cases: to preempt sleeping callin opens if we are callout,
681	 * and to complete a callin open after DCD rises.
682	 */
683	sc->setwin(sc, 0);
684
685	bc->rout = bc->rin;	/* clear input queue */
686	bc->idata = 1;
687	bc->iempty = 1;
688	bc->ilow = 1;
689	bc->mint = port->cd | port->sc->csigs->ri;
690	bc->tin = bc->tout;
691	if (port->ialtpin) {
692		port->cd = sc->csigs->dsr;
693		port->dsr = sc->csigs->cd;
694	} else {
695		port->cd = sc->csigs->cd;
696		port->dsr = sc->csigs->dsr;
697	}
698	tp->t_wopeners++;			/* XXX required ? */
699	error = digiparam(tp, &tp->t_termios);
700	tp->t_wopeners--;
701
702	return (error);
703}
704
705static int
706digicclose(struct cdev *dev, int flag, int mode, struct thread *td)
707{
708	struct digi_softc *sc;
709
710	sc = dev->si_drv1;
711	sc->opencnt--;
712	return (0);
713}
714
715static void
716digidtrwakeup(void *chan)
717{
718	struct digi_p *port = chan;
719
720	port->status &= ~DIGI_DTR_OFF;
721	wakeup(&port->tp->t_dtr_wait);
722	port->tp->t_wopeners--;
723}
724
725static void
726digiclose(struct tty *tp)
727{
728	volatile struct board_chan *bc;
729	struct digi_p *port;
730	int s;
731
732	port = tp->t_sc;
733	bc = port->bc;
734
735	s = spltty();
736	port->sc->setwin(port->sc, 0);
737	bc->idata = 0;
738	bc->iempty = 0;
739	bc->ilow = 0;
740	bc->mint = 0;
741	if ((tp->t_cflag & HUPCL) ||
742	    (!tp->t_actout && !(bc->mstat & port->cd) &&
743	    !(tp->t_init_in.c_cflag & CLOCAL)) ||
744	    !(tp->t_state & TS_ISOPEN)) {
745		digimodem(tp, 0, SER_DTR | SER_RTS);
746		if (tp->t_dtr_wait != 0) {
747			/* Schedule a wakeup of any callin devices */
748			tp->t_wopeners++;
749			timeout(&digidtrwakeup, port, tp->t_dtr_wait);
750			port->status |= DIGI_DTR_OFF;
751		}
752	}
753	tp->t_actout = FALSE;
754	wakeup(&tp->t_actout);
755	wakeup(TSA_CARR_ON(tp));
756	splx(s);
757}
758
759/*
760 * Load module "digi_<mod>.ko" and look for a symbol called digi_mod_<mod>.
761 *
762 * Populate sc->bios, sc->fep, and sc->link from this data.
763 *
764 * sc->fep.data, sc->bios.data and sc->link.data are malloc()d according
765 * to their respective sizes.
766 *
767 * The module is unloaded when we're done.
768 */
769static int
770digi_loadmoduledata(struct digi_softc *sc)
771{
772	struct digi_mod *digi_mod;
773	linker_file_t lf;
774	char *modfile, *sym;
775	caddr_t symptr;
776	int modlen, res;
777
778	KASSERT(sc->bios.data == NULL, ("Uninitialised BIOS variable"));
779	KASSERT(sc->fep.data == NULL, ("Uninitialised FEP variable"));
780	KASSERT(sc->link.data == NULL, ("Uninitialised LINK variable"));
781	KASSERT(sc->module != NULL, ("Uninitialised module name"));
782
783	modlen = strlen(sc->module);
784	modfile = malloc(modlen + 6, M_TEMP, M_WAITOK);
785	snprintf(modfile, modlen + 6, "digi_%s", sc->module);
786	res = linker_reference_module(modfile, NULL, &lf);
787	if ((res = linker_reference_module(modfile, NULL, &lf)) != 0)
788		printf("%s: Failed %d to autoload module\n", modfile, res);
789	free(modfile, M_TEMP);
790	if (res != 0)
791		return (res);
792
793	sym = malloc(modlen + 10, M_TEMP, M_WAITOK);
794	snprintf(sym, modlen + 10, "digi_mod_%s", sc->module);
795	symptr = linker_file_lookup_symbol(lf, sym, 0);
796	free(sym, M_TEMP);
797	if (symptr == NULL) {
798		printf("digi_%s.ko: Symbol `%s' not found\n", sc->module, sym);
799		linker_file_unload(lf, LINKER_UNLOAD_FORCE);
800		return (EINVAL);
801	}
802
803	digi_mod = (struct digi_mod *)symptr;
804	if (digi_mod->dm_version != DIGI_MOD_VERSION) {
805		printf("digi_%s.ko: Invalid version %d (need %d)\n",
806		    sc->module, digi_mod->dm_version, DIGI_MOD_VERSION);
807		linker_file_unload(lf, LINKER_UNLOAD_FORCE);
808		return (EINVAL);
809	}
810
811	sc->bios.size = digi_mod->dm_bios.size;
812	if (sc->bios.size != 0 && digi_mod->dm_bios.data != NULL) {
813		sc->bios.data = malloc(sc->bios.size, M_TTYS, M_WAITOK);
814		bcopy(digi_mod->dm_bios.data, sc->bios.data, sc->bios.size);
815	}
816
817	sc->fep.size = digi_mod->dm_fep.size;
818	if (sc->fep.size != 0 && digi_mod->dm_fep.data != NULL) {
819		sc->fep.data = malloc(sc->fep.size, M_TTYS, M_WAITOK);
820		bcopy(digi_mod->dm_fep.data, sc->fep.data, sc->fep.size);
821	}
822
823	sc->link.size = digi_mod->dm_link.size;
824	if (sc->link.size != 0 && digi_mod->dm_link.data != NULL) {
825		sc->link.data = malloc(sc->link.size, M_TTYS, M_WAITOK);
826		bcopy(digi_mod->dm_link.data, sc->link.data, sc->link.size);
827	}
828
829	linker_file_unload(lf, LINKER_UNLOAD_FORCE);
830
831	return (0);
832}
833
834static int
835digisioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
836{
837	struct digi_p *port;
838	struct digi_softc *sc;
839
840	port = dev->si_drv1;
841	sc = port->sc;
842
843	switch (cmd) {
844	case DIGIIO_GETALTPIN:
845		if (ISINIT(dev))
846			*(int *)data = port->ialtpin;
847		else if (ISLOCK(dev))
848			*(int *)data = port->laltpin;
849		else
850			return (ENOTTY);
851		break;
852	case DIGIIO_SETALTPIN:
853		if (ISINIT(dev)) {
854			if (!port->laltpin) {
855				port->ialtpin = !!*(int *)data;
856				DLOG(DIGIDB_SET, (sc->dev,
857				    "port%d: initial ALTPIN %s\n", port->pnum,
858				    port->ialtpin ? "set" : "cleared"));
859			}
860		} else if (ISLOCK(dev)) {
861			port->laltpin = !!*(int *)data;
862			DLOG(DIGIDB_SET, (sc->dev,
863			    "port%d: ALTPIN %slocked\n",
864			    port->pnum, port->laltpin ? "" : "un"));
865		} else
866			return (ENOTTY);
867		break;
868	default:
869		return (ENOTTY);
870	}
871	return (0);
872}
873
874static int
875digicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
876{
877	int error;
878	struct digi_softc *sc;
879
880	sc = dev->si_drv1;
881
882	if (sc->status == DIGI_STATUS_DISABLED)
883		return (ENXIO);
884
885	switch (cmd) {
886	case DIGIIO_DEBUG:
887#ifdef DEBUG
888		digi_debug = *(int *)data;
889		return (0);
890#else
891		device_printf(sc->dev, "DEBUG not defined\n");
892		return (ENXIO);
893#endif
894	case DIGIIO_REINIT:
895		digi_loadmoduledata(sc);
896		error = digi_init(sc);
897		digi_freemoduledata(sc);
898		return (error);
899
900	case DIGIIO_MODEL:
901		*(enum digi_model *)data = sc->model;
902		return (0);
903
904	case DIGIIO_IDENT:
905		return (copyout(sc->name, *(char **)data,
906		    strlen(sc->name) + 1));
907	default:
908		return (ENOIOCTL);
909	}
910}
911
912static int
913digiioctl(struct tty *tp, u_long cmd, void *data, int flag, struct thread *td)
914{
915	struct digi_softc *sc;
916	struct digi_p *port;
917
918	port = tp->t_sc;
919	sc = port->sc;
920	if (sc->status == DIGI_STATUS_DISABLED)
921		return (ENXIO);
922
923	if (!(port->status & ENABLED))
924		return (ENXIO);
925
926	switch (cmd) {
927	case DIGIIO_GETALTPIN:
928		*(int *)data = !!(port->dsr == sc->csigs->cd);
929		return (0);
930
931	case DIGIIO_SETALTPIN:
932		if (!port->laltpin) {
933			if (*(int *)data) {
934				DLOG(DIGIDB_SET, (sc->dev,
935				    "port%d: ALTPIN set\n", port->pnum));
936				port->cd = sc->csigs->dsr;
937				port->dsr = sc->csigs->cd;
938			} else {
939				DLOG(DIGIDB_SET, (sc->dev,
940				    "port%d: ALTPIN cleared\n", port->pnum));
941				port->cd = sc->csigs->cd;
942				port->dsr = sc->csigs->dsr;
943			}
944		}
945		return (0);
946	case DIGIIO_RING:
947		port->send_ring = *(u_char *)data;
948		break;
949	default:
950		return (ENOTTY);
951	}
952	return (0);
953}
954
955static void
956digibreak(struct tty *tp, int brk)
957{
958	struct digi_p *port;
959
960	port = tp->t_sc;
961
962	/*
963	 * now it sends 400 millisecond break because I don't know
964	 * how to send an infinite break
965	 */
966	if (brk)
967		fepcmd_w(port, SENDBREAK, 400, 10);
968}
969
970static int
971digiparam(struct tty *tp, struct termios *t)
972{
973	struct digi_softc *sc;
974	struct digi_p *port;
975	int cflag;
976	int iflag;
977	int hflow;
978	int s;
979	int window;
980
981	port = tp->t_sc;
982	sc = port->sc;
983	DLOG(DIGIDB_SET, (sc->dev, "port%d: setting parameters\n", port->pnum));
984
985	if (t->c_ispeed == 0)
986		t->c_ispeed = t->c_ospeed;
987
988	cflag = ttspeedtab(t->c_ospeed, digispeedtab);
989
990	if (cflag < 0 || (cflag > 0 && t->c_ispeed != t->c_ospeed))
991		return (EINVAL);
992
993	s = splclock();
994
995	window = sc->window;
996	sc->setwin(sc, 0);
997
998	if (cflag == 0) {				/* hangup */
999		DLOG(DIGIDB_SET, (sc->dev, "port%d: hangup\n", port->pnum));
1000		digimodem(port->tp, 0, SER_DTR | SER_RTS);
1001	} else {
1002		digimodem(port->tp, SER_DTR | SER_RTS, 0);
1003
1004		DLOG(DIGIDB_SET, (sc->dev, "port%d: CBAUD = %d\n", port->pnum,
1005		    cflag));
1006
1007#if 0
1008		/* convert flags to sysV-style values */
1009		if (t->c_cflag & PARODD)
1010			cflag |= 0x0200;
1011		if (t->c_cflag & PARENB)
1012			cflag |= 0x0100;
1013		if (t->c_cflag & CSTOPB)
1014			cflag |= 0x0080;
1015#else
1016		/* convert flags to sysV-style values */
1017		if (t->c_cflag & PARODD)
1018			cflag |= FEP_PARODD;
1019		if (t->c_cflag & PARENB)
1020			cflag |= FEP_PARENB;
1021		if (t->c_cflag & CSTOPB)
1022			cflag |= FEP_CSTOPB;
1023		if (t->c_cflag & CLOCAL)
1024			cflag |= FEP_CLOCAL;
1025#endif
1026
1027		cflag |= (t->c_cflag & CSIZE) >> 4;
1028		DLOG(DIGIDB_SET, (sc->dev, "port%d: CFLAG = 0x%x\n", port->pnum,
1029		    cflag));
1030		fepcmd_w(port, SETCFLAGS, (unsigned)cflag, 0);
1031	}
1032
1033	iflag =
1034	    t->c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP);
1035	if (port->c_iflag & IXON)
1036		iflag |= 0x400;
1037	if (port->c_iflag & IXANY)
1038		iflag |= 0x800;
1039	if (port->c_iflag & IXOFF)
1040		iflag |= 0x1000;
1041
1042	DLOG(DIGIDB_SET, (sc->dev, "port%d: set iflag = 0x%x\n", port->pnum, iflag));
1043	fepcmd_w(port, SETIFLAGS, (unsigned)iflag, 0);
1044
1045	hflow = 0;
1046	if (t->c_cflag & CDTR_IFLOW)
1047		hflow |= sc->csigs->dtr;
1048	if (t->c_cflag & CRTS_IFLOW)
1049		hflow |= sc->csigs->rts;
1050	if (t->c_cflag & CCTS_OFLOW)
1051		hflow |= sc->csigs->cts;
1052	if (t->c_cflag & CDSR_OFLOW)
1053		hflow |= port->dsr;
1054	if (t->c_cflag & CCAR_OFLOW)
1055		hflow |= port->cd;
1056
1057	DLOG(DIGIDB_SET, (sc->dev, "port%d: set hflow = 0x%x\n", port->pnum, hflow));
1058	fepcmd_w(port, SETHFLOW, 0xff00 | (unsigned)hflow, 0);
1059
1060	DLOG(DIGIDB_SET, (sc->dev, "port%d: set startc(0x%x), stopc(0x%x)\n",
1061	    port->pnum, t->c_cc[VSTART], t->c_cc[VSTOP]));
1062	fepcmd_b(port, SONOFFC, t->c_cc[VSTART], t->c_cc[VSTOP], 0);
1063
1064	if (sc->window != 0)
1065		sc->towin(sc, 0);
1066	if (window != 0)
1067		sc->towin(sc, window);
1068	splx(s);
1069
1070	return (0);
1071}
1072
1073static void
1074digi_intr(void *vp)
1075{
1076	struct digi_p *port;
1077	char *cxcon;
1078	struct digi_softc *sc;
1079	int ehead, etail;
1080	volatile struct board_chan *bc;
1081	struct tty *tp;
1082	int head, tail;
1083	int wrapmask;
1084	int size, window;
1085	struct event {
1086		u_char pnum;
1087		u_char event;
1088		u_char mstat;
1089		u_char lstat;
1090	} event;
1091
1092	sc = vp;
1093
1094	if (sc->status != DIGI_STATUS_ENABLED) {
1095		DLOG(DIGIDB_IRQ, (sc->dev, "interrupt on disabled board !\n"));
1096		return;
1097	}
1098
1099#ifdef DIGI_INTERRUPT
1100	microtime(&sc->intr_timestamp);
1101#endif
1102
1103	window = sc->window;
1104	sc->setwin(sc, 0);
1105
1106	if (sc->model >= PCXEM && W(sc->vmem + 0xd00)) {
1107		struct con_bios *con = con_bios_list;
1108		register u_char *ptr;
1109
1110		ptr = sc->vmem + W(sc->vmem + 0xd00);
1111		while (con) {
1112			if (ptr[1] && W(ptr + 2) == W(con->bios + 2))
1113				/* Not first block -- exact match */
1114				break;
1115
1116			if (W(ptr + 4) >= W(con->bios + 4) &&
1117			    W(ptr + 4) <= W(con->bios + 6))
1118				/* Initial search concetrator BIOS */
1119				break;
1120		}
1121
1122		if (con == NULL) {
1123			log(LOG_ERR, "digi%d: wanted bios LREV = 0x%04x"
1124			    " not found!\n", sc->res.unit, W(ptr + 4));
1125			W(ptr + 10) = 0;
1126			W(sc->vmem + 0xd00) = 0;
1127			goto eoi;
1128		}
1129		cxcon = con->bios;
1130		W(ptr + 4) = W(cxcon + 4);
1131		W(ptr + 6) = W(cxcon + 6);
1132		if (ptr[1] == 0)
1133			W(ptr + 2) = W(cxcon + 2);
1134		W(ptr + 8) = (ptr[1] << 6) + W(cxcon + 8);
1135		size = W(cxcon + 10) - (ptr[1] << 10);
1136		if (size <= 0) {
1137			W(ptr + 8) = W(cxcon + 8);
1138			W(ptr + 10) = 0;
1139		} else {
1140			if (size > 1024)
1141				size = 1024;
1142			W(ptr + 10) = size;
1143			bcopy(cxcon + (ptr[1] << 10), ptr + 12, size);
1144		}
1145		W(sc->vmem + 0xd00) = 0;
1146		goto eoi;
1147	}
1148
1149	ehead = sc->gdata->ein;
1150	etail = sc->gdata->eout;
1151	if (ehead == etail) {
1152#ifdef DEBUG
1153		sc->intr_count++;
1154		if (sc->intr_count % 6000 == 0) {
1155			DLOG(DIGIDB_IRQ, (sc->dev,
1156			    "6000 useless polls %x %x\n", ehead, etail));
1157			sc->intr_count = 0;
1158		}
1159#endif
1160		goto eoi;
1161	}
1162	while (ehead != etail) {
1163		event = *(volatile struct event *)(sc->memevent + etail);
1164
1165		etail = (etail + 4) & sc->gdata->imax;
1166
1167		if (event.pnum >= sc->numports) {
1168			log(LOG_ERR, "digi%d: port %d: got event"
1169			    " on nonexisting port\n", sc->res.unit,
1170			    event.pnum);
1171			continue;
1172		}
1173		port = &sc->ports[event.pnum];
1174		bc = port->bc;
1175		tp = port->tp;
1176
1177		if (!(tp->t_state & TS_ISOPEN) && !tp->t_wopeners) {
1178			DLOG(DIGIDB_IRQ, (sc->dev,
1179			    "port %d: event 0x%x on closed port\n",
1180			    event.pnum, event.event));
1181			bc->rout = bc->rin;
1182			bc->idata = 0;
1183			bc->iempty = 0;
1184			bc->ilow = 0;
1185			bc->mint = 0;
1186			continue;
1187		}
1188		if (event.event & ~ALL_IND)
1189			log(LOG_ERR, "digi%d: port%d: ? event 0x%x mstat 0x%x"
1190			    " lstat 0x%x\n", sc->res.unit, event.pnum,
1191			    event.event, event.mstat, event.lstat);
1192
1193		if (event.event & DATA_IND) {
1194			DLOG(DIGIDB_IRQ, (sc->dev, "port %d: DATA_IND\n",
1195			    event.pnum));
1196			wrapmask = port->rxbufsize - 1;
1197			head = bc->rin;
1198			tail = bc->rout;
1199
1200			size = 0;
1201			if (!(tp->t_state & TS_ISOPEN)) {
1202				bc->rout = head;
1203				goto end_of_data;
1204			}
1205			while (head != tail) {
1206				int top;
1207
1208				DLOG(DIGIDB_INT, (sc->dev,
1209				    "port %d: p rx head = %d tail = %d\n",
1210				    event.pnum, head, tail));
1211				top = (head > tail) ? head : wrapmask + 1;
1212				sc->towin(sc, port->rxwin);
1213				size = top - tail;
1214				if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1215					size = b_to_q((char *)port->rxbuf +
1216					    tail, size, &tp->t_rawq);
1217					tail = top - size;
1218					ttwakeup(tp);
1219				} else for (; tail < top;) {
1220					ttyld_rint(tp, port->rxbuf[tail]);
1221					sc->towin(sc, port->rxwin);
1222					size--;
1223					tail++;
1224					if (tp->t_state & TS_TBLOCK)
1225						break;
1226				}
1227				tail &= wrapmask;
1228				sc->setwin(sc, 0);
1229				bc->rout = tail;
1230				head = bc->rin;
1231				if (size)
1232					break;
1233			}
1234
1235			if (bc->orun) {
1236				CE_RECORD(port, CE_OVERRUN);
1237				log(LOG_ERR, "digi%d: port%d: %s\n",
1238				    sc->res.unit, event.pnum,
1239				    digi_errortxt(CE_OVERRUN));
1240				bc->orun = 0;
1241			}
1242end_of_data:
1243			if (size) {
1244				tp->t_state |= TS_TBLOCK;
1245				port->status |= PAUSE_RX;
1246				DLOG(DIGIDB_RX, (sc->dev, "port %d: pause RX\n",
1247				    event.pnum));
1248			} else {
1249				bc->idata = 1;
1250			}
1251		}
1252
1253		if (event.event & MODEMCHG_IND) {
1254			DLOG(DIGIDB_MODEM, (sc->dev, "port %d: MODEMCHG_IND\n",
1255			    event.pnum));
1256
1257			if ((event.mstat ^ event.lstat) & port->cd) {
1258				sc->hidewin(sc);
1259				ttyld_modem(tp, event.mstat & port->cd);
1260				sc->setwin(sc, 0);
1261				wakeup(TSA_CARR_ON(tp));
1262			}
1263
1264			if (event.mstat & sc->csigs->ri) {
1265				DLOG(DIGIDB_RI, (sc->dev, "port %d: RING\n",
1266				    event.pnum));
1267				if (port->send_ring) {
1268					ttyld_rint(tp, 'R');
1269					ttyld_rint(tp, 'I');
1270					ttyld_rint(tp, 'N');
1271					ttyld_rint(tp, 'G');
1272					ttyld_rint(tp, '\r');
1273					ttyld_rint(tp, '\n');
1274				}
1275			}
1276		}
1277		if (event.event & BREAK_IND) {
1278			DLOG(DIGIDB_MODEM, (sc->dev, "port %d: BREAK_IND\n",
1279			    event.pnum));
1280			ttyld_rint(tp, TTY_BI);
1281		}
1282		if (event.event & (LOWTX_IND | EMPTYTX_IND)) {
1283			DLOG(DIGIDB_IRQ, (sc->dev, "port %d:%s%s\n",
1284			    event.pnum,
1285			    event.event & LOWTX_IND ? " LOWTX" : "",
1286			    event.event & EMPTYTX_IND ?  " EMPTYTX" : ""));
1287			ttyld_start(tp);
1288		}
1289	}
1290	sc->gdata->eout = etail;
1291eoi:
1292	if (sc->window != 0)
1293		sc->towin(sc, 0);
1294	if (window != 0)
1295		sc->towin(sc, window);
1296}
1297
1298static void
1299digistart(struct tty *tp)
1300{
1301	struct digi_p *port;
1302	struct digi_softc *sc;
1303	volatile struct board_chan *bc;
1304	int head, tail;
1305	int size, ocount, totcnt = 0;
1306	int s;
1307	int wmask;
1308
1309	port = tp->t_sc;
1310	sc = port->sc;
1311	bc = port->bc;
1312
1313	wmask = port->txbufsize - 1;
1314
1315	s = spltty();
1316	port->lcc = tp->t_outq.c_cc;
1317	sc->setwin(sc, 0);
1318	if (!(tp->t_state & TS_TBLOCK)) {
1319		if (port->status & PAUSE_RX) {
1320			DLOG(DIGIDB_RX, (sc->dev, "port %d: resume RX\n",
1321			    port->pnum));
1322			/*
1323			 * CAREFUL - braces are needed here if the DLOG is
1324			 * optimised out!
1325			 */
1326		}
1327		port->status &= ~PAUSE_RX;
1328		bc->idata = 1;
1329	}
1330	if (!(tp->t_state & TS_TTSTOP) && port->status & PAUSE_TX) {
1331		DLOG(DIGIDB_TX, (sc->dev, "port %d: resume TX\n", port->pnum));
1332		port->status &= ~PAUSE_TX;
1333		fepcmd_w(port, RESUMETX, 0, 10);
1334	}
1335	if (tp->t_outq.c_cc == 0)
1336		tp->t_state &= ~TS_BUSY;
1337	else
1338		tp->t_state |= TS_BUSY;
1339
1340	head = bc->tin;
1341	while (tp->t_outq.c_cc != 0) {
1342		tail = bc->tout;
1343		DLOG(DIGIDB_INT, (sc->dev, "port%d: s tx head = %d tail = %d\n",
1344		    port->pnum, head, tail));
1345
1346		if (head < tail)
1347			size = tail - head - 1;
1348		else {
1349			size = port->txbufsize - head;
1350			if (tail == 0)
1351				size--;
1352		}
1353
1354		if (size == 0)
1355			break;
1356		sc->towin(sc, port->txwin);
1357		ocount = q_to_b(&tp->t_outq, port->txbuf + head, size);
1358		totcnt += ocount;
1359		head += ocount;
1360		head &= wmask;
1361		sc->setwin(sc, 0);
1362		bc->tin = head;
1363		bc->iempty = 1;
1364		bc->ilow = 1;
1365	}
1366	port->lostcc = tp->t_outq.c_cc;
1367	tail = bc->tout;
1368	if (head < tail)
1369		size = port->txbufsize - tail + head;
1370	else
1371		size = head - tail;
1372
1373	port->lbuf = size;
1374	DLOG(DIGIDB_INT, (sc->dev, "port%d: s total cnt = %d\n", port->pnum, totcnt));
1375	ttwwakeup(tp);
1376	splx(s);
1377}
1378
1379static void
1380digistop(struct tty *tp, int rw)
1381{
1382	struct digi_softc *sc;
1383	struct digi_p *port;
1384
1385	port = tp->t_sc;
1386	sc = port->sc;
1387
1388	DLOG(DIGIDB_TX, (sc->dev, "port %d: pause TX\n", port->pnum));
1389	port->status |= PAUSE_TX;
1390	fepcmd_w(port, PAUSETX, 0, 10);
1391}
1392
1393static void
1394fepcmd(struct digi_p *port, int cmd, int op1, int ncmds)
1395{
1396	u_char *mem;
1397	unsigned tail, head;
1398	int count, n;
1399
1400	mem = port->sc->memcmd;
1401
1402	port->sc->setwin(port->sc, 0);
1403
1404	head = port->sc->gdata->cin;
1405	mem[head + 0] = cmd;
1406	mem[head + 1] = port->pnum;
1407	*(u_short *)(mem + head + 2) = op1;
1408
1409	head = (head + 4) & port->sc->gdata->cmax;
1410	port->sc->gdata->cin = head;
1411
1412	for (count = FEPTIMEOUT; count > 0; count--) {
1413		head = port->sc->gdata->cin;
1414		tail = port->sc->gdata->cout;
1415		n = (head - tail) & port->sc->gdata->cmax;
1416
1417		if (n <= ncmds * sizeof(short) * 4)
1418			break;
1419	}
1420	if (count == 0)
1421		log(LOG_ERR, "digi%d: port%d: timeout on FEP command\n",
1422		    port->sc->res.unit, port->pnum);
1423}
1424
1425const char *
1426digi_errortxt(int id)
1427{
1428	static const char *error_desc[] = {
1429		"silo overflow",
1430		"interrupt-level buffer overflow",
1431		"tty-level buffer overflow",
1432	};
1433
1434	KASSERT(id >= 0 && id < sizeof(error_desc) / sizeof(error_desc[0]),
1435	    ("Unexpected digi error id %d\n", id));
1436
1437	return (error_desc[id]);
1438}
1439
1440int
1441digi_attach(struct digi_softc *sc)
1442{
1443	sc->res.ctldev = make_dev(&digi_csw,
1444	    sc->res.unit << 16, UID_ROOT, GID_WHEEL,
1445	    0600, "digi%r.ctl", sc->res.unit);
1446	sc->res.ctldev->si_drv1 = sc;
1447
1448	digi_loadmoduledata(sc);
1449	digi_init(sc);
1450	digi_freemoduledata(sc);
1451
1452	return (0);
1453}
1454
1455static int
1456digi_inuse(struct digi_softc *sc)
1457{
1458	int i;
1459	struct digi_p *port;
1460
1461	port = &sc->ports[0];
1462	for (i = 0; i < sc->numports; i++, port++)
1463		if (port->tp->t_state & TS_ISOPEN) {
1464			DLOG(DIGIDB_INIT, (sc->dev, "port%d: busy\n", i));
1465			return (1);
1466		} else if (port->tp->t_wopeners || port->opencnt) {
1467			DLOG(DIGIDB_INIT, (sc->dev, "port%d: blocked in open\n",
1468			    i));
1469			return (1);
1470		}
1471	return (0);
1472}
1473
1474static void
1475digi_free_state(struct digi_softc *sc)
1476{
1477	int i;
1478
1479	/* Blow it all away */
1480
1481	for (i = 0; i < sc->numports; i++)
1482		ttygone(sc->ports[i].tp);
1483
1484	/* XXX: this might be better done as a ttypurge method */
1485	untimeout(digi_poll, sc, sc->callout);
1486	callout_handle_init(&sc->callout);
1487	untimeout(digi_int_test, sc, sc->inttest);
1488	callout_handle_init(&sc->inttest);
1489
1490	for (i = 0; i < sc->numports; i++)
1491		ttyfree(sc->ports[i].tp);
1492
1493	bus_teardown_intr(sc->dev, sc->res.irq, sc->res.irqHandler);
1494#ifdef DIGI_INTERRUPT
1495	if (sc->res.irq != NULL) {
1496		bus_release_resource(dev, SYS_RES_IRQ, sc->res.irqrid,
1497		    sc->res.irq);
1498		sc->res.irq = NULL;
1499	}
1500#endif
1501	if (sc->numports) {
1502		KASSERT(sc->ports, ("digi%d: Lost my ports ?", sc->res.unit));
1503		free(sc->ports, M_TTYS);
1504		sc->ports = NULL;
1505		sc->numports = 0;
1506	}
1507
1508	sc->status = DIGI_STATUS_NOTINIT;
1509}
1510
1511int
1512digi_detach(device_t dev)
1513{
1514	struct digi_softc *sc = device_get_softc(dev);
1515
1516	DLOG(DIGIDB_INIT, (sc->dev, "detaching\n"));
1517
1518	/* If we're INIT'd, numports must be 0 */
1519	KASSERT(sc->numports == 0 || sc->status != DIGI_STATUS_NOTINIT,
1520	    ("digi%d: numports(%d) & status(%d) are out of sync",
1521	    sc->res.unit, sc->numports, (int)sc->status));
1522
1523	if (digi_inuse(sc))
1524		return (EBUSY);
1525
1526	digi_free_state(sc);
1527
1528	destroy_dev(sc->res.ctldev);
1529
1530	if (sc->res.mem != NULL) {
1531		bus_release_resource(dev, SYS_RES_MEMORY, sc->res.mrid,
1532		    sc->res.mem);
1533		sc->res.mem = NULL;
1534	}
1535	if (sc->res.io != NULL) {
1536		bus_release_resource(dev, SYS_RES_IOPORT, sc->res.iorid,
1537		    sc->res.io);
1538		sc->res.io = NULL;
1539	}
1540
1541	return (0);
1542}
1543
1544int
1545digi_shutdown(device_t dev)
1546{
1547	return (0);
1548}
1549
1550MODULE_VERSION(digi, 1);
1551