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