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