diff options
author | Nick Johnson <arachnid@notdot.net> | 2017-02-20 00:19:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-20 00:19:40 +0800 |
commit | e51f65af1f440aa4b221568e4a546455b7fe0964 (patch) | |
tree | 41a823398466c725f5072e052ef5fefe6326dcef /vendor/github.com/karalabe/gousb/usb | |
parent | 037c8b9ae9abe7b8739943838c91255abc2dd3b5 (diff) | |
parent | 6ec81352560e3e4268855115eda9d78f6e27875f (diff) | |
download | dexon-e51f65af1f440aa4b221568e4a546455b7fe0964.tar dexon-e51f65af1f440aa4b221568e4a546455b7fe0964.tar.gz dexon-e51f65af1f440aa4b221568e4a546455b7fe0964.tar.bz2 dexon-e51f65af1f440aa4b221568e4a546455b7fe0964.tar.lz dexon-e51f65af1f440aa4b221568e4a546455b7fe0964.tar.xz dexon-e51f65af1f440aa4b221568e4a546455b7fe0964.tar.zst dexon-e51f65af1f440aa4b221568e4a546455b7fe0964.zip |
Merge pull request #3681 from karalabe/usb-hidapi
accounts/usbwallet: swap karalabe/gousb to karalabe/hid
Diffstat (limited to 'vendor/github.com/karalabe/gousb/usb')
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/config.go | 153 | ||||
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/constants.go | 183 | ||||
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/debug.go | 36 | ||||
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/descriptor.go | 77 | ||||
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/device.go | 295 | ||||
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/endpoint.go | 100 | ||||
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/error.go | 92 | ||||
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/iso.c | 79 | ||||
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/iso.go | 148 | ||||
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/misc.go | 47 | ||||
-rw-r--r-- | vendor/github.com/karalabe/gousb/usb/usb.go | 178 |
11 files changed, 0 insertions, 1388 deletions
diff --git a/vendor/github.com/karalabe/gousb/usb/config.go b/vendor/github.com/karalabe/gousb/usb/config.go deleted file mode 100644 index 840bce63c..000000000 --- a/vendor/github.com/karalabe/gousb/usb/config.go +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright 2013 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package usb - -/* -#ifndef OS_WINDOWS - #include "os/threads_posix.h" -#endif -#include "libusbi.h" -#include "libusb.h" -*/ -import "C" - -import ( - "fmt" - "reflect" - "unsafe" -) - -type EndpointInfo struct { - Address uint8 - Attributes uint8 - MaxPacketSize uint16 - MaxIsoPacket uint32 - PollInterval uint8 - RefreshRate uint8 - SynchAddress uint8 -} - -func (e EndpointInfo) Number() int { - return int(e.Address) & ENDPOINT_NUM_MASK -} - -func (e EndpointInfo) Direction() EndpointDirection { - return EndpointDirection(e.Address) & ENDPOINT_DIR_MASK -} - -func (e EndpointInfo) String() string { - return fmt.Sprintf("Endpoint %d %-3s %s - %s %s [%d %d]", - e.Number(), e.Direction(), - TransferType(e.Attributes)&TRANSFER_TYPE_MASK, - IsoSyncType(e.Attributes)&ISO_SYNC_TYPE_MASK, - IsoUsageType(e.Attributes)&ISO_USAGE_TYPE_MASK, - e.MaxPacketSize, e.MaxIsoPacket, - ) -} - -type InterfaceInfo struct { - Number uint8 - Setups []InterfaceSetup -} - -func (i InterfaceInfo) String() string { - return fmt.Sprintf("Interface %02x (%d setups)", i.Number, len(i.Setups)) -} - -type InterfaceSetup struct { - Number uint8 - Alternate uint8 - IfClass uint8 - IfSubClass uint8 - IfProtocol uint8 - Endpoints []EndpointInfo -} - -func (a InterfaceSetup) String() string { - return fmt.Sprintf("Interface %02x Setup %02x", a.Number, a.Alternate) -} - -type ConfigInfo struct { - Config uint8 - Attributes uint8 - MaxPower uint8 - Interfaces []InterfaceInfo -} - -func (c ConfigInfo) String() string { - return fmt.Sprintf("Config %02x", c.Config) -} - -func newConfig(dev *C.libusb_device, cfg *C.struct_libusb_config_descriptor) ConfigInfo { - c := ConfigInfo{ - Config: uint8(cfg.bConfigurationValue), - Attributes: uint8(cfg.bmAttributes), - MaxPower: uint8(cfg.MaxPower), - } - - var ifaces []C.struct_libusb_interface - *(*reflect.SliceHeader)(unsafe.Pointer(&ifaces)) = reflect.SliceHeader{ - Data: uintptr(unsafe.Pointer(cfg._interface)), - Len: int(cfg.bNumInterfaces), - Cap: int(cfg.bNumInterfaces), - } - c.Interfaces = make([]InterfaceInfo, 0, len(ifaces)) - for _, iface := range ifaces { - if iface.num_altsetting == 0 { - continue - } - - var alts []C.struct_libusb_interface_descriptor - *(*reflect.SliceHeader)(unsafe.Pointer(&alts)) = reflect.SliceHeader{ - Data: uintptr(unsafe.Pointer(iface.altsetting)), - Len: int(iface.num_altsetting), - Cap: int(iface.num_altsetting), - } - descs := make([]InterfaceSetup, 0, len(alts)) - for _, alt := range alts { - i := InterfaceSetup{ - Number: uint8(alt.bInterfaceNumber), - Alternate: uint8(alt.bAlternateSetting), - IfClass: uint8(alt.bInterfaceClass), - IfSubClass: uint8(alt.bInterfaceSubClass), - IfProtocol: uint8(alt.bInterfaceProtocol), - } - var ends []C.struct_libusb_endpoint_descriptor - *(*reflect.SliceHeader)(unsafe.Pointer(&ends)) = reflect.SliceHeader{ - Data: uintptr(unsafe.Pointer(alt.endpoint)), - Len: int(alt.bNumEndpoints), - Cap: int(alt.bNumEndpoints), - } - i.Endpoints = make([]EndpointInfo, 0, len(ends)) - for _, end := range ends { - i.Endpoints = append(i.Endpoints, EndpointInfo{ - Address: uint8(end.bEndpointAddress), - Attributes: uint8(end.bmAttributes), - MaxPacketSize: uint16(end.wMaxPacketSize), - //MaxIsoPacket: uint32(C.libusb_get_max_iso_packet_size(dev, C.uchar(end.bEndpointAddress))), - PollInterval: uint8(end.bInterval), - RefreshRate: uint8(end.bRefresh), - SynchAddress: uint8(end.bSynchAddress), - }) - } - descs = append(descs, i) - } - c.Interfaces = append(c.Interfaces, InterfaceInfo{ - Number: descs[0].Number, - Setups: descs, - }) - } - return c -} diff --git a/vendor/github.com/karalabe/gousb/usb/constants.go b/vendor/github.com/karalabe/gousb/usb/constants.go deleted file mode 100644 index 7f0287d5c..000000000 --- a/vendor/github.com/karalabe/gousb/usb/constants.go +++ /dev/null @@ -1,183 +0,0 @@ -// Copyright 2013 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package usb - -// #include "libusb.h" -import "C" - -type Class uint8 - -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 uint8 - -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 uint8 - -const ( - ENDPOINT_NUM_MASK = 0x03 - ENDPOINT_DIR_IN EndpointDirection = C.LIBUSB_ENDPOINT_IN - ENDPOINT_DIR_OUT EndpointDirection = C.LIBUSB_ENDPOINT_OUT - ENDPOINT_DIR_MASK EndpointDirection = 0x80 -) - -var endpointDirectionDescription = map[EndpointDirection]string{ - ENDPOINT_DIR_IN: "IN", - ENDPOINT_DIR_OUT: "OUT", -} - -func (ed EndpointDirection) String() string { - return endpointDirectionDescription[ed] -} - -type TransferType uint8 - -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 - TRANSFER_TYPE_MASK TransferType = 0x03 -) - -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 uint8 - -const ( - ISO_SYNC_TYPE_NONE IsoSyncType = C.LIBUSB_ISO_SYNC_TYPE_NONE << 2 - ISO_SYNC_TYPE_ASYNC IsoSyncType = C.LIBUSB_ISO_SYNC_TYPE_ASYNC << 2 - ISO_SYNC_TYPE_ADAPTIVE IsoSyncType = C.LIBUSB_ISO_SYNC_TYPE_ADAPTIVE << 2 - ISO_SYNC_TYPE_SYNC IsoSyncType = C.LIBUSB_ISO_SYNC_TYPE_SYNC << 2 - ISO_SYNC_TYPE_MASK IsoSyncType = 0x0C -) - -var isoSyncTypeDescription = map[IsoSyncType]string{ - ISO_SYNC_TYPE_NONE: "unsynchronized", - 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 uint8 - -const ( - ISO_USAGE_TYPE_DATA IsoUsageType = C.LIBUSB_ISO_USAGE_TYPE_DATA << 4 - ISO_USAGE_TYPE_FEEDBACK IsoUsageType = C.LIBUSB_ISO_USAGE_TYPE_FEEDBACK << 4 - ISO_USAGE_TYPE_IMPLICIT IsoUsageType = C.LIBUSB_ISO_USAGE_TYPE_IMPLICIT << 4 - ISO_USAGE_TYPE_MASK IsoUsageType = 0x30 -) - -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] -} - -type RequestType uint8 - -const ( - REQUEST_TYPE_STANDARD = C.LIBUSB_REQUEST_TYPE_STANDARD - REQUEST_TYPE_CLASS = C.LIBUSB_REQUEST_TYPE_CLASS - REQUEST_TYPE_VENDOR = C.LIBUSB_REQUEST_TYPE_VENDOR - REQUEST_TYPE_RESERVED = C.LIBUSB_REQUEST_TYPE_RESERVED -) - -var requestTypeDescription = map[RequestType]string{ - REQUEST_TYPE_STANDARD: "standard", - REQUEST_TYPE_CLASS: "class", - REQUEST_TYPE_VENDOR: "vendor", - REQUEST_TYPE_RESERVED: "reserved", -} - -func (rt RequestType) String() string { - return requestTypeDescription[rt] -} diff --git a/vendor/github.com/karalabe/gousb/usb/debug.go b/vendor/github.com/karalabe/gousb/usb/debug.go deleted file mode 100644 index e7de8e522..000000000 --- a/vendor/github.com/karalabe/gousb/usb/debug.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2013 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package usb - -// To enable internal debugging: -// -ldflags "-X github.com/karalabe/gousb/usb.debugInternal true" - -import ( - "io" - "io/ioutil" - "log" // TODO(kevlar): make a logger - "os" -) - -var debug *log.Logger -var debugInternal string - -func init() { - var out io.Writer = ioutil.Discard - if debugInternal != "" { - out = os.Stderr - } - debug = log.New(out, "usb", log.LstdFlags|log.Lshortfile) -} diff --git a/vendor/github.com/karalabe/gousb/usb/descriptor.go b/vendor/github.com/karalabe/gousb/usb/descriptor.go deleted file mode 100644 index 2551dfc23..000000000 --- a/vendor/github.com/karalabe/gousb/usb/descriptor.go +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2013 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package usb - -/* -#ifndef OS_WINDOWS - #include "os/threads_posix.h" -#endif -#include "libusbi.h" -#include "libusb.h" -*/ -import "C" - -type Descriptor struct { - // Bus information - Bus uint8 // The bus on which the device was detected - Address uint8 // The address of the device on the bus - - // Version information - Spec BCD // USB Specification Release Number - Device BCD // The device version - - // Product information - Vendor ID // The Vendor identifer - Product ID // The Product identifier - - // Protocol information - Class uint8 // 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 - - // Configuration information - Configs []ConfigInfo -} - -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) - } - - // Enumerate configurations - var cfgs []ConfigInfo - for i := 0; i < int(desc.bNumConfigurations); i++ { - var cfg *C.struct_libusb_config_descriptor - if errno := C.libusb_get_config_descriptor(dev, C.uint8_t(i), &cfg); errno < 0 { - return nil, usbError(errno) - } - cfgs = append(cfgs, newConfig(dev, cfg)) - C.libusb_free_config_descriptor(cfg) - } - - return &Descriptor{ - Bus: uint8(C.libusb_get_bus_number(dev)), - Address: uint8(C.libusb_get_device_address(dev)), - Spec: BCD(desc.bcdUSB), - Device: BCD(desc.bcdDevice), - Vendor: ID(desc.idVendor), - Product: ID(desc.idProduct), - Class: uint8(desc.bDeviceClass), - SubClass: uint8(desc.bDeviceSubClass), - Protocol: uint8(desc.bDeviceProtocol), - Configs: cfgs, - }, nil -} diff --git a/vendor/github.com/karalabe/gousb/usb/device.go b/vendor/github.com/karalabe/gousb/usb/device.go deleted file mode 100644 index b4c4131fa..000000000 --- a/vendor/github.com/karalabe/gousb/usb/device.go +++ /dev/null @@ -1,295 +0,0 @@ -// Copyright 2013 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package usb - -/* -#ifndef OS_WINDOWS - #include "os/threads_posix.h" -#endif -#include "libusbi.h" -#include "libusb.h" -*/ -import "C" - -import ( - "fmt" - "reflect" - "sync" - "time" - "unsafe" -) - -var DefaultReadTimeout = 1 * time.Second -var DefaultWriteTimeout = 1 * time.Second -var DefaultControlTimeout = 250 * time.Millisecond //5 * time.Second - -type Device struct { - handle *C.libusb_device_handle - - // Embed the device information for easy access - *Descriptor - - // Timeouts - ReadTimeout time.Duration - WriteTimeout time.Duration - ControlTimeout time.Duration - - // Claimed interfaces - lock *sync.Mutex - claimed map[uint8]int - - // Detached kernel interfaces - detached map[uint8]int -} - -func newDevice(handle *C.libusb_device_handle, desc *Descriptor) (*Device, error) { - ifaces := 0 - d := &Device{ - handle: handle, - Descriptor: desc, - ReadTimeout: DefaultReadTimeout, - WriteTimeout: DefaultWriteTimeout, - ControlTimeout: DefaultControlTimeout, - lock: new(sync.Mutex), - claimed: make(map[uint8]int, ifaces), - detached: make(map[uint8]int), - } - - if err := d.detachKernelDriver(); err != nil { - d.Close() - return nil, err - } - - return d, nil -} - -// detachKernelDriver detaches any active kernel drivers, if supported by the platform. -// If there are any errors, like Context.ListDevices, only the final one will be returned. -func (d *Device) detachKernelDriver() (err error) { - for _, cfg := range d.Configs { - for _, iface := range cfg.Interfaces { - switch activeErr := C.libusb_kernel_driver_active(d.handle, C.int(iface.Number)); activeErr { - case C.LIBUSB_ERROR_NOT_SUPPORTED: - // no need to do any futher checking, no platform support - return - case 0: - continue - case 1: - switch detachErr := C.libusb_detach_kernel_driver(d.handle, C.int(iface.Number)); detachErr { - case C.LIBUSB_ERROR_NOT_SUPPORTED: - // shouldn't ever get here, should be caught by the outer switch - return - case 0: - d.detached[iface.Number]++ - case C.LIBUSB_ERROR_NOT_FOUND: - // this status is returned if libusb's driver is already attached to the device - d.detached[iface.Number]++ - default: - err = fmt.Errorf("usb: detach kernel driver: %s", usbError(detachErr)) - } - default: - err = fmt.Errorf("usb: active kernel driver check: %s", usbError(activeErr)) - } - } - } - - return -} - -// attachKernelDriver re-attaches kernel drivers to any previously detached interfaces, if supported by the platform. -// If there are any errors, like Context.ListDevices, only the final one will be returned. -func (d *Device) attachKernelDriver() (err error) { - for iface := range d.detached { - switch attachErr := C.libusb_attach_kernel_driver(d.handle, C.int(iface)); attachErr { - case C.LIBUSB_ERROR_NOT_SUPPORTED: - // no need to do any futher checking, no platform support - return - case 0: - continue - default: - err = fmt.Errorf("usb: attach kernel driver: %s", usbError(attachErr)) - } - } - - return -} - -func (d *Device) Reset() error { - if errno := C.libusb_reset_device(d.handle); errno != 0 { - return usbError(errno) - } - return nil -} - -func (d *Device) Control(rType, request uint8, val, idx uint16, data []byte) (int, error) { - //log.Printf("control xfer: %d:%d/%d:%d %x", idx, rType, request, val, string(data)) - dataSlice := (*reflect.SliceHeader)(unsafe.Pointer(&data)) - n := C.libusb_control_transfer( - d.handle, - C.uint8_t(rType), - C.uint8_t(request), - C.uint16_t(val), - C.uint16_t(idx), - (*C.uchar)(unsafe.Pointer(dataSlice.Data)), - C.uint16_t(len(data)), - C.uint(d.ControlTimeout/time.Millisecond)) - if n < 0 { - return int(n), usbError(n) - } - return int(n), nil -} - -// ActiveConfig returns the config id (not the index) of the active configuration. -// This corresponds to the ConfigInfo.Config field. -func (d *Device) ActiveConfig() (uint8, error) { - var cfg C.int - if errno := C.libusb_get_configuration(d.handle, &cfg); errno < 0 { - return 0, usbError(errno) - } - return uint8(cfg), nil -} - -// SetConfig attempts to change the active configuration. -// The cfg provided is the config id (not the index) of the configuration to set, -// which corresponds to the ConfigInfo.Config field. -func (d *Device) SetConfig(cfg uint8) error { - if errno := C.libusb_set_configuration(d.handle, C.int(cfg)); errno < 0 { - return usbError(errno) - } - return nil -} - -// Close the device. -func (d *Device) Close() error { - if d.handle == nil { - return fmt.Errorf("usb: double close on device") - } - d.lock.Lock() - defer d.lock.Unlock() - for iface := range d.claimed { - C.libusb_release_interface(d.handle, C.int(iface)) - } - d.attachKernelDriver() - C.libusb_close(d.handle) - d.handle = nil - return nil -} - -func (d *Device) OpenEndpoint(conf, iface, setup, epoint uint8) (Endpoint, error) { - end := &endpoint{ - Device: d, - } - - var setAlternate bool - for _, c := range d.Configs { - if c.Config != conf { - continue - } - debug.Printf("found conf: %#v\n", c) - for _, i := range c.Interfaces { - if i.Number != iface { - continue - } - debug.Printf("found iface: %#v\n", i) - for i, s := range i.Setups { - if s.Alternate != setup { - continue - } - setAlternate = i != 0 - - debug.Printf("found setup: %#v [default: %v]\n", s, !setAlternate) - for _, e := range s.Endpoints { - debug.Printf("ep %02x search: %#v\n", epoint, s) - if e.Address != epoint { - continue - } - end.InterfaceSetup = s - end.EndpointInfo = e - switch tt := TransferType(e.Attributes) & TRANSFER_TYPE_MASK; tt { - case TRANSFER_TYPE_BULK: - end.xfer = bulk_xfer - case TRANSFER_TYPE_INTERRUPT: - end.xfer = interrupt_xfer - case TRANSFER_TYPE_ISOCHRONOUS: - end.xfer = isochronous_xfer - default: - return nil, fmt.Errorf("usb: %s transfer is unsupported", tt) - } - goto found - } - return nil, fmt.Errorf("usb: unknown endpoint %02x", epoint) - } - return nil, fmt.Errorf("usb: unknown setup %02x", setup) - } - return nil, fmt.Errorf("usb: unknown interface %02x", iface) - } - return nil, fmt.Errorf("usb: unknown configuration %02x", conf) - -found: - - // Set the configuration - var activeConf C.int - if errno := C.libusb_get_configuration(d.handle, &activeConf); errno < 0 { - return nil, fmt.Errorf("usb: getcfg: %s", usbError(errno)) - } - if int(activeConf) != int(conf) { - if errno := C.libusb_set_configuration(d.handle, C.int(conf)); errno < 0 { - return nil, fmt.Errorf("usb: setcfg: %s", usbError(errno)) - } - } - - // Claim the interface - if errno := C.libusb_claim_interface(d.handle, C.int(iface)); errno < 0 { - return nil, fmt.Errorf("usb: claim: %s", usbError(errno)) - } - - // Increment the claim count - d.lock.Lock() - d.claimed[iface]++ - d.lock.Unlock() // unlock immediately because the next calls may block - - // Choose the alternate - if setAlternate { - if errno := C.libusb_set_interface_alt_setting(d.handle, C.int(iface), C.int(setup)); errno < 0 { - debug.Printf("altsetting error: %s", usbError(errno)) - return nil, fmt.Errorf("usb: setalt: %s", usbError(errno)) - } - } - - return end, nil -} - -func (d *Device) GetStringDescriptor(desc_index int) (string, error) { - - // allocate 200-byte array limited the length of string descriptor - goBuffer := make([]byte, 200) - - // get string descriptor from libusb. if errno < 0 then there are any errors. - // if errno >= 0; it is a length of result string descriptor - errno := C.libusb_get_string_descriptor_ascii( - d.handle, - C.uint8_t(desc_index), - (*C.uchar)(unsafe.Pointer(&goBuffer[0])), - 200) - - // if any errors occur - if errno < 0 { - return "", fmt.Errorf("usb: getstr: %s", usbError(errno)) - } - // convert slice of byte to string with limited length from errno - stringDescriptor := string(goBuffer[:errno]) - - return stringDescriptor, nil -} diff --git a/vendor/github.com/karalabe/gousb/usb/endpoint.go b/vendor/github.com/karalabe/gousb/usb/endpoint.go deleted file mode 100644 index 12b4ccf95..000000000 --- a/vendor/github.com/karalabe/gousb/usb/endpoint.go +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright 2013 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package usb - -// #include "libusb.h" -import "C" - -import ( - "fmt" - "reflect" - "time" - "unsafe" -) - -type Endpoint interface { - Read(b []byte) (int, error) - Write(b []byte) (int, error) - Interface() InterfaceSetup - Info() EndpointInfo -} - -type endpoint struct { - *Device - InterfaceSetup - EndpointInfo - xfer func(*endpoint, []byte, time.Duration) (int, error) -} - -func (e *endpoint) Read(buf []byte) (int, error) { - if EndpointDirection(e.Address)&ENDPOINT_DIR_MASK != ENDPOINT_DIR_IN { - return 0, fmt.Errorf("usb: read: not an IN endpoint") - } - - return e.xfer(e, buf, e.ReadTimeout) -} - -func (e *endpoint) Write(buf []byte) (int, error) { - if EndpointDirection(e.Address)&ENDPOINT_DIR_MASK != ENDPOINT_DIR_OUT { - return 0, fmt.Errorf("usb: write: not an OUT endpoint") - } - - return e.xfer(e, buf, e.WriteTimeout) -} - -func (e *endpoint) Interface() InterfaceSetup { return e.InterfaceSetup } -func (e *endpoint) Info() EndpointInfo { return e.EndpointInfo } - -// TODO(kevlar): (*Endpoint).Close - -func bulk_xfer(e *endpoint, buf []byte, timeout time.Duration) (int, error) { - if len(buf) == 0 { - return 0, nil - } - - data := (*reflect.SliceHeader)(unsafe.Pointer(&buf)).Data - - var cnt C.int - if errno := C.libusb_bulk_transfer( - e.handle, - C.uchar(e.Address), - (*C.uchar)(unsafe.Pointer(data)), - C.int(len(buf)), - &cnt, - C.uint(timeout/time.Millisecond)); errno < 0 { - return 0, usbError(errno) - } - return int(cnt), nil -} - -func interrupt_xfer(e *endpoint, buf []byte, timeout time.Duration) (int, error) { - if len(buf) == 0 { - return 0, nil - } - - data := (*reflect.SliceHeader)(unsafe.Pointer(&buf)).Data - - var cnt C.int - if errno := C.libusb_interrupt_transfer( - e.handle, - C.uchar(e.Address), - (*C.uchar)(unsafe.Pointer(data)), - C.int(len(buf)), - &cnt, - C.uint(timeout/time.Millisecond)); errno < 0 { - return 0, usbError(errno) - } - return int(cnt), nil -} diff --git a/vendor/github.com/karalabe/gousb/usb/error.go b/vendor/github.com/karalabe/gousb/usb/error.go deleted file mode 100644 index 30d47bce3..000000000 --- a/vendor/github.com/karalabe/gousb/usb/error.go +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright 2013 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package usb - -import ( - "fmt" -) - -// #include "libusb.h" -import "C" - -type usbError C.int - -func (e usbError) Error() string { - return fmt.Sprintf("libusb: %s [code %d]", usbErrorString[e], int(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", -} - -type TransferStatus uint8 - -const ( - LIBUSB_TRANSFER_COMPLETED TransferStatus = C.LIBUSB_TRANSFER_COMPLETED - LIBUSB_TRANSFER_ERROR TransferStatus = C.LIBUSB_TRANSFER_ERROR - LIBUSB_TRANSFER_TIMED_OUT TransferStatus = C.LIBUSB_TRANSFER_TIMED_OUT - LIBUSB_TRANSFER_CANCELLED TransferStatus = C.LIBUSB_TRANSFER_CANCELLED - LIBUSB_TRANSFER_STALL TransferStatus = C.LIBUSB_TRANSFER_STALL - LIBUSB_TRANSFER_NO_DEVICE TransferStatus = C.LIBUSB_TRANSFER_NO_DEVICE - LIBUSB_TRANSFER_OVERFLOW TransferStatus = C.LIBUSB_TRANSFER_OVERFLOW -) - -var transferStatusDescription = map[TransferStatus]string{ - LIBUSB_TRANSFER_COMPLETED: "transfer completed without error", - LIBUSB_TRANSFER_ERROR: "transfer failed", - LIBUSB_TRANSFER_TIMED_OUT: "transfer timed out", - LIBUSB_TRANSFER_CANCELLED: "transfer was cancelled", - LIBUSB_TRANSFER_STALL: "halt condition detected (endpoint stalled) or control request not supported", - LIBUSB_TRANSFER_NO_DEVICE: "device was disconnected", - LIBUSB_TRANSFER_OVERFLOW: "device sent more data than requested", -} - -func (ts TransferStatus) String() string { - return transferStatusDescription[ts] -} - -func (ts TransferStatus) Error() string { - return "libusb: " + ts.String() -} diff --git a/vendor/github.com/karalabe/gousb/usb/iso.c b/vendor/github.com/karalabe/gousb/usb/iso.c deleted file mode 100644 index 054486318..000000000 --- a/vendor/github.com/karalabe/gousb/usb/iso.c +++ /dev/null @@ -1,79 +0,0 @@ -#include "libusb.h" -#include <stdio.h> -#include <string.h> - -void print_xfer(struct libusb_transfer *xfer); -void iso_callback(void *); - -void callback(struct libusb_transfer *xfer) { - //printf("Callback!\n"); - //print_xfer(xfer); - iso_callback(xfer->user_data); -} - -int submit(struct libusb_transfer *xfer) { - xfer->callback = &callback; - xfer->status = -1; - //print_xfer(xfer); - //printf("Transfer submitted\n"); - - /* fake - strcpy(xfer->buffer, "hello"); - xfer->actual_length = 5; - callback(xfer); - return 0; */ - return libusb_submit_transfer(xfer); -} - -void print_xfer(struct libusb_transfer *xfer) { - int i; - - printf("Transfer:\n"); - printf(" dev_handle: %p\n", xfer->dev_handle); - printf(" flags: %08x\n", xfer->flags); - printf(" endpoint: %x\n", xfer->endpoint); - printf(" type: %x\n", xfer->type); - printf(" timeout: %dms\n", xfer->timeout); - printf(" status: %x\n", xfer->status); - printf(" length: %d (act: %d)\n", xfer->length, xfer->actual_length); - printf(" callback: %p\n", xfer->callback); - printf(" user_data: %p\n", xfer->user_data); - printf(" buffer: %p\n", xfer->buffer); - printf(" num_iso_pkts: %d\n", xfer->num_iso_packets); - printf(" packets:\n"); - for (i = 0; i < xfer->num_iso_packets; i++) { - printf(" [%04d] %d (act: %d) %x\n", i, - xfer->iso_packet_desc[i].length, - xfer->iso_packet_desc[i].actual_length, - xfer->iso_packet_desc[i].status); - } -} - -int extract_data(struct libusb_transfer *xfer, void *raw, int max, unsigned char *status) { - int i; - int copied = 0; - unsigned char *in = xfer->buffer; - unsigned char *out = raw; - for (i = 0; i < xfer->num_iso_packets; i++) { - struct libusb_iso_packet_descriptor pkt = xfer->iso_packet_desc[i]; - - // Copy the data - int len = pkt.actual_length; - if (len > max) { - len = max; - } - memcpy(out, in, len); - copied += len; - - // Increment offsets - in += pkt.length; - out += len; - - // Extract first error - if (pkt.status == 0 || *status != 0) { - continue; - } - *status = pkt.status; - } - return copied; -} diff --git a/vendor/github.com/karalabe/gousb/usb/iso.go b/vendor/github.com/karalabe/gousb/usb/iso.go deleted file mode 100644 index 1ba694e0e..000000000 --- a/vendor/github.com/karalabe/gousb/usb/iso.go +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright 2013 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package usb - -/* -#include "libusb.h" - -int submit(struct libusb_transfer *xfer); -void print_xfer(struct libusb_transfer *xfer); -int extract_data(struct libusb_transfer *xfer, void *data, int max, unsigned char *status); -*/ -import "C" - -import ( - "fmt" - "log" - "time" - "unsafe" -) - -//export iso_callback -func iso_callback(cptr unsafe.Pointer) { - ch := *(*chan struct{})(cptr) - close(ch) -} - -func (end *endpoint) allocTransfer() *Transfer { - // Use libusb_get_max_iso_packet_size ? - const ( - iso_packets = 8 // 128 // 242 - packet_size = 2 * 960 // 1760 - ) - - xfer := C.libusb_alloc_transfer(C.int(iso_packets)) - if xfer == nil { - log.Printf("usb: transfer allocation failed?!") - return nil - } - - buf := make([]byte, iso_packets*packet_size) - done := make(chan struct{}, 1) - - xfer.dev_handle = end.Device.handle - xfer.endpoint = C.uchar(end.Address) - xfer._type = C.LIBUSB_TRANSFER_TYPE_ISOCHRONOUS - - xfer.buffer = (*C.uchar)((unsafe.Pointer)(&buf[0])) - xfer.length = C.int(len(buf)) - xfer.num_iso_packets = iso_packets - - C.libusb_set_iso_packet_lengths(xfer, packet_size) - /* - pkts := *(*[]C.struct_libusb_packet_descriptor)(unsafe.Pointer(&reflect.SliceHeader{ - Data: uintptr(unsafe.Pointer(&xfer.iso_packet_desc)), - Len: iso_packets, - Cap: iso_packets, - })) - */ - - t := &Transfer{ - xfer: xfer, - done: done, - buf: buf, - } - xfer.user_data = (unsafe.Pointer)(&t.done) - - return t -} - -type Transfer struct { - xfer *C.struct_libusb_transfer - pkts []*C.struct_libusb_packet_descriptor - done chan struct{} - buf []byte -} - -func (t *Transfer) Submit(timeout time.Duration) error { - //log.Printf("iso: submitting %#v", t.xfer) - t.xfer.timeout = C.uint(timeout / time.Millisecond) - if errno := C.submit(t.xfer); errno < 0 { - return usbError(errno) - } - return nil -} - -func (t *Transfer) Wait(b []byte) (n int, err error) { - select { - case <-time.After(10 * time.Second): - return 0, fmt.Errorf("wait timed out after 10s") - case <-t.done: - } - // Non-iso transfers: - //n = int(t.xfer.actual_length) - //copy(b, ((*[1 << 16]byte)(unsafe.Pointer(t.xfer.buffer)))[:n]) - - //C.print_xfer(t.xfer) - /* - buf, offset := ((*[1 << 16]byte)(unsafe.Pointer(t.xfer.buffer))), 0 - for i, pkt := range *t.pkts { - log.Printf("Type is %T", t.pkts) - n += copy(b[n:], buf[offset:][:pkt.actual_length]) - offset += pkt.Length - if pkt.status != 0 && err == nil { - err = error(TransferStatus(pkt.status)) - } - } - */ - var status uint8 - n = int(C.extract_data(t.xfer, unsafe.Pointer(&b[0]), C.int(len(b)), (*C.uchar)(unsafe.Pointer(&status)))) - if status != 0 { - err = TransferStatus(status) - } - return n, err -} - -func (t *Transfer) Close() error { - C.libusb_free_transfer(t.xfer) - return nil -} - -func isochronous_xfer(e *endpoint, buf []byte, timeout time.Duration) (int, error) { - t := e.allocTransfer() - defer t.Close() - - if err := t.Submit(timeout); err != nil { - log.Printf("iso: xfer failed to submit: %s", err) - return 0, err - } - - n, err := t.Wait(buf) - if err != nil { - log.Printf("iso: xfer failed: %s", err) - return 0, err - } - return n, err -} diff --git a/vendor/github.com/karalabe/gousb/usb/misc.go b/vendor/github.com/karalabe/gousb/usb/misc.go deleted file mode 100644 index 6e25431bf..000000000 --- a/vendor/github.com/karalabe/gousb/usb/misc.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2013 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -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)) -} diff --git a/vendor/github.com/karalabe/gousb/usb/usb.go b/vendor/github.com/karalabe/gousb/usb/usb.go deleted file mode 100644 index 366827e69..000000000 --- a/vendor/github.com/karalabe/gousb/usb/usb.go +++ /dev/null @@ -1,178 +0,0 @@ -// Copyright 2013 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Package usb provides a wrapper around libusb-1.0. -package usb - -/* -#cgo CFLAGS: -I../internal/libusb/libusb -#cgo CFLAGS: -DDEFAULT_VISIBILITY="" -#cgo linux CFLAGS: -DOS_LINUX -D_GNU_SOURCE -DPOLL_NFDS_TYPE=int -#cgo darwin CFLAGS: -DOS_DARWIN -DPOLL_NFDS_TYPE=int -#cgo darwin LDFLAGS: -framework CoreFoundation -framework IOKit -lobjc -#cgo openbsd CFLAGS: -DOS_OPENBSD -DPOLL_NFDS_TYPE=int -#cgo windows CFLAGS: -DOS_WINDOWS -DUSE_USBDK -DPOLL_NFDS_TYPE=int - -#if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD) - #include <sys/poll.h> - #include "os/threads_posix.c" - #include "os/poll_posix.c" -#elif defined(OS_WINDOWS) - #include "os/threads_windows.c" - #include "os/poll_windows.c" -#endif - -#ifdef OS_LINUX - #include "os/linux_usbfs.c" - #include "os/linux_netlink.c" -#elif OS_DARWIN - #include "os/darwin_usb.c" -#elif OS_OPENBSD - #include "os/openbsd_usb.c" -#elif OS_WINDOWS - #include "os/windows_nt_common.c" - #include "os/windows_usbdk.c" -#endif - -#include "core.c" -#include "descriptor.c" -#include "hotplug.c" -#include "io.c" -#include "strerror.c" -#include "sync.c" -*/ -import "C" - -import ( - "log" - "reflect" - "unsafe" -) - -type Context struct { - ctx *C.libusb_context - done chan struct{} -} - -func (c *Context) Debug(level int) { - C.libusb_set_debug(c.ctx, C.int(level)) -} - -func NewContext() (*Context, error) { - c := &Context{ - done: make(chan struct{}), - } - - if errno := C.libusb_init(&c.ctx); errno != 0 { - return nil, usbError(errno) - } - - go func() { - tv := C.struct_timeval{ - tv_sec: 0, - tv_usec: 100000, - } - for { - select { - case <-c.done: - return - default: - } - if errno := C.libusb_handle_events_timeout_completed(c.ctx, &tv, nil); errno < 0 { - log.Printf("handle_events: error: %s", usbError(errno)) - continue - } - //log.Printf("handle_events returned") - } - }() - - return c, nil -} - -// ListDevices calls each with each enumerated device. -// If the function returns true, the device is opened and a Device is returned if the operation succeeds. -// Every Device returned (whether an error is also returned or not) must be closed. -// If there are any errors enumerating the devices, -// the final one is returned along with any successfully opened devices. -func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, 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), - } - - var reterr error - var ret []*Device - for _, dev := range slice { - desc, err := newDescriptor(dev) - if err != nil { - reterr = err - continue - } - - if each(desc) { - var handle *C.libusb_device_handle - if errno := C.libusb_open(dev, &handle); errno != 0 { - reterr = usbError(errno) - continue - } - if dev, err := newDevice(handle, desc); err != nil { - reterr = err - } else { - ret = append(ret, dev) - } - } - } - return ret, reterr -} - -// OpenDeviceWithVidPid opens Device from specific VendorId and ProductId. -// If there are any errors, it'll returns at second value. -func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) { - - handle := C.libusb_open_device_with_vid_pid(c.ctx, (C.uint16_t)(vid), (C.uint16_t)(pid)) - if handle == nil { - return nil, ERROR_NOT_FOUND - } - - dev := C.libusb_get_device(handle) - if dev == nil { - return nil, ERROR_NO_DEVICE - } - - desc, err := newDescriptor(dev) - - // return an error from nil-handle and nil-device - if err != nil { - return nil, err - } - return newDevice(handle, desc) -} - -func (c *Context) Close() error { - close(c.done) - if c.ctx != nil { - C.libusb_exit(c.ctx) - } - c.ctx = nil - return nil -} |