📄 ftdi_sio.c
字号:
/* * USB FTDI SIO driver * * Copyright (C) 1999 - 2001 * Greg Kroah-Hartman (greg@kroah.com) * Bill Ryder (bryder@sgi.com) * Copyright (C) 2002 * Kuba Ober (kuba@mareimbrium.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * See Documentation/usb/usb-serial.txt for more information on using this driver * * See http://ftdi-usb-sio.sourceforge.net for upto date testing info * and extra documentation * * (25/Jul/2002) Bill Ryder inserted Dmitri's TIOCMIWAIT patch * Not tested by me but it doesn't break anything I use. * * (04/Jan/2002) Kuba Ober * Implemented 38400 baudrate kludge, where it can be substituted with other * values. That's the only way to set custom baudrates. * Implemented TIOCSSERIAL, TIOCGSERIAL ioctl's so that setserial is happy. * FIXME: both baudrate things should eventually go to usbserial.c as other * devices may need that functionality too. Actually, it can probably be * merged in serial.c somehow - too many drivers repeat this code over * and over. * Fixed baudrate forgetfulness - open() used to reset baudrate to 9600 every time. * Divisors for baudrates are calculated by a macro. * Small code cleanups. Ugly whitespace changes for Plato's sake only ;-]. * * (04/Nov/2001) Bill Ryder * Fixed bug in read_bulk_callback where incorrect urb buffer was used. * Cleaned up write offset calculation * Added write_room since default values can be incorrect for sio * Changed write_bulk_callback to use same queue_task as other drivers * (the previous version caused panics) * Removed port iteration code since the device only has one I/O port and it * was wrong anyway. * * (31/May/2001) gkh * Switched from using spinlock to a semaphore, which fixes lots of problems. * * (23/May/2001) Bill Ryder * Added runtime debug patch (thanx Tyson D Sawyer). * Cleaned up comments for 8U232 * Added parity, framing and overrun error handling * Added receive break handling. * * (04/08/2001) gb * Identify version on module load. * * (18/March/2001) Bill Ryder * (Not released) * Added send break handling. (requires kernel patch too) * Fixed 8U232AM hardware RTS/CTS etc status reporting. * Added flipbuf fix copied from generic device * * (12/3/2000) Bill Ryder * Added support for 8U232AM device. * Moved PID and VIDs into header file only. * Turned on low-latency for the tty (device will do high baudrates) * Added shutdown routine to close files when device removed. * More debug and error message cleanups. * * (11/13/2000) Bill Ryder * Added spinlock protected open code and close code. * Multiple opens work (sort of - see webpage mentioned above). * Cleaned up comments. Removed multiple PID/VID definitions. * Factorised cts/dtr code * Made use of __FUNCTION__ in dbg's * * (11/01/2000) Adam J. Richter * usb_device_id table support * * (10/05/2000) gkh * Fixed bug with urb->dev not being set properly, now that the usb * core needs it. * * (09/11/2000) gkh * Removed DEBUG #ifdefs with call to usb_serial_debug_data * * (07/19/2000) gkh * Added module_init and module_exit functions to handle the fact that this * driver is a loadable module now. * * (04/04/2000) Bill Ryder * Fixed bugs in TCGET/TCSET ioctls (by removing them - they are * handled elsewhere in the tty io driver chain). * * (03/30/2000) Bill Ryder * Implemented lots of ioctls * Fixed a race condition in write * Changed some dbg's to errs * * (03/26/2000) gkh * Split driver up into device specific pieces. * *//* Bill Ryder - bryder@sgi.com - wrote the FTDI_SIO implementation *//* Thanx to FTDI for so kindly providing details of the protocol required *//* to talk to the device *//* Thanx to gkh and the rest of the usb dev group for all code I have assimilated :-) */#include <linux/config.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/tty.h>#include <linux/tty_driver.h>#include <linux/tty_flip.h>#include <linux/module.h>#include <linux/spinlock.h>#include <asm/uaccess.h>#include <linux/usb.h>#include <linux/serial.h>#ifdef CONFIG_USB_SERIAL_DEBUG static int debug = 1;#else static int debug;#endif#include "usb-serial.h"#include "ftdi_sio.h"/* * Version Information */#define DRIVER_VERSION "v1.2.1"#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Bill Ryder <bryder@sgi.com>, Kuba Ober <kuba@mareimbrium.org>"#define DRIVER_DESC "USB FTDI Serial Converters Driver"static struct usb_device_id id_table_sio [] = { { USB_DEVICE(FTDI_VID, FTDI_SIO_PID) }, { } /* Terminating entry */};/* * The 8U232AM has the same API as the sio except for: * - it can support MUCH higher baudrates; up to: * o 921600 for RS232 and 2000000 for RS422/485 at 48MHz * o 230400 at 12MHz * so .. 8U232AM's baudrate setting codes are different * - it has a two byte status code. * - it returns characters every 16ms (the FTDI does it every 40ms) */static struct usb_device_id id_table_8U232AM [] = { { USB_DEVICE(FTDI_VID, FTDI_8U232AM_PID) }, { USB_DEVICE(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID) }, { } /* Terminating entry */};static __devinitdata struct usb_device_id id_table_combined [] = { { USB_DEVICE(FTDI_VID, FTDI_SIO_PID) }, { USB_DEVICE(FTDI_VID, FTDI_8U232AM_PID) }, { USB_DEVICE(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID) }, { } /* Terminating entry */};MODULE_DEVICE_TABLE (usb, id_table_combined);struct ftdi_private { ftdi_chip_type_t chip_type; /* type of the device, either SIO or FT8U232AM */ int baud_base; /* baud base clock for divisor setting */ int custom_divisor; /* custom_divisor kludge, this is for baud_base (different from what goes to the chip!) */ __u16 last_set_data_urb_value ; /* the last data state set - needed for doing a break */ int write_offset; /* This is the offset in the usb data block to write the serial data - * it is different between devices */ int flags; /* some ASYNC_xxxx flags are supported */ wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ char prev_status, diff_status; /* Used for TIOCMIWAIT */};/* Used for TIOCMIWAIT */#define FTDI_STATUS_B0_MASK (FTDI_RS0_CTS | FTDI_RS0_DSR | FTDI_RS0_RI | FTDI_RS0_RLSD)#define FTDI_STATUS_B1_MASK (FTDI_RS_BI)/* End TIOCMIWAIT */#define FTDI_IMPL_ASYNC_FLAGS = ( ASYNC_SPD_HI | ASYNC_SPD_VHI \ ASYNC_SPD_CUST | ASYNC_SPD_SHI | ASYNC_SPD_WARP )/* function prototypes for a FTDI serial converter */static int ftdi_SIO_startup (struct usb_serial *serial);static int ftdi_8U232AM_startup (struct usb_serial *serial);static void ftdi_shutdown (struct usb_serial *serial);static int ftdi_open (struct usb_serial_port *port, struct file *filp);static void ftdi_close (struct usb_serial_port *port, struct file *filp);static int ftdi_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);static int ftdi_write_room (struct usb_serial_port *port);static void ftdi_write_bulk_callback (struct urb *urb);static void ftdi_read_bulk_callback (struct urb *urb);static void ftdi_set_termios (struct usb_serial_port *port, struct termios * old);static int ftdi_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);static void ftdi_break_ctl (struct usb_serial_port *port, int break_state );static struct usb_serial_device_type ftdi_SIO_device = { .owner = THIS_MODULE, .name = "FTDI SIO", .id_table = id_table_sio, .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_SIO_startup, .shutdown = ftdi_shutdown,};static struct usb_serial_device_type ftdi_8U232AM_device = { .owner = THIS_MODULE, .name = "FTDI 8U232AM", .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, .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_8U232AM_startup, .shutdown = ftdi_shutdown,};#define WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */#define HIGH 1#define LOW 0/* * *************************************************************************** * Utlity functions * *************************************************************************** */static int set_rts(struct usb_device *dev, unsigned int pipe, int high_or_low){ static char buf[1]; unsigned ftdi_high_or_low = (high_or_low? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW); return(usb_control_msg(dev, pipe, FTDI_SIO_SET_MODEM_CTRL_REQUEST, FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE, ftdi_high_or_low, 0, buf, 0, WDR_TIMEOUT));}static int set_dtr(struct usb_device *dev, unsigned int pipe, int high_or_low){ static char buf[1]; unsigned ftdi_high_or_low = (high_or_low? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW); return(usb_control_msg(dev, pipe, FTDI_SIO_SET_MODEM_CTRL_REQUEST, FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE, ftdi_high_or_low, 0, buf, 0, WDR_TIMEOUT));}static __u16 get_ftdi_divisor(struct usb_serial_port * port);static int change_speed(struct usb_serial_port *port){ char buf[1]; __u16 urb_value; urb_value = get_ftdi_divisor(port); 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, 0, buf, 0, 100) < 0);}static __u16 get_ftdi_divisor(struct usb_serial_port * port){ /* get_ftdi_divisor */ struct ftdi_private * priv = (struct ftdi_private *)port->private; __u16 urb_value = 0; 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); } /* 3. Convert baudrate to device-specific divisor */ if (!baud) baud = 9600; switch(priv->chip_type) { case SIO: /* SIO chip */ switch(baud) { case 300: urb_value = ftdi_sio_b300; break; case 600: urb_value = ftdi_sio_b600; break; case 1200: urb_value = ftdi_sio_b1200; break; case 2400: urb_value = ftdi_sio_b2400; break; case 4800: urb_value = ftdi_sio_b4800; break; case 9600: urb_value = ftdi_sio_b9600; break; case 19200: urb_value = ftdi_sio_b19200; break; case 38400: urb_value = ftdi_sio_b38400; break; case 57600: urb_value = ftdi_sio_b57600; break; case 115200: urb_value = ftdi_sio_b115200; break; } /* baud */ if (urb_value == 0) dbg("%s - Baudrate (%d) requested is not supported", __FUNCTION__, baud); break; case FT8U232AM: /* 8U232AM chip */ if (baud <= 3000000) { urb_value = FTDI_SIO_BAUD_TO_DIVISOR(baud); } else { dbg("%s - Baud rate too high!", __FUNCTION__); } break; } /* priv->chip_type */ if (urb_value == 0) { urb_value = ftdi_sio_b9600; } else { dbg("%s - Baud rate set to %d (divisor %d) on chip %s", __FUNCTION__, baud, urb_value, (priv->chip_type == SIO) ? "SIO" : "FT8U232AM" ); } return(urb_value);}static int get_serial_info(struct usb_serial_port * port, struct serial_struct * retinfo){ struct ftdi_private * priv = (struct ftdi_private*) port->private; struct serial_struct tmp; if (!retinfo) return -EFAULT; memset(&tmp, 0, sizeof(tmp)); tmp.flags = priv->flags; tmp.baud_base = priv->baud_base; tmp.custom_divisor = priv->custom_divisor;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -