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