[USBEHCI]
authorJohannes Anderwald <johannes.anderwald@reactos.org>
Tue, 14 Feb 2012 19:10:34 +0000 (19:10 +0000)
committerJohannes Anderwald <johannes.anderwald@reactos.org>
Tue, 14 Feb 2012 19:10:34 +0000 (19:10 +0000)
- Remove bogus assert
- Store full configuration descriptor
- skip intermediate descriptors until the requested descriptor is found while parsing the configuration descriptor
- simplify CUSBDevice::GetConfigurationDescriptors
- check if an interface descriptor has endpoint descriptors

svn path=/trunk/; revision=55595

reactos/drivers/usb/usbehci/hub_controller.cpp
reactos/drivers/usb/usbehci/usb_device.cpp
reactos/drivers/usb/usbehci/usbehci.h

index 9b97655..acde656 100644 (file)
@@ -1576,11 +1576,6 @@ CHubController::HandleGetDescriptor(
                 //
                 UsbDevice->GetConfigurationDescriptors((PUSB_CONFIGURATION_DESCRIPTOR)Urb->UrbControlDescriptorRequest.TransferBuffer, Urb->UrbControlDescriptorRequest.TransferBufferLength, &Length);
 
-                //
-                // sanity check
-                //
-                PC_ASSERT(Urb->UrbControlDescriptorRequest.TransferBufferLength >= Length);
-
                 //
                 // store result size
                 //
index b2db3cf..b75f970 100644 (file)
@@ -784,7 +784,7 @@ CUSBDevice::CreateConfigurationDescriptor(
     //
     // request is complete, initialize configuration descriptor
     //
-    RtlCopyMemory(&m_ConfigurationDescriptors[Index].ConfigurationDescriptor, ConfigurationDescriptor, ConfigurationDescriptor->bLength);
+    m_ConfigurationDescriptors[Index].ConfigurationDescriptor = ConfigurationDescriptor;
 
     //
     // now allocate interface descriptors
@@ -814,68 +814,93 @@ CUSBDevice::CreateConfigurationDescriptor(
     //
     for(InterfaceIndex = 0; InterfaceIndex < ConfigurationDescriptor->bNumInterfaces; InterfaceIndex++)
     {
-        //
-        // sanity check
-        //
-        PC_ASSERT(InterfaceDescriptor->bLength == sizeof(USB_INTERFACE_DESCRIPTOR));
-        PC_ASSERT(InterfaceDescriptor->bNumEndpoints);
-
-        //
-        // copy current interface descriptor
-        //
-        RtlCopyMemory(&m_ConfigurationDescriptors[Index].Interfaces[InterfaceIndex].InterfaceDescriptor, InterfaceDescriptor, InterfaceDescriptor->bLength);
-
-        //
-        // allocate end point descriptors
-        //
-        m_ConfigurationDescriptors[Index].Interfaces[InterfaceIndex].EndPoints = (PUSB_ENDPOINT)ExAllocatePoolWithTag(NonPagedPool, sizeof(USB_ENDPOINT) * InterfaceDescriptor->bNumEndpoints, TAG_USBEHCI);
-        if (!m_ConfigurationDescriptors[Index].Interfaces[InterfaceIndex].EndPoints)
+        while(InterfaceDescriptor->bDescriptorType != USB_INTERFACE_DESCRIPTOR_TYPE && InterfaceDescriptor->bLength != sizeof(USB_INTERFACE_DESCRIPTOR))
         {
             //
-            // failed to allocate endpoint
+            // move to next descriptor
             //
-            Status = STATUS_INSUFFICIENT_RESOURCES;
-            break;
+            InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
         }
 
         //
-        // zero memory
+        // sanity checks
         //
-        RtlZeroMemory(m_ConfigurationDescriptors[Index].Interfaces[InterfaceIndex].EndPoints, sizeof(USB_ENDPOINT) * InterfaceDescriptor->bNumEndpoints);
+        ASSERT(InterfaceDescriptor->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE);
+        ASSERT(InterfaceDescriptor->bLength == sizeof(USB_INTERFACE_DESCRIPTOR));
 
         //
-        // initialize end point descriptors
+        // copy current interface descriptor
         //
-        EndPointDescriptor = (PUSB_ENDPOINT_DESCRIPTOR)(InterfaceDescriptor + 1);
+        RtlCopyMemory(&m_ConfigurationDescriptors[Index].Interfaces[InterfaceIndex].InterfaceDescriptor, InterfaceDescriptor, InterfaceDescriptor->bLength);
 
-        for(EndPointIndex = 0; EndPointIndex < InterfaceDescriptor->bNumEndpoints; EndPointIndex++)
+        if (InterfaceDescriptor->bNumEndpoints)
         {
             //
-            // sanity check
+            // allocate end point descriptors
             //
-            PC_ASSERT(EndPointDescriptor->bLength == sizeof(USB_ENDPOINT_DESCRIPTOR));
+            m_ConfigurationDescriptors[Index].Interfaces[InterfaceIndex].EndPoints = (PUSB_ENDPOINT)ExAllocatePoolWithTag(NonPagedPool, sizeof(USB_ENDPOINT) * InterfaceDescriptor->bNumEndpoints, TAG_USBEHCI);
+            if (!m_ConfigurationDescriptors[Index].Interfaces[InterfaceIndex].EndPoints)
+            {
+                //
+                // failed to allocate endpoint
+                //
+                Status = STATUS_INSUFFICIENT_RESOURCES;
+                break;
+            }
 
             //
-            // copy endpoint descriptor
+            // zero memory
             //
-            RtlCopyMemory(&m_ConfigurationDescriptors[Index].Interfaces[InterfaceIndex].EndPoints[EndPointIndex].EndPointDescriptor, EndPointDescriptor, EndPointDescriptor->bLength);
+            RtlZeroMemory(m_ConfigurationDescriptors[Index].Interfaces[InterfaceIndex].EndPoints, sizeof(USB_ENDPOINT) * InterfaceDescriptor->bNumEndpoints);
 
             //
-            // move to next offset
+            // initialize end point descriptors
             //
-            EndPointDescriptor = (PUSB_ENDPOINT_DESCRIPTOR)((ULONG_PTR)EndPointDescriptor + EndPointDescriptor->bLength);
-        }
+            EndPointDescriptor = (PUSB_ENDPOINT_DESCRIPTOR)(InterfaceDescriptor + 1);
 
-        //
-        // update interface descriptor offset
-        //
-        InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)EndPointDescriptor;
-    }
+            for(EndPointIndex = 0; EndPointIndex < InterfaceDescriptor->bNumEndpoints; EndPointIndex++)
+            {
+                //
+                // skip other descriptors
+                //
+                while(EndPointDescriptor->bDescriptorType != USB_ENDPOINT_DESCRIPTOR_TYPE && EndPointDescriptor->bLength != sizeof(USB_ENDPOINT_DESCRIPTOR))
+                {
+                    //
+                    // assert when next interface descriptor is reached before the next endpoint
+                    //
+                    ASSERT(EndPointDescriptor->bDescriptorType != USB_INTERFACE_DESCRIPTOR_TYPE);
+                    ASSERT(EndPointDescriptor->bLength);
+                    DPRINT1("InterfaceDescriptor  bNumEndpoints┬┤%x EndpointIndex %x Skipping Descriptor Type %x\n", InterfaceDescriptor->bNumEndpoints, EndPointIndex, EndPointDescriptor->bDescriptorType);
+
+                    //
+                    // move to next descriptor
+                    //
+                    EndPointDescriptor = (PUSB_ENDPOINT_DESCRIPTOR)((ULONG_PTR)EndPointDescriptor + EndPointDescriptor->bLength);
+                }
 
-    //
-    // free buffer
-    //
-    ExFreePoolWithTag(Buffer, TAG_USBEHCI);
+                //
+                // sanity check
+                //
+                ASSERT(EndPointDescriptor->bDescriptorType == USB_ENDPOINT_DESCRIPTOR_TYPE);
+                ASSERT(EndPointDescriptor->bLength == sizeof(USB_ENDPOINT_DESCRIPTOR));
+
+                //
+                // copy endpoint descriptor
+                //
+                RtlCopyMemory(&m_ConfigurationDescriptors[Index].Interfaces[InterfaceIndex].EndPoints[EndPointIndex].EndPointDescriptor, EndPointDescriptor, EndPointDescriptor->bLength);
+
+                //
+                // move to next offset
+                //
+                EndPointDescriptor = (PUSB_ENDPOINT_DESCRIPTOR)((ULONG_PTR)EndPointDescriptor + EndPointDescriptor->bLength);
+            }
+
+            //
+            // update interface descriptor offset
+            //
+            InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)EndPointDescriptor;
+        }
+    }
 
     //
     // done
@@ -889,9 +914,6 @@ CUSBDevice::GetConfigurationDescriptors(
     IN ULONG BufferLength,
     OUT PULONG OutBufferLength)
 {
-    PVOID Buffer;
-    ULONG InterfaceIndex, EndpointIndex;
-
     //
     // sanity check
     //
@@ -910,78 +932,10 @@ CUSBDevice::GetConfigurationDescriptors(
     PC_ASSERT(m_DeviceDescriptor.bNumConfigurations == 1);
 
     //
-    // copy first configuration descriptor
-    //
-    RtlCopyMemory(ConfigDescriptorBuffer, &m_ConfigurationDescriptors[0].ConfigurationDescriptor, sizeof(USB_CONFIGURATION_DESCRIPTOR));
-
-    //
-    // subtract length
-    //
-    BufferLength -= sizeof(USB_CONFIGURATION_DESCRIPTOR);
-    *OutBufferLength += sizeof(USB_CONFIGURATION_DESCRIPTOR);
-
+    // copy configuration descriptor
     //
-    // increment offset
-    //
-    Buffer = (PVOID)(ConfigDescriptorBuffer + 1);
-
-    for(InterfaceIndex = 0; InterfaceIndex < m_ConfigurationDescriptors[0].ConfigurationDescriptor.bNumInterfaces; InterfaceIndex++)
-    {
-        if (BufferLength < sizeof(USB_INTERFACE_DESCRIPTOR))
-        {
-            //
-            // no more room in buffer
-            //
-            return;
-        }
-
-        //
-        // copy interface descriptor
-        //
-        RtlCopyMemory(Buffer, &m_ConfigurationDescriptors[0].Interfaces[InterfaceIndex].InterfaceDescriptor, sizeof(USB_INTERFACE_DESCRIPTOR));
-
-        //
-        // increment offset
-        //
-        Buffer = (PVOID)((ULONG_PTR)Buffer + sizeof(USB_INTERFACE_DESCRIPTOR));
-        BufferLength -= sizeof(USB_INTERFACE_DESCRIPTOR);
-        *OutBufferLength += sizeof(USB_INTERFACE_DESCRIPTOR);
-
-        //
-        // does the interface have endpoints
-        //
-        if (m_ConfigurationDescriptors[0].Interfaces[InterfaceIndex].InterfaceDescriptor.bNumEndpoints)
-        {
-            //
-            // is enough space available
-            //
-            if (BufferLength < sizeof(USB_ENDPOINT_DESCRIPTOR) * m_ConfigurationDescriptors[0].Interfaces[InterfaceIndex].InterfaceDescriptor.bNumEndpoints)
-            {
-                //
-                // no buffer
-                //
-                return;
-            }
-
-            //
-            // copy end points
-            //
-            for(EndpointIndex = 0; EndpointIndex < m_ConfigurationDescriptors[0].Interfaces[InterfaceIndex].InterfaceDescriptor.bNumEndpoints; EndpointIndex++)
-            {
-                //
-                // copy endpoint
-                //
-                RtlCopyMemory(Buffer, &m_ConfigurationDescriptors[0].Interfaces[InterfaceIndex].EndPoints[EndpointIndex].EndPointDescriptor, sizeof(USB_ENDPOINT_DESCRIPTOR));
-
-                //
-                // increment buffer offset
-                //
-                Buffer = (PVOID)((ULONG_PTR)Buffer + sizeof(USB_ENDPOINT_DESCRIPTOR));
-                BufferLength -= sizeof(USB_ENDPOINT_DESCRIPTOR);
-                *OutBufferLength += sizeof(USB_ENDPOINT_DESCRIPTOR);
-            }
-        }
-    }
+    RtlCopyMemory(ConfigDescriptorBuffer, m_ConfigurationDescriptors[0].ConfigurationDescriptor, min(m_ConfigurationDescriptors[0].ConfigurationDescriptor->wTotalLength, BufferLength));
+    *OutBufferLength = m_ConfigurationDescriptors[0].ConfigurationDescriptor->wTotalLength;
 }
 
 //----------------------------------------------------------------------------------------
@@ -993,7 +947,7 @@ CUSBDevice::GetConfigurationDescriptorsLength()
     //
     PC_ASSERT(m_DeviceDescriptor.bNumConfigurations == 1);
 
-    return m_ConfigurationDescriptors[0].ConfigurationDescriptor.wTotalLength;
+    return m_ConfigurationDescriptors[0].ConfigurationDescriptor->wTotalLength;
 }
 //----------------------------------------------------------------------------------------
 VOID
@@ -1094,12 +1048,12 @@ CUSBDevice::SelectConfiguration(
     // sanity checks
     //
     ASSERT(ConfigurationDescriptor->iConfiguration < m_DeviceDescriptor.bNumConfigurations);
-    ASSERT(ConfigurationDescriptor->iConfiguration == m_ConfigurationDescriptors[ConfigurationDescriptor->iConfiguration].ConfigurationDescriptor.iConfiguration);
+    ASSERT(ConfigurationDescriptor->iConfiguration == m_ConfigurationDescriptors[ConfigurationDescriptor->iConfiguration].ConfigurationDescriptor->iConfiguration);
 
     //
     // sanity check
     //
-    ASSERT(ConfigurationDescriptor->bNumInterfaces <= m_ConfigurationDescriptors[ConfigurationDescriptor->iConfiguration].ConfigurationDescriptor.bNumInterfaces);
+    ASSERT(ConfigurationDescriptor->bNumInterfaces <= m_ConfigurationDescriptors[ConfigurationDescriptor->iConfiguration].ConfigurationDescriptor->bNumInterfaces);
 
     //
     // now build setup packet
@@ -1209,10 +1163,10 @@ CUSBDevice::SelectInterface(
     //
     // sanity check
     //
-    ASSERT(Configuration->ConfigurationDescriptor.bDescriptorType == USB_CONFIGURATION_DESCRIPTOR_TYPE);
-    ASSERT(Configuration->ConfigurationDescriptor.bLength == sizeof(USB_CONFIGURATION_DESCRIPTOR));
-    ASSERT(Configuration->ConfigurationDescriptor.iConfiguration < m_DeviceDescriptor.bNumConfigurations);
-    ASSERT(&m_ConfigurationDescriptors[Configuration->ConfigurationDescriptor.iConfiguration] == Configuration);
+    ASSERT(Configuration->ConfigurationDescriptor->bDescriptorType == USB_CONFIGURATION_DESCRIPTOR_TYPE);
+    ASSERT(Configuration->ConfigurationDescriptor->bLength == sizeof(USB_CONFIGURATION_DESCRIPTOR));
+    ASSERT(Configuration->ConfigurationDescriptor->iConfiguration < m_DeviceDescriptor.bNumConfigurations);
+    ASSERT(&m_ConfigurationDescriptors[Configuration->ConfigurationDescriptor->iConfiguration] == Configuration);
 
     //
     // initialize setup packet
@@ -1244,7 +1198,7 @@ CUSBDevice::SelectInterface(
     //
     // sanity checks
     //
-    PC_ASSERT(Configuration->ConfigurationDescriptor.bNumInterfaces > InterfaceInfo->InterfaceNumber);
+    PC_ASSERT(Configuration->ConfigurationDescriptor->bNumInterfaces > InterfaceInfo->InterfaceNumber);
     PC_ASSERT(Configuration->Interfaces[InterfaceInfo->InterfaceNumber].InterfaceDescriptor.bNumEndpoints == InterfaceInfo->NumberOfPipes);
 #ifdef _MSC_VER
     PC_ASSERT(InterfaceInfo->Length == FIELD_OFFSET(USBD_INTERFACE_INFORMATION, Pipes[InterfaceInfo->NumberOfPipes]));
@@ -1273,7 +1227,7 @@ CUSBDevice::SelectInterface(
         //
         // data toggle is reset on select interface requests
         //
-        m_ConfigurationDescriptors[Configuration->ConfigurationDescriptor.iConfiguration].Interfaces[InterfaceInfo->InterfaceNumber].EndPoints[PipeIndex].DataToggle = FALSE;
+        m_ConfigurationDescriptors[Configuration->ConfigurationDescriptor->iConfiguration].Interfaces[InterfaceInfo->InterfaceNumber].EndPoints[PipeIndex].DataToggle = FALSE;
 
         if (Configuration->Interfaces[InterfaceInfo->InterfaceNumber].EndPoints[PipeIndex].EndPointDescriptor.bmAttributes & (USB_ENDPOINT_TYPE_ISOCHRONOUS | USB_ENDPOINT_TYPE_INTERRUPT))
         {
index 8b05ef3..346a3aa 100644 (file)
@@ -55,12 +55,10 @@ typedef struct _USB_INTERFACE
 
 typedef struct _USB_CONFIGURATION
 {
-    USB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor;
+    PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor;
     USB_INTERFACE *Interfaces;
 } USB_CONFIGURATION, *PUSB_CONFIGURATION;
 
-
-
 typedef struct
 {
     BOOLEAN IsFDO;                                               // is device a FDO or PDO