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