70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package usb
|
|
|
|
// Device represents a USB device
|
|
type Device struct {
|
|
BusID string `json:"bus_id"` // e.g. "1-1.4"
|
|
BusNum uint32 `json:"bus_num"`
|
|
DevNum uint32 `json:"dev_num"`
|
|
Speed uint32 `json:"speed"`
|
|
VendorID uint16 `json:"vendor_id"`
|
|
ProductID uint16 `json:"product_id"`
|
|
BcdDevice uint16 `json:"bcd_device"`
|
|
DeviceClass uint8 `json:"device_class"`
|
|
DeviceSubClass uint8 `json:"device_sub_class"`
|
|
DeviceProtocol uint8 `json:"device_protocol"`
|
|
ConfigValue uint8 `json:"config_value"`
|
|
NumConfigs uint8 `json:"num_configs"`
|
|
Manufacturer string `json:"manufacturer"`
|
|
Product string `json:"product"`
|
|
Serial string `json:"serial"`
|
|
SysPath string `json:"sys_path"` // sysfs path
|
|
DevPath string `json:"dev_path"` // /dev/bus/usb path
|
|
Interfaces []Interface `json:"interfaces"`
|
|
}
|
|
|
|
// Interface represents a USB interface
|
|
type Interface struct {
|
|
Number uint8 `json:"number"`
|
|
Class uint8 `json:"class"`
|
|
SubClass uint8 `json:"sub_class"`
|
|
Protocol uint8 `json:"protocol"`
|
|
Driver string `json:"driver"`
|
|
Endpoints []Endpoint `json:"endpoints"`
|
|
}
|
|
|
|
// Endpoint represents a USB endpoint
|
|
type Endpoint struct {
|
|
Address uint8 `json:"address"` // bEndpointAddress (bit 7=direction, bits 3:0=number)
|
|
TransferType uint8 `json:"transfer_type"` // 0=control, 1=iso, 2=bulk, 3=interrupt
|
|
MaxPacketSize uint16 `json:"max_packet_size"`
|
|
}
|
|
|
|
// USB transfer types (from bmAttributes)
|
|
const (
|
|
TransferTypeControl = 0
|
|
TransferTypeIsochronous = 1
|
|
TransferTypeBulk = 2
|
|
TransferTypeInterrupt = 3
|
|
)
|
|
|
|
// DevID returns the USB/IP device ID (busnum << 16 | devnum)
|
|
func (d *Device) DevID() uint32 {
|
|
return (d.BusNum << 16) | d.DevNum
|
|
}
|
|
|
|
// IsHub returns true if this is a USB hub
|
|
func (d *Device) IsHub() bool {
|
|
return d.DeviceClass == 9
|
|
}
|
|
|
|
// DisplayName returns a human-readable device name
|
|
func (d *Device) DisplayName() string {
|
|
if d.Product != "" {
|
|
if d.Manufacturer != "" {
|
|
return d.Manufacturer + " " + d.Product
|
|
}
|
|
return d.Product
|
|
}
|
|
return "Unknown USB Device"
|
|
}
|