/*****************************************************************************/ /* * istallion.c -- stallion intelligent multiport serial driver. * * Copyright (C) 1996-1999 Stallion Technologies * Copyright (C) 1994-1996 Greg Ungerer. * * This code is loosely based on the Linux serial driver, written by * Linus Torvalds, Theodore T'so and others. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * */ /*****************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /*****************************************************************************/ /* * Define different board types. Not all of the following board types * are supported by this driver. But I will use the standard "assigned" * board numbers. Currently supported boards are abbreviated as: * ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and * STAL = Stallion. */ #define BRD_UNKNOWN 0 #define BRD_STALLION 1 #define BRD_BRUMBY4 2 #define BRD_ONBOARD2 3 #define BRD_ONBOARD 4 #define BRD_ONBOARDE 7 #define BRD_ECP 23 #define BRD_ECPE 24 #define BRD_ECPMC 25 #define BRD_ECPPCI 29 #define BRD_BRUMBY BRD_BRUMBY4 /* * Define a configuration structure to hold the board configuration. * Need to set this up in the code (for now) with the boards that are * to be configured into the system. This is what needs to be modified * when adding/removing/modifying boards. Each line entry in the * stli_brdconf[] array is a board. Each line contains io/irq/memory * ranges for that board (as well as what type of board it is). * Some examples: * { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 }, * This line will configure an EasyConnection 8/64 at io address 2a0, * and shared memory address of cc000. Multiple EasyConnection 8/64 * boards can share the same shared memory address space. No interrupt * is required for this board type. * Another example: * { BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 }, * This line will configure an EasyConnection 8/64 EISA in slot 5 and * shared memory address of 0x80000000 (2 GByte). Multiple * EasyConnection 8/64 EISA boards can share the same shared memory * address space. No interrupt is required for this board type. * Another example: * { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 }, * This line will configure an ONboard (ISA type) at io address 240, * and shared memory address of d0000. Multiple ONboards can share * the same shared memory address space. No interrupt required. * Another example: * { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 }, * This line will configure a Brumby board (any number of ports!) at * io address 360 and shared memory address of c8000. All Brumby boards * configured into a system must have their own separate io and memory * addresses. No interrupt is required. * Another example: * { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 }, * This line will configure an original Stallion board at io address 330 * and shared memory address d0000 (this would only be valid for a "V4.0" * or Rev.O Stallion board). All Stallion boards configured into the * system must have their own separate io and memory addresses. No * interrupt is required. */ struct stlconf { int brdtype; int ioaddr1; int ioaddr2; unsigned long memaddr; int irq; int irqtype; }; static unsigned int stli_nrbrds; /* stli_lock must NOT be taken holding brd_lock */ static spinlock_t stli_lock; /* TTY logic lock */ static spinlock_t brd_lock; /* Board logic lock */ /* * There is some experimental EISA board detection code in this driver. * By default it is disabled, but for those that want to try it out, * then set the define below to be 1. */ #define STLI_EISAPROBE 0 /*****************************************************************************/ /* * Define some important driver characteristics. Device major numbers * allocated as per Linux Device Registry. */ #ifndef STL_SIOMEMMAJOR #define STL_SIOMEMMAJOR 28 #endif #ifndef STL_SERIALMAJOR #define STL_SERIALMAJOR 24 #endif #ifndef STL_CALLOUTMAJOR #define STL_CALLOUTMAJOR 25 #endif /*****************************************************************************/ /* * Define our local driver identity first. Set up stuff to deal with * all the local structures required by a serial tty driver. */ static char *stli_drvtitle = "Stallion Intelligent Multiport Serial Driver"; static char *stli_drvname = "istallion"; static char *stli_drvversion = "5.6.0"; static char *stli_serialname = "ttyE"; static struct tty_driver *stli_serial; #define STLI_TXBUFSIZE 4096 /* * Use a fast local buffer for cooked characters. Typically a whole * bunch of cooked characters come in for a port, 1 at a time. So we * save those up into a local buffer, then write out the whole lot * with a large memcpy. Just use 1 buffer for all ports, since its * use it is only need for short periods of time by each port. */ static char *stli_txcookbuf; static int stli_txcooksize; static int stli_txcookrealsize; static struct tty_struct *stli_txcooktty; /* * Define a local default termios struct. All ports will be created * with this termios initially. Basically all it defines is a raw port * at 9600 baud, 8 data bits, no parity, 1 stop bit. */ static struct ktermios stli_deftermios = { .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL), .c_cc = INIT_C_CC, .c_ispeed = 9600, .c_ospeed = 9600, }; /* * Define global stats structures. Not used often, and can be * re-used for each stats call. */ static comstats_t stli_comstats; static combrd_t stli_brdstats; static struct asystats stli_cdkstats; /*****************************************************************************/ static DEFINE_MUTEX(stli_brdslock); static struct stlibrd *stli_brds[STL_MAXBRDS]; static int stli_shared; /* * Per board state flags. Used with the state field of the board struct. * Not really much here... All we need to do is keep track of whether * the board has been detected, and whether it is actually running a slave * or not. */ #define BST_FOUND 0x1 #define BST_STARTED 0x2 #define BST_PROBED 0x4 /* * Define the set of port state flags. These are marked for internal * state purposes only, usually to do with the state of communications * with the slave. Most of them need to be updated atomically, so always * use the bit setting operations (unless protected by cli/sti). */ #define ST_INITIALIZING 1 #define ST_OPENING 2 #define ST_CLOSING 3 #define ST_CMDING 4 #define ST_TXBUSY 5 #define ST_RXING 6 #define ST_DOFLUSHRX 7 #define ST_DOFLUSHTX 8 #define ST_DOSIGS 9 #define ST_RXSTOP 10 #define ST_GETSIGS 11 /* * Define an array of board names as printable strings. Handy for * referencing boards when printing trace and stuff. */ static char *stli_brdnames[] = { "Unknown", "Stallion", "Brumby", "ONboard-MC", "ONboard", "Brumby", "Brumby", "ONboard-EI", NULL, "ONboard", "ONboard-MC", "ONboard-MC", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "EasyIO", "EC8/32-AT", "EC8/32-MC", "EC8/64-AT", "EC8/64-EI", "EC8/64-MC", "EC8/32-PCI", "EC8/64-PCI", "EasyIO-PCI", "EC/RA-PCI", }; /*****************************************************************************/ /* * Define some string labels for arguments passed from the module * load line. These allow for easy board definitions, and easy * modification of the io, memory and irq resoucres. */ static char *board0[8]; static char *board1[8]; static char *board2[8]; static char *board3[8]; static char **stli_brdsp[] = { (char **) &board0, (char **) &board1, (char **) &board2, (char **) &board3 }; /* * Define a set of common board names, and types. This is used to * parse any module arguments. */ static struct stlibrdtype { char *name; int type; } stli_brdstr[] = { { "stallion", BRD_STALLION }, { "1", BRD_STALLION }, { "brumby", BRD_BRUMBY }, { "brumby4", BRD_BRUMBY }, { "brumby/4", BRD_BRUMBY }, { "brumby-4", BRD_BRUMBY }, { "brumby8", BRD_BRUMBY }, { "brumby/8", BRD_BRUMBY }, { "brumby-8", BRD_BRUMBY }, { "brumby16", BRD_BRUMBY }, { "brumby/16", BRD_BRUMBY }, { "brumby-16", BRD_BRUMBY }, { "2", BRD_BRUMBY }, { "onboard2", BRD_ONBOARD2 }, { "onboard-2", BRD_ONBOARD2 }, { "onboard/2", BRD_ONBOARD2 }, { "onboard-mc", BRD_ONBOARD2 }, { "onboard/mc", BRD_ONBOARD2 }, { "onboard-mca", BRD_ONBOARD2 }, { "onboard/mca", BRD_ONBOARD2 }, { "3", BRD_ONBOARD2 }, { "onboard", BRD_ONBOARD }, { "onboardat", BRD_ONBOARD }, { "4", BRD_ONBOARD }, { "onboarde", BRD_ONBOARDE }, { "onboard-e", BRD_ONBOARDE }, { "onboard/e", BRD_ONBOARDE }, { "onboard-ei", BRD_ONBOARDE }, { "onboard/ei", BRD_ONBOARDE }, { "7", BRD_ONBOARDE }, { "ecp", BRD_ECP }, { "ecpat", BRD_ECP }, { "ec8/64", BRD_ECP }, { "ec8/64-at", BRD_ECP }, { "ec8/64-isa", BRD_ECP }, { "23", BRD_ECP }, { "ecpe", BRD_ECPE }, { "ecpei", BRD_ECPE }, { "ec8/64-e", BRD_ECPE }, { "ec8/64-ei", BRD_ECPE }, { "24", BRD_ECPE }, { "ecpmc", BRD_ECPMC }, { "ec8/64-mc", BRD_ECPMC }, { "ec8/64-mca", BRD_ECPMC }, { "25", BRD_ECPMC }, { "ecppci", BRD_ECPPCI }, { "ec/ra", BRD_ECPPCI }, { "ec/ra-pc", BRD_ECPPCI }, { "ec/ra-pci", BRD_ECPPCI }, { "29", BRD_ECPPCI }, }; /* * Define the module agruments. */ MODULE_AUTHOR("Greg Ungerer"); MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver"); MODULE_LICENSE("GPL"); module_param_array(board0, charp, NULL, 0); MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]"); module_param_array(board1, charp, NULL, 0); MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]"); module_param_array(board2, charp, NULL, 0); MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]"); module_param_array(board3, charp, NULL, 0); MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]"); #if STLI_EISAPROBE != 0 /* * Set up a default memory address table for EISA board probing. * The default addresses are all bellow 1Mbyte, which has to be the * case anyway. They should be safe, since we only read values from * them, and interrupts are disabled while we do it. If the higher * memory support is compiled in then we also try probing around * the 1Gb, 2Gb and 3Gb areas as well... */ static unsigned long stli_eisamemprobeaddrs[] = { 0xc0000, 0xd0000, 0xe0000, 0xf0000, 0x80000000, 0x80010000, 0x80020000, 0x80030000, 0x40000000, 0x40010000, 0x40020000, 0x40030000, 0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000, 0xff000000, 0xff010000, 0xff020000, 0xff030000, }; static int stli_eisamempsize = ARRAY_SIZE(stli_eisamemprobeaddrs); #endif /* * Define the Stallion PCI vendor and device IDs. */ #ifndef PCI_DEVICE_ID_ECRA #define PCI_DEVICE_ID_ECRA 0x0004 #endif static struct pci_device_id istallion_pci_tbl[] = { { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA), }, { 0 } }; MODULE_DEVICE_TABLE(pci, istallion_pci_tbl); static struct pci_driver stli_pcidriver; /*****************************************************************************/ /* * Hardware configuration info for ECP boards. These defines apply * to the directly accessible io ports of the ECP. There is a set of * defines for each ECP board type, ISA, EISA, MCA and PCI. */ #define ECP_IOSIZE 4 #define ECP_MEMSIZE (128 * 1024) #define ECP_PCIMEMSIZE (256 * 1024) #define ECP_ATPAGESIZE (4 * 1024) #define ECP_MCPAGESIZE (4 * 1024) #define ECP_EIPAGESIZE (64 * 1024) #define ECP_PCIPAGESIZE (64 * 1024) #define STL_EISAID 0x8c4e /* * Important defines for the ISA class of ECP board. */ #define ECP_ATIREG 0 #define ECP_ATCONFR 1 #define ECP_ATMEMAR 2 #define ECP_ATMEMPR 3 #define ECP_ATSTOP 0x1 #define ECP_ATINTENAB 0x10 #define ECP_ATENABLE 0x20 #define ECP_ATDISABLE 0x00 #define ECP_ATADDRMASK 0x3f000 #define ECP_ATADDRSHFT 12 /* * Important defines for the EISA class of ECP board. */ #define ECP_EIIREG 0 #define ECP_EIMEMARL 1 #define ECP_EICONFR 2 #define ECP_EIMEMARH 3 #define ECP_EIENABLE 0x1 #define ECP_EIDISABLE 0x0 #define ECP_EISTOP 0x4 #define ECP_EIEDGE 0x00 #define ECP_EILEVEL 0x80 #define ECP_EIADDRMASKL 0x00ff0000 #define ECP_EIADDRSHFTL 16 #define ECP_EIADDRMASKH 0xff000000 #define ECP_EIADDRSHFTH 24 #define ECP_EIBRDENAB 0xc84 #define ECP_EISAID 0x4 /* * Important defines for the Micro-channel class of ECP board. * (It has a lot in common with the ISA boards.) */ #define ECP_MCIREG 0 #define ECP_MCCONFR 1 #define ECP_MCSTOP 0x20 #define ECP_MCENABLE 0x80 #define ECP_MCDISABLE 0x00 /* * Important defines for the PCI class of ECP board. * (It has a lot in common with the other ECP boards.) */ #define ECP_PCIIREG 0 #define ECP_PCICONFR 1 #define ECP_PCISTOP 0x01 /* * Hardware configuration info for ONboard and Brumby boards. These * defines apply to the directly accessible io ports of these boards. */ #define ONB_IOSIZE 16 #define ONB_MEMSIZE (64 * 1024) #define ONB_ATPAGESIZE (64 * 1024) #define ONB_MCPAGESIZE (64 * 1024) #define ONB_EIMEMSIZE (128 * 1024) #define ONB_EIPAGESIZE (64 * 1024) /* * Important defines for the ISA class of ONboard board. */ #define ONB_ATIREG 0 #define ONB_ATMEMAR 1 #define ONB_ATCONFR 2 #define ONB_ATSTOP 0x4 #define ONB_ATENABLE 0x01 #define ONB_ATDISABLE 0x00 #define ONB_ATADDRMASK 0xff0000 #define ONB_ATADDRSHFT 16 #define ONB_MEMENABLO 0 #define ONB_MEMENABHI 0x02 /* * Important defines for the EISA class of ONboard board. */ #define ONB_EIIREG 0 #define ONB_EIMEMARL 1 #define ONB_EICONFR 2 #define ONB_EIMEMARH 3 #define ONB_EIENABLE 0x1 #define ONB_EIDISABLE 0x0 #define ONB_EISTOP 0x4 #define ONB_EIEDGE 0x00 #define ONB_EILEVEL 0x80 #define ONB_EIADDRMASKL 0x00ff0000 #define ONB_EIADDRSHFTL 16 #define ONB_EIADDRMASKH 0xff000000 #define ONB_EIADDRSHFTH 24 #define ONB_EIBRDENAB 0xc84 #define ONB_EISAID 0x1 /* * Important defines for the Brumby boards. They are pretty simple, * there is not much that is programmably configurable. */ #define BBY_IOSIZE 16 #define BBY_MEMSIZE (64 * 1024) #define BBY_PAGESIZE (16 * 1024) #define BBY_ATIREG 0 #define BBY_ATCONFR 1 #define BBY_ATSTOP 0x4 /* * Important defines for the Stallion boards. They are pretty simple, * there is not much that is programmably configurable. */ #define STAL_IOSIZE 16 #define STAL_MEMSIZE (64 * 1024) #define STAL_PAGESIZE (64 * 1024) /* * Define the set of status register values for EasyConnection panels. * The signature will return with the status value for each panel. From * this we can determine what is attached to the board - before we have * actually down loaded any code to it. */ #define ECH_PNLSTATUS 2 #define ECH_PNL16PORT 0x20 #define ECH_PNLIDMASK 0x07 #define ECH_PNLXPID 0x40 #define ECH_PNLINTRPEND 0x80 /* * Define some macros to do things to the board. Even those these boards * are somewhat related there is often significantly different ways of * doing some operation on it (like enable, paging, reset, etc). So each * board class has a set of functions which do the commonly required * operations. The macros below basically just call these functions, * generally checking for a NULL function - which means that the board * needs nothing done to it to achieve this operation! */ #define EBRDINIT(brdp) \ if (brdp->init != NULL) \ (* brdp->init)(brdp) #define EBRDENABLE(brdp) \ if (brdp->enable != NULL) \ (* brdp->enable)(brdp); #define EBRDDISABLE(brdp) \ if (brdp->disable != NULL) \ (* brdp->disable)(brdp); #define EBRDINTR(brdp) \ if (brdp->intr != NULL) \ (* brdp->intr)(brdp); #define EBRDRESET(brdp) \ if (brdp->reset != NULL) \ (* brdp->reset)(brdp); #define EBRDGETMEMPTR(brdp,offset) \ (* brdp->getmemptr)(brdp, offset, __LINE__) /* * Define the maximal baud rate, and the default baud base for ports. */ #define STL_MAXBAUD 460800 #define STL_BAUDBASE 115200 #define STL_CLOSEDELAY (5 * HZ / 10) /*****************************************************************************/ /* * Define macros to extract a brd or port number from a minor number. */ #define MINOR2BRD(min) (((min) & 0xc0) >> 6) #define MINOR2PORT(min) ((min) & 0x3f) /*****************************************************************************/ /* * Prototype all functions in this driver! */ static int stli_parsebrd(struct stlconf *confp, char **argp); static int stli_open(struct tty_struct *tty, struct file *filp); static void stli_close(struct tty_struct *tty, struct file *filp); static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count); static void stli_putchar(struct tty_struct *tty, unsigned char ch); static void stli_flushchars(struct tty_struct *tty); static int stli_writeroom(struct tty_struct *tty); static int stli_charsinbuffer(struct tty_struct *tty); static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg); static void stli_settermios(struct tty_struct *tty, struct ktermios *old); static void stli_throttle(struct tty_struct *tty); static void stli_unthrottle(struct tty_struct *tty); static void stli_stop(struct tty_struct *tty); static void stli_start(struct tty_struct *tty); static void stli_flushbuffer(struct tty_struct *tty); static void stli_breakctl(struct tty_struct *tty, int state); static void stli_waituntilsent(struct tty_struct *tty, int timeout); static void stli_sendxchar(struct tty_struct *tty, char ch); static void stli_hangup(struct tty_struct *tty); static int stli_portinfo(struct stlibrd *brdp, struct stliport *portp, int portnr, char *pos); static int stli_brdinit(struct stlibrd *brdp); static int stli_startbrd(struct stlibrd *brdp); static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp); static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp); static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg); static void stli_brdpoll(struct stlibrd *brdp, cdkhdr_t __iomem *hdrp); static void stli_poll(unsigned long arg); static int stli_hostcmd(struct stlibrd *brdp, struct stliport *portp); static int stli_initopen(struct stlibrd *brdp, struct stliport *portp); static int stli_rawopen(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait); static int stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait); static int stli_waitcarrier(struct stlibrd *brdp, struct stliport *portp, struct file *filp); static void stli_dohangup(struct work_struct *); static int stli_setport(struct stliport *portp); static int stli_cmdwait(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback); static void stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback); static void __stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback); static void stli_dodelaycmd(struct stliport *portp, cdkctrl_t __iomem *cp); static void stli_mkasyport(struct stliport *portp, asyport_t *pp, struct ktermios *tiosp); static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts); static long stli_mktiocm(unsigned long sigvalue); static void stli_read(struct stlibrd *brdp, struct stliport *portp); static int stli_getserial(struct stliport *portp, struct serial_struct __user *sp); static int stli_setserial(struct stliport *portp, struct serial_struct __user *sp); static int stli_getbrdstats(combrd_t __user *bp); static int stli_getportstats(struct stliport *portp, comstats_t __user *cp); static int stli_portcmdstats(struct stliport *portp); static int stli_clrportstats(struct stliport *portp, comstats_t __user *cp); static int stli_getportstruct(struct stliport __user *arg); static int stli_getbrdstruct(struct stlibrd __user *arg); static struct stlibrd *stli_allocbrd(void); static void stli_ecpinit(struct stlibrd *brdp); static void stli_ecpenable(struct stlibrd *brdp); static void stli_ecpdisable(struct stlibrd *brdp); static void __iomem *stli_ecpgetmemptr(struct stlibrd *brdp, unsigned long offset, int line); static void stli_ecpreset(struct stlibrd *brdp); static void stli_ecpintr(struct stlibrd *brdp); static void stli_ecpeiinit(struct stlibrd *brdp); static void stli_ecpeienable(struct stlibrd *brdp); static void stli_ecpeidisable(struct stlibrd *brdp); static void __iomem *stli_ecpeigetmemptr(struct stlibrd *brdp, unsigned long offset, int line); static void stli_ecpeireset(struct stlibrd *brdp); static void stli_ecpmcenable(struct stlibrd *brdp); static void stli_ecpmcdisable(struct stlibrd *brdp); static void __iomem *stli_ecpmcgetmemptr(struct stlibrd *brdp, unsigned long offset, int line); static void stli_ecpmcreset(struct stlibrd *brdp); static void stli_ecppciinit(struct stlibrd *brdp); static void __iomem *stli_ecppcigetmemptr(struct stlibrd *brdp, unsigned long offset, int line); static void stli_ecppcireset(struct stlibrd *brdp); static void stli_onbinit(struct stlibrd *brdp); static void stli_onbenable(struct stlibrd *brdp); static void stli_onbdisable(struct stlibrd *brdp); static void __iomem *stli_onbgetmemptr(struct stlibrd *brdp, unsigned long offset, int line); static void stli_onbreset(struct stlibrd *brdp); static void stli_onbeinit(struct stlibrd *brdp); static void stli_onbeenable(struct stlibrd *brdp); static void stli_onbedisable(struct stlibrd *brdp); static void __iomem *stli_onbegetmemptr(struct stlibrd *brdp, unsigned long offset, int line); static void stli_onbereset(struct stlibrd *brdp); static void stli_bbyinit(struct stlibrd *brdp); static void __iomem *stli_bbygetmemptr(struct stlibrd *brdp, unsigned long offset, int line); static void stli_bbyreset(struct stlibrd *brdp); static void stli_stalinit(struct stlibrd *brdp); static void __iomem *stli_stalgetmemptr(struct stlibrd *brdp, unsigned long offset, int line); static void stli_stalreset(struct stlibrd *brdp); static struct stliport *stli_getport(unsigned int brdnr, unsigned int panelnr, unsigned int portnr); static int stli_initecp(struct stlibrd *brdp); static int stli_initonb(struct stlibrd *brdp); #if STLI_EISAPROBE != 0 static int stli_eisamemprobe(struct stlibrd *brdp); #endif static int stli_initports(struct stlibrd *brdp); /*****************************************************************************/ /* * Define the driver info for a user level shared memory device. This * device will work sort of like the /dev/kmem device - except that it * will give access to the shared memory on the Stallion intelligent * board. This is also a very useful debugging tool. */ static const struct file_operations stli_fsiomem = { .owner = THIS_MODULE, .read = stli_memread, .write = stli_memwrite, .ioctl = stli_memioctl, }; /*****************************************************************************/ /* * Define a timer_list entry for our poll routine. The slave board * is polled every so often to see if anything needs doing. This is * much cheaper on host cpu than using interrupts. It turns out to * not increase character latency by much either... */ static DEFINE_TIMER(stli_timerlist, stli_poll, 0, 0); static int stli_timeron; /* * Define the calculation for the timeout routine. */ #define STLI_TIMEOUT (jiffies + 1) /*****************************************************************************/ static struct class *istallion_class; static void stli_cleanup_ports(struct stlibrd *brdp) { struct stliport *portp; unsigned int j; for (j = 0; j < STL_MAXPORTS; j++) { portp = brdp->ports[j]; if (portp != NULL) { if (portp->tty != NULL) tty_hangup(portp->tty); kfree(portp); } } } /*****************************************************************************/ /* * Parse the supplied argument string, into the board conf struct. */ static int stli_parsebrd(struct stlconf *confp, char **argp) { unsigned int i; char *sp; if (argp[0] == NULL || *argp[0] == 0) return 0; for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++) *sp = tolower(*sp); for (i = 0; i < ARRAY_SIZE(stli_brdstr); i++) { if (strcmp(stli_brdstr[i].name, argp[0]) == 0) break; } if (i == ARRAY_SIZE(stli_brdstr)) { printk("STALLION: unknown board name, %s?\n", argp[0]); return 0; } confp->brdtype = stli_brdstr[i].type; if (argp[1] != NULL && *argp[1] != 0) confp->ioaddr1 = simple_strtoul(argp[1], NULL, 0); if (argp[2] != NULL && *argp[2] != 0) confp->memaddr = simple_strtoul(argp[2], NULL, 0); return(1); } /*****************************************************************************/ static int stli_open(struct tty_struct *tty, struct file *filp) { struct stlibrd *brdp; struct stliport *portp; unsigned int minordev, brdnr, portnr; int rc; minordev = tty->index; brdnr = MINOR2BRD(minordev); if (brdnr >= stli_nrbrds) return -ENODEV; brdp = stli_brds[brdnr]; if (brdp == NULL) return -ENODEV; if ((brdp->state & BST_STARTED) == 0) return -ENODEV; portnr = MINOR2PORT(minordev); if (portnr > brdp->nrports) return -ENODEV; portp = brdp->ports[portnr]; if (portp == NULL) return -ENODEV; if (portp->devnr < 1) return -ENODEV; /* * Check if this port is in the middle of closing. If so then wait * until it is closed then return error status based on flag settings. * The sleep here does not need interrupt protection since the wakeup * for it is done with the same context. */ if (portp->flags & ASYNC_CLOSING) { interruptible_sleep_on(&portp->close_wait); if (portp->flags & ASYNC_HUP_NOTIFY) return -EAGAIN; return -ERESTARTSYS; } /* * On the first open of the device setup the port hardware, and * initialize the per port data structure. Since initializing the port * requires several commands to the board we will need to wait for any * other open that is already initializing the port. */ portp->tty = tty; tty->driver_data = portp; portp->refcount++; wait_event_interruptible(portp->raw_wait, !test_bit(ST_INITIALIZING, &portp->state)); if (signal_pending(current)) return -ERESTARTSYS; if ((portp->flags & ASYNC_INITIALIZED) == 0) { set_bit(ST_INITIALIZING, &portp->state); if ((rc = stli_initopen(brdp, portp)) >= 0) { portp->flags |= ASYNC_INITIALIZED; clear_bit(TTY_IO_ERROR, &tty->flags); } clear_bit(ST_INITIALIZING, &portp->state); wake_up_interruptible(&portp->raw_wait); if (rc < 0) return rc; } /* * Check if this port is in the middle of closing. If so then wait * until it is closed then return error status, based on flag settings. * The sleep here does not need interrupt protection since the wakeup * for it is done with the same context. */ if (portp->flags & ASYNC_CLOSING) { interruptible_sleep_on(&portp->close_wait); if (portp->flags & ASYNC_HUP_NOTIFY) return -EAGAIN; return -ERESTARTSYS; } /* * Based on type of open being done check if it can overlap with any * previous opens still in effect. If we are a normal serial device * then also we might have to wait for carrier. */ if (!(filp->f_flags & O_NONBLOCK)) { if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0) return rc; } portp->flags |= ASYNC_NORMAL_ACTIVE; return 0; } /*****************************************************************************/ static void stli_close(struct tty_struct *tty, struct file *filp) { struct stlibrd *brdp; struct stliport *portp; unsigned long flags; portp = tty->driver_data; if (portp == NULL) return; spin_lock_irqsave(&stli_lock, flags); if (tty_hung_up_p(filp)) { spin_unlock_irqrestore(&stli_lock, flags); return; } if ((tty->count == 1) && (portp->refcount != 1)) portp->refcount = 1; if (portp->refcount-- > 1) { spin_unlock_irqrestore(&stli_lock, flags); return; } portp->flags |= ASYNC_CLOSING; /* * May want to wait for data to drain before closing. The BUSY flag * keeps track of whether we are still transmitting or not. It is * updated by messages from the slave - indicating when all chars * really have drained. */ if (tty == stli_txcooktty) stli_flushchars(tty); tty->closing = 1; spin_unlock_irqrestore(&stli_lock, flags); if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE) tty_wait_until_sent(tty, portp->closing_wait); portp->flags &= ~ASYNC_INITIALIZED; brdp = stli_brds[portp->brdnr]; stli_rawclose(brdp, portp, 0, 0); if (tty->termios->c_cflag & HUPCL) { stli_mkasysigs(&portp->asig, 0, 0); if (test_bit(ST_CMDING, &portp->state)) set_bit(ST_DOSIGS, &portp->state); else stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0); } clear_bit(ST_TXBUSY, &portp->state); clear_bit(ST_RXSTOP, &portp->state); set_bit(TTY_IO_ERROR, &tty->flags); if (tty->ldisc.flush_buffer) (tty->ldisc.flush_buffer)(tty); set_bit(ST_DOFLUSHRX, &portp->state); stli_flushbuffer(tty); tty->closing = 0; portp->tty = NULL; if (portp->openwaitcnt) { if (portp->close_delay) msleep_interruptible(jiffies_to_msecs(portp->close_delay)); wake_up_interruptible(&portp->open_wait); } portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING); wake_up_interruptible(&portp->close_wait); } /*****************************************************************************/ /* * Carry out first open operations on a port. This involves a number of * commands to be sent to the slave. We need to open the port, set the * notification events, set the initial port settings, get and set the * initial signal values. We sleep and wait in between each one. But * this still all happens pretty quickly. */ static int stli_initopen(struct stlibrd *brdp, struct stliport *portp) { struct tty_struct *tty; asynotify_t nt; asyport_t aport; int rc; if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0) return rc; memset(&nt, 0, sizeof(asynotify_t)); nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK); nt.signal = SG_DCD; if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt, sizeof(asynotify_t), 0)) < 0) return rc; tty = portp->tty; if (tty == NULL) return -ENODEV; stli_mkasyport(portp, &aport, tty->termios); if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0)) < 0) return rc; set_bit(ST_GETSIGS, &portp->state); if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig, sizeof(asysigs_t), 1)) < 0) return rc; if (test_and_clear_bit(ST_GETSIGS, &portp->state)) portp->sigs = stli_mktiocm(portp->asig.sigvalue); stli_mkasysigs(&portp->asig, 1, 1); if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0)) < 0) return rc; return 0; } /*****************************************************************************/ /* * Send an open message to the slave. This will sleep waiting for the * acknowledgement, so must have user context. We need to co-ordinate * with close events here, since we don't want open and close events * to overlap. */ static int stli_rawopen(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait) { cdkhdr_t __iomem *hdrp; cdkctrl_t __iomem *cp; unsigned char __iomem *bits; unsigned long flags; int rc; /* * Send a message to the slave to open this port. */ /* * Slave is already closing this port. This can happen if a hangup * occurs on this port. So we must wait until it is complete. The * order of opens and closes may not be preserved across shared * memory, so we must wait until it is complete. */ wait_event_interruptible(portp->raw_wait, !test_bit(ST_CLOSING, &portp->state)); if (signal_pending(current)) { return -ERESTARTSYS; } /* * Everything is ready now, so write the open message into shared * memory. Once the message is in set the service bits to say that * this port wants service. */ spin_lock_irqsave(&brd_lock, flags); EBRDENABLE(brdp); cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl; writel(arg, &cp->openarg); writeb(1, &cp->open); hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR); bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset + portp->portidx; writeb(readb(bits) | portp->portbit, bits); EBRDDISABLE(brdp); if (wait == 0) { spin_unlock_irqrestore(&brd_lock, flags); return 0; } /* * Slave is in action, so now we must wait for the open acknowledgment * to come back. */ rc = 0; set_bit(ST_OPENING, &portp->state); spin_unlock_irqrestore(&brd_lock, flags); wait_event_interruptible(portp->raw_wait, !test_bit(ST_OPENING, &portp->state)); if (signal_pending(current)) rc = -ERESTARTSYS; if ((rc == 0) && (portp->rc != 0)) rc = -EIO; return rc; } /*****************************************************************************/ /* * Send a close message to the slave. Normally this will sleep waiting * for the acknowledgement, but if wait parameter is 0 it will not. If * wait is true then must have user context (to sleep). */ static int stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait) { cdkhdr_t __iomem *hdrp; cdkctrl_t __iomem *cp; unsigned char __iomem *bits; unsigned long flags; int rc; /* * Slave is already closing this port. This can happen if a hangup * occurs on this port. */ if (wait) { wait_event_interruptible(portp->raw_wait, !test_bit(ST_CLOSING, &portp->state)); if (signal_pending(current)) { return -ERESTARTSYS; } } /* * Write the close command into shared memory. */ spin_lock_irqsave(&brd_lock, flags); EBRDENABLE(brdp); cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl; writel(arg, &cp->closearg); writeb(1, &cp->close); hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR); bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset + portp->portidx; writeb(readb(bits) |portp->portbit, bits); EBRDDISABLE(brdp); set_bit(ST_CLOSING, &portp->state); spin_unlock_irqrestore(&brd_lock, flags); if (wait == 0) return 0; /* * Slave is in action, so now we must wait for the open acknowledgment * to come back. */ rc = 0; wait_event_interruptible(portp->raw_wait, !test_bit(ST_CLOSING, &portp->state)); if (signal_pending(current)) rc = -ERESTARTSYS; if ((rc == 0) && (portp->rc != 0)) rc = -EIO; return rc; } /*****************************************************************************/ /* * Send a command to the slave and wait for the response. This must * have user context (it sleeps). This routine is generic in that it * can send any type of command. Its purpose is to wait for that command * to complete (as opposed to initiating the command then returning). */ static int stli_cmdwait(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback) { wait_event_interruptible(portp->raw_wait, !test_bit(ST_CMDING, &portp->state)); if (signal_pending(current)) return -ERESTARTSYS; stli_sendcmd(brdp, portp, cmd, arg, size, copyback); wait_event_interruptible(portp->raw_wait, !test_bit(ST_CMDING, &portp->state)); if (signal_pending(current)) return -ERESTARTSYS; if (portp->rc != 0) return -EIO; return 0; } /*****************************************************************************/ /* * Send the termios settings for this port to the slave. This sleeps * waiting for the command to complete - so must have user context. */ static int stli_setport(struct stliport *portp) { struct stlibrd *brdp; asyport_t aport; if (portp == NULL) return -ENODEV; if (portp->tty == NULL) return -ENODEV; if (portp->brdnr >= stli_nrbrds) return -ENODEV; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return -ENODEV; stli_mkasyport(portp, &aport, portp->tty->termios); return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0)); } /*****************************************************************************/ /* * Possibly need to wait for carrier (DCD signal) to come high. Say * maybe because if we are clocal then we don't need to wait... */ static int stli_waitcarrier(struct stlibrd *brdp, struct stliport *portp, struct file *filp) { unsigned long flags; int rc, doclocal; rc = 0; doclocal = 0; if (portp->tty->termios->c_cflag & CLOCAL) doclocal++; spin_lock_irqsave(&stli_lock, flags); portp->openwaitcnt++; if (! tty_hung_up_p(filp)) portp->refcount--; spin_unlock_irqrestore(&stli_lock, flags); for (;;) { stli_mkasysigs(&portp->asig, 1, 1); if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0)) < 0) break; if (tty_hung_up_p(filp) || ((portp->flags & ASYNC_INITIALIZED) == 0)) { if (portp->flags & ASYNC_HUP_NOTIFY) rc = -EBUSY; else rc = -ERESTARTSYS; break; } if (((portp->flags & ASYNC_CLOSING) == 0) && (doclocal || (portp->sigs & TIOCM_CD))) { break; } if (signal_pending(current)) { rc = -ERESTARTSYS; break; } interruptible_sleep_on(&portp->open_wait); } spin_lock_irqsave(&stli_lock, flags); if (! tty_hung_up_p(filp)) portp->refcount++; portp->openwaitcnt--; spin_unlock_irqrestore(&stli_lock, flags); return rc; } /*****************************************************************************/ /* * Write routine. Take the data and put it in the shared memory ring * queue. If port is not already sending chars then need to mark the * service bits for this port. */ static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count) { cdkasy_t __iomem *ap; cdkhdr_t __iomem *hdrp; unsigned char __iomem *bits; unsigned char __iomem *shbuf; unsigned char *chbuf; struct stliport *portp; struct stlibrd *brdp; unsigned int len, stlen, head, tail, size; unsigned long flags; if (tty == stli_txcooktty) stli_flushchars(tty); portp = tty->driver_data; if (portp == NULL) return 0; if (portp->brdnr >= stli_nrbrds) return 0; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return 0; chbuf = (unsigned char *) buf; /* * All data is now local, shove as much as possible into shared memory. */ spin_lock_irqsave(&brd_lock, flags); EBRDENABLE(brdp); ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr); head = (unsigned int) readw(&ap->txq.head); tail = (unsigned int) readw(&ap->txq.tail); if (tail != ((unsigned int) readw(&ap->txq.tail))) tail = (unsigned int) readw(&ap->txq.tail); size = portp->txsize; if (head >= tail) { len = size - (head - tail) - 1; stlen = size - head; } else { len = tail - head - 1; stlen = len; } len = min(len, (unsigned int)count); count = 0; shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->txoffset); while (len > 0) { stlen = min(len, stlen); memcpy_toio(shbuf + head, chbuf, stlen); chbuf += stlen; len -= stlen; count += stlen; head += stlen; if (head >= size) { head = 0; stlen = tail; } } ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr); writew(head, &ap->txq.head); if (test_bit(ST_TXBUSY, &portp->state)) { if (readl(&ap->changed.data) & DT_TXEMPTY) writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data); } hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR); bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset + portp->portidx; writeb(readb(bits) | portp->portbit, bits); set_bit(ST_TXBUSY, &portp->state); EBRDDISABLE(brdp); spin_unlock_irqrestore(&brd_lock, flags); return(count); } /*****************************************************************************/ /* * Output a single character. We put it into a temporary local buffer * (for speed) then write out that buffer when the flushchars routine * is called. There is a safety catch here so that if some other port * writes chars before the current buffer has been, then we write them * first them do the new ports. */ static void stli_putchar(struct tty_struct *tty, unsigned char ch) { if (tty != stli_txcooktty) { if (stli_txcooktty != NULL) stli_flushchars(stli_txcooktty); stli_txcooktty = tty; } stli_txcookbuf[stli_txcooksize++] = ch; } /*****************************************************************************/ /* * Transfer characters from the local TX cooking buffer to the board. * We sort of ignore the tty that gets passed in here. We rely on the * info stored with the TX cook buffer to tell us which port to flush * the data on. In any case we clean out the TX cook buffer, for re-use * by someone else. */ static void stli_flushchars(struct tty_struct *tty) { cdkhdr_t __iomem *hdrp; unsigned char __iomem *bits; cdkasy_t __iomem *ap; struct tty_struct *cooktty; struct stliport *portp; struct stlibrd *brdp; unsigned int len, stlen, head, tail, size, count, cooksize; unsigned char *buf; unsigned char __iomem *shbuf; unsigned long flags; cooksize = stli_txcooksize; cooktty = stli_txcooktty; stli_txcooksize = 0; stli_txcookrealsize = 0; stli_txcooktty = NULL; if (tty == NULL) return; if (cooktty == NULL) return; if (tty != cooktty) tty = cooktty; if (cooksize == 0) return; portp = tty->driver_data; if (portp == NULL) return; if (portp->brdnr >= stli_nrbrds) return; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return; spin_lock_irqsave(&brd_lock, flags); EBRDENABLE(brdp); ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr); head = (unsigned int) readw(&ap->txq.head); tail = (unsigned int) readw(&ap->txq.tail); if (tail != ((unsigned int) readw(&ap->txq.tail))) tail = (unsigned int) readw(&ap->txq.tail); size = portp->txsize; if (head >= tail) { len = size - (head - tail) - 1; stlen = size - head; } else { len = tail - head - 1; stlen = len; } len = min(len, cooksize); count = 0; shbuf = EBRDGETMEMPTR(brdp, portp->txoffset); buf = stli_txcookbuf; while (len > 0) { stlen = min(len, stlen); memcpy_toio(shbuf + head, buf, stlen); buf += stlen; len -= stlen; count += stlen; head += stlen; if (head >= size) { head = 0; stlen = tail; } } ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr); writew(head, &ap->txq.head); if (test_bit(ST_TXBUSY, &portp->state)) { if (readl(&ap->changed.data) & DT_TXEMPTY) writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data); } hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR); bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset + portp->portidx; writeb(readb(bits) | portp->portbit, bits); set_bit(ST_TXBUSY, &portp->state); EBRDDISABLE(brdp); spin_unlock_irqrestore(&brd_lock, flags); } /*****************************************************************************/ static int stli_writeroom(struct tty_struct *tty) { cdkasyrq_t __iomem *rp; struct stliport *portp; struct stlibrd *brdp; unsigned int head, tail, len; unsigned long flags; if (tty == stli_txcooktty) { if (stli_txcookrealsize != 0) { len = stli_txcookrealsize - stli_txcooksize; return len; } } portp = tty->driver_data; if (portp == NULL) return 0; if (portp->brdnr >= stli_nrbrds) return 0; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return 0; spin_lock_irqsave(&brd_lock, flags); EBRDENABLE(brdp); rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq; head = (unsigned int) readw(&rp->head); tail = (unsigned int) readw(&rp->tail); if (tail != ((unsigned int) readw(&rp->tail))) tail = (unsigned int) readw(&rp->tail); len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head); len--; EBRDDISABLE(brdp); spin_unlock_irqrestore(&brd_lock, flags); if (tty == stli_txcooktty) { stli_txcookrealsize = len; len -= stli_txcooksize; } return len; } /*****************************************************************************/ /* * Return the number of characters in the transmit buffer. Normally we * will return the number of chars in the shared memory ring queue. * We need to kludge around the case where the shared memory buffer is * empty but not all characters have drained yet, for this case just * return that there is 1 character in the buffer! */ static int stli_charsinbuffer(struct tty_struct *tty) { cdkasyrq_t __iomem *rp; struct stliport *portp; struct stlibrd *brdp; unsigned int head, tail, len; unsigned long flags; if (tty == stli_txcooktty) stli_flushchars(tty); portp = tty->driver_data; if (portp == NULL) return 0; if (portp->brdnr >= stli_nrbrds) return 0; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return 0; spin_lock_irqsave(&brd_lock, flags); EBRDENABLE(brdp); rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq; head = (unsigned int) readw(&rp->head); tail = (unsigned int) readw(&rp->tail); if (tail != ((unsigned int) readw(&rp->tail))) tail = (unsigned int) readw(&rp->tail); len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head)); if ((len == 0) && test_bit(ST_TXBUSY, &portp->state)) len = 1; EBRDDISABLE(brdp); spin_unlock_irqrestore(&brd_lock, flags); return len; } /*****************************************************************************/ /* * Generate the serial struct info. */ static int stli_getserial(struct stliport *portp, struct serial_struct __user *sp) { struct serial_struct sio; struct stlibrd *brdp; memset(&sio, 0, sizeof(struct serial_struct)); sio.type = PORT_UNKNOWN; sio.line = portp->portnr; sio.irq = 0; sio.flags = portp->flags; sio.baud_base = portp->baud_base; sio.close_delay = portp->close_delay; sio.closing_wait = portp->closing_wait; sio.custom_divisor = portp->custom_divisor; sio.xmit_fifo_size = 0; sio.hub6 = 0; brdp = stli_brds[portp->brdnr]; if (brdp != NULL) sio.port = brdp->iobase; return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0; } /*****************************************************************************/ /* * Set port according to the serial struct info. * At this point we do not do any auto-configure stuff, so we will * just quietly ignore any requests to change irq, etc. */ static int stli_setserial(struct stliport *portp, struct serial_struct __user *sp) { struct serial_struct sio; int rc; if (copy_from_user(&sio, sp, sizeof(struct serial_struct))) return -EFAULT; if (!capable(CAP_SYS_ADMIN)) { if ((sio.baud_base != portp->baud_base) || (sio.close_delay != portp->close_delay) || ((sio.flags & ~ASYNC_USR_MASK) != (portp->flags & ~ASYNC_USR_MASK))) return -EPERM; } portp->flags = (portp->flags & ~ASYNC_USR_MASK) | (sio.flags & ASYNC_USR_MASK); portp->baud_base = sio.baud_base; portp->close_delay = sio.close_delay; portp->closing_wait = sio.closing_wait; portp->custom_divisor = sio.custom_divisor; if ((rc = stli_setport(portp)) < 0) return rc; return 0; } /*****************************************************************************/ static int stli_tiocmget(struct tty_struct *tty, struct file *file) { struct stliport *portp = tty->driver_data; struct stlibrd *brdp; int rc; if (portp == NULL) return -ENODEV; if (portp->brdnr >= stli_nrbrds) return 0; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return 0; if (tty->flags & (1 << TTY_IO_ERROR)) return -EIO; if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig, sizeof(asysigs_t), 1)) < 0) return rc; return stli_mktiocm(portp->asig.sigvalue); } static int stli_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear) { struct stliport *portp = tty->driver_data; struct stlibrd *brdp; int rts = -1, dtr = -1; if (portp == NULL) return -ENODEV; if (portp->brdnr >= stli_nrbrds) return 0; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return 0; if (tty->flags & (1 << TTY_IO_ERROR)) return -EIO; if (set & TIOCM_RTS) rts = 1; if (set & TIOCM_DTR) dtr = 1; if (clear & TIOCM_RTS) rts = 0; if (clear & TIOCM_DTR) dtr = 0; stli_mkasysigs(&portp->asig, dtr, rts); return stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0); } static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) { struct stliport *portp; struct stlibrd *brdp; unsigned int ival; int rc; void __user *argp = (void __user *)arg; portp = tty->driver_data; if (portp == NULL) return -ENODEV; if (portp->brdnr >= stli_nrbrds) return 0; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return 0; if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) { if (tty->flags & (1 << TTY_IO_ERROR)) return -EIO; } rc = 0; switch (cmd) { case TIOCGSOFTCAR: rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0), (unsigned __user *) arg); break; case TIOCSSOFTCAR: if ((rc = get_user(ival, (unsigned __user *) arg)) == 0) tty->termios->c_cflag = (tty->termios->c_cflag & ~CLOCAL) | (ival ? CLOCAL : 0); break; case TIOCGSERIAL: rc = stli_getserial(portp, argp); break; case TIOCSSERIAL: rc = stli_setserial(portp, argp); break; case STL_GETPFLAG: rc = put_user(portp->pflag, (unsigned __user *)argp); break; case STL_SETPFLAG: if ((rc = get_user(portp->pflag, (unsigned __user *)argp)) == 0) stli_setport(portp); break; case COM_GETPORTSTATS: rc = stli_getportstats(portp, argp); break; case COM_CLRPORTSTATS: rc = stli_clrportstats(portp, argp); break; case TIOCSERCONFIG: case TIOCSERGWILD: case TIOCSERSWILD: case TIOCSERGETLSR: case TIOCSERGSTRUCT: case TIOCSERGETMULTI: case TIOCSERSETMULTI: default: rc = -ENOIOCTLCMD; break; } return rc; } /*****************************************************************************/ /* * This routine assumes that we have user context and can sleep. * Looks like it is true for the current ttys implementation..!! */ static void stli_settermios(struct tty_struct *tty, struct ktermios *old) { struct stliport *portp; struct stlibrd *brdp; struct ktermios *tiosp; asyport_t aport; if (tty == NULL) return; portp = tty->driver_data; if (portp == NULL) return; if (portp->brdnr >= stli_nrbrds) return; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return; tiosp = tty->termios; stli_mkasyport(portp, &aport, tiosp); stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0); stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1); stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0); if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) tty->hw_stopped = 0; if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL)) wake_up_interruptible(&portp->open_wait); } /*****************************************************************************/ /* * Attempt to flow control who ever is sending us data. We won't really * do any flow control action here. We can't directly, and even if we * wanted to we would have to send a command to the slave. The slave * knows how to flow control, and will do so when its buffers reach its * internal high water marks. So what we will do is set a local state * bit that will stop us sending any RX data up from the poll routine * (which is the place where RX data from the slave is handled). */ static void stli_throttle(struct tty_struct *tty) { struct stliport *portp = tty->driver_data; if (portp == NULL) return; set_bit(ST_RXSTOP, &portp->state); } /*****************************************************************************/ /* * Unflow control the device sending us data... That means that all * we have to do is clear the RXSTOP state bit. The next poll call * will then be able to pass the RX data back up. */ static void stli_unthrottle(struct tty_struct *tty) { struct stliport *portp = tty->driver_data; if (portp == NULL) return; clear_bit(ST_RXSTOP, &portp->state); } /*****************************************************************************/ /* * Stop the transmitter. */ static void stli_stop(struct tty_struct *tty) { } /*****************************************************************************/ /* * Start the transmitter again. */ static void stli_start(struct tty_struct *tty) { } /*****************************************************************************/ /* * Scheduler called hang up routine. This is called from the scheduler, * not direct from the driver "poll" routine. We can't call it there * since the real local hangup code will enable/disable the board and * other things that we can't do while handling the poll. Much easier * to deal with it some time later (don't really care when, hangups * aren't that time critical). */ static void stli_dohangup(struct work_struct *ugly_api) { struct stliport *portp = container_of(ugly_api, struct stliport, tqhangup); if (portp->tty != NULL) { tty_hangup(portp->tty); } } /*****************************************************************************/ /* * Hangup this port. This is pretty much like closing the port, only * a little more brutal. No waiting for data to drain. Shutdown the * port and maybe drop signals. This is rather tricky really. We want * to close the port as well. */ static void stli_hangup(struct tty_struct *tty) { struct stliport *portp; struct stlibrd *brdp; unsigned long flags; portp = tty->driver_data; if (portp == NULL) return; if (portp->brdnr >= stli_nrbrds) return; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return; portp->flags &= ~ASYNC_INITIALIZED; if (!test_bit(ST_CLOSING, &portp->state)) stli_rawclose(brdp, portp, 0, 0); spin_lock_irqsave(&stli_lock, flags); if (tty->termios->c_cflag & HUPCL) { stli_mkasysigs(&portp->asig, 0, 0); if (test_bit(ST_CMDING, &portp->state)) { set_bit(ST_DOSIGS, &portp->state); set_bit(ST_DOFLUSHTX, &portp->state); set_bit(ST_DOFLUSHRX, &portp->state); } else { stli_sendcmd(brdp, portp, A_SETSIGNALSF, &portp->asig, sizeof(asysigs_t), 0); } } clear_bit(ST_TXBUSY, &portp->state); clear_bit(ST_RXSTOP, &portp->state); set_bit(TTY_IO_ERROR, &tty->flags); portp->tty = NULL; portp->flags &= ~ASYNC_NORMAL_ACTIVE; portp->refcount = 0; spin_unlock_irqrestore(&stli_lock, flags); wake_up_interruptible(&portp->open_wait); } /*****************************************************************************/ /* * Flush characters from the lower buffer. We may not have user context * so we cannot sleep waiting for it to complete. Also we need to check * if there is chars for this port in the TX cook buffer, and flush them * as well. */ static void stli_flushbuffer(struct tty_struct *tty) { struct stliport *portp; struct stlibrd *brdp; unsigned long ftype, flags; portp = tty->driver_data; if (portp == NULL) return; if (portp->brdnr >= stli_nrbrds) return; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return; spin_lock_irqsave(&brd_lock, flags); if (tty == stli_txcooktty) { stli_txcooktty = NULL; stli_txcooksize = 0; stli_txcookrealsize = 0; } if (test_bit(ST_CMDING, &portp->state)) { set_bit(ST_DOFLUSHTX, &portp->state); } else { ftype = FLUSHTX; if (test_bit(ST_DOFLUSHRX, &portp->state)) { ftype |= FLUSHRX; clear_bit(ST_DOFLUSHRX, &portp->state); } __stli_sendcmd(brdp, portp, A_FLUSH, &ftype, sizeof(u32), 0); } spin_unlock_irqrestore(&brd_lock, flags); tty_wakeup(tty); } /*****************************************************************************/ static void stli_breakctl(struct tty_struct *tty, int state) { struct stlibrd *brdp; struct stliport *portp; long arg; portp = tty->driver_data; if (portp == NULL) return; if (portp->brdnr >= stli_nrbrds) return; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return; arg = (state == -1) ? BREAKON : BREAKOFF; stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0); } /*****************************************************************************/ static void stli_waituntilsent(struct tty_struct *tty, int timeout) { struct stliport *portp; unsigned long tend; if (tty == NULL) return; portp = tty->driver_data; if (portp == NULL) return; if (timeout == 0) timeout = HZ; tend = jiffies + timeout; while (test_bit(ST_TXBUSY, &portp->state)) { if (signal_pending(current)) break; msleep_interruptible(20); if (time_after_eq(jiffies, tend)) break; } } /*****************************************************************************/ static void stli_sendxchar(struct tty_struct *tty, char ch) { struct stlibrd *brdp; struct stliport *portp; asyctrl_t actrl; portp = tty->driver_data; if (portp == NULL) return; if (portp->brdnr >= stli_nrbrds) return; brdp = stli_brds[portp->brdnr]; if (brdp == NULL) return; memset(&actrl, 0, sizeof(asyctrl_t)); if (ch == STOP_CHAR(tty)) { actrl.rxctrl = CT_STOPFLOW; } else if (ch == START_CHAR(tty)) { actrl.rxctrl = CT_STARTFLOW; } else { actrl.txctrl = CT_SENDCHR; actrl.tximdch = ch; } stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0); } /*****************************************************************************/ #define MAXLINE 80 /* * Format info for a specified port. The line is deliberately limited * to 80 characters. (If it is too long it will be truncated, if too * short then padded with spaces). */ static int stli_portinfo(struct stlibrd *brdp, struct stliport *portp, int portnr, char *pos) { char *sp, *uart; int rc, cnt; rc = stli_portcmdstats(portp); uart = "UNKNOWN"; if (brdp->state & BST_STARTED) { switch (stli_comstats.hwid) { case 0: uart = "2681"; break; case 1: uart = "SC26198"; break; default:uart = "CD1400"; break; } } sp = pos; sp += sprintf(sp, "%d: uart:%s ", portnr, uart); if ((brdp->state & BST_STARTED) && (rc >= 0)) { sp += sprintf(sp, "tx:%d rx:%d", (int) stli_comstats.txtotal, (int) stli_comstats.rxtotal); if (stli_comstats.rxframing) sp += sprintf(sp, " fe:%d", (int) stli_comstats.rxframing); if (stli_comstats.rxparity) sp += sprintf(sp, " pe:%d", (int) stli_comstats.rxparity); if (stli_comstats.rxbreaks) sp += sprintf(sp, " brk:%d", (int) stli_comstats.rxbreaks); if (stli_comstats.rxoverrun) sp += sprintf(sp, " oe:%d", (int) stli_comstats.rxoverrun); cnt = sprintf(sp, "%s%s%s%s%s ", (stli_comstats.signals & TIOCM_RTS) ? "|RTS" : "", (stli_comstats.signals & TIOCM_CTS) ? "|CTS" : "", (stli_comstats.signals & TIOCM_DTR) ? "|DTR" : "", (stli_comstats.signals & TIOCM_CD) ? "|DCD" : "", (stli_comstats.signals & TIOCM_DSR) ? "|DSR" : ""); *sp = ' '; sp += cnt; } for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++) *sp++ = ' '; if (cnt >= MAXLINE) pos[(MAXLINE - 2)] = '+'; pos[(MAXLINE - 1)] = '\n'; return(MAXLINE); } /*****************************************************************************/ /* * Port info, read from the /proc file system. */ static int stli_readproc(char *page, char **start, off_t off, int count, int *eof, void *data) { struct stlibrd *brdp; struct stliport *portp; unsigned int brdnr, portnr, totalport; int curoff, maxoff; char *pos; pos = page; totalport = 0; curoff = 0; if (off == 0) { pos += sprintf(pos, "%s: version %s", stli_drvtitle, stli_drvversion); while (pos < (page + MAXLINE - 1)) *pos++ = ' '; *pos++ = '\n'; } curoff = MAXLINE; /* * We scan through for each board, panel and port. The offset is * calculated on the fly, and irrelevant ports are skipped. */ for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) { brdp = stli_brds[brdnr]; if (brdp == NULL) continue; if (brdp->state == 0) continue; maxoff = curoff + (brdp->nrports * MAXLINE); if (off >= maxoff) { curoff = maxoff; continue; } totalport = brdnr * STL_MAXPORTS; for (portnr = 0; (portnr < brdp->nrports); portnr++, totalport++) { portp = brdp->ports[portnr]; if (portp == NULL) continue; if (off >= (curoff += MAXLINE)) continue; if ((pos - page + MAXLINE) > count) goto stli_readdone; pos += stli_portinfo(brdp, portp, totalport, pos); } } *eof = 1; stli_readdone: *start = page; return(pos - page); } /*****************************************************************************/ /* * Generic send command routine. This will send a message to the slave, * of the specified type with the specified argument. Must be very * careful of data that will be copied out from shared memory - * containing command results. The command completion is all done from * a poll routine that does not have user context. Therefore you cannot * copy back directly into user space, or to the kernel stack of a * process. This routine does not sleep, so can be called from anywhere. * * The caller must hold the brd_lock (see also stli_sendcmd the usual * entry point) */ static void __stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback) { cdkhdr_t __iomem *hdrp; cdkctrl_t __iomem *cp; unsigned char __iomem *bits; if (test_bit(ST_CMDING, &portp->state)) { printk(KERN_ERR "STALLION: command already busy, cmd=%x!\n", (int) cmd); return; } EBRDENABLE(brdp); cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl; if (size > 0) { memcpy_toio((void __iomem *) &(cp->args[0]), arg, size); if (copyback) { portp->argp = arg; portp->argsize = size; } } writel(0, &cp->status); writel(cmd, &cp->cmd); hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR); bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset + portp->portidx; writeb(readb(bits) | portp->portbit, bits); set_bit(ST_CMDING, &portp->state); EBRDDISABLE(brdp); } static void stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback) { unsigned long flags; spin_lock_irqsave(&brd_lock, flags); __stli_sendcmd(brdp, portp, cmd, arg, size, copyback); spin_unlock_irqrestore(&brd_lock, flags); } /*****************************************************************************/ /* * Read data from shared memory. This assumes that the shared memory * is enabled and that interrupts are off. Basically we just empty out * the shared memory buffer into the tty buffer. Must be careful to * handle the case where we fill up the tty buffer, but still have * more chars to unload. */ static void stli_read(struct stlibrd *brdp, struct stliport *portp) { cdkasyrq_t __iomem *rp; char __iomem *shbuf; struct tty_struct *tty; unsigned int head, tail, size; unsigned int len, stlen; if (test_bit(ST_RXSTOP, &portp->state)) return; tty = portp->tty; if (tty == NULL) return; rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq; head = (unsigned int) readw(&rp->head); if (head != ((unsigned int) readw(&rp->head))) head = (unsigned int) readw(&rp->head); tail = (unsigned int) readw(&rp->tail); size = portp->rxsize; if (head >= tail) { len = head - tail; stlen = len; } else { len = size - (tail - head); stlen = size - tail; } len = tty_buffer_request_room(tty, len); shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->rxoffset); while (len > 0) { unsigned char *cptr; stlen = min(len, stlen); tty_prepare_flip_string(tty, &cptr, stlen); memcpy_fromio(cptr, shbuf + tail, stlen); len -= stlen; tail += stlen; if (tail >= size) { tail = 0; stlen = head; } } rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq; writew(tail, &rp->tail); if (head != tail) set_bit(ST_RXING, &portp->state); tty_schedule_flip(tty); } /*****************************************************************************/ /* * Set up and carry out any delayed commands. There is only a small set * of slave commands that can be done "off-level". So it is not too * difficult to deal with them here. */ static void stli_dodelaycmd(struct stliport *portp, cdkctrl_t __iomem *cp) { int cmd; if (test_bit(ST_DOSIGS, &portp->state)) { if (test_bit(ST_DOFLUSHTX, &portp->state) && test_bit(ST_DOFLUSHRX, &portp->state)) cmd = A_SETSIGNALSF; else if (test_bit(ST_DOFLUSHTX, &portp->state)) cmd = A_SETSIGNALSFTX; else if (test_bit(ST_DOFLUSHRX, &portp->state)) cmd = A_SETSIGNALSFRX; else cmd = A_SETSIGNALS; clear_bit(ST_DOFLUSHTX, &portp->state); clear_bit(ST_DOFLUSHRX, &portp->state); clear_bit(ST_DOSIGS, &portp->state); memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &port