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