Synchronize with trunk's revision r57629.
[reactos.git] / drivers / usb / usbhub / usbhub.c
1 /*
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
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 NTSTATUS NTAPI
15 USBHUB_Create(
16 IN PDEVICE_OBJECT DeviceObject,
17 IN PIRP Irp)
18 {
19 DPRINT("USBHUB: IRP_MJ_CREATE\n");
20
21 Irp->IoStatus.Status = STATUS_SUCCESS;
22 Irp->IoStatus.Information = 0;
23 IoCompleteRequest(Irp, IO_NO_INCREMENT);
24 return STATUS_SUCCESS;
25 }
26
27 NTSTATUS NTAPI
28 USBHUB_Close(
29 IN PDEVICE_OBJECT DeviceObject,
30 IN PIRP Irp)
31 {
32 DPRINT("USBHUB: IRP_MJ_CLOSE\n");
33
34 Irp->IoStatus.Status = STATUS_SUCCESS;
35 Irp->IoStatus.Information = 0;
36 IoCompleteRequest(Irp, IO_NO_INCREMENT);
37 return STATUS_SUCCESS;
38 }
39
40 NTSTATUS NTAPI
41 USBHUB_Cleanup(
42 IN PDEVICE_OBJECT DeviceObject,
43 IN PIRP Irp)
44 {
45 DPRINT("USBHUB: IRP_MJ_CLEANUP\n");
46
47 Irp->IoStatus.Status = STATUS_SUCCESS;
48 Irp->IoStatus.Information = 0;
49 IoCompleteRequest(Irp, IO_NO_INCREMENT);
50 return STATUS_SUCCESS;
51 }
52
53
54 NTSTATUS NTAPI
55 USBHUB_AddDevice(
56 IN PDRIVER_OBJECT DriverObject,
57 IN PDEVICE_OBJECT PhysicalDeviceObject)
58 {
59 PDEVICE_OBJECT DeviceObject;
60 PHUB_DEVICE_EXTENSION HubDeviceExtension;
61 NTSTATUS Status;
62 DPRINT("USBHUB: AddDevice\n");
63 //
64 // Create the Device Object
65 //
66 Status = IoCreateDevice(DriverObject,
67 sizeof(HUB_DEVICE_EXTENSION),
68 NULL,
69 FILE_DEVICE_BUS_EXTENDER,
70 FILE_AUTOGENERATED_DEVICE_NAME,
71 FALSE,
72 &DeviceObject);
73
74 if (!NT_SUCCESS(Status))
75 {
76 DPRINT1("USBHUB: IoCreateDevice() failed with status 0x%08lx\n", Status);
77 return Status;
78 }
79
80 //
81 // Zero Hub Extension
82 //
83 HubDeviceExtension = (PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
84 RtlZeroMemory(HubDeviceExtension, sizeof(HUB_DEVICE_EXTENSION));
85
86 //
87 // Set this to Fdo
88 //
89 HubDeviceExtension->Common.IsFDO = TRUE;
90 DeviceObject->Flags |= DO_POWER_PAGABLE;
91
92 //
93 // initialize reset complete event
94 //
95 KeInitializeEvent(&HubDeviceExtension->ResetComplete, NotificationEvent, FALSE);
96
97 //
98 // Attached to lower device
99 //
100 //Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice);
101 HubDeviceExtension->LowerDeviceObject = IoAttachDeviceToDeviceStack(DeviceObject, PhysicalDeviceObject);
102 if (!NT_SUCCESS(Status))
103 {
104 DPRINT1("USBHUB: IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status);
105 IoDeleteDevice(DeviceObject);
106 return Status;
107 }
108
109 DeviceObject->Flags |= DO_BUFFERED_IO;
110 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
111
112 return STATUS_SUCCESS;
113 }
114
115 static NTSTATUS NTAPI
116 USBHUB_IrpStub(
117 IN PDEVICE_OBJECT DeviceObject,
118 IN PIRP Irp)
119 {
120 NTSTATUS Status;
121
122 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
123 {
124 DPRINT1("Usbhub: FDO stub for major function 0x%lx\n",
125 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
126 return ForwardIrpAndForget(DeviceObject, Irp);
127 }
128 else
129 {
130 //
131 // Cant forward as we are the PDO!
132 //
133 DPRINT1("USBHUB: ERROR- PDO stub for major function 0x%lx\n",
134 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
135 #ifndef NDEBUG
136 DbgBreakPoint();
137 #endif
138 }
139
140 Status = Irp->IoStatus.Status;
141 IoCompleteRequest(Irp, IO_NO_INCREMENT);
142 return Status;
143 }
144
145
146 NTSTATUS NTAPI
147 USBHUB_DispatchDeviceControl(
148 PDEVICE_OBJECT DeviceObject,
149 PIRP Irp)
150 {
151 DPRINT("Usbhub: DispatchDeviceControl\n");
152 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
153 return USBHUB_FdoHandleDeviceControl(DeviceObject, Irp);
154 else
155 return USBHUB_IrpStub(DeviceObject, Irp);
156 }
157
158 NTSTATUS NTAPI
159 USBHUB_DispatchInternalDeviceControl(
160 PDEVICE_OBJECT DeviceObject,
161 PIRP Irp)
162 {
163 DPRINT("Usbhub: DispatchInternalDeviceControl\n");
164 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
165 return USBHUB_IrpStub(DeviceObject, Irp);
166 else
167 return USBHUB_PdoHandleInternalDeviceControl(DeviceObject, Irp);
168 }
169
170 NTSTATUS NTAPI
171 USBHUB_DispatchPnp(
172 PDEVICE_OBJECT DeviceObject,
173 PIRP Irp)
174 {
175 DPRINT("USBHUB: DispatchPnp\n");
176 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
177 return USBHUB_FdoHandlePnp(DeviceObject, Irp);
178 else
179 return USBHUB_PdoHandlePnp(DeviceObject, Irp);
180 }
181
182 NTSTATUS NTAPI
183 USBHUB_DispatchPower(
184 PDEVICE_OBJECT DeviceObject,
185 PIRP Irp)
186 {
187 PIO_STACK_LOCATION IoStack;
188
189 IoStack = IoGetCurrentIrpStackLocation(Irp);
190 DPRINT1("Power Function %x\n", IoStack->MinorFunction);
191
192 if (IoStack->MinorFunction == IRP_MN_SET_POWER)
193 {
194 PoStartNextPowerIrp(Irp);
195 Irp->IoStatus.Status = STATUS_SUCCESS;
196 IoCompleteRequest(Irp, IO_NO_INCREMENT);
197 return STATUS_SUCCESS;
198
199 }
200 else if (IoStack->MinorFunction == IRP_MN_QUERY_POWER)
201 {
202 PoStartNextPowerIrp(Irp);
203 Irp->IoStatus.Status = STATUS_SUCCESS;
204 IoCompleteRequest(Irp, IO_NO_INCREMENT);
205 return STATUS_SUCCESS;
206
207 }
208 else if (IoStack->MinorFunction == IRP_MN_WAIT_WAKE)
209 {
210 PoStartNextPowerIrp(Irp);
211 Irp->IoStatus.Status = STATUS_SUCCESS;
212 IoCompleteRequest(Irp, IO_NO_INCREMENT);
213 return STATUS_SUCCESS;
214 }
215
216 PoStartNextPowerIrp(Irp);
217 Irp->IoStatus.Status = STATUS_SUCCESS;
218 IoCompleteRequest(Irp, IO_NO_INCREMENT);
219 return STATUS_SUCCESS;
220 }
221
222 VOID
223 NTAPI
224 USBHUB_Unload(
225 IN PDRIVER_OBJECT DriverObject)
226 {
227 UNIMPLEMENTED
228 }
229
230
231 NTSTATUS NTAPI
232 DriverEntry(
233 IN PDRIVER_OBJECT DriverObject,
234 IN PUNICODE_STRING RegistryPath)
235 {
236 DriverObject->DriverExtension->AddDevice = USBHUB_AddDevice;
237 DriverObject->DriverUnload = USBHUB_Unload;
238
239 DPRINT("USBHUB: DriverEntry\n");
240
241 DriverObject->MajorFunction[IRP_MJ_CREATE] = USBHUB_Create;
242 DriverObject->MajorFunction[IRP_MJ_CLOSE] = USBHUB_Close;
243 DriverObject->MajorFunction[IRP_MJ_CLEANUP] = USBHUB_Cleanup;
244 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = USBHUB_DispatchDeviceControl;
245 DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = USBHUB_DispatchInternalDeviceControl;
246 DriverObject->MajorFunction[IRP_MJ_PNP] = USBHUB_DispatchPnp;
247 DriverObject->MajorFunction[IRP_MJ_POWER] =USBHUB_DispatchPower;
248
249 return STATUS_SUCCESS;
250 }
251