天天看點

Linux插入華為無線網卡無法識别問題

CPU:Powerpc P1014

核心:版本較老,版本3.18.16。

插入後無法例化出網口,看dmesg列印

[  856.603371] usb 1-1.1: new high-speed USB device number 4 using fsl-ehci
[  856.696366] usb 1-1.1: New USB device found, idVendor=12d1, idProduct=1f01
[  856.703284] usb 1-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[  856.710642] usb 1-1.1: Product: HUAWEI_MOBILE
[  856.715034] usb 1-1.1: Manufacturer: HUAWEI_MOBILE
[  856.719845] usb 1-1.1: SerialNumber: 0123456789ABCDEF
[  856.726372] usb-storage 1-1.1:1.0: USB Mass Storage device detected
[  856.733792] scsi host1: usb-storage 1-1.1:1.0
[  857.741958] scsi 1:0:0:0: CD-ROM            HUAWEI   Mass Storage     2.31 PQ: 0 ANSI: 2
[  857.750745] scsi 1:0:0:0: Attached scsi generic sg1 type 5
           

發現系統檢測到了,卻把網卡當做了一個閃存裝置。當把網卡插入我的Ubuntu系統電腦可以識别成功。

[  467.837302] usb 3-2.1: new high-speed USB device number 8 using xhci_hcd
[  468.050958] usb 3-2.1: New USB device found, idVendor=12d1, idProduct=1f01
[  468.050962] usb 3-2.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[  468.050964] usb 3-2.1: Product: HUAWEI_MOBILE
[  468.050966] usb 3-2.1: Manufacturer: HUAWEI_MOBILE
[  468.050967] usb 3-2.1: SerialNumber: 0123456789ABCDEF
[  468.199562] usb-storage 3-2.1:1.0: USB Mass Storage device detected
[  468.201522] scsi host33: usb-storage 3-2.1:1.0
[  468.219579] usbcore: registered new interface driver usb-storage
[  468.281310] usbcore: registered new interface driver uas
[  469.219011] scsi 33:0:0:0: CD-ROM            HUAWEI   Mass Storage     2.31 PQ: 0 ANSI: 2
[  469.222339] sr 33:0:0:0: Power-on or device reset occurred
[  469.229746] sr 33:0:0:0: [sr1] scsi-1 drive
[  469.230103] sr 33:0:0:0: Attached scsi CD-ROM sr1
[  469.230365] sr 33:0:0:0: Attached scsi generic sg2 type 5
[  469.345674] usb 3-2.1: USB disconnect, device number 8
[  474.384538] usb 3-2.2: new high-speed USB device number 9 using xhci_hcd
[  474.595088] usb 3-2.2: New USB device found, idVendor=12d1, idProduct=14db
[  474.595090] usb 3-2.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[  474.595092] usb 3-2.2: Product: HUAWEI_MOBILE
[  474.595092] usb 3-2.2: Manufacturer: HUAWEI_MOBILE
[  474.643835] cdc_ether 3-2.2:1.0 eth0: register 'cdc_ether' at usb-0000:03:00.0-2.2, CDC Ethernet Device, 0c:5b:8f:27:9a:64
[  474.644024] usbcore: registered new interface driver cdc_ether
[  474.661899] cdc_ether 3-2.2:1.0 enx0c5b8f279a64: renamed from eth0
[  474.691638] IPv6: ADDRCONF(NETDEV_UP): enx0c5b8f279a64: link is not ready
           

發現電腦進行了兩次識别,第一次識别廠商号和裝置号為idVendor=12d1, idProduct=1f01。

第二次識别為idVendor=12d1, idProduct=14db,然後成功挂載。

而我們的linux3.18.16卡在執行完第一次識别就沒下文了。

通過網址(http://www.linux-usb.org/usb.ids)查閱了廠商号和裝置号,發現裝置号0x14db是網卡,0x1f01是這個網卡的大容量存儲模式。

Linux插入華為無線網卡無法識别問題

網上查閱資料後,終于明白這個網卡内部含有windows驅動,第一次插入的時候作為一個記憶體裝置,并提示你安裝驅動。

在安裝驅動之後,驅動會自動切換USB裝置的模式,儲存設備将會消失新的裝置将會産生(如USB類型的Modem)。這種特征被無線裝置的制造商稱其為“免CD”的裝置。

Linux切換usb模式用到一個工具叫USB_MODESWITCH,新的核心都內建了這個工具,是以能完成自動切換,舊的核心沒有,是以需要移植一個usb_modeswitch。

移植usb_modeswitch

需要移植三個東西

  1. mkdir USB-4G && cd USB-4G 建立檔案夾并進入
  2. 下載下傳libusb-1.0.21 ,并解壓 tar jvxf libusb-1.0.21.tar.bz2
  3. 下載下傳libusb-compat-0.1.5,并解壓tar jvxf libusb-compat-0.1.5.tar.bz2
  4. 下載下傳usb_modeswitch-2.5.1, 并解壓tar jvxf usb-modeswitch-2.5.1.tar.bz2

平台:powerpc

1.移植libusb-1.0.21

  1. cd libusb-1.0.21
  2. ./configure --host=ppc-linux CC=ppc_85xxDP-gcc --prefix=/home/truman/work/examples/usb-4g/install  --enable-static  --disable-udev --disable-shared
  3. make
  4. make install

2.移植libusb-compat-0.1.5

  1. cd ../libusb-compat-0.1.5/
  2. ./configure --host=ppc-linux CC=ppc_85xxDP-gcc --prefix=/home/truman/work/examples/usb-4g/install  --disable-shared --enable-static 
  3. PKG_CONFIG_PATH=/home/lingyun/fulinux/usbmode/install/lib/pkgconfig
  4. make
  5. make install

3.移植usb-modeswitch

CC          = ppc_85xxDP-gcc
CFLAGS      += -Wall -I /home/truman/work/examples/usb-4g/install/include/libusb-1.0 -static
LIBS        = -L /home/truman/work/examples/usb-4g/install/lib -l usb -l usb-1.0 -l pthread -lrt
           

不加lrt會報一個錯誤:

ppc_85xxDP-gcc -o usb_modeswitch usb_modeswitch.c -Wall -I /home/truman/work/examples/usb-4g/install/include/libusb-1.0 -static -L /home/truman/work/examples/usb-4g/install/lib -l usb -l usb-1.0 -l pthread
/home/truman/work/examples/usb-4g/install/lib/libusb-1.0.a(libusb_1_0_la-linux_usbfs.o): In function `op_clock_gettime':
/home/truman/work/examples/usb-4g/libusb-1.0.21/libusb/os/linux_usbfs.c:2670: undefined reference to `clock_gettime'
/home/truman/work/examples/usb-4g/libusb-1.0.21/libusb/os/linux_usbfs.c:2672: undefined reference to `clock_gettime'
/home/truman/work/examples/usb-4g/install/lib/libusb-1.0.a(libusb_1_0_la-linux_usbfs.o): In function `find_monotonic_clock':
/home/truman/work/examples/usb-4g/libusb-1.0.21/libusb/os/linux_usbfs.c:336: undefined reference to `clock_gettime'
collect2: ld returned 1 exit status
make: *** [usb_modeswitch] Error 1
           

編譯成功後,将usb_modeswitch放到闆卡上就可以運作了,該應用有很多用法,可以-h檢視,這裡用的是./usb_modeswitch -c usb_modeswitch.conf 。

usb_modeswitch.conf 是一個配置檔案,裡面的内容是

DefaultVendor=  0x12d1
DefaultProduct= 0x1f01
TargetVendor=   0x12d1
TargetProductList="14db,14dc"
HuaweiNewMode=1
           

 DefaultVendor和DefaultProduct是需要切換的廠商号和裝置号,TargetVendor和TargetProductList是要切換到的目的廠商号和裝置号。

執行結果如下,成功切換并識别網卡。

-sh-3.2# ./usb_modeswitch -c usb_modeswitch.conf 
Look for target devices ...
 No devices in target mode or class found
Look for default devices ...
 Found devices in default mode (1)
Access device 003 on bus 001
Get the current device configuration ...
Current configuration number is 1
Use interface number 0
 with class 8
Use endpoints 0x01 (out) and 0x81 (in)
Using standard Huawei switching message
Looking for active drivers ...
 OK, driver detached
Set up interface 0
Use endpoint 0x01 for message sending ...
Trying to send message 1 to endpoint 0x01 ...
 OK, message successfully sent
Read the response to message 1 (CSW) ...
 Response successfully read (13 bytes), status 0
Reset response endpoint 0x81
Reset message endpoint 0x01
 Could not reset endpoint (probably harmless): -99
-> Run lsusb to note any changes. Bye!

-sh-3.2# [   75.379658] usb 1-1.1: USB disconnect, device number 3
[   76.313396] usb 1-1.1: new high-speed USB device number 5 using fsl-ehci
[   76.406889] usb 1-1.1: New USB device found, idVendor=12d1, idProduct=14db
[   76.413806] usb 1-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[   76.421162] usb 1-1.1: Product: HUAWEI_MOBILE
[   76.425541] usb 1-1.1: Manufacturer: HUAWEI_MOBILE
[   76.435701] cdc_ether 1-1.1:1.0 eth2: register 'cdc_ether' at usb-fsl-ehci.0-1.1, CDC Ethernet Device, 0c:5b:8f:27:9a:64
           

現在出現兩個問題:

1.每次插上網卡都需要手動敲一下切換指令,網卡才可以成功加載。

2.網卡會給我的闆卡dhcp自動配置設定一個IP,才可以上網,但我的Linux系統裁剪過,沒有dhcp功能,需要把這個功能加上才可以上網。

接下來解決這兩個問題。

移植udev

  1. 下載下傳UDEV源碼udev-080.tar.bz2。
  2. 修改Makefile:

    CROSS = ppc_85xxDP-

  3. 修改源碼

    編輯udevmonitor.c檔案

    第53行

    addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path +1) + 1;

    修改為:

    addrlen = __builtin_offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;

    然後儲存退出。

  4. make
  5. 生成了udev udevd udevstart udevinfo udevtest  udevcontrol udevmonitor udevsend 指令,我隻用到了udevd udevstart udevmonitor 這三個指令,将這個三個指令複制到闆子/bin目錄下。
  6. 修改/etc/init.d/rcS,添加如下内容
    /bin/mount -t sysfs sysfs /sys 
    /bin/mount -t tmpfs tmpfs /dev
    /bin/udevd --daemon
    /bin/udevstart
               
  7. 在/etc目錄下建立目錄udev,将udev-080源碼中的etc/udev.conf檔案拷貝到闆卡的/etc/udev下
  8. 在闆卡/etc/udev目錄下建立rules.d檔案夾,建立一個規則檔案usb_modeswitch.rules,内容如下
    ACTION=="add",KERNEL=="sg[0-9]",ATTRS{idProduct}=="1f01",ATTRS{idVendor}=="12d1",RUN+="/sbin/usb_modeswitch -c /etc/usb_modeswitch.conf"
               
  9. 将上文所說的usb_modeswitc放在/sbin目錄下,usb_modeswitc.conf放在/etc目錄下。

完成移植,當插入一個叫sg*的裝置并且裝置号和廠商号與規則一緻時,就調用/sbin/usb_modeswitch -c /etc/usb_modeswitch.conf這條指令。

此外對核心還要進行一些設定,來支援udev

CONFIG_DEVTMPFS=y

CONFIG_HOTPLUG=y

CONFIG_INOTIFY_USER=y

CONFIG_NET=y

CONFIG_PROC_FS=y

CONFIG_SIGNALFD=y

CONFIG_SYSFS=y

CONFIG_SYSFS_DEPRECATED*=n

CONFIG_UEVENT_HELPER_PATH=""

繼續閱讀