Skip to content

Add USB devices#424

Merged
jaypipes merged 2 commits intojaypipes:mainfrom
christoph-zededa:usb
Dec 1, 2025
Merged

Add USB devices#424
jaypipes merged 2 commits intojaypipes:mainfrom
christoph-zededa:usb

Conversation

@christoph-zededa
Copy link
Copy Markdown
Contributor

This PR adds USB devices to the list of devices.

The code basically iterates over /sys/bus/usb/devices, follows the symlinks and reads uevent, interface and product.

For the snapshot, the symlinks in /sys/bus/usb/devices are added as well as the aforementioned files.

@christoph-zededa christoph-zededa force-pushed the usb branch 3 times, most recently from 215b125 to 03aa3e7 Compare September 1, 2025 12:09
@christoph-zededa
Copy link
Copy Markdown
Contributor Author

Fixed the build errors for Mac OS X and the linting errors about missing error-return-checks.

@christoph-zededa christoph-zededa force-pushed the usb branch 2 times, most recently from 11861e0 to 67402b7 Compare September 1, 2025 16:59
@jaypipes
Copy link
Copy Markdown
Owner

jaypipes commented Sep 2, 2025

@christoph-zededa thanks so much for this submission! I'll try to do a full review later today or tomorrow :)

Copy link
Copy Markdown
Collaborator

@ffromani ffromani left a comment

Choose a reason for hiding this comment

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

many thanks for the submission. I think that a usb package will benefit the project.
I'm very wary to add more deps, we should rather head to remove them all.
This is especially true if we can get similar (or identical?) results using golang code and peeking around in /sys.
Other than that, the code looks reasonnable, but I think we can use a bit more tests.

At glance, API additions (user-facing APIs) looks good but I'll leave the final word to @jaypipes

Comment thread cmd/ghwc/commands/root.go Outdated
}
if err := showAccelerator(cmd, args); err != nil {
return err
fmt.Println("------------------------------")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

minor: we should avoid fixed size separators; either avoid them for now or automatically align to the longest line, this approach will likely require extra code and/or a 3rd party helper.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I forgot to remove it after debugging. Now it is gone.

Comment thread host.go
info.Baseboard.String(),
info.Product.String(),
info.PCI.String(),
info.USB.String(),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

unrelated: this scales pretty poorly and is too hard to extend, I'll file a PR to send an improvement ASAP

Comment thread pkg/linuxpath/path_linux.go Outdated
}

type Paths struct {
ChrootSys string
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this should probably be "SysRoot"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I renamed it to SysRoot

Comment thread pkg/snapshot/clonetree.go
Comment on lines 41 to 45
fileSpecs := ExpectedCloneStaticContent()
fileSpecs = append(fileSpecs, ExpectedCloneNetContent()...)
fileSpecs = append(fileSpecs, ExpectedCloneUSBContent()...)
fileSpecs = append(fileSpecs, ExpectedClonePCIContent()...)
fileSpecs = append(fileSpecs, ExpectedCloneGPUContent()...)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

likewise, this code probably deserves some cleanup, I'll take care in a different PR

Comment thread pkg/usb/usb_gousb.go Outdated
Comment on lines +19 to +21
/*
#cgo pkg-config: libusb-1.0
*/
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unless this provide critical functionalities we can't replace with our code, I think we should avoid this especially becase it requires C code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The idea behind this is to use libusb on Mac OS X and use sysfs directly on Linux.
Perhaps it is better that I remove the OS X implementation now and later create another PR so this can be discussed there?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ok, this is an important clarification which changes the perspective a bit. I would prefer this approach you outline in your last comment, but please don't do any change just yet, let's wait for @jaypipes 's review.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@christoph-zededa yes, let's separate out the MacOSX support from the Linux support please. That way we can do a clean PR review on the Linux support and get that merged while we decide on a path forward for Mac.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I removed it from this PR.

Comment thread pkg/usb/usb_stub.go Outdated
import (
"runtime"

"github.com/pkg/errors"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please use the native errors in new code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed.

@christoph-zededa christoph-zededa force-pushed the usb branch 2 times, most recently from f2e3a5f to 8031e2b Compare October 13, 2025 14:23
@europaul
Copy link
Copy Markdown

@jaypipes @ffromani could you give this PR another look? I think it might have been forgotten :)

@jaypipes
Copy link
Copy Markdown
Owner

@jaypipes @ffromani could you give this PR another look? I think it might have been forgotten :)

@europaul yes, sorry, was at Kubecon last week and behind on reviews. will get to this today or tomorrow! sorry for the delay!

Copy link
Copy Markdown
Owner

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

@christoph-zededa please pull the latest main and rebase this branch in order to bring in the latest go.mod and other changes. I've left some comments inline for you as well.

So sorry for the long delay in reviewing and thank you again for the submission!

Comment thread cmd/ghwc/commands/usb.go Outdated
"github.com/spf13/cobra"
)

// netCmd represents the install command
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suggested change
// netCmd represents the install command
// usbCmd represents the `usb` command

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread cmd/ghwc/commands/usb.go Outdated
RunE: showUSB,
}

// showNetwork show network information for the host system.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suggested change
// showNetwork show network information for the host system.
// showUSB shows USB information for the host system.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread pkg/usb/usb.go Outdated
"github.com/jaypipes/ghw/pkg/option"
)

type USB struct {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think this struct should be called Device.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I renamed it to Device.

Comment thread pkg/usb/usb.go

}

return str.String()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This would probably be easier to read like this:

	attrs := map[string]string{
		"driver": u.Driver,
		"type", u.Type,
		"vendor_id", u.VendorID,
		"product_id", u.ProductID,
		"product", u.Product,
		"revision_id": u.RevisionID,
		"interface": u.Interface,
	}
    return strings.Join(
        lo.MapToSlice(
            attrs,
            func (k, v string) string {
                return fmt.Sprintf("%s=%q", k, v)
            },
        ), " ",
    )

We use the samber/lo library for generic manipulations like the above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't see this library yet in go.mod, but I can add it.

One thing I see that with the map it is now unordered:
before:

christoph@ponyhof ~/p/g/c/ghwc (usb)> go build && ./ghwc usb
USB (19 USBs)
 driver=hub type=9/0/1 vendorID=1d6b productID=2 revisionID=617
 driver=hub type=9/0/3 vendorID=1d6b productID=3 revisionID=617
 driver=hub type=9/0/1 vendorID=1d6b productID=2 revisionID=617
 driver=usb type=224/1/1 vendorID=8087 productID=33 revisionID=0
 driver=btusb type=224/1/1 vendorID=8087 productID=33 revisionID=0
 driver=btusb type=224/1/1 vendorID=8087 productID=33 revisionID=0
 driver=usb type=255/16/255 vendorID=6cb productID=fc revisionID=0
 type=255/16/255 vendorID=6cb productID=fc revisionID=0
 driver=usb type=239/2/1 vendorID=174f productID=1813 product="Integrated Camera" revisionID=1015
 driver=uvcvideo type=239/2/1 vendorID=174f productID=1813 revisionID=1015 interface="Integrated Camera"
 driver=uvcvideo type=239/2/1 vendorID=174f productID=1813 revisionID=1015
 driver=uvcvideo type=239/2/1 vendorID=174f productID=1813 revisionID=1015 interface="Integrated IR Camera"
 driver=uvcvideo type=239/2/1 vendorID=174f productID=1813 revisionID=1015
 type=239/2/1 vendorID=174f productID=1813 revisionID=1015 interface="Camera DFU Device"
 driver=hub type=9/0/3 vendorID=1d6b productID=3 revisionID=617
 driver=usb type=9/0/1 vendorID=1d6b productID=2 product="xHCI Host Controller" revisionID=617
 driver=usb type=9/0/3 vendorID=1d6b productID=3 product="xHCI Host Controller" revisionID=617
 driver=usb type=9/0/1 vendorID=1d6b productID=2 product="xHCI Host Controller" revisionID=617
 driver=usb type=9/0/3 vendorID=1d6b productID=3 product="xHCI Host Controller" revisionID=617

after:

christoph@ponyhof ~/p/g/c/ghwc (usb)> go build && ./ghwc usb
USB (19 USBs)
 vendor_id="1d6b" product_id="2" product="" revision_id="617" interface="" driver="hub" type="9/0/1"
 interface="" driver="hub" type="9/0/3" vendor_id="1d6b" product_id="3" product="" revision_id="617"
 product="" revision_id="617" interface="" driver="hub" type="9/0/1" vendor_id="1d6b" product_id="2"
 product_id="33" product="" revision_id="0" interface="" driver="usb" type="224/1/1" vendor_id="8087"
 revision_id="0" interface="" driver="btusb" type="224/1/1" vendor_id="8087" product_id="33" product=""
 product="" revision_id="0" interface="" driver="btusb" type="224/1/1" vendor_id="8087" product_id="33"
 vendor_id="6cb" product_id="fc" product="" revision_id="0" interface="" driver="usb" type="255/16/255"
 revision_id="0" interface="" driver="" type="255/16/255" vendor_id="6cb" product_id="fc" product=""
 vendor_id="174f" product_id="1813" product="Integrated Camera" revision_id="1015" interface="" driver="usb" type="239/2/1"
 type="239/2/1" vendor_id="174f" product_id="1813" product="" revision_id="1015" interface="Integrated Camera" driver="uvcvideo"
 type="239/2/1" vendor_id="174f" product_id="1813" product="" revision_id="1015" interface="" driver="uvcvideo"
 driver="uvcvideo" type="239/2/1" vendor_id="174f" product_id="1813" product="" revision_id="1015" interface="Integrated IR Camera"
 type="239/2/1" vendor_id="174f" product_id="1813" product="" revision_id="1015" interface="" driver="uvcvideo"
 product_id="1813" product="" revision_id="1015" interface="Camera DFU Device" driver="" type="239/2/1" vendor_id="174f"
 driver="hub" type="9/0/3" vendor_id="1d6b" product_id="3" product="" revision_id="617" interface=""
 vendor_id="1d6b" product_id="2" product="xHCI Host Controller" revision_id="617" interface="" driver="usb" type="9/0/1"
 revision_id="617" interface="" driver="usb" type="9/0/3" vendor_id="1d6b" product_id="3" product="xHCI Host Controller"
 revision_id="617" interface="" driver="usb" type="9/0/1" vendor_id="1d6b" product_id="2" product="xHCI Host Controller"
 interface="" driver="usb" type="9/0/3" vendor_id="1d6b" product_id="3" product="xHCI Host Controller" revision_id="617"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

for now I have not included the change in the PR

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I don't see this library yet in go.mod, but I can add it.

Doh, you are totally correct @christoph-zededa! I've used samber/lo in so many of my other libraries I just assumed we were using it in ghw!

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@christoph-zededa good point about the ordering. OK, let's leave this alone then :)

Comment thread pkg/usb/usb_gousb.go Outdated
Comment on lines +19 to +21
/*
#cgo pkg-config: libusb-1.0
*/
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@christoph-zededa yes, let's separate out the MacOSX support from the Linux support please. That way we can do a clean PR review on the Linux support and get that merged while we decide on a path forward for Mac.

@christoph-zededa christoph-zededa force-pushed the usb branch 3 times, most recently from 51cebaf to e8bf258 Compare November 25, 2025 18:35
@christoph-zededa
Copy link
Copy Markdown
Contributor Author

I removed the Mac support.

instead of checking every return manually, use a for-loop

Signed-off-by: Christoph Ostarek <christoph@zededa.com>
Copy link
Copy Markdown
Owner

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

sorry @christoph-zededa noticed one thing, see inline comment.

Comment thread pkg/usb/usb.go Outdated
// Info describes all network interface controllers (NICs) in the host system.
type Info struct {
ctx *context.Context
USBs []*Device `json:"usbs"`
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Just noticed, this should be Devices not USBs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed.

Comment thread pkg/usb/usb.go Outdated
// Info describes all network interface controllers (NICs) in the host system.
type Info struct {
ctx *context.Context
Devices []*Device `json:"usbs"`
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suggested change
Devices []*Device `json:"usbs"`
Devices []*Device `json:"devices"`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Sorry, I am still in Monday mode ;-)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

LOL, no worries @christoph-zededa! Really glad you have been so patient with us in this review! :)

basically this code iterates over entries in `/sys/bus/usb/devices/`
and reads out information from the `uevent` file and some other
files

Signed-off-by: Christoph Ostarek <christoph@zededa.com>
Copy link
Copy Markdown
Owner

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

Thank you @christoph-zededa! :)

@jaypipes jaypipes merged commit 706bdae into jaypipes:main Dec 1, 2025
14 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.

4 participants