Fix visibility of various functions in a number of headers.
[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)
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                                 lwkt_reltoken(&tty_token);
1006                                 return (error);
1007                         }
1008                         *ct = *(struct termios *)data;
1009                         lwkt_reltoken(&tty_token);
1010                         return (0);
1011                 case TIOCGETA:
1012                         *(struct termios *)data = *ct;
1013                         lwkt_reltoken(&tty_token);
1014                         return (0);
1015                 case TIOCGETD:
1016                         *(int *)data = TTYDISC;
1017                         lwkt_reltoken(&tty_token);
1018                         return (0);
1019                 case TIOCGWINSZ:
1020                         bzero(data, sizeof(struct winsize));
1021                         lwkt_reltoken(&tty_token);
1022                         return (0);
1023                 default:
1024                         lwkt_reltoken(&tty_token);
1025                         return (ENOTTY);
1026                 }
1027         }
1028         /*
1029          * Do the old-style ioctl compat routines...
1030          */
1031 #if defined(COMPAT_43)
1032         term = tp->t_termios;
1033         oldcmd = cmd;
1034         error = ttsetcompat(tp, &cmd, data, &term);
1035         if (error != 0) {
1036                 lwkt_reltoken(&tty_token);
1037                 return (error);
1038         }
1039         if (cmd != oldcmd)
1040                 data = (caddr_t)&term;
1041 #endif
1042         /*
1043          * Do the initial / lock state business
1044          */
1045         if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1046                 int     cc;
1047                 struct termios *dt = (struct termios *)data;
1048                 struct termios *lt = mynor & SI_CALLOUT_MASK
1049                                      ? &pp->sp_lout : &pp->sp_lin;
1050
1051                 dt->c_iflag = (tp->t_iflag & lt->c_iflag) |
1052                         (dt->c_iflag & ~lt->c_iflag);
1053                 dt->c_oflag = (tp->t_oflag & lt->c_oflag) |
1054                         (dt->c_oflag & ~lt->c_oflag);
1055                 dt->c_cflag = (tp->t_cflag & lt->c_cflag) |
1056                         (dt->c_cflag & ~lt->c_cflag);
1057                 dt->c_lflag = (tp->t_lflag & lt->c_lflag) |
1058                         (dt->c_lflag & ~lt->c_lflag);
1059                 for (cc = 0; cc < NCCS; ++cc)
1060                         if (lt->c_cc[cc] != 0)
1061                                 dt->c_cc[cc] = tp->t_cc[cc];
1062                 if (lt->c_ispeed != 0)
1063                         dt->c_ispeed = tp->t_ispeed;
1064                 if (lt->c_ospeed != 0)
1065                         dt->c_ospeed = tp->t_ospeed;
1066         }
1067
1068         /*
1069          * Block user-level writes to give the ttywait()
1070          * a chance to completely drain for commands
1071          * that require the port to be in a quiescent state.
1072          */
1073         switch (cmd) {
1074         case TIOCSETAW:
1075         case TIOCSETAF:
1076         case TIOCDRAIN:
1077 #ifdef COMPAT_43
1078         case TIOCSETP:
1079 #endif
1080                 blocked++;      /* block writes for ttywait() and siparam() */
1081                 si_write_enable(pp, 0);
1082         }
1083
1084         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data,
1085                                               ap->a_fflag, ap->a_cred);
1086         if (error != ENOIOCTL)
1087                 goto out;
1088
1089         crit_enter();
1090
1091         error = ttioctl(tp, cmd, data, ap->a_fflag);
1092         si_disc_optim(tp, &tp->t_termios, pp);
1093         if (error != ENOIOCTL) {
1094                 crit_exit();
1095                 goto out;
1096         }
1097
1098         error = 0;
1099         switch (cmd) {
1100         case TIOCSBRK:
1101                 si_command(pp, SBREAK, SI_WAIT);
1102                 break;
1103         case TIOCCBRK:
1104                 si_command(pp, EBREAK, SI_WAIT);
1105                 break;
1106         case TIOCSDTR:
1107                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1108                 break;
1109         case TIOCCDTR:
1110                 (void) si_modem(pp, SET, 0);
1111                 break;
1112         case TIOCMSET:
1113                 (void) si_modem(pp, SET, *(int *)data);
1114                 break;
1115         case TIOCMBIS:
1116                 (void) si_modem(pp, BIS, *(int *)data);
1117                 break;
1118         case TIOCMBIC:
1119                 (void) si_modem(pp, BIC, *(int *)data);
1120                 break;
1121         case TIOCMGET:
1122                 *(int *)data = si_modem(pp, GET, 0);
1123                 break;
1124         case TIOCMSDTRWAIT:
1125                 /* must be root since the wait applies to following logins */
1126                 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1127                 if (error == 0)
1128                         pp->sp_dtr_wait = *(int *)data * hz / 100;
1129                 break;
1130         case TIOCMGDTRWAIT:
1131                 *(int *)data = pp->sp_dtr_wait * 100 / hz;
1132                 break;
1133         default:
1134                 error = ENOTTY;
1135         }
1136         crit_exit();
1137
1138 out:
1139         DPRINT((pp, DBG_IOCTL|DBG_EXIT, "siioctl ret %d\n", error));
1140         if (blocked)
1141                 si_write_enable(pp, 1);
1142         lwkt_reltoken(&tty_token);
1143         return(error);
1144 }
1145
1146 /*
1147  * Handle the Specialix ioctls. All MUST be called via the CONTROL device
1148  */
1149 static int
1150 si_Sioctl(cdev_t dev, u_long cmd, caddr_t data, int flag, struct ucred *cred)
1151 {
1152         struct si_softc *xsc;
1153         struct si_port *xpp;
1154         volatile struct si_reg *regp;
1155         struct si_tcsi *dp;
1156         struct si_pstat *sps;
1157         int *ip, error = 0;
1158         int card, port;
1159         int mynor = minor(dev);
1160
1161         DPRINT((0, DBG_ENTRY|DBG_IOCTL, "si_Sioctl(%s,%lx,%p,%x)\n",
1162                 devtoname(dev), cmd, data, flag));
1163
1164 #if 1
1165         DPRINT((0, DBG_IOCTL, "TCSI_PORT=%lx\n", TCSI_PORT));
1166         DPRINT((0, DBG_IOCTL, "TCSI_CCB=%lx\n", TCSI_CCB));
1167         DPRINT((0, DBG_IOCTL, "TCSI_TTY=%lx\n", TCSI_TTY));
1168 #endif
1169
1170         if (!IS_CONTROLDEV(mynor)) {
1171                 DPRINT((0, DBG_IOCTL|DBG_FAIL, "not called from control device!\n"));
1172                 return(ENODEV);
1173         }
1174
1175         crit_enter();   /* better safe than sorry */
1176         lwkt_gettoken(&tty_token);
1177
1178         ip = (int *)data;
1179
1180 #define SUCHECK if ((error = priv_check_cred(cred, PRIV_ROOT, 0))) goto out
1181
1182         switch (cmd) {
1183         case TCSIPORTS:
1184                 *ip = si_Nports;
1185                 goto out;
1186         case TCSIMODULES:
1187                 *ip = si_Nmodules;
1188                 goto out;
1189         case TCSISDBG_ALL:
1190                 SUCHECK;
1191                 si_debug = *ip;
1192                 goto out;
1193         case TCSIGDBG_ALL:
1194                 *ip = si_debug;
1195                 goto out;
1196         default:
1197                 /*
1198                  * Check that a controller for this port exists
1199                  */
1200
1201                 /* may also be a struct si_pstat, a superset of si_tcsi */
1202
1203                 dp = (struct si_tcsi *)data;
1204                 sps = (struct si_pstat *)data;
1205                 card = dp->tc_card;
1206                 xsc = devclass_get_softc(si_devclass, card);    /* check.. */
1207                 if (xsc == NULL || xsc->sc_type == SIEMPTY) {
1208                         error = ENOENT;
1209                         goto out;
1210                 }
1211                 /*
1212                  * And check that a port exists
1213                  */
1214                 port = dp->tc_port;
1215                 if (port < 0 || port >= xsc->sc_nport) {
1216                         error = ENOENT;
1217                         goto out;
1218                 }
1219                 xpp = xsc->sc_ports + port;
1220                 regp = (struct si_reg *)xsc->sc_maddr;
1221         }
1222
1223         switch (cmd) {
1224         case TCSIDEBUG:
1225 #ifdef  SI_DEBUG
1226                 SUCHECK;
1227                 if (xpp->sp_debug)
1228                         xpp->sp_debug = 0;
1229                 else {
1230                         xpp->sp_debug = DBG_ALL;
1231                         DPRINT((xpp, DBG_IOCTL, "debug toggled %s\n",
1232                                 (xpp->sp_debug&DBG_ALL)?"ON":"OFF"));
1233                 }
1234                 break;
1235 #else
1236                 error = ENODEV;
1237                 goto out;
1238 #endif
1239         case TCSISDBG_LEVEL:
1240         case TCSIGDBG_LEVEL:
1241 #ifdef  SI_DEBUG
1242                 if (cmd == TCSIGDBG_LEVEL) {
1243                         dp->tc_dbglvl = xpp->sp_debug;
1244                 } else {
1245                         SUCHECK;
1246                         xpp->sp_debug = dp->tc_dbglvl;
1247                 }
1248                 break;
1249 #else
1250                 error = ENODEV;
1251                 goto out;
1252 #endif
1253         case TCSIGRXIT:
1254                 dp->tc_int = regp->rx_int_count;
1255                 break;
1256         case TCSIRXIT:
1257                 SUCHECK;
1258                 regp->rx_int_count = dp->tc_int;
1259                 break;
1260         case TCSIGIT:
1261                 dp->tc_int = regp->int_count;
1262                 break;
1263         case TCSIIT:
1264                 SUCHECK;
1265                 regp->int_count = dp->tc_int;
1266                 break;
1267         case TCSISTATE:
1268                 dp->tc_int = xpp->sp_ccb->hi_ip;
1269                 break;
1270         /* these next three use a different structure */
1271         case TCSI_PORT:
1272                 SUCHECK;
1273                 si_bcopy(xpp, &sps->tc_siport, sizeof(sps->tc_siport));
1274                 break;
1275         case TCSI_CCB:
1276                 SUCHECK;
1277                 si_vbcopy(xpp->sp_ccb, &sps->tc_ccb, sizeof(sps->tc_ccb));
1278                 break;
1279         case TCSI_TTY:
1280                 SUCHECK;
1281                 si_bcopy(xpp->sp_tty, &sps->tc_tty, sizeof(sps->tc_tty));
1282                 break;
1283         default:
1284                 error = EINVAL;
1285                 goto out;
1286         }
1287 out:
1288         lwkt_reltoken(&tty_token);
1289         crit_exit();
1290         return(error);          /* success */
1291 }
1292
1293 /*
1294  *      siparam()       : Configure line params
1295  *      called at spltty();
1296  *      this may sleep, does not flush, nor wait for drain, nor block writes
1297  *      caller must arrange this if it's important..
1298  */
1299 static int
1300 siparam(struct tty *tp, struct termios *t)
1301 {
1302         struct si_port *pp = TP2PP(tp);
1303         volatile struct si_channel *ccbp;
1304         int cflag, iflag, oflag, lflag;
1305         int error = 0;          /* shutup gcc */
1306         int ispeed = 0;         /* shutup gcc */
1307         int ospeed = 0;         /* shutup gcc */
1308         BYTE val;
1309
1310         lwkt_gettoken(&tty_token);
1311         DPRINT((pp, DBG_ENTRY|DBG_PARAM, "siparam(%p,%p)\n", tp, t));
1312         cflag = t->c_cflag;
1313         iflag = t->c_iflag;
1314         oflag = t->c_oflag;
1315         lflag = t->c_lflag;
1316         DPRINT((pp, DBG_PARAM, "OFLAG 0x%x CFLAG 0x%x IFLAG 0x%x LFLAG 0x%x\n",
1317                 oflag, cflag, iflag, lflag));
1318
1319         /* XXX - if Jet host and SXDC module, use extended baud rates */
1320
1321         /* if not hung up.. */
1322         if (t->c_ospeed != 0) {
1323                 /* translate baud rate to firmware values */
1324                 ospeed = ttspeedtab(t->c_ospeed, bdrates);
1325                 ispeed = t->c_ispeed ?
1326                          ttspeedtab(t->c_ispeed, bdrates) : ospeed;
1327
1328                 /* enforce legit baud rate */
1329                 if (ospeed < 0 || ispeed < 0) {
1330                         lwkt_reltoken(&tty_token);
1331                         return (EINVAL);
1332                 }
1333         }
1334
1335         crit_enter();
1336
1337         ccbp = pp->sp_ccb;
1338
1339         /* ========== set hi_break ========== */
1340         val = 0;
1341         if (iflag & IGNBRK)             /* Breaks */
1342                 val |= BR_IGN;
1343         if (iflag & BRKINT)             /* Interrupt on break? */
1344                 val |= BR_INT;
1345         if (iflag & PARMRK)             /* Parity mark? */
1346                 val |= BR_PARMRK;
1347         if (iflag & IGNPAR)             /* Ignore chars with parity errors? */
1348                 val |= BR_PARIGN;
1349         ccbp->hi_break = val;
1350
1351         /* ========== set hi_csr ========== */
1352         /* if not hung up.. */
1353         if (t->c_ospeed != 0) {
1354                 /* Set I/O speeds */
1355                  val = (ispeed << 4) | ospeed;
1356         }
1357         ccbp->hi_csr = val;
1358
1359         /* ========== set hi_mr2 ========== */
1360         val = 0;
1361         if (cflag & CSTOPB)                             /* Stop bits */
1362                 val |= MR2_2_STOP;
1363         else
1364                 val |= MR2_1_STOP;
1365         /*
1366          * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1367          * a DCE, hence the reverse sense of RTS and CTS
1368          */
1369         /* Output Flow - RTS must be raised before data can be sent */
1370         if (cflag & CCTS_OFLOW)
1371                 val |= MR2_RTSCONT;
1372
1373         ccbp->hi_mr2 = val;
1374
1375         /* ========== set hi_mr1 ========== */
1376         val = 0;
1377         if (!(cflag & PARENB))                          /* Parity */
1378                 val |= MR1_NONE;
1379         else
1380                 val |= MR1_WITH;
1381         if (cflag & PARODD)
1382                 val |= MR1_ODD;
1383
1384         if ((cflag & CS8) == CS8) {                     /* 8 data bits? */
1385                 val |= MR1_8_BITS;
1386         } else if ((cflag & CS7) == CS7) {              /* 7 data bits? */
1387                 val |= MR1_7_BITS;
1388         } else if ((cflag & CS6) == CS6) {              /* 6 data bits? */
1389                 val |= MR1_6_BITS;
1390         } else {                                        /* Must be 5 */
1391                 val |= MR1_5_BITS;
1392         }
1393         /*
1394          * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1395          * a DCE, hence the reverse sense of RTS and CTS
1396          */
1397         /* Input Flow - CTS is raised when port is ready to receive data */
1398         if (cflag & CRTS_IFLOW)
1399                 val |= MR1_CTSCONT;
1400
1401         ccbp->hi_mr1 = val;
1402
1403         /* ========== set hi_mask ========== */
1404         val = 0xff;
1405         if ((cflag & CS8) == CS8) {                     /* 8 data bits? */
1406                 val &= 0xFF;
1407         } else if ((cflag & CS7) == CS7) {              /* 7 data bits? */
1408                 val &= 0x7F;
1409         } else if ((cflag & CS6) == CS6) {              /* 6 data bits? */
1410                 val &= 0x3F;
1411         } else {                                        /* Must be 5 */
1412                 val &= 0x1F;
1413         }
1414         if (iflag & ISTRIP)
1415                 val &= 0x7F;
1416
1417         ccbp->hi_mask = val;
1418
1419         /* ========== set hi_prtcl ========== */
1420         val = SP_DCEN;          /* Monitor DCD always, or TIOCMGET misses it */
1421         if (iflag & IXANY)
1422                 val |= SP_TANY;
1423         if (iflag & IXON)
1424                 val |= SP_TXEN;
1425         if (iflag & IXOFF)
1426                 val |= SP_RXEN;
1427         if (iflag & INPCK)
1428                 val |= SP_PAEN;
1429
1430         ccbp->hi_prtcl = val;
1431
1432
1433         /* ========== set hi_{rx|tx}{on|off} ========== */
1434         /* XXX: the card TOTALLY shields us from the flow control... */
1435         ccbp->hi_txon = t->c_cc[VSTART];
1436         ccbp->hi_txoff = t->c_cc[VSTOP];
1437
1438         ccbp->hi_rxon = t->c_cc[VSTART];
1439         ccbp->hi_rxoff = t->c_cc[VSTOP];
1440
1441         /* ========== send settings to the card ========== */
1442         /* potential sleep here */
1443         if (ccbp->hi_stat == IDLE_CLOSE)                /* Not yet open */
1444                 si_command(pp, LOPEN, SI_WAIT);         /* open it */
1445         else
1446                 si_command(pp, CONFIG, SI_WAIT);        /* change params */
1447
1448         /* ========== set DTR etc ========== */
1449         /* Hangup if ospeed == 0 */
1450         if (t->c_ospeed == 0) {
1451                 (void) si_modem(pp, BIC, TIOCM_DTR|TIOCM_RTS);
1452         } else {
1453                 /*
1454                  * If the previous speed was 0, may need to re-enable
1455                  * the modem signals
1456                  */
1457                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1458         }
1459
1460         DPRINT((pp, DBG_PARAM, "siparam, complete: MR1 %x MR2 %x HI_MASK %x PRTCL %x HI_BREAK %x\n",
1461                 ccbp->hi_mr1, ccbp->hi_mr2, ccbp->hi_mask, ccbp->hi_prtcl, ccbp->hi_break));
1462
1463         crit_exit();
1464         lwkt_reltoken(&tty_token);
1465         return(error);
1466 }
1467
1468 /*
1469  * Enable or Disable the writes to this channel...
1470  * "state" ->  enabled = 1; disabled = 0;
1471  * NOTE: Must be called with tty_token held
1472  */
1473 static void
1474 si_write_enable(struct si_port *pp, int state)
1475 {
1476
1477         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1478         crit_enter();
1479
1480         if (state) {
1481                 pp->sp_state &= ~SS_BLOCKWRITE;
1482                 if (pp->sp_state & SS_WAITWRITE) {
1483                         pp->sp_state &= ~SS_WAITWRITE;
1484                         /* thunder away! */
1485                         wakeup((caddr_t)pp);
1486                 }
1487         } else {
1488                 pp->sp_state |= SS_BLOCKWRITE;
1489         }
1490
1491         crit_exit();
1492 }
1493
1494 /*
1495  * Set/Get state of modem control lines.
1496  * Due to DCE-like behaviour of the adapter, some signals need translation:
1497  *      TIOCM_DTR       DSR
1498  *      TIOCM_RTS       CTS
1499  * NOTE: Must be called with tty_token held
1500  */
1501 static int
1502 si_modem(struct si_port *pp, enum si_mctl cmd, int bits)
1503 {
1504         volatile struct si_channel *ccbp;
1505         int x;
1506
1507         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1508         DPRINT((pp, DBG_ENTRY|DBG_MODEM, "si_modem(%p,%s,%x)\n", pp, si_mctl2str(cmd), bits));
1509         ccbp = pp->sp_ccb;              /* Find channel address */
1510         switch (cmd) {
1511         case GET:
1512                 x = ccbp->hi_ip;
1513                 bits = TIOCM_LE;
1514                 if (x & IP_DCD)         bits |= TIOCM_CAR;
1515                 if (x & IP_DTR)         bits |= TIOCM_DTR;
1516                 if (x & IP_RTS)         bits |= TIOCM_RTS;
1517                 if (x & IP_RI)          bits |= TIOCM_RI;
1518                 return(bits);
1519         case SET:
1520                 ccbp->hi_op &= ~(OP_DSR|OP_CTS);
1521                 /* fall through */
1522         case BIS:
1523                 x = 0;
1524                 if (bits & TIOCM_DTR)
1525                         x |= OP_DSR;
1526                 if (bits & TIOCM_RTS)
1527                         x |= OP_CTS;
1528                 ccbp->hi_op |= x;
1529                 break;
1530         case BIC:
1531                 if (bits & TIOCM_DTR)
1532                         ccbp->hi_op &= ~OP_DSR;
1533                 if (bits & TIOCM_RTS)
1534                         ccbp->hi_op &= ~OP_CTS;
1535         }
1536         return 0;
1537 }
1538
1539 /*
1540  * Handle change of modem state
1541  * NOTE: Must be called with tty_token held
1542  */
1543 static void
1544 si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip)
1545 {
1546         ASSERT_LWKT_TOKEN_HELD(&tty_token);
1547                                                         /* if a modem dev */
1548         if (hi_ip & IP_DCD) {
1549                 if (!(pp->sp_last_hi_ip & IP_DCD)) {
1550                         DPRINT((pp, DBG_INTR, "modem carr on t_line %d\n",
1551                                 tp->t_line));
1552                         (void)(*linesw[tp->t_line].l_modem)(tp, 1);
1553                 }
1554         } else {
1555                 if (pp->sp_last_hi_ip & IP_DCD) {
1556                         DPRINT((pp, DBG_INTR, "modem carr off\n"));
1557                         if ((*linesw[tp->t_line].l_modem)(tp, 0))
1558                                 (void) si_modem(pp, SET, 0);
1559                 }
1560         }
1561         pp->sp_last_hi_ip = hi_ip;
1562
1563 }
1564
1565 /*
1566  * Poller to catch missed interrupts.
1567  *
1568  * Note that the SYSV Specialix drivers poll at 100 times per second to get
1569  * better response.  We could really use a "periodic" version timeout(). :-)
1570  */
1571 #ifdef POLL
1572 static void
1573 si_poll(void *nothing)
1574 {
1575         struct si_softc *sc;
1576         int i;
1577         volatile struct si_reg *regp;
1578         struct si_port *pp;
1579         int lost, port;
1580
1581         DPRINT((0, DBG_POLL, "si_poll()\n"));
1582         crit_enter();
1583         lwkt_gettoken(&tty_token);
1584         if (in_intr)
1585                 goto out;
1586         lost = 0;
1587         for (i = 0; i < si_numunits; i++) {
1588                 sc = devclass_get_softc(si_devclass, i);
1589                 if (sc == NULL || sc->sc_type == SIEMPTY)
1590                         continue;
1591                 regp = (struct si_reg *)sc->sc_maddr;
1592
1593                 /*
1594                  * See if there has been a pending interrupt for 2 seconds
1595                  * or so. The test (int_scounter >= 200) won't correspond
1596                  * to 2 seconds if int_count gets changed.
1597                  */
1598                 if (regp->int_pending != 0) {
1599                         if (regp->int_scounter >= 200 &&
1600                             regp->initstat == 1) {
1601                                 kprintf("si%d: lost intr\n", i);
1602                                 lost++;
1603                         }
1604                 } else {
1605                         regp->int_scounter = 0;
1606                 }
1607
1608                 /*
1609                  * gripe about no input flow control..
1610                  */
1611                 pp = sc->sc_ports;
1612                 for (port = 0; port < sc->sc_nport; pp++, port++) {
1613                         if (pp->sp_delta_overflows > 0) {
1614                                 kprintf("si%d: %d tty level buffer overflows\n",
1615                                         i, pp->sp_delta_overflows);
1616                                 pp->sp_delta_overflows = 0;
1617                         }
1618                 }
1619         }
1620         if (lost || si_realpoll)
1621                 si_intr(NULL);  /* call intr with fake vector */
1622 out:
1623         lwkt_reltoken(&tty_token);
1624         crit_exit();
1625
1626         callout_reset(&poll_ch, si_pollrate, si_poll, NULL);
1627 }
1628 #endif  /* ifdef POLL */
1629
1630 /*
1631  * The interrupt handler polls ALL ports on ALL adapters each time
1632  * it is called.
1633  */
1634
1635 static BYTE si_rxbuf[SI_BUFFERSIZE];    /* input staging area */
1636 static BYTE si_txbuf[SI_BUFFERSIZE];    /* output staging area */
1637
1638 void
1639 si_intr(void *arg)
1640 {
1641         struct si_softc *sc;
1642         struct si_port *pp;
1643         volatile struct si_channel *ccbp;
1644         struct tty *tp;
1645         volatile caddr_t maddr;
1646         BYTE op, ip;
1647         int x, card, port, n, i, isopen;
1648         volatile BYTE *z;
1649         BYTE c;
1650
1651         sc = arg;
1652
1653         DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "si_intr\n"));
1654         if (in_intr)
1655                 return;
1656         in_intr = 1;
1657
1658         lwkt_gettoken(&tty_token);
1659
1660         /*
1661          * When we get an int we poll all the channels and do ALL pending
1662          * work, not just the first one we find. This allows all cards to
1663          * share the same vector.
1664          *
1665          * XXX - But if we're sharing the vector with something that's NOT
1666          * a SI/XIO/SX card, we may be making more work for ourselves.
1667          */
1668         for (card = 0; card < si_numunits; card++) {
1669                 sc = devclass_get_softc(si_devclass, card);
1670                 if (sc == NULL || sc->sc_type == SIEMPTY)
1671                         continue;
1672
1673                 /*
1674                  * First, clear the interrupt
1675                  */
1676                 switch(sc->sc_type) {
1677                 case SIHOST:
1678                         maddr = sc->sc_maddr;
1679                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1680                                                         /* flag nothing pending */
1681                         *(maddr+SIINTCL) = 0x00;        /* Set IRQ clear */
1682                         *(maddr+SIINTCL_CL) = 0x00;     /* Clear IRQ clear */
1683                         break;
1684                 case SIHOST2:
1685                         maddr = sc->sc_maddr;
1686                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1687                         *(maddr+SIPLIRQCLR) = 0x00;
1688                         *(maddr+SIPLIRQCLR) = 0x10;
1689                         break;
1690                 case SIPCI:
1691                         maddr = sc->sc_maddr;
1692                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1693                         *(maddr+SIPCIINTCL) = 0x0;
1694                         break;
1695                 case SIJETPCI:  /* fall through to JETISA case */
1696                 case SIJETISA:
1697                         maddr = sc->sc_maddr;
1698                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1699                         *(maddr+SIJETINTCL) = 0x0;
1700                         break;
1701                 case SIEISA:
1702                         maddr = sc->sc_maddr;
1703                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1704                         (void)inb(sc->sc_iobase + 3);
1705                         break;
1706                 case SIEMPTY:
1707                 default:
1708                         continue;
1709                 }
1710                 ((volatile struct si_reg *)maddr)->int_scounter = 0;
1711
1712                 /*
1713                  * check each port
1714                  */
1715                 for (pp = sc->sc_ports, port = 0; port < sc->sc_nport;
1716                      pp++, port++) {
1717                         ccbp = pp->sp_ccb;
1718                         tp = pp->sp_tty;
1719
1720                         /*
1721                          * See if a command has completed ?
1722                          */
1723                         if (ccbp->hi_stat != pp->sp_pend) {
1724                                 DPRINT((pp, DBG_INTR,
1725                                         "si_intr hi_stat = 0x%x, pend = %d\n",
1726                                         ccbp->hi_stat, pp->sp_pend));
1727                                 switch(pp->sp_pend) {
1728                                 case LOPEN:
1729                                 case MPEND:
1730                                 case MOPEN:
1731                                 case CONFIG:
1732                                 case SBREAK:
1733                                 case EBREAK:
1734                                         pp->sp_pend = ccbp->hi_stat;
1735                                                 /* sleeping in si_command */
1736                                         wakeup(&pp->sp_state);
1737                                         break;
1738                                 default:
1739                                         pp->sp_pend = ccbp->hi_stat;
1740                                 }
1741                         }
1742
1743                         /*
1744                          * Continue on if it's closed
1745                          */
1746                         if (ccbp->hi_stat == IDLE_CLOSE) {
1747                                 continue;
1748                         }
1749
1750                         /*
1751                          * Do modem state change if not a local device
1752                          */
1753                         si_modem_state(pp, tp, ccbp->hi_ip);
1754
1755                         /*
1756                          * Check to see if we should 'receive' characters.
1757                          */
1758                         if (tp->t_state & TS_CONNECTED &&
1759                             tp->t_state & TS_ISOPEN)
1760                                 isopen = 1;
1761                         else
1762                                 isopen = 0;
1763
1764                         /*
1765                          * Do input break processing
1766                          */
1767                         if (ccbp->hi_state & ST_BREAK) {
1768                                 if (isopen) {
1769                                     (*linesw[tp->t_line].l_rint)(TTY_BI, tp);
1770                                 }
1771                                 ccbp->hi_state &= ~ST_BREAK;   /* A Bit iffy this */
1772                                 DPRINT((pp, DBG_INTR, "si_intr break\n"));
1773                         }
1774
1775                         /*
1776                          * Do RX stuff - if not open then dump any characters.
1777                          * XXX: This is VERY messy and needs to be cleaned up.
1778                          *
1779                          * XXX: can we leave data in the host adapter buffer
1780                          * when the clists are full?  That may be dangerous
1781                          * if the user cannot get an interrupt signal through.
1782                          */
1783
1784         more_rx:        /* XXX Sorry. the nesting was driving me bats! :-( */
1785
1786                         if (!isopen) {
1787                                 ccbp->hi_rxopos = ccbp->hi_rxipos;
1788                                 goto end_rx;
1789                         }
1790
1791                         /*
1792                          * If the tty input buffers are blocked, stop emptying
1793                          * the incoming buffers and let the auto flow control
1794                          * assert..
1795                          */
1796                         if (tp->t_state & TS_TBLOCK) {
1797                                 goto end_rx;
1798                         }
1799
1800                         /*
1801                          * Process read characters if not skipped above
1802                          */
1803                         op = ccbp->hi_rxopos;
1804                         ip = ccbp->hi_rxipos;
1805                         c = ip - op;
1806                         if (c == 0) {
1807                                 goto end_rx;
1808                         }
1809
1810                         n = c & 0xff;
1811                         if (n > 250)
1812                                 n = 250;
1813
1814                         DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1815                                                 n, op, ip));
1816
1817                         /*
1818                          * Suck characters out of host card buffer into the
1819                          * "input staging buffer" - so that we dont leave the
1820                          * host card in limbo while we're possibly echoing
1821                          * characters and possibly flushing input inside the
1822                          * ldisc l_rint() routine.
1823                          */
1824                         if (n <= SI_BUFFERSIZE - op) {
1825
1826                                 DPRINT((pp, DBG_INTR, "\tsingle copy\n"));
1827                                 z = ccbp->hi_rxbuf + op;
1828                                 si_vbcopy(z, si_rxbuf, n);
1829
1830                                 op += n;
1831                         } else {
1832                                 x = SI_BUFFERSIZE - op;
1833
1834                                 DPRINT((pp, DBG_INTR, "\tdouble part 1 %d\n", x));
1835                                 z = ccbp->hi_rxbuf + op;
1836                                 si_vbcopy(z, si_rxbuf, x);
1837
1838                                 DPRINT((pp, DBG_INTR, "\tdouble part 2 %d\n",
1839                                         n - x));
1840                                 z = ccbp->hi_rxbuf;
1841                                 si_vbcopy(z, si_rxbuf + x, n - x);
1842
1843                                 op += n;
1844                         }
1845
1846                         /* clear collected characters from buffer */
1847                         ccbp->hi_rxopos = op;
1848
1849                         DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1850                                                 n, op, ip));
1851
1852                         /*
1853                          * at this point...
1854                          * n = number of chars placed in si_rxbuf
1855                          */
1856
1857                         /*
1858                          * Avoid the grotesquely inefficient lineswitch
1859                          * routine (ttyinput) in "raw" mode. It usually
1860                          * takes about 450 instructions (that's without
1861                          * canonical processing or echo!). slinput is
1862                          * reasonably fast (usually 40 instructions
1863                          * plus call overhead).
1864                          */
1865                         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1866
1867                                 /* block if the driver supports it */
1868                                 if (tp->t_rawq.c_cc + n >= SI_I_HIGH_WATER &&
1869                                     (tp->t_cflag & CRTS_IFLOW ||
1870                                      tp->t_iflag & IXOFF) &&
1871                                     !(tp->t_state & TS_TBLOCK))
1872                                         ttyblock(tp);
1873
1874                                 tk_nin += n;
1875                                 tk_rawcc += n;
1876                                 tp->t_rawcc += n;
1877
1878                                 pp->sp_delta_overflows +=
1879                                     b_to_q((char *)si_rxbuf, n, &tp->t_rawq);
1880
1881                                 ttwakeup(tp);
1882                                 if (tp->t_state & TS_TTSTOP &&
1883                                     (tp->t_iflag & IXANY ||
1884                                      tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1885                                         tp->t_state &= ~TS_TTSTOP;
1886                                         tp->t_lflag &= ~FLUSHO;
1887                                         si_start(tp);
1888                                 }
1889                         } else {
1890                                 /*
1891                                  * It'd be nice to not have to go through the
1892                                  * function call overhead for each char here.
1893                                  * It'd be nice to block input it, saving a
1894                                  * loop here and the call/return overhead.
1895                                  */
1896                                 for(x = 0; x < n; x++) {
1897                                         i = si_rxbuf[x];
1898                                         if ((*linesw[tp->t_line].l_rint)(i, tp)
1899                                              == -1) {
1900                                                 pp->sp_delta_overflows++;
1901                                         }
1902                                         /*
1903                                          * doesn't seem to be much point doing
1904                                          * this here.. this driver has no
1905                                          * softtty processing! ??
1906                                          */
1907                                         if (pp->sp_hotchar && i == pp->sp_hotchar) {
1908                                                 setsofttty();
1909                                         }
1910                                 }
1911                         }
1912                         goto more_rx;   /* try for more until RXbuf is empty */
1913
1914         end_rx:         /* XXX: Again, sorry about the gotos.. :-) */
1915
1916                         /*
1917                          * Do TX stuff
1918                          */
1919                         (*linesw[tp->t_line].l_start)(tp);
1920
1921                 } /* end of for (all ports on this controller) */
1922         } /* end of for (all controllers) */
1923
1924         in_intr = 0;
1925         lwkt_reltoken(&tty_token);
1926         DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "end si_intr\n"));
1927 }
1928
1929 /*
1930  * Nudge the transmitter...
1931  *
1932  * XXX: I inherited some funny code here.  It implies the host card only
1933  * interrupts when the transmit buffer reaches the low-water-mark, and does
1934  * not interrupt when it's actually hits empty.  In some cases, we have
1935  * processes waiting for complete drain, and we need to simulate an interrupt
1936  * about when we think the buffer is going to be empty (and retry if not).
1937  * I really am not certain about this...  I *need* the hardware manuals.
1938  */
1939 static void
1940 si_start(struct tty *tp)
1941 {
1942         struct si_port *pp;
1943         volatile struct si_channel *ccbp;
1944         struct clist *qp;
1945         BYTE ipos;
1946         int nchar;
1947         int count, n, amount, buffer_full;
1948
1949         crit_enter();
1950         lwkt_gettoken(&tty_token);
1951
1952         qp = &tp->t_outq;
1953         pp = TP2PP(tp);
1954
1955         DPRINT((pp, DBG_ENTRY|DBG_START,
1956                 "si_start(%p) t_state %x sp_state %x t_outq.c_cc %d\n",
1957                 tp, tp->t_state, pp->sp_state, qp->c_cc));
1958
1959         if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
1960                 goto out;
1961
1962         buffer_full = 0;
1963         ccbp = pp->sp_ccb;
1964
1965         count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1966         DPRINT((pp, DBG_START, "count %d\n", (BYTE)count));
1967
1968         while ((nchar = qp->c_cc) > 0) {
1969                 if ((BYTE)count >= 255) {
1970                         buffer_full++;
1971                         break;
1972                 }
1973                 amount = min(nchar, (255 - (BYTE)count));
1974                 ipos = (unsigned int)ccbp->hi_txipos;
1975                 n = q_to_b(&tp->t_outq, si_txbuf, amount);
1976                 /* will it fit in one lump? */
1977                 if ((SI_BUFFERSIZE - ipos) >= n) {
1978                         si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos], n);
1979                 } else {
1980                         si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos],
1981                                 SI_BUFFERSIZE - ipos);
1982                         si_bcopyv(si_txbuf + (SI_BUFFERSIZE - ipos),
1983                                 &ccbp->hi_txbuf[0], n - (SI_BUFFERSIZE - ipos));
1984                 }
1985                 ccbp->hi_txipos += n;
1986                 count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1987         }
1988
1989         if (count != 0 && nchar == 0) {
1990                 tp->t_state |= TS_BUSY;
1991         } else {
1992                 tp->t_state &= ~TS_BUSY;
1993         }
1994
1995         /* wakeup time? */
1996         ttwwakeup(tp);
1997
1998         DPRINT((pp, DBG_START, "count %d, nchar %d, tp->t_state 0x%x\n",
1999                 (BYTE)count, nchar, tp->t_state));
2000
2001         if (tp->t_state & TS_BUSY)
2002         {
2003                 int time;
2004
2005                 time = ttspeedtab(tp->t_ospeed, chartimes);
2006
2007                 if (time > 0) {
2008                         if (time < nchar)
2009                                 time = nchar / time;
2010                         else
2011                                 time = 2;
2012                 } else {
2013                         DPRINT((pp, DBG_START,
2014                                 "bad char time value! %d\n", time));
2015                         time = hz/10;
2016                 }
2017
2018                 if ((pp->sp_state & (SS_LSTART|SS_INLSTART)) == SS_LSTART) {
2019                         callout_stop(&pp->lstart_ch);
2020                 } else {
2021                         pp->sp_state |= SS_LSTART;
2022                 }
2023                 DPRINT((pp, DBG_START, "arming lstart, time=%d\n", time));
2024                 callout_reset(&pp->lstart_ch, time, si_lstart, (void *)pp);
2025         }
2026
2027 out:
2028         lwkt_reltoken(&tty_token);
2029         crit_exit();
2030         DPRINT((pp, DBG_EXIT|DBG_START, "leave si_start()\n"));
2031 }
2032
2033 /*
2034  * Note: called at splsoftclock from the timeout code
2035  * This has to deal with two things...  cause wakeups while waiting for
2036  * tty drains on last process exit, and call l_start at about the right
2037  * time for protocols like ppp.
2038  */
2039 static void
2040 si_lstart(void *arg)
2041 {
2042         struct si_port *pp = arg;
2043         struct tty *tp;
2044
2045         DPRINT((pp, DBG_ENTRY|DBG_LSTART, "si_lstart(%p) sp_state %x\n",
2046                 pp, pp->sp_state));
2047
2048         crit_enter();
2049         lwkt_gettoken(&tty_token);
2050
2051         if ((pp->sp_state & SS_OPEN) == 0 || (pp->sp_state & SS_LSTART) == 0) {
2052                 lwkt_reltoken(&tty_token);
2053                 crit_exit();
2054                 return;
2055         }
2056         pp->sp_state &= ~SS_LSTART;
2057         pp->sp_state |= SS_INLSTART;
2058
2059         tp = pp->sp_tty;
2060
2061         /* deal with the process exit case */
2062         ttwwakeup(tp);
2063
2064         /* nudge protocols - eg: ppp */
2065         (*linesw[tp->t_line].l_start)(tp);
2066
2067         pp->sp_state &= ~SS_INLSTART;
2068         lwkt_reltoken(&tty_token);
2069         crit_exit();
2070 }
2071
2072 /*
2073  * Stop output on a line. called at spltty();
2074  */
2075 void
2076 si_stop(struct tty *tp, int rw)
2077 {
2078         volatile struct si_channel *ccbp;
2079         struct si_port *pp;
2080
2081         lwkt_gettoken(&tty_token);
2082         pp = TP2PP(tp);
2083         ccbp = pp->sp_ccb;
2084
2085         DPRINT((TP2PP(tp), DBG_ENTRY|DBG_STOP, "si_stop(%p,%x)\n", tp, rw));
2086
2087         /* XXX: must check (rw & FWRITE | FREAD) etc flushing... */
2088         if (rw & FWRITE) {
2089                 /* what level are we meant to be flushing anyway? */
2090                 if (tp->t_state & TS_BUSY) {
2091                         si_command(TP2PP(tp), WFLUSH, SI_NOWAIT);
2092                         tp->t_state &= ~TS_BUSY;
2093                         ttwwakeup(tp);  /* Bruce???? */
2094                 }
2095         }
2096 #if 1   /* XXX: this doesn't work right yet.. */
2097         /* XXX: this may have been failing because we used to call l_rint()
2098          * while we were looping based on these two counters. Now, we collect
2099          * the data and then loop stuffing it into l_rint(), making this
2100          * useless.  Should we cause this to blow away the staging buffer?
2101          */
2102         if (rw & FREAD) {
2103                 ccbp->hi_rxopos = ccbp->hi_rxipos;
2104         }
2105 #endif
2106         lwkt_reltoken(&tty_token);
2107 }
2108
2109 /*
2110  * Issue a command to the host card CPU.
2111  */
2112
2113 static void
2114 si_command(struct si_port *pp, int cmd, int waitflag)
2115 {
2116         volatile struct si_channel *ccbp = pp->sp_ccb;
2117         int x;
2118
2119         DPRINT((pp, DBG_ENTRY|DBG_PARAM, "si_command(%p,%x,%d): hi_stat 0x%x\n",
2120                 pp, cmd, waitflag, ccbp->hi_stat));
2121
2122         crit_enter();           /* Keep others out */
2123         lwkt_gettoken(&tty_token);
2124
2125         /* wait until it's finished what it was doing.. */
2126         /* XXX: sits in IDLE_BREAK until something disturbs it or break
2127          * is turned off. */
2128         while((x = ccbp->hi_stat) != IDLE_OPEN &&
2129                         x != IDLE_CLOSE &&
2130                         x != IDLE_BREAK &&
2131                         x != cmd) {
2132                 if (in_intr) {                  /* Prevent sleep in intr */
2133                         DPRINT((pp, DBG_PARAM,
2134                                 "cmd intr collision - completing %d\trequested %d\n",
2135                                 x, cmd));
2136                         lwkt_reltoken(&tty_token);
2137                         crit_exit();
2138                         return;
2139                 } else if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, PCATCH,
2140                                 "sicmd1", 1)) {
2141                         lwkt_reltoken(&tty_token);
2142                         crit_exit();
2143                         return;
2144                 }
2145         }
2146         /* it should now be in IDLE_{OPEN|CLOSE|BREAK}, or "cmd" */
2147
2148         /* if there was a pending command, cause a state-change wakeup */
2149         switch(pp->sp_pend) {
2150         case LOPEN:
2151         case MPEND:
2152         case MOPEN:
2153         case CONFIG:
2154         case SBREAK:
2155         case EBREAK:
2156                 wakeup(&pp->sp_state);
2157                 break;
2158         default:
2159                 break;
2160         }
2161
2162         pp->sp_pend = cmd;              /* New command pending */
2163         ccbp->hi_stat = cmd;            /* Post it */
2164
2165         if (waitflag) {
2166                 if (in_intr) {          /* If in interrupt handler */
2167                         DPRINT((pp, DBG_PARAM,
2168                                 "attempt to sleep in si_intr - cmd req %d\n",
2169                                 cmd));
2170                         lwkt_reltoken(&tty_token);
2171                         crit_exit();
2172                         return;
2173                 } else while(ccbp->hi_stat != IDLE_OPEN &&
2174                              ccbp->hi_stat != IDLE_BREAK) {
2175                         if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, PCATCH,
2176                             "sicmd2", 0))
2177                                 break;
2178                 }
2179         }
2180         lwkt_reltoken(&tty_token);
2181         crit_exit();
2182 }
2183
2184 static void
2185 si_disc_optim(struct tty *tp, struct termios *t, struct si_port *pp)
2186 {
2187         lwkt_gettoken(&tty_token);
2188         /*
2189          * XXX can skip a lot more cases if Smarts.  Maybe
2190          * (IGNCR | ISTRIP | IXON) in c_iflag.  But perhaps we
2191          * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2192          */
2193         if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) &&
2194             (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) &&
2195             (!(t->c_iflag & PARMRK) ||
2196              (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) &&
2197             !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) &&
2198             linesw[tp->t_line].l_rint == ttyinput)
2199                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2200         else
2201                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2202         pp->sp_hotchar = linesw[tp->t_line].l_hotchar;
2203         DPRINT((pp, DBG_OPTIM, "bypass: %s, hotchar: %x\n",
2204                 (tp->t_state & TS_CAN_BYPASS_L_RINT) ? "on" : "off",
2205                 pp->sp_hotchar));
2206         lwkt_reltoken(&tty_token);
2207 }
2208
2209
2210 #ifdef  SI_DEBUG
2211
2212 void
2213 si_dprintf(struct si_port *pp, int flags, const char *fmt, ...)
2214 {
2215         __va_list ap;
2216
2217         if ((pp == NULL && (si_debug&flags)) ||
2218             (pp != NULL && ((pp->sp_debug&flags) || (si_debug&flags)))) {
2219                 if (pp != NULL)
2220                         kprintf("%ci%d(%d): ", 's',
2221                                 (int)SI_CARD(minor(pp->sp_tty->t_dev)),
2222                                 (int)SI_PORT(minor(pp->sp_tty->t_dev)));
2223                 __va_start(ap, fmt);
2224                 kvprintf(fmt, ap);
2225                 __va_end(ap);
2226         }
2227 }
2228
2229 static char *
2230 si_mctl2str(enum si_mctl cmd)
2231 {
2232         switch (cmd) {
2233         case GET:
2234                 return("GET");
2235         case SET:
2236                 return("SET");
2237         case BIS:
2238                 return("BIS");
2239         case BIC:
2240                 return("BIC");
2241         }
2242         return("BAD");
2243 }
2244
2245 #endif  /* DEBUG */
2246
2247 static char *
2248 si_modulename(int host_type, int uart_type)
2249 {
2250         switch (host_type) {
2251         /* Z280 based cards */
2252         case SIEISA:
2253         case SIHOST2:
2254         case SIHOST:
2255         case SIPCI:
2256                 switch (uart_type) {
2257                 case 0:
2258                         return(" (XIO)");
2259                 case 1:
2260                         return(" (SI)");
2261                 }
2262                 break;
2263         /* T225 based hosts */
2264         case SIJETPCI:
2265         case SIJETISA:
2266                 switch (uart_type) {
2267                 case 0:
2268                         return(" (SI)");
2269                 case 40:
2270                         return(" (XIO)");
2271                 case 72:
2272                         return(" (SXDC)");
2273                 }
2274                 break;
2275         }
2276         return("");
2277 }