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
);
25 DestroyUsbChildDeviceObject(
26 IN PDEVICE_OBJECT UsbHubDeviceObject
,
30 SubmitRequestToRootHub(
31 IN PDEVICE_OBJECT RootHubDeviceObject
,
32 IN ULONG IoControlCode
,
33 OUT PVOID OutParameter1
,
34 OUT PVOID OutParameter2
)
38 IO_STATUS_BLOCK IoStatus
;
40 PIO_STACK_LOCATION Stack
= NULL
;
42 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
45 // Build Control Request
47 Irp
= IoBuildDeviceIoControlRequest(IoControlCode
,
57 DPRINT("Usbhub: IoBuildDeviceIoControlRequest() failed\n");
58 return STATUS_INSUFFICIENT_RESOURCES
;
62 // Initialize the status block before sending the IRP
64 IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
65 IoStatus
.Information
= 0;
68 // Get Next Stack Location and Initialize it
70 Stack
= IoGetNextIrpStackLocation(Irp
);
71 Stack
->Parameters
.Others
.Argument1
= OutParameter1
;
72 Stack
->Parameters
.Others
.Argument2
= OutParameter2
;
77 Status
= IoCallDriver(RootHubDeviceObject
, Irp
);
80 // Its ok to block here as this function is called in an nonarbitrary thread
82 if (Status
== STATUS_PENDING
)
84 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
85 Status
= IoStatus
.Status
;
89 // The IO Manager will free the IRP
96 GetPortStatusAndChange(
97 IN PDEVICE_OBJECT RootHubDeviceObject
,
99 OUT PPORT_STATUS_CHANGE StatusChange
)
107 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
110 DPRINT1("Failed to allocate memory for URB!\n");
111 return STATUS_INSUFFICIENT_RESOURCES
;
117 RtlZeroMemory(Urb
, sizeof(URB
));
120 // Initialize URB for getting Port Status
122 UsbBuildVendorRequest(Urb
,
123 URB_FUNCTION_CLASS_OTHER
,
124 sizeof(Urb
->UrbControlVendorClassRequest
),
125 USBD_TRANSFER_DIRECTION_OUT
,
127 USB_REQUEST_GET_STATUS
,
132 sizeof(PORT_STATUS_CHANGE
),
136 // Query the Root Hub
138 Status
= SubmitRequestToRootHub(RootHubDeviceObject
, IOCTL_INTERNAL_USB_SUBMIT_URB
, Urb
, NULL
);
150 IN PDEVICE_OBJECT RootHubDeviceObject
,
160 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
163 DPRINT1("Failed to allocate memory for URB!\n");
164 return STATUS_INSUFFICIENT_RESOURCES
;
170 RtlZeroMemory(Urb
, sizeof(URB
));
173 // Initialize URB for Clearing Port Reset
175 UsbBuildVendorRequest(Urb
,
176 URB_FUNCTION_CLASS_OTHER
,
177 sizeof(Urb
->UrbControlVendorClassRequest
),
178 USBD_TRANSFER_DIRECTION_IN
,
180 USB_REQUEST_SET_FEATURE
,
188 // Query the Root Hub
190 Status
= SubmitRequestToRootHub(RootHubDeviceObject
, IOCTL_INTERNAL_USB_SUBMIT_URB
, Urb
, NULL
);
202 IN PDEVICE_OBJECT RootHubDeviceObject
,
212 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
215 DPRINT1("Failed to allocate memory for URB!\n");
216 return STATUS_INSUFFICIENT_RESOURCES
;
222 RtlZeroMemory(Urb
, sizeof(URB
));
225 // Initialize URB for Clearing Port Reset
227 UsbBuildVendorRequest(Urb
,
228 URB_FUNCTION_CLASS_OTHER
,
229 sizeof(Urb
->UrbControlVendorClassRequest
),
230 USBD_TRANSFER_DIRECTION_IN
,
232 USB_REQUEST_CLEAR_FEATURE
,
240 // Query the Root Hub
242 Status
= SubmitRequestToRootHub(RootHubDeviceObject
, IOCTL_INTERNAL_USB_SUBMIT_URB
, Urb
, NULL
);
253 DeviceStatusChangeThread(
257 PDEVICE_OBJECT DeviceObject
, RootHubDeviceObject
;
258 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
259 PWORK_ITEM_DATA WorkItemData
;
260 PORT_STATUS_CHANGE PortStatus
;
263 static LONG failsafe
= 0;
265 DPRINT1("Entered DeviceStatusChangeThread, Context %x\n", Context
);
267 WorkItemData
= (PWORK_ITEM_DATA
)Context
;
268 DeviceObject
= (PDEVICE_OBJECT
)WorkItemData
->Context
;
269 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
270 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
274 for (PortId
= 1; PortId
<= HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
; PortId
++)
279 Status
= GetPortStatusAndChange(RootHubDeviceObject
, PortId
, &PortStatus
);
280 if (!NT_SUCCESS(Status
))
282 DPRINT1("Failed to get port status for port %d, Status %x\n", PortId
, Status
);
283 // FIXME: Do we really want to halt further SCE requests?
287 DPRINT1("Port %d Status %x\n", PortId
, PortStatus
.Status
);
288 DPRINT1("Port %d Change %x\n", PortId
, PortStatus
.Change
);
292 // Check for new device connection
294 if (PortStatus
.Change
& USB_PORT_STATUS_CONNECT
)
297 // Clear Port Connect
299 Status
= ClearPortFeature(RootHubDeviceObject
, PortId
, C_PORT_CONNECTION
);
300 if (!NT_SUCCESS(Status
))
302 DPRINT1("Failed to clear connection change for port %d\n", PortId
);
306 // Is this a connect or disconnect?
308 if (!(PortStatus
.Status
& USB_PORT_STATUS_CONNECT
))
310 DPRINT1("Device disconnected from port %d\n", PortId
);
312 Status
= DestroyUsbChildDeviceObject(DeviceObject
, PortId
);
313 if (!NT_SUCCESS(Status
))
315 DPRINT1("Failed to delete child device object after disconnect\n");
320 DPRINT1("Device connected from port %d\n", PortId
);
322 // No SCE completion done for clearing C_PORT_CONNECT
327 Status
= SetPortFeature(RootHubDeviceObject
, PortId
, PORT_RESET
);
328 if (!NT_SUCCESS(Status
))
330 DPRINT1("Failed to reset port %d\n", PortId
);
334 else if (PortStatus
.Change
& USB_PORT_STATUS_ENABLE
)
339 Status
= ClearPortFeature(RootHubDeviceObject
, PortId
, C_PORT_ENABLE
);
340 if (!NT_SUCCESS(Status
))
342 DPRINT1("Failed to clear enable change on port %d\n", PortId
);
345 else if (PortStatus
.Change
& USB_PORT_STATUS_RESET
)
350 Status
= ClearPortFeature(RootHubDeviceObject
, PortId
, C_PORT_RESET
);
351 if (!NT_SUCCESS(Status
))
353 DPRINT1("Failed to clear reset change on port %d\n", PortId
);
359 Status
= GetPortStatusAndChange(RootHubDeviceObject
, PortId
, &PortStatus
);
360 if (!NT_SUCCESS(Status
))
362 DPRINT1("Failed to get port status for port %d, Status %x\n", PortId
, Status
);
363 // FIXME: Do we really want to halt further SCE requests?
367 DPRINT1("Port %d Status %x\n", PortId
, PortStatus
.Status
);
368 DPRINT1("Port %d Change %x\n", PortId
, PortStatus
.Change
);
371 // Check that reset was cleared
373 if(PortStatus
.Change
& USB_PORT_STATUS_RESET
)
375 DPRINT1("Port did not clear reset! Possible Hardware problem!\n");
379 // Check if the device is still connected
381 if (!(PortStatus
.Status
& USB_PORT_STATUS_CONNECT
))
383 DPRINT1("Device has been disconnected\n");
388 // Make sure its Connected and Enabled
390 if (!(PortStatus
.Status
& (USB_PORT_STATUS_CONNECT
| USB_PORT_STATUS_ENABLE
)))
392 DPRINT1("Usb Device is not connected and enabled!\n");
394 // Attempt another reset
396 Status
= SetPortFeature(RootHubDeviceObject
, PortId
, PORT_RESET
);
397 if (!NT_SUCCESS(Status
))
399 DPRINT1("Failed to reset port %d\n", PortId
);
405 // This is a new device
407 Status
= CreateUsbChildDeviceObject(DeviceObject
, PortId
, NULL
);
412 // FIXME: Still in testing
417 DPRINT1("SCE completed over 100 times but no action has been taken to clear the Change of any ports.\n");
419 // Return and dont send any more SCE Requests
424 ExFreePool(WorkItemData
);
427 // Send another SCE Request
429 DPRINT1("Sending another SCE!\n");
430 QueryStatusChangeEndpoint(DeviceObject
);
435 StatusChangeEndpointCompletion(
436 IN PDEVICE_OBJECT DeviceObject
,
440 PDEVICE_OBJECT RealDeviceObject
;
441 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
442 PWORK_ITEM_DATA WorkItemData
;
444 RealDeviceObject
= (PDEVICE_OBJECT
)Context
;
445 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)RealDeviceObject
->DeviceExtension
;
448 // NOTE: USBPORT frees this IRP
450 DPRINT1("Received Irp %x, HubDeviceExtension->PendingSCEIrp %x\n", Irp
, HubDeviceExtension
->PendingSCEIrp
);
454 // Create and initialize work item data
456 WorkItemData
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(WORK_ITEM_DATA
), USB_HUB_TAG
);
459 DPRINT1("Failed to allocate memory!n");
460 return STATUS_INSUFFICIENT_RESOURCES
;
462 WorkItemData
->Context
= RealDeviceObject
;
463 DPRINT1("Initialize work item\n");
464 ExInitializeWorkItem(&WorkItemData
->WorkItem
, DeviceStatusChangeThread
, (PVOID
)WorkItemData
);
467 // Queue the work item to handle initializing the device
469 ExQueueWorkItem(&WorkItemData
->WorkItem
, DelayedWorkQueue
);
472 // Return more processing required so the IO Manger doesn’t try to mess with IRP just freed
474 return STATUS_MORE_PROCESSING_REQUIRED
;
478 QueryStatusChangeEndpoint(
479 IN PDEVICE_OBJECT DeviceObject
)
482 PDEVICE_OBJECT RootHubDeviceObject
;
483 PIO_STACK_LOCATION Stack
;
484 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
487 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
488 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
493 PendingSCEUrb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
496 // Initialize URB for Status Change Endpoint request
498 UsbBuildInterruptOrBulkTransferRequest(PendingSCEUrb
,
499 sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER
),
500 HubDeviceExtension
->PipeHandle
,
501 HubDeviceExtension
->PortStatusChange
,
503 sizeof(USHORT
) * 2 * HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
,
504 USBD_TRANSFER_DIRECTION_IN
| USBD_SHORT_TRANSFER_OK
,
508 // Set the device handle to null for roothub
510 PendingSCEUrb
->UrbHeader
.UsbdDeviceHandle
= NULL
;//HubDeviceExtension->RootHubHandle;
516 HubDeviceExtension
->PendingSCEIrp
= ExAllocatePoolWithTag(NonPagedPool
,
517 IoSizeOfIrp(RootHubDeviceObject
->StackSize
),
520 HubDeviceExtension->PendingSCEIrp = IoAllocateIrp(RootHubDeviceObject->StackSize,
523 DPRINT1("Allocated IRP %x\n", HubDeviceExtension
->PendingSCEIrp
);
525 if (!HubDeviceExtension
->PendingSCEIrp
)
527 DPRINT1("USBHUB: Failed to allocate IRP for SCE request!\n");
528 return STATUS_INSUFFICIENT_RESOURCES
;
532 // Initialize the IRP
534 IoInitializeIrp(HubDeviceExtension
->PendingSCEIrp
,
535 IoSizeOfIrp(RootHubDeviceObject
->StackSize
),
536 RootHubDeviceObject
->StackSize
);
538 HubDeviceExtension
->PendingSCEIrp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
539 HubDeviceExtension
->PendingSCEIrp
->IoStatus
.Information
= 0;
540 HubDeviceExtension
->PendingSCEIrp
->Flags
= 0;
541 HubDeviceExtension
->PendingSCEIrp
->UserBuffer
= NULL
;
544 // Get the Next Stack Location and Initialize it
546 Stack
= IoGetNextIrpStackLocation(HubDeviceExtension
->PendingSCEIrp
);
547 Stack
->DeviceObject
= DeviceObject
;
548 Stack
->Parameters
.Others
.Argument1
= PendingSCEUrb
;
549 Stack
->Parameters
.Others
.Argument2
= NULL
;
550 Stack
->MajorFunction
= IRP_MJ_INTERNAL_DEVICE_CONTROL
;
551 Stack
->Parameters
.DeviceIoControl
.IoControlCode
= IOCTL_INTERNAL_USB_SUBMIT_URB
;
554 // Set the completion routine for when device is connected to root hub
556 IoSetCompletionRoutine(HubDeviceExtension
->PendingSCEIrp
,
557 StatusChangeEndpointCompletion
,
566 DPRINT1("DeviceObject is %x\n", DeviceObject
);
567 DPRINT1("Iocalldriver %x with irp %x\n", RootHubDeviceObject
, HubDeviceExtension
->PendingSCEIrp
);
568 Status
= IoCallDriver(RootHubDeviceObject
, HubDeviceExtension
->PendingSCEIrp
);
570 return STATUS_PENDING
;
575 IN PDEVICE_OBJECT DeviceObject
,
576 IN CONST GUID InterfaceType
,
583 IO_STATUS_BLOCK IoStatus
;
585 PIO_STACK_LOCATION Stack
= NULL
;
588 // Initialize the Event used to wait for Irp completion
590 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
593 // Build Control Request
595 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_PNP
,
604 // Get Next Stack Location and Initialize it.
606 Stack
= IoGetNextIrpStackLocation(Irp
);
607 Stack
->MinorFunction
= IRP_MN_QUERY_INTERFACE
;
608 Stack
->Parameters
.QueryInterface
.InterfaceType
= &InterfaceType
;//USB_BUS_INTERFACE_HUB_GUID;
609 Stack
->Parameters
.QueryInterface
.Size
= Size
;
610 Stack
->Parameters
.QueryInterface
.Version
= Version
;
611 Stack
->Parameters
.QueryInterface
.Interface
= Interface
;
612 Stack
->Parameters
.QueryInterface
.InterfaceSpecificData
= NULL
;
614 Status
= IoCallDriver(DeviceObject
, Irp
);
616 if (Status
== STATUS_PENDING
)
618 DPRINT("Operation pending\n");
619 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
620 Status
= IoStatus
.Status
;
627 GetUsbDeviceDescriptor(
628 IN PDEVICE_OBJECT ChildDeviceObject
,
629 IN UCHAR DescriptorType
,
632 OUT PVOID TransferBuffer
,
633 IN ULONG TransferBufferLength
)
636 PDEVICE_OBJECT RootHubDeviceObject
;
638 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
639 PHUB_CHILDDEVICE_EXTENSION ChildDeviceExtension
;
642 // Get the Hubs Device Extension
644 ChildDeviceExtension
= (PHUB_CHILDDEVICE_EXTENSION
)ChildDeviceObject
->DeviceExtension
;
645 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) ChildDeviceExtension
->ParentDeviceObject
->DeviceExtension
;
646 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
651 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
654 DPRINT1("Failed to allocate memory for URB!\n");
655 return STATUS_INSUFFICIENT_RESOURCES
;
661 RtlZeroMemory(Urb
, sizeof(URB
));
664 // Initialize URB for getting device descriptor
666 UsbBuildGetDescriptorRequest(Urb
,
667 sizeof(Urb
->UrbControlDescriptorRequest
),
673 TransferBufferLength
,
677 // Set the device handle
679 Urb
->UrbHeader
.UsbdDeviceHandle
= (PVOID
)ChildDeviceExtension
->UsbDeviceHandle
;
682 // Query the Root Hub
684 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
685 IOCTL_INTERNAL_USB_SUBMIT_URB
,
693 GetUsbStringDescriptor(
694 IN PDEVICE_OBJECT ChildDeviceObject
,
697 OUT PVOID
*TransferBuffer
,
701 PUSB_STRING_DESCRIPTOR StringDesc
= NULL
;
705 StringDesc
= ExAllocatePoolWithTag(NonPagedPool
,
706 sizeof(USB_STRING_DESCRIPTOR
),
710 DPRINT1("Failed to allocate buffer for string!\n");
711 return STATUS_INSUFFICIENT_RESOURCES
;
715 // Get the index string descriptor length
716 // FIXME: Implement LangIds
718 Status
= GetUsbDeviceDescriptor(ChildDeviceObject
,
719 USB_STRING_DESCRIPTOR_TYPE
,
723 sizeof(USB_STRING_DESCRIPTOR
));
724 if (!NT_SUCCESS(Status
))
726 DPRINT1("GetUsbDeviceDescriptor failed with status %x\n", Status
);
727 ExFreePool(StringDesc
);
730 DPRINT1("StringDesc->bLength %d\n", StringDesc
->bLength
);
733 // Did we get something more than the length of the first two fields of structure?
735 if (StringDesc
->bLength
== 2)
737 DPRINT1("USB Device Error!\n");
738 ExFreePool(StringDesc
);
739 return STATUS_DEVICE_DATA_ERROR
;
741 SizeNeeded
= StringDesc
->bLength
+ sizeof(WCHAR
);
746 ExFreePool(StringDesc
);
749 // Recreate with appropriate size
751 StringDesc
= ExAllocatePoolWithTag(NonPagedPool
,
756 DPRINT1("Failed to allocate buffer for string!\n");
757 return STATUS_INSUFFICIENT_RESOURCES
;
760 RtlZeroMemory(StringDesc
, SizeNeeded
);
765 Status
= GetUsbDeviceDescriptor(ChildDeviceObject
,
766 USB_STRING_DESCRIPTOR_TYPE
,
771 if (!NT_SUCCESS(Status
))
773 DPRINT1("GetUsbDeviceDescriptor failed with status %x\n", Status
);
774 ExFreePool(StringDesc
);
779 // Allocate Buffer to return
781 Buffer
= ExAllocatePoolWithTag(NonPagedPool
,
786 DPRINT1("Failed to allocate buffer for string!\n");
787 ExFreePool(StringDesc
);
788 return STATUS_INSUFFICIENT_RESOURCES
;
790 DPRINT1("Buffer %p\n", Buffer
);
791 RtlZeroMemory(Buffer
, SizeNeeded
);
793 DPRINT1("Buffer %p\n", Buffer
);
794 DPRINT1("SizeNeeded %lu\n", SizeNeeded
);
795 DPRINT1("Offset %lu\n", FIELD_OFFSET(USB_STRING_DESCRIPTOR
, bLength
));
796 DPRINT1("Length %lu\n", SizeNeeded
- FIELD_OFFSET(USB_STRING_DESCRIPTOR
, bLength
));
799 // Copy the string to destination
801 RtlCopyMemory(Buffer
, StringDesc
->bString
, SizeNeeded
- FIELD_OFFSET(USB_STRING_DESCRIPTOR
, bString
));
803 *TransferBuffer
= Buffer
;
805 ExFreePool(StringDesc
);
807 return STATUS_SUCCESS
;
812 IN PUSB_DEVICE_DESCRIPTOR DeviceDescriptor
,
813 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
)
815 if (DeviceDescriptor
->bNumConfigurations
!= 1)
818 // composite device must have only one configuration
823 if (ConfigurationDescriptor
->bNumInterfaces
< 2)
826 // composite device must have multiple interfaces
831 if (DeviceDescriptor
->bDeviceClass
== 0)
836 ASSERT(DeviceDescriptor
->bDeviceSubClass
== 0);
837 ASSERT(DeviceDescriptor
->bDeviceProtocol
== 0);
841 if (DeviceDescriptor
->bDeviceClass
== 0xEF &&
842 DeviceDescriptor
->bDeviceSubClass
== 0x02 &&
843 DeviceDescriptor
->bDeviceProtocol
== 0x01)
846 // USB-IF association descriptor
852 // not a composite device
859 PDEVICE_OBJECT UsbChildDeviceObject
)
861 NTSTATUS Status
= STATUS_SUCCESS
;
865 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
;
866 PUSB_DEVICE_DESCRIPTOR DeviceDescriptor
;
867 PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
;
868 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
871 // get child device extension
873 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)UsbChildDeviceObject
->DeviceExtension
;
876 // get device descriptor
878 DeviceDescriptor
= &UsbChildExtension
->DeviceDesc
;
881 // get configuration descriptor
883 ConfigurationDescriptor
= UsbChildExtension
->FullConfigDesc
;
886 // use first interface descriptor available
888 InterfaceDescriptor
= USBD_ParseConfigurationDescriptorEx(ConfigurationDescriptor
, ConfigurationDescriptor
, 0, -1, -1, -1, -1);
889 ASSERT(InterfaceDescriptor
);
892 // Construct the CompatibleIds
894 if (IsCompositeDevice(DeviceDescriptor
, ConfigurationDescriptor
))
899 ASSERT(DeviceDescriptor
->bNumConfigurations
== 1);
900 ASSERT(ConfigurationDescriptor
->bNumInterfaces
> 1);
901 Index
+= swprintf(&Buffer
[Index
],
902 L
"USB\\DevClass_%02x&SubClass_%02x&Prot_%02x",
903 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
, DeviceDescriptor
->bDeviceProtocol
) + 1;
904 Index
+= swprintf(&Buffer
[Index
],
905 L
"USB\\DevClass_%02x&SubClass_%02x",
906 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
) + 1;
907 Index
+= swprintf(&Buffer
[Index
],
908 L
"USB\\DevClass_%02x",
909 DeviceDescriptor
->bDeviceClass
) + 1;
910 Index
+= swprintf(&Buffer
[Index
],
911 L
"USB\\COMPOSITE") + 1;
916 // sanity checks for simple usb device
918 ASSERT(ConfigurationDescriptor
->bNumInterfaces
== 1);
921 // FIXME: support multiple configurations
923 ASSERT(DeviceDescriptor
->bNumConfigurations
== 1);
925 if (DeviceDescriptor
->bDeviceClass
== 0)
927 Index
+= swprintf(&Buffer
[Index
],
928 L
"USB\\Class_%02x&SubClass_%02x&Prot_%02x",
929 InterfaceDescriptor
->bInterfaceClass
, InterfaceDescriptor
->bInterfaceSubClass
, InterfaceDescriptor
->bInterfaceProtocol
) + 1;
930 Index
+= swprintf(&Buffer
[Index
],
931 L
"USB\\Class_%02x&SubClass_%02x",
932 InterfaceDescriptor
->bInterfaceClass
, InterfaceDescriptor
->bInterfaceSubClass
) + 1;
933 Index
+= swprintf(&Buffer
[Index
],
935 InterfaceDescriptor
->bInterfaceClass
) + 1;
939 Index
+= swprintf(&Buffer
[Index
],
940 L
"USB\\Class_%02x&SubClass_%02x&Prot_%02x",
941 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
, DeviceDescriptor
->bDeviceProtocol
) + 1;
942 Index
+= swprintf(&Buffer
[Index
],
943 L
"USB\\Class_%02x&SubClass_%02x",
944 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
) + 1;
945 Index
+= swprintf(&Buffer
[Index
],
947 DeviceDescriptor
->bDeviceClass
) + 1;
952 // now allocate the buffer
954 DeviceString
= ExAllocatePool(NonPagedPool
, (Index
+ 1) * sizeof(WCHAR
));
960 return STATUS_INSUFFICIENT_RESOURCES
;
966 RtlCopyMemory(DeviceString
, Buffer
, Index
* sizeof(WCHAR
));
967 DeviceString
[Index
] = UNICODE_NULL
;
968 UsbChildExtension
->usCompatibleIds
.Buffer
= DeviceString
;
969 UsbChildExtension
->usCompatibleIds
.Length
= Index
* sizeof(WCHAR
);
970 UsbChildExtension
->usCompatibleIds
.MaximumLength
= (Index
+ 1) * sizeof(WCHAR
);
971 DPRINT1("usCompatibleIds %wZ\n", &UsbChildExtension
->usCompatibleIds
);
974 // Construct DeviceId string
976 Index
= swprintf(Buffer
, L
"USB\\Vid_%04x&Pid_%04x", UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
) + 1;
979 // now allocate the buffer
981 DeviceString
= ExAllocatePool(NonPagedPool
, Index
* sizeof(WCHAR
));
987 return STATUS_INSUFFICIENT_RESOURCES
;
993 RtlCopyMemory(DeviceString
, Buffer
, Index
* sizeof(WCHAR
));
994 UsbChildExtension
->usDeviceId
.Buffer
= DeviceString
;
995 UsbChildExtension
->usDeviceId
.Length
= (Index
-1) * sizeof(WCHAR
);
996 UsbChildExtension
->usDeviceId
.MaximumLength
= Index
* sizeof(WCHAR
);
997 DPRINT1("usDeviceId %wZ\n", &UsbChildExtension
->usDeviceId
);
1000 // Construct HardwareIds
1003 Index
+= swprintf(&Buffer
[Index
],
1004 L
"USB\\Vid_%04x&Pid_%04x&Rev_%04x",
1005 UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
, UsbChildExtension
->DeviceDesc
.bcdDevice
) + 1;
1006 Index
+= swprintf(&Buffer
[Index
],
1007 L
"USB\\Vid_%04x&Pid_%04x",
1008 UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
) + 1;
1011 // now allocate the buffer
1013 DeviceString
= ExAllocatePool(NonPagedPool
, (Index
+ 1) * sizeof(WCHAR
));
1019 return STATUS_INSUFFICIENT_RESOURCES
;
1025 RtlCopyMemory(DeviceString
, Buffer
, Index
* sizeof(WCHAR
));
1026 DeviceString
[Index
] = UNICODE_NULL
;
1027 UsbChildExtension
->usHardwareIds
.Buffer
= DeviceString
;
1028 UsbChildExtension
->usHardwareIds
.Length
= (Index
+ 1) * sizeof(WCHAR
);
1029 UsbChildExtension
->usHardwareIds
.MaximumLength
= (Index
+ 1) * sizeof(WCHAR
);
1030 DPRINT1("usHardWareIds %wZ\n", &UsbChildExtension
->usHardwareIds
);
1033 // FIXME: Handle Lang ids
1037 // Get the product string if obe provided
1039 if (UsbChildExtension
->DeviceDesc
.iProduct
)
1041 Status
= GetUsbStringDescriptor(UsbChildDeviceObject
,
1042 UsbChildExtension
->DeviceDesc
.iProduct
,
1044 (PVOID
*)&UsbChildExtension
->usTextDescription
.Buffer
,
1045 &UsbChildExtension
->usTextDescription
.Length
);
1046 if (!NT_SUCCESS(Status
))
1048 DPRINT1("USBHUB: GetUsbStringDescriptor failed with status %x\n", Status
);
1052 UsbChildExtension
->usTextDescription
.MaximumLength
= UsbChildExtension
->usTextDescription
.Length
;
1053 DPRINT1("Usb TextDescription %wZ\n", &UsbChildExtension
->usTextDescription
);
1057 // Get the Serial Number string if obe provided
1059 if (UsbChildExtension
->DeviceDesc
.iSerialNumber
)
1061 Status
= GetUsbStringDescriptor(UsbChildDeviceObject
,
1062 UsbChildExtension
->DeviceDesc
.iSerialNumber
,
1064 (PVOID
*)&UsbChildExtension
->usInstanceId
.Buffer
,
1065 &UsbChildExtension
->usInstanceId
.Length
);
1066 if (!NT_SUCCESS(Status
))
1068 DPRINT1("USBHUB: GetUsbStringDescriptor failed with status %x\n", Status
);
1072 UsbChildExtension
->usInstanceId
.MaximumLength
= UsbChildExtension
->usInstanceId
.Length
;
1073 DPRINT1("Usb InstanceId %wZ\n", &UsbChildExtension
->usInstanceId
);
1078 // the device did not provide a serial number, lets create a pseudo instance id
1080 Index
= swprintf(Buffer
, L
"0&%04d", UsbChildExtension
->PortNumber
) + 1;
1081 UsbChildExtension
->usInstanceId
.Buffer
= (LPWSTR
)ExAllocatePool(NonPagedPool
, Index
* sizeof(WCHAR
));
1082 if (UsbChildExtension
->usInstanceId
.Buffer
== NULL
)
1084 DPRINT1("Error: failed to allocate %lu bytes\n", Index
* sizeof(WCHAR
));
1085 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1092 RtlCopyMemory(UsbChildExtension
->usInstanceId
.Buffer
, Buffer
, Index
* sizeof(WCHAR
));
1093 UsbChildExtension
->usInstanceId
.Length
= UsbChildExtension
->usInstanceId
.MaximumLength
= Index
* sizeof(WCHAR
);
1095 DPRINT1("usDeviceId %wZ\n", &UsbChildExtension
->usInstanceId
);
1102 DestroyUsbChildDeviceObject(
1103 IN PDEVICE_OBJECT UsbHubDeviceObject
,
1106 PHUB_DEVICE_EXTENSION HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)UsbHubDeviceObject
->DeviceExtension
;
1107 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
= NULL
;
1108 PDEVICE_OBJECT ChildDeviceObject
= NULL
;
1111 DPRINT1("Removing device on port %d (Child index: %d)\n", PortId
, Index
);
1113 for (Index
= 0; Index
< USB_MAXCHILDREN
; Index
++)
1115 if (HubDeviceExtension
->ChildDeviceObject
[Index
])
1117 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)HubDeviceExtension
->ChildDeviceObject
[Index
]->DeviceExtension
;
1119 /* Check if it matches the port ID */
1120 if (UsbChildExtension
->PortNumber
== PortId
)
1123 ChildDeviceObject
= HubDeviceExtension
->ChildDeviceObject
[Index
];
1129 /* Fail the request if the device doesn't exist */
1130 if (!ChildDeviceObject
)
1132 DPRINT1("Removal request for non-existant device!\n");
1133 return STATUS_UNSUCCESSFUL
;
1136 /* Remove the device from the table */
1137 HubDeviceExtension
->ChildDeviceObject
[Index
] = NULL
;
1139 /* Invalidate device relations for the root hub */
1140 IoInvalidateDeviceRelations(HubDeviceExtension
->RootHubPhysicalDeviceObject
, BusRelations
);
1142 /* The rest of the removal process takes place in IRP_MN_REMOVE_DEVICE handling for the PDO */
1143 return STATUS_SUCCESS
;
1147 CreateUsbChildDeviceObject(
1148 IN PDEVICE_OBJECT UsbHubDeviceObject
,
1150 OUT PDEVICE_OBJECT
*UsbChildDeviceObject
)
1153 PDEVICE_OBJECT RootHubDeviceObject
, NewChildDeviceObject
;
1154 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1155 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
;
1156 PUSB_BUS_INTERFACE_HUB_V5 HubInterface
;
1157 ULONG ChildDeviceCount
, UsbDeviceNumber
= 0;
1158 WCHAR CharDeviceName
[64];
1159 UNICODE_STRING DeviceName
;
1160 ULONG ConfigDescSize
, DeviceDescSize
;
1161 PVOID HubInterfaceBusContext
;
1162 USB_CONFIGURATION_DESCRIPTOR ConfigDesc
;
1164 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) UsbHubDeviceObject
->DeviceExtension
;
1165 HubInterface
= &HubDeviceExtension
->HubInterface
;
1166 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
1167 HubInterfaceBusContext
= HubDeviceExtension
->UsbDInterface
.BusContext
;
1169 // Find an empty slot in the child device array
1171 for (ChildDeviceCount
= 0; ChildDeviceCount
< USB_MAXCHILDREN
; ChildDeviceCount
++)
1173 if (HubDeviceExtension
->ChildDeviceObject
[ChildDeviceCount
] == NULL
)
1175 DPRINT1("Found unused entry at %d\n", ChildDeviceCount
);
1181 // Check if the limit has been reached for maximum usb devices
1183 if (ChildDeviceCount
== USB_MAXCHILDREN
)
1185 DPRINT1("USBHUB: Too many child devices!\n");
1186 return STATUS_UNSUCCESSFUL
;
1192 // Create a Device Name
1194 swprintf(CharDeviceName
, L
"\\Device\\USBPDO-%d", UsbDeviceNumber
);
1197 // Initialize UnicodeString
1199 RtlInitUnicodeString(&DeviceName
, CharDeviceName
);
1202 // Create a DeviceObject
1204 Status
= IoCreateDevice(UsbHubDeviceObject
->DriverObject
,
1205 sizeof(HUB_CHILDDEVICE_EXTENSION
),
1207 FILE_DEVICE_CONTROLLER
,
1208 FILE_AUTOGENERATED_DEVICE_NAME
,
1210 &NewChildDeviceObject
);
1213 // Check if the name is already in use
1215 if ((Status
== STATUS_OBJECT_NAME_EXISTS
) || (Status
== STATUS_OBJECT_NAME_COLLISION
))
1225 // Check for other errors
1227 if (!NT_SUCCESS(Status
))
1229 DPRINT1("USBHUB: IoCreateDevice failed with status %x\n", Status
);
1233 DPRINT1("USBHUB: Created Device %x\n", NewChildDeviceObject
);
1237 NewChildDeviceObject
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
1240 // Assign the device extensions
1242 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)NewChildDeviceObject
->DeviceExtension
;
1243 RtlZeroMemory(UsbChildExtension
, sizeof(HUB_CHILDDEVICE_EXTENSION
));
1244 UsbChildExtension
->ParentDeviceObject
= UsbHubDeviceObject
;
1245 UsbChildExtension
->PortNumber
= PortId
;
1248 // Create the UsbDeviceObject
1250 Status
= HubInterface
->CreateUsbDevice(HubInterfaceBusContext
,
1251 (PVOID
)&UsbChildExtension
->UsbDeviceHandle
,
1252 HubDeviceExtension
->RootHubHandle
,
1255 if (!NT_SUCCESS(Status
))
1257 DPRINT1("USBHUB: CreateUsbDevice failed with status %x\n", Status
);
1262 // Initialize UsbDevice
1264 Status
= HubInterface
->InitializeUsbDevice(HubInterfaceBusContext
, UsbChildExtension
->UsbDeviceHandle
);
1265 if (!NT_SUCCESS(Status
))
1267 DPRINT1("USBHUB: InitializeUsbDevice failed with status %x\n", Status
);
1271 DPRINT1("Usb Device Handle %x\n", UsbChildExtension
->UsbDeviceHandle
);
1273 ConfigDescSize
= sizeof(USB_CONFIGURATION_DESCRIPTOR
);
1274 DeviceDescSize
= sizeof(USB_DEVICE_DESCRIPTOR
);
1277 // Get the descriptors
1279 Status
= HubInterface
->GetUsbDescriptors(HubInterfaceBusContext
,
1280 UsbChildExtension
->UsbDeviceHandle
,
1281 (PUCHAR
)&UsbChildExtension
->DeviceDesc
,
1283 (PUCHAR
)&ConfigDesc
,
1285 if (!NT_SUCCESS(Status
))
1287 DPRINT1("USBHUB: GetUsbDescriptors failed with status %x\n", Status
);
1291 DumpDeviceDescriptor(&UsbChildExtension
->DeviceDesc
);
1292 DumpConfigurationDescriptor(&ConfigDesc
);
1295 // FIXME: Support more than one configuration and one interface?
1297 if (UsbChildExtension
->DeviceDesc
.bNumConfigurations
> 1)
1299 DPRINT1("Warning: Device has more than one configuration. Only one configuration (the first) is supported!\n");
1302 if (ConfigDesc
.bNumInterfaces
> 1)
1304 DPRINT1("Warning: Device has more that one interface. Only one interface (the first) is currently supported\n");
1307 ConfigDescSize
= ConfigDesc
.wTotalLength
;
1310 // Allocate memory for the first full descriptor, including interfaces and endpoints.
1312 UsbChildExtension
->FullConfigDesc
= ExAllocatePoolWithTag(PagedPool
, ConfigDescSize
, USB_HUB_TAG
);
1315 // Retrieve the full configuration descriptor
1317 Status
= GetUsbDeviceDescriptor(NewChildDeviceObject
,
1318 USB_CONFIGURATION_DESCRIPTOR_TYPE
,
1321 UsbChildExtension
->FullConfigDesc
,
1324 if (!NT_SUCCESS(Status
))
1326 DPRINT1("USBHUB: GetUsbDeviceDescriptor failed with status %x\n", Status
);
1330 DumpFullConfigurationDescriptor(UsbChildExtension
->FullConfigDesc
);
1333 // Construct all the strings that will described the device to PNP
1335 Status
= CreateDeviceIds(NewChildDeviceObject
);
1336 if (!NT_SUCCESS(Status
))
1338 DPRINT1("Failed to create strings needed to describe device to PNP.\n");
1342 HubDeviceExtension
->ChildDeviceObject
[ChildDeviceCount
] = NewChildDeviceObject
;
1344 IoInvalidateDeviceRelations(RootHubDeviceObject
, BusRelations
);
1345 return STATUS_SUCCESS
;
1350 // Remove the usb device if it was created
1352 if (UsbChildExtension
->UsbDeviceHandle
)
1353 HubInterface
->RemoveUsbDevice(HubInterfaceBusContext
, UsbChildExtension
->UsbDeviceHandle
, 0);
1356 // Free full configuration descriptor if one was allocated
1358 if (UsbChildExtension
->FullConfigDesc
)
1359 ExFreePool(UsbChildExtension
->FullConfigDesc
);
1362 // Delete the device object
1364 IoDeleteDevice(NewChildDeviceObject
);
1369 USBHUB_FdoQueryBusRelations(
1370 IN PDEVICE_OBJECT DeviceObject
,
1371 OUT PDEVICE_RELATIONS
* pDeviceRelations
)
1373 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1374 PDEVICE_RELATIONS DeviceRelations
;
1379 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1382 // Count the number of children
1384 for (i
= 0; i
< USB_MAXCHILDREN
; i
++)
1387 if (HubDeviceExtension
->ChildDeviceObject
[i
] == NULL
)
1394 NeededSize
= sizeof(DEVICE_RELATIONS
);
1396 NeededSize
+= (Children
- 1) * sizeof(PDEVICE_OBJECT
);
1399 // Allocate DeviceRelations
1401 DeviceRelations
= (PDEVICE_RELATIONS
)ExAllocatePool(PagedPool
,
1404 if (!DeviceRelations
)
1405 return STATUS_INSUFFICIENT_RESOURCES
;
1406 DeviceRelations
->Count
= Children
;
1410 // Fill in return structure
1412 for (i
= 0; i
< USB_MAXCHILDREN
; i
++)
1414 if (HubDeviceExtension
->ChildDeviceObject
[i
])
1416 ObReferenceObject(HubDeviceExtension
->ChildDeviceObject
[i
]);
1417 HubDeviceExtension
->ChildDeviceObject
[i
]->Flags
&= ~DO_DEVICE_INITIALIZING
;
1418 DeviceRelations
->Objects
[Children
++] = HubDeviceExtension
->ChildDeviceObject
[i
];
1422 ASSERT(Children
== DeviceRelations
->Count
);
1423 *pDeviceRelations
= DeviceRelations
;
1425 return STATUS_SUCCESS
;
1430 RootHubInitCallbackFunction(
1433 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Context
;
1436 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1437 PORT_STATUS_CHANGE StatusChange
;
1439 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1441 DPRINT1("RootHubInitCallbackFunction Sending the initial SCE Request %x\n", DeviceObject
);
1443 for (PortId
= 1; PortId
<= HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
; PortId
++)
1448 Status
= GetPortStatusAndChange(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, &StatusChange
);
1449 if (NT_SUCCESS(Status
))
1452 // is there a device connected
1454 if (StatusChange
.Status
& USB_PORT_STATUS_CONNECT
)
1459 Status
= SetPortFeature(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, PORT_RESET
);
1460 if (!NT_SUCCESS(Status
))
1461 DPRINT1("Failed to reset on port %d\n", PortId
);
1467 // Send the first SCE Request
1469 QueryStatusChangeEndpoint(DeviceObject
);
1473 USBHUB_FdoHandlePnp(
1474 IN PDEVICE_OBJECT DeviceObject
,
1477 PIO_STACK_LOCATION Stack
;
1478 NTSTATUS Status
= STATUS_SUCCESS
;
1479 ULONG_PTR Information
= 0;
1480 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1481 PDEVICE_OBJECT RootHubDeviceObject
;
1482 PVOID HubInterfaceBusContext
, UsbDInterfaceBusContext
;
1483 PORT_STATUS_CHANGE StatusChange
;
1485 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1487 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1489 switch (Stack
->MinorFunction
)
1491 case IRP_MN_START_DEVICE
:
1494 PUSB_INTERFACE_DESCRIPTOR Pid
;
1495 ULONG Result
= 0, PortId
;
1496 USBD_INTERFACE_LIST_ENTRY InterfaceList
[2] = {{NULL
, NULL
}, {NULL
, NULL
}};
1497 PURB ConfigUrb
= NULL
;
1500 DPRINT1("IRP_MJ_PNP / IRP_MN_START_DEVICE\n");
1503 // Allocated size including the sizeof USBD_INTERFACE_LIST_ENTRY
1505 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
) + sizeof(USBD_INTERFACE_LIST_ENTRY
), USB_HUB_TAG
);
1506 RtlZeroMemory(Urb
, sizeof(URB
) + sizeof(USBD_INTERFACE_LIST_ENTRY
));
1509 // Get the Root Hub Pdo
1511 SubmitRequestToRootHub(HubDeviceExtension
->LowerDeviceObject
,
1512 IOCTL_INTERNAL_USB_GET_ROOTHUB_PDO
,
1513 &HubDeviceExtension
->RootHubPhysicalDeviceObject
,
1514 &HubDeviceExtension
->RootHubFunctionalDeviceObject
);
1516 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
1517 ASSERT(HubDeviceExtension
->RootHubPhysicalDeviceObject
);
1518 ASSERT(HubDeviceExtension
->RootHubFunctionalDeviceObject
);
1519 DPRINT1("RootPdo %x, RootFdo %x\n",
1520 HubDeviceExtension
->RootHubPhysicalDeviceObject
,
1521 HubDeviceExtension
->RootHubFunctionalDeviceObject
);
1524 // Send the StartDevice to RootHub
1526 Status
= ForwardIrpAndWait(RootHubDeviceObject
, Irp
);
1528 if (!NT_SUCCESS(Status
))
1530 DPRINT1("Failed to start the RootHub PDO\n");
1535 // Get the current number of hubs
1537 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1538 IOCTL_INTERNAL_USB_GET_HUB_COUNT
,
1539 &HubDeviceExtension
->NumberOfHubs
, NULL
);
1542 // Get the Hub Interface
1544 Status
= QueryInterface(RootHubDeviceObject
,
1545 USB_BUS_INTERFACE_HUB_GUID
,
1546 sizeof(USB_BUS_INTERFACE_HUB_V5
),
1548 (PVOID
)&HubDeviceExtension
->HubInterface
);
1550 if (!NT_SUCCESS(Status
))
1552 DPRINT1("Failed to get HUB_GUID interface with status 0x%08lx\n", Status
);
1553 return STATUS_UNSUCCESSFUL
;
1556 HubInterfaceBusContext
= HubDeviceExtension
->HubInterface
.BusContext
;
1559 // Get the USBDI Interface
1561 Status
= QueryInterface(RootHubDeviceObject
,
1562 USB_BUS_INTERFACE_USBDI_GUID
,
1563 sizeof(USB_BUS_INTERFACE_USBDI_V2
),
1565 (PVOID
)&HubDeviceExtension
->UsbDInterface
);
1567 if (!NT_SUCCESS(Status
))
1569 DPRINT1("Failed to get USBDI_GUID interface with status 0x%08lx\n", Status
);
1573 UsbDInterfaceBusContext
= HubDeviceExtension
->UsbDInterface
.BusContext
;
1576 // Get Root Hub Device Handle
1578 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1579 IOCTL_INTERNAL_USB_GET_DEVICE_HANDLE
,
1580 &HubDeviceExtension
->RootHubHandle
,
1583 if (!NT_SUCCESS(Status
))
1585 DPRINT1("GetRootHubDeviceHandle failed with status 0x%08lx\n", Status
);
1590 // Get Hub Device Information
1592 Status
= HubDeviceExtension
->HubInterface
.QueryDeviceInformation(HubInterfaceBusContext
,
1593 HubDeviceExtension
->RootHubHandle
,
1594 &HubDeviceExtension
->DeviceInformation
,
1595 sizeof(USB_DEVICE_INFORMATION_0
),
1598 DPRINT1("Status %x, Result 0x%08lx\n", Status
, Result
);
1599 DPRINT1("InformationLevel %x\n", HubDeviceExtension
->DeviceInformation
.InformationLevel
);
1600 DPRINT1("ActualLength %x\n", HubDeviceExtension
->DeviceInformation
.ActualLength
);
1601 DPRINT1("PortNumber %x\n", HubDeviceExtension
->DeviceInformation
.PortNumber
);
1602 DPRINT1("DeviceDescriptor %x\n", HubDeviceExtension
->DeviceInformation
.DeviceDescriptor
);
1603 DPRINT1("HubAddress %x\n", HubDeviceExtension
->DeviceInformation
.HubAddress
);
1604 DPRINT1("NumberofPipes %x\n", HubDeviceExtension
->DeviceInformation
.NumberOfOpenPipes
);
1607 // Get Root Hubs Device Descriptor
1609 UsbBuildGetDescriptorRequest(Urb
,
1610 sizeof(Urb
->UrbControlDescriptorRequest
),
1611 USB_DEVICE_DESCRIPTOR_TYPE
,
1614 &HubDeviceExtension
->HubDeviceDescriptor
,
1616 sizeof(USB_DEVICE_DESCRIPTOR
),
1619 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;//HubDeviceExtension->RootHubHandle;
1621 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1622 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1626 if (!NT_SUCCESS(Status
))
1628 DPRINT1("Failed to get HubDeviceDescriptor!\n");
1631 DumpDeviceDescriptor(&HubDeviceExtension
->HubDeviceDescriptor
);
1634 // Get Root Hubs Configuration Descriptor
1636 UsbBuildGetDescriptorRequest(Urb
,
1637 sizeof(Urb
->UrbControlDescriptorRequest
),
1638 USB_CONFIGURATION_DESCRIPTOR_TYPE
,
1641 &HubDeviceExtension
->HubConfigDescriptor
,
1643 sizeof(USB_CONFIGURATION_DESCRIPTOR
) + sizeof(USB_INTERFACE_DESCRIPTOR
) + sizeof(USB_ENDPOINT_DESCRIPTOR
),
1646 DPRINT1("RootHub Handle %x\n", HubDeviceExtension
->RootHubHandle
);
1647 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;//HubDeviceExtension->RootHubHandle;
1649 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1650 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1654 if (!NT_SUCCESS(Status
))
1656 DPRINT1("Failed to get RootHub Configuration with status %x\n", Status
);
1659 ASSERT(HubDeviceExtension
->HubConfigDescriptor
.wTotalLength
);
1661 DumpConfigurationDescriptor(&HubDeviceExtension
->HubConfigDescriptor
);
1663 Status
= HubDeviceExtension
->HubInterface
.GetExtendedHubInformation(HubInterfaceBusContext
,
1664 RootHubDeviceObject
,
1665 &HubDeviceExtension
->UsbExtHubInfo
,
1666 sizeof(USB_EXTHUB_INFORMATION_0
),
1668 if (!NT_SUCCESS(Status
))
1670 DPRINT1("Failed to extended hub information. Unable to determine the number of ports!\n");
1674 DPRINT1("HubDeviceExtension->UsbExtHubInfo.NumberOfPorts %x\n", HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
);
1677 // Get the Hub Descriptor
1679 UsbBuildVendorRequest(Urb
,
1680 URB_FUNCTION_CLASS_DEVICE
,
1681 sizeof(Urb
->UrbControlVendorClassRequest
),
1682 USBD_TRANSFER_DIRECTION_IN
| USBD_SHORT_TRANSFER_OK
,
1684 USB_REQUEST_GET_DESCRIPTOR
,
1685 USB_DEVICE_CLASS_RESERVED
,
1687 &HubDeviceExtension
->HubDescriptor
,
1689 sizeof(USB_HUB_DESCRIPTOR
),
1692 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;//HubDeviceExtension->RootHubHandle;
1694 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1695 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1699 DPRINT1("bDescriptorType %x\n", HubDeviceExtension
->HubDescriptor
.bDescriptorType
);
1701 if (!NT_SUCCESS(Status
))
1703 DPRINT1("Failed to get Hub Descriptor!\n");
1705 return STATUS_UNSUCCESSFUL
;
1709 UsbBuildGetStatusRequest(Urb
,
1710 URB_FUNCTION_GET_STATUS_FROM_DEVICE
,
1715 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;//HubDeviceExtension->RootHubHandle;
1717 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1718 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1721 if (!NT_SUCCESS(Status
))
1723 DPRINT1("Failed to get Hub Status!\n");
1725 return STATUS_UNSUCCESSFUL
;
1728 DPRINT1("HubStatus %x\n", HubStatus
);
1731 // Allocate memory for PortStatusChange to hold 2 USHORTs for each port on hub
1733 HubDeviceExtension
->PortStatusChange
= ExAllocatePoolWithTag(NonPagedPool
,
1734 sizeof(ULONG
) * HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
,
1738 // Get the first Configuration Descriptor
1740 Pid
= USBD_ParseConfigurationDescriptorEx(&HubDeviceExtension
->HubConfigDescriptor
,
1741 &HubDeviceExtension
->HubConfigDescriptor
,
1742 -1, -1, -1, -1, -1);
1744 ASSERT(Pid
!= NULL
);
1746 InterfaceList
[0].InterfaceDescriptor
= Pid
;
1747 ConfigUrb
= USBD_CreateConfigurationRequestEx(&HubDeviceExtension
->HubConfigDescriptor
,
1748 (PUSBD_INTERFACE_LIST_ENTRY
)&InterfaceList
);
1749 ASSERT(ConfigUrb
!= NULL
);
1751 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1752 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1756 HubDeviceExtension
->ConfigurationHandle
= ConfigUrb
->UrbSelectConfiguration
.ConfigurationHandle
;
1757 HubDeviceExtension
->PipeHandle
= ConfigUrb
->UrbSelectConfiguration
.Interface
.Pipes
[0].PipeHandle
;
1758 DPRINT1("Configuration Handle %x\n", HubDeviceExtension
->ConfigurationHandle
);
1761 // check if function is available
1763 if (HubDeviceExtension
->UsbDInterface
.IsDeviceHighSpeed
)
1766 // is it high speed bus
1768 if (HubDeviceExtension
->UsbDInterface
.IsDeviceHighSpeed(HubInterfaceBusContext
))
1771 // initialize usb 2.0 hub
1773 Status
= HubDeviceExtension
->HubInterface
.Initialize20Hub(HubInterfaceBusContext
,
1774 HubDeviceExtension
->RootHubHandle
, 1);
1775 DPRINT1("Status %x\n", Status
);
1778 // FIXME handle error
1780 ASSERT(Status
== STATUS_SUCCESS
);
1784 ExFreePool(ConfigUrb
);
1787 // Enable power on all ports
1790 DPRINT1("Enabling PortPower on all ports!\n");
1792 for (PortId
= 1; PortId
<= HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
; PortId
++)
1794 Status
= SetPortFeature(RootHubDeviceObject
, PortId
, PORT_POWER
);
1795 if (!NT_SUCCESS(Status
))
1796 DPRINT1("Failed to power on port %d\n", PortId
);
1798 Status
= ClearPortFeature(RootHubDeviceObject
, PortId
, C_PORT_CONNECTION
);
1799 if (!NT_SUCCESS(Status
))
1800 DPRINT1("Failed to power on port %d\n", PortId
);
1803 DPRINT1("RootHubInitNotification %x\n", HubDeviceExtension
->HubInterface
.RootHubInitNotification
);
1806 // init roo hub notification
1808 if (HubDeviceExtension
->HubInterface
.RootHubInitNotification
)
1810 Status
= HubDeviceExtension
->HubInterface
.RootHubInitNotification(HubInterfaceBusContext
,
1812 RootHubInitCallbackFunction
);
1813 if (!NT_SUCCESS(Status
))
1815 DPRINT1("Failed to set callback\n");
1823 for (PortId
= 1; PortId
<= HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
; PortId
++)
1828 Status
= GetPortStatusAndChange(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, &StatusChange
);
1829 if (NT_SUCCESS(Status
))
1832 // is there a device connected
1834 if (StatusChange
.Status
& USB_PORT_STATUS_CONNECT
)
1839 Status
= SetPortFeature(HubDeviceExtension
->RootHubPhysicalDeviceObject
, PortId
, PORT_RESET
);
1840 if (!NT_SUCCESS(Status
))
1841 DPRINT1("Failed to reset on port %d\n", PortId
);
1847 // Send the first SCE Request
1849 QueryStatusChangeEndpoint(DeviceObject
);
1856 case IRP_MN_QUERY_DEVICE_RELATIONS
:
1858 switch (Stack
->Parameters
.QueryDeviceRelations
.Type
)
1862 PDEVICE_RELATIONS DeviceRelations
= NULL
;
1863 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / BusRelations\n");
1865 Status
= USBHUB_FdoQueryBusRelations(DeviceObject
, &DeviceRelations
);
1867 Information
= (ULONG_PTR
)DeviceRelations
;
1870 case RemovalRelations
:
1872 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / RemovalRelations\n");
1873 return ForwardIrpAndForget(DeviceObject
, Irp
);
1876 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / Unknown type 0x%lx\n",
1877 Stack
->Parameters
.QueryDeviceRelations
.Type
);
1878 return ForwardIrpAndForget(DeviceObject
, Irp
);
1882 case IRP_MN_QUERY_BUS_INFORMATION
:
1884 DPRINT1("IRP_MN_QUERY_BUS_INFORMATION\n");
1887 case IRP_MN_QUERY_ID
:
1889 DPRINT1("IRP_MN_QUERY_ID\n");
1892 case IRP_MN_QUERY_CAPABILITIES
:
1894 DPRINT1("IRP_MN_QUERY_CAPABILITIES\n");
1899 DPRINT1(" IRP_MJ_PNP / unknown minor function 0x%lx\n", Stack
->MinorFunction
);
1900 return ForwardIrpAndForget(DeviceObject
, Irp
);
1904 Irp
->IoStatus
.Information
= Information
;
1905 Irp
->IoStatus
.Status
= Status
;
1906 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1911 USBHUB_FdoHandleDeviceControl(
1912 IN PDEVICE_OBJECT DeviceObject
,
1915 DPRINT1("FdoHandleDeviceControl\n");
1917 return STATUS_NOT_IMPLEMENTED
;