USB匯流排號和設備號是如何分配的?
使用時
lsusb
,我意識到USB設備的匯流排號和設備號會不時變化。據我了解,每次重新啟動時匯流排編號都會發生變化。設備編號將在每次重新連接時更改。我的問題是系統使用什麼算法來獲取匯流排號和設備號?尤其是設備號,是不是單調的?是否會出現在不重新啟動作業系統的情況下,一個重新連接的設備正在使用另一個重新連接的設備的舊匯流排號和設備號的情況?
注意:這是一個Linux答案;其他核心將採用稍微不同的方式來處理這個問題。
語境:
不談論 PCI 匯流排就很難談論 USB 匯流排。CPU 不能直接與 USB 匯流排通信,但發生的情況是 CPU 與連接有 USB 控制器的 PCI 匯流排通信(USB 控制器/集線器就是
lsusb
所謂的 USB 匯流排)。PCI 匯流排根據它們與 CPU 的距離進行編號,例如:+-----+ | CPU | +-----+ | PCI Bus 0 ---+--+-----------------------------+ | | +---+----+ +----+---+ | Bridge | | Bridge | +---+----+ +----+---+ | PCI bus 1 | PCI bus 2 --+--------+ +-------+-------------+ | | | Disk Controller USB Controller Network Card (Device 00) (Device 00) (Device 01)
往裡看,
man lspci
我們看到以下內容:Slot The name of the slot where the device resides ([domain:]bus:device.function). This tag is always the first in a record.
因此,我們現在知道如何解釋 PCI 編號。接下來,我們將查看連接到 PCI 匯流排的 USB 控制器。我目前使用的機器有一個有趣的 USB 配置,因此我將使用它作為範例:
$ lspci -tv -[0000:00]-+-00.0 Advanced Micro Devices, Inc. [AMD] RS780 Host Bridge +-01.0-[01]----05.0 Advanced Micro Devices, Inc. [AMD/ATI] RS780M [Mobility Radeon HD 3200] +-04.0-[02]----00.0 Qualcomm Atheros AR928X Wireless Network Adapter (PCI-Express) +-05.0-[03]----00.0 Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller +-06.0-[04-06]-- +-11.0 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 SATA Controller [IDE mode] +-12.0 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI0 Controller +-12.1 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0 USB OHCI1 Controller +-12.2 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB EHCI Controller +-13.0 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI0 Controller +-13.1 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0 USB OHCI1 Controller +-13.2 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB EHCI Controller +-14.0 Advanced Micro Devices, Inc. [AMD/ATI] SBx00 SMBus Controller +-14.1 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 IDE Controller +-14.2 Advanced Micro Devices, Inc. [AMD/ATI] SBx00 Azalia (Intel HDA) +-14.3 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 LPC host controller +-14.4-[07]-- +-14.5 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI2 Controller +-18.0 Advanced Micro Devices, Inc. [AMD] Family 11h Processor HyperTransport Configuration +-18.1 Advanced Micro Devices, Inc. [AMD] Family 11h Processor Address Map +-18.2 Advanced Micro Devices, Inc. [AMD] Family 11h Processor DRAM Controller +-18.3 Advanced Micro Devices, Inc. [AMD] Family 11h Processor Miscellaneous Control \-18.4 Advanced Micro Devices, Inc. [AMD] Family 11h Processor Link Control
等等等等,這些優點是什麼?在頂部,我們有域和 PCI 匯流排
-[0000:00]
(這台機器只有一個 PCI 匯流排)。然後我們有幾個設備連接到該匯流排。讓我們看看哪些是USB設備:$ lspci -tv | grep -i usb +-12.0 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI0 Controller +-12.1 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0 USB OHCI1 Controller +-12.2 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB EHCI Controller +-13.0 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI0 Controller +-13.1 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0 USB OHCI1 Controller +-13.2 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB EHCI Controller +-14.5 Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI2 Controller
好的,現在讓我們將其與
lsusb
(我sort
只是為了以後更容易搜尋列表)進行比較:$ lsusb | sort Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 002 Device 002: ID 174f:5a31 Syntek Sonix USB 2.0 Camera Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 003 Device 002: ID 046d:c019 Logitech, Inc. Optical Tilt Wheel Mouse Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 007 Device 002: ID 0b05:1751 ASUSTek Computer, Inc. BT-253 Bluetooth Adapter
再等等。我們有 7 個 USB 設備,
lspci
但有 10 個設備lsusb
!lspci
僅列出 USB 控制器;一個控制器可以連接多個 USB 設備。讓我們探索/sys/bus/
一下這是如何發生的。$ ls -l /sys/bus/usb/devices/ ... 1-0:1.0 -> ../../../devices/pci0000:00/0000:00:12.2/usb1/1-0:1.0 ... 2-0:1.0 -> ../../../devices/pci0000:00/0000:00:13.2/usb2/2-0:1.0 ... 2-1 -> ../../../devices/pci0000:00/0000:00:13.2/usb2/2-1 ... 2-1:1.0 -> ../../../devices/pci0000:00/0000:00:13.2/usb2/2-1/2-1:1.0 ... 2-1:1.1 -> ../../../devices/pci0000:00/0000:00:13.2/usb2/2-1/2-1:1.1 ... 3-0:1.0 -> ../../../devices/pci0000:00/0000:00:12.0/usb3/3-0:1.0 ... 3-1 -> ../../../devices/pci0000:00/0000:00:12.0/usb3/3-1 ... 3-1:1.0 -> ../../../devices/pci0000:00/0000:00:12.0/usb3/3-1/3-1:1.0 ... 4-0:1.0 -> ../../../devices/pci0000:00/0000:00:12.1/usb4/4-0:1.0 ... 5-0:1.0 -> ../../../devices/pci0000:00/0000:00:13.0/usb5/5-0:1.0 ... 6-0:1.0 -> ../../../devices/pci0000:00/0000:00:13.1/usb6/6-0:1.0 ... 7-0:1.0 -> ../../../devices/pci0000:00/0000:00:14.5/usb7/7-0:1.0 ... 7-1 -> ../../../devices/pci0000:00/0000:00:14.5/usb7/7-1 ... 7-1:1.0 -> ../../../devices/pci0000:00/0000:00:14.5/usb7/7-1/7-1:1.0 ... 7-1:1.1 -> ../../../devices/pci0000:00/0000:00:14.5/usb7/7-1/7-1:1.1 ... 7-1:1.2 -> ../../../devices/pci0000:00/0000:00:14.5/usb7/7-1/7-1:1.2 ... 7-1:1.3 -> ../../../devices/pci0000:00/0000:00:14.5/usb7/7-1/7-1:1.3 ... usb1 -> ../../../devices/pci0000:00/0000:00:12.2/usb1 ... usb2 -> ../../../devices/pci0000:00/0000:00:13.2/usb2 ... usb3 -> ../../../devices/pci0000:00/0000:00:12.0/usb3 ... usb4 -> ../../../devices/pci0000:00/0000:00:12.1/usb4 ... usb5 -> ../../../devices/pci0000:00/0000:00:13.0/usb5 ... usb6 -> ../../../devices/pci0000:00/0000:00:13.1/usb6 ... usb7 -> ../../../devices/pci0000:00/0000:00:14.5/usb7
現在,這開始變得有意義了:我們有 7 個 USB 控制器作為設備插入 PCI 匯流排。例如,USB 匯流排 001 對應 PCI 設備
0000:00:12.2
,USB 匯流排 007 對應0000:00:14.5
設備。設備編號:
以 USB 匯流排編號(例如 )開頭的目錄
7-1:1.2
是連接到 USB 控制器的實際設備。就像 PCI 匯流排可以連接多個設備一樣,USB 控制器(集線器)可以連接多個 USB 設備。設備編號只是計數器:連接的第一個設備獲得 1,下一個獲得 2,依此類推。但還有一點:USB 設計為可熱插拔;因此您可以連接和斷開設備。當您斷開 USB 設備時,核心不會再將設備號用於該 USB 控制器上的任何其他設備。例如,如果您連接和斷開筆式驅動器並繼續操作,
lsusb
您將看到筆式驅動器的設備編號上升。巴士編號:
如果您仔細閱讀了上述內容,您可能會想知道我沒有觸及的一件事。PCI 編號的順序與 USB 控制器的編號順序不對應!讓我們再看一遍:
USB | PCI -----+---- usb1 | 0000:00/0000:00:12.2 usb2 | 0000:00/0000:00:13.2 usb3 | 0000:00/0000:00:12.0 usb4 | 0000:00/0000:00:12.1 usb5 | 0000:00/0000:00:13.0 usb6 | 0000:00/0000:00:13.1 usb7 | 0000:00/0000:00:14.5
清單是有序的,但並不完全。前兩個 USB 控制器似乎出現故障。然而,這是有原因的:如果您仔細查看
lspci
上面的輸出,您會發現那些是EHCI
USB (USB 2.0),而所有其他 USB 控制器都是OHCI
USB (USB 1.x)。因此我們可以將這個表重新繪製為:
USB | PCI -----+---- usb1 | 0000:00/0000:00:12.2 usb2 | 0000:00/0000:00:13.2 USB 2.0 -----+--------------------------------- usb3 | 0000:00/0000:00:12.0 USB 1.x usb4 | 0000:00/0000:00:12.1 usb5 | 0000:00/0000:00:13.0 usb6 | 0000:00/0000:00:13.1 usb7 | 0000:00/0000:00:14.5
數字分配變得清晰!