ReactOS  0.4.14-dev-384-g5b37caa
usbhub.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Universal Serial Bus Hub Driver
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: drivers/usb/usbhub/usbhub.c
5  * PURPOSE: UsbHub Driver
6  * PROGRAMMERS:
7  * HervĂ© Poussineau (hpoussin@reactos.org)
8  * Michael Martin (michael.martin@reactos.org)
9  * Johannes Anderwald (johannes.anderwald@reactos.org)
10  */
11 
12 #include "usbhub.h"
13 
14 #define NDEBUG
15 #include <debug.h>
16 
20  IN PIRP Irp)
21 {
22  DPRINT("USBHUB: IRP_MJ_CREATE\n");
23 
24  Irp->IoStatus.Status = STATUS_SUCCESS;
25  Irp->IoStatus.Information = 0;
27  return STATUS_SUCCESS;
28 }
29 
33  IN PIRP Irp)
34 {
35  DPRINT("USBHUB: IRP_MJ_CLOSE\n");
36 
37  Irp->IoStatus.Status = STATUS_SUCCESS;
38  Irp->IoStatus.Information = 0;
40  return STATUS_SUCCESS;
41 }
42 
46  IN PIRP Irp)
47 {
48  DPRINT("USBHUB: IRP_MJ_CLEANUP\n");
49 
50  Irp->IoStatus.Status = STATUS_SUCCESS;
51  Irp->IoStatus.Information = 0;
53  return STATUS_SUCCESS;
54 }
55 
56 
61 {
63  PHUB_DEVICE_EXTENSION HubDeviceExtension;
65  DPRINT("USBHUB: AddDevice (%p)\n", PhysicalDeviceObject);
66  //
67  // Create the Device Object
68  //
70  sizeof(HUB_DEVICE_EXTENSION),
71  NULL,
74  FALSE,
75  &DeviceObject);
76 
77  if (!NT_SUCCESS(Status))
78  {
79  DPRINT1("USBHUB: IoCreateDevice() failed with status 0x%08lx\n", Status);
80  return Status;
81  }
82 
83  //
84  // Zero Hub Extension
85  //
86  HubDeviceExtension = (PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
87  RtlZeroMemory(HubDeviceExtension, sizeof(HUB_DEVICE_EXTENSION));
88 
89  INITIALIZE_PNP_STATE(HubDeviceExtension->Common);
90 
91  //
92  // Set this to Fdo
93  //
94  HubDeviceExtension->Common.IsFDO = TRUE;
96 
97  // initialize mutex
98  KeInitializeGuardedMutex(&HubDeviceExtension->HubMutexLock);
99 
100  // initialize remove lock
101  IoInitializeRemoveLock(&HubDeviceExtension->Common.RemoveLock, 'buH', 0, 0);
102 
103  //
104  // initialize reset complete event
105  //
106  KeInitializeEvent(&HubDeviceExtension->ResetComplete, NotificationEvent, FALSE);
107 
108  //
109  // Attached to lower device
110  //
111  //Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice);
113  if (!NT_SUCCESS(Status))
114  {
115  DPRINT1("USBHUB: IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status);
117  return Status;
118  }
119 
122 
123  return STATUS_SUCCESS;
124 }
125 
126 static NTSTATUS NTAPI
129  IN PIRP Irp)
130 {
132 
133  if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
134  {
135  DPRINT1("Usbhub: FDO stub for major function 0x%lx\n",
136  IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
138  }
139  else
140  {
141  //
142  // Cant forward as we are the PDO!
143  //
144  DPRINT1("USBHUB: ERROR- PDO stub for major function 0x%lx\n",
145  IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
146 #ifndef NDEBUG
147  DbgBreakPoint();
148 #endif
149  }
150 
151  Status = Irp->IoStatus.Status;
153  return Status;
154 }
155 
156 
160  PIRP Irp)
161 {
162  DPRINT("Usbhub: DispatchDeviceControl\n");
163  if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
165  else
167 }
168 
172  PIRP Irp)
173 {
174  DPRINT("Usbhub: DispatchSystemControl\n");
175  if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
177  else
179 }
180 
184  PIRP Irp)
185 {
186  DPRINT("Usbhub: DispatchInternalDeviceControl\n");
187  if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
189  else
191 }
192 
196  PIRP Irp)
197 {
198  DPRINT("USBHUB: DispatchPnp\n");
199  if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
201  else
203 }
204 
208  PIRP Irp)
209 {
210  PIO_STACK_LOCATION IoStack;
211  PHUB_DEVICE_EXTENSION DeviceExtension;
214  DeviceExtension = DeviceObject->DeviceExtension;
215 
216  Status = IoAcquireRemoveLock(&DeviceExtension->Common.RemoveLock, Irp);
217  if (!NT_SUCCESS(Status))
218  {
219  Irp->IoStatus.Status = Status;
221  return Status;
222  }
223 
224  DPRINT1("Power Function %x\n", IoStack->MinorFunction);
225 
226  if (DeviceExtension->Common.IsFDO)
227  {
230  Status = PoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
231  IoReleaseRemoveLock(&DeviceExtension->Common.RemoveLock, Irp);
232  return Status;
233  }
234 
235  switch (IoStack->MinorFunction)
236  {
237  case IRP_MN_SET_POWER:
238  {
239  DPRINT("IRP_MN_SET_POWER\n");
240  break;
241  }
242  case IRP_MN_QUERY_POWER:
243  {
244  DPRINT("IRP_MN_QUERY_POWER\n");
245  break;
246  }
247  case IRP_MN_WAIT_WAKE:
248  {
249  DPRINT("IRP_MN_WAIT_WAKE\n");
250  break;
251  }
252  default:
253  {
254  DPRINT1("PDO IRP_MJ_POWER / unknown minor function 0x%lx\n", IoStack->MinorFunction);
256  return Irp->IoStatus.Status;
257  }
258  }
259 
261  Irp->IoStatus.Status = STATUS_SUCCESS;
263  IoReleaseRemoveLock(&DeviceExtension->Common.RemoveLock, Irp);
264  return STATUS_SUCCESS;
265 }
266 
267 VOID
268 NTAPI
271 {
273 }
274 
275 
280 {
283 
284  DPRINT("USBHUB: DriverEntry\n");
285 
294 
295  return STATUS_SUCCESS;
296 }
297 
#define DO_DEVICE_INITIALIZING
Definition: env_spec_w32.h:399
#define DO_POWER_PAGABLE
#define IN
Definition: typedefs.h:38
NTSTATUS NTAPI USBHUB_DispatchSystemControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: usbhub.c:170
#define TRUE
Definition: types.h:120
#define IRP_MJ_CREATE
Definition: rdpdr.c:44
NTSTATUS NTAPI USBHUB_Close(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: usbhub.c:31
NTSTATUS NTAPI USBHUB_DispatchPower(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: usbhub.c:206
#define IRP_MN_QUERY_POWER
NTSTATUS NTAPI PoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: power.c:485
_In_ PIRP Irp
Definition: csq.h:116
#define IoInitializeRemoveLock(Lock, AllocateTag, MaxLockedMinutes, HighWatermark)
Definition: iofuncs.h:2785
#define IRP_MJ_PNP
Definition: cdrw_usr.h:52
NTSTATUS NTAPI USBHUB_DispatchPnp(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: usbhub.c:194
DRIVER_DISPATCH ForwardIrpAndForget
Definition: i8042prt.h:341
VOID NTAPI USBHUB_Unload(IN PDRIVER_OBJECT DriverObject)
Definition: usbhub.c:269
LONG NTSTATUS
Definition: precomp.h:26
#define IoReleaseRemoveLock(_RemoveLock, _Tag)
Definition: iofuncs.h:2716
PDEVICE_OBJECT LowerDeviceObject
Definition: usbhub.h:103
NTSTATUS NTAPI USBHUB_Create(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: usbhub.c:18
PDEVICE_OBJECT PhysicalDeviceObject
Definition: btrfs_drv.h:1122
NTSTATUS NTAPI USBHUB_DispatchDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: usbhub.c:158
struct _HUB_DEVICE_EXTENSION * PHUB_DEVICE_EXTENSION
void DbgBreakPoint()
Definition: mach.c:553
NTSTATUS USBHUB_PdoHandleInternalDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: pdo.c:175
IO_REMOVE_LOCK RemoveLock
Definition: usbhub.h:78
KEVENT ResetComplete
Definition: usbhub.h:112
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
PDEVICE_OBJECT NTAPI IoAttachDeviceToDeviceStack(IN PDEVICE_OBJECT SourceDevice, IN PDEVICE_OBJECT TargetDevice)
Definition: device.c:966
PDRIVER_EXTENSION DriverExtension
Definition: iotypes.h:2174
NTSTATUS USBHUB_PdoHandlePnp(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: pdo.c:583
KGUARDED_MUTEX HubMutexLock
Definition: usbhub.h:109
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
#define IoCompleteRequest
Definition: irp.c:1240
NTSTATUS NTAPI USBHUB_DispatchInternalDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: usbhub.c:182
void DPRINT(...)
Definition: polytest.cpp:61
#define FILE_AUTOGENERATED_DEVICE_NAME
Definition: iotypes.h:138
NTSTATUS USBHUB_FdoHandlePnp(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: fdo.c:2029
NTSTATUS NTAPI DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
Definition: usbhub.c:277
static NTSTATUS NTAPI USBHUB_IrpStub(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: usbhub.c:127
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define DO_BUFFERED_IO
Definition: env_spec_w32.h:394
#define IRP_MJ_INTERNAL_DEVICE_CONTROL
#define IRP_MJ_POWER
#define IRP_MJ_CLOSE
Definition: rdpdr.c:45
PDRIVER_UNLOAD DriverUnload
Definition: iotypes.h:2180
NTSTATUS NTAPI USBHUB_AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT PhysicalDeviceObject)
Definition: usbhub.c:58
#define IRP_MN_SET_POWER
#define IRP_MJ_SYSTEM_CONTROL
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
VOID NTAPI IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject)
Definition: device.c:1251
PDRIVER_ADD_DEVICE AddDevice
Definition: iotypes.h:2112
VOID FASTCALL KeInitializeGuardedMutex(OUT PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:31
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
COMMON_DEVICE_EXTENSION Common
Definition: usbhub.h:102
VOID NTAPI PoStartNextPowerIrp(IN PIRP Irp)
Definition: power.c:626
NTSTATUS USBHUB_FdoHandleDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: fdo.c:2280
#define IoSkipCurrentIrpStackLocation(Irp)
Definition: ntifs_ex.h:421
#define DPRINT1
Definition: precomp.h:8
#define INITIALIZE_PNP_STATE(_Data_)
Definition: fbtusb.h:107
#define IRP_MJ_CLEANUP
PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION+1]
Definition: iotypes.h:2181
#define IO_NO_INCREMENT
Definition: iotypes.h:566
NTSTATUS NTAPI IoCreateDevice(IN PDRIVER_OBJECT DriverObject, IN ULONG DeviceExtensionSize, IN PUNICODE_STRING DeviceName, IN DEVICE_TYPE DeviceType, IN ULONG DeviceCharacteristics, IN BOOLEAN Exclusive, OUT PDEVICE_OBJECT *DeviceObject)
Definition: device.c:1031
#define UNIMPLEMENTED
Definition: debug.h:114
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
_In_ PUNICODE_STRING RegistryPath
Definition: wmip.h:27
#define IRP_MN_WAIT_WAKE
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define FILE_DEVICE_BUS_EXTENDER
Definition: winioctl.h:147
#define IoAcquireRemoveLock(RemoveLock, Tag)
NTSTATUS NTAPI USBHUB_Cleanup(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: usbhub.c:44
#define IRP_MJ_DEVICE_CONTROL
Definition: rdpdr.c:52