Ubuntu – Automatically use usb_modeswitch for Wifi USB

systemdudevusb-modeswitch

I've recently purchased a Wifi Dongle containing a Realtek rtl8811CU chipset. I'm using Ubuntu 18.04 LTS.

Luckily, after finding this Github repo, I could compile it and install the driver without any problem.

However, every time I connect the dongle, is detected as a Disk, and I need to run usb_modeswitch to make it work:

$ lsusb
 [...]
 Bus 001 Device 013: ID 0bda:1a2b Realtek Semiconductor Corp. 
$ sudo usb_modeswitch -KW -v 0bda -p 1a2b
$ lsusb
 [...]
 Bus 001 Device 014: ID 0bda:c811 Realtek Semiconductor Corp.

The device ID changes and now I can correctly use my Wifi dongle.

I've tried to add this entry, to the udev rules, located in /lib/udev/rules.d/40-usb_modeswitch.rules, supposedly to run the command every time the device is detected:

 # Realtek 8211CU Wifi AC USB
 ATTR{idVendor}=="0bda", ATTR{idProduct}=="1a2b", RUN+="usb_modeswitch '/%k'"

But then I recconect, and nothing happens.

I've checked that maybe you need a service to do this, but it seems that cannot be activated.

$ systemctl --all | grep usb_modeswitch
● usb_modeswitch@1-4.1.service    loaded    failed   failed    USB_ModeSwitch_1-4.1                                                            
system-usb_modeswitch.slice       loaded    active   active    system-usb_modeswitch.slice    

$ systemctl status usb_modeswitch@1-4.1.service
● usb_modeswitch@1-4.1.service - USB_ModeSwitch_1-4.1
Loaded: loaded (/lib/systemd/system/usb_modeswitch@.service; static; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2018-10-04 11:49:17 CDT; 2min 5s ago
    Docs: man:usb_modeswitch_dispatcher(1)
Process: 32216 ExecStart=/usr/sbin/usb_modeswitch_dispatcher --switch-mode 1-4.1 (code=exited, status=1/FAILURE)
Main PID: 32216 (code=exited, status=1/FAILURE)

oct 04 11:49:17  systemd[1]: Starting USB_ModeSwitch_1-4.1...
oct 04 11:49:17  usb_modeswitch_dispatcher[32216]: Could not read attribute: No such file or directory
oct 04 11:49:17  usb_modeswitch_dispatcher[32216]: Could not read attribute: No such file or directory
oct 04 11:49:17  systemd[1]: usb_modeswitch@1-4.1.service: Main process exited, code=exited, status=1/FAILURE
oct 04 11:49:17  systemd[1]: usb_modeswitch@1-4.1.service: Failed with result 'exit-code'.
oct 04 11:49:17  systemd[1]: Failed to start USB_ModeSwitch_1-4.1.

Seems that there is a problem with the Ubuntu wrapper, does anyone knows how I can investigate further? I'm new to systemd and udev rules, so I'm not really sure where to find more logs or debug features.

Best Answer

I've found the way to do it. I've modified the file /lib/udev/rules.d/40-usb_modeswitch.rules appending before the line LABEL="modeswitch_rules_end" the following:

# Realtek 8211CU Wifi AC USB
ATTR{idVendor}=="0bda", ATTR{idProduct}=="1a2b", RUN+="/usr/sbin/usb_modeswitch -K -v 0bda -p 1a2b"

After that, every time I connected the wifi dongle, the disk doesn't appear and automatically the Wifi is detected. Maybe it's not the cleanest method, but it works.

Related Question