6 #include <libusb-1.0/libusb.h>
8 #define USB_TIMEOUT 10000
10 #define ID_VENDOR 0x18ef
11 #define ID_PRODUCT 0xe015
13 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
14 static char * usb_strerror(int e
)
16 static char unknerr
[256];
22 return "Input/output error";
23 case LIBUSB_ERROR_INVALID_PARAM
:
24 return "Invalid parameter";
25 case LIBUSB_ERROR_ACCESS
:
26 return "Access denied (insufficient permissions)";
27 case LIBUSB_ERROR_NO_DEVICE
:
28 return "No such device (it may have been disconnected)";
29 case LIBUSB_ERROR_NOT_FOUND
:
30 return "Entity not found";
31 case LIBUSB_ERROR_BUSY
:
32 return "Resource busy";
33 case LIBUSB_ERROR_TIMEOUT
:
34 return "Operation timed out";
35 case LIBUSB_ERROR_OVERFLOW
:
37 case LIBUSB_ERROR_PIPE
:
39 case LIBUSB_ERROR_INTERRUPTED
:
40 return "System call interrupted (perhaps due to signal)";
41 case LIBUSB_ERROR_NO_MEM
:
42 return "Insufficient memory";
43 case LIBUSB_ERROR_NOT_SUPPORTED
:
44 return "Operation not supported or unimplemented on this platform";
45 case LIBUSB_ERROR_OTHER
:
48 snprintf(unknerr
, sizeof(unknerr
), "Unknown error code %d / 0x%02x", e
, e
);
52 libusb_device_handle
*fs20pcs_find() {
53 libusb_device_handle
*devh
= NULL
;
59 cnt
= libusb_get_device_list(NULL
, &list
);
61 fprintf(stderr
, "Can't get USB device list: %d\n", (int)cnt
);
65 for (i
= 0; i
< cnt
; i
++){
66 struct libusb_device_descriptor desc
;
68 err
= libusb_get_device_descriptor(list
[i
], &desc
);
72 if ((desc
.idVendor
== ID_VENDOR
) && (desc
.idProduct
== ID_PRODUCT
)) {
73 libusb_device
*dev
= list
[i
];
75 err
= libusb_open(dev
, &devh
);
77 fprintf(stderr
, "Can't open device: %s\n", usb_strerror(err
));
81 err
= libusb_detach_kernel_driver(devh
, 0);
82 if ((err
!= 0) && (err
!= LIBUSB_ERROR_NOT_FOUND
)) {
83 fprintf(stderr
, "Can't detach kernel driver: %s\n", usb_strerror(err
));
87 err
= libusb_claim_interface(devh
, 0);
89 fprintf(stderr
, "Can't claim interface: %s\n", usb_strerror(err
));
101 int fs20pcs_send(libusb_device_handle
*devh
, unsigned char* send_data
)
103 unsigned char recv_data
[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
109 err
= libusb_interrupt_transfer(devh
, 0x01, send_data
, 11, &cnt
, 5000);
111 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
115 err
= libusb_interrupt_transfer(devh
, 0x81, recv_data
, sizeof(recv_data
), &cnt
, 5000);
117 fprintf(stderr
, "Can't receive data: %s\n", usb_strerror(err
));
121 if ((recv_data
[0] != 0x02) ||
122 (recv_data
[1] != 0x03) ||
123 (recv_data
[2] != 0xa0)) {
124 fprintf(stderr
, "Unexpected response: ");
125 for (i
= 0; i
< cnt
; i
++) {
126 fprintf(stderr
, "0x%02x ", recv_data
[i
]);
128 fprintf(stderr
, "\n");
135 switch(recv_data
[3]) {
140 printf("Firmware V%d.%d", ((recv_data
[4] & 0xf0) >> 4) & 0x0f, recv_data
[4] & 0x0f);
143 printf("Unknown command");
147 printf("Wrong length");
151 printf("Aborted sending long press");
154 printf("Nothing to stop");
157 printf("Unknown response: 0x%02x 0x%02x", recv_data
[3], recv_data
[4]);
167 int main(int argc
, char **argv
)
169 unsigned char send_data
[11];
170 libusb_device_handle
*devh
= NULL
;
174 if ((argc
< 2) || (argc
> 12)) {
175 fprintf(stderr
, "Invalid number of parameters!\n");
179 memset(send_data
, 0, sizeof(send_data
));
180 for (i
= 0; i
< argc
- 1; i
++) {
181 send_data
[i
] = strtoul(argv
[i
+1], NULL
, 16);
184 err
= libusb_init(NULL
);
186 fprintf(stderr
, "Can't initialize libusb: %s\n", usb_strerror(err
));
190 devh
= fs20pcs_find();
192 fprintf(stderr
, "Can't find/open fs20pcs!\n");
196 if (fs20pcs_send(devh
, send_data
) == 0) {
197 fprintf(stderr
, "Can't communicate with fs20pcs!\n");