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
44 #define ASYNC_SIZE 0x0040
45 #define ASYNC_INTERVAL 32
55 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
56 static char * usb_strerror(int e
)
58 static char unknerr
[256];
64 return "Input/output error";
65 case LIBUSB_ERROR_INVALID_PARAM
:
66 return "Invalid parameter";
67 case LIBUSB_ERROR_ACCESS
:
68 return "Access denied (insufficient permissions)";
69 case LIBUSB_ERROR_NO_DEVICE
:
70 return "No such device (it may have been disconnected)";
71 case LIBUSB_ERROR_NOT_FOUND
:
72 return "Entity not found";
73 case LIBUSB_ERROR_BUSY
:
74 return "Resource busy";
75 case LIBUSB_ERROR_TIMEOUT
:
76 return "Operation timed out";
77 case LIBUSB_ERROR_OVERFLOW
:
79 case LIBUSB_ERROR_PIPE
:
81 case LIBUSB_ERROR_INTERRUPTED
:
82 return "System call interrupted (perhaps due to signal)";
83 case LIBUSB_ERROR_NO_MEM
:
84 return "Insufficient memory";
85 case LIBUSB_ERROR_NOT_SUPPORTED
:
86 return "Operation not supported or unimplemented on this platform";
87 case LIBUSB_ERROR_OTHER
:
90 snprintf(unknerr
, sizeof(unknerr
), "Unknown error code %d / 0x%02x", e
, e
);
94 static libusb_device_handle
*hmcfgusb_find() {
95 libusb_device_handle
*devh
= NULL
;
101 cnt
= libusb_get_device_list(NULL
, &list
);
103 fprintf(stderr
, "Can't get USB device list: %d\n", (int)cnt
);
107 for (i
= 0; i
< cnt
; i
++){
108 struct libusb_device_descriptor desc
;
110 err
= libusb_get_device_descriptor(list
[i
], &desc
);
114 if ((desc
.idVendor
== ID_VENDOR
) && (desc
.idProduct
== ID_PRODUCT
)) {
115 libusb_device
*dev
= list
[i
];
117 err
= libusb_open(dev
, &devh
);
119 fprintf(stderr
, "Can't open device: %s\n", usb_strerror(err
));
123 err
= libusb_detach_kernel_driver(devh
, INTERFACE
);
124 if ((err
!= 0) && (err
!= LIBUSB_ERROR_NOT_FOUND
)) {
125 fprintf(stderr
, "Can't detach kernel driver: %s\n", usb_strerror(err
));
129 err
= libusb_claim_interface(devh
, INTERFACE
);
131 fprintf(stderr
, "Can't claim interface: %s\n", usb_strerror(err
));
143 int hmcfgusb_send_null_frame(struct hmcfgusb_dev
*usbdev
)
148 err
= libusb_interrupt_transfer(usbdev
->usb_devh
, EP_OUT
, NULL
, 0, &cnt
, USB_TIMEOUT
);
150 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
157 int hmcfgusb_send(struct hmcfgusb_dev
*usbdev
, unsigned char* send_data
, int len
, int done
)
161 struct timeval tv_start
, tv_end
;
165 hexdump(send_data
, len
, "USB < ");
166 gettimeofday(&tv_start
, NULL
);
169 err
= libusb_interrupt_transfer(usbdev
->usb_devh
, EP_OUT
, send_data
, len
, &cnt
, USB_TIMEOUT
);
171 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
176 if (!hmcfgusb_send_null_frame(usbdev
)) {
182 gettimeofday(&tv_end
, NULL
);
183 msec
= ((tv_end
.tv_sec
-tv_start
.tv_sec
)*1000)+((tv_end
.tv_usec
-tv_start
.tv_usec
)/1000);
184 fprintf(stderr
, "send took %dms!\n", msec
);
190 static struct libusb_transfer
*hmcfgusb_prepare_int(libusb_device_handle
*devh
, libusb_transfer_cb_fn cb
, void *data
)
192 unsigned char *data_buf
;
193 struct libusb_transfer
*transfer
;
196 data_buf
= malloc(ASYNC_SIZE
);
198 fprintf(stderr
, "Can't allocate memory for data-buffer!\n");
202 transfer
= libusb_alloc_transfer(0);
204 fprintf(stderr
, "Can't allocate memory for usb-transfer!\n");
209 libusb_fill_interrupt_transfer(transfer
, devh
, EP_IN
,
210 data_buf
, ASYNC_SIZE
, cb
, data
, USB_TIMEOUT
);
212 transfer
->flags
= LIBUSB_TRANSFER_SHORT_NOT_OK
| LIBUSB_TRANSFER_FREE_BUFFER
;
214 err
= libusb_submit_transfer(transfer
);
216 fprintf(stderr
, "Can't submit transfer: %s\n", usb_strerror(err
));
217 libusb_free_transfer(transfer
);
224 struct hmcfgusb_cb_data
{
225 struct hmcfgusb_dev
*dev
;
230 static void LIBUSB_CALL
hmcfgusb_interrupt(struct libusb_transfer
*transfer
)
233 struct hmcfgusb_cb_data
*cb_data
;
235 cb_data
= transfer
->user_data
;
237 if (transfer
->status
!= LIBUSB_TRANSFER_COMPLETED
) {
238 if (transfer
->status
!= LIBUSB_TRANSFER_TIMED_OUT
) {
239 fprintf(stderr
, "Interrupt transfer not completed: %d!\n", transfer
->status
);
242 if (cb_data
&& cb_data
->dev
&& cb_data
->dev
->transfer
) {
243 libusb_free_transfer(cb_data
->dev
->transfer
);
244 cb_data
->dev
->transfer
= NULL
;
250 if (cb_data
&& cb_data
->cb
) {
252 hexdump(transfer
->buffer
, transfer
->actual_length
, "USB > ");
254 if (!cb_data
->cb(transfer
->buffer
, transfer
->actual_length
, cb_data
->data
)) {
257 if (cb_data
&& cb_data
->dev
&& cb_data
->dev
->transfer
) {
258 libusb_free_transfer(cb_data
->dev
->transfer
);
259 cb_data
->dev
->transfer
= NULL
;
266 hexdump(transfer
->buffer
, transfer
->actual_length
, "> ");
270 err
= libusb_submit_transfer(transfer
);
272 fprintf(stderr
, "Can't re-submit transfer: %s\n", usb_strerror(err
));
273 libusb_free_transfer(transfer
);
274 cb_data
->dev
->transfer
= NULL
;
279 struct hmcfgusb_dev
*hmcfgusb_init(hmcfgusb_cb_fn cb
, void *data
)
281 libusb_device_handle
*devh
= NULL
;
282 const struct libusb_pollfd
**usb_pfd
= NULL
;
283 struct hmcfgusb_dev
*dev
= NULL
;
284 struct hmcfgusb_cb_data
*cb_data
= NULL
;
288 err
= libusb_init(NULL
);
290 fprintf(stderr
, "Can't initialize libusb: %s\n", usb_strerror(err
));
294 devh
= hmcfgusb_find();
296 fprintf(stderr
, "Can't find/open hmcfgusb!\n");
300 dev
= malloc(sizeof(struct hmcfgusb_dev
));
302 perror("Can't allocate memory for hmcfgusb_dev");
306 memset(dev
, 0, sizeof(struct hmcfgusb_dev
));
307 dev
->usb_devh
= devh
;
309 cb_data
= malloc(sizeof(struct hmcfgusb_cb_data
));
311 perror("Can't allocate memory for hmcfgusb_cb_data");
316 memset(cb_data
, 0, sizeof(struct hmcfgusb_cb_data
));
320 cb_data
->data
= data
;
322 dev
->transfer
= hmcfgusb_prepare_int(devh
, hmcfgusb_interrupt
, cb_data
);
323 if (!dev
->transfer
) {
324 fprintf(stderr
, "Can't prepare async device io!\n");
330 usb_pfd
= libusb_get_pollfds(NULL
);
332 fprintf(stderr
, "Can't get FDset from libusb!\n");
339 for(i
= 0; usb_pfd
[i
]; i
++)
342 dev
->pfd
= malloc(dev
->n_usb_pfd
* sizeof(struct pollfd
));
344 perror("Can't allocate memory for poll-fds");
350 memset(dev
->pfd
, 0, dev
->n_usb_pfd
* sizeof(struct pollfd
));
352 for (i
= 0; i
< dev
->n_usb_pfd
; i
++) {
353 dev
->pfd
[i
].fd
= usb_pfd
[i
]->fd
;
354 dev
->pfd
[i
].events
= usb_pfd
[i
]->events
;
355 dev
->pfd
[i
].revents
= 0;
360 dev
->n_pfd
= dev
->n_usb_pfd
;
367 int hmcfgusb_add_pfd(struct hmcfgusb_dev
*dev
, int fd
, short events
)
370 dev
->pfd
= realloc(dev
->pfd
, dev
->n_pfd
* sizeof(struct pollfd
));
372 perror("Can't realloc poll-fds");
376 dev
->pfd
[dev
->n_pfd
-1].fd
= fd
;
377 dev
->pfd
[dev
->n_pfd
-1].events
= events
;
378 dev
->pfd
[dev
->n_pfd
-1].revents
= 0;
383 int hmcfgusb_poll(struct hmcfgusb_dev
*dev
, int timeout
)
394 memset(&tv
, 0, sizeof(tv
));
395 err
= libusb_get_next_timeout(NULL
, &tv
);
397 fprintf(stderr
, "libusb_get_next_timeout: %s\n", usb_strerror(err
));
400 } else if (err
== 0) {
401 /* No pending timeout or a sane platform */
404 if ((tv
.tv_sec
== 0) && (tv
.tv_usec
== 0)) {
410 for (i
= 0; i
< dev
->n_pfd
; i
++) {
411 dev
->pfd
[i
].revents
= 0;
414 n
= poll(dev
->pfd
, dev
->n_pfd
, tv
.tv_sec
* 1000);
422 for (fd_n
= 0; fd_n
< dev
->n_pfd
; fd_n
++) {
423 if (dev
->pfd
[fd_n
].revents
) {
424 if (fd_n
< dev
->n_usb_pfd
) {
429 return dev
->pfd
[fd_n
].fd
;
437 memset(&tv
, 0, sizeof(tv
));
438 err
= libusb_handle_events_timeout_completed(NULL
, &tv
, NULL
);
440 fprintf(stderr
, "libusb_handle_events_timeout_completed: %s\n", usb_strerror(err
));
448 fprintf(stderr
, "closing device-connection due to error %d\n", quit
);
455 void hmcfgusb_close(struct hmcfgusb_dev
*dev
)
460 libusb_cancel_transfer(dev
->transfer
);
463 err
= libusb_release_interface(dev
->usb_devh
, INTERFACE
);
465 fprintf(stderr
, "Can't release interface: %s\n", usb_strerror(err
));
468 libusb_close(dev
->usb_devh
);
475 void hmcfgusb_set_debug(int d
)