Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumerate more subsystems without libudev #238

Merged

Conversation

sirhcel
Copy link
Contributor

@sirhcel sirhcel commented Jan 4, 2025

The serialport crate currently has two different port enumeration strategies on Linux: using libudev and manually scanning /sys/class/tty.

But there is a discrepancy between the ports returned. For example, the serial ports from a PCI card are not enumerated by the latter. Ports returned with the libudev enumeration on this machine:

$ cargo run --target x86_64-unknown-linux-gnu --features usbportinfo-interface --example list_ports
[...]
Found 7 ports:
    /dev/ttyS0
        Type: Unknown
    /dev/ttyS4
        Type: PCI
    /dev/ttyS5
        Type: PCI
    /dev/ttyUSB2
        Type: USB
        VID: 0403
        PID: 6001
        Interface: 00
        Serial Number: FTFOO
        Manufacturer: FTDI
        Product: UT232R
    /dev/ttyUSB3
[...]

And the ports returned without:

$ cargo run --target x86_64-unknown-linux-musl --no-default-features --features usbportinfo-interface --example list_ports
[...]
Found 5 ports:
    /dev/ttyS0
        Type: Unknown
    /dev/ttyUSB2
        Type: USB
        VID: 0403
        PID: 6001
        Interface: 00
        Serial Number: FTFOO
        Manufacturer: FTDI
        Product: UT232R
    /dev/ttyUSB3
[...]

This PR sets out to close this gap by using the device subsystem as criterion whether to report a certain device or not. Previously, presence and contents of the driver_override file in the sysfs tree was used but this excluded PCI devices as well as the UARTs of Raspberry Pi devices.

Enumeration of ports without libudev with the changes from this PR:

$ cargo run --target x86_64-unknown-linux-musl --no-default-features --features usbportinfo-interface --example list_ports
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/x86_64-unknown-linux-musl/debug/examples/list_ports`
Found 7 ports:
    /dev/ttyS0
        Type: Unknown
    /dev/ttyS4
        Type: PCI
    /dev/ttyS5
        Type: PCI
    /dev/ttyUSB2
        Type: USB
        VID: 0403
        PID: 6001
        Interface: 00
        Serial Number: FTFOO
        Manufacturer: FTDI
        Product: UT232R
    /dev/ttyUSB3
[...]

There might be corner cases still not caught by this PR. But it improves the enumeration results for cases which knowingly failed beforehand.

The PCI serial ports I have at hand have been rejected previously by the
driver_override criterion: having the file present and its contents
being '(null)'.

Looking at the subsystem instead allows us to tell "actually existing"
serial ports apart from other TTYs which are not actual serial devices.
For these UARTs the driver_overrive file containing '(null)' is also
present and the previous filtering would have ignored them as well.
@sirhcel sirhcel marked this pull request as ready for review January 9, 2025 21:50
@sirhcel sirhcel requested a review from eldruin January 9, 2025 21:50
@sirhcel sirhcel mentioned this pull request Jan 9, 2025
Copy link
Contributor

@eldruin eldruin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thank you for your work!

@eldruin eldruin merged commit b56fd25 into serialport:main Jan 10, 2025
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants