[USBCCGP]
[reactos.git] / drivers / usb / usbccgp / function.c
1 /*
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.
6 * PROGRAMMERS:
7 * Michael Martin (michael.martin@reactos.org)
8 * Johannes Anderwald (johannes.anderwald@reactos.org)
9 * Cameron Gutman
10 */
11
12 #include "usbccgp.h"
13
14 NTSTATUS
15 USBCCGP_QueryInterface(
16 IN PDEVICE_OBJECT DeviceObject,
17 OUT PUSBC_DEVICE_CONFIGURATION_INTERFACE_V1 BusInterface)
18 {
19 KEVENT Event;
20 NTSTATUS Status;
21 PIRP Irp;
22 IO_STATUS_BLOCK IoStatus;
23 PIO_STACK_LOCATION Stack;
24
25 //
26 // sanity checks
27 //
28 ASSERT(DeviceObject);
29
30 //
31 // initialize event
32 //
33 KeInitializeEvent(&Event, NotificationEvent, FALSE);
34
35 //
36 // init interface
37 //
38 RtlZeroMemory(BusInterface, sizeof(USBC_DEVICE_CONFIGURATION_INTERFACE_V1));
39 BusInterface->Version = USBC_DEVICE_CONFIGURATION_INTERFACE_VERSION_1;
40 BusInterface->Size = sizeof(USBC_DEVICE_CONFIGURATION_INTERFACE_V1);
41
42 //
43 // create irp
44 //
45 Irp = IoBuildSynchronousFsdRequest(IRP_MJ_PNP,
46 DeviceObject,
47 NULL,
48 0,
49 NULL,
50 &Event,
51 &IoStatus);
52
53 //
54 // was irp built
55 //
56 if (Irp == NULL)
57 {
58 //
59 // no memory
60 //
61 return STATUS_INSUFFICIENT_RESOURCES;
62 }
63
64 //
65 // initialize request
66 //
67 Stack=IoGetNextIrpStackLocation(Irp);
68 Stack->MajorFunction = IRP_MJ_PNP;
69 Stack->MinorFunction = IRP_MN_QUERY_INTERFACE;
70 Stack->Parameters.QueryInterface.Size = sizeof(BUS_INTERFACE_STANDARD);
71 Stack->Parameters.QueryInterface.InterfaceType = (LPGUID)&USB_BUS_INTERFACE_USBC_CONFIGURATION_GUID;
72 Stack->Parameters.QueryInterface.Version = 2;
73 Stack->Parameters.QueryInterface.Interface = (PINTERFACE)&BusInterface;
74 Stack->Parameters.QueryInterface.InterfaceSpecificData = NULL;
75 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
76
77 //
78 // call driver
79 //
80 Status= IoCallDriver(DeviceObject, Irp);
81
82 //
83 // did operation complete
84 //
85 if (Status == STATUS_PENDING)
86 {
87 //
88 // wait for completion
89 //
90 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
91
92 //
93 // collect status
94 //
95 Status = IoStatus.Status;
96 }
97
98 return Status;
99 }
100
101 NTSTATUS
102 USBCCGP_CustomEnumWithInterface(
103 IN PDEVICE_OBJECT DeviceObject)
104 {
105 PFDO_DEVICE_EXTENSION FDODeviceExtension;
106 ULONG FunctionDescriptorBufferLength = 0;
107 NTSTATUS Status;
108 PUSBC_FUNCTION_DESCRIPTOR FunctionDescriptorBuffer = NULL;
109
110 //
111 // get device extension
112 //
113 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
114 ASSERT(FDODeviceExtension->Common.IsFDO);
115
116 if (FDODeviceExtension->BusInterface.StartDeviceCallback == NULL)
117 {
118 //
119 // not supported
120 //
121 return STATUS_NOT_SUPPORTED;
122 }
123
124 //
125 // invoke callback
126 //
127 Status = FDODeviceExtension->BusInterface.StartDeviceCallback(FDODeviceExtension->DeviceDescriptor,
128 FDODeviceExtension->ConfigurationDescriptor,
129 &FunctionDescriptorBuffer,
130 &FunctionDescriptorBufferLength,
131 DeviceObject,
132 FDODeviceExtension->PhysicalDeviceObject);
133
134 DPRINT1("USBCCGP_CustomEnumWithInterface Status %x\n", Status);
135 if (!NT_SUCCESS(Status))
136 {
137 //
138 // failed
139 //
140 return Status;
141 }
142
143 DPRINT1("FunctionDescriptorBufferLength %lu\n", FunctionDescriptorBufferLength);
144 DPRINT1("FunctionDescriptorBuffer %p\n", FunctionDescriptorBuffer);
145
146 //
147 // assume length % function buffer size
148 //
149 ASSERT(FunctionDescriptorBufferLength);
150 ASSERT(FunctionDescriptorBufferLength % sizeof(USBC_FUNCTION_DESCRIPTOR) == 0);
151
152 //
153 // store result
154 //
155 FDODeviceExtension->FunctionDescriptor = FunctionDescriptorBuffer;
156 FDODeviceExtension->FunctionDescriptorCount = FunctionDescriptorBufferLength / sizeof(USBC_FUNCTION_DESCRIPTOR);
157
158 //
159 // success
160 //
161 return STATUS_SUCCESS;
162 }
163
164 ULONG
165 USBCCGP_CountAssociationDescriptors(
166 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor)
167 {
168 PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR Descriptor;
169 PUCHAR Offset, End;
170 ULONG Count = 0;
171
172 //
173 // init offsets
174 //
175 Offset = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->bLength;
176 End = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength;
177
178 while(Offset < End)
179 {
180 //
181 // get association descriptor
182 //
183 Descriptor = (PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR)Offset;
184
185 if (Descriptor->bLength == sizeof(USB_INTERFACE_ASSOCIATION_DESCRIPTOR) && Descriptor->bDescriptorType == USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)
186 {
187 //
188 // found descriptor
189 //
190 Count++;
191 }
192
193 //
194 // move to next descriptor
195 //
196 Offset += Descriptor->bLength;
197 }
198
199 //
200 // done
201 //
202 return Count;
203 }
204
205 PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR
206 USBCCGP_GetAssociationDescriptorAtIndex(
207 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
208 IN ULONG Index)
209 {
210 PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR Descriptor;
211 PUCHAR Offset, End;
212 ULONG Count = 0;
213
214 //
215 // init offsets
216 //
217 Offset = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->bLength;
218 End = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength;
219
220 while(Offset < End)
221 {
222 //
223 // get association descriptor
224 //
225 Descriptor = (PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR)Offset;
226
227 if (Descriptor->bLength == sizeof(USB_INTERFACE_ASSOCIATION_DESCRIPTOR) && Descriptor->bDescriptorType == USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)
228 {
229 if (Index == Count)
230 {
231 //
232 // found descriptor
233 //
234 return Descriptor;
235 }
236
237 //
238 // not the searched one
239 //
240 Count++;
241 }
242
243 //
244 // move to next descriptor
245 //
246 Offset += Descriptor->bLength;
247 }
248
249 //
250 // failed to find descriptor at the specified index
251 //
252 return NULL;
253 }
254
255 NTSTATUS
256 USBCCGP_InitInterfaceListOfFunctionDescriptor(
257 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
258 IN PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR AssociationDescriptor,
259 OUT PUSBC_FUNCTION_DESCRIPTOR FunctionDescriptor)
260 {
261 PUSB_INTERFACE_DESCRIPTOR Descriptor;
262 PUCHAR Offset, End;
263 ULONG Count = 0;
264
265 //
266 // init offsets
267 //
268 Offset = (PUCHAR)AssociationDescriptor + AssociationDescriptor->bLength;
269 End = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength;
270
271 while(Offset < End)
272 {
273 //
274 // get association descriptor
275 //
276 Descriptor = (PUSB_INTERFACE_DESCRIPTOR)Offset;
277
278 if (Descriptor->bLength == sizeof(USB_INTERFACE_DESCRIPTOR) && Descriptor->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE)
279 {
280 //
281 // store interface descriptor
282 //
283 FunctionDescriptor->InterfaceDescriptorList[Count] = Descriptor;
284 Count++;
285
286 if (Count == AssociationDescriptor->bInterfaceCount)
287 {
288 //
289 // got all interfaces
290 //
291 return STATUS_SUCCESS;
292 }
293 }
294
295 if (Descriptor->bLength == sizeof(USB_INTERFACE_ASSOCIATION_DESCRIPTOR) && Descriptor->bDescriptorType == USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)
296 {
297 //
298 // WTF? a association descriptor which overlaps the next association descriptor
299 //
300 DPRINT1("Invalid association descriptor\n");
301 ASSERT(FALSE);
302 return STATUS_UNSUCCESSFUL;
303 }
304
305 //
306 // move to next descriptor
307 //
308 Offset += Descriptor->bLength;
309 }
310
311 //
312 // invalid association descriptor
313 //
314 DPRINT1("Invalid association descriptor\n");
315 return STATUS_UNSUCCESSFUL;
316 }
317
318 NTSTATUS
319 USBCCGP_InitFunctionDescriptor(
320 IN PFDO_DEVICE_EXTENSION FDODeviceExtension,
321 IN ULONG FunctionNumber,
322 OUT PUSBC_FUNCTION_DESCRIPTOR FunctionDescriptor)
323 {
324 PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR Descriptor;
325 NTSTATUS Status;
326 LPWSTR DescriptionBuffer;
327 WCHAR Buffer[100];
328 ULONG Index;
329
330 // init function number
331 FunctionDescriptor->FunctionNumber = (UCHAR)FunctionNumber;
332
333 // get association descriptor
334 Descriptor = USBCCGP_GetAssociationDescriptorAtIndex(FDODeviceExtension->ConfigurationDescriptor, FunctionNumber);
335 ASSERT(Descriptor);
336
337 // store number interfaces
338 FunctionDescriptor->NumberOfInterfaces = Descriptor->bInterfaceCount;
339
340 // allocate array for interface count
341 FunctionDescriptor->InterfaceDescriptorList = AllocateItem(NonPagedPool, sizeof(PUSB_INTERFACE_DESCRIPTOR) * Descriptor->bInterfaceCount);
342 if (FunctionDescriptor->InterfaceDescriptorList)
343 {
344 //
345 // no memory
346 //
347 return STATUS_INSUFFICIENT_RESOURCES;
348 }
349
350 // init interface list
351 Status = USBCCGP_InitInterfaceListOfFunctionDescriptor(FDODeviceExtension->ConfigurationDescriptor, Descriptor, FunctionDescriptor);
352 if (!NT_SUCCESS(Status))
353 {
354 //
355 // failed
356 //
357 return Status;
358 }
359
360 //
361 // now init interface description
362 //
363 if (Descriptor->iFunction)
364 {
365 //
366 // get interface description
367 //
368 Status = USBCCGP_GetDescriptor(FDODeviceExtension->NextDeviceObject,
369 USB_STRING_DESCRIPTOR_TYPE,
370 100 * sizeof(WCHAR),
371 Descriptor->iFunction,
372 0x0409, //FIXME
373 (PVOID*)&DescriptionBuffer);
374 if (!NT_SUCCESS(Status))
375 {
376 //
377 // no description
378 //
379 RtlInitUnicodeString(&FunctionDescriptor->FunctionDescription, L"");
380 }
381 else
382 {
383 //
384 // init description
385 //
386 RtlInitUnicodeString(&FunctionDescriptor->FunctionDescription, DescriptionBuffer);
387 }
388 DPRINT1("FunctionDescription %wZ\n", &FunctionDescriptor->FunctionDescription);
389 }
390
391 //
392 // now init hardware id
393 //
394 Index = swprintf(Buffer, L"USB\\VID_%04x&PID_%04x&Rev_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
395 FDODeviceExtension->DeviceDescriptor->idProduct,
396 FDODeviceExtension->DeviceDescriptor->bcdDevice,
397 Descriptor->bFirstInterface) + 1;
398 Index = swprintf(&Buffer[Index], L"USB\\VID_%04x&PID_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
399 FDODeviceExtension->DeviceDescriptor->idProduct,
400 Descriptor->bFirstInterface) + 1;
401
402 // allocate result buffer
403 DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
404 if (!DescriptionBuffer)
405 {
406 //
407 // failed to allocate memory
408 //
409 return STATUS_INSUFFICIENT_RESOURCES;
410 }
411
412 // copy description
413 RtlCopyMemory(DescriptionBuffer, Buffer, Index * sizeof(WCHAR));
414 RtlInitUnicodeString(&FunctionDescriptor->HardwareId, DescriptionBuffer);
415
416 //
417 // now init the compatible id
418 //
419 Index = swprintf(Buffer, L"USB\\Class_%02x&SubClass_%02x&Prot_%02x", Descriptor->bFunctionClass, Descriptor->bFunctionSubClass, Descriptor->bFunctionProtocol) + 1;
420 Index = swprintf(&Buffer[Index], L"USB\\Class_%04x&SubClass_%04x", Descriptor->bFunctionClass, Descriptor->bFunctionSubClass) + 1;
421 Index = swprintf(&Buffer[Index], L"USB\\Class_%04x", Descriptor->bFunctionClass) + 1;
422
423 // allocate result buffer
424 DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
425 if (!DescriptionBuffer)
426 {
427 //
428 // failed to allocate memory
429 //
430 return STATUS_INSUFFICIENT_RESOURCES;
431 }
432
433 // copy description
434 // copy description
435 RtlCopyMemory(DescriptionBuffer, Buffer, Index * sizeof(WCHAR));
436 RtlInitUnicodeString(&FunctionDescriptor->CompatibleId, DescriptionBuffer);
437
438 //
439 // done
440 //
441 return STATUS_SUCCESS;
442 }
443
444 NTSTATUS
445 USBCCGP_EnumWithAssociationDescriptor(
446 IN PDEVICE_OBJECT DeviceObject)
447 {
448 ULONG DescriptorCount, Index;
449 PFDO_DEVICE_EXTENSION FDODeviceExtension;
450 NTSTATUS Status = STATUS_SUCCESS;
451
452 //
453 // get device extension
454 //
455 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
456 ASSERT(FDODeviceExtension->Common.IsFDO);
457
458 //
459 // count association descriptors
460 //
461 DescriptorCount = USBCCGP_CountAssociationDescriptors(FDODeviceExtension->ConfigurationDescriptor);
462 if (!DescriptorCount)
463 {
464 //
465 // no descriptors found
466 //
467 return STATUS_NOT_SUPPORTED;
468 }
469
470 //
471 // allocate function descriptor array
472 //
473 FDODeviceExtension->FunctionDescriptor = AllocateItem(NonPagedPool, sizeof(USBC_FUNCTION_DESCRIPTOR) * DescriptorCount);
474 if (!FDODeviceExtension->FunctionDescriptorCount)
475 {
476 //
477 // no memory
478 //
479 DPRINT1("USBCCGP_EnumWithAssociationDescriptor failed to allocate function descriptor count %x\n", DescriptorCount);
480 return STATUS_INSUFFICIENT_RESOURCES;
481 }
482
483 for(Index = 0; Index < DescriptorCount; Index++)
484 {
485 //
486 // init function descriptors
487 //
488 Status = USBCCGP_InitFunctionDescriptor(FDODeviceExtension, Index, &FDODeviceExtension->FunctionDescriptor[Index]);
489 if (!NT_SUCCESS(Status))
490 {
491 //
492 // failed
493 //
494 return Status;
495 }
496 }
497
498 //
499 // store function descriptor count
500 //
501 FDODeviceExtension->FunctionDescriptorCount = DescriptorCount;
502
503 //
504 // done
505 //
506 return Status;
507 }
508
509
510 NTSTATUS
511 USBCCGP_EnumWithAudioLegacy(
512 IN PDEVICE_OBJECT DeviceObject)
513 {
514 UNIMPLEMENTED
515 return STATUS_NOT_IMPLEMENTED;
516 }
517
518 NTSTATUS
519 USBCCGP_EnumWithUnionFunctionDescriptors(
520 IN PDEVICE_OBJECT DeviceObject)
521 {
522 UNIMPLEMENTED
523 return STATUS_NOT_IMPLEMENTED;
524 }
525
526
527 NTSTATUS
528 USBCCGP_EnumerateFunctions(
529 IN PDEVICE_OBJECT DeviceObject)
530 {
531 NTSTATUS Status;
532 PFDO_DEVICE_EXTENSION FDODeviceExtension;
533
534 //
535 // get device extension
536 //
537 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
538 ASSERT(FDODeviceExtension->Common.IsFDO);
539
540 //
541 // first try with filter driver
542 //
543 Status = USBCCGP_CustomEnumWithInterface(DeviceObject);
544 if (NT_SUCCESS(Status))
545 {
546 //
547 // succeeded
548 //
549 return Status;
550 }
551
552 //
553 // enumerate functions with interface association descriptor
554 //
555 Status = USBCCGP_EnumWithAssociationDescriptor(DeviceObject);
556 if (NT_SUCCESS(Status))
557 {
558 //
559 // succeeded
560 //
561 return Status;
562 }
563
564 //
565 // try with union function descriptors
566 //
567 Status = USBCCGP_EnumWithUnionFunctionDescriptors(DeviceObject);
568 if (NT_SUCCESS(Status))
569 {
570 //
571 // succeeded
572 //
573 return Status;
574 }
575
576 //
577 // try with legacy audio methods
578 //
579 return USBCCGP_EnumWithAudioLegacy(DeviceObject);
580 }