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