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