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