参考书:《linux device drivers》、《usb 2.0规范》 《usb3.1规范》《usb白皮书

以linux为例来说明usb系统。

先看一下usb蓝牙适配器的详细信息:

$ lsusb | grep Cambridge
Bus Device : ID 0a12: Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
$ sudo lsusb -s 001:006 -v
[sudo] password for host: Bus 001 Device 006: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass          224 Wireless
  bDeviceSubClass         1 Radio Frequency
  bDeviceProtocol         1 Bluetooth
  bMaxPacketSize0        64
  idVendor           0x0a12 Cambridge Silicon Radio, Ltd
  idProduct          0x0001 Bluetooth Dongle (HCI mode)
  bcdDevice           88.91
  iManufacturer           0
  iProduct                2 CSR8510 A10
  iSerial                 0
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength          177
    bNumInterfaces          2
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0xe0
      Self Powered
      Remote Wakeup
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           3
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0010  1x 16 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0000  1x 0 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0000  1x 0 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       1
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0009  1x 9 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0009  1x 9 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       2
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0011  1x 17 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0011  1x 17 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       3
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0019  1x 25 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0019  1x 25 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       4
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0021  1x 33 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0021  1x 33 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       5
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0031  1x 49 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0031  1x 49 bytes
        bInterval               1
Device Status:     0x0001
  Self Powered

根据《linux device drivers》中的说明,一个设备可以有一个或者多个configuration(通常只有一个),

一个configuration可以有一个或者多个interface,一个interface中可以有一个或者多个endpoint.

上面的蓝牙适配器有1个configuration,这个configuration有7个interface,每个interface有2个或者3个endpoint.

在linux驱动中usb设备驱动与interface相对应。

usb之间最基本的通信是通过endpoint完成的,endpoint之间只能向一个方向发送数据(主机到设备(即OUT endpoint)或者设备到主机(即IN endpoint))。

endpoint传输数据可以是下面四种类型之一:

(1) control endpoint: 用于存取usb设备的不通部分,可以配置设备、向设备发命令或者获取设备状态信息。

(2)interrupt endpoint: 主机向设备请求数据量较少时使用,例如键盘和鼠标

(3)bulk endpoint:用于大量数据传送。 如果传送数据过大,则可能分成多个块传送

(4)isochronous endpoint:可以传送大量数据,通常用于能处理数据丢失以及固定速率传送数据的设备。

usb数据传输实际是通过packet来进行传输的,每个packet由SYNC(1个字节)、PID(1个字节)、CONTENT(不同PID可能有不同长度)和CRC(不同PID可能有不同长度)

详细的packet定义可以查看usb规范。

interface用于处理一种类型的usb连接,例如鼠标或者键盘等。前面的蓝牙适配器有多个interface,可以用来支持多个蓝牙设备。

interface还可能有alternate setting, 可以用来选择不同的interface参数值。

查看pci设备信息:

$ lspci
:00.0 Host bridge: Intel Corporation 82G33/G31/P35/P31 Express DRAM Controller (rev )
:02.0 VGA compatible controller: Intel Corporation 82G33/G31 Express Integrated Graphics Controller (rev )
:1b. Audio device: Intel Corporation NM10/ICH7 Family High Definition Audio Controller (rev )
:1c. PCI bridge: Intel Corporation NM10/ICH7 Family PCI Express Port (rev )
:1c. PCI bridge: Intel Corporation NM10/ICH7 Family PCI Express Port (rev )
:1c. PCI bridge: Intel Corporation NM10/ICH7 Family PCI Express Port (rev )
:1c. PCI bridge: Intel Corporation NM10/ICH7 Family PCI Express Port (rev )
:1d. USB controller: Intel Corporation NM10/ICH7 Family USB UHCI Controller # (rev )
:1d. USB controller: Intel Corporation NM10/ICH7 Family USB UHCI Controller # (rev )
:1d. USB controller: Intel Corporation NM10/ICH7 Family USB UHCI Controller # (rev )
:1d. USB controller: Intel Corporation NM10/ICH7 Family USB UHCI Controller # (rev )
:1d. USB controller: Intel Corporation NM10/ICH7 Family USB2 EHCI Controller (rev )
:1e. PCI bridge: Intel Corporation PCI Bridge (rev e1)
:1f. ISA bridge: Intel Corporation 82801GB/GR (ICH7 Family) LPC Interface Bridge (rev )
:1f. IDE interface: Intel Corporation 82801G (ICH7 Family) IDE Controller (rev )
:1f. IDE interface: Intel Corporation NM10/ICH7 Family SATA Controller [IDE mode] (rev )
:1f. SMBus: Intel Corporation NM10/ICH7 Family SMBus Controller (rev )
:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8101E/RTL8102E PCI Express Fast Ethernet controller (rev )

安装tree:

sudo apt-get install tree

查看sysfs中所有usb设备文件:

$ tree /sys/bus/usb/devices/
/sys/bus/usb/devices/
├── -:1.0 -> ../../../devices/pci0000:/::1d./usb1/-:1.0
├── - -> ../../../devices/pci0000:/::1d./usb1/-
├── -:1.0 -> ../../../devices/pci0000:/::1d./usb1/-/-:1.0
├── -1.2 -> ../../../devices/pci0000:/::1d./usb1/-/-1.2
├── -1.2:1.0 -> ../../../devices/pci0000:/::1d./usb1/-/-1.2/-1.2:1.0
├── -1.2:1.1 -> ../../../devices/pci0000:/::1d./usb1/-/-1.2/-1.2:1.1
├── -1.3 -> ../../../devices/pci0000:/::1d./usb1/-/-1.3
├── -1.3:1.0 -> ../../../devices/pci0000:/::1d./usb1/-/-1.3/-1.3:1.0
├── -1.3:1.1 -> ../../../devices/pci0000:/::1d./usb1/-/-1.3/-1.3:1.1
├── - -> ../../../devices/pci0000:/::1d./usb1/-
├── -:1.0 -> ../../../devices/pci0000:/::1d./usb1/-/-:1.0
├── -:1.0 -> ../../../devices/pci0000:/::1d./usb2/-:1.0
├── - -> ../../../devices/pci0000:/::1d./usb2/-
├── -:1.0 -> ../../../devices/pci0000:/::1d./usb2/-/-:1.0
├── -:1.0 -> ../../../devices/pci0000:/::1d./usb3/-:1.0
├── - -> ../../../devices/pci0000:/::1d./usb3/-
├── -:1.0 -> ../../../devices/pci0000:/::1d./usb3/-/-:1.0
├── -:1.0 -> ../../../devices/pci0000:/::1d./usb4/-:1.0
├── -:1.0 -> ../../../devices/pci0000:/::1d./usb5/-:1.0
├── usb1 -> ../../../devices/pci0000:/::1d./usb1
├── usb2 -> ../../../devices/pci0000:/::1d./usb2
├── usb3 -> ../../../devices/pci0000:/::1d./usb3
├── usb4 -> ../../../devices/pci0000:/::1d./usb4
└── usb5 -> ../../../devices/pci0000:/::1d./usb5

本机蓝牙适配器连接到usb集线器,usb集线器连接到pc的一个usb口上。并且usb集线器还连接了其他的一些设备。

从上面的输出可以推测,1-1开头部分应该是usb集线器上连接的设备。

/sys/devices/pci0000\:00/0000\:00\:1d.7/usb1应该对应host controller设备,1-1对应struct usb_device.

1-1.2表示蓝牙适配器所在hub口,1-1.2:1.0和1-1.2:1.1可能分别表示蓝牙适配器的接口。

 

查看这两个接口的endpoint:

$ ls -d /sys/devices/pci0000\:/\:\:1d./usb1/-/-1.2/-1.2\:1.0/ep*
/sys/devices/pci0000:/::1d./usb1/-/-1.2/-1.2:1.0/ep_02 /sys/devices/pci0000:/::1d./usb1/-/-1.2/-1.2:1.0/ep_82
/sys/devices/pci0000:/::1d./usb1/-/-1.2/-1.2:1.0/ep_81 $ ls -d /sys/devices/pci0000\:00/0000\:00\:1d.7/usb1/1-1/1-1.2/1-1.2\:1.1/ep*
/sys/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.2/1-1.2:1.1/ep_03  /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.2/1-1.2:1.1/ep_83

1-1.2:1.0下有三个endpoint,对应与lsusb输出信息中的第一个interface中的3个endpoint.

1-1.2:1.1下有两个endpoint,对应于lsusb输出信息中的其他interface中的2个endpoint。

但是lsusb中总共有7个interface,最后6个interface只用一个interface就能表示,似乎看起来很奇怪。

仔细看上面的输出信息可以知道,最后6个interface确实是一个interface,只是使用了alterate setting,其bAlternateSetting的值分别是0-5,其它组成部分的值一模一样。

linux usb简介的更多相关文章

  1. 《网蜂A8实战演练》——8.Linux USB 主机控制器和设备驱动

    USB 的全称是 Universal Serial Bus,顾名思义:通用串行总线. 提到总线,联想一下,在你心目中总线总是用来干嘛的?还记得 I2C 总线? I2C 总线上挂有二条信号线,一条是 S ...

  2. Linux usb 2. 协议分析

    文章目录 0. 背景 1. USB 协议传输格式 1.1 Packet 1.1.1 Token Packet 1.1.2 Data Packet 1.1.3 Handshake Packet 1.1. ...

  3. linux 文件系统简介

    linux文件系统简介   文件系统是linux的一个十分基础的知识,同时也是学习linux的必备知识. 本文将站在一个较高的视图来了解linux的文件系统,主要包括了linux磁盘分区和目录.挂载基 ...

  4. Linux USB Project

    转自:http://www.linux-usb.org/ Welcome to the home of the Linux USB Project This web site was created ...

  5. Linux USB驱动

    linux usb 驱动详解 一 http://blog.163.com/cl2006ky@126/blog/static/87195173201131245557340/ USB设备驱动开发-USB ...

  6. Linux 内核简介

    Linux内核简介 一.系统架构 (1). Linux系统架构 ##用户空间: 文件系统 C库 ##内核空间: 接口 内核 (2). Linux内核架构 二.Linux内核源代码 下载地址 www.k ...

  7. Linux usb子系统(二):USB设备驱动usb-skeleton.c

    usb驱动分为通过usbfs操作设备的用户空间驱动,内核空间的内核驱动.两者不能同时进行,否则容易引发对共享资源访问的问题,死锁!使用了内核驱动,就不能在usbfs里驱动该设备. 下面转载的一篇分析u ...

  8. Linux usb子系统(一):子系统架构

    一.USB协议基础知识   前序:USB概念概述 USB1.0版本速度1.5Mbps(低速USB) USB1.1版本速度12Mbps(全速USB)  USB2.0版本速度480Mbps(高速USB). ...

  9. linux usb 驱动详解

    linux usb 驱动详解 USB 设备驱动代码通过urb和所有的 USB 设备通讯.urb用 struct urb 结构描述(include/linux/usb.h ). urb 以一种异步的方式 ...

随机推荐

  1. Struts2 是什么?

    Struts2是流行和成熟的基于MVC设计模式的Web应用程序框架. Struts2不只是Struts1下一个版本,它是一个完全重写的Struts架构. WebWork框架开始以Struts框架为基础 ...

  2. 这篇文章主要为大家详细介绍了jQuery密码强度验证控件使用详解的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    本文实例为大家分享了jQuery密码强度验证控件,供大家参考,具体内容如下 <html>   <head>     <meta http-equiv="Cont ...

  3. OpenCV学习笔记十一:opencv_ocl模块

    一,简介: 基于OpenCL优化的代码.

  4. Quartz.NET 作业调度

    Quartz 简介: Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调 ...

  5. PMP杂谈--项目组织,矩阵组织,职能型组织,复合型组织

    (1)项目组织的优缺点:优:项目经理权力大:缺:解散时焦虑.没有提拔权力,不利于专业技术积累,设备反复.难以保证资源的充分利用. (2)矩阵组织的优缺点:优:资源利用率高,横向信息沟通.项目经理权力提 ...

  6. [Spring Data Repositories]学习笔记--定义自己的repository

    有时,我们会需要用到自己定义的一些查询方法,可以按照下面几步进行. 1. 定义一个包含该方法的接口 Interface UserRepositoryCustom { public void someC ...

  7. IIPP迷你项目(二)"Guess the Number!"

    本来这个程序是早就编完了的,一直没时间发布博客.时至今日已时隔多天,也算是复习一下事件驱动型编程的过程吧. 1 事件驱动型编程 本质上这次的作业是披着猜数字皮的图形化界面编程,好在 simplegui ...

  8. 1280 前缀后缀集合(map)

    1280 前缀后缀集合 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个数组包含N个正整数,其中有些是重复的.一个前缀后缀集是满足 ...

  9. Introduction to Mathematical Thinking - Week 4

    否定的逻辑 应该思考符号背后表示的逻辑,而不是像操作算术运算符一样操作逻辑符号. 比如 对于任意的 x,x属于自然数,那么 x 是偶数或者奇数:这是对的 如果使用“乘法分配律”拆分,变成“对于任意的x ...

  10. ThinkPHP官网瀑布流实现分享

    很多人都想做瀑布流的效果,这里告诉大家官网使用的方法. 首先要下载瀑布流的插件jquery.masonry.min.js 地址:http://masonry.desandro.com/index.ht ...