summaryrefslogtreecommitdiffstats
path: root/usbmuxd/usb-linux.c
diff options
context:
space:
mode:
Diffstat (limited to 'usbmuxd/usb-linux.c')
-rw-r--r--usbmuxd/usb-linux.c503
1 files changed, 503 insertions, 0 deletions
diff --git a/usbmuxd/usb-linux.c b/usbmuxd/usb-linux.c
new file mode 100644
index 0000000..6e99a95
--- /dev/null
+++ b/usbmuxd/usb-linux.c
@@ -0,0 +1,503 @@
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};
49
50static struct collection device_list;
51
52static struct timeval next_dev_poll_time;
53
54static int devlist_failures;
55
56static void usb_disconnect(struct usb_device *dev)
57{
58 if(!dev->dev) {
59 return;
60 }
61
62 // kill the rx xfer and tx xfers and try to make sure the callbacks get called before we free the device
63 if(dev->rx_xfer) {
64 usbmuxd_log(LL_DEBUG, "usb_disconnect: cancelling RX xfer");
65 libusb_cancel_transfer(dev->rx_xfer);
66 }
67 FOREACH(struct libusb_transfer *xfer, &dev->tx_xfers) {
68 usbmuxd_log(LL_DEBUG, "usb_disconnect: cancelling TX xfer %p", xfer);
69 libusb_cancel_transfer(xfer);
70 } ENDFOREACH
71
72 while(dev->rx_xfer || collection_count(&dev->tx_xfers)) {
73 struct timeval tv;
74 int res;
75
76 tv.tv_sec = 0;
77 tv.tv_usec = 1000;
78 if((res = libusb_handle_events_timeout(NULL, &tv)) < 0) {
79 usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout for usb_disconnect failed: %d", res);
80 break;
81 }
82 }
83 collection_free(&dev->tx_xfers);
84 libusb_release_interface(dev->dev, USB_INTERFACE);
85 libusb_close(dev->dev);
86 dev->dev = NULL;
87 collection_remove(&device_list, dev);
88 free(dev);
89}
90
91static void tx_callback(struct libusb_transfer *xfer)
92{
93 struct usb_device *dev = xfer->user_data;
94 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);
95 if(xfer->status != LIBUSB_TRANSFER_COMPLETED) {
96 switch(xfer->status) {
97 case LIBUSB_TRANSFER_COMPLETED: //shut up compiler
98 case LIBUSB_TRANSFER_ERROR:
99 // funny, this happens when we disconnect the device while waiting for a transfer, sometimes
100 usbmuxd_log(LL_INFO, "Device %d-%d TX aborted due to error or disconnect", dev->bus, dev->address);
101 break;
102 case LIBUSB_TRANSFER_TIMED_OUT:
103 usbmuxd_log(LL_ERROR, "TX transfer timed out for device %d-%d", dev->bus, dev->address);
104 break;
105 case LIBUSB_TRANSFER_CANCELLED:
106 usbmuxd_log(LL_DEBUG, "Device %d-%d TX transfer cancelled", dev->bus, dev->address);
107 break;
108 case LIBUSB_TRANSFER_STALL:
109 usbmuxd_log(LL_ERROR, "TX transfer stalled for device %d-%d", dev->bus, dev->address);
110 break;
111 case LIBUSB_TRANSFER_NO_DEVICE:
112 // other times, this happens, and also even when we abort the transfer after device removal
113 usbmuxd_log(LL_INFO, "Device %d-%d TX aborted due to disconnect", dev->bus, dev->address);
114 break;
115 case LIBUSB_TRANSFER_OVERFLOW:
116 usbmuxd_log(LL_ERROR, "TX transfer overflow for device %d-%d", dev->bus, dev->address);
117 break;
118 // and nothing happens (this never gets called) if the device is freed after a disconnect! (bad)
119 }
120 // we can't usb_disconnect here due to a deadlock, so instead mark it as dead and reap it after processing events
121 // we'll do device_remove there too
122 dev->alive = 0;
123 }
124 if(xfer->buffer)
125 free(xfer->buffer);
126 collection_remove(&dev->tx_xfers, xfer);
127 libusb_free_transfer(xfer);
128}
129
130int usb_send(struct usb_device *dev, const unsigned char *buf, int length)
131{
132 int res;
133 struct libusb_transfer *xfer = libusb_alloc_transfer(0);
134 libusb_fill_bulk_transfer(xfer, dev->dev, BULK_OUT, (void*)buf, length, tx_callback, dev, 0);
135 xfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK;
136 if((res = libusb_submit_transfer(xfer)) < 0) {
137 usbmuxd_log(LL_ERROR, "Failed to submit TX transfer %p len %d to device %d-%d: %d", buf, length, dev->bus, dev->address, res);
138 libusb_free_transfer(xfer);
139 return res;
140 }
141 collection_add(&dev->tx_xfers, xfer);
142 if((length % 512) == 0) {
143 usbmuxd_log(LL_DEBUG, "Send ZLP");
144 // Send Zero Length Packet
145 xfer = libusb_alloc_transfer(0);
146 void *buffer = malloc(1);
147 libusb_fill_bulk_transfer(xfer, dev->dev, BULK_OUT, buffer, 0, tx_callback, dev, 0);
148 xfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK;
149 if((res = libusb_submit_transfer(xfer)) < 0) {
150 usbmuxd_log(LL_ERROR, "Failed to submit TX ZLP transfer to device %d-%d: %d", dev->bus, dev->address, res);
151 libusb_free_transfer(xfer);
152 return res;
153 }
154 collection_add(&dev->tx_xfers, xfer);
155 }
156 return 0;
157}
158
159static void rx_callback(struct libusb_transfer *xfer)
160{
161 struct usb_device *dev = xfer->user_data;
162 usbmuxd_log(LL_SPEW, "RX callback dev %d-%d len %d status %d", dev->bus, dev->address, xfer->actual_length, xfer->status);
163 if(xfer->status == LIBUSB_TRANSFER_COMPLETED) {
164 device_data_input(dev, xfer->buffer, xfer->actual_length);
165 libusb_submit_transfer(xfer);
166 } else {
167 switch(xfer->status) {
168 case LIBUSB_TRANSFER_COMPLETED: //shut up compiler
169 case LIBUSB_TRANSFER_ERROR:
170 // funny, this happens when we disconnect the device while waiting for a transfer, sometimes
171 usbmuxd_log(LL_INFO, "Device %d-%d RX aborted due to error or disconnect", dev->bus, dev->address);
172 break;
173 case LIBUSB_TRANSFER_TIMED_OUT:
174 usbmuxd_log(LL_ERROR, "RX transfer timed out for device %d-%d", dev->bus, dev->address);
175 break;
176 case LIBUSB_TRANSFER_CANCELLED:
177 usbmuxd_log(LL_DEBUG, "Device %d-%d RX transfer cancelled", dev->bus, dev->address);
178 break;
179 case LIBUSB_TRANSFER_STALL:
180 usbmuxd_log(LL_ERROR, "RX transfer stalled for device %d-%d", dev->bus, dev->address);
181 break;
182 case LIBUSB_TRANSFER_NO_DEVICE:
183 // other times, this happens, and also even when we abort the transfer after device removal
184 usbmuxd_log(LL_INFO, "Device %d-%d RX aborted due to disconnect", dev->bus, dev->address);
185 break;
186 case LIBUSB_TRANSFER_OVERFLOW:
187 usbmuxd_log(LL_ERROR, "RX transfer overflow for device %d-%d", dev->bus, dev->address);
188 break;
189 // and nothing happens (this never gets called) if the device is freed after a disconnect! (bad)
190 }
191 free(xfer->buffer);
192 dev->rx_xfer = NULL;
193 libusb_free_transfer(xfer);
194 // we can't usb_disconnect here due to a deadlock, so instead mark it as dead and reap it after processing events
195 // we'll do device_remove there too
196 dev->alive = 0;
197 }
198}
199
200static int start_rx(struct usb_device *dev)
201{
202 int res;
203 void *buf;
204 dev->rx_xfer = libusb_alloc_transfer(0);
205 buf = malloc(USB_MRU);
206 libusb_fill_bulk_transfer(dev->rx_xfer, dev->dev, BULK_IN, buf, USB_MRU, rx_callback, dev, 0);
207 if((res = libusb_submit_transfer(dev->rx_xfer)) != 0) {
208 usbmuxd_log(LL_ERROR, "Failed to submit RX transfer to device %d-%d: %d", dev->bus, dev->address, res);
209 libusb_free_transfer(dev->rx_xfer);
210 dev->rx_xfer = NULL;
211 return res;
212 }
213 return 0;
214}
215
216static int usb_discover(void)
217{
218 int cnt, i, res;
219 int valid_count = 0;
220 libusb_device **devs;
221
222 cnt = libusb_get_device_list(NULL, &devs);
223 if(cnt < 0) {
224 usbmuxd_log(LL_WARNING, "Could not get device list: %d", cnt);
225 devlist_failures++;
226 // sometimes libusb fails getting the device list if you've just removed something
227 if(devlist_failures > 5) {
228 usbmuxd_log(LL_FATAL, "Too many errors getting device list\n");
229 return cnt;
230 } else {
231 gettimeofday(&next_dev_poll_time, NULL);
232 next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000;
233 next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000;
234 next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000;
235 return 0;
236 }
237 }
238 devlist_failures = 0;
239
240 usbmuxd_log(LL_SPEW, "usb_discover: scanning %d devices", cnt);
241
242 FOREACH(struct usb_device *usbdev, &device_list) {
243 usbdev->alive = 0;
244 } ENDFOREACH
245
246 for(i=0; i<cnt; i++) {
247 // the following are non-blocking operations on the device list
248 libusb_device *dev = devs[i];
249 uint8_t bus = libusb_get_bus_number(dev);
250 uint8_t address = libusb_get_device_address(dev);
251 struct libusb_device_descriptor devdesc;
252 int found = 0;
253 FOREACH(struct usb_device *usbdev, &device_list) {
254 if(usbdev->bus == bus && usbdev->address == address) {
255 valid_count++;
256 usbdev->alive = 1;
257 found = 1;
258 break;
259 }
260 } ENDFOREACH
261 if(found)
262 continue; //device already found
263 if((res = libusb_get_device_descriptor(dev, &devdesc)) != 0) {
264 usbmuxd_log(LL_WARNING, "Could not get device descriptor for device %d-%d: %d", bus, address, res);
265 continue;
266 }
267 if(devdesc.idVendor != VID_APPLE)
268 continue;
269 if( (devdesc.idProduct != PID_IPHONE2G) &&
270 (devdesc.idProduct != PID_ITOUCH1G) &&
271 (devdesc.idProduct != PID_IPHONE3G) &&
272 (devdesc.idProduct != PID_ITOUCH2G) &&
273 (devdesc.idProduct != PID_IPHONE3GS))
274 continue;
275 libusb_device_handle *handle;
276 usbmuxd_log(LL_INFO, "Found new device with v/p %04x:%04x at %d-%d", devdesc.idVendor, devdesc.idProduct, bus, address);
277 // potentially blocking operations follow; they will only run when new devices are detected, which is acceptable
278 if((res = libusb_open(dev, &handle)) != 0) {
279 usbmuxd_log(LL_WARNING, "Could not open device %d-%d: %d", bus, address, res);
280 continue;
281 }
282 if((res = libusb_set_configuration(handle, USB_CONFIGURATION)) != 0) {
283 usbmuxd_log(LL_WARNING, "Could not set configuration %d for device %d-%d: %d", USB_CONFIGURATION, bus, address, res);
284 libusb_close(handle);
285 continue;
286 }
287 if((res = libusb_claim_interface(handle, USB_INTERFACE)) != 0) {
288 usbmuxd_log(LL_WARNING, "Could not claim interface %d for device %d-%d: %d", USB_INTERFACE, bus, address, res);
289 libusb_close(handle);
290 continue;
291 }
292 struct usb_device *usbdev;
293 usbdev = malloc(sizeof(struct usb_device));
294
295 if((res = libusb_get_string_descriptor_ascii(handle, devdesc.iSerialNumber, (uint8_t *)usbdev->serial, 256)) <= 0) {
296 usbmuxd_log(LL_WARNING, "Could not get serial number for device %d-%d: %d", bus, address, res);
297 libusb_release_interface(handle, USB_INTERFACE);
298 libusb_close(handle);
299 free(usbdev);
300 continue;
301 }
302 usbdev->serial[res] = 0;
303 usbdev->bus = bus;
304 usbdev->address = address;
305 usbdev->vid = devdesc.idVendor;
306 usbdev->pid = devdesc.idProduct;
307 usbdev->dev = handle;
308 usbdev->alive = 1;
309 collection_init(&usbdev->tx_xfers);
310
311 collection_add(&device_list, usbdev);
312
313 if(device_add(usbdev) < 0) {
314 usb_disconnect(usbdev);
315 continue;
316 }
317 if(start_rx(usbdev) < 0) {
318 device_remove(usbdev);
319 usb_disconnect(usbdev);
320 continue;
321 }
322 valid_count++;
323 }
324 FOREACH(struct usb_device *usbdev, &device_list) {
325 if(!usbdev->alive) {
326 device_remove(usbdev);
327 usb_disconnect(usbdev);
328 }
329 } ENDFOREACH
330
331 libusb_free_device_list(devs, 1);
332
333 gettimeofday(&next_dev_poll_time, NULL);
334 next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000;
335 next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000;
336 next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000;
337
338 return valid_count;
339}
340
341const char *usb_get_serial(struct usb_device *dev)
342{
343 if(!dev->dev) {
344 return NULL;
345 }
346 return dev->serial;
347}
348
349uint32_t usb_get_location(struct usb_device *dev)
350{
351 if(!dev->dev) {
352 return 0;
353 }
354 return (dev->bus << 16) | dev->address;
355}
356
357uint16_t usb_get_pid(struct usb_device *dev)
358{
359 if(!dev->dev) {
360 return 0;
361 }
362 return dev->pid;
363}
364
365void usb_get_fds(struct fdlist *list)
366{
367 const struct libusb_pollfd **usbfds;
368 const struct libusb_pollfd **p;
369 usbfds = libusb_get_pollfds(NULL);
370 if(!usbfds) {
371 usbmuxd_log(LL_ERROR, "libusb_get_pollfds failed");
372 return;
373 }
374 p = usbfds;
375 while(*p) {
376 fdlist_add(list, FD_USB, (*p)->fd, (*p)->events);
377 p++;
378 }
379 free(usbfds);
380}
381
382static int dev_poll_remain_ms(void)
383{
384 int msecs;
385 struct timeval tv;
386 gettimeofday(&tv, NULL);
387 msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000;
388 msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000;
389 if(msecs < 0)
390 return 0;
391 return msecs;
392}
393
394int usb_get_timeout(void)
395{
396 struct timeval tv;
397 int msec;
398 int res;
399 int pollrem;
400 pollrem = dev_poll_remain_ms();
401 res = libusb_get_next_timeout(NULL, &tv);
402 if(res == 0)
403 return pollrem;
404 if(res < 0) {
405 usbmuxd_log(LL_ERROR, "libusb_get_next_timeout failed: %d", res);
406 return pollrem;
407 }
408 msec = tv.tv_sec * 1000;
409 msec += tv.tv_usec / 1000;
410 if(msec > pollrem)
411 return pollrem;
412 return msec;
413}
414
415int usb_process(void)
416{
417 int res;
418 struct timeval tv;
419 tv.tv_sec = tv.tv_usec = 0;
420 res = libusb_handle_events_timeout(NULL, &tv);
421 if(res < 0) {
422 usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res);
423 return res;
424 }
425 // reap devices marked dead due to an RX error
426 FOREACH(struct usb_device *usbdev, &device_list) {
427 if(!usbdev->alive) {
428 device_remove(usbdev);
429 usb_disconnect(usbdev);
430 }
431 } ENDFOREACH
432
433 if(dev_poll_remain_ms() <= 0) {
434 res = usb_discover();
435 if(res < 0) {
436 usbmuxd_log(LL_ERROR, "usb_discover failed: %d", res);
437 return res;
438 }
439 }
440 return 0;
441}
442
443int usb_process_timeout(int msec)
444{
445 int res;
446 struct timeval tleft, tcur, tfin;
447 gettimeofday(&tcur, NULL);
448 tfin.tv_sec = tcur.tv_sec + (msec / 1000);
449 tfin.tv_usec = tcur.tv_usec + (msec % 1000) * 1000;
450 tfin.tv_sec += tfin.tv_usec / 1000000;
451 tfin.tv_usec %= 1000000;
452 while((tfin.tv_sec > tcur.tv_sec) || ((tfin.tv_sec == tcur.tv_sec) && (tfin.tv_usec > tcur.tv_usec))) {
453 tleft.tv_sec = tfin.tv_sec - tcur.tv_sec;
454 tleft.tv_usec = tfin.tv_usec - tcur.tv_usec;
455 if(tleft.tv_usec < 0) {
456 tleft.tv_usec += 1000000;
457 tleft.tv_sec -= 1;
458 }
459 res = libusb_handle_events_timeout(NULL, &tleft);
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 gettimeofday(&tcur, NULL);
472 }
473 return 0;
474}
475
476int usb_init(void)
477{
478 int res;
479 usbmuxd_log(LL_DEBUG, "usb_init for linux / libusb 1.0");
480
481 devlist_failures = 0;
482 res = libusb_init(NULL);
483 //libusb_set_debug(NULL, 3);
484 if(res != 0) {
485 usbmuxd_log(LL_FATAL, "libusb_init failed: %d", res);
486 return -1;
487 }
488
489 collection_init(&device_list);
490
491 return usb_discover();
492}
493
494void usb_shutdown(void)
495{
496 usbmuxd_log(LL_DEBUG, "usb_shutdown");
497 FOREACH(struct usb_device *usbdev, &device_list) {
498 device_remove(usbdev);
499 usb_disconnect(usbdev);
500 } ENDFOREACH
501 collection_free(&device_list);
502 libusb_exit(NULL);
503}