Merge from vendor branch GCC:
[dragonfly.git] / sys / dev / serial / digi / digi.c
1 /*-
2  * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3  *   based on work by Slawa Olhovchenkov
4  *                    John Prince <johnp@knight-trosoft.com>
5  *                    Eric Hernes
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/dev/digi/digi.c,v 1.36 2003/09/26 09:05:57 phk Exp $
30  * $DragonFly: src/sys/dev/serial/digi/digi.c,v 1.2 2005/01/04 05:27:47 cpressey 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 <machine/resource.h>
55
56 #include <dev/serial/digi/digiio.h>
57 #include <dev/serial/digi/digireg.h>
58 #include <dev/serial/digi/digi.h>
59 #include <dev/serial/digi/digi_pci.h>
60 #include <dev/serial/digi/digi_bios.h>
61
62 #define CDEV_MAJOR      162
63
64 #define CTRL_DEV                0x800000
65 #define CALLOUT_MASK            0x400000
66 #define CONTROL_INIT_STATE      0x100000
67 #define CONTROL_LOCK_STATE      0x200000
68 #define CONTROL_MASK            (CTRL_DEV|CONTROL_INIT_STATE|CONTROL_LOCK_STATE)
69 #define UNIT_MASK               0x030000
70 #define PORT_MASK               0x0000FF
71 #define DEV_TO_UNIT(dev)        (MINOR_TO_UNIT(minor(dev)))
72 #define MINOR_MAGIC_MASK        (CALLOUT_MASK | CONTROL_MASK)
73 #define MINOR_TO_UNIT(mynor)    (((mynor) & UNIT_MASK)>>16)
74 #define MINOR_TO_PORT(mynor)    ((mynor) & PORT_MASK)
75
76 static d_open_t         digiopen;
77 static d_close_t        digiclose;
78 static d_read_t         digiread;
79 static d_write_t        digiwrite;
80 static d_ioctl_t        digiioctl;
81
82 static void     digistop(struct tty *tp, int rw);
83 static int      digimctl(struct digi_p *port, int bits, int how);
84 static void     digi_poll(void *ptr);
85 static void     digi_freedata(struct digi_softc *);
86 static void     fepcmd(struct digi_p *port, int cmd, int op, int ncmds);
87 static void     digistart(struct tty *tp);
88 static int      digiparam(struct tty *tp, struct termios *t);
89 static void     digihardclose(struct digi_p *port);
90 static void     digi_intr(void *);
91 static int      digi_init(struct digi_softc *_sc);
92 static int      digi_loaddata(struct digi_softc *);
93 static int      digi_inuse(struct digi_softc *);
94 static void     digi_free_state(struct digi_softc *);
95
96 #define fepcmd_b(port, cmd, op1, op2, ncmds) \
97         fepcmd(port, cmd, (op2 << 8) | op1, ncmds)
98 #define fepcmd_w        fepcmd
99
100
101 static speed_t digidefaultrate = TTYDEF_SPEED;
102
103 struct con_bios {
104         struct con_bios *next;
105         u_char *bios;
106         size_t size;
107 };
108
109 static struct con_bios *con_bios_list;
110 devclass_t       digi_devclass;
111 unsigned         digi_debug = 0;
112
113 static struct speedtab digispeedtab[] = {
114         { 0,            0},                     /* old (sysV-like) Bx codes */
115         { 50,           1},
116         { 75,           2},
117         { 110,          3},
118         { 134,          4},
119         { 150,          5},
120         { 200,          6},
121         { 300,          7},
122         { 600,          8},
123         { 1200,         9},
124         { 1800,         10},
125         { 2400,         11},
126         { 4800,         12},
127         { 9600,         13},
128         { 19200,        14},
129         { 38400,        15},
130         { 57600,        (02000 | 1)},
131         { 76800,        (02000 | 2)},
132         { 115200,       (02000 | 3)},
133         { 230400,       (02000 | 6)},
134         { -1,           -1}
135 };
136
137 const struct digi_control_signals digi_xixe_signals = {
138         0x02, 0x08, 0x10, 0x20, 0x40, 0x80
139 };
140
141 const struct digi_control_signals digi_normal_signals = {
142         0x02, 0x80, 0x20, 0x10, 0x40, 0x01
143 };
144
145 static struct cdevsw digi_sw = {
146         /* name */      "dgm",
147         /* maj */       CDEV_MAJOR,
148         /* flags */     D_TTY,
149         /* port */      NULL,
150         /* clone */     NULL,
151         /* open */      digiopen,
152         /* close */     digiclose,
153         /* read */      digiread,
154         /* write */     digiwrite,
155         /* ioctl */     digiioctl,
156         /* poll */      ttypoll,
157         /* mmap */      nommap,
158         /* stragety */  nostrategy,
159         /* dump */      nodump,
160         /* psize */     nopsize,
161         /* kqfilter */  ttykqfilter
162 };
163
164 static void
165 digi_poll(void *ptr)
166 {
167         struct digi_softc *sc;
168
169         sc = (struct digi_softc *)ptr;
170         callout_init(&sc->callout);
171         digi_intr(sc);
172         callout_reset(&sc->callout, (hz >= 200) ? hz / 100 : 1, digi_poll, sc);
173 }
174
175 static void
176 digi_int_test(void *v)
177 {
178         struct digi_softc *sc = v;
179
180         callout_init(&sc->inttest);
181 #ifdef DIGI_INTERRUPT
182         if (sc->intr_timestamp.tv_sec || sc->intr_timestamp.tv_usec) {
183                 /* interrupt OK! */
184                 return;
185         }
186         log(LOG_ERR, "digi%d: Interrupt didn't work, use polled mode\n", unit);
187 #endif
188         callout_reset(&sc->callout, (hz >= 200) ? hz / 100 : 1, digi_poll, sc);
189 }
190
191 static void
192 digi_freedata(struct digi_softc *sc)
193 {
194         if (sc->fep.data != NULL) {
195                 free(sc->fep.data, M_TTYS);
196                 sc->fep.data = NULL;
197         }
198         if (sc->link.data != NULL) {
199                 free(sc->link.data, M_TTYS);
200                 sc->link.data = NULL;
201         }
202         if (sc->bios.data != NULL) {
203                 free(sc->bios.data, M_TTYS);
204                 sc->bios.data = NULL;
205         }
206 }
207
208 static int
209 digi_bcopy(const void *vfrom, void *vto, size_t sz)
210 {
211         volatile const char *from = (volatile const char *)vfrom;
212         volatile char *to = (volatile char *)vto;
213         size_t i;
214
215         for (i = 0; i < sz; i++)
216                 *to++ = *from++;
217
218         from = (const volatile char *)vfrom;
219         to = (volatile char *)vto;
220         for (i = 0; i < sz; i++)
221                 if (*to++ != *from++)
222                         return (0);
223         return (1);
224 }
225
226 void
227 digi_delay(struct digi_softc *sc, const char *txt, u_long timo)
228 {
229         if (cold)
230                 DELAY(timo * 1000000 / hz);
231         else
232                 tsleep(sc, PCATCH, txt, timo);
233 }
234
235 static int
236 digi_init(struct digi_softc *sc)
237 {
238         int i, cnt, resp;
239         u_char *ptr;
240         int lowwater;
241         struct digi_p *port;
242         volatile struct board_chan *bc;
243
244         ptr = NULL;
245
246         if (sc->status == DIGI_STATUS_DISABLED) {
247                 log(LOG_ERR, "digi%d: Cannot init a disabled card\n",
248                     sc->res.unit);
249                 return (EIO);
250         }
251         if (sc->bios.data == NULL) {
252                 log(LOG_ERR, "digi%d: Cannot init without BIOS\n",
253                     sc->res.unit);
254                 return (EIO);
255         }
256 #if 0
257         if (sc->link.data == NULL && sc->model >= PCCX) {
258                 log(LOG_ERR, "digi%d: Cannot init without link info\n",
259                     sc->res.unit);
260                 return (EIO);
261         }
262 #endif
263         if (sc->fep.data == NULL) {
264                 log(LOG_ERR, "digi%d: Cannot init without fep code\n",
265                     sc->res.unit);
266                 return (EIO);
267         }
268         sc->status = DIGI_STATUS_NOTINIT;
269
270         if (sc->numports) {
271                 /*
272                  * We're re-initialising - maybe because someone's attached
273                  * another port module.  For now, we just re-initialise
274                  * everything.
275                  */
276                 if (digi_inuse(sc))
277                         return (EBUSY);
278
279                 digi_free_state(sc);
280         }
281
282         ptr = sc->setwin(sc, MISCGLOBAL);
283         for (i = 0; i < 16; i += 2)
284                 vW(ptr + i) = 0;
285
286         switch (sc->model) {
287         case PCXEVE:
288                 outb(sc->wport, 0xff);          /* window 7 */
289                 ptr = sc->vmem + (BIOSCODE & 0x1fff);
290
291                 if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
292                         device_printf(sc->dev, "BIOS upload failed\n");
293                         return (EIO);
294                 }
295
296                 outb(sc->port, FEPCLR);
297                 break;
298
299         case PCXE:
300         case PCXI:
301         case PCCX:
302                 ptr = sc->setwin(sc, BIOSCODE + ((0xf000 - sc->mem_seg) << 4));
303                 if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
304                         device_printf(sc->dev, "BIOS upload failed\n");
305                         return (EIO);
306                 }
307                 break;
308
309         case PCXEM:
310         case PCIEPCX:
311         case PCIXR:
312                 if (sc->pcibus)
313                         PCIPORT = FEPRST;
314                 else
315                         outb(sc->port, FEPRST | FEPMEM);
316
317                 for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) &
318                     FEPMASK) != FEPRST; i++) {
319                         if (i > hz) {
320                                 log(LOG_ERR, "digi%d: %s init reset failed\n",
321                                     sc->res.unit, sc->name);
322                                 return (EIO);
323                         }
324                         digi_delay(sc, "digiinit0", 5);
325                 }
326                 DLOG(DIGIDB_INIT, (sc->dev, "Got init reset after %d us\n", i));
327
328                 /* Now upload the BIOS */
329                 cnt = (sc->bios.size < sc->win_size - BIOSOFFSET) ?
330                     sc->bios.size : sc->win_size - BIOSOFFSET;
331
332                 ptr = sc->setwin(sc, BIOSOFFSET);
333                 if (!digi_bcopy(sc->bios.data, ptr, cnt)) {
334                         device_printf(sc->dev, "BIOS upload (1) failed\n");
335                         return (EIO);
336                 }
337
338                 if (cnt != sc->bios.size) {
339                         /* and the second part */
340                         ptr = sc->setwin(sc, sc->win_size);
341                         if (!digi_bcopy(sc->bios.data + cnt, ptr,
342                             sc->bios.size - cnt)) {
343                                 device_printf(sc->dev, "BIOS upload failed\n");
344                                 return (EIO);
345                         }
346                 }
347
348                 ptr = sc->setwin(sc, 0);
349                 vW(ptr + 0) = 0x0401;
350                 vW(ptr + 2) = 0x0bf0;
351                 vW(ptr + 4) = 0x0000;
352                 vW(ptr + 6) = 0x0000;
353
354                 break;
355         }
356
357         DLOG(DIGIDB_INIT, (sc->dev, "BIOS uploaded\n"));
358
359         ptr = sc->setwin(sc, MISCGLOBAL);
360         W(ptr) = 0;
361
362         if (sc->pcibus) {
363                 PCIPORT = FEPCLR;
364                 resp = FEPRST;
365         } else if (sc->model == PCXEVE) {
366                 outb(sc->port, FEPCLR);
367                 resp = FEPRST;
368         } else {
369                 outb(sc->port, FEPCLR | FEPMEM);
370                 resp = FEPRST | FEPMEM;
371         }
372
373         for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) & FEPMASK)
374             == resp; i++) {
375                 if (i > hz) {
376                         log(LOG_ERR, "digi%d: BIOS start failed\n",
377                             sc->res.unit);
378                         return (EIO);
379                 }
380                 digi_delay(sc, "digibios0", 5);
381         }
382
383         DLOG(DIGIDB_INIT, (sc->dev, "BIOS started after %d us\n", i));
384
385         for (i = 0; vW(ptr) != *(u_short *)"GD"; i++) {
386                 if (i > 2*hz) {
387                         log(LOG_ERR, "digi%d: BIOS boot failed "
388                             "(0x%02x != 0x%02x)\n",
389                             sc->res.unit, vW(ptr), *(u_short *)"GD");
390                         return (EIO);
391                 }
392                 digi_delay(sc, "digibios1", 5);
393         }
394
395         DLOG(DIGIDB_INIT, (sc->dev, "BIOS booted after %d iterations\n", i));
396
397         if (sc->link.data != NULL) {
398                 DLOG(DIGIDB_INIT, (sc->dev, "Loading link data\n"));
399                 ptr = sc->setwin(sc, 0xcd0);
400                 digi_bcopy(sc->link.data, ptr, 21);     /* XXX 21 ? */
401         }
402
403         /* load FEP/OS */
404
405         switch (sc->model) {
406         case PCXE:
407         case PCXEVE:
408         case PCXI:
409                 ptr = sc->setwin(sc, sc->model == PCXI ? 0x2000 : 0x0);
410                 digi_bcopy(sc->fep.data, ptr, sc->fep.size);
411
412                 /* A BIOS request to move our data to 0x2000 */
413                 ptr = sc->setwin(sc, MBOX);
414                 vW(ptr + 0) = 2;
415                 vW(ptr + 2) = sc->mem_seg + FEPCODESEG;
416                 vW(ptr + 4) = 0;
417                 vW(ptr + 6) = FEPCODESEG;
418                 vW(ptr + 8) = 0;
419                 vW(ptr + 10) = sc->fep.size;
420
421                 /* Run the BIOS request */
422                 outb(sc->port, FEPREQ | FEPMEM);
423                 outb(sc->port, FEPCLR | FEPMEM);
424
425                 for (i = 0; W(ptr); i++) {
426                         if (i > hz) {
427                                 log(LOG_ERR, "digi%d: FEP/OS move failed\n",
428                                     sc->res.unit);
429                                 sc->hidewin(sc);
430                                 return (EIO);
431                         }
432                         digi_delay(sc, "digifep0", 5);
433                 }
434                 DLOG(DIGIDB_INIT,
435                     (sc->dev, "FEP/OS moved after %d iterations\n", i));
436
437                 /* Clear the confirm word */
438                 ptr = sc->setwin(sc, FEPSTAT);
439                 vW(ptr + 0) = 0;
440
441                 /* A BIOS request to execute the FEP/OS */
442                 ptr = sc->setwin(sc, MBOX);
443                 vW(ptr + 0) = 0x01;
444                 vW(ptr + 2) = FEPCODESEG;
445                 vW(ptr + 4) = 0x04;
446
447                 /* Run the BIOS request */
448                 outb(sc->port, FEPREQ);
449                 outb(sc->port, FEPCLR);
450
451                 ptr = sc->setwin(sc, FEPSTAT);
452
453                 break;
454
455         case PCXEM:
456         case PCIEPCX:
457         case PCIXR:
458                 DLOG(DIGIDB_INIT, (sc->dev, "Loading FEP/OS\n"));
459
460                 cnt = (sc->fep.size < sc->win_size - BIOSOFFSET) ?
461                     sc->fep.size : sc->win_size - BIOSOFFSET;
462
463                 ptr = sc->setwin(sc, BIOSOFFSET);
464                 digi_bcopy(sc->fep.data, ptr, cnt);
465
466                 if (cnt != sc->fep.size) {
467                         ptr = sc->setwin(sc, BIOSOFFSET + cnt);
468                         digi_bcopy(sc->fep.data + cnt, ptr,
469                             sc->fep.size - cnt);
470                 }
471
472                 DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS loaded\n"));
473
474                 ptr = sc->setwin(sc, 0xc30);
475                 W(ptr + 4) = 0x1004;
476                 W(ptr + 6) = 0xbfc0;
477                 W(ptr + 0) = 0x03;
478                 W(ptr + 2) = 0x00;
479
480                 /* Clear the confirm word */
481                 ptr = sc->setwin(sc, FEPSTAT);
482                 W(ptr + 0) = 0;
483
484                 if (sc->port)
485                         outb(sc->port, 0);              /* XXX necessary ? */
486
487                 break;
488
489         case PCCX:
490                 ptr = sc->setwin(sc, 0xd000);
491                 digi_bcopy(sc->fep.data, ptr, sc->fep.size);
492
493                 /* A BIOS request to execute the FEP/OS */
494                 ptr = sc->setwin(sc, 0xc40);
495                 W(ptr + 0) = 1;
496                 W(ptr + 2) = FEPCODE >> 4;
497                 W(ptr + 4) = 4;
498
499                 /* Clear the confirm word */
500                 ptr = sc->setwin(sc, FEPSTAT);
501                 W(ptr + 0) = 0;
502
503                 /* Run the BIOS request */
504                 outb(sc->port, FEPREQ | FEPMEM); /* send interrupt to BIOS */
505                 outb(sc->port, FEPCLR | FEPMEM);
506                 break;
507         }
508
509         /* Now wait 'till the FEP/OS has booted */
510         for (i = 0; vW(ptr) != *(u_short *)"OS"; i++) {
511                 if (i > 2*hz) {
512                         log(LOG_ERR, "digi%d: FEP/OS start failed "
513                             "(0x%02x != 0x%02x)\n",
514                             sc->res.unit, vW(ptr), *(u_short *)"OS");
515                         sc->hidewin(sc);
516                         return (EIO);
517                 }
518                 digi_delay(sc, "digifep1", 5);
519         }
520
521         DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS started after %d iterations\n", i));
522
523         if (sc->model >= PCXEM) {
524                 ptr = sc->setwin(sc, 0xe04);
525                 vW(ptr) = 2;
526                 ptr = sc->setwin(sc, 0xc02);
527                 sc->numports = vW(ptr);
528         } else {
529                 ptr = sc->setwin(sc, 0xc22);
530                 sc->numports = vW(ptr);
531         }
532
533         if (sc->numports == 0) {
534                 device_printf(sc->dev, "%s, 0 ports found\n", sc->name);
535                 sc->hidewin(sc);
536                 return (0);
537         }
538
539         if (sc->numports > 256) {
540                 /* Our minor numbering scheme is broken for more than 256 */
541                 device_printf(sc->dev, "%s, 256 ports (%d ports found)\n",
542                     sc->name, sc->numports);
543                 sc->numports = 256;
544         } else
545                 device_printf(sc->dev, "%s, %d ports found\n", sc->name,
546                     sc->numports);
547
548         if (sc->ports)
549                 free(sc->ports, M_TTYS);
550         sc->ports = malloc(sizeof(struct digi_p) * sc->numports,
551             M_TTYS, M_WAITOK | M_ZERO);
552
553         if (sc->ttys)
554                 free(sc->ttys, M_TTYS);
555         sc->ttys = malloc(sizeof(struct tty) * sc->numports,
556             M_TTYS, M_WAITOK | M_ZERO);
557
558         /*
559          * XXX Should read port 0xc90 for an array of 2byte values, 1 per
560          * port.  If the value is 0, the port is broken....
561          */
562
563         ptr = sc->setwin(sc, 0);
564
565         /* We should now init per-port structures */
566         bc = (volatile struct board_chan *)(ptr + CHANSTRUCT);
567         sc->gdata = (volatile struct global_data *)(ptr + FEP_GLOBAL);
568
569         sc->memcmd = ptr + sc->gdata->cstart;
570         sc->memevent = ptr + sc->gdata->istart;
571
572         for (i = 0; i < sc->numports; i++, bc++) {
573                 port = sc->ports + i;
574                 port->pnum = i;
575                 port->sc = sc;
576                 port->status = ENABLED;
577                 port->tp = sc->ttys + i;
578                 port->bc = bc;
579
580                 if (sc->model == PCXEVE) {
581                         port->txbuf = ptr +
582                             (((bc->tseg - sc->mem_seg) << 4) & 0x1fff);
583                         port->rxbuf = ptr +
584                             (((bc->rseg - sc->mem_seg) << 4) & 0x1fff);
585                         port->txwin = FEPWIN | ((bc->tseg - sc->mem_seg) >> 9);
586                         port->rxwin = FEPWIN | ((bc->rseg - sc->mem_seg) >> 9);
587                 } else if (sc->model == PCXI || sc->model == PCXE) {
588                         port->txbuf = ptr + ((bc->tseg - sc->mem_seg) << 4);
589                         port->rxbuf = ptr + ((bc->rseg - sc->mem_seg) << 4);
590                         port->txwin = port->rxwin = 0;
591                 } else {
592                         port->txbuf = ptr +
593                             (((bc->tseg - sc->mem_seg) << 4) % sc->win_size);
594                         port->rxbuf = ptr +
595                             (((bc->rseg - sc->mem_seg) << 4) % sc->win_size);
596                         port->txwin = FEPWIN |
597                             (((bc->tseg - sc->mem_seg) << 4) / sc->win_size);
598                         port->rxwin = FEPWIN |
599                             (((bc->rseg - sc->mem_seg) << 4) / sc->win_size);
600                 }
601                 port->txbufsize = bc->tmax + 1;
602                 port->rxbufsize = bc->rmax + 1;
603
604                 lowwater = port->txbufsize >> 2;
605                 if (lowwater > 1024)
606                         lowwater = 1024;
607                 sc->setwin(sc, 0);
608                 fepcmd_w(port, STXLWATER, lowwater, 10);
609                 fepcmd_w(port, SRXLWATER, port->rxbufsize >> 2, 10);
610                 fepcmd_w(port, SRXHWATER, (3 * port->rxbufsize) >> 2, 10);
611
612                 bc->edelay = 100;
613                 port->dtr_wait = 3 * hz;
614
615                 /*
616                  * We don't use all the flags from <sys/ttydefaults.h> since
617                  * they are only relevant for logins.  It's important to have
618                  * echo off initially so that the line doesn't start blathering
619                  * before the echo flag can be turned off.
620                  */
621                 port->it_in.c_iflag = 0;
622                 port->it_in.c_oflag = 0;
623                 port->it_in.c_cflag = TTYDEF_CFLAG;
624                 port->it_in.c_lflag = 0;
625                 termioschars(&port->it_in);
626                 port->it_in.c_ispeed = port->it_in.c_ospeed = digidefaultrate;
627                 port->it_out = port->it_in;
628                 port->send_ring = 1;    /* Default action on signal RI */
629
630                 port->dev[0] = make_dev(&digi_sw, (sc->res.unit << 16) + i,
631                     UID_ROOT, GID_WHEEL, 0600, "ttyD%d.%d", sc->res.unit, i);
632                 port->dev[1] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
633                     CONTROL_INIT_STATE, UID_ROOT, GID_WHEEL,
634                     0600, "ttyiD%d.%d", sc->res.unit, i);
635                 port->dev[2] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
636                     CONTROL_LOCK_STATE, UID_ROOT, GID_WHEEL,
637                     0600, "ttylD%d.%d", sc->res.unit, i);
638                 port->dev[3] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
639                     CALLOUT_MASK, UID_UUCP, GID_DIALER,
640                     0660, "cuaD%d.%d", sc->res.unit, i);
641                 port->dev[4] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
642                     CALLOUT_MASK | CONTROL_INIT_STATE, UID_UUCP, GID_DIALER,
643                     0660, "cuaiD%d.%d", sc->res.unit, i);
644                 port->dev[5] = make_dev(&digi_sw, ((sc->res.unit << 16) + i) |
645                     CALLOUT_MASK | CONTROL_LOCK_STATE, UID_UUCP, GID_DIALER,
646                     0660, "cualD%d.%d", sc->res.unit, i);
647         }
648
649         sc->hidewin(sc);
650         callout_reset(&sc->inttest, hz, digi_int_test, sc);
651         /* fepcmd_w(&sc->ports[0], 0xff, 0, 0); */
652         sc->status = DIGI_STATUS_ENABLED;
653
654         return (0);
655 }
656
657 static int
658 digimctl(struct digi_p *port, int bits, int how)
659 {
660         int mstat;
661
662         if (how == DMGET) {
663                 port->sc->setwin(port->sc, 0);
664                 mstat = port->bc->mstat;
665                 port->sc->hidewin(port->sc);
666                 bits = TIOCM_LE;
667                 if (mstat & port->sc->csigs->rts)
668                         bits |= TIOCM_RTS;
669                 if (mstat & port->cd)
670                         bits |= TIOCM_CD;
671                 if (mstat & port->dsr)
672                         bits |= TIOCM_DSR;
673                 if (mstat & port->sc->csigs->cts)
674                         bits |= TIOCM_CTS;
675                 if (mstat & port->sc->csigs->ri)
676                         bits |= TIOCM_RI;
677                 if (mstat & port->sc->csigs->dtr)
678                         bits |= TIOCM_DTR;
679                 return (bits);
680         }
681
682         /* Only DTR and RTS may be set */
683         mstat = 0;
684         if (bits & TIOCM_DTR)
685                 mstat |= port->sc->csigs->dtr;
686         if (bits & TIOCM_RTS)
687                 mstat |= port->sc->csigs->rts;
688
689         switch (how) {
690         case DMSET:
691                 fepcmd_b(port, SETMODEM, mstat, ~mstat, 0);
692                 break;
693         case DMBIS:
694                 fepcmd_b(port, SETMODEM, mstat, 0, 0);
695                 break;
696         case DMBIC:
697                 fepcmd_b(port, SETMODEM, 0, mstat, 0);
698                 break;
699         }
700
701         return (0);
702 }
703
704 static void
705 digi_disc_optim(struct tty *tp, struct termios *t, struct digi_p *port)
706 {
707         if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP)) &&
708             (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) &&
709             (!(t->c_iflag & PARMRK) ||
710             (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) &&
711             !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) &&
712             linesw[tp->t_line].l_rint == ttyinput)
713                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
714         else
715                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
716 }
717
718 static int
719 digiopen(dev_t dev, int flag, int mode, struct thread *td)
720 {
721         struct digi_softc *sc;
722         struct tty *tp;
723         int unit;
724         int pnum;
725         struct digi_p *port;
726         int s;
727         int error, mynor;
728         volatile struct board_chan *bc;
729
730         error = 0;
731         mynor = minor(dev);
732         unit = MINOR_TO_UNIT(minor(dev));
733         pnum = MINOR_TO_PORT(minor(dev));
734
735         sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
736         if (!sc)
737                 return (ENXIO);
738
739         if (sc->status != DIGI_STATUS_ENABLED) {
740                 DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
741                 return (ENXIO);
742         }
743         if (pnum >= sc->numports) {
744                 DLOG(DIGIDB_OPEN, (sc->dev, "port%d: Doesn't exist\n", pnum));
745                 return (ENXIO);
746         }
747         if (mynor & (CTRL_DEV | CONTROL_MASK)) {
748                 sc->opencnt++;
749                 return (0);
750         }
751         port = &sc->ports[pnum];
752         tp = dev->si_tty = port->tp;
753         bc = port->bc;
754
755         s = spltty();
756
757 open_top:
758         while (port->status & DIGI_DTR_OFF) {
759                 port->wopeners++;
760                 error = tsleep(&port->dtr_wait, PCATCH, "digidtr", 0);
761                 port->wopeners--;
762                 if (error)
763                         goto out;
764         }
765
766         if (tp->t_state & TS_ISOPEN) {
767                 /*
768                  * The device is open, so everything has been initialized.
769                  * Handle conflicts.
770                  */
771                 if (mynor & CALLOUT_MASK) {
772                         if (!port->active_out) {
773                                 error = EBUSY;
774                                 DLOG(DIGIDB_OPEN, (sc->dev, "port %d:"
775                                     " BUSY error = %d\n", pnum, error));
776                                 goto out;
777                         }
778                 } else if (port->active_out) {
779                         if (flag & O_NONBLOCK) {
780                                 error = EBUSY;
781                                 DLOG(DIGIDB_OPEN, (sc->dev,
782                                     "port %d: BUSY error = %d\n", pnum, error));
783                                 goto out;
784                         }
785                         port->wopeners++;
786                         error = tsleep(&port->active_out, PCATCH, "digibi", 0);
787                         port->wopeners--;
788                         if (error != 0) {
789                                 DLOG(DIGIDB_OPEN, (sc->dev,
790                                     "port %d: tsleep(digibi) error = %d\n",
791                                     pnum, error));
792                                 goto out;
793                         }
794                         goto open_top;
795                 }
796                 if (tp->t_state & TS_XCLUDE && suser(td) != 0) {
797                         error = EBUSY;
798                         goto out;
799                 }
800         } else {
801                 /*
802                  * The device isn't open, so there are no conflicts.
803                  * Initialize it.  Initialization is done twice in many
804                  * cases: to preempt sleeping callin opens if we are callout,
805                  * and to complete a callin open after DCD rises.
806                  */
807                 callout_init(&port->wakeupco);
808                 tp->t_oproc = digistart;
809                 tp->t_param = digiparam;
810                 tp->t_stop = digistop;
811                 tp->t_dev = dev;
812                 tp->t_termios = (mynor & CALLOUT_MASK) ?
813                     port->it_out : port->it_in;
814                 sc->setwin(sc, 0);
815
816                 bc->rout = bc->rin;     /* clear input queue */
817                 bc->idata = 1;
818                 bc->iempty = 1;
819                 bc->ilow = 1;
820                 bc->mint = port->cd | port->sc->csigs->ri;
821                 bc->tin = bc->tout;
822                 if (port->ialtpin) {
823                         port->cd = sc->csigs->dsr;
824                         port->dsr = sc->csigs->cd;
825                 } else {
826                         port->cd = sc->csigs->cd;
827                         port->dsr = sc->csigs->dsr;
828                 }
829                 port->wopeners++;                       /* XXX required ? */
830                 error = digiparam(tp, &tp->t_termios);
831                 port->wopeners--;
832
833                 if (error != 0) {
834                         DLOG(DIGIDB_OPEN, (sc->dev,
835                             "port %d: cxpparam error = %d\n", pnum, error));
836                         goto out;
837                 }
838                 ttsetwater(tp);
839
840                 /* handle fake and initial DCD for callout devices */
841
842                 if (bc->mstat & port->cd || mynor & CALLOUT_MASK)
843                         linesw[tp->t_line].l_modem(tp, 1);
844         }
845
846         /* Wait for DCD if necessary */
847         if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) &&
848             !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
849                 port->wopeners++;
850                 error = tsleep(TSA_CARR_ON(tp), PCATCH, "digidcd", 0);
851                 port->wopeners--;
852                 if (error != 0) {
853                         DLOG(DIGIDB_OPEN, (sc->dev,
854                             "port %d: tsleep(digidcd) error = %d\n",
855                             pnum, error));
856                         goto out;
857                 }
858                 goto open_top;
859         }
860         error = linesw[tp->t_line].l_open(dev, tp);
861         DLOG(DIGIDB_OPEN, (sc->dev, "port %d: l_open error = %d\n",
862             pnum, error));
863
864         digi_disc_optim(tp, &tp->t_termios, port);
865
866         if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
867                 port->active_out = TRUE;
868
869         if (tp->t_state & TS_ISOPEN)
870                 sc->opencnt++;
871 out:
872         splx(s);
873
874         if (!(tp->t_state & TS_ISOPEN))
875                 digihardclose(port);
876
877         DLOG(DIGIDB_OPEN, (sc->dev, "port %d: open() returns %d\n",
878             pnum, error));
879
880         return (error);
881 }
882
883 static int
884 digiclose(dev_t dev, int flag, int mode, struct thread *td)
885 {
886         int mynor;
887         struct tty *tp;
888         int unit, pnum;
889         struct digi_softc *sc;
890         struct digi_p *port;
891         int s;
892
893         mynor = minor(dev);
894         unit = MINOR_TO_UNIT(mynor);
895         pnum = MINOR_TO_PORT(mynor);
896
897         sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
898         KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
899
900         if (mynor & (CTRL_DEV | CONTROL_MASK)) {
901                 sc->opencnt--;
902                 return (0);
903         }
904
905         port = sc->ports + pnum;
906         tp = port->tp;
907
908         DLOG(DIGIDB_CLOSE, (sc->dev, "port %d: closing\n", pnum));
909
910         s = spltty();
911         linesw[tp->t_line].l_close(tp, flag);
912         digi_disc_optim(tp, &tp->t_termios, port);
913         digistop(tp, FREAD | FWRITE);
914         digihardclose(port);
915         ttyclose(tp);
916         if (--sc->opencnt == 0)
917                 splx(s);
918         return (0);
919 }
920
921 static void
922 digidtrwakeup(void *chan)
923 {
924         struct digi_p *port = chan;
925
926         port->status &= ~DIGI_DTR_OFF;
927         wakeup(&port->dtr_wait);
928         port->wopeners--;
929 }
930
931 static void
932 digihardclose(struct digi_p *port)
933 {
934         volatile struct board_chan *bc;
935         int s;
936
937         bc = port->bc;
938
939         s = spltty();
940         port->sc->setwin(port->sc, 0);
941         bc->idata = 0;
942         bc->iempty = 0;
943         bc->ilow = 0;
944         bc->mint = 0;
945         if ((port->tp->t_cflag & HUPCL) ||
946             (!port->active_out && !(bc->mstat & port->cd) &&
947             !(port->it_in.c_cflag & CLOCAL)) ||
948             !(port->tp->t_state & TS_ISOPEN)) {
949                 digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
950                 if (port->dtr_wait != 0) {
951                         /* Schedule a wakeup of any callin devices */
952                         port->wopeners++;
953                         callout_reset(&port->wakeupco, port->dtr_wait,
954                                       digidtrwakeup, port);
955                         port->status |= DIGI_DTR_OFF;
956                 }
957         }
958         port->active_out = FALSE;
959         wakeup(&port->active_out);
960         wakeup(TSA_CARR_ON(port->tp));
961         splx(s);
962 }
963
964 static int
965 digiread(dev_t dev, struct uio *uio, int flag)
966 {
967         int mynor;
968         struct tty *tp;
969         int error, unit, pnum;
970         struct digi_softc *sc;
971
972         mynor = minor(dev);
973         if (mynor & CONTROL_MASK)
974                 return (ENODEV);
975
976         unit = MINOR_TO_UNIT(mynor);
977         pnum = MINOR_TO_PORT(mynor);
978
979         sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
980         KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
981         tp = &sc->ttys[pnum];
982
983         error = linesw[tp->t_line].l_read(tp, uio, flag);
984         DLOG(DIGIDB_READ, (sc->dev, "port %d: read() returns %d\n",
985             pnum, error));
986
987         return (error);
988 }
989
990 static int
991 digiwrite(dev_t dev, struct uio *uio, int flag)
992 {
993         int mynor;
994         struct tty *tp;
995         int error, unit, pnum;
996         struct digi_softc *sc;
997
998         mynor = minor(dev);
999         if (mynor & CONTROL_MASK)
1000                 return (ENODEV);
1001
1002         unit = MINOR_TO_UNIT(mynor);
1003         pnum = MINOR_TO_PORT(mynor);
1004
1005         sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1006         KASSERT(sc, ("digi%d: softc not allocated in digiclose\n", unit));
1007         tp = &sc->ttys[pnum];
1008
1009         error = linesw[tp->t_line].l_write(tp, uio, flag);
1010         DLOG(DIGIDB_WRITE, (sc->dev, "port %d: write() returns %d\n",
1011             pnum, error));
1012
1013         return (error);
1014 }
1015
1016 /*
1017  * Load module "digi_<mod>.ko" and look for a symbol called digi_mod_<mod>.
1018  *
1019  * Populate sc->bios, sc->fep, and sc->link from this data.
1020  *
1021  * sc->fep.data, sc->bios.data and sc->link.data are malloc()d according
1022  * to their respective sizes.
1023  *
1024  * The module is unloaded when we're done.
1025  */
1026 static int
1027 digi_loaddata(struct digi_softc *sc)
1028 {
1029         struct digi_bios *bios;
1030
1031         KASSERT(sc->bios.data == NULL, ("Uninitialised BIOS variable"));
1032         KASSERT(sc->fep.data == NULL, ("Uninitialised FEP variable"));
1033         KASSERT(sc->link.data == NULL, ("Uninitialised LINK variable"));
1034         KASSERT(sc->module != NULL, ("Uninitialised module name"));
1035
1036         for (bios = digi_bioses; bios->model != NULL; bios++) {
1037                 if (!strcmp(bios->model, sc->module))
1038                         break;
1039         }
1040         if (bios->model == NULL) {
1041                 printf("digi.ko: driver %s not found", sc->module);
1042                 return(EINVAL);
1043         }
1044
1045         sc->bios.size = bios->bios_size;
1046         if (sc->bios.size != 0 && bios->bios != NULL) {
1047                 sc->bios.data = malloc(sc->bios.size, M_TTYS, M_WAITOK);
1048                 bcopy(bios->bios, sc->bios.data, sc->bios.size);
1049         }
1050
1051         sc->fep.size = bios->fep_size;
1052         if (sc->fep.size != 0 && bios->fep != NULL) {
1053                 sc->fep.data = malloc(sc->fep.size, M_TTYS, M_WAITOK);
1054                 bcopy(bios->fep, sc->fep.data, sc->fep.size);
1055         }
1056
1057         return (0);
1058 }
1059
1060 static int
1061 digiioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1062 {
1063         int unit, pnum, mynor, error, s;
1064         struct digi_softc *sc;
1065         struct digi_p *port;
1066         struct tty *tp;
1067 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1068         int oldcmd;
1069         struct termios term;
1070 #endif
1071
1072         mynor = minor(dev);
1073         unit = MINOR_TO_UNIT(mynor);
1074         pnum = MINOR_TO_PORT(mynor);
1075
1076         sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1077         KASSERT(sc, ("digi%d: softc not allocated in digiioctl\n", unit));
1078
1079         if (sc->status == DIGI_STATUS_DISABLED)
1080                 return (ENXIO);
1081
1082         if (mynor & CTRL_DEV) {
1083                 switch (cmd) {
1084                 case DIGIIO_DEBUG:
1085 #ifdef DEBUG
1086                         digi_debug = *(int *)data;
1087                         return (0);
1088 #else
1089                         device_printf(sc->dev, "DEBUG not defined\n");
1090                         return (ENXIO);
1091 #endif
1092                 case DIGIIO_REINIT:
1093                         digi_loaddata(sc);
1094                         error = digi_init(sc);
1095                         digi_freedata(sc);
1096                         return (error);
1097
1098                 case DIGIIO_MODEL:
1099                         *(enum digi_model *)data = sc->model;
1100                         return (0);
1101
1102                 case DIGIIO_IDENT:
1103                         return (copyout(sc->name, *(char **)data,
1104                             strlen(sc->name) + 1));
1105                 }
1106         }
1107
1108         if (pnum >= sc->numports)
1109                 return (ENXIO);
1110
1111         port = sc->ports + pnum;
1112         if (!(port->status & ENABLED))
1113                 return (ENXIO);
1114
1115         tp = port->tp;
1116
1117         if (mynor & CONTROL_MASK) {
1118                 struct termios *ct;
1119
1120                 switch (mynor & CONTROL_MASK) {
1121                 case CONTROL_INIT_STATE:
1122                         ct = (mynor & CALLOUT_MASK) ?
1123                             &port->it_out : &port->it_in;
1124                         break;
1125                 case CONTROL_LOCK_STATE:
1126                         ct = (mynor & CALLOUT_MASK) ?
1127                             &port->lt_out : &port->lt_in;
1128                         break;
1129                 default:
1130                         return (ENODEV);        /* /dev/nodev */
1131                 }
1132
1133                 switch (cmd) {
1134                 case TIOCSETA:
1135                         error = suser(td);
1136                         if (error != 0)
1137                                 return (error);
1138                         *ct = *(struct termios *)data;
1139                         return (0);
1140
1141                 case TIOCGETA:
1142                         *(struct termios *)data = *ct;
1143                         return (0);
1144
1145                 case TIOCGETD:
1146                         *(int *)data = TTYDISC;
1147                         return (0);
1148
1149                 case TIOCGWINSZ:
1150                         bzero(data, sizeof(struct winsize));
1151                         return (0);
1152
1153                 case DIGIIO_GETALTPIN:
1154                         switch (mynor & CONTROL_MASK) {
1155                         case CONTROL_INIT_STATE:
1156                                 *(int *)data = port->ialtpin;
1157                                 break;
1158
1159                         case CONTROL_LOCK_STATE:
1160                                 *(int *)data = port->laltpin;
1161                                 break;
1162
1163                         default:
1164                                 panic("Confusion when re-testing minor");
1165                                 return (ENODEV);
1166                         }
1167                         return (0);
1168
1169                 case DIGIIO_SETALTPIN:
1170                         switch (mynor & CONTROL_MASK) {
1171                         case CONTROL_INIT_STATE:
1172                                 if (!port->laltpin) {
1173                                         port->ialtpin = !!*(int *)data;
1174                                         DLOG(DIGIDB_SET, (sc->dev,
1175                                             "port%d: initial ALTPIN %s\n", pnum,
1176                                             port->ialtpin ? "set" : "cleared"));
1177                                 }
1178                                 break;
1179
1180                         case CONTROL_LOCK_STATE:
1181                                 port->laltpin = !!*(int *)data;
1182                                 DLOG(DIGIDB_SET, (sc->dev,
1183                                     "port%d: ALTPIN %slocked\n",
1184                                     pnum, port->laltpin ? "" : "un"));
1185                                 break;
1186
1187                         default:
1188                                 panic("Confusion when re-testing minor");
1189                                 return (ENODEV);
1190                         }
1191                         return (0);
1192
1193                 default:
1194                         return (ENOTTY);
1195                 }
1196         }
1197
1198         switch (cmd) {
1199         case DIGIIO_GETALTPIN:
1200                 *(int *)data = !!(port->dsr == sc->csigs->cd);
1201                 return (0);
1202
1203         case DIGIIO_SETALTPIN:
1204                 if (!port->laltpin) {
1205                         if (*(int *)data) {
1206                                 DLOG(DIGIDB_SET, (sc->dev,
1207                                     "port%d: ALTPIN set\n", pnum));
1208                                 port->cd = sc->csigs->dsr;
1209                                 port->dsr = sc->csigs->cd;
1210                         } else {
1211                                 DLOG(DIGIDB_SET, (sc->dev,
1212                                     "port%d: ALTPIN cleared\n", pnum));
1213                                 port->cd = sc->csigs->cd;
1214                                 port->dsr = sc->csigs->dsr;
1215                         }
1216                 }
1217                 return (0);
1218         }
1219
1220         tp = port->tp;
1221 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1222         term = tp->t_termios;
1223         oldcmd = cmd;
1224         error = ttsetcompat(tp, &cmd, data, &term);
1225         if (error != 0)
1226                 return (error);
1227         if (cmd != oldcmd)
1228                 data = (caddr_t) & term;
1229 #endif
1230         if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1231                 int cc;
1232                 struct termios *dt;
1233                 struct termios *lt;
1234
1235                 dt = (struct termios *)data;
1236                 lt = (mynor & CALLOUT_MASK) ? &port->lt_out : &port->lt_in;
1237
1238                 dt->c_iflag =
1239                     (tp->t_iflag & lt->c_iflag) | (dt->c_iflag & ~lt->c_iflag);
1240                 dt->c_oflag =
1241                     (tp->t_oflag & lt->c_oflag) | (dt->c_oflag & ~lt->c_oflag);
1242                 dt->c_cflag =
1243                     (tp->t_cflag & lt->c_cflag) | (dt->c_cflag & ~lt->c_cflag);
1244                 dt->c_lflag =
1245                     (tp->t_lflag & lt->c_lflag) | (dt->c_lflag & ~lt->c_lflag);
1246                 port->c_iflag = dt->c_iflag & (IXOFF | IXON | IXANY);
1247                 dt->c_iflag &= ~(IXOFF | IXON | IXANY);
1248                 for (cc = 0; cc < NCCS; ++cc)
1249                         if (lt->c_cc[cc] != 0)
1250                                 dt->c_cc[cc] = tp->t_cc[cc];
1251                 if (lt->c_ispeed != 0)
1252                         dt->c_ispeed = tp->t_ispeed;
1253                 if (lt->c_ospeed != 0)
1254                         dt->c_ospeed = tp->t_ospeed;
1255         }
1256         error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, td);
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         s = spltty();
1263         error = ttioctl(tp, cmd, data, flag);
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                 splx(s);
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(td);
1307                 if (error != 0) {
1308                         splx(s);
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                 splx(s);
1325                 return (ENOTTY);
1326         }
1327         splx(s);
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 s;
1343         int window;
1344
1345         mynor = minor(tp->t_dev);
1346         unit = MINOR_TO_UNIT(mynor);
1347         pnum = MINOR_TO_PORT(mynor);
1348
1349         sc = (struct digi_softc *)devclass_get_softc(digi_devclass, unit);
1350         KASSERT(sc, ("digi%d: softc not allocated in digiparam\n", unit));
1351
1352         port = &sc->ports[pnum];
1353
1354         DLOG(DIGIDB_SET, (sc->dev, "port%d: setting parameters\n", pnum));
1355
1356         if (t->c_ispeed == 0)
1357                 t->c_ispeed = t->c_ospeed;
1358
1359         cflag = ttspeedtab(t->c_ospeed, digispeedtab);
1360
1361         if (cflag < 0 || (cflag > 0 && t->c_ispeed != t->c_ospeed))
1362                 return (EINVAL);
1363
1364         s = splclock();
1365
1366         window = sc->window;
1367         sc->setwin(sc, 0);
1368
1369         if (cflag == 0) {                               /* hangup */
1370                 DLOG(DIGIDB_SET, (sc->dev, "port%d: hangup\n", pnum));
1371                 digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIC);
1372         } else {
1373                 digimctl(port, TIOCM_DTR | TIOCM_RTS, DMBIS);
1374
1375                 DLOG(DIGIDB_SET, (sc->dev, "port%d: CBAUD = %d\n", pnum,
1376                     cflag));
1377
1378 #if 0
1379                 /* convert flags to sysV-style values */
1380                 if (t->c_cflag & PARODD)
1381                         cflag |= 0x0200;
1382                 if (t->c_cflag & PARENB)
1383                         cflag |= 0x0100;
1384                 if (t->c_cflag & CSTOPB)
1385                         cflag |= 0x0080;
1386 #else
1387                 /* convert flags to sysV-style values */
1388                 if (t->c_cflag & PARODD)
1389                         cflag |= FEP_PARODD;
1390                 if (t->c_cflag & PARENB)
1391                         cflag |= FEP_PARENB;
1392                 if (t->c_cflag & CSTOPB)
1393                         cflag |= FEP_CSTOPB;
1394                 if (t->c_cflag & CLOCAL)
1395                         cflag |= FEP_CLOCAL;
1396 #endif
1397
1398                 cflag |= (t->c_cflag & CSIZE) >> 4;
1399                 DLOG(DIGIDB_SET, (sc->dev, "port%d: CFLAG = 0x%x\n", pnum,
1400                     cflag));
1401                 fepcmd_w(port, SETCFLAGS, (unsigned)cflag, 0);
1402         }
1403
1404         iflag =
1405             t->c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP);
1406         if (port->c_iflag & IXON)
1407                 iflag |= 0x400;
1408         if (port->c_iflag & IXANY)
1409                 iflag |= 0x800;
1410         if (port->c_iflag & IXOFF)
1411                 iflag |= 0x1000;
1412
1413         DLOG(DIGIDB_SET, (sc->dev, "port%d: set iflag = 0x%x\n", pnum, iflag));
1414         fepcmd_w(port, SETIFLAGS, (unsigned)iflag, 0);
1415
1416         hflow = 0;
1417         if (t->c_cflag & CDTR_IFLOW)
1418                 hflow |= sc->csigs->dtr;
1419         if (t->c_cflag & CRTS_IFLOW)
1420                 hflow |= sc->csigs->rts;
1421         if (t->c_cflag & CCTS_OFLOW)
1422                 hflow |= sc->csigs->cts;
1423         if (t->c_cflag & CDSR_OFLOW)
1424                 hflow |= port->dsr;
1425         if (t->c_cflag & CCAR_OFLOW)
1426                 hflow |= port->cd;
1427
1428         DLOG(DIGIDB_SET, (sc->dev, "port%d: set hflow = 0x%x\n", pnum, hflow));
1429         fepcmd_w(port, SETHFLOW, 0xff00 | (unsigned)hflow, 0);
1430
1431         DLOG(DIGIDB_SET, (sc->dev, "port%d: set startc(0x%x), stopc(0x%x)\n",
1432             pnum, t->c_cc[VSTART], t->c_cc[VSTOP]));
1433         fepcmd_b(port, SONOFFC, t->c_cc[VSTART], t->c_cc[VSTOP], 0);
1434
1435         if (sc->window != 0)
1436                 sc->towin(sc, 0);
1437         if (window != 0)
1438                 sc->towin(sc, window);
1439         splx(s);
1440
1441         return (0);
1442 }
1443
1444 static void
1445 digi_intr(void *vp)
1446 {
1447         struct digi_p *port;
1448         char *cxcon;
1449         struct digi_softc *sc;
1450         int ehead, etail;
1451         volatile struct board_chan *bc;
1452         struct tty *tp;
1453         int head, tail;
1454         int wrapmask;
1455         int size, window;
1456         struct event {
1457                 u_char pnum;
1458                 u_char event;
1459                 u_char mstat;
1460                 u_char lstat;
1461         } event;
1462
1463         sc = vp;
1464
1465         if (sc->status != DIGI_STATUS_ENABLED) {
1466                 DLOG(DIGIDB_IRQ, (sc->dev, "interrupt on disabled board !\n"));
1467                 return;
1468         }
1469
1470 #ifdef DIGI_INTERRUPT
1471         microtime(&sc->intr_timestamp);
1472 #endif
1473
1474         window = sc->window;
1475         sc->setwin(sc, 0);
1476
1477         if (sc->model >= PCXEM && W(sc->vmem + 0xd00)) {
1478                 struct con_bios *con = con_bios_list;
1479                 u_char *ptr;
1480
1481                 ptr = sc->vmem + W(sc->vmem + 0xd00);
1482                 while (con) {
1483                         if (ptr[1] && W(ptr + 2) == W(con->bios + 2))
1484                                 /* Not first block -- exact match */
1485                                 break;
1486
1487                         if (W(ptr + 4) >= W(con->bios + 4) &&
1488                             W(ptr + 4) <= W(con->bios + 6))
1489                                 /* Initial search concetrator BIOS */
1490                                 break;
1491                 }
1492
1493                 if (con == NULL) {
1494                         log(LOG_ERR, "digi%d: wanted bios LREV = 0x%04x"
1495                             " not found!\n", sc->res.unit, W(ptr + 4));
1496                         W(ptr + 10) = 0;
1497                         W(sc->vmem + 0xd00) = 0;
1498                         goto eoi;
1499                 }
1500                 cxcon = con->bios;
1501                 W(ptr + 4) = W(cxcon + 4);
1502                 W(ptr + 6) = W(cxcon + 6);
1503                 if (ptr[1] == 0)
1504                         W(ptr + 2) = W(cxcon + 2);
1505                 W(ptr + 8) = (ptr[1] << 6) + W(cxcon + 8);
1506                 size = W(cxcon + 10) - (ptr[1] << 10);
1507                 if (size <= 0) {
1508                         W(ptr + 8) = W(cxcon + 8);
1509                         W(ptr + 10) = 0;
1510                 } else {
1511                         if (size > 1024)
1512                                 size = 1024;
1513                         W(ptr + 10) = size;
1514                         bcopy(cxcon + (ptr[1] << 10), ptr + 12, size);
1515                 }
1516                 W(sc->vmem + 0xd00) = 0;
1517                 goto eoi;
1518         }
1519
1520         ehead = sc->gdata->ein;
1521         etail = sc->gdata->eout;
1522         if (ehead == etail) {
1523 #ifdef DEBUG
1524                 sc->intr_count++;
1525                 if (sc->intr_count % 6000 == 0) {
1526                         DLOG(DIGIDB_IRQ, (sc->dev,
1527                             "6000 useless polls %x %x\n", ehead, etail));
1528                         sc->intr_count = 0;
1529                 }
1530 #endif
1531                 goto eoi;
1532         }
1533         while (ehead != etail) {
1534                 event = *(volatile struct event *)(sc->memevent + etail);
1535
1536                 etail = (etail + 4) & sc->gdata->imax;
1537
1538                 if (event.pnum >= sc->numports) {
1539                         log(LOG_ERR, "digi%d: port %d: got event"
1540                             " on nonexisting port\n", sc->res.unit,
1541                             event.pnum);
1542                         continue;
1543                 }
1544                 port = &sc->ports[event.pnum];
1545                 bc = port->bc;
1546                 tp = port->tp;
1547
1548                 if (!(tp->t_state & TS_ISOPEN) && !port->wopeners) {
1549                         DLOG(DIGIDB_IRQ, (sc->dev,
1550                             "port %d: event 0x%x on closed port\n",
1551                             event.pnum, event.event));
1552                         bc->rout = bc->rin;
1553                         bc->idata = 0;
1554                         bc->iempty = 0;
1555                         bc->ilow = 0;
1556                         bc->mint = 0;
1557                         continue;
1558                 }
1559                 if (event.event & ~ALL_IND)
1560                         log(LOG_ERR, "digi%d: port%d: ? event 0x%x mstat 0x%x"
1561                             " lstat 0x%x\n", sc->res.unit, event.pnum,
1562                             event.event, event.mstat, event.lstat);
1563
1564                 if (event.event & DATA_IND) {
1565                         DLOG(DIGIDB_IRQ, (sc->dev, "port %d: DATA_IND\n",
1566                             event.pnum));
1567                         wrapmask = port->rxbufsize - 1;
1568                         head = bc->rin;
1569                         tail = bc->rout;
1570
1571                         size = 0;
1572                         if (!(tp->t_state & TS_ISOPEN)) {
1573                                 bc->rout = head;
1574                                 goto end_of_data;
1575                         }
1576                         while (head != tail) {
1577                                 int top;
1578
1579                                 DLOG(DIGIDB_INT, (sc->dev,
1580                                     "port %d: p rx head = %d tail = %d\n",
1581                                     event.pnum, head, tail));
1582                                 top = (head > tail) ? head : wrapmask + 1;
1583                                 sc->towin(sc, port->rxwin);
1584                                 size = top - tail;
1585                                 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1586                                         size = b_to_q((char *)port->rxbuf +
1587                                             tail, size, &tp->t_rawq);
1588                                         tail = top - size;
1589                                         ttwakeup(tp);
1590                                 } else for (; tail < top;) {
1591                                         linesw[tp->t_line].
1592                                             l_rint(port->rxbuf[tail], tp);
1593                                         sc->towin(sc, port->rxwin);
1594                                         size--;
1595                                         tail++;
1596                                         if (tp->t_state & TS_TBLOCK)
1597                                                 break;
1598                                 }
1599                                 tail &= wrapmask;
1600                                 sc->setwin(sc, 0);
1601                                 bc->rout = tail;
1602                                 head = bc->rin;
1603                                 if (size)
1604                                         break;
1605                         }
1606
1607                         if (bc->orun) {
1608                                 CE_RECORD(port, CE_OVERRUN);
1609                                 log(LOG_ERR, "digi%d: port%d: %s\n",
1610                                     sc->res.unit, event.pnum,
1611                                     digi_errortxt(CE_OVERRUN));
1612                                 bc->orun = 0;
1613                         }
1614 end_of_data:
1615                         if (size) {
1616                                 tp->t_state |= TS_TBLOCK;
1617                                 port->status |= PAUSE_RX;
1618                                 DLOG(DIGIDB_RX, (sc->dev, "port %d: pause RX\n",
1619                                     event.pnum));
1620                         } else {
1621                                 bc->idata = 1;
1622                         }
1623                 }
1624
1625                 if (event.event & MODEMCHG_IND) {
1626                         DLOG(DIGIDB_MODEM, (sc->dev, "port %d: MODEMCHG_IND\n",
1627                             event.pnum));
1628
1629                         if ((event.mstat ^ event.lstat) & port->cd) {
1630                                 sc->hidewin(sc);
1631                                 linesw[tp->t_line].l_modem
1632                                     (tp, event.mstat & port->cd);
1633                                 sc->setwin(sc, 0);
1634                                 wakeup(TSA_CARR_ON(tp));
1635                         }
1636
1637                         if (event.mstat & sc->csigs->ri) {
1638                                 DLOG(DIGIDB_RI, (sc->dev, "port %d: RING\n",
1639                                     event.pnum));
1640                                 if (port->send_ring) {
1641                                         linesw[tp->t_line].l_rint('R', tp);
1642                                         linesw[tp->t_line].l_rint('I', tp);
1643                                         linesw[tp->t_line].l_rint('N', tp);
1644                                         linesw[tp->t_line].l_rint('G', tp);
1645                                         linesw[tp->t_line].l_rint('\r', tp);
1646                                         linesw[tp->t_line].l_rint('\n', tp);
1647                                 }
1648                         }
1649                 }
1650                 if (event.event & BREAK_IND) {
1651                         DLOG(DIGIDB_MODEM, (sc->dev, "port %d: BREAK_IND\n",
1652                             event.pnum));
1653                         linesw[tp->t_line].l_rint(TTY_BI, tp);
1654                 }
1655                 if (event.event & (LOWTX_IND | EMPTYTX_IND)) {
1656                         DLOG(DIGIDB_IRQ, (sc->dev, "port %d:%s%s\n",
1657                             event.pnum,
1658                             event.event & LOWTX_IND ? " LOWTX" : "",
1659                             event.event & EMPTYTX_IND ?  " EMPTYTX" : ""));
1660                         (*linesw[tp->t_line].l_start)(tp);
1661                 }
1662         }
1663         sc->gdata->eout = etail;
1664 eoi:
1665         if (sc->window != 0)
1666                 sc->towin(sc, 0);
1667         if (window != 0)
1668                 sc->towin(sc, window);
1669 }
1670
1671 static void
1672 digistart(struct tty *tp)
1673 {
1674         int unit;
1675         int pnum;
1676         struct digi_p *port;
1677         struct digi_softc *sc;
1678         volatile struct board_chan *bc;
1679         int head, tail;
1680         int size, ocount, totcnt = 0;
1681         int s;
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         s = spltty();
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         splx(s);
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_sw,
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                 free(sc->ports, M_TTYS);
1883                 sc->ports = NULL;
1884                 free(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
1922         return (0);
1923 }
1924
1925 int
1926 digi_shutdown(device_t dev)
1927 {
1928         return (0);
1929 }
1930
1931 MODULE_VERSION(digi, 1);