-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblaster.go
95 lines (88 loc) · 2.08 KB
/
blaster.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
// Binary blaster reads/writes the EEPROM of the kinX’s CY7C65632 USB 2.0 hub.
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"time"
"github.com/google/gousb"
)
var (
write = flag.Bool("write",
false,
"Write the default config instead of reading and displaying the current config")
raw = flag.Bool("raw",
false,
"Print the raw EEPROM bytes to stdout instead of parsing")
)
const eepromRequest = 14
var defaultConfig = &config{
VID: 0x04b4,
PID: 0x6570,
Removable: 0x18,
Ports: 0x4,
MaxPower: 0xfa, // 500mA
Vendor: "stapelberg",
Product: "kinX hub v2018-02-11",
Serial: "00050034031B",
}
func logic() error {
usb := gousb.NewContext()
defer usb.Close()
dev, err := usb.OpenDeviceWithVIDPID(0x04b4, 0x6570)
if err != nil {
return err
}
log.Printf("device = %+v", dev)
if *write {
b, err := defaultConfig.Marshal()
if err != nil {
return err
}
log.Printf("writing EEPROM (takes about 3s)")
for wIndex := uint16(0); wIndex < 64; wIndex++ {
n, err := dev.Control(gousb.RequestTypeVendor, eepromRequest, 0, wIndex, b[wIndex*2:wIndex*2+2])
if err != nil {
return err
}
if got, want := n, 2; got != want {
return fmt.Errorf("protocol error: unexpected response length: got %d, want %d", got, want)
}
// Must not overwhelm the device by sending too quickly, otherwise
// writes will silently fail:
time.Sleep(10 * time.Millisecond)
}
} else {
eeprom := make([]byte, 128)
for wIndex := uint16(0); wIndex < 64; wIndex++ {
data := make([]byte, 2)
n, err := dev.Control(gousb.RequestTypeVendor|0x80, eepromRequest, 0, wIndex, data)
if err != nil {
return err
}
if got, want := n, 2; got != want {
return fmt.Errorf("protocol error: unexpected response length: got %d, want %d", got, want)
}
copy(eeprom[wIndex*2:], data)
}
if *raw {
io.Copy(os.Stdout, bytes.NewReader(eeprom))
return nil
}
cfg, err := parse(eeprom)
if err != nil {
return err
}
fmt.Println(cfg)
}
return nil
}
func main() {
flag.Parse()
if err := logic(); err != nil {
log.Fatal(err)
}
}