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