Skip to content

Commit e73c5f5

Browse files
committed
WIP: darwin: Create device descriptor from OS cached values
Still missing bcdUSB, and (compile) testing. References libusb#1564 Signed-off-by: Tormod Volden <[email protected]>
1 parent 467b6a8 commit e73c5f5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

libusb/os/darwin_usb.c

+36
Original file line numberDiff line numberDiff line change
@@ -1105,12 +1105,47 @@ static enum libusb_error darwin_cache_device_descriptor (struct libusb_context *
11051105
(*device)->GetDeviceProduct (device, &idProduct);
11061106
(*device)->GetDeviceVendor (device, &idVendor);
11071107

1108+
/* Try synthesize a device descriptor from OS cached values */
1109+
do {
1110+
IOUSBDeviceDescriptor *desc = &dev->dev_descriptor;
1111+
UInt16 bcdDevice;
1112+
1113+
/* If anything fails, fall back to requesting descriptor from device */
1114+
if (!get_ioregistry_value_number (dev->service, CFSTR("bMaxPacketSize0"), kCFNumberSInt8Type, &desc->bMaxPacketSize0))
1115+
break;
1116+
1117+
desc->bcdUSB = libusb_cpu_to_le16(0x0200); // FIXME get from somewhere
1118+
1119+
desc->bDeviceClass = bDeviceClass;
1120+
(*device)->GetDeviceSubClass (device, &desc->bDeviceSubClass);
1121+
(*device)->GetDeviceProtocol (device, &desc->bDeviceProtocol);
1122+
1123+
(*device)->GetDeviceVendor (device, &idVendor);
1124+
desc->idVendor = libusb_cpu_to_le16(idVendor); // needed?
1125+
(*device)->GetDeviceProduct (device, &idProduct);
1126+
desc->idProduct = libusb_cpu_to_le16(idProduct);
1127+
(*device)->GetDeviceReleaseNumber (device, &bcdDevice);
1128+
desc->bcdDevice = libusb_cpu_to_le16(bcdDevice);
1129+
1130+
(*device)->USBGetManufacturerStringIndex (device, &desc->iManufacturer);
1131+
(*device)->USBGetProductStringIndex (device, &desc->iProduct);
1132+
(*device)->USBGetSerialNumberStringIndex (device, &desc->iSerialNumber);
1133+
(*device)->GetNumberOfConfigurations (device, &desc->bNumConfigurations);
1134+
1135+
// if needed do validity checks here
1136+
1137+
desc->bDescriptorType = LIBUSB_DT_DEVICE;
1138+
desc->bLength = LIBUSB_DT_DEVICE_SIZE;
1139+
goto done_desc;
1140+
} while (0);
1141+
11081142
/* According to Apple's documentation the device must be open for DeviceRequest but we may not be able to open some
11091143
* devices and Apple's USB Prober doesn't bother to open the device before issuing a descriptor request. Still,
11101144
* to follow the spec as closely as possible, try opening the device */
11111145
is_open = ((*device)->USBDeviceOpenSeize(device) == kIOReturnSuccess);
11121146

11131147
do {
1148+
usbi_dbg (ctx, "requesting device descriptor from device");
11141149
/**** retrieve device descriptor ****/
11151150
ret = darwin_request_descriptor (device, kUSBDeviceDesc, 0, &dev->dev_descriptor, sizeof(dev->dev_descriptor));
11161151

@@ -1201,6 +1236,7 @@ static enum libusb_error darwin_cache_device_descriptor (struct libusb_context *
12011236
return LIBUSB_ERROR_NO_DEVICE;
12021237
}
12031238

1239+
done_desc:
12041240
usbi_dbg (ctx, "cached device descriptor:");
12051241
usbi_dbg (ctx, " bDescriptorType: 0x%02x", dev->dev_descriptor.bDescriptorType);
12061242
usbi_dbg (ctx, " bcdUSB: 0x%04x", libusb_le16_to_cpu (dev->dev_descriptor.bcdUSB));

0 commit comments

Comments
 (0)