Do a major clean-up of the BUSDMA architecture. A large number of
[dfdiff.git] / sys / dev / serial / digi / digi.c
blobc6a8637a5967e9bafa211e01851d7d47dca34131
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.
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.
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.
29 * $FreeBSD: src/sys/dev/digi/digi.c,v 1.36 2003/09/26 09:05:57 phk Exp $
30 * $DragonFly: src/sys/dev/serial/digi/digi.c,v 1.9 2006/10/25 20:56:01 dillon Exp $
33 /*-
34 * TODO:
35 * Figure out what the con bios stuff is supposed to do
36 * Test with *LOTS* more cards - I only have a PCI8r and an ISA Xem.
39 #include "opt_compat.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/conf.h>
45 #include <sys/linker.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/malloc.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 <sys/thread2.h>
56 #include <dev/serial/digi/digiio.h>
57 #include <dev/serial/digi/digireg.h>
58 #include <dev/serial/digi/digi.h>
59 #include <dev/serial/digi/digi_pci.h>
60 #include <dev/serial/digi/digi_bios.h>
62 #define CDEV_MAJOR 162
64 #define CTRL_DEV 0x800000
65 #define CALLOUT_MASK 0x400000
66 #define CONTROL_INIT_STATE 0x100000
67 #define CONTROL_LOCK_STATE 0x200000
68 #define CONTROL_MASK (CTRL_DEV|CONTROL_INIT_STATE|CONTROL_LOCK_STATE)
69 #define UNIT_MASK 0x030000
70 #define PORT_MASK 0x0000FF
71 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
72 #define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK)
73 #define MINOR_TO_UNIT(mynor) (((mynor) & UNIT_MASK)>>16)
74 #define MINOR_TO_PORT(mynor) ((mynor) & PORT_MASK)
76 static d_open_t digiopen;
77 static d_close_t digiclose;
78 static d_read_t digiread;
79 static d_write_t digiwrite;
80 static d_ioctl_t digiioctl;
82 static void digistop(struct tty *tp, int rw);
83 static int digimctl(struct digi_p *port, int bits, int how);
84 static void digi_poll(void *ptr);
85 static void digi_freedata(struct digi_softc *);
86 static void fepcmd(struct digi_p *port, int cmd, int op, int ncmds);
87 static void digistart(struct tty *tp);
88 static int digiparam(struct tty *tp, struct termios *t);
89 static void digihardclose(struct digi_p *port);
90 static void digi_intr(void *);
91 static int digi_init(struct digi_softc *_sc);
92 static int digi_loaddata(struct digi_softc *);
93 static int digi_inuse(struct digi_softc *);
94 static void digi_free_state(struct digi_softc *);
96 #define fepcmd_b(port, cmd, op1, op2, ncmds) \
97 fepcmd(port, cmd, (op2 << 8) | op1, ncmds)
98 #define fepcmd_w fepcmd
101 static speed_t digidefaultrate = TTYDEF_SPEED;
103 struct con_bios {
104 struct con_bios *next;
105 u_char *bios;
106 size_t size;
109 static struct con_bios *con_bios_list;
110 devclass_t digi_devclass;
111 unsigned digi_debug = 0;
113 static 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}
137 const struct digi_control_signals digi_xixe_signals = {
138 0x02, 0x08, 0x10, 0x20, 0x40, 0x80
141 const struct digi_control_signals digi_normal_signals = {
142 0x02, 0x80, 0x20, 0x10, 0x40, 0x01
145 static struct dev_ops digi_ops = {
146 { "dgm", CDEV_MAJOR, D_TTY },
147 .d_open = digiopen,
148 .d_close = digiclose,
149 .d_read = digiread,
150 .d_write = digiwrite,
151 .d_ioctl = digiioctl,
152 .d_poll = ttypoll,
153 .d_kqfilter = ttykqfilter
156 static void
157 digi_poll(void *ptr)
159 struct digi_softc *sc;
161 sc = (struct digi_softc *)ptr;
162 callout_init(&sc->callout);
163 digi_intr(sc);
164 callout_reset(&sc->callout, (hz >= 200) ? hz / 100 : 1, digi_poll, sc);
167 static void
168 digi_int_test(void *v)
170 struct digi_softc *sc = v;
172 callout_init(&sc->inttest);
173 #ifdef DIGI_INTERRUPT
174 if (sc->intr_timestamp.tv_sec || sc->intr_timestamp.tv_usec) {
175 /* interrupt OK! */
176 return;
178 log(LOG_ERR, "digi%d: Interrupt didn't work, use polled mode\n", unit);
179 #endif
180 callout_reset(&sc->callout, (hz >= 200) ? hz / 100 : 1, digi_poll, sc);
183 static void
184 digi_freedata(struct digi_softc *sc)
186 if (sc->fep.data != NULL) {
187 kfree(sc->fep.data, M_TTYS);
188 sc->fep.data = NULL;
190 if (sc->link.data != NULL) {
191 kfree(sc->link.data, M_TTYS);
192 sc->link.data = NULL;
194 if (sc->bios.data != NULL) {
195 kfree(sc->bios.data, M_TTYS);
196 sc->bios.data = NULL;
200 static int
201 digi_bcopy(const void *vfrom, void *vto, size_t sz)
203 volatile const char *from = (volatile const char *)vfrom;
204 volatile char *to = (volatile char *)vto;
205 size_t i;
207 for (i = 0; i < sz; i++)
208 *to++ = *from++;
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);
218 void
219 digi_delay(struct digi_softc *sc, const char *txt, u_long timo)
221 if (cold)
222 DELAY(timo * 1000000 / hz);
223 else
224 tsleep(sc, PCATCH, txt, timo);
227 static int
228 digi_init(struct digi_softc *sc)
230 int i, cnt, resp;
231 u_char *ptr;
232 int lowwater;
233 struct digi_p *port;
234 volatile struct board_chan *bc;
236 ptr = NULL;
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);
243 if (sc->bios.data == NULL) {
244 log(LOG_ERR, "digi%d: Cannot init without BIOS\n",
245 sc->res.unit);
246 return (EIO);
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);
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);
260 sc->status = DIGI_STATUS_NOTINIT;
262 if (sc->numports) {
264 * We're re-initialising - maybe because someone's attached
265 * another port module. For now, we just re-initialise
266 * everything.
268 if (digi_inuse(sc))
269 return (EBUSY);
271 digi_free_state(sc);
274 ptr = sc->setwin(sc, MISCGLOBAL);
275 for (i = 0; i < 16; i += 2)
276 vW(ptr + i) = 0;
278 switch (sc->model) {
279 case PCXEVE:
280 outb(sc->wport, 0xff); /* window 7 */
281 ptr = sc->vmem + (BIOSCODE & 0x1fff);
283 if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
284 device_printf(sc->dev, "BIOS upload failed\n");
285 return (EIO);
288 outb(sc->port, FEPCLR);
289 break;
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);
299 break;
301 case PCXEM:
302 case PCIEPCX:
303 case PCIXR:
304 if (sc->pcibus)
305 PCIPORT = FEPRST;
306 else
307 outb(sc->port, FEPRST | FEPMEM);
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);
316 digi_delay(sc, "digiinit0", 5);
318 DLOG(DIGIDB_INIT, (sc->dev, "Got init reset after %d us\n", i));
320 /* Now upload the BIOS */
321 cnt = (sc->bios.size < sc->win_size - BIOSOFFSET) ?
322 sc->bios.size : sc->win_size - BIOSOFFSET;
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);
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);
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;
346 break;
349 DLOG(DIGIDB_INIT, (sc->dev, "BIOS uploaded\n"));
351 ptr = sc->setwin(sc, MISCGLOBAL);
352 W(ptr) = 0;
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;
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);
372 digi_delay(sc, "digibios0", 5);
375 DLOG(DIGIDB_INIT, (sc->dev, "BIOS started after %d us\n", i));
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);
384 digi_delay(sc, "digibios1", 5);
387 DLOG(DIGIDB_INIT, (sc->dev, "BIOS booted after %d iterations\n", i));
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 ? */
395 /* load FEP/OS */
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);
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;
413 /* Run the BIOS request */
414 outb(sc->port, FEPREQ | FEPMEM);
415 outb(sc->port, FEPCLR | FEPMEM);
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);
424 digi_delay(sc, "digifep0", 5);
426 DLOG(DIGIDB_INIT,
427 (sc->dev, "FEP/OS moved after %d iterations\n", i));
429 /* Clear the confirm word */
430 ptr = sc->setwin(sc, FEPSTAT);
431 vW(ptr + 0) = 0;
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;
439 /* Run the BIOS request */
440 outb(sc->port, FEPREQ);
441 outb(sc->port, FEPCLR);
443 ptr = sc->setwin(sc, FEPSTAT);
445 break;
447 case PCXEM:
448 case PCIEPCX:
449 case PCIXR:
450 DLOG(DIGIDB_INIT, (sc->dev, "Loading FEP/OS\n"));
452 cnt = (sc->fep.size < sc->win_size - BIOSOFFSET) ?
453 sc->fep.size : sc->win_size - BIOSOFFSET;
455 ptr = sc->setwin(sc, BIOSOFFSET);
456 digi_bcopy(sc->fep.data, ptr, cnt);
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);
464 DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS loaded\n"));
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;
472 /* Clear the confirm word */
473 ptr = sc->setwin(sc, FEPSTAT);
474 W(ptr + 0) = 0;
476 if (sc->port)
477 outb(sc->port, 0); /* XXX necessary ? */
479 break;
481 case PCCX:
482 ptr = sc->setwin(sc, 0xd000);
483 digi_bcopy(sc->fep.data, ptr, sc->fep.size);
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;
491 /* Clear the confirm word */
492 ptr = sc->setwin(sc, FEPSTAT);
493 W(ptr + 0) = 0;
495 /* Run the BIOS request */
496 outb(sc->port, FEPREQ | FEPMEM); /* send interrupt to BIOS */
497 outb(sc->port, FEPCLR | FEPMEM);
498 break;
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);
510 digi_delay(sc, "digifep1", 5);
513 DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS started after %d iterations\n", i));
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);
525 if (sc->numports == 0) {
526 device_printf(sc->dev, "%s, 0 ports found\n", sc->name);
527 sc->hidewin(sc);
528 return (0);
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);
540 if (sc->ports)
541 kfree(sc->ports, M_TTYS);
542 sc->ports = kmalloc(sizeof(struct digi_p) * sc->numports,
543 M_TTYS, M_WAITOK | M_ZERO);
545 if (sc->ttys)
546 kfree(sc->ttys, M_TTYS);
547 sc->ttys = kmalloc(sizeof(struct tty) * sc->numports,
548 M_TTYS, M_WAITOK | M_ZERO);
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....
555 ptr = sc->setwin(sc, 0);
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);
561 sc->memcmd = ptr + sc->gdata->cstart;
562 sc->memevent = ptr + sc->gdata->istart;
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;
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);
593 port->txbufsize = bc->tmax + 1;
594 port->rxbufsize = bc->rmax + 1;
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);
604 bc->edelay = 100;
605 port->dtr_wait = 3 * hz;
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.
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 */
622 port->dev[0] = make_dev(&digi_ops, (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_ops, ((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_ops, ((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_ops, ((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_ops, ((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_ops, ((sc->res.unit << 16) + i) |
637 CALLOUT_MASK | CONTROL_LOCK_STATE, UID_UUCP, GID_DIALER,
638 0660, "cualD%d.%d", sc->res.unit, i);
641 sc->hidewin(sc);
642 callout_reset(&sc->inttest, hz, digi_int_test, sc);
643 /* fepcmd_w(&sc->ports[0], 0xff, 0, 0); */
644 sc->status = DIGI_STATUS_ENABLED;
646 return (0);
649 static int
650 digimctl(struct digi_p *port, int bits, int how)
652 int mstat;
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);
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;
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;
693 return (0);
696 static void
697 digi_disc_optim(struct tty *tp, struct termios *t, struct digi_p *port)
699 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP)) &&
700 (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) &&
701 (!(t->c_iflag & PARMRK) ||
702 (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) &&
703 !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) &&
704 linesw[tp->t_line].l_rint == ttyinput)
705 tp->t_state |= TS_CAN_BYPASS_L_RINT;
706 else
707 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
710 static int
711 digiopen(struct dev_open_args *ap)
713 cdev_t dev = ap->a_head.a_dev;
714 struct digi_softc *sc;
715 struct tty *tp;
716 int unit;
717 int pnum;
718 struct digi_p *port;
719 int error, mynor;
720 volatile struct board_chan *bc;
722 error = 0;
723 mynor = minor(dev);
724 unit = MINOR_TO_UNIT(minor(dev));
725 pnum = MINOR_TO_PORT(minor(dev));
727 sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
728 if (!sc)
729 return (ENXIO);
731 if (sc->status != DIGI_STATUS_ENABLED) {
732 DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
733 return (ENXIO);
735 if (pnum >= sc->numports) {
736 DLOG(DIGIDB_OPEN, (sc->dev, "port%d: Doesn't exist\n", pnum));
737 return (ENXIO);
739 if (mynor & (CTRL_DEV | CONTROL_MASK)) {
740 sc->opencnt++;
741 return (0);
743 port = &sc->ports[pnum];
744 tp = dev->si_tty = port->tp;
745 bc = port->bc;
747 crit_enter();
749 open_top:
750 while (port->status & DIGI_DTR_OFF) {
751 port->wopeners++;
752 error = tsleep(&port->dtr_wait, PCATCH, "digidtr", 0);
753 port->wopeners--;
754 if (error)
755 goto out;
758 if (tp->t_state & TS_ISOPEN) {
760 * The device is open, so everything has been initialized.
761 * Handle conflicts.
763 if (mynor & CALLOUT_MASK) {
764 if (!port->active_out) {
765 error = EBUSY;
766 DLOG(DIGIDB_OPEN, (sc->dev, "port %d:"
767 " BUSY error = %d\n", pnum, error));
768 goto out;
770 } else if (port->active_out) {
771 if (ap->a_oflags & O_NONBLOCK) {
772 error = EBUSY;
773 DLOG(DIGIDB_OPEN, (sc->dev,
774 "port %d: BUSY error = %d\n", pnum, error));
775 goto out;
777 port->wopeners++;
778 error = tsleep(&port->active_out, PCATCH, "digibi", 0);
779 port->wopeners--;
780 if (error != 0) {
781 DLOG(DIGIDB_OPEN, (sc->dev,
782 "port %d: tsleep(digibi) error = %d\n",
783 pnum, error));
784 goto out;
786 goto open_top;
788 if (tp->t_state & TS_XCLUDE && suser_cred(ap->a_cred, 0) != 0) {
789 error = EBUSY;
790 goto out;
792 } else {
794 * The device isn't open, so there are no conflicts.
795 * Initialize it. Initialization is done twice in many
796 * cases: to preempt sleeping callin opens if we are callout,
797 * and to complete a callin open after DCD rises.
799 callout_init(&port->wakeupco);
800 tp->t_oproc = digistart;
801 tp->t_param = digiparam;
802 tp->t_stop = digistop;
803 tp->t_dev = dev;
804 tp->t_termios = (mynor & CALLOUT_MASK) ?
805 port->it_out : port->it_in;
806 sc->setwin(sc, 0);
808 bc->rout = bc->rin; /* clear input queue */
809 bc->idata = 1;
810 bc->iempty = 1;
811 bc->ilow = 1;
812 bc->mint = port->cd | port->sc->csigs->ri;
813 bc->tin = bc->tout;
814 if (port->ialtpin) {
815 port->cd = sc->csigs->dsr;
816 port->dsr = sc->csigs->cd;
817 } else {
818 port->cd = sc->csigs->cd;
819 port->dsr = sc->csigs->dsr;
821 port->wopeners++; /* XXX required ? */
822 error = digiparam(tp, &tp->t_termios);
823 port->wopeners--;
825 if (error != 0) {
826 DLOG(DIGIDB_OPEN, (sc->dev,
827 "port %d: cxpparam error = %d\n", pnum, error));
828 goto out;
830 ttsetwater(tp);
832 /* handle fake and initial DCD for callout devices */
834 if (bc->mstat & port->cd || mynor & CALLOUT_MASK)
835 linesw[tp->t_line].l_modem(tp, 1);
838 /* Wait for DCD if necessary */
839 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) &&
840 !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) {
841 port->wopeners++;
842 error = tsleep(TSA_CARR_ON(tp), PCATCH, "digidcd", 0);
843 port->wopeners--;
844 if (error != 0) {
845 DLOG(DIGIDB_OPEN, (sc->dev,
846 "port %d: tsleep(digidcd) error = %d\n",
847 pnum, error));
848 goto out;
850 goto open_top;
852 error = linesw[tp->t_line].l_open(dev, tp);
853 DLOG(DIGIDB_OPEN, (sc->dev, "port %d: l_open error = %d\n",
854 pnum, error));
856 digi_disc_optim(tp, &tp->t_termios, port);
858 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
859 port->active_out = TRUE;
861 if (tp->t_state & TS_ISOPEN)
862 sc->opencnt++;
863 out:
864 crit_exit();
866 if (!(tp->t_state & TS_ISOPEN))
867 digihardclose(port);
869 DLOG(DIGIDB_OPEN, (sc->dev, "port %d: open() returns %d\n",
870 pnum, error));
872 return (error);
875 static int
876 digiclose(struct dev_close_args *ap)
878 cdev_t dev = ap->a_head.a_dev;
879 int mynor;
880 struct tty *tp;
881 int unit, pnum;
882 struct digi_softc *sc;
883 struct digi_p *port;
885 mynor = minor(dev);
886 unit = MINOR_TO_UNIT(mynor);
887 pnum = MINOR_TO_PORT(mynor);
889 sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
890 KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
892 if (mynor & (CTRL_DEV | CONTROL_MASK)) {
893 sc->opencnt--;
894 return (0);
897 port = sc->ports + pnum;
898 tp = port->tp;
900 DLOG(DIGIDB_CLOSE, (sc->dev, "port %d: closing\n", pnum));
902 crit_enter();
903 linesw[tp->t_line].l_close(tp, ap->a_fflag);
904 digi_disc_optim(tp, &tp->t_termios, port);
905 digistop(tp, FREAD | FWRITE);
906 digihardclose(port);
907 ttyclose(tp);
908 --sc->opencnt;
909 crit_exit();
910 return (0);
913 static void
914 digidtrwakeup(void *chan)
916 struct digi_p *port = chan;
918 port->status &= ~DIGI_DTR_OFF;
919 wakeup(&port->dtr_wait);
920 port->wopeners--;
923 static void
924 digihardclose(struct digi_p *port)
926 volatile struct board_chan *bc;
928 bc = port->bc;
930 crit_enter();
931 port->sc->setwin(port->sc, 0);
932 bc->idata = 0;
933 bc->iempty = 0;
934 bc->ilow = 0;
935 bc->mint = 0;
936 if ((port->tp->t_cflag & HUPCL) ||
937 (!port->active_out && !(bc->mstat & port->cd) &&
938 !(port->it_in.c_cflag & CLOCAL)) ||
939 !(port->tp->t_state & TS_ISOPEN)) {
940 digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
941 if (port->dtr_wait != 0) {
942 /* Schedule a wakeup of any callin devices */
943 port->wopeners++;
944 callout_reset(&port->wakeupco, port->dtr_wait,
945 digidtrwakeup, port);
946 port->status |= DIGI_DTR_OFF;
949 port->active_out = FALSE;
950 wakeup(&port->active_out);
951 wakeup(TSA_CARR_ON(port->tp));
952 crit_exit();
955 static int
956 digiread(struct dev_read_args *ap)
958 cdev_t dev = ap->a_head.a_dev;
959 int mynor;
960 struct tty *tp;
961 int error, unit, pnum;
962 struct digi_softc *sc;
964 mynor = minor(dev);
965 if (mynor & CONTROL_MASK)
966 return (ENODEV);
968 unit = MINOR_TO_UNIT(mynor);
969 pnum = MINOR_TO_PORT(mynor);
971 sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
972 KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
973 tp = &sc->ttys[pnum];
975 error = linesw[tp->t_line].l_read(tp, ap->a_uio, ap->a_ioflag);
976 DLOG(DIGIDB_READ, (sc->dev, "port %d: read() returns %d\n",
977 pnum, error));
979 return (error);
982 static int
983 digiwrite(struct dev_write_args *ap)
985 cdev_t dev = ap->a_head.a_dev;
986 int mynor;
987 struct tty *tp;
988 int error, unit, pnum;
989 struct digi_softc *sc;
991 mynor = minor(dev);
992 if (mynor & CONTROL_MASK)
993 return (ENODEV);
995 unit = MINOR_TO_UNIT(mynor);
996 pnum = MINOR_TO_PORT(mynor);
998 sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
999 KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
1000 tp = &sc->ttys[pnum];
1002 error = linesw[tp->t_line].l_write(tp, ap->a_uio, ap->a_ioflag);
1003 DLOG(DIGIDB_WRITE, (sc->dev, "port %d: write() returns %d\n",
1004 pnum, error));
1006 return (error);
1010 * Load module "digi_<mod>.ko" and look for a symbol called digi_mod_<mod>.
1012 * Populate sc->bios, sc->fep, and sc->link from this data.
1014 * sc->fep.data, sc->bios.data and sc->link.data are malloc()d according
1015 * to their respective sizes.
1017 * The module is unloaded when we're done.
1019 static int
1020 digi_loaddata(struct digi_softc *sc)
1022 struct digi_bios *bios;
1024 KASSERT(sc->bios.data == NULL, ("Uninitialised BIOS variable"));
1025 KASSERT(sc->fep.data == NULL, ("Uninitialised FEP variable"));
1026 KASSERT(sc->link.data == NULL, ("Uninitialised LINK variable"));
1027 KASSERT(sc->module != NULL, ("Uninitialised module name"));
1029 for (bios = digi_bioses; bios->model != NULL; bios++) {
1030 if (!strcmp(bios->model, sc->module))
1031 break;
1033 if (bios->model == NULL) {
1034 printf("digi.ko: driver %s not found", sc->module);
1035 return(EINVAL);
1038 sc->bios.size = bios->bios_size;
1039 if (sc->bios.size != 0 && bios->bios != NULL) {
1040 sc->bios.data = kmalloc(sc->bios.size, M_TTYS, M_WAITOK);
1041 bcopy(bios->bios, sc->bios.data, sc->bios.size);
1044 sc->fep.size = bios->fep_size;
1045 if (sc->fep.size != 0 && bios->fep != NULL) {
1046 sc->fep.data = kmalloc(sc->fep.size, M_TTYS, M_WAITOK);
1047 bcopy(bios->fep, sc->fep.data, sc->fep.size);
1050 return (0);
1053 static int
1054 digiioctl(struct dev_ioctl_args *ap)
1056 cdev_t dev = ap->a_head.a_dev;
1057 u_long cmd = ap->a_cmd;
1058 caddr_t data = ap->a_data;
1059 int unit, pnum, mynor, error;
1060 struct digi_softc *sc;
1061 struct digi_p *port;
1062 struct tty *tp;
1063 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1064 int oldcmd;
1065 struct termios term;
1066 #endif
1068 mynor = minor(dev);
1069 unit = MINOR_TO_UNIT(mynor);
1070 pnum = MINOR_TO_PORT(mynor);
1072 sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1073 KASSERT(sc, ("digi%d: softc not allocated in digiioctl\n", unit));
1075 if (sc->status == DIGI_STATUS_DISABLED)
1076 return (ENXIO);
1078 if (mynor & CTRL_DEV) {
1079 switch (cmd) {
1080 case DIGIIO_DEBUG:
1081 #ifdef DEBUG
1082 digi_debug = *(int *)data;
1083 return (0);
1084 #else
1085 device_printf(sc->dev, "DEBUG not defined\n");
1086 return (ENXIO);
1087 #endif
1088 case DIGIIO_REINIT:
1089 digi_loaddata(sc);
1090 error = digi_init(sc);
1091 digi_freedata(sc);
1092 return (error);
1094 case DIGIIO_MODEL:
1095 *(enum digi_model *)data = sc->model;
1096 return (0);
1098 case DIGIIO_IDENT:
1099 return (copyout(sc->name, *(char **)data,
1100 strlen(sc->name) + 1));
1104 if (pnum >= sc->numports)
1105 return (ENXIO);
1107 port = sc->ports + pnum;
1108 if (!(port->status & ENABLED))
1109 return (ENXIO);
1111 tp = port->tp;
1113 if (mynor & CONTROL_MASK) {
1114 struct termios *ct;
1116 switch (mynor & CONTROL_MASK) {
1117 case CONTROL_INIT_STATE:
1118 ct = (mynor & CALLOUT_MASK) ?
1119 &port->it_out : &port->it_in;
1120 break;
1121 case CONTROL_LOCK_STATE:
1122 ct = (mynor & CALLOUT_MASK) ?
1123 &port->lt_out : &port->lt_in;
1124 break;
1125 default:
1126 return (ENODEV); /* /dev/nodev */
1129 switch (cmd) {
1130 case TIOCSETA:
1131 error = suser_cred(ap->a_cred, 0);
1132 if (error != 0)
1133 return (error);
1134 *ct = *(struct termios *)data;
1135 return (0);
1137 case TIOCGETA:
1138 *(struct termios *)data = *ct;
1139 return (0);
1141 case TIOCGETD:
1142 *(int *)data = TTYDISC;
1143 return (0);
1145 case TIOCGWINSZ:
1146 bzero(data, sizeof(struct winsize));
1147 return (0);
1149 case DIGIIO_GETALTPIN:
1150 switch (mynor & CONTROL_MASK) {
1151 case CONTROL_INIT_STATE:
1152 *(int *)data = port->ialtpin;
1153 break;
1155 case CONTROL_LOCK_STATE:
1156 *(int *)data = port->laltpin;
1157 break;
1159 default:
1160 panic("Confusion when re-testing minor");
1161 return (ENODEV);
1163 return (0);
1165 case DIGIIO_SETALTPIN:
1166 switch (mynor & CONTROL_MASK) {
1167 case CONTROL_INIT_STATE:
1168 if (!port->laltpin) {
1169 port->ialtpin = !!*(int *)data;
1170 DLOG(DIGIDB_SET, (sc->dev,
1171 "port%d: initial ALTPIN %s\n", pnum,
1172 port->ialtpin ? "set" : "cleared"));
1174 break;
1176 case CONTROL_LOCK_STATE:
1177 port->laltpin = !!*(int *)data;
1178 DLOG(DIGIDB_SET, (sc->dev,
1179 "port%d: ALTPIN %slocked\n",
1180 pnum, port->laltpin ? "" : "un"));
1181 break;
1183 default:
1184 panic("Confusion when re-testing minor");
1185 return (ENODEV);
1187 return (0);
1189 default:
1190 return (ENOTTY);
1194 switch (cmd) {
1195 case DIGIIO_GETALTPIN:
1196 *(int *)data = !!(port->dsr == sc->csigs->cd);
1197 return (0);
1199 case DIGIIO_SETALTPIN:
1200 if (!port->laltpin) {
1201 if (*(int *)data) {
1202 DLOG(DIGIDB_SET, (sc->dev,
1203 "port%d: ALTPIN set\n", pnum));
1204 port->cd = sc->csigs->dsr;
1205 port->dsr = sc->csigs->cd;
1206 } else {
1207 DLOG(DIGIDB_SET, (sc->dev,
1208 "port%d: ALTPIN cleared\n", pnum));
1209 port->cd = sc->csigs->cd;
1210 port->dsr = sc->csigs->dsr;
1213 return (0);
1216 tp = port->tp;
1217 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1218 term = tp->t_termios;
1219 oldcmd = cmd;
1220 error = ttsetcompat(tp, &cmd, data, &term);
1221 if (error != 0)
1222 return (error);
1223 if (cmd != oldcmd)
1224 data = (caddr_t) & term;
1225 #endif
1226 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1227 int cc;
1228 struct termios *dt;
1229 struct termios *lt;
1231 dt = (struct termios *)data;
1232 lt = (mynor & CALLOUT_MASK) ? &port->lt_out : &port->lt_in;
1234 dt->c_iflag =
1235 (tp->t_iflag & lt->c_iflag) | (dt->c_iflag & ~lt->c_iflag);
1236 dt->c_oflag =
1237 (tp->t_oflag & lt->c_oflag) | (dt->c_oflag & ~lt->c_oflag);
1238 dt->c_cflag =
1239 (tp->t_cflag & lt->c_cflag) | (dt->c_cflag & ~lt->c_cflag);
1240 dt->c_lflag =
1241 (tp->t_lflag & lt->c_lflag) | (dt->c_lflag & ~lt->c_lflag);
1242 port->c_iflag = dt->c_iflag & (IXOFF | IXON | IXANY);
1243 dt->c_iflag &= ~(IXOFF | IXON | IXANY);
1244 for (cc = 0; cc < NCCS; ++cc)
1245 if (lt->c_cc[cc] != 0)
1246 dt->c_cc[cc] = tp->t_cc[cc];
1247 if (lt->c_ispeed != 0)
1248 dt->c_ispeed = tp->t_ispeed;
1249 if (lt->c_ospeed != 0)
1250 dt->c_ospeed = tp->t_ospeed;
1252 error = linesw[tp->t_line].l_ioctl(tp, cmd, data,
1253 ap->a_fflag, ap->a_cred);
1254 if (error == 0 && cmd == TIOCGETA)
1255 ((struct termios *)data)->c_iflag |= port->c_iflag;
1257 if (error >= 0 && error != ENOIOCTL)
1258 return (error);
1259 crit_enter();
1260 error = ttioctl(tp, cmd, data, ap->a_fflag);
1261 if (error == 0 && cmd == TIOCGETA)
1262 ((struct termios *)data)->c_iflag |= port->c_iflag;
1264 digi_disc_optim(tp, &tp->t_termios, port);
1265 if (error >= 0 && error != ENOIOCTL) {
1266 crit_exit();
1267 return (error);
1269 sc->setwin(sc, 0);
1270 switch (cmd) {
1271 case DIGIIO_RING:
1272 port->send_ring = *(u_char *)data;
1273 break;
1274 case TIOCSBRK:
1276 * now it sends 400 millisecond break because I don't know
1277 * how to send an infinite break
1279 fepcmd_w(port, SENDBREAK, 400, 10);
1280 break;
1281 case TIOCCBRK:
1282 /* now it's empty */
1283 break;
1284 case TIOCSDTR:
1285 digimctl(port, TIOCM_DTR, DMBIS);
1286 break;
1287 case TIOCCDTR:
1288 digimctl(port, TIOCM_DTR, DMBIC);
1289 break;
1290 case TIOCMSET:
1291 digimctl(port, *(int *)data, DMSET);
1292 break;
1293 case TIOCMBIS:
1294 digimctl(port, *(int *)data, DMBIS);
1295 break;
1296 case TIOCMBIC:
1297 digimctl(port, *(int *)data, DMBIC);
1298 break;
1299 case TIOCMGET:
1300 *(int *)data = digimctl(port, 0, DMGET);
1301 break;
1302 case TIOCMSDTRWAIT:
1303 error = suser_cred(ap->a_cred, 0);
1304 if (error != 0) {
1305 crit_exit();
1306 return (error);
1308 port->dtr_wait = *(int *)data *hz / 100;
1310 break;
1311 case TIOCMGDTRWAIT:
1312 *(int *)data = port->dtr_wait * 100 / hz;
1313 break;
1314 #ifdef DIGI_INTERRUPT
1315 case TIOCTIMESTAMP:
1316 *(struct timeval *)data = sc->intr_timestamp;
1318 break;
1319 #endif
1320 default:
1321 crit_exit();
1322 return (ENOTTY);
1324 crit_exit();
1325 return (0);
1328 static int
1329 digiparam(struct tty *tp, struct termios *t)
1331 int mynor;
1332 int unit;
1333 int pnum;
1334 struct digi_softc *sc;
1335 struct digi_p *port;
1336 int cflag;
1337 int iflag;
1338 int hflow;
1339 int window;
1341 mynor = minor(tp->t_dev);
1342 unit = MINOR_TO_UNIT(mynor);
1343 pnum = MINOR_TO_PORT(mynor);
1345 sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1346 KASSERT(sc, ("digi%d: softc not allocated in digiparam\n", unit));
1348 port = &sc->ports[pnum];
1350 DLOG(DIGIDB_SET, (sc->dev, "port%d: setting parameters\n", pnum));
1352 if (t->c_ispeed == 0)
1353 t->c_ispeed = t->c_ospeed;
1355 cflag = ttspeedtab(t->c_ospeed, digispeedtab);
1357 if (cflag < 0 || (cflag > 0 && t->c_ispeed != t->c_ospeed))
1358 return (EINVAL);
1360 crit_enter();
1362 window = sc->window;
1363 sc->setwin(sc, 0);
1365 if (cflag == 0) { /* hangup */
1366 DLOG(DIGIDB_SET, (sc->dev, "port%d: hangup\n", pnum));
1367 digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
1368 } else {
1369 digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIS);
1371 DLOG(DIGIDB_SET, (sc->dev, "port%d: CBAUD = %d\n", pnum,
1372 cflag));
1374 #if 0
1375 /* convert flags to sysV-style values */
1376 if (t->c_cflag & PARODD)
1377 cflag |= 0x0200;
1378 if (t->c_cflag & PARENB)
1379 cflag |= 0x0100;
1380 if (t->c_cflag & CSTOPB)
1381 cflag |= 0x0080;
1382 #else
1383 /* convert flags to sysV-style values */
1384 if (t->c_cflag & PARODD)
1385 cflag |= FEP_PARODD;
1386 if (t->c_cflag & PARENB)
1387 cflag |= FEP_PARENB;
1388 if (t->c_cflag & CSTOPB)
1389 cflag |= FEP_CSTOPB;
1390 if (t->c_cflag & CLOCAL)
1391 cflag |= FEP_CLOCAL;
1392 #endif
1394 cflag |= (t->c_cflag & CSIZE) >> 4;
1395 DLOG(DIGIDB_SET, (sc->dev, "port%d: CFLAG = 0x%x\n", pnum,
1396 cflag));
1397 fepcmd_w(port, SETCFLAGS, (unsigned)cflag, 0);
1400 iflag =
1401 t->c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP);
1402 if (port->c_iflag & IXON)
1403 iflag |= 0x400;
1404 if (port->c_iflag & IXANY)
1405 iflag |= 0x800;
1406 if (port->c_iflag & IXOFF)
1407 iflag |= 0x1000;
1409 DLOG(DIGIDB_SET, (sc->dev, "port%d: set iflag = 0x%x\n", pnum, iflag));
1410 fepcmd_w(port, SETIFLAGS, (unsigned)iflag, 0);
1412 hflow = 0;
1413 if (t->c_cflag & CDTR_IFLOW)
1414 hflow |= sc->csigs->dtr;
1415 if (t->c_cflag & CRTS_IFLOW)
1416 hflow |= sc->csigs->rts;
1417 if (t->c_cflag & CCTS_OFLOW)
1418 hflow |= sc->csigs->cts;
1419 if (t->c_cflag & CDSR_OFLOW)
1420 hflow |= port->dsr;
1421 if (t->c_cflag & CCAR_OFLOW)
1422 hflow |= port->cd;
1424 DLOG(DIGIDB_SET, (sc->dev, "port%d: set hflow = 0x%x\n", pnum, hflow));
1425 fepcmd_w(port, SETHFLOW, 0xff00 | (unsigned)hflow, 0);
1427 DLOG(DIGIDB_SET, (sc->dev, "port%d: set startc(0x%x), stopc(0x%x)\n",
1428 pnum, t->c_cc[VSTART], t->c_cc[VSTOP]));
1429 fepcmd_b(port, SONOFFC, t->c_cc[VSTART], t->c_cc[VSTOP], 0);
1431 if (sc->window != 0)
1432 sc->towin(sc, 0);
1433 if (window != 0)
1434 sc->towin(sc, window);
1435 crit_exit();
1437 return (0);
1440 static void
1441 digi_intr(void *vp)
1443 struct digi_p *port;
1444 char *cxcon;
1445 struct digi_softc *sc;
1446 int ehead, etail;
1447 volatile struct board_chan *bc;
1448 struct tty *tp;
1449 int head, tail;
1450 int wrapmask;
1451 int size, window;
1452 struct event {
1453 u_char pnum;
1454 u_char event;
1455 u_char mstat;
1456 u_char lstat;
1457 } event;
1459 sc = vp;
1461 if (sc->status != DIGI_STATUS_ENABLED) {
1462 DLOG(DIGIDB_IRQ, (sc->dev, "interrupt on disabled board !\n"));
1463 return;
1466 #ifdef DIGI_INTERRUPT
1467 microtime(&sc->intr_timestamp);
1468 #endif
1470 window = sc->window;
1471 sc->setwin(sc, 0);
1473 if (sc->model >= PCXEM && W(sc->vmem + 0xd00)) {
1474 struct con_bios *con = con_bios_list;
1475 u_char *ptr;
1477 ptr = sc->vmem + W(sc->vmem + 0xd00);
1478 while (con) {
1479 if (ptr[1] && W(ptr + 2) == W(con->bios + 2))
1480 /* Not first block -- exact match */
1481 break;
1483 if (W(ptr + 4) >= W(con->bios + 4) &&
1484 W(ptr + 4) <= W(con->bios + 6))
1485 /* Initial search concetrator BIOS */
1486 break;
1489 if (con == NULL) {
1490 log(LOG_ERR, "digi%d: wanted bios LREV = 0x%04x"
1491 " not found!\n", sc->res.unit, W(ptr + 4));
1492 W(ptr + 10) = 0;
1493 W(sc->vmem + 0xd00) = 0;
1494 goto eoi;
1496 cxcon = con->bios;
1497 W(ptr + 4) = W(cxcon + 4);
1498 W(ptr + 6) = W(cxcon + 6);
1499 if (ptr[1] == 0)
1500 W(ptr + 2) = W(cxcon + 2);
1501 W(ptr + 8) = (ptr[1] << 6) + W(cxcon + 8);
1502 size = W(cxcon + 10) - (ptr[1] << 10);
1503 if (size <= 0) {
1504 W(ptr + 8) = W(cxcon + 8);
1505 W(ptr + 10) = 0;
1506 } else {
1507 if (size > 1024)
1508 size = 1024;
1509 W(ptr + 10) = size;
1510 bcopy(cxcon + (ptr[1] << 10), ptr + 12, size);
1512 W(sc->vmem + 0xd00) = 0;
1513 goto eoi;
1516 ehead = sc->gdata->ein;
1517 etail = sc->gdata->eout;
1518 if (ehead == etail) {
1519 #ifdef DEBUG
1520 sc->intr_count++;
1521 if (sc->intr_count % 6000 == 0) {
1522 DLOG(DIGIDB_IRQ, (sc->dev,
1523 "6000 useless polls %x %x\n", ehead, etail));
1524 sc->intr_count = 0;
1526 #endif
1527 goto eoi;
1529 while (ehead != etail) {
1530 event = *(volatile struct event *)(sc->memevent + etail);
1532 etail = (etail + 4) & sc->gdata->imax;
1534 if (event.pnum >= sc->numports) {
1535 log(LOG_ERR, "digi%d: port %d: got event"
1536 " on nonexisting port\n", sc->res.unit,
1537 event.pnum);
1538 continue;
1540 port = &sc->ports[event.pnum];
1541 bc = port->bc;
1542 tp = port->tp;
1544 if (!(tp->t_state & TS_ISOPEN) && !port->wopeners) {
1545 DLOG(DIGIDB_IRQ, (sc->dev,
1546 "port %d: event 0x%x on closed port\n",
1547 event.pnum, event.event));
1548 bc->rout = bc->rin;
1549 bc->idata = 0;
1550 bc->iempty = 0;
1551 bc->ilow = 0;
1552 bc->mint = 0;
1553 continue;
1555 if (event.event & ~ALL_IND)
1556 log(LOG_ERR, "digi%d: port%d: ? event 0x%x mstat 0x%x"
1557 " lstat 0x%x\n", sc->res.unit, event.pnum,
1558 event.event, event.mstat, event.lstat);
1560 if (event.event & DATA_IND) {
1561 DLOG(DIGIDB_IRQ, (sc->dev, "port %d: DATA_IND\n",
1562 event.pnum));
1563 wrapmask = port->rxbufsize - 1;
1564 head = bc->rin;
1565 tail = bc->rout;
1567 size = 0;
1568 if (!(tp->t_state & TS_ISOPEN)) {
1569 bc->rout = head;
1570 goto end_of_data;
1572 while (head != tail) {
1573 int top;
1575 DLOG(DIGIDB_INT, (sc->dev,
1576 "port %d: p rx head = %d tail = %d\n",
1577 event.pnum, head, tail));
1578 top = (head > tail) ? head : wrapmask + 1;
1579 sc->towin(sc, port->rxwin);
1580 size = top - tail;
1581 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1582 size = b_to_q((char *)port->rxbuf +
1583 tail, size, &tp->t_rawq);
1584 tail = top - size;
1585 ttwakeup(tp);
1586 } else for (; tail < top;) {
1587 linesw[tp->t_line].
1588 l_rint(port->rxbuf[tail], tp);
1589 sc->towin(sc, port->rxwin);
1590 size--;
1591 tail++;
1592 if (tp->t_state & TS_TBLOCK)
1593 break;
1595 tail &= wrapmask;
1596 sc->setwin(sc, 0);
1597 bc->rout = tail;
1598 head = bc->rin;
1599 if (size)
1600 break;
1603 if (bc->orun) {
1604 CE_RECORD(port, CE_OVERRUN);
1605 log(LOG_ERR, "digi%d: port%d: %s\n",
1606 sc->res.unit, event.pnum,
1607 digi_errortxt(CE_OVERRUN));
1608 bc->orun = 0;
1610 end_of_data:
1611 if (size) {
1612 tp->t_state |= TS_TBLOCK;
1613 port->status |= PAUSE_RX;
1614 DLOG(DIGIDB_RX, (sc->dev, "port %d: pause RX\n",
1615 event.pnum));
1616 } else {
1617 bc->idata = 1;
1621 if (event.event & MODEMCHG_IND) {
1622 DLOG(DIGIDB_MODEM, (sc->dev, "port %d: MODEMCHG_IND\n",
1623 event.pnum));
1625 if ((event.mstat ^ event.lstat) & port->cd) {
1626 sc->hidewin(sc);
1627 linesw[tp->t_line].l_modem
1628 (tp, event.mstat & port->cd);
1629 sc->setwin(sc, 0);
1630 wakeup(TSA_CARR_ON(tp));
1633 if (event.mstat & sc->csigs->ri) {
1634 DLOG(DIGIDB_RI, (sc->dev, "port %d: RING\n",
1635 event.pnum));
1636 if (port->send_ring) {
1637 linesw[tp->t_line].l_rint('R', tp);
1638 linesw[tp->t_line].l_rint('I', tp);
1639 linesw[tp->t_line].l_rint('N', tp);
1640 linesw[tp->t_line].l_rint('G', tp);
1641 linesw[tp->t_line].l_rint('\r', tp);
1642 linesw[tp->t_line].l_rint('\n', tp);
1646 if (event.event & BREAK_IND) {
1647 DLOG(DIGIDB_MODEM, (sc->dev, "port %d: BREAK_IND\n",
1648 event.pnum));
1649 linesw[tp->t_line].l_rint(TTY_BI, tp);
1651 if (event.event & (LOWTX_IND | EMPTYTX_IND)) {
1652 DLOG(DIGIDB_IRQ, (sc->dev, "port %d:%s%s\n",
1653 event.pnum,
1654 event.event & LOWTX_IND ? " LOWTX" : "",
1655 event.event & EMPTYTX_IND ? " EMPTYTX" : ""));
1656 (*linesw[tp->t_line].l_start)(tp);
1659 sc->gdata->eout = etail;
1660 eoi:
1661 if (sc->window != 0)
1662 sc->towin(sc, 0);
1663 if (window != 0)
1664 sc->towin(sc, window);
1667 static void
1668 digistart(struct tty *tp)
1670 int unit;
1671 int pnum;
1672 struct digi_p *port;
1673 struct digi_softc *sc;
1674 volatile struct board_chan *bc;
1675 int head, tail;
1676 int size, ocount, totcnt = 0;
1677 int wmask;
1679 unit = MINOR_TO_UNIT(minor(tp->t_dev));
1680 pnum = MINOR_TO_PORT(minor(tp->t_dev));
1682 sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1683 KASSERT(sc, ("digi%d: softc not allocated in digistart\n", unit));
1685 port = &sc->ports[pnum];
1686 bc = port->bc;
1688 wmask = port->txbufsize - 1;
1690 crit_enter();
1691 port->lcc = tp->t_outq.c_cc;
1692 sc->setwin(sc, 0);
1693 if (!(tp->t_state & TS_TBLOCK)) {
1694 if (port->status & PAUSE_RX) {
1695 DLOG(DIGIDB_RX, (sc->dev, "port %d: resume RX\n",
1696 pnum));
1698 * CAREFUL - braces are needed here if the DLOG is
1699 * optimised out!
1702 port->status &= ~PAUSE_RX;
1703 bc->idata = 1;
1705 if (!(tp->t_state & TS_TTSTOP) && port->status & PAUSE_TX) {
1706 DLOG(DIGIDB_TX, (sc->dev, "port %d: resume TX\n", pnum));
1707 port->status &= ~PAUSE_TX;
1708 fepcmd_w(port, RESUMETX, 0, 10);
1710 if (tp->t_outq.c_cc == 0)
1711 tp->t_state &= ~TS_BUSY;
1712 else
1713 tp->t_state |= TS_BUSY;
1715 head = bc->tin;
1716 while (tp->t_outq.c_cc != 0) {
1717 tail = bc->tout;
1718 DLOG(DIGIDB_INT, (sc->dev, "port%d: s tx head = %d tail = %d\n",
1719 pnum, head, tail));
1721 if (head < tail)
1722 size = tail - head - 1;
1723 else {
1724 size = port->txbufsize - head;
1725 if (tail == 0)
1726 size--;
1729 if (size == 0)
1730 break;
1731 sc->towin(sc, port->txwin);
1732 ocount = q_to_b(&tp->t_outq, port->txbuf + head, size);
1733 totcnt += ocount;
1734 head += ocount;
1735 head &= wmask;
1736 sc->setwin(sc, 0);
1737 bc->tin = head;
1738 bc->iempty = 1;
1739 bc->ilow = 1;
1741 port->lostcc = tp->t_outq.c_cc;
1742 tail = bc->tout;
1743 if (head < tail)
1744 size = port->txbufsize - tail + head;
1745 else
1746 size = head - tail;
1748 port->lbuf = size;
1749 DLOG(DIGIDB_INT, (sc->dev, "port%d: s total cnt = %d\n", pnum, totcnt));
1750 ttwwakeup(tp);
1751 crit_exit();
1754 static void
1755 digistop(struct tty *tp, int rw)
1757 struct digi_softc *sc;
1758 int unit;
1759 int pnum;
1760 struct digi_p *port;
1762 unit = MINOR_TO_UNIT(minor(tp->t_dev));
1763 pnum = MINOR_TO_PORT(minor(tp->t_dev));
1765 sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1766 KASSERT(sc, ("digi%d: softc not allocated in digistop\n", unit));
1767 port = sc->ports + pnum;
1769 DLOG(DIGIDB_TX, (sc->dev, "port %d: pause TX\n", pnum));
1770 port->status |= PAUSE_TX;
1771 fepcmd_w(port, PAUSETX, 0, 10);
1774 static void
1775 fepcmd(struct digi_p *port, int cmd, int op1, int ncmds)
1777 u_char *mem;
1778 unsigned tail, head;
1779 int count, n;
1781 mem = port->sc->memcmd;
1783 port->sc->setwin(port->sc, 0);
1785 head = port->sc->gdata->cin;
1786 mem[head + 0] = cmd;
1787 mem[head + 1] = port->pnum;
1788 *(u_short *)(mem + head + 2) = op1;
1790 head = (head + 4) & port->sc->gdata->cmax;
1791 port->sc->gdata->cin = head;
1793 for (count = FEPTIMEOUT; count > 0; count--) {
1794 head = port->sc->gdata->cin;
1795 tail = port->sc->gdata->cout;
1796 n = (head - tail) & port->sc->gdata->cmax;
1798 if (n <= ncmds * sizeof(short) * 4)
1799 break;
1801 if (count == 0)
1802 log(LOG_ERR, "digi%d: port%d: timeout on FEP command\n",
1803 port->sc->res.unit, port->pnum);
1806 const char *
1807 digi_errortxt(int id)
1809 static const char *error_desc[] = {
1810 "silo overflow",
1811 "interrupt-level buffer overflow",
1812 "tty-level buffer overflow",
1815 KASSERT(id >= 0 && id < sizeof(error_desc) / sizeof(error_desc[0]),
1816 ("Unexpected digi error id %d\n", id));
1818 return (error_desc[id]);
1822 digi_attach(struct digi_softc *sc)
1824 sc->res.ctldev = make_dev(&digi_ops,
1825 (sc->res.unit << 16) | CTRL_DEV, UID_ROOT, GID_WHEEL,
1826 0600, "digi%r.ctl", sc->res.unit);
1828 digi_loaddata(sc);
1829 digi_init(sc);
1830 digi_freedata(sc);
1832 return (0);
1835 static int
1836 digi_inuse(struct digi_softc *sc)
1838 int i;
1840 for (i = 0; i < sc->numports; i++)
1841 if (sc->ttys[i].t_state & TS_ISOPEN) {
1842 DLOG(DIGIDB_INIT, (sc->dev, "port%d: busy\n", i));
1843 return (1);
1844 } else if (sc->ports[i].wopeners || sc->ports[i].opencnt) {
1845 DLOG(DIGIDB_INIT, (sc->dev, "port%d: blocked in open\n",
1846 i));
1847 return (1);
1849 return (0);
1852 static void
1853 digi_free_state(struct digi_softc *sc)
1855 int d, i;
1857 /* Blow it all away */
1859 for (i = 0; i < sc->numports; i++)
1860 for (d = 0; d < 6; d++)
1861 destroy_dev(sc->ports[i].dev[d]);
1863 callout_stop(&sc->callout);
1864 callout_stop(&sc->inttest);
1866 bus_teardown_intr(sc->dev, sc->res.irq, sc->res.irqHandler);
1867 #ifdef DIGI_INTERRUPT
1868 if (sc->res.irq != NULL) {
1869 bus_release_resource(dev, SYS_RES_IRQ, sc->res.irqrid,
1870 sc->res.irq);
1871 sc->res.irq = NULL;
1873 #endif
1874 if (sc->numports) {
1875 KASSERT(sc->ports, ("digi%d: Lost my ports ?", sc->res.unit));
1876 KASSERT(sc->ttys, ("digi%d: Lost my ttys ?", sc->res.unit));
1877 kfree(sc->ports, M_TTYS);
1878 sc->ports = NULL;
1879 kfree(sc->ttys, M_TTYS);
1880 sc->ttys = NULL;
1881 sc->numports = 0;
1884 sc->status = DIGI_STATUS_NOTINIT;
1888 digi_detach(device_t dev)
1890 struct digi_softc *sc = device_get_softc(dev);
1892 DLOG(DIGIDB_INIT, (sc->dev, "detaching\n"));
1894 /* If we're INIT'd, numports must be 0 */
1895 KASSERT(sc->numports == 0 || sc->status != DIGI_STATUS_NOTINIT,
1896 ("digi%d: numports(%d) & status(%d) are out of sync",
1897 sc->res.unit, sc->numports, (int)sc->status));
1899 if (digi_inuse(sc))
1900 return (EBUSY);
1902 digi_free_state(sc);
1904 destroy_dev(sc->res.ctldev);
1906 if (sc->res.mem != NULL) {
1907 bus_release_resource(dev, SYS_RES_MEMORY, sc->res.mrid,
1908 sc->res.mem);
1909 sc->res.mem = NULL;
1911 if (sc->res.io != NULL) {
1912 bus_release_resource(dev, SYS_RES_IOPORT, sc->res.iorid,
1913 sc->res.io);
1914 sc->res.io = NULL;
1917 return (0);
1921 digi_shutdown(device_t dev)
1923 return (0);
1926 MODULE_VERSION(digi, 1);