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 // Create the device object only if the port manipulation was started by a device connect
407 if (HubDeviceExtension
->PortStatusChange
[PortId
-1].Status
)
409 HubDeviceExtension
->PortStatusChange
[PortId
-1].Status
= 0;
410 Status
= CreateUsbChildDeviceObject(DeviceObject
, PortId
, NULL
);
416 // FIXME: Still in testing
421 DPRINT1("SCE completed over 100 times but no action has been taken to clear the Change of any ports.\n");
423 // Return and dont send any more SCE Requests
428 ExFreePool(WorkItemData
);
431 // Send another SCE Request
433 DPRINT1("Sending another SCE!\n");
434 QueryStatusChangeEndpoint(DeviceObject
);
439 StatusChangeEndpointCompletion(
440 IN PDEVICE_OBJECT DeviceObject
,
444 PDEVICE_OBJECT RealDeviceObject
;
445 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
446 PWORK_ITEM_DATA WorkItemData
;
448 RealDeviceObject
= (PDEVICE_OBJECT
)Context
;
449 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)RealDeviceObject
->DeviceExtension
;
452 // NOTE: USBPORT frees this IRP
454 DPRINT1("Received Irp %x, HubDeviceExtension->PendingSCEIrp %x\n", Irp
, HubDeviceExtension
->PendingSCEIrp
);
458 // Create and initialize work item data
460 WorkItemData
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(WORK_ITEM_DATA
), USB_HUB_TAG
);
463 DPRINT1("Failed to allocate memory!n");
464 return STATUS_INSUFFICIENT_RESOURCES
;
466 WorkItemData
->Context
= RealDeviceObject
;
467 DPRINT1("Initialize work item\n");
468 ExInitializeWorkItem(&WorkItemData
->WorkItem
, DeviceStatusChangeThread
, (PVOID
)WorkItemData
);
471 // Queue the work item to handle initializing the device
473 ExQueueWorkItem(&WorkItemData
->WorkItem
, DelayedWorkQueue
);
476 // Return more processing required so the IO Manger doesn’t try to mess with IRP just freed
478 return STATUS_MORE_PROCESSING_REQUIRED
;
482 QueryStatusChangeEndpoint(
483 IN PDEVICE_OBJECT DeviceObject
)
486 PDEVICE_OBJECT RootHubDeviceObject
;
487 PIO_STACK_LOCATION Stack
;
488 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
491 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
492 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
497 PendingSCEUrb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
500 // Initialize URB for Status Change Endpoint request
502 UsbBuildInterruptOrBulkTransferRequest(PendingSCEUrb
,
503 sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER
),
504 HubDeviceExtension
->PipeHandle
,
505 HubDeviceExtension
->PortStatusChange
,
507 sizeof(USHORT
) * 2 * HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
,
508 USBD_TRANSFER_DIRECTION_IN
| USBD_SHORT_TRANSFER_OK
,
512 // Set the device handle to null for roothub
514 PendingSCEUrb
->UrbHeader
.UsbdDeviceHandle
= NULL
;//HubDeviceExtension->RootHubHandle;
520 HubDeviceExtension
->PendingSCEIrp
= ExAllocatePoolWithTag(NonPagedPool
,
521 IoSizeOfIrp(RootHubDeviceObject
->StackSize
),
524 HubDeviceExtension->PendingSCEIrp = IoAllocateIrp(RootHubDeviceObject->StackSize,
527 DPRINT1("Allocated IRP %x\n", HubDeviceExtension
->PendingSCEIrp
);
529 if (!HubDeviceExtension
->PendingSCEIrp
)
531 DPRINT1("USBHUB: Failed to allocate IRP for SCE request!\n");
532 return STATUS_INSUFFICIENT_RESOURCES
;
536 // Initialize the IRP
538 IoInitializeIrp(HubDeviceExtension
->PendingSCEIrp
,
539 IoSizeOfIrp(RootHubDeviceObject
->StackSize
),
540 RootHubDeviceObject
->StackSize
);
542 HubDeviceExtension
->PendingSCEIrp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
543 HubDeviceExtension
->PendingSCEIrp
->IoStatus
.Information
= 0;
544 HubDeviceExtension
->PendingSCEIrp
->Flags
= 0;
545 HubDeviceExtension
->PendingSCEIrp
->UserBuffer
= NULL
;
548 // Get the Next Stack Location and Initialize it
550 Stack
= IoGetNextIrpStackLocation(HubDeviceExtension
->PendingSCEIrp
);
551 Stack
->DeviceObject
= DeviceObject
;
552 Stack
->Parameters
.Others
.Argument1
= PendingSCEUrb
;
553 Stack
->Parameters
.Others
.Argument2
= NULL
;
554 Stack
->MajorFunction
= IRP_MJ_INTERNAL_DEVICE_CONTROL
;
555 Stack
->Parameters
.DeviceIoControl
.IoControlCode
= IOCTL_INTERNAL_USB_SUBMIT_URB
;
558 // Set the completion routine for when device is connected to root hub
560 IoSetCompletionRoutine(HubDeviceExtension
->PendingSCEIrp
,
561 StatusChangeEndpointCompletion
,
570 DPRINT1("DeviceObject is %x\n", DeviceObject
);
571 DPRINT1("Iocalldriver %x with irp %x\n", RootHubDeviceObject
, HubDeviceExtension
->PendingSCEIrp
);
572 Status
= IoCallDriver(RootHubDeviceObject
, HubDeviceExtension
->PendingSCEIrp
);
574 return STATUS_PENDING
;
579 IN PDEVICE_OBJECT DeviceObject
,
580 IN CONST GUID InterfaceType
,
587 IO_STATUS_BLOCK IoStatus
;
589 PIO_STACK_LOCATION Stack
= NULL
;
592 // Initialize the Event used to wait for Irp completion
594 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
597 // Build Control Request
599 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_PNP
,
608 // Get Next Stack Location and Initialize it.
610 Stack
= IoGetNextIrpStackLocation(Irp
);
611 Stack
->MinorFunction
= IRP_MN_QUERY_INTERFACE
;
612 Stack
->Parameters
.QueryInterface
.InterfaceType
= &InterfaceType
;//USB_BUS_INTERFACE_HUB_GUID;
613 Stack
->Parameters
.QueryInterface
.Size
= Size
;
614 Stack
->Parameters
.QueryInterface
.Version
= Version
;
615 Stack
->Parameters
.QueryInterface
.Interface
= Interface
;
616 Stack
->Parameters
.QueryInterface
.InterfaceSpecificData
= NULL
;
618 Status
= IoCallDriver(DeviceObject
, Irp
);
620 if (Status
== STATUS_PENDING
)
622 DPRINT("Operation pending\n");
623 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
624 Status
= IoStatus
.Status
;
631 GetUsbDeviceDescriptor(
632 IN PDEVICE_OBJECT ChildDeviceObject
,
633 IN UCHAR DescriptorType
,
636 OUT PVOID TransferBuffer
,
637 IN ULONG TransferBufferLength
)
640 PDEVICE_OBJECT RootHubDeviceObject
;
642 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
643 PHUB_CHILDDEVICE_EXTENSION ChildDeviceExtension
;
646 // Get the Hubs Device Extension
648 ChildDeviceExtension
= (PHUB_CHILDDEVICE_EXTENSION
)ChildDeviceObject
->DeviceExtension
;
649 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) ChildDeviceExtension
->ParentDeviceObject
->DeviceExtension
;
650 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
655 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
), USB_HUB_TAG
);
658 DPRINT1("Failed to allocate memory for URB!\n");
659 return STATUS_INSUFFICIENT_RESOURCES
;
665 RtlZeroMemory(Urb
, sizeof(URB
));
668 // Initialize URB for getting device descriptor
670 UsbBuildGetDescriptorRequest(Urb
,
671 sizeof(Urb
->UrbControlDescriptorRequest
),
677 TransferBufferLength
,
681 // Set the device handle
683 Urb
->UrbHeader
.UsbdDeviceHandle
= (PVOID
)ChildDeviceExtension
->UsbDeviceHandle
;
686 // Query the Root Hub
688 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
689 IOCTL_INTERNAL_USB_SUBMIT_URB
,
697 GetUsbStringDescriptor(
698 IN PDEVICE_OBJECT ChildDeviceObject
,
701 OUT PVOID
*TransferBuffer
,
705 PUSB_STRING_DESCRIPTOR StringDesc
= NULL
;
708 StringDesc
= ExAllocatePoolWithTag(NonPagedPool
,
709 sizeof(USB_STRING_DESCRIPTOR
),
713 DPRINT1("Failed to allocate buffer for string!\n");
714 return STATUS_INSUFFICIENT_RESOURCES
;
718 // Get the index string descriptor length
719 // FIXME: Implement LangIds
721 Status
= GetUsbDeviceDescriptor(ChildDeviceObject
,
722 USB_STRING_DESCRIPTOR_TYPE
,
726 sizeof(USB_STRING_DESCRIPTOR
));
727 if (!NT_SUCCESS(Status
))
729 DPRINT1("GetUsbDeviceDescriptor failed with status %x\n", Status
);
730 ExFreePool(StringDesc
);
733 DPRINT1("StringDesc->bLength %d\n", StringDesc
->bLength
);
736 // Did we get something more than the length of the first two fields of structure?
738 if (StringDesc
->bLength
== 2)
740 DPRINT1("USB Device Error!\n");
741 ExFreePool(StringDesc
);
742 return STATUS_DEVICE_DATA_ERROR
;
744 SizeNeeded
= StringDesc
->bLength
+ sizeof(WCHAR
);
749 ExFreePool(StringDesc
);
752 // Recreate with appropriate size
754 StringDesc
= ExAllocatePoolWithTag(NonPagedPool
,
759 DPRINT1("Failed to allocate buffer for string!\n");
760 return STATUS_INSUFFICIENT_RESOURCES
;
763 RtlZeroMemory(StringDesc
, SizeNeeded
);
768 Status
= GetUsbDeviceDescriptor(ChildDeviceObject
,
769 USB_STRING_DESCRIPTOR_TYPE
,
774 if (!NT_SUCCESS(Status
))
776 DPRINT1("GetUsbDeviceDescriptor failed with status %x\n", Status
);
777 ExFreePool(StringDesc
);
782 // Allocate Buffer to return
784 *TransferBuffer
= ExAllocatePoolWithTag(NonPagedPool
,
787 if (!*TransferBuffer
)
789 DPRINT1("Failed to allocate buffer for string!\n");
790 ExFreePool(StringDesc
);
791 return STATUS_INSUFFICIENT_RESOURCES
;
794 RtlZeroMemory(*TransferBuffer
, SizeNeeded
);
797 // Copy the string to destination
799 RtlCopyMemory(*TransferBuffer
, StringDesc
->bString
, SizeNeeded
- FIELD_OFFSET(USB_STRING_DESCRIPTOR
, bLength
));
802 ExFreePool(StringDesc
);
804 return STATUS_SUCCESS
;
809 IN PUSB_DEVICE_DESCRIPTOR DeviceDescriptor
,
810 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
)
812 if (DeviceDescriptor
->bNumConfigurations
!= 1)
815 // composite device must have only one configuration
820 if (ConfigurationDescriptor
->bNumInterfaces
< 2)
823 // composite device must have multiple interfaces
828 if (DeviceDescriptor
->bDeviceClass
== 0)
833 ASSERT(DeviceDescriptor
->bDeviceSubClass
== 0);
834 ASSERT(DeviceDescriptor
->bDeviceProtocol
== 0);
838 if (DeviceDescriptor
->bDeviceClass
== 0xEF &&
839 DeviceDescriptor
->bDeviceSubClass
== 0x02 &&
840 DeviceDescriptor
->bDeviceProtocol
== 0x01)
843 // USB-IF association descriptor
849 // not a composite device
856 PDEVICE_OBJECT UsbChildDeviceObject
)
858 NTSTATUS Status
= STATUS_SUCCESS
;
862 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
;
863 PUSB_DEVICE_DESCRIPTOR DeviceDescriptor
;
864 PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
;
865 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
868 // get child device extension
870 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)UsbChildDeviceObject
->DeviceExtension
;
873 // Initialize the CompatibleIds String
875 UsbChildExtension
->usCompatibleIds
.Length
= 144; //FIXME
876 UsbChildExtension
->usCompatibleIds
.MaximumLength
= UsbChildExtension
->usCompatibleIds
.Length
;
879 // allocate mem for compatible id string
881 BufferPtr
= ExAllocatePoolWithTag(NonPagedPool
,
882 UsbChildExtension
->usCompatibleIds
.Length
,
886 DPRINT1("Failed to allocate memory\n");
887 return STATUS_INSUFFICIENT_RESOURCES
;
890 RtlZeroMemory(BufferPtr
, UsbChildExtension
->usCompatibleIds
.Length
);
894 // get device descriptor
896 DeviceDescriptor
= &UsbChildExtension
->DeviceDesc
;
899 // get configuration descriptor
901 ConfigurationDescriptor
= UsbChildExtension
->FullConfigDesc
;
904 // get interface descriptor
906 InterfaceDescriptor
= (PUSB_INTERFACE_DESCRIPTOR
)(ConfigurationDescriptor
+ 1);
909 // Construct the CompatibleIds
911 if (IsCompositeDevice(DeviceDescriptor
, ConfigurationDescriptor
))
916 ASSERT(DeviceDescriptor
->bNumConfigurations
== 1);
917 ASSERT(ConfigurationDescriptor
->bNumInterfaces
> 1);
919 Index
+= swprintf(&BufferPtr
[Index
],
920 L
"USB\\DevClass_%02x&SubClass_%02x&Prot_%02x",
921 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
, DeviceDescriptor
->bDeviceProtocol
) + 1;
922 Index
+= swprintf(&BufferPtr
[Index
],
923 L
"USB\\DevClass_%02x&SubClass_%02x",
924 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
) + 1;
925 Index
+= swprintf(&BufferPtr
[Index
],
926 L
"USB\\DevClass_%02x",
927 DeviceDescriptor
->bDeviceClass
) + 1;
928 Index
+= swprintf(&BufferPtr
[Index
],
929 L
"USB\\COMPOSITE") + 1;
934 // sanity checks for simple usb device
936 ASSERT(ConfigurationDescriptor
->bNumInterfaces
== 1);
939 // FIXME: support multiple configurations
941 ASSERT(DeviceDescriptor
->bNumConfigurations
== 1);
943 if (DeviceDescriptor
->bDeviceClass
== 0)
945 Index
+= swprintf(&BufferPtr
[Index
],
946 L
"USB\\Class_%02x&SubClass_%02x&Prot_%02x",
947 InterfaceDescriptor
->bInterfaceClass
, InterfaceDescriptor
->bInterfaceSubClass
, InterfaceDescriptor
->bInterfaceProtocol
) + 1;
948 Index
+= swprintf(&BufferPtr
[Index
],
949 L
"USB\\Class_%02x&SubClass_%02x",
950 InterfaceDescriptor
->bInterfaceClass
, InterfaceDescriptor
->bInterfaceSubClass
) + 1;
951 Index
+= swprintf(&BufferPtr
[Index
],
953 InterfaceDescriptor
->bInterfaceClass
) + 1;
957 Index
+= swprintf(&BufferPtr
[Index
],
958 L
"USB\\Class_%02x&SubClass_%02x&Prot_%02x",
959 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
, DeviceDescriptor
->bDeviceProtocol
) + 1;
960 Index
+= swprintf(&BufferPtr
[Index
],
961 L
"USB\\Class_%02x&SubClass_%02x",
962 DeviceDescriptor
->bDeviceClass
, DeviceDescriptor
->bDeviceSubClass
) + 1;
963 Index
+= swprintf(&BufferPtr
[Index
],
965 DeviceDescriptor
->bDeviceClass
) + 1;
971 BufferPtr
[Index
] = UNICODE_NULL
;
972 UsbChildExtension
->usCompatibleIds
.Buffer
= BufferPtr
;
973 DPRINT1("usCompatibleIds %wZ\n", &UsbChildExtension
->usCompatibleIds
);
976 // Initialize the DeviceId String
978 UsbChildExtension
->usDeviceId
.Length
= 44;
979 UsbChildExtension
->usDeviceId
.MaximumLength
= UsbChildExtension
->usDeviceId
.Length
;
980 BufferPtr
= ExAllocatePoolWithTag(NonPagedPool
,
981 UsbChildExtension
->usDeviceId
.Length
,
985 DPRINT1("Failed to allocate memory\n");
986 Status
= STATUS_INSUFFICIENT_RESOURCES
;
991 // Construct DeviceId
993 swprintf(BufferPtr
, L
"USB\\Vid_%04x&Pid_%04x\0", UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
);
994 UsbChildExtension
->usDeviceId
.Buffer
= BufferPtr
;
995 DPRINT1("usDeviceId %wZ\n", &UsbChildExtension
->usDeviceId
);
998 // Initialize the HardwareId String
1000 UsbChildExtension
->usHardwareIds
.Length
= 110;
1001 UsbChildExtension
->usHardwareIds
.MaximumLength
= UsbChildExtension
->usHardwareIds
.Length
;
1002 BufferPtr
= ExAllocatePoolWithTag(NonPagedPool
, UsbChildExtension
->usHardwareIds
.Length
, USB_HUB_TAG
);
1005 DPRINT1("Failed to allocate memory\n");
1006 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1010 RtlZeroMemory(BufferPtr
, UsbChildExtension
->usHardwareIds
.Length
);
1013 // Consturct HardwareIds
1016 Index
+= swprintf(&BufferPtr
[Index
],
1017 L
"USB\\Vid_%04x&Pid_%04x&Rev_%04x",
1018 UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
, UsbChildExtension
->DeviceDesc
.bcdDevice
) + 1;
1019 Index
+= swprintf(&BufferPtr
[Index
],
1020 L
"USB\\Vid_%04x&Pid_%04x",
1021 UsbChildExtension
->DeviceDesc
.idVendor
, UsbChildExtension
->DeviceDesc
.idProduct
) + 1;
1022 BufferPtr
[Index
] = UNICODE_NULL
;
1023 UsbChildExtension
->usHardwareIds
.Buffer
= BufferPtr
;
1024 DPRINT1("usHardWareIds %wZ\n", &UsbChildExtension
->usHardwareIds
);
1027 // FIXME: Handle Lang ids
1031 // Get the product string if obe provided
1033 if (UsbChildExtension
->DeviceDesc
.iProduct
)
1035 Status
= GetUsbStringDescriptor(UsbChildDeviceObject
,
1036 UsbChildExtension
->DeviceDesc
.iProduct
,
1038 (PVOID
*)&UsbChildExtension
->usTextDescription
.Buffer
,
1039 &UsbChildExtension
->usTextDescription
.Length
);
1040 if (!NT_SUCCESS(Status
))
1042 DPRINT1("USBHUB: GetUsbStringDescriptor failed with status %x\n", Status
);
1046 UsbChildExtension
->usTextDescription
.MaximumLength
= UsbChildExtension
->usTextDescription
.Length
;
1047 DPRINT1("Usb TextDescription %wZ\n", &UsbChildExtension
->usTextDescription
);
1051 // Get the Serial Number string if obe provided
1053 if (UsbChildExtension
->DeviceDesc
.iSerialNumber
)
1055 Status
= GetUsbStringDescriptor(UsbChildDeviceObject
,
1056 UsbChildExtension
->DeviceDesc
.iSerialNumber
,
1058 (PVOID
*)&UsbChildExtension
->usInstanceId
.Buffer
,
1059 &UsbChildExtension
->usInstanceId
.Length
);
1060 if (!NT_SUCCESS(Status
))
1062 DPRINT1("USBHUB: GetUsbStringDescriptor failed with status %x\n", Status
);
1066 UsbChildExtension
->usInstanceId
.MaximumLength
= UsbChildExtension
->usInstanceId
.Length
;
1067 DPRINT1("Usb InstanceId %wZ\n", &UsbChildExtension
->usInstanceId
);
1072 // the device did not provide a serial number, lets create a pseudo instance id
1074 Index
= swprintf(Buffer
, L
"0&%04d", UsbChildExtension
->PortNumber
) + 1;
1075 UsbChildExtension
->usInstanceId
.Buffer
= (LPWSTR
)ExAllocatePool(NonPagedPool
, Index
* sizeof(WCHAR
));
1076 if (UsbChildExtension
->usInstanceId
.Buffer
== NULL
)
1078 DPRINT1("Error: failed to allocate %lu bytes\n", Index
* sizeof(WCHAR
));
1079 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1086 RtlCopyMemory(UsbChildExtension
->usInstanceId
.Buffer
, Buffer
, Index
* sizeof(WCHAR
));
1087 UsbChildExtension
->usInstanceId
.Length
= UsbChildExtension
->usDeviceId
.MaximumLength
= Index
* sizeof(WCHAR
);
1089 DPRINT1("usDeviceId %wZ\n", &UsbChildExtension
->usInstanceId
);
1099 if (UsbChildExtension
->usCompatibleIds
.Buffer
)
1100 ExFreePool(UsbChildExtension
->usCompatibleIds
.Buffer
);
1101 if (UsbChildExtension
->usDeviceId
.Buffer
)
1102 ExFreePool(UsbChildExtension
->usDeviceId
.Buffer
);
1103 if (UsbChildExtension
->usHardwareIds
.Buffer
)
1104 ExFreePool(UsbChildExtension
->usHardwareIds
.Buffer
);
1105 if (UsbChildExtension
->usTextDescription
.Buffer
)
1106 ExFreePool(UsbChildExtension
->usTextDescription
.Buffer
);
1107 if (UsbChildExtension
->usInstanceId
.Buffer
)
1108 ExFreePool(UsbChildExtension
->usInstanceId
.Buffer
);
1114 DestroyUsbChildDeviceObject(
1115 IN PDEVICE_OBJECT UsbHubDeviceObject
,
1118 PHUB_DEVICE_EXTENSION HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)UsbHubDeviceObject
->DeviceExtension
;
1119 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
= NULL
;
1120 PDEVICE_OBJECT ChildDeviceObject
= NULL
;
1123 DPRINT1("Removing device on port %d (Child index: %d)\n", PortId
, Index
);
1125 for (Index
= 0; Index
< USB_MAXCHILDREN
; Index
++)
1127 if (HubDeviceExtension
->ChildDeviceObject
[Index
])
1129 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)HubDeviceExtension
->ChildDeviceObject
[Index
]->DeviceExtension
;
1131 /* Check if it matches the port ID */
1132 if (UsbChildExtension
->PortNumber
== PortId
)
1135 ChildDeviceObject
= HubDeviceExtension
->ChildDeviceObject
[Index
];
1141 /* Fail the request if the device doesn't exist */
1142 if (!ChildDeviceObject
)
1144 DPRINT1("Removal request for non-existant device!\n");
1145 return STATUS_UNSUCCESSFUL
;
1148 /* Remove the device from the table */
1149 HubDeviceExtension
->ChildDeviceObject
[Index
] = NULL
;
1151 /* Invalidate device relations for the root hub */
1152 IoInvalidateDeviceRelations(HubDeviceExtension
->RootHubPhysicalDeviceObject
, BusRelations
);
1154 /* The rest of the removal process takes place in IRP_MN_REMOVE_DEVICE handling for the PDO */
1155 return STATUS_SUCCESS
;
1159 CreateUsbChildDeviceObject(
1160 IN PDEVICE_OBJECT UsbHubDeviceObject
,
1162 OUT PDEVICE_OBJECT
*UsbChildDeviceObject
)
1165 PDEVICE_OBJECT RootHubDeviceObject
, NewChildDeviceObject
;
1166 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1167 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
;
1168 PUSB_BUS_INTERFACE_HUB_V5 HubInterface
;
1169 ULONG ChildDeviceCount
, UsbDeviceNumber
= 0;
1170 WCHAR CharDeviceName
[64];
1171 UNICODE_STRING DeviceName
;
1172 ULONG ConfigDescSize
, DeviceDescSize
;
1173 PVOID HubInterfaceBusContext
;
1174 USB_CONFIGURATION_DESCRIPTOR ConfigDesc
;
1176 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) UsbHubDeviceObject
->DeviceExtension
;
1177 HubInterface
= &HubDeviceExtension
->HubInterface
;
1178 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
1179 HubInterfaceBusContext
= HubDeviceExtension
->UsbDInterface
.BusContext
;
1181 // Find an empty slot in the child device array
1183 for (ChildDeviceCount
= 0; ChildDeviceCount
< USB_MAXCHILDREN
; ChildDeviceCount
++)
1185 if (HubDeviceExtension
->ChildDeviceObject
[ChildDeviceCount
] == NULL
)
1187 DPRINT1("Found unused entry at %d\n", ChildDeviceCount
);
1193 // Check if the limit has been reached for maximum usb devices
1195 if (ChildDeviceCount
== USB_MAXCHILDREN
)
1197 DPRINT1("USBHUB: Too many child devices!\n");
1198 return STATUS_UNSUCCESSFUL
;
1204 // Create a Device Name
1206 swprintf(CharDeviceName
, L
"\\Device\\USBPDO-%d", UsbDeviceNumber
);
1209 // Initialize UnicodeString
1211 RtlInitUnicodeString(&DeviceName
, CharDeviceName
);
1214 // Create a DeviceObject
1216 Status
= IoCreateDevice(UsbHubDeviceObject
->DriverObject
,
1217 sizeof(HUB_CHILDDEVICE_EXTENSION
),
1219 FILE_DEVICE_CONTROLLER
,
1220 FILE_AUTOGENERATED_DEVICE_NAME
,
1222 &NewChildDeviceObject
);
1225 // Check if the name is already in use
1227 if ((Status
== STATUS_OBJECT_NAME_EXISTS
) || (Status
== STATUS_OBJECT_NAME_COLLISION
))
1237 // Check for other errors
1239 if (!NT_SUCCESS(Status
))
1241 DPRINT1("USBHUB: IoCreateDevice failed with status %x\n", Status
);
1245 DPRINT1("USBHUB: Created Device %x\n", NewChildDeviceObject
);
1249 NewChildDeviceObject
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
1252 // Assign the device extensions
1254 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)NewChildDeviceObject
->DeviceExtension
;
1255 RtlZeroMemory(UsbChildExtension
, sizeof(HUB_CHILDDEVICE_EXTENSION
));
1256 UsbChildExtension
->ParentDeviceObject
= UsbHubDeviceObject
;
1257 UsbChildExtension
->PortNumber
= PortId
;
1260 // Create the UsbDeviceObject
1262 Status
= HubInterface
->CreateUsbDevice(HubInterfaceBusContext
,
1263 (PVOID
)&UsbChildExtension
->UsbDeviceHandle
,
1264 HubDeviceExtension
->RootHubHandle
,
1267 if (!NT_SUCCESS(Status
))
1269 DPRINT1("USBHUB: CreateUsbDevice failed with status %x\n", Status
);
1274 // Initialize UsbDevice
1276 Status
= HubInterface
->InitializeUsbDevice(HubInterfaceBusContext
, UsbChildExtension
->UsbDeviceHandle
);
1277 if (!NT_SUCCESS(Status
))
1279 DPRINT1("USBHUB: InitializeUsbDevice failed with status %x\n", Status
);
1283 DPRINT1("Usb Device Handle %x\n", UsbChildExtension
->UsbDeviceHandle
);
1285 ConfigDescSize
= sizeof(USB_CONFIGURATION_DESCRIPTOR
);
1286 DeviceDescSize
= sizeof(USB_DEVICE_DESCRIPTOR
);
1289 // Get the descriptors
1291 Status
= HubInterface
->GetUsbDescriptors(HubInterfaceBusContext
,
1292 UsbChildExtension
->UsbDeviceHandle
,
1293 (PUCHAR
)&UsbChildExtension
->DeviceDesc
,
1295 (PUCHAR
)&ConfigDesc
,
1297 if (!NT_SUCCESS(Status
))
1299 DPRINT1("USBHUB: GetUsbDescriptors failed with status %x\n", Status
);
1303 DumpDeviceDescriptor(&UsbChildExtension
->DeviceDesc
);
1304 DumpConfigurationDescriptor(&ConfigDesc
);
1307 // FIXME: Support more than one configuration and one interface?
1309 if (UsbChildExtension
->DeviceDesc
.bNumConfigurations
> 1)
1311 DPRINT1("Warning: Device has more than one configuration. Only one configuration (the first) is supported!\n");
1314 if (ConfigDesc
.bNumInterfaces
> 1)
1316 DPRINT1("Warning: Device has more that one interface. Only one interface (the first) is currently supported\n");
1319 ConfigDescSize
= ConfigDesc
.wTotalLength
;
1322 // Allocate memory for the first full descriptor, including interfaces and endpoints.
1324 UsbChildExtension
->FullConfigDesc
= ExAllocatePoolWithTag(PagedPool
, ConfigDescSize
, USB_HUB_TAG
);
1327 // Retrieve the full configuration descriptor
1329 Status
= GetUsbDeviceDescriptor(NewChildDeviceObject
,
1330 USB_CONFIGURATION_DESCRIPTOR_TYPE
,
1333 UsbChildExtension
->FullConfigDesc
,
1336 if (!NT_SUCCESS(Status
))
1338 DPRINT1("USBHUB: GetUsbDeviceDescriptor failed with status %x\n", Status
);
1342 DumpFullConfigurationDescriptor(UsbChildExtension
->FullConfigDesc
);
1345 // Construct all the strings that will described the device to PNP
1347 Status
= CreateDeviceIds(NewChildDeviceObject
);
1348 if (!NT_SUCCESS(Status
))
1350 DPRINT1("Failed to create strings needed to describe device to PNP.\n");
1354 HubDeviceExtension
->ChildDeviceObject
[ChildDeviceCount
] = NewChildDeviceObject
;
1356 IoInvalidateDeviceRelations(RootHubDeviceObject
, BusRelations
);
1357 return STATUS_SUCCESS
;
1362 // Remove the usb device if it was created
1364 if (UsbChildExtension
->UsbDeviceHandle
)
1365 HubInterface
->RemoveUsbDevice(HubInterfaceBusContext
, UsbChildExtension
->UsbDeviceHandle
, 0);
1368 // Free full configuration descriptor if one was allocated
1370 if (UsbChildExtension
->FullConfigDesc
)
1371 ExFreePool(UsbChildExtension
->FullConfigDesc
);
1374 // Delete the device object
1376 IoDeleteDevice(NewChildDeviceObject
);
1381 USBHUB_FdoQueryBusRelations(
1382 IN PDEVICE_OBJECT DeviceObject
,
1383 OUT PDEVICE_RELATIONS
* pDeviceRelations
)
1385 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1386 PDEVICE_RELATIONS DeviceRelations
;
1391 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1394 // Count the number of children
1396 for (i
= 0; i
< USB_MAXCHILDREN
; i
++)
1399 if (HubDeviceExtension
->ChildDeviceObject
[i
] == NULL
)
1406 NeededSize
= sizeof(DEVICE_RELATIONS
);
1408 NeededSize
+= (Children
- 1) * sizeof(PDEVICE_OBJECT
);
1411 // Allocate DeviceRelations
1413 DeviceRelations
= (PDEVICE_RELATIONS
)ExAllocatePool(PagedPool
,
1416 if (!DeviceRelations
)
1417 return STATUS_INSUFFICIENT_RESOURCES
;
1418 DeviceRelations
->Count
= Children
;
1422 // Fill in return structure
1424 for (i
= 0; i
< USB_MAXCHILDREN
; i
++)
1426 if (HubDeviceExtension
->ChildDeviceObject
[i
])
1428 ObReferenceObject(HubDeviceExtension
->ChildDeviceObject
[i
]);
1429 HubDeviceExtension
->ChildDeviceObject
[i
]->Flags
&= ~DO_DEVICE_INITIALIZING
;
1430 DeviceRelations
->Objects
[Children
++] = HubDeviceExtension
->ChildDeviceObject
[i
];
1434 ASSERT(Children
== DeviceRelations
->Count
);
1435 *pDeviceRelations
= DeviceRelations
;
1437 return STATUS_SUCCESS
;
1442 RootHubInitCallbackFunction(
1445 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Context
;
1447 DPRINT1("Sending the initial SCE Request %x\n", DeviceObject
);
1450 // Send the first SCE Request
1452 QueryStatusChangeEndpoint(DeviceObject
);
1456 USBHUB_FdoHandlePnp(
1457 IN PDEVICE_OBJECT DeviceObject
,
1460 PIO_STACK_LOCATION Stack
;
1461 NTSTATUS Status
= STATUS_SUCCESS
;
1462 ULONG_PTR Information
= 0;
1463 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
1464 PDEVICE_OBJECT RootHubDeviceObject
;
1465 PVOID HubInterfaceBusContext
, UsbDInterfaceBusContext
;
1467 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1469 Stack
= IoGetCurrentIrpStackLocation(Irp
);
1471 switch (Stack
->MinorFunction
)
1473 case IRP_MN_START_DEVICE
:
1476 PUSB_INTERFACE_DESCRIPTOR Pid
;
1477 ULONG Result
= 0, PortId
;
1478 USBD_INTERFACE_LIST_ENTRY InterfaceList
[2] = {{NULL
, NULL
}, {NULL
, NULL
}};
1479 PURB ConfigUrb
= NULL
;
1482 DPRINT1("IRP_MJ_PNP / IRP_MN_START_DEVICE\n");
1485 // Allocated size including the sizeof USBD_INTERFACE_LIST_ENTRY
1487 Urb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(URB
) + sizeof(USBD_INTERFACE_LIST_ENTRY
), USB_HUB_TAG
);
1488 RtlZeroMemory(Urb
, sizeof(URB
) + sizeof(USBD_INTERFACE_LIST_ENTRY
));
1491 // Get the Root Hub Pdo
1493 SubmitRequestToRootHub(HubDeviceExtension
->LowerDeviceObject
,
1494 IOCTL_INTERNAL_USB_GET_ROOTHUB_PDO
,
1495 &HubDeviceExtension
->RootHubPhysicalDeviceObject
,
1496 &HubDeviceExtension
->RootHubFunctionalDeviceObject
);
1498 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
1499 ASSERT(HubDeviceExtension
->RootHubPhysicalDeviceObject
);
1500 ASSERT(HubDeviceExtension
->RootHubFunctionalDeviceObject
);
1501 DPRINT1("RootPdo %x, RootFdo %x\n",
1502 HubDeviceExtension
->RootHubPhysicalDeviceObject
,
1503 HubDeviceExtension
->RootHubFunctionalDeviceObject
);
1506 // Send the StartDevice to RootHub
1508 Status
= ForwardIrpAndWait(RootHubDeviceObject
, Irp
);
1510 if (!NT_SUCCESS(Status
))
1512 DPRINT1("Failed to start the RootHub PDO\n");
1517 // Get the current number of hubs
1519 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1520 IOCTL_INTERNAL_USB_GET_HUB_COUNT
,
1521 &HubDeviceExtension
->NumberOfHubs
, NULL
);
1524 // Get the Hub Interface
1526 Status
= QueryInterface(RootHubDeviceObject
,
1527 USB_BUS_INTERFACE_HUB_GUID
,
1528 sizeof(USB_BUS_INTERFACE_HUB_V5
),
1530 (PVOID
)&HubDeviceExtension
->HubInterface
);
1532 if (!NT_SUCCESS(Status
))
1534 DPRINT1("Failed to get HUB_GUID interface with status 0x%08lx\n", Status
);
1535 return STATUS_UNSUCCESSFUL
;
1538 HubInterfaceBusContext
= HubDeviceExtension
->HubInterface
.BusContext
;
1541 // Get the USBDI Interface
1543 Status
= QueryInterface(RootHubDeviceObject
,
1544 USB_BUS_INTERFACE_USBDI_GUID
,
1545 sizeof(USB_BUS_INTERFACE_USBDI_V2
),
1547 (PVOID
)&HubDeviceExtension
->UsbDInterface
);
1549 if (!NT_SUCCESS(Status
))
1551 DPRINT1("Failed to get USBDI_GUID interface with status 0x%08lx\n", Status
);
1555 UsbDInterfaceBusContext
= HubDeviceExtension
->UsbDInterface
.BusContext
;
1558 // Get Root Hub Device Handle
1560 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1561 IOCTL_INTERNAL_USB_GET_DEVICE_HANDLE
,
1562 &HubDeviceExtension
->RootHubHandle
,
1565 if (!NT_SUCCESS(Status
))
1567 DPRINT1("GetRootHubDeviceHandle failed with status 0x%08lx\n", Status
);
1572 // Get Hub Device Information
1574 Status
= HubDeviceExtension
->HubInterface
.QueryDeviceInformation(HubInterfaceBusContext
,
1575 HubDeviceExtension
->RootHubHandle
,
1576 &HubDeviceExtension
->DeviceInformation
,
1577 sizeof(USB_DEVICE_INFORMATION_0
),
1580 DPRINT1("Status %x, Result 0x%08lx\n", Status
, Result
);
1581 DPRINT1("InformationLevel %x\n", HubDeviceExtension
->DeviceInformation
.InformationLevel
);
1582 DPRINT1("ActualLength %x\n", HubDeviceExtension
->DeviceInformation
.ActualLength
);
1583 DPRINT1("PortNumber %x\n", HubDeviceExtension
->DeviceInformation
.PortNumber
);
1584 DPRINT1("DeviceDescriptor %x\n", HubDeviceExtension
->DeviceInformation
.DeviceDescriptor
);
1585 DPRINT1("HubAddress %x\n", HubDeviceExtension
->DeviceInformation
.HubAddress
);
1586 DPRINT1("NumberofPipes %x\n", HubDeviceExtension
->DeviceInformation
.NumberOfOpenPipes
);
1589 // Get Root Hubs Device Descriptor
1591 UsbBuildGetDescriptorRequest(Urb
,
1592 sizeof(Urb
->UrbControlDescriptorRequest
),
1593 USB_DEVICE_DESCRIPTOR_TYPE
,
1596 &HubDeviceExtension
->HubDeviceDescriptor
,
1598 sizeof(USB_DEVICE_DESCRIPTOR
),
1601 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;//HubDeviceExtension->RootHubHandle;
1603 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1604 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1608 if (!NT_SUCCESS(Status
))
1610 DPRINT1("Failed to get HubDeviceDescriptor!\n");
1613 DumpDeviceDescriptor(&HubDeviceExtension
->HubDeviceDescriptor
);
1616 // Get Root Hubs Configuration Descriptor
1618 UsbBuildGetDescriptorRequest(Urb
,
1619 sizeof(Urb
->UrbControlDescriptorRequest
),
1620 USB_CONFIGURATION_DESCRIPTOR_TYPE
,
1623 &HubDeviceExtension
->HubConfigDescriptor
,
1625 sizeof(USB_CONFIGURATION_DESCRIPTOR
) + sizeof(USB_INTERFACE_DESCRIPTOR
) + sizeof(USB_ENDPOINT_DESCRIPTOR
),
1628 DPRINT1("RootHub Handle %x\n", HubDeviceExtension
->RootHubHandle
);
1629 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;//HubDeviceExtension->RootHubHandle;
1631 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1632 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1636 if (!NT_SUCCESS(Status
))
1638 DPRINT1("Failed to get RootHub Configuration with status %x\n", Status
);
1641 ASSERT(HubDeviceExtension
->HubConfigDescriptor
.wTotalLength
);
1643 DumpConfigurationDescriptor(&HubDeviceExtension
->HubConfigDescriptor
);
1645 Status
= HubDeviceExtension
->HubInterface
.GetExtendedHubInformation(HubInterfaceBusContext
,
1646 RootHubDeviceObject
,
1647 &HubDeviceExtension
->UsbExtHubInfo
,
1648 sizeof(USB_EXTHUB_INFORMATION_0
),
1650 if (!NT_SUCCESS(Status
))
1652 DPRINT1("Failed to extended hub information. Unable to determine the number of ports!\n");
1656 DPRINT1("HubDeviceExtension->UsbExtHubInfo.NumberOfPorts %x\n", HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
);
1659 // Get the Hub Descriptor
1661 UsbBuildVendorRequest(Urb
,
1662 URB_FUNCTION_CLASS_DEVICE
,
1663 sizeof(Urb
->UrbControlVendorClassRequest
),
1664 USBD_TRANSFER_DIRECTION_IN
| USBD_SHORT_TRANSFER_OK
,
1666 USB_REQUEST_GET_DESCRIPTOR
,
1667 USB_DEVICE_CLASS_RESERVED
,
1669 &HubDeviceExtension
->HubDescriptor
,
1671 sizeof(USB_HUB_DESCRIPTOR
),
1674 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;//HubDeviceExtension->RootHubHandle;
1676 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1677 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1681 DPRINT1("bDescriptorType %x\n", HubDeviceExtension
->HubDescriptor
.bDescriptorType
);
1683 if (!NT_SUCCESS(Status
))
1685 DPRINT1("Failed to get Hub Descriptor!\n");
1687 return STATUS_UNSUCCESSFUL
;
1691 UsbBuildGetStatusRequest(Urb
,
1692 URB_FUNCTION_GET_STATUS_FROM_DEVICE
,
1697 Urb
->UrbHeader
.UsbdDeviceHandle
= NULL
;//HubDeviceExtension->RootHubHandle;
1699 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1700 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1703 if (!NT_SUCCESS(Status
))
1705 DPRINT1("Failed to get Hub Status!\n");
1707 return STATUS_UNSUCCESSFUL
;
1710 DPRINT1("HubStatus %x\n", HubStatus
);
1713 // Allocate memory for PortStatusChange to hold 2 USHORTs for each port on hub
1715 HubDeviceExtension
->PortStatusChange
= ExAllocatePoolWithTag(NonPagedPool
,
1716 sizeof(ULONG
) * HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
,
1720 // Get the first Configuration Descriptor
1722 Pid
= USBD_ParseConfigurationDescriptorEx(&HubDeviceExtension
->HubConfigDescriptor
,
1723 &HubDeviceExtension
->HubConfigDescriptor
,
1724 -1, -1, -1, -1, -1);
1726 ASSERT(Pid
!= NULL
);
1728 InterfaceList
[0].InterfaceDescriptor
= Pid
;
1729 ConfigUrb
= USBD_CreateConfigurationRequestEx(&HubDeviceExtension
->HubConfigDescriptor
,
1730 (PUSBD_INTERFACE_LIST_ENTRY
)&InterfaceList
);
1731 ASSERT(ConfigUrb
!= NULL
);
1733 Status
= SubmitRequestToRootHub(RootHubDeviceObject
,
1734 IOCTL_INTERNAL_USB_SUBMIT_URB
,
1738 HubDeviceExtension
->ConfigurationHandle
= ConfigUrb
->UrbSelectConfiguration
.ConfigurationHandle
;
1739 HubDeviceExtension
->PipeHandle
= ConfigUrb
->UrbSelectConfiguration
.Interface
.Pipes
[0].PipeHandle
;
1740 DPRINT1("Configuration Handle %x\n", HubDeviceExtension
->ConfigurationHandle
);
1743 // check if function is available
1745 if (HubDeviceExtension
->UsbDInterface
.IsDeviceHighSpeed
)
1748 // is it high speed bus
1750 if (HubDeviceExtension
->UsbDInterface
.IsDeviceHighSpeed(HubInterfaceBusContext
))
1753 // initialize usb 2.0 hub
1755 Status
= HubDeviceExtension
->HubInterface
.Initialize20Hub(HubInterfaceBusContext
,
1756 HubDeviceExtension
->RootHubHandle
, 1);
1757 DPRINT1("Status %x\n", Status
);
1760 // FIXME handle error
1762 ASSERT(Status
== STATUS_SUCCESS
);
1766 ExFreePool(ConfigUrb
);
1769 // Enable power on all ports
1772 DPRINT1("Enabling PortPower on all ports!\n");
1774 for (PortId
= 1; PortId
<= HubDeviceExtension
->HubDescriptor
.bNumberOfPorts
; PortId
++)
1776 Status
= SetPortFeature(RootHubDeviceObject
, PortId
, PORT_POWER
);
1777 if (!NT_SUCCESS(Status
))
1778 DPRINT1("Failed to power on port %d\n", PortId
);
1780 Status
= ClearPortFeature(RootHubDeviceObject
, PortId
, C_PORT_CONNECTION
);
1781 if (!NT_SUCCESS(Status
))
1782 DPRINT1("Failed to power on port %d\n", PortId
);
1785 DPRINT1("RootHubInitNotification %x\n", HubDeviceExtension
->HubInterface
.RootHubInitNotification
);
1788 // init roo hub notification
1790 if (HubDeviceExtension
->HubInterface
.RootHubInitNotification
)
1792 Status
= HubDeviceExtension
->HubInterface
.RootHubInitNotification(HubInterfaceBusContext
,
1794 RootHubInitCallbackFunction
);
1795 if (!NT_SUCCESS(Status
))
1797 DPRINT1("Failed to set callback\n");
1803 // Send the first SCE Request
1805 QueryStatusChangeEndpoint(DeviceObject
);
1812 case IRP_MN_QUERY_DEVICE_RELATIONS
:
1814 switch (Stack
->Parameters
.QueryDeviceRelations
.Type
)
1818 PDEVICE_RELATIONS DeviceRelations
= NULL
;
1819 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / BusRelations\n");
1821 Status
= USBHUB_FdoQueryBusRelations(DeviceObject
, &DeviceRelations
);
1823 Information
= (ULONG_PTR
)DeviceRelations
;
1826 case RemovalRelations
:
1828 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / RemovalRelations\n");
1829 return ForwardIrpAndForget(DeviceObject
, Irp
);
1832 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / Unknown type 0x%lx\n",
1833 Stack
->Parameters
.QueryDeviceRelations
.Type
);
1834 return ForwardIrpAndForget(DeviceObject
, Irp
);
1838 case IRP_MN_QUERY_BUS_INFORMATION
:
1840 DPRINT1("IRP_MN_QUERY_BUS_INFORMATION\n");
1843 case IRP_MN_QUERY_ID
:
1845 DPRINT1("IRP_MN_QUERY_ID\n");
1848 case IRP_MN_QUERY_CAPABILITIES
:
1850 DPRINT1("IRP_MN_QUERY_CAPABILITIES\n");
1855 DPRINT1(" IRP_MJ_PNP / unknown minor function 0x%lx\n", Stack
->MinorFunction
);
1856 return ForwardIrpAndForget(DeviceObject
, Irp
);
1860 Irp
->IoStatus
.Information
= Information
;
1861 Irp
->IoStatus
.Status
= Status
;
1862 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1867 USBHUB_FdoHandleDeviceControl(
1868 IN PDEVICE_OBJECT DeviceObject
,
1871 DPRINT1("FdoHandleDeviceControl\n");
1873 return STATUS_NOT_IMPLEMENTED
;