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