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

📄 serial_amba.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
{	struct amba_state *state = info->state;	struct amba_port *port = info->port;	struct serial_struct tmp;	memset(&tmp, 0, sizeof(tmp));	tmp.type	   = 0;	tmp.line	   = state->line;	tmp.port	   = port->uart_base;	if (HIGH_BITS_OFFSET)		tmp.port_high = port->uart_base >> HIGH_BITS_OFFSET;	tmp.irq		   = port->irq;	tmp.flags	   = 0;	tmp.xmit_fifo_size = port->fifosize;	tmp.baud_base	   = port->uartclk / 16;	tmp.close_delay	   = state->close_delay;	tmp.closing_wait   = state->closing_wait;	tmp.custom_divisor = state->custom_divisor;	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))		return -EFAULT;	return 0;}static int set_serial_info(struct amba_info *info,			   struct serial_struct *newinfo){	struct serial_struct new_serial;	struct amba_state *state, old_state;	struct amba_port *port;	unsigned long new_port;	unsigned int i, change_irq, change_port;	int retval = 0;	if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))		return -EFAULT;	state = info->state;	old_state = *state;	port = info->port;	new_port = new_serial.port;	if (HIGH_BITS_OFFSET)		new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;	change_irq  = new_serial.irq != port->irq;	change_port = new_port != port->uart_base;	if (!capable(CAP_SYS_ADMIN)) {		if (change_irq || change_port ||		    (new_serial.baud_base != port->uartclk / 16) ||		    (new_serial.close_delay != state->close_delay) ||		    (new_serial.xmit_fifo_size != port->fifosize) ||		    ((new_serial.flags & ~ASYNC_USR_MASK) !=		     (state->flags & ~ASYNC_USR_MASK)))			return -EPERM;		state->flags = ((state->flags & ~ASYNC_USR_MASK) |				(new_serial.flags & ASYNC_USR_MASK));		info->flags = ((info->flags & ~ASYNC_USR_MASK) |			       (new_serial.flags & ASYNC_USR_MASK));		state->custom_divisor = new_serial.custom_divisor;		goto check_and_exit;	}	if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||	    (new_serial.baud_base < 9600))		return -EINVAL;	if (new_serial.type && change_port) {		for (i = 0; i < SERIAL_AMBA_NR; i++)			if ((port != amba_ports + i) &&			    amba_ports[i].uart_base != new_port)				return -EADDRINUSE;	}	if ((change_port || change_irq) && (state->count > 1))		return -EBUSY;	/*	 * OK, past this point, all the error checking has been done.	 * At this point, we start making changes.....	 */	port->uartclk = new_serial.baud_base * 16;	state->flags = ((state->flags & ~ASYNC_FLAGS) |			(new_serial.flags & ASYNC_FLAGS));	info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |		       (info->flags & ASYNC_INTERNAL_FLAGS));	state->custom_divisor = new_serial.custom_divisor;	state->close_delay = new_serial.close_delay * HZ / 100;	state->closing_wait = new_serial.closing_wait * HZ / 100;	info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;	port->fifosize = new_serial.xmit_fifo_size;	if (change_port || change_irq) {		/*		 * We need to shutdown the serial port at the old		 * port/irq combination.		 */		ambauart_shutdown(info);		port->irq = new_serial.irq;		port->uart_base = new_port;	}check_and_exit:	if (!port->uart_base)		return 0;	if (info->flags & ASYNC_INITIALIZED) {		if ((old_state.flags & ASYNC_SPD_MASK) !=		    (state->flags & ASYNC_SPD_MASK) ||		    (old_state.custom_divisor != state->custom_divisor)) {			if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)				info->tty->alt_speed = 57600;			if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)				info->tty->alt_speed = 115200;			if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)				info->tty->alt_speed = 230400;			if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)				info->tty->alt_speed = 460800;			ambauart_change_speed(info, NULL);		}	} else		retval = ambauart_startup(info);	return retval;}/* * get_lsr_info - get line status register info */static int get_lsr_info(struct amba_info *info, unsigned int *value){	unsigned int result, status;	unsigned long flags;	save_flags(flags); cli();	status = UART_GET_FR(info->port);	restore_flags(flags);	result = status & AMBA_UARTFR_BUSY ? TIOCSER_TEMT : 0;	/*	 * If we're about to load something into the transmit	 * register, we'll pretend the transmitter isn't empty to	 * avoid a race condition (depending on when the transmit	 * interrupt happens).	 */	if (info->x_char ||	    ((CIRC_CNT(info->xmit.head, info->xmit.tail,		       AMBA_XMIT_SIZE) > 0) &&	     !info->tty->stopped && !info->tty->hw_stopped))		result &= TIOCSER_TEMT;		return put_user(result, value);}static int get_modem_info(struct amba_info *info, unsigned int *value){	unsigned int result = info->mctrl;	unsigned int status;	status = UART_GET_FR(info->port);	if (status & AMBA_UARTFR_DCD)		result |= TIOCM_CAR;	if (status & AMBA_UARTFR_DSR)		result |= TIOCM_DSR;	if (status & AMBA_UARTFR_CTS)		result |= TIOCM_CTS;	return put_user(result, value);}static int set_modem_info(struct amba_info *info, unsigned int cmd,			  unsigned int *value){	unsigned int arg, old;	unsigned long flags;	if (get_user(arg, value))		return -EFAULT;	old = info->mctrl;	switch (cmd) {	case TIOCMBIS:		info->mctrl |= arg;		break;	case TIOCMBIC:		info->mctrl &= ~arg;		break;	case TIOCMSET:		info->mctrl = arg;		break;	default:		return -EINVAL;	}	save_flags(flags); cli();	if (old != info->mctrl)		info->port->set_mctrl(info->port, info->mctrl);	restore_flags(flags);	return 0;}static void ambauart_break_ctl(struct tty_struct *tty, int break_state){	struct amba_info *info = tty->driver_data;	unsigned long flags;	unsigned int lcr_h;	save_flags(flags); cli();	lcr_h = UART_GET_LCRH(info->port);	if (break_state == -1)		lcr_h |= AMBA_UARTLCR_H_BRK;	else		lcr_h &= ~AMBA_UARTLCR_H_BRK;	UART_PUT_LCRH(info->port, lcr_h);	restore_flags(flags);}static int ambauart_ioctl(struct tty_struct *tty, struct file *file,			   unsigned int cmd, unsigned long arg){	struct amba_info *info = tty->driver_data;	struct amba_icount cprev, cnow;	struct serial_icounter_struct icount;	unsigned long flags;	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&	    (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {		if (tty->flags & (1 << TTY_IO_ERROR))			return -EIO;	}	switch (cmd) {		case TIOCMGET:			return get_modem_info(info, (unsigned int *)arg);		case TIOCMBIS:		case TIOCMBIC:		case TIOCMSET:			return set_modem_info(info, cmd, (unsigned int *)arg);		case TIOCGSERIAL:			return get_serial_info(info,					       (struct serial_struct *)arg);		case TIOCSSERIAL:			return set_serial_info(info,					       (struct serial_struct *)arg);		case TIOCSERGETLSR: /* Get line status register */			return get_lsr_info(info, (unsigned int *)arg);		/*		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change		 * - mask passed in arg for lines of interest		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)		 * Caller should use TIOCGICOUNT to see which one it was		 */		case TIOCMIWAIT:			save_flags(flags); cli();			/* note the counters on entry */			cprev = info->state->icount;			/* Force modem status interrupts on */			UART_PUT_CR(info->port, UART_GET_CR(info->port) | AMBA_UARTCR_MSIE);			restore_flags(flags);			while (1) {				interruptible_sleep_on(&info->delta_msr_wait);				/* see if a signal did it */				if (signal_pending(current))					return -ERESTARTSYS;				save_flags(flags); cli();				cnow = info->state->icount; /* atomic copy */				restore_flags(flags);				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)					return -EIO; /* no change => error */				if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||				    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||				    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||				    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {					return 0;				}				cprev = cnow;			}			/* NOTREACHED */		/*		 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)		 * Return: write counters to the user passed counter struct		 * NB: both 1->0 and 0->1 transitions are counted except for		 *     RI where only 0->1 is counted.		 */		case TIOCGICOUNT:			save_flags(flags); cli();			cnow = info->state->icount;			restore_flags(flags);			icount.cts = cnow.cts;			icount.dsr = cnow.dsr;			icount.rng = cnow.rng;			icount.dcd = cnow.dcd;			icount.rx  = cnow.rx;			icount.tx  = cnow.tx;			icount.frame = cnow.frame;			icount.overrun = cnow.overrun;			icount.parity = cnow.parity;			icount.brk = cnow.brk;			icount.buf_overrun = cnow.buf_overrun;			return copy_to_user((void *)arg, &icount, sizeof(icount))					? -EFAULT : 0;		default:			return -ENOIOCTLCMD;	}	return 0;}static void ambauart_set_termios(struct tty_struct *tty, struct termios *old_termios){	struct amba_info *info = tty->driver_data;	unsigned long flags;	unsigned int cflag = tty->termios->c_cflag;	if ((cflag ^ old_termios->c_cflag) == 0 &&	    RELEVENT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)		return;	ambauart_change_speed(info, old_termios);	/* Handle transition to B0 status */	if ((old_termios->c_cflag & CBAUD) &&	    !(cflag & CBAUD)) {		save_flags(flags); cli();		info->mctrl &= ~(TIOCM_RTS | TIOCM_DTR);		info->port->set_mctrl(info->port, info->mctrl);		restore_flags(flags);	}	/* Handle transition away from B0 status */	if (!(old_termios->c_cflag & CBAUD) &&	    (cflag & CBAUD)) {		save_flags(flags); cli();		info->mctrl |= TIOCM_DTR;		if (!(cflag & CRTSCTS) ||		    !test_bit(TTY_THROTTLED, &tty->flags))			info->mctrl |= TIOCM_RTS;		info->port->set_mctrl(info->port, info->mctrl);		restore_flags(flags);	}	/* Handle turning off CRTSCTS */	if ((old_termios->c_cflag & CRTSCTS) &&	    !(cflag & CRTSCTS)) {		tty->hw_stopped = 0;		ambauart_start(tty);	}#if 0	/*	 * No need to wake up processes in open wait, since they	 * sample the CLOCAL flag once, and don't recheck it.	 * XXX  It's not clear whether the current behavior is correct	 * or not.  Hence, this may change.....	 */	if (!(old_termios->c_cflag & CLOCAL) &&	    (tty->termios->c_cflag & CLOCAL))		wake_up_interruptible(&info->open_wait);#endif}static void ambauart_close(struct tty_struct *tty, struct file *filp){	struct amba_info *info = tty->driver_data;	struct amba_state *state;	unsigned long flags;	if (!info)		return;	state = info->state;#if DEBUG	printk("ambauart_close() called\n");#endif	save_flags(flags); cli();	if (tty_hung_up_p(filp)) {		MOD_DEC_USE_COUNT;		restore_flags(flags);		return;	}	if ((tty->count == 1) && (state->count != 1)) {		/*		 * Uh, oh.  tty->count is 1, which means that the tty		 * structure will be freed.  state->count should always		 * be one in these conditions.  If it's greater than		 * one, we've got real problems, since it means the		 * serial port won't be shutdown.		 */		printk("ambauart_close: bad serial port count; tty->count is 1, "		       "state->count is %d\n", state->count);		state->count = 1;	}	if (--state->count < 0) {		printk("rs_close: bad serial port count for %s%d: %d\n",		       tty->driver.name, info->state->line, state->count);		state->count = 0;	}	if (state->count) {		MOD_DEC_USE_COUNT;		restore_flags(flags);		return;	}	info->flags |= ASYNC_CLOSING;	restore_flags(flags);	/*	 * Save the termios structure, since this port may have	 * separate termios for callout and dialin.	 */	if (info->flags & ASYNC_NORMAL_ACTIVE)		info->state->normal_termios = *tty->termios;	if (info->flags & ASYNC_CALLOUT_ACTIVE)		info->state->callout_termios = *tty->termios;	/*	 * Now we wait for the transmit buffer to clear; and we notify	 * the line discipline to only process XON/XOFF characters.	 */	tty->closing = 1;	if (info->state->closing_wait != ASYNC_CLOSING_WAIT_NONE)		tty_wait_until_sent(tty, info->state->closing_wait);	/*	 * At this point, we stop accepting input.  To do this, we	 * disable the receive line status interrupts.	 */	if (info->flags & ASYNC_INITIALIZED) {		ambauart_disable_rx_interrupt(info);		/*		 * Before we drop DTR, make sure the UART transmitter		 * has completely drained; this is especially		 * important if there is a transmit FIFO!		 */		ambauart_wait_until_sent(tty, info->timeout);	}	ambauart_shutdown(info);	if (tty->driver.flush_buffer)		tty->driver.flush_buffer(tty);	if (tty->ldisc.flush_buffer)		tty->ldisc.flush_buffer(tty);	tty->closing = 0;	info->event = 0;	info->tty = NULL;	if (info->blocked_open) {		if (info->state->close_delay) {			set_current_state(TASK_INTERRUPTIBLE);			schedule_timeout(info->state->close_delay);		}		wake_up_interruptible(&info->open_wait);	}	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|			 ASYNC_CLOSING);	wake_up_interruptible(&info->close_wait);	MOD_DEC_USE_COUNT;}static void ambauart_wait_until_sent(struct tty_struct *tty, int timeout){	struct amba_info *info = (struct amba_info *) tty->driver_data;	unsigned long char_time, expire;	unsigned int status;	if (info->port->fifosize == 0)		return;	/*	 * Set the check interval to be 1/5 of the estimated time to	 * send a single character, and make it at least 1.  The check	 * interval should also be less than the timeout.	 *	 * Note: we have to use pretty tight timings here to satisfy	 * the NIST-PCTS.	 */	char_time = (info->timeout - HZ/50) / info->port->fifosize;	char_time = char_time / 5;	if (char_time == 0)		char_time = 1;	if (timeout && timeout < char_time)		char_time = timeout;	/*	 * If the transmitter hasn't cleared in twice the approximate	 * amount of time to send the entire FIFO, it probably won't	 * ever clear.  This assumes the UART isn't doing flow	 * control, which is currently the case.  Hence, if it ever	 * takes longer than info->timeout, this is probably due to a	 * UART bug of some kind.  So, we clamp the timeout parameter at	 * 2*info->timeout.	 */	if (!timeout || timeout > 2 * info->timeout)		timeout = 2 * info->timeout;	expire = jiffies + timeout;#if DEBUG	printk("ambauart_wait_until_sent(%d), jiff=%lu, expire=%lu...\n",	       MINOR(tty->device) - tty->driver.minor_start, jiffies,	       expire);#endif	while (UART_GET_FR(info->port) & AMBA_UARTFR_BUSY) {		set_current_state(TASK_INTERRUPTIBLE);		schedule_timeout(char_time);		if (signal_pending(current))

⌨️ 快捷键说明

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