]> git.zerfleddert.de Git - hmcfgusb/blob - hmcfgusb.c
add delays to stop device from resetting
[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 <libusb-1.0/libusb.h>
33
34 #include "hexdump.h"
35 #include "hmcfgusb.h"
36
37 #define USB_TIMEOUT 10000
38
39 #define ID_VENDOR 0x1b1f
40 #define ID_PRODUCT 0xc00f
41
42 /* TODO: dynamic */
43 #define ASYNC_SIZE 0x0040
44 #define ASYNC_INTERVAL 32
45
46 #define EP_OUT 0x02
47 #define EP_IN 0x83
48
49 #define INTERFACE 0
50
51 static int quit = 0;
52 static int debug = 0;
53
54 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
55 static char * usb_strerror(int e)
56 {
57 static char unknerr[256];
58
59 switch (e) {
60 case LIBUSB_SUCCESS:
61 return "Success";
62 case LIBUSB_ERROR_IO:
63 return "Input/output error";
64 case LIBUSB_ERROR_INVALID_PARAM:
65 return "Invalid parameter";
66 case LIBUSB_ERROR_ACCESS:
67 return "Access denied (insufficient permissions)";
68 case LIBUSB_ERROR_NO_DEVICE:
69 return "No such device (it may have been disconnected)";
70 case LIBUSB_ERROR_NOT_FOUND:
71 return "Entity not found";
72 case LIBUSB_ERROR_BUSY:
73 return "Resource busy";
74 case LIBUSB_ERROR_TIMEOUT:
75 return "Operation timed out";
76 case LIBUSB_ERROR_OVERFLOW:
77 return "Overflow";
78 case LIBUSB_ERROR_PIPE:
79 return "Pipe error";
80 case LIBUSB_ERROR_INTERRUPTED:
81 return "System call interrupted (perhaps due to signal)";
82 case LIBUSB_ERROR_NO_MEM:
83 return "Insufficient memory";
84 case LIBUSB_ERROR_NOT_SUPPORTED:
85 return "Operation not supported or unimplemented on this platform";
86 case LIBUSB_ERROR_OTHER:
87 return "Other error";
88 };
89 snprintf(unknerr, sizeof(unknerr), "Unknown error code %d / 0x%02x", e, e);
90 return unknerr;
91 }
92
93 static libusb_device_handle *hmcfgusb_find() {
94 libusb_device_handle *devh = NULL;
95 libusb_device **list;
96 ssize_t cnt;
97 ssize_t i;
98 int err;
99
100 cnt = libusb_get_device_list(NULL, &list);
101 if (cnt < 0) {
102 fprintf(stderr, "Can't get USB device list: %d\n", (int)cnt);
103 return NULL;
104 }
105
106 for (i = 0; i < cnt; i++){
107 struct libusb_device_descriptor desc;
108
109 err = libusb_get_device_descriptor(list[i], &desc);
110 if (err)
111 continue;
112
113 if ((desc.idVendor == ID_VENDOR) && (desc.idProduct == ID_PRODUCT)) {
114 libusb_device *dev = list[i];
115
116 err = libusb_open(dev, &devh);
117 if (err) {
118 fprintf(stderr, "Can't open device: %s\n", usb_strerror(err));
119 return NULL;
120 }
121
122 err = libusb_detach_kernel_driver(devh, INTERFACE);
123 if ((err != 0) && (err != LIBUSB_ERROR_NOT_FOUND)) {
124 fprintf(stderr, "Can't detach kernel driver: %s\n", usb_strerror(err));
125 return NULL;
126 }
127
128 err = libusb_claim_interface(devh, INTERFACE);
129 if ((err != 0)) {
130 fprintf(stderr, "Can't claim interface: %s\n", usb_strerror(err));
131 return NULL;
132 }
133
134 return devh;
135 }
136
137 }
138
139 return NULL;
140 }
141
142 int hmcfgusb_send(struct hmcfgusb_dev *usbdev, unsigned char* send_data, int len, int done)
143 {
144 int err;
145 int cnt;
146
147 if (debug)
148 hexdump(send_data, len, "USB < ");
149 err = libusb_interrupt_transfer(usbdev->usb_devh, EP_OUT, send_data, len, &cnt, USB_TIMEOUT);
150 if (err) {
151 fprintf(stderr, "Can't send data: %s\n", usb_strerror(err));
152 return 0;
153 }
154
155 usleep(1000);
156
157 if (done) {
158 err = libusb_interrupt_transfer(usbdev->usb_devh, EP_OUT, send_data, 0, &cnt, USB_TIMEOUT);
159 if (err) {
160 fprintf(stderr, "Can't send data: %s\n", usb_strerror(err));
161 return 0;
162 }
163
164 usleep(1000);
165 }
166
167 return 1;
168 }
169
170 static struct libusb_transfer *hmcfgusb_prepare_int(libusb_device_handle *devh, libusb_transfer_cb_fn cb, void *data)
171 {
172 unsigned char *data_buf;
173 struct libusb_transfer *transfer;
174 int err;
175
176 data_buf = malloc(ASYNC_SIZE);
177 if (!data_buf) {
178 fprintf(stderr, "Can't allocate memory for data-buffer!\n");
179 return NULL;
180 }
181
182 transfer = libusb_alloc_transfer(0);
183 if (!transfer) {
184 fprintf(stderr, "Can't allocate memory for usb-transfer!\n");
185 free(data_buf);
186 return NULL;
187 }
188
189 libusb_fill_interrupt_transfer(transfer, devh, EP_IN,
190 data_buf, ASYNC_SIZE, cb, data, USB_TIMEOUT);
191
192 transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK | LIBUSB_TRANSFER_FREE_BUFFER;
193
194 err = libusb_submit_transfer(transfer);
195 if (err != 0) {
196 fprintf(stderr, "Can't submit transfer: %s\n", usb_strerror(err));
197 libusb_free_transfer(transfer);
198 free(data_buf);
199 return NULL;
200 }
201
202 return transfer;
203 }
204
205 struct hmcfgusb_cb_data {
206 struct hmcfgusb_dev *dev;
207 hmcfgusb_cb_fn cb;
208 void *data;
209 };
210
211 static void LIBUSB_CALL hmcfgusb_interrupt(struct libusb_transfer *transfer)
212 {
213 int err;
214 struct hmcfgusb_cb_data *cb_data;
215
216 cb_data = transfer->user_data;
217
218 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
219 if (transfer->status != LIBUSB_TRANSFER_TIMED_OUT) {
220 fprintf(stderr, "Interrupt transfer not completed: %d!\n", transfer->status);
221 quit = EIO;
222
223 if (cb_data && cb_data->dev && cb_data->dev->transfer) {
224 libusb_free_transfer(cb_data->dev->transfer);
225 cb_data->dev->transfer = NULL;
226 }
227 return;
228 }
229 } else {
230 if (cb_data && cb_data->cb) {
231 if (debug)
232 hexdump(transfer->buffer, transfer->actual_length, "USB > ");
233 cb_data->cb(transfer->buffer, transfer->actual_length, cb_data->data);
234 } else {
235 hexdump(transfer->buffer, transfer->actual_length, "> ");
236 }
237 }
238
239 err = libusb_submit_transfer(transfer);
240 if (err != 0) {
241 fprintf(stderr, "Can't re-submit transfer: %s\n", usb_strerror(err));
242 free(transfer->buffer);
243 libusb_free_transfer(transfer);
244 }
245 }
246
247 struct hmcfgusb_dev *hmcfgusb_init(hmcfgusb_cb_fn cb, void *data)
248 {
249 libusb_device_handle *devh = NULL;
250 const struct libusb_pollfd **usb_pfd = NULL;
251 struct hmcfgusb_dev *dev = NULL;
252 struct hmcfgusb_cb_data *cb_data = NULL;
253 int err;
254 int i;
255
256 err = libusb_init(NULL);
257 if (err != 0) {
258 fprintf(stderr, "Can't initialize libusb: %s\n", usb_strerror(err));
259 return NULL;
260 }
261
262 devh = hmcfgusb_find();
263 if (!devh) {
264 fprintf(stderr, "Can't find/open hmcfgusb!\n");
265 return NULL;
266 }
267
268 dev = malloc(sizeof(struct hmcfgusb_dev));
269 if (!dev) {
270 perror("Can't allocate memory for hmcfgusb_dev");
271 return NULL;
272 }
273
274 memset(dev, 0, sizeof(struct hmcfgusb_dev));
275 dev->usb_devh = devh;
276
277 cb_data = malloc(sizeof(struct hmcfgusb_cb_data));
278 if (!cb_data) {
279 perror("Can't allocate memory for hmcfgusb_cb_data");
280 return NULL;
281 }
282
283 memset(cb_data, 0, sizeof(struct hmcfgusb_cb_data));
284
285 cb_data->dev = dev;
286 cb_data->cb = cb;
287 cb_data->data = data;
288
289 dev->transfer = hmcfgusb_prepare_int(devh, hmcfgusb_interrupt, cb_data);
290 if (!dev->transfer) {
291 fprintf(stderr, "Can't prepare async device io!\n");
292 return NULL;
293 }
294
295 usb_pfd = libusb_get_pollfds(NULL);
296 if (!usb_pfd) {
297 fprintf(stderr, "Can't get FDset from libusb!\n");
298 free(dev);
299 return NULL;
300 }
301
302 dev->n_usb_pfd = 0;
303 for(i = 0; usb_pfd[i]; i++)
304 dev->n_usb_pfd++;
305
306 dev->pfd = malloc(dev->n_usb_pfd * sizeof(struct pollfd));
307 if (!dev->pfd) {
308 perror("Can't allocate memory for poll-fds");
309 return NULL;
310 }
311
312 memset(dev->pfd, 0, dev->n_usb_pfd * sizeof(struct pollfd));
313
314 for (i = 0; i < dev->n_usb_pfd; i++) {
315 dev->pfd[i].fd = usb_pfd[i]->fd;
316 dev->pfd[i].events = usb_pfd[i]->events;
317 dev->pfd[i].revents = 0;
318 }
319
320 free(usb_pfd);
321
322 dev->n_pfd = dev->n_usb_pfd;
323
324 return dev;
325 }
326
327 int hmcfgusb_add_pfd(struct hmcfgusb_dev *dev, int fd, short events)
328 {
329 dev->n_pfd++;
330 dev->pfd = realloc(dev->pfd, dev->n_pfd * sizeof(struct pollfd));
331 if (!dev->pfd) {
332 perror("Can't realloc poll-fds");
333 return 0;
334 }
335
336 dev->pfd[dev->n_pfd-1].fd = fd;
337 dev->pfd[dev->n_pfd-1].events = events;
338 dev->pfd[dev->n_pfd-1].revents = 0;
339
340 return 1;
341 }
342
343 int hmcfgusb_poll(struct hmcfgusb_dev *dev, int timeout)
344 {
345 struct timeval tv;
346 int usb_event = 0;
347 int i;
348 int n;
349 int fd_n;
350 int err;
351
352 errno = 0;
353
354 memset(&tv, 0, sizeof(tv));
355 err = libusb_get_next_timeout(NULL, &tv);
356 if (err < 0) {
357 fprintf(stderr, "libusb_get_next_timeout: %s\n", usb_strerror(err));
358 errno = EIO;
359 return -1;
360 } else if (err == 0) {
361 /* No pending timeout or a sane platform */
362 tv.tv_sec = timeout;
363 } else {
364 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) {
365 usb_event = 1;
366 }
367 }
368
369 if (!usb_event) {
370 for (i = 0; i < dev->n_pfd; i++) {
371 dev->pfd[i].revents = 0;
372 }
373
374 n = poll(dev->pfd, dev->n_pfd, tv.tv_sec * 1000);
375 if (n < 0) {
376 perror("poll");
377 return -1;
378 } else if (n == 0) {
379 usb_event = 1;
380 } else {
381 for (fd_n = 0; fd_n < dev->n_pfd; fd_n++) {
382 if (dev->pfd[fd_n].revents) {
383 if (fd_n < dev->n_usb_pfd) {
384 usb_event = 1;
385 break;
386 } else {
387 return dev->pfd[fd_n].fd;
388 }
389 }
390 }
391 }
392 }
393
394 if (usb_event) {
395 memset(&tv, 0, sizeof(tv));
396 err = libusb_handle_events_timeout_completed(NULL, &tv, NULL);
397 if (err < 0) {
398 fprintf(stderr, "libusb_handle_events_timeout_completed: %s\n", usb_strerror(err));
399 errno = EIO;
400 return -1;
401 }
402 }
403
404 if (quit)
405 errno = quit;
406
407 return -1;
408 }
409
410 void hmcfgusb_close(struct hmcfgusb_dev *dev)
411 {
412 int err;
413
414 if (dev->transfer) {
415 libusb_cancel_transfer(dev->transfer);
416 }
417
418 err = libusb_release_interface(dev->usb_devh, INTERFACE);
419 if ((err != 0)) {
420 fprintf(stderr, "Can't release interface: %s\n", usb_strerror(err));
421 }
422
423 libusb_close(dev->usb_devh);
424 free(dev->pfd);
425 free(dev);
426
427 libusb_exit(NULL);
428 }
429
430 void hmcfgusb_set_debug(int d)
431 {
432 debug = d;
433 }
Impressum, Datenschutz