linux usb学习笔记
发布日期:2021-06-30 18:54:00 浏览次数:3 分类:技术文章

本文共 10150 字,大约阅读时间需要 33 分钟。

USB 设备基础知识

usb端点

usb端点用一个结构体来描述

  USB 端点在内核中使用结构 struct usb_host_endpoint 来描述. 这个结构包含真实的端点信息在另一个结构中, 称为 struct usb_endpoint_descriptor. 后者包含所有的 USB-特定 数据, 以设备自身特定的准确格式. 驱动关心的这个结构的成员是:
  bEndpointAddress
这是这个特定端点的 USB 地址. 还包含在这个 8-位 值的是端点的方向. 位掩码USB_DIR_OUT 和USB_DIR_IN 可用来和这个成员比对, 来决定给这个端点的数据是到设备还是到主机.
  bmAttributes
这是端点的类型. 位掩码 USB_ENDPOINT_XFERTYPE_MASK 应当用来和这个值比对,来决定这个端点是否是 USB_ENDPOINT_XFER_ISOC, USB_ENDPOINT_XFER_BULK, 或者是类型 USB_ENDPOINT_XFER_INT. 这些宏定义了同步, 块, 和中断端点, 相应地.LINUX DEVICE DRIVERS,3RD EDITION
  wMaxPacketSize
这是以字节计的这个端点可一次处理的最大大小. 注意驱动可能发送大量的比这个值大的数据到端点, 但是数据会被分为 wMaxPakcetSize 的块, 当真正传送到设备时. 对于高速设备, 这个成员可用来支持端点的一个高带宽模式, 通过使用几个额外位在这个值的高位部分. 关于如何完成的细节见 USB 规范.
  bInterval
如果这个端点是中断类型的, 这个值是为这个端点设置的间隔, 即在请求端点的中断之间的时间. 这个值以毫秒表示.这个结构的成员没有一个”传统” Linux 内核的命名机制. 这是因为这些成员直接对应于USB 规范中的名子. USB 内核程序员认为使用规定的名子更重要, 以便在阅读规范时减少混乱, 不必使这些名子对Linux 程序员看起来熟悉

在usb.h这个文件里面

/** * struct usb_host_endpoint - host-side endpoint descriptor and queue * @desc: descriptor for this endpoint, wMaxPacketSize in native byteorder * @ss_ep_comp: SuperSpeed companion descriptor for this endpoint * @urb_list: urbs queued to this endpoint; maintained by usbcore * @hcpriv: for use by HCD; typically holds hardware dma queue head (QH) *  with one or more transfer descriptors (TDs) per urb * @ep_dev: ep_device for sysfs info * @extra: descriptors following this endpoint in the configuration * @extralen: how many bytes of "extra" are valid * @enabled: URBs may be submitted to this endpoint * * USB requests are always queued to a given endpoint, identified by a * descriptor within an active interface in a given USB configuration. */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;};

usb接口

  USB 端点被绑在接口中. USB 接口只处理一类 USB 逻辑连接, 例如一个鼠标, 一个键盘,或者一个音频流. 一些 USB 设备有多个接口, 例如一个 USB 扬声器可能有 2 个接口:一个 USB 键盘给按钮和一个 USB 音频流. 因为一个 USB 接口表示基本的功能, 每个USB 驱动控制一个接口; 因此, 对扬声器的例子, Linux 需要 2 个不同的驱动给一个硬件设备。

  还是在usb.h文件里面
  

/** * struct usb_interface - what usb device drivers talk to * @altsetting: array of interface structures, one for each alternate *  setting that may be selected.  Each one includes a set of *  endpoint configurations.  They will be in no particular order. * @cur_altsetting: the current altsetting. * @num_altsetting: number of altsettings defined. * @intf_assoc: interface association descriptor * @minor: the minor number assigned to this interface, if this *  interface is bound to a driver that uses the USB major number. *  If this interface does not use the USB major, this field should *  be unused.  The driver should set this value in the probe() *  function of the driver, after it has been assigned a minor *  number from the USB core by calling usb_register_dev(). * @condition: binding state of the interface: not bound, binding *  (in probe()), bound to a driver, or unbinding (in disconnect()) * @sysfs_files_created: sysfs attributes exist * @ep_devs_created: endpoint child pseudo-devices exist * @unregistering: flag set when the interface is being unregistered * @needs_remote_wakeup: flag set when the driver requires remote-wakeup *  capability during autosuspend. * @needs_altsetting0: flag set when a set-interface request for altsetting 0 *  has been deferred. * @needs_binding: flag set when the driver should be re-probed or unbound *  following a reset or suspend operation it doesn't support. * @dev: driver model's view of this device * @usb_dev: if an interface is bound to the USB major, this will point *  to the sysfs representation for that device. * @pm_usage_cnt: PM usage counter for this interface * @reset_ws: Used for scheduling resets from atomic context. * @reset_running: set to 1 if the interface is currently running a *      queued reset so that usb_cancel_queued_reset() doesn't try to *      remove from the workqueue when running inside the worker *      thread. See __usb_queue_reset_device(). * @resetting_device: USB core reset the device, so use alt setting 0 as *  current; needs bandwidth alloc after reset. * * USB device drivers attach to interfaces on a physical device.  Each * interface encapsulates a single high level function, such as feeding * an audio stream to a speaker or reporting a change in a volume control. * Many USB devices only have one interface.  The protocol used to talk to * an interface's endpoints can be defined in a usb "class" specification, * or by a product's vendor.  The (default) control endpoint is part of * every interface, but is never listed among the interface's descriptors. * * The driver that is bound to the interface can use standard driver model * calls such as dev_get_drvdata() on the dev member of this structure. * * Each interface may have alternate settings.  The initial configuration * of a device sets altsetting 0, but the device driver can change * that setting using usb_set_interface().  Alternate settings are often * used to control the use of periodic endpoints, such as by having * different endpoints use different amounts of reserved USB bandwidth. * All standards-conformant USB devices that use isochronous endpoints * will use them in non-default settings. * * The USB specification says that alternate setting numbers must run from * 0 to one less than the total number of alternate settings.  But some * devices manage to mess this up, and the structures aren't necessarily * stored in numerical order anyhow.  Use usb_altnum_to_altsetting() to * look up an alternate setting in the altsetting array based on its number. */struct usb_interface {    /* array of alternate settings for this interface,     * stored in no particular order */    struct usb_host_interface *altsetting;    struct usb_host_interface *cur_altsetting;  /* the currently                     * active alternate setting */    unsigned num_altsetting;    /* number of alternate settings */    /* 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 */};

usb配置

  linux 描述 USB 配置使用结构 struct usb_host_config 和整个 USB 设备使用结构struct usb_device. USB 设备驱动通常不会需要读写这些结构的任何值, 因此它们在这里没有详细定义. 好奇的读者可在内核源码树的文件 include/linux/usb.h 中找到对它们的描述

  

/** * struct usb_host_config - representation of a device's configuration * @desc: the device's configuration descriptor. * @string: pointer to the cached version of the iConfiguration string, if *  present for this configuration. * @intf_assoc: list of any interface association descriptors in this config * @interface: array of pointers to usb_interface structures, one for each *  interface in the configuration.  The number of interfaces is stored *  in desc.bNumInterfaces.  These pointers are valid only while the *  the configuration is active. * @intf_cache: array of pointers to usb_interface_cache structures, one *  for each interface in the configuration.  These structures exist *  for the entire life of the device. * @extra: pointer to buffer containing all extra descriptors associated *  with this configuration (those preceding the first interface *  descriptor). * @extralen: length of the extra descriptors buffer. * * USB devices may have multiple configurations, but only one can be active * at any time.  Each encapsulates a different operational environment; * for example, a dual-speed device would have separate configurations for * full-speed and high-speed operation.  The number of configurations * available is stored in the device descriptor as bNumConfigurations. * * A configuration can contain multiple interfaces.  Each corresponds to * a different function of the USB device, and all are available whenever * the configuration is active.  The USB standard says that interfaces * are supposed to be numbered from 0 to desc.bNumInterfaces-1, but a lot * of devices get this wrong.  In addition, the interface array is not * guaranteed to be sorted in numerical order.  Use usb_ifnum_to_if() to * look up an interface entry based on its number. * * Device drivers should not attempt to activate configurations.  The choice * of which configuration to install is a policy decision based on such * considerations as available power, functionality provided, and the user's * desires (expressed through userspace tools).  However, drivers can call * usb_reset_configuration() to reinitialize the current configuration and * all its interfaces. */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];    unsigned char *extra;   /* Extra descriptors */    int extralen;};

转载地址:https://linus.blog.csdn.net/article/details/51518157 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:git入门学习笔记
下一篇:C语言 程序代码编写规范

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月25日 17时20分04秒