2 * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3 * based on work by Slawa Olhovchenkov
4 * John Prince <johnp@knight-trosoft.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
38 #include "opt_compat.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/linker.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
50 #include <sys/syslog.h>
51 #include <sys/fcntl.h>
52 #include <sys/serial.h>
54 #include <machine/resource.h>
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>
62 static t_open_t digiopen;
63 static d_open_t digicopen;
64 static d_close_t digicclose;
65 static t_ioctl_t digiioctl;
66 static d_ioctl_t digisioctl;
67 static d_ioctl_t digicioctl;
69 static void digistop(struct tty *tp, int rw);
70 static void digibreak(struct tty *tp, int brk);
71 static int digimodem(struct tty *tp, int sigon, int sigoff);
72 static void digi_poll(void *ptr);
73 static void digi_freemoduledata(struct digi_softc *);
74 static void fepcmd(struct digi_p *port, int cmd, int op, int ncmds);
75 static void digistart(struct tty *tp);
76 static int digiparam(struct tty *tp, struct termios *t);
77 static void digiclose(struct tty *tp);
78 static void digi_intr(void *);
79 static int digi_init(struct digi_softc *_sc);
80 static int digi_loadmoduledata(struct digi_softc *);
81 static int digi_inuse(struct digi_softc *);
82 static void digi_free_state(struct digi_softc *);
84 #define fepcmd_b(port, cmd, op1, op2, ncmds) \
85 fepcmd(port, cmd, (op2 << 8) | op1, ncmds)
86 #define fepcmd_w fepcmd
89 struct con_bios *next;
94 static struct con_bios *con_bios_list;
95 devclass_t digi_devclass;
96 static char driver_name[] = "digi";
97 unsigned digi_debug = 0;
99 static struct speedtab digispeedtab[] = {
100 { 0, 0}, /* old (sysV-like) Bx codes */
116 { 57600, (02000 | 1)},
117 { 76800, (02000 | 2)},
118 { 115200, (02000 | 3)},
119 { 230400, (02000 | 6)},
123 const struct digi_control_signals digi_xixe_signals = {
124 0x02, 0x08, 0x10, 0x20, 0x40, 0x80
127 const struct digi_control_signals digi_normal_signals = {
128 0x02, 0x80, 0x20, 0x10, 0x40, 0x01
131 static struct cdevsw digi_csw = {
132 .d_version = D_VERSION,
134 .d_close = digicclose,
135 .d_ioctl = digicioctl,
136 .d_name = driver_name,
137 .d_flags = D_TTY | D_NEEDGIANT,
143 struct digi_softc *sc;
145 sc = (struct digi_softc *)ptr;
146 callout_handle_init(&sc->callout);
148 sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
152 digi_int_test(void *v)
154 struct digi_softc *sc = v;
156 callout_handle_init(&sc->inttest);
157 #ifdef DIGI_INTERRUPT
158 if (sc->intr_timestamp.tv_sec || sc->intr_timestamp.tv_usec) {
162 log(LOG_ERR, "digi%d: Interrupt didn't work, use polled mode\n", unit);
164 sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
168 digi_freemoduledata(struct digi_softc *sc)
170 if (sc->fep.data != NULL) {
171 free(sc->fep.data, M_TTYS);
174 if (sc->link.data != NULL) {
175 free(sc->link.data, M_TTYS);
176 sc->link.data = NULL;
178 if (sc->bios.data != NULL) {
179 free(sc->bios.data, M_TTYS);
180 sc->bios.data = NULL;
185 digi_bcopy(const void *vfrom, void *vto, size_t sz)
187 volatile const char *from = (volatile const char *)vfrom;
188 volatile char *to = (volatile char *)vto;
191 for (i = 0; i < sz; i++)
194 from = (const volatile char *)vfrom;
195 to = (volatile char *)vto;
196 for (i = 0; i < sz; i++)
197 if (*to++ != *from++)
203 digi_delay(struct digi_softc *sc, const char *txt, u_long timo)
206 DELAY(timo * 1000000 / hz);
208 tsleep(sc, PUSER | PCATCH, txt, timo);
212 digi_init(struct digi_softc *sc)
218 volatile struct board_chan *bc;
223 if (sc->status == DIGI_STATUS_DISABLED) {
224 log(LOG_ERR, "digi%d: Cannot init a disabled card\n",
228 if (sc->bios.data == NULL) {
229 log(LOG_ERR, "digi%d: Cannot init without BIOS\n",
234 if (sc->link.data == NULL && sc->model >= PCCX) {
235 log(LOG_ERR, "digi%d: Cannot init without link info\n",
240 if (sc->fep.data == NULL) {
241 log(LOG_ERR, "digi%d: Cannot init without fep code\n",
245 sc->status = DIGI_STATUS_NOTINIT;
249 * We're re-initialising - maybe because someone's attached
250 * another port module. For now, we just re-initialise
259 ptr = sc->setwin(sc, MISCGLOBAL);
260 for (i = 0; i < 16; i += 2)
265 outb(sc->wport, 0xff); /* window 7 */
266 ptr = sc->vmem + (BIOSCODE & 0x1fff);
268 if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
269 device_printf(sc->dev, "BIOS upload failed\n");
273 outb(sc->port, FEPCLR);
279 ptr = sc->setwin(sc, BIOSCODE + ((0xf000 - sc->mem_seg) << 4));
280 if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
281 device_printf(sc->dev, "BIOS upload failed\n");
292 outb(sc->port, FEPRST | FEPMEM);
294 for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) &
295 FEPMASK) != FEPRST; i++) {
297 log(LOG_ERR, "digi%d: %s init reset failed\n",
298 sc->res.unit, sc->name);
301 digi_delay(sc, "digiinit0", 5);
303 DLOG(DIGIDB_INIT, (sc->dev, "Got init reset after %d us\n", i));
305 /* Now upload the BIOS */
306 cnt = (sc->bios.size < sc->win_size - BIOSOFFSET) ?
307 sc->bios.size : sc->win_size - BIOSOFFSET;
309 ptr = sc->setwin(sc, BIOSOFFSET);
310 if (!digi_bcopy(sc->bios.data, ptr, cnt)) {
311 device_printf(sc->dev, "BIOS upload (1) failed\n");
315 if (cnt != sc->bios.size) {
316 /* and the second part */
317 ptr = sc->setwin(sc, sc->win_size);
318 if (!digi_bcopy(sc->bios.data + cnt, ptr,
319 sc->bios.size - cnt)) {
320 device_printf(sc->dev, "BIOS upload failed\n");
325 ptr = sc->setwin(sc, 0);
326 vW(ptr + 0) = 0x0401;
327 vW(ptr + 2) = 0x0bf0;
328 vW(ptr + 4) = 0x0000;
329 vW(ptr + 6) = 0x0000;
334 DLOG(DIGIDB_INIT, (sc->dev, "BIOS uploaded\n"));
336 ptr = sc->setwin(sc, MISCGLOBAL);
342 } else if (sc->model == PCXEVE) {
343 outb(sc->port, FEPCLR);
346 outb(sc->port, FEPCLR | FEPMEM);
347 resp = FEPRST | FEPMEM;
350 for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) & FEPMASK)
353 log(LOG_ERR, "digi%d: BIOS start failed\n",
357 digi_delay(sc, "digibios0", 5);
360 DLOG(DIGIDB_INIT, (sc->dev, "BIOS started after %d us\n", i));
362 for (i = 0; vW(ptr) != *(u_short *)"GD"; i++) {
364 log(LOG_ERR, "digi%d: BIOS boot failed "
365 "(0x%02x != 0x%02x)\n",
366 sc->res.unit, vW(ptr), *(u_short *)"GD");
369 digi_delay(sc, "digibios1", 5);
372 DLOG(DIGIDB_INIT, (sc->dev, "BIOS booted after %d iterations\n", i));
374 if (sc->link.data != NULL) {
375 DLOG(DIGIDB_INIT, (sc->dev, "Loading link data\n"));
376 ptr = sc->setwin(sc, 0xcd0);
377 digi_bcopy(sc->link.data, ptr, 21); /* XXX 21 ? */
386 ptr = sc->setwin(sc, sc->model == PCXI ? 0x2000 : 0x0);
387 digi_bcopy(sc->fep.data, ptr, sc->fep.size);
389 /* A BIOS request to move our data to 0x2000 */
390 ptr = sc->setwin(sc, MBOX);
392 vW(ptr + 2) = sc->mem_seg + FEPCODESEG;
394 vW(ptr + 6) = FEPCODESEG;
396 vW(ptr + 10) = sc->fep.size;
398 /* Run the BIOS request */
399 outb(sc->port, FEPREQ | FEPMEM);
400 outb(sc->port, FEPCLR | FEPMEM);
402 for (i = 0; W(ptr); i++) {
404 log(LOG_ERR, "digi%d: FEP/OS move failed\n",
409 digi_delay(sc, "digifep0", 5);
412 (sc->dev, "FEP/OS moved after %d iterations\n", i));
414 /* Clear the confirm word */
415 ptr = sc->setwin(sc, FEPSTAT);
418 /* A BIOS request to execute the FEP/OS */
419 ptr = sc->setwin(sc, MBOX);
421 vW(ptr + 2) = FEPCODESEG;
424 /* Run the BIOS request */
425 outb(sc->port, FEPREQ);
426 outb(sc->port, FEPCLR);
428 ptr = sc->setwin(sc, FEPSTAT);
435 DLOG(DIGIDB_INIT, (sc->dev, "Loading FEP/OS\n"));
437 cnt = (sc->fep.size < sc->win_size - BIOSOFFSET) ?
438 sc->fep.size : sc->win_size - BIOSOFFSET;
440 ptr = sc->setwin(sc, BIOSOFFSET);
441 digi_bcopy(sc->fep.data, ptr, cnt);
443 if (cnt != sc->fep.size) {
444 ptr = sc->setwin(sc, BIOSOFFSET + cnt);
445 digi_bcopy(sc->fep.data + cnt, ptr,
449 DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS loaded\n"));
451 ptr = sc->setwin(sc, 0xc30);
457 /* Clear the confirm word */
458 ptr = sc->setwin(sc, FEPSTAT);
462 outb(sc->port, 0); /* XXX necessary ? */
467 ptr = sc->setwin(sc, 0xd000);
468 digi_bcopy(sc->fep.data, ptr, sc->fep.size);
470 /* A BIOS request to execute the FEP/OS */
471 ptr = sc->setwin(sc, 0xc40);
473 W(ptr + 2) = FEPCODE >> 4;
476 /* Clear the confirm word */
477 ptr = sc->setwin(sc, FEPSTAT);
480 /* Run the BIOS request */
481 outb(sc->port, FEPREQ | FEPMEM); /* send interrupt to BIOS */
482 outb(sc->port, FEPCLR | FEPMEM);
486 /* Now wait 'till the FEP/OS has booted */
487 for (i = 0; vW(ptr) != *(u_short *)"OS"; i++) {
489 log(LOG_ERR, "digi%d: FEP/OS start failed "
490 "(0x%02x != 0x%02x)\n",
491 sc->res.unit, vW(ptr), *(u_short *)"OS");
495 digi_delay(sc, "digifep1", 5);
498 DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS started after %d iterations\n", i));
500 if (sc->model >= PCXEM) {
501 ptr = sc->setwin(sc, 0xe04);
503 ptr = sc->setwin(sc, 0xc02);
504 sc->numports = vW(ptr);
506 ptr = sc->setwin(sc, 0xc22);
507 sc->numports = vW(ptr);
510 if (sc->numports == 0) {
511 device_printf(sc->dev, "%s, 0 ports found\n", sc->name);
516 device_printf(sc->dev, "%s, %d ports found\n", sc->name, sc->numports);
519 free(sc->ports, M_TTYS);
520 sc->ports = malloc(sizeof(struct digi_p) * sc->numports,
521 M_TTYS, M_WAITOK | M_ZERO);
524 * XXX Should read port 0xc90 for an array of 2byte values, 1 per
525 * port. If the value is 0, the port is broken....
528 ptr = sc->setwin(sc, 0);
530 /* We should now init per-port structures */
531 bc = (volatile struct board_chan *)(ptr + CHANSTRUCT);
532 sc->gdata = (volatile struct global_data *)(ptr + FEP_GLOBAL);
534 sc->memcmd = ptr + sc->gdata->cstart;
535 sc->memevent = ptr + sc->gdata->istart;
537 for (i = 0; i < sc->numports; i++, bc++) {
538 port = sc->ports + i;
541 port->status = ENABLED;
543 tp = port->tp = ttyalloc();
544 tp->t_oproc = digistart;
545 tp->t_param = digiparam;
546 tp->t_modem = digimodem;
547 tp->t_break = digibreak;
548 tp->t_stop = digistop;
549 tp->t_cioctl = digisioctl;
550 tp->t_ioctl = digiioctl;
551 tp->t_open = digiopen;
552 tp->t_close = digiclose;
555 if (sc->model == PCXEVE) {
557 (((bc->tseg - sc->mem_seg) << 4) & 0x1fff);
559 (((bc->rseg - sc->mem_seg) << 4) & 0x1fff);
560 port->txwin = FEPWIN | ((bc->tseg - sc->mem_seg) >> 9);
561 port->rxwin = FEPWIN | ((bc->rseg - sc->mem_seg) >> 9);
562 } else if (sc->model == PCXI || sc->model == PCXE) {
563 port->txbuf = ptr + ((bc->tseg - sc->mem_seg) << 4);
564 port->rxbuf = ptr + ((bc->rseg - sc->mem_seg) << 4);
565 port->txwin = port->rxwin = 0;
568 (((bc->tseg - sc->mem_seg) << 4) % sc->win_size);
570 (((bc->rseg - sc->mem_seg) << 4) % sc->win_size);
571 port->txwin = FEPWIN |
572 (((bc->tseg - sc->mem_seg) << 4) / sc->win_size);
573 port->rxwin = FEPWIN |
574 (((bc->rseg - sc->mem_seg) << 4) / sc->win_size);
576 port->txbufsize = bc->tmax + 1;
577 port->rxbufsize = bc->rmax + 1;
579 lowwater = port->txbufsize >> 2;
583 fepcmd_w(port, STXLWATER, lowwater, 10);
584 fepcmd_w(port, SRXLWATER, port->rxbufsize >> 2, 10);
585 fepcmd_w(port, SRXHWATER, (3 * port->rxbufsize) >> 2, 10);
589 ttyinitmode(tp, 0, 0);
590 port->send_ring = 1; /* Default action on signal RI */
591 ttycreate(tp, NULL, 0, MINOR_CALLOUT, "D%r%r", sc->res.unit, i);
595 sc->inttest = timeout(digi_int_test, sc, hz);
596 /* fepcmd_w(&sc->ports[0], 0xff, 0, 0); */
597 sc->status = DIGI_STATUS_ENABLED;
603 digimodem(struct tty *tp, int sigon, int sigoff)
605 struct digi_softc *sc;
607 int bitand, bitor, mstat;
612 if (sigon == 0 && sigoff == 0) {
613 port->sc->setwin(port->sc, 0);
614 mstat = port->bc->mstat;
615 port->sc->hidewin(port->sc);
616 if (mstat & port->sc->csigs->rts)
618 if (mstat & port->cd)
620 if (mstat & port->dsr)
622 if (mstat & port->sc->csigs->cts)
624 if (mstat & port->sc->csigs->ri)
626 if (mstat & port->sc->csigs->dtr)
634 if (sigoff & SER_DTR)
635 bitand |= port->sc->csigs->dtr;
636 if (sigoff & SER_RTS)
637 bitand |= port->sc->csigs->rts;
639 bitor |= port->sc->csigs->dtr;
641 bitor |= port->sc->csigs->rts;
642 fepcmd_b(port, SETMODEM, bitor, ~bitand, 0);
647 digicopen(struct cdev *dev, int flag, int mode, struct thread *td)
649 struct digi_softc *sc;
652 if (sc->status != DIGI_STATUS_ENABLED) {
653 DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
661 digiopen(struct tty *tp, struct cdev *dev)
664 struct digi_softc *sc;
666 volatile struct board_chan *bc;
671 if (sc->status != DIGI_STATUS_ENABLED) {
672 DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
678 * The device isn't open, so there are no conflicts.
679 * Initialize it. Initialization is done twice in many
680 * cases: to preempt sleeping callin opens if we are callout,
681 * and to complete a callin open after DCD rises.
685 bc->rout = bc->rin; /* clear input queue */
689 bc->mint = port->cd | port->sc->csigs->ri;
692 port->cd = sc->csigs->dsr;
693 port->dsr = sc->csigs->cd;
695 port->cd = sc->csigs->cd;
696 port->dsr = sc->csigs->dsr;
698 tp->t_wopeners++; /* XXX required ? */
699 error = digiparam(tp, &tp->t_termios);
706 digicclose(struct cdev *dev, int flag, int mode, struct thread *td)
708 struct digi_softc *sc;
716 digidtrwakeup(void *chan)
718 struct digi_p *port = chan;
720 port->status &= ~DIGI_DTR_OFF;
721 wakeup(&port->tp->t_dtr_wait);
722 port->tp->t_wopeners--;
726 digiclose(struct tty *tp)
728 volatile struct board_chan *bc;
736 port->sc->setwin(port->sc, 0);
741 if ((tp->t_cflag & HUPCL) ||
742 (!tp->t_actout && !(bc->mstat & port->cd) &&
743 !(tp->t_init_in.c_cflag & CLOCAL)) ||
744 !(tp->t_state & TS_ISOPEN)) {
745 digimodem(tp, 0, SER_DTR | SER_RTS);
746 if (tp->t_dtr_wait != 0) {
747 /* Schedule a wakeup of any callin devices */
749 timeout(&digidtrwakeup, port, tp->t_dtr_wait);
750 port->status |= DIGI_DTR_OFF;
753 tp->t_actout = FALSE;
754 wakeup(&tp->t_actout);
755 wakeup(TSA_CARR_ON(tp));
760 * Load module "digi_<mod>.ko" and look for a symbol called digi_mod_<mod>.
762 * Populate sc->bios, sc->fep, and sc->link from this data.
764 * sc->fep.data, sc->bios.data and sc->link.data are malloc()d according
765 * to their respective sizes.
767 * The module is unloaded when we're done.
770 digi_loadmoduledata(struct digi_softc *sc)
772 struct digi_mod *digi_mod;
778 KASSERT(sc->bios.data == NULL, ("Uninitialised BIOS variable"));
779 KASSERT(sc->fep.data == NULL, ("Uninitialised FEP variable"));
780 KASSERT(sc->link.data == NULL, ("Uninitialised LINK variable"));
781 KASSERT(sc->module != NULL, ("Uninitialised module name"));
783 modlen = strlen(sc->module);
784 modfile = malloc(modlen + 6, M_TEMP, M_WAITOK);
785 snprintf(modfile, modlen + 6, "digi_%s", sc->module);
786 res = linker_reference_module(modfile, NULL, &lf);
787 if ((res = linker_reference_module(modfile, NULL, &lf)) != 0)
788 printf("%s: Failed %d to autoload module\n", modfile, res);
789 free(modfile, M_TEMP);
793 sym = malloc(modlen + 10, M_TEMP, M_WAITOK);
794 snprintf(sym, modlen + 10, "digi_mod_%s", sc->module);
795 if ((symptr = linker_file_lookup_symbol(lf, sym, 0)) == NULL)
796 printf("digi_%s.ko: Symbol `%s' not found\n", sc->module, sym);
799 digi_mod = (struct digi_mod *)symptr;
800 if (digi_mod->dm_version != DIGI_MOD_VERSION) {
801 printf("digi_%s.ko: Invalid version %d (need %d)\n",
802 sc->module, digi_mod->dm_version, DIGI_MOD_VERSION);
803 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
807 sc->bios.size = digi_mod->dm_bios.size;
808 if (sc->bios.size != 0 && digi_mod->dm_bios.data != NULL) {
809 sc->bios.data = malloc(sc->bios.size, M_TTYS, M_WAITOK);
810 bcopy(digi_mod->dm_bios.data, sc->bios.data, sc->bios.size);
813 sc->fep.size = digi_mod->dm_fep.size;
814 if (sc->fep.size != 0 && digi_mod->dm_fep.data != NULL) {
815 sc->fep.data = malloc(sc->fep.size, M_TTYS, M_WAITOK);
816 bcopy(digi_mod->dm_fep.data, sc->fep.data, sc->fep.size);
819 sc->link.size = digi_mod->dm_link.size;
820 if (sc->link.size != 0 && digi_mod->dm_link.data != NULL) {
821 sc->link.data = malloc(sc->link.size, M_TTYS, M_WAITOK);
822 bcopy(digi_mod->dm_link.data, sc->link.data, sc->link.size);
825 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
831 digisioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
834 struct digi_softc *sc;
840 case DIGIIO_GETALTPIN:
842 *(int *)data = port->ialtpin;
843 else if (ISLOCK(dev))
844 *(int *)data = port->laltpin;
848 case DIGIIO_SETALTPIN:
850 if (!port->laltpin) {
851 port->ialtpin = !!*(int *)data;
852 DLOG(DIGIDB_SET, (sc->dev,
853 "port%d: initial ALTPIN %s\n", port->pnum,
854 port->ialtpin ? "set" : "cleared"));
856 } else if (ISLOCK(dev)) {
857 port->laltpin = !!*(int *)data;
858 DLOG(DIGIDB_SET, (sc->dev,
859 "port%d: ALTPIN %slocked\n",
860 port->pnum, port->laltpin ? "" : "un"));
871 digicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
874 struct digi_softc *sc;
878 if (sc->status == DIGI_STATUS_DISABLED)
884 digi_debug = *(int *)data;
887 device_printf(sc->dev, "DEBUG not defined\n");
891 digi_loadmoduledata(sc);
892 error = digi_init(sc);
893 digi_freemoduledata(sc);
897 *(enum digi_model *)data = sc->model;
901 return (copyout(sc->name, *(char **)data,
902 strlen(sc->name) + 1));
909 digiioctl(struct tty *tp, u_long cmd, void *data, int flag, struct thread *td)
911 struct digi_softc *sc;
916 if (sc->status == DIGI_STATUS_DISABLED)
919 if (!(port->status & ENABLED))
923 case DIGIIO_GETALTPIN:
924 *(int *)data = !!(port->dsr == sc->csigs->cd);
927 case DIGIIO_SETALTPIN:
928 if (!port->laltpin) {
930 DLOG(DIGIDB_SET, (sc->dev,
931 "port%d: ALTPIN set\n", port->pnum));
932 port->cd = sc->csigs->dsr;
933 port->dsr = sc->csigs->cd;
935 DLOG(DIGIDB_SET, (sc->dev,
936 "port%d: ALTPIN cleared\n", port->pnum));
937 port->cd = sc->csigs->cd;
938 port->dsr = sc->csigs->dsr;
943 port->send_ring = *(u_char *)data;
952 digibreak(struct tty *tp, int brk)
959 * now it sends 400 millisecond break because I don't know
960 * how to send an infinite break
963 fepcmd_w(port, SENDBREAK, 400, 10);
967 digiparam(struct tty *tp, struct termios *t)
969 struct digi_softc *sc;
979 DLOG(DIGIDB_SET, (sc->dev, "port%d: setting parameters\n", port->pnum));
981 if (t->c_ispeed == 0)
982 t->c_ispeed = t->c_ospeed;
984 cflag = ttspeedtab(t->c_ospeed, digispeedtab);
986 if (cflag < 0 || (cflag > 0 && t->c_ispeed != t->c_ospeed))
994 if (cflag == 0) { /* hangup */
995 DLOG(DIGIDB_SET, (sc->dev, "port%d: hangup\n", port->pnum));
996 digimodem(port->tp, 0, SER_DTR | SER_RTS);
998 digimodem(port->tp, SER_DTR | SER_RTS, 0);
1000 DLOG(DIGIDB_SET, (sc->dev, "port%d: CBAUD = %d\n", port->pnum,
1004 /* convert flags to sysV-style values */
1005 if (t->c_cflag & PARODD)
1007 if (t->c_cflag & PARENB)
1009 if (t->c_cflag & CSTOPB)
1012 /* convert flags to sysV-style values */
1013 if (t->c_cflag & PARODD)
1014 cflag |= FEP_PARODD;
1015 if (t->c_cflag & PARENB)
1016 cflag |= FEP_PARENB;
1017 if (t->c_cflag & CSTOPB)
1018 cflag |= FEP_CSTOPB;
1019 if (t->c_cflag & CLOCAL)
1020 cflag |= FEP_CLOCAL;
1023 cflag |= (t->c_cflag & CSIZE) >> 4;
1024 DLOG(DIGIDB_SET, (sc->dev, "port%d: CFLAG = 0x%x\n", port->pnum,
1026 fepcmd_w(port, SETCFLAGS, (unsigned)cflag, 0);
1030 t->c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP);
1031 if (port->c_iflag & IXON)
1033 if (port->c_iflag & IXANY)
1035 if (port->c_iflag & IXOFF)
1038 DLOG(DIGIDB_SET, (sc->dev, "port%d: set iflag = 0x%x\n", port->pnum, iflag));
1039 fepcmd_w(port, SETIFLAGS, (unsigned)iflag, 0);
1042 if (t->c_cflag & CDTR_IFLOW)
1043 hflow |= sc->csigs->dtr;
1044 if (t->c_cflag & CRTS_IFLOW)
1045 hflow |= sc->csigs->rts;
1046 if (t->c_cflag & CCTS_OFLOW)
1047 hflow |= sc->csigs->cts;
1048 if (t->c_cflag & CDSR_OFLOW)
1050 if (t->c_cflag & CCAR_OFLOW)
1053 DLOG(DIGIDB_SET, (sc->dev, "port%d: set hflow = 0x%x\n", port->pnum, hflow));
1054 fepcmd_w(port, SETHFLOW, 0xff00 | (unsigned)hflow, 0);
1056 DLOG(DIGIDB_SET, (sc->dev, "port%d: set startc(0x%x), stopc(0x%x)\n",
1057 port->pnum, t->c_cc[VSTART], t->c_cc[VSTOP]));
1058 fepcmd_b(port, SONOFFC, t->c_cc[VSTART], t->c_cc[VSTOP], 0);
1060 if (sc->window != 0)
1063 sc->towin(sc, window);
1072 struct digi_p *port;
1074 struct digi_softc *sc;
1076 volatile struct board_chan *bc;
1090 if (sc->status != DIGI_STATUS_ENABLED) {
1091 DLOG(DIGIDB_IRQ, (sc->dev, "interrupt on disabled board !\n"));
1095 #ifdef DIGI_INTERRUPT
1096 microtime(&sc->intr_timestamp);
1099 window = sc->window;
1102 if (sc->model >= PCXEM && W(sc->vmem + 0xd00)) {
1103 struct con_bios *con = con_bios_list;
1104 register u_char *ptr;
1106 ptr = sc->vmem + W(sc->vmem + 0xd00);
1108 if (ptr[1] && W(ptr + 2) == W(con->bios + 2))
1109 /* Not first block -- exact match */
1112 if (W(ptr + 4) >= W(con->bios + 4) &&
1113 W(ptr + 4) <= W(con->bios + 6))
1114 /* Initial search concetrator BIOS */
1119 log(LOG_ERR, "digi%d: wanted bios LREV = 0x%04x"
1120 " not found!\n", sc->res.unit, W(ptr + 4));
1122 W(sc->vmem + 0xd00) = 0;
1126 W(ptr + 4) = W(cxcon + 4);
1127 W(ptr + 6) = W(cxcon + 6);
1129 W(ptr + 2) = W(cxcon + 2);
1130 W(ptr + 8) = (ptr[1] << 6) + W(cxcon + 8);
1131 size = W(cxcon + 10) - (ptr[1] << 10);
1133 W(ptr + 8) = W(cxcon + 8);
1139 bcopy(cxcon + (ptr[1] << 10), ptr + 12, size);
1141 W(sc->vmem + 0xd00) = 0;
1145 ehead = sc->gdata->ein;
1146 etail = sc->gdata->eout;
1147 if (ehead == etail) {
1150 if (sc->intr_count % 6000 == 0) {
1151 DLOG(DIGIDB_IRQ, (sc->dev,
1152 "6000 useless polls %x %x\n", ehead, etail));
1158 while (ehead != etail) {
1159 event = *(volatile struct event *)(sc->memevent + etail);
1161 etail = (etail + 4) & sc->gdata->imax;
1163 if (event.pnum >= sc->numports) {
1164 log(LOG_ERR, "digi%d: port %d: got event"
1165 " on nonexisting port\n", sc->res.unit,
1169 port = &sc->ports[event.pnum];
1173 if (!(tp->t_state & TS_ISOPEN) && !tp->t_wopeners) {
1174 DLOG(DIGIDB_IRQ, (sc->dev,
1175 "port %d: event 0x%x on closed port\n",
1176 event.pnum, event.event));
1184 if (event.event & ~ALL_IND)
1185 log(LOG_ERR, "digi%d: port%d: ? event 0x%x mstat 0x%x"
1186 " lstat 0x%x\n", sc->res.unit, event.pnum,
1187 event.event, event.mstat, event.lstat);
1189 if (event.event & DATA_IND) {
1190 DLOG(DIGIDB_IRQ, (sc->dev, "port %d: DATA_IND\n",
1192 wrapmask = port->rxbufsize - 1;
1197 if (!(tp->t_state & TS_ISOPEN)) {
1201 while (head != tail) {
1204 DLOG(DIGIDB_INT, (sc->dev,
1205 "port %d: p rx head = %d tail = %d\n",
1206 event.pnum, head, tail));
1207 top = (head > tail) ? head : wrapmask + 1;
1208 sc->towin(sc, port->rxwin);
1210 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1211 size = b_to_q((char *)port->rxbuf +
1212 tail, size, &tp->t_rawq);
1215 } else for (; tail < top;) {
1216 ttyld_rint(tp, port->rxbuf[tail]);
1217 sc->towin(sc, port->rxwin);
1220 if (tp->t_state & TS_TBLOCK)
1232 CE_RECORD(port, CE_OVERRUN);
1233 log(LOG_ERR, "digi%d: port%d: %s\n",
1234 sc->res.unit, event.pnum,
1235 digi_errortxt(CE_OVERRUN));
1240 tp->t_state |= TS_TBLOCK;
1241 port->status |= PAUSE_RX;
1242 DLOG(DIGIDB_RX, (sc->dev, "port %d: pause RX\n",
1249 if (event.event & MODEMCHG_IND) {
1250 DLOG(DIGIDB_MODEM, (sc->dev, "port %d: MODEMCHG_IND\n",
1253 if ((event.mstat ^ event.lstat) & port->cd) {
1255 ttyld_modem(tp, event.mstat & port->cd);
1257 wakeup(TSA_CARR_ON(tp));
1260 if (event.mstat & sc->csigs->ri) {
1261 DLOG(DIGIDB_RI, (sc->dev, "port %d: RING\n",
1263 if (port->send_ring) {
1264 ttyld_rint(tp, 'R');
1265 ttyld_rint(tp, 'I');
1266 ttyld_rint(tp, 'N');
1267 ttyld_rint(tp, 'G');
1268 ttyld_rint(tp, '\r');
1269 ttyld_rint(tp, '\n');
1273 if (event.event & BREAK_IND) {
1274 DLOG(DIGIDB_MODEM, (sc->dev, "port %d: BREAK_IND\n",
1276 ttyld_rint(tp, TTY_BI);
1278 if (event.event & (LOWTX_IND | EMPTYTX_IND)) {
1279 DLOG(DIGIDB_IRQ, (sc->dev, "port %d:%s%s\n",
1281 event.event & LOWTX_IND ? " LOWTX" : "",
1282 event.event & EMPTYTX_IND ? " EMPTYTX" : ""));
1286 sc->gdata->eout = etail;
1288 if (sc->window != 0)
1291 sc->towin(sc, window);
1295 digistart(struct tty *tp)
1297 struct digi_p *port;
1298 struct digi_softc *sc;
1299 volatile struct board_chan *bc;
1301 int size, ocount, totcnt = 0;
1309 wmask = port->txbufsize - 1;
1312 port->lcc = tp->t_outq.c_cc;
1314 if (!(tp->t_state & TS_TBLOCK)) {
1315 if (port->status & PAUSE_RX) {
1316 DLOG(DIGIDB_RX, (sc->dev, "port %d: resume RX\n",
1319 * CAREFUL - braces are needed here if the DLOG is
1323 port->status &= ~PAUSE_RX;
1326 if (!(tp->t_state & TS_TTSTOP) && port->status & PAUSE_TX) {
1327 DLOG(DIGIDB_TX, (sc->dev, "port %d: resume TX\n", port->pnum));
1328 port->status &= ~PAUSE_TX;
1329 fepcmd_w(port, RESUMETX, 0, 10);
1331 if (tp->t_outq.c_cc == 0)
1332 tp->t_state &= ~TS_BUSY;
1334 tp->t_state |= TS_BUSY;
1337 while (tp->t_outq.c_cc != 0) {
1339 DLOG(DIGIDB_INT, (sc->dev, "port%d: s tx head = %d tail = %d\n",
1340 port->pnum, head, tail));
1343 size = tail - head - 1;
1345 size = port->txbufsize - head;
1352 sc->towin(sc, port->txwin);
1353 ocount = q_to_b(&tp->t_outq, port->txbuf + head, size);
1362 port->lostcc = tp->t_outq.c_cc;
1365 size = port->txbufsize - tail + head;
1370 DLOG(DIGIDB_INT, (sc->dev, "port%d: s total cnt = %d\n", port->pnum, totcnt));
1376 digistop(struct tty *tp, int rw)
1378 struct digi_softc *sc;
1379 struct digi_p *port;
1384 DLOG(DIGIDB_TX, (sc->dev, "port %d: pause TX\n", port->pnum));
1385 port->status |= PAUSE_TX;
1386 fepcmd_w(port, PAUSETX, 0, 10);
1390 fepcmd(struct digi_p *port, int cmd, int op1, int ncmds)
1393 unsigned tail, head;
1396 mem = port->sc->memcmd;
1398 port->sc->setwin(port->sc, 0);
1400 head = port->sc->gdata->cin;
1401 mem[head + 0] = cmd;
1402 mem[head + 1] = port->pnum;
1403 *(u_short *)(mem + head + 2) = op1;
1405 head = (head + 4) & port->sc->gdata->cmax;
1406 port->sc->gdata->cin = head;
1408 for (count = FEPTIMEOUT; count > 0; count--) {
1409 head = port->sc->gdata->cin;
1410 tail = port->sc->gdata->cout;
1411 n = (head - tail) & port->sc->gdata->cmax;
1413 if (n <= ncmds * sizeof(short) * 4)
1417 log(LOG_ERR, "digi%d: port%d: timeout on FEP command\n",
1418 port->sc->res.unit, port->pnum);
1422 digi_errortxt(int id)
1424 static const char *error_desc[] = {
1426 "interrupt-level buffer overflow",
1427 "tty-level buffer overflow",
1430 KASSERT(id >= 0 && id < sizeof(error_desc) / sizeof(error_desc[0]),
1431 ("Unexpected digi error id %d\n", id));
1433 return (error_desc[id]);
1437 digi_attach(struct digi_softc *sc)
1439 sc->res.ctldev = make_dev(&digi_csw,
1440 sc->res.unit << 16, UID_ROOT, GID_WHEEL,
1441 0600, "digi%r.ctl", sc->res.unit);
1442 sc->res.ctldev->si_drv1 = sc;
1444 digi_loadmoduledata(sc);
1446 digi_freemoduledata(sc);
1452 digi_inuse(struct digi_softc *sc)
1455 struct digi_p *port;
1457 port = &sc->ports[0];
1458 for (i = 0; i < sc->numports; i++, port++)
1459 if (port->tp->t_state & TS_ISOPEN) {
1460 DLOG(DIGIDB_INIT, (sc->dev, "port%d: busy\n", i));
1462 } else if (port->tp->t_wopeners || port->opencnt) {
1463 DLOG(DIGIDB_INIT, (sc->dev, "port%d: blocked in open\n",
1471 digi_free_state(struct digi_softc *sc)
1475 /* Blow it all away */
1477 for (i = 0; i < sc->numports; i++)
1478 ttygone(sc->ports[i].tp);
1480 /* XXX: this might be better done as a ttypurge method */
1481 untimeout(digi_poll, sc, sc->callout);
1482 callout_handle_init(&sc->callout);
1483 untimeout(digi_int_test, sc, sc->inttest);
1484 callout_handle_init(&sc->inttest);
1486 for (i = 0; i < sc->numports; i++)
1487 ttyfree(sc->ports[i].tp);
1489 bus_teardown_intr(sc->dev, sc->res.irq, sc->res.irqHandler);
1490 #ifdef DIGI_INTERRUPT
1491 if (sc->res.irq != NULL) {
1492 bus_release_resource(dev, SYS_RES_IRQ, sc->res.irqrid,
1498 KASSERT(sc->ports, ("digi%d: Lost my ports ?", sc->res.unit));
1499 free(sc->ports, M_TTYS);
1504 sc->status = DIGI_STATUS_NOTINIT;
1508 digi_detach(device_t dev)
1510 struct digi_softc *sc = device_get_softc(dev);
1512 DLOG(DIGIDB_INIT, (sc->dev, "detaching\n"));
1514 /* If we're INIT'd, numports must be 0 */
1515 KASSERT(sc->numports == 0 || sc->status != DIGI_STATUS_NOTINIT,
1516 ("digi%d: numports(%d) & status(%d) are out of sync",
1517 sc->res.unit, sc->numports, (int)sc->status));
1522 digi_free_state(sc);
1524 destroy_dev(sc->res.ctldev);
1526 if (sc->res.mem != NULL) {
1527 bus_release_resource(dev, SYS_RES_MEMORY, sc->res.mrid,
1531 if (sc->res.io != NULL) {
1532 bus_release_resource(dev, SYS_RES_IOPORT, sc->res.iorid,
1541 digi_shutdown(device_t dev)
1546 MODULE_VERSION(digi, 1);