⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stallion.c

📁 powerpc内核mpc8241linux系统下char驱动程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/* *	Allocate a new board structure. Fill out the basic info in it. */static stlbrd_t *stl_allocbrd(){	stlbrd_t	*brdp;	brdp = (stlbrd_t *) stl_memalloc(sizeof(stlbrd_t));	if (brdp == (stlbrd_t *) NULL) {		printk("STALLION: failed to allocate memory (size=%d)\n",			sizeof(stlbrd_t));		return((stlbrd_t *) NULL);	}	memset(brdp, 0, sizeof(stlbrd_t));	brdp->magic = STL_BOARDMAGIC;	return(brdp);}/*****************************************************************************/static int stl_open(struct tty_struct *tty, struct file *filp){	stlport_t	*portp;	stlbrd_t	*brdp;	unsigned int	minordev;	int		brdnr, panelnr, portnr, rc;#if DEBUG	printk("stl_open(tty=%x,filp=%x): device=%x\n", (int) tty,		(int) filp, tty->device);#endif	minordev = MINOR(tty->device);	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);	MOD_INC_USE_COUNT;/* *	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 (tty->driver.subtype == STL_DRVTYPCALLOUT) {		if (portp->flags & ASYNC_NORMAL_ACTIVE)			return(-EBUSY);		if (portp->flags & ASYNC_CALLOUT_ACTIVE) {			if ((portp->flags & ASYNC_SESSION_LOCKOUT) &&			    (portp->session != current->session))				return(-EBUSY);			if ((portp->flags & ASYNC_PGRP_LOCKOUT) &&			    (portp->pgrp != current->pgrp))				return(-EBUSY);		}		portp->flags |= ASYNC_CALLOUT_ACTIVE;	} else {		if (filp->f_flags & O_NONBLOCK) {			if (portp->flags & ASYNC_CALLOUT_ACTIVE)				return(-EBUSY);		} else {			if ((rc = stl_waitcarrier(portp, filp)) != 0)				return(rc);		}		portp->flags |= ASYNC_NORMAL_ACTIVE;	}	if ((portp->refcount == 1) && (portp->flags & ASYNC_SPLIT_TERMIOS)) {		if (tty->driver.subtype == STL_DRVTYPSERIAL)			*tty->termios = portp->normaltermios;		else			*tty->termios = portp->callouttermios;		stl_setport(portp, tty->termios);	}	portp->session = current->session;	portp->pgrp = current->pgrp;	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;#if DEBUG	printk("stl_waitcarrier(portp=%x,filp=%x)\n", (int) portp, (int) filp);#endif	rc = 0;	doclocal = 0;	if (portp->flags & ASYNC_CALLOUT_ACTIVE) {		if (portp->normaltermios.c_cflag & CLOCAL)			doclocal++;	} else {		if (portp->tty->termios->c_cflag & CLOCAL)			doclocal++;	}	save_flags(flags);	cli();	portp->openwaitcnt++;	if (! tty_hung_up_p(filp))		portp->refcount--;	for (;;) {		if ((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0)			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_CALLOUT_ACTIVE) == 0) &&		    ((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;#if 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)) {		MOD_DEC_USE_COUNT;		restore_flags(flags);		return;	}	if ((tty->count == 1) && (portp->refcount != 1))		portp->refcount = 1;	if (portp->refcount-- > 1) {		MOD_DEC_USE_COUNT;		restore_flags(flags);		return;	}	portp->refcount = 0;	portp->flags |= ASYNC_CLOSING;	if (portp->flags & ASYNC_NORMAL_ACTIVE)		portp->normaltermios = *tty->termios;	if (portp->flags & ASYNC_CALLOUT_ACTIVE)		portp->callouttermios = *tty->termios;/* *	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_s(portp->tx.buf, STL_TXBUFSIZE);		portp->tx.buf = (char *) NULL;		portp->tx.head = (char *) NULL;		portp->tx.tail = (char *) NULL;	}	set_bit(TTY_IO_ERROR, &tty->flags);	if (tty->ldisc.flush_buffer)		(tty->ldisc.flush_buffer)(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_CALLOUT_ACTIVE | ASYNC_NORMAL_ACTIVE |		ASYNC_CLOSING);	wake_up_interruptible(&portp->close_wait);	MOD_DEC_USE_COUNT;	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){#if DEBUG	printk("stl_delay(len=%d)\n", len);#endif	if (len > 0) {		current->state = TASK_INTERRUPTIBLE;		schedule_timeout(len);		current->state = TASK_RUNNING;	}}/*****************************************************************************//* *	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;#if 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);		copy_from_user(stl_tmpwritebuf, chbuf, count);		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;#if 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;#if 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;#if 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;#if DEBUG

⌨️ 快捷键说明

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