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

📄 ftdi_sio.c

📁 linux-2.4.29操作系统的源码
💻 C
📖 第 1 页 / 共 5 页
字号:
			goto exit;		}		/* Allocate memory for the urb if necessary */		if (urb->transfer_buffer == NULL) {			urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);			if (urb->transfer_buffer == NULL) {				err("%s ran out of kernel memory for urb ...", __FUNCTION__);				goto exit;			}		}				/* The original sio needs the first byte to contain the bytecount 		 * so the urb may be one byte bigger than the user data 		 */		urb_byte_count = min (count + data_offset, URB_TRANSFER_BUFFER_SIZE);		user_byte_count = urb_byte_count - data_offset; 		/* Copy in the data to send */		if (from_user) {			if (copy_from_user(urb->transfer_buffer + data_offset,					   current_position, user_byte_count )){				return -EFAULT;			}		} else {			memcpy(urb->transfer_buffer + data_offset,			       current_position, user_byte_count );		}  		first_byte = urb->transfer_buffer;		if (data_offset > 0){			/* Write the control byte at the front of the packet*/			*first_byte = 1 | ((user_byte_count) << 2) ; 			dbg("%s Bytes: %d, First Byte: 0x%02x", __FUNCTION__,count, first_byte[0]);		}				usb_serial_debug_data (__FILE__, __FUNCTION__, urb_byte_count, first_byte);				/* fill the buffer and send it */		FILL_BULK_URB(urb, serial->dev, 			      usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),			      urb->transfer_buffer, urb_byte_count,			      ftdi_write_bulk_callback, port);		urb->transfer_flags |= USB_QUEUE_BULK;				/* uhci stack errors if it status set to -EINPROGRESS on */		/* urb submission, so clear status before submission.    */		spin_lock_irqsave (&(priv->write_urb_pool_lock), flags) ; 		urb->status = 0;		result = usb_submit_urb(urb);		if (result) {			spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);			err("%s - failed submitting write urb, error %d", __FUNCTION__, result);			user_bytes_sent = result;			goto exit;		}		spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);		/* house keeping */		current_position += user_byte_count;		user_bytes_sent += user_byte_count;		count -= user_byte_count;			} /* while  count > 0 */ exit:	dbg("%s write returning: %d", __FUNCTION__, user_bytes_sent);	return user_bytes_sent;} /* ftdi_write *//* This function may get called when the device is closed */static void ftdi_write_bulk_callback (struct urb *urb){	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;	if (port_paranoia_check (port, __FUNCTION__))		return;		dbg("%s - port %d", __FUNCTION__, port->number);		if (urb->status) {		dbg("nonzero write bulk status received: %d", urb->status);		return;	} 	if (port->open_count > 0){		queue_task(&port->tqueue, &tq_immediate);		mark_bh(IMMEDIATE_BH);	}} /* ftdi_write_bulk_callback */static int ftdi_write_room( struct usb_serial_port *port ){	struct ftdi_private *priv = (struct ftdi_private *)port->private;	int room = 0;	int i;	unsigned long flags;	spin_lock_irqsave (&priv->write_urb_pool_lock, flags);	for (i = 0; i < NUM_URBS && priv->write_urb_pool[i]; i++) {		if (priv->write_urb_pool[i]->status != -EINPROGRESS) {			room += URB_TRANSFER_BUFFER_SIZE - priv->write_offset;		}	}		spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);	dbg("%s - returns %d", __FUNCTION__, room);		return(room);} /* ftdi_write_room */static int ftdi_chars_in_buffer (struct usb_serial_port *port){ /* ftdi_chars_in_buffer */	unsigned long flags;	int i;	int chars = 0;	struct ftdi_private *priv = (struct ftdi_private *)port->private;	int data_offset = priv->write_offset;	dbg("%s - port %d", __FUNCTION__, port->number);	spin_lock_irqsave (&priv->write_urb_pool_lock, flags);	/* tally up the number of bytes waiting */	for (i = 0; i < NUM_URBS && priv->write_urb_pool[i]; i++) {		if (priv->write_urb_pool[i]->status == -EINPROGRESS) {			chars += URB_TRANSFER_BUFFER_SIZE - data_offset;		}	}	spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);	dbg("%s - returns %d", __FUNCTION__, chars);	return (chars);} /* ftdi_chars_in_buffer */static void ftdi_read_bulk_callback (struct urb *urb){ /* ftdi_read_bulk_callback */	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;	struct usb_serial *serial;	struct tty_struct *tty;	struct ftdi_private *priv;	char error_flag;       	unsigned char *data = urb->transfer_buffer;	int i;	int result;	int need_flip;	int packet_offset;	if (urb->number_of_packets > 0) {		err("%s transfer_buffer_length %d actual_length %d number of packets %d",__FUNCTION__,		    urb->transfer_buffer_length, urb->actual_length, urb->number_of_packets );		err("%s transfer_flags %x USB_QUEUE_BULK %x ", __FUNCTION__,urb->transfer_flags, USB_QUEUE_BULK );	}	dbg("%s", __FUNCTION__);	if (port_paranoia_check (port, __FUNCTION__)) {		return;	}	if (port->open_count <= 0)		return;	serial = get_usb_serial(port,__FUNCTION__);	if (!serial){		dbg("%s - bad serial pointer - exiting",__FUNCTION__);		return;	}		tty = port->tty;	if (!tty) {		dbg("%s - bad tty pointer - exiting",__FUNCTION__);		return;	}	priv = (struct ftdi_private *) port->private;	if (urb->status) {		/* This will happen at close every time so it is a dbg not an err */		dbg("(this is ok on close) nonzero read bulk status received: %d", urb->status);		return;	}        /* The first two bytes of every read packet are status */	if (urb->actual_length > 2) {		usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);	} else {                dbg("Status only: %03oo %03oo",data[0],data[1]);        }	/* TO DO -- check for hung up line and handle appropriately: */	/*   send hangup  */	/* See acm.c - you do a tty_hangup  - eg tty_hangup(tty) */	/* if CD is dropped and the line is not CLOCAL then we should hangup */	need_flip = 0;	for (packet_offset=0; packet_offset < urb->actual_length; packet_offset += PKTSZ) {		/* Compare new line status to the old one, signal if different */		if (priv != NULL) {			char new_status = data[packet_offset+0] & FTDI_STATUS_B0_MASK;			if (new_status != priv->prev_status) {				priv->diff_status |= new_status ^ priv->prev_status;				wake_up_interruptible(&priv->delta_msr_wait);				priv->prev_status = new_status;			}		}		/* Handle errors and break */		error_flag = TTY_NORMAL;		/* Although the device uses a bitmask and hence can have multiple */		/* errors on a packet - the order here sets the priority the */		/* error is returned to the tty layer  */		if ( data[packet_offset+1] & FTDI_RS_OE ) {			error_flag = TTY_OVERRUN;			dbg("OVERRRUN error");		}		if ( data[packet_offset+1] & FTDI_RS_BI ) {			error_flag = TTY_BREAK;			dbg("BREAK received");		}		if ( data[packet_offset+1] & FTDI_RS_PE ) {			error_flag = TTY_PARITY;			dbg("PARITY error");		}		if ( data[packet_offset+1] & FTDI_RS_FE ) {			error_flag = TTY_FRAME;			dbg("FRAMING error");		}		if (urb->actual_length > packet_offset + 2) {			for (i = 2; (i < PKTSZ) && ((i+packet_offset) < urb->actual_length); ++i) {				/* have to make sure we don't overflow the buffer				  with tty_insert_flip_char's */				if(tty->flip.count >= TTY_FLIPBUF_SIZE) {					tty_flip_buffer_push(tty);				}				/* Note that the error flag is duplicated for 				   every character received since we don't know				   which character it applied to */				tty_insert_flip_char(tty, data[packet_offset+i], error_flag);			}			need_flip = 1;		}#ifdef NOT_CORRECT_BUT_KEEPING_IT_FOR_NOW		/* if a parity error is detected you get status packets forever		   until a character is sent without a parity error.		   This doesn't work well since the application receives a never		   ending stream of bad data - even though new data hasn't been sent.		   Therefore I (bill) have taken this out.		   However - this might make sense for framing errors and so on 		   so I am leaving the code in for now.		*/		else {			if (error_flag != TTY_NORMAL){				dbg("error_flag is not normal");				/* In this case it is just status - if that is an error send a bad character */				if(tty->flip.count >= TTY_FLIPBUF_SIZE) {					tty_flip_buffer_push(tty);				}				tty_insert_flip_char(tty, 0xff, error_flag);				need_flip = 1;			}		}#endif	} /* "for(packet_offset=0..." */	/* Low latency */	if (need_flip) {		tty_flip_buffer_push(tty);	}	/* if the port is closed stop trying to read */	if (port->open_count > 0){		/* Continue trying to always read  */		FILL_BULK_URB(port->read_urb, serial->dev, 			      usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),			      port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,			      ftdi_read_bulk_callback, port);		result = usb_submit_urb(port->read_urb);		if (result)			err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);	}	return;} /* ftdi_read_bulk_callback */static void ftdi_break_ctl( struct usb_serial_port *port, int break_state ){	struct usb_serial *serial = port->serial;	struct ftdi_private *priv = (struct ftdi_private *)port->private;	__u16 urb_value = 0; 	char buf[1];		/* break_state = -1 to turn on break, and 0 to turn off break */	/* see drivers/char/tty_io.c to see it used */	/* last_set_data_urb_value NEVER has the break bit set in it */	if (break_state) {		urb_value = priv->last_set_data_urb_value | FTDI_SIO_SET_BREAK;	} else {		urb_value = priv->last_set_data_urb_value; 	}		if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),			    FTDI_SIO_SET_DATA_REQUEST, 			    FTDI_SIO_SET_DATA_REQUEST_TYPE,			    urb_value , 0,			    buf, 0, WDR_TIMEOUT) < 0) {		err("%s FAILED to enable/disable break state (state was %d)", __FUNCTION__,break_state);	}	   	dbg("%s break state is %d - urb is %d", __FUNCTION__,break_state, urb_value);	}/* old_termios contains the original termios settings and tty->termios contains * the new setting to be used * WARNING: set_termios calls this with old_termios in kernel space */static void ftdi_set_termios (struct usb_serial_port *port, struct termios *old_termios){ /* ftdi_termios */	struct usb_serial *serial = port->serial;	unsigned int cflag;	struct ftdi_private *priv = (struct ftdi_private *)port->private;		__u16 urb_value; /* will hold the new flags */	char buf[1]; /* Perhaps I should dynamically alloc this? */		// Added for xon/xoff support	unsigned int iflag = port->tty->termios->c_iflag;	unsigned char vstop;	unsigned char vstart;		dbg("%s", __FUNCTION__);	/* Force baud rate if this device requires it, unless it is set to B0. */	if (priv->force_baud && ((port->tty->termios->c_cflag & CBAUD) != B0)) {		dbg("%s: forcing baud rate for this device", __FUNCTION__);		port->tty->termios->c_cflag &= ~CBAUD;		port->tty->termios->c_cflag |= priv->force_baud;	}	/* Force RTS-CTS if this device requires it. */	if (priv->force_rtscts) {		dbg("%s: forcing rtscts for this device", __FUNCTION__);		port->tty->termios->c_cflag |= CRTSCTS;	}	cflag = port->tty->termios->c_cflag;	/* FIXME -For this cut I don't care

⌨️ 快捷键说明

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