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