diff options
author | Nikias Bassen | 2018-01-08 02:35:36 +0100 |
---|---|---|
committer | Nikias Bassen | 2018-01-08 02:39:38 +0100 |
commit | 63cd3057a38c8daf49e839712f1f0bebd0046acb (patch) | |
tree | 195a77f30cf311e223c9fb8451c7aa197c4b88b5 | |
parent | d29b74db77433a80fa2eb293216e8f572ffe4c57 (diff) | |
download | usbmuxd-63cd3057a38c8daf49e839712f1f0bebd0046acb.tar.gz usbmuxd-63cd3057a38c8daf49e839712f1f0bebd0046acb.tar.bz2 |
usb: Use heap buffer instead of making the usb_device struct larger
Since this buffer is only used during device initialization we don't want
the usb_device struct to be unecessary big.
-rw-r--r-- | src/usb.c | 24 |
1 files changed, 18 insertions, 6 deletions
@@ -61,7 +61,6 @@ struct usb_device { int wMaxPacketSize; uint64_t speed; struct libusb_device_descriptor devdesc; - unsigned char transfer_buffer[1024 + LIBUSB_CONTROL_SETUP_SIZE]; }; static struct collection device_list; @@ -321,6 +320,8 @@ static void get_langid_callback(struct libusb_transfer *transfer) int res; struct usb_device *usbdev = transfer->user_data; + transfer->flags |= LIBUSB_TRANSFER_FREE_BUFFER; + if(transfer->status != LIBUSB_TRANSFER_COMPLETED) { usbmuxd_log(LL_ERROR, "Failed to request lang ID for device %d-%d (%i)", usbdev->bus, usbdev->address, transfer->status); @@ -333,10 +334,10 @@ static void get_langid_callback(struct libusb_transfer *transfer) usbmuxd_log(LL_INFO, "Got lang ID %u for device %d-%d", langid, usbdev->bus, usbdev->address); /* re-use the same transfer */ - libusb_fill_control_setup(usbdev->transfer_buffer, LIBUSB_ENDPOINT_IN, LIBUSB_REQUEST_GET_DESCRIPTOR, + libusb_fill_control_setup(transfer->buffer, LIBUSB_ENDPOINT_IN, LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | usbdev->devdesc.iSerialNumber), - langid, sizeof(usbdev->transfer_buffer)); - libusb_fill_control_transfer(transfer, usbdev->dev, usbdev->transfer_buffer, get_serial_callback, usbdev, 1000); + langid, 1024 + LIBUSB_CONTROL_SETUP_SIZE); + libusb_fill_control_transfer(transfer, usbdev->dev, transfer->buffer, get_serial_callback, usbdev, 1000); if((res = libusb_submit_transfer(transfer)) < 0) { usbmuxd_log(LL_ERROR, "Could not request transfer for device %d-%d (%d)", usbdev->bus, usbdev->address, res); @@ -482,6 +483,16 @@ static int usb_device_add(libusb_device* dev) return -1; } + unsigned char *transfer_buffer = malloc(1024 + LIBUSB_CONTROL_SETUP_SIZE + 8); +usbmuxd_log(LL_INFO, "%p", transfer_buffer); + if (!transfer_buffer) { + usbmuxd_log(LL_WARNING, "Failed to allocate transfer buffer for device %d-%d: %d", bus, address, res); + libusb_close(handle); + free(usbdev); + return -1; + } + memset(transfer_buffer, '\0', 1024 + LIBUSB_CONTROL_SETUP_SIZE + 8); + usbdev->serial[0] = 0; usbdev->bus = bus; usbdev->address = address; @@ -522,13 +533,14 @@ static int usb_device_add(libusb_device* dev) * descriptor that contains all the language IDs supported by the * device. **/ - libusb_fill_control_setup(usbdev->transfer_buffer, LIBUSB_ENDPOINT_IN, LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_DT_STRING << 8, 0, sizeof(usbdev->transfer_buffer)); - libusb_fill_control_transfer(transfer, handle, usbdev->transfer_buffer, get_langid_callback, usbdev, 1000); + libusb_fill_control_setup(transfer_buffer, LIBUSB_ENDPOINT_IN, LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_DT_STRING << 8, 0, 1024 + LIBUSB_CONTROL_SETUP_SIZE); + libusb_fill_control_transfer(transfer, handle, transfer_buffer, get_langid_callback, usbdev, 1000); if((res = libusb_submit_transfer(transfer)) < 0) { usbmuxd_log(LL_ERROR, "Could not request transfer for device %d-%d (%d)", usbdev->bus, usbdev->address, res); libusb_free_transfer(transfer); libusb_close(handle); + free(transfer_buffer); free(usbdev); return -1; } |