?? serial.c
字號:
/* * serial_fd/serial.c * * Copyright (c) 2000, 2001, 2002 Lineo * Copyright (c) 2001 Hewlett Packard * * By: * Stuart Lynne <sl@lineo.com>, * Tom Rushworth <tbr@lineo.com>, * Bruce Balden <balden@lineo.com> * * Changes copyright (c) 2003 MontaVista Software, Inc. * * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *//* * The encapsultaion is designed to overcome difficulties with some USB hardware. * * While the USB protocol has a CRC over the data while in transit, i.e. while * being carried over the bus, there is no end to end protection. If the hardware * has any problems getting the data into or out of the USB transmit and receive * FIFO's then data can be lost. * * This protocol adds a two byte trailer to each USB packet to specify the number * of bytes of valid data and a 10 bit CRC that will allow the receiver to verify * that the entire USB packet was received without error. * * This means we now have end to end protection from the class driver to the function * driver and back. * * There is an additional option that can be used to force all transmitted packets * to be padded to the maximum packet size. This provides a work around for some * devices which have problems with small USB packets. * * Assuming a packetsize of N: * * 0..N-2 data and optional padding * * N-2 bits 7-2 - number of bytes of valid data * bits 1-0 top two bits of 10 bit CRC * N-1 bottom 8 bits of 10 bit CRC * * * | Data Length | 10 bit CRC | * + 7 . 6 . 5 . 4 . 3 . 2 . 1 . 0 | 7 . 6 . 5 . 4 . 3 . 2 . 1 . 0 + * * The 10 bit CRC is computed across the sent data, followed by the trailer with * the length set and the CRC set to zero. The CRC is then OR'd into the trailer. * * When received a 10 bit CRC is computed over the entire frame including the trailer * and should be equal to zero. * * Two module parameters are used to control the encapsulation, if both are * turned of the module works as a simple serial device with NO * encapsulation. * * See linux/drivers/usb/serial/safe_serial.c for a host class driver * implementation of this. * */#include <linux/config.h>#include <linux/module.h>#include "../usbd-export.h"#include "../usbd-build.h"#include "../usbd-module.h"MODULE_AUTHOR ("sl@lineo.com, tbr@lineo.com");MODULE_LICENSE("GPL");MODULE_DESCRIPTION ("USB Device Serial Function");USBD_MODULE_INFO ("serial_fd 0.1-beta");#ifndef MODULE#undef GET_USE_COUNT#define GET_USE_COUNT(foo) 1#endif#include <linux/init.h>#include <linux/kernel.h>#include <linux/list.h>#include <asm/uaccess.h>#include <linux/netdevice.h>#include <linux/skbuff.h>#include <linux/etherdevice.h>#include <net/arp.h>#include <linux/rtnetlink.h>#include <linux/smp_lock.h>#include <linux/ctype.h>#include <linux/timer.h>#include <linux/string.h>#include <linux/atmdev.h>#include <linux/pkt_sched.h>#include "../usbd.h"#include "../usbd-func.h"#include "../usbd-bus.h"#include "../usbd-debug.h"#include "../usbd-inline.h"#include "../usbd-arch.h"#include "crc10.h"#include "serproto.h"#if 0#define MIN(a, b) ({ \ typeof(a) _a = (a); \ typeof(b) _b = (b); \ _a < _b ? _a : _b; \})#endif#define MAX_INTERFACES 1#define MTU 1500+100#if !defined (CONFIG_USBD_VENDORID) && !defined(CONFIG_USBD_SERIAL_VENDORID)#error No Vendor ID#endif#if !defined (CONFIG_USBD_PRODUCTID) && !defined(CONFIG_USBD_SERIAL_PRODUCTID)#error No Product ID#endif#if CONFIG_USBD_SERIAL_VENDORID#undef CONFIG_USBD_VENDORID#define CONFIG_USBD_VENDORID CONFIG_USBD_SERIAL_VENDORID#endif#if CONFIG_USBD_SERIAL_PRODUCTID#undef CONFIG_USBD_PRODUCTID#define CONFIG_USBD_PRODUCTID CONFIG_USBD_SERIAL_PRODUCTID#endif#ifndef CONFIG_USBD_SERIAL_NUMBER_STR#define CONFIG_USBD_SERIAL_NUMBER_STR ""#endif#ifdef CONFIG_USBD_SELFPOWERED#define BMATTRIBUTE BMATTRIBUTE_RESERVED | BMATTRIBUTE_SELF_POWERED#define BMAXPOWER 0#else#define BMATTRIBUTE BMATTRIBUTE_RESERVED#define BMAXPOWER CONFIG_USBD_MAXPOWER#endif/* * setup some default values for pktsizes and endpoint addresses. */#ifndef CONFIG_USBD_SERIAL_OUT_PKTSIZE#define CONFIG_USBD_SERIAL_OUT_PKTSIZE 64#endif#ifndef CONFIG_USBD_SERIAL_IN_PKTSIZE#define CONFIG_USBD_SERIAL_IN_PKTSIZE 64#endif#ifndef CONFIG_USBD_SERIAL_INT_PKTSIZE#define CONFIG_USBD_SERIAL_INT_PKTSIZE 16#endif#ifndef CONFIG_USBD_SERIAL_OUT_ENDPOINT#define CONFIG_USBD_SERIAL_OUT_ENDPOINT 1#endif#ifndef CONFIG_USBD_SERIAL_IN_ENDPOINT#define CONFIG_USBD_SERIAL_IN_ENDPOINT 2#endif#ifndef CONFIG_USBD_SERIAL_INT_ENDPOINT#define CONFIG_USBD_SERIAL_INT_ENDPOINT 3#endif/* * check for architecture specific endpoint configurations */#if defined(ABS_OUT_ADDR)#warning#warning USING ABS ENDPOINT OUT ADDRESS#undef CONFIG_USBD_SERIAL_OUT_ENDPOINT#if ABS_OUT_ADDR > 0#define CONFIG_USBD_SERIAL_OUT_ENDPOINT ABS_OUT_ADDR#endif#elif defined(MAX_OUT_ADDR) && defined(CONFIG_USBD_SERIAL_OUT_ENDPOINT) && (CONFIG_USBD_SERIAL_OUT_ENDPOINT > MAX_OUT_ADDR)#warning#warning USING DEFAULT ENDPOINT OUT ADDRESS#undef CONFIG_USBD_SERIAL_OUT_ENDPOINT#define CONFIG_USBD_SERIAL_OUT_ENDPOINT DFL_OUT_ADDR#endif#if defined(ABS_IN_ADDR)#warning#warning USING ABS ENDPOINT IN ADDRESS#undef CONFIG_USBD_SERIAL_IN_ENDPOINT#if ABS_IN_ADDR#define CONFIG_USBD_SERIAL_IN_ENDPOINT ABS_IN_ADDR#endif#elif defined(MAX_IN_ADDR) && defined(CONFIG_USBD_SERIAL_IN_ENDPOINT) && (CONFIG_USBD_SERIAL_IN_ENDPOINT > MAX_IN_ADDR)#warning#warning USING DEFAULT ENDPOINT IN ADDRESS#undef CONFIG_USBD_SERIAL_IN_ENDPOINT#define CONFIG_USBD_SERIAL_IN_ENDPOINT DFL_IN_ADDR#endif#if defined(ABS_INT_ADDR)#warning#warning USING ABS ENDPOINT INT ADDRESS#undef CONFIG_USBD_SERIAL_INT_ENDPOINT#if ABS_INT_ADDR#define CONFIG_USBD_SERIAL_INT_ENDPOINT ABS_INT_ADDR#endif#elif defined(MAX_INT_ADDR) && defined(CONFIG_USBD_SERIAL_INT_ENDPOINT) && (CONFIG_USBD_SERIAL_INT_ENDPOINT > MAX_INT_ADDR)#warning#warning USING DEFAULT ENDPOINT INT ADDRESS#undef CONFIG_USBD_SERIAL_INT_ENDPOINT#define CONFIG_USBD_SERIAL_INT_ENDPOINT DFL_INT_ADDR#endif#if defined(MAX_OUT_PKTSIZE) && defined(CONFIG_USBD_SERIAL_OUT_PKTSIZE) && CONFIG_USBD_SERIAL_OUT_PKTSIZE > MAX_OUT_PKTSIZE#warning#warning OVERIDING ENDPOINT OUT PKTSIZE#undef CONFIG_USBD_SERIAL_OUT_PKTSIZE#define CONFIG_USBD_SERIAL_OUT_PKTSIZE MAX_OUT_PKTSIZE#endif#if defined(MAX_IN_PKTSIZE) && defined(CONFIG_USBD_SERIAL_IN_PKTSIZE) && CONFIG_USBD_SERIAL_IN_PKTSIZE > MAX_IN_PKTSIZE#warning#warning OVERIDING ENDPOINT IN PKTSIZE#undef CONFIG_USBD_SERIAL_IN_PKTSIZE#define CONFIG_USBD_SERIAL_IN_PKTSIZE MAX_IN_PKTSIZE#endif#if defined(MAX_INT_PKTSIZE) && defined(CONFIG_USBD_SERIAL_INT_PKTSIZE) && CONFIG_USBD_SERIAL_INT_PKTSIZE > MAX_INT_PKTSIZE#warning#warning OVERIDING ENDPOINT INT PKTSIZE#undef CONFIG_USBD_SERIAL_INT_PKTSIZE#define CONFIG_USBD_SERIAL_INT_PKTSIZE MAX_INT_PKTSIZE#endifstruct usb_serial_private { int interface; struct usb_device_instance *device; rwlock_t rwlock;};/* Module Parameters ************************************************************************* */static char *dbg = NULL;static u32 vendor_id;static u32 product_id;static u32 txqueue_urbs;static u32 txqueue_bytes = 3032;#ifndef CONFIG_USBD_SERIAL_SAFE_DEFAULT#define CONFIG_USBD_SERIAL_SAFE_DEFAULT 0#endif#ifndef CONFIG_USBD_SERIAL_SAFE_PADDED#define CONFIG_USBD_SERIAL_SAFE_PADDED 0#endifstatic int safe = CONFIG_USBD_SERIAL_SAFE_DEFAULT;static int padded = CONFIG_USBD_SERIAL_SAFE_PADDED;MODULE_PARM (dbg, "s");MODULE_PARM (vendor_id, "i");MODULE_PARM (product_id, "i");MODULE_PARM (txqueue_urbs, "i");MODULE_PARM (txqueue_bytes, "i");MODULE_PARM (safe, "i");MODULE_PARM (padded, "i");MODULE_PARM_DESC (dbg, "USB Device Debug options");MODULE_PARM_DESC (vendor_id, "USB Device Vendor ID");MODULE_PARM_DESC (product_id, "USB Device Product ID");MODULE_PARM_DESC (txqueue_urbs, "Maximum TX Queue Urbs");MODULE_PARM_DESC (txqueue_bytes, "Maximum TX Queue Bytes");MODULE_PARM_DESC (safe, "Safe Encapsulation");MODULE_PARM_DESC (padded, "Safe Encapsulation Padding");/* Debug switches (module parameter "dbg=...") *********************************************** */extern int dbgflg_usbdfd_init;int dbgflg_usbdfd_usbe;int dbgflg_usbdfd_tx;int dbgflg_usbdfd_rx;int dbgflg_usbdfd_loopback;static debug_option dbg_table[] = { {&dbgflg_usbdfd_init, NULL, "init", "initialization and termination"}, {&dbgflg_usbdfd_usbe, NULL, "usbe", "USB events"}, {&dbgflg_usbdfd_rx, NULL, "rx", "receive (from host)"}, {&dbgflg_usbdfd_tx, NULL, "tx", "transmit (to host)"}, {&dbgflg_usbdfd_loopback, NULL, "loop", "loopback mode if non-zero"}, {NULL, NULL, "ser", "serial device (tty) handling"}, {NULL, NULL, NULL, NULL}};#define dbg_init(lvl,fmt,args...) dbgPRINT(dbgflg_usbdfd_init,lvl,fmt,##args)#define dbg_usbe(lvl,fmt,args...) dbgPRINT(dbgflg_usbdfd_usbe,lvl,fmt,##args)#define dbg_rx(lvl,fmt,args...) dbgPRINT(dbgflg_usbdfd_rx,lvl,fmt,##args)#define dbg_tx(lvl,fmt,args...) dbgPRINT(dbgflg_usbdfd_tx,lvl,fmt,##args)#define dbg_loop(lvl,fmt,args...) dbgPRINT(dbgflg_usbdfd_loopback,lvl,fmt,##args)/* ******************************************************************************************* */static int serial_created;static struct usb_serial_private *serial_private_array[MAX_INTERFACES];static rwlock_t serial_rwlock = RW_LOCK_UNLOCKED; // lock for serproto device array accessstatic __inline__ struct usb_serial_private *get_serial_private (int interface){ if (interface < 0 || interface >= MAX_INTERFACES) { return NULL; } return serial_private_array[interface];}/* usb-func.c ******************************************************************************** *//* Communications Interface Class descriptions */static struct usb_endpoint_description serial_default[] = { {bEndpointAddress:CONFIG_USBD_SERIAL_OUT_ENDPOINT, bmAttributes:BULK, wMaxPacketSize:CONFIG_USBD_SERIAL_OUT_PKTSIZE, bInterval:0, direction:OUT, transferSize:CONFIG_USBD_SERIAL_OUT_PKTSIZE,}, {bEndpointAddress:CONFIG_USBD_SERIAL_IN_ENDPOINT, bmAttributes:BULK, wMaxPacketSize:CONFIG_USBD_SERIAL_IN_PKTSIZE, bInterval:0, direction:IN, transferSize:CONFIG_USBD_SERIAL_IN_PKTSIZE,},#if defined(CONFIG_USBD_SERIAL_INT_ENDPOINT) && (CONFIG_USBD_SERIAL_INT_ENDPOINT > 0) {bEndpointAddress:CONFIG_USBD_SERIAL_INT_ENDPOINT, bmAttributes:INTERRUPT, wMaxPacketSize:CONFIG_USBD_SERIAL_INT_PKTSIZE, bInterval:0, direction:IN, transferSize:CONFIG_USBD_SERIAL_INT_PKTSIZE,},#endif};/* Data Interface Alternate description(s) */static __devinitdata struct usb_alternate_description serial_data_alternate_descriptions[] = { {iInterface:"Simple Serial Data Interface - Bulk mode", bAlternateSetting:0, endpoints:sizeof (serial_default) / sizeof (struct usb_endpoint_description), endpoint_list:serial_default,},};/* Interface description(s) */static __devinitdata struct usb_interface_description serial_interfaces[] = { {iInterface:"Simple Serial Data Interface", bInterfaceClass:LINEO_CLASS, bInterfaceSubClass:LINEO_SUBCLASS_SAFESERIAL, bInterfaceProtocol:LINEO_SAFESERIAL_CRC, alternates:sizeof (serial_data_alternate_descriptions) / sizeof (struct usb_alternate_description), alternate_list:serial_data_alternate_descriptions,},};#ifdef CONFIG_USBD_SERIAL_CDC/* * CDC ACM Configuration *//* Communication Interface Class descriptions */static struct usb_class_description cdc_comm_class_descriptions[] = { { CS_INTERFACE, USB_ST_HEADER, 0, { header: { bcdCDC: CLASS_BCD_VERSION, } }}, { CS_INTERFACE, USB_ST_UF, 1, { union_function: { bMasterInterface: 0, bSlaveInterface: { 1 }, }}}, { CS_INTERFACE, USB_ST_CMF, 0, { call_management: { bmCapabilities: 0, bDataInterface: 1, }}}, { CS_INTERFACE, USB_ST_ACMF, 0, { abstract_control: { bmCapabilities: 0, }}},};/* Data Interface Alternate 1 endpoints */static __devinitdata struct usb_endpoint_description serial_alt_1_endpoints[] = { {bEndpointAddress:CONFIG_USBD_SERIAL_OUT_ENDPOINT, bmAttributes:BULK, wMaxPacketSize:CONFIG_USBD_SERIAL_OUT_PKTSIZE, bInterval:0, direction:OUT, transferSize:CONFIG_USBD_SERIAL_OUT_PKTSIZE,}, {bEndpointAddress:CONFIG_USBD_SERIAL_IN_ENDPOINT, bmAttributes:BULK, wMaxPacketSize:CONFIG_USBD_SERIAL_IN_PKTSIZE, bInterval:0, direction:IN, transferSize:CONFIG_USBD_SERIAL_OUT_PKTSIZE,},#if defined(CONFIG_USBD_SERIAL_INT_ENDPOINT) && (CONFIG_USBD_SERIAL_INT_ENDPOINT > 0) {bEndpointAddress:CONFIG_USBD_SERIAL_INT_ENDPOINT, bmAttributes:INTERRUPT, wMaxPacketSize:CONFIG_USBD_SERIAL_INT_PKTSIZE, bInterval:0, direction:IN, transferSize:CONFIG_USBD_SERIAL_INT_PKTSIZE,},#endif};/* Data Interface Alternate description(s) */static __devinitdata struct usb_alternate_description cdc_comm_alternate_descriptions[] = { {iInterface:"CDC ACM Comm Interface", bAlternateSetting:0, classes:sizeof (cdc_comm_class_descriptions) / sizeof (struct usb_class_description), class_list:cdc_comm_class_descriptions,},};static __devinitdata struct usb_alternate_description cdc_data_alternate_descriptions[] = { {iInterface:"CDC ACM Data Interface - Disabled mode",#ifdef CONFIG_ARCH_LUBBOCK bAlternateSetting:1,},#else bAlternateSetting:0,},#endif {iInterface:"CDC ACM Data Interface - Bulk mode",#ifdef CONFIG_ARCH_LUBBOCK bAlternateSetting:0,#else bAlternateSetting:1,#endif endpoints:sizeof (serial_alt_1_endpoints) / sizeof (struct usb_endpoint_description), endpoint_list:serial_alt_1_endpoints,},};/* Interface description(s) */static __devinitdata struct usb_interface_description cdc_interfaces[] = { {iInterface:"CDC ACM Communication Interface", bInterfaceClass:COMMUNICATIONS_INTERFACE_CLASS, bInterfaceSubClass:COMMUNICATIONS_ACM_SUBCLASS, bInterfaceProtocol:COMMUNICATIONS_NO_PROTOCOL, alternates:sizeof (cdc_comm_alternate_descriptions) / sizeof (struct usb_alternate_description), alternate_list:cdc_comm_alternate_descriptions,}, {iInterface:"CDC ACM Data Interface", bInterfaceClass:DATA_INTERFACE_CLASS, bInterfaceSubClass:COMMUNICATIONS_NO_SUBCLASS, bInterfaceProtocol:COMMUNICATIONS_NO_PROTOCOL, alternates:sizeof (cdc_data_alternate_descriptions) / sizeof (struct usb_alternate_description), alternate_list:cdc_data_alternate_descriptions,},};#endif /* CONFIG_USBD_SERIAL_CDC */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -