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

📄 ftdi_sio.c

📁 NP-Convert,usb4口转换器的驱动开发示例.面向Linux.
💻 C
📖 第 1 页 / 共 5 页
字号:
 * baudrate (38400 gets mapped to 100000) */static int ftdi_HE_TIRA1_startup (struct usb_serial *serial){ /* ftdi_HE_TIRA1_startup */	struct ftdi_private *priv;	int err;	dbg("%s",__FUNCTION__);	err = ftdi_FT232BM_startup(serial);	if (err){		return (err);	}	priv = serial->port->private;	priv->flags |= ASYNC_SPD_CUST;	priv->custom_divisor = 240;	priv->force_baud = B38400;	priv->force_rtscts = 1;		return (0);} /* ftdi_HE_TIRA1_startup *//* Startup for user specified 8U232AM (or 232BM) device */static int ftdi_userdev_startup (struct usb_serial *serial){	struct ftdi_private *priv;	int err;	dbg("%s",__FUNCTION__);	/* XXX Assume it's a FT8U232AM.  An FT232BM device can be used, but	 * it will behave like a FT8U232AM.  -- IJA */	err = ftdi_8U232AM_startup(serial);	if (err){		return (err);	}	priv = serial->port->private;	priv->baud_base = baud_base; 		return (0);}/* ftdi_shutdown is called from usbserial:usb_serial_disconnect  *   it is called when the usb device is disconnected * *   usbserial:usb_serial_disconnect *      calls __serial_close for each open of the port *      shutdown is called then (ie ftdi_shutdown) */static void ftdi_shutdown (struct usb_serial *serial){ /* ftdi_shutdown */		struct usb_serial_port *port = &serial->port[0];		struct ftdi_private *priv = serial->port->private; 	int i;	unsigned long flags; 	dbg("%s", __FUNCTION__);	/* all open ports are closed at this point          *    (by usbserial.c:__serial_close, which calls ftdi_close)  	 */		/* Only execute this if this is the final open port for this device */	if (port->open_count == 0){		spin_lock_irqsave (&priv->write_urb_pool_lock, flags);		for (i = 0; i < NUM_URBS && priv->write_urb_pool[i]; ++i) {			/* FIXME - uncomment the following usb_unlink_urb call when			 * the host controllers get fixed to set urb->dev = NULL after			 * the urb is finished.  Otherwise this call oopses. */			/* usb_unlink_urb(priv->write_urb_pool[i]); */			if (priv->write_urb_pool[i]->transfer_buffer) {				kfree(priv->write_urb_pool[i]->transfer_buffer);				priv->write_urb_pool[i]->transfer_buffer = NULL;			}			usb_free_urb (priv->write_urb_pool[i]);			priv->write_urb_pool[i] = NULL;		}		spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);		/* usb_disconnect shuts down the port->read_urb so don't do it here */		/* as was done previously */	}	if (serial->port->private){		kfree(serial->port->private);		serial->port->private = NULL;	}} /* ftdi_shutdown */static int  ftdi_open (struct usb_serial_port *port, struct file *filp){ /* ftdi_open */	struct termios tmp_termios;	struct usb_serial *serial = port->serial;	struct ftdi_private *priv = port->private;		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, 			priv->interface, 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__);	}	/* 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;	struct ftdi_private *priv = (struct ftdi_private *)port->private;	char buf[1];	int err;	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, priv->interface, 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");			}		} /* Note change no line is hupcl is off */		/* shutdown our bulk read */		if (port->read_urb) {			err = usb_unlink_urb (port->read_urb);			if (err < 0 && err != -ENODEV)				err("Error unlinking urb (%d)", err);		}		/* unlink the running write urbs */	} /* 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) {		dbg("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 && priv->write_urb_pool[i]; i++) {			if (priv->write_urb_pool[i] -> status != -EINPROGRESS) {				urb = priv->write_urb_pool[i];				/* Must make sure another write doesn't grab this */				urb->status = -EINPROGRESS;				break;			}		}		spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);		if (urb == NULL) {			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) {				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)

⌨️ 快捷键说明

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