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