Linux采用4G模块上网_NDIS拨号


1.说明

本章将介绍Jetson Nano如何采用SIM7600 4G模块进行无线上网,并描述其相关细节,本章将先讲解NDIS拨号。

2.采用4G模块的上网方式有哪些?

Jetson Nano 或Raspberry Pi通过SIM7600模组,可采用以下几种方式进行无线上网:

  • 采用AT指令使用封装好的TCP,MQTT,HTTP(S)

    该方式适用于资源比较紧张的微处理器,如MCU等,或适用于对数据量比较少的应用场景,如通过http(s),MQTT给服务器,云平台等上传传感器数据。
    对于Jetson Nano,如果网络应用不复杂,数据量比较小时(比如传输传感器数据至服务器,接收服务器下发的控制指令),采用AT指令即可快速使用功能。

  • 采用NDIS驱动上网

    该方式必须依赖于Linux系统,适用于需要采用Linux网络套接字编程开发的应用场景,将驱动加载到内核后,将SIM7600和Jetson Nano采用USB线连接,待SIM7600开机后,即可识别到wwan0网口,可通过该网口上网。
    该方式底层依赖于SIM7600的USB虚拟串口。
    该方式为官方推荐的上网方式,同样,建议采用该种方式进行拨号上网。

  • 采用ppp拨号上网

    该方式必须依赖于Linux系统,适用于需要采用Linux网络套接字编程开发的应用场景,配置并运行相关脚本后,将SIM7600和Jetson Nano采用USB线连接,待SIM7600开机后,即可识别到ppp0网口,可通过该网口上网。
    该方式底层依赖于SIM7600的USB虚拟串口。

  • 采用wvdial拨号上网

    该方式的底层实现同ppp拨号上网,不再阐述。

3. 准备工作

接下来,将介绍Jetson Nano或Raspberry Pi采用NDIS驱动通过SIM7600 4G模块上网的过程。

3.1 硬件准备

3.2 硬件连接

硬件连接如下图所示:

注意

1.若接到40Pin引脚上使用,则注意将SIM7600的Flight Mode,PWR引脚拉低,即需要设计初始化脚本,详情见相关产品页面,否则将进入飞行模式。

2.若树莓派通过USB线单独给SIM7600供电,则可能出现供电不稳定,导致SIM7600开机后关机的现象。

3.若仅仅使用拨号上网功能,则可以不接到Jetson Nano或Raspberry Pi的40Pin上。

4. 正式开始

4.1 检查硬件连接,网络连接

  • 长按SIM7600CE 4G HAT上的PWKKEY键,使得SIM7600CE 4G HAT开机,正常开机后,NET灯应当闪烁(若未闪烁,请检查SIM卡是否可用,或是否进入了飞行模式)。

  • 检查Jetson Nano的Ubuntu系统或树莓派的Raspbian系统是否内置了高通USB串口驱动,非新版本镜像系统可能未内置该驱动,查看方式如下图所示:

    # 查看Ubuntu版本
    uname -a
    lsb_release -a
    cat /proc/version
    # 查看是否有高通USB虚拟串口驱动
    lsusb


    如果能看到Qualcomm/Option字样,则说明内置了高通USB虚拟串口驱动

  • 输入AT指令,设置为4G上网,查看网络连接状态

    # 关闭ModemManager进程,以防止minicom调试AT串口时,显示没用的数据
    sudo su
    killall ModemManager
    # 安装minicom串口工具
    apt-get install minicom
    # 查看串口设备,AT指令串口,为/dev/ttyUSB2
    ls /dev/ttyUSB*
    # 用minicom打开串口
    sudo minicom -D /dev/ttyUSB2
    # 强制设置为4G上网
    AT+CNMP=38
    # 查询网络质量
    AT+CSQ
    # 查询网络注册状
    AT+CREG?
    # 查询网络运营商
    AT+COPS?
    # 查询网络波段
    AT+CPSI?

至此,SIM7600CE和Jetson Nano或Raspberry Pi硬件连接正常,网络连接正常。

4.2 编译并安装驱动模块文件

对于Jetson Nano的Ubuntu系统,默认没有simcom的用于wwan0网口的驱动模块文件,可用以下命令查看

lsmod

但是对于Raspberry Pi的Raspbian系统,默认是安装了高通的用于wwan0网口的驱动模块文件,如下图:

对于Raspberry Pi,需要先卸载该驱动,再安装上simcom的用于wwan0网口的驱动模块文件,卸载方式如下:

sudo su
rmmod qmi_wwan

在此基础上,执行后续步骤。

将编译以下驱动模块源程序:

该源程序及其Makefile将在文章末尾出给出

为了避免交叉编译的繁琐(交叉编译需要在虚拟机Ubuntu下编译Jetson Nano内核后,再编译驱动模块),可直接将以上两个文件保存到Jetson Nano文件系统中,再编译,注意该过程需要使用超级用户,如下图所示:

make

由上图可查看到,已编译成内核驱动模块文件。

接下来,将安装该驱动模块文件,注意需要使用超级用户:

insmod simcom_wwan.ko
lsmod

此时,simcom_wwan驱动模块已安装上。

可查看到内核的打印信息:

dmesg

Raspberry Pi编译及安装驱动模块的操作同Jetson Nano。

4.3 开始拨号,并分配IP

此时,可看到已经出现wwan0网口:

ifconfig -a

但是该网口并未分配IP地址,如需分配IP,需要

  • 首先开启网口
ifconfig wwan0 up
  • 然后拨号
minicom -D /dev/ttyUSB2
AT$QCRMCALL=1,1

  • 最后,分配IP
apt-get install udhcpc
udhcpc -i wwan0

此时,已经分配到IP,可对该网口进行测试:

ifconfig -a
ping -I wwan0 www.baidu.com

由上图可知,wwan0网口测试通过。

如果出现dns解析异常,输入以下命令可解决:

route add -net 0.0.0.0 wwan0

5 测速

最后,对采用NDIS拨号上网进行测速:


注意,以上数值仅供参考,不同地域,不同运营商,测速所得出的数据略有差距,在理想的情况下,在SIMCOM实验室测试,理想上行与下行速度为20Mbps。

6 驱动源码

simcom_wwan.c&Makefile:

/*
 * Copyright (c) 2016 Xiaobin Wang <xiaobin.wang@sim.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 */

/*
 * history 
 * V1.00 - first release  -20160822
*/

#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/ethtool.h>
#include <linux/etherdevice.h>
#include <linux/mii.h>
#include <linux/usb.h>
#include <linux/usb/cdc.h>
#include <linux/usb/usbnet.h>


/* very simplistic detection of IPv4 or IPv6 headers */
static bool possibly_iphdr(const char *data)
{
	return (data[0] & 0xd0) == 0x40;
}

/* SIMCOM devices combine the "control" and "data" functions into a
 * single interface with all three endpoints: interrupt + bulk in and
 * out
 */
static int simcom_wwan_bind(struct usbnet *dev, struct usb_interface *intf)
{
	int rv = -EINVAL;
  
	//struct usb_driver *subdriver = NULL;
	atomic_t *pmcount = (void *)&dev->data[1];

  /* ignore any interface with additional descriptors */
	if (intf->cur_altsetting->extralen)
		goto err;
  
	/* Some makes devices where the interface descriptors and endpoint
	 * configurations of two or more interfaces are identical, even
	 * though the functions are completely different.  If set, then
	 * driver_info->data is a bitmap of acceptable interface numbers
	 * allowing us to bind to one such interface without binding to
	 * all of them
	 */
	if (dev->driver_info->data &&
	    !test_bit(intf->cur_altsetting->desc.bInterfaceNumber, &dev->driver_info->data)) {
		dev_info(&intf->dev, "not on our whitelist - ignored");
		rv = -ENODEV;
		goto err;
	}

	atomic_set(pmcount, 0);

	/* collect all three endpoints */
	rv = usbnet_get_endpoints(dev, intf);
	if (rv < 0)
		goto err;

	/* require interrupt endpoint for subdriver */
	if (!dev->status) {
		rv = -EINVAL;
		goto err;
	}

	/* can't let usbnet use the interrupt endpoint */
	dev->status = NULL;

	printk("simcom usbnet bind here\n");

  /*
   * SIMCOM SIM7600 only support the RAW_IP mode, so the host net driver would
   * remove the arp so the packets can transmit to the modem
  */
  dev->net->flags |= IFF_NOARP;
    
  /* 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 */
	}
	
  /*
   * SIMCOM SIM7600 need set line state
  */
  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, //line state 1
      intf->cur_altsetting->desc.bInterfaceNumber,
      NULL,0,100);

err:
	return rv;
}

static void simcom_wwan_unbind(struct usbnet *dev, struct usb_interface *intf)
{
	struct usb_driver *subdriver = (void *)dev->data[0];

	if (subdriver && subdriver->disconnect)
		subdriver->disconnect(intf);

	dev->data[0] = (unsigned long)NULL;
}

#ifdef CONFIG_PM
static int simcom_wwan_suspend(struct usb_interface *intf, pm_message_t message)
{
	struct usbnet *dev = usb_get_intfdata(intf);
	int ret;

	ret = usbnet_suspend(intf, message);
	if (ret < 0)
		goto err;
		
err:
	return ret;
}

static int simcom_wwan_resume(struct usb_interface *intf)
{
	struct usbnet *dev = usb_get_intfdata(intf);
	int ret = 0;
	
	ret = usbnet_resume(intf);

err:
	return ret;
}
#endif

struct sk_buff *simcom_wwan_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
{

  //skip ethernet header 
  if(skb_pull(skb, ETH_HLEN))
  {
    return skb;
  }else
  {
    dev_err(&dev->intf->dev, "Packet Dropped\n");
  }

  if (skb != NULL)
    dev_kfree_skb_any(skb);

   return NULL;
}

static int simcom_wwan_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
{
  __be16 proto;

  /* 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:
		printk("packetv4 coming ,,,\n");
		proto = htons(ETH_P_IP);
		break;
	case 0x60:
		printk("packetv6 coming ,,,\n");
		proto = htons(ETH_P_IPV6);
		break;
	case 0x00:
		printk("packet coming ,,,\n");
		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;
}

static const struct driver_info	simcom_wwan_usbnet_driver_info = {
	.description	= "SIMCOM wwan/QMI device",
	.flags		= FLAG_WWAN,
	.bind		  = simcom_wwan_bind,
	.unbind		= simcom_wwan_unbind,
	.rx_fixup       = simcom_wwan_rx_fixup,
	.tx_fixup       = simcom_wwan_tx_fixup,
};

static const struct usb_device_id products[] = {
  {USB_DEVICE(0x1e0e, 0x9025), .driver_info = (unsigned long)&simcom_wwan_usbnet_driver_info },
	{USB_DEVICE(0x1e0e, 0x9001), .driver_info = (unsigned long)&simcom_wwan_usbnet_driver_info },
	{ } /* END */
};

MODULE_DEVICE_TABLE(usb, products);

static struct usb_driver simcom_wwan_usb_driver = {
	.name		        = "simcom_wwan",
	.id_table	      = products,
	.probe		      =	usbnet_probe,
	.disconnect	    = usbnet_disconnect,
#ifdef CONFIG_PM
	.suspend	      = simcom_wwan_suspend,
	.resume		      =	simcom_wwan_resume,
	.reset_resume         = simcom_wwan_resume,
	.supports_autosuspend = 1,
#endif
};

static int __init simcom_wwan_init(void)
{
	return usb_register(&simcom_wwan_usb_driver);
}
module_init(simcom_wwan_init);

static void __exit simcom_wwan_exit(void)
{
	usb_deregister(&simcom_wwan_usb_driver);
}
module_exit(simcom_wwan_exit);

MODULE_AUTHOR("Xiaobin Wang <xiaobin.wang@sim.com>");
MODULE_DESCRIPTION("SIM7600 RMNET WWAN driver");
MODULE_LICENSE("GPL");
obj-m:=simcom_wwan.o    
simcom_wwanmodule-objs:=module    
KDIR:=/lib/modules/$(shell uname -r)/build    
MAKE:=make    
default:
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules    
clean:
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean 

Author: Ruimin Huang
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Ruimin Huang !
  TOC