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

📄 ftdi_sio.c

📁 NP-Convert,usb4口转换器的驱动开发示例.面向Linux.
💻 C
📖 第 1 页 / 共 5 页
字号:
	.num_interrupt_in =	0,	.num_bulk_in =		1,	.num_bulk_out =		1,	.num_ports =		1,	.open =			ftdi_open,	.close =		ftdi_close,	.throttle =		ftdi_throttle,	.unthrottle =		ftdi_unthrottle,	.write =		ftdi_write,	.write_room =		ftdi_write_room,	.chars_in_buffer =	ftdi_chars_in_buffer,	.read_bulk_callback =	ftdi_read_bulk_callback,	.write_bulk_callback =	ftdi_write_bulk_callback,	.ioctl =		ftdi_ioctl,	.set_termios =		ftdi_set_termios,	.break_ctl =		ftdi_break_ctl,	.startup =		ftdi_SIO_startup,	.shutdown =		ftdi_shutdown,};static struct usb_serial_device_type ftdi_8U232AM_device = {	.owner =		THIS_MODULE,	.name =			"FTDI 8U232AM Compatible",	.id_table =		id_table_8U232AM,	.num_interrupt_in =	0,	.num_bulk_in =		1,	.num_bulk_out =		1,	.num_ports =		1,	.open =			ftdi_open,	.close =		ftdi_close,	.throttle =		ftdi_throttle,	.unthrottle =		ftdi_unthrottle,	.write =		ftdi_write,	.write_room =		ftdi_write_room,	.chars_in_buffer =	ftdi_chars_in_buffer,	.read_bulk_callback =	ftdi_read_bulk_callback,	.write_bulk_callback =	ftdi_write_bulk_callback,	.ioctl =		ftdi_ioctl,	.set_termios =		ftdi_set_termios,	.break_ctl =		ftdi_break_ctl,	.startup =		ftdi_8U232AM_startup,	.shutdown =		ftdi_shutdown,};static struct usb_serial_device_type ftdi_FT232BM_device = {	.owner =		THIS_MODULE,	.name =			"FTDI FT232BM Compatible",	.id_table =		id_table_FT232BM,	.num_interrupt_in =	0,	.num_bulk_in =		1,	.num_bulk_out =		1,	.num_ports =		1,	.open =			ftdi_open,	.close =		ftdi_close,	.throttle =		ftdi_throttle,	.unthrottle =		ftdi_unthrottle,	.write =		ftdi_write,	.write_room =		ftdi_write_room,	.chars_in_buffer =	ftdi_chars_in_buffer,	.read_bulk_callback =	ftdi_read_bulk_callback,	.write_bulk_callback =	ftdi_write_bulk_callback,	.ioctl =		ftdi_ioctl,	.set_termios =		ftdi_set_termios,	.break_ctl =		ftdi_break_ctl,	.startup =		ftdi_FT232BM_startup,	.shutdown =		ftdi_shutdown,};static struct usb_serial_device_type ftdi_FT2232C_device = {	.owner =		THIS_MODULE,	.name =			"FTDI FT2232C Compatible",	.id_table =		id_table_FT2232C,	.num_interrupt_in =	0,	.num_bulk_in =		1,	.num_bulk_out =		1,	.num_ports =		1,	.open =			ftdi_open,	.close =		ftdi_close,	.throttle =		ftdi_throttle,	.unthrottle =		ftdi_unthrottle,	.write =		ftdi_write,	.write_room =		ftdi_write_room,	.chars_in_buffer =	ftdi_chars_in_buffer,	.read_bulk_callback =	ftdi_read_bulk_callback,	.write_bulk_callback =	ftdi_write_bulk_callback,	.ioctl =		ftdi_ioctl,	.set_termios =		ftdi_set_termios,	.break_ctl =		ftdi_break_ctl,	.startup =		ftdi_FT2232C_startup,	.shutdown =		ftdi_shutdown,};static struct usb_serial_device_type ftdi_FT232R_device = {	.owner =		THIS_MODULE,	.name =			"FTDI FT232R Compatible",	.id_table =		id_table_FT232R,	.num_interrupt_in =	0,	.num_bulk_in =		1,	.num_bulk_out =		1,	.num_ports =		1,	.open =			ftdi_open,	.close =		ftdi_close,	.throttle =		ftdi_throttle,	.unthrottle =		ftdi_unthrottle,	.write =		ftdi_write,	.write_room =		ftdi_write_room,	.chars_in_buffer =	ftdi_chars_in_buffer,	.read_bulk_callback =	ftdi_read_bulk_callback,	.write_bulk_callback =	ftdi_write_bulk_callback,	.ioctl =		ftdi_ioctl,	.set_termios =		ftdi_set_termios,	.break_ctl =		ftdi_break_ctl,	.startup =		ftdi_FT232R_startup,	.shutdown =		ftdi_shutdown,};static struct usb_serial_device_type ftdi_USB_UIRT_device = {	.owner =		THIS_MODULE,	.name =			"USB-UIRT Infrared Tranceiver",	.id_table =		id_table_USB_UIRT,	.num_interrupt_in =	0,	.num_bulk_in =		1,	.num_bulk_out =		1,	.num_ports =		1,	.open =			ftdi_open,	.close =		ftdi_close,	.throttle =		ftdi_throttle,	.unthrottle =		ftdi_unthrottle,	.write =		ftdi_write,	.write_room =		ftdi_write_room,	.chars_in_buffer =	ftdi_chars_in_buffer,	.read_bulk_callback =	ftdi_read_bulk_callback,	.write_bulk_callback =	ftdi_write_bulk_callback,	.ioctl =		ftdi_ioctl,	.set_termios =		ftdi_set_termios,	.break_ctl =		ftdi_break_ctl,	.startup =		ftdi_USB_UIRT_startup,	.shutdown =		ftdi_shutdown,};/* The TIRA1 is based on a  FT232BM which requires a fixed baud rate of 100000 * and which requires RTS-CTS to be enabled. */static struct usb_serial_device_type ftdi_HE_TIRA1_device = {	.owner =		THIS_MODULE,	.name =			"Home-Electronics TIRA-1 IR Transceiver",	.id_table =		id_table_HE_TIRA1,	.num_interrupt_in =	0,	.num_bulk_in =		1,	.num_bulk_out =		1,	.num_ports =		1,	.open =			ftdi_open,	.close =		ftdi_close,	.throttle =		ftdi_throttle,	.unthrottle =		ftdi_unthrottle,	.write =		ftdi_write,	.write_room =		ftdi_write_room,	.chars_in_buffer =	ftdi_chars_in_buffer,	.read_bulk_callback =	ftdi_read_bulk_callback,	.write_bulk_callback =	ftdi_write_bulk_callback,	.ioctl =		ftdi_ioctl,	.set_termios =		ftdi_set_termios,	.break_ctl =		ftdi_break_ctl,	.startup =		ftdi_HE_TIRA1_startup,	.shutdown =		ftdi_shutdown,};static struct usb_serial_device_type ftdi_userdev_device = {	.owner =		THIS_MODULE,	.name =			"FTDI SIO compatible",	.id_table =		id_table_userdev,	.num_interrupt_in =	0,	.num_bulk_in =		1,	.num_bulk_out =		1,	.num_ports =		1,	.open =			ftdi_open,	.close =		ftdi_close,	.write =		ftdi_write,	.write_room =		ftdi_write_room,	.read_bulk_callback =	ftdi_read_bulk_callback,	.write_bulk_callback =	ftdi_write_bulk_callback,	.ioctl =		ftdi_ioctl,	.set_termios =		ftdi_set_termios,	.break_ctl =		ftdi_break_ctl,	.startup =		ftdi_userdev_startup,	.shutdown =		ftdi_shutdown,};#define WDR_TIMEOUT (HZ * 5 ) /* default urb timeout *//* High and low are for DTR, RTS etc etc */#define HIGH 1#define LOW 0/* * *************************************************************************** * Utlity functions * *************************************************************************** */static unsigned short int ftdi_232am_baud_base_to_divisor(int baud, int base){	unsigned short int divisor;	int divisor3 = base / 2 / baud; // divisor shifted 3 bits to the left	if ((divisor3 & 0x7) == 7) divisor3 ++; // round x.7/8 up to x+1	divisor = divisor3 >> 3;	divisor3 &= 0x7;	if (divisor3 == 1) divisor |= 0xc000; else // 0.125	if (divisor3 >= 4) divisor |= 0x4000; else // 0.5	if (divisor3 != 0) divisor |= 0x8000;      // 0.25	if (divisor == 1) divisor = 0;	/* special case for maximum baud rate */	return divisor;}static unsigned short int ftdi_232am_baud_to_divisor(int baud){	 return(ftdi_232am_baud_base_to_divisor(baud, 48000000));}static __u32 ftdi_232bm_baud_base_to_divisor(int baud, int base){	static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };	__u32 divisor;	int divisor3 = base / 2 / baud; // divisor shifted 3 bits to the left	divisor = divisor3 >> 3;	divisor |= (__u32)divfrac[divisor3 & 0x7] << 14;	/* Deal with special cases for highest baud rates. */	if (divisor == 1) divisor = 0; else	// 1.0	if (divisor == 0x4001) divisor = 1;	// 1.5	return divisor;}static __u32 ftdi_232bm_baud_to_divisor(int baud){	 return(ftdi_232bm_baud_base_to_divisor(baud, 48000000));}static int set_rts(struct usb_serial_port *port, int high_or_low){	struct ftdi_private * priv = (struct ftdi_private *)port->private;	char buf[1];	unsigned ftdi_high_or_low;	if (high_or_low) {		ftdi_high_or_low = FTDI_SIO_SET_RTS_HIGH;		priv->last_dtr_rts |= TIOCM_RTS;	} else {		ftdi_high_or_low = FTDI_SIO_SET_RTS_LOW;		priv->last_dtr_rts &= ~TIOCM_RTS;	}	return(usb_control_msg(port->serial->dev,			       usb_sndctrlpipe(port->serial->dev, 0),			       FTDI_SIO_SET_MODEM_CTRL_REQUEST, 			       FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,			       ftdi_high_or_low, priv->interface, 			       buf, 0, WDR_TIMEOUT));}static int set_dtr(struct usb_serial_port *port, int high_or_low){	struct ftdi_private * priv = (struct ftdi_private *)port->private;	char buf[1];	unsigned ftdi_high_or_low;	if (high_or_low) {		ftdi_high_or_low = FTDI_SIO_SET_DTR_HIGH;		priv->last_dtr_rts |= TIOCM_DTR;	} else {		ftdi_high_or_low = FTDI_SIO_SET_DTR_LOW;		priv->last_dtr_rts &= ~TIOCM_DTR;	}	return(usb_control_msg(port->serial->dev,			       usb_sndctrlpipe(port->serial->dev, 0),			       FTDI_SIO_SET_MODEM_CTRL_REQUEST, 			       FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,			       ftdi_high_or_low, priv->interface, 			       buf, 0, WDR_TIMEOUT));}static __u32 get_ftdi_divisor(struct usb_serial_port * port);static int change_speed(struct usb_serial_port *port){	char buf[1];	struct ftdi_private * priv = (struct ftdi_private *)port->private;        __u16 urb_value;	__u16 urb_index;	__u32 urb_index_value;	urb_index_value = get_ftdi_divisor(port);	urb_value = (__u16)urb_index_value;	if(priv->chip_type == FT2232C) {		urb_index = (__u16)(urb_index_value >> 8);		urb_index &= 0xFF00;		urb_index |= priv->interface;	} else {		urb_index = (__u16)(urb_index_value >> 16);		urb_index |= priv->interface;	}			return (usb_control_msg(port->serial->dev,			    usb_sndctrlpipe(port->serial->dev, 0),			    FTDI_SIO_SET_BAUDRATE_REQUEST,			    FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE,			    urb_value, urb_index,			    buf, 0, 100) < 0);}static __u32 get_ftdi_divisor(struct usb_serial_port * port){ /* get_ftdi_divisor */		struct ftdi_private * priv = (struct ftdi_private *)port->private;	__u32 div_value = 0;	int div_okay = 1;	char *chip_name = "";	int baud;	/*	 * The logic involved in setting the baudrate can be cleanly split in 3 steps.	 * Obtaining the actual baud rate is a little tricky since unix traditionally	 * somehow ignored the possibility to set non-standard baud rates.	 * 1. Standard baud rates are set in tty->termios->c_cflag	 * 2. If these are not enough, you can set any speed using alt_speed as follows:	 *    - set tty->termios->c_cflag speed to B38400	 *    - set your real speed in tty->alt_speed; it gets ignored when	 *      alt_speed==0, (or)	 *    - call TIOCSSERIAL ioctl with (struct serial_struct) set as follows:	 *      flags & ASYNC_SPD_MASK == ASYNC_SPD_[HI, VHI, SHI, WARP], this just	 *      sets alt_speed to (HI: 57600, VHI: 115200, SHI: 230400, WARP: 460800)	 * ** Steps 1, 2 are done courtesy of tty_get_baud_rate	 * 3. You can also set baud rate by setting custom divisor as follows	 *    - set tty->termios->c_cflag speed to B38400	 *    - call TIOCSSERIAL ioctl with (struct serial_struct) set as follows:	 *      o flags & ASYNC_SPD_MASK == ASYNC_SPD_CUST	 *      o custom_divisor set to baud_base / your_new_baudrate	 * ** Step 3 is done courtesy of code borrowed from serial.c - I should really	 *    spend some time and separate+move this common code to serial.c, it is	 *    replicated in nearly every serial driver you see.	 */	/* 1. Get the baud rate from the tty settings, this observes alt_speed hack */	baud = tty_get_baud_rate(port->tty);	dbg("%s - tty_get_baud_rate reports speed %d", __FUNCTION__, baud);	/* 2. Observe async-compatible custom_divisor hack, update baudrate if needed */	if (baud == 38400 &&	    ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) &&	     (priv->custom_divisor)) {		baud = priv->baud_base / priv->custom_divisor;		dbg("%s - custom divisor %d sets baud rate to %d", __FUNCTION__, priv->custom_divisor, baud);	}

⌨️ 快捷键说明

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