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