[LIBUSB][USBCCGP]
[reactos.git] / reactos / drivers / usb / usbccgp / function.c
index 2840166..5d17ade 100644 (file)
@@ -22,26 +22,18 @@ USBCCGP_QueryInterface(
     IO_STATUS_BLOCK IoStatus;
     PIO_STACK_LOCATION Stack;
 
-    //
-    // sanity checks
-    //
+    /* Sanity checks */
     ASSERT(DeviceObject);
 
-    //
-    // initialize event
-    //
+    /* Initialize event */
     KeInitializeEvent(&Event, NotificationEvent, FALSE);
 
-    //
-    // init interface
-    //
+    /* Init interface */
     RtlZeroMemory(BusInterface, sizeof(USBC_DEVICE_CONFIGURATION_INTERFACE_V1));
     BusInterface->Version = USBC_DEVICE_CONFIGURATION_INTERFACE_VERSION_1;
     BusInterface->Size = sizeof(USBC_DEVICE_CONFIGURATION_INTERFACE_V1);
 
-    //
-    // create irp
-    //
+    /* Create irp */
     Irp = IoBuildSynchronousFsdRequest(IRP_MJ_PNP,
                                        DeviceObject,
                                        NULL,
@@ -64,7 +56,7 @@ USBCCGP_QueryInterface(
     //
     // initialize request
     //
-    Stack=IoGetNextIrpStackLocation(Irp);
+    Stack = IoGetNextIrpStackLocation(Irp);
     Stack->MajorFunction = IRP_MJ_PNP;
     Stack->MinorFunction = IRP_MN_QUERY_INTERFACE;
     Stack->Parameters.QueryInterface.Size = sizeof(BUS_INTERFACE_STANDARD);
@@ -77,7 +69,7 @@ USBCCGP_QueryInterface(
     //
     // call driver
     //
-    Status= IoCallDriver(DeviceObject, Irp);
+    Status = IoCallDriver(DeviceObject, Irp);
 
     //
     // did operation complete
@@ -124,14 +116,14 @@ USBCCGP_CustomEnumWithInterface(
     //
     // invoke callback
     //
-    Status = FDODeviceExtension->BusInterface.StartDeviceCallback(FDODeviceExtension->DeviceDescriptor, 
-                                                                  FDODeviceExtension->ConfigurationDescriptor, 
+    Status = FDODeviceExtension->BusInterface.StartDeviceCallback(FDODeviceExtension->DeviceDescriptor,
+                                                                  FDODeviceExtension->ConfigurationDescriptor,
                                                                   &FunctionDescriptorBuffer,
                                                                   &FunctionDescriptorBufferLength,
                                                                   DeviceObject,
                                                                   FDODeviceExtension->PhysicalDeviceObject);
 
-    DPRINT("USBCCGP_CustomEnumWithInterface Status %x\n", Status);
+    DPRINT("USBCCGP_CustomEnumWithInterface Status %lx\n", Status);
     if (!NT_SUCCESS(Status))
     {
         //
@@ -175,7 +167,7 @@ USBCCGP_CountAssociationDescriptors(
     Offset = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->bLength;
     End = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength;
 
-    while(Offset < End)
+    while (Offset < End)
     {
         //
         // get association descriptor
@@ -217,7 +209,7 @@ USBCCGP_GetAssociationDescriptorAtIndex(
     Offset = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->bLength;
     End = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength;
 
-    while(Offset < End)
+    while (Offset < End)
     {
         //
         // get association descriptor
@@ -268,7 +260,7 @@ USBCCGP_InitInterfaceListOfFunctionDescriptor(
     Offset = (PUCHAR)AssociationDescriptor + AssociationDescriptor->bLength;
     End = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength;
 
-    while(Offset < End)
+    while (Offset < End)
     {
         //
         // get association descriptor
@@ -365,12 +357,11 @@ USBCCGP_InitFunctionDescriptor(
         //
         // get interface description
         //
-         Status = USBCCGP_GetDescriptor(FDODeviceExtension->NextDeviceObject, 
-                                        USB_STRING_DESCRIPTOR_TYPE, 
-                                        100 * sizeof(WCHAR), 
-                                        Descriptor->iFunction, 
-                                        0x0409, //FIXME
-                                        (PVOID*)&DescriptionBuffer);
+         Status = USBCCGP_GetStringDescriptor(FDODeviceExtension->NextDeviceObject,
+                                              100 * sizeof(WCHAR),
+                                              Descriptor->iFunction,
+                                              0x0409, //FIXME
+                                              (PVOID*)&DescriptionBuffer);
         if (!NT_SUCCESS(Status))
         {
             //
@@ -395,9 +386,9 @@ USBCCGP_InitFunctionDescriptor(
                                                                          FDODeviceExtension->DeviceDescriptor->idProduct,
                                                                          FDODeviceExtension->DeviceDescriptor->bcdDevice,
                                                                          Descriptor->bFirstInterface) + 1;
-    Index = swprintf(&Buffer[Index], L"USB\\VID_%04x&PID_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
-                                                                        FDODeviceExtension->DeviceDescriptor->idProduct,
-                                                                        Descriptor->bFirstInterface) + 1;
+    Index += swprintf(&Buffer[Index], L"USB\\VID_%04x&PID_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
+                                                                         FDODeviceExtension->DeviceDescriptor->idProduct,
+                                                                         Descriptor->bFirstInterface) + 1;
 
     // allocate result buffer
     DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
@@ -410,18 +401,17 @@ USBCCGP_InitFunctionDescriptor(
     }
 
     // copy description
-    RtlCopyMemory(DescriptionBuffer, Buffer, Index * sizeof(WCHAR));
+    RtlCopyMemory(DescriptionBuffer, Buffer, (Index + 1) * sizeof(WCHAR));
     FunctionDescriptor->HardwareId.Buffer = DescriptionBuffer;
     FunctionDescriptor->HardwareId.Length = Index * sizeof(WCHAR);
     FunctionDescriptor->HardwareId.MaximumLength = (Index + 1) * sizeof(WCHAR);
 
-
     //
     // now init the compatible id
     //
     Index = swprintf(Buffer, L"USB\\Class_%02x&SubClass_%02x&Prot_%02x", Descriptor->bFunctionClass, Descriptor->bFunctionSubClass, Descriptor->bFunctionProtocol) + 1;
-    Index = swprintf(&Buffer[Index], L"USB\\Class_%04x&SubClass_%04x",  Descriptor->bFunctionClass, Descriptor->bFunctionSubClass) + 1;
-    Index = swprintf(&Buffer[Index], L"USB\\Class_%04x", Descriptor->bFunctionClass) + 1;
+    Index += swprintf(&Buffer[Index], L"USB\\Class_%02x&SubClass_%02x",  Descriptor->bFunctionClass, Descriptor->bFunctionSubClass) + 1;
+    Index += swprintf(&Buffer[Index], L"USB\\Class_%02x", Descriptor->bFunctionClass) + 1;
 
     // allocate result buffer
     DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
@@ -434,7 +424,7 @@ USBCCGP_InitFunctionDescriptor(
     }
 
     // copy description
-    RtlCopyMemory(DescriptionBuffer, Buffer, Index * sizeof(WCHAR));
+    RtlCopyMemory(DescriptionBuffer, Buffer, (Index + 1) * sizeof(WCHAR));
     FunctionDescriptor->CompatibleId.Buffer = DescriptionBuffer;
     FunctionDescriptor->CompatibleId.Length = Index * sizeof(WCHAR);
     FunctionDescriptor->CompatibleId.MaximumLength = (Index + 1) * sizeof(WCHAR);
@@ -484,7 +474,7 @@ USBCCGP_EnumWithAssociationDescriptor(
         return STATUS_INSUFFICIENT_RESOURCES;
     }
 
-    for(Index = 0; Index < DescriptorCount; Index++)
+    for (Index = 0; Index < DescriptorCount; Index++)
     {
         //
         // init function descriptors
@@ -530,12 +520,11 @@ USBCCG_InitIdsWithInterfaceDescriptor(
         //
         // get interface description
         //
-         Status = USBCCGP_GetDescriptor(FDODeviceExtension->NextDeviceObject, 
-                                        USB_STRING_DESCRIPTOR_TYPE, 
-                                        100 * sizeof(WCHAR), 
-                                        Descriptor->iInterface, 
-                                        0x0409, //FIXME
-                                        (PVOID*)&DescriptionBuffer);
+         Status = USBCCGP_GetStringDescriptor(FDODeviceExtension->NextDeviceObject,
+                                              100 * sizeof(WCHAR),
+                                              Descriptor->iInterface,
+                                              0x0409, //FIXME
+                                              (PVOID*)&DescriptionBuffer);
         if (!NT_SUCCESS(Status))
         {
             //
@@ -562,8 +551,8 @@ USBCCG_InitIdsWithInterfaceDescriptor(
                                                                          FDODeviceExtension->DeviceDescriptor->bcdDevice,
                                                                          FunctionIndex) + 1;
     Index += swprintf(&Buffer[Index], L"USB\\VID_%04x&PID_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
-                                                                        FDODeviceExtension->DeviceDescriptor->idProduct,
-                                                                        FunctionIndex) + 1;
+                                                                         FDODeviceExtension->DeviceDescriptor->idProduct,
+                                                                         FunctionIndex) + 1;
 
     // allocate result buffer
     DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
@@ -576,7 +565,7 @@ USBCCG_InitIdsWithInterfaceDescriptor(
     }
 
     // copy description
-    RtlCopyMemory(DescriptionBuffer, Buffer, Index * sizeof(WCHAR));
+    RtlCopyMemory(DescriptionBuffer, Buffer, (Index + 1) * sizeof(WCHAR));
     FunctionDescriptor->HardwareId.Buffer = DescriptionBuffer;
     FunctionDescriptor->HardwareId.Length = Index * sizeof(WCHAR);
     FunctionDescriptor->HardwareId.MaximumLength = (Index + 1) * sizeof(WCHAR);
@@ -599,7 +588,7 @@ USBCCG_InitIdsWithInterfaceDescriptor(
     }
 
     // copy description
-    RtlCopyMemory(DescriptionBuffer, Buffer, Index * sizeof(WCHAR));
+    RtlCopyMemory(DescriptionBuffer, Buffer, (Index + 1) * sizeof(WCHAR));
     FunctionDescriptor->CompatibleId.Buffer = DescriptionBuffer;
     FunctionDescriptor->CompatibleId.Length = Index * sizeof(WCHAR);
     FunctionDescriptor->CompatibleId.MaximumLength = (Index + 1) * sizeof(WCHAR);
@@ -647,7 +636,7 @@ USBCCGP_LegacyEnum(
     //
     // init function descriptors
     //
-    for(Index = 0; Index < FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces; Index++)
+    for (Index = 0; Index < FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces; Index++)
     {
         // get interface descriptor
         InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx(FDODeviceExtension->ConfigurationDescriptor, FDODeviceExtension->ConfigurationDescriptor, Index, 0, -1, -1, -1);
@@ -689,7 +678,7 @@ USBCCGP_LegacyEnum(
             //
             // failed to init ids
             //
-            DPRINT1("[USBCCGP] Failed to init ids with %x\n", Status);
+            DPRINT1("[USBCCGP] Failed to init ids with %lx\n", Status);
             return Status;
         }
 
@@ -721,7 +710,6 @@ USBCCGP_EnumWithAudioLegacy(
     PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor, FirstDescriptor = NULL;
     PFDO_DEVICE_EXTENSION FDODeviceExtension;
     NTSTATUS Status = STATUS_SUCCESS;
-    PVOID StartPosition;
 
     //
     // get device extension
@@ -733,21 +721,15 @@ USBCCGP_EnumWithAudioLegacy(
     //
     // first check if all interfaces belong to the same audio class
     //
-    StartPosition = FDODeviceExtension->ConfigurationDescriptor;
-    for(Index = 0; Index < CountInterfaceDescriptors(FDODeviceExtension->ConfigurationDescriptor); Index++)
+    for (Index = 0; Index < FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces; Index++)
     {
         //
         // get interface descriptor
         //
-        InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx(FDODeviceExtension->ConfigurationDescriptor, StartPosition, -1, -1, -1, -1, -1);
+        InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx(FDODeviceExtension->ConfigurationDescriptor, FDODeviceExtension->ConfigurationDescriptor, Index, 0, -1, -1, -1);
         DPRINT1("Index %lu Descriptor %p\n", Index, InterfaceDescriptor);
         ASSERT(InterfaceDescriptor);
 
-        //
-        // move to next descriptor
-        //
-        StartPosition = (PVOID)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
-
         if (InterfaceDescriptor->bInterfaceClass != 0x1)
         {
             //
@@ -806,7 +788,7 @@ USBCCGP_EnumWithAudioLegacy(
         //
         // failed to allocate descriptor array
         //
-        DPRINT1("[USBCCGP] Failed to allocate descriptor array %x\n", Status);
+        DPRINT1("[USBCCGP] Failed to allocate descriptor array %lx\n", Status);
         return Status;
     }
 
@@ -819,14 +801,14 @@ USBCCGP_EnumWithAudioLegacy(
         //
         // failed to init ids
         //
-        DPRINT1("[USBCCGP] Failed to init ids with %x\n", Status);
+        DPRINT1("[USBCCGP] Failed to init ids with %lx\n", Status);
         return Status;
     }
 
     //
     // number of interfaces
     //
-    FDODeviceExtension->FunctionDescriptor[0].NumberOfInterfaces = CountInterfaceDescriptors(FDODeviceExtension->ConfigurationDescriptor);
+    FDODeviceExtension->FunctionDescriptor[0].NumberOfInterfaces = FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces;
 
     //
     // store function count