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