1 /* HM-CFG-USB libusb-driver
3 * Copyright (c) 2013 Michael Gernoth <michael@gernoth.net>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #include <libusb-1.0/libusb.h>
38 #define USB_TIMEOUT 10000
40 #define ID_VENDOR 0x1b1f
41 #define ID_PRODUCT 0xc00f
42 #define ID_PRODUCT_BL 0xc010
45 #define ASYNC_SIZE 0x0040
46 #define ASYNC_INTERVAL 32
56 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
57 static char * usb_strerror(int e
)
59 static char unknerr
[256];
65 return "Input/output error";
66 case LIBUSB_ERROR_INVALID_PARAM
:
67 return "Invalid parameter";
68 case LIBUSB_ERROR_ACCESS
:
69 return "Access denied (insufficient permissions)";
70 case LIBUSB_ERROR_NO_DEVICE
:
71 return "No such device (it may have been disconnected)";
72 case LIBUSB_ERROR_NOT_FOUND
:
73 return "Entity not found";
74 case LIBUSB_ERROR_BUSY
:
75 return "Resource busy";
76 case LIBUSB_ERROR_TIMEOUT
:
77 return "Operation timed out";
78 case LIBUSB_ERROR_OVERFLOW
:
80 case LIBUSB_ERROR_PIPE
:
82 case LIBUSB_ERROR_INTERRUPTED
:
83 return "System call interrupted (perhaps due to signal)";
84 case LIBUSB_ERROR_NO_MEM
:
85 return "Insufficient memory";
86 case LIBUSB_ERROR_NOT_SUPPORTED
:
87 return "Operation not supported or unimplemented on this platform";
88 case LIBUSB_ERROR_OTHER
:
91 snprintf(unknerr
, sizeof(unknerr
), "Unknown error code %d / 0x%02x", e
, e
);
95 static libusb_device_handle
*hmcfgusb_find() {
96 libusb_device_handle
*devh
= NULL
;
102 cnt
= libusb_get_device_list(NULL
, &list
);
104 fprintf(stderr
, "Can't get USB device list: %d\n", (int)cnt
);
108 for (i
= 0; i
< cnt
; i
++){
109 struct libusb_device_descriptor desc
;
111 err
= libusb_get_device_descriptor(list
[i
], &desc
);
115 if ((desc
.idVendor
== ID_VENDOR
) &&
116 ((desc
.idProduct
== ID_PRODUCT
) || (desc
.idProduct
== ID_PRODUCT_BL
))) {
117 libusb_device
*dev
= list
[i
];
119 err
= libusb_open(dev
, &devh
);
121 fprintf(stderr
, "Can't open device: %s\n", usb_strerror(err
));
125 err
= libusb_detach_kernel_driver(devh
, INTERFACE
);
126 if ((err
!= 0) && (err
!= LIBUSB_ERROR_NOT_FOUND
)) {
127 fprintf(stderr
, "Can't detach kernel driver: %s\n", usb_strerror(err
));
131 err
= libusb_claim_interface(devh
, INTERFACE
);
133 fprintf(stderr
, "Can't claim interface: %s\n", usb_strerror(err
));
145 int hmcfgusb_send_null_frame(struct hmcfgusb_dev
*usbdev
, int silent
)
149 unsigned char out
[0x40];
151 memset(out
, 0, sizeof(out
));
153 err
= libusb_interrupt_transfer(usbdev
->usb_devh
, EP_OUT
, out
, sizeof(out
), &cnt
, USB_TIMEOUT
);
154 if (err
&& (!silent
)) {
155 fprintf(stderr
, "Can't send null frame: %s\n", usb_strerror(err
));
162 int hmcfgusb_send(struct hmcfgusb_dev
*usbdev
, unsigned char* send_data
, int len
, int done
)
166 struct timeval tv_start
, tv_end
;
170 hexdump(send_data
, len
, "USB < ");
173 gettimeofday(&tv_start
, NULL
);
175 err
= libusb_interrupt_transfer(usbdev
->usb_devh
, EP_OUT
, send_data
, len
, &cnt
, USB_TIMEOUT
);
177 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
182 if (!hmcfgusb_send_null_frame(usbdev
, 0)) {
187 gettimeofday(&tv_end
, NULL
);
188 msec
= ((tv_end
.tv_sec
-tv_start
.tv_sec
)*1000)+((tv_end
.tv_usec
-tv_start
.tv_usec
)/1000);
191 fprintf(stderr
, "usb-transfer took more than 100ms (%dms), this may lead to timing problems!\n", msec
);
193 fprintf(stderr
, "usb-transfer took %dms!\n", msec
);
199 static struct libusb_transfer
*hmcfgusb_prepare_int(libusb_device_handle
*devh
, libusb_transfer_cb_fn cb
, void *data
)
201 unsigned char *data_buf
;
202 struct libusb_transfer
*transfer
;
205 data_buf
= malloc(ASYNC_SIZE
);
207 fprintf(stderr
, "Can't allocate memory for data-buffer!\n");
211 transfer
= libusb_alloc_transfer(0);
213 fprintf(stderr
, "Can't allocate memory for usb-transfer!\n");
218 libusb_fill_interrupt_transfer(transfer
, devh
, EP_IN
,
219 data_buf
, ASYNC_SIZE
, cb
, data
, USB_TIMEOUT
);
221 transfer
->flags
= LIBUSB_TRANSFER_SHORT_NOT_OK
| LIBUSB_TRANSFER_FREE_BUFFER
;
223 err
= libusb_submit_transfer(transfer
);
225 fprintf(stderr
, "Can't submit transfer: %s\n", usb_strerror(err
));
226 libusb_free_transfer(transfer
);
233 struct hmcfgusb_cb_data
{
234 struct hmcfgusb_dev
*dev
;
239 static void LIBUSB_CALL
hmcfgusb_interrupt(struct libusb_transfer
*transfer
)
242 struct hmcfgusb_cb_data
*cb_data
;
244 cb_data
= transfer
->user_data
;
246 if (transfer
->status
!= LIBUSB_TRANSFER_COMPLETED
) {
247 if (transfer
->status
!= LIBUSB_TRANSFER_TIMED_OUT
) {
248 fprintf(stderr
, "Interrupt transfer not completed: %s!\n", usb_strerror(transfer
->status
));
251 if (cb_data
&& cb_data
->dev
&& cb_data
->dev
->transfer
) {
252 libusb_free_transfer(cb_data
->dev
->transfer
);
253 cb_data
->dev
->transfer
= NULL
;
259 if (cb_data
&& cb_data
->cb
) {
261 hexdump(transfer
->buffer
, transfer
->actual_length
, "USB > ");
263 if (!cb_data
->cb(transfer
->buffer
, transfer
->actual_length
, cb_data
->data
)) {
266 if (cb_data
&& cb_data
->dev
&& cb_data
->dev
->transfer
) {
267 libusb_free_transfer(cb_data
->dev
->transfer
);
268 cb_data
->dev
->transfer
= NULL
;
275 hexdump(transfer
->buffer
, transfer
->actual_length
, "> ");
279 err
= libusb_submit_transfer(transfer
);
281 fprintf(stderr
, "Can't re-submit transfer: %s\n", usb_strerror(err
));
282 libusb_free_transfer(transfer
);
283 cb_data
->dev
->transfer
= NULL
;
288 struct hmcfgusb_dev
*hmcfgusb_init(hmcfgusb_cb_fn cb
, void *data
)
290 libusb_device_handle
*devh
= NULL
;
291 const struct libusb_pollfd
**usb_pfd
= NULL
;
292 struct hmcfgusb_dev
*dev
= NULL
;
293 struct hmcfgusb_cb_data
*cb_data
= NULL
;
297 err
= libusb_init(NULL
);
299 fprintf(stderr
, "Can't initialize libusb: %s\n", usb_strerror(err
));
303 devh
= hmcfgusb_find();
305 fprintf(stderr
, "Can't find/open hmcfgusb!\n");
309 dev
= malloc(sizeof(struct hmcfgusb_dev
));
311 perror("Can't allocate memory for hmcfgusb_dev");
315 memset(dev
, 0, sizeof(struct hmcfgusb_dev
));
316 dev
->usb_devh
= devh
;
318 cb_data
= malloc(sizeof(struct hmcfgusb_cb_data
));
320 perror("Can't allocate memory for hmcfgusb_cb_data");
325 memset(cb_data
, 0, sizeof(struct hmcfgusb_cb_data
));
329 cb_data
->data
= data
;
331 dev
->transfer
= hmcfgusb_prepare_int(devh
, hmcfgusb_interrupt
, cb_data
);
332 if (!dev
->transfer
) {
333 fprintf(stderr
, "Can't prepare async device io!\n");
339 usb_pfd
= libusb_get_pollfds(NULL
);
341 fprintf(stderr
, "Can't get FDset from libusb!\n");
348 for(i
= 0; usb_pfd
[i
]; i
++)
351 dev
->pfd
= malloc(dev
->n_usb_pfd
* sizeof(struct pollfd
));
353 perror("Can't allocate memory for poll-fds");
359 memset(dev
->pfd
, 0, dev
->n_usb_pfd
* sizeof(struct pollfd
));
361 for (i
= 0; i
< dev
->n_usb_pfd
; i
++) {
362 dev
->pfd
[i
].fd
= usb_pfd
[i
]->fd
;
363 dev
->pfd
[i
].events
= usb_pfd
[i
]->events
;
364 dev
->pfd
[i
].revents
= 0;
369 dev
->n_pfd
= dev
->n_usb_pfd
;
376 int hmcfgusb_add_pfd(struct hmcfgusb_dev
*dev
, int fd
, short events
)
379 dev
->pfd
= realloc(dev
->pfd
, dev
->n_pfd
* sizeof(struct pollfd
));
381 perror("Can't realloc poll-fds");
385 dev
->pfd
[dev
->n_pfd
-1].fd
= fd
;
386 dev
->pfd
[dev
->n_pfd
-1].events
= events
;
387 dev
->pfd
[dev
->n_pfd
-1].revents
= 0;
392 int hmcfgusb_poll(struct hmcfgusb_dev
*dev
, int timeout
)
404 memset(&tv
, 0, sizeof(tv
));
405 err
= libusb_get_next_timeout(NULL
, &tv
);
407 fprintf(stderr
, "libusb_get_next_timeout: %s\n", usb_strerror(err
));
410 } else if (err
== 0) {
411 /* No pending timeout or a sane platform */
414 if ((tv
.tv_sec
== 0) && (tv
.tv_usec
== 0)) {
416 } else if (tv
.tv_sec
> timeout
) {
423 for (i
= 0; i
< dev
->n_pfd
; i
++) {
424 dev
->pfd
[i
].revents
= 0;
427 n
= poll(dev
->pfd
, dev
->n_pfd
, tv
.tv_sec
* 1000);
436 for (fd_n
= 0; fd_n
< dev
->n_pfd
; fd_n
++) {
437 if (dev
->pfd
[fd_n
].revents
) {
438 if (fd_n
< dev
->n_usb_pfd
) {
443 return dev
->pfd
[fd_n
].fd
;
451 memset(&tv
, 0, sizeof(tv
));
452 err
= libusb_handle_events_timeout_completed(NULL
, &tv
, NULL
);
454 fprintf(stderr
, "libusb_handle_events_timeout_completed: %s\n", usb_strerror(err
));
462 fprintf(stderr
, "closing device-connection due to error %d\n", quit
);
472 void hmcfgusb_close(struct hmcfgusb_dev
*dev
)
477 libusb_cancel_transfer(dev
->transfer
);
480 err
= libusb_release_interface(dev
->usb_devh
, INTERFACE
);
482 fprintf(stderr
, "Can't release interface: %s\n", usb_strerror(err
));
485 libusb_close(dev
->usb_devh
);
492 void hmcfgusb_set_debug(int d
)