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