summaryrefslogtreecommitdiffstats
path: root/daemon/usb-linux.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2009-08-25 03:26:22 +0200
committerGravatar Nikias Bassen2009-08-25 03:26:22 +0200
commit4711a2b493f76561e9803bf7e8be77186f3e7798 (patch)
tree339684fe1b996e01047c3eb220a8e22e4491c5e2 /daemon/usb-linux.c
parentde30ca5d5c98a8cbee3d8748601519e2263b3e1d (diff)
downloadusbmuxd-4711a2b493f76561e9803bf7e8be77186f3e7798.tar.gz
usbmuxd-4711a2b493f76561e9803bf7e8be77186f3e7798.tar.bz2
Renamed directory 'usbmuxd' to more suitable 'daemon'.
Diffstat (limited to 'daemon/usb-linux.c')
-rw-r--r--daemon/usb-linux.c542
1 files changed, 542 insertions, 0 deletions
diff --git a/daemon/usb-linux.c b/daemon/usb-linux.c
new file mode 100644
index 0000000..c09cdc9
--- /dev/null
+++ b/daemon/usb-linux.c
@@ -0,0 +1,542 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19*/
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <stdint.h>
28#include <string.h>
29
30#include <libusb.h>
31
32#include "usb.h"
33#include "log.h"
34#include "device.h"
35
36// interval for device connection/disconnection polling, in milliseconds
37// we need this because there is currently no asynchronous device discovery mechanism in libusb
38#define DEVICE_POLL_TIME 1000
39
40struct usb_device {
41 libusb_device_handle *dev;
42 uint8_t bus, address;
43 uint16_t vid, pid;
44 char serial[256];
45 int alive;
46 struct libusb_transfer *rx_xfer;
47 struct collection tx_xfers;
48 int wMaxPacketSize;
49};
50
51static struct collection device_list;
52
53static struct timeval next_dev_poll_time;
54
55static int devlist_failures;
56
57static void usb_disconnect(struct usb_device *dev)
58{
59 if(!dev->dev) {
60 return;
61 }
62
63 // kill the rx xfer and tx xfers and try to make sure the callbacks get called before we free the device
64 if(dev->rx_xfer) {
65 usbmuxd_log(LL_DEBUG, "usb_disconnect: cancelling RX xfer");
66 libusb_cancel_transfer(dev->rx_xfer);
67 }
68 FOREACH(struct libusb_transfer *xfer, &dev->tx_xfers) {
69 usbmuxd_log(LL_DEBUG, "usb_disconnect: cancelling TX xfer %p", xfer);
70 libusb_cancel_transfer(xfer);
71 } ENDFOREACH
72
73 while(dev->rx_xfer || collection_count(&dev->tx_xfers)) {
74 struct timeval tv;
75 int res;
76
77 tv.tv_sec = 0;
78 tv.tv_usec = 1000;
79 if((res = libusb_handle_events_timeout(NULL, &tv)) < 0) {
80 usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout for usb_disconnect failed: %d", res);
81 break;
82 }
83 }
84 collection_free(&dev->tx_xfers);
85 libusb_release_interface(dev->dev, USB_INTERFACE);
86 libusb_close(dev->dev);
87 dev->dev = NULL;
88 collection_remove(&device_list, dev);
89 free(dev);
90}
91
92static void tx_callback(struct libusb_transfer *xfer)
93{
94 struct usb_device *dev = xfer->user_data;
95 usbmuxd_log(LL_SPEW, "TX callback dev %d-%d len %d -> %d status %d", dev->bus, dev->address, xfer->length, xfer->actual_length, xfer->status);
96 if(xfer->status != LIBUSB_TRANSFER_COMPLETED) {
97 switch(xfer->status) {
98 case LIBUSB_TRANSFER_COMPLETED: //shut up compiler
99 case LIBUSB_TRANSFER_ERROR:
100 // funny, this happens when we disconnect the device while waiting for a transfer, sometimes
101 usbmuxd_log(LL_INFO, "Device %d-%d TX aborted due to error or disconnect", dev->bus, dev->address);
102 break;
103 case LIBUSB_TRANSFER_TIMED_OUT:
104 usbmuxd_log(LL_ERROR, "TX transfer timed out for device %d-%d", dev->bus, dev->address);
105 break;
106 case LIBUSB_TRANSFER_CANCELLED:
107 usbmuxd_log(LL_DEBUG, "Device %d-%d TX transfer cancelled", dev->bus, dev->address);
108 break;
109 case LIBUSB_TRANSFER_STALL:
110 usbmuxd_log(LL_ERROR, "TX transfer stalled for device %d-%d", dev->bus, dev->address);
111 break;
112 case LIBUSB_TRANSFER_NO_DEVICE:
113 // other times, this happens, and also even when we abort the transfer after device removal
114 usbmuxd_log(LL_INFO, "Device %d-%d TX aborted due to disconnect", dev->bus, dev->address);
115 break;
116 case LIBUSB_TRANSFER_OVERFLOW:
117 usbmuxd_log(LL_ERROR, "TX transfer overflow for device %d-%d", dev->bus, dev->address);
118 break;
119 // and nothing happens (this never gets called) if the device is freed after a disconnect! (bad)
120 }
121 // we can't usb_disconnect here due to a deadlock, so instead mark it as dead and reap it after processing events
122 // we'll do device_remove there too
123 dev->alive = 0;
124 }
125 if(xfer->buffer)
126 free(xfer->buffer);
127 collection_remove(&dev->tx_xfers, xfer);
128 libusb_free_transfer(xfer);
129}
130
131int usb_send(struct usb_device *dev, const unsigned char *buf, int length)
132{
133 int res;
134 struct libusb_transfer *xfer = libusb_alloc_transfer(0);
135 libusb_fill_bulk_transfer(xfer, dev->dev, BULK_OUT, (void*)buf, length, tx_callback, dev, 0);
136 xfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK;
137#ifndef EXPLICIT_ZLP_TRANSACTION
138 if (length % dev->wMaxPacketSize == 0) {
139 xfer->flags |= LIBUSB_TRANSFER_ZERO_PACKET;
140 }
141#endif
142 if((res = libusb_submit_transfer(xfer)) < 0) {
143 usbmuxd_log(LL_ERROR, "Failed to submit TX transfer %p len %d to device %d-%d: %d", buf, length, dev->bus, dev->address, res);
144 libusb_free_transfer(xfer);
145 return res;
146 }
147 collection_add(&dev->tx_xfers, xfer);
148#ifdef EXPLICIT_ZLP_TRANSACTION
149 if (length % dev->wMaxPacketSize == 0) {
150 usbmuxd_log(LL_DEBUG, "Send ZLP");
151 // Send Zero Length Packet
152 xfer = libusb_alloc_transfer(0);
153 void *buffer = malloc(1);
154 libusb_fill_bulk_transfer(xfer, dev->dev, BULK_OUT, buffer, 0, tx_callback, dev, 0);
155 xfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK;
156 if((res = libusb_submit_transfer(xfer)) < 0) {
157 usbmuxd_log(LL_ERROR, "Failed to submit TX ZLP transfer to device %d-%d: %d", dev->bus, dev->address, res);
158 libusb_free_transfer(xfer);
159 return res;
160 }
161 collection_add(&dev->tx_xfers, xfer);
162 }
163#endif
164 return 0;
165}
166
167static void rx_callback(struct libusb_transfer *xfer)
168{
169 struct usb_device *dev = xfer->user_data;
170 usbmuxd_log(LL_SPEW, "RX callback dev %d-%d len %d status %d", dev->bus, dev->address, xfer->actual_length, xfer->status);
171 if(xfer->status == LIBUSB_TRANSFER_COMPLETED) {
172 device_data_input(dev, xfer->buffer, xfer->actual_length);
173 libusb_submit_transfer(xfer);
174 } else {
175 switch(xfer->status) {
176 case LIBUSB_TRANSFER_COMPLETED: //shut up compiler
177 case LIBUSB_TRANSFER_ERROR:
178 // funny, this happens when we disconnect the device while waiting for a transfer, sometimes
179 usbmuxd_log(LL_INFO, "Device %d-%d RX aborted due to error or disconnect", dev->bus, dev->address);
180 break;
181 case LIBUSB_TRANSFER_TIMED_OUT:
182 usbmuxd_log(LL_ERROR, "RX transfer timed out for device %d-%d", dev->bus, dev->address);
183 break;
184 case LIBUSB_TRANSFER_CANCELLED:
185 usbmuxd_log(LL_DEBUG, "Device %d-%d RX transfer cancelled", dev->bus, dev->address);
186 break;
187 case LIBUSB_TRANSFER_STALL:
188 usbmuxd_log(LL_ERROR, "RX transfer stalled for device %d-%d", dev->bus, dev->address);
189 break;
190 case LIBUSB_TRANSFER_NO_DEVICE:
191 // other times, this happens, and also even when we abort the transfer after device removal
192 usbmuxd_log(LL_INFO, "Device %d-%d RX aborted due to disconnect", dev->bus, dev->address);
193 break;
194 case LIBUSB_TRANSFER_OVERFLOW:
195 usbmuxd_log(LL_ERROR, "RX transfer overflow for device %d-%d", dev->bus, dev->address);
196 break;
197 // and nothing happens (this never gets called) if the device is freed after a disconnect! (bad)
198 }
199 free(xfer->buffer);
200 dev->rx_xfer = NULL;
201 libusb_free_transfer(xfer);
202 // we can't usb_disconnect here due to a deadlock, so instead mark it as dead and reap it after processing events
203 // we'll do device_remove there too
204 dev->alive = 0;
205 }
206}
207
208static int start_rx(struct usb_device *dev)
209{
210 int res;
211 void *buf;
212 dev->rx_xfer = libusb_alloc_transfer(0);
213 buf = malloc(USB_MRU);
214 libusb_fill_bulk_transfer(dev->rx_xfer, dev->dev, BULK_IN, buf, USB_MRU, rx_callback, dev, 0);
215 if((res = libusb_submit_transfer(dev->rx_xfer)) != 0) {
216 usbmuxd_log(LL_ERROR, "Failed to submit RX transfer to device %d-%d: %d", dev->bus, dev->address, res);
217 libusb_free_transfer(dev->rx_xfer);
218 dev->rx_xfer = NULL;
219 return res;
220 }
221 return 0;
222}
223
224static int usb_discover(void)
225{
226 int cnt, i, res;
227 int valid_count = 0;
228 libusb_device **devs;
229
230 cnt = libusb_get_device_list(NULL, &devs);
231 if(cnt < 0) {
232 usbmuxd_log(LL_WARNING, "Could not get device list: %d", cnt);
233 devlist_failures++;
234 // sometimes libusb fails getting the device list if you've just removed something
235 if(devlist_failures > 5) {
236 usbmuxd_log(LL_FATAL, "Too many errors getting device list\n");
237 return cnt;
238 } else {
239 gettimeofday(&next_dev_poll_time, NULL);
240 next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000;
241 next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000;
242 next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000;
243 return 0;
244 }
245 }
246 devlist_failures = 0;
247
248 usbmuxd_log(LL_SPEW, "usb_discover: scanning %d devices", cnt);
249
250 FOREACH(struct usb_device *usbdev, &device_list) {
251 usbdev->alive = 0;
252 } ENDFOREACH
253
254 for(i=0; i<cnt; i++) {
255 // the following are non-blocking operations on the device list
256 libusb_device *dev = devs[i];
257 uint8_t bus = libusb_get_bus_number(dev);
258 uint8_t address = libusb_get_device_address(dev);
259 struct libusb_device_descriptor devdesc;
260 int found = 0;
261 FOREACH(struct usb_device *usbdev, &device_list) {
262 if(usbdev->bus == bus && usbdev->address == address) {
263 valid_count++;
264 usbdev->alive = 1;
265 found = 1;
266 break;
267 }
268 } ENDFOREACH
269 if(found)
270 continue; //device already found
271 if((res = libusb_get_device_descriptor(dev, &devdesc)) != 0) {
272 usbmuxd_log(LL_WARNING, "Could not get device descriptor for device %d-%d: %d", bus, address, res);
273 continue;
274 }
275 if(devdesc.idVendor != VID_APPLE)
276 continue;
277 if( (devdesc.idProduct != PID_IPHONE2G) &&
278 (devdesc.idProduct != PID_ITOUCH1G) &&
279 (devdesc.idProduct != PID_IPHONE3G) &&
280 (devdesc.idProduct != PID_ITOUCH2G) &&
281 (devdesc.idProduct != PID_IPHONE3GS))
282 continue;
283 libusb_device_handle *handle;
284 usbmuxd_log(LL_INFO, "Found new device with v/p %04x:%04x at %d-%d", devdesc.idVendor, devdesc.idProduct, bus, address);
285 // potentially blocking operations follow; they will only run when new devices are detected, which is acceptable
286 if((res = libusb_open(dev, &handle)) != 0) {
287 usbmuxd_log(LL_WARNING, "Could not open device %d-%d: %d", bus, address, res);
288 continue;
289 }
290 int current_config = 0;
291 if((res = libusb_get_configuration(handle, &current_config)) != 0) {
292 usbmuxd_log(LL_WARNING, "Could not get configuration for device %d-%d: %d", bus, address, res);
293 libusb_close(handle);
294 continue;
295 }
296 if (current_config != devdesc.bNumConfigurations) {
297 if((res = libusb_set_configuration(handle, devdesc.bNumConfigurations)) != 0) {
298 usbmuxd_log(LL_WARNING, "Could not set configuration %d for device %d-%d: %d", devdesc.bNumConfigurations, bus, address, res);
299 libusb_close(handle);
300 continue;
301 }
302 }
303 if((res = libusb_claim_interface(handle, USB_INTERFACE)) != 0) {
304 usbmuxd_log(LL_WARNING, "Could not claim interface %d for device %d-%d: %d", USB_INTERFACE, bus, address, res);
305 libusb_close(handle);
306 continue;
307 }
308 struct usb_device *usbdev;
309 usbdev = malloc(sizeof(struct usb_device));
310
311 if((res = libusb_get_string_descriptor_ascii(handle, devdesc.iSerialNumber, (uint8_t *)usbdev->serial, 256)) <= 0) {
312 usbmuxd_log(LL_WARNING, "Could not get serial number for device %d-%d: %d", bus, address, res);
313 libusb_release_interface(handle, USB_INTERFACE);
314 libusb_close(handle);
315 free(usbdev);
316 continue;
317 }
318 usbdev->serial[res] = 0;
319 usbdev->bus = bus;
320 usbdev->address = address;
321 usbdev->vid = devdesc.idVendor;
322 usbdev->pid = devdesc.idProduct;
323 usbdev->dev = handle;
324 usbdev->alive = 1;
325 usbdev->wMaxPacketSize = 0;
326 struct libusb_config_descriptor *cfg;
327 if (libusb_get_active_config_descriptor(dev, &cfg) == 0
328 && cfg && cfg->bNumInterfaces >= (USB_INTERFACE+1)) {
329 const struct libusb_interface *ifp = &cfg->interface[USB_INTERFACE];
330 if (ifp && ifp->num_altsetting >= 1) {
331 const struct libusb_interface_descriptor *as = &ifp->altsetting[0];
332 int i;
333 for (i = 0; i < as->bNumEndpoints; i++) {
334 const struct libusb_endpoint_descriptor *ep = &as->endpoint[i];
335 if (ep->bEndpointAddress == BULK_OUT) {
336 usbdev->wMaxPacketSize = ep->wMaxPacketSize;
337 }
338 }
339 }
340 }
341 if (usbdev->wMaxPacketSize == 0) {
342 usbmuxd_log(LL_ERROR, "Could not determine wMaxPacketSize for device %d-%d, setting to 64", usbdev->bus, usbdev->address);
343 usbdev->wMaxPacketSize = 64;
344 } else {
345 usbmuxd_log(LL_INFO, "Using wMaxPacketSize=%d for device %d-%d", usbdev->wMaxPacketSize, usbdev->bus, usbdev->address);
346 }
347
348 collection_init(&usbdev->tx_xfers);
349
350 collection_add(&device_list, usbdev);
351
352 if(device_add(usbdev) < 0) {
353 usb_disconnect(usbdev);
354 continue;
355 }
356 if(start_rx(usbdev) < 0) {
357 device_remove(usbdev);
358 usb_disconnect(usbdev);
359 continue;
360 }
361 valid_count++;
362 }
363 FOREACH(struct usb_device *usbdev, &device_list) {
364 if(!usbdev->alive) {
365 device_remove(usbdev);
366 usb_disconnect(usbdev);
367 }
368 } ENDFOREACH
369
370 libusb_free_device_list(devs, 1);
371
372 gettimeofday(&next_dev_poll_time, NULL);
373 next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000;
374 next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000;
375 next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000;
376
377 return valid_count;
378}
379
380const char *usb_get_serial(struct usb_device *dev)
381{
382 if(!dev->dev) {
383 return NULL;
384 }
385 return dev->serial;
386}
387
388uint32_t usb_get_location(struct usb_device *dev)
389{
390 if(!dev->dev) {
391 return 0;
392 }
393 return (dev->bus << 16) | dev->address;
394}
395
396uint16_t usb_get_pid(struct usb_device *dev)
397{
398 if(!dev->dev) {
399 return 0;
400 }
401 return dev->pid;
402}
403
404void usb_get_fds(struct fdlist *list)
405{
406 const struct libusb_pollfd **usbfds;
407 const struct libusb_pollfd **p;
408 usbfds = libusb_get_pollfds(NULL);
409 if(!usbfds) {
410 usbmuxd_log(LL_ERROR, "libusb_get_pollfds failed");
411 return;
412 }
413 p = usbfds;
414 while(*p) {
415 fdlist_add(list, FD_USB, (*p)->fd, (*p)->events);
416 p++;
417 }
418 free(usbfds);
419}
420
421static int dev_poll_remain_ms(void)
422{
423 int msecs;
424 struct timeval tv;
425 gettimeofday(&tv, NULL);
426 msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000;
427 msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000;
428 if(msecs < 0)
429 return 0;
430 return msecs;
431}
432
433int usb_get_timeout(void)
434{
435 struct timeval tv;
436 int msec;
437 int res;
438 int pollrem;
439 pollrem = dev_poll_remain_ms();
440 res = libusb_get_next_timeout(NULL, &tv);
441 if(res == 0)
442 return pollrem;
443 if(res < 0) {
444 usbmuxd_log(LL_ERROR, "libusb_get_next_timeout failed: %d", res);
445 return pollrem;
446 }
447 msec = tv.tv_sec * 1000;
448 msec += tv.tv_usec / 1000;
449 if(msec > pollrem)
450 return pollrem;
451 return msec;
452}
453
454int usb_process(void)
455{
456 int res;
457 struct timeval tv;
458 tv.tv_sec = tv.tv_usec = 0;
459 res = libusb_handle_events_timeout(NULL, &tv);
460 if(res < 0) {
461 usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res);
462 return res;
463 }
464 // reap devices marked dead due to an RX error
465 FOREACH(struct usb_device *usbdev, &device_list) {
466 if(!usbdev->alive) {
467 device_remove(usbdev);
468 usb_disconnect(usbdev);
469 }
470 } ENDFOREACH
471
472 if(dev_poll_remain_ms() <= 0) {
473 res = usb_discover();
474 if(res < 0) {
475 usbmuxd_log(LL_ERROR, "usb_discover failed: %d", res);
476 return res;
477 }
478 }
479 return 0;
480}
481
482int usb_process_timeout(int msec)
483{
484 int res;
485 struct timeval tleft, tcur, tfin;
486 gettimeofday(&tcur, NULL);
487 tfin.tv_sec = tcur.tv_sec + (msec / 1000);
488 tfin.tv_usec = tcur.tv_usec + (msec % 1000) * 1000;
489 tfin.tv_sec += tfin.tv_usec / 1000000;
490 tfin.tv_usec %= 1000000;
491 while((tfin.tv_sec > tcur.tv_sec) || ((tfin.tv_sec == tcur.tv_sec) && (tfin.tv_usec > tcur.tv_usec))) {
492 tleft.tv_sec = tfin.tv_sec - tcur.tv_sec;
493 tleft.tv_usec = tfin.tv_usec - tcur.tv_usec;
494 if(tleft.tv_usec < 0) {
495 tleft.tv_usec += 1000000;
496 tleft.tv_sec -= 1;
497 }
498 res = libusb_handle_events_timeout(NULL, &tleft);
499 if(res < 0) {
500 usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res);
501 return res;
502 }
503 // reap devices marked dead due to an RX error
504 FOREACH(struct usb_device *usbdev, &device_list) {
505 if(!usbdev->alive) {
506 device_remove(usbdev);
507 usb_disconnect(usbdev);
508 }
509 } ENDFOREACH
510 gettimeofday(&tcur, NULL);
511 }
512 return 0;
513}
514
515int usb_init(void)
516{
517 int res;
518 usbmuxd_log(LL_DEBUG, "usb_init for linux / libusb 1.0");
519
520 devlist_failures = 0;
521 res = libusb_init(NULL);
522 //libusb_set_debug(NULL, 3);
523 if(res != 0) {
524 usbmuxd_log(LL_FATAL, "libusb_init failed: %d", res);
525 return -1;
526 }
527
528 collection_init(&device_list);
529
530 return usb_discover();
531}
532
533void usb_shutdown(void)
534{
535 usbmuxd_log(LL_DEBUG, "usb_shutdown");
536 FOREACH(struct usb_device *usbdev, &device_list) {
537 device_remove(usbdev);
538 usb_disconnect(usbdev);
539 } ENDFOREACH
540 collection_free(&device_list);
541 libusb_exit(NULL);
542}