a6dd9cc656ceab45ff5265d52fd1b86cd1ea2326
[reactos.git] / drivers / usb / usbhub_new / pnp.c
1 /*
2 * PROJECT: ReactOS USB Hub Driver
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: USBHub plug and play functions
5 * COPYRIGHT: Copyright 2017 Vadim Galyant <vgal@rambler.ru>
6 */
7
8 #include "usbhub.h"
9
10 #define NDEBUG
11 #include <debug.h>
12
13 #define NDEBUG_USBHUB_PNP
14 #define NDEBUG_USBHUB_ENUM
15 #include "dbg_uhub.h"
16
17 NTSTATUS
18 NTAPI
19 USBH_IrpCompletion(IN PDEVICE_OBJECT DeviceObject,
20 IN PIRP Irp,
21 IN PVOID Context)
22 {
23 PRKEVENT Event;
24
25 DPRINT("USBH_IrpCompletion: Irp - %p\n", Irp);
26
27 Event = Context;
28 KeSetEvent(Event, EVENT_INCREMENT, FALSE);
29 return STATUS_MORE_PROCESSING_REQUIRED;
30 }
31
32 NTSTATUS
33 NTAPI
34 USBH_HubPnPIrpComplete(IN PDEVICE_OBJECT DeviceObject,
35 IN PIRP Irp,
36 IN PVOID Context)
37 {
38 PUSBHUB_FDO_EXTENSION HubExtension;
39
40 DPRINT("USBH_HubPnPIrpComplete: Irp - %p\n", Irp);
41
42 HubExtension = Context;
43
44 if (!NT_SUCCESS(Irp->IoStatus.Status))
45 {
46 DPRINT1("USBH_HubPnPIrpComplete: Irp failed - %lX\n", Irp->IoStatus.Status);
47 HubExtension->HubFlags |= USBHUB_FDO_FLAG_DEVICE_FAILED;
48 }
49
50 Irp->IoStatus.Status = STATUS_MORE_PROCESSING_REQUIRED;
51
52 KeSetEvent(&HubExtension->LowerDeviceEvent, EVENT_INCREMENT, FALSE);
53
54 return STATUS_MORE_PROCESSING_REQUIRED;
55 }
56
57 NTSTATUS
58 NTAPI
59 USBH_QueryCapsComplete(IN PDEVICE_OBJECT DeviceObject,
60 IN PIRP Irp,
61 IN PVOID Context)
62 {
63 PIO_STACK_LOCATION IoStack;
64 PDEVICE_CAPABILITIES Capabilities;
65
66 DPRINT("USBH_QueryCapsComplete: ... \n");
67
68 ASSERT(NT_SUCCESS(Irp->IoStatus.Status));
69
70 if (Irp->PendingReturned)
71 {
72 IoMarkIrpPending(Irp);
73 }
74
75 IoStack= IoGetCurrentIrpStackLocation(Irp);
76 Capabilities = IoStack->Parameters.DeviceCapabilities.Capabilities;
77
78 Capabilities->SurpriseRemovalOK = 1;
79
80 return STATUS_CONTINUE_COMPLETION;
81 }
82
83 NTSTATUS
84 NTAPI
85 USBHUB_GetBusInterface(IN PDEVICE_OBJECT DeviceObject,
86 OUT PUSB_BUS_INTERFACE_HUB_V5 BusInterface)
87 {
88 PIRP Irp;
89 NTSTATUS Status;
90 PIO_STACK_LOCATION IoStack;
91 KEVENT Event;
92
93 DPRINT("USBHUB_GetBusInterface: ... \n");
94
95 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
96
97 if (!Irp)
98 {
99 DPRINT1("USBHUB_GetBusInterface: IoAllocateIrp() failed\n");
100 return STATUS_INSUFFICIENT_RESOURCES;
101 }
102
103 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
104
105 KeInitializeEvent(&Event, NotificationEvent, FALSE);
106
107 IoSetCompletionRoutine(Irp,
108 USBH_IrpCompletion,
109 &Event,
110 TRUE,
111 TRUE,
112 TRUE);
113
114 IoStack = IoGetNextIrpStackLocation(Irp);
115
116 IoStack->MajorFunction = IRP_MJ_PNP;
117 IoStack->MinorFunction = IRP_MN_QUERY_INTERFACE;
118
119 IoStack->Parameters.QueryInterface.InterfaceType = &USB_BUS_INTERFACE_HUB_GUID;
120 IoStack->Parameters.QueryInterface.Size = sizeof(USB_BUS_INTERFACE_HUB_V5);
121 IoStack->Parameters.QueryInterface.Version = USB_BUSIF_HUB_VERSION_5;
122 IoStack->Parameters.QueryInterface.Interface = (PINTERFACE)BusInterface;
123 IoStack->Parameters.QueryInterface.InterfaceSpecificData = DeviceObject;
124
125 Status = IoCallDriver(DeviceObject, Irp);
126
127 if (Status == STATUS_PENDING)
128 {
129 KeWaitForSingleObject(&Event,
130 Suspended,
131 KernelMode,
132 FALSE,
133 NULL);
134
135 Status = Irp->IoStatus.Status;
136 }
137
138 IoFreeIrp(Irp);
139
140 return Status;
141 }
142
143 NTSTATUS
144 NTAPI
145 USBHUB_GetBusInterfaceUSBDI(IN PDEVICE_OBJECT DeviceObject,
146 OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterfaceUSBDI)
147 {
148 PIRP Irp;
149 NTSTATUS Status;
150 PIO_STACK_LOCATION IoStack;
151 KEVENT Event;
152
153 DPRINT("USBHUB_GetBusInterfaceUSBDI: ... \n");
154
155 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
156
157 if (!Irp)
158 {
159 DPRINT1("USBHUB_GetBusInterfaceUSBDI: IoAllocateIrp() failed\n");
160 return STATUS_INSUFFICIENT_RESOURCES;
161 }
162
163 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
164
165 KeInitializeEvent(&Event, NotificationEvent, FALSE);
166
167 IoSetCompletionRoutine(Irp,
168 USBH_IrpCompletion,
169 &Event,
170 TRUE,
171 TRUE,
172 TRUE);
173
174 IoStack = IoGetNextIrpStackLocation(Irp);
175
176 IoStack->MajorFunction = IRP_MJ_PNP;
177 IoStack->MinorFunction = IRP_MN_QUERY_INTERFACE;
178
179 IoStack->Parameters.QueryInterface.InterfaceType = &USB_BUS_INTERFACE_USBDI_GUID;
180 IoStack->Parameters.QueryInterface.Size = sizeof(USB_BUS_INTERFACE_USBDI_V2);
181 IoStack->Parameters.QueryInterface.Version = USB_BUSIF_USBDI_VERSION_2;
182 IoStack->Parameters.QueryInterface.Interface = (PINTERFACE)BusInterfaceUSBDI;
183 IoStack->Parameters.QueryInterface.InterfaceSpecificData = NULL;
184
185 Status = IoCallDriver(DeviceObject, Irp);
186
187 if (Status == STATUS_PENDING)
188 {
189 KeWaitForSingleObject(&Event,
190 Suspended,
191 KernelMode,
192 FALSE,
193 NULL);
194
195 Status = Irp->IoStatus.Status;
196 }
197
198 IoFreeIrp(Irp);
199
200 return Status;
201 }
202
203 VOID
204 NTAPI
205 USBH_QueryCapabilities(IN PDEVICE_OBJECT DeviceObject,
206 IN PDEVICE_CAPABILITIES DeviceCapabilities)
207 {
208 PIRP Irp;
209 PIO_STACK_LOCATION IoStack;
210 KEVENT Event;
211
212 DPRINT("USBH_QueryCapabilities: ... \n");
213
214 RtlZeroMemory(DeviceCapabilities, sizeof(DEVICE_CAPABILITIES));
215
216 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
217
218 if (!Irp)
219 {
220 DPRINT1("USBH_QueryCapabilities: IoAllocateIrp() failed\n");
221 return;
222 }
223
224 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
225
226 KeInitializeEvent(&Event, NotificationEvent, FALSE);
227
228 IoSetCompletionRoutine(Irp,
229 USBH_IrpCompletion,
230 &Event,
231 TRUE,
232 TRUE,
233 TRUE);
234
235 IoStack = IoGetNextIrpStackLocation(Irp);
236
237 IoStack->MajorFunction = IRP_MJ_PNP;
238 IoStack->MinorFunction = IRP_MN_QUERY_CAPABILITIES;
239
240 IoStack->Parameters.DeviceCapabilities.Capabilities = DeviceCapabilities;
241 IoStack->Parameters.DeviceCapabilities.Capabilities->Size = sizeof(DEVICE_CAPABILITIES);
242 IoStack->Parameters.DeviceCapabilities.Capabilities->Version = 1;
243 IoStack->Parameters.DeviceCapabilities.Capabilities->Address = MAXULONG;
244 IoStack->Parameters.DeviceCapabilities.Capabilities->UINumber = MAXULONG;
245
246 if (IoCallDriver(DeviceObject, Irp) == STATUS_PENDING)
247 {
248 KeWaitForSingleObject(&Event,
249 Suspended,
250 KernelMode,
251 FALSE,
252 NULL);
253 }
254
255 IoFreeIrp(Irp);
256 }
257
258 NTSTATUS
259 NTAPI
260 USBH_OpenConfiguration(IN PUSBHUB_FDO_EXTENSION HubExtension)
261 {
262 PUSB_INTERFACE_DESCRIPTOR Pid;
263 PURB Urb;
264 NTSTATUS Status;
265 USBD_INTERFACE_LIST_ENTRY InterfaceList[2] = {{NULL, NULL}, {NULL, NULL}};
266
267 DPRINT("USBH_OpenConfiguration ... \n");
268
269 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_USB20_HUB &&
270 HubExtension->LowerPDO != HubExtension->RootHubPdo)
271 {
272 Pid = USBD_ParseConfigurationDescriptorEx(HubExtension->HubConfigDescriptor,
273 HubExtension->HubConfigDescriptor,
274 -1,
275 -1,
276 USB_DEVICE_CLASS_HUB,
277 -1,
278 2);
279
280 if (Pid)
281 {
282 HubExtension->HubFlags |= USBHUB_FDO_FLAG_MULTIPLE_TTS;
283 }
284 else
285 {
286 Pid = USBD_ParseConfigurationDescriptorEx(HubExtension->HubConfigDescriptor,
287 HubExtension->HubConfigDescriptor,
288 -1,
289 -1,
290 USB_DEVICE_CLASS_HUB,
291 -1,
292 1);
293
294 if (Pid)
295 {
296 goto Next;
297 }
298
299 Pid = USBD_ParseConfigurationDescriptorEx(HubExtension->HubConfigDescriptor,
300 HubExtension->HubConfigDescriptor,
301 -1,
302 -1,
303 USB_DEVICE_CLASS_HUB,
304 -1,
305 0);
306 }
307 }
308 else
309 {
310 Pid = USBD_ParseConfigurationDescriptorEx(HubExtension->HubConfigDescriptor,
311 HubExtension->HubConfigDescriptor,
312 -1,
313 -1,
314 USB_DEVICE_CLASS_HUB,
315 -1,
316 -1);
317 }
318
319 if (!Pid)
320 {
321 return STATUS_UNSUCCESSFUL;
322 }
323
324 Next:
325
326 if (Pid->bInterfaceClass != USB_DEVICE_CLASS_HUB)
327 {
328 return STATUS_UNSUCCESSFUL;
329 }
330
331 InterfaceList[0].InterfaceDescriptor = Pid;
332
333 Urb = USBD_CreateConfigurationRequestEx(HubExtension->HubConfigDescriptor,
334 InterfaceList);
335
336 if (!Urb)
337 {
338 return STATUS_INSUFFICIENT_RESOURCES;
339 }
340
341 Status = USBH_FdoSyncSubmitUrb(HubExtension->Common.SelfDevice, Urb);
342
343 if (NT_SUCCESS(Status))
344 {
345 RtlCopyMemory(&HubExtension->PipeInfo,
346 InterfaceList[0].Interface->Pipes,
347 sizeof(USBD_PIPE_INFORMATION));
348
349 HubExtension->ConfigHandle = Urb->UrbSelectConfiguration.ConfigurationHandle;
350 }
351
352 ExFreePool(Urb);
353
354 return Status;
355 }
356
357 NTSTATUS
358 NTAPI
359 USBD_Initialize20Hub(IN PUSBHUB_FDO_EXTENSION HubExtension)
360 {
361 PUSB_BUSIFFN_INITIALIZE_20HUB Initialize20Hub;
362 ULONG TtCount;
363 PUSB_DEVICE_HANDLE DeviceHandle;
364
365 DPRINT("USBD_InitUsb2Hub ... \n");
366
367 Initialize20Hub = HubExtension->BusInterface.Initialize20Hub;
368
369 if (!Initialize20Hub)
370 {
371 return STATUS_NOT_IMPLEMENTED;
372 }
373
374 TtCount = 1;
375
376 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_MULTIPLE_TTS)
377 {
378 TtCount = HubExtension->HubDescriptor->bNumberOfPorts;
379 }
380
381 DeviceHandle = USBH_SyncGetDeviceHandle(HubExtension->LowerDevice);
382
383 return Initialize20Hub(HubExtension->BusInterface.BusContext,
384 DeviceHandle,
385 TtCount);
386 }
387
388 NTSTATUS
389 NTAPI
390 USBH_AbortInterruptPipe(IN PUSBHUB_FDO_EXTENSION HubExtension)
391 {
392 struct _URB_PIPE_REQUEST * Urb;
393 NTSTATUS Status;
394
395 DPRINT("USBH_AbortInterruptPipe: HubExtension - %p\n", HubExtension);
396
397 Urb = ExAllocatePoolWithTag(NonPagedPool,
398 sizeof(struct _URB_PIPE_REQUEST),
399 USB_HUB_TAG);
400
401 if (!Urb)
402 {
403 return STATUS_INSUFFICIENT_RESOURCES;
404 }
405
406 RtlZeroMemory(Urb, sizeof(struct _URB_PIPE_REQUEST));
407
408 Urb->Hdr.Length = sizeof(struct _URB_PIPE_REQUEST);
409 Urb->Hdr.Function = URB_FUNCTION_ABORT_PIPE;
410 Urb->PipeHandle = HubExtension->PipeInfo.PipeHandle;
411
412 Status = USBH_FdoSyncSubmitUrb(HubExtension->Common.SelfDevice,
413 (PURB)Urb);
414
415 if (NT_SUCCESS(Status))
416 {
417 KeWaitForSingleObject(&HubExtension->StatusChangeEvent,
418 Suspended,
419 KernelMode,
420 FALSE,
421 NULL);
422 }
423
424 ExFreePoolWithTag(Urb, USB_HUB_TAG);
425
426 return Status;
427 }
428
429 VOID
430 NTAPI
431 USBH_FdoCleanup(IN PUSBHUB_FDO_EXTENSION HubExtension)
432 {
433 PIRP IdleIrp = NULL;
434 PIRP WakeIrp = NULL;
435 PUSBHUB_PORT_DATA PortData;
436 PUSBHUB_PORT_PDO_EXTENSION PortExtension;
437 PIRP PortIdleIrp = NULL;
438 PIRP PortWakeIrp = NULL;
439 PVOID DeviceHandle;
440 NTSTATUS Status;
441 USHORT Port;
442 UCHAR NumberPorts;
443 KIRQL Irql;
444
445 DPRINT("USBH_FdoCleanup: HubExtension - %p\n", HubExtension);
446
447 USBD_UnRegisterRootHubCallBack(HubExtension);
448
449 HubExtension->HubFlags |= USBHUB_FDO_FLAG_DEVICE_STOPPING;
450
451 if (HubExtension->ResetRequestCount)
452 {
453 IoCancelIrp(HubExtension->ResetPortIrp);
454
455 KeWaitForSingleObject(&HubExtension->IdleEvent,
456 Suspended,
457 KernelMode,
458 FALSE,
459 NULL);
460 }
461
462 IoFreeIrp(HubExtension->ResetPortIrp);
463
464 HubExtension->ResetPortIrp = NULL;
465
466 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_WAIT_IDLE_REQUEST)
467 {
468 KeWaitForSingleObject(&HubExtension->IdleEvent,
469 Suspended,
470 KernelMode,
471 FALSE,
472 NULL);
473 }
474
475 IoAcquireCancelSpinLock(&Irql);
476
477 if (HubExtension->PendingWakeIrp)
478 {
479 WakeIrp = HubExtension->PendingWakeIrp;
480 HubExtension->PendingWakeIrp = NULL;
481 }
482
483 if (HubExtension->PendingIdleIrp)
484 {
485 IdleIrp = HubExtension->PendingIdleIrp;
486 HubExtension->PendingIdleIrp = NULL;
487 }
488
489 IoReleaseCancelSpinLock(Irql);
490
491 if (WakeIrp)
492 {
493 USBH_HubCancelWakeIrp(HubExtension, WakeIrp);
494 }
495
496 USBH_HubCompletePortWakeIrps(HubExtension, STATUS_DELETE_PENDING);
497
498 if (IdleIrp)
499 {
500 USBH_HubCancelIdleIrp(HubExtension, IdleIrp);
501 }
502
503 if (InterlockedDecrement(&HubExtension->PendingRequestCount) > 0)
504 {
505 KeWaitForSingleObject(&HubExtension->PendingRequestEvent,
506 Suspended,
507 KernelMode,
508 FALSE,
509 NULL);
510 }
511
512 if (HubExtension->SCEIrp)
513 {
514 Status = USBH_AbortInterruptPipe(HubExtension);
515
516 if (!NT_SUCCESS(Status) && IoCancelIrp(HubExtension->SCEIrp))
517 {
518 KeWaitForSingleObject(&HubExtension->StatusChangeEvent,
519 Suspended,
520 KernelMode,
521 FALSE,
522 NULL);
523 }
524
525 IoFreeIrp(HubExtension->SCEIrp);
526
527 HubExtension->SCEIrp = NULL;
528 }
529
530 if (!HubExtension->PortData ||
531 !HubExtension->HubDescriptor)
532 {
533 goto Exit;
534 }
535
536 PortData = HubExtension->PortData;
537 NumberPorts = HubExtension->HubDescriptor->bNumberOfPorts;
538
539 for (Port = 0; Port < NumberPorts; Port++)
540 {
541 if (PortData[Port].DeviceObject)
542 {
543 PortExtension = PortData[Port].DeviceObject->DeviceExtension;
544
545 IoAcquireCancelSpinLock(&Irql);
546
547 PortIdleIrp = PortExtension->IdleNotificationIrp;
548
549 if (PortIdleIrp)
550 {
551 PortExtension->PortPdoFlags &= ~USBHUB_PDO_FLAG_IDLE_NOTIFICATION;
552 PortExtension->IdleNotificationIrp = NULL;
553
554 if (PortIdleIrp->Cancel)
555 {
556 PortIdleIrp = NULL;
557 }
558
559 if (PortIdleIrp)
560 {
561 IoSetCancelRoutine(PortIdleIrp, NULL);
562 }
563 }
564
565 PortWakeIrp = PortExtension->PdoWaitWakeIrp;
566
567 if (PortWakeIrp)
568 {
569 PortExtension->PortPdoFlags &= ~USBHUB_PDO_FLAG_WAIT_WAKE;
570 PortExtension->PdoWaitWakeIrp = NULL;
571
572 if (PortWakeIrp->Cancel || !IoSetCancelRoutine(PortWakeIrp, NULL))
573 {
574 PortWakeIrp = NULL;
575
576 if (!InterlockedDecrement(&HubExtension->PendingRequestCount))
577 {
578 KeSetEvent(&HubExtension->PendingRequestEvent,
579 EVENT_INCREMENT,
580 FALSE);
581 }
582 }
583 }
584
585 IoReleaseCancelSpinLock(Irql);
586
587 if (PortIdleIrp)
588 {
589 PortIdleIrp->IoStatus.Status = STATUS_CANCELLED;
590 IoCompleteRequest(PortIdleIrp, IO_NO_INCREMENT);
591 }
592
593 if (PortWakeIrp)
594 {
595 USBH_CompletePowerIrp(HubExtension,
596 PortWakeIrp,
597 STATUS_CANCELLED);
598 }
599
600 if (!(PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_POWER_D3))
601 {
602 DeviceHandle = InterlockedExchangePointer(&PortExtension->DeviceHandle,
603 NULL);
604
605 if (DeviceHandle)
606 {
607 USBD_RemoveDeviceEx(HubExtension, DeviceHandle, 0);
608 }
609
610 PortExtension->PortPdoFlags |= USBHUB_PDO_FLAG_POWER_D3;
611 }
612 }
613
614 USBH_SyncDisablePort(HubExtension, Port + 1);
615 }
616
617 Exit:
618
619 if (HubExtension->SCEBitmap)
620 {
621 ExFreePoolWithTag(HubExtension->SCEBitmap, USB_HUB_TAG);
622 }
623
624 if (HubExtension->HubDescriptor)
625 {
626 ExFreePoolWithTag(HubExtension->HubDescriptor, USB_HUB_TAG);
627 }
628
629 if (HubExtension->HubConfigDescriptor)
630 {
631 ExFreePoolWithTag(HubExtension->HubConfigDescriptor, USB_HUB_TAG);
632 }
633
634 HubExtension->HubFlags &= ~USBHUB_FDO_FLAG_DEVICE_STARTED;
635
636 HubExtension->HubDescriptor = NULL;
637 HubExtension->HubConfigDescriptor = NULL;
638
639 HubExtension->SCEIrp = NULL;
640 HubExtension->SCEBitmap = NULL;
641 }
642
643 NTSTATUS
644 NTAPI
645 USBH_StartHubFdoDevice(IN PUSBHUB_FDO_EXTENSION HubExtension,
646 IN PIRP Irp)
647 {
648 NTSTATUS Status;
649 ULONG DisableRemoteWakeup = 0;
650 ULONG HubCount = 0;
651 PUSB_DEVICE_HANDLE DeviceHandle;
652 USB_DEVICE_TYPE DeviceType;
653 DEVICE_CAPABILITIES DeviceCapabilities;
654 BOOLEAN IsBusPowered;
655 static WCHAR DisableWakeValueName[] = L"DisableRemoteWakeup";
656
657 DPRINT("USBH_StartHubFdoDevice: ... \n");
658
659 KeInitializeEvent(&HubExtension->IdleEvent, NotificationEvent, FALSE);
660 KeInitializeEvent(&HubExtension->ResetEvent, NotificationEvent, TRUE);
661 KeInitializeEvent(&HubExtension->PendingRequestEvent, NotificationEvent, FALSE);
662 KeInitializeEvent(&HubExtension->LowerDeviceEvent, NotificationEvent, FALSE);
663 KeInitializeEvent(&HubExtension->StatusChangeEvent, NotificationEvent, TRUE);
664 KeInitializeEvent(&HubExtension->RootHubNotificationEvent,
665 NotificationEvent,
666 TRUE);
667
668 KeInitializeSpinLock(&HubExtension->RelationsWorkerSpinLock);
669 KeInitializeSpinLock(&HubExtension->CheckIdleSpinLock);
670
671 KeInitializeSemaphore(&HubExtension->ResetDeviceSemaphore, 1, 1);
672 KeInitializeSemaphore(&HubExtension->HubPortSemaphore, 1, 1);
673 KeInitializeSemaphore(&HubExtension->HubSemaphore, 1, 1);
674
675 HubExtension->HubFlags = 0;
676 HubExtension->HubConfigDescriptor = NULL;
677 HubExtension->HubDescriptor = NULL;
678 HubExtension->SCEIrp = NULL;
679 HubExtension->SCEBitmap = NULL;
680 HubExtension->SystemPowerState.SystemState = PowerSystemWorking;
681 HubExtension->PendingRequestCount = 1;
682 HubExtension->ResetRequestCount = 0;
683 HubExtension->PendingIdleIrp = NULL;
684 HubExtension->PendingWakeIrp = NULL;
685
686 InitializeListHead(&HubExtension->PdoList);
687
688 HubExtension->HubFlags |= USBHUB_FDO_FLAG_WITEM_INIT;
689 InitializeListHead(&HubExtension->WorkItemList);
690 KeInitializeSpinLock(&HubExtension->WorkItemSpinLock);
691
692 IoCopyCurrentIrpStackLocationToNext(Irp);
693
694 IoSetCompletionRoutine(Irp,
695 USBH_HubPnPIrpComplete,
696 HubExtension,
697 TRUE,
698 TRUE,
699 TRUE);
700
701 if (IoCallDriver(HubExtension->LowerDevice, Irp) == STATUS_PENDING)
702 {
703 KeWaitForSingleObject(&HubExtension->LowerDeviceEvent,
704 Suspended,
705 KernelMode,
706 FALSE,
707 NULL);
708 }
709
710 HubExtension->RootHubPdo = NULL;
711
712 Status = USBH_SyncGetRootHubPdo(HubExtension->LowerDevice,
713 &HubExtension->RootHubPdo,
714 &HubExtension->RootHubPdo2);
715
716 if (!NT_SUCCESS(Status))
717 {
718 DPRINT1("USBH_SyncGetRootHubPdo() failed - %lX\n", Status);
719 goto ErrorExit;
720 }
721
722 USBH_WriteFailReasonID(HubExtension->LowerPDO, USBHUB_FAIL_NO_FAIL);
723
724 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_DEVICE_FAILED)
725 {
726 DPRINT1("USBH_StartHubFdoDevice: USBHUB_FDO_FLAG_DEVICE_FAILED - TRUE\n");
727 Status = STATUS_UNSUCCESSFUL;
728 goto ErrorExit;
729 }
730
731 HubExtension->HubFlags |= USBHUB_FDO_FLAG_REMOTE_WAKEUP;
732
733 Status = USBD_GetPdoRegistryParameter(HubExtension->LowerPDO,
734 &DisableRemoteWakeup,
735 sizeof(DisableRemoteWakeup),
736 DisableWakeValueName,
737 sizeof(DisableWakeValueName));
738
739 if (NT_SUCCESS(Status) && DisableRemoteWakeup)
740 {
741 DPRINT("USBH_StartHubFdoDevice: DisableRemoteWakeup - TRUE\n");
742 HubExtension->HubFlags &= ~USBHUB_FDO_FLAG_REMOTE_WAKEUP;
743 }
744
745 HubExtension->CurrentPowerState.DeviceState = PowerDeviceD0;
746
747 USBH_SyncGetHubCount(HubExtension->LowerDevice,
748 &HubCount);
749
750 Status = USBHUB_GetBusInterface(HubExtension->RootHubPdo,
751 &HubExtension->BusInterface);
752
753 if (!NT_SUCCESS(Status))
754 {
755 DPRINT1("USBH_StartHubFdoDevice: USBHUB_GetBusInterface() failed - %lX\n",
756 Status);
757 goto ErrorExit;
758 }
759
760 Status = USBHUB_GetBusInterfaceUSBDI(HubExtension->LowerDevice,
761 &HubExtension->BusInterfaceUSBDI);
762
763 if (!NT_SUCCESS(Status))
764 {
765 DPRINT1("USBH_StartHubFdoDevice: USBHUB_GetBusInterfaceUSBDI() failed - %lX\n",
766 Status);
767 goto ErrorExit;
768 }
769
770 DeviceHandle = USBH_SyncGetDeviceHandle(HubExtension->LowerDevice);
771
772 if (DeviceHandle)
773 {
774 Status = USBH_GetDeviceType(HubExtension, DeviceHandle, &DeviceType);
775
776 if (!NT_SUCCESS(Status))
777 {
778 DPRINT1("USBH_StartHubFdoDevice: USBH_GetDeviceType() failed - %lX\n",
779 Status);
780
781 goto ErrorExit;
782 }
783
784 if (DeviceType == Usb20Device)
785 {
786 HubExtension->HubFlags |= USBHUB_FDO_FLAG_USB20_HUB;
787 }
788 }
789
790 if (HubCount > USBHUB_MAX_CASCADE_LEVELS)
791 {
792 PUSBHUB_PORT_PDO_EXTENSION ParentPdoExtension;
793 PUSBHUB_FDO_EXTENSION ParentHubExtension;
794 USHORT ParentPort;
795 PUSBHUB_PORT_DATA PortData;
796
797 DPRINT1("USBH_StartHubFdoDevice: HubCount > 6 - %x\n", HubCount);
798
799 USBH_WriteFailReasonID(HubExtension->LowerPDO,
800 USBHUB_FAIL_NESTED_TOO_DEEPLY);
801
802 ParentPdoExtension = HubExtension->LowerPDO->DeviceExtension;
803 ParentHubExtension = ParentPdoExtension->HubExtension;
804
805 ParentPort = ParentPdoExtension->PortNumber - 1;
806 PortData = &ParentHubExtension->PortData[ParentPort];
807 PortData->ConnectionStatus = DeviceHubNestedTooDeeply;
808
809 HubExtension->HubFlags |= USBHUB_FDO_FLAG_DEVICE_FAILED;
810 }
811
812 USBH_QueryCapabilities(HubExtension->LowerDevice, &DeviceCapabilities);
813
814 HubExtension->SystemWake = DeviceCapabilities.SystemWake;
815 HubExtension->DeviceWake = DeviceCapabilities.DeviceWake;
816
817 RtlCopyMemory(HubExtension->DeviceState,
818 &DeviceCapabilities.DeviceState,
819 POWER_SYSTEM_MAXIMUM * sizeof(DEVICE_POWER_STATE));
820
821 Status = USBH_GetDeviceDescriptor(HubExtension->Common.SelfDevice,
822 &HubExtension->HubDeviceDescriptor);
823
824 if (!NT_SUCCESS(Status))
825 {
826 DPRINT1("USBH_StartHubFdoDevice: USBH_GetDeviceDescriptor() failed - %lX\n",
827 Status);
828 goto ErrorExit;
829 }
830
831 Status = USBH_GetConfigurationDescriptor(HubExtension->Common.SelfDevice,
832 &HubExtension->HubConfigDescriptor);
833
834 if (!NT_SUCCESS(Status))
835 {
836 DPRINT1("USBH_StartHubFdoDevice: USBH_GetConfigurationDescriptor() failed - %lX\n",
837 Status);
838 goto ErrorExit;
839 }
840
841 Status = USBH_SyncGetHubDescriptor(HubExtension);
842
843 if (!NT_SUCCESS(Status))
844 {
845 DPRINT1("USBH_StartHubFdoDevice: USBH_SyncGetHubDescriptor() failed - %lX\n",
846 Status);
847 goto ErrorExit;
848 }
849
850 IsBusPowered = USBH_HubIsBusPowered(HubExtension->Common.SelfDevice,
851 HubExtension->HubConfigDescriptor);
852
853 if (IsBusPowered)
854 {
855 /* bus-powered hub is allowed a maximum of 100 mA only for each port */
856 HubExtension->MaxPowerPerPort = 100;
857
858 /* can have 4 ports (4 * 100 mA) and 100 mA remains for itself;
859 expressed in 2 mA units (i.e., 250 = 500 mA). */
860 HubExtension->HubConfigDescriptor->MaxPower = 250;
861 }
862 else
863 {
864 /* self-powered hub is allowed a maximum of 500 mA for each port */
865 HubExtension->MaxPowerPerPort = 500;
866 }
867
868 Status = USBH_OpenConfiguration(HubExtension);
869
870 if (!NT_SUCCESS(Status))
871 {
872 DPRINT1("USBH_StartHubFdoDevice: USBH_OpenConfiguration() failed - %lX\n",
873 Status);
874 goto ErrorExit;
875 }
876
877 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_USB20_HUB)
878 {
879 Status = USBD_Initialize20Hub(HubExtension);
880 }
881
882 if (!NT_SUCCESS(Status))
883 {
884 goto ErrorExit;
885 }
886
887 HubExtension->SCEIrp = IoAllocateIrp(HubExtension->Common.SelfDevice->StackSize,
888 FALSE);
889
890 HubExtension->ResetPortIrp = IoAllocateIrp(HubExtension->Common.SelfDevice->StackSize,
891 FALSE);
892
893 if (!HubExtension->SCEIrp || !HubExtension->ResetPortIrp)
894 {
895 Status = STATUS_INSUFFICIENT_RESOURCES;
896 goto ErrorExit;
897 }
898
899 HubExtension->SCEBitmapLength = HubExtension->PipeInfo.MaximumPacketSize;
900
901 HubExtension->SCEBitmap = ExAllocatePoolWithTag(NonPagedPool,
902 HubExtension->SCEBitmapLength,
903 USB_HUB_TAG);
904
905 if (!HubExtension->SCEBitmap)
906 {
907 Status = STATUS_INSUFFICIENT_RESOURCES;
908 goto ErrorExit;
909 }
910
911 RtlZeroMemory(HubExtension->SCEBitmap, HubExtension->SCEBitmapLength);
912
913 Status = USBH_SyncPowerOnPorts(HubExtension);
914
915 if (!NT_SUCCESS(Status))
916 {
917 goto ErrorExit;
918 }
919 else
920 {
921 USHORT Port;
922
923 HubExtension->HubFlags |= USBHUB_FDO_FLAG_DEVICE_STARTED;
924
925 for (Port = 1;
926 Port <= HubExtension->HubDescriptor->bNumberOfPorts;
927 Port++)
928 {
929 USBH_SyncClearPortStatus(HubExtension,
930 Port,
931 USBHUB_FEATURE_C_PORT_CONNECTION);
932 }
933 }
934
935 if (HubExtension->LowerPDO == HubExtension->RootHubPdo)
936 {
937 USBD_RegisterRootHubCallBack(HubExtension);
938 }
939 else
940 {
941 HubExtension->HubFlags |= USBHUB_FDO_FLAG_DO_ENUMERATION;
942 USBH_SubmitStatusChangeTransfer(HubExtension);
943 }
944
945 goto Exit;
946
947 ErrorExit:
948
949 if (HubExtension->HubDescriptor)
950 {
951 ExFreePoolWithTag(HubExtension->HubDescriptor, USB_HUB_TAG);
952 HubExtension->HubDescriptor = NULL;
953 }
954
955 if (HubExtension->SCEIrp)
956 {
957 IoFreeIrp(HubExtension->SCEIrp);
958 HubExtension->SCEIrp = NULL;
959 }
960
961 if (HubExtension->ResetPortIrp)
962 {
963 IoFreeIrp(HubExtension->ResetPortIrp);
964 HubExtension->ResetPortIrp = NULL;
965 }
966
967 if (HubExtension->SCEBitmap)
968 {
969 ExFreePoolWithTag(HubExtension->SCEBitmap, USB_HUB_TAG);
970 HubExtension->SCEBitmap = NULL;
971 }
972
973 if (HubExtension->HubConfigDescriptor)
974 {
975 ExFreePoolWithTag(HubExtension->HubConfigDescriptor, USB_HUB_TAG);
976 HubExtension->HubConfigDescriptor = NULL;
977 }
978
979 Exit:
980
981 USBH_CompleteIrp(Irp, Status);
982
983 return Status;
984 }
985
986 NTSTATUS
987 NTAPI
988 USBH_FdoStartDevice(IN PUSBHUB_FDO_EXTENSION HubExtension,
989 IN PIRP Irp)
990 {
991 NTSTATUS Status;
992
993 DPRINT("USBH_FdoStartDevice: HubExtension - %p\n", HubExtension);
994
995 HubExtension->RootHubPdo = NULL;
996
997 Status = USBH_SyncGetRootHubPdo(HubExtension->LowerDevice,
998 &HubExtension->RootHubPdo,
999 &HubExtension->RootHubPdo2);
1000
1001 if (NT_SUCCESS(Status))
1002 {
1003 if (HubExtension->RootHubPdo)
1004 {
1005 Status = USBH_StartHubFdoDevice(HubExtension, Irp);
1006 }
1007 else
1008 {
1009 DPRINT1("USBH_FdoStartDevice: FIXME. start ParentDevice\n");
1010 DbgBreakPoint();
1011 }
1012 }
1013 else
1014 {
1015 DPRINT1("USBH_FdoStartDevice: FIXME. USBH_SyncGetRootHubPdo return - %lX\n",
1016 Status);
1017
1018 DbgBreakPoint();
1019 USBH_CompleteIrp(Irp, Status);
1020 }
1021
1022 return Status;
1023 }
1024
1025 NTSTATUS
1026 NTAPI
1027 USBH_FdoQueryBusRelations(IN PUSBHUB_FDO_EXTENSION HubExtension,
1028 IN PIRP Irp)
1029 {
1030 PDEVICE_RELATIONS DeviceRelations = NULL;
1031 NTSTATUS Status = STATUS_SUCCESS;
1032 LIST_ENTRY GhostPdoList;
1033 KIRQL OldIrql;
1034 PLIST_ENTRY PdoList;
1035 UCHAR NumberPorts;
1036 USHORT Port;
1037 USHORT GhostPort;
1038 PUSBHUB_PORT_DATA PortData;
1039 PDEVICE_OBJECT PdoDevice;
1040 PUSBHUB_PORT_PDO_EXTENSION PdoExtension;
1041 PUSBHUB_PORT_PDO_EXTENSION pdoExtension;
1042 NTSTATUS NtStatus;
1043 PVOID SerialNumber;
1044 PVOID DeviceHandle;
1045 USB_PORT_STATUS UsbPortStatus;
1046 PLIST_ENTRY Entry;
1047 ULONG Length;
1048
1049 DPRINT_ENUM("USBH_FdoQueryBusRelations: HubFlags - %lX\n",
1050 HubExtension->HubFlags);
1051
1052 if (!(HubExtension->HubFlags & USBHUB_FDO_FLAG_DEVICE_STARTED))
1053 {
1054 Status = STATUS_INVALID_DEVICE_STATE;
1055 goto RelationsWorker;
1056 }
1057
1058 if (!HubExtension->HubDescriptor)
1059 {
1060 Status = STATUS_UNSUCCESSFUL;
1061 goto RelationsWorker;
1062 }
1063
1064 if (!(HubExtension->HubFlags & USBHUB_FDO_FLAG_DO_ENUMERATION))
1065 {
1066 DPRINT_ENUM("USBH_FdoQueryBusRelations: Skip enumeration\n");
1067 goto RelationsWorker;
1068 }
1069
1070 InterlockedIncrement(&HubExtension->PendingRequestCount);
1071
1072 KeWaitForSingleObject(&HubExtension->ResetDeviceSemaphore,
1073 Executive,
1074 KernelMode,
1075 FALSE,
1076 NULL);
1077
1078 NumberPorts = HubExtension->HubDescriptor->bNumberOfPorts;
1079 DPRINT_ENUM("USBH_FdoQueryBusRelations: NumberPorts - %x\n", NumberPorts);
1080
1081 Length = FIELD_OFFSET(DEVICE_RELATIONS, Objects) +
1082 NumberPorts * sizeof(PDEVICE_OBJECT);
1083
1084 if (Irp->IoStatus.Information)
1085 {
1086 DPRINT1("FIXME: leaking old bus relations\n");
1087 }
1088
1089 DeviceRelations = ExAllocatePoolWithTag(NonPagedPool, Length, USB_HUB_TAG);
1090
1091 if (!DeviceRelations)
1092 {
1093 HubExtension->HubFlags &= ~USBHUB_FDO_FLAG_NOT_ENUMERATED;
1094
1095 KeReleaseSemaphore(&HubExtension->ResetDeviceSemaphore,
1096 LOW_REALTIME_PRIORITY,
1097 1,
1098 FALSE);
1099
1100 if (!InterlockedDecrement(&HubExtension->PendingRequestCount))
1101 {
1102 KeSetEvent(&HubExtension->PendingRequestEvent, EVENT_INCREMENT, FALSE);
1103 }
1104
1105 Status = STATUS_INSUFFICIENT_RESOURCES;
1106 goto RelationsWorker;
1107 }
1108
1109 RtlZeroMemory(DeviceRelations, Length);
1110
1111 DeviceRelations->Count = 0;
1112
1113 EnumStart:
1114
1115 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_ESD_RECOVERING)
1116 {
1117 HubExtension->HubFlags &= ~USBHUB_FDO_FLAG_NOT_ENUMERATED;
1118
1119 KeReleaseSemaphore(&HubExtension->ResetDeviceSemaphore,
1120 LOW_REALTIME_PRIORITY,
1121 1,
1122 FALSE);
1123
1124 if (!InterlockedDecrement(&HubExtension->PendingRequestCount))
1125 {
1126 KeSetEvent(&HubExtension->PendingRequestEvent, EVENT_INCREMENT, FALSE);
1127 }
1128
1129 Status = STATUS_SUCCESS;
1130 goto RelationsWorker;
1131 }
1132
1133 HubExtension->HubFlags &= ~USBHUB_FDO_FLAG_ENUM_POST_RECOVER;
1134
1135 for (Port = 1; Port <= NumberPorts; Port++)
1136 {
1137 PortData = &HubExtension->PortData[Port - 1];
1138
1139 DPRINT_ENUM("USBH_FdoQueryBusRelations: Port - %x, ConnectStatus - %x\n",
1140 Port,
1141 PortData->PortStatus.PortStatus.Usb20PortStatus.CurrentConnectStatus);
1142
1143 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_DEVICE_FAILED)
1144 {
1145 continue;
1146 }
1147
1148 Status = USBH_SyncGetPortStatus(HubExtension,
1149 Port,
1150 &PortData->PortStatus,
1151 sizeof(USB_PORT_STATUS_AND_CHANGE));
1152
1153 if (!NT_SUCCESS(Status))
1154 {
1155 HubExtension->HubFlags |= USBHUB_FDO_FLAG_DEVICE_FAILED;
1156 DeviceRelations->Count = 0;
1157 goto EnumStart;
1158 }
1159
1160 PdoDevice = PortData->DeviceObject;
1161
1162 if (PortData->DeviceObject)
1163 {
1164 PdoExtension = PdoDevice->DeviceExtension;
1165
1166 if (PdoExtension->PortPdoFlags & USBHUB_PDO_FLAG_OVERCURRENT_PORT)
1167 {
1168 PortData->PortStatus.PortStatus.Usb20PortStatus.CurrentConnectStatus = 1;
1169 }
1170 }
1171
1172 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_DEVICE_FAILED)
1173 {
1174 DPRINT1("USBH_FdoQueryBusRelations: DbgBreakPoint() \n");
1175 DbgBreakPoint();
1176 }
1177
1178 if (!PortData->PortStatus.PortStatus.Usb20PortStatus.CurrentConnectStatus)
1179 {
1180 if (PdoDevice)
1181 {
1182 PdoExtension = PdoDevice->DeviceExtension;
1183
1184 PdoExtension->PortPdoFlags |= USBHUB_PDO_FLAG_DELETE_PENDING;
1185 PdoExtension->EnumFlags &= ~USBHUB_ENUM_FLAG_DEVICE_PRESENT;
1186
1187 SerialNumber = InterlockedExchangePointer((PVOID)&PdoExtension->SerialNumber,
1188 NULL);
1189
1190 if (SerialNumber)
1191 {
1192 ExFreePoolWithTag(SerialNumber, USB_HUB_TAG);
1193 }
1194
1195 DeviceHandle = InterlockedExchangePointer(&PdoExtension->DeviceHandle,
1196 NULL);
1197
1198 if (DeviceHandle)
1199 {
1200 USBD_RemoveDeviceEx(HubExtension, DeviceHandle, 0);
1201 USBH_SyncDisablePort(HubExtension, Port);
1202 }
1203 }
1204
1205 PortData->DeviceObject = NULL;
1206 PortData->ConnectionStatus = NoDeviceConnected;
1207 continue;
1208 }
1209
1210 if (PdoDevice)
1211 {
1212 ObReferenceObject(PdoDevice);
1213
1214 PdoDevice->Flags |= DO_POWER_PAGABLE;
1215 PdoDevice->Flags &= ~DO_DEVICE_INITIALIZING;
1216
1217 DeviceRelations->Objects[DeviceRelations->Count++] = PdoDevice;
1218
1219 PdoExtension = PdoDevice->DeviceExtension;
1220 PdoExtension->PortPdoFlags &= ~USBHUB_PDO_FLAG_POWER_D1_OR_D2;
1221
1222 continue;
1223 }
1224
1225 USBH_Wait(100);
1226
1227 NtStatus = USBH_SyncResetPort(HubExtension, Port);
1228
1229 if (!NT_SUCCESS(NtStatus))
1230 {
1231 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_USB20_HUB)
1232 {
1233 PortData->DeviceObject = NULL;
1234 PortData->ConnectionStatus = NoDeviceConnected;
1235 continue;
1236 }
1237 }
1238 else
1239 {
1240 NtStatus = USBH_SyncGetPortStatus(HubExtension,
1241 Port,
1242 &PortData->PortStatus,
1243 sizeof(USB_PORT_STATUS_AND_CHANGE));
1244
1245 UsbPortStatus = PortData->PortStatus.PortStatus;
1246
1247 if (NT_SUCCESS(NtStatus))
1248 {
1249 ULONG ix = 0;
1250
1251 for (NtStatus = USBH_CreateDevice(HubExtension, Port, UsbPortStatus, ix);
1252 !NT_SUCCESS(NtStatus);
1253 NtStatus = USBH_CreateDevice(HubExtension, Port, UsbPortStatus, ix))
1254 {
1255 USBH_Wait(500);
1256
1257 if (ix >= 2)
1258 {
1259 break;
1260 }
1261
1262 if (PortData->DeviceObject)
1263 {
1264 IoDeleteDevice(PortData->DeviceObject);
1265 PortData->DeviceObject = NULL;
1266 PortData->ConnectionStatus = NoDeviceConnected;
1267 }
1268
1269 USBH_SyncResetPort(HubExtension, Port);
1270
1271 ix++;
1272 }
1273
1274 if (NT_SUCCESS(NtStatus))
1275 {
1276 PdoExtension = PortData->DeviceObject->DeviceExtension;
1277
1278 if (!(PdoExtension->PortPdoFlags & USBHUB_PDO_FLAG_PORT_LOW_SPEED) &&
1279 !(PdoExtension->PortPdoFlags & USBHUB_PDO_FLAG_PORT_HIGH_SPEED) &&
1280 !(HubExtension->HubFlags & USBHUB_FDO_FLAG_USB20_HUB))
1281 {
1282 DPRINT1("USBH_FdoQueryBusRelations: FIXME USBH_DeviceIs2xDualMode()\n");
1283
1284 if (0)//USBH_DeviceIs2xDualMode(PdoExtension))
1285 {
1286 PdoExtension->PortPdoFlags |= USBHUB_PDO_FLAG_HS_USB1_DUALMODE;
1287 }
1288 }
1289
1290 ObReferenceObject(PortData->DeviceObject);
1291
1292 DeviceRelations->Objects[DeviceRelations->Count] = PortData->DeviceObject;
1293
1294 PortData->DeviceObject->Flags |= DO_POWER_PAGABLE;
1295 PortData->DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
1296
1297 DeviceRelations->Count++;
1298
1299 PortData->ConnectionStatus = DeviceConnected;
1300
1301 continue;
1302 }
1303 }
1304 }
1305
1306 PortData->ConnectionStatus = DeviceFailedEnumeration;
1307
1308 if (NT_ERROR(USBH_SyncDisablePort(HubExtension, Port)))
1309 {
1310 HubExtension->HubFlags |= USBHUB_FDO_FLAG_DEVICE_FAILED;
1311 }
1312
1313 if (PortData->DeviceObject)
1314 {
1315 ObReferenceObject(PortData->DeviceObject);
1316 PortData->DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
1317 DeviceRelations->Objects[DeviceRelations->Count++] = PortData->DeviceObject;
1318 }
1319 }
1320
1321 HubExtension->HubFlags &= ~USBHUB_FDO_FLAG_NOT_ENUMERATED;
1322
1323 KeReleaseSemaphore(&HubExtension->ResetDeviceSemaphore,
1324 LOW_REALTIME_PRIORITY,
1325 1,
1326 FALSE);
1327
1328 if (!InterlockedDecrement(&HubExtension->PendingRequestCount))
1329 {
1330 KeSetEvent(&HubExtension->PendingRequestEvent, EVENT_INCREMENT, FALSE);
1331 }
1332
1333 RelationsWorker:
1334
1335 Irp->IoStatus.Status = Status;
1336
1337 if (!NT_SUCCESS(Status))
1338 {
1339 //Irp->IoStatus.Information = 0;
1340
1341 if (DeviceRelations)
1342 {
1343 ExFreePoolWithTag(DeviceRelations, USB_HUB_TAG);
1344 }
1345
1346 USBH_CompleteIrp(Irp, Status);
1347
1348 return Status;
1349 }
1350
1351 KeAcquireSpinLock(&HubExtension->RelationsWorkerSpinLock, &OldIrql);
1352
1353 if (DeviceRelations && DeviceRelations->Count)
1354 {
1355 for (Port = 0; Port < DeviceRelations->Count; Port++)
1356 {
1357 PdoDevice = DeviceRelations->Objects[Port];
1358 Entry = HubExtension->PdoList.Flink;
1359
1360 while (Entry != &HubExtension->PdoList)
1361 {
1362 pdoExtension = CONTAINING_RECORD(Entry,
1363 USBHUB_PORT_PDO_EXTENSION,
1364 PortLink);
1365
1366 if (pdoExtension == PdoDevice->DeviceExtension)
1367 {
1368 PdoExt(PdoDevice)->EnumFlags |= USBHUB_ENUM_FLAG_GHOST_DEVICE;
1369 goto PortNext;
1370 }
1371
1372 Entry = Entry->Flink;
1373 }
1374
1375 PdoExt(PdoDevice)->EnumFlags |= USBHUB_ENUM_FLAG_DEVICE_PRESENT;
1376
1377 PortNext:;
1378 }
1379
1380 for (Port = 0; Port < DeviceRelations->Count; Port++)
1381 {
1382 PdoDevice = DeviceRelations->Objects[Port];
1383
1384 if (PdoExt(PdoDevice)->EnumFlags & USBHUB_ENUM_FLAG_GHOST_DEVICE)
1385 {
1386 for (GhostPort = Port;
1387 GhostPort < DeviceRelations->Count;
1388 GhostPort++)
1389 {
1390 DeviceRelations->Objects[GhostPort] =
1391 DeviceRelations->Objects[GhostPort + 1];
1392 }
1393
1394 ObDereferenceObject(PdoDevice);
1395
1396 DeviceRelations->Count--;
1397
1398 if (PdoExt(PdoDevice)->EnumFlags & USBHUB_ENUM_FLAG_DEVICE_PRESENT)
1399 {
1400 PdoExt(PdoDevice)->EnumFlags &= ~USBHUB_ENUM_FLAG_GHOST_DEVICE;
1401 }
1402 }
1403 }
1404 }
1405
1406 Irp->IoStatus.Information = (ULONG_PTR)DeviceRelations;
1407
1408 InitializeListHead(&GhostPdoList);
1409 PdoList = &HubExtension->PdoList;
1410
1411 while (!IsListEmpty(PdoList))
1412 {
1413 Entry = RemoveHeadList(PdoList);
1414
1415 PdoExtension = CONTAINING_RECORD(Entry,
1416 USBHUB_PORT_PDO_EXTENSION,
1417 PortLink);
1418
1419 PdoExtension->EnumFlags &= ~USBHUB_ENUM_FLAG_DEVICE_PRESENT;
1420
1421 if (PdoExtension->EnumFlags & USBHUB_ENUM_FLAG_GHOST_DEVICE)
1422 {
1423 InsertTailList(&GhostPdoList, &PdoExtension->PortLink);
1424 }
1425 }
1426
1427 KeReleaseSpinLock(&HubExtension->RelationsWorkerSpinLock, OldIrql);
1428
1429 while (!IsListEmpty(&GhostPdoList))
1430 {
1431 Entry = RemoveHeadList(&GhostPdoList);
1432
1433 PdoExtension = CONTAINING_RECORD(Entry,
1434 USBHUB_PORT_PDO_EXTENSION,
1435 PortLink);
1436
1437 IoDeleteDevice(PdoExtension->Common.SelfDevice);
1438 }
1439
1440 return USBH_PassIrp(HubExtension->LowerDevice, Irp);
1441 }
1442
1443 NTSTATUS
1444 NTAPI
1445 USBH_FdoStopDevice(IN PUSBHUB_FDO_EXTENSION HubExtension,
1446 IN PIRP Irp)
1447 {
1448 DPRINT1("USBH_FdoStopDevice: UNIMPLEMENTED. FIXME\n");
1449 DbgBreakPoint();
1450 return STATUS_SUCCESS;
1451 }
1452
1453 NTSTATUS
1454 NTAPI
1455 USBH_FdoRemoveDevice(IN PUSBHUB_FDO_EXTENSION HubExtension,
1456 IN PIRP Irp)
1457 {
1458 PUSB_HUB_DESCRIPTOR HubDescriptor;
1459 PUSBHUB_PORT_DATA PortData;
1460 USHORT NumPorts;
1461 USHORT ix;
1462 PDEVICE_OBJECT PortDevice;
1463 PUSBHUB_PORT_PDO_EXTENSION PortExtension;
1464 NTSTATUS Status;
1465
1466 DPRINT("USBH_FdoRemoveDevice: HubExtension - %p\n", HubExtension);
1467
1468 HubDescriptor = HubExtension->HubDescriptor;
1469
1470 if (HubDescriptor && HubExtension->PortData)
1471 {
1472 NumPorts = HubDescriptor->bNumberOfPorts;
1473
1474 for (ix = 0; ix < NumPorts; ++ix)
1475 {
1476 PortData = HubExtension->PortData + ix;
1477
1478 PortDevice = PortData->DeviceObject;
1479
1480 if (PortDevice)
1481 {
1482 PortData->PortStatus.AsUlong32 = 0;
1483 PortData->DeviceObject = NULL;
1484
1485 PortExtension = PortDevice->DeviceExtension;
1486 PortExtension->EnumFlags &= ~USBHUB_ENUM_FLAG_DEVICE_PRESENT;
1487
1488 USBH_PdoRemoveDevice(PortExtension, HubExtension);
1489 }
1490 }
1491 }
1492
1493 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_DEVICE_STARTED)
1494 {
1495 USBH_FdoCleanup(HubExtension);
1496 }
1497
1498 if (HubExtension->PortData)
1499 {
1500 ExFreePoolWithTag(HubExtension->PortData, USB_HUB_TAG);
1501 HubExtension->PortData = NULL;
1502 }
1503
1504 DPRINT1("USBH_FdoRemoveDevice: call IoWMIRegistrationControl UNIMPLEMENTED. FIXME\n");
1505
1506 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
1507
1508 IoDetachDevice(HubExtension->LowerDevice);
1509 IoDeleteDevice(HubExtension->Common.SelfDevice);
1510
1511 return Status;
1512 }
1513
1514 VOID
1515 NTAPI
1516 USBH_FdoSurpriseRemoveDevice(IN PUSBHUB_FDO_EXTENSION HubExtension,
1517 IN PIRP Irp)
1518 {
1519 PUSBHUB_PORT_PDO_EXTENSION PortExtension;
1520 PUSBHUB_PORT_DATA PortData;
1521 ULONG NumberPorts;
1522 ULONG Port;
1523
1524 DPRINT("USBH_FdoSurpriseRemoveDevice: HubExtension - %p, Irp - %p\n",
1525 HubExtension,
1526 Irp);
1527
1528 if (!HubExtension->PortData ||
1529 !HubExtension->HubDescriptor)
1530 {
1531 return;
1532 }
1533
1534 PortData = HubExtension->PortData;
1535 NumberPorts = HubExtension->HubDescriptor->bNumberOfPorts;
1536
1537 for (Port = 0; Port < NumberPorts; Port++)
1538 {
1539 if (PortData[Port].DeviceObject)
1540 {
1541 PortExtension = PdoExt(PortData[Port].DeviceObject);
1542 PortExtension->PortPdoFlags |= USBHUB_PDO_FLAG_DELETE_PENDING;
1543 PortExtension->EnumFlags &= ~USBHUB_ENUM_FLAG_DEVICE_PRESENT;
1544
1545 PortData[Port].DeviceObject = NULL;
1546 PortData[Port].ConnectionStatus = NoDeviceConnected;
1547 }
1548 }
1549 }
1550
1551 NTSTATUS
1552 NTAPI
1553 USBH_PdoQueryId(IN PUSBHUB_PORT_PDO_EXTENSION PortExtension,
1554 IN PIRP Irp)
1555 {
1556 ULONG IdType;
1557 WCHAR Buffer[200];
1558 PWCHAR EndBuffer;
1559 size_t Remaining = sizeof(Buffer);
1560 ULONG Length;
1561 PWCHAR Id = NULL;
1562 NTSTATUS Status = STATUS_SUCCESS;
1563 PUSB_DEVICE_DESCRIPTOR DeviceDescriptor;
1564 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
1565
1566 IdType = IoGetCurrentIrpStackLocation(Irp)->Parameters.QueryId.IdType;
1567 DeviceDescriptor = &PortExtension->DeviceDescriptor;
1568 InterfaceDescriptor = &PortExtension->InterfaceDescriptor;
1569
1570 RtlZeroMemory(Buffer, sizeof(Buffer));
1571
1572 switch (IdType)
1573 {
1574 case BusQueryDeviceID:
1575 DPRINT("USBH_PdoQueryId: BusQueryDeviceID\n");
1576
1577 if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_INIT_PORT_FAILED)
1578 {
1579 DPRINT("USBH_PdoQueryId: USBHUB_PDO_FLAG_INIT_PORT_FAILED\n");
1580 RtlStringCbPrintfExW(Buffer,
1581 Remaining,
1582 NULL,
1583 &Remaining,
1584 0,
1585 L"USB\\Vid_0000&Pid_0000");
1586 }
1587 else
1588 {
1589 RtlStringCbPrintfExW(Buffer,
1590 Remaining,
1591 NULL,
1592 &Remaining,
1593 0,
1594 L"USB\\Vid_%04x&Pid_%04x",
1595 DeviceDescriptor->idVendor,
1596 DeviceDescriptor->idProduct);
1597 }
1598
1599 Length = sizeof(Buffer) - (Remaining - sizeof(UNICODE_NULL));
1600
1601 Id = ExAllocatePoolWithTag(PagedPool, Length, USB_HUB_TAG);
1602
1603 if (!Id)
1604 {
1605 break;
1606 }
1607
1608 RtlCopyMemory(Id, Buffer, Length);
1609 DPRINT("USBH_PdoQueryId: BusQueryDeviceID - %S\n", Id);
1610 break;
1611
1612 case BusQueryHardwareIDs:
1613 DPRINT("USBH_PdoQueryId: BusQueryHardwareIDs\n");
1614
1615 if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_INIT_PORT_FAILED)
1616 {
1617 DPRINT("USBH_PdoQueryId: USBHUB_PDO_FLAG_INIT_PORT_FAILED\n");
1618
1619 RtlStringCbPrintfExW(Buffer,
1620 Remaining,
1621 NULL,
1622 &Remaining,
1623 0,
1624 L"USB\\UNKNOWN");
1625 }
1626 else
1627 {
1628 RtlStringCbPrintfExW(Buffer,
1629 Remaining,
1630 &EndBuffer,
1631 &Remaining,
1632 0,
1633 L"USB\\Vid_%04x&Pid_%04x&Rev_%04x",
1634 DeviceDescriptor->idVendor,
1635 DeviceDescriptor->idProduct,
1636 DeviceDescriptor->bcdDevice);
1637
1638 EndBuffer++;
1639 Remaining -= sizeof(UNICODE_NULL);
1640
1641 RtlStringCbPrintfExW(EndBuffer,
1642 Remaining,
1643 NULL,
1644 &Remaining,
1645 0,
1646 L"USB\\Vid_%04x&Pid_%04x",
1647 DeviceDescriptor->idVendor,
1648 DeviceDescriptor->idProduct);
1649 }
1650
1651 Length = sizeof(Buffer) - (Remaining - sizeof(UNICODE_NULL));
1652
1653 Id = ExAllocatePoolWithTag(PagedPool, Length, USB_HUB_TAG);
1654
1655 if (!Id)
1656 {
1657 break;
1658 }
1659
1660 RtlCopyMemory(Id, Buffer, Length);
1661
1662 if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_INIT_PORT_FAILED)
1663 {
1664 DPRINT("USBH_PdoQueryId: BusQueryInstanceID - %S\n", Id);
1665 }
1666 else
1667 {
1668 USBHUB_DumpingIDs(Id);
1669 }
1670
1671 break;
1672
1673 case BusQueryCompatibleIDs:
1674 DPRINT("USBH_PdoQueryId: BusQueryCompatibleIDs\n");
1675
1676 if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_INIT_PORT_FAILED)
1677 {
1678 DPRINT("USBH_PdoQueryId: USBHUB_PDO_FLAG_INIT_PORT_FAILED\n");
1679
1680 RtlStringCbPrintfExW(Buffer,
1681 Remaining,
1682 NULL,
1683 &Remaining,
1684 0,
1685 L"USB\\UNKNOWN");
1686 }
1687 else if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_MULTI_INTERFACE)
1688 {
1689 RtlStringCbPrintfExW(Buffer,
1690 Remaining,
1691 &EndBuffer,
1692 &Remaining,
1693 0,
1694 L"USB\\DevClass_%02x&SubClass_%02x&Prot_%02x",
1695 InterfaceDescriptor->bInterfaceClass,
1696 InterfaceDescriptor->bInterfaceSubClass,
1697 InterfaceDescriptor->bInterfaceProtocol);
1698
1699 EndBuffer++;
1700 Remaining -= sizeof(UNICODE_NULL);
1701
1702 RtlStringCbPrintfExW(EndBuffer,
1703 Remaining,
1704 &EndBuffer,
1705 &Remaining,
1706 0,
1707 L"USB\\DevClass_%02x&SubClass_%02x",
1708 InterfaceDescriptor->bInterfaceClass,
1709 InterfaceDescriptor->bInterfaceSubClass);
1710
1711 EndBuffer++;
1712 Remaining -= sizeof(UNICODE_NULL);
1713
1714 RtlStringCbPrintfExW(EndBuffer,
1715 Remaining,
1716 &EndBuffer,
1717 &Remaining,
1718 0,
1719 L"USB\\DevClass_%02x",
1720 InterfaceDescriptor->bInterfaceClass);
1721
1722 EndBuffer++;
1723 Remaining -= sizeof(UNICODE_NULL);
1724
1725 RtlStringCbPrintfExW(EndBuffer,
1726 Remaining,
1727 NULL,
1728 &Remaining,
1729 0,
1730 L"USB\\COMPOSITE");
1731 }
1732 else
1733 {
1734 RtlStringCbPrintfExW(Buffer,
1735 Remaining,
1736 &EndBuffer,
1737 &Remaining,
1738 0,
1739 L"USB\\Class_%02x&SubClass_%02x&Prot_%02x",
1740 InterfaceDescriptor->bInterfaceClass,
1741 InterfaceDescriptor->bInterfaceSubClass,
1742 InterfaceDescriptor->bInterfaceProtocol);
1743
1744 EndBuffer++;
1745 Remaining -= sizeof(UNICODE_NULL);
1746
1747 RtlStringCbPrintfExW(EndBuffer,
1748 Remaining,
1749 &EndBuffer,
1750 &Remaining,
1751 0,
1752 L"USB\\Class_%02x&SubClass_%02x",
1753 InterfaceDescriptor->bInterfaceClass,
1754 InterfaceDescriptor->bInterfaceSubClass);
1755
1756 EndBuffer++;
1757 Remaining -= sizeof(UNICODE_NULL);
1758
1759 RtlStringCbPrintfExW(EndBuffer,
1760 Remaining,
1761 NULL,
1762 &Remaining,
1763 0,
1764 L"USB\\Class_%02x",
1765 InterfaceDescriptor->bInterfaceClass);
1766 }
1767
1768 Length = sizeof(Buffer) - (Remaining - sizeof(UNICODE_NULL));
1769
1770 Id = ExAllocatePoolWithTag(PagedPool, Length, USB_HUB_TAG);
1771
1772 if (!Id)
1773 {
1774 break;
1775 }
1776
1777 RtlCopyMemory(Id, Buffer, Length);
1778
1779 if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_INIT_PORT_FAILED)
1780 {
1781 DPRINT("USBH_PdoQueryId: BusQueryInstanceID - %S\n", Id);
1782 }
1783 else
1784 {
1785 USBHUB_DumpingIDs(Id);
1786 }
1787
1788 break;
1789
1790 case BusQueryInstanceID:
1791 DPRINT("USBH_PdoQueryId: BusQueryInstanceID\n");
1792
1793 if (PortExtension->SerialNumber)
1794 {
1795 Id = ExAllocatePoolWithTag(PagedPool,
1796 PortExtension->SN_DescriptorLength,
1797 USB_HUB_TAG);
1798
1799 if (Id)
1800 {
1801 RtlZeroMemory(Id, PortExtension->SN_DescriptorLength);
1802
1803 RtlCopyMemory(Id,
1804 PortExtension->SerialNumber,
1805 PortExtension->SN_DescriptorLength);
1806 }
1807 }
1808 else
1809 {
1810 Length = sizeof(PortExtension->InstanceID) +
1811 sizeof(UNICODE_NULL);
1812
1813 Id = ExAllocatePoolWithTag(PagedPool, Length, USB_HUB_TAG);
1814
1815 if (Id)
1816 {
1817 RtlZeroMemory(Id, Length);
1818
1819 RtlCopyMemory(Id,
1820 PortExtension->InstanceID,
1821 sizeof(PortExtension->InstanceID));
1822 }
1823 }
1824
1825 DPRINT("USBH_PdoQueryId: BusQueryInstanceID - %S\n", Id);
1826 break;
1827
1828 default:
1829 DPRINT1("USBH_PdoQueryId: unknown query id type 0x%lx\n", IdType);
1830 return Irp->IoStatus.Status;
1831 }
1832
1833 Irp->IoStatus.Information = (ULONG_PTR)Id;
1834
1835 if (!Id)
1836 {
1837 Status = STATUS_INSUFFICIENT_RESOURCES;
1838 }
1839
1840 return Status;
1841 }
1842
1843 NTSTATUS
1844 NTAPI
1845 USBH_PdoQueryDeviceText(IN PUSBHUB_PORT_PDO_EXTENSION PortExtension,
1846 IN PIRP Irp)
1847 {
1848 PDEVICE_OBJECT DeviceObject;
1849 PIO_STACK_LOCATION IoStack;
1850 DEVICE_TEXT_TYPE DeviceTextType;
1851 USHORT LanguageId;
1852 USHORT DefaultId;
1853 PUSB_STRING_DESCRIPTOR Descriptor;
1854 PWCHAR DeviceText;
1855 UCHAR iProduct = 0;
1856 NTSTATUS Status;
1857 ULONG NumSymbols;
1858 ULONG Length;
1859
1860 DPRINT("USBH_PdoQueryDeviceText ... \n");
1861
1862 DeviceObject = PortExtension->Common.SelfDevice;
1863 IoStack = IoGetCurrentIrpStackLocation(Irp);
1864 DeviceTextType = IoStack->Parameters.QueryDeviceText.DeviceTextType;
1865
1866 if (DeviceTextType != DeviceTextDescription &&
1867 DeviceTextType != DeviceTextLocationInformation)
1868 {
1869 return Irp->IoStatus.Status;
1870 }
1871
1872 LanguageId = LANGIDFROMLCID(IoStack->Parameters.QueryDeviceText.LocaleId);
1873 DefaultId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
1874
1875 if (!LanguageId)
1876 {
1877 LanguageId = DefaultId;
1878 }
1879
1880 iProduct = PortExtension->DeviceDescriptor.iProduct;
1881
1882 if (PortExtension->DeviceHandle && iProduct &&
1883 !PortExtension->IgnoringHwSerial &&
1884 !(PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_INIT_PORT_FAILED))
1885 {
1886 Descriptor = ExAllocatePoolWithTag(NonPagedPool,
1887 MAXIMUM_USB_STRING_LENGTH,
1888 USB_HUB_TAG);
1889
1890 if (Descriptor)
1891 {
1892 RtlZeroMemory(Descriptor, MAXIMUM_USB_STRING_LENGTH);
1893
1894 for (Status = USBH_CheckDeviceLanguage(DeviceObject, LanguageId);
1895 ;
1896 Status = USBH_CheckDeviceLanguage(DeviceObject, DefaultId))
1897 {
1898 if (NT_SUCCESS(Status))
1899 {
1900 Status = USBH_SyncGetStringDescriptor(DeviceObject,
1901 iProduct,
1902 LanguageId,
1903 Descriptor,
1904 MAXIMUM_USB_STRING_LENGTH,
1905 NULL,
1906 TRUE);
1907
1908 if (NT_SUCCESS(Status))
1909 {
1910 break;
1911 }
1912 }
1913
1914 if (LanguageId == DefaultId)
1915 {
1916 goto Exit;
1917 }
1918
1919 LanguageId = DefaultId;
1920 }
1921
1922 if (Descriptor->bLength <= sizeof(USB_COMMON_DESCRIPTOR))
1923 {
1924 Status = STATUS_UNSUCCESSFUL;
1925 }
1926
1927 if (NT_SUCCESS(Status))
1928 {
1929 Length = Descriptor->bLength -
1930 FIELD_OFFSET(USB_STRING_DESCRIPTOR, bString);
1931
1932 DeviceText = ExAllocatePoolWithTag(PagedPool,
1933 Length + sizeof(UNICODE_NULL),
1934 USB_HUB_TAG);
1935
1936 if (DeviceText)
1937 {
1938 RtlZeroMemory(DeviceText, Length + sizeof(UNICODE_NULL));
1939
1940 RtlCopyMemory(DeviceText, Descriptor->bString, Length);
1941
1942 Irp->IoStatus.Information = (ULONG_PTR)DeviceText;
1943
1944 DPRINT("USBH_PdoQueryDeviceText: Descriptor->bString - %S\n",
1945 DeviceText);
1946 }
1947 else
1948 {
1949 Status = STATUS_INSUFFICIENT_RESOURCES;
1950 }
1951 }
1952
1953 Exit:
1954
1955 ExFreePoolWithTag(Descriptor, USB_HUB_TAG);
1956
1957 if (NT_SUCCESS(Status))
1958 {
1959 return Status;
1960 }
1961 }
1962 else
1963 {
1964 Status = STATUS_INSUFFICIENT_RESOURCES;
1965 }
1966 }
1967 else
1968 {
1969 Status = STATUS_NOT_SUPPORTED;
1970 }
1971
1972 if (!GenericUSBDeviceString)
1973 {
1974 return Status;
1975 }
1976
1977 NumSymbols = wcslen(GenericUSBDeviceString);
1978 Length = (NumSymbols + 1) * sizeof(WCHAR);
1979
1980 DeviceText = ExAllocatePoolWithTag(PagedPool, Length, USB_HUB_TAG);
1981
1982 if (!DeviceText)
1983 {
1984 return STATUS_INSUFFICIENT_RESOURCES;
1985 }
1986
1987 RtlZeroMemory(DeviceText, Length);
1988
1989 RtlCopyMemory(DeviceText,
1990 GenericUSBDeviceString,
1991 NumSymbols * sizeof(WCHAR));
1992
1993 Irp->IoStatus.Information = (ULONG_PTR)DeviceText;
1994
1995 return STATUS_SUCCESS;
1996 }
1997
1998 NTSTATUS
1999 NTAPI
2000 USBH_SymbolicLink(IN PUSBHUB_PORT_PDO_EXTENSION PortExtension,
2001 IN const GUID * InterfaceClassGuid,
2002 IN BOOLEAN IsEnable)
2003 {
2004 NTSTATUS Status = STATUS_SUCCESS;
2005 PVOID NameBuffer;
2006
2007 DPRINT("USBH_SymbolicLink ... \n");
2008
2009 if (IsEnable)
2010 {
2011 Status = IoRegisterDeviceInterface(PortExtension->Common.SelfDevice,
2012 InterfaceClassGuid,
2013 NULL,
2014 &PortExtension->SymbolicLinkName);
2015
2016 if (NT_SUCCESS(Status))
2017 {
2018 USBH_SetPdoRegistryParameter(PortExtension->Common.SelfDevice,
2019 L"SymbolicName",
2020 PortExtension->SymbolicLinkName.Buffer,
2021 PortExtension->SymbolicLinkName.Length,
2022 REG_SZ,
2023 PLUGPLAY_REGKEY_DEVICE);
2024
2025 Status = IoSetDeviceInterfaceState(&PortExtension->SymbolicLinkName,
2026 TRUE);
2027 }
2028 }
2029 else
2030 {
2031 NameBuffer = PortExtension->SymbolicLinkName.Buffer;
2032
2033 if (NameBuffer)
2034 {
2035 Status = IoSetDeviceInterfaceState(&PortExtension->SymbolicLinkName,
2036 FALSE);
2037
2038 ExFreePool(PortExtension->SymbolicLinkName.Buffer);
2039
2040 PortExtension->SymbolicLinkName.Buffer = NULL;
2041 }
2042 }
2043
2044 return Status;
2045 }
2046
2047 NTSTATUS
2048 NTAPI
2049 USBH_RestoreDevice(IN PUSBHUB_PORT_PDO_EXTENSION PortExtension,
2050 IN BOOLEAN IsKeepDeviceData)
2051 {
2052 PUSBHUB_FDO_EXTENSION HubExtension;
2053 PUSBHUB_PORT_DATA PortData;
2054 NTSTATUS Status;
2055 ULONG ix;
2056
2057 DPRINT("USBH_RestoreDevice ... \n");
2058
2059 HubExtension = PortExtension->HubExtension;
2060
2061 if (!HubExtension)
2062 {
2063 Status = STATUS_UNSUCCESSFUL;
2064 return Status;
2065 }
2066
2067 ASSERT(PortExtension->PortNumber > 0);
2068 PortData = &HubExtension->PortData[PortExtension->PortNumber - 1];
2069
2070 if (PortExtension->Common.SelfDevice == PortData->DeviceObject)
2071 {
2072 Status = STATUS_UNSUCCESSFUL;
2073 return Status;
2074 }
2075
2076 Status = USBH_SyncGetPortStatus(HubExtension,
2077 PortExtension->PortNumber,
2078 &PortData->PortStatus,
2079 sizeof(USB_PORT_STATUS_AND_CHANGE));
2080
2081 if (NT_SUCCESS(Status))
2082 {
2083 for (ix = 0; ix < 3; ix++)
2084 {
2085 Status = USBH_ResetDevice((PUSBHUB_FDO_EXTENSION)HubExtension,
2086 PortExtension->PortNumber,
2087 IsKeepDeviceData,
2088 ix == 0);
2089
2090 if (NT_SUCCESS(Status) || Status == STATUS_NO_SUCH_DEVICE)
2091 {
2092 break;
2093 }
2094
2095 USBH_Wait(1000);
2096 }
2097 }
2098
2099 PortExtension->PortPdoFlags &= ~USBHUB_PDO_FLAG_POWER_D3;
2100
2101 if (NT_SUCCESS(Status))
2102 {
2103 PortExtension->PortPdoFlags &= ~USBHUB_PDO_FLAG_PORT_RESTORE_FAIL;
2104 }
2105 else
2106 {
2107 PortExtension->PortPdoFlags |= (USBHUB_PDO_FLAG_INIT_PORT_FAILED |
2108 USBHUB_PDO_FLAG_PORT_RESTORE_FAIL);
2109 }
2110
2111 return Status;
2112 }
2113
2114 NTSTATUS
2115 NTAPI
2116 USBH_PdoStartDevice(IN PUSBHUB_PORT_PDO_EXTENSION PortExtension,
2117 IN PIRP Irp)
2118 {
2119 PUSBHUB_FDO_EXTENSION HubExtension;
2120 const GUID * Guid;
2121 NTSTATUS Status;
2122
2123 DPRINT("USBH_PdoStartDevice: PortExtension - %p\n", PortExtension);
2124
2125 if (!PortExtension->HubExtension &&
2126 PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_POWER_D3)
2127 {
2128 PortExtension->HubExtension = PortExtension->RootHubExtension;
2129 }
2130
2131 HubExtension = PortExtension->HubExtension;
2132
2133 if (HubExtension)
2134 {
2135 USBHUB_SetDeviceHandleData(HubExtension,
2136 PortExtension->Common.SelfDevice,
2137 PortExtension->DeviceHandle);
2138 }
2139
2140 if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_HUB_DEVICE)
2141 {
2142 Guid = &GUID_DEVINTERFACE_USB_HUB;
2143 }
2144 else
2145 {
2146 Guid = &GUID_DEVINTERFACE_USB_DEVICE;
2147 }
2148
2149 Status = USBH_SymbolicLink(PortExtension, Guid, TRUE);
2150
2151 if (NT_SUCCESS(Status))
2152 {
2153 PortExtension->PortPdoFlags |= USBHUB_PDO_FLAG_REG_DEV_INTERFACE;
2154 }
2155
2156 if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_POWER_D3)
2157 {
2158 Status = USBH_RestoreDevice(PortExtension, 0);
2159 }
2160
2161 PortExtension->PortPdoFlags |= USBHUB_PDO_FLAG_DEVICE_STARTED;
2162
2163 PortExtension->CurrentPowerState.DeviceState = PowerDeviceD0;
2164
2165 DPRINT1("USBH_PdoStartDevice: call IoWMIRegistrationControl UNIMPLEMENTED. FIXME\n");
2166
2167 return Status;
2168 }
2169
2170 NTSTATUS
2171 NTAPI
2172 USBH_PdoRemoveDevice(IN PUSBHUB_PORT_PDO_EXTENSION PortExtension,
2173 IN PUSBHUB_FDO_EXTENSION HubExtension)
2174 {
2175 NTSTATUS Status = STATUS_SUCCESS;
2176 PDEVICE_OBJECT PortDevice;
2177 PUSBHUB_PORT_PDO_EXTENSION PortExt;
2178 PUSBHUB_PORT_DATA PortData;
2179 PIRP IdleNotificationIrp;
2180 PIRP WakeIrp;
2181 PVOID DeviceHandle;
2182 PDEVICE_OBJECT Pdo;
2183 PVOID SerialNumber;
2184 USHORT Port;
2185 KIRQL Irql;
2186
2187 DPRINT("USBH_PdoRemoveDevice ... \n");
2188
2189 PortDevice = PortExtension->Common.SelfDevice;
2190 PortExtension->HubExtension = NULL;
2191
2192 Port = PortExtension->PortNumber;
2193 ASSERT(Port > 0);
2194
2195 ASSERT(HubExtension);
2196
2197 if (HubExtension->CurrentPowerState.DeviceState != PowerDeviceD0 &&
2198 (HubExtension->HubFlags & USBHUB_FDO_FLAG_DEVICE_STARTED) != 0)
2199 {
2200 USBH_HubSetD0(HubExtension);
2201 }
2202
2203 IoAcquireCancelSpinLock(&Irql);
2204 IdleNotificationIrp = PortExtension->IdleNotificationIrp;
2205
2206 if (IdleNotificationIrp)
2207 {
2208 PortExtension->IdleNotificationIrp = NULL;
2209 PortExtension->PortPdoFlags &= ~USBHUB_PDO_FLAG_IDLE_NOTIFICATION;
2210
2211 if (IdleNotificationIrp->Cancel)
2212 {
2213 IdleNotificationIrp = NULL;
2214 }
2215
2216 if (IdleNotificationIrp)
2217 {
2218 IoSetCancelRoutine(IdleNotificationIrp, NULL);
2219 }
2220 }
2221
2222 WakeIrp = PortExtension->PdoWaitWakeIrp;
2223
2224 if (WakeIrp)
2225 {
2226 PortExtension->PdoWaitWakeIrp = NULL;
2227 PortExtension->PortPdoFlags &= ~USBHUB_PDO_FLAG_WAIT_WAKE;
2228
2229 if (WakeIrp->Cancel || !IoSetCancelRoutine(WakeIrp, NULL))
2230 {
2231 WakeIrp = NULL;
2232
2233 if (!InterlockedDecrement(&HubExtension->PendingRequestCount))
2234 {
2235 KeSetEvent(&HubExtension->PendingRequestEvent,
2236 EVENT_INCREMENT,
2237 FALSE);
2238 }
2239 }
2240 }
2241
2242 IoReleaseCancelSpinLock(Irql);
2243
2244 if (IdleNotificationIrp)
2245 {
2246 IdleNotificationIrp->IoStatus.Status = STATUS_CANCELLED;
2247 IoCompleteRequest(IdleNotificationIrp, IO_NO_INCREMENT);
2248 }
2249
2250 if (WakeIrp)
2251 {
2252 USBH_CompletePowerIrp(HubExtension, WakeIrp, STATUS_CANCELLED);
2253 }
2254
2255 PortExtension->PortPdoFlags |= USBHUB_PDO_FLAG_POWER_D3;
2256
2257 if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_REG_DEV_INTERFACE)
2258 {
2259 Status = USBH_SymbolicLink(PortExtension, NULL, FALSE);
2260
2261 if (NT_SUCCESS(Status))
2262 {
2263 PortExtension->PortPdoFlags &= ~USBHUB_PDO_FLAG_REG_DEV_INTERFACE;
2264 }
2265 }
2266
2267 DeviceHandle = InterlockedExchangePointer(&PortExtension->DeviceHandle,
2268 NULL);
2269
2270 if (DeviceHandle)
2271 {
2272 Status = USBD_RemoveDeviceEx(HubExtension, DeviceHandle, 0);
2273
2274 if (HubExtension->PortData &&
2275 HubExtension->PortData[Port - 1].DeviceObject == PortDevice)
2276 {
2277 USBH_SyncDisablePort(HubExtension, Port);
2278 }
2279 }
2280
2281 if (NT_SUCCESS(Status))
2282 {
2283 PortExtension->PortPdoFlags &= ~USBHUB_PDO_FLAG_DEVICE_STARTED;
2284
2285 if (HubExtension->PortData)
2286 {
2287 PortData = &HubExtension->PortData[Port - 1];
2288
2289 if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_DELETE_PENDING)
2290 {
2291 Pdo = PortData->DeviceObject;
2292
2293 if (Pdo)
2294 {
2295 PortData->DeviceObject = NULL;
2296 PortData->ConnectionStatus = NoDeviceConnected;
2297
2298 if (PdoExt(Pdo)->EnumFlags & USBHUB_ENUM_FLAG_DEVICE_PRESENT)
2299 {
2300 PortExt = PdoExt(Pdo);
2301
2302 InsertTailList(&HubExtension->PdoList,
2303 &PortExt->PortLink);
2304 }
2305 }
2306 }
2307 }
2308
2309 if (!(PortExtension->EnumFlags & USBHUB_ENUM_FLAG_DEVICE_PRESENT) &&
2310 !(PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_NOT_CONNECTED))
2311 {
2312 PortExtension->PortPdoFlags |= USBHUB_PDO_FLAG_NOT_CONNECTED;
2313
2314 SerialNumber = InterlockedExchangePointer((PVOID)&PortExtension->SerialNumber,
2315 NULL);
2316
2317 if (SerialNumber)
2318 {
2319 ExFreePoolWithTag(SerialNumber, USB_HUB_TAG);
2320 }
2321
2322 DPRINT1("USBH_PdoRemoveDevice: call IoWMIRegistrationControl UNIMPLEMENTED. FIXME\n");
2323
2324 USBHUB_FlushAllTransfers(HubExtension);
2325
2326 IoDeleteDevice(PortDevice);
2327 }
2328 }
2329
2330 DPRINT("USBH_PdoRemoveDevice: call USBH_CheckIdleDeferred()\n");
2331 USBH_CheckIdleDeferred(HubExtension);
2332
2333 return Status;
2334 }
2335
2336 NTSTATUS
2337 NTAPI
2338 USBH_PdoStopDevice(IN PUSBHUB_PORT_PDO_EXTENSION PortExtension,
2339 IN PIRP Irp)
2340 {
2341 DPRINT1("USBH_PdoStopDevice: UNIMPLEMENTED. FIXME\n");
2342 DbgBreakPoint();
2343 return STATUS_SUCCESS;
2344 }
2345
2346 NTSTATUS
2347 NTAPI
2348 USBH_FdoPnP(IN PUSBHUB_FDO_EXTENSION HubExtension,
2349 IN PIRP Irp,
2350 IN UCHAR Minor)
2351 {
2352 NTSTATUS Status;
2353 PIO_STACK_LOCATION IoStack;
2354 DEVICE_RELATION_TYPE RelationsType;
2355 BOOLEAN IsCheckIdle;
2356
2357 DPRINT_PNP("USBH_FdoPnP: HubExtension - %p, Irp - %p, Minor - %X\n",
2358 HubExtension,
2359 Irp,
2360 Minor);
2361
2362 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_WAIT_IDLE_REQUEST &&
2363 (Minor == IRP_MN_REMOVE_DEVICE || Minor == IRP_MN_STOP_DEVICE))
2364 {
2365 HubExtension->HubFlags |= USBHUB_FDO_FLAG_DEVICE_SUSPENDED;
2366 }
2367
2368 KeWaitForSingleObject(&HubExtension->IdleSemaphore,
2369 Executive,
2370 KernelMode,
2371 FALSE,
2372 NULL);
2373
2374 DPRINT_PNP("USBH_FdoPnP: HubFlags - %lX\n", HubExtension->HubFlags);
2375
2376 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_GOING_IDLE)
2377 {
2378 HubExtension->HubFlags |= USBHUB_FDO_FLAG_DEVICE_SUSPENDED;
2379 }
2380
2381 IoStack = IoGetCurrentIrpStackLocation(Irp);
2382 RelationsType = IoStack->Parameters.QueryDeviceRelations.Type;
2383
2384 if ((HubExtension->CurrentPowerState.DeviceState == PowerDeviceD0) ||
2385 !(HubExtension->HubFlags & (USBHUB_FDO_FLAG_DEVICE_STOPPED | USBHUB_FDO_FLAG_DEVICE_STARTED)) ||
2386 (Minor == IRP_MN_QUERY_DEVICE_RELATIONS && RelationsType == TargetDeviceRelation))
2387 {
2388 IsCheckIdle = FALSE;
2389 }
2390 else
2391 {
2392 DPRINT_PNP("USBH_FdoPnP: IsCheckIdle - TRUE\n");
2393 IsCheckIdle = TRUE;
2394 USBH_HubSetD0(HubExtension);
2395 }
2396
2397 switch (Minor)
2398 {
2399 case IRP_MN_START_DEVICE:
2400 DPRINT_PNP("FDO IRP_MN_START_DEVICE\n");
2401 IsCheckIdle = FALSE;
2402 Irp->IoStatus.Status = STATUS_SUCCESS;
2403 Status = USBH_FdoStartDevice(HubExtension, Irp);
2404 break;
2405
2406 case IRP_MN_QUERY_REMOVE_DEVICE:
2407 DPRINT_PNP("FDO IRP_MN_QUERY_REMOVE_DEVICE\n");
2408 Irp->IoStatus.Status = STATUS_SUCCESS;
2409 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2410 break;
2411
2412 case IRP_MN_REMOVE_DEVICE:
2413 DPRINT_PNP("FDO IRP_MN_REMOVE_DEVICE\n");
2414 IsCheckIdle = FALSE;
2415 HubExtension->HubFlags |= USBHUB_FDO_FLAG_DEVICE_REMOVED;
2416 Irp->IoStatus.Status = STATUS_SUCCESS;
2417 Status = USBH_FdoRemoveDevice(HubExtension, Irp);
2418 break;
2419
2420 case IRP_MN_CANCEL_REMOVE_DEVICE:
2421 DPRINT_PNP("FDO IRP_MN_CANCEL_REMOVE_DEVICE\n");
2422 Irp->IoStatus.Status = STATUS_SUCCESS;
2423 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2424 break;
2425
2426 case IRP_MN_STOP_DEVICE:
2427 DPRINT_PNP("FDO IRP_MN_STOP_DEVICE\n");
2428 IsCheckIdle = FALSE;
2429 Irp->IoStatus.Status = STATUS_SUCCESS;
2430 Status = USBH_FdoStopDevice(HubExtension, Irp);
2431 break;
2432
2433 case IRP_MN_QUERY_STOP_DEVICE:
2434 DPRINT_PNP("FDO IRP_MN_QUERY_STOP_DEVICE\n");
2435 Irp->IoStatus.Status = STATUS_SUCCESS;
2436 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2437 break;
2438
2439 case IRP_MN_CANCEL_STOP_DEVICE:
2440 DPRINT_PNP("FDO IRP_MN_CANCEL_STOP_DEVICE\n");
2441 Irp->IoStatus.Status = STATUS_SUCCESS;
2442 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2443 break;
2444
2445 case IRP_MN_QUERY_DEVICE_RELATIONS:
2446 DPRINT_PNP("FDO IRP_MN_QUERY_DEVICE_RELATIONS\n");
2447
2448 if (RelationsType != BusRelations)
2449 {
2450 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2451 break;
2452 }
2453
2454 HubExtension->HubFlags |= USBHUB_FDO_FLAG_HUB_BUSY;
2455
2456 IsCheckIdle = TRUE;
2457 DPRINT_PNP("USBH_FdoPnP: IsCheckIdle - TRUE\n");
2458
2459 Status = USBH_FdoQueryBusRelations(HubExtension, Irp);
2460
2461 HubExtension->HubFlags &= ~USBHUB_FDO_FLAG_HUB_BUSY;
2462 break;
2463
2464 case IRP_MN_QUERY_INTERFACE:
2465 DPRINT_PNP("FDO IRP_MN_QUERY_INTERFACE\n");
2466 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2467 break;
2468
2469 case IRP_MN_QUERY_CAPABILITIES:
2470 DPRINT_PNP("FDO IRP_MN_QUERY_CAPABILITIES\n");
2471 IoCopyCurrentIrpStackLocationToNext(Irp);
2472
2473 IoSetCompletionRoutine(Irp,
2474 USBH_QueryCapsComplete,
2475 HubExtension,
2476 TRUE,
2477 FALSE,
2478 FALSE);
2479
2480 Status = IoCallDriver(HubExtension->LowerDevice, Irp);
2481 break;
2482
2483 case IRP_MN_QUERY_RESOURCES:
2484 DPRINT_PNP("FDO IRP_MN_QUERY_RESOURCES\n");
2485 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2486 break;
2487
2488 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
2489 DPRINT_PNP("FDO IRP_MN_QUERY_RESOURCE_REQUIREMENTS\n");
2490 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2491 break;
2492
2493 case IRP_MN_QUERY_DEVICE_TEXT:
2494 DPRINT_PNP("FDO IRP_MN_QUERY_DEVICE_TEXT\n");
2495 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2496 break;
2497
2498 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
2499 DPRINT_PNP("FDO IRP_MN_FILTER_RESOURCE_REQUIREMENTS\n");
2500 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2501 break;
2502
2503 case IRP_MN_READ_CONFIG:
2504 DPRINT_PNP("FDO IRP_MN_READ_CONFIG\n");
2505 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2506 break;
2507
2508 case IRP_MN_WRITE_CONFIG:
2509 DPRINT_PNP("FDO IRP_MN_WRITE_CONFIG\n");
2510 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2511 break;
2512
2513 case IRP_MN_EJECT:
2514 DPRINT_PNP("FDO IRP_MN_EJECT\n");
2515 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2516 break;
2517
2518 case IRP_MN_SET_LOCK:
2519 DPRINT_PNP("FDO IRP_MN_SET_LOCK\n");
2520 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2521 break;
2522
2523 case IRP_MN_QUERY_ID:
2524 DPRINT_PNP("FDO IRP_MN_QUERY_ID\n");
2525 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2526 break;
2527
2528 case IRP_MN_QUERY_PNP_DEVICE_STATE:
2529 DPRINT_PNP("FDO IRP_MN_QUERY_PNP_DEVICE_STATE\n");
2530
2531 if (HubExtension->HubFlags & USBHUB_FDO_FLAG_DEVICE_FAILED)
2532 {
2533 Irp->IoStatus.Information |= PNP_DEVICE_FAILED;
2534 }
2535
2536 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2537 break;
2538
2539 case IRP_MN_QUERY_BUS_INFORMATION:
2540 DPRINT_PNP("FDO IRP_MN_QUERY_BUS_INFORMATION\n");
2541 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2542 break;
2543
2544 case IRP_MN_DEVICE_USAGE_NOTIFICATION:
2545 DPRINT_PNP("FDO IRP_MN_DEVICE_USAGE_NOTIFICATION\n");
2546 Irp->IoStatus.Status = STATUS_SUCCESS;
2547 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2548 break;
2549
2550 case IRP_MN_SURPRISE_REMOVAL:
2551 DPRINT_PNP("FDO IRP_MN_SURPRISE_REMOVAL\n");
2552 USBH_FdoSurpriseRemoveDevice(HubExtension, Irp);
2553 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2554 break;
2555
2556 default:
2557 DPRINT_PNP("FDO unknown IRP_MN_???\n");
2558 Status = USBH_PassIrp(HubExtension->LowerDevice, Irp);
2559 break;
2560 }
2561
2562 KeReleaseSemaphore(&HubExtension->IdleSemaphore,
2563 LOW_REALTIME_PRIORITY,
2564 1,
2565 FALSE);
2566
2567 if (IsCheckIdle)
2568 {
2569 DPRINT_PNP("USBH_FdoPnP: call USBH_CheckIdleDeferred()\n");
2570 USBH_CheckIdleDeferred(HubExtension);
2571 }
2572
2573 HubExtension->HubFlags &= ~USBHUB_FDO_FLAG_STATE_CHANGING;
2574
2575 return Status;
2576 }
2577
2578 NTSTATUS
2579 NTAPI
2580 USBH_PdoPnP(IN PUSBHUB_PORT_PDO_EXTENSION PortExtension,
2581 IN PIRP Irp,
2582 IN UCHAR Minor,
2583 OUT BOOLEAN * IsCompleteIrp)
2584 {
2585 NTSTATUS Status;
2586 PIO_STACK_LOCATION IoStack;
2587 PPNP_BUS_INFORMATION BusInfo;
2588 PDEVICE_CAPABILITIES DeviceCapabilities;
2589 USHORT Size;
2590 USHORT Version;
2591 PUSBHUB_FDO_EXTENSION HubExtension;
2592 PDEVICE_RELATIONS DeviceRelation;
2593
2594 DPRINT_PNP("USBH_PdoPnP: PortExtension - %p, Irp - %p, Minor - %X\n",
2595 PortExtension,
2596 Irp,
2597 Minor);
2598
2599 IoStack = IoGetCurrentIrpStackLocation(Irp);
2600
2601 *IsCompleteIrp = TRUE;
2602
2603 switch (Minor)
2604 {
2605 case IRP_MN_START_DEVICE:
2606 DPRINT_PNP("PDO IRP_MN_START_DEVICE\n");
2607 return USBH_PdoStartDevice(PortExtension, Irp);
2608
2609 case IRP_MN_QUERY_REMOVE_DEVICE:
2610 DPRINT_PNP("PDO IRP_MN_QUERY_REMOVE_DEVICE\n");
2611 return STATUS_SUCCESS;
2612
2613 case IRP_MN_REMOVE_DEVICE:
2614 DPRINT_PNP("PDO IRP_MN_REMOVE_DEVICE\n");
2615 return USBH_PdoRemoveDevice(PortExtension, PortExtension->HubExtension);
2616
2617 case IRP_MN_CANCEL_REMOVE_DEVICE:
2618 DPRINT_PNP("PDO IRP_MN_CANCEL_REMOVE_DEVICE\n");
2619 return STATUS_SUCCESS;
2620
2621 case IRP_MN_STOP_DEVICE:
2622 DPRINT_PNP("PDO IRP_MN_STOP_DEVICE\n");
2623 return USBH_PdoStopDevice(PortExtension, Irp);
2624
2625 case IRP_MN_QUERY_STOP_DEVICE:
2626 DPRINT_PNP("PDO IRP_MN_QUERY_STOP_DEVICE\n");
2627 return STATUS_SUCCESS;
2628
2629 case IRP_MN_CANCEL_STOP_DEVICE:
2630 DPRINT_PNP("PDO IRP_MN_CANCEL_STOP_DEVICE\n");
2631 return STATUS_SUCCESS;
2632
2633 case IRP_MN_QUERY_DEVICE_RELATIONS:
2634 DPRINT_PNP("PDO IRP_MN_QUERY_DEVICE_RELATIONS\n");
2635
2636 if (IoStack->Parameters.QueryDeviceRelations.Type != TargetDeviceRelation)
2637 {
2638 return Irp->IoStatus.Status;
2639 }
2640
2641 DeviceRelation = ExAllocatePoolWithTag(PagedPool,
2642 sizeof(DEVICE_RELATIONS),
2643 USB_HUB_TAG);
2644
2645 if (DeviceRelation)
2646 {
2647 RtlZeroMemory(DeviceRelation, sizeof(DEVICE_RELATIONS));
2648
2649 DeviceRelation->Count = 1;
2650 DeviceRelation->Objects[0] = PortExtension->Common.SelfDevice;
2651
2652 ObReferenceObject(DeviceRelation->Objects[0]);
2653
2654 Status = STATUS_SUCCESS;
2655 }
2656 else
2657 {
2658 Status = STATUS_INSUFFICIENT_RESOURCES;
2659 }
2660
2661 Irp->IoStatus.Information = (ULONG_PTR)DeviceRelation;
2662 break;
2663
2664 case IRP_MN_QUERY_INTERFACE:
2665 DPRINT_PNP("PDO IRP_MN_QUERY_INTERFACE\n");
2666
2667 *IsCompleteIrp = 0;
2668
2669 if (IsEqualGUIDAligned(IoStack->Parameters.QueryInterface.InterfaceType,
2670 &USB_BUS_INTERFACE_USBDI_GUID))
2671 {
2672 IoStack->Parameters.QueryInterface.InterfaceSpecificData = PortExtension->DeviceHandle;
2673 }
2674
2675 HubExtension = PortExtension->HubExtension;
2676
2677 if (!HubExtension)
2678 {
2679 HubExtension = PortExtension->RootHubExtension;
2680 }
2681
2682 Status = USBH_PassIrp(HubExtension->RootHubPdo, Irp);
2683 break;
2684
2685 case IRP_MN_QUERY_CAPABILITIES:
2686 DPRINT_PNP("PDO IRP_MN_QUERY_CAPABILITIES\n");
2687
2688 DeviceCapabilities = IoStack->Parameters.DeviceCapabilities.Capabilities;
2689
2690 Size = DeviceCapabilities->Size;
2691 Version = DeviceCapabilities->Version;
2692
2693 RtlCopyMemory(DeviceCapabilities,
2694 &PortExtension->Capabilities,
2695 sizeof(DEVICE_CAPABILITIES));
2696
2697 DeviceCapabilities->Size = Size;
2698 DeviceCapabilities->Version = Version;
2699
2700 Status = STATUS_SUCCESS;
2701 break;
2702
2703 case IRP_MN_QUERY_RESOURCES:
2704 DPRINT_PNP("PDO IRP_MN_QUERY_RESOURCES\n");
2705 Status = Irp->IoStatus.Status;
2706 break;
2707
2708 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
2709 DPRINT_PNP("PDO IRP_MN_QUERY_RESOURCE_REQUIREMENTS\n");
2710 PortExtension->PortPdoFlags |= USBHUB_PDO_FLAG_ENUMERATED;
2711
2712 /* FIXME HKEY_LOCAL_MACHINE\SYSTEM\ControlSetXXX\Enum\USB\
2713 Vid_????&Pid_????\????????????\Device Parameters\
2714 if (ExtPropDescSemaphore)
2715 */
2716
2717 Status = STATUS_SUCCESS;
2718 break;
2719
2720 case IRP_MN_QUERY_DEVICE_TEXT:
2721 DPRINT_PNP("PDO IRP_MN_QUERY_DEVICE_TEXT\n");
2722 return USBH_PdoQueryDeviceText(PortExtension, Irp);
2723
2724 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
2725 DPRINT_PNP("PDO IRP_MN_FILTER_RESOURCE_REQUIREMENTS\n");
2726 Status = Irp->IoStatus.Status;
2727 break;
2728
2729 case IRP_MN_READ_CONFIG:
2730 DPRINT_PNP("PDO IRP_MN_READ_CONFIG\n");
2731 DbgBreakPoint();
2732 Status = Irp->IoStatus.Status;
2733 break;
2734
2735 case IRP_MN_WRITE_CONFIG:
2736 DPRINT_PNP("PDO IRP_MN_WRITE_CONFIG\n");
2737 DbgBreakPoint();
2738 Status = Irp->IoStatus.Status;
2739 break;
2740
2741 case IRP_MN_EJECT:
2742 DPRINT_PNP("PDO IRP_MN_EJECT\n");
2743 DbgBreakPoint();
2744 Status = Irp->IoStatus.Status;
2745 break;
2746
2747 case IRP_MN_SET_LOCK:
2748 DPRINT_PNP("PDO IRP_MN_SET_LOCK\n");
2749 DbgBreakPoint();
2750 Status = Irp->IoStatus.Status;
2751 break;
2752
2753 case IRP_MN_QUERY_ID:
2754 DPRINT_PNP("PDO IRP_MN_QUERY_ID\n");
2755 return USBH_PdoQueryId(PortExtension, Irp);
2756
2757 case IRP_MN_QUERY_PNP_DEVICE_STATE:
2758 DPRINT_PNP("PDO IRP_MN_QUERY_PNP_DEVICE_STATE\n");
2759 if (PortExtension->PortPdoFlags & (USBHUB_PDO_FLAG_INSUFFICIENT_PWR |
2760 USBHUB_PDO_FLAG_OVERCURRENT_PORT |
2761 USBHUB_PDO_FLAG_PORT_RESTORE_FAIL |
2762 USBHUB_PDO_FLAG_INIT_PORT_FAILED))
2763 {
2764 Irp->IoStatus.Information |= PNP_DEVICE_FAILED;
2765 }
2766
2767 Status = STATUS_SUCCESS;
2768 break;
2769
2770 case IRP_MN_QUERY_BUS_INFORMATION:
2771 DPRINT_PNP("PDO IRP_MN_QUERY_BUS_INFORMATION\n");
2772
2773 BusInfo = ExAllocatePoolWithTag(PagedPool,
2774 sizeof(PNP_BUS_INFORMATION),
2775 USB_HUB_TAG);
2776
2777 if (!BusInfo)
2778 {
2779 return STATUS_INSUFFICIENT_RESOURCES;
2780 }
2781
2782 RtlZeroMemory(BusInfo, sizeof(PNP_BUS_INFORMATION));
2783
2784 RtlCopyMemory(&BusInfo->BusTypeGuid,
2785 &GUID_BUS_TYPE_USB,
2786 sizeof(BusInfo->BusTypeGuid));
2787
2788 BusInfo->LegacyBusType = PNPBus;
2789 BusInfo->BusNumber = 0;
2790
2791 Irp->IoStatus.Information = (ULONG_PTR)BusInfo;
2792 Status = STATUS_SUCCESS;
2793 break;
2794
2795 case IRP_MN_DEVICE_USAGE_NOTIFICATION:
2796 DPRINT_PNP("PDO IRP_MN_DEVICE_USAGE_NOTIFICATION\n");
2797 DbgBreakPoint();
2798 Status = Irp->IoStatus.Status;
2799 break;
2800
2801 case IRP_MN_SURPRISE_REMOVAL:
2802 DPRINT_PNP("PDO IRP_MN_SURPRISE_REMOVAL\n");
2803 if (PortExtension->PortPdoFlags & USBHUB_PDO_FLAG_REG_DEV_INTERFACE)
2804 {
2805 Status = USBH_SymbolicLink(PortExtension, NULL, FALSE);
2806
2807 if (NT_SUCCESS(Status))
2808 {
2809 PortExtension->PortPdoFlags &= ~USBHUB_PDO_FLAG_REG_DEV_INTERFACE;
2810 }
2811 }
2812
2813 Status = STATUS_SUCCESS;
2814 break;
2815
2816 default:
2817 DPRINT_PNP("PDO unknown IRP_MN_???\n");
2818 Status = Irp->IoStatus.Status;
2819 break;
2820 }
2821
2822 return Status;
2823 }