vserver 1.9.3
[linux-2.6.git] / drivers / char / istallion.c
1 /*****************************************************************************/
2
3 /*
4  *      istallion.c  -- stallion intelligent multiport serial driver.
5  *
6  *      Copyright (C) 1996-1999  Stallion Technologies
7  *      Copyright (C) 1994-1996  Greg Ungerer.
8  *
9  *      This code is loosely based on the Linux serial driver, written by
10  *      Linus Torvalds, Theodore T'so and others.
11  *
12  *      This program is free software; you can redistribute it and/or modify
13  *      it under the terms of the GNU General Public License as published by
14  *      the Free Software Foundation; either version 2 of the License, or
15  *      (at your option) any later version.
16  *
17  *      This program is distributed in the hope that it will be useful,
18  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *      GNU General Public License for more details.
21  *
22  *      You should have received a copy of the GNU General Public License
23  *      along with this program; if not, write to the Free Software
24  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 /*****************************************************************************/
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/serial.h>
36 #include <linux/cdk.h>
37 #include <linux/comstats.h>
38 #include <linux/istallion.h>
39 #include <linux/ioport.h>
40 #include <linux/delay.h>
41 #include <linux/init.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/device.h>
44
45 #include <asm/io.h>
46 #include <asm/uaccess.h>
47
48 #ifdef CONFIG_PCI
49 #include <linux/pci.h>
50 #endif
51
52 /*****************************************************************************/
53
54 /*
55  *      Define different board types. Not all of the following board types
56  *      are supported by this driver. But I will use the standard "assigned"
57  *      board numbers. Currently supported boards are abbreviated as:
58  *      ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
59  *      STAL = Stallion.
60  */
61 #define BRD_UNKNOWN     0
62 #define BRD_STALLION    1
63 #define BRD_BRUMBY4     2
64 #define BRD_ONBOARD2    3
65 #define BRD_ONBOARD     4
66 #define BRD_BRUMBY8     5
67 #define BRD_BRUMBY16    6
68 #define BRD_ONBOARDE    7
69 #define BRD_ONBOARD32   9
70 #define BRD_ONBOARD2_32 10
71 #define BRD_ONBOARDRS   11
72 #define BRD_EASYIO      20
73 #define BRD_ECH         21
74 #define BRD_ECHMC       22
75 #define BRD_ECP         23
76 #define BRD_ECPE        24
77 #define BRD_ECPMC       25
78 #define BRD_ECHPCI      26
79 #define BRD_ECH64PCI    27
80 #define BRD_EASYIOPCI   28
81 #define BRD_ECPPCI      29
82
83 #define BRD_BRUMBY      BRD_BRUMBY4
84
85 /*
86  *      Define a configuration structure to hold the board configuration.
87  *      Need to set this up in the code (for now) with the boards that are
88  *      to be configured into the system. This is what needs to be modified
89  *      when adding/removing/modifying boards. Each line entry in the
90  *      stli_brdconf[] array is a board. Each line contains io/irq/memory
91  *      ranges for that board (as well as what type of board it is).
92  *      Some examples:
93  *              { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
94  *      This line will configure an EasyConnection 8/64 at io address 2a0,
95  *      and shared memory address of cc000. Multiple EasyConnection 8/64
96  *      boards can share the same shared memory address space. No interrupt
97  *      is required for this board type.
98  *      Another example:
99  *              { BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
100  *      This line will configure an EasyConnection 8/64 EISA in slot 5 and
101  *      shared memory address of 0x80000000 (2 GByte). Multiple
102  *      EasyConnection 8/64 EISA boards can share the same shared memory
103  *      address space. No interrupt is required for this board type.
104  *      Another example:
105  *              { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
106  *      This line will configure an ONboard (ISA type) at io address 240,
107  *      and shared memory address of d0000. Multiple ONboards can share
108  *      the same shared memory address space. No interrupt required.
109  *      Another example:
110  *              { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
111  *      This line will configure a Brumby board (any number of ports!) at
112  *      io address 360 and shared memory address of c8000. All Brumby boards
113  *      configured into a system must have their own separate io and memory
114  *      addresses. No interrupt is required.
115  *      Another example:
116  *              { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
117  *      This line will configure an original Stallion board at io address 330
118  *      and shared memory address d0000 (this would only be valid for a "V4.0"
119  *      or Rev.O Stallion board). All Stallion boards configured into the
120  *      system must have their own separate io and memory addresses. No
121  *      interrupt is required.
122  */
123
124 typedef struct {
125         int             brdtype;
126         int             ioaddr1;
127         int             ioaddr2;
128         unsigned long   memaddr;
129         int             irq;
130         int             irqtype;
131 } stlconf_t;
132
133 static stlconf_t        stli_brdconf[] = {
134         /*{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },*/
135 };
136
137 static int      stli_nrbrds = sizeof(stli_brdconf) / sizeof(stlconf_t);
138
139 /*
140  *      There is some experimental EISA board detection code in this driver.
141  *      By default it is disabled, but for those that want to try it out,
142  *      then set the define below to be 1.
143  */
144 #define STLI_EISAPROBE  0
145
146 /*****************************************************************************/
147
148 /*
149  *      Define some important driver characteristics. Device major numbers
150  *      allocated as per Linux Device Registry.
151  */
152 #ifndef STL_SIOMEMMAJOR
153 #define STL_SIOMEMMAJOR         28
154 #endif
155 #ifndef STL_SERIALMAJOR
156 #define STL_SERIALMAJOR         24
157 #endif
158 #ifndef STL_CALLOUTMAJOR
159 #define STL_CALLOUTMAJOR        25
160 #endif
161
162 /*****************************************************************************/
163
164 /*
165  *      Define our local driver identity first. Set up stuff to deal with
166  *      all the local structures required by a serial tty driver.
167  */
168 static char     *stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
169 static char     *stli_drvname = "istallion";
170 static char     *stli_drvversion = "5.6.0";
171 static char     *stli_serialname = "ttyE";
172
173 static struct tty_driver        *stli_serial;
174
175 /*
176  *      We will need to allocate a temporary write buffer for chars that
177  *      come direct from user space. The problem is that a copy from user
178  *      space might cause a page fault (typically on a system that is
179  *      swapping!). All ports will share one buffer - since if the system
180  *      is already swapping a shared buffer won't make things any worse.
181  */
182 static char                     *stli_tmpwritebuf;
183 static DECLARE_MUTEX(stli_tmpwritesem);
184
185 #define STLI_TXBUFSIZE          4096
186
187 /*
188  *      Use a fast local buffer for cooked characters. Typically a whole
189  *      bunch of cooked characters come in for a port, 1 at a time. So we
190  *      save those up into a local buffer, then write out the whole lot
191  *      with a large memcpy. Just use 1 buffer for all ports, since its
192  *      use it is only need for short periods of time by each port.
193  */
194 static char                     *stli_txcookbuf;
195 static int                      stli_txcooksize;
196 static int                      stli_txcookrealsize;
197 static struct tty_struct        *stli_txcooktty;
198
199 /*
200  *      Define a local default termios struct. All ports will be created
201  *      with this termios initially. Basically all it defines is a raw port
202  *      at 9600 baud, 8 data bits, no parity, 1 stop bit.
203  */
204 static struct termios           stli_deftermios = {
205         .c_cflag        = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
206         .c_cc           = INIT_C_CC,
207 };
208
209 /*
210  *      Define global stats structures. Not used often, and can be
211  *      re-used for each stats call.
212  */
213 static comstats_t       stli_comstats;
214 static combrd_t         stli_brdstats;
215 static asystats_t       stli_cdkstats;
216 static stlibrd_t        stli_dummybrd;
217 static stliport_t       stli_dummyport;
218
219 /*****************************************************************************/
220
221 static stlibrd_t        *stli_brds[STL_MAXBRDS];
222
223 static int              stli_shared;
224
225 /*
226  *      Per board state flags. Used with the state field of the board struct.
227  *      Not really much here... All we need to do is keep track of whether
228  *      the board has been detected, and whether it is actually running a slave
229  *      or not.
230  */
231 #define BST_FOUND       0x1
232 #define BST_STARTED     0x2
233
234 /*
235  *      Define the set of port state flags. These are marked for internal
236  *      state purposes only, usually to do with the state of communications
237  *      with the slave. Most of them need to be updated atomically, so always
238  *      use the bit setting operations (unless protected by cli/sti).
239  */
240 #define ST_INITIALIZING 1
241 #define ST_OPENING      2
242 #define ST_CLOSING      3
243 #define ST_CMDING       4
244 #define ST_TXBUSY       5
245 #define ST_RXING        6
246 #define ST_DOFLUSHRX    7
247 #define ST_DOFLUSHTX    8
248 #define ST_DOSIGS       9
249 #define ST_RXSTOP       10
250 #define ST_GETSIGS      11
251
252 /*
253  *      Define an array of board names as printable strings. Handy for
254  *      referencing boards when printing trace and stuff.
255  */
256 static char     *stli_brdnames[] = {
257         "Unknown",
258         "Stallion",
259         "Brumby",
260         "ONboard-MC",
261         "ONboard",
262         "Brumby",
263         "Brumby",
264         "ONboard-EI",
265         (char *) NULL,
266         "ONboard",
267         "ONboard-MC",
268         "ONboard-MC",
269         (char *) NULL,
270         (char *) NULL,
271         (char *) NULL,
272         (char *) NULL,
273         (char *) NULL,
274         (char *) NULL,
275         (char *) NULL,
276         (char *) NULL,
277         "EasyIO",
278         "EC8/32-AT",
279         "EC8/32-MC",
280         "EC8/64-AT",
281         "EC8/64-EI",
282         "EC8/64-MC",
283         "EC8/32-PCI",
284         "EC8/64-PCI",
285         "EasyIO-PCI",
286         "EC/RA-PCI",
287 };
288
289 /*****************************************************************************/
290
291 #ifdef MODULE
292 /*
293  *      Define some string labels for arguments passed from the module
294  *      load line. These allow for easy board definitions, and easy
295  *      modification of the io, memory and irq resoucres.
296  */
297
298 static char     *board0[8];
299 static char     *board1[8];
300 static char     *board2[8];
301 static char     *board3[8];
302
303 static char     **stli_brdsp[] = {
304         (char **) &board0,
305         (char **) &board1,
306         (char **) &board2,
307         (char **) &board3
308 };
309
310 /*
311  *      Define a set of common board names, and types. This is used to
312  *      parse any module arguments.
313  */
314
315 typedef struct stlibrdtype {
316         char    *name;
317         int     type;
318 } stlibrdtype_t;
319
320 static stlibrdtype_t    stli_brdstr[] = {
321         { "stallion", BRD_STALLION },
322         { "1", BRD_STALLION },
323         { "brumby", BRD_BRUMBY },
324         { "brumby4", BRD_BRUMBY },
325         { "brumby/4", BRD_BRUMBY },
326         { "brumby-4", BRD_BRUMBY },
327         { "brumby8", BRD_BRUMBY },
328         { "brumby/8", BRD_BRUMBY },
329         { "brumby-8", BRD_BRUMBY },
330         { "brumby16", BRD_BRUMBY },
331         { "brumby/16", BRD_BRUMBY },
332         { "brumby-16", BRD_BRUMBY },
333         { "2", BRD_BRUMBY },
334         { "onboard2", BRD_ONBOARD2 },
335         { "onboard-2", BRD_ONBOARD2 },
336         { "onboard/2", BRD_ONBOARD2 },
337         { "onboard-mc", BRD_ONBOARD2 },
338         { "onboard/mc", BRD_ONBOARD2 },
339         { "onboard-mca", BRD_ONBOARD2 },
340         { "onboard/mca", BRD_ONBOARD2 },
341         { "3", BRD_ONBOARD2 },
342         { "onboard", BRD_ONBOARD },
343         { "onboardat", BRD_ONBOARD },
344         { "4", BRD_ONBOARD },
345         { "onboarde", BRD_ONBOARDE },
346         { "onboard-e", BRD_ONBOARDE },
347         { "onboard/e", BRD_ONBOARDE },
348         { "onboard-ei", BRD_ONBOARDE },
349         { "onboard/ei", BRD_ONBOARDE },
350         { "7", BRD_ONBOARDE },
351         { "ecp", BRD_ECP },
352         { "ecpat", BRD_ECP },
353         { "ec8/64", BRD_ECP },
354         { "ec8/64-at", BRD_ECP },
355         { "ec8/64-isa", BRD_ECP },
356         { "23", BRD_ECP },
357         { "ecpe", BRD_ECPE },
358         { "ecpei", BRD_ECPE },
359         { "ec8/64-e", BRD_ECPE },
360         { "ec8/64-ei", BRD_ECPE },
361         { "24", BRD_ECPE },
362         { "ecpmc", BRD_ECPMC },
363         { "ec8/64-mc", BRD_ECPMC },
364         { "ec8/64-mca", BRD_ECPMC },
365         { "25", BRD_ECPMC },
366         { "ecppci", BRD_ECPPCI },
367         { "ec/ra", BRD_ECPPCI },
368         { "ec/ra-pc", BRD_ECPPCI },
369         { "ec/ra-pci", BRD_ECPPCI },
370         { "29", BRD_ECPPCI },
371 };
372
373 /*
374  *      Define the module agruments.
375  */
376 MODULE_AUTHOR("Greg Ungerer");
377 MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
378 MODULE_LICENSE("GPL");
379
380
381 MODULE_PARM(board0, "1-3s");
382 MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
383 MODULE_PARM(board1, "1-3s");
384 MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
385 MODULE_PARM(board2, "1-3s");
386 MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
387 MODULE_PARM(board3, "1-3s");
388 MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
389
390 #endif
391
392 /*
393  *      Set up a default memory address table for EISA board probing.
394  *      The default addresses are all bellow 1Mbyte, which has to be the
395  *      case anyway. They should be safe, since we only read values from
396  *      them, and interrupts are disabled while we do it. If the higher
397  *      memory support is compiled in then we also try probing around
398  *      the 1Gb, 2Gb and 3Gb areas as well...
399  */
400 static unsigned long    stli_eisamemprobeaddrs[] = {
401         0xc0000,    0xd0000,    0xe0000,    0xf0000,
402         0x80000000, 0x80010000, 0x80020000, 0x80030000,
403         0x40000000, 0x40010000, 0x40020000, 0x40030000,
404         0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
405         0xff000000, 0xff010000, 0xff020000, 0xff030000,
406 };
407
408 static int      stli_eisamempsize = sizeof(stli_eisamemprobeaddrs) / sizeof(unsigned long);
409 int             stli_eisaprobe = STLI_EISAPROBE;
410
411 /*
412  *      Define the Stallion PCI vendor and device IDs.
413  */
414 #ifdef CONFIG_PCI
415 #ifndef PCI_VENDOR_ID_STALLION
416 #define PCI_VENDOR_ID_STALLION          0x124d
417 #endif
418 #ifndef PCI_DEVICE_ID_ECRA
419 #define PCI_DEVICE_ID_ECRA              0x0004
420 #endif
421
422 static struct pci_device_id istallion_pci_tbl[] = {
423         { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
424         { 0 }
425 };
426 MODULE_DEVICE_TABLE(pci, istallion_pci_tbl);
427
428 #endif /* CONFIG_PCI */
429
430 /*****************************************************************************/
431
432 /*
433  *      Hardware configuration info for ECP boards. These defines apply
434  *      to the directly accessible io ports of the ECP. There is a set of
435  *      defines for each ECP board type, ISA, EISA, MCA and PCI.
436  */
437 #define ECP_IOSIZE      4
438
439 #define ECP_MEMSIZE     (128 * 1024)
440 #define ECP_PCIMEMSIZE  (256 * 1024)
441
442 #define ECP_ATPAGESIZE  (4 * 1024)
443 #define ECP_MCPAGESIZE  (4 * 1024)
444 #define ECP_EIPAGESIZE  (64 * 1024)
445 #define ECP_PCIPAGESIZE (64 * 1024)
446
447 #define STL_EISAID      0x8c4e
448
449 /*
450  *      Important defines for the ISA class of ECP board.
451  */
452 #define ECP_ATIREG      0
453 #define ECP_ATCONFR     1
454 #define ECP_ATMEMAR     2
455 #define ECP_ATMEMPR     3
456 #define ECP_ATSTOP      0x1
457 #define ECP_ATINTENAB   0x10
458 #define ECP_ATENABLE    0x20
459 #define ECP_ATDISABLE   0x00
460 #define ECP_ATADDRMASK  0x3f000
461 #define ECP_ATADDRSHFT  12
462
463 /*
464  *      Important defines for the EISA class of ECP board.
465  */
466 #define ECP_EIIREG      0
467 #define ECP_EIMEMARL    1
468 #define ECP_EICONFR     2
469 #define ECP_EIMEMARH    3
470 #define ECP_EIENABLE    0x1
471 #define ECP_EIDISABLE   0x0
472 #define ECP_EISTOP      0x4
473 #define ECP_EIEDGE      0x00
474 #define ECP_EILEVEL     0x80
475 #define ECP_EIADDRMASKL 0x00ff0000
476 #define ECP_EIADDRSHFTL 16
477 #define ECP_EIADDRMASKH 0xff000000
478 #define ECP_EIADDRSHFTH 24
479 #define ECP_EIBRDENAB   0xc84
480
481 #define ECP_EISAID      0x4
482
483 /*
484  *      Important defines for the Micro-channel class of ECP board.
485  *      (It has a lot in common with the ISA boards.)
486  */
487 #define ECP_MCIREG      0
488 #define ECP_MCCONFR     1
489 #define ECP_MCSTOP      0x20
490 #define ECP_MCENABLE    0x80
491 #define ECP_MCDISABLE   0x00
492
493 /*
494  *      Important defines for the PCI class of ECP board.
495  *      (It has a lot in common with the other ECP boards.)
496  */
497 #define ECP_PCIIREG     0
498 #define ECP_PCICONFR    1
499 #define ECP_PCISTOP     0x01
500
501 /*
502  *      Hardware configuration info for ONboard and Brumby boards. These
503  *      defines apply to the directly accessible io ports of these boards.
504  */
505 #define ONB_IOSIZE      16
506 #define ONB_MEMSIZE     (64 * 1024)
507 #define ONB_ATPAGESIZE  (64 * 1024)
508 #define ONB_MCPAGESIZE  (64 * 1024)
509 #define ONB_EIMEMSIZE   (128 * 1024)
510 #define ONB_EIPAGESIZE  (64 * 1024)
511
512 /*
513  *      Important defines for the ISA class of ONboard board.
514  */
515 #define ONB_ATIREG      0
516 #define ONB_ATMEMAR     1
517 #define ONB_ATCONFR     2
518 #define ONB_ATSTOP      0x4
519 #define ONB_ATENABLE    0x01
520 #define ONB_ATDISABLE   0x00
521 #define ONB_ATADDRMASK  0xff0000
522 #define ONB_ATADDRSHFT  16
523
524 #define ONB_MEMENABLO   0
525 #define ONB_MEMENABHI   0x02
526
527 /*
528  *      Important defines for the EISA class of ONboard board.
529  */
530 #define ONB_EIIREG      0
531 #define ONB_EIMEMARL    1
532 #define ONB_EICONFR     2
533 #define ONB_EIMEMARH    3
534 #define ONB_EIENABLE    0x1
535 #define ONB_EIDISABLE   0x0
536 #define ONB_EISTOP      0x4
537 #define ONB_EIEDGE      0x00
538 #define ONB_EILEVEL     0x80
539 #define ONB_EIADDRMASKL 0x00ff0000
540 #define ONB_EIADDRSHFTL 16
541 #define ONB_EIADDRMASKH 0xff000000
542 #define ONB_EIADDRSHFTH 24
543 #define ONB_EIBRDENAB   0xc84
544
545 #define ONB_EISAID      0x1
546
547 /*
548  *      Important defines for the Brumby boards. They are pretty simple,
549  *      there is not much that is programmably configurable.
550  */
551 #define BBY_IOSIZE      16
552 #define BBY_MEMSIZE     (64 * 1024)
553 #define BBY_PAGESIZE    (16 * 1024)
554
555 #define BBY_ATIREG      0
556 #define BBY_ATCONFR     1
557 #define BBY_ATSTOP      0x4
558
559 /*
560  *      Important defines for the Stallion boards. They are pretty simple,
561  *      there is not much that is programmably configurable.
562  */
563 #define STAL_IOSIZE     16
564 #define STAL_MEMSIZE    (64 * 1024)
565 #define STAL_PAGESIZE   (64 * 1024)
566
567 /*
568  *      Define the set of status register values for EasyConnection panels.
569  *      The signature will return with the status value for each panel. From
570  *      this we can determine what is attached to the board - before we have
571  *      actually down loaded any code to it.
572  */
573 #define ECH_PNLSTATUS   2
574 #define ECH_PNL16PORT   0x20
575 #define ECH_PNLIDMASK   0x07
576 #define ECH_PNLXPID     0x40
577 #define ECH_PNLINTRPEND 0x80
578
579 /*
580  *      Define some macros to do things to the board. Even those these boards
581  *      are somewhat related there is often significantly different ways of
582  *      doing some operation on it (like enable, paging, reset, etc). So each
583  *      board class has a set of functions which do the commonly required
584  *      operations. The macros below basically just call these functions,
585  *      generally checking for a NULL function - which means that the board
586  *      needs nothing done to it to achieve this operation!
587  */
588 #define EBRDINIT(brdp)                                          \
589         if (brdp->init != NULL)                                 \
590                 (* brdp->init)(brdp)
591
592 #define EBRDENABLE(brdp)                                        \
593         if (brdp->enable != NULL)                               \
594                 (* brdp->enable)(brdp);
595
596 #define EBRDDISABLE(brdp)                                       \
597         if (brdp->disable != NULL)                              \
598                 (* brdp->disable)(brdp);
599
600 #define EBRDINTR(brdp)                                          \
601         if (brdp->intr != NULL)                                 \
602                 (* brdp->intr)(brdp);
603
604 #define EBRDRESET(brdp)                                         \
605         if (brdp->reset != NULL)                                \
606                 (* brdp->reset)(brdp);
607
608 #define EBRDGETMEMPTR(brdp,offset)                              \
609         (* brdp->getmemptr)(brdp, offset, __LINE__)
610
611 /*
612  *      Define the maximal baud rate, and the default baud base for ports.
613  */
614 #define STL_MAXBAUD     460800
615 #define STL_BAUDBASE    115200
616 #define STL_CLOSEDELAY  (5 * HZ / 10)
617
618 /*****************************************************************************/
619
620 /*
621  *      Define macros to extract a brd or port number from a minor number.
622  */
623 #define MINOR2BRD(min)          (((min) & 0xc0) >> 6)
624 #define MINOR2PORT(min)         ((min) & 0x3f)
625
626 /*
627  *      Define a baud rate table that converts termios baud rate selector
628  *      into the actual baud rate value. All baud rate calculations are based
629  *      on the actual baud rate required.
630  */
631 static unsigned int     stli_baudrates[] = {
632         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
633         9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
634 };
635
636 /*****************************************************************************/
637
638 /*
639  *      Define some handy local macros...
640  */
641 #undef MIN
642 #define MIN(a,b)        (((a) <= (b)) ? (a) : (b))
643
644 #undef  TOLOWER
645 #define TOLOWER(x)      ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
646
647 /*****************************************************************************/
648
649 /*
650  *      Prototype all functions in this driver!
651  */
652
653 #ifdef MODULE
654 static void     stli_argbrds(void);
655 static int      stli_parsebrd(stlconf_t *confp, char **argp);
656
657 static unsigned long    stli_atol(char *str);
658 #endif
659
660 int             stli_init(void);
661 static int      stli_open(struct tty_struct *tty, struct file *filp);
662 static void     stli_close(struct tty_struct *tty, struct file *filp);
663 static int      stli_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
664 static void     stli_putchar(struct tty_struct *tty, unsigned char ch);
665 static void     stli_flushchars(struct tty_struct *tty);
666 static int      stli_writeroom(struct tty_struct *tty);
667 static int      stli_charsinbuffer(struct tty_struct *tty);
668 static int      stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
669 static void     stli_settermios(struct tty_struct *tty, struct termios *old);
670 static void     stli_throttle(struct tty_struct *tty);
671 static void     stli_unthrottle(struct tty_struct *tty);
672 static void     stli_stop(struct tty_struct *tty);
673 static void     stli_start(struct tty_struct *tty);
674 static void     stli_flushbuffer(struct tty_struct *tty);
675 static void     stli_breakctl(struct tty_struct *tty, int state);
676 static void     stli_waituntilsent(struct tty_struct *tty, int timeout);
677 static void     stli_sendxchar(struct tty_struct *tty, char ch);
678 static void     stli_hangup(struct tty_struct *tty);
679 static int      stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos);
680
681 static int      stli_brdinit(stlibrd_t *brdp);
682 static int      stli_startbrd(stlibrd_t *brdp);
683 static ssize_t  stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp);
684 static ssize_t  stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp);
685 static int      stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
686 static void     stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp);
687 static void     stli_poll(unsigned long arg);
688 static int      stli_hostcmd(stlibrd_t *brdp, stliport_t *portp);
689 static int      stli_initopen(stlibrd_t *brdp, stliport_t *portp);
690 static int      stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
691 static int      stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
692 static int      stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp);
693 static void     stli_dohangup(void *arg);
694 static void     stli_delay(int len);
695 static int      stli_setport(stliport_t *portp);
696 static int      stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
697 static void     stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
698 static void     stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp);
699 static void     stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp);
700 static void     stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
701 static long     stli_mktiocm(unsigned long sigvalue);
702 static void     stli_read(stlibrd_t *brdp, stliport_t *portp);
703 static int      stli_getserial(stliport_t *portp, struct serial_struct __user *sp);
704 static int      stli_setserial(stliport_t *portp, struct serial_struct __user *sp);
705 static int      stli_getbrdstats(combrd_t __user *bp);
706 static int      stli_getportstats(stliport_t *portp, comstats_t __user *cp);
707 static int      stli_portcmdstats(stliport_t *portp);
708 static int      stli_clrportstats(stliport_t *portp, comstats_t __user *cp);
709 static int      stli_getportstruct(stliport_t __user *arg);
710 static int      stli_getbrdstruct(stlibrd_t __user *arg);
711 static void     *stli_memalloc(int len);
712 static stlibrd_t *stli_allocbrd(void);
713
714 static void     stli_ecpinit(stlibrd_t *brdp);
715 static void     stli_ecpenable(stlibrd_t *brdp);
716 static void     stli_ecpdisable(stlibrd_t *brdp);
717 static char     *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
718 static void     stli_ecpreset(stlibrd_t *brdp);
719 static void     stli_ecpintr(stlibrd_t *brdp);
720 static void     stli_ecpeiinit(stlibrd_t *brdp);
721 static void     stli_ecpeienable(stlibrd_t *brdp);
722 static void     stli_ecpeidisable(stlibrd_t *brdp);
723 static char     *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
724 static void     stli_ecpeireset(stlibrd_t *brdp);
725 static void     stli_ecpmcenable(stlibrd_t *brdp);
726 static void     stli_ecpmcdisable(stlibrd_t *brdp);
727 static char     *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
728 static void     stli_ecpmcreset(stlibrd_t *brdp);
729 static void     stli_ecppciinit(stlibrd_t *brdp);
730 static char     *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
731 static void     stli_ecppcireset(stlibrd_t *brdp);
732
733 static void     stli_onbinit(stlibrd_t *brdp);
734 static void     stli_onbenable(stlibrd_t *brdp);
735 static void     stli_onbdisable(stlibrd_t *brdp);
736 static char     *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
737 static void     stli_onbreset(stlibrd_t *brdp);
738 static void     stli_onbeinit(stlibrd_t *brdp);
739 static void     stli_onbeenable(stlibrd_t *brdp);
740 static void     stli_onbedisable(stlibrd_t *brdp);
741 static char     *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
742 static void     stli_onbereset(stlibrd_t *brdp);
743 static void     stli_bbyinit(stlibrd_t *brdp);
744 static char     *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
745 static void     stli_bbyreset(stlibrd_t *brdp);
746 static void     stli_stalinit(stlibrd_t *brdp);
747 static char     *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
748 static void     stli_stalreset(stlibrd_t *brdp);
749
750 static stliport_t *stli_getport(int brdnr, int panelnr, int portnr);
751
752 static int      stli_initecp(stlibrd_t *brdp);
753 static int      stli_initonb(stlibrd_t *brdp);
754 static int      stli_eisamemprobe(stlibrd_t *brdp);
755 static int      stli_initports(stlibrd_t *brdp);
756
757 #ifdef  CONFIG_PCI
758 static int      stli_initpcibrd(int brdtype, struct pci_dev *devp);
759 #endif
760
761 /*****************************************************************************/
762
763 /*
764  *      Define the driver info for a user level shared memory device. This
765  *      device will work sort of like the /dev/kmem device - except that it
766  *      will give access to the shared memory on the Stallion intelligent
767  *      board. This is also a very useful debugging tool.
768  */
769 static struct file_operations   stli_fsiomem = {
770         .owner          = THIS_MODULE,
771         .read           = stli_memread,
772         .write          = stli_memwrite,
773         .ioctl          = stli_memioctl,
774 };
775
776 /*****************************************************************************/
777
778 /*
779  *      Define a timer_list entry for our poll routine. The slave board
780  *      is polled every so often to see if anything needs doing. This is
781  *      much cheaper on host cpu than using interrupts. It turns out to
782  *      not increase character latency by much either...
783  */
784 static struct timer_list stli_timerlist = TIMER_INITIALIZER(stli_poll, 0, 0);
785
786 static int      stli_timeron;
787
788 /*
789  *      Define the calculation for the timeout routine.
790  */
791 #define STLI_TIMEOUT    (jiffies + 1)
792
793 /*****************************************************************************/
794
795 static struct class_simple *istallion_class;
796
797 #ifdef MODULE
798
799 /*
800  *      Loadable module initialization stuff.
801  */
802
803 static int __init istallion_module_init(void)
804 {
805         unsigned long   flags;
806
807 #ifdef DEBUG
808         printk("init_module()\n");
809 #endif
810
811         save_flags(flags);
812         cli();
813         stli_init();
814         restore_flags(flags);
815
816         return(0);
817 }
818
819 /*****************************************************************************/
820
821 static void __exit istallion_module_exit(void)
822 {
823         stlibrd_t       *brdp;
824         stliport_t      *portp;
825         unsigned long   flags;
826         int             i, j;
827
828 #ifdef DEBUG
829         printk("cleanup_module()\n");
830 #endif
831
832         printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
833                 stli_drvversion);
834
835         save_flags(flags);
836         cli();
837
838 /*
839  *      Free up all allocated resources used by the ports. This includes
840  *      memory and interrupts.
841  */
842         if (stli_timeron) {
843                 stli_timeron = 0;
844                 del_timer(&stli_timerlist);
845         }
846
847         i = tty_unregister_driver(stli_serial);
848         if (i) {
849                 printk("STALLION: failed to un-register tty driver, "
850                         "errno=%d\n", -i);
851                 restore_flags(flags);
852                 return;
853         }
854         put_tty_driver(stli_serial);
855         for (i = 0; i < 4; i++) {
856                 devfs_remove("staliomem/%d", i);
857                 class_simple_device_remove(MKDEV(STL_SIOMEMMAJOR, i));
858         }
859         devfs_remove("staliomem");
860         class_simple_destroy(istallion_class);
861         if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
862                 printk("STALLION: failed to un-register serial memory device, "
863                         "errno=%d\n", -i);
864         if (stli_tmpwritebuf != (char *) NULL)
865                 kfree(stli_tmpwritebuf);
866         if (stli_txcookbuf != (char *) NULL)
867                 kfree(stli_txcookbuf);
868
869         for (i = 0; (i < stli_nrbrds); i++) {
870                 if ((brdp = stli_brds[i]) == (stlibrd_t *) NULL)
871                         continue;
872                 for (j = 0; (j < STL_MAXPORTS); j++) {
873                         portp = brdp->ports[j];
874                         if (portp != (stliport_t *) NULL) {
875                                 if (portp->tty != (struct tty_struct *) NULL)
876                                         tty_hangup(portp->tty);
877                                 kfree(portp);
878                         }
879                 }
880
881                 iounmap(brdp->membase);
882                 if (brdp->iosize > 0)
883                         release_region(brdp->iobase, brdp->iosize);
884                 kfree(brdp);
885                 stli_brds[i] = (stlibrd_t *) NULL;
886         }
887
888         restore_flags(flags);
889 }
890
891 module_init(istallion_module_init);
892 module_exit(istallion_module_exit);
893
894 /*****************************************************************************/
895
896 /*
897  *      Check for any arguments passed in on the module load command line.
898  */
899
900 static void stli_argbrds(void)
901 {
902         stlconf_t       conf;
903         stlibrd_t       *brdp;
904         int             nrargs, i;
905
906 #ifdef DEBUG
907         printk("stli_argbrds()\n");
908 #endif
909
910         nrargs = sizeof(stli_brdsp) / sizeof(char **);
911
912         for (i = stli_nrbrds; (i < nrargs); i++) {
913                 memset(&conf, 0, sizeof(conf));
914                 if (stli_parsebrd(&conf, stli_brdsp[i]) == 0)
915                         continue;
916                 if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
917                         continue;
918                 stli_nrbrds = i + 1;
919                 brdp->brdnr = i;
920                 brdp->brdtype = conf.brdtype;
921                 brdp->iobase = conf.ioaddr1;
922                 brdp->memaddr = conf.memaddr;
923                 stli_brdinit(brdp);
924         }
925 }
926
927 /*****************************************************************************/
928
929 /*
930  *      Convert an ascii string number into an unsigned long.
931  */
932
933 static unsigned long stli_atol(char *str)
934 {
935         unsigned long   val;
936         int             base, c;
937         char            *sp;
938
939         val = 0;
940         sp = str;
941         if ((*sp == '0') && (*(sp+1) == 'x')) {
942                 base = 16;
943                 sp += 2;
944         } else if (*sp == '0') {
945                 base = 8;
946                 sp++;
947         } else {
948                 base = 10;
949         }
950
951         for (; (*sp != 0); sp++) {
952                 c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
953                 if ((c < 0) || (c >= base)) {
954                         printk("STALLION: invalid argument %s\n", str);
955                         val = 0;
956                         break;
957                 }
958                 val = (val * base) + c;
959         }
960         return(val);
961 }
962
963 /*****************************************************************************/
964
965 /*
966  *      Parse the supplied argument string, into the board conf struct.
967  */
968
969 static int stli_parsebrd(stlconf_t *confp, char **argp)
970 {
971         char    *sp;
972         int     nrbrdnames, i;
973
974 #ifdef DEBUG
975         printk("stli_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
976 #endif
977
978         if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
979                 return(0);
980
981         for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
982                 *sp = TOLOWER(*sp);
983
984         nrbrdnames = sizeof(stli_brdstr) / sizeof(stlibrdtype_t);
985         for (i = 0; (i < nrbrdnames); i++) {
986                 if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
987                         break;
988         }
989         if (i >= nrbrdnames) {
990                 printk("STALLION: unknown board name, %s?\n", argp[0]);
991                 return(0);
992         }
993
994         confp->brdtype = stli_brdstr[i].type;
995         if ((argp[1] != (char *) NULL) && (*argp[1] != 0))
996                 confp->ioaddr1 = stli_atol(argp[1]);
997         if ((argp[2] != (char *) NULL) && (*argp[2] != 0))
998                 confp->memaddr = stli_atol(argp[2]);
999         return(1);
1000 }
1001
1002 #endif
1003
1004 /*****************************************************************************/
1005
1006 /*
1007  *      Local driver kernel malloc routine.
1008  */
1009
1010 static void *stli_memalloc(int len)
1011 {
1012         return((void *) kmalloc(len, GFP_KERNEL));
1013 }
1014
1015 /*****************************************************************************/
1016
1017 static int stli_open(struct tty_struct *tty, struct file *filp)
1018 {
1019         stlibrd_t       *brdp;
1020         stliport_t      *portp;
1021         unsigned int    minordev;
1022         int             brdnr, portnr, rc;
1023
1024 #ifdef DEBUG
1025         printk("stli_open(tty=%x,filp=%x): device=%s\n", (int) tty,
1026                 (int) filp, tty->name);
1027 #endif
1028
1029         minordev = tty->index;
1030         brdnr = MINOR2BRD(minordev);
1031         if (brdnr >= stli_nrbrds)
1032                 return(-ENODEV);
1033         brdp = stli_brds[brdnr];
1034         if (brdp == (stlibrd_t *) NULL)
1035                 return(-ENODEV);
1036         if ((brdp->state & BST_STARTED) == 0)
1037                 return(-ENODEV);
1038         portnr = MINOR2PORT(minordev);
1039         if ((portnr < 0) || (portnr > brdp->nrports))
1040                 return(-ENODEV);
1041
1042         portp = brdp->ports[portnr];
1043         if (portp == (stliport_t *) NULL)
1044                 return(-ENODEV);
1045         if (portp->devnr < 1)
1046                 return(-ENODEV);
1047
1048
1049 /*
1050  *      Check if this port is in the middle of closing. If so then wait
1051  *      until it is closed then return error status based on flag settings.
1052  *      The sleep here does not need interrupt protection since the wakeup
1053  *      for it is done with the same context.
1054  */
1055         if (portp->flags & ASYNC_CLOSING) {
1056                 interruptible_sleep_on(&portp->close_wait);
1057                 if (portp->flags & ASYNC_HUP_NOTIFY)
1058                         return(-EAGAIN);
1059                 return(-ERESTARTSYS);
1060         }
1061
1062 /*
1063  *      On the first open of the device setup the port hardware, and
1064  *      initialize the per port data structure. Since initializing the port
1065  *      requires several commands to the board we will need to wait for any
1066  *      other open that is already initializing the port.
1067  */
1068         portp->tty = tty;
1069         tty->driver_data = portp;
1070         portp->refcount++;
1071
1072         while (test_bit(ST_INITIALIZING, &portp->state)) {
1073                 if (signal_pending(current))
1074                         return(-ERESTARTSYS);
1075                 interruptible_sleep_on(&portp->raw_wait);
1076         }
1077
1078         if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1079                 set_bit(ST_INITIALIZING, &portp->state);
1080                 if ((rc = stli_initopen(brdp, portp)) >= 0) {
1081                         portp->flags |= ASYNC_INITIALIZED;
1082                         clear_bit(TTY_IO_ERROR, &tty->flags);
1083                 }
1084                 clear_bit(ST_INITIALIZING, &portp->state);
1085                 wake_up_interruptible(&portp->raw_wait);
1086                 if (rc < 0)
1087                         return(rc);
1088         }
1089
1090 /*
1091  *      Check if this port is in the middle of closing. If so then wait
1092  *      until it is closed then return error status, based on flag settings.
1093  *      The sleep here does not need interrupt protection since the wakeup
1094  *      for it is done with the same context.
1095  */
1096         if (portp->flags & ASYNC_CLOSING) {
1097                 interruptible_sleep_on(&portp->close_wait);
1098                 if (portp->flags & ASYNC_HUP_NOTIFY)
1099                         return(-EAGAIN);
1100                 return(-ERESTARTSYS);
1101         }
1102
1103 /*
1104  *      Based on type of open being done check if it can overlap with any
1105  *      previous opens still in effect. If we are a normal serial device
1106  *      then also we might have to wait for carrier.
1107  */
1108         if (!(filp->f_flags & O_NONBLOCK)) {
1109                 if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
1110                         return(rc);
1111         }
1112         portp->flags |= ASYNC_NORMAL_ACTIVE;
1113         return(0);
1114 }
1115
1116 /*****************************************************************************/
1117
1118 static void stli_close(struct tty_struct *tty, struct file *filp)
1119 {
1120         stlibrd_t       *brdp;
1121         stliport_t      *portp;
1122         unsigned long   flags;
1123
1124 #ifdef DEBUG
1125         printk("stli_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
1126 #endif
1127
1128         portp = tty->driver_data;
1129         if (portp == (stliport_t *) NULL)
1130                 return;
1131
1132         save_flags(flags);
1133         cli();
1134         if (tty_hung_up_p(filp)) {
1135                 restore_flags(flags);
1136                 return;
1137         }
1138         if ((tty->count == 1) && (portp->refcount != 1))
1139                 portp->refcount = 1;
1140         if (portp->refcount-- > 1) {
1141                 restore_flags(flags);
1142                 return;
1143         }
1144
1145         portp->flags |= ASYNC_CLOSING;
1146
1147 /*
1148  *      May want to wait for data to drain before closing. The BUSY flag
1149  *      keeps track of whether we are still transmitting or not. It is
1150  *      updated by messages from the slave - indicating when all chars
1151  *      really have drained.
1152  */
1153         if (tty == stli_txcooktty)
1154                 stli_flushchars(tty);
1155         tty->closing = 1;
1156         if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1157                 tty_wait_until_sent(tty, portp->closing_wait);
1158
1159         portp->flags &= ~ASYNC_INITIALIZED;
1160         brdp = stli_brds[portp->brdnr];
1161         stli_rawclose(brdp, portp, 0, 0);
1162         if (tty->termios->c_cflag & HUPCL) {
1163                 stli_mkasysigs(&portp->asig, 0, 0);
1164                 if (test_bit(ST_CMDING, &portp->state))
1165                         set_bit(ST_DOSIGS, &portp->state);
1166                 else
1167                         stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
1168                                 sizeof(asysigs_t), 0);
1169         }
1170         clear_bit(ST_TXBUSY, &portp->state);
1171         clear_bit(ST_RXSTOP, &portp->state);
1172         set_bit(TTY_IO_ERROR, &tty->flags);
1173         if (tty->ldisc.flush_buffer)
1174                 (tty->ldisc.flush_buffer)(tty);
1175         set_bit(ST_DOFLUSHRX, &portp->state);
1176         stli_flushbuffer(tty);
1177
1178         tty->closing = 0;
1179         portp->tty = (struct tty_struct *) NULL;
1180
1181         if (portp->openwaitcnt) {
1182                 if (portp->close_delay)
1183                         stli_delay(portp->close_delay);
1184                 wake_up_interruptible(&portp->open_wait);
1185         }
1186
1187         portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1188         wake_up_interruptible(&portp->close_wait);
1189         restore_flags(flags);
1190 }
1191
1192 /*****************************************************************************/
1193
1194 /*
1195  *      Carry out first open operations on a port. This involves a number of
1196  *      commands to be sent to the slave. We need to open the port, set the
1197  *      notification events, set the initial port settings, get and set the
1198  *      initial signal values. We sleep and wait in between each one. But
1199  *      this still all happens pretty quickly.
1200  */
1201
1202 static int stli_initopen(stlibrd_t *brdp, stliport_t *portp)
1203 {
1204         struct tty_struct       *tty;
1205         asynotify_t             nt;
1206         asyport_t               aport;
1207         int                     rc;
1208
1209 #ifdef DEBUG
1210         printk("stli_initopen(brdp=%x,portp=%x)\n", (int) brdp, (int) portp);
1211 #endif
1212
1213         if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
1214                 return(rc);
1215
1216         memset(&nt, 0, sizeof(asynotify_t));
1217         nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
1218         nt.signal = SG_DCD;
1219         if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
1220             sizeof(asynotify_t), 0)) < 0)
1221                 return(rc);
1222
1223         tty = portp->tty;
1224         if (tty == (struct tty_struct *) NULL)
1225                 return(-ENODEV);
1226         stli_mkasyport(portp, &aport, tty->termios);
1227         if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
1228             sizeof(asyport_t), 0)) < 0)
1229                 return(rc);
1230
1231         set_bit(ST_GETSIGS, &portp->state);
1232         if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
1233             sizeof(asysigs_t), 1)) < 0)
1234                 return(rc);
1235         if (test_and_clear_bit(ST_GETSIGS, &portp->state))
1236                 portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1237         stli_mkasysigs(&portp->asig, 1, 1);
1238         if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1239             sizeof(asysigs_t), 0)) < 0)
1240                 return(rc);
1241
1242         return(0);
1243 }
1244
1245 /*****************************************************************************/
1246
1247 /*
1248  *      Send an open message to the slave. This will sleep waiting for the
1249  *      acknowledgement, so must have user context. We need to co-ordinate
1250  *      with close events here, since we don't want open and close events
1251  *      to overlap.
1252  */
1253
1254 static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1255 {
1256         volatile cdkhdr_t       *hdrp;
1257         volatile cdkctrl_t      *cp;
1258         volatile unsigned char  *bits;
1259         unsigned long           flags;
1260         int                     rc;
1261
1262 #ifdef DEBUG
1263         printk("stli_rawopen(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
1264                 (int) brdp, (int) portp, (int) arg, wait);
1265 #endif
1266
1267 /*
1268  *      Send a message to the slave to open this port.
1269  */
1270         save_flags(flags);
1271         cli();
1272
1273 /*
1274  *      Slave is already closing this port. This can happen if a hangup
1275  *      occurs on this port. So we must wait until it is complete. The
1276  *      order of opens and closes may not be preserved across shared
1277  *      memory, so we must wait until it is complete.
1278  */
1279         while (test_bit(ST_CLOSING, &portp->state)) {
1280                 if (signal_pending(current)) {
1281                         restore_flags(flags);
1282                         return(-ERESTARTSYS);
1283                 }
1284                 interruptible_sleep_on(&portp->raw_wait);
1285         }
1286
1287 /*
1288  *      Everything is ready now, so write the open message into shared
1289  *      memory. Once the message is in set the service bits to say that
1290  *      this port wants service.
1291  */
1292         EBRDENABLE(brdp);
1293         cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1294         cp->openarg = arg;
1295         cp->open = 1;
1296         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1297         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1298                 portp->portidx;
1299         *bits |= portp->portbit;
1300         EBRDDISABLE(brdp);
1301
1302         if (wait == 0) {
1303                 restore_flags(flags);
1304                 return(0);
1305         }
1306
1307 /*
1308  *      Slave is in action, so now we must wait for the open acknowledgment
1309  *      to come back.
1310  */
1311         rc = 0;
1312         set_bit(ST_OPENING, &portp->state);
1313         while (test_bit(ST_OPENING, &portp->state)) {
1314                 if (signal_pending(current)) {
1315                         rc = -ERESTARTSYS;
1316                         break;
1317                 }
1318                 interruptible_sleep_on(&portp->raw_wait);
1319         }
1320         restore_flags(flags);
1321
1322         if ((rc == 0) && (portp->rc != 0))
1323                 rc = -EIO;
1324         return(rc);
1325 }
1326
1327 /*****************************************************************************/
1328
1329 /*
1330  *      Send a close message to the slave. Normally this will sleep waiting
1331  *      for the acknowledgement, but if wait parameter is 0 it will not. If
1332  *      wait is true then must have user context (to sleep).
1333  */
1334
1335 static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1336 {
1337         volatile cdkhdr_t       *hdrp;
1338         volatile cdkctrl_t      *cp;
1339         volatile unsigned char  *bits;
1340         unsigned long           flags;
1341         int                     rc;
1342
1343 #ifdef DEBUG
1344         printk("stli_rawclose(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
1345                 (int) brdp, (int) portp, (int) arg, wait);
1346 #endif
1347
1348         save_flags(flags);
1349         cli();
1350
1351 /*
1352  *      Slave is already closing this port. This can happen if a hangup
1353  *      occurs on this port.
1354  */
1355         if (wait) {
1356                 while (test_bit(ST_CLOSING, &portp->state)) {
1357                         if (signal_pending(current)) {
1358                                 restore_flags(flags);
1359                                 return(-ERESTARTSYS);
1360                         }
1361                         interruptible_sleep_on(&portp->raw_wait);
1362                 }
1363         }
1364
1365 /*
1366  *      Write the close command into shared memory.
1367  */
1368         EBRDENABLE(brdp);
1369         cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1370         cp->closearg = arg;
1371         cp->close = 1;
1372         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1373         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1374                 portp->portidx;
1375         *bits |= portp->portbit;
1376         EBRDDISABLE(brdp);
1377
1378         set_bit(ST_CLOSING, &portp->state);
1379         if (wait == 0) {
1380                 restore_flags(flags);
1381                 return(0);
1382         }
1383
1384 /*
1385  *      Slave is in action, so now we must wait for the open acknowledgment
1386  *      to come back.
1387  */
1388         rc = 0;
1389         while (test_bit(ST_CLOSING, &portp->state)) {
1390                 if (signal_pending(current)) {
1391                         rc = -ERESTARTSYS;
1392                         break;
1393                 }
1394                 interruptible_sleep_on(&portp->raw_wait);
1395         }
1396         restore_flags(flags);
1397
1398         if ((rc == 0) && (portp->rc != 0))
1399                 rc = -EIO;
1400         return(rc);
1401 }
1402
1403 /*****************************************************************************/
1404
1405 /*
1406  *      Send a command to the slave and wait for the response. This must
1407  *      have user context (it sleeps). This routine is generic in that it
1408  *      can send any type of command. Its purpose is to wait for that command
1409  *      to complete (as opposed to initiating the command then returning).
1410  */
1411
1412 static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
1413 {
1414         unsigned long   flags;
1415
1416 #ifdef DEBUG
1417         printk("stli_cmdwait(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
1418                 "copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
1419                 (int) arg, size, copyback);
1420 #endif
1421
1422         save_flags(flags);
1423         cli();
1424         while (test_bit(ST_CMDING, &portp->state)) {
1425                 if (signal_pending(current)) {
1426                         restore_flags(flags);
1427                         return(-ERESTARTSYS);
1428                 }
1429                 interruptible_sleep_on(&portp->raw_wait);
1430         }
1431
1432         stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1433
1434         while (test_bit(ST_CMDING, &portp->state)) {
1435                 if (signal_pending(current)) {
1436                         restore_flags(flags);
1437                         return(-ERESTARTSYS);
1438                 }
1439                 interruptible_sleep_on(&portp->raw_wait);
1440         }
1441         restore_flags(flags);
1442
1443         if (portp->rc != 0)
1444                 return(-EIO);
1445         return(0);
1446 }
1447
1448 /*****************************************************************************/
1449
1450 /*
1451  *      Send the termios settings for this port to the slave. This sleeps
1452  *      waiting for the command to complete - so must have user context.
1453  */
1454
1455 static int stli_setport(stliport_t *portp)
1456 {
1457         stlibrd_t       *brdp;
1458         asyport_t       aport;
1459
1460 #ifdef DEBUG
1461         printk("stli_setport(portp=%x)\n", (int) portp);
1462 #endif
1463
1464         if (portp == (stliport_t *) NULL)
1465                 return(-ENODEV);
1466         if (portp->tty == (struct tty_struct *) NULL)
1467                 return(-ENODEV);
1468         if ((portp->brdnr < 0) && (portp->brdnr >= stli_nrbrds))
1469                 return(-ENODEV);
1470         brdp = stli_brds[portp->brdnr];
1471         if (brdp == (stlibrd_t *) NULL)
1472                 return(-ENODEV);
1473
1474         stli_mkasyport(portp, &aport, portp->tty->termios);
1475         return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
1476 }
1477
1478 /*****************************************************************************/
1479
1480 /*
1481  *      Wait for a specified delay period, this is not a busy-loop. It will
1482  *      give up the processor while waiting. Unfortunately this has some
1483  *      rather intimate knowledge of the process management stuff.
1484  */
1485
1486 static void stli_delay(int len)
1487 {
1488 #ifdef DEBUG
1489         printk("stli_delay(len=%d)\n", len);
1490 #endif
1491         if (len > 0) {
1492                 set_current_state(TASK_INTERRUPTIBLE);
1493                 schedule_timeout(len);
1494         }
1495 }
1496
1497 /*****************************************************************************/
1498
1499 /*
1500  *      Possibly need to wait for carrier (DCD signal) to come high. Say
1501  *      maybe because if we are clocal then we don't need to wait...
1502  */
1503
1504 static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp)
1505 {
1506         unsigned long   flags;
1507         int             rc, doclocal;
1508
1509 #ifdef DEBUG
1510         printk("stli_waitcarrier(brdp=%x,portp=%x,filp=%x)\n",
1511                 (int) brdp, (int) portp, (int) filp);
1512 #endif
1513
1514         rc = 0;
1515         doclocal = 0;
1516
1517         if (portp->tty->termios->c_cflag & CLOCAL)
1518                 doclocal++;
1519
1520         save_flags(flags);
1521         cli();
1522         portp->openwaitcnt++;
1523         if (! tty_hung_up_p(filp))
1524                 portp->refcount--;
1525
1526         for (;;) {
1527                 stli_mkasysigs(&portp->asig, 1, 1);
1528                 if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
1529                     &portp->asig, sizeof(asysigs_t), 0)) < 0)
1530                         break;
1531                 if (tty_hung_up_p(filp) ||
1532                     ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1533                         if (portp->flags & ASYNC_HUP_NOTIFY)
1534                                 rc = -EBUSY;
1535                         else
1536                                 rc = -ERESTARTSYS;
1537                         break;
1538                 }
1539                 if (((portp->flags & ASYNC_CLOSING) == 0) &&
1540                     (doclocal || (portp->sigs & TIOCM_CD))) {
1541                         break;
1542                 }
1543                 if (signal_pending(current)) {
1544                         rc = -ERESTARTSYS;
1545                         break;
1546                 }
1547                 interruptible_sleep_on(&portp->open_wait);
1548         }
1549
1550         if (! tty_hung_up_p(filp))
1551                 portp->refcount++;
1552         portp->openwaitcnt--;
1553         restore_flags(flags);
1554
1555         return(rc);
1556 }
1557
1558 /*****************************************************************************/
1559
1560 /*
1561  *      Write routine. Take the data and put it in the shared memory ring
1562  *      queue. If port is not already sending chars then need to mark the
1563  *      service bits for this port.
1564  */
1565
1566 static int stli_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
1567 {
1568         volatile cdkasy_t       *ap;
1569         volatile cdkhdr_t       *hdrp;
1570         volatile unsigned char  *bits;
1571         unsigned char           *shbuf, *chbuf;
1572         stliport_t              *portp;
1573         stlibrd_t               *brdp;
1574         unsigned int            len, stlen, head, tail, size;
1575         unsigned long           flags;
1576
1577 #ifdef DEBUG
1578         printk("stli_write(tty=%x,from_user=%d,buf=%x,count=%d)\n",
1579                 (int) tty, from_user, (int) buf, count);
1580 #endif
1581
1582         if ((tty == (struct tty_struct *) NULL) ||
1583             (stli_tmpwritebuf == (char *) NULL))
1584                 return(0);
1585         if (tty == stli_txcooktty)
1586                 stli_flushchars(tty);
1587         portp = tty->driver_data;
1588         if (portp == (stliport_t *) NULL)
1589                 return(0);
1590         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1591                 return(0);
1592         brdp = stli_brds[portp->brdnr];
1593         if (brdp == (stlibrd_t *) NULL)
1594                 return(0);
1595         chbuf = (unsigned char *) buf;
1596
1597 /*
1598  *      If copying direct from user space we need to be able to handle page
1599  *      faults while we are copying. To do this copy as much as we can now
1600  *      into a kernel buffer. From there we copy it into shared memory. The
1601  *      big problem is that we do not want shared memory enabled when we are
1602  *      sleeping (other boards may be serviced while asleep). Something else
1603  *      to note here is the reading of the tail twice. Since the boards
1604  *      shared memory can be on an 8-bit bus then we need to be very careful
1605  *      reading 16 bit quantities - since both the board (slave) and host
1606  *      could be writing and reading at the same time.
1607  */
1608         if (from_user) {
1609                 save_flags(flags);
1610                 cli();
1611                 EBRDENABLE(brdp);
1612                 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1613                 head = (unsigned int) ap->txq.head;
1614                 tail = (unsigned int) ap->txq.tail;
1615                 if (tail != ((unsigned int) ap->txq.tail))
1616                         tail = (unsigned int) ap->txq.tail;
1617                 len = (head >= tail) ? (portp->txsize - (head - tail) - 1) :
1618                         (tail - head - 1);
1619                 count = MIN(len, count);
1620                 EBRDDISABLE(brdp);
1621                 restore_flags(flags);
1622
1623                 down(&stli_tmpwritesem);
1624                 if (copy_from_user(stli_tmpwritebuf, chbuf, count)) 
1625                         return -EFAULT;
1626                 chbuf = &stli_tmpwritebuf[0];
1627         }
1628
1629 /*
1630  *      All data is now local, shove as much as possible into shared memory.
1631  */
1632         save_flags(flags);
1633         cli();
1634         EBRDENABLE(brdp);
1635         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1636         head = (unsigned int) ap->txq.head;
1637         tail = (unsigned int) ap->txq.tail;
1638         if (tail != ((unsigned int) ap->txq.tail))
1639                 tail = (unsigned int) ap->txq.tail;
1640         size = portp->txsize;
1641         if (head >= tail) {
1642                 len = size - (head - tail) - 1;
1643                 stlen = size - head;
1644         } else {
1645                 len = tail - head - 1;
1646                 stlen = len;
1647         }
1648
1649         len = MIN(len, count);
1650         count = 0;
1651         shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1652
1653         while (len > 0) {
1654                 stlen = MIN(len, stlen);
1655                 memcpy((shbuf + head), chbuf, stlen);
1656                 chbuf += stlen;
1657                 len -= stlen;
1658                 count += stlen;
1659                 head += stlen;
1660                 if (head >= size) {
1661                         head = 0;
1662                         stlen = tail;
1663                 }
1664         }
1665
1666         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1667         ap->txq.head = head;
1668         if (test_bit(ST_TXBUSY, &portp->state)) {
1669                 if (ap->changed.data & DT_TXEMPTY)
1670                         ap->changed.data &= ~DT_TXEMPTY;
1671         }
1672         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1673         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1674                 portp->portidx;
1675         *bits |= portp->portbit;
1676         set_bit(ST_TXBUSY, &portp->state);
1677         EBRDDISABLE(brdp);
1678
1679         if (from_user)
1680                 up(&stli_tmpwritesem);
1681         restore_flags(flags);
1682
1683         return(count);
1684 }
1685
1686 /*****************************************************************************/
1687
1688 /*
1689  *      Output a single character. We put it into a temporary local buffer
1690  *      (for speed) then write out that buffer when the flushchars routine
1691  *      is called. There is a safety catch here so that if some other port
1692  *      writes chars before the current buffer has been, then we write them
1693  *      first them do the new ports.
1694  */
1695
1696 static void stli_putchar(struct tty_struct *tty, unsigned char ch)
1697 {
1698 #ifdef DEBUG
1699         printk("stli_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1700 #endif
1701
1702         if (tty == (struct tty_struct *) NULL)
1703                 return;
1704         if (tty != stli_txcooktty) {
1705                 if (stli_txcooktty != (struct tty_struct *) NULL)
1706                         stli_flushchars(stli_txcooktty);
1707                 stli_txcooktty = tty;
1708         }
1709
1710         stli_txcookbuf[stli_txcooksize++] = ch;
1711 }
1712
1713 /*****************************************************************************/
1714
1715 /*
1716  *      Transfer characters from the local TX cooking buffer to the board.
1717  *      We sort of ignore the tty that gets passed in here. We rely on the
1718  *      info stored with the TX cook buffer to tell us which port to flush
1719  *      the data on. In any case we clean out the TX cook buffer, for re-use
1720  *      by someone else.
1721  */
1722
1723 static void stli_flushchars(struct tty_struct *tty)
1724 {
1725         volatile cdkhdr_t       *hdrp;
1726         volatile unsigned char  *bits;
1727         volatile cdkasy_t       *ap;
1728         struct tty_struct       *cooktty;
1729         stliport_t              *portp;
1730         stlibrd_t               *brdp;
1731         unsigned int            len, stlen, head, tail, size, count, cooksize;
1732         unsigned char           *buf, *shbuf;
1733         unsigned long           flags;
1734
1735 #ifdef DEBUG
1736         printk("stli_flushchars(tty=%x)\n", (int) tty);
1737 #endif
1738
1739         cooksize = stli_txcooksize;
1740         cooktty = stli_txcooktty;
1741         stli_txcooksize = 0;
1742         stli_txcookrealsize = 0;
1743         stli_txcooktty = (struct tty_struct *) NULL;
1744
1745         if (tty == (struct tty_struct *) NULL)
1746                 return;
1747         if (cooktty == (struct tty_struct *) NULL)
1748                 return;
1749         if (tty != cooktty)
1750                 tty = cooktty;
1751         if (cooksize == 0)
1752                 return;
1753
1754         portp = tty->driver_data;
1755         if (portp == (stliport_t *) NULL)
1756                 return;
1757         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1758                 return;
1759         brdp = stli_brds[portp->brdnr];
1760         if (brdp == (stlibrd_t *) NULL)
1761                 return;
1762
1763         save_flags(flags);
1764         cli();
1765         EBRDENABLE(brdp);
1766
1767         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1768         head = (unsigned int) ap->txq.head;
1769         tail = (unsigned int) ap->txq.tail;
1770         if (tail != ((unsigned int) ap->txq.tail))
1771                 tail = (unsigned int) ap->txq.tail;
1772         size = portp->txsize;
1773         if (head >= tail) {
1774                 len = size - (head - tail) - 1;
1775                 stlen = size - head;
1776         } else {
1777                 len = tail - head - 1;
1778                 stlen = len;
1779         }
1780
1781         len = MIN(len, cooksize);
1782         count = 0;
1783         shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1784         buf = stli_txcookbuf;
1785
1786         while (len > 0) {
1787                 stlen = MIN(len, stlen);
1788                 memcpy((shbuf + head), buf, stlen);
1789                 buf += stlen;
1790                 len -= stlen;
1791                 count += stlen;
1792                 head += stlen;
1793                 if (head >= size) {
1794                         head = 0;
1795                         stlen = tail;
1796                 }
1797         }
1798
1799         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1800         ap->txq.head = head;
1801
1802         if (test_bit(ST_TXBUSY, &portp->state)) {
1803                 if (ap->changed.data & DT_TXEMPTY)
1804                         ap->changed.data &= ~DT_TXEMPTY;
1805         }
1806         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1807         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1808                 portp->portidx;
1809         *bits |= portp->portbit;
1810         set_bit(ST_TXBUSY, &portp->state);
1811
1812         EBRDDISABLE(brdp);
1813         restore_flags(flags);
1814 }
1815
1816 /*****************************************************************************/
1817
1818 static int stli_writeroom(struct tty_struct *tty)
1819 {
1820         volatile cdkasyrq_t     *rp;
1821         stliport_t              *portp;
1822         stlibrd_t               *brdp;
1823         unsigned int            head, tail, len;
1824         unsigned long           flags;
1825
1826 #ifdef DEBUG
1827         printk("stli_writeroom(tty=%x)\n", (int) tty);
1828 #endif
1829
1830         if (tty == (struct tty_struct *) NULL)
1831                 return(0);
1832         if (tty == stli_txcooktty) {
1833                 if (stli_txcookrealsize != 0) {
1834                         len = stli_txcookrealsize - stli_txcooksize;
1835                         return(len);
1836                 }
1837         }
1838
1839         portp = tty->driver_data;
1840         if (portp == (stliport_t *) NULL)
1841                 return(0);
1842         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1843                 return(0);
1844         brdp = stli_brds[portp->brdnr];
1845         if (brdp == (stlibrd_t *) NULL)
1846                 return(0);
1847
1848         save_flags(flags);
1849         cli();
1850         EBRDENABLE(brdp);
1851         rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1852         head = (unsigned int) rp->head;
1853         tail = (unsigned int) rp->tail;
1854         if (tail != ((unsigned int) rp->tail))
1855                 tail = (unsigned int) rp->tail;
1856         len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
1857         len--;
1858         EBRDDISABLE(brdp);
1859         restore_flags(flags);
1860
1861         if (tty == stli_txcooktty) {
1862                 stli_txcookrealsize = len;
1863                 len -= stli_txcooksize;
1864         }
1865         return(len);
1866 }
1867
1868 /*****************************************************************************/
1869
1870 /*
1871  *      Return the number of characters in the transmit buffer. Normally we
1872  *      will return the number of chars in the shared memory ring queue.
1873  *      We need to kludge around the case where the shared memory buffer is
1874  *      empty but not all characters have drained yet, for this case just
1875  *      return that there is 1 character in the buffer!
1876  */
1877
1878 static int stli_charsinbuffer(struct tty_struct *tty)
1879 {
1880         volatile cdkasyrq_t     *rp;
1881         stliport_t              *portp;
1882         stlibrd_t               *brdp;
1883         unsigned int            head, tail, len;
1884         unsigned long           flags;
1885
1886 #ifdef DEBUG
1887         printk("stli_charsinbuffer(tty=%x)\n", (int) tty);
1888 #endif
1889
1890         if (tty == (struct tty_struct *) NULL)
1891                 return(0);
1892         if (tty == stli_txcooktty)
1893                 stli_flushchars(tty);
1894         portp = tty->driver_data;
1895         if (portp == (stliport_t *) NULL)
1896                 return(0);
1897         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1898                 return(0);
1899         brdp = stli_brds[portp->brdnr];
1900         if (brdp == (stlibrd_t *) NULL)
1901                 return(0);
1902
1903         save_flags(flags);
1904         cli();
1905         EBRDENABLE(brdp);
1906         rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1907         head = (unsigned int) rp->head;
1908         tail = (unsigned int) rp->tail;
1909         if (tail != ((unsigned int) rp->tail))
1910                 tail = (unsigned int) rp->tail;
1911         len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
1912         if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
1913                 len = 1;
1914         EBRDDISABLE(brdp);
1915         restore_flags(flags);
1916
1917         return(len);
1918 }
1919
1920 /*****************************************************************************/
1921
1922 /*
1923  *      Generate the serial struct info.
1924  */
1925
1926 static int stli_getserial(stliport_t *portp, struct serial_struct __user *sp)
1927 {
1928         struct serial_struct    sio;
1929         stlibrd_t               *brdp;
1930
1931 #ifdef DEBUG
1932         printk("stli_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1933 #endif
1934
1935         memset(&sio, 0, sizeof(struct serial_struct));
1936         sio.type = PORT_UNKNOWN;
1937         sio.line = portp->portnr;
1938         sio.irq = 0;
1939         sio.flags = portp->flags;
1940         sio.baud_base = portp->baud_base;
1941         sio.close_delay = portp->close_delay;
1942         sio.closing_wait = portp->closing_wait;
1943         sio.custom_divisor = portp->custom_divisor;
1944         sio.xmit_fifo_size = 0;
1945         sio.hub6 = 0;
1946
1947         brdp = stli_brds[portp->brdnr];
1948         if (brdp != (stlibrd_t *) NULL)
1949                 sio.port = brdp->iobase;
1950                 
1951         return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ?
1952                         -EFAULT : 0;
1953 }
1954
1955 /*****************************************************************************/
1956
1957 /*
1958  *      Set port according to the serial struct info.
1959  *      At this point we do not do any auto-configure stuff, so we will
1960  *      just quietly ignore any requests to change irq, etc.
1961  */
1962
1963 static int stli_setserial(stliport_t *portp, struct serial_struct __user *sp)
1964 {
1965         struct serial_struct    sio;
1966         int                     rc;
1967
1968 #ifdef DEBUG
1969         printk("stli_setserial(portp=%p,sp=%p)\n", portp, sp);
1970 #endif
1971
1972         if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1973                 return -EFAULT;
1974         if (!capable(CAP_SYS_ADMIN)) {
1975                 if ((sio.baud_base != portp->baud_base) ||
1976                     (sio.close_delay != portp->close_delay) ||
1977                     ((sio.flags & ~ASYNC_USR_MASK) !=
1978                     (portp->flags & ~ASYNC_USR_MASK)))
1979                         return(-EPERM);
1980         } 
1981
1982         portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
1983                 (sio.flags & ASYNC_USR_MASK);
1984         portp->baud_base = sio.baud_base;
1985         portp->close_delay = sio.close_delay;
1986         portp->closing_wait = sio.closing_wait;
1987         portp->custom_divisor = sio.custom_divisor;
1988
1989         if ((rc = stli_setport(portp)) < 0)
1990                 return(rc);
1991         return(0);
1992 }
1993
1994 /*****************************************************************************/
1995
1996 static int stli_tiocmget(struct tty_struct *tty, struct file *file)
1997 {
1998         stliport_t *portp = tty->driver_data;
1999         stlibrd_t *brdp;
2000         int rc;
2001
2002         if (portp == (stliport_t *) NULL)
2003                 return(-ENODEV);
2004         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2005                 return(0);
2006         brdp = stli_brds[portp->brdnr];
2007         if (brdp == (stlibrd_t *) NULL)
2008                 return(0);
2009         if (tty->flags & (1 << TTY_IO_ERROR))
2010                 return(-EIO);
2011
2012         if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS,
2013                                &portp->asig, sizeof(asysigs_t), 1)) < 0)
2014                 return(rc);
2015
2016         return stli_mktiocm(portp->asig.sigvalue);
2017 }
2018
2019 static int stli_tiocmset(struct tty_struct *tty, struct file *file,
2020                          unsigned int set, unsigned int clear)
2021 {
2022         stliport_t *portp = tty->driver_data;
2023         stlibrd_t *brdp;
2024         int rts = -1, dtr = -1;
2025
2026         if (portp == (stliport_t *) NULL)
2027                 return(-ENODEV);
2028         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2029                 return(0);
2030         brdp = stli_brds[portp->brdnr];
2031         if (brdp == (stlibrd_t *) NULL)
2032                 return(0);
2033         if (tty->flags & (1 << TTY_IO_ERROR))
2034                 return(-EIO);
2035
2036         if (set & TIOCM_RTS)
2037                 rts = 1;
2038         if (set & TIOCM_DTR)
2039                 dtr = 1;
2040         if (clear & TIOCM_RTS)
2041                 rts = 0;
2042         if (clear & TIOCM_DTR)
2043                 dtr = 0;
2044
2045         stli_mkasysigs(&portp->asig, dtr, rts);
2046
2047         return stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
2048                             sizeof(asysigs_t), 0);
2049 }
2050
2051 static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
2052 {
2053         stliport_t      *portp;
2054         stlibrd_t       *brdp;
2055         unsigned int    ival;
2056         int             rc;
2057         void __user *argp = (void __user *)arg;
2058
2059 #ifdef DEBUG
2060         printk("stli_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
2061                 (int) tty, (int) file, cmd, (int) arg);
2062 #endif
2063
2064         if (tty == (struct tty_struct *) NULL)
2065                 return(-ENODEV);
2066         portp = tty->driver_data;
2067         if (portp == (stliport_t *) NULL)
2068                 return(-ENODEV);
2069         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2070                 return(0);
2071         brdp = stli_brds[portp->brdnr];
2072         if (brdp == (stlibrd_t *) NULL)
2073                 return(0);
2074
2075         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2076             (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
2077                 if (tty->flags & (1 << TTY_IO_ERROR))
2078                         return(-EIO);
2079         }
2080
2081         rc = 0;
2082
2083         switch (cmd) {
2084         case TIOCGSOFTCAR:
2085                 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
2086                         (unsigned __user *) arg);
2087                 break;
2088         case TIOCSSOFTCAR:
2089                 if ((rc = get_user(ival, (unsigned __user *) arg)) == 0)
2090                         tty->termios->c_cflag =
2091                                 (tty->termios->c_cflag & ~CLOCAL) |
2092                                 (ival ? CLOCAL : 0);
2093                 break;
2094         case TIOCGSERIAL:
2095                 rc = stli_getserial(portp, argp);
2096                 break;
2097         case TIOCSSERIAL:
2098                 rc = stli_setserial(portp, argp);
2099                 break;
2100         case STL_GETPFLAG:
2101                 rc = put_user(portp->pflag, (unsigned __user *)argp);
2102                 break;
2103         case STL_SETPFLAG:
2104                 if ((rc = get_user(portp->pflag, (unsigned __user *)argp)) == 0)
2105                         stli_setport(portp);
2106                 break;
2107         case COM_GETPORTSTATS:
2108                 rc = stli_getportstats(portp, argp);
2109                 break;
2110         case COM_CLRPORTSTATS:
2111                 rc = stli_clrportstats(portp, argp);
2112                 break;
2113         case TIOCSERCONFIG:
2114         case TIOCSERGWILD:
2115         case TIOCSERSWILD:
2116         case TIOCSERGETLSR:
2117         case TIOCSERGSTRUCT:
2118         case TIOCSERGETMULTI:
2119         case TIOCSERSETMULTI:
2120         default:
2121                 rc = -ENOIOCTLCMD;
2122                 break;
2123         }
2124
2125         return(rc);
2126 }
2127
2128 /*****************************************************************************/
2129
2130 /*
2131  *      This routine assumes that we have user context and can sleep.
2132  *      Looks like it is true for the current ttys implementation..!!
2133  */
2134
2135 static void stli_settermios(struct tty_struct *tty, struct termios *old)
2136 {
2137         stliport_t      *portp;
2138         stlibrd_t       *brdp;
2139         struct termios  *tiosp;
2140         asyport_t       aport;
2141
2142 #ifdef DEBUG
2143         printk("stli_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
2144 #endif
2145
2146         if (tty == (struct tty_struct *) NULL)
2147                 return;
2148         portp = tty->driver_data;
2149         if (portp == (stliport_t *) NULL)
2150                 return;
2151         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2152                 return;
2153         brdp = stli_brds[portp->brdnr];
2154         if (brdp == (stlibrd_t *) NULL)
2155                 return;
2156
2157         tiosp = tty->termios;
2158         if ((tiosp->c_cflag == old->c_cflag) &&
2159             (tiosp->c_iflag == old->c_iflag))
2160                 return;
2161
2162         stli_mkasyport(portp, &aport, tiosp);
2163         stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
2164         stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
2165         stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
2166                 sizeof(asysigs_t), 0);
2167         if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
2168                 tty->hw_stopped = 0;
2169         if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
2170                 wake_up_interruptible(&portp->open_wait);
2171 }
2172
2173 /*****************************************************************************/
2174
2175 /*
2176  *      Attempt to flow control who ever is sending us data. We won't really
2177  *      do any flow control action here. We can't directly, and even if we
2178  *      wanted to we would have to send a command to the slave. The slave
2179  *      knows how to flow control, and will do so when its buffers reach its
2180  *      internal high water marks. So what we will do is set a local state
2181  *      bit that will stop us sending any RX data up from the poll routine
2182  *      (which is the place where RX data from the slave is handled).
2183  */
2184
2185 static void stli_throttle(struct tty_struct *tty)
2186 {
2187         stliport_t      *portp;
2188
2189 #ifdef DEBUG
2190         printk("stli_throttle(tty=%x)\n", (int) tty);
2191 #endif
2192
2193         if (tty == (struct tty_struct *) NULL)
2194                 return;
2195         portp = tty->driver_data;
2196         if (portp == (stliport_t *) NULL)
2197                 return;
2198
2199         set_bit(ST_RXSTOP, &portp->state);
2200 }
2201
2202 /*****************************************************************************/
2203
2204 /*
2205  *      Unflow control the device sending us data... That means that all
2206  *      we have to do is clear the RXSTOP state bit. The next poll call
2207  *      will then be able to pass the RX data back up.
2208  */
2209
2210 static void stli_unthrottle(struct tty_struct *tty)
2211 {
2212         stliport_t      *portp;
2213
2214 #ifdef DEBUG
2215         printk("stli_unthrottle(tty=%x)\n", (int) tty);
2216 #endif
2217
2218         if (tty == (struct tty_struct *) NULL)
2219                 return;
2220         portp = tty->driver_data;
2221         if (portp == (stliport_t *) NULL)
2222                 return;
2223
2224         clear_bit(ST_RXSTOP, &portp->state);
2225 }
2226
2227 /*****************************************************************************/
2228
2229 /*
2230  *      Stop the transmitter. Basically to do this we will just turn TX
2231  *      interrupts off.
2232  */
2233
2234 static void stli_stop(struct tty_struct *tty)
2235 {
2236         stlibrd_t       *brdp;
2237         stliport_t      *portp;
2238         asyctrl_t       actrl;
2239
2240 #ifdef DEBUG
2241         printk("stli_stop(tty=%x)\n", (int) tty);
2242 #endif
2243
2244         if (tty == (struct tty_struct *) NULL)
2245                 return;
2246         portp = tty->driver_data;
2247         if (portp == (stliport_t *) NULL)
2248                 return;
2249         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2250                 return;
2251         brdp = stli_brds[portp->brdnr];
2252         if (brdp == (stlibrd_t *) NULL)
2253                 return;
2254
2255         memset(&actrl, 0, sizeof(asyctrl_t));
2256         actrl.txctrl = CT_STOPFLOW;
2257 #if 0
2258         stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2259 #endif
2260 }
2261
2262 /*****************************************************************************/
2263
2264 /*
2265  *      Start the transmitter again. Just turn TX interrupts back on.
2266  */
2267
2268 static void stli_start(struct tty_struct *tty)
2269 {
2270         stliport_t      *portp;
2271         stlibrd_t       *brdp;
2272         asyctrl_t       actrl;
2273
2274 #ifdef DEBUG
2275         printk("stli_start(tty=%x)\n", (int) tty);
2276 #endif
2277
2278         if (tty == (struct tty_struct *) NULL)
2279                 return;
2280         portp = tty->driver_data;
2281         if (portp == (stliport_t *) NULL)
2282                 return;
2283         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2284                 return;
2285         brdp = stli_brds[portp->brdnr];
2286         if (brdp == (stlibrd_t *) NULL)
2287                 return;
2288
2289         memset(&actrl, 0, sizeof(asyctrl_t));
2290         actrl.txctrl = CT_STARTFLOW;
2291 #if 0
2292         stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2293 #endif
2294 }
2295
2296 /*****************************************************************************/
2297
2298 /*
2299  *      Scheduler called hang up routine. This is called from the scheduler,
2300  *      not direct from the driver "poll" routine. We can't call it there
2301  *      since the real local hangup code will enable/disable the board and
2302  *      other things that we can't do while handling the poll. Much easier
2303  *      to deal with it some time later (don't really care when, hangups
2304  *      aren't that time critical).
2305  */
2306
2307 static void stli_dohangup(void *arg)
2308 {
2309         stliport_t      *portp;
2310
2311 #ifdef DEBUG
2312         printk(KERN_DEBUG "stli_dohangup(portp=%x)\n", (int) arg);
2313 #endif
2314
2315         /*
2316          * FIXME: There's a module removal race here: tty_hangup
2317          * calls schedule_work which will call into this
2318          * driver later.
2319          */
2320         portp = (stliport_t *) arg;
2321         if (portp != (stliport_t *) NULL) {
2322                 if (portp->tty != (struct tty_struct *) NULL) {
2323                         tty_hangup(portp->tty);
2324                 }
2325         }
2326 }
2327
2328 /*****************************************************************************/
2329
2330 /*
2331  *      Hangup this port. This is pretty much like closing the port, only
2332  *      a little more brutal. No waiting for data to drain. Shutdown the
2333  *      port and maybe drop signals. This is rather tricky really. We want
2334  *      to close the port as well.
2335  */
2336
2337 static void stli_hangup(struct tty_struct *tty)
2338 {
2339         stliport_t      *portp;
2340         stlibrd_t       *brdp;
2341         unsigned long   flags;
2342
2343 #ifdef DEBUG
2344         printk(KERN_DEBUG "stli_hangup(tty=%x)\n", (int) tty);
2345 #endif
2346
2347         if (tty == (struct tty_struct *) NULL)
2348                 return;
2349         portp = tty->driver_data;
2350         if (portp == (stliport_t *) NULL)
2351                 return;
2352         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2353                 return;
2354         brdp = stli_brds[portp->brdnr];
2355         if (brdp == (stlibrd_t *) NULL)
2356                 return;
2357
2358         portp->flags &= ~ASYNC_INITIALIZED;
2359
2360         save_flags(flags);
2361         cli();
2362         if (! test_bit(ST_CLOSING, &portp->state))
2363                 stli_rawclose(brdp, portp, 0, 0);
2364         if (tty->termios->c_cflag & HUPCL) {
2365                 stli_mkasysigs(&portp->asig, 0, 0);
2366                 if (test_bit(ST_CMDING, &portp->state)) {
2367                         set_bit(ST_DOSIGS, &portp->state);
2368                         set_bit(ST_DOFLUSHTX, &portp->state);
2369                         set_bit(ST_DOFLUSHRX, &portp->state);
2370                 } else {
2371                         stli_sendcmd(brdp, portp, A_SETSIGNALSF,
2372                                 &portp->asig, sizeof(asysigs_t), 0);
2373                 }
2374         }
2375         restore_flags(flags);
2376
2377         clear_bit(ST_TXBUSY, &portp->state);
2378         clear_bit(ST_RXSTOP, &portp->state);
2379         set_bit(TTY_IO_ERROR, &tty->flags);
2380         portp->tty = (struct tty_struct *) NULL;
2381         portp->flags &= ~ASYNC_NORMAL_ACTIVE;
2382         portp->refcount = 0;
2383         wake_up_interruptible(&portp->open_wait);
2384 }
2385
2386 /*****************************************************************************/
2387
2388 /*
2389  *      Flush characters from the lower buffer. We may not have user context
2390  *      so we cannot sleep waiting for it to complete. Also we need to check
2391  *      if there is chars for this port in the TX cook buffer, and flush them
2392  *      as well.
2393  */
2394
2395 static void stli_flushbuffer(struct tty_struct *tty)
2396 {
2397         stliport_t      *portp;
2398         stlibrd_t       *brdp;
2399         unsigned long   ftype, flags;
2400
2401 #ifdef DEBUG
2402         printk(KERN_DEBUG "stli_flushbuffer(tty=%x)\n", (int) tty);
2403 #endif
2404
2405         if (tty == (struct tty_struct *) NULL)
2406                 return;
2407         portp = tty->driver_data;
2408         if (portp == (stliport_t *) NULL)
2409                 return;
2410         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2411                 return;
2412         brdp = stli_brds[portp->brdnr];
2413         if (brdp == (stlibrd_t *) NULL)
2414                 return;
2415
2416         save_flags(flags);
2417         cli();
2418         if (tty == stli_txcooktty) {
2419                 stli_txcooktty = (struct tty_struct *) NULL;
2420                 stli_txcooksize = 0;
2421                 stli_txcookrealsize = 0;
2422         }
2423         if (test_bit(ST_CMDING, &portp->state)) {
2424                 set_bit(ST_DOFLUSHTX, &portp->state);
2425         } else {
2426                 ftype = FLUSHTX;
2427                 if (test_bit(ST_DOFLUSHRX, &portp->state)) {
2428                         ftype |= FLUSHRX;
2429                         clear_bit(ST_DOFLUSHRX, &portp->state);
2430                 }
2431                 stli_sendcmd(brdp, portp, A_FLUSH, &ftype,
2432                         sizeof(unsigned long), 0);
2433         }
2434         restore_flags(flags);
2435
2436         wake_up_interruptible(&tty->write_wait);
2437         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2438             tty->ldisc.write_wakeup)
2439                 (tty->ldisc.write_wakeup)(tty);
2440 }
2441
2442 /*****************************************************************************/
2443
2444 static void stli_breakctl(struct tty_struct *tty, int state)
2445 {
2446         stlibrd_t       *brdp;
2447         stliport_t      *portp;
2448         long            arg;
2449         /* long savestate, savetime; */
2450
2451 #ifdef DEBUG
2452         printk(KERN_DEBUG "stli_breakctl(tty=%x,state=%d)\n", (int) tty, state);
2453 #endif
2454
2455         if (tty == (struct tty_struct *) NULL)
2456                 return;
2457         portp = tty->driver_data;
2458         if (portp == (stliport_t *) NULL)
2459                 return;
2460         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2461                 return;
2462         brdp = stli_brds[portp->brdnr];
2463         if (brdp == (stlibrd_t *) NULL)
2464                 return;
2465
2466 /*
2467  *      Due to a bug in the tty send_break() code we need to preserve
2468  *      the current process state and timeout...
2469         savetime = current->timeout;
2470         savestate = current->state;
2471  */
2472
2473         arg = (state == -1) ? BREAKON : BREAKOFF;
2474         stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0);
2475
2476 /*
2477  *
2478         current->timeout = savetime;
2479         current->state = savestate;
2480  */
2481 }
2482
2483 /*****************************************************************************/
2484
2485 static void stli_waituntilsent(struct tty_struct *tty, int timeout)
2486 {
2487         stliport_t      *portp;
2488         unsigned long   tend;
2489
2490 #ifdef DEBUG
2491         printk(KERN_DEBUG "stli_waituntilsent(tty=%x,timeout=%x)\n", (int) tty, timeout);
2492 #endif
2493
2494         if (tty == (struct tty_struct *) NULL)
2495                 return;
2496         portp = tty->driver_data;
2497         if (portp == (stliport_t *) NULL)
2498                 return;
2499
2500         if (timeout == 0)
2501                 timeout = HZ;
2502         tend = jiffies + timeout;
2503
2504         while (test_bit(ST_TXBUSY, &portp->state)) {
2505                 if (signal_pending(current))
2506                         break;
2507                 stli_delay(2);
2508                 if (time_after_eq(jiffies, tend))
2509                         break;
2510         }
2511 }
2512
2513 /*****************************************************************************/
2514
2515 static void stli_sendxchar(struct tty_struct *tty, char ch)
2516 {
2517         stlibrd_t       *brdp;
2518         stliport_t      *portp;
2519         asyctrl_t       actrl;
2520
2521 #ifdef DEBUG
2522         printk(KERN_DEBUG "stli_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
2523 #endif
2524
2525         if (tty == (struct tty_struct *) NULL)
2526                 return;
2527         portp = tty->driver_data;
2528         if (portp == (stliport_t *) NULL)
2529                 return;
2530         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2531                 return;
2532         brdp = stli_brds[portp->brdnr];
2533         if (brdp == (stlibrd_t *) NULL)
2534                 return;
2535
2536         memset(&actrl, 0, sizeof(asyctrl_t));
2537         if (ch == STOP_CHAR(tty)) {
2538                 actrl.rxctrl = CT_STOPFLOW;
2539         } else if (ch == START_CHAR(tty)) {
2540                 actrl.rxctrl = CT_STARTFLOW;
2541         } else {
2542                 actrl.txctrl = CT_SENDCHR;
2543                 actrl.tximdch = ch;
2544         }
2545
2546         stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2547 }
2548
2549 /*****************************************************************************/
2550
2551 #define MAXLINE         80
2552
2553 /*
2554  *      Format info for a specified port. The line is deliberately limited
2555  *      to 80 characters. (If it is too long it will be truncated, if too
2556  *      short then padded with spaces).
2557  */
2558
2559 static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos)
2560 {
2561         char    *sp, *uart;
2562         int     rc, cnt;
2563
2564         rc = stli_portcmdstats(portp);
2565
2566         uart = "UNKNOWN";
2567         if (brdp->state & BST_STARTED) {
2568                 switch (stli_comstats.hwid) {
2569                 case 0:         uart = "2681"; break;
2570                 case 1:         uart = "SC26198"; break;
2571                 default:        uart = "CD1400"; break;
2572                 }
2573         }
2574
2575         sp = pos;
2576         sp += sprintf(sp, "%d: uart:%s ", portnr, uart);
2577
2578         if ((brdp->state & BST_STARTED) && (rc >= 0)) {
2579                 sp += sprintf(sp, "tx:%d rx:%d", (int) stli_comstats.txtotal,
2580                         (int) stli_comstats.rxtotal);
2581
2582                 if (stli_comstats.rxframing)
2583                         sp += sprintf(sp, " fe:%d",
2584                                 (int) stli_comstats.rxframing);
2585                 if (stli_comstats.rxparity)
2586                         sp += sprintf(sp, " pe:%d",
2587                                 (int) stli_comstats.rxparity);
2588                 if (stli_comstats.rxbreaks)
2589                         sp += sprintf(sp, " brk:%d",
2590                                 (int) stli_comstats.rxbreaks);
2591                 if (stli_comstats.rxoverrun)
2592                         sp += sprintf(sp, " oe:%d",
2593                                 (int) stli_comstats.rxoverrun);
2594
2595                 cnt = sprintf(sp, "%s%s%s%s%s ",
2596                         (stli_comstats.signals & TIOCM_RTS) ? "|RTS" : "",
2597                         (stli_comstats.signals & TIOCM_CTS) ? "|CTS" : "",
2598                         (stli_comstats.signals & TIOCM_DTR) ? "|DTR" : "",
2599                         (stli_comstats.signals & TIOCM_CD) ? "|DCD" : "",
2600                         (stli_comstats.signals & TIOCM_DSR) ? "|DSR" : "");
2601                 *sp = ' ';
2602                 sp += cnt;
2603         }
2604
2605         for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
2606                 *sp++ = ' ';
2607         if (cnt >= MAXLINE)
2608                 pos[(MAXLINE - 2)] = '+';
2609         pos[(MAXLINE - 1)] = '\n';
2610
2611         return(MAXLINE);
2612 }
2613
2614 /*****************************************************************************/
2615
2616 /*
2617  *      Port info, read from the /proc file system.
2618  */
2619
2620 static int stli_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
2621 {
2622         stlibrd_t       *brdp;
2623         stliport_t      *portp;
2624         int             brdnr, portnr, totalport;
2625         int             curoff, maxoff;
2626         char            *pos;
2627
2628 #ifdef DEBUG
2629         printk(KERN_DEBUG "stli_readproc(page=%x,start=%x,off=%x,count=%d,eof=%x,"
2630                 "data=%x\n", (int) page, (int) start, (int) off, count,
2631                 (int) eof, (int) data);
2632 #endif
2633
2634         pos = page;
2635         totalport = 0;
2636         curoff = 0;
2637
2638         if (off == 0) {
2639                 pos += sprintf(pos, "%s: version %s", stli_drvtitle,
2640                         stli_drvversion);
2641                 while (pos < (page + MAXLINE - 1))
2642                         *pos++ = ' ';
2643                 *pos++ = '\n';
2644         }
2645         curoff =  MAXLINE;
2646
2647 /*
2648  *      We scan through for each board, panel and port. The offset is
2649  *      calculated on the fly, and irrelevant ports are skipped.
2650  */
2651         for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2652                 brdp = stli_brds[brdnr];
2653                 if (brdp == (stlibrd_t *) NULL)
2654                         continue;
2655                 if (brdp->state == 0)
2656                         continue;
2657
2658                 maxoff = curoff + (brdp->nrports * MAXLINE);
2659                 if (off >= maxoff) {
2660                         curoff = maxoff;
2661                         continue;
2662                 }
2663
2664                 totalport = brdnr * STL_MAXPORTS;
2665                 for (portnr = 0; (portnr < brdp->nrports); portnr++,
2666                     totalport++) {
2667                         portp = brdp->ports[portnr];
2668                         if (portp == (stliport_t *) NULL)
2669                                 continue;
2670                         if (off >= (curoff += MAXLINE))
2671                                 continue;
2672                         if ((pos - page + MAXLINE) > count)
2673                                 goto stli_readdone;
2674                         pos += stli_portinfo(brdp, portp, totalport, pos);
2675                 }
2676         }
2677
2678         *eof = 1;
2679
2680 stli_readdone:
2681         *start = page;
2682         return(pos - page);
2683 }
2684
2685 /*****************************************************************************/
2686
2687 /*
2688  *      Generic send command routine. This will send a message to the slave,
2689  *      of the specified type with the specified argument. Must be very
2690  *      careful of data that will be copied out from shared memory -
2691  *      containing command results. The command completion is all done from
2692  *      a poll routine that does not have user context. Therefore you cannot
2693  *      copy back directly into user space, or to the kernel stack of a
2694  *      process. This routine does not sleep, so can be called from anywhere.
2695  */
2696
2697 static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
2698 {
2699         volatile cdkhdr_t       *hdrp;
2700         volatile cdkctrl_t      *cp;
2701         volatile unsigned char  *bits;
2702         unsigned long           flags;
2703
2704 #ifdef DEBUG
2705         printk(KERN_DEBUG "stli_sendcmd(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
2706                 "copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
2707                 (int) arg, size, copyback);
2708 #endif
2709
2710         save_flags(flags);
2711         cli();
2712
2713         if (test_bit(ST_CMDING, &portp->state)) {
2714                 printk(KERN_ERR "STALLION: command already busy, cmd=%x!\n",
2715                                 (int) cmd);
2716                 restore_flags(flags);
2717                 return;
2718         }
2719
2720         EBRDENABLE(brdp);
2721         cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
2722         if (size > 0) {
2723                 memcpy((void *) &(cp->args[0]), arg, size);
2724                 if (copyback) {
2725                         portp->argp = arg;
2726                         portp->argsize = size;
2727                 }
2728         }
2729         cp->status = 0;
2730         cp->cmd = cmd;
2731         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2732         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
2733                 portp->portidx;
2734         *bits |= portp->portbit;
2735         set_bit(ST_CMDING, &portp->state);
2736         EBRDDISABLE(brdp);
2737         restore_flags(flags);
2738 }
2739
2740 /*****************************************************************************/
2741
2742 /*
2743  *      Read data from shared memory. This assumes that the shared memory
2744  *      is enabled and that interrupts are off. Basically we just empty out
2745  *      the shared memory buffer into the tty buffer. Must be careful to
2746  *      handle the case where we fill up the tty buffer, but still have
2747  *      more chars to unload.
2748  */
2749
2750 static void stli_read(stlibrd_t *brdp, stliport_t *portp)
2751 {
2752         volatile cdkasyrq_t     *rp;
2753         volatile char           *shbuf;
2754         struct tty_struct       *tty;
2755         unsigned int            head, tail, size;
2756         unsigned int            len, stlen;
2757
2758 #ifdef DEBUG
2759         printk(KERN_DEBUG "stli_read(brdp=%x,portp=%d)\n",
2760                         (int) brdp, (int) portp);
2761 #endif
2762
2763         if (test_bit(ST_RXSTOP, &portp->state))
2764                 return;
2765         tty = portp->tty;
2766         if (tty == (struct tty_struct *) NULL)
2767                 return;
2768
2769         rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2770         head = (unsigned int) rp->head;
2771         if (head != ((unsigned int) rp->head))
2772                 head = (unsigned int) rp->head;
2773         tail = (unsigned int) rp->tail;
2774         size = portp->rxsize;
2775         if (head >= tail) {
2776                 len = head - tail;
2777                 stlen = len;
2778         } else {
2779                 len = size - (tail - head);
2780                 stlen = size - tail;
2781         }
2782
2783         len = MIN(len, (TTY_FLIPBUF_SIZE - tty->flip.count));
2784         shbuf = (volatile char *) EBRDGETMEMPTR(brdp, portp->rxoffset);
2785
2786         while (len > 0) {
2787                 stlen = MIN(len, stlen);
2788                 memcpy(tty->flip.char_buf_ptr, (char *) (shbuf + tail), stlen);
2789                 memset(tty->flip.flag_buf_ptr, 0, stlen);
2790                 tty->flip.char_buf_ptr += stlen;
2791                 tty->flip.flag_buf_ptr += stlen;
2792                 tty->flip.count += stlen;
2793
2794                 len -= stlen;
2795                 tail += stlen;
2796                 if (tail >= size) {
2797                         tail = 0;
2798                         stlen = head;
2799                 }
2800         }
2801         rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2802         rp->tail = tail;
2803
2804         if (head != tail)
2805                 set_bit(ST_RXING, &portp->state);
2806
2807         tty_schedule_flip(tty);
2808 }
2809
2810 /*****************************************************************************/
2811
2812 /*
2813  *      Set up and carry out any delayed commands. There is only a small set
2814  *      of slave commands that can be done "off-level". So it is not too
2815  *      difficult to deal with them here.
2816  */
2817
2818 static void stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp)
2819 {
2820         int     cmd;
2821
2822         if (test_bit(ST_DOSIGS, &portp->state)) {
2823                 if (test_bit(ST_DOFLUSHTX, &portp->state) &&
2824                     test_bit(ST_DOFLUSHRX, &portp->state))
2825                         cmd = A_SETSIGNALSF;
2826                 else if (test_bit(ST_DOFLUSHTX, &portp->state))
2827                         cmd = A_SETSIGNALSFTX;
2828                 else if (test_bit(ST_DOFLUSHRX, &portp->state))
2829                         cmd = A_SETSIGNALSFRX;
2830                 else
2831                         cmd = A_SETSIGNALS;
2832                 clear_bit(ST_DOFLUSHTX, &portp->state);
2833                 clear_bit(ST_DOFLUSHRX, &portp->state);
2834                 clear_bit(ST_DOSIGS, &portp->state);
2835                 memcpy((void *) &(cp->args[0]), (void *) &portp->asig,
2836                         sizeof(asysigs_t));
2837                 cp->status = 0;
2838                 cp->cmd = cmd;
2839                 set_bit(ST_CMDING, &portp->state);
2840         } else if (test_bit(ST_DOFLUSHTX, &portp->state) ||
2841             test_bit(ST_DOFLUSHRX, &portp->state)) {
2842                 cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
2843                 cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
2844                 clear_bit(ST_DOFLUSHTX, &portp->state);
2845                 clear_bit(ST_DOFLUSHRX, &portp->state);
2846                 memcpy((void *) &(cp->args[0]), (void *) &cmd, sizeof(int));
2847                 cp->status = 0;
2848                 cp->cmd = A_FLUSH;
2849                 set_bit(ST_CMDING, &portp->state);
2850         }
2851 }
2852
2853 /*****************************************************************************/
2854
2855 /*
2856  *      Host command service checking. This handles commands or messages
2857  *      coming from the slave to the host. Must have board shared memory
2858  *      enabled and interrupts off when called. Notice that by servicing the
2859  *      read data last we don't need to change the shared memory pointer
2860  *      during processing (which is a slow IO operation).
2861  *      Return value indicates if this port is still awaiting actions from
2862  *      the slave (like open, command, or even TX data being sent). If 0
2863  *      then port is still busy, otherwise no longer busy.
2864  */
2865
2866 static int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp)
2867 {
2868         volatile cdkasy_t       *ap;
2869         volatile cdkctrl_t      *cp;
2870         struct tty_struct       *tty;
2871         asynotify_t             nt;
2872         unsigned long           oldsigs;
2873         int                     rc, donerx;
2874
2875 #ifdef DEBUG
2876         printk(KERN_DEBUG "stli_hostcmd(brdp=%x,channr=%d)\n",
2877                         (int) brdp, channr);
2878 #endif
2879
2880         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
2881         cp = &ap->ctrl;
2882
2883 /*
2884  *      Check if we are waiting for an open completion message.
2885  */
2886         if (test_bit(ST_OPENING, &portp->state)) {
2887                 rc = (int) cp->openarg;
2888                 if ((cp->open == 0) && (rc != 0)) {
2889                         if (rc > 0)
2890                                 rc--;
2891                         cp->openarg = 0;
2892                         portp->rc = rc;
2893                         clear_bit(ST_OPENING, &portp->state);
2894                         wake_up_interruptible(&portp->raw_wait);
2895                 }
2896         }
2897
2898 /*
2899  *      Check if we are waiting for a close completion message.
2900  */
2901         if (test_bit(ST_CLOSING, &portp->state)) {
2902                 rc = (int) cp->closearg;
2903                 if ((cp->close == 0) && (rc != 0)) {
2904                         if (rc > 0)
2905                                 rc--;
2906                         cp->closearg = 0;
2907                         portp->rc = rc;
2908                         clear_bit(ST_CLOSING, &portp->state);
2909                         wake_up_interruptible(&portp->raw_wait);
2910                 }
2911         }
2912
2913 /*
2914  *      Check if we are waiting for a command completion message. We may
2915  *      need to copy out the command results associated with this command.
2916  */
2917         if (test_bit(ST_CMDING, &portp->state)) {
2918                 rc = cp->status;
2919                 if ((cp->cmd == 0) && (rc != 0)) {
2920                         if (rc > 0)
2921                                 rc--;
2922                         if (portp->argp != (void *) NULL) {
2923                                 memcpy(portp->argp, (void *) &(cp->args[0]),
2924                                         portp->argsize);
2925                                 portp->argp = (void *) NULL;
2926                         }
2927                         cp->status = 0;
2928                         portp->rc = rc;
2929                         clear_bit(ST_CMDING, &portp->state);
2930                         stli_dodelaycmd(portp, cp);
2931                         wake_up_interruptible(&portp->raw_wait);
2932                 }
2933         }
2934
2935 /*
2936  *      Check for any notification messages ready. This includes lots of
2937  *      different types of events - RX chars ready, RX break received,
2938  *      TX data low or empty in the slave, modem signals changed state.
2939  */
2940         donerx = 0;
2941
2942         if (ap->notify) {
2943                 nt = ap->changed;
2944                 ap->notify = 0;
2945                 tty = portp->tty;
2946
2947                 if (nt.signal & SG_DCD) {
2948                         oldsigs = portp->sigs;
2949                         portp->sigs = stli_mktiocm(nt.sigvalue);
2950                         clear_bit(ST_GETSIGS, &portp->state);
2951                         if ((portp->sigs & TIOCM_CD) &&
2952                             ((oldsigs & TIOCM_CD) == 0))
2953                                 wake_up_interruptible(&portp->open_wait);
2954                         if ((oldsigs & TIOCM_CD) &&
2955                             ((portp->sigs & TIOCM_CD) == 0)) {
2956                                 if (portp->flags & ASYNC_CHECK_CD) {
2957                                         if (tty)
2958                                                 schedule_work(&portp->tqhangup);
2959                                 }
2960                         }
2961                 }
2962
2963                 if (nt.data & DT_TXEMPTY)
2964                         clear_bit(ST_TXBUSY, &portp->state);
2965                 if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
2966                         if (tty != (struct tty_struct *) NULL) {
2967                                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2968                                     tty->ldisc.write_wakeup) {
2969                                         (tty->ldisc.write_wakeup)(tty);
2970                                         EBRDENABLE(brdp);
2971                                 }
2972                                 wake_up_interruptible(&tty->write_wait);
2973                         }
2974                 }
2975
2976                 if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
2977                         if (tty != (struct tty_struct *) NULL) {
2978                                 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
2979                                         tty->flip.count++;
2980                                         *tty->flip.flag_buf_ptr++ = TTY_BREAK;
2981                                         *tty->flip.char_buf_ptr++ = 0;
2982                                         if (portp->flags & ASYNC_SAK) {
2983                                                 do_SAK(tty);
2984                                                 EBRDENABLE(brdp);
2985                                         }
2986                                         tty_schedule_flip(tty);
2987                                 }
2988                         }
2989                 }
2990
2991                 if (nt.data & DT_RXBUSY) {
2992                         donerx++;
2993                         stli_read(brdp, portp);
2994                 }
2995         }
2996
2997 /*
2998  *      It might seem odd that we are checking for more RX chars here.
2999  *      But, we need to handle the case where the tty buffer was previously
3000  *      filled, but we had more characters to pass up. The slave will not
3001  *      send any more RX notify messages until the RX buffer has been emptied.
3002  *      But it will leave the service bits on (since the buffer is not empty).
3003  *      So from here we can try to process more RX chars.
3004  */
3005         if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
3006                 clear_bit(ST_RXING, &portp->state);
3007                 stli_read(brdp, portp);
3008         }
3009
3010         return((test_bit(ST_OPENING, &portp->state) ||
3011                 test_bit(ST_CLOSING, &portp->state) ||
3012                 test_bit(ST_CMDING, &portp->state) ||
3013                 test_bit(ST_TXBUSY, &portp->state) ||
3014                 test_bit(ST_RXING, &portp->state)) ? 0 : 1);
3015 }
3016
3017 /*****************************************************************************/
3018
3019 /*
3020  *      Service all ports on a particular board. Assumes that the boards
3021  *      shared memory is enabled, and that the page pointer is pointed
3022  *      at the cdk header structure.
3023  */
3024
3025 static void stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp)
3026 {
3027         stliport_t      *portp;
3028         unsigned char   hostbits[(STL_MAXCHANS / 8) + 1];
3029         unsigned char   slavebits[(STL_MAXCHANS / 8) + 1];
3030         unsigned char   *slavep;
3031         int             bitpos, bitat, bitsize;
3032         int             channr, nrdevs, slavebitchange;
3033
3034         bitsize = brdp->bitsize;
3035         nrdevs = brdp->nrdevs;
3036
3037 /*
3038  *      Check if slave wants any service. Basically we try to do as
3039  *      little work as possible here. There are 2 levels of service
3040  *      bits. So if there is nothing to do we bail early. We check
3041  *      8 service bits at a time in the inner loop, so we can bypass
3042  *      the lot if none of them want service.
3043  */
3044         memcpy(&hostbits[0], (((unsigned char *) hdrp) + brdp->hostoffset),
3045                 bitsize);
3046
3047         memset(&slavebits[0], 0, bitsize);
3048         slavebitchange = 0;
3049
3050         for (bitpos = 0; (bitpos < bitsize); bitpos++) {
3051                 if (hostbits[bitpos] == 0)
3052                         continue;
3053                 channr = bitpos * 8;
3054                 for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
3055                         if (hostbits[bitpos] & bitat) {
3056                                 portp = brdp->ports[(channr - 1)];
3057                                 if (stli_hostcmd(brdp, portp)) {
3058                                         slavebitchange++;
3059                                         slavebits[bitpos] |= bitat;
3060                                 }
3061                         }
3062                 }
3063         }
3064
3065 /*
3066  *      If any of the ports are no longer busy then update them in the
3067  *      slave request bits. We need to do this after, since a host port
3068  *      service may initiate more slave requests.
3069  */
3070         if (slavebitchange) {
3071                 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3072                 slavep = ((unsigned char *) hdrp) + brdp->slaveoffset;
3073                 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
3074                         if (slavebits[bitpos])
3075                                 slavep[bitpos] &= ~slavebits[bitpos];
3076                 }
3077         }
3078 }
3079
3080 /*****************************************************************************/
3081
3082 /*
3083  *      Driver poll routine. This routine polls the boards in use and passes
3084  *      messages back up to host when necessary. This is actually very
3085  *      CPU efficient, since we will always have the kernel poll clock, it
3086  *      adds only a few cycles when idle (since board service can be
3087  *      determined very easily), but when loaded generates no interrupts
3088  *      (with their expensive associated context change).
3089  */
3090
3091 static void stli_poll(unsigned long arg)
3092 {
3093         volatile cdkhdr_t       *hdrp;
3094         stlibrd_t               *brdp;
3095         int                     brdnr;
3096
3097         stli_timerlist.expires = STLI_TIMEOUT;
3098         add_timer(&stli_timerlist);
3099
3100 /*
3101  *      Check each board and do any servicing required.
3102  */
3103         for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
3104                 brdp = stli_brds[brdnr];
3105                 if (brdp == (stlibrd_t *) NULL)
3106                         continue;
3107                 if ((brdp->state & BST_STARTED) == 0)
3108                         continue;
3109
3110                 EBRDENABLE(brdp);
3111                 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3112                 if (hdrp->hostreq)
3113                         stli_brdpoll(brdp, hdrp);
3114                 EBRDDISABLE(brdp);
3115         }
3116 }
3117
3118 /*****************************************************************************/
3119
3120 /*
3121  *      Translate the termios settings into the port setting structure of
3122  *      the slave.
3123  */
3124
3125 static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp)
3126 {
3127 #ifdef DEBUG
3128         printk(KERN_DEBUG "stli_mkasyport(portp=%x,pp=%x,tiosp=%d)\n",
3129                 (int) portp, (int) pp, (int) tiosp);
3130 #endif
3131
3132         memset(pp, 0, sizeof(asyport_t));
3133
3134 /*
3135  *      Start of by setting the baud, char size, parity and stop bit info.
3136  */
3137         pp->baudout = tiosp->c_cflag & CBAUD;
3138         if (pp->baudout & CBAUDEX) {
3139                 pp->baudout &= ~CBAUDEX;
3140                 if ((pp->baudout < 1) || (pp->baudout > 4))
3141                         tiosp->c_cflag &= ~CBAUDEX;
3142                 else
3143                         pp->baudout += 15;
3144         }
3145         pp->baudout = stli_baudrates[pp->baudout];
3146         if ((tiosp->c_cflag & CBAUD) == B38400) {
3147                 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3148                         pp->baudout = 57600;
3149                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3150                         pp->baudout = 115200;
3151                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3152                         pp->baudout = 230400;
3153                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3154                         pp->baudout = 460800;
3155                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3156                         pp->baudout = (portp->baud_base / portp->custom_divisor);
3157         }
3158         if (pp->baudout > STL_MAXBAUD)
3159                 pp->baudout = STL_MAXBAUD;
3160         pp->baudin = pp->baudout;
3161
3162         switch (tiosp->c_cflag & CSIZE) {
3163         case CS5:
3164                 pp->csize = 5;
3165                 break;
3166         case CS6:
3167                 pp->csize = 6;
3168                 break;
3169         case CS7:
3170                 pp->csize = 7;
3171                 break;
3172         default:
3173                 pp->csize = 8;
3174                 break;
3175         }
3176
3177         if (tiosp->c_cflag & CSTOPB)
3178                 pp->stopbs = PT_STOP2;
3179         else
3180                 pp->stopbs = PT_STOP1;
3181
3182         if (tiosp->c_cflag & PARENB) {
3183                 if (tiosp->c_cflag & PARODD)
3184                         pp->parity = PT_ODDPARITY;
3185                 else
3186                         pp->parity = PT_EVENPARITY;
3187         } else {
3188                 pp->parity = PT_NOPARITY;
3189         }
3190
3191 /*
3192  *      Set up any flow control options enabled.
3193  */
3194         if (tiosp->c_iflag & IXON) {
3195                 pp->flow |= F_IXON;
3196                 if (tiosp->c_iflag & IXANY)
3197                         pp->flow |= F_IXANY;
3198         }
3199         if (tiosp->c_cflag & CRTSCTS)
3200                 pp->flow |= (F_RTSFLOW | F_CTSFLOW);
3201
3202         pp->startin = tiosp->c_cc[VSTART];
3203         pp->stopin = tiosp->c_cc[VSTOP];
3204         pp->startout = tiosp->c_cc[VSTART];
3205         pp->stopout = tiosp->c_cc[VSTOP];
3206
3207 /*
3208  *      Set up the RX char marking mask with those RX error types we must
3209  *      catch. We can get the slave to help us out a little here, it will
3210  *      ignore parity errors and breaks for us, and mark parity errors in
3211  *      the data stream.
3212  */
3213         if (tiosp->c_iflag & IGNPAR)
3214                 pp->iflag |= FI_IGNRXERRS;
3215         if (tiosp->c_iflag & IGNBRK)
3216                 pp->iflag |= FI_IGNBREAK;
3217
3218         portp->rxmarkmsk = 0;
3219         if (tiosp->c_iflag & (INPCK | PARMRK))
3220                 pp->iflag |= FI_1MARKRXERRS;
3221         if (tiosp->c_iflag & BRKINT)
3222                 portp->rxmarkmsk |= BRKINT;
3223
3224 /*
3225  *      Set up clocal processing as required.
3226  */
3227         if (tiosp->c_cflag & CLOCAL)
3228                 portp->flags &= ~ASYNC_CHECK_CD;
3229         else
3230                 portp->flags |= ASYNC_CHECK_CD;
3231
3232 /*
3233  *      Transfer any persistent flags into the asyport structure.
3234  */
3235         pp->pflag = (portp->pflag & 0xffff);
3236         pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
3237         pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
3238         pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
3239 }
3240
3241 /*****************************************************************************/
3242
3243 /*
3244  *      Construct a slave signals structure for setting the DTR and RTS
3245  *      signals as specified.
3246  */
3247
3248 static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
3249 {
3250 #ifdef DEBUG
3251         printk(KERN_DEBUG "stli_mkasysigs(sp=%x,dtr=%d,rts=%d)\n",
3252                         (int) sp, dtr, rts);
3253 #endif
3254
3255         memset(sp, 0, sizeof(asysigs_t));
3256         if (dtr >= 0) {
3257                 sp->signal |= SG_DTR;
3258                 sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
3259         }
3260         if (rts >= 0) {
3261                 sp->signal |= SG_RTS;
3262                 sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
3263         }
3264 }
3265
3266 /*****************************************************************************/
3267
3268 /*
3269  *      Convert the signals returned from the slave into a local TIOCM type
3270  *      signals value. We keep them locally in TIOCM format.
3271  */
3272
3273 static long stli_mktiocm(unsigned long sigvalue)
3274 {
3275         long    tiocm;
3276
3277 #ifdef DEBUG
3278         printk(KERN_DEBUG "stli_mktiocm(sigvalue=%x)\n", (int) sigvalue);
3279 #endif
3280
3281         tiocm = 0;
3282         tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
3283         tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
3284         tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
3285         tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
3286         tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
3287         tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
3288         return(tiocm);
3289 }
3290
3291 /*****************************************************************************/
3292
3293 /*
3294  *      All panels and ports actually attached have been worked out. All
3295  *      we need to do here is set up the appropriate per port data structures.
3296  */
3297
3298 static int stli_initports(stlibrd_t *brdp)
3299 {
3300         stliport_t      *portp;
3301         int             i, panelnr, panelport;
3302
3303 #ifdef DEBUG
3304         printk(KERN_DEBUG "stli_initports(brdp=%x)\n", (int) brdp);
3305 #endif
3306
3307         for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
3308                 portp = (stliport_t *) stli_memalloc(sizeof(stliport_t));
3309                 if (portp == (stliport_t *) NULL) {
3310                         printk("STALLION: failed to allocate port structure\n");
3311                         continue;
3312                 }
3313
3314                 memset(portp, 0, sizeof(stliport_t));
3315                 portp->magic = STLI_PORTMAGIC;
3316                 portp->portnr = i;
3317                 portp->brdnr = brdp->brdnr;
3318                 portp->panelnr = panelnr;
3319                 portp->baud_base = STL_BAUDBASE;
3320                 portp->close_delay = STL_CLOSEDELAY;
3321                 portp->closing_wait = 30 * HZ;
3322                 INIT_WORK(&portp->tqhangup, stli_dohangup, portp);
3323                 init_waitqueue_head(&portp->open_wait);
3324                 init_waitqueue_head(&portp->close_wait);
3325                 init_waitqueue_head(&portp->raw_wait);
3326                 panelport++;
3327                 if (panelport >= brdp->panels[panelnr]) {
3328                         panelport = 0;
3329                         panelnr++;
3330                 }
3331                 brdp->ports[i] = portp;
3332         }
3333
3334         return(0);
3335 }
3336
3337 /*****************************************************************************/
3338
3339 /*
3340  *      All the following routines are board specific hardware operations.
3341  */
3342
3343 static void stli_ecpinit(stlibrd_t *brdp)
3344 {
3345         unsigned long   memconf;
3346
3347 #ifdef DEBUG
3348         printk(KERN_DEBUG "stli_ecpinit(brdp=%d)\n", (int) brdp);
3349 #endif
3350
3351         outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
3352         udelay(10);
3353         outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3354         udelay(100);
3355
3356         memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
3357         outb(memconf, (brdp->iobase + ECP_ATMEMAR));
3358 }
3359
3360 /*****************************************************************************/
3361
3362 static void stli_ecpenable(stlibrd_t *brdp)
3363 {       
3364 #ifdef DEBUG
3365         printk(KERN_DEBUG "stli_ecpenable(brdp=%x)\n", (int) brdp);
3366 #endif
3367         outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
3368 }
3369
3370 /*****************************************************************************/
3371
3372 static void stli_ecpdisable(stlibrd_t *brdp)
3373 {       
3374 #ifdef DEBUG
3375         printk(KERN_DEBUG "stli_ecpdisable(brdp=%x)\n", (int) brdp);
3376 #endif
3377         outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3378 }
3379
3380 /*****************************************************************************/
3381
3382 static char *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3383 {       
3384         void            *ptr;
3385         unsigned char   val;
3386
3387 #ifdef DEBUG
3388         printk(KERN_DEBUG "stli_ecpgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3389                 (int) offset);
3390 #endif
3391
3392         if (offset > brdp->memsize) {
3393                 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3394                                 "range at line=%d(%d), brd=%d\n",
3395                         (int) offset, line, __LINE__, brdp->brdnr);
3396                 ptr = NULL;
3397                 val = 0;
3398         } else {
3399                 ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
3400                 val = (unsigned char) (offset / ECP_ATPAGESIZE);
3401         }
3402         outb(val, (brdp->iobase + ECP_ATMEMPR));
3403         return(ptr);
3404 }
3405
3406 /*****************************************************************************/
3407
3408 static void stli_ecpreset(stlibrd_t *brdp)
3409 {       
3410 #ifdef DEBUG
3411         printk(KERN_DEBUG "stli_ecpreset(brdp=%x)\n", (int) brdp);
3412 #endif
3413
3414         outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
3415         udelay(10);
3416         outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3417         udelay(500);
3418 }
3419
3420 /*****************************************************************************/
3421
3422 static void stli_ecpintr(stlibrd_t *brdp)
3423 {       
3424 #ifdef DEBUG
3425         printk(KERN_DEBUG "stli_ecpintr(brdp=%x)\n", (int) brdp);
3426 #endif
3427         outb(0x1, brdp->iobase);
3428 }
3429
3430 /*****************************************************************************/
3431
3432 /*
3433  *      The following set of functions act on ECP EISA boards.
3434  */
3435
3436 static void stli_ecpeiinit(stlibrd_t *brdp)
3437 {
3438         unsigned long   memconf;
3439
3440 #ifdef DEBUG
3441         printk(KERN_DEBUG "stli_ecpeiinit(brdp=%x)\n", (int) brdp);
3442 #endif
3443
3444         outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
3445         outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3446         udelay(10);
3447         outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3448         udelay(500);
3449
3450         memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
3451         outb(memconf, (brdp->iobase + ECP_EIMEMARL));
3452         memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
3453         outb(memconf, (brdp->iobase + ECP_EIMEMARH));
3454 }
3455
3456 /*****************************************************************************/
3457
3458 static void stli_ecpeienable(stlibrd_t *brdp)
3459 {       
3460         outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
3461 }
3462
3463 /*****************************************************************************/
3464
3465 static void stli_ecpeidisable(stlibrd_t *brdp)
3466 {       
3467         outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3468 }
3469
3470 /*****************************************************************************/
3471
3472 static char *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3473 {       
3474         void            *ptr;
3475         unsigned char   val;
3476
3477 #ifdef DEBUG
3478         printk(KERN_DEBUG "stli_ecpeigetmemptr(brdp=%x,offset=%x,line=%d)\n",
3479                 (int) brdp, (int) offset, line);
3480 #endif
3481
3482         if (offset > brdp->memsize) {
3483                 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3484                                 "range at line=%d(%d), brd=%d\n",
3485                         (int) offset, line, __LINE__, brdp->brdnr);
3486                 ptr = NULL;
3487                 val = 0;
3488         } else {
3489                 ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
3490                 if (offset < ECP_EIPAGESIZE)
3491                         val = ECP_EIENABLE;
3492                 else
3493                         val = ECP_EIENABLE | 0x40;
3494         }
3495         outb(val, (brdp->iobase + ECP_EICONFR));
3496         return(ptr);
3497 }
3498
3499 /*****************************************************************************/
3500
3501 static void stli_ecpeireset(stlibrd_t *brdp)
3502 {       
3503         outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3504         udelay(10);
3505         outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3506         udelay(500);
3507 }
3508
3509 /*****************************************************************************/
3510
3511 /*
3512  *      The following set of functions act on ECP MCA boards.
3513  */
3514
3515 static void stli_ecpmcenable(stlibrd_t *brdp)
3516 {       
3517         outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
3518 }
3519
3520 /*****************************************************************************/
3521
3522 static void stli_ecpmcdisable(stlibrd_t *brdp)
3523 {       
3524         outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3525 }
3526
3527 /*****************************************************************************/
3528
3529 static char *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3530 {       
3531         void            *ptr;
3532         unsigned char   val;
3533
3534         if (offset > brdp->memsize) {
3535                 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3536                                 "range at line=%d(%d), brd=%d\n",
3537                         (int) offset, line, __LINE__, brdp->brdnr);
3538                 ptr = NULL;
3539                 val = 0;
3540         } else {
3541                 ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
3542                 val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
3543         }
3544         outb(val, (brdp->iobase + ECP_MCCONFR));
3545         return(ptr);
3546 }
3547
3548 /*****************************************************************************/
3549
3550 static void stli_ecpmcreset(stlibrd_t *brdp)
3551 {       
3552         outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
3553         udelay(10);
3554         outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3555         udelay(500);
3556 }
3557
3558 /*****************************************************************************/
3559
3560 /*
3561  *      The following set of functions act on ECP PCI boards.
3562  */
3563
3564 static void stli_ecppciinit(stlibrd_t *brdp)
3565 {
3566 #ifdef DEBUG
3567         printk(KERN_DEBUG "stli_ecppciinit(brdp=%x)\n", (int) brdp);
3568 #endif
3569
3570         outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3571         udelay(10);
3572         outb(0, (brdp->iobase + ECP_PCICONFR));
3573         udelay(500);
3574 }
3575
3576 /*****************************************************************************/
3577
3578 static char *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3579 {       
3580         void            *ptr;
3581         unsigned char   val;
3582
3583 #ifdef DEBUG
3584         printk(KERN_DEBUG "stli_ecppcigetmemptr(brdp=%x,offset=%x,line=%d)\n",
3585                 (int) brdp, (int) offset, line);
3586 #endif
3587
3588         if (offset > brdp->memsize) {
3589                 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3590                                 "range at line=%d(%d), board=%d\n",
3591                                 (int) offset, line, __LINE__, brdp->brdnr);
3592                 ptr = NULL;
3593                 val = 0;
3594         } else {
3595                 ptr = brdp->membase + (offset % ECP_PCIPAGESIZE);
3596                 val = (offset / ECP_PCIPAGESIZE) << 1;
3597         }
3598         outb(val, (brdp->iobase + ECP_PCICONFR));
3599         return(ptr);
3600 }
3601
3602 /*****************************************************************************/
3603
3604 static void stli_ecppcireset(stlibrd_t *brdp)
3605 {       
3606         outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3607         udelay(10);
3608         outb(0, (brdp->iobase + ECP_PCICONFR));
3609         udelay(500);
3610 }
3611
3612 /*****************************************************************************/
3613
3614 /*
3615  *      The following routines act on ONboards.
3616  */
3617
3618 static void stli_onbinit(stlibrd_t *brdp)
3619 {
3620         unsigned long   memconf;
3621
3622 #ifdef DEBUG
3623         printk(KERN_DEBUG "stli_onbinit(brdp=%d)\n", (int) brdp);
3624 #endif
3625
3626         outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3627         udelay(10);
3628         outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3629         mdelay(1000);
3630
3631         memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
3632         outb(memconf, (brdp->iobase + ONB_ATMEMAR));
3633         outb(0x1, brdp->iobase);
3634         mdelay(1);
3635 }
3636
3637 /*****************************************************************************/
3638
3639 static void stli_onbenable(stlibrd_t *brdp)
3640 {       
3641 #ifdef DEBUG
3642         printk(KERN_DEBUG "stli_onbenable(brdp=%x)\n", (int) brdp);
3643 #endif
3644         outb((brdp->enabval | ONB_ATENABLE), (brdp->iobase + ONB_ATCONFR));
3645 }
3646
3647 /*****************************************************************************/
3648
3649 static void stli_onbdisable(stlibrd_t *brdp)
3650 {       
3651 #ifdef DEBUG
3652         printk(KERN_DEBUG "stli_onbdisable(brdp=%x)\n", (int) brdp);
3653 #endif
3654         outb((brdp->enabval | ONB_ATDISABLE), (brdp->iobase + ONB_ATCONFR));
3655 }
3656
3657 /*****************************************************************************/
3658
3659 static char *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3660 {       
3661         void    *ptr;
3662
3663 #ifdef DEBUG
3664         printk(KERN_DEBUG "stli_onbgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3665                 (int) offset);
3666 #endif
3667
3668         if (offset > brdp->memsize) {
3669                 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3670                                 "range at line=%d(%d), brd=%d\n",
3671                                 (int) offset, line, __LINE__, brdp->brdnr);
3672                 ptr = NULL;
3673         } else {
3674                 ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
3675         }
3676         return(ptr);
3677 }
3678
3679 /*****************************************************************************/
3680
3681 static void stli_onbreset(stlibrd_t *brdp)
3682 {       
3683
3684 #ifdef DEBUG
3685         printk(KERN_DEBUG "stli_onbreset(brdp=%x)\n", (int) brdp);
3686 #endif
3687
3688         outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3689         udelay(10);
3690         outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3691         mdelay(1000);
3692 }
3693
3694 /*****************************************************************************/
3695
3696 /*
3697  *      The following routines act on ONboard EISA.
3698  */
3699
3700 static void stli_onbeinit(stlibrd_t *brdp)
3701 {
3702         unsigned long   memconf;
3703
3704 #ifdef DEBUG
3705         printk(KERN_DEBUG "stli_onbeinit(brdp=%d)\n", (int) brdp);
3706 #endif
3707
3708         outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3709         outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3710         udelay(10);
3711         outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3712         mdelay(1000);
3713
3714         memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
3715         outb(memconf, (brdp->iobase + ONB_EIMEMARL));
3716         memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
3717         outb(memconf, (brdp->iobase + ONB_EIMEMARH));
3718         outb(0x1, brdp->iobase);
3719         mdelay(1);
3720 }
3721
3722 /*****************************************************************************/
3723
3724 static void stli_onbeenable(stlibrd_t *brdp)
3725 {       
3726 #ifdef DEBUG
3727         printk(KERN_DEBUG "stli_onbeenable(brdp=%x)\n", (int) brdp);
3728 #endif
3729         outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
3730 }
3731
3732 /*****************************************************************************/
3733
3734 static void stli_onbedisable(stlibrd_t *brdp)
3735 {       
3736 #ifdef DEBUG
3737         printk(KERN_DEBUG "stli_onbedisable(brdp=%x)\n", (int) brdp);
3738 #endif
3739         outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3740 }
3741
3742 /*****************************************************************************/
3743
3744 static char *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3745 {       
3746         void            *ptr;
3747         unsigned char   val;
3748
3749 #ifdef DEBUG
3750         printk(KERN_DEBUG "stli_onbegetmemptr(brdp=%x,offset=%x,line=%d)\n",
3751                 (int) brdp, (int) offset, line);
3752 #endif
3753
3754         if (offset > brdp->memsize) {
3755                 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3756                                 "range at line=%d(%d), brd=%d\n",
3757                         (int) offset, line, __LINE__, brdp->brdnr);
3758                 ptr = NULL;
3759                 val = 0;
3760         } else {
3761                 ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
3762                 if (offset < ONB_EIPAGESIZE)
3763                         val = ONB_EIENABLE;
3764                 else
3765                         val = ONB_EIENABLE | 0x40;
3766         }
3767         outb(val, (brdp->iobase + ONB_EICONFR));
3768         return(ptr);
3769 }
3770
3771 /*****************************************************************************/
3772
3773 static void stli_onbereset(stlibrd_t *brdp)
3774 {       
3775
3776 #ifdef DEBUG
3777         printk(KERN_ERR "stli_onbereset(brdp=%x)\n", (int) brdp);
3778 #endif
3779
3780         outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3781         udelay(10);
3782         outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3783         mdelay(1000);
3784 }
3785
3786 /*****************************************************************************/
3787
3788 /*
3789  *      The following routines act on Brumby boards.
3790  */
3791
3792 static void stli_bbyinit(stlibrd_t *brdp)
3793 {
3794
3795 #ifdef DEBUG
3796         printk(KERN_ERR "stli_bbyinit(brdp=%d)\n", (int) brdp);
3797 #endif
3798
3799         outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3800         udelay(10);
3801         outb(0, (brdp->iobase + BBY_ATCONFR));
3802         mdelay(1000);
3803         outb(0x1, brdp->iobase);
3804         mdelay(1);
3805 }
3806
3807 /*****************************************************************************/
3808
3809 static char *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3810 {       
3811         void            *ptr;
3812         unsigned char   val;
3813
3814 #ifdef DEBUG
3815         printk(KERN_ERR "stli_bbygetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3816                 (int) offset);
3817 #endif
3818
3819         if (offset > brdp->memsize) {
3820                 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3821                                 "range at line=%d(%d), brd=%d\n",
3822                                 (int) offset, line, __LINE__, brdp->brdnr);
3823                 ptr = NULL;
3824                 val = 0;
3825         } else {
3826                 ptr = brdp->membase + (offset % BBY_PAGESIZE);
3827                 val = (unsigned char) (offset / BBY_PAGESIZE);
3828         }
3829         outb(val, (brdp->iobase + BBY_ATCONFR));
3830         return(ptr);
3831 }
3832
3833 /*****************************************************************************/
3834
3835 static void stli_bbyreset(stlibrd_t *brdp)
3836 {       
3837
3838 #ifdef DEBUG
3839         printk(KERN_DEBUG "stli_bbyreset(brdp=%x)\n", (int) brdp);
3840 #endif
3841
3842         outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3843         udelay(10);
3844         outb(0, (brdp->iobase + BBY_ATCONFR));
3845         mdelay(1000);
3846 }
3847
3848 /*****************************************************************************/
3849
3850 /*
3851  *      The following routines act on original old Stallion boards.
3852  */
3853
3854 static void stli_stalinit(stlibrd_t *brdp)
3855 {
3856
3857 #ifdef DEBUG
3858         printk(KERN_DEBUG "stli_stalinit(brdp=%d)\n", (int) brdp);
3859 #endif
3860
3861         outb(0x1, brdp->iobase);
3862         mdelay(1000);
3863 }
3864
3865 /*****************************************************************************/
3866
3867 static char *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3868 {       
3869         void    *ptr;
3870
3871 #ifdef DEBUG
3872         printk(KERN_DEBUG "stli_stalgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3873                 (int) offset);
3874 #endif
3875
3876         if (offset > brdp->memsize) {
3877                 printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3878                                 "range at line=%d(%d), brd=%d\n",
3879                                 (int) offset, line, __LINE__, brdp->brdnr);
3880                 ptr = NULL;
3881         } else {
3882                 ptr = brdp->membase + (offset % STAL_PAGESIZE);
3883         }
3884         return(ptr);
3885 }
3886
3887 /*****************************************************************************/
3888
3889 static void stli_stalreset(stlibrd_t *brdp)
3890 {       
3891         volatile unsigned long  *vecp;
3892
3893 #ifdef DEBUG
3894         printk(KERN_DEBUG "stli_stalreset(brdp=%x)\n", (int) brdp);
3895 #endif
3896
3897         vecp = (volatile unsigned long *) (brdp->membase + 0x30);
3898         *vecp = 0xffff0000;
3899         outb(0, brdp->iobase);
3900         mdelay(1000);
3901 }
3902
3903 /*****************************************************************************/
3904
3905 /*
3906  *      Try to find an ECP board and initialize it. This handles only ECP
3907  *      board types.
3908  */
3909
3910 static int stli_initecp(stlibrd_t *brdp)
3911 {
3912         cdkecpsig_t     sig;
3913         cdkecpsig_t     *sigsp;
3914         unsigned int    status, nxtid;
3915         char            *name;
3916         int             panelnr, nrports;
3917
3918 #ifdef DEBUG
3919         printk(KERN_DEBUG "stli_initecp(brdp=%x)\n", (int) brdp);
3920 #endif
3921
3922         if (!request_region(brdp->iobase, brdp->iosize, "istallion"))
3923                 return -EIO;
3924         
3925         if ((brdp->iobase == 0) || (brdp->memaddr == 0))
3926         {
3927                 release_region(brdp->iobase, brdp->iosize);
3928                 return(-ENODEV);
3929         }
3930
3931         brdp->iosize = ECP_IOSIZE;
3932
3933 /*
3934  *      Based on the specific board type setup the common vars to access
3935  *      and enable shared memory. Set all board specific information now
3936  *      as well.
3937  */
3938         switch (brdp->brdtype) {
3939         case BRD_ECP:
3940                 brdp->membase = (void *) brdp->memaddr;
3941                 brdp->memsize = ECP_MEMSIZE;
3942                 brdp->pagesize = ECP_ATPAGESIZE;
3943                 brdp->init = stli_ecpinit;
3944                 brdp->enable = stli_ecpenable;
3945                 brdp->reenable = stli_ecpenable;
3946                 brdp->disable = stli_ecpdisable;
3947                 brdp->getmemptr = stli_ecpgetmemptr;
3948                 brdp->intr = stli_ecpintr;
3949                 brdp->reset = stli_ecpreset;
3950                 name = "serial(EC8/64)";
3951                 break;
3952
3953         case BRD_ECPE:
3954                 brdp->membase = (void *) brdp->memaddr;
3955                 brdp->memsize = ECP_MEMSIZE;
3956                 brdp->pagesize = ECP_EIPAGESIZE;
3957                 brdp->init = stli_ecpeiinit;
3958                 brdp->enable = stli_ecpeienable;
3959                 brdp->reenable = stli_ecpeienable;
3960                 brdp->disable = stli_ecpeidisable;
3961                 brdp->getmemptr = stli_ecpeigetmemptr;
3962                 brdp->intr = stli_ecpintr;
3963                 brdp->reset = stli_ecpeireset;
3964                 name = "serial(EC8/64-EI)";
3965                 break;
3966
3967         case BRD_ECPMC:
3968                 brdp->membase = (void *) brdp->memaddr;
3969                 brdp->memsize = ECP_MEMSIZE;
3970                 brdp->pagesize = ECP_MCPAGESIZE;
3971                 brdp->init = NULL;
3972                 brdp->enable = stli_ecpmcenable;
3973                 brdp->reenable = stli_ecpmcenable;
3974                 brdp->disable = stli_ecpmcdisable;
3975                 brdp->getmemptr = stli_ecpmcgetmemptr;
3976                 brdp->intr = stli_ecpintr;
3977                 brdp->reset = stli_ecpmcreset;
3978                 name = "serial(EC8/64-MCA)";
3979                 break;
3980
3981         case BRD_ECPPCI:
3982                 brdp->membase = (void *) brdp->memaddr;
3983                 brdp->memsize = ECP_PCIMEMSIZE;
3984                 brdp->pagesize = ECP_PCIPAGESIZE;
3985                 brdp->init = stli_ecppciinit;
3986                 brdp->enable = NULL;
3987                 brdp->reenable = NULL;
3988                 brdp->disable = NULL;
3989                 brdp->getmemptr = stli_ecppcigetmemptr;
3990                 brdp->intr = stli_ecpintr;
3991                 brdp->reset = stli_ecppcireset;
3992                 name = "serial(EC/RA-PCI)";
3993                 break;
3994
3995         default:
3996                 release_region(brdp->iobase, brdp->iosize);
3997                 return(-EINVAL);
3998         }
3999
4000 /*
4001  *      The per-board operations structure is all set up, so now let's go
4002  *      and get the board operational. Firstly initialize board configuration
4003  *      registers. Set the memory mapping info so we can get at the boards
4004  *      shared memory.
4005  */
4006         EBRDINIT(brdp);
4007
4008         brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4009         if (brdp->membase == (void *) NULL)
4010         {
4011                 release_region(brdp->iobase, brdp->iosize);
4012                 return(-ENOMEM);
4013         }
4014
4015 /*
4016  *      Now that all specific code is set up, enable the shared memory and
4017  *      look for the a signature area that will tell us exactly what board
4018  *      this is, and what it is connected to it.
4019  */
4020         EBRDENABLE(brdp);
4021         sigsp = (cdkecpsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
4022         memcpy(&sig, sigsp, sizeof(cdkecpsig_t));
4023         EBRDDISABLE(brdp);
4024
4025 #if 0
4026         printk("%s(%d): sig-> magic=%x rom=%x panel=%x,%x,%x,%x,%x,%x,%x,%x\n",
4027                 __FILE__, __LINE__, (int) sig.magic, sig.romver, sig.panelid[0],
4028                 (int) sig.panelid[1], (int) sig.panelid[2],
4029                 (int) sig.panelid[3], (int) sig.panelid[4],
4030                 (int) sig.panelid[5], (int) sig.panelid[6],
4031                 (int) sig.panelid[7]);
4032 #endif
4033
4034         if (sig.magic != ECP_MAGIC)
4035         {
4036                 release_region(brdp->iobase, brdp->iosize);
4037                 return(-ENODEV);
4038         }
4039
4040 /*
4041  *      Scan through the signature looking at the panels connected to the
4042  *      board. Calculate the total number of ports as we go.
4043  */
4044         for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
4045                 status = sig.panelid[nxtid];
4046                 if ((status & ECH_PNLIDMASK) != nxtid)
4047                         break;
4048
4049                 brdp->panelids[panelnr] = status;
4050                 nrports = (status & ECH_PNL16PORT) ? 16 : 8;
4051                 if ((nrports == 16) && ((status & ECH_PNLXPID) == 0))
4052                         nxtid++;
4053                 brdp->panels[panelnr] = nrports;
4054                 brdp->nrports += nrports;
4055                 nxtid++;
4056                 brdp->nrpanels++;
4057         }
4058
4059
4060         brdp->state |= BST_FOUND;
4061         return(0);
4062 }
4063
4064 /*****************************************************************************/
4065
4066 /*
4067  *      Try to find an ONboard, Brumby or Stallion board and initialize it.
4068  *      This handles only these board types.
4069  */
4070
4071 static int stli_initonb(stlibrd_t *brdp)
4072 {
4073         cdkonbsig_t     sig;
4074         cdkonbsig_t     *sigsp;
4075         char            *name;
4076         int             i;
4077
4078 #ifdef DEBUG
4079         printk(KERN_DEBUG "stli_initonb(brdp=%x)\n", (int) brdp);
4080 #endif
4081
4082 /*
4083  *      Do a basic sanity check on the IO and memory addresses.
4084  */
4085         if ((brdp->iobase == 0) || (brdp->memaddr == 0))
4086                 return(-ENODEV);
4087
4088         brdp->iosize = ONB_IOSIZE;
4089         
4090         if (!request_region(brdp->iobase, brdp->iosize, "istallion"))
4091                 return -EIO;
4092
4093 /*
4094  *      Based on the specific board type setup the common vars to access
4095  *      and enable shared memory. Set all board specific information now
4096  *      as well.
4097  */
4098         switch (brdp->brdtype) {
4099         case BRD_ONBOARD:
4100         case BRD_ONBOARD32:
4101         case BRD_ONBOARD2:
4102         case BRD_ONBOARD2_32:
4103         case BRD_ONBOARDRS:
4104                 brdp->membase = (void *) brdp->memaddr;
4105                 brdp->memsize = ONB_MEMSIZE;
4106                 brdp->pagesize = ONB_ATPAGESIZE;
4107                 brdp->init = stli_onbinit;
4108                 brdp->enable = stli_onbenable;
4109                 brdp->reenable = stli_onbenable;
4110                 brdp->disable = stli_onbdisable;
4111                 brdp->getmemptr = stli_onbgetmemptr;
4112                 brdp->intr = stli_ecpintr;
4113                 brdp->reset = stli_onbreset;
4114                 if (brdp->memaddr > 0x100000)
4115                         brdp->enabval = ONB_MEMENABHI;
4116                 else
4117                         brdp->enabval = ONB_MEMENABLO;
4118                 name = "serial(ONBoard)";
4119                 break;
4120
4121         case BRD_ONBOARDE:
4122                 brdp->membase = (void *) brdp->memaddr;
4123                 brdp->memsize = ONB_EIMEMSIZE;
4124                 brdp->pagesize = ONB_EIPAGESIZE;
4125                 brdp->init = stli_onbeinit;
4126                 brdp->enable = stli_onbeenable;
4127                 brdp->reenable = stli_onbeenable;
4128                 brdp->disable = stli_onbedisable;
4129                 brdp->getmemptr = stli_onbegetmemptr;
4130                 brdp->intr = stli_ecpintr;
4131                 brdp->reset = stli_onbereset;
4132                 name = "serial(ONBoard/E)";
4133                 break;
4134
4135         case BRD_BRUMBY4:
4136         case BRD_BRUMBY8:
4137         case BRD_BRUMBY16:
4138                 brdp->membase = (void *) brdp->memaddr;
4139                 brdp->memsize = BBY_MEMSIZE;
4140                 brdp->pagesize = BBY_PAGESIZE;
4141                 brdp->init = stli_bbyinit;
4142                 brdp->enable = NULL;
4143                 brdp->reenable = NULL;
4144                 brdp->disable = NULL;
4145                 brdp->getmemptr = stli_bbygetmemptr;
4146                 brdp->intr = stli_ecpintr;
4147                 brdp->reset = stli_bbyreset;
4148                 name = "serial(Brumby)";
4149                 break;
4150
4151         case BRD_STALLION:
4152                 brdp->membase = (void *) brdp->memaddr;
4153                 brdp->memsize = STAL_MEMSIZE;
4154                 brdp->pagesize = STAL_PAGESIZE;
4155                 brdp->init = stli_stalinit;
4156                 brdp->enable = NULL;
4157                 brdp->reenable = NULL;
4158                 brdp->disable = NULL;
4159                 brdp->getmemptr = stli_stalgetmemptr;
4160                 brdp->intr = stli_ecpintr;
4161                 brdp->reset = stli_stalreset;
4162                 name = "serial(Stallion)";
4163                 break;
4164
4165         default:
4166                 release_region(brdp->iobase, brdp->iosize);
4167                 return(-EINVAL);
4168         }
4169
4170 /*
4171  *      The per-board operations structure is all set up, so now let's go
4172  *      and get the board operational. Firstly initialize board configuration
4173  *      registers. Set the memory mapping info so we can get at the boards
4174  *      shared memory.
4175  */
4176         EBRDINIT(brdp);
4177
4178         brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4179         if (brdp->membase == (void *) NULL)
4180         {
4181                 release_region(brdp->iobase, brdp->iosize);
4182                 return(-ENOMEM);
4183         }
4184
4185 /*
4186  *      Now that all specific code is set up, enable the shared memory and
4187  *      look for the a signature area that will tell us exactly what board
4188  *      this is, and how many ports.
4189  */
4190         EBRDENABLE(brdp);
4191         sigsp = (cdkonbsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
4192         memcpy(&sig, sigsp, sizeof(cdkonbsig_t));
4193         EBRDDISABLE(brdp);
4194
4195 #if 0
4196         printk("%s(%d): sig-> magic=%x:%x:%x:%x romver=%x amask=%x:%x:%x\n",
4197                 __FILE__, __LINE__, sig.magic0, sig.magic1, sig.magic2,
4198                 sig.magic3, sig.romver, sig.amask0, sig.amask1, sig.amask2);
4199 #endif
4200
4201         if ((sig.magic0 != ONB_MAGIC0) || (sig.magic1 != ONB_MAGIC1) ||
4202             (sig.magic2 != ONB_MAGIC2) || (sig.magic3 != ONB_MAGIC3))
4203         {
4204                 release_region(brdp->iobase, brdp->iosize);
4205                 return(-ENODEV);
4206         }
4207
4208 /*
4209  *      Scan through the signature alive mask and calculate how many ports
4210  *      there are on this board.
4211  */
4212         brdp->nrpanels = 1;
4213         if (sig.amask1) {
4214                 brdp->nrports = 32;
4215         } else {
4216                 for (i = 0; (i < 16); i++) {
4217                         if (((sig.amask0 << i) & 0x8000) == 0)
4218                                 break;
4219                 }
4220                 brdp->nrports = i;
4221         }
4222         brdp->panels[0] = brdp->nrports;
4223
4224
4225         brdp->state |= BST_FOUND;
4226         return(0);
4227 }
4228
4229 /*****************************************************************************/
4230
4231 /*
4232  *      Start up a running board. This routine is only called after the
4233  *      code has been down loaded to the board and is operational. It will
4234  *      read in the memory map, and get the show on the road...
4235  */
4236
4237 static int stli_startbrd(stlibrd_t *brdp)
4238 {
4239         volatile cdkhdr_t       *hdrp;
4240         volatile cdkmem_t       *memp;
4241         volatile cdkasy_t       *ap;
4242         unsigned long           flags;
4243         stliport_t              *portp;
4244         int                     portnr, nrdevs, i, rc;
4245
4246 #ifdef DEBUG
4247         printk(KERN_DEBUG "stli_startbrd(brdp=%x)\n", (int) brdp);
4248 #endif
4249
4250         rc = 0;
4251
4252         save_flags(flags);
4253         cli();
4254         EBRDENABLE(brdp);
4255         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
4256         nrdevs = hdrp->nrdevs;
4257
4258 #if 0
4259         printk("%s(%d): CDK version %d.%d.%d --> "
4260                 "nrdevs=%d memp=%x hostp=%x slavep=%x\n",
4261                  __FILE__, __LINE__, hdrp->ver_release, hdrp->ver_modification,
4262                  hdrp->ver_fix, nrdevs, (int) hdrp->memp, (int) hdrp->hostp,
4263                  (int) hdrp->slavep);
4264 #endif
4265
4266         if (nrdevs < (brdp->nrports + 1)) {
4267                 printk(KERN_ERR "STALLION: slave failed to allocate memory for "
4268                                 "all devices, devices=%d\n", nrdevs);
4269                 brdp->nrports = nrdevs - 1;
4270         }
4271         brdp->nrdevs = nrdevs;
4272         brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
4273         brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
4274         brdp->bitsize = (nrdevs + 7) / 8;
4275         memp = (volatile cdkmem_t *) hdrp->memp;
4276         if (((unsigned long) memp) > brdp->memsize) {
4277                 printk(KERN_ERR "STALLION: corrupted shared memory region?\n");
4278                 rc = -EIO;
4279                 goto stli_donestartup;
4280         }
4281         memp = (volatile cdkmem_t *) EBRDGETMEMPTR(brdp, (unsigned long) memp);
4282         if (memp->dtype != TYP_ASYNCTRL) {
4283                 printk(KERN_ERR "STALLION: no slave control device found\n");
4284                 goto stli_donestartup;
4285         }
4286         memp++;
4287
4288 /*
4289  *      Cycle through memory allocation of each port. We are guaranteed to
4290  *      have all ports inside the first page of slave window, so no need to
4291  *      change pages while reading memory map.
4292  */
4293         for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
4294                 if (memp->dtype != TYP_ASYNC)
4295                         break;
4296                 portp = brdp->ports[portnr];
4297                 if (portp == (stliport_t *) NULL)
4298                         break;
4299                 portp->devnr = i;
4300                 portp->addr = memp->offset;
4301                 portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
4302                 portp->portidx = (unsigned char) (i / 8);
4303                 portp->portbit = (unsigned char) (0x1 << (i % 8));
4304         }
4305
4306         hdrp->slavereq = 0xff;
4307
4308 /*
4309  *      For each port setup a local copy of the RX and TX buffer offsets
4310  *      and sizes. We do this separate from the above, because we need to
4311  *      move the shared memory page...
4312  */
4313         for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
4314                 portp = brdp->ports[portnr];
4315                 if (portp == (stliport_t *) NULL)
4316                         break;
4317                 if (portp->addr == 0)
4318                         break;
4319                 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
4320                 if (ap != (volatile cdkasy_t *) NULL) {
4321                         portp->rxsize = ap->rxq.size;
4322                         portp->txsize = ap->txq.size;
4323                         portp->rxoffset = ap->rxq.offset;
4324                         portp->txoffset = ap->txq.offset;
4325                 }
4326         }
4327
4328 stli_donestartup:
4329         EBRDDISABLE(brdp);
4330         restore_flags(flags);
4331
4332         if (rc == 0)
4333                 brdp->state |= BST_STARTED;
4334
4335         if (! stli_timeron) {
4336                 stli_timeron++;
4337                 stli_timerlist.expires = STLI_TIMEOUT;
4338                 add_timer(&stli_timerlist);
4339         }
4340
4341         return(rc);
4342 }
4343
4344 /*****************************************************************************/
4345
4346 /*
4347  *      Probe and initialize the specified board.
4348  */
4349
4350 static int __init stli_brdinit(stlibrd_t *brdp)
4351 {
4352 #ifdef DEBUG
4353         printk(KERN_DEBUG "stli_brdinit(brdp=%x)\n", (int) brdp);
4354 #endif
4355
4356         stli_brds[brdp->brdnr] = brdp;
4357
4358         switch (brdp->brdtype) {
4359         case BRD_ECP:
4360         case BRD_ECPE:
4361         case BRD_ECPMC:
4362         case BRD_ECPPCI:
4363                 stli_initecp(brdp);
4364                 break;
4365         case BRD_ONBOARD:
4366         case BRD_ONBOARDE:
4367         case BRD_ONBOARD2:
4368         case BRD_ONBOARD32:
4369         case BRD_ONBOARD2_32:
4370         case BRD_ONBOARDRS:
4371         case BRD_BRUMBY4:
4372         case BRD_BRUMBY8:
4373         case BRD_BRUMBY16:
4374         case BRD_STALLION:
4375                 stli_initonb(brdp);
4376                 break;
4377         case BRD_EASYIO:
4378         case BRD_ECH:
4379         case BRD_ECHMC:
4380         case BRD_ECHPCI:
4381                 printk(KERN_ERR "STALLION: %s board type not supported in "
4382                                 "this driver\n", stli_brdnames[brdp->brdtype]);
4383                 return(ENODEV);
4384         default:
4385                 printk(KERN_ERR "STALLION: board=%d is unknown board "
4386                                 "type=%d\n", brdp->brdnr, brdp->brdtype);
4387                 return(ENODEV);
4388         }
4389
4390         if ((brdp->state & BST_FOUND) == 0) {
4391                 printk(KERN_ERR "STALLION: %s board not found, board=%d "
4392                                 "io=%x mem=%x\n",
4393                         stli_brdnames[brdp->brdtype], brdp->brdnr,
4394                         brdp->iobase, (int) brdp->memaddr);
4395                 return(ENODEV);
4396         }
4397
4398         stli_initports(brdp);
4399         printk(KERN_INFO "STALLION: %s found, board=%d io=%x mem=%x "
4400                 "nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype],
4401                 brdp->brdnr, brdp->iobase, (int) brdp->memaddr,
4402                 brdp->nrpanels, brdp->nrports);
4403         return(0);
4404 }
4405
4406 /*****************************************************************************/
4407
4408 /*
4409  *      Probe around trying to find where the EISA boards shared memory
4410  *      might be. This is a bit if hack, but it is the best we can do.
4411  */
4412
4413 static int stli_eisamemprobe(stlibrd_t *brdp)
4414 {
4415         cdkecpsig_t     ecpsig, *ecpsigp;
4416         cdkonbsig_t     onbsig, *onbsigp;
4417         int             i, foundit;
4418
4419 #ifdef DEBUG
4420         printk(KERN_DEBUG "stli_eisamemprobe(brdp=%x)\n", (int) brdp);
4421 #endif
4422
4423 /*
4424  *      First up we reset the board, to get it into a known state. There
4425  *      is only 2 board types here we need to worry about. Don;t use the
4426  *      standard board init routine here, it programs up the shared
4427  *      memory address, and we don't know it yet...
4428  */
4429         if (brdp->brdtype == BRD_ECPE) {
4430                 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
4431                 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
4432                 udelay(10);
4433                 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
4434                 udelay(500);
4435                 stli_ecpeienable(brdp);
4436         } else if (brdp->brdtype == BRD_ONBOARDE) {
4437                 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
4438                 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
4439                 udelay(10);
4440                 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
4441                 mdelay(100);
4442                 outb(0x1, brdp->iobase);
4443                 mdelay(1);
4444                 stli_onbeenable(brdp);
4445         } else {
4446                 return(-ENODEV);
4447         }
4448
4449         foundit = 0;
4450         brdp->memsize = ECP_MEMSIZE;
4451
4452 /*
4453  *      Board shared memory is enabled, so now we have a poke around and
4454  *      see if we can find it.
4455  */
4456         for (i = 0; (i < stli_eisamempsize); i++) {
4457                 brdp->memaddr = stli_eisamemprobeaddrs[i];
4458                 brdp->membase = (void *) brdp->memaddr;
4459                 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4460                 if (brdp->membase == (void *) NULL)
4461                         continue;
4462
4463                 if (brdp->brdtype == BRD_ECPE) {
4464                         ecpsigp = (cdkecpsig_t *) stli_ecpeigetmemptr(brdp,
4465                                 CDK_SIGADDR, __LINE__);
4466                         memcpy(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
4467                         if (ecpsig.magic == ECP_MAGIC)
4468                                 foundit = 1;
4469                 } else {
4470                         onbsigp = (cdkonbsig_t *) stli_onbegetmemptr(brdp,
4471                                 CDK_SIGADDR, __LINE__);
4472                         memcpy(&onbsig, onbsigp, sizeof(cdkonbsig_t));
4473                         if ((onbsig.magic0 == ONB_MAGIC0) &&
4474                             (onbsig.magic1 == ONB_MAGIC1) &&
4475                             (onbsig.magic2 == ONB_MAGIC2) &&
4476                             (onbsig.magic3 == ONB_MAGIC3))
4477                                 foundit = 1;
4478                 }
4479
4480                 iounmap(brdp->membase);
4481                 if (foundit)
4482                         break;
4483         }
4484
4485 /*
4486  *      Regardless of whether we found the shared memory or not we must
4487  *      disable the region. After that return success or failure.
4488  */
4489         if (brdp->brdtype == BRD_ECPE)
4490                 stli_ecpeidisable(brdp);
4491         else
4492                 stli_onbedisable(brdp);
4493
4494         if (! foundit) {
4495                 brdp->memaddr = 0;
4496                 brdp->membase = NULL;
4497                 printk(KERN_ERR "STALLION: failed to probe shared memory "
4498                                 "region for %s in EISA slot=%d\n",
4499                         stli_brdnames[brdp->brdtype], (brdp->iobase >> 12));
4500                 return(-ENODEV);
4501         }
4502         return(0);
4503 }
4504
4505 static int stli_getbrdnr(void)
4506 {
4507         int i;
4508
4509         for (i = 0; i < STL_MAXBRDS; i++) {
4510                 if (!stli_brds[i]) {
4511                         if (i >= stli_nrbrds)
4512                                 stli_nrbrds = i + 1;
4513                         return i;
4514                 }
4515         }
4516         return -1;
4517 }
4518
4519 /*****************************************************************************/
4520
4521 /*
4522  *      Probe around and try to find any EISA boards in system. The biggest
4523  *      problem here is finding out what memory address is associated with
4524  *      an EISA board after it is found. The registers of the ECPE and
4525  *      ONboardE are not readable - so we can't read them from there. We
4526  *      don't have access to the EISA CMOS (or EISA BIOS) so we don't
4527  *      actually have any way to find out the real value. The best we can
4528  *      do is go probing around in the usual places hoping we can find it.
4529  */
4530
4531 static int stli_findeisabrds(void)
4532 {
4533         stlibrd_t       *brdp;
4534         unsigned int    iobase, eid;
4535         int             i;
4536
4537 #ifdef DEBUG
4538         printk(KERN_DEBUG "stli_findeisabrds()\n");
4539 #endif
4540
4541 /*
4542  *      Firstly check if this is an EISA system. Do this by probing for
4543  *      the system board EISA ID. If this is not an EISA system then
4544  *      don't bother going any further!
4545  */
4546         outb(0xff, 0xc80);
4547         if (inb(0xc80) == 0xff)
4548                 return(0);
4549
4550 /*
4551  *      Looks like an EISA system, so go searching for EISA boards.
4552  */
4553         for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
4554                 outb(0xff, (iobase + 0xc80));
4555                 eid = inb(iobase + 0xc80);
4556                 eid |= inb(iobase + 0xc81) << 8;
4557                 if (eid != STL_EISAID)
4558                         continue;
4559
4560 /*
4561  *              We have found a board. Need to check if this board was
4562  *              statically configured already (just in case!).
4563  */
4564                 for (i = 0; (i < STL_MAXBRDS); i++) {
4565                         brdp = stli_brds[i];
4566                         if (brdp == (stlibrd_t *) NULL)
4567                                 continue;
4568                         if (brdp->iobase == iobase)
4569                                 break;
4570                 }
4571                 if (i < STL_MAXBRDS)
4572                         continue;
4573
4574 /*
4575  *              We have found a Stallion board and it is not configured already.
4576  *              Allocate a board structure and initialize it.
4577  */
4578                 if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4579                         return(-ENOMEM);
4580                 if ((brdp->brdnr = stli_getbrdnr()) < 0)
4581                         return(-ENOMEM);
4582                 eid = inb(iobase + 0xc82);
4583                 if (eid == ECP_EISAID)
4584                         brdp->brdtype = BRD_ECPE;
4585                 else if (eid == ONB_EISAID)
4586                         brdp->brdtype = BRD_ONBOARDE;
4587                 else
4588                         brdp->brdtype = BRD_UNKNOWN;
4589                 brdp->iobase = iobase;
4590                 outb(0x1, (iobase + 0xc84));
4591                 if (stli_eisamemprobe(brdp))
4592                         outb(0, (iobase + 0xc84));
4593                 stli_brdinit(brdp);
4594         }
4595
4596         return(0);
4597 }
4598
4599 /*****************************************************************************/
4600
4601 /*
4602  *      Find the next available board number that is free.
4603  */
4604
4605 /*****************************************************************************/
4606
4607 #ifdef  CONFIG_PCI
4608
4609 /*
4610  *      We have a Stallion board. Allocate a board structure and
4611  *      initialize it. Read its IO and MEMORY resources from PCI
4612  *      configuration space.
4613  */
4614
4615 static int stli_initpcibrd(int brdtype, struct pci_dev *devp)
4616 {
4617         stlibrd_t       *brdp;
4618
4619 #ifdef DEBUG
4620         printk(KERN_DEBUG "stli_initpcibrd(brdtype=%d,busnr=%x,devnr=%x)\n",
4621                 brdtype, dev->bus->number, dev->devfn);
4622 #endif
4623
4624         if (pci_enable_device(devp))
4625                 return(-EIO);
4626         if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4627                 return(-ENOMEM);
4628         if ((brdp->brdnr = stli_getbrdnr()) < 0) {
4629                 printk(KERN_INFO "STALLION: too many boards found, "
4630                         "maximum supported %d\n", STL_MAXBRDS);
4631                 return(0);
4632         }
4633         brdp->brdtype = brdtype;
4634
4635 #ifdef DEBUG
4636         printk(KERN_DEBUG "%s(%d): BAR[]=%lx,%lx,%lx,%lx\n", __FILE__, __LINE__,
4637                 pci_resource_start(devp, 0),
4638                 pci_resource_start(devp, 1),
4639                 pci_resource_start(devp, 2),
4640                 pci_resource_start(devp, 3));
4641 #endif
4642
4643 /*
4644  *      We have all resources from the board, so lets setup the actual
4645  *      board structure now.
4646  */
4647         brdp->iobase = pci_resource_start(devp, 3);
4648         brdp->memaddr = pci_resource_start(devp, 2);
4649         stli_brdinit(brdp);
4650
4651         return(0);
4652 }
4653
4654 /*****************************************************************************/
4655
4656 /*
4657  *      Find all Stallion PCI boards that might be installed. Initialize each
4658  *      one as it is found.
4659  */
4660
4661 static int stli_findpcibrds(void)
4662 {
4663         struct pci_dev  *dev = NULL;
4664         int             rc;
4665
4666 #ifdef DEBUG
4667         printk("stli_findpcibrds()\n");
4668 #endif
4669
4670         while ((dev = pci_find_device(PCI_VENDOR_ID_STALLION,
4671             PCI_DEVICE_ID_ECRA, dev))) {
4672                 if ((rc = stli_initpcibrd(BRD_ECPPCI, dev)))
4673                         return(rc);
4674         }
4675
4676         return(0);
4677 }
4678
4679 #endif
4680
4681 /*****************************************************************************/
4682
4683 /*
4684  *      Allocate a new board structure. Fill out the basic info in it.
4685  */
4686
4687 static stlibrd_t *stli_allocbrd(void)
4688 {
4689         stlibrd_t       *brdp;
4690
4691         brdp = (stlibrd_t *) stli_memalloc(sizeof(stlibrd_t));
4692         if (brdp == (stlibrd_t *) NULL) {
4693                 printk(KERN_ERR "STALLION: failed to allocate memory "
4694                                 "(size=%d)\n", sizeof(stlibrd_t));
4695                 return((stlibrd_t *) NULL);
4696         }
4697
4698         memset(brdp, 0, sizeof(stlibrd_t));
4699         brdp->magic = STLI_BOARDMAGIC;
4700         return(brdp);
4701 }
4702
4703 /*****************************************************************************/
4704
4705 /*
4706  *      Scan through all the boards in the configuration and see what we
4707  *      can find.
4708  */
4709
4710 static int stli_initbrds(void)
4711 {
4712         stlibrd_t       *brdp, *nxtbrdp;
4713         stlconf_t       *confp;
4714         int             i, j;
4715
4716 #ifdef DEBUG
4717         printk(KERN_DEBUG "stli_initbrds()\n");
4718 #endif
4719
4720         if (stli_nrbrds > STL_MAXBRDS) {
4721                 printk(KERN_INFO "STALLION: too many boards in configuration "
4722                         "table, truncating to %d\n", STL_MAXBRDS);
4723                 stli_nrbrds = STL_MAXBRDS;
4724         }
4725
4726 /*
4727  *      Firstly scan the list of static boards configured. Allocate
4728  *      resources and initialize the boards as found. If this is a
4729  *      module then let the module args override static configuration.
4730  */
4731         for (i = 0; (i < stli_nrbrds); i++) {
4732                 confp = &stli_brdconf[i];
4733 #ifdef MODULE
4734                 stli_parsebrd(confp, stli_brdsp[i]);
4735 #endif
4736                 if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4737                         return(-ENOMEM);
4738                 brdp->brdnr = i;
4739                 brdp->brdtype = confp->brdtype;
4740                 brdp->iobase = confp->ioaddr1;
4741                 brdp->memaddr = confp->memaddr;
4742                 stli_brdinit(brdp);
4743         }
4744
4745 /*
4746  *      Static configuration table done, so now use dynamic methods to
4747  *      see if any more boards should be configured.
4748  */
4749 #ifdef MODULE
4750         stli_argbrds();
4751 #endif
4752         if (stli_eisaprobe)
4753                 stli_findeisabrds();
4754 #ifdef CONFIG_PCI
4755         stli_findpcibrds();
4756 #endif
4757
4758 /*
4759  *      All found boards are initialized. Now for a little optimization, if
4760  *      no boards are sharing the "shared memory" regions then we can just
4761  *      leave them all enabled. This is in fact the usual case.
4762  */
4763         stli_shared = 0;
4764         if (stli_nrbrds > 1) {
4765                 for (i = 0; (i < stli_nrbrds); i++) {
4766                         brdp = stli_brds[i];
4767                         if (brdp == (stlibrd_t *) NULL)
4768                                 continue;
4769                         for (j = i + 1; (j < stli_nrbrds); j++) {
4770                                 nxtbrdp = stli_brds[j];
4771                                 if (nxtbrdp == (stlibrd_t *) NULL)
4772                                         continue;
4773                                 if ((brdp->membase >= nxtbrdp->membase) &&
4774                                     (brdp->membase <= (nxtbrdp->membase +
4775                                     nxtbrdp->memsize - 1))) {
4776                                         stli_shared++;
4777                                         break;
4778                                 }
4779                         }
4780                 }
4781         }
4782
4783         if (stli_shared == 0) {
4784                 for (i = 0; (i < stli_nrbrds); i++) {
4785                         brdp = stli_brds[i];
4786                         if (brdp == (stlibrd_t *) NULL)
4787                                 continue;
4788                         if (brdp->state & BST_FOUND) {
4789                                 EBRDENABLE(brdp);
4790                                 brdp->enable = NULL;
4791                                 brdp->disable = NULL;
4792                         }
4793                 }
4794         }
4795
4796         return(0);
4797 }
4798
4799 /*****************************************************************************/
4800
4801 /*
4802  *      Code to handle an "staliomem" read operation. This device is the 
4803  *      contents of the board shared memory. It is used for down loading
4804  *      the slave image (and debugging :-)
4805  */
4806
4807 static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp)
4808 {
4809         unsigned long   flags;
4810         void            *memptr;
4811         stlibrd_t       *brdp;
4812         int             brdnr, size, n;
4813
4814 #ifdef DEBUG
4815         printk(KERN_DEBUG "stli_memread(fp=%x,buf=%x,count=%x,offp=%x)\n",
4816                         (int) fp, (int) buf, count, (int) offp);
4817 #endif
4818
4819         brdnr = iminor(fp->f_dentry->d_inode);
4820         if (brdnr >= stli_nrbrds)
4821                 return(-ENODEV);
4822         brdp = stli_brds[brdnr];
4823         if (brdp == (stlibrd_t *) NULL)
4824                 return(-ENODEV);
4825         if (brdp->state == 0)
4826                 return(-ENODEV);
4827         if (fp->f_pos >= brdp->memsize)
4828                 return(0);
4829
4830         size = MIN(count, (brdp->memsize - fp->f_pos));
4831
4832         save_flags(flags);
4833         cli();
4834         EBRDENABLE(brdp);
4835         while (size > 0) {
4836                 memptr = (void *) EBRDGETMEMPTR(brdp, fp->f_pos);
4837                 n = MIN(size, (brdp->pagesize - (((unsigned long) fp->f_pos) % brdp->pagesize)));
4838                 if (copy_to_user(buf, memptr, n)) {
4839                         count = -EFAULT;
4840                         goto out;
4841                 }
4842                 fp->f_pos += n;
4843                 buf += n;
4844                 size -= n;
4845         }
4846 out:
4847         EBRDDISABLE(brdp);
4848         restore_flags(flags);
4849
4850         return(count);
4851 }
4852
4853 /*****************************************************************************/
4854
4855 /*
4856  *      Code to handle an "staliomem" write operation. This device is the 
4857  *      contents of the board shared memory. It is used for down loading
4858  *      the slave image (and debugging :-)
4859  */
4860
4861 static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp)
4862 {
4863         unsigned long   flags;
4864         void            *memptr;
4865         stlibrd_t       *brdp;
4866         char            __user *chbuf;
4867         int             brdnr, size, n;
4868
4869 #ifdef DEBUG
4870         printk(KERN_DEBUG "stli_memwrite(fp=%x,buf=%x,count=%x,offp=%x)\n",
4871                         (int) fp, (int) buf, count, (int) offp);
4872 #endif
4873
4874         brdnr = iminor(fp->f_dentry->d_inode);
4875         if (brdnr >= stli_nrbrds)
4876                 return(-ENODEV);
4877         brdp = stli_brds[brdnr];
4878         if (brdp == (stlibrd_t *) NULL)
4879                 return(-ENODEV);
4880         if (brdp->state == 0)
4881                 return(-ENODEV);
4882         if (fp->f_pos >= brdp->memsize)
4883                 return(0);
4884
4885         chbuf = (char __user *) buf;
4886         size = MIN(count, (brdp->memsize - fp->f_pos));
4887
4888         save_flags(flags);
4889         cli();
4890         EBRDENABLE(brdp);
4891         while (size > 0) {
4892                 memptr = (void *) EBRDGETMEMPTR(brdp, fp->f_pos);
4893                 n = MIN(size, (brdp->pagesize - (((unsigned long) fp->f_pos) % brdp->pagesize)));
4894                 if (copy_from_user(memptr, chbuf, n)) {
4895                         count = -EFAULT;
4896                         goto out;
4897                 }
4898                 fp->f_pos += n;
4899                 chbuf += n;
4900                 size -= n;
4901         }
4902 out:
4903         EBRDDISABLE(brdp);
4904         restore_flags(flags);
4905
4906         return(count);
4907 }
4908
4909 /*****************************************************************************/
4910
4911 /*
4912  *      Return the board stats structure to user app.
4913  */
4914
4915 static int stli_getbrdstats(combrd_t __user *bp)
4916 {
4917         stlibrd_t       *brdp;
4918         int             i;
4919
4920         if (copy_from_user(&stli_brdstats, bp, sizeof(combrd_t)))
4921                 return -EFAULT;
4922         if (stli_brdstats.brd >= STL_MAXBRDS)
4923                 return(-ENODEV);
4924         brdp = stli_brds[stli_brdstats.brd];
4925         if (brdp == (stlibrd_t *) NULL)
4926                 return(-ENODEV);
4927
4928         memset(&stli_brdstats, 0, sizeof(combrd_t));
4929         stli_brdstats.brd = brdp->brdnr;
4930         stli_brdstats.type = brdp->brdtype;
4931         stli_brdstats.hwid = 0;
4932         stli_brdstats.state = brdp->state;
4933         stli_brdstats.ioaddr = brdp->iobase;
4934         stli_brdstats.memaddr = brdp->memaddr;
4935         stli_brdstats.nrpanels = brdp->nrpanels;
4936         stli_brdstats.nrports = brdp->nrports;
4937         for (i = 0; (i < brdp->nrpanels); i++) {
4938                 stli_brdstats.panels[i].panel = i;
4939                 stli_brdstats.panels[i].hwid = brdp->panelids[i];
4940                 stli_brdstats.panels[i].nrports = brdp->panels[i];
4941         }
4942
4943         if (copy_to_user(bp, &stli_brdstats, sizeof(combrd_t)))
4944                 return -EFAULT;
4945         return(0);
4946 }
4947
4948 /*****************************************************************************/
4949
4950 /*
4951  *      Resolve the referenced port number into a port struct pointer.
4952  */
4953
4954 static stliport_t *stli_getport(int brdnr, int panelnr, int portnr)
4955 {
4956         stlibrd_t       *brdp;
4957         int             i;
4958
4959         if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
4960                 return((stliport_t *) NULL);
4961         brdp = stli_brds[brdnr];
4962         if (brdp == (stlibrd_t *) NULL)
4963                 return((stliport_t *) NULL);
4964         for (i = 0; (i < panelnr); i++)
4965                 portnr += brdp->panels[i];
4966         if ((portnr < 0) || (portnr >= brdp->nrports))
4967                 return((stliport_t *) NULL);
4968         return(brdp->ports[portnr]);
4969 }
4970
4971 /*****************************************************************************/
4972
4973 /*
4974  *      Return the port stats structure to user app. A NULL port struct
4975  *      pointer passed in means that we need to find out from the app
4976  *      what port to get stats for (used through board control device).
4977  */
4978
4979 static int stli_portcmdstats(stliport_t *portp)
4980 {
4981         unsigned long   flags;
4982         stlibrd_t       *brdp;
4983         int             rc;
4984
4985         memset(&stli_comstats, 0, sizeof(comstats_t));
4986
4987         if (portp == (stliport_t *) NULL)
4988                 return(-ENODEV);
4989         brdp = stli_brds[portp->brdnr];
4990         if (brdp == (stlibrd_t *) NULL)
4991                 return(-ENODEV);
4992
4993         if (brdp->state & BST_STARTED) {
4994                 if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
4995                     &stli_cdkstats, sizeof(asystats_t), 1)) < 0)
4996                         return(rc);
4997         } else {
4998                 memset(&stli_cdkstats, 0, sizeof(asystats_t));
4999         }
5000
5001         stli_comstats.brd = portp->brdnr;
5002         stli_comstats.panel = portp->panelnr;
5003         stli_comstats.port = portp->portnr;
5004         stli_comstats.state = portp->state;
5005         stli_comstats.flags = portp->flags;
5006
5007         save_flags(flags);
5008         cli();
5009         if (portp->tty != (struct tty_struct *) NULL) {
5010                 if (portp->tty->driver_data == portp) {
5011                         stli_comstats.ttystate = portp->tty->flags;
5012                         stli_comstats.rxbuffered = portp->tty->flip.count;
5013                         if (portp->tty->termios != (struct termios *) NULL) {
5014                                 stli_comstats.cflags = portp->tty->termios->c_cflag;
5015                                 stli_comstats.iflags = portp->tty->termios->c_iflag;
5016                                 stli_comstats.oflags = portp->tty->termios->c_oflag;
5017                                 stli_comstats.lflags = portp->tty->termios->c_lflag;
5018                         }
5019                 }
5020         }
5021         restore_flags(flags);
5022
5023         stli_comstats.txtotal = stli_cdkstats.txchars;
5024         stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
5025         stli_comstats.txbuffered = stli_cdkstats.txringq;
5026         stli_comstats.rxbuffered += stli_cdkstats.rxringq;
5027         stli_comstats.rxoverrun = stli_cdkstats.overruns;
5028         stli_comstats.rxparity = stli_cdkstats.parity;
5029         stli_comstats.rxframing = stli_cdkstats.framing;
5030         stli_comstats.rxlost = stli_cdkstats.ringover;
5031         stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
5032         stli_comstats.txbreaks = stli_cdkstats.txbreaks;
5033         stli_comstats.txxon = stli_cdkstats.txstart;
5034         stli_comstats.txxoff = stli_cdkstats.txstop;
5035         stli_comstats.rxxon = stli_cdkstats.rxstart;
5036         stli_comstats.rxxoff = stli_cdkstats.rxstop;
5037         stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
5038         stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
5039         stli_comstats.modem = stli_cdkstats.dcdcnt;
5040         stli_comstats.hwid = stli_cdkstats.hwid;
5041         stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
5042
5043         return(0);
5044 }
5045
5046 /*****************************************************************************/
5047
5048 /*
5049  *      Return the port stats structure to user app. A NULL port struct
5050  *      pointer passed in means that we need to find out from the app
5051  *      what port to get stats for (used through board control device).
5052  */
5053
5054 static int stli_getportstats(stliport_t *portp, comstats_t __user *cp)
5055 {
5056         stlibrd_t       *brdp;
5057         int             rc;
5058
5059         if (!portp) {
5060                 if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
5061                         return -EFAULT;
5062                 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
5063                         stli_comstats.port);
5064                 if (!portp)
5065                         return -ENODEV;
5066         }
5067
5068         brdp = stli_brds[portp->brdnr];
5069         if (!brdp)
5070                 return -ENODEV;
5071
5072         if ((rc = stli_portcmdstats(portp)) < 0)
5073                 return rc;
5074
5075         return copy_to_user(cp, &stli_comstats, sizeof(comstats_t)) ?
5076                         -EFAULT : 0;
5077 }
5078
5079 /*****************************************************************************/
5080
5081 /*
5082  *      Clear the port stats structure. We also return it zeroed out...
5083  */
5084
5085 static int stli_clrportstats(stliport_t *portp, comstats_t __user *cp)
5086 {
5087         stlibrd_t       *brdp;
5088         int             rc;
5089
5090         if (!portp) {
5091                 if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
5092                         return -EFAULT;
5093                 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
5094                         stli_comstats.port);
5095                 if (!portp)
5096                         return -ENODEV;
5097         }
5098
5099         brdp = stli_brds[portp->brdnr];
5100         if (!brdp)
5101                 return -ENODEV;
5102
5103         if (brdp->state & BST_STARTED) {
5104                 if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, NULL, 0, 0)) < 0)
5105                         return rc;
5106         }
5107
5108         memset(&stli_comstats, 0, sizeof(comstats_t));
5109         stli_comstats.brd = portp->brdnr;
5110         stli_comstats.panel = portp->panelnr;
5111         stli_comstats.port = portp->portnr;
5112
5113         if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t)))
5114                 return -EFAULT;
5115         return 0;
5116 }
5117
5118 /*****************************************************************************/
5119
5120 /*
5121  *      Return the entire driver ports structure to a user app.
5122  */
5123
5124 static int stli_getportstruct(stliport_t __user *arg)
5125 {
5126         stliport_t      *portp;
5127
5128         if (copy_from_user(&stli_dummyport, arg, sizeof(stliport_t)))
5129                 return -EFAULT;
5130         portp = stli_getport(stli_dummyport.brdnr, stli_dummyport.panelnr,
5131                  stli_dummyport.portnr);
5132         if (!portp)
5133                 return -ENODEV;
5134         if (copy_to_user(arg, portp, sizeof(stliport_t)))
5135                 return -EFAULT;
5136         return 0;
5137 }
5138
5139 /*****************************************************************************/
5140
5141 /*
5142  *      Return the entire driver board structure to a user app.
5143  */
5144
5145 static int stli_getbrdstruct(stlibrd_t __user *arg)
5146 {
5147         stlibrd_t       *brdp;
5148
5149         if (copy_from_user(&stli_dummybrd, arg, sizeof(stlibrd_t)))
5150                 return -EFAULT;
5151         if ((stli_dummybrd.brdnr < 0) || (stli_dummybrd.brdnr >= STL_MAXBRDS))
5152                 return -ENODEV;
5153         brdp = stli_brds[stli_dummybrd.brdnr];
5154         if (!brdp)
5155                 return -ENODEV;
5156         if (copy_to_user(arg, brdp, sizeof(stlibrd_t)))
5157                 return -EFAULT;
5158         return 0;
5159 }
5160
5161 /*****************************************************************************/
5162
5163 /*
5164  *      The "staliomem" device is also required to do some special operations on
5165  *      the board. We need to be able to send an interrupt to the board,
5166  *      reset it, and start/stop it.
5167  */
5168
5169 static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
5170 {
5171         stlibrd_t       *brdp;
5172         int             brdnr, rc, done;
5173         void __user *argp = (void __user *)arg;
5174
5175 #ifdef DEBUG
5176         printk(KERN_DEBUG "stli_memioctl(ip=%x,fp=%x,cmd=%x,arg=%x)\n",
5177                         (int) ip, (int) fp, cmd, (int) arg);
5178 #endif
5179
5180 /*
5181  *      First up handle the board independent ioctls.
5182  */
5183         done = 0;
5184         rc = 0;
5185
5186         switch (cmd) {
5187         case COM_GETPORTSTATS:
5188                 rc = stli_getportstats(NULL, argp);
5189                 done++;
5190                 break;
5191         case COM_CLRPORTSTATS:
5192                 rc = stli_clrportstats(NULL, argp);
5193                 done++;
5194                 break;
5195         case COM_GETBRDSTATS:
5196                 rc = stli_getbrdstats(argp);
5197                 done++;
5198                 break;
5199         case COM_READPORT:
5200                 rc = stli_getportstruct(argp);
5201                 done++;
5202                 break;
5203         case COM_READBOARD:
5204                 rc = stli_getbrdstruct(argp);
5205                 done++;
5206                 break;
5207         }
5208
5209         if (done)
5210                 return(rc);
5211
5212 /*
5213  *      Now handle the board specific ioctls. These all depend on the
5214  *      minor number of the device they were called from.
5215  */
5216         brdnr = iminor(ip);
5217         if (brdnr >= STL_MAXBRDS)
5218                 return(-ENODEV);
5219         brdp = stli_brds[brdnr];
5220         if (!brdp)
5221                 return(-ENODEV);
5222         if (brdp->state == 0)
5223                 return(-ENODEV);
5224
5225         switch (cmd) {
5226         case STL_BINTR:
5227                 EBRDINTR(brdp);
5228                 break;
5229         case STL_BSTART:
5230                 rc = stli_startbrd(brdp);
5231                 break;
5232         case STL_BSTOP:
5233                 brdp->state &= ~BST_STARTED;
5234                 break;
5235         case STL_BRESET:
5236                 brdp->state &= ~BST_STARTED;
5237                 EBRDRESET(brdp);
5238                 if (stli_shared == 0) {
5239                         if (brdp->reenable != NULL)
5240                                 (* brdp->reenable)(brdp);
5241                 }
5242                 break;
5243         default:
5244                 rc = -ENOIOCTLCMD;
5245                 break;
5246         }
5247
5248         return(rc);
5249 }
5250
5251 static struct tty_operations stli_ops = {
5252         .open = stli_open,
5253         .close = stli_close,
5254         .write = stli_write,
5255         .put_char = stli_putchar,
5256         .flush_chars = stli_flushchars,
5257         .write_room = stli_writeroom,
5258         .chars_in_buffer = stli_charsinbuffer,
5259         .ioctl = stli_ioctl,
5260         .set_termios = stli_settermios,
5261         .throttle = stli_throttle,
5262         .unthrottle = stli_unthrottle,
5263         .stop = stli_stop,
5264         .start = stli_start,
5265         .hangup = stli_hangup,
5266         .flush_buffer = stli_flushbuffer,
5267         .break_ctl = stli_breakctl,
5268         .wait_until_sent = stli_waituntilsent,
5269         .send_xchar = stli_sendxchar,
5270         .read_proc = stli_readproc,
5271         .tiocmget = stli_tiocmget,
5272         .tiocmset = stli_tiocmset,
5273 };
5274
5275 /*****************************************************************************/
5276
5277 int __init stli_init(void)
5278 {
5279         int i;
5280         printk(KERN_INFO "%s: version %s\n", stli_drvtitle, stli_drvversion);
5281
5282         stli_initbrds();
5283
5284         stli_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
5285         if (!stli_serial)
5286                 return -ENOMEM;
5287
5288 /*
5289  *      Allocate a temporary write buffer.
5290  */
5291         stli_tmpwritebuf = (char *) stli_memalloc(STLI_TXBUFSIZE);
5292         if (stli_tmpwritebuf == (char *) NULL)
5293                 printk(KERN_ERR "STALLION: failed to allocate memory "
5294                                 "(size=%d)\n", STLI_TXBUFSIZE);
5295         stli_txcookbuf = stli_memalloc(STLI_TXBUFSIZE);
5296         if (stli_txcookbuf == (char *) NULL)
5297                 printk(KERN_ERR "STALLION: failed to allocate memory "
5298                                 "(size=%d)\n", STLI_TXBUFSIZE);
5299
5300 /*
5301  *      Set up a character driver for the shared memory region. We need this
5302  *      to down load the slave code image. Also it is a useful debugging tool.
5303  */
5304         if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem))
5305                 printk(KERN_ERR "STALLION: failed to register serial memory "
5306                                 "device\n");
5307
5308         devfs_mk_dir("staliomem");
5309         istallion_class = class_simple_create(THIS_MODULE, "staliomem");
5310         for (i = 0; i < 4; i++) {
5311                 devfs_mk_cdev(MKDEV(STL_SIOMEMMAJOR, i),
5312                                S_IFCHR | S_IRUSR | S_IWUSR,
5313                                "staliomem/%d", i);
5314                 class_simple_device_add(istallion_class, MKDEV(STL_SIOMEMMAJOR, i), 
5315                                 NULL, "staliomem%d", i);
5316         }
5317
5318 /*
5319  *      Set up the tty driver structure and register us as a driver.
5320  */
5321         stli_serial->owner = THIS_MODULE;
5322         stli_serial->driver_name = stli_drvname;
5323         stli_serial->name = stli_serialname;
5324         stli_serial->major = STL_SERIALMAJOR;
5325         stli_serial->minor_start = 0;
5326         stli_serial->type = TTY_DRIVER_TYPE_SERIAL;
5327         stli_serial->subtype = SERIAL_TYPE_NORMAL;
5328         stli_serial->init_termios = stli_deftermios;
5329         stli_serial->flags = TTY_DRIVER_REAL_RAW;
5330         tty_set_operations(stli_serial, &stli_ops);
5331
5332         if (tty_register_driver(stli_serial)) {
5333                 put_tty_driver(stli_serial);
5334                 printk(KERN_ERR "STALLION: failed to register serial driver\n");
5335                 return -EBUSY;
5336         }
5337         return(0);
5338 }
5339
5340 /*****************************************************************************/