imx6ulevk linux 连接4G usb上网卡拨号上网

武, 军/ 九月 8, 2017/ Linux

准备材料:imx6ulevk,5V电源适配器,micro USB线缆,4G usb上网卡

我用的是E3276s-920,闲鱼50元包邮。
这种USB上网卡,如果是给人用的,马上会被淘汰,因为现在的智能机都有开wifi热点的功能,不需要再多此一举。
如果是给物用做物联网,应该还可以有很大市场,
这种上网卡实际上类似一台小手机。所以价格并不便宜。制造成本本身就很高。
还有那种mini pcie接口/USB接口的4G模块,都是类似的产品。

下面就一步步进行。

1.更新内核
(1)使能内核编译选项
wujun@wj-vBox:~/freescale/linux-imx$ source ~/freescale/sdk/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
wujun@wj-vBox:~/freescale/linux-imx$ make menuconfig

(2)编译内核
wujun@wj-vBox:~/freescale/linux-imx$ make -j8

(3)下载内核
我是使用uboot,替换掉内核而已,文件系统还是用micro sd卡。
PC开tftp共享内核镜像zImage存放路径(别忘了设备树)。
uboot环境下:
=> setenv ipaddr 192.168.56.6;setenv serverip 192.168.56.5;
=> mmc dev 1;setenv bootargs console=ttymxc0,115200 root=/dev/mmcblk1p2 rootwait rw;
=> tftp 0x81000000 zImage;tftp 0x81A00000 imx6ul.dtb.bin;
(4)启动内核
=> bootz 0x81000000 – 0x81A00000;

2.模块接入USB后查看log和设备ID。
root@imx6ulevk:~# dmesg | tail -20
[ 117.780190] usb 2-1: new high-speed USB device number 2 using ci_hdrc
[ 117.991226] usb-storage 2-1:1.0: USB Mass Storage device detected
[ 117.999583] scsi host0: usb-storage 2-1:1.0
[ 118.011326] usb-storage 2-1:1.1: USB Mass Storage device detected
[ 118.017339] scsi host1: usb-storage 2-1:1.1
[ 119.002172] scsi 0:0:0:0: CD-ROM HUAWEI Mass Storage 2.31 PQ: 0 ANSI: 2
[ 119.028194] scsi 1:0:0:0: Direct-Access HUAWEI TF CARD Storage 2.31 PQ: 0 ANSI: 2
[ 119.052891] sd 1:0:0:0: [sda] Attached SCSI removable disk
root@imx6ulevk:~# lsusb
Bus 001 Device 001: ID 1d6b:0002
Bus 002 Device 002: ID 12d1:14fe
下载usb-modeswitch-data 根据设备ID找到相应配置文件。
http://www.draisberghof.de/usb_modeswitch/

wujun@wj-vBox:~/usb-modeswitch-data-20170806/usb_modeswitch.d$ cat 12d1_14fe
# Huawei E352 (T-Mobile NL), E173s (Variant)
TargetVendor=0x12d1
TargetProductList=”1506,150f,151d,1c1e”
HuaweiNewMode=1

看上去并不符合我选用的上网卡。
从另外一个网页上可以找到相应的设备参数,先用这个参数试试吧。使用usb modeswitch命令后,设备id应该变成0x1506。
http://www.draisberghof.de/usb_modeswitch/device_reference.txt
#######################################################
# Huawei E535
#
# Contributor: Andres Septer

DefaultVendor= 0x12d1
DefaultProduct=0x14fe

TargetVendor= 0x12d1
TargetProduct= 0x1506

MessageContent=”55534243123456780000000000000011062000000100000000000000000000″

########################################################
# T-Mobile NL (Huawei E352)
#
# Contributor: Alex Hermann

DefaultVendor= 0x12d1
DefaultProduct=0x14fe

TargetVendor= 0x12d1
TargetProduct= 0x1506

MessageContent=”55534243123456780000000000000011062000000100000000000000000000″

3.编译安装usb-modeswitch
(1)安装gcc
下载:gcc-7.1.1-2-armv7h.pkg.tar.xz
传到设备:tftp -g -r gcc-7.1.1-2-armv7h.pkg.tar.xz 192.168.56.5
解压:gtar -xJvf gcc-7.1.1-2-armv7h.pkg.tar.xz -C /
测试:gcc –version
gcc (GCC) 7.1.1 20170516
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
(2)安装usb mode switch
下载:usb-modeswitch-2.5.1.tar.bz2
上传到设备:ftp -g -r usb-modeswitch-2.5.1.tar.bz2 192.168.56.5
解压:tar -jxf usb-modeswitch-2.5.1.tar.bz2
编译安装:make install

错误1:/bin/sh: pkg-config: command not found
下载:pkg-config-0.29.2-1-armv7h.pkg.tar.xz
上传:tftp -g -r pkg-config-0.29.2-1-armv7h.pkg.tar.xz 192.168.56.5
解压安装:tar -Jxf pkg-config-0.29.2-1-armv7h.pkg.tar.xz -C /

错误2:libmpc.so.3: cannot open shared object file: No such file or directory
下载:libmpc-1.0.3-2-armv7h.pkg.tar.xz
上传:tftp -g -r libmpc-1.0.3-2-armv7h.pkg.tar.xz 192.168.56.5
解压安装:tar -Jxf libmpc-1.0.3-2-armv7h.pkg.tar.xz -C /

错误3:libmpfr.so.4: cannot open shared object file: No such file or directory
下载:mpfr-3.1.5.p2-1-armv7h.pkg.tar.xz
上传:tftp -g -r mpfr-3.1.5.p2-1-armv7h.pkg.tar.xz 192.168.56.5
解压安装:tar -Jxf mpfr-3.1.5.p2-1-armv7h.pkg.tar.xz -C /

root@imx6ulevk:~/usb-modeswitch-2.5.1# make install
sed ‘s_!/usr/bin/tclsh_!'”/usr/bin/tclsh”‘_’ < usb_modeswitch.tcl > usb_modeswitch_dispatcher
install -D –mode=755 usb_modeswitch /usr/sbin/usb_modeswitch
install -D –mode=755 usb_modeswitch.sh /lib/udev/usb_modeswitch
install -D –mode=644 usb_modeswitch.conf /etc/usb_modeswitch.conf
install -D –mode=644 usb_modeswitch.1 /usr/share/man/man1/usb_modeswitch.1
install -D –mode=644 usb_modeswitch_dispatcher.1 /usr/share/man/man1/usb_modeswitch_dispatcher.1
install -D –mode=755 usb_modeswitch_dispatcher /usr/sbin/usb_modeswitch_dispatcher
install -d /var/lib/usb_modeswitch
test -d /etc/init -a -e /sbin/initctl && install –mode=644 usb-modeswitch-upstart.conf /etc/init || test 1
test -d /etc/systemd/system -a \( -e /usr/bin/systemctl -o -e /bin/systemctl \) && install –mode=644 usb_modeswitch@.service /etc/systemd/system || test 1

4.安装libusb
我这个文件系统里已经安装
root@imx6ulevk: ls /lib | grep usb
libusb-1.0.so
如果没有的话,也可以直接下载安装(arm A7版本)不需要自己编译。

5.切换USB模式
我用的是命令切换的,有的同学用的是配置文件。感兴趣的同学可以试试。
root@imx6ulevk:~# usb_modeswitch -v 12d1 -p 14fe -M ‘55534243123456780000000000000011062000000100000000000000000000’
Look for default devices …
Found devices in default mode (1)
Access device 002 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)
Looking for active drivers …
OK, driver detached
OK, driver detached
Set up interface 0
Use endpoint 0x01 for message sending …
Trying to send message 1 to endusb 1-1: USB disconnect, device number 2
point 0x01 …
OK, message successfully sent
Read the response to message 1 (CSW) …
Device seems to have vanished after reading. Good.
Device is gone, skip any further commands
-> Run lsusb to note any changes. Bye!

root@imx6ulevk:~#
usb 1-1: new high-speed USB device number 3 using ci_hdrc
option 1-1:1.0: GSM modem (1-port) converter detected
usb 1-1: GSM modem (1-port) converter now attached to ttyUSB0
option 1-1:1.1: GSM modem (1-port) converter detected
usb 1-1: GSM modem (1-port) converter now attached to ttyUSB1
usb-storage 1-1:1.3: USB Mass Storage device detected
scsi host2: usb-storage 1-1:1.3
usb-storage 1-1:1.4: USB Mass Storage device detected
scsi host3: usb-storage 1-1:1.4
scsi 2:0:0:0: CD-ROM HUAWEI Mass Storage 2.31 PQ: 0 ANSI: 2
scsi 3:0:0:0: Direct-Access HUAWEI TF CARD Storage 2.31 PQ: 0 ANSI: 2
sd 3:0:0:0: [sda] Attached SCSI removable disk

可以发现设备id已经变成1506了,而且出现了两个ttyUSB设备,输入AT指令有返回。
root@imx6ulevk:~# lsusb
Bus 001 Device 003: ID 12d1:1506
Bus 001 Device 001: ID 1d6b:0002

root@imx6ulevk:~# ls -l /dev/ttyUSB*
crw-rw—- 1 root dialout 188, 0 Sep 6 16:30 /dev/ttyUSB0
crw-rw—- 1 root dialout 188, 1 Sep 6 16:30 /dev/ttyUSB1

root@imx6ulevk:~# minicom -D/dev/ttyUSB0
Welcome to minicom 2.7

OPTIONS: I18n
Compiled on Jun 19 2017, 19:16:21.
Port /dev/ttyUSB0, 16:33:56

Press CTRL-A Z for help on special keys

AT
OK
6.编写ppp连接配置文件td-lte, tdlte-chat-connect和tdlte-chat-disconnect
下面是抄的了,具体含义还没分析。网上搜一搜有很多类似脚本。

root@imx6ulevk:/etc/ppp/peers# vi td-lte
#—————–
nodetach
lock
/dev/ttyUSB0
115200
crtscts
modem
#noauth
debug
#hide-password
usepeerdns
noipdefault
defaultroute
user “cmnet”
0.0.0.0:0.0.0.0
#ipcp-accept-local
#ipcp-accept-remote
#lcp-echo-failure 12
#lcp-echo-interval 3
#noccp
#novj
#novjccomp
#persist
connect ‘/usr/sbin/chat -s -v -f /etc/ppp/peers/tdlte-chat-connect’
disconnect ‘/usr/sbin/chat -s -v -f /etc/ppp/peers/tdlte-chat-disconnect’
#———————-

root@imx6ulevk:/etc/ppp# vi tdlte-chat-connect
#Chat script for China Mobile, used HUAWEI E3276s-920 TD module.
TIMEOUT 15
ABORT “DELAYED”
ABORT “BUSY”
ABORT “ERROR”
ABORT “NO DIALTONE”
ABORT “NO CARRIER”
TIMEOUT 40

” \rAT
OK ATS0=0
OK ATE0V1
OK AT+CGDCONT=1,”IP”,”CMNET”
OK AT+CGEQREQ=1,2,128,384,,,0,,,,,,
OK ATDT*99*1#
CONNECT
#————————–
root@imx6ulevk:/etc/ppp# vi tdlte-chat-disconnect
#———————–
ABORT “ERROR”
ABORT “NO DIALTONE”
SAY “\NSending break to the modem\n”
“”\k”
“”+++ATH”
SAY “\nGood bay\n”
#—————————
7.连接网络
root@imx6ulevk:/etc/ppp/peers# pppd call td-lte

timeout set to 15 seconds
abort on (DELAYED)
abort on (BUSY)
abort on (ERROR)
abort on (NO DIALTONE)
abort on (NO CARRIER)
timeout set to 40 seconds
send (^MAT^M)
expect (OK)
^MAT^M^M
OK
— got it

send (ATS0=0^M)
expect (OK)
^M
ATS0=0^M^M
OK
— got it

send (ATE0V1^M)
expect (OK)
^M
ATE0V1^M^M
OK
— got it

send (AT+CGDCONT=1,”IP”,”CMNET”^M)
expect (OK)
^M
^M
OK
— got it

send (AT+CGEQREQ=1,2,128,384,,,0,,,,,,^M)
expect (OK)
^M
^M
OK
— got it

send (ATDT*99*1#^M)
expect (CONNECT)
^M
^M
CONNECT
— got it

Script /usr/sbin/chat -s -v -f /etc/ppp/peers/tdlte-chat-connect finished (pid 1311), status = 0x0
Serial connection established.
using channel 1
Using interface ppp0
Connect: ppp0 <–> /dev/ttyUSB1
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0x4bc41d72> <pcomp> <accomp>]
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0x4bc41d72> <pcomp> <accomp>]
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0x4bc41d72> <pcomp> <accomp>]
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0x4bc41d72> <pcomp> <accomp>]
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0x4bc41d72> <pcomp> <accomp>]
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0x4bc41d72> <pcomp> <accomp>]
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0x4bc41d72> <pcomp> <accomp>]
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0x4bc41d72> <pcomp> <accomp>]
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0x4bc41d72> <pcomp> <accomp>]
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0x4bc41d72> <pcomp> <accomp>]

可以用ifconfig看网络接口多出来一个ppp0.
root@imx6ulevk:/etc/ppp/peers# ifconfig
ppp0 Link encap:Point-to-Point Protocol
inet addr:10.19.138.253 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:33 errors:0 dropped:0 overruns:0 frame:0
TX packets:34 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:2881 (2.8 KiB) TX bytes:2215 (2.1 KiB)

8.测试网络
ping一下baidu

root@imx6ulevk:/etc/ppp/peers# ping www.baidu.com
PING www.a.shifen.com (111.13.12.139) 56(84) bytes of data.
64 bytes from localhost (111.13.12.139): icmp_seq=1 ttl=51 time=27.6 ms
64 bytes from localhost (111.13.12.139): icmp_seq=2 ttl=51 time=36.4 ms
64 bytes from localhost (111.13.12.139): icmp_seq=3 ttl=51 time=45.1 ms
64 bytes from localhost (111.13.12.139): icmp_seq=4 ttl=51 time=43.2 ms
64 bytes from localhost (111.13.12.139): icmp_seq=5 ttl=51 time=41.1 ms
64 bytes from localhost (111.13.12.139): icmp_seq=6 ttl=51 time=39.2 ms
64 bytes from localhost (111.13.12.139): icmp_seq=7 ttl=51 time=37.2 ms

这种方式是将USB上网卡模拟成一个高速串口的形式。
还有一种是直接模拟成网卡设备。以后有时间可以试试。很多4G模块是支持多种模式的。