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