show textual error
[hmcfgusb] / hmcfgusb.c
CommitLineData
9db2e455
MG
1/* HM-CFG-USB libusb-driver
2 *
3 * Copyright (c) 2013 Michael Gernoth <michael@gernoth.net>
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24#include <string.h>
25#include <stdio.h>
26#include <stdint.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <math.h>
30#include <poll.h>
31#include <errno.h>
f8718b41 32#include <sys/time.h>
9db2e455
MG
33#include <libusb-1.0/libusb.h>
34
35#include "hexdump.h"
36#include "hmcfgusb.h"
37
813afd12 38#define USB_TIMEOUT 10000
9db2e455
MG
39
40#define ID_VENDOR 0x1b1f
41#define ID_PRODUCT 0xc00f
4d0be794 42#define ID_PRODUCT_BL 0xc010
9db2e455
MG
43
44/* TODO: dynamic */
45#define ASYNC_SIZE 0x0040
46#define ASYNC_INTERVAL 32
47
48#define EP_OUT 0x02
49#define EP_IN 0x83
50
813afd12
MG
51#define INTERFACE 0
52
9db2e455 53static int quit = 0;
e75295bb 54static int debug = 0;
9db2e455
MG
55
56/* Not in all libusb-1.0 versions, so we have to roll our own :-( */
57static char * usb_strerror(int e)
58{
59 static char unknerr[256];
60
61 switch (e) {
62 case LIBUSB_SUCCESS:
63 return "Success";
64 case LIBUSB_ERROR_IO:
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:
79 return "Overflow";
80 case LIBUSB_ERROR_PIPE:
81 return "Pipe error";
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:
89 return "Other error";
90 };
91 snprintf(unknerr, sizeof(unknerr), "Unknown error code %d / 0x%02x", e, e);
92 return unknerr;
93}
94
95static libusb_device_handle *hmcfgusb_find() {
96 libusb_device_handle *devh = NULL;
97 libusb_device **list;
98 ssize_t cnt;
99 ssize_t i;
100 int err;
101
102 cnt = libusb_get_device_list(NULL, &list);
103 if (cnt < 0) {
104 fprintf(stderr, "Can't get USB device list: %d\n", (int)cnt);
105 return NULL;
106 }
107
108 for (i = 0; i < cnt; i++){
109 struct libusb_device_descriptor desc;
110
111 err = libusb_get_device_descriptor(list[i], &desc);
112 if (err)
113 continue;
114
4d0be794
MG
115 if ((desc.idVendor == ID_VENDOR) &&
116 ((desc.idProduct == ID_PRODUCT) || (desc.idProduct == ID_PRODUCT_BL))) {
9db2e455
MG
117 libusb_device *dev = list[i];
118
119 err = libusb_open(dev, &devh);
120 if (err) {
121 fprintf(stderr, "Can't open device: %s\n", usb_strerror(err));
122 return NULL;
123 }
124
813afd12 125 err = libusb_detach_kernel_driver(devh, INTERFACE);
9db2e455
MG
126 if ((err != 0) && (err != LIBUSB_ERROR_NOT_FOUND)) {
127 fprintf(stderr, "Can't detach kernel driver: %s\n", usb_strerror(err));
128 return NULL;
129 }
130
813afd12 131 err = libusb_claim_interface(devh, INTERFACE);
9db2e455
MG
132 if ((err != 0)) {
133 fprintf(stderr, "Can't claim interface: %s\n", usb_strerror(err));
134 return NULL;
135 }
136
137 return devh;
138 }
139
140 }
141
142 return NULL;
143}
144
6262005e 145int hmcfgusb_send_null_frame(struct hmcfgusb_dev *usbdev, int silent)
2a6eaefc
MG
146{
147 int err;
148 int cnt;
149
150 err = libusb_interrupt_transfer(usbdev->usb_devh, EP_OUT, NULL, 0, &cnt, USB_TIMEOUT);
6262005e
MG
151 if (err && (!silent)) {
152 fprintf(stderr, "Can't send null frame: %s\n", usb_strerror(err));
2a6eaefc
MG
153 return 0;
154 }
155
156 return 1;
157}
158
9db2e455
MG
159int hmcfgusb_send(struct hmcfgusb_dev *usbdev, unsigned char* send_data, int len, int done)
160{
161 int err;
162 int cnt;
f8718b41
MG
163 struct timeval tv_start, tv_end;
164 int msec;
9db2e455 165
f8718b41 166 if (debug) {
627e3f33 167 hexdump(send_data, len, "USB < ");
f8718b41
MG
168 }
169
d200d8ca
MG
170 gettimeofday(&tv_start, NULL);
171
9db2e455
MG
172 err = libusb_interrupt_transfer(usbdev->usb_devh, EP_OUT, send_data, len, &cnt, USB_TIMEOUT);
173 if (err) {
174 fprintf(stderr, "Can't send data: %s\n", usb_strerror(err));
9db2e455
MG
175 return 0;
176 }
177
178 if (done) {
6262005e 179 if (!hmcfgusb_send_null_frame(usbdev, 0)) {
9db2e455
MG
180 return 0;
181 }
182 }
183
d200d8ca
MG
184 gettimeofday(&tv_end, NULL);
185 msec = ((tv_end.tv_sec-tv_start.tv_sec)*1000)+((tv_end.tv_usec-tv_start.tv_usec)/1000);
186
187 if (msec > 100) {
6df7655e 188 fprintf(stderr, "usb-transfer took more than 100ms (%dms), this may lead to timing problems!\n", msec);
d200d8ca
MG
189 } else if (debug) {
190 fprintf(stderr, "usb-transfer took %dms!\n", msec);
f8718b41
MG
191 }
192
e0a7146e 193 return 1;
9db2e455
MG
194}
195
196static struct libusb_transfer *hmcfgusb_prepare_int(libusb_device_handle *devh, libusb_transfer_cb_fn cb, void *data)
197{
198 unsigned char *data_buf;
199 struct libusb_transfer *transfer;
200 int err;
201
202 data_buf = malloc(ASYNC_SIZE);
203 if (!data_buf) {
204 fprintf(stderr, "Can't allocate memory for data-buffer!\n");
205 return NULL;
206 }
207
208 transfer = libusb_alloc_transfer(0);
209 if (!transfer) {
210 fprintf(stderr, "Can't allocate memory for usb-transfer!\n");
211 free(data_buf);
212 return NULL;
213 }
214
215 libusb_fill_interrupt_transfer(transfer, devh, EP_IN,
216 data_buf, ASYNC_SIZE, cb, data, USB_TIMEOUT);
217
813afd12 218 transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK | LIBUSB_TRANSFER_FREE_BUFFER;
9db2e455
MG
219
220 err = libusb_submit_transfer(transfer);
221 if (err != 0) {
222 fprintf(stderr, "Can't submit transfer: %s\n", usb_strerror(err));
223 libusb_free_transfer(transfer);
9db2e455
MG
224 return NULL;
225 }
226
227 return transfer;
228}
229
230struct hmcfgusb_cb_data {
813afd12 231 struct hmcfgusb_dev *dev;
9db2e455
MG
232 hmcfgusb_cb_fn cb;
233 void *data;
234};
235
236static void LIBUSB_CALL hmcfgusb_interrupt(struct libusb_transfer *transfer)
237{
238 int err;
239 struct hmcfgusb_cb_data *cb_data;
240
813afd12
MG
241 cb_data = transfer->user_data;
242
9db2e455
MG
243 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
244 if (transfer->status != LIBUSB_TRANSFER_TIMED_OUT) {
032d6050 245 fprintf(stderr, "Interrupt transfer not completed: %s!\n", usb_strerror(transfer->status));
9db2e455 246 quit = EIO;
813afd12
MG
247
248 if (cb_data && cb_data->dev && cb_data->dev->transfer) {
249 libusb_free_transfer(cb_data->dev->transfer);
250 cb_data->dev->transfer = NULL;
4cadd56d 251 free(cb_data);
813afd12 252 }
9db2e455
MG
253 return;
254 }
9db2e455 255 } else {
27bb301b 256 if (cb_data && cb_data->cb) {
e0a7146e 257 if (debug)
627e3f33 258 hexdump(transfer->buffer, transfer->actual_length, "USB > ");
4371275b
MG
259
260 if (!cb_data->cb(transfer->buffer, transfer->actual_length, cb_data->data)) {
261 quit = EIO;
262
263 if (cb_data && cb_data->dev && cb_data->dev->transfer) {
264 libusb_free_transfer(cb_data->dev->transfer);
265 cb_data->dev->transfer = NULL;
4cadd56d 266 free(cb_data);
4371275b
MG
267 }
268
269 return;
270 }
27bb301b 271 } else {
e0a7146e 272 hexdump(transfer->buffer, transfer->actual_length, "> ");
27bb301b 273 }
9db2e455
MG
274 }
275
276 err = libusb_submit_transfer(transfer);
277 if (err != 0) {
278 fprintf(stderr, "Can't re-submit transfer: %s\n", usb_strerror(err));
9db2e455 279 libusb_free_transfer(transfer);
4cadd56d
MG
280 cb_data->dev->transfer = NULL;
281 free(cb_data);
9db2e455
MG
282 }
283}
284
285struct hmcfgusb_dev *hmcfgusb_init(hmcfgusb_cb_fn cb, void *data)
286{
287 libusb_device_handle *devh = NULL;
288 const struct libusb_pollfd **usb_pfd = NULL;
289 struct hmcfgusb_dev *dev = NULL;
290 struct hmcfgusb_cb_data *cb_data = NULL;
291 int err;
292 int i;
293
294 err = libusb_init(NULL);
295 if (err != 0) {
296 fprintf(stderr, "Can't initialize libusb: %s\n", usb_strerror(err));
297 return NULL;
298 }
299
300 devh = hmcfgusb_find();
301 if (!devh) {
302 fprintf(stderr, "Can't find/open hmcfgusb!\n");
303 return NULL;
304 }
305
306 dev = malloc(sizeof(struct hmcfgusb_dev));
307 if (!dev) {
308 perror("Can't allocate memory for hmcfgusb_dev");
309 return NULL;
310 }
311
312 memset(dev, 0, sizeof(struct hmcfgusb_dev));
313 dev->usb_devh = devh;
314
315 cb_data = malloc(sizeof(struct hmcfgusb_cb_data));
316 if (!cb_data) {
317 perror("Can't allocate memory for hmcfgusb_cb_data");
4cadd56d 318 free(dev);
9db2e455
MG
319 return NULL;
320 }
321
322 memset(cb_data, 0, sizeof(struct hmcfgusb_cb_data));
323
813afd12 324 cb_data->dev = dev;
9db2e455
MG
325 cb_data->cb = cb;
326 cb_data->data = data;
327
328 dev->transfer = hmcfgusb_prepare_int(devh, hmcfgusb_interrupt, cb_data);
329 if (!dev->transfer) {
330 fprintf(stderr, "Can't prepare async device io!\n");
4cadd56d
MG
331 free(dev);
332 free(cb_data);
9db2e455
MG
333 return NULL;
334 }
335
336 usb_pfd = libusb_get_pollfds(NULL);
337 if (!usb_pfd) {
338 fprintf(stderr, "Can't get FDset from libusb!\n");
339 free(dev);
4cadd56d 340 free(cb_data);
9db2e455
MG
341 return NULL;
342 }
343
344 dev->n_usb_pfd = 0;
345 for(i = 0; usb_pfd[i]; i++)
346 dev->n_usb_pfd++;
347
348 dev->pfd = malloc(dev->n_usb_pfd * sizeof(struct pollfd));
349 if (!dev->pfd) {
350 perror("Can't allocate memory for poll-fds");
4cadd56d
MG
351 free(dev);
352 free(cb_data);
9db2e455
MG
353 return NULL;
354 }
355
356 memset(dev->pfd, 0, dev->n_usb_pfd * sizeof(struct pollfd));
357
358 for (i = 0; i < dev->n_usb_pfd; i++) {
359 dev->pfd[i].fd = usb_pfd[i]->fd;
360 dev->pfd[i].events = usb_pfd[i]->events;
361 dev->pfd[i].revents = 0;
362 }
363
364 free(usb_pfd);
365
366 dev->n_pfd = dev->n_usb_pfd;
367
f16395da
MG
368 quit = 0;
369
9db2e455
MG
370 return dev;
371}
372
373int hmcfgusb_add_pfd(struct hmcfgusb_dev *dev, int fd, short events)
374{
375 dev->n_pfd++;
376 dev->pfd = realloc(dev->pfd, dev->n_pfd * sizeof(struct pollfd));
377 if (!dev->pfd) {
378 perror("Can't realloc poll-fds");
379 return 0;
380 }
381
382 dev->pfd[dev->n_pfd-1].fd = fd;
383 dev->pfd[dev->n_pfd-1].events = events;
384 dev->pfd[dev->n_pfd-1].revents = 0;
385
386 return 1;
387}
388
389int hmcfgusb_poll(struct hmcfgusb_dev *dev, int timeout)
390{
391 struct timeval tv;
392 int usb_event = 0;
393 int i;
394 int n;
395 int fd_n;
396 int err;
397
398 errno = 0;
399
400 memset(&tv, 0, sizeof(tv));
401 err = libusb_get_next_timeout(NULL, &tv);
402 if (err < 0) {
403 fprintf(stderr, "libusb_get_next_timeout: %s\n", usb_strerror(err));
404 errno = EIO;
405 return -1;
406 } else if (err == 0) {
407 /* No pending timeout or a sane platform */
408 tv.tv_sec = timeout;
409 } else {
410 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) {
411 usb_event = 1;
ec30827c
MG
412 } else if (tv.tv_sec > timeout) {
413 tv.tv_sec = timeout;
414 tv.tv_usec = 0;
9db2e455
MG
415 }
416 }
417
418 if (!usb_event) {
419 for (i = 0; i < dev->n_pfd; i++) {
420 dev->pfd[i].revents = 0;
421 }
422
423 n = poll(dev->pfd, dev->n_pfd, tv.tv_sec * 1000);
424 if (n < 0) {
425 perror("poll");
816f5cd2 426 errno = 0;
9db2e455
MG
427 return -1;
428 } else if (n == 0) {
429 usb_event = 1;
430 } else {
431 for (fd_n = 0; fd_n < dev->n_pfd; fd_n++) {
432 if (dev->pfd[fd_n].revents) {
433 if (fd_n < dev->n_usb_pfd) {
434 usb_event = 1;
435 break;
436 } else {
816f5cd2 437 errno = 0;
9db2e455
MG
438 return dev->pfd[fd_n].fd;
439 }
440 }
441 }
442 }
443 }
444
445 if (usb_event) {
446 memset(&tv, 0, sizeof(tv));
447 err = libusb_handle_events_timeout_completed(NULL, &tv, NULL);
448 if (err < 0) {
27bb301b 449 fprintf(stderr, "libusb_handle_events_timeout_completed: %s\n", usb_strerror(err));
9db2e455
MG
450 errno = EIO;
451 return -1;
452 }
453 }
454
816f5cd2
MG
455 errno = 0;
456 if (quit) {
457 fprintf(stderr, "closing device-connection due to error %d\n", quit);
9db2e455 458 errno = quit;
816f5cd2 459 }
9db2e455
MG
460
461 return -1;
462}
813afd12
MG
463
464void hmcfgusb_close(struct hmcfgusb_dev *dev)
465{
466 int err;
467
468 if (dev->transfer) {
469 libusb_cancel_transfer(dev->transfer);
470 }
471
472 err = libusb_release_interface(dev->usb_devh, INTERFACE);
473 if ((err != 0)) {
474 fprintf(stderr, "Can't release interface: %s\n", usb_strerror(err));
475 }
476
477 libusb_close(dev->usb_devh);
e0a7146e 478 free(dev->pfd);
813afd12
MG
479 free(dev);
480
481 libusb_exit(NULL);
482}
e75295bb
MG
483
484void hmcfgusb_set_debug(int d)
485{
486 debug = d;
487}
Impressum, Datenschutz