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