-
Notifications
You must be signed in to change notification settings - Fork 0
/
usbdev.go
150 lines (131 loc) · 3.23 KB
/
usbdev.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package ftdi
/*
#include <stdlib.h>
#include <ftdi.h>
#include <libusb.h>
#cgo pkg-config: libftdi1
//#cgo CFLAGS: -I/usr/local/include/libftdi1 -I/usr/include/libusb-1.0
//#cgo LDFLAGS: /usr/local/lib/libftdi1.a /usr/lib/x86_64-linux-gnu/libusb-1.0.a -ludev -pthread
*/
import "C"
import (
"runtime"
"unsafe"
)
type USBDev struct {
Manufacturer string
Description string
Serial string
d *C.struct_libusb_device
}
func (u *USBDev) unref() {
if u.d == nil {
panic("USBDev.unref on uninitialized device")
}
C.libusb_unref_device(u.d)
u.d = nil // Help GC.
}
func (u *USBDev) Close() {
runtime.SetFinalizer(u, nil)
u.unref()
}
// getStrings updates Manufacturer, Description, Serial strings descriptors
// in unicode form. It doesn't use ftdi_usb_get_strings because libftdi
// converts unicode strings to ASCII.
func (u *USBDev) getStrings(dev *C.libusb_device, ds *C.struct_libusb_device_descriptor) error {
var dh *C.libusb_device_handle
if e := C.libusb_open(dev, &dh); e != 0 {
return USBError(e)
}
defer C.libusb_close(dh)
langid, err := getLangId(dh)
if err != nil {
return err
}
if u.Manufacturer, err = getStringDescriptor(dh, ds.iManufacturer, langid); err != nil {
return err
}
if u.Description, err = getStringDescriptor(dh, ds.iProduct, langid); err != nil {
return err
}
if u.Serial, err = getStringDescriptor(dh, ds.iSerialNumber, langid); err != nil {
return err
}
return nil
}
// FindAll search for all USB devices with specified vendor and product id.
// It returns slice od found devices.
func FindAll(vendor, product int) ([]*USBDev, error) {
if e := C.libusb_init(nil); e != 0 {
return nil, USBError(e)
}
var dptr **C.struct_libusb_device
if e := C.libusb_get_device_list(nil, &dptr); e < 0 {
return nil, USBError(e)
}
defer C.libusb_free_device_list(dptr, 1)
devs := (*[1 << 28]*C.libusb_device)(unsafe.Pointer(dptr))
var n int
for i, dev := range devs[:] {
if dev == nil {
n = i
break
}
}
descr := make([]*C.struct_libusb_device_descriptor, n)
for i, dev := range devs[:n] {
var ds C.struct_libusb_device_descriptor
if e := C.libusb_get_device_descriptor(dev, &ds); e < 0 {
return nil, USBError(e)
}
if int(ds.idVendor) == vendor && int(ds.idProduct) == product {
descr[i] = &ds
continue
}
if vendor == 0 && product == 0 && ds.idVendor == 0x403 {
switch ds.idProduct {
case 0x6001, 0x6010, 0x6011, 0x6014, 0x6015:
descr[i] = &ds
continue
}
}
n--
}
if n == 0 {
return nil, nil
}
ret := make([]*USBDev, n)
n = 0
for i, ds := range descr {
if ds == nil {
continue
}
u := new(USBDev)
u.d = devs[i]
C.libusb_ref_device(u.d)
runtime.SetFinalizer(u, (*USBDev).unref)
if err := u.getStrings(u.d, ds); err != nil {
return nil, err
}
ret[n] = u
n++
}
return ret, nil
}
// OpenUSBDev opens channel (interface) c of USB device u.
// u must be FTDI device.
func OpenUSBDev(u *USBDev, c Channel) (*Device, error) {
d, err := makeDevice(c)
if err != nil {
return nil, err
}
if e := C.ftdi_usb_open_dev(d.ctx, u.d); e < 0 {
defer d.free()
return nil, d.makeError(e)
}
runtime.SetFinalizer(d, (*Device).close)
return d, nil
}
func (u *USBDev) Open(c Channel) (*Device, error) {
return OpenUSBDev(u, c)
}