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