~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/char/stallion.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*****************************************************************************/
  2 
  3 /*
  4  *      stallion.c  -- stallion multiport serial driver.
  5  *
  6  *      Copyright (C) 1996-1999  Stallion Technologies (support@stallion.oz.au).
  7  *      Copyright (C) 1994-1996  Greg Ungerer.
  8  *
  9  *      This code is loosely based on the Linux serial driver, written by
 10  *      Linus Torvalds, Theodore T'so and others.
 11  *
 12  *      This program is free software; you can redistribute it and/or modify
 13  *      it under the terms of the GNU General Public License as published by
 14  *      the Free Software Foundation; either version 2 of the License, or
 15  *      (at your option) any later version.
 16  *
 17  *      This program is distributed in the hope that it will be useful,
 18  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 19  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20  *      GNU General Public License for more details.
 21  *
 22  *      You should have received a copy of the GNU General Public License
 23  *      along with this program; if not, write to the Free Software
 24  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 25  */
 26 
 27 /*****************************************************************************/
 28 
 29 #include <linux/config.h>
 30 #include <linux/module.h>
 31 #include <linux/version.h> /* for linux/stallion.h */
 32 #include <linux/malloc.h>
 33 #include <linux/interrupt.h>
 34 #include <linux/tty_flip.h>
 35 #include <linux/serial.h>
 36 #include <linux/cd1400.h>
 37 #include <linux/sc26198.h>
 38 #include <linux/comstats.h>
 39 #include <linux/stallion.h>
 40 #include <linux/ioport.h>
 41 #include <linux/init.h>
 42 #include <linux/smp_lock.h>
 43 #include <linux/devfs_fs_kernel.h>
 44 
 45 #include <asm/io.h>
 46 #include <asm/uaccess.h>
 47 
 48 #ifdef CONFIG_PCI
 49 #include <linux/pci.h>
 50 #endif
 51 
 52 /*****************************************************************************/
 53 
 54 /*
 55  *      Define different board types. Use the standard Stallion "assigned"
 56  *      board numbers. Boards supported in this driver are abbreviated as
 57  *      EIO = EasyIO and ECH = EasyConnection 8/32.
 58  */
 59 #define BRD_EASYIO      20
 60 #define BRD_ECH         21
 61 #define BRD_ECHMC       22
 62 #define BRD_ECHPCI      26
 63 #define BRD_ECH64PCI    27
 64 #define BRD_EASYIOPCI   28
 65 
 66 /*
 67  *      Define a configuration structure to hold the board configuration.
 68  *      Need to set this up in the code (for now) with the boards that are
 69  *      to be configured into the system. This is what needs to be modified
 70  *      when adding/removing/modifying boards. Each line entry in the
 71  *      stl_brdconf[] array is a board. Each line contains io/irq/memory
 72  *      ranges for that board (as well as what type of board it is).
 73  *      Some examples:
 74  *              { BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },
 75  *      This line would configure an EasyIO board (4 or 8, no difference),
 76  *      at io address 2a0 and irq 10.
 77  *      Another example:
 78  *              { BRD_ECH, 0x2a8, 0x280, 0, 12, 0 },
 79  *      This line will configure an EasyConnection 8/32 board at primary io
 80  *      address 2a8, secondary io address 280 and irq 12.
 81  *      Enter as many lines into this array as you want (only the first 4
 82  *      will actually be used!). Any combination of EasyIO and EasyConnection
 83  *      boards can be specified. EasyConnection 8/32 boards can share their
 84  *      secondary io addresses between each other.
 85  *
 86  *      NOTE: there is no need to put any entries in this table for PCI
 87  *      boards. They will be found automatically by the driver - provided
 88  *      PCI BIOS32 support is compiled into the kernel.
 89  */
 90 
 91 typedef struct {
 92         int             brdtype;
 93         int             ioaddr1;
 94         int             ioaddr2;
 95         unsigned long   memaddr;
 96         int             irq;
 97         int             irqtype;
 98 } stlconf_t;
 99 
100 static stlconf_t        stl_brdconf[] = {
101         /*{ BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },*/
102 };
103 
104 static int      stl_nrbrds = sizeof(stl_brdconf) / sizeof(stlconf_t);
105 
106 /*****************************************************************************/
107 
108 /*
109  *      Define some important driver characteristics. Device major numbers
110  *      allocated as per Linux Device Registry.
111  */
112 #ifndef STL_SIOMEMMAJOR
113 #define STL_SIOMEMMAJOR         28
114 #endif
115 #ifndef STL_SERIALMAJOR
116 #define STL_SERIALMAJOR         24
117 #endif
118 #ifndef STL_CALLOUTMAJOR
119 #define STL_CALLOUTMAJOR        25
120 #endif
121 
122 #define STL_DRVTYPSERIAL        1
123 #define STL_DRVTYPCALLOUT       2
124 
125 /*
126  *      Set the TX buffer size. Bigger is better, but we don't want
127  *      to chew too much memory with buffers!
128  */
129 #define STL_TXBUFLOW            512
130 #define STL_TXBUFSIZE           4096
131 
132 /*****************************************************************************/
133 
134 /*
135  *      Define our local driver identity first. Set up stuff to deal with
136  *      all the local structures required by a serial tty driver.
137  */
138 static char     *stl_drvtitle = "Stallion Multiport Serial Driver";
139 static char     *stl_drvname = "stallion";
140 static char     *stl_drvversion = "5.6.0";
141 static char     *stl_serialname = "ttyE";
142 static char     *stl_calloutname = "cue";
143 
144 static struct tty_driver        stl_serial;
145 static struct tty_driver        stl_callout;
146 static struct tty_struct        *stl_ttys[STL_MAXDEVS];
147 static struct termios           *stl_termios[STL_MAXDEVS];
148 static struct termios           *stl_termioslocked[STL_MAXDEVS];
149 static int                      stl_refcount;
150 
151 /*
152  *      We will need to allocate a temporary write buffer for chars that
153  *      come direct from user space. The problem is that a copy from user
154  *      space might cause a page fault (typically on a system that is
155  *      swapping!). All ports will share one buffer - since if the system
156  *      is already swapping a shared buffer won't make things any worse.
157  */
158 static char                     *stl_tmpwritebuf;
159 static DECLARE_MUTEX(stl_tmpwritesem);
160 
161 /*
162  *      Define a local default termios struct. All ports will be created
163  *      with this termios initially. Basically all it defines is a raw port
164  *      at 9600, 8 data bits, 1 stop bit.
165  */
166 static struct termios           stl_deftermios = {
167         0,
168         0,
169         (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
170         0,
171         0,
172         INIT_C_CC
173 };
174 
175 /*
176  *      Define global stats structures. Not used often, and can be
177  *      re-used for each stats call.
178  */
179 static comstats_t       stl_comstats;
180 static combrd_t         stl_brdstats;
181 static stlbrd_t         stl_dummybrd;
182 static stlport_t        stl_dummyport;
183 
184 /*
185  *      Define global place to put buffer overflow characters.
186  */
187 static char             stl_unwanted[SC26198_RXFIFOSIZE];
188 
189 /*
190  *      Keep track of what interrupts we have requested for us.
191  *      We don't need to request an interrupt twice if it is being
192  *      shared with another Stallion board.
193  */
194 static int      stl_gotintrs[STL_MAXBRDS];
195 static int      stl_numintrs;
196 
197 /*****************************************************************************/
198 
199 static stlbrd_t         *stl_brds[STL_MAXBRDS];
200 
201 /*
202  *      Per board state flags. Used with the state field of the board struct.
203  *      Not really much here!
204  */
205 #define BRD_FOUND       0x1
206 
207 /*
208  *      Define the port structure istate flags. These set of flags are
209  *      modified at interrupt time - so setting and reseting them needs
210  *      to be atomic. Use the bit clear/setting routines for this.
211  */
212 #define ASYI_TXBUSY     1
213 #define ASYI_TXLOW      2
214 #define ASYI_DCDCHANGE  3
215 #define ASYI_TXFLOWED   4
216 
217 /*
218  *      Define an array of board names as printable strings. Handy for
219  *      referencing boards when printing trace and stuff.
220  */
221 static char     *stl_brdnames[] = {
222         (char *) NULL,
223         (char *) NULL,
224         (char *) NULL,
225         (char *) NULL,
226         (char *) NULL,
227         (char *) NULL,
228         (char *) NULL,
229         (char *) NULL,
230         (char *) NULL,
231         (char *) NULL,
232         (char *) NULL,
233         (char *) NULL,
234         (char *) NULL,
235         (char *) NULL,
236         (char *) NULL,
237         (char *) NULL,
238         (char *) NULL,
239         (char *) NULL,
240         (char *) NULL,
241         (char *) NULL,
242         "EasyIO",
243         "EC8/32-AT",
244         "EC8/32-MC",
245         (char *) NULL,
246         (char *) NULL,
247         (char *) NULL,
248         "EC8/32-PCI",
249         "EC8/64-PCI",
250         "EasyIO-PCI",
251 };
252 
253 /*****************************************************************************/
254 
255 #ifdef MODULE
256 /*
257  *      Define some string labels for arguments passed from the module
258  *      load line. These allow for easy board definitions, and easy
259  *      modification of the io, memory and irq resoucres.
260  */
261 
262 static char     *board0[4];
263 static char     *board1[4];
264 static char     *board2[4];
265 static char     *board3[4];
266 
267 static char     **stl_brdsp[] = {
268         (char **) &board0,
269         (char **) &board1,
270         (char **) &board2,
271         (char **) &board3
272 };
273 
274 /*
275  *      Define a set of common board names, and types. This is used to
276  *      parse any module arguments.
277  */
278 
279 typedef struct stlbrdtype {
280         char    *name;
281         int     type;
282 } stlbrdtype_t;
283 
284 static stlbrdtype_t     stl_brdstr[] = {
285         { "easyio", BRD_EASYIO },
286         { "eio", BRD_EASYIO },
287         { "20", BRD_EASYIO },
288         { "ec8/32", BRD_ECH },
289         { "ec8/32-at", BRD_ECH },
290         { "ec8/32-isa", BRD_ECH },
291         { "ech", BRD_ECH },
292         { "echat", BRD_ECH },
293         { "21", BRD_ECH },
294         { "ec8/32-mc", BRD_ECHMC },
295         { "ec8/32-mca", BRD_ECHMC },
296         { "echmc", BRD_ECHMC },
297         { "echmca", BRD_ECHMC },
298         { "22", BRD_ECHMC },
299         { "ec8/32-pc", BRD_ECHPCI },
300         { "ec8/32-pci", BRD_ECHPCI },
301         { "26", BRD_ECHPCI },
302         { "ec8/64-pc", BRD_ECH64PCI },
303         { "ec8/64-pci", BRD_ECH64PCI },
304         { "ech-pci", BRD_ECH64PCI },
305         { "echpci", BRD_ECH64PCI },
306         { "echpc", BRD_ECH64PCI },
307         { "27", BRD_ECH64PCI },
308         { "easyio-pc", BRD_EASYIOPCI },
309         { "easyio-pci", BRD_EASYIOPCI },
310         { "eio-pci", BRD_EASYIOPCI },
311         { "eiopci", BRD_EASYIOPCI },
312         { "28", BRD_EASYIOPCI },
313 };
314 
315 /*
316  *      Define the module agruments.
317  */
318 MODULE_AUTHOR("Greg Ungerer");
319 MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
320 
321 MODULE_PARM(board0, "1-4s");
322 MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
323 MODULE_PARM(board1, "1-4s");
324 MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
325 MODULE_PARM(board2, "1-4s");
326 MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
327 MODULE_PARM(board3, "1-4s");
328 MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");
329 
330 #endif
331 
332 /*****************************************************************************/
333 
334 /*
335  *      Hardware ID bits for the EasyIO and ECH boards. These defines apply
336  *      to the directly accessible io ports of these boards (not the uarts -
337  *      they are in cd1400.h and sc26198.h).
338  */
339 #define EIO_8PORTRS     0x04
340 #define EIO_4PORTRS     0x05
341 #define EIO_8PORTDI     0x00
342 #define EIO_8PORTM      0x06
343 #define EIO_MK3         0x03
344 #define EIO_IDBITMASK   0x07
345 
346 #define EIO_BRDMASK     0xf0
347 #define ID_BRD4         0x10
348 #define ID_BRD8         0x20
349 #define ID_BRD16        0x30
350 
351 #define EIO_INTRPEND    0x08
352 #define EIO_INTEDGE     0x00
353 #define EIO_INTLEVEL    0x08
354 #define EIO_0WS         0x10
355 
356 #define ECH_ID          0xa0
357 #define ECH_IDBITMASK   0xe0
358 #define ECH_BRDENABLE   0x08
359 #define ECH_BRDDISABLE  0x00
360 #define ECH_INTENABLE   0x01
361 #define ECH_INTDISABLE  0x00
362 #define ECH_INTLEVEL    0x02
363 #define ECH_INTEDGE     0x00
364 #define ECH_INTRPEND    0x01
365 #define ECH_BRDRESET    0x01
366 
367 #define ECHMC_INTENABLE 0x01
368 #define ECHMC_BRDRESET  0x02
369 
370 #define ECH_PNLSTATUS   2
371 #define ECH_PNL16PORT   0x20
372 #define ECH_PNLIDMASK   0x07
373 #define ECH_PNLXPID     0x40
374 #define ECH_PNLINTRPEND 0x80
375 
376 #define ECH_ADDR2MASK   0x1e0
377 
378 /*
379  *      Define the vector mapping bits for the programmable interrupt board
380  *      hardware. These bits encode the interrupt for the board to use - it
381  *      is software selectable (except the EIO-8M).
382  */
383 static unsigned char    stl_vecmap[] = {
384         0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
385         0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
386 };
387 
388 /*
389  *      Set up enable and disable macros for the ECH boards. They require
390  *      the secondary io address space to be activated and deactivated.
391  *      This way all ECH boards can share their secondary io region.
392  *      If this is an ECH-PCI board then also need to set the page pointer
393  *      to point to the correct page.
394  */
395 #define BRDENABLE(brdnr,pagenr)                                         \
396         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
397                 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE),    \
398                         stl_brds[(brdnr)]->ioctrl);                     \
399         else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI)              \
400                 outb((pagenr), stl_brds[(brdnr)]->ioctrl);
401 
402 #define BRDDISABLE(brdnr)                                               \
403         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
404                 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE),   \
405                         stl_brds[(brdnr)]->ioctrl);
406 
407 #define STL_CD1400MAXBAUD       230400
408 #define STL_SC26198MAXBAUD      460800
409 
410 #define STL_BAUDBASE            115200
411 #define STL_CLOSEDELAY          (5 * HZ / 10)
412 
413 /*****************************************************************************/
414 
415 #ifdef CONFIG_PCI
416 
417 /*
418  *      Define the Stallion PCI vendor and device IDs.
419  */
420 #ifndef PCI_VENDOR_ID_STALLION
421 #define PCI_VENDOR_ID_STALLION          0x124d
422 #endif
423 #ifndef PCI_DEVICE_ID_ECHPCI832
424 #define PCI_DEVICE_ID_ECHPCI832         0x0000
425 #endif
426 #ifndef PCI_DEVICE_ID_ECHPCI864
427 #define PCI_DEVICE_ID_ECHPCI864         0x0002
428 #endif
429 #ifndef PCI_DEVICE_ID_EIOPCI
430 #define PCI_DEVICE_ID_EIOPCI            0x0003
431 #endif
432 
433 /*
434  *      Define structure to hold all Stallion PCI boards.
435  */
436 typedef struct stlpcibrd {
437         unsigned short          vendid;
438         unsigned short          devid;
439         int                     brdtype;
440 } stlpcibrd_t;
441 
442 static stlpcibrd_t      stl_pcibrds[] = {
443         { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864, BRD_ECH64PCI },
444         { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI, BRD_EASYIOPCI },
445         { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832, BRD_ECHPCI },
446         { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410, BRD_ECHPCI },
447 };
448 
449 static int      stl_nrpcibrds = sizeof(stl_pcibrds) / sizeof(stlpcibrd_t);
450 
451 #endif
452 
453 /*****************************************************************************/
454 
455 /*
456  *      Define macros to extract a brd/port number from a minor number.
457  */
458 #define MINOR2BRD(min)          (((min) & 0xc0) >> 6)
459 #define MINOR2PORT(min)         ((min) & 0x3f)
460 
461 /*
462  *      Define a baud rate table that converts termios baud rate selector
463  *      into the actual baud rate value. All baud rate calculations are
464  *      based on the actual baud rate required.
465  */
466 static unsigned int     stl_baudrates[] = {
467         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
468         9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
469 };
470 
471 /*
472  *      Define some handy local macros...
473  */
474 #undef  MIN
475 #define MIN(a,b)        (((a) <= (b)) ? (a) : (b))
476 
477 #undef  TOLOWER
478 #define TOLOWER(x)      ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
479 
480 /*****************************************************************************/
481 
482 /*
483  *      Declare all those functions in this driver!
484  */
485 
486 #ifdef MODULE
487 int             init_module(void);
488 void            cleanup_module(void);
489 static void     stl_argbrds(void);
490 static int      stl_parsebrd(stlconf_t *confp, char **argp);
491 
492 static unsigned long stl_atol(char *str);
493 #endif
494 
495 int             stl_init(void);
496 static int      stl_open(struct tty_struct *tty, struct file *filp);
497 static void     stl_close(struct tty_struct *tty, struct file *filp);
498 static int      stl_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
499 static void     stl_putchar(struct tty_struct *tty, unsigned char ch);
500 static void     stl_flushchars(struct tty_struct *tty);
501 static int      stl_writeroom(struct tty_struct *tty);
502 static int      stl_charsinbuffer(struct tty_struct *tty);
503 static int      stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
504 static void     stl_settermios(struct tty_struct *tty, struct termios *old);
505 static void     stl_throttle(struct tty_struct *tty);
506 static void     stl_unthrottle(struct tty_struct *tty);
507 static void     stl_stop(struct tty_struct *tty);
508 static void     stl_start(struct tty_struct *tty);
509 static void     stl_flushbuffer(struct tty_struct *tty);
510 static void     stl_breakctl(struct tty_struct *tty, int state);
511 static void     stl_waituntilsent(struct tty_struct *tty, int timeout);
512 static void     stl_sendxchar(struct tty_struct *tty, char ch);
513 static void     stl_hangup(struct tty_struct *tty);
514 static int      stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
515 static int      stl_portinfo(stlport_t *portp, int portnr, char *pos);
516 static int      stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data);
517 
518 static int      stl_brdinit(stlbrd_t *brdp);
519 static int      stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
520 static int      stl_mapirq(int irq, char *name);
521 static void     stl_getserial(stlport_t *portp, struct serial_struct *sp);
522 static int      stl_setserial(stlport_t *portp, struct serial_struct *sp);
523 static int      stl_getbrdstats(combrd_t *bp);
524 static int      stl_getportstats(stlport_t *portp, comstats_t *cp);
525 static int      stl_clrportstats(stlport_t *portp, comstats_t *cp);
526 static int      stl_getportstruct(unsigned long arg);
527 static int      stl_getbrdstruct(unsigned long arg);
528 static int      stl_waitcarrier(stlport_t *portp, struct file *filp);
529 static void     stl_delay(int len);
530 static void     stl_intr(int irq, void *dev_id, struct pt_regs *regs);
531 static void     stl_eiointr(stlbrd_t *brdp);
532 static void     stl_echatintr(stlbrd_t *brdp);
533 static void     stl_echmcaintr(stlbrd_t *brdp);
534 static void     stl_echpciintr(stlbrd_t *brdp);
535 static void     stl_echpci64intr(stlbrd_t *brdp);
536 static void     stl_offintr(void *private);
537 static void     *stl_memalloc(int len);
538 static stlbrd_t *stl_allocbrd(void);
539 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
540 
541 static inline int       stl_initbrds(void);
542 static inline int       stl_initeio(stlbrd_t *brdp);
543 static inline int       stl_initech(stlbrd_t *brdp);
544 static inline int       stl_getbrdnr(void);
545 
546 #ifdef  CONFIG_PCI
547 static inline int       stl_findpcibrds(void);
548 static inline int       stl_initpcibrd(int brdtype, struct pci_dev *devp);
549 #endif
550 
551 /*
552  *      CD1400 uart specific handling functions.
553  */
554 static void     stl_cd1400setreg(stlport_t *portp, int regnr, int value);
555 static int      stl_cd1400getreg(stlport_t *portp, int regnr);
556 static int      stl_cd1400updatereg(stlport_t *portp, int regnr, int value);
557 static int      stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
558 static void     stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
559 static void     stl_cd1400setport(stlport_t *portp, struct termios *tiosp);
560 static int      stl_cd1400getsignals(stlport_t *portp);
561 static void     stl_cd1400setsignals(stlport_t *portp, int dtr, int rts);
562 static void     stl_cd1400ccrwait(stlport_t *portp);
563 static void     stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx);
564 static void     stl_cd1400startrxtx(stlport_t *portp, int rx, int tx);
565 static void     stl_cd1400disableintrs(stlport_t *portp);
566 static void     stl_cd1400sendbreak(stlport_t *portp, int len);
567 static void     stl_cd1400flowctrl(stlport_t *portp, int state);
568 static void     stl_cd1400sendflow(stlport_t *portp, int state);
569 static void     stl_cd1400flush(stlport_t *portp);
570 static int      stl_cd1400datastate(stlport_t *portp);
571 static void     stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase);
572 static void     stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase);
573 static void     stl_cd1400txisr(stlpanel_t *panelp, int ioaddr);
574 static void     stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr);
575 static void     stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr);
576 
577 static inline int       stl_cd1400breakisr(stlport_t *portp, int ioaddr);
578 
579 /*
580  *      SC26198 uart specific handling functions.
581  */
582 static void     stl_sc26198setreg(stlport_t *portp, int regnr, int value);
583 static int      stl_sc26198getreg(stlport_t *portp, int regnr);
584 static int      stl_sc26198updatereg(stlport_t *portp, int regnr, int value);
585 static int      stl_sc26198getglobreg(stlport_t *portp, int regnr);
586 static int      stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
587 static void     stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
588 static void     stl_sc26198setport(stlport_t *portp, struct termios *tiosp);
589 static int      stl_sc26198getsignals(stlport_t *portp);
590 static void     stl_sc26198setsignals(stlport_t *portp, int dtr, int rts);
591 static void     stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx);
592 static void     stl_sc26198startrxtx(stlport_t *portp, int rx, int tx);
593 static void     stl_sc26198disableintrs(stlport_t *portp);
594 static void     stl_sc26198sendbreak(stlport_t *portp, int len);
595 static void     stl_sc26198flowctrl(stlport_t *portp, int state);
596 static void     stl_sc26198sendflow(stlport_t *portp, int state);
597 static void     stl_sc26198flush(stlport_t *portp);
598 static int      stl_sc26198datastate(stlport_t *portp);
599 static void     stl_sc26198wait(stlport_t *portp);
600 static void     stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty);
601 static void     stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase);
602 static void     stl_sc26198txisr(stlport_t *port);
603 static void     stl_sc26198rxisr(stlport_t *port, unsigned int iack);
604 static void     stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch);
605 static void     stl_sc26198rxbadchars(stlport_t *portp);
606 static void     stl_sc26198otherisr(stlport_t *port, unsigned int iack);
607 
608 /*****************************************************************************/
609 
610 /*
611  *      Generic UART support structure.
612  */
613 typedef struct uart {
614         int     (*panelinit)(stlbrd_t *brdp, stlpanel_t *panelp);
615         void    (*portinit)(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
616         void    (*setport)(stlport_t *portp, struct termios *tiosp);
617         int     (*getsignals)(stlport_t *portp);
618         void    (*setsignals)(stlport_t *portp, int dtr, int rts);
619         void    (*enablerxtx)(stlport_t *portp, int rx, int tx);
620         void    (*startrxtx)(stlport_t *portp, int rx, int tx);
621         void    (*disableintrs)(stlport_t *portp);
622         void    (*sendbreak)(stlport_t *portp, int len);
623         void    (*flowctrl)(stlport_t *portp, int state);
624         void    (*sendflow)(stlport_t *portp, int state);
625         void    (*flush)(stlport_t *portp);
626         int     (*datastate)(stlport_t *portp);
627         void    (*intr)(stlpanel_t *panelp, unsigned int iobase);
628 } uart_t;
629 
630 /*
631  *      Define some macros to make calling these functions nice and clean.
632  */
633 #define stl_panelinit           (* ((uart_t *) panelp->uartp)->panelinit)
634 #define stl_portinit            (* ((uart_t *) portp->uartp)->portinit)
635 #define stl_setport             (* ((uart_t *) portp->uartp)->setport)
636 #define stl_getsignals          (* ((uart_t *) portp->uartp)->getsignals)
637 #define stl_setsignals          (* ((uart_t *) portp->uartp)->setsignals)
638 #define stl_enablerxtx          (* ((uart_t *) portp->uartp)->enablerxtx)
639 #define stl_startrxtx           (* ((uart_t *) portp->uartp)->startrxtx)
640 #define stl_disableintrs        (* ((uart_t *) portp->uartp)->disableintrs)
641 #define stl_sendbreak           (* ((uart_t *) portp->uartp)->sendbreak)
642 #define stl_flowctrl            (* ((uart_t *) portp->uartp)->flowctrl)
643 #define stl_sendflow            (* ((uart_t *) portp->uartp)->sendflow)
644 #define stl_flush               (* ((uart_t *) portp->uartp)->flush)
645 #define stl_datastate           (* ((uart_t *) portp->uartp)->datastate)
646 
647 /*****************************************************************************/
648 
649 /*
650  *      CD1400 UART specific data initialization.
651  */
652 static uart_t stl_cd1400uart = {
653         stl_cd1400panelinit,
654         stl_cd1400portinit,
655         stl_cd1400setport,
656         stl_cd1400getsignals,
657         stl_cd1400setsignals,
658         stl_cd1400enablerxtx,
659         stl_cd1400startrxtx,
660         stl_cd1400disableintrs,
661         stl_cd1400sendbreak,
662         stl_cd1400flowctrl,
663         stl_cd1400sendflow,
664         stl_cd1400flush,
665         stl_cd1400datastate,
666         stl_cd1400eiointr
667 };
668 
669 /*
670  *      Define the offsets within the register bank of a cd1400 based panel.
671  *      These io address offsets are common to the EasyIO board as well.
672  */
673 #define EREG_ADDR       0
674 #define EREG_DATA       4
675 #define EREG_RXACK      5
676 #define EREG_TXACK      6
677 #define EREG_MDACK      7
678 
679 #define EREG_BANKSIZE   8
680 
681 #define CD1400_CLK      25000000
682 #define CD1400_CLK8M    20000000
683 
684 /*
685  *      Define the cd1400 baud rate clocks. These are used when calculating
686  *      what clock and divisor to use for the required baud rate. Also
687  *      define the maximum baud rate allowed, and the default base baud.
688  */
689 static int      stl_cd1400clkdivs[] = {
690         CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
691 };
692 
693 /*****************************************************************************/
694 
695 /*
696  *      SC26198 UART specific data initization.
697  */
698 static uart_t stl_sc26198uart = {
699         stl_sc26198panelinit,
700         stl_sc26198portinit,
701         stl_sc26198setport,
702         stl_sc26198getsignals,
703         stl_sc26198setsignals,
704         stl_sc26198enablerxtx,
705         stl_sc26198startrxtx,
706         stl_sc26198disableintrs,
707         stl_sc26198sendbreak,
708         stl_sc26198flowctrl,
709         stl_sc26198sendflow,
710         stl_sc26198flush,
711         stl_sc26198datastate,
712         stl_sc26198intr
713 };
714 
715 /*
716  *      Define the offsets within the register bank of a sc26198 based panel.
717  */
718 #define XP_DATA         0
719 #define XP_ADDR         1
720 #define XP_MODID        2
721 #define XP_STATUS       2
722 #define XP_IACK         3
723 
724 #define XP_BANKSIZE     4
725 
726 /*
727  *      Define the sc26198 baud rate table. Offsets within the table
728  *      represent the actual baud rate selector of sc26198 registers.
729  */
730 static unsigned int     sc26198_baudtable[] = {
731         50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
732         4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
733         230400, 460800, 921600
734 };
735 
736 #define SC26198_NRBAUDS         (sizeof(sc26198_baudtable) / sizeof(unsigned int))
737 
738 /*****************************************************************************/
739 
740 /*
741  *      Define the driver info for a user level control device. Used mainly
742  *      to get at port stats - only not using the port device itself.
743  */
744 static struct file_operations   stl_fsiomem = {
745         owner:          THIS_MODULE,
746         ioctl:          stl_memioctl,
747 };
748 
749 /*****************************************************************************/
750 
751 static devfs_handle_t devfs_handle;
752 
753 #ifdef MODULE
754 
755 /*
756  *      Loadable module initialization stuff.
757  */
758 
759 int init_module()
760 {
761         unsigned long   flags;
762 
763 #if DEBUG
764         printk("init_module()\n");
765 #endif
766 
767         save_flags(flags);
768         cli();
769         stl_init();
770         restore_flags(flags);
771 
772         return(0);
773 }
774 
775 /*****************************************************************************/
776 
777 void cleanup_module()
778 {
779         stlbrd_t        *brdp;
780         stlpanel_t      *panelp;
781         stlport_t       *portp;
782         unsigned long   flags;
783         int             i, j, k;
784 
785 #if DEBUG
786         printk("cleanup_module()\n");
787 #endif
788 
789         printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
790                 stl_drvversion);
791 
792         save_flags(flags);
793         cli();
794 
795 /*
796  *      Free up all allocated resources used by the ports. This includes
797  *      memory and interrupts. As part of this process we will also do
798  *      a hangup on every open port - to try to flush out any processes
799  *      hanging onto ports.
800  */
801         i = tty_unregister_driver(&stl_serial);
802         j = tty_unregister_driver(&stl_callout);
803         if (i || j) {
804                 printk("STALLION: failed to un-register tty driver, "
805                         "errno=%d,%d\n", -i, -j);
806                 restore_flags(flags);
807                 return;
808         }
809         devfs_unregister (devfs_handle);
810         if ((i = devfs_unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
811                 printk("STALLION: failed to un-register serial memory device, "
812                         "errno=%d\n", -i);
813 
814         if (stl_tmpwritebuf != (char *) NULL)
815                 kfree(stl_tmpwritebuf);
816 
817         for (i = 0; (i < stl_nrbrds); i++) {
818                 if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL)
819                         continue;
820                 for (j = 0; (j < STL_MAXPANELS); j++) {
821                         panelp = brdp->panels[j];
822                         if (panelp == (stlpanel_t *) NULL)
823                                 continue;
824                         for (k = 0; (k < STL_PORTSPERPANEL); k++) {
825                                 portp = panelp->ports[k];
826                                 if (portp == (stlport_t *) NULL)
827                                         continue;
828                                 if (portp->tty != (struct tty_struct *) NULL)
829                                         stl_hangup(portp->tty);
830                                 if (portp->tx.buf != (char *) NULL)
831                                         kfree(portp->tx.buf);
832                                 kfree(portp);
833                         }
834                         kfree(panelp);
835                 }
836 
837                 release_region(brdp->ioaddr1, brdp->iosize1);
838                 if (brdp->iosize2 > 0)
839                         release_region(brdp->ioaddr2, brdp->iosize2);
840 
841                 kfree(brdp);
842                 stl_brds[i] = (stlbrd_t *) NULL;
843         }
844 
845         for (i = 0; (i < stl_numintrs); i++)
846                 free_irq(stl_gotintrs[i], NULL);
847 
848         restore_flags(flags);
849 }
850 
851 /*****************************************************************************/
852 
853 /*
854  *      Check for any arguments passed in on the module load command line.
855  */
856 
857 static void stl_argbrds()
858 {
859         stlconf_t       conf;
860         stlbrd_t        *brdp;
861         int             nrargs, i;
862 
863 #if DEBUG
864         printk("stl_argbrds()\n");
865 #endif
866 
867         nrargs = sizeof(stl_brdsp) / sizeof(char **);
868 
869         for (i = stl_nrbrds; (i < nrargs); i++) {
870                 memset(&conf, 0, sizeof(conf));
871                 if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
872                         continue;
873                 if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
874                         continue;
875                 stl_nrbrds = i + 1;
876                 brdp->brdnr = i;
877                 brdp->brdtype = conf.brdtype;
878                 brdp->ioaddr1 = conf.ioaddr1;
879                 brdp->ioaddr2 = conf.ioaddr2;
880                 brdp->irq = conf.irq;
881                 brdp->irqtype = conf.irqtype;
882                 stl_brdinit(brdp);
883         }
884 }
885 
886 /*****************************************************************************/
887 
888 /*
889  *      Convert an ascii string number into an unsigned long.
890  */
891 
892 static unsigned long stl_atol(char *str)
893 {
894         unsigned long   val;
895         int             base, c;
896         char            *sp;
897 
898         val = 0;
899         sp = str;
900         if ((*sp == '') && (*(sp+1) == 'x')) {
901                 base = 16;
902                 sp += 2;
903         } else if (*sp == '') {
904                 base = 8;
905                 sp++;
906         } else {
907                 base = 10;
908         }
909 
910         for (; (*sp != 0); sp++) {
911                 c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '');
912                 if ((c < 0) || (c >= base)) {
913                         printk("STALLION: invalid argument %s\n", str);
914                         val = 0;
915                         break;
916                 }
917                 val = (val * base) + c;
918         }
919         return(val);
920 }
921 
922 /*****************************************************************************/
923 
924 /*
925  *      Parse the supplied argument string, into the board conf struct.
926  */
927 
928 static int stl_parsebrd(stlconf_t *confp, char **argp)
929 {
930         char    *sp;
931         int     nrbrdnames, i;
932 
933 #if DEBUG
934         printk("stl_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
935 #endif
936 
937         if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
938                 return(0);
939 
940         for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
941                 *sp = TOLOWER(*sp);
942 
943         nrbrdnames = sizeof(stl_brdstr) / sizeof(stlbrdtype_t);
944         for (i = 0; (i < nrbrdnames); i++) {
945                 if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
946                         break;
947         }
948         if (i >= nrbrdnames) {
949                 printk("STALLION: unknown board name, %s?\n", argp[0]);
950                 return(0);
951         }
952 
953         confp->brdtype = stl_brdstr[i].type;
954 
955         i = 1;
956         if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
957                 confp->ioaddr1 = stl_atol(argp[i]);
958         i++;
959         if (confp->brdtype == BRD_ECH) {
960                 if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
961                         confp->ioaddr2 = stl_atol(argp[i]);
962                 i++;
963         }
964         if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
965                 confp->irq = stl_atol(argp[i]);
966         return(1);
967 }
968 
969 #endif
970 
971 /*****************************************************************************/
972 
973 /*
974  *      Local driver kernel memory allocation routine.
975  */
976 
977 static void *stl_memalloc(int len)
978 {
979         return((void *) kmalloc(len, GFP_KERNEL));
980 }
981 
982 /*****************************************************************************/
983 
984 /*
985  *      Allocate a new board structure. Fill out the basic info in it.
986  */
987 
988 static stlbrd_t *stl_allocbrd()
989 {
990         stlbrd_t        *brdp;
991 
992         brdp = (stlbrd_t *) stl_memalloc(sizeof(stlbrd_t));
993         if (brdp == (stlbrd_t *) NULL) {
994                 printk("STALLION: failed to allocate memory (size=%d)\n",
995                         sizeof(stlbrd_t));
996                 return((stlbrd_t *) NULL);
997         }
998 
999         memset(brdp, 0, sizeof(stlbrd_t));
1000         brdp->magic = STL_BOARDMAGIC;
1001         return(brdp);
1002 }
1003 
1004 /*****************************************************************************/
1005 
1006 static int stl_open(struct tty_struct *tty, struct file *filp)
1007 {
1008         stlport_t       *portp;
1009         stlbrd_t        *brdp;
1010         unsigned int    minordev;
1011         int             brdnr, panelnr, portnr, rc;
1012 
1013 #if DEBUG
1014         printk("stl_open(tty=%x,filp=%x): device=%x\n", (int) tty,
1015                 (int) filp, tty->device);
1016 #endif
1017 
1018         minordev = MINOR(tty->device);
1019         brdnr = MINOR2BRD(minordev);
1020         if (brdnr >= stl_nrbrds)
1021                 return(-ENODEV);
1022         brdp = stl_brds[brdnr];
1023         if (brdp == (stlbrd_t *) NULL)
1024                 return(-ENODEV);
1025         minordev = MINOR2PORT(minordev);
1026         for (portnr = -1, panelnr = 0; (panelnr < STL_MAXPANELS); panelnr++) {
1027                 if (brdp->panels[panelnr] == (stlpanel_t *) NULL)
1028                         break;
1029                 if (minordev < brdp->panels[panelnr]->nrports) {
1030                         portnr = minordev;
1031                         break;
1032                 }
1033                 minordev -= brdp->panels[panelnr]->nrports;
1034         }
1035         if (portnr < 0)
1036                 return(-ENODEV);
1037 
1038         portp = brdp->panels[panelnr]->ports[portnr];
1039         if (portp == (stlport_t *) NULL)
1040                 return(-ENODEV);
1041 
1042         MOD_INC_USE_COUNT;
1043 
1044 /*
1045  *      On the first open of the device setup the port hardware, and
1046  *      initialize the per port data structure.
1047  */
1048         portp->tty = tty;
1049         tty->driver_data = portp;
1050         portp->refcount++;
1051 
1052         if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1053                 if (portp->tx.buf == (char *) NULL) {
1054                         portp->tx.buf = (char *) stl_memalloc(STL_TXBUFSIZE);
1055                         if (portp->tx.buf == (char *) NULL)
1056                                 return(-ENOMEM);
1057                         portp->tx.head = portp->tx.buf;
1058                         portp->tx.tail = portp->tx.buf;
1059                 }
1060                 stl_setport(portp, tty->termios);
1061                 portp->sigs = stl_getsignals(portp);
1062                 stl_setsignals(portp, 1, 1);
1063                 stl_enablerxtx(portp, 1, 1);
1064                 stl_startrxtx(portp, 1, 0);
1065                 clear_bit(TTY_IO_ERROR, &tty->flags);
1066                 portp->flags |= ASYNC_INITIALIZED;
1067         }
1068 
1069 /*
1070  *      Check if this port is in the middle of closing. If so then wait
1071  *      until it is closed then return error status, based on flag settings.
1072  *      The sleep here does not need interrupt protection since the wakeup
1073  *      for it is done with the same context.
1074  */
1075         if (portp->flags & ASYNC_CLOSING) {
1076                 interruptible_sleep_on(&portp->close_wait);
1077                 if (portp->flags & ASYNC_HUP_NOTIFY)
1078                         return(-EAGAIN);
1079                 return(-ERESTARTSYS);
1080         }
1081 
1082 /*
1083  *      Based on type of open being done check if it can overlap with any
1084  *      previous opens still in effect. If we are a normal serial device
1085  *      then also we might have to wait for carrier.
1086  */
1087         if (tty->driver.subtype == STL_DRVTYPCALLOUT) {
1088                 if (portp->flags & ASYNC_NORMAL_ACTIVE)
1089                         return(-EBUSY);
1090                 if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
1091                         if ((portp->flags & ASYNC_SESSION_LOCKOUT) &&
1092                             (portp->session != current->session))
1093                                 return(-EBUSY);
1094                         if ((portp->flags & ASYNC_PGRP_LOCKOUT) &&
1095                             (portp->pgrp != current->pgrp))
1096                                 return(-EBUSY);
1097                 }
1098                 portp->flags |= ASYNC_CALLOUT_ACTIVE;
1099         } else {
1100                 if (filp->f_flags & O_NONBLOCK) {
1101                         if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1102                                 return(-EBUSY);
1103                 } else {
1104                         if ((rc = stl_waitcarrier(portp, filp)) != 0)
1105                                 return(rc);
1106                 }
1107                 portp->flags |= ASYNC_NORMAL_ACTIVE;
1108         }
1109 
1110         if ((portp->refcount == 1) && (portp->flags & ASYNC_SPLIT_TERMIOS)) {
1111                 if (tty->driver.subtype == STL_DRVTYPSERIAL)
1112                         *tty->termios = portp->normaltermios;
1113                 else
1114                         *tty->termios = portp->callouttermios;
1115                 stl_setport(portp, tty->termios);
1116         }
1117 
1118         portp->session = current->session;
1119         portp->pgrp = current->pgrp;
1120         return(0);
1121 }
1122 
1123 /*****************************************************************************/
1124 
1125 /*
1126  *      Possibly need to wait for carrier (DCD signal) to come high. Say
1127  *      maybe because if we are clocal then we don't need to wait...
1128  */
1129 
1130 static int stl_waitcarrier(stlport_t *portp, struct file *filp)
1131 {
1132         unsigned long   flags;
1133         int             rc, doclocal;
1134 
1135 #if DEBUG
1136         printk("stl_waitcarrier(portp=%x,filp=%x)\n", (int) portp, (int) filp);
1137 #endif
1138 
1139         rc = 0;
1140         doclocal = 0;
1141 
1142         if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
1143                 if (portp->normaltermios.c_cflag & CLOCAL)
1144                         doclocal++;
1145         } else {
1146                 if (portp->tty->termios->c_cflag & CLOCAL)
1147                         doclocal++;
1148         }
1149 
1150         save_flags(flags);
1151         cli();
1152         portp->openwaitcnt++;
1153         if (! tty_hung_up_p(filp))
1154                 portp->refcount--;
1155 
1156         for (;;) {
1157                 if ((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0)
1158                         stl_setsignals(portp, 1, 1);
1159                 if (tty_hung_up_p(filp) ||
1160                     ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1161                         if (portp->flags & ASYNC_HUP_NOTIFY)
1162                                 rc = -EBUSY;
1163                         else
1164                                 rc = -ERESTARTSYS;
1165                         break;
1166                 }
1167                 if (((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) &&
1168                     ((portp->flags & ASYNC_CLOSING) == 0) &&
1169                     (doclocal || (portp->sigs & TIOCM_CD))) {
1170                         break;
1171                 }
1172                 if (signal_pending(current)) {
1173                         rc = -ERESTARTSYS;
1174                         break;
1175                 }
1176                 interruptible_sleep_on(&portp->open_wait);
1177         }
1178 
1179         if (! tty_hung_up_p(filp))
1180                 portp->refcount++;
1181         portp->openwaitcnt--;
1182         restore_flags(flags);
1183 
1184         return(rc);
1185 }
1186 
1187 /*****************************************************************************/
1188 
1189 static void stl_close(struct tty_struct *tty, struct file *filp)
1190 {
1191         stlport_t       *portp;
1192         unsigned long   flags;
1193 
1194 #if DEBUG
1195         printk("stl_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
1196 #endif
1197 
1198         portp = tty->driver_data;
1199         if (portp == (stlport_t *) NULL)
1200                 return;
1201 
1202         save_flags(flags);
1203         cli();
1204         if (tty_hung_up_p(filp)) {
1205                 MOD_DEC_USE_COUNT;
1206                 restore_flags(flags);
1207                 return;
1208         }
1209         if ((tty->count == 1) && (portp->refcount != 1))
1210                 portp->refcount = 1;
1211         if (portp->refcount-- > 1) {
1212                 MOD_DEC_USE_COUNT;
1213                 restore_flags(flags);
1214                 return;
1215         }
1216 
1217         portp->refcount = 0;
1218         portp->flags |= ASYNC_CLOSING;
1219 
1220         if (portp->flags & ASYNC_NORMAL_ACTIVE)
1221                 portp->normaltermios = *tty->termios;
1222         if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1223                 portp->callouttermios = *tty->termios;
1224 
1225 /*
1226  *      May want to wait for any data to drain before closing. The BUSY
1227  *      flag keeps track of whether we are still sending or not - it is
1228  *      very accurate for the cd1400, not quite so for the sc26198.
1229  *      (The sc26198 has no "end-of-data" interrupt only empty FIFO)
1230  */
1231         tty->closing = 1;
1232         if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1233                 tty_wait_until_sent(tty, portp->closing_wait);
1234         stl_waituntilsent(tty, (HZ / 2));
1235 
1236         portp->flags &= ~ASYNC_INITIALIZED;
1237         stl_disableintrs(portp);
1238         if (tty->termios->c_cflag & HUPCL)
1239                 stl_setsignals(portp, 0, 0);
1240         stl_enablerxtx(portp, 0, 0);
1241         stl_flushbuffer(tty);
1242         portp->istate = 0;
1243         if (portp->tx.buf != (char *) NULL) {
1244                 kfree(portp->tx.buf);
1245                 portp->tx.buf = (char *) NULL;
1246                 portp->tx.head = (char *) NULL;
1247                 portp->tx.tail = (char *) NULL;
1248         }
1249         set_bit(TTY_IO_ERROR, &tty->flags);
1250         if (tty->ldisc.flush_buffer)
1251                 (tty->ldisc.flush_buffer)(tty);
1252 
1253         tty->closing = 0;
1254         portp->tty = (struct tty_struct *) NULL;
1255 
1256         if (portp->openwaitcnt) {
1257                 if (portp->close_delay)
1258                         stl_delay(portp->close_delay);
1259                 wake_up_interruptible(&portp->open_wait);
1260         }
1261 
1262         portp->flags &= ~(ASYNC_CALLOUT_ACTIVE | ASYNC_NORMAL_ACTIVE |
1263                 ASYNC_CLOSING);
1264         wake_up_interruptible(&portp->close_wait);
1265         MOD_DEC_USE_COUNT;
1266         restore_flags(flags);
1267 }
1268 
1269 /*****************************************************************************/
1270 
1271 /*
1272  *      Wait for a specified delay period, this is not a busy-loop. It will
1273  *      give up the processor while waiting. Unfortunately this has some
1274  *      rather intimate knowledge of the process management stuff.
1275  */
1276 
1277 static void stl_delay(int len)
1278 {
1279 #if DEBUG
1280         printk("stl_delay(len=%d)\n", len);
1281 #endif
1282         if (len > 0) {
1283                 current->state = TASK_INTERRUPTIBLE;
1284                 schedule_timeout(len);
1285                 current->state = TASK_RUNNING;
1286         }
1287 }
1288 
1289 /*****************************************************************************/
1290 
1291 /*
1292  *      Write routine. Take data and stuff it in to the TX ring queue.
1293  *      If transmit interrupts are not running then start them.
1294  */
1295 
1296 static int stl_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
1297 {
1298         stlport_t       *portp;
1299         unsigned int    len, stlen;
1300         unsigned char   *chbuf;
1301         char            *head, *tail;
1302 
1303 #if DEBUG
1304         printk("stl_write(tty=%x,from_user=%d,buf=%x,count=%d)\n",
1305                 (int) tty, from_user, (int) buf, count);
1306 #endif
1307 
1308         if ((tty == (struct tty_struct *) NULL) ||
1309             (stl_tmpwritebuf == (char *) NULL))
1310                 return(0);
1311         portp = tty->driver_data;
1312         if (portp == (stlport_t *) NULL)
1313                 return(0);
1314         if (portp->tx.buf == (char *) NULL)
1315                 return(0);
1316 
1317 /*
1318  *      If copying direct from user space we must cater for page faults,
1319  *      causing us to "sleep" here for a while. To handle this copy in all
1320  *      the data we need now, into a local buffer. Then when we got it all
1321  *      copy it into the TX buffer.
1322  */
1323         chbuf = (unsigned char *) buf;
1324         if (from_user) {
1325                 head = portp->tx.head;
1326                 tail = portp->tx.tail;
1327                 len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) :
1328                         (tail - head - 1);
1329                 count = MIN(len, count);
1330                 
1331                 down(&stl_tmpwritesem);
1332                 copy_from_user(stl_tmpwritebuf, chbuf, count);
1333                 chbuf = &stl_tmpwritebuf[0];
1334         }
1335 
1336         head = portp->tx.head;
1337         tail = portp->tx.tail;
1338         if (head >= tail) {
1339                 len = STL_TXBUFSIZE - (head - tail) - 1;
1340                 stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
1341         } else {
1342                 len = tail - head - 1;
1343                 stlen = len;
1344         }
1345 
1346         len = MIN(len, count);
1347         count = 0;
1348         while (len > 0) {
1349                 stlen = MIN(len, stlen);
1350                 memcpy(head, chbuf, stlen);
1351                 len -= stlen;
1352                 chbuf += stlen;
1353                 count += stlen;
1354                 head += stlen;
1355                 if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
1356                         head = portp->tx.buf;
1357                         stlen = tail - head;
1358                 }
1359         }
1360         portp->tx.head = head;
1361 
1362         clear_bit(ASYI_TXLOW, &portp->istate);
1363         stl_startrxtx(portp, -1, 1);
1364 
1365         if (from_user)
1366                 up(&stl_tmpwritesem);
1367 
1368         return(count);
1369 }
1370 
1371 /*****************************************************************************/
1372 
1373 static void stl_putchar(struct tty_struct *tty, unsigned char ch)
1374 {
1375         stlport_t       *portp;
1376         unsigned int    len;
1377         char            *head, *tail;
1378 
1379 #if DEBUG
1380         printk("stl_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1381 #endif
1382 
1383         if (tty == (struct tty_struct *) NULL)
1384                 return;
1385         portp = tty->driver_data;
1386         if (portp == (stlport_t *) NULL)
1387                 return;
1388         if (portp->tx.buf == (char *) NULL)
1389                 return;
1390 
1391         head = portp->tx.head;
1392         tail = portp->tx.tail;
1393 
1394         len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
1395         len--;
1396 
1397         if (len > 0) {
1398                 *head++ = ch;
1399                 if (head >= (portp->tx.buf + STL_TXBUFSIZE))
1400                         head = portp->tx.buf;
1401         }       
1402         portp->tx.head = head;
1403 }
1404 
1405 /*****************************************************************************/
1406 
1407 /*
1408  *      If there are any characters in the buffer then make sure that TX
1409  *      interrupts are on and get'em out. Normally used after the putchar
1410  *      routine has been called.
1411  */
1412 
1413 static void stl_flushchars(struct tty_struct *tty)
1414 {
1415         stlport_t       *portp;
1416 
1417 #if DEBUG
1418         printk("stl_flushchars(tty=%x)\n", (int) tty);
1419 #endif
1420 
1421         if (tty == (struct tty_struct *) NULL)
1422                 return;
1423         portp = tty->driver_data;
1424         if (portp == (stlport_t *) NULL)
1425                 return;
1426         if (portp->tx.buf == (char *) NULL)
1427                 return;
1428 
1429 #if 0
1430         if (tty->stopped || tty->hw_stopped ||
1431             (portp->tx.head == portp->tx.tail))
1432                 return;
1433 #endif
1434         stl_startrxtx(portp, -1, 1);
1435 }
1436 
1437 /*****************************************************************************/
1438 
1439 static int stl_writeroom(struct tty_struct *tty)
1440 {
1441         stlport_t       *portp;
1442         char            *head, *tail;
1443 
1444 #if DEBUG
1445         printk("stl_writeroom(tty=%x)\n", (int) tty);
1446 #endif
1447 
1448         if (tty == (struct tty_struct *) NULL)
1449                 return(0);
1450         portp = tty->driver_data;
1451         if (portp == (stlport_t *) NULL)
1452                 return(0);
1453         if (portp->tx.buf == (char *) NULL)
1454                 return(0);
1455 
1456         head = portp->tx.head;
1457         tail = portp->tx.tail;
1458         return((head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1));
1459 }
1460 
1461 /*****************************************************************************/
1462 
1463 /*
1464  *      Return number of chars in the TX buffer. Normally we would just
1465  *      calculate the number of chars in the buffer and return that, but if
1466  *      the buffer is empty and TX interrupts are still on then we return
1467  *      that the buffer still has 1 char in it. This way whoever called us
1468  *      will not think that ALL chars have drained - since the UART still
1469  *      must have some chars in it (we are busy after all).
1470  */
1471 
1472 static int stl_charsinbuffer(struct tty_struct *tty)
1473 {
1474         stlport_t       *portp;
1475         unsigned int    size;
1476         char            *head, *tail;
1477 
1478 #if DEBUG
1479         printk("stl_charsinbuffer(tty=%x)\n", (int) tty);
1480 #endif
1481 
1482         if (tty == (struct tty_struct *) NULL)
1483                 return(0);
1484         portp = tty->driver_data;
1485         if (portp == (stlport_t *) NULL)
1486                 return(0);
1487         if (portp->tx.buf == (char *) NULL)
1488                 return(0);
1489 
1490         head = portp->tx.head;
1491         tail = portp->tx.tail;
1492         size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1493         if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
1494                 size = 1;
1495         return(size);
1496 }
1497 
1498 /*****************************************************************************/
1499 
1500 /*
1501  *      Generate the serial struct info.
1502  */
1503 
1504 static void stl_getserial(stlport_t *portp, struct serial_struct *sp)
1505 {
1506         struct serial_struct    sio;
1507         stlbrd_t                *brdp;
1508 
1509 #if DEBUG
1510         printk("stl_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1511 #endif
1512 
1513         memset(&sio, 0, sizeof(struct serial_struct));
1514         sio.line = portp->portnr;
1515         sio.port = portp->ioaddr;
1516         sio.flags = portp->flags;
1517         sio.baud_base = portp->baud_base;
1518         sio.close_delay = portp->close_delay;
1519         sio.closing_wait = portp->closing_wait;
1520         sio.custom_divisor = portp->custom_divisor;
1521         sio.hub6 = 0;
1522         if (portp->uartp == &stl_cd1400uart) {
1523                 sio.type = PORT_CIRRUS;
1524                 sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
1525         } else {
1526                 sio.type = PORT_UNKNOWN;
1527                 sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
1528         }
1529 
1530         brdp = stl_brds[portp->brdnr];
1531         if (brdp != (stlbrd_t *) NULL)
1532                 sio.irq = brdp->irq;
1533 
1534         copy_to_user(sp, &sio, sizeof(struct serial_struct));
1535 }
1536 
1537 /*****************************************************************************/
1538 
1539 /*
1540  *      Set port according to the serial struct info.
1541  *      At this point we do not do any auto-configure stuff, so we will
1542  *      just quietly ignore any requests to change irq, etc.
1543  */
1544 
1545 static int stl_setserial(stlport_t *portp, struct serial_struct *sp)
1546 {
1547         struct serial_struct    sio;
1548 
1549 #if DEBUG
1550         printk("stl_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1551 #endif
1552 
1553         copy_from_user(&sio, sp, sizeof(struct serial_struct));
1554         if (!capable(CAP_SYS_ADMIN)) {
1555                 if ((sio.baud_base != portp->baud_base) ||
1556                     (sio.close_delay != portp->close_delay) ||
1557                     ((sio.flags & ~ASYNC_USR_MASK) !=
1558                     (portp->flags & ~ASYNC_USR_MASK)))
1559                         return(-EPERM);
1560         } 
1561 
1562         portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
1563                 (sio.flags & ASYNC_USR_MASK);
1564         portp->baud_base = sio.baud_base;
1565         portp->close_delay = sio.close_delay;
1566         portp->closing_wait = sio.closing_wait;
1567         portp->custom_divisor = sio.custom_divisor;
1568         stl_setport(portp, portp->tty->termios);
1569         return(0);
1570 }
1571 
1572 /*****************************************************************************/
1573 
1574 static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1575 {
1576         stlport_t       *portp;
1577         unsigned int    ival;
1578         int             rc;
1579 
1580 #if DEBUG
1581         printk("stl_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
1582                 (int) tty, (int) file, cmd, (int) arg);
1583 #endif
1584 
1585         if (tty == (struct tty_struct *) NULL)
1586                 return(-ENODEV);
1587         portp = tty->driver_data;
1588         if (portp == (stlport_t *) NULL)
1589                 return(-ENODEV);
1590 
1591         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1592             (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
1593                 if (tty->flags & (1 << TTY_IO_ERROR))
1594                         return(-EIO);
1595         }
1596 
1597         rc = 0;
1598 
1599         switch (cmd) {
1600         case TIOCGSOFTCAR:
1601                 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1602                         (unsigned int *) arg);
1603                 break;
1604         case TIOCSSOFTCAR:
1605                 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1606                     sizeof(int))) == 0) {
1607                         get_user(ival, (unsigned int *) arg);
1608                         tty->termios->c_cflag =
1609                                 (tty->termios->c_cflag & ~CLOCAL) |
1610                                 (ival ? CLOCAL : 0);
1611                 }
1612                 break;
1613         case TIOCMGET:
1614                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1615                     sizeof(unsigned int))) == 0) {
1616                         ival = stl_getsignals(portp);
1617                         put_user(ival, (unsigned int *) arg);
1618                 }
1619                 break;
1620         case TIOCMBIS:
1621                 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1622                     sizeof(unsigned int))) == 0) {
1623                         get_user(ival, (unsigned int *) arg);
1624                         stl_setsignals(portp, ((ival & TIOCM_DTR) ? 1 : -1),
1625                                 ((ival & TIOCM_RTS) ? 1 : -1));
1626                 }
1627                 break;
1628         case TIOCMBIC:
1629                 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1630                     sizeof(unsigned int))) == 0) {
1631                         get_user(ival, (unsigned int *) arg);
1632                         stl_setsignals(portp, ((ival & TIOCM_DTR) ? 0 : -1),
1633                                 ((ival & TIOCM_RTS) ? 0 : -1));
1634                 }
1635                 break;
1636         case TIOCMSET:
1637                 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1638                     sizeof(unsigned int))) == 0) {
1639                         get_user(ival, (unsigned int *) arg);
1640                         stl_setsignals(portp, ((ival & TIOCM_DTR) ? 1 : 0),
1641                                 ((ival & TIOCM_RTS) ? 1 : 0));
1642                 }
1643                 break;
1644         case TIOCGSERIAL:
1645                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1646                     sizeof(struct serial_struct))) == 0)
1647                         stl_getserial(portp, (struct serial_struct *) arg);
1648                 break;
1649         case TIOCSSERIAL:
1650                 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1651                     sizeof(struct serial_struct))) == 0)
1652                         rc = stl_setserial(portp, (struct serial_struct *) arg);
1653                 break;
1654         case COM_GETPORTSTATS:
1655                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1656                     sizeof(comstats_t))) == 0)
1657                         rc = stl_getportstats(portp, (comstats_t *) arg);
1658                 break;
1659         case COM_CLRPORTSTATS:
1660                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1661                     sizeof(comstats_t))) == 0)
1662                         rc = stl_clrportstats(portp, (comstats_t *) arg);
1663                 break;
1664         case TIOCSERCONFIG:
1665         case TIOCSERGWILD:
1666         case TIOCSERSWILD:
1667         case TIOCSERGETLSR:
1668         case TIOCSERGSTRUCT:
1669         case TIOCSERGETMULTI:
1670         case TIOCSERSETMULTI:
1671         default:
1672                 rc = -ENOIOCTLCMD;
1673                 break;
1674         }
1675 
1676         return(rc);
1677 }
1678 
1679 /*****************************************************************************/
1680 
1681 static void stl_settermios(struct tty_struct *tty, struct termios *old)
1682 {
1683         stlport_t       *portp;
1684         struct termios  *tiosp;
1685 
1686 #if DEBUG
1687         printk("stl_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
1688 #endif
1689 
1690         if (tty == (struct tty_struct *) NULL)
1691                 return;
1692         portp = tty->driver_data;
1693         if (portp == (stlport_t *) NULL)
1694                 return;
1695 
1696         tiosp = tty->termios;
1697         if ((tiosp->c_cflag == old->c_cflag) &&
1698             (tiosp->c_iflag == old->c_iflag))
1699                 return;
1700 
1701         stl_setport(portp, tiosp);
1702         stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
1703                 -1);
1704         if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
1705                 tty->hw_stopped = 0;
1706                 stl_start(tty);
1707         }
1708         if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1709                 wake_up_interruptible(&portp->open_wait);
1710 }
1711 
1712 /*****************************************************************************/
1713 
1714 /*
1715  *      Attempt to flow control who ever is sending us data. Based on termios
1716  *      settings use software or/and hardware flow control.
1717  */
1718 
1719 static void stl_throttle(struct tty_struct *tty)
1720 {
1721         stlport_t       *portp;
1722 
1723 #if DEBUG
1724         printk("stl_throttle(tty=%x)\n", (int) tty);
1725 #endif
1726 
1727         if (tty == (struct tty_struct *) NULL)
1728                 return;
1729         portp = tty->driver_data;
1730         if (portp == (stlport_t *) NULL)
1731                 return;
1732         stl_flowctrl(portp, 0);
1733 }
1734 
1735 /*****************************************************************************/
1736 
1737 /*
1738  *      Unflow control the device sending us data...
1739  */
1740 
1741 static void stl_unthrottle(struct tty_struct *tty)
1742 {
1743         stlport_t       *portp;
1744 
1745 #if DEBUG
1746         printk("stl_unthrottle(tty=%x)\n", (int) tty);
1747 #endif
1748 
1749         if (tty == (struct tty_struct *) NULL)
1750                 return;
1751         portp = tty->driver_data;
1752         if (portp == (stlport_t *) NULL)
1753                 return;
1754         stl_flowctrl(portp, 1);
1755 }
1756 
1757 /*****************************************************************************/
1758 
1759 /*
1760  *      Stop the transmitter. Basically to do this we will just turn TX
1761  *      interrupts off.
1762  */
1763 
1764 static void stl_stop(struct tty_struct *tty)
1765 {
1766         stlport_t       *portp;
1767 
1768 #if DEBUG
1769         printk("stl_stop(tty=%x)\n", (int) tty);
1770 #endif
1771 
1772         if (tty == (struct tty_struct *) NULL)
1773                 return;
1774         portp = tty->driver_data;
1775         if (portp == (stlport_t *) NULL)
1776                 return;
1777         stl_startrxtx(portp, -1, 0);
1778 }
1779 
1780 /*****************************************************************************/
1781 
1782 /*
1783  *      Start the transmitter again. Just turn TX interrupts back on.
1784  */
1785 
1786 static void stl_start(struct tty_struct *tty)
1787 {
1788         stlport_t       *portp;
1789 
1790 #if DEBUG
1791         printk("stl_start(tty=%x)\n", (int) tty);
1792 #endif
1793 
1794         if (tty == (struct tty_struct *) NULL)
1795                 return;
1796         portp = tty->driver_data;
1797         if (portp == (stlport_t *) NULL)
1798                 return;
1799         stl_startrxtx(portp, -1, 1);
1800 }
1801 
1802 /*****************************************************************************/
1803 
1804 /*
1805  *      Hangup this port. This is pretty much like closing the port, only
1806  *      a little more brutal. No waiting for data to drain. Shutdown the
1807  *      port and maybe drop signals.
1808  */
1809 
1810 static void stl_hangup(struct tty_struct *tty)
1811 {
1812         stlport_t       *portp;
1813 
1814 #if DEBUG
1815         printk("stl_hangup(tty=%x)\n", (int) tty);
1816 #endif
1817 
1818         if (tty == (struct tty_struct *) NULL)
1819                 return;
1820         portp = tty->driver_data;
1821         if (portp == (stlport_t *) NULL)
1822                 return;
1823 
1824         portp->flags &= ~ASYNC_INITIALIZED;
1825         stl_disableintrs(portp);
1826         if (tty->termios->c_cflag & HUPCL)
1827                 stl_setsignals(portp, 0, 0);
1828         stl_enablerxtx(portp, 0, 0);
1829         stl_flushbuffer(tty);
1830         portp->istate = 0;
1831         set_bit(TTY_IO_ERROR, &tty->flags);
1832         if (portp->tx.buf != (char *) NULL) {
1833                 kfree(portp->tx.buf);
1834                 portp->tx.buf = (char *) NULL;
1835                 portp->tx.head = (char *) NULL;
1836                 portp->tx.tail = (char *) NULL;
1837         }
1838         portp->tty = (struct tty_struct *) NULL;
1839         portp->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
1840         portp->refcount = 0;
1841         wake_up_interruptible(&portp->open_wait);
1842 }
1843 
1844 /*****************************************************************************/
1845 
1846 static void stl_flushbuffer(struct tty_struct *tty)
1847 {
1848         stlport_t       *portp;
1849 
1850 #if DEBUG
1851         printk("stl_flushbuffer(tty=%x)\n", (int) tty);
1852 #endif
1853 
1854         if (tty == (struct tty_struct *) NULL)
1855                 return;
1856         portp = tty->driver_data;
1857         if (portp == (stlport_t *) NULL)
1858                 return;
1859 
1860         stl_flush(portp);
1861         wake_up_interruptible(&tty->write_wait);
1862         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1863             tty->ldisc.write_wakeup)
1864                 (tty->ldisc.write_wakeup)(tty);
1865 }
1866 
1867 /*****************************************************************************/
1868 
1869 static void stl_breakctl(struct tty_struct *tty, int state)
1870 {
1871         stlport_t       *portp;
1872 
1873 #if DEBUG
1874         printk("stl_breakctl(tty=%x,state=%d)\n", (int) tty, state);
1875 #endif
1876 
1877         if (tty == (struct tty_struct *) NULL)
1878                 return;
1879         portp = tty->driver_data;
1880         if (portp == (stlport_t *) NULL)
1881                 return;
1882 
1883         stl_sendbreak(portp, ((state == -1) ? 1 : 2));
1884 }
1885 
1886 /*****************************************************************************/
1887 
1888 static void stl_waituntilsent(struct tty_struct *tty, int timeout)
1889 {
1890         stlport_t       *portp;
1891         unsigned long   tend;
1892 
1893 #if DEBUG
1894         printk("stl_waituntilsent(tty=%x,timeout=%d)\n", (int) tty, timeout);
1895 #endif
1896 
1897         if (tty == (struct tty_struct *) NULL)
1898                 return;
1899         portp = tty->driver_data;
1900         if (portp == (stlport_t *) NULL)
1901                 return;
1902 
1903         if (timeout == 0)
1904                 timeout = HZ;
1905         tend = jiffies + timeout;
1906 
1907         while (stl_datastate(portp)) {
1908                 if (signal_pending(current))
1909                         break;
1910                 stl_delay(2);
1911                 if (time_after_eq(jiffies, tend))
1912                         break;
1913         }
1914 }
1915 
1916 /*****************************************************************************/
1917 
1918 static void stl_sendxchar(struct tty_struct *tty, char ch)
1919 {
1920         stlport_t       *portp;
1921 
1922 #if DEBUG
1923         printk("stl_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
1924 #endif
1925 
1926         if (tty == (struct tty_struct *) NULL)
1927                 return;
1928         portp = tty->driver_data;
1929         if (portp == (stlport_t *) NULL)
1930                 return;
1931 
1932         if (ch == STOP_CHAR(tty))
1933                 stl_sendflow(portp, 0);
1934         else if (ch == START_CHAR(tty))
1935                 stl_sendflow(portp, 1);
1936         else
1937                 stl_putchar(tty, ch);
1938 }
1939 
1940 /*****************************************************************************/
1941 
1942 #define MAXLINE         80
1943 
1944 /*
1945  *      Format info for a specified port. The line is deliberately limited
1946  *      to 80 characters. (If it is too long it will be truncated, if too
1947  *      short then padded with spaces).
1948  */
1949 
1950 static int stl_portinfo(stlport_t *portp, int portnr, char *pos)
1951 {
1952         char    *sp;
1953         int     sigs, cnt;
1954 
1955         sp = pos;
1956         sp += sprintf(sp, "%d: uart:%s tx:%d rx:%d",
1957                 portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
1958                 (int) portp->stats.txtotal, (int) portp->stats.rxtotal);
1959 
1960         if (portp->stats.rxframing)
1961                 sp += sprintf(sp, " fe:%d", (int) portp->stats.rxframing);
1962         if (portp->stats.rxparity)
1963                 sp += sprintf(sp, " pe:%d", (int) portp->stats.rxparity);
1964         if (portp->stats.rxbreaks)
1965                 sp += sprintf(sp, " brk:%d", (int) portp->stats.rxbreaks);
1966         if (portp->stats.rxoverrun)
1967                 sp += sprintf(sp, " oe:%d", (int) portp->stats.rxoverrun);
1968 
1969         sigs = stl_getsignals(portp);
1970         cnt = sprintf(sp, "%s%s%s%s%s ",
1971                 (sigs & TIOCM_RTS) ? "|RTS" : "",
1972                 (sigs & TIOCM_CTS) ? "|CTS" : "",
1973                 (sigs & TIOCM_DTR) ? "|DTR" : "",
1974                 (sigs & TIOCM_CD) ? "|DCD" : "",
1975                 (sigs & TIOCM_DSR) ? "|DSR" : "");
1976         *sp = ' ';
1977         sp += cnt;
1978 
1979         for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
1980                 *sp++ = ' ';
1981         if (cnt >= MAXLINE)
1982                 pos[(MAXLINE - 2)] = '+';
1983         pos[(MAXLINE - 1)] = '\n';
1984 
1985         return(MAXLINE);
1986 }
1987 
1988 /*****************************************************************************/
1989 
1990 /*
1991  *      Port info, read from the /proc file system.
1992  */
1993 
1994 static int stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
1995 {
1996         stlbrd_t        *brdp;
1997         stlpanel_t      *panelp;
1998         stlport_t       *portp;
1999         int             brdnr, panelnr, portnr, totalport;
2000         int             curoff, maxoff;
2001         char            *pos;
2002 
2003 #if DEBUG
2004         printk("stl_readproc(page=%x,start=%x,off=%x,count=%d,eof=%x,"
2005                 "data=%x\n", (int) page, (int) start, (int) off, count,
2006                 (int) eof, (int) data);
2007 #endif
2008 
2009         pos = page;
2010         totalport = 0;
2011         curoff = 0;
2012 
2013         if (off == 0) {
2014                 pos += sprintf(pos, "%s: version %s", stl_drvtitle,
2015                         stl_drvversion);
2016                 while (pos < (page + MAXLINE - 1))
2017                         *pos++ = ' ';
2018                 *pos++ = '\n';
2019         }
2020         curoff =  MAXLINE;
2021 
2022 /*
2023  *      We scan through for each board, panel and port. The offset is
2024  *      calculated on the fly, and irrelevant ports are skipped.
2025  */
2026         for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
2027                 brdp = stl_brds[brdnr];
2028                 if (brdp == (stlbrd_t *) NULL)
2029                         continue;
2030                 if (brdp->state == 0)
2031                         continue;
2032 
2033                 maxoff = curoff + (brdp->nrports * MAXLINE);
2034                 if (off >= maxoff) {
2035                         curoff = maxoff;
2036                         continue;
2037                 }
2038 
2039                 totalport = brdnr * STL_MAXPORTS;
2040                 for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
2041                         panelp = brdp->panels[panelnr];
2042                         if (panelp == (stlpanel_t *) NULL)
2043                                 continue;
2044 
2045                         maxoff = curoff + (panelp->nrports * MAXLINE);
2046                         if (off >= maxoff) {
2047                                 curoff = maxoff;
2048                                 totalport += panelp->nrports;
2049                                 continue;
2050                         }
2051 
2052                         for (portnr = 0; (portnr < panelp->nrports); portnr++,
2053                             totalport++) {
2054                                 portp = panelp->ports[portnr];
2055                                 if (portp == (stlport_t *) NULL)
2056                                         continue;
2057                                 if (off >= (curoff += MAXLINE))
2058                                         continue;
2059                                 if ((pos - page + MAXLINE) > count)
2060                                         goto stl_readdone;
2061                                 pos += stl_portinfo(portp, totalport, pos);
2062                         }
2063                 }
2064         }
2065 
2066         *eof = 1;
2067 
2068 stl_readdone:
2069         *start = page;
2070         return(pos - page);
2071 }
2072 
2073 /*****************************************************************************/
2074 
2075 /*
2076  *      All board interrupts are vectored through here first. This code then
2077  *      calls off to the approrpriate board interrupt handlers.
2078  */
2079 
2080 static void stl_intr(int irq, void *dev_id, struct pt_regs *regs)
2081 {
2082         stlbrd_t        *brdp;
2083         int             i;
2084 
2085 #if DEBUG
2086         printk("stl_intr(irq=%d,regs=%x)\n", irq, (int) regs);
2087 #endif
2088 
2089         for (i = 0; (i < stl_nrbrds); i++) {
2090                 if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL)
2091                         continue;
2092                 if (brdp->state == 0)
2093                         continue;
2094                 (* brdp->isr)(brdp);
2095         }
2096 }
2097 
2098 /*****************************************************************************/
2099 
2100 /*
2101  *      Interrupt service routine for EasyIO board types.
2102  */
2103 
2104 static void stl_eiointr(stlbrd_t *brdp)
2105 {
2106         stlpanel_t      *panelp;
2107         unsigned int    iobase;
2108 
2109         panelp = brdp->panels[0];
2110         iobase = panelp->iobase;
2111         while (inb(brdp->iostatus) & EIO_INTRPEND)
2112                 (* panelp->isr)(panelp, iobase);
2113 }
2114 
2115 /*****************************************************************************/
2116 
2117 /*
2118  *      Interrupt service routine for ECH-AT board types.
2119  */
2120 
2121 static void stl_echatintr(stlbrd_t *brdp)
2122 {
2123         stlpanel_t      *panelp;
2124         unsigned int    ioaddr;
2125         int             bnknr;
2126 
2127         outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
2128 
2129         while (inb(brdp->iostatus) & ECH_INTRPEND) {
2130                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2131                         ioaddr = brdp->bnkstataddr[bnknr];
2132                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
2133                                 panelp = brdp->bnk2panel[bnknr];
2134                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2135                         }
2136                 }
2137         }
2138 
2139         outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2140 }
2141 
2142 /*****************************************************************************/
2143 
2144 /*
2145  *      Interrupt service routine for ECH-MCA board types.
2146  */
2147 
2148 static void stl_echmcaintr(stlbrd_t *brdp)
2149 {
2150         stlpanel_t      *panelp;
2151         unsigned int    ioaddr;
2152         int             bnknr;
2153 
2154         while (inb(brdp->iostatus) & ECH_INTRPEND) {
2155                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2156                         ioaddr = brdp->bnkstataddr[bnknr];
2157                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
2158                                 panelp = brdp->bnk2panel[bnknr];
2159                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2160                         }
2161                 }
2162         }
2163 }
2164 
2165 /*****************************************************************************/
2166 
2167 /*
2168  *      Interrupt service routine for ECH-PCI board types.
2169  */
2170 
2171 static void stl_echpciintr(stlbrd_t *brdp)
2172 {
2173         stlpanel_t      *panelp;
2174         unsigned int    ioaddr;
2175         int             bnknr, recheck;
2176 
2177         while (1) {
2178                 recheck = 0;
2179                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2180                         outb(brdp->bnkpageaddr[bnknr], brdp->ioctrl);
2181                         ioaddr = brdp->bnkstataddr[bnknr];
2182                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
2183                                 panelp = brdp->bnk2panel[bnknr];
2184                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2185                                 recheck++;
2186                         }
2187                 }
2188                 if (! recheck)
2189                         break;
2190         }
2191 }
2192 
2193 /*****************************************************************************/
2194 
2195 /*
2196  *      Interrupt service routine for ECH-8/64-PCI board types.
2197  */
2198 
2199 static void stl_echpci64intr(stlbrd_t *brdp)
2200 {
2201         stlpanel_t      *panelp;
2202         unsigned int    ioaddr;
2203         int             bnknr;
2204 
2205         while (inb(brdp->ioctrl) & 0x1) {
2206                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2207                         ioaddr = brdp->bnkstataddr[bnknr];
2208                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
2209                                 panelp = brdp->bnk2panel[bnknr];
2210                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2211                         }
2212                 }
2213         }
2214 }
2215 
2216 /*****************************************************************************/
2217 
2218 /*
2219  *      Service an off-level request for some channel.
2220  */
2221 static void stl_offintr(void *private)
2222 {
2223         stlport_t               *portp;
2224         struct tty_struct       *tty;
2225         unsigned int            oldsigs;
2226 
2227         portp = private;
2228 
2229 #if DEBUG
2230         printk("stl_offintr(portp=%x)\n", (int) portp);
2231 #endif
2232 
2233         if (portp == (stlport_t *) NULL)
2234                 goto out;
2235 
2236         tty = portp->tty;
2237         if (tty == (struct tty_struct *) NULL)
2238                 goto out;
2239 
2240         lock_kernel();
2241         if (test_bit(ASYI_TXLOW, &portp->istate)) {
2242                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2243                     tty->ldisc.write_wakeup)
2244                         (tty->ldisc.write_wakeup)(tty);
2245                 wake_up_interruptible(&tty->write_wait);
2246         }
2247         if (test_bit(ASYI_DCDCHANGE, &portp->istate)) {
2248                 clear_bit(ASYI_DCDCHANGE, &portp->istate);
2249                 oldsigs = portp->sigs;
2250                 portp->sigs = stl_getsignals(portp);
2251                 if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
2252                         wake_up_interruptible(&portp->open_wait);
2253                 if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0)) {
2254                         if (portp->flags & ASYNC_CHECK_CD) {
2255                                 if (! ((portp->flags & ASYNC_CALLOUT_ACTIVE) &&
2256                                     (portp->flags & ASYNC_CALLOUT_NOHUP))) {
2257                                         tty_hangup(tty);        /* FIXME: module removal race here - AKPM */
2258                                 }
2259                         }
2260                 }
2261         }
2262         unlock_kernel();
2263 out:
2264         MOD_DEC_USE_COUNT;
2265 }
2266 
2267 /*****************************************************************************/
2268 
2269 /*
2270  *      Map in interrupt vector to this driver. Check that we don't
2271  *      already have this vector mapped, we might be sharing this
2272  *      interrupt across multiple boards.
2273  */
2274 
2275 static int __init stl_mapirq(int irq, char *name)
2276 {
2277         int     rc, i;
2278 
2279 #if DEBUG
2280         printk("stl_mapirq(irq=%d,name=%s)\n", irq, name);
2281 #endif
2282 
2283         rc = 0;
2284         for (i = 0; (i < stl_numintrs); i++) {
2285                 if (stl_gotintrs[i] == irq)
2286                         break;
2287         }
2288         if (i >= stl_numintrs) {
2289                 if (request_irq(irq, stl_intr, SA_SHIRQ, name, NULL) != 0) {
2290                         printk("STALLION: failed to register interrupt "
2291                                 "routine for %s irq=%d\n", name, irq);
2292                         rc = -ENODEV;
2293                 } else {
2294                         stl_gotintrs[stl_numintrs++] = irq;
2295                 }
2296         }
2297         return(rc);
2298 }
2299 
2300 /*****************************************************************************/
2301 
2302 /*
2303  *      Initialize all the ports on a panel.
2304  */
2305 
2306 static int __init stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
2307 {
2308         stlport_t       *portp;
2309         int             chipmask, i;
2310 
2311 #if DEBUG
2312         printk("stl_initports(brdp=%x,panelp=%x)\n", (int) brdp, (int) panelp);
2313 #endif
2314 
2315         chipmask = stl_panelinit(brdp, panelp);
2316 
2317 /*
2318  *      All UART's are initialized (if found!). Now go through and setup
2319  *      each ports data structures.
2320  */
2321         for (i = 0; (i < panelp->nrports); i++) {
2322                 portp = (stlport_t *) stl_memalloc(sizeof(stlport_t));
2323                 if (portp == (stlport_t *) NULL) {
2324                         printk("STALLION: failed to allocate memory "
2325                                 "(size=%d)\n", sizeof(stlport_t));
2326                         break;
2327                 }
2328                 memset(portp, 0, sizeof(stlport_t));
2329 
2330                 portp->magic = STL_PORTMAGIC;
2331                 portp->portnr = i;
2332                 portp->brdnr = panelp->brdnr;
2333                 portp->panelnr = panelp->panelnr;
2334                 portp->uartp = panelp->uartp;
2335                 portp->clk = brdp->clk;
2336                 portp->baud_base = STL_BAUDBASE;
2337                 portp->close_delay = STL_CLOSEDELAY;
2338                 portp->closing_wait = 30 * HZ;
2339                 portp->normaltermios = stl_deftermios;
2340                 portp->callouttermios = stl_deftermios;
2341                 portp->tqueue.routine = stl_offintr;
2342                 portp->tqueue.data = portp;
2343                 init_waitqueue_head(&portp->open_wait);
2344                 init_waitqueue_head(&portp->close_wait);
2345                 portp->stats.brd = portp->brdnr;
2346                 portp->stats.panel = portp->panelnr;
2347                 portp->stats.port = portp->portnr;
2348                 panelp->ports[i] = portp;
2349                 stl_portinit(brdp, panelp, portp);
2350         }
2351 
2352         return(0);
2353 }
2354 
2355 /*****************************************************************************/
2356 
2357 /*
2358  *      Try to find and initialize an EasyIO board.
2359  */
2360 
2361 static inline int stl_initeio(stlbrd_t *brdp)
2362 {
2363         stlpanel_t      *panelp;
2364         unsigned int    status;
2365         char            *name;
2366         int             rc;
2367 
2368 #if DEBUG
2369         printk("stl_initeio(brdp=%x)\n", (int) brdp);
2370 #endif
2371 
2372         brdp->ioctrl = brdp->ioaddr1 + 1;
2373         brdp->iostatus = brdp->ioaddr1 + 2;
2374 
2375         status = inb(brdp->iostatus);
2376         if ((status & EIO_IDBITMASK) == EIO_MK3)
2377                 brdp->ioctrl++;
2378 
2379 /*
2380  *      Handle board specific stuff now. The real difference is PCI
2381  *      or not PCI.
2382  */
2383         if (brdp->brdtype == BRD_EASYIOPCI) {
2384                 brdp->iosize1 = 0x80;
2385                 brdp->iosize2 = 0x80;
2386                 name = "serial(EIO-PCI)";
2387                 outb(0x41, (brdp->ioaddr2 + 0x4c));
2388         } else {
2389                 brdp->iosize1 = 8;
2390                 name = "serial(EIO)";
2391                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2392                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2393                         printk("STALLION: invalid irq=%d for brd=%d\n",
2394                                 brdp->irq, brdp->brdnr);
2395                         return(-EINVAL);
2396                 }
2397                 outb((stl_vecmap[brdp->irq] | EIO_0WS |
2398                         ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)),
2399                         brdp->ioctrl);
2400         }
2401 
2402         if (check_region(brdp->ioaddr1, brdp->iosize1)) {
2403                 printk("STALLION: Warning, board %d I/O address %x conflicts "
2404                         "with another device\n", brdp->brdnr, brdp->ioaddr1);
2405         }
2406         if (brdp->iosize2 > 0) {
2407                 if (check_region(brdp->ioaddr2, brdp->iosize2)) {
2408                         printk("STALLION: Warning, board %d I/O address %x "
2409                                 "conflicts with another device\n",
2410                                 brdp->brdnr, brdp->ioaddr2);
2411                 }
2412         }
2413  
2414 /*
2415  *      Everything looks OK, so let's go ahead and probe for the hardware.
2416  */
2417         brdp->clk = CD1400_CLK;
2418         brdp->isr = stl_eiointr;
2419 
2420         switch (status & EIO_IDBITMASK) {
2421         case EIO_8PORTM:
2422                 brdp->clk = CD1400_CLK8M;
2423                 /* fall thru */
2424         case EIO_8PORTRS:
2425         case EIO_8PORTDI:
2426                 brdp->nrports = 8;
2427                 break;
2428         case EIO_4PORTRS:
2429                 brdp->nrports = 4;
2430                 break;
2431         case EIO_MK3:
2432                 switch (status & EIO_BRDMASK) {
2433                 case ID_BRD4:
2434                         brdp->nrports = 4;
2435                         break;
2436                 case ID_BRD8:
2437                         brdp->nrports = 8;
2438                         break;
2439                 case ID_BRD16:
2440                         brdp->nrports = 16;
2441                         break;
2442                 default:
2443                         return(-ENODEV);
2444                 }
2445                 break;
2446         default:
2447                 return(-ENODEV);
2448         }
2449 
2450 /*
2451  *      We have verfied that the board is actually present, so now we
2452  *      can complete the setup.
2453  */
2454         request_region(brdp->ioaddr1, brdp->iosize1, name);
2455         if (brdp->iosize2 > 0)
2456                 request_region(brdp->ioaddr2, brdp->iosize2, name);
2457 
2458         panelp = (stlpanel_t *) stl_memalloc(sizeof(stlpanel_t));
2459         if (panelp == (stlpanel_t *) NULL) {
2460                 printk("STALLION: failed to allocate memory (size=%d)\n",
2461                         sizeof(stlpanel_t));
2462                 return(-ENOMEM);
2463         }
2464         memset(panelp, 0, sizeof(stlpanel_t));
2465 
2466         panelp->magic = STL_PANELMAGIC;
2467         panelp->brdnr = brdp->brdnr;
2468         panelp->panelnr = 0;
2469         panelp->nrports = brdp->nrports;
2470         panelp->iobase = brdp->ioaddr1;
2471         panelp->hwid = status;
2472         if ((status & EIO_IDBITMASK) == EIO_MK3) {
2473                 panelp->uartp = (void *) &stl_sc26198uart;
2474                 panelp->isr = stl_sc26198intr;
2475         } else {
2476                 panelp->uartp = (void *) &stl_cd1400uart;
2477                 panelp->isr = stl_cd1400eiointr;
2478         }
2479 
2480         brdp->panels[0] = panelp;
2481         brdp->nrpanels = 1;
2482         brdp->state |= BRD_FOUND;
2483         brdp->hwid = status;
2484         rc = stl_mapirq(brdp->irq, name);
2485         return(rc);
2486 }
2487 
2488 /*****************************************************************************/
2489 
2490 /*
2491  *      Try to find an ECH board and initialize it. This code is capable of
2492  *      dealing with all types of ECH board.
2493  */
2494 
2495 static int inline stl_initech(stlbrd_t *brdp)
2496 {
2497         stlpanel_t      *panelp;
2498         unsigned int    status, nxtid, ioaddr, conflict;
2499         int             panelnr, banknr, i;
2500         char            *name;
2501 
2502 #if DEBUG
2503         printk("stl_initech(brdp=%x)\n", (int) brdp);
2504 #endif
2505 
2506         status = 0;
2507         conflict = 0;
2508 
2509 /*
2510  *      Set up the initial board register contents for boards. This varies a
2511  *      bit between the different board types. So we need to handle each
2512  *      separately. Also do a check that the supplied IRQ is good.
2513  */
2514         switch (brdp->brdtype) {
2515 
2516         case BRD_ECH:
2517                 brdp->isr = stl_echatintr;
2518                 brdp->ioctrl = brdp->ioaddr1 + 1;
2519                 brdp->iostatus = brdp->ioaddr1 + 1;
2520                 status = inb(brdp->iostatus);
2521                 if ((status & ECH_IDBITMASK) != ECH_ID)
2522                         return(-ENODEV);
2523                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2524                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2525                         printk("STALLION: invalid irq=%d for brd=%d\n",
2526                                 brdp->irq, brdp->brdnr);
2527                         return(-EINVAL);
2528                 }
2529                 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2530                 status |= (stl_vecmap[brdp->irq] << 1);
2531                 outb((status | ECH_BRDRESET), brdp->ioaddr1);
2532                 brdp->ioctrlval = ECH_INTENABLE |
2533                         ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2534                 for (i = 0; (i < 10); i++)
2535                         outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
2536                 brdp->iosize1 = 2;
2537                 brdp->iosize2 = 32;
2538                 name = "serial(EC8/32)";
2539                 outb(status, brdp->ioaddr1);
2540                 break;
2541 
2542         case BRD_ECHMC:
2543                 brdp->isr = stl_echmcaintr;
2544                 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2545                 brdp->iostatus = brdp->ioctrl;
2546                 status = inb(brdp->iostatus);
2547                 if ((status & ECH_IDBITMASK) != ECH_ID)
2548                         return(-ENODEV);
2549                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2550                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2551                         printk("STALLION: invalid irq=%d for brd=%d\n",
2552                                 brdp->irq, brdp->brdnr);
2553                         return(-EINVAL);
2554                 }
2555                 outb(ECHMC_BRDRESET, brdp->ioctrl);
2556                 outb(ECHMC_INTENABLE, brdp->ioctrl);
2557                 brdp->iosize1 = 64;
2558                 name = "serial(EC8/32-MC)";
2559                 break;
2560 
2561         case BRD_ECHPCI:
2562                 brdp->isr = stl_echpciintr;
2563                 brdp->ioctrl = brdp->ioaddr1 + 2;
2564                 brdp->iosize1 = 4;
2565                 brdp->iosize2 = 8;
2566                 name = "serial(EC8/32-PCI)";
2567                 break;
2568 
2569         case BRD_ECH64PCI:
2570                 brdp->isr = stl_echpci64intr;
2571                 brdp->ioctrl = brdp->ioaddr2 + 0x40;
2572                 outb(0x43, (brdp->ioaddr1 + 0x4c));
2573                 brdp->iosize1 = 0x80;
2574                 brdp->iosize2 = 0x80;
2575                 name = "serial(EC8/64-PCI)";
2576                 break;
2577 
2578         default:
2579                 printk("STALLION: unknown board type=%d\n", brdp->brdtype);
2580                 return(-EINVAL);
2581                 break;
2582         }
2583 
2584 /*
2585  *      Check boards for possible IO address conflicts. We won't actually
2586  *      do anything about it here, just issue a warning...
2587  */
2588         conflict = check_region(brdp->ioaddr1, brdp->iosize1) ?
2589                 brdp->ioaddr1 : 0;
2590         if ((conflict == 0) && (brdp->iosize2 > 0))
2591                 conflict = check_region(brdp->ioaddr2, brdp->iosize2) ?
2592                         brdp->ioaddr2 : 0;
2593         if (conflict) {
2594                 printk("STALLION: Warning, board %d I/O address %x conflicts "
2595                         "with another device\n", brdp->brdnr, conflict);
2596         }
2597 
2598         request_region(brdp->ioaddr1, brdp->iosize1, name);
2599         if (brdp->iosize2 > 0)
2600                 request_region(brdp->ioaddr2, brdp->iosize2, name);
2601 
2602 /*
2603  *      Scan through the secondary io address space looking for panels.
2604  *      As we find'em allocate and initialize panel structures for each.
2605  */
2606         brdp->clk = CD1400_CLK;
2607         brdp->hwid = status;
2608 
2609         ioaddr = brdp->ioaddr2;
2610         banknr = 0;
2611         panelnr = 0;
2612         nxtid = 0;
2613 
2614         for (i = 0; (i < STL_MAXPANELS); i++) {
2615                 if (brdp->brdtype == BRD_ECHPCI) {
2616                         outb(nxtid, brdp->ioctrl);
2617                         ioaddr = brdp->ioaddr2;
2618                 }
2619                 status = inb(ioaddr + ECH_PNLSTATUS);
2620                 if ((status & ECH_PNLIDMASK) != nxtid)
2621                         break;
2622                 panelp = (stlpanel_t *) stl_memalloc(sizeof(stlpanel_t));
2623                 if (panelp == (stlpanel_t *) NULL) {
2624                         printk("STALLION: failed to allocate memory "
2625                                 "(size=%d)\n", sizeof(stlpanel_t));
2626                         break;
2627                 }
2628                 memset(panelp, 0, sizeof(stlpanel_t));
2629                 panelp->magic = STL_PANELMAGIC;
2630                 panelp->brdnr = brdp->brdnr;
2631                 panelp->panelnr = panelnr;
2632                 panelp->iobase = ioaddr;
2633                 panelp->pagenr = nxtid;
2634                 panelp->hwid = status;
2635                 brdp->bnk2panel[banknr] = panelp;
2636                 brdp->bnkpageaddr[banknr] = nxtid;
2637                 brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
2638 
2639                 if (status & ECH_PNLXPID) {
2640                         panelp->uartp = (void *) &stl_sc26198uart;
2641                         panelp->isr = stl_sc26198intr;
2642                         if (status & ECH_PNL16PORT) {
2643                                 panelp->nrports = 16;
2644                                 brdp->bnk2panel[banknr] = panelp;
2645                                 brdp->bnkpageaddr[banknr] = nxtid;
2646                                 brdp->bnkstataddr[banknr++] = ioaddr + 4 +
2647                                         ECH_PNLSTATUS;
2648                         } else {
2649                                 panelp->nrports = 8;
2650                         }
2651                 } else {
2652                         panelp->uartp = (void *) &stl_cd1400uart;
2653                         panelp->isr = stl_cd1400echintr;
2654                         if (status & ECH_PNL16PORT) {
2655                                 panelp->nrports = 16;
2656                                 panelp->ackmask = 0x80;
2657                                 if (brdp->brdtype != BRD_ECHPCI)
2658                                         ioaddr += EREG_BANKSIZE;
2659                                 brdp->bnk2panel[banknr] = panelp;
2660                                 brdp->bnkpageaddr[banknr] = ++nxtid;
2661                                 brdp->bnkstataddr[banknr++] = ioaddr +
2662                                         ECH_PNLSTATUS;
2663                         } else {
2664                                 panelp->nrports = 8;
2665                                 panelp->ackmask = 0xc0;
2666                         }
2667                 }
2668 
2669                 nxtid++;
2670                 ioaddr += EREG_BANKSIZE;
2671                 brdp->nrports += panelp->nrports;
2672                 brdp->panels[panelnr++] = panelp;
2673                 if ((brdp->brdtype != BRD_ECHPCI) &&
2674                     (ioaddr >= (brdp->ioaddr2 + brdp->iosize2)))
2675                         break;
2676         }
2677 
2678         brdp->nrpanels = panelnr;
2679         brdp->nrbnks = banknr;
2680         if (brdp->brdtype == BRD_ECH)
2681                 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2682 
2683         brdp->state |= BRD_FOUND;
2684         i = stl_mapirq(brdp->irq, name);
2685         return(i);
2686 }
2687 
2688 /*****************************************************************************/
2689 
2690 /*
2691  *      Initialize and configure the specified board.
2692  *      Scan through all the boards in the configuration and see what we
2693  *      can find. Handle EIO and the ECH boards a little differently here
2694  *      since the initial search and setup is very different.
2695  */
2696 
2697 static int __init stl_brdinit(stlbrd_t *brdp)
2698 {
2699         int     i;
2700 
2701 #if DEBUG
2702         printk("stl_brdinit(brdp=%x)\n", (int) brdp);
2703 #endif
2704 
2705         switch (brdp->brdtype) {
2706         case BRD_EASYIO:
2707         case BRD_EASYIOPCI:
2708                 stl_initeio(brdp);
2709                 break;
2710         case BRD_ECH:
2711         case BRD_ECHMC:
2712         case BRD_ECHPCI:
2713         case BRD_ECH64PCI:
2714                 stl_initech(brdp);
2715                 break;
2716         default:
2717                 printk("STALLION: board=%d is unknown board type=%d\n",
2718                         brdp->brdnr, brdp->brdtype);
2719                 return(ENODEV);
2720         }
2721 
2722         stl_brds[brdp->brdnr] = brdp;
2723         if ((brdp->state & BRD_FOUND) == 0) {
2724                 printk("STALLION: %s board not found, board=%d io=%x irq=%d\n",
2725                         stl_brdnames[brdp->brdtype], brdp->brdnr,
2726                         brdp->ioaddr1, brdp->irq);
2727                 return(ENODEV);
2728         }
2729 
2730         for (i = 0; (i < STL_MAXPANELS); i++)
2731                 if (brdp->panels[i] != (stlpanel_t *) NULL)
2732                         stl_initports(brdp, brdp->panels[i]);
2733 
2734         printk("STALLION: %s found, board=%d io=%x irq=%d "
2735                 "nrpanels=%d nrports=%d\n", stl_brdnames[brdp->brdtype],
2736                 brdp->brdnr, brdp->ioaddr1, brdp->irq, brdp->nrpanels,
2737                 brdp->nrports);
2738         return(0);
2739 }
2740 
2741 /*****************************************************************************/
2742 
2743 /*
2744  *      Find the next available board number that is free.
2745  */
2746 
2747 static inline int stl_getbrdnr()
2748 {
2749         int     i;
2750 
2751         for (i = 0; (i < STL_MAXBRDS); i++) {
2752                 if (stl_brds[i] == (stlbrd_t *) NULL) {
2753                         if (i >= stl_nrbrds)
2754                                 stl_nrbrds = i + 1;
2755                         return(i);
2756                 }
2757         }
2758         return(-1);
2759 }
2760 
2761 /*****************************************************************************/
2762 
2763 #ifdef  CONFIG_PCI
2764 
2765 /*
2766  *      We have a Stallion board. Allocate a board structure and
2767  *      initialize it. Read its IO and IRQ resources from PCI
2768  *      configuration space.
2769  */
2770 
2771 static inline int stl_initpcibrd(int brdtype, struct pci_dev *devp)
2772 {
2773         stlbrd_t        *brdp;
2774 
2775 #if DEBUG
2776         printk("stl_initpcibrd(brdtype=%d,busnr=%x,devnr=%x)\n", brdtype,
2777                 devp->bus->number, devp->devfn);
2778 #endif
2779 
2780         if (pci_enable_device(devp))
2781                 return(-EIO);
2782         if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
2783                 return(-ENOMEM);
2784         if ((brdp->brdnr = stl_getbrdnr()) < 0) {
2785                 printk("STALLION: too many boards found, "
2786                         "maximum supported %d\n", STL_MAXBRDS);
2787                 return(0);
2788         }
2789         brdp->brdtype = brdtype;
2790 
2791 /*
2792  *      Different Stallion boards use the BAR registers in different ways,
2793  *      so set up io addresses based on board type.
2794  */
2795 #if DEBUG
2796         printk("%s(%d): BAR[]=%x,%x,%x,%x IRQ=%x\n", __FILE__, __LINE__,
2797                 pci_resource_start(devp, 0), pci_resource_start(devp, 1),
2798                 pci_resource_start(devp, 2), pci_resource_start(devp, 3), devp->irq);
2799 #endif
2800 
2801 /*
2802  *      We have all resources from the board, so let's setup the actual
2803  *      board structure now.
2804  */
2805         switch (brdtype) {
2806         case BRD_ECHPCI:
2807                 brdp->ioaddr2 = pci_resource_start(devp, 0);
2808                 brdp->ioaddr1 = pci_resource_start(devp, 1);
2809                 break;
2810         case BRD_ECH64PCI:
2811                 brdp->ioaddr2 = pci_resource_start(devp, 2);
2812                 brdp->ioaddr1 = pci_resource_start(devp, 1);
2813                 break;
2814         case BRD_EASYIOPCI:
2815                 brdp->ioaddr1 = pci_resource_start(devp, 2);
2816                 brdp->ioaddr2 = pci_resource_start(devp, 1);
2817                 break;
2818         default:
2819                 printk("STALLION: unknown PCI board type=%d\n", brdtype);
2820                 break;
2821         }
2822 
2823         brdp->irq = devp->irq;
2824         stl_brdinit(brdp);
2825 
2826         return(0);
2827 }
2828 
2829 /*****************************************************************************/
2830 
2831 /*
2832  *      Find all Stallion PCI boards that might be installed. Initialize each
2833  *      one as it is found.
2834  */
2835 
2836 
2837 static inline int stl_findpcibrds()
2838 {
2839         struct pci_dev  *dev = NULL;
2840         int             i, rc;
2841 
2842 #if DEBUG
2843         printk("stl_findpcibrds()\n");
2844 #endif
2845 
2846         if (! pci_present())
2847                 return(0);
2848 
2849         for (i = 0; (i < stl_nrpcibrds); i++)
2850                 while ((dev = pci_find_device(stl_pcibrds[i].vendid,
2851                     stl_pcibrds[i].devid, dev))) {
2852 
2853 /*
2854  *                      Found a device on the PCI bus that has our vendor and
2855  *                      device ID. Need to check now that it is really us.
2856  */
2857                         if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
2858                                 continue;
2859 
2860                         rc = stl_initpcibrd(stl_pcibrds[i].brdtype, dev);
2861                         if (rc)
2862                                 return(rc);
2863                 }
2864 
2865         return(0);
2866 }
2867 
2868 #endif
2869 
2870 /*****************************************************************************/
2871 
2872 /*
2873  *      Scan through all the boards in the configuration and see what we
2874  *      can find. Handle EIO and the ECH boards a little differently here
2875  *      since the initial search and setup is too different.
2876  */
2877 
2878 static inline int stl_initbrds()
2879 {
2880         stlbrd_t        *brdp;
2881         stlconf_t       *confp;
2882         int             i;
2883 
2884 #if DEBUG
2885         printk("stl_initbrds()\n");
2886 #endif
2887 
2888         if (stl_nrbrds > STL_MAXBRDS) {
2889                 printk("STALLION: too many boards in configuration table, "
2890                         "truncating to %d\n", STL_MAXBRDS);
2891                 stl_nrbrds = STL_MAXBRDS;
2892         }
2893 
2894 /*
2895  *      Firstly scan the list of static boards configured. Allocate
2896  *      resources and initialize the boards as found.
2897  */
2898         for (i = 0; (i < stl_nrbrds); i++) {
2899                 confp = &stl_brdconf[i];
2900 #ifdef MODULE
2901                 stl_parsebrd(confp, stl_brdsp[i]);
2902 #endif
2903                 if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
2904                         return(-ENOMEM);
2905                 brdp->brdnr = i;
2906                 brdp->brdtype = confp->brdtype;
2907                 brdp->ioaddr1 = confp->ioaddr1;
2908                 brdp->ioaddr2 = confp->ioaddr2;
2909                 brdp->irq = confp->irq;
2910                 brdp->irqtype = confp->irqtype;
2911                 stl_brdinit(brdp);
2912         }
2913 
2914 /*
2915  *      Find any dynamically supported boards. That is via module load
2916  *      line options or auto-detected on the PCI bus.
2917  */
2918 #ifdef MODULE
2919         stl_argbrds();
2920 #endif
2921 #ifdef CONFIG_PCI
2922         stl_findpcibrds();
2923 #endif
2924 
2925         return(0);
2926 }
2927 
2928 /*****************************************************************************/
2929 
2930 /*
2931  *      Return the board stats structure to user app.
2932  */
2933 
2934 static int stl_getbrdstats(combrd_t *bp)
2935 {
2936         stlbrd_t        *brdp;
2937         stlpanel_t      *panelp;
2938         int             i;
2939 
2940         copy_from_user(&stl_brdstats, bp, sizeof(combrd_t));
2941         if (stl_brdstats.brd >= STL_MAXBRDS)
2942                 return(-ENODEV);
2943         brdp = stl_brds[stl_brdstats.brd];
2944         if (brdp == (stlbrd_t *) NULL)
2945                 return(-ENODEV);
2946 
2947         memset(&stl_brdstats, 0, sizeof(combrd_t));
2948         stl_brdstats.brd = brdp->brdnr;
2949         stl_brdstats.type = brdp->brdtype;
2950         stl_brdstats.hwid = brdp->hwid;
2951         stl_brdstats.state = brdp->state;
2952         stl_brdstats.ioaddr = brdp->ioaddr1;
2953         stl_brdstats.ioaddr2 = brdp->ioaddr2;
2954         stl_brdstats.irq = brdp->irq;
2955         stl_brdstats.nrpanels = brdp->nrpanels;
2956         stl_brdstats.nrports = brdp->nrports;
2957         for (i = 0; (i < brdp->nrpanels); i++) {
2958                 panelp = brdp->panels[i];
2959                 stl_brdstats.panels[i].panel = i;
2960                 stl_brdstats.panels[i].hwid = panelp->hwid;
2961                 stl_brdstats.panels[i].nrports = panelp->nrports;
2962         }
2963 
2964         copy_to_user(bp, &stl_brdstats, sizeof(combrd_t));
2965         return(0);
2966 }
2967 
2968 /*****************************************************************************/
2969 
2970 /*
2971  *      Resolve the referenced port number into a port struct pointer.
2972  */
2973 
2974 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
2975 {
2976         stlbrd_t        *brdp;
2977         stlpanel_t      *panelp;
2978 
2979         if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
2980                 return((stlport_t *) NULL);
2981         brdp = stl_brds[brdnr];
2982         if (brdp == (stlbrd_t *) NULL)
2983                 return((stlport_t *) NULL);
2984         if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
2985                 return((stlport_t *) NULL);
2986         panelp = brdp->panels[panelnr];
2987         if (panelp == (stlpanel_t *) NULL)
2988                 return((stlport_t *) NULL);
2989         if ((portnr < 0) || (portnr >= panelp->nrports))
2990                 return((stlport_t *) NULL);
2991         return(panelp->ports[portnr]);
2992 }
2993 
2994 /*****************************************************************************/
2995 
2996 /*
2997  *      Return the port stats structure to user app. A NULL port struct
2998  *      pointer passed in means that we need to find out from the app
2999  *      what port to get stats for (used through board control device).
3000  */
3001 
3002 static int stl_getportstats(stlport_t *portp, comstats_t *cp)
3003 {
3004         unsigned char   *head, *tail;
3005         unsigned long   flags;
3006 
3007         if (portp == (stlport_t *) NULL) {
3008                 copy_from_user(&stl_comstats, cp, sizeof(comstats_t));
3009                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
3010                         stl_comstats.port);
3011                 if (portp == (stlport_t *) NULL)
3012                         return(-ENODEV);
3013         }
3014 
3015         portp->stats.state = portp->istate;
3016         portp->stats.flags = portp->flags;
3017         portp->stats.hwid = portp->hwid;
3018 
3019         portp->stats.ttystate = 0;
3020         portp->stats.cflags = 0;
3021         portp->stats.iflags = 0;
3022         portp->stats.oflags = 0;
3023         portp->stats.lflags = 0;
3024         portp->stats.rxbuffered = 0;
3025 
3026         save_flags(flags);
3027         cli();
3028         if (portp->tty != (struct tty_struct *) NULL) {
3029                 if (portp->tty->driver_data == portp) {
3030                         portp->stats.ttystate = portp->tty->flags;
3031                         portp->stats.rxbuffered = portp->tty->flip.count;
3032                         if (portp->tty->termios != (struct termios *) NULL) {
3033                                 portp->stats.cflags = portp->tty->termios->c_cflag;
3034                                 portp->stats.iflags = portp->tty->termios->c_iflag;
3035                                 portp->stats.oflags = portp->tty->termios->c_oflag;
3036                                 portp->stats.lflags = portp->tty->termios->c_lflag;
3037                         }
3038                 }
3039         }
3040         restore_flags(flags);
3041 
3042         head = portp->tx.head;
3043         tail = portp->tx.tail;
3044         portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
3045                 (STL_TXBUFSIZE - (tail - head)));
3046 
3047         portp->stats.signals = (unsigned long) stl_getsignals(portp);
3048 
3049         copy_to_user(cp, &portp->stats, sizeof(comstats_t));
3050         return(0);
3051 }
3052 
3053 /*****************************************************************************/
3054 
3055 /*
3056  *      Clear the port stats structure. We also return it zeroed out...
3057  */
3058 
3059 static int stl_clrportstats(stlport_t *portp, comstats_t *cp)
3060 {
3061         if (portp == (stlport_t *) NULL) {
3062                 copy_from_user(&stl_comstats, cp, sizeof(comstats_t));
3063                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
3064                         stl_comstats.port);
3065                 if (portp == (stlport_t *) NULL)
3066                         return(-ENODEV);
3067         }
3068 
3069         memset(&portp->stats, 0, sizeof(comstats_t));
3070         portp->stats.brd = portp->brdnr;
3071         portp->stats.panel = portp->panelnr;
3072         portp->stats.port = portp->portnr;
3073         copy_to_user(cp, &portp->stats, sizeof(comstats_t));
3074         return(0);
3075 }
3076 
3077 /*****************************************************************************/
3078 
3079 /*
3080  *      Return the entire driver ports structure to a user app.
3081  */
3082 
3083 static int stl_getportstruct(unsigned long arg)
3084 {
3085         stlport_t       *portp;
3086 
3087         copy_from_user(&stl_dummyport, (void *) arg, sizeof(stlport_t));
3088         portp = stl_getport(stl_dummyport.brdnr, stl_dummyport.panelnr,
3089                  stl_dummyport.portnr);
3090         if (portp == (stlport_t *) NULL)
3091                 return(-ENODEV);
3092         copy_to_user((void *) arg, portp, sizeof(stlport_t));
3093         return(0);
3094 }
3095 
3096 /*****************************************************************************/
3097 
3098 /*
3099  *      Return the entire driver board structure to a user app.
3100  */
3101 
3102 static int stl_getbrdstruct(unsigned long arg)
3103 {
3104         stlbrd_t        *brdp;
3105 
3106         copy_from_user(&stl_dummybrd, (void *) arg, sizeof(stlbrd_t));
3107         if ((stl_dummybrd.brdnr < 0) || (stl_dummybrd.brdnr >= STL_MAXBRDS))
3108                 return(-ENODEV);
3109         brdp = stl_brds[stl_dummybrd.brdnr];
3110         if (brdp == (stlbrd_t *) NULL)
3111                 return(-ENODEV);
3112         copy_to_user((void *) arg, brdp, sizeof(stlbrd_t));
3113         return(0);
3114 }
3115 
3116 /*****************************************************************************/
3117 
3118 /*
3119  *      The "staliomem" device is also required to do some special operations
3120  *      on the board and/or ports. In this driver it is mostly used for stats
3121  *      collection.
3122  */
3123 
3124 static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
3125 {
3126         int     brdnr, rc;
3127 
3128 #if DEBUG
3129         printk("stl_memioctl(ip=%x,fp=%x,cmd=%x,arg=%x)\n", (int) ip,
3130                 (int) fp, cmd, (int) arg);
3131 #endif
3132 
3133         brdnr = MINOR(ip->i_rdev);
3134         if (brdnr >= STL_MAXBRDS)
3135                 return(-ENODEV);
3136         rc = 0;
3137 
3138         switch (cmd) {
3139         case COM_GETPORTSTATS:
3140                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
3141                     sizeof(comstats_t))) == 0)
3142                         rc = stl_getportstats((stlport_t *) NULL,
3143                                 (comstats_t *) arg);
3144                 break;
3145         case COM_CLRPORTSTATS:
3146                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
3147                     sizeof(comstats_t))) == 0)
3148                         rc = stl_clrportstats((stlport_t *) NULL,
3149                                 (comstats_t *) arg);
3150                 break;
3151         case COM_GETBRDSTATS:
3152                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
3153                     sizeof(combrd_t))) == 0)
3154                         rc = stl_getbrdstats((combrd_t *) arg);
3155                 break;
3156         case COM_READPORT:
3157                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
3158                     sizeof(stlport_t))) == 0)
3159                         rc = stl_getportstruct(arg);
3160                 break;
3161         case COM_READBOARD:
3162                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
3163                     sizeof(stlbrd_t))) == 0)
3164                         rc = stl_getbrdstruct(arg);
3165                 break;
3166         default:
3167                 rc = -ENOIOCTLCMD;
3168                 break;
3169         }
3170 
3171         return(rc);
3172 }
3173 
3174 /*****************************************************************************/
3175 
3176 int __init stl_init(void)
3177 {
3178         printk(KERN_INFO "%s: version %s\n", stl_drvtitle, stl_drvversion);
3179 
3180         stl_initbrds();
3181 
3182 /*
3183  *      Allocate a temporary write buffer.
3184  */
3185         stl_tmpwritebuf = (char *) stl_memalloc(STL_TXBUFSIZE);
3186         if (stl_tmpwritebuf == (char *) NULL)
3187                 printk("STALLION: failed to allocate memory (size=%d)\n",
3188                         STL_TXBUFSIZE);
3189 
3190 /*
3191  *      Set up a character driver for per board stuff. This is mainly used
3192  *      to do stats ioctls on the ports.
3193  */
3194         if (devfs_register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stl_fsiomem))
3195                 printk("STALLION: failed to register serial board device\n");
3196         devfs_handle = devfs_mk_dir (NULL, "staliomem", NULL);
3197         devfs_register_series (devfs_handle, "%u", 4, DEVFS_FL_DEFAULT,
3198                                STL_SIOMEMMAJOR, 0,
3199                                S_IFCHR | S_IRUSR | S_IWUSR,
3200                                &stl_fsiomem, NULL);
3201 
3202 /*
3203  *      Set up the tty driver structure and register us as a driver.
3204  *      Also setup the callout tty device.
3205  */
3206         memset(&stl_serial, 0, sizeof(struct tty_driver));
3207         stl_serial.magic = TTY_DRIVER_MAGIC;
3208         stl_serial.driver_name = stl_drvname;
3209         stl_serial.name = stl_serialname;
3210         stl_serial.major = STL_SERIALMAJOR;
3211         stl_serial.minor_start = 0;
3212         stl_serial.num = STL_MAXBRDS * STL_MAXPORTS;
3213         stl_serial.type = TTY_DRIVER_TYPE_SERIAL;
3214         stl_serial.subtype = STL_DRVTYPSERIAL;
3215         stl_serial.init_termios = stl_deftermios;
3216         stl_serial.flags = TTY_DRIVER_REAL_RAW;
3217         stl_serial.refcount = &stl_refcount;
3218         stl_serial.table = stl_ttys;
3219         stl_serial.termios = stl_termios;
3220         stl_serial.termios_locked = stl_termioslocked;
3221         
3222         stl_serial.open = stl_open;
3223         stl_serial.close = stl_close;
3224         stl_serial.write = stl_write;
3225         stl_serial.put_char = stl_putchar;
3226         stl_serial.flush_chars = stl_flushchars;
3227         stl_serial.write_room = stl_writeroom;
3228         stl_serial.chars_in_buffer = stl_charsinbuffer;
3229         stl_serial.ioctl = stl_ioctl;
3230         stl_serial.set_termios = stl_settermios;
3231         stl_serial.throttle = stl_throttle;
3232         stl_serial.unthrottle = stl_unthrottle;
3233         stl_serial.stop = stl_stop;
3234         stl_serial.start = stl_start;
3235         stl_serial.hangup = stl_hangup;
3236         stl_serial.flush_buffer = stl_flushbuffer;
3237         stl_serial.break_ctl = stl_breakctl;
3238         stl_serial.wait_until_sent = stl_waituntilsent;
3239         stl_serial.send_xchar = stl_sendxchar;
3240         stl_serial.read_proc = stl_readproc;
3241 
3242         stl_callout = stl_serial;
3243         stl_callout.name = stl_calloutname;
3244         stl_callout.major = STL_CALLOUTMAJOR;
3245         stl_callout.subtype = STL_DRVTYPCALLOUT;
3246         stl_callout.read_proc = 0;
3247 
3248         if (tty_register_driver(&stl_serial))
3249                 printk("STALLION: failed to register serial driver\n");
3250         if (tty_register_driver(&stl_callout))
3251                 printk("STALLION: failed to register callout driver\n");
3252 
3253         return(0);
3254 }
3255 
3256 /*****************************************************************************/
3257 /*                       CD1400 HARDWARE FUNCTIONS                           */
3258 /*****************************************************************************/
3259 
3260 /*
3261  *      These functions get/set/update the registers of the cd1400 UARTs.
3262  *      Access to the cd1400 registers is via an address/data io port pair.
3263  *      (Maybe should make this inline...)
3264  */
3265 
3266 static int stl_cd1400getreg(stlport_t *portp, int regnr)
3267 {
3268         outb((regnr + portp->uartaddr), portp->ioaddr);
3269         return(inb(portp->ioaddr + EREG_DATA));
3270 }
3271 
3272 static void stl_cd1400setreg(stlport_t *portp, int regnr, int value)
3273 {
3274         outb((regnr + portp->uartaddr), portp->ioaddr);
3275         outb(value, portp->ioaddr + EREG_DATA);
3276 }
3277 
3278 static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value)
3279 {
3280         outb((regnr + portp->uartaddr), portp->ioaddr);
3281         if (inb(portp->ioaddr + EREG_DATA) != value) {
3282                 outb(value, portp->ioaddr + EREG_DATA);
3283                 return(1);
3284         }
3285         return(0);
3286 }
3287 
3288 /*****************************************************************************/
3289 
3290 /*
3291  *      Inbitialize the UARTs in a panel. We don't care what sort of board
3292  *      these ports are on - since the port io registers are almost
3293  *      identical when dealing with ports.
3294  */
3295 
3296 static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
3297 {
3298         unsigned int    gfrcr;
3299         int             chipmask, i, j;
3300         int             nrchips, uartaddr, ioaddr;
3301 
3302 #if DEBUG
3303         printk("stl_panelinit(brdp=%x,panelp=%x)\n", (int) brdp, (int) panelp);
3304 #endif
3305 
3306         BRDENABLE(panelp->brdnr, panelp->pagenr);
3307 
3308 /*
3309  *      Check that each chip is present and started up OK.
3310  */
3311         chipmask = 0;
3312         nrchips = panelp->nrports / CD1400_PORTS;
3313         for (i = 0; (i < nrchips); i++) {
3314                 if (brdp->brdtype == BRD_ECHPCI) {
3315                         outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
3316                         ioaddr = panelp->iobase;
3317                 } else {
3318                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
3319                 }
3320                 uartaddr = (i & 0x01) ? 0x080 : 0;
3321                 outb((GFRCR + uartaddr), ioaddr);
3322                 outb(0, (ioaddr + EREG_DATA));
3323                 outb((CCR + uartaddr), ioaddr);
3324                 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
3325                 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
3326                 outb((GFRCR + uartaddr), ioaddr);
3327                 for (j = 0; (j < CCR_MAXWAIT); j++) {
3328                         if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
3329                                 break;
3330                 }
3331                 if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
3332                         printk("STALLION: cd1400 not responding, "
3333                                 "brd=%d panel=%d chip=%d\n",
3334                                 panelp->brdnr, panelp->panelnr, i);
3335                         continue;
3336                 }
3337                 chipmask |= (0x1 << i);
3338                 outb((PPR + uartaddr), ioaddr);
3339                 outb(PPR_SCALAR, (ioaddr + EREG_DATA));
3340         }
3341 
3342         BRDDISABLE(panelp->brdnr);
3343         return(chipmask);
3344 }
3345 
3346 /*****************************************************************************/
3347 
3348 /*
3349  *      Initialize hardware specific port registers.
3350  */
3351 
3352 static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
3353 {
3354 #if DEBUG
3355         printk("stl_cd1400portinit(brdp=%x,panelp=%x,portp=%x)\n",
3356                 (int) brdp, (int) panelp, (int) portp);
3357 #endif
3358 
3359         if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
3360             (portp == (stlport_t *) NULL))
3361                 return;
3362 
3363         portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
3364                 (portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
3365         portp->uartaddr = (portp->portnr & 0x04) << 5;
3366         portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
3367 
3368         BRDENABLE(portp->brdnr, portp->pagenr);
3369         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3370         stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
3371         portp->hwid = stl_cd1400getreg(portp, GFRCR);
3372         BRDDISABLE(portp->brdnr);
3373 }
3374 
3375 /*****************************************************************************/
3376 
3377 /*
3378  *      Wait for the command register to be ready. We will poll this,
3379  *      since it won't usually take too long to be ready.
3380  */
3381 
3382 static void stl_cd1400ccrwait(stlport_t *portp)
3383 {
3384         int     i;
3385 
3386         for (i = 0; (i < CCR_MAXWAIT); i++) {
3387                 if (stl_cd1400getreg(portp, CCR) == 0) {
3388                         return;
3389                 }
3390         }
3391 
3392         printk("STALLION: cd1400 not responding, port=%d panel=%d brd=%d\n",
3393                 portp->portnr, portp->panelnr, portp->brdnr);
3394 }
3395 
3396 /*****************************************************************************/
3397 
3398 /*
3399  *      Set up the cd1400 registers for a port based on the termios port
3400  *      settings.
3401  */
3402 
3403 static void stl_cd1400setport(stlport_t *portp, struct termios *tiosp)
3404 {
3405         stlbrd_t        *brdp;
3406         unsigned long   flags;
3407         unsigned int    clkdiv, baudrate;
3408         unsigned char   cor1, cor2, cor3;
3409         unsigned char   cor4, cor5, ccr;
3410         unsigned char   srer, sreron, sreroff;
3411         unsigned char   mcor1, mcor2, rtpr;
3412         unsigned char   clk, div;
3413 
3414         cor1 = 0;
3415         cor2 = 0;
3416         cor3 = 0;
3417         cor4 = 0;
3418         cor5 = 0;
3419         ccr = 0;
3420         rtpr = 0;
3421         clk = 0;
3422         div = 0;
3423         mcor1 = 0;
3424         mcor2 = 0;
3425         sreron = 0;
3426         sreroff = 0;
3427 
3428         brdp = stl_brds[portp->brdnr];
3429         if (brdp == (stlbrd_t *) NULL)
3430                 return;
3431 
3432 /*
3433  *      Set up the RX char ignore mask with those RX error types we
3434  *      can ignore. We can get the cd1400 to help us out a little here,
3435  *      it will ignore parity errors and breaks for us.
3436  */
3437         portp->rxignoremsk = 0;
3438         if (tiosp->c_iflag & IGNPAR) {
3439                 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
3440                 cor1 |= COR1_PARIGNORE;
3441         }
3442         if (tiosp->c_iflag & IGNBRK) {
3443                 portp->rxignoremsk |= ST_BREAK;
3444                 cor4 |= COR4_IGNBRK;
3445         }
3446 
3447         portp->rxmarkmsk = ST_OVERRUN;
3448         if (tiosp->c_iflag & (INPCK | PARMRK))
3449                 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
3450         if (tiosp->c_iflag & BRKINT)
3451                 portp->rxmarkmsk |= ST_BREAK;
3452 
3453 /*
3454  *      Go through the char size, parity and stop bits and set all the
3455  *      option register appropriately.
3456  */
3457         switch (tiosp->c_cflag & CSIZE) {
3458         case CS5:
3459                 cor1 |= COR1_CHL5;
3460                 break;
3461         case CS6:
3462                 cor1 |= COR1_CHL6;
3463                 break;
3464         case CS7:
3465                 cor1 |= COR1_CHL7;
3466                 break;
3467         default:
3468                 cor1 |= COR1_CHL8;
3469                 break;
3470         }
3471 
3472         if (tiosp->c_cflag & CSTOPB)
3473                 cor1 |= COR1_STOP2;
3474         else
3475                 cor1 |= COR1_STOP1;
3476 
3477         if (tiosp->c_cflag & PARENB) {
3478                 if (tiosp->c_cflag & PARODD)
3479                         cor1 |= (COR1_PARENB | COR1_PARODD);
3480                 else
3481                         cor1 |= (COR1_PARENB | COR1_PAREVEN);
3482         } else {
3483                 cor1 |= COR1_PARNONE;
3484         }
3485 
3486 /*
3487  *      Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
3488  *      space for hardware flow control and the like. This should be set to
3489  *      VMIN. Also here we will set the RX data timeout to 10ms - this should
3490  *      really be based on VTIME.
3491  */
3492         cor3 |= FIFO_RXTHRESHOLD;
3493         rtpr = 2;
3494 
3495 /*
3496  *      Calculate the baud rate timers. For now we will just assume that
3497  *      the input and output baud are the same. Could have used a baud
3498  *      table here, but this way we can generate virtually any baud rate
3499  *      we like!
3500  */
3501         baudrate = tiosp->c_cflag & CBAUD;
3502         if (baudrate & CBAUDEX) {
3503                 baudrate &= ~CBAUDEX;
3504                 if ((baudrate < 1) || (baudrate > 4))
3505                         tiosp->c_cflag &= ~CBAUDEX;
3506                 else
3507                         baudrate += 15;
3508         }
3509         baudrate = stl_baudrates[baudrate];
3510         if ((tiosp->c_cflag & CBAUD) == B38400) {
3511                 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3512                         baudrate = 57600;
3513                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3514                         baudrate = 115200;
3515                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3516                         baudrate = 230400;
3517                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3518                         baudrate = 460800;
3519                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3520                         baudrate = (portp->baud_base / portp->custom_divisor);
3521         }
3522         if (baudrate > STL_CD1400MAXBAUD)
3523                 baudrate = STL_CD1400MAXBAUD;
3524 
3525         if (baudrate > 0) {
3526                 for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
3527                         clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) / baudrate);
3528                         if (clkdiv < 0x100)
3529                                 break;
3530                 }
3531                 div = (unsigned char) clkdiv;
3532         }
3533 
3534 /*
3535  *      Check what form of modem signaling is required and set it up.
3536  */
3537         if ((tiosp->c_cflag & CLOCAL) == 0) {
3538                 mcor1 |= MCOR1_DCD;
3539                 mcor2 |= MCOR2_DCD;
3540                 sreron |= SRER_MODEM;
3541                 portp->flags |= ASYNC_CHECK_CD;
3542         } else {
3543                 portp->flags &= ~ASYNC_CHECK_CD;
3544         }
3545 
3546 /*
3547  *      Setup cd1400 enhanced modes if we can. In particular we want to
3548  *      handle as much of the flow control as possible automatically. As
3549  *      well as saving a few CPU cycles it will also greatly improve flow
3550  *      control reliability.
3551  */
3552         if (tiosp->c_iflag & IXON) {
3553                 cor2 |= COR2_TXIBE;
3554                 cor3 |= COR3_SCD12;
3555                 if (tiosp->c_iflag & IXANY)
3556                         cor2 |= COR2_IXM;
3557         }
3558 
3559         if (tiosp->c_cflag & CRTSCTS) {
3560                 cor2 |= COR2_CTSAE;
3561                 mcor1 |= FIFO_RTSTHRESHOLD;
3562         }
3563 
3564 /*
3565  *      All cd1400 register values calculated so go through and set
3566  *      them all up.
3567  */
3568 
3569 #if DEBUG
3570         printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
3571                 portp->portnr, portp->panelnr, portp->brdnr);
3572         printk("    cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n",
3573                 cor1, cor2, cor3, cor4, cor5);
3574         printk("    mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
3575                 mcor1, mcor2, rtpr, sreron, sreroff);
3576         printk("    tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
3577         printk("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
3578                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
3579                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
3580 #endif
3581 
3582         save_flags(flags);
3583         cli();
3584         BRDENABLE(portp->brdnr, portp->pagenr);
3585         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3586         srer = stl_cd1400getreg(portp, SRER);
3587         stl_cd1400setreg(portp, SRER, 0);
3588         if (stl_cd1400updatereg(portp, COR1, cor1))
3589                 ccr = 1;
3590         if (stl_cd1400updatereg(portp, COR2, cor2))
3591                 ccr = 1;
3592         if (stl_cd1400updatereg(portp, COR3, cor3))
3593                 ccr = 1;
3594         if (ccr) {
3595                 stl_cd1400ccrwait(portp);
3596                 stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
3597         }
3598         stl_cd1400setreg(portp, COR4, cor4);
3599         stl_cd1400setreg(portp, COR5, cor5);
3600         stl_cd1400setreg(portp, MCOR1, mcor1);
3601         stl_cd1400setreg(portp, MCOR2, mcor2);
3602         if (baudrate > 0) {
3603                 stl_cd1400setreg(portp, TCOR, clk);
3604                 stl_cd1400setreg(portp, TBPR, div);
3605                 stl_cd1400setreg(portp, RCOR, clk);
3606                 stl_cd1400setreg(portp, RBPR, div);
3607         }
3608         stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
3609         stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
3610         stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
3611         stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
3612         stl_cd1400setreg(portp, RTPR, rtpr);
3613         mcor1 = stl_cd1400getreg(portp, MSVR1);
3614         if (mcor1 & MSVR1_DCD)
3615                 portp->sigs |= TIOCM_CD;
3616         else
3617                 portp->sigs &= ~TIOCM_CD;
3618         stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
3619         BRDDISABLE(portp->brdnr);
3620         restore_flags(flags);
3621 }
3622 
3623 /*****************************************************************************/
3624 
3625 /*
3626  *      Set the state of the DTR and RTS signals.
3627  */
3628 
3629 static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts)
3630 {
3631         unsigned char   msvr1, msvr2;
3632         unsigned long   flags;
3633 
3634 #if DEBUG
3635         printk("stl_cd1400setsignals(portp=%x,dtr=%d,rts=%d)\n",
3636                 (int) portp, dtr, rts);
3637 #endif
3638 
3639         msvr1 = 0;
3640         msvr2 = 0;
3641         if (dtr > 0)
3642                 msvr1 = MSVR1_DTR;
3643         if (rts > 0)
3644                 msvr2 = MSVR2_RTS;
3645 
3646         save_flags(flags);
3647         cli();
3648         BRDENABLE(portp->brdnr, portp->pagenr);
3649         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3650         if (rts >= 0)
3651                 stl_cd1400setreg(portp, MSVR2, msvr2);
3652         if (dtr >= 0)
3653                 stl_cd1400setreg(portp, MSVR1, msvr1);
3654         BRDDISABLE(portp->brdnr);
3655         restore_flags(flags);
3656 }
3657 
3658 /*****************************************************************************/
3659 
3660 /*
3661  *      Return the state of the signals.
3662  */
3663 
3664 static int stl_cd1400getsignals(stlport_t *portp)
3665 {
3666         unsigned char   msvr1, msvr2;
3667         unsigned long   flags;
3668         int             sigs;
3669 
3670 #if DEBUG
3671         printk("stl_cd1400getsignals(portp=%x)\n", (int) portp);
3672 #endif
3673 
3674         save_flags(flags);
3675         cli();
3676         BRDENABLE(portp->brdnr, portp->pagenr);
3677         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3678         msvr1 = stl_cd1400getreg(portp, MSVR1);
3679         msvr2 = stl_cd1400getreg(portp, MSVR2);
3680         BRDDISABLE(portp->brdnr);
3681         restore_flags(flags);
3682 
3683         sigs = 0;
3684         sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
3685         sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
3686         sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
3687         sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
3688 #if 0
3689         sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
3690         sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
3691 #else
3692         sigs |= TIOCM_DSR;
3693 #endif
3694         return(sigs);
3695 }
3696 
3697 /*****************************************************************************/
3698 
3699 /*
3700  *      Enable/Disable the Transmitter and/or Receiver.
3701  */
3702 
3703 static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx)
3704 {
3705         unsigned char   ccr;
3706         unsigned long   flags;
3707 
3708 #if DEBUG
3709         printk("stl_cd1400enablerxtx(portp=%x,rx=%d,tx=%d)\n",
3710                 (int) portp, rx, tx);
3711 #endif
3712         ccr = 0;
3713 
3714         if (tx == 0)
3715                 ccr |= CCR_TXDISABLE;
3716         else if (tx > 0)
3717                 ccr |= CCR_TXENABLE;
3718         if (rx == 0)
3719                 ccr |= CCR_RXDISABLE;
3720         else if (rx > 0)
3721                 ccr |= CCR_RXENABLE;
3722 
3723         save_flags(flags);
3724         cli();
3725         BRDENABLE(portp->brdnr, portp->pagenr);
3726         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3727         stl_cd1400ccrwait(portp);
3728         stl_cd1400setreg(portp, CCR, ccr);
3729         stl_cd1400ccrwait(portp);
3730         BRDDISABLE(portp->brdnr);
3731         restore_flags(flags);
3732 }
3733 
3734 /*****************************************************************************/
3735 
3736 /*
3737  *      Start/stop the Transmitter and/or Receiver.
3738  */
3739 
3740 static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx)
3741 {
3742         unsigned char   sreron, sreroff;
3743         unsigned long   flags;
3744 
3745 #if DEBUG
3746         printk("stl_cd1400startrxtx(portp=%x,rx=%d,tx=%d)\n",
3747                 (int) portp, rx, tx);
3748 #endif
3749 
3750         sreron = 0;
3751         sreroff = 0;
3752         if (tx == 0)
3753                 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
3754         else if (tx == 1)
3755                 sreron |= SRER_TXDATA;
3756         else if (tx >= 2)
3757                 sreron |= SRER_TXEMPTY;
3758         if (rx == 0)
3759                 sreroff |= SRER_RXDATA;
3760         else if (rx > 0)
3761                 sreron |= SRER_RXDATA;
3762 
3763         save_flags(flags);
3764         cli();
3765         BRDENABLE(portp->brdnr, portp->pagenr);
3766         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3767         stl_cd1400setreg(portp, SRER,
3768                 ((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
3769         BRDDISABLE(portp->brdnr);
3770         if (tx > 0)
3771                 set_bit(ASYI_TXBUSY, &portp->istate);
3772         restore_flags(flags);
3773 }
3774 
3775 /*****************************************************************************/
3776 
3777 /*
3778  *      Disable all interrupts from this port.
3779  */
3780 
3781 static void stl_cd1400disableintrs(stlport_t *portp)
3782 {
3783         unsigned long   flags;
3784 
3785 #if DEBUG
3786         printk("stl_cd1400disableintrs(portp=%x)\n", (int) portp);
3787 #endif
3788         save_flags(flags);
3789         cli();
3790         BRDENABLE(portp->brdnr, portp->pagenr);
3791         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3792         stl_cd1400setreg(portp, SRER, 0);
3793         BRDDISABLE(portp->brdnr);
3794         restore_flags(flags);
3795 }
3796 
3797 /*****************************************************************************/
3798 
3799 static void stl_cd1400sendbreak(stlport_t *portp, int len)
3800 {
3801         unsigned long   flags;
3802 
3803 #if DEBUG
3804         printk("stl_cd1400sendbreak(portp=%x,len=%d)\n", (int) portp, len);
3805 #endif
3806 
3807         save_flags(flags);
3808         cli();
3809         BRDENABLE(portp->brdnr, portp->pagenr);
3810         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3811         stl_cd1400setreg(portp, SRER,
3812                 ((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
3813                 SRER_TXEMPTY));
3814         BRDDISABLE(portp->brdnr);
3815         portp->brklen = len;
3816         if (len == 1)
3817                 portp->stats.txbreaks++;
3818         restore_flags(flags);
3819 }
3820 
3821 /*****************************************************************************/
3822 
3823 /*
3824  *      Take flow control actions...
3825  */
3826 
3827 static void stl_cd1400flowctrl(stlport_t *portp, int state)
3828 {
3829         struct tty_struct       *tty;
3830         unsigned long           flags;
3831 
3832 #if DEBUG
3833         printk("stl_cd1400flowctrl(portp=%x,state=%x)\n", (int) portp, state);
3834 #endif
3835 
3836         if (portp == (stlport_t *) NULL)
3837                 return;
3838         tty = portp->tty;
3839         if (tty == (struct tty_struct *) NULL)
3840                 return;
3841 
3842         save_flags(flags);
3843         cli();
3844         BRDENABLE(portp->brdnr, portp->pagenr);
3845         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3846 
3847         if (state) {
3848                 if (tty->termios->c_iflag & IXOFF) {
3849                         stl_cd1400ccrwait(portp);
3850                         stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3851                         portp->stats.rxxon++;
3852                         stl_cd1400ccrwait(portp);
3853                 }
3854 /*
3855  *              Question: should we return RTS to what it was before? It may
3856  *              have been set by an ioctl... Suppose not, since if you have
3857  *              hardware flow control set then it is pretty silly to go and
3858  *              set the RTS line by hand.
3859  */
3860                 if (tty->termios->c_cflag & CRTSCTS) {
3861                         stl_cd1400setreg(portp, MCOR1,
3862                                 (stl_cd1400getreg(portp, MCOR1) |
3863                                 FIFO_RTSTHRESHOLD));
3864                         stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
3865                         portp->stats.rxrtson++;
3866                 }
3867         } else {
3868                 if (tty->termios->c_iflag & IXOFF) {
3869                         stl_cd1400ccrwait(portp);
3870                         stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3871                         portp->stats.rxxoff++;
3872                         stl_cd1400ccrwait(portp);
3873                 }
3874                 if (tty->termios->c_cflag & CRTSCTS) {
3875                         stl_cd1400setreg(portp, MCOR1,
3876                                 (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3877                         stl_cd1400setreg(portp, MSVR2, 0);
3878                         portp->stats.rxrtsoff++;
3879                 }
3880         }
3881 
3882         BRDDISABLE(portp->brdnr);
3883         restore_flags(flags);
3884 }
3885 
3886 /*****************************************************************************/
3887 
3888 /*
3889  *      Send a flow control character...
3890  */
3891 
3892 static void stl_cd1400sendflow(stlport_t *portp, int state)
3893 {
3894         struct tty_struct       *tty;
3895         unsigned long           flags;
3896 
3897 #if DEBUG
3898         printk("stl_cd1400sendflow(portp=%x,state=%x)\n", (int) portp, state);
3899 #endif
3900 
3901         if (portp == (stlport_t *) NULL)
3902                 return;
3903         tty = portp->tty;
3904         if (tty == (struct tty_struct *) NULL)
3905                 return;
3906 
3907         save_flags(flags);
3908         cli();
3909         BRDENABLE(portp->brdnr, portp->pagenr);
3910         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3911         if (state) {
3912                 stl_cd1400ccrwait(portp);
3913                 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3914                 portp->stats.rxxon++;
3915                 stl_cd1400ccrwait(portp);
3916         } else {
3917                 stl_cd1400ccrwait(portp);
3918                 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3919                 portp->stats.rxxoff++;
3920                 stl_cd1400ccrwait(portp);
3921         }
3922         BRDDISABLE(portp->brdnr);
3923         restore_flags(flags);
3924 }
3925 
3926 /*****************************************************************************/
3927 
3928 static void stl_cd1400flush(stlport_t *portp)
3929 {
3930         unsigned long   flags;
3931 
3932 #if DEBUG
3933         printk("stl_cd1400flush(portp=%x)\n", (int) portp);
3934 #endif
3935 
3936         if (portp == (stlport_t *) NULL)
3937                 return;
3938 
3939         save_flags(flags);
3940         cli();
3941         BRDENABLE(portp->brdnr, portp->pagenr);
3942         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3943         stl_cd1400ccrwait(portp);
3944         stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
3945         stl_cd1400ccrwait(portp);
3946         portp->tx.tail = portp->tx.head;
3947         BRDDISABLE(portp->brdnr);
3948         restore_flags(flags);
3949 }
3950 
3951 /*****************************************************************************/
3952 
3953 /*
3954  *      Return the current state of data flow on this port. This is only
3955  *      really interresting when determining if data has fully completed
3956  *      transmission or not... This is easy for the cd1400, it accurately
3957  *      maintains the busy port flag.
3958  */
3959 
3960 static int stl_cd1400datastate(stlport_t *portp)
3961 {
3962 #if DEBUG
3963         printk("stl_cd1400datastate(portp=%x)\n", (int) portp);
3964 #endif
3965 
3966         if (portp == (stlport_t *) NULL)
3967                 return(0);
3968 
3969         return(test_bit(ASYI_TXBUSY, &portp->istate) ? 1 : 0);
3970 }
3971 
3972 /*****************************************************************************/
3973 
3974 /*
3975  *      Interrupt service routine for cd1400 EasyIO boards.
3976  */
3977 
3978 static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase)
3979 {
3980         unsigned char   svrtype;
3981 
3982 #if DEBUG
3983         printk("stl_cd1400eiointr(panelp=%x,iobase=%x)\n",
3984                 (int) panelp, iobase);
3985 #endif
3986 
3987         outb(SVRR, iobase);
3988         svrtype = inb(iobase + EREG_DATA);
3989         if (panelp->nrports > 4) {
3990                 outb((SVRR + 0x80), iobase);
3991                 svrtype |= inb(iobase + EREG_DATA);
3992         }
3993 
3994         if (svrtype & SVRR_RX)
3995                 stl_cd1400rxisr(panelp, iobase);
3996         else if (svrtype & SVRR_TX)
3997                 stl_cd1400txisr(panelp, iobase);
3998         else if (svrtype & SVRR_MDM)
3999                 stl_cd1400mdmisr(panelp, iobase);
4000 }
4001 
4002 /*****************************************************************************/
4003 
4004 /*
4005  *      Interrupt service routine for cd1400 panels.
4006  */
4007 
4008 static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase)
4009 {
4010         unsigned char   svrtype;
4011 
4012 #if DEBUG
4013         printk("stl_cd1400echintr(panelp=%x,iobase=%x)\n", (int) panelp,
4014                 iobase);
4015 #endif
4016 
4017         outb(SVRR, iobase);
4018         svrtype = inb(iobase + EREG_DATA);
4019         outb((SVRR + 0x80), iobase);
4020         svrtype |= inb(iobase + EREG_DATA);
4021         if (svrtype & SVRR_RX)
4022                 stl_cd1400rxisr(panelp, iobase);
4023         else if (svrtype & SVRR_TX)
4024                 stl_cd1400txisr(panelp, iobase);
4025         else if (svrtype & SVRR_MDM)
4026                 stl_cd1400mdmisr(panelp, iobase);
4027 }
4028 
4029 
4030 /*****************************************************************************/
4031 
4032 /*
4033  *      Unfortunately we need to handle breaks in the TX data stream, since
4034  *      this is the only way to generate them on the cd1400.
4035  */
4036 
4037 static inline int stl_cd1400breakisr(stlport_t *portp, int ioaddr)
4038 {
4039         if (portp->brklen == 1) {
4040                 outb((COR2 + portp->uartaddr), ioaddr);
4041                 outb((inb(ioaddr + EREG_DATA) | COR2_ETC),
4042                         (ioaddr + EREG_DATA));
4043                 outb((TDR + portp->uartaddr), ioaddr);
4044                 outb(ETC_CMD, (ioaddr + EREG_DATA));
4045                 outb(ETC_STARTBREAK, (ioaddr + EREG_DATA));
4046                 outb((SRER + portp->uartaddr), ioaddr);
4047                 outb((inb(ioaddr + EREG_DATA) & ~(SRER_TXDATA | SRER_TXEMPTY)),
4048                         (ioaddr + EREG_DATA));
4049                 return(1);
4050         } else if (portp->brklen > 1) {
4051                 outb((TDR + portp->uartaddr), ioaddr);
4052                 outb(ETC_CMD, (ioaddr + EREG_DATA));
4053                 outb(ETC_STOPBREAK, (ioaddr + EREG_DATA));
4054                 portp->brklen = -1;
4055                 return(1);
4056         } else {
4057                 outb((COR2 + portp->uartaddr), ioaddr);
4058                 outb((inb(ioaddr + EREG_DATA) & ~COR2_ETC),
4059                         (ioaddr + EREG_DATA));
4060                 portp->brklen = 0;
4061         }
4062         return(0);
4063 }
4064 
4065 /*****************************************************************************/
4066 
4067 /*
4068  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
4069  *      chars is pretty simple, stuff as many as possible from the TX buffer
4070  *      into the cd1400 FIFO. Must also handle TX breaks here, since they
4071  *      are embedded as commands in the data stream. Oh no, had to use a goto!
4072  *      This could be optimized more, will do when I get time...
4073  *      In practice it is possible that interrupts are enabled but that the
4074  *      port has been hung up. Need to handle not having any TX buffer here,
4075  *      this is done by using the side effect that head and tail will also
4076  *      be NULL if the buffer has been freed.
4077  */
4078 
4079 static void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr)
4080 {
4081         stlport_t       *portp;
4082         int             len, stlen;
4083         char            *head, *tail;
4084         unsigned char   ioack, srer;
4085 
4086 #if DEBUG
4087         printk("stl_cd1400txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
4088 #endif
4089 
4090         ioack = inb(ioaddr + EREG_TXACK);
4091         if (((ioack & panelp->ackmask) != 0) ||
4092             ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
4093                 printk("STALLION: bad TX interrupt ack value=%x\n", ioack);
4094                 return;
4095         }
4096         portp = panelp->ports[(ioack >> 3)];
4097 
4098 /*
4099  *      Unfortunately we need to handle breaks in the data stream, since
4100  *      this is the only way to generate them on the cd1400. Do it now if
4101  *      a break is to be sent.
4102  */
4103         if (portp->brklen != 0)
4104                 if (stl_cd1400breakisr(portp, ioaddr))
4105                         goto stl_txalldone;
4106 
4107         head = portp->tx.head;
4108         tail = portp->tx.tail;
4109         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
4110         if ((len == 0) || ((len < STL_TXBUFLOW) &&
4111             (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
4112                 set_bit(ASYI_TXLOW, &portp->istate);
4113                 MOD_INC_USE_COUNT;
4114                 if (schedule_task(&portp->tqueue) == 0)
4115                         MOD_DEC_USE_COUNT;
4116         }
4117 
4118         if (len == 0) {
4119                 outb((SRER + portp->uartaddr), ioaddr);
4120                 srer = inb(ioaddr + EREG_DATA);
4121                 if (srer & SRER_TXDATA) {
4122                         srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
4123                 } else {
4124                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
4125                         clear_bit(ASYI_TXBUSY, &portp->istate);
4126                 }
4127                 outb(srer, (ioaddr + EREG_DATA));
4128         } else {
4129                 len = MIN(len, CD1400_TXFIFOSIZE);
4130                 portp->stats.txtotal += len;
4131                 stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
4132                 outb((TDR + portp->uartaddr), ioaddr);
4133                 outsb((ioaddr + EREG_DATA), tail, stlen);
4134                 len -= stlen;
4135                 tail += stlen;
4136                 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
4137                         tail = portp->tx.buf;
4138                 if (len > 0) {
4139                         outsb((ioaddr + EREG_DATA), tail, len);
4140                         tail += len;
4141                 }
4142                 portp->tx.tail = tail;
4143         }
4144 
4145 stl_txalldone:
4146         outb((EOSRR + portp->uartaddr), ioaddr);
4147         outb(0, (ioaddr + EREG_DATA));
4148 }
4149 
4150 /*****************************************************************************/
4151 
4152 /*
4153  *      Receive character interrupt handler. Determine if we have good chars
4154  *      or bad chars and then process appropriately. Good chars are easy
4155  *      just shove the lot into the RX buffer and set all status byte to 0.
4156  *      If a bad RX char then process as required. This routine needs to be
4157  *      fast!  In practice it is possible that we get an interrupt on a port
4158  *      that is closed. This can happen on hangups - since they completely
4159  *      shutdown a port not in user context. Need to handle this case.
4160  */
4161 
4162 static void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr)
4163 {
4164         stlport_t               *portp;
4165         struct tty_struct       *tty;
4166         unsigned int            ioack, len, buflen;
4167         unsigned char           status;
4168         char                    ch;
4169 
4170 #if DEBUG
4171         printk("stl_cd1400rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
4172 #endif
4173 
4174         ioack = inb(ioaddr + EREG_RXACK);
4175         if ((ioack & panelp->ackmask) != 0) {
4176                 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
4177                 return;
4178         }
4179         portp = panelp->ports[(ioack >> 3)];
4180         tty = portp->tty;
4181 
4182         if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
4183                 outb((RDCR + portp->uartaddr), ioaddr);
4184                 len = inb(ioaddr + EREG_DATA);
4185                 if ((tty == (struct tty_struct *) NULL) ||
4186                     (tty->flip.char_buf_ptr == (char *) NULL) ||
4187                     ((buflen = TTY_FLIPBUF_SIZE - tty->flip.count) == 0)) {
4188                         len = MIN(len, sizeof(stl_unwanted));
4189                         outb((RDSR + portp->uartaddr), ioaddr);
4190                         insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
4191                         portp->stats.rxlost += len;
4192                         portp->stats.rxtotal += len;
4193                 } else {
4194                         len = MIN(len, buflen);
4195                         if (len > 0) {
4196                                 outb((RDSR + portp->uartaddr), ioaddr);
4197                                 insb((ioaddr + EREG_DATA), tty->flip.char_buf_ptr, len);
4198                                 memset(tty->flip.flag_buf_ptr, 0, len);
4199                                 tty->flip.flag_buf_ptr += len;
4200                                 tty->flip.char_buf_ptr += len;
4201                                 tty->flip.count += len;
4202                                 tty_schedule_flip(tty);
4203                                 portp->stats.rxtotal += len;
4204                         }
4205                 }
4206         } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
4207                 outb((RDSR + portp->uartaddr), ioaddr);
4208                 status = inb(ioaddr + EREG_DATA);
4209                 ch = inb(ioaddr + EREG_DATA);
4210                 if (status & ST_PARITY)
4211                         portp->stats.rxparity++;
4212                 if (status & ST_FRAMING)
4213                         portp->stats.rxframing++;
4214                 if (status & ST_OVERRUN)
4215                         portp->stats.rxoverrun++;
4216                 if (status & ST_BREAK)
4217                         portp->stats.rxbreaks++;
4218                 if (status & ST_SCHARMASK) {
4219                         if ((status & ST_SCHARMASK) == ST_SCHAR1)
4220                                 portp->stats.txxon++;
4221                         if ((status & ST_SCHARMASK) == ST_SCHAR2)
4222                                 portp->stats.txxoff++;
4223                         goto stl_rxalldone;
4224                 }
4225                 if ((tty != (struct tty_struct *) NULL) &&
4226                     ((portp->rxignoremsk & status) == 0)) {
4227                         if (portp->rxmarkmsk & status) {
4228                                 if (status & ST_BREAK) {
4229                                         status = TTY_BREAK;
4230                                         if (portp->flags & ASYNC_SAK) {
4231                                                 do_SAK(tty);
4232                                                 BRDENABLE(portp->brdnr, portp->pagenr);
4233                                         }
4234                                 } else if (status & ST_PARITY) {
4235                                         status = TTY_PARITY;
4236                                 } else if (status & ST_FRAMING) {
4237                                         status = TTY_FRAME;
4238                                 } else if(status & ST_OVERRUN) {
4239                                         status = TTY_OVERRUN;
4240                                 } else {
4241                                         status = 0;
4242                                 }
4243                         } else {
4244                                 status = 0;
4245                         }
4246                         if (tty->flip.char_buf_ptr != (char *) NULL) {
4247                                 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
4248                                         *tty->flip.flag_buf_ptr++ = status;
4249                                         *tty->flip.char_buf_ptr++ = ch;
4250                                         tty->flip.count++;
4251                                 }
4252                                 tty_schedule_flip(tty);
4253                         }
4254                 }
4255         } else {
4256                 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
4257                 return;
4258         }
4259 
4260 stl_rxalldone:
4261         outb((EOSRR + portp->uartaddr), ioaddr);
4262         outb(0, (ioaddr + EREG_DATA));
4263 }
4264 
4265 /*****************************************************************************/
4266 
4267 /*
4268  *      Modem interrupt handler. The is called when the modem signal line
4269  *      (DCD) has changed state. Leave most of the work to the off-level
4270  *      processing routine.
4271  */
4272 
4273 static void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr)
4274 {
4275         stlport_t       *portp;
4276         unsigned int    ioack;
4277         unsigned char   misr;
4278 
4279 #if DEBUG
4280         printk("stl_cd1400mdmisr(panelp=%x)\n", (int) panelp);
4281 #endif
4282 
4283         ioack = inb(ioaddr + EREG_MDACK);
4284         if (((ioack & panelp->ackmask) != 0) ||
4285             ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
4286                 printk("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
4287                 return;
4288         }
4289         portp = panelp->ports[(ioack >> 3)];
4290 
4291         outb((MISR + portp->uartaddr), ioaddr);
4292         misr = inb(ioaddr + EREG_DATA);
4293         if (misr & MISR_DCD) {
4294                 set_bit(ASYI_DCDCHANGE, &portp->istate);
4295                 MOD_INC_USE_COUNT;
4296                 if (schedule_task(&portp->tqueue) == 0)
4297                         MOD_DEC_USE_COUNT;
4298                 portp->stats.modem++;
4299         }
4300 
4301         outb((EOSRR + portp->uartaddr), ioaddr);
4302         outb(0, (ioaddr + EREG_DATA));
4303 }
4304 
4305 /*****************************************************************************/
4306 /*                      SC26198 HARDWARE FUNCTIONS                           */
4307 /*****************************************************************************/
4308 
4309 /*
4310  *      These functions get/set/update the registers of the sc26198 UARTs.
4311  *      Access to the sc26198 registers is via an address/data io port pair.
4312  *      (Maybe should make this inline...)
4313  */
4314 
4315 static int stl_sc26198getreg(stlport_t *portp, int regnr)
4316 {
4317         outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4318         return(inb(portp->ioaddr + XP_DATA));
4319 }
4320 
4321 static void stl_sc26198setreg(stlport_t *portp, int regnr, int value)
4322 {
4323         outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4324         outb(value, (portp->ioaddr + XP_DATA));
4325 }
4326 
4327 static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value)
4328 {
4329         outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4330         if (inb(portp->ioaddr + XP_DATA) != value) {
4331                 outb(value, (portp->ioaddr + XP_DATA));
4332                 return(1);
4333         }
4334         return(0);
4335 }
4336 
4337 /*****************************************************************************/
4338 
4339 /*
4340  *      Functions to get and set the sc26198 global registers.
4341  */
4342 
4343 static int stl_sc26198getglobreg(stlport_t *portp, int regnr)
4344 {
4345         outb(regnr, (portp->ioaddr + XP_ADDR));
4346         return(inb(portp->ioaddr + XP_DATA));
4347 }
4348 
4349 #if 0
4350 static void stl_sc26198setglobreg(stlport_t *portp, int regnr, int value)
4351 {
4352         outb(regnr, (portp->ioaddr + XP_ADDR));
4353         outb(value, (portp->ioaddr + XP_DATA));
4354 }
4355 #endif
4356 
4357 /*****************************************************************************/
4358 
4359 /*
4360  *      Inbitialize the UARTs in a panel. We don't care what sort of board
4361  *      these ports are on - since the port io registers are almost
4362  *      identical when dealing with ports.
4363  */
4364 
4365 static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
4366 {
4367         int     chipmask, i;
4368         int     nrchips, ioaddr;
4369 
4370 #if DEBUG
4371         printk("stl_sc26198panelinit(brdp=%x,panelp=%x)\n",
4372                 (int) brdp, (int) panelp);
4373 #endif
4374 
4375         BRDENABLE(panelp->brdnr, panelp->pagenr);
4376 
4377 /*
4378  *      Check that each chip is present and started up OK.
4379  */
4380         chipmask = 0;
4381         nrchips = (panelp->nrports + 4) / SC26198_PORTS;
4382         if (brdp->brdtype == BRD_ECHPCI)
4383                 outb(panelp->pagenr, brdp->ioctrl);
4384 
4385         for (i = 0; (i < nrchips); i++) {
4386                 ioaddr = panelp->iobase + (i * 4); 
4387                 outb(SCCR, (ioaddr + XP_ADDR));
4388                 outb(CR_RESETALL, (ioaddr + XP_DATA));
4389                 outb(TSTR, (ioaddr + XP_ADDR));
4390                 if (inb(ioaddr + XP_DATA) != 0) {
4391                         printk("STALLION: sc26198 not responding, "
4392                                 "brd=%d panel=%d chip=%d\n",
4393                                 panelp->brdnr, panelp->panelnr, i);
4394                         continue;
4395                 }
4396                 chipmask |= (0x1 << i);
4397                 outb(GCCR, (ioaddr + XP_ADDR));
4398                 outb(GCCR_IVRTYPCHANACK, (ioaddr + XP_DATA));
4399                 outb(WDTRCR, (ioaddr + XP_ADDR));
4400                 outb(0xff, (ioaddr + XP_DATA));
4401         }
4402 
4403         BRDDISABLE(panelp->brdnr);
4404         return(chipmask);
4405 }
4406 
4407 /*****************************************************************************/
4408 
4409 /*
4410  *      Initialize hardware specific port registers.
4411  */
4412 
4413 static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
4414 {
4415 #if DEBUG
4416         printk("stl_sc26198portinit(brdp=%x,panelp=%x,portp=%x)\n",
4417                 (int) brdp, (int) panelp, (int) portp);
4418 #endif
4419 
4420         if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
4421             (portp == (stlport_t *) NULL))
4422                 return;
4423 
4424         portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
4425         portp->uartaddr = (portp->portnr & 0x07) << 4;
4426         portp->pagenr = panelp->pagenr;
4427         portp->hwid = 0x1;
4428 
4429         BRDENABLE(portp->brdnr, portp->pagenr);
4430         stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
4431         BRDDISABLE(portp->brdnr);
4432 }
4433 
4434 /*****************************************************************************/
4435 
4436 /*
4437  *      Set up the sc26198 registers for a port based on the termios port
4438  *      settings.
4439  */
4440 
4441 static void stl_sc26198setport(stlport_t *portp, struct termios *tiosp)
4442 {
4443         stlbrd_t        *brdp;
4444         unsigned long   flags;
4445         unsigned int    baudrate;
4446         unsigned char   mr0, mr1, mr2, clk;
4447         unsigned char   imron, imroff, iopr, ipr;
4448 
4449         mr0 = 0;
4450         mr1 = 0;
4451         mr2 = 0;
4452         clk = 0;
4453         iopr = 0;
4454         imron = 0;
4455         imroff = 0;
4456 
4457         brdp = stl_brds[portp->brdnr];
4458         if (brdp == (stlbrd_t *) NULL)
4459                 return;
4460 
4461 /*
4462  *      Set up the RX char ignore mask with those RX error types we
4463  *      can ignore.
4464  */
4465         portp->rxignoremsk = 0;
4466         if (tiosp->c_iflag & IGNPAR)
4467                 portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
4468                         SR_RXOVERRUN);
4469         if (tiosp->c_iflag & IGNBRK)
4470                 portp->rxignoremsk |= SR_RXBREAK;
4471 
4472         portp->rxmarkmsk = SR_RXOVERRUN;
4473         if (tiosp->c_iflag & (INPCK | PARMRK))
4474                 portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
4475         if (tiosp->c_iflag & BRKINT)
4476                 portp->rxmarkmsk |= SR_RXBREAK;
4477 
4478 /*
4479  *      Go through the char size, parity and stop bits and set all the
4480  *      option register appropriately.
4481  */
4482         switch (tiosp->c_cflag & CSIZE) {
4483         case CS5:
4484                 mr1 |= MR1_CS5;
4485                 break;
4486         case CS6:
4487                 mr1 |= MR1_CS6;
4488                 break;
4489         case CS7:
4490                 mr1 |= MR1_CS7;
4491                 break;
4492         default:
4493                 mr1 |= MR1_CS8;
4494                 break;
4495         }
4496 
4497         if (tiosp->c_cflag & CSTOPB)
4498                 mr2 |= MR2_STOP2;
4499         else
4500                 mr2 |= MR2_STOP1;
4501 
4502         if (tiosp->c_cflag & PARENB) {
4503                 if (tiosp->c_cflag & PARODD)
4504                         mr1 |= (MR1_PARENB | MR1_PARODD);
4505                 else
4506                         mr1 |= (MR1_PARENB | MR1_PAREVEN);
4507         } else {
4508                 mr1 |= MR1_PARNONE;
4509         }
4510 
4511         mr1 |= MR1_ERRBLOCK;
4512 
4513 /*
4514  *      Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
4515  *      space for hardware flow control and the like. This should be set to
4516  *      VMIN.
4517  */
4518         mr2 |= MR2_RXFIFOHALF;
4519 
4520 /*
4521  *      Calculate the baud rate timers. For now we will just assume that
4522  *      the input and output baud are the same. The sc26198 has a fixed
4523  *      baud rate table, so only discrete baud rates possible.
4524  */
4525         baudrate = tiosp->c_cflag & CBAUD;
4526         if (baudrate & CBAUDEX) {
4527                 baudrate &= ~CBAUDEX;
4528                 if ((baudrate < 1) || (baudrate > 4))
4529                         tiosp->c_cflag &= ~CBAUDEX;
4530                 else
4531                         baudrate += 15;
4532         }
4533         baudrate = stl_baudrates[baudrate];
4534         if ((tiosp->c_cflag & CBAUD) == B38400) {
4535                 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
4536                         baudrate = 57600;
4537                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
4538                         baudrate = 115200;
4539                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
4540                         baudrate = 230400;
4541                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
4542                         baudrate = 460800;
4543                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
4544                         baudrate = (portp->baud_base / portp->custom_divisor);
4545         }
4546         if (baudrate > STL_SC26198MAXBAUD)
4547                 baudrate = STL_SC26198MAXBAUD;
4548 
4549         if (baudrate > 0) {
4550                 for (clk = 0; (clk < SC26198_NRBAUDS); clk++) {
4551                         if (baudrate <= sc26198_baudtable[clk])
4552                                 break;
4553                 }
4554         }
4555 
4556 /*
4557  *      Check what form of modem signaling is required and set it up.
4558  */
4559         if (tiosp->c_cflag & CLOCAL) {
4560                 portp->flags &= ~ASYNC_CHECK_CD;
4561         } else {
4562                 iopr |= IOPR_DCDCOS;
4563                 imron |= IR_IOPORT;
4564                 portp->flags |= ASYNC_CHECK_CD;
4565         }
4566 
4567 /*
4568  *      Setup sc26198 enhanced modes if we can. In particular we want to
4569  *      handle as much of the flow control as possible automatically. As
4570  *      well as saving a few CPU cycles it will also greatly improve flow
4571  *      control reliability.
4572  */
4573         if (tiosp->c_iflag & IXON) {
4574                 mr0 |= MR0_SWFTX | MR0_SWFT;
4575                 imron |= IR_XONXOFF;
4576         } else {
4577                 imroff |= IR_XONXOFF;
4578         }
4579         if (tiosp->c_iflag & IXOFF)
4580                 mr0 |= MR0_SWFRX;
4581 
4582         if (tiosp->c_cflag & CRTSCTS) {
4583                 mr2 |= MR2_AUTOCTS;
4584                 mr1 |= MR1_AUTORTS;
4585         }
4586 
4587 /*
4588  *      All sc26198 register values calculated so go through and set
4589  *      them all up.
4590  */
4591 
4592 #if DEBUG
4593         printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
4594                 portp->portnr, portp->panelnr, portp->brdnr);
4595         printk("    mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
4596         printk("    iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
4597         printk("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
4598                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
4599                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
4600 #endif
4601 
4602         save_flags(flags);
4603         cli();
4604         BRDENABLE(portp->brdnr, portp->pagenr);
4605         stl_sc26198setreg(portp, IMR, 0);
4606         stl_sc26198updatereg(portp, MR0, mr0);
4607         stl_sc26198updatereg(portp, MR1, mr1);
4608         stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
4609         stl_sc26198updatereg(portp, MR2, mr2);
4610         stl_sc26198updatereg(portp, IOPIOR,
4611                 ((stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr));
4612 
4613         if (baudrate > 0) {
4614                 stl_sc26198setreg(portp, TXCSR, clk);
4615                 stl_sc26198setreg(portp, RXCSR, clk);
4616         }
4617 
4618         stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
4619         stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
4620 
4621         ipr = stl_sc26198getreg(portp, IPR);
4622         if (ipr & IPR_DCD)
4623                 portp->sigs &= ~TIOCM_CD;
4624         else
4625                 portp->sigs |= TIOCM_CD;
4626 
4627         portp->imr = (portp->imr & ~imroff) | imron;
4628         stl_sc26198setreg(portp, IMR, portp->imr);
4629         BRDDISABLE(portp->brdnr);
4630         restore_flags(flags);
4631 }
4632 
4633 /*****************************************************************************/
4634 
4635 /*
4636  *      Set the state of the DTR and RTS signals.
4637  */
4638 
4639 static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts)
4640 {
4641         unsigned char   iopioron, iopioroff;
4642         unsigned long   flags;
4643 
4644 #if DEBUG
4645         printk("stl_sc26198setsignals(portp=%x,dtr=%d,rts=%d)\n",
4646                 (int) portp, dtr, rts);
4647 #endif
4648 
4649         iopioron = 0;
4650         iopioroff = 0;
4651         if (dtr == 0)
4652                 iopioroff |= IPR_DTR;
4653         else if (dtr > 0)
4654                 iopioron |= IPR_DTR;
4655         if (rts == 0)
4656                 iopioroff |= IPR_RTS;
4657         else if (rts > 0)
4658                 iopioron |= IPR_RTS;
4659 
4660         save_flags(flags);
4661         cli();
4662         BRDENABLE(portp->brdnr, portp->pagenr);
4663         stl_sc26198setreg(portp, IOPIOR,
4664                 ((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
4665         BRDDISABLE(portp->brdnr);
4666         restore_flags(flags);
4667 }
4668 
4669 /*****************************************************************************/
4670 
4671 /*
4672  *      Return the state of the signals.
4673  */
4674 
4675 static int stl_sc26198getsignals(stlport_t *portp)
4676 {
4677         unsigned char   ipr;
4678         unsigned long   flags;
4679         int             sigs;
4680 
4681 #if DEBUG
4682         printk("stl_sc26198getsignals(portp=%x)\n", (int) portp);
4683 #endif
4684 
4685         save_flags(flags);
4686         cli();
4687         BRDENABLE(portp->brdnr, portp->pagenr);
4688         ipr = stl_sc26198getreg(portp, IPR);
4689         BRDDISABLE(portp->brdnr);
4690         restore_flags(flags);
4691 
4692         sigs = 0;
4693         sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
4694         sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
4695         sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
4696         sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
4697         sigs |= TIOCM_DSR;
4698         return(sigs);
4699 }
4700 
4701 /*****************************************************************************/
4702 
4703 /*
4704  *      Enable/Disable the Transmitter and/or Receiver.
4705  */
4706 
4707 static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx)
4708 {
4709         unsigned char   ccr;
4710         unsigned long   flags;
4711 
4712 #if DEBUG
4713         printk("stl_sc26198enablerxtx(portp=%x,rx=%d,tx=%d)\n",
4714                 (int) portp, rx, tx);
4715 #endif
4716 
4717         ccr = portp->crenable;
4718         if (tx == 0)
4719                 ccr &= ~CR_TXENABLE;
4720         else if (tx > 0)
4721                 ccr |= CR_TXENABLE;
4722         if (rx == 0)
4723                 ccr &= ~CR_RXENABLE;
4724         else if (rx > 0)
4725                 ccr |= CR_RXENABLE;
4726 
4727         save_flags(flags);
4728         cli();
4729         BRDENABLE(portp->brdnr, portp->pagenr);
4730         stl_sc26198setreg(portp, SCCR, ccr);
4731         BRDDISABLE(portp->brdnr);
4732         portp->crenable = ccr;
4733         restore_flags(flags);
4734 }
4735 
4736 /*****************************************************************************/
4737 
4738 /*
4739  *      Start/stop the Transmitter and/or Receiver.
4740  */
4741 
4742 static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx)
4743 {
4744         unsigned char   imr;
4745         unsigned long   flags;
4746 
4747 #if DEBUG
4748         printk("stl_sc26198startrxtx(portp=%x,rx=%d,tx=%d)\n",
4749                 (int) portp, rx, tx);
4750 #endif
4751 
4752         imr = portp->imr;
4753         if (tx == 0)
4754                 imr &= ~IR_TXRDY;
4755         else if (tx == 1)
4756                 imr |= IR_TXRDY;
4757         if (rx == 0)
4758                 imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
4759         else if (rx > 0)
4760                 imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
4761 
4762         save_flags(flags);
4763         cli();
4764         BRDENABLE(portp->brdnr, portp->pagenr);
4765         stl_sc26198setreg(portp, IMR, imr);
4766         BRDDISABLE(portp->brdnr);
4767         portp->imr = imr;
4768         if (tx > 0)
4769                 set_bit(ASYI_TXBUSY, &portp->istate);
4770         restore_flags(flags);
4771 }
4772 
4773 /*****************************************************************************/
4774 
4775 /*
4776  *      Disable all interrupts from this port.
4777  */
4778 
4779 static void stl_sc26198disableintrs(stlport_t *portp)
4780 {
4781         unsigned long   flags;
4782 
4783 #if DEBUG
4784         printk("stl_sc26198disableintrs(portp=%x)\n", (int) portp);
4785 #endif
4786 
4787         save_flags(flags);
4788         cli();
4789         BRDENABLE(portp->brdnr, portp->pagenr);
4790         portp->imr = 0;
4791         stl_sc26198setreg(portp, IMR, 0);
4792         BRDDISABLE(portp->brdnr);
4793         restore_flags(flags);
4794 }
4795 
4796 /*****************************************************************************/
4797 
4798 static void stl_sc26198sendbreak(stlport_t *portp, int len)
4799 {
4800         unsigned long   flags;
4801 
4802 #if DEBUG
4803         printk("stl_sc26198sendbreak(portp=%x,len=%d)\n", (int) portp, len);
4804 #endif
4805 
4806         save_flags(flags);
4807         cli();
4808         BRDENABLE(portp->brdnr, portp->pagenr);
4809         if (len == 1) {
4810                 stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
4811                 portp->stats.txbreaks++;
4812         } else {
4813                 stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
4814         }
4815         BRDDISABLE(portp->brdnr);
4816         restore_flags(flags);
4817 }
4818 
4819 /*****************************************************************************/
4820 
4821 /*
4822  *      Take flow control actions...
4823  */
4824 
4825 static void stl_sc26198flowctrl(stlport_t *portp, int state)
4826 {
4827         struct tty_struct       *tty;
4828         unsigned long           flags;
4829         unsigned char           mr0;
4830 
4831 #if DEBUG
4832         printk("stl_sc26198flowctrl(portp=%x,state=%x)\n", (int) portp, state);
4833 #endif
4834 
4835         if (portp == (stlport_t *) NULL)
4836                 return;
4837         tty = portp->tty;
4838         if (tty == (struct tty_struct *) NULL)
4839                 return;
4840 
4841         save_flags(flags);
4842         cli();
4843         BRDENABLE(portp->brdnr, portp->pagenr);
4844 
4845         if (state) {
4846                 if (tty->termios->c_iflag & IXOFF) {
4847                         mr0 = stl_sc26198getreg(portp, MR0);
4848                         stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4849                         stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4850                         mr0 |= MR0_SWFRX;
4851                         portp->stats.rxxon++;
4852                         stl_sc26198wait(portp);
4853                         stl_sc26198setreg(portp, MR0, mr0);
4854                 }
4855 /*
4856  *              Question: should we return RTS to what it was before? It may
4857  *              have been set by an ioctl... Suppose not, since if you have
4858  *              hardware flow control set then it is pretty silly to go and
4859  *              set the RTS line by hand.
4860  */
4861                 if (tty->termios->c_cflag & CRTSCTS) {
4862                         stl_sc26198setreg(portp, MR1,
4863                                 (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4864                         stl_sc26198setreg(portp, IOPIOR,
4865                                 (stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
4866                         portp->stats.rxrtson++;
4867                 }
4868         } else {
4869                 if (tty->termios->c_iflag & IXOFF) {
4870                         mr0 = stl_sc26198getreg(portp, MR0);
4871                         stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4872                         stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4873                         mr0 &= ~MR0_SWFRX;
4874                         portp->stats.rxxoff++;
4875                         stl_sc26198wait(portp);
4876                         stl_sc26198setreg(portp, MR0, mr0);
4877                 }
4878                 if (tty->termios->c_cflag & CRTSCTS) {
4879                         stl_sc26198setreg(portp, MR1,
4880                                 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4881                         stl_sc26198setreg(portp, IOPIOR,
4882                                 (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4883                         portp->stats.rxrtsoff++;
4884                 }
4885         }
4886 
4887         BRDDISABLE(portp->brdnr);
4888         restore_flags(flags);
4889 }
4890 
4891 /*****************************************************************************/
4892 
4893 /*
4894  *      Send a flow control character.
4895  */
4896 
4897 static void stl_sc26198sendflow(stlport_t *portp, int state)
4898 {
4899         struct tty_struct       *tty;
4900         unsigned long           flags;
4901         unsigned char           mr0;
4902 
4903 #if DEBUG
4904         printk("stl_sc26198sendflow(portp=%x,state=%x)\n", (int) portp, state);
4905 #endif
4906 
4907         if (portp == (stlport_t *) NULL)
4908                 return;
4909         tty = portp->tty;
4910         if (tty == (struct tty_struct *) NULL)
4911                 return;
4912 
4913         save_flags(flags);
4914         cli();
4915         BRDENABLE(portp->brdnr, portp->pagenr);
4916         if (state) {
4917                 mr0 = stl_sc26198getreg(portp, MR0);
4918                 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4919                 stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4920                 mr0 |= MR0_SWFRX;
4921                 portp->stats.rxxon++;
4922                 stl_sc26198wait(portp);
4923                 stl_sc26198setreg(portp, MR0, mr0);
4924         } else {
4925                 mr0 = stl_sc26198getreg(portp, MR0);
4926                 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4927                 stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4928                 mr0 &= ~MR0_SWFRX;
4929                 portp->stats.rxxoff++;
4930                 stl_sc26198wait(portp);
4931                 stl_sc26198setreg(portp, MR0, mr0);
4932         }
4933         BRDDISABLE(portp->brdnr);
4934         restore_flags(flags);
4935 }
4936 
4937 /*****************************************************************************/
4938 
4939 static void stl_sc26198flush(stlport_t *portp)
4940 {
4941         unsigned long   flags;
4942 
4943 #if DEBUG
4944         printk("stl_sc26198flush(portp=%x)\n", (int) portp);
4945 #endif
4946 
4947         if (portp == (stlport_t *) NULL)
4948                 return;
4949 
4950         save_flags(flags);
4951         cli();
4952         BRDENABLE(portp->brdnr, portp->pagenr);
4953         stl_sc26198setreg(portp, SCCR, CR_TXRESET);
4954         stl_sc26198setreg(portp, SCCR, portp->crenable);
4955         BRDDISABLE(portp->brdnr);
4956         portp->tx.tail = portp->tx.head;
4957         restore_flags(flags);
4958 }
4959 
4960 /*****************************************************************************/
4961 
4962 /*
4963  *      Return the current state of data flow on this port. This is only
4964  *      really interresting when determining if data has fully completed
4965  *      transmission or not... The sc26198 interrupt scheme cannot
4966  *      determine when all data has actually drained, so we need to
4967  *      check the port statusy register to be sure.
4968  */
4969 
4970 static int stl_sc26198datastate(stlport_t *portp)
4971 {
4972         unsigned long   flags;
4973         unsigned char   sr;
4974 
4975 #if DEBUG
4976         printk("stl_sc26198datastate(portp=%x)\n", (int) portp);
4977 #endif
4978 
4979         if (portp == (stlport_t *) NULL)
4980                 return(0);
4981         if (test_bit(ASYI_TXBUSY, &portp->istate))
4982                 return(1);
4983 
4984         save_flags(flags);
4985         cli();
4986         BRDENABLE(portp->brdnr, portp->pagenr);
4987         sr = stl_sc26198getreg(portp, SR);
4988         BRDDISABLE(portp->brdnr);
4989         restore_flags(flags);
4990 
4991         return((sr & SR_TXEMPTY) ? 0 : 1);
4992 }
4993 
4994 /*****************************************************************************/
4995 
4996 /*
4997  *      Delay for a small amount of time, to give the sc26198 a chance
4998  *      to process a command...
4999  */
5000 
5001 static void stl_sc26198wait(stlport_t *portp)
5002 {
5003         int     i;
5004 
5005 #if DEBUG
5006         printk("stl_sc26198wait(portp=%x)\n", (int) portp);
5007 #endif
5008 
5009         if (portp == (stlport_t *) NULL)
5010                 return;
5011 
5012         for (i = 0; (i < 20); i++)
5013                 stl_sc26198getglobreg(portp, TSTR);
5014 }
5015 
5016 /*****************************************************************************/
5017 
5018 /*
5019  *      If we are TX flow controlled and in IXANY mode then we may
5020  *      need to unflow control here. We gotta do this because of the
5021  *      automatic flow control modes of the sc26198.
5022  */
5023 
5024 static inline void stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty)
5025 {
5026         unsigned char   mr0;
5027 
5028         mr0 = stl_sc26198getreg(portp, MR0);
5029         stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
5030         stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
5031         stl_sc26198wait(portp);
5032         stl_sc26198setreg(portp, MR0, mr0);
5033         clear_bit(ASYI_TXFLOWED, &portp->istate);
5034 }
5035 
5036 /*****************************************************************************/
5037 
5038 /*
5039  *      Interrupt service routine for sc26198 panels.
5040  */
5041 
5042 static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase)
5043 {
5044         stlport_t       *portp;
5045         unsigned int    iack;
5046 
5047 /* 
5048  *      Work around bug in sc26198 chip... Cannot have A6 address
5049  *      line of UART high, else iack will be returned as 0.
5050  */
5051         outb(0, (iobase + 1));
5052 
5053         iack = inb(iobase + XP_IACK);
5054         portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
5055 
5056         if (iack & IVR_RXDATA)
5057                 stl_sc26198rxisr(portp, iack);
5058         else if (iack & IVR_TXDATA)
5059                 stl_sc26198txisr(portp);
5060         else
5061                 stl_sc26198otherisr(portp, iack);
5062 }
5063 
5064 /*****************************************************************************/
5065 
5066 /*
5067  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
5068  *      chars is pretty simple, stuff as many as possible from the TX buffer
5069  *      into the sc26198 FIFO.
5070  *      In practice it is possible that interrupts are enabled but that the
5071  *      port has been hung up. Need to handle not having any TX buffer here,
5072  *      this is done by using the side effect that head and tail will also
5073  *      be NULL if the buffer has been freed.
5074  */
5075 
5076 static void stl_sc26198txisr(stlport_t *portp)
5077 {
5078         unsigned int    ioaddr;
5079         unsigned char   mr0;
5080         int             len, stlen;
5081         char            *head, *tail;
5082 
5083 #if DEBUG
5084         printk("stl_sc26198txisr(portp=%x)\n", (int) portp);
5085 #endif
5086 
5087         ioaddr = portp->ioaddr;
5088         head = portp->tx.head;
5089         tail = portp->tx.tail;
5090         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
5091         if ((len == 0) || ((len < STL_TXBUFLOW) &&
5092             (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
5093                 set_bit(ASYI_TXLOW, &portp->istate);
5094                 MOD_INC_USE_COUNT;
5095                 if (schedule_task(&portp->tqueue) == 0)
5096                         MOD_DEC_USE_COUNT;
5097         }
5098 
5099         if (len == 0) {
5100                 outb((MR0 | portp->uartaddr), (ioaddr + XP_ADDR));
5101                 mr0 = inb(ioaddr + XP_DATA);
5102                 if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
5103                         portp->imr &= ~IR_TXRDY;
5104                         outb((IMR | portp->uartaddr), (ioaddr + XP_ADDR));
5105                         outb(portp->imr, (ioaddr + XP_DATA));
5106                         clear_bit(ASYI_TXBUSY, &portp->istate);
5107                 } else {
5108                         mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
5109                         outb(mr0, (ioaddr + XP_DATA));
5110                 }
5111         } else {
5112                 len = MIN(len, SC26198_TXFIFOSIZE);
5113                 portp->stats.txtotal += len;
5114                 stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
5115                 outb(GTXFIFO, (ioaddr + XP_ADDR));
5116                 outsb((ioaddr + XP_DATA), tail, stlen);
5117                 len -= stlen;
5118                 tail += stlen;
5119                 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
5120                         tail = portp->tx.buf;
5121                 if (len > 0) {
5122                         outsb((ioaddr + XP_DATA), tail, len);
5123                         tail += len;
5124                 }
5125                 portp->tx.tail = tail;
5126         }
5127 }
5128 
5129 /*****************************************************************************/
5130 
5131 /*
5132  *      Receive character interrupt handler. Determine if we have good chars
5133  *      or bad chars and then process appropriately. Good chars are easy
5134  *      just shove the lot into the RX buffer and set all status byte to 0.
5135  *      If a bad RX char then process as required. This routine needs to be
5136  *      fast!  In practice it is possible that we get an interrupt on a port
5137  *      that is closed. This can happen on hangups - since they completely
5138  *      shutdown a port not in user context. Need to handle this case.
5139  */
5140 
5141 static void stl_sc26198rxisr(stlport_t *portp, unsigned int iack)
5142 {
5143         struct tty_struct       *tty;
5144         unsigned int            len, buflen, ioaddr;
5145 
5146 #if DEBUG
5147         printk("stl_sc26198rxisr(portp=%x,iack=%x)\n", (int) portp, iack);
5148 #endif
5149 
5150         tty = portp->tty;
5151         ioaddr = portp->ioaddr;
5152         outb(GIBCR, (ioaddr + XP_ADDR));
5153         len = inb(ioaddr + XP_DATA) + 1;
5154 
5155         if ((iack & IVR_TYPEMASK) == IVR_RXDATA) {
5156                 if ((tty == (struct tty_struct *) NULL) ||
5157                     (tty->flip.char_buf_ptr == (char *) NULL) ||
5158                     ((buflen = TTY_FLIPBUF_SIZE - tty->flip.count) == 0)) {
5159                         len = MIN(len, sizeof(stl_unwanted));
5160                         outb(GRXFIFO, (ioaddr + XP_ADDR));
5161                         insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
5162                         portp->stats.rxlost += len;
5163                         portp->stats.rxtotal += len;
5164                 } else {
5165                         len = MIN(len, buflen);
5166                         if (len > 0) {
5167                                 outb(GRXFIFO, (ioaddr + XP_ADDR));
5168                                 insb((ioaddr + XP_DATA), tty->flip.char_buf_ptr, len);
5169                                 memset(tty->flip.flag_buf_ptr, 0, len);
5170                                 tty->flip.flag_buf_ptr += len;
5171                                 tty->flip.char_buf_ptr += len;
5172                                 tty->flip.count += len;
5173                                 tty_schedule_flip(tty);
5174                                 portp->stats.rxtotal += len;
5175                         }
5176                 }
5177         } else {
5178                 stl_sc26198rxbadchars(portp);
5179         }
5180 
5181 /*
5182  *      If we are TX flow controlled and in IXANY mode then we may need
5183  *      to unflow control here. We gotta do this because of the automatic
5184  *      flow control modes of the sc26198.
5185  */
5186         if (test_bit(ASYI_TXFLOWED, &portp->istate)) {
5187                 if ((tty != (struct tty_struct *) NULL) &&
5188                     (tty->termios != (struct termios *) NULL) &&
5189                     (tty->termios->c_iflag & IXANY)) {
5190                         stl_sc26198txunflow(portp, tty);
5191                 }
5192         }
5193 }
5194 
5195 /*****************************************************************************/
5196 
5197 /*
5198  *      Process an RX bad character.
5199  */
5200 
5201 static void inline stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch)
5202 {
5203         struct tty_struct       *tty;
5204         unsigned int            ioaddr;
5205 
5206         tty = portp->tty;
5207         ioaddr = portp->ioaddr;
5208 
5209         if (status & SR_RXPARITY)
5210                 portp->stats.rxparity++;
5211         if (status & SR_RXFRAMING)
5212                 portp->stats.rxframing++;
5213         if (status & SR_RXOVERRUN)
5214                 portp->stats.rxoverrun++;
5215         if (status & SR_RXBREAK)
5216                 portp->stats.rxbreaks++;
5217 
5218         if ((tty != (struct tty_struct *) NULL) &&
5219             ((portp->rxignoremsk & status) == 0)) {
5220                 if (portp->rxmarkmsk & status) {
5221                         if (status & SR_RXBREAK) {
5222                                 status = TTY_BREAK;
5223                                 if (portp->flags & ASYNC_SAK) {
5224                                         do_SAK(tty);
5225                                         BRDENABLE(portp->brdnr, portp->pagenr);
5226                                 }
5227                         } else if (status & SR_RXPARITY) {
5228                                 status = TTY_PARITY;
5229                         } else if (status & SR_RXFRAMING) {
5230                                 status = TTY_FRAME;
5231                         } else if(status & SR_RXOVERRUN) {
5232                                 status = TTY_OVERRUN;
5233                         } else {
5234                                 status = 0;
5235                         }
5236                 } else {
5237                         status = 0;
5238                 }
5239 
5240                 if (tty->flip.char_buf_ptr != (char *) NULL) {
5241                         if (tty->flip.count < TTY_FLIPBUF_SIZE) {
5242                                 *tty->flip.flag_buf_ptr++ = status;
5243                                 *tty->flip.char_buf_ptr++ = ch;
5244                                 tty->flip.count++;
5245                         }
5246                         tty_schedule_flip(tty);
5247                 }
5248 
5249                 if (status == 0)
5250                         portp->stats.rxtotal++;
5251         }
5252 }
5253 
5254 /*****************************************************************************/
5255 
5256 /*
5257  *      Process all characters in the RX FIFO of the UART. Check all char
5258  *      status bytes as well, and process as required. We need to check
5259  *      all bytes in the FIFO, in case some more enter the FIFO while we
5260  *      are here. To get the exact character error type we need to switch
5261  *      into CHAR error mode (that is why we need to make sure we empty
5262  *      the FIFO).
5263  */
5264 
5265 static void stl_sc26198rxbadchars(stlport_t *portp)
5266 {
5267         unsigned char   status, mr1;
5268         char            ch;
5269 
5270 /*
5271  *      To get the precise error type for each character we must switch
5272  *      back into CHAR error mode.
5273  */
5274         mr1 = stl_sc26198getreg(portp, MR1);
5275         stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
5276 
5277         while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
5278                 stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
5279                 ch = stl_sc26198getreg(portp, RXFIFO);
5280                 stl_sc26198rxbadch(portp, status, ch);
5281         }
5282 
5283 /*
5284  *      To get correct interrupt class we must switch back into BLOCK
5285  *      error mode.
5286  */
5287         stl_sc26198setreg(portp, MR1, mr1);
5288 }
5289 
5290 /*****************************************************************************/
5291 
5292 /*
5293  *      Other interrupt handler. This includes modem signals, flow
5294  *      control actions, etc. Most stuff is left to off-level interrupt
5295  *      processing time.
5296  */
5297 
5298 static void stl_sc26198otherisr(stlport_t *portp, unsigned int iack)
5299 {
5300         unsigned char   cir, ipr, xisr;
5301 
5302 #if DEBUG
5303         printk("stl_sc26198otherisr(portp=%x,iack=%x)\n", (int) portp, iack);
5304 #endif
5305 
5306         cir = stl_sc26198getglobreg(portp, CIR);
5307 
5308         switch (cir & CIR_SUBTYPEMASK) {
5309         case CIR_SUBCOS:
5310                 ipr = stl_sc26198getreg(portp, IPR);
5311                 if (ipr & IPR_DCDCHANGE) {
5312                         set_bit(ASYI_DCDCHANGE, &portp->istate);
5313                         MOD_INC_USE_COUNT;
5314                         if (schedule_task(&portp->tqueue) == 0)
5315                                 MOD_DEC_USE_COUNT;
5316                         portp->stats.modem++;
5317                 }
5318                 break;
5319         case CIR_SUBXONXOFF:
5320                 xisr = stl_sc26198getreg(portp, XISR);
5321                 if (xisr & XISR_RXXONGOT) {
5322                         set_bit(ASYI_TXFLOWED, &portp->istate);
5323                         portp->stats.txxoff++;
5324                 }
5325                 if (xisr & XISR_RXXOFFGOT) {
5326                         clear_bit(ASYI_TXFLOWED, &portp->istate);
5327                         portp->stats.txxon++;
5328                 }
5329                 break;
5330         case CIR_SUBBREAK:
5331                 stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
5332                 stl_sc26198rxbadchars(portp);
5333                 break;
5334         default:
5335                 break;
5336         }
5337 }
5338 
5339 /*****************************************************************************/
5340 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.