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
62 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
63 static char * usb_strerror(int e
)
65 static char unknerr
[256];
71 return "Input/output error";
72 case LIBUSB_ERROR_INVALID_PARAM
:
73 return "Invalid parameter";
74 case LIBUSB_ERROR_ACCESS
:
75 return "Access denied (insufficient permissions)";
76 case LIBUSB_ERROR_NO_DEVICE
:
77 return "No such device (it may have been disconnected)";
78 case LIBUSB_ERROR_NOT_FOUND
:
79 return "Entity not found";
80 case LIBUSB_ERROR_BUSY
:
81 return "Resource busy";
82 case LIBUSB_ERROR_TIMEOUT
:
83 return "Operation timed out";
84 case LIBUSB_ERROR_OVERFLOW
:
86 case LIBUSB_ERROR_PIPE
:
88 case LIBUSB_ERROR_INTERRUPTED
:
89 return "System call interrupted (perhaps due to signal)";
90 case LIBUSB_ERROR_NO_MEM
:
91 return "Insufficient memory";
92 case LIBUSB_ERROR_NOT_SUPPORTED
:
93 return "Operation not supported or unimplemented on this platform";
94 case LIBUSB_ERROR_OTHER
:
97 snprintf(unknerr
, sizeof(unknerr
), "Unknown error code %d / 0x%02x", e
, e
);
101 static libusb_device_handle
*hmcfgusb_find(int vid
, int pid
) {
102 libusb_device_handle
*devh
= NULL
;
103 libusb_device
**list
;
108 cnt
= libusb_get_device_list(NULL
, &list
);
110 fprintf(stderr
, "Can't get USB device list: %d\n", (int)cnt
);
114 for (i
= 0; i
< cnt
; i
++){
115 struct libusb_device_descriptor desc
;
117 err
= libusb_get_device_descriptor(list
[i
], &desc
);
121 if ((desc
.idVendor
== vid
) && (desc
.idProduct
== pid
)) {
122 libusb_device
*dev
= list
[i
];
124 err
= libusb_open(dev
, &devh
);
126 fprintf(stderr
, "Can't open device: %s\n", usb_strerror(err
));
130 err
= libusb_detach_kernel_driver(devh
, INTERFACE
);
131 if ((err
!= 0) && (err
!= LIBUSB_ERROR_NOT_FOUND
)) {
132 fprintf(stderr
, "Can't detach kernel driver: %s\n", usb_strerror(err
));
136 err
= libusb_claim_interface(devh
, INTERFACE
);
138 fprintf(stderr
, "Can't claim interface: %s\n", usb_strerror(err
));
150 int hmcfgusb_send_null_frame(struct hmcfgusb_dev
*usbdev
, int silent
)
154 unsigned char out
[0x40];
156 memset(out
, 0, sizeof(out
));
158 err
= libusb_interrupt_transfer(usbdev
->usb_devh
, EP_OUT
, out
, 0, &cnt
, USB_TIMEOUT
);
159 if (err
&& (!silent
)) {
160 fprintf(stderr
, "Can't send null frame: %s\n", usb_strerror(err
));
167 int hmcfgusb_send(struct hmcfgusb_dev
*usbdev
, unsigned char* send_data
, int len
, int done
)
171 struct timeval tv_start
, tv_end
;
175 hexdump(send_data
, len
, "USB < ");
178 gettimeofday(&tv_start
, NULL
);
180 err
= libusb_interrupt_transfer(usbdev
->usb_devh
, EP_OUT
, send_data
, len
, &cnt
, USB_TIMEOUT
);
182 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
187 if (!hmcfgusb_send_null_frame(usbdev
, 0)) {
192 gettimeofday(&tv_end
, NULL
);
193 msec
= ((tv_end
.tv_sec
-tv_start
.tv_sec
)*1000)+((tv_end
.tv_usec
-tv_start
.tv_usec
)/1000);
196 fprintf(stderr
, "usb-transfer took more than 100ms (%dms), this may lead to timing problems!\n", msec
);
198 fprintf(stderr
, "usb-transfer took %dms!\n", msec
);
204 static struct libusb_transfer
*hmcfgusb_prepare_int(libusb_device_handle
*devh
, libusb_transfer_cb_fn cb
, void *data
, int in_size
)
206 unsigned char *data_buf
;
207 struct libusb_transfer
*transfer
;
210 data_buf
= malloc(in_size
);
212 fprintf(stderr
, "Can't allocate memory for data-buffer!\n");
216 transfer
= libusb_alloc_transfer(0);
218 fprintf(stderr
, "Can't allocate memory for usb-transfer!\n");
223 libusb_fill_interrupt_transfer(transfer
, devh
, EP_IN
,
224 data_buf
, in_size
, cb
, data
, USB_TIMEOUT
);
226 transfer
->flags
= LIBUSB_TRANSFER_FREE_BUFFER
;
228 err
= libusb_submit_transfer(transfer
);
230 fprintf(stderr
, "Can't submit transfer: %s\n", usb_strerror(err
));
231 libusb_free_transfer(transfer
);
238 struct hmcfgusb_cb_data
{
239 struct hmcfgusb_dev
*dev
;
244 static void LIBUSB_CALL
hmcfgusb_interrupt(struct libusb_transfer
*transfer
)
247 struct hmcfgusb_cb_data
*cb_data
;
249 cb_data
= transfer
->user_data
;
251 if (transfer
->status
!= LIBUSB_TRANSFER_COMPLETED
) {
252 if (transfer
->status
!= LIBUSB_TRANSFER_TIMED_OUT
) {
253 fprintf(stderr
, "Interrupt transfer not completed: %s!\n", usb_strerror(transfer
->status
));
256 if (cb_data
&& cb_data
->dev
&& cb_data
->dev
->transfer
) {
257 libusb_free_transfer(cb_data
->dev
->transfer
);
258 cb_data
->dev
->transfer
= NULL
;
264 if (cb_data
&& cb_data
->cb
) {
266 hexdump(transfer
->buffer
, transfer
->actual_length
, "USB > ");
268 if (!cb_data
->cb(transfer
->buffer
, transfer
->actual_length
, cb_data
->data
)) {
271 if (cb_data
&& cb_data
->dev
&& cb_data
->dev
->transfer
) {
272 libusb_free_transfer(cb_data
->dev
->transfer
);
273 cb_data
->dev
->transfer
= NULL
;
280 hexdump(transfer
->buffer
, transfer
->actual_length
, "> ");
284 err
= libusb_submit_transfer(transfer
);
286 fprintf(stderr
, "Can't re-submit transfer: %s\n", usb_strerror(err
));
287 libusb_free_transfer(transfer
);
290 cb_data
->dev
->transfer
= NULL
;
296 struct hmcfgusb_dev
*hmcfgusb_init(hmcfgusb_cb_fn cb
, void *data
)
298 libusb_device_handle
*devh
= NULL
;
299 const struct libusb_pollfd
**usb_pfd
= NULL
;
300 struct hmcfgusb_dev
*dev
= NULL
;
301 struct hmcfgusb_cb_data
*cb_data
= NULL
;
306 err
= libusb_init(NULL
);
308 fprintf(stderr
, "Can't initialize libusb: %s\n", usb_strerror(err
));
312 devh
= hmcfgusb_find(ID_VENDOR
, ID_PRODUCT
);
314 devh
= hmcfgusb_find(ID_VENDOR
, ID_PRODUCT_BL
);
316 fprintf(stderr
, "Can't find/open hmcfgusb!\n");
323 dev
= malloc(sizeof(struct hmcfgusb_dev
));
325 perror("Can't allocate memory for hmcfgusb_dev");
331 memset(dev
, 0, sizeof(struct hmcfgusb_dev
));
332 dev
->usb_devh
= devh
;
333 dev
->bootloader
= bootloader
;
334 dev
->opened_at
= time(NULL
);
336 cb_data
= malloc(sizeof(struct hmcfgusb_cb_data
));
338 perror("Can't allocate memory for hmcfgusb_cb_data");
345 memset(cb_data
, 0, sizeof(struct hmcfgusb_cb_data
));
349 cb_data
->data
= data
;
351 dev
->transfer
= hmcfgusb_prepare_int(devh
, hmcfgusb_interrupt
, cb_data
, ASYNC_SIZE
);
353 if (!dev
->transfer
) {
354 fprintf(stderr
, "Can't prepare async device io!\n");
362 usb_pfd
= libusb_get_pollfds(NULL
);
364 fprintf(stderr
, "Can't get FDset from libusb!\n");
373 for(i
= 0; usb_pfd
[i
]; i
++)
376 dev
->pfd
= malloc(dev
->n_usb_pfd
* sizeof(struct pollfd
));
378 perror("Can't allocate memory for poll-fds");
386 memset(dev
->pfd
, 0, dev
->n_usb_pfd
* sizeof(struct pollfd
));
388 for (i
= 0; i
< dev
->n_usb_pfd
; i
++) {
389 dev
->pfd
[i
].fd
= usb_pfd
[i
]->fd
;
390 dev
->pfd
[i
].events
= usb_pfd
[i
]->events
;
391 dev
->pfd
[i
].revents
= 0;
396 dev
->n_pfd
= dev
->n_usb_pfd
;
403 int hmcfgusb_add_pfd(struct hmcfgusb_dev
*dev
, int fd
, short events
)
406 dev
->pfd
= realloc(dev
->pfd
, dev
->n_pfd
* sizeof(struct pollfd
));
408 perror("Can't realloc poll-fds");
412 dev
->pfd
[dev
->n_pfd
-1].fd
= fd
;
413 dev
->pfd
[dev
->n_pfd
-1].events
= events
;
414 dev
->pfd
[dev
->n_pfd
-1].revents
= 0;
419 int hmcfgusb_poll(struct hmcfgusb_dev
*dev
, int timeout
)
431 memset(&tv
, 0, sizeof(tv
));
432 err
= libusb_get_next_timeout(NULL
, &tv
);
434 fprintf(stderr
, "libusb_get_next_timeout: %s\n", usb_strerror(err
));
437 } else if (err
== 0) {
438 /* No pending timeout or a sane platform */
440 if ((tv
.tv_sec
== 0) && (tv
.tv_usec
== 0)) {
442 } else if ((tv
.tv_sec
* 1000) < timeout
) {
443 timeout
= tv
.tv_sec
* 1000;
448 for (i
= 0; i
< dev
->n_pfd
; i
++) {
449 dev
->pfd
[i
].revents
= 0;
452 n
= poll(dev
->pfd
, dev
->n_pfd
, timeout
);
461 for (fd_n
= 0; fd_n
< dev
->n_pfd
; fd_n
++) {
462 if (dev
->pfd
[fd_n
].revents
) {
463 if (fd_n
< dev
->n_usb_pfd
) {
468 return dev
->pfd
[fd_n
].fd
;
476 memset(&tv
, 0, sizeof(tv
));
477 err
= libusb_handle_events_timeout_completed(NULL
, &tv
, NULL
);
479 fprintf(stderr
, "libusb_handle_events_timeout_completed: %s\n", usb_strerror(err
));
487 fprintf(stderr
, "closing device-connection due to error %d\n", quit
);
497 void hmcfgusb_enter_bootloader(struct hmcfgusb_dev
*dev
)
499 uint8_t out
[ASYNC_SIZE
];
501 if (dev
->bootloader
) {
502 fprintf(stderr
, "request for bootloader mode, but device already in bootloader!\n");
506 memset(out
, 0, sizeof(out
));
508 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
513 void hmcfgusb_leave_bootloader(struct hmcfgusb_dev
*dev
)
515 uint8_t out
[ASYNC_SIZE
];
517 if (!dev
->bootloader
) {
518 fprintf(stderr
, "request for leaving bootloader mode, but device already in normal mode!\n");
522 memset(out
, 0, sizeof(out
));
524 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
529 void hmcfgusb_close(struct hmcfgusb_dev
*dev
)
534 libusb_cancel_transfer(dev
->transfer
);
537 err
= libusb_release_interface(dev
->usb_devh
, INTERFACE
);
539 fprintf(stderr
, "Can't release interface: %s\n", usb_strerror(err
));
542 libusb_close(dev
->usb_devh
);
549 void hmcfgusb_set_debug(int d
)