]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/digi/digi.c
This commit was generated by cvs2svn to compensate for changes in r136647,
[FreeBSD/FreeBSD.git] / sys / dev / 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$
30  */
31
32 /*-
33  * TODO:
34  *      Figure out what the con bios stuff is supposed to do
35  *      Test with *LOTS* more cards - I only have a PCI8r and an ISA Xem.
36  */
37
38 #include "opt_compat.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/conf.h>
44 #include <sys/linker.h>
45 #include <sys/kernel.h>
46 #include <sys/mbuf.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/tty.h>
50 #include <sys/syslog.h>
51 #include <sys/fcntl.h>
52 #include <sys/serial.h>
53 #include <sys/bus.h>
54 #include <machine/resource.h>
55
56 #include <sys/digiio.h>
57 #include <dev/digi/digireg.h>
58 #include <dev/digi/digi.h>
59 #include <dev/digi/digi_mod.h>
60 #include <dev/digi/digi_pci.h>
61
62 static t_open_t         digiopen;
63 static d_open_t         digicopen;
64 static d_close_t        digicclose;
65 static t_ioctl_t        digiioctl;
66 static d_ioctl_t        digisioctl;
67 static d_ioctl_t        digicioctl;
68
69 static void     digistop(struct tty *tp, int rw);
70 static void     digibreak(struct tty *tp, int brk);
71 static int      digimodem(struct tty *tp, int sigon, int sigoff);
72 static void     digi_poll(void *ptr);
73 static void     digi_freemoduledata(struct digi_softc *);
74 static void     fepcmd(struct digi_p *port, int cmd, int op, int ncmds);
75 static void     digistart(struct tty *tp);
76 static int      digiparam(struct tty *tp, struct termios *t);
77 static void     digiclose(struct tty *tp);
78 static void     digi_intr(void *);
79 static int      digi_init(struct digi_softc *_sc);
80 static int      digi_loadmoduledata(struct digi_softc *);
81 static int      digi_inuse(struct digi_softc *);
82 static void     digi_free_state(struct digi_softc *);
83
84 #define fepcmd_b(port, cmd, op1, op2, ncmds) \
85         fepcmd(port, cmd, (op2 << 8) | op1, ncmds)
86 #define fepcmd_w        fepcmd
87
88
89 static speed_t digidefaultrate = TTYDEF_SPEED;
90
91 struct con_bios {
92         struct con_bios *next;
93         u_char *bios;
94         size_t size;
95 };
96
97 static struct con_bios *con_bios_list;
98 devclass_t       digi_devclass;
99 static char      driver_name[] = "digi";
100 unsigned         digi_debug = 0;
101
102 static struct speedtab digispeedtab[] = {
103         { 0,            0},                     /* old (sysV-like) Bx codes */
104         { 50,           1},
105         { 75,           2},
106         { 110,          3},
107         { 134,          4},
108         { 150,          5},
109         { 200,          6},
110         { 300,          7},
111         { 600,          8},
112         { 1200,         9},
113         { 1800,         10},
114         { 2400,         11},
115         { 4800,         12},
116         { 9600,         13},
117         { 19200,        14},
118         { 38400,        15},
119         { 57600,        (02000 | 1)},
120         { 76800,        (02000 | 2)},
121         { 115200,       (02000 | 3)},
122         { 230400,       (02000 | 6)},
123         { -1,           -1}
124 };
125
126 const struct digi_control_signals digi_xixe_signals = {
127         0x02, 0x08, 0x10, 0x20, 0x40, 0x80
128 };
129
130 const struct digi_control_signals digi_normal_signals = {
131         0x02, 0x80, 0x20, 0x10, 0x40, 0x01
132 };
133
134 static struct cdevsw digi_csw = {
135         .d_version =    D_VERSION,
136         .d_open =       digicopen,
137         .d_close =      digicclose,
138         .d_ioctl =      digicioctl,
139         .d_name =       driver_name,
140         .d_flags =      D_TTY | D_NEEDGIANT,
141 };
142
143 static void
144 digi_poll(void *ptr)
145 {
146         struct digi_softc *sc;
147
148         sc = (struct digi_softc *)ptr;
149         callout_handle_init(&sc->callout);
150         digi_intr(sc);
151         sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
152 }
153
154 static void
155 digi_int_test(void *v)
156 {
157         struct digi_softc *sc = v;
158
159         callout_handle_init(&sc->inttest);
160 #ifdef DIGI_INTERRUPT
161         if (sc->intr_timestamp.tv_sec || sc->intr_timestamp.tv_usec) {
162                 /* interrupt OK! */
163                 return;
164         }
165         log(LOG_ERR, "digi%d: Interrupt didn't work, use polled mode\n", unit);
166 #endif
167         sc->callout = timeout(digi_poll, sc, (hz >= 200) ? hz / 100 : 1);
168 }
169
170 static void
171 digi_freemoduledata(struct digi_softc *sc)
172 {
173         if (sc->fep.data != NULL) {
174                 free(sc->fep.data, M_TTYS);
175                 sc->fep.data = NULL;
176         }
177         if (sc->link.data != NULL) {
178                 free(sc->link.data, M_TTYS);
179                 sc->link.data = NULL;
180         }
181         if (sc->bios.data != NULL) {
182                 free(sc->bios.data, M_TTYS);
183                 sc->bios.data = NULL;
184         }
185 }
186
187 static int
188 digi_bcopy(const void *vfrom, void *vto, size_t sz)
189 {
190         volatile const char *from = (volatile const char *)vfrom;
191         volatile char *to = (volatile char *)vto;
192         size_t i;
193
194         for (i = 0; i < sz; i++)
195                 *to++ = *from++;
196
197         from = (const volatile char *)vfrom;
198         to = (volatile char *)vto;
199         for (i = 0; i < sz; i++)
200                 if (*to++ != *from++)
201                         return (0);
202         return (1);
203 }
204
205 void
206 digi_delay(struct digi_softc *sc, const char *txt, u_long timo)
207 {
208         if (cold)
209                 DELAY(timo * 1000000 / hz);
210         else
211                 tsleep(sc, PUSER | PCATCH, txt, timo);
212 }
213
214 static int
215 digi_init(struct digi_softc *sc)
216 {
217         int i, cnt, resp;
218         u_char *ptr;
219         int lowwater;
220         struct digi_p *port;
221         volatile struct board_chan *bc;
222         struct tty *tp;
223
224         ptr = NULL;
225
226         if (sc->status == DIGI_STATUS_DISABLED) {
227                 log(LOG_ERR, "digi%d: Cannot init a disabled card\n",
228                     sc->res.unit);
229                 return (EIO);
230         }
231         if (sc->bios.data == NULL) {
232                 log(LOG_ERR, "digi%d: Cannot init without BIOS\n",
233                     sc->res.unit);
234                 return (EIO);
235         }
236 #if 0
237         if (sc->link.data == NULL && sc->model >= PCCX) {
238                 log(LOG_ERR, "digi%d: Cannot init without link info\n",
239                     sc->res.unit);
240                 return (EIO);
241         }
242 #endif
243         if (sc->fep.data == NULL) {
244                 log(LOG_ERR, "digi%d: Cannot init without fep code\n",
245                     sc->res.unit);
246                 return (EIO);
247         }
248         sc->status = DIGI_STATUS_NOTINIT;
249
250         if (sc->numports) {
251                 /*
252                  * We're re-initialising - maybe because someone's attached
253                  * another port module.  For now, we just re-initialise
254                  * everything.
255                  */
256                 if (digi_inuse(sc))
257                         return (EBUSY);
258
259                 digi_free_state(sc);
260         }
261
262         ptr = sc->setwin(sc, MISCGLOBAL);
263         for (i = 0; i < 16; i += 2)
264                 vW(ptr + i) = 0;
265
266         switch (sc->model) {
267         case PCXEVE:
268                 outb(sc->wport, 0xff);          /* window 7 */
269                 ptr = sc->vmem + (BIOSCODE & 0x1fff);
270
271                 if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
272                         device_printf(sc->dev, "BIOS upload failed\n");
273                         return (EIO);
274                 }
275
276                 outb(sc->port, FEPCLR);
277                 break;
278
279         case PCXE:
280         case PCXI:
281         case PCCX:
282                 ptr = sc->setwin(sc, BIOSCODE + ((0xf000 - sc->mem_seg) << 4));
283                 if (!digi_bcopy(sc->bios.data, ptr, sc->bios.size)) {
284                         device_printf(sc->dev, "BIOS upload failed\n");
285                         return (EIO);
286                 }
287                 break;
288
289         case PCXEM:
290         case PCIEPCX:
291         case PCIXR:
292                 if (sc->pcibus)
293                         PCIPORT = FEPRST;
294                 else
295                         outb(sc->port, FEPRST | FEPMEM);
296
297                 for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) &
298                     FEPMASK) != FEPRST; i++) {
299                         if (i > hz) {
300                                 log(LOG_ERR, "digi%d: %s init reset failed\n",
301                                     sc->res.unit, sc->name);
302                                 return (EIO);
303                         }
304                         digi_delay(sc, "digiinit0", 5);
305                 }
306                 DLOG(DIGIDB_INIT, (sc->dev, "Got init reset after %d us\n", i));
307
308                 /* Now upload the BIOS */
309                 cnt = (sc->bios.size < sc->win_size - BIOSOFFSET) ?
310                     sc->bios.size : sc->win_size - BIOSOFFSET;
311
312                 ptr = sc->setwin(sc, BIOSOFFSET);
313                 if (!digi_bcopy(sc->bios.data, ptr, cnt)) {
314                         device_printf(sc->dev, "BIOS upload (1) failed\n");
315                         return (EIO);
316                 }
317
318                 if (cnt != sc->bios.size) {
319                         /* and the second part */
320                         ptr = sc->setwin(sc, sc->win_size);
321                         if (!digi_bcopy(sc->bios.data + cnt, ptr,
322                             sc->bios.size - cnt)) {
323                                 device_printf(sc->dev, "BIOS upload failed\n");
324                                 return (EIO);
325                         }
326                 }
327
328                 ptr = sc->setwin(sc, 0);
329                 vW(ptr + 0) = 0x0401;
330                 vW(ptr + 2) = 0x0bf0;
331                 vW(ptr + 4) = 0x0000;
332                 vW(ptr + 6) = 0x0000;
333
334                 break;
335         }
336
337         DLOG(DIGIDB_INIT, (sc->dev, "BIOS uploaded\n"));
338
339         ptr = sc->setwin(sc, MISCGLOBAL);
340         W(ptr) = 0;
341
342         if (sc->pcibus) {
343                 PCIPORT = FEPCLR;
344                 resp = FEPRST;
345         } else if (sc->model == PCXEVE) {
346                 outb(sc->port, FEPCLR);
347                 resp = FEPRST;
348         } else {
349                 outb(sc->port, FEPCLR | FEPMEM);
350                 resp = FEPRST | FEPMEM;
351         }
352
353         for (i = 0; ((sc->pcibus ? PCIPORT : inb(sc->port)) & FEPMASK)
354             == resp; i++) {
355                 if (i > hz) {
356                         log(LOG_ERR, "digi%d: BIOS start failed\n",
357                             sc->res.unit);
358                         return (EIO);
359                 }
360                 digi_delay(sc, "digibios0", 5);
361         }
362
363         DLOG(DIGIDB_INIT, (sc->dev, "BIOS started after %d us\n", i));
364
365         for (i = 0; vW(ptr) != *(u_short *)"GD"; i++) {
366                 if (i > 2*hz) {
367                         log(LOG_ERR, "digi%d: BIOS boot failed "
368                             "(0x%02x != 0x%02x)\n",
369                             sc->res.unit, vW(ptr), *(u_short *)"GD");
370                         return (EIO);
371                 }
372                 digi_delay(sc, "digibios1", 5);
373         }
374
375         DLOG(DIGIDB_INIT, (sc->dev, "BIOS booted after %d iterations\n", i));
376
377         if (sc->link.data != NULL) {
378                 DLOG(DIGIDB_INIT, (sc->dev, "Loading link data\n"));
379                 ptr = sc->setwin(sc, 0xcd0);
380                 digi_bcopy(sc->link.data, ptr, 21);     /* XXX 21 ? */
381         }
382
383         /* load FEP/OS */
384
385         switch (sc->model) {
386         case PCXE:
387         case PCXEVE:
388         case PCXI:
389                 ptr = sc->setwin(sc, sc->model == PCXI ? 0x2000 : 0x0);
390                 digi_bcopy(sc->fep.data, ptr, sc->fep.size);
391
392                 /* A BIOS request to move our data to 0x2000 */
393                 ptr = sc->setwin(sc, MBOX);
394                 vW(ptr + 0) = 2;
395                 vW(ptr + 2) = sc->mem_seg + FEPCODESEG;
396                 vW(ptr + 4) = 0;
397                 vW(ptr + 6) = FEPCODESEG;
398                 vW(ptr + 8) = 0;
399                 vW(ptr + 10) = sc->fep.size;
400
401                 /* Run the BIOS request */
402                 outb(sc->port, FEPREQ | FEPMEM);
403                 outb(sc->port, FEPCLR | FEPMEM);
404
405                 for (i = 0; W(ptr); i++) {
406                         if (i > hz) {
407                                 log(LOG_ERR, "digi%d: FEP/OS move failed\n",
408                                     sc->res.unit);
409                                 sc->hidewin(sc);
410                                 return (EIO);
411                         }
412                         digi_delay(sc, "digifep0", 5);
413                 }
414                 DLOG(DIGIDB_INIT,
415                     (sc->dev, "FEP/OS moved after %d iterations\n", i));
416
417                 /* Clear the confirm word */
418                 ptr = sc->setwin(sc, FEPSTAT);
419                 vW(ptr + 0) = 0;
420
421                 /* A BIOS request to execute the FEP/OS */
422                 ptr = sc->setwin(sc, MBOX);
423                 vW(ptr + 0) = 0x01;
424                 vW(ptr + 2) = FEPCODESEG;
425                 vW(ptr + 4) = 0x04;
426
427                 /* Run the BIOS request */
428                 outb(sc->port, FEPREQ);
429                 outb(sc->port, FEPCLR);
430
431                 ptr = sc->setwin(sc, FEPSTAT);
432
433                 break;
434
435         case PCXEM:
436         case PCIEPCX:
437         case PCIXR:
438                 DLOG(DIGIDB_INIT, (sc->dev, "Loading FEP/OS\n"));
439
440                 cnt = (sc->fep.size < sc->win_size - BIOSOFFSET) ?
441                     sc->fep.size : sc->win_size - BIOSOFFSET;
442
443                 ptr = sc->setwin(sc, BIOSOFFSET);
444                 digi_bcopy(sc->fep.data, ptr, cnt);
445
446                 if (cnt != sc->fep.size) {
447                         ptr = sc->setwin(sc, BIOSOFFSET + cnt);
448                         digi_bcopy(sc->fep.data + cnt, ptr,
449                             sc->fep.size - cnt);
450                 }
451
452                 DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS loaded\n"));
453
454                 ptr = sc->setwin(sc, 0xc30);
455                 W(ptr + 4) = 0x1004;
456                 W(ptr + 6) = 0xbfc0;
457                 W(ptr + 0) = 0x03;
458                 W(ptr + 2) = 0x00;
459
460                 /* Clear the confirm word */
461                 ptr = sc->setwin(sc, FEPSTAT);
462                 W(ptr + 0) = 0;
463
464                 if (sc->port)
465                         outb(sc->port, 0);              /* XXX necessary ? */
466
467                 break;
468
469         case PCCX:
470                 ptr = sc->setwin(sc, 0xd000);
471                 digi_bcopy(sc->fep.data, ptr, sc->fep.size);
472
473                 /* A BIOS request to execute the FEP/OS */
474                 ptr = sc->setwin(sc, 0xc40);
475                 W(ptr + 0) = 1;
476                 W(ptr + 2) = FEPCODE >> 4;
477                 W(ptr + 4) = 4;
478
479                 /* Clear the confirm word */
480                 ptr = sc->setwin(sc, FEPSTAT);
481                 W(ptr + 0) = 0;
482
483                 /* Run the BIOS request */
484                 outb(sc->port, FEPREQ | FEPMEM); /* send interrupt to BIOS */
485                 outb(sc->port, FEPCLR | FEPMEM);
486                 break;
487         }
488
489         /* Now wait 'till the FEP/OS has booted */
490         for (i = 0; vW(ptr) != *(u_short *)"OS"; i++) {
491                 if (i > 2*hz) {
492                         log(LOG_ERR, "digi%d: FEP/OS start failed "
493                             "(0x%02x != 0x%02x)\n",
494                             sc->res.unit, vW(ptr), *(u_short *)"OS");
495                         sc->hidewin(sc);
496                         return (EIO);
497                 }
498                 digi_delay(sc, "digifep1", 5);
499         }
500
501         DLOG(DIGIDB_INIT, (sc->dev, "FEP/OS started after %d iterations\n", i));
502
503         if (sc->model >= PCXEM) {
504                 ptr = sc->setwin(sc, 0xe04);
505                 vW(ptr) = 2;
506                 ptr = sc->setwin(sc, 0xc02);
507                 sc->numports = vW(ptr);
508         } else {
509                 ptr = sc->setwin(sc, 0xc22);
510                 sc->numports = vW(ptr);
511         }
512
513         if (sc->numports == 0) {
514                 device_printf(sc->dev, "%s, 0 ports found\n", sc->name);
515                 sc->hidewin(sc);
516                 return (0);
517         }
518
519         device_printf(sc->dev, "%s, %d ports found\n", sc->name, sc->numports);
520
521         if (sc->ports)
522                 free(sc->ports, M_TTYS);
523         sc->ports = malloc(sizeof(struct digi_p) * sc->numports,
524             M_TTYS, M_WAITOK | M_ZERO);
525
526         /*
527          * XXX Should read port 0xc90 for an array of 2byte values, 1 per
528          * port.  If the value is 0, the port is broken....
529          */
530
531         ptr = sc->setwin(sc, 0);
532
533         /* We should now init per-port structures */
534         bc = (volatile struct board_chan *)(ptr + CHANSTRUCT);
535         sc->gdata = (volatile struct global_data *)(ptr + FEP_GLOBAL);
536
537         sc->memcmd = ptr + sc->gdata->cstart;
538         sc->memevent = ptr + sc->gdata->istart;
539
540         for (i = 0; i < sc->numports; i++, bc++) {
541                 port = sc->ports + i;
542                 port->pnum = i;
543                 port->sc = sc;
544                 port->status = ENABLED;
545                 port->bc = bc;
546                 tp = port->tp = ttyalloc();
547                 tp->t_oproc = digistart;
548                 tp->t_param = digiparam;
549                 tp->t_modem = digimodem;
550                 tp->t_break = digibreak;
551                 tp->t_stop = digistop;
552                 tp->t_cioctl = digisioctl;
553                 tp->t_ioctl = digiioctl;
554                 tp->t_open = digiopen;
555                 tp->t_close = digiclose;
556                 tp->t_sc = port;
557
558                 if (sc->model == PCXEVE) {
559                         port->txbuf = ptr +
560                             (((bc->tseg - sc->mem_seg) << 4) & 0x1fff);
561                         port->rxbuf = ptr +
562                             (((bc->rseg - sc->mem_seg) << 4) & 0x1fff);
563                         port->txwin = FEPWIN | ((bc->tseg - sc->mem_seg) >> 9);
564                         port->rxwin = FEPWIN | ((bc->rseg - sc->mem_seg) >> 9);
565                 } else if (sc->model == PCXI || sc->model == PCXE) {
566                         port->txbuf = ptr + ((bc->tseg - sc->mem_seg) << 4);
567                         port->rxbuf = ptr + ((bc->rseg - sc->mem_seg) << 4);
568                         port->txwin = port->rxwin = 0;
569                 } else {
570                         port->txbuf = ptr +
571                             (((bc->tseg - sc->mem_seg) << 4) % sc->win_size);
572                         port->rxbuf = ptr +
573                             (((bc->rseg - sc->mem_seg) << 4) % sc->win_size);
574                         port->txwin = FEPWIN |
575                             (((bc->tseg - sc->mem_seg) << 4) / sc->win_size);
576                         port->rxwin = FEPWIN |
577                             (((bc->rseg - sc->mem_seg) << 4) / sc->win_size);
578                 }
579                 port->txbufsize = bc->tmax + 1;
580                 port->rxbufsize = bc->rmax + 1;
581
582                 lowwater = port->txbufsize >> 2;
583                 if (lowwater > 1024)
584                         lowwater = 1024;
585                 sc->setwin(sc, 0);
586                 fepcmd_w(port, STXLWATER, lowwater, 10);
587                 fepcmd_w(port, SRXLWATER, port->rxbufsize >> 2, 10);
588                 fepcmd_w(port, SRXHWATER, (3 * port->rxbufsize) >> 2, 10);
589
590                 bc->edelay = 100;
591
592                 /*
593                  * We don't use all the flags from <sys/ttydefaults.h> since
594                  * they are only relevant for logins.  It's important to have
595                  * echo off initially so that the line doesn't start blathering
596                  * before the echo flag can be turned off.
597                  */
598                 tp->t_init_in.c_iflag = 0;
599                 tp->t_init_in.c_oflag = 0;
600                 tp->t_init_in.c_cflag = TTYDEF_CFLAG;
601                 tp->t_init_in.c_lflag = 0;
602                 termioschars(&tp->t_init_in);
603                 tp->t_init_in.c_ispeed = tp->t_init_in.c_ospeed = digidefaultrate;
604                 tp->t_init_out = tp->t_init_in;
605                 port->send_ring = 1;    /* Default action on signal RI */
606                 ttycreate(tp, NULL, 0, MINOR_CALLOUT, "D%r%r", sc->res.unit, i);
607         }
608
609         sc->hidewin(sc);
610         sc->inttest = timeout(digi_int_test, sc, hz);
611         /* fepcmd_w(&sc->ports[0], 0xff, 0, 0); */
612         sc->status = DIGI_STATUS_ENABLED;
613
614         return (0);
615 }
616
617 static int
618 digimodem(struct tty *tp, int sigon, int sigoff)
619 {
620         struct digi_softc *sc;
621         struct digi_p *port;
622         int bitand, bitor, mstat;
623
624         port = tp->t_sc;
625         sc = port->sc;
626
627         if (sigon == 0 && sigoff == 0) {
628                 port->sc->setwin(port->sc, 0);
629                 mstat = port->bc->mstat;
630                 port->sc->hidewin(port->sc);
631                 if (mstat & port->sc->csigs->rts)
632                         sigon |= SER_RTS;
633                 if (mstat & port->cd)
634                         sigon |= SER_DCD;
635                 if (mstat & port->dsr)
636                         sigon |= SER_DSR;
637                 if (mstat & port->sc->csigs->cts)
638                         sigon |= SER_CTS;
639                 if (mstat & port->sc->csigs->ri)
640                         sigon |= SER_RI;
641                 if (mstat & port->sc->csigs->dtr)
642                         sigon |= SER_DTR;
643                 return (sigon);
644         }
645
646         bitand = 0;
647         bitor = 0;
648
649         if (sigoff & SER_DTR)
650                 bitand |= port->sc->csigs->dtr;
651         if (sigoff & SER_RTS)
652                 bitand |= port->sc->csigs->rts;
653         if (sigon & SER_DTR)
654                 bitor |= port->sc->csigs->dtr;
655         if (sigon & SER_RTS)
656                 bitor |= port->sc->csigs->rts;
657         fepcmd_b(port, SETMODEM, bitor, ~bitand, 0);
658         return (0);
659 }
660
661 static int
662 digicopen(struct cdev *dev, int flag, int mode, struct thread *td)
663 {
664         struct digi_softc *sc;
665
666         sc = dev->si_drv1;
667         if (sc->status != DIGI_STATUS_ENABLED) {
668                 DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
669                 return (ENXIO);
670         }
671         sc->opencnt++;
672         return (0);
673 }
674
675 static int
676 digiopen(struct tty *tp, struct cdev *dev)
677 {
678         int error;
679         struct digi_softc *sc;
680         struct digi_p *port;
681         volatile struct board_chan *bc;
682
683         port = tp->t_sc;
684         sc = port->sc;
685
686         if (sc->status != DIGI_STATUS_ENABLED) {
687                 DLOG(DIGIDB_OPEN, (sc->dev, "Cannot open a disabled card\n"));
688                 return (ENXIO);
689         }
690         bc = port->bc;
691
692         /*
693          * The device isn't open, so there are no conflicts.
694          * Initialize it.  Initialization is done twice in many
695          * cases: to preempt sleeping callin opens if we are callout,
696          * and to complete a callin open after DCD rises.
697          */
698         sc->setwin(sc, 0);
699
700         bc->rout = bc->rin;     /* clear input queue */
701         bc->idata = 1;
702         bc->iempty = 1;
703         bc->ilow = 1;
704         bc->mint = port->cd | port->sc->csigs->ri;
705         bc->tin = bc->tout;
706         if (port->ialtpin) {
707                 port->cd = sc->csigs->dsr;
708                 port->dsr = sc->csigs->cd;
709         } else {
710                 port->cd = sc->csigs->cd;
711                 port->dsr = sc->csigs->dsr;
712         }
713         tp->t_wopeners++;                       /* XXX required ? */
714         error = digiparam(tp, &tp->t_termios);
715         tp->t_wopeners--;
716
717         return (error);
718 }
719
720 static int
721 digicclose(struct cdev *dev, int flag, int mode, struct thread *td)
722 {
723         struct digi_softc *sc;
724
725         sc = dev->si_drv1;
726         sc->opencnt--;
727         return (0);
728 }
729
730 static void
731 digidtrwakeup(void *chan)
732 {
733         struct digi_p *port = chan;
734
735         port->status &= ~DIGI_DTR_OFF;
736         wakeup(&port->tp->t_dtr_wait);
737         port->tp->t_wopeners--;
738 }
739
740 static void
741 digiclose(struct tty *tp)
742 {
743         volatile struct board_chan *bc;
744         struct digi_p *port;
745         int s;
746
747         port = tp->t_sc;
748         bc = port->bc;
749
750         s = spltty();
751         port->sc->setwin(port->sc, 0);
752         bc->idata = 0;
753         bc->iempty = 0;
754         bc->ilow = 0;
755         bc->mint = 0;
756         if ((tp->t_cflag & HUPCL) ||
757             (!tp->t_actout && !(bc->mstat & port->cd) &&
758             !(tp->t_init_in.c_cflag & CLOCAL)) ||
759             !(tp->t_state & TS_ISOPEN)) {
760                 digimodem(tp, 0, SER_DTR | SER_RTS);
761                 if (tp->t_dtr_wait != 0) {
762                         /* Schedule a wakeup of any callin devices */
763                         tp->t_wopeners++;
764                         timeout(&digidtrwakeup, port, tp->t_dtr_wait);
765                         port->status |= DIGI_DTR_OFF;
766                 }
767         }
768         tp->t_actout = FALSE;
769         wakeup(&tp->t_actout);
770         wakeup(TSA_CARR_ON(tp));
771         splx(s);
772 }
773
774 /*
775  * Load module "digi_<mod>.ko" and look for a symbol called digi_mod_<mod>.
776  *
777  * Populate sc->bios, sc->fep, and sc->link from this data.
778  *
779  * sc->fep.data, sc->bios.data and sc->link.data are malloc()d according
780  * to their respective sizes.
781  *
782  * The module is unloaded when we're done.
783  */
784 static int
785 digi_loadmoduledata(struct digi_softc *sc)
786 {
787         struct digi_mod *digi_mod;
788         linker_file_t lf;
789         char *modfile, *sym;
790         caddr_t symptr;
791         int modlen, res;
792
793         KASSERT(sc->bios.data == NULL, ("Uninitialised BIOS variable"));
794         KASSERT(sc->fep.data == NULL, ("Uninitialised FEP variable"));
795         KASSERT(sc->link.data == NULL, ("Uninitialised LINK variable"));
796         KASSERT(sc->module != NULL, ("Uninitialised module name"));
797
798         modlen = strlen(sc->module);
799         modfile = malloc(modlen + 6, M_TEMP, M_WAITOK);
800         snprintf(modfile, modlen + 6, "digi_%s", sc->module);
801         res = linker_reference_module(modfile, NULL, &lf);
802         if ((res = linker_reference_module(modfile, NULL, &lf)) != 0)
803                 printf("%s: Failed %d to autoload module\n", modfile, res);
804         free(modfile, M_TEMP);
805         if (res != 0)
806                 return (res);
807
808         sym = malloc(modlen + 10, M_TEMP, M_WAITOK);
809         snprintf(sym, modlen + 10, "digi_mod_%s", sc->module);
810         if ((symptr = linker_file_lookup_symbol(lf, sym, 0)) == NULL)
811                 printf("digi_%s.ko: Symbol `%s' not found\n", sc->module, sym);
812         free(sym, M_TEMP);
813
814         digi_mod = (struct digi_mod *)symptr;
815         if (digi_mod->dm_version != DIGI_MOD_VERSION) {
816                 printf("digi_%s.ko: Invalid version %d (need %d)\n",
817                     sc->module, digi_mod->dm_version, DIGI_MOD_VERSION);
818                 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
819                 return (EINVAL);
820         }
821
822         sc->bios.size = digi_mod->dm_bios.size;
823         if (sc->bios.size != 0 && digi_mod->dm_bios.data != NULL) {
824                 sc->bios.data = malloc(sc->bios.size, M_TTYS, M_WAITOK);
825                 bcopy(digi_mod->dm_bios.data, sc->bios.data, sc->bios.size);
826         }
827
828         sc->fep.size = digi_mod->dm_fep.size;
829         if (sc->fep.size != 0 && digi_mod->dm_fep.data != NULL) {
830                 sc->fep.data = malloc(sc->fep.size, M_TTYS, M_WAITOK);
831                 bcopy(digi_mod->dm_fep.data, sc->fep.data, sc->fep.size);
832         }
833
834         sc->link.size = digi_mod->dm_link.size;
835         if (sc->link.size != 0 && digi_mod->dm_link.data != NULL) {
836                 sc->link.data = malloc(sc->link.size, M_TTYS, M_WAITOK);
837                 bcopy(digi_mod->dm_link.data, sc->link.data, sc->link.size);
838         }
839
840         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
841
842         return (0);
843 }
844
845 static int
846 digisioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
847 {
848         struct digi_p *port;
849         struct digi_softc *sc;
850
851         port = dev->si_drv1;
852         sc = port->sc;
853
854         switch (cmd) {
855         case DIGIIO_GETALTPIN:
856                 if (ISINIT(dev))
857                         *(int *)data = port->ialtpin;
858                 else if (ISLOCK(dev))
859                         *(int *)data = port->laltpin;
860                 else
861                         return (ENOTTY);
862                 break;
863         case DIGIIO_SETALTPIN:
864                 if (ISINIT(dev)) {
865                         if (!port->laltpin) {
866                                 port->ialtpin = !!*(int *)data;
867                                 DLOG(DIGIDB_SET, (sc->dev,
868                                     "port%d: initial ALTPIN %s\n", port->pnum,
869                                     port->ialtpin ? "set" : "cleared"));
870                         }
871                 } else if (ISLOCK(dev)) {
872                         port->laltpin = !!*(int *)data;
873                         DLOG(DIGIDB_SET, (sc->dev,
874                             "port%d: ALTPIN %slocked\n",
875                             port->pnum, port->laltpin ? "" : "un"));
876                 } else
877                         return (ENOTTY);
878                 break;
879         default:
880                 return (ENOTTY);
881         }
882         return (0);
883 }
884
885 static int
886 digicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
887 {
888         int error;
889         struct digi_softc *sc;
890
891         sc = dev->si_drv1;
892
893         if (sc->status == DIGI_STATUS_DISABLED)
894                 return (ENXIO);
895
896         switch (cmd) {
897         case DIGIIO_DEBUG:
898 #ifdef DEBUG
899                 digi_debug = *(int *)data;
900                 return (0);
901 #else
902                 device_printf(sc->dev, "DEBUG not defined\n");
903                 return (ENXIO);
904 #endif
905         case DIGIIO_REINIT:
906                 digi_loadmoduledata(sc);
907                 error = digi_init(sc);
908                 digi_freemoduledata(sc);
909                 return (error);
910
911         case DIGIIO_MODEL:
912                 *(enum digi_model *)data = sc->model;
913                 return (0);
914
915         case DIGIIO_IDENT:
916                 return (copyout(sc->name, *(char **)data,
917                     strlen(sc->name) + 1));
918         default:
919                 return (ENOIOCTL);
920         }
921 }
922
923 static int
924 digiioctl(struct tty *tp, u_long cmd, void *data, int flag, struct thread *td)
925 {
926         struct digi_softc *sc;
927         struct digi_p *port;
928
929         port = tp->t_sc;
930         sc = port->sc;
931         if (sc->status == DIGI_STATUS_DISABLED)
932                 return (ENXIO);
933
934         if (!(port->status & ENABLED))
935                 return (ENXIO);
936
937         switch (cmd) {
938         case DIGIIO_GETALTPIN:
939                 *(int *)data = !!(port->dsr == sc->csigs->cd);
940                 return (0);
941
942         case DIGIIO_SETALTPIN:
943                 if (!port->laltpin) {
944                         if (*(int *)data) {
945                                 DLOG(DIGIDB_SET, (sc->dev,
946                                     "port%d: ALTPIN set\n", port->pnum));
947                                 port->cd = sc->csigs->dsr;
948                                 port->dsr = sc->csigs->cd;
949                         } else {
950                                 DLOG(DIGIDB_SET, (sc->dev,
951                                     "port%d: ALTPIN cleared\n", port->pnum));
952                                 port->cd = sc->csigs->cd;
953                                 port->dsr = sc->csigs->dsr;
954                         }
955                 }
956                 return (0);
957         case DIGIIO_RING:
958                 port->send_ring = *(u_char *)data;
959                 break;
960         default:
961                 return (ENOTTY);
962         }
963         return (0);
964 }
965
966 static void
967 digibreak(struct tty *tp, int brk)
968 {
969         struct digi_p *port;
970
971         port = tp->t_sc;
972
973         /*
974          * now it sends 400 millisecond break because I don't know
975          * how to send an infinite break
976          */
977         if (brk)
978                 fepcmd_w(port, SENDBREAK, 400, 10);
979 }
980
981 static int
982 digiparam(struct tty *tp, struct termios *t)
983 {
984         struct digi_softc *sc;
985         struct digi_p *port;
986         int cflag;
987         int iflag;
988         int hflow;
989         int s;
990         int window;
991
992         port = tp->t_sc;
993         sc = port->sc;
994         DLOG(DIGIDB_SET, (sc->dev, "port%d: setting parameters\n", port->pnum));
995
996         if (t->c_ispeed == 0)
997                 t->c_ispeed = t->c_ospeed;
998
999         cflag = ttspeedtab(t->c_ospeed, digispeedtab);
1000
1001         if (cflag < 0 || (cflag > 0 && t->c_ispeed != t->c_ospeed))
1002                 return (EINVAL);
1003
1004         s = splclock();
1005
1006         window = sc->window;
1007         sc->setwin(sc, 0);
1008
1009         if (cflag == 0) {                               /* hangup */
1010                 DLOG(DIGIDB_SET, (sc->dev, "port%d: hangup\n", port->pnum));
1011                 digimodem(port->tp, 0, SER_DTR | SER_RTS);
1012         } else {
1013                 digimodem(port->tp, SER_DTR | SER_RTS, 0);
1014
1015                 DLOG(DIGIDB_SET, (sc->dev, "port%d: CBAUD = %d\n", port->pnum,
1016                     cflag));
1017
1018 #if 0
1019                 /* convert flags to sysV-style values */
1020                 if (t->c_cflag & PARODD)
1021                         cflag |= 0x0200;
1022                 if (t->c_cflag & PARENB)
1023                         cflag |= 0x0100;
1024                 if (t->c_cflag & CSTOPB)
1025                         cflag |= 0x0080;
1026 #else
1027                 /* convert flags to sysV-style values */
1028                 if (t->c_cflag & PARODD)
1029                         cflag |= FEP_PARODD;
1030                 if (t->c_cflag & PARENB)
1031                         cflag |= FEP_PARENB;
1032                 if (t->c_cflag & CSTOPB)
1033                         cflag |= FEP_CSTOPB;
1034                 if (t->c_cflag & CLOCAL)
1035                         cflag |= FEP_CLOCAL;
1036 #endif
1037
1038                 cflag |= (t->c_cflag & CSIZE) >> 4;
1039                 DLOG(DIGIDB_SET, (sc->dev, "port%d: CFLAG = 0x%x\n", port->pnum,
1040                     cflag));
1041                 fepcmd_w(port, SETCFLAGS, (unsigned)cflag, 0);
1042         }
1043
1044         iflag =
1045             t->c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP);
1046         if (port->c_iflag & IXON)
1047                 iflag |= 0x400;
1048         if (port->c_iflag & IXANY)
1049                 iflag |= 0x800;
1050         if (port->c_iflag & IXOFF)
1051                 iflag |= 0x1000;
1052
1053         DLOG(DIGIDB_SET, (sc->dev, "port%d: set iflag = 0x%x\n", port->pnum, iflag));
1054         fepcmd_w(port, SETIFLAGS, (unsigned)iflag, 0);
1055
1056         hflow = 0;
1057         if (t->c_cflag & CDTR_IFLOW)
1058                 hflow |= sc->csigs->dtr;
1059         if (t->c_cflag & CRTS_IFLOW)
1060                 hflow |= sc->csigs->rts;
1061         if (t->c_cflag & CCTS_OFLOW)
1062                 hflow |= sc->csigs->cts;
1063         if (t->c_cflag & CDSR_OFLOW)
1064                 hflow |= port->dsr;
1065         if (t->c_cflag & CCAR_OFLOW)
1066                 hflow |= port->cd;
1067
1068         DLOG(DIGIDB_SET, (sc->dev, "port%d: set hflow = 0x%x\n", port->pnum, hflow));
1069         fepcmd_w(port, SETHFLOW, 0xff00 | (unsigned)hflow, 0);
1070
1071         DLOG(DIGIDB_SET, (sc->dev, "port%d: set startc(0x%x), stopc(0x%x)\n",
1072             port->pnum, t->c_cc[VSTART], t->c_cc[VSTOP]));
1073         fepcmd_b(port, SONOFFC, t->c_cc[VSTART], t->c_cc[VSTOP], 0);
1074
1075         if (sc->window != 0)
1076                 sc->towin(sc, 0);
1077         if (window != 0)
1078                 sc->towin(sc, window);
1079         splx(s);
1080
1081         return (0);
1082 }
1083
1084 static void
1085 digi_intr(void *vp)
1086 {
1087         struct digi_p *port;
1088         char *cxcon;
1089         struct digi_softc *sc;
1090         int ehead, etail;
1091         volatile struct board_chan *bc;
1092         struct tty *tp;
1093         int head, tail;
1094         int wrapmask;
1095         int size, window;
1096         struct event {
1097                 u_char pnum;
1098                 u_char event;
1099                 u_char mstat;
1100                 u_char lstat;
1101         } event;
1102
1103         sc = vp;
1104
1105         if (sc->status != DIGI_STATUS_ENABLED) {
1106                 DLOG(DIGIDB_IRQ, (sc->dev, "interrupt on disabled board !\n"));
1107                 return;
1108         }
1109
1110 #ifdef DIGI_INTERRUPT
1111         microtime(&sc->intr_timestamp);
1112 #endif
1113
1114         window = sc->window;
1115         sc->setwin(sc, 0);
1116
1117         if (sc->model >= PCXEM && W(sc->vmem + 0xd00)) {
1118                 struct con_bios *con = con_bios_list;
1119                 register u_char *ptr;
1120
1121                 ptr = sc->vmem + W(sc->vmem + 0xd00);
1122                 while (con) {
1123                         if (ptr[1] && W(ptr + 2) == W(con->bios + 2))
1124                                 /* Not first block -- exact match */
1125                                 break;
1126
1127                         if (W(ptr + 4) >= W(con->bios + 4) &&
1128                             W(ptr + 4) <= W(con->bios + 6))
1129                                 /* Initial search concetrator BIOS */
1130                                 break;
1131                 }
1132
1133                 if (con == NULL) {
1134                         log(LOG_ERR, "digi%d: wanted bios LREV = 0x%04x"
1135                             " not found!\n", sc->res.unit, W(ptr + 4));
1136                         W(ptr + 10) = 0;
1137                         W(sc->vmem + 0xd00) = 0;
1138                         goto eoi;
1139                 }
1140                 cxcon = con->bios;
1141                 W(ptr + 4) = W(cxcon + 4);
1142                 W(ptr + 6) = W(cxcon + 6);
1143                 if (ptr[1] == 0)
1144                         W(ptr + 2) = W(cxcon + 2);
1145                 W(ptr + 8) = (ptr[1] << 6) + W(cxcon + 8);
1146                 size = W(cxcon + 10) - (ptr[1] << 10);
1147                 if (size <= 0) {
1148                         W(ptr + 8) = W(cxcon + 8);
1149                         W(ptr + 10) = 0;
1150                 } else {
1151                         if (size > 1024)
1152                                 size = 1024;
1153                         W(ptr + 10) = size;
1154                         bcopy(cxcon + (ptr[1] << 10), ptr + 12, size);
1155                 }
1156                 W(sc->vmem + 0xd00) = 0;
1157                 goto eoi;
1158         }
1159
1160         ehead = sc->gdata->ein;
1161         etail = sc->gdata->eout;
1162         if (ehead == etail) {
1163 #ifdef DEBUG
1164                 sc->intr_count++;
1165                 if (sc->intr_count % 6000 == 0) {
1166                         DLOG(DIGIDB_IRQ, (sc->dev,
1167                             "6000 useless polls %x %x\n", ehead, etail));
1168                         sc->intr_count = 0;
1169                 }
1170 #endif
1171                 goto eoi;
1172         }
1173         while (ehead != etail) {
1174                 event = *(volatile struct event *)(sc->memevent + etail);
1175
1176                 etail = (etail + 4) & sc->gdata->imax;
1177
1178                 if (event.pnum >= sc->numports) {
1179                         log(LOG_ERR, "digi%d: port %d: got event"
1180                             " on nonexisting port\n", sc->res.unit,
1181                             event.pnum);
1182                         continue;
1183                 }
1184                 port = &sc->ports[event.pnum];
1185                 bc = port->bc;
1186                 tp = port->tp;
1187
1188                 if (!(tp->t_state & TS_ISOPEN) && !tp->t_wopeners) {
1189                         DLOG(DIGIDB_IRQ, (sc->dev,
1190                             "port %d: event 0x%x on closed port\n",
1191                             event.pnum, event.event));
1192                         bc->rout = bc->rin;
1193                         bc->idata = 0;
1194                         bc->iempty = 0;
1195                         bc->ilow = 0;
1196                         bc->mint = 0;
1197                         continue;
1198                 }
1199                 if (event.event & ~ALL_IND)
1200                         log(LOG_ERR, "digi%d: port%d: ? event 0x%x mstat 0x%x"
1201                             " lstat 0x%x\n", sc->res.unit, event.pnum,
1202                             event.event, event.mstat, event.lstat);
1203
1204                 if (event.event & DATA_IND) {
1205                         DLOG(DIGIDB_IRQ, (sc->dev, "port %d: DATA_IND\n",
1206                             event.pnum));
1207                         wrapmask = port->rxbufsize - 1;
1208                         head = bc->rin;
1209                         tail = bc->rout;
1210
1211                         size = 0;
1212                         if (!(tp->t_state & TS_ISOPEN)) {
1213                                 bc->rout = head;
1214                                 goto end_of_data;
1215                         }
1216                         while (head != tail) {
1217                                 int top;
1218
1219                                 DLOG(DIGIDB_INT, (sc->dev,
1220                                     "port %d: p rx head = %d tail = %d\n",
1221                                     event.pnum, head, tail));
1222                                 top = (head > tail) ? head : wrapmask + 1;
1223                                 sc->towin(sc, port->rxwin);
1224                                 size = top - tail;
1225                                 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1226                                         size = b_to_q((char *)port->rxbuf +
1227                                             tail, size, &tp->t_rawq);
1228                                         tail = top - size;
1229                                         ttwakeup(tp);
1230                                 } else for (; tail < top;) {
1231                                         ttyld_rint(tp, port->rxbuf[tail]);
1232                                         sc->towin(sc, port->rxwin);
1233                                         size--;
1234                                         tail++;
1235                                         if (tp->t_state & TS_TBLOCK)
1236                                                 break;
1237                                 }
1238                                 tail &= wrapmask;
1239                                 sc->setwin(sc, 0);
1240                                 bc->rout = tail;
1241                                 head = bc->rin;
1242                                 if (size)
1243                                         break;
1244                         }
1245
1246                         if (bc->orun) {
1247                                 CE_RECORD(port, CE_OVERRUN);
1248                                 log(LOG_ERR, "digi%d: port%d: %s\n",
1249                                     sc->res.unit, event.pnum,
1250                                     digi_errortxt(CE_OVERRUN));
1251                                 bc->orun = 0;
1252                         }
1253 end_of_data:
1254                         if (size) {
1255                                 tp->t_state |= TS_TBLOCK;
1256                                 port->status |= PAUSE_RX;
1257                                 DLOG(DIGIDB_RX, (sc->dev, "port %d: pause RX\n",
1258                                     event.pnum));
1259                         } else {
1260                                 bc->idata = 1;
1261                         }
1262                 }
1263
1264                 if (event.event & MODEMCHG_IND) {
1265                         DLOG(DIGIDB_MODEM, (sc->dev, "port %d: MODEMCHG_IND\n",
1266                             event.pnum));
1267
1268                         if ((event.mstat ^ event.lstat) & port->cd) {
1269                                 sc->hidewin(sc);
1270                                 ttyld_modem(tp, event.mstat & port->cd);
1271                                 sc->setwin(sc, 0);
1272                                 wakeup(TSA_CARR_ON(tp));
1273                         }
1274
1275                         if (event.mstat & sc->csigs->ri) {
1276                                 DLOG(DIGIDB_RI, (sc->dev, "port %d: RING\n",
1277                                     event.pnum));
1278                                 if (port->send_ring) {
1279                                         ttyld_rint(tp, 'R');
1280                                         ttyld_rint(tp, 'I');
1281                                         ttyld_rint(tp, 'N');
1282                                         ttyld_rint(tp, 'G');
1283                                         ttyld_rint(tp, '\r');
1284                                         ttyld_rint(tp, '\n');
1285                                 }
1286                         }
1287                 }
1288                 if (event.event & BREAK_IND) {
1289                         DLOG(DIGIDB_MODEM, (sc->dev, "port %d: BREAK_IND\n",
1290                             event.pnum));
1291                         ttyld_rint(tp, TTY_BI);
1292                 }
1293                 if (event.event & (LOWTX_IND | EMPTYTX_IND)) {
1294                         DLOG(DIGIDB_IRQ, (sc->dev, "port %d:%s%s\n",
1295                             event.pnum,
1296                             event.event & LOWTX_IND ? " LOWTX" : "",
1297                             event.event & EMPTYTX_IND ?  " EMPTYTX" : ""));
1298                         ttyld_start(tp);
1299                 }
1300         }
1301         sc->gdata->eout = etail;
1302 eoi:
1303         if (sc->window != 0)
1304                 sc->towin(sc, 0);
1305         if (window != 0)
1306                 sc->towin(sc, window);
1307 }
1308
1309 static void
1310 digistart(struct tty *tp)
1311 {
1312         struct digi_p *port;
1313         struct digi_softc *sc;
1314         volatile struct board_chan *bc;
1315         int head, tail;
1316         int size, ocount, totcnt = 0;
1317         int s;
1318         int wmask;
1319
1320         port = tp->t_sc;
1321         sc = port->sc;
1322         bc = port->bc;
1323
1324         wmask = port->txbufsize - 1;
1325
1326         s = spltty();
1327         port->lcc = tp->t_outq.c_cc;
1328         sc->setwin(sc, 0);
1329         if (!(tp->t_state & TS_TBLOCK)) {
1330                 if (port->status & PAUSE_RX) {
1331                         DLOG(DIGIDB_RX, (sc->dev, "port %d: resume RX\n",
1332                             port->pnum));
1333                         /*
1334                          * CAREFUL - braces are needed here if the DLOG is
1335                          * optimised out!
1336                          */
1337                 }
1338                 port->status &= ~PAUSE_RX;
1339                 bc->idata = 1;
1340         }
1341         if (!(tp->t_state & TS_TTSTOP) && port->status & PAUSE_TX) {
1342                 DLOG(DIGIDB_TX, (sc->dev, "port %d: resume TX\n", port->pnum));
1343                 port->status &= ~PAUSE_TX;
1344                 fepcmd_w(port, RESUMETX, 0, 10);
1345         }
1346         if (tp->t_outq.c_cc == 0)
1347                 tp->t_state &= ~TS_BUSY;
1348         else
1349                 tp->t_state |= TS_BUSY;
1350
1351         head = bc->tin;
1352         while (tp->t_outq.c_cc != 0) {
1353                 tail = bc->tout;
1354                 DLOG(DIGIDB_INT, (sc->dev, "port%d: s tx head = %d tail = %d\n",
1355                     port->pnum, head, tail));
1356
1357                 if (head < tail)
1358                         size = tail - head - 1;
1359                 else {
1360                         size = port->txbufsize - head;
1361                         if (tail == 0)
1362                                 size--;
1363                 }
1364
1365                 if (size == 0)
1366                         break;
1367                 sc->towin(sc, port->txwin);
1368                 ocount = q_to_b(&tp->t_outq, port->txbuf + head, size);
1369                 totcnt += ocount;
1370                 head += ocount;
1371                 head &= wmask;
1372                 sc->setwin(sc, 0);
1373                 bc->tin = head;
1374                 bc->iempty = 1;
1375                 bc->ilow = 1;
1376         }
1377         port->lostcc = tp->t_outq.c_cc;
1378         tail = bc->tout;
1379         if (head < tail)
1380                 size = port->txbufsize - tail + head;
1381         else
1382                 size = head - tail;
1383
1384         port->lbuf = size;
1385         DLOG(DIGIDB_INT, (sc->dev, "port%d: s total cnt = %d\n", port->pnum, totcnt));
1386         ttwwakeup(tp);
1387         splx(s);
1388 }
1389
1390 static void
1391 digistop(struct tty *tp, int rw)
1392 {
1393         struct digi_softc *sc;
1394         struct digi_p *port;
1395
1396         port = tp->t_sc;
1397         sc = port->sc;
1398
1399         DLOG(DIGIDB_TX, (sc->dev, "port %d: pause TX\n", port->pnum));
1400         port->status |= PAUSE_TX;
1401         fepcmd_w(port, PAUSETX, 0, 10);
1402 }
1403
1404 static void
1405 fepcmd(struct digi_p *port, int cmd, int op1, int ncmds)
1406 {
1407         u_char *mem;
1408         unsigned tail, head;
1409         int count, n;
1410
1411         mem = port->sc->memcmd;
1412
1413         port->sc->setwin(port->sc, 0);
1414
1415         head = port->sc->gdata->cin;
1416         mem[head + 0] = cmd;
1417         mem[head + 1] = port->pnum;
1418         *(u_short *)(mem + head + 2) = op1;
1419
1420         head = (head + 4) & port->sc->gdata->cmax;
1421         port->sc->gdata->cin = head;
1422
1423         for (count = FEPTIMEOUT; count > 0; count--) {
1424                 head = port->sc->gdata->cin;
1425                 tail = port->sc->gdata->cout;
1426                 n = (head - tail) & port->sc->gdata->cmax;
1427
1428                 if (n <= ncmds * sizeof(short) * 4)
1429                         break;
1430         }
1431         if (count == 0)
1432                 log(LOG_ERR, "digi%d: port%d: timeout on FEP command\n",
1433                     port->sc->res.unit, port->pnum);
1434 }
1435
1436 const char *
1437 digi_errortxt(int id)
1438 {
1439         static const char *error_desc[] = {
1440                 "silo overflow",
1441                 "interrupt-level buffer overflow",
1442                 "tty-level buffer overflow",
1443         };
1444
1445         KASSERT(id >= 0 && id < sizeof(error_desc) / sizeof(error_desc[0]),
1446             ("Unexpected digi error id %d\n", id));
1447
1448         return (error_desc[id]);
1449 }
1450
1451 int
1452 digi_attach(struct digi_softc *sc)
1453 {
1454         sc->res.ctldev = make_dev(&digi_csw,
1455             sc->res.unit << 16, UID_ROOT, GID_WHEEL,
1456             0600, "digi%r.ctl", sc->res.unit);
1457         sc->res.ctldev->si_drv1 = sc;
1458
1459         digi_loadmoduledata(sc);
1460         digi_init(sc);
1461         digi_freemoduledata(sc);
1462
1463         return (0);
1464 }
1465
1466 static int
1467 digi_inuse(struct digi_softc *sc)
1468 {
1469         int i;
1470         struct digi_p *port;
1471
1472         port = &sc->ports[0];
1473         for (i = 0; i < sc->numports; i++, port++)
1474                 if (port->tp->t_state & TS_ISOPEN) {
1475                         DLOG(DIGIDB_INIT, (sc->dev, "port%d: busy\n", i));
1476                         return (1);
1477                 } else if (port->tp->t_wopeners || port->opencnt) {
1478                         DLOG(DIGIDB_INIT, (sc->dev, "port%d: blocked in open\n",
1479                             i));
1480                         return (1);
1481                 }
1482         return (0);
1483 }
1484
1485 static void
1486 digi_free_state(struct digi_softc *sc)
1487 {
1488         int i;
1489
1490         /* Blow it all away */
1491
1492         for (i = 0; i < sc->numports; i++)
1493                 ttygone(sc->ports[i].tp);
1494
1495         /* XXX: this might be better done as a ttypurge method */
1496         untimeout(digi_poll, sc, sc->callout);
1497         callout_handle_init(&sc->callout);
1498         untimeout(digi_int_test, sc, sc->inttest);
1499         callout_handle_init(&sc->inttest);
1500
1501         for (i = 0; i < sc->numports; i++)
1502                 ttyfree(sc->ports[i].tp);
1503
1504         bus_teardown_intr(sc->dev, sc->res.irq, sc->res.irqHandler);
1505 #ifdef DIGI_INTERRUPT
1506         if (sc->res.irq != NULL) {
1507                 bus_release_resource(dev, SYS_RES_IRQ, sc->res.irqrid,
1508                     sc->res.irq);
1509                 sc->res.irq = NULL;
1510         }
1511 #endif
1512         if (sc->numports) {
1513                 KASSERT(sc->ports, ("digi%d: Lost my ports ?", sc->res.unit));
1514                 free(sc->ports, M_TTYS);
1515                 sc->ports = NULL;
1516                 sc->numports = 0;
1517         }
1518
1519         sc->status = DIGI_STATUS_NOTINIT;
1520 }
1521
1522 int
1523 digi_detach(device_t dev)
1524 {
1525         struct digi_softc *sc = device_get_softc(dev);
1526
1527         DLOG(DIGIDB_INIT, (sc->dev, "detaching\n"));
1528
1529         /* If we're INIT'd, numports must be 0 */
1530         KASSERT(sc->numports == 0 || sc->status != DIGI_STATUS_NOTINIT,
1531             ("digi%d: numports(%d) & status(%d) are out of sync",
1532             sc->res.unit, sc->numports, (int)sc->status));
1533
1534         if (digi_inuse(sc))
1535                 return (EBUSY);
1536
1537         digi_free_state(sc);
1538
1539         destroy_dev(sc->res.ctldev);
1540
1541         if (sc->res.mem != NULL) {
1542                 bus_release_resource(dev, SYS_RES_MEMORY, sc->res.mrid,
1543                     sc->res.mem);
1544                 sc->res.mem = NULL;
1545         }
1546         if (sc->res.io != NULL) {
1547                 bus_release_resource(dev, SYS_RES_IOPORT, sc->res.iorid,
1548                     sc->res.io);
1549                 sc->res.io = NULL;
1550         }
1551
1552         return (0);
1553 }
1554
1555 int
1556 digi_shutdown(device_t dev)
1557 {
1558         return (0);
1559 }
1560
1561 MODULE_VERSION(digi, 1);