[USBHUB]
[reactos.git] / drivers / usb / usbhub_new / 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 DPRINT1("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 DPRINT1("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 DPRINT1("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 DPRINT1("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 // Attached to lower device
94 //
95 //Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice);
96 HubDeviceExtension->LowerDeviceObject = IoAttachDeviceToDeviceStack(DeviceObject, PhysicalDeviceObject);
97 if (!NT_SUCCESS(Status))
98 {
99 DPRINT1("USBHUB: IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status);
100 IoDeleteDevice(DeviceObject);
101 return Status;
102 }
103
104 DeviceObject->Flags |= DO_BUFFERED_IO;
105 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
106
107 return STATUS_SUCCESS;
108 }
109
110 static NTSTATUS NTAPI
111 USBHUB_IrpStub(
112 IN PDEVICE_OBJECT DeviceObject,
113 IN PIRP Irp)
114 {
115 NTSTATUS Status;
116
117 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
118 {
119 DPRINT1("Usbhub: FDO stub for major function 0x%lx\n",
120 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
121 return ForwardIrpAndForget(DeviceObject, Irp);
122 }
123 else
124 {
125 //
126 // Cant forward as we are the PDO!
127 //
128 DPRINT1("USBHUB: ERROR- PDO stub for major function 0x%lx\n",
129 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
130 #ifndef NDEBUG
131 DbgBreakPoint();
132 #endif
133 }
134
135 Status = Irp->IoStatus.Status;
136 IoCompleteRequest(Irp, IO_NO_INCREMENT);
137 return Status;
138 }
139
140
141 NTSTATUS NTAPI
142 USBHUB_DispatchDeviceControl(
143 PDEVICE_OBJECT DeviceObject,
144 PIRP Irp)
145 {
146 DPRINT1("Usbhub: DispatchDeviceControl\n");
147 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
148 return USBHUB_FdoHandleDeviceControl(DeviceObject, Irp);
149 else
150 return USBHUB_IrpStub(DeviceObject, Irp);
151 }
152
153 NTSTATUS NTAPI
154 USBHUB_DispatchInternalDeviceControl(
155 PDEVICE_OBJECT DeviceObject,
156 PIRP Irp)
157 {
158 //DPRINT1("Usbhub: DispatchInternalDeviceControl\n");
159 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
160 return USBHUB_IrpStub(DeviceObject, Irp);
161 else
162 return USBHUB_PdoHandleInternalDeviceControl(DeviceObject, Irp);
163 }
164
165 NTSTATUS NTAPI
166 USBHUB_DispatchPnp(
167 PDEVICE_OBJECT DeviceObject,
168 PIRP Irp)
169 {
170 DPRINT1("USBHUB: DispatchPnp\n");
171 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->Common.IsFDO)
172 return USBHUB_FdoHandlePnp(DeviceObject, Irp);
173 else
174 return USBHUB_PdoHandlePnp(DeviceObject, Irp);
175 }
176
177 NTSTATUS NTAPI
178 USBHUB_DispatchPower(
179 PDEVICE_OBJECT DeviceObject,
180 PIRP Irp)
181 {
182 IoCompleteRequest(Irp, IO_NO_INCREMENT);
183 return STATUS_NOT_SUPPORTED;
184 }
185
186 NTSTATUS NTAPI
187 DriverEntry(
188 IN PDRIVER_OBJECT DriverObject,
189 IN PUNICODE_STRING RegistryPath)
190 {
191 DriverObject->DriverExtension->AddDevice = USBHUB_AddDevice;
192 DPRINT1("USBHUB: DriverEntry\n");
193
194 DriverObject->MajorFunction[IRP_MJ_CREATE] = USBHUB_Create;
195 DriverObject->MajorFunction[IRP_MJ_CLOSE] = USBHUB_Close;
196 DriverObject->MajorFunction[IRP_MJ_CLEANUP] = USBHUB_Cleanup;
197 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = USBHUB_DispatchDeviceControl;
198 DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = USBHUB_DispatchInternalDeviceControl;
199 DriverObject->MajorFunction[IRP_MJ_PNP] = USBHUB_DispatchPnp;
200 DriverObject->MajorFunction[IRP_MJ_POWER] =USBHUB_DispatchPower;
201
202 return STATUS_SUCCESS;
203 }
204