I'm working on a driver for a device called the Tourbox, for Linux specifically. I have a working version, which uses udev rules to start the driver process whenever it detects the device connects.
The issue with this approach is udev only detects the device when it's plugged in. If it's already connected at boot, nothing happens. Ideally, too, I'd rather have the driver process run constantly in the background to make configuration and error reporting more consistent.
The Tourbox exposes itself as a serial port over USB. I can identify the device with its vendor/product ID, but I've been struggling to find a way to go from that to identifying the specific serial device to target within Rust. My initial thought was the udev crate, given it could monitor for devices being added, but I couldn't find a way to go from USB device to a serial port (and, even if I could, the example given for monitoring is way above my current knowledge).
I'm sure this is a problem other people have run into, wanted to see if anyone knew the best direction to approach this from.
I could be wrong, but looking at your code in not sure you are writing a driver. I.e. I believe all Linux drivers run in kernel space, not user space (monolithic vs micro kernel).
Why not just run it up as a service that will check if the device is available at startup? (I believe what you are after is a service rather than a driver.)
That's fair, 'service' is probably the correct term, I'm just calling it a 'driver' since I feel that's the term more people would be familiar with.
Either way- what you suggested is how I want it to work, I just need to figure out a way to actually do that. I can enumerate devices with the udev crate, but I don't know how to associate a tty device with usb to compare the vendor/product ID.
@rikyborg solution should work best for you because you should be able to identify the device using VID and PID (unless it is using a generic USB Serial chip). Easiest way would be to start it as a service and write a detection function (basically the main function that @rikyborg linked) that you call it in a loop until the device is present. (Of course put a thread::sleep(..) in the loop so you don’t thrash the CPU polling.) This way it will detect both device changes and already present device.
There are quite a few articles about making a systemd service. The links below should help you.