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