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