digi.c revision 130892
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 130892 2004-06-21 22:57:16Z phk $
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/bus.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
62
63#define	CTRL_DEV		0x800000
64#define	CALLOUT_MASK		0x400000
65#define	CONTROL_INIT_STATE	0x100000
66#define	CONTROL_LOCK_STATE	0x200000
67#define	CONTROL_MASK		(CTRL_DEV|CONTROL_INIT_STATE|CONTROL_LOCK_STATE)
68#define UNIT_MASK		0x030000
69#define PORT_MASK		0x0000FF
70#define	DEV_TO_UNIT(dev)	(MINOR_TO_UNIT(minor(dev)))
71#define	MINOR_MAGIC_MASK	(CALLOUT_MASK | CONTROL_MASK)
72#define	MINOR_TO_UNIT(mynor)	(((mynor) & UNIT_MASK)>>16)
73#define MINOR_TO_PORT(mynor)	((mynor) & PORT_MASK)
74
75static d_open_t		digiopen;
76static d_close_t	digiclose;
77static d_read_t		digiread;
78static d_write_t	digiwrite;
79static d_ioctl_t	digiioctl;
80
81static void	digistop(struct tty *tp, int rw);
82static int	digimctl(struct digi_p *port, int bits, int how);
83static void	digi_poll(void *ptr);
84static void	digi_freemoduledata(struct digi_softc *);
85static void	fepcmd(struct digi_p *port, int cmd, int op, int ncmds);
86static void	digistart(struct tty *tp);
87static int	digiparam(struct tty *tp, struct termios *t);
88static void	digihardclose(struct digi_p *port);
89static void	digi_intr(void *);
90static int	digi_init(struct digi_softc *_sc);
91static int	digi_loadmoduledata(struct digi_softc *);
92static int	digi_inuse(struct digi_softc *);
93static void	digi_free_state(struct digi_softc *);
94
95#define	fepcmd_b(port, cmd, op1, op2, ncmds) \
96	fepcmd(port, cmd, (op2 << 8) | op1, ncmds)
97#define	fepcmd_w	fepcmd
98
99
100static speed_t digidefaultrate = TTYDEF_SPEED;
101
102struct con_bios {
103	struct con_bios *next;
104	u_char *bios;
105	size_t size;
106};
107
108static struct con_bios *con_bios_list;
109devclass_t	 digi_devclass;
110static char	 driver_name[] = "digi";
111unsigned 	 digi_debug = 0;
112
113static struct speedtab digispeedtab[] = {
114	{ 0,		0},			/* old (sysV-like) Bx codes */
115	{ 50,		1},
116	{ 75,		2},
117	{ 110,		3},
118	{ 134,		4},
119	{ 150,		5},
120	{ 200,		6},
121	{ 300,		7},
122	{ 600,		8},
123	{ 1200,		9},
124	{ 1800,		10},
125	{ 2400,		11},
126	{ 4800,		12},
127	{ 9600,		13},
128	{ 19200,	14},
129	{ 38400,	15},
130	{ 57600,	(02000 | 1)},
131	{ 76800,	(02000 | 2)},
132	{ 115200,	(02000 | 3)},
133	{ 230400,	(02000 | 6)},
134	{ -1,		-1}
135};
136
137const struct digi_control_signals digi_xixe_signals = {
138	0x02, 0x08, 0x10, 0x20, 0x40, 0x80
139};
140
141const struct digi_control_signals digi_normal_signals = {
142	0x02, 0x80, 0x20, 0x10, 0x40, 0x01
143};
144
145static struct cdevsw digi_sw = {
146	.d_version =	D_VERSION,
147	.d_open =	digiopen,
148	.d_close =	digiclose,
149	.d_read =	digiread,
150	.d_write =	digiwrite,
151	.d_ioctl =	digiioctl,
152	.d_name =	driver_name,
153	.d_flags =	D_TTY | D_NEEDGIANT,
154};
155
156static void
157digi_poll(void *ptr)
158{
159	struct digi_softc *sc;
160
161	sc = (struct digi_softc *)ptr;
162	callout_handle_init(&sc->callout);
163	digi_intr(sc);
164	sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
165}
166
167static void
168digi_int_test(void *v)
169{
170	struct digi_softc *sc = v;
171
172	callout_handle_init(&sc->inttest);
173#ifdef DIGI_INTERRUPT
174	if (sc->intr_timestamp.tv_sec || sc->intr_timestamp.tv_usec) {
175		/* interrupt OK! */
176		return;
177	}
178	log(LOG_ERR, "digi%d: Interrupt didn't work, use polled mode\n", unit);
179#endif
180	sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
181}
182
183static void
184digi_freemoduledata(struct digi_softc *sc)
185{
186	if (sc->fep.data != NULL) {
187		free(sc->fep.data, M_TTYS);
188		sc->fep.data = NULL;
189	}
190	if (sc->link.data != NULL) {
191		free(sc->link.data, M_TTYS);
192		sc->link.data = NULL;
193	}
194	if (sc->bios.data != NULL) {
195		free(sc->bios.data, M_TTYS);
196		sc->bios.data = NULL;
197	}
198}
199
200static int
201digi_bcopy(const void *vfrom, void *vto, size_t sz)
202{
203	volatile const char *from = (volatile const char *)vfrom;
204	volatile char *to = (volatile char *)vto;
205	size_t i;
206
207	for (i = 0; i < sz; i++)
208		*to++ = *from++;
209
210	from = (const volatile char *)vfrom;
211	to = (volatile char *)vto;
212	for (i = 0; i < sz; i++)
213		if (*to++ != *from++)
214			return (0);
215	return (1);
216}
217
218void
219digi_delay(struct digi_softc *sc, const char *txt, u_long timo)
220{
221	if (cold)
222		DELAY(timo * 1000000 / hz);
223	else
224		tsleep(sc, PUSER | PCATCH, txt, timo);
225}
226
227static int
228digi_init(struct digi_softc *sc)
229{
230	int i, cnt, resp;
231	u_char *ptr;
232	int lowwater;
233	struct digi_p *port;
234	volatile struct board_chan *bc;
235
236	ptr = NULL;
237
238	if (sc->status == DIGI_STATUS_DISABLED) {
239		log(LOG_ERR, "digi%d: Cannot init a disabled card\n",
240		    sc->res.unit);
241		return (EIO);
242	}
243	if (sc->bios.data == NULL) {
244		log(LOG_ERR, "digi%d: Cannot init without BIOS\n",
245		    sc->res.unit);
246		return (EIO);
247	}
248#if 0
249	if (sc->link.data == NULL && sc->model >= PCCX) {
250		log(LOG_ERR, "digi%d: Cannot init without link info\n",
251		    sc->res.unit);
252		return (EIO);
253	}
254#endif
255	if (sc->fep.data == NULL) {
256		log(LOG_ERR, "digi%d: Cannot init without fep code\n",
257		    sc->res.unit);
258		return (EIO);
259	}
260	sc->status = DIGI_STATUS_NOTINIT;
261
262	if (sc->numports) {
263		/*
264		 * We're re-initialising - maybe because someone's attached
265		 * another port module.  For now, we just re-initialise
266		 * everything.
267		 */
268		if (digi_inuse(sc))
269			return (EBUSY);
270
271		digi_free_state(sc);
272	}
273
274	ptr = sc->setwin(sc, MISCGLOBAL);
275	for (i = 0; i < 16; i += 2)
276		vW(ptr + i) = 0;
277
278	switch (sc->model) {
279	case PCXEVE:
280		outb(sc->wport, 0xff);		/* window 7 */
281		ptr = sc->vmem + (BIOSCODE & 0x1fff);
282
283		if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
284			device_printf(sc->dev, "BIOS upload failed\n");
285			return (EIO);
286		}
287
288		outb(sc->port, FEPCLR);
289		break;
290
291	case PCXE:
292	case PCXI:
293	case PCCX:
294		ptr = sc->setwin(sc, BIOSCODE + ((0xf000 - sc->mem_seg) << 4));
295		if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
296			device_printf(sc->dev, "BIOS upload failed\n");
297			return (EIO);
298		}
299		break;
300
301	case PCXEM:
302	case PCIEPCX:
303	case PCIXR:
304		if (sc->pcibus)
305			PCIPORT = FEPRST;
306		else
307			outb(sc->port, FEPRST | FEPMEM);
308
309		for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) &
310		    FEPMASK) != FEPRST; i++) {
311			if (i > hz) {
312				log(LOG_ERR, "digi%d: %s init reset failed\n",
313				    sc->res.unit, sc->name);
314				return (EIO);
315			}
316			digi_delay(sc, "digiinit0", 5);
317		}
318		DLOG(DIGIDB_INIT, (sc->dev, "Got init reset after %d us\n", i));
319
320		/* Now upload the BIOS */
321		cnt = (sc->bios.size < sc->win_size - BIOSOFFSET) ?
322		    sc->bios.size : sc->win_size - BIOSOFFSET;
323
324		ptr = sc->setwin(sc, BIOSOFFSET);
325		if (!digi_bcopy(sc->bios.data, ptr, cnt)) {
326			device_printf(sc->dev, "BIOS upload (1) failed\n");
327			return (EIO);
328		}
329
330		if (cnt != sc->bios.size) {
331			/* and the second part */
332			ptr = sc->setwin(sc, sc->win_size);
333			if (!digi_bcopy(sc->bios.data + cnt, ptr,
334			    sc->bios.size - cnt)) {
335				device_printf(sc->dev, "BIOS upload failed\n");
336				return (EIO);
337			}
338		}
339
340		ptr = sc->setwin(sc, 0);
341		vW(ptr + 0) = 0x0401;
342		vW(ptr + 2) = 0x0bf0;
343		vW(ptr + 4) = 0x0000;
344		vW(ptr + 6) = 0x0000;
345
346		break;
347	}
348
349	DLOG(DIGIDB_INIT, (sc->dev, "BIOS uploaded\n"));
350
351	ptr = sc->setwin(sc, MISCGLOBAL);
352	W(ptr) = 0;
353
354	if (sc->pcibus) {
355		PCIPORT = FEPCLR;
356		resp = FEPRST;
357	} else if (sc->model == PCXEVE) {
358		outb(sc->port, FEPCLR);
359		resp = FEPRST;
360	} else {
361		outb(sc->port, FEPCLR | FEPMEM);
362		resp = FEPRST | FEPMEM;
363	}
364
365	for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) & FEPMASK)
366	    == resp; i++) {
367		if (i > hz) {
368			log(LOG_ERR, "digi%d: BIOS start failed\n",
369			    sc->res.unit);
370			return (EIO);
371		}
372		digi_delay(sc, "digibios0", 5);
373	}
374
375	DLOG(DIGIDB_INIT, (sc->dev, "BIOS started after %d us\n", i));
376
377	for (i = 0; vW(ptr) != *(u_short *)"GD"; i++) {
378		if (i > 2*hz) {
379			log(LOG_ERR, "digi%d: BIOS boot failed "
380			    "(0x%02x != 0x%02x)\n",
381			    sc->res.unit, vW(ptr), *(u_short *)"GD");
382			return (EIO);
383		}
384		digi_delay(sc, "digibios1", 5);
385	}
386
387	DLOG(DIGIDB_INIT, (sc->dev, "BIOS booted after %d iterations\n", i));
388
389	if (sc->link.data != NULL) {
390		DLOG(DIGIDB_INIT, (sc->dev, "Loading link data\n"));
391		ptr = sc->setwin(sc, 0xcd0);
392		digi_bcopy(sc->link.data, ptr, 21);	/* XXX 21 ? */
393	}
394
395	/* load FEP/OS */
396
397	switch (sc->model) {
398	case PCXE:
399	case PCXEVE:
400	case PCXI:
401		ptr = sc->setwin(sc, sc->model == PCXI ? 0x2000 : 0x0);
402		digi_bcopy(sc->fep.data, ptr, sc->fep.size);
403
404		/* A BIOS request to move our data to 0x2000 */
405		ptr = sc->setwin(sc, MBOX);
406		vW(ptr + 0) = 2;
407		vW(ptr + 2) = sc->mem_seg + FEPCODESEG;
408		vW(ptr + 4) = 0;
409		vW(ptr + 6) = FEPCODESEG;
410		vW(ptr + 8) = 0;
411		vW(ptr + 10) = sc->fep.size;
412
413		/* Run the BIOS request */
414		outb(sc->port, FEPREQ | FEPMEM);
415		outb(sc->port, FEPCLR | FEPMEM);
416
417		for (i = 0; W(ptr); i++) {
418			if (i > hz) {
419				log(LOG_ERR, "digi%d: FEP/OS move failed\n",
420				    sc->res.unit);
421				sc->hidewin(sc);
422				return (EIO);
423			}
424			digi_delay(sc, "digifep0", 5);
425		}
426		DLOG(DIGIDB_INIT,
427		    (sc->dev, "FEP/OS moved after %d iterations\n", i));
428
429		/* Clear the confirm word */
430		ptr = sc->setwin(sc, FEPSTAT);
431		vW(ptr + 0) = 0;
432
433		/* A BIOS request to execute the FEP/OS */
434		ptr = sc->setwin(sc, MBOX);
435		vW(ptr + 0) = 0x01;
436		vW(ptr + 2) = FEPCODESEG;
437		vW(ptr + 4) = 0x04;
438
439		/* Run the BIOS request */
440		outb(sc->port, FEPREQ);
441		outb(sc->port, FEPCLR);
442
443		ptr = sc->setwin(sc, FEPSTAT);
444
445		break;
446
447	case PCXEM:
448	case PCIEPCX:
449	case PCIXR:
450		DLOG(DIGIDB_INIT, (sc->dev, "Loading FEP/OS\n"));
451
452		cnt = (sc->fep.size < sc->win_size - BIOSOFFSET) ?
453		    sc->fep.size : sc->win_size - BIOSOFFSET;
454
455		ptr = sc->setwin(sc, BIOSOFFSET);
456		digi_bcopy(sc->fep.data, ptr, cnt);
457
458		if (cnt != sc->fep.size) {
459			ptr = sc->setwin(sc, BIOSOFFSET + cnt);
460			digi_bcopy(sc->fep.data + cnt, ptr,
461			    sc->fep.size - cnt);
462		}
463
464		DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS loaded\n"));
465
466		ptr = sc->setwin(sc, 0xc30);
467		W(ptr + 4) = 0x1004;
468		W(ptr + 6) = 0xbfc0;
469		W(ptr + 0) = 0x03;
470		W(ptr + 2) = 0x00;
471
472		/* Clear the confirm word */
473		ptr = sc->setwin(sc, FEPSTAT);
474		W(ptr + 0) = 0;
475
476		if (sc->port)
477			outb(sc->port, 0);		/* XXX necessary ? */
478
479		break;
480
481	case PCCX:
482		ptr = sc->setwin(sc, 0xd000);
483		digi_bcopy(sc->fep.data, ptr, sc->fep.size);
484
485		/* A BIOS request to execute the FEP/OS */
486		ptr = sc->setwin(sc, 0xc40);
487		W(ptr + 0) = 1;
488		W(ptr + 2) = FEPCODE >> 4;
489		W(ptr + 4) = 4;
490
491		/* Clear the confirm word */
492		ptr = sc->setwin(sc, FEPSTAT);
493		W(ptr + 0) = 0;
494
495		/* Run the BIOS request */
496		outb(sc->port, FEPREQ | FEPMEM); /* send interrupt to BIOS */
497		outb(sc->port, FEPCLR | FEPMEM);
498		break;
499	}
500
501	/* Now wait 'till the FEP/OS has booted */
502	for (i = 0; vW(ptr) != *(u_short *)"OS"; i++) {
503		if (i > 2*hz) {
504			log(LOG_ERR, "digi%d: FEP/OS start failed "
505			    "(0x%02x != 0x%02x)\n",
506			    sc->res.unit, vW(ptr), *(u_short *)"OS");
507			sc->hidewin(sc);
508			return (EIO);
509		}
510		digi_delay(sc, "digifep1", 5);
511	}
512
513	DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS started after %d iterations\n", i));
514
515	if (sc->model >= PCXEM) {
516		ptr = sc->setwin(sc, 0xe04);
517		vW(ptr) = 2;
518		ptr = sc->setwin(sc, 0xc02);
519		sc->numports = vW(ptr);
520	} else {
521		ptr = sc->setwin(sc, 0xc22);
522		sc->numports = vW(ptr);
523	}
524
525	if (sc->numports == 0) {
526		device_printf(sc->dev, "%s, 0 ports found\n", sc->name);
527		sc->hidewin(sc);
528		return (0);
529	}
530
531	if (sc->numports > 256) {
532		/* Our minor numbering scheme is broken for more than 256 */
533		device_printf(sc->dev, "%s, 256 ports (%d ports found)\n",
534		    sc->name, sc->numports);
535		sc->numports = 256;
536	} else
537		device_printf(sc->dev, "%s, %d ports found\n", sc->name,
538		    sc->numports);
539
540	if (sc->ports)
541		free(sc->ports, M_TTYS);
542	sc->ports = malloc(sizeof(struct digi_p) * sc->numports,
543	    M_TTYS, M_WAITOK | M_ZERO);
544
545	if (sc->ttys)
546		free(sc->ttys, M_TTYS);
547	sc->ttys = malloc(sizeof(struct tty) * sc->numports,
548	    M_TTYS, M_WAITOK | M_ZERO);
549
550	/*
551	 * XXX Should read port 0xc90 for an array of 2byte values, 1 per
552	 * port.  If the value is 0, the port is broken....
553	 */
554
555	ptr = sc->setwin(sc, 0);
556
557	/* We should now init per-port structures */
558	bc = (volatile struct board_chan *)(ptr + CHANSTRUCT);
559	sc->gdata = (volatile struct global_data *)(ptr + FEP_GLOBAL);
560
561	sc->memcmd = ptr + sc->gdata->cstart;
562	sc->memevent = ptr + sc->gdata->istart;
563
564	for (i = 0; i < sc->numports; i++, bc++) {
565		port = sc->ports + i;
566		port->pnum = i;
567		port->sc = sc;
568		port->status = ENABLED;
569		port->tp = sc->ttys + i;
570		port->bc = bc;
571
572		if (sc->model == PCXEVE) {
573			port->txbuf = ptr +
574			    (((bc->tseg - sc->mem_seg) << 4) & 0x1fff);
575			port->rxbuf = ptr +
576			    (((bc->rseg - sc->mem_seg) << 4) & 0x1fff);
577			port->txwin = FEPWIN | ((bc->tseg - sc->mem_seg) >> 9);
578			port->rxwin = FEPWIN | ((bc->rseg - sc->mem_seg) >> 9);
579		} else if (sc->model == PCXI || sc->model == PCXE) {
580			port->txbuf = ptr + ((bc->tseg - sc->mem_seg) << 4);
581			port->rxbuf = ptr + ((bc->rseg - sc->mem_seg) << 4);
582			port->txwin = port->rxwin = 0;
583		} else {
584			port->txbuf = ptr +
585			    (((bc->tseg - sc->mem_seg) << 4) % sc->win_size);
586			port->rxbuf = ptr +
587			    (((bc->rseg - sc->mem_seg) << 4) % sc->win_size);
588			port->txwin = FEPWIN |
589			    (((bc->tseg - sc->mem_seg) << 4) / sc->win_size);
590			port->rxwin = FEPWIN |
591			    (((bc->rseg - sc->mem_seg) << 4) / sc->win_size);
592		}
593		port->txbufsize = bc->tmax + 1;
594		port->rxbufsize = bc->rmax + 1;
595
596		lowwater = port->txbufsize >> 2;
597		if (lowwater > 1024)
598			lowwater = 1024;
599		sc->setwin(sc, 0);
600		fepcmd_w(port, STXLWATER, lowwater, 10);
601		fepcmd_w(port, SRXLWATER, port->rxbufsize >> 2, 10);
602		fepcmd_w(port, SRXHWATER, (3 * port->rxbufsize) >> 2, 10);
603
604		bc->edelay = 100;
605		port->dtr_wait = 3 * hz;
606
607		/*
608		 * We don't use all the flags from <sys/ttydefaults.h> since
609		 * they are only relevant for logins.  It's important to have
610		 * echo off initially so that the line doesn't start blathering
611		 * before the echo flag can be turned off.
612		 */
613		port->it_in.c_iflag = 0;
614		port->it_in.c_oflag = 0;
615		port->it_in.c_cflag = TTYDEF_CFLAG;
616		port->it_in.c_lflag = 0;
617		termioschars(&port->it_in);
618		port->it_in.c_ispeed = port->it_in.c_ospeed = digidefaultrate;
619		port->it_out = port->it_in;
620		port->send_ring = 1;	/* Default action on signal RI */
621
622		port->dev[0] = make_dev(&digi_sw, (sc->res.unit << 16) + i,
623		    UID_ROOT, GID_WHEEL, 0600, "ttyD%d.%d", sc->res.unit, i);
624		port->dev[1] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
625		    CONTROL_INIT_STATE, UID_ROOT, GID_WHEEL,
626		    0600, "ttyiD%d.%d", sc->res.unit, i);
627		port->dev[2] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
628		    CONTROL_LOCK_STATE, UID_ROOT, GID_WHEEL,
629		    0600, "ttylD%d.%d", sc->res.unit, i);
630		port->dev[3] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
631		    CALLOUT_MASK, UID_UUCP, GID_DIALER,
632		    0660, "cuaD%d.%d", sc->res.unit, i);
633		port->dev[4] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
634		    CALLOUT_MASK | CONTROL_INIT_STATE, UID_UUCP, GID_DIALER,
635		    0660, "cuaiD%d.%d", sc->res.unit, i);
636		port->dev[5] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
637		    CALLOUT_MASK | CONTROL_LOCK_STATE, UID_UUCP, GID_DIALER,
638		    0660, "cualD%d.%d", sc->res.unit, i);
639	}
640
641	sc->hidewin(sc);
642	sc->inttest = timeout(digi_int_test, sc, hz);
643	/* fepcmd_w(&sc->ports[0], 0xff, 0, 0); */
644	sc->status = DIGI_STATUS_ENABLED;
645
646	return (0);
647}
648
649static int
650digimctl(struct digi_p *port, int bits, int how)
651{
652	int mstat;
653
654	if (how == DMGET) {
655		port->sc->setwin(port->sc, 0);
656		mstat = port->bc->mstat;
657		port->sc->hidewin(port->sc);
658		bits = TIOCM_LE;
659		if (mstat & port->sc->csigs->rts)
660			bits |= TIOCM_RTS;
661		if (mstat & port->cd)
662			bits |= TIOCM_CD;
663		if (mstat & port->dsr)
664			bits |= TIOCM_DSR;
665		if (mstat & port->sc->csigs->cts)
666			bits |= TIOCM_CTS;
667		if (mstat & port->sc->csigs->ri)
668			bits |= TIOCM_RI;
669		if (mstat & port->sc->csigs->dtr)
670			bits |= TIOCM_DTR;
671		return (bits);
672	}
673
674	/* Only DTR and RTS may be set */
675	mstat = 0;
676	if (bits & TIOCM_DTR)
677		mstat |= port->sc->csigs->dtr;
678	if (bits & TIOCM_RTS)
679		mstat |= port->sc->csigs->rts;
680
681	switch (how) {
682	case DMSET:
683		fepcmd_b(port, SETMODEM, mstat, ~mstat, 0);
684		break;
685	case DMBIS:
686		fepcmd_b(port, SETMODEM, mstat, 0, 0);
687		break;
688	case DMBIC:
689		fepcmd_b(port, SETMODEM, 0, mstat, 0);
690		break;
691	}
692
693	return (0);
694}
695
696static int
697digiopen(struct cdev *dev, int flag, int mode, struct thread *td)
698{
699	struct digi_softc *sc;
700	struct tty *tp;
701	int unit;
702	int pnum;
703	struct digi_p *port;
704	int s;
705	int error, mynor;
706	volatile struct board_chan *bc;
707
708	error = 0;
709	mynor = minor(dev);
710	unit = MINOR_TO_UNIT(minor(dev));
711	pnum = MINOR_TO_PORT(minor(dev));
712
713	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
714	if (!sc)
715		return (ENXIO);
716
717	if (sc->status != DIGI_STATUS_ENABLED) {
718		DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
719		return (ENXIO);
720	}
721	if (pnum >= sc->numports) {
722		DLOG(DIGIDB_OPEN, (sc->dev, "port%d: Doesn't exist\n", pnum));
723		return (ENXIO);
724	}
725	if (mynor & (CTRL_DEV | CONTROL_MASK)) {
726		sc->opencnt++;
727		return (0);
728	}
729	port = &sc->ports[pnum];
730	tp = dev->si_tty = port->tp;
731	bc = port->bc;
732
733	s = spltty();
734
735open_top:
736	while (port->status & DIGI_DTR_OFF) {
737		port->wopeners++;
738		error = tsleep(&port->dtr_wait, TTIPRI | PCATCH, "digidtr", 0);
739		port->wopeners--;
740		if (error)
741			goto out;
742	}
743
744	if (tp->t_state & TS_ISOPEN) {
745		/*
746		 * The device is open, so everything has been initialized.
747		 * Handle conflicts.
748		 */
749		if (mynor & CALLOUT_MASK) {
750			if (!port->active_out) {
751				error = EBUSY;
752				DLOG(DIGIDB_OPEN, (sc->dev, "port %d:"
753				    " BUSY error = %d\n", pnum, error));
754				goto out;
755			}
756		} else if (port->active_out) {
757			if (flag & O_NONBLOCK) {
758				error = EBUSY;
759				DLOG(DIGIDB_OPEN, (sc->dev,
760				    "port %d: BUSY error = %d\n", pnum, error));
761				goto out;
762			}
763			port->wopeners++;
764			error = tsleep(&port->active_out, TTIPRI | PCATCH,
765			    "digibi", 0);
766			port->wopeners--;
767			if (error != 0) {
768				DLOG(DIGIDB_OPEN, (sc->dev,
769				    "port %d: tsleep(digibi) error = %d\n",
770				    pnum, error));
771				goto out;
772			}
773			goto open_top;
774		}
775		if (tp->t_state & TS_XCLUDE && suser(td) != 0) {
776			error = EBUSY;
777			goto out;
778		}
779	} else {
780		/*
781		 * The device isn't open, so there are no conflicts.
782		 * Initialize it.  Initialization is done twice in many
783		 * cases: to preempt sleeping callin opens if we are callout,
784		 * and to complete a callin open after DCD rises.
785		 */
786		tp->t_oproc = digistart;
787		tp->t_param = digiparam;
788		tp->t_stop = digistop;
789		tp->t_dev = dev;
790		tp->t_termios = (mynor & CALLOUT_MASK) ?
791		    port->it_out : port->it_in;
792		sc->setwin(sc, 0);
793
794		bc->rout = bc->rin;	/* clear input queue */
795		bc->idata = 1;
796		bc->iempty = 1;
797		bc->ilow = 1;
798		bc->mint = port->cd | port->sc->csigs->ri;
799		bc->tin = bc->tout;
800		if (port->ialtpin) {
801			port->cd = sc->csigs->dsr;
802			port->dsr = sc->csigs->cd;
803		} else {
804			port->cd = sc->csigs->cd;
805			port->dsr = sc->csigs->dsr;
806		}
807		port->wopeners++;			/* XXX required ? */
808		error = digiparam(tp, &tp->t_termios);
809		port->wopeners--;
810
811		if (error != 0) {
812			DLOG(DIGIDB_OPEN, (sc->dev,
813			    "port %d: cxpparam error = %d\n", pnum, error));
814			goto out;
815		}
816		ttsetwater(tp);
817
818		/* handle fake and initial DCD for callout devices */
819
820		if (bc->mstat & port->cd || mynor & CALLOUT_MASK)
821			ttyld_modem(tp, 1);
822	}
823
824	/* Wait for DCD if necessary */
825	if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) &&
826	    !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
827		port->wopeners++;
828		error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "digidcd", 0);
829		port->wopeners--;
830		if (error != 0) {
831			DLOG(DIGIDB_OPEN, (sc->dev,
832			    "port %d: tsleep(digidcd) error = %d\n",
833			    pnum, error));
834			goto out;
835		}
836		goto open_top;
837	}
838	error = ttyld_open(tp, dev);
839	DLOG(DIGIDB_OPEN, (sc->dev, "port %d: l_open error = %d\n",
840	    pnum, error));
841
842	ttyldoptim(tp);
843
844	if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
845		port->active_out = TRUE;
846
847	if (tp->t_state & TS_ISOPEN)
848		sc->opencnt++;
849out:
850	splx(s);
851
852	if (!(tp->t_state & TS_ISOPEN))
853		digihardclose(port);
854
855	DLOG(DIGIDB_OPEN, (sc->dev, "port %d: open() returns %d\n",
856	    pnum, error));
857
858	return (error);
859}
860
861static int
862digiclose(struct cdev *dev, int flag, int mode, struct thread *td)
863{
864	int mynor;
865	struct tty *tp;
866	int unit, pnum;
867	struct digi_softc *sc;
868	struct digi_p *port;
869	int s;
870
871	mynor = minor(dev);
872	unit = MINOR_TO_UNIT(mynor);
873	pnum = MINOR_TO_PORT(mynor);
874
875	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
876	KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
877
878	if (mynor & (CTRL_DEV | CONTROL_MASK)) {
879		sc->opencnt--;
880		return (0);
881	}
882
883	port = sc->ports + pnum;
884	tp = port->tp;
885
886	DLOG(DIGIDB_CLOSE, (sc->dev, "port %d: closing\n", pnum));
887
888	s = spltty();
889	ttyld_close(tp, flag);
890	ttyldoptim(tp);
891	digihardclose(port);
892	ttyclose(tp);
893	if (--sc->opencnt == 0)
894		splx(s);
895	return (0);
896}
897
898static void
899digidtrwakeup(void *chan)
900{
901	struct digi_p *port = chan;
902
903	port->status &= ~DIGI_DTR_OFF;
904	wakeup(&port->dtr_wait);
905	port->wopeners--;
906}
907
908static void
909digihardclose(struct digi_p *port)
910{
911	volatile struct board_chan *bc;
912	int s;
913
914	bc = port->bc;
915
916	s = spltty();
917	port->sc->setwin(port->sc, 0);
918	bc->idata = 0;
919	bc->iempty = 0;
920	bc->ilow = 0;
921	bc->mint = 0;
922	if ((port->tp->t_cflag & HUPCL) ||
923	    (!port->active_out && !(bc->mstat & port->cd) &&
924	    !(port->it_in.c_cflag & CLOCAL)) ||
925	    !(port->tp->t_state & TS_ISOPEN)) {
926		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
927		if (port->dtr_wait != 0) {
928			/* Schedule a wakeup of any callin devices */
929			port->wopeners++;
930			timeout(&digidtrwakeup, port, port->dtr_wait);
931			port->status |= DIGI_DTR_OFF;
932		}
933	}
934	port->active_out = FALSE;
935	wakeup(&port->active_out);
936	wakeup(TSA_CARR_ON(port->tp));
937	splx(s);
938}
939
940static int
941digiread(struct cdev *dev, struct uio *uio, int flag)
942{
943	int mynor;
944	struct tty *tp;
945	int error, unit, pnum;
946	struct digi_softc *sc;
947
948	mynor = minor(dev);
949	if (mynor & CONTROL_MASK)
950		return (ENODEV);
951
952	unit = MINOR_TO_UNIT(mynor);
953	pnum = MINOR_TO_PORT(mynor);
954
955	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
956	KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
957	tp = &sc->ttys[pnum];
958
959	error = ttyld_read(tp, uio, flag);
960	DLOG(DIGIDB_READ, (sc->dev, "port %d: read() returns %d\n",
961	    pnum, error));
962
963	return (error);
964}
965
966static int
967digiwrite(struct cdev *dev, struct uio *uio, int flag)
968{
969	int mynor;
970	struct tty *tp;
971	int error, unit, pnum;
972	struct digi_softc *sc;
973
974	mynor = minor(dev);
975	if (mynor & CONTROL_MASK)
976		return (ENODEV);
977
978	unit = MINOR_TO_UNIT(mynor);
979	pnum = MINOR_TO_PORT(mynor);
980
981	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
982	KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
983	tp = &sc->ttys[pnum];
984
985	error = ttyld_write(tp, uio, flag);
986	DLOG(DIGIDB_WRITE, (sc->dev, "port %d: write() returns %d\n",
987	    pnum, error));
988
989	return (error);
990}
991
992/*
993 * Load module "digi_<mod>.ko" and look for a symbol called digi_mod_<mod>.
994 *
995 * Populate sc->bios, sc->fep, and sc->link from this data.
996 *
997 * sc->fep.data, sc->bios.data and sc->link.data are malloc()d according
998 * to their respective sizes.
999 *
1000 * The module is unloaded when we're done.
1001 */
1002static int
1003digi_loadmoduledata(struct digi_softc *sc)
1004{
1005	struct digi_mod *digi_mod;
1006	linker_file_t lf;
1007	char *modfile, *sym;
1008	caddr_t symptr;
1009	int modlen, res;
1010
1011	KASSERT(sc->bios.data == NULL, ("Uninitialised BIOS variable"));
1012	KASSERT(sc->fep.data == NULL, ("Uninitialised FEP variable"));
1013	KASSERT(sc->link.data == NULL, ("Uninitialised LINK variable"));
1014	KASSERT(sc->module != NULL, ("Uninitialised module name"));
1015
1016	modlen = strlen(sc->module);
1017	modfile = malloc(modlen + 6, M_TEMP, M_WAITOK);
1018	snprintf(modfile, modlen + 6, "digi_%s", sc->module);
1019	if ((res = linker_reference_module(modfile, NULL, &lf)) != 0) {
1020		if (res == ENOENT && rootdev == NULL)
1021			printf("%s: Failed to autoload module: No filesystem\n",
1022			    modfile);
1023		else
1024			printf("%s: Failed %d to autoload module\n", modfile,
1025			    res);
1026	}
1027	free(modfile, M_TEMP);
1028	if (res != 0)
1029		return (res);
1030
1031	sym = malloc(modlen + 10, M_TEMP, M_WAITOK);
1032	snprintf(sym, modlen + 10, "digi_mod_%s", sc->module);
1033	if ((symptr = linker_file_lookup_symbol(lf, sym, 0)) == NULL)
1034		printf("digi_%s.ko: Symbol `%s' not found\n", sc->module, sym);
1035	free(sym, M_TEMP);
1036
1037	digi_mod = (struct digi_mod *)symptr;
1038	if (digi_mod->dm_version != DIGI_MOD_VERSION) {
1039		printf("digi_%s.ko: Invalid version %d (need %d)\n",
1040		    sc->module, digi_mod->dm_version, DIGI_MOD_VERSION);
1041		linker_file_unload(lf);
1042		return (EINVAL);
1043	}
1044
1045	sc->bios.size = digi_mod->dm_bios.size;
1046	if (sc->bios.size != 0 && digi_mod->dm_bios.data != NULL) {
1047		sc->bios.data = malloc(sc->bios.size, M_TTYS, M_WAITOK);
1048		bcopy(digi_mod->dm_bios.data, sc->bios.data, sc->bios.size);
1049	}
1050
1051	sc->fep.size = digi_mod->dm_fep.size;
1052	if (sc->fep.size != 0 && digi_mod->dm_fep.data != NULL) {
1053		sc->fep.data = malloc(sc->fep.size, M_TTYS, M_WAITOK);
1054		bcopy(digi_mod->dm_fep.data, sc->fep.data, sc->fep.size);
1055	}
1056
1057	sc->link.size = digi_mod->dm_link.size;
1058	if (sc->link.size != 0 && digi_mod->dm_link.data != NULL) {
1059		sc->link.data = malloc(sc->link.size, M_TTYS, M_WAITOK);
1060		bcopy(digi_mod->dm_link.data, sc->link.data, sc->link.size);
1061	}
1062
1063	linker_file_unload(lf);
1064
1065	return (0);
1066}
1067
1068static int
1069digiioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1070{
1071	int unit, pnum, mynor, error, s;
1072	struct digi_softc *sc;
1073	struct digi_p *port;
1074	struct tty *tp;
1075#ifndef BURN_BRIDGES
1076#if defined(COMPAT_43)
1077	int oldcmd;
1078	struct termios term;
1079#endif
1080#endif
1081
1082	mynor = minor(dev);
1083	unit = MINOR_TO_UNIT(mynor);
1084	pnum = MINOR_TO_PORT(mynor);
1085
1086	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1087	KASSERT(sc, ("digi%d: softc not allocated in digiioctl\n", unit));
1088
1089	if (sc->status == DIGI_STATUS_DISABLED)
1090		return (ENXIO);
1091
1092	if (mynor & CTRL_DEV) {
1093		switch (cmd) {
1094		case DIGIIO_DEBUG:
1095#ifdef DEBUG
1096			digi_debug = *(int *)data;
1097			return (0);
1098#else
1099			device_printf(sc->dev, "DEBUG not defined\n");
1100			return (ENXIO);
1101#endif
1102		case DIGIIO_REINIT:
1103			digi_loadmoduledata(sc);
1104			error = digi_init(sc);
1105			digi_freemoduledata(sc);
1106			return (error);
1107
1108		case DIGIIO_MODEL:
1109			*(enum digi_model *)data = sc->model;
1110			return (0);
1111
1112		case DIGIIO_IDENT:
1113			return (copyout(sc->name, *(char **)data,
1114			    strlen(sc->name) + 1));
1115		}
1116	}
1117
1118	if (pnum >= sc->numports)
1119		return (ENXIO);
1120
1121	port = sc->ports + pnum;
1122	if (!(port->status & ENABLED))
1123		return (ENXIO);
1124
1125	tp = port->tp;
1126
1127	if (mynor & CONTROL_MASK) {
1128		struct termios *ct;
1129
1130		switch (mynor & CONTROL_MASK) {
1131		case CONTROL_INIT_STATE:
1132			ct = (mynor & CALLOUT_MASK) ?
1133			    &port->it_out : &port->it_in;
1134			break;
1135		case CONTROL_LOCK_STATE:
1136			ct = (mynor & CALLOUT_MASK) ?
1137			    &port->lt_out : &port->lt_in;
1138			break;
1139		default:
1140			return (ENODEV);	/* /dev/nodev */
1141		}
1142
1143		switch (cmd) {
1144		case TIOCSETA:
1145			error = suser(td);
1146			if (error != 0)
1147				return (error);
1148			*ct = *(struct termios *)data;
1149			return (0);
1150
1151		case TIOCGETA:
1152			*(struct termios *)data = *ct;
1153			return (0);
1154
1155		case TIOCGETD:
1156			*(int *)data = TTYDISC;
1157			return (0);
1158
1159		case TIOCGWINSZ:
1160			bzero(data, sizeof(struct winsize));
1161			return (0);
1162
1163		case DIGIIO_GETALTPIN:
1164			switch (mynor & CONTROL_MASK) {
1165			case CONTROL_INIT_STATE:
1166				*(int *)data = port->ialtpin;
1167				break;
1168
1169			case CONTROL_LOCK_STATE:
1170				*(int *)data = port->laltpin;
1171				break;
1172
1173			default:
1174				panic("Confusion when re-testing minor");
1175				return (ENODEV);
1176			}
1177			return (0);
1178
1179		case DIGIIO_SETALTPIN:
1180			switch (mynor & CONTROL_MASK) {
1181			case CONTROL_INIT_STATE:
1182				if (!port->laltpin) {
1183					port->ialtpin = !!*(int *)data;
1184					DLOG(DIGIDB_SET, (sc->dev,
1185					    "port%d: initial ALTPIN %s\n", pnum,
1186					    port->ialtpin ? "set" : "cleared"));
1187				}
1188				break;
1189
1190			case CONTROL_LOCK_STATE:
1191				port->laltpin = !!*(int *)data;
1192				DLOG(DIGIDB_SET, (sc->dev,
1193				    "port%d: ALTPIN %slocked\n",
1194				    pnum, port->laltpin ? "" : "un"));
1195				break;
1196
1197			default:
1198				panic("Confusion when re-testing minor");
1199				return (ENODEV);
1200			}
1201			return (0);
1202
1203		default:
1204			return (ENOTTY);
1205		}
1206	}
1207
1208	switch (cmd) {
1209	case DIGIIO_GETALTPIN:
1210		*(int *)data = !!(port->dsr == sc->csigs->cd);
1211		return (0);
1212
1213	case DIGIIO_SETALTPIN:
1214		if (!port->laltpin) {
1215			if (*(int *)data) {
1216				DLOG(DIGIDB_SET, (sc->dev,
1217				    "port%d: ALTPIN set\n", pnum));
1218				port->cd = sc->csigs->dsr;
1219				port->dsr = sc->csigs->cd;
1220			} else {
1221				DLOG(DIGIDB_SET, (sc->dev,
1222				    "port%d: ALTPIN cleared\n", pnum));
1223				port->cd = sc->csigs->cd;
1224				port->dsr = sc->csigs->dsr;
1225			}
1226		}
1227		return (0);
1228	}
1229
1230	tp = port->tp;
1231#ifndef BURN_BRIDGES
1232#if defined(COMPAT_43)
1233	term = tp->t_termios;
1234	oldcmd = cmd;
1235	error = ttsetcompat(tp, &cmd, data, &term);
1236	if (error != 0)
1237		return (error);
1238	if (cmd != oldcmd)
1239		data = (caddr_t) & term;
1240#endif
1241#endif
1242	if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1243		int cc;
1244		struct termios *dt;
1245		struct termios *lt;
1246
1247		dt = (struct termios *)data;
1248		lt = (mynor & CALLOUT_MASK) ? &port->lt_out : &port->lt_in;
1249
1250		dt->c_iflag =
1251		    (tp->t_iflag & lt->c_iflag) | (dt->c_iflag & ~lt->c_iflag);
1252		dt->c_oflag =
1253		    (tp->t_oflag & lt->c_oflag) | (dt->c_oflag & ~lt->c_oflag);
1254		dt->c_cflag =
1255		    (tp->t_cflag & lt->c_cflag) | (dt->c_cflag & ~lt->c_cflag);
1256		dt->c_lflag =
1257		    (tp->t_lflag & lt->c_lflag) | (dt->c_lflag & ~lt->c_lflag);
1258		port->c_iflag = dt->c_iflag & (IXOFF | IXON | IXANY);
1259		dt->c_iflag &= ~(IXOFF | IXON | IXANY);
1260		for (cc = 0; cc < NCCS; ++cc)
1261			if (lt->c_cc[cc] != 0)
1262				dt->c_cc[cc] = tp->t_cc[cc];
1263		if (lt->c_ispeed != 0)
1264			dt->c_ispeed = tp->t_ispeed;
1265		if (lt->c_ospeed != 0)
1266			dt->c_ospeed = tp->t_ospeed;
1267	}
1268	error = ttyioctl(dev, cmd, data, flag, td);
1269	if (error == 0 && cmd == TIOCGETA)
1270		((struct termios *)data)->c_iflag |= port->c_iflag;
1271	ttyldoptim(tp);
1272	if (error >= 0 && error != ENOTTY)
1273		return (error);
1274	s = spltty();
1275	sc->setwin(sc, 0);
1276	switch (cmd) {
1277	case DIGIIO_RING:
1278		port->send_ring = *(u_char *)data;
1279		break;
1280	case TIOCSBRK:
1281		/*
1282		 * now it sends 400 millisecond break because I don't know
1283		 * how to send an infinite break
1284		 */
1285		fepcmd_w(port, SENDBREAK, 400, 10);
1286		break;
1287	case TIOCCBRK:
1288		/* now it's empty */
1289		break;
1290	case TIOCSDTR:
1291		digimctl(port, TIOCM_DTR, DMBIS);
1292		break;
1293	case TIOCCDTR:
1294		digimctl(port, TIOCM_DTR, DMBIC);
1295		break;
1296	case TIOCMSET:
1297		digimctl(port, *(int *)data, DMSET);
1298		break;
1299	case TIOCMBIS:
1300		digimctl(port, *(int *)data, DMBIS);
1301		break;
1302	case TIOCMBIC:
1303		digimctl(port, *(int *)data, DMBIC);
1304		break;
1305	case TIOCMGET:
1306		*(int *)data = digimctl(port, 0, DMGET);
1307		break;
1308	case TIOCMSDTRWAIT:
1309		error = suser(td);
1310		if (error != 0) {
1311			splx(s);
1312			return (error);
1313		}
1314		port->dtr_wait = *(int *)data *hz / 100;
1315
1316		break;
1317	case TIOCMGDTRWAIT:
1318		*(int *)data = port->dtr_wait * 100 / hz;
1319		break;
1320#ifdef DIGI_INTERRUPT
1321	case TIOCTIMESTAMP:
1322		*(struct timeval *)data = sc->intr_timestamp;
1323
1324		break;
1325#endif
1326	default:
1327		splx(s);
1328		return (ENOTTY);
1329	}
1330	splx(s);
1331	return (0);
1332}
1333
1334static int
1335digiparam(struct tty *tp, struct termios *t)
1336{
1337	int mynor;
1338	int unit;
1339	int pnum;
1340	struct digi_softc *sc;
1341	struct digi_p *port;
1342	int cflag;
1343	int iflag;
1344	int hflow;
1345	int s;
1346	int window;
1347
1348	mynor = minor(tp->t_dev);
1349	unit = MINOR_TO_UNIT(mynor);
1350	pnum = MINOR_TO_PORT(mynor);
1351
1352	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1353	KASSERT(sc, ("digi%d: softc not allocated in digiparam\n", unit));
1354
1355	port = &sc->ports[pnum];
1356
1357	DLOG(DIGIDB_SET, (sc->dev, "port%d: setting parameters\n", pnum));
1358
1359	if (t->c_ispeed == 0)
1360		t->c_ispeed = t->c_ospeed;
1361
1362	cflag = ttspeedtab(t->c_ospeed, digispeedtab);
1363
1364	if (cflag < 0 || (cflag > 0 && t->c_ispeed != t->c_ospeed))
1365		return (EINVAL);
1366
1367	s = splclock();
1368
1369	window = sc->window;
1370	sc->setwin(sc, 0);
1371
1372	if (cflag == 0) {				/* hangup */
1373		DLOG(DIGIDB_SET, (sc->dev, "port%d: hangup\n", pnum));
1374		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
1375	} else {
1376		digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIS);
1377
1378		DLOG(DIGIDB_SET, (sc->dev, "port%d: CBAUD = %d\n", pnum,
1379		    cflag));
1380
1381#if 0
1382		/* convert flags to sysV-style values */
1383		if (t->c_cflag & PARODD)
1384			cflag |= 0x0200;
1385		if (t->c_cflag & PARENB)
1386			cflag |= 0x0100;
1387		if (t->c_cflag & CSTOPB)
1388			cflag |= 0x0080;
1389#else
1390		/* convert flags to sysV-style values */
1391		if (t->c_cflag & PARODD)
1392			cflag |= FEP_PARODD;
1393		if (t->c_cflag & PARENB)
1394			cflag |= FEP_PARENB;
1395		if (t->c_cflag & CSTOPB)
1396			cflag |= FEP_CSTOPB;
1397		if (t->c_cflag & CLOCAL)
1398			cflag |= FEP_CLOCAL;
1399#endif
1400
1401		cflag |= (t->c_cflag & CSIZE) >> 4;
1402		DLOG(DIGIDB_SET, (sc->dev, "port%d: CFLAG = 0x%x\n", pnum,
1403		    cflag));
1404		fepcmd_w(port, SETCFLAGS, (unsigned)cflag, 0);
1405	}
1406
1407	iflag =
1408	    t->c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP);
1409	if (port->c_iflag & IXON)
1410		iflag |= 0x400;
1411	if (port->c_iflag & IXANY)
1412		iflag |= 0x800;
1413	if (port->c_iflag & IXOFF)
1414		iflag |= 0x1000;
1415
1416	DLOG(DIGIDB_SET, (sc->dev, "port%d: set iflag = 0x%x\n", pnum, iflag));
1417	fepcmd_w(port, SETIFLAGS, (unsigned)iflag, 0);
1418
1419	hflow = 0;
1420	if (t->c_cflag & CDTR_IFLOW)
1421		hflow |= sc->csigs->dtr;
1422	if (t->c_cflag & CRTS_IFLOW)
1423		hflow |= sc->csigs->rts;
1424	if (t->c_cflag & CCTS_OFLOW)
1425		hflow |= sc->csigs->cts;
1426	if (t->c_cflag & CDSR_OFLOW)
1427		hflow |= port->dsr;
1428	if (t->c_cflag & CCAR_OFLOW)
1429		hflow |= port->cd;
1430
1431	DLOG(DIGIDB_SET, (sc->dev, "port%d: set hflow = 0x%x\n", pnum, hflow));
1432	fepcmd_w(port, SETHFLOW, 0xff00 | (unsigned)hflow, 0);
1433
1434	DLOG(DIGIDB_SET, (sc->dev, "port%d: set startc(0x%x), stopc(0x%x)\n",
1435	    pnum, t->c_cc[VSTART], t->c_cc[VSTOP]));
1436	fepcmd_b(port, SONOFFC, t->c_cc[VSTART], t->c_cc[VSTOP], 0);
1437
1438	if (sc->window != 0)
1439		sc->towin(sc, 0);
1440	if (window != 0)
1441		sc->towin(sc, window);
1442	splx(s);
1443
1444	return (0);
1445}
1446
1447static void
1448digi_intr(void *vp)
1449{
1450	struct digi_p *port;
1451	char *cxcon;
1452	struct digi_softc *sc;
1453	int ehead, etail;
1454	volatile struct board_chan *bc;
1455	struct tty *tp;
1456	int head, tail;
1457	int wrapmask;
1458	int size, window;
1459	struct event {
1460		u_char pnum;
1461		u_char event;
1462		u_char mstat;
1463		u_char lstat;
1464	} event;
1465
1466	sc = vp;
1467
1468	if (sc->status != DIGI_STATUS_ENABLED) {
1469		DLOG(DIGIDB_IRQ, (sc->dev, "interrupt on disabled board !\n"));
1470		return;
1471	}
1472
1473#ifdef DIGI_INTERRUPT
1474	microtime(&sc->intr_timestamp);
1475#endif
1476
1477	window = sc->window;
1478	sc->setwin(sc, 0);
1479
1480	if (sc->model >= PCXEM && W(sc->vmem + 0xd00)) {
1481		struct con_bios *con = con_bios_list;
1482		register u_char *ptr;
1483
1484		ptr = sc->vmem + W(sc->vmem + 0xd00);
1485		while (con) {
1486			if (ptr[1] && W(ptr + 2) == W(con->bios + 2))
1487				/* Not first block -- exact match */
1488				break;
1489
1490			if (W(ptr + 4) >= W(con->bios + 4) &&
1491			    W(ptr + 4) <= W(con->bios + 6))
1492				/* Initial search concetrator BIOS */
1493				break;
1494		}
1495
1496		if (con == NULL) {
1497			log(LOG_ERR, "digi%d: wanted bios LREV = 0x%04x"
1498			    " not found!\n", sc->res.unit, W(ptr + 4));
1499			W(ptr + 10) = 0;
1500			W(sc->vmem + 0xd00) = 0;
1501			goto eoi;
1502		}
1503		cxcon = con->bios;
1504		W(ptr + 4) = W(cxcon + 4);
1505		W(ptr + 6) = W(cxcon + 6);
1506		if (ptr[1] == 0)
1507			W(ptr + 2) = W(cxcon + 2);
1508		W(ptr + 8) = (ptr[1] << 6) + W(cxcon + 8);
1509		size = W(cxcon + 10) - (ptr[1] << 10);
1510		if (size <= 0) {
1511			W(ptr + 8) = W(cxcon + 8);
1512			W(ptr + 10) = 0;
1513		} else {
1514			if (size > 1024)
1515				size = 1024;
1516			W(ptr + 10) = size;
1517			bcopy(cxcon + (ptr[1] << 10), ptr + 12, size);
1518		}
1519		W(sc->vmem + 0xd00) = 0;
1520		goto eoi;
1521	}
1522
1523	ehead = sc->gdata->ein;
1524	etail = sc->gdata->eout;
1525	if (ehead == etail) {
1526#ifdef DEBUG
1527		sc->intr_count++;
1528		if (sc->intr_count % 6000 == 0) {
1529			DLOG(DIGIDB_IRQ, (sc->dev,
1530			    "6000 useless polls %x %x\n", ehead, etail));
1531			sc->intr_count = 0;
1532		}
1533#endif
1534		goto eoi;
1535	}
1536	while (ehead != etail) {
1537		event = *(volatile struct event *)(sc->memevent + etail);
1538
1539		etail = (etail + 4) & sc->gdata->imax;
1540
1541		if (event.pnum >= sc->numports) {
1542			log(LOG_ERR, "digi%d: port %d: got event"
1543			    " on nonexisting port\n", sc->res.unit,
1544			    event.pnum);
1545			continue;
1546		}
1547		port = &sc->ports[event.pnum];
1548		bc = port->bc;
1549		tp = port->tp;
1550
1551		if (!(tp->t_state & TS_ISOPEN) && !port->wopeners) {
1552			DLOG(DIGIDB_IRQ, (sc->dev,
1553			    "port %d: event 0x%x on closed port\n",
1554			    event.pnum, event.event));
1555			bc->rout = bc->rin;
1556			bc->idata = 0;
1557			bc->iempty = 0;
1558			bc->ilow = 0;
1559			bc->mint = 0;
1560			continue;
1561		}
1562		if (event.event & ~ALL_IND)
1563			log(LOG_ERR, "digi%d: port%d: ? event 0x%x mstat 0x%x"
1564			    " lstat 0x%x\n", sc->res.unit, event.pnum,
1565			    event.event, event.mstat, event.lstat);
1566
1567		if (event.event & DATA_IND) {
1568			DLOG(DIGIDB_IRQ, (sc->dev, "port %d: DATA_IND\n",
1569			    event.pnum));
1570			wrapmask = port->rxbufsize - 1;
1571			head = bc->rin;
1572			tail = bc->rout;
1573
1574			size = 0;
1575			if (!(tp->t_state & TS_ISOPEN)) {
1576				bc->rout = head;
1577				goto end_of_data;
1578			}
1579			while (head != tail) {
1580				int top;
1581
1582				DLOG(DIGIDB_INT, (sc->dev,
1583				    "port %d: p rx head = %d tail = %d\n",
1584				    event.pnum, head, tail));
1585				top = (head > tail) ? head : wrapmask + 1;
1586				sc->towin(sc, port->rxwin);
1587				size = top - tail;
1588				if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1589					size = b_to_q((char *)port->rxbuf +
1590					    tail, size, &tp->t_rawq);
1591					tail = top - size;
1592					ttwakeup(tp);
1593				} else for (; tail < top;) {
1594					ttyld_rint(tp, port->rxbuf[tail]);
1595					sc->towin(sc, port->rxwin);
1596					size--;
1597					tail++;
1598					if (tp->t_state & TS_TBLOCK)
1599						break;
1600				}
1601				tail &= wrapmask;
1602				sc->setwin(sc, 0);
1603				bc->rout = tail;
1604				head = bc->rin;
1605				if (size)
1606					break;
1607			}
1608
1609			if (bc->orun) {
1610				CE_RECORD(port, CE_OVERRUN);
1611				log(LOG_ERR, "digi%d: port%d: %s\n",
1612				    sc->res.unit, event.pnum,
1613				    digi_errortxt(CE_OVERRUN));
1614				bc->orun = 0;
1615			}
1616end_of_data:
1617			if (size) {
1618				tp->t_state |= TS_TBLOCK;
1619				port->status |= PAUSE_RX;
1620				DLOG(DIGIDB_RX, (sc->dev, "port %d: pause RX\n",
1621				    event.pnum));
1622			} else {
1623				bc->idata = 1;
1624			}
1625		}
1626
1627		if (event.event & MODEMCHG_IND) {
1628			DLOG(DIGIDB_MODEM, (sc->dev, "port %d: MODEMCHG_IND\n",
1629			    event.pnum));
1630
1631			if ((event.mstat ^ event.lstat) & port->cd) {
1632				sc->hidewin(sc);
1633				ttyld_modem(tp, event.mstat & port->cd);
1634				sc->setwin(sc, 0);
1635				wakeup(TSA_CARR_ON(tp));
1636			}
1637
1638			if (event.mstat & sc->csigs->ri) {
1639				DLOG(DIGIDB_RI, (sc->dev, "port %d: RING\n",
1640				    event.pnum));
1641				if (port->send_ring) {
1642					ttyld_rint(tp, 'R');
1643					ttyld_rint(tp, 'I');
1644					ttyld_rint(tp, 'N');
1645					ttyld_rint(tp, 'G');
1646					ttyld_rint(tp, '\r');
1647					ttyld_rint(tp, '\n');
1648				}
1649			}
1650		}
1651		if (event.event & BREAK_IND) {
1652			DLOG(DIGIDB_MODEM, (sc->dev, "port %d: BREAK_IND\n",
1653			    event.pnum));
1654			ttyld_rint(tp, TTY_BI);
1655		}
1656		if (event.event & (LOWTX_IND | EMPTYTX_IND)) {
1657			DLOG(DIGIDB_IRQ, (sc->dev, "port %d:%s%s\n",
1658			    event.pnum,
1659			    event.event & LOWTX_IND ? " LOWTX" : "",
1660			    event.event & EMPTYTX_IND ?  " EMPTYTX" : ""));
1661			ttyld_start(tp);
1662		}
1663	}
1664	sc->gdata->eout = etail;
1665eoi:
1666	if (sc->window != 0)
1667		sc->towin(sc, 0);
1668	if (window != 0)
1669		sc->towin(sc, window);
1670}
1671
1672static void
1673digistart(struct tty *tp)
1674{
1675	int unit;
1676	int pnum;
1677	struct digi_p *port;
1678	struct digi_softc *sc;
1679	volatile struct board_chan *bc;
1680	int head, tail;
1681	int size, ocount, totcnt = 0;
1682	int s;
1683	int wmask;
1684
1685	unit = MINOR_TO_UNIT(minor(tp->t_dev));
1686	pnum = MINOR_TO_PORT(minor(tp->t_dev));
1687
1688	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1689	KASSERT(sc, ("digi%d: softc not allocated in digistart\n", unit));
1690
1691	port = &sc->ports[pnum];
1692	bc = port->bc;
1693
1694	wmask = port->txbufsize - 1;
1695
1696	s = spltty();
1697	port->lcc = tp->t_outq.c_cc;
1698	sc->setwin(sc, 0);
1699	if (!(tp->t_state & TS_TBLOCK)) {
1700		if (port->status & PAUSE_RX) {
1701			DLOG(DIGIDB_RX, (sc->dev, "port %d: resume RX\n",
1702			    pnum));
1703			/*
1704			 * CAREFUL - braces are needed here if the DLOG is
1705			 * optimised out!
1706			 */
1707		}
1708		port->status &= ~PAUSE_RX;
1709		bc->idata = 1;
1710	}
1711	if (!(tp->t_state & TS_TTSTOP) && port->status & PAUSE_TX) {
1712		DLOG(DIGIDB_TX, (sc->dev, "port %d: resume TX\n", pnum));
1713		port->status &= ~PAUSE_TX;
1714		fepcmd_w(port, RESUMETX, 0, 10);
1715	}
1716	if (tp->t_outq.c_cc == 0)
1717		tp->t_state &= ~TS_BUSY;
1718	else
1719		tp->t_state |= TS_BUSY;
1720
1721	head = bc->tin;
1722	while (tp->t_outq.c_cc != 0) {
1723		tail = bc->tout;
1724		DLOG(DIGIDB_INT, (sc->dev, "port%d: s tx head = %d tail = %d\n",
1725		    pnum, head, tail));
1726
1727		if (head < tail)
1728			size = tail - head - 1;
1729		else {
1730			size = port->txbufsize - head;
1731			if (tail == 0)
1732				size--;
1733		}
1734
1735		if (size == 0)
1736			break;
1737		sc->towin(sc, port->txwin);
1738		ocount = q_to_b(&tp->t_outq, port->txbuf + head, size);
1739		totcnt += ocount;
1740		head += ocount;
1741		head &= wmask;
1742		sc->setwin(sc, 0);
1743		bc->tin = head;
1744		bc->iempty = 1;
1745		bc->ilow = 1;
1746	}
1747	port->lostcc = tp->t_outq.c_cc;
1748	tail = bc->tout;
1749	if (head < tail)
1750		size = port->txbufsize - tail + head;
1751	else
1752		size = head - tail;
1753
1754	port->lbuf = size;
1755	DLOG(DIGIDB_INT, (sc->dev, "port%d: s total cnt = %d\n", pnum, totcnt));
1756	ttwwakeup(tp);
1757	splx(s);
1758}
1759
1760static void
1761digistop(struct tty *tp, int rw)
1762{
1763	struct digi_softc *sc;
1764	int unit;
1765	int pnum;
1766	struct digi_p *port;
1767
1768	unit = MINOR_TO_UNIT(minor(tp->t_dev));
1769	pnum = MINOR_TO_PORT(minor(tp->t_dev));
1770
1771	sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1772	KASSERT(sc, ("digi%d: softc not allocated in digistop\n", unit));
1773	port = sc->ports + pnum;
1774
1775	DLOG(DIGIDB_TX, (sc->dev, "port %d: pause TX\n", pnum));
1776	port->status |= PAUSE_TX;
1777	fepcmd_w(port, PAUSETX, 0, 10);
1778}
1779
1780static void
1781fepcmd(struct digi_p *port, int cmd, int op1, int ncmds)
1782{
1783	u_char *mem;
1784	unsigned tail, head;
1785	int count, n;
1786
1787	mem = port->sc->memcmd;
1788
1789	port->sc->setwin(port->sc, 0);
1790
1791	head = port->sc->gdata->cin;
1792	mem[head + 0] = cmd;
1793	mem[head + 1] = port->pnum;
1794	*(u_short *)(mem + head + 2) = op1;
1795
1796	head = (head + 4) & port->sc->gdata->cmax;
1797	port->sc->gdata->cin = head;
1798
1799	for (count = FEPTIMEOUT; count > 0; count--) {
1800		head = port->sc->gdata->cin;
1801		tail = port->sc->gdata->cout;
1802		n = (head - tail) & port->sc->gdata->cmax;
1803
1804		if (n <= ncmds * sizeof(short) * 4)
1805			break;
1806	}
1807	if (count == 0)
1808		log(LOG_ERR, "digi%d: port%d: timeout on FEP command\n",
1809		    port->sc->res.unit, port->pnum);
1810}
1811
1812const char *
1813digi_errortxt(int id)
1814{
1815	static const char *error_desc[] = {
1816		"silo overflow",
1817		"interrupt-level buffer overflow",
1818		"tty-level buffer overflow",
1819	};
1820
1821	KASSERT(id >= 0 && id < sizeof(error_desc) / sizeof(error_desc[0]),
1822	    ("Unexpected digi error id %d\n", id));
1823
1824	return (error_desc[id]);
1825}
1826
1827int
1828digi_attach(struct digi_softc *sc)
1829{
1830	sc->res.ctldev = make_dev(&digi_sw,
1831	    (sc->res.unit << 16) | CTRL_DEV, UID_ROOT, GID_WHEEL,
1832	    0600, "digi%r.ctl", sc->res.unit);
1833
1834	digi_loadmoduledata(sc);
1835	digi_init(sc);
1836	digi_freemoduledata(sc);
1837
1838	return (0);
1839}
1840
1841static int
1842digi_inuse(struct digi_softc *sc)
1843{
1844	int i;
1845
1846	for (i = 0; i < sc->numports; i++)
1847		if (sc->ttys[i].t_state & TS_ISOPEN) {
1848			DLOG(DIGIDB_INIT, (sc->dev, "port%d: busy\n", i));
1849			return (1);
1850		} else if (sc->ports[i].wopeners || sc->ports[i].opencnt) {
1851			DLOG(DIGIDB_INIT, (sc->dev, "port%d: blocked in open\n",
1852			    i));
1853			return (1);
1854		}
1855	return (0);
1856}
1857
1858static void
1859digi_free_state(struct digi_softc *sc)
1860{
1861	int d, i;
1862
1863	/* Blow it all away */
1864
1865	for (i = 0; i < sc->numports; i++)
1866		for (d = 0; d < 6; d++)
1867			destroy_dev(sc->ports[i].dev[d]);
1868
1869	untimeout(digi_poll, sc, sc->callout);
1870	callout_handle_init(&sc->callout);
1871	untimeout(digi_int_test, sc, sc->inttest);
1872	callout_handle_init(&sc->inttest);
1873
1874	bus_teardown_intr(sc->dev, sc->res.irq, sc->res.irqHandler);
1875#ifdef DIGI_INTERRUPT
1876	if (sc->res.irq != NULL) {
1877		bus_release_resource(dev, SYS_RES_IRQ, sc->res.irqrid,
1878		    sc->res.irq);
1879		sc->res.irq = NULL;
1880	}
1881#endif
1882	if (sc->numports) {
1883		KASSERT(sc->ports, ("digi%d: Lost my ports ?", sc->res.unit));
1884		KASSERT(sc->ttys, ("digi%d: Lost my ttys ?", sc->res.unit));
1885		free(sc->ports, M_TTYS);
1886		sc->ports = NULL;
1887		free(sc->ttys, M_TTYS);
1888		sc->ttys = NULL;
1889		sc->numports = 0;
1890	}
1891
1892	sc->status = DIGI_STATUS_NOTINIT;
1893}
1894
1895int
1896digi_detach(device_t dev)
1897{
1898	struct digi_softc *sc = device_get_softc(dev);
1899
1900	DLOG(DIGIDB_INIT, (sc->dev, "detaching\n"));
1901
1902	/* If we're INIT'd, numports must be 0 */
1903	KASSERT(sc->numports == 0 || sc->status != DIGI_STATUS_NOTINIT,
1904	    ("digi%d: numports(%d) & status(%d) are out of sync",
1905	    sc->res.unit, sc->numports, (int)sc->status));
1906
1907	if (digi_inuse(sc))
1908		return (EBUSY);
1909
1910	digi_free_state(sc);
1911
1912	destroy_dev(sc->res.ctldev);
1913
1914	if (sc->res.mem != NULL) {
1915		bus_release_resource(dev, SYS_RES_MEMORY, sc->res.mrid,
1916		    sc->res.mem);
1917		sc->res.mem = NULL;
1918	}
1919	if (sc->res.io != NULL) {
1920		bus_release_resource(dev, SYS_RES_IOPORT, sc->res.iorid,
1921		    sc->res.io);
1922		sc->res.io = NULL;
1923	}
1924
1925	return (0);
1926}
1927
1928int
1929digi_shutdown(device_t dev)
1930{
1931	return (0);
1932}
1933
1934MODULE_VERSION(digi, 1);
1935