How to automate usb_modeswitch?

I use a USB LTE modem to connect to the internet. The problem is, every time I connect it to my PC, it first gets recognised as a mass storage device. I need to manually run this command every time I plug it in: sudo usb_modeswitch -J -v 12d1 -p 14fe to change its mode to Modem/Networkcard. It's not a great problem, but it becomes very tiring to those steps every time.
How do I automate this process such that every time I plug in the device it gets recognised as a Modem in the first place?

1 Answer

The easiest way is to create an udev rule in /etc/udev/rules.d/70-usb-modeswitch.rules .

For example with a USB LTE stick from another vendor:

# own udev rule USB LTE
# switch from mass storage mode to modem modem
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="1c9e", ATTRS{idProduct}=="9bfe", RUN+="/usr/sbin/usb_modeswitch -v 1c9e -p 9bfe -M '55534243123456780000000000000606f50402527000000000000000000000'"
# load driver for modem mode
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="1c9e", ATTRS{idProduct}=="9b01", RUN+="/bin/bash -c 'modprobe option && echo 1c9e 9b01 > /sys/bus/usb-serial/drivers/option1/new_id'"
# rule for modemmanager (example /lib/udev/rules.d/77-mm-longcheer-port-types.rules)
ACTION!="add|change", GOTO="mm_longcheer_port_types_end"
SUBSYSTEM!="tty", GOTO="mm_longcheer_port_types_end"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="1c9e", GOTO="mm_longcheer_vendorcheck"
GOTO="mm_longcheer_port_types_end"
LABEL="mm_longcheer_vendorcheck"
SUBSYSTEMS=="usb", ATTRS{bInterfaceNumber}=="?*", ENV{.MM_USBIFNUM}="$attr{bInterfaceNumber}"
ATTRS{idVendor}=="1c9e", ATTRS{idProduct}=="9b01", ENV{.MM_USBIFNUM}=="03", ENV{ID_MM_LONGCHEER_PORT_TYPE_MODEM}="1"
ATTRS{idVendor}=="1c9e", ATTRS{idProduct}=="9b01", ENV{.MM_USBIFNUM}=="00", ENV{ID_MM_LONGCHEER_PORT_TYPE_AUX}="1"
ATTRS{idVendor}=="1c9e", ATTRS{idProduct}=="9b01", ENV{.MM_USBIFNUM}=="01", ENV{ID_MM_LONGCHEER_PORT_TYPE_AUX}="1"
ATTRS{idVendor}=="1c9e", ATTRS{idProduct}=="9b01", ENV{.MM_USBIFNUM}=="02", ENV{ID_MM_LONGCHEER_PORT_TYPE_AUX}="1"
ATTRS{idVendor}=="1c9e", ATTRS{idProduct}=="9b01", ENV{ID_MM_LONGCHEER_TAGGED}="1"
GOTO="mm_longcheer_port_types_end"

After saving your rule do udevadm control --reload-rules.

see also this longer blog post

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like