DEVFS - remove dev_ops_add(), dev_ops_get(), and get_dev()
[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.23 2006/12/22 23:26:24 swildner 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/priv.h>
54 #include <sys/conf.h>
55 #include <sys/fcntl.h>
56 #include <sys/dkstat.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/sysctl.h>
60 #include <sys/bus.h>
61 #include <sys/rman.h>
62 #include <sys/thread2.h>
63
64 #include <machine/clock.h>
65
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68
69 #include <machine/stdarg.h>
70
71 #include "sireg.h"
72 #include "sivar.h"
73 #include "si.h"
74
75 /*
76  * This device driver is designed to interface the Specialix International
77  * SI, XIO and SX range of serial multiplexor cards to FreeBSD on an ISA,
78  * EISA or PCI bus machine.
79  *
80  * The controller is interfaced to the host via dual port RAM
81  * and an interrupt.
82  *
83  * The code for the Host 1 (very old ISA cards) has not been tested.
84  */
85
86 #define POLL            /* turn on poller to scan for lost interrupts */
87 #define REALPOLL        /* on each poll, scan for work regardless */
88 #define POLLHZ  (hz/10) /* 10 times per second */
89 #define SI_I_HIGH_WATER (TTYHOG - 2 * SI_BUFFERSIZE)
90 #define INT_COUNT 25000         /* max of 125 ints per second */
91 #define JET_INT_COUNT 100       /* max of 100 ints per second */
92 #define RXINT_COUNT 1   /* one rxint per 10 milliseconds */
93
94 enum si_mctl { GET, SET, BIS, BIC };
95
96 static void si_command(struct si_port *, int, int);
97 static int si_modem(struct si_port *, enum si_mctl, int);
98 static void si_write_enable(struct si_port *, int);
99 static int si_Sioctl(cdev_t, u_long, caddr_t, int, struct ucred *);
100 static void si_start(struct tty *);
101 static void si_stop(struct tty *, int);
102 static timeout_t si_lstart;
103 static void si_disc_optim(struct tty *tp, struct termios *t,struct si_port *pp);
104 static void sihardclose(struct si_port *pp);
105 static void sidtrwakeup(void *chan);
106
107 #ifdef SI_DEBUG
108 static char     *si_mctl2str(enum si_mctl cmd);
109 #endif
110
111 static int      siparam(struct tty *, struct termios *);
112
113 static void     si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip);
114 static char *   si_modulename(int host_type, int uart_type);
115
116 static  d_open_t        siopen;
117 static  d_close_t       siclose;
118 static  d_write_t       siwrite;
119 static  d_ioctl_t       siioctl;
120
121 #define CDEV_MAJOR      68
122 static struct dev_ops si_ops = {
123         { "si", CDEV_MAJOR, D_TTY | D_KQFILTER },
124         .d_open =       siopen,
125         .d_close =      siclose,
126         .d_read =       ttyread,
127         .d_write =      siwrite,
128         .d_ioctl =      siioctl,
129         .d_poll =       ttypoll,
130         .d_kqfilter =   ttykqfilter,
131         .d_revoke =     ttyrevoke
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                         kprintf("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                 kprintf("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                 kprintf("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                 kprintf("si%d: %s - no ports found\n", unit,
437                         si_type[sc->sc_type]);
438                 return 0;
439         default:
440                 kprintf("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                         kprintf("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                         kprintf("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 = kmalloc(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                         kprintf("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                         kprintf("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         for (x = 0; x < sc->sc_nport; x++) {
604                 /* sync with the manuals that start at 1 */
605                 y = x + 1 + unit * (1 << SI_CARDSHIFT);
606                 make_dev(&si_ops, x, 0, 0, 0600, "ttyA%02d", y);
607                 make_dev(&si_ops, x + 0x00080, 0, 0, 0600, "cuaA%02d", y);
608                 make_dev(&si_ops, x + 0x10000, 0, 0, 0600, "ttyiA%02d", y);
609                 make_dev(&si_ops, x + 0x10080, 0, 0, 0600, "cuaiA%02d", y);
610                 make_dev(&si_ops, x + 0x20000, 0, 0, 0600, "ttylA%02d", y);
611                 make_dev(&si_ops, x + 0x20080, 0, 0, 0600, "cualA%02d", y);
612         }
613         make_dev(&si_ops, 0x40000, 0, 0, 0600, "si_control");
614         return (0);
615 }
616
617 static  int
618 siopen(struct dev_open_args *ap)
619 {
620         cdev_t dev = ap->a_head.a_dev;
621         int error;
622         int card, port;
623         struct si_softc *sc;
624         struct tty *tp;
625         volatile struct si_channel *ccbp;
626         struct si_port *pp;
627         int mynor = minor(dev);
628
629         /* quickly let in /dev/si_control */
630         if (IS_CONTROLDEV(mynor)) {
631                 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)))
632                         return(error);
633                 return(0);
634         }
635
636         card = SI_CARD(mynor);
637         sc = devclass_get_softc(si_devclass, card);
638         if (sc == NULL)
639                 return (ENXIO);
640
641         if (sc->sc_type == SIEMPTY) {
642                 DPRINT((0, DBG_OPEN|DBG_FAIL, "si%d: type %s??\n",
643                         card, sc->sc_typename));
644                 return(ENXIO);
645         }
646
647         port = SI_PORT(mynor);
648         if (port >= sc->sc_nport) {
649                 DPRINT((0, DBG_OPEN|DBG_FAIL, "si%d: nports %d\n",
650                         card, sc->sc_nport));
651                 return(ENXIO);
652         }
653
654 #ifdef  POLL
655         /*
656          * We've now got a device, so start the poller.
657          */
658         if (init_finished == 0) {
659                 callout_reset(&poll_ch, si_pollrate, si_poll, NULL);
660                 init_finished = 1;
661         }
662 #endif
663
664         /* initial/lock device */
665         if (IS_STATE(mynor)) {
666                 return(0);
667         }
668
669         pp = sc->sc_ports + port;
670         tp = pp->sp_tty;                        /* the "real" tty */
671         dev->si_tty = tp;
672         ccbp = pp->sp_ccb;                      /* Find control block */
673         DPRINT((pp, DBG_ENTRY|DBG_OPEN, "siopen(%s,%x,%x)\n",
674                 devtoname(dev), ap->a_oflags, ap->a_devtype));
675
676         crit_enter();                   /* Keep others out */
677         error = 0;
678
679 open_top:
680         while (pp->sp_state & SS_DTR_OFF) {
681                 error = tsleep(&pp->sp_dtr_wait, PCATCH, "sidtr", 0);
682                 if (error != 0)
683                         goto out;
684         }
685
686         if (tp->t_state & TS_ISOPEN) {
687                 /*
688                  * The device is open, so everything has been initialised.
689                  * handle conflicts.
690                  */
691                 if (IS_CALLOUT(mynor)) {
692                         if (!pp->sp_active_out) {
693                                 error = EBUSY;
694                                 goto out;
695                         }
696                 } else {
697                         if (pp->sp_active_out) {
698                                 if (ap->a_oflags & O_NONBLOCK) {
699                                         error = EBUSY;
700                                         goto out;
701                                 }
702                                 error = tsleep(&pp->sp_active_out,
703                                                 PCATCH, "sibi", 0);
704                                 if (error != 0)
705                                         goto out;
706                                 goto open_top;
707                         }
708                 }
709                 if (tp->t_state & TS_XCLUDE &&
710                     priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
711                         DPRINT((pp, DBG_OPEN|DBG_FAIL,
712                                 "already open and EXCLUSIVE set\n"));
713                         error = EBUSY;
714                         goto out;
715                 }
716         } else {
717                 /*
718                  * The device isn't open, so there are no conflicts.
719                  * Initialize it. Avoid sleep... :-)
720                  */
721                 DPRINT((pp, DBG_OPEN, "first open\n"));
722                 tp->t_oproc = si_start;
723                 tp->t_stop = si_stop;
724                 tp->t_param = siparam;
725                 tp->t_dev = dev;
726                 tp->t_termios = mynor & SI_CALLOUT_MASK
727                                 ? pp->sp_iout : pp->sp_iin;
728
729                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
730
731                 ++pp->sp_wopeners;      /* in case of sleep in siparam */
732
733                 error = siparam(tp, &tp->t_termios);
734
735                 --pp->sp_wopeners;
736                 if (error != 0)
737                         goto out;
738                 /* XXX: we should goto_top if siparam slept */
739
740                 /* set initial DCD state */
741                 pp->sp_last_hi_ip = ccbp->hi_ip;
742                 if ((pp->sp_last_hi_ip & IP_DCD) || IS_CALLOUT(mynor)) {
743                         (*linesw[tp->t_line].l_modem)(tp, 1);
744                 }
745         }
746
747         /* whoops! we beat the close! */
748         if (pp->sp_state & SS_CLOSING) {
749                 /* try and stop it from proceeding to bash the hardware */
750                 pp->sp_state &= ~SS_CLOSING;
751         }
752
753         /*
754          * Wait for DCD if necessary
755          */
756         if (!(tp->t_state & TS_CARR_ON) &&
757             !IS_CALLOUT(mynor) &&
758             !(tp->t_cflag & CLOCAL) &&
759             !(ap->a_oflags & O_NONBLOCK)) {
760                 ++pp->sp_wopeners;
761                 DPRINT((pp, DBG_OPEN, "sleeping for carrier\n"));
762                 error = tsleep(TSA_CARR_ON(tp), PCATCH, "sidcd", 0);
763                 --pp->sp_wopeners;
764                 if (error != 0)
765                         goto out;
766                 goto open_top;
767         }
768
769         error = (*linesw[tp->t_line].l_open)(dev, tp);
770         si_disc_optim(tp, &tp->t_termios, pp);
771         if (tp->t_state & TS_ISOPEN && IS_CALLOUT(mynor))
772                 pp->sp_active_out = TRUE;
773
774         pp->sp_state |= SS_OPEN;        /* made it! */
775
776 out:
777         crit_exit();
778
779         DPRINT((pp, DBG_OPEN, "leaving siopen\n"));
780
781         if (!(tp->t_state & TS_ISOPEN) && pp->sp_wopeners == 0)
782                 sihardclose(pp);
783
784         return(error);
785 }
786
787 static  int
788 siclose(struct dev_close_args *ap)
789 {
790         cdev_t dev = ap->a_head.a_dev;
791         struct si_port *pp;
792         struct tty *tp;
793         int error = 0;
794         int mynor = minor(dev);
795
796         if (IS_SPECIAL(mynor))
797                 return(0);
798
799         crit_enter();
800
801         pp = MINOR2PP(mynor);
802         tp = pp->sp_tty;
803
804         DPRINT((pp, DBG_ENTRY|DBG_CLOSE, "siclose(%s,%x,%x) sp_state:%x\n",
805                 devtoname(dev), ap->a_fflag, ap->a_devtype, pp->sp_state));
806
807         /* did we sleep and loose a race? */
808         if (pp->sp_state & SS_CLOSING) {
809                 /* error = ESOMETING? */
810                 goto out;
811         }
812
813         /* begin race detection.. */
814         pp->sp_state |= SS_CLOSING;
815
816         si_write_enable(pp, 0);         /* block writes for ttywait() */
817
818         /* THIS MAY SLEEP IN TTYWAIT!!! */
819         (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
820
821         si_write_enable(pp, 1);
822
823         /* did we sleep and somebody started another open? */
824         if (!(pp->sp_state & SS_CLOSING)) {
825                 /* error = ESOMETING? */
826                 goto out;
827         }
828         /* ok. we are now still on the right track.. nuke the hardware */
829
830         if (pp->sp_state & SS_LSTART) {
831                 callout_stop(&pp->lstart_ch);
832                 pp->sp_state &= ~SS_LSTART;
833         }
834
835         si_stop(tp, FREAD | FWRITE);
836
837         sihardclose(pp);
838         ttyclose(tp);
839         pp->sp_state &= ~SS_OPEN;
840
841 out:
842         DPRINT((pp, DBG_CLOSE|DBG_EXIT, "close done, returning\n"));
843         crit_exit();
844         return(error);
845 }
846
847 static void
848 sihardclose(struct si_port *pp)
849 {
850         struct tty *tp;
851         volatile struct si_channel *ccbp;
852
853         crit_enter();
854
855         tp = pp->sp_tty;
856         ccbp = pp->sp_ccb;                      /* Find control block */
857         if (tp->t_cflag & HUPCL ||
858             (!pp->sp_active_out &&
859              !(ccbp->hi_ip & IP_DCD) &&
860              !(pp->sp_iin.c_cflag && CLOCAL)) ||
861             !(tp->t_state & TS_ISOPEN)) {
862
863                 (void) si_modem(pp, BIC, TIOCM_DTR|TIOCM_RTS);
864                 (void) si_command(pp, FCLOSE, SI_NOWAIT);
865
866                 if (pp->sp_dtr_wait != 0) {
867                         callout_reset(&pp->dtr_ch, pp->sp_dtr_wait,
868                                         sidtrwakeup, pp);
869                         pp->sp_state |= SS_DTR_OFF;
870                 }
871
872         }
873         pp->sp_active_out = FALSE;
874         wakeup((caddr_t)&pp->sp_active_out);
875         wakeup(TSA_CARR_ON(tp));
876
877         crit_exit();
878 }
879
880
881 /*
882  * called at splsoftclock()...
883  */
884 static void
885 sidtrwakeup(void *chan)
886 {
887         struct si_port *pp;
888
889         crit_enter();
890
891         pp = (struct si_port *)chan;
892         pp->sp_state &= ~SS_DTR_OFF;
893         wakeup(&pp->sp_dtr_wait);
894
895         crit_exit();
896 }
897
898 static  int
899 siwrite(struct dev_write_args *ap)
900 {
901         cdev_t dev = ap->a_head.a_dev;
902         struct si_port *pp;
903         struct tty *tp;
904         int error = 0;
905         int mynor = minor(dev);
906
907         if (IS_SPECIAL(mynor)) {
908                 DPRINT((0, DBG_ENTRY|DBG_FAIL|DBG_WRITE, "siwrite(CONTROLDEV!!)\n"));
909                 return(ENODEV);
910         }
911         pp = MINOR2PP(mynor);
912         tp = pp->sp_tty;
913         DPRINT((pp, DBG_WRITE, "siwrite(%s,%x,%x)\n", devtoname(dev), ap->a_uio, ap->a_ioflag));
914
915         crit_enter();
916         /*
917          * If writes are currently blocked, wait on the "real" tty
918          */
919         while (pp->sp_state & SS_BLOCKWRITE) {
920                 pp->sp_state |= SS_WAITWRITE;
921                 DPRINT((pp, DBG_WRITE, "in siwrite, wait for SS_BLOCKWRITE to clear\n"));
922                 if ((error = ttysleep(tp, (caddr_t)pp, PCATCH,
923                                      "siwrite", tp->t_timeout))) {
924                         if (error == EWOULDBLOCK)
925                                 error = EIO;
926                         goto out;
927                 }
928         }
929
930         error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag);
931 out:
932         crit_exit();
933         return (error);
934 }
935
936
937 static  int
938 siioctl(struct dev_ioctl_args *ap)
939 {
940         cdev_t dev = ap->a_head.a_dev;
941         caddr_t data = ap->a_data;
942         u_long cmd = ap->a_cmd;
943         struct si_port *pp;
944         struct tty *tp;
945         int error;
946         int mynor = minor(dev);
947         int blocked = 0;
948 #if defined(COMPAT_43)
949         u_long oldcmd;
950         struct termios term;
951 #endif
952
953         if (IS_SI_IOCTL(cmd))
954                 return(si_Sioctl(dev, cmd, data, ap->a_fflag, ap->a_cred));
955
956         pp = MINOR2PP(mynor);
957         tp = pp->sp_tty;
958
959         DPRINT((pp, DBG_ENTRY|DBG_IOCTL, "siioctl(%s,%lx,%x,%x)\n",
960                 devtoname(dev), cmd, data, ap->a_fflag));
961         if (IS_STATE(mynor)) {
962                 struct termios *ct;
963
964                 switch (mynor & SI_STATE_MASK) {
965                 case SI_INIT_STATE_MASK:
966                         ct = IS_CALLOUT(mynor) ? &pp->sp_iout : &pp->sp_iin;
967                         break;
968                 case SI_LOCK_STATE_MASK:
969                         ct = IS_CALLOUT(mynor) ? &pp->sp_lout : &pp->sp_lin;
970                         break;
971                 default:
972                         return (ENODEV);
973                 }
974                 switch (cmd) {
975                 case TIOCSETA:
976                         error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
977                         if (error != 0)
978                                 return (error);
979                         *ct = *(struct termios *)data;
980                         return (0);
981                 case TIOCGETA:
982                         *(struct termios *)data = *ct;
983                         return (0);
984                 case TIOCGETD:
985                         *(int *)data = TTYDISC;
986                         return (0);
987                 case TIOCGWINSZ:
988                         bzero(data, sizeof(struct winsize));
989                         return (0);
990                 default:
991                         return (ENOTTY);
992                 }
993         }
994         /*
995          * Do the old-style ioctl compat routines...
996          */
997 #if defined(COMPAT_43)
998         term = tp->t_termios;
999         oldcmd = cmd;
1000         error = ttsetcompat(tp, &cmd, data, &term);
1001         if (error != 0)
1002                 return (error);
1003         if (cmd != oldcmd)
1004                 data = (caddr_t)&term;
1005 #endif
1006         /*
1007          * Do the initial / lock state business
1008          */
1009         if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1010                 int     cc;
1011                 struct termios *dt = (struct termios *)data;
1012                 struct termios *lt = mynor & SI_CALLOUT_MASK
1013                                      ? &pp->sp_lout : &pp->sp_lin;
1014
1015                 dt->c_iflag = (tp->t_iflag & lt->c_iflag) |
1016                         (dt->c_iflag & ~lt->c_iflag);
1017                 dt->c_oflag = (tp->t_oflag & lt->c_oflag) |
1018                         (dt->c_oflag & ~lt->c_oflag);
1019                 dt->c_cflag = (tp->t_cflag & lt->c_cflag) |
1020                         (dt->c_cflag & ~lt->c_cflag);
1021                 dt->c_lflag = (tp->t_lflag & lt->c_lflag) |
1022                         (dt->c_lflag & ~lt->c_lflag);
1023                 for (cc = 0; cc < NCCS; ++cc)
1024                         if (lt->c_cc[cc] != 0)
1025                                 dt->c_cc[cc] = tp->t_cc[cc];
1026                 if (lt->c_ispeed != 0)
1027                         dt->c_ispeed = tp->t_ispeed;
1028                 if (lt->c_ospeed != 0)
1029                         dt->c_ospeed = tp->t_ospeed;
1030         }
1031
1032         /*
1033          * Block user-level writes to give the ttywait()
1034          * a chance to completely drain for commands
1035          * that require the port to be in a quiescent state.
1036          */
1037         switch (cmd) {
1038         case TIOCSETAW:
1039         case TIOCSETAF:
1040         case TIOCDRAIN:
1041 #ifdef COMPAT_43
1042         case TIOCSETP:
1043 #endif
1044                 blocked++;      /* block writes for ttywait() and siparam() */
1045                 si_write_enable(pp, 0);
1046         }
1047
1048         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data,
1049                                               ap->a_fflag, ap->a_cred);
1050         if (error != ENOIOCTL)
1051                 goto out;
1052
1053         crit_enter();
1054
1055         error = ttioctl(tp, cmd, data, ap->a_fflag);
1056         si_disc_optim(tp, &tp->t_termios, pp);
1057         if (error != ENOIOCTL) {
1058                 crit_exit();
1059                 goto out;
1060         }
1061
1062         error = 0;
1063         switch (cmd) {
1064         case TIOCSBRK:
1065                 si_command(pp, SBREAK, SI_WAIT);
1066                 break;
1067         case TIOCCBRK:
1068                 si_command(pp, EBREAK, SI_WAIT);
1069                 break;
1070         case TIOCSDTR:
1071                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1072                 break;
1073         case TIOCCDTR:
1074                 (void) si_modem(pp, SET, 0);
1075                 break;
1076         case TIOCMSET:
1077                 (void) si_modem(pp, SET, *(int *)data);
1078                 break;
1079         case TIOCMBIS:
1080                 (void) si_modem(pp, BIS, *(int *)data);
1081                 break;
1082         case TIOCMBIC:
1083                 (void) si_modem(pp, BIC, *(int *)data);
1084                 break;
1085         case TIOCMGET:
1086                 *(int *)data = si_modem(pp, GET, 0);
1087                 break;
1088         case TIOCMSDTRWAIT:
1089                 /* must be root since the wait applies to following logins */
1090                 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1091                 if (error == 0)
1092                         pp->sp_dtr_wait = *(int *)data * hz / 100;
1093                 break;
1094         case TIOCMGDTRWAIT:
1095                 *(int *)data = pp->sp_dtr_wait * 100 / hz;
1096                 break;
1097         default:
1098                 error = ENOTTY;
1099         }
1100         crit_exit();
1101
1102 out:
1103         DPRINT((pp, DBG_IOCTL|DBG_EXIT, "siioctl ret %d\n", error));
1104         if (blocked)
1105                 si_write_enable(pp, 1);
1106         return(error);
1107 }
1108
1109 /*
1110  * Handle the Specialix ioctls. All MUST be called via the CONTROL device
1111  */
1112 static int
1113 si_Sioctl(cdev_t dev, u_long cmd, caddr_t data, int flag, struct ucred *cred)
1114 {
1115         struct si_softc *xsc;
1116         struct si_port *xpp;
1117         volatile struct si_reg *regp;
1118         struct si_tcsi *dp;
1119         struct si_pstat *sps;
1120         int *ip, error = 0;
1121         int card, port;
1122         int mynor = minor(dev);
1123
1124         DPRINT((0, DBG_ENTRY|DBG_IOCTL, "si_Sioctl(%s,%lx,%x,%x)\n",
1125                 devtoname(dev), cmd, data, flag));
1126
1127 #if 1
1128         DPRINT((0, DBG_IOCTL, "TCSI_PORT=%x\n", TCSI_PORT));
1129         DPRINT((0, DBG_IOCTL, "TCSI_CCB=%x\n", TCSI_CCB));
1130         DPRINT((0, DBG_IOCTL, "TCSI_TTY=%x\n", TCSI_TTY));
1131 #endif
1132
1133         if (!IS_CONTROLDEV(mynor)) {
1134                 DPRINT((0, DBG_IOCTL|DBG_FAIL, "not called from control device!\n"));
1135                 return(ENODEV);
1136         }
1137
1138         crit_enter();   /* better safe than sorry */
1139
1140         ip = (int *)data;
1141
1142 #define SUCHECK if ((error = priv_check_cred(cred, PRIV_ROOT, 0))) goto out
1143
1144         switch (cmd) {
1145         case TCSIPORTS:
1146                 *ip = si_Nports;
1147                 goto out;
1148         case TCSIMODULES:
1149                 *ip = si_Nmodules;
1150                 goto out;
1151         case TCSISDBG_ALL:
1152                 SUCHECK;
1153                 si_debug = *ip;
1154                 goto out;
1155         case TCSIGDBG_ALL:
1156                 *ip = si_debug;
1157                 goto out;
1158         default:
1159                 /*
1160                  * Check that a controller for this port exists
1161                  */
1162
1163                 /* may also be a struct si_pstat, a superset of si_tcsi */
1164
1165                 dp = (struct si_tcsi *)data;
1166                 sps = (struct si_pstat *)data;
1167                 card = dp->tc_card;
1168                 xsc = devclass_get_softc(si_devclass, card);    /* check.. */
1169                 if (xsc == NULL || xsc->sc_type == SIEMPTY) {
1170                         error = ENOENT;
1171                         goto out;
1172                 }
1173                 /*
1174                  * And check that a port exists
1175                  */
1176                 port = dp->tc_port;
1177                 if (port < 0 || port >= xsc->sc_nport) {
1178                         error = ENOENT;
1179                         goto out;
1180                 }
1181                 xpp = xsc->sc_ports + port;
1182                 regp = (struct si_reg *)xsc->sc_maddr;
1183         }
1184
1185         switch (cmd) {
1186         case TCSIDEBUG:
1187 #ifdef  SI_DEBUG
1188                 SUCHECK;
1189                 if (xpp->sp_debug)
1190                         xpp->sp_debug = 0;
1191                 else {
1192                         xpp->sp_debug = DBG_ALL;
1193                         DPRINT((xpp, DBG_IOCTL, "debug toggled %s\n",
1194                                 (xpp->sp_debug&DBG_ALL)?"ON":"OFF"));
1195                 }
1196                 break;
1197 #else
1198                 error = ENODEV;
1199                 goto out;
1200 #endif
1201         case TCSISDBG_LEVEL:
1202         case TCSIGDBG_LEVEL:
1203 #ifdef  SI_DEBUG
1204                 if (cmd == TCSIGDBG_LEVEL) {
1205                         dp->tc_dbglvl = xpp->sp_debug;
1206                 } else {
1207                         SUCHECK;
1208                         xpp->sp_debug = dp->tc_dbglvl;
1209                 }
1210                 break;
1211 #else
1212                 error = ENODEV;
1213                 goto out;
1214 #endif
1215         case TCSIGRXIT:
1216                 dp->tc_int = regp->rx_int_count;
1217                 break;
1218         case TCSIRXIT:
1219                 SUCHECK;
1220                 regp->rx_int_count = dp->tc_int;
1221                 break;
1222         case TCSIGIT:
1223                 dp->tc_int = regp->int_count;
1224                 break;
1225         case TCSIIT:
1226                 SUCHECK;
1227                 regp->int_count = dp->tc_int;
1228                 break;
1229         case TCSISTATE:
1230                 dp->tc_int = xpp->sp_ccb->hi_ip;
1231                 break;
1232         /* these next three use a different structure */
1233         case TCSI_PORT:
1234                 SUCHECK;
1235                 si_bcopy(xpp, &sps->tc_siport, sizeof(sps->tc_siport));
1236                 break;
1237         case TCSI_CCB:
1238                 SUCHECK;
1239                 si_vbcopy(xpp->sp_ccb, &sps->tc_ccb, sizeof(sps->tc_ccb));
1240                 break;
1241         case TCSI_TTY:
1242                 SUCHECK;
1243                 si_bcopy(xpp->sp_tty, &sps->tc_tty, sizeof(sps->tc_tty));
1244                 break;
1245         default:
1246                 error = EINVAL;
1247                 goto out;
1248         }
1249 out:
1250         crit_exit();
1251         return(error);          /* success */
1252 }
1253
1254 /*
1255  *      siparam()       : Configure line params
1256  *      called at spltty();
1257  *      this may sleep, does not flush, nor wait for drain, nor block writes
1258  *      caller must arrange this if it's important..
1259  */
1260 static int
1261 siparam(struct tty *tp, struct termios *t)
1262 {
1263         struct si_port *pp = TP2PP(tp);
1264         volatile struct si_channel *ccbp;
1265         int cflag, iflag, oflag, lflag;
1266         int error = 0;          /* shutup gcc */
1267         int ispeed = 0;         /* shutup gcc */
1268         int ospeed = 0;         /* shutup gcc */
1269         BYTE val;
1270
1271         DPRINT((pp, DBG_ENTRY|DBG_PARAM, "siparam(%x,%x)\n", tp, t));
1272         cflag = t->c_cflag;
1273         iflag = t->c_iflag;
1274         oflag = t->c_oflag;
1275         lflag = t->c_lflag;
1276         DPRINT((pp, DBG_PARAM, "OFLAG 0x%x CFLAG 0x%x IFLAG 0x%x LFLAG 0x%x\n",
1277                 oflag, cflag, iflag, lflag));
1278
1279         /* XXX - if Jet host and SXDC module, use extended baud rates */
1280
1281         /* if not hung up.. */
1282         if (t->c_ospeed != 0) {
1283                 /* translate baud rate to firmware values */
1284                 ospeed = ttspeedtab(t->c_ospeed, bdrates);
1285                 ispeed = t->c_ispeed ?
1286                          ttspeedtab(t->c_ispeed, bdrates) : ospeed;
1287
1288                 /* enforce legit baud rate */
1289                 if (ospeed < 0 || ispeed < 0)
1290                         return (EINVAL);
1291         }
1292
1293         crit_enter();
1294
1295         ccbp = pp->sp_ccb;
1296
1297         /* ========== set hi_break ========== */
1298         val = 0;
1299         if (iflag & IGNBRK)             /* Breaks */
1300                 val |= BR_IGN;
1301         if (iflag & BRKINT)             /* Interrupt on break? */
1302                 val |= BR_INT;
1303         if (iflag & PARMRK)             /* Parity mark? */
1304                 val |= BR_PARMRK;
1305         if (iflag & IGNPAR)             /* Ignore chars with parity errors? */
1306                 val |= BR_PARIGN;
1307         ccbp->hi_break = val;
1308
1309         /* ========== set hi_csr ========== */
1310         /* if not hung up.. */
1311         if (t->c_ospeed != 0) {
1312                 /* Set I/O speeds */
1313                  val = (ispeed << 4) | ospeed;
1314         }
1315         ccbp->hi_csr = val;
1316
1317         /* ========== set hi_mr2 ========== */
1318         val = 0;
1319         if (cflag & CSTOPB)                             /* Stop bits */
1320                 val |= MR2_2_STOP;
1321         else
1322                 val |= MR2_1_STOP;
1323         /*
1324          * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1325          * a DCE, hence the reverse sense of RTS and CTS
1326          */
1327         /* Output Flow - RTS must be raised before data can be sent */
1328         if (cflag & CCTS_OFLOW)
1329                 val |= MR2_RTSCONT;
1330
1331         ccbp->hi_mr2 = val;
1332
1333         /* ========== set hi_mr1 ========== */
1334         val = 0;
1335         if (!(cflag & PARENB))                          /* Parity */
1336                 val |= MR1_NONE;
1337         else
1338                 val |= MR1_WITH;
1339         if (cflag & PARODD)
1340                 val |= MR1_ODD;
1341
1342         if ((cflag & CS8) == CS8) {                     /* 8 data bits? */
1343                 val |= MR1_8_BITS;
1344         } else if ((cflag & CS7) == CS7) {              /* 7 data bits? */
1345                 val |= MR1_7_BITS;
1346         } else if ((cflag & CS6) == CS6) {              /* 6 data bits? */
1347                 val |= MR1_6_BITS;
1348         } else {                                        /* Must be 5 */
1349                 val |= MR1_5_BITS;
1350         }
1351         /*
1352          * Enable H/W RTS/CTS handshaking. The default TA/MTA is
1353          * a DCE, hence the reverse sense of RTS and CTS
1354          */
1355         /* Input Flow - CTS is raised when port is ready to receive data */
1356         if (cflag & CRTS_IFLOW)
1357                 val |= MR1_CTSCONT;
1358
1359         ccbp->hi_mr1 = val;
1360
1361         /* ========== set hi_mask ========== */
1362         val = 0xff;
1363         if ((cflag & CS8) == CS8) {                     /* 8 data bits? */
1364                 val &= 0xFF;
1365         } else if ((cflag & CS7) == CS7) {              /* 7 data bits? */
1366                 val &= 0x7F;
1367         } else if ((cflag & CS6) == CS6) {              /* 6 data bits? */
1368                 val &= 0x3F;
1369         } else {                                        /* Must be 5 */
1370                 val &= 0x1F;
1371         }
1372         if (iflag & ISTRIP)
1373                 val &= 0x7F;
1374
1375         ccbp->hi_mask = val;
1376
1377         /* ========== set hi_prtcl ========== */
1378         val = SP_DCEN;          /* Monitor DCD always, or TIOCMGET misses it */
1379         if (iflag & IXANY)
1380                 val |= SP_TANY;
1381         if (iflag & IXON)
1382                 val |= SP_TXEN;
1383         if (iflag & IXOFF)
1384                 val |= SP_RXEN;
1385         if (iflag & INPCK)
1386                 val |= SP_PAEN;
1387
1388         ccbp->hi_prtcl = val;
1389
1390
1391         /* ========== set hi_{rx|tx}{on|off} ========== */
1392         /* XXX: the card TOTALLY shields us from the flow control... */
1393         ccbp->hi_txon = t->c_cc[VSTART];
1394         ccbp->hi_txoff = t->c_cc[VSTOP];
1395
1396         ccbp->hi_rxon = t->c_cc[VSTART];
1397         ccbp->hi_rxoff = t->c_cc[VSTOP];
1398
1399         /* ========== send settings to the card ========== */
1400         /* potential sleep here */
1401         if (ccbp->hi_stat == IDLE_CLOSE)                /* Not yet open */
1402                 si_command(pp, LOPEN, SI_WAIT);         /* open it */
1403         else
1404                 si_command(pp, CONFIG, SI_WAIT);        /* change params */
1405
1406         /* ========== set DTR etc ========== */
1407         /* Hangup if ospeed == 0 */
1408         if (t->c_ospeed == 0) {
1409                 (void) si_modem(pp, BIC, TIOCM_DTR|TIOCM_RTS);
1410         } else {
1411                 /*
1412                  * If the previous speed was 0, may need to re-enable
1413                  * the modem signals
1414                  */
1415                 (void) si_modem(pp, SET, TIOCM_DTR|TIOCM_RTS);
1416         }
1417
1418         DPRINT((pp, DBG_PARAM, "siparam, complete: MR1 %x MR2 %x HI_MASK %x PRTCL %x HI_BREAK %x\n",
1419                 ccbp->hi_mr1, ccbp->hi_mr2, ccbp->hi_mask, ccbp->hi_prtcl, ccbp->hi_break));
1420
1421         crit_exit();
1422         return(error);
1423 }
1424
1425 /*
1426  * Enable or Disable the writes to this channel...
1427  * "state" ->  enabled = 1; disabled = 0;
1428  */
1429 static void
1430 si_write_enable(struct si_port *pp, int state)
1431 {
1432
1433         crit_enter();
1434
1435         if (state) {
1436                 pp->sp_state &= ~SS_BLOCKWRITE;
1437                 if (pp->sp_state & SS_WAITWRITE) {
1438                         pp->sp_state &= ~SS_WAITWRITE;
1439                         /* thunder away! */
1440                         wakeup((caddr_t)pp);
1441                 }
1442         } else {
1443                 pp->sp_state |= SS_BLOCKWRITE;
1444         }
1445
1446         crit_exit();
1447 }
1448
1449 /*
1450  * Set/Get state of modem control lines.
1451  * Due to DCE-like behaviour of the adapter, some signals need translation:
1452  *      TIOCM_DTR       DSR
1453  *      TIOCM_RTS       CTS
1454  */
1455 static int
1456 si_modem(struct si_port *pp, enum si_mctl cmd, int bits)
1457 {
1458         volatile struct si_channel *ccbp;
1459         int x;
1460
1461         DPRINT((pp, DBG_ENTRY|DBG_MODEM, "si_modem(%x,%s,%x)\n", pp, si_mctl2str(cmd), bits));
1462         ccbp = pp->sp_ccb;              /* Find channel address */
1463         switch (cmd) {
1464         case GET:
1465                 x = ccbp->hi_ip;
1466                 bits = TIOCM_LE;
1467                 if (x & IP_DCD)         bits |= TIOCM_CAR;
1468                 if (x & IP_DTR)         bits |= TIOCM_DTR;
1469                 if (x & IP_RTS)         bits |= TIOCM_RTS;
1470                 if (x & IP_RI)          bits |= TIOCM_RI;
1471                 return(bits);
1472         case SET:
1473                 ccbp->hi_op &= ~(OP_DSR|OP_CTS);
1474                 /* fall through */
1475         case BIS:
1476                 x = 0;
1477                 if (bits & TIOCM_DTR)
1478                         x |= OP_DSR;
1479                 if (bits & TIOCM_RTS)
1480                         x |= OP_CTS;
1481                 ccbp->hi_op |= x;
1482                 break;
1483         case BIC:
1484                 if (bits & TIOCM_DTR)
1485                         ccbp->hi_op &= ~OP_DSR;
1486                 if (bits & TIOCM_RTS)
1487                         ccbp->hi_op &= ~OP_CTS;
1488         }
1489         return 0;
1490 }
1491
1492 /*
1493  * Handle change of modem state
1494  */
1495 static void
1496 si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip)
1497 {
1498                                                         /* if a modem dev */
1499         if (hi_ip & IP_DCD) {
1500                 if (!(pp->sp_last_hi_ip & IP_DCD)) {
1501                         DPRINT((pp, DBG_INTR, "modem carr on t_line %d\n",
1502                                 tp->t_line));
1503                         (void)(*linesw[tp->t_line].l_modem)(tp, 1);
1504                 }
1505         } else {
1506                 if (pp->sp_last_hi_ip & IP_DCD) {
1507                         DPRINT((pp, DBG_INTR, "modem carr off\n"));
1508                         if ((*linesw[tp->t_line].l_modem)(tp, 0))
1509                                 (void) si_modem(pp, SET, 0);
1510                 }
1511         }
1512         pp->sp_last_hi_ip = hi_ip;
1513
1514 }
1515
1516 /*
1517  * Poller to catch missed interrupts.
1518  *
1519  * Note that the SYSV Specialix drivers poll at 100 times per second to get
1520  * better response.  We could really use a "periodic" version timeout(). :-)
1521  */
1522 #ifdef POLL
1523 static void
1524 si_poll(void *nothing)
1525 {
1526         struct si_softc *sc;
1527         int i;
1528         volatile struct si_reg *regp;
1529         struct si_port *pp;
1530         int lost, port;
1531
1532         DPRINT((0, DBG_POLL, "si_poll()\n"));
1533         crit_enter();
1534         if (in_intr)
1535                 goto out;
1536         lost = 0;
1537         for (i = 0; i < si_numunits; i++) {
1538                 sc = devclass_get_softc(si_devclass, i);
1539                 if (sc == NULL || sc->sc_type == SIEMPTY)
1540                         continue;
1541                 regp = (struct si_reg *)sc->sc_maddr;
1542
1543                 /*
1544                  * See if there has been a pending interrupt for 2 seconds
1545                  * or so. The test (int_scounter >= 200) won't correspond
1546                  * to 2 seconds if int_count gets changed.
1547                  */
1548                 if (regp->int_pending != 0) {
1549                         if (regp->int_scounter >= 200 &&
1550                             regp->initstat == 1) {
1551                                 kprintf("si%d: lost intr\n", i);
1552                                 lost++;
1553                         }
1554                 } else {
1555                         regp->int_scounter = 0;
1556                 }
1557
1558                 /*
1559                  * gripe about no input flow control..
1560                  */
1561                 pp = sc->sc_ports;
1562                 for (port = 0; port < sc->sc_nport; pp++, port++) {
1563                         if (pp->sp_delta_overflows > 0) {
1564                                 kprintf("si%d: %d tty level buffer overflows\n",
1565                                         i, pp->sp_delta_overflows);
1566                                 pp->sp_delta_overflows = 0;
1567                         }
1568                 }
1569         }
1570         if (lost || si_realpoll)
1571                 si_intr(NULL);  /* call intr with fake vector */
1572 out:
1573         crit_exit();
1574
1575         callout_reset(&poll_ch, si_pollrate, si_poll, NULL);
1576 }
1577 #endif  /* ifdef POLL */
1578
1579 /*
1580  * The interrupt handler polls ALL ports on ALL adapters each time
1581  * it is called.
1582  */
1583
1584 static BYTE si_rxbuf[SI_BUFFERSIZE];    /* input staging area */
1585 static BYTE si_txbuf[SI_BUFFERSIZE];    /* output staging area */
1586
1587 void
1588 si_intr(void *arg)
1589 {
1590         struct si_softc *sc;
1591         struct si_port *pp;
1592         volatile struct si_channel *ccbp;
1593         struct tty *tp;
1594         volatile caddr_t maddr;
1595         BYTE op, ip;
1596         int x, card, port, n, i, isopen;
1597         volatile BYTE *z;
1598         BYTE c;
1599
1600         sc = arg;
1601
1602         DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "si_intr\n"));
1603         if (in_intr)
1604                 return;
1605         in_intr = 1;
1606
1607         /*
1608          * When we get an int we poll all the channels and do ALL pending
1609          * work, not just the first one we find. This allows all cards to
1610          * share the same vector.
1611          *
1612          * XXX - But if we're sharing the vector with something that's NOT
1613          * a SI/XIO/SX card, we may be making more work for ourselves.
1614          */
1615         for (card = 0; card < si_numunits; card++) {
1616                 sc = devclass_get_softc(si_devclass, card);
1617                 if (sc == NULL || sc->sc_type == SIEMPTY)
1618                         continue;
1619
1620                 /*
1621                  * First, clear the interrupt
1622                  */
1623                 switch(sc->sc_type) {
1624                 case SIHOST:
1625                         maddr = sc->sc_maddr;
1626                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1627                                                         /* flag nothing pending */
1628                         *(maddr+SIINTCL) = 0x00;        /* Set IRQ clear */
1629                         *(maddr+SIINTCL_CL) = 0x00;     /* Clear IRQ clear */
1630                         break;
1631                 case SIHOST2:
1632                         maddr = sc->sc_maddr;
1633                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1634                         *(maddr+SIPLIRQCLR) = 0x00;
1635                         *(maddr+SIPLIRQCLR) = 0x10;
1636                         break;
1637                 case SIPCI:
1638                         maddr = sc->sc_maddr;
1639                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1640                         *(maddr+SIPCIINTCL) = 0x0;
1641                         break;
1642                 case SIJETPCI:  /* fall through to JETISA case */
1643                 case SIJETISA:
1644                         maddr = sc->sc_maddr;
1645                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1646                         *(maddr+SIJETINTCL) = 0x0;
1647                         break;
1648                 case SIEISA:
1649                         maddr = sc->sc_maddr;
1650                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1651                         (void)inb(sc->sc_iobase + 3);
1652                         break;
1653                 case SIEMPTY:
1654                 default:
1655                         continue;
1656                 }
1657                 ((volatile struct si_reg *)maddr)->int_scounter = 0;
1658
1659                 /*
1660                  * check each port
1661                  */
1662                 for (pp = sc->sc_ports, port = 0; port < sc->sc_nport;
1663                      pp++, port++) {
1664                         ccbp = pp->sp_ccb;
1665                         tp = pp->sp_tty;
1666
1667                         /*
1668                          * See if a command has completed ?
1669                          */
1670                         if (ccbp->hi_stat != pp->sp_pend) {
1671                                 DPRINT((pp, DBG_INTR,
1672                                         "si_intr hi_stat = 0x%x, pend = %d\n",
1673                                         ccbp->hi_stat, pp->sp_pend));
1674                                 switch(pp->sp_pend) {
1675                                 case LOPEN:
1676                                 case MPEND:
1677                                 case MOPEN:
1678                                 case CONFIG:
1679                                 case SBREAK:
1680                                 case EBREAK:
1681                                         pp->sp_pend = ccbp->hi_stat;
1682                                                 /* sleeping in si_command */
1683                                         wakeup(&pp->sp_state);
1684                                         break;
1685                                 default:
1686                                         pp->sp_pend = ccbp->hi_stat;
1687                                 }
1688                         }
1689
1690                         /*
1691                          * Continue on if it's closed
1692                          */
1693                         if (ccbp->hi_stat == IDLE_CLOSE) {
1694                                 continue;
1695                         }
1696
1697                         /*
1698                          * Do modem state change if not a local device
1699                          */
1700                         si_modem_state(pp, tp, ccbp->hi_ip);
1701
1702                         /*
1703                          * Check to see if we should 'receive' characters.
1704                          */
1705                         if (tp->t_state & TS_CONNECTED &&
1706                             tp->t_state & TS_ISOPEN)
1707                                 isopen = 1;
1708                         else
1709                                 isopen = 0;
1710
1711                         /*
1712                          * Do input break processing
1713                          */
1714                         if (ccbp->hi_state & ST_BREAK) {
1715                                 if (isopen) {
1716                                     (*linesw[tp->t_line].l_rint)(TTY_BI, tp);
1717                                 }
1718                                 ccbp->hi_state &= ~ST_BREAK;   /* A Bit iffy this */
1719                                 DPRINT((pp, DBG_INTR, "si_intr break\n"));
1720                         }
1721
1722                         /*
1723                          * Do RX stuff - if not open then dump any characters.
1724                          * XXX: This is VERY messy and needs to be cleaned up.
1725                          *
1726                          * XXX: can we leave data in the host adapter buffer
1727                          * when the clists are full?  That may be dangerous
1728                          * if the user cannot get an interrupt signal through.
1729                          */
1730
1731         more_rx:        /* XXX Sorry. the nesting was driving me bats! :-( */
1732
1733                         if (!isopen) {
1734                                 ccbp->hi_rxopos = ccbp->hi_rxipos;
1735                                 goto end_rx;
1736                         }
1737
1738                         /*
1739                          * If the tty input buffers are blocked, stop emptying
1740                          * the incoming buffers and let the auto flow control
1741                          * assert..
1742                          */
1743                         if (tp->t_state & TS_TBLOCK) {
1744                                 goto end_rx;
1745                         }
1746
1747                         /*
1748                          * Process read characters if not skipped above
1749                          */
1750                         op = ccbp->hi_rxopos;
1751                         ip = ccbp->hi_rxipos;
1752                         c = ip - op;
1753                         if (c == 0) {
1754                                 goto end_rx;
1755                         }
1756
1757                         n = c & 0xff;
1758                         if (n > 250)
1759                                 n = 250;
1760
1761                         DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1762                                                 n, op, ip));
1763
1764                         /*
1765                          * Suck characters out of host card buffer into the
1766                          * "input staging buffer" - so that we dont leave the
1767                          * host card in limbo while we're possibly echoing
1768                          * characters and possibly flushing input inside the
1769                          * ldisc l_rint() routine.
1770                          */
1771                         if (n <= SI_BUFFERSIZE - op) {
1772
1773                                 DPRINT((pp, DBG_INTR, "\tsingle copy\n"));
1774                                 z = ccbp->hi_rxbuf + op;
1775                                 si_vbcopy(z, si_rxbuf, n);
1776
1777                                 op += n;
1778                         } else {
1779                                 x = SI_BUFFERSIZE - op;
1780
1781                                 DPRINT((pp, DBG_INTR, "\tdouble part 1 %d\n", x));
1782                                 z = ccbp->hi_rxbuf + op;
1783                                 si_vbcopy(z, si_rxbuf, x);
1784
1785                                 DPRINT((pp, DBG_INTR, "\tdouble part 2 %d\n",
1786                                         n - x));
1787                                 z = ccbp->hi_rxbuf;
1788                                 si_vbcopy(z, si_rxbuf + x, n - x);
1789
1790                                 op += n;
1791                         }
1792
1793                         /* clear collected characters from buffer */
1794                         ccbp->hi_rxopos = op;
1795
1796                         DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1797                                                 n, op, ip));
1798
1799                         /*
1800                          * at this point...
1801                          * n = number of chars placed in si_rxbuf
1802                          */
1803
1804                         /*
1805                          * Avoid the grotesquely inefficient lineswitch
1806                          * routine (ttyinput) in "raw" mode. It usually
1807                          * takes about 450 instructions (that's without
1808                          * canonical processing or echo!). slinput is
1809                          * reasonably fast (usually 40 instructions
1810                          * plus call overhead).
1811                          */
1812                         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1813
1814                                 /* block if the driver supports it */
1815                                 if (tp->t_rawq.c_cc + n >= SI_I_HIGH_WATER &&
1816                                     (tp->t_cflag & CRTS_IFLOW ||
1817                                      tp->t_iflag & IXOFF) &&
1818                                     !(tp->t_state & TS_TBLOCK))
1819                                         ttyblock(tp);
1820
1821                                 tk_nin += n;
1822                                 tk_rawcc += n;
1823                                 tp->t_rawcc += n;
1824
1825                                 pp->sp_delta_overflows +=
1826                                     b_to_q((char *)si_rxbuf, n, &tp->t_rawq);
1827
1828                                 ttwakeup(tp);
1829                                 if (tp->t_state & TS_TTSTOP &&
1830                                     (tp->t_iflag & IXANY ||
1831                                      tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1832                                         tp->t_state &= ~TS_TTSTOP;
1833                                         tp->t_lflag &= ~FLUSHO;
1834                                         si_start(tp);
1835                                 }
1836                         } else {
1837                                 /*
1838                                  * It'd be nice to not have to go through the
1839                                  * function call overhead for each char here.
1840                                  * It'd be nice to block input it, saving a
1841                                  * loop here and the call/return overhead.
1842                                  */
1843                                 for(x = 0; x < n; x++) {
1844                                         i = si_rxbuf[x];
1845                                         if ((*linesw[tp->t_line].l_rint)(i, tp)
1846                                              == -1) {
1847                                                 pp->sp_delta_overflows++;
1848                                         }
1849                                         /*
1850                                          * doesn't seem to be much point doing
1851                                          * this here.. this driver has no
1852                                          * softtty processing! ??
1853                                          */
1854                                         if (pp->sp_hotchar && i == pp->sp_hotchar) {
1855                                                 setsofttty();
1856                                         }
1857                                 }
1858                         }
1859                         goto more_rx;   /* try for more until RXbuf is empty */
1860
1861         end_rx:         /* XXX: Again, sorry about the gotos.. :-) */
1862
1863                         /*
1864                          * Do TX stuff
1865                          */
1866                         (*linesw[tp->t_line].l_start)(tp);
1867
1868                 } /* end of for (all ports on this controller) */
1869         } /* end of for (all controllers) */
1870
1871         in_intr = 0;
1872         DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "end si_intr\n"));
1873 }
1874
1875 /*
1876  * Nudge the transmitter...
1877  *
1878  * XXX: I inherited some funny code here.  It implies the host card only
1879  * interrupts when the transmit buffer reaches the low-water-mark, and does
1880  * not interrupt when it's actually hits empty.  In some cases, we have
1881  * processes waiting for complete drain, and we need to simulate an interrupt
1882  * about when we think the buffer is going to be empty (and retry if not).
1883  * I really am not certain about this...  I *need* the hardware manuals.
1884  */
1885 static void
1886 si_start(struct tty *tp)
1887 {
1888         struct si_port *pp;
1889         volatile struct si_channel *ccbp;
1890         struct clist *qp;
1891         BYTE ipos;
1892         int nchar;
1893         int count, n, amount, buffer_full;
1894
1895         crit_enter();
1896
1897         qp = &tp->t_outq;
1898         pp = TP2PP(tp);
1899
1900         DPRINT((pp, DBG_ENTRY|DBG_START,
1901                 "si_start(%x) t_state %x sp_state %x t_outq.c_cc %d\n",
1902                 tp, tp->t_state, pp->sp_state, qp->c_cc));
1903
1904         if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
1905                 goto out;
1906
1907         buffer_full = 0;
1908         ccbp = pp->sp_ccb;
1909
1910         count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1911         DPRINT((pp, DBG_START, "count %d\n", (BYTE)count));
1912
1913         while ((nchar = qp->c_cc) > 0) {
1914                 if ((BYTE)count >= 255) {
1915                         buffer_full++;
1916                         break;
1917                 }
1918                 amount = min(nchar, (255 - (BYTE)count));
1919                 ipos = (unsigned int)ccbp->hi_txipos;
1920                 n = q_to_b(&tp->t_outq, si_txbuf, amount);
1921                 /* will it fit in one lump? */
1922                 if ((SI_BUFFERSIZE - ipos) >= n) {
1923                         si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos], n);
1924                 } else {
1925                         si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos],
1926                                 SI_BUFFERSIZE - ipos);
1927                         si_bcopyv(si_txbuf + (SI_BUFFERSIZE - ipos),
1928                                 &ccbp->hi_txbuf[0], n - (SI_BUFFERSIZE - ipos));
1929                 }
1930                 ccbp->hi_txipos += n;
1931                 count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos;
1932         }
1933
1934         if (count != 0 && nchar == 0) {
1935                 tp->t_state |= TS_BUSY;
1936         } else {
1937                 tp->t_state &= ~TS_BUSY;
1938         }
1939
1940         /* wakeup time? */
1941         ttwwakeup(tp);
1942
1943         DPRINT((pp, DBG_START, "count %d, nchar %d, tp->t_state 0x%x\n",
1944                 (BYTE)count, nchar, tp->t_state));
1945
1946         if (tp->t_state & TS_BUSY)
1947         {
1948                 int time;
1949
1950                 time = ttspeedtab(tp->t_ospeed, chartimes);
1951
1952                 if (time > 0) {
1953                         if (time < nchar)
1954                                 time = nchar / time;
1955                         else
1956                                 time = 2;
1957                 } else {
1958                         DPRINT((pp, DBG_START,
1959                                 "bad char time value! %d\n", time));
1960                         time = hz/10;
1961                 }
1962
1963                 if ((pp->sp_state & (SS_LSTART|SS_INLSTART)) == SS_LSTART) {
1964                         callout_stop(&pp->lstart_ch);
1965                 } else {
1966                         pp->sp_state |= SS_LSTART;
1967                 }
1968                 DPRINT((pp, DBG_START, "arming lstart, time=%d\n", time));
1969                 callout_reset(&pp->lstart_ch, time, si_lstart, (void *)pp);
1970         }
1971
1972 out:
1973         crit_exit();
1974         DPRINT((pp, DBG_EXIT|DBG_START, "leave si_start()\n"));
1975 }
1976
1977 /*
1978  * Note: called at splsoftclock from the timeout code
1979  * This has to deal with two things...  cause wakeups while waiting for
1980  * tty drains on last process exit, and call l_start at about the right
1981  * time for protocols like ppp.
1982  */
1983 static void
1984 si_lstart(void *arg)
1985 {
1986         struct si_port *pp = arg;
1987         struct tty *tp;
1988
1989         DPRINT((pp, DBG_ENTRY|DBG_LSTART, "si_lstart(%x) sp_state %x\n",
1990                 pp, pp->sp_state));
1991
1992         crit_enter();
1993
1994         if ((pp->sp_state & SS_OPEN) == 0 || (pp->sp_state & SS_LSTART) == 0) {
1995                 crit_exit();
1996                 return;
1997         }
1998         pp->sp_state &= ~SS_LSTART;
1999         pp->sp_state |= SS_INLSTART;
2000
2001         tp = pp->sp_tty;
2002
2003         /* deal with the process exit case */
2004         ttwwakeup(tp);
2005
2006         /* nudge protocols - eg: ppp */
2007         (*linesw[tp->t_line].l_start)(tp);
2008
2009         pp->sp_state &= ~SS_INLSTART;
2010         crit_exit();
2011 }
2012
2013 /*
2014  * Stop output on a line. called at spltty();
2015  */
2016 void
2017 si_stop(struct tty *tp, int rw)
2018 {
2019         volatile struct si_channel *ccbp;
2020         struct si_port *pp;
2021
2022         pp = TP2PP(tp);
2023         ccbp = pp->sp_ccb;
2024
2025         DPRINT((TP2PP(tp), DBG_ENTRY|DBG_STOP, "si_stop(%x,%x)\n", tp, rw));
2026
2027         /* XXX: must check (rw & FWRITE | FREAD) etc flushing... */
2028         if (rw & FWRITE) {
2029                 /* what level are we meant to be flushing anyway? */
2030                 if (tp->t_state & TS_BUSY) {
2031                         si_command(TP2PP(tp), WFLUSH, SI_NOWAIT);
2032                         tp->t_state &= ~TS_BUSY;
2033                         ttwwakeup(tp);  /* Bruce???? */
2034                 }
2035         }
2036 #if 1   /* XXX: this doesn't work right yet.. */
2037         /* XXX: this may have been failing because we used to call l_rint()
2038          * while we were looping based on these two counters. Now, we collect
2039          * the data and then loop stuffing it into l_rint(), making this
2040          * useless.  Should we cause this to blow away the staging buffer?
2041          */
2042         if (rw & FREAD) {
2043                 ccbp->hi_rxopos = ccbp->hi_rxipos;
2044         }
2045 #endif
2046 }
2047
2048 /*
2049  * Issue a command to the host card CPU.
2050  */
2051
2052 static void
2053 si_command(struct si_port *pp, int cmd, int waitflag)
2054 {
2055         volatile struct si_channel *ccbp = pp->sp_ccb;
2056         int x;
2057
2058         DPRINT((pp, DBG_ENTRY|DBG_PARAM, "si_command(%x,%x,%d): hi_stat 0x%x\n",
2059                 pp, cmd, waitflag, ccbp->hi_stat));
2060
2061         crit_enter();           /* Keep others out */
2062
2063         /* wait until it's finished what it was doing.. */
2064         /* XXX: sits in IDLE_BREAK until something disturbs it or break
2065          * is turned off. */
2066         while((x = ccbp->hi_stat) != IDLE_OPEN &&
2067                         x != IDLE_CLOSE &&
2068                         x != IDLE_BREAK &&
2069                         x != cmd) {
2070                 if (in_intr) {                  /* Prevent sleep in intr */
2071                         DPRINT((pp, DBG_PARAM,
2072                                 "cmd intr collision - completing %d\trequested %d\n",
2073                                 x, cmd));
2074                         crit_exit();
2075                         return;
2076                 } else if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, PCATCH,
2077                                 "sicmd1", 1)) {
2078                         crit_exit();
2079                         return;
2080                 }
2081         }
2082         /* it should now be in IDLE_{OPEN|CLOSE|BREAK}, or "cmd" */
2083
2084         /* if there was a pending command, cause a state-change wakeup */
2085         switch(pp->sp_pend) {
2086         case LOPEN:
2087         case MPEND:
2088         case MOPEN:
2089         case CONFIG:
2090         case SBREAK:
2091         case EBREAK:
2092                 wakeup(&pp->sp_state);
2093                 break;
2094         default:
2095                 break;
2096         }
2097
2098         pp->sp_pend = cmd;              /* New command pending */
2099         ccbp->hi_stat = cmd;            /* Post it */
2100
2101         if (waitflag) {
2102                 if (in_intr) {          /* If in interrupt handler */
2103                         DPRINT((pp, DBG_PARAM,
2104                                 "attempt to sleep in si_intr - cmd req %d\n",
2105                                 cmd));
2106                         crit_exit();
2107                         return;
2108                 } else while(ccbp->hi_stat != IDLE_OPEN &&
2109                              ccbp->hi_stat != IDLE_BREAK) {
2110                         if (ttysleep(pp->sp_tty, (caddr_t)&pp->sp_state, PCATCH,
2111                             "sicmd2", 0))
2112                                 break;
2113                 }
2114         }
2115         crit_exit();
2116 }
2117
2118 static void
2119 si_disc_optim(struct tty *tp, struct termios *t, struct si_port *pp)
2120 {
2121         /*
2122          * XXX can skip a lot more cases if Smarts.  Maybe
2123          * (IGNCR | ISTRIP | IXON) in c_iflag.  But perhaps we
2124          * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2125          */
2126         if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) &&
2127             (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) &&
2128             (!(t->c_iflag & PARMRK) ||
2129              (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) &&
2130             !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) &&
2131             linesw[tp->t_line].l_rint == ttyinput)
2132                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2133         else
2134                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2135         pp->sp_hotchar = linesw[tp->t_line].l_hotchar;
2136         DPRINT((pp, DBG_OPTIM, "bypass: %s, hotchar: %x\n",
2137                 (tp->t_state & TS_CAN_BYPASS_L_RINT) ? "on" : "off",
2138                 pp->sp_hotchar));
2139 }
2140
2141
2142 #ifdef  SI_DEBUG
2143
2144 void
2145 si_dprintf(struct si_port *pp, int flags, const char *fmt, ...)
2146 {
2147         __va_list ap;
2148
2149         if ((pp == NULL && (si_debug&flags)) ||
2150             (pp != NULL && ((pp->sp_debug&flags) || (si_debug&flags)))) {
2151                 if (pp != NULL)
2152                         kprintf("%ci%d(%d): ", 's',
2153                                 (int)SI_CARD(minor(pp->sp_tty->t_dev)),
2154                                 (int)SI_PORT(minor(pp->sp_tty->t_dev)));
2155                 __va_start(ap, fmt);
2156                 kvprintf(fmt, ap);
2157                 __va_end(ap);
2158         }
2159 }
2160
2161 static char *
2162 si_mctl2str(enum si_mctl cmd)
2163 {
2164         switch (cmd) {
2165         case GET:
2166                 return("GET");
2167         case SET:
2168                 return("SET");
2169         case BIS:
2170                 return("BIS");
2171         case BIC:
2172                 return("BIC");
2173         }
2174         return("BAD");
2175 }
2176
2177 #endif  /* DEBUG */
2178
2179 static char *
2180 si_modulename(int host_type, int uart_type)
2181 {
2182         switch (host_type) {
2183         /* Z280 based cards */
2184         case SIEISA:
2185         case SIHOST2:
2186         case SIHOST:
2187         case SIPCI:
2188                 switch (uart_type) {
2189                 case 0:
2190                         return(" (XIO)");
2191                 case 1:
2192                         return(" (SI)");
2193                 }
2194                 break;
2195         /* T225 based hosts */
2196         case SIJETPCI:
2197         case SIJETISA:
2198                 switch (uart_type) {
2199                 case 0:
2200                         return(" (SI)");
2201                 case 40:
2202                         return(" (XIO)");
2203                 case 72:
2204                         return(" (SXDC)");
2205                 }
2206                 break;
2207         }
2208         return("");
2209 }