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

📄 ftdi_sio.c

📁 ep9315平台下USB驱动的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
	int result = 0;	char buf[1]; /* Needed for the usb_control_msg I think */	dbg("%s", __FUNCTION__);	port->tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0;	/* No error checking for this (will get errors later anyway) */	/* See ftdi_sio.h for description of what is reset */	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),			FTDI_SIO_RESET_REQUEST, FTDI_SIO_RESET_REQUEST_TYPE, 			FTDI_SIO_RESET_SIO, 			0, buf, 0, WDR_TIMEOUT);	/* Termios defaults are set by usb_serial_init. We don't change	   port->tty->termios - this would loose speed settings, etc.	   This is same behaviour as serial.c/rs_open() - Kuba */	/* ftdi_set_termios  will send usb control messages */	ftdi_set_termios(port, &tmp_termios);	/* FIXME: Flow control might be enabled, so it should be checked -	   we have no control of defaults! */	/* Turn on RTS and DTR since we are not flow controlling by default */	if (set_dtr(port, HIGH) < 0) {		err("%s Error from DTR HIGH urb", __FUNCTION__);	}	if (set_rts(port, HIGH) < 0){		err("%s Error from RTS HIGH urb", __FUNCTION__);	}	/* Make sure write_urb is initialised since a write_pool is used now */	port->write_urb = NULL; /* prevents usbserial.c from trying something silly */	/* Start reading from the device */	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 submitting read urb, error %d", __FUNCTION__, result);	return result;} /* ftdi_open *//*  * usbserial:__serial_close  only calls ftdi_close if the point is open * *   This only gets called when it is the last close *    *    */static void ftdi_close (struct usb_serial_port *port, struct file *filp){ /* ftdi_close */	struct usb_serial *serial;	unsigned int c_cflag = port->tty->termios->c_cflag;	char buf[1];	dbg("%s", __FUNCTION__);	serial = get_usb_serial ( port, __FUNCTION__);	if (!serial)		return;	if (serial->dev) {		if (c_cflag & HUPCL){			/* Disable flow control */			if (usb_control_msg(serial->dev, 					    usb_sndctrlpipe(serial->dev, 0),					    FTDI_SIO_SET_FLOW_CTRL_REQUEST,					    FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,					    0, 0, buf, 0, WDR_TIMEOUT) < 0) {				err("error from flowcontrol urb");			}	    			/* drop DTR */			if (set_dtr(port, LOW) < 0){				err("Error from DTR LOW urb");			}			/* drop RTS */			if (set_rts(port, LOW) < 0) {				err("Error from RTS LOW urb");			}				/* shutdown our bulk read */			if (port->read_urb) {				usb_unlink_urb (port->read_urb);				}			/* unlink the running write urbs */					} /* Note change no line is hupcl is off */	} /* if (serial->dev) */} /* ftdi_close */  /* The SIO requires the first byte to have: *  B0 1 *  B1 0 *  B2..7 length of message excluding byte 0 * * The new devices do not require this byte */static int ftdi_write (struct usb_serial_port *port, int from_user,			   const unsigned char *buf, int count){ /* ftdi_write */	struct usb_serial *serial = get_usb_serial ( port, __FUNCTION__);	struct ftdi_private *priv = (struct ftdi_private *)port->private;	unsigned char *first_byte;	int data_offset ;       /* will be 1 for the SIO and 0 otherwise */	int result;	int user_bytes_sent = 0 ;   /* amount of user data sent */	/* Variables for urb pool management */	unsigned char *current_position = (unsigned char *)buf;	int i; 	struct urb *urb; /* pointer to urb from urb pool */	unsigned long flags;	/* end of urb pool management */			dbg("%s port %d, %d bytes", __FUNCTION__, port->number, count);	if (count == 0) {		err("write request of 0 bytes");		goto exit;	}		data_offset = priv->write_offset;        dbg("data_offset set to %d",data_offset);	while (count > 0) {		/* urb_byte_count = user_byte_count + data_offset */		int urb_byte_count;  /* Number of bytes of URB data   */		int user_byte_count; /* Number of bytes of user data */		/* Find a free urb in the list */		urb = NULL;		spin_lock_irqsave (&(priv->write_urb_pool_lock), flags) ; 		for (i = 0 ; i < NUM_URBS; i++) {			if (priv->write_urb_pool[i] -> status != -EINPROGRESS) {			  urb = priv->write_urb_pool[i];			  /* Must make sure another device doesn't grab this */			  /* BUT unfortunately the uhci stack errors if it sees this */			  /* so have to increase the size of the spin_lock */			  /* urb->status = -EINPROGRESS; */				break;			}		}		if (urb == NULL) {		        spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);			dbg("%s - no more free urbs", __FUNCTION__);			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) {				spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);				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 )){				spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);				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;				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;	struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);	dbg("%s", __FUNCTION__);	if (port_paranoia_check (port, __FUNCTION__))		return;		if (urb->status) {		dbg("nonzero write bulk status received: %d", urb->status);		return;	}	if (!serial) {		dbg("%s - bad serial pointer, exiting", __FUNCTION__);		return;	}	/* Have to check for validity of queueing up the tasks */	dbg("%s - port->open_count = %d", __FUNCTION__, port->open_count); 	if (port->open_count > 0){		queue_task(&port->tqueue, &tq_immediate);		mark_bh(IMMEDIATE_BH);	} 	return;} /* 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; 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; ++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 = get_usb_serial(port,__FUNCTION__);       	struct tty_struct *tty = port->tty ;	struct ftdi_private *priv = (struct ftdi_private *) port->private;	char error_flag;       	unsigned char *data = urb->transfer_buffer;	const int data_offset = 2;	int i;	int result;	dbg("%s", __FUNCTION__);	if (port_paranoia_check (port, __FUNCTION__)) {		return;	}	if (port->open_count <= 0)		return;	if (!serial){		dbg("%s - bad serial pointer - exiting",__FUNCTION__);		return;	}		if (!tty) {		dbg("%s - bad tty pointer - exiting",__FUNCTION__);		return;	}	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 */	/* Compare new line status to the old one, signal if different */	if (priv != NULL) {		char new_status = data[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[1] & FTDI_RS_OE ) { 		error_flag = TTY_OVERRUN;                dbg("OVERRRUN error");	}	if ( data[1] & FTDI_RS_BI ) { 		error_flag = TTY_BREAK;                dbg("BREAK received");	}	if ( data[1] & FTDI_RS_PE ) { 		error_flag = TTY_PARITY;                dbg("PARITY error");	}	if ( data[1] & FTDI_RS_FE ) { 		error_flag = TTY_FRAME;                dbg("FRAMING error");	}	if (urb->actual_length > data_offset) {		for (i = data_offset ; i < 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[i], error_flag);		}	  	tty_flip_buffer_push(tty);	} #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

⌨️ 快捷键说明

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