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