kernel: Remove most definitions of CDEV_MAJOR.
[dragonfly.git] / sys / dev / serial / stli / istallion.c
1 /*****************************************************************************/
2
3 /*
4  * istallion.c  -- stallion intelligent multiport serial driver.
5  *
6  * Copyright (c) 1994-1998 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: src/sys/i386/isa/istallion.c,v 1.36.2.2 2001/08/30 12:29:57 murray Exp $
37  */
38
39 /*****************************************************************************/
40
41 #include "opt_compat.h"
42
43 #define TTYDEFCHARS     1
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/proc.h>
51 #include <sys/priv.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/uio.h>
55 #include <sys/thread2.h>
56 #include <machine/clock.h>
57 #include <vm/vm.h>
58 #include <vm/pmap.h>
59 #include <bus/isa/isa_device.h>
60 #include <machine/cdk.h>
61 #include <machine/comstats.h>
62
63 #undef STLDEBUG
64
65 /*****************************************************************************/
66
67 /*
68  *      Define the version level of the kernel - so we can compile in the
69  *      appropriate bits of code. By default this will compile for a 2.1
70  *      level kernel.
71  */
72 #define VFREEBSD        220
73
74 #if VFREEBSD >= 220
75 #define STATIC          static
76 #else
77 #define STATIC
78 #endif
79
80 /*****************************************************************************/
81
82 /*
83  *      Define different board types. Not all of the following board types
84  *      are supported by this driver. But I will use the standard "assigned"
85  *      board numbers. Currently supported boards are abbreviated as:
86  *      ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
87  *      STAL = Stallion.
88  */
89 #define BRD_UNKNOWN     0
90 #define BRD_STALLION    1
91 #define BRD_BRUMBY4     2
92 #define BRD_ONBOARD2    3
93 #define BRD_ONBOARD     4
94 #define BRD_BRUMBY8     5
95 #define BRD_BRUMBY16    6
96 #define BRD_ONBOARDE    7
97 #define BRD_ONBOARD32   9
98 #define BRD_ONBOARD2_32 10
99 #define BRD_ONBOARDRS   11
100 #define BRD_EASYIO      20
101 #define BRD_ECH         21
102 #define BRD_ECHMC       22
103 #define BRD_ECP         23
104 #define BRD_ECPE        24
105 #define BRD_ECHPCI      26
106 #define BRD_ECH64PCI    27
107 #define BRD_EASYIOPCI   28
108
109 #define BRD_BRUMBY      BRD_BRUMBY4
110
111 /*****************************************************************************/
112
113 /*
114  *      Define important driver limitations.
115  */
116 #define STL_MAXBRDS             8
117 #define STL_MAXPANELS           4
118 #define STL_PORTSPERPANEL       16
119 #define STL_PORTSPERBRD         64
120
121 #define STL_MAXCHANS            STL_PORTSPERBRD
122
123
124 /*
125  *      Define the important minor number break down bits. These have been
126  *      chosen to be "compatible" with the standard sio driver minor numbers.
127  *      Extra high bits are used to distinguish between boards and also for
128  *      really high port numbers (> 32).
129  */
130 #define STL_CALLOUTDEV  0x80
131 #define STL_CTRLLOCK    0x40
132 #define STL_CTRLINIT    0x20
133 #define STL_CTRLDEV     (STL_CTRLLOCK | STL_CTRLINIT)
134
135 #define STL_MEMDEV      0x07000000
136
137 #define STL_DEFSPEED    TTYDEF_SPEED
138 #define STL_DEFCFLAG    (CS8 | CREAD | HUPCL)
139
140 /*****************************************************************************/
141
142 /*
143  *      Define our local driver identity first. Set up stuff to deal with
144  *      all the local structures required by a serial tty driver.
145  */
146 static char             stli_drvname[] = "stli";
147 static char const       stli_drvtitle[] = "Stallion Multiport Serial Driver";
148 static char const       stli_drvversion[] = "2.0.0";
149
150 static int      stli_nrbrds = 0;
151 static int      stli_doingtimeout = 0;
152 static struct callout   stli_poll_ch;
153
154 /*
155  *      Define some macros to use to class define boards.
156  */
157 #define BRD_ISA         0x1
158 #define BRD_EISA        0x2
159 #define BRD_PCI         0x8
160
161 static unsigned char    stli_stliprobed[STL_MAXBRDS];
162
163 /*****************************************************************************/
164
165 /*
166  *      Define a set of structures to hold all the board/panel/port info
167  *      for our ports. These will be dynamically allocated as required at
168  *      driver initialization time.
169  */
170
171 /*
172  *      Port and board structures to hold status info about each object.
173  *      The board structure contains pointers to structures for each port
174  *      connected to it. Panels are not distinguished here, since
175  *      communication with the slave board will always be on a per port
176  *      basis.
177  */
178 typedef struct {
179         struct tty      tty;
180         int             portnr;
181         int             panelnr;
182         int             brdnr;
183         int             ioaddr;
184         int             callout;
185         int             devnr;
186         int             dtrwait;
187         int             dotimestamp;
188         int             waitopens;
189         int             hotchar;
190         int             rc;
191         int             argsize;
192         void            *argp;
193         unsigned int    state;
194         unsigned int    sigs;
195         struct termios  initintios;
196         struct termios  initouttios;
197         struct termios  lockintios;
198         struct termios  lockouttios;
199         struct timeval  timestamp;
200         asysigs_t       asig;
201         unsigned long   addr;
202         unsigned long   rxlost;
203         unsigned long   rxoffset;
204         unsigned long   txoffset;
205         unsigned long   pflag;
206         unsigned int    rxsize;
207         unsigned int    txsize;
208         unsigned char   reqidx;
209         unsigned char   reqbit;
210         unsigned char   portidx;
211         unsigned char   portbit;
212         struct callout  dtr_ch;
213 } stliport_t;
214
215 /*
216  *      Use a structure of function pointers to do board level operations.
217  *      These include, enable/disable, paging shared memory, interrupting, etc.
218  */
219 typedef struct stlibrd {
220         int             brdnr;
221         int             brdtype;
222         int             unitid;
223         int             state;
224         int             nrpanels;
225         int             nrports;
226         int             nrdevs;
227         unsigned int    iobase;
228         unsigned long   paddr;
229         void            *vaddr;
230         int             memsize;
231         int             pagesize;
232         int             hostoffset;
233         int             slaveoffset;
234         int             bitsize;
235         int             confbits;
236         void            (*init)(struct stlibrd *brdp);
237         void            (*enable)(struct stlibrd *brdp);
238         void            (*reenable)(struct stlibrd *brdp);
239         void            (*disable)(struct stlibrd *brdp);
240         void            (*intr)(struct stlibrd *brdp);
241         void            (*reset)(struct stlibrd *brdp);
242         char            *(*getmemptr)(struct stlibrd *brdp,
243                                 unsigned long offset, int line);
244         int             panels[STL_MAXPANELS];
245         int             panelids[STL_MAXPANELS];
246         stliport_t      *ports[STL_PORTSPERBRD];
247 } stlibrd_t;
248
249 static stlibrd_t        *stli_brds[STL_MAXBRDS];
250
251 static int              stli_shared = 0;
252
253 /*
254  *      Keep a local char buffer for processing chars into the LD. We
255  *      do this to avoid copying from the boards shared memory one char
256  *      at a time.
257  */
258 static int              stli_rxtmplen;
259 static stliport_t       *stli_rxtmpport;
260 static char             stli_rxtmpbuf[TTYHOG];
261
262 /*
263  *      Define global stats structures. Not used often, and can be re-used
264  *      for each stats call.
265  */
266 static comstats_t       stli_comstats;
267 static combrd_t         stli_brdstats;
268 static asystats_t       stli_cdkstats;
269
270 /*
271  *      Per board state flags. Used with the state field of the board struct.
272  *      Not really much here... All we need to do is keep track of whether
273  *      the board has been detected, and whether it is actully running a slave
274  *      or not.
275  */
276 #define BST_FOUND       0x1
277 #define BST_STARTED     0x2
278
279 /*
280  *      Define the set of port state flags. These are marked for internal
281  *      state purposes only, usually to do with the state of communications
282  *      with the slave. They need to be updated atomically.
283  */
284 #define ST_INITIALIZING 0x1
285 #define ST_INITIALIZED  0x2
286 #define ST_OPENING      0x4
287 #define ST_CLOSING      0x8
288 #define ST_CMDING       0x10
289 #define ST_RXING        0x20
290 #define ST_TXBUSY       0x40
291 #define ST_DOFLUSHRX    0x80
292 #define ST_DOFLUSHTX    0x100
293 #define ST_DOSIGS       0x200
294 #define ST_GETSIGS      0x400
295 #define ST_DTRWAIT      0x800
296
297 /*
298  *      Define an array of board names as printable strings. Handy for
299  *      referencing boards when printing trace and stuff.
300  */
301 static char     *stli_brdnames[] = {
302         "Unknown",
303         "Stallion",
304         "Brumby",
305         "ONboard-MC",
306         "ONboard",
307         "Brumby",
308         "Brumby",
309         "ONboard-EI",
310         NULL,
311         "ONboard",
312         "ONboard-MC",
313         "ONboard-MC",
314         NULL,
315         NULL,
316         NULL,
317         NULL,
318         NULL,
319         NULL,
320         NULL,
321         NULL,
322         "EasyIO",
323         "EC8/32-AT",
324         "EC8/32-MC",
325         "EC8/64-AT",
326         "EC8/64-EI",
327         "EC8/64-MC",
328         "EC8/32-PCI",
329 };
330
331 /*****************************************************************************/
332
333 /*
334  *      Hardware configuration info for ECP boards. These defines apply
335  *      to the directly accessable io ports of the ECP. There is a set of
336  *      defines for each ECP board type, ISA and EISA.
337  */
338 #define ECP_IOSIZE      4
339 #define ECP_MEMSIZE     (128 * 1024)
340 #define ECP_ATPAGESIZE  (4 * 1024)
341 #define ECP_EIPAGESIZE  (64 * 1024)
342
343 #define STL_EISAID      0x8c4e
344
345 /*
346  *      Important defines for the ISA class of ECP board.
347  */
348 #define ECP_ATIREG      0
349 #define ECP_ATCONFR     1
350 #define ECP_ATMEMAR     2
351 #define ECP_ATMEMPR     3
352 #define ECP_ATSTOP      0x1
353 #define ECP_ATINTENAB   0x10
354 #define ECP_ATENABLE    0x20
355 #define ECP_ATDISABLE   0x00
356 #define ECP_ATADDRMASK  0x3f000
357 #define ECP_ATADDRSHFT  12
358
359 /*
360  *      Important defines for the EISA class of ECP board.
361  */
362 #define ECP_EIIREG      0
363 #define ECP_EIMEMARL    1
364 #define ECP_EICONFR     2
365 #define ECP_EIMEMARH    3
366 #define ECP_EIENABLE    0x1
367 #define ECP_EIDISABLE   0x0
368 #define ECP_EISTOP      0x4
369 #define ECP_EIEDGE      0x00
370 #define ECP_EILEVEL     0x80
371 #define ECP_EIADDRMASKL 0x00ff0000
372 #define ECP_EIADDRSHFTL 16
373 #define ECP_EIADDRMASKH 0xff000000
374 #define ECP_EIADDRSHFTH 24
375 #define ECP_EIBRDENAB   0xc84
376
377 #define ECP_EISAID      0x4
378
379 /*
380  *      Important defines for the Micro-channel class of ECP board.
381  *      (It has a lot in common with the ISA boards.)
382  */
383 #define ECP_MCIREG      0
384 #define ECP_MCCONFR     1
385 #define ECP_MCSTOP      0x20
386 #define ECP_MCENABLE    0x80
387 #define ECP_MCDISABLE   0x00
388
389 /*
390  *      Hardware configuration info for ONboard and Brumby boards. These
391  *      defines apply to the directly accessable io ports of these boards.
392  */
393 #define ONB_IOSIZE      16
394 #define ONB_MEMSIZE     (64 * 1024)
395 #define ONB_ATPAGESIZE  (64 * 1024)
396 #define ONB_MCPAGESIZE  (64 * 1024)
397 #define ONB_EIMEMSIZE   (128 * 1024)
398 #define ONB_EIPAGESIZE  (64 * 1024)
399
400 /*
401  *      Important defines for the ISA class of ONboard board.
402  */
403 #define ONB_ATIREG      0
404 #define ONB_ATMEMAR     1
405 #define ONB_ATCONFR     2
406 #define ONB_ATSTOP      0x4
407 #define ONB_ATENABLE    0x01
408 #define ONB_ATDISABLE   0x00
409 #define ONB_ATADDRMASK  0xff0000
410 #define ONB_ATADDRSHFT  16
411
412 #define ONB_HIMEMENAB   0x02
413
414 /*
415  *      Important defines for the EISA class of ONboard board.
416  */
417 #define ONB_EIIREG      0
418 #define ONB_EIMEMARL    1
419 #define ONB_EICONFR     2
420 #define ONB_EIMEMARH    3
421 #define ONB_EIENABLE    0x1
422 #define ONB_EIDISABLE   0x0
423 #define ONB_EISTOP      0x4
424 #define ONB_EIEDGE      0x00
425 #define ONB_EILEVEL     0x80
426 #define ONB_EIADDRMASKL 0x00ff0000
427 #define ONB_EIADDRSHFTL 16
428 #define ONB_EIADDRMASKH 0xff000000
429 #define ONB_EIADDRSHFTH 24
430 #define ONB_EIBRDENAB   0xc84
431
432 #define ONB_EISAID      0x1
433
434 /*
435  *      Important defines for the Brumby boards. They are pretty simple,
436  *      there is not much that is programmably configurable.
437  */
438 #define BBY_IOSIZE      16
439 #define BBY_MEMSIZE     (64 * 1024)
440 #define BBY_PAGESIZE    (16 * 1024)
441
442 #define BBY_ATIREG      0
443 #define BBY_ATCONFR     1
444 #define BBY_ATSTOP      0x4
445
446 /*
447  *      Important defines for the Stallion boards. They are pretty simple,
448  *      there is not much that is programmably configurable.
449  */
450 #define STAL_IOSIZE     16
451 #define STAL_MEMSIZE    (64 * 1024)
452 #define STAL_PAGESIZE   (64 * 1024)
453
454 /*
455  *      Define the set of status register values for EasyConnection panels.
456  *      The signature will return with the status value for each panel. From
457  *      this we can determine what is attached to the board - before we have
458  *      actually down loaded any code to it.
459  */
460 #define ECH_PNLSTATUS   2
461 #define ECH_PNL16PORT   0x20
462 #define ECH_PNLIDMASK   0x07
463 #define ECH_PNLXPID     0x40
464 #define ECH_PNLINTRPEND 0x80
465
466 /*
467  *      Define some macros to do things to the board. Even those these boards
468  *      are somewhat related there is often significantly different ways of
469  *      doing some operation on it (like enable, paging, reset, etc). So each
470  *      board class has a set of functions which do the commonly required
471  *      operations. The macros below basically just call these functions,
472  *      generally checking for a NULL function - which means that the board
473  *      needs nothing done to it to achieve this operation!
474  */
475 #define EBRDINIT(brdp)                                  \
476         if (brdp->init != NULL)                         \
477                 (* brdp->init)(brdp)
478
479 #define EBRDENABLE(brdp)                                \
480         if (brdp->enable != NULL)                       \
481                 (* brdp->enable)(brdp);
482
483 #define EBRDDISABLE(brdp)                               \
484         if (brdp->disable != NULL)                      \
485                 (* brdp->disable)(brdp);
486
487 #define EBRDINTR(brdp)                                  \
488         if (brdp->intr != NULL)                         \
489                 (* brdp->intr)(brdp);
490
491 #define EBRDRESET(brdp)                                 \
492         if (brdp->reset != NULL)                        \
493                 (* brdp->reset)(brdp);
494
495 #define EBRDGETMEMPTR(brdp,offset)                      \
496         (* brdp->getmemptr)(brdp, offset, __LINE__)
497
498 /*
499  *      Define the maximal baud rate.
500  */
501 #define STL_MAXBAUD     230400
502
503 /*****************************************************************************/
504
505 /*
506  *      Define macros to extract a brd and port number from a minor number.
507  *      This uses the extended minor number range in the upper 2 bytes of
508  *      the device number. This gives us plenty of minor numbers to play
509  *      with...
510  */
511 #define MKDEV2BRD(m)    ((minor(m) & 0x00700000) >> 20)
512 #define MKDEV2PORT(m)   ((minor(m) & 0x1f) | ((minor(m) & 0x00010000) >> 11))
513
514 /*
515  *      Define some handy local macros...
516  */
517 #ifndef MIN
518 #define MIN(a,b)        (((a) <= (b)) ? (a) : (b))
519 #endif
520
521 /*****************************************************************************/
522
523 /*
524  *      Declare all those functions in this driver!  First up is the set of
525  *      externally visible functions.
526  */
527 static int      stliprobe(struct isa_device *idp);
528 static int      stliattach(struct isa_device *idp);
529
530 STATIC  d_open_t        stliopen;
531 STATIC  d_close_t       stliclose;
532 STATIC  d_read_t        stliread;
533 STATIC  d_write_t       stliwrite;
534 STATIC  d_ioctl_t       stliioctl;
535
536 /*
537  *      Internal function prototypes.
538  */
539 static stliport_t *stli_dev2port(cdev_t dev);
540 static int      stli_isaprobe(struct isa_device *idp);
541 static int      stli_eisaprobe(struct isa_device *idp);
542 static int      stli_brdinit(stlibrd_t *brdp);
543 static int      stli_brdattach(stlibrd_t *brdp);
544 static int      stli_initecp(stlibrd_t *brdp);
545 static int      stli_initonb(stlibrd_t *brdp);
546 static int      stli_initports(stlibrd_t *brdp);
547 static int      stli_startbrd(stlibrd_t *brdp);
548 static void     stli_poll(void *arg);
549 static __inline void    stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp);
550 static __inline int     stli_hostcmd(stlibrd_t *brdp, stliport_t *portp);
551 static __inline void    stli_dodelaycmd(stliport_t *portp,
552                                         volatile cdkctrl_t *cp);
553 static void     stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
554 static long     stli_mktiocm(unsigned long sigvalue);
555 static void     stli_rxprocess(stlibrd_t *brdp, stliport_t *portp);
556 static void     stli_flush(stliport_t *portp, int flag);
557 static void     stli_start(struct tty *tp);
558 static void     stli_stop(struct tty *tp, int rw);
559 static int      stli_param(struct tty *tp, struct termios *tiosp);
560 static void     stli_ttyoptim(stliport_t *portp, struct termios *tiosp);
561 static void     stli_dtrwakeup(void *arg);
562 static int      stli_initopen(stliport_t *portp);
563 static int      stli_shutdownclose(stliport_t *portp);
564 static int      stli_rawopen(stlibrd_t *brdp, stliport_t *portp,
565                         unsigned long arg, int wait);
566 static int      stli_rawclose(stlibrd_t *brdp, stliport_t *portp,
567                         unsigned long arg, int wait);
568 static int      stli_cmdwait(stlibrd_t *brdp, stliport_t *portp,
569                         unsigned long cmd, void *arg, int size, int copyback);
570 static void     stli_sendcmd(stlibrd_t *brdp, stliport_t *portp,
571                         unsigned long cmd, void *arg, int size, int copyback);
572 static void     stli_mkasyport(stliport_t *portp, asyport_t *pp,
573                         struct termios *tiosp);
574 static int      stli_memrw(cdev_t dev, struct uio *uiop, int flag);
575 static int      stli_memioctl(cdev_t dev, unsigned long cmd, caddr_t data,
576                         int flag);
577 static int      stli_getbrdstats(caddr_t data);
578 static int      stli_getportstats(stliport_t *portp, caddr_t data);
579 static int      stli_clrportstats(stliport_t *portp, caddr_t data);
580 static stliport_t *stli_getport(int brdnr, int panelnr, int portnr);
581
582 static void     stli_ecpinit(stlibrd_t *brdp);
583 static void     stli_ecpenable(stlibrd_t *brdp);
584 static void     stli_ecpdisable(stlibrd_t *brdp);
585 static void     stli_ecpreset(stlibrd_t *brdp);
586 static char     *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset,
587                         int line);
588 static void     stli_ecpintr(stlibrd_t *brdp);
589 static void     stli_ecpeiinit(stlibrd_t *brdp);
590 static void     stli_ecpeienable(stlibrd_t *brdp);
591 static void     stli_ecpeidisable(stlibrd_t *brdp);
592 static void     stli_ecpeireset(stlibrd_t *brdp);
593 static char     *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset,
594                         int line);
595 static void     stli_onbinit(stlibrd_t *brdp);
596 static void     stli_onbenable(stlibrd_t *brdp);
597 static void     stli_onbdisable(stlibrd_t *brdp);
598 static void     stli_onbreset(stlibrd_t *brdp);
599 static char     *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset,
600                         int line);
601 static void     stli_onbeinit(stlibrd_t *brdp);
602 static void     stli_onbeenable(stlibrd_t *brdp);
603 static void     stli_onbedisable(stlibrd_t *brdp);
604 static void     stli_onbereset(stlibrd_t *brdp);
605 static char     *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset,
606                         int line);
607 static void     stli_bbyinit(stlibrd_t *brdp);
608 static void     stli_bbyreset(stlibrd_t *brdp);
609 static char     *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset,
610                         int line);
611 static void     stli_stalinit(stlibrd_t *brdp);
612 static void     stli_stalreset(stlibrd_t *brdp);
613 static char     *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset,
614                         int line);
615
616 /*****************************************************************************/
617
618 /*
619  *      Declare the driver isa structure.
620  */
621 struct isa_driver       stlidriver = {
622         stliprobe, stliattach, stli_drvname
623 };
624
625 /*****************************************************************************/
626
627 #if VFREEBSD >= 220
628
629 /*
630  *      FreeBSD-2.2+ kernel linkage.
631  */
632
633 static struct dev_ops stli_ops = {
634         { stli_drvname, 0, D_TTY },
635         .d_open =       stliopen,
636         .d_close =      stliclose,
637         .d_read =       stliread,
638         .d_write =      stliwrite,
639         .d_ioctl =      stliioctl,
640         .d_kqfilter =   ttykqfilter,
641         .d_revoke =     ttyrevoke
642 };
643
644 #endif
645
646 /*****************************************************************************/
647
648 static stlibrd_t *stli_brdalloc(void)
649 {
650         stlibrd_t       *brdp;
651
652         brdp = kmalloc(sizeof(stlibrd_t), M_TTYS, M_WAITOK | M_ZERO);
653         return(brdp);
654 }
655
656 /*****************************************************************************/
657
658 /*
659  *      Find an available internal board number (unit number). The problem
660  *      is that the same unit numbers can be assigned to different class
661  *      boards - but we only want to maintain one setup board structures.
662  */
663
664 static int stli_findfreeunit(void)
665 {
666         int     i;
667
668         for (i = 0; (i < STL_MAXBRDS); i++)
669                 if (stli_brds[i] == NULL)
670                         break;
671         return((i >= STL_MAXBRDS) ? -1 : i);
672 }
673
674 /*****************************************************************************/
675
676 /*
677  *      Try and determine the ISA board type. Hopefully the board
678  *      configuration entry will help us out, using the flags field.
679  *      If not, we may ne be able to determine the board type...
680  */
681
682 static int stli_isaprobe(struct isa_device *idp)
683 {
684         int     btype;
685
686 #if STLDEBUG
687         kprintf("stli_isaprobe(idp=%x): unit=%d iobase=%x flags=%x\n",
688                 (int) idp, idp->id_unit, idp->id_iobase, idp->id_flags);
689 #endif
690
691         switch (idp->id_flags) {
692         case BRD_STALLION:
693         case BRD_BRUMBY4:
694         case BRD_BRUMBY8:
695         case BRD_BRUMBY16:
696         case BRD_ONBOARD:
697         case BRD_ONBOARD32:
698         case BRD_ECP:
699                 btype = idp->id_flags;
700                 break;
701         default:
702                 btype = 0;
703                 break;
704         }
705         return(btype);
706 }
707
708 /*****************************************************************************/
709
710 /*
711  *      Probe for an EISA board type. We should be able to read the EISA ID,
712  *      that will tell us if a board is present or not...
713  */
714
715 static int stli_eisaprobe(struct isa_device *idp)
716 {
717         int     btype, eid;
718
719 #if STLDEBUG
720         kprintf("stli_eisaprobe(idp=%x): unit=%d iobase=%x flags=%x\n",
721                 (int) idp, idp->id_unit, idp->id_iobase, idp->id_flags);
722 #endif
723
724 /*
725  *      Firstly check if this is an EISA system. Do this by probing for
726  *      the system board EISA ID. If this is not an EISA system then
727  *      don't bother going any further!
728  */
729         outb(0xc80, 0xff);
730         if (inb(0xc80) == 0xff)
731                 return(0);
732
733 /*
734  *      Try and read the EISA ID from the board at specified address.
735  *      If one is present it will tell us the board type as well.
736  */
737         outb((idp->id_iobase + 0xc80), 0xff);
738         eid = inb(idp->id_iobase + 0xc80);
739         eid |= inb(idp->id_iobase + 0xc81) << 8;
740         if (eid != STL_EISAID)
741                 return(0);
742
743         btype = 0;
744         eid = inb(idp->id_iobase + 0xc82);
745         if (eid == ECP_EISAID)
746                 btype = BRD_ECPE;
747         else if (eid == ONB_EISAID)
748                 btype = BRD_ONBOARDE;
749
750         outb((idp->id_iobase + 0xc84), 0x1);
751         return(btype);
752 }
753
754 /*****************************************************************************/
755
756 /*
757  *      Probe for a board. This is involved, since we need to enable the
758  *      shared memory region to see if the board is really there or not...
759  */
760
761 static int stliprobe(struct isa_device *idp)
762 {
763         stlibrd_t       *brdp;
764         int             btype, bclass;
765
766 #if STLDEBUG
767         kprintf("stliprobe(idp=%x): unit=%d iobase=%x flags=%x\n", (int) idp,
768                 idp->id_unit, idp->id_iobase, idp->id_flags);
769 #endif
770
771         if (idp->id_unit > STL_MAXBRDS)
772                 return(0);
773
774 /*
775  *      First up determine what bus type of board we might be dealing
776  *      with. It is easy to separate out the ISA from the EISA
777  *      boards, based on their IO addresses.
778  */
779         bclass = 0;
780         if ((idp->id_iobase > 0) && (idp->id_iobase < 0x400))
781                 bclass |= BRD_ISA;
782         else if ((idp->id_iobase & ~0xf000) == 0)
783                 bclass |= BRD_EISA;
784
785         if ((bclass == 0) || (idp->id_iobase == 0))
786                 return(0);
787
788 /*
789  *      Based on the board bus type, try and figure out what it might be...
790  */
791         btype = 0;
792         if (bclass & BRD_ISA)
793                 btype = stli_isaprobe(idp);
794         if ((btype == 0) && (bclass & BRD_EISA))
795                 btype = stli_eisaprobe(idp);
796         if (btype == 0)
797                 return(0);
798
799 /*
800  *      Go ahead and try probing for the shared memory region now.
801  *      This way we will really know if the board is here...
802  */
803         if ((brdp = stli_brdalloc()) == NULL)
804                 return(0);
805
806         brdp->brdnr = stli_findfreeunit();
807         brdp->brdtype = btype;
808         brdp->unitid = idp->id_unit;
809         brdp->iobase = idp->id_iobase;
810         brdp->vaddr = idp->id_maddr;
811         brdp->paddr = vtophys(idp->id_maddr);
812
813 #if STLDEBUG
814         kprintf("%s(%d): btype=%x unit=%d brd=%d io=%x mem=%lx(%p)\n",
815                 __file__, __LINE__, btype, brdp->unitid, brdp->brdnr,
816                 brdp->iobase, brdp->paddr, (void *) brdp->vaddr);
817 #endif
818
819         stli_stliprobed[idp->id_unit] = brdp->brdnr;
820         stli_brdinit(brdp);
821         if ((brdp->state & BST_FOUND) == 0) {
822                 stli_brds[brdp->brdnr] = NULL;
823                 return(0);
824         }
825         stli_nrbrds++;
826         return(1);
827 }
828
829 /*****************************************************************************/
830
831 /*
832  *      Allocate resources for and initialize a board.
833  */
834
835 static int stliattach(struct isa_device *idp)
836 {
837         stlibrd_t       *brdp;
838         int             brdnr;
839
840 #if STLDEBUG
841         kprintf("stliattach(idp=%p): unit=%d iobase=%x\n", (void *) idp,
842                 idp->id_unit, idp->id_iobase);
843 #endif
844
845         brdnr = stli_stliprobed[idp->id_unit];
846         brdp = stli_brds[brdnr];
847         if (brdp == NULL)
848                 return(0);
849         if (brdp->state & BST_FOUND)
850                 stli_brdattach(brdp);
851         return(1);
852 }
853
854
855 /*****************************************************************************/
856
857 STATIC int stliopen(struct dev_open_args *ap)
858 {
859         cdev_t dev = ap->a_head.a_dev;
860         struct tty      *tp;
861         stliport_t      *portp;
862         int             error, callout;
863
864 #if STLDEBUG
865         kprintf("stliopen(dev=%x,flag=%x,mode=%x,p=%x)\n", (int) dev, flag,
866                 mode, (int) p);
867 #endif
868
869 /*
870  *      Firstly check if the supplied device number is a valid device.
871  */
872         if (minor(dev) & STL_MEMDEV)
873                 return(0);
874
875         portp = stli_dev2port(dev);
876         if (portp == NULL)
877                 return(ENXIO);
878         if (minor(dev) & STL_CTRLDEV)
879                 return(0);
880         tp = &portp->tty;
881         dev->si_tty = tp;
882         callout = minor(dev) & STL_CALLOUTDEV;
883         error = 0;
884
885         crit_enter();
886
887 stliopen_restart:
888 /*
889  *      Wait here for the DTR drop timeout period to expire.
890  */
891         while (portp->state & ST_DTRWAIT) {
892                 error = tsleep(&portp->dtrwait, PCATCH, "stlidtr", 0);
893                 if (error)
894                         goto stliopen_end;
895         }
896
897 /*
898  *      If the port is in its raw hardware initialization phase, then
899  *      hold up here 'till it is done.
900  */
901         while (portp->state & (ST_INITIALIZING | ST_CLOSING)) {
902                 error = tsleep(&portp->state, PCATCH, "stliraw", 0);
903                 if (error)
904                         goto stliopen_end;
905         }
906
907 /*
908  *      We have a valid device, so now we check if it is already open.
909  *      If not then initialize the port hardware and set up the tty
910  *      struct as required.
911  */
912         if ((tp->t_state & TS_ISOPEN) == 0) {
913                 tp->t_oproc = stli_start;
914                 tp->t_param = stli_param;
915                 tp->t_stop = stli_stop;
916                 tp->t_dev = dev;
917                 tp->t_termios = callout ? portp->initouttios :
918                         portp->initintios;
919                 stli_initopen(portp);
920                 wakeup(&portp->state);
921                 ttsetwater(tp);
922                 if ((portp->sigs & TIOCM_CD) || callout)
923                         (*linesw[tp->t_line].l_modem)(tp, 1);
924         } else {
925                 if (callout) {
926                         if (portp->callout == 0) {
927                                 error = EBUSY;
928                                 goto stliopen_end;
929                         }
930                 } else {
931                         if (portp->callout != 0) {
932                                 if (ap->a_oflags & O_NONBLOCK) {
933                                         error = EBUSY;
934                                         goto stliopen_end;
935                                 }
936                                 error = tsleep(&portp->callout,
937                                             PCATCH, "stlicall", 0);
938                                 if (error)
939                                         goto stliopen_end;
940                                 goto stliopen_restart;
941                         }
942                 }
943                 if ((tp->t_state & TS_XCLUDE) &&
944                     priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
945                         error = EBUSY;
946                         goto stliopen_end;
947                 }
948         }
949
950 /*
951  *      If this port is not the callout device and we do not have carrier
952  *      then we need to sleep, waiting for it to be asserted.
953  */
954         if (((tp->t_state & TS_CARR_ON) == 0) && !callout &&
955                         ((tp->t_cflag & CLOCAL) == 0) &&
956                         ((ap->a_oflags & O_NONBLOCK) == 0)) {
957                 portp->waitopens++;
958                 error = tsleep(TSA_CARR_ON(tp), PCATCH, "stlidcd",0);
959                 portp->waitopens--;
960                 if (error)
961                         goto stliopen_end;
962                 goto stliopen_restart;
963         }
964
965 /*
966  *      Open the line discipline.
967  */
968         error = (*linesw[tp->t_line].l_open)(dev, tp);
969         stli_ttyoptim(portp, &tp->t_termios);
970         if ((tp->t_state & TS_ISOPEN) && callout)
971                 portp->callout = 1;
972
973 /*
974  *      If for any reason we get to here and the port is not actually
975  *      open then close of the physical hardware - no point leaving it
976  *      active when the open failed...
977  */
978 stliopen_end:
979         crit_exit();
980         if (((tp->t_state & TS_ISOPEN) == 0) && (portp->waitopens == 0))
981                 stli_shutdownclose(portp);
982
983         return(error);
984 }
985
986 /*****************************************************************************/
987
988 STATIC int stliclose(struct dev_close_args *ap)
989 {
990         cdev_t dev = ap->a_head.a_dev;
991         struct tty      *tp;
992         stliport_t      *portp;
993
994 #if STLDEBUG
995         kprintf("stliclose(dev=%s,flag=%x,mode=%x,p=%p)\n",
996                 devtoname(dev), flag, mode, (void *) p);
997 #endif
998
999         if (minor(dev) & STL_MEMDEV)
1000                 return(0);
1001         if (minor(dev) & STL_CTRLDEV)
1002                 return(0);
1003
1004         portp = stli_dev2port(dev);
1005         if (portp == NULL)
1006                 return(ENXIO);
1007         tp = &portp->tty;
1008
1009         crit_enter();
1010         (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
1011         stli_ttyoptim(portp, &tp->t_termios);
1012         stli_shutdownclose(portp);
1013         ttyclose(tp);
1014         crit_exit();
1015         return(0);
1016 }
1017
1018
1019 STATIC int stliread(struct dev_read_args *ap)
1020 {
1021         cdev_t dev = ap->a_head.a_dev;
1022         stliport_t      *portp;
1023
1024 #if STLDEBUG
1025         kprintf("stliread(dev=%s,uiop=%p,flag=%x)\n", devtoname(dev),
1026                 ap->a_uio, flag);
1027 #endif
1028
1029         if (minor(dev) & STL_MEMDEV)
1030                 return(stli_memrw(dev, ap->a_uio, ap->a_ioflag));
1031         if (minor(dev) & STL_CTRLDEV)
1032                 return(ENODEV);
1033
1034         portp = stli_dev2port(dev);
1035         if (portp == NULL)
1036                 return(ENODEV);
1037         return ttyread(ap);
1038 }
1039
1040 /*****************************************************************************/
1041
1042 #if VFREEBSD >= 220
1043
1044 STATIC void stli_stop(struct tty *tp, int rw)
1045 {
1046 #if STLDEBUG
1047         kprintf("stli_stop(tp=%x,rw=%x)\n", (int) tp, rw);
1048 #endif
1049
1050         stli_flush((stliport_t *) tp, rw);
1051 }
1052
1053 #else
1054
1055 STATIC int stlistop(struct tty *tp, int rw)
1056 {
1057 #if STLDEBUG
1058         kprintf("stlistop(tp=%x,rw=%x)\n", (int) tp, rw);
1059 #endif
1060
1061         stli_flush((stliport_t *) tp, rw);
1062         return(0);
1063 }
1064
1065 #endif
1066
1067 /*****************************************************************************/
1068
1069 STATIC int stliwrite(struct dev_write_args *ap)
1070 {
1071         cdev_t dev = ap->a_head.a_dev;
1072         stliport_t      *portp;
1073
1074 #if STLDEBUG
1075         kprintf("stliwrite(dev=%s,uiop=%p,flag=%x)\n", devtoname(dev),
1076                 ap->a_uio, flag);
1077 #endif
1078
1079         if (minor(dev) & STL_MEMDEV)
1080                 return(stli_memrw(dev, ap->a_uio, ap->a_ioflag));
1081         if (minor(dev) & STL_CTRLDEV)
1082                 return(ENODEV);
1083         portp = stli_dev2port(dev);
1084         if (portp == NULL)
1085                 return(ENODEV);
1086         return ttywrite(ap);
1087 }
1088
1089 /*****************************************************************************/
1090
1091 STATIC int stliioctl(struct dev_ioctl_args *ap)
1092 {
1093         cdev_t dev = ap->a_head.a_dev;
1094         u_long cmd = ap->a_cmd;
1095         caddr_t data = ap->a_data;
1096         struct termios  *newtios, *localtios;
1097         struct tty      *tp;
1098         stlibrd_t       *brdp;
1099         stliport_t      *portp;
1100         long            arg;
1101         int             error, i;
1102
1103 #if STLDEBUG
1104         kprintf("stliioctl(dev=%s,cmd=%lx,data=%p,flag=%x,p=%p)\n",
1105                 devtoname(dev), cmd, (void *) data, ap->a_fflag, (void *) p);
1106 #endif
1107
1108         if (minor(dev) & STL_MEMDEV)
1109                 return(stli_memioctl(dev, cmd, data, ap->a_fflag));
1110
1111         portp = stli_dev2port(dev);
1112         if (portp == NULL)
1113                 return(ENODEV);
1114         if ((brdp = stli_brds[portp->brdnr]) == NULL)
1115                 return(ENODEV);
1116         tp = &portp->tty;
1117         error = 0;
1118         
1119 /*
1120  *      First up handle ioctls on the control devices.
1121  */
1122         if (minor(dev) & STL_CTRLDEV) {
1123                 if ((minor(dev) & STL_CTRLDEV) == STL_CTRLINIT)
1124                         localtios = (minor(dev) & STL_CALLOUTDEV) ?
1125                                 &portp->initouttios : &portp->initintios;
1126                 else if ((minor(dev) & STL_CTRLDEV) == STL_CTRLLOCK)
1127                         localtios = (minor(dev) & STL_CALLOUTDEV) ?
1128                                 &portp->lockouttios : &portp->lockintios;
1129                 else
1130                         return(ENODEV);
1131
1132                 switch (cmd) {
1133                 case TIOCSETA:
1134                         if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) == 0)
1135                                 *localtios = *((struct termios *) data);
1136                         break;
1137                 case TIOCGETA:
1138                         *((struct termios *) data) = *localtios;
1139                         break;
1140                 case TIOCGETD:
1141                         *((int *) data) = TTYDISC;
1142                         break;
1143                 case TIOCGWINSZ:
1144                         bzero(data, sizeof(struct winsize));
1145                         break;
1146                 default:
1147                         error = ENOTTY;
1148                         break;
1149                 }
1150                 return(error);
1151         }
1152
1153 /*
1154  *      Deal with 4.3 compatibility issues if we have too...
1155  */
1156 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1157         if (1) {
1158                 struct termios  tios;
1159                 unsigned long   oldcmd;
1160
1161                 tios = tp->t_termios;
1162                 oldcmd = cmd;
1163                 if ((error = ttsetcompat(tp, &cmd, data, &tios)))
1164                         return(error);
1165                 if (cmd != oldcmd)
1166                         data = (caddr_t) &tios;
1167         }
1168 #endif
1169
1170 /*
1171  *      Carry out some pre-cmd processing work first...
1172  *      Hmmm, not so sure we want this, disable for now...
1173  */
1174         if ((cmd == TIOCSETA) || (cmd == TIOCSETAW) || (cmd == TIOCSETAF)) {
1175                 newtios = (struct termios *) data;
1176                 localtios = (minor(dev) & STL_CALLOUTDEV) ? &portp->lockouttios :
1177                          &portp->lockintios;
1178
1179                 newtios->c_iflag = (tp->t_iflag & localtios->c_iflag) |
1180                         (newtios->c_iflag & ~localtios->c_iflag);
1181                 newtios->c_oflag = (tp->t_oflag & localtios->c_oflag) |
1182                         (newtios->c_oflag & ~localtios->c_oflag);
1183                 newtios->c_cflag = (tp->t_cflag & localtios->c_cflag) |
1184                         (newtios->c_cflag & ~localtios->c_cflag);
1185                 newtios->c_lflag = (tp->t_lflag & localtios->c_lflag) |
1186                         (newtios->c_lflag & ~localtios->c_lflag);
1187                 for (i = 0; (i < NCCS); i++) {
1188                         if (localtios->c_cc[i] != 0)
1189                                 newtios->c_cc[i] = tp->t_cc[i];
1190                 }
1191                 if (localtios->c_ispeed != 0)
1192                         newtios->c_ispeed = tp->t_ispeed;
1193                 if (localtios->c_ospeed != 0)
1194                         newtios->c_ospeed = tp->t_ospeed;
1195         }
1196
1197 /*
1198  *      Call the line discipline and the common command processing to
1199  *      process this command (if they can).
1200  */
1201         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data,
1202                                               ap->a_fflag, ap->a_cred);
1203         if (error != ENOIOCTL)
1204                 return(error);
1205
1206         crit_enter();
1207         error = ttioctl(tp, cmd, data, ap->a_fflag);
1208         stli_ttyoptim(portp, &tp->t_termios);
1209         if (error != ENOIOCTL) {
1210                 crit_exit();
1211                 return(error);
1212         }
1213
1214         error = 0;
1215
1216 /*
1217  *      Process local commands here. These are all commands that only we
1218  *      can take care of (they all rely on actually doing something special
1219  *      to the actual hardware).
1220  */
1221         switch (cmd) {
1222         case TIOCSBRK:
1223                 arg = BREAKON;
1224                 error = stli_cmdwait(brdp, portp, A_BREAK, &arg,
1225                         sizeof(unsigned long), 0);
1226                 break;
1227         case TIOCCBRK:
1228                 arg = BREAKOFF;
1229                 error = stli_cmdwait(brdp, portp, A_BREAK, &arg,
1230                         sizeof(unsigned long), 0);
1231                 break;
1232         case TIOCSDTR:
1233                 stli_mkasysigs(&portp->asig, 1, -1);
1234                 error = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1235                         sizeof(asysigs_t), 0);
1236                 break;
1237         case TIOCCDTR:
1238                 stli_mkasysigs(&portp->asig, 0, -1);
1239                 error = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1240                         sizeof(asysigs_t), 0);
1241                 break;
1242         case TIOCMSET:
1243                 i = *((int *) data);
1244                 stli_mkasysigs(&portp->asig, ((i & TIOCM_DTR) ? 1 : 0),
1245                         ((i & TIOCM_RTS) ? 1 : 0));
1246                 error = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1247                         sizeof(asysigs_t), 0);
1248                 break;
1249         case TIOCMBIS:
1250                 i = *((int *) data);
1251                 stli_mkasysigs(&portp->asig, ((i & TIOCM_DTR) ? 1 : -1),
1252                         ((i & TIOCM_RTS) ? 1 : -1));
1253                 error = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1254                         sizeof(asysigs_t), 0);
1255                 break;
1256         case TIOCMBIC:
1257                 i = *((int *) data);
1258                 stli_mkasysigs(&portp->asig, ((i & TIOCM_DTR) ? 0 : -1),
1259                         ((i & TIOCM_RTS) ? 0 : -1));
1260                 error = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1261                         sizeof(asysigs_t), 0);
1262                 break;
1263         case TIOCMGET:
1264                 if ((error = stli_cmdwait(brdp, portp, A_GETSIGNALS,
1265                                 &portp->asig, sizeof(asysigs_t), 1)) < 0)
1266                         break;
1267                 portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1268                 *((int *) data) = (portp->sigs | TIOCM_LE);
1269                 break;
1270         case TIOCMSDTRWAIT:
1271                 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) == 0)
1272                         portp->dtrwait = *((int *) data) * hz / 100;
1273                 break;
1274         case TIOCMGDTRWAIT:
1275                 *((int *) data) = portp->dtrwait * 100 / hz;
1276                 break;
1277         case TIOCTIMESTAMP:
1278                 portp->dotimestamp = 1;
1279                 *((struct timeval *) data) = portp->timestamp;
1280                 break;
1281         case STL_GETPFLAG:
1282                 *((unsigned long *) data) = portp->pflag;
1283                 break;
1284         case STL_SETPFLAG:
1285                 portp->pflag = *((unsigned long *) data);
1286                 stli_param(&portp->tty, &portp->tty.t_termios);
1287                 break;
1288         default:
1289                 error = ENOTTY;
1290                 break;
1291         }
1292         crit_exit();
1293
1294         return(error);
1295 }
1296
1297 /*****************************************************************************/
1298
1299 /*
1300  *      Convert the specified minor device number into a port struct
1301  *      pointer. Return NULL if the device number is not a valid port.
1302  */
1303
1304 STATIC stliport_t *stli_dev2port(cdev_t dev)
1305 {
1306         stlibrd_t       *brdp;
1307
1308         brdp = stli_brds[MKDEV2BRD(dev)];
1309         if (brdp == NULL)
1310                 return(NULL);
1311         if ((brdp->state & BST_STARTED) == 0)
1312                 return(NULL);
1313         return(brdp->ports[MKDEV2PORT(dev)]);
1314 }
1315
1316 /*****************************************************************************/
1317
1318 /*
1319  *      Carry out first open operations on a port. This involves a number of
1320  *      commands to be sent to the slave. We need to open the port, set the
1321  *      notification events, set the initial port settings, get and set the
1322  *      initial signal values. We sleep and wait in between each one. But
1323  *      this still all happens pretty quickly.
1324  */
1325
1326 static int stli_initopen(stliport_t *portp)
1327 {
1328         stlibrd_t       *brdp;
1329         asynotify_t     nt;
1330         asyport_t       aport;
1331         int             rc;
1332
1333 #if STLDEBUG
1334         kprintf("stli_initopen(portp=%x)\n", (int) portp);
1335 #endif
1336
1337         if ((brdp = stli_brds[portp->brdnr]) == NULL)
1338                 return(ENXIO);
1339         if (portp->state & ST_INITIALIZED)
1340                 return(0);
1341         portp->state |= ST_INITIALIZED;
1342
1343         if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
1344                 return(rc);
1345
1346         bzero(&nt, sizeof(asynotify_t));
1347         nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
1348         nt.signal = SG_DCD;
1349         if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
1350             sizeof(asynotify_t), 0)) < 0)
1351                 return(rc);
1352
1353         stli_mkasyport(portp, &aport, &portp->tty.t_termios);
1354         if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
1355             sizeof(asyport_t), 0)) < 0)
1356                 return(rc);
1357
1358         portp->state |= ST_GETSIGS;
1359         if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
1360             sizeof(asysigs_t), 1)) < 0)
1361                 return(rc);
1362         if (portp->state & ST_GETSIGS) {
1363                 portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1364                 portp->state &= ~ST_GETSIGS;
1365         }
1366
1367         stli_mkasysigs(&portp->asig, 1, 1);
1368         if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1369             sizeof(asysigs_t), 0)) < 0)
1370                 return(rc);
1371
1372         return(0);
1373 }
1374
1375 /*****************************************************************************/
1376
1377 /*
1378  *      Shutdown the hardware of a port.
1379  */
1380
1381 static int stli_shutdownclose(stliport_t *portp)
1382 {
1383         stlibrd_t       *brdp;
1384         struct tty      *tp;
1385
1386 #if STLDEBUG
1387         kprintf("stli_shutdownclose(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1388                 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1389 #endif
1390
1391         if ((brdp = stli_brds[portp->brdnr]) == NULL)
1392                 return(ENXIO);
1393
1394         tp = &portp->tty;
1395         stli_rawclose(brdp, portp, 0, 0);
1396         stli_flush(portp, (FWRITE | FREAD));
1397         if (tp->t_cflag & HUPCL) {
1398                 crit_enter();
1399                 stli_mkasysigs(&portp->asig, 0, 0);
1400                 if (portp->state & ST_CMDING) {
1401                         portp->state |= ST_DOSIGS;
1402                 } else {
1403                         stli_sendcmd(brdp, portp, A_SETSIGNALS,
1404                                 &portp->asig, sizeof(asysigs_t), 0);
1405                 }
1406                 crit_exit();
1407                 if (portp->dtrwait != 0) {
1408                         portp->state |= ST_DTRWAIT;
1409                         callout_reset(&portp->dtr_ch, portp->dtrwait,
1410                                         stli_dtrwakeup, portp);
1411                 }
1412         }
1413         portp->callout = 0;
1414         portp->state &= ~ST_INITIALIZED;
1415         wakeup(&portp->callout);
1416         wakeup(TSA_CARR_ON(tp));
1417         return(0);
1418 }
1419
1420 /*****************************************************************************/
1421
1422 /*
1423  *      Clear the DTR waiting flag, and wake up any sleepers waiting for
1424  *      DTR wait period to finish.
1425  */
1426
1427 static void stli_dtrwakeup(void *arg)
1428 {
1429         stliport_t      *portp;
1430
1431         portp = (stliport_t *) arg;
1432         portp->state &= ~ST_DTRWAIT;
1433         wakeup(&portp->dtrwait);
1434 }
1435
1436 /*****************************************************************************/
1437
1438 /*
1439  *      Send an open message to the slave. This will sleep waiting for the
1440  *      acknowledgement, so must have user context. We need to co-ordinate
1441  *      with close events here, since we don't want open and close events
1442  *      to overlap.
1443  */
1444
1445 static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1446 {
1447         volatile cdkhdr_t       *hdrp;
1448         volatile cdkctrl_t      *cp;
1449         volatile unsigned char  *bits;
1450         int                     rc;
1451
1452 #if STLDEBUG
1453         kprintf("stli_rawopen(brdp=%x,portp=%x,arg=%x,wait=%d)\n", (int) brdp,
1454                 (int) portp, (int) arg, wait);
1455 #endif
1456
1457         crit_enter();
1458
1459 /*
1460  *      Slave is already closing this port. This can happen if a hangup
1461  *      occurs on this port. So we must wait until it is complete. The
1462  *      order of opens and closes may not be preserved across shared
1463  *      memory, so we must wait until it is complete.
1464  */
1465         while (portp->state & ST_CLOSING) {
1466                 rc = tsleep(&portp->state, PCATCH, "stliraw", 0);
1467                 if (rc) {
1468                         crit_exit();
1469                         return(rc);
1470                 }
1471         }
1472
1473 /*
1474  *      Everything is ready now, so write the open message into shared
1475  *      memory. Once the message is in set the service bits to say that
1476  *      this port wants service.
1477  */
1478         EBRDENABLE(brdp);
1479         cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1480         cp->openarg = arg;
1481         cp->open = 1;
1482         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1483         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1484                 portp->portidx;
1485         *bits |= portp->portbit;
1486         EBRDDISABLE(brdp);
1487
1488         if (wait == 0) {
1489                 crit_exit();
1490                 return(0);
1491         }
1492
1493 /*
1494  *      Slave is in action, so now we must wait for the open acknowledgment
1495  *      to come back.
1496  */
1497         rc = 0;
1498         portp->state |= ST_OPENING;
1499         while (portp->state & ST_OPENING) {
1500                 rc = tsleep(&portp->state, PCATCH, "stliraw", 0);
1501                 if (rc) {
1502                         crit_exit();
1503                         return(rc);
1504                 }
1505         }
1506         crit_exit();
1507
1508         if ((rc == 0) && (portp->rc != 0))
1509                 rc = EIO;
1510         return(rc);
1511 }
1512
1513 /*****************************************************************************/
1514
1515 /*
1516  *      Send a close message to the slave. Normally this will sleep waiting
1517  *      for the acknowledgement, but if wait parameter is 0 it will not. If
1518  *      wait is true then must have user context (to sleep).
1519  */
1520
1521 static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1522 {
1523         volatile cdkhdr_t       *hdrp;
1524         volatile cdkctrl_t      *cp;
1525         volatile unsigned char  *bits;
1526         int                     rc;
1527
1528 #if STLDEBUG
1529         kprintf("stli_rawclose(brdp=%x,portp=%x,arg=%x,wait=%d)\n", (int) brdp,
1530                 (int) portp, (int) arg, wait);
1531 #endif
1532
1533         crit_enter();
1534
1535 /*
1536  *      Slave is already closing this port. This can happen if a hangup
1537  *      occurs on this port.
1538  */
1539         if (wait) {
1540                 while (portp->state & ST_CLOSING) {
1541                         rc = tsleep(&portp->state, PCATCH, "stliraw", 0);
1542                         if (rc) {
1543                                 crit_exit();
1544                                 return(rc);
1545                         }
1546                 }
1547         }
1548
1549 /*
1550  *      Write the close command into shared memory.
1551  */
1552         EBRDENABLE(brdp);
1553         cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1554         cp->closearg = arg;
1555         cp->close = 1;
1556         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1557         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1558                 portp->portidx;
1559         *bits |= portp->portbit;
1560         EBRDDISABLE(brdp);
1561
1562         portp->state |= ST_CLOSING;
1563         if (wait == 0) {
1564                 crit_exit();
1565                 return(0);
1566         }
1567
1568 /*
1569  *      Slave is in action, so now we must wait for the open acknowledgment
1570  *      to come back.
1571  */
1572         rc = 0;
1573         while (portp->state & ST_CLOSING) {
1574                 rc = tsleep(&portp->state, PCATCH, "stliraw", 0);
1575                 if (rc) {
1576                         crit_exit();
1577                         return(rc);
1578                 }
1579         }
1580         crit_exit();
1581
1582         if ((rc == 0) && (portp->rc != 0))
1583                 rc = EIO;
1584         return(rc);
1585 }
1586
1587 /*****************************************************************************/
1588
1589 /*
1590  *      Send a command to the slave and wait for the response. This must
1591  *      have user context (it sleeps). This routine is generic in that it
1592  *      can send any type of command. Its purpose is to wait for that command
1593  *      to complete (as opposed to initiating the command then returning).
1594  */
1595
1596 static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
1597 {
1598         int     rc;
1599
1600 #if STLDEBUG
1601         kprintf("stli_cmdwait(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
1602                 "copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
1603                 (int) arg, size, copyback);
1604 #endif
1605
1606         crit_enter();
1607         while (portp->state & ST_CMDING) {
1608                 rc = tsleep(&portp->state, PCATCH, "stliraw", 0);
1609                 if (rc) {
1610                         crit_exit();
1611                         return(rc);
1612                 }
1613         }
1614
1615         stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1616
1617         while (portp->state & ST_CMDING) {
1618                 rc = tsleep(&portp->state, PCATCH, "stliraw", 0);
1619                 if (rc) {
1620                         crit_exit();
1621                         return(rc);
1622                 }
1623         }
1624         crit_exit();
1625
1626         if (portp->rc != 0)
1627                 return(EIO);
1628         return(0);
1629 }
1630
1631 /*****************************************************************************/
1632
1633 /*
1634  *      Start (or continue) the transfer of TX data on this port. If the
1635  *      port is not currently busy then load up the interrupt ring queue
1636  *      buffer and kick of the transmitter. If the port is running low on
1637  *      TX data then refill the ring queue. This routine is also used to
1638  *      activate input flow control!
1639  */
1640
1641 static void stli_start(struct tty *tp)
1642 {
1643         volatile cdkasy_t       *ap;
1644         volatile cdkhdr_t       *hdrp;
1645         volatile unsigned char  *bits;
1646         unsigned char           *shbuf;
1647         stliport_t              *portp;
1648         stlibrd_t               *brdp;
1649         unsigned int            len, stlen, head, tail, size;
1650         int                     count;
1651
1652         portp = (stliport_t *) tp;
1653
1654 #if STLDEBUG
1655         kprintf("stli_start(tp=%x): brdnr=%d portnr=%d\n", (int) tp, 
1656                 portp->brdnr, portp->portnr);
1657 #endif
1658
1659         crit_enter();
1660
1661 #if VFREEBSD == 205
1662 /*
1663  *      Check if the output cooked clist buffers are near empty, wake up
1664  *      the line discipline to fill it up.
1665  */
1666         if (tp->t_outq.c_cc <= tp->t_lowat) {
1667                 if (tp->t_state & TS_ASLEEP) {
1668                         tp->t_state &= ~TS_ASLEEP;
1669                         wakeup(&tp->t_outq);
1670                 }
1671                 KNOTE(&tp->t_wsel.si_note, 0);
1672         }
1673 #endif
1674
1675         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
1676                 crit_exit();
1677                 return;
1678         }
1679
1680 /*
1681  *      Copy data from the clists into the interrupt ring queue. This will
1682  *      require at most 2 copys... What we do is calculate how many chars
1683  *      can fit into the ring queue, and how many can fit in 1 copy. If after
1684  *      the first copy there is still more room then do the second copy. 
1685  */
1686         if (tp->t_outq.c_cc != 0) {
1687                 brdp = stli_brds[portp->brdnr];
1688                 if (brdp == NULL) {
1689                         crit_exit();
1690                         return;
1691                 }
1692
1693                 EBRDENABLE(brdp);
1694                 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1695                 head = (unsigned int) ap->txq.head;
1696                 tail = (unsigned int) ap->txq.tail;
1697                 if (tail != ((unsigned int) ap->txq.tail))
1698                         tail = (unsigned int) ap->txq.tail;
1699                 size = portp->txsize;
1700                 if (head >= tail) {
1701                         len = size - (head - tail) - 1;
1702                         stlen = size - head;
1703                 } else {
1704                         len = tail - head - 1;
1705                         stlen = len;
1706                 }
1707
1708                 count = 0;
1709                 shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1710
1711                 if (len > 0) {
1712                         stlen = MIN(len, stlen);
1713                         count = q_to_b(&tp->t_outq, (shbuf + head), stlen);
1714                         len -= count;
1715                         head += count;
1716                         if (head >= size) {
1717                                 head = 0;
1718                                 if (len > 0) {
1719                                         stlen = q_to_b(&tp->t_outq, shbuf, len);
1720                                         head += stlen;
1721                                         count += stlen;
1722                                 }
1723                         }
1724                 }
1725
1726                 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1727                 ap->txq.head = head;
1728                 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1729                 bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1730                         portp->portidx;
1731                 *bits |= portp->portbit;
1732                 portp->state |= ST_TXBUSY;
1733                 tp->t_state |= TS_BUSY;
1734
1735                 EBRDDISABLE(brdp);
1736         }
1737
1738 #if VFREEBSD != 205
1739 /*
1740  *      Do any writer wakeups.
1741  */
1742         ttwwakeup(tp);
1743 #endif
1744
1745         crit_exit();
1746 }
1747
1748 /*****************************************************************************/
1749
1750 /*
1751  *      Send a new port configuration to the slave.
1752  */
1753
1754 static int stli_param(struct tty *tp, struct termios *tiosp)
1755 {
1756         stlibrd_t       *brdp;
1757         stliport_t      *portp;
1758         asyport_t       aport;
1759         int             rc;
1760
1761         portp = (stliport_t *) tp;
1762         if ((brdp = stli_brds[portp->brdnr]) == NULL)
1763                 return(ENXIO);
1764
1765         crit_enter();
1766         stli_mkasyport(portp, &aport, tiosp);
1767         /* can we sleep here? */
1768         rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
1769         stli_ttyoptim(portp, tiosp);
1770         crit_exit();
1771         return(rc);
1772 }
1773
1774 /*****************************************************************************/
1775
1776 /*
1777  *      Flush characters from the lower buffer. We may not have user context
1778  *      so we cannot sleep waiting for it to complete. Also we need to check
1779  *      if there is chars for this port in the TX cook buffer, and flush them
1780  *      as well.
1781  */
1782
1783 static void stli_flush(stliport_t *portp, int flag)
1784 {
1785         stlibrd_t       *brdp;
1786         unsigned long   ftype;
1787
1788 #if STLDEBUG
1789         kprintf("stli_flush(portp=%x,flag=%x)\n", (int) portp, flag);
1790 #endif
1791
1792         if (portp == NULL)
1793                 return;
1794         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1795                 return;
1796         brdp = stli_brds[portp->brdnr];
1797         if (brdp == NULL)
1798                 return;
1799
1800         crit_enter();
1801         if (portp->state & ST_CMDING) {
1802                 portp->state |= (flag & FWRITE) ? ST_DOFLUSHTX : 0;
1803                 portp->state |= (flag & FREAD) ? ST_DOFLUSHRX : 0;
1804         } else {
1805                 ftype = (flag & FWRITE) ? FLUSHTX : 0;
1806                 ftype |= (flag & FREAD) ? FLUSHRX : 0;
1807                 portp->state &= ~(ST_DOFLUSHTX | ST_DOFLUSHRX);
1808                 stli_sendcmd(brdp, portp, A_FLUSH, &ftype,
1809                         sizeof(unsigned long), 0);
1810         }
1811         if ((flag & FREAD) && (stli_rxtmpport == portp))
1812                 stli_rxtmplen = 0;
1813         crit_exit();
1814 }
1815
1816 /*****************************************************************************/
1817
1818 /*
1819  *      Generic send command routine. This will send a message to the slave,
1820  *      of the specified type with the specified argument. Must be very
1821  *      carefull of data that will be copied out from shared memory -
1822  *      containing command results. The command completion is all done from
1823  *      a poll routine that does not have user coontext. Therefore you cannot
1824  *      copy back directly into user space, or to the kernel stack of a
1825  *      process. This routine does not sleep, so can be called from anywhere,
1826  *      and must be called with interrupt locks set.
1827  */
1828
1829 static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
1830 {
1831         volatile cdkhdr_t       *hdrp;
1832         volatile cdkctrl_t      *cp;
1833         volatile unsigned char  *bits;
1834
1835 #if STLDEBUG
1836         kprintf("stli_sendcmd(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
1837                 "copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
1838                 (int) arg, size, copyback);
1839 #endif
1840
1841         if (portp->state & ST_CMDING) {
1842                 kprintf("STALLION: command already busy, cmd=%x!\n", (int) cmd);
1843                 return;
1844         }
1845
1846         EBRDENABLE(brdp);
1847         cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1848         if (size > 0) {
1849                 bcopy(arg, &(cp->args[0]), size);
1850                 if (copyback) {
1851                         portp->argp = arg;
1852                         portp->argsize = size;
1853                 }
1854         }
1855         cp->status = 0;
1856         cp->cmd = cmd;
1857         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1858         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1859                 portp->portidx;
1860         *bits |= portp->portbit;
1861         portp->state |= ST_CMDING;
1862         EBRDDISABLE(brdp);
1863 }
1864
1865 /*****************************************************************************/
1866
1867 /*
1868  *      Read data from shared memory. This assumes that the shared memory
1869  *      is enabled and that interrupts are off. Basically we just empty out
1870  *      the shared memory buffer into the tty buffer. Must be carefull to
1871  *      handle the case where we fill up the tty buffer, but still have
1872  *      more chars to unload.
1873  */
1874
1875 static void stli_rxprocess(stlibrd_t *brdp, stliport_t *portp)
1876 {
1877         volatile cdkasyrq_t     *rp;
1878         volatile char           *shbuf;
1879         struct tty              *tp;
1880         unsigned int            head, tail, size;
1881         unsigned int            len, stlen, i;
1882         int                     ch;
1883
1884 #if STLDEBUG
1885         kprintf("stli_rxprocess(brdp=%x,portp=%d)\n", (int) brdp, (int) portp);
1886 #endif
1887
1888         tp = &portp->tty;
1889         if ((tp->t_state & TS_ISOPEN) == 0) {
1890                 stli_flush(portp, FREAD);
1891                 return;
1892         }
1893         if (tp->t_state & TS_TBLOCK)
1894                 return;
1895
1896         rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
1897         head = (unsigned int) rp->head;
1898         if (head != ((unsigned int) rp->head))
1899                 head = (unsigned int) rp->head;
1900         tail = (unsigned int) rp->tail;
1901         size = portp->rxsize;
1902         if (head >= tail) {
1903                 len = head - tail;
1904                 stlen = len;
1905         } else {
1906                 len = size - (tail - head);
1907                 stlen = size - tail;
1908         }
1909
1910         if (len == 0)
1911                 return;
1912
1913         shbuf = (volatile char *) EBRDGETMEMPTR(brdp, portp->rxoffset);
1914
1915 /*
1916  *      If we can bypass normal LD processing then just copy direct
1917  *      from board shared memory into the tty buffers.
1918  */
1919         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1920                 if (((tp->t_rawq.c_cc + len) >= TTYHOG) &&
1921                     ((tp->t_cflag & CRTS_IFLOW) || (tp->t_iflag & IXOFF)) &&
1922                     ((tp->t_state & TS_TBLOCK) == 0)) {
1923                         ch = TTYHOG - tp->t_rawq.c_cc - 1;
1924                         len = (ch > 0) ? ch : 0;
1925                         stlen = MIN(stlen, len);
1926                         tp->t_state |= TS_TBLOCK;
1927                 }
1928                 i = b_to_q(__DEVOLATILE(char *, shbuf + tail), stlen,
1929                            &tp->t_rawq);
1930                 tail += stlen;
1931                 len -= stlen;
1932                 if (tail >= size) {
1933                         tail = 0;
1934                         i += b_to_q(__DEVOLATILE(char *, shbuf), len,
1935                                     &tp->t_rawq);
1936                         tail += len;
1937                 }
1938                 portp->rxlost += i;
1939                 ttwakeup(tp);
1940                 rp = &((volatile cdkasy_t *)
1941                         EBRDGETMEMPTR(brdp, portp->addr))->rxq;
1942                 rp->tail = tail;
1943
1944         } else {
1945 /*
1946  *              Copy the data from board shared memory into a local
1947  *              memory buffer. Then feed them from here into the LD.
1948  *              We don't want to go into board shared memory one char
1949  *              at a time, it is too slow...
1950  */
1951                 if (len > TTYHOG) {
1952                         len = TTYHOG - 1;
1953                         stlen = min(len, stlen);
1954                 }
1955                 stli_rxtmpport = portp;
1956                 stli_rxtmplen = len;
1957                 bcopy(__DEVOLATILE(char *, shbuf + tail), &stli_rxtmpbuf[0],
1958                       stlen);
1959                 len -= stlen;
1960                 if (len > 0)
1961                         bcopy(shbuf, &stli_rxtmpbuf[stlen], len);
1962                 
1963                 for (i = 0; (i < stli_rxtmplen); i++) {
1964                         ch = (unsigned char) stli_rxtmpbuf[i];
1965                         (*linesw[tp->t_line].l_rint)(ch, tp);
1966                 }
1967                 EBRDENABLE(brdp);
1968                 rp = &((volatile cdkasy_t *)
1969                         EBRDGETMEMPTR(brdp, portp->addr))->rxq;
1970                 if (stli_rxtmplen == 0) {
1971                         head = (unsigned int) rp->head;
1972                         if (head != ((unsigned int) rp->head))
1973                                 head = (unsigned int) rp->head;
1974                         tail = head;
1975                 } else {
1976                         tail += i;
1977                         if (tail >= size)
1978                                 tail -= size;
1979                 }
1980                 rp->tail = tail;
1981                 stli_rxtmpport = NULL;
1982                 stli_rxtmplen = 0;
1983         }
1984
1985         portp->state |= ST_RXING;
1986 }
1987
1988 /*****************************************************************************/
1989
1990 /*
1991  *      Set up and carry out any delayed commands. There is only a small set
1992  *      of slave commands that can be done "off-level". So it is not too
1993  *      difficult to deal with them as a special case here.
1994  */
1995
1996 static __inline void stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp)
1997 {
1998         int     cmd;
1999
2000         if (portp->state & ST_DOSIGS) {
2001                 if ((portp->state & ST_DOFLUSHTX) &&
2002                     (portp->state & ST_DOFLUSHRX))
2003                         cmd = A_SETSIGNALSF;
2004                 else if (portp->state & ST_DOFLUSHTX)
2005                         cmd = A_SETSIGNALSFTX;
2006                 else if (portp->state & ST_DOFLUSHRX)
2007                         cmd = A_SETSIGNALSFRX;
2008                 else
2009                         cmd = A_SETSIGNALS;
2010                 portp->state &= ~(ST_DOFLUSHTX | ST_DOFLUSHRX | ST_DOSIGS);
2011                 bcopy(&portp->asig, &(cp->args[0]), sizeof(asysigs_t));
2012                 cp->status = 0;
2013                 cp->cmd = cmd;
2014                 portp->state |= ST_CMDING;
2015         } else if ((portp->state & ST_DOFLUSHTX) ||
2016             (portp->state & ST_DOFLUSHRX)) {
2017                 cmd = ((portp->state & ST_DOFLUSHTX) ? FLUSHTX : 0);
2018                 cmd |= ((portp->state & ST_DOFLUSHRX) ? FLUSHRX : 0);
2019                 portp->state &= ~(ST_DOFLUSHTX | ST_DOFLUSHRX);
2020                 bcopy(&cmd, &(cp->args[0]), sizeof(int));
2021                 cp->status = 0;
2022                 cp->cmd = A_FLUSH;
2023                 portp->state |= ST_CMDING;
2024         }
2025 }
2026
2027 /*****************************************************************************/
2028
2029 /*
2030  *      Host command service checking. This handles commands or messages
2031  *      coming from the slave to the host. Must have board shared memory
2032  *      enabled and interrupts off when called. Notice that by servicing the
2033  *      read data last we don't need to change the shared memory pointer
2034  *      during processing (which is a slow IO operation).
2035  *      Return value indicates if this port is still awaiting actions from
2036  *      the slave (like open, command, or even TX data being sent). If 0
2037  *      then port is still busy, otherwise the port request bit flag is
2038  *      returned.
2039  */
2040
2041 static __inline int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp)
2042 {
2043         volatile cdkasy_t       *ap;
2044         volatile cdkctrl_t      *cp;
2045         asynotify_t             nt;
2046         unsigned long           oldsigs;
2047         unsigned int            head, tail;
2048         int                     rc, donerx;
2049
2050 #if STLDEBUG
2051         kprintf("stli_hostcmd(brdp=%x,portp=%x)\n", (int) brdp, (int) portp);
2052 #endif
2053
2054         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
2055         cp = &ap->ctrl;
2056
2057 /*
2058  *      Check if we are waiting for an open completion message.
2059  */
2060         if (portp->state & ST_OPENING) {
2061                 rc = (int) cp->openarg;
2062                 if ((cp->open == 0) && (rc != 0)) {
2063                         if (rc > 0)
2064                                 rc--;
2065                         cp->openarg = 0;
2066                         portp->rc = rc;
2067                         portp->state &= ~ST_OPENING;
2068                         wakeup(&portp->state);
2069                 }
2070         }
2071
2072 /*
2073  *      Check if we are waiting for a close completion message.
2074  */
2075         if (portp->state & ST_CLOSING) {
2076                 rc = (int) cp->closearg;
2077                 if ((cp->close == 0) && (rc != 0)) {
2078                         if (rc > 0)
2079                                 rc--;
2080                         cp->closearg = 0;
2081                         portp->rc = rc;
2082                         portp->state &= ~ST_CLOSING;
2083                         wakeup(&portp->state);
2084                 }
2085         }
2086
2087 /*
2088  *      Check if we are waiting for a command completion message. We may
2089  *      need to copy out the command results associated with this command.
2090  */
2091         if (portp->state & ST_CMDING) {
2092                 rc = cp->status;
2093                 if ((cp->cmd == 0) && (rc != 0)) {
2094                         if (rc > 0)
2095                                 rc--;
2096                         if (portp->argp != NULL) {
2097                                 bcopy(&(cp->args[0]), portp->argp,
2098                                         portp->argsize);
2099                                 portp->argp = NULL;
2100                         }
2101                         cp->status = 0;
2102                         portp->rc = rc;
2103                         portp->state &= ~ST_CMDING;
2104                         stli_dodelaycmd(portp, cp);
2105                         wakeup(&portp->state);
2106                 }
2107         }
2108
2109 /*
2110  *      Check for any notification messages ready. This includes lots of
2111  *      different types of events - RX chars ready, RX break received,
2112  *      TX data low or empty in the slave, modem signals changed state.
2113  *      Must be extremely carefull if we call to the LD, it may call
2114  *      other routines of ours that will disable the memory...
2115  *      Something else we need to be carefull of is race conditions on
2116  *      marking the TX as empty...
2117  */
2118         donerx = 0;
2119
2120         if (ap->notify) {
2121                 struct tty      *tp;
2122
2123                 nt = ap->changed;
2124                 ap->notify = 0;
2125                 tp = &portp->tty;
2126
2127                 if (nt.signal & SG_DCD) {
2128                         oldsigs = portp->sigs;
2129                         portp->sigs = stli_mktiocm(nt.sigvalue);
2130                         portp->state &= ~ST_GETSIGS;
2131                         (*linesw[tp->t_line].l_modem)(tp,
2132                                 (portp->sigs & TIOCM_CD));
2133                         EBRDENABLE(brdp);
2134                 }
2135                 if (nt.data & DT_RXBUSY) {
2136                         donerx++;
2137                         stli_rxprocess(brdp, portp);
2138                 }
2139                 if (nt.data & DT_RXBREAK) {
2140                         (*linesw[tp->t_line].l_rint)(TTY_BI, tp);
2141                         EBRDENABLE(brdp);
2142                 }
2143                 if (nt.data & DT_TXEMPTY) {
2144                         ap = (volatile cdkasy_t *)
2145                                 EBRDGETMEMPTR(brdp, portp->addr);
2146                         head = (unsigned int) ap->txq.head;
2147                         tail = (unsigned int) ap->txq.tail;
2148                         if (tail != ((unsigned int) ap->txq.tail))
2149                                 tail = (unsigned int) ap->txq.tail;
2150                         head = (head >= tail) ? (head - tail) :
2151                                 portp->txsize - (tail - head);
2152                         if (head == 0) {
2153                                 portp->state &= ~ST_TXBUSY;
2154                                 tp->t_state &= ~TS_BUSY;
2155                         }
2156                 }
2157                 if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
2158                         (*linesw[tp->t_line].l_start)(tp);
2159                         EBRDENABLE(brdp);
2160                 }
2161         }
2162
2163 /*
2164  *      It might seem odd that we are checking for more RX chars here.
2165  *      But, we need to handle the case where the tty buffer was previously
2166  *      filled, but we had more characters to pass up. The slave will not
2167  *      send any more RX notify messages until the RX buffer has been emptied.
2168  *      But it will leave the service bits on (since the buffer is not empty).
2169  *      So from here we can try to process more RX chars.
2170  */
2171         if ((!donerx) && (portp->state & ST_RXING)) {
2172                 portp->state &= ~ST_RXING;
2173                 stli_rxprocess(brdp, portp);
2174         }
2175
2176         return((portp->state & (ST_OPENING | ST_CLOSING | ST_CMDING |
2177                 ST_TXBUSY | ST_RXING)) ? 0 : 1);
2178 }
2179
2180 /*****************************************************************************/
2181
2182 /*
2183  *      Service all ports on a particular board. Assumes that the boards
2184  *      shared memory is enabled, and that the page pointer is pointed
2185  *      at the cdk header structure.
2186  */
2187
2188 static __inline void stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp)
2189 {
2190         stliport_t      *portp;
2191         unsigned char   hostbits[(STL_MAXCHANS / 8) + 1];
2192         unsigned char   slavebits[(STL_MAXCHANS / 8) + 1];
2193         unsigned char   *slavep;
2194         int             bitpos, bitat, bitsize;
2195         int             channr, nrdevs, slavebitchange;
2196
2197         bitsize = brdp->bitsize;
2198         nrdevs = brdp->nrdevs;
2199
2200 /*
2201  *      Check if slave wants any service. Basically we try to do as
2202  *      little work as possible here. There are 2 levels of service
2203  *      bits. So if there is nothing to do we bail early. We check
2204  *      8 service bits at a time in the inner loop, so we can bypass
2205  *      the lot if none of them want service.
2206  */
2207         bcopy(__DEVOLATILE(unsigned char *, hdrp) + brdp->hostoffset,
2208               &hostbits[0], bitsize);
2209
2210         bzero(&slavebits[0], bitsize);
2211         slavebitchange = 0;
2212
2213         for (bitpos = 0; (bitpos < bitsize); bitpos++) {
2214                 if (hostbits[bitpos] == 0)
2215                         continue;
2216                 channr = bitpos * 8;
2217                 bitat = 0x1;
2218                 for (; (channr < nrdevs); channr++, bitat <<=1) {
2219                         if (hostbits[bitpos] & bitat) {
2220                                 portp = brdp->ports[(channr - 1)];
2221                                 if (stli_hostcmd(brdp, portp)) {
2222                                         slavebitchange++;
2223                                         slavebits[bitpos] |= bitat;
2224                                 }
2225                         }
2226                 }
2227         }
2228
2229 /*
2230  *      If any of the ports are no longer busy then update them in the
2231  *      slave request bits. We need to do this after, since a host port
2232  *      service may initiate more slave requests...
2233  */
2234         if (slavebitchange) {
2235                 hdrp = (volatile cdkhdr_t *)
2236                         EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2237                 slavep = __DEVOLATILE(unsigned char *, hdrp) + brdp->slaveoffset;
2238                 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
2239                         if (slavebits[bitpos])
2240                                 slavep[bitpos] &= ~slavebits[bitpos];
2241                 }
2242         }
2243 }
2244
2245 /*****************************************************************************/
2246
2247 /*
2248  *      Driver poll routine. This routine polls the boards in use and passes
2249  *      messages back up to host when neccesary. This is actually very
2250  *      CPU efficient, since we will always have the kernel poll clock, it
2251  *      adds only a few cycles when idle (since board service can be
2252  *      determined very easily), but when loaded generates no interrupts
2253  *      (with their expensive associated context change).
2254  */
2255
2256 static void stli_poll(void *arg)
2257 {
2258         volatile cdkhdr_t       *hdrp;
2259         stlibrd_t               *brdp;
2260         int                     brdnr;
2261
2262         crit_enter();
2263
2264 /*
2265  *      Check each board and do any servicing required.
2266  */
2267         for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2268                 brdp = stli_brds[brdnr];
2269                 if (brdp == NULL)
2270                         continue;
2271                 if ((brdp->state & BST_STARTED) == 0)
2272                         continue;
2273
2274                 EBRDENABLE(brdp);
2275                 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2276                 if (hdrp->hostreq)
2277                         stli_brdpoll(brdp, hdrp);
2278                 EBRDDISABLE(brdp);
2279         }
2280         crit_exit();
2281
2282         callout_reset(&stli_poll_ch, 1, stli_poll, NULL);
2283 }
2284
2285 /*****************************************************************************/
2286
2287 /*
2288  *      Translate the termios settings into the port setting structure of
2289  *      the slave.
2290  */
2291
2292 static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp)
2293 {
2294 #if STLDEBUG
2295         kprintf("stli_mkasyport(portp=%x,pp=%x,tiosp=%d)\n", (int) portp,
2296                 (int) pp, (int) tiosp);
2297 #endif
2298
2299         bzero(pp, sizeof(asyport_t));
2300
2301 /*
2302  *      Start of by setting the baud, char size, parity and stop bit info.
2303  */
2304         if (tiosp->c_ispeed == 0)
2305                 tiosp->c_ispeed = tiosp->c_ospeed;
2306         if ((tiosp->c_ospeed < 0) || (tiosp->c_ospeed > STL_MAXBAUD))
2307                 tiosp->c_ospeed = STL_MAXBAUD;
2308         pp->baudout = tiosp->c_ospeed;
2309         pp->baudin = pp->baudout;
2310
2311         switch (tiosp->c_cflag & CSIZE) {
2312         case CS5:
2313                 pp->csize = 5;
2314                 break;
2315         case CS6:
2316                 pp->csize = 6;
2317                 break;
2318         case CS7:
2319                 pp->csize = 7;
2320                 break;
2321         default:
2322                 pp->csize = 8;
2323                 break;
2324         }
2325
2326         if (tiosp->c_cflag & CSTOPB)
2327                 pp->stopbs = PT_STOP2;
2328         else
2329                 pp->stopbs = PT_STOP1;
2330
2331         if (tiosp->c_cflag & PARENB) {
2332                 if (tiosp->c_cflag & PARODD)
2333                         pp->parity = PT_ODDPARITY;
2334                 else
2335                         pp->parity = PT_EVENPARITY;
2336         } else {
2337                 pp->parity = PT_NOPARITY;
2338         }
2339
2340         if (tiosp->c_iflag & ISTRIP)
2341                 pp->iflag |= FI_ISTRIP;
2342
2343 /*
2344  *      Set up any flow control options enabled.
2345  */
2346         if (tiosp->c_iflag & IXON) {
2347                 pp->flow |= F_IXON;
2348                 if (tiosp->c_iflag & IXANY)
2349                         pp->flow |= F_IXANY;
2350         }
2351         if (tiosp->c_iflag & IXOFF)
2352                 pp->flow |= F_IXOFF;
2353         if (tiosp->c_cflag & CCTS_OFLOW)
2354                 pp->flow |= F_CTSFLOW;
2355         if (tiosp->c_cflag & CRTS_IFLOW)
2356                 pp->flow |= F_RTSFLOW;
2357
2358         pp->startin = tiosp->c_cc[VSTART];
2359         pp->stopin = tiosp->c_cc[VSTOP];
2360         pp->startout = tiosp->c_cc[VSTART];
2361         pp->stopout = tiosp->c_cc[VSTOP];
2362
2363 /*
2364  *      Set up the RX char marking mask with those RX error types we must
2365  *      catch. We can get the slave to help us out a little here, it will
2366  *      ignore parity errors and breaks for us, and mark parity errors in
2367  *      the data stream.
2368  */
2369         if (tiosp->c_iflag & IGNPAR)
2370                 pp->iflag |= FI_IGNRXERRS;
2371         if (tiosp->c_iflag & IGNBRK)
2372                 pp->iflag |= FI_IGNBREAK;
2373         if (tiosp->c_iflag & (INPCK | PARMRK))
2374                 pp->iflag |= FI_1MARKRXERRS;
2375
2376 /*
2377  *      Transfer any persistent flags into the asyport structure.
2378  */
2379         pp->pflag = (portp->pflag & 0xffff);
2380         pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
2381         pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
2382         pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
2383 }
2384
2385 /*****************************************************************************/
2386
2387 /*
2388  *      Construct a slave signals structure for setting the DTR and RTS
2389  *      signals as specified.
2390  */
2391
2392 static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
2393 {
2394 #if STLDEBUG
2395         kprintf("stli_mkasysigs(sp=%x,dtr=%d,rts=%d)\n", (int) sp, dtr, rts);
2396 #endif
2397
2398         bzero(sp, sizeof(asysigs_t));
2399         if (dtr >= 0) {
2400                 sp->signal |= SG_DTR;
2401                 sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
2402         }
2403         if (rts >= 0) {
2404                 sp->signal |= SG_RTS;
2405                 sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
2406         }
2407 }
2408
2409 /*****************************************************************************/
2410
2411 /*
2412  *      Convert the signals returned from the slave into a local TIOCM type
2413  *      signals value. We keep them localy in TIOCM format.
2414  */
2415
2416 static long stli_mktiocm(unsigned long sigvalue)
2417 {
2418         long    tiocm;
2419
2420 #if STLDEBUG
2421         kprintf("stli_mktiocm(sigvalue=%x)\n", (int) sigvalue);
2422 #endif
2423
2424         tiocm = 0;
2425         tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
2426         tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
2427         tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
2428         tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
2429         tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
2430         tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
2431         return(tiocm);
2432 }
2433
2434 /*****************************************************************************/
2435
2436 /*
2437  *      Enable l_rint processing bypass mode if tty modes allow it.
2438  */
2439
2440 static void stli_ttyoptim(stliport_t *portp, struct termios *tiosp)
2441 {
2442         struct tty      *tp;
2443
2444         tp = &portp->tty;
2445         if (((tiosp->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR)) == 0) &&
2446             (((tiosp->c_iflag & BRKINT) == 0) || (tiosp->c_iflag & IGNBRK)) &&
2447             (((tiosp->c_iflag & PARMRK) == 0) ||
2448                 ((tiosp->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))) &&
2449             ((tiosp->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) ==0) &&
2450             (linesw[tp->t_line].l_rint == ttyinput))
2451                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2452         else
2453                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2454         portp->hotchar = linesw[tp->t_line].l_hotchar;
2455 }
2456
2457 /*****************************************************************************/
2458
2459 /*
2460  *      All panels and ports actually attached have been worked out. All
2461  *      we need to do here is set up the appropriate per port data structures.
2462  */
2463
2464 static int stli_initports(stlibrd_t *brdp)
2465 {
2466         stliport_t      *portp;
2467         int             i, panelnr, panelport;
2468
2469 #if STLDEBUG
2470         kprintf("stli_initports(brdp=%x)\n", (int) brdp);
2471 #endif
2472
2473         for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
2474                 portp = kmalloc(sizeof(stliport_t), M_TTYS, M_WAITOK | M_ZERO);
2475                 callout_init(&portp->dtr_ch);
2476                 portp->portnr = i;
2477                 portp->brdnr = brdp->brdnr;
2478                 portp->panelnr = panelnr;
2479                 portp->initintios.c_ispeed = STL_DEFSPEED;
2480                 portp->initintios.c_ospeed = STL_DEFSPEED;
2481                 portp->initintios.c_cflag = STL_DEFCFLAG;
2482                 portp->initintios.c_iflag = 0;
2483                 portp->initintios.c_oflag = 0;
2484                 portp->initintios.c_lflag = 0;
2485                 bcopy(&ttydefchars[0], &portp->initintios.c_cc[0],
2486                         sizeof(portp->initintios.c_cc));
2487                 portp->initouttios = portp->initintios;
2488                 portp->dtrwait = 3 * hz;
2489
2490                 panelport++;
2491                 if (panelport >= brdp->panels[panelnr]) {
2492                         panelport = 0;
2493                         panelnr++;
2494                 }
2495                 brdp->ports[i] = portp;
2496
2497         }
2498
2499         return(0);
2500 }
2501
2502 /*****************************************************************************/
2503
2504 /*
2505  *      All the following routines are board specific hardware operations.
2506  */
2507
2508 static void stli_ecpinit(stlibrd_t *brdp)
2509 {
2510         unsigned long   memconf;
2511
2512 #if STLDEBUG
2513         kprintf("stli_ecpinit(brdp=%d)\n", (int) brdp);
2514 #endif
2515
2516         outb((brdp->iobase + ECP_ATCONFR), ECP_ATSTOP);
2517         DELAY(10);
2518         outb((brdp->iobase + ECP_ATCONFR), ECP_ATDISABLE);
2519         DELAY(100);
2520
2521         memconf = (brdp->paddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
2522         outb((brdp->iobase + ECP_ATMEMAR), memconf);
2523 }
2524
2525 /*****************************************************************************/
2526
2527 static void stli_ecpenable(stlibrd_t *brdp)
2528 {       
2529 #if STLDEBUG
2530         kprintf("stli_ecpenable(brdp=%x)\n", (int) brdp);
2531 #endif
2532         outb((brdp->iobase + ECP_ATCONFR), ECP_ATENABLE);
2533 }
2534
2535 /*****************************************************************************/
2536
2537 static void stli_ecpdisable(stlibrd_t *brdp)
2538 {       
2539 #if STLDEBUG
2540         kprintf("stli_ecpdisable(brdp=%x)\n", (int) brdp);
2541 #endif
2542         outb((brdp->iobase + ECP_ATCONFR), ECP_ATDISABLE);
2543 }
2544
2545 /*****************************************************************************/
2546
2547 static char *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
2548 {       
2549         void            *ptr;
2550         unsigned char   val;
2551
2552 #if STLDEBUG
2553         kprintf("stli_ecpgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
2554                 (int) offset);
2555 #endif
2556
2557         if (offset > brdp->memsize) {
2558                 kprintf("STALLION: shared memory pointer=%x out of range at "
2559                         "line=%d(%d), brd=%d\n", (int) offset, line,
2560                         __LINE__, brdp->brdnr);
2561                 ptr = 0;
2562                 val = 0;
2563         } else {
2564                 ptr = (char *) brdp->vaddr + (offset % ECP_ATPAGESIZE);
2565                 val = (unsigned char) (offset / ECP_ATPAGESIZE);
2566         }
2567         outb((brdp->iobase + ECP_ATMEMPR), val);
2568         return(ptr);
2569 }
2570
2571 /*****************************************************************************/
2572
2573 static void stli_ecpreset(stlibrd_t *brdp)
2574 {       
2575 #if STLDEBUG
2576         kprintf("stli_ecpreset(brdp=%x)\n", (int) brdp);
2577 #endif
2578
2579         outb((brdp->iobase + ECP_ATCONFR), ECP_ATSTOP);
2580         DELAY(10);
2581         outb((brdp->iobase + ECP_ATCONFR), ECP_ATDISABLE);
2582         DELAY(500);
2583 }
2584
2585 /*****************************************************************************/
2586
2587 static void stli_ecpintr(stlibrd_t *brdp)
2588 {       
2589 #if STLDEBUG
2590         kprintf("stli_ecpintr(brdp=%x)\n", (int) brdp);
2591 #endif
2592         outb(brdp->iobase, 0x1);
2593 }
2594
2595 /*****************************************************************************/
2596
2597 /*
2598  *      The following set of functions act on ECP EISA boards.
2599  */
2600
2601 static void stli_ecpeiinit(stlibrd_t *brdp)
2602 {
2603         unsigned long   memconf;
2604
2605 #if STLDEBUG
2606         kprintf("stli_ecpeiinit(brdp=%x)\n", (int) brdp);
2607 #endif
2608
2609         outb((brdp->iobase + ECP_EIBRDENAB), 0x1);
2610         outb((brdp->iobase + ECP_EICONFR), ECP_EISTOP);
2611         DELAY(10);
2612         outb((brdp->iobase + ECP_EICONFR), ECP_EIDISABLE);
2613         DELAY(500);
2614
2615         memconf = (brdp->paddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
2616         outb((brdp->iobase + ECP_EIMEMARL), memconf);
2617         memconf = (brdp->paddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
2618         outb((brdp->iobase + ECP_EIMEMARH), memconf);
2619 }
2620
2621 /*****************************************************************************/
2622
2623 static void stli_ecpeienable(stlibrd_t *brdp)
2624 {       
2625         outb((brdp->iobase + ECP_EICONFR), ECP_EIENABLE);
2626 }
2627
2628 /*****************************************************************************/
2629
2630 static void stli_ecpeidisable(stlibrd_t *brdp)
2631 {       
2632         outb((brdp->iobase + ECP_EICONFR), ECP_EIDISABLE);
2633 }
2634
2635 /*****************************************************************************/
2636
2637 static char *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
2638 {       
2639         void            *ptr;
2640         unsigned char   val;
2641
2642 #if STLDEBUG
2643         kprintf("stli_ecpeigetmemptr(brdp=%x,offset=%x,line=%d)\n",
2644                 (int) brdp, (int) offset, line);
2645 #endif
2646
2647         if (offset > brdp->memsize) {
2648                 kprintf("STALLION: shared memory pointer=%x out of range at "
2649                         "line=%d(%d), brd=%d\n", (int) offset, line,
2650                         __LINE__, brdp->brdnr);
2651                 ptr = 0;
2652                 val = 0;
2653         } else {
2654                 ptr = (char *) brdp->vaddr + (offset % ECP_EIPAGESIZE);
2655                 if (offset < ECP_EIPAGESIZE)
2656                         val = ECP_EIENABLE;
2657                 else
2658                         val = ECP_EIENABLE | 0x40;
2659         }
2660         outb((brdp->iobase + ECP_EICONFR), val);
2661         return(ptr);
2662 }
2663
2664 /*****************************************************************************/
2665
2666 static void stli_ecpeireset(stlibrd_t *brdp)
2667 {       
2668         outb((brdp->iobase + ECP_EICONFR), ECP_EISTOP);
2669         DELAY(10);
2670         outb((brdp->iobase + ECP_EICONFR), ECP_EIDISABLE);
2671         DELAY(500);
2672 }
2673
2674 /*****************************************************************************/
2675
2676 /*
2677  *      The following routines act on ONboards.
2678  */
2679
2680 static void stli_onbinit(stlibrd_t *brdp)
2681 {
2682         unsigned long   memconf;
2683         int             i;
2684
2685 #if STLDEBUG
2686         kprintf("stli_onbinit(brdp=%d)\n", (int) brdp);
2687 #endif
2688
2689         outb((brdp->iobase + ONB_ATCONFR), ONB_ATSTOP);
2690         DELAY(10);
2691         outb((brdp->iobase + ONB_ATCONFR), ONB_ATDISABLE);
2692         for (i = 0; (i < 1000); i++)
2693                 DELAY(1000);
2694
2695         memconf = (brdp->paddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
2696         outb((brdp->iobase + ONB_ATMEMAR), memconf);
2697         outb(brdp->iobase, 0x1);
2698         DELAY(1000);
2699 }
2700
2701 /*****************************************************************************/
2702
2703 static void stli_onbenable(stlibrd_t *brdp)
2704 {       
2705 #if STLDEBUG
2706         kprintf("stli_onbenable(brdp=%x)\n", (int) brdp);
2707 #endif
2708         outb((brdp->iobase + ONB_ATCONFR), (ONB_ATENABLE | brdp->confbits));
2709 }
2710
2711 /*****************************************************************************/
2712
2713 static void stli_onbdisable(stlibrd_t *brdp)
2714 {       
2715 #if STLDEBUG
2716         kprintf("stli_onbdisable(brdp=%x)\n", (int) brdp);
2717 #endif
2718         outb((brdp->iobase + ONB_ATCONFR), (ONB_ATDISABLE | brdp->confbits));
2719 }
2720
2721 /*****************************************************************************/
2722
2723 static char *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
2724 {       
2725         void    *ptr;
2726
2727 #if STLDEBUG
2728         kprintf("stli_onbgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
2729                 (int) offset);
2730 #endif
2731
2732         if (offset > brdp->memsize) {
2733                 kprintf("STALLION: shared memory pointer=%x out of range at "
2734                         "line=%d(%d), brd=%d\n", (int) offset, line,
2735                         __LINE__, brdp->brdnr);
2736                 ptr = 0;
2737         } else {
2738                 ptr = (char *) brdp->vaddr + (offset % ONB_ATPAGESIZE);
2739         }
2740         return(ptr);
2741 }
2742
2743 /*****************************************************************************/
2744
2745 static void stli_onbreset(stlibrd_t *brdp)
2746 {       
2747         int     i;
2748
2749 #if STLDEBUG
2750         kprintf("stli_onbreset(brdp=%x)\n", (int) brdp);
2751 #endif
2752
2753         outb((brdp->iobase + ONB_ATCONFR), ONB_ATSTOP);
2754         DELAY(10);
2755         outb((brdp->iobase + ONB_ATCONFR), ONB_ATDISABLE);
2756         for (i = 0; (i < 1000); i++)
2757                 DELAY(1000);
2758 }
2759
2760 /*****************************************************************************/
2761
2762 /*
2763  *      The following routines act on ONboard EISA.
2764  */
2765
2766 static void stli_onbeinit(stlibrd_t *brdp)
2767 {
2768         unsigned long   memconf;
2769         int             i;
2770
2771 #if STLDEBUG
2772         kprintf("stli_onbeinit(brdp=%d)\n", (int) brdp);
2773 #endif
2774
2775         outb((brdp->iobase + ONB_EIBRDENAB), 0x1);
2776         outb((brdp->iobase + ONB_EICONFR), ONB_EISTOP);
2777         DELAY(10);
2778         outb((brdp->iobase + ONB_EICONFR), ONB_EIDISABLE);
2779         for (i = 0; (i < 1000); i++)
2780                 DELAY(1000);
2781
2782         memconf = (brdp->paddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
2783         outb((brdp->iobase + ONB_EIMEMARL), memconf);
2784         memconf = (brdp->paddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
2785         outb((brdp->iobase + ONB_EIMEMARH), memconf);
2786         outb(brdp->iobase, 0x1);
2787         DELAY(1000);
2788 }
2789
2790 /*****************************************************************************/
2791
2792 static void stli_onbeenable(stlibrd_t *brdp)
2793 {       
2794 #if STLDEBUG
2795         kprintf("stli_onbeenable(brdp=%x)\n", (int) brdp);
2796 #endif
2797         outb((brdp->iobase + ONB_EICONFR), ONB_EIENABLE);
2798 }
2799
2800 /*****************************************************************************/
2801
2802 static void stli_onbedisable(stlibrd_t *brdp)
2803 {       
2804 #if STLDEBUG
2805         kprintf("stli_onbedisable(brdp=%x)\n", (int) brdp);
2806 #endif
2807         outb((brdp->iobase + ONB_EICONFR), ONB_EIDISABLE);
2808 }
2809
2810 /*****************************************************************************/
2811
2812 static char *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
2813 {       
2814         void            *ptr;
2815         unsigned char   val;
2816
2817 #if STLDEBUG
2818         kprintf("stli_onbegetmemptr(brdp=%x,offset=%x,line=%d)\n", (int) brdp,
2819                 (int) offset, line);
2820 #endif
2821
2822         if (offset > brdp->memsize) {
2823                 kprintf("STALLION: shared memory pointer=%x out of range at "
2824                         "line=%d(%d), brd=%d\n", (int) offset, line,
2825                         __LINE__, brdp->brdnr);
2826                 ptr = 0;
2827                 val = 0;
2828         } else {
2829                 ptr = (char *) brdp->vaddr + (offset % ONB_EIPAGESIZE);
2830                 if (offset < ONB_EIPAGESIZE)
2831                         val = ONB_EIENABLE;
2832                 else
2833                         val = ONB_EIENABLE | 0x40;
2834         }
2835         outb((brdp->iobase + ONB_EICONFR), val);
2836         return(ptr);
2837 }
2838
2839 /*****************************************************************************/
2840
2841 static void stli_onbereset(stlibrd_t *brdp)
2842 {       
2843         int     i;
2844
2845 #if STLDEBUG
2846         kprintf("stli_onbereset(brdp=%x)\n", (int) brdp);
2847 #endif
2848
2849         outb((brdp->iobase + ONB_EICONFR), ONB_EISTOP);
2850         DELAY(10);
2851         outb((brdp->iobase + ONB_EICONFR), ONB_EIDISABLE);
2852         for (i = 0; (i < 1000); i++)
2853                 DELAY(1000);
2854 }
2855
2856 /*****************************************************************************/
2857
2858 /*
2859  *      The following routines act on Brumby boards.
2860  */
2861
2862 static void stli_bbyinit(stlibrd_t *brdp)
2863 {
2864         int     i;
2865
2866 #if STLDEBUG
2867         kprintf("stli_bbyinit(brdp=%d)\n", (int) brdp);
2868 #endif
2869
2870         outb((brdp->iobase + BBY_ATCONFR), BBY_ATSTOP);
2871         DELAY(10);
2872         outb((brdp->iobase + BBY_ATCONFR), 0);
2873         for (i = 0; (i < 1000); i++)
2874                 DELAY(1000);
2875         outb(brdp->iobase, 0x1);
2876         DELAY(1000);
2877 }
2878
2879 /*****************************************************************************/
2880
2881 static char *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
2882 {       
2883         void            *ptr;
2884         unsigned char   val;
2885
2886 #if STLDEBUG
2887         kprintf("stli_bbygetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
2888                 (int) offset);
2889 #endif
2890
2891         if (offset > brdp->memsize) {
2892                 kprintf("STALLION: shared memory pointer=%x out of range at "
2893                         "line=%d(%d), brd=%d\n", (int) offset, line,
2894                         __LINE__, brdp->brdnr);
2895                 ptr = 0;
2896                 val = 0;
2897         } else {
2898                 ptr = (char *) brdp->vaddr + (offset % BBY_PAGESIZE);
2899                 val = (unsigned char) (offset / BBY_PAGESIZE);
2900         }
2901         outb((brdp->iobase + BBY_ATCONFR), val);
2902         return(ptr);
2903 }
2904
2905 /*****************************************************************************/
2906
2907 static void stli_bbyreset(stlibrd_t *brdp)
2908 {       
2909         int     i;
2910
2911 #if STLDEBUG
2912         kprintf("stli_bbyreset(brdp=%x)\n", (int) brdp);
2913 #endif
2914
2915         outb((brdp->iobase + BBY_ATCONFR), BBY_ATSTOP);
2916         DELAY(10);
2917         outb((brdp->iobase + BBY_ATCONFR), 0);
2918         for (i = 0; (i < 1000); i++)
2919                 DELAY(1000);
2920 }
2921
2922 /*****************************************************************************/
2923
2924 /*
2925  *      The following routines act on original old Stallion boards.
2926  */
2927
2928 static void stli_stalinit(stlibrd_t *brdp)
2929 {
2930         int     i;
2931
2932 #if STLDEBUG
2933         kprintf("stli_stalinit(brdp=%d)\n", (int) brdp);
2934 #endif
2935
2936         outb(brdp->iobase, 0x1);
2937         for (i = 0; (i < 1000); i++)
2938                 DELAY(1000);
2939 }
2940
2941 /*****************************************************************************/
2942
2943 static char *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
2944 {       
2945         void    *ptr;
2946
2947 #if STLDEBUG
2948         kprintf("stli_stalgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
2949                 (int) offset);
2950 #endif
2951
2952         if (offset > brdp->memsize) {
2953                 kprintf("STALLION: shared memory pointer=%x out of range at "
2954                         "line=%d(%d), brd=%d\n", (int) offset, line,
2955                         __LINE__, brdp->brdnr);
2956                 ptr = 0;
2957         } else {
2958                 ptr = (char *) brdp->vaddr + (offset % STAL_PAGESIZE);
2959         }
2960         return(ptr);
2961 }
2962
2963 /*****************************************************************************/
2964
2965 static void stli_stalreset(stlibrd_t *brdp)
2966 {       
2967         volatile unsigned long  *vecp;
2968         int                     i;
2969
2970 #if STLDEBUG
2971         kprintf("stli_stalreset(brdp=%x)\n", (int) brdp);
2972 #endif
2973
2974         vecp = (volatile unsigned long *) ((char *) brdp->vaddr + 0x30);
2975         *vecp = 0xffff0000;
2976         outb(brdp->iobase, 0);
2977         for (i = 0; (i < 1000); i++)
2978                 DELAY(1000);
2979 }
2980
2981 /*****************************************************************************/
2982
2983 /*
2984  *      Try to find an ECP board and initialize it. This handles only ECP
2985  *      board types.
2986  */
2987
2988 static int stli_initecp(stlibrd_t *brdp)
2989 {
2990         cdkecpsig_t     sig;
2991         cdkecpsig_t     *sigsp;
2992         unsigned int    status, nxtid;
2993         int             panelnr;
2994
2995 #if STLDEBUG
2996         kprintf("stli_initecp(brdp=%x)\n", (int) brdp);
2997 #endif
2998
2999 /*
3000  *      Do a basic sanity check on the IO and memory addresses.
3001  */
3002         if ((brdp->iobase == 0) || (brdp->paddr == 0))
3003                 return(EINVAL);
3004
3005 /*
3006  *      Based on the specific board type setup the common vars to access
3007  *      and enable shared memory. Set all board specific information now
3008  *      as well.
3009  */
3010         switch (brdp->brdtype) {
3011         case BRD_ECP:
3012                 brdp->memsize = ECP_MEMSIZE;
3013                 brdp->pagesize = ECP_ATPAGESIZE;
3014                 brdp->init = stli_ecpinit;
3015                 brdp->enable = stli_ecpenable;
3016                 brdp->reenable = stli_ecpenable;
3017                 brdp->disable = stli_ecpdisable;
3018                 brdp->getmemptr = stli_ecpgetmemptr;
3019                 brdp->intr = stli_ecpintr;
3020                 brdp->reset = stli_ecpreset;
3021                 break;
3022
3023         case BRD_ECPE:
3024                 brdp->memsize = ECP_MEMSIZE;
3025                 brdp->pagesize = ECP_EIPAGESIZE;
3026                 brdp->init = stli_ecpeiinit;
3027                 brdp->enable = stli_ecpeienable;
3028                 brdp->reenable = stli_ecpeienable;
3029                 brdp->disable = stli_ecpeidisable;
3030                 brdp->getmemptr = stli_ecpeigetmemptr;
3031                 brdp->intr = stli_ecpintr;
3032                 brdp->reset = stli_ecpeireset;
3033                 break;
3034
3035         default:
3036                 return(EINVAL);
3037         }
3038
3039 /*
3040  *      The per-board operations structure is all setup, so now lets go
3041  *      and get the board operational. Firstly initialize board configuration
3042  *      registers.
3043  */
3044         EBRDINIT(brdp);
3045
3046 /*
3047  *      Now that all specific code is set up, enable the shared memory and
3048  *      look for the a signature area that will tell us exactly what board
3049  *      this is, and what it is connected to it.
3050  */
3051         EBRDENABLE(brdp);
3052         sigsp = (cdkecpsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
3053         bcopy(sigsp, &sig, sizeof(cdkecpsig_t));
3054         EBRDDISABLE(brdp);
3055
3056 #if 0
3057         kprintf("%s(%d): sig-> magic=%x rom=%x panel=%x,%x,%x,%x,%x,%x,%x,%x\n",
3058                 __file__, __LINE__, (int) sig.magic, sig.romver,
3059                 sig.panelid[0], (int) sig.panelid[1], (int) sig.panelid[2],
3060                 (int) sig.panelid[3], (int) sig.panelid[4],
3061                 (int) sig.panelid[5], (int) sig.panelid[6],
3062                 (int) sig.panelid[7]);
3063 #endif
3064
3065         if (sig.magic != ECP_MAGIC)
3066                 return(ENXIO);
3067
3068 /*
3069  *      Scan through the signature looking at the panels connected to the
3070  *      board. Calculate the total number of ports as we go.
3071  */
3072         for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
3073                 status = sig.panelid[nxtid];
3074                 if ((status & ECH_PNLIDMASK) != nxtid)
3075                         break;
3076                 brdp->panelids[panelnr] = status;
3077                 if (status & ECH_PNL16PORT) {
3078                         brdp->panels[panelnr] = 16;
3079                         brdp->nrports += 16;
3080                         nxtid += 2;
3081                 } else {
3082                         brdp->panels[panelnr] = 8;
3083                         brdp->nrports += 8;
3084                         nxtid++;
3085                 }
3086                 brdp->nrpanels++;
3087         }
3088
3089         brdp->state |= BST_FOUND;
3090         return(0);
3091 }
3092
3093 /*****************************************************************************/
3094
3095 /*
3096  *      Try to find an ONboard, Brumby or Stallion board and initialize it.
3097  *      This handles only these board types.
3098  */
3099
3100 static int stli_initonb(stlibrd_t *brdp)
3101 {
3102         cdkonbsig_t     sig;
3103         cdkonbsig_t     *sigsp;
3104         int             i;
3105
3106 #if STLDEBUG
3107         kprintf("stli_initonb(brdp=%x)\n", (int) brdp);
3108 #endif
3109
3110 /*
3111  *      Do a basic sanity check on the IO and memory addresses.
3112  */
3113         if ((brdp->iobase == 0) || (brdp->paddr == 0))
3114                 return(EINVAL);
3115
3116 /*
3117  *      Based on the specific board type setup the common vars to access
3118  *      and enable shared memory. Set all board specific information now
3119  *      as well.
3120  */
3121         switch (brdp->brdtype) {
3122         case BRD_ONBOARD:
3123         case BRD_ONBOARD32:
3124         case BRD_ONBOARD2:
3125         case BRD_ONBOARD2_32:
3126         case BRD_ONBOARDRS:
3127                 brdp->memsize = ONB_MEMSIZE;
3128                 brdp->pagesize = ONB_ATPAGESIZE;
3129                 brdp->init = stli_onbinit;
3130                 brdp->enable = stli_onbenable;
3131                 brdp->reenable = stli_onbenable;
3132                 brdp->disable = stli_onbdisable;
3133                 brdp->getmemptr = stli_onbgetmemptr;
3134                 brdp->intr = stli_ecpintr;
3135                 brdp->reset = stli_onbreset;
3136                 brdp->confbits = (brdp->paddr > 0x100000) ? ONB_HIMEMENAB : 0;
3137                 break;
3138
3139         case BRD_ONBOARDE:
3140                 brdp->memsize = ONB_EIMEMSIZE;
3141                 brdp->pagesize = ONB_EIPAGESIZE;
3142                 brdp->init = stli_onbeinit;
3143                 brdp->enable = stli_onbeenable;
3144                 brdp->reenable = stli_onbeenable;
3145                 brdp->disable = stli_onbedisable;
3146                 brdp->getmemptr = stli_onbegetmemptr;
3147                 brdp->intr = stli_ecpintr;
3148                 brdp->reset = stli_onbereset;
3149                 break;
3150
3151         case BRD_BRUMBY4:
3152         case BRD_BRUMBY8:
3153         case BRD_BRUMBY16:
3154                 brdp->memsize = BBY_MEMSIZE;
3155                 brdp->pagesize = BBY_PAGESIZE;
3156                 brdp->init = stli_bbyinit;
3157                 brdp->enable = NULL;
3158                 brdp->reenable = NULL;
3159                 brdp->disable = NULL;
3160                 brdp->getmemptr = stli_bbygetmemptr;
3161                 brdp->intr = stli_ecpintr;
3162                 brdp->reset = stli_bbyreset;
3163                 break;
3164
3165         case BRD_STALLION:
3166                 brdp->memsize = STAL_MEMSIZE;
3167                 brdp->pagesize = STAL_PAGESIZE;
3168                 brdp->init = stli_stalinit;
3169                 brdp->enable = NULL;
3170                 brdp->reenable = NULL;
3171                 brdp->disable = NULL;
3172                 brdp->getmemptr = stli_stalgetmemptr;
3173                 brdp->intr = stli_ecpintr;
3174                 brdp->reset = stli_stalreset;
3175                 break;
3176
3177         default:
3178                 return(EINVAL);
3179         }
3180
3181 /*
3182  *      The per-board operations structure is all setup, so now lets go
3183  *      and get the board operational. Firstly initialize board configuration
3184  *      registers.
3185  */
3186         EBRDINIT(brdp);
3187
3188 /*
3189  *      Now that all specific code is set up, enable the shared memory and
3190  *      look for the a signature area that will tell us exactly what board
3191  *      this is, and how many ports.
3192  */
3193         EBRDENABLE(brdp);
3194         sigsp = (cdkonbsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
3195         bcopy(sigsp, &sig, sizeof(cdkonbsig_t));
3196         EBRDDISABLE(brdp);
3197
3198 #if 0
3199         kprintf("%s(%d): sig-> magic=%x:%x:%x:%x romver=%x amask=%x:%x:%x\n",
3200                 __file__, __LINE__, sig.magic0, sig.magic1, sig.magic2,
3201                 sig.magic3, sig.romver, sig.amask0, sig.amask1, sig.amask2);
3202 #endif
3203
3204         if ((sig.magic0 != ONB_MAGIC0) || (sig.magic1 != ONB_MAGIC1) ||
3205             (sig.magic2 != ONB_MAGIC2) || (sig.magic3 != ONB_MAGIC3))
3206                 return(ENXIO);
3207
3208 /*
3209  *      Scan through the signature alive mask and calculate how many ports
3210  *      there are on this board.
3211  */
3212         brdp->nrpanels = 1;
3213         if (sig.amask1) {
3214                 brdp->nrports = 32;
3215         } else {
3216                 for (i = 0; (i < 16); i++) {
3217                         if (((sig.amask0 << i) & 0x8000) == 0)
3218                                 break;
3219                 }
3220                 brdp->nrports = i;
3221         }
3222         brdp->panels[0] = brdp->nrports;
3223
3224         brdp->state |= BST_FOUND;
3225         return(0);
3226 }
3227
3228 /*****************************************************************************/
3229
3230 /*
3231  *      Start up a running board. This routine is only called after the
3232  *      code has been down loaded to the board and is operational. It will
3233  *      read in the memory map, and get the show on the road...
3234  */
3235
3236 static int stli_startbrd(stlibrd_t *brdp)
3237 {
3238         volatile cdkhdr_t       *hdrp;
3239         volatile cdkmem_t       *memp;
3240         volatile cdkasy_t       *ap;
3241         stliport_t              *portp;
3242         int                     portnr, nrdevs, i, rc;
3243
3244 #if STLDEBUG
3245         kprintf("stli_startbrd(brdp=%x)\n", (int) brdp);
3246 #endif
3247
3248         rc = 0;
3249
3250         crit_enter();
3251         EBRDENABLE(brdp);
3252         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3253         nrdevs = hdrp->nrdevs;
3254
3255 #if 0
3256         kprintf("%s(%d): CDK version %d.%d.%d --> nrdevs=%d memp=%x hostp=%x "
3257                 "slavep=%x\n", __file__, __LINE__, hdrp->ver_release,
3258                 hdrp->ver_modification, hdrp->ver_fix, nrdevs,
3259                 (int) hdrp->memp, (int) hdrp->hostp, (int) hdrp->slavep);
3260 #endif
3261
3262         if (nrdevs < (brdp->nrports + 1)) {
3263                 kprintf("STALLION: slave failed to allocate memory for all "
3264                         "devices, devices=%d\n", nrdevs);
3265                 brdp->nrports = nrdevs - 1;
3266         }
3267         brdp->nrdevs = nrdevs;
3268         brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
3269         brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
3270         brdp->bitsize = (nrdevs + 7) / 8;
3271         memp = (volatile cdkmem_t *) (void *) (uintptr_t) hdrp->memp;
3272         if ((uintptr_t)(volatile void *)memp > brdp->memsize) {
3273                 kprintf("STALLION: corrupted shared memory region?\n");
3274                 rc = EIO;
3275                 goto stli_donestartup;
3276         }
3277         memp = (volatile cdkmem_t *) EBRDGETMEMPTR(brdp,
3278                                                    (uintptr_t)(volatile void *)memp);
3279         if (memp->dtype != TYP_ASYNCTRL) {
3280                 kprintf("STALLION: no slave control device found\n");
3281                 rc = EIO;
3282                 goto stli_donestartup;
3283         }
3284         memp++;
3285
3286 /*
3287  *      Cycle through memory allocation of each port. We are guaranteed to
3288  *      have all ports inside the first page of slave window, so no need to
3289  *      change pages while reading memory map.
3290  */
3291         for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
3292                 if (memp->dtype != TYP_ASYNC)
3293                         break;
3294                 portp = brdp->ports[portnr];
3295                 if (portp == NULL)
3296                         break;
3297                 portp->devnr = i;
3298                 portp->addr = memp->offset;
3299                 portp->reqidx = (unsigned char) (i * 8 / nrdevs);
3300                 portp->reqbit = (unsigned char) (0x1 << portp->reqidx);
3301                 portp->portidx = (unsigned char) (i / 8);
3302                 portp->portbit = (unsigned char) (0x1 << (i % 8));
3303         }
3304
3305         hdrp->slavereq = 0xff;
3306
3307 /*
3308  *      For each port setup a local copy of the RX and TX buffer offsets
3309  *      and sizes. We do this separate from the above, because we need to
3310  *      move the shared memory page...
3311  */
3312         for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
3313                 portp = brdp->ports[portnr];
3314                 if (portp == NULL)
3315                         break;
3316                 if (portp->addr == 0)
3317                         break;
3318                 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
3319                 if (ap != NULL) {
3320                         portp->rxsize = ap->rxq.size;
3321                         portp->txsize = ap->txq.size;
3322                         portp->rxoffset = ap->rxq.offset;
3323                         portp->txoffset = ap->txq.offset;
3324                 }
3325         }
3326
3327 stli_donestartup:
3328         EBRDDISABLE(brdp);
3329         crit_exit();
3330
3331         if (rc == 0)
3332                 brdp->state |= BST_STARTED;
3333
3334         if (stli_doingtimeout == 0) {
3335                 stli_doingtimeout++;
3336                 callout_init(&stli_poll_ch);
3337                 callout_reset(&stli_poll_ch, 1, stli_poll, NULL);
3338         }
3339
3340         return(rc);
3341 }
3342
3343 /*****************************************************************************/
3344
3345 /*
3346  *      Probe and initialize the specified board.
3347  */
3348
3349 static int stli_brdinit(stlibrd_t *brdp)
3350 {
3351 #if STLDEBUG
3352         kprintf("stli_brdinit(brdp=%x)\n", (int) brdp);
3353 #endif
3354
3355         stli_brds[brdp->brdnr] = brdp;
3356
3357         switch (brdp->brdtype) {
3358         case BRD_ECP:
3359         case BRD_ECPE:
3360                 stli_initecp(brdp);
3361                 break;
3362         case BRD_ONBOARD:
3363         case BRD_ONBOARDE:
3364         case BRD_ONBOARD2:
3365         case BRD_ONBOARD32:
3366         case BRD_ONBOARD2_32:
3367         case BRD_ONBOARDRS:
3368         case BRD_BRUMBY4:
3369         case BRD_BRUMBY8:
3370         case BRD_BRUMBY16:
3371         case BRD_STALLION:
3372                 stli_initonb(brdp);
3373                 break;
3374         case BRD_EASYIO:
3375         case BRD_ECH:
3376         case BRD_ECHMC:
3377         case BRD_ECHPCI:
3378                 kprintf("STALLION: %s board type not supported in this driver\n",
3379                         stli_brdnames[brdp->brdtype]);
3380                 return(ENODEV);
3381         default:
3382                 kprintf("STALLION: unit=%d is unknown board type=%d\n",
3383                         brdp->brdnr, brdp->brdtype);
3384                 return(ENODEV);
3385         }
3386
3387         return(0);
3388 }
3389
3390 /*****************************************************************************/
3391
3392 /*
3393  *      Finish off the remaining initialization for a board.
3394  */
3395
3396 static int stli_brdattach(stlibrd_t *brdp)
3397 {
3398 #if STLDEBUG
3399         kprintf("stli_brdattach(brdp=%x)\n", (int) brdp);
3400 #endif
3401
3402 #if 0
3403         if ((brdp->state & BST_FOUND) == 0) {
3404                 kprintf("STALLION: %s board not found, unit=%d io=%x mem=%x\n",
3405                         stli_brdnames[brdp->brdtype], brdp->brdnr,
3406                         brdp->iobase, (int) brdp->paddr);
3407                 return(ENXIO);
3408         }
3409 #endif
3410
3411         stli_initports(brdp);
3412         kprintf("stli%d: %s (driver version %s), unit=%d nrpanels=%d "
3413                 "nrports=%d\n", brdp->unitid, stli_brdnames[brdp->brdtype],
3414                 stli_drvversion, brdp->brdnr, brdp->nrpanels, brdp->nrports);
3415         return(0);
3416 }
3417
3418 /*****************************************************************************/
3419
3420 /*****************************************************************************/
3421
3422 /*
3423  *      Return the board stats structure to user app.
3424  */
3425
3426 static int stli_getbrdstats(caddr_t data)
3427 {
3428         stlibrd_t       *brdp;
3429         int             i;
3430
3431 #if STLDEBUG
3432         kprintf("stli_getbrdstats(data=%p)\n", (void *) data);
3433 #endif
3434
3435         stli_brdstats = *((combrd_t *) data);
3436         if (stli_brdstats.brd >= STL_MAXBRDS)
3437                 return(ENODEV);
3438         brdp = stli_brds[stli_brdstats.brd];
3439         if (brdp == NULL)
3440                 return(ENODEV);
3441
3442         bzero(&stli_brdstats, sizeof(combrd_t));
3443         stli_brdstats.brd = brdp->brdnr;
3444         stli_brdstats.type = brdp->brdtype;
3445         stli_brdstats.hwid = 0;
3446         stli_brdstats.state = brdp->state;
3447         stli_brdstats.ioaddr = brdp->iobase;
3448         stli_brdstats.memaddr = brdp->paddr;
3449         stli_brdstats.nrpanels = brdp->nrpanels;
3450         stli_brdstats.nrports = brdp->nrports;
3451         for (i = 0; (i < brdp->nrpanels); i++) {
3452                 stli_brdstats.panels[i].panel = i;
3453                 stli_brdstats.panels[i].hwid = brdp->panelids[i];
3454                 stli_brdstats.panels[i].nrports = brdp->panels[i];
3455         }
3456
3457         *((combrd_t *) data) = stli_brdstats;
3458         return(0);
3459 }
3460
3461 /*****************************************************************************/
3462
3463 /*
3464  *      Resolve the referenced port number into a port struct pointer.
3465  */
3466
3467 static stliport_t *stli_getport(int brdnr, int panelnr, int portnr)
3468 {
3469         stlibrd_t       *brdp;
3470         int             i;
3471
3472         if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
3473                 return(NULL);
3474         brdp = stli_brds[brdnr];
3475         if (brdp == NULL)
3476                 return(NULL);
3477         for (i = 0; (i < panelnr); i++)
3478                 portnr += brdp->panels[i];
3479         if ((portnr < 0) || (portnr >= brdp->nrports))
3480                 return(NULL);
3481         return(brdp->ports[portnr]);
3482 }
3483
3484 /*****************************************************************************/
3485
3486 /*
3487  *      Return the port stats structure to user app. A NULL port struct
3488  *      pointer passed in means that we need to find out from the app
3489  *      what port to get stats for (used through board control device).
3490  */
3491
3492 static int stli_getportstats(stliport_t *portp, caddr_t data)
3493 {
3494         stlibrd_t       *brdp;
3495         int             rc;
3496
3497         if (portp == NULL) {
3498                 stli_comstats = *((comstats_t *) data);
3499                 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
3500                         stli_comstats.port);
3501                 if (portp == NULL)
3502                         return(ENODEV);
3503         }
3504
3505         brdp = stli_brds[portp->brdnr];
3506         if (brdp == NULL)
3507                 return(ENODEV);
3508
3509         if (brdp->state & BST_STARTED) {
3510                 if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS, &stli_cdkstats,
3511                     sizeof(asystats_t), 1)) < 0)
3512                         return(rc);
3513         } else {
3514                 bzero(&stli_cdkstats, sizeof(asystats_t));
3515         }
3516
3517         stli_comstats.brd = portp->brdnr;
3518         stli_comstats.panel = portp->panelnr;
3519         stli_comstats.port = portp->portnr;
3520         stli_comstats.state = portp->state;
3521         /*stli_comstats.flags = portp->flags;*/
3522         stli_comstats.ttystate = portp->tty.t_state;
3523         stli_comstats.cflags = portp->tty.t_cflag;
3524         stli_comstats.iflags = portp->tty.t_iflag;
3525         stli_comstats.oflags = portp->tty.t_oflag;
3526         stli_comstats.lflags = portp->tty.t_lflag;
3527
3528         stli_comstats.txtotal = stli_cdkstats.txchars;
3529         stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
3530         stli_comstats.txbuffered = stli_cdkstats.txringq;
3531         stli_comstats.rxbuffered = stli_cdkstats.rxringq;
3532         stli_comstats.rxoverrun = stli_cdkstats.overruns;
3533         stli_comstats.rxparity = stli_cdkstats.parity;
3534         stli_comstats.rxframing = stli_cdkstats.framing;
3535         stli_comstats.rxlost = stli_cdkstats.ringover + portp->rxlost;
3536         stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
3537         stli_comstats.txbreaks = stli_cdkstats.txbreaks;
3538         stli_comstats.txxon = stli_cdkstats.txstart;
3539         stli_comstats.txxoff = stli_cdkstats.txstop;
3540         stli_comstats.rxxon = stli_cdkstats.rxstart;
3541         stli_comstats.rxxoff = stli_cdkstats.rxstop;
3542         stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
3543         stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
3544         stli_comstats.modem = stli_cdkstats.dcdcnt;
3545         stli_comstats.hwid = stli_cdkstats.hwid;
3546         stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
3547
3548         *((comstats_t *) data) = stli_comstats;
3549         return(0);
3550 }
3551
3552 /*****************************************************************************/
3553
3554 /*
3555  *      Clear the port stats structure. We also return it zeroed out...
3556  */
3557
3558 static int stli_clrportstats(stliport_t *portp, caddr_t data)
3559 {
3560         stlibrd_t       *brdp;
3561         int             rc;
3562
3563         if (portp == NULL) {
3564                 stli_comstats = *((comstats_t *) data);
3565                 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
3566                         stli_comstats.port);
3567                 if (portp == NULL)
3568                         return(ENODEV);
3569         }
3570
3571         brdp = stli_brds[portp->brdnr];
3572         if (brdp == NULL)
3573                 return(ENODEV);
3574
3575         if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, 0, 0, 0)) < 0)
3576                 return(rc);
3577
3578         portp->rxlost = 0;
3579         bzero(&stli_comstats, sizeof(comstats_t));
3580         stli_comstats.brd = portp->brdnr;
3581         stli_comstats.panel = portp->panelnr;
3582         stli_comstats.port = portp->portnr;
3583
3584         *((comstats_t *) data) = stli_comstats;
3585         return(0);
3586 }
3587
3588 /*****************************************************************************/
3589
3590 /*
3591  *      Code to handle an "staliomem" read and write operations. This device
3592  *      is the contents of the board shared memory. It is used for down
3593  *      loading the slave image (and debugging :-)
3594  */
3595
3596 STATIC int stli_memrw(cdev_t dev, struct uio *uiop, int flag)
3597 {
3598         stlibrd_t       *brdp;
3599         void            *memptr;
3600         int             brdnr, size, n, error;
3601
3602 #if STLDEBUG
3603         kprintf("stli_memrw(dev=%x,uiop=%x,flag=%x)\n", (int) dev,
3604                 (int) uiop, flag);
3605 #endif
3606
3607         brdnr = minor(dev) & 0x7;
3608         brdp = stli_brds[brdnr];
3609         if (brdp == NULL)
3610                 return(ENODEV);
3611         if (brdp->state == 0)
3612                 return(ENODEV);
3613
3614         if (uiop->uio_offset >= brdp->memsize)
3615                 return(0);
3616
3617         error = 0;
3618         size = brdp->memsize - uiop->uio_offset;
3619
3620         crit_enter();
3621         EBRDENABLE(brdp);
3622         while (size > 0) {
3623                 memptr = (void *) EBRDGETMEMPTR(brdp, uiop->uio_offset);
3624                 n = MIN(size, (brdp->pagesize -
3625                         (((unsigned long) uiop->uio_offset) % brdp->pagesize)));
3626                 error = uiomove(memptr, n, uiop);
3627                 if ((uiop->uio_resid == 0) || error)
3628                         break;
3629         }
3630         EBRDDISABLE(brdp);
3631         crit_exit();
3632
3633         return(error);
3634 }
3635
3636 /*****************************************************************************/
3637
3638 /*
3639  *      The "staliomem" device is also required to do some special operations
3640  *      on the board. We need to be able to send an interrupt to the board,
3641  *      reset it, and start/stop it.
3642  */
3643
3644 static int stli_memioctl(cdev_t dev, unsigned long cmd, caddr_t data, int flag)
3645 {
3646         stlibrd_t       *brdp;
3647         int             brdnr, rc;
3648
3649 #if STLDEBUG
3650         kprintf("stli_memioctl(dev=%s,cmd=%lx,data=%p,flag=%x)\n",
3651                 devtoname(dev), cmd, (void *) data, flag);
3652 #endif
3653
3654 /*
3655  *      Handle board independant ioctls first.
3656  */
3657         switch (cmd) {
3658         case COM_GETPORTSTATS:
3659                 return(stli_getportstats(NULL, data));
3660                 break;
3661         case COM_CLRPORTSTATS:
3662                 return(stli_clrportstats(NULL, data));
3663                 break;
3664         case COM_GETBRDSTATS:
3665                 return(stli_getbrdstats(data));
3666                 break;
3667         default:
3668                 break;
3669         }
3670
3671 /*
3672  *      Handle board dependant ioctls now.
3673  */
3674         brdnr = minor(dev) & 0x7;
3675         brdp = stli_brds[brdnr];
3676         if (brdp == NULL)
3677                 return(ENODEV);
3678         if (brdp->state == 0)
3679                 return(ENODEV);
3680
3681         rc = 0;
3682
3683         switch (cmd) {
3684         case STL_BINTR:
3685                 EBRDINTR(brdp);
3686                 break;
3687         case STL_BSTART:
3688                 rc = stli_startbrd(brdp);
3689                 break;
3690         case STL_BSTOP:
3691                 brdp->state &= ~BST_STARTED;
3692                 break;
3693         case STL_BRESET:
3694                 brdp->state &= ~BST_STARTED;
3695                 EBRDRESET(brdp);
3696                 if (stli_shared == 0) {
3697                         if (brdp->reenable != NULL)
3698                                 (* brdp->reenable)(brdp);
3699                 }
3700                 break;
3701         case COM_GETPORTSTATS:
3702                 rc = stli_getportstats(NULL, data);
3703                 break;
3704         case COM_CLRPORTSTATS:
3705                 rc = stli_clrportstats(NULL, data);
3706                 break;
3707         case COM_GETBRDSTATS:
3708                 rc = stli_getbrdstats(data);
3709                 break;
3710         default:
3711                 rc = ENOTTY;
3712                 break;
3713         }
3714
3715         return(rc);
3716 }
3717
3718 /*****************************************************************************/