]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/stallion.c
This commit was generated by cvs2svn to compensate for changes in r73393,
[FreeBSD/FreeBSD.git] / sys / i386 / isa / stallion.c
1 /*****************************************************************************/
2
3 /*
4  * stallion.c  -- stallion multiport serial driver.
5  *
6  * Copyright (c) 1995-1996 Greg Ungerer (gerg@stallion.oz.au).
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Greg Ungerer.
20  * 4. Neither the name of the author nor the names of any co-contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD$
37  */
38
39 /*****************************************************************************/
40
41 #define TTYDEFCHARS     1
42
43 #include "opt_compat.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/tty.h>
50 #include <sys/conf.h>
51 #include <sys/fcntl.h>
52 #include <sys/bus.h>
53 #include <i386/isa/isa_device.h>
54 #include <i386/isa/ic/scd1400.h>
55 #include <machine/comstats.h>
56
57 #include "pci.h"
58 #ifdef COMPILING_LINT
59 #warning "The stallion pci device is broken and not compiled with LINT"
60 #undef NPCI
61 #define NPCI 0
62 #endif
63 #if NPCI > 0
64 #ifndef COMPAT_OLDPCI
65 #error "The stallion pci driver requires the old pci compatibility shims"
66 #endif
67 #include <pci/pcivar.h>
68 #include <pci/pcireg.h>
69 #endif
70
71 /*****************************************************************************/
72
73 /*
74  *      Define the version level of the kernel - so we can compile in the
75  *      appropriate bits of code. By default this will compile for a 2.1
76  *      level kernel.
77  */
78 #define VFREEBSD        220
79
80 #if VFREEBSD >= 220
81 #define STATIC          static
82 #else
83 #define STATIC
84 #endif
85
86 /*****************************************************************************/
87
88 /*
89  *      Define different board types. At the moment I have only declared
90  *      those boards that this driver supports. But I will use the standard
91  *      "assigned" board numbers. In the future this driver will support
92  *      some of the other Stallion boards. Currently supported boards are
93  *      abbreviated as EIO = EasyIO and ECH = EasyConnection 8/32.
94  */
95 #define BRD_EASYIO      20
96 #define BRD_ECH         21
97 #define BRD_ECHMC       22
98 #define BRD_ECHPCI      26
99
100 /*
101  *      When using the BSD "config" stuff there is no easy way to specifiy
102  *      a secondary IO address region. So it is hard wired here. Also the
103  *      shared interrupt information is hard wired here...
104  */
105 static unsigned int     stl_ioshared = 0x280;
106 static unsigned int     stl_irqshared = 0;
107
108 /*****************************************************************************/
109
110 /*
111  *      Define important driver limitations.
112  */
113 #define STL_MAXBRDS             8
114 #define STL_MAXPANELS           4
115 #define STL_PORTSPERPANEL       16
116 #define STL_PORTSPERBRD         64
117
118 /*
119  *      Define the important minor number break down bits. These have been
120  *      chosen to be "compatible" with the standard sio driver minor numbers.
121  *      Extra high bits are used to distinguish between boards.
122  */
123 #define STL_CALLOUTDEV          0x80
124 #define STL_CTRLLOCK            0x40
125 #define STL_CTRLINIT            0x20
126 #define STL_CTRLDEV             (STL_CTRLLOCK | STL_CTRLINIT)
127
128 #define STL_MEMDEV      0x07000000
129
130 #define STL_DEFSPEED    TTYDEF_SPEED
131 #define STL_DEFCFLAG    (CS8 | CREAD | HUPCL)
132
133 /*
134  *      I haven't really decided (or measured) what buffer sizes give
135  *      a good balance between performance and memory usage. These seem
136  *      to work pretty well...
137  */
138 #define STL_RXBUFSIZE           2048
139 #define STL_TXBUFSIZE           2048
140
141 #define STL_TXBUFLOW            (STL_TXBUFSIZE / 4)
142 #define STL_RXBUFHIGH           (3 * STL_RXBUFSIZE / 4)
143
144 /*****************************************************************************/
145
146 /*
147  *      Define our local driver identity first. Set up stuff to deal with
148  *      all the local structures required by a serial tty driver.
149  */
150 static const char       stl_drvname[] = "stl";
151 static const char       stl_longdrvname[] = "Stallion Multiport Serial Driver";
152 static const char       stl_drvversion[] = "1.0.0";
153 static int              stl_brdprobed[STL_MAXBRDS];
154
155 static int              stl_nrbrds = 0;
156 static int              stl_doingtimeout = 0;
157
158 static const char       __file__[] = /*__FILE__*/ "stallion.c";
159
160 /*
161  *      Define global stats structures. Not used often, and can be
162  *      re-used for each stats call.
163  */
164 static combrd_t         stl_brdstats;
165 static comstats_t       stl_comstats;
166
167 /*****************************************************************************/
168
169 /*
170  *      Define a set of structures to hold all the board/panel/port info
171  *      for our ports. These will be dynamically allocated as required.
172  */
173
174 /*
175  *      Define a ring queue structure for each port. This will hold the
176  *      TX data waiting to be output. Characters are fed into this buffer
177  *      from the line discipline (or even direct from user space!) and
178  *      then fed into the UARTs during interrupts. Will use a clasic ring
179  *      queue here for this. The good thing about this type of ring queue
180  *      is that the head and tail pointers can be updated without interrupt
181  *      protection - since "write" code only needs to change the head, and
182  *      interrupt code only needs to change the tail.
183  */
184 typedef struct {
185         char    *buf;
186         char    *endbuf;
187         char    *head;
188         char    *tail;
189 } stlrq_t;
190
191 /*
192  *      Port, panel and board structures to hold status info about each.
193  *      The board structure contains pointers to structures for each panel
194  *      connected to it, and in turn each panel structure contains pointers
195  *      for each port structure for each port on that panel. Note that
196  *      the port structure also contains the board and panel number that it
197  *      is associated with, this makes it (fairly) easy to get back to the
198  *      board/panel info for a port. Also note that the tty struct is at
199  *      the top of the structure, this is important, since the code uses
200  *      this fact to get the port struct pointer from the tty struct
201  *      pointer!
202  */
203 typedef struct {
204         struct tty      tty;
205         int             portnr;
206         int             panelnr;
207         int             brdnr;
208         int             ioaddr;
209         int             uartaddr;
210         int             pagenr;
211         int             callout;
212         int             brklen;
213         int             dtrwait;
214         int             dotimestamp;
215         int             waitopens;
216         int             hotchar;
217         unsigned int    state;
218         unsigned int    hwid;
219         unsigned int    sigs;
220         unsigned int    rxignoremsk;
221         unsigned int    rxmarkmsk;
222         unsigned long   clk;
223         struct termios  initintios;
224         struct termios  initouttios;
225         struct termios  lockintios;
226         struct termios  lockouttios;
227         struct timeval  timestamp;
228         comstats_t      stats;
229         stlrq_t         tx;
230         stlrq_t         rx;
231         stlrq_t         rxstatus;
232 } stlport_t;
233
234 typedef struct {
235         int             panelnr;
236         int             brdnr;
237         int             pagenr;
238         int             nrports;
239         int             iobase;
240         unsigned int    hwid;
241         unsigned int    ackmask;
242         stlport_t       *ports[STL_PORTSPERPANEL];
243 } stlpanel_t;
244
245 typedef struct {
246         int             brdnr;
247         int             brdtype;
248         int             unitid;
249         int             state;
250         int             nrpanels;
251         int             nrports;
252         int             irq;
253         int             irqtype;
254         unsigned int    ioaddr1;
255         unsigned int    ioaddr2;
256         unsigned int    iostatus;
257         unsigned int    ioctrl;
258         unsigned int    ioctrlval;
259         unsigned int    hwid;
260         unsigned long   clk;
261         stlpanel_t      *panels[STL_MAXPANELS];
262         stlport_t       *ports[STL_PORTSPERBRD];
263 } stlbrd_t;
264
265 static stlbrd_t         *stl_brds[STL_MAXBRDS];
266
267 /*
268  *      Per board state flags. Used with the state field of the board struct.
269  *      Not really much here yet!
270  */
271 #define BRD_FOUND       0x1
272
273 /*
274  *      Define the port structure state flags. These set of flags are
275  *      modified at interrupt time - so setting and reseting them needs
276  *      to be atomic.
277  */
278 #define ASY_TXLOW       0x1
279 #define ASY_RXDATA      0x2
280 #define ASY_DCDCHANGE   0x4
281 #define ASY_DTRWAIT     0x8
282 #define ASY_RTSFLOW     0x10
283 #define ASY_RTSFLOWMODE 0x20
284 #define ASY_CTSFLOWMODE 0x40
285
286 #define ASY_ACTIVE      (ASY_TXLOW | ASY_RXDATA | ASY_DCDCHANGE)
287
288 /*
289  *      Define an array of board names as printable strings. Handy for
290  *      referencing boards when printing trace and stuff.
291  */
292 static char     *stl_brdnames[] = {
293         (char *) NULL,
294         (char *) NULL,
295         (char *) NULL,
296         (char *) NULL,
297         (char *) NULL,
298         (char *) NULL,
299         (char *) NULL,
300         (char *) NULL,
301         (char *) NULL,
302         (char *) NULL,
303         (char *) NULL,
304         (char *) NULL,
305         (char *) NULL,
306         (char *) NULL,
307         (char *) NULL,
308         (char *) NULL,
309         (char *) NULL,
310         (char *) NULL,
311         (char *) NULL,
312         (char *) NULL,
313         "EasyIO",
314         "EC8/32-AT",
315         "EC8/32-MC",
316         (char *) NULL,
317         (char *) NULL,
318         (char *) NULL,
319         "EC8/32-PCI",
320 };
321
322 /*****************************************************************************/
323
324 /*
325  *      Hardware ID bits for the EasyIO and ECH boards. These defines apply
326  *      to the directly accessible io ports of these boards (not the cd1400
327  *      uarts - they are in scd1400.h).
328  */
329 #define EIO_8PORTRS     0x04
330 #define EIO_4PORTRS     0x05
331 #define EIO_8PORTDI     0x00
332 #define EIO_8PORTM      0x06
333 #define EIO_IDBITMASK   0x07
334 #define EIO_INTRPEND    0x08
335 #define EIO_INTEDGE     0x00
336 #define EIO_INTLEVEL    0x08
337
338 #define ECH_ID          0xa0
339 #define ECH_IDBITMASK   0xe0
340 #define ECH_BRDENABLE   0x08
341 #define ECH_BRDDISABLE  0x00
342 #define ECH_INTENABLE   0x01
343 #define ECH_INTDISABLE  0x00
344 #define ECH_INTLEVEL    0x02
345 #define ECH_INTEDGE     0x00
346 #define ECH_INTRPEND    0x01
347 #define ECH_BRDRESET    0x01
348
349 #define ECHMC_INTENABLE 0x01
350 #define ECHMC_BRDRESET  0x02
351
352 #define ECH_PNLSTATUS   2
353 #define ECH_PNL16PORT   0x20
354 #define ECH_PNLIDMASK   0x07
355 #define ECH_PNLINTRPEND 0x80
356 #define ECH_ADDR2MASK   0x1e0
357
358 #define EIO_CLK         25000000
359 #define EIO_CLK8M       20000000
360 #define ECH_CLK         EIO_CLK
361
362 /*
363  *      Define the offsets within the register bank for all io registers.
364  *      These io address offsets are common to both the EIO and ECH.
365  */
366 #define EREG_ADDR       0
367 #define EREG_DATA       4
368 #define EREG_RXACK      5
369 #define EREG_TXACK      6
370 #define EREG_MDACK      7
371
372 #define EREG_BANKSIZE   8
373
374 /*
375  *      Define the PCI vendor and device id for ECH8/32-PCI.
376  */
377 #define STL_PCIDEVID    0xd001100b
378
379 /*
380  *      Define the vector mapping bits for the programmable interrupt board
381  *      hardware. These bits encode the interrupt for the board to use - it
382  *      is software selectable (except the EIO-8M).
383  */
384 static unsigned char    stl_vecmap[] = {
385         0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
386         0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
387 };
388
389 /*
390  *      Set up enable and disable macros for the ECH boards. They require
391  *      the secondary io address space to be activated and deactivated.
392  *      This way all ECH boards can share their secondary io region.
393  *      If this is an ECH-PCI board then also need to set the page pointer
394  *      to point to the correct page.
395  */
396 #define BRDENABLE(brdnr,pagenr)                                         \
397         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
398                 outb(stl_brds[(brdnr)]->ioctrl,                         \
399                         (stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE));\
400         else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI)              \
401                 outb(stl_brds[(brdnr)]->ioctrl, (pagenr));
402
403 #define BRDDISABLE(brdnr)                                               \
404         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
405                 outb(stl_brds[(brdnr)]->ioctrl,                         \
406                         (stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE));
407
408 /*
409  *      Define the cd1400 baud rate clocks. These are used when calculating
410  *      what clock and divisor to use for the required baud rate. Also
411  *      define the maximum baud rate allowed, and the default base baud.
412  */
413 static int      stl_cd1400clkdivs[] = {
414         CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
415 };
416
417 #define STL_MAXBAUD     230400
418
419 /*****************************************************************************/
420
421 /*
422  *      Define macros to extract a brd and port number from a minor number.
423  *      This uses the extended minor number range in the upper 2 bytes of
424  *      the device number. This gives us plenty of minor numbers to play
425  *      with...
426  */
427 #define MKDEV2BRD(m)    ((minor(m) & 0x00700000) >> 20)
428 #define MKDEV2PORT(m)   ((minor(m) & 0x1f) | ((minor(m) & 0x00010000) >> 11))
429
430 /*
431  *      Define some handy local macros...
432  */
433 #ifndef MIN
434 #define MIN(a,b)        (((a) <= (b)) ? (a) : (b))
435 #endif
436
437 /*****************************************************************************/
438
439 /*
440  *      Declare all those functions in this driver!  First up is the set of
441  *      externally visible functions.
442  */
443
444 static int      stlprobe(struct isa_device *idp);
445 static int      stlattach(struct isa_device *idp);
446
447 STATIC  d_open_t        stlopen;
448 STATIC  d_close_t       stlclose;
449 STATIC  d_ioctl_t       stlioctl;
450
451 /*
452  *      Internal function prototypes.
453  */
454 static stlport_t *stl_dev2port(dev_t dev);
455 static int      stl_findfreeunit(void);
456 static int      stl_rawopen(stlport_t *portp);
457 static int      stl_rawclose(stlport_t *portp);
458 static int      stl_param(struct tty *tp, struct termios *tiosp);
459 static void     stl_start(struct tty *tp);
460 static void     stl_stop(struct tty *tp, int);
461 static void     stl_ttyoptim(stlport_t *portp, struct termios *tiosp);
462 static void     stl_dotimeout(void);
463 static void     stl_poll(void *arg);
464 static void     stl_rxprocess(stlport_t *portp);
465 static void     stl_dtrwakeup(void *arg);
466 static int      stl_brdinit(stlbrd_t *brdp);
467 static int      stl_initeio(stlbrd_t *brdp);
468 static int      stl_initech(stlbrd_t *brdp);
469 static int      stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
470 static ointhand2_t      stlintr;
471 static __inline void    stl_txisr(stlpanel_t *panelp, int ioaddr);
472 static __inline void    stl_rxisr(stlpanel_t *panelp, int ioaddr);
473 static __inline void    stl_mdmisr(stlpanel_t *panelp, int ioaddr);
474 static void     stl_setreg(stlport_t *portp, int regnr, int value);
475 static int      stl_getreg(stlport_t *portp, int regnr);
476 static int      stl_updatereg(stlport_t *portp, int regnr, int value);
477 static int      stl_getsignals(stlport_t *portp);
478 static void     stl_setsignals(stlport_t *portp, int dtr, int rts);
479 static void     stl_flowcontrol(stlport_t *portp, int hw, int sw);
480 static void     stl_ccrwait(stlport_t *portp);
481 static void     stl_enablerxtx(stlport_t *portp, int rx, int tx);
482 static void     stl_startrxtx(stlport_t *portp, int rx, int tx);
483 static void     stl_disableintrs(stlport_t *portp);
484 static void     stl_sendbreak(stlport_t *portp, long len);
485 static void     stl_flush(stlport_t *portp, int flag);
486 static int      stl_memioctl(dev_t dev, unsigned long cmd, caddr_t data,
487                         int flag, struct proc *p);
488 static int      stl_getbrdstats(caddr_t data);
489 static int      stl_getportstats(stlport_t *portp, caddr_t data);
490 static int      stl_clrportstats(stlport_t *portp, caddr_t data);
491 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
492
493 #if NPCI > 0
494 static const char *stlpciprobe(pcici_t tag, pcidi_t type);
495 static void     stlpciattach(pcici_t tag, int unit);
496 static void     stlpciintr(void * arg);
497 #endif
498
499 /*****************************************************************************/
500
501 /*
502  *      Declare the driver isa structure.
503  */
504 struct isa_driver       stldriver = {
505         INTR_TYPE_TTY,
506         stlprobe,
507         stlattach,
508         "stl"
509 };
510 COMPAT_ISA_DRIVER(stl, stldriver);
511
512 /*****************************************************************************/
513
514 #if NPCI > 0
515
516 /*
517  *      Declare the driver pci structure.
518  */
519 static unsigned long    stl_count;
520
521 static struct pci_device        stlpcidriver = {
522         "stl",
523         stlpciprobe,
524         stlpciattach,
525         &stl_count,
526         NULL,
527 };
528
529 COMPAT_PCI_DRIVER (stlpci, stlpcidriver);
530
531 #endif
532
533 /*****************************************************************************/
534
535 #if VFREEBSD >= 220
536
537 /*
538  *      FreeBSD-2.2+ kernel linkage.
539  */
540
541 #define CDEV_MAJOR      72
542 static struct cdevsw stl_cdevsw = {
543         /* open */      stlopen,
544         /* close */     stlclose,
545         /* read */      ttyread,
546         /* write */     ttywrite,
547         /* ioctl */     stlioctl,
548         /* poll */      ttypoll,
549         /* mmap */      nommap,
550         /* strategy */  nostrategy,
551         /* name */      "stl",
552         /* maj */       CDEV_MAJOR,
553         /* dump */      nodump,
554         /* psize */     nopsize,
555         /* flags */     D_TTY | D_KQFILTER,
556         /* bmaj */      -1,
557         /* kqfilter */  ttykqfilter,
558 };
559
560 static void stl_drvinit(void *unused)
561 {
562
563         cdevsw_add(&stl_cdevsw);
564 }
565
566 SYSINIT(sidev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,stl_drvinit,NULL)
567
568 #endif
569
570 /*****************************************************************************/
571
572 /*
573  *      Probe for some type of EasyIO or EasyConnection 8/32 board at
574  *      the supplied address. All we do is check if we can find the
575  *      board ID for the board... (Note, PCI boards not checked here,
576  *      they are done in the stlpciprobe() routine).
577  */
578
579 static int stlprobe(struct isa_device *idp)
580 {
581         unsigned int    status;
582
583 #if DEBUG
584         printf("stlprobe(idp=%x): unit=%d iobase=%x\n", (int) idp,
585                 idp->id_unit, idp->id_iobase);
586 #endif
587
588         if (idp->id_unit > STL_MAXBRDS)
589                 return(0);
590
591         status = inb(idp->id_iobase + 1);
592         if ((status & ECH_IDBITMASK) == ECH_ID) {
593                 stl_brdprobed[idp->id_unit] = BRD_ECH;
594                 return(1);
595         }
596
597         status = inb(idp->id_iobase + 2);
598         switch (status & EIO_IDBITMASK) {
599         case EIO_8PORTRS:
600         case EIO_8PORTM:
601         case EIO_8PORTDI:
602         case EIO_4PORTRS:
603                 stl_brdprobed[idp->id_unit] = BRD_EASYIO;
604                 return(1);
605         default:
606                 break;
607         }
608         
609         return(0);
610 }
611
612 /*****************************************************************************/
613
614 /*
615  *      Find an available internal board number (unit number). The problem
616  *      is that the same unit numbers can be assigned to different boards
617  *      detected during the ISA and PCI initialization phases.
618  */
619
620 static int stl_findfreeunit()
621 {
622         int     i;
623
624         for (i = 0; (i < STL_MAXBRDS); i++)
625                 if (stl_brds[i] == (stlbrd_t *) NULL)
626                         break;
627         return((i >= STL_MAXBRDS) ? -1 : i);
628 }
629
630 /*****************************************************************************/
631
632 /*
633  *      Allocate resources for and initialize the specified board.
634  */
635
636 static int stlattach(struct isa_device *idp)
637 {
638         stlbrd_t        *brdp;
639
640 #if DEBUG
641         printf("stlattach(idp=%p): unit=%d iobase=%x\n", (void *) idp,
642                 idp->id_unit, idp->id_iobase);
643 #endif
644
645         idp->id_ointr = stlintr;
646
647         brdp = (stlbrd_t *) malloc(sizeof(stlbrd_t), M_TTYS, M_NOWAIT | M_ZERO);
648         if (brdp == (stlbrd_t *) NULL) {
649                 printf("STALLION: failed to allocate memory (size=%d)\n",
650                         sizeof(stlbrd_t));
651                 return(0);
652         }
653
654         if ((brdp->brdnr = stl_findfreeunit()) < 0) {
655                 printf("STALLION: too many boards found, max=%d\n",
656                         STL_MAXBRDS);
657                 return(0);
658         }
659         if (brdp->brdnr >= stl_nrbrds)
660                 stl_nrbrds = brdp->brdnr + 1;
661
662         brdp->unitid = idp->id_unit;
663         brdp->brdtype = stl_brdprobed[idp->id_unit];
664         brdp->ioaddr1 = idp->id_iobase;
665         brdp->ioaddr2 = stl_ioshared;
666         brdp->irq = ffs(idp->id_irq) - 1;
667         brdp->irqtype = stl_irqshared;
668         stl_brdinit(brdp);
669
670         return(1);
671 }
672
673 /*****************************************************************************/
674
675 #if NPCI > 0
676
677 /*
678  *      Probe specifically for the PCI boards. We need to be a little
679  *      carefull here, since it looks sort like a Nat Semi IDE chip...
680  */
681
682 static const char *stlpciprobe(pcici_t tag, pcidi_t type)
683 {
684         unsigned long   class;
685
686 #if DEBUG
687         printf("stlpciprobe(tag=%x,type=%x)\n", (int) &tag, (int) type);
688 #endif
689
690         switch (type) {
691         case STL_PCIDEVID:
692                 break;
693         default:
694                 return((char *) NULL);
695         }
696
697         class = pci_conf_read(tag, PCI_CLASS_REG);
698         if ((class & PCI_CLASS_MASK) == PCI_CLASS_MASS_STORAGE)
699                 return((char *) NULL);
700
701         return("Stallion EasyConnection 8/32-PCI");
702 }
703
704 /*****************************************************************************/
705
706 /*
707  *      Allocate resources for and initialize the specified PCI board.
708  */
709
710 void stlpciattach(pcici_t tag, int unit)
711 {
712         stlbrd_t        *brdp;
713
714 #if DEBUG
715         printf("stlpciattach(tag=%x,unit=%x)\n", (int) &tag, unit);
716 #endif
717
718         brdp = (stlbrd_t *) malloc(sizeof(stlbrd_t), M_TTYS, M_NOWAIT | M_ZERO);
719         if (brdp == (stlbrd_t *) NULL) {
720                 printf("STALLION: failed to allocate memory (size=%d)\n",
721                         sizeof(stlbrd_t));
722                 return;
723         }
724
725         if ((unit < 0) || (unit > STL_MAXBRDS)) {
726                 printf("STALLION: bad PCI board unit number=%d\n", unit);
727                 return;
728         }
729
730 /*
731  *      Allocate us a new driver unique unit number.
732  */
733         if ((brdp->brdnr = stl_findfreeunit()) < 0) {
734                 printf("STALLION: too many boards found, max=%d\n",
735                         STL_MAXBRDS);
736                 return;
737         }
738         if (brdp->brdnr >= stl_nrbrds)
739                 stl_nrbrds = brdp->brdnr + 1;
740
741         brdp->unitid = 0;
742         brdp->brdtype = BRD_ECHPCI;
743         brdp->ioaddr1 = ((unsigned int) pci_conf_read(tag, 0x14)) & 0xfffc;
744         brdp->ioaddr2 = ((unsigned int) pci_conf_read(tag, 0x10)) & 0xfffc;
745         brdp->irq = ((int) pci_conf_read(tag, 0x3c)) & 0xff;
746         brdp->irqtype = 0;
747         if (pci_map_int(tag, stlpciintr, (void *) NULL, &tty_imask) == 0) {
748                 printf("STALLION: failed to map interrupt irq=%d for unit=%d\n",
749                         brdp->irq, brdp->brdnr);
750                 return;
751         }
752
753 #if 0
754         printf("%s(%d): ECH-PCI iobase=%x iopage=%x irq=%d\n", __file__,                         __LINE__, brdp->ioaddr2, brdp->ioaddr1, brdp->irq);
755 #endif
756         stl_brdinit(brdp);
757 }
758
759 #endif
760
761 /*****************************************************************************/
762
763 STATIC int stlopen(dev_t dev, int flag, int mode, struct proc *p)
764 {
765         struct tty      *tp;
766         stlport_t       *portp;
767         int             error, callout, x;
768
769 #if DEBUG
770         printf("stlopen(dev=%x,flag=%x,mode=%x,p=%x)\n", (int) dev, flag,
771                 mode, (int) p);
772 #endif
773
774 /*
775  *      Firstly check if the supplied device number is a valid device.
776  */
777         if (minor(dev) & STL_MEMDEV)
778                 return(0);
779
780         portp = stl_dev2port(dev);
781         if (portp == (stlport_t *) NULL)
782                 return(ENXIO);
783         tp = &portp->tty;
784         dev->si_tty = tp;
785         callout = minor(dev) & STL_CALLOUTDEV;
786         error = 0;
787
788         x = spltty();
789
790 stlopen_restart:
791 /*
792  *      Wait here for the DTR drop timeout period to expire.
793  */
794         while (portp->state & ASY_DTRWAIT) {
795                 error = tsleep(&portp->dtrwait, (TTIPRI | PCATCH),
796                         "stldtr", 0);
797                 if (error)
798                         goto stlopen_end;
799         }
800         
801 /*
802  *      We have a valid device, so now we check if it is already open.
803  *      If not then initialize the port hardware and set up the tty
804  *      struct as required.
805  */
806         if ((tp->t_state & TS_ISOPEN) == 0) {
807                 tp->t_oproc = stl_start;
808                 tp->t_stop = stl_stop;
809                 tp->t_param = stl_param;
810                 tp->t_dev = dev;
811                 tp->t_termios = callout ? portp->initouttios :
812                         portp->initintios;
813                 stl_rawopen(portp);
814                 if ((portp->sigs & TIOCM_CD) || callout)
815                         (*linesw[tp->t_line].l_modem)(tp, 1);
816         } else {
817                 if (callout) {
818                         if (portp->callout == 0) {
819                                 error = EBUSY;
820                                 goto stlopen_end;
821                         }
822                 } else {
823                         if (portp->callout != 0) {
824                                 if (flag & O_NONBLOCK) {
825                                         error = EBUSY;
826                                         goto stlopen_end;
827                                 }
828                                 error = tsleep(&portp->callout,
829                                         (TTIPRI | PCATCH), "stlcall", 0);
830                                 if (error)
831                                         goto stlopen_end;
832                                 goto stlopen_restart;
833                         }
834                 }
835                 if ((tp->t_state & TS_XCLUDE) &&
836                     suser(p)) {
837                         error = EBUSY;
838                         goto stlopen_end;
839                 }
840         }
841
842 /*
843  *      If this port is not the callout device and we do not have carrier
844  *      then we need to sleep, waiting for it to be asserted.
845  */
846         if (((tp->t_state & TS_CARR_ON) == 0) && !callout &&
847                         ((tp->t_cflag & CLOCAL) == 0) &&
848                         ((flag & O_NONBLOCK) == 0)) {
849                 portp->waitopens++;
850                 error = tsleep(TSA_CARR_ON(tp), (TTIPRI | PCATCH), "stldcd", 0);
851                 portp->waitopens--;
852                 if (error)
853                         goto stlopen_end;
854                 goto stlopen_restart;
855         }
856
857 /*
858  *      Open the line discipline.
859  */
860         error = (*linesw[tp->t_line].l_open)(dev, tp);
861         stl_ttyoptim(portp, &tp->t_termios);
862         if ((tp->t_state & TS_ISOPEN) && callout)
863                 portp->callout = 1;
864
865 /*
866  *      If for any reason we get to here and the port is not actually
867  *      open then close of the physical hardware - no point leaving it
868  *      active when the open failed...
869  */
870 stlopen_end:
871         splx(x);
872         if (((tp->t_state & TS_ISOPEN) == 0) && (portp->waitopens == 0))
873                 stl_rawclose(portp);
874
875         return(error);
876 }
877
878 /*****************************************************************************/
879
880 STATIC int stlclose(dev_t dev, int flag, int mode, struct proc *p)
881 {
882         struct tty      *tp;
883         stlport_t       *portp;
884         int             x;
885
886 #if DEBUG
887         printf("stlclose(dev=%s,flag=%x,mode=%x,p=%p)\n", devtoname(dev),
888                 flag, mode, (void *) p);
889 #endif
890
891         if (minor(dev) & STL_MEMDEV)
892                 return(0);
893
894         portp = stl_dev2port(dev);
895         if (portp == (stlport_t *) NULL)
896                 return(ENXIO);
897         tp = &portp->tty;
898
899         x = spltty();
900         (*linesw[tp->t_line].l_close)(tp, flag);
901         stl_ttyoptim(portp, &tp->t_termios);
902         stl_rawclose(portp);
903         ttyclose(tp);
904         splx(x);
905         return(0);
906 }
907
908 /*****************************************************************************/
909
910 #if VFREEBSD >= 220
911
912 STATIC void stl_stop(struct tty *tp, int rw)
913 {
914 #if DEBUG
915         printf("stl_stop(tp=%x,rw=%x)\n", (int) tp, rw);
916 #endif
917
918         stl_flush((stlport_t *) tp, rw);
919 }
920
921 #else
922
923 STATIC int stlstop(struct tty *tp, int rw)
924 {
925 #if DEBUG
926         printf("stlstop(tp=%x,rw=%x)\n", (int) tp, rw);
927 #endif
928
929         stl_flush((stlport_t *) tp, rw);
930         return(0);
931 }
932
933 #endif
934
935 /*****************************************************************************/
936
937 STATIC int stlioctl(dev_t dev, unsigned long cmd, caddr_t data, int flag,
938                     struct proc *p)
939 {
940         struct termios  *newtios, *localtios;
941         struct tty      *tp;
942         stlport_t       *portp;
943         int             error, i, x;
944
945 #if DEBUG
946         printf("stlioctl(dev=%s,cmd=%lx,data=%p,flag=%x,p=%p)\n",
947                 devtoname(dev), cmd, (void *) data, flag, (void *) p);
948 #endif
949
950         if (minor(dev) & STL_MEMDEV)
951                 return(stl_memioctl(dev, cmd, data, flag, p));
952
953         portp = stl_dev2port(dev);
954         if (portp == (stlport_t *) NULL)
955                 return(ENODEV);
956         tp = &portp->tty;
957         error = 0;
958         
959 /*
960  *      First up handle ioctls on the control devices.
961  */
962         if (minor(dev) & STL_CTRLDEV) {
963                 if ((minor(dev) & STL_CTRLDEV) == STL_CTRLINIT)
964                         localtios = (minor(dev) & STL_CALLOUTDEV) ?
965                                 &portp->initouttios : &portp->initintios;
966                 else if ((minor(dev) & STL_CTRLDEV) == STL_CTRLLOCK)
967                         localtios = (minor(dev) & STL_CALLOUTDEV) ?
968                                 &portp->lockouttios : &portp->lockintios;
969                 else
970                         return(ENODEV);
971
972                 switch (cmd) {
973                 case TIOCSETA:
974                         if ((error = suser(p)) == 0)
975                                 *localtios = *((struct termios *) data);
976                         break;
977                 case TIOCGETA:
978                         *((struct termios *) data) = *localtios;
979                         break;
980                 case TIOCGETD:
981                         *((int *) data) = TTYDISC;
982                         break;
983                 case TIOCGWINSZ:
984                         bzero(data, sizeof(struct winsize));
985                         break;
986                 default:
987                         error = ENOTTY;
988                         break;
989                 }
990                 return(error);
991         }
992
993 /*
994  *      Deal with 4.3 compatibility issues if we have too...
995  */
996 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
997         if (1) {
998                 struct termios  tios;
999                 unsigned long   oldcmd;
1000
1001                 tios = tp->t_termios;
1002                 oldcmd = cmd;
1003                 if ((error = ttsetcompat(tp, &cmd, data, &tios)))
1004                         return(error);
1005                 if (cmd != oldcmd)
1006                         data = (caddr_t) &tios;
1007         }
1008 #endif
1009
1010 /*
1011  *      Carry out some pre-cmd processing work first...
1012  *      Hmmm, not so sure we want this, disable for now...
1013  */
1014         if ((cmd == TIOCSETA) || (cmd == TIOCSETAW) || (cmd == TIOCSETAF)) {
1015                 newtios = (struct termios *) data;
1016                 localtios = (minor(dev) & STL_CALLOUTDEV) ? 
1017                         &portp->lockouttios : &portp->lockintios;
1018
1019                 newtios->c_iflag = (tp->t_iflag & localtios->c_iflag) |
1020                         (newtios->c_iflag & ~localtios->c_iflag);
1021                 newtios->c_oflag = (tp->t_oflag & localtios->c_oflag) |
1022                         (newtios->c_oflag & ~localtios->c_oflag);
1023                 newtios->c_cflag = (tp->t_cflag & localtios->c_cflag) |
1024                         (newtios->c_cflag & ~localtios->c_cflag);
1025                 newtios->c_lflag = (tp->t_lflag & localtios->c_lflag) |
1026                         (newtios->c_lflag & ~localtios->c_lflag);
1027                 for (i = 0; (i < NCCS); i++) {
1028                         if (localtios->c_cc[i] != 0)
1029                                 newtios->c_cc[i] = tp->t_cc[i];
1030                 }
1031                 if (localtios->c_ispeed != 0)
1032                         newtios->c_ispeed = tp->t_ispeed;
1033                 if (localtios->c_ospeed != 0)
1034                         newtios->c_ospeed = tp->t_ospeed;
1035         }
1036
1037 /*
1038  *      Call the line discipline and the common command processing to
1039  *      process this command (if they can).
1040  */
1041         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1042         if (error != ENOIOCTL)
1043                 return(error);
1044
1045         x = spltty();
1046         error = ttioctl(tp, cmd, data, flag);
1047         stl_ttyoptim(portp, &tp->t_termios);
1048         if (error != ENOIOCTL) {
1049                 splx(x);
1050                 return(error);
1051         }
1052
1053         error = 0;
1054
1055 /*
1056  *      Process local commands here. These are all commands that only we
1057  *      can take care of (they all rely on actually doing something special
1058  *      to the actual hardware).
1059  */
1060         switch (cmd) {
1061         case TIOCSBRK:
1062                 stl_sendbreak(portp, -1);
1063                 break;
1064         case TIOCCBRK:
1065                 stl_sendbreak(portp, -2);
1066                 break;
1067         case TIOCSDTR:
1068                 stl_setsignals(portp, 1, -1);
1069                 break;
1070         case TIOCCDTR:
1071                 stl_setsignals(portp, 0, -1);
1072                 break;
1073         case TIOCMSET:
1074                 i = *((int *) data);
1075                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 1 : 0),
1076                         ((i & TIOCM_RTS) ? 1 : 0));
1077                 break;
1078         case TIOCMBIS:
1079                 i = *((int *) data);
1080                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 1 : -1),
1081                         ((i & TIOCM_RTS) ? 1 : -1));
1082                 break;
1083         case TIOCMBIC:
1084                 i = *((int *) data);
1085                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 0 : -1),
1086                         ((i & TIOCM_RTS) ? 0 : -1));
1087                 break;
1088         case TIOCMGET:
1089                 *((int *) data) = (stl_getsignals(portp) | TIOCM_LE);
1090                 break;
1091         case TIOCMSDTRWAIT:
1092                 if ((error = suser(p)) == 0)
1093                         portp->dtrwait = *((int *) data) * hz / 100;
1094                 break;
1095         case TIOCMGDTRWAIT:
1096                 *((int *) data) = portp->dtrwait * 100 / hz;
1097                 break;
1098         case TIOCTIMESTAMP:
1099                 portp->dotimestamp = 1;
1100                 *((struct timeval *) data) = portp->timestamp;
1101                 break;
1102         default:
1103                 error = ENOTTY;
1104                 break;
1105         }
1106         splx(x);
1107
1108         return(error);
1109 }
1110
1111 /*****************************************************************************/
1112
1113 /*
1114  *      Convert the specified minor device number into a port struct
1115  *      pointer. Return NULL if the device number is not a valid port.
1116  */
1117
1118 STATIC stlport_t *stl_dev2port(dev_t dev)
1119 {
1120         stlbrd_t        *brdp;
1121
1122         brdp = stl_brds[MKDEV2BRD(dev)];
1123         if (brdp == (stlbrd_t *) NULL)
1124                 return((stlport_t *) NULL);
1125         return(brdp->ports[MKDEV2PORT(dev)]);
1126 }
1127
1128 /*****************************************************************************/
1129
1130 /*
1131  *      Initialize the port hardware. This involves enabling the transmitter
1132  *      and receiver, setting the port configuration, and setting the initial
1133  *      signal state.
1134  */
1135
1136 static int stl_rawopen(stlport_t *portp)
1137 {
1138 #if DEBUG
1139         printf("stl_rawopen(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1140                 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1141 #endif
1142         stl_param(&portp->tty, &portp->tty.t_termios);
1143         portp->sigs = stl_getsignals(portp);
1144         stl_setsignals(portp, 1, 1);
1145         stl_enablerxtx(portp, 1, 1);
1146         stl_startrxtx(portp, 1, 0);
1147         return(0);
1148 }
1149
1150 /*****************************************************************************/
1151
1152 /*
1153  *      Shutdown the hardware of a port. Disable its transmitter and
1154  *      receiver, and maybe drop signals if appropriate.
1155  */
1156
1157 static int stl_rawclose(stlport_t *portp)
1158 {
1159         struct tty      *tp;
1160
1161 #if DEBUG
1162         printf("stl_rawclose(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1163                 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1164 #endif
1165
1166         tp = &portp->tty;
1167         stl_disableintrs(portp);
1168         stl_enablerxtx(portp, 0, 0);
1169         stl_flush(portp, (FWRITE | FREAD));
1170         if (tp->t_cflag & HUPCL) {
1171                 stl_setsignals(portp, 0, 0);
1172                 if (portp->dtrwait != 0) {
1173                         portp->state |= ASY_DTRWAIT;
1174                         timeout(stl_dtrwakeup, portp, portp->dtrwait);
1175                 }
1176         }
1177         portp->callout = 0;
1178         portp->brklen = 0;
1179         portp->state &= ~(ASY_ACTIVE | ASY_RTSFLOW);
1180         wakeup(&portp->callout);
1181         wakeup(TSA_CARR_ON(tp));
1182         return(0);
1183 }
1184
1185 /*****************************************************************************/
1186
1187 /*
1188  *      Clear the DTR waiting flag, and wake up any sleepers waiting for
1189  *      DTR wait period to finish.
1190  */
1191
1192 static void stl_dtrwakeup(void *arg)
1193 {
1194         stlport_t       *portp;
1195
1196         portp = (stlport_t *) arg;
1197         portp->state &= ~ASY_DTRWAIT;
1198         wakeup(&portp->dtrwait);
1199 }
1200
1201 /*****************************************************************************/
1202
1203 /*
1204  *      Start (or continue) the transfer of TX data on this port. If the
1205  *      port is not currently busy then load up the interrupt ring queue
1206  *      buffer and kick of the transmitter. If the port is running low on
1207  *      TX data then refill the ring queue. This routine is also used to
1208  *      activate input flow control!
1209  */
1210
1211 static void stl_start(struct tty *tp)
1212 {
1213         stlport_t       *portp;
1214         unsigned int    len, stlen;
1215         char            *head, *tail;
1216         int             count, x;
1217
1218         portp = (stlport_t *) tp;
1219
1220 #if DEBUG
1221         printf("stl_start(tp=%x): brdnr=%d portnr=%d\n", (int) tp, 
1222                 portp->brdnr, portp->portnr);
1223 #endif
1224
1225         x = spltty();
1226
1227 /*
1228  *      Check if the ports input has been blocked, and take appropriate action.
1229  *      Not very often do we really need to do anything, so make it quick.
1230  */
1231         if (tp->t_state & TS_TBLOCK) {
1232                 if ((portp->state & ASY_RTSFLOW) == 0)
1233                         stl_flowcontrol(portp, 0, -1);
1234         } else {
1235                 if (portp->state & ASY_RTSFLOW)
1236                         stl_flowcontrol(portp, 1, -1);
1237         }
1238
1239 #if VFREEBSD == 205
1240 /*
1241  *      Check if the output cooked clist buffers are near empty, wake up
1242  *      the line discipline to fill it up.
1243  */
1244         if (tp->t_outq.c_cc <= tp->t_lowat) {
1245                 if (tp->t_state & TS_ASLEEP) {
1246                         tp->t_state &= ~TS_ASLEEP;
1247                         wakeup(&tp->t_outq);
1248                 }
1249                 selwakeup(&tp->t_wsel);
1250         }
1251 #endif
1252
1253         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
1254                 splx(x);
1255                 return;
1256         }
1257
1258 /*
1259  *      Copy data from the clists into the interrupt ring queue. This will
1260  *      require at most 2 copys... What we do is calculate how many chars
1261  *      can fit into the ring queue, and how many can fit in 1 copy. If after
1262  *      the first copy there is still more room then do the second copy. 
1263  *      The beauty of this type of ring queue is that we do not need to
1264  *      spl protect our-selves, since we only ever update the head pointer,
1265  *      and the interrupt routine only ever updates the tail pointer.
1266  */
1267         if (tp->t_outq.c_cc != 0) {
1268                 head = portp->tx.head;
1269                 tail = portp->tx.tail;
1270                 if (head >= tail) {
1271                         len = STL_TXBUFSIZE - (head - tail) - 1;
1272                         stlen = portp->tx.endbuf - head;
1273                 } else {
1274                         len = tail - head - 1;
1275                         stlen = len;
1276                 }
1277
1278                 if (len > 0) {
1279                         stlen = MIN(len, stlen);
1280                         count = q_to_b(&tp->t_outq, head, stlen);
1281                         len -= count;
1282                         head += count;
1283                         if (head >= portp->tx.endbuf) {
1284                                 head = portp->tx.buf;
1285                                 if (len > 0) {
1286                                         stlen = q_to_b(&tp->t_outq, head, len);
1287                                         head += stlen;
1288                                         count += stlen;
1289                                 }
1290                         }
1291                         portp->tx.head = head;
1292                         if (count > 0)
1293                                 stl_startrxtx(portp, -1, 1);
1294                 }
1295
1296 /*
1297  *              If we sent something, make sure we are called again.
1298  */
1299                 tp->t_state |= TS_BUSY;
1300         }
1301
1302 #if VFREEBSD != 205
1303 /*
1304  *      Do any writer wakeups.
1305  */
1306         ttwwakeup(tp);
1307 #endif
1308
1309         splx(x);
1310 }
1311
1312 /*****************************************************************************/
1313
1314 static void stl_flush(stlport_t *portp, int flag)
1315 {
1316         char    *head, *tail;
1317         int     len, x;
1318
1319 #if DEBUG
1320         printf("stl_flush(portp=%x,flag=%x)\n", (int) portp, flag);
1321 #endif
1322
1323         if (portp == (stlport_t *) NULL)
1324                 return;
1325
1326         x = spltty();
1327
1328         if (flag & FWRITE) {
1329                 BRDENABLE(portp->brdnr, portp->pagenr);
1330                 stl_setreg(portp, CAR, (portp->portnr & 0x03));
1331                 stl_ccrwait(portp);
1332                 stl_setreg(portp, CCR, CCR_TXFLUSHFIFO);
1333                 stl_ccrwait(portp);
1334                 portp->tx.tail = portp->tx.head;
1335                 BRDDISABLE(portp->brdnr);
1336         }
1337
1338 /*
1339  *      The only thing to watch out for when flushing the read side is
1340  *      the RX status buffer. The interrupt code relys on the status
1341  *      bytes as being zeroed all the time (it does not bother setting
1342  *      a good char status to 0, it expects that it already will be).
1343  *      We also need to un-flow the RX channel if flow control was
1344  *      active.
1345  */
1346         if (flag & FREAD) {
1347                 head = portp->rx.head;
1348                 tail = portp->rx.tail;
1349                 if (head != tail) {
1350                         if (head >= tail) {
1351                                 len = head - tail;
1352                         } else {
1353                                 len = portp->rx.endbuf - tail;
1354                                 bzero(portp->rxstatus.buf,
1355                                         (head - portp->rx.buf));
1356                         }
1357                         bzero((tail + STL_RXBUFSIZE), len);
1358                         portp->rx.tail = head;
1359                 }
1360
1361                 if ((portp->state & ASY_RTSFLOW) &&
1362                                 ((portp->tty.t_state & TS_TBLOCK) == 0))
1363                         stl_flowcontrol(portp, 1, -1);
1364         }
1365
1366         splx(x);
1367 }
1368
1369 /*****************************************************************************/
1370
1371 /*
1372  *      These functions get/set/update the registers of the cd1400 UARTs.
1373  *      Access to the cd1400 registers is via an address/data io port pair.
1374  *      (Maybe should make this inline...)
1375  */
1376
1377 static int stl_getreg(stlport_t *portp, int regnr)
1378 {
1379         outb(portp->ioaddr, (regnr + portp->uartaddr));
1380         return(inb(portp->ioaddr + EREG_DATA));
1381 }
1382
1383 /*****************************************************************************/
1384
1385 static void stl_setreg(stlport_t *portp, int regnr, int value)
1386 {
1387         outb(portp->ioaddr, (regnr + portp->uartaddr));
1388         outb((portp->ioaddr + EREG_DATA), value);
1389 }
1390
1391 /*****************************************************************************/
1392
1393 static int stl_updatereg(stlport_t *portp, int regnr, int value)
1394 {
1395         outb(portp->ioaddr, (regnr + portp->uartaddr));
1396         if (inb(portp->ioaddr + EREG_DATA) != value) {
1397                 outb((portp->ioaddr + EREG_DATA), value);
1398                 return(1);
1399         }
1400         return(0);
1401 }
1402
1403 /*****************************************************************************/
1404
1405 /*
1406  *      Wait for the command register to be ready. We will poll this, since
1407  *      it won't usually take too long to be ready, and it is only really
1408  *      used for non-critical actions.
1409  */
1410
1411 static void stl_ccrwait(stlport_t *portp)
1412 {
1413         int     i;
1414
1415         for (i = 0; (i < CCR_MAXWAIT); i++) {
1416                 if (stl_getreg(portp, CCR) == 0) {
1417                         return;
1418                 }
1419         }
1420
1421         printf("STALLION: cd1400 device not responding, brd=%d panel=%d"
1422                 "port=%d\n", portp->brdnr, portp->panelnr, portp->portnr);
1423 }
1424
1425 /*****************************************************************************/
1426
1427 /*
1428  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
1429  *      chars is pretty simple, stuff as many as possible from the TX buffer
1430  *      into the cd1400 FIFO. Must also handle TX breaks here, since they
1431  *      are embedded as commands in the data stream. Oh no, had to use a goto!
1432  *      This could be optimized more, will do when I get time...
1433  *      In practice it is possible that interrupts are enabled but that the
1434  *      port has been hung up. Need to handle not having any TX buffer here,
1435  *      this is done by using the side effect that head and tail will also
1436  *      be NULL if the buffer has been freed.
1437  */
1438
1439 static __inline void stl_txisr(stlpanel_t *panelp, int ioaddr)
1440 {
1441         stlport_t       *portp;
1442         int             len, stlen;
1443         char            *head, *tail;
1444         unsigned char   ioack, srer;
1445
1446 #if DEBUG
1447         printf("stl_txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1448 #endif
1449
1450         ioack = inb(ioaddr + EREG_TXACK);
1451         if (((ioack & panelp->ackmask) != 0) ||
1452                         ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
1453                 printf("STALLION: bad TX interrupt ack value=%x\n", ioack);
1454                 return;
1455         }
1456         portp = panelp->ports[(ioack >> 3)];
1457
1458 /*
1459  *      Unfortunately we need to handle breaks in the data stream, since
1460  *      this is the only way to generate them on the cd1400. Do it now if
1461  *      a break is to be sent. Some special cases here: brklen is -1 then
1462  *      start sending an un-timed break, if brklen is -2 then stop sending
1463  *      an un-timed break, if brklen is -3 then we have just sent an
1464  *      un-timed break and do not want any data to go out, if brklen is -4
1465  *      then a break has just completed so clean up the port settings.
1466  */
1467         if (portp->brklen != 0) {
1468                 if (portp->brklen >= -1) {
1469                         outb(ioaddr, (TDR + portp->uartaddr));
1470                         outb((ioaddr + EREG_DATA), ETC_CMD);
1471                         outb((ioaddr + EREG_DATA), ETC_STARTBREAK);
1472                         if (portp->brklen > 0) {
1473                                 outb((ioaddr + EREG_DATA), ETC_CMD);
1474                                 outb((ioaddr + EREG_DATA), ETC_DELAY);
1475                                 outb((ioaddr + EREG_DATA), portp->brklen);
1476                                 outb((ioaddr + EREG_DATA), ETC_CMD);
1477                                 outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
1478                                 portp->brklen = -4;
1479                         } else {
1480                                 portp->brklen = -3;
1481                         }
1482                 } else if (portp->brklen == -2) {
1483                         outb(ioaddr, (TDR + portp->uartaddr));
1484                         outb((ioaddr + EREG_DATA), ETC_CMD);
1485                         outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
1486                         portp->brklen = -4;
1487                 } else if (portp->brklen == -3) {
1488                         outb(ioaddr, (SRER + portp->uartaddr));
1489                         srer = inb(ioaddr + EREG_DATA);
1490                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
1491                         outb((ioaddr + EREG_DATA), srer);
1492                 } else {
1493                         outb(ioaddr, (COR2 + portp->uartaddr));
1494                         outb((ioaddr + EREG_DATA),
1495                                 (inb(ioaddr + EREG_DATA) & ~COR2_ETC));
1496                         portp->brklen = 0;
1497                 }
1498                 goto stl_txalldone;
1499         }
1500
1501         head = portp->tx.head;
1502         tail = portp->tx.tail;
1503         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1504         if ((len == 0) || ((len < STL_TXBUFLOW) &&
1505                         ((portp->state & ASY_TXLOW) == 0))) {
1506                 portp->state |= ASY_TXLOW;
1507                 stl_dotimeout();
1508         }
1509
1510         if (len == 0) {
1511                 outb(ioaddr, (SRER + portp->uartaddr));
1512                 srer = inb(ioaddr + EREG_DATA);
1513                 if (srer & SRER_TXDATA) {
1514                         srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
1515                 } else {
1516                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
1517                         portp->tty.t_state &= ~TS_BUSY;
1518                 }
1519                 outb((ioaddr + EREG_DATA), srer);
1520         } else {
1521                 len = MIN(len, CD1400_TXFIFOSIZE);
1522                 portp->stats.txtotal += len;
1523                 stlen = MIN(len, (portp->tx.endbuf - tail));
1524                 outb(ioaddr, (TDR + portp->uartaddr));
1525                 outsb((ioaddr + EREG_DATA), tail, stlen);
1526                 len -= stlen;
1527                 tail += stlen;
1528                 if (tail >= portp->tx.endbuf)
1529                         tail = portp->tx.buf;
1530                 if (len > 0) {
1531                         outsb((ioaddr + EREG_DATA), tail, len);
1532                         tail += len;
1533                 }
1534                 portp->tx.tail = tail;
1535         }
1536
1537 stl_txalldone:
1538         outb(ioaddr, (EOSRR + portp->uartaddr));
1539         outb((ioaddr + EREG_DATA), 0);
1540 }
1541
1542 /*****************************************************************************/
1543
1544 /*
1545  *      Receive character interrupt handler. Determine if we have good chars
1546  *      or bad chars and then process appropriately. Good chars are easy
1547  *      just shove the lot into the RX buffer and set all status bytes to 0.
1548  *      If a bad RX char then process as required. This routine needs to be
1549  *      fast!
1550  */
1551
1552 static __inline void stl_rxisr(stlpanel_t *panelp, int ioaddr)
1553 {
1554         stlport_t       *portp;
1555         struct tty      *tp;
1556         unsigned int    ioack, len, buflen, stlen;
1557         unsigned char   status;
1558         char            ch;
1559         char            *head, *tail;
1560         static char     unwanted[CD1400_RXFIFOSIZE];
1561
1562 #if DEBUG
1563         printf("stl_rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1564 #endif
1565
1566         ioack = inb(ioaddr + EREG_RXACK);
1567         if ((ioack & panelp->ackmask) != 0) {
1568                 printf("STALLION: bad RX interrupt ack value=%x\n", ioack);
1569                 return;
1570         }
1571         portp = panelp->ports[(ioack >> 3)];
1572         tp = &portp->tty;
1573
1574 /*
1575  *      First up, caluclate how much room there is in the RX ring queue.
1576  *      We also want to keep track of the longest possible copy length,
1577  *      this has to allow for the wrapping of the ring queue.
1578  */
1579         head = portp->rx.head;
1580         tail = portp->rx.tail;
1581         if (head >= tail) {
1582                 buflen = STL_RXBUFSIZE - (head - tail) - 1;
1583                 stlen = portp->rx.endbuf - head;
1584         } else {
1585                 buflen = tail - head - 1;
1586                 stlen = buflen;
1587         }
1588
1589 /*
1590  *      Check if the input buffer is near full. If so then we should take
1591  *      some flow control action... It is very easy to do hardware and
1592  *      software flow control from here since we have the port selected on
1593  *      the UART.
1594  */
1595         if (buflen <= (STL_RXBUFSIZE - STL_RXBUFHIGH)) {
1596                 if (((portp->state & ASY_RTSFLOW) == 0) &&
1597                                 (portp->state & ASY_RTSFLOWMODE)) {
1598                         portp->state |= ASY_RTSFLOW;
1599                         stl_setreg(portp, MCOR1,
1600                                 (stl_getreg(portp, MCOR1) & 0xf0));
1601                         stl_setreg(portp, MSVR2, 0);
1602                         portp->stats.rxrtsoff++;
1603                 }
1604         }
1605
1606 /*
1607  *      OK we are set, process good data... If the RX ring queue is full
1608  *      just chuck the chars - don't leave them in the UART.
1609  */
1610         if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
1611                 outb(ioaddr, (RDCR + portp->uartaddr));
1612                 len = inb(ioaddr + EREG_DATA);
1613                 if (buflen == 0) {
1614                         outb(ioaddr, (RDSR + portp->uartaddr));
1615                         insb((ioaddr + EREG_DATA), &unwanted[0], len);
1616                         portp->stats.rxlost += len;
1617                         portp->stats.rxtotal += len;
1618                 } else {
1619                         len = MIN(len, buflen);
1620                         portp->stats.rxtotal += len;
1621                         stlen = MIN(len, stlen);
1622                         if (len > 0) {
1623                                 outb(ioaddr, (RDSR + portp->uartaddr));
1624                                 insb((ioaddr + EREG_DATA), head, stlen);
1625                                 head += stlen;
1626                                 if (head >= portp->rx.endbuf) {
1627                                         head = portp->rx.buf;
1628                                         len -= stlen;
1629                                         insb((ioaddr + EREG_DATA), head, len);
1630                                         head += len;
1631                                 }
1632                         }
1633                 }
1634         } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
1635                 outb(ioaddr, (RDSR + portp->uartaddr));
1636                 status = inb(ioaddr + EREG_DATA);
1637                 ch = inb(ioaddr + EREG_DATA);
1638                 if (status & ST_BREAK)
1639                         portp->stats.rxbreaks++;
1640                 if (status & ST_FRAMING)
1641                         portp->stats.rxframing++;
1642                 if (status & ST_PARITY)
1643                         portp->stats.rxparity++;
1644                 if (status & ST_OVERRUN)
1645                         portp->stats.rxoverrun++;
1646                 if (status & ST_SCHARMASK) {
1647                         if ((status & ST_SCHARMASK) == ST_SCHAR1)
1648                                 portp->stats.txxon++;
1649                         if ((status & ST_SCHARMASK) == ST_SCHAR2)
1650                                 portp->stats.txxoff++;
1651                         goto stl_rxalldone;
1652                 }
1653                 if ((portp->rxignoremsk & status) == 0) {
1654                         if ((tp->t_state & TS_CAN_BYPASS_L_RINT) &&
1655                             ((status & ST_FRAMING) ||
1656                             ((status & ST_PARITY) && (tp->t_iflag & INPCK))))
1657                                 ch = 0;
1658                         if ((portp->rxmarkmsk & status) == 0)
1659                                 status = 0;
1660                         *(head + STL_RXBUFSIZE) = status;
1661                         *head++ = ch;
1662                         if (head >= portp->rx.endbuf)
1663                                 head = portp->rx.buf;
1664                 }
1665         } else {
1666                 printf("STALLION: bad RX interrupt ack value=%x\n", ioack);
1667                 return;
1668         }
1669
1670         portp->rx.head = head;
1671         portp->state |= ASY_RXDATA;
1672         stl_dotimeout();
1673
1674 stl_rxalldone:
1675         outb(ioaddr, (EOSRR + portp->uartaddr));
1676         outb((ioaddr + EREG_DATA), 0);
1677 }
1678
1679 /*****************************************************************************/
1680
1681 /*
1682  *      Modem interrupt handler. The is called when the modem signal line
1683  *      (DCD) has changed state. Leave most of the work to the off-level
1684  *      processing routine.
1685  */
1686
1687 static __inline void stl_mdmisr(stlpanel_t *panelp, int ioaddr)
1688 {
1689         stlport_t       *portp;
1690         unsigned int    ioack;
1691         unsigned char   misr;
1692
1693 #if DEBUG
1694         printf("stl_mdmisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1695 #endif
1696
1697         ioack = inb(ioaddr + EREG_MDACK);
1698         if (((ioack & panelp->ackmask) != 0) ||
1699                         ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
1700                 printf("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
1701                 return;
1702         }
1703         portp = panelp->ports[(ioack >> 3)];
1704
1705         outb(ioaddr, (MISR + portp->uartaddr));
1706         misr = inb(ioaddr + EREG_DATA);
1707         if (misr & MISR_DCD) {
1708                 portp->state |= ASY_DCDCHANGE;
1709                 portp->stats.modem++;
1710                 stl_dotimeout();
1711         }
1712
1713         outb(ioaddr, (EOSRR + portp->uartaddr));
1714         outb((ioaddr + EREG_DATA), 0);
1715 }
1716
1717 /*****************************************************************************/
1718
1719 /*
1720  *      Interrupt handler for EIO and ECH boards. This code ain't all that
1721  *      pretty, but the idea is to make it as fast as possible. This code is
1722  *      well suited to be assemblerized :-)  We don't use the general purpose
1723  *      register access functions here, for speed we will go strait to the
1724  *      io register.
1725  */
1726
1727 static void stlintr(int unit)
1728 {
1729         stlbrd_t        *brdp;
1730         stlpanel_t      *panelp;
1731         unsigned char   svrtype;
1732         int             i, panelnr, iobase;
1733         int             cnt;
1734
1735 #if DEBUG
1736         printf("stlintr(unit=%d)\n", unit);
1737 #endif
1738
1739         cnt = 0;
1740         panelp = (stlpanel_t *) NULL;
1741         for (i = 0; (i < stl_nrbrds); ) {
1742                 if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL) {
1743                         i++;
1744                         continue;
1745                 }
1746                 if (brdp->state == 0) {
1747                         i++;
1748                         continue;
1749                 }
1750 /*
1751  *              The following section of code handles the subtle differences
1752  *              between board types. It is sort of similar, but different
1753  *              enough to handle each separately.
1754  */
1755                 if (brdp->brdtype == BRD_EASYIO) {
1756                         if ((inb(brdp->iostatus) & EIO_INTRPEND) == 0) {
1757                                 i++;
1758                                 continue;
1759                         }
1760                         panelp = brdp->panels[0];
1761                         iobase = panelp->iobase;
1762                         outb(iobase, SVRR);
1763                         svrtype = inb(iobase + EREG_DATA);
1764                         if (brdp->nrports > 4) {
1765                                 outb(iobase, (SVRR + 0x80));
1766                                 svrtype |= inb(iobase + EREG_DATA);
1767                         }
1768                 } else if (brdp->brdtype == BRD_ECH) {
1769                         if ((inb(brdp->iostatus) & ECH_INTRPEND) == 0) {
1770                                 i++;
1771                                 continue;
1772                         }
1773                         outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
1774                         for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1775                                 panelp = brdp->panels[panelnr];
1776                                 iobase = panelp->iobase;
1777                                 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1778                                         break;
1779                                 if (panelp->nrports > 8) {
1780                                         iobase += 0x8;
1781                                         if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1782                                                 break;
1783                                 }
1784                         }       
1785                         if (panelnr >= brdp->nrpanels) {
1786                                 i++;
1787                                 continue;
1788                         }
1789                         outb(iobase, SVRR);
1790                         svrtype = inb(iobase + EREG_DATA);
1791                         outb(iobase, (SVRR + 0x80));
1792                         svrtype |= inb(iobase + EREG_DATA);
1793                 } else if (brdp->brdtype == BRD_ECHPCI) {
1794                         iobase = brdp->ioaddr2;
1795                         for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1796                                 panelp = brdp->panels[panelnr];
1797                                 outb(brdp->ioctrl, panelp->pagenr);
1798                                 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1799                                         break;
1800                                 if (panelp->nrports > 8) {
1801                                         outb(brdp->ioctrl, (panelp->pagenr + 1));
1802                                         if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1803                                                 break;
1804                                 }
1805                         }       
1806                         if (panelnr >= brdp->nrpanels) {
1807                                 i++;
1808                                 continue;
1809                         }
1810                         outb(iobase, SVRR);
1811                         svrtype = inb(iobase + EREG_DATA);
1812                         outb(iobase, (SVRR + 0x80));
1813                         svrtype |= inb(iobase + EREG_DATA);
1814                 } else if (brdp->brdtype == BRD_ECHMC) {
1815                         if ((inb(brdp->iostatus) & ECH_INTRPEND) == 0) {
1816                                 i++;
1817                                 continue;
1818                         }
1819                         for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1820                                 panelp = brdp->panels[panelnr];
1821                                 iobase = panelp->iobase;
1822                                 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1823                                         break;
1824                                 if (panelp->nrports > 8) {
1825                                         iobase += 0x8;
1826                                         if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1827                                                 break;
1828                                 }
1829                         }       
1830                         if (panelnr >= brdp->nrpanels) {
1831                                 i++;
1832                                 continue;
1833                         }
1834                         outb(iobase, SVRR);
1835                         svrtype = inb(iobase + EREG_DATA);
1836                         outb(iobase, (SVRR + 0x80));
1837                         svrtype |= inb(iobase + EREG_DATA);
1838                 } else {
1839                         printf("STALLION: unknown board type=%x\n", brdp->brdtype);
1840                         i++;
1841                         continue;
1842                 }
1843
1844 /*
1845  *              We have determined what type of service is required for a
1846  *              port. From here on in the service of a port is the same no
1847  *              matter what the board type...
1848  */
1849                 if (svrtype & SVRR_RX)
1850                         stl_rxisr(panelp, iobase);
1851                 if (svrtype & SVRR_TX)
1852                         stl_txisr(panelp, iobase);
1853                 if (svrtype & SVRR_MDM)
1854                         stl_mdmisr(panelp, iobase);
1855
1856                 if (brdp->brdtype == BRD_ECH)
1857                         outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
1858         }
1859 }
1860
1861 /*****************************************************************************/
1862
1863 #if NPCI > 0
1864
1865 static void stlpciintr(void *arg)
1866 {
1867         stlintr(0);
1868 }
1869
1870 #endif
1871
1872 /*****************************************************************************/
1873
1874 /*
1875  *      If we haven't scheduled a timeout then do it, some port needs high
1876  *      level processing.
1877  */
1878
1879 static void stl_dotimeout()
1880 {
1881 #if DEBUG
1882         printf("stl_dotimeout()\n");
1883 #endif
1884
1885         if (stl_doingtimeout == 0) {
1886                 timeout(stl_poll, 0, 1);
1887                 stl_doingtimeout++;
1888         }
1889 }
1890
1891 /*****************************************************************************/
1892
1893 /*
1894  *      Service "software" level processing. Too slow or painfull to be done
1895  *      at real hardware interrupt time. This way we might also be able to
1896  *      do some service on other waiting ports as well...
1897  */
1898
1899 static void stl_poll(void *arg)
1900 {
1901         stlbrd_t        *brdp;
1902         stlport_t       *portp;
1903         struct tty      *tp;
1904         int             brdnr, portnr, rearm, x;
1905
1906 #if DEBUG
1907         printf("stl_poll()\n");
1908 #endif
1909
1910         stl_doingtimeout = 0;
1911         rearm = 0;
1912
1913         x = spltty();
1914         for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
1915                 if ((brdp = stl_brds[brdnr]) == (stlbrd_t *) NULL)
1916                         continue;
1917                 for (portnr = 0; (portnr < brdp->nrports); portnr++) {
1918                         if ((portp = brdp->ports[portnr]) == (stlport_t *) NULL)
1919                                 continue;
1920                         if ((portp->state & ASY_ACTIVE) == 0)
1921                                 continue;
1922                         tp = &portp->tty;
1923
1924                         if (portp->state & ASY_RXDATA)
1925                                 stl_rxprocess(portp);
1926                         if (portp->state & ASY_DCDCHANGE) {
1927                                 portp->state &= ~ASY_DCDCHANGE;
1928                                 portp->sigs = stl_getsignals(portp);
1929                                 (*linesw[tp->t_line].l_modem)(tp,
1930                                         (portp->sigs & TIOCM_CD));
1931                         }
1932                         if (portp->state & ASY_TXLOW) {
1933                                 portp->state &= ~ASY_TXLOW;
1934                                 (*linesw[tp->t_line].l_start)(tp);
1935                         }
1936
1937                         if (portp->state & ASY_ACTIVE)
1938                                 rearm++;
1939                 }
1940         }
1941         splx(x);
1942
1943         if (rearm)
1944                 stl_dotimeout();
1945 }
1946
1947 /*****************************************************************************/
1948
1949 /*
1950  *      Process the RX data that has been buffered up in the RX ring queue.
1951  */
1952
1953 static void stl_rxprocess(stlport_t *portp)
1954 {
1955         struct tty      *tp;
1956         unsigned int    len, stlen, lostlen;
1957         char            *head, *tail;
1958         char            status;
1959         int             ch;
1960
1961 #if DEBUG
1962         printf("stl_rxprocess(portp=%x): brdnr=%d portnr=%d\n", (int) portp, 
1963                 portp->brdnr, portp->portnr);
1964 #endif
1965
1966         tp = &portp->tty;
1967         portp->state &= ~ASY_RXDATA;
1968
1969         if ((tp->t_state & TS_ISOPEN) == 0) {
1970                 stl_flush(portp, FREAD);
1971                 return;
1972         }
1973
1974 /*
1975  *      Calculate the amount of data in the RX ring queue. Also calculate
1976  *      the largest single copy size...
1977  */
1978         head = portp->rx.head;
1979         tail = portp->rx.tail;
1980         if (head >= tail) {
1981                 len = head - tail;
1982                 stlen = len;
1983         } else {
1984                 len = STL_RXBUFSIZE - (tail - head);
1985                 stlen = portp->rx.endbuf - tail;
1986         }
1987
1988         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1989                 if (len > 0) {
1990                         if (((tp->t_rawq.c_cc + len) >= TTYHOG) &&
1991                                         ((portp->state & ASY_RTSFLOWMODE) ||
1992                                         (tp->t_iflag & IXOFF)) &&
1993                                         ((tp->t_state & TS_TBLOCK) == 0)) {
1994                                 ch = TTYHOG - tp->t_rawq.c_cc - 1;
1995                                 len = (ch > 0) ? ch : 0;
1996                                 stlen = MIN(stlen, len);
1997                                 ttyblock(tp);
1998                         }
1999                         lostlen = b_to_q(tail, stlen, &tp->t_rawq);
2000                         tail += stlen;
2001                         len -= stlen;
2002                         if (tail >= portp->rx.endbuf) {
2003                                 tail = portp->rx.buf;
2004                                 lostlen += b_to_q(tail, len, &tp->t_rawq);
2005                                 tail += len;
2006                         }
2007                         portp->stats.rxlost += lostlen;
2008                         ttwakeup(tp);
2009                         portp->rx.tail = tail;
2010                 }
2011         } else {
2012                 while (portp->rx.tail != head) {
2013                         ch = (unsigned char) *(portp->rx.tail);
2014                         status = *(portp->rx.tail + STL_RXBUFSIZE);
2015                         if (status) {
2016                                 *(portp->rx.tail + STL_RXBUFSIZE) = 0;
2017                                 if (status & ST_BREAK)
2018                                         ch |= TTY_BI;
2019                                 if (status & ST_FRAMING)
2020                                         ch |= TTY_FE;
2021                                 if (status & ST_PARITY)
2022                                         ch |= TTY_PE;
2023                                 if (status & ST_OVERRUN)
2024                                         ch |= TTY_OE;
2025                         }
2026                         (*linesw[tp->t_line].l_rint)(ch, tp);
2027                         if (portp->rx.tail == head)
2028                                 break;
2029
2030                         if (++(portp->rx.tail) >= portp->rx.endbuf)
2031                                 portp->rx.tail = portp->rx.buf;
2032                 }
2033         }
2034
2035         if (head != portp->rx.tail)
2036                 portp->state |= ASY_RXDATA;
2037
2038 /*
2039  *      If we where flow controled then maybe the buffer is low enough that
2040  *      we can re-activate it.
2041  */
2042         if ((portp->state & ASY_RTSFLOW) && ((tp->t_state & TS_TBLOCK) == 0))
2043                 stl_flowcontrol(portp, 1, -1);
2044 }
2045
2046 /*****************************************************************************/
2047
2048 /*
2049  *      Set up the cd1400 registers for a port based on the termios port
2050  *      settings.
2051  */
2052
2053 static int stl_param(struct tty *tp, struct termios *tiosp)
2054 {
2055         stlport_t       *portp;
2056         unsigned int    clkdiv;
2057         unsigned char   cor1, cor2, cor3;
2058         unsigned char   cor4, cor5, ccr;
2059         unsigned char   srer, sreron, sreroff;
2060         unsigned char   mcor1, mcor2, rtpr;
2061         unsigned char   clk, div;
2062         int             x;
2063
2064         portp = (stlport_t *) tp;
2065
2066 #if DEBUG
2067         printf("stl_param(tp=%x,tiosp=%x): brdnr=%d portnr=%d\n", (int) tp, 
2068                 (int) tiosp, portp->brdnr, portp->portnr);
2069 #endif
2070
2071         cor1 = 0;
2072         cor2 = 0;
2073         cor3 = 0;
2074         cor4 = 0;
2075         cor5 = 0;
2076         ccr = 0;
2077         rtpr = 0;
2078         clk = 0;
2079         div = 0;
2080         mcor1 = 0;
2081         mcor2 = 0;
2082         sreron = 0;
2083         sreroff = 0;
2084
2085 /*
2086  *      Set up the RX char ignore mask with those RX error types we
2087  *      can ignore. We could have used some special modes of the cd1400
2088  *      UART to help, but it is better this way because we can keep stats
2089  *      on the number of each type of RX exception event.
2090  */
2091         portp->rxignoremsk = 0;
2092         if (tiosp->c_iflag & IGNPAR)
2093                 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
2094         if (tiosp->c_iflag & IGNBRK)
2095                 portp->rxignoremsk |= ST_BREAK;
2096
2097         portp->rxmarkmsk = ST_OVERRUN;
2098         if (tiosp->c_iflag & (INPCK | PARMRK))
2099                 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
2100         if (tiosp->c_iflag & BRKINT)
2101                 portp->rxmarkmsk |= ST_BREAK;
2102
2103 /*
2104  *      Go through the char size, parity and stop bits and set all the
2105  *      option registers appropriately.
2106  */
2107         switch (tiosp->c_cflag & CSIZE) {
2108         case CS5:
2109                 cor1 |= COR1_CHL5;
2110                 break;
2111         case CS6:
2112                 cor1 |= COR1_CHL6;
2113                 break;
2114         case CS7:
2115                 cor1 |= COR1_CHL7;
2116                 break;
2117         default:
2118                 cor1 |= COR1_CHL8;
2119                 break;
2120         }
2121
2122         if (tiosp->c_cflag & CSTOPB)
2123                 cor1 |= COR1_STOP2;
2124         else
2125                 cor1 |= COR1_STOP1;
2126
2127         if (tiosp->c_cflag & PARENB) {
2128                 if (tiosp->c_cflag & PARODD)
2129                         cor1 |= (COR1_PARENB | COR1_PARODD);
2130                 else
2131                         cor1 |= (COR1_PARENB | COR1_PAREVEN);
2132         } else {
2133                 cor1 |= COR1_PARNONE;
2134         }
2135
2136         if (tiosp->c_iflag & ISTRIP)
2137                 cor5 |= COR5_ISTRIP;
2138
2139 /*
2140  *      Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
2141  *      space for hardware flow control and the like. This should be set to
2142  *      VMIN. Also here we will set the RX data timeout to 10ms - this should
2143  *      really be based on VTIME...
2144  */
2145         cor3 |= FIFO_RXTHRESHOLD;
2146         rtpr = 2;
2147
2148 /*
2149  *      Calculate the baud rate timers. For now we will just assume that
2150  *      the input and output baud are the same. Could have used a baud
2151  *      table here, but this way we can generate virtually any baud rate
2152  *      we like!
2153  */
2154         if (tiosp->c_ispeed == 0)
2155                 tiosp->c_ispeed = tiosp->c_ospeed;
2156         if ((tiosp->c_ospeed < 0) || (tiosp->c_ospeed > STL_MAXBAUD))
2157                 return(EINVAL);
2158
2159         if (tiosp->c_ospeed > 0) {
2160                 for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
2161                         clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) /
2162                                 tiosp->c_ospeed);
2163                         if (clkdiv < 0x100)
2164                                 break;
2165                 }
2166                 div = (unsigned char) clkdiv;
2167         }
2168
2169 /*
2170  *      Check what form of modem signaling is required and set it up.
2171  */
2172         if ((tiosp->c_cflag & CLOCAL) == 0) {
2173                 mcor1 |= MCOR1_DCD;
2174                 mcor2 |= MCOR2_DCD;
2175                 sreron |= SRER_MODEM;
2176         }
2177
2178 /*
2179  *      Setup cd1400 enhanced modes if we can. In particular we want to
2180  *      handle as much of the flow control as possbile automatically. As
2181  *      well as saving a few CPU cycles it will also greatly improve flow
2182  *      control reliablilty.
2183  */
2184         if (tiosp->c_iflag & IXON) {
2185                 cor2 |= COR2_TXIBE;
2186                 cor3 |= COR3_SCD12;
2187                 if (tiosp->c_iflag & IXANY)
2188                         cor2 |= COR2_IXM;
2189         }
2190
2191         if (tiosp->c_cflag & CCTS_OFLOW)
2192                 cor2 |= COR2_CTSAE;
2193         if (tiosp->c_cflag & CRTS_IFLOW)
2194                 mcor1 |= FIFO_RTSTHRESHOLD;
2195
2196 /*
2197  *      All cd1400 register values calculated so go through and set them
2198  *      all up.
2199  */
2200 #if DEBUG
2201         printf("SETPORT: portnr=%d panelnr=%d brdnr=%d\n", portp->portnr,
2202                 portp->panelnr, portp->brdnr);
2203         printf("    cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n", cor1, cor2,
2204                 cor3, cor4, cor5);
2205         printf("    mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
2206                 mcor1, mcor2, rtpr, sreron, sreroff);
2207         printf("    tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
2208         printf("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
2209                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP], tiosp->c_cc[VSTART],
2210                 tiosp->c_cc[VSTOP]);
2211 #endif
2212
2213         x = spltty();
2214         BRDENABLE(portp->brdnr, portp->pagenr);
2215         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2216         srer = stl_getreg(portp, SRER);
2217         stl_setreg(portp, SRER, 0);
2218         ccr += stl_updatereg(portp, COR1, cor1);
2219         ccr += stl_updatereg(portp, COR2, cor2);
2220         ccr += stl_updatereg(portp, COR3, cor3);
2221         if (ccr) {
2222                 stl_ccrwait(portp);
2223                 stl_setreg(portp, CCR, CCR_CORCHANGE);
2224         }
2225         stl_setreg(portp, COR4, cor4);
2226         stl_setreg(portp, COR5, cor5);
2227         stl_setreg(portp, MCOR1, mcor1);
2228         stl_setreg(portp, MCOR2, mcor2);
2229         if (tiosp->c_ospeed == 0) {
2230                 stl_setreg(portp, MSVR1, 0);
2231         } else {
2232                 stl_setreg(portp, MSVR1, MSVR1_DTR);
2233                 stl_setreg(portp, TCOR, clk);
2234                 stl_setreg(portp, TBPR, div);
2235                 stl_setreg(portp, RCOR, clk);
2236                 stl_setreg(portp, RBPR, div);
2237         }
2238         stl_setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
2239         stl_setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
2240         stl_setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
2241         stl_setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
2242         stl_setreg(portp, RTPR, rtpr);
2243         mcor1 = stl_getreg(portp, MSVR1);
2244         if (mcor1 & MSVR1_DCD)
2245                 portp->sigs |= TIOCM_CD;
2246         else
2247                 portp->sigs &= ~TIOCM_CD;
2248         stl_setreg(portp, SRER, ((srer & ~sreroff) | sreron));
2249         BRDDISABLE(portp->brdnr);
2250         portp->state &= ~(ASY_RTSFLOWMODE | ASY_CTSFLOWMODE);
2251         portp->state |= ((tiosp->c_cflag & CRTS_IFLOW) ? ASY_RTSFLOWMODE : 0);
2252         portp->state |= ((tiosp->c_cflag & CCTS_OFLOW) ? ASY_CTSFLOWMODE : 0);
2253         stl_ttyoptim(portp, tiosp);
2254         splx(x);
2255
2256         return(0);
2257 }
2258
2259 /*****************************************************************************/
2260
2261 /*
2262  *      Action the flow control as required. The hw and sw args inform the
2263  *      routine what flow control methods it should try.
2264  */
2265
2266 static void stl_flowcontrol(stlport_t *portp, int hw, int sw)
2267 {
2268         unsigned char   *head, *tail;
2269         int             len, hwflow, x;
2270
2271 #if DEBUG
2272         printf("stl_flowcontrol(portp=%x,hw=%d,sw=%d)\n", (int) portp, hw, sw);
2273 #endif
2274
2275         hwflow = -1;
2276
2277         if (portp->state & ASY_RTSFLOWMODE) {
2278                 if (hw == 0) {
2279                         if ((portp->state & ASY_RTSFLOW) == 0)
2280                                 hwflow = 0;
2281                 } else if (hw > 0) {
2282                         if (portp->state & ASY_RTSFLOW) {
2283                                 head = portp->rx.head;
2284                                 tail = portp->rx.tail;
2285                                 len = (head >= tail) ? (head - tail) :
2286                                         (STL_RXBUFSIZE - (tail - head));
2287                                 if (len < STL_RXBUFHIGH)
2288                                         hwflow = 1;
2289                         }
2290                 }
2291         }
2292
2293 /*
2294  *      We have worked out what to do, if anything. So now apply it to the
2295  *      UART port.
2296  */
2297         if (hwflow >= 0) {
2298                 x = spltty();
2299                 BRDENABLE(portp->brdnr, portp->pagenr);
2300                 stl_setreg(portp, CAR, (portp->portnr & 0x03));
2301                 if (hwflow == 0) {
2302                         portp->state |= ASY_RTSFLOW;
2303                         stl_setreg(portp, MCOR1,
2304                                 (stl_getreg(portp, MCOR1) & 0xf0));
2305                         stl_setreg(portp, MSVR2, 0);
2306                         portp->stats.rxrtsoff++;
2307                 } else if (hwflow > 0) {
2308                         portp->state &= ~ASY_RTSFLOW;
2309                         stl_setreg(portp, MSVR2, MSVR2_RTS);
2310                         stl_setreg(portp, MCOR1,
2311                                 (stl_getreg(portp, MCOR1) | FIFO_RTSTHRESHOLD));
2312                         portp->stats.rxrtson++;
2313                 }
2314                 BRDDISABLE(portp->brdnr);
2315                 splx(x);
2316         }
2317 }
2318
2319
2320 /*****************************************************************************/
2321
2322 /*
2323  *      Set the state of the DTR and RTS signals.
2324  */
2325
2326 static void stl_setsignals(stlport_t *portp, int dtr, int rts)
2327 {
2328         unsigned char   msvr1, msvr2;
2329         int             x;
2330
2331 #if DEBUG
2332         printf("stl_setsignals(portp=%x,dtr=%d,rts=%d)\n", (int) portp,
2333                 dtr, rts);
2334 #endif
2335
2336         msvr1 = 0;
2337         msvr2 = 0;
2338         if (dtr > 0)
2339                 msvr1 = MSVR1_DTR;
2340         if (rts > 0)
2341                 msvr2 = MSVR2_RTS;
2342
2343         x = spltty();
2344         BRDENABLE(portp->brdnr, portp->pagenr);
2345         stl_setreg(portp, CAR, (portp->portnr & 0x03));
2346         if (rts >= 0)
2347                 stl_setreg(portp, MSVR2, msvr2);
2348         if (dtr >= 0)
2349                 stl_setreg(portp, MSVR1, msvr1);
2350         BRDDISABLE(portp->brdnr);
2351         splx(x);
2352 }
2353
2354 /*****************************************************************************/
2355
2356 /*
2357  *      Get the state of the signals.
2358  */
2359
2360 static int stl_getsignals(stlport_t *portp)
2361 {
2362         unsigned char   msvr1, msvr2;
2363         int             sigs, x;
2364
2365 #if DEBUG
2366         printf("stl_getsignals(portp=%x)\n", (int) portp);
2367 #endif
2368
2369         x = spltty();
2370         BRDENABLE(portp->brdnr, portp->pagenr);
2371         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2372         msvr1 = stl_getreg(portp, MSVR1);
2373         msvr2 = stl_getreg(portp, MSVR2);
2374         BRDDISABLE(portp->brdnr);
2375         splx(x);
2376
2377         sigs = 0;
2378         sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
2379         sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
2380         sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
2381         sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
2382         sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
2383         sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
2384         return(sigs);
2385 }
2386
2387 /*****************************************************************************/
2388
2389 /*
2390  *      Enable or disable the Transmitter and/or Receiver.
2391  */
2392
2393 static void stl_enablerxtx(stlport_t *portp, int rx, int tx)
2394 {
2395         unsigned char   ccr;
2396         int             x;
2397
2398 #if DEBUG
2399         printf("stl_enablerxtx(portp=%x,rx=%d,tx=%d)\n", (int) portp, rx, tx);
2400 #endif
2401
2402         ccr = 0;
2403         if (tx == 0)
2404                 ccr |= CCR_TXDISABLE;
2405         else if (tx > 0)
2406                 ccr |= CCR_TXENABLE;
2407         if (rx == 0)
2408                 ccr |= CCR_RXDISABLE;
2409         else if (rx > 0)
2410                 ccr |= CCR_RXENABLE;
2411
2412         x = spltty();
2413         BRDENABLE(portp->brdnr, portp->pagenr);
2414         stl_setreg(portp, CAR, (portp->portnr & 0x03));
2415         stl_ccrwait(portp);
2416         stl_setreg(portp, CCR, ccr);
2417         stl_ccrwait(portp);
2418         BRDDISABLE(portp->brdnr);
2419         splx(x);
2420 }
2421
2422 /*****************************************************************************/
2423
2424 /*
2425  *      Start or stop the Transmitter and/or Receiver.
2426  */
2427
2428 static void stl_startrxtx(stlport_t *portp, int rx, int tx)
2429 {
2430         unsigned char   sreron, sreroff;
2431         int             x;
2432
2433 #if DEBUG
2434         printf("stl_startrxtx(portp=%x,rx=%d,tx=%d)\n", (int) portp, rx, tx);
2435 #endif
2436
2437         sreron = 0;
2438         sreroff = 0;
2439         if (tx == 0)
2440                 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
2441         else if (tx == 1)
2442                 sreron |= SRER_TXDATA;
2443         else if (tx >= 2)
2444                 sreron |= SRER_TXEMPTY;
2445         if (rx == 0)
2446                 sreroff |= SRER_RXDATA;
2447         else if (rx > 0)
2448                 sreron |= SRER_RXDATA;
2449
2450         x = spltty();
2451         BRDENABLE(portp->brdnr, portp->pagenr);
2452         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2453         stl_setreg(portp, SRER,
2454                 ((stl_getreg(portp, SRER) & ~sreroff) | sreron));
2455         BRDDISABLE(portp->brdnr);
2456         if (tx > 0)
2457                 portp->tty.t_state |= TS_BUSY;
2458         splx(x);
2459 }
2460
2461 /*****************************************************************************/
2462
2463 /*
2464  *      Disable all interrupts from this port.
2465  */
2466
2467 static void stl_disableintrs(stlport_t *portp)
2468 {
2469         int     x;
2470
2471 #if DEBUG
2472         printf("stl_disableintrs(portp=%x)\n", (int) portp);
2473 #endif
2474
2475         x = spltty();
2476         BRDENABLE(portp->brdnr, portp->pagenr);
2477         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2478         stl_setreg(portp, SRER, 0);
2479         BRDDISABLE(portp->brdnr);
2480         splx(x);
2481 }
2482
2483 /*****************************************************************************/
2484
2485 static void stl_sendbreak(stlport_t *portp, long len)
2486 {
2487         int     x;
2488
2489 #if DEBUG
2490         printf("stl_sendbreak(portp=%x,len=%d)\n", (int) portp, (int) len);
2491 #endif
2492
2493         x = spltty();
2494         BRDENABLE(portp->brdnr, portp->pagenr);
2495         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2496         stl_setreg(portp, COR2, (stl_getreg(portp, COR2) | COR2_ETC));
2497         stl_setreg(portp, SRER,
2498                 ((stl_getreg(portp, SRER) & ~SRER_TXDATA) | SRER_TXEMPTY));
2499         BRDDISABLE(portp->brdnr);
2500         if (len > 0) {
2501                 len = len / 5;
2502                 portp->brklen = (len > 255) ? 255 : len;
2503         } else {
2504                 portp->brklen = len;
2505         }
2506         splx(x);
2507         portp->stats.txbreaks++;
2508 }
2509
2510 /*****************************************************************************/
2511
2512 /*
2513  *      Enable l_rint processing bypass mode if tty modes allow it.
2514  */
2515
2516 static void stl_ttyoptim(stlport_t *portp, struct termios *tiosp)
2517 {
2518         struct tty      *tp;
2519
2520         tp = &portp->tty;
2521         if (((tiosp->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR)) == 0) &&
2522             (((tiosp->c_iflag & BRKINT) == 0) || (tiosp->c_iflag & IGNBRK)) &&
2523             (((tiosp->c_iflag & PARMRK) == 0) ||
2524                 ((tiosp->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))) &&
2525             ((tiosp->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) ==0) &&
2526             (linesw[tp->t_line].l_rint == ttyinput))
2527                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2528         else
2529                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2530         portp->hotchar = linesw[tp->t_line].l_hotchar;
2531 }
2532
2533 /*****************************************************************************/
2534
2535 /*
2536  *      Try and find and initialize all the ports on a panel. We don't care
2537  *      what sort of board these ports are on - since the port io registers
2538  *      are almost identical when dealing with ports.
2539  */
2540
2541 static int stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
2542 {
2543         stlport_t       *portp;
2544         unsigned int    chipmask;
2545         unsigned int    gfrcr;
2546         int             nrchips, uartaddr, ioaddr;
2547         int             i, j;
2548
2549 #if DEBUG
2550         printf("stl_initports(panelp=%x)\n", (int) panelp);
2551 #endif
2552
2553         BRDENABLE(panelp->brdnr, panelp->pagenr);
2554
2555 /*
2556  *      Check that each chip is present and started up OK.
2557  */
2558         chipmask = 0;
2559         nrchips = panelp->nrports / CD1400_PORTS;
2560         for (i = 0; (i < nrchips); i++) {
2561                 if (brdp->brdtype == BRD_ECHPCI) {
2562                         outb(brdp->ioctrl, (panelp->pagenr + (i >> 1)));
2563                         ioaddr = panelp->iobase;
2564                 } else {
2565                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
2566                 }
2567                 uartaddr = (i & 0x01) ? 0x080 : 0;
2568                 outb(ioaddr, (GFRCR + uartaddr));
2569                 outb((ioaddr + EREG_DATA), 0);
2570                 outb(ioaddr, (CCR + uartaddr));
2571                 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
2572                 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
2573                 outb(ioaddr, (GFRCR + uartaddr));
2574                 for (j = 0; (j < CCR_MAXWAIT); j++) {
2575                         gfrcr = inb(ioaddr + EREG_DATA);
2576                         if ((gfrcr > 0x40) && (gfrcr < 0x60))
2577                                 break;
2578                 }
2579                 if (j >= CCR_MAXWAIT) {
2580                         printf("STALLION: cd1400 not responding, brd=%d "
2581                                 "panel=%d chip=%d\n", panelp->brdnr,
2582                                 panelp->panelnr, i);
2583                         continue;
2584                 }
2585                 chipmask |= (0x1 << i);
2586                 outb(ioaddr, (PPR + uartaddr));
2587                 outb((ioaddr + EREG_DATA), PPR_SCALAR);
2588         }
2589
2590 /*
2591  *      All cd1400's are initialized (if found!). Now go through and setup
2592  *      each ports data structures. Also init the LIVR register of cd1400
2593  *      for each port.
2594  */
2595         ioaddr = panelp->iobase;
2596         for (i = 0; (i < panelp->nrports); i++) {
2597                 if (brdp->brdtype == BRD_ECHPCI) {
2598                         outb(brdp->ioctrl, (panelp->pagenr + (i >> 3)));
2599                         ioaddr = panelp->iobase;
2600                 } else {
2601                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 3));
2602                 }
2603                 if ((chipmask & (0x1 << (i / 4))) == 0)
2604                         continue;
2605                 portp = (stlport_t *) malloc(sizeof(stlport_t), M_TTYS,
2606                         M_NOWAIT | M_ZERO);
2607                 if (portp == (stlport_t *) NULL) {
2608                         printf("STALLION: failed to allocate port memory "
2609                                 "(size=%d)\n", sizeof(stlport_t));
2610                         break;
2611                 }
2612
2613                 portp->portnr = i;
2614                 portp->brdnr = panelp->brdnr;
2615                 portp->panelnr = panelp->panelnr;
2616                 portp->clk = brdp->clk;
2617                 portp->ioaddr = ioaddr;
2618                 portp->uartaddr = (i & 0x4) << 5;
2619                 portp->pagenr = panelp->pagenr + (i >> 3);
2620                 portp->hwid = stl_getreg(portp, GFRCR);
2621                 stl_setreg(portp, CAR, (i & 0x3));
2622                 stl_setreg(portp, LIVR, (i << 3));
2623                 panelp->ports[i] = portp;
2624
2625                 j = STL_TXBUFSIZE + (2 * STL_RXBUFSIZE);
2626                 portp->tx.buf = (char *) malloc(j, M_TTYS, M_NOWAIT);
2627                 if (portp->tx.buf == (char *) NULL) {
2628                         printf("STALLION: failed to allocate buffer memory "
2629                                 "(size=%d)\n", j);
2630                         break;
2631                 }
2632                 portp->tx.endbuf = portp->tx.buf + STL_TXBUFSIZE;
2633                 portp->tx.head = portp->tx.buf;
2634                 portp->tx.tail = portp->tx.buf;
2635                 portp->rx.buf = portp->tx.buf + STL_TXBUFSIZE;
2636                 portp->rx.endbuf = portp->rx.buf + STL_RXBUFSIZE;
2637                 portp->rx.head = portp->rx.buf;
2638                 portp->rx.tail = portp->rx.buf;
2639                 portp->rxstatus.buf = portp->rx.buf + STL_RXBUFSIZE;
2640                 portp->rxstatus.endbuf = portp->rxstatus.buf + STL_RXBUFSIZE;
2641                 portp->rxstatus.head = portp->rxstatus.buf;
2642                 portp->rxstatus.tail = portp->rxstatus.buf;
2643                 bzero(portp->rxstatus.head, STL_RXBUFSIZE);
2644
2645                 portp->initintios.c_ispeed = STL_DEFSPEED;
2646                 portp->initintios.c_ospeed = STL_DEFSPEED;
2647                 portp->initintios.c_cflag = STL_DEFCFLAG;
2648                 portp->initintios.c_iflag = 0;
2649                 portp->initintios.c_oflag = 0;
2650                 portp->initintios.c_lflag = 0;
2651                 bcopy(&ttydefchars[0], &portp->initintios.c_cc[0],
2652                         sizeof(portp->initintios.c_cc));
2653                 portp->initouttios = portp->initintios;
2654                 portp->dtrwait = 3 * hz;
2655         }
2656
2657         BRDDISABLE(panelp->brdnr);
2658         return(0);
2659 }
2660
2661 /*****************************************************************************/
2662
2663 /*
2664  *      Try to find and initialize an EasyIO board.
2665  */
2666
2667 static int stl_initeio(stlbrd_t *brdp)
2668 {
2669         stlpanel_t      *panelp;
2670         unsigned int    status;
2671
2672 #if DEBUG
2673         printf("stl_initeio(brdp=%x)\n", (int) brdp);
2674 #endif
2675
2676         brdp->ioctrl = brdp->ioaddr1 + 1;
2677         brdp->iostatus = brdp->ioaddr1 + 2;
2678         brdp->clk = EIO_CLK;
2679
2680         status = inb(brdp->iostatus);
2681         switch (status & EIO_IDBITMASK) {
2682         case EIO_8PORTM:
2683                 brdp->clk = EIO_CLK8M;
2684                 /* fall thru */
2685         case EIO_8PORTRS:
2686         case EIO_8PORTDI:
2687                 brdp->nrports = 8;
2688                 break;
2689         case EIO_4PORTRS:
2690                 brdp->nrports = 4;
2691                 break;
2692         default:
2693                 return(ENODEV);
2694         }
2695
2696 /*
2697  *      Check that the supplied IRQ is good and then use it to setup the
2698  *      programmable interrupt bits on EIO board. Also set the edge/level
2699  *      triggered interrupt bit.
2700  */
2701         if ((brdp->irq < 0) || (brdp->irq > 15) ||
2702                         (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2703                 printf("STALLION: invalid irq=%d for brd=%d\n", brdp->irq,
2704                         brdp->brdnr);
2705                 return(EINVAL);
2706         }
2707         outb(brdp->ioctrl, (stl_vecmap[brdp->irq] |
2708                 ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)));
2709
2710         panelp = (stlpanel_t *) malloc(sizeof(stlpanel_t), M_TTYS,
2711                 M_NOWAIT | M_ZERO);
2712         if (panelp == (stlpanel_t *) NULL) {
2713                 printf("STALLION: failed to allocate memory (size=%d)\n",
2714                         sizeof(stlpanel_t));
2715                 return(ENOMEM);
2716         }
2717
2718         panelp->brdnr = brdp->brdnr;
2719         panelp->panelnr = 0;
2720         panelp->nrports = brdp->nrports;
2721         panelp->iobase = brdp->ioaddr1;
2722         panelp->hwid = status;
2723         brdp->panels[0] = panelp;
2724         brdp->nrpanels = 1;
2725         brdp->hwid = status;
2726         brdp->state |= BRD_FOUND;
2727         return(0);
2728 }
2729
2730 /*****************************************************************************/
2731
2732 /*
2733  *      Try to find an ECH board and initialize it. This code is capable of
2734  *      dealing with all types of ECH board.
2735  */
2736
2737 static int stl_initech(stlbrd_t *brdp)
2738 {
2739         stlpanel_t      *panelp;
2740         unsigned int    status, nxtid;
2741         int             panelnr, ioaddr, i;
2742
2743 #if DEBUG
2744         printf("stl_initech(brdp=%x)\n", (int) brdp);
2745 #endif
2746
2747 /*
2748  *      Set up the initial board register contents for boards. This varys a
2749  *      bit between the different board types. So we need to handle each
2750  *      separately. Also do a check that the supplied IRQ is good.
2751  */
2752         if (brdp->brdtype == BRD_ECH) {
2753                 brdp->ioctrl = brdp->ioaddr1 + 1;
2754                 brdp->iostatus = brdp->ioaddr1 + 1;
2755                 status = inb(brdp->iostatus);
2756                 if ((status & ECH_IDBITMASK) != ECH_ID)
2757                         return(ENODEV);
2758                 brdp->hwid = status;
2759
2760                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2761                                 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2762                         printf("STALLION: invalid irq=%d for brd=%d\n",
2763                                 brdp->irq, brdp->brdnr);
2764                         return(EINVAL);
2765                 }
2766                 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2767                 status |= (stl_vecmap[brdp->irq] << 1);
2768                 outb(brdp->ioaddr1, (status | ECH_BRDRESET));
2769                 brdp->ioctrlval = ECH_INTENABLE |
2770                         ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2771                 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
2772                 outb(brdp->ioaddr1, status);
2773         } else if (brdp->brdtype == BRD_ECHMC) {
2774                 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2775                 brdp->iostatus = brdp->ioctrl;
2776                 status = inb(brdp->iostatus);
2777                 if ((status & ECH_IDBITMASK) != ECH_ID)
2778                         return(ENODEV);
2779                 brdp->hwid = status;
2780
2781                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2782                                 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2783                         printf("STALLION: invalid irq=%d for brd=%d\n",
2784                                 brdp->irq, brdp->brdnr);
2785                         return(EINVAL);
2786                 }
2787                 outb(brdp->ioctrl, ECHMC_BRDRESET);
2788                 outb(brdp->ioctrl, ECHMC_INTENABLE);
2789         } else if (brdp->brdtype == BRD_ECHPCI) {
2790                 brdp->ioctrl = brdp->ioaddr1 + 2;
2791         }
2792
2793         brdp->clk = ECH_CLK;
2794
2795 /*
2796  *      Scan through the secondary io address space looking for panels.
2797  *      As we find'em allocate and initialize panel structures for each.
2798  */
2799         ioaddr = brdp->ioaddr2;
2800         panelnr = 0;
2801         nxtid = 0;
2802
2803         for (i = 0; (i < STL_MAXPANELS); i++) {
2804                 if (brdp->brdtype == BRD_ECHPCI) {
2805                         outb(brdp->ioctrl, nxtid);
2806                         ioaddr = brdp->ioaddr2;
2807                 }
2808                 status = inb(ioaddr + ECH_PNLSTATUS);
2809                 if ((status & ECH_PNLIDMASK) != nxtid)
2810                         break;
2811                 panelp = (stlpanel_t *) malloc(sizeof(stlpanel_t), M_TTYS,
2812                         M_NOWAIT | M_ZERO);
2813                 if (panelp == (stlpanel_t *) NULL) {
2814                         printf("STALLION: failed to allocate memory"
2815                                 "(size=%d)\n", sizeof(stlpanel_t));
2816                         break;
2817                 }
2818                 panelp->brdnr = brdp->brdnr;
2819                 panelp->panelnr = panelnr;
2820                 panelp->iobase = ioaddr;
2821                 panelp->pagenr = nxtid;
2822                 panelp->hwid = status;
2823                 if (status & ECH_PNL16PORT) {
2824                         if ((brdp->nrports + 16) > 32)
2825                                 break;
2826                         panelp->nrports = 16;
2827                         panelp->ackmask = 0x80;
2828                         brdp->nrports += 16;
2829                         ioaddr += (EREG_BANKSIZE * 2);
2830                         nxtid += 2;
2831                 } else {
2832                         panelp->nrports = 8;
2833                         panelp->ackmask = 0xc0;
2834                         brdp->nrports += 8;
2835                         ioaddr += EREG_BANKSIZE;
2836                         nxtid++;
2837                 }
2838                 brdp->panels[panelnr++] = panelp;
2839                 brdp->nrpanels++;
2840                 if (ioaddr >= (brdp->ioaddr2 + 0x20))
2841                         break;
2842         }
2843
2844         if (brdp->brdtype == BRD_ECH)
2845                 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
2846
2847         brdp->state |= BRD_FOUND;
2848         return(0);
2849 }
2850
2851 /*****************************************************************************/
2852
2853 /*
2854  *      Initialize and configure the specified board. This firstly probes
2855  *      for the board, if it is found then the board is initialized and
2856  *      then all its ports are initialized as well.
2857  */
2858
2859 static int stl_brdinit(stlbrd_t *brdp)
2860 {
2861         stlpanel_t      *panelp;
2862         int             i, j, k;
2863
2864 #if DEBUG
2865         printf("stl_brdinit(brdp=%x): unit=%d type=%d io1=%x io2=%x irq=%d\n",
2866                 (int) brdp, brdp->brdnr, brdp->brdtype, brdp->ioaddr1,
2867                 brdp->ioaddr2, brdp->irq);
2868 #endif
2869
2870         switch (brdp->brdtype) {
2871         case BRD_EASYIO:
2872                 stl_initeio(brdp);
2873                 break;
2874         case BRD_ECH:
2875         case BRD_ECHMC:
2876         case BRD_ECHPCI:
2877                 stl_initech(brdp);
2878                 break;
2879         default:
2880                 printf("STALLION: unit=%d is unknown board type=%d\n",
2881                         brdp->brdnr, brdp->brdtype);
2882                 return(ENODEV);
2883         }
2884
2885         stl_brds[brdp->brdnr] = brdp;
2886         if ((brdp->state & BRD_FOUND) == 0) {
2887 #if 0
2888                 printf("STALLION: %s board not found, unit=%d io=%x irq=%d\n",
2889                         stl_brdnames[brdp->brdtype], brdp->brdnr,
2890                         brdp->ioaddr1, brdp->irq);
2891 #endif
2892                 return(ENODEV);
2893         }
2894
2895         for (i = 0, k = 0; (i < STL_MAXPANELS); i++) {
2896                 panelp = brdp->panels[i];
2897                 if (panelp != (stlpanel_t *) NULL) {
2898                         stl_initports(brdp, panelp);
2899                         for (j = 0; (j < panelp->nrports); j++)
2900                                 brdp->ports[k++] = panelp->ports[j];
2901                 }
2902         }
2903
2904         printf("stl%d: %s (driver version %s) unit=%d nrpanels=%d nrports=%d\n",
2905                 brdp->unitid, stl_brdnames[brdp->brdtype], stl_drvversion,
2906                 brdp->brdnr, brdp->nrpanels, brdp->nrports);
2907         return(0);
2908 }
2909
2910 /*****************************************************************************/
2911
2912 /*
2913  *      Return the board stats structure to user app.
2914  */
2915
2916 static int stl_getbrdstats(caddr_t data)
2917 {
2918         stlbrd_t        *brdp;
2919         stlpanel_t      *panelp;
2920         int             i;
2921
2922         stl_brdstats = *((combrd_t *) data);
2923         if (stl_brdstats.brd >= STL_MAXBRDS)
2924                 return(-ENODEV);
2925         brdp = stl_brds[stl_brdstats.brd];
2926         if (brdp == (stlbrd_t *) NULL)
2927                 return(-ENODEV);
2928
2929         bzero(&stl_brdstats, sizeof(combrd_t));
2930         stl_brdstats.brd = brdp->brdnr;
2931         stl_brdstats.type = brdp->brdtype;
2932         stl_brdstats.hwid = brdp->hwid;
2933         stl_brdstats.state = brdp->state;
2934         stl_brdstats.ioaddr = brdp->ioaddr1;
2935         stl_brdstats.ioaddr2 = brdp->ioaddr2;
2936         stl_brdstats.irq = brdp->irq;
2937         stl_brdstats.nrpanels = brdp->nrpanels;
2938         stl_brdstats.nrports = brdp->nrports;
2939         for (i = 0; (i < brdp->nrpanels); i++) {
2940                 panelp = brdp->panels[i];
2941                 stl_brdstats.panels[i].panel = i;
2942                 stl_brdstats.panels[i].hwid = panelp->hwid;
2943                 stl_brdstats.panels[i].nrports = panelp->nrports;
2944         }
2945
2946         *((combrd_t *) data) = stl_brdstats;;
2947         return(0);
2948 }
2949
2950 /*****************************************************************************/
2951
2952 /*
2953  *      Resolve the referenced port number into a port struct pointer.
2954  */
2955
2956 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
2957 {
2958         stlbrd_t        *brdp;
2959         stlpanel_t      *panelp;
2960
2961         if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
2962                 return((stlport_t *) NULL);
2963         brdp = stl_brds[brdnr];
2964         if (brdp == (stlbrd_t *) NULL)
2965                 return((stlport_t *) NULL);
2966         if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
2967                 return((stlport_t *) NULL);
2968         panelp = brdp->panels[panelnr];
2969         if (panelp == (stlpanel_t *) NULL)
2970                 return((stlport_t *) NULL);
2971         if ((portnr < 0) || (portnr >= panelp->nrports))
2972                 return((stlport_t *) NULL);
2973         return(panelp->ports[portnr]);
2974 }
2975
2976 /*****************************************************************************/
2977
2978 /*
2979  *      Return the port stats structure to user app. A NULL port struct
2980  *      pointer passed in means that we need to find out from the app
2981  *      what port to get stats for (used through board control device).
2982  */
2983
2984 static int stl_getportstats(stlport_t *portp, caddr_t data)
2985 {
2986         unsigned char   *head, *tail;
2987
2988         if (portp == (stlport_t *) NULL) {
2989                 stl_comstats = *((comstats_t *) data);
2990                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2991                         stl_comstats.port);
2992                 if (portp == (stlport_t *) NULL)
2993                         return(-ENODEV);
2994         }
2995
2996         portp->stats.state = portp->state;
2997         /*portp->stats.flags = portp->flags;*/
2998         portp->stats.hwid = portp->hwid;
2999         portp->stats.ttystate = portp->tty.t_state;
3000         portp->stats.cflags = portp->tty.t_cflag;
3001         portp->stats.iflags = portp->tty.t_iflag;
3002         portp->stats.oflags = portp->tty.t_oflag;
3003         portp->stats.lflags = portp->tty.t_lflag;
3004
3005         head = portp->tx.head;
3006         tail = portp->tx.tail;
3007         portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
3008                 (STL_TXBUFSIZE - (tail - head)));
3009
3010         head = portp->rx.head;
3011         tail = portp->rx.tail;
3012         portp->stats.rxbuffered = (head >= tail) ? (head - tail) :
3013                 (STL_RXBUFSIZE - (tail - head));
3014
3015         portp->stats.signals = (unsigned long) stl_getsignals(portp);
3016
3017         *((comstats_t *) data) = portp->stats;
3018         return(0);
3019 }
3020
3021 /*****************************************************************************/
3022
3023 /*
3024  *      Clear the port stats structure. We also return it zeroed out...
3025  */
3026
3027 static int stl_clrportstats(stlport_t *portp, caddr_t data)
3028 {
3029         if (portp == (stlport_t *) NULL) {
3030                 stl_comstats = *((comstats_t *) data);
3031                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
3032                         stl_comstats.port);
3033                 if (portp == (stlport_t *) NULL)
3034                         return(-ENODEV);
3035         }
3036
3037         bzero(&portp->stats, sizeof(comstats_t));
3038         portp->stats.brd = portp->brdnr;
3039         portp->stats.panel = portp->panelnr;
3040         portp->stats.port = portp->portnr;
3041         *((comstats_t *) data) = stl_comstats;
3042         return(0);
3043 }
3044
3045 /*****************************************************************************/
3046
3047 /*
3048  *      The "staliomem" device is used for stats collection in this driver.
3049  */
3050
3051 static int stl_memioctl(dev_t dev, unsigned long cmd, caddr_t data, int flag,
3052                         struct proc *p)
3053 {
3054         stlbrd_t        *brdp;
3055         int             brdnr, rc;
3056
3057 #if DEBUG
3058         printf("stl_memioctl(dev=%s,cmd=%lx,data=%p,flag=%x)\n",
3059                 devtoname(dev), cmd, (void *) data, flag);
3060 #endif
3061
3062         brdnr = minor(dev) & 0x7;
3063         brdp = stl_brds[brdnr];
3064         if (brdp == (stlbrd_t *) NULL)
3065                 return(ENODEV);
3066         if (brdp->state == 0)
3067                 return(ENODEV);
3068
3069         rc = 0;
3070
3071         switch (cmd) {
3072         case COM_GETPORTSTATS:
3073                 rc = stl_getportstats((stlport_t *) NULL, data);
3074                 break;
3075         case COM_CLRPORTSTATS:
3076                 rc = stl_clrportstats((stlport_t *) NULL, data);
3077                 break;
3078         case COM_GETBRDSTATS:
3079                 rc = stl_getbrdstats(data);
3080                 break;
3081         default:
3082                 rc = ENOTTY;
3083                 break;
3084         }
3085
3086         return(rc);
3087 }
3088
3089 /*****************************************************************************/