amiserial.c

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

C
2,129
字号
static int get_serial_info(struct async_struct * info,			   struct serial_struct __user * retinfo){	struct serial_struct tmp;	struct serial_state *state = info->state;   	if (!retinfo)		return -EFAULT;	memset(&tmp, 0, sizeof(tmp));	tmp.type = state->type;	tmp.line = state->line;	tmp.port = state->port;	tmp.irq = state->irq;	tmp.flags = state->flags;	tmp.xmit_fifo_size = state->xmit_fifo_size;	tmp.baud_base = state->baud_base;	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 async_struct * info,			   struct serial_struct __user * new_info){	struct serial_struct new_serial; 	struct serial_state old_state, *state;	unsigned int		change_irq,change_port;	int 			retval = 0;	if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))		return -EFAULT;	state = info->state;	old_state = *state;  	change_irq = new_serial.irq != state->irq;	change_port = (new_serial.port != state->port);	if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))	  return -EINVAL;  	if (!serial_isroot()) {		if ((new_serial.baud_base != state->baud_base) ||		    (new_serial.close_delay != state->close_delay) ||		    (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||		    ((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.baud_base < 9600)		return -EINVAL;	/*	 * OK, past this point, all the error checking has been done.	 * At this point, we start making changes.....	 */	state->baud_base = new_serial.baud_base;	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;check_and_exit:	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;			change_speed(info, NULL);		}	} else		retval = startup(info);	return retval;}/* * get_lsr_info - get line status register info * * Purpose: Let user call ioctl() to get info when the UART physically * 	    is emptied.  On bus types like RS485, the transmitter must * 	    release the bus after transmitting. This must be done when * 	    the transmit shift register is empty, not be done when the * 	    transmit holding register is empty.  This functionality * 	    allows an RS485 driver to be written in user space.  */static int get_lsr_info(struct async_struct * info, unsigned int __user *value){	unsigned char status;	unsigned int result;	unsigned long flags;	local_irq_save(flags);	status = custom.serdatr;	mb();	local_irq_restore(flags);	result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);	if (copy_to_user(value, &result, sizeof(int)))		return -EFAULT;	return 0;}static int rs_tiocmget(struct tty_struct *tty, struct file *file){	struct async_struct * info = (struct async_struct *)tty->driver_data;	unsigned char control, status;	unsigned long flags;	if (serial_paranoia_check(info, tty->name, "rs_ioctl"))		return -ENODEV;	if (tty->flags & (1 << TTY_IO_ERROR))		return -EIO;	control = info->MCR;	local_irq_save(flags);	status = ciab.pra;	local_irq_restore(flags);	return    ((control & SER_RTS) ? TIOCM_RTS : 0)		| ((control & SER_DTR) ? TIOCM_DTR : 0)		| (!(status  & SER_DCD) ? TIOCM_CAR : 0)		| (!(status  & SER_DSR) ? TIOCM_DSR : 0)		| (!(status  & SER_CTS) ? TIOCM_CTS : 0);}static int rs_tiocmset(struct tty_struct *tty, struct file *file,		       unsigned int set, unsigned int clear){	struct async_struct * info = (struct async_struct *)tty->driver_data;	unsigned long flags;	if (serial_paranoia_check(info, tty->name, "rs_ioctl"))		return -ENODEV;	if (tty->flags & (1 << TTY_IO_ERROR))		return -EIO;	local_irq_save(flags);	if (set & TIOCM_RTS)		info->MCR |= SER_RTS;	if (set & TIOCM_DTR)		info->MCR |= SER_DTR;	if (clear & TIOCM_RTS)		info->MCR &= ~SER_RTS;	if (clear & TIOCM_DTR)		info->MCR &= ~SER_DTR;	rtsdtr_ctrl(info->MCR);	local_irq_restore(flags);	return 0;}/* * rs_break() --- routine which turns the break handling on or off */static void rs_break(struct tty_struct *tty, int break_state){	struct async_struct * info = (struct async_struct *)tty->driver_data;	unsigned long flags;	if (serial_paranoia_check(info, tty->name, "rs_break"))		return;	local_irq_save(flags);	if (break_state == -1)	  custom.adkcon = AC_SETCLR | AC_UARTBRK;	else	  custom.adkcon = AC_UARTBRK;	mb();	local_irq_restore(flags);}static int rs_ioctl(struct tty_struct *tty, struct file * file,		    unsigned int cmd, unsigned long arg){	struct async_struct * info = (struct async_struct *)tty->driver_data;	struct async_icount cprev, cnow;	/* kernel counter temps */	struct serial_icounter_struct icount;	void __user *argp = (void __user *)arg;	unsigned long flags;	if (serial_paranoia_check(info, tty->name, "rs_ioctl"))		return -ENODEV;	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 TIOCGSERIAL:			return get_serial_info(info, argp);		case TIOCSSERIAL:			return set_serial_info(info, argp);		case TIOCSERCONFIG:			return 0;		case TIOCSERGETLSR: /* Get line status register */			return get_lsr_info(info, argp);		case TIOCSERGSTRUCT:			if (copy_to_user(argp,					 info, sizeof(struct async_struct)))				return -EFAULT;			return 0;		/*		 * 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:			local_irq_save(flags);			/* note the counters on entry */			cprev = info->state->icount;			local_irq_restore(flags);			while (1) {				interruptible_sleep_on(&info->delta_msr_wait);				/* see if a signal did it */				if (signal_pending(current))					return -ERESTARTSYS;				local_irq_save(flags);				cnow = info->state->icount; /* atomic copy */				local_irq_restore(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:			local_irq_save(flags);			cnow = info->state->icount;			local_irq_restore(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;			if (copy_to_user(argp, &icount, sizeof(icount)))				return -EFAULT;			return 0;		case TIOCSERGWILD:		case TIOCSERSWILD:			/* "setserial -W" is called in Debian boot */			printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");			return 0;		default:			return -ENOIOCTLCMD;		}	return 0;}static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios){	struct async_struct *info = (struct async_struct *)tty->driver_data;	unsigned long flags;	unsigned int cflag = tty->termios->c_cflag;	change_speed(info, old_termios);	/* Handle transition to B0 status */	if ((old_termios->c_cflag & CBAUD) &&	    !(cflag & CBAUD)) {		info->MCR &= ~(SER_DTR|SER_RTS);		local_irq_save(flags);		rtsdtr_ctrl(info->MCR);		local_irq_restore(flags);	}	/* Handle transition away from B0 status */	if (!(old_termios->c_cflag & CBAUD) &&	    (cflag & CBAUD)) {		info->MCR |= SER_DTR;		if (!(tty->termios->c_cflag & CRTSCTS) || 		    !test_bit(TTY_THROTTLED, &tty->flags)) {			info->MCR |= SER_RTS;		}		local_irq_save(flags);		rtsdtr_ctrl(info->MCR);		local_irq_restore(flags);	}	/* Handle turning off CRTSCTS */	if ((old_termios->c_cflag & CRTSCTS) &&	    !(tty->termios->c_cflag & CRTSCTS)) {		tty->hw_stopped = 0;		rs_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}/* * ------------------------------------------------------------ * rs_close() *  * This routine is called when the serial port gets closed.  First, we * wait for the last remaining data to be sent.  Then, we unlink its * async structure from the interrupt chain if necessary, and we free * that IRQ if nothing is left in the chain. * ------------------------------------------------------------ */static void rs_close(struct tty_struct *tty, struct file * filp){	struct async_struct * info = (struct async_struct *)tty->driver_data;	struct serial_state *state;	unsigned long flags;	if (!info || serial_paranoia_check(info, tty->name, "rs_close"))		return;	state = info->state;	local_irq_save(flags);	if (tty_hung_up_p(filp)) {		DBG_CNT("before DEC-hung");		local_irq_restore(flags);		return;	}#ifdef SERIAL_DEBUG_OPEN	printk("rs_close ttys%d, count = %d\n", info->line, state->count);#endif	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("rs_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 ttys%d: %d\n",		       info->line, state->count);		state->count = 0;	}	if (state->count) {		DBG_CNT("before DEC-2");		local_irq_restore(flags);		return;	}	info->flags |= ASYNC_CLOSING;	/*	 * 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->closing_wait != ASYNC_CLOSING_WAIT_NONE)		tty_wait_until_sent(tty, info->closing_wait);	/*	 * At this point we stop accepting input.  To do this, we	 * disable the receive line status interrupts, and tell the	 * interrupt driver to stop checking the data ready bit in the	 * line status register.	 */	info->read_status_mask &= ~UART_LSR_DR;	if (info->flags & ASYNC_INITIALIZED) {	        /* disable receive interrupts */	        custom.intena = IF_RBF;		mb();		/* clear any pending receive interrupt */		custom.intreq = IF_RBF;		mb();		/*		 * Before we drop DTR, make sure the UART transmitter		 * has completely drained; this is especially		 * important if there is a transmit FIFO!		 */		rs_wait_until_sent(tty, info->timeout);	}	shutdown(info);	if (tty->driver->flush_buffer)		tty->driver->flush_buffer(tty);			tty_ldisc_flush(tty);	tty->closing = 0;	info->event = 0;	info->tty = NULL;	if (info->blocked_open) {		if (info->close_delay) {			msleep_interruptible(jiffies_to_msecs(info->close_delay));		}		wake_up_interruptible(&info->open_wait);	}	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);	wake_up_interruptible(&info->close_wait);	local_irq_restore(flags);}/* * rs_wait_until_sent() --- wait until the transmitter is empty */static void rs_wait_until_sent(struct tty_struct *tty, int timeout){	struct async_struct * info = (struct async_struct *)tty->driver_data;	unsigned long orig_jiffies, char_time;	int lsr;	if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))		return;	if (info->xmit_fifo_size == 0)		return; /* Just in case.... */	orig_jiffies = jiffies;	/*	 * 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->xmit_fifo_size;	char_time = char_time / 5;	if (char_time == 0)		char_time = 1;	if (timeout)	  char_time = min_t(unsigned long, 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;#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT	printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);	printk("jiff=%lu...", jiffies);#endif	while(!((lsr = custom.serdatr) & SDR_TSRE)) {#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT		printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);#endif		msleep_interruptible(jiffies_to_msecs(char_time));		if (signal_pending(current))			break;		if (timeout && time_after(jiffies, orig_jiffies + timeout))			break;	}	__set_current_state(TASK_RUNNING);#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT	printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);#endif}/* * rs_hangup() --- called by tty_hangup() when a hangup is signaled. */static void rs_hangup(struct tty_struct *tty){	struct async_struct * info = (struct async_struct *)tty->driver_data;	struct serial_state *state = info->state;	if (serial_paranoia_check(info, tty->name, "rs_hangup"))		return;	state = info->state;	rs_flush_buffer(tty);	shutdown(info);	info->event = 0;	state->count = 0;	info->flags &= ~ASYNC_NORMAL_ACTIVE;	info->tty = NULL;	wake_up_interruptible(&info->open_wait);}

⌨️ 快捷键说明

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