0%

使用 mdev 检测 3g 设备并自动连接

udevrules 用于 3G 设备的自动连接,而嵌入式系统中使用精简版的 mdev,没有 rules 来完成 3G 设备的自动连接,本文记录折腾过程

Linux Version: 4.9.22

0

插入设备 -> 判断型号是否支持 -> usb modeswitch -> ttyUSB/ttyACM -> ppp

Linux 内核配置

针对 3G ppp 配置不再赘述,使用 mdev 对设备 sg,配置如下

sg0 0:6 660 @ /etc/sbin/modeswitch $MDEV $ACTION

内核需要打开 CONFIG_CHR_DEV_SG,从而产生设备节点 /dev/sg*

modeswitch 中判断型号是否支持,进行 usb modeswitch,从而产生设备 /dev/ttyUSB/dev/ttyACM,配置如下

SUBSYSTEM=tty;ttyUSB0  0:6 660 * /etc/sbin/lookup $MDEV $ACTION
SUBSYSTEM=tty;ttyACM0  0:6 660 * /etc/sbin/lookup $MDEV $ACTION

lookup 中将设备添加到管理列表中,从而完成设备的添加及删除

部分设备使用的是 /dev/ttACM,需要内核打开配置 CONFIG_USB_ACM

Example

Modeswitch

for device in $usb_device_list
do
    # look up  usb 3g  storage config
    usb_3g_storage_list=$(grep "support_id_usbdisc" /home/sbin/3g_card_list | grep $device | wc -l)

    if [ $usb_3g_storage_list -eq 0 ];then
        continue
    fi

    usb_3g_vendor_id=$(echo $device | awk -F":" '{print $1}')
    usb_3g_product_id=$(echo $device | awk -F":" '{print $2}')

    if [ -z $usb_3g_vendor_id ];then
        # no usb 3g
        exit 1
    fi

    usb_3g_delay_ms=0
    if [ "$usb_3g_vendor_id:$usb_3g_product_id" = "19d2:2000" ];then
        usb_3g_delay_ms=5000
    fi

    usb_3g_modeswitch=$(ps -o comm | grep -w usb_modeswitch)
    if [ ! -z $usb_3g_modeswitch ];then
        #modeswitch not finish
        exit 1
    fi

    convert_device=/dev/ttyUSB0
    if [ "$usb_3g_vendor_id:$usb_3g_product_id" = "19d2:1514" ]; then
        convert_device=/dev/ttyACM0
    fi

    echo -e "\033[1;31m[3G MODESWITCH] New 3G <$device> in $1, convert to $convert_device\033[0m" > /dev/ttyS0

    if [ ! -e $convert_device ];then
        # convert stroage device to ttyUSB0/ttyACM0
        if [ $usb_3g_delay_ms -gt 0 ];then
            usb_modeswitch -c /etc/usb_modeswitch.d/${usb_3g_vendor_id}:${usb_3g_product_id} -v 0x${usb_3g_vendor_id} -p 0x${usb_3g_product_id} -w $usb_3g_delay_ms &
        else
            usb_modeswitch -c /etc/usb_modeswitch.d/${usb_3g_vendor_id}:${usb_3g_product_id} -v 0x${usb_3g_vendor_id} -p 0x${usb_3g_product_id} &
            if [ "$usb_3g_vendor_id:$usb_3g_product_id" = "0685:2000" ];then
                sleep 5
            fi
        fi
    fi
    exit 0
done
exit 1

Lookup

usb_device_list=$(lsusb | awk '{print $6}' | uniq | grep -v $usb_host_vendor_id)

for device in $usb_device_list
do
    # look up usb 3g net card
    usb_eth3g_net_card_list=$(grep "eth3g_id_switch" /home/sbin/3g_card_list | grep $device | wc -l)
    if [ $usb_eth3g_net_card_list -ne 0 ];then
        echo $USB_ETH3G_DEVNAME-$device# > $return_file
        echo -e "\033[1;32m[3G LOOKUP] $2 $USB_ETH3G_DEVNAME-$device with $1\033[0m" > /dev/ttyS0
        exit 0
    fi

    usb_3g_net_card_list=$(grep "support_id_switch" /home/sbin/3g_card_list | grep $device | wc -l)
    if [ $usb_3g_net_card_list -ne 0 ];then
        echo $USB_3G_DEVNAME-$device# > $return_file
        echo -e "\033[1;32m[3G LOOKUP] $2 $USB_3G_DEVNAME-$device with $1\033[0m" > /dev/ttyS0
        exit 0
    fi
done

exit 1

Ref

  1. FL2440 3G 上网卡拨号上网,并实现 mdev 自动挂载
  2. lubuntu do not detect 3g mobile broadband huawei
  3. Huawei E1550 3G modem
  4. USB 3G Modem
  5. 关于华为 3G/4G 上网卡设备使用 usb modemswitch 的问题