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

📄 ftdi_sio.c

📁 基于S3CEB2410平台LINUX操作系统下 USB驱动源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * USB FTDI SIO driver * * 	Copyright (C) 1999 - 2001 * 	    Greg Kroah-Hartman (greg@kroah.com) *          Bill Ryder (bryder@sgi.com) * * 	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 *  * (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/sched.h>#include <linux/signal.h>#include <linux/errno.h>#include <linux/poll.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/fcntl.h>#include <linux/tty.h>#include <linux/tty_driver.h>#include <linux/tty_flip.h>#include <linux/module.h>#include <linux/spinlock.h>#include <linux/usb.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.0"#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Bill Ryder <bryder@sgi.com>"#define DRIVER_DESC "USB FTDI RS232 Converters Driver"static __devinitdata 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 (921600 at 48MHz/230400      at 12MHz so .. it's baudrate setting codes are different    - it has a two byte status code.   - it returns characters very 16ms (the FTDI does it every 40ms)  */   static __devinitdata struct usb_device_id id_table_8U232AM [] = {	{ USB_DEVICE(FTDI_VID, FTDI_8U232AM_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) },	{ }						/* Terminating entry */};MODULE_DEVICE_TABLE (usb, id_table_combined);struct ftdi_private {	ftdi_type_t ftdi_type;	__u16 last_set_data_urb_value ; /* the last data state set - needed for doing a break */        int write_offset;};/* 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_sio_shutdown		(struct usb_serial *serial);static int  ftdi_sio_open		(struct usb_serial_port *port, struct file *filp);static void ftdi_sio_close		(struct usb_serial_port *port, struct file *filp);static int  ftdi_sio_write		(struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);static int  ftdi_sio_write_room		(struct usb_serial_port *port);static void ftdi_sio_write_bulk_callback (struct urb *urb);static void ftdi_sio_read_bulk_callback	(struct urb *urb);static void ftdi_sio_set_termios	(struct usb_serial_port *port, struct termios * old);static int  ftdi_sio_ioctl		(struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);static void ftdi_sio_break_ctl		(struct usb_serial_port *port, int break_state );/* Should rename most ftdi_sio's to ftdi_ now since there are two devices    which share common code */ static struct usb_serial_device_type ftdi_sio_device = {	name:			"FTDI SIO",	id_table:		id_table_sio,	needs_interrupt_in:	MUST_HAVE_NOT,	needs_bulk_in:		MUST_HAVE,	needs_bulk_out:		MUST_HAVE,	num_interrupt_in:	0,	num_bulk_in:		1,	num_bulk_out:		1,	num_ports:		1,	open:			ftdi_sio_open,	close:			ftdi_sio_close,	write:			ftdi_sio_write,	write_room:		ftdi_sio_write_room,	read_bulk_callback:	ftdi_sio_read_bulk_callback,	write_bulk_callback:	ftdi_sio_write_bulk_callback,	ioctl:			ftdi_sio_ioctl,	set_termios:		ftdi_sio_set_termios,	break_ctl:		ftdi_sio_break_ctl,	startup:		ftdi_sio_startup,        shutdown:               ftdi_sio_shutdown,};static struct usb_serial_device_type ftdi_8U232AM_device = {	name:			"FTDI 8U232AM",	id_table:		id_table_8U232AM,	needs_interrupt_in:	DONT_CARE,	needs_bulk_in:		MUST_HAVE,	needs_bulk_out:		MUST_HAVE,	num_interrupt_in:	0,	num_bulk_in:		1,	num_bulk_out:		1,	num_ports:		1,	open:			ftdi_sio_open,	close:			ftdi_sio_close,	write:			ftdi_sio_write,	write_room:		ftdi_sio_write_room,	read_bulk_callback:	ftdi_sio_read_bulk_callback,	write_bulk_callback:	ftdi_sio_write_bulk_callback,	ioctl:			ftdi_sio_ioctl,	set_termios:		ftdi_sio_set_termios,	break_ctl:		ftdi_sio_break_ctl,	startup:		ftdi_8U232AM_startup,        shutdown:               ftdi_sio_shutdown,};/* * *************************************************************************** * FTDI SIO Serial Converter specific driver functions * *************************************************************************** */#define WDR_TIMEOUT (HZ * 5 ) /* default urb timeout *//* utility functions to set and unset dtr and rts */#define HIGH 1#define LOW 0static 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 int ftdi_sio_startup (struct usb_serial *serial){	struct ftdi_private *priv;			priv = serial->port->private = kmalloc(sizeof(struct ftdi_private), GFP_KERNEL);	if (!priv){		err(__FUNCTION__"- kmalloc(%Zd) failed.", sizeof(struct ftdi_private));		return -ENOMEM;	}	priv->ftdi_type = sio;	priv->write_offset = 1;		return (0);}static int ftdi_8U232AM_startup (struct usb_serial *serial){	struct ftdi_private *priv; 	priv = serial->port->private = kmalloc(sizeof(struct ftdi_private), GFP_KERNEL);	if (!priv){		err(__FUNCTION__"- kmalloc(%Zd) failed.", sizeof(struct ftdi_private));		return -ENOMEM;	}	priv->ftdi_type = F8U232AM;	priv->write_offset = 0;		return (0);}static void ftdi_sio_shutdown (struct usb_serial *serial){		dbg (__FUNCTION__);	/* stop reads and writes on all ports */	while (serial->port[0].open_count > 0) {	        ftdi_sio_close (&serial->port[0], NULL);	}	if (serial->port[0].private){		kfree(serial->port[0].private);		serial->port[0].private = NULL;	}}static int  ftdi_sio_open (struct usb_serial_port *port, struct file *filp){ /* ftdi_sio_open */	struct termios tmp_termios;	struct usb_serial *serial = port->serial;	int result = 0;	char buf[1]; /* Needed for the usb_control_msg I think */	dbg(__FUNCTION__);	down (&port->sem);		MOD_INC_USE_COUNT;	++port->open_count;	if (!port->active){		port->active = 1;		/* This will push the characters through immediately rather 		   than queue a task to deliver them */		port->tty->low_latency = 1;		/* No error checking for this (will get errors later anyway) */		/* See ftdi_sio.h for description of what is reset */

⌨️ 快捷键说明

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