1 /*****************************************************************************/
4 * stallion.c -- stallion multiport serial driver.
6 * Copyright (C) 1996-1999 Stallion Technologies
7 * Copyright (C) 1994-1996 Greg Ungerer.
9 * This code is loosely based on the Linux serial driver, written by
10 * Linus Torvalds, Theodore T'so and others.
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.
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.
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.
27 /*****************************************************************************/
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
34 #include <linux/serial.h>
35 #include <linux/cd1400.h>
36 #include <linux/sc26198.h>
37 #include <linux/comstats.h>
38 #include <linux/stallion.h>
39 #include <linux/ioport.h>
40 #include <linux/init.h>
41 #include <linux/smp_lock.h>
42 #include <linux/device.h>
43 #include <linux/delay.h>
44 #include <linux/ctype.h>
47 #include <asm/uaccess.h>
49 #include <linux/pci.h>
51 /*****************************************************************************/
54 * Define different board types. Use the standard Stallion "assigned"
55 * board numbers. Boards supported in this driver are abbreviated as
56 * EIO = EasyIO and ECH = EasyConnection 8/32.
62 #define BRD_ECH64PCI 27
63 #define BRD_EASYIOPCI 28
69 unsigned long memaddr;
74 static unsigned int stl_nrbrds;
76 /*****************************************************************************/
79 * Define some important driver characteristics. Device major numbers
80 * allocated as per Linux Device Registry.
82 #ifndef STL_SIOMEMMAJOR
83 #define STL_SIOMEMMAJOR 28
85 #ifndef STL_SERIALMAJOR
86 #define STL_SERIALMAJOR 24
88 #ifndef STL_CALLOUTMAJOR
89 #define STL_CALLOUTMAJOR 25
93 * Set the TX buffer size. Bigger is better, but we don't want
94 * to chew too much memory with buffers!
96 #define STL_TXBUFLOW 512
97 #define STL_TXBUFSIZE 4096
99 /*****************************************************************************/
102 * Define our local driver identity first. Set up stuff to deal with
103 * all the local structures required by a serial tty driver.
105 static char *stl_drvtitle = "Stallion Multiport Serial Driver";
106 static char *stl_drvname = "stallion";
107 static char *stl_drvversion = "5.6.0";
109 static struct tty_driver *stl_serial;
112 * Define a local default termios struct. All ports will be created
113 * with this termios initially. Basically all it defines is a raw port
114 * at 9600, 8 data bits, 1 stop bit.
116 static struct ktermios stl_deftermios = {
117 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
124 * Define global place to put buffer overflow characters.
126 static char stl_unwanted[SC26198_RXFIFOSIZE];
128 /*****************************************************************************/
130 static DEFINE_MUTEX(stl_brdslock);
131 static struct stlbrd *stl_brds[STL_MAXBRDS];
133 static const struct tty_port_operations stl_port_ops;
136 * Per board state flags. Used with the state field of the board struct.
137 * Not really much here!
139 #define BRD_FOUND 0x1
140 #define STL_PROBED 0x2
144 * Define the port structure istate flags. These set of flags are
145 * modified at interrupt time - so setting and reseting them needs
146 * to be atomic. Use the bit clear/setting routines for this.
148 #define ASYI_TXBUSY 1
150 #define ASYI_TXFLOWED 3
153 * Define an array of board names as printable strings. Handy for
154 * referencing boards when printing trace and stuff.
156 static char *stl_brdnames[] = {
188 /*****************************************************************************/
191 * Define some string labels for arguments passed from the module
192 * load line. These allow for easy board definitions, and easy
193 * modification of the io, memory and irq resoucres.
195 static unsigned int stl_nargs;
196 static char *board0[4];
197 static char *board1[4];
198 static char *board2[4];
199 static char *board3[4];
201 static char **stl_brdsp[] = {
209 * Define a set of common board names, and types. This is used to
210 * parse any module arguments.
217 { "easyio", BRD_EASYIO },
218 { "eio", BRD_EASYIO },
219 { "20", BRD_EASYIO },
220 { "ec8/32", BRD_ECH },
221 { "ec8/32-at", BRD_ECH },
222 { "ec8/32-isa", BRD_ECH },
224 { "echat", BRD_ECH },
226 { "ec8/32-mc", BRD_ECHMC },
227 { "ec8/32-mca", BRD_ECHMC },
228 { "echmc", BRD_ECHMC },
229 { "echmca", BRD_ECHMC },
231 { "ec8/32-pc", BRD_ECHPCI },
232 { "ec8/32-pci", BRD_ECHPCI },
233 { "26", BRD_ECHPCI },
234 { "ec8/64-pc", BRD_ECH64PCI },
235 { "ec8/64-pci", BRD_ECH64PCI },
236 { "ech-pci", BRD_ECH64PCI },
237 { "echpci", BRD_ECH64PCI },
238 { "echpc", BRD_ECH64PCI },
239 { "27", BRD_ECH64PCI },
240 { "easyio-pc", BRD_EASYIOPCI },
241 { "easyio-pci", BRD_EASYIOPCI },
242 { "eio-pci", BRD_EASYIOPCI },
243 { "eiopci", BRD_EASYIOPCI },
244 { "28", BRD_EASYIOPCI },
248 * Define the module agruments.
251 module_param_array(board0, charp, &stl_nargs, 0);
252 MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
253 module_param_array(board1, charp, &stl_nargs, 0);
254 MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
255 module_param_array(board2, charp, &stl_nargs, 0);
256 MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
257 module_param_array(board3, charp, &stl_nargs, 0);
258 MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");
260 /*****************************************************************************/
263 * Hardware ID bits for the EasyIO and ECH boards. These defines apply
264 * to the directly accessible io ports of these boards (not the uarts -
265 * they are in cd1400.h and sc26198.h).
267 #define EIO_8PORTRS 0x04
268 #define EIO_4PORTRS 0x05
269 #define EIO_8PORTDI 0x00
270 #define EIO_8PORTM 0x06
272 #define EIO_IDBITMASK 0x07
274 #define EIO_BRDMASK 0xf0
277 #define ID_BRD16 0x30
279 #define EIO_INTRPEND 0x08
280 #define EIO_INTEDGE 0x00
281 #define EIO_INTLEVEL 0x08
285 #define ECH_IDBITMASK 0xe0
286 #define ECH_BRDENABLE 0x08
287 #define ECH_BRDDISABLE 0x00
288 #define ECH_INTENABLE 0x01
289 #define ECH_INTDISABLE 0x00
290 #define ECH_INTLEVEL 0x02
291 #define ECH_INTEDGE 0x00
292 #define ECH_INTRPEND 0x01
293 #define ECH_BRDRESET 0x01
295 #define ECHMC_INTENABLE 0x01
296 #define ECHMC_BRDRESET 0x02
298 #define ECH_PNLSTATUS 2
299 #define ECH_PNL16PORT 0x20
300 #define ECH_PNLIDMASK 0x07
301 #define ECH_PNLXPID 0x40
302 #define ECH_PNLINTRPEND 0x80
304 #define ECH_ADDR2MASK 0x1e0
307 * Define the vector mapping bits for the programmable interrupt board
308 * hardware. These bits encode the interrupt for the board to use - it
309 * is software selectable (except the EIO-8M).
311 static unsigned char stl_vecmap[] = {
312 0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
313 0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
317 * Lock ordering is that you may not take stallion_lock holding
321 static spinlock_t brd_lock; /* Guard the board mapping */
322 static spinlock_t stallion_lock; /* Guard the tty driver */
325 * Set up enable and disable macros for the ECH boards. They require
326 * the secondary io address space to be activated and deactivated.
327 * This way all ECH boards can share their secondary io region.
328 * If this is an ECH-PCI board then also need to set the page pointer
329 * to point to the correct page.
331 #define BRDENABLE(brdnr,pagenr) \
332 if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
333 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE), \
334 stl_brds[(brdnr)]->ioctrl); \
335 else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI) \
336 outb((pagenr), stl_brds[(brdnr)]->ioctrl);
338 #define BRDDISABLE(brdnr) \
339 if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
340 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE), \
341 stl_brds[(brdnr)]->ioctrl);
343 #define STL_CD1400MAXBAUD 230400
344 #define STL_SC26198MAXBAUD 460800
346 #define STL_BAUDBASE 115200
347 #define STL_CLOSEDELAY (5 * HZ / 10)
349 /*****************************************************************************/
352 * Define the Stallion PCI vendor and device IDs.
354 #ifndef PCI_VENDOR_ID_STALLION
355 #define PCI_VENDOR_ID_STALLION 0x124d
357 #ifndef PCI_DEVICE_ID_ECHPCI832
358 #define PCI_DEVICE_ID_ECHPCI832 0x0000
360 #ifndef PCI_DEVICE_ID_ECHPCI864
361 #define PCI_DEVICE_ID_ECHPCI864 0x0002
363 #ifndef PCI_DEVICE_ID_EIOPCI
364 #define PCI_DEVICE_ID_EIOPCI 0x0003
368 * Define structure to hold all Stallion PCI boards.
371 static struct pci_device_id stl_pcibrds[] = {
372 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864),
373 .driver_data = BRD_ECH64PCI },
374 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI),
375 .driver_data = BRD_EASYIOPCI },
376 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832),
377 .driver_data = BRD_ECHPCI },
378 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410),
379 .driver_data = BRD_ECHPCI },
382 MODULE_DEVICE_TABLE(pci, stl_pcibrds);
384 /*****************************************************************************/
387 * Define macros to extract a brd/port number from a minor number.
389 #define MINOR2BRD(min) (((min) & 0xc0) >> 6)
390 #define MINOR2PORT(min) ((min) & 0x3f)
393 * Define a baud rate table that converts termios baud rate selector
394 * into the actual baud rate value. All baud rate calculations are
395 * based on the actual baud rate required.
397 static unsigned int stl_baudrates[] = {
398 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
399 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
402 /*****************************************************************************/
405 * Declare all those functions in this driver!
408 static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
409 static int stl_brdinit(struct stlbrd *brdp);
410 static int stl_getportstats(struct tty_struct *tty, struct stlport *portp, comstats_t __user *cp);
411 static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp);
412 static int stl_waitcarrier(struct tty_struct *tty, struct stlport *portp, struct file *filp);
415 * CD1400 uart specific handling functions.
417 static void stl_cd1400setreg(struct stlport *portp, int regnr, int value);
418 static int stl_cd1400getreg(struct stlport *portp, int regnr);
419 static int stl_cd1400updatereg(struct stlport *portp, int regnr, int value);
420 static int stl_cd1400panelinit(struct stlbrd *brdp, struct stlpanel *panelp);
421 static void stl_cd1400portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
422 static void stl_cd1400setport(struct stlport *portp, struct ktermios *tiosp);
423 static int stl_cd1400getsignals(struct stlport *portp);
424 static void stl_cd1400setsignals(struct stlport *portp, int dtr, int rts);
425 static void stl_cd1400ccrwait(struct stlport *portp);
426 static void stl_cd1400enablerxtx(struct stlport *portp, int rx, int tx);
427 static void stl_cd1400startrxtx(struct stlport *portp, int rx, int tx);
428 static void stl_cd1400disableintrs(struct stlport *portp);
429 static void stl_cd1400sendbreak(struct stlport *portp, int len);
430 static void stl_cd1400flowctrl(struct stlport *portp, int state);
431 static void stl_cd1400sendflow(struct stlport *portp, int state);
432 static void stl_cd1400flush(struct stlport *portp);
433 static int stl_cd1400datastate(struct stlport *portp);
434 static void stl_cd1400eiointr(struct stlpanel *panelp, unsigned int iobase);
435 static void stl_cd1400echintr(struct stlpanel *panelp, unsigned int iobase);
436 static void stl_cd1400txisr(struct stlpanel *panelp, int ioaddr);
437 static void stl_cd1400rxisr(struct stlpanel *panelp, int ioaddr);
438 static void stl_cd1400mdmisr(struct stlpanel *panelp, int ioaddr);
440 static inline int stl_cd1400breakisr(struct stlport *portp, int ioaddr);
443 * SC26198 uart specific handling functions.
445 static void stl_sc26198setreg(struct stlport *portp, int regnr, int value);
446 static int stl_sc26198getreg(struct stlport *portp, int regnr);
447 static int stl_sc26198updatereg(struct stlport *portp, int regnr, int value);
448 static int stl_sc26198getglobreg(struct stlport *portp, int regnr);
449 static int stl_sc26198panelinit(struct stlbrd *brdp, struct stlpanel *panelp);
450 static void stl_sc26198portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
451 static void stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp);
452 static int stl_sc26198getsignals(struct stlport *portp);
453 static void stl_sc26198setsignals(struct stlport *portp, int dtr, int rts);
454 static void stl_sc26198enablerxtx(struct stlport *portp, int rx, int tx);
455 static void stl_sc26198startrxtx(struct stlport *portp, int rx, int tx);
456 static void stl_sc26198disableintrs(struct stlport *portp);
457 static void stl_sc26198sendbreak(struct stlport *portp, int len);
458 static void stl_sc26198flowctrl(struct stlport *portp, int state);
459 static void stl_sc26198sendflow(struct stlport *portp, int state);
460 static void stl_sc26198flush(struct stlport *portp);
461 static int stl_sc26198datastate(struct stlport *portp);
462 static void stl_sc26198wait(struct stlport *portp);
463 static void stl_sc26198txunflow(struct stlport *portp, struct tty_struct *tty);
464 static void stl_sc26198intr(struct stlpanel *panelp, unsigned int iobase);
465 static void stl_sc26198txisr(struct stlport *port);
466 static void stl_sc26198rxisr(struct stlport *port, unsigned int iack);
467 static void stl_sc26198rxbadch(struct stlport *portp, unsigned char status, char ch);
468 static void stl_sc26198rxbadchars(struct stlport *portp);
469 static void stl_sc26198otherisr(struct stlport *port, unsigned int iack);
471 /*****************************************************************************/
474 * Generic UART support structure.
476 typedef struct uart {
477 int (*panelinit)(struct stlbrd *brdp, struct stlpanel *panelp);
478 void (*portinit)(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
479 void (*setport)(struct stlport *portp, struct ktermios *tiosp);
480 int (*getsignals)(struct stlport *portp);
481 void (*setsignals)(struct stlport *portp, int dtr, int rts);
482 void (*enablerxtx)(struct stlport *portp, int rx, int tx);
483 void (*startrxtx)(struct stlport *portp, int rx, int tx);
484 void (*disableintrs)(struct stlport *portp);
485 void (*sendbreak)(struct stlport *portp, int len);
486 void (*flowctrl)(struct stlport *portp, int state);
487 void (*sendflow)(struct stlport *portp, int state);
488 void (*flush)(struct stlport *portp);
489 int (*datastate)(struct stlport *portp);
490 void (*intr)(struct stlpanel *panelp, unsigned int iobase);
494 * Define some macros to make calling these functions nice and clean.
496 #define stl_panelinit (* ((uart_t *) panelp->uartp)->panelinit)
497 #define stl_portinit (* ((uart_t *) portp->uartp)->portinit)
498 #define stl_setport (* ((uart_t *) portp->uartp)->setport)
499 #define stl_getsignals (* ((uart_t *) portp->uartp)->getsignals)
500 #define stl_setsignals (* ((uart_t *) portp->uartp)->setsignals)
501 #define stl_enablerxtx (* ((uart_t *) portp->uartp)->enablerxtx)
502 #define stl_startrxtx (* ((uart_t *) portp->uartp)->startrxtx)
503 #define stl_disableintrs (* ((uart_t *) portp->uartp)->disableintrs)
504 #define stl_sendbreak (* ((uart_t *) portp->uartp)->sendbreak)
505 #define stl_flowctrl (* ((uart_t *) portp->uartp)->flowctrl)
506 #define stl_sendflow (* ((uart_t *) portp->uartp)->sendflow)
507 #define stl_flush (* ((uart_t *) portp->uartp)->flush)
508 #define stl_datastate (* ((uart_t *) portp->uartp)->datastate)
510 /*****************************************************************************/
513 * CD1400 UART specific data initialization.
515 static uart_t stl_cd1400uart = {
519 stl_cd1400getsignals,
520 stl_cd1400setsignals,
521 stl_cd1400enablerxtx,
523 stl_cd1400disableintrs,
533 * Define the offsets within the register bank of a cd1400 based panel.
534 * These io address offsets are common to the EasyIO board as well.
542 #define EREG_BANKSIZE 8
544 #define CD1400_CLK 25000000
545 #define CD1400_CLK8M 20000000
548 * Define the cd1400 baud rate clocks. These are used when calculating
549 * what clock and divisor to use for the required baud rate. Also
550 * define the maximum baud rate allowed, and the default base baud.
552 static int stl_cd1400clkdivs[] = {
553 CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
556 /*****************************************************************************/
559 * SC26198 UART specific data initization.
561 static uart_t stl_sc26198uart = {
562 stl_sc26198panelinit,
565 stl_sc26198getsignals,
566 stl_sc26198setsignals,
567 stl_sc26198enablerxtx,
568 stl_sc26198startrxtx,
569 stl_sc26198disableintrs,
570 stl_sc26198sendbreak,
574 stl_sc26198datastate,
579 * Define the offsets within the register bank of a sc26198 based panel.
587 #define XP_BANKSIZE 4
590 * Define the sc26198 baud rate table. Offsets within the table
591 * represent the actual baud rate selector of sc26198 registers.
593 static unsigned int sc26198_baudtable[] = {
594 50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
595 4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
596 230400, 460800, 921600
599 #define SC26198_NRBAUDS ARRAY_SIZE(sc26198_baudtable)
601 /*****************************************************************************/
604 * Define the driver info for a user level control device. Used mainly
605 * to get at port stats - only not using the port device itself.
607 static const struct file_operations stl_fsiomem = {
608 .owner = THIS_MODULE,
609 .ioctl = stl_memioctl,
612 static struct class *stallion_class;
614 static void stl_cd_change(struct stlport *portp)
616 unsigned int oldsigs = portp->sigs;
617 struct tty_struct *tty = tty_port_tty_get(&portp->port);
622 portp->sigs = stl_getsignals(portp);
624 if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
625 wake_up_interruptible(&portp->port.open_wait);
627 if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0))
628 if (portp->port.flags & ASYNC_CHECK_CD)
634 * Check for any arguments passed in on the module load command line.
637 /*****************************************************************************/
640 * Parse the supplied argument string, into the board conf struct.
643 static int __init stl_parsebrd(struct stlconf *confp, char **argp)
648 pr_debug("stl_parsebrd(confp=%p,argp=%p)\n", confp, argp);
650 if ((argp[0] == NULL) || (*argp[0] == 0))
653 for (sp = argp[0], i = 0; (*sp != 0) && (i < 25); sp++, i++)
656 for (i = 0; i < ARRAY_SIZE(stl_brdstr); i++)
657 if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
660 if (i == ARRAY_SIZE(stl_brdstr)) {
661 printk("STALLION: unknown board name, %s?\n", argp[0]);
665 confp->brdtype = stl_brdstr[i].type;
668 if ((argp[i] != NULL) && (*argp[i] != 0))
669 confp->ioaddr1 = simple_strtoul(argp[i], NULL, 0);
671 if (confp->brdtype == BRD_ECH) {
672 if ((argp[i] != NULL) && (*argp[i] != 0))
673 confp->ioaddr2 = simple_strtoul(argp[i], NULL, 0);
676 if ((argp[i] != NULL) && (*argp[i] != 0))
677 confp->irq = simple_strtoul(argp[i], NULL, 0);
681 /*****************************************************************************/
684 * Allocate a new board structure. Fill out the basic info in it.
687 static struct stlbrd *stl_allocbrd(void)
691 brdp = kzalloc(sizeof(struct stlbrd), GFP_KERNEL);
693 printk("STALLION: failed to allocate memory (size=%Zd)\n",
694 sizeof(struct stlbrd));
698 brdp->magic = STL_BOARDMAGIC;
702 /*****************************************************************************/
704 static int stl_open(struct tty_struct *tty, struct file *filp)
706 struct stlport *portp;
708 unsigned int minordev, brdnr, panelnr;
711 pr_debug("stl_open(tty=%p,filp=%p): device=%s\n", tty, filp, tty->name);
713 minordev = tty->index;
714 brdnr = MINOR2BRD(minordev);
715 if (brdnr >= stl_nrbrds)
717 brdp = stl_brds[brdnr];
720 minordev = MINOR2PORT(minordev);
721 for (portnr = -1, panelnr = 0; panelnr < STL_MAXPANELS; panelnr++) {
722 if (brdp->panels[panelnr] == NULL)
724 if (minordev < brdp->panels[panelnr]->nrports) {
728 minordev -= brdp->panels[panelnr]->nrports;
733 portp = brdp->panels[panelnr]->ports[portnr];
738 * On the first open of the device setup the port hardware, and
739 * initialize the per port data structure.
741 tty_port_tty_set(&portp->port, tty);
742 tty->driver_data = portp;
745 if ((portp->port.flags & ASYNC_INITIALIZED) == 0) {
746 if (!portp->tx.buf) {
747 portp->tx.buf = kmalloc(STL_TXBUFSIZE, GFP_KERNEL);
750 portp->tx.head = portp->tx.buf;
751 portp->tx.tail = portp->tx.buf;
753 stl_setport(portp, tty->termios);
754 portp->sigs = stl_getsignals(portp);
755 stl_setsignals(portp, 1, 1);
756 stl_enablerxtx(portp, 1, 1);
757 stl_startrxtx(portp, 1, 0);
758 clear_bit(TTY_IO_ERROR, &tty->flags);
759 portp->port.flags |= ASYNC_INITIALIZED;
763 * Check if this port is in the middle of closing. If so then wait
764 * until it is closed then return error status, based on flag settings.
765 * The sleep here does not need interrupt protection since the wakeup
766 * for it is done with the same context.
768 if (portp->port.flags & ASYNC_CLOSING) {
769 interruptible_sleep_on(&portp->port.close_wait);
770 if (portp->port.flags & ASYNC_HUP_NOTIFY)
776 * Based on type of open being done check if it can overlap with any
777 * previous opens still in effect. If we are a normal serial device
778 * then also we might have to wait for carrier.
780 if (!(filp->f_flags & O_NONBLOCK))
781 if ((rc = stl_waitcarrier(tty, portp, filp)) != 0)
784 portp->port.flags |= ASYNC_NORMAL_ACTIVE;
789 /*****************************************************************************/
791 static int stl_carrier_raised(struct tty_port *port)
793 struct stlport *portp = container_of(port, struct stlport, port);
794 return (portp->sigs & TIOCM_CD) ? 1 : 0;
798 * Possibly need to wait for carrier (DCD signal) to come high. Say
799 * maybe because if we are clocal then we don't need to wait...
802 static int stl_waitcarrier(struct tty_struct *tty, struct stlport *portp,
807 struct tty_port *port = &portp->port;
809 pr_debug("stl_waitcarrier(portp=%p,filp=%p)\n", portp, filp);
814 spin_lock_irqsave(&stallion_lock, flags);
816 if (tty->termios->c_cflag & CLOCAL)
819 portp->openwaitcnt++;
820 if (! tty_hung_up_p(filp))
824 /* Takes brd_lock internally */
825 stl_setsignals(portp, 1, 1);
826 if (tty_hung_up_p(filp) ||
827 ((port->flags & ASYNC_INITIALIZED) == 0)) {
828 if (port->flags & ASYNC_HUP_NOTIFY)
834 if (((port->flags & ASYNC_CLOSING) == 0) &&
835 (doclocal || tty_port_carrier_raised(port)))
837 if (signal_pending(current)) {
842 interruptible_sleep_on(&port->open_wait);
845 if (! tty_hung_up_p(filp))
847 portp->openwaitcnt--;
848 spin_unlock_irqrestore(&stallion_lock, flags);
853 /*****************************************************************************/
855 static void stl_flushbuffer(struct tty_struct *tty)
857 struct stlport *portp;
859 pr_debug("stl_flushbuffer(tty=%p)\n", tty);
861 portp = tty->driver_data;
869 /*****************************************************************************/
871 static void stl_waituntilsent(struct tty_struct *tty, int timeout)
873 struct stlport *portp;
876 pr_debug("stl_waituntilsent(tty=%p,timeout=%d)\n", tty, timeout);
878 portp = tty->driver_data;
884 tend = jiffies + timeout;
887 while (stl_datastate(portp)) {
888 if (signal_pending(current))
890 msleep_interruptible(20);
891 if (time_after_eq(jiffies, tend))
897 /*****************************************************************************/
899 static void stl_close(struct tty_struct *tty, struct file *filp)
901 struct stlport *portp;
904 pr_debug("stl_close(tty=%p,filp=%p)\n", tty, filp);
906 portp = tty->driver_data;
910 spin_lock_irqsave(&stallion_lock, flags);
911 if (tty_hung_up_p(filp)) {
912 spin_unlock_irqrestore(&stallion_lock, flags);
915 if ((tty->count == 1) && (portp->port.count != 1))
916 portp->port.count = 1;
917 if (portp->port.count-- > 1) {
918 spin_unlock_irqrestore(&stallion_lock, flags);
922 portp->port.count = 0;
923 portp->port.flags |= ASYNC_CLOSING;
926 * May want to wait for any data to drain before closing. The BUSY
927 * flag keeps track of whether we are still sending or not - it is
928 * very accurate for the cd1400, not quite so for the sc26198.
929 * (The sc26198 has no "end-of-data" interrupt only empty FIFO)
933 spin_unlock_irqrestore(&stallion_lock, flags);
935 if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
936 tty_wait_until_sent(tty, portp->closing_wait);
937 stl_waituntilsent(tty, (HZ / 2));
940 spin_lock_irqsave(&stallion_lock, flags);
941 portp->port.flags &= ~ASYNC_INITIALIZED;
942 spin_unlock_irqrestore(&stallion_lock, flags);
944 stl_disableintrs(portp);
945 if (tty->termios->c_cflag & HUPCL)
946 stl_setsignals(portp, 0, 0);
947 stl_enablerxtx(portp, 0, 0);
948 stl_flushbuffer(tty);
950 if (portp->tx.buf != NULL) {
951 kfree(portp->tx.buf);
952 portp->tx.buf = NULL;
953 portp->tx.head = NULL;
954 portp->tx.tail = NULL;
956 set_bit(TTY_IO_ERROR, &tty->flags);
957 tty_ldisc_flush(tty);
960 tty_port_tty_set(&portp->port, NULL);
962 if (portp->openwaitcnt) {
963 if (portp->close_delay)
964 msleep_interruptible(jiffies_to_msecs(portp->close_delay));
965 wake_up_interruptible(&portp->port.open_wait);
968 portp->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
969 wake_up_interruptible(&portp->port.close_wait);
972 /*****************************************************************************/
975 * Write routine. Take data and stuff it in to the TX ring queue.
976 * If transmit interrupts are not running then start them.
979 static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count)
981 struct stlport *portp;
982 unsigned int len, stlen;
983 unsigned char *chbuf;
986 pr_debug("stl_write(tty=%p,buf=%p,count=%d)\n", tty, buf, count);
988 portp = tty->driver_data;
991 if (portp->tx.buf == NULL)
995 * If copying direct from user space we must cater for page faults,
996 * causing us to "sleep" here for a while. To handle this copy in all
997 * the data we need now, into a local buffer. Then when we got it all
998 * copy it into the TX buffer.
1000 chbuf = (unsigned char *) buf;
1002 head = portp->tx.head;
1003 tail = portp->tx.tail;
1005 len = STL_TXBUFSIZE - (head - tail) - 1;
1006 stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
1008 len = tail - head - 1;
1012 len = min(len, (unsigned int)count);
1015 stlen = min(len, stlen);
1016 memcpy(head, chbuf, stlen);
1021 if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
1022 head = portp->tx.buf;
1023 stlen = tail - head;
1026 portp->tx.head = head;
1028 clear_bit(ASYI_TXLOW, &portp->istate);
1029 stl_startrxtx(portp, -1, 1);
1034 /*****************************************************************************/
1036 static int stl_putchar(struct tty_struct *tty, unsigned char ch)
1038 struct stlport *portp;
1042 pr_debug("stl_putchar(tty=%p,ch=%x)\n", tty, ch);
1044 portp = tty->driver_data;
1047 if (portp->tx.buf == NULL)
1050 head = portp->tx.head;
1051 tail = portp->tx.tail;
1053 len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
1058 if (head >= (portp->tx.buf + STL_TXBUFSIZE))
1059 head = portp->tx.buf;
1061 portp->tx.head = head;
1065 /*****************************************************************************/
1068 * If there are any characters in the buffer then make sure that TX
1069 * interrupts are on and get'em out. Normally used after the putchar
1070 * routine has been called.
1073 static void stl_flushchars(struct tty_struct *tty)
1075 struct stlport *portp;
1077 pr_debug("stl_flushchars(tty=%p)\n", tty);
1079 portp = tty->driver_data;
1082 if (portp->tx.buf == NULL)
1085 stl_startrxtx(portp, -1, 1);
1088 /*****************************************************************************/
1090 static int stl_writeroom(struct tty_struct *tty)
1092 struct stlport *portp;
1095 pr_debug("stl_writeroom(tty=%p)\n", tty);
1097 portp = tty->driver_data;
1100 if (portp->tx.buf == NULL)
1103 head = portp->tx.head;
1104 tail = portp->tx.tail;
1105 return (head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1);
1108 /*****************************************************************************/
1111 * Return number of chars in the TX buffer. Normally we would just
1112 * calculate the number of chars in the buffer and return that, but if
1113 * the buffer is empty and TX interrupts are still on then we return
1114 * that the buffer still has 1 char in it. This way whoever called us
1115 * will not think that ALL chars have drained - since the UART still
1116 * must have some chars in it (we are busy after all).
1119 static int stl_charsinbuffer(struct tty_struct *tty)
1121 struct stlport *portp;
1125 pr_debug("stl_charsinbuffer(tty=%p)\n", tty);
1127 portp = tty->driver_data;
1130 if (portp->tx.buf == NULL)
1133 head = portp->tx.head;
1134 tail = portp->tx.tail;
1135 size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1136 if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
1141 /*****************************************************************************/
1144 * Generate the serial struct info.
1147 static int stl_getserial(struct stlport *portp, struct serial_struct __user *sp)
1149 struct serial_struct sio;
1150 struct stlbrd *brdp;
1152 pr_debug("stl_getserial(portp=%p,sp=%p)\n", portp, sp);
1154 memset(&sio, 0, sizeof(struct serial_struct));
1155 sio.line = portp->portnr;
1156 sio.port = portp->ioaddr;
1157 sio.flags = portp->port.flags;
1158 sio.baud_base = portp->baud_base;
1159 sio.close_delay = portp->close_delay;
1160 sio.closing_wait = portp->closing_wait;
1161 sio.custom_divisor = portp->custom_divisor;
1163 if (portp->uartp == &stl_cd1400uart) {
1164 sio.type = PORT_CIRRUS;
1165 sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
1167 sio.type = PORT_UNKNOWN;
1168 sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
1171 brdp = stl_brds[portp->brdnr];
1173 sio.irq = brdp->irq;
1175 return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0;
1178 /*****************************************************************************/
1181 * Set port according to the serial struct info.
1182 * At this point we do not do any auto-configure stuff, so we will
1183 * just quietly ignore any requests to change irq, etc.
1186 static int stl_setserial(struct tty_struct *tty, struct serial_struct __user *sp)
1188 struct stlport * portp = tty->driver_data;
1189 struct serial_struct sio;
1191 pr_debug("stl_setserial(portp=%p,sp=%p)\n", portp, sp);
1193 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1195 if (!capable(CAP_SYS_ADMIN)) {
1196 if ((sio.baud_base != portp->baud_base) ||
1197 (sio.close_delay != portp->close_delay) ||
1198 ((sio.flags & ~ASYNC_USR_MASK) !=
1199 (portp->port.flags & ~ASYNC_USR_MASK)))
1203 portp->port.flags = (portp->port.flags & ~ASYNC_USR_MASK) |
1204 (sio.flags & ASYNC_USR_MASK);
1205 portp->baud_base = sio.baud_base;
1206 portp->close_delay = sio.close_delay;
1207 portp->closing_wait = sio.closing_wait;
1208 portp->custom_divisor = sio.custom_divisor;
1209 stl_setport(portp, tty->termios);
1213 /*****************************************************************************/
1215 static int stl_tiocmget(struct tty_struct *tty, struct file *file)
1217 struct stlport *portp;
1219 portp = tty->driver_data;
1222 if (tty->flags & (1 << TTY_IO_ERROR))
1225 return stl_getsignals(portp);
1228 static int stl_tiocmset(struct tty_struct *tty, struct file *file,
1229 unsigned int set, unsigned int clear)
1231 struct stlport *portp;
1232 int rts = -1, dtr = -1;
1234 portp = tty->driver_data;
1237 if (tty->flags & (1 << TTY_IO_ERROR))
1240 if (set & TIOCM_RTS)
1242 if (set & TIOCM_DTR)
1244 if (clear & TIOCM_RTS)
1246 if (clear & TIOCM_DTR)
1249 stl_setsignals(portp, dtr, rts);
1253 static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1255 struct stlport *portp;
1257 void __user *argp = (void __user *)arg;
1259 pr_debug("stl_ioctl(tty=%p,file=%p,cmd=%x,arg=%lx)\n", tty, file, cmd,
1262 portp = tty->driver_data;
1266 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1267 (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS))
1268 if (tty->flags & (1 << TTY_IO_ERROR))
1277 rc = stl_getserial(portp, argp);
1280 rc = stl_setserial(tty, argp);
1282 case COM_GETPORTSTATS:
1283 rc = stl_getportstats(tty, portp, argp);
1285 case COM_CLRPORTSTATS:
1286 rc = stl_clrportstats(portp, argp);
1292 case TIOCSERGSTRUCT:
1293 case TIOCSERGETMULTI:
1294 case TIOCSERSETMULTI:
1303 /*****************************************************************************/
1306 * Start the transmitter again. Just turn TX interrupts back on.
1309 static void stl_start(struct tty_struct *tty)
1311 struct stlport *portp;
1313 pr_debug("stl_start(tty=%p)\n", tty);
1315 portp = tty->driver_data;
1318 stl_startrxtx(portp, -1, 1);
1321 /*****************************************************************************/
1323 static void stl_settermios(struct tty_struct *tty, struct ktermios *old)
1325 struct stlport *portp;
1326 struct ktermios *tiosp;
1328 pr_debug("stl_settermios(tty=%p,old=%p)\n", tty, old);
1330 portp = tty->driver_data;
1334 tiosp = tty->termios;
1335 if ((tiosp->c_cflag == old->c_cflag) &&
1336 (tiosp->c_iflag == old->c_iflag))
1339 stl_setport(portp, tiosp);
1340 stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
1342 if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
1343 tty->hw_stopped = 0;
1346 if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1347 wake_up_interruptible(&portp->port.open_wait);
1350 /*****************************************************************************/
1353 * Attempt to flow control who ever is sending us data. Based on termios
1354 * settings use software or/and hardware flow control.
1357 static void stl_throttle(struct tty_struct *tty)
1359 struct stlport *portp;
1361 pr_debug("stl_throttle(tty=%p)\n", tty);
1363 portp = tty->driver_data;
1366 stl_flowctrl(portp, 0);
1369 /*****************************************************************************/
1372 * Unflow control the device sending us data...
1375 static void stl_unthrottle(struct tty_struct *tty)
1377 struct stlport *portp;
1379 pr_debug("stl_unthrottle(tty=%p)\n", tty);
1381 portp = tty->driver_data;
1384 stl_flowctrl(portp, 1);
1387 /*****************************************************************************/
1390 * Stop the transmitter. Basically to do this we will just turn TX
1394 static void stl_stop(struct tty_struct *tty)
1396 struct stlport *portp;
1398 pr_debug("stl_stop(tty=%p)\n", tty);
1400 portp = tty->driver_data;
1403 stl_startrxtx(portp, -1, 0);
1406 /*****************************************************************************/
1409 * Hangup this port. This is pretty much like closing the port, only
1410 * a little more brutal. No waiting for data to drain. Shutdown the
1411 * port and maybe drop signals.
1414 static void stl_hangup(struct tty_struct *tty)
1416 struct stlport *portp;
1418 pr_debug("stl_hangup(tty=%p)\n", tty);
1420 portp = tty->driver_data;
1424 portp->port.flags &= ~ASYNC_INITIALIZED;
1425 stl_disableintrs(portp);
1426 if (tty->termios->c_cflag & HUPCL)
1427 stl_setsignals(portp, 0, 0);
1428 stl_enablerxtx(portp, 0, 0);
1429 stl_flushbuffer(tty);
1431 set_bit(TTY_IO_ERROR, &tty->flags);
1432 if (portp->tx.buf != NULL) {
1433 kfree(portp->tx.buf);
1434 portp->tx.buf = NULL;
1435 portp->tx.head = NULL;
1436 portp->tx.tail = NULL;
1438 tty_port_tty_set(&portp->port, NULL);
1439 portp->port.flags &= ~ASYNC_NORMAL_ACTIVE;
1440 portp->port.count = 0;
1441 wake_up_interruptible(&portp->port.open_wait);
1444 /*****************************************************************************/
1446 static int stl_breakctl(struct tty_struct *tty, int state)
1448 struct stlport *portp;
1450 pr_debug("stl_breakctl(tty=%p,state=%d)\n", tty, state);
1452 portp = tty->driver_data;
1456 stl_sendbreak(portp, ((state == -1) ? 1 : 2));
1460 /*****************************************************************************/
1462 static void stl_sendxchar(struct tty_struct *tty, char ch)
1464 struct stlport *portp;
1466 pr_debug("stl_sendxchar(tty=%p,ch=%x)\n", tty, ch);
1468 portp = tty->driver_data;
1472 if (ch == STOP_CHAR(tty))
1473 stl_sendflow(portp, 0);
1474 else if (ch == START_CHAR(tty))
1475 stl_sendflow(portp, 1);
1477 stl_putchar(tty, ch);
1480 /*****************************************************************************/
1485 * Format info for a specified port. The line is deliberately limited
1486 * to 80 characters. (If it is too long it will be truncated, if too
1487 * short then padded with spaces).
1490 static int stl_portinfo(struct stlport *portp, int portnr, char *pos)
1496 sp += sprintf(sp, "%d: uart:%s tx:%d rx:%d",
1497 portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
1498 (int) portp->stats.txtotal, (int) portp->stats.rxtotal);
1500 if (portp->stats.rxframing)
1501 sp += sprintf(sp, " fe:%d", (int) portp->stats.rxframing);
1502 if (portp->stats.rxparity)
1503 sp += sprintf(sp, " pe:%d", (int) portp->stats.rxparity);
1504 if (portp->stats.rxbreaks)
1505 sp += sprintf(sp, " brk:%d", (int) portp->stats.rxbreaks);
1506 if (portp->stats.rxoverrun)
1507 sp += sprintf(sp, " oe:%d", (int) portp->stats.rxoverrun);
1509 sigs = stl_getsignals(portp);
1510 cnt = sprintf(sp, "%s%s%s%s%s ",
1511 (sigs & TIOCM_RTS) ? "|RTS" : "",
1512 (sigs & TIOCM_CTS) ? "|CTS" : "",
1513 (sigs & TIOCM_DTR) ? "|DTR" : "",
1514 (sigs & TIOCM_CD) ? "|DCD" : "",
1515 (sigs & TIOCM_DSR) ? "|DSR" : "");
1519 for (cnt = sp - pos; cnt < (MAXLINE - 1); cnt++)
1522 pos[(MAXLINE - 2)] = '+';
1523 pos[(MAXLINE - 1)] = '\n';
1528 /*****************************************************************************/
1531 * Port info, read from the /proc file system.
1534 static int stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
1536 struct stlbrd *brdp;
1537 struct stlpanel *panelp;
1538 struct stlport *portp;
1539 unsigned int brdnr, panelnr, portnr;
1540 int totalport, curoff, maxoff;
1543 pr_debug("stl_readproc(page=%p,start=%p,off=%lx,count=%d,eof=%p,"
1544 "data=%p\n", page, start, off, count, eof, data);
1551 pos += sprintf(pos, "%s: version %s", stl_drvtitle,
1553 while (pos < (page + MAXLINE - 1))
1560 * We scan through for each board, panel and port. The offset is
1561 * calculated on the fly, and irrelevant ports are skipped.
1563 for (brdnr = 0; brdnr < stl_nrbrds; brdnr++) {
1564 brdp = stl_brds[brdnr];
1567 if (brdp->state == 0)
1570 maxoff = curoff + (brdp->nrports * MAXLINE);
1571 if (off >= maxoff) {
1576 totalport = brdnr * STL_MAXPORTS;
1577 for (panelnr = 0; panelnr < brdp->nrpanels; panelnr++) {
1578 panelp = brdp->panels[panelnr];
1582 maxoff = curoff + (panelp->nrports * MAXLINE);
1583 if (off >= maxoff) {
1585 totalport += panelp->nrports;
1589 for (portnr = 0; portnr < panelp->nrports; portnr++,
1591 portp = panelp->ports[portnr];
1594 if (off >= (curoff += MAXLINE))
1596 if ((pos - page + MAXLINE) > count)
1598 pos += stl_portinfo(portp, totalport, pos);
1610 /*****************************************************************************/
1613 * All board interrupts are vectored through here first. This code then
1614 * calls off to the approrpriate board interrupt handlers.
1617 static irqreturn_t stl_intr(int irq, void *dev_id)
1619 struct stlbrd *brdp = dev_id;
1621 pr_debug("stl_intr(brdp=%p,irq=%d)\n", brdp, brdp->irq);
1623 return IRQ_RETVAL((* brdp->isr)(brdp));
1626 /*****************************************************************************/
1629 * Interrupt service routine for EasyIO board types.
1632 static int stl_eiointr(struct stlbrd *brdp)
1634 struct stlpanel *panelp;
1635 unsigned int iobase;
1638 spin_lock(&brd_lock);
1639 panelp = brdp->panels[0];
1640 iobase = panelp->iobase;
1641 while (inb(brdp->iostatus) & EIO_INTRPEND) {
1643 (* panelp->isr)(panelp, iobase);
1645 spin_unlock(&brd_lock);
1649 /*****************************************************************************/
1652 * Interrupt service routine for ECH-AT board types.
1655 static int stl_echatintr(struct stlbrd *brdp)
1657 struct stlpanel *panelp;
1658 unsigned int ioaddr, bnknr;
1661 outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
1663 while (inb(brdp->iostatus) & ECH_INTRPEND) {
1665 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1666 ioaddr = brdp->bnkstataddr[bnknr];
1667 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1668 panelp = brdp->bnk2panel[bnknr];
1669 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1674 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
1679 /*****************************************************************************/
1682 * Interrupt service routine for ECH-MCA board types.
1685 static int stl_echmcaintr(struct stlbrd *brdp)
1687 struct stlpanel *panelp;
1688 unsigned int ioaddr, bnknr;
1691 while (inb(brdp->iostatus) & ECH_INTRPEND) {
1693 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1694 ioaddr = brdp->bnkstataddr[bnknr];
1695 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1696 panelp = brdp->bnk2panel[bnknr];
1697 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1704 /*****************************************************************************/
1707 * Interrupt service routine for ECH-PCI board types.
1710 static int stl_echpciintr(struct stlbrd *brdp)
1712 struct stlpanel *panelp;
1713 unsigned int ioaddr, bnknr, recheck;
1718 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1719 outb(brdp->bnkpageaddr[bnknr], brdp->ioctrl);
1720 ioaddr = brdp->bnkstataddr[bnknr];
1721 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1722 panelp = brdp->bnk2panel[bnknr];
1723 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1734 /*****************************************************************************/
1737 * Interrupt service routine for ECH-8/64-PCI board types.
1740 static int stl_echpci64intr(struct stlbrd *brdp)
1742 struct stlpanel *panelp;
1743 unsigned int ioaddr, bnknr;
1746 while (inb(brdp->ioctrl) & 0x1) {
1748 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1749 ioaddr = brdp->bnkstataddr[bnknr];
1750 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1751 panelp = brdp->bnk2panel[bnknr];
1752 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1760 /*****************************************************************************/
1763 * Initialize all the ports on a panel.
1766 static int __devinit stl_initports(struct stlbrd *brdp, struct stlpanel *panelp)
1768 struct stlport *portp;
1772 pr_debug("stl_initports(brdp=%p,panelp=%p)\n", brdp, panelp);
1774 chipmask = stl_panelinit(brdp, panelp);
1777 * All UART's are initialized (if found!). Now go through and setup
1778 * each ports data structures.
1780 for (i = 0; i < panelp->nrports; i++) {
1781 portp = kzalloc(sizeof(struct stlport), GFP_KERNEL);
1783 printk("STALLION: failed to allocate memory "
1784 "(size=%Zd)\n", sizeof(struct stlport));
1787 tty_port_init(&portp->port);
1788 portp->port.ops = &stl_port_ops;
1789 portp->magic = STL_PORTMAGIC;
1791 portp->brdnr = panelp->brdnr;
1792 portp->panelnr = panelp->panelnr;
1793 portp->uartp = panelp->uartp;
1794 portp->clk = brdp->clk;
1795 portp->baud_base = STL_BAUDBASE;
1796 portp->close_delay = STL_CLOSEDELAY;
1797 portp->closing_wait = 30 * HZ;
1798 init_waitqueue_head(&portp->port.open_wait);
1799 init_waitqueue_head(&portp->port.close_wait);
1800 portp->stats.brd = portp->brdnr;
1801 portp->stats.panel = portp->panelnr;
1802 portp->stats.port = portp->portnr;
1803 panelp->ports[i] = portp;
1804 stl_portinit(brdp, panelp, portp);
1810 static void stl_cleanup_panels(struct stlbrd *brdp)
1812 struct stlpanel *panelp;
1813 struct stlport *portp;
1815 struct tty_struct *tty;
1817 for (j = 0; j < STL_MAXPANELS; j++) {
1818 panelp = brdp->panels[j];
1821 for (k = 0; k < STL_PORTSPERPANEL; k++) {
1822 portp = panelp->ports[k];
1825 tty = tty_port_tty_get(&portp->port);
1830 kfree(portp->tx.buf);
1837 /*****************************************************************************/
1840 * Try to find and initialize an EasyIO board.
1843 static int __devinit stl_initeio(struct stlbrd *brdp)
1845 struct stlpanel *panelp;
1846 unsigned int status;
1850 pr_debug("stl_initeio(brdp=%p)\n", brdp);
1852 brdp->ioctrl = brdp->ioaddr1 + 1;
1853 brdp->iostatus = brdp->ioaddr1 + 2;
1855 status = inb(brdp->iostatus);
1856 if ((status & EIO_IDBITMASK) == EIO_MK3)
1860 * Handle board specific stuff now. The real difference is PCI
1863 if (brdp->brdtype == BRD_EASYIOPCI) {
1864 brdp->iosize1 = 0x80;
1865 brdp->iosize2 = 0x80;
1866 name = "serial(EIO-PCI)";
1867 outb(0x41, (brdp->ioaddr2 + 0x4c));
1870 name = "serial(EIO)";
1871 if ((brdp->irq < 0) || (brdp->irq > 15) ||
1872 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
1873 printk("STALLION: invalid irq=%d for brd=%d\n",
1874 brdp->irq, brdp->brdnr);
1878 outb((stl_vecmap[brdp->irq] | EIO_0WS |
1879 ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)),
1884 if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
1885 printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
1886 "%x conflicts with another device\n", brdp->brdnr,
1891 if (brdp->iosize2 > 0)
1892 if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
1893 printk(KERN_WARNING "STALLION: Warning, board %d I/O "
1894 "address %x conflicts with another device\n",
1895 brdp->brdnr, brdp->ioaddr2);
1896 printk(KERN_WARNING "STALLION: Warning, also "
1897 "releasing board %d I/O address %x \n",
1898 brdp->brdnr, brdp->ioaddr1);
1903 * Everything looks OK, so let's go ahead and probe for the hardware.
1905 brdp->clk = CD1400_CLK;
1906 brdp->isr = stl_eiointr;
1909 switch (status & EIO_IDBITMASK) {
1911 brdp->clk = CD1400_CLK8M;
1921 switch (status & EIO_BRDMASK) {
1940 * We have verified that the board is actually present, so now we
1941 * can complete the setup.
1944 panelp = kzalloc(sizeof(struct stlpanel), GFP_KERNEL);
1946 printk(KERN_WARNING "STALLION: failed to allocate memory "
1947 "(size=%Zd)\n", sizeof(struct stlpanel));
1952 panelp->magic = STL_PANELMAGIC;
1953 panelp->brdnr = brdp->brdnr;
1954 panelp->panelnr = 0;
1955 panelp->nrports = brdp->nrports;
1956 panelp->iobase = brdp->ioaddr1;
1957 panelp->hwid = status;
1958 if ((status & EIO_IDBITMASK) == EIO_MK3) {
1959 panelp->uartp = &stl_sc26198uart;
1960 panelp->isr = stl_sc26198intr;
1962 panelp->uartp = &stl_cd1400uart;
1963 panelp->isr = stl_cd1400eiointr;
1966 brdp->panels[0] = panelp;
1968 brdp->state |= BRD_FOUND;
1969 brdp->hwid = status;
1970 if (request_irq(brdp->irq, stl_intr, IRQF_SHARED, name, brdp) != 0) {
1971 printk("STALLION: failed to register interrupt "
1972 "routine for %s irq=%d\n", name, brdp->irq);
1979 stl_cleanup_panels(brdp);
1981 if (brdp->iosize2 > 0)
1982 release_region(brdp->ioaddr2, brdp->iosize2);
1984 release_region(brdp->ioaddr1, brdp->iosize1);
1989 /*****************************************************************************/
1992 * Try to find an ECH board and initialize it. This code is capable of
1993 * dealing with all types of ECH board.
1996 static int __devinit stl_initech(struct stlbrd *brdp)
1998 struct stlpanel *panelp;
1999 unsigned int status, nxtid, ioaddr, conflict, panelnr, banknr, i;
2003 pr_debug("stl_initech(brdp=%p)\n", brdp);
2009 * Set up the initial board register contents for boards. This varies a
2010 * bit between the different board types. So we need to handle each
2011 * separately. Also do a check that the supplied IRQ is good.
2013 switch (brdp->brdtype) {
2016 brdp->isr = stl_echatintr;
2017 brdp->ioctrl = brdp->ioaddr1 + 1;
2018 brdp->iostatus = brdp->ioaddr1 + 1;
2019 status = inb(brdp->iostatus);
2020 if ((status & ECH_IDBITMASK) != ECH_ID) {
2024 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2025 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2026 printk("STALLION: invalid irq=%d for brd=%d\n",
2027 brdp->irq, brdp->brdnr);
2031 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2032 status |= (stl_vecmap[brdp->irq] << 1);
2033 outb((status | ECH_BRDRESET), brdp->ioaddr1);
2034 brdp->ioctrlval = ECH_INTENABLE |
2035 ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2036 for (i = 0; i < 10; i++)
2037 outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
2040 name = "serial(EC8/32)";
2041 outb(status, brdp->ioaddr1);
2045 brdp->isr = stl_echmcaintr;
2046 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2047 brdp->iostatus = brdp->ioctrl;
2048 status = inb(brdp->iostatus);
2049 if ((status & ECH_IDBITMASK) != ECH_ID) {
2053 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2054 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2055 printk("STALLION: invalid irq=%d for brd=%d\n",
2056 brdp->irq, brdp->brdnr);
2060 outb(ECHMC_BRDRESET, brdp->ioctrl);
2061 outb(ECHMC_INTENABLE, brdp->ioctrl);
2063 name = "serial(EC8/32-MC)";
2067 brdp->isr = stl_echpciintr;
2068 brdp->ioctrl = brdp->ioaddr1 + 2;
2071 name = "serial(EC8/32-PCI)";
2075 brdp->isr = stl_echpci64intr;
2076 brdp->ioctrl = brdp->ioaddr2 + 0x40;
2077 outb(0x43, (brdp->ioaddr1 + 0x4c));
2078 brdp->iosize1 = 0x80;
2079 brdp->iosize2 = 0x80;
2080 name = "serial(EC8/64-PCI)";
2084 printk("STALLION: unknown board type=%d\n", brdp->brdtype);
2090 * Check boards for possible IO address conflicts and return fail status
2091 * if an IO conflict found.
2094 if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
2095 printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
2096 "%x conflicts with another device\n", brdp->brdnr,
2101 if (brdp->iosize2 > 0)
2102 if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
2103 printk(KERN_WARNING "STALLION: Warning, board %d I/O "
2104 "address %x conflicts with another device\n",
2105 brdp->brdnr, brdp->ioaddr2);
2106 printk(KERN_WARNING "STALLION: Warning, also "
2107 "releasing board %d I/O address %x \n",
2108 brdp->brdnr, brdp->ioaddr1);
2113 * Scan through the secondary io address space looking for panels.
2114 * As we find'em allocate and initialize panel structures for each.
2116 brdp->clk = CD1400_CLK;
2117 brdp->hwid = status;
2119 ioaddr = brdp->ioaddr2;
2124 for (i = 0; i < STL_MAXPANELS; i++) {
2125 if (brdp->brdtype == BRD_ECHPCI) {
2126 outb(nxtid, brdp->ioctrl);
2127 ioaddr = brdp->ioaddr2;
2129 status = inb(ioaddr + ECH_PNLSTATUS);
2130 if ((status & ECH_PNLIDMASK) != nxtid)
2132 panelp = kzalloc(sizeof(struct stlpanel), GFP_KERNEL);
2134 printk("STALLION: failed to allocate memory "
2135 "(size=%Zd)\n", sizeof(struct stlpanel));
2139 panelp->magic = STL_PANELMAGIC;
2140 panelp->brdnr = brdp->brdnr;
2141 panelp->panelnr = panelnr;
2142 panelp->iobase = ioaddr;
2143 panelp->pagenr = nxtid;
2144 panelp->hwid = status;
2145 brdp->bnk2panel[banknr] = panelp;
2146 brdp->bnkpageaddr[banknr] = nxtid;
2147 brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
2149 if (status & ECH_PNLXPID) {
2150 panelp->uartp = &stl_sc26198uart;
2151 panelp->isr = stl_sc26198intr;
2152 if (status & ECH_PNL16PORT) {
2153 panelp->nrports = 16;
2154 brdp->bnk2panel[banknr] = panelp;
2155 brdp->bnkpageaddr[banknr] = nxtid;
2156 brdp->bnkstataddr[banknr++] = ioaddr + 4 +
2159 panelp->nrports = 8;
2161 panelp->uartp = &stl_cd1400uart;
2162 panelp->isr = stl_cd1400echintr;
2163 if (status & ECH_PNL16PORT) {
2164 panelp->nrports = 16;
2165 panelp->ackmask = 0x80;
2166 if (brdp->brdtype != BRD_ECHPCI)
2167 ioaddr += EREG_BANKSIZE;
2168 brdp->bnk2panel[banknr] = panelp;
2169 brdp->bnkpageaddr[banknr] = ++nxtid;
2170 brdp->bnkstataddr[banknr++] = ioaddr +
2173 panelp->nrports = 8;
2174 panelp->ackmask = 0xc0;
2179 ioaddr += EREG_BANKSIZE;
2180 brdp->nrports += panelp->nrports;
2181 brdp->panels[panelnr++] = panelp;
2182 if ((brdp->brdtype != BRD_ECHPCI) &&
2183 (ioaddr >= (brdp->ioaddr2 + brdp->iosize2))) {
2189 brdp->nrpanels = panelnr;
2190 brdp->nrbnks = banknr;
2191 if (brdp->brdtype == BRD_ECH)
2192 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2194 brdp->state |= BRD_FOUND;
2195 if (request_irq(brdp->irq, stl_intr, IRQF_SHARED, name, brdp) != 0) {
2196 printk("STALLION: failed to register interrupt "
2197 "routine for %s irq=%d\n", name, brdp->irq);
2204 stl_cleanup_panels(brdp);
2205 if (brdp->iosize2 > 0)
2206 release_region(brdp->ioaddr2, brdp->iosize2);
2208 release_region(brdp->ioaddr1, brdp->iosize1);
2213 /*****************************************************************************/
2216 * Initialize and configure the specified board.
2217 * Scan through all the boards in the configuration and see what we
2218 * can find. Handle EIO and the ECH boards a little differently here
2219 * since the initial search and setup is very different.
2222 static int __devinit stl_brdinit(struct stlbrd *brdp)
2226 pr_debug("stl_brdinit(brdp=%p)\n", brdp);
2228 switch (brdp->brdtype) {
2231 retval = stl_initeio(brdp);
2239 retval = stl_initech(brdp);
2244 printk("STALLION: board=%d is unknown board type=%d\n",
2245 brdp->brdnr, brdp->brdtype);
2250 if ((brdp->state & BRD_FOUND) == 0) {
2251 printk("STALLION: %s board not found, board=%d io=%x irq=%d\n",
2252 stl_brdnames[brdp->brdtype], brdp->brdnr,
2253 brdp->ioaddr1, brdp->irq);
2257 for (i = 0; i < STL_MAXPANELS; i++)
2258 if (brdp->panels[i] != NULL)
2259 stl_initports(brdp, brdp->panels[i]);
2261 printk("STALLION: %s found, board=%d io=%x irq=%d "
2262 "nrpanels=%d nrports=%d\n", stl_brdnames[brdp->brdtype],
2263 brdp->brdnr, brdp->ioaddr1, brdp->irq, brdp->nrpanels,
2268 free_irq(brdp->irq, brdp);
2270 stl_cleanup_panels(brdp);
2272 release_region(brdp->ioaddr1, brdp->iosize1);
2273 if (brdp->iosize2 > 0)
2274 release_region(brdp->ioaddr2, brdp->iosize2);
2279 /*****************************************************************************/
2282 * Find the next available board number that is free.
2285 static int __devinit stl_getbrdnr(void)
2289 for (i = 0; i < STL_MAXBRDS; i++)
2290 if (stl_brds[i] == NULL) {
2291 if (i >= stl_nrbrds)
2299 /*****************************************************************************/
2301 * We have a Stallion board. Allocate a board structure and
2302 * initialize it. Read its IO and IRQ resources from PCI
2303 * configuration space.
2306 static int __devinit stl_pciprobe(struct pci_dev *pdev,
2307 const struct pci_device_id *ent)
2309 struct stlbrd *brdp;
2310 unsigned int i, brdtype = ent->driver_data;
2311 int brdnr, retval = -ENODEV;
2313 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE)
2316 retval = pci_enable_device(pdev);
2319 brdp = stl_allocbrd();
2324 mutex_lock(&stl_brdslock);
2325 brdnr = stl_getbrdnr();
2327 dev_err(&pdev->dev, "too many boards found, "
2328 "maximum supported %d\n", STL_MAXBRDS);
2329 mutex_unlock(&stl_brdslock);
2333 brdp->brdnr = (unsigned int)brdnr;
2334 stl_brds[brdp->brdnr] = brdp;
2335 mutex_unlock(&stl_brdslock);
2337 brdp->brdtype = brdtype;
2338 brdp->state |= STL_PROBED;
2341 * We have all resources from the board, so let's setup the actual
2342 * board structure now.
2346 brdp->ioaddr2 = pci_resource_start(pdev, 0);
2347 brdp->ioaddr1 = pci_resource_start(pdev, 1);
2350 brdp->ioaddr2 = pci_resource_start(pdev, 2);
2351 brdp->ioaddr1 = pci_resource_start(pdev, 1);
2354 brdp->ioaddr1 = pci_resource_start(pdev, 2);
2355 brdp->ioaddr2 = pci_resource_start(pdev, 1);
2358 dev_err(&pdev->dev, "unknown PCI board type=%u\n", brdtype);
2362 brdp->irq = pdev->irq;
2363 retval = stl_brdinit(brdp);
2367 pci_set_drvdata(pdev, brdp);
2369 for (i = 0; i < brdp->nrports; i++)
2370 tty_register_device(stl_serial,
2371 brdp->brdnr * STL_MAXPORTS + i, &pdev->dev);
2375 stl_brds[brdp->brdnr] = NULL;
2382 static void __devexit stl_pciremove(struct pci_dev *pdev)
2384 struct stlbrd *brdp = pci_get_drvdata(pdev);
2387 free_irq(brdp->irq, brdp);
2389 stl_cleanup_panels(brdp);
2391 release_region(brdp->ioaddr1, brdp->iosize1);
2392 if (brdp->iosize2 > 0)
2393 release_region(brdp->ioaddr2, brdp->iosize2);
2395 for (i = 0; i < brdp->nrports; i++)
2396 tty_unregister_device(stl_serial,
2397 brdp->brdnr * STL_MAXPORTS + i);
2399 stl_brds[brdp->brdnr] = NULL;
2403 static struct pci_driver stl_pcidriver = {
2405 .id_table = stl_pcibrds,
2406 .probe = stl_pciprobe,
2407 .remove = __devexit_p(stl_pciremove)
2410 /*****************************************************************************/
2413 * Return the board stats structure to user app.
2416 static int stl_getbrdstats(combrd_t __user *bp)
2418 combrd_t stl_brdstats;
2419 struct stlbrd *brdp;
2420 struct stlpanel *panelp;
2423 if (copy_from_user(&stl_brdstats, bp, sizeof(combrd_t)))
2425 if (stl_brdstats.brd >= STL_MAXBRDS)
2427 brdp = stl_brds[stl_brdstats.brd];
2431 memset(&stl_brdstats, 0, sizeof(combrd_t));
2432 stl_brdstats.brd = brdp->brdnr;
2433 stl_brdstats.type = brdp->brdtype;
2434 stl_brdstats.hwid = brdp->hwid;
2435 stl_brdstats.state = brdp->state;
2436 stl_brdstats.ioaddr = brdp->ioaddr1;
2437 stl_brdstats.ioaddr2 = brdp->ioaddr2;
2438 stl_brdstats.irq = brdp->irq;
2439 stl_brdstats.nrpanels = brdp->nrpanels;
2440 stl_brdstats.nrports = brdp->nrports;
2441 for (i = 0; i < brdp->nrpanels; i++) {
2442 panelp = brdp->panels[i];
2443 stl_brdstats.panels[i].panel = i;
2444 stl_brdstats.panels[i].hwid = panelp->hwid;
2445 stl_brdstats.panels[i].nrports = panelp->nrports;
2448 return copy_to_user(bp, &stl_brdstats, sizeof(combrd_t)) ? -EFAULT : 0;
2451 /*****************************************************************************/
2454 * Resolve the referenced port number into a port struct pointer.
2457 static struct stlport *stl_getport(int brdnr, int panelnr, int portnr)
2459 struct stlbrd *brdp;
2460 struct stlpanel *panelp;
2462 if (brdnr < 0 || brdnr >= STL_MAXBRDS)
2464 brdp = stl_brds[brdnr];
2467 if (panelnr < 0 || (unsigned int)panelnr >= brdp->nrpanels)
2469 panelp = brdp->panels[panelnr];
2472 if (portnr < 0 || (unsigned int)portnr >= panelp->nrports)
2474 return panelp->ports[portnr];
2477 /*****************************************************************************/
2480 * Return the port stats structure to user app. A NULL port struct
2481 * pointer passed in means that we need to find out from the app
2482 * what port to get stats for (used through board control device).
2485 static int stl_getportstats(struct tty_struct *tty, struct stlport *portp, comstats_t __user *cp)
2487 comstats_t stl_comstats;
2488 unsigned char *head, *tail;
2489 unsigned long flags;
2492 if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
2494 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2500 portp->stats.state = portp->istate;
2501 portp->stats.flags = portp->port.flags;
2502 portp->stats.hwid = portp->hwid;
2504 portp->stats.ttystate = 0;
2505 portp->stats.cflags = 0;
2506 portp->stats.iflags = 0;
2507 portp->stats.oflags = 0;
2508 portp->stats.lflags = 0;
2509 portp->stats.rxbuffered = 0;
2511 spin_lock_irqsave(&stallion_lock, flags);
2512 if (tty != NULL && portp->port.tty == tty) {
2513 portp->stats.ttystate = tty->flags;
2514 /* No longer available as a statistic */
2515 portp->stats.rxbuffered = 1; /*tty->flip.count; */
2516 if (tty->termios != NULL) {
2517 portp->stats.cflags = tty->termios->c_cflag;
2518 portp->stats.iflags = tty->termios->c_iflag;
2519 portp->stats.oflags = tty->termios->c_oflag;
2520 portp->stats.lflags = tty->termios->c_lflag;
2523 spin_unlock_irqrestore(&stallion_lock, flags);
2525 head = portp->tx.head;
2526 tail = portp->tx.tail;
2527 portp->stats.txbuffered = (head >= tail) ? (head - tail) :
2528 (STL_TXBUFSIZE - (tail - head));
2530 portp->stats.signals = (unsigned long) stl_getsignals(portp);
2532 return copy_to_user(cp, &portp->stats,
2533 sizeof(comstats_t)) ? -EFAULT : 0;
2536 /*****************************************************************************/
2539 * Clear the port stats structure. We also return it zeroed out...
2542 static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp)
2544 comstats_t stl_comstats;
2547 if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
2549 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2555 memset(&portp->stats, 0, sizeof(comstats_t));
2556 portp->stats.brd = portp->brdnr;
2557 portp->stats.panel = portp->panelnr;
2558 portp->stats.port = portp->portnr;
2559 return copy_to_user(cp, &portp->stats,
2560 sizeof(comstats_t)) ? -EFAULT : 0;
2563 /*****************************************************************************/
2566 * Return the entire driver ports structure to a user app.
2569 static int stl_getportstruct(struct stlport __user *arg)
2571 struct stlport stl_dummyport;
2572 struct stlport *portp;
2574 if (copy_from_user(&stl_dummyport, arg, sizeof(struct stlport)))
2576 portp = stl_getport(stl_dummyport.brdnr, stl_dummyport.panelnr,
2577 stl_dummyport.portnr);
2580 return copy_to_user(arg, portp, sizeof(struct stlport)) ? -EFAULT : 0;
2583 /*****************************************************************************/
2586 * Return the entire driver board structure to a user app.
2589 static int stl_getbrdstruct(struct stlbrd __user *arg)
2591 struct stlbrd stl_dummybrd;
2592 struct stlbrd *brdp;
2594 if (copy_from_user(&stl_dummybrd, arg, sizeof(struct stlbrd)))
2596 if (stl_dummybrd.brdnr >= STL_MAXBRDS)
2598 brdp = stl_brds[stl_dummybrd.brdnr];
2601 return copy_to_user(arg, brdp, sizeof(struct stlbrd)) ? -EFAULT : 0;
2604 /*****************************************************************************/
2607 * The "staliomem" device is also required to do some special operations
2608 * on the board and/or ports. In this driver it is mostly used for stats
2612 static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
2615 void __user *argp = (void __user *)arg;
2617 pr_debug("stl_memioctl(ip=%p,fp=%p,cmd=%x,arg=%lx)\n", ip, fp, cmd,arg);
2620 if (brdnr >= STL_MAXBRDS)
2625 case COM_GETPORTSTATS:
2626 rc = stl_getportstats(NULL, NULL, argp);
2628 case COM_CLRPORTSTATS:
2629 rc = stl_clrportstats(NULL, argp);
2631 case COM_GETBRDSTATS:
2632 rc = stl_getbrdstats(argp);
2635 rc = stl_getportstruct(argp);
2638 rc = stl_getbrdstruct(argp);
2648 static const struct tty_operations stl_ops = {
2652 .put_char = stl_putchar,
2653 .flush_chars = stl_flushchars,
2654 .write_room = stl_writeroom,
2655 .chars_in_buffer = stl_charsinbuffer,
2657 .set_termios = stl_settermios,
2658 .throttle = stl_throttle,
2659 .unthrottle = stl_unthrottle,
2662 .hangup = stl_hangup,
2663 .flush_buffer = stl_flushbuffer,
2664 .break_ctl = stl_breakctl,
2665 .wait_until_sent = stl_waituntilsent,
2666 .send_xchar = stl_sendxchar,
2667 .read_proc = stl_readproc,
2668 .tiocmget = stl_tiocmget,
2669 .tiocmset = stl_tiocmset,
2672 static const struct tty_port_operations stl_port_ops = {
2673 .carrier_raised = stl_carrier_raised,
2676 /*****************************************************************************/
2677 /* CD1400 HARDWARE FUNCTIONS */
2678 /*****************************************************************************/
2681 * These functions get/set/update the registers of the cd1400 UARTs.
2682 * Access to the cd1400 registers is via an address/data io port pair.
2683 * (Maybe should make this inline...)
2686 static int stl_cd1400getreg(struct stlport *portp, int regnr)
2688 outb((regnr + portp->uartaddr), portp->ioaddr);
2689 return inb(portp->ioaddr + EREG_DATA);
2692 static void stl_cd1400setreg(struct stlport *portp, int regnr, int value)
2694 outb(regnr + portp->uartaddr, portp->ioaddr);
2695 outb(value, portp->ioaddr + EREG_DATA);
2698 static int stl_cd1400updatereg(struct stlport *portp, int regnr, int value)
2700 outb(regnr + portp->uartaddr, portp->ioaddr);
2701 if (inb(portp->ioaddr + EREG_DATA) != value) {
2702 outb(value, portp->ioaddr + EREG_DATA);
2708 /*****************************************************************************/
2711 * Inbitialize the UARTs in a panel. We don't care what sort of board
2712 * these ports are on - since the port io registers are almost
2713 * identical when dealing with ports.
2716 static int stl_cd1400panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
2720 int nrchips, uartaddr, ioaddr;
2721 unsigned long flags;
2723 pr_debug("stl_panelinit(brdp=%p,panelp=%p)\n", brdp, panelp);
2725 spin_lock_irqsave(&brd_lock, flags);
2726 BRDENABLE(panelp->brdnr, panelp->pagenr);
2729 * Check that each chip is present and started up OK.
2732 nrchips = panelp->nrports / CD1400_PORTS;
2733 for (i = 0; i < nrchips; i++) {
2734 if (brdp->brdtype == BRD_ECHPCI) {
2735 outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
2736 ioaddr = panelp->iobase;
2738 ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
2739 uartaddr = (i & 0x01) ? 0x080 : 0;
2740 outb((GFRCR + uartaddr), ioaddr);
2741 outb(0, (ioaddr + EREG_DATA));
2742 outb((CCR + uartaddr), ioaddr);
2743 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
2744 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
2745 outb((GFRCR + uartaddr), ioaddr);
2746 for (j = 0; j < CCR_MAXWAIT; j++)
2747 if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
2750 if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
2751 printk("STALLION: cd1400 not responding, "
2752 "brd=%d panel=%d chip=%d\n",
2753 panelp->brdnr, panelp->panelnr, i);
2756 chipmask |= (0x1 << i);
2757 outb((PPR + uartaddr), ioaddr);
2758 outb(PPR_SCALAR, (ioaddr + EREG_DATA));
2761 BRDDISABLE(panelp->brdnr);
2762 spin_unlock_irqrestore(&brd_lock, flags);
2766 /*****************************************************************************/
2769 * Initialize hardware specific port registers.
2772 static void stl_cd1400portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp)
2774 unsigned long flags;
2775 pr_debug("stl_cd1400portinit(brdp=%p,panelp=%p,portp=%p)\n", brdp,
2778 if ((brdp == NULL) || (panelp == NULL) ||
2782 spin_lock_irqsave(&brd_lock, flags);
2783 portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
2784 (portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
2785 portp->uartaddr = (portp->portnr & 0x04) << 5;
2786 portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
2788 BRDENABLE(portp->brdnr, portp->pagenr);
2789 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2790 stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
2791 portp->hwid = stl_cd1400getreg(portp, GFRCR);
2792 BRDDISABLE(portp->brdnr);
2793 spin_unlock_irqrestore(&brd_lock, flags);
2796 /*****************************************************************************/
2799 * Wait for the command register to be ready. We will poll this,
2800 * since it won't usually take too long to be ready.
2803 static void stl_cd1400ccrwait(struct stlport *portp)
2807 for (i = 0; i < CCR_MAXWAIT; i++)
2808 if (stl_cd1400getreg(portp, CCR) == 0)
2811 printk("STALLION: cd1400 not responding, port=%d panel=%d brd=%d\n",
2812 portp->portnr, portp->panelnr, portp->brdnr);
2815 /*****************************************************************************/
2818 * Set up the cd1400 registers for a port based on the termios port
2822 static void stl_cd1400setport(struct stlport *portp, struct ktermios *tiosp)
2824 struct stlbrd *brdp;
2825 unsigned long flags;
2826 unsigned int clkdiv, baudrate;
2827 unsigned char cor1, cor2, cor3;
2828 unsigned char cor4, cor5, ccr;
2829 unsigned char srer, sreron, sreroff;
2830 unsigned char mcor1, mcor2, rtpr;
2831 unsigned char clk, div;
2847 brdp = stl_brds[portp->brdnr];
2852 * Set up the RX char ignore mask with those RX error types we
2853 * can ignore. We can get the cd1400 to help us out a little here,
2854 * it will ignore parity errors and breaks for us.
2856 portp->rxignoremsk = 0;
2857 if (tiosp->c_iflag & IGNPAR) {
2858 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
2859 cor1 |= COR1_PARIGNORE;
2861 if (tiosp->c_iflag & IGNBRK) {
2862 portp->rxignoremsk |= ST_BREAK;
2863 cor4 |= COR4_IGNBRK;
2866 portp->rxmarkmsk = ST_OVERRUN;
2867 if (tiosp->c_iflag & (INPCK | PARMRK))
2868 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
2869 if (tiosp->c_iflag & BRKINT)
2870 portp->rxmarkmsk |= ST_BREAK;
2873 * Go through the char size, parity and stop bits and set all the
2874 * option register appropriately.
2876 switch (tiosp->c_cflag & CSIZE) {
2891 if (tiosp->c_cflag & CSTOPB)
2896 if (tiosp->c_cflag & PARENB) {
2897 if (tiosp->c_cflag & PARODD)
2898 cor1 |= (COR1_PARENB | COR1_PARODD);
2900 cor1 |= (COR1_PARENB | COR1_PAREVEN);
2902 cor1 |= COR1_PARNONE;
2906 * Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
2907 * space for hardware flow control and the like. This should be set to
2908 * VMIN. Also here we will set the RX data timeout to 10ms - this should
2909 * really be based on VTIME.
2911 cor3 |= FIFO_RXTHRESHOLD;
2915 * Calculate the baud rate timers. For now we will just assume that
2916 * the input and output baud are the same. Could have used a baud
2917 * table here, but this way we can generate virtually any baud rate
2920 baudrate = tiosp->c_cflag & CBAUD;
2921 if (baudrate & CBAUDEX) {
2922 baudrate &= ~CBAUDEX;
2923 if ((baudrate < 1) || (baudrate > 4))
2924 tiosp->c_cflag &= ~CBAUDEX;
2928 baudrate = stl_baudrates[baudrate];
2929 if ((tiosp->c_cflag & CBAUD) == B38400) {
2930 if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2932 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2934 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2936 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2938 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
2939 baudrate = (portp->baud_base / portp->custom_divisor);
2941 if (baudrate > STL_CD1400MAXBAUD)
2942 baudrate = STL_CD1400MAXBAUD;
2945 for (clk = 0; clk < CD1400_NUMCLKS; clk++) {
2946 clkdiv = (portp->clk / stl_cd1400clkdivs[clk]) / baudrate;
2950 div = (unsigned char) clkdiv;
2954 * Check what form of modem signaling is required and set it up.
2956 if ((tiosp->c_cflag & CLOCAL) == 0) {
2959 sreron |= SRER_MODEM;
2960 portp->port.flags |= ASYNC_CHECK_CD;
2962 portp->port.flags &= ~ASYNC_CHECK_CD;
2965 * Setup cd1400 enhanced modes if we can. In particular we want to
2966 * handle as much of the flow control as possible automatically. As
2967 * well as saving a few CPU cycles it will also greatly improve flow
2968 * control reliability.
2970 if (tiosp->c_iflag & IXON) {
2973 if (tiosp->c_iflag & IXANY)
2977 if (tiosp->c_cflag & CRTSCTS) {
2979 mcor1 |= FIFO_RTSTHRESHOLD;
2983 * All cd1400 register values calculated so go through and set
2987 pr_debug("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
2988 portp->portnr, portp->panelnr, portp->brdnr);
2989 pr_debug(" cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n",
2990 cor1, cor2, cor3, cor4, cor5);
2991 pr_debug(" mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
2992 mcor1, mcor2, rtpr, sreron, sreroff);
2993 pr_debug(" tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
2994 pr_debug(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
2995 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
2996 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
2998 spin_lock_irqsave(&brd_lock, flags);
2999 BRDENABLE(portp->brdnr, portp->pagenr);
3000 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3001 srer = stl_cd1400getreg(portp, SRER);
3002 stl_cd1400setreg(portp, SRER, 0);
3003 if (stl_cd1400updatereg(portp, COR1, cor1))
3005 if (stl_cd1400updatereg(portp, COR2, cor2))
3007 if (stl_cd1400updatereg(portp, COR3, cor3))
3010 stl_cd1400ccrwait(portp);
3011 stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
3013 stl_cd1400setreg(portp, COR4, cor4);
3014 stl_cd1400setreg(portp, COR5, cor5);
3015 stl_cd1400setreg(portp, MCOR1, mcor1);
3016 stl_cd1400setreg(portp, MCOR2, mcor2);
3018 stl_cd1400setreg(portp, TCOR, clk);
3019 stl_cd1400setreg(portp, TBPR, div);
3020 stl_cd1400setreg(portp, RCOR, clk);
3021 stl_cd1400setreg(portp, RBPR, div);
3023 stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
3024 stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
3025 stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
3026 stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
3027 stl_cd1400setreg(portp, RTPR, rtpr);
3028 mcor1 = stl_cd1400getreg(portp, MSVR1);
3029 if (mcor1 & MSVR1_DCD)
3030 portp->sigs |= TIOCM_CD;
3032 portp->sigs &= ~TIOCM_CD;
3033 stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
3034 BRDDISABLE(portp->brdnr);
3035 spin_unlock_irqrestore(&brd_lock, flags);
3038 /*****************************************************************************/
3041 * Set the state of the DTR and RTS signals.
3044 static void stl_cd1400setsignals(struct stlport *portp, int dtr, int rts)
3046 unsigned char msvr1, msvr2;
3047 unsigned long flags;
3049 pr_debug("stl_cd1400setsignals(portp=%p,dtr=%d,rts=%d)\n",
3059 spin_lock_irqsave(&brd_lock, flags);
3060 BRDENABLE(portp->brdnr, portp->pagenr);
3061 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3063 stl_cd1400setreg(portp, MSVR2, msvr2);
3065 stl_cd1400setreg(portp, MSVR1, msvr1);
3066 BRDDISABLE(portp->brdnr);
3067 spin_unlock_irqrestore(&brd_lock, flags);
3070 /*****************************************************************************/
3073 * Return the state of the signals.
3076 static int stl_cd1400getsignals(struct stlport *portp)
3078 unsigned char msvr1, msvr2;
3079 unsigned long flags;
3082 pr_debug("stl_cd1400getsignals(portp=%p)\n", portp);
3084 spin_lock_irqsave(&brd_lock, flags);
3085 BRDENABLE(portp->brdnr, portp->pagenr);
3086 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3087 msvr1 = stl_cd1400getreg(portp, MSVR1);
3088 msvr2 = stl_cd1400getreg(portp, MSVR2);
3089 BRDDISABLE(portp->brdnr);
3090 spin_unlock_irqrestore(&brd_lock, flags);
3093 sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
3094 sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
3095 sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
3096 sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
3098 sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
3099 sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
3106 /*****************************************************************************/
3109 * Enable/Disable the Transmitter and/or Receiver.
3112 static void stl_cd1400enablerxtx(struct stlport *portp, int rx, int tx)
3115 unsigned long flags;
3117 pr_debug("stl_cd1400enablerxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
3122 ccr |= CCR_TXDISABLE;
3124 ccr |= CCR_TXENABLE;
3126 ccr |= CCR_RXDISABLE;
3128 ccr |= CCR_RXENABLE;
3130 spin_lock_irqsave(&brd_lock, flags);
3131 BRDENABLE(portp->brdnr, portp->pagenr);
3132 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3133 stl_cd1400ccrwait(portp);
3134 stl_cd1400setreg(portp, CCR, ccr);
3135 stl_cd1400ccrwait(portp);
3136 BRDDISABLE(portp->brdnr);
3137 spin_unlock_irqrestore(&brd_lock, flags);
3140 /*****************************************************************************/
3143 * Start/stop the Transmitter and/or Receiver.
3146 static void stl_cd1400startrxtx(struct stlport *portp, int rx, int tx)
3148 unsigned char sreron, sreroff;
3149 unsigned long flags;
3151 pr_debug("stl_cd1400startrxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
3156 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
3158 sreron |= SRER_TXDATA;
3160 sreron |= SRER_TXEMPTY;
3162 sreroff |= SRER_RXDATA;
3164 sreron |= SRER_RXDATA;
3166 spin_lock_irqsave(&brd_lock, flags);
3167 BRDENABLE(portp->brdnr, portp->pagenr);
3168 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3169 stl_cd1400setreg(portp, SRER,
3170 ((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
3171 BRDDISABLE(portp->brdnr);
3173 set_bit(ASYI_TXBUSY, &portp->istate);
3174 spin_unlock_irqrestore(&brd_lock, flags);
3177 /*****************************************************************************/
3180 * Disable all interrupts from this port.
3183 static void stl_cd1400disableintrs(struct stlport *portp)
3185 unsigned long flags;
3187 pr_debug("stl_cd1400disableintrs(portp=%p)\n", portp);
3189 spin_lock_irqsave(&brd_lock, flags);
3190 BRDENABLE(portp->brdnr, portp->pagenr);
3191 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3192 stl_cd1400setreg(portp, SRER, 0);
3193 BRDDISABLE(portp->brdnr);
3194 spin_unlock_irqrestore(&brd_lock, flags);
3197 /*****************************************************************************/
3199 static void stl_cd1400sendbreak(struct stlport *portp, int len)
3201 unsigned long flags;
3203 pr_debug("stl_cd1400sendbreak(portp=%p,len=%d)\n", portp, len);
3205 spin_lock_irqsave(&brd_lock, flags);
3206 BRDENABLE(portp->brdnr, portp->pagenr);
3207 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3208 stl_cd1400setreg(portp, SRER,
3209 ((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
3211 BRDDISABLE(portp->brdnr);
3212 portp->brklen = len;
3214 portp->stats.txbreaks++;
3215 spin_unlock_irqrestore(&brd_lock, flags);
3218 /*****************************************************************************/
3221 * Take flow control actions...
3224 static void stl_cd1400flowctrl(struct stlport *portp, int state)
3226 struct tty_struct *tty;
3227 unsigned long flags;
3229 pr_debug("stl_cd1400flowctrl(portp=%p,state=%x)\n", portp, state);
3233 tty = tty_port_tty_get(&portp->port);
3237 spin_lock_irqsave(&brd_lock, flags);
3238 BRDENABLE(portp->brdnr, portp->pagenr);
3239 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3242 if (tty->termios->c_iflag & IXOFF) {
3243 stl_cd1400ccrwait(portp);
3244 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3245 portp->stats.rxxon++;
3246 stl_cd1400ccrwait(portp);
3249 * Question: should we return RTS to what it was before? It may
3250 * have been set by an ioctl... Suppose not, since if you have
3251 * hardware flow control set then it is pretty silly to go and
3252 * set the RTS line by hand.
3254 if (tty->termios->c_cflag & CRTSCTS) {
3255 stl_cd1400setreg(portp, MCOR1,
3256 (stl_cd1400getreg(portp, MCOR1) |
3257 FIFO_RTSTHRESHOLD));
3258 stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
3259 portp->stats.rxrtson++;
3262 if (tty->termios->c_iflag & IXOFF) {
3263 stl_cd1400ccrwait(portp);
3264 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3265 portp->stats.rxxoff++;
3266 stl_cd1400ccrwait(portp);
3268 if (tty->termios->c_cflag & CRTSCTS) {
3269 stl_cd1400setreg(portp, MCOR1,
3270 (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3271 stl_cd1400setreg(portp, MSVR2, 0);
3272 portp->stats.rxrtsoff++;
3276 BRDDISABLE(portp->brdnr);
3277 spin_unlock_irqrestore(&brd_lock, flags);
3281 /*****************************************************************************/
3284 * Send a flow control character...
3287 static void stl_cd1400sendflow(struct stlport *portp, int state)
3289 struct tty_struct *tty;
3290 unsigned long flags;
3292 pr_debug("stl_cd1400sendflow(portp=%p,state=%x)\n", portp, state);
3296 tty = tty_port_tty_get(&portp->port);
3300 spin_lock_irqsave(&brd_lock, flags);
3301 BRDENABLE(portp->brdnr, portp->pagenr);
3302 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3304 stl_cd1400ccrwait(portp);
3305 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3306 portp->stats.rxxon++;
3307 stl_cd1400ccrwait(portp);
3309 stl_cd1400ccrwait(portp);
3310 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3311 portp->stats.rxxoff++;
3312 stl_cd1400ccrwait(portp);
3314 BRDDISABLE(portp->brdnr);
3315 spin_unlock_irqrestore(&brd_lock, flags);
3319 /*****************************************************************************/
3321 static void stl_cd1400flush(struct stlport *portp)
3323 unsigned long flags;
3325 pr_debug("stl_cd1400flush(portp=%p)\n", portp);
3330 spin_lock_irqsave(&brd_lock, flags);
3331 BRDENABLE(portp->brdnr, portp->pagenr);
3332 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3333 stl_cd1400ccrwait(portp);
3334 stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
3335 stl_cd1400ccrwait(portp);
3336 portp->tx.tail = portp->tx.head;
3337 BRDDISABLE(portp->brdnr);
3338 spin_unlock_irqrestore(&brd_lock, flags);
3341 /*****************************************************************************/
3344 * Return the current state of data flow on this port. This is only
3345 * really interresting when determining if data has fully completed
3346 * transmission or not... This is easy for the cd1400, it accurately
3347 * maintains the busy port flag.
3350 static int stl_cd1400datastate(struct stlport *portp)
3352 pr_debug("stl_cd1400datastate(portp=%p)\n", portp);
3357 return test_bit(ASYI_TXBUSY, &portp->istate) ? 1 : 0;
3360 /*****************************************************************************/
3363 * Interrupt service routine for cd1400 EasyIO boards.
3366 static void stl_cd1400eiointr(struct stlpanel *panelp, unsigned int iobase)
3368 unsigned char svrtype;
3370 pr_debug("stl_cd1400eiointr(panelp=%p,iobase=%x)\n", panelp, iobase);
3372 spin_lock(&brd_lock);
3374 svrtype = inb(iobase + EREG_DATA);
3375 if (panelp->nrports > 4) {
3376 outb((SVRR + 0x80), iobase);
3377 svrtype |= inb(iobase + EREG_DATA);
3380 if (svrtype & SVRR_RX)
3381 stl_cd1400rxisr(panelp, iobase);
3382 else if (svrtype & SVRR_TX)
3383 stl_cd1400txisr(panelp, iobase);
3384 else if (svrtype & SVRR_MDM)
3385 stl_cd1400mdmisr(panelp, iobase);
3387 spin_unlock(&brd_lock);
3390 /*****************************************************************************/
3393 * Interrupt service routine for cd1400 panels.
3396 static void stl_cd1400echintr(struct stlpanel *panelp, unsigned int iobase)
3398 unsigned char svrtype;
3400 pr_debug("stl_cd1400echintr(panelp=%p,iobase=%x)\n", panelp, iobase);
3403 svrtype = inb(iobase + EREG_DATA);
3404 outb((SVRR + 0x80), iobase);
3405 svrtype |= inb(iobase + EREG_DATA);
3406 if (svrtype & SVRR_RX)
3407 stl_cd1400rxisr(panelp, iobase);
3408 else if (svrtype & SVRR_TX)
3409 stl_cd1400txisr(panelp, iobase);
3410 else if (svrtype & SVRR_MDM)
3411 stl_cd1400mdmisr(panelp, iobase);
3415 /*****************************************************************************/
3418 * Unfortunately we need to handle breaks in the TX data stream, since
3419 * this is the only way to generate them on the cd1400.
3422 static int stl_cd1400breakisr(struct stlport *portp, int ioaddr)
3424 if (portp->brklen == 1) {
3425 outb((COR2 + portp->uartaddr), ioaddr);
3426 outb((inb(ioaddr + EREG_DATA) | COR2_ETC),
3427 (ioaddr + EREG_DATA));
3428 outb((TDR + portp->uartaddr), ioaddr);
3429 outb(ETC_CMD, (ioaddr + EREG_DATA));
3430 outb(ETC_STARTBREAK, (ioaddr + EREG_DATA));
3431 outb((SRER + portp->uartaddr), ioaddr);
3432 outb((inb(ioaddr + EREG_DATA) & ~(SRER_TXDATA | SRER_TXEMPTY)),
3433 (ioaddr + EREG_DATA));
3435 } else if (portp->brklen > 1) {
3436 outb((TDR + portp->uartaddr), ioaddr);
3437 outb(ETC_CMD, (ioaddr + EREG_DATA));
3438 outb(ETC_STOPBREAK, (ioaddr + EREG_DATA));
3442 outb((COR2 + portp->uartaddr), ioaddr);
3443 outb((inb(ioaddr + EREG_DATA) & ~COR2_ETC),
3444 (ioaddr + EREG_DATA));
3450 /*****************************************************************************/
3453 * Transmit interrupt handler. This has gotta be fast! Handling TX
3454 * chars is pretty simple, stuff as many as possible from the TX buffer
3455 * into the cd1400 FIFO. Must also handle TX breaks here, since they
3456 * are embedded as commands in the data stream. Oh no, had to use a goto!
3457 * This could be optimized more, will do when I get time...
3458 * In practice it is possible that interrupts are enabled but that the
3459 * port has been hung up. Need to handle not having any TX buffer here,
3460 * this is done by using the side effect that head and tail will also
3461 * be NULL if the buffer has been freed.
3464 static void stl_cd1400txisr(struct stlpanel *panelp, int ioaddr)
3466 struct stlport *portp;
3469 unsigned char ioack, srer;
3470 struct tty_struct *tty;
3472 pr_debug("stl_cd1400txisr(panelp=%p,ioaddr=%x)\n", panelp, ioaddr);
3474 ioack = inb(ioaddr + EREG_TXACK);
3475 if (((ioack & panelp->ackmask) != 0) ||
3476 ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
3477 printk("STALLION: bad TX interrupt ack value=%x\n", ioack);
3480 portp = panelp->ports[(ioack >> 3)];
3483 * Unfortunately we need to handle breaks in the data stream, since
3484 * this is the only way to generate them on the cd1400. Do it now if
3485 * a break is to be sent.
3487 if (portp->brklen != 0)
3488 if (stl_cd1400breakisr(portp, ioaddr))
3491 head = portp->tx.head;
3492 tail = portp->tx.tail;
3493 len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
3494 if ((len == 0) || ((len < STL_TXBUFLOW) &&
3495 (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
3496 set_bit(ASYI_TXLOW, &portp->istate);
3497 tty = tty_port_tty_get(&portp->port);
3505 outb((SRER + portp->uartaddr), ioaddr);
3506 srer = inb(ioaddr + EREG_DATA);
3507 if (srer & SRER_TXDATA) {
3508 srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
3510 srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
3511 clear_bit(ASYI_TXBUSY, &portp->istate);
3513 outb(srer, (ioaddr + EREG_DATA));
3515 len = min(len, CD1400_TXFIFOSIZE);
3516 portp->stats.txtotal += len;
3517 stlen = min_t(unsigned int, len,
3518 (portp->tx.buf + STL_TXBUFSIZE) - tail);
3519 outb((TDR + portp->uartaddr), ioaddr);
3520 outsb((ioaddr + EREG_DATA), tail, stlen);
3523 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
3524 tail = portp->tx.buf;
3526 outsb((ioaddr + EREG_DATA), tail, len);
3529 portp->tx.tail = tail;
3533 outb((EOSRR + portp->uartaddr), ioaddr);
3534 outb(0, (ioaddr + EREG_DATA));
3537 /*****************************************************************************/
3540 * Receive character interrupt handler. Determine if we have good chars
3541 * or bad chars and then process appropriately. Good chars are easy
3542 * just shove the lot into the RX buffer and set all status byte to 0.
3543 * If a bad RX char then process as required. This routine needs to be
3544 * fast! In practice it is possible that we get an interrupt on a port
3545 * that is closed. This can happen on hangups - since they completely
3546 * shutdown a port not in user context. Need to handle this case.
3549 static void stl_cd1400rxisr(struct stlpanel *panelp, int ioaddr)
3551 struct stlport *portp;
3552 struct tty_struct *tty;
3553 unsigned int ioack, len, buflen;
3554 unsigned char status;
3557 pr_debug("stl_cd1400rxisr(panelp=%p,ioaddr=%x)\n", panelp, ioaddr);
3559 ioack = inb(ioaddr + EREG_RXACK);
3560 if ((ioack & panelp->ackmask) != 0) {
3561 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
3564 portp = panelp->ports[(ioack >> 3)];
3565 tty = tty_port_tty_get(&portp->port);
3567 if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
3568 outb((RDCR + portp->uartaddr), ioaddr);
3569 len = inb(ioaddr + EREG_DATA);
3570 if (tty == NULL || (buflen = tty_buffer_request_room(tty, len)) == 0) {
3571 len = min_t(unsigned int, len, sizeof(stl_unwanted));
3572 outb((RDSR + portp->uartaddr), ioaddr);
3573 insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
3574 portp->stats.rxlost += len;
3575 portp->stats.rxtotal += len;
3577 len = min(len, buflen);
3580 outb((RDSR + portp->uartaddr), ioaddr);
3581 tty_prepare_flip_string(tty, &ptr, len);
3582 insb((ioaddr + EREG_DATA), ptr, len);
3583 tty_schedule_flip(tty);
3584 portp->stats.rxtotal += len;
3587 } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
3588 outb((RDSR + portp->uartaddr), ioaddr);
3589 status = inb(ioaddr + EREG_DATA);
3590 ch = inb(ioaddr + EREG_DATA);
3591 if (status & ST_PARITY)
3592 portp->stats.rxparity++;
3593 if (status & ST_FRAMING)
3594 portp->stats.rxframing++;
3595 if (status & ST_OVERRUN)
3596 portp->stats.rxoverrun++;
3597 if (status & ST_BREAK)
3598 portp->stats.rxbreaks++;
3599 if (status & ST_SCHARMASK) {
3600 if ((status & ST_SCHARMASK) == ST_SCHAR1)
3601 portp->stats.txxon++;
3602 if ((status & ST_SCHARMASK) == ST_SCHAR2)
3603 portp->stats.txxoff++;
3606 if (tty != NULL && (portp->rxignoremsk & status) == 0) {
3607 if (portp->rxmarkmsk & status) {
3608 if (status & ST_BREAK) {
3610 if (portp->port.flags & ASYNC_SAK) {
3612 BRDENABLE(portp->brdnr, portp->pagenr);
3614 } else if (status & ST_PARITY)
3615 status = TTY_PARITY;
3616 else if (status & ST_FRAMING)
3618 else if(status & ST_OVERRUN)
3619 status = TTY_OVERRUN;
3624 tty_insert_flip_char(tty, ch, status);
3625 tty_schedule_flip(tty);
3628 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
3635 outb((EOSRR + portp->uartaddr), ioaddr);
3636 outb(0, (ioaddr + EREG_DATA));
3639 /*****************************************************************************/
3642 * Modem interrupt handler. The is called when the modem signal line
3643 * (DCD) has changed state. Leave most of the work to the off-level
3644 * processing routine.
3647 static void stl_cd1400mdmisr(struct stlpanel *panelp, int ioaddr)
3649 struct stlport *portp;
3653 pr_debug("stl_cd1400mdmisr(panelp=%p)\n", panelp);
3655 ioack = inb(ioaddr + EREG_MDACK);
3656 if (((ioack & panelp->ackmask) != 0) ||
3657 ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
3658 printk("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
3661 portp = panelp->ports[(ioack >> 3)];
3663 outb((MISR + portp->uartaddr), ioaddr);
3664 misr = inb(ioaddr + EREG_DATA);
3665 if (misr & MISR_DCD) {
3666 stl_cd_change(portp);
3667 portp->stats.modem++;
3670 outb((EOSRR + portp->uartaddr), ioaddr);
3671 outb(0, (ioaddr + EREG_DATA));
3674 /*****************************************************************************/
3675 /* SC26198 HARDWARE FUNCTIONS */
3676 /*****************************************************************************/
3679 * These functions get/set/update the registers of the sc26198 UARTs.
3680 * Access to the sc26198 registers is via an address/data io port pair.
3681 * (Maybe should make this inline...)
3684 static int stl_sc26198getreg(struct stlport *portp, int regnr)
3686 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
3687 return inb(portp->ioaddr + XP_DATA);
3690 static void stl_sc26198setreg(struct stlport *portp, int regnr, int value)
3692 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
3693 outb(value, (portp->ioaddr + XP_DATA));
3696 static int stl_sc26198updatereg(struct stlport *portp, int regnr, int value)
3698 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
3699 if (inb(portp->ioaddr + XP_DATA) != value) {
3700 outb(value, (portp->ioaddr + XP_DATA));
3706 /*****************************************************************************/
3709 * Functions to get and set the sc26198 global registers.
3712 static int stl_sc26198getglobreg(struct stlport *portp, int regnr)
3714 outb(regnr, (portp->ioaddr + XP_ADDR));
3715 return inb(portp->ioaddr + XP_DATA);
3719 static void stl_sc26198setglobreg(struct stlport *portp, int regnr, int value)
3721 outb(regnr, (portp->ioaddr + XP_ADDR));
3722 outb(value, (portp->ioaddr + XP_DATA));
3726 /*****************************************************************************/
3729 * Inbitialize the UARTs in a panel. We don't care what sort of board
3730 * these ports are on - since the port io registers are almost
3731 * identical when dealing with ports.
3734 static int stl_sc26198panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
3737 int nrchips, ioaddr;
3739 pr_debug("stl_sc26198panelinit(brdp=%p,panelp=%p)\n", brdp, panelp);
3741 BRDENABLE(panelp->brdnr, panelp->pagenr);
3744 * Check that each chip is present and started up OK.
3747 nrchips = (panelp->nrports + 4) / SC26198_PORTS;
3748 if (brdp->brdtype == BRD_ECHPCI)
3749 outb(panelp->pagenr, brdp->ioctrl);
3751 for (i = 0; i < nrchips; i++) {
3752 ioaddr = panelp->iobase + (i * 4);
3753 outb(SCCR, (ioaddr + XP_ADDR));
3754 outb(CR_RESETALL, (ioaddr + XP_DATA));
3755 outb(TSTR, (ioaddr + XP_ADDR));
3756 if (inb(ioaddr + XP_DATA) != 0) {
3757 printk("STALLION: sc26198 not responding, "
3758 "brd=%d panel=%d chip=%d\n",
3759 panelp->brdnr, panelp->panelnr, i);
3762 chipmask |= (0x1 << i);
3763 outb(GCCR, (ioaddr + XP_ADDR));
3764 outb(GCCR_IVRTYPCHANACK, (ioaddr + XP_DATA));
3765 outb(WDTRCR, (ioaddr + XP_ADDR));
3766 outb(0xff, (ioaddr + XP_DATA));
3769 BRDDISABLE(panelp->brdnr);
3773 /*****************************************************************************/
3776 * Initialize hardware specific port registers.
3779 static void stl_sc26198portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp)
3781 pr_debug("stl_sc26198portinit(brdp=%p,panelp=%p,portp=%p)\n", brdp,
3784 if ((brdp == NULL) || (panelp == NULL) ||
3788 portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
3789 portp->uartaddr = (portp->portnr & 0x07) << 4;
3790 portp->pagenr = panelp->pagenr;
3793 BRDENABLE(portp->brdnr, portp->pagenr);
3794 stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
3795 BRDDISABLE(portp->brdnr);
3798 /*****************************************************************************/
3801 * Set up the sc26198 registers for a port based on the termios port
3805 static void stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp)
3807 struct stlbrd *brdp;
3808 unsigned long flags;
3809 unsigned int baudrate;
3810 unsigned char mr0, mr1, mr2, clk;
3811 unsigned char imron, imroff, iopr, ipr;
3821 brdp = stl_brds[portp->brdnr];
3826 * Set up the RX char ignore mask with those RX error types we
3829 portp->rxignoremsk = 0;
3830 if (tiosp->c_iflag & IGNPAR)
3831 portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
3833 if (tiosp->c_iflag & IGNBRK)
3834 portp->rxignoremsk |= SR_RXBREAK;
3836 portp->rxmarkmsk = SR_RXOVERRUN;
3837 if (tiosp->c_iflag & (INPCK | PARMRK))
3838 portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
3839 if (tiosp->c_iflag & BRKINT)
3840 portp->rxmarkmsk |= SR_RXBREAK;
3843 * Go through the char size, parity and stop bits and set all the
3844 * option register appropriately.
3846 switch (tiosp->c_cflag & CSIZE) {
3861 if (tiosp->c_cflag & CSTOPB)
3866 if (tiosp->c_cflag & PARENB) {
3867 if (tiosp->c_cflag & PARODD)
3868 mr1 |= (MR1_PARENB | MR1_PARODD);
3870 mr1 |= (MR1_PARENB | MR1_PAREVEN);
3874 mr1 |= MR1_ERRBLOCK;
3877 * Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
3878 * space for hardware flow control and the like. This should be set to
3881 mr2 |= MR2_RXFIFOHALF;
3884 * Calculate the baud rate timers. For now we will just assume that
3885 * the input and output baud are the same. The sc26198 has a fixed
3886 * baud rate table, so only discrete baud rates possible.
3888 baudrate = tiosp->c_cflag & CBAUD;
3889 if (baudrate & CBAUDEX) {
3890 baudrate &= ~CBAUDEX;
3891 if ((baudrate < 1) || (baudrate > 4))
3892 tiosp->c_cflag &= ~CBAUDEX;
3896 baudrate = stl_baudrates[baudrate];
3897 if ((tiosp->c_cflag & CBAUD) == B38400) {
3898 if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3900 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3902 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3904 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3906 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3907 baudrate = (portp->baud_base / portp->custom_divisor);
3909 if (baudrate > STL_SC26198MAXBAUD)
3910 baudrate = STL_SC26198MAXBAUD;
3913 for (clk = 0; clk < SC26198_NRBAUDS; clk++)
3914 if (baudrate <= sc26198_baudtable[clk])
3918 * Check what form of modem signaling is required and set it up.
3920 if (tiosp->c_cflag & CLOCAL) {
3921 portp->port.flags &= ~ASYNC_CHECK_CD;
3923 iopr |= IOPR_DCDCOS;
3925 portp->port.flags |= ASYNC_CHECK_CD;
3929 * Setup sc26198 enhanced modes if we can. In particular we want to
3930 * handle as much of the flow control as possible automatically. As
3931 * well as saving a few CPU cycles it will also greatly improve flow
3932 * control reliability.
3934 if (tiosp->c_iflag & IXON) {
3935 mr0 |= MR0_SWFTX | MR0_SWFT;
3936 imron |= IR_XONXOFF;
3938 imroff |= IR_XONXOFF;
3940 if (tiosp->c_iflag & IXOFF)
3943 if (tiosp->c_cflag & CRTSCTS) {
3949 * All sc26198 register values calculated so go through and set
3953 pr_debug("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
3954 portp->portnr, portp->panelnr, portp->brdnr);
3955 pr_debug(" mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
3956 pr_debug(" iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
3957 pr_debug(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
3958 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
3959 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
3961 spin_lock_irqsave(&brd_lock, flags);
3962 BRDENABLE(portp->brdnr, portp->pagenr);
3963 stl_sc26198setreg(portp, IMR, 0);
3964 stl_sc26198updatereg(portp, MR0, mr0);
3965 stl_sc26198updatereg(portp, MR1, mr1);
3966 stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
3967 stl_sc26198updatereg(portp, MR2, mr2);
3968 stl_sc26198updatereg(portp, IOPIOR,
3969 ((stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr));
3972 stl_sc26198setreg(portp, TXCSR, clk);
3973 stl_sc26198setreg(portp, RXCSR, clk);
3976 stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
3977 stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
3979 ipr = stl_sc26198getreg(portp, IPR);
3981 portp->sigs &= ~TIOCM_CD;
3983 portp->sigs |= TIOCM_CD;
3985 portp->imr = (portp->imr & ~imroff) | imron;
3986 stl_sc26198setreg(portp, IMR, portp->imr);
3987 BRDDISABLE(portp->brdnr);
3988 spin_unlock_irqrestore(&brd_lock, flags);
3991 /*****************************************************************************/
3994 * Set the state of the DTR and RTS signals.
3997 static void stl_sc26198setsignals(struct stlport *portp, int dtr, int rts)
3999 unsigned char iopioron, iopioroff;
4000 unsigned long flags;
4002 pr_debug("stl_sc26198setsignals(portp=%p,dtr=%d,rts=%d)\n", portp,
4008 iopioroff |= IPR_DTR;
4010 iopioron |= IPR_DTR;
4012 iopioroff |= IPR_RTS;
4014 iopioron |= IPR_RTS;
4016 spin_lock_irqsave(&brd_lock, flags);
4017 BRDENABLE(portp->brdnr, portp->pagenr);
4018 stl_sc26198setreg(portp, IOPIOR,
4019 ((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
4020 BRDDISABLE(portp->brdnr);
4021 spin_unlock_irqrestore(&brd_lock, flags);
4024 /*****************************************************************************/
4027 * Return the state of the signals.
4030 static int stl_sc26198getsignals(struct stlport *portp)
4033 unsigned long flags;
4036 pr_debug("stl_sc26198getsignals(portp=%p)\n", portp);
4038 spin_lock_irqsave(&brd_lock, flags);
4039 BRDENABLE(portp->brdnr, portp->pagenr);
4040 ipr = stl_sc26198getreg(portp, IPR);
4041 BRDDISABLE(portp->brdnr);
4042 spin_unlock_irqrestore(&brd_lock, flags);
4045 sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
4046 sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
4047 sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
4048 sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
4053 /*****************************************************************************/
4056 * Enable/Disable the Transmitter and/or Receiver.
4059 static void stl_sc26198enablerxtx(struct stlport *portp, int rx, int tx)
4062 unsigned long flags;
4064 pr_debug("stl_sc26198enablerxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx,tx);
4066 ccr = portp->crenable;
4068 ccr &= ~CR_TXENABLE;
4072 ccr &= ~CR_RXENABLE;
4076 spin_lock_irqsave(&brd_lock, flags);
4077 BRDENABLE(portp->brdnr, portp->pagenr);
4078 stl_sc26198setreg(portp, SCCR, ccr);
4079 BRDDISABLE(portp->brdnr);
4080 portp->crenable = ccr;
4081 spin_unlock_irqrestore(&brd_lock, flags);
4084 /*****************************************************************************/
4087 * Start/stop the Transmitter and/or Receiver.
4090 static void stl_sc26198startrxtx(struct stlport *portp, int rx, int tx)
4093 unsigned long flags;
4095 pr_debug("stl_sc26198startrxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
4103 imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
4105 imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
4107 spin_lock_irqsave(&brd_lock, flags);
4108 BRDENABLE(portp->brdnr, portp->pagenr);
4109 stl_sc26198setreg(portp, IMR, imr);
4110 BRDDISABLE(portp->brdnr);
4113 set_bit(ASYI_TXBUSY, &portp->istate);
4114 spin_unlock_irqrestore(&brd_lock, flags);
4117 /*****************************************************************************/
4120 * Disable all interrupts from this port.
4123 static void stl_sc26198disableintrs(struct stlport *portp)
4125 unsigned long flags;
4127 pr_debug("stl_sc26198disableintrs(portp=%p)\n", portp);
4129 spin_lock_irqsave(&brd_lock, flags);
4130 BRDENABLE(portp->brdnr, portp->pagenr);
4132 stl_sc26198setreg(portp, IMR, 0);
4133 BRDDISABLE(portp->brdnr);
4134 spin_unlock_irqrestore(&brd_lock, flags);
4137 /*****************************************************************************/
4139 static void stl_sc26198sendbreak(struct stlport *portp, int len)
4141 unsigned long flags;
4143 pr_debug("stl_sc26198sendbreak(portp=%p,len=%d)\n", portp, len);
4145 spin_lock_irqsave(&brd_lock, flags);
4146 BRDENABLE(portp->brdnr, portp->pagenr);
4148 stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
4149 portp->stats.txbreaks++;
4151 stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
4153 BRDDISABLE(portp->brdnr);
4154 spin_unlock_irqrestore(&brd_lock, flags);
4157 /*****************************************************************************/
4160 * Take flow control actions...
4163 static void stl_sc26198flowctrl(struct stlport *portp, int state)
4165 struct tty_struct *tty;
4166 unsigned long flags;
4169 pr_debug("stl_sc26198flowctrl(portp=%p,state=%x)\n", portp, state);
4173 tty = tty_port_tty_get(&portp->port);
4177 spin_lock_irqsave(&brd_lock, flags);
4178 BRDENABLE(portp->brdnr, portp->pagenr);
4181 if (tty->termios->c_iflag & IXOFF) {
4182 mr0 = stl_sc26198getreg(portp, MR0);
4183 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4184 stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4186 portp->stats.rxxon++;
4187 stl_sc26198wait(portp);
4188 stl_sc26198setreg(portp, MR0, mr0);
4191 * Question: should we return RTS to what it was before? It may
4192 * have been set by an ioctl... Suppose not, since if you have
4193 * hardware flow control set then it is pretty silly to go and
4194 * set the RTS line by hand.
4196 if (tty->termios->c_cflag & CRTSCTS) {
4197 stl_sc26198setreg(portp, MR1,
4198 (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4199 stl_sc26198setreg(portp, IOPIOR,
4200 (stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
4201 portp->stats.rxrtson++;
4204 if (tty->termios->c_iflag & IXOFF) {
4205 mr0 = stl_sc26198getreg(portp, MR0);
4206 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4207 stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4209 portp->stats.rxxoff++;
4210 stl_sc26198wait(portp);
4211 stl_sc26198setreg(portp, MR0, mr0);
4213 if (tty->termios->c_cflag & CRTSCTS) {
4214 stl_sc26198setreg(portp, MR1,
4215 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4216 stl_sc26198setreg(portp, IOPIOR,
4217 (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4218 portp->stats.rxrtsoff++;
4222 BRDDISABLE(portp->brdnr);
4223 spin_unlock_irqrestore(&brd_lock, flags);
4227 /*****************************************************************************/
4230 * Send a flow control character.
4233 static void stl_sc26198sendflow(struct stlport *portp, int state)
4235 struct tty_struct *tty;
4236 unsigned long flags;
4239 pr_debug("stl_sc26198sendflow(portp=%p,state=%x)\n", portp, state);
4243 tty = tty_port_tty_get(&portp->port);
4247 spin_lock_irqsave(&brd_lock, flags);
4248 BRDENABLE(portp->brdnr, portp->pagenr);
4250 mr0 = stl_sc26198getreg(portp, MR0);
4251 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4252 stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4254 portp->stats.rxxon++;
4255 stl_sc26198wait(portp);
4256 stl_sc26198setreg(portp, MR0, mr0);
4258 mr0 = stl_sc26198getreg(portp, MR0);
4259 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4260 stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4262 portp->stats.rxxoff++;
4263 stl_sc26198wait(portp);
4264 stl_sc26198setreg(portp, MR0, mr0);
4266 BRDDISABLE(portp->brdnr);
4267 spin_unlock_irqrestore(&brd_lock, flags);
4271 /*****************************************************************************/
4273 static void stl_sc26198flush(struct stlport *portp)
4275 unsigned long flags;
4277 pr_debug("stl_sc26198flush(portp=%p)\n", portp);
4282 spin_lock_irqsave(&brd_lock, flags);
4283 BRDENABLE(portp->brdnr, portp->pagenr);
4284 stl_sc26198setreg(portp, SCCR, CR_TXRESET);
4285 stl_sc26198setreg(portp, SCCR, portp->crenable);
4286 BRDDISABLE(portp->brdnr);
4287 portp->tx.tail = portp->tx.head;
4288 spin_unlock_irqrestore(&brd_lock, flags);
4291 /*****************************************************************************/
4294 * Return the current state of data flow on this port. This is only
4295 * really interresting when determining if data has fully completed
4296 * transmission or not... The sc26198 interrupt scheme cannot
4297 * determine when all data has actually drained, so we need to
4298 * check the port statusy register to be sure.
4301 static int stl_sc26198datastate(struct stlport *portp)
4303 unsigned long flags;
4306 pr_debug("stl_sc26198datastate(portp=%p)\n", portp);
4310 if (test_bit(ASYI_TXBUSY, &portp->istate))
4313 spin_lock_irqsave(&brd_lock, flags);
4314 BRDENABLE(portp->brdnr, portp->pagenr);
4315 sr = stl_sc26198getreg(portp, SR);
4316 BRDDISABLE(portp->brdnr);
4317 spin_unlock_irqrestore(&brd_lock, flags);
4319 return (sr & SR_TXEMPTY) ? 0 : 1;
4322 /*****************************************************************************/
4325 * Delay for a small amount of time, to give the sc26198 a chance
4326 * to process a command...
4329 static void stl_sc26198wait(struct stlport *portp)
4333 pr_debug("stl_sc26198wait(portp=%p)\n", portp);
4338 for (i = 0; i < 20; i++)
4339 stl_sc26198getglobreg(portp, TSTR);
4342 /*****************************************************************************/
4345 * If we are TX flow controlled and in IXANY mode then we may
4346 * need to unflow control here. We gotta do this because of the
4347 * automatic flow control modes of the sc26198.
4350 static void stl_sc26198txunflow(struct stlport *portp, struct tty_struct *tty)
4354 mr0 = stl_sc26198getreg(portp, MR0);
4355 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4356 stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
4357 stl_sc26198wait(portp);
4358 stl_sc26198setreg(portp, MR0, mr0);
4359 clear_bit(ASYI_TXFLOWED, &portp->istate);
4362 /*****************************************************************************/
4365 * Interrupt service routine for sc26198 panels.
4368 static void stl_sc26198intr(struct stlpanel *panelp, unsigned int iobase)
4370 struct stlport *portp;
4373 spin_lock(&brd_lock);
4376 * Work around bug in sc26198 chip... Cannot have A6 address
4377 * line of UART high, else iack will be returned as 0.
4379 outb(0, (iobase + 1));
4381 iack = inb(iobase + XP_IACK);
4382 portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
4384 if (iack & IVR_RXDATA)
4385 stl_sc26198rxisr(portp, iack);
4386 else if (iack & IVR_TXDATA)
4387 stl_sc26198txisr(portp);
4389 stl_sc26198otherisr(portp, iack);
4391 spin_unlock(&brd_lock);
4394 /*****************************************************************************/
4397 * Transmit interrupt handler. This has gotta be fast! Handling TX
4398 * chars is pretty simple, stuff as many as possible from the TX buffer
4399 * into the sc26198 FIFO.
4400 * In practice it is possible that interrupts are enabled but that the
4401 * port has been hung up. Need to handle not having any TX buffer here,
4402 * this is done by using the side effect that head and tail will also
4403 * be NULL if the buffer has been freed.
4406 static void stl_sc26198txisr(struct stlport *portp)
4408 struct tty_struct *tty;
4409 unsigned int ioaddr;
4414 pr_debug("stl_sc26198txisr(portp=%p)\n", portp);
4416 ioaddr = portp->ioaddr;
4417 head = portp->tx.head;
4418 tail = portp->tx.tail;
4419 len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
4420 if ((len == 0) || ((len < STL_TXBUFLOW) &&
4421 (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
4422 set_bit(ASYI_TXLOW, &portp->istate);
4423 tty = tty_port_tty_get(&portp->port);
4431 outb((MR0 | portp->uartaddr), (ioaddr + XP_ADDR));
4432 mr0 = inb(ioaddr + XP_DATA);
4433 if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
4434 portp->imr &= ~IR_TXRDY;
4435 outb((IMR | portp->uartaddr), (ioaddr + XP_ADDR));
4436 outb(portp->imr, (ioaddr + XP_DATA));
4437 clear_bit(ASYI_TXBUSY, &portp->istate);
4439 mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
4440 outb(mr0, (ioaddr + XP_DATA));
4443 len = min(len, SC26198_TXFIFOSIZE);
4444 portp->stats.txtotal += len;
4445 stlen = min_t(unsigned int, len,
4446 (portp->tx.buf + STL_TXBUFSIZE) - tail);
4447 outb(GTXFIFO, (ioaddr + XP_ADDR));
4448 outsb((ioaddr + XP_DATA), tail, stlen);
4451 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
4452 tail = portp->tx.buf;
4454 outsb((ioaddr + XP_DATA), tail, len);
4457 portp->tx.tail = tail;
4461 /*****************************************************************************/
4464 * Receive character interrupt handler. Determine if we have good chars
4465 * or bad chars and then process appropriately. Good chars are easy
4466 * just shove the lot into the RX buffer and set all status byte to 0.
4467 * If a bad RX char then process as required. This routine needs to be
4468 * fast! In practice it is possible that we get an interrupt on a port
4469 * that is closed. This can happen on hangups - since they completely
4470 * shutdown a port not in user context. Need to handle this case.
4473 static void stl_sc26198rxisr(struct stlport *portp, unsigned int iack)
4475 struct tty_struct *tty;
4476 unsigned int len, buflen, ioaddr;
4478 pr_debug("stl_sc26198rxisr(portp=%p,iack=%x)\n", portp, iack);
4480 tty = tty_port_tty_get(&portp->port);
4481 ioaddr = portp->ioaddr;
4482 outb(GIBCR, (ioaddr + XP_ADDR));
4483 len = inb(ioaddr + XP_DATA) + 1;
4485 if ((iack & IVR_TYPEMASK) == IVR_RXDATA) {
4486 if (tty == NULL || (buflen = tty_buffer_request_room(tty, len)) == 0) {
4487 len = min_t(unsigned int, len, sizeof(stl_unwanted));
4488 outb(GRXFIFO, (ioaddr + XP_ADDR));
4489 insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
4490 portp->stats.rxlost += len;
4491 portp->stats.rxtotal += len;
4493 len = min(len, buflen);
4496 outb(GRXFIFO, (ioaddr + XP_ADDR));
4497 tty_prepare_flip_string(tty, &ptr, len);
4498 insb((ioaddr + XP_DATA), ptr, len);
4499 tty_schedule_flip(tty);
4500 portp->stats.rxtotal += len;
4504 stl_sc26198rxbadchars(portp);
4508 * If we are TX flow controlled and in IXANY mode then we may need
4509 * to unflow control here. We gotta do this because of the automatic
4510 * flow control modes of the sc26198.
4512 if (test_bit(ASYI_TXFLOWED, &portp->istate)) {
4513 if ((tty != NULL) &&
4514 (tty->termios != NULL) &&
4515 (tty->termios->c_iflag & IXANY)) {
4516 stl_sc26198txunflow(portp, tty);
4522 /*****************************************************************************/
4525 * Process an RX bad character.
4528 static void stl_sc26198rxbadch(struct stlport *portp, unsigned char status, char ch)
4530 struct tty_struct *tty;
4531 unsigned int ioaddr;
4533 tty = tty_port_tty_get(&portp->port);
4534 ioaddr = portp->ioaddr;
4536 if (status & SR_RXPARITY)
4537 portp->stats.rxparity++;
4538 if (status & SR_RXFRAMING)
4539 portp->stats.rxframing++;
4540 if (status & SR_RXOVERRUN)
4541 portp->stats.rxoverrun++;
4542 if (status & SR_RXBREAK)
4543 portp->stats.rxbreaks++;
4545 if ((tty != NULL) &&
4546 ((portp->rxignoremsk & status) == 0)) {
4547 if (portp->rxmarkmsk & status) {
4548 if (status & SR_RXBREAK) {
4550 if (portp->port.flags & ASYNC_SAK) {
4552 BRDENABLE(portp->brdnr, portp->pagenr);
4554 } else if (status & SR_RXPARITY)
4555 status = TTY_PARITY;
4556 else if (status & SR_RXFRAMING)
4558 else if(status & SR_RXOVERRUN)
4559 status = TTY_OVERRUN;
4565 tty_insert_flip_char(tty, ch, status);
4566 tty_schedule_flip(tty);
4569 portp->stats.rxtotal++;
4574 /*****************************************************************************/
4577 * Process all characters in the RX FIFO of the UART. Check all char
4578 * status bytes as well, and process as required. We need to check
4579 * all bytes in the FIFO, in case some more enter the FIFO while we
4580 * are here. To get the exact character error type we need to switch
4581 * into CHAR error mode (that is why we need to make sure we empty
4585 static void stl_sc26198rxbadchars(struct stlport *portp)
4587 unsigned char status, mr1;
4591 * To get the precise error type for each character we must switch
4592 * back into CHAR error mode.
4594 mr1 = stl_sc26198getreg(portp, MR1);
4595 stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
4597 while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
4598 stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
4599 ch = stl_sc26198getreg(portp, RXFIFO);
4600 stl_sc26198rxbadch(portp, status, ch);
4604 * To get correct interrupt class we must switch back into BLOCK
4607 stl_sc26198setreg(portp, MR1, mr1);
4610 /*****************************************************************************/
4613 * Other interrupt handler. This includes modem signals, flow
4614 * control actions, etc. Most stuff is left to off-level interrupt
4618 static void stl_sc26198otherisr(struct stlport *portp, unsigned int iack)
4620 unsigned char cir, ipr, xisr;
4622 pr_debug("stl_sc26198otherisr(portp=%p,iack=%x)\n", portp, iack);
4624 cir = stl_sc26198getglobreg(portp, CIR);
4626 switch (cir & CIR_SUBTYPEMASK) {
4628 ipr = stl_sc26198getreg(portp, IPR);
4629 if (ipr & IPR_DCDCHANGE) {
4630 stl_cd_change(portp);
4631 portp->stats.modem++;
4634 case CIR_SUBXONXOFF:
4635 xisr = stl_sc26198getreg(portp, XISR);
4636 if (xisr & XISR_RXXONGOT) {
4637 set_bit(ASYI_TXFLOWED, &portp->istate);
4638 portp->stats.txxoff++;
4640 if (xisr & XISR_RXXOFFGOT) {
4641 clear_bit(ASYI_TXFLOWED, &portp->istate);
4642 portp->stats.txxon++;
4646 stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
4647 stl_sc26198rxbadchars(portp);
4654 static void stl_free_isabrds(void)
4656 struct stlbrd *brdp;
4659 for (i = 0; i < stl_nrbrds; i++) {
4660 if ((brdp = stl_brds[i]) == NULL || (brdp->state & STL_PROBED))
4663 free_irq(brdp->irq, brdp);
4665 stl_cleanup_panels(brdp);
4667 release_region(brdp->ioaddr1, brdp->iosize1);
4668 if (brdp->iosize2 > 0)
4669 release_region(brdp->ioaddr2, brdp->iosize2);
4677 * Loadable module initialization stuff.
4679 static int __init stallion_module_init(void)
4681 struct stlbrd *brdp;
4682 struct stlconf conf;
4686 printk(KERN_INFO "%s: version %s\n", stl_drvtitle, stl_drvversion);
4688 spin_lock_init(&stallion_lock);
4689 spin_lock_init(&brd_lock);
4691 stl_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
4697 stl_serial->owner = THIS_MODULE;
4698 stl_serial->driver_name = stl_drvname;
4699 stl_serial->name = "ttyE";
4700 stl_serial->major = STL_SERIALMAJOR;
4701 stl_serial->minor_start = 0;
4702 stl_serial->type = TTY_DRIVER_TYPE_SERIAL;
4703 stl_serial->subtype = SERIAL_TYPE_NORMAL;
4704 stl_serial->init_termios = stl_deftermios;
4705 stl_serial->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4706 tty_set_operations(stl_serial, &stl_ops);
4708 retval = tty_register_driver(stl_serial);
4710 printk("STALLION: failed to register serial driver\n");
4715 * Find any dynamically supported boards. That is via module load
4718 for (i = stl_nrbrds; i < stl_nargs; i++) {
4719 memset(&conf, 0, sizeof(conf));
4720 if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
4722 if ((brdp = stl_allocbrd()) == NULL)
4725 brdp->brdtype = conf.brdtype;
4726 brdp->ioaddr1 = conf.ioaddr1;
4727 brdp->ioaddr2 = conf.ioaddr2;
4728 brdp->irq = conf.irq;
4729 brdp->irqtype = conf.irqtype;
4730 stl_brds[brdp->brdnr] = brdp;
4731 if (stl_brdinit(brdp)) {
4732 stl_brds[brdp->brdnr] = NULL;
4735 for (j = 0; j < brdp->nrports; j++)
4736 tty_register_device(stl_serial,
4737 brdp->brdnr * STL_MAXPORTS + j, NULL);
4742 /* this has to be _after_ isa finding because of locking */
4743 retval = pci_register_driver(&stl_pcidriver);
4744 if (retval && stl_nrbrds == 0) {
4745 printk(KERN_ERR "STALLION: can't register pci driver\n");
4750 * Set up a character driver for per board stuff. This is mainly used
4751 * to do stats ioctls on the ports.
4753 if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stl_fsiomem))
4754 printk("STALLION: failed to register serial board device\n");
4756 stallion_class = class_create(THIS_MODULE, "staliomem");
4757 if (IS_ERR(stallion_class))
4758 printk("STALLION: failed to create class\n");
4759 for (i = 0; i < 4; i++)
4760 device_create(stallion_class, NULL, MKDEV(STL_SIOMEMMAJOR, i),
4761 NULL, "staliomem%d", i);
4765 tty_unregister_driver(stl_serial);
4767 put_tty_driver(stl_serial);
4772 static void __exit stallion_module_exit(void)
4774 struct stlbrd *brdp;
4777 pr_debug("cleanup_module()\n");
4779 printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
4783 * Free up all allocated resources used by the ports. This includes
4784 * memory and interrupts. As part of this process we will also do
4785 * a hangup on every open port - to try to flush out any processes
4786 * hanging onto ports.
4788 for (i = 0; i < stl_nrbrds; i++) {
4789 if ((brdp = stl_brds[i]) == NULL || (brdp->state & STL_PROBED))
4791 for (j = 0; j < brdp->nrports; j++)
4792 tty_unregister_device(stl_serial,
4793 brdp->brdnr * STL_MAXPORTS + j);
4796 for (i = 0; i < 4; i++)
4797 device_destroy(stallion_class, MKDEV(STL_SIOMEMMAJOR, i));
4798 unregister_chrdev(STL_SIOMEMMAJOR, "staliomem");
4799 class_destroy(stallion_class);
4801 pci_unregister_driver(&stl_pcidriver);
4805 tty_unregister_driver(stl_serial);
4806 put_tty_driver(stl_serial);
4809 module_init(stallion_module_init);
4810 module_exit(stallion_module_exit);
4812 MODULE_AUTHOR("Greg Ungerer");
4813 MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
4814 MODULE_LICENSE("GPL");