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 static speed_t digidefaultrate = TTYDEF_SPEED;
92 struct con_bios *next;
97 static struct con_bios *con_bios_list;
98 devclass_t digi_devclass;
99 static char driver_name[] = "digi";
100 unsigned digi_debug = 0;
102 static struct speedtab digispeedtab[] = {
103 { 0, 0}, /* old (sysV-like) Bx codes */
119 { 57600, (02000 | 1)},
120 { 76800, (02000 | 2)},
121 { 115200, (02000 | 3)},
122 { 230400, (02000 | 6)},
126 const struct digi_control_signals digi_xixe_signals = {
127 0x02, 0x08, 0x10, 0x20, 0x40, 0x80
130 const struct digi_control_signals digi_normal_signals = {
131 0x02, 0x80, 0x20, 0x10, 0x40, 0x01
134 static struct cdevsw digi_csw = {
135 .d_version = D_VERSION,
137 .d_close = digicclose,
138 .d_ioctl = digicioctl,
139 .d_name = driver_name,
140 .d_flags = D_TTY | D_NEEDGIANT,
146 struct digi_softc *sc;
148 sc = (struct digi_softc *)ptr;
149 callout_handle_init(&sc->callout);
151 sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
155 digi_int_test(void *v)
157 struct digi_softc *sc = v;
159 callout_handle_init(&sc->inttest);
160 #ifdef DIGI_INTERRUPT
161 if (sc->intr_timestamp.tv_sec || sc->intr_timestamp.tv_usec) {
165 log(LOG_ERR, "digi%d: Interrupt didn't work, use polled mode\n", unit);
167 sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
171 digi_freemoduledata(struct digi_softc *sc)
173 if (sc->fep.data != NULL) {
174 free(sc->fep.data, M_TTYS);
177 if (sc->link.data != NULL) {
178 free(sc->link.data, M_TTYS);
179 sc->link.data = NULL;
181 if (sc->bios.data != NULL) {
182 free(sc->bios.data, M_TTYS);
183 sc->bios.data = NULL;
188 digi_bcopy(const void *vfrom, void *vto, size_t sz)
190 volatile const char *from = (volatile const char *)vfrom;
191 volatile char *to = (volatile char *)vto;
194 for (i = 0; i < sz; i++)
197 from = (const volatile char *)vfrom;
198 to = (volatile char *)vto;
199 for (i = 0; i < sz; i++)
200 if (*to++ != *from++)
206 digi_delay(struct digi_softc *sc, const char *txt, u_long timo)
209 DELAY(timo * 1000000 / hz);
211 tsleep(sc, PUSER | PCATCH, txt, timo);
215 digi_init(struct digi_softc *sc)
221 volatile struct board_chan *bc;
226 if (sc->status == DIGI_STATUS_DISABLED) {
227 log(LOG_ERR, "digi%d: Cannot init a disabled card\n",
231 if (sc->bios.data == NULL) {
232 log(LOG_ERR, "digi%d: Cannot init without BIOS\n",
237 if (sc->link.data == NULL && sc->model >= PCCX) {
238 log(LOG_ERR, "digi%d: Cannot init without link info\n",
243 if (sc->fep.data == NULL) {
244 log(LOG_ERR, "digi%d: Cannot init without fep code\n",
248 sc->status = DIGI_STATUS_NOTINIT;
252 * We're re-initialising - maybe because someone's attached
253 * another port module. For now, we just re-initialise
262 ptr = sc->setwin(sc, MISCGLOBAL);
263 for (i = 0; i < 16; i += 2)
268 outb(sc->wport, 0xff); /* window 7 */
269 ptr = sc->vmem + (BIOSCODE & 0x1fff);
271 if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
272 device_printf(sc->dev, "BIOS upload failed\n");
276 outb(sc->port, FEPCLR);
282 ptr = sc->setwin(sc, BIOSCODE + ((0xf000 - sc->mem_seg) << 4));
283 if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
284 device_printf(sc->dev, "BIOS upload failed\n");
295 outb(sc->port, FEPRST | FEPMEM);
297 for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) &
298 FEPMASK) != FEPRST; i++) {
300 log(LOG_ERR, "digi%d: %s init reset failed\n",
301 sc->res.unit, sc->name);
304 digi_delay(sc, "digiinit0", 5);
306 DLOG(DIGIDB_INIT, (sc->dev, "Got init reset after %d us\n", i));
308 /* Now upload the BIOS */
309 cnt = (sc->bios.size < sc->win_size - BIOSOFFSET) ?
310 sc->bios.size : sc->win_size - BIOSOFFSET;
312 ptr = sc->setwin(sc, BIOSOFFSET);
313 if (!digi_bcopy(sc->bios.data, ptr, cnt)) {
314 device_printf(sc->dev, "BIOS upload (1) failed\n");
318 if (cnt != sc->bios.size) {
319 /* and the second part */
320 ptr = sc->setwin(sc, sc->win_size);
321 if (!digi_bcopy(sc->bios.data + cnt, ptr,
322 sc->bios.size - cnt)) {
323 device_printf(sc->dev, "BIOS upload failed\n");
328 ptr = sc->setwin(sc, 0);
329 vW(ptr + 0) = 0x0401;
330 vW(ptr + 2) = 0x0bf0;
331 vW(ptr + 4) = 0x0000;
332 vW(ptr + 6) = 0x0000;
337 DLOG(DIGIDB_INIT, (sc->dev, "BIOS uploaded\n"));
339 ptr = sc->setwin(sc, MISCGLOBAL);
345 } else if (sc->model == PCXEVE) {
346 outb(sc->port, FEPCLR);
349 outb(sc->port, FEPCLR | FEPMEM);
350 resp = FEPRST | FEPMEM;
353 for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) & FEPMASK)
356 log(LOG_ERR, "digi%d: BIOS start failed\n",
360 digi_delay(sc, "digibios0", 5);
363 DLOG(DIGIDB_INIT, (sc->dev, "BIOS started after %d us\n", i));
365 for (i = 0; vW(ptr) != *(u_short *)"GD"; i++) {
367 log(LOG_ERR, "digi%d: BIOS boot failed "
368 "(0x%02x != 0x%02x)\n",
369 sc->res.unit, vW(ptr), *(u_short *)"GD");
372 digi_delay(sc, "digibios1", 5);
375 DLOG(DIGIDB_INIT, (sc->dev, "BIOS booted after %d iterations\n", i));
377 if (sc->link.data != NULL) {
378 DLOG(DIGIDB_INIT, (sc->dev, "Loading link data\n"));
379 ptr = sc->setwin(sc, 0xcd0);
380 digi_bcopy(sc->link.data, ptr, 21); /* XXX 21 ? */
389 ptr = sc->setwin(sc, sc->model == PCXI ? 0x2000 : 0x0);
390 digi_bcopy(sc->fep.data, ptr, sc->fep.size);
392 /* A BIOS request to move our data to 0x2000 */
393 ptr = sc->setwin(sc, MBOX);
395 vW(ptr + 2) = sc->mem_seg + FEPCODESEG;
397 vW(ptr + 6) = FEPCODESEG;
399 vW(ptr + 10) = sc->fep.size;
401 /* Run the BIOS request */
402 outb(sc->port, FEPREQ | FEPMEM);
403 outb(sc->port, FEPCLR | FEPMEM);
405 for (i = 0; W(ptr); i++) {
407 log(LOG_ERR, "digi%d: FEP/OS move failed\n",
412 digi_delay(sc, "digifep0", 5);
415 (sc->dev, "FEP/OS moved after %d iterations\n", i));
417 /* Clear the confirm word */
418 ptr = sc->setwin(sc, FEPSTAT);
421 /* A BIOS request to execute the FEP/OS */
422 ptr = sc->setwin(sc, MBOX);
424 vW(ptr + 2) = FEPCODESEG;
427 /* Run the BIOS request */
428 outb(sc->port, FEPREQ);
429 outb(sc->port, FEPCLR);
431 ptr = sc->setwin(sc, FEPSTAT);
438 DLOG(DIGIDB_INIT, (sc->dev, "Loading FEP/OS\n"));
440 cnt = (sc->fep.size < sc->win_size - BIOSOFFSET) ?
441 sc->fep.size : sc->win_size - BIOSOFFSET;
443 ptr = sc->setwin(sc, BIOSOFFSET);
444 digi_bcopy(sc->fep.data, ptr, cnt);
446 if (cnt != sc->fep.size) {
447 ptr = sc->setwin(sc, BIOSOFFSET + cnt);
448 digi_bcopy(sc->fep.data + cnt, ptr,
452 DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS loaded\n"));
454 ptr = sc->setwin(sc, 0xc30);
460 /* Clear the confirm word */
461 ptr = sc->setwin(sc, FEPSTAT);
465 outb(sc->port, 0); /* XXX necessary ? */
470 ptr = sc->setwin(sc, 0xd000);
471 digi_bcopy(sc->fep.data, ptr, sc->fep.size);
473 /* A BIOS request to execute the FEP/OS */
474 ptr = sc->setwin(sc, 0xc40);
476 W(ptr + 2) = FEPCODE >> 4;
479 /* Clear the confirm word */
480 ptr = sc->setwin(sc, FEPSTAT);
483 /* Run the BIOS request */
484 outb(sc->port, FEPREQ | FEPMEM); /* send interrupt to BIOS */
485 outb(sc->port, FEPCLR | FEPMEM);
489 /* Now wait 'till the FEP/OS has booted */
490 for (i = 0; vW(ptr) != *(u_short *)"OS"; i++) {
492 log(LOG_ERR, "digi%d: FEP/OS start failed "
493 "(0x%02x != 0x%02x)\n",
494 sc->res.unit, vW(ptr), *(u_short *)"OS");
498 digi_delay(sc, "digifep1", 5);
501 DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS started after %d iterations\n", i));
503 if (sc->model >= PCXEM) {
504 ptr = sc->setwin(sc, 0xe04);
506 ptr = sc->setwin(sc, 0xc02);
507 sc->numports = vW(ptr);
509 ptr = sc->setwin(sc, 0xc22);
510 sc->numports = vW(ptr);
513 if (sc->numports == 0) {
514 device_printf(sc->dev, "%s, 0 ports found\n", sc->name);
519 device_printf(sc->dev, "%s, %d ports found\n", sc->name, sc->numports);
522 free(sc->ports, M_TTYS);
523 sc->ports = malloc(sizeof(struct digi_p) * sc->numports,
524 M_TTYS, M_WAITOK | M_ZERO);
527 * XXX Should read port 0xc90 for an array of 2byte values, 1 per
528 * port. If the value is 0, the port is broken....
531 ptr = sc->setwin(sc, 0);
533 /* We should now init per-port structures */
534 bc = (volatile struct board_chan *)(ptr + CHANSTRUCT);
535 sc->gdata = (volatile struct global_data *)(ptr + FEP_GLOBAL);
537 sc->memcmd = ptr + sc->gdata->cstart;
538 sc->memevent = ptr + sc->gdata->istart;
540 for (i = 0; i < sc->numports; i++, bc++) {
541 port = sc->ports + i;
544 port->status = ENABLED;
546 tp = port->tp = ttyalloc();
547 tp->t_oproc = digistart;
548 tp->t_param = digiparam;
549 tp->t_modem = digimodem;
550 tp->t_break = digibreak;
551 tp->t_stop = digistop;
552 tp->t_cioctl = digisioctl;
553 tp->t_ioctl = digiioctl;
554 tp->t_open = digiopen;
555 tp->t_close = digiclose;
558 if (sc->model == PCXEVE) {
560 (((bc->tseg - sc->mem_seg) << 4) & 0x1fff);
562 (((bc->rseg - sc->mem_seg) << 4) & 0x1fff);
563 port->txwin = FEPWIN | ((bc->tseg - sc->mem_seg) >> 9);
564 port->rxwin = FEPWIN | ((bc->rseg - sc->mem_seg) >> 9);
565 } else if (sc->model == PCXI || sc->model == PCXE) {
566 port->txbuf = ptr + ((bc->tseg - sc->mem_seg) << 4);
567 port->rxbuf = ptr + ((bc->rseg - sc->mem_seg) << 4);
568 port->txwin = port->rxwin = 0;
571 (((bc->tseg - sc->mem_seg) << 4) % sc->win_size);
573 (((bc->rseg - sc->mem_seg) << 4) % sc->win_size);
574 port->txwin = FEPWIN |
575 (((bc->tseg - sc->mem_seg) << 4) / sc->win_size);
576 port->rxwin = FEPWIN |
577 (((bc->rseg - sc->mem_seg) << 4) / sc->win_size);
579 port->txbufsize = bc->tmax + 1;
580 port->rxbufsize = bc->rmax + 1;
582 lowwater = port->txbufsize >> 2;
586 fepcmd_w(port, STXLWATER, lowwater, 10);
587 fepcmd_w(port, SRXLWATER, port->rxbufsize >> 2, 10);
588 fepcmd_w(port, SRXHWATER, (3 * port->rxbufsize) >> 2, 10);
593 * We don't use all the flags from <sys/ttydefaults.h> since
594 * they are only relevant for logins. It's important to have
595 * echo off initially so that the line doesn't start blathering
596 * before the echo flag can be turned off.
598 tp->t_init_in.c_iflag = 0;
599 tp->t_init_in.c_oflag = 0;
600 tp->t_init_in.c_cflag = TTYDEF_CFLAG;
601 tp->t_init_in.c_lflag = 0;
602 termioschars(&tp->t_init_in);
603 tp->t_init_in.c_ispeed = tp->t_init_in.c_ospeed = digidefaultrate;
604 tp->t_init_out = tp->t_init_in;
605 port->send_ring = 1; /* Default action on signal RI */
606 ttycreate(tp, NULL, 0, MINOR_CALLOUT, "D%r%r", sc->res.unit, i);
610 sc->inttest = timeout(digi_int_test, sc, hz);
611 /* fepcmd_w(&sc->ports[0], 0xff, 0, 0); */
612 sc->status = DIGI_STATUS_ENABLED;
618 digimodem(struct tty *tp, int sigon, int sigoff)
620 struct digi_softc *sc;
622 int bitand, bitor, mstat;
627 if (sigon == 0 && sigoff == 0) {
628 port->sc->setwin(port->sc, 0);
629 mstat = port->bc->mstat;
630 port->sc->hidewin(port->sc);
631 if (mstat & port->sc->csigs->rts)
633 if (mstat & port->cd)
635 if (mstat & port->dsr)
637 if (mstat & port->sc->csigs->cts)
639 if (mstat & port->sc->csigs->ri)
641 if (mstat & port->sc->csigs->dtr)
649 if (sigoff & SER_DTR)
650 bitand |= port->sc->csigs->dtr;
651 if (sigoff & SER_RTS)
652 bitand |= port->sc->csigs->rts;
654 bitor |= port->sc->csigs->dtr;
656 bitor |= port->sc->csigs->rts;
657 fepcmd_b(port, SETMODEM, bitor, ~bitand, 0);
662 digicopen(struct cdev *dev, int flag, int mode, struct thread *td)
664 struct digi_softc *sc;
667 if (sc->status != DIGI_STATUS_ENABLED) {
668 DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
676 digiopen(struct tty *tp, struct cdev *dev)
679 struct digi_softc *sc;
681 volatile struct board_chan *bc;
686 if (sc->status != DIGI_STATUS_ENABLED) {
687 DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
693 * The device isn't open, so there are no conflicts.
694 * Initialize it. Initialization is done twice in many
695 * cases: to preempt sleeping callin opens if we are callout,
696 * and to complete a callin open after DCD rises.
700 bc->rout = bc->rin; /* clear input queue */
704 bc->mint = port->cd | port->sc->csigs->ri;
707 port->cd = sc->csigs->dsr;
708 port->dsr = sc->csigs->cd;
710 port->cd = sc->csigs->cd;
711 port->dsr = sc->csigs->dsr;
713 tp->t_wopeners++; /* XXX required ? */
714 error = digiparam(tp, &tp->t_termios);
721 digicclose(struct cdev *dev, int flag, int mode, struct thread *td)
723 struct digi_softc *sc;
731 digidtrwakeup(void *chan)
733 struct digi_p *port = chan;
735 port->status &= ~DIGI_DTR_OFF;
736 wakeup(&port->tp->t_dtr_wait);
737 port->tp->t_wopeners--;
741 digiclose(struct tty *tp)
743 volatile struct board_chan *bc;
751 port->sc->setwin(port->sc, 0);
756 if ((tp->t_cflag & HUPCL) ||
757 (!tp->t_actout && !(bc->mstat & port->cd) &&
758 !(tp->t_init_in.c_cflag & CLOCAL)) ||
759 !(tp->t_state & TS_ISOPEN)) {
760 digimodem(tp, 0, SER_DTR | SER_RTS);
761 if (tp->t_dtr_wait != 0) {
762 /* Schedule a wakeup of any callin devices */
764 timeout(&digidtrwakeup, port, tp->t_dtr_wait);
765 port->status |= DIGI_DTR_OFF;
768 tp->t_actout = FALSE;
769 wakeup(&tp->t_actout);
770 wakeup(TSA_CARR_ON(tp));
775 * Load module "digi_<mod>.ko" and look for a symbol called digi_mod_<mod>.
777 * Populate sc->bios, sc->fep, and sc->link from this data.
779 * sc->fep.data, sc->bios.data and sc->link.data are malloc()d according
780 * to their respective sizes.
782 * The module is unloaded when we're done.
785 digi_loadmoduledata(struct digi_softc *sc)
787 struct digi_mod *digi_mod;
793 KASSERT(sc->bios.data == NULL, ("Uninitialised BIOS variable"));
794 KASSERT(sc->fep.data == NULL, ("Uninitialised FEP variable"));
795 KASSERT(sc->link.data == NULL, ("Uninitialised LINK variable"));
796 KASSERT(sc->module != NULL, ("Uninitialised module name"));
798 modlen = strlen(sc->module);
799 modfile = malloc(modlen + 6, M_TEMP, M_WAITOK);
800 snprintf(modfile, modlen + 6, "digi_%s", sc->module);
801 res = linker_reference_module(modfile, NULL, &lf);
802 if ((res = linker_reference_module(modfile, NULL, &lf)) != 0)
803 printf("%s: Failed %d to autoload module\n", modfile, res);
804 free(modfile, M_TEMP);
808 sym = malloc(modlen + 10, M_TEMP, M_WAITOK);
809 snprintf(sym, modlen + 10, "digi_mod_%s", sc->module);
810 if ((symptr = linker_file_lookup_symbol(lf, sym, 0)) == NULL)
811 printf("digi_%s.ko: Symbol `%s' not found\n", sc->module, sym);
814 digi_mod = (struct digi_mod *)symptr;
815 if (digi_mod->dm_version != DIGI_MOD_VERSION) {
816 printf("digi_%s.ko: Invalid version %d (need %d)\n",
817 sc->module, digi_mod->dm_version, DIGI_MOD_VERSION);
818 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
822 sc->bios.size = digi_mod->dm_bios.size;
823 if (sc->bios.size != 0 && digi_mod->dm_bios.data != NULL) {
824 sc->bios.data = malloc(sc->bios.size, M_TTYS, M_WAITOK);
825 bcopy(digi_mod->dm_bios.data, sc->bios.data, sc->bios.size);
828 sc->fep.size = digi_mod->dm_fep.size;
829 if (sc->fep.size != 0 && digi_mod->dm_fep.data != NULL) {
830 sc->fep.data = malloc(sc->fep.size, M_TTYS, M_WAITOK);
831 bcopy(digi_mod->dm_fep.data, sc->fep.data, sc->fep.size);
834 sc->link.size = digi_mod->dm_link.size;
835 if (sc->link.size != 0 && digi_mod->dm_link.data != NULL) {
836 sc->link.data = malloc(sc->link.size, M_TTYS, M_WAITOK);
837 bcopy(digi_mod->dm_link.data, sc->link.data, sc->link.size);
840 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
846 digisioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
849 struct digi_softc *sc;
855 case DIGIIO_GETALTPIN:
857 *(int *)data = port->ialtpin;
858 else if (ISLOCK(dev))
859 *(int *)data = port->laltpin;
863 case DIGIIO_SETALTPIN:
865 if (!port->laltpin) {
866 port->ialtpin = !!*(int *)data;
867 DLOG(DIGIDB_SET, (sc->dev,
868 "port%d: initial ALTPIN %s\n", port->pnum,
869 port->ialtpin ? "set" : "cleared"));
871 } else if (ISLOCK(dev)) {
872 port->laltpin = !!*(int *)data;
873 DLOG(DIGIDB_SET, (sc->dev,
874 "port%d: ALTPIN %slocked\n",
875 port->pnum, port->laltpin ? "" : "un"));
886 digicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
889 struct digi_softc *sc;
893 if (sc->status == DIGI_STATUS_DISABLED)
899 digi_debug = *(int *)data;
902 device_printf(sc->dev, "DEBUG not defined\n");
906 digi_loadmoduledata(sc);
907 error = digi_init(sc);
908 digi_freemoduledata(sc);
912 *(enum digi_model *)data = sc->model;
916 return (copyout(sc->name, *(char **)data,
917 strlen(sc->name) + 1));
924 digiioctl(struct tty *tp, u_long cmd, void *data, int flag, struct thread *td)
926 struct digi_softc *sc;
931 if (sc->status == DIGI_STATUS_DISABLED)
934 if (!(port->status & ENABLED))
938 case DIGIIO_GETALTPIN:
939 *(int *)data = !!(port->dsr == sc->csigs->cd);
942 case DIGIIO_SETALTPIN:
943 if (!port->laltpin) {
945 DLOG(DIGIDB_SET, (sc->dev,
946 "port%d: ALTPIN set\n", port->pnum));
947 port->cd = sc->csigs->dsr;
948 port->dsr = sc->csigs->cd;
950 DLOG(DIGIDB_SET, (sc->dev,
951 "port%d: ALTPIN cleared\n", port->pnum));
952 port->cd = sc->csigs->cd;
953 port->dsr = sc->csigs->dsr;
958 port->send_ring = *(u_char *)data;
967 digibreak(struct tty *tp, int brk)
974 * now it sends 400 millisecond break because I don't know
975 * how to send an infinite break
978 fepcmd_w(port, SENDBREAK, 400, 10);
982 digiparam(struct tty *tp, struct termios *t)
984 struct digi_softc *sc;
994 DLOG(DIGIDB_SET, (sc->dev, "port%d: setting parameters\n", port->pnum));
996 if (t->c_ispeed == 0)
997 t->c_ispeed = t->c_ospeed;
999 cflag = ttspeedtab(t->c_ospeed, digispeedtab);
1001 if (cflag < 0 || (cflag > 0 && t->c_ispeed != t->c_ospeed))
1006 window = sc->window;
1009 if (cflag == 0) { /* hangup */
1010 DLOG(DIGIDB_SET, (sc->dev, "port%d: hangup\n", port->pnum));
1011 digimodem(port->tp, 0, SER_DTR | SER_RTS);
1013 digimodem(port->tp, SER_DTR | SER_RTS, 0);
1015 DLOG(DIGIDB_SET, (sc->dev, "port%d: CBAUD = %d\n", port->pnum,
1019 /* convert flags to sysV-style values */
1020 if (t->c_cflag & PARODD)
1022 if (t->c_cflag & PARENB)
1024 if (t->c_cflag & CSTOPB)
1027 /* convert flags to sysV-style values */
1028 if (t->c_cflag & PARODD)
1029 cflag |= FEP_PARODD;
1030 if (t->c_cflag & PARENB)
1031 cflag |= FEP_PARENB;
1032 if (t->c_cflag & CSTOPB)
1033 cflag |= FEP_CSTOPB;
1034 if (t->c_cflag & CLOCAL)
1035 cflag |= FEP_CLOCAL;
1038 cflag |= (t->c_cflag & CSIZE) >> 4;
1039 DLOG(DIGIDB_SET, (sc->dev, "port%d: CFLAG = 0x%x\n", port->pnum,
1041 fepcmd_w(port, SETCFLAGS, (unsigned)cflag, 0);
1045 t->c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP);
1046 if (port->c_iflag & IXON)
1048 if (port->c_iflag & IXANY)
1050 if (port->c_iflag & IXOFF)
1053 DLOG(DIGIDB_SET, (sc->dev, "port%d: set iflag = 0x%x\n", port->pnum, iflag));
1054 fepcmd_w(port, SETIFLAGS, (unsigned)iflag, 0);
1057 if (t->c_cflag & CDTR_IFLOW)
1058 hflow |= sc->csigs->dtr;
1059 if (t->c_cflag & CRTS_IFLOW)
1060 hflow |= sc->csigs->rts;
1061 if (t->c_cflag & CCTS_OFLOW)
1062 hflow |= sc->csigs->cts;
1063 if (t->c_cflag & CDSR_OFLOW)
1065 if (t->c_cflag & CCAR_OFLOW)
1068 DLOG(DIGIDB_SET, (sc->dev, "port%d: set hflow = 0x%x\n", port->pnum, hflow));
1069 fepcmd_w(port, SETHFLOW, 0xff00 | (unsigned)hflow, 0);
1071 DLOG(DIGIDB_SET, (sc->dev, "port%d: set startc(0x%x), stopc(0x%x)\n",
1072 port->pnum, t->c_cc[VSTART], t->c_cc[VSTOP]));
1073 fepcmd_b(port, SONOFFC, t->c_cc[VSTART], t->c_cc[VSTOP], 0);
1075 if (sc->window != 0)
1078 sc->towin(sc, window);
1087 struct digi_p *port;
1089 struct digi_softc *sc;
1091 volatile struct board_chan *bc;
1105 if (sc->status != DIGI_STATUS_ENABLED) {
1106 DLOG(DIGIDB_IRQ, (sc->dev, "interrupt on disabled board !\n"));
1110 #ifdef DIGI_INTERRUPT
1111 microtime(&sc->intr_timestamp);
1114 window = sc->window;
1117 if (sc->model >= PCXEM && W(sc->vmem + 0xd00)) {
1118 struct con_bios *con = con_bios_list;
1119 register u_char *ptr;
1121 ptr = sc->vmem + W(sc->vmem + 0xd00);
1123 if (ptr[1] && W(ptr + 2) == W(con->bios + 2))
1124 /* Not first block -- exact match */
1127 if (W(ptr + 4) >= W(con->bios + 4) &&
1128 W(ptr + 4) <= W(con->bios + 6))
1129 /* Initial search concetrator BIOS */
1134 log(LOG_ERR, "digi%d: wanted bios LREV = 0x%04x"
1135 " not found!\n", sc->res.unit, W(ptr + 4));
1137 W(sc->vmem + 0xd00) = 0;
1141 W(ptr + 4) = W(cxcon + 4);
1142 W(ptr + 6) = W(cxcon + 6);
1144 W(ptr + 2) = W(cxcon + 2);
1145 W(ptr + 8) = (ptr[1] << 6) + W(cxcon + 8);
1146 size = W(cxcon + 10) - (ptr[1] << 10);
1148 W(ptr + 8) = W(cxcon + 8);
1154 bcopy(cxcon + (ptr[1] << 10), ptr + 12, size);
1156 W(sc->vmem + 0xd00) = 0;
1160 ehead = sc->gdata->ein;
1161 etail = sc->gdata->eout;
1162 if (ehead == etail) {
1165 if (sc->intr_count % 6000 == 0) {
1166 DLOG(DIGIDB_IRQ, (sc->dev,
1167 "6000 useless polls %x %x\n", ehead, etail));
1173 while (ehead != etail) {
1174 event = *(volatile struct event *)(sc->memevent + etail);
1176 etail = (etail + 4) & sc->gdata->imax;
1178 if (event.pnum >= sc->numports) {
1179 log(LOG_ERR, "digi%d: port %d: got event"
1180 " on nonexisting port\n", sc->res.unit,
1184 port = &sc->ports[event.pnum];
1188 if (!(tp->t_state & TS_ISOPEN) && !tp->t_wopeners) {
1189 DLOG(DIGIDB_IRQ, (sc->dev,
1190 "port %d: event 0x%x on closed port\n",
1191 event.pnum, event.event));
1199 if (event.event & ~ALL_IND)
1200 log(LOG_ERR, "digi%d: port%d: ? event 0x%x mstat 0x%x"
1201 " lstat 0x%x\n", sc->res.unit, event.pnum,
1202 event.event, event.mstat, event.lstat);
1204 if (event.event & DATA_IND) {
1205 DLOG(DIGIDB_IRQ, (sc->dev, "port %d: DATA_IND\n",
1207 wrapmask = port->rxbufsize - 1;
1212 if (!(tp->t_state & TS_ISOPEN)) {
1216 while (head != tail) {
1219 DLOG(DIGIDB_INT, (sc->dev,
1220 "port %d: p rx head = %d tail = %d\n",
1221 event.pnum, head, tail));
1222 top = (head > tail) ? head : wrapmask + 1;
1223 sc->towin(sc, port->rxwin);
1225 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1226 size = b_to_q((char *)port->rxbuf +
1227 tail, size, &tp->t_rawq);
1230 } else for (; tail < top;) {
1231 ttyld_rint(tp, port->rxbuf[tail]);
1232 sc->towin(sc, port->rxwin);
1235 if (tp->t_state & TS_TBLOCK)
1247 CE_RECORD(port, CE_OVERRUN);
1248 log(LOG_ERR, "digi%d: port%d: %s\n",
1249 sc->res.unit, event.pnum,
1250 digi_errortxt(CE_OVERRUN));
1255 tp->t_state |= TS_TBLOCK;
1256 port->status |= PAUSE_RX;
1257 DLOG(DIGIDB_RX, (sc->dev, "port %d: pause RX\n",
1264 if (event.event & MODEMCHG_IND) {
1265 DLOG(DIGIDB_MODEM, (sc->dev, "port %d: MODEMCHG_IND\n",
1268 if ((event.mstat ^ event.lstat) & port->cd) {
1270 ttyld_modem(tp, event.mstat & port->cd);
1272 wakeup(TSA_CARR_ON(tp));
1275 if (event.mstat & sc->csigs->ri) {
1276 DLOG(DIGIDB_RI, (sc->dev, "port %d: RING\n",
1278 if (port->send_ring) {
1279 ttyld_rint(tp, 'R');
1280 ttyld_rint(tp, 'I');
1281 ttyld_rint(tp, 'N');
1282 ttyld_rint(tp, 'G');
1283 ttyld_rint(tp, '\r');
1284 ttyld_rint(tp, '\n');
1288 if (event.event & BREAK_IND) {
1289 DLOG(DIGIDB_MODEM, (sc->dev, "port %d: BREAK_IND\n",
1291 ttyld_rint(tp, TTY_BI);
1293 if (event.event & (LOWTX_IND | EMPTYTX_IND)) {
1294 DLOG(DIGIDB_IRQ, (sc->dev, "port %d:%s%s\n",
1296 event.event & LOWTX_IND ? " LOWTX" : "",
1297 event.event & EMPTYTX_IND ? " EMPTYTX" : ""));
1301 sc->gdata->eout = etail;
1303 if (sc->window != 0)
1306 sc->towin(sc, window);
1310 digistart(struct tty *tp)
1312 struct digi_p *port;
1313 struct digi_softc *sc;
1314 volatile struct board_chan *bc;
1316 int size, ocount, totcnt = 0;
1324 wmask = port->txbufsize - 1;
1327 port->lcc = tp->t_outq.c_cc;
1329 if (!(tp->t_state & TS_TBLOCK)) {
1330 if (port->status & PAUSE_RX) {
1331 DLOG(DIGIDB_RX, (sc->dev, "port %d: resume RX\n",
1334 * CAREFUL - braces are needed here if the DLOG is
1338 port->status &= ~PAUSE_RX;
1341 if (!(tp->t_state & TS_TTSTOP) && port->status & PAUSE_TX) {
1342 DLOG(DIGIDB_TX, (sc->dev, "port %d: resume TX\n", port->pnum));
1343 port->status &= ~PAUSE_TX;
1344 fepcmd_w(port, RESUMETX, 0, 10);
1346 if (tp->t_outq.c_cc == 0)
1347 tp->t_state &= ~TS_BUSY;
1349 tp->t_state |= TS_BUSY;
1352 while (tp->t_outq.c_cc != 0) {
1354 DLOG(DIGIDB_INT, (sc->dev, "port%d: s tx head = %d tail = %d\n",
1355 port->pnum, head, tail));
1358 size = tail - head - 1;
1360 size = port->txbufsize - head;
1367 sc->towin(sc, port->txwin);
1368 ocount = q_to_b(&tp->t_outq, port->txbuf + head, size);
1377 port->lostcc = tp->t_outq.c_cc;
1380 size = port->txbufsize - tail + head;
1385 DLOG(DIGIDB_INT, (sc->dev, "port%d: s total cnt = %d\n", port->pnum, totcnt));
1391 digistop(struct tty *tp, int rw)
1393 struct digi_softc *sc;
1394 struct digi_p *port;
1399 DLOG(DIGIDB_TX, (sc->dev, "port %d: pause TX\n", port->pnum));
1400 port->status |= PAUSE_TX;
1401 fepcmd_w(port, PAUSETX, 0, 10);
1405 fepcmd(struct digi_p *port, int cmd, int op1, int ncmds)
1408 unsigned tail, head;
1411 mem = port->sc->memcmd;
1413 port->sc->setwin(port->sc, 0);
1415 head = port->sc->gdata->cin;
1416 mem[head + 0] = cmd;
1417 mem[head + 1] = port->pnum;
1418 *(u_short *)(mem + head + 2) = op1;
1420 head = (head + 4) & port->sc->gdata->cmax;
1421 port->sc->gdata->cin = head;
1423 for (count = FEPTIMEOUT; count > 0; count--) {
1424 head = port->sc->gdata->cin;
1425 tail = port->sc->gdata->cout;
1426 n = (head - tail) & port->sc->gdata->cmax;
1428 if (n <= ncmds * sizeof(short) * 4)
1432 log(LOG_ERR, "digi%d: port%d: timeout on FEP command\n",
1433 port->sc->res.unit, port->pnum);
1437 digi_errortxt(int id)
1439 static const char *error_desc[] = {
1441 "interrupt-level buffer overflow",
1442 "tty-level buffer overflow",
1445 KASSERT(id >= 0 && id < sizeof(error_desc) / sizeof(error_desc[0]),
1446 ("Unexpected digi error id %d\n", id));
1448 return (error_desc[id]);
1452 digi_attach(struct digi_softc *sc)
1454 sc->res.ctldev = make_dev(&digi_csw,
1455 sc->res.unit << 16, UID_ROOT, GID_WHEEL,
1456 0600, "digi%r.ctl", sc->res.unit);
1457 sc->res.ctldev->si_drv1 = sc;
1459 digi_loadmoduledata(sc);
1461 digi_freemoduledata(sc);
1467 digi_inuse(struct digi_softc *sc)
1470 struct digi_p *port;
1472 port = &sc->ports[0];
1473 for (i = 0; i < sc->numports; i++, port++)
1474 if (port->tp->t_state & TS_ISOPEN) {
1475 DLOG(DIGIDB_INIT, (sc->dev, "port%d: busy\n", i));
1477 } else if (port->tp->t_wopeners || port->opencnt) {
1478 DLOG(DIGIDB_INIT, (sc->dev, "port%d: blocked in open\n",
1486 digi_free_state(struct digi_softc *sc)
1490 /* Blow it all away */
1492 for (i = 0; i < sc->numports; i++)
1493 ttygone(sc->ports[i].tp);
1495 /* XXX: this might be better done as a ttypurge method */
1496 untimeout(digi_poll, sc, sc->callout);
1497 callout_handle_init(&sc->callout);
1498 untimeout(digi_int_test, sc, sc->inttest);
1499 callout_handle_init(&sc->inttest);
1501 for (i = 0; i < sc->numports; i++)
1502 ttyfree(sc->ports[i].tp);
1504 bus_teardown_intr(sc->dev, sc->res.irq, sc->res.irqHandler);
1505 #ifdef DIGI_INTERRUPT
1506 if (sc->res.irq != NULL) {
1507 bus_release_resource(dev, SYS_RES_IRQ, sc->res.irqrid,
1513 KASSERT(sc->ports, ("digi%d: Lost my ports ?", sc->res.unit));
1514 free(sc->ports, M_TTYS);
1519 sc->status = DIGI_STATUS_NOTINIT;
1523 digi_detach(device_t dev)
1525 struct digi_softc *sc = device_get_softc(dev);
1527 DLOG(DIGIDB_INIT, (sc->dev, "detaching\n"));
1529 /* If we're INIT'd, numports must be 0 */
1530 KASSERT(sc->numports == 0 || sc->status != DIGI_STATUS_NOTINIT,
1531 ("digi%d: numports(%d) & status(%d) are out of sync",
1532 sc->res.unit, sc->numports, (int)sc->status));
1537 digi_free_state(sc);
1539 destroy_dev(sc->res.ctldev);
1541 if (sc->res.mem != NULL) {
1542 bus_release_resource(dev, SYS_RES_MEMORY, sc->res.mrid,
1546 if (sc->res.io != NULL) {
1547 bus_release_resource(dev, SYS_RES_IOPORT, sc->res.iorid,
1556 digi_shutdown(device_t dev)
1561 MODULE_VERSION(digi, 1);