riscom8.c

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

C
1,842
字号
				rc_check_modem(bp);			else				printk(KERN_WARNING "rc%d: Bad modem ack "						    "0x%02x.\n",				       board_No(bp), ack);				} 		rc_out(bp, CD180_EOIR, 0);   /* Mark end of interrupt */		rc_out(bp, RC_CTOUT, 0);     /* Clear timeout flag    */	}	return IRQ_RETVAL(handled);}/* *  Routines for open & close processing. *//* Called with disabled interrupts */static inline int rc_setup_board(struct riscom_board * bp){	int error;	if (bp->flags & RC_BOARD_ACTIVE) 		return 0;		error = request_irq(bp->irq, rc_interrupt, SA_INTERRUPT,			    "RISCom/8", NULL);	if (error) 		return error;		rc_out(bp, RC_CTOUT, 0);       		/* Just in case         */	bp->DTR = ~0;	rc_out(bp, RC_DTR, bp->DTR);	        /* Drop DTR on all ports */		IRQ_to_board[bp->irq] = bp;	bp->flags |= RC_BOARD_ACTIVE;		return 0;}/* Called with disabled interrupts */static inline void rc_shutdown_board(struct riscom_board *bp){	if (!(bp->flags & RC_BOARD_ACTIVE))		return;		bp->flags &= ~RC_BOARD_ACTIVE;		free_irq(bp->irq, NULL);	IRQ_to_board[bp->irq] = NULL;		bp->DTR = ~0;	rc_out(bp, RC_DTR, bp->DTR);	       /* Drop DTR on all ports */	}/* * Setting up port characteristics.  * Must be called with disabled interrupts */static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port){	struct tty_struct *tty;	unsigned long baud;	long tmp;	unsigned char cor1 = 0, cor3 = 0;	unsigned char mcor1 = 0, mcor2 = 0;		if (!(tty = port->tty) || !tty->termios)		return;	port->IER  = 0;	port->COR2 = 0;	port->MSVR = MSVR_RTS;		baud = C_BAUD(tty);		if (baud & CBAUDEX) {		baud &= ~CBAUDEX;		if (baud < 1 || baud > 2) 			port->tty->termios->c_cflag &= ~CBAUDEX;		else			baud += 15;	}	if (baud == 15)  {		if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)			baud ++;		if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)			baud += 2;	}		/* Select port on the board */	rc_out(bp, CD180_CAR, port_No(port));		if (!baud_table[baud])  {		/* Drop DTR & exit */		bp->DTR |= (1u << port_No(port));		rc_out(bp, RC_DTR, bp->DTR);		return;	} else  {		/* Set DTR on */		bp->DTR &= ~(1u << port_No(port));		rc_out(bp, RC_DTR, bp->DTR);	}		/*	 * Now we must calculate some speed depended things 	 */		/* Set baud rate for port */	tmp = (((RC_OSCFREQ + baud_table[baud]/2) / baud_table[baud] +		CD180_TPC/2) / CD180_TPC);	rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff); 	rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff); 	rc_out(bp, CD180_RBPRL, tmp & 0xff); 	rc_out(bp, CD180_TBPRL, tmp & 0xff);		baud = (baud_table[baud] + 5) / 10;   /* Estimated CPS */		/* Two timer ticks seems enough to wakeup something like SLIP driver */	tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;			port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?					      SERIAL_XMIT_SIZE - 1 : tmp);		/* Receiver timeout will be transmission time for 1.5 chars */	tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;	tmp = (tmp > 0xff) ? 0xff : tmp;	rc_out(bp, CD180_RTPR, tmp);		switch (C_CSIZE(tty))  {	 case CS5:		cor1 |= COR1_5BITS;		break;	 case CS6:		cor1 |= COR1_6BITS;		break;	 case CS7:		cor1 |= COR1_7BITS;		break;	 case CS8:		cor1 |= COR1_8BITS;		break;	}		if (C_CSTOPB(tty)) 		cor1 |= COR1_2SB;		cor1 |= COR1_IGNORE;	if (C_PARENB(tty))  {		cor1 |= COR1_NORMPAR;		if (C_PARODD(tty)) 			cor1 |= COR1_ODDP;		if (I_INPCK(tty)) 			cor1 &= ~COR1_IGNORE;	}	/* Set marking of some errors */	port->mark_mask = RCSR_OE | RCSR_TOUT;	if (I_INPCK(tty)) 		port->mark_mask |= RCSR_FE | RCSR_PE;	if (I_BRKINT(tty) || I_PARMRK(tty)) 		port->mark_mask |= RCSR_BREAK;	if (I_IGNPAR(tty)) 		port->mark_mask &= ~(RCSR_FE | RCSR_PE);	if (I_IGNBRK(tty))  {		port->mark_mask &= ~RCSR_BREAK;		if (I_IGNPAR(tty)) 			/* Real raw mode. Ignore all */			port->mark_mask &= ~RCSR_OE;	}	/* Enable Hardware Flow Control */	if (C_CRTSCTS(tty))  {#ifdef RISCOM_BRAIN_DAMAGED_CTS		port->IER |= IER_DSR | IER_CTS;		mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;		mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;		tty->hw_stopped = !(rc_in(bp, CD180_MSVR) & (MSVR_CTS|MSVR_DSR));#else		port->COR2 |= COR2_CTSAE;#endif	}	/* Enable Software Flow Control. FIXME: I'm not sure about this */	/* Some people reported that it works, but I still doubt */	if (I_IXON(tty))  {		port->COR2 |= COR2_TXIBE;		cor3 |= (COR3_FCT | COR3_SCDE);		if (I_IXANY(tty))			port->COR2 |= COR2_IXM;		rc_out(bp, CD180_SCHR1, START_CHAR(tty));		rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));		rc_out(bp, CD180_SCHR3, START_CHAR(tty));		rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));	}	if (!C_CLOCAL(tty))  {		/* Enable CD check */		port->IER |= IER_CD;		mcor1 |= MCOR1_CDZD;		mcor2 |= MCOR2_CDOD;	}		if (C_CREAD(tty)) 		/* Enable receiver */		port->IER |= IER_RXD;		/* Set input FIFO size (1-8 bytes) */	cor3 |= RISCOM_RXFIFO; 	/* Setting up CD180 channel registers */	rc_out(bp, CD180_COR1, cor1);	rc_out(bp, CD180_COR2, port->COR2);	rc_out(bp, CD180_COR3, cor3);	/* Make CD180 know about registers change */	rc_wait_CCR(bp);	rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);	/* Setting up modem option registers */	rc_out(bp, CD180_MCOR1, mcor1);	rc_out(bp, CD180_MCOR2, mcor2);	/* Enable CD180 transmitter & receiver */	rc_wait_CCR(bp);	rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);	/* Enable interrupts */	rc_out(bp, CD180_IER, port->IER);	/* And finally set RTS on */	rc_out(bp, CD180_MSVR, port->MSVR);}/* Must be called with interrupts enabled */static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port){	unsigned long flags;		if (port->flags & ASYNC_INITIALIZED)		return 0;		if (!port->xmit_buf) {		/* We may sleep in get_zeroed_page() */		unsigned long tmp;				if (!(tmp = get_zeroed_page(GFP_KERNEL)))			return -ENOMEM;		    		if (port->xmit_buf) {			free_page(tmp);			return -ERESTARTSYS;		}		port->xmit_buf = (unsigned char *) tmp;	}			save_flags(flags); cli();			if (port->tty) 		clear_bit(TTY_IO_ERROR, &port->tty->flags);			if (port->count == 1) 		bp->count++;			port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;	rc_change_speed(bp, port);	port->flags |= ASYNC_INITIALIZED;			restore_flags(flags);	return 0;}/* Must be called with interrupts disabled */static void rc_shutdown_port(struct riscom_board *bp, struct riscom_port *port){	struct tty_struct *tty;		if (!(port->flags & ASYNC_INITIALIZED)) 		return;	#ifdef RC_REPORT_OVERRUN	printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",	       board_No(bp), port_No(port), port->overrun);#endif	#ifdef RC_REPORT_FIFO	{		int i;				printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",		       board_No(bp), port_No(port));		for (i = 0; i < 10; i++)  {			printk("%ld ", port->hits[i]);		}		printk("].\n");	}#endif		if (port->xmit_buf)  {		free_page((unsigned long) port->xmit_buf);		port->xmit_buf = NULL;	}	if (!(tty = port->tty) || C_HUPCL(tty))  {		/* Drop DTR */		bp->DTR |= (1u << port_No(port));		rc_out(bp, RC_DTR, bp->DTR);	}	        /* Select port */	rc_out(bp, CD180_CAR, port_No(port));	/* Reset port */	rc_wait_CCR(bp);	rc_out(bp, CD180_CCR, CCR_SOFTRESET);	/* Disable all interrupts from this port */	port->IER = 0;	rc_out(bp, CD180_IER, port->IER);		if (tty)  		set_bit(TTY_IO_ERROR, &tty->flags);	port->flags &= ~ASYNC_INITIALIZED;		if (--bp->count < 0)  {		printk(KERN_INFO "rc%d: rc_shutdown_port: "				 "bad board count: %d\n",		       board_No(bp), bp->count);		bp->count = 0;	}		/*	 * If this is the last opened port on the board	 * shutdown whole board	 */	if (!bp->count) 		rc_shutdown_board(bp);}	static int block_til_ready(struct tty_struct *tty, struct file * filp,			   struct riscom_port *port){	DECLARE_WAITQUEUE(wait, current);	struct riscom_board *bp = port_Board(port);	int    retval;	int    do_clocal = 0;	int    CD;	/*	 * If the device is in the middle of being closed, then block	 * until it's done, and then try again.	 */	if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {		interruptible_sleep_on(&port->close_wait);		if (port->flags & ASYNC_HUP_NOTIFY)			return -EAGAIN;		else			return -ERESTARTSYS;	}	/*	 * If non-blocking mode is set, or the port is not enabled,	 * then make the check up front and then exit.	 */	if ((filp->f_flags & O_NONBLOCK) ||	    (tty->flags & (1 << TTY_IO_ERROR))) {		port->flags |= ASYNC_NORMAL_ACTIVE;		return 0;	}	if (C_CLOCAL(tty))  		do_clocal = 1;	/*	 * Block waiting for the carrier detect and the line to become	 * free (i.e., not in use by the callout).  While we are in	 * this loop, info->count is dropped by one, so that	 * rs_close() knows when to free things.  We restore it upon	 * exit, either normal or abnormal.	 */	retval = 0;	add_wait_queue(&port->open_wait, &wait);	cli();	if (!tty_hung_up_p(filp))		port->count--;	sti();	port->blocked_open++;	while (1) {		cli();		rc_out(bp, CD180_CAR, port_No(port));		CD = rc_in(bp, CD180_MSVR) & MSVR_CD;		rc_out(bp, CD180_MSVR, MSVR_RTS);		bp->DTR &= ~(1u << port_No(port));		rc_out(bp, RC_DTR, bp->DTR);		sti();		set_current_state(TASK_INTERRUPTIBLE);		if (tty_hung_up_p(filp) ||		    !(port->flags & ASYNC_INITIALIZED)) {			if (port->flags & ASYNC_HUP_NOTIFY)				retval = -EAGAIN;			else				retval = -ERESTARTSYS;				break;		}		if (!(port->flags & ASYNC_CLOSING) &&		    (do_clocal || CD))			break;		if (signal_pending(current)) {			retval = -ERESTARTSYS;			break;		}		schedule();	}	current->state = TASK_RUNNING;	remove_wait_queue(&port->open_wait, &wait);	if (!tty_hung_up_p(filp))		port->count++;	port->blocked_open--;	if (retval)		return retval;		port->flags |= ASYNC_NORMAL_ACTIVE;	return 0;}	static int rc_open(struct tty_struct * tty, struct file * filp){	int board;	int error;	struct riscom_port * port;	struct riscom_board * bp;		board = RC_BOARD(tty->index);	if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))		return -ENODEV;		bp = &rc_board[board];	port = rc_port + board * RC_NPORT + RC_PORT(tty->index);	if (rc_paranoia_check(port, tty->name, "rc_open"))		return -ENODEV;		if ((error = rc_setup_board(bp))) 		return error;			port->count++;	tty->driver_data = port;	port->tty = tty;		if ((error = rc_setup_port(bp, port))) 		return error;		if ((error = block_til_ready(tty, filp, port)))		return error;		return 0;}static void rc_close(struct tty_struct * tty, struct file * filp){	struct riscom_port *port = (struct riscom_port *) tty->driver_data;	struct riscom_board *bp;	unsigned long flags;	unsigned long timeout;		if (!port || rc_paranoia_check(port, tty->name, "close"))		return;		save_flags(flags); cli();	if (tty_hung_up_p(filp))		goto out;		bp = port_Board(port);	if ((tty->count == 1) && (port->count != 1))  {		printk(KERN_INFO "rc%d: rc_close: bad port count;"		       " tty->count is 1, port count is %d\n",		       board_No(bp), port->count);		port->count = 1;	}	if (--port->count < 0)  {		printk(KERN_INFO "rc%d: rc_close: bad port count "				 "for tty%d: %d\n",		       board_No(bp), port_No(port), port->count);		port->count = 0;	}	if (port->count)		goto out;	port->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 (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)		tty_wait_until_sent(tty, port->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.	 */	port->IER &= ~IER_RXD;	if (port->flags & ASYNC_INITIALIZED) {		port->IER &= ~IER_TXRDY;		port->IER |= IER_TXEMPTY;		rc_out(bp, CD180_CAR, port_No(port));		rc_out(bp, CD180_IER, port->IER);		/*		 * Before we drop DTR, make sure the UART transmitter		 * has completely drained; this is especially		 * important if there is a transmit FIFO!		 */		timeout = jiffies+HZ;		while(port->IER & IER_TXEMPTY)  {			current->state = TASK_INTERRUPTIBLE; 			schedule_timeout(port->timeout);			if (time_after(jiffies, timeout))				break;		}	}	rc_shutdown_port(bp, port);	if (tty->driver->flush_buffer)		tty->driver->flush_buffer(tty);	tty_ldisc_flush(tty);	tty->closing = 0;	port->event = 0;	port->tty = NULL;	if (port->blocked_open) {		if (port->close_delay) {			current->state = TASK_INTERRUPTIBLE;			schedule_timeout(port->close_delay);		}		wake_up_interruptible(&port->open_wait);	}	port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);	wake_up_interruptible(&port->close_wait);out:	restore_flags(flags);}static int rc_write(struct tty_struct * tty, int from_user, 		    const unsigned char *buf, int count){	struct riscom_port *port = (struct riscom_port *)tty->driver_data;	struct riscom_board *bp;	int c, total = 0;	unsigned long flags;					if (rc_paranoia_check(port, tty->name, "rc_write"))		return 0;		bp = port_Board(port);	if (!tty || !port->xmit_buf || !tmp_buf)		return 0;	save_flags(flags);	if (from_user) {		down(&tmp_buf_sem);		while (1) {			cli();					c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,						  SERIAL_XMIT_SIZE - port->xmit_head));			if (c <= 0)				break;			c -= copy_from_user(tmp_buf, buf, c);			if (!c) {				if (!total)					total = -EFAULT;				break;			}			cli();			c = min_t(int, c, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,					 SERIAL_XMIT_SIZE - port->xmit_head));			memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);			port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);			port->xmit_cnt += c;			restore_flags(flags);			buf += c;			count -= c;			total += c;		}		up(&tmp_buf_sem);	} else {		while (1) {			cli();					c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,						  SERIAL_XMIT_SIZE - port->xmit_head));			if (c <= 0) {				restore_flags(flags);				break;			}			memcpy(port->xmit_buf + port->xmit_head, buf, c);			port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);			port->xmit_cnt += c;			restore_flags(flags);			buf += c;			count -= c;			total += c;		}	}	cli();	if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&	    !(port->IER & IER_TXRDY)) {		port->IER |= IER_TXRDY;		rc_out(bp, CD180_CAR, port_No(port));		rc_out(bp, CD180_IER, port->IER);	}	restore_flags(flags);	return total;}static void rc_put_char(struct tty_struct * tty, unsigned char ch){	struct riscom_port *port = (struct riscom_port *)tty->driver_data;	unsigned long flags;	if (rc_paranoia_check(port, tty->name, "rc_put_char"))		return;

⌨️ 快捷键说明

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