[USBCCGP]
[reactos.git] / reactos / 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 DPRINT("USBCCGP_CustomEnumWithInterface Status %x\n", Status);
135 if (!NT_SUCCESS(Status))
136 {
137 //
138 // failed
139 //
140 return Status;
141 }
142
143 DPRINT("FunctionDescriptorBufferLength %lu\n", FunctionDescriptorBufferLength);
144 DPRINT("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_GetStringDescriptor(FDODeviceExtension->NextDeviceObject,
369 100 * sizeof(WCHAR),
370 Descriptor->iFunction,
371 0x0409, //FIXME
372 (PVOID*)&DescriptionBuffer);
373 if (!NT_SUCCESS(Status))
374 {
375 //
376 // no description
377 //
378 RtlInitUnicodeString(&FunctionDescriptor->FunctionDescription, L"");
379 }
380 else
381 {
382 //
383 // init description
384 //
385 RtlInitUnicodeString(&FunctionDescriptor->FunctionDescription, DescriptionBuffer);
386 }
387 DPRINT1("FunctionDescription %wZ\n", &FunctionDescriptor->FunctionDescription);
388 }
389
390 //
391 // now init hardware id
392 //
393 Index = swprintf(Buffer, L"USB\\VID_%04x&PID_%04x&Rev_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
394 FDODeviceExtension->DeviceDescriptor->idProduct,
395 FDODeviceExtension->DeviceDescriptor->bcdDevice,
396 Descriptor->bFirstInterface) + 1;
397 Index = swprintf(&Buffer[Index], L"USB\\VID_%04x&PID_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
398 FDODeviceExtension->DeviceDescriptor->idProduct,
399 Descriptor->bFirstInterface) + 1;
400
401 // allocate result buffer
402 DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
403 if (!DescriptionBuffer)
404 {
405 //
406 // failed to allocate memory
407 //
408 return STATUS_INSUFFICIENT_RESOURCES;
409 }
410
411 // copy description
412 RtlCopyMemory(DescriptionBuffer, Buffer, Index * sizeof(WCHAR));
413 FunctionDescriptor->HardwareId.Buffer = DescriptionBuffer;
414 FunctionDescriptor->HardwareId.Length = Index * sizeof(WCHAR);
415 FunctionDescriptor->HardwareId.MaximumLength = (Index + 1) * sizeof(WCHAR);
416
417
418 //
419 // now init the compatible id
420 //
421 Index = swprintf(Buffer, L"USB\\Class_%02x&SubClass_%02x&Prot_%02x", Descriptor->bFunctionClass, Descriptor->bFunctionSubClass, Descriptor->bFunctionProtocol) + 1;
422 Index = swprintf(&Buffer[Index], L"USB\\Class_%04x&SubClass_%04x", Descriptor->bFunctionClass, Descriptor->bFunctionSubClass) + 1;
423 Index = swprintf(&Buffer[Index], L"USB\\Class_%04x", Descriptor->bFunctionClass) + 1;
424
425 // allocate result buffer
426 DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
427 if (!DescriptionBuffer)
428 {
429 //
430 // failed to allocate memory
431 //
432 return STATUS_INSUFFICIENT_RESOURCES;
433 }
434
435 // copy description
436 RtlCopyMemory(DescriptionBuffer, Buffer, Index * sizeof(WCHAR));
437 FunctionDescriptor->CompatibleId.Buffer = DescriptionBuffer;
438 FunctionDescriptor->CompatibleId.Length = Index * sizeof(WCHAR);
439 FunctionDescriptor->CompatibleId.MaximumLength = (Index + 1) * sizeof(WCHAR);
440
441 //
442 // done
443 //
444 return STATUS_SUCCESS;
445 }
446
447 NTSTATUS
448 USBCCGP_EnumWithAssociationDescriptor(
449 IN PDEVICE_OBJECT DeviceObject)
450 {
451 ULONG DescriptorCount, Index;
452 PFDO_DEVICE_EXTENSION FDODeviceExtension;
453 NTSTATUS Status = STATUS_SUCCESS;
454
455 //
456 // get device extension
457 //
458 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
459 ASSERT(FDODeviceExtension->Common.IsFDO);
460
461 //
462 // count association descriptors
463 //
464 DescriptorCount = USBCCGP_CountAssociationDescriptors(FDODeviceExtension->ConfigurationDescriptor);
465 if (!DescriptorCount)
466 {
467 //
468 // no descriptors found
469 //
470 return STATUS_NOT_SUPPORTED;
471 }
472
473 //
474 // allocate function descriptor array
475 //
476 FDODeviceExtension->FunctionDescriptor = AllocateItem(NonPagedPool, sizeof(USBC_FUNCTION_DESCRIPTOR) * DescriptorCount);
477 if (!FDODeviceExtension->FunctionDescriptorCount)
478 {
479 //
480 // no memory
481 //
482 DPRINT1("USBCCGP_EnumWithAssociationDescriptor failed to allocate function descriptor count %x\n", DescriptorCount);
483 return STATUS_INSUFFICIENT_RESOURCES;
484 }
485
486 for(Index = 0; Index < DescriptorCount; Index++)
487 {
488 //
489 // init function descriptors
490 //
491 Status = USBCCGP_InitFunctionDescriptor(FDODeviceExtension, Index, &FDODeviceExtension->FunctionDescriptor[Index]);
492 if (!NT_SUCCESS(Status))
493 {
494 //
495 // failed
496 //
497 return Status;
498 }
499 }
500
501 //
502 // store function descriptor count
503 //
504 FDODeviceExtension->FunctionDescriptorCount = DescriptorCount;
505
506 //
507 // done
508 //
509 return Status;
510 }
511
512 NTSTATUS
513 USBCCG_InitIdsWithInterfaceDescriptor(
514 IN PFDO_DEVICE_EXTENSION FDODeviceExtension,
515 IN PUSB_INTERFACE_DESCRIPTOR Descriptor,
516 IN ULONG FunctionIndex,
517 OUT PUSBC_FUNCTION_DESCRIPTOR FunctionDescriptor)
518 {
519 ULONG Index;
520 WCHAR Buffer[200];
521 LPWSTR DescriptionBuffer;
522 NTSTATUS Status;
523
524 //
525 // now init interface description
526 //
527 if (Descriptor->iInterface)
528 {
529 //
530 // get interface description
531 //
532 Status = USBCCGP_GetStringDescriptor(FDODeviceExtension->NextDeviceObject,
533 100 * sizeof(WCHAR),
534 Descriptor->iInterface,
535 0x0409, //FIXME
536 (PVOID*)&DescriptionBuffer);
537 if (!NT_SUCCESS(Status))
538 {
539 //
540 // no description
541 //
542 RtlInitUnicodeString(&FunctionDescriptor->FunctionDescription, L"");
543 }
544 else
545 {
546 //
547 // init description
548 //
549 RtlInitUnicodeString(&FunctionDescriptor->FunctionDescription, DescriptionBuffer);
550 }
551 DPRINT1("FunctionDescription %wZ\n", &FunctionDescriptor->FunctionDescription);
552 }
553
554
555 //
556 // now init hardware id
557 //
558 Index = swprintf(Buffer, L"USB\\VID_%04x&PID_%04x&Rev_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
559 FDODeviceExtension->DeviceDescriptor->idProduct,
560 FDODeviceExtension->DeviceDescriptor->bcdDevice,
561 FunctionIndex) + 1;
562 Index += swprintf(&Buffer[Index], L"USB\\VID_%04x&PID_%04x&MI_%02x", FDODeviceExtension->DeviceDescriptor->idVendor,
563 FDODeviceExtension->DeviceDescriptor->idProduct,
564 FunctionIndex) + 1;
565
566 // allocate result buffer
567 DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
568 if (!DescriptionBuffer)
569 {
570 //
571 // failed to allocate memory
572 //
573 return STATUS_INSUFFICIENT_RESOURCES;
574 }
575
576 // copy description
577 RtlCopyMemory(DescriptionBuffer, Buffer, Index * sizeof(WCHAR));
578 FunctionDescriptor->HardwareId.Buffer = DescriptionBuffer;
579 FunctionDescriptor->HardwareId.Length = Index * sizeof(WCHAR);
580 FunctionDescriptor->HardwareId.MaximumLength = (Index + 1) * sizeof(WCHAR);
581
582 //
583 // now init the compatible id
584 //
585 Index = swprintf(Buffer, L"USB\\Class_%02x&SubClass_%02x&Prot_%02x", Descriptor->bInterfaceClass, Descriptor->bInterfaceSubClass, Descriptor->bInterfaceProtocol) + 1;
586 Index += swprintf(&Buffer[Index], L"USB\\Class_%02x&SubClass_%02x", Descriptor->bInterfaceClass, Descriptor->bInterfaceSubClass) + 1;
587 Index += swprintf(&Buffer[Index], L"USB\\Class_%02x", Descriptor->bInterfaceClass) + 1;
588
589 // allocate result buffer
590 DescriptionBuffer = AllocateItem(NonPagedPool, (Index + 1) * sizeof(WCHAR));
591 if (!DescriptionBuffer)
592 {
593 //
594 // failed to allocate memory
595 //
596 return STATUS_INSUFFICIENT_RESOURCES;
597 }
598
599 // copy description
600 RtlCopyMemory(DescriptionBuffer, Buffer, Index * sizeof(WCHAR));
601 FunctionDescriptor->CompatibleId.Buffer = DescriptionBuffer;
602 FunctionDescriptor->CompatibleId.Length = Index * sizeof(WCHAR);
603 FunctionDescriptor->CompatibleId.MaximumLength = (Index + 1) * sizeof(WCHAR);
604
605 //
606 // done
607 //
608 return STATUS_SUCCESS;
609 }
610
611
612 NTSTATUS
613 USBCCGP_LegacyEnum(
614 IN PDEVICE_OBJECT DeviceObject)
615 {
616 ULONG Index;
617 PFDO_DEVICE_EXTENSION FDODeviceExtension;
618 NTSTATUS Status = STATUS_SUCCESS;
619 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
620
621 //
622 // get device extension
623 //
624 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
625 ASSERT(FDODeviceExtension->Common.IsFDO);
626
627 //
628 // sanity check
629 //
630 ASSERT(FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces);
631
632 //
633 // allocate function array
634 //
635 FDODeviceExtension->FunctionDescriptor = AllocateItem(NonPagedPool, sizeof(USBC_FUNCTION_DESCRIPTOR) * FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces);
636 if (!FDODeviceExtension->FunctionDescriptor)
637 {
638 //
639 // no memory
640 //
641 DPRINT1("USBCCGP_EnumWithAssociationDescriptor failed to allocate function descriptor %lu\n", FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces);
642 return STATUS_INSUFFICIENT_RESOURCES;
643 }
644
645 //
646 // init function descriptors
647 //
648 for(Index = 0; Index < FDODeviceExtension->ConfigurationDescriptor->bNumInterfaces; Index++)
649 {
650 // get interface descriptor
651 InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx(FDODeviceExtension->ConfigurationDescriptor, FDODeviceExtension->ConfigurationDescriptor, Index, 0, -1, -1, -1);
652 if (InterfaceDescriptor == NULL)
653 {
654 //
655 // failed to find interface descriptor
656 //
657 DPRINT1("[USBCCGP] Failed to find interface descriptor index %lu\n", Index);
658 ASSERT(FALSE);
659 return STATUS_UNSUCCESSFUL;
660 }
661
662 //
663 // init function descriptor
664 //
665 FDODeviceExtension->FunctionDescriptor[Index].FunctionNumber = Index;
666 FDODeviceExtension->FunctionDescriptor[Index].NumberOfInterfaces = 1;
667 FDODeviceExtension->FunctionDescriptor[Index].InterfaceDescriptorList = AllocateItem(NonPagedPool, sizeof(PUSB_INTERFACE_DESCRIPTOR) * 1);
668 if (!FDODeviceExtension->FunctionDescriptor[Index].InterfaceDescriptorList)
669 {
670 //
671 // no memory
672 //
673 return STATUS_INSUFFICIENT_RESOURCES;
674 }
675
676 //
677 // store interface descriptor
678 //
679 FDODeviceExtension->FunctionDescriptor[Index].InterfaceDescriptorList[0] = InterfaceDescriptor;
680
681 //
682 // now init the device ids
683 //
684 Status = USBCCG_InitIdsWithInterfaceDescriptor(FDODeviceExtension, InterfaceDescriptor, Index, &FDODeviceExtension->FunctionDescriptor[Index]);
685 if (!NT_SUCCESS(Status))
686 {
687 //
688 // failed to init ids
689 //
690 DPRINT1("[USBCCGP] Failed to init ids with %x\n", Status);
691 return Status;
692 }
693
694 //
695 // store function count
696 //
697 FDODeviceExtension->FunctionDescriptorCount++;
698 }
699
700 //
701 // done
702 //
703 return Status;
704 }
705
706 NTSTATUS
707 USBCCGP_EnumWithUnionFunctionDescriptors(
708 IN PDEVICE_OBJECT DeviceObject)
709 {
710 UNIMPLEMENTED
711 return STATUS_NOT_IMPLEMENTED;
712 }
713
714 NTSTATUS
715 USBCCGP_EnumWithAudioLegacy(
716 IN PDEVICE_OBJECT DeviceObject)
717 {
718 ULONG Index;
719 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor, FirstDescriptor = NULL;
720 PFDO_DEVICE_EXTENSION FDODeviceExtension;
721 NTSTATUS Status = STATUS_SUCCESS;
722 PVOID StartPosition;
723
724 //
725 // get device extension
726 //
727 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
728 ASSERT(FDODeviceExtension->Common.IsFDO);
729
730
731 //
732 // first check if all interfaces belong to the same audio class
733 //
734 StartPosition = FDODeviceExtension->ConfigurationDescriptor;
735 for(Index = 0; Index < CountInterfaceDescriptors(FDODeviceExtension->ConfigurationDescriptor); Index++)
736 {
737 //
738 // get interface descriptor
739 //
740 InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx(FDODeviceExtension->ConfigurationDescriptor, StartPosition, -1, -1, -1, -1, -1);
741 DPRINT1("Index %lu Descriptor %p\n", Index, InterfaceDescriptor);
742 ASSERT(InterfaceDescriptor);
743
744 //
745 // move to next descriptor
746 //
747 StartPosition = (PVOID)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
748
749 if (InterfaceDescriptor->bInterfaceClass != 0x1)
750 {
751 //
752 // collection contains non audio class
753 //
754 return STATUS_UNSUCCESSFUL;
755 }
756
757 if (FirstDescriptor == NULL)
758 {
759 //
760 // store interface descriptor
761 //
762 FirstDescriptor = InterfaceDescriptor;
763 continue;
764 }
765
766 if (FirstDescriptor->bInterfaceSubClass == InterfaceDescriptor->bInterfaceSubClass)
767 {
768 //
769 // interface subclass must be different from the first interface
770 //
771 return STATUS_UNSUCCESSFUL;
772 }
773 }
774
775 //
776 // this is an composite audio device
777 //
778 DPRINT("[USBCCGP] Audio Composite Device detected\n");
779
780 //
781 // audio interfaces are all grouped into one single function
782 //
783 FDODeviceExtension->FunctionDescriptor = AllocateItem(NonPagedPool, sizeof(USBC_FUNCTION_DESCRIPTOR));
784 if (!FDODeviceExtension->FunctionDescriptor)
785 {
786 //
787 // no memory
788 //
789 DPRINT1("USBCCGP_EnumWithAssociationDescriptor failed to allocate function descriptor count\n");
790 return STATUS_INSUFFICIENT_RESOURCES;
791 }
792
793 //
794 // init function number
795 //
796 FDODeviceExtension->FunctionDescriptor[0].FunctionNumber = 0;
797
798 //
799 // store interfaces
800 //
801 Status = AllocateInterfaceDescriptorsArray(FDODeviceExtension->ConfigurationDescriptor, &FDODeviceExtension->FunctionDescriptor[0].InterfaceDescriptorList);
802 if (!NT_SUCCESS(Status))
803 {
804 //
805 // failed to allocate descriptor array
806 //
807 DPRINT1("[USBCCGP] Failed to allocate descriptor array %x\n", Status);
808 return Status;
809 }
810
811 //
812 // now init the device ids
813 //
814 Status = USBCCG_InitIdsWithInterfaceDescriptor(FDODeviceExtension, FirstDescriptor, 0, &FDODeviceExtension->FunctionDescriptor[0]);
815 if (!NT_SUCCESS(Status))
816 {
817 //
818 // failed to init ids
819 //
820 DPRINT1("[USBCCGP] Failed to init ids with %x\n", Status);
821 return Status;
822 }
823
824 //
825 // number of interfaces
826 //
827 FDODeviceExtension->FunctionDescriptor[0].NumberOfInterfaces = CountInterfaceDescriptors(FDODeviceExtension->ConfigurationDescriptor);
828
829 //
830 // store function count
831 //
832 FDODeviceExtension->FunctionDescriptorCount = 1;
833
834 //
835 // done
836 //
837 return STATUS_SUCCESS;
838 }
839
840 NTSTATUS
841 USBCCGP_EnumerateFunctions(
842 IN PDEVICE_OBJECT DeviceObject)
843 {
844 NTSTATUS Status;
845 PFDO_DEVICE_EXTENSION FDODeviceExtension;
846
847 //
848 // get device extension
849 //
850 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
851 ASSERT(FDODeviceExtension->Common.IsFDO);
852
853 //
854 // first try with filter driver
855 //
856 Status = USBCCGP_CustomEnumWithInterface(DeviceObject);
857 if (NT_SUCCESS(Status))
858 {
859 //
860 // succeeded
861 //
862 return Status;
863 }
864
865 //
866 // enumerate functions with interface association descriptor
867 //
868 Status = USBCCGP_EnumWithAssociationDescriptor(DeviceObject);
869 if (NT_SUCCESS(Status))
870 {
871 //
872 // succeeded
873 //
874 return Status;
875 }
876
877 #if 0
878 //
879 // try with union function descriptors
880 //
881 Status = USBCCGP_EnumWithUnionFunctionDescriptors(DeviceObject);
882 if (NT_SUCCESS(Status))
883 {
884 //
885 // succeeded
886 //
887 return Status;
888 }
889 #endif
890
891 //
892 // try with legacy audio methods
893 //
894 Status = USBCCGP_EnumWithAudioLegacy(DeviceObject);
895 if (NT_SUCCESS(Status))
896 {
897 //
898 // succeeded
899 //
900 return Status;
901 }
902
903 //
904 // try with legacy enumeration
905 //
906 return USBCCGP_LegacyEnum(DeviceObject);
907 }