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