First cut

This commit is contained in:
Kyle Lemons
2012-03-25 04:11:01 -07:00
commit 804a3c6ab8
11 changed files with 17791 additions and 0 deletions

194
usb/descriptor.go Normal file
View File

@@ -0,0 +1,194 @@
package usb
// #cgo LDFLAGS: -lusb-1.0
// #include <libusb-1.0/libusb.h>
import "C"
type Descriptor struct {
desc *C.struct_libusb_device_descriptor
Type DescriptorType // The type of this descriptor
Spec BCD // USB Specification Release Number
Class Class // The class of this device
SubClass uint8 // The sub-class (within the class) of this device
Protocol uint8 // The protocol (within the sub-class) of this device
Vendor ID // The 8-bit Vendor identifer
Product ID // The 8-bit Product identifier
Device BCD // The device version
Configs int // Number of configurations
}
func newDescriptor(dev *C.libusb_device) (*Descriptor, error) {
var desc C.struct_libusb_device_descriptor
if errno := C.libusb_get_device_descriptor(dev, &desc); errno < 0 {
return nil, usbError(errno)
}
return &Descriptor{
desc: &desc,
Type: DescriptorType(desc.bDescriptorType),
Spec: BCD(desc.bcdUSB),
Class: Class(desc.bDeviceClass),
SubClass: uint8(desc.bDeviceSubClass),
Protocol: uint8(desc.bDeviceProtocol),
Vendor: ID(desc.idVendor),
Product: ID(desc.idProduct),
Device: BCD(desc.bcdDevice),
Configs: int(desc.bNumConfigurations),
}, nil
}
/*
func (d Descriptor) str(idx int) (string, error) {
str := [64]byte{}
n := C.libusb_get_string_descriptor_ascii(
d.dev.handle,
C.uint8_t(idx),
(*C.uchar)(unsafe.Pointer(&str)),
64,
)
if n < 0 {
return "", usbError(n)
}
return string(str[:n]), nil
}
*/
type Class int
const (
CLASS_PER_INTERFACE Class = C.LIBUSB_CLASS_PER_INTERFACE
CLASS_AUDIO Class = C.LIBUSB_CLASS_AUDIO
CLASS_COMM Class = C.LIBUSB_CLASS_COMM
CLASS_HID Class = C.LIBUSB_CLASS_HID
CLASS_PRINTER Class = C.LIBUSB_CLASS_PRINTER
CLASS_PTP Class = C.LIBUSB_CLASS_PTP
CLASS_MASS_STORAGE Class = C.LIBUSB_CLASS_MASS_STORAGE
CLASS_HUB Class = C.LIBUSB_CLASS_HUB
CLASS_DATA Class = C.LIBUSB_CLASS_DATA
CLASS_WIRELESS Class = C.LIBUSB_CLASS_WIRELESS
CLASS_APPLICATION Class = C.LIBUSB_CLASS_APPLICATION
CLASS_VENDOR_SPEC Class = C.LIBUSB_CLASS_VENDOR_SPEC
)
var classDescription = map[Class]string{
CLASS_PER_INTERFACE: "per-interface",
CLASS_AUDIO: "audio",
CLASS_COMM: "communications",
CLASS_HID: "human interface device",
CLASS_PRINTER: "printer dclass",
CLASS_PTP: "picture transfer protocol",
CLASS_MASS_STORAGE: "mass storage",
CLASS_HUB: "hub",
CLASS_DATA: "data",
CLASS_WIRELESS: "wireless",
CLASS_APPLICATION: "application",
CLASS_VENDOR_SPEC: "vendor-specific",
}
func (c Class) String() string {
return classDescription[c]
}
type DescriptorType int
const (
DT_DEVICE DescriptorType = C.LIBUSB_DT_DEVICE
DT_CONFIG DescriptorType = C.LIBUSB_DT_CONFIG
DT_STRING DescriptorType = C.LIBUSB_DT_STRING
DT_INTERFACE DescriptorType = C.LIBUSB_DT_INTERFACE
DT_ENDPOINT DescriptorType = C.LIBUSB_DT_ENDPOINT
DT_HID DescriptorType = C.LIBUSB_DT_HID
DT_REPORT DescriptorType = C.LIBUSB_DT_REPORT
DT_PHYSICAL DescriptorType = C.LIBUSB_DT_PHYSICAL
DT_HUB DescriptorType = C.LIBUSB_DT_HUB
)
var descriptorTypeDescription = map[DescriptorType]string{
DT_DEVICE: "device",
DT_CONFIG: "configuration",
DT_STRING: "string",
DT_INTERFACE: "interface",
DT_ENDPOINT: "endpoint",
DT_HID: "HID",
DT_REPORT: "HID report",
DT_PHYSICAL: "physical",
DT_HUB: "hub",
}
func (dt DescriptorType) String() string {
return descriptorTypeDescription[dt]
}
type EndpointDirection int
const (
ENDPOINT_IN EndpointDirection = C.LIBUSB_ENDPOINT_IN
ENDPOINT_OUT EndpointDirection = C.LIBUSB_ENDPOINT_OUT
)
var endpointDirectionDescription = map[EndpointDirection]string{
ENDPOINT_IN: "device-to-host",
ENDPOINT_OUT: "host-to-device",
}
func (ed EndpointDirection) String() string {
return endpointDirectionDescription[ed]
}
type TransferType int
const (
TRANSFER_TYPE_CONTROL TransferType = C.LIBUSB_TRANSFER_TYPE_CONTROL
TRANSFER_TYPE_ISOCHRONOUS TransferType = C.LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
TRANSFER_TYPE_BULK TransferType = C.LIBUSB_TRANSFER_TYPE_BULK
TRANSFER_TYPE_INTERRUPT TransferType = C.LIBUSB_TRANSFER_TYPE_INTERRUPT
)
var transferTypeDescription = map[TransferType]string{
TRANSFER_TYPE_CONTROL: "control",
TRANSFER_TYPE_ISOCHRONOUS: "isochronous",
TRANSFER_TYPE_BULK: "bulk",
TRANSFER_TYPE_INTERRUPT: "interrupt",
}
func (tt TransferType) String() string {
return transferTypeDescription[tt]
}
type IsoSyncType int
const (
ISO_SYNC_TYPE_NONE IsoSyncType = C.LIBUSB_ISO_SYNC_TYPE_NONE
ISO_SYNC_TYPE_ASYNC IsoSyncType = C.LIBUSB_ISO_SYNC_TYPE_ASYNC
ISO_SYNC_TYPE_ADAPTIVE IsoSyncType = C.LIBUSB_ISO_SYNC_TYPE_ADAPTIVE
ISO_SYNC_TYPE_SYNC IsoSyncType = C.LIBUSB_ISO_SYNC_TYPE_SYNC
)
var isoSyncTypeDescription = map[IsoSyncType]string{
ISO_SYNC_TYPE_NONE: "no synchronization",
ISO_SYNC_TYPE_ASYNC: "asynchronous",
ISO_SYNC_TYPE_ADAPTIVE: "adaptive",
ISO_SYNC_TYPE_SYNC: "synchronous",
}
func (ist IsoSyncType) String() string {
return isoSyncTypeDescription[ist]
}
type IsoUsageType int
const (
ISO_USAGE_TYPE_DATA IsoUsageType = C.LIBUSB_ISO_USAGE_TYPE_DATA
ISO_USAGE_TYPE_FEEDBACK IsoUsageType = C.LIBUSB_ISO_USAGE_TYPE_FEEDBACK
ISO_USAGE_TYPE_IMPLICIT IsoUsageType = C.LIBUSB_ISO_USAGE_TYPE_IMPLICIT
)
var isoUsageTypeDescription = map[IsoUsageType]string{
ISO_USAGE_TYPE_DATA: "data",
ISO_USAGE_TYPE_FEEDBACK: "feedback",
ISO_USAGE_TYPE_IMPLICIT: "implicit data",
}
func (iut IsoUsageType) String() string {
return isoUsageTypeDescription[iut]
}

90
usb/device.go Normal file
View File

@@ -0,0 +1,90 @@
package usb
// #cgo LDFLAGS: -lusb-1.0
// #include <libusb-1.0/libusb.h>
import "C"
import (
"log"
"runtime"
)
type DeviceInfo struct {
dev *C.libusb_device
}
func newDeviceInfo(dev *C.libusb_device) *DeviceInfo {
d := &DeviceInfo{
dev: dev,
}
// Reference this device
C.libusb_ref_device(dev)
// I still can't get this to be called
runtime.SetFinalizer(d, (*DeviceInfo).Close)
log.Printf("deviceInfo %p initialized", d.dev)
return d
}
// Get the assigned Bus Number for this USB device.
func (d *DeviceInfo) BusNumber() byte {
return byte(C.libusb_get_bus_number(d.dev))
}
// Get the assigned Bus Address for this USB device.
func (d *DeviceInfo) Address() byte {
return byte(C.libusb_get_device_address(d.dev))
}
// Get a descriptor for the device.
func (d *DeviceInfo) Descriptor() (*Descriptor, error) {
return newDescriptor(d.dev)
}
// Open the given device for I/O.
func (d *DeviceInfo) Open() (*Device, error) {
var handle *C.libusb_device_handle
if errno := C.libusb_open(d.dev, &handle); errno != 0 {
return nil, usbError(errno)
}
return newDevice(handle), nil
}
// Close decrements the reference count for the device in the libusb driver
// code. It should be called exactly once!
func (d *DeviceInfo) Close() error {
if d.dev != nil {
log.Printf("deviceInfo %p closed", d.dev)
C.libusb_unref_device(d.dev)
}
d.dev = nil
return nil
}
type Device struct {
handle *C.libusb_device_handle
}
func newDevice(handle *C.libusb_device_handle) *Device {
d := &Device{
handle: handle,
}
// :(
runtime.SetFinalizer(d, (*Device).Close)
log.Printf("device %p initialized", d.handle)
return d
}
func (d *Device) Close() error {
if d.handle != nil {
log.Printf("device %p closed", d.handle)
C.libusb_unref_device(d.handle)
}
d.handle = nil
return nil
}

45
usb/error.go Normal file
View File

@@ -0,0 +1,45 @@
package usb
// #cgo LDFLAGS: -lusb-1.0
// #include <libusb-1.0/libusb.h>
import "C"
type usbError C.int
func (e usbError) Error() string {
return "libusb: " + usbErrorString[e]
}
const (
SUCCESS usbError = C.LIBUSB_SUCCESS
ERROR_IO usbError = C.LIBUSB_ERROR_IO
ERROR_INVALID_PARAM usbError = C.LIBUSB_ERROR_INVALID_PARAM
ERROR_ACCESS usbError = C.LIBUSB_ERROR_ACCESS
ERROR_NO_DEVICE usbError = C.LIBUSB_ERROR_NO_DEVICE
ERROR_NOT_FOUND usbError = C.LIBUSB_ERROR_NOT_FOUND
ERROR_BUSY usbError = C.LIBUSB_ERROR_BUSY
ERROR_TIMEOUT usbError = C.LIBUSB_ERROR_TIMEOUT
ERROR_OVERFLOW usbError = C.LIBUSB_ERROR_OVERFLOW
ERROR_PIPE usbError = C.LIBUSB_ERROR_PIPE
ERROR_INTERRUPTED usbError = C.LIBUSB_ERROR_INTERRUPTED
ERROR_NO_MEM usbError = C.LIBUSB_ERROR_NO_MEM
ERROR_NOT_SUPPORTED usbError = C.LIBUSB_ERROR_NOT_SUPPORTED
ERROR_OTHER usbError = C.LIBUSB_ERROR_OTHER
)
var usbErrorString = map[usbError]string{
C.LIBUSB_SUCCESS: "success",
C.LIBUSB_ERROR_IO: "i/o error",
C.LIBUSB_ERROR_INVALID_PARAM: "invalid param",
C.LIBUSB_ERROR_ACCESS: "bad access",
C.LIBUSB_ERROR_NO_DEVICE: "no device",
C.LIBUSB_ERROR_NOT_FOUND: "not found",
C.LIBUSB_ERROR_BUSY: "device or resource busy",
C.LIBUSB_ERROR_TIMEOUT: "timeout",
C.LIBUSB_ERROR_OVERFLOW: "overflow",
C.LIBUSB_ERROR_PIPE: "pipe error",
C.LIBUSB_ERROR_INTERRUPTED: "interrupted",
C.LIBUSB_ERROR_NO_MEM: "out of memory",
C.LIBUSB_ERROR_NOT_SUPPORTED: "not supported",
C.LIBUSB_ERROR_OTHER: "unknown error",
}

33
usb/misc.go Normal file
View File

@@ -0,0 +1,33 @@
package usb
import (
"fmt"
)
type BCD uint16
const (
USB_2_0 BCD = 0x0200
USB_1_1 BCD = 0x0110
USB_1_0 BCD = 0x0100
)
func (d BCD) Int() (i int) {
ten := 1
for o := uint(0); o < 4; o++ {
n := ((0xF << (o*4)) & d) >> (o*4)
i += int(n) * ten
ten *= 10
}
return
}
func (d BCD) String() string {
return fmt.Sprintf("%02x.%02x", int(d >> 8), int(d & 0xFF))
}
type ID uint16
func (id ID) String() string {
return fmt.Sprintf("%04x", int(id))
}

24
usb/misc_test.go Normal file
View File

@@ -0,0 +1,24 @@
package usb
import (
"testing"
)
func TestBCD(t *testing.T) {
tests := []struct{
BCD
Int int
Str string
}{
{0x1234, 1234, "12.34"},
}
for _, test := range tests {
if got, want := test.BCD.Int(), test.Int; got != want {
t.Errorf("Int(%x) = %d, want %d", test.BCD, got, want)
}
if got, want := test.BCD.String(), test.Str; got != want {
t.Errorf("String(%x) = %q, want %q", test.BCD, got, want)
}
}
}

76
usb/usb.go Normal file
View File

@@ -0,0 +1,76 @@
package usb
// #cgo LDFLAGS: -lusb-1.0
// #include <libusb-1.0/libusb.h>
import "C"
import (
"log"
"reflect"
"runtime"
"unsafe"
)
type Context struct {
ctx *C.libusb_context
}
func (c *Context) Debug(level int) {
C.libusb_set_debug(c.ctx, C.int(level))
}
func NewContext() *Context {
c := new(Context)
log.Printf("gousb initialized")
if errno := C.libusb_init(&c.ctx); errno != 0 {
panic(usbError(errno))
}
// This doesn't seem to actually get called. Sigh.
runtime.SetFinalizer(c, (*Context).Close)
return c
}
// ListDevices calls each with each enumerated device. If the function returns
// true, the device is added to the list of *DeviceInfo to return. All of
// these must be Closed, even if an error is also returned.
func (c *Context) ListDevices(each func(bus, addr int, desc *Descriptor) bool) ([]*DeviceInfo, error) {
var list **C.libusb_device
cnt := C.libusb_get_device_list(c.ctx, &list)
if cnt < 0 {
return nil, usbError(cnt)
}
defer C.libusb_free_device_list(list, 1)
var slice []*C.libusb_device
*(*reflect.SliceHeader)(unsafe.Pointer(&slice)) = reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(list)),
Len: int(cnt),
Cap: int(cnt),
}
ret := []*DeviceInfo{}
for _, dev := range slice {
bus := int(C.libusb_get_bus_number(dev))
addr := int(C.libusb_get_device_address(dev))
desc, err := newDescriptor(dev)
if err != nil {
return ret, err
}
if each(bus, addr, desc) {
ret = append(ret, newDeviceInfo(dev))
}
}
return ret, nil
}
func (c *Context) Close() error {
if c.ctx != nil {
C.libusb_exit(c.ctx)
log.Printf("gousb finished")
}
c.ctx = nil
return nil
}

40
usb/usb_test.go Normal file
View File

@@ -0,0 +1,40 @@
package usb_test
import (
"testing"
. "github.com/kylelemons/gousb/usb"
"github.com/kylelemons/gousb/usbid"
)
func TestNoop(t *testing.T) {
c := NewContext()
defer c.Close()
c.Debug(3)
}
func TestEnum(t *testing.T) {
c := NewContext()
defer c.Close()
c.Debug(3)
devs, err := c.ListDevices(func(bus, addr int, desc *Descriptor) bool {
t.Logf("%03d:%03d %+v", bus, addr, desc)
if v, ok := usbid.Vendors[desc.Vendor]; ok {
if p, ok := v.Devices[desc.Product]; ok {
t.Logf(" - %s (%s) %s (%s)", v, desc.Vendor, p, desc.Product)
} else {
t.Logf(" - %s (%s) Unknown", v, desc.Vendor)
}
}
return false
})
defer func() {
for _, d := range devs {
d.Close()
}
}()
if err != nil {
t.Fatalf("list: %s", err)
}
}

41
usbid/load.go Normal file
View File

@@ -0,0 +1,41 @@
package usbid
import (
"net/http"
"strings"
"log"
"github.com/kylelemons/gousb/usb"
)
const (
LinuxUsbDotOrg = "http://www.linux-usb.org/usb.ids"
)
var Vendors map[usb.ID]*Vendor
func LoadFromURL(url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
ids, err := ParseIDs(resp.Body)
if err != nil {
return err
}
Vendors = ids
return nil
}
func init() {
ids, err := ParseIDs(strings.NewReader(usbIdListData))
if err != nil {
log.Printf("usbid: failed to parse: %s", err)
return
}
Vendors = ids
}

16998
usbid/load_data.go Normal file

File diff suppressed because it is too large Load Diff

162
usbid/parse.go Normal file
View File

@@ -0,0 +1,162 @@
package usbid
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"github.com/kylelemons/gousb/usb"
)
type Vendor struct {
Name string
Devices map[usb.ID]*Device
}
func (v Vendor) String() string {
return v.Name
}
// TODO(kevlar) s/device/
type Device struct {
Name string
Interfaces map[usb.ID]string
}
func (d Device) String() string {
return d.Name
}
/*
type Class struct {
Name string
SubClass map[usb.ID]*SubClass
}
type SubClass struct {
Name string
Protocol map[usb.ID]string
}
*/
func ParseIDs(r io.Reader) (map[usb.ID]*Vendor, error) {
vendors := make(map[usb.ID]*Vendor, 2800)
split := func(s string) (kind string, level int, id usb.ID, name string, err error) {
pieces := strings.SplitN(s, " ", 2)
if len(pieces) != 2 {
err = fmt.Errorf("malformatted line %q", s)
return
}
// Save the name
name = pieces[1]
// Parse out the level
for len(pieces[0]) > 0 && pieces[0][0] == '\t' {
level, pieces[0] = level+1, pieces[0][1:]
}
// Parse the first piece to see if it has a kind
first := strings.SplitN(pieces[0], " ", 2)
if len(first) == 2 {
kind, pieces[0] = first[0], first[1]
}
// Parse the ID
i, err := strconv.ParseUint(pieces[0], 16, 16)
if err != nil {
err = fmt.Errorf("malformatted id %q: %s", pieces[0], err)
return
}
id = usb.ID(i)
return
}
// Hold the interim values
var vendor *Vendor
var device *Device
parseVendor := func(level int, id usb.ID, name string) error {
switch level {
case 0:
vendor = &Vendor{
Name: name,
}
vendors[id] = vendor
case 1:
if vendor == nil {
return fmt.Errorf("product line without vendor line")
}
device = &Device{
Name: name,
}
if vendor.Devices == nil {
vendor.Devices = make(map[usb.ID]*Device)
}
vendor.Devices[id] = device
case 2:
if device == nil {
return fmt.Errorf("interface line without device line")
}
if device.Interfaces == nil {
device.Interfaces = make(map[usb.ID]string)
}
device.Interfaces[id] = name
default:
return fmt.Errorf("too many levels of nesting for vendor block")
}
return nil
}
// TODO(kevlar): Parse class information, etc
//var class *Class
//var subclass *SubClass
var kind string
lines := bufio.NewReaderSize(r, 512)
parseLines:
for lineno := 0; ; lineno++ {
b, isPrefix, err := lines.ReadLine()
switch {
case err == io.EOF:
break parseLines
case err != nil:
return nil, err
case isPrefix:
return nil, fmt.Errorf("line %d: line too long", lineno)
}
line := string(b)
if len(line) == 0 || line[0] == '#' {
continue
}
k, level, id, name, err := split(line)
if err != nil {
return nil, fmt.Errorf("line %d: %s", lineno, err)
}
if k != "" {
kind = k
}
switch kind {
case "": err = parseVendor(level, id, name)
}
if err != nil {
return nil, fmt.Errorf("line %d: %s", lineno, err)
}
}
return vendors, nil
}

88
usbid/parse_test.go Normal file
View File

@@ -0,0 +1,88 @@
package usbid
import (
"reflect"
"strings"
"testing"
"github.com/kylelemons/gousb/usb"
)
func TestParse(t *testing.T) {
tests := []struct {
Input string
Output map[usb.ID]*Vendor
}{{
Input: `
# Skip comment
abcd Vendor One
0123 Product One
0124 Product Two
efef Vendor Two
0aba Product
12 Interface One
24 Interface Two
0abb Product
12 Interface
C 00 (Defined at Interface level)
C 01 Audio
01 Control Device
02 Streaming
03 MIDI Streaming
C 02 Communications
01 Direct Line
02 Abstract (modem)
00 None
01 AT-commands (v.25ter)
02 AT-commands (PCCA101)
03 AT-commands (PCCA101 + wakeup)
04 AT-commands (GSM)
05 AT-commands (3G)
06 AT-commands (CDMA)
fe Defined by command set descriptor
ff Vendor Specific (MSFT RNDIS?)
`,
Output: map[usb.ID]*Vendor{
0xabcd: {
Name: "Vendor One",
Devices: map[usb.ID]*Device{
0x0123: {Name: "Product One"},
0x0124: {Name: "Product Two"},
},
},
0xefef: {
Name: "Vendor Two",
Devices: map[usb.ID]*Device{
0x0aba: {
Name: "Product",
Interfaces: map[usb.ID]string{
0x12: "Interface One",
0x24: "Interface Two",
},
},
0x0abb: {
Name: "Product",
Interfaces: map[usb.ID]string{
0x12: "Interface",
},
},
},
},
},
}}
for idx, test := range tests {
vendors, err := ParseIDs(strings.NewReader(test.Input))
if err != nil {
t.Errorf("%d. ParseIDs: %s", idx, err)
continue
}
if got, want := vendors, test.Output; !reflect.DeepEqual(got, want) {
t.Errorf("%d. Parse mismatch", idx)
t.Errorf(" - got: %v", idx, got)
t.Errorf(" - want: %v", idx, want)
continue
}
}
}