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