当前位置: 首页 > news >正文

Linux子系统 - USB描述符结构体

 

目录

简介:

1、USB设备描述符

usb_device

usb_device_descriptor - 设备描述符

2、USB配置描述符

usb_host_config 

usb_config_descriptor - 配置描述符

3、USB接口描述符

usb_interface 

usb_host_interface

usb_interface_descriptor - 接口描述符

4、USB端点描述符

usb_host_endpoint

usb_endpoint_descriptor - 端点描述符


简介:


        USB描述符是USB设备中用于描述设备特性的数据结构,主要用于帮助USB主机识别和管理设备。USB描述符存储在设备的固件中,当设备连接到主机时,主机通过发送特定的请求来获取这些描述符信息。

描述符类型
设备描述符(struct usb_device_descriptor)1
配置描述符(struct usb_config_descriptor)2
字符串描述符3
接口字符串(struct usb_interface_descriptor)4
端点描述符(struct usb_endpoint_descriptor)5
  • 一个设备只有一个设备描述符
  • 一个设备描述符可以有多个配置描述符,但通常情况下也是只有一个配置描述符
  • 一个配置描述符可以有多个接口描述符,这个很常见:如声卡设备就有两个接口:录音接口和播放接口,UVC设备至少两个接口:视频控制接口和视频流接口
  • 一个接口描述符可以有多个端点描述符

1、USB设备描述符

usb设备数据结构 usb_device包含了一个usb设备所有的描述符信息及类特定描述符信息。

usb_device

struct usb_device {
    int devnum;         //devnum只是usb设备在一条usb总线上的编号.一条usb_bus_type类型的总线上最多可以连上128个设备
    char devpath [16];   /* Use in messages: /port/port/...*/  //对于root hub.会将dev->devpath[0]=’0’
    enum usb_device_state   state;  //设备的状态Attached,Powered,Default,Address,Configured,Suspended;
                               //Attached表示设备已经连接到usb接口上了,是hub检测到设备时的初始状态。那么这里所谓的USB_STATE_NOTATTACHED就是表示设备并没有Attached。
                               //Address状态表示主机分配了一个唯一的地址给设备,此时设备可以使用缺省管道响应主机的请求
                               //Configured状态表示设备已经被主机配置过了,也就是协议里说的处理了一个带有非0值的SetConfiguration()请求,此时主机可以使用设备提供的所有功能
                               //Suspended挂起状态,为了省电,设备在指定的时间内,3ms吧,如果没有发生总线传输,就要进入挂起状态。此时,usb设备要自己维护包括地址、配置在内的信息             

    enum usb_device_speed   speed;  /* high/full/low (or error) */
    struct usb_tt   *tt;            //如果一个高速设备里有这么一个TT,那么就可以连接低速/全速设备,如不然,那低速/全速设备没法用,只能连接到OHCI/UHCI那边出来的hub口里。
    int             ttport;         //如果一个高速设备里有这么一个TT,那么就可以连接低速/全速设备,如不然,那低速/全速设备没法用,只能连接到OHCI/UHCI那边出来的hub口里。

    unsigned int toggle[2];         /* one bit for each endpoint     //他实际上就是一个位图.IN方向的是toggle[0].OUT方向的是toggle[1].其实,这个数组中的每一位表示ep的toggle值
                                             * ([0] = IN, [1] = OUT) */它里面的每一位表示的就是每个端点当前发送或接收的数据包是DATA0还是DATA1
    
    struct usb_device *parent;      /* our hub, unless we're the root */
                              //USB设备是从Root Hub开始,一个一个往外面连的,比如Root Hub有4个口,每个口连一个USB设备,比如其中有一个是Hub,那么这个Hub有可以继续有多个口,于是一级一级的往下连,
                               //最终连成了一棵树。
    struct usb_bus *bus;            /* Bus we're part of */设备所在的总线
    struct usb_host_endpoint ep0;   //端点0的特殊地位决定了她必将受到特殊的待遇,在struct usb_device对象产生的时候它就要初始化

    struct device dev;              /* Generic device interface */嵌入到struct usb_device结构里的struct device结构

    struct usb_device_descriptor descriptor;/* Descriptor */设备描述符,此结构体的bMaxPacketSize0 filed保存了端点0的maximum packet size
    struct usb_host_config *config; //设备拥有的所有配置(配置描述符相关)

    struct usb_host_config *actconfig;//设备正在使用的配置(配置描述符相关)
    struct usb_host_endpoint *ep_in[16];//ep_in[16],359行,ep_out[16],除了端点0,一个设备即使在高速模式下也最多只能再有15个IN端点和15个OUT端点,端点0太特殊了,
    struct usb_host_endpoint *ep_out[16];//对应的管道是Message管道,又能进又能出特能屈能伸的那种,所以这里的ep_in和ep_out数组都有16个值

    char **rawdescriptors;          /* Raw descriptors for each config */

    unsigned short bus_mA;          /* Current available from the bus */这个值是在host controller的驱动程序中设置的,通常来讲,计算机的usb端口可以提供500mA的电流
    u8 portnum;                     //不管是root hub还是一般的hub,你的USB设备总归要插在一个hub的端口上才能用,portnum就是那个端口号。
    u8 level;                       //层次,也可以说是级别,表征usb设备树的级连关系。Root Hub的level当然就是0,其下面一层就是level 1,再下面一层就是level 2,依此类推

    unsigned discon_suspended:1;    /* Disconnected while suspended */
    unsigned have_langid:1;         /* whether string_langid is valid */
    int string_langid;              /* language ID for strings */

    /* static strings from the device */
    char *product;                  /* iProduct string, if present */
    char *manufacturer;             /* iManufacturer string, if present */
    char *serial;                   /* iSerialNumber string, if present */
                                    //分别用来保存产品、厂商和序列号对应的字符串描述符信息

    struct list_head filelist;
#ifdef CONFIG_USB_DEVICE_CLASS
    struct device *usb_classdev;
#endif

#ifdef CONFIG_USB_DEVICEFS
    struct dentry *usbfs_dentry;    /* usbfs dentry entry for the device */
#endif
    /*
    * Child devices - these can be either new devices
    * (if this is a hub device), or different instances
    * of this same device.
    *
    * Each instance needs its own set of data structures.
    */

    int maxchild;                   /* Number of ports if hub */
    struct usb_device *children[USB_MAXCHILDREN];

    int pm_usage_cnt;               /* usage counter for autosuspend */
    u32 quirks;                     //quirk就是用来判断这些有毛病的产品啥毛病的

#ifdef CONFIG_PM
    struct delayed_work autosuspend; /* for delayed autosuspends */
    struct mutex pm_mutex;          /* protects PM operations */
    unsigned long last_busy;        /* time of last use */
    int autosuspend_delay;          /* in jiffies */

    unsigned auto_pm:1;             /* autosuspend/resume in progress */
    unsigned do_remote_wakeup:1;    /* remote wakeup should be enabled */
    unsigned autosuspend_disabled:1; /* autosuspend and autoresume */
    unsigned autoresume_disabled:1;  /*  disabled by the user */
#endif
};

usb_device_descriptor - 设备描述符

struct usb_device_descriptor {
 __u8  bLength;                    //本描述符的size
 __u8  bDescriptorType;            //描述符的类型,这里是设备描述符DEVICE
 __u16 bcdUSB;                     //指明usb的版本,比如usb2.0
 __u8  bDeviceClass;               //类
 __u8  bDeviceSubClass;            //子类
 __u8  bDeviceProtocol;            //指定协议
 __u8  bMaxPacketSize0;            //端点0对应的最大包大小
 __u16 idVendor;                   //厂家ID(VID)
 __u16 idProduct;                  //产品ID(PID)
 __u16 bcdDevice;                  //设备的发布号
 __u8  iManufacturer;              //字符串描述符中厂家ID的索引
 __u8  iProduct;                   //字符串描述符中产品ID的索引
 __u8  iSerialNumber;              //字符串描述符中设备序列号的索引
 __u8  bNumConfigurations;         //配置描述符的个数,表示有多少个配置描述符
} __attribute__ ((packed));

2、USB配置描述符

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]; //此配置中所有相关联的接口描述符的列表

    /* the interfaces associated with this configuration,
     * stored in no particular order */
    struct usb_interface *interface[USB_MAXINTERFACES]; //接口描述符相关,这个数组的顺序未必是按照配置里接口号的顺序

    /* Interface information available even when this is not the
     * active configuration */
    struct usb_interface_cache *intf_cache[USB_MAXINTERFACES]; //usb接口的缓存

    unsigned char *extra;   /* Extra descriptors */
    int extralen;
};

usb_config_descriptor - 配置描述符

struct usb_config_descriptor {   
  __u8  bLength;                      //描述符的长度
  __u8  bDescriptorType;              //描述符类型的编号

  __le16 wTotalLength;                //配置 所返回的所有数据的大小,包括除设备描述符以外的所有描述符大小
  __u8  bNumInterfaces;               //配置 所支持的接口个数, 表示有多少个接口描述符
  __u8  bConfigurationValue;          //Set_Configuration命令需要的参数值
  __u8  iConfiguration;               //描述该配置的字符串的索引值
  __u8  bmAttributes;                 //供电模式的选择
  __u8  bMaxPower;                    //设备从总线提取的最大电流
 } __attribute__ ((packed));

3、USB接口描述符

usb_interface是什么的接口:设备的接口,USB接口。


为什么会出现这样的类型出来,根据实际情况,例如:
        一个设备有两种功能 一个键盘 一个音频,两种接口,那这样肯定得要两个驱动程序,一个是键盘驱动程序,一个是音频流驱动程序。两种功能整合在一起称为一个设备。但是不同的接口需要不同的驱动,音频驱动 和键盘驱动。用interface来区分,就有了接口类型。

usb_interface 

struct usb_interface {
	/* array of alternate settings for this interface,
	* stored in no particular order */
	struct usb_host_interface *altsetting;  // 当前接口的可选设置
	/* the currently active alternate setting */
	struct usb_host_interface *cur_altsetting;  // 当前接口使用的设置
	/* number of alternate settings */ 
	unsigned num_altsetting;  // 当前接口具有的可选设置总数

	/* If there is an interface association descriptor then it will list
	* the associated interfaces */
	struct usb_interface_assoc_descriptor *intf_assoc;
	/* minor number this interface is * bound to */ 
	/**
	 * 当前接口的在主设备号为USB_MAJOR时的子设备号,minor只在USB_MAJOR起作用时起作用。 usb设备没有与其它任何子系统关联,
	 * 就需要在对应驱动的probe函数里使用usb_register_dev函数来注册并获得主设备号USB_MAJOR。如果usb设备关联了其它子系统,
	 * 则需要在对应驱动的probe函数里使用相应的注册函数,USB_MAJOR也就该干吗干吗去,用不着它了。比如,usb键盘关联了input子系统,
	 * 驱动对应drivers/hid/usbhid目录下的usbkbd.c文件,在它的probe函数里可以看到使用了input_register_device来注册一个输入设备。 
	 * */
	int minor;   

	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 resetting_device:1; /* true: bandwidth alloc after reset */

	struct device dev; /* interface specific device info */ // linux设备模型里的device嵌在这儿的对象
	struct device *usb_dev;  // 当接口使用USB_MAJOR作为主设备号时,usb_dev才会用到, usb_register_dev和usb_deregister_dev使用这个结构体,usb_dev指向的
                                   就是usb_register_dev函数里创建的usb class device。
	atomic_t pm_usage_cnt; /* usage counter for autosuspend */
	struct work_struct reset_ws; /* for resets in atomic context */
}

struct usb_interface 中的 struct usb_host_interface *cur_altsetting 成员,表示当前正在使用的设置。

usb_host_interface

struct usb_host_interface 
{
         struct usb_interface_descriptor desc;    //接口描述符

          //usb描述符放在usb设备的eeprom里边
         /* array of desc.bNumEndpoint endpoints associated with this
          * interface setting. these will be in no particular order.
          */
         struct usb_host_endpoint *endpoint;    //端点描述符相关

         char *string;               //接口描述字符串

         /* 额外描述符, 通常是类特定描述符,比如uvc中的类特定控制接口描述符,类特定视频流接口描述符*/
         unsigned char *extra;       //额外描述符
         int extralen;
};

具体到接口描述符,它当然就是描述接口本身的信息的。一个接口可以有多个设置,使用不同的设置,描述接口的信息会有些不同,所以接口描述符并没有放在struct usb_interface结构里,而是放在表示接口设置的struct usb_host_interface结构里。

usb_interface_descriptor - 接口描述符
struct usb_interface_descriptor {  
  __u8  bLength;                      //描述符的长度
  __u8  bDescriptorType;              //描述符类型的编号

  __u8  bInterfaceNumber;             //接口的编号
  __u8  bAlternateSetting;            //备用的接口描述符编号,提供不同质量的服务参数.
  __u8  bNumEndpoints;                //要使用的端点个数(不包括端点0), 表示有多少个端点描述符,比如鼠标就只有一个端点
  __u8  bInterfaceClass;              //接口类型,与驱动的id_table 
  __u8  bInterfaceSubClass;           //接口子类型
  __u8  bInterfaceProtocol;           //接口所遵循的协议
  __u8  iInterface;                   //描述该接口的字符串索引值
 } __attribute__ ((packed)

4、USB端点描述符

usb_host_endpoint

struct usb_host_endpoint 
{
         struct usb_endpoint_descriptor desc; //端点描述符
         struct list_head                urb_list;//端点要处理的urb队列.urb是usb通信的主角,设备中的每个端点都可以处理一个urb队列.要想和你的usb通信,就得创建一个urb,并且为它赋好值,
                                   //交给咱们的usb core,它会找到合适的host controller,从而进行具体的数据传输
         void                            *hcpriv;//这是提供给HCD(host controller driver)用的
         struct ep_device                *ep_dev;        /* For sysfs info */

         unsigned char *extra;   /* Extra descriptors */
         int extralen;
       	int enabled;  //使能的话,urb才能被提交到此端口
		int streams;
};

usb_endpoint_descriptor - 端点描述符

struct usb_endpoint_descriptor {
__u8  bLength;                       //描述符的长度
__u8  bDescriptorType;               //描述符类型的编号

__u8  bEndpointAddress;              //端点编号,比如端点1,就是1
__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));

相关文章:

  • 深入探索Windows Hook技术:原理、内核级实现与高级应用
  • 压力测试未覆盖边界条件的后果有哪些
  • 常见的锁策略
  • angular获取roleFormGroup的control值
  • k8s scheduler几种扩展方式的关系及区别
  • RDMA的挑战与限制
  • 远程办公新体验:用触屏手机流畅操作电脑桌面
  • PhotoScissors快速抠图与背景填充
  • 快速入手-基于Django-rest-framework的ModelViewSet终极版(七)
  • redis常见面试题
  • Java全栈面试宝典:多线程与Spring核心机制深度解析
  • Unix/Linux 中 dup、dup2 和 dup3 系统调用解析
  • Windows10清理机器大全集
  • parallelStream线程问题及解决方案
  • 内网服务器无法通过公网地址访问映射到公网的内网服务
  • SQL问题分析与诊断(8)——关键信息(1)
  • E5071C数据保存教程:SNP文件/CSV导出+远程传输步骤一键收藏
  • 蓝桥与力扣刷题(蓝桥 最少刷题数)
  • 2025企业级项目设计三叉戟:权限控制+错误监控+工程化提效实战指南
  • 手动搭建并配置react项目(webpack5)
  • 深圳罗湖网站制作/杭州网站定制
  • 日本网站开发用啥/给公司建网站需要多少钱
  • 企业网站可概括为/游戏广告推广平台
  • 莒县建设局网站/南京网络营销服务
  • 天猫代运营公司/太原seo快速排名怎么样
  • 门户网站 模块/百度指数在线查询