79f6a59ff37853198d876bb784814c13e5a31a34
[reactos.git] / reactos / drivers / usb / usbhub / pdo.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: USB hub driver
4 * FILE: drivers/usb/cromwell/hub/pdo.c
5 * PURPOSE: IRP_MJ_PNP operations for PDOs
6 *
7 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.com)
8 */
9
10 #define NDEBUG
11 #include <stdio.h>
12 #include "usbhub.h"
13
14 #define IO_METHOD_FROM_CTL_CODE(ctlCode) (ctlCode&0x00000003)
15
16 NTSTATUS
17 UsbhubDeviceControlPdo(
18 IN PDEVICE_OBJECT DeviceObject,
19 IN PIRP Irp)
20 {
21 PIO_STACK_LOCATION Stack;
22 ULONG_PTR Information = 0;
23 NTSTATUS Status;
24
25 DPRINT("Usbhub: UsbhubDeviceControlPdo() called\n");
26
27 Stack = IoGetCurrentIrpStackLocation(Irp);
28 Status = Irp->IoStatus.Status;
29
30 switch (Stack->Parameters.DeviceIoControl.IoControlCode)
31 {
32 default:
33 {
34 DPRINT1("Usbhub: Unknown IOCTL code 0x%lx\n", Stack->Parameters.DeviceIoControl.IoControlCode);
35 Information = Irp->IoStatus.Information;
36 Status = Irp->IoStatus.Status;
37 }
38 }
39
40 Irp->IoStatus.Information = Information;
41 Irp->IoStatus.Status = Status;
42 IoCompleteRequest(Irp, IO_NO_INCREMENT);
43 return Status;
44 }
45
46 static NTSTATUS
47 UsbhubPdoStartDevice(
48 IN PDEVICE_OBJECT DeviceObject,
49 IN PIRP Irp)
50 {
51 PHUB_DEVICE_EXTENSION DeviceExtension;
52 NTSTATUS Status;
53
54 DeviceExtension = (PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
55 DbgBreakPoint();
56
57 /* Register and activate device interface */
58 Status = IoRegisterDeviceInterface(
59 DeviceObject,
60 DeviceExtension->dev->descriptor.bDeviceClass == USB_CLASS_HUB ?
61 &GUID_DEVINTERFACE_USB_HUB :
62 &GUID_DEVINTERFACE_USB_DEVICE,
63 NULL, /* Reference string */
64 &DeviceExtension->SymbolicLinkName);
65 if (!NT_SUCCESS(Status))
66 {
67 DPRINT("Usbhub: IoRegisterDeviceInterface() failed with status 0x%08lx\n", Status);
68 return Status;
69 }
70
71 Status = IoSetDeviceInterfaceState(&DeviceExtension->SymbolicLinkName, TRUE);
72 if (!NT_SUCCESS(Status))
73 {
74 DPRINT("Usbhub: IoSetDeviceInterfaceState() failed with status 0x%08lx\n", Status);
75 return Status;
76 }
77
78 return STATUS_SUCCESS;
79 }
80
81 static NTSTATUS
82 UsbhubPdoQueryId(
83 IN PDEVICE_OBJECT DeviceObject,
84 IN PIRP Irp,
85 OUT ULONG_PTR* Information)
86 {
87 PHUB_DEVICE_EXTENSION DeviceExtension;
88 ULONG IdType;
89 PUNICODE_STRING SourceString;
90 UNICODE_STRING String;
91 NTSTATUS Status;
92
93 IdType = IoGetCurrentIrpStackLocation(Irp)->Parameters.QueryId.IdType;
94 DeviceExtension = (PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
95 RtlInitUnicodeString(&String, NULL);
96
97 switch (IdType)
98 {
99 case BusQueryDeviceID:
100 {
101 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryDeviceID\n");
102 SourceString = &DeviceExtension->DeviceId;
103 break;
104 }
105 case BusQueryHardwareIDs:
106 {
107 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryHardwareIDs\n");
108 SourceString = &DeviceExtension->HardwareIds;
109 break;
110 }
111 case BusQueryCompatibleIDs:
112 {
113 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryCompatibleIDs\n");
114 SourceString = &DeviceExtension->CompatibleIds;
115 break;
116 }
117 case BusQueryInstanceID:
118 {
119 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryInstanceID\n");
120 SourceString = &DeviceExtension->InstanceId;
121 break;
122 }
123 default:
124 DPRINT1("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_ID / unknown query id type 0x%lx\n", IdType);
125 return STATUS_NOT_SUPPORTED;
126 }
127
128 Status = UsbhubDuplicateUnicodeString(
129 &String,
130 SourceString,
131 PagedPool);
132 *Information = (ULONG_PTR)String.Buffer;
133 return Status;
134 }
135
136 static NTSTATUS
137 UsbhubPdoQueryDeviceText(
138 IN PDEVICE_OBJECT DeviceObject,
139 IN PIRP Irp,
140 OUT ULONG_PTR* Information)
141 {
142 PHUB_DEVICE_EXTENSION DeviceExtension;
143 DEVICE_TEXT_TYPE DeviceTextType;
144 LCID LocaleId;
145
146 DeviceTextType = IoGetCurrentIrpStackLocation(Irp)->Parameters.QueryDeviceText.DeviceTextType;
147 LocaleId = IoGetCurrentIrpStackLocation(Irp)->Parameters.QueryDeviceText.LocaleId;
148 DeviceExtension = (PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
149
150 switch (DeviceTextType)
151 {
152 /*case DeviceTextDescription:
153 {
154 DPRINT1("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_TEXT / DeviceTextDescription\n");
155 return STATUS_NOT_IMPLEMENTED;
156 }*/
157 case DeviceTextLocationInformation:
158 {
159 unsigned short size;
160 int ret;
161 PWCHAR buf;
162 PWCHAR bufret;
163
164 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_TEXT / DeviceTextLocationInformation\n");
165 if (!DeviceExtension->dev->descriptor.iProduct)
166 return STATUS_NOT_SUPPORTED;
167
168 ret = usb_get_string(DeviceExtension->dev, LocaleId, DeviceExtension->dev->descriptor.iProduct, &size, sizeof(size));
169 if (ret < 2)
170 {
171 DPRINT("Usbhub: usb_get_string() failed with error %d\n", ret);
172 return STATUS_IO_DEVICE_ERROR;
173 }
174 size &= 0xff;
175 buf = ExAllocatePool(PagedPool, size);
176 if (buf == NULL)
177 {
178 DPRINT("Usbhub: ExAllocatePool() failed\n");
179 return STATUS_INSUFFICIENT_RESOURCES;
180 }
181 ret = usb_get_string(DeviceExtension->dev, LocaleId, DeviceExtension->dev->descriptor.iProduct, buf, size);
182 if (ret < 0)
183 {
184 DPRINT("Usbhub: usb_get_string() failed with error %d\n", ret);
185 ExFreePool(buf);
186 return STATUS_IO_DEVICE_ERROR;
187 }
188 bufret = ExAllocatePool(PagedPool, size - 2 /* size of length identifier */ + 2 /* final NULL */);
189 if (bufret == NULL)
190 {
191 DPRINT("Usbhub: ExAllocatePool() failed\n");
192 ExFreePool(buf);
193 return STATUS_INSUFFICIENT_RESOURCES;
194 }
195
196 RtlCopyMemory(bufret, &buf[1], size - 2);
197 bufret[(size - 1) / sizeof(WCHAR)] = 0;
198 *Information = (ULONG_PTR)bufret;
199 ExFreePool(buf);
200 return STATUS_SUCCESS;
201 }
202 default:
203 DPRINT1("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_TEXT / unknown device text type 0x%lx\n", DeviceTextType);
204 return STATUS_NOT_SUPPORTED;
205 }
206 }
207
208 NTSTATUS STDCALL
209 UsbhubPnpPdo(
210 IN PDEVICE_OBJECT DeviceObject,
211 IN PIRP Irp)
212 {
213 ULONG MinorFunction;
214 PIO_STACK_LOCATION Stack;
215 ULONG_PTR Information = 0;
216 NTSTATUS Status;
217
218 Stack = IoGetCurrentIrpStackLocation(Irp);
219 MinorFunction = Stack->MinorFunction;
220
221 switch (MinorFunction)
222 {
223 case IRP_MN_START_DEVICE: /* 0x0 */
224 {
225 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_START_DEVICE\n");
226 Status = UsbhubPdoStartDevice(DeviceObject, Irp);
227 break;
228 }
229 case IRP_MN_QUERY_CAPABILITIES: /* 0x09 */
230 {
231 PDEVICE_CAPABILITIES DeviceCapabilities;
232 ULONG i;
233 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_CAPABILITIES\n");
234
235 DeviceCapabilities = (PDEVICE_CAPABILITIES)Stack->Parameters.DeviceCapabilities.Capabilities;
236 /* FIXME: capabilities can change with connected device */
237 DeviceCapabilities->LockSupported = TRUE;
238 DeviceCapabilities->EjectSupported = FALSE;
239 DeviceCapabilities->Removable = FALSE;
240 DeviceCapabilities->DockDevice = FALSE;
241 DeviceCapabilities->UniqueID = FALSE;
242 DeviceCapabilities->SilentInstall = TRUE;
243 DeviceCapabilities->RawDeviceOK = FALSE;
244 DeviceCapabilities->SurpriseRemovalOK = FALSE;
245 DeviceCapabilities->HardwareDisabled = FALSE; /* FIXME */
246 //DeviceCapabilities->NoDisplayInUI = FALSE; /* FIXME */
247 DeviceCapabilities->DeviceState[0] = PowerDeviceD0; /* FIXME */
248 for (i = 0; i < PowerSystemMaximum; i++)
249 DeviceCapabilities->DeviceState[i] = PowerDeviceD3; /* FIXME */
250 //DeviceCapabilities->DeviceWake = PowerDeviceUndefined; /* FIXME */
251 DeviceCapabilities->D1Latency = 0; /* FIXME */
252 DeviceCapabilities->D2Latency = 0; /* FIXME */
253 DeviceCapabilities->D3Latency = 0; /* FIXME */
254 Status = STATUS_SUCCESS;
255 break;
256 }
257 case IRP_MN_QUERY_RESOURCES: /* 0x0a */
258 {
259 PCM_RESOURCE_LIST ResourceList;
260
261 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_RESOURCES\n");
262 ResourceList = ExAllocatePool(PagedPool, sizeof(CM_RESOURCE_LIST));
263 if (!ResourceList)
264 {
265 DPRINT("Usbhub: ExAllocatePool() failed\n");
266 Status = STATUS_INSUFFICIENT_RESOURCES;
267 }
268 else
269 {
270 ResourceList->Count = 0;
271 Information = (ULONG_PTR)ResourceList;
272 Status = STATUS_SUCCESS;
273 }
274 break;
275 }
276 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS: /* 0x0b */
277 {
278 PIO_RESOURCE_REQUIREMENTS_LIST ResourceList;
279
280 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_RESOURCE_REQUIREMENTS\n");
281 ResourceList = ExAllocatePool(PagedPool, sizeof(IO_RESOURCE_REQUIREMENTS_LIST));
282 if (!ResourceList)
283 {
284 DPRINT("Usbhub: ExAllocatePool() failed\n");
285 Status = STATUS_INSUFFICIENT_RESOURCES;
286 }
287 else
288 {
289 RtlZeroMemory(ResourceList, sizeof(IO_RESOURCE_REQUIREMENTS_LIST));
290 ResourceList->ListSize = sizeof(IO_RESOURCE_REQUIREMENTS_LIST);
291 ResourceList->AlternativeLists = 1;
292 ResourceList->List->Version = 1;
293 ResourceList->List->Revision = 1;
294 ResourceList->List->Count = 0;
295 Information = (ULONG_PTR)ResourceList;
296 Status = STATUS_SUCCESS;
297 }
298 break;
299 }
300 case IRP_MN_QUERY_DEVICE_TEXT: /* 0x0c */
301 {
302 Status = UsbhubPdoQueryDeviceText(DeviceObject, Irp, &Information);
303 break;
304 }
305 case IRP_MN_QUERY_ID: /* 0x13 */
306 {
307 Status = UsbhubPdoQueryId(DeviceObject, Irp, &Information);
308 break;
309 }
310 default:
311 {
312 /* We can't forward request to the lower driver, because
313 * we are a Pdo, so we don't have lower driver...
314 */
315 DPRINT1("Usbhub: IRP_MJ_PNP / unknown minor function 0x%lx\n", MinorFunction);
316 Information = Irp->IoStatus.Information;
317 Status = Irp->IoStatus.Status;
318 }
319 }
320
321 Irp->IoStatus.Information = Information;
322 Irp->IoStatus.Status = Status;
323 IoCompleteRequest(Irp, IO_NO_INCREMENT);
324 return Status;
325 }
326