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 CountInterfaceDescriptors(
225 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
)
227 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
228 PVOID CurrentPosition
;
232 // enumerate all interfaces
234 CurrentPosition
= ConfigurationDescriptor
;
238 // find next descriptor
240 InterfaceDescriptor
= USBD_ParseConfigurationDescriptorEx(ConfigurationDescriptor
, CurrentPosition
, -1, -1, -1, -1, -1);
241 if (!InterfaceDescriptor
)
245 // advance to next descriptor
247 CurrentPosition
= (PVOID
)((ULONG_PTR
)InterfaceDescriptor
+ InterfaceDescriptor
->bLength
);
250 // increment descriptor count
263 AllocateInterfaceDescriptorsArray(
264 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
,
265 OUT PUSB_INTERFACE_DESCRIPTOR
**OutArray
)
267 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
268 PVOID CurrentPosition
;
270 PUSB_INTERFACE_DESCRIPTOR
*Array
;
272 Count
= CountInterfaceDescriptors(ConfigurationDescriptor
);
278 Array
= AllocateItem(NonPagedPool
, sizeof(PUSB_INTERFACE_DESCRIPTOR
) * Count
);
280 return STATUS_INSUFFICIENT_RESOURCES
;
283 // enumerate all interfaces
285 CurrentPosition
= ConfigurationDescriptor
;
290 // find next descriptor
292 InterfaceDescriptor
= USBD_ParseConfigurationDescriptorEx(ConfigurationDescriptor
, CurrentPosition
, -1, -1, -1, -1, -1);
293 if (!InterfaceDescriptor
)
299 Array
[Count
] = InterfaceDescriptor
;
303 // advance to next descriptor
305 CurrentPosition
= (PVOID
)((ULONG_PTR
)InterfaceDescriptor
+ InterfaceDescriptor
->bLength
);
317 return STATUS_SUCCESS
;
322 USBCCGP_ScanConfigurationDescriptor(
323 IN OUT PFDO_DEVICE_EXTENSION FDODeviceExtension
,
324 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
)
326 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
327 ULONG InterfaceIndex
= 0;
328 PVOID CurrentPosition
;
329 ULONG DescriptorCount
;
334 ASSERT(ConfigurationDescriptor
);
335 ASSERT(ConfigurationDescriptor
->bNumInterfaces
);
338 // count all interface descriptors
340 DescriptorCount
= ConfigurationDescriptor
->bNumInterfaces
;
343 // allocate array holding the interface descriptors
345 FDODeviceExtension
->InterfaceList
= AllocateItem(NonPagedPool
, sizeof(USBD_INTERFACE_LIST_ENTRY
) * (DescriptorCount
+ 1));
346 if (!FDODeviceExtension
->InterfaceList
)
351 return STATUS_INSUFFICIENT_RESOURCES
;
354 CurrentPosition
= ConfigurationDescriptor
;
358 // parse configuration descriptor
360 InterfaceDescriptor
= USBD_ParseConfigurationDescriptorEx(ConfigurationDescriptor
, ConfigurationDescriptor
, InterfaceIndex
, -1, -1, -1, -1);
361 ASSERT(InterfaceDescriptor
);
362 if (InterfaceDescriptor
)
365 // store in interface list
367 FDODeviceExtension
->InterfaceList
[FDODeviceExtension
->InterfaceListCount
].InterfaceDescriptor
= InterfaceDescriptor
;
368 FDODeviceExtension
->InterfaceListCount
++;
369 CurrentPosition
= (PVOID
)((ULONG_PTR
)InterfaceDescriptor
+ InterfaceDescriptor
->bLength
);
373 // move to next interface
377 }while(InterfaceIndex
< DescriptorCount
);
382 ASSERT(FDODeviceExtension
->InterfaceListCount
);
387 return STATUS_SUCCESS
;
391 DumpConfigurationDescriptor(PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
)
393 DPRINT1("Dumping ConfigurationDescriptor %x\n", ConfigurationDescriptor
);
394 DPRINT1("bLength %x\n", ConfigurationDescriptor
->bLength
);
395 DPRINT1("bDescriptorType %x\n", ConfigurationDescriptor
->bDescriptorType
);
396 DPRINT1("wTotalLength %x\n", ConfigurationDescriptor
->wTotalLength
);
397 DPRINT1("bNumInterfaces %x\n", ConfigurationDescriptor
->bNumInterfaces
);
398 DPRINT1("bConfigurationValue %x\n", ConfigurationDescriptor
->bConfigurationValue
);
399 DPRINT1("iConfiguration %x\n", ConfigurationDescriptor
->iConfiguration
);
400 DPRINT1("bmAttributes %x\n", ConfigurationDescriptor
->bmAttributes
);
401 DPRINT1("MaxPower %x\n", ConfigurationDescriptor
->MaxPower
);
405 USBCCGP_SelectInterface(
406 IN PDEVICE_OBJECT DeviceObject
,
407 IN PFDO_DEVICE_EXTENSION DeviceExtension
,
408 IN ULONG InterfaceIndex
)
416 Urb
= AllocateItem(NonPagedPool
, GET_SELECT_INTERFACE_REQUEST_SIZE(DeviceExtension
->InterfaceList
[InterfaceIndex
].InterfaceDescriptor
->bNumEndpoints
));
422 return STATUS_INSUFFICIENT_RESOURCES
;
426 // now prepare interface urb
428 UsbBuildSelectInterfaceRequest(Urb
, GET_SELECT_INTERFACE_REQUEST_SIZE(DeviceExtension
->InterfaceList
[InterfaceIndex
].InterfaceDescriptor
->bNumEndpoints
), DeviceExtension
->ConfigurationHandle
, DeviceExtension
->InterfaceList
[InterfaceIndex
].InterfaceDescriptor
->bInterfaceNumber
, DeviceExtension
->InterfaceList
[InterfaceIndex
].InterfaceDescriptor
->bAlternateSetting
);
431 // now select the interface
433 Status
= USBCCGP_SyncUrbRequest(DeviceExtension
->NextDeviceObject
, Urb
);
438 if (NT_SUCCESS(Status
))
441 // update configuration info
443 ASSERT(Urb
->UrbSelectInterface
.Interface
.Length
== DeviceExtension
->InterfaceList
[InterfaceIndex
].Interface
->Length
);
444 RtlCopyMemory(DeviceExtension
->InterfaceList
[InterfaceIndex
].Interface
, &Urb
->UrbSelectInterface
.Interface
, Urb
->UrbSelectInterface
.Interface
.Length
);
459 USBCCGP_SelectConfiguration(
460 IN PDEVICE_OBJECT DeviceObject
,
461 IN PFDO_DEVICE_EXTENSION DeviceExtension
)
463 PUSBD_INTERFACE_INFORMATION InterfaceInformation
;
469 // now scan configuration descriptors
471 Status
= USBCCGP_ScanConfigurationDescriptor(DeviceExtension
, DeviceExtension
->ConfigurationDescriptor
);
472 if (!NT_SUCCESS(Status
))
481 // now allocate the urb
483 Urb
= USBD_CreateConfigurationRequestEx(DeviceExtension
->ConfigurationDescriptor
, DeviceExtension
->InterfaceList
);
489 return STATUS_INSUFFICIENT_RESOURCES
;
495 Status
= USBCCGP_SyncUrbRequest(DeviceExtension
->NextDeviceObject
, Urb
);
496 if (!NT_SUCCESS(Status
))
499 // failed to set configuration
501 DPRINT1("USBCCGP_SyncUrbRequest failed to set interface %x\n", Status
);
507 // get interface information
509 InterfaceInformation
= &Urb
->UrbSelectConfiguration
.Interface
;
510 for(Index
= 0; Index
< DeviceExtension
->InterfaceListCount
; Index
++)
513 // allocate buffer to store interface information
515 DeviceExtension
->InterfaceList
[Index
].Interface
= AllocateItem(NonPagedPool
, InterfaceInformation
->Length
);
516 if (!DeviceExtension
->InterfaceList
[Index
].Interface
)
521 return STATUS_INSUFFICIENT_RESOURCES
;
525 // copy interface information
527 RtlCopyMemory(DeviceExtension
->InterfaceList
[Index
].Interface
, InterfaceInformation
, InterfaceInformation
->Length
);
530 // move to next interface
532 InterfaceInformation
= (PUSBD_INTERFACE_INFORMATION
)((ULONG_PTR
)InterfaceInformation
+ InterfaceInformation
->Length
);
539 DeviceExtension
->ConfigurationHandle
= Urb
->UrbSelectConfiguration
.ConfigurationHandle
;
542 // free interface list & urb