Merge branch 'vendor/FILE'
[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 | D_KQFILTER },
124         .d_open =       siopen,
125         .d_close =      siclose,
126         .d_read =       ttyread,
127         .d_write =      siwrite,
128         .d_ioctl =      siioctl,
129         .d_poll =       ttypoll,
130         .d_kqfilter =   ttykqfilter
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         dev_ops_add(&si_ops, 0x7f, unit);
603 /*      path    name    devsw           minor   type   uid gid perm*/
604         for (x = 0; x < sc->sc_nport; x++) {
605                 /* sync with the manuals that start at 1 */
606                 y = x + 1 + unit * (1 << SI_CARDSHIFT);
607                 make_dev(&si_ops, x, 0, 0, 0600, "ttyA%02d", y);
608                 make_dev(&si_ops, x + 0x00080, 0, 0, 0600, "cuaA%02d", y);
609                 make_dev(&si_ops, x + 0x10000, 0, 0, 0600, "ttyiA%02d", y);
610                 make_dev(&si_ops, x + 0x10080, 0, 0, 0600, "cuaiA%02d", y);
611                 make_dev(&si_ops, x + 0x20000, 0, 0, 0600, "ttylA%02d", y);
612                 make_dev(&si_ops, x + 0x20080, 0, 0, 0600, "cualA%02d", y);
613         }
614         make_dev(&si_ops, 0x40000, 0, 0, 0600, "si_control");
615         return (0);
616 }
617
618 static  int
619 siopen(struct dev_open_args *ap)
620 {
621         cdev_t dev = ap->a_head.a_dev;
622         int error;
623         int card, port;
624         struct si_softc *sc;
625         struct tty *tp;
626         volatile struct si_channel *ccbp;
627         struct si_port *pp;
628         int mynor = minor(dev);
629
630         /* quickly let in /dev/si_control */
631         if (IS_CONTROLDEV(mynor)) {
632                 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)))
633                         return(error);
634                 return(0);
635         }
636
637         card = SI_CARD(mynor);
638         sc = devclass_get_softc(si_devclass, card);
639         if (sc == NULL)
640                 return (ENXIO);
641
642         if (sc->sc_type == SIEMPTY) {
643                 DPRINT((0, DBG_OPEN|DBG_FAIL, "si%d: type %s??\n",
644                         card, sc->sc_typename));
645                 return(ENXIO);
646         }
647
648         port = SI_PORT(mynor);
649         if (port >= sc->sc_nport) {
650                 DPRINT((0, DBG_OPEN|DBG_FAIL, "si%d: nports %d\n",
651                         card, sc->sc_nport));
652                 return(ENXIO);
653         }
654
655 #ifdef  POLL
656         /*
657          * We've now got a device, so start the poller.
658          */
659         if (init_finished == 0) {
660                 callout_reset(&poll_ch, si_pollrate, si_poll, NULL);
661                 init_finished = 1;
662         }
663 #endif
664
665         /* initial/lock device */
666         if (IS_STATE(mynor)) {
667                 return(0);
668         }
669
670         pp = sc->sc_ports + port;
671         tp = pp->sp_tty;                        /* the "real" tty */
672         dev->si_tty = tp;
673         ccbp = pp->sp_ccb;                      /* Find control block */
674         DPRINT((pp, DBG_ENTRY|DBG_OPEN, "siopen(%s,%x,%x)\n",
675                 devtoname(dev), ap->a_oflags, ap->a_devtype));
676
677         crit_enter();                   /* Keep others out */
678         error = 0;
679
680 open_top:
681         while (pp->sp_state & SS_DTR_OFF) {
682                 error = tsleep(&pp->sp_dtr_wait, PCATCH, "sidtr", 0);
683                 if (error != 0)
684                         goto out;
685         }
686
687         if (tp->t_state & TS_ISOPEN) {
688                 /*
689                  * The device is open, so everything has been initialised.
690                  * handle conflicts.
691                  */
692                 if (IS_CALLOUT(mynor)) {
693                         if (!pp->sp_active_out) {
694                                 error = EBUSY;
695                                 goto out;
696                         }
697                 } else {
698                         if (pp->sp_active_out) {
699                                 if (ap->a_oflags & O_NONBLOCK) {
700                                         error = EBUSY;
701                                         goto out;
702                                 }
703                                 error = tsleep(&pp->sp_active_out,
704                                                 PCATCH, "sibi", 0);
705                                 if (error != 0)
706                                         goto out;
707                                 goto open_top;
708                         }
709                 }
710                 if (tp->t_state & TS_XCLUDE &&
711                     priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
712                         DPRINT((pp, DBG_OPEN|DBG_FAIL,
713                                 "already open and EXCLUSIVE set\n"));
714                         error = EBUSY;
715                         goto out;
716                 }
717         } else {
718                 /*
719                  * The device isn't open, so there are no conflicts.
720                  * Initialize it. Avoid sleep... :-)
721                  */
722                 DPRINT((pp, DBG_OPEN, "first open\n"));
723                 tp->t_oproc = si_start;
724                 tp->t_stop = si_stop;
725                 tp->t_param = siparam;
726                 tp->t_dev = dev;
727                 tp->t_termios = mynor & SI_CALLOUT_MASK
728                                 ? pp->sp_iout : pp->sp_iin;
729
730                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
731
732                 ++pp->sp_wopeners;      /* in case of sleep in siparam */
733
734                 error = siparam(tp, &tp->t_termios);
735
736                 --pp->sp_wopeners;
737                 if (error != 0)
738                         goto out;
739                 /* XXX: we should goto_top if siparam slept */
740
741                 /* set initial DCD state */
742                 pp->sp_last_hi_ip = ccbp->hi_ip;
743                 if ((pp->sp_last_hi_ip & IP_DCD) || IS_CALLOUT(mynor)) {
744                         (*linesw[tp->t_line].l_modem)(tp, 1);
745                 }
746         }
747
748         /* whoops! we beat the close! */
749         if (pp->sp_state & SS_CLOSING) {
750                 /* try and stop it from proceeding to bash the hardware */
751                 pp->sp_state &= ~SS_CLOSING;
752         }
753
754         /*
755          * Wait for DCD if necessary
756          */
757         if (!(tp->t_state & TS_CARR_ON) &&
758             !IS_CALLOUT(mynor) &&
759             !(tp->t_cflag & CLOCAL) &&
760             !(ap->a_oflags & O_NONBLOCK)) {
761                 ++pp->sp_wopeners;
762                 DPRINT((pp, DBG_OPEN, "sleeping for carrier\n"));
763                 error = tsleep(TSA_CARR_ON(tp), PCATCH, "sidcd", 0);
764                 --pp->sp_wopeners;
765                 if (error != 0)
766                         goto out;
767                 goto open_top;
768         }
769
770         error = (*linesw[tp->t_line].l_open)(dev, tp);
771         si_disc_optim(tp, &tp->t_termios, pp);
772         if (tp->t_state & TS_ISOPEN && IS_CALLOUT(mynor))
773                 pp->sp_active_out = TRUE;
774
775         pp->sp_state |= SS_OPEN;        /* made it! */
776
777 out:
778         crit_exit();
779
780         DPRINT((pp, DBG_OPEN, "leaving siopen\n"));
781
782         if (!(tp->t_state & TS_ISOPEN) && pp->sp_wopeners == 0)
783                 sihardclose(pp);
784
785         return(error);
786 }
787
788 static  int
789 siclose(struct dev_close_args *ap)
790 {
791         cdev_t dev = ap->a_head.a_dev;
792         struct si_port *pp;
793         struct tty *tp;
794         int error = 0;
795         int mynor = minor(dev);
796
797         if (IS_SPECIAL(mynor))
798                 return(0);
799
800         crit_enter();
801
802         pp = MINOR2PP(mynor);
803         tp = pp->sp_tty;
804
805         DPRINT((pp, DBG_ENTRY|DBG_CLOSE, "siclose(%s,%x,%x) sp_state:%x\n",
806                 devtoname(dev), ap->a_fflag, ap->a_devtype, pp->sp_state));
807
808         /* did we sleep and loose a race? */
809         if (pp->sp_state & SS_CLOSING) {
810                 /* error = ESOMETING? */
811                 goto out;
812         }
813
814         /* begin race detection.. */
815         pp->sp_state |= SS_CLOSING;
816
817         si_write_enable(pp, 0);         /* block writes for ttywait() */
818
819         /* THIS MAY SLEEP IN TTYWAIT!!! */
820         (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
821
822         si_write_enable(pp, 1);
823
824         /* did we sleep and somebody started another open? */
825         if (!(pp->sp_state & SS_CLOSING)) {
826                 /* error = ESOMETING? */
827                 goto out;
828         }
829         /* ok. we are now still on the right track.. nuke the hardware */
830
831         if (pp->sp_state & SS_LSTART) {
832                 callout_stop(&pp->lstart_ch);
833                 pp->sp_state &= ~SS_LSTART;
834         }
835
836         si_stop(tp, FREAD | FWRITE);
837
838         sihardclose(pp);
839         ttyclose(tp);
840         pp->sp_state &= ~SS_OPEN;
841
842 out:
843         DPRINT((pp, DBG_CLOSE|DBG_EXIT, "close done, returning\n"));
844         crit_exit();
845         return(error);
846 }
847
848 static void
849 sihardclose(struct si_port *pp)
850 {
851         struct tty *tp;
852         volatile struct si_channel *ccbp;
853
854         crit_enter();
855
856         tp = pp->sp_tty;
857         ccbp = pp->sp_ccb;                      /* Find control block */
858         if (tp->t_cflag & HUPCL ||
859             (!pp->sp_active_out &&
860              !(ccbp->hi_ip & IP_DCD) &&
861              !(pp->sp_iin.c_cflag && CLOCAL)) ||
862             !(tp->t_state & TS_ISOPEN)) {
863
864                 (void) si_modem(pp, BIC, TIOCM_DTR|TIOCM_RTS);
865                 (void) si_command(pp, FCLOSE, SI_NOWAIT);
866
867                 if (pp->sp_dtr_wait != 0) {
868                         callout_reset(&pp->dtr_ch, pp->sp_dtr_wait,
869                                         sidtrwakeup, pp);
870                         pp->sp_state |= SS_DTR_OFF;
871                 }
872
873         }
874         pp->sp_active_out = FALSE;
875         wakeup((caddr_t)&pp->sp_active_out);
876         wakeup(TSA_CARR_ON(tp));
877
878         crit_exit();
879 }
880
881
882 /*
883  * called at splsoftclock()...
884  */
885 static void
886 sidtrwakeup(void *chan)
887 {
888         struct si_port *pp;
889
890         crit_enter();
891
892         pp = (struct si_port *)chan;
893         pp->sp_state &= ~SS_DTR_OFF;
894         wakeup(&pp->sp_dtr_wait);
895
896         crit_exit();
897 }
898
899 static  int
900 siwrite(struct dev_write_args *ap)
901 {
902         cdev_t dev = ap->a_head.a_dev;
903         struct si_port *pp;
904         struct tty *tp;
905         int error = 0;
906         int mynor = minor(dev);
907
908         if (IS_SPECIAL(mynor)) {
909                 DPRINT((0, DBG_ENTRY|DBG_FAIL|DBG_WRITE, "siwrite(CONTROLDEV!!)\n"));
910                 return(ENODEV);
911         }
912         pp = MINOR2PP(mynor);
913         tp = pp->sp_tty;
914         DPRINT((pp, DBG_WRITE, "siwrite(%s,%x,%x)\n", devtoname(dev), ap->a_uio, ap->a_ioflag));
915
916         crit_enter();
917         /*
918          * If writes are currently blocked, wait on the "real" tty
919          */
920         while (pp->sp_state & SS_BLOCKWRITE) {
921                 pp->sp_state |= SS_WAITWRITE;
922                 DPRINT((pp, DBG_WRITE, "in siwrite, wait for SS_BLOCKWRITE to clear\n"));
923                 if ((error = ttysleep(tp, (caddr_t)pp, PCATCH,
924                                      "siwrite", tp->t_timeout))) {
925                         if (error == EWOULDBLOCK)
926                                 error = EIO;
927                         goto out;
928                 }
929         }
930
931         error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
932 out:
933         crit_exit();
934         return (error);
935 }
936
937
938 static  int
939 siioctl(struct dev_ioctl_args *ap)
940 {
941         cdev_t dev = ap->a_head.a_dev;
942         caddr_t data = ap->a_data;
943         u_long cmd = ap->a_cmd;
944         struct si_port *pp;
945         struct tty *tp;
946         int error;
947         int mynor = minor(dev);
948         int blocked = 0;
949 #if defined(COMPAT_43)
950         u_long oldcmd;
951         struct termios term;
952 #endif
953
954         if (IS_SI_IOCTL(cmd))
955                 return(si_Sioctl(dev, cmd, data, ap->a_fflag, ap->a_cred));
956
957         pp = MINOR2PP(mynor);
958         tp = pp->sp_tty;
959
960         DPRINT((pp, DBG_ENTRY|DBG_IOCTL, "siioctl(%s,%lx,%x,%x)\n",
961                 devtoname(dev), cmd, data, ap->a_fflag));
962         if (IS_STATE(mynor)) {
963                 struct termios *ct;
964
965                 switch (mynor & SI_STATE_MASK) {
966                 case SI_INIT_STATE_MASK:
967                         ct = IS_CALLOUT(mynor) ? &pp->sp_iout : &pp->sp_iin;
968                         break;
969                 case SI_LOCK_STATE_MASK:
970                         ct = IS_CALLOUT(mynor) ? &pp->sp_lout : &pp->sp_lin;
971                         break;
972                 default:
973                         return (ENODEV);
974                 }
975                 switch (cmd) {
976                 case TIOCSETA:
977                         error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
978                         if (error != 0)
979                                 return (error);
980                         *ct = *(struct termios *)data;
981                         return (0);
982                 case TIOCGETA:
983                         *(struct termios *)data = *ct;
984                         return (0);
985                 case TIOCGETD:
986                         *(int *)data = TTYDISC;
987                         return (0);
988                 case TIOCGWINSZ:
989                         bzero(data, sizeof(struct winsize));
990                         return (0);
991                 default:
992                         return (ENOTTY);
993                 }
994         }
995         /*
996          * Do the old-style ioctl compat routines...
997          */
998 #if defined(COMPAT_43)
999         term = tp->t_termios;
1000         oldcmd = cmd;
1001         error = ttsetcompat(tp, &cmd, data, &term);
1002         if (error != 0)
1003                 return (error);
1004         if (cmd != oldcmd)
1005                 data = (caddr_t)&term;
1006 #endif
1007         /*
1008          * Do the initial / lock state business
1009          */
1010         if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1011                 int     cc;
1012                 struct termios *dt = (struct termios *)data;
1013                 struct termios *lt = mynor & SI_CALLOUT_MASK
1014                                      ? &pp->sp_lout : &pp->sp_lin;
1015
1016                 dt->c_iflag = (tp->t_iflag & lt->c_iflag) |
1017                         (dt->c_iflag & ~lt->c_iflag);
1018                 dt->c_oflag = (tp->t_oflag & lt->c_oflag) |
1019                         (dt->c_oflag & ~lt->c_oflag);
1020                 dt->c_cflag = (tp->t_cflag & lt->c_cflag) |
1021                         (dt->c_cflag & ~lt->c_cflag);
1022                 dt->c_lflag = (tp->t_lflag & lt->c_lflag) |
1023                         (dt->c_lflag & ~lt->c_lflag);
1024                 for (cc = 0; cc < NCCS; ++cc)
1025                         if (lt->c_cc[cc] != 0)
1026                                 dt->c_cc[cc] = tp->t_cc[cc];
1027                 if (lt->c_ispeed != 0)
1028                         dt->c_ispeed = tp->t_ispeed;
1029                 if (lt->c_ospeed != 0)
1030                         dt->c_ospeed = tp->t_ospeed;
1031         }
1032
1033         /*
1034          * Block user-level writes to give the ttywait()
1035          * a chance to completely drain for commands
1036          * that require the port to be in a quiescent state.
1037          */
1038         switch (cmd) {
1039         case TIOCSETAW:
1040         case TIOCSETAF:
1041         case TIOCDRAIN:
1042 #ifdef COMPAT_43
1043         case TIOCSETP:
1044 #endif
1045                 blocked++;      /* block writes for ttywait() and siparam() */
1046                 si_write_enable(pp, 0);
1047         }
1048
1049         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data,
1050                                               ap->a_fflag, ap->a_cred);
1051         if (error != ENOIOCTL)
1052                 goto out;
1053
1054         crit_enter();
1055
1056         error = ttioctl(tp, cmd, data, ap->a_fflag);
1057         si_disc_optim(tp, &tp->t_termios, pp);
1058         if (error != ENOIOCTL) {
1059                 crit_exit();
1060                 goto out;
1061         }
1062
1063         error = 0;
1064         switch (cmd) {
1065         case TIOCSBRK:
1066                 si_command(pp, SBREAK, SI_WAIT);
1067                 break;
1068         case TIOCCBRK:
1069                 si_command(pp, EBREAK, SI_WAIT);
1070                 break;
1071         case TIOCSDTR:
1072                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1073                 break;
1074         case TIOCCDTR:
1075                 (void) si_modem(pp, SET, 0);
1076                 break;
1077         case TIOCMSET:
1078                 (void) si_modem(pp, SET, *(int *)data);
1079                 break;
1080         case TIOCMBIS:
1081                 (void) si_modem(pp, BIS, *(int *)data);
1082                 break;
1083         case TIOCMBIC:
1084                 (void) si_modem(pp, BIC, *(int *)data);
1085                 break;
1086         case TIOCMGET:
1087                 *(int *)data = si_modem(pp, GET, 0);
1088                 break;
1089         case TIOCMSDTRWAIT:
1090                 /* must be root since the wait applies to following logins */
1091                 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1092                 if (error == 0)
1093                         pp->sp_dtr_wait = *(int *)data * hz / 100;
1094                 break;
1095         case TIOCMGDTRWAIT:
1096                 *(int *)data = pp->sp_dtr_wait * 100 / hz;
1097                 break;
1098         default:
1099                 error = ENOTTY;
1100         }
1101         crit_exit();
1102
1103 out:
1104         DPRINT((pp, DBG_IOCTL|DBG_EXIT, "siioctl ret %d\n", error));
1105         if (blocked)
1106                 si_write_enable(pp, 1);
1107         return(error);
1108 }
1109
1110 /*
1111  * Handle the Specialix ioctls. All MUST be called via the CONTROL device
1112  */
1113 static int
1114 si_Sioctl(cdev_t dev, u_long cmd, caddr_t data, int flag, struct ucred *cred)
1115 {
1116         struct si_softc *xsc;
1117         struct si_port *xpp;
1118         volatile struct si_reg *regp;
1119         struct si_tcsi *dp;
1120         struct si_pstat *sps;
1121         int *ip, error = 0;
1122         int card, port;
1123         int mynor = minor(dev);
1124
1125         DPRINT((0, DBG_ENTRY|DBG_IOCTL, "si_Sioctl(%s,%lx,%x,%x)\n",
1126                 devtoname(dev), cmd, data, flag));
1127
1128 #if 1
1129         DPRINT((0, DBG_IOCTL, "TCSI_PORT=%x\n", TCSI_PORT));
1130         DPRINT((0, DBG_IOCTL, "TCSI_CCB=%x\n", TCSI_CCB));
1131         DPRINT((0, DBG_IOCTL, "TCSI_TTY=%x\n", TCSI_TTY));
1132 #endif
1133
1134         if (!IS_CONTROLDEV(mynor)) {
1135                 DPRINT((0, DBG_IOCTL|DBG_FAIL, "not called from control device!\n"));
1136                 return(ENODEV);
1137         }
1138
1139         crit_enter();   /* better safe than sorry */
1140
1141         ip = (int *)data;
1142
1143 #define SUCHECK if ((error = priv_check_cred(cred, PRIV_ROOT, 0))) goto out
1144
1145         switch (cmd) {
1146         case TCSIPORTS:
1147                 *ip = si_Nports;
1148                 goto out;
1149         case TCSIMODULES:
1150                 *ip = si_Nmodules;
1151                 goto out;
1152         case TCSISDBG_ALL:
1153                 SUCHECK;
1154                 si_debug = *ip;
1155                 goto out;
1156         case TCSIGDBG_ALL:
1157                 *ip = si_debug;
1158                 goto out;
1159         default:
1160                 /*
1161                  * Check that a controller for this port exists
1162                  */
1163
1164                 /* may also be a struct si_pstat, a superset of si_tcsi */
1165
1166                 dp = (struct si_tcsi *)data;
1167                 sps = (struct si_pstat *)data;
1168                 card = dp->tc_card;
1169                 xsc = devclass_get_softc(si_devclass, card);    /* check.. */
1170                 if (xsc == NULL || xsc->sc_type == SIEMPTY) {
1171                         error = ENOENT;
1172                         goto out;
1173                 }
1174                 /*
1175                  * And check that a port exists
1176                  */
1177                 port = dp->tc_port;
1178                 if (port < 0 || port >= xsc->sc_nport) {
1179                         error = ENOENT;
1180                         goto out;
1181                 }
1182                 xpp = xsc->sc_ports + port;
1183                 regp = (struct si_reg *)xsc->sc_maddr;
1184         }
1185
1186         switch (cmd) {
1187         case TCSIDEBUG:
1188 #ifdef  SI_DEBUG
1189                 SUCHECK;
1190                 if (xpp->sp_debug)
1191                         xpp->sp_debug = 0;
1192                 else {
1193                         xpp->sp_debug = DBG_ALL;
1194                         DPRINT((xpp, DBG_IOCTL, "debug toggled %s\n",
1195                                 (xpp->sp_debug&DBG_ALL)?"ON":"OFF"));
1196                 }
1197                 break;
1198 #else
1199                 error = ENODEV;
1200                 goto out;
1201 #endif
1202         case TCSISDBG_LEVEL:
1203         case TCSIGDBG_LEVEL:
1204 #ifdef  SI_DEBUG
1205                 if (cmd == TCSIGDBG_LEVEL) {
1206                         dp->tc_dbglvl = xpp->sp_debug;
1207                 } else {
1208                         SUCHECK;
1209                         xpp->sp_debug = dp->tc_dbglvl;
1210                 }
1211                 break;
1212 #else
1213                 error = ENODEV;
1214                 goto out;
1215 #endif
1216         case TCSIGRXIT:
1217                 dp->tc_int = regp->rx_int_count;
1218                 break;
1219         case TCSIRXIT:
1220                 SUCHECK;
1221                 regp->rx_int_count = dp->tc_int;
1222                 break;
1223         case TCSIGIT:
1224                 dp->tc_int = regp->int_count;
1225                 break;
1226         case TCSIIT:
1227                 SUCHECK;
1228                 regp->int_count = dp->tc_int;
1229                 break;
1230         case TCSISTATE:
1231                 dp->tc_int = xpp->sp_ccb->hi_ip;
1232                 break;
1233         /* these next three use a different structure */
1234         case TCSI_PORT:
1235                 SUCHECK;
1236                 si_bcopy(xpp, &sps->tc_siport, sizeof(sps->tc_siport));
1237                 break;
1238         case TCSI_CCB:
1239                 SUCHECK;
1240                 si_vbcopy(xpp->sp_ccb, &sps->tc_ccb, sizeof(sps->tc_ccb));
1241                 break;
1242         case TCSI_TTY:
1243                 SUCHECK;
1244                 si_bcopy(xpp->sp_tty, &sps->tc_tty, sizeof(sps->tc_tty));
1245                 break;
1246         default:
1247                 error = EINVAL;
1248                 goto out;
1249         }
1250 out:
1251         crit_exit();
1252         return(error);          /* success */
1253 }
1254
1255 /*
1256  *      siparam()       : Configure line params
1257  *      called at spltty();
1258  *      this may sleep, does not flush, nor wait for drain, nor block writes
1259  *      caller must arrange this if it's important..
1260  */
1261 static int
1262 siparam(struct tty *tp, struct termios *t)
1263 {
1264         struct si_port *pp = TP2PP(tp);
1265         volatile struct si_channel *ccbp;
1266         int cflag, iflag, oflag, lflag;
1267         int error = 0;          /* shutup gcc */
1268         int ispeed = 0;         /* shutup gcc */
1269         int ospeed = 0;         /* shutup gcc */
1270         BYTE val;
1271
1272         DPRINT((pp, DBG_ENTRY|DBG_PARAM, "siparam(%x,%x)\n", tp, t));
1273         cflag = t->c_cflag;
1274         iflag = t->c_iflag;
1275         oflag = t->c_oflag;
1276         lflag = t->c_lflag;
1277         DPRINT((pp, DBG_PARAM, "OFLAG 0x%x CFLAG 0x%x IFLAG 0x%x LFLAG 0x%x\n",
1278                 oflag, cflag, iflag, lflag));
1279
1280         /* XXX - if Jet host and SXDC module, use extended baud rates */
1281
1282         /* if not hung up.. */
1283         if (t->c_ospeed != 0) {
1284                 /* translate baud rate to firmware values */
1285                 ospeed = ttspeedtab(t->c_ospeed, bdrates);
1286                 ispeed = t->c_ispeed ?
1287                          ttspeedtab(t->c_ispeed, bdrates) : ospeed;
1288
1289                 /* enforce legit baud rate */
1290                 if (ospeed < 0 || ispeed < 0)
1291                         return (EINVAL);
1292         }
1293
1294         crit_enter();
1295
1296         ccbp = pp->sp_ccb;
1297
1298         /* ========== set hi_break ========== */
1299         val = 0;
1300         if (iflag & IGNBRK)             /* Breaks */
1301                 val |= BR_IGN;
1302         if (iflag & BRKINT)             /* Interrupt on break? */
1303                 val |= BR_INT;
1304         if (iflag & PARMRK)             /* Parity mark? */
1305                 val |= BR_PARMRK;
1306         if (iflag & IGNPAR)             /* Ignore chars with parity errors? */
1307                 val |= BR_PARIGN;
1308         ccbp->hi_break = val;
1309
1310         /* ========== set hi_csr ========== */
1311         /* if not hung up.. */
1312         if (t->c_ospeed != 0) {
1313                 /* Set I/O speeds */
1314                  val = (ispeed << 4) | ospeed;
1315         }
1316         ccbp->hi_csr = val;
1317
1318         /* ========== set hi_mr2 ========== */
1319         val = 0;
1320         if (cflag & CSTOPB)                             /* Stop bits */
1321                 val |= MR2_2_STOP;
1322         else
1323                 val |= MR2_1_STOP;
1324         /*
1325          * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1326          * a DCE, hence the reverse sense of RTS and CTS
1327          */
1328         /* Output Flow - RTS must be raised before data can be sent */
1329         if (cflag & CCTS_OFLOW)
1330                 val |= MR2_RTSCONT;
1331
1332         ccbp->hi_mr2 = val;
1333
1334         /* ========== set hi_mr1 ========== */
1335         val = 0;
1336         if (!(cflag & PARENB))                          /* Parity */
1337                 val |= MR1_NONE;
1338         else
1339                 val |= MR1_WITH;
1340         if (cflag & PARODD)
1341                 val |= MR1_ODD;
1342
1343         if ((cflag & CS8) == CS8) {                     /* 8 data bits? */
1344                 val |= MR1_8_BITS;
1345         } else if ((cflag & CS7) == CS7) {              /* 7 data bits? */
1346                 val |= MR1_7_BITS;
1347         } else if ((cflag & CS6) == CS6) {              /* 6 data bits? */
1348                 val |= MR1_6_BITS;
1349         } else {                                        /* Must be 5 */
1350                 val |= MR1_5_BITS;
1351         }
1352         /*
1353          * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1354          * a DCE, hence the reverse sense of RTS and CTS
1355          */
1356         /* Input Flow - CTS is raised when port is ready to receive data */
1357         if (cflag & CRTS_IFLOW)
1358                 val |= MR1_CTSCONT;
1359
1360         ccbp->hi_mr1 = val;
1361
1362         /* ========== set hi_mask ========== */
1363         val = 0xff;
1364         if ((cflag & CS8) == CS8) {                     /* 8 data bits? */
1365                 val &= 0xFF;
1366         } else if ((cflag & CS7) == CS7) {              /* 7 data bits? */
1367                 val &= 0x7F;
1368         } else if ((cflag & CS6) == CS6) {              /* 6 data bits? */
1369                 val &= 0x3F;
1370         } else {                                        /* Must be 5 */
1371                 val &= 0x1F;
1372         }
1373         if (iflag & ISTRIP)
1374                 val &= 0x7F;
1375
1376         ccbp->hi_mask = val;
1377
1378         /* ========== set hi_prtcl ========== */
1379         val = SP_DCEN;          /* Monitor DCD always, or TIOCMGET misses it */
1380         if (iflag & IXANY)
1381                 val |= SP_TANY;
1382         if (iflag & IXON)
1383                 val |= SP_TXEN;
1384         if (iflag & IXOFF)
1385                 val |= SP_RXEN;
1386         if (iflag & INPCK)
1387                 val |= SP_PAEN;
1388
1389         ccbp->hi_prtcl = val;
1390
1391
1392         /* ========== set hi_{rx|tx}{on|off} ========== */
1393         /* XXX: the card TOTALLY shields us from the flow control... */
1394         ccbp->hi_txon = t->c_cc[VSTART];
1395         ccbp->hi_txoff = t->c_cc[VSTOP];
1396
1397         ccbp->hi_rxon = t->c_cc[VSTART];
1398         ccbp->hi_rxoff = t->c_cc[VSTOP];
1399
1400         /* ========== send settings to the card ========== */
1401         /* potential sleep here */
1402         if (ccbp->hi_stat == IDLE_CLOSE)                /* Not yet open */
1403                 si_command(pp, LOPEN, SI_WAIT);         /* open it */
1404         else
1405                 si_command(pp, CONFIG, SI_WAIT);        /* change params */
1406
1407         /* ========== set DTR etc ========== */
1408         /* Hangup if ospeed == 0 */
1409         if (t->c_ospeed == 0) {
1410                 (void) si_modem(pp, BIC, TIOCM_DTR|TIOCM_RTS);
1411         } else {
1412                 /*
1413                  * If the previous speed was 0, may need to re-enable
1414                  * the modem signals
1415                  */
1416                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1417         }
1418
1419         DPRINT((pp, DBG_PARAM, "siparam, complete: MR1 %x MR2 %x HI_MASK %x PRTCL %x HI_BREAK %x\n",
1420                 ccbp->hi_mr1, ccbp->hi_mr2, ccbp->hi_mask, ccbp->hi_prtcl, ccbp->hi_break));
1421
1422         crit_exit();
1423         return(error);
1424 }
1425
1426 /*
1427  * Enable or Disable the writes to this channel...
1428  * "state" ->  enabled = 1; disabled = 0;
1429  */
1430 static void
1431 si_write_enable(struct si_port *pp, int state)
1432 {
1433
1434         crit_enter();
1435
1436         if (state) {
1437                 pp->sp_state &= ~SS_BLOCKWRITE;
1438                 if (pp->sp_state & SS_WAITWRITE) {
1439                         pp->sp_state &= ~SS_WAITWRITE;
1440                         /* thunder away! */
1441                         wakeup((caddr_t)pp);
1442                 }
1443         } else {
1444                 pp->sp_state |= SS_BLOCKWRITE;
1445         }
1446
1447         crit_exit();
1448 }
1449
1450 /*
1451  * Set/Get state of modem control lines.
1452  * Due to DCE-like behaviour of the adapter, some signals need translation:
1453  *      TIOCM_DTR       DSR
1454  *      TIOCM_RTS       CTS
1455  */
1456 static int
1457 si_modem(struct si_port *pp, enum si_mctl cmd, int bits)
1458 {
1459         volatile struct si_channel *ccbp;
1460         int x;
1461
1462         DPRINT((pp, DBG_ENTRY|DBG_MODEM, "si_modem(%x,%s,%x)\n", pp, si_mctl2str(cmd), bits));
1463         ccbp = pp->sp_ccb;              /* Find channel address */
1464         switch (cmd) {
1465         case GET:
1466                 x = ccbp->hi_ip;
1467                 bits = TIOCM_LE;
1468                 if (x & IP_DCD)         bits |= TIOCM_CAR;
1469                 if (x & IP_DTR)         bits |= TIOCM_DTR;
1470                 if (x & IP_RTS)         bits |= TIOCM_RTS;
1471                 if (x & IP_RI)          bits |= TIOCM_RI;
1472                 return(bits);
1473         case SET:
1474                 ccbp->hi_op &= ~(OP_DSR|OP_CTS);
1475                 /* fall through */
1476         case BIS:
1477                 x = 0;
1478                 if (bits & TIOCM_DTR)
1479                         x |= OP_DSR;
1480                 if (bits & TIOCM_RTS)
1481                         x |= OP_CTS;
1482                 ccbp->hi_op |= x;
1483                 break;
1484         case BIC:
1485                 if (bits & TIOCM_DTR)
1486                         ccbp->hi_op &= ~OP_DSR;
1487                 if (bits & TIOCM_RTS)
1488                         ccbp->hi_op &= ~OP_CTS;
1489         }
1490         return 0;
1491 }
1492
1493 /*
1494  * Handle change of modem state
1495  */
1496 static void
1497 si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip)
1498 {
1499                                                         /* if a modem dev */
1500         if (hi_ip & IP_DCD) {
1501                 if (!(pp->sp_last_hi_ip & IP_DCD)) {
1502                         DPRINT((pp, DBG_INTR, "modem carr on t_line %d\n",
1503                                 tp->t_line));
1504                         (void)(*linesw[tp->t_line].l_modem)(tp, 1);
1505                 }
1506         } else {
1507                 if (pp->sp_last_hi_ip & IP_DCD) {
1508                         DPRINT((pp, DBG_INTR, "modem carr off\n"));
1509                         if ((*linesw[tp->t_line].l_modem)(tp, 0))
1510                                 (void) si_modem(pp, SET, 0);
1511                 }
1512         }
1513         pp->sp_last_hi_ip = hi_ip;
1514
1515 }
1516
1517 /*
1518  * Poller to catch missed interrupts.
1519  *
1520  * Note that the SYSV Specialix drivers poll at 100 times per second to get
1521  * better response.  We could really use a "periodic" version timeout(). :-)
1522  */
1523 #ifdef POLL
1524 static void
1525 si_poll(void *nothing)
1526 {
1527         struct si_softc *sc;
1528         int i;
1529         volatile struct si_reg *regp;
1530         struct si_port *pp;
1531         int lost, port;
1532
1533         DPRINT((0, DBG_POLL, "si_poll()\n"));
1534         crit_enter();
1535         if (in_intr)
1536                 goto out;
1537         lost = 0;
1538         for (i = 0; i < si_numunits; i++) {
1539                 sc = devclass_get_softc(si_devclass, i);
1540                 if (sc == NULL || sc->sc_type == SIEMPTY)
1541                         continue;
1542                 regp = (struct si_reg *)sc->sc_maddr;
1543
1544                 /*
1545                  * See if there has been a pending interrupt for 2 seconds
1546                  * or so. The test (int_scounter >= 200) won't correspond
1547                  * to 2 seconds if int_count gets changed.
1548                  */
1549                 if (regp->int_pending != 0) {
1550                         if (regp->int_scounter >= 200 &&
1551                             regp->initstat == 1) {
1552                                 kprintf("si%d: lost intr\n", i);
1553                                 lost++;
1554                         }
1555                 } else {
1556                         regp->int_scounter = 0;
1557                 }
1558
1559                 /*
1560                  * gripe about no input flow control..
1561                  */
1562                 pp = sc->sc_ports;
1563                 for (port = 0; port < sc->sc_nport; pp++, port++) {
1564                         if (pp->sp_delta_overflows > 0) {
1565                                 kprintf("si%d: %d tty level buffer overflows\n",
1566                                         i, pp->sp_delta_overflows);
1567                                 pp->sp_delta_overflows = 0;
1568                         }
1569                 }
1570         }
1571         if (lost || si_realpoll)
1572                 si_intr(NULL);  /* call intr with fake vector */
1573 out:
1574         crit_exit();
1575
1576         callout_reset(&poll_ch, si_pollrate, si_poll, NULL);
1577 }
1578 #endif  /* ifdef POLL */
1579
1580 /*
1581  * The interrupt handler polls ALL ports on ALL adapters each time
1582  * it is called.
1583  */
1584
1585 static BYTE si_rxbuf[SI_BUFFERSIZE];    /* input staging area */
1586 static BYTE si_txbuf[SI_BUFFERSIZE];    /* output staging area */
1587
1588 void
1589 si_intr(void *arg)
1590 {
1591         struct si_softc *sc;
1592         struct si_port *pp;
1593         volatile struct si_channel *ccbp;
1594         struct tty *tp;
1595         volatile caddr_t maddr;
1596         BYTE op, ip;
1597         int x, card, port, n, i, isopen;
1598         volatile BYTE *z;
1599         BYTE c;
1600
1601         sc = arg;
1602
1603         DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "si_intr\n"));
1604         if (in_intr)
1605                 return;
1606         in_intr = 1;
1607
1608         /*
1609          * When we get an int we poll all the channels and do ALL pending
1610          * work, not just the first one we find. This allows all cards to
1611          * share the same vector.
1612          *
1613          * XXX - But if we're sharing the vector with something that's NOT
1614          * a SI/XIO/SX card, we may be making more work for ourselves.
1615          */
1616         for (card = 0; card < si_numunits; card++) {
1617                 sc = devclass_get_softc(si_devclass, card);
1618                 if (sc == NULL || sc->sc_type == SIEMPTY)
1619                         continue;
1620
1621                 /*
1622                  * First, clear the interrupt
1623                  */
1624                 switch(sc->sc_type) {
1625                 case SIHOST:
1626                         maddr = sc->sc_maddr;
1627                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1628                                                         /* flag nothing pending */
1629                         *(maddr+SIINTCL) = 0x00;        /* Set IRQ clear */
1630                         *(maddr+SIINTCL_CL) = 0x00;     /* Clear IRQ clear */
1631                         break;
1632                 case SIHOST2:
1633                         maddr = sc->sc_maddr;
1634                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1635                         *(maddr+SIPLIRQCLR) = 0x00;
1636                         *(maddr+SIPLIRQCLR) = 0x10;
1637                         break;
1638                 case SIPCI:
1639                         maddr = sc->sc_maddr;
1640                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1641                         *(maddr+SIPCIINTCL) = 0x0;
1642                         break;
1643                 case SIJETPCI:  /* fall through to JETISA case */
1644                 case SIJETISA:
1645                         maddr = sc->sc_maddr;
1646                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1647                         *(maddr+SIJETINTCL) = 0x0;
1648                         break;
1649                 case SIEISA:
1650                         maddr = sc->sc_maddr;
1651                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1652                         (void)inb(sc->sc_iobase + 3);
1653                         break;
1654                 case SIEMPTY:
1655                 default:
1656                         continue;
1657                 }
1658                 ((volatile struct si_reg *)maddr)->int_scounter = 0;
1659
1660                 /*
1661                  * check each port
1662                  */
1663                 for (pp = sc->sc_ports, port = 0; port < sc->sc_nport;
1664                      pp++, port++) {
1665                         ccbp = pp->sp_ccb;
1666                         tp = pp->sp_tty;
1667
1668                         /*
1669                          * See if a command has completed ?
1670                          */
1671                         if (ccbp->hi_stat != pp->sp_pend) {
1672                                 DPRINT((pp, DBG_INTR,
1673                                         "si_intr hi_stat = 0x%x, pend = %d\n",
1674                                         ccbp->hi_stat, pp->sp_pend));
1675                                 switch(pp->sp_pend) {
1676                                 case LOPEN:
1677                                 case MPEND:
1678                                 case MOPEN:
1679                                 case CONFIG:
1680                                 case SBREAK:
1681                                 case EBREAK:
1682                                         pp->sp_pend = ccbp->hi_stat;
1683                                                 /* sleeping in si_command */
1684                                         wakeup(&pp->sp_state);
1685                                         break;
1686                                 default:
1687                                         pp->sp_pend = ccbp->hi_stat;
1688                                 }
1689                         }
1690
1691                         /*
1692                          * Continue on if it's closed
1693                          */
1694                         if (ccbp->hi_stat == IDLE_CLOSE) {
1695                                 continue;
1696                         }
1697
1698                         /*
1699                          * Do modem state change if not a local device
1700                          */
1701                         si_modem_state(pp, tp, ccbp->hi_ip);
1702
1703                         /*
1704                          * Check to see if we should 'receive' characters.
1705                          */
1706                         if (tp->t_state & TS_CONNECTED &&
1707                             tp->t_state & TS_ISOPEN)
1708                                 isopen = 1;
1709                         else
1710                                 isopen = 0;
1711
1712                         /*
1713                          * Do input break processing
1714                          */
1715                         if (ccbp->hi_state & ST_BREAK) {
1716                                 if (isopen) {
1717                                     (*linesw[tp->t_line].l_rint)(TTY_BI, tp);
1718                                 }
1719                                 ccbp->hi_state &= ~ST_BREAK;   /* A Bit iffy this */
1720                                 DPRINT((pp, DBG_INTR, "si_intr break\n"));
1721                         }
1722
1723                         /*
1724                          * Do RX stuff - if not open then dump any characters.
1725                          * XXX: This is VERY messy and needs to be cleaned up.
1726                          *
1727                          * XXX: can we leave data in the host adapter buffer
1728                          * when the clists are full?  That may be dangerous
1729                          * if the user cannot get an interrupt signal through.
1730                          */
1731
1732         more_rx:        /* XXX Sorry. the nesting was driving me bats! :-( */
1733
1734                         if (!isopen) {
1735                                 ccbp->hi_rxopos = ccbp->hi_rxipos;
1736                                 goto end_rx;
1737                         }
1738
1739                         /*
1740                          * If the tty input buffers are blocked, stop emptying
1741                          * the incoming buffers and let the auto flow control
1742                          * assert..
1743                          */
1744                         if (tp->t_state & TS_TBLOCK) {
1745                                 goto end_rx;
1746                         }
1747
1748                         /*
1749                          * Process read characters if not skipped above
1750                          */
1751                         op = ccbp->hi_rxopos;
1752                         ip = ccbp->hi_rxipos;
1753                         c = ip - op;
1754                         if (c == 0) {
1755                                 goto end_rx;
1756                         }
1757
1758                         n = c & 0xff;
1759                         if (n > 250)
1760                                 n = 250;
1761
1762                         DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1763                                                 n, op, ip));
1764
1765                         /*
1766                          * Suck characters out of host card buffer into the
1767                          * "input staging buffer" - so that we dont leave the
1768                          * host card in limbo while we're possibly echoing
1769                          * characters and possibly flushing input inside the
1770                          * ldisc l_rint() routine.
1771                          */
1772                         if (n <= SI_BUFFERSIZE - op) {
1773
1774                                 DPRINT((pp, DBG_INTR, "\tsingle copy\n"));
1775                                 z = ccbp->hi_rxbuf + op;
1776                                 si_vbcopy(z, si_rxbuf, n);
1777
1778                                 op += n;
1779                         } else {
1780                                 x = SI_BUFFERSIZE - op;
1781
1782                                 DPRINT((pp, DBG_INTR, "\tdouble part 1 %d\n", x));
1783                                 z = ccbp->hi_rxbuf + op;
1784                                 si_vbcopy(z, si_rxbuf, x);
1785
1786                                 DPRINT((pp, DBG_INTR, "\tdouble part 2 %d\n",
1787                                         n - x));
1788                                 z = ccbp->hi_rxbuf;
1789                                 si_vbcopy(z, si_rxbuf + x, n - x);
1790
1791                                 op += n;
1792                         }
1793
1794                         /* clear collected characters from buffer */
1795                         ccbp->hi_rxopos = op;
1796
1797                         DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1798                                                 n, op, ip));
1799
1800                         /*
1801                          * at this point...
1802                          * n = number of chars placed in si_rxbuf
1803                          */
1804
1805                         /*
1806                          * Avoid the grotesquely inefficient lineswitch
1807                          * routine (ttyinput) in "raw" mode. It usually
1808                          * takes about 450 instructions (that's without
1809                          * canonical processing or echo!). slinput is
1810                          * reasonably fast (usually 40 instructions
1811                          * plus call overhead).
1812                          */
1813                         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1814
1815                                 /* block if the driver supports it */
1816                                 if (tp->t_rawq.c_cc + n >= SI_I_HIGH_WATER &&
1817                                     (tp->t_cflag & CRTS_IFLOW ||
1818                                      tp->t_iflag & IXOFF) &&
1819                                     !(tp->t_state & TS_TBLOCK))
1820                                         ttyblock(tp);
1821
1822                                 tk_nin += n;
1823                                 tk_rawcc += n;
1824                                 tp->t_rawcc += n;
1825
1826                                 pp->sp_delta_overflows +=
1827                                     b_to_q((char *)si_rxbuf, n, &tp->t_rawq);
1828
1829                                 ttwakeup(tp);
1830                                 if (tp->t_state & TS_TTSTOP &&
1831                                     (tp->t_iflag & IXANY ||
1832                                      tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1833                                         tp->t_state &= ~TS_TTSTOP;
1834                                         tp->t_lflag &= ~FLUSHO;
1835                                         si_start(tp);
1836                                 }
1837                         } else {
1838                                 /*
1839                                  * It'd be nice to not have to go through the
1840                                  * function call overhead for each char here.
1841                                  * It'd be nice to block input it, saving a
1842                                  * loop here and the call/return overhead.
1843                                  */
1844                                 for(x = 0; x < n; x++) {
1845                                         i = si_rxbuf[x];
1846                                         if ((*linesw[tp->t_line].l_rint)(i, tp)
1847                                              == -1) {
1848                                                 pp->sp_delta_overflows++;
1849                                         }
1850                                         /*
1851                                          * doesn't seem to be much point doing
1852                                          * this here.. this driver has no
1853                                          * softtty processing! ??
1854                                          */
1855                                         if (pp->sp_hotchar && i == pp->sp_hotchar) {
1856                                                 setsofttty();
1857                                         }
1858                                 }
1859                         }
1860                         goto more_rx;   /* try for more until RXbuf is empty */
1861
1862         end_rx:         /* XXX: Again, sorry about the gotos.. :-) */
1863
1864                         /*
1865                          * Do TX stuff
1866                          */
1867                         (*linesw[tp->t_line].l_start)(tp);
1868
1869                 } /* end of for (all ports on this controller) */
1870         } /* end of for (all controllers) */
1871
1872         in_intr = 0;
1873         DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "end si_intr\n"));
1874 }
1875
1876 /*
1877  * Nudge the transmitter...
1878  *
1879  * XXX: I inherited some funny code here.  It implies the host card only
1880  * interrupts when the transmit buffer reaches the low-water-mark, and does
1881  * not interrupt when it's actually hits empty.  In some cases, we have
1882  * processes waiting for complete drain, and we need to simulate an interrupt
1883  * about when we think the buffer is going to be empty (and retry if not).
1884  * I really am not certain about this...  I *need* the hardware manuals.
1885  */
1886 static void
1887 si_start(struct tty *tp)
1888 {
1889         struct si_port *pp;
1890         volatile struct si_channel *ccbp;
1891         struct clist *qp;
1892         BYTE ipos;
1893         int nchar;
1894         int count, n, amount, buffer_full;
1895
1896         crit_enter();
1897
1898         qp = &tp->t_outq;
1899         pp = TP2PP(tp);
1900
1901         DPRINT((pp, DBG_ENTRY|DBG_START,
1902                 "si_start(%x) t_state %x sp_state %x t_outq.c_cc %d\n",
1903                 tp, tp->t_state, pp->sp_state, qp->c_cc));
1904
1905         if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
1906                 goto out;
1907
1908         buffer_full = 0;
1909         ccbp = pp->sp_ccb;
1910
1911         count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1912         DPRINT((pp, DBG_START, "count %d\n", (BYTE)count));
1913
1914         while ((nchar = qp->c_cc) > 0) {
1915                 if ((BYTE)count >= 255) {
1916                         buffer_full++;
1917                         break;
1918                 }
1919                 amount = min(nchar, (255 - (BYTE)count));
1920                 ipos = (unsigned int)ccbp->hi_txipos;
1921                 n = q_to_b(&tp->t_outq, si_txbuf, amount);
1922                 /* will it fit in one lump? */
1923                 if ((SI_BUFFERSIZE - ipos) >= n) {
1924                         si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos], n);
1925                 } else {
1926                         si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos],
1927                                 SI_BUFFERSIZE - ipos);
1928                         si_bcopyv(si_txbuf + (SI_BUFFERSIZE - ipos),
1929                                 &ccbp->hi_txbuf[0], n - (SI_BUFFERSIZE - ipos));
1930                 }
1931                 ccbp->hi_txipos += n;
1932                 count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1933         }
1934
1935         if (count != 0 && nchar == 0) {
1936                 tp->t_state |= TS_BUSY;
1937         } else {
1938                 tp->t_state &= ~TS_BUSY;
1939         }
1940
1941         /* wakeup time? */
1942         ttwwakeup(tp);
1943
1944         DPRINT((pp, DBG_START, "count %d, nchar %d, tp->t_state 0x%x\n",
1945                 (BYTE)count, nchar, tp->t_state));
1946
1947         if (tp->t_state & TS_BUSY)
1948         {
1949                 int time;
1950
1951                 time = ttspeedtab(tp->t_ospeed, chartimes);
1952
1953                 if (time > 0) {
1954                         if (time < nchar)
1955                                 time = nchar / time;
1956                         else
1957                                 time = 2;
1958                 } else {
1959                         DPRINT((pp, DBG_START,
1960                                 "bad char time value! %d\n", time));
1961                         time = hz/10;
1962                 }
1963
1964                 if ((pp->sp_state & (SS_LSTART|SS_INLSTART)) == SS_LSTART) {
1965                         callout_stop(&pp->lstart_ch);
1966                 } else {
1967                         pp->sp_state |= SS_LSTART;
1968                 }
1969                 DPRINT((pp, DBG_START, "arming lstart, time=%d\n", time));
1970                 callout_reset(&pp->lstart_ch, time, si_lstart, (void *)pp);
1971         }
1972
1973 out:
1974         crit_exit();
1975         DPRINT((pp, DBG_EXIT|DBG_START, "leave si_start()\n"));
1976 }
1977
1978 /*
1979  * Note: called at splsoftclock from the timeout code
1980  * This has to deal with two things...  cause wakeups while waiting for
1981  * tty drains on last process exit, and call l_start at about the right
1982  * time for protocols like ppp.
1983  */
1984 static void
1985 si_lstart(void *arg)
1986 {
1987         struct si_port *pp = arg;
1988         struct tty *tp;
1989
1990         DPRINT((pp, DBG_ENTRY|DBG_LSTART, "si_lstart(%x) sp_state %x\n",
1991                 pp, pp->sp_state));
1992
1993         crit_enter();
1994
1995         if ((pp->sp_state & SS_OPEN) == 0 || (pp->sp_state & SS_LSTART) == 0) {
1996                 crit_exit();
1997                 return;
1998         }
1999         pp->sp_state &= ~SS_LSTART;
2000         pp->sp_state |= SS_INLSTART;
2001
2002         tp = pp->sp_tty;
2003
2004         /* deal with the process exit case */
2005         ttwwakeup(tp);
2006
2007         /* nudge protocols - eg: ppp */
2008         (*linesw[tp->t_line].l_start)(tp);
2009
2010         pp->sp_state &= ~SS_INLSTART;
2011         crit_exit();
2012 }
2013
2014 /*
2015  * Stop output on a line. called at spltty();
2016  */
2017 void
2018 si_stop(struct tty *tp, int rw)
2019 {
2020         volatile struct si_channel *ccbp;
2021         struct si_port *pp;
2022
2023         pp = TP2PP(tp);
2024         ccbp = pp->sp_ccb;
2025
2026         DPRINT((TP2PP(tp), DBG_ENTRY|DBG_STOP, "si_stop(%x,%x)\n", tp, rw));
2027
2028         /* XXX: must check (rw & FWRITE | FREAD) etc flushing... */
2029         if (rw & FWRITE) {
2030                 /* what level are we meant to be flushing anyway? */
2031                 if (tp->t_state & TS_BUSY) {
2032                         si_command(TP2PP(tp), WFLUSH, SI_NOWAIT);
2033                         tp->t_state &= ~TS_BUSY;
2034                         ttwwakeup(tp);  /* Bruce???? */
2035                 }
2036         }
2037 #if 1   /* XXX: this doesn't work right yet.. */
2038         /* XXX: this may have been failing because we used to call l_rint()
2039          * while we were looping based on these two counters. Now, we collect
2040          * the data and then loop stuffing it into l_rint(), making this
2041          * useless.  Should we cause this to blow away the staging buffer?
2042          */
2043         if (rw & FREAD) {
2044                 ccbp->hi_rxopos = ccbp->hi_rxipos;
2045         }
2046 #endif
2047 }
2048
2049 /*
2050  * Issue a command to the host card CPU.
2051  */
2052
2053 static void
2054 si_command(struct si_port *pp, int cmd, int waitflag)
2055 {
2056         volatile struct si_channel *ccbp = pp->sp_ccb;
2057         int x;
2058
2059         DPRINT((pp, DBG_ENTRY|DBG_PARAM, "si_command(%x,%x,%d): hi_stat 0x%x\n",
2060                 pp, cmd, waitflag, ccbp->hi_stat));
2061
2062         crit_enter();           /* Keep others out */
2063
2064         /* wait until it's finished what it was doing.. */
2065         /* XXX: sits in IDLE_BREAK until something disturbs it or break
2066          * is turned off. */
2067         while((x = ccbp->hi_stat) != IDLE_OPEN &&
2068                         x != IDLE_CLOSE &&
2069                         x != IDLE_BREAK &&
2070                         x != cmd) {
2071                 if (in_intr) {                  /* Prevent sleep in intr */
2072                         DPRINT((pp, DBG_PARAM,
2073                                 "cmd intr collision - completing %d\trequested %d\n",
2074                                 x, cmd));
2075                         crit_exit();
2076                         return;
2077                 } else if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, PCATCH,
2078                                 "sicmd1", 1)) {
2079                         crit_exit();
2080                         return;
2081                 }
2082         }
2083         /* it should now be in IDLE_{OPEN|CLOSE|BREAK}, or "cmd" */
2084
2085         /* if there was a pending command, cause a state-change wakeup */
2086         switch(pp->sp_pend) {
2087         case LOPEN:
2088         case MPEND:
2089         case MOPEN:
2090         case CONFIG:
2091         case SBREAK:
2092         case EBREAK:
2093                 wakeup(&pp->sp_state);
2094                 break;
2095         default:
2096                 break;
2097         }
2098
2099         pp->sp_pend = cmd;              /* New command pending */
2100         ccbp->hi_stat = cmd;            /* Post it */
2101
2102         if (waitflag) {
2103                 if (in_intr) {          /* If in interrupt handler */
2104                         DPRINT((pp, DBG_PARAM,
2105                                 "attempt to sleep in si_intr - cmd req %d\n",
2106                                 cmd));
2107                         crit_exit();
2108                         return;
2109                 } else while(ccbp->hi_stat != IDLE_OPEN &&
2110                              ccbp->hi_stat != IDLE_BREAK) {
2111                         if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, PCATCH,
2112                             "sicmd2", 0))
2113                                 break;
2114                 }
2115         }
2116         crit_exit();
2117 }
2118
2119 static void
2120 si_disc_optim(struct tty *tp, struct termios *t, struct si_port *pp)
2121 {
2122         /*
2123          * XXX can skip a lot more cases if Smarts.  Maybe
2124          * (IGNCR | ISTRIP | IXON) in c_iflag.  But perhaps we
2125          * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2126          */
2127         if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) &&
2128             (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) &&
2129             (!(t->c_iflag & PARMRK) ||
2130              (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) &&
2131             !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) &&
2132             linesw[tp->t_line].l_rint == ttyinput)
2133                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2134         else
2135                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2136         pp->sp_hotchar = linesw[tp->t_line].l_hotchar;
2137         DPRINT((pp, DBG_OPTIM, "bypass: %s, hotchar: %x\n",
2138                 (tp->t_state & TS_CAN_BYPASS_L_RINT) ? "on" : "off",
2139                 pp->sp_hotchar));
2140 }
2141
2142
2143 #ifdef  SI_DEBUG
2144
2145 void
2146 si_dprintf(struct si_port *pp, int flags, const char *fmt, ...)
2147 {
2148         __va_list ap;
2149
2150         if ((pp == NULL && (si_debug&flags)) ||
2151             (pp != NULL && ((pp->sp_debug&flags) || (si_debug&flags)))) {
2152                 if (pp != NULL)
2153                         kprintf("%ci%d(%d): ", 's',
2154                                 (int)SI_CARD(minor(pp->sp_tty->t_dev)),
2155                                 (int)SI_PORT(minor(pp->sp_tty->t_dev)));
2156                 __va_start(ap, fmt);
2157                 kvprintf(fmt, ap);
2158                 __va_end(ap);
2159         }
2160 }
2161
2162 static char *
2163 si_mctl2str(enum si_mctl cmd)
2164 {
2165         switch (cmd) {
2166         case GET:
2167                 return("GET");
2168         case SET:
2169                 return("SET");
2170         case BIS:
2171                 return("BIS");
2172         case BIC:
2173                 return("BIC");
2174         }
2175         return("BAD");
2176 }
2177
2178 #endif  /* DEBUG */
2179
2180 static char *
2181 si_modulename(int host_type, int uart_type)
2182 {
2183         switch (host_type) {
2184         /* Z280 based cards */
2185         case SIEISA:
2186         case SIHOST2:
2187         case SIHOST:
2188         case SIPCI:
2189                 switch (uart_type) {
2190                 case 0:
2191                         return(" (XIO)");
2192                 case 1:
2193                         return(" (SI)");
2194                 }
2195                 break;
2196         /* T225 based hosts */
2197         case SIJETPCI:
2198         case SIJETISA:
2199                 switch (uart_type) {
2200                 case 0:
2201                         return(" (SI)");
2202                 case 40:
2203                         return(" (XIO)");
2204                 case 72:
2205                         return(" (SXDC)");
2206                 }
2207                 break;
2208         }
2209         return("");
2210 }