]> cvs.zerfleddert.de Git - hmcfgusb/blob - hmcfgusb.c
add USB send-duration to debug output
[hmcfgusb] / hmcfgusb.c
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>
32 #include <sys/time.h>
33 #include <libusb-1.0/libusb.h>
34
35 #include "hexdump.h"
36 #include "hmcfgusb.h"
37
38 #define USB_TIMEOUT 10000
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
50 #define INTERFACE 0
51
52 static int quit = 0;
53 static int debug = 0;
54
55 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
56 static 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
94 static 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
123 err = libusb_detach_kernel_driver(devh, INTERFACE);
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
129 err = libusb_claim_interface(devh, INTERFACE);
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
143 int hmcfgusb_send(struct hmcfgusb_dev *usbdev, unsigned char* send_data, int len, int done)
144 {
145 int err;
146 int cnt;
147 struct timeval tv_start, tv_end;
148 int msec;
149
150 if (debug) {
151 hexdump(send_data, len, "USB < ");
152 gettimeofday(&tv_start, NULL);
153 }
154
155 err = libusb_interrupt_transfer(usbdev->usb_devh, EP_OUT, send_data, len, &cnt, USB_TIMEOUT);
156 if (err) {
157 fprintf(stderr, "Can't send data: %s\n", usb_strerror(err));
158 return 0;
159 }
160
161 if (done) {
162 err = libusb_interrupt_transfer(usbdev->usb_devh, EP_OUT, send_data, 0, &cnt, USB_TIMEOUT);
163 if (err) {
164 fprintf(stderr, "Can't send data: %s\n", usb_strerror(err));
165 return 0;
166 }
167 }
168
169 if (debug) {
170 gettimeofday(&tv_end, NULL);
171 msec = ((tv_end.tv_sec-tv_start.tv_sec)*1000)+((tv_end.tv_usec-tv_start.tv_usec)/1000);
172 fprintf(stderr, "send took %dms!\n", msec);
173 }
174
175 return 1;
176 }
177
178 static struct libusb_transfer *hmcfgusb_prepare_int(libusb_device_handle *devh, libusb_transfer_cb_fn cb, void *data)
179 {
180 unsigned char *data_buf;
181 struct libusb_transfer *transfer;
182 int err;
183
184 data_buf = malloc(ASYNC_SIZE);
185 if (!data_buf) {
186 fprintf(stderr, "Can't allocate memory for data-buffer!\n");
187 return NULL;
188 }
189
190 transfer = libusb_alloc_transfer(0);
191 if (!transfer) {
192 fprintf(stderr, "Can't allocate memory for usb-transfer!\n");
193 free(data_buf);
194 return NULL;
195 }
196
197 libusb_fill_interrupt_transfer(transfer, devh, EP_IN,
198 data_buf, ASYNC_SIZE, cb, data, USB_TIMEOUT);
199
200 transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK | LIBUSB_TRANSFER_FREE_BUFFER;
201
202 err = libusb_submit_transfer(transfer);
203 if (err != 0) {
204 fprintf(stderr, "Can't submit transfer: %s\n", usb_strerror(err));
205 libusb_free_transfer(transfer);
206 return NULL;
207 }
208
209 return transfer;
210 }
211
212 struct hmcfgusb_cb_data {
213 struct hmcfgusb_dev *dev;
214 hmcfgusb_cb_fn cb;
215 void *data;
216 };
217
218 static void LIBUSB_CALL hmcfgusb_interrupt(struct libusb_transfer *transfer)
219 {
220 int err;
221 struct hmcfgusb_cb_data *cb_data;
222
223 cb_data = transfer->user_data;
224
225 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
226 if (transfer->status != LIBUSB_TRANSFER_TIMED_OUT) {
227 fprintf(stderr, "Interrupt transfer not completed: %d!\n", transfer->status);
228 quit = EIO;
229
230 if (cb_data && cb_data->dev && cb_data->dev->transfer) {
231 libusb_free_transfer(cb_data->dev->transfer);
232 cb_data->dev->transfer = NULL;
233 free(cb_data);
234 }
235 return;
236 }
237 } else {
238 if (cb_data && cb_data->cb) {
239 if (debug)
240 hexdump(transfer->buffer, transfer->actual_length, "USB > ");
241
242 if (!cb_data->cb(transfer->buffer, transfer->actual_length, cb_data->data)) {
243 quit = EIO;
244
245 if (cb_data && cb_data->dev && cb_data->dev->transfer) {
246 libusb_free_transfer(cb_data->dev->transfer);
247 cb_data->dev->transfer = NULL;
248 free(cb_data);
249 }
250
251 return;
252 }
253 } else {
254 hexdump(transfer->buffer, transfer->actual_length, "> ");
255 }
256 }
257
258 err = libusb_submit_transfer(transfer);
259 if (err != 0) {
260 fprintf(stderr, "Can't re-submit transfer: %s\n", usb_strerror(err));
261 libusb_free_transfer(transfer);
262 cb_data->dev->transfer = NULL;
263 free(cb_data);
264 }
265 }
266
267 struct hmcfgusb_dev *hmcfgusb_init(hmcfgusb_cb_fn cb, void *data)
268 {
269 libusb_device_handle *devh = NULL;
270 const struct libusb_pollfd **usb_pfd = NULL;
271 struct hmcfgusb_dev *dev = NULL;
272 struct hmcfgusb_cb_data *cb_data = NULL;
273 int err;
274 int i;
275
276 err = libusb_init(NULL);
277 if (err != 0) {
278 fprintf(stderr, "Can't initialize libusb: %s\n", usb_strerror(err));
279 return NULL;
280 }
281
282 devh = hmcfgusb_find();
283 if (!devh) {
284 fprintf(stderr, "Can't find/open hmcfgusb!\n");
285 return NULL;
286 }
287
288 dev = malloc(sizeof(struct hmcfgusb_dev));
289 if (!dev) {
290 perror("Can't allocate memory for hmcfgusb_dev");
291 return NULL;
292 }
293
294 memset(dev, 0, sizeof(struct hmcfgusb_dev));
295 dev->usb_devh = devh;
296
297 cb_data = malloc(sizeof(struct hmcfgusb_cb_data));
298 if (!cb_data) {
299 perror("Can't allocate memory for hmcfgusb_cb_data");
300 free(dev);
301 return NULL;
302 }
303
304 memset(cb_data, 0, sizeof(struct hmcfgusb_cb_data));
305
306 cb_data->dev = dev;
307 cb_data->cb = cb;
308 cb_data->data = data;
309
310 dev->transfer = hmcfgusb_prepare_int(devh, hmcfgusb_interrupt, cb_data);
311 if (!dev->transfer) {
312 fprintf(stderr, "Can't prepare async device io!\n");
313 free(dev);
314 free(cb_data);
315 return NULL;
316 }
317
318 usb_pfd = libusb_get_pollfds(NULL);
319 if (!usb_pfd) {
320 fprintf(stderr, "Can't get FDset from libusb!\n");
321 free(dev);
322 free(cb_data);
323 return NULL;
324 }
325
326 dev->n_usb_pfd = 0;
327 for(i = 0; usb_pfd[i]; i++)
328 dev->n_usb_pfd++;
329
330 dev->pfd = malloc(dev->n_usb_pfd * sizeof(struct pollfd));
331 if (!dev->pfd) {
332 perror("Can't allocate memory for poll-fds");
333 free(dev);
334 free(cb_data);
335 return NULL;
336 }
337
338 memset(dev->pfd, 0, dev->n_usb_pfd * sizeof(struct pollfd));
339
340 for (i = 0; i < dev->n_usb_pfd; i++) {
341 dev->pfd[i].fd = usb_pfd[i]->fd;
342 dev->pfd[i].events = usb_pfd[i]->events;
343 dev->pfd[i].revents = 0;
344 }
345
346 free(usb_pfd);
347
348 dev->n_pfd = dev->n_usb_pfd;
349
350 quit = 0;
351
352 return dev;
353 }
354
355 int hmcfgusb_add_pfd(struct hmcfgusb_dev *dev, int fd, short events)
356 {
357 dev->n_pfd++;
358 dev->pfd = realloc(dev->pfd, dev->n_pfd * sizeof(struct pollfd));
359 if (!dev->pfd) {
360 perror("Can't realloc poll-fds");
361 return 0;
362 }
363
364 dev->pfd[dev->n_pfd-1].fd = fd;
365 dev->pfd[dev->n_pfd-1].events = events;
366 dev->pfd[dev->n_pfd-1].revents = 0;
367
368 return 1;
369 }
370
371 int hmcfgusb_poll(struct hmcfgusb_dev *dev, int timeout)
372 {
373 struct timeval tv;
374 int usb_event = 0;
375 int i;
376 int n;
377 int fd_n;
378 int err;
379
380 errno = 0;
381
382 memset(&tv, 0, sizeof(tv));
383 err = libusb_get_next_timeout(NULL, &tv);
384 if (err < 0) {
385 fprintf(stderr, "libusb_get_next_timeout: %s\n", usb_strerror(err));
386 errno = EIO;
387 return -1;
388 } else if (err == 0) {
389 /* No pending timeout or a sane platform */
390 tv.tv_sec = timeout;
391 } else {
392 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) {
393 usb_event = 1;
394 }
395 }
396
397 if (!usb_event) {
398 for (i = 0; i < dev->n_pfd; i++) {
399 dev->pfd[i].revents = 0;
400 }
401
402 n = poll(dev->pfd, dev->n_pfd, tv.tv_sec * 1000);
403 if (n < 0) {
404 perror("poll");
405 errno = 0;
406 return -1;
407 } else if (n == 0) {
408 usb_event = 1;
409 } else {
410 for (fd_n = 0; fd_n < dev->n_pfd; fd_n++) {
411 if (dev->pfd[fd_n].revents) {
412 if (fd_n < dev->n_usb_pfd) {
413 usb_event = 1;
414 break;
415 } else {
416 errno = 0;
417 return dev->pfd[fd_n].fd;
418 }
419 }
420 }
421 }
422 }
423
424 if (usb_event) {
425 memset(&tv, 0, sizeof(tv));
426 err = libusb_handle_events_timeout_completed(NULL, &tv, NULL);
427 if (err < 0) {
428 fprintf(stderr, "libusb_handle_events_timeout_completed: %s\n", usb_strerror(err));
429 errno = EIO;
430 return -1;
431 }
432 }
433
434 errno = 0;
435 if (quit) {
436 fprintf(stderr, "closing device-connection due to error %d\n", quit);
437 errno = quit;
438 }
439
440 return -1;
441 }
442
443 void hmcfgusb_close(struct hmcfgusb_dev *dev)
444 {
445 int err;
446
447 if (dev->transfer) {
448 libusb_cancel_transfer(dev->transfer);
449 }
450
451 err = libusb_release_interface(dev->usb_devh, INTERFACE);
452 if ((err != 0)) {
453 fprintf(stderr, "Can't release interface: %s\n", usb_strerror(err));
454 }
455
456 libusb_close(dev->usb_devh);
457 free(dev->pfd);
458 free(dev);
459
460 libusb_exit(NULL);
461 }
462
463 void hmcfgusb_set_debug(int d)
464 {
465 debug = d;
466 }
Impressum, Datenschutz