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

📄 ftdi_sio.c

📁 ep9315平台下USB驱动的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * 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 * * (23/Feb/2003) Bill Ryder *      Added matrix orb device vid/pids from Wayne Wylupski * * (19/Feb/2003) Ian Abbott *      For TIOCSSERIAL, set alt_speed to 0 when ASYNC_SPD_MASK value has *      changed to something other than ASYNC_SPD_HI, ASYNC_SPD_VHI, *      ASYNC_SPD_SHI or ASYNC_SPD_WARP.  Also, unless ASYNC_SPD_CUST is in *      force, don't bother changing baud rate when custom_divisor has changed. * * (18/Feb/2003) Ian Abbott *      Fixed TIOCMGET handling to include state of DTR and RTS, the state *      of which are now saved by set_dtr() and set_rts(). *      Fixed improper storage class for buf in set_dtr() and set_rts(). *      Added FT232BM chip type and support for its extra baud rates (compared *      to FT8U232AM). *      Took account of special case divisor values for highest baud rates of *      FT8U232AM and FT232BM. *      For TIOCSSERIAL, forced alt_speed to 0 when ASYNC_SPD_CUST kludge used, *      as previous alt_speed setting is now stale. *      Moved startup code common between the startup routines for the *      different chip types into a common subroutine. * * (17/Feb/2003) Bill Ryder *      Added write urb buffer pool on a per device basis *      Added more checking for open file on callbacks (fixed OOPS) *      Added CrystalFontz 632 and 634 PIDs  *         (thanx to CrystalFontz for the sample devices - they flushed out *           some driver bugs) *      Minor debugging message changes *      Added throttle, unthrottle and chars_in_buffer functions *      Fixed FTDI_SIO (the original device) bug *      Fixed some shutdown handling *       *  *  *  * (07/Jun/2002) Kuba Ober *	Changed FTDI_SIO_BASE_BAUD_TO_DIVISOR macro into ftdi_baud_to_divisor *	function. It was getting too complex. *	Fix the divisor calculation logic which was setting divisor of 0.125 *	instead of 0.5 for fractional parts of divisor equal to 5/8, 6/8, 7/8. *	Also make it bump up the divisor to next integer in case of 7/8 - it's *	a better approximation. * * (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.3.2"#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) * * the bcdDevice value is used to differentiate FT232BM and FT245BM from * the earlier FT8U232AM and FT8U232BM.  For now, include all known VID/PID * combinations in both tables. * FIXME: perhaps bcdDevice can also identify 12MHz devices, but I don't know * if those ever went into mass production. [Ian Abbott] */static struct usb_device_id id_table_8U232AM [] = {	{ USB_DEVICE_VER(FTDI_VID, FTDI_8U232AM_PID, 0, 0x3ff) },	{ USB_DEVICE_VER(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID, 0, 0x3ff) },	{ USB_DEVICE_VER(FTDI_VID, FTDI_XF_634_PID, 0, 0x3ff) },	{ USB_DEVICE_VER(FTDI_VID, FTDI_XF_632_PID, 0, 0x3ff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_0_PID, 0, 0x3ff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_1_PID, 0, 0x3ff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_2_PID, 0, 0x3ff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_3_PID, 0, 0x3ff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_4_PID, 0, 0x3ff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_5_PID, 0, 0x3ff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_6_PID, 0, 0x3ff) },	{ }						/* Terminating entry */};static struct usb_device_id id_table_FT232BM [] = {	{ USB_DEVICE_VER(FTDI_VID, FTDI_8U232AM_PID, 0x400, 0xffff) },	{ USB_DEVICE_VER(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID, 0x400, 0xffff) },	{ USB_DEVICE_VER(FTDI_VID, FTDI_XF_634_PID, 0x400, 0xffff) },	{ USB_DEVICE_VER(FTDI_VID, FTDI_XF_632_PID, 0x400, 0xffff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_0_PID, 0x400, 0xffff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_1_PID, 0x400, 0xffff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_2_PID, 0x400, 0xffff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_3_PID, 0x400, 0xffff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_4_PID, 0x400, 0xffff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_5_PID, 0x400, 0xffff) },	{ USB_DEVICE_VER(FTDI_MTXORB_VID, FTDI_MTXORB_6_PID, 0x400, 0xffff) },	{ }						/* 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_VID, FTDI_XF_634_PID) },	{ USB_DEVICE(FTDI_VID, FTDI_XF_632_PID) },	{ USB_DEVICE(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID) },	{ USB_DEVICE(FTDI_MTXORB_VID, FTDI_MTXORB_0_PID) },	{ USB_DEVICE(FTDI_MTXORB_VID, FTDI_MTXORB_1_PID) },	{ USB_DEVICE(FTDI_MTXORB_VID, FTDI_MTXORB_2_PID) },	{ USB_DEVICE(FTDI_MTXORB_VID, FTDI_MTXORB_3_PID) },	{ USB_DEVICE(FTDI_MTXORB_VID, FTDI_MTXORB_4_PID) },	{ USB_DEVICE(FTDI_MTXORB_VID, FTDI_MTXORB_5_PID) },	{ USB_DEVICE(FTDI_MTXORB_VID, FTDI_MTXORB_6_PID) },	{ }						/* Terminating entry */};MODULE_DEVICE_TABLE (usb, id_table_combined);/* constants which set the number of write urb buffers */#define NUM_URBS			32/* Don't be tempted to increase this buffer to > 64 ! I tried it and it doesn't work */#define URB_TRANSFER_BUFFER_SIZE	64 /* the device's max packet size */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 */	unsigned long last_dtr_rts;	/* saved modem control outputs */        wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ 	char prev_status, diff_status;        /* Used for TIOCMIWAIT */	struct urb	*write_urb_pool[NUM_URBS];	spinlock_t	write_urb_pool_lock;};/* 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 int  ftdi_FT232BM_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 int  ftdi_chars_in_buffer	(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 void ftdi_throttle		(struct usb_serial_port *port);static void ftdi_unthrottle		(struct usb_serial_port *port);static unsigned short int ftdi_232am_baud_base_to_divisor (int baud, int base);static unsigned short int ftdi_232am_baud_to_divisor (int baud);static __u32 ftdi_232bm_baud_base_to_divisor (int baud, int base);static __u32 ftdi_232bm_baud_to_divisor (int baud);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,	.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,};#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

⌨️ 快捷键说明

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