stallion.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 2,504 行 · 第 1/5 页

C
2,504
字号
	unsigned int	minordev;	int		brdnr, panelnr, portnr, rc;#ifdef DEBUG	printk("stl_open(tty=%x,filp=%x): device=%s\n", (int) tty,		(int) filp, tty->name);#endif	minordev = tty->index;	brdnr = MINOR2BRD(minordev);	if (brdnr >= stl_nrbrds)		return(-ENODEV);	brdp = stl_brds[brdnr];	if (brdp == (stlbrd_t *) NULL)		return(-ENODEV);	minordev = MINOR2PORT(minordev);	for (portnr = -1, panelnr = 0; (panelnr < STL_MAXPANELS); panelnr++) {		if (brdp->panels[panelnr] == (stlpanel_t *) 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 == (stlport_t *) 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 == (char *) NULL) {			portp->tx.buf = (char *) stl_memalloc(STL_TXBUFSIZE);			if (portp->tx.buf == (char *) NULL)				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(stlport_t *portp, struct file *filp){	unsigned long	flags;	int		rc, doclocal;#ifdef DEBUG	printk("stl_waitcarrier(portp=%x,filp=%x)\n", (int) portp, (int) filp);#endif	rc = 0;	doclocal = 0;	if (portp->tty->termios->c_cflag & CLOCAL)		doclocal++;	save_flags(flags);	cli();	portp->openwaitcnt++;	if (! tty_hung_up_p(filp))		portp->refcount--;	for (;;) {		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;		}		interruptible_sleep_on(&portp->open_wait);	}	if (! tty_hung_up_p(filp))		portp->refcount++;	portp->openwaitcnt--;	restore_flags(flags);	return(rc);}/*****************************************************************************/static void stl_close(struct tty_struct *tty, struct file *filp){	stlport_t	*portp;	unsigned long	flags;#ifdef DEBUG	printk("stl_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);#endif	portp = tty->driver_data;	if (portp == (stlport_t *) NULL)		return;	save_flags(flags);	cli();	if (tty_hung_up_p(filp)) {		restore_flags(flags);		return;	}	if ((tty->count == 1) && (portp->refcount != 1))		portp->refcount = 1;	if (portp->refcount-- > 1) {		restore_flags(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;	if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)		tty_wait_until_sent(tty, portp->closing_wait);	stl_waituntilsent(tty, (HZ / 2));	portp->flags &= ~ASYNC_INITIALIZED;	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 != (char *) NULL) {		kfree(portp->tx.buf);		portp->tx.buf = (char *) NULL;		portp->tx.head = (char *) NULL;		portp->tx.tail = (char *) NULL;	}	set_bit(TTY_IO_ERROR, &tty->flags);	tty_ldisc_flush(tty);	tty->closing = 0;	portp->tty = (struct tty_struct *) NULL;	if (portp->openwaitcnt) {		if (portp->close_delay)			stl_delay(portp->close_delay);		wake_up_interruptible(&portp->open_wait);	}	portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);	wake_up_interruptible(&portp->close_wait);	restore_flags(flags);}/*****************************************************************************//* *	Wait for a specified delay period, this is not a busy-loop. It will *	give up the processor while waiting. Unfortunately this has some *	rather intimate knowledge of the process management stuff. */static void stl_delay(int len){#ifdef DEBUG	printk("stl_delay(len=%d)\n", len);#endif	if (len > 0) {		current->state = TASK_INTERRUPTIBLE;		schedule_timeout(len);	}}/*****************************************************************************//* *	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, int from_user, const unsigned char *buf, int count){	stlport_t	*portp;	unsigned int	len, stlen;	unsigned char	*chbuf;	char		*head, *tail;#ifdef DEBUG	printk("stl_write(tty=%x,from_user=%d,buf=%x,count=%d)\n",		(int) tty, from_user, (int) buf, count);#endif	if ((tty == (struct tty_struct *) NULL) ||	    (stl_tmpwritebuf == (char *) NULL))		return(0);	portp = tty->driver_data;	if (portp == (stlport_t *) NULL)		return(0);	if (portp->tx.buf == (char *) 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;	if (from_user) {		head = portp->tx.head;		tail = portp->tx.tail;		len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) :			(tail - head - 1);		count = MIN(len, count);				down(&stl_tmpwritesem);		if (copy_from_user(stl_tmpwritebuf, chbuf, count)) 			return -EFAULT;		chbuf = &stl_tmpwritebuf[0];	}	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, 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);	if (from_user)		up(&stl_tmpwritesem);	return(count);}/*****************************************************************************/static void stl_putchar(struct tty_struct *tty, unsigned char ch){	stlport_t	*portp;	unsigned int	len;	char		*head, *tail;#ifdef DEBUG	printk("stl_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);#endif	if (tty == (struct tty_struct *) NULL)		return;	portp = tty->driver_data;	if (portp == (stlport_t *) NULL)		return;	if (portp->tx.buf == (char *) NULL)		return;	head = portp->tx.head;	tail = portp->tx.tail;	len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);	len--;	if (len > 0) {		*head++ = ch;		if (head >= (portp->tx.buf + STL_TXBUFSIZE))			head = portp->tx.buf;	}		portp->tx.head = head;}/*****************************************************************************//* *	If there are any characters in the buffer then make sure that TX *	interrupts are on and get'em out. Normally used after the putchar *	routine has been called. */static void stl_flushchars(struct tty_struct *tty){	stlport_t	*portp;#ifdef DEBUG	printk("stl_flushchars(tty=%x)\n", (int) tty);#endif	if (tty == (struct tty_struct *) NULL)		return;	portp = tty->driver_data;	if (portp == (stlport_t *) NULL)		return;	if (portp->tx.buf == (char *) NULL)		return;#if 0	if (tty->stopped || tty->hw_stopped ||	    (portp->tx.head == portp->tx.tail))		return;#endif	stl_startrxtx(portp, -1, 1);}/*****************************************************************************/static int stl_writeroom(struct tty_struct *tty){	stlport_t	*portp;	char		*head, *tail;#ifdef DEBUG	printk("stl_writeroom(tty=%x)\n", (int) tty);#endif	if (tty == (struct tty_struct *) NULL)		return(0);	portp = tty->driver_data;	if (portp == (stlport_t *) NULL)		return(0);	if (portp->tx.buf == (char *) NULL)		return(0);	head = portp->tx.head;	tail = portp->tx.tail;	return((head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1));}/*****************************************************************************//* *	Return number of chars in the TX buffer. Normally we would just *	calculate the number of chars in the buffer and return that, but if *	the buffer is empty and TX interrupts are still on then we return *	that the buffer still has 1 char in it. This way whoever called us *	will not think that ALL chars have drained - since the UART still *	must have some chars in it (we are busy after all). */static int stl_charsinbuffer(struct tty_struct *tty){	stlport_t	*portp;	unsigned int	size;	char		*head, *tail;#ifdef DEBUG	printk("stl_charsinbuffer(tty=%x)\n", (int) tty);#endif	if (tty == (struct tty_struct *) NULL)		return(0);	portp = tty->driver_data;	if (portp == (stlport_t *) NULL)		return(0);	if (portp->tx.buf == (char *) NULL)		return(0);	head = portp->tx.head;	tail = portp->tx.tail;	size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));	if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))		size = 1;	return(size);}/*****************************************************************************//* *	Generate the serial struct info. */static int stl_getserial(stlport_t *portp, struct serial_struct __user *sp){	struct serial_struct	sio;	stlbrd_t		*brdp;#ifdef DEBUG	printk("stl_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);#endif	memset(&sio, 0, sizeof(struct serial_struct));	sio.line = portp->portnr;	sio.port = portp->ioaddr;	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.hub6 = 0;	if (portp->uartp == &stl_cd1400uart) {		sio.type = PORT_CIRRUS;		sio.xmit_fifo_size = CD1400_TXFIFOSIZE;	} else {		sio.type = PORT_UNKNOWN;		sio.xmit_fifo_size = SC26198_TXFIFOSIZE;	}	brdp = stl_brds[portp->brdnr];	if (brdp != (stlbrd_t *) NULL)		sio.irq = brdp->irq;	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 stl_setserial(stlport_t *portp, struct serial_struct __user *sp){	struct serial_struct	sio;#ifdef DEBUG	printk("stl_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);#endif	if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))		return -EFAULT;	if (!capable(CAP_SYS_ADMIN)) {		if ((sio.baud_base != portp->baud_base) ||

⌨️ 快捷键说明

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