Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / serial / si / si.c
1 /*
2  * Device driver for Specialix range (SI/XIO) of serial line multiplexors.
3  *
4  * Copyright (C) 1990, 1992, 1998 Specialix International,
5  * Copyright (C) 1993, Andy Rutter <andy@acronym.co.uk>
6  * Copyright (C) 2000, Peter Wemm <peter@netplex.com.au>
7  *
8  * Originally derived from:     SunOS 4.x version
9  * Ported from BSDI version to FreeBSD by Peter Wemm.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notices, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notices, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by Andy Rutter of
22  *      Advanced Methods and Tools Ltd. based on original information
23  *      from Specialix International.
24  * 4. Neither the name of Advanced Methods and Tools, nor Specialix
25  *    International may be used to endorse or promote products derived from
26  *    this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
31  * NO EVENT SHALL THE AUTHORS BE LIABLE.
32  *
33  * $FreeBSD: src/sys/dev/si/si.c,v 1.101.2.1 2001/02/26 04:23:06 jlemon Exp $
34  */
35
36 #ifndef lint
37 static const char si_copyright1[] =  "@(#) Copyright (C) Specialix International, 1990,1992,1998",
38                   si_copyright2[] =  "@(#) Copyright (C) Andy Rutter 1993",
39                   si_copyright3[] =  "@(#) Copyright (C) Peter Wemm 2000";
40 #endif  /* not lint */
41
42 #include "opt_compat.h"
43 #include "opt_debug_si.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
48 #include <sys/ioctl_compat.h>
49 #endif
50 #include <sys/tty.h>
51 #include <sys/proc.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/dkstat.h>
55 #include <sys/kernel.h>
56 #include <sys/malloc.h>
57 #include <sys/sysctl.h>
58 #include <sys/bus.h>
59 #include <machine/bus.h>
60 #include <sys/rman.h>
61 #include <machine/resource.h>
62
63 #include <machine/clock.h>
64
65 #include <vm/vm.h>
66 #include <vm/pmap.h>
67
68 #include <machine/stdarg.h>
69
70 #include <dev/si/sireg.h>
71 #include <dev/si/sivar.h>
72 #include <dev/si/si.h>
73
74 /*
75  * This device driver is designed to interface the Specialix International
76  * SI, XIO and SX range of serial multiplexor cards to FreeBSD on an ISA,
77  * EISA or PCI bus machine.
78  *
79  * The controller is interfaced to the host via dual port RAM
80  * and an interrupt.
81  *
82  * The code for the Host 1 (very old ISA cards) has not been tested.
83  */
84
85 #define POLL            /* turn on poller to scan for lost interrupts */
86 #define REALPOLL        /* on each poll, scan for work regardless */
87 #define POLLHZ  (hz/10) /* 10 times per second */
88 #define SI_I_HIGH_WATER (TTYHOG - 2 * SI_BUFFERSIZE)
89 #define INT_COUNT 25000         /* max of 125 ints per second */
90 #define JET_INT_COUNT 100       /* max of 100 ints per second */
91 #define RXINT_COUNT 1   /* one rxint per 10 milliseconds */
92
93 enum si_mctl { GET, SET, BIS, BIC };
94
95 static void si_command(struct si_port *, int, int);
96 static int si_modem(struct si_port *, enum si_mctl, int);
97 static void si_write_enable(struct si_port *, int);
98 static int si_Sioctl(dev_t, u_long, caddr_t, int, struct proc *);
99 static void si_start(struct tty *);
100 static void si_stop(struct tty *, int);
101 static timeout_t si_lstart;
102 static void si_disc_optim(struct tty *tp, struct termios *t,struct si_port *pp);
103 static void sihardclose(struct si_port *pp);
104 static void sidtrwakeup(void *chan);
105
106 #ifdef SI_DEBUG
107 static char     *si_mctl2str(enum si_mctl cmd);
108 #endif
109
110 static int      siparam(struct tty *, struct termios *);
111
112 static void     si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip);
113 static char *   si_modulename(int host_type, int uart_type);
114
115 static  d_open_t        siopen;
116 static  d_close_t       siclose;
117 static  d_write_t       siwrite;
118 static  d_ioctl_t       siioctl;
119
120 #define CDEV_MAJOR      68
121 static struct cdevsw si_cdevsw = {
122         /* open */      siopen,
123         /* close */     siclose,
124         /* read */      ttyread,
125         /* write */     siwrite,
126         /* ioctl */     siioctl,
127         /* poll */      ttypoll,
128         /* mmap */      nommap,
129         /* strategy */  nostrategy,
130         /* name */      "si",
131         /* maj */       CDEV_MAJOR,
132         /* dump */      nodump,
133         /* psize */     nopsize,
134         /* flags */     D_TTY | D_KQFILTER,
135         /* bmaj */      -1,
136         /* kqfilter */  ttykqfilter,
137 };
138
139 static int si_Nports;
140 static int si_Nmodules;
141 static int si_debug = 0;        /* data, not bss, so it's patchable */
142
143 SYSCTL_INT(_machdep, OID_AUTO, si_debug, CTLFLAG_RW, &si_debug, 0, "");
144
145 static struct tty *si__tty;
146
147 static int si_numunits;
148
149 devclass_t si_devclass;
150
151 #ifndef B2000   /* not standard, but the hardware knows it. */
152 # define B2000 2000
153 #endif
154 static struct speedtab bdrates[] = {
155         { B75,          CLK75, },       /* 0x0 */
156         { B110,         CLK110, },      /* 0x1 */
157         { B150,         CLK150, },      /* 0x3 */
158         { B300,         CLK300, },      /* 0x4 */
159         { B600,         CLK600, },      /* 0x5 */
160         { B1200,        CLK1200, },     /* 0x6 */
161         { B2000,        CLK2000, },     /* 0x7 */
162         { B2400,        CLK2400, },     /* 0x8 */
163         { B4800,        CLK4800, },     /* 0x9 */
164         { B9600,        CLK9600, },     /* 0xb */
165         { B19200,       CLK19200, },    /* 0xc */
166         { B38400,       CLK38400, },    /* 0x2 (out of order!) */
167         { B57600,       CLK57600, },    /* 0xd */
168         { B115200,      CLK110, },      /* 0x1 (dupe!, 110 baud on "si") */
169         { -1,           -1 },
170 };
171
172
173 /* populated with approx character/sec rates - translated at card
174  * initialisation time to chars per tick of the clock */
175 static int done_chartimes = 0;
176 static struct speedtab chartimes[] = {
177         { B75,          8, },
178         { B110,         11, },
179         { B150,         15, },
180         { B300,         30, },
181         { B600,         60, },
182         { B1200,        120, },
183         { B2000,        200, },
184         { B2400,        240, },
185         { B4800,        480, },
186         { B9600,        960, },
187         { B19200,       1920, },
188         { B38400,       3840, },
189         { B57600,       5760, },
190         { B115200,      11520, },
191         { -1,           -1 },
192 };
193 static volatile int in_intr = 0;        /* Inside interrupt handler? */
194
195 #ifdef POLL
196 static int si_pollrate;                 /* in addition to irq */
197 static int si_realpoll = 0;             /* poll HW on timer */
198
199 SYSCTL_INT(_machdep, OID_AUTO, si_pollrate, CTLFLAG_RW, &si_pollrate, 0, "");
200 SYSCTL_INT(_machdep, OID_AUTO, si_realpoll, CTLFLAG_RW, &si_realpoll, 0, "");
201
202 static int init_finished = 0;
203 static void si_poll(void *);
204 #endif
205
206 /*
207  * Array of adapter types and the corresponding RAM size. The order of
208  * entries here MUST match the ordinal of the adapter type.
209  */
210 static char *si_type[] = {
211         "EMPTY",
212         "SIHOST",
213         "SIMCA",                /* FreeBSD does not support Microchannel */
214         "SIHOST2",
215         "SIEISA",
216         "SIPCI",
217         "SXPCI",
218         "SXISA",
219 };
220
221 /*
222  * We have to make an 8 bit version of bcopy, since some cards can't
223  * deal with 32 bit I/O
224  */
225 static void __inline
226 si_bcopy(const void *src, void *dst, size_t len)
227 {
228         while (len--)
229                 *(((u_char *)dst)++) = *(((const u_char *)src)++);
230 }
231 static void __inline
232 si_vbcopy(const volatile void *src, void *dst, size_t len)
233 {
234         while (len--)
235                 *(((u_char *)dst)++) = *(((const volatile u_char *)src)++);
236 }
237 static void __inline
238 si_bcopyv(const void *src, volatile void *dst, size_t len)
239 {
240         while (len--)
241                 *(((volatile u_char *)dst)++) = *(((const u_char *)src)++);
242 }
243
244
245 /*
246  * Attach the device.  Initialize the card.
247  */
248 int
249 siattach(device_t dev)
250 {
251         int unit;
252         struct si_softc *sc;
253         struct si_port *pp;
254         volatile struct si_channel *ccbp;
255         volatile struct si_reg *regp;
256         volatile caddr_t maddr;
257         struct si_module *modp;
258         struct tty *tp;
259         struct speedtab *spt;
260         int nmodule, nport, x, y;
261         int uart_type;
262
263         sc = device_get_softc(dev);
264         unit = device_get_unit(dev);
265
266         sc->sc_typename = si_type[sc->sc_type];
267         if (si_numunits < unit + 1)
268                 si_numunits = unit + 1;
269
270         DPRINT((0, DBG_AUTOBOOT, "si%d: siattach\n", unit));
271
272 #ifdef POLL
273         if (si_pollrate == 0) {
274                 si_pollrate = POLLHZ;           /* in addition to irq */
275 #ifdef REALPOLL
276                 si_realpoll = 1;                /* scan always */
277 #endif
278         }
279 #endif
280
281         DPRINT((0, DBG_AUTOBOOT, "si%d: type: %s paddr: %x maddr: %x\n", unit,
282                 sc->sc_typename, sc->sc_paddr, sc->sc_maddr));
283
284         sc->sc_ports = NULL;                    /* mark as uninitialised */
285
286         maddr = sc->sc_maddr;
287
288         /* Stop the CPU first so it won't stomp around while we load */
289
290         switch (sc->sc_type) {
291                 case SIEISA:
292                         outb(sc->sc_iobase + 2, sc->sc_irq << 4);
293                 break;
294                 case SIPCI:
295                         *(maddr+SIPCIRESET) = 0;
296                 break;
297                 case SIJETPCI: /* fall through to JET ISA */
298                 case SIJETISA:
299                         *(maddr+SIJETCONFIG) = 0;
300                 break;
301                 case SIHOST2:
302                         *(maddr+SIPLRESET) = 0;
303                 break;
304                 case SIHOST:
305                         *(maddr+SIRESET) = 0;
306                 break;
307                 default: /* this should never happen */
308                         printf("si%d: unsupported configuration\n", unit);
309                         return EINVAL;
310                 break;
311         }
312
313         /* OK, now lets download the download code */
314
315         if (SI_ISJET(sc->sc_type)) {
316                 DPRINT((0, DBG_DOWNLOAD, "si%d: jet_download: nbytes %d\n",
317                         unit, si3_t225_dsize));
318                 si_bcopy(si3_t225_download, maddr + si3_t225_downloadaddr,
319                         si3_t225_dsize);
320                 DPRINT((0, DBG_DOWNLOAD,
321                         "si%d: jet_bootstrap: nbytes %d -> %x\n",
322                         unit, si3_t225_bsize, si3_t225_bootloadaddr));
323                 si_bcopy(si3_t225_bootstrap, maddr + si3_t225_bootloadaddr,
324                         si3_t225_bsize);
325         } else {
326                 DPRINT((0, DBG_DOWNLOAD, "si%d: si_download: nbytes %d\n",
327                         unit, si2_z280_dsize));
328                 si_bcopy(si2_z280_download, maddr + si2_z280_downloadaddr,
329                         si2_z280_dsize);
330         }
331
332         /* Now start the CPU */
333
334         switch (sc->sc_type) {
335         case SIEISA:
336                 /* modify the download code to tell it that it's on an EISA */
337                 *(maddr + 0x42) = 1;
338                 outb(sc->sc_iobase + 2, (sc->sc_irq << 4) | 4);
339                 (void)inb(sc->sc_iobase + 3); /* reset interrupt */
340                 break;
341         case SIPCI:
342                 /* modify the download code to tell it that it's on a PCI */
343                 *(maddr+0x42) = 1;
344                 *(maddr+SIPCIRESET) = 1;
345                 *(maddr+SIPCIINTCL) = 0;
346                 break;
347         case SIJETPCI:
348                 *(maddr+SIJETRESET) = 0;
349                 *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN;
350                 break;
351         case SIJETISA:
352                 *(maddr+SIJETRESET) = 0;
353                 switch (sc->sc_irq) {
354                 case 9:
355                         *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0x90;
356                         break;
357                 case 10:
358                         *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xa0;
359                         break;
360                 case 11:
361                         *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xb0;
362                         break;
363                 case 12:
364                         *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xc0;
365                         break;
366                 case 15:
367                         *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xf0;
368                         break;
369                 }
370                 break;
371         case SIHOST:
372                 *(maddr+SIRESET_CL) = 0;
373                 *(maddr+SIINTCL_CL) = 0;
374                 break;
375         case SIHOST2:
376                 *(maddr+SIPLRESET) = 0x10;
377                 switch (sc->sc_irq) {
378                 case 11:
379                         *(maddr+SIPLIRQ11) = 0x10;
380                         break;
381                 case 12:
382                         *(maddr+SIPLIRQ12) = 0x10;
383                         break;
384                 case 15:
385                         *(maddr+SIPLIRQ15) = 0x10;
386                         break;
387                 }
388                 *(maddr+SIPLIRQCLR) = 0x10;
389                 break;
390         default: /* this should _REALLY_ never happen */
391                 printf("si%d: Uh, it was supported a second ago...\n", unit);
392                 return EINVAL;
393         }
394
395         DELAY(1000000);                 /* wait around for a second */
396
397         regp = (struct si_reg *)maddr;
398         y = 0;
399                                         /* wait max of 5 sec for init OK */
400         while (regp->initstat == 0 && y++ < 10) {
401                 DELAY(500000);
402         }
403         switch (regp->initstat) {
404         case 0:
405                 printf("si%d: startup timeout - aborting\n", unit);
406                 sc->sc_type = SIEMPTY;
407                 return EINVAL;
408         case 1:
409                 if (SI_ISJET(sc->sc_type)) {
410                         /* set throttle to 100 times per second */
411                         regp->int_count = JET_INT_COUNT;
412                         /* rx_intr_count is a NOP in Jet */
413                 } else {
414                         /* set throttle to 125 times per second */
415                         regp->int_count = INT_COUNT;
416                         /* rx intr max of 25 times per second */
417                         regp->rx_int_count = RXINT_COUNT;
418                 }
419                 regp->int_pending = 0;          /* no intr pending */
420                 regp->int_scounter = 0; /* reset counter */
421                 break;
422         case 0xff:
423                 /*
424                  * No modules found, so give up on this one.
425                  */
426                 printf("si%d: %s - no ports found\n", unit,
427                         si_type[sc->sc_type]);
428                 return 0;
429         default:
430                 printf("si%d: download code version error - initstat %x\n",
431                         unit, regp->initstat);
432                 return EINVAL;
433         }
434
435         /*
436          * First time around the ports just count them in order
437          * to allocate some memory.
438          */
439         nport = 0;
440         modp = (struct si_module *)(maddr + 0x80);
441         for (;;) {
442                 DPRINT((0, DBG_DOWNLOAD, "si%d: ccb addr 0x%x\n", unit, modp));
443                 switch (modp->sm_type) {
444                 case TA4:
445                         DPRINT((0, DBG_DOWNLOAD,
446                                 "si%d: Found old TA4 module, 4 ports\n",
447                                 unit));
448                         x = 4;
449                         break;
450                 case TA8:
451                         DPRINT((0, DBG_DOWNLOAD,
452                                 "si%d: Found old TA8 module, 8 ports\n",
453                                 unit));
454                         x = 8;
455                         break;
456                 case TA4_ASIC:
457                         DPRINT((0, DBG_DOWNLOAD,
458                                 "si%d: Found ASIC TA4 module, 4 ports\n",
459                                 unit));
460                         x = 4;
461                         break;
462                 case TA8_ASIC:
463                         DPRINT((0, DBG_DOWNLOAD,
464                                 "si%d: Found ASIC TA8 module, 8 ports\n",
465                                 unit));
466                         x = 8;
467                         break;
468                 case MTA:
469                         DPRINT((0, DBG_DOWNLOAD,
470                                 "si%d: Found CD1400 module, 8 ports\n",
471                                 unit));
472                         x = 8;
473                         break;
474                 case SXDC:
475                         DPRINT((0, DBG_DOWNLOAD,
476                                 "si%d: Found SXDC module, 8 ports\n",
477                                 unit));
478                         x = 8;
479                         break;
480                 default:
481                         printf("si%d: unknown module type %d\n",
482                                 unit, modp->sm_type);
483                         goto try_next;
484                 }
485
486                 /* this was limited in firmware and is also a driver issue */
487                 if ((nport + x) > SI_MAXPORTPERCARD) {
488                         printf("si%d: extra ports ignored\n", unit);
489                         goto try_next;
490                 }
491
492                 nport += x;
493                 si_Nports += x;
494                 si_Nmodules++;
495
496 try_next:
497                 if (modp->sm_next == 0)
498                         break;
499                 modp = (struct si_module *)
500                         (maddr + (unsigned)(modp->sm_next & 0x7fff));
501         }
502         sc->sc_ports = (struct si_port *)malloc(sizeof(struct si_port) * nport,
503                 M_DEVBUF, M_NOWAIT);
504         if (sc->sc_ports == 0) {
505 mem_fail:
506                 printf("si%d: fail to malloc memory for port structs\n",
507                         unit);
508                 return EINVAL;
509         }
510         bzero(sc->sc_ports, sizeof(struct si_port) * nport);
511         sc->sc_nport = nport;
512
513         /*
514          * allocate tty structures for ports
515          */
516         tp = (struct tty *)malloc(sizeof(*tp) * nport, M_DEVBUF, M_NOWAIT);
517         if (tp == 0)
518                 goto mem_fail;
519         bzero(tp, sizeof(*tp) * nport);
520         si__tty = tp;
521
522         /*
523          * Scan round the ports again, this time initialising.
524          */
525         pp = sc->sc_ports;
526         nmodule = 0;
527         modp = (struct si_module *)(maddr + 0x80);
528         uart_type = 1000;       /* arbitary, > uchar_max */
529         for (;;) {
530                 switch (modp->sm_type) {
531                 case TA4:
532                         nport = 4;
533                         break;
534                 case TA8:
535                         nport = 8;
536                         break;
537                 case TA4_ASIC:
538                         nport = 4;
539                         break;
540                 case TA8_ASIC:
541                         nport = 8;
542                         break;
543                 case MTA:
544                         nport = 8;
545                         break;
546                 case SXDC:
547                         nport = 8;
548                         break;
549                 default:
550                         goto try_next2;
551                 }
552                 nmodule++;
553                 ccbp = (struct si_channel *)((char *)modp + 0x100);
554                 if (uart_type == 1000)
555                         uart_type = ccbp->type;
556                 else if (uart_type != ccbp->type)
557                         printf("si%d: Warning: module %d mismatch! (%d%s != %d%s)\n",
558                             unit, nmodule,
559                             ccbp->type, si_modulename(sc->sc_type, ccbp->type),
560                             uart_type, si_modulename(sc->sc_type, uart_type));
561
562                 for (x = 0; x < nport; x++, pp++, ccbp++) {
563                         pp->sp_ccb = ccbp;      /* save the address */
564                         pp->sp_tty = tp++;
565                         pp->sp_pend = IDLE_CLOSE;
566                         pp->sp_state = 0;       /* internal flag */
567                         pp->sp_dtr_wait = 3 * hz;
568                         pp->sp_iin.c_iflag = TTYDEF_IFLAG;
569                         pp->sp_iin.c_oflag = TTYDEF_OFLAG;
570                         pp->sp_iin.c_cflag = TTYDEF_CFLAG;
571                         pp->sp_iin.c_lflag = TTYDEF_LFLAG;
572                         termioschars(&pp->sp_iin);
573                         pp->sp_iin.c_ispeed = pp->sp_iin.c_ospeed =
574                                 TTYDEF_SPEED;;
575                         pp->sp_iout = pp->sp_iin;
576                 }
577 try_next2:
578                 if (modp->sm_next == 0) {
579                         printf("si%d: card: %s, ports: %d, modules: %d, type: %d%s\n",
580                                 unit,
581                                 sc->sc_typename,
582                                 sc->sc_nport,
583                                 nmodule,
584                                 uart_type,
585                                 si_modulename(sc->sc_type, uart_type));
586                         break;
587                 }
588                 modp = (struct si_module *)
589                         (maddr + (unsigned)(modp->sm_next & 0x7fff));
590         }
591         if (done_chartimes == 0) {
592                 for (spt = chartimes ; spt->sp_speed != -1; spt++) {
593                         if ((spt->sp_code /= hz) == 0)
594                                 spt->sp_code = 1;
595                 }
596                 done_chartimes = 1;
597         }
598
599 /*      path    name    devsw           minor   type   uid gid perm*/
600         for (x = 0; x < sc->sc_nport; x++) {
601                 /* sync with the manuals that start at 1 */
602                 y = x + 1 + unit * (1 << SI_CARDSHIFT);
603                 make_dev(&si_cdevsw, x, 0, 0, 0600, "ttyA%02d", y);
604                 make_dev(&si_cdevsw, x + 0x00080, 0, 0, 0600, "cuaA%02d", y);
605                 make_dev(&si_cdevsw, x + 0x10000, 0, 0, 0600, "ttyiA%02d", y);
606                 make_dev(&si_cdevsw, x + 0x10080, 0, 0, 0600, "cuaiA%02d", y);
607                 make_dev(&si_cdevsw, x + 0x20000, 0, 0, 0600, "ttylA%02d", y);
608                 make_dev(&si_cdevsw, x + 0x20080, 0, 0, 0600, "cualA%02d", y);
609         }
610         make_dev(&si_cdevsw, 0x40000, 0, 0, 0600, "si_control");
611         return (0);
612 }
613
614 static  int
615 siopen(dev_t dev, int flag, int mode, struct proc *p)
616 {
617         int oldspl, error;
618         int card, port;
619         struct si_softc *sc;
620         struct tty *tp;
621         volatile struct si_channel *ccbp;
622         struct si_port *pp;
623         int mynor = minor(dev);
624
625         /* quickly let in /dev/si_control */
626         if (IS_CONTROLDEV(mynor)) {
627                 if ((error = suser(p)))
628                         return(error);
629                 return(0);
630         }
631
632         card = SI_CARD(mynor);
633         sc = devclass_get_softc(si_devclass, card);
634         if (sc == NULL)
635                 return (ENXIO);
636
637         if (sc->sc_type == SIEMPTY) {
638                 DPRINT((0, DBG_OPEN|DBG_FAIL, "si%d: type %s??\n",
639                         card, sc->sc_typename));
640                 return(ENXIO);
641         }
642
643         port = SI_PORT(mynor);
644         if (port >= sc->sc_nport) {
645                 DPRINT((0, DBG_OPEN|DBG_FAIL, "si%d: nports %d\n",
646                         card, sc->sc_nport));
647                 return(ENXIO);
648         }
649
650 #ifdef  POLL
651         /*
652          * We've now got a device, so start the poller.
653          */
654         if (init_finished == 0) {
655                 timeout(si_poll, (caddr_t)0L, si_pollrate);
656                 init_finished = 1;
657         }
658 #endif
659
660         /* initial/lock device */
661         if (IS_STATE(mynor)) {
662                 return(0);
663         }
664
665         pp = sc->sc_ports + port;
666         tp = pp->sp_tty;                        /* the "real" tty */
667         dev->si_tty = tp;
668         ccbp = pp->sp_ccb;                      /* Find control block */
669         DPRINT((pp, DBG_ENTRY|DBG_OPEN, "siopen(%s,%x,%x,%x)\n",
670                 devtoname(dev), flag, mode, p));
671
672         oldspl = spltty();                      /* Keep others out */
673         error = 0;
674
675 open_top:
676         while (pp->sp_state & SS_DTR_OFF) {
677                 error = tsleep(&pp->sp_dtr_wait, TTIPRI|PCATCH, "sidtr", 0);
678                 if (error != 0)
679                         goto out;
680         }
681
682         if (tp->t_state & TS_ISOPEN) {
683                 /*
684                  * The device is open, so everything has been initialised.
685                  * handle conflicts.
686                  */
687                 if (IS_CALLOUT(mynor)) {
688                         if (!pp->sp_active_out) {
689                                 error = EBUSY;
690                                 goto out;
691                         }
692                 } else {
693                         if (pp->sp_active_out) {
694                                 if (flag & O_NONBLOCK) {
695                                         error = EBUSY;
696                                         goto out;
697                                 }
698                                 error = tsleep(&pp->sp_active_out,
699                                                 TTIPRI|PCATCH, "sibi", 0);
700                                 if (error != 0)
701                                         goto out;
702                                 goto open_top;
703                         }
704                 }
705                 if (tp->t_state & TS_XCLUDE &&
706                     suser(p)) {
707                         DPRINT((pp, DBG_OPEN|DBG_FAIL,
708                                 "already open and EXCLUSIVE set\n"));
709                         error = EBUSY;
710                         goto out;
711                 }
712         } else {
713                 /*
714                  * The device isn't open, so there are no conflicts.
715                  * Initialize it. Avoid sleep... :-)
716                  */
717                 DPRINT((pp, DBG_OPEN, "first open\n"));
718                 tp->t_oproc = si_start;
719                 tp->t_stop = si_stop;
720                 tp->t_param = siparam;
721                 tp->t_dev = dev;
722                 tp->t_termios = mynor & SI_CALLOUT_MASK
723                                 ? pp->sp_iout : pp->sp_iin;
724
725                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
726
727                 ++pp->sp_wopeners;      /* in case of sleep in siparam */
728
729                 error = siparam(tp, &tp->t_termios);
730
731                 --pp->sp_wopeners;
732                 if (error != 0)
733                         goto out;
734                 /* XXX: we should goto_top if siparam slept */
735
736                 /* set initial DCD state */
737                 pp->sp_last_hi_ip = ccbp->hi_ip;
738                 if ((pp->sp_last_hi_ip & IP_DCD) || IS_CALLOUT(mynor)) {
739                         (*linesw[tp->t_line].l_modem)(tp, 1);
740                 }
741         }
742
743         /* whoops! we beat the close! */
744         if (pp->sp_state & SS_CLOSING) {
745                 /* try and stop it from proceeding to bash the hardware */
746                 pp->sp_state &= ~SS_CLOSING;
747         }
748
749         /*
750          * Wait for DCD if necessary
751          */
752         if (!(tp->t_state & TS_CARR_ON) &&
753             !IS_CALLOUT(mynor) &&
754             !(tp->t_cflag & CLOCAL) &&
755             !(flag & O_NONBLOCK)) {
756                 ++pp->sp_wopeners;
757                 DPRINT((pp, DBG_OPEN, "sleeping for carrier\n"));
758                 error = tsleep(TSA_CARR_ON(tp), TTIPRI|PCATCH, "sidcd", 0);
759                 --pp->sp_wopeners;
760                 if (error != 0)
761                         goto out;
762                 goto open_top;
763         }
764
765         error = (*linesw[tp->t_line].l_open)(dev, tp);
766         si_disc_optim(tp, &tp->t_termios, pp);
767         if (tp->t_state & TS_ISOPEN && IS_CALLOUT(mynor))
768                 pp->sp_active_out = TRUE;
769
770         pp->sp_state |= SS_OPEN;        /* made it! */
771
772 out:
773         splx(oldspl);
774
775         DPRINT((pp, DBG_OPEN, "leaving siopen\n"));
776
777         if (!(tp->t_state & TS_ISOPEN) && pp->sp_wopeners == 0)
778                 sihardclose(pp);
779
780         return(error);
781 }
782
783 static  int
784 siclose(dev_t dev, int flag, int mode, struct proc *p)
785 {
786         struct si_port *pp;
787         struct tty *tp;
788         int oldspl;
789         int error = 0;
790         int mynor = minor(dev);
791
792         if (IS_SPECIAL(mynor))
793                 return(0);
794
795         oldspl = spltty();
796
797         pp = MINOR2PP(mynor);
798         tp = pp->sp_tty;
799
800         DPRINT((pp, DBG_ENTRY|DBG_CLOSE, "siclose(%s,%x,%x,%x) sp_state:%x\n",
801                 devtoname(dev), flag, mode, p, pp->sp_state));
802
803         /* did we sleep and loose a race? */
804         if (pp->sp_state & SS_CLOSING) {
805                 /* error = ESOMETING? */
806                 goto out;
807         }
808
809         /* begin race detection.. */
810         pp->sp_state |= SS_CLOSING;
811
812         si_write_enable(pp, 0);         /* block writes for ttywait() */
813
814         /* THIS MAY SLEEP IN TTYWAIT!!! */
815         (*linesw[tp->t_line].l_close)(tp, flag);
816
817         si_write_enable(pp, 1);
818
819         /* did we sleep and somebody started another open? */
820         if (!(pp->sp_state & SS_CLOSING)) {
821                 /* error = ESOMETING? */
822                 goto out;
823         }
824         /* ok. we are now still on the right track.. nuke the hardware */
825
826         if (pp->sp_state & SS_LSTART) {
827                 untimeout(si_lstart, (caddr_t)pp, pp->lstart_ch);
828                 pp->sp_state &= ~SS_LSTART;
829         }
830
831         si_stop(tp, FREAD | FWRITE);
832
833         sihardclose(pp);
834         ttyclose(tp);
835         pp->sp_state &= ~SS_OPEN;
836
837 out:
838         DPRINT((pp, DBG_CLOSE|DBG_EXIT, "close done, returning\n"));
839         splx(oldspl);
840         return(error);
841 }
842
843 static void
844 sihardclose(struct si_port *pp)
845 {
846         int oldspl;
847         struct tty *tp;
848         volatile struct si_channel *ccbp;
849
850         oldspl = spltty();
851
852         tp = pp->sp_tty;
853         ccbp = pp->sp_ccb;                      /* Find control block */
854         if (tp->t_cflag & HUPCL ||
855             (!pp->sp_active_out &&
856              !(ccbp->hi_ip & IP_DCD) &&
857              !(pp->sp_iin.c_cflag && CLOCAL)) ||
858             !(tp->t_state & TS_ISOPEN)) {
859
860                 (void) si_modem(pp, BIC, TIOCM_DTR|TIOCM_RTS);
861                 (void) si_command(pp, FCLOSE, SI_NOWAIT);
862
863                 if (pp->sp_dtr_wait != 0) {
864                         timeout(sidtrwakeup, pp, pp->sp_dtr_wait);
865                         pp->sp_state |= SS_DTR_OFF;
866                 }
867
868         }
869         pp->sp_active_out = FALSE;
870         wakeup((caddr_t)&pp->sp_active_out);
871         wakeup(TSA_CARR_ON(tp));
872
873         splx(oldspl);
874 }
875
876
877 /*
878  * called at splsoftclock()...
879  */
880 static void
881 sidtrwakeup(void *chan)
882 {
883         struct si_port *pp;
884         int oldspl;
885
886         oldspl = spltty();
887
888         pp = (struct si_port *)chan;
889         pp->sp_state &= ~SS_DTR_OFF;
890         wakeup(&pp->sp_dtr_wait);
891
892         splx(oldspl);
893 }
894
895 static  int
896 siwrite(dev_t dev, struct uio *uio, int flag)
897 {
898         struct si_port *pp;
899         struct tty *tp;
900         int error = 0;
901         int mynor = minor(dev);
902         int oldspl;
903
904         if (IS_SPECIAL(mynor)) {
905                 DPRINT((0, DBG_ENTRY|DBG_FAIL|DBG_WRITE, "siwrite(CONTROLDEV!!)\n"));
906                 return(ENODEV);
907         }
908         pp = MINOR2PP(mynor);
909         tp = pp->sp_tty;
910         DPRINT((pp, DBG_WRITE, "siwrite(%s,%x,%x)\n", devtoname(dev), uio, flag));
911
912         oldspl = spltty();
913         /*
914          * If writes are currently blocked, wait on the "real" tty
915          */
916         while (pp->sp_state & SS_BLOCKWRITE) {
917                 pp->sp_state |= SS_WAITWRITE;
918                 DPRINT((pp, DBG_WRITE, "in siwrite, wait for SS_BLOCKWRITE to clear\n"));
919                 if ((error = ttysleep(tp, (caddr_t)pp, TTOPRI|PCATCH,
920                                      "siwrite", tp->t_timeout))) {
921                         if (error == EWOULDBLOCK)
922                                 error = EIO;
923                         goto out;
924                 }
925         }
926
927         error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
928 out:
929         splx(oldspl);
930         return (error);
931 }
932
933
934 static  int
935 siioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
936 {
937         struct si_port *pp;
938         struct tty *tp;
939         int error;
940         int mynor = minor(dev);
941         int oldspl;
942         int blocked = 0;
943 #if defined(COMPAT_43)
944         u_long oldcmd;
945         struct termios term;
946 #endif
947
948         if (IS_SI_IOCTL(cmd))
949                 return(si_Sioctl(dev, cmd, data, flag, p));
950
951         pp = MINOR2PP(mynor);
952         tp = pp->sp_tty;
953
954         DPRINT((pp, DBG_ENTRY|DBG_IOCTL, "siioctl(%s,%lx,%x,%x)\n",
955                 devtoname(dev), cmd, data, flag));
956         if (IS_STATE(mynor)) {
957                 struct termios *ct;
958
959                 switch (mynor & SI_STATE_MASK) {
960                 case SI_INIT_STATE_MASK:
961                         ct = IS_CALLOUT(mynor) ? &pp->sp_iout : &pp->sp_iin;
962                         break;
963                 case SI_LOCK_STATE_MASK:
964                         ct = IS_CALLOUT(mynor) ? &pp->sp_lout : &pp->sp_lin;
965                         break;
966                 default:
967                         return (ENODEV);
968                 }
969                 switch (cmd) {
970                 case TIOCSETA:
971                         error = suser(p);
972                         if (error != 0)
973                                 return (error);
974                         *ct = *(struct termios *)data;
975                         return (0);
976                 case TIOCGETA:
977                         *(struct termios *)data = *ct;
978                         return (0);
979                 case TIOCGETD:
980                         *(int *)data = TTYDISC;
981                         return (0);
982                 case TIOCGWINSZ:
983                         bzero(data, sizeof(struct winsize));
984                         return (0);
985                 default:
986                         return (ENOTTY);
987                 }
988         }
989         /*
990          * Do the old-style ioctl compat routines...
991          */
992 #if defined(COMPAT_43)
993         term = tp->t_termios;
994         oldcmd = cmd;
995         error = ttsetcompat(tp, &cmd, data, &term);
996         if (error != 0)
997                 return (error);
998         if (cmd != oldcmd)
999                 data = (caddr_t)&term;
1000 #endif
1001         /*
1002          * Do the initial / lock state business
1003          */
1004         if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1005                 int     cc;
1006                 struct termios *dt = (struct termios *)data;
1007                 struct termios *lt = mynor & SI_CALLOUT_MASK
1008                                      ? &pp->sp_lout : &pp->sp_lin;
1009
1010                 dt->c_iflag = (tp->t_iflag & lt->c_iflag) |
1011                         (dt->c_iflag & ~lt->c_iflag);
1012                 dt->c_oflag = (tp->t_oflag & lt->c_oflag) |
1013                         (dt->c_oflag & ~lt->c_oflag);
1014                 dt->c_cflag = (tp->t_cflag & lt->c_cflag) |
1015                         (dt->c_cflag & ~lt->c_cflag);
1016                 dt->c_lflag = (tp->t_lflag & lt->c_lflag) |
1017                         (dt->c_lflag & ~lt->c_lflag);
1018                 for (cc = 0; cc < NCCS; ++cc)
1019                         if (lt->c_cc[cc] != 0)
1020                                 dt->c_cc[cc] = tp->t_cc[cc];
1021                 if (lt->c_ispeed != 0)
1022                         dt->c_ispeed = tp->t_ispeed;
1023                 if (lt->c_ospeed != 0)
1024                         dt->c_ospeed = tp->t_ospeed;
1025         }
1026
1027         /*
1028          * Block user-level writes to give the ttywait()
1029          * a chance to completely drain for commands
1030          * that require the port to be in a quiescent state.
1031          */
1032         switch (cmd) {
1033         case TIOCSETAW:
1034         case TIOCSETAF:
1035         case TIOCDRAIN:
1036 #ifdef COMPAT_43
1037         case TIOCSETP:
1038 #endif
1039                 blocked++;      /* block writes for ttywait() and siparam() */
1040                 si_write_enable(pp, 0);
1041         }
1042
1043         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1044         if (error != ENOIOCTL)
1045                 goto out;
1046
1047         oldspl = spltty();
1048
1049         error = ttioctl(tp, cmd, data, flag);
1050         si_disc_optim(tp, &tp->t_termios, pp);
1051         if (error != ENOIOCTL) {
1052                 splx(oldspl);
1053                 goto out;
1054         }
1055
1056         error = 0;
1057         switch (cmd) {
1058         case TIOCSBRK:
1059                 si_command(pp, SBREAK, SI_WAIT);
1060                 break;
1061         case TIOCCBRK:
1062                 si_command(pp, EBREAK, SI_WAIT);
1063                 break;
1064         case TIOCSDTR:
1065                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1066                 break;
1067         case TIOCCDTR:
1068                 (void) si_modem(pp, SET, 0);
1069                 break;
1070         case TIOCMSET:
1071                 (void) si_modem(pp, SET, *(int *)data);
1072                 break;
1073         case TIOCMBIS:
1074                 (void) si_modem(pp, BIS, *(int *)data);
1075                 break;
1076         case TIOCMBIC:
1077                 (void) si_modem(pp, BIC, *(int *)data);
1078                 break;
1079         case TIOCMGET:
1080                 *(int *)data = si_modem(pp, GET, 0);
1081                 break;
1082         case TIOCMSDTRWAIT:
1083                 /* must be root since the wait applies to following logins */
1084                 error = suser(p);
1085                 if (error == 0)
1086                         pp->sp_dtr_wait = *(int *)data * hz / 100;
1087                 break;
1088         case TIOCMGDTRWAIT:
1089                 *(int *)data = pp->sp_dtr_wait * 100 / hz;
1090                 break;
1091         default:
1092                 error = ENOTTY;
1093         }
1094         splx(oldspl);
1095
1096 out:
1097         DPRINT((pp, DBG_IOCTL|DBG_EXIT, "siioctl ret %d\n", error));
1098         if (blocked)
1099                 si_write_enable(pp, 1);
1100         return(error);
1101 }
1102
1103 /*
1104  * Handle the Specialix ioctls. All MUST be called via the CONTROL device
1105  */
1106 static int
1107 si_Sioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1108 {
1109         struct si_softc *xsc;
1110         struct si_port *xpp;
1111         volatile struct si_reg *regp;
1112         struct si_tcsi *dp;
1113         struct si_pstat *sps;
1114         int *ip, error = 0;
1115         int oldspl;
1116         int card, port;
1117         int mynor = minor(dev);
1118
1119         DPRINT((0, DBG_ENTRY|DBG_IOCTL, "si_Sioctl(%s,%lx,%x,%x)\n",
1120                 devtoname(dev), cmd, data, flag));
1121
1122 #if 1
1123         DPRINT((0, DBG_IOCTL, "TCSI_PORT=%x\n", TCSI_PORT));
1124         DPRINT((0, DBG_IOCTL, "TCSI_CCB=%x\n", TCSI_CCB));
1125         DPRINT((0, DBG_IOCTL, "TCSI_TTY=%x\n", TCSI_TTY));
1126 #endif
1127
1128         if (!IS_CONTROLDEV(mynor)) {
1129                 DPRINT((0, DBG_IOCTL|DBG_FAIL, "not called from control device!\n"));
1130                 return(ENODEV);
1131         }
1132
1133         oldspl = spltty();      /* better safe than sorry */
1134
1135         ip = (int *)data;
1136
1137 #define SUCHECK if ((error = suser(p))) goto out
1138
1139         switch (cmd) {
1140         case TCSIPORTS:
1141                 *ip = si_Nports;
1142                 goto out;
1143         case TCSIMODULES:
1144                 *ip = si_Nmodules;
1145                 goto out;
1146         case TCSISDBG_ALL:
1147                 SUCHECK;
1148                 si_debug = *ip;
1149                 goto out;
1150         case TCSIGDBG_ALL:
1151                 *ip = si_debug;
1152                 goto out;
1153         default:
1154                 /*
1155                  * Check that a controller for this port exists
1156                  */
1157
1158                 /* may also be a struct si_pstat, a superset of si_tcsi */
1159
1160                 dp = (struct si_tcsi *)data;
1161                 sps = (struct si_pstat *)data;
1162                 card = dp->tc_card;
1163                 xsc = devclass_get_softc(si_devclass, card);    /* check.. */
1164                 if (xsc == NULL || xsc->sc_type == SIEMPTY) {
1165                         error = ENOENT;
1166                         goto out;
1167                 }
1168                 /*
1169                  * And check that a port exists
1170                  */
1171                 port = dp->tc_port;
1172                 if (port < 0 || port >= xsc->sc_nport) {
1173                         error = ENOENT;
1174                         goto out;
1175                 }
1176                 xpp = xsc->sc_ports + port;
1177                 regp = (struct si_reg *)xsc->sc_maddr;
1178         }
1179
1180         switch (cmd) {
1181         case TCSIDEBUG:
1182 #ifdef  SI_DEBUG
1183                 SUCHECK;
1184                 if (xpp->sp_debug)
1185                         xpp->sp_debug = 0;
1186                 else {
1187                         xpp->sp_debug = DBG_ALL;
1188                         DPRINT((xpp, DBG_IOCTL, "debug toggled %s\n",
1189                                 (xpp->sp_debug&DBG_ALL)?"ON":"OFF"));
1190                 }
1191                 break;
1192 #else
1193                 error = ENODEV;
1194                 goto out;
1195 #endif
1196         case TCSISDBG_LEVEL:
1197         case TCSIGDBG_LEVEL:
1198 #ifdef  SI_DEBUG
1199                 if (cmd == TCSIGDBG_LEVEL) {
1200                         dp->tc_dbglvl = xpp->sp_debug;
1201                 } else {
1202                         SUCHECK;
1203                         xpp->sp_debug = dp->tc_dbglvl;
1204                 }
1205                 break;
1206 #else
1207                 error = ENODEV;
1208                 goto out;
1209 #endif
1210         case TCSIGRXIT:
1211                 dp->tc_int = regp->rx_int_count;
1212                 break;
1213         case TCSIRXIT:
1214                 SUCHECK;
1215                 regp->rx_int_count = dp->tc_int;
1216                 break;
1217         case TCSIGIT:
1218                 dp->tc_int = regp->int_count;
1219                 break;
1220         case TCSIIT:
1221                 SUCHECK;
1222                 regp->int_count = dp->tc_int;
1223                 break;
1224         case TCSISTATE:
1225                 dp->tc_int = xpp->sp_ccb->hi_ip;
1226                 break;
1227         /* these next three use a different structure */
1228         case TCSI_PORT:
1229                 SUCHECK;
1230                 si_bcopy(xpp, &sps->tc_siport, sizeof(sps->tc_siport));
1231                 break;
1232         case TCSI_CCB:
1233                 SUCHECK;
1234                 si_vbcopy(xpp->sp_ccb, &sps->tc_ccb, sizeof(sps->tc_ccb));
1235                 break;
1236         case TCSI_TTY:
1237                 SUCHECK;
1238                 si_bcopy(xpp->sp_tty, &sps->tc_tty, sizeof(sps->tc_tty));
1239                 break;
1240         default:
1241                 error = EINVAL;
1242                 goto out;
1243         }
1244 out:
1245         splx(oldspl);
1246         return(error);          /* success */
1247 }
1248
1249 /*
1250  *      siparam()       : Configure line params
1251  *      called at spltty();
1252  *      this may sleep, does not flush, nor wait for drain, nor block writes
1253  *      caller must arrange this if it's important..
1254  */
1255 static int
1256 siparam(struct tty *tp, struct termios *t)
1257 {
1258         struct si_port *pp = TP2PP(tp);
1259         volatile struct si_channel *ccbp;
1260         int oldspl, cflag, iflag, oflag, lflag;
1261         int error = 0;          /* shutup gcc */
1262         int ispeed = 0;         /* shutup gcc */
1263         int ospeed = 0;         /* shutup gcc */
1264         BYTE val;
1265
1266         DPRINT((pp, DBG_ENTRY|DBG_PARAM, "siparam(%x,%x)\n", tp, t));
1267         cflag = t->c_cflag;
1268         iflag = t->c_iflag;
1269         oflag = t->c_oflag;
1270         lflag = t->c_lflag;
1271         DPRINT((pp, DBG_PARAM, "OFLAG 0x%x CFLAG 0x%x IFLAG 0x%x LFLAG 0x%x\n",
1272                 oflag, cflag, iflag, lflag));
1273
1274         /* XXX - if Jet host and SXDC module, use extended baud rates */
1275
1276         /* if not hung up.. */
1277         if (t->c_ospeed != 0) {
1278                 /* translate baud rate to firmware values */
1279                 ospeed = ttspeedtab(t->c_ospeed, bdrates);
1280                 ispeed = t->c_ispeed ?
1281                          ttspeedtab(t->c_ispeed, bdrates) : ospeed;
1282
1283                 /* enforce legit baud rate */
1284                 if (ospeed < 0 || ispeed < 0)
1285                         return (EINVAL);
1286         }
1287
1288         oldspl = spltty();
1289
1290         ccbp = pp->sp_ccb;
1291
1292         /* ========== set hi_break ========== */
1293         val = 0;
1294         if (iflag & IGNBRK)             /* Breaks */
1295                 val |= BR_IGN;
1296         if (iflag & BRKINT)             /* Interrupt on break? */
1297                 val |= BR_INT;
1298         if (iflag & PARMRK)             /* Parity mark? */
1299                 val |= BR_PARMRK;
1300         if (iflag & IGNPAR)             /* Ignore chars with parity errors? */
1301                 val |= BR_PARIGN;
1302         ccbp->hi_break = val;
1303
1304         /* ========== set hi_csr ========== */
1305         /* if not hung up.. */
1306         if (t->c_ospeed != 0) {
1307                 /* Set I/O speeds */
1308                  val = (ispeed << 4) | ospeed;
1309         }
1310         ccbp->hi_csr = val;
1311
1312         /* ========== set hi_mr2 ========== */
1313         val = 0;
1314         if (cflag & CSTOPB)                             /* Stop bits */
1315                 val |= MR2_2_STOP;
1316         else
1317                 val |= MR2_1_STOP;
1318         /*
1319          * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1320          * a DCE, hence the reverse sense of RTS and CTS
1321          */
1322         /* Output Flow - RTS must be raised before data can be sent */
1323         if (cflag & CCTS_OFLOW)
1324                 val |= MR2_RTSCONT;
1325
1326         ccbp->hi_mr2 = val;
1327
1328         /* ========== set hi_mr1 ========== */
1329         val = 0;
1330         if (!(cflag & PARENB))                          /* Parity */
1331                 val |= MR1_NONE;
1332         else
1333                 val |= MR1_WITH;
1334         if (cflag & PARODD)
1335                 val |= MR1_ODD;
1336
1337         if ((cflag & CS8) == CS8) {                     /* 8 data bits? */
1338                 val |= MR1_8_BITS;
1339         } else if ((cflag & CS7) == CS7) {              /* 7 data bits? */
1340                 val |= MR1_7_BITS;
1341         } else if ((cflag & CS6) == CS6) {              /* 6 data bits? */
1342                 val |= MR1_6_BITS;
1343         } else {                                        /* Must be 5 */
1344                 val |= MR1_5_BITS;
1345         }
1346         /*
1347          * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1348          * a DCE, hence the reverse sense of RTS and CTS
1349          */
1350         /* Input Flow - CTS is raised when port is ready to receive data */
1351         if (cflag & CRTS_IFLOW)
1352                 val |= MR1_CTSCONT;
1353
1354         ccbp->hi_mr1 = val;
1355
1356         /* ========== set hi_mask ========== */
1357         val = 0xff;
1358         if ((cflag & CS8) == CS8) {                     /* 8 data bits? */
1359                 val &= 0xFF;
1360         } else if ((cflag & CS7) == CS7) {              /* 7 data bits? */
1361                 val &= 0x7F;
1362         } else if ((cflag & CS6) == CS6) {              /* 6 data bits? */
1363                 val &= 0x3F;
1364         } else {                                        /* Must be 5 */
1365                 val &= 0x1F;
1366         }
1367         if (iflag & ISTRIP)
1368                 val &= 0x7F;
1369
1370         ccbp->hi_mask = val;
1371
1372         /* ========== set hi_prtcl ========== */
1373         val = SP_DCEN;          /* Monitor DCD always, or TIOCMGET misses it */
1374         if (iflag & IXANY)
1375                 val |= SP_TANY;
1376         if (iflag & IXON)
1377                 val |= SP_TXEN;
1378         if (iflag & IXOFF)
1379                 val |= SP_RXEN;
1380         if (iflag & INPCK)
1381                 val |= SP_PAEN;
1382
1383         ccbp->hi_prtcl = val;
1384
1385
1386         /* ========== set hi_{rx|tx}{on|off} ========== */
1387         /* XXX: the card TOTALLY shields us from the flow control... */
1388         ccbp->hi_txon = t->c_cc[VSTART];
1389         ccbp->hi_txoff = t->c_cc[VSTOP];
1390
1391         ccbp->hi_rxon = t->c_cc[VSTART];
1392         ccbp->hi_rxoff = t->c_cc[VSTOP];
1393
1394         /* ========== send settings to the card ========== */
1395         /* potential sleep here */
1396         if (ccbp->hi_stat == IDLE_CLOSE)                /* Not yet open */
1397                 si_command(pp, LOPEN, SI_WAIT);         /* open it */
1398         else
1399                 si_command(pp, CONFIG, SI_WAIT);        /* change params */
1400
1401         /* ========== set DTR etc ========== */
1402         /* Hangup if ospeed == 0 */
1403         if (t->c_ospeed == 0) {
1404                 (void) si_modem(pp, BIC, TIOCM_DTR|TIOCM_RTS);
1405         } else {
1406                 /*
1407                  * If the previous speed was 0, may need to re-enable
1408                  * the modem signals
1409                  */
1410                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1411         }
1412
1413         DPRINT((pp, DBG_PARAM, "siparam, complete: MR1 %x MR2 %x HI_MASK %x PRTCL %x HI_BREAK %x\n",
1414                 ccbp->hi_mr1, ccbp->hi_mr2, ccbp->hi_mask, ccbp->hi_prtcl, ccbp->hi_break));
1415
1416         splx(oldspl);
1417         return(error);
1418 }
1419
1420 /*
1421  * Enable or Disable the writes to this channel...
1422  * "state" ->  enabled = 1; disabled = 0;
1423  */
1424 static void
1425 si_write_enable(struct si_port *pp, int state)
1426 {
1427         int oldspl;
1428
1429         oldspl = spltty();
1430
1431         if (state) {
1432                 pp->sp_state &= ~SS_BLOCKWRITE;
1433                 if (pp->sp_state & SS_WAITWRITE) {
1434                         pp->sp_state &= ~SS_WAITWRITE;
1435                         /* thunder away! */
1436                         wakeup((caddr_t)pp);
1437                 }
1438         } else {
1439                 pp->sp_state |= SS_BLOCKWRITE;
1440         }
1441
1442         splx(oldspl);
1443 }
1444
1445 /*
1446  * Set/Get state of modem control lines.
1447  * Due to DCE-like behaviour of the adapter, some signals need translation:
1448  *      TIOCM_DTR       DSR
1449  *      TIOCM_RTS       CTS
1450  */
1451 static int
1452 si_modem(struct si_port *pp, enum si_mctl cmd, int bits)
1453 {
1454         volatile struct si_channel *ccbp;
1455         int x;
1456
1457         DPRINT((pp, DBG_ENTRY|DBG_MODEM, "si_modem(%x,%s,%x)\n", pp, si_mctl2str(cmd), bits));
1458         ccbp = pp->sp_ccb;              /* Find channel address */
1459         switch (cmd) {
1460         case GET:
1461                 x = ccbp->hi_ip;
1462                 bits = TIOCM_LE;
1463                 if (x & IP_DCD)         bits |= TIOCM_CAR;
1464                 if (x & IP_DTR)         bits |= TIOCM_DTR;
1465                 if (x & IP_RTS)         bits |= TIOCM_RTS;
1466                 if (x & IP_RI)          bits |= TIOCM_RI;
1467                 return(bits);
1468         case SET:
1469                 ccbp->hi_op &= ~(OP_DSR|OP_CTS);
1470                 /* fall through */
1471         case BIS:
1472                 x = 0;
1473                 if (bits & TIOCM_DTR)
1474                         x |= OP_DSR;
1475                 if (bits & TIOCM_RTS)
1476                         x |= OP_CTS;
1477                 ccbp->hi_op |= x;
1478                 break;
1479         case BIC:
1480                 if (bits & TIOCM_DTR)
1481                         ccbp->hi_op &= ~OP_DSR;
1482                 if (bits & TIOCM_RTS)
1483                         ccbp->hi_op &= ~OP_CTS;
1484         }
1485         return 0;
1486 }
1487
1488 /*
1489  * Handle change of modem state
1490  */
1491 static void
1492 si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip)
1493 {
1494                                                         /* if a modem dev */
1495         if (hi_ip & IP_DCD) {
1496                 if (!(pp->sp_last_hi_ip & IP_DCD)) {
1497                         DPRINT((pp, DBG_INTR, "modem carr on t_line %d\n",
1498                                 tp->t_line));
1499                         (void)(*linesw[tp->t_line].l_modem)(tp, 1);
1500                 }
1501         } else {
1502                 if (pp->sp_last_hi_ip & IP_DCD) {
1503                         DPRINT((pp, DBG_INTR, "modem carr off\n"));
1504                         if ((*linesw[tp->t_line].l_modem)(tp, 0))
1505                                 (void) si_modem(pp, SET, 0);
1506                 }
1507         }
1508         pp->sp_last_hi_ip = hi_ip;
1509
1510 }
1511
1512 /*
1513  * Poller to catch missed interrupts.
1514  *
1515  * Note that the SYSV Specialix drivers poll at 100 times per second to get
1516  * better response.  We could really use a "periodic" version timeout(). :-)
1517  */
1518 #ifdef POLL
1519 static void
1520 si_poll(void *nothing)
1521 {
1522         struct si_softc *sc;
1523         int i;
1524         volatile struct si_reg *regp;
1525         struct si_port *pp;
1526         int lost, oldspl, port;
1527
1528         DPRINT((0, DBG_POLL, "si_poll()\n"));
1529         oldspl = spltty();
1530         if (in_intr)
1531                 goto out;
1532         lost = 0;
1533         for (i = 0; i < si_numunits; i++) {
1534                 sc = devclass_get_softc(si_devclass, i);
1535                 if (sc == NULL || sc->sc_type == SIEMPTY)
1536                         continue;
1537                 regp = (struct si_reg *)sc->sc_maddr;
1538
1539                 /*
1540                  * See if there has been a pending interrupt for 2 seconds
1541                  * or so. The test (int_scounter >= 200) won't correspond
1542                  * to 2 seconds if int_count gets changed.
1543                  */
1544                 if (regp->int_pending != 0) {
1545                         if (regp->int_scounter >= 200 &&
1546                             regp->initstat == 1) {
1547                                 printf("si%d: lost intr\n", i);
1548                                 lost++;
1549                         }
1550                 } else {
1551                         regp->int_scounter = 0;
1552                 }
1553
1554                 /*
1555                  * gripe about no input flow control..
1556                  */
1557                 pp = sc->sc_ports;
1558                 for (port = 0; port < sc->sc_nport; pp++, port++) {
1559                         if (pp->sp_delta_overflows > 0) {
1560                                 printf("si%d: %d tty level buffer overflows\n",
1561                                         i, pp->sp_delta_overflows);
1562                                 pp->sp_delta_overflows = 0;
1563                         }
1564                 }
1565         }
1566         if (lost || si_realpoll)
1567                 si_intr(NULL);  /* call intr with fake vector */
1568 out:
1569         splx(oldspl);
1570
1571         timeout(si_poll, (caddr_t)0L, si_pollrate);
1572 }
1573 #endif  /* ifdef POLL */
1574
1575 /*
1576  * The interrupt handler polls ALL ports on ALL adapters each time
1577  * it is called.
1578  */
1579
1580 static BYTE si_rxbuf[SI_BUFFERSIZE];    /* input staging area */
1581 static BYTE si_txbuf[SI_BUFFERSIZE];    /* output staging area */
1582
1583 void
1584 si_intr(void *arg)
1585 {
1586         struct si_softc *sc;
1587         struct si_port *pp;
1588         volatile struct si_channel *ccbp;
1589         struct tty *tp;
1590         volatile caddr_t maddr;
1591         BYTE op, ip;
1592         int x, card, port, n, i, isopen;
1593         volatile BYTE *z;
1594         BYTE c;
1595
1596         sc = arg;
1597
1598         DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "si_intr\n"));
1599         if (in_intr)
1600                 return;
1601         in_intr = 1;
1602
1603         /*
1604          * When we get an int we poll all the channels and do ALL pending
1605          * work, not just the first one we find. This allows all cards to
1606          * share the same vector.
1607          *
1608          * XXX - But if we're sharing the vector with something that's NOT
1609          * a SI/XIO/SX card, we may be making more work for ourselves.
1610          */
1611         for (card = 0; card < si_numunits; card++) {
1612                 sc = devclass_get_softc(si_devclass, card);
1613                 if (sc == NULL || sc->sc_type == SIEMPTY)
1614                         continue;
1615
1616                 /*
1617                  * First, clear the interrupt
1618                  */
1619                 switch(sc->sc_type) {
1620                 case SIHOST:
1621                         maddr = sc->sc_maddr;
1622                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1623                                                         /* flag nothing pending */
1624                         *(maddr+SIINTCL) = 0x00;        /* Set IRQ clear */
1625                         *(maddr+SIINTCL_CL) = 0x00;     /* Clear IRQ clear */
1626                         break;
1627                 case SIHOST2:
1628                         maddr = sc->sc_maddr;
1629                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1630                         *(maddr+SIPLIRQCLR) = 0x00;
1631                         *(maddr+SIPLIRQCLR) = 0x10;
1632                         break;
1633                 case SIPCI:
1634                         maddr = sc->sc_maddr;
1635                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1636                         *(maddr+SIPCIINTCL) = 0x0;
1637                         break;
1638                 case SIJETPCI:  /* fall through to JETISA case */
1639                 case SIJETISA:
1640                         maddr = sc->sc_maddr;
1641                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1642                         *(maddr+SIJETINTCL) = 0x0;
1643                         break;
1644                 case SIEISA:
1645                         maddr = sc->sc_maddr;
1646                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1647                         (void)inb(sc->sc_iobase + 3);
1648                         break;
1649                 case SIEMPTY:
1650                 default:
1651                         continue;
1652                 }
1653                 ((volatile struct si_reg *)maddr)->int_scounter = 0;
1654
1655                 /*
1656                  * check each port
1657                  */
1658                 for (pp = sc->sc_ports, port = 0; port < sc->sc_nport;
1659                      pp++, port++) {
1660                         ccbp = pp->sp_ccb;
1661                         tp = pp->sp_tty;
1662
1663                         /*
1664                          * See if a command has completed ?
1665                          */
1666                         if (ccbp->hi_stat != pp->sp_pend) {
1667                                 DPRINT((pp, DBG_INTR,
1668                                         "si_intr hi_stat = 0x%x, pend = %d\n",
1669                                         ccbp->hi_stat, pp->sp_pend));
1670                                 switch(pp->sp_pend) {
1671                                 case LOPEN:
1672                                 case MPEND:
1673                                 case MOPEN:
1674                                 case CONFIG:
1675                                 case SBREAK:
1676                                 case EBREAK:
1677                                         pp->sp_pend = ccbp->hi_stat;
1678                                                 /* sleeping in si_command */
1679                                         wakeup(&pp->sp_state);
1680                                         break;
1681                                 default:
1682                                         pp->sp_pend = ccbp->hi_stat;
1683                                 }
1684                         }
1685
1686                         /*
1687                          * Continue on if it's closed
1688                          */
1689                         if (ccbp->hi_stat == IDLE_CLOSE) {
1690                                 continue;
1691                         }
1692
1693                         /*
1694                          * Do modem state change if not a local device
1695                          */
1696                         si_modem_state(pp, tp, ccbp->hi_ip);
1697
1698                         /*
1699                          * Check to see if we should 'receive' characters.
1700                          */
1701                         if (tp->t_state & TS_CONNECTED &&
1702                             tp->t_state & TS_ISOPEN)
1703                                 isopen = 1;
1704                         else
1705                                 isopen = 0;
1706
1707                         /*
1708                          * Do input break processing
1709                          */
1710                         if (ccbp->hi_state & ST_BREAK) {
1711                                 if (isopen) {
1712                                     (*linesw[tp->t_line].l_rint)(TTY_BI, tp);
1713                                 }
1714                                 ccbp->hi_state &= ~ST_BREAK;   /* A Bit iffy this */
1715                                 DPRINT((pp, DBG_INTR, "si_intr break\n"));
1716                         }
1717
1718                         /*
1719                          * Do RX stuff - if not open then dump any characters.
1720                          * XXX: This is VERY messy and needs to be cleaned up.
1721                          *
1722                          * XXX: can we leave data in the host adapter buffer
1723                          * when the clists are full?  That may be dangerous
1724                          * if the user cannot get an interrupt signal through.
1725                          */
1726
1727         more_rx:        /* XXX Sorry. the nesting was driving me bats! :-( */
1728
1729                         if (!isopen) {
1730                                 ccbp->hi_rxopos = ccbp->hi_rxipos;
1731                                 goto end_rx;
1732                         }
1733
1734                         /*
1735                          * If the tty input buffers are blocked, stop emptying
1736                          * the incoming buffers and let the auto flow control
1737                          * assert..
1738                          */
1739                         if (tp->t_state & TS_TBLOCK) {
1740                                 goto end_rx;
1741                         }
1742
1743                         /*
1744                          * Process read characters if not skipped above
1745                          */
1746                         op = ccbp->hi_rxopos;
1747                         ip = ccbp->hi_rxipos;
1748                         c = ip - op;
1749                         if (c == 0) {
1750                                 goto end_rx;
1751                         }
1752
1753                         n = c & 0xff;
1754                         if (n > 250)
1755                                 n = 250;
1756
1757                         DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1758                                                 n, op, ip));
1759
1760                         /*
1761                          * Suck characters out of host card buffer into the
1762                          * "input staging buffer" - so that we dont leave the
1763                          * host card in limbo while we're possibly echoing
1764                          * characters and possibly flushing input inside the
1765                          * ldisc l_rint() routine.
1766                          */
1767                         if (n <= SI_BUFFERSIZE - op) {
1768
1769                                 DPRINT((pp, DBG_INTR, "\tsingle copy\n"));
1770                                 z = ccbp->hi_rxbuf + op;
1771                                 si_vbcopy(z, si_rxbuf, n);
1772
1773                                 op += n;
1774                         } else {
1775                                 x = SI_BUFFERSIZE - op;
1776
1777                                 DPRINT((pp, DBG_INTR, "\tdouble part 1 %d\n", x));
1778                                 z = ccbp->hi_rxbuf + op;
1779                                 si_vbcopy(z, si_rxbuf, x);
1780
1781                                 DPRINT((pp, DBG_INTR, "\tdouble part 2 %d\n",
1782                                         n - x));
1783                                 z = ccbp->hi_rxbuf;
1784                                 si_vbcopy(z, si_rxbuf + x, n - x);
1785
1786                                 op += n;
1787                         }
1788
1789                         /* clear collected characters from buffer */
1790                         ccbp->hi_rxopos = op;
1791
1792                         DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1793                                                 n, op, ip));
1794
1795                         /*
1796                          * at this point...
1797                          * n = number of chars placed in si_rxbuf
1798                          */
1799
1800                         /*
1801                          * Avoid the grotesquely inefficient lineswitch
1802                          * routine (ttyinput) in "raw" mode. It usually
1803                          * takes about 450 instructions (that's without
1804                          * canonical processing or echo!). slinput is
1805                          * reasonably fast (usually 40 instructions
1806                          * plus call overhead).
1807                          */
1808                         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1809
1810                                 /* block if the driver supports it */
1811                                 if (tp->t_rawq.c_cc + n >= SI_I_HIGH_WATER &&
1812                                     (tp->t_cflag & CRTS_IFLOW ||
1813                                      tp->t_iflag & IXOFF) &&
1814                                     !(tp->t_state & TS_TBLOCK))
1815                                         ttyblock(tp);
1816
1817                                 tk_nin += n;
1818                                 tk_rawcc += n;
1819                                 tp->t_rawcc += n;
1820
1821                                 pp->sp_delta_overflows +=
1822                                     b_to_q((char *)si_rxbuf, n, &tp->t_rawq);
1823
1824                                 ttwakeup(tp);
1825                                 if (tp->t_state & TS_TTSTOP &&
1826                                     (tp->t_iflag & IXANY ||
1827                                      tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1828                                         tp->t_state &= ~TS_TTSTOP;
1829                                         tp->t_lflag &= ~FLUSHO;
1830                                         si_start(tp);
1831                                 }
1832                         } else {
1833                                 /*
1834                                  * It'd be nice to not have to go through the
1835                                  * function call overhead for each char here.
1836                                  * It'd be nice to block input it, saving a
1837                                  * loop here and the call/return overhead.
1838                                  */
1839                                 for(x = 0; x < n; x++) {
1840                                         i = si_rxbuf[x];
1841                                         if ((*linesw[tp->t_line].l_rint)(i, tp)
1842                                              == -1) {
1843                                                 pp->sp_delta_overflows++;
1844                                         }
1845                                         /*
1846                                          * doesn't seem to be much point doing
1847                                          * this here.. this driver has no
1848                                          * softtty processing! ??
1849                                          */
1850                                         if (pp->sp_hotchar && i == pp->sp_hotchar) {
1851                                                 setsofttty();
1852                                         }
1853                                 }
1854                         }
1855                         goto more_rx;   /* try for more until RXbuf is empty */
1856
1857         end_rx:         /* XXX: Again, sorry about the gotos.. :-) */
1858
1859                         /*
1860                          * Do TX stuff
1861                          */
1862                         (*linesw[tp->t_line].l_start)(tp);
1863
1864                 } /* end of for (all ports on this controller) */
1865         } /* end of for (all controllers) */
1866
1867         in_intr = 0;
1868         DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "end si_intr\n"));
1869 }
1870
1871 /*
1872  * Nudge the transmitter...
1873  *
1874  * XXX: I inherited some funny code here.  It implies the host card only
1875  * interrupts when the transmit buffer reaches the low-water-mark, and does
1876  * not interrupt when it's actually hits empty.  In some cases, we have
1877  * processes waiting for complete drain, and we need to simulate an interrupt
1878  * about when we think the buffer is going to be empty (and retry if not).
1879  * I really am not certain about this...  I *need* the hardware manuals.
1880  */
1881 static void
1882 si_start(struct tty *tp)
1883 {
1884         struct si_port *pp;
1885         volatile struct si_channel *ccbp;
1886         struct clist *qp;
1887         BYTE ipos;
1888         int nchar;
1889         int oldspl, count, n, amount, buffer_full;
1890
1891         oldspl = spltty();
1892
1893         qp = &tp->t_outq;
1894         pp = TP2PP(tp);
1895
1896         DPRINT((pp, DBG_ENTRY|DBG_START,
1897                 "si_start(%x) t_state %x sp_state %x t_outq.c_cc %d\n",
1898                 tp, tp->t_state, pp->sp_state, qp->c_cc));
1899
1900         if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
1901                 goto out;
1902
1903         buffer_full = 0;
1904         ccbp = pp->sp_ccb;
1905
1906         count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1907         DPRINT((pp, DBG_START, "count %d\n", (BYTE)count));
1908
1909         while ((nchar = qp->c_cc) > 0) {
1910                 if ((BYTE)count >= 255) {
1911                         buffer_full++;
1912                         break;
1913                 }
1914                 amount = min(nchar, (255 - (BYTE)count));
1915                 ipos = (unsigned int)ccbp->hi_txipos;
1916                 n = q_to_b(&tp->t_outq, si_txbuf, amount);
1917                 /* will it fit in one lump? */
1918                 if ((SI_BUFFERSIZE - ipos) >= n) {
1919                         si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos], n);
1920                 } else {
1921                         si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos],
1922                                 SI_BUFFERSIZE - ipos);
1923                         si_bcopyv(si_txbuf + (SI_BUFFERSIZE - ipos),
1924                                 &ccbp->hi_txbuf[0], n - (SI_BUFFERSIZE - ipos));
1925                 }
1926                 ccbp->hi_txipos += n;
1927                 count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1928         }
1929
1930         if (count != 0 && nchar == 0) {
1931                 tp->t_state |= TS_BUSY;
1932         } else {
1933                 tp->t_state &= ~TS_BUSY;
1934         }
1935
1936         /* wakeup time? */
1937         ttwwakeup(tp);
1938
1939         DPRINT((pp, DBG_START, "count %d, nchar %d, tp->t_state 0x%x\n",
1940                 (BYTE)count, nchar, tp->t_state));
1941
1942         if (tp->t_state & TS_BUSY)
1943         {
1944                 int time;
1945
1946                 time = ttspeedtab(tp->t_ospeed, chartimes);
1947
1948                 if (time > 0) {
1949                         if (time < nchar)
1950                                 time = nchar / time;
1951                         else
1952                                 time = 2;
1953                 } else {
1954                         DPRINT((pp, DBG_START,
1955                                 "bad char time value! %d\n", time));
1956                         time = hz/10;
1957                 }
1958
1959                 if ((pp->sp_state & (SS_LSTART|SS_INLSTART)) == SS_LSTART) {
1960                         untimeout(si_lstart, (caddr_t)pp, pp->lstart_ch);
1961                 } else {
1962                         pp->sp_state |= SS_LSTART;
1963                 }
1964                 DPRINT((pp, DBG_START, "arming lstart, time=%d\n", time));
1965                 pp->lstart_ch = timeout(si_lstart, (caddr_t)pp, time);
1966         }
1967
1968 out:
1969         splx(oldspl);
1970         DPRINT((pp, DBG_EXIT|DBG_START, "leave si_start()\n"));
1971 }
1972
1973 /*
1974  * Note: called at splsoftclock from the timeout code
1975  * This has to deal with two things...  cause wakeups while waiting for
1976  * tty drains on last process exit, and call l_start at about the right
1977  * time for protocols like ppp.
1978  */
1979 static void
1980 si_lstart(void *arg)
1981 {
1982         struct si_port *pp = arg;
1983         struct tty *tp;
1984         int oldspl;
1985
1986         DPRINT((pp, DBG_ENTRY|DBG_LSTART, "si_lstart(%x) sp_state %x\n",
1987                 pp, pp->sp_state));
1988
1989         oldspl = spltty();
1990
1991         if ((pp->sp_state & SS_OPEN) == 0 || (pp->sp_state & SS_LSTART) == 0) {
1992                 splx(oldspl);
1993                 return;
1994         }
1995         pp->sp_state &= ~SS_LSTART;
1996         pp->sp_state |= SS_INLSTART;
1997
1998         tp = pp->sp_tty;
1999
2000         /* deal with the process exit case */
2001         ttwwakeup(tp);
2002
2003         /* nudge protocols - eg: ppp */
2004         (*linesw[tp->t_line].l_start)(tp);
2005
2006         pp->sp_state &= ~SS_INLSTART;
2007         splx(oldspl);
2008 }
2009
2010 /*
2011  * Stop output on a line. called at spltty();
2012  */
2013 void
2014 si_stop(struct tty *tp, int rw)
2015 {
2016         volatile struct si_channel *ccbp;
2017         struct si_port *pp;
2018
2019         pp = TP2PP(tp);
2020         ccbp = pp->sp_ccb;
2021
2022         DPRINT((TP2PP(tp), DBG_ENTRY|DBG_STOP, "si_stop(%x,%x)\n", tp, rw));
2023
2024         /* XXX: must check (rw & FWRITE | FREAD) etc flushing... */
2025         if (rw & FWRITE) {
2026                 /* what level are we meant to be flushing anyway? */
2027                 if (tp->t_state & TS_BUSY) {
2028                         si_command(TP2PP(tp), WFLUSH, SI_NOWAIT);
2029                         tp->t_state &= ~TS_BUSY;
2030                         ttwwakeup(tp);  /* Bruce???? */
2031                 }
2032         }
2033 #if 1   /* XXX: this doesn't work right yet.. */
2034         /* XXX: this may have been failing because we used to call l_rint()
2035          * while we were looping based on these two counters. Now, we collect
2036          * the data and then loop stuffing it into l_rint(), making this
2037          * useless.  Should we cause this to blow away the staging buffer?
2038          */
2039         if (rw & FREAD) {
2040                 ccbp->hi_rxopos = ccbp->hi_rxipos;
2041         }
2042 #endif
2043 }
2044
2045 /*
2046  * Issue a command to the host card CPU.
2047  */
2048
2049 static void
2050 si_command(struct si_port *pp, int cmd, int waitflag)
2051 {
2052         int oldspl;
2053         volatile struct si_channel *ccbp = pp->sp_ccb;
2054         int x;
2055
2056         DPRINT((pp, DBG_ENTRY|DBG_PARAM, "si_command(%x,%x,%d): hi_stat 0x%x\n",
2057                 pp, cmd, waitflag, ccbp->hi_stat));
2058
2059         oldspl = spltty();              /* Keep others out */
2060
2061         /* wait until it's finished what it was doing.. */
2062         /* XXX: sits in IDLE_BREAK until something disturbs it or break
2063          * is turned off. */
2064         while((x = ccbp->hi_stat) != IDLE_OPEN &&
2065                         x != IDLE_CLOSE &&
2066                         x != IDLE_BREAK &&
2067                         x != cmd) {
2068                 if (in_intr) {                  /* Prevent sleep in intr */
2069                         DPRINT((pp, DBG_PARAM,
2070                                 "cmd intr collision - completing %d\trequested %d\n",
2071                                 x, cmd));
2072                         splx(oldspl);
2073                         return;
2074                 } else if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, TTIPRI|PCATCH,
2075                                 "sicmd1", 1)) {
2076                         splx(oldspl);
2077                         return;
2078                 }
2079         }
2080         /* it should now be in IDLE_{OPEN|CLOSE|BREAK}, or "cmd" */
2081
2082         /* if there was a pending command, cause a state-change wakeup */
2083         switch(pp->sp_pend) {
2084         case LOPEN:
2085         case MPEND:
2086         case MOPEN:
2087         case CONFIG:
2088         case SBREAK:
2089         case EBREAK:
2090                 wakeup(&pp->sp_state);
2091                 break;
2092         default:
2093                 break;
2094         }
2095
2096         pp->sp_pend = cmd;              /* New command pending */
2097         ccbp->hi_stat = cmd;            /* Post it */
2098
2099         if (waitflag) {
2100                 if (in_intr) {          /* If in interrupt handler */
2101                         DPRINT((pp, DBG_PARAM,
2102                                 "attempt to sleep in si_intr - cmd req %d\n",
2103                                 cmd));
2104                         splx(oldspl);
2105                         return;
2106                 } else while(ccbp->hi_stat != IDLE_OPEN &&
2107                              ccbp->hi_stat != IDLE_BREAK) {
2108                         if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, TTIPRI|PCATCH,
2109                             "sicmd2", 0))
2110                                 break;
2111                 }
2112         }
2113         splx(oldspl);
2114 }
2115
2116 static void
2117 si_disc_optim(struct tty *tp, struct termios *t, struct si_port *pp)
2118 {
2119         /*
2120          * XXX can skip a lot more cases if Smarts.  Maybe
2121          * (IGNCR | ISTRIP | IXON) in c_iflag.  But perhaps we
2122          * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2123          */
2124         if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) &&
2125             (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) &&
2126             (!(t->c_iflag & PARMRK) ||
2127              (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) &&
2128             !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) &&
2129             linesw[tp->t_line].l_rint == ttyinput)
2130                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2131         else
2132                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2133         pp->sp_hotchar = linesw[tp->t_line].l_hotchar;
2134         DPRINT((pp, DBG_OPTIM, "bypass: %s, hotchar: %x\n",
2135                 (tp->t_state & TS_CAN_BYPASS_L_RINT) ? "on" : "off",
2136                 pp->sp_hotchar));
2137 }
2138
2139
2140 #ifdef  SI_DEBUG
2141
2142 void
2143 si_dprintf(struct si_port *pp, int flags, const char *fmt, ...)
2144 {
2145         va_list ap;
2146
2147         if ((pp == NULL && (si_debug&flags)) ||
2148             (pp != NULL && ((pp->sp_debug&flags) || (si_debug&flags)))) {
2149                 if (pp != NULL)
2150                         printf("%ci%d(%d): ", 's',
2151                                 (int)SI_CARD(minor(pp->sp_tty->t_dev)),
2152                                 (int)SI_PORT(minor(pp->sp_tty->t_dev)));
2153                 va_start(ap, fmt);
2154                 vprintf(fmt, ap);
2155                 va_end(ap);
2156         }
2157 }
2158
2159 static char *
2160 si_mctl2str(enum si_mctl cmd)
2161 {
2162         switch (cmd) {
2163         case GET:
2164                 return("GET");
2165         case SET:
2166                 return("SET");
2167         case BIS:
2168                 return("BIS");
2169         case BIC:
2170                 return("BIC");
2171         }
2172         return("BAD");
2173 }
2174
2175 #endif  /* DEBUG */
2176
2177 static char *
2178 si_modulename(int host_type, int uart_type)
2179 {
2180         switch (host_type) {
2181         /* Z280 based cards */
2182         case SIEISA:
2183         case SIHOST2:
2184         case SIHOST:
2185         case SIPCI:
2186                 switch (uart_type) {
2187                 case 0:
2188                         return(" (XIO)");
2189                 case 1:
2190                         return(" (SI)");
2191                 }
2192                 break;
2193         /* T225 based hosts */
2194         case SIJETPCI:
2195         case SIJETISA:
2196                 switch (uart_type) {
2197                 case 0:
2198                         return(" (SI)");
2199                 case 40:
2200                         return(" (XIO)");
2201                 case 72:
2202                         return(" (SXDC)");
2203                 }
2204                 break;
2205         }
2206         return("");
2207 }