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