]> cvs.zerfleddert.de Git - hmcfgusb/blame - hmcfgusb.c
Add support for printing the transfer status
[hmcfgusb] / hmcfgusb.c
CommitLineData
9db2e455
MG
1/* HM-CFG-USB libusb-driver
2 *
7ba4ea19 3 * Copyright (c) 2013-16 Michael Gernoth <michael@gernoth.net>
9db2e455
MG
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
37c97e62
MG
35/* Workaround for old libusb-1.0 */
36#ifndef LIBUSB_CALL
37#define LIBUSB_CALL
38#define libusb_handle_events_timeout_completed(ctx, tv, x) libusb_handle_events_timeout(ctx, tv)
39#endif
40
9db2e455
MG
41#include "hexdump.h"
42#include "hmcfgusb.h"
43
813afd12 44#define USB_TIMEOUT 10000
9db2e455
MG
45
46#define ID_VENDOR 0x1b1f
47#define ID_PRODUCT 0xc00f
4d0be794 48#define ID_PRODUCT_BL 0xc010
9db2e455
MG
49
50/* TODO: dynamic */
51#define ASYNC_SIZE 0x0040
52#define ASYNC_INTERVAL 32
53
54#define EP_OUT 0x02
55#define EP_IN 0x83
56
813afd12
MG
57#define INTERFACE 0
58
9db2e455 59static int quit = 0;
e75295bb 60static int debug = 0;
018f85fa 61static int libusb_initialized = 0;
9db2e455
MG
62
63/* Not in all libusb-1.0 versions, so we have to roll our own :-( */
64static char * usb_strerror(int e)
65{
66 static char unknerr[256];
67
68 switch (e) {
69 case LIBUSB_SUCCESS:
70 return "Success";
71 case LIBUSB_ERROR_IO:
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:
86 return "Overflow";
87 case LIBUSB_ERROR_PIPE:
88 return "Pipe error";
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:
96 return "Other error";
97 };
98 snprintf(unknerr, sizeof(unknerr), "Unknown error code %d / 0x%02x", e, e);
99 return unknerr;
100}
101
867564c5
FF
102static char * usb_str_transfer_status(int e)
103{
104 static char unknerr[256];
105
106 switch (e) {
107 case LIBUSB_TRANSFER_COMPLETED:
108 return "Transfer completed";
109 case LIBUSB_TRANSFER_ERROR:
110 return "Transfer error";
111 case LIBUSB_TRANSFER_TIMED_OUT:
112 return "Transfer timed out";
113 case LIBUSB_TRANSFER_CANCELLED:
114 return "Transfer cancelled";
115 case LIBUSB_TRANSFER_STALL:
116 return "No device";
117 case LIBUSB_TRANSFER_OVERFLOW:
118 return "Transfer overflow";
119 };
120 snprintf(unknerr, sizeof(unknerr), "Unknown transfer status %d / 0x%02x", e, e);
121 return unknerr;
122}
123
f51714be 124static libusb_device_handle *hmcfgusb_find(int vid, int pid, char *serial) {
9db2e455
MG
125 libusb_device_handle *devh = NULL;
126 libusb_device **list;
127 ssize_t cnt;
128 ssize_t i;
129 int err;
130
131 cnt = libusb_get_device_list(NULL, &list);
132 if (cnt < 0) {
133 fprintf(stderr, "Can't get USB device list: %d\n", (int)cnt);
134 return NULL;
135 }
136
137 for (i = 0; i < cnt; i++){
138 struct libusb_device_descriptor desc;
139
140 err = libusb_get_device_descriptor(list[i], &desc);
141 if (err)
142 continue;
143
62f25bae 144 if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
9db2e455
MG
145 libusb_device *dev = list[i];
146
147 err = libusb_open(dev, &devh);
148 if (err) {
149 fprintf(stderr, "Can't open device: %s\n", usb_strerror(err));
018f85fa 150 libusb_free_device_list(list, 1);
9db2e455
MG
151 return NULL;
152 }
153
f51714be
MG
154 if (serial) {
155 if (desc.iSerialNumber > 0) {
156 uint8_t devSerial[256];
157 err = libusb_get_string_descriptor_ascii(devh, desc.iSerialNumber, devSerial, sizeof(devSerial));
158 if (err < 0) {
159 fprintf(stderr, "Can't read serial-number: %s\n", usb_strerror(err));
160 libusb_close(devh);
161 libusb_free_device_list(list, 1);
162 return NULL;
163 }
164 if (strcmp((char*)devSerial, (char*)serial)) {
165 libusb_close(devh);
166 continue;
167 }
168 } else {
169 libusb_close(devh);
170 continue;
171 }
172 }
173
813afd12 174 err = libusb_detach_kernel_driver(devh, INTERFACE);
9db2e455
MG
175 if ((err != 0) && (err != LIBUSB_ERROR_NOT_FOUND)) {
176 fprintf(stderr, "Can't detach kernel driver: %s\n", usb_strerror(err));
018f85fa
MG
177 libusb_close(devh);
178 libusb_free_device_list(list, 1);
9db2e455
MG
179 return NULL;
180 }
181
813afd12 182 err = libusb_claim_interface(devh, INTERFACE);
9db2e455
MG
183 if ((err != 0)) {
184 fprintf(stderr, "Can't claim interface: %s\n", usb_strerror(err));
018f85fa
MG
185 libusb_close(devh);
186 libusb_free_device_list(list, 1);
9db2e455
MG
187 return NULL;
188 }
189
018f85fa 190 libusb_free_device_list(list, 0);
9db2e455
MG
191 return devh;
192 }
193
194 }
195
018f85fa 196 libusb_free_device_list(list, 1);
9db2e455
MG
197 return NULL;
198}
199
6262005e 200int hmcfgusb_send_null_frame(struct hmcfgusb_dev *usbdev, int silent)
2a6eaefc
MG
201{
202 int err;
203 int cnt;
3435bdb6 204 unsigned char out[0x40];
2a6eaefc 205
3435bdb6
MG
206 memset(out, 0, sizeof(out));
207
3d5b8e70 208 err = libusb_interrupt_transfer(usbdev->usb_devh, EP_OUT, out, 0, &cnt, USB_TIMEOUT);
6262005e
MG
209 if (err && (!silent)) {
210 fprintf(stderr, "Can't send null frame: %s\n", usb_strerror(err));
2a6eaefc
MG
211 return 0;
212 }
213
214 return 1;
215}
216
9db2e455
MG
217int hmcfgusb_send(struct hmcfgusb_dev *usbdev, unsigned char* send_data, int len, int done)
218{
219 int err;
220 int cnt;
f8718b41
MG
221 struct timeval tv_start, tv_end;
222 int msec;
9db2e455 223
f8718b41 224 if (debug) {
627e3f33 225 hexdump(send_data, len, "USB < ");
f8718b41
MG
226 }
227
d200d8ca
MG
228 gettimeofday(&tv_start, NULL);
229
9db2e455
MG
230 err = libusb_interrupt_transfer(usbdev->usb_devh, EP_OUT, send_data, len, &cnt, USB_TIMEOUT);
231 if (err) {
232 fprintf(stderr, "Can't send data: %s\n", usb_strerror(err));
9db2e455
MG
233 return 0;
234 }
235
236 if (done) {
6262005e 237 if (!hmcfgusb_send_null_frame(usbdev, 0)) {
9db2e455
MG
238 return 0;
239 }
240 }
241
d200d8ca
MG
242 gettimeofday(&tv_end, NULL);
243 msec = ((tv_end.tv_sec-tv_start.tv_sec)*1000)+((tv_end.tv_usec-tv_start.tv_usec)/1000);
244
245 if (msec > 100) {
6df7655e 246 fprintf(stderr, "usb-transfer took more than 100ms (%dms), this may lead to timing problems!\n", msec);
d200d8ca
MG
247 } else if (debug) {
248 fprintf(stderr, "usb-transfer took %dms!\n", msec);
f8718b41
MG
249 }
250
e0a7146e 251 return 1;
9db2e455
MG
252}
253
b5e57d26 254static struct libusb_transfer *hmcfgusb_prepare_int(libusb_device_handle *devh, libusb_transfer_cb_fn cb, void *data, int in_size)
9db2e455
MG
255{
256 unsigned char *data_buf;
257 struct libusb_transfer *transfer;
258 int err;
259
b5e57d26 260 data_buf = malloc(in_size);
9db2e455
MG
261 if (!data_buf) {
262 fprintf(stderr, "Can't allocate memory for data-buffer!\n");
263 return NULL;
264 }
265
266 transfer = libusb_alloc_transfer(0);
267 if (!transfer) {
268 fprintf(stderr, "Can't allocate memory for usb-transfer!\n");
269 free(data_buf);
270 return NULL;
271 }
272
273 libusb_fill_interrupt_transfer(transfer, devh, EP_IN,
b5e57d26 274 data_buf, in_size, cb, data, USB_TIMEOUT);
9db2e455 275
bafd75c9 276 transfer->flags = LIBUSB_TRANSFER_FREE_BUFFER;
9db2e455
MG
277
278 err = libusb_submit_transfer(transfer);
279 if (err != 0) {
280 fprintf(stderr, "Can't submit transfer: %s\n", usb_strerror(err));
281 libusb_free_transfer(transfer);
9db2e455
MG
282 return NULL;
283 }
284
285 return transfer;
286}
287
288struct hmcfgusb_cb_data {
813afd12 289 struct hmcfgusb_dev *dev;
9db2e455
MG
290 hmcfgusb_cb_fn cb;
291 void *data;
292};
293
294static void LIBUSB_CALL hmcfgusb_interrupt(struct libusb_transfer *transfer)
295{
296 int err;
297 struct hmcfgusb_cb_data *cb_data;
298
813afd12
MG
299 cb_data = transfer->user_data;
300
9db2e455
MG
301 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
302 if (transfer->status != LIBUSB_TRANSFER_TIMED_OUT) {
018f85fa 303 if (transfer->status != LIBUSB_TRANSFER_CANCELLED)
867564c5 304 fprintf(stderr, "Interrupt transfer not completed: %s!\n", usb_str_transfer_status(transfer->status));
018f85fa 305
9db2e455 306 quit = EIO;
1c346883 307 goto out;
9db2e455 308 }
9db2e455 309 } else {
27bb301b 310 if (cb_data && cb_data->cb) {
e0a7146e 311 if (debug)
627e3f33 312 hexdump(transfer->buffer, transfer->actual_length, "USB > ");
4371275b
MG
313
314 if (!cb_data->cb(transfer->buffer, transfer->actual_length, cb_data->data)) {
315 quit = EIO;
1c346883 316 goto out;
4371275b 317 }
27bb301b 318 } else {
e0a7146e 319 hexdump(transfer->buffer, transfer->actual_length, "> ");
27bb301b 320 }
9db2e455
MG
321 }
322
323 err = libusb_submit_transfer(transfer);
324 if (err != 0) {
325 fprintf(stderr, "Can't re-submit transfer: %s\n", usb_strerror(err));
1c346883
MG
326 goto out;
327 }
328
329 return;
330
331out:
332 libusb_free_transfer(transfer);
333 if (cb_data) {
334 if (cb_data->dev && cb_data->dev->transfer) {
335 cb_data->dev->transfer = NULL;
325ed703 336 }
1c346883 337 free(cb_data);
9db2e455
MG
338 }
339}
340
f51714be 341struct hmcfgusb_dev *hmcfgusb_init(hmcfgusb_cb_fn cb, void *data, char *serial)
9db2e455
MG
342{
343 libusb_device_handle *devh = NULL;
344 const struct libusb_pollfd **usb_pfd = NULL;
345 struct hmcfgusb_dev *dev = NULL;
346 struct hmcfgusb_cb_data *cb_data = NULL;
62f25bae 347 int bootloader = 0;
9db2e455
MG
348 int err;
349 int i;
350
018f85fa
MG
351 if (!libusb_initialized) {
352 err = libusb_init(NULL);
353 if (err != 0) {
354 fprintf(stderr, "Can't initialize libusb: %s\n", usb_strerror(err));
355 return NULL;
356 }
9db2e455 357 }
018f85fa 358 libusb_initialized = 1;
9db2e455 359
f51714be 360 devh = hmcfgusb_find(ID_VENDOR, ID_PRODUCT, serial);
9db2e455 361 if (!devh) {
f51714be 362 devh = hmcfgusb_find(ID_VENDOR, ID_PRODUCT_BL, serial);
62f25bae 363 if (!devh) {
f51714be
MG
364 if (serial) {
365 fprintf(stderr, "Can't find/open HM-CFG-USB with serial %s!\n", serial);
366 } else {
367 fprintf(stderr, "Can't find/open HM-CFG-USB!\n");
368 }
018f85fa
MG
369#ifdef NEED_LIBUSB_EXIT
370 hmcfgusb_exit();
371#endif
62f25bae
MG
372 return NULL;
373 }
374 bootloader = 1;
9db2e455
MG
375 }
376
377 dev = malloc(sizeof(struct hmcfgusb_dev));
378 if (!dev) {
379 perror("Can't allocate memory for hmcfgusb_dev");
211fd7e5 380 libusb_close(devh);
018f85fa
MG
381#ifdef NEED_LIBUSB_EXIT
382 hmcfgusb_exit();
383#endif
9db2e455
MG
384 return NULL;
385 }
386
387 memset(dev, 0, sizeof(struct hmcfgusb_dev));
388 dev->usb_devh = devh;
62f25bae
MG
389 dev->bootloader = bootloader;
390 dev->opened_at = time(NULL);
9db2e455
MG
391
392 cb_data = malloc(sizeof(struct hmcfgusb_cb_data));
393 if (!cb_data) {
394 perror("Can't allocate memory for hmcfgusb_cb_data");
4cadd56d 395 free(dev);
211fd7e5 396 libusb_close(devh);
018f85fa
MG
397#ifdef NEED_LIBUSB_EXIT
398 hmcfgusb_exit();
399#endif
9db2e455
MG
400 return NULL;
401 }
402
403 memset(cb_data, 0, sizeof(struct hmcfgusb_cb_data));
404
813afd12 405 cb_data->dev = dev;
9db2e455
MG
406 cb_data->cb = cb;
407 cb_data->data = data;
408
bafd75c9 409 dev->transfer = hmcfgusb_prepare_int(devh, hmcfgusb_interrupt, cb_data, ASYNC_SIZE);
b5e57d26 410
9db2e455
MG
411 if (!dev->transfer) {
412 fprintf(stderr, "Can't prepare async device io!\n");
4cadd56d
MG
413 free(dev);
414 free(cb_data);
211fd7e5 415 libusb_close(devh);
018f85fa
MG
416#ifdef NEED_LIBUSB_EXIT
417 hmcfgusb_exit();
418#endif
9db2e455
MG
419 return NULL;
420 }
421
422 usb_pfd = libusb_get_pollfds(NULL);
423 if (!usb_pfd) {
424 fprintf(stderr, "Can't get FDset from libusb!\n");
018f85fa
MG
425 libusb_cancel_transfer(dev->transfer);
426 libusb_handle_events(NULL);
9db2e455 427 free(dev);
4cadd56d 428 free(cb_data);
211fd7e5 429 libusb_close(devh);
018f85fa
MG
430#ifdef NEED_LIBUSB_EXIT
431 hmcfgusb_exit();
432#endif
9db2e455
MG
433 return NULL;
434 }
435
436 dev->n_usb_pfd = 0;
437 for(i = 0; usb_pfd[i]; i++)
438 dev->n_usb_pfd++;
439
440 dev->pfd = malloc(dev->n_usb_pfd * sizeof(struct pollfd));
441 if (!dev->pfd) {
442 perror("Can't allocate memory for poll-fds");
018f85fa
MG
443 libusb_cancel_transfer(dev->transfer);
444 libusb_handle_events(NULL);
4cadd56d
MG
445 free(dev);
446 free(cb_data);
211fd7e5 447 libusb_close(devh);
018f85fa
MG
448#ifdef NEED_LIBUSB_EXIT
449 hmcfgusb_exit();
450#endif
9db2e455
MG
451 return NULL;
452 }
453
454 memset(dev->pfd, 0, dev->n_usb_pfd * sizeof(struct pollfd));
455
456 for (i = 0; i < dev->n_usb_pfd; i++) {
457 dev->pfd[i].fd = usb_pfd[i]->fd;
458 dev->pfd[i].events = usb_pfd[i]->events;
459 dev->pfd[i].revents = 0;
460 }
461
462 free(usb_pfd);
463
464 dev->n_pfd = dev->n_usb_pfd;
465
f16395da
MG
466 quit = 0;
467
9db2e455
MG
468 return dev;
469}
470
471int hmcfgusb_add_pfd(struct hmcfgusb_dev *dev, int fd, short events)
472{
473 dev->n_pfd++;
474 dev->pfd = realloc(dev->pfd, dev->n_pfd * sizeof(struct pollfd));
475 if (!dev->pfd) {
476 perror("Can't realloc poll-fds");
477 return 0;
478 }
479
480 dev->pfd[dev->n_pfd-1].fd = fd;
481 dev->pfd[dev->n_pfd-1].events = events;
482 dev->pfd[dev->n_pfd-1].revents = 0;
483
484 return 1;
485}
486
487int hmcfgusb_poll(struct hmcfgusb_dev *dev, int timeout)
488{
489 struct timeval tv;
490 int usb_event = 0;
1e79d00a 491 int timed_out = 0;
9db2e455
MG
492 int i;
493 int n;
494 int fd_n;
495 int err;
496
497 errno = 0;
498
499 memset(&tv, 0, sizeof(tv));
500 err = libusb_get_next_timeout(NULL, &tv);
501 if (err < 0) {
502 fprintf(stderr, "libusb_get_next_timeout: %s\n", usb_strerror(err));
503 errno = EIO;
504 return -1;
505 } else if (err == 0) {
506 /* No pending timeout or a sane platform */
9db2e455
MG
507 } else {
508 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) {
509 usb_event = 1;
3b35a8c1
MG
510 } else if ((tv.tv_sec * 1000) < timeout) {
511 timeout = tv.tv_sec * 1000;
9db2e455
MG
512 }
513 }
514
515 if (!usb_event) {
516 for (i = 0; i < dev->n_pfd; i++) {
517 dev->pfd[i].revents = 0;
518 }
519
3b35a8c1 520 n = poll(dev->pfd, dev->n_pfd, timeout);
9db2e455
MG
521 if (n < 0) {
522 perror("poll");
816f5cd2 523 errno = 0;
9db2e455
MG
524 return -1;
525 } else if (n == 0) {
526 usb_event = 1;
1e79d00a 527 timed_out = 1;
9db2e455
MG
528 } else {
529 for (fd_n = 0; fd_n < dev->n_pfd; fd_n++) {
530 if (dev->pfd[fd_n].revents) {
531 if (fd_n < dev->n_usb_pfd) {
532 usb_event = 1;
533 break;
534 } else {
816f5cd2 535 errno = 0;
9db2e455
MG
536 return dev->pfd[fd_n].fd;
537 }
538 }
539 }
540 }
541 }
542
543 if (usb_event) {
544 memset(&tv, 0, sizeof(tv));
545 err = libusb_handle_events_timeout_completed(NULL, &tv, NULL);
546 if (err < 0) {
27bb301b 547 fprintf(stderr, "libusb_handle_events_timeout_completed: %s\n", usb_strerror(err));
9db2e455
MG
548 errno = EIO;
549 return -1;
550 }
551 }
552
816f5cd2
MG
553 errno = 0;
554 if (quit) {
555 fprintf(stderr, "closing device-connection due to error %d\n", quit);
9db2e455 556 errno = quit;
816f5cd2 557 }
9db2e455 558
1e79d00a
MG
559 if (timed_out)
560 errno = ETIMEDOUT;
561
9db2e455
MG
562 return -1;
563}
813afd12 564
62f25bae
MG
565void hmcfgusb_enter_bootloader(struct hmcfgusb_dev *dev)
566{
567 uint8_t out[ASYNC_SIZE];
568
569 if (dev->bootloader) {
570 fprintf(stderr, "request for bootloader mode, but device already in bootloader!\n");
571 return;
572 }
573
574 memset(out, 0, sizeof(out));
575 out[0] = 'B';
576 hmcfgusb_send(dev, out, sizeof(out), 1);
577
578 return;
579}
580
0ddb3740
MG
581void hmcfgusb_leave_bootloader(struct hmcfgusb_dev *dev)
582{
583 uint8_t out[ASYNC_SIZE];
584
585 if (!dev->bootloader) {
586 fprintf(stderr, "request for leaving bootloader mode, but device already in normal mode!\n");
587 return;
588 }
589
590 memset(out, 0, sizeof(out));
591 out[0] = 'K';
592 hmcfgusb_send(dev, out, sizeof(out), 1);
593
594 return;
595}
596
813afd12
MG
597void hmcfgusb_close(struct hmcfgusb_dev *dev)
598{
599 int err;
600
601 if (dev->transfer) {
602 libusb_cancel_transfer(dev->transfer);
018f85fa 603 libusb_handle_events(NULL);
813afd12
MG
604 }
605
606 err = libusb_release_interface(dev->usb_devh, INTERFACE);
607 if ((err != 0)) {
608 fprintf(stderr, "Can't release interface: %s\n", usb_strerror(err));
609 }
610
611 libusb_close(dev->usb_devh);
018f85fa
MG
612#ifdef NEED_LIBUSB_EXIT
613 hmcfgusb_exit();
614#endif
e0a7146e 615 free(dev->pfd);
813afd12 616 free(dev);
018f85fa 617}
813afd12 618
018f85fa
MG
619void hmcfgusb_exit(void)
620{
621 if (libusb_initialized) {
622 libusb_exit(NULL);
623 libusb_initialized = 0;
624 }
813afd12 625}
e75295bb
MG
626
627void hmcfgusb_set_debug(int d)
628{
629 debug = d;
630}
Impressum, Datenschutz