2 * PROJECT: ReactOS Universal Serial Bus Bulk Enhanced Host Controller Interface
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: drivers/usb/usbccgp/descriptor.c
5 * PURPOSE: USB device driver.
7 * Michael Martin (michael.martin@reactos.org)
8 * Johannes Anderwald (johannes.anderwald@reactos.org)
16 USBCCGP_GetDescriptor(
17 IN PDEVICE_OBJECT DeviceObject
,
18 IN UCHAR DescriptorType
,
19 IN ULONG DescriptorLength
,
20 IN UCHAR DescriptorIndex
,
22 OUT PVOID
*OutDescriptor
)
32 ASSERT(OutDescriptor
);
33 ASSERT(DescriptorLength
);
36 // first allocate descriptor buffer
38 Descriptor
= AllocateItem(NonPagedPool
, DescriptorLength
);
44 return STATUS_INSUFFICIENT_RESOURCES
;
50 Urb
= (PURB
) AllocateItem(NonPagedPool
, sizeof(URB
));
57 return STATUS_INSUFFICIENT_RESOURCES
;
63 UsbBuildGetDescriptorRequest(Urb
,
64 sizeof(Urb
->UrbControlDescriptorRequest
),
76 Status
= USBCCGP_SyncUrbRequest(DeviceObject
, Urb
);
83 if (NT_SUCCESS(Status
))
88 *OutDescriptor
= Descriptor
;
99 USBCCGP_GetStringDescriptor(
100 IN PDEVICE_OBJECT DeviceObject
,
101 IN ULONG DescriptorLength
,
102 IN UCHAR DescriptorIndex
,
103 IN LANGID LanguageId
,
104 OUT PVOID
*OutDescriptor
)
107 PUSB_STRING_DESCRIPTOR StringDescriptor
;
111 // retrieve descriptor
112 Status
= USBCCGP_GetDescriptor(DeviceObject
, USB_STRING_DESCRIPTOR_TYPE
, DescriptorLength
, DescriptorIndex
, LanguageId
, OutDescriptor
);
113 if (!NT_SUCCESS(Status
))
119 // get descriptor structure
120 StringDescriptor
= (PUSB_STRING_DESCRIPTOR
)*OutDescriptor
;
123 ASSERT(StringDescriptor
->bLength
< DescriptorLength
- 2);
125 if (StringDescriptor
->bLength
== 2)
127 // invalid descriptor
128 FreeItem(StringDescriptor
);
129 return STATUS_DEVICE_DATA_ERROR
;
133 Size
= StringDescriptor
->bLength
+ sizeof(WCHAR
);
136 Buffer
= AllocateItem(NonPagedPool
, Size
);
140 FreeItem(StringDescriptor
);
141 return STATUS_INSUFFICIENT_RESOURCES
;
145 RtlCopyMemory(Buffer
, StringDescriptor
->bString
, Size
- FIELD_OFFSET(USB_STRING_DESCRIPTOR
, bString
));
148 FreeItem(StringDescriptor
);
151 *OutDescriptor
= (PVOID
)Buffer
;
152 return STATUS_SUCCESS
;
157 USBCCGP_GetDescriptors(
158 IN PDEVICE_OBJECT DeviceObject
)
161 PFDO_DEVICE_EXTENSION DeviceExtension
;
162 USHORT DescriptorLength
;
165 // get device extension
167 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
170 // first get device descriptor
172 Status
= USBCCGP_GetDescriptor(DeviceExtension
->NextDeviceObject
, USB_DEVICE_DESCRIPTOR_TYPE
, sizeof(USB_DEVICE_DESCRIPTOR
), 0, 0, (PVOID
*)&DeviceExtension
->DeviceDescriptor
);
173 if (!NT_SUCCESS(Status
))
176 // failed to get device descriptor
178 DeviceExtension
->DeviceDescriptor
= NULL
;
183 // now get basic configuration descriptor
185 Status
= USBCCGP_GetDescriptor(DeviceExtension
->NextDeviceObject
, USB_CONFIGURATION_DESCRIPTOR_TYPE
, sizeof(USB_CONFIGURATION_DESCRIPTOR
), 0, 0, (PVOID
*)&DeviceExtension
->ConfigurationDescriptor
);
186 if (!NT_SUCCESS(Status
))
189 // failed to get configuration descriptor
191 FreeItem(DeviceExtension
->DeviceDescriptor
);
192 DeviceExtension
->DeviceDescriptor
= NULL
;
199 DescriptorLength
= DeviceExtension
->ConfigurationDescriptor
->wTotalLength
;
202 // release basic descriptor
204 FreeItem(DeviceExtension
->ConfigurationDescriptor
);
205 DeviceExtension
->ConfigurationDescriptor
= NULL
;
208 // allocate full descriptor
210 Status
= USBCCGP_GetDescriptor(DeviceExtension
->NextDeviceObject
, USB_CONFIGURATION_DESCRIPTOR_TYPE
, DescriptorLength
, 0, 0, (PVOID
*)&DeviceExtension
->ConfigurationDescriptor
);
211 if (!NT_SUCCESS(Status
))
214 // failed to get configuration descriptor
216 FreeItem(DeviceExtension
->DeviceDescriptor
);
217 DeviceExtension
->DeviceDescriptor
= NULL
;
224 AllocateInterfaceDescriptorsArray(
225 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
,
226 OUT PUSB_INTERFACE_DESCRIPTOR
**OutArray
)
228 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
230 PUSB_INTERFACE_DESCRIPTOR
*Array
;
235 Array
= AllocateItem(NonPagedPool
, sizeof(PUSB_INTERFACE_DESCRIPTOR
) * ConfigurationDescriptor
->bNumInterfaces
);
237 return STATUS_INSUFFICIENT_RESOURCES
;
240 // enumerate all interfaces
246 // find next descriptor
248 InterfaceDescriptor
= USBD_ParseConfigurationDescriptorEx(ConfigurationDescriptor
, ConfigurationDescriptor
, Count
, 0, -1, -1, -1);
249 if (!InterfaceDescriptor
)
255 Array
[Count
] = InterfaceDescriptor
;
268 return STATUS_SUCCESS
;
272 DumpFullConfigurationDescriptor(
273 IN PFDO_DEVICE_EXTENSION FDODeviceExtension
,
274 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
)
276 PUSB_COMMON_DESCRIPTOR Descriptor
;
278 Descriptor
= (PUSB_COMMON_DESCRIPTOR
)ConfigurationDescriptor
;
280 DbgPrint("Bogus ConfigurationDescriptor Found\n");
281 DbgPrint("InterfaceCount %x\n", ConfigurationDescriptor
->bNumInterfaces
);
285 if (((ULONG_PTR
)Descriptor
) >= ((ULONG_PTR
)ConfigurationDescriptor
+ ConfigurationDescriptor
->wTotalLength
))
288 DbgPrint("Descriptor Type %x Length %lu Offset %lu\n", Descriptor
->bDescriptorType
, Descriptor
->bLength
, ((ULONG_PTR
)Descriptor
- (ULONG_PTR
)ConfigurationDescriptor
));
290 // check for invalid descriptors
291 if (!Descriptor
->bLength
)
293 DbgPrint("Bogus Descriptor!!!\n");
297 // advance to next descriptor
298 Descriptor
= (PUSB_COMMON_DESCRIPTOR
)((ULONG_PTR
)Descriptor
+ Descriptor
->bLength
);
308 USBCCGP_ScanConfigurationDescriptor(
309 IN OUT PFDO_DEVICE_EXTENSION FDODeviceExtension
,
310 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
)
312 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
313 ULONG InterfaceIndex
= 0;
314 ULONG DescriptorCount
;
319 ASSERT(ConfigurationDescriptor
);
320 ASSERT(ConfigurationDescriptor
->bNumInterfaces
);
323 // count all interface descriptors
325 DescriptorCount
= ConfigurationDescriptor
->bNumInterfaces
;
328 // allocate array holding the interface descriptors
330 FDODeviceExtension
->InterfaceList
= AllocateItem(NonPagedPool
, sizeof(USBD_INTERFACE_LIST_ENTRY
) * (DescriptorCount
+ 1));
331 if (!FDODeviceExtension
->InterfaceList
)
336 return STATUS_INSUFFICIENT_RESOURCES
;
342 // parse configuration descriptor
344 InterfaceDescriptor
= USBD_ParseConfigurationDescriptorEx(ConfigurationDescriptor
, ConfigurationDescriptor
, InterfaceIndex
, -1, -1, -1, -1);
345 if (InterfaceDescriptor
)
348 // store in interface list
350 FDODeviceExtension
->InterfaceList
[FDODeviceExtension
->InterfaceListCount
].InterfaceDescriptor
= InterfaceDescriptor
;
351 FDODeviceExtension
->InterfaceListCount
++;
355 DumpConfigurationDescriptor(ConfigurationDescriptor
);
356 DumpFullConfigurationDescriptor(FDODeviceExtension
, ConfigurationDescriptor
);
360 // CORE-6574 Test 3 (USB Web Cam)
362 if (FDODeviceExtension
->DeviceDescriptor
&& FDODeviceExtension
->DeviceDescriptor
->idVendor
== 0x0458 && FDODeviceExtension
->DeviceDescriptor
->idProduct
== 0x705f)
367 // move to next interface
371 }while(InterfaceIndex
< DescriptorCount
);
376 ASSERT(FDODeviceExtension
->InterfaceListCount
);
381 return STATUS_SUCCESS
;
385 DumpConfigurationDescriptor(PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
)
387 DbgPrint("Dumping ConfigurationDescriptor %x\n", ConfigurationDescriptor
);
388 DbgPrint("bLength %x\n", ConfigurationDescriptor
->bLength
);
389 DbgPrint("bDescriptorType %x\n", ConfigurationDescriptor
->bDescriptorType
);
390 DbgPrint("wTotalLength %x\n", ConfigurationDescriptor
->wTotalLength
);
391 DbgPrint("bNumInterfaces %x\n", ConfigurationDescriptor
->bNumInterfaces
);
392 DbgPrint("bConfigurationValue %x\n", ConfigurationDescriptor
->bConfigurationValue
);
393 DbgPrint("iConfiguration %x\n", ConfigurationDescriptor
->iConfiguration
);
394 DbgPrint("bmAttributes %x\n", ConfigurationDescriptor
->bmAttributes
);
395 DbgPrint("MaxPower %x\n", ConfigurationDescriptor
->MaxPower
);
399 USBCCGP_SelectInterface(
400 IN PDEVICE_OBJECT DeviceObject
,
401 IN PFDO_DEVICE_EXTENSION DeviceExtension
,
402 IN ULONG InterfaceIndex
)
410 Urb
= AllocateItem(NonPagedPool
, GET_SELECT_INTERFACE_REQUEST_SIZE(DeviceExtension
->InterfaceList
[InterfaceIndex
].InterfaceDescriptor
->bNumEndpoints
));
416 return STATUS_INSUFFICIENT_RESOURCES
;
420 // now prepare interface urb
422 UsbBuildSelectInterfaceRequest(Urb
, GET_SELECT_INTERFACE_REQUEST_SIZE(DeviceExtension
->InterfaceList
[InterfaceIndex
].InterfaceDescriptor
->bNumEndpoints
), DeviceExtension
->ConfigurationHandle
, DeviceExtension
->InterfaceList
[InterfaceIndex
].InterfaceDescriptor
->bInterfaceNumber
, DeviceExtension
->InterfaceList
[InterfaceIndex
].InterfaceDescriptor
->bAlternateSetting
);
425 // now select the interface
427 Status
= USBCCGP_SyncUrbRequest(DeviceExtension
->NextDeviceObject
, Urb
);
432 if (NT_SUCCESS(Status
))
435 // update configuration info
437 ASSERT(Urb
->UrbSelectInterface
.Interface
.Length
== DeviceExtension
->InterfaceList
[InterfaceIndex
].Interface
->Length
);
438 RtlCopyMemory(DeviceExtension
->InterfaceList
[InterfaceIndex
].Interface
, &Urb
->UrbSelectInterface
.Interface
, Urb
->UrbSelectInterface
.Interface
.Length
);
453 USBCCGP_SelectConfiguration(
454 IN PDEVICE_OBJECT DeviceObject
,
455 IN PFDO_DEVICE_EXTENSION DeviceExtension
)
457 PUSBD_INTERFACE_INFORMATION InterfaceInformation
;
463 // now scan configuration descriptors
465 Status
= USBCCGP_ScanConfigurationDescriptor(DeviceExtension
, DeviceExtension
->ConfigurationDescriptor
);
466 if (!NT_SUCCESS(Status
))
475 // now allocate the urb
477 Urb
= USBD_CreateConfigurationRequestEx(DeviceExtension
->ConfigurationDescriptor
, DeviceExtension
->InterfaceList
);
483 return STATUS_INSUFFICIENT_RESOURCES
;
489 Status
= USBCCGP_SyncUrbRequest(DeviceExtension
->NextDeviceObject
, Urb
);
490 if (!NT_SUCCESS(Status
))
493 // failed to set configuration
495 DPRINT1("USBCCGP_SyncUrbRequest failed to set interface %x\n", Status
);
501 // get interface information
503 InterfaceInformation
= &Urb
->UrbSelectConfiguration
.Interface
;
504 for(Index
= 0; Index
< DeviceExtension
->InterfaceListCount
; Index
++)
507 // allocate buffer to store interface information
509 DeviceExtension
->InterfaceList
[Index
].Interface
= AllocateItem(NonPagedPool
, InterfaceInformation
->Length
);
510 if (!DeviceExtension
->InterfaceList
[Index
].Interface
)
515 return STATUS_INSUFFICIENT_RESOURCES
;
519 // copy interface information
521 RtlCopyMemory(DeviceExtension
->InterfaceList
[Index
].Interface
, InterfaceInformation
, InterfaceInformation
->Length
);
524 // move to next interface
526 InterfaceInformation
= (PUSBD_INTERFACE_INFORMATION
)((ULONG_PTR
)InterfaceInformation
+ InterfaceInformation
->Length
);
533 DeviceExtension
->ConfigurationHandle
= Urb
->UrbSelectConfiguration
.ConfigurationHandle
;
536 // free interface list & urb