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