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 PUSB_DEVICE_DESCRIPTOR DeviceDescriptor
;
823 PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
;
824 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
827 // get child device extension
829 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)UsbChildDeviceObject
->DeviceExtension
;
832 // get device descriptor
834 DeviceDescriptor
= &UsbChildExtension
->DeviceDesc
;
837 // get configuration descriptor
839 ConfigurationDescriptor
= UsbChildExtension
->FullConfigDesc
;
842 // use first interface descriptor available
844 InterfaceDescriptor
= USBD_ParseConfigurationDescriptorEx(ConfigurationDescriptor
, ConfigurationDescriptor
, 0, -1, -1, -1, -1);
845 ASSERT(InterfaceDescriptor
);
848 // Construct the CompatibleIds
850 if (IsCompositeDevice(DeviceDescriptor
, ConfigurationDescriptor
))
855 ASSERT(DeviceDescriptor
->bNumConfigurations
== 1);
856 ASSERT(ConfigurationDescriptor
->bNumInterfaces
> 1);
857 Index
+= swprintf(&Buffer
[Index
],
858 L
"USB\\DevClass_%02x&SubClass_%02x&Prot_%02x",
859 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
, DeviceDescriptor
->bDeviceProtocol
) + 1;
860 Index
+= swprintf(&Buffer
[Index
],
861 L
"USB\\DevClass_%02x&SubClass_%02x",
862 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
) + 1;
863 Index
+= swprintf(&Buffer
[Index
],
864 L
"USB\\DevClass_%02x",
865 DeviceDescriptor
->bDeviceClass
) + 1;
866 Index
+= swprintf(&Buffer
[Index
],
867 L
"USB\\COMPOSITE") + 1;
872 // FIXME: support multiple configurations
874 ASSERT(DeviceDescriptor
->bNumConfigurations
== 1);
876 if (DeviceDescriptor
->bDeviceClass
== 0)
878 Index
+= swprintf(&Buffer
[Index
],
879 L
"USB\\Class_%02x&SubClass_%02x&Prot_%02x",
880 InterfaceDescriptor
->bInterfaceClass
, InterfaceDescriptor
->bInterfaceSubClass
, InterfaceDescriptor
->bInterfaceProtocol
) + 1;
881 Index
+= swprintf(&Buffer
[Index
],
882 L
"USB\\Class_%02x&SubClass_%02x",
883 InterfaceDescriptor
->bInterfaceClass
, InterfaceDescriptor
->bInterfaceSubClass
) + 1;
884 Index
+= swprintf(&Buffer
[Index
],
886 InterfaceDescriptor
->bInterfaceClass
) + 1;
890 Index
+= swprintf(&Buffer
[Index
],
891 L
"USB\\Class_%02x&SubClass_%02x&Prot_%02x",
892 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
, DeviceDescriptor
->bDeviceProtocol
) + 1;
893 Index
+= swprintf(&Buffer
[Index
],
894 L
"USB\\Class_%02x&SubClass_%02x",
895 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
) + 1;
896 Index
+= swprintf(&Buffer
[Index
],
898 DeviceDescriptor
->bDeviceClass
) + 1;
903 // now allocate the buffer
905 DeviceString
= ExAllocatePool(NonPagedPool
, (Index
+ 1) * sizeof(WCHAR
));
911 return STATUS_INSUFFICIENT_RESOURCES
;
917 RtlCopyMemory(DeviceString
, Buffer
, Index
* sizeof(WCHAR
));
918 DeviceString
[Index
] = UNICODE_NULL
;
919 UsbChildExtension
->usCompatibleIds
.Buffer
= DeviceString
;
920 UsbChildExtension
->usCompatibleIds
.Length
= Index
* sizeof(WCHAR
);
921 UsbChildExtension
->usCompatibleIds
.MaximumLength
= (Index
+ 1) * sizeof(WCHAR
);
922 DPRINT("usCompatibleIds %wZ\n", &UsbChildExtension
->usCompatibleIds
);
925 // Construct DeviceId string
927 Index
= swprintf(Buffer
, L
"USB\\Vid_%04x&Pid_%04x", UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
) + 1;
930 // now allocate the buffer
932 DeviceString
= ExAllocatePool(NonPagedPool
, Index
* sizeof(WCHAR
));
938 return STATUS_INSUFFICIENT_RESOURCES
;
944 RtlCopyMemory(DeviceString
, Buffer
, Index
* sizeof(WCHAR
));
945 UsbChildExtension
->usDeviceId
.Buffer
= DeviceString
;
946 UsbChildExtension
->usDeviceId
.Length
= (Index
-1) * sizeof(WCHAR
);
947 UsbChildExtension
->usDeviceId
.MaximumLength
= Index
* sizeof(WCHAR
);
948 DPRINT("usDeviceId %wZ\n", &UsbChildExtension
->usDeviceId
);
951 // Construct HardwareIds
954 Index
+= swprintf(&Buffer
[Index
],
955 L
"USB\\Vid_%04x&Pid_%04x&Rev_%04x",
956 UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
, UsbChildExtension
->DeviceDesc
.bcdDevice
) + 1;
957 Index
+= swprintf(&Buffer
[Index
],
958 L
"USB\\Vid_%04x&Pid_%04x",
959 UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
) + 1;
962 // now allocate the buffer
964 DeviceString
= ExAllocatePool(NonPagedPool
, (Index
+ 1) * sizeof(WCHAR
));
970 return STATUS_INSUFFICIENT_RESOURCES
;
976 RtlCopyMemory(DeviceString
, Buffer
, Index
* sizeof(WCHAR
));
977 DeviceString
[Index
] = UNICODE_NULL
;
978 UsbChildExtension
->usHardwareIds
.Buffer
= DeviceString
;
979 UsbChildExtension
->usHardwareIds
.Length
= (Index
+ 1) * sizeof(WCHAR
);
980 UsbChildExtension
->usHardwareIds
.MaximumLength
= (Index
+ 1) * sizeof(WCHAR
);
981 DPRINT("usHardWareIds %wZ\n", &UsbChildExtension
->usHardwareIds
);
984 // FIXME: Handle Lang ids
988 // Get the product string if obe provided
990 if (UsbChildExtension
->DeviceDesc
.iProduct
)
992 Status
= GetUsbStringDescriptor(UsbChildDeviceObject
,
993 UsbChildExtension
->DeviceDesc
.iProduct
,
995 (PVOID
*)&UsbChildExtension
->usTextDescription
.Buffer
,
996 &UsbChildExtension
->usTextDescription
.Length
);
997 if (!NT_SUCCESS(Status
))
999 DPRINT1("USBHUB: GetUsbStringDescriptor failed with status %x\n", Status
);
1000 RtlInitUnicodeString(&UsbChildExtension
->usTextDescription
, L
"USB Device"); // FIXME NON-NLS
1004 UsbChildExtension
->usTextDescription
.MaximumLength
= UsbChildExtension
->usTextDescription
.Length
;
1005 DPRINT("Usb TextDescription %wZ\n", &UsbChildExtension
->usTextDescription
);
1010 // Get the Serial Number string if obe provided
1012 if (UsbChildExtension
->DeviceDesc
.iSerialNumber
)
1014 Status
= GetUsbStringDescriptor(UsbChildDeviceObject
,
1015 UsbChildExtension
->DeviceDesc
.iSerialNumber
,
1017 (PVOID
*)&UsbChildExtension
->usInstanceId
.Buffer
,
1018 &UsbChildExtension
->usInstanceId
.Length
);
1019 if (!NT_SUCCESS(Status
))
1021 DPRINT1("USBHUB: GetUsbStringDescriptor failed with status %x\n", Status
);
1025 UsbChildExtension
->usInstanceId
.MaximumLength
= UsbChildExtension
->usInstanceId
.Length
;
1026 DPRINT("Usb InstanceId %wZ\n", &UsbChildExtension
->usInstanceId
);
1031 // the device did not provide a serial number, lets create a pseudo instance id
1033 Index
= swprintf(Buffer
, L
"0&%04d", UsbChildExtension
->PortNumber
) + 1;
1034 UsbChildExtension
->usInstanceId
.Buffer
= (LPWSTR
)ExAllocatePool(NonPagedPool
, Index
* sizeof(WCHAR
));
1035 if (UsbChildExtension
->usInstanceId
.Buffer
== NULL
)
1037 DPRINT1("Error: failed to allocate %lu bytes\n", Index
* sizeof(WCHAR
));
1038 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1045 RtlCopyMemory(UsbChildExtension
->usInstanceId
.Buffer
, Buffer
, Index
* sizeof(WCHAR
));
1046 UsbChildExtension
->usInstanceId
.Length
= UsbChildExtension
->usInstanceId
.MaximumLength
= Index
* sizeof(WCHAR
);
1048 DPRINT("usDeviceId %wZ\n", &UsbChildExtension
->usInstanceId
);
1051 return STATUS_SUCCESS
;
1055 DestroyUsbChildDeviceObject(
1056 IN PDEVICE_OBJECT UsbHubDeviceObject
,
1059 PHUB_DEVICE_EXTENSION HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)UsbHubDeviceObject
->DeviceExtension
;
1060 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
= NULL
;
1061 PDEVICE_OBJECT ChildDeviceObject
= NULL
;
1064 DPRINT("Removing device on port %d (Child index: %d)\n", PortId
, Index
);
1066 for (Index
= 0; Index
< USB_MAXCHILDREN
; Index
++)
1068 if (HubDeviceExtension
->ChildDeviceObject
[Index
])
1070 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)HubDeviceExtension
->ChildDeviceObject
[Index
]->DeviceExtension
;
1072 /* Check if it matches the port ID */
1073 if (UsbChildExtension
->PortNumber
== PortId
)
1076 ChildDeviceObject
= HubDeviceExtension
->ChildDeviceObject
[Index
];
1082 /* Fail the request if the device doesn't exist */
1083 if (!ChildDeviceObject
)
1085 DPRINT1("Removal request for non-existant device!\n");
1086 return STATUS_UNSUCCESSFUL
;
1089 /* Remove the device from the table */
1090 HubDeviceExtension
->ChildDeviceObject
[Index
] = NULL
;
1092 /* Invalidate device relations for the root hub */
1093 IoInvalidateDeviceRelations(HubDeviceExtension
->RootHubPhysicalDeviceObject
, BusRelations
);
1095 /* The rest of the removal process takes place in IRP_MN_REMOVE_DEVICE handling for the PDO */
1096 return STATUS_SUCCESS
;
1100 CreateUsbChildDeviceObject(
1101 IN PDEVICE_OBJECT UsbHubDeviceObject
,
1103 OUT PDEVICE_OBJECT
*UsbChildDeviceObject
,
1104 IN ULONG PortStatus
)
1107 PDEVICE_OBJECT RootHubDeviceObject
, NewChildDeviceObject
;
1108 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1109 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
;
1110 PUSB_BUS_INTERFACE_HUB_V5 HubInterface
;
1111 ULONG ChildDeviceCount
, UsbDeviceNumber
= 0;
1112 WCHAR CharDeviceName
[64];
1113 UNICODE_STRING DeviceName
;
1114 ULONG ConfigDescSize
, DeviceDescSize
;
1115 PVOID HubInterfaceBusContext
;
1116 USB_CONFIGURATION_DESCRIPTOR ConfigDesc
;
1118 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) UsbHubDeviceObject
->DeviceExtension
;
1119 HubInterface
= &HubDeviceExtension
->HubInterface
;
1120 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
1121 HubInterfaceBusContext
= HubDeviceExtension
->UsbDInterface
.BusContext
;
1123 // Find an empty slot in the child device array
1125 for (ChildDeviceCount
= 0; ChildDeviceCount
< USB_MAXCHILDREN
; ChildDeviceCount
++)
1127 if (HubDeviceExtension
->ChildDeviceObject
[ChildDeviceCount
] == NULL
)
1129 DPRINT("Found unused entry at %d\n", ChildDeviceCount
);
1135 // Check if the limit has been reached for maximum usb devices
1137 if (ChildDeviceCount
== USB_MAXCHILDREN
)
1139 DPRINT1("USBHUB: Too many child devices!\n");
1140 return STATUS_UNSUCCESSFUL
;
1146 // Create a Device Name
1148 swprintf(CharDeviceName
, L
"\\Device\\USBPDO-%d", UsbDeviceNumber
);
1151 // Initialize UnicodeString
1153 RtlInitUnicodeString(&DeviceName
, CharDeviceName
);
1156 // Create a DeviceObject
1158 Status
= IoCreateDevice(UsbHubDeviceObject
->DriverObject
,
1159 sizeof(HUB_CHILDDEVICE_EXTENSION
),
1161 FILE_DEVICE_CONTROLLER
,
1162 FILE_AUTOGENERATED_DEVICE_NAME
,
1164 &NewChildDeviceObject
);
1167 // Check if the name is already in use
1169 if ((Status
== STATUS_OBJECT_NAME_EXISTS
) || (Status
== STATUS_OBJECT_NAME_COLLISION
))
1179 // Check for other errors
1181 if (!NT_SUCCESS(Status
))
1183 DPRINT1("USBHUB: IoCreateDevice failed with status %x\n", Status
);
1187 DPRINT("USBHUB: Created Device %x\n", NewChildDeviceObject
);
1191 NewChildDeviceObject
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
1194 // Assign the device extensions
1196 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)NewChildDeviceObject
->DeviceExtension
;
1197 RtlZeroMemory(UsbChildExtension
, sizeof(HUB_CHILDDEVICE_EXTENSION
));
1198 UsbChildExtension
->ParentDeviceObject
= UsbHubDeviceObject
;
1199 UsbChildExtension
->PortNumber
= PortId
;
1202 // Create the UsbDeviceObject
1204 Status
= HubInterface
->CreateUsbDevice(HubInterfaceBusContext
,
1205 (PVOID
)&UsbChildExtension
->UsbDeviceHandle
,
1206 HubDeviceExtension
->RootHubHandle
,
1209 if (!NT_SUCCESS(Status
))
1211 DPRINT1("USBHUB: CreateUsbDevice failed with status %x\n", Status
);
1216 // Initialize UsbDevice
1218 Status
= HubInterface
->InitializeUsbDevice(HubInterfaceBusContext
, UsbChildExtension
->UsbDeviceHandle
);
1219 if (!NT_SUCCESS(Status
))
1221 DPRINT1("USBHUB: InitializeUsbDevice failed with status %x\n", Status
);
1225 DPRINT("Usb Device Handle %x\n", UsbChildExtension
->UsbDeviceHandle
);
1227 ConfigDescSize
= sizeof(USB_CONFIGURATION_DESCRIPTOR
);
1228 DeviceDescSize
= sizeof(USB_DEVICE_DESCRIPTOR
);
1231 // Get the descriptors
1233 Status
= HubInterface
->GetUsbDescriptors(HubInterfaceBusContext
,
1234 UsbChildExtension
->UsbDeviceHandle
,
1235 (PUCHAR
)&UsbChildExtension
->DeviceDesc
,
1237 (PUCHAR
)&ConfigDesc
,
1239 if (!NT_SUCCESS(Status
))
1241 DPRINT1("USBHUB: GetUsbDescriptors failed with status %x\n", Status
);
1245 DumpDeviceDescriptor(&UsbChildExtension
->DeviceDesc
);
1246 DumpConfigurationDescriptor(&ConfigDesc
);
1249 // FIXME: Support more than one configuration and one interface?
1251 if (UsbChildExtension
->DeviceDesc
.bNumConfigurations
> 1)
1253 DPRINT1("Warning: Device has more than one configuration. Only one configuration (the first) is supported!\n");
1256 if (ConfigDesc
.bNumInterfaces
> 1)
1258 DPRINT1("Warning: Device has more that one interface. Only one interface (the first) is currently supported\n");
1261 ConfigDescSize
= ConfigDesc
.wTotalLength
;
1264 // Allocate memory for the first full descriptor, including interfaces and endpoints.
1266 UsbChildExtension
->FullConfigDesc
= ExAllocatePoolWithTag(PagedPool
, ConfigDescSize
, USB_HUB_TAG
);
1269 // Retrieve the full configuration descriptor
1271 Status
= GetUsbDeviceDescriptor(NewChildDeviceObject
,
1272 USB_CONFIGURATION_DESCRIPTOR_TYPE
,
1275 UsbChildExtension
->FullConfigDesc
,
1278 if (!NT_SUCCESS(Status
))
1280 DPRINT1("USBHUB: GetUsbDeviceDescriptor failed with status %x\n", Status
);
1284 //DumpFullConfigurationDescriptor(UsbChildExtension->FullConfigDesc);
1287 // Construct all the strings that will described the device to PNP
1289 Status
= CreateDeviceIds(NewChildDeviceObject
);
1290 if (!NT_SUCCESS(Status
))
1292 DPRINT1("Failed to create strings needed to describe device to PNP.\n");
1296 HubDeviceExtension
->ChildDeviceObject
[ChildDeviceCount
] = NewChildDeviceObject
;
1298 IoInvalidateDeviceRelations(RootHubDeviceObject
, BusRelations
);
1299 return STATUS_SUCCESS
;
1304 // Remove the usb device if it was created
1306 if (UsbChildExtension
->UsbDeviceHandle
)
1307 HubInterface
->RemoveUsbDevice(HubInterfaceBusContext
, UsbChildExtension
->UsbDeviceHandle
, 0);
1310 // Free full configuration descriptor if one was allocated
1312 if (UsbChildExtension
->FullConfigDesc
)
1313 ExFreePool(UsbChildExtension
->FullConfigDesc
);
1316 // Delete the device object
1318 IoDeleteDevice(NewChildDeviceObject
);
1323 USBHUB_FdoQueryBusRelations(
1324 IN PDEVICE_OBJECT DeviceObject
,
1325 OUT PDEVICE_RELATIONS
* pDeviceRelations
)
1327 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1328 PDEVICE_RELATIONS DeviceRelations
;
1333 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1336 // Count the number of children
1338 for (i
= 0; i
< USB_MAXCHILDREN
; i
++)
1341 if (HubDeviceExtension
->ChildDeviceObject
[i
] == NULL
)
1348 NeededSize
= sizeof(DEVICE_RELATIONS
);
1350 NeededSize
+= (Children
- 1) * sizeof(PDEVICE_OBJECT
);
1353 // Allocate DeviceRelations
1355 DeviceRelations
= (PDEVICE_RELATIONS
)ExAllocatePool(PagedPool
,
1358 if (!DeviceRelations
)
1359 return STATUS_INSUFFICIENT_RESOURCES
;
1360 DeviceRelations
->Count
= Children
;
1364 // Fill in return structure
1366 for (i
= 0; i
< USB_MAXCHILDREN
; i
++)
1368 if (HubDeviceExtension
->ChildDeviceObject
[i
])
1370 ObReferenceObject(HubDeviceExtension
->ChildDeviceObject
[i
]);
1371 HubDeviceExtension
->ChildDeviceObject
[i
]->Flags
&= ~DO_DEVICE_INITIALIZING
;
1372 DeviceRelations
->Objects
[Children
++] = HubDeviceExtension
->ChildDeviceObject
[i
];
1376 ASSERT(Children
== DeviceRelations
->Count
);
1377 *pDeviceRelations
= DeviceRelations
;
1379 return STATUS_SUCCESS
;
1384 RootHubInitCallbackFunction(
1387 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Context
;
1390 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1391 PORT_STATUS_CHANGE StatusChange
;
1393 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1395 DPRINT("RootHubInitCallbackFunction Sending the initial SCE Request %x\n", DeviceObject
);
1398 // Send the first SCE Request
1400 QueryStatusChangeEndpoint(DeviceObject
);
1402 for (PortId
= 1; PortId
<= HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
; PortId
++)
1407 Status
= GetPortStatusAndChange(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, &StatusChange
);
1408 if (NT_SUCCESS(Status
))
1411 // is there a device connected
1413 if (StatusChange
.Status
& USB_PORT_STATUS_CONNECT
)
1418 Status
= SetPortFeature(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, PORT_RESET
);
1419 if (!NT_SUCCESS(Status
))
1421 DPRINT1("Failed to reset on port %d\n", PortId
);
1426 // wait for the reset to be handled since we want to enumerate synchronously
1428 KeWaitForSingleObject(&HubDeviceExtension
->ResetComplete
,
1433 KeClearEvent(&HubDeviceExtension
->ResetComplete
);
1441 USBHUB_IsRootHubFDO(
1442 IN PDEVICE_OBJECT DeviceObject
)
1445 PDEVICE_OBJECT RootHubPhysicalDeviceObject
= NULL
;
1446 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1448 // get hub device extension
1449 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1451 // Get the Root Hub Pdo
1452 Status
= SubmitRequestToRootHub(HubDeviceExtension
->LowerDeviceObject
,
1453 IOCTL_INTERNAL_USB_GET_ROOTHUB_PDO
,
1454 &RootHubPhysicalDeviceObject
,
1457 // FIXME handle error
1458 ASSERT(NT_SUCCESS(Status
));
1460 // physical device object is only obtained for root hubs
1461 return (RootHubPhysicalDeviceObject
!= NULL
);
1466 USBHUB_FdoStartDevice(
1467 IN PDEVICE_OBJECT DeviceObject
,
1471 PUSB_INTERFACE_DESCRIPTOR Pid
;
1472 ULONG Result
= 0, PortId
;
1473 USBD_INTERFACE_LIST_ENTRY InterfaceList
[2] = {{NULL
, NULL
}, {NULL
, NULL
}};
1474 PURB ConfigUrb
= NULL
;
1476 PIO_STACK_LOCATION Stack
;
1477 NTSTATUS Status
= STATUS_SUCCESS
;
1478 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1479 PDEVICE_OBJECT RootHubDeviceObject
;
1480 PVOID HubInterfaceBusContext
, UsbDInterfaceBusContext
;
1481 PORT_STATUS_CHANGE StatusChange
;
1483 // get current stack location
1484 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1486 // get hub device extension
1487 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1489 DPRINT("IRP_MJ_PNP / IRP_MN_START_DEVICE\n");
1491 // Allocated size including the sizeof USBD_INTERFACE_LIST_ENTRY
1492 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
) + sizeof(USBD_INTERFACE_LIST_ENTRY
), USB_HUB_TAG
);
1496 return STATUS_INSUFFICIENT_RESOURCES
;
1500 RtlZeroMemory(Urb
, sizeof(URB
) + sizeof(USBD_INTERFACE_LIST_ENTRY
));
1502 // Get the Root Hub Pdo
1503 Status
= SubmitRequestToRootHub(HubDeviceExtension
->LowerDeviceObject
,
1504 IOCTL_INTERNAL_USB_GET_ROOTHUB_PDO
,
1505 &HubDeviceExtension
->RootHubPhysicalDeviceObject
,
1506 &HubDeviceExtension
->RootHubFunctionalDeviceObject
);
1507 if (!NT_SUCCESS(Status
))
1509 // failed to obtain hub pdo
1510 DPRINT1("IOCTL_INTERNAL_USB_GET_ROOTHUB_PDO failed with %x\n", Status
);
1516 ASSERT(HubDeviceExtension
->RootHubPhysicalDeviceObject
);
1517 ASSERT(HubDeviceExtension
->RootHubFunctionalDeviceObject
);
1520 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
1522 // Send the StartDevice to RootHub
1523 Status
= ForwardIrpAndWait(RootHubDeviceObject
, Irp
);
1525 if (!NT_SUCCESS(Status
))
1527 // failed to start pdo
1528 DPRINT1("Failed to start the RootHub PDO\n");
1533 // Get the current number of hubs
1534 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1535 IOCTL_INTERNAL_USB_GET_HUB_COUNT
,
1536 &HubDeviceExtension
->NumberOfHubs
, NULL
);
1537 if (!NT_SUCCESS(Status
))
1539 // failed to get number of hubs
1540 DPRINT1("IOCTL_INTERNAL_USB_GET_HUB_COUNT failed with %x\n", Status
);
1545 // Get the Hub Interface
1546 Status
= QueryInterface(RootHubDeviceObject
,
1547 USB_BUS_INTERFACE_HUB_GUID
,
1548 sizeof(USB_BUS_INTERFACE_HUB_V5
),
1549 USB_BUSIF_HUB_VERSION_5
,
1550 (PVOID
)&HubDeviceExtension
->HubInterface
);
1552 if (!NT_SUCCESS(Status
))
1554 // failed to get root hub interface
1555 DPRINT1("Failed to get HUB_GUID interface with status 0x%08lx\n", Status
);
1560 HubInterfaceBusContext
= HubDeviceExtension
->HubInterface
.BusContext
;
1562 // Get the USBDI Interface
1563 Status
= QueryInterface(RootHubDeviceObject
,
1564 USB_BUS_INTERFACE_USBDI_GUID
,
1565 sizeof(USB_BUS_INTERFACE_USBDI_V2
),
1566 USB_BUSIF_USBDI_VERSION_2
,
1567 (PVOID
)&HubDeviceExtension
->UsbDInterface
);
1569 if (!NT_SUCCESS(Status
))
1571 // failed to get usbdi interface
1572 DPRINT1("Failed to get USBDI_GUID interface with status 0x%08lx\n", Status
);
1577 UsbDInterfaceBusContext
= HubDeviceExtension
->UsbDInterface
.BusContext
;
1579 // Get Root Hub Device Handle
1580 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1581 IOCTL_INTERNAL_USB_GET_DEVICE_HANDLE
,
1582 &HubDeviceExtension
->RootHubHandle
,
1585 if (!NT_SUCCESS(Status
))
1588 DPRINT1("IOCTL_INTERNAL_USB_GET_DEVICE_HANDLE failed with status 0x%08lx\n", Status
);
1594 // Get Hub Device Information
1596 Status
= HubDeviceExtension
->HubInterface
.QueryDeviceInformation(HubInterfaceBusContext
,
1597 HubDeviceExtension
->RootHubHandle
,
1598 &HubDeviceExtension
->DeviceInformation
,
1599 sizeof(USB_DEVICE_INFORMATION_0
),
1602 DPRINT1("Status %x, Result 0x%08lx\n", Status
, Result
);
1603 DPRINT1("InformationLevel %x\n", HubDeviceExtension
->DeviceInformation
.InformationLevel
);
1604 DPRINT1("ActualLength %x\n", HubDeviceExtension
->DeviceInformation
.ActualLength
);
1605 DPRINT1("PortNumber %x\n", HubDeviceExtension
->DeviceInformation
.PortNumber
);
1606 DPRINT1("DeviceDescriptor %x\n", HubDeviceExtension
->DeviceInformation
.DeviceDescriptor
);
1607 DPRINT1("HubAddress %x\n", HubDeviceExtension
->DeviceInformation
.HubAddress
);
1608 DPRINT1("NumberofPipes %x\n", HubDeviceExtension
->DeviceInformation
.NumberOfOpenPipes
);
1610 // Get Root Hubs Device Descriptor
1611 UsbBuildGetDescriptorRequest(Urb
,
1612 sizeof(Urb
->UrbControlDescriptorRequest
),
1613 USB_DEVICE_DESCRIPTOR_TYPE
,
1616 &HubDeviceExtension
->HubDeviceDescriptor
,
1618 sizeof(USB_DEVICE_DESCRIPTOR
),
1621 // set device handle
1622 Urb
->UrbHeader
.UsbdDeviceHandle
= HubDeviceExtension
->RootHubHandle
;
1624 // get hub device descriptor
1625 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1626 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1630 if (!NT_SUCCESS(Status
))
1632 // failed to get device descriptor of hub
1633 DPRINT1("Failed to get HubDeviceDescriptor!\n");
1638 // build configuration request
1639 UsbBuildGetDescriptorRequest(Urb
,
1640 sizeof(Urb
->UrbControlDescriptorRequest
),
1641 USB_CONFIGURATION_DESCRIPTOR_TYPE
,
1644 &HubDeviceExtension
->HubConfigDescriptor
,
1646 sizeof(USB_CONFIGURATION_DESCRIPTOR
) + sizeof(USB_INTERFACE_DESCRIPTOR
) + sizeof(USB_ENDPOINT_DESCRIPTOR
),
1649 // set device handle
1650 Urb
->UrbHeader
.UsbdDeviceHandle
= HubDeviceExtension
->RootHubHandle
;
1652 // request configuration descriptor
1653 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1654 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1658 if (!NT_SUCCESS(Status
))
1660 // failed to get configuration descriptor
1661 DPRINT1("Failed to get RootHub Configuration with status %x\n", Status
);
1667 ASSERT(HubDeviceExtension
->HubConfigDescriptor
.wTotalLength
== sizeof(USB_CONFIGURATION_DESCRIPTOR
) + sizeof(USB_INTERFACE_DESCRIPTOR
) + sizeof(USB_ENDPOINT_DESCRIPTOR
));
1668 ASSERT(HubDeviceExtension
->HubConfigDescriptor
.bDescriptorType
== USB_CONFIGURATION_DESCRIPTOR_TYPE
);
1669 ASSERT(HubDeviceExtension
->HubConfigDescriptor
.bLength
== sizeof(USB_CONFIGURATION_DESCRIPTOR
));
1670 ASSERT(HubDeviceExtension
->HubConfigDescriptor
.bNumInterfaces
== 1);
1671 ASSERT(HubDeviceExtension
->HubInterfaceDescriptor
.bLength
== sizeof(USB_INTERFACE_DESCRIPTOR
));
1672 ASSERT(HubDeviceExtension
->HubInterfaceDescriptor
.bDescriptorType
== USB_INTERFACE_DESCRIPTOR_TYPE
);
1673 ASSERT(HubDeviceExtension
->HubInterfaceDescriptor
.bNumEndpoints
== 1);
1674 ASSERT(HubDeviceExtension
->HubEndPointDescriptor
.bDescriptorType
== USB_ENDPOINT_DESCRIPTOR_TYPE
);
1675 ASSERT(HubDeviceExtension
->HubEndPointDescriptor
.bLength
== sizeof(USB_ENDPOINT_DESCRIPTOR
));
1676 ASSERT(HubDeviceExtension
->HubEndPointDescriptor
.bmAttributes
== USB_ENDPOINT_TYPE_INTERRUPT
);
1677 ASSERT(HubDeviceExtension
->HubEndPointDescriptor
.bEndpointAddress
== 0x81); // interrupt in
1679 // get hub information
1680 Status
= HubDeviceExtension
->HubInterface
.GetExtendedHubInformation(HubInterfaceBusContext
,
1681 RootHubDeviceObject
,
1682 &HubDeviceExtension
->UsbExtHubInfo
,
1683 sizeof(USB_EXTHUB_INFORMATION_0
),
1685 if (!NT_SUCCESS(Status
))
1687 // failed to get hub information
1688 DPRINT1("Failed to extended hub information. Unable to determine the number of ports!\n");
1693 if (!HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
)
1695 // bogus port driver
1696 DPRINT1("Failed to retrieve the number of ports\n");
1698 return STATUS_UNSUCCESSFUL
;
1701 DPRINT1("HubDeviceExtension->UsbExtHubInfo.NumberOfPorts %x\n", HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
);
1703 // Build hub descriptor request
1704 UsbBuildVendorRequest(Urb
,
1705 URB_FUNCTION_CLASS_DEVICE
,
1706 sizeof(Urb
->UrbControlVendorClassRequest
),
1707 USBD_TRANSFER_DIRECTION_IN
| USBD_SHORT_TRANSFER_OK
,
1709 USB_REQUEST_GET_DESCRIPTOR
,
1710 USB_DEVICE_CLASS_RESERVED
,
1712 &HubDeviceExtension
->HubDescriptor
,
1714 sizeof(USB_HUB_DESCRIPTOR
),
1717 // set device handle
1718 Urb
->UrbHeader
.UsbdDeviceHandle
= HubDeviceExtension
->RootHubHandle
;
1721 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1722 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1726 if (!NT_SUCCESS(Status
))
1728 DPRINT1("Failed to get Hub Descriptor!\n");
1730 return STATUS_UNSUCCESSFUL
;
1734 ASSERT(HubDeviceExtension
->HubDescriptor
.bDescriptorLength
== sizeof(USB_HUB_DESCRIPTOR
));
1735 ASSERT(HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
== HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
);
1736 ASSERT(HubDeviceExtension
->HubDescriptor
.bDescriptorType
== 0x29);
1738 // build get status request
1740 UsbBuildGetStatusRequest(Urb
,
1741 URB_FUNCTION_GET_STATUS_FROM_DEVICE
,
1746 // set device handle
1747 Urb
->UrbHeader
.UsbdDeviceHandle
= HubDeviceExtension
->RootHubHandle
;
1750 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1751 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1754 if (!NT_SUCCESS(Status
))
1756 // failed to get hub status
1757 DPRINT1("Failed to get Hub Status!\n");
1759 return STATUS_UNSUCCESSFUL
;
1762 // Allocate memory for PortStatusChange to hold 2 USHORTs for each port on hub
1763 HubDeviceExtension
->PortStatusChange
= ExAllocatePoolWithTag(NonPagedPool
,
1764 sizeof(ULONG
) * HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
,
1767 // Get the first Configuration Descriptor
1768 Pid
= USBD_ParseConfigurationDescriptorEx(&HubDeviceExtension
->HubConfigDescriptor
,
1769 &HubDeviceExtension
->HubConfigDescriptor
,
1770 -1, -1, -1, -1, -1);
1773 // failed parse hub descriptor
1774 DPRINT1("Failed to parse configuration descriptor\n");
1776 return STATUS_UNSUCCESSFUL
;
1779 // create configuration request
1780 InterfaceList
[0].InterfaceDescriptor
= Pid
;
1781 ConfigUrb
= USBD_CreateConfigurationRequestEx(&HubDeviceExtension
->HubConfigDescriptor
,
1782 (PUSBD_INTERFACE_LIST_ENTRY
)&InterfaceList
);
1783 if (ConfigUrb
== NULL
)
1785 // failed to build urb
1786 DPRINT1("Failed to allocate urb\n");
1788 return STATUS_INSUFFICIENT_RESOURCES
;
1792 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1793 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1796 if (!NT_SUCCESS(Status
))
1798 // failed to select configuration
1799 DPRINT1("Failed to select configuration with %x\n", Status
);
1801 ExFreePool(ConfigUrb
);
1805 // store configuration & pipe handle
1806 HubDeviceExtension
->ConfigurationHandle
= ConfigUrb
->UrbSelectConfiguration
.ConfigurationHandle
;
1807 HubDeviceExtension
->PipeHandle
= ConfigUrb
->UrbSelectConfiguration
.Interface
.Pipes
[0].PipeHandle
;
1808 DPRINT("Configuration Handle %x\n", HubDeviceExtension
->ConfigurationHandle
);
1811 ExFreePool(ConfigUrb
);
1813 // check if function is available
1814 if (HubDeviceExtension
->UsbDInterface
.IsDeviceHighSpeed
)
1816 // is it high speed bus
1817 if (HubDeviceExtension
->UsbDInterface
.IsDeviceHighSpeed(HubInterfaceBusContext
))
1819 // initialize usb 2.0 hub
1820 Status
= HubDeviceExtension
->HubInterface
.Initialize20Hub(HubInterfaceBusContext
,
1821 HubDeviceExtension
->RootHubHandle
, 1);
1822 DPRINT("Status %x\n", Status
);
1824 // FIXME handle error
1825 ASSERT(Status
== STATUS_SUCCESS
);
1830 // Enable power on all ports
1831 DPRINT("Enabling PortPower on all ports!\n");
1832 for (PortId
= 1; PortId
<= HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
; PortId
++)
1834 Status
= SetPortFeature(RootHubDeviceObject
, PortId
, PORT_POWER
);
1835 if (!NT_SUCCESS(Status
))
1836 DPRINT1("Failed to power on port %d\n", PortId
);
1838 Status
= ClearPortFeature(RootHubDeviceObject
, PortId
, C_PORT_CONNECTION
);
1839 if (!NT_SUCCESS(Status
))
1840 DPRINT1("Failed to power on port %d\n", PortId
);
1843 // init root hub notification
1844 if (HubDeviceExtension
->HubInterface
.RootHubInitNotification
)
1846 Status
= HubDeviceExtension
->HubInterface
.RootHubInitNotification(HubInterfaceBusContext
,
1848 RootHubInitCallbackFunction
);
1849 if (!NT_SUCCESS(Status
))
1851 DPRINT1("Failed to set callback\n");
1858 // Send the first SCE Request
1859 QueryStatusChangeEndpoint(DeviceObject
);
1864 for (PortId
= 1; PortId
<= HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
; PortId
++)
1869 Status
= GetPortStatusAndChange(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, &StatusChange
);
1870 if (NT_SUCCESS(Status
))
1873 // is there a device connected
1875 if (StatusChange
.Status
& USB_PORT_STATUS_CONNECT
)
1880 Status
= SetPortFeature(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, PORT_RESET
);
1881 if (!NT_SUCCESS(Status
))
1883 DPRINT1("Failed to reset on port %d\n", PortId
);
1888 // wait for the reset to be handled since we want to enumerate synchronously
1890 KeWaitForSingleObject(&HubDeviceExtension
->ResetComplete
,
1895 KeClearEvent(&HubDeviceExtension
->ResetComplete
);
1910 USBHUB_FdoHandlePnp(
1911 IN PDEVICE_OBJECT DeviceObject
,
1914 PIO_STACK_LOCATION Stack
;
1915 NTSTATUS Status
= STATUS_SUCCESS
;
1916 ULONG_PTR Information
= 0;
1917 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1919 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1921 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1923 switch (Stack
->MinorFunction
)
1925 case IRP_MN_START_DEVICE
:
1927 if (USBHUB_IsRootHubFDO(DeviceObject
))
1929 // start root hub fdo
1930 Status
= USBHUB_FdoStartDevice(DeviceObject
, Irp
);
1934 Status
= USBHUB_ParentFDOStartDevice(DeviceObject
, Irp
);
1939 case IRP_MN_QUERY_DEVICE_RELATIONS
:
1941 switch (Stack
->Parameters
.QueryDeviceRelations
.Type
)
1945 PDEVICE_RELATIONS DeviceRelations
= NULL
;
1946 DPRINT("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / BusRelations\n");
1948 Status
= USBHUB_FdoQueryBusRelations(DeviceObject
, &DeviceRelations
);
1950 Information
= (ULONG_PTR
)DeviceRelations
;
1953 case RemovalRelations
:
1955 DPRINT("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / RemovalRelations\n");
1956 return ForwardIrpAndForget(DeviceObject
, Irp
);
1959 DPRINT("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / Unknown type 0x%lx\n",
1960 Stack
->Parameters
.QueryDeviceRelations
.Type
);
1961 return ForwardIrpAndForget(DeviceObject
, Irp
);
1965 case IRP_MN_QUERY_REMOVE_DEVICE
:
1966 case IRP_MN_QUERY_STOP_DEVICE
:
1968 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1969 return ForwardIrpAndForget(DeviceObject
, Irp
);
1971 case IRP_MN_REMOVE_DEVICE
:
1973 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1974 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1976 IoDetachDevice(HubDeviceExtension
->LowerDeviceObject
);
1977 IoDeleteDevice(DeviceObject
);
1979 return STATUS_SUCCESS
;
1981 case IRP_MN_QUERY_BUS_INFORMATION
:
1983 DPRINT("IRP_MN_QUERY_BUS_INFORMATION\n");
1986 case IRP_MN_QUERY_ID
:
1988 DPRINT("IRP_MN_QUERY_ID\n");
1991 case IRP_MN_QUERY_CAPABILITIES
:
1993 DPRINT("IRP_MN_QUERY_CAPABILITIES\n");
1998 DPRINT(" IRP_MJ_PNP / unknown minor function 0x%lx\n", Stack
->MinorFunction
);
1999 return ForwardIrpAndForget(DeviceObject
, Irp
);
2003 Irp
->IoStatus
.Information
= Information
;
2004 Irp
->IoStatus
.Status
= Status
;
2005 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2010 USBHUB_FdoHandleDeviceControl(
2011 IN PDEVICE_OBJECT DeviceObject
,
2014 DPRINT1("FdoHandleDeviceControl\n");
2016 return STATUS_NOT_IMPLEMENTED
;