[USBCCGP]
[reactos.git] / reactos / drivers / usb / usbccgp / pdo.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/pdo.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_PdoHandleQueryDeviceText(
16 IN PDEVICE_OBJECT DeviceObject,
17 IN OUT PIRP Irp)
18 {
19 LPWSTR Buffer;
20 PPDO_DEVICE_EXTENSION PDODeviceExtension;
21 LPWSTR GenericString = L"Composite USB Device";
22 //
23 // get device extension
24 //
25 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
26
27 //
28 // is there a device description
29 //
30 if (PDODeviceExtension->FunctionDescriptor->FunctionDescription.Length)
31 {
32 //
33 // allocate buffer
34 //
35 Buffer = AllocateItem(NonPagedPool, PDODeviceExtension->FunctionDescriptor->FunctionDescription.Length + sizeof(WCHAR));
36 if (!Buffer)
37 {
38 //
39 // no memory
40 //
41 return STATUS_INSUFFICIENT_RESOURCES;
42 }
43
44 //
45 // copy buffer
46 //
47 Irp->IoStatus.Information = (ULONG_PTR)Buffer;
48 RtlCopyMemory(Buffer, PDODeviceExtension->FunctionDescriptor->FunctionDescription.Buffer, PDODeviceExtension->FunctionDescriptor->FunctionDescription.Length);
49 return STATUS_SUCCESS;
50 }
51
52 //
53 // FIXME use GenericCompositeUSBDeviceString
54 //
55 UNIMPLEMENTED
56 Buffer = AllocateItem(PagedPool, (wcslen(GenericString) + 1) * sizeof(WCHAR));
57 if (!Buffer)
58 {
59 //
60 // no memory
61 //
62 return STATUS_INSUFFICIENT_RESOURCES;
63 }
64 RtlCopyMemory(Buffer, GenericString, (wcslen(GenericString) + 1) * sizeof(WCHAR));
65 Irp->IoStatus.Information = (ULONG_PTR)Buffer;
66
67 return STATUS_SUCCESS;
68 }
69
70 NTSTATUS
71 USBCCGP_PdoHandleDeviceRelations(
72 IN PDEVICE_OBJECT DeviceObject,
73 IN OUT PIRP Irp)
74 {
75 PDEVICE_RELATIONS DeviceRelations;
76 PIO_STACK_LOCATION IoStack;
77
78 DPRINT("USBCCGP_PdoHandleDeviceRelations\n");
79
80 //
81 // get current irp stack location
82 //
83 IoStack = IoGetCurrentIrpStackLocation(Irp);
84
85 //
86 // check if relation type is BusRelations
87 //
88 if (IoStack->Parameters.QueryDeviceRelations.Type != TargetDeviceRelation)
89 {
90 //
91 // PDO handles only target device relation
92 //
93 return Irp->IoStatus.Status;
94 }
95
96 //
97 // allocate device relations
98 //
99 DeviceRelations = (PDEVICE_RELATIONS)AllocateItem(PagedPool, sizeof(DEVICE_RELATIONS));
100 if (!DeviceRelations)
101 {
102 //
103 // no memory
104 //
105 return STATUS_INSUFFICIENT_RESOURCES;
106 }
107
108 //
109 // initialize device relations
110 //
111 DeviceRelations->Count = 1;
112 DeviceRelations->Objects[0] = DeviceObject;
113 ObReferenceObject(DeviceObject);
114
115 //
116 // store result
117 //
118 Irp->IoStatus.Information = (ULONG_PTR)DeviceRelations;
119
120 //
121 // completed successfully
122 //
123 return STATUS_SUCCESS;
124 }
125
126 NTSTATUS
127 USBCCGP_PdoAppendInterfaceNumber(
128 IN LPWSTR DeviceId,
129 IN ULONG InterfaceNumber,
130 OUT LPWSTR *OutString)
131 {
132 ULONG Length = 0, StringLength;
133 LPWSTR String;
134
135 //
136 // count length of string
137 //
138 String = DeviceId;
139 while(*String)
140 {
141 StringLength = wcslen(String) + 1;
142 Length += StringLength;
143 Length += 6; //&MI_XX
144 String += StringLength;
145 }
146
147 //
148 // now allocate the buffer
149 //
150 String = AllocateItem(NonPagedPool, (Length + 2) * sizeof(WCHAR));
151 if (!String)
152 {
153 //
154 // no memory
155 //
156 return STATUS_INSUFFICIENT_RESOURCES;
157 }
158
159 //
160 // store result
161 //
162 *OutString = String;
163
164 while(*DeviceId)
165 {
166 StringLength = swprintf(String, L"%s&MI_%02x", DeviceId) + 1;
167 Length = wcslen(DeviceId) + 1;
168 DPRINT("String %p\n", String);
169
170 //
171 // next string
172 //
173 String += StringLength;
174 DeviceId += Length;
175 }
176
177 //
178 // success
179 //
180 return STATUS_SUCCESS;
181 }
182
183
184 NTSTATUS
185 USBCCGP_PdoHandleQueryId(
186 PDEVICE_OBJECT DeviceObject,
187 PIRP Irp)
188 {
189 PIO_STACK_LOCATION IoStack;
190 PUNICODE_STRING DeviceString = NULL;
191 PPDO_DEVICE_EXTENSION PDODeviceExtension;
192 NTSTATUS Status;
193 LPWSTR Buffer;
194
195 //
196 // get current irp stack location
197 //
198 IoStack = IoGetCurrentIrpStackLocation(Irp);
199
200 //
201 // get device extension
202 //
203 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
204
205
206 if (IoStack->Parameters.QueryId.IdType == BusQueryDeviceID)
207 {
208 //
209 // handle query device id
210 //
211 Status = USBCCGP_SyncForwardIrp(PDODeviceExtension->NextDeviceObject, Irp);
212 if (NT_SUCCESS(Status))
213 {
214 //
215 // allocate buffer
216 //
217 Buffer = AllocateItem(NonPagedPool, (wcslen((LPWSTR)Irp->IoStatus.Information) + 7) * sizeof(WCHAR));
218 if (Buffer)
219 {
220 //
221 // append interface number
222 //
223 ASSERT(Irp->IoStatus.Information);
224 swprintf(Buffer, L"%s&MI_%02x", (LPWSTR)Irp->IoStatus.Information, PDODeviceExtension->FunctionDescriptor->FunctionNumber);
225 DPRINT("BusQueryDeviceID %S\n", Buffer);
226
227 ExFreePool((PVOID)Irp->IoStatus.Information);
228 Irp->IoStatus .Information = (ULONG_PTR)Buffer;
229 }
230 else
231 {
232 //
233 // no memory
234 //
235 Status = STATUS_INSUFFICIENT_RESOURCES;
236 }
237 }
238 return Status;
239 }
240 else if (IoStack->Parameters.QueryId.IdType == BusQueryHardwareIDs)
241 {
242 //
243 // handle instance id
244 //
245 DeviceString = &PDODeviceExtension->FunctionDescriptor->HardwareId;
246 }
247 else if (IoStack->Parameters.QueryId.IdType == BusQueryInstanceID)
248 {
249 //
250 // handle instance id
251 //
252 Buffer = AllocateItem(NonPagedPool, 5 * sizeof(WCHAR));
253 if (!Buffer)
254 {
255 //
256 // no memory
257 //
258 return STATUS_INSUFFICIENT_RESOURCES;
259 }
260
261 //
262 // use function number
263 //
264 swprintf(Buffer, L"%04x", PDODeviceExtension->FunctionDescriptor->FunctionNumber);
265 Irp->IoStatus.Information = (ULONG_PTR)Buffer;
266 return STATUS_SUCCESS;
267 }
268 else if (IoStack->Parameters.QueryId.IdType == BusQueryCompatibleIDs)
269 {
270 //
271 // handle instance id
272 //
273 DeviceString = &PDODeviceExtension->FunctionDescriptor->CompatibleId;
274 }
275
276 //
277 // sanity check
278 //
279 ASSERT(DeviceString != NULL);
280
281 //
282 // allocate buffer
283 //
284 Buffer = AllocateItem(NonPagedPool, DeviceString->Length + sizeof(WCHAR));
285 if (!Buffer)
286 {
287 //
288 // no memory
289 //
290 return STATUS_INSUFFICIENT_RESOURCES;
291 }
292
293 //
294 // copy buffer
295 //
296 RtlCopyMemory(Buffer, DeviceString->Buffer, DeviceString->Length);
297 Irp->IoStatus.Information = (ULONG_PTR)Buffer;
298
299 return STATUS_SUCCESS;
300 }
301
302 NTSTATUS
303 PDO_HandlePnp(
304 PDEVICE_OBJECT DeviceObject,
305 PIRP Irp)
306 {
307 PIO_STACK_LOCATION IoStack;
308 PPDO_DEVICE_EXTENSION PDODeviceExtension;
309 NTSTATUS Status;
310 ULONG Index, bFound;
311
312 //
313 // get current stack location
314 //
315 IoStack = IoGetCurrentIrpStackLocation(Irp);
316
317 //
318 // get device extension
319 //
320 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
321
322 //
323 // sanity check
324 //
325 ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
326
327 switch(IoStack->MinorFunction)
328 {
329 case IRP_MN_QUERY_DEVICE_RELATIONS:
330 {
331 //
332 // handle device relations
333 //
334 Status = USBCCGP_PdoHandleDeviceRelations(DeviceObject, Irp);
335 break;
336 }
337 case IRP_MN_QUERY_DEVICE_TEXT:
338 {
339 //
340 // handle query device text
341 //
342 Status = USBCCGP_PdoHandleQueryDeviceText(DeviceObject, Irp);
343 break;
344 }
345 case IRP_MN_QUERY_ID:
346 {
347 //
348 // handle request
349 //
350 Status = USBCCGP_PdoHandleQueryId(DeviceObject, Irp);
351 break;
352 }
353 case IRP_MN_REMOVE_DEVICE:
354 {
355 //
356 // remove us from the fdo's pdo list
357 //
358 bFound = FALSE;
359 for(Index = 0; Index < PDODeviceExtension->FDODeviceExtension->FunctionDescriptorCount; Index++)
360 {
361 if (PDODeviceExtension->FDODeviceExtension->ChildPDO[Index] == DeviceObject)
362 {
363 //
364 // remove us
365 //
366 PDODeviceExtension->FDODeviceExtension->ChildPDO[Index] = NULL;
367 bFound = TRUE;
368 break;
369 }
370 }
371
372 //
373 // Complete the IRP
374 //
375 Irp->IoStatus.Status = STATUS_SUCCESS;
376 IoCompleteRequest(Irp, IO_NO_INCREMENT);
377
378 if (bFound)
379 {
380 //
381 // Delete the device object
382 //
383 IoDeleteDevice(DeviceObject);
384 }
385 return STATUS_SUCCESS;
386 }
387 case IRP_MN_QUERY_CAPABILITIES:
388 {
389 //
390 // copy device capabilities
391 //
392 RtlCopyMemory(IoStack->Parameters.DeviceCapabilities.Capabilities, &PDODeviceExtension->Capabilities, sizeof(DEVICE_CAPABILITIES));
393
394 /* Complete the IRP */
395 Irp->IoStatus.Status = STATUS_SUCCESS;
396 IoCompleteRequest(Irp, IO_NO_INCREMENT);
397 return STATUS_SUCCESS;
398 }
399 case IRP_MN_QUERY_REMOVE_DEVICE:
400 case IRP_MN_QUERY_STOP_DEVICE:
401 {
402 //
403 // sure
404 //
405 Status = STATUS_SUCCESS;
406 break;
407 }
408 case IRP_MN_START_DEVICE:
409 {
410 //
411 // no-op for PDO
412 //
413 DPRINT("[USBCCGP] PDO IRP_MN_START\n");
414 Status = STATUS_SUCCESS;
415 break;
416 }
417 default:
418 {
419 //
420 // do nothing
421 //
422 Status = Irp->IoStatus.Status;
423 break;
424 }
425 }
426
427 //
428 // complete request
429 //
430 if (Status != STATUS_PENDING)
431 {
432 //
433 // store result
434 //
435 Irp->IoStatus.Status = Status;
436
437 //
438 // complete request
439 //
440 IoCompleteRequest(Irp, IO_NO_INCREMENT);
441 }
442
443 //
444 // done processing
445 //
446 return Status;
447
448 }
449
450 NTSTATUS
451 USBCCGP_BuildConfigurationDescriptor(
452 PDEVICE_OBJECT DeviceObject,
453 PIRP Irp)
454 {
455 PIO_STACK_LOCATION IoStack;
456 PPDO_DEVICE_EXTENSION PDODeviceExtension;
457 PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor;
458 PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
459 ULONG TotalSize, Index;
460 PURB Urb;
461 PVOID Buffer;
462 PUCHAR BufferPtr;
463
464 //
465 // get current stack location
466 //
467 IoStack = IoGetCurrentIrpStackLocation(Irp);
468
469 DPRINT("USBCCGP_BuildConfigurationDescriptor\n");
470
471 //
472 // get device extension
473 //
474 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
475
476 //
477 // get configuration descriptor
478 //
479 ConfigurationDescriptor = PDODeviceExtension->ConfigurationDescriptor;
480
481 //
482 // calculate size of configuration descriptor
483 //
484 TotalSize = sizeof(USB_CONFIGURATION_DESCRIPTOR);
485
486 for(Index = 0; Index < PDODeviceExtension->FunctionDescriptor->NumberOfInterfaces; Index++)
487 {
488 //
489 // get current interface descriptor
490 //
491 InterfaceDescriptor = PDODeviceExtension->FunctionDescriptor->InterfaceDescriptorList[Index];
492
493 //
494 // add to size and move to next descriptor
495 //
496 TotalSize += InterfaceDescriptor->bLength;
497 InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
498
499 do
500 {
501 if ((ULONG_PTR)InterfaceDescriptor >= ((ULONG_PTR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength))
502 {
503 //
504 // reached end of configuration descriptor
505 //
506 break;
507 }
508
509 //
510 // association descriptors are removed
511 //
512 if (InterfaceDescriptor->bDescriptorType != USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)
513 {
514 if (InterfaceDescriptor->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE)
515 {
516 //
517 // reached next descriptor
518 //
519 break;
520 }
521
522 //
523 // append size
524 //
525 TotalSize += InterfaceDescriptor->bLength;
526 }
527
528 //
529 // move to next descriptor
530 //
531 InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
532 }while(TRUE);
533 }
534
535 //
536 // now allocate temporary buffer for the configuration descriptor
537 //
538 Buffer = AllocateItem(NonPagedPool, TotalSize);
539 if (!Buffer)
540 {
541 //
542 // failed to allocate buffer
543 //
544 DPRINT1("[USBCCGP] Failed to allocate %lu Bytes\n", TotalSize);
545 return STATUS_INSUFFICIENT_RESOURCES;
546 }
547
548 //
549 // first copy the configuration descriptor
550 //
551 RtlCopyMemory(Buffer, ConfigurationDescriptor, sizeof(USB_CONFIGURATION_DESCRIPTOR));
552 BufferPtr = (PUCHAR)((ULONG_PTR)Buffer + ConfigurationDescriptor->bLength);
553
554 for(Index = 0; Index < PDODeviceExtension->FunctionDescriptor->NumberOfInterfaces; Index++)
555 {
556 //
557 // get current interface descriptor
558 //
559 InterfaceDescriptor = PDODeviceExtension->FunctionDescriptor->InterfaceDescriptorList[Index];
560
561 //
562 // copy descriptor and move to next descriptor
563 //
564 RtlCopyMemory(BufferPtr, InterfaceDescriptor, InterfaceDescriptor->bLength);
565 BufferPtr += InterfaceDescriptor->bLength;
566 InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
567
568 do
569 {
570 if ((ULONG_PTR)InterfaceDescriptor >= ((ULONG_PTR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength))
571 {
572 //
573 // reached end of configuration descriptor
574 //
575 break;
576 }
577
578 //
579 // association descriptors are removed
580 //
581 if (InterfaceDescriptor->bDescriptorType != USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)
582 {
583 if (InterfaceDescriptor->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE)
584 {
585 //
586 // reached next descriptor
587 //
588 break;
589 }
590
591 //
592 // copy descriptor
593 //
594 RtlCopyMemory(BufferPtr, InterfaceDescriptor, InterfaceDescriptor->bLength);
595 BufferPtr += InterfaceDescriptor->bLength;
596 }
597
598 //
599 // move to next descriptor
600 //
601 InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
602 }while(TRUE);
603 }
604
605 //
606 // modify configuration descriptor
607 //
608 ConfigurationDescriptor = Buffer;
609 ConfigurationDescriptor->wTotalLength = TotalSize;
610 ConfigurationDescriptor->bNumInterfaces = PDODeviceExtension->FunctionDescriptor->NumberOfInterfaces;
611
612 //
613 // get urb
614 //
615 Urb = (PURB)IoStack->Parameters.Others.Argument1;
616 ASSERT(Urb);
617
618 //
619 // copy descriptor
620 //
621 RtlCopyMemory(Urb->UrbControlDescriptorRequest.TransferBuffer, Buffer, min(TotalSize, Urb->UrbControlDescriptorRequest.TransferBufferLength));
622
623 //
624 // store final size
625 //
626 Urb->UrbControlDescriptorRequest.TransferBufferLength = TotalSize;
627
628 //
629 // free buffer
630 //
631 FreeItem(Buffer);
632
633 //
634 // done
635 //
636 return STATUS_SUCCESS;
637 }
638
639 NTSTATUS
640 USBCCGP_PDOSelectConfiguration(
641 PDEVICE_OBJECT DeviceObject,
642 PIRP Irp)
643 {
644 PIO_STACK_LOCATION IoStack;
645 PPDO_DEVICE_EXTENSION PDODeviceExtension;
646 PURB Urb, NewUrb;
647 PUSBD_INTERFACE_INFORMATION InterfaceInformation;
648 ULONG InterfaceInformationCount, Index, InterfaceIndex;
649 PUSBD_INTERFACE_LIST_ENTRY Entry;
650 ULONG NeedSelect, FoundInterface;
651 NTSTATUS Status;
652
653 //
654 // get current stack location
655 //
656 IoStack = IoGetCurrentIrpStackLocation(Irp);
657
658 //
659 // get device extension
660 //
661 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
662
663 //
664 // get urb
665 //
666 Urb = (PURB)IoStack->Parameters.Others.Argument1;
667 ASSERT(Urb);
668
669 //
670 // is there already an configuration handle
671 //
672 if (Urb->UrbSelectConfiguration.ConfigurationHandle)
673 {
674 //
675 // nothing to do
676 //
677 return STATUS_SUCCESS;
678 }
679
680 //
681 // count interface information
682 //
683 InterfaceInformationCount = 0;
684 InterfaceInformation = &Urb->UrbSelectConfiguration.Interface;
685 do
686 {
687 InterfaceInformationCount++;
688 InterfaceInformation = (PUSBD_INTERFACE_INFORMATION)((ULONG_PTR)InterfaceInformation + InterfaceInformation->Length);
689 }while((ULONG_PTR)InterfaceInformation < (ULONG_PTR)Urb + Urb->UrbSelectConfiguration.Hdr.Length);
690
691 //
692 // check all interfaces
693 //
694 InterfaceInformation = &Urb->UrbSelectConfiguration.Interface;
695 Index = 0;
696 Entry = NULL;
697 DPRINT("Count %x\n", InterfaceInformationCount);
698 do
699 {
700 DPRINT1("[USBCCGP] SelectConfiguration Function %x InterfaceNumber %x Alternative %x\n", PDODeviceExtension->FunctionDescriptor->FunctionNumber, InterfaceInformation->InterfaceNumber, InterfaceInformation->AlternateSetting);
701
702 //
703 // search for the interface in the local interface list
704 //
705 FoundInterface = FALSE;
706 for(InterfaceIndex = 0; InterfaceIndex < PDODeviceExtension->FunctionDescriptor->NumberOfInterfaces; InterfaceIndex++)
707 {
708 if (PDODeviceExtension->FunctionDescriptor->InterfaceDescriptorList[InterfaceIndex]->bInterfaceNumber == InterfaceInformation->InterfaceNumber)
709 {
710 // found interface entry
711 FoundInterface = TRUE;
712 break;
713 }
714 }
715
716 if (!FoundInterface)
717 {
718 //
719 // invalid parameter
720 //
721 DPRINT1("InterfaceInformation InterfaceNumber %x Alternative %x NumberOfPipes %x not found\n", InterfaceInformation->InterfaceNumber, InterfaceInformation->AlternateSetting, InterfaceInformation->NumberOfPipes);
722 ASSERT(FALSE);
723 return STATUS_INVALID_PARAMETER;
724 }
725
726 //
727 // now query the total interface list
728 //
729 Entry = NULL;
730 for(InterfaceIndex = 0; InterfaceIndex < PDODeviceExtension->InterfaceListCount; InterfaceIndex++)
731 {
732 if (PDODeviceExtension->InterfaceList[InterfaceIndex].Interface->InterfaceNumber == InterfaceInformation->InterfaceNumber)
733 {
734 //
735 // found entry
736 //
737 Entry = &PDODeviceExtension->InterfaceList[InterfaceIndex];
738 }
739 }
740
741 //
742 // sanity check
743 //
744 ASSERT(Entry);
745 if (!Entry)
746 {
747 //
748 // corruption detected
749 //
750 KeBugCheck(0);
751 }
752
753 NeedSelect = FALSE;
754 if (Entry->InterfaceDescriptor->bAlternateSetting == InterfaceInformation->AlternateSetting)
755 {
756
757 for(InterfaceIndex = 0; InterfaceIndex < InterfaceInformation->NumberOfPipes; InterfaceIndex++)
758 {
759 if (InterfaceInformation->Pipes[InterfaceIndex].MaximumTransferSize != Entry->Interface->Pipes[InterfaceIndex].MaximumTransferSize)
760 {
761 //
762 // changed interface
763 //
764 NeedSelect = TRUE;
765 }
766 }
767 }
768 else
769 {
770 //
771 // need select as the interface number differ
772 //
773 NeedSelect = TRUE;
774 }
775
776 if (!NeedSelect)
777 {
778 //
779 // interface is already selected
780 //
781 RtlCopyMemory(InterfaceInformation, Entry->Interface, min(InterfaceInformation->Length, Entry->Interface->Length));
782 }
783 else
784 {
785 //
786 // select interface
787 //
788 DPRINT1("Selecting InterfaceIndex %lu AlternateSetting %lu NumberOfPipes %lu\n", InterfaceInformation->InterfaceNumber, InterfaceInformation->AlternateSetting, InterfaceInformation->NumberOfPipes);
789 ASSERT(InterfaceInformation->Length == Entry->Interface->Length);
790
791 //
792 // build urb
793 //
794 NewUrb = AllocateItem(NonPagedPool, GET_SELECT_INTERFACE_REQUEST_SIZE(InterfaceInformation->NumberOfPipes));
795 if (!NewUrb)
796 {
797 //
798 // no memory
799 //
800 return STATUS_INSUFFICIENT_RESOURCES;
801 }
802
803 //
804 // now prepare interface urb
805 //
806 UsbBuildSelectInterfaceRequest(NewUrb, GET_SELECT_INTERFACE_REQUEST_SIZE(InterfaceInformation->NumberOfPipes), PDODeviceExtension->ConfigurationHandle, InterfaceInformation->InterfaceNumber, InterfaceInformation->AlternateSetting);
807
808 //
809 // now select the interface
810 //
811 Status = USBCCGP_SyncUrbRequest(PDODeviceExtension->NextDeviceObject, NewUrb);
812 DPRINT1("SelectInterface Status %x\n", Status);
813
814 //
815 // did it succeeed
816 //
817 if (NT_SUCCESS(Status))
818 {
819 //
820 // update configuration info
821 //
822 ASSERT(Entry->Interface->Length == NewUrb->UrbSelectInterface.Interface.Length);
823 ASSERT(InterfaceInformation->Length == NewUrb->UrbSelectInterface.Interface.Length);
824 RtlCopyMemory(Entry->Interface, &NewUrb->UrbSelectInterface.Interface, NewUrb->UrbSelectInterface.Interface.Length);
825
826 //
827 // update provided interface information
828 //
829 RtlCopyMemory(InterfaceInformation, Entry->Interface, Entry->Interface->Length);
830 }
831
832 //
833 // free urb
834 //
835 FreeItem(NewUrb);
836 }
837
838 //
839 // move to next information
840 //
841 InterfaceInformation = (PUSBD_INTERFACE_INFORMATION)((ULONG_PTR)InterfaceInformation + InterfaceInformation->Length);
842 Index++;
843 }while(Index < InterfaceInformationCount);
844
845 //
846 // store configuration handle
847 //
848 Urb->UrbSelectConfiguration.ConfigurationHandle = PDODeviceExtension->ConfigurationHandle;
849
850 DPRINT1("[USBCCGP] SelectConfiguration Function %x Completed\n", PDODeviceExtension->FunctionDescriptor->FunctionNumber);
851
852 //
853 // done
854 //
855 return STATUS_SUCCESS;
856 }
857
858 NTSTATUS
859 PDO_HandleInternalDeviceControl(
860 PDEVICE_OBJECT DeviceObject,
861 PIRP Irp)
862 {
863 PIO_STACK_LOCATION IoStack;
864 PPDO_DEVICE_EXTENSION PDODeviceExtension;
865 NTSTATUS Status;
866 PURB Urb;
867
868 //
869 // get current stack location
870 //
871 IoStack = IoGetCurrentIrpStackLocation(Irp);
872
873 //
874 // get device extension
875 //
876 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
877
878 if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_INTERNAL_USB_SUBMIT_URB)
879 {
880 //
881 // get urb
882 //
883 Urb = (PURB)IoStack->Parameters.Others.Argument1;
884 ASSERT(Urb);
885 DPRINT("IOCTL_INTERNAL_USB_SUBMIT_URB Function %x\n", Urb->UrbHeader.Function);
886
887 if (Urb->UrbHeader.Function == URB_FUNCTION_SELECT_CONFIGURATION)
888 {
889 //
890 // select configuration
891 //
892 Status = USBCCGP_PDOSelectConfiguration(DeviceObject, Irp);
893 Irp->IoStatus.Status = Status;
894 IoCompleteRequest(Irp, IO_NO_INCREMENT);
895 return Status;
896 }
897 else if (Urb->UrbHeader.Function == URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE)
898 {
899 if(Urb->UrbControlDescriptorRequest.DescriptorType == USB_DEVICE_DESCRIPTOR_TYPE)
900 {
901 //
902 // is the buffer big enough
903 //
904 if (Urb->UrbControlDescriptorRequest.TransferBufferLength < sizeof(USB_DEVICE_DESCRIPTOR))
905 {
906 //
907 // invalid buffer size
908 //
909 DPRINT1("[USBCCGP] invalid device descriptor size %lu\n", Urb->UrbControlDescriptorRequest.TransferBufferLength);
910 Urb->UrbControlDescriptorRequest.TransferBufferLength = sizeof(USB_DEVICE_DESCRIPTOR);
911 Irp->IoStatus.Status = STATUS_INVALID_BUFFER_SIZE;
912 IoCompleteRequest(Irp, IO_NO_INCREMENT);
913 return STATUS_INVALID_BUFFER_SIZE;
914 }
915
916 //
917 // copy device descriptor
918 //
919 ASSERT(Urb->UrbControlDescriptorRequest.TransferBuffer);
920 RtlCopyMemory(Urb->UrbControlDescriptorRequest.TransferBuffer, &PDODeviceExtension->DeviceDescriptor, sizeof(USB_DEVICE_DESCRIPTOR));
921 Irp->IoStatus.Status = STATUS_SUCCESS;
922 IoCompleteRequest(Irp, IO_NO_INCREMENT);
923 return STATUS_SUCCESS;
924 }
925 else if (Urb->UrbControlDescriptorRequest.DescriptorType == USB_CONFIGURATION_DESCRIPTOR_TYPE)
926 {
927 //
928 // build configuration descriptor
929 //
930 Status = USBCCGP_BuildConfigurationDescriptor(DeviceObject, Irp);
931 Irp->IoStatus.Status = Status;
932 IoCompleteRequest(Irp, IO_NO_INCREMENT);
933 return Status;
934 }
935 }
936 else
937 {
938 IoSkipCurrentIrpStackLocation(Irp);
939 Status = IoCallDriver(PDODeviceExtension->NextDeviceObject, Irp);
940 return Status;
941 }
942 }
943 else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_INTERNAL_USB_GET_PORT_STATUS)
944 {
945 IoSkipCurrentIrpStackLocation(Irp);
946 Status = IoCallDriver(PDODeviceExtension->NextDeviceObject, Irp);
947 return Status;
948 }
949 else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_INTERNAL_USB_RESET_PORT)
950 {
951 IoSkipCurrentIrpStackLocation(Irp);
952 Status = IoCallDriver(PDODeviceExtension->NextDeviceObject, Irp);
953 return Status;
954 }
955 else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_INTERNAL_USB_CYCLE_PORT)
956 {
957 IoSkipCurrentIrpStackLocation(Irp);
958 Status = IoCallDriver(PDODeviceExtension->NextDeviceObject, Irp);
959 return Status;
960 }
961
962
963
964 DPRINT1("IOCTL %x\n", IoStack->Parameters.DeviceIoControl.IoControlCode);
965 DPRINT1("InputBufferLength %lu\n", IoStack->Parameters.DeviceIoControl.InputBufferLength);
966 DPRINT1("OutputBufferLength %lu\n", IoStack->Parameters.DeviceIoControl.OutputBufferLength);
967 DPRINT1("Type3InputBuffer %p\n", IoStack->Parameters.DeviceIoControl.Type3InputBuffer);
968
969 ASSERT(FALSE);
970
971 Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
972 IoCompleteRequest(Irp, IO_NO_INCREMENT);
973 return STATUS_NOT_IMPLEMENTED;
974 }
975
976
977 NTSTATUS
978 PDO_Dispatch(
979 PDEVICE_OBJECT DeviceObject,
980 PIRP Irp)
981 {
982 PIO_STACK_LOCATION IoStack;
983 NTSTATUS Status;
984
985 /* get stack location */
986 IoStack = IoGetCurrentIrpStackLocation(Irp);
987
988 switch(IoStack->MajorFunction)
989 {
990 case IRP_MJ_PNP:
991 return PDO_HandlePnp(DeviceObject, Irp);
992 case IRP_MJ_INTERNAL_DEVICE_CONTROL:
993 return PDO_HandleInternalDeviceControl(DeviceObject, Irp);
994 default:
995 DPRINT1("PDO_Dispatch Function %x not implemented\n", IoStack->MajorFunction);
996 Status = Irp->IoStatus.Status;
997 IoCompleteRequest(Irp, IO_NO_INCREMENT);
998 return Status;
999 }
1000
1001 }