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