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