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>
35 /* Workaround for old libusb-1.0 */
38 #define libusb_handle_events_timeout_completed(ctx, tv, x) libusb_handle_events_timeout(ctx, tv)
44 #define USB_TIMEOUT 10000
46 #define ID_VENDOR 0x1b1f
47 #define ID_PRODUCT 0xc00f
48 #define ID_PRODUCT_BL 0xc010
51 #define ASYNC_SIZE 0x0040
52 #define ASYNC_INTERVAL 32
61 static int libusb_initialized
= 0;
63 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
64 static char * usb_strerror(int e
)
66 static char unknerr
[256];
72 return "Input/output error";
73 case LIBUSB_ERROR_INVALID_PARAM
:
74 return "Invalid parameter";
75 case LIBUSB_ERROR_ACCESS
:
76 return "Access denied (insufficient permissions)";
77 case LIBUSB_ERROR_NO_DEVICE
:
78 return "No such device (it may have been disconnected)";
79 case LIBUSB_ERROR_NOT_FOUND
:
80 return "Entity not found";
81 case LIBUSB_ERROR_BUSY
:
82 return "Resource busy";
83 case LIBUSB_ERROR_TIMEOUT
:
84 return "Operation timed out";
85 case LIBUSB_ERROR_OVERFLOW
:
87 case LIBUSB_ERROR_PIPE
:
89 case LIBUSB_ERROR_INTERRUPTED
:
90 return "System call interrupted (perhaps due to signal)";
91 case LIBUSB_ERROR_NO_MEM
:
92 return "Insufficient memory";
93 case LIBUSB_ERROR_NOT_SUPPORTED
:
94 return "Operation not supported or unimplemented on this platform";
95 case LIBUSB_ERROR_OTHER
:
98 snprintf(unknerr
, sizeof(unknerr
), "Unknown error code %d / 0x%02x", e
, e
);
102 static libusb_device_handle
*hmcfgusb_find(int vid
, int pid
) {
103 libusb_device_handle
*devh
= NULL
;
104 libusb_device
**list
;
109 cnt
= libusb_get_device_list(NULL
, &list
);
111 fprintf(stderr
, "Can't get USB device list: %d\n", (int)cnt
);
115 for (i
= 0; i
< cnt
; i
++){
116 struct libusb_device_descriptor desc
;
118 err
= libusb_get_device_descriptor(list
[i
], &desc
);
122 if ((desc
.idVendor
== vid
) && (desc
.idProduct
== pid
)) {
123 libusb_device
*dev
= list
[i
];
125 err
= libusb_open(dev
, &devh
);
127 fprintf(stderr
, "Can't open device: %s\n", usb_strerror(err
));
128 libusb_free_device_list(list
, 1);
132 err
= libusb_detach_kernel_driver(devh
, INTERFACE
);
133 if ((err
!= 0) && (err
!= LIBUSB_ERROR_NOT_FOUND
)) {
134 fprintf(stderr
, "Can't detach kernel driver: %s\n", usb_strerror(err
));
136 libusb_free_device_list(list
, 1);
140 err
= libusb_claim_interface(devh
, INTERFACE
);
142 fprintf(stderr
, "Can't claim interface: %s\n", usb_strerror(err
));
144 libusb_free_device_list(list
, 1);
148 libusb_free_device_list(list
, 0);
154 libusb_free_device_list(list
, 1);
158 int hmcfgusb_send_null_frame(struct hmcfgusb_dev
*usbdev
, int silent
)
162 unsigned char out
[0x40];
164 memset(out
, 0, sizeof(out
));
166 err
= libusb_interrupt_transfer(usbdev
->usb_devh
, EP_OUT
, out
, 0, &cnt
, USB_TIMEOUT
);
167 if (err
&& (!silent
)) {
168 fprintf(stderr
, "Can't send null frame: %s\n", usb_strerror(err
));
175 int hmcfgusb_send(struct hmcfgusb_dev
*usbdev
, unsigned char* send_data
, int len
, int done
)
179 struct timeval tv_start
, tv_end
;
183 hexdump(send_data
, len
, "USB < ");
186 gettimeofday(&tv_start
, NULL
);
188 err
= libusb_interrupt_transfer(usbdev
->usb_devh
, EP_OUT
, send_data
, len
, &cnt
, USB_TIMEOUT
);
190 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
195 if (!hmcfgusb_send_null_frame(usbdev
, 0)) {
200 gettimeofday(&tv_end
, NULL
);
201 msec
= ((tv_end
.tv_sec
-tv_start
.tv_sec
)*1000)+((tv_end
.tv_usec
-tv_start
.tv_usec
)/1000);
204 fprintf(stderr
, "usb-transfer took more than 100ms (%dms), this may lead to timing problems!\n", msec
);
206 fprintf(stderr
, "usb-transfer took %dms!\n", msec
);
212 static struct libusb_transfer
*hmcfgusb_prepare_int(libusb_device_handle
*devh
, libusb_transfer_cb_fn cb
, void *data
, int in_size
)
214 unsigned char *data_buf
;
215 struct libusb_transfer
*transfer
;
218 data_buf
= malloc(in_size
);
220 fprintf(stderr
, "Can't allocate memory for data-buffer!\n");
224 transfer
= libusb_alloc_transfer(0);
226 fprintf(stderr
, "Can't allocate memory for usb-transfer!\n");
231 libusb_fill_interrupt_transfer(transfer
, devh
, EP_IN
,
232 data_buf
, in_size
, cb
, data
, USB_TIMEOUT
);
234 transfer
->flags
= LIBUSB_TRANSFER_FREE_BUFFER
;
236 err
= libusb_submit_transfer(transfer
);
238 fprintf(stderr
, "Can't submit transfer: %s\n", usb_strerror(err
));
239 libusb_free_transfer(transfer
);
246 struct hmcfgusb_cb_data
{
247 struct hmcfgusb_dev
*dev
;
252 static void LIBUSB_CALL
hmcfgusb_interrupt(struct libusb_transfer
*transfer
)
255 struct hmcfgusb_cb_data
*cb_data
;
257 cb_data
= transfer
->user_data
;
259 if (transfer
->status
!= LIBUSB_TRANSFER_COMPLETED
) {
260 if (transfer
->status
!= LIBUSB_TRANSFER_TIMED_OUT
) {
261 if (transfer
->status
!= LIBUSB_TRANSFER_CANCELLED
)
262 fprintf(stderr
, "Interrupt transfer not completed: %s!\n", usb_strerror(transfer
->status
));
268 if (cb_data
&& cb_data
->cb
) {
270 hexdump(transfer
->buffer
, transfer
->actual_length
, "USB > ");
272 if (!cb_data
->cb(transfer
->buffer
, transfer
->actual_length
, cb_data
->data
)) {
277 hexdump(transfer
->buffer
, transfer
->actual_length
, "> ");
281 err
= libusb_submit_transfer(transfer
);
283 fprintf(stderr
, "Can't re-submit transfer: %s\n", usb_strerror(err
));
290 libusb_free_transfer(transfer
);
292 if (cb_data
->dev
&& cb_data
->dev
->transfer
) {
293 cb_data
->dev
->transfer
= NULL
;
299 struct hmcfgusb_dev
*hmcfgusb_init(hmcfgusb_cb_fn cb
, void *data
)
301 libusb_device_handle
*devh
= NULL
;
302 const struct libusb_pollfd
**usb_pfd
= NULL
;
303 struct hmcfgusb_dev
*dev
= NULL
;
304 struct hmcfgusb_cb_data
*cb_data
= NULL
;
309 if (!libusb_initialized
) {
310 err
= libusb_init(NULL
);
312 fprintf(stderr
, "Can't initialize libusb: %s\n", usb_strerror(err
));
316 libusb_initialized
= 1;
318 devh
= hmcfgusb_find(ID_VENDOR
, ID_PRODUCT
);
320 devh
= hmcfgusb_find(ID_VENDOR
, ID_PRODUCT_BL
);
322 fprintf(stderr
, "Can't find/open hmcfgusb!\n");
323 #ifdef NEED_LIBUSB_EXIT
331 dev
= malloc(sizeof(struct hmcfgusb_dev
));
333 perror("Can't allocate memory for hmcfgusb_dev");
335 #ifdef NEED_LIBUSB_EXIT
341 memset(dev
, 0, sizeof(struct hmcfgusb_dev
));
342 dev
->usb_devh
= devh
;
343 dev
->bootloader
= bootloader
;
344 dev
->opened_at
= time(NULL
);
346 cb_data
= malloc(sizeof(struct hmcfgusb_cb_data
));
348 perror("Can't allocate memory for hmcfgusb_cb_data");
351 #ifdef NEED_LIBUSB_EXIT
357 memset(cb_data
, 0, sizeof(struct hmcfgusb_cb_data
));
361 cb_data
->data
= data
;
363 dev
->transfer
= hmcfgusb_prepare_int(devh
, hmcfgusb_interrupt
, cb_data
, ASYNC_SIZE
);
365 if (!dev
->transfer
) {
366 fprintf(stderr
, "Can't prepare async device io!\n");
370 #ifdef NEED_LIBUSB_EXIT
376 usb_pfd
= libusb_get_pollfds(NULL
);
378 fprintf(stderr
, "Can't get FDset from libusb!\n");
379 libusb_cancel_transfer(dev
->transfer
);
380 libusb_handle_events(NULL
);
384 #ifdef NEED_LIBUSB_EXIT
391 for(i
= 0; usb_pfd
[i
]; i
++)
394 dev
->pfd
= malloc(dev
->n_usb_pfd
* sizeof(struct pollfd
));
396 perror("Can't allocate memory for poll-fds");
397 libusb_cancel_transfer(dev
->transfer
);
398 libusb_handle_events(NULL
);
402 #ifdef NEED_LIBUSB_EXIT
408 memset(dev
->pfd
, 0, dev
->n_usb_pfd
* sizeof(struct pollfd
));
410 for (i
= 0; i
< dev
->n_usb_pfd
; i
++) {
411 dev
->pfd
[i
].fd
= usb_pfd
[i
]->fd
;
412 dev
->pfd
[i
].events
= usb_pfd
[i
]->events
;
413 dev
->pfd
[i
].revents
= 0;
418 dev
->n_pfd
= dev
->n_usb_pfd
;
425 int hmcfgusb_add_pfd(struct hmcfgusb_dev
*dev
, int fd
, short events
)
428 dev
->pfd
= realloc(dev
->pfd
, dev
->n_pfd
* sizeof(struct pollfd
));
430 perror("Can't realloc poll-fds");
434 dev
->pfd
[dev
->n_pfd
-1].fd
= fd
;
435 dev
->pfd
[dev
->n_pfd
-1].events
= events
;
436 dev
->pfd
[dev
->n_pfd
-1].revents
= 0;
441 int hmcfgusb_poll(struct hmcfgusb_dev
*dev
, int timeout
)
453 memset(&tv
, 0, sizeof(tv
));
454 err
= libusb_get_next_timeout(NULL
, &tv
);
456 fprintf(stderr
, "libusb_get_next_timeout: %s\n", usb_strerror(err
));
459 } else if (err
== 0) {
460 /* No pending timeout or a sane platform */
462 if ((tv
.tv_sec
== 0) && (tv
.tv_usec
== 0)) {
464 } else if ((tv
.tv_sec
* 1000) < timeout
) {
465 timeout
= tv
.tv_sec
* 1000;
470 for (i
= 0; i
< dev
->n_pfd
; i
++) {
471 dev
->pfd
[i
].revents
= 0;
474 n
= poll(dev
->pfd
, dev
->n_pfd
, timeout
);
483 for (fd_n
= 0; fd_n
< dev
->n_pfd
; fd_n
++) {
484 if (dev
->pfd
[fd_n
].revents
) {
485 if (fd_n
< dev
->n_usb_pfd
) {
490 return dev
->pfd
[fd_n
].fd
;
498 memset(&tv
, 0, sizeof(tv
));
499 err
= libusb_handle_events_timeout_completed(NULL
, &tv
, NULL
);
501 fprintf(stderr
, "libusb_handle_events_timeout_completed: %s\n", usb_strerror(err
));
509 fprintf(stderr
, "closing device-connection due to error %d\n", quit
);
519 void hmcfgusb_enter_bootloader(struct hmcfgusb_dev
*dev
)
521 uint8_t out
[ASYNC_SIZE
];
523 if (dev
->bootloader
) {
524 fprintf(stderr
, "request for bootloader mode, but device already in bootloader!\n");
528 memset(out
, 0, sizeof(out
));
530 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
535 void hmcfgusb_leave_bootloader(struct hmcfgusb_dev
*dev
)
537 uint8_t out
[ASYNC_SIZE
];
539 if (!dev
->bootloader
) {
540 fprintf(stderr
, "request for leaving bootloader mode, but device already in normal mode!\n");
544 memset(out
, 0, sizeof(out
));
546 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
551 void hmcfgusb_close(struct hmcfgusb_dev
*dev
)
556 libusb_cancel_transfer(dev
->transfer
);
557 libusb_handle_events(NULL
);
560 err
= libusb_release_interface(dev
->usb_devh
, INTERFACE
);
562 fprintf(stderr
, "Can't release interface: %s\n", usb_strerror(err
));
565 libusb_close(dev
->usb_devh
);
566 #ifdef NEED_LIBUSB_EXIT
573 void hmcfgusb_exit(void)
575 if (libusb_initialized
) {
577 libusb_initialized
= 0;
581 void hmcfgusb_set_debug(int d
)