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