diff options
-rw-r--r-- | src/usb.c | 32 |
1 files changed, 17 insertions, 15 deletions
@@ -366,7 +366,8 @@ static void get_langid_callback(struct libusb_transfer *transfer) | |||
366 | } | 366 | } |
367 | } | 367 | } |
368 | 368 | ||
369 | static int submit_vendor_specific(struct libusb_device_handle *handle, struct mode_user_data *user_data, libusb_transfer_cb_fn callback) { | 369 | static int submit_vendor_specific(struct libusb_device_handle *handle, struct mode_user_data *user_data, libusb_transfer_cb_fn callback) |
370 | { | ||
370 | struct libusb_transfer* ctrl_transfer = libusb_alloc_transfer(0); | 371 | struct libusb_transfer* ctrl_transfer = libusb_alloc_transfer(0); |
371 | unsigned char* buffer = malloc(LIBUSB_CONTROL_SETUP_SIZE); | 372 | unsigned char* buffer = malloc(LIBUSB_CONTROL_SETUP_SIZE); |
372 | uint8_t bRequestType = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_DEVICE; | 373 | uint8_t bRequestType = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_DEVICE; |
@@ -378,7 +379,8 @@ static int submit_vendor_specific(struct libusb_device_handle *handle, struct mo | |||
378 | return libusb_submit_transfer(ctrl_transfer); | 379 | return libusb_submit_transfer(ctrl_transfer); |
379 | } | 380 | } |
380 | 381 | ||
381 | static void switch_mode_cb(struct libusb_transfer* transfer) { | 382 | static void switch_mode_cb(struct libusb_transfer* transfer) |
383 | { | ||
382 | struct mode_user_data* user_data = transfer->user_data; | 384 | struct mode_user_data* user_data = transfer->user_data; |
383 | 385 | ||
384 | if(transfer->status != LIBUSB_TRANSFER_COMPLETED) { | 386 | if(transfer->status != LIBUSB_TRANSFER_COMPLETED) { |
@@ -393,7 +395,8 @@ static void switch_mode_cb(struct libusb_transfer* transfer) { | |||
393 | free(transfer->user_data); | 395 | free(transfer->user_data); |
394 | } | 396 | } |
395 | 397 | ||
396 | static void get_mode_cb(struct libusb_transfer* transfer) { | 398 | static void get_mode_cb(struct libusb_transfer* transfer) |
399 | { | ||
397 | struct mode_user_data* user_data = transfer->user_data; | 400 | struct mode_user_data* user_data = transfer->user_data; |
398 | int res; | 401 | int res; |
399 | 402 | ||
@@ -406,19 +409,19 @@ static void get_mode_cb(struct libusb_transfer* transfer) { | |||
406 | unsigned char *data = libusb_control_transfer_get_data(transfer); | 409 | unsigned char *data = libusb_control_transfer_get_data(transfer); |
407 | 410 | ||
408 | char* desired_mode = getenv(ENV_DEVICE_MODE); | 411 | char* desired_mode = getenv(ENV_DEVICE_MODE); |
409 | if (!desired_mode) { | 412 | if(!desired_mode) { |
410 | user_data->wIndex = 0x1; | 413 | user_data->wIndex = 0x1; |
411 | } | 414 | } |
412 | else if (!strncmp(desired_mode, "2", 1)) { | 415 | else if(!strncmp(desired_mode, "2", 1)) { |
413 | user_data->wIndex = 0x2; | 416 | user_data->wIndex = 0x2; |
414 | } | 417 | } |
415 | else if (!strncmp(desired_mode, "3", 1)) { | 418 | else if(!strncmp(desired_mode, "3", 1)) { |
416 | user_data->wIndex = 0x3; | 419 | user_data->wIndex = 0x3; |
417 | } | 420 | } |
418 | // Response is 3:3:3 for initial mode, 5:3:3 otherwise. | 421 | // Response is 3:3:3 for initial mode, 5:3:3 otherwise. |
419 | // In later commit, should infer the mode from available configurations and interfaces. | 422 | // In later commit, should infer the mode from available configurations and interfaces. |
420 | usbmuxd_log(LL_INFO, "Received response %i:%i:%i for get_mode request for device %i-%i", data[0], data[1], data[2], user_data->bus, user_data->address); | 423 | usbmuxd_log(LL_INFO, "Received response %i:%i:%i for get_mode request for device %i-%i", data[0], data[1], data[2], user_data->bus, user_data->address); |
421 | if (user_data->wIndex > 1 && data[0] == 3 && data[1] == 3 && data[2] == 3) { | 424 | if(user_data->wIndex > 1 && data[0] == 3 && data[1] == 3 && data[2] == 3) { |
422 | // 3:3:3 means the initial mode | 425 | // 3:3:3 means the initial mode |
423 | usbmuxd_log(LL_WARNING, "Switching device %i-%i mode to %i", user_data->bus, user_data->address, user_data->wIndex); | 426 | usbmuxd_log(LL_WARNING, "Switching device %i-%i mode to %i", user_data->bus, user_data->address, user_data->wIndex); |
424 | 427 | ||
@@ -426,7 +429,7 @@ static void get_mode_cb(struct libusb_transfer* transfer) { | |||
426 | user_data->wValue = 0; | 429 | user_data->wValue = 0; |
427 | user_data->wLength = 1; | 430 | user_data->wLength = 1; |
428 | 431 | ||
429 | if ((res = submit_vendor_specific(transfer->dev_handle, user_data, switch_mode_cb)) != 0) { | 432 | if((res = submit_vendor_specific(transfer->dev_handle, user_data, switch_mode_cb)) != 0) { |
430 | usbmuxd_log(LL_WARNING, "Could not request to switch mode %i for device %i-%i (%i)", user_data->wIndex, user_data->bus, user_data->address, res); | 433 | usbmuxd_log(LL_WARNING, "Could not request to switch mode %i for device %i-%i (%i)", user_data->wIndex, user_data->bus, user_data->address, res); |
431 | } | 434 | } |
432 | } | 435 | } |
@@ -492,13 +495,13 @@ static int usb_device_add(libusb_device* dev) | |||
492 | user_data->wLength = 4; | 495 | user_data->wLength = 4; |
493 | user_data->timeout = 1000; | 496 | user_data->timeout = 1000; |
494 | 497 | ||
495 | if (submit_vendor_specific(handle, user_data, get_mode_cb) != 0) { | 498 | if(submit_vendor_specific(handle, user_data, get_mode_cb) != 0) { |
496 | usbmuxd_log(LL_WARNING, "Could not request current mode from device %d-%d", bus, address); | 499 | usbmuxd_log(LL_WARNING, "Could not request current mode from device %d-%d", bus, address); |
497 | } | 500 | } |
498 | // Potentially, the rest of this function can be factored out and called from get_mode_callback/switch_mode_callback (where desired mode is known) | 501 | // Potentially, the rest of this function can be factored out and called from get_mode_callback/switch_mode_callback (where desired mode is known) |
499 | int desired_config = devdesc.bNumConfigurations; | 502 | int desired_config = devdesc.bNumConfigurations; |
500 | if (desired_config > 4) { | 503 | if(desired_config > 4) { |
501 | if (desired_config > 5) { | 504 | if(desired_config > 5) { |
502 | usbmuxd_log(LL_ERROR, "Device %d-%d has more than 5 configurations, but usbmuxd doesn't support that. Choosing configuration 5 instead.", bus, address); | 505 | usbmuxd_log(LL_ERROR, "Device %d-%d has more than 5 configurations, but usbmuxd doesn't support that. Choosing configuration 5 instead.", bus, address); |
503 | desired_config = 5; | 506 | desired_config = 5; |
504 | } | 507 | } |
@@ -516,10 +519,9 @@ static int usb_device_add(libusb_device* dev) | |||
516 | // Otherwize, 0 is expected to be of a different class. | 519 | // Otherwize, 0 is expected to be of a different class. |
517 | int usbmux_intf_index = config->bNumInterfaces == 3 ? 2 : config->bNumInterfaces == 4 ? 1 : 0; | 520 | int usbmux_intf_index = config->bNumInterfaces == 3 ? 2 : config->bNumInterfaces == 4 ? 1 : 0; |
518 | intf = &config->interface[usbmux_intf_index].altsetting[0]; | 521 | intf = &config->interface[usbmux_intf_index].altsetting[0]; |
519 | if ( | 522 | if(intf->bInterfaceClass != INTERFACE_CLASS || |
520 | intf->bInterfaceClass != INTERFACE_CLASS || | 523 | intf->bInterfaceSubClass != INTERFACE_SUBCLASS || |
521 | intf->bInterfaceSubClass != INTERFACE_SUBCLASS || | 524 | intf->bInterfaceProtocol != INTERFACE_PROTOCOL) { |
522 | intf->bInterfaceProtocol != INTERFACE_PROTOCOL) { | ||
523 | usbmuxd_log(LL_WARNING, "Device %d-%d: can't find usbmux interface in configuration 5, choosing configuration 4 instead.", bus, address); | 525 | usbmuxd_log(LL_WARNING, "Device %d-%d: can't find usbmux interface in configuration 5, choosing configuration 4 instead.", bus, address); |
524 | desired_config = 4; | 526 | desired_config = 4; |
525 | break; | 527 | break; |