summaryrefslogtreecommitdiffstats
path: root/usbmuxd/usb-linux.c
diff options
context:
space:
mode:
authorGravatar Hector Martin2009-05-05 00:38:06 +0200
committerGravatar Hector Martin2009-05-05 00:38:06 +0200
commitbd335df6954fe5e9419499820cd082c27de111f1 (patch)
tree4d7ccd2e767083baa2c41880081780de57665cf7 /usbmuxd/usb-linux.c
parent9bf93e406de3a06cee0a1452bf1da3c6f697ee31 (diff)
downloadusbmuxd-bd335df6954fe5e9419499820cd082c27de111f1.tar.gz
usbmuxd-bd335df6954fe5e9419499820cd082c27de111f1.tar.bz2
Move usbmuxd to its own folder
Diffstat (limited to 'usbmuxd/usb-linux.c')
-rw-r--r--usbmuxd/usb-linux.c501
1 files changed, 501 insertions, 0 deletions
diff --git a/usbmuxd/usb-linux.c b/usbmuxd/usb-linux.c
new file mode 100644
index 0000000..b401cde
--- /dev/null
+++ b/usbmuxd/usb-linux.c
@@ -0,0 +1,501 @@
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_ERROR, "TX transfer cancelled for device %d-%d", 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_ERROR, "RX transfer cancelled for device %d-%d", 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 continue;
273 libusb_device_handle *handle;
274 usbmuxd_log(LL_INFO, "Found new device with v/p %04x:%04x at %d-%d", devdesc.idVendor, devdesc.idProduct, bus, address);
275 // potentially blocking operations follow; they will only run when new devices are detected, which is acceptable
276 if((res = libusb_open(dev, &handle)) != 0) {
277 usbmuxd_log(LL_WARNING, "Could not open device %d-%d: %d", bus, address, res);
278 continue;
279 }
280 if((res = libusb_set_configuration(handle, USB_CONFIGURATION)) != 0) {
281 usbmuxd_log(LL_WARNING, "Could not set configuration %d for device %d-%d: %d", USB_CONFIGURATION, bus, address, res);
282 libusb_close(handle);
283 continue;
284 }
285 if((res = libusb_claim_interface(handle, USB_INTERFACE)) != 0) {
286 usbmuxd_log(LL_WARNING, "Could not claim interface %d for device %d-%d: %d", USB_INTERFACE, bus, address, res);
287 libusb_close(handle);
288 continue;
289 }
290 struct usb_device *usbdev;
291 usbdev = malloc(sizeof(struct usb_device));
292
293 if((res = libusb_get_string_descriptor_ascii(handle, devdesc.iSerialNumber, (uint8_t *)usbdev->serial, 256)) <= 0) {
294 usbmuxd_log(LL_WARNING, "Could not get serial number for device %d-%d: %d", bus, address, res);
295 libusb_release_interface(handle, USB_INTERFACE);
296 libusb_close(handle);
297 free(usbdev);
298 continue;
299 }
300 usbdev->serial[res] = 0;
301 usbdev->bus = bus;
302 usbdev->address = address;
303 usbdev->vid = devdesc.idVendor;
304 usbdev->pid = devdesc.idProduct;
305 usbdev->dev = handle;
306 usbdev->alive = 1;
307 collection_init(&usbdev->tx_xfers);
308
309 collection_add(&device_list, usbdev);
310
311 if(device_add(usbdev) < 0) {
312 usb_disconnect(usbdev);
313 continue;
314 }
315 if(start_rx(usbdev) < 0) {
316 device_remove(usbdev);
317 usb_disconnect(usbdev);
318 continue;
319 }
320 valid_count++;
321 }
322 FOREACH(struct usb_device *usbdev, &device_list) {
323 if(!usbdev->alive) {
324 device_remove(usbdev);
325 usb_disconnect(usbdev);
326 }
327 } ENDFOREACH
328
329 libusb_free_device_list(devs, 1);
330
331 gettimeofday(&next_dev_poll_time, NULL);
332 next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000;
333 next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000;
334 next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000;
335
336 return valid_count;
337}
338
339const char *usb_get_serial(struct usb_device *dev)
340{
341 if(!dev->dev) {
342 return NULL;
343 }
344 return dev->serial;
345}
346
347uint32_t usb_get_location(struct usb_device *dev)
348{
349 if(!dev->dev) {
350 return 0;
351 }
352 return (dev->bus << 16) | dev->address;
353}
354
355uint16_t usb_get_pid(struct usb_device *dev)
356{
357 if(!dev->dev) {
358 return 0;
359 }
360 return dev->pid;
361}
362
363void usb_get_fds(struct fdlist *list)
364{
365 const struct libusb_pollfd **usbfds;
366 const struct libusb_pollfd **p;
367 usbfds = libusb_get_pollfds(NULL);
368 if(!usbfds) {
369 usbmuxd_log(LL_ERROR, "libusb_get_pollfds failed");
370 return;
371 }
372 p = usbfds;
373 while(*p) {
374 fdlist_add(list, FD_USB, (*p)->fd, (*p)->events);
375 p++;
376 }
377 free(usbfds);
378}
379
380static int dev_poll_remain_ms(void)
381{
382 int msecs;
383 struct timeval tv;
384 gettimeofday(&tv, NULL);
385 msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000;
386 msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000;
387 if(msecs < 0)
388 return 0;
389 return msecs;
390}
391
392int usb_get_timeout(void)
393{
394 struct timeval tv;
395 int msec;
396 int res;
397 int pollrem;
398 pollrem = dev_poll_remain_ms();
399 res = libusb_get_next_timeout(NULL, &tv);
400 if(res == 0)
401 return pollrem;
402 if(res < 0) {
403 usbmuxd_log(LL_ERROR, "libusb_get_next_timeout failed: %d", res);
404 return pollrem;
405 }
406 msec = tv.tv_sec * 1000;
407 msec += tv.tv_usec / 1000;
408 if(msec > pollrem)
409 return pollrem;
410 return msec;
411}
412
413int usb_process(void)
414{
415 int res;
416 struct timeval tv;
417 tv.tv_sec = tv.tv_usec = 0;
418 res = libusb_handle_events_timeout(NULL, &tv);
419 if(res < 0) {
420 usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res);
421 return res;
422 }
423 // reap devices marked dead due to an RX error
424 FOREACH(struct usb_device *usbdev, &device_list) {
425 if(!usbdev->alive) {
426 device_remove(usbdev);
427 usb_disconnect(usbdev);
428 }
429 } ENDFOREACH
430
431 if(dev_poll_remain_ms() <= 0) {
432 res = usb_discover();
433 if(res < 0) {
434 usbmuxd_log(LL_ERROR, "usb_discover failed: %d", res);
435 return res;
436 }
437 }
438 return 0;
439}
440
441int usb_process_timeout(int msec)
442{
443 int res;
444 struct timeval tleft, tcur, tfin;
445 gettimeofday(&tcur, NULL);
446 tfin.tv_sec = tcur.tv_sec + (msec / 1000);
447 tfin.tv_usec = tcur.tv_usec + (msec % 1000) * 1000;
448 tfin.tv_sec += tfin.tv_usec / 1000000;
449 tfin.tv_usec %= 1000000;
450 while((tfin.tv_sec > tcur.tv_sec) || ((tfin.tv_sec == tcur.tv_sec) && (tfin.tv_usec > tcur.tv_usec))) {
451 tleft.tv_sec = tfin.tv_sec - tcur.tv_sec;
452 tleft.tv_usec = tfin.tv_usec - tcur.tv_usec;
453 if(tleft.tv_usec < 0) {
454 tleft.tv_usec += 1000000;
455 tleft.tv_sec -= 1;
456 }
457 res = libusb_handle_events_timeout(NULL, &tleft);
458 if(res < 0) {
459 usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res);
460 return res;
461 }
462 // reap devices marked dead due to an RX error
463 FOREACH(struct usb_device *usbdev, &device_list) {
464 if(!usbdev->alive) {
465 device_remove(usbdev);
466 usb_disconnect(usbdev);
467 }
468 } ENDFOREACH
469 gettimeofday(&tcur, NULL);
470 }
471 return 0;
472}
473
474int usb_init(void)
475{
476 int res;
477 usbmuxd_log(LL_DEBUG, "usb_init for linux / libusb 1.0");
478
479 devlist_failures = 0;
480 res = libusb_init(NULL);
481 //libusb_set_debug(NULL, 3);
482 if(res != 0) {
483 usbmuxd_log(LL_FATAL, "libusb_init failed: %d", res);
484 return -1;
485 }
486
487 collection_init(&device_list);
488
489 return usb_discover();
490}
491
492void usb_shutdown(void)
493{
494 usbmuxd_log(LL_DEBUG, "usb_shutdown");
495 FOREACH(struct usb_device *usbdev, &device_list) {
496 device_remove(usbdev);
497 usb_disconnect(usbdev);
498 } ENDFOREACH
499 collection_free(&device_list);
500 libusb_exit(NULL);
501}