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