《OpenWrt开发笔记》第31章 OpenWrt 4G拨号上网(Ubuntu12.04+Openwrt15.05)

开发环境:
开发板:RT5350
4G 模块:QUECTEL EC25
OpenWrt 源码版本:Chaos_Calmer
参考文档:Quectel_WCDMA<E_Linux_USB_Driver_User_Guide_V1.7.pdf

31.1 USB Serial驱动

首先修改内核,内核文件在chaos_calmer/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_rt305x/linux-3.18.109目录下,需要事先编译过系统。当模块连接到USB串行驱动时,驱动程序将在目录/dev中创建设备文件,
ttyUSB0/ttyUSB1/ttyUSB2…
接下来就是讲解如何移植USB Serial。

31.1.1增加PID&VID

要想识别模块,客户应该在下面添加模块维和PID信息
File: [KERNEL]/drivers/usb/serial/option.c

static const struct usb_device_id option_ids[] = {
#if 1 //Added by Quectel
{ USB_DEVICE(0x05C6, 0x9090) }, /* Quectel UC15 */
{ USB_DEVICE(0x05C6, 0x9003) }, /* Quectel UC20 */
{ USB_DEVICE(0x2C7C, 0x0125) }, /* Quectel EC25 */
{ USB_DEVICE(0x2C7C, 0x0121) }, /* Quectel EC21 */
{ USB_DEVICE(0x05C6, 0x9215) }, /* Quectel EC20 */
{ USB_DEVICE(0x2C7C, 0x0191) }, /* Quectel EG91 */
{ USB_DEVICE(0x2C7C, 0x0195) }, /* Quectel EG95 */
{ USB_DEVICE(0x2C7C, 0x0306) }, /* Quectel EG06/EP06/EM06 */
{ USB_DEVICE(0x2C7C, 0x0296) }, /* Quectel BG96 */
{ USB_DEVICE(0x2C7C, 0x0435) }, /* Quectel AG35 */
#endif

31.1.2添加零包处理

根据USB协议的要求,客户需要添加处理零数据包的机制。
For Linux Kernel Version newer than 2.6.34:
File: [KERNEL]/drivers/usb/serial/usb_wwan.c

static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
int dir, void *ctx, char *buf, int len,void (*callback) (struct urb *))
{
……
usb_fill_bulk_urb(urb, serial->dev,
usb_sndbulkpipe(serial->dev, endpoint) | dir,
buf, len, callback, ctx);
#if 1 //Added by Quectel for zero packet
if (dir == USB_DIR_OUT) {
struct usb_device_descriptor *desc = &serial->dev->descriptor;
if (desc->idVendor == cpu_to_le16(0x05C6) && desc->idProduct == cpu_to_le16(0x9090))
urb->transfer_flags |= URB_ZERO_PACKET;
if (desc->idVendor == cpu_to_le16(0x05C6) && desc->idProduct == cpu_to_le16(0x9003))
urb->transfer_flags |= URB_ZERO_PACKET;
if (desc->idVendor == cpu_to_le16(0x05C6) && desc->idProduct == cpu_to_le16(0x9215))
urb->transfer_flags |= URB_ZERO_PACKET;
if (desc->idVendor == cpu_to_le16(0x2C7C))
urb->transfer_flags |= URB_ZERO_PACKET;
}
#endif
return urb;
}

31.1.3增加休眠后唤醒接口

当MCU进入暂停/休眠模式时,一些USB主机控制器/USB集线器将失去电源或重新设置,并且在MCU退出暂停/休眠模式后,它们不能恢复USB设备。请添加以下语句以启用重新设置恢复过程。
For Linux kernel version higher than 3.4:
File: [KERNEL]/drivers/usb/serial/option.c

static struct usb_serial_driver option_1port_device = {
……
#ifdef CONFIG_PM
.suspend = usb_wwan_suspend,
.resume = usb_wwan_resume,
#if 1 //Added by Quectel
.reset_resume = usb_wwan_resume,
#endif
#endif
};

31.1.4使用 GobiNet or QMI WWAN

如果客户使用ucxx/ec2x/egxx/EP06/EM06/BG96/AG35,并要求GobiNet或QMI WWAN,请添加以下语句,以防止这些模块接口4被用作USB串行设备。
For Linux Kernel Version newer than 2.6.30:
File: [KERNEL]/drivers/usb/serial/option.c

static int option_probe(struct usb_serial *serial, const struct usb_device_id *id) {
struct usb_wwan_intf_private *data;
……
#if 1 //Added by Quectel
//Quectel UC20's interface 4 can be used as USB network device
if (serial->dev->descriptor.idVendor == cpu_to_le16(0x05C6) &&
serial->dev->descriptor.idProduct == cpu_to_le16(0x9003)
&& serial->interface->cur_altsetting->desc.bInterfaceNumber >= 4)
return -ENODEV;
//Quectel EC20's interface 4 can be used as USB network device
if (serial->dev->descriptor.idVendor == cpu_to_le16(0x05C6) &&
serial->dev->descriptor.idProduct == cpu_to_le16(0x9215)
&& serial->interface->cur_altsetting->desc.bInterfaceNumber >= 4)
return -ENODEV;
//Quectel EC25&EC21&EG91&EG95&EG06&EP06&EM06&BG96/AG35's interface 4 can be used as USB network device
if (serial->dev->descriptor.idVendor == cpu_to_le16(0x2C7C)
&& serial->interface->cur_altsetting->desc.bInterfaceNumber >= 4)
return -ENODEV;
#endif
/* Store device id so we can use it during attach. */
usb_set_serial_data(serial, (void *)id);
return 0;
}

31.2 QMI WWAN驱动

31.2.1. Add VID and PID

QMI WWAN driver source file is [KERNEL]/drivers/net/usb/qmi_wwan.c.
File: [KERNEL]/drivers/net/usb/qmi_wwan.c

static const struct usb_device_id products[] = {
#if 1 //Added by Quectel
#ifndef QMI_FIXED_INTF
/* map QMI/wwan function by a fixed interface number */
#define QMI_FIXED_INTF(vend, prod, num) \
.match_flags = USB_DEVICE_ID_MATCH_DEVICE |
USB_DEVICE_ID_MATCH_INT_INFO, \
.idVendor = vend, \
.idProduct = prod, \
.bInterfaceClass = 0xff, \
.bInterfaceSubClass = 0xff, \
.bInterfaceProtocol = 0xff, \
.driver_info = (unsigned long)&qmi_wwan_force_int##num,
#endif
{ QMI_FIXED_INTF(0x05C6, 0x9003, 4) }, /* Quectel UC20 */
{ QMI_FIXED_INTF(0x2C7C, 0x0125, 4) }, /* Quectel EC25 */
{ QMI_FIXED_INTF(0x2C7C, 0x0121, 4) }, /* Quectel EC21 */
{ QMI_FIXED_INTF(0x05C6, 0x9215, 4) }, /* Quectel EC20 */
{ QMI_FIXED_INTF(0x2C7C, 0x0191, 4) }, /* Quectel EG91 */
{ QMI_FIXED_INTF(0x2C7C, 0x0195, 4) }, /* Quectel EG95 */
{ QMI_FIXED_INTF(0x2C7C, 0x0306, 4) }, /* Quectel EG06/EP06/EM06 */
{ QMI_FIXED_INTF(0x2C7C, 0x0296, 4) }, /* Quectel BG96 */
{ QMI_FIXED_INTF(0x2C7C, 0x0435, 4) }, /* Quectel AG35 */
#endif

31.2.2 Add Support for Raw IP Mode

File: [KERNEL]/drivers/net/usb/qmi_wwan.c

#include 
#include 
#if 1 //Added by Quectel
#include 
struct sk_buff *qmi_wwan_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
{
if (dev->udev->descriptor.idVendor != cpu_to_le16(0x2C7C))
return skb;
// Skip Ethernet header from message
if (skb_pull(skb, ETH_HLEN)) {
return skb;
} else {
dev_err(&dev->intf->dev, "Packet Dropped ");
}
// Filter the packet out, release it
dev_kfree_skb_any(skb);
return NULL;
}
#include 
#if (LINUX_VERSION_CODE < KERNEL_VERSION( 3,9,1 ))
static int qmi_wwan_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
{
__be16 proto;
if (dev->udev->descriptor.idVendor != cpu_to_le16(0x2C7C))
return 1;
/* This check is no longer done by usbnet */
if (skb->len < dev->net->hard_header_len)
return 0;
switch (skb->data[0] & 0xf0) {
case 0x40:
proto = htons(ETH_P_IP);
break;
case 0x60:
proto = htons(ETH_P_IPV6);
break;
case 0x00:
if (is_multicast_ether_addr(skb->data))
return 1;
/* possibly bogus destination - rewrite just in case */
skb_reset_mac_header(skb);
goto fix_dest;
default:
/* pass along other packets without modifications */
return 1;
}
if (skb_headroom(skb) < ETH_HLEN)
return 0;
skb_push(skb, ETH_HLEN);
skb_reset_mac_header(skb);
eth_hdr(skb)->h_proto = proto;
memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
fix_dest:
memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
return 1;
}
/* very simplistic detection of IPv4 or IPv6 headers */
static bool possibly_iphdr(const char *data)
{
return (data[0] & 0xd0) == 0x40;
}
#endif
#endif
……
/* if follow function exist, modify it as below */
static int qmi_wwan_bind(struct usbnet *dev, struct usb_interface *intf)
{
……
#if 1 //Added by Quectel
if (dev->udev->descriptor.idVendor == cpu_to_le16(0x2C7C)) {
dev_info(&intf->dev, "Quectel
EC25&EC21&EG91&EG95&EG06&EP06&EM06&BG96&AG35 work on RawIP mode\n");
dev->net->flags |= IFF_NOARP;
#if (LINUX_VERSION_CODE < KERNEL_VERSION( 3,9,1 ))
/* make MAC addr easily distinguishable from an IP header */
if (possibly_iphdr(dev->net->dev_addr)) {
dev->net->dev_addr[0] |= 0x02; /* set local assignment bit */
dev->net->dev_addr[0] &= 0xbf; /* clear "IP" bit */
}
#endif
usb_control_msg(
interface_to_usbdev(intf),
usb_sndctrlpipe(interface_to_usbdev(intf), 0),
0x22, //USB_CDC_REQ_SET_CONTROL_LINE_STATE
0x21, //USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE
1, //active CDC DTR
intf->cur_altsetting->desc.bInterfaceNumber,
NULL, 0, 100);
}
#endif
err:
return status;
}
……
/* if follow function exist, modify it as below */
static int qmi_wwan_bind_shared(struct usbnet *dev, struct usb_interface *intf)
{
……
#if 1 //Added by Quectel
if (dev->udev->descriptor.idVendor == cpu_to_le16(0x2C7C)) {
dev_info(&intf->dev, "Quectel EC25&EC21&
EG91&EG95&EG06&EP06&EM06&BG96&AG35 work on RawIP mode\n");
dev->net->flags |= IFF_NOARP;
#if (LINUX_VERSION_CODE < KERNEL_VERSION( 3,9,1 ))
/* make MAC addr easily distinguishable from an IP header */
if (possibly_iphdr(dev->net->dev_addr)) {
dev->net->dev_addr[0] |= 0x02; /* set local assignment bit */
dev->net->dev_addr[0] &= 0xbf; /* clear "IP" bit */
}
#endif
usb_control_msg(
interface_to_usbdev(intf),
usb_sndctrlpipe(interface_to_usbdev(intf), 0),
0x22, //USB_CDC_REQ_SET_CONTROL_LINE_STATE
0x21, //USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE
1, //active CDC DTR
intf->cur_altsetting->desc.bInterfaceNumber,
NULL, 0, 100);
}
#endif
err:
return status;
}
……
/* if follow struct exist, modify it as below */
static const struct driver_info qmi_wwan_info =
{
……
#if 1 //Added by Quectel
.tx_fixup = qmi_wwan_tx_fixup,
.rx_fixup = qmi_wwan_rx_fixup,
#endif
}
 
……
/* if follow struct exist, modify it as below */
static const struct driver_info qmi_wwan_force_int4 = {
……
#if 1 //Added by Quectel
.tx_fixup = qmi_wwan_tx_fixup,
.rx_fixup = qmi_wwan_rx_fixup,
#endif
};
/* if follow struct exist, modify it as below */
static const struct driver_info qmi_wwan_shared = {
……
#if 1 //Added by Quectel
.tx_fixup = qmi_wwan_tx_fixup,
.rx_fixup = qmi_wwan_rx_fixup,
#endif
};

31.3修改配置

第一步:进入配置环境
$make menuconfig
第二步:配置

Kernel modules >>
	USB Support >>
		<*> Kmod -usb-core
		-*-Kmod -usb-net
		-*- kmod-usb-net-cdc-ether
		<*> kmod-usb-net-cdc-mbim
		-*- kmod-usb-net-cdc-ncm
		<*> kmod-usb-net-cdc-subset
		<*>kmod-usb-net-qmi-wwan
		<*>Kmod-usb-ohci     //这个选项一定要勾选,否则可能无法在系统中查看设备
		<*>Kmod-usb-serial
		<*>Kmod-usb-serial-option
		<*>Kmod-usb-serial-wwan
		<*>kmod-usb-uhci
		<*>Kmod-usb2
NetWork   >>
	<*> wwan
	<*>chat
	<*>ppp
	<*>uqmi
Utilities
	-*- comgt
	<*> comgt-ncm 
	<*>usb-modeswitch

Luci以下为可选:

Luci
1. Collections
  		<*> luci
3. Applications
  		<*> luci-app-multiwan (optional to support multiple 3g dongles)
  		<*> luci-app-qos (optional to provide QOS support)
6. Protocols
  		<*> luci-proto-3g
  		-*- luci-proto-ppp

31.4编译测试

以上操作完成后就是编译源码了。
$ make V=99
将固件烧写进入板子中,插入4G模块,启动板子会看到以下信息。

在这里插入图片描述
《OpenWrt开发笔记》第31章 OpenWrt 4G拨号上网(Ubuntu12.04+Openwrt15.05)_第1张图片

也可以在dev目录下查看

《OpenWrt开发笔记》第31章 OpenWrt 4G拨号上网(Ubuntu12.04+Openwrt15.05)_第2张图片

有以上信息表示驱动配置成功,接下来就拨号了。

31.5拨号上网

拨号程序仍然使用移远提供的quectel-CM,这是一个4G连接管理程序,这里没什么说的,交叉编译后得到可执行bin“quectel-CM”,

$mipsel-openwrt-linux-gcc *.c -o quectel-CM -lpthread –ldl

【注】将quectel-CM下的Makefile文件修改成如下,然后 make
CROSS-COMPILE:=mipsel-openwrt-linux-

《OpenWrt开发笔记》第31章 OpenWrt 4G拨号上网(Ubuntu12.04+Openwrt15.05)_第3张图片

把这个bin放到板卡上启动后上述一、二驱动没有问题的情况下直接执行该bin就可以。将quectel-CM可执行程序拷贝到板子中。

后台运行:./quectel-CM &

root@OpenWrt:~# ./quectel-CM &
root@OpenWrt:~# [09-05_02:21:55:872] Quectel_Linux_ConnectManager_SR01A01V30
[09-05_02:21:55:872] ./quectel-CM profile[1] = (null)/(null)/(null)/0, pincode = (null)
[09-05_02:21:55:874] Find /sys/bus/usb/devices/1-1 idVendor=2c7c idProduct=0125
[09-05_02:21:55:875] Find /sys/bus/usb/devices/1-1:1.4/net/wwan0
[09-05_02:21:55:875] Find usbnet_adapter = wwan0
[09-05_02:21:55:876] Find /sys/bus/usb/devices/1-1:1.4/usbmisc/cdc-wdm0
[09-05_02:21:55:877] Find qmichannel = /dev/cdc-wdm0
[09-05_02:21:55:896] cdc_wdm_fd = 7
[09-05_02:21:55:996] Get clientWDS = 20
[09-05_02:21:56:027] Get clientDMS = 1
[09-05_02:21:56:061] Get clientNAS = 4
[09-05_02:21:56:091] Get clientUIM = 1
[09-05_02:21:56:124] Get clientWDA = 1
[09-05_02:21:56:156] requestBaseBandVersion EC20CEFAGR06A08M4G
[09-05_02:21:56:221] requestGetSIMStatus SIMStatus: SIM_READY
[09-05_02:21:56:253] requestGetProfile[1] ///0
[09-05_02:21:56:285] requestRegistrationState2 MCC: 460, MNC: 0, PS: Attached, DataCap: LTE
[09-05_02:21:56:316] requestQueryDataCall IPv4ConnectionStatus: DISCONNECTED
[09-05_02:21:56:382] requestRegistrationState2 MCC: 460, MNC: 0, PS: Attached, DataCap: LTE
[09-05_02:21:57:084] requestSetupDataCall WdsConnectionIPv4Handle: 0x8722b410
[09-05_02:21:57:148] requestQueryDataCall IPv4ConnectionStatus: CONNECTED
[09-05_02:21:57:180] ifconfig wwan0 up
[09-05_02:21:57:207] busybox udhcpc -f -n -q -t 5 -i wwan0
[09-05_02:21:57:229] udhcpc (v1.23.2) started
[09-05_02:21:57:255] Sending discover…
[09-05_02:21:57:261] Sending select for 10.28.26.75…
[09-05_02:21:57:264] Lease of 10.28.26.75 obtained, lease time 7200
[09-05_02:21:57:276] udhcpc: ifconfig wwan0 10.28.26.75 netmask 255.255.255.248 broadcast +
[09-05_02:21:57:296] udhcpc: setting default routers: 10.28.26.76

这就是拨号成功了,接下来就是ping一下看是否可以联网。
$ping 47.106.72.113

在这里插入图片描述

【注意】这里需要UDHCPC ,检测你的busybox是否有这个东西,如果不存在,你需要重新移植busybox,启用CONFIG_UDHCPC选项。还需要配置一个配置文件,注意检查。可以在开发板中输入busybox中查看,笔者的文件系统有,所以就不需要配置了。

《OpenWrt开发笔记》第31章 OpenWrt 4G拨号上网(Ubuntu12.04+Openwrt15.05)_第4张图片

参考网页:
https://wiki.openwrt.org/doc/howtobuild/wireless-router-with-a-3g-dongle
参考文档:Quectel_WCDMA<E_Linux_USB_Driver_User_Guide_V1.7.pdf

附件
点击进入下载

你可能感兴趣的:(EC25,4G,RT5350)