嵌入式系统中,我们会经常使用到usb设备,不仅仅是用于usb用作U盘,还可以将usb转换成其他类型的设备来使用,可以这么说,usb是嵌入式系统中不可或缺的一部分.
今天,我吧我最近整理的用于测试usb的脚本贡献出来.
加载信息检测
lsusb
root@fl-imx6ull:~# lsusb
Bus 001 Device 004: ID 0bda:d723
Bus 001 Device 003: ID 14cd:1212
Bus 001 Device 002: ID 0424:2514
Bus 001 Device 001: ID 1d6b:0002
bus 总线号,device 设备序号,id,设备id,后面可能还有生产厂商信息
find /dev/bus
列出bus信息
/dev/bus
/dev/bus/usb
/dev/bus/usb/001
/dev/bus/usb/001/006
/dev/bus/usb/001/004
/dev/bus/usb/001/002
/dev/bus/usb/001/001
lsusb –v 列出详细信息
可增加参数-D ,列出特定设备的信息.如
lsusb –D /dev/bus/usb/001/006
Device: ID 14cd:1212
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
idVendor 0x14cd
idProduct 0x1212
bcdDevice 1.00
iManufacturer 1 Generic
iProduct 3 Mass Storage Device
iSerial 2 121220130416
……
Device Qualifier (for other device speed):
bLength 10
bDescriptorType 6
bcdUSB 2.00
bDeviceClass 0
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
bNumConfigurations 1
can't get debug descriptor: Resource temporarily unavailable
Device Status: 0xff00
(Bus Powered)
lsusb –t
以树层级结构输出USB设备(这个不支持设备过滤)
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ci_hdrc/1p, 480M
|__ Port 1: Dev 2, If 0, Class=, Driver=hub/4p, 480M
|__ Port 1: Dev 6, If 0, Class=, Driver=usb-storage, 480M
|__ Port 4: Dev 4, If 0, Class=, Driver=, 480M
|__ Port 4: Dev 4, If 1, Class=, Driver=, 480M
|__ Port 4: Dev 4, If 2, Class=, Driver=rtl8723du, 480M
cat devices文件
cat /sys/kernel/debug/usb/devices 可查看所有usb设备信息
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: Product=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
具体细节分析,可参考
[USB-详解/sys/kernel/debug/usb/devices]
可以看到上面的是所有信息放到了一个文件中,如果要得到其中一部分详细的信息,需要使用精准的算法来过滤出来,实际上还有一个办法,查看/sys/bus/usb/devices在这个目录下,会有多个目录,每个目录代表一个设备,如下:
root@fl-imx6ull:/sys/bus/usb/devices/1-1.1# ls
1-1.1:1.0 busnum port
authorized configuration power
avoid_reset_quirk descriptors product
bConfigurationValue dev quirks
bDeviceClass devnum removable
bDeviceProtocol devpath remove
bDeviceSubClass driver serial
bMaxPacketSize0 ep_00 speed
bMaxPower idProduct subsystem
bNumConfigurations idVendor uevent
bNumInterfaces ltm_capable urbnum
bcdDevice manufacturer version
bmAttributes maxchild
这些都是文件,和上面的那些参数是一一对应的.
对应关系分析:
1)不插入U盘
root@fl-imx6ull:/sys/bus/usb/devices# ls
1-0:1.0 1-1.4 1-1.4:1.1 1-1:1.0
1-1 1-1.4:1.0 1-1.4:1.2 usb1
root@fl-imx6ull:/sys/bus/usb/devices# ls -l
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-0:1.0 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-0:1.0
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1.4 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.4
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1.4:1.0 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.4/1-1.4:1.0
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1.4:1.1 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.4/1-1.4:1.1
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1.4:1.2 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.4/1-1.4:1.2
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1:1.0 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1:1.0
lrwxrwxrwx 1 root root 0 Jan 1 1970 usb1 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1
2)插入U盘
root@fl-imx6ull:/sys/bus/usb/devices# ls
1-0:1.0 1-1.1 1-1.4 1-1.4:1.1 1-1:1.0
1-1 1-1.1:1.0 1-1.4:1.0 1-1.4:1.2 usb1
root@fl-imx6ull:/sys/bus/usb/devices# ls -l
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-0:1.0 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-0:1.0
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1
lrwxrwxrwx 1 root root 0 Mar 26 04:15 1-1.1 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.1
lrwxrwxrwx 1 root root 0 Mar 26 04:15 1-1.1:1.0 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.1/1-1.1:1.0
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1.4 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.4
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1.4:1.0 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.4/1-1.4:1.0
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1.4:1.1 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.4/1-1.4:1.1
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1.4:1.2 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.4/1-1.4:1.2
lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1:1.0 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1:1.0
lrwxrwxrwx 1 root root 0 Jan 1 1970 usb1 -> ../../../devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1
通过对比我们看到多了一个目录1-1.1 ,这个就是我们新增加的U盘的目录.进入1-1.1,
root@fl-imx6ull:/sys/bus/usb/devices# cd 1-1.1
root@fl-imx6ull:/sys/bus/usb/devices/1-1.1# ls
1-1.1:1.0 busnum port
authorized configuration power
avoid_reset_quirk descriptors product
bConfigurationValue dev quirks
bDeviceClass devnum removable
bDeviceProtocol devpath remove
bDeviceSubClass driver serial
bMaxPacketSize0 ep_00 speed
bMaxPower idProduct subsystem
bNumConfigurations idVendor uevent
bNumInterfaces ltm_capable urbnum
bcdDevice manufacturer version
bmAttributes maxchild
我们来查看其中几个重要的参数
speed 速率 cat speed 得到480
descriptors 设备描述符
version 版本 2.00
product 产品id信息 Mass Storage Device
idVendor 厂商id 14cd
idProduct 产品id 1212
serial 串号 121220130416
manufacturer 生产厂商描述性信息 Generic
bMaxPower 最大功耗(以2ma为单位) 250mA
下面我们通过另外一种方式来查看upan的属性
①.不插入U盘
cat /sys/kernel/debug/usb/devices >~/1.log
②插入U盘
cat /sys/kernel/debug/usb/devices >~/2.log
diff 1.log 2.log
我们可以看到多出来的设备信息
22c22,33
< T: Bus=01 Lev=02 Prnt=02 Port=03 Cnt=01 Dev#= 4 Spd=480 MxCh= 0
---
> T: Bus=01 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#= 9 Spd=480 MxCh= 0
> D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
> P: Vendor=14cd ProdID=1212 Rev= 1.00
> S: Manufacturer=Generic
> S: Product=Mass Storage Device
> S: SerialNumber=121220130416
> C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=250mA
> I:* If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
> E: Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
> E: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
>
> T: Bus=01 Lev=02 Prnt=02 Port=03 Cnt=02 Dev#= 4 Spd=480 MxCh= 0
从图中可以看到我们要得到信息
cat mount查看分区信息
root@fl-imx6ull:/# cat /proc/mounts
/dev/root / ext3 rw,relatime,errors=continue,user_xattr,acl,barrier=1,data=ordered 0 0
devtmpfs /dev devtmpfs rw,relatime,size=80656k,nr_inodes=20164,mode=755 0 0
proc /proc proc rw,relatime 0 0
tmpfs /mnt/.psplash tmpfs rw,relatime,size=40k 0 0
sysfs /sys sysfs rw,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
tmpfs /run tmpfs rw,nosuid,nodev,mode=755 0 0
tmpfs /var/volatile tmpfs rw,relatime 0 0
/dev/mmcblk1p1 /run/media/mmcblk1p1 vfat rw,relatime,gid=6,fmask=0007,dmask=0007,allow_utime=0020,codepage=437,iocharset=utf8,shortname=mixed,errors=remount-ro 0 0
devpts /dev/pts devpts rw,relatime,gid=5,mode=620 0 0
/dev/sda1 /run/media/sda1 vfat rw,relatime,gid=6,fmask=0007,dmask=0007,allow_utime=0020,codepage=437,iocharset=utf8,shortname=mixed,errors=remount-ro 0 0
root@fl-imx6ull:/# cat /proc/mounts | grep media | grep sd | awk '{print $2}'
/run/media/sda1
经过认真考虑,比对,决定采用如下方法检测U盘信息,顺便可作为自动化脚本的自动执行的方法来使用.
cat usb_auto.sh
#!/bin/bash
red="\e[0;31;1m"
blue="\e[0;34;1m"
yellow="\e[0;33;1m"
green="\e[0;32;1m"
white="\e[0;37;1m"
purple="\e[0;35;1m"
restore="\e[0m"
index=1
while true
do
echo -e $yellow"--------------------test $index start---------------------------"$restore
media=`cat /proc/mounts | grep media | grep sd | tail -n 1| awk '{print $2}' `
if [ -z $media ]
then
echo -e $red"usb device is not found"$restore
else
if [ -d $media ]
then
./usb_run.sh $media $index
./usb_speed.sh $media $index
else
echo -e $red"usb device is not find"$restore
fi
fi
sleep 13s
index=$((${index}+1))
echo ""
done
稳定性检测
我采用对usb设备进行文件读写操作来完成测试和验证
usb_run.sh
#!/bin/bash
red="\e[0;31;1m"
blue="\e[0;34;1m"
yellow="\e[0;33;1m"
green="\e[0;32;1m"
white="\e[0;37;1m"
purple="\e[0;35;1m"
restore="\e[0m"
media=$1
index=$2
#echo ${media}/usbfile${index}
if [ -e ${media}/usbfile${index} ]
then
rm ${media}/usbfile${index}
fi
# 写入U盘文件特定文字
echo "test write www.forlinx.com" > $media/usbfile${index}
sync
sleep 1s
# 读取刚才的文字,判断是否正确
str=`cat ${media}/usbfile${index}`
if [ "$str" = "test write www.forlinx.com" ]
then
echo -e $green"`date +%T` Test usb ${index} success "$restore | tee -a ./result.log
else
echo -e $green"`date +%T` Test usb ${index} failure "$restore | tee -a ./result.log
fi
速度检测
根据数据判定是那种类型的usb,根据标准可知,USB2.0 High Speed:理论速度是480Mbps,对应之前的USB2.0;USB2.0 Full Speed:理论速度是12Mbps,也就是过去的USB1.1;
也就是说高速的为480/6=80M/s 全速的为12/8=1.5M/s
usb_speed.sh
#!/bin/sh
red="\e[0;31;1m"
blue="\e[0;34;1m"
yellow="\e[0;33;1m"
green="\e[0;32;1m"
white="\e[0;37;1m"
purple="\e[0;35;1m"
restore="\e[0m"
args=2
if [ $# -ne ${args} ]
then
echo "Usage: ./testusb_speed.sh usb_dir index"
exit 1
fi
rm u.log
sh -c "sync > /proc/sys/vm/drop_caches" #sh -c "sync && echo 3 > /proc/sys/vm/drop_caches"
dd if=/dev/zero of=${1}/test.bin bs=16k count=1000 conv=fsync oflag=direct 2>>u.log
sleep 3s
sh -c "sync > /proc/sys/vm/drop_caches"
dd if=${1}/test.bin of=/dev/null bs=16k count=1000 conv=sync iflag=direct 2>>u.log
sleep 3s
speed=`cat u.log | grep "No such file or directory" | wc -l`
#echo "speed=$speed"
if ((${speed}>0))
then
echo –e $red"usb device is not found"$restore
else
speed=`cat u.log | grep bytes | head -n 1 | awk '{print $10}'`
echo "write speed is ${speed}M"
speed=`echo $speed | awk -F '.' '{print $1}'`
if ((${speed}>2))
then
echo -e $green"Write this usb is High speed mode "$restore
else
echo -e $purple"Write this usb is Full speed mode "$restore
fi
speed=`cat u.log | grep bytes | tail -n 1 | awk '{print $10}'`
echo "read speed is ${speed}M"
speed=`echo $speed | awk -F '.' '{print $1}'`
if ((${speed}>2))
then
echo -e $green"read this usb is High speed mode "$restore
else
echo -e $purple"read this usb is Full speed mode "$restore
fi
fi
容量适应性检测
对于不同容量的适应性检测,现在还没有找到好的方法,只能通过人工手动实现.这里不在说明,以后是否有可能自动实现,以后再说.
编写不易,关注点赞在走!!!