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