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 * Hervé Poussineau (hpoussin@reactos.org)
8 * Michael Martin (michael.martin@reactos.org)
9 * Johannes Anderwald (johannes.anderwald@reactos.org)
14 #define IO_METHOD_FROM_CTL_CODE(ctlCode) (ctlCode&0x00000003)
19 PDEVICE_OBJECT DeviceObject
,
24 DPRINT("Entered Urb Completion\n");
27 // Get the original Irp
29 OriginalIrp
= (PIRP
)Context
;
32 // Update it to match what was returned for the IRP that was passed to RootHub
34 OriginalIrp
->IoStatus
.Status
= Irp
->IoStatus
.Status
;
35 OriginalIrp
->IoStatus
.Information
= Irp
->IoStatus
.Information
;
36 DPRINT("Status %x, Information %x\n", Irp
->IoStatus
.Status
, Irp
->IoStatus
.Information
);
39 // Complete the original Irp
41 IoCompleteRequest(OriginalIrp
, IO_NO_INCREMENT
);
44 // Return this status so the IO Manager doesnt mess with the Irp
46 return STATUS_MORE_PROCESSING_REQUIRED
;
51 PDEVICE_OBJECT RootHubDeviceObject
,
52 IN ULONG IoControlCode
,
54 OUT PVOID OutParameter1
,
55 OUT PVOID OutParameter2
)
59 IO_STATUS_BLOCK IoStatus
;
60 PIO_STACK_LOCATION ForwardStack
, CurrentStack
;
64 // Get the current stack location for the Irp
66 CurrentStack
= IoGetCurrentIrpStackLocation(Irp
);
70 // Pull the Urb from that stack, it will be reused in the Irp sent to RootHub
72 Urb
= (PURB
)CurrentStack
->Parameters
.Others
.Argument1
;
76 // Create the Irp to forward to RootHub
78 ForwardIrp
= IoBuildAsynchronousFsdRequest(IRP_MJ_SHUTDOWN
,
86 DPRINT1("Failed to allocate IRP\n");
87 return STATUS_INSUFFICIENT_RESOURCES
;
91 // Get the new Irps next stack
93 ForwardStack
= IoGetNextIrpStackLocation(ForwardIrp
);
96 // Copy the stack for the current irp into the next stack of new irp
98 RtlCopyMemory(ForwardStack
, CurrentStack
, sizeof(IO_STACK_LOCATION
));
100 IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
101 IoStatus
.Information
= 0;
104 // Mark the Irp from upper driver as pending
106 IoMarkIrpPending(Irp
);
109 // Now set the completion routine for the new Irp.
111 IoSetCompletionRoutine(ForwardIrp
,
118 Status
= IoCallDriver(RootHubDeviceObject
, ForwardIrp
);
121 // Always return pending as the completion routine will take care of it
123 return STATUS_PENDING
;
127 USBHUB_PdoHandleInternalDeviceControl(
128 IN PDEVICE_OBJECT DeviceObject
,
132 PIO_STACK_LOCATION Stack
;
133 ULONG_PTR Information
= 0;
134 PHUB_DEVICE_EXTENSION HubDeviceExtension
;
135 PHUB_CHILDDEVICE_EXTENSION ChildDeviceExtension
;
136 PDEVICE_OBJECT RootHubDeviceObject
;
139 //DPRINT1("UsbhubInternalDeviceControlPdo(%x) called\n", DeviceObject);
142 // get current stack location
144 Stack
= IoGetCurrentIrpStackLocation(Irp
);
148 // Set default status
150 Status
= Irp
->IoStatus
.Status
;
152 ChildDeviceExtension
= (PHUB_CHILDDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
153 ASSERT(ChildDeviceExtension
->Common
.IsFDO
== FALSE
);
154 HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)ChildDeviceExtension
->ParentDeviceObject
->DeviceExtension
;
155 RootHubDeviceObject
= HubDeviceExtension
->RootHubPhysicalDeviceObject
;
157 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
159 case IOCTL_INTERNAL_USB_GET_PARENT_HUB_INFO
:
161 PHUB_DEVICE_EXTENSION DeviceExtension
;
163 DPRINT1("IOCTL_INTERNAL_USB_GET_PARENT_HUB_INFO\n");
164 if (Irp
->AssociatedIrp
.SystemBuffer
== NULL
165 || Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
!= sizeof(PVOID
))
167 Status
= STATUS_INVALID_PARAMETER
;
172 DeviceExtension
= (PHUB_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
174 pHubPointer
= (PVOID
*)Irp
->AssociatedIrp
.SystemBuffer
;
177 Information
= sizeof(PVOID
);
178 Status
= STATUS_SUCCESS
;
182 case IOCTL_INTERNAL_USB_SUBMIT_URB
:
184 //DPRINT1("IOCTL_INTERNAL_USB_SUBMIT_URB\n");
189 Urb
= (PURB
)Stack
->Parameters
.Others
.Argument1
;
193 // Set the real device handle
195 //DPRINT("UsbdDeviceHandle %x, ChildDeviceHandle %x\n", Urb->UrbHeader.UsbdDeviceHandle, ChildDeviceExtension->UsbDeviceHandle);
197 Urb
->UrbHeader
.UsbdDeviceHandle
= ChildDeviceExtension
->UsbDeviceHandle
;
202 switch (Urb
->UrbHeader
.Function
)
207 case URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE
:
208 DPRINT1("URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE\n");
210 case URB_FUNCTION_CLASS_DEVICE
:
211 DPRINT1("URB_FUNCTION_CLASS_DEVICE\n");
213 case URB_FUNCTION_GET_STATUS_FROM_DEVICE
:
214 DPRINT1("URB_FUNCTION_GET_STATUS_FROM_DEVICE\n");
216 case URB_FUNCTION_SELECT_CONFIGURATION
:
217 DPRINT1("URB_FUNCTION_SELECT_CONFIGURATION\n");
219 case URB_FUNCTION_SELECT_INTERFACE
:
220 DPRINT1("URB_FUNCTION_SELECT_INTERFACE\n");
222 case URB_FUNCTION_CLASS_OTHER
:
223 DPRINT1("URB_FUNCTION_CLASS_OTHER\n");
225 case URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER
:
228 DPRINT1("URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER\n");
229 DPRINT1("PipeHandle %x\n", Urb->UrbBulkOrInterruptTransfer.PipeHandle);
230 DPRINT1("TransferFlags %x\n", Urb->UrbBulkOrInterruptTransfer.TransferFlags);
231 DPRINT1("Buffer %x\n", Urb->UrbBulkOrInterruptTransfer.TransferBuffer);
232 DPRINT1("BufferMDL %x\n", Urb->UrbBulkOrInterruptTransfer.TransferBufferMDL);
233 DPRINT1("Length %x\n", Urb->UrbBulkOrInterruptTransfer.TransferBufferLength);
234 DPRINT1("UrbLink %x\n", Urb->UrbBulkOrInterruptTransfer.UrbLink);
235 DPRINT1("hca %x\n", Urb->UrbBulkOrInterruptTransfer.hca);
236 if (Urb->UrbBulkOrInterruptTransfer.TransferFlags == USBD_SHORT_TRANSFER_OK)
243 case URB_FUNCTION_CLASS_INTERFACE
:
244 DPRINT1("URB_FUNCTION_CLASS_INTERFACE\n");
247 DPRINT1("IOCTL_INTERNAL_USB_SUBMIT_URB Function %x NOT IMPLEMENTED\n", Urb
->UrbHeader
.Function
);
250 Urb
->UrbHeader
.UsbdDeviceHandle
= ChildDeviceExtension
->UsbDeviceHandle
;
251 //DPRINT1("Stack->CompletionRoutine %x\n", Stack->CompletionRoutine);
253 // Send the request to RootHub
255 Status
= FowardUrbToRootHub(RootHubDeviceObject
, IOCTL_INTERNAL_USB_SUBMIT_URB
, Irp
, Urb
, NULL
);
260 // FIXME: Can these be sent to RootHub?
262 case IOCTL_INTERNAL_USB_RESET_PORT
:
263 DPRINT1("IOCTL_INTERNAL_USB_RESET_PORT\n");
265 case IOCTL_INTERNAL_USB_GET_PORT_STATUS
:
267 PORT_STATUS_CHANGE PortStatus
;
269 PUCHAR PortStatusBits
;
271 PortStatusBits
= (PUCHAR
)Stack
->Parameters
.Others
.Argument1
;
273 // USBD_PORT_ENABLED (bit 0) or USBD_PORT_CONNECTED (bit 1)
275 DPRINT1("IOCTL_INTERNAL_USB_GET_PORT_STATUS\n");
276 DPRINT1("Arg1 %x\n", *PortStatusBits
);
278 if (Stack
->Parameters
.Others
.Argument1
)
280 for (PortId
= 1; PortId
<= HubDeviceExtension
->UsbExtHubInfo
.NumberOfPorts
; PortId
++)
282 Status
= GetPortStatusAndChange(RootHubDeviceObject
, PortId
, &PortStatus
);
283 if (NT_SUCCESS(Status
))
285 DPRINT1("Connect %x\n", ((PortStatus
.Status
& USB_PORT_STATUS_CONNECT
) << 1) << ((PortId
- 1) * 2));
286 DPRINT1("Enable %x\n", ((PortStatus
.Status
& USB_PORT_STATUS_ENABLE
) >> 1) << ((PortId
- 1) * 2));
288 (((PortStatus
.Status
& USB_PORT_STATUS_CONNECT
) << 1) << ((PortId
- 1) * 2)) +
289 (((PortStatus
.Status
& USB_PORT_STATUS_ENABLE
) >> 1) << ((PortId
- 1) * 2));
295 DPRINT1("Arg1 %x\n", *PortStatusBits
);
296 Status
= STATUS_SUCCESS
;
299 case IOCTL_INTERNAL_USB_ENABLE_PORT
:
300 DPRINT1("IOCTL_INTERNAL_USB_ENABLE_PORT\n");
302 case IOCTL_INTERNAL_USB_CYCLE_PORT
:
303 DPRINT1("IOCTL_INTERNAL_USB_CYCLE_PORT\n");
305 case IOCTL_INTERNAL_USB_GET_DEVICE_HANDLE
:
306 DPRINT1("IOCTL_INTERNAL_USB_GET_DEVICE_HANDLE\n");
310 DPRINT1("Unknown IOCTL code 0x%lx\n", Stack
->Parameters
.DeviceIoControl
.IoControlCode
);
311 Information
= Irp
->IoStatus
.Information
;
312 Status
= Irp
->IoStatus
.Status
;
316 if (Status
!= STATUS_PENDING
)
318 Irp
->IoStatus
.Information
= Information
;
319 Irp
->IoStatus
.Status
= Status
;
320 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
326 USBHUB_PdoStartDevice(
327 IN PDEVICE_OBJECT DeviceObject
,
330 PHUB_CHILDDEVICE_EXTENSION ChildDeviceExtension
;
332 DPRINT1("USBHUB_PdoStartDevice %x\n", DeviceObject
);
333 ChildDeviceExtension
= (PHUB_CHILDDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
336 // This should be a PDO
338 ASSERT(ChildDeviceExtension
->Common
.IsFDO
== FALSE
);
341 // FIXME: Fow now assume success
345 return STATUS_SUCCESS
;
350 IN PDEVICE_OBJECT DeviceObject
,
352 OUT ULONG_PTR
* Information
)
354 PHUB_CHILDDEVICE_EXTENSION ChildDeviceExtension
;
356 PUNICODE_STRING SourceString
= NULL
;
357 PWCHAR ReturnString
= NULL
;
358 NTSTATUS Status
= STATUS_SUCCESS
;
360 IdType
= IoGetCurrentIrpStackLocation(Irp
)->Parameters
.QueryId
.IdType
;
361 ChildDeviceExtension
= (PHUB_CHILDDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
365 case BusQueryDeviceID
:
367 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryDeviceID\n");
368 SourceString
= &ChildDeviceExtension
->usDeviceId
;
371 case BusQueryHardwareIDs
:
373 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryHardwareIDs\n");
374 SourceString
= &ChildDeviceExtension
->usHardwareIds
;
377 case BusQueryCompatibleIDs
:
379 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryCompatibleIDs\n");
380 SourceString
= &ChildDeviceExtension
->usCompatibleIds
;
383 case BusQueryInstanceID
:
385 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryInstanceID\n");
386 SourceString
= &ChildDeviceExtension
->usInstanceId
;
390 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_ID / unknown query id type 0x%lx\n", IdType
);
391 return STATUS_NOT_SUPPORTED
;
396 ReturnString
= ExAllocatePool(PagedPool
, SourceString
->Length
);
397 RtlCopyMemory(ReturnString
, SourceString
->Buffer
, SourceString
->Length
);
398 DPRINT1("%S\n", ReturnString
);
401 *Information
= (ULONG_PTR
)ReturnString
;
407 USBHUB_PdoQueryDeviceText(
408 IN PDEVICE_OBJECT DeviceObject
,
410 OUT ULONG_PTR
* Information
)
412 PHUB_CHILDDEVICE_EXTENSION ChildDeviceExtension
;
413 DEVICE_TEXT_TYPE DeviceTextType
;
414 PUNICODE_STRING SourceString
= NULL
;
415 PWCHAR ReturnString
= NULL
;
416 NTSTATUS Status
= STATUS_SUCCESS
;
419 DeviceTextType
= IoGetCurrentIrpStackLocation(Irp
)->Parameters
.QueryDeviceText
.DeviceTextType
;
420 LocaleId
= IoGetCurrentIrpStackLocation(Irp
)->Parameters
.QueryDeviceText
.LocaleId
;
421 ChildDeviceExtension
= (PHUB_CHILDDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
427 switch (DeviceTextType
)
429 case DeviceTextDescription
:
430 case DeviceTextLocationInformation
:
432 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_TEXT / DeviceTextDescription\n");
435 // does the device provide a text description
437 if (ChildDeviceExtension
->usTextDescription
.Buffer
&& ChildDeviceExtension
->usTextDescription
.Length
)
442 SourceString
= &ChildDeviceExtension
->usTextDescription
;
448 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_TEXT / unknown device text type 0x%lx\n", DeviceTextType
);
449 Status
= STATUS_NOT_SUPPORTED
;
456 ReturnString
= ExAllocatePool(PagedPool
, SourceString
->Length
);
457 RtlCopyMemory(ReturnString
, SourceString
->Buffer
, SourceString
->Length
);
458 DPRINT1("%S\n", ReturnString
);
459 *Information
= (ULONG_PTR
)ReturnString
;
467 IN PDEVICE_OBJECT DeviceObject
,
472 PIO_STACK_LOCATION Stack
;
473 ULONG_PTR Information
= 0;
474 PHUB_CHILDDEVICE_EXTENSION UsbChildExtension
;
476 UsbChildExtension
= (PHUB_CHILDDEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
477 Stack
= IoGetCurrentIrpStackLocation(Irp
);
478 MinorFunction
= Stack
->MinorFunction
;
480 switch (MinorFunction
)
482 case IRP_MN_START_DEVICE
:
484 DPRINT1("IRP_MJ_PNP / IRP_MN_START_DEVICE\n");
485 Status
= USBHUB_PdoStartDevice(DeviceObject
, Irp
);
488 case IRP_MN_QUERY_CAPABILITIES
:
490 PDEVICE_CAPABILITIES DeviceCapabilities
;
492 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_CAPABILITIES\n");
494 DeviceCapabilities
= (PDEVICE_CAPABILITIES
)Stack
->Parameters
.DeviceCapabilities
.Capabilities
;
495 // FIXME: capabilities can change with connected device
496 DeviceCapabilities
->LockSupported
= FALSE
;
497 DeviceCapabilities
->EjectSupported
= FALSE
;
498 DeviceCapabilities
->Removable
= TRUE
;
499 DeviceCapabilities
->DockDevice
= FALSE
;
500 DeviceCapabilities
->UniqueID
= FALSE
;
501 DeviceCapabilities
->SilentInstall
= FALSE
;
502 DeviceCapabilities
->RawDeviceOK
= FALSE
;
503 DeviceCapabilities
->SurpriseRemovalOK
= FALSE
;
504 DeviceCapabilities
->HardwareDisabled
= FALSE
;
505 //DeviceCapabilities->NoDisplayInUI = FALSE;
506 DeviceCapabilities
->Address
= UsbChildExtension
->PortNumber
;
507 DeviceCapabilities
->UINumber
= 0;
508 DeviceCapabilities
->DeviceState
[0] = PowerDeviceD0
;
509 for (i
= 1; i
< PowerSystemMaximum
; i
++)
510 DeviceCapabilities
->DeviceState
[i
] = PowerDeviceD3
;
511 //DeviceCapabilities->DeviceWake = PowerDeviceUndefined;
512 DeviceCapabilities
->D1Latency
= 0;
513 DeviceCapabilities
->D2Latency
= 0;
514 DeviceCapabilities
->D3Latency
= 0;
515 Status
= STATUS_SUCCESS
;
518 case IRP_MN_QUERY_RESOURCES
:
520 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_RESOURCES\n");
522 Information
= Irp
->IoStatus
.Information
;
523 Status
= Irp
->IoStatus
.Status
;
526 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS
:
528 DPRINT1("IRP_MJ_PNP / IRP_MN_QUERY_RESOURCE_REQUIREMENTS\n");
530 Information
= Irp
->IoStatus
.Information
;
531 Status
= Irp
->IoStatus
.Status
;
534 case IRP_MN_QUERY_DEVICE_TEXT
:
536 Status
= USBHUB_PdoQueryDeviceText(DeviceObject
, Irp
, &Information
);
539 case IRP_MN_QUERY_ID
:
541 Status
= USBHUB_PdoQueryId(DeviceObject
, Irp
, &Information
);
544 case IRP_MN_QUERY_BUS_INFORMATION
:
546 PPNP_BUS_INFORMATION BusInfo
;
547 BusInfo
= (PPNP_BUS_INFORMATION
)ExAllocatePool(PagedPool
, sizeof(PNP_BUS_INFORMATION
));
548 RtlCopyMemory(&BusInfo
->BusTypeGuid
,
550 sizeof(BusInfo
->BusTypeGuid
));
551 BusInfo
->LegacyBusType
= PNPBus
;
553 BusInfo
->BusNumber
= 0;
554 Information
= (ULONG_PTR
)BusInfo
;
555 Status
= STATUS_SUCCESS
;
558 case IRP_MN_REMOVE_DEVICE
:
560 PHUB_DEVICE_EXTENSION HubDeviceExtension
= (PHUB_DEVICE_EXTENSION
)UsbChildExtension
->ParentDeviceObject
->DeviceExtension
;
561 PUSB_BUS_INTERFACE_HUB_V5 HubInterface
= &HubDeviceExtension
->HubInterface
;
563 DPRINT1("IRP_MJ_PNP / IRP_MN_REMOVE_DEVICE\n");
565 /* Remove the device */
566 HubInterface
->RemoveUsbDevice(HubDeviceExtension
->UsbDInterface
.BusContext
, UsbChildExtension
->UsbDeviceHandle
, 0);
568 /* Complete the IRP */
569 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
570 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
572 /* Delete the device object */
573 IoDeleteDevice(DeviceObject
);
574 return STATUS_SUCCESS
;
578 DPRINT1("PDO IRP_MJ_PNP / unknown minor function 0x%lx\n", MinorFunction
);
579 Information
= Irp
->IoStatus
.Information
;
580 Status
= Irp
->IoStatus
.Status
;
584 Irp
->IoStatus
.Information
= Information
;
585 Irp
->IoStatus
.Status
= Status
;
586 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);