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