-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpowerusb.c
169 lines (141 loc) · 3.39 KB
/
powerusb.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <libusb-1.0/libusb.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define USB_VENDOR_ID 0x04d8
#define USB_PRODUCT_ID 0x003f
#define ENDPOINT_IN 0x81
#define ENDPOINT_OUT 0x01
#define CMD_GET_MODEL 0xaa
#define CMD_GET_FIRM_VER 0xa7
#define CMD_GET_STATE1 0xa1
#define CMD_GET_STATE2 0xa2
#define CMD_GET_STATE3 0xac
#define DEBUG
#ifdef DEBUG
#define Dprintf printf
#else
#define Dprintf(fmt, ...) while(0){}
#endif
libusb_context *ctx = NULL;
libusb_device *dev;
libusb_device **devs;
struct libusb_device_handle *devh = NULL;
uint8_t* send_cmd(struct libusb_device_handle *devh,int cmd)
{
int i;
uint8_t buf[64],ret[2];
int size=0;
Dprintf("send_cmd:%x\n",cmd);
memset(buf, 0xff, sizeof(buf));
buf[0] = cmd;
if((libusb_interrupt_transfer(devh,ENDPOINT_OUT,buf,
sizeof(buf),&size, 1000)) < 0 ) {
perror("libusb_interrupt_transfer");
exit(1);
}
memset(buf, 0x00, sizeof(buf));
if((libusb_interrupt_transfer(devh,ENDPOINT_IN,buf,
sizeof(buf),&size, 1000)) < 0 ) {
perror("libusb_interrupt_transfer");
exit(1);
}
Dprintf("send_cmd:read:");
for(i=0;i<2;i++){
ret[i] = buf[i];
Dprintf("%02x",buf[i]);
}
Dprintf("\n");
return ret;
}
void finalize(void){
libusb_close(devh);
libusb_exit(ctx);
}
int initialize(void){
int r=1;
int i=0;
int cnt=0;
/* libusb initialize*/
if ((r = libusb_init(&ctx)) < 0) {
perror("libusb_init\n");
exit(1);
} else {
libusb_set_debug(ctx,3);
Dprintf("init done\n");
}
/* confirm powerusb device */
/* list up all usb devices */
if((libusb_get_device_list(ctx,&devs)) < 0) {
perror("no usb device found");
exit(1);
}
/* check every usb devices */
while((dev =devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
if (libusb_get_device_descriptor(dev,&desc) < 0) {
perror("failed to get device descriptor\n");
return 1;
}
/* count how many PowerUSB device connected */
if (desc.idVendor == USB_VENDOR_ID &&
desc.idProduct == USB_PRODUCT_ID) {
cnt++;
Dprintf("PowerUSB device found\n");
}
}
/* no PowerUSB found*/
if (cnt == 0) {
fprintf(stderr, "Power USB device not connected\n");
exit(1);
}
/* multi-PowerUSB device found: return error*/
if (cnt > 1) {
/* FIXME */
fprintf(stderr, "multi PowerUSB is not implemented yet\n");
exit(1);
}
/* open powerusb device */
if ((devh = libusb_open_device_with_vid_pid(ctx,USB_VENDOR_ID,
USB_PRODUCT_ID)) < 0 ) {
perror("can't find PowerUSB device\n");
finalize();
exit(1);
} else {
Dprintf("PowerUSB device opened\n");
}
/* detach kernel driver if attached. */
/* is kernel driver active?*/
r = libusb_kernel_driver_active(devh,0);
if (r == 1) {
/*detaching kernel driver*/
r = libusb_detach_kernel_driver(devh,0);
if (r != 0) {
perror("detaching kernel driver failed");
exit(1);
}
}
return 0;
}
int main(int argc, char **argv)
{
initialize();
send_cmd(devh,CMD_GET_MODEL);
send_cmd(devh,CMD_GET_FIRM_VER);
send_cmd(devh,CMD_GET_STATE1);
send_cmd(devh,CMD_GET_STATE2);
send_cmd(devh,CMD_GET_STATE3);
#if 0
send_cmd(devh,0x42);
send_cmd(devh,0x44);
send_cmd(devh,0x45);
send_cmd(devh,0x42);
send_cmd(devh,0x44);
send_cmd(devh,0x45);
send_cmd(devh,0xb1);
send_cmd(devh,0xb2);
#endif
finalize();
exit(0);
}