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