usb子系统重要的数据结构分析:
第一:逻辑结构
1. 设备 usb_device //代表一个具有复合功能的设备
2. 配置 usb_host_config //功能的组合
3. 接口 usb_interface //一种功能
4. 设置 usb_host_interface//一种功能的设置
5. 端点 usb_host_endpoint //该设置中的
//-------------------------------------------------------------------------------------------------------------------
struct usb_device {
int devnum;//在usb总线上的地址
char devpath[16];
u32 route;
enum usb_device_state state;//该设备所处的状态,有attached,power,defualt,address,config,suspend
enum usb_device_speed speed;//该设备的速度
struct usb_tt *tt;
int ttport;
unsigned int toggle[2];//实现交替传输,data0,data1
struct usb_device *parent;//该设备的父设备,也就是hub,除非是该设备是root hub
struct usb_bus *bus;//该设备所依附的总线
struct usb_host_endpoint ep0;//该设备的端点0,该端点比较特殊,只属于该设备
struct device dev;//设备驱动模型的dev
struct usb_device_descriptor descriptor;//该device的设备描述符
struct usb_host_config *config;//该device所具有的config
struct usb_host_config *actconfig;//当前生效的config
struct usb_host_endpoint *ep_in[16];//该device的in端点
struct usb_host_endpoint *ep_out[16];//该device的out端点
char **rawdescriptors;
unsigned short bus_mA;//总线上可用的电流
u8 portnum;//父设备的接口号,也就是hub的第几个端口
u8 level;
unsigned can_submit:1;
unsigned persist_enabled:1;
unsigned have_langid:1;
unsigned authorized:1;
unsigned authenticated:1;
unsigned wusb:1;//设备是否是无线usb
int string_langid;
/* static strings from the device */
char *product;
char *manufacturer;
char *serial;
struct list_head filelist;
#ifdef CONFIG_USB_DEVICE_CLASS
struct device *usb_classdev;
#endif
#ifdef CONFIG_USB_DEVICEFS
struct dentry *usbfs_dentry;
#endif
#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
/*otg add ons */
u8 otgdevice; /*device is otg type */
/*otg states from otg driver, suspend, enumerate, disconnect */
u8 otgstate;
void *otgpriv;
void (*otg_notif) (void *otg_priv,
unsigned long notif, unsigned long data);
void *hcd_priv;
void (*hcd_suspend) (void *hcd_priv);
#endif
int maxchild;//如果该设备是hub,该hub有多少端口
struct usb_device *children[USB_MAXCHILDREN];//如果是hub,则表示连接在该hub上的设备
u32 quirks;
atomic_t urbnum;//该设备一共提交的urb数量
unsigned long active_duration;
#ifdef CONFIG_PM
unsigned long last_busy;
int autosuspend_delay;
unsigned long connect_time;
unsigned do_remote_wakeup:1;
unsigned reset_resume:1;
#endif
struct wusb_dev *wusb_dev;
int slot_id;
};
//该结构的操作宏
#define to_usb_device(d) container_of(d, struct usb_device, dev)//通过设备驱动模型的dev,获得usb_device结构体
static inline struct usb_device *interface_to_usbdev(struct usb_interface *intf)
{
return to_usb_device(intf->dev.parent);
}
//-------------------------------------------------------------------------------------------------------------------
struct usb_host_config {
struct usb_config_descriptor desc;//配置描述符
char *string; /* iConfiguration string, if present */
/* List of any Interface Association Descriptors in this
* configuration. */
struct usb_interface_assoc_descriptor *intf_assoc[USB_MAXIADS];
struct usb_interface *interface[USB_MAXINTERFACES];//该config所支持的interface数量,支持的接口数目在配置描述符中描述
/* Interface information available even when this is not the
* active configuration */
struct usb_interface_cache *intf_cache[USB_MAXINTERFACES];
unsigned char *extra; /* Extra descriptors */
int extralen;
};
//-------------------------------------------------------------------------------------------------------------------
struct usb_interface {
struct usb_host_interface *altsetting;//当前接口的可用设置
struct usb_host_interface *cur_altsetting;//当前活动的设置
unsigned num_altsetting;//可选设置的数目
/* If there is an interface association descriptor then it will list
* the associated interfaces */
struct usb_interface_assoc_descriptor *intf_assoc;
int minor; /* minor number this interface is bound to */
enum usb_interface_condition condition; /* state of binding */
unsigned sysfs_files_created:1; /* the sysfs attributes exist */
unsigned ep_devs_created:1; /* endpoint "devices" exist */
unsigned unregistering:1; /* unregistration is in progress */
unsigned needs_remote_wakeup:1; /* driver requires remote wakeup */
unsigned needs_altsetting0:1; /* switch to altsetting 0 is pending */
unsigned needs_binding:1; /* needs delayed unbind/rebind */
unsigned reset_running:1;
unsigned resetting_device:1; /* true: bandwidth alloc after reset */
struct device dev; /* interface specific device info */
struct device *usb_dev;
atomic_t pm_usage_cnt; /* usage counter for autosuspend */
struct work_struct reset_ws; /* for resets in atomic context */
};
#define to_usb_interface(d) container_of(d, struct usb_interface, dev)//通过设备驱动模型的dev,获得usb_interface结构体
static inline void *usb_get_intfdata(struct usb_interface *intf)
{
return dev_get_drvdata(&intf->dev);
}
static inline void usb_set_intfdata(struct usb_interface *intf, void *data)
{
dev_set_drvdata(&intf->dev, data);
}
//-------------------------------------------------------------------------------------------------------------------
//上面的配置中有设置,该设置里有接口描述符和端点
struct usb_host_interface {
struct usb_interface_descriptor desc;//接口描述符
/* array of desc.bNumEndpoint endpoints associated with this
* interface setting. these will be in no particular order.
*/
struct usb_host_endpoint *endpoint;//该设置中的端点
char *string; /* iInterface string, if present */
unsigned char *extra; /* Extra descriptors */
int extralen;
};
//-------------------------------------------------------------------------------------------------------------------
//设置中的端点描述符,单向的传输管道
struct usb_host_endpoint {
struct usb_endpoint_descriptor desc;//端点描述符
struct usb_ss_ep_comp_descriptor ss_ep_comp;
struct list_head urb_list;
void *hcpriv;
struct ep_device *ep_dev; /* For sysfs info */
unsigned char *extra; /* Extra descriptors */
int extralen;
int enabled;
};
第二:描述符
1. 设备描述符struct usb_device_descriptor
2. 配置描述符struct usb_config_descriptor
3. 接口描述符struct usb_interface_descriptor
4. 端点描述符struct usb_endpoint_descriptor
//设备描述符
struct usb_device_descriptor {
__u8 bLength;//端点描述符长度,单位为Byte
__u8 bDescriptorType;//描述符的类型
__le16 bcdUSB;//以bcd码,标出的usb的版本号
__u8 bDeviceClass;//该设备所属的类
__u8 bDeviceSubClass;//该设备所属的子类
__u8 bDeviceProtocol;//该设备所用的协议
__u8 bMaxPacketSize0;//端点0的最大包大小
__le16 idVendor;//厂商id
__le16 idProduct;//产品id
__le16 bcdDevice;//设备版本号
__u8 iManufacturer;//制造商描述符串索引
__u8 iProduct;//产品描述符串索引
__u8 iSerialNumber;//usb串号串索引
__u8 bNumConfigurations;//该设备所拥有的配置个数
} __attribute__ ((packed));
//配置描述符
struct usb_config_descriptor {
__u8 bLength;//端点描述符长度,单位为Byte
__u8 bDescriptorType;//描述符的类型
__le16 wTotalLength;//配置描述符的总长度
__u8 bNumInterfaces;//该配置下的接口个数
__u8 bConfigurationValue;//该配置的索引
__u8 iConfiguration;//配置描述符串索引
__u8 bmAttributes;
__u8 bMaxPower;//该配置所能供应的最大电流,以2mA为单位
} __attribute__ ((packed));
//接口描述符
struct usb_interface_descriptor {
__u8 bLength;//端点描述符长度,单位为Byte
__u8 bDescriptorType;//描述符的类型
__u8 bInterfaceNumber;//接口号
__u8 bAlternateSetting;//可选设置
__u8 bNumEndpoints;//该接口所具有的端点数量
__u8 bInterfaceClass;
__u8 bInterfaceSubClass;
__u8 bInterfaceProtocol;
__u8 iInterface;//接口描述符串索引
} __attribute__ ((packed));
//端点描述符
struct usb_endpoint_descriptor {
__u8 bLength;//端点描述符长度,单位为Byte
__u8 bDescriptorType;//描述符的类型
__u8 bEndpointAddress;//包含端点方向(最高位,使用USB_ENDPOINT_DIR_MASK,它的含义就是0x80,判断端点方向),端点地址(0-3位,与0xff进行与操作)
__u8 bmAttributes;//端点类型,四种传输类型
__le16 wMaxPacketSize;//该端点能发送接收的最大包大小
__u8 bInterval;//主机对设备的查询时间
/* NOTE: these two are _only_ in audio endpoints. */
/* use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof. */
__u8 bRefresh;
__u8 bSynchAddress;
} __attribute__ ((packed));
//注意:0号端点是特殊的端点,它没有自己的端点描述符
总结:这8个数据结构的基本关系是
通过usb_device 可以找到 usb_host_config和struct usb_device_descriptor
通过usb_host_config可以找到usb_interface和struct usb_config_descriptor
通过usb_interface可以找到usb_host_interface
通过usb_host_interface可以找到truct usb_interface_descriptor 和 usb _host_endpoint
通过 usb_host_endpoint可以找到usb_endpoint_descriptor
阅读(154) | 评论(0) | 转发(0) |