From owner-p4-projects@FreeBSD.ORG Thu Apr 30 17:53:23 2009 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 05EC01065670; Thu, 30 Apr 2009 17:53:23 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B9CC11065674 for ; Thu, 30 Apr 2009 17:53:22 +0000 (UTC) (envelope-from syl@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id A73078FC13 for ; Thu, 30 Apr 2009 17:53:22 +0000 (UTC) (envelope-from syl@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.3/8.14.3) with ESMTP id n3UHrMSN044350 for ; Thu, 30 Apr 2009 17:53:22 GMT (envelope-from syl@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.3/8.14.3/Submit) id n3UHrMna044348 for perforce@freebsd.org; Thu, 30 Apr 2009 17:53:22 GMT (envelope-from syl@FreeBSD.org) Date: Thu, 30 Apr 2009 17:53:22 GMT Message-Id: <200904301753.n3UHrMna044348@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to syl@FreeBSD.org using -f From: Sylvestre Gallon To: Perforce Change Reviews Cc: Subject: PERFORCE change 161394 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Apr 2009 17:53:23 -0000 http://perforce.freebsd.org/chv.cgi?CH=161394 Change 161394 by syl@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);