-2

I have a USB device that I'm using and I'm developing an application using WebUSB with Google Chrome. The thing is whenever I plug the USB device into my Linux computer, I have to manually run sudo modprobe -r ftdi_sio to unload it.

I want it to be unloaded automatically whenever I plug that device into my computer instead of having to type it manually every single time.

Any ideas on how this could be implemented ? Help would be much appreciated

KZander
  • 300
  • 4
  • 16

5 Answers5

1

Put your command in /etc/rc.local and restart. See if it works. Or you can find how other .ko are configured to automatically load during system startup. Follow the same to make your module load automatically.

  • First of all thank you for your reply !! This works when I restart my computer, not when I unplug-then-plug my device. I need this command to be initiated when I plug my device to my computer. Because every time I unplug my device I would have to run this command manually. – KZander Nov 07 '19 at 09:25
  • Or atleast in a way, to NEVER let Linux load this device for its own use. – KZander Nov 07 '19 at 09:35
1

One option would be to "blacklist" the ftdi_sio module to stop it being loaded automatically. To do that create the following file:

/etc/modprobe.d/ftdi_sio-blacklist.conf

# This is a comment. Change it if you want.
blacklist ftdi_sio
Ian Abbott
  • 15,083
  • 19
  • 33
1

The proper way is to create a udev rule that is triggered when the specific USB device is attached.

Create a file /etc/udev/rules.d/99-usb-load.rules, and replace the "7523" and "1a86" with the Product ID and Vendor ID of your USB device.

# For debugging if the rule is working
ACTION=="add", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+="/bin/sh -c '/bin/echo inserted device >> /tmp/udev_file'"
ACTION=="remove", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+="/bin/sh -c '/bin/echo removed device >> /tmp/udev_file'"

# Actual rules
ACTION=="add", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+="/sbin/rmmod ftdi_sio"
ACTION=="remove", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+=""

Restart the udev daemon

sudo /etc/init.d/udev restart

The add rule will be executed whenever the USB device with matching PID/VID is attached and it will unload the module ftdi_sio. The first two rules are for debugging purposes which write a line into /tmp/udev_file and can be used to verify if the rules has been triggered.

Yasir Khan
  • 645
  • 4
  • 9
  • Thank you for your reply, I'll try it out now – KZander Nov 08 '19 at 07:31
  • Did you get the logs in /tmp/udev_file? The first thing is to make sure that device triggers the rule correctly. – Yasir Khan Nov 08 '19 at 08:29
  • It might help if you could share some logs while adding the desired device. First the output of udevadm monitor --property and then udevadm monitor. Launch this command in one terminal and insert your device. – Yasir Khan Nov 08 '19 at 08:34
  • No unfortunately, I checked before commenting. I even tried putting those commands in the rule I had already created before, didn't work either. I even tried restarting my laptop but unfortunately didn't get anywhere. Thank you for help !! – KZander Nov 08 '19 at 08:34
1

Looks like USB device is new and there are no existing drivers to handle as soon as it is plugged in. You need an interrupt line and a USB driver code for your requirement. You need to register your device to that driver and driver to the USB bus. Also need to write appropriate interrupt routines to be called as soon as your device is plugged in. This will make it work as you want !!

1

If you already have a .rules file for the USB device then append the following to the pre-existing file in the /etc/udev/rules.d/ directory. Otherwise, create a file in the /etc/udev/rules.d/ directory with the following content:

ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", ACTION=="add", RUN+="/sbin/rmmod ftdi_sio"
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", ACTION=="remove", RUN+=""

This means that when the device with vendorID 0403 and product ID 6010 is plugged in it runs /sbin/rmmod ftdi_sio which removes the ftdi_sio module. The second rule means nothing will be run when unplugged. See [writing udev rules] for more info on how to name the .rules file (e.g. 99-mydevice.rules)

You'll have to replace 0403 and 6010 with your device's vendor ID and prodcut ID. The vendor ID and product ID can be found by running lsusb on the command line after plugging in the USB device. It will have the format:

Bus xxx Device xxx: ID idVendor:idProduct ManufacturerName

After creating or editing the .rules file in /etc/udev/rules.d/ directory be sure to reload the .rules file with the following command:

sudo udevadm control --reload

Some further references on udev rules:

debian wiki

arch wiki

writing udev rules

dopamane
  • 1,315
  • 2
  • 14
  • 27