Implement IRP_MN_QUERY_CAPABILITIES, IRP_MN_QUERY_RESOURCES, IRP_MN_QUERY_RESOURCE_RE...
[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 int size;
160 char *buf;
161
162 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_TEXT / DeviceTextLocationInformation\n");
163 if (!DeviceExtension->dev->descriptor.iProduct)
164 return STATUS_NOT_SUPPORTED;
165
166 size = usb_get_string(DeviceExtension->dev, LocaleId, DeviceExtension->dev->descriptor.iProduct, NULL, 0);
167 if (size < 2)
168 {
169 DPRINT("Usbhub: usb_get_string() failed\n");
170 return STATUS_IO_DEVICE_ERROR;
171 }
172 buf = ExAllocatePool(PagedPool, size);
173 if (buf == NULL)
174 return STATUS_INSUFFICIENT_RESOURCES;
175 size = usb_get_string(DeviceExtension->dev, LocaleId, DeviceExtension->dev->descriptor.iProduct, buf, size);
176 if (size < 0)
177 {
178 DPRINT("Usbhub: usb_get_string() failed\n");
179 ExFreePool(buf);
180 return STATUS_IO_DEVICE_ERROR;
181 }
182 *Information = (ULONG_PTR)buf;
183 return STATUS_SUCCESS;
184 }
185 default:
186 DPRINT1("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_TEXT / unknown device text type 0x%lx\n", DeviceTextType);
187 return STATUS_NOT_SUPPORTED;
188 }
189 }
190
191 NTSTATUS STDCALL
192 UsbhubPnpPdo(
193 IN PDEVICE_OBJECT DeviceObject,
194 IN PIRP Irp)
195 {
196 ULONG MinorFunction;
197 PIO_STACK_LOCATION Stack;
198 ULONG_PTR Information = 0;
199 NTSTATUS Status;
200
201 Stack = IoGetCurrentIrpStackLocation(Irp);
202 MinorFunction = Stack->MinorFunction;
203
204 switch (MinorFunction)
205 {
206 case IRP_MN_START_DEVICE: /* 0x0 */
207 {
208 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_START_DEVICE\n");
209 Status = UsbhubPdoStartDevice(DeviceObject, Irp);
210 break;
211 }
212 case IRP_MN_QUERY_CAPABILITIES: /* 0x09 */
213 {
214 PDEVICE_CAPABILITIES DeviceCapabilities;
215 ULONG i;
216 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_CAPABILITIES\n");
217
218 DeviceCapabilities = (PDEVICE_CAPABILITIES)Stack->Parameters.DeviceCapabilities.Capabilities;
219 /* FIXME: capabilities can change with connected device */
220 DeviceCapabilities->LockSupported = TRUE;
221 DeviceCapabilities->EjectSupported = FALSE;
222 DeviceCapabilities->Removable = FALSE;
223 DeviceCapabilities->DockDevice = FALSE;
224 DeviceCapabilities->UniqueID = FALSE;
225 DeviceCapabilities->SilentInstall = TRUE;
226 DeviceCapabilities->RawDeviceOK = FALSE;
227 DeviceCapabilities->SurpriseRemovalOK = FALSE;
228 DeviceCapabilities->HardwareDisabled = FALSE; /* FIXME */
229 //DeviceCapabilities->NoDisplayInUI = FALSE; /* FIXME */
230 DeviceCapabilities->DeviceState[0] = PowerDeviceD0; /* FIXME */
231 for (i = 0; i < PowerSystemMaximum; i++)
232 DeviceCapabilities->DeviceState[i] = PowerDeviceD3; /* FIXME */
233 //DeviceCapabilities->DeviceWake = PowerDeviceUndefined; /* FIXME */
234 DeviceCapabilities->D1Latency = 0; /* FIXME */
235 DeviceCapabilities->D2Latency = 0; /* FIXME */
236 DeviceCapabilities->D3Latency = 0; /* FIXME */
237 Status = STATUS_SUCCESS;
238 break;
239 }
240 case IRP_MN_QUERY_RESOURCES: /* 0x0a */
241 {
242 PCM_RESOURCE_LIST ResourceList;
243
244 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_RESOURCES\n");
245 ResourceList = ExAllocatePool(PagedPool, sizeof(CM_RESOURCE_LIST));
246 if (!ResourceList)
247 {
248 DPRINT("Usbhub: ExAllocatePool() failed\n");
249 Status = STATUS_INSUFFICIENT_RESOURCES;
250 }
251 else
252 {
253 ResourceList->Count = 0;
254 Information = (ULONG_PTR)ResourceList;
255 Status = STATUS_SUCCESS;
256 }
257 break;
258 }
259 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS: /* 0x0b */
260 {
261 PIO_RESOURCE_REQUIREMENTS_LIST ResourceList;
262
263 DPRINT("Usbhub: IRP_MJ_PNP / IRP_MN_QUERY_RESOURCE_REQUIREMENTS\n");
264 ResourceList = ExAllocatePool(PagedPool, sizeof(IO_RESOURCE_REQUIREMENTS_LIST));
265 if (!ResourceList)
266 {
267 DPRINT("Usbhub: ExAllocatePool() failed\n");
268 Status = STATUS_INSUFFICIENT_RESOURCES;
269 }
270 else
271 {
272 RtlZeroMemory(ResourceList, sizeof(IO_RESOURCE_REQUIREMENTS_LIST));
273 ResourceList->ListSize = sizeof(IO_RESOURCE_REQUIREMENTS_LIST);
274 ResourceList->AlternativeLists = 1;
275 ResourceList->List->Version = 1;
276 ResourceList->List->Revision = 1;
277 ResourceList->List->Count = 0;
278 Information = (ULONG_PTR)ResourceList;
279 Status = STATUS_SUCCESS;
280 }
281 break;
282 }
283 case IRP_MN_QUERY_DEVICE_TEXT: /* 0x0c */
284 {
285 Status = UsbhubPdoQueryDeviceText(DeviceObject, Irp, &Information);
286 break;
287 }
288 case IRP_MN_QUERY_ID: /* 0x13 */
289 {
290 Status = UsbhubPdoQueryId(DeviceObject, Irp, &Information);
291 break;
292 }
293 default:
294 {
295 /* We can't forward request to the lower driver, because
296 * we are a Pdo, so we don't have lower driver...
297 */
298 DPRINT1("Usbhub: IRP_MJ_PNP / unknown minor function 0x%lx\n", MinorFunction);
299 Information = Irp->IoStatus.Information;
300 Status = Irp->IoStatus.Status;
301 }
302 }
303
304 Irp->IoStatus.Information = Information;
305 Irp->IoStatus.Status = Status;
306 IoCompleteRequest(Irp, IO_NO_INCREMENT);
307 return Status;
308 }
309