/* usbmuxd - iPhone/iPod Touch USB multiplex server daemon Copyright (C) 2009 Hector Martin "marcan" This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 or version 3. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "usb.h" #include "log.h" #include "device.h" // interval for device connection/disconnection polling, in milliseconds // we need this because there is currently no asynchronous device discovery mechanism in libusb #define DEVICE_POLL_TIME 1000 struct usb_device { libusb_device_handle *dev; uint8_t bus, address; char serial[256]; int alive; struct libusb_transfer *rx_xfer; }; static int num_devs; static struct usb_device *device_list; static struct timeval next_dev_poll_time; static int alloc_device(void) { int i; for(i=0; idev) { return; } if(dev->rx_xfer) { // kill the rx xfer and try to make sure the rx callback gets called before we free the device struct timeval tv; int res; // TODO: BUG: outstanding TX xfers are not listed but we need to free them libusb_cancel_transfer(dev->rx_xfer); tv.tv_sec = tv.tv_usec = 0; if((res = libusb_handle_events_timeout(NULL, &tv)) < 0) { usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout for device removal failed: %d", res); } } libusb_release_interface(dev->dev, USB_INTERFACE); libusb_close(dev->dev); dev->dev = NULL; } static void tx_callback(struct libusb_transfer *xfer) { struct usb_device *dev = xfer->user_data; 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); if(xfer->status != LIBUSB_TRANSFER_COMPLETED) { switch(xfer->status) { case LIBUSB_TRANSFER_COMPLETED: //shut up compiler case LIBUSB_TRANSFER_ERROR: // funny, this happens when we disconnect the device while waiting for a transfer, sometimes usbmuxd_log(LL_INFO, "Device %d-%d TX aborted due to error or disconnect", dev->bus, dev->address); break; case LIBUSB_TRANSFER_TIMED_OUT: usbmuxd_log(LL_ERROR, "TX transfer timed out for device %d-%d", dev->bus, dev->address); break; case LIBUSB_TRANSFER_CANCELLED: usbmuxd_log(LL_ERROR, "TX transfer cancelled for device %d-%d", dev->bus, dev->address); break; case LIBUSB_TRANSFER_STALL: usbmuxd_log(LL_ERROR, "TX transfer stalled for device %d-%d", dev->bus, dev->address); break; case LIBUSB_TRANSFER_NO_DEVICE: // other times, this happens, and also even when we abort the transfer after device removal usbmuxd_log(LL_INFO, "Device %d-%d TX aborted due to disconnect", dev->bus, dev->address); break; case LIBUSB_TRANSFER_OVERFLOW: usbmuxd_log(LL_ERROR, "TX transfer overflow for device %d-%d", dev->bus, dev->address); break; // and nothing happens (this never gets called) if the device is freed after a disconnect! (bad) } // we can't usb_disconnect here due to a deadlock, so instead mark it as dead and reap it after processing events // we'll do device_remove there too dev->alive = 0; } free(xfer->buffer); libusb_free_transfer(xfer); } int usb_send(struct usb_device *dev, const unsigned char *buf, int length) { int res; struct libusb_transfer *xfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(xfer, dev->dev, BULK_OUT, (void*)buf, length, tx_callback, dev, 0); xfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK; if((res = libusb_submit_transfer(xfer)) < 0) { usbmuxd_log(LL_ERROR, "Failed to submit TX transfer %p len %d to device %d-%d: %d", buf, length, dev->bus, dev->address, res); libusb_free_transfer(xfer); return res; } return 0; } static void rx_callback(struct libusb_transfer *xfer) { struct usb_device *dev = xfer->user_data; usbmuxd_log(LL_SPEW, "RX callback dev %d-%d len %d status %d", dev->bus, dev->address, xfer->actual_length, xfer->status); if(xfer->status == LIBUSB_TRANSFER_COMPLETED) { device_data_input(dev, xfer->buffer, xfer->actual_length); libusb_submit_transfer(xfer); } else { switch(xfer->status) { case LIBUSB_TRANSFER_COMPLETED: //shut up compiler case LIBUSB_TRANSFER_ERROR: // funny, this happens when we disconnect the device while waiting for a transfer, sometimes usbmuxd_log(LL_INFO, "Device %d-%d RX aborted due to error or disconnect", dev->bus, dev->address); break; case LIBUSB_TRANSFER_TIMED_OUT: usbmuxd_log(LL_ERROR, "RX transfer timed out for device %d-%d", dev->bus, dev->address); break; case LIBUSB_TRANSFER_CANCELLED: usbmuxd_log(LL_ERROR, "RX transfer cancelled for device %d-%d", dev->bus, dev->address); break; case LIBUSB_TRANSFER_STALL: usbmuxd_log(LL_ERROR, "RX transfer stalled for device %d-%d", dev->bus, dev->address); break; case LIBUSB_TRANSFER_NO_DEVICE: // other times, this happens, and also even when we abort the transfer after device removal usbmuxd_log(LL_INFO, "Device %d-%d RX aborted due to disconnect", dev->bus, dev->address); break; case LIBUSB_TRANSFER_OVERFLOW: usbmuxd_log(LL_ERROR, "RX transfer overflow for device %d-%d", dev->bus, dev->address); break; // and nothing happens (this never gets called) if the device is freed after a disconnect! (bad) } free(xfer->buffer); dev->rx_xfer = NULL; libusb_free_transfer(xfer); // we can't usb_disconnect here due to a deadlock, so instead mark it as dead and reap it after processing events // we'll do device_remove there too dev->alive = 0; } } static int start_rx(struct usb_device *dev) { int res; void *buf; dev->rx_xfer = libusb_alloc_transfer(0); buf = malloc(USB_MTU); libusb_fill_bulk_transfer(dev->rx_xfer, dev->dev, BULK_IN, buf, USB_MTU, rx_callback, dev, 0); if((res = libusb_submit_transfer(dev->rx_xfer)) != 0) { usbmuxd_log(LL_ERROR, "Failed to submit RX transfer to device %d-%d: %d", dev->bus, dev->address, res); libusb_free_transfer(dev->rx_xfer); dev->rx_xfer = NULL; return res; } return 0; } static int usb_discover(void) { int cnt, i, j, res; int valid_count = 0; libusb_device **devs; cnt = libusb_get_device_list(NULL, &devs); if(cnt < 0) { usbmuxd_log(LL_FATAL, "Could not get device list: %d", cnt); return cnt; } usbmuxd_log(LL_SPEW, "usb_discover: scanning %d devices", cnt); for(j=0; jdev) { return NULL; } return dev->serial; } int usb_get_location(struct usb_device *dev) { if(!dev->dev) { return 0; } return (dev->bus << 16) | dev->address; } void usb_get_fds(struct fdlist *list) { const struct libusb_pollfd **usbfds; const struct libusb_pollfd **p; usbfds = libusb_get_pollfds(NULL); if(!usbfds) { usbmuxd_log(LL_ERROR, "libusb_get_pollfds failed"); return; } p = usbfds; while(*p) { fdlist_add(list, FD_USB, (*p)->fd, (*p)->events); p++; } free(usbfds); } static int dev_poll_remain_ms(void) { int msecs; struct timeval tv; gettimeofday(&tv, NULL); msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000; msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000; if(msecs < 0) return 0; return msecs; } int usb_get_timeout(void) { struct timeval tv; int msec; int res; int pollrem; pollrem = dev_poll_remain_ms(); res = libusb_get_next_timeout(NULL, &tv); if(res == 0) return pollrem; if(res < 0) { usbmuxd_log(LL_ERROR, "libusb_get_next_timeout failed: %d", res); return pollrem; } msec = tv.tv_sec * 1000; msec += tv.tv_usec / 1000; if(msec > pollrem) return pollrem; return msec; } int usb_process(void) { int i, res; struct timeval tv; tv.tv_sec = tv.tv_usec = 0; res = libusb_handle_events_timeout(NULL, &tv); if(res < 0) { usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res); return res; } // reap devices marked dead due to an RX error for(i=0; i