diff options
author | Péter Szilágyi <peterke@gmail.com> | 2019-06-01 22:19:47 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2019-06-04 23:04:55 +0800 |
commit | 5d68400cad8cda3b265d2bc9d45223b303349023 (patch) | |
tree | 49a7b41a221cfb71739bb1587c69e66176e34b91 /vendor/github.com/karalabe/usb/usb_enabled.go | |
parent | b4cc7b660cfaee01da625ed5f09ce2b9d34933ba (diff) | |
download | go-tangerine-5d68400cad8cda3b265d2bc9d45223b303349023.tar go-tangerine-5d68400cad8cda3b265d2bc9d45223b303349023.tar.gz go-tangerine-5d68400cad8cda3b265d2bc9d45223b303349023.tar.bz2 go-tangerine-5d68400cad8cda3b265d2bc9d45223b303349023.tar.lz go-tangerine-5d68400cad8cda3b265d2bc9d45223b303349023.tar.xz go-tangerine-5d68400cad8cda3b265d2bc9d45223b303349023.tar.zst go-tangerine-5d68400cad8cda3b265d2bc9d45223b303349023.zip |
accounts/usbwallet, vendor: switch from HID to generic USB lib
Diffstat (limited to 'vendor/github.com/karalabe/usb/usb_enabled.go')
-rw-r--r-- | vendor/github.com/karalabe/usb/usb_enabled.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/vendor/github.com/karalabe/usb/usb_enabled.go b/vendor/github.com/karalabe/usb/usb_enabled.go new file mode 100644 index 000000000..6ba37af7b --- /dev/null +++ b/vendor/github.com/karalabe/usb/usb_enabled.go @@ -0,0 +1,98 @@ +// usb - Self contained USB and HID library for Go +// Copyright 2019 The library Authors +// +// This library is free software: you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) any +// later version. +// +// The library is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +// A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License along +// with the library. If not, see <http://www.gnu.org/licenses/>. + +// +build freebsd,cgo linux,cgo darwin,!ios,cgo windows,cgo + +package usb + +import ( + "sync" +) + +// enumerateLock is a mutex serializing access to USB device enumeration needed +// by the macOS USB HID system calls, which require 2 consecutive method calls +// for enumeration, causing crashes if called concurrently. +// +// For more details, see: +// https://developer.apple.com/documentation/iokit/1438371-iohidmanagersetdevicematching +// > "subsequent calls will cause the hid manager to release previously enumerated devices" +var enumerateLock sync.Mutex + +// Supported returns whether this platform is supported by the USB library or not. +// The goal of this method is to allow programatically handling platforms that do +// not support USB and not having to fall back to build constraints. +func Supported() bool { + return true +} + +// Enumerate returns a list of all the USB devices attached to the system which +// match the vendor and product id: +// - If the vendor id is set to 0 then any vendor matches. +// - If the product id is set to 0 then any product matches. +// - If the vendor and product id are both 0, all devices are returned. +// +// For any device that is HID capable, the enumeration will return an interface +// to the HID endpoints. For pure raw USB access, please use EnumerateRaw. +func Enumerate(vendorID uint16, productID uint16) ([]DeviceInfo, error) { + enumerateLock.Lock() + defer enumerateLock.Unlock() + + // Enumerate all the raw USB devices and skip the HID ones + raws, err := enumerateRaw(vendorID, productID) + if err != nil { + return nil, err + } + // Enumerate all the HID USB devices + hids, err := enumerateHid(vendorID, productID) + if err != nil { + return nil, err + } + return append(raws, hids...), nil +} + +// EnumerateRaw returns a list of all the USB devices attached to the system which +// match the vendor and product id: +// - If the vendor id is set to 0 then any vendor matches. +// - If the product id is set to 0 then any product matches. +// - If the vendor and product id are both 0, all devices are returned. +func EnumerateRaw(vendorID uint16, productID uint16) ([]DeviceInfo, error) { + enumerateLock.Lock() + defer enumerateLock.Unlock() + + return enumerateRaw(vendorID, productID) +} + +// EnumerateHid returns a list of all the HID devices attached to the system which +// match the vendor and product id: +// - If the vendor id is set to 0 then any vendor matches. +// - If the product id is set to 0 then any product matches. +// - If the vendor and product id are both 0, all devices are returned. +func EnumerateHid(vendorID uint16, productID uint16) ([]DeviceInfo, error) { + enumerateLock.Lock() + defer enumerateLock.Unlock() + + return enumerateHid(vendorID, productID) +} + +// Open connects to a previsouly discovered USB device. +func (info DeviceInfo) Open() (Device, error) { + enumerateLock.Lock() + defer enumerateLock.Unlock() + + if info.rawDevice == nil { + return openHid(info) + } + return openRaw(info) +} |