Skip to content

Commit

Permalink
WIP: darwin: Create device descriptor from OS cached values
Browse files Browse the repository at this point in the history
Still missing bcdUSB, and (compile) testing.

References libusb#1564

Signed-off-by: Tormod Volden <[email protected]>
  • Loading branch information
tormodvolden committed Sep 13, 2024
1 parent 467b6a8 commit 3c95826
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions libusb/os/darwin_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,40 @@ static enum libusb_error darwin_cache_device_descriptor (struct libusb_context *
(*device)->GetDeviceProduct (device, &idProduct);
(*device)->GetDeviceVendor (device, &idVendor);

/* Try synthesize a device descriptor from OS cached values */
do {
IOUSBDeviceDescriptor *desc = &dev->dev_descriptor;
UInt16 bcdDevice;

/* If anything fails, fall back to requesting descriptor from device */
if (!get_ioregistry_value_number (dev->service, CFSTR("bMaxPacketSize0"), kCFNumberSInt8Type, &desc->bMaxPacketSize0))
break;

desc->bcdUSB = libusb_cpu_to_le16(0x0200); // FIXME get from somewhere

desc->bDeviceClass = bDeviceClass;
(*device)->GetDeviceSubClass (device, &desc->bDeviceSubClass);
(*device)->GetDeviceProtocol (device, &desc->bDeviceProtocol);

(*device)->GetDeviceVendor (device, &idVendor);
desc->idVendor = libusb_cpu_to_le16(idVendor); // needed?
(*device)->GetDeviceProduct (device, &idProduct);
desc->idProduct = libusb_cpu_to_le16(idProduct);
(*device)->GetDeviceReleaseNumber (device, &bcdDevice);
desc->bcdDevice = libusb_cpu_to_le16(bcdDevice);

(*device)->USBGetManufacturerStringIndex (device, &desc->iManufacturer);
(*device)->USBGetProductStringIndex (device, &desc->iProduct);
(*device)->USBGetSerialNumberStringIndex (device, &desc->iSerialNumber);
(*device)->GetNumberOfConfigurations (device, &desc->bNumConfigurations);

// if needed do validity checks here

desc->bDescriptorType = LIBUSB_DT_DEVICE;
desc->bLength = LIBUSB_DT_DEVICE_SIZE;
goto done_desc;
} while (0);

/* According to Apple's documentation the device must be open for DeviceRequest but we may not be able to open some
* devices and Apple's USB Prober doesn't bother to open the device before issuing a descriptor request. Still,
* to follow the spec as closely as possible, try opening the device */
Expand Down Expand Up @@ -1201,6 +1235,7 @@ static enum libusb_error darwin_cache_device_descriptor (struct libusb_context *
return LIBUSB_ERROR_NO_DEVICE;
}

done_desc:
usbi_dbg (ctx, "cached device descriptor:");
usbi_dbg (ctx, " bDescriptorType: 0x%02x", dev->dev_descriptor.bDescriptorType);
usbi_dbg (ctx, " bcdUSB: 0x%04x", libusb_le16_to_cpu (dev->dev_descriptor.bcdUSB));
Expand Down

0 comments on commit 3c95826

Please sign in to comment.