2 * PROJECT: ReactOS Universal Serial Bus Hub Driver
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: drivers/usb/usbhub/fdo.c
7 * Michael Martin (michael.martin@reactos.org)
8 * Johannes Anderwald (johannes.anderwald@reactos.org)
15 QueryStatusChangeEndpoint(
16 IN PDEVICE_OBJECT DeviceObject
);
19 CreateUsbChildDeviceObject(
20 IN PDEVICE_OBJECT UsbHubDeviceObject
,
22 OUT PDEVICE_OBJECT
*UsbChildDeviceObject
,
26 DestroyUsbChildDeviceObject(
27 IN PDEVICE_OBJECT UsbHubDeviceObject
,
32 GetPortStatusAndChange(
33 IN PDEVICE_OBJECT RootHubDeviceObject
,
35 OUT PPORT_STATUS_CHANGE StatusChange
)
43 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
46 DPRINT1("Failed to allocate memory for URB!\n");
47 return STATUS_INSUFFICIENT_RESOURCES
;
53 RtlZeroMemory(Urb
, sizeof(URB
));
56 // Initialize URB for getting Port Status
58 UsbBuildVendorRequest(Urb
,
59 URB_FUNCTION_CLASS_OTHER
,
60 sizeof(Urb
->UrbControlVendorClassRequest
),
61 USBD_TRANSFER_DIRECTION_OUT
,
63 USB_REQUEST_GET_STATUS
,
68 sizeof(PORT_STATUS_CHANGE
),
71 // FIXME: support usb hubs
72 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;
78 Status
= SubmitRequestToRootHub(RootHubDeviceObject
, IOCTL_INTERNAL_USB_SUBMIT_URB
, Urb
, NULL
);
90 IN PDEVICE_OBJECT RootHubDeviceObject
,
100 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
103 DPRINT1("Failed to allocate memory for URB!\n");
104 return STATUS_INSUFFICIENT_RESOURCES
;
110 RtlZeroMemory(Urb
, sizeof(URB
));
113 // Initialize URB for Clearing Port Reset
115 UsbBuildVendorRequest(Urb
,
116 URB_FUNCTION_CLASS_OTHER
,
117 sizeof(Urb
->UrbControlVendorClassRequest
),
118 USBD_TRANSFER_DIRECTION_IN
,
120 USB_REQUEST_SET_FEATURE
,
128 // FIXME support usbhubs
129 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;
132 // Query the Root Hub
134 Status
= SubmitRequestToRootHub(RootHubDeviceObject
, IOCTL_INTERNAL_USB_SUBMIT_URB
, Urb
, NULL
);
146 IN PDEVICE_OBJECT RootHubDeviceObject
,
156 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
159 DPRINT1("Failed to allocate memory for URB!\n");
160 return STATUS_INSUFFICIENT_RESOURCES
;
166 RtlZeroMemory(Urb
, sizeof(URB
));
169 // Initialize URB for Clearing Port Reset
171 UsbBuildVendorRequest(Urb
,
172 URB_FUNCTION_CLASS_OTHER
,
173 sizeof(Urb
->UrbControlVendorClassRequest
),
174 USBD_TRANSFER_DIRECTION_IN
,
176 USB_REQUEST_CLEAR_FEATURE
,
184 // FIXME: support usb hubs
185 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;
188 // Query the Root Hub
190 Status
= SubmitRequestToRootHub(RootHubDeviceObject
, IOCTL_INTERNAL_USB_SUBMIT_URB
, Urb
, NULL
);
201 DeviceStatusChangeThread(
205 PDEVICE_OBJECT DeviceObject
, RootHubDeviceObject
;
206 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
207 PWORK_ITEM_DATA WorkItemData
;
208 PORT_STATUS_CHANGE PortStatus
;
210 BOOLEAN SignalResetComplete
= FALSE
;
212 DPRINT("Entered DeviceStatusChangeThread, Context %x\n", Context
);
214 WorkItemData
= (PWORK_ITEM_DATA
)Context
;
215 DeviceObject
= (PDEVICE_OBJECT
)WorkItemData
->Context
;
216 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
217 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
221 for (PortId
= 1; PortId
<= HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
; PortId
++)
226 Status
= GetPortStatusAndChange(RootHubDeviceObject
, PortId
, &PortStatus
);
227 if (!NT_SUCCESS(Status
))
229 DPRINT1("Failed to get port status for port %d, Status %x\n", PortId
, Status
);
230 // FIXME: Do we really want to halt further SCE requests?
234 DPRINT1("Port %d Status %x\n", PortId
, PortStatus
.Status
);
235 DPRINT1("Port %d Change %x\n", PortId
, PortStatus
.Change
);
239 // Check for new device connection
241 if (PortStatus
.Change
& USB_PORT_STATUS_CONNECT
)
244 // Clear Port Connect
246 Status
= ClearPortFeature(RootHubDeviceObject
, PortId
, C_PORT_CONNECTION
);
247 if (!NT_SUCCESS(Status
))
249 DPRINT1("Failed to clear connection change for port %d\n", PortId
);
254 // Is this a connect or disconnect?
256 if (!(PortStatus
.Status
& USB_PORT_STATUS_CONNECT
))
258 DPRINT1("Device disconnected from port %d\n", PortId
);
260 Status
= DestroyUsbChildDeviceObject(DeviceObject
, PortId
);
261 if (!NT_SUCCESS(Status
))
263 DPRINT1("Failed to delete child device object after disconnect\n");
269 DPRINT1("Device connected from port %d\n", PortId
);
271 // No SCE completion done for clearing C_PORT_CONNECT
276 Status
= SetPortFeature(RootHubDeviceObject
, PortId
, PORT_RESET
);
277 if (!NT_SUCCESS(Status
))
279 DPRINT1("Failed to reset port %d\n", PortId
);
280 SignalResetComplete
= TRUE
;
285 else if (PortStatus
.Change
& USB_PORT_STATUS_ENABLE
)
290 Status
= ClearPortFeature(RootHubDeviceObject
, PortId
, C_PORT_ENABLE
);
291 if (!NT_SUCCESS(Status
))
293 DPRINT1("Failed to clear enable change on port %d\n", PortId
);
297 else if (PortStatus
.Change
& USB_PORT_STATUS_RESET
)
300 // Request event signalling later
302 SignalResetComplete
= TRUE
;
307 Status
= ClearPortFeature(RootHubDeviceObject
, PortId
, C_PORT_RESET
);
308 if (!NT_SUCCESS(Status
))
310 DPRINT1("Failed to clear reset change on port %d\n", PortId
);
317 Status
= GetPortStatusAndChange(RootHubDeviceObject
, PortId
, &PortStatus
);
318 if (!NT_SUCCESS(Status
))
320 DPRINT1("Failed to get port status for port %d, Status %x\n", PortId
, Status
);
321 // FIXME: Do we really want to halt further SCE requests?
325 DPRINT1("Port %d Status %x\n", PortId
, PortStatus
.Status
);
326 DPRINT1("Port %d Change %x\n", PortId
, PortStatus
.Change
);
329 // Check that reset was cleared
331 if(PortStatus
.Change
& USB_PORT_STATUS_RESET
)
333 DPRINT1("Port did not clear reset! Possible Hardware problem!\n");
338 // Check if the device is still connected
340 if (!(PortStatus
.Status
& USB_PORT_STATUS_CONNECT
))
342 DPRINT1("Device has been disconnected\n");
347 // Make sure its Connected and Enabled
349 if (!(PortStatus
.Status
& (USB_PORT_STATUS_CONNECT
| USB_PORT_STATUS_ENABLE
)))
351 DPRINT1("Usb Device is not connected and enabled!\n");
353 // Attempt another reset
355 Status
= SetPortFeature(RootHubDeviceObject
, PortId
, PORT_RESET
);
356 if (!NT_SUCCESS(Status
))
358 DPRINT1("Failed to reset port %d\n", PortId
);
364 // This is a new device
366 Status
= CreateUsbChildDeviceObject(DeviceObject
, PortId
, NULL
, PortStatus
.Status
);
370 ExFreePool(WorkItemData
);
373 // Send another SCE Request
375 DPRINT("Sending another SCE!\n");
376 QueryStatusChangeEndpoint(DeviceObject
);
379 // Check if a reset event was satisfied
381 if (SignalResetComplete
)
384 // Signal anyone waiting on it
386 KeSetEvent(&HubDeviceExtension
->ResetComplete
, IO_NO_INCREMENT
, FALSE
);
392 StatusChangeEndpointCompletion(
393 IN PDEVICE_OBJECT DeviceObject
,
397 PDEVICE_OBJECT RealDeviceObject
;
398 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
399 PWORK_ITEM_DATA WorkItemData
;
401 RealDeviceObject
= (PDEVICE_OBJECT
)Context
;
402 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)RealDeviceObject
->DeviceExtension
;
405 // NOTE: USBPORT frees this IRP
407 DPRINT("Received Irp %x, HubDeviceExtension->PendingSCEIrp %x\n", Irp
, HubDeviceExtension
->PendingSCEIrp
);
411 // Create and initialize work item data
413 WorkItemData
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(WORK_ITEM_DATA
), USB_HUB_TAG
);
416 DPRINT1("Failed to allocate memory!n");
417 return STATUS_INSUFFICIENT_RESOURCES
;
419 WorkItemData
->Context
= RealDeviceObject
;
421 DPRINT("Queuing work item\n");
424 // Queue the work item to handle initializing the device
426 ExInitializeWorkItem(&WorkItemData
->WorkItem
, DeviceStatusChangeThread
, (PVOID
)WorkItemData
);
427 ExQueueWorkItem(&WorkItemData
->WorkItem
, DelayedWorkQueue
);
430 // Return more processing required so the IO Manger doesn’t try to mess with IRP just freed
432 return STATUS_MORE_PROCESSING_REQUIRED
;
436 QueryStatusChangeEndpoint(
437 IN PDEVICE_OBJECT DeviceObject
)
440 PDEVICE_OBJECT RootHubDeviceObject
;
441 PIO_STACK_LOCATION Stack
;
442 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
445 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
446 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
451 PendingSCEUrb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
454 // Initialize URB for Status Change Endpoint request
456 UsbBuildInterruptOrBulkTransferRequest(PendingSCEUrb
,
457 sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER
),
458 HubDeviceExtension
->PipeHandle
,
459 HubDeviceExtension
->PortStatusChange
,
461 sizeof(USHORT
) * 2 * HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
,
462 USBD_TRANSFER_DIRECTION_IN
| USBD_SHORT_TRANSFER_OK
,
465 // Set the device handle
466 PendingSCEUrb
->UrbHeader
.UsbdDeviceHandle
= HubDeviceExtension
->RootHubHandle
;
471 HubDeviceExtension
->PendingSCEIrp
= ExAllocatePoolWithTag(NonPagedPool
,
472 IoSizeOfIrp(RootHubDeviceObject
->StackSize
),
475 HubDeviceExtension->PendingSCEIrp = IoAllocateIrp(RootHubDeviceObject->StackSize,
478 DPRINT("Allocated IRP %x\n", HubDeviceExtension
->PendingSCEIrp
);
480 if (!HubDeviceExtension
->PendingSCEIrp
)
482 DPRINT1("USBHUB: Failed to allocate IRP for SCE request!\n");
483 return STATUS_INSUFFICIENT_RESOURCES
;
487 // Initialize the IRP
489 IoInitializeIrp(HubDeviceExtension
->PendingSCEIrp
,
490 IoSizeOfIrp(RootHubDeviceObject
->StackSize
),
491 RootHubDeviceObject
->StackSize
);
493 HubDeviceExtension
->PendingSCEIrp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
494 HubDeviceExtension
->PendingSCEIrp
->IoStatus
.Information
= 0;
495 HubDeviceExtension
->PendingSCEIrp
->Flags
= 0;
496 HubDeviceExtension
->PendingSCEIrp
->UserBuffer
= NULL
;
499 // Get the Next Stack Location and Initialize it
501 Stack
= IoGetNextIrpStackLocation(HubDeviceExtension
->PendingSCEIrp
);
502 Stack
->DeviceObject
= DeviceObject
;
503 Stack
->Parameters
.Others
.Argument1
= PendingSCEUrb
;
504 Stack
->Parameters
.Others
.Argument2
= NULL
;
505 Stack
->MajorFunction
= IRP_MJ_INTERNAL_DEVICE_CONTROL
;
506 Stack
->Parameters
.DeviceIoControl
.IoControlCode
= IOCTL_INTERNAL_USB_SUBMIT_URB
;
509 // Set the completion routine for when device is connected to root hub
511 IoSetCompletionRoutine(HubDeviceExtension
->PendingSCEIrp
,
512 StatusChangeEndpointCompletion
,
521 DPRINT("DeviceObject is %x\n", DeviceObject
);
522 DPRINT("Iocalldriver %x with irp %x\n", RootHubDeviceObject
, HubDeviceExtension
->PendingSCEIrp
);
523 Status
= IoCallDriver(RootHubDeviceObject
, HubDeviceExtension
->PendingSCEIrp
);
525 return STATUS_PENDING
;
530 IN PDEVICE_OBJECT DeviceObject
,
531 IN CONST GUID InterfaceType
,
538 IO_STATUS_BLOCK IoStatus
;
540 PIO_STACK_LOCATION Stack
= NULL
;
543 // Initialize the Event used to wait for Irp completion
545 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
548 // Build Control Request
550 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_PNP
,
559 // Get Next Stack Location and Initialize it.
561 Stack
= IoGetNextIrpStackLocation(Irp
);
562 Stack
->MinorFunction
= IRP_MN_QUERY_INTERFACE
;
563 Stack
->Parameters
.QueryInterface
.InterfaceType
= &InterfaceType
;//USB_BUS_INTERFACE_HUB_GUID;
564 Stack
->Parameters
.QueryInterface
.Size
= Size
;
565 Stack
->Parameters
.QueryInterface
.Version
= Version
;
566 Stack
->Parameters
.QueryInterface
.Interface
= Interface
;
567 Stack
->Parameters
.QueryInterface
.InterfaceSpecificData
= NULL
;
569 Status
= IoCallDriver(DeviceObject
, Irp
);
571 if (Status
== STATUS_PENDING
)
573 DPRINT("Operation pending\n");
574 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
575 Status
= IoStatus
.Status
;
582 GetUsbDeviceDescriptor(
583 IN PDEVICE_OBJECT ChildDeviceObject
,
584 IN UCHAR DescriptorType
,
587 OUT PVOID TransferBuffer
,
588 IN ULONG TransferBufferLength
)
591 PDEVICE_OBJECT RootHubDeviceObject
;
593 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
594 PHUB_CHILDDEVICE_EXTENSION ChildDeviceExtension
;
597 // Get the Hubs Device Extension
599 ChildDeviceExtension
= (PHUB_CHILDDEVICE_EXTENSION
)ChildDeviceObject
->DeviceExtension
;
600 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) ChildDeviceExtension
->ParentDeviceObject
->DeviceExtension
;
601 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
606 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
609 DPRINT1("Failed to allocate memory for URB!\n");
610 return STATUS_INSUFFICIENT_RESOURCES
;
616 RtlZeroMemory(Urb
, sizeof(URB
));
619 // Initialize URB for getting device descriptor
621 UsbBuildGetDescriptorRequest(Urb
,
622 sizeof(Urb
->UrbControlDescriptorRequest
),
628 TransferBufferLength
,
632 // Set the device handle
634 Urb
->UrbHeader
.UsbdDeviceHandle
= (PVOID
)ChildDeviceExtension
->UsbDeviceHandle
;
637 // Query the Root Hub
639 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
640 IOCTL_INTERNAL_USB_SUBMIT_URB
,
648 GetUsbStringDescriptor(
649 IN PDEVICE_OBJECT ChildDeviceObject
,
652 OUT PVOID
*TransferBuffer
,
656 PUSB_STRING_DESCRIPTOR StringDesc
= NULL
;
660 StringDesc
= ExAllocatePoolWithTag(NonPagedPool
,
661 sizeof(USB_STRING_DESCRIPTOR
),
665 DPRINT1("Failed to allocate buffer for string!\n");
666 return STATUS_INSUFFICIENT_RESOURCES
;
670 // Get the index string descriptor length
671 // FIXME: Implement LangIds
673 Status
= GetUsbDeviceDescriptor(ChildDeviceObject
,
674 USB_STRING_DESCRIPTOR_TYPE
,
678 sizeof(USB_STRING_DESCRIPTOR
));
679 if (!NT_SUCCESS(Status
))
681 DPRINT1("GetUsbDeviceDescriptor failed with status %x\n", Status
);
682 ExFreePool(StringDesc
);
685 DPRINT1("StringDesc->bLength %d\n", StringDesc
->bLength
);
688 // Did we get something more than the length of the first two fields of structure?
690 if (StringDesc
->bLength
== 2)
692 DPRINT1("USB Device Error!\n");
693 ExFreePool(StringDesc
);
694 return STATUS_DEVICE_DATA_ERROR
;
696 SizeNeeded
= StringDesc
->bLength
+ sizeof(WCHAR
);
701 ExFreePool(StringDesc
);
704 // Recreate with appropriate size
706 StringDesc
= ExAllocatePoolWithTag(NonPagedPool
,
711 DPRINT1("Failed to allocate buffer for string!\n");
712 return STATUS_INSUFFICIENT_RESOURCES
;
715 RtlZeroMemory(StringDesc
, SizeNeeded
);
720 Status
= GetUsbDeviceDescriptor(ChildDeviceObject
,
721 USB_STRING_DESCRIPTOR_TYPE
,
726 if (!NT_SUCCESS(Status
))
728 DPRINT1("GetUsbDeviceDescriptor failed with status %x\n", Status
);
729 ExFreePool(StringDesc
);
734 // Allocate Buffer to return
736 Buffer
= ExAllocatePoolWithTag(NonPagedPool
,
741 DPRINT1("Failed to allocate buffer for string!\n");
742 ExFreePool(StringDesc
);
743 return STATUS_INSUFFICIENT_RESOURCES
;
746 RtlZeroMemory(Buffer
, SizeNeeded
);
749 // Copy the string to destination
751 RtlCopyMemory(Buffer
, StringDesc
->bString
, SizeNeeded
- FIELD_OFFSET(USB_STRING_DESCRIPTOR
, bString
));
753 *TransferBuffer
= Buffer
;
755 ExFreePool(StringDesc
);
757 return STATUS_SUCCESS
;
762 IN PUSB_DEVICE_DESCRIPTOR DeviceDescriptor
,
763 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
)
765 if (DeviceDescriptor
->bNumConfigurations
!= 1)
768 // composite device must have only one configuration
770 DPRINT1("IsCompositeDevice bNumConfigurations %x\n", DeviceDescriptor
->bNumConfigurations
);
774 if (ConfigurationDescriptor
->bNumInterfaces
< 2)
777 // composite device must have multiple interfaces
779 DPRINT1("IsCompositeDevice bNumInterfaces %x\n", ConfigurationDescriptor
->bNumInterfaces
);
783 if (DeviceDescriptor
->bDeviceClass
== 0)
788 ASSERT(DeviceDescriptor
->bDeviceSubClass
== 0);
789 ASSERT(DeviceDescriptor
->bDeviceProtocol
== 0);
790 DPRINT1("IsCompositeDevice: TRUE\n");
794 if (DeviceDescriptor
->bDeviceClass
== 0xEF &&
795 DeviceDescriptor
->bDeviceSubClass
== 0x02 &&
796 DeviceDescriptor
->bDeviceProtocol
== 0x01)
799 // USB-IF association descriptor
801 DPRINT1("IsCompositeDevice: TRUE\n");
805 DPRINT1("DeviceDescriptor bDeviceClass %x bDeviceSubClass %x bDeviceProtocol %x\n", DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
, DeviceDescriptor
->bDeviceProtocol
);
808 // not a composite device
815 PDEVICE_OBJECT UsbChildDeviceObject
)
817 NTSTATUS Status
= STATUS_SUCCESS
;
821 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
;
822 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
823 PUSB_DEVICE_DESCRIPTOR DeviceDescriptor
;
824 PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
;
825 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
828 // get child device extension
830 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)UsbChildDeviceObject
->DeviceExtension
;
832 // get hub device extension
833 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) UsbChildExtension
->ParentDeviceObject
->DeviceExtension
;
836 // get device descriptor
838 DeviceDescriptor
= &UsbChildExtension
->DeviceDesc
;
841 // get configuration descriptor
843 ConfigurationDescriptor
= UsbChildExtension
->FullConfigDesc
;
846 // use first interface descriptor available
848 InterfaceDescriptor
= USBD_ParseConfigurationDescriptorEx(ConfigurationDescriptor
, ConfigurationDescriptor
, 0, -1, -1, -1, -1);
849 ASSERT(InterfaceDescriptor
);
852 // Construct the CompatibleIds
854 if (IsCompositeDevice(DeviceDescriptor
, ConfigurationDescriptor
))
859 ASSERT(DeviceDescriptor
->bNumConfigurations
== 1);
860 ASSERT(ConfigurationDescriptor
->bNumInterfaces
> 1);
861 Index
+= swprintf(&Buffer
[Index
],
862 L
"USB\\DevClass_%02x&SubClass_%02x&Prot_%02x",
863 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
, DeviceDescriptor
->bDeviceProtocol
) + 1;
864 Index
+= swprintf(&Buffer
[Index
],
865 L
"USB\\DevClass_%02x&SubClass_%02x",
866 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
) + 1;
867 Index
+= swprintf(&Buffer
[Index
],
868 L
"USB\\DevClass_%02x",
869 DeviceDescriptor
->bDeviceClass
) + 1;
870 Index
+= swprintf(&Buffer
[Index
],
871 L
"USB\\COMPOSITE") + 1;
876 // FIXME: support multiple configurations
878 ASSERT(DeviceDescriptor
->bNumConfigurations
== 1);
880 if (DeviceDescriptor
->bDeviceClass
== 0)
882 Index
+= swprintf(&Buffer
[Index
],
883 L
"USB\\Class_%02x&SubClass_%02x&Prot_%02x",
884 InterfaceDescriptor
->bInterfaceClass
, InterfaceDescriptor
->bInterfaceSubClass
, InterfaceDescriptor
->bInterfaceProtocol
) + 1;
885 Index
+= swprintf(&Buffer
[Index
],
886 L
"USB\\Class_%02x&SubClass_%02x",
887 InterfaceDescriptor
->bInterfaceClass
, InterfaceDescriptor
->bInterfaceSubClass
) + 1;
888 Index
+= swprintf(&Buffer
[Index
],
890 InterfaceDescriptor
->bInterfaceClass
) + 1;
894 Index
+= swprintf(&Buffer
[Index
],
895 L
"USB\\Class_%02x&SubClass_%02x&Prot_%02x",
896 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
, DeviceDescriptor
->bDeviceProtocol
) + 1;
897 Index
+= swprintf(&Buffer
[Index
],
898 L
"USB\\Class_%02x&SubClass_%02x",
899 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
) + 1;
900 Index
+= swprintf(&Buffer
[Index
],
902 DeviceDescriptor
->bDeviceClass
) + 1;
907 // now allocate the buffer
909 DeviceString
= ExAllocatePool(NonPagedPool
, (Index
+ 1) * sizeof(WCHAR
));
915 return STATUS_INSUFFICIENT_RESOURCES
;
921 RtlCopyMemory(DeviceString
, Buffer
, Index
* sizeof(WCHAR
));
922 DeviceString
[Index
] = UNICODE_NULL
;
923 UsbChildExtension
->usCompatibleIds
.Buffer
= DeviceString
;
924 UsbChildExtension
->usCompatibleIds
.Length
= Index
* sizeof(WCHAR
);
925 UsbChildExtension
->usCompatibleIds
.MaximumLength
= (Index
+ 1) * sizeof(WCHAR
);
926 DPRINT("usCompatibleIds %wZ\n", &UsbChildExtension
->usCompatibleIds
);
929 // Construct DeviceId string
931 Index
= swprintf(Buffer
, L
"USB\\Vid_%04x&Pid_%04x", UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
) + 1;
934 // now allocate the buffer
936 DeviceString
= ExAllocatePool(NonPagedPool
, Index
* sizeof(WCHAR
));
942 return STATUS_INSUFFICIENT_RESOURCES
;
948 RtlCopyMemory(DeviceString
, Buffer
, Index
* sizeof(WCHAR
));
949 UsbChildExtension
->usDeviceId
.Buffer
= DeviceString
;
950 UsbChildExtension
->usDeviceId
.Length
= (Index
-1) * sizeof(WCHAR
);
951 UsbChildExtension
->usDeviceId
.MaximumLength
= Index
* sizeof(WCHAR
);
952 DPRINT("usDeviceId %wZ\n", &UsbChildExtension
->usDeviceId
);
955 // Construct HardwareIds
958 Index
+= swprintf(&Buffer
[Index
],
959 L
"USB\\Vid_%04x&Pid_%04x&Rev_%04x",
960 UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
, UsbChildExtension
->DeviceDesc
.bcdDevice
) + 1;
961 Index
+= swprintf(&Buffer
[Index
],
962 L
"USB\\Vid_%04x&Pid_%04x",
963 UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
) + 1;
966 // now allocate the buffer
968 DeviceString
= ExAllocatePool(NonPagedPool
, (Index
+ 1) * sizeof(WCHAR
));
974 return STATUS_INSUFFICIENT_RESOURCES
;
980 RtlCopyMemory(DeviceString
, Buffer
, Index
* sizeof(WCHAR
));
981 DeviceString
[Index
] = UNICODE_NULL
;
982 UsbChildExtension
->usHardwareIds
.Buffer
= DeviceString
;
983 UsbChildExtension
->usHardwareIds
.Length
= (Index
+ 1) * sizeof(WCHAR
);
984 UsbChildExtension
->usHardwareIds
.MaximumLength
= (Index
+ 1) * sizeof(WCHAR
);
985 DPRINT("usHardWareIds %wZ\n", &UsbChildExtension
->usHardwareIds
);
988 // FIXME: Handle Lang ids
992 // Get the product string if obe provided
994 if (UsbChildExtension
->DeviceDesc
.iProduct
)
996 Status
= GetUsbStringDescriptor(UsbChildDeviceObject
,
997 UsbChildExtension
->DeviceDesc
.iProduct
,
999 (PVOID
*)&UsbChildExtension
->usTextDescription
.Buffer
,
1000 &UsbChildExtension
->usTextDescription
.Length
);
1001 if (!NT_SUCCESS(Status
))
1003 DPRINT1("USBHUB: GetUsbStringDescriptor failed with status %x\n", Status
);
1004 RtlInitUnicodeString(&UsbChildExtension
->usTextDescription
, L
"USB Device"); // FIXME NON-NLS
1008 UsbChildExtension
->usTextDescription
.MaximumLength
= UsbChildExtension
->usTextDescription
.Length
;
1009 DPRINT("Usb TextDescription %wZ\n", &UsbChildExtension
->usTextDescription
);
1014 // Get the Serial Number string if obe provided
1016 if (UsbChildExtension
->DeviceDesc
.iSerialNumber
)
1018 LPWSTR SerialBuffer
= NULL
;
1020 Status
= GetUsbStringDescriptor(UsbChildDeviceObject
,
1021 UsbChildExtension
->DeviceDesc
.iSerialNumber
,
1023 (PVOID
*)&SerialBuffer
,
1024 &UsbChildExtension
->usInstanceId
.Length
);
1025 if (!NT_SUCCESS(Status
))
1027 DPRINT1("USBHUB: GetUsbStringDescriptor failed with status %x\n", Status
);
1031 // construct instance id buffer
1032 Index
= swprintf(Buffer
, L
"%04d&%s", HubDeviceExtension
->InstanceCount
, SerialBuffer
) + 1;
1033 UsbChildExtension
->usInstanceId
.Buffer
= (LPWSTR
)ExAllocatePool(NonPagedPool
, Index
* sizeof(WCHAR
));
1034 if (UsbChildExtension
->usInstanceId
.Buffer
== NULL
)
1036 DPRINT1("Error: failed to allocate %lu bytes\n", Index
* sizeof(WCHAR
));
1037 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1044 RtlCopyMemory(UsbChildExtension
->usInstanceId
.Buffer
, Buffer
, Index
* sizeof(WCHAR
));
1045 UsbChildExtension
->usInstanceId
.Length
= UsbChildExtension
->usInstanceId
.MaximumLength
= Index
* sizeof(WCHAR
);
1046 ExFreePool(SerialBuffer
);
1048 DPRINT("Usb InstanceId %wZ InstanceCount %x\n", &UsbChildExtension
->usInstanceId
, HubDeviceExtension
->InstanceCount
);
1053 // the device did not provide a serial number, lets create a pseudo instance id
1055 Index
= swprintf(Buffer
, L
"%04d&%04d", HubDeviceExtension
->InstanceCount
, UsbChildExtension
->PortNumber
) + 1;
1056 UsbChildExtension
->usInstanceId
.Buffer
= (LPWSTR
)ExAllocatePool(NonPagedPool
, Index
* sizeof(WCHAR
));
1057 if (UsbChildExtension
->usInstanceId
.Buffer
== NULL
)
1059 DPRINT1("Error: failed to allocate %lu bytes\n", Index
* sizeof(WCHAR
));
1060 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1067 RtlCopyMemory(UsbChildExtension
->usInstanceId
.Buffer
, Buffer
, Index
* sizeof(WCHAR
));
1068 UsbChildExtension
->usInstanceId
.Length
= UsbChildExtension
->usInstanceId
.MaximumLength
= Index
* sizeof(WCHAR
);
1070 DPRINT("usDeviceId %wZ\n", &UsbChildExtension
->usInstanceId
);
1073 return STATUS_SUCCESS
;
1077 DestroyUsbChildDeviceObject(
1078 IN PDEVICE_OBJECT UsbHubDeviceObject
,
1081 PHUB_DEVICE_EXTENSION HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)UsbHubDeviceObject
->DeviceExtension
;
1082 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
= NULL
;
1083 PDEVICE_OBJECT ChildDeviceObject
= NULL
;
1086 DPRINT("Removing device on port %d (Child index: %d)\n", PortId
, Index
);
1088 for (Index
= 0; Index
< USB_MAXCHILDREN
; Index
++)
1090 if (HubDeviceExtension
->ChildDeviceObject
[Index
])
1092 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)HubDeviceExtension
->ChildDeviceObject
[Index
]->DeviceExtension
;
1094 /* Check if it matches the port ID */
1095 if (UsbChildExtension
->PortNumber
== PortId
)
1098 ChildDeviceObject
= HubDeviceExtension
->ChildDeviceObject
[Index
];
1104 /* Fail the request if the device doesn't exist */
1105 if (!ChildDeviceObject
)
1107 DPRINT1("Removal request for non-existant device!\n");
1108 return STATUS_UNSUCCESSFUL
;
1111 /* Remove the device from the table */
1112 HubDeviceExtension
->ChildDeviceObject
[Index
] = NULL
;
1114 /* Invalidate device relations for the root hub */
1115 IoInvalidateDeviceRelations(HubDeviceExtension
->RootHubPhysicalDeviceObject
, BusRelations
);
1117 /* The rest of the removal process takes place in IRP_MN_REMOVE_DEVICE handling for the PDO */
1118 return STATUS_SUCCESS
;
1122 CreateUsbChildDeviceObject(
1123 IN PDEVICE_OBJECT UsbHubDeviceObject
,
1125 OUT PDEVICE_OBJECT
*UsbChildDeviceObject
,
1126 IN ULONG PortStatus
)
1129 PDEVICE_OBJECT RootHubDeviceObject
, NewChildDeviceObject
;
1130 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1131 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
;
1132 PUSB_BUS_INTERFACE_HUB_V5 HubInterface
;
1133 ULONG ChildDeviceCount
, UsbDeviceNumber
= 0;
1134 WCHAR CharDeviceName
[64];
1135 UNICODE_STRING DeviceName
;
1136 ULONG ConfigDescSize
, DeviceDescSize
;
1137 PVOID HubInterfaceBusContext
;
1138 USB_CONFIGURATION_DESCRIPTOR ConfigDesc
;
1140 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) UsbHubDeviceObject
->DeviceExtension
;
1141 HubInterface
= &HubDeviceExtension
->HubInterface
;
1142 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
1143 HubInterfaceBusContext
= HubDeviceExtension
->UsbDInterface
.BusContext
;
1145 // Find an empty slot in the child device array
1147 for (ChildDeviceCount
= 0; ChildDeviceCount
< USB_MAXCHILDREN
; ChildDeviceCount
++)
1149 if (HubDeviceExtension
->ChildDeviceObject
[ChildDeviceCount
] == NULL
)
1151 DPRINT("Found unused entry at %d\n", ChildDeviceCount
);
1157 // Check if the limit has been reached for maximum usb devices
1159 if (ChildDeviceCount
== USB_MAXCHILDREN
)
1161 DPRINT1("USBHUB: Too many child devices!\n");
1162 return STATUS_UNSUCCESSFUL
;
1168 // Create a Device Name
1170 swprintf(CharDeviceName
, L
"\\Device\\USBPDO-%d", UsbDeviceNumber
);
1173 // Initialize UnicodeString
1175 RtlInitUnicodeString(&DeviceName
, CharDeviceName
);
1178 // Create a DeviceObject
1180 Status
= IoCreateDevice(UsbHubDeviceObject
->DriverObject
,
1181 sizeof(HUB_CHILDDEVICE_EXTENSION
),
1183 FILE_DEVICE_CONTROLLER
,
1184 FILE_AUTOGENERATED_DEVICE_NAME
,
1186 &NewChildDeviceObject
);
1189 // Check if the name is already in use
1191 if ((Status
== STATUS_OBJECT_NAME_EXISTS
) || (Status
== STATUS_OBJECT_NAME_COLLISION
))
1201 // Check for other errors
1203 if (!NT_SUCCESS(Status
))
1205 DPRINT1("USBHUB: IoCreateDevice failed with status %x\n", Status
);
1209 DPRINT("USBHUB: Created Device %x\n", NewChildDeviceObject
);
1213 NewChildDeviceObject
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
1216 // Assign the device extensions
1218 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)NewChildDeviceObject
->DeviceExtension
;
1219 RtlZeroMemory(UsbChildExtension
, sizeof(HUB_CHILDDEVICE_EXTENSION
));
1220 UsbChildExtension
->ParentDeviceObject
= UsbHubDeviceObject
;
1221 UsbChildExtension
->PortNumber
= PortId
;
1223 // copy device interface
1224 RtlCopyMemory(&UsbChildExtension
->DeviceInterface
, &HubDeviceExtension
->DeviceInterface
, sizeof(USB_BUS_INTERFACE_USBDI_V2
));
1228 // Create the UsbDeviceObject
1230 Status
= HubInterface
->CreateUsbDevice(HubInterfaceBusContext
,
1231 (PVOID
)&UsbChildExtension
->UsbDeviceHandle
,
1232 HubDeviceExtension
->RootHubHandle
,
1235 if (!NT_SUCCESS(Status
))
1237 DPRINT1("USBHUB: CreateUsbDevice failed with status %x\n", Status
);
1241 // copy device interface
1242 RtlCopyMemory(&UsbChildExtension
->DeviceInterface
, &HubDeviceExtension
->DeviceInterface
, sizeof(USB_BUS_INTERFACE_USBDI_V2
));
1244 // FIXME replace buscontext
1245 UsbChildExtension
->DeviceInterface
.BusContext
= UsbChildExtension
->UsbDeviceHandle
;
1248 // Initialize UsbDevice
1250 Status
= HubInterface
->InitializeUsbDevice(HubInterfaceBusContext
, UsbChildExtension
->UsbDeviceHandle
);
1251 if (!NT_SUCCESS(Status
))
1253 DPRINT1("USBHUB: InitializeUsbDevice failed with status %x\n", Status
);
1257 DPRINT("Usb Device Handle %x\n", UsbChildExtension
->UsbDeviceHandle
);
1259 ConfigDescSize
= sizeof(USB_CONFIGURATION_DESCRIPTOR
);
1260 DeviceDescSize
= sizeof(USB_DEVICE_DESCRIPTOR
);
1263 // Get the descriptors
1265 Status
= HubInterface
->GetUsbDescriptors(HubInterfaceBusContext
,
1266 UsbChildExtension
->UsbDeviceHandle
,
1267 (PUCHAR
)&UsbChildExtension
->DeviceDesc
,
1269 (PUCHAR
)&ConfigDesc
,
1271 if (!NT_SUCCESS(Status
))
1273 DPRINT1("USBHUB: GetUsbDescriptors failed with status %x\n", Status
);
1277 DumpDeviceDescriptor(&UsbChildExtension
->DeviceDesc
);
1278 DumpConfigurationDescriptor(&ConfigDesc
);
1281 // FIXME: Support more than one configuration and one interface?
1283 if (UsbChildExtension
->DeviceDesc
.bNumConfigurations
> 1)
1285 DPRINT1("Warning: Device has more than one configuration. Only one configuration (the first) is supported!\n");
1288 if (ConfigDesc
.bNumInterfaces
> 1)
1290 DPRINT1("Warning: Device has more that one interface. Only one interface (the first) is currently supported\n");
1293 ConfigDescSize
= ConfigDesc
.wTotalLength
;
1296 // Allocate memory for the first full descriptor, including interfaces and endpoints.
1298 UsbChildExtension
->FullConfigDesc
= ExAllocatePoolWithTag(PagedPool
, ConfigDescSize
, USB_HUB_TAG
);
1301 // Retrieve the full configuration descriptor
1303 Status
= GetUsbDeviceDescriptor(NewChildDeviceObject
,
1304 USB_CONFIGURATION_DESCRIPTOR_TYPE
,
1307 UsbChildExtension
->FullConfigDesc
,
1310 if (!NT_SUCCESS(Status
))
1312 DPRINT1("USBHUB: GetUsbDeviceDescriptor failed with status %x\n", Status
);
1316 //DumpFullConfigurationDescriptor(UsbChildExtension->FullConfigDesc);
1319 // Construct all the strings that will described the device to PNP
1321 Status
= CreateDeviceIds(NewChildDeviceObject
);
1322 if (!NT_SUCCESS(Status
))
1324 DPRINT1("Failed to create strings needed to describe device to PNP.\n");
1328 HubDeviceExtension
->ChildDeviceObject
[ChildDeviceCount
] = NewChildDeviceObject
;
1329 HubDeviceExtension
->InstanceCount
++;
1331 IoInvalidateDeviceRelations(RootHubDeviceObject
, BusRelations
);
1332 return STATUS_SUCCESS
;
1337 // Remove the usb device if it was created
1339 if (UsbChildExtension
->UsbDeviceHandle
)
1340 HubInterface
->RemoveUsbDevice(HubInterfaceBusContext
, UsbChildExtension
->UsbDeviceHandle
, 0);
1343 // Free full configuration descriptor if one was allocated
1345 if (UsbChildExtension
->FullConfigDesc
)
1346 ExFreePool(UsbChildExtension
->FullConfigDesc
);
1349 // Delete the device object
1351 IoDeleteDevice(NewChildDeviceObject
);
1356 USBHUB_FdoQueryBusRelations(
1357 IN PDEVICE_OBJECT DeviceObject
,
1358 OUT PDEVICE_RELATIONS
* pDeviceRelations
)
1360 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1361 PDEVICE_RELATIONS DeviceRelations
;
1366 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1369 // Count the number of children
1371 for (i
= 0; i
< USB_MAXCHILDREN
; i
++)
1374 if (HubDeviceExtension
->ChildDeviceObject
[i
] == NULL
)
1381 NeededSize
= sizeof(DEVICE_RELATIONS
);
1383 NeededSize
+= (Children
- 1) * sizeof(PDEVICE_OBJECT
);
1386 // Allocate DeviceRelations
1388 DeviceRelations
= (PDEVICE_RELATIONS
)ExAllocatePool(PagedPool
,
1391 if (!DeviceRelations
)
1392 return STATUS_INSUFFICIENT_RESOURCES
;
1393 DeviceRelations
->Count
= Children
;
1397 // Fill in return structure
1399 for (i
= 0; i
< USB_MAXCHILDREN
; i
++)
1401 if (HubDeviceExtension
->ChildDeviceObject
[i
])
1403 ObReferenceObject(HubDeviceExtension
->ChildDeviceObject
[i
]);
1404 HubDeviceExtension
->ChildDeviceObject
[i
]->Flags
&= ~DO_DEVICE_INITIALIZING
;
1405 DeviceRelations
->Objects
[Children
++] = HubDeviceExtension
->ChildDeviceObject
[i
];
1409 ASSERT(Children
== DeviceRelations
->Count
);
1410 *pDeviceRelations
= DeviceRelations
;
1412 return STATUS_SUCCESS
;
1417 RootHubInitCallbackFunction(
1420 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Context
;
1423 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1424 PORT_STATUS_CHANGE StatusChange
;
1426 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1428 DPRINT("RootHubInitCallbackFunction Sending the initial SCE Request %x\n", DeviceObject
);
1431 // Send the first SCE Request
1433 QueryStatusChangeEndpoint(DeviceObject
);
1435 for (PortId
= 1; PortId
<= HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
; PortId
++)
1440 Status
= GetPortStatusAndChange(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, &StatusChange
);
1441 if (NT_SUCCESS(Status
))
1444 // is there a device connected
1446 if (StatusChange
.Status
& USB_PORT_STATUS_CONNECT
)
1451 Status
= SetPortFeature(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, PORT_RESET
);
1452 if (!NT_SUCCESS(Status
))
1454 DPRINT1("Failed to reset on port %d\n", PortId
);
1459 // wait for the reset to be handled since we want to enumerate synchronously
1461 KeWaitForSingleObject(&HubDeviceExtension
->ResetComplete
,
1466 KeClearEvent(&HubDeviceExtension
->ResetComplete
);
1474 USBHUB_IsRootHubFDO(
1475 IN PDEVICE_OBJECT DeviceObject
)
1478 PDEVICE_OBJECT RootHubPhysicalDeviceObject
= NULL
;
1479 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1481 // get hub device extension
1482 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1484 // Get the Root Hub Pdo
1485 Status
= SubmitRequestToRootHub(HubDeviceExtension
->LowerDeviceObject
,
1486 IOCTL_INTERNAL_USB_GET_ROOTHUB_PDO
,
1487 &RootHubPhysicalDeviceObject
,
1490 // FIXME handle error
1491 ASSERT(NT_SUCCESS(Status
));
1493 // physical device object is only obtained for root hubs
1494 return (RootHubPhysicalDeviceObject
!= NULL
);
1499 USBHUB_FdoStartDevice(
1500 IN PDEVICE_OBJECT DeviceObject
,
1504 PUSB_INTERFACE_DESCRIPTOR Pid
;
1505 ULONG Result
= 0, PortId
;
1506 USBD_INTERFACE_LIST_ENTRY InterfaceList
[2] = {{NULL
, NULL
}, {NULL
, NULL
}};
1507 PURB ConfigUrb
= NULL
;
1509 PIO_STACK_LOCATION Stack
;
1510 NTSTATUS Status
= STATUS_SUCCESS
;
1511 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1512 PDEVICE_OBJECT RootHubDeviceObject
;
1513 PVOID HubInterfaceBusContext
, UsbDInterfaceBusContext
;
1514 PORT_STATUS_CHANGE StatusChange
;
1516 // get current stack location
1517 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1519 // get hub device extension
1520 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1522 DPRINT("IRP_MJ_PNP / IRP_MN_START_DEVICE\n");
1524 // Allocated size including the sizeof USBD_INTERFACE_LIST_ENTRY
1525 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
) + sizeof(USBD_INTERFACE_LIST_ENTRY
), USB_HUB_TAG
);
1529 return STATUS_INSUFFICIENT_RESOURCES
;
1533 RtlZeroMemory(Urb
, sizeof(URB
) + sizeof(USBD_INTERFACE_LIST_ENTRY
));
1535 // Get the Root Hub Pdo
1536 Status
= SubmitRequestToRootHub(HubDeviceExtension
->LowerDeviceObject
,
1537 IOCTL_INTERNAL_USB_GET_ROOTHUB_PDO
,
1538 &HubDeviceExtension
->RootHubPhysicalDeviceObject
,
1539 &HubDeviceExtension
->RootHubFunctionalDeviceObject
);
1540 if (!NT_SUCCESS(Status
))
1542 // failed to obtain hub pdo
1543 DPRINT1("IOCTL_INTERNAL_USB_GET_ROOTHUB_PDO failed with %x\n", Status
);
1549 ASSERT(HubDeviceExtension
->RootHubPhysicalDeviceObject
);
1550 ASSERT(HubDeviceExtension
->RootHubFunctionalDeviceObject
);
1553 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
1555 // Send the StartDevice to RootHub
1556 Status
= ForwardIrpAndWait(RootHubDeviceObject
, Irp
);
1558 if (!NT_SUCCESS(Status
))
1560 // failed to start pdo
1561 DPRINT1("Failed to start the RootHub PDO\n");
1566 // Get the current number of hubs
1567 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1568 IOCTL_INTERNAL_USB_GET_HUB_COUNT
,
1569 &HubDeviceExtension
->NumberOfHubs
, NULL
);
1570 if (!NT_SUCCESS(Status
))
1572 // failed to get number of hubs
1573 DPRINT1("IOCTL_INTERNAL_USB_GET_HUB_COUNT failed with %x\n", Status
);
1578 // Get the Hub Interface
1579 Status
= QueryInterface(RootHubDeviceObject
,
1580 USB_BUS_INTERFACE_HUB_GUID
,
1581 sizeof(USB_BUS_INTERFACE_HUB_V5
),
1582 USB_BUSIF_HUB_VERSION_5
,
1583 (PVOID
)&HubDeviceExtension
->HubInterface
);
1585 if (!NT_SUCCESS(Status
))
1587 // failed to get root hub interface
1588 DPRINT1("Failed to get HUB_GUID interface with status 0x%08lx\n", Status
);
1593 HubInterfaceBusContext
= HubDeviceExtension
->HubInterface
.BusContext
;
1595 // Get the USBDI Interface
1596 Status
= QueryInterface(RootHubDeviceObject
,
1597 USB_BUS_INTERFACE_USBDI_GUID
,
1598 sizeof(USB_BUS_INTERFACE_USBDI_V2
),
1599 USB_BUSIF_USBDI_VERSION_2
,
1600 (PVOID
)&HubDeviceExtension
->UsbDInterface
);
1602 if (!NT_SUCCESS(Status
))
1604 // failed to get usbdi interface
1605 DPRINT1("Failed to get USBDI_GUID interface with status 0x%08lx\n", Status
);
1610 UsbDInterfaceBusContext
= HubDeviceExtension
->UsbDInterface
.BusContext
;
1612 // Get Root Hub Device Handle
1613 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1614 IOCTL_INTERNAL_USB_GET_DEVICE_HANDLE
,
1615 &HubDeviceExtension
->RootHubHandle
,
1618 if (!NT_SUCCESS(Status
))
1621 DPRINT1("IOCTL_INTERNAL_USB_GET_DEVICE_HANDLE failed with status 0x%08lx\n", Status
);
1627 // Get Hub Device Information
1629 Status
= HubDeviceExtension
->HubInterface
.QueryDeviceInformation(HubInterfaceBusContext
,
1630 HubDeviceExtension
->RootHubHandle
,
1631 &HubDeviceExtension
->DeviceInformation
,
1632 sizeof(USB_DEVICE_INFORMATION_0
),
1635 DPRINT1("Status %x, Result 0x%08lx\n", Status
, Result
);
1636 DPRINT1("InformationLevel %x\n", HubDeviceExtension
->DeviceInformation
.InformationLevel
);
1637 DPRINT1("ActualLength %x\n", HubDeviceExtension
->DeviceInformation
.ActualLength
);
1638 DPRINT1("PortNumber %x\n", HubDeviceExtension
->DeviceInformation
.PortNumber
);
1639 DPRINT1("DeviceDescriptor %x\n", HubDeviceExtension
->DeviceInformation
.DeviceDescriptor
);
1640 DPRINT1("HubAddress %x\n", HubDeviceExtension
->DeviceInformation
.HubAddress
);
1641 DPRINT1("NumberofPipes %x\n", HubDeviceExtension
->DeviceInformation
.NumberOfOpenPipes
);
1643 // Get Root Hubs Device Descriptor
1644 UsbBuildGetDescriptorRequest(Urb
,
1645 sizeof(Urb
->UrbControlDescriptorRequest
),
1646 USB_DEVICE_DESCRIPTOR_TYPE
,
1649 &HubDeviceExtension
->HubDeviceDescriptor
,
1651 sizeof(USB_DEVICE_DESCRIPTOR
),
1654 // set device handle
1655 Urb
->UrbHeader
.UsbdDeviceHandle
= HubDeviceExtension
->RootHubHandle
;
1657 // get hub device descriptor
1658 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1659 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1663 if (!NT_SUCCESS(Status
))
1665 // failed to get device descriptor of hub
1666 DPRINT1("Failed to get HubDeviceDescriptor!\n");
1671 // build configuration request
1672 UsbBuildGetDescriptorRequest(Urb
,
1673 sizeof(Urb
->UrbControlDescriptorRequest
),
1674 USB_CONFIGURATION_DESCRIPTOR_TYPE
,
1677 &HubDeviceExtension
->HubConfigDescriptor
,
1679 sizeof(USB_CONFIGURATION_DESCRIPTOR
) + sizeof(USB_INTERFACE_DESCRIPTOR
) + sizeof(USB_ENDPOINT_DESCRIPTOR
),
1682 // set device handle
1683 Urb
->UrbHeader
.UsbdDeviceHandle
= HubDeviceExtension
->RootHubHandle
;
1685 // request configuration descriptor
1686 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1687 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1691 if (!NT_SUCCESS(Status
))
1693 // failed to get configuration descriptor
1694 DPRINT1("Failed to get RootHub Configuration with status %x\n", Status
);
1700 ASSERT(HubDeviceExtension
->HubConfigDescriptor
.wTotalLength
== sizeof(USB_CONFIGURATION_DESCRIPTOR
) + sizeof(USB_INTERFACE_DESCRIPTOR
) + sizeof(USB_ENDPOINT_DESCRIPTOR
));
1701 ASSERT(HubDeviceExtension
->HubConfigDescriptor
.bDescriptorType
== USB_CONFIGURATION_DESCRIPTOR_TYPE
);
1702 ASSERT(HubDeviceExtension
->HubConfigDescriptor
.bLength
== sizeof(USB_CONFIGURATION_DESCRIPTOR
));
1703 ASSERT(HubDeviceExtension
->HubConfigDescriptor
.bNumInterfaces
== 1);
1704 ASSERT(HubDeviceExtension
->HubInterfaceDescriptor
.bLength
== sizeof(USB_INTERFACE_DESCRIPTOR
));
1705 ASSERT(HubDeviceExtension
->HubInterfaceDescriptor
.bDescriptorType
== USB_INTERFACE_DESCRIPTOR_TYPE
);
1706 ASSERT(HubDeviceExtension
->HubInterfaceDescriptor
.bNumEndpoints
== 1);
1707 ASSERT(HubDeviceExtension
->HubEndPointDescriptor
.bDescriptorType
== USB_ENDPOINT_DESCRIPTOR_TYPE
);
1708 ASSERT(HubDeviceExtension
->HubEndPointDescriptor
.bLength
== sizeof(USB_ENDPOINT_DESCRIPTOR
));
1709 ASSERT(HubDeviceExtension
->HubEndPointDescriptor
.bmAttributes
== USB_ENDPOINT_TYPE_INTERRUPT
);
1710 ASSERT(HubDeviceExtension
->HubEndPointDescriptor
.bEndpointAddress
== 0x81); // interrupt in
1712 // get hub information
1713 Status
= HubDeviceExtension
->HubInterface
.GetExtendedHubInformation(HubInterfaceBusContext
,
1714 RootHubDeviceObject
,
1715 &HubDeviceExtension
->UsbExtHubInfo
,
1716 sizeof(USB_EXTHUB_INFORMATION_0
),
1718 if (!NT_SUCCESS(Status
))
1720 // failed to get hub information
1721 DPRINT1("Failed to extended hub information. Unable to determine the number of ports!\n");
1726 if (!HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
)
1728 // bogus port driver
1729 DPRINT1("Failed to retrieve the number of ports\n");
1731 return STATUS_UNSUCCESSFUL
;
1734 DPRINT1("HubDeviceExtension->UsbExtHubInfo.NumberOfPorts %x\n", HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
);
1736 // Build hub descriptor request
1737 UsbBuildVendorRequest(Urb
,
1738 URB_FUNCTION_CLASS_DEVICE
,
1739 sizeof(Urb
->UrbControlVendorClassRequest
),
1740 USBD_TRANSFER_DIRECTION_IN
| USBD_SHORT_TRANSFER_OK
,
1742 USB_REQUEST_GET_DESCRIPTOR
,
1743 USB_DEVICE_CLASS_RESERVED
,
1745 &HubDeviceExtension
->HubDescriptor
,
1747 sizeof(USB_HUB_DESCRIPTOR
),
1750 // set device handle
1751 Urb
->UrbHeader
.UsbdDeviceHandle
= HubDeviceExtension
->RootHubHandle
;
1754 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1755 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1759 if (!NT_SUCCESS(Status
))
1761 DPRINT1("Failed to get Hub Descriptor!\n");
1763 return STATUS_UNSUCCESSFUL
;
1767 ASSERT(HubDeviceExtension
->HubDescriptor
.bDescriptorLength
== sizeof(USB_HUB_DESCRIPTOR
));
1768 ASSERT(HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
== HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
);
1769 ASSERT(HubDeviceExtension
->HubDescriptor
.bDescriptorType
== 0x29);
1771 // build get status request
1773 UsbBuildGetStatusRequest(Urb
,
1774 URB_FUNCTION_GET_STATUS_FROM_DEVICE
,
1779 // set device handle
1780 Urb
->UrbHeader
.UsbdDeviceHandle
= HubDeviceExtension
->RootHubHandle
;
1783 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1784 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1787 if (!NT_SUCCESS(Status
))
1789 // failed to get hub status
1790 DPRINT1("Failed to get Hub Status!\n");
1792 return STATUS_UNSUCCESSFUL
;
1795 // Allocate memory for PortStatusChange to hold 2 USHORTs for each port on hub
1796 HubDeviceExtension
->PortStatusChange
= ExAllocatePoolWithTag(NonPagedPool
,
1797 sizeof(ULONG
) * HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
,
1800 // Get the first Configuration Descriptor
1801 Pid
= USBD_ParseConfigurationDescriptorEx(&HubDeviceExtension
->HubConfigDescriptor
,
1802 &HubDeviceExtension
->HubConfigDescriptor
,
1803 -1, -1, -1, -1, -1);
1806 // failed parse hub descriptor
1807 DPRINT1("Failed to parse configuration descriptor\n");
1809 return STATUS_UNSUCCESSFUL
;
1812 // create configuration request
1813 InterfaceList
[0].InterfaceDescriptor
= Pid
;
1814 ConfigUrb
= USBD_CreateConfigurationRequestEx(&HubDeviceExtension
->HubConfigDescriptor
,
1815 (PUSBD_INTERFACE_LIST_ENTRY
)&InterfaceList
);
1816 if (ConfigUrb
== NULL
)
1818 // failed to build urb
1819 DPRINT1("Failed to allocate urb\n");
1821 return STATUS_INSUFFICIENT_RESOURCES
;
1825 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1826 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1829 if (!NT_SUCCESS(Status
))
1831 // failed to select configuration
1832 DPRINT1("Failed to select configuration with %x\n", Status
);
1834 ExFreePool(ConfigUrb
);
1838 // store configuration & pipe handle
1839 HubDeviceExtension
->ConfigurationHandle
= ConfigUrb
->UrbSelectConfiguration
.ConfigurationHandle
;
1840 HubDeviceExtension
->PipeHandle
= ConfigUrb
->UrbSelectConfiguration
.Interface
.Pipes
[0].PipeHandle
;
1841 DPRINT("Configuration Handle %x\n", HubDeviceExtension
->ConfigurationHandle
);
1843 FDO_QueryInterface(DeviceObject
, &HubDeviceExtension
->DeviceInterface
);
1847 ExFreePool(ConfigUrb
);
1849 // check if function is available
1850 if (HubDeviceExtension
->UsbDInterface
.IsDeviceHighSpeed
)
1852 // is it high speed bus
1853 if (HubDeviceExtension
->UsbDInterface
.IsDeviceHighSpeed(HubInterfaceBusContext
))
1855 // initialize usb 2.0 hub
1856 Status
= HubDeviceExtension
->HubInterface
.Initialize20Hub(HubInterfaceBusContext
,
1857 HubDeviceExtension
->RootHubHandle
, 1);
1858 DPRINT("Status %x\n", Status
);
1860 // FIXME handle error
1861 ASSERT(Status
== STATUS_SUCCESS
);
1866 // Enable power on all ports
1867 DPRINT("Enabling PortPower on all ports!\n");
1868 for (PortId
= 1; PortId
<= HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
; PortId
++)
1870 Status
= SetPortFeature(RootHubDeviceObject
, PortId
, PORT_POWER
);
1871 if (!NT_SUCCESS(Status
))
1872 DPRINT1("Failed to power on port %d\n", PortId
);
1874 Status
= ClearPortFeature(RootHubDeviceObject
, PortId
, C_PORT_CONNECTION
);
1875 if (!NT_SUCCESS(Status
))
1876 DPRINT1("Failed to power on port %d\n", PortId
);
1879 // init root hub notification
1880 if (HubDeviceExtension
->HubInterface
.RootHubInitNotification
)
1882 Status
= HubDeviceExtension
->HubInterface
.RootHubInitNotification(HubInterfaceBusContext
,
1884 RootHubInitCallbackFunction
);
1885 if (!NT_SUCCESS(Status
))
1887 DPRINT1("Failed to set callback\n");
1894 // Send the first SCE Request
1895 QueryStatusChangeEndpoint(DeviceObject
);
1900 for (PortId
= 1; PortId
<= HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
; PortId
++)
1905 Status
= GetPortStatusAndChange(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, &StatusChange
);
1906 if (NT_SUCCESS(Status
))
1909 // is there a device connected
1911 if (StatusChange
.Status
& USB_PORT_STATUS_CONNECT
)
1916 Status
= SetPortFeature(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, PORT_RESET
);
1917 if (!NT_SUCCESS(Status
))
1919 DPRINT1("Failed to reset on port %d\n", PortId
);
1924 // wait for the reset to be handled since we want to enumerate synchronously
1926 KeWaitForSingleObject(&HubDeviceExtension
->ResetComplete
,
1931 KeClearEvent(&HubDeviceExtension
->ResetComplete
);
1946 USBHUB_FdoHandlePnp(
1947 IN PDEVICE_OBJECT DeviceObject
,
1950 PIO_STACK_LOCATION Stack
;
1951 NTSTATUS Status
= STATUS_SUCCESS
;
1952 ULONG_PTR Information
= 0;
1953 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1955 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1957 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1959 switch (Stack
->MinorFunction
)
1961 case IRP_MN_START_DEVICE
:
1963 if (USBHUB_IsRootHubFDO(DeviceObject
))
1965 // start root hub fdo
1966 Status
= USBHUB_FdoStartDevice(DeviceObject
, Irp
);
1970 Status
= USBHUB_ParentFDOStartDevice(DeviceObject
, Irp
);
1975 case IRP_MN_QUERY_DEVICE_RELATIONS
:
1977 switch (Stack
->Parameters
.QueryDeviceRelations
.Type
)
1981 PDEVICE_RELATIONS DeviceRelations
= NULL
;
1982 DPRINT("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / BusRelations\n");
1984 Status
= USBHUB_FdoQueryBusRelations(DeviceObject
, &DeviceRelations
);
1986 Information
= (ULONG_PTR
)DeviceRelations
;
1989 case RemovalRelations
:
1991 DPRINT("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / RemovalRelations\n");
1992 return ForwardIrpAndForget(DeviceObject
, Irp
);
1995 DPRINT("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / Unknown type 0x%lx\n",
1996 Stack
->Parameters
.QueryDeviceRelations
.Type
);
1997 return ForwardIrpAndForget(DeviceObject
, Irp
);
2001 case IRP_MN_QUERY_REMOVE_DEVICE
:
2002 case IRP_MN_QUERY_STOP_DEVICE
:
2004 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2005 return ForwardIrpAndForget(DeviceObject
, Irp
);
2007 case IRP_MN_REMOVE_DEVICE
:
2009 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2010 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2012 IoDetachDevice(HubDeviceExtension
->LowerDeviceObject
);
2013 IoDeleteDevice(DeviceObject
);
2015 return STATUS_SUCCESS
;
2017 case IRP_MN_QUERY_BUS_INFORMATION
:
2019 DPRINT("IRP_MN_QUERY_BUS_INFORMATION\n");
2022 case IRP_MN_QUERY_ID
:
2024 DPRINT("IRP_MN_QUERY_ID\n");
2027 case IRP_MN_QUERY_CAPABILITIES
:
2029 DPRINT("IRP_MN_QUERY_CAPABILITIES\n");
2034 DPRINT(" IRP_MJ_PNP / unknown minor function 0x%lx\n", Stack
->MinorFunction
);
2035 return ForwardIrpAndForget(DeviceObject
, Irp
);
2039 Irp
->IoStatus
.Information
= Information
;
2040 Irp
->IoStatus
.Status
= Status
;
2041 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2046 USBHUB_FdoHandleDeviceControl(
2047 IN PDEVICE_OBJECT DeviceObject
,
2050 DPRINT1("FdoHandleDeviceControl\n");
2052 return STATUS_NOT_IMPLEMENTED
;