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