usb子系统中重要的数据结构

  1. 设备 usb_device
  2. 配置 usb_host_config
  3. 接口 usb_interface
  4. 设置 usb_host_interface
  5. 端点 usb_host_endpoint

usb_device

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);
}

usb_host_config

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;
};

usb_host_interface

//上面的配置中有设置,该设置里有接口描述符和端点
/* host-side wrapper for one interface setting's parsed descriptors */
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;
};

usb_interface

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; //本接口绑定的次设备号
 enum usb_interface_condition condition; //接口是否绑定
 unsigned sysfs_files_created:1; /* 文件系统存在的文件的属性 */
 unsigned ep_devs_created:1; /* endpoint "devices" exist */
 unsigned unregistering:1; /* 标识卸载interface */
 unsigned needs_remote_wakeup:1; /* 驱动要求远程唤醒 */
 unsigned needs_altsetting0:1; /* 当设置0被推迟时标识 */
 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 */

        //当接口被绑定到usb主设备号的时候,它指向文件系统中表示的usb设备
 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);
}

usb_host_endpoint

//设置中的端点描述符,单向的传输管道
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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值