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