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
32 #include <libusb-1.0/libusb.h>
37 #define USB_TIMEOUT 10000
39 #define ID_VENDOR 0x1b1f
40 #define ID_PRODUCT 0xc00f
43 #define ASYNC_SIZE 0x0040
44 #define ASYNC_INTERVAL 32
51 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
52 static char * usb_strerror(int e
)
54 static char unknerr
[256];
60 return "Input/output error";
61 case LIBUSB_ERROR_INVALID_PARAM
:
62 return "Invalid parameter";
63 case LIBUSB_ERROR_ACCESS
:
64 return "Access denied (insufficient permissions)";
65 case LIBUSB_ERROR_NO_DEVICE
:
66 return "No such device (it may have been disconnected)";
67 case LIBUSB_ERROR_NOT_FOUND
:
68 return "Entity not found";
69 case LIBUSB_ERROR_BUSY
:
70 return "Resource busy";
71 case LIBUSB_ERROR_TIMEOUT
:
72 return "Operation timed out";
73 case LIBUSB_ERROR_OVERFLOW
:
75 case LIBUSB_ERROR_PIPE
:
77 case LIBUSB_ERROR_INTERRUPTED
:
78 return "System call interrupted (perhaps due to signal)";
79 case LIBUSB_ERROR_NO_MEM
:
80 return "Insufficient memory";
81 case LIBUSB_ERROR_NOT_SUPPORTED
:
82 return "Operation not supported or unimplemented on this platform";
83 case LIBUSB_ERROR_OTHER
:
86 snprintf(unknerr
, sizeof(unknerr
), "Unknown error code %d / 0x%02x", e
, e
);
90 static libusb_device_handle
*hmcfgusb_find() {
91 libusb_device_handle
*devh
= NULL
;
97 cnt
= libusb_get_device_list(NULL
, &list
);
99 fprintf(stderr
, "Can't get USB device list: %d\n", (int)cnt
);
103 for (i
= 0; i
< cnt
; i
++){
104 struct libusb_device_descriptor desc
;
106 err
= libusb_get_device_descriptor(list
[i
], &desc
);
110 if ((desc
.idVendor
== ID_VENDOR
) && (desc
.idProduct
== ID_PRODUCT
)) {
111 libusb_device
*dev
= list
[i
];
113 err
= libusb_open(dev
, &devh
);
115 fprintf(stderr
, "Can't open device: %s\n", usb_strerror(err
));
119 err
= libusb_detach_kernel_driver(devh
, 0);
120 if ((err
!= 0) && (err
!= LIBUSB_ERROR_NOT_FOUND
)) {
121 fprintf(stderr
, "Can't detach kernel driver: %s\n", usb_strerror(err
));
125 err
= libusb_claim_interface(devh
, 0);
127 fprintf(stderr
, "Can't claim interface: %s\n", usb_strerror(err
));
139 int hmcfgusb_send(struct hmcfgusb_dev
*usbdev
, unsigned char* send_data
, int len
, int done
)
145 err
= libusb_interrupt_transfer(usbdev
->usb_devh
, EP_OUT
, send_data
, len
, &cnt
, USB_TIMEOUT
);
147 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
148 if (err
== LIBUSB_ERROR_NO_DEVICE
)
154 err
= libusb_interrupt_transfer(usbdev
->usb_devh
, EP_OUT
, send_data
, 0, &cnt
, USB_TIMEOUT
);
156 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
157 if (err
== LIBUSB_ERROR_NO_DEVICE
)
166 static struct libusb_transfer
*hmcfgusb_prepare_int(libusb_device_handle
*devh
, libusb_transfer_cb_fn cb
, void *data
)
168 unsigned char *data_buf
;
169 struct libusb_transfer
*transfer
;
172 data_buf
= malloc(ASYNC_SIZE
);
174 fprintf(stderr
, "Can't allocate memory for data-buffer!\n");
178 transfer
= libusb_alloc_transfer(0);
180 fprintf(stderr
, "Can't allocate memory for usb-transfer!\n");
185 libusb_fill_interrupt_transfer(transfer
, devh
, EP_IN
,
186 data_buf
, ASYNC_SIZE
, cb
, data
, USB_TIMEOUT
);
188 transfer
->flags
= LIBUSB_TRANSFER_SHORT_NOT_OK
;
190 err
= libusb_submit_transfer(transfer
);
192 fprintf(stderr
, "Can't submit transfer: %s\n", usb_strerror(err
));
193 libusb_free_transfer(transfer
);
201 struct hmcfgusb_cb_data
{
206 static void LIBUSB_CALL
hmcfgusb_interrupt(struct libusb_transfer
*transfer
)
209 struct hmcfgusb_cb_data
*cb_data
;
211 if (transfer
->status
!= LIBUSB_TRANSFER_COMPLETED
) {
212 if (transfer
->status
!= LIBUSB_TRANSFER_TIMED_OUT
) {
213 fprintf(stderr
, "Interrupt transfer not completed: %d!\n", transfer
->status
);
218 cb_data
= transfer
->user_data
;
219 if (cb_data
&& cb_data
->cb
) {
220 cb_data
->cb(transfer
->buffer
, transfer
->actual_length
, cb_data
->data
);
222 hexdump(transfer
->buffer
, transfer
->actual_length
, "RECV> ");
226 err
= libusb_submit_transfer(transfer
);
228 fprintf(stderr
, "Can't re-submit transfer: %s\n", usb_strerror(err
));
229 free(transfer
->buffer
);
230 libusb_free_transfer(transfer
);
234 struct hmcfgusb_dev
*hmcfgusb_init(hmcfgusb_cb_fn cb
, void *data
)
236 libusb_device_handle
*devh
= NULL
;
237 const struct libusb_pollfd
**usb_pfd
= NULL
;
238 struct hmcfgusb_dev
*dev
= NULL
;
239 struct hmcfgusb_cb_data
*cb_data
= NULL
;
243 err
= libusb_init(NULL
);
245 fprintf(stderr
, "Can't initialize libusb: %s\n", usb_strerror(err
));
249 devh
= hmcfgusb_find();
251 fprintf(stderr
, "Can't find/open hmcfgusb!\n");
255 dev
= malloc(sizeof(struct hmcfgusb_dev
));
257 perror("Can't allocate memory for hmcfgusb_dev");
261 memset(dev
, 0, sizeof(struct hmcfgusb_dev
));
262 dev
->usb_devh
= devh
;
264 cb_data
= malloc(sizeof(struct hmcfgusb_cb_data
));
266 perror("Can't allocate memory for hmcfgusb_cb_data");
270 memset(cb_data
, 0, sizeof(struct hmcfgusb_cb_data
));
273 cb_data
->data
= data
;
275 dev
->transfer
= hmcfgusb_prepare_int(devh
, hmcfgusb_interrupt
, cb_data
);
276 if (!dev
->transfer
) {
277 fprintf(stderr
, "Can't prepare async device io!\n");
281 usb_pfd
= libusb_get_pollfds(NULL
);
283 fprintf(stderr
, "Can't get FDset from libusb!\n");
289 for(i
= 0; usb_pfd
[i
]; i
++)
292 dev
->pfd
= malloc(dev
->n_usb_pfd
* sizeof(struct pollfd
));
294 perror("Can't allocate memory for poll-fds");
298 memset(dev
->pfd
, 0, dev
->n_usb_pfd
* sizeof(struct pollfd
));
300 for (i
= 0; i
< dev
->n_usb_pfd
; i
++) {
301 dev
->pfd
[i
].fd
= usb_pfd
[i
]->fd
;
302 dev
->pfd
[i
].events
= usb_pfd
[i
]->events
;
303 dev
->pfd
[i
].revents
= 0;
308 dev
->n_pfd
= dev
->n_usb_pfd
;
313 int hmcfgusb_add_pfd(struct hmcfgusb_dev
*dev
, int fd
, short events
)
316 dev
->pfd
= realloc(dev
->pfd
, dev
->n_pfd
* sizeof(struct pollfd
));
318 perror("Can't realloc poll-fds");
322 dev
->pfd
[dev
->n_pfd
-1].fd
= fd
;
323 dev
->pfd
[dev
->n_pfd
-1].events
= events
;
324 dev
->pfd
[dev
->n_pfd
-1].revents
= 0;
329 int hmcfgusb_poll(struct hmcfgusb_dev
*dev
, int timeout
)
340 memset(&tv
, 0, sizeof(tv
));
341 err
= libusb_get_next_timeout(NULL
, &tv
);
343 fprintf(stderr
, "libusb_get_next_timeout: %s\n", usb_strerror(err
));
346 } else if (err
== 0) {
347 /* No pending timeout or a sane platform */
350 if ((tv
.tv_sec
== 0) && (tv
.tv_usec
== 0)) {
356 for (i
= 0; i
< dev
->n_pfd
; i
++) {
357 dev
->pfd
[i
].revents
= 0;
360 n
= poll(dev
->pfd
, dev
->n_pfd
, tv
.tv_sec
* 1000);
367 for (fd_n
= 0; fd_n
< dev
->n_pfd
; fd_n
++) {
368 if (dev
->pfd
[fd_n
].revents
) {
369 if (fd_n
< dev
->n_usb_pfd
) {
373 return dev
->pfd
[fd_n
].fd
;
381 memset(&tv
, 0, sizeof(tv
));
382 err
= libusb_handle_events_timeout_completed(NULL
, &tv
, NULL
);
384 fprintf(stderr
, "libusb_handle_events_timeout_completed: %s\n", usb_strerror(err
));