A. 怎么查看linux usb设备驱动
下面的信息都是在VMware中运行Ubuntu12-04系统上执行的。同样该命令也支持在嵌入式系统中进行USB调试。
一、cat设备节点获取信息
在一些嵌入式开发中需要调试USB功能,经常会cat /sys 下的相关设备节点来查看某些信息,比如说我们可以看到 /sys/bus/usb/devices 目录有多个子目录。进入到某个子目录可以看到usb设备更加详细的信息(可以理解为设备描述符)。
1、usb设备在总线上的信息
// usb设备在总线上的信息
root@ubuntu:/sys/kernel/debug# cd /sys/bus/usb/devices
root@ubuntu:/sys/bus/usb/devices# ll
total 0
drwxr-xr-x 2 root root 0 Nov 26 21:21 ./
drwxr-xr-x 4 root root 0 Nov 26 21:21 ../
lrwxrwxrwx 1 root root 0 Nov 26 21:21 1-0:1.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-0:1.0/
lrwxrwxrwx 1 root root 0 Dec 15 23:10 1-1 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/
lrwxrwxrwx 1 root root 0 Dec 15 23:18 1-1:1.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/
lrwxrwxrwx 1 root root 0 Nov 26 21:21 2-0:1.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-0:1.0/
lrwxrwxrwx 1 root root 0 Nov 26 21:21 2-1 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-1/
lrwxrwxrwx 1 root root 0 Nov 26 21:21 2-1:1.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-1/2-1:1.0/
lrwxrwxrwx 1 root root 0 Nov 26 21:21 2-2 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-2/
lrwxrwxrwx 1 root root 0 Nov 26 21:21 2-2:1.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-2/2-2:1.0/
lrwxrwxrwx 1 root root 0 Nov 26 21:21 usb1 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/
lrwxrwxrwx 1 root root 0 Nov 26 21:21 usb2 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/
其中 usbx/第x个总线,x-y:a.b/的目录格式,x表示总线号,y表示端口,a表示配置,b表示接口。
具体解释可以参照如下:
The names that begin with "usb" refer to USB controllers. More accurately, they refer to the "root hub" associated with each controller. The number is the USB bus number. In the example there is only one controller, so its bus is number 1. Hence the name "usb1".
"1-0:1.0" is a special case. It refers to the root hub's interface. This acts just like the interface in an actual hub an almost every respect; see below.
All the other entries refer to genuine USB devices and their interfaces. The devices are named by a scheme like this:
bus-port.port.port ...
In other words, the name starts with the bus number followed by a '-'. Then comes the sequence of port numbers for each of the intermediate hubs along the path to the device.
For example, "1-1" is a device plugged into bus 1, port 1. It happens to be a hub, and "1-1.3" is the device plugged into port 3 of that hub. That device is another hub, and "1-1.3.1" is the device plugged into its port 1.
The interfaces are indicated by suffixes having this form:
:config.interface
That is, a ':' followed by the configuration number followed by '.' followed by the interface number. In the above example, each of the devices is using configuration 1 and this configuration has only a single interface, number 0. So the interfaces show up as;
1-1:1.0 1-1.3:1.0 1-1.3.1:1.0
A hub will never have more than a single interface; that's part of the USB spec. But other devices can and do have multiple interfaces (and sometimes multiple configurations). Each interface gets its own entry in sysfs and can have its own driver.
2、特定设备的详细信息
进入到某个目录中去,可以看到该设备的详细信息,可用cat命令获取信息。
// usb设备的详细信息
root@ubuntu:/sys/bus/usb/devices/usb1# ll
total 0
drwxr-xr-x 6 root root 0 Nov 26 21:21 ./
drwxr-xr-x 4 root root 0 Nov 26 21:21 ../
drwxr-xr-x 10 root root 0 Nov 26 21:21 1-0:1.0/
drwxr-xr-x 5 root root 0 Dec 15 23:10 1-1/
-rw-r--r-- 1 root root 4096 Dec 15 23:40 authorized
-rw-r--r-- 1 root root 4096 Dec 15 23:40 authorized_default
-rw-r--r-- 1 root root 4096 Dec 15 23:40 avoid_reset_quirk
-r--r--r-- 1 root root 4096 Nov 26 21:21 bcdDevice
-rw-r--r-- 1 root root 4096 Nov 26 21:21 bConfigurationValue
-r--r--r-- 1 root root 4096 Nov 26 21:21 bDeviceClass
-r--r--r-- 1 root root 4096 Nov 26 21:21 bDeviceProtocol
-r--r--r-- 1 root root 4096 Nov 26 21:21 bDeviceSubClass
-r--r--r-- 1 root root 4096 Dec 15 23:40 bmAttributes
-r--r--r-- 1 root root 4096 Dec 15 23:40 bMaxPacketSize0
-r--r--r-- 1 root root 4096 Dec 15 23:40 bMaxPower
-r--r--r-- 1 root root 4096 Dec 15 23:40 bNumConfigurations
-r--r--r-- 1 root root 4096 Dec 15 23:40 bNumInterfaces
-r--r--r-- 1 root root 4096 Nov 26 21:21 busnum
-r--r--r-- 1 root root 4096 Dec 15 23:40 configuration
-r--r--r-- 1 root root 65553 Nov 26 21:21 descriptors
-r--r--r-- 1 root root 4096 Dec 15 23:40 dev
-r--r--r-- 1 root root 4096 Nov 26 21:21 devnum
-r--r--r-- 1 root root 4096 Dec 15 23:40 devpath
lrwxrwxrwx 1 root root 0 Nov 27 20:06 driver -> ../../../../../bus/usb/drivers/usb/
drwxr-xr-x 3 root root 0 Dec 15 23:40 ep_00/
-r--r--r-- 1 root root 4096 Nov 26 21:21 idProct
-r--r--r-- 1 root root 4096 Nov 26 21:21 idVendor
-r--r--r-- 1 root root 4096 Dec 15 23:40 ltm_capable
-r--r--r-- 1 root root 4096 Nov 26 21:21 manufacturer
-r--r--r-- 1 root root 4096 Dec 15 23:40 maxchild
drwxr-xr-x 2 root root 0 Nov 26 21:21 power/
-r--r--r-- 1 root root 4096 Nov 26 21:21 proct
-r--r--r-- 1 root root 4096 Dec 15 23:40 quirks
-r--r--r-- 1 root root 4096 Nov 26 21:21 removable
--w------- 1 root root 4096 Dec 15 23:40 remove
-r--r--r-- 1 root root 4096 Nov 26 21:21 serial
-r--r--r-- 1 root root 4096 Nov 26 21:21 speed
lrwxrwxrwx 1 root root 0 Nov 26 21:21 subsystem -> ../../../../../bus/usb/
-rw-r--r-- 1 root root 4096 Nov 26 21:21 uevent
-r--r--r-- 1 root root 4096 Dec 15 23:40 urbnum
-r--r--r-- 1 root root 4096 Dec 15 23:40 version
二、使用debugfs
1、挂载 debugfs 到 /sys/kernel/debug 路径下
root@ubuntu:mount -t debugfs none /sys/kernel/debug
2、执行上述步骤之后,在 /sys/kernel/debug 就会生成如下的文件
root@ubuntu:/sys/bus/usb/devices# cd /sys/kernel/debug/
root@ubuntu:/sys/kernel/debug# ll
total 0
drwx------ 22 root root 0 Nov 26 21:21 ./
drwxr-xr-x 7 root root 0 Nov 26 21:21 ../
drwxr-xr-x 2 root root 0 Nov 26 21:21 acpi/
drwxr-xr-x 32 root root 0 Dec 4 16:30 bdi/
drwxr-xr-x 2 root root 0 Nov 26 21:21 bluetooth/
drwxr-xr-x 2 root root 0 Nov 26 21:21 cleancache/
drwxr-xr-x 2 root root 0 Nov 26 21:21 dma_buf/
drwxr-xr-x 4 root root 0 Nov 26 21:21 dri/
drwxr-xr-x 2 root root 0 Nov 26 21:21 dynamic_debug/
drwxr-xr-x 2 root root 0 Nov 26 21:21 extfrag/
drwxr-xr-x 2 root root 0 Nov 26 21:21 frontswap/
-r--r--r-- 1 root root 0 Nov 26 21:21 gpio
drwxr-xr-x 3 root root 0 Nov 26 21:21 hid/
drwxr-xr-x 2 root root 0 Nov 26 21:21 kprobes/
drwxr-xr-x 3 root root 0 Nov 26 21:21 kvm-guest/
drwxr-xr-x 2 root root 0 Nov 26 21:21 mce/
drwxr-xr-x 2 root root 0 Nov 26 21:21 pinctrl/
-r--r--r-- 1 root root 0 Nov 26 21:21 pwm
drwxr-xr-x 2 root root 0 Nov 26 21:21 regmap/
drwxr-xr-x 3 root root 0 Nov 26 21:21 regulator/
-rw-r--r-- 1 root root 0 Nov 26 21:21 sched_features
-r--r--r-- 1 root root 0 Nov 26 21:21 sleep_time
-r--r--r-- 1 root root 0 Nov 26 21:21 suspend_stats
drwxr-xr-x 7 root root 0 Nov 26 21:21 tracing/
drwxr-xr-x 3 root root 0 Nov 26 21:21 usb/
drwxr-xr-x 2 root root 0 Nov 26 21:21 virtio-ports/
-r--r--r-- 1 root root 0 Nov 26 21:21 vmmemctl
-r--r--r-- 1 root root 0 Nov 26 21:21 wakeup_sources
drwxr-xr-x 2 root root 0 Nov 26 21:21 x86/
3、cat 设备节点
执行下述命令之后会以特定格式打印目前USB总线上所有USB设备的信息如下:
root@ubuntu:/sys/kernel/debug# cat usb/devices
T: Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
B: Alloc= 17/900 us ( 2%), #Int= 1, #Iso= 0
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0001 Rev= 3.13
S: Manufacturer=Linux 3.13.0-32-generic uhci_hcd
S: Proct=UHCI Host Controller
S: SerialNumber=0000:02:00.0
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 2 Ivl=255ms
T: Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=12 MxCh= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=0e0f ProdID=0003 Rev= 1.03
S: Manufacturer=VMware
S: Proct=VMware Virtual USB Mouse
C:* #Ifs= 1 Cfg#= 1 Atr=c0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=02 Driver=usbhid
E: Ad=81(I) Atr=03(Int.) MxPS= 8 Ivl=1ms
T: Bus=02 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#= 3 Spd=12 MxCh= 7
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=0e0f ProdID=0002 Rev= 1.00
S: Proct=VMware Virtual USB Hub
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 1 Ivl=255ms
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 6
B: Alloc= 0/800 us ( 0%), #Int= 1, #Iso= 0
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev= 3.13
S: Manufacturer=Linux 3.13.0-32-generic ehci_hcd
S: Proct=EHCI Host Controller
S: SerialNumber=0000:02:03.0
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 4 Ivl=256ms
T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 7 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=ff(vend.) Sub=ff Prot=ff MxPS=64 #Cfgs= 1
P: Vendor=0bda ProdID=0129 Rev=39.60
S: Manufacturer=Generic
S: Proct=USB2.0-CRW
S: SerialNumber=20100201396000000
C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=500mA
I:* If#= 0 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=06 Prot=50 Driver=rts5139
E: Ad=01(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=82(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=83(I) Atr=03(Int.) MxPS= 3 Ivl=64ms
至于信息的详细解析可以参照 Linux源代码中 Documentation/usb/proc_usb_info.txt 文件。现摘录其中对该格式的详细解释:
| | |__Proct ID code
| |__Vendor ID code
|__Device info tag #2
String descriptor info:
S: Manufacturer=ssss
| |__Manufacturer of this device as read from the device.
| For USB host controller drivers (virtual root hubs) this may
| be omitted, or (for newer drivers) will identify the kernel
| version and the driver which provi
B. Linux驱动与设备节点简介 & Android内核与Linux内核的区别
驱动是内核的一部分,作为直接访问物理硬件的一个软件层,用于应用程序与物理硬件设备通信。内核包含多种驱动,如WIFI、USB、Audio、蓝牙、相机、显示驱动。
(1)设备驱动程序三类:字符设备驱动程序、块设备驱动程序、网络设备驱动程序;
(2)对应Linux三类设备:字符设备、块设备、网络设备;
(3)常见字符设备:鼠标、键盘、串口、控制台等;
(4)常见块设备:各种硬盘、flash磁盘、RAM磁盘等;
(5)网络设备(网络接口):eth0、eth1,注:网络设备没有设备节点,应用程序通过Socket访问网络设备。由于网络设备面向报文,较难实现相关read、write等文件读写函数,所以驱动的实现也与字符设备和块设备不同。
Linux使用对文件一样的管理方式来管理设备,所有设备都以文件的形式存放在/dev目录下,系统中的每个字符设备或者块设备都必须为其创建一个设备文件,它包含了该设备的设备类型(块设备或字符设滚桥备)、设备号(主设备号和次设备号)以及设备访问控制属性等。设备节点通过 mknod 命令创建,也可以由Udev用户工具软件在系统启动后根据/sys目录下每个设备的实际信息创建,使用后一种方式可以为每个设备动态分配设备号。
Linux中设备节点通过“mknod”命令创建,创建时需要指定主设备号和次设备号,即指定对应的驱动程序和对应的物理设备(访问设备节点时就相当于通过其设备号访问驱动程序进而间接访问到物理设备)。主设备号用来区分不同种类的设备,而次设备号用来区分同一类大举猛型的多个设备。对于常用设备,Linux有约定俗成的编号,如硬盘的主设备号是3
理解:应用程序通过访问设备节点读取主设备号和次设备号,通过主设答枯备号找对应的驱动,通过次设备号对应到具体物理设备。注:1个驱动对应一类设备,并用唯一主设备号标识。
Linux支持的各种设备的主设备号定义在include/linux/major.h文件中,已经在官方注册的主设备号和次设备号在Documentation/devices.txt文件中。
Android系统最底层是Linux,并且在中间加上了一个Dalvik / ART的Java虚拟机,从表面层看是Android运行库。每个Android应用都运行在自己的进程上,享有Dalvik / ART虚拟机为它分配的专有实例,并支持多个虚拟机在同一设备上高效运行,虚拟机执行的是专有格式的可执行文件(.dex) - 该格式经过优化,以将内存好用降到最低。
Android内核和Linux内核的差别主要体现在如下11个方面:
C. linux 一个驱动怎么对应多个节点的操作
Linux 中的设备有2种类型:字符设备(无缓冲且只能顺序存取)、块设备(有缓冲且可以随机存取)。每个字符设备和块设备都必须有主、次设备号,主设备号相同的设备是同类设备(使用同一个驱动程序)。这些设备中,有些设备是对实际存在的物理硬件的抽象,而有些设备则是内核自身提供的功能(不依赖于特定的物理硬件,又称为"虚拟设备")。每个设备在 /dev 目录下都有一个对应的文件(节点)。可以通过 cat /proc/devices 命令查看当前已经加载的设备驱动程序的主设备号。内核能够识别的所有设备都记录在原码树下的 Documentation/devices.txt 文件中。在 /dev 目录下除了字符设备和块设备节点之外还通常还会存在:FIFO管道、Socket、软/硬连接、目录。这些东西没有主/次设备号。
了解这些设备的最基本要求就是对 每个设备文件的含义了如指掌,下面就医列表的形式列出常见的设备文件以及相应的含义(比较偏僻的就省略了):
关注驱动人生,了解更多电脑相关资讯