Synchronize with trunk r58528.
[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 /* Sanity checks */
26 ASSERT(DeviceObject);
27
28 /* Initialize event */
29 KeInitializeEvent(&Event, NotificationEvent, FALSE);
30
31 /* Init interface */
32 RtlZeroMemory(BusInterface, sizeof(USBC_DEVICE_CONFIGURATION_INTERFACE_V1));
33 BusInterface->Version = USBC_DEVICE_CONFIGURATION_INTERFACE_VERSION_1;
34 BusInterface->Size = sizeof(USBC_DEVICE_CONFIGURATION_INTERFACE_V1);
35
36 /* Create irp */
37 Irp = IoBuildSynchronousFsdRequest(IRP_MJ_PNP,
38 DeviceObject,
39 NULL,
40 0,
41 NULL,
42 &Event,
43 &IoStatus);
44
45 //
46 // was irp built
47 //
48 if (Irp == NULL)
49 {
50 //
51 // no memory
52 //
53 return STATUS_INSUFFICIENT_RESOURCES;
54 }
55
56 //
57 // initialize request
58 //
59 Stack = IoGetNextIrpStackLocation(Irp);
60 Stack->MajorFunction = IRP_MJ_PNP;
61 Stack->MinorFunction = IRP_MN_QUERY_INTERFACE;
62 Stack->Parameters.QueryInterface.Size = sizeof(BUS_INTERFACE_STANDARD);
63 Stack->Parameters.QueryInterface.InterfaceType = (LPGUID)&USB_BUS_INTERFACE_USBC_CONFIGURATION_GUID;
64 Stack->Parameters.QueryInterface.Version = 2;
65 Stack->Parameters.QueryInterface.Interface = (PINTERFACE)&BusInterface;
66 Stack->Parameters.QueryInterface.InterfaceSpecificData = NULL;
67 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
68
69 //
70 // call driver
71 //
72 Status = IoCallDriver(DeviceObject, Irp);
73
74 //
75 // did operation complete
76 //
77 if (Status == STATUS_PENDING)
78 {
79 //
80 // wait for completion
81 //
82 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
83
84 //
85 // collect status
86 //
87 Status = IoStatus.Status;
88 }
89
90 return Status;
91 }
92
93 NTSTATUS
94 USBCCGP_CustomEnumWithInterface(
95 IN PDEVICE_OBJECT DeviceObject)
96 {
97 PFDO_DEVICE_EXTENSION FDODeviceExtension;
98 ULONG FunctionDescriptorBufferLength = 0;
99 NTSTATUS Status;
100 PUSBC_FUNCTION_DESCRIPTOR FunctionDescriptorBuffer = NULL;
101
102 //
103 // get device extension
104 //
105 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
106 ASSERT(FDODeviceExtension->Common.IsFDO);
107
108 if (FDODeviceExtension->BusInterface.StartDeviceCallback == NULL)
109 {
110 //
111 // not supported
112 //
113 return STATUS_NOT_SUPPORTED;
114 }
115
116 //
117 // invoke callback
118 //
119 Status = FDODeviceExtension->BusInterface.StartDeviceCallback(FDODeviceExtension->DeviceDescriptor,
120 FDODeviceExtension->ConfigurationDescriptor,
121 &FunctionDescriptorBuffer,
122 &FunctionDescriptorBufferLength,
123 DeviceObject,
124 FDODeviceExtension->PhysicalDeviceObject);
125
126 DPRINT("USBCCGP_CustomEnumWithInterface Status %lx\n", Status);
127 if (!NT_SUCCESS(Status))
128 {
129 //
130 // failed
131 //
132 return Status;
133 }
134
135 DPRINT("FunctionDescriptorBufferLength %lu\n", FunctionDescriptorBufferLength);
136 DPRINT("FunctionDescriptorBuffer %p\n", FunctionDescriptorBuffer);
137
138 //
139 // assume length % function buffer size
140 //
141 ASSERT(FunctionDescriptorBufferLength);
142 ASSERT(FunctionDescriptorBufferLength % sizeof(USBC_FUNCTION_DESCRIPTOR) == 0);
143
144 //
145 // store result
146 //
147 FDODeviceExtension->FunctionDescriptor = FunctionDescriptorBuffer;
148 FDODeviceExtension->FunctionDescriptorCount = FunctionDescriptorBufferLength / sizeof(USBC_FUNCTION_DESCRIPTOR);
149
150 //
151 // success
152 //
153 return STATUS_SUCCESS;
154 }
155
156 ULONG
157 USBCCGP_CountAssociationDescriptors(
158 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor)
159 {
160 PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR Descriptor;
161 PUCHAR Offset, End;
162 ULONG Count = 0;
163
164 //
165 // init offsets
166 //
167 Offset = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->bLength;
168 End = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength;
169
170 while (Offset < End)
171 {
172 //
173 // get association descriptor
174 //
175 Descriptor = (PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR)Offset;
176
177 if (Descriptor->bLength == sizeof(USB_INTERFACE_ASSOCIATION_DESCRIPTOR) && Descriptor->bDescriptorType == USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)
178 {
179 //
180 // found descriptor
181 //
182 Count++;
183 }
184
185 //
186 // move to next descriptor
187 //
188 Offset += Descriptor->bLength;
189 }
190
191 //
192 // done
193 //
194 return Count;
195 }
196
197 PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR
198 USBCCGP_GetAssociationDescriptorAtIndex(
199 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
200 IN ULONG Index)
201 {
202 PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR Descriptor;
203 PUCHAR Offset, End;
204 ULONG Count = 0;
205
206 //
207 // init offsets
208 //
209 Offset = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->bLength;
210 End = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength;
211
212 while (Offset < End)
213 {
214 //
215 // get association descriptor
216 //
217 Descriptor = (PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR)Offset;
218
219 if (Descriptor->bLength == sizeof(USB_INTERFACE_ASSOCIATION_DESCRIPTOR) && Descriptor->bDescriptorType == USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)
220 {
221 if (Index == Count)
222 {
223 //
224 // found descriptor
225 //
226 return Descriptor;
227 }
228
229 //
230 // not the searched one
231 //
232 Count++;
233 }
234
235 //
236 // move to next descriptor
237 //
238 Offset += Descriptor->bLength;
239 }
240
241 //
242 // failed to find descriptor at the specified index
243 //
244 return NULL;
245 }
246
247 NTSTATUS
248 USBCCGP_InitInterfaceListOfFunctionDescriptor(
249 IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
250 IN PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR AssociationDescriptor,
251 OUT PUSBC_FUNCTION_DESCRIPTOR FunctionDescriptor)
252 {
253 PUSB_INTERFACE_DESCRIPTOR Descriptor;
254 PUCHAR Offset, End;
255 ULONG Count = 0;
256
257 //
258 // init offsets
259 //
260 Offset = (PUCHAR)AssociationDescriptor + AssociationDescriptor->bLength;
261 End = (PUCHAR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength;
262
263 while (Offset < End)
264 {
265 //
266 // get association descriptor
267 //
268 Descriptor = (PUSB_INTERFACE_DESCRIPTOR)Offset;
269
270 if (Descriptor->bLength == sizeof(USB_INTERFACE_DESCRIPTOR) && Descriptor->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE)
271 {
272 //
273 // store interface descriptor
274 //
275 FunctionDescriptor->InterfaceDescriptorList[Count] = Descriptor;
276 Count++;
277
278 if (Count == AssociationDescriptor->bInterfaceCount)
279 {
280 //
281 // got all interfaces
282 //
283 return STATUS_SUCCESS;
284 }
285 }
286
287 if (Descriptor->bLength == sizeof(USB_INTERFACE_ASSOCIATION_DESCRIPTOR) && Descriptor->bDescriptorType == USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)
288 {
289 //
290 // WTF? a association descriptor which overlaps the next association descriptor
291 //
292 DPRINT1("Invalid association descriptor\n");
293 ASSERT(FALSE);
294 return STATUS_UNSUCCESSFUL;
295 }
296
297 //
298 // move to next descriptor
299 //
300 Offset += Descriptor->bLength;
301 }
302
303 //
304 // invalid association descriptor
305 //
306 DPRINT1("Invalid association descriptor\n");
307 return STATUS_UNSUCCESSFUL;
308 }
309
310 NTSTATUS
311 USBCCGP_InitFunctionDescriptor(
312 IN PFDO_DEVICE_EXTENSION FDODeviceExtension,
313 IN ULONG FunctionNumber,
314 OUT PUSBC_FUNCTION_DESCRIPTOR FunctionDescriptor)
315 {
316 PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR Descriptor;
317 NTSTATUS Status;
318 LPWSTR DescriptionBuffer;
319 WCHAR Buffer[100];
320 ULONG Index;
321
322 // init function number
323 FunctionDescriptor->FunctionNumber = (UCHAR)FunctionNumber;
324
325 // get association descriptor
326 Descriptor = USBCCGP_GetAssociationDescriptorAtIndex(FDODeviceExtension->ConfigurationDescriptor, FunctionNumber);
327 ASSERT(Descriptor);
328
329 // store number interfaces
330 FunctionDescriptor->NumberOfInterfaces = Descriptor->bInterfaceCount;
331
332 // allocate array for interface count
333 FunctionDescriptor->InterfaceDescriptorList = AllocateItem(NonPagedPool, sizeof(PUSB_INTERFACE_DESCRIPTOR) * Descriptor->bInterfaceCount);
334 if (!FunctionDescriptor->InterfaceDescriptorList)
335 {
336 //
337 // no memory
338 //
339 return STATUS_INSUFFICIENT_RESOURCES;
340 }
341
342 // init interface list
343 Status = USBCCGP_InitInterfaceListOfFunctionDescriptor(FDODeviceExtension->ConfigurationDescriptor, Descriptor, FunctionDescriptor);
344 if (!NT_SUCCESS(Status))
345 {
346 //
347 // failed
348 //
349 return Status;
350 }
351
352 //
353 // now init interface description
354 //
355 if (Descriptor->iFunction)
356 {
357 //
358 // get interface description
359 //
360 Status = USBCCGP_GetStringDescriptor(FDODeviceExtension->NextDeviceObject,
361 100 * sizeof(WCHAR),
362 Descriptor->iFunction,
363 0x0409, //FIXME
364 (PVOID*)&DescriptionBuffer);
365 if (!NT_SUCCESS(Status))
366 {
367 //
368 // no description
369 //
370 RtlInitUnicodeString(&FunctionDescriptor->FunctionDescription, L"");
371 }
372 else
373 {
374 //
375 // init description
376 //
377 RtlInitUnicodeString(&FunctionDescriptor->FunctionDescription, DescriptionBuffer);
378 }
379 DPRINT1("FunctionDescription %wZ\n", &FunctionDescriptor->FunctionDescription);
380 }
381
382 //
383 // now init hardware id
384 //
385 Index = swprintf(Buffer, L"USB\\VID_%04x&PID_%04x&Rev_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
386 FDODeviceExtension->DeviceDescriptor->idProduct,
387 FDODeviceExtension->DeviceDescriptor->bcdDevice,
388 Descriptor->bFirstInterface) + 1;
389 Index += swprintf(&Buffer[Index], L"USB\\VID_%04x&PID_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
390 FDODeviceExtension->DeviceDescriptor->idProduct,
391 Descriptor->bFirstInterface) + 1;
392
393 // allocate result buffer
394 DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
395 if (!DescriptionBuffer)
396 {
397 //
398 // failed to allocate memory
399 //
400 return STATUS_INSUFFICIENT_RESOURCES;
401 }
402
403 // copy description
404 RtlCopyMemory(DescriptionBuffer, Buffer, (Index + 1) * sizeof(WCHAR));
405 FunctionDescriptor->HardwareId.Buffer = DescriptionBuffer;
406 FunctionDescriptor->HardwareId.Length = Index * sizeof(WCHAR);
407 FunctionDescriptor->HardwareId.MaximumLength = (Index + 1) * sizeof(WCHAR);
408
409 //
410 // now init the compatible id
411 //
412 Index = swprintf(Buffer, L"USB\\Class_%02x&SubClass_%02x&Prot_%02x", Descriptor->bFunctionClass, Descriptor->bFunctionSubClass, Descriptor->bFunctionProtocol) + 1;
413 Index += swprintf(&Buffer[Index], L"USB\\Class_%02x&SubClass_%02x", Descriptor->bFunctionClass, Descriptor->bFunctionSubClass) + 1;
414 Index += swprintf(&Buffer[Index], L"USB\\Class_%02x", Descriptor->bFunctionClass) + 1;
415
416 // allocate result buffer
417 DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
418 if (!DescriptionBuffer)
419 {
420 //
421 // failed to allocate memory
422 //
423 return STATUS_INSUFFICIENT_RESOURCES;
424 }
425
426 // copy description
427 RtlCopyMemory(DescriptionBuffer, Buffer, (Index + 1) * sizeof(WCHAR));
428 FunctionDescriptor->CompatibleId.Buffer = DescriptionBuffer;
429 FunctionDescriptor->CompatibleId.Length = Index * sizeof(WCHAR);
430 FunctionDescriptor->CompatibleId.MaximumLength = (Index + 1) * sizeof(WCHAR);
431
432 //
433 // done
434 //
435 return STATUS_SUCCESS;
436 }
437
438 NTSTATUS
439 USBCCGP_EnumWithAssociationDescriptor(
440 IN PDEVICE_OBJECT DeviceObject)
441 {
442 ULONG DescriptorCount, Index;
443 PFDO_DEVICE_EXTENSION FDODeviceExtension;
444 NTSTATUS Status = STATUS_SUCCESS;
445
446 //
447 // get device extension
448 //
449 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
450 ASSERT(FDODeviceExtension->Common.IsFDO);
451
452 //
453 // count association descriptors
454 //
455 DescriptorCount = USBCCGP_CountAssociationDescriptors(FDODeviceExtension->ConfigurationDescriptor);
456 if (!DescriptorCount)
457 {
458 //
459 // no descriptors found
460 //
461 return STATUS_NOT_SUPPORTED;
462 }
463
464 //
465 // allocate function descriptor array
466 //
467 FDODeviceExtension->FunctionDescriptor = AllocateItem(NonPagedPool, sizeof(USBC_FUNCTION_DESCRIPTOR) * DescriptorCount);
468 if (!FDODeviceExtension->FunctionDescriptor)
469 {
470 //
471 // no memory
472 //
473 DPRINT1("USBCCGP_EnumWithAssociationDescriptor failed to allocate function descriptor count %x\n", DescriptorCount);
474 return STATUS_INSUFFICIENT_RESOURCES;
475 }
476
477 for (Index = 0; Index < DescriptorCount; Index++)
478 {
479 //
480 // init function descriptors
481 //
482 Status = USBCCGP_InitFunctionDescriptor(FDODeviceExtension, Index, &FDODeviceExtension->FunctionDescriptor[Index]);
483 if (!NT_SUCCESS(Status))
484 {
485 //
486 // failed
487 //
488 return Status;
489 }
490 }
491
492 //
493 // store function descriptor count
494 //
495 FDODeviceExtension->FunctionDescriptorCount = DescriptorCount;
496
497 //
498 // done
499 //
500 return Status;
501 }
502
503 NTSTATUS
504 USBCCG_InitIdsWithInterfaceDescriptor(
505 IN PFDO_DEVICE_EXTENSION FDODeviceExtension,
506 IN PUSB_INTERFACE_DESCRIPTOR Descriptor,
507 IN ULONG FunctionIndex,
508 OUT PUSBC_FUNCTION_DESCRIPTOR FunctionDescriptor)
509 {
510 ULONG Index;
511 WCHAR Buffer[200];
512 LPWSTR DescriptionBuffer;
513 NTSTATUS Status;
514
515 //
516 // now init interface description
517 //
518 if (Descriptor->iInterface)
519 {
520 //
521 // get interface description
522 //
523 Status = USBCCGP_GetStringDescriptor(FDODeviceExtension->NextDeviceObject,
524 100 * sizeof(WCHAR),
525 Descriptor->iInterface,
526 0x0409, //FIXME
527 (PVOID*)&DescriptionBuffer);
528 if (!NT_SUCCESS(Status))
529 {
530 //
531 // no description
532 //
533 RtlInitUnicodeString(&FunctionDescriptor->FunctionDescription, L"");
534 }
535 else
536 {
537 //
538 // init description
539 //
540 RtlInitUnicodeString(&FunctionDescriptor->FunctionDescription, DescriptionBuffer);
541 }
542 DPRINT1("FunctionDescription %wZ\n", &FunctionDescriptor->FunctionDescription);
543 }
544
545
546 //
547 // now init hardware id
548 //
549 Index = swprintf(Buffer, L"USB\\VID_%04x&PID_%04x&Rev_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
550 FDODeviceExtension->DeviceDescriptor->idProduct,
551 FDODeviceExtension->DeviceDescriptor->bcdDevice,
552 FunctionIndex) + 1;
553 Index += swprintf(&Buffer[Index], L"USB\\VID_%04x&PID_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
554 FDODeviceExtension->DeviceDescriptor->idProduct,
555 FunctionIndex) + 1;
556
557 // allocate result buffer
558 DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
559 if (!DescriptionBuffer)
560 {
561 //
562 // failed to allocate memory
563 //
564 return STATUS_INSUFFICIENT_RESOURCES;
565 }
566
567 // copy description
568 RtlCopyMemory(DescriptionBuffer, Buffer, (Index + 1) * sizeof(WCHAR));
569 FunctionDescriptor->HardwareId.Buffer = DescriptionBuffer;
570 FunctionDescriptor->HardwareId.Length = Index * sizeof(WCHAR);
571 FunctionDescriptor->HardwareId.MaximumLength = (Index + 1) * sizeof(WCHAR);
572
573 //
574 // now init the compatible id
575 //
576 Index = swprintf(Buffer, L"USB\\Class_%02x&SubClass_%02x&Prot_%02x", Descriptor->bInterfaceClass, Descriptor->bInterfaceSubClass, Descriptor->bInterfaceProtocol) + 1;
577 Index += swprintf(&Buffer[Index], L"USB\\Class_%02x&SubClass_%02x", Descriptor->bInterfaceClass, Descriptor->bInterfaceSubClass) + 1;
578 Index += swprintf(&Buffer[Index], L"USB\\Class_%02x", Descriptor->bInterfaceClass) + 1;
579
580 // allocate result buffer
581 DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
582 if (!DescriptionBuffer)
583 {
584 //
585 // failed to allocate memory
586 //
587 return STATUS_INSUFFICIENT_RESOURCES;
588 }
589
590 // copy description
591 RtlCopyMemory(DescriptionBuffer, Buffer, (Index + 1) * sizeof(WCHAR));
592 FunctionDescriptor->CompatibleId.Buffer = DescriptionBuffer;
593 FunctionDescriptor->CompatibleId.Length = Index * sizeof(WCHAR);
594 FunctionDescriptor->CompatibleId.MaximumLength = (Index + 1) * sizeof(WCHAR);
595
596 //
597 // done
598 //
599 return STATUS_SUCCESS;
600 }
601
602
603 NTSTATUS
604 USBCCGP_LegacyEnum(
605 IN PDEVICE_OBJECT DeviceObject)
606 {
607 ULONG Index;
608 PFDO_DEVICE_EXTENSION FDODeviceExtension;
609 NTSTATUS Status = STATUS_SUCCESS;
610 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
611
612 //
613 // get device extension
614 //
615 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
616 ASSERT(FDODeviceExtension->Common.IsFDO);
617
618 //
619 // sanity check
620 //
621 ASSERT(FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces);
622
623 //
624 // allocate function array
625 //
626 FDODeviceExtension->FunctionDescriptor = AllocateItem(NonPagedPool, sizeof(USBC_FUNCTION_DESCRIPTOR) * FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces);
627 if (!FDODeviceExtension->FunctionDescriptor)
628 {
629 //
630 // no memory
631 //
632 DPRINT1("USBCCGP_EnumWithAssociationDescriptor failed to allocate function descriptor %lu\n", FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces);
633 return STATUS_INSUFFICIENT_RESOURCES;
634 }
635
636 //
637 // init function descriptors
638 //
639 for (Index = 0; Index < FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces; Index++)
640 {
641 // get interface descriptor
642 InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx(FDODeviceExtension->ConfigurationDescriptor, FDODeviceExtension->ConfigurationDescriptor, Index, 0, -1, -1, -1);
643 if (InterfaceDescriptor == NULL)
644 {
645 //
646 // failed to find interface descriptor
647 //
648 DPRINT1("[USBCCGP] Failed to find interface descriptor index %lu\n", Index);
649 ASSERT(FALSE);
650 return STATUS_UNSUCCESSFUL;
651 }
652
653 //
654 // init function descriptor
655 //
656 FDODeviceExtension->FunctionDescriptor[Index].FunctionNumber = Index;
657 FDODeviceExtension->FunctionDescriptor[Index].NumberOfInterfaces = 1;
658 FDODeviceExtension->FunctionDescriptor[Index].InterfaceDescriptorList = AllocateItem(NonPagedPool, sizeof(PUSB_INTERFACE_DESCRIPTOR) * 1);
659 if (!FDODeviceExtension->FunctionDescriptor[Index].InterfaceDescriptorList)
660 {
661 //
662 // no memory
663 //
664 return STATUS_INSUFFICIENT_RESOURCES;
665 }
666
667 //
668 // store interface descriptor
669 //
670 FDODeviceExtension->FunctionDescriptor[Index].InterfaceDescriptorList[0] = InterfaceDescriptor;
671
672 //
673 // now init the device ids
674 //
675 Status = USBCCG_InitIdsWithInterfaceDescriptor(FDODeviceExtension, InterfaceDescriptor, Index, &FDODeviceExtension->FunctionDescriptor[Index]);
676 if (!NT_SUCCESS(Status))
677 {
678 //
679 // failed to init ids
680 //
681 DPRINT1("[USBCCGP] Failed to init ids with %lx\n", Status);
682 return Status;
683 }
684
685 //
686 // store function count
687 //
688 FDODeviceExtension->FunctionDescriptorCount++;
689 }
690
691 //
692 // done
693 //
694 return Status;
695 }
696
697 NTSTATUS
698 USBCCGP_EnumWithUnionFunctionDescriptors(
699 IN PDEVICE_OBJECT DeviceObject)
700 {
701 UNIMPLEMENTED
702 return STATUS_NOT_IMPLEMENTED;
703 }
704
705 NTSTATUS
706 USBCCGP_EnumWithAudioLegacy(
707 IN PDEVICE_OBJECT DeviceObject)
708 {
709 ULONG Index;
710 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor, FirstDescriptor = NULL;
711 PFDO_DEVICE_EXTENSION FDODeviceExtension;
712 NTSTATUS Status = STATUS_SUCCESS;
713
714 //
715 // get device extension
716 //
717 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
718 ASSERT(FDODeviceExtension->Common.IsFDO);
719
720
721 //
722 // first check if all interfaces belong to the same audio class
723 //
724 for (Index = 0; Index < FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces; Index++)
725 {
726 //
727 // get interface descriptor
728 //
729 InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx(FDODeviceExtension->ConfigurationDescriptor, FDODeviceExtension->ConfigurationDescriptor, Index, 0, -1, -1, -1);
730 DPRINT1("Index %lu Descriptor %p\n", Index, InterfaceDescriptor);
731 ASSERT(InterfaceDescriptor);
732
733 if (InterfaceDescriptor->bInterfaceClass != 0x1)
734 {
735 //
736 // collection contains non audio class
737 //
738 return STATUS_UNSUCCESSFUL;
739 }
740
741 if (FirstDescriptor == NULL)
742 {
743 //
744 // store interface descriptor
745 //
746 FirstDescriptor = InterfaceDescriptor;
747 continue;
748 }
749
750 if (FirstDescriptor->bInterfaceSubClass == InterfaceDescriptor->bInterfaceSubClass)
751 {
752 //
753 // interface subclass must be different from the first interface
754 //
755 return STATUS_UNSUCCESSFUL;
756 }
757 }
758
759 //
760 // this is an composite audio device
761 //
762 DPRINT("[USBCCGP] Audio Composite Device detected\n");
763
764 //
765 // audio interfaces are all grouped into one single function
766 //
767 FDODeviceExtension->FunctionDescriptor = AllocateItem(NonPagedPool, sizeof(USBC_FUNCTION_DESCRIPTOR));
768 if (!FDODeviceExtension->FunctionDescriptor)
769 {
770 //
771 // no memory
772 //
773 DPRINT1("USBCCGP_EnumWithAssociationDescriptor failed to allocate function descriptor count\n");
774 return STATUS_INSUFFICIENT_RESOURCES;
775 }
776
777 //
778 // init function number
779 //
780 FDODeviceExtension->FunctionDescriptor[0].FunctionNumber = 0;
781
782 //
783 // store interfaces
784 //
785 Status = AllocateInterfaceDescriptorsArray(FDODeviceExtension->ConfigurationDescriptor, &FDODeviceExtension->FunctionDescriptor[0].InterfaceDescriptorList);
786 if (!NT_SUCCESS(Status))
787 {
788 //
789 // failed to allocate descriptor array
790 //
791 DPRINT1("[USBCCGP] Failed to allocate descriptor array %lx\n", Status);
792 return Status;
793 }
794
795 //
796 // now init the device ids
797 //
798 Status = USBCCG_InitIdsWithInterfaceDescriptor(FDODeviceExtension, FirstDescriptor, 0, &FDODeviceExtension->FunctionDescriptor[0]);
799 if (!NT_SUCCESS(Status))
800 {
801 //
802 // failed to init ids
803 //
804 DPRINT1("[USBCCGP] Failed to init ids with %lx\n", Status);
805 return Status;
806 }
807
808 //
809 // number of interfaces
810 //
811 FDODeviceExtension->FunctionDescriptor[0].NumberOfInterfaces = FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces;
812
813 //
814 // store function count
815 //
816 FDODeviceExtension->FunctionDescriptorCount = 1;
817
818 //
819 // done
820 //
821 return STATUS_SUCCESS;
822 }
823
824 NTSTATUS
825 USBCCGP_EnumerateFunctions(
826 IN PDEVICE_OBJECT DeviceObject)
827 {
828 NTSTATUS Status;
829 PFDO_DEVICE_EXTENSION FDODeviceExtension;
830
831 //
832 // get device extension
833 //
834 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
835 ASSERT(FDODeviceExtension->Common.IsFDO);
836
837 //
838 // first try with filter driver
839 //
840 Status = USBCCGP_CustomEnumWithInterface(DeviceObject);
841 if (NT_SUCCESS(Status))
842 {
843 //
844 // succeeded
845 //
846 return Status;
847 }
848
849 //
850 // enumerate functions with interface association descriptor
851 //
852 Status = USBCCGP_EnumWithAssociationDescriptor(DeviceObject);
853 if (NT_SUCCESS(Status))
854 {
855 //
856 // succeeded
857 //
858 return Status;
859 }
860
861 #if 0
862 //
863 // try with union function descriptors
864 //
865 Status = USBCCGP_EnumWithUnionFunctionDescriptors(DeviceObject);
866 if (NT_SUCCESS(Status))
867 {
868 //
869 // succeeded
870 //
871 return Status;
872 }
873 #endif
874
875 //
876 // try with legacy audio methods
877 //
878 Status = USBCCGP_EnumWithAudioLegacy(DeviceObject);
879 if (NT_SUCCESS(Status))
880 {
881 //
882 // succeeded
883 //
884 return Status;
885 }
886
887 //
888 // try with legacy enumeration
889 //
890 return USBCCGP_LegacyEnum(DeviceObject);
891 }