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