stallion.c

来自「linux 内核源代码」· C语言 代码 · 共 2,551 行 · 第 1/5 页

C
2,551
字号
 *	CD1400 UART specific data initialization. */static uart_t stl_cd1400uart = {	stl_cd1400panelinit,	stl_cd1400portinit,	stl_cd1400setport,	stl_cd1400getsignals,	stl_cd1400setsignals,	stl_cd1400enablerxtx,	stl_cd1400startrxtx,	stl_cd1400disableintrs,	stl_cd1400sendbreak,	stl_cd1400flowctrl,	stl_cd1400sendflow,	stl_cd1400flush,	stl_cd1400datastate,	stl_cd1400eiointr};/* *	Define the offsets within the register bank of a cd1400 based panel. *	These io address offsets are common to the EasyIO board as well. */#define	EREG_ADDR	0#define	EREG_DATA	4#define	EREG_RXACK	5#define	EREG_TXACK	6#define	EREG_MDACK	7#define	EREG_BANKSIZE	8#define	CD1400_CLK	25000000#define	CD1400_CLK8M	20000000/* *	Define the cd1400 baud rate clocks. These are used when calculating *	what clock and divisor to use for the required baud rate. Also *	define the maximum baud rate allowed, and the default base baud. */static int	stl_cd1400clkdivs[] = {	CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4};/*****************************************************************************//* *	SC26198 UART specific data initization. */static uart_t stl_sc26198uart = {	stl_sc26198panelinit,	stl_sc26198portinit,	stl_sc26198setport,	stl_sc26198getsignals,	stl_sc26198setsignals,	stl_sc26198enablerxtx,	stl_sc26198startrxtx,	stl_sc26198disableintrs,	stl_sc26198sendbreak,	stl_sc26198flowctrl,	stl_sc26198sendflow,	stl_sc26198flush,	stl_sc26198datastate,	stl_sc26198intr};/* *	Define the offsets within the register bank of a sc26198 based panel. */#define	XP_DATA		0#define	XP_ADDR		1#define	XP_MODID	2#define	XP_STATUS	2#define	XP_IACK		3#define	XP_BANKSIZE	4/* *	Define the sc26198 baud rate table. Offsets within the table *	represent the actual baud rate selector of sc26198 registers. */static unsigned int	sc26198_baudtable[] = {	50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,	4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,	230400, 460800, 921600};#define	SC26198_NRBAUDS		ARRAY_SIZE(sc26198_baudtable)/*****************************************************************************//* *	Define the driver info for a user level control device. Used mainly *	to get at port stats - only not using the port device itself. */static const struct file_operations	stl_fsiomem = {	.owner		= THIS_MODULE,	.ioctl		= stl_memioctl,};static struct class *stallion_class;/* *	Check for any arguments passed in on the module load command line. *//*****************************************************************************//* *	Parse the supplied argument string, into the board conf struct. */static int __init stl_parsebrd(struct stlconf *confp, char **argp){	char	*sp;	unsigned int i;	pr_debug("stl_parsebrd(confp=%p,argp=%p)\n", confp, argp);	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(stl_brdstr); i++)		if (strcmp(stl_brdstr[i].name, argp[0]) == 0)			break;	if (i == ARRAY_SIZE(stl_brdstr)) {		printk("STALLION: unknown board name, %s?\n", argp[0]);		return 0;	}	confp->brdtype = stl_brdstr[i].type;	i = 1;	if ((argp[i] != NULL) && (*argp[i] != 0))		confp->ioaddr1 = simple_strtoul(argp[i], NULL, 0);	i++;	if (confp->brdtype == BRD_ECH) {		if ((argp[i] != NULL) && (*argp[i] != 0))			confp->ioaddr2 = simple_strtoul(argp[i], NULL, 0);		i++;	}	if ((argp[i] != NULL) && (*argp[i] != 0))		confp->irq = simple_strtoul(argp[i], NULL, 0);	return 1;}/*****************************************************************************//* *	Allocate a new board structure. Fill out the basic info in it. */static struct stlbrd *stl_allocbrd(void){	struct stlbrd	*brdp;	brdp = kzalloc(sizeof(struct stlbrd), GFP_KERNEL);	if (!brdp) {		printk("STALLION: failed to allocate memory (size=%Zd)\n",			sizeof(struct stlbrd));		return NULL;	}	brdp->magic = STL_BOARDMAGIC;	return brdp;}/*****************************************************************************/static int stl_open(struct tty_struct *tty, struct file *filp){	struct stlport	*portp;	struct stlbrd	*brdp;	unsigned int	minordev, brdnr, panelnr;	int		portnr, rc;	pr_debug("stl_open(tty=%p,filp=%p): device=%s\n", tty, filp, tty->name);	minordev = tty->index;	brdnr = MINOR2BRD(minordev);	if (brdnr >= stl_nrbrds)		return -ENODEV;	brdp = stl_brds[brdnr];	if (brdp == NULL)		return -ENODEV;	minordev = MINOR2PORT(minordev);	for (portnr = -1, panelnr = 0; panelnr < STL_MAXPANELS; panelnr++) {		if (brdp->panels[panelnr] == NULL)			break;		if (minordev < brdp->panels[panelnr]->nrports) {			portnr = minordev;			break;		}		minordev -= brdp->panels[panelnr]->nrports;	}	if (portnr < 0)		return -ENODEV;	portp = brdp->panels[panelnr]->ports[portnr];	if (portp == NULL)		return -ENODEV;/* *	On the first open of the device setup the port hardware, and *	initialize the per port data structure. */	portp->tty = tty;	tty->driver_data = portp;	portp->refcount++;	if ((portp->flags & ASYNC_INITIALIZED) == 0) {		if (!portp->tx.buf) {			portp->tx.buf = kmalloc(STL_TXBUFSIZE, GFP_KERNEL);			if (!portp->tx.buf)				return -ENOMEM;			portp->tx.head = portp->tx.buf;			portp->tx.tail = portp->tx.buf;		}		stl_setport(portp, tty->termios);		portp->sigs = stl_getsignals(portp);		stl_setsignals(portp, 1, 1);		stl_enablerxtx(portp, 1, 1);		stl_startrxtx(portp, 1, 0);		clear_bit(TTY_IO_ERROR, &tty->flags);		portp->flags |= ASYNC_INITIALIZED;	}/* *	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 = stl_waitcarrier(portp, filp)) != 0)			return rc;	portp->flags |= ASYNC_NORMAL_ACTIVE;	return 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 stl_waitcarrier(struct stlport *portp, struct file *filp){	unsigned long	flags;	int		rc, doclocal;	pr_debug("stl_waitcarrier(portp=%p,filp=%p)\n", portp, filp);	rc = 0;	doclocal = 0;	spin_lock_irqsave(&stallion_lock, flags);	if (portp->tty->termios->c_cflag & CLOCAL)		doclocal++;	portp->openwaitcnt++;	if (! tty_hung_up_p(filp))		portp->refcount--;	for (;;) {		/* Takes brd_lock internally */		stl_setsignals(portp, 1, 1);		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;		}		/* FIXME */		interruptible_sleep_on(&portp->open_wait);	}	if (! tty_hung_up_p(filp))		portp->refcount++;	portp->openwaitcnt--;	spin_unlock_irqrestore(&stallion_lock, flags);	return rc;}/*****************************************************************************/static void stl_flushbuffer(struct tty_struct *tty){	struct stlport	*portp;	pr_debug("stl_flushbuffer(tty=%p)\n", tty);	if (tty == NULL)		return;	portp = tty->driver_data;	if (portp == NULL)		return;	stl_flush(portp);	tty_wakeup(tty);}/*****************************************************************************/static void stl_waituntilsent(struct tty_struct *tty, int timeout){	struct stlport	*portp;	unsigned long	tend;	pr_debug("stl_waituntilsent(tty=%p,timeout=%d)\n", tty, timeout);	if (tty == NULL)		return;	portp = tty->driver_data;	if (portp == NULL)		return;	if (timeout == 0)		timeout = HZ;	tend = jiffies + timeout;	while (stl_datastate(portp)) {		if (signal_pending(current))			break;		msleep_interruptible(20);		if (time_after_eq(jiffies, tend))			break;	}}/*****************************************************************************/static void stl_close(struct tty_struct *tty, struct file *filp){	struct stlport	*portp;	unsigned long	flags;	pr_debug("stl_close(tty=%p,filp=%p)\n", tty, filp);	portp = tty->driver_data;	if (portp == NULL)		return;	spin_lock_irqsave(&stallion_lock, flags);	if (tty_hung_up_p(filp)) {		spin_unlock_irqrestore(&stallion_lock, flags);		return;	}	if ((tty->count == 1) && (portp->refcount != 1))		portp->refcount = 1;	if (portp->refcount-- > 1) {		spin_unlock_irqrestore(&stallion_lock, flags);		return;	}	portp->refcount = 0;	portp->flags |= ASYNC_CLOSING;/* *	May want to wait for any data to drain before closing. The BUSY *	flag keeps track of whether we are still sending or not - it is *	very accurate for the cd1400, not quite so for the sc26198. *	(The sc26198 has no "end-of-data" interrupt only empty FIFO) */	tty->closing = 1;	spin_unlock_irqrestore(&stallion_lock, flags);	if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)		tty_wait_until_sent(tty, portp->closing_wait);	stl_waituntilsent(tty, (HZ / 2));	spin_lock_irqsave(&stallion_lock, flags);	portp->flags &= ~ASYNC_INITIALIZED;	spin_unlock_irqrestore(&stallion_lock, flags);	stl_disableintrs(portp);	if (tty->termios->c_cflag & HUPCL)		stl_setsignals(portp, 0, 0);	stl_enablerxtx(portp, 0, 0);	stl_flushbuffer(tty);	portp->istate = 0;	if (portp->tx.buf != NULL) {		kfree(portp->tx.buf);		portp->tx.buf = NULL;		portp->tx.head = NULL;		portp->tx.tail = NULL;	}	set_bit(TTY_IO_ERROR, &tty->flags);	tty_ldisc_flush(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);}/*****************************************************************************//* *	Write routine. Take data and stuff it in to the TX ring queue. *	If transmit interrupts are not running then start them. */static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count){	struct stlport	*portp;	unsigned int	len, stlen;	unsigned char	*chbuf;	char		*head, *tail;	pr_debug("stl_write(tty=%p,buf=%p,count=%d)\n", tty, buf, count);	portp = tty->driver_data;	if (portp == NULL)		return 0;	if (portp->tx.buf == NULL)		return 0;/* *	If copying direct from user space we must cater for page faults, *	causing us to "sleep" here for a while. To handle this copy in all *	the data we need now, into a local buffer. Then when we got it all *	copy it into the TX buffer. */	chbuf = (unsigned char *) buf;	head = portp->tx.head;	tail = portp->tx.tail;	if (head >= tail) {		len = STL_TXBUFSIZE - (head - tail) - 1;		stlen = STL_TXBUFSIZE - (head - portp->tx.buf);	} else {		len = tail - head - 1;		stlen = len;	}	len = min(len, (unsigned int)count);	count = 0;	while (len > 0) {		stlen = min(len, stlen);		memcpy(head, chbuf, stlen);		len -= stlen;		chbuf += stlen;		count += stlen;		head += stlen;		if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {			head = portp->tx.buf;			stlen = tail - head;		}	}	portp->tx.head = head;	clear_bit(ASYI_TXLOW, &portp->istate);	stl_startrxtx(portp, -1, 1);	return count;}/*****************************************************************************/static void stl_putchar(struct tty_struct *tty, unsigned char ch){	struct stlport	*portp;	unsigned int	len;	char		*head, *tail;	pr_debug("stl_putchar(tty=%p,ch=%x)\n", tty, ch);	if (tty == NULL)		return;	portp = tty->driver_data;	if (portp == NULL)		return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?