PERFORCE change 161394 for review
Sylvestre Gallon
syl at FreeBSD.org
Thu Apr 30 17:53:23 UTC 2009
http://perforce.freebsd.org/chv.cgi?CH=161394
Change 161394 by syl at syl_atuin on 2009/04/30 17:52:29
Cleaning the code following Hans Petter Selasky advices.
Affected files ...
.. //depot/projects/soc2009/syl_usb/src/lib/libusb/libusb10_desc.c#4 edit
Differences ...
==== //depot/projects/soc2009/syl_usb/src/lib/libusb/libusb10_desc.c#4 (text+ko) ====
@@ -78,167 +78,108 @@
return (libusb_get_config_descriptor(dev, idx, config));
}
-/*
- * XXX Code need to be updated concerning altsetting
+/*
+ * Need to handle extra field.
*/
int
libusb_get_config_descriptor(libusb_device * dev, uint8_t config_index,
struct libusb_config_descriptor **config)
{
- struct LIBUSB20_DEVICE_DESC_DECODED ddev;
- struct LIBUSB20_CONFIG_DESC_DECODED dconf;
- struct LIBUSB20_INTERFACE_DESC_DECODED dinf;
- struct LIBUSB20_ENDPOINT_DESC_DECODED dend;
struct libusb20_device *pdev;
- struct libusb20_me_struct me;
+ struct libusb20_config *pconf;
+ struct libusb20_interface *pinf;
+ struct libusb20_endpoint *pend;
libusb_interface_descriptor *ifd;
libusb_endpoint_descriptor *endd;
uint8_t nif, nend, i, j;
- const char *ptr;
- char *ptr_save;
if (dev == NULL || config == NULL)
return (LIBUSB_ERROR_NO_MEM);
- LIBUSB20_INIT(LIBUSB20_DEVICE_DESC, &ddev);
- LIBUSB20_INIT(LIBUSB20_CONFIG_DESC, &dconf);
- LIBUSB20_INIT(LIBUSB20_INTERFACE_DESC, &dinf);
- LIBUSB20_INIT(LIBUSB20_ENDPOINT_DESC, &dend);
-
pdev = dev->os_priv;
- ptr = libusb20_dev_get_desc(pdev);
+ pconf = libusb20_dev_alloc_config(pdev, config_index);
- /*
- * Get the good configuration.
- */
-
- me.ptr = LIBUSB20_ADD_BYTES(ptr,0);
- me.len = strlen(ptr);
- me.type = LIBUSB20_ME_IS_RAW;
-
- while ((ptr = libusb20_desc_foreach(&me, ptr))) {
- switch (ptr[1]) {
- case LIBUSB20_DT_DEVICE:
- libusb20_me_decode(ptr, ptr[0], &ddev);
- if (ddev.bNumConfigurations < config_index)
- return LIBUSB_ERROR_NOT_FOUND;
- break;
- case LIBUSB20_DT_CONFIG:
- libusb20_me_decode(ptr, ptr[0], &dconf);
- if (dconf.bConfigurationValue == config_index)
- goto out;
- break;
- default:
- break;
- }
- }
-
-out:
- if (ptr[1] != LIBUSB20_DT_CONFIG)
+ if (pconf == NULL)
return (LIBUSB_ERROR_NOT_FOUND);
- /*
- * Get number of interfaces and enpoints for allocation
- */
-
- me.ptr = LIBUSB20_ADD_BYTES(ptr, 0);
- me.len = dconf.wTotalLength;
- me.type = LIBUSB20_ME_IS_RAW;
-
- ptr_save = (char *)ptr;
- nif = nend = 0;
- while ((ptr = libusb20_desc_foreach(&me, ptr))) {
- if (ptr[1] == LIBUSB20_DT_INTERFACE)
- nif++;
- else if (ptr[1] == LIBUSB20_DT_ENDPOINT)
- nend++;
- }
+ nif = pconf->num_interface;
+ nend = 0;
+ for (i = 0 ; i < nif ; i++)
+ nend += pconf->interface[i].num_endpoints;
- /*
- * Alloc config and fill it
- */
*config = malloc(sizeof(libusb_config_descriptor) +
(nif * sizeof(libusb_interface)) +
(nif * sizeof(libusb_interface_descriptor)) +
(nend * sizeof(libusb_endpoint_descriptor)));
+ if (*config == NULL) {
+ free(pconf);
+ return (LIBUSB_ERROR_NO_MEM);
+ }
+
+ (*config)->bLength = pconf->desc.bLength;
+ (*config)->bDescriptorType = pconf->desc.bDescriptorType;
+ (*config)->wTotalLength = pconf->desc.wTotalLength;
+ (*config)->bNumInterfaces = pconf->desc.bNumInterfaces;
+ (*config)->bConfigurationValue = pconf->desc.bConfigurationValue;
+ (*config)->iConfiguration = pconf->desc.iConfiguration;
+ (*config)->bmAttributes = pconf->desc.bmAttributes;
+ (*config)->MaxPower = pconf->desc.bMaxPower;
- ptr = (const char *)ptr_save;
- me.ptr = LIBUSB20_ADD_BYTES(ptr, 0);
- me.len = dconf.wTotalLength;
- me.type = LIBUSB20_ME_IS_RAW;
- i = j = 0 - 1;
- ifd = NULL;
+ for ( i = 0 ; i < nif ; i++) {
+ pinf = &pconf->interface[i];
+ (*config)->interface[i].num_altsetting = pinf->num_altsetting;
+ ifd = (*config)->interface[i].altsetting;
+ ifd->bLength = pinf->desc.bLength;
+ ifd->bDescriptorType = pinf->desc.bDescriptorType;
+ ifd->bInterfaceNumber = pinf->desc.bInterfaceNumber;
+ ifd->bAlternateSetting = pinf->desc.bAlternateSetting;
+ ifd->bNumEndpoints = pinf->desc.bNumEndpoints;
+ ifd->bInterfaceClass = pinf->desc.bInterfaceClass;
+ ifd->bInterfaceSubClass = pinf->desc.bInterfaceSubClass;
+ ifd->bInterfaceProtocol = pinf->desc.bInterfaceProtocol;
+ ifd->iInterface = pinf->desc.iInterface;
- while (ptr = libusb20_desc_foreach(&me, ptr)) {
- switch (ptr[i]) {
- case LIBUSB20_DT_INTERFACE:
- i++;
- j = 0 - 1;
- libusb20_me_decode(ptr, ptr[0], &dinf);
- (*config)->interface[i].num_altsetting =
- dconf.bNumInterfaces - 1;
- ifd = (*config)->interface[i].altsetting;
- ifd->bLength = dinf.bLength;
- ifd->bDescriptorType = dinf.bDescriptorType;
- ifd->bInterfaceNumber = dinf.bInterfaceNumber;
- ifd->bAlternateSetting = dinf.bAlternateSetting;
- ifd->bNumEndpoints = dinf.bNumEndpoints;
- ifd->bInterfaceClass = dinf.bInterfaceClass;
- ifd->bInterfaceSubClass = dinf.bInterfaceSubClass;
- ifd->bInterfaceProtocol = dinf.bInterfaceProtocol;
- ifd->iInterface = dinf.iInterface;
- break;
- case LIBUSB20_DT_ENDPOINT:
- if (ifd != NULL) {
- j++;
- libusb20_me_decode(ptr, ptr[0], &dend);
- endd = &ifd->endpoint[j];
- endd->bLength = dend.bLength;
- endd->bDescriptorType = dend.bDescriptorType;
- endd->bEndpointAddress = dend.bEndpointAddress;
- endd->bmAttributes = dend.bmAttributes;
- endd->wMaxPacketSize = dend.wMaxPacketSize;
- endd->bInterval = dend.bInterval;
- endd->bRefresh = dend.bRefresh;
- endd->bSynchAddress = dend.bSynchAddress;
- break;
- }
+ for (j = 0 ; j < pinf->num_endpoints ; j++) {
+ pend = &pconf->interface[i].endpoints[j];
+ endd = &ifd->endpoint[j];
+ endd->bLength = pend->desc.bLength;
+ endd->bDescriptorType = pend->desc.bDescriptorType;
+ endd->bEndpointAddress = pend->desc.bEndpointAddress;
+ endd->bmAttributes = pend->desc.bmAttributes;
+ endd->wMaxPacketSize = pend->desc.wMaxPacketSize;
+ endd->bInterval = pend->desc.bInterval;
+ endd->bRefresh = pend->desc.bRefresh;
+ endd->bSynchAddress = pend->desc.bSynchAddress;
}
}
+ free(pconf);
return (0);
}
-/*
- * XXX Check that value means bConfigurationValue...
- */
int
libusb_get_config_descriptor_by_value(libusb_device * dev,
uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
{
- struct LIBUSB20_CONFIG_DESC_DECODED *pconf;
+ struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
struct libusb20_device *pdev;
- struct libusb20_me_struct me;
- const char *ptr;
+ struct libusb20_config *pconf;
+ int i;
if (dev == NULL || config == NULL)
return (LIBUSB_ERROR_NO_MEM);
pdev = dev->os_priv;
- ptr = libusb20_dev_get_desc(pdev);
+ pdesc = libusb20_dev_get_device_desc(pdev);
+ for (i = 0 ; i < pdesc->bNumConfigurations ; i++) {
+ pconf = libusb20_dev_alloc_config(pdev, i);
+ if (pconf->desc.bConfigurationValue == bConfigurationValue) {
+ free(pconf);
+ return libusb_get_config_descriptor(dev, i, config);
- me.ptr = LIBUSB20_ADD_BYTES(ptr, 0);
- me.len = strlen(ptr);
- me.type = LIBUSB20_ME_IS_RAW;
-
- while (ptr = libusb20_desc_foreach(&me, ptr)) {
- if (ptr[1] == LIBUSB20_DT_CONFIG) {
- pconf = (struct LIBUSB20_CONFIG_DESC_DECODED *) ptr;
- if (pconf->bConfigurationValue == bConfigurationValue)
- return (libusb_get_config_descriptor(dev,
- pconf->bConfigurationValue , config));
}
+ free(pconf);
}
return (LIBUSB_ERROR_NOT_FOUND);
More information about the p4-projects
mailing list