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