usb

package module
v0.0.0-...-69aee45 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2014 License: BSD-3-Clause Imports: 4 Imported by: 6

README

These are CGO bindings for libusb, created and tested on Linux.

They used to be part of go-mtpfs, a FUSE filesystem for mounting MTP devices on Linux.

DISCLAIMER

This is not an official Google product.

Documentation

Index

Constants

View Source
const CLASS_APPLICATION = 0xfe
View Source
const CLASS_AUDIO = 1
View Source
const CLASS_COMM = 2
View Source
const CLASS_CONTENT_SECURITY = 0x0d
View Source
const CLASS_DATA = 10
View Source
const CLASS_DIAGNOSTIC_DEVICE = 0xdc
View Source
const CLASS_HID = 3
View Source
const CLASS_HUB = 9
View Source
const CLASS_IMAGE = 6
View Source
const CLASS_MASS_STORAGE = 8
View Source
const CLASS_PERSONAL_HEALTHCARE = 0x0f
View Source
const CLASS_PER_INTERFACE = 0

Device and/or interface class codes.

View Source
const CLASS_PHYSICAL = 5
View Source
const CLASS_PRINTER = 7
View Source
const CLASS_SMART_CARD = 0x0b
View Source
const CLASS_VENDOR_SPEC = 0xff
View Source
const CLASS_VIDEO = 0x0e
View Source
const CLASS_WIRELESS = 0xe0
View Source
const DT_CONFIG = 0x02
View Source
const DT_DEVICE = 0x01

Descriptor types as defined by the USB specification.

View Source
const DT_ENDPOINT = 0x05
View Source
const DT_HID = 0x21
View Source
const DT_HUB = 0x29
View Source
const DT_INTERFACE = 0x04
View Source
const DT_PHYSICAL = 0x23
View Source
const DT_REPORT = 0x22
View Source
const DT_STRING = 0x03
View Source
const ENDPOINT_IN = 0x80

in: device-to-host

View Source
const ENDPOINT_OUT = 0x00

out: host-to-device

View Source
const ERROR_ACCESS = Error(-3)
View Source
const ERROR_BUSY = Error(-6)
View Source
const ERROR_INTERRUPTED = Error(-10)
View Source
const ERROR_INVALID_PARAM = Error(-2)
View Source
const ERROR_IO = Error(-1)
View Source
const ERROR_NOT_FOUND = Error(-5)
View Source
const ERROR_NOT_SUPPORTED = Error(-12)
View Source
const ERROR_NO_DEVICE = Error(-4)
View Source
const ERROR_NO_MEM = Error(-11)
View Source
const ERROR_OTHER = Error(-99)
View Source
const ERROR_OVERFLOW = Error(-8)
View Source
const ERROR_PIPE = Error(-9)
View Source
const ERROR_TIMEOUT = Error(-7)
View Source
const ISO_SYNC_TYPE_ADAPTIVE = 2
View Source
const ISO_SYNC_TYPE_ASYNC = 1
View Source
const ISO_SYNC_TYPE_NONE = 0

Synchronization types for isochronous endpoints, used in EndpointDescriptor.Attributes, bits 2:3.

View Source
const ISO_SYNC_TYPE_SYNC = 3
View Source
const ISO_USAGE_TYPE_DATA = 0

Usage types used in EndpointDescriptor.Attributes, bits 4:5.

View Source
const ISO_USAGE_TYPE_FEEDBACK = 1
View Source
const ISO_USAGE_TYPE_IMPLICIT = 2
View Source
const RECIPIENT_DEVICE = 0x00

Recipient bits for the reqType of ControlTransfer(). Values 4 - 31 are reserved.

View Source
const RECIPIENT_ENDPOINT = 0x02
View Source
const RECIPIENT_INTERFACE = 0x01
View Source
const RECIPIENT_OTHER = 0x03
View Source
const REQUEST_CLEAR_FEATURE = 0x01
View Source
const REQUEST_GET_CONFIGURATION = 0x08

Get the current device configuration value

View Source
const REQUEST_GET_DESCRIPTOR = 0x06

Get the specified descriptor

View Source
const REQUEST_GET_INTERFACE = 0x0A

Return the selected alternate setting for the specified interface.

View Source
const REQUEST_GET_STATUS = 0x00

Standard request types, as defined in table 9-3 of the USB2 specifications

View Source
const REQUEST_SET_ADDRESS = 0x05

Set device address for all future accesses

View Source
const REQUEST_SET_CONFIGURATION = 0x09

Set device configuration

View Source
const REQUEST_SET_DESCRIPTOR = 0x07

Used to update existing descriptors or add new descriptors

View Source
const REQUEST_SET_FEATURE = 0x03

Set or enable a specific feature

View Source
const REQUEST_SET_INTERFACE = 0x0B

Select an alternate interface for the specified interface

View Source
const REQUEST_SYNCH_FRAME = 0x0C

Set then report an endpoint's synchronization frame

View Source
const REQUEST_TYPE_CLASS = (0x01 << 5)
View Source
const REQUEST_TYPE_RESERVED = (0x03 << 5)
View Source
const REQUEST_TYPE_STANDARD = (0x00 << 5)

Request types to use in ControlTransfer().

View Source
const REQUEST_TYPE_VENDOR = (0x02 << 5)
View Source
const SPEED_FULL = C.LIBUSB_SPEED_FULL
View Source
const SPEED_HIGH = C.LIBUSB_SPEED_HIGH
View Source
const SPEED_LOW = C.LIBUSB_SPEED_LOW
View Source
const SPEED_SUPER = C.LIBUSB_SPEED_SUPER
View Source
const SPEED_UNKNOWN = C.LIBUSB_SPEED_UNKNOWN
View Source
const SUCCESS = Error(0)
View Source
const TRANSFER_CANCELLED = 3
View Source
const TRANSFER_COMPLETED = 0
View Source
const TRANSFER_ERROR = 1
View Source
const TRANSFER_FREE_BUFFER = 1 << 1
View Source
const TRANSFER_FREE_TRANSFER = 1 << 2
View Source
const TRANSFER_NO_DEVICE = 5
View Source
const TRANSFER_OVERFLOW = 6
View Source
const TRANSFER_SHORT_NOT_OK = 1 << 0
View Source
const TRANSFER_STALL = 4
View Source
const TRANSFER_TIMED_OUT = 2
View Source
const TRANSFER_TYPE_BULK = 2
View Source
const TRANSFER_TYPE_CONTROL = 0

Endpoint transfer types, for bits 0:1 of EndpointDescriptor.Attributes

View Source
const TRANSFER_TYPE_INTERRUPT = 3
View Source
const TRANSFER_TYPE_ISOCHRONOUS = 1

Variables

View Source
var CLASS_names = map[byte]string{
	0:    "PER_INTERFACE",
	1:    "AUDIO",
	2:    "COMM",
	3:    "HID",
	5:    "PHYSICAL",
	7:    "PRINTER",
	6:    "IMAGE",
	8:    "MASS_STORAGE",
	9:    "HUB",
	10:   "DATA",
	0x0b: "SMART_CARD",
	0x0d: "CONTENT_SECURITY",
	0x0e: "VIDEO",
	0x0f: "PERSONAL_HEALTHCARE",
	0xdc: "DIAGNOSTIC_DEVICE",
	0xe0: "WIRELESS",
	0xfe: "APPLICATION",
	0xff: "VENDOR_SPEC",
}

Device and/or interface class codes.

View Source
var SPEED_names = map[byte]string{
	byte(C.LIBUSB_SPEED_UNKNOWN): "UNKNOWN",
	byte(C.LIBUSB_SPEED_LOW):     "LOW",
	byte(C.LIBUSB_SPEED_FULL):    "FULL",
	byte(C.LIBUSB_SPEED_HIGH):    "HIGH",
	byte(C.LIBUSB_SPEED_SUPER):   "SUPER",
}

Functions

func ClassToStr

func ClassToStr(c byte) string

Types

type ConfigDescriptor

type ConfigDescriptor struct {
	// Size of this descriptor (in bytes)
	Length byte

	// Descriptor type. Will have value DT_CONFIG LIBUSB_DT_CONFIG
	// in this context.
	DescriptorType byte

	// Total length of data returned for this configuration
	TotalLength uint16

	// Identifier value for this configuration
	ConfigurationValue byte

	// Index of string descriptor describing this configuration
	ConfigurationIndex byte

	// Configuration characteristics
	Attributes byte

	// Maximum power consumption of the USB device from this bus in this
	// configuration when the device is fully opreation. Expressed in units
	// of 2 mA.
	MaxPower byte

	// Array of interfaces supported by this configuration.
	Interfaces []Interface

	// Extra descriptors. If libusb encounters unknown configuration
	// descriptors, it will store them here, should you wish to parse them.
	Extra []byte
}

type Context

type Context C.struct_libusb_context

func NewContext

func NewContext() *Context

func (*Context) Exit

func (c *Context) Exit()

func (*Context) GetDeviceList

func (c *Context) GetDeviceList() (DeviceList, error)

func (*Context) SetDebug

func (c *Context) SetDebug(level int)

type ControlSetup

type ControlSetup C.struct_libusb_control_setup

type Device

type Device C.struct_libusb_device

func (*Device) GetActiveConfigDescriptor

func (d *Device) GetActiveConfigDescriptor() (*ConfigDescriptor, error)

func (*Device) GetBusNumber

func (d *Device) GetBusNumber() uint8

Get the number of the bus that a device is connected to.

func (*Device) GetConfigDescriptor

func (d *Device) GetConfigDescriptor(config byte) (*ConfigDescriptor, error)

func (*Device) GetConfigDescriptorByValue

func (d *Device) GetConfigDescriptorByValue(value byte) (*ConfigDescriptor, error)

func (*Device) GetDeviceAddress

func (d *Device) GetDeviceAddress() uint8

Get the address of the device on the bus it is connected to.

func (*Device) GetDeviceDescriptor

func (d *Device) GetDeviceDescriptor() (*DeviceDescriptor, error)

func (*Device) GetDeviceSpeed

func (d *Device) GetDeviceSpeed() int

Get the negotiated connection speed for a device.

func (*Device) GetMaxIsoPacketSize

func (d *Device) GetMaxIsoPacketSize(endpoint byte) int

Calculate the maximum packet size which a specific endpoint is capable is sending or receiving in the duration of 1 microframe.

func (*Device) GetMaxPacketSize

func (d *Device) GetMaxPacketSize(endpoint byte) int

Convenience function to retrieve the MaxPacketSize value for a particular endpoint in the active device configuration.

func (*Device) Open

func (d *Device) Open() (*DeviceHandle, error)

Open a device and obtain a device handle.

func (*Device) Ref

func (d *Device) Ref() *Device

func (*Device) Unref

func (d *Device) Unref()

Decrement the reference count of a device.

type DeviceDescriptor

type DeviceDescriptor struct {
	// Size of this descriptor (in bytes)
	Length byte
	// Descriptor type.
	DescriptorType byte
	// USB specification release number in binary-coded decimal.
	USBRelease uint16

	// USB-IF class code for the device.
	DeviceClass byte
	// USB-IF subclass code for the device, qualified by the
	// DeviceClass value.
	DeviceSubClass byte
	// USB-IF protocol code for the device, qualified by the
	// DeviceClass and DeviceSubClass values.
	DeviceProtocol byte
	// Maximum packet size for endpoint 0.
	MaxPacketSize0 byte
	// USB-IF vendor ID.
	IdVendor uint16
	// USB-IF product ID.
	IdProduct uint16
	// Device release number in binary-coded decimal.
	Device uint16

	// Index of string descriptor describing manufacturer.
	Manufacturer byte
	// Index of string descriptor describing product.
	Product byte
	// Index of string descriptor containing device serial number.
	SerialNumber byte

	// Number of possible configurations.
	NumConfigurations byte
}

DeviceDescriptor is the standard USB device descriptor as documented in section 9.6.1 of the USB 2.0 specification.

type DeviceHandle

type DeviceHandle C.struct_libusb_device_handle

func (*DeviceHandle) AttachKernelDriver

func (h *DeviceHandle) AttachKernelDriver(ifaceNum byte) error

Re-attach an interface's kernel driver, which was previously detached using libusb_detach_kernel_driver().

func (*DeviceHandle) BulkTransfer

func (h *DeviceHandle) BulkTransfer(endpoint byte, data []byte, timeout int) (actual int, err error)

func (*DeviceHandle) ClaimInterface

func (h *DeviceHandle) ClaimInterface(num byte) error

Claim an interface on a given device handle.

func (*DeviceHandle) ClearHalt

func (h *DeviceHandle) ClearHalt(endpoint byte) error

Clear an halt/stall for a endpoint.

func (*DeviceHandle) Close

func (h *DeviceHandle) Close() error

Close a device handle.

func (*DeviceHandle) ControlTransfer

func (h *DeviceHandle) ControlTransfer(reqType, req byte, value, index uint16,
	data []byte, timeout int) error

func (*DeviceHandle) DetachKernelDriver

func (h *DeviceHandle) DetachKernelDriver(ifaceNum byte) error

Detach a kernel driver from an interface.

func (*DeviceHandle) Device

func (h *DeviceHandle) Device() *Device

Get the underlying device for a handle.

func (*DeviceHandle) GetConfiguration

func (h *DeviceHandle) GetConfiguration() (byte, error)

Determine the ConfigurationValue of the currently active configuration.

func (*DeviceHandle) GetStringDescriptorASCII

func (h *DeviceHandle) GetStringDescriptorASCII(descIndex byte) (string, error)

func (*DeviceHandle) InterruptTransfer

func (h *DeviceHandle) InterruptTransfer(endpoint byte, data []byte, timeout int) (actual int, err error)

func (*DeviceHandle) KernelDriverActive

func (h *DeviceHandle) KernelDriverActive(ifaceNum byte) (bool, error)

Determine if a kernel driver is active on an interface.

func (*DeviceHandle) ReleaseInterface

func (h *DeviceHandle) ReleaseInterface(num byte) error

Release an interface previously claimed with libusb_claim_interface().

func (*DeviceHandle) Reset

func (h *DeviceHandle) Reset() error

Perform a USB port reset to reinitialize a device.

func (*DeviceHandle) SetConfiguration

func (h *DeviceHandle) SetConfiguration(c byte) error

Set the active configuration for a device. The argument should be a ConfigurationValue, as given in the ConfigDescriptor.

func (*DeviceHandle) SetInterfaceAltSetting

func (h *DeviceHandle) SetInterfaceAltSetting(num int, alternate int) error

Activate an alternate setting for an interface.

type DeviceList

type DeviceList []*Device

func (DeviceList) Done

func (d DeviceList) Done()

type EndpointDescriptor

type EndpointDescriptor struct {
	// Size of this descriptor (in bytes)
	Length byte

	// Descriptor type. Will have value LIBUSB_DT_ENDPOINT in this
	// context.
	DescriptorType byte

	// The address of the endpoint described by this descriptor. Bits 0:3 are
	// the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction.
	EndpointAddress byte

	// Attributes which apply to the endpoint when it is configured using
	// the ConfigurationValue. Bits 0:1 determine the transfer type and
	// correspond to libusb_transfer_type. Bits 2:3 are only used for
	// isochronous endpoints and correspond to libusb_iso_sync_type.
	// Bits 4:5 are also only used for isochronous endpoints and correspond to
	// libusb_iso_usage_type. Bits 6:7 are reserved.
	Attributes byte

	// Maximum packet size this endpoint is capable of sending/receiving.
	MaxPacketSize uint16

	// Interval for polling endpoint for data transfers.
	Interval byte

	// For audio devices only: the rate at which synchronization feedback
	// is provided.
	Refresh byte

	// For audio devices only: the address if the synch endpoint
	SynchAddress byte

	// Extra descriptors. If libusb encounters unknown endpoint
	// descriptors, it will store them here, should you wish to
	// parse them.
	Extra []byte
}

EndpointDescriptor represents the standard USB endpoint descriptor. This descriptor is documented in section 9.6.3 of the USB 2.0 specification.

func (*EndpointDescriptor) Direction

func (e *EndpointDescriptor) Direction() byte

func (*EndpointDescriptor) Number

func (e *EndpointDescriptor) Number() byte

func (*EndpointDescriptor) String

func (e *EndpointDescriptor) String() string

func (*EndpointDescriptor) TransferType

func (e *EndpointDescriptor) TransferType() byte

type Error

type Error int

The error codes returned by libusb.

func (Error) Error

func (e Error) Error() string

type Interface

type Interface struct {
	AltSetting []InterfaceDescriptor
}

A collection of alternate settings for a USB interface.

type InterfaceDescriptor

type InterfaceDescriptor struct {
	// Size of this descriptor (in bytes)
	Length byte

	// Descriptor type. Will have value DT_INTERFACE
	// LIBUSB_DT_INTERFACE in this context.
	DescriptorType byte

	// Number of this interface
	InterfaceNumber byte

	// Value used to select this alternate setting for this interface
	AlternateSetting byte

	// USB-IF class code for this interface.
	InterfaceClass byte

	// USB-IF subclass code for this interface, qualified by the
	// InterfaceClass value
	InterfaceSubClass byte

	// USB-IF protocol code for this interface, qualified by the
	// InterfaceClass and InterfaceSubClass values
	InterfaceProtocol byte

	// Index of string descriptor describing this interface
	InterfaceStringIndex byte

	// Array of endpoint descriptors.
	EndPoints []EndpointDescriptor

	// Extra descriptors. If libusb encounters unknown interface
	// descriptors, it will store them here, should you wish to
	// parse them.
	Extra []byte
}

InterfaceDescriptor contains the standard USB interface descriptor, according to section 9.6.5 of the USB 2.0 specification.

type IsoPacketDescriptor

type IsoPacketDescriptor struct {
	// Length of data to request in this packet
	Length uint

	// Amount of data that was actually transferred
	ActualLength uint

	// Status code for this packet
	Status int
}

type Transfer

type Transfer C.struct_libusb_transfer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL