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