[USBCCG]
[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, InterfaceNumber) + 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 UCHAR InterfaceNumber;
464
465 //
466 // get current stack location
467 //
468 IoStack = IoGetCurrentIrpStackLocation(Irp);
469
470 DPRINT("USBCCGP_BuildConfigurationDescriptor\n");
471
472 //
473 // get device extension
474 //
475 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
476
477 //
478 // get configuration descriptor
479 //
480 ConfigurationDescriptor = PDODeviceExtension->ConfigurationDescriptor;
481
482 //
483 // calculate size of configuration descriptor
484 //
485 TotalSize = sizeof(USB_CONFIGURATION_DESCRIPTOR);
486
487 for(Index = 0; Index < PDODeviceExtension->FunctionDescriptor->NumberOfInterfaces; Index++)
488 {
489 //
490 // get current interface descriptor
491 //
492 InterfaceDescriptor = PDODeviceExtension->FunctionDescriptor->InterfaceDescriptorList[Index];
493 InterfaceNumber = InterfaceDescriptor->bInterfaceNumber;
494
495 //
496 // add to size and move to next descriptor
497 //
498 TotalSize += InterfaceDescriptor->bLength;
499 InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
500
501 do
502 {
503 if ((ULONG_PTR)InterfaceDescriptor >= ((ULONG_PTR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength))
504 {
505 //
506 // reached end of configuration descriptor
507 //
508 break;
509 }
510
511 //
512 // association descriptors are removed
513 //
514 if (InterfaceDescriptor->bDescriptorType != USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)
515 {
516 if (InterfaceDescriptor->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE)
517 {
518 if (InterfaceNumber != InterfaceDescriptor->bInterfaceNumber)
519 {
520 //
521 // reached next descriptor
522 //
523 break;
524 }
525
526 //
527 // include alternate descriptor
528 //
529 }
530
531 //
532 // append size
533 //
534 TotalSize += InterfaceDescriptor->bLength;
535 }
536
537 //
538 // move to next descriptor
539 //
540 InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
541 }while(TRUE);
542 }
543
544 //
545 // now allocate temporary buffer for the configuration descriptor
546 //
547 Buffer = AllocateItem(NonPagedPool, TotalSize);
548 if (!Buffer)
549 {
550 //
551 // failed to allocate buffer
552 //
553 DPRINT1("[USBCCGP] Failed to allocate %lu Bytes\n", TotalSize);
554 return STATUS_INSUFFICIENT_RESOURCES;
555 }
556
557 //
558 // first copy the configuration descriptor
559 //
560 RtlCopyMemory(Buffer, ConfigurationDescriptor, sizeof(USB_CONFIGURATION_DESCRIPTOR));
561 BufferPtr = (PUCHAR)((ULONG_PTR)Buffer + ConfigurationDescriptor->bLength);
562
563 for(Index = 0; Index < PDODeviceExtension->FunctionDescriptor->NumberOfInterfaces; Index++)
564 {
565 //
566 // get current interface descriptor
567 //
568 InterfaceDescriptor = PDODeviceExtension->FunctionDescriptor->InterfaceDescriptorList[Index];
569 InterfaceNumber = InterfaceDescriptor->bInterfaceNumber;
570
571 //
572 // copy descriptor and move to next descriptor
573 //
574 RtlCopyMemory(BufferPtr, InterfaceDescriptor, InterfaceDescriptor->bLength);
575 BufferPtr += InterfaceDescriptor->bLength;
576 InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
577
578 do
579 {
580 if ((ULONG_PTR)InterfaceDescriptor >= ((ULONG_PTR)ConfigurationDescriptor + ConfigurationDescriptor->wTotalLength))
581 {
582 //
583 // reached end of configuration descriptor
584 //
585 break;
586 }
587
588 //
589 // association descriptors are removed
590 //
591 if (InterfaceDescriptor->bDescriptorType != USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)
592 {
593 if (InterfaceDescriptor->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE)
594 {
595 if (InterfaceNumber != InterfaceDescriptor->bInterfaceNumber)
596 {
597 //
598 // reached next descriptor
599 //
600 break;
601 }
602
603 //
604 // include alternate descriptor
605 //
606 DPRINT("InterfaceDescriptor %p Alternate %x InterfaceNumber %x\n", InterfaceDescriptor, InterfaceDescriptor->bAlternateSetting, InterfaceDescriptor->bInterfaceNumber);
607 }
608
609 //
610 // copy descriptor
611 //
612 RtlCopyMemory(BufferPtr, InterfaceDescriptor, InterfaceDescriptor->bLength);
613 BufferPtr += InterfaceDescriptor->bLength;
614 }
615
616 //
617 // move to next descriptor
618 //
619 InterfaceDescriptor = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)InterfaceDescriptor + InterfaceDescriptor->bLength);
620 }while(TRUE);
621 }
622
623 //
624 // modify configuration descriptor
625 //
626 ConfigurationDescriptor = Buffer;
627 ConfigurationDescriptor->wTotalLength = TotalSize;
628 ConfigurationDescriptor->bNumInterfaces = PDODeviceExtension->FunctionDescriptor->NumberOfInterfaces;
629
630 //
631 // get urb
632 //
633 Urb = (PURB)IoStack->Parameters.Others.Argument1;
634 ASSERT(Urb);
635
636 //
637 // copy descriptor
638 //
639 RtlCopyMemory(Urb->UrbControlDescriptorRequest.TransferBuffer, Buffer, min(TotalSize, Urb->UrbControlDescriptorRequest.TransferBufferLength));
640
641 //
642 // store final size
643 //
644 Urb->UrbControlDescriptorRequest.TransferBufferLength = TotalSize;
645
646 //
647 // free buffer
648 //
649 FreeItem(Buffer);
650
651 //
652 // done
653 //
654 return STATUS_SUCCESS;
655 }
656
657 NTSTATUS
658 USBCCGP_PDOSelectConfiguration(
659 PDEVICE_OBJECT DeviceObject,
660 PIRP Irp)
661 {
662 PIO_STACK_LOCATION IoStack;
663 PPDO_DEVICE_EXTENSION PDODeviceExtension;
664 PURB Urb, NewUrb;
665 PUSBD_INTERFACE_INFORMATION InterfaceInformation;
666 ULONG InterfaceIndex, Length;
667 PUSBD_INTERFACE_LIST_ENTRY Entry;
668 ULONG NeedSelect, FoundInterface;
669 NTSTATUS Status;
670
671 //
672 // get current stack location
673 //
674 IoStack = IoGetCurrentIrpStackLocation(Irp);
675
676 //
677 // get device extension
678 //
679 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
680
681 //
682 // get urb
683 //
684 Urb = (PURB)IoStack->Parameters.Others.Argument1;
685 ASSERT(Urb);
686
687 //
688 // is there already an configuration handle
689 //
690 if (Urb->UrbSelectConfiguration.ConfigurationHandle)
691 {
692 //
693 // nothing to do
694 //
695 return STATUS_SUCCESS;
696 }
697
698 // sanity checks
699 //C_ASSERT(sizeof(struct _URB_HEADER) == 16);
700 //C_ASSERT(FIELD_OFFSET(struct _URB_SELECT_CONFIGURATION, Interface.Length) == 24);
701 //C_ASSERT(sizeof(USBD_INTERFACE_INFORMATION) == 36);
702 //C_ASSERT(sizeof(struct _URB_SELECT_CONFIGURATION) == 0x3C);
703
704 // available buffer length
705 Length = Urb->UrbSelectConfiguration.Hdr.Length - FIELD_OFFSET(struct _URB_SELECT_CONFIGURATION, Interface.Length);
706
707 //
708 // check all interfaces
709 //
710 InterfaceInformation = &Urb->UrbSelectConfiguration.Interface;
711
712 Entry = NULL;
713 do
714 {
715 DPRINT1("[USBCCGP] SelectConfiguration Function %x InterfaceNumber %x Alternative %x Length %lu InterfaceInformation->Length %lu\n", PDODeviceExtension->FunctionDescriptor->FunctionNumber, InterfaceInformation->InterfaceNumber, InterfaceInformation->AlternateSetting, Length, InterfaceInformation->Length);
716 ASSERT(InterfaceInformation->Length);
717 //
718 // search for the interface in the local interface list
719 //
720 FoundInterface = FALSE;
721 for(InterfaceIndex = 0; InterfaceIndex < PDODeviceExtension->FunctionDescriptor->NumberOfInterfaces; InterfaceIndex++)
722 {
723 if (PDODeviceExtension->FunctionDescriptor->InterfaceDescriptorList[InterfaceIndex]->bInterfaceNumber == InterfaceInformation->InterfaceNumber)
724 {
725 // found interface entry
726 FoundInterface = TRUE;
727 break;
728 }
729 }
730
731 if (!FoundInterface)
732 {
733 //
734 // invalid parameter
735 //
736 DPRINT1("InterfaceInformation InterfaceNumber %x Alternative %x NumberOfPipes %x not found\n", InterfaceInformation->InterfaceNumber, InterfaceInformation->AlternateSetting, InterfaceInformation->NumberOfPipes);
737 ASSERT(FALSE);
738 return STATUS_INVALID_PARAMETER;
739 }
740
741 //
742 // now query the total interface list
743 //
744 Entry = NULL;
745 for(InterfaceIndex = 0; InterfaceIndex < PDODeviceExtension->InterfaceListCount; InterfaceIndex++)
746 {
747 if (PDODeviceExtension->InterfaceList[InterfaceIndex].Interface->InterfaceNumber == InterfaceInformation->InterfaceNumber)
748 {
749 //
750 // found entry
751 //
752 Entry = &PDODeviceExtension->InterfaceList[InterfaceIndex];
753 }
754 }
755
756 //
757 // sanity check
758 //
759 ASSERT(Entry);
760 if (!Entry)
761 {
762 //
763 // corruption detected
764 //
765 KeBugCheck(0);
766 }
767
768 NeedSelect = FALSE;
769 if (Entry->InterfaceDescriptor->bAlternateSetting == InterfaceInformation->AlternateSetting)
770 {
771
772 for(InterfaceIndex = 0; InterfaceIndex < InterfaceInformation->NumberOfPipes; InterfaceIndex++)
773 {
774 if (InterfaceInformation->Pipes[InterfaceIndex].MaximumTransferSize != Entry->Interface->Pipes[InterfaceIndex].MaximumTransferSize)
775 {
776 //
777 // changed interface
778 //
779 NeedSelect = TRUE;
780 }
781 }
782 }
783 else
784 {
785 //
786 // need select as the interface number differ
787 //
788 NeedSelect = TRUE;
789 }
790
791 if (!NeedSelect)
792 {
793 //
794 // interface is already selected
795 //
796 ASSERT(Length >= Entry->Interface->Length);
797 RtlCopyMemory(InterfaceInformation, Entry->Interface, Entry->Interface->Length);
798
799 //
800 // adjust remaining buffer size
801 //
802 ASSERT(Entry->Interface->Length);
803 Length -= Entry->Interface->Length;
804
805 //
806 // move to next output interface information
807 //
808 InterfaceInformation = (PUSBD_INTERFACE_INFORMATION)((ULONG_PTR)InterfaceInformation + Entry->Interface->Length);
809 }
810 else
811 {
812 //
813 // select interface
814 //
815 DPRINT1("Selecting InterfaceIndex %lu AlternateSetting %lu NumberOfPipes %lu\n", InterfaceInformation->InterfaceNumber, InterfaceInformation->AlternateSetting, InterfaceInformation->NumberOfPipes);
816 ASSERT(InterfaceInformation->Length == Entry->Interface->Length);
817
818 //
819 // build urb
820 //
821 NewUrb = AllocateItem(NonPagedPool, GET_SELECT_INTERFACE_REQUEST_SIZE(InterfaceInformation->NumberOfPipes));
822 if (!NewUrb)
823 {
824 //
825 // no memory
826 //
827 return STATUS_INSUFFICIENT_RESOURCES;
828 }
829
830 //
831 // now prepare interface urb
832 //
833 UsbBuildSelectInterfaceRequest(NewUrb, GET_SELECT_INTERFACE_REQUEST_SIZE(InterfaceInformation->NumberOfPipes), PDODeviceExtension->ConfigurationHandle, InterfaceInformation->InterfaceNumber, InterfaceInformation->AlternateSetting);
834
835 //
836 // now select the interface
837 //
838 Status = USBCCGP_SyncUrbRequest(PDODeviceExtension->NextDeviceObject, NewUrb);
839 DPRINT1("SelectInterface Status %x\n", Status);
840
841 if (!NT_SUCCESS(Status))
842 {
843 //
844 // failed
845 //
846 break;
847 }
848
849 //
850 // update configuration info
851 //
852 ASSERT(Entry->Interface->Length >= NewUrb->UrbSelectInterface.Interface.Length);
853 ASSERT(Length >= NewUrb->UrbSelectInterface.Interface.Length);
854 RtlCopyMemory(Entry->Interface, &NewUrb->UrbSelectInterface.Interface, NewUrb->UrbSelectInterface.Interface.Length);
855
856 //
857 // update provided interface information
858 //
859 ASSERT(Length >= Entry->Interface->Length);
860 RtlCopyMemory(InterfaceInformation, Entry->Interface, Entry->Interface->Length);
861
862 //
863 // decrement remaining buffer size
864 //
865 ASSERT(Entry->Interface->Length);
866 Length -= Entry->Interface->Length;
867
868 //
869 // adjust output buffer offset
870 //
871 InterfaceInformation = (PUSBD_INTERFACE_INFORMATION)((ULONG_PTR)InterfaceInformation + Entry->Interface->Length);
872
873 //
874 // free urb
875 //
876 FreeItem(NewUrb);
877 }
878
879 }while(Length);
880
881 //
882 // store configuration handle
883 //
884 Urb->UrbSelectConfiguration.ConfigurationHandle = PDODeviceExtension->ConfigurationHandle;
885
886 DPRINT1("[USBCCGP] SelectConfiguration Function %x Completed\n", PDODeviceExtension->FunctionDescriptor->FunctionNumber);
887
888 //
889 // done
890 //
891 return STATUS_SUCCESS;
892 }
893
894 NTSTATUS
895 PDO_HandleInternalDeviceControl(
896 PDEVICE_OBJECT DeviceObject,
897 PIRP Irp)
898 {
899 PIO_STACK_LOCATION IoStack;
900 PPDO_DEVICE_EXTENSION PDODeviceExtension;
901 NTSTATUS Status;
902 PURB Urb;
903
904 //
905 // get current stack location
906 //
907 IoStack = IoGetCurrentIrpStackLocation(Irp);
908
909 //
910 // get device extension
911 //
912 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
913
914 if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_INTERNAL_USB_SUBMIT_URB)
915 {
916 //
917 // get urb
918 //
919 Urb = (PURB)IoStack->Parameters.Others.Argument1;
920 ASSERT(Urb);
921 DPRINT("IOCTL_INTERNAL_USB_SUBMIT_URB Function %x\n", Urb->UrbHeader.Function);
922
923 if (Urb->UrbHeader.Function == URB_FUNCTION_SELECT_CONFIGURATION)
924 {
925 //
926 // select configuration
927 //
928 Status = USBCCGP_PDOSelectConfiguration(DeviceObject, Irp);
929 Irp->IoStatus.Status = Status;
930 IoCompleteRequest(Irp, IO_NO_INCREMENT);
931 return Status;
932 }
933 else if (Urb->UrbHeader.Function == URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE)
934 {
935 if(Urb->UrbControlDescriptorRequest.DescriptorType == USB_DEVICE_DESCRIPTOR_TYPE)
936 {
937 //
938 // is the buffer big enough
939 //
940 if (Urb->UrbControlDescriptorRequest.TransferBufferLength < sizeof(USB_DEVICE_DESCRIPTOR))
941 {
942 //
943 // invalid buffer size
944 //
945 DPRINT1("[USBCCGP] invalid device descriptor size %lu\n", Urb->UrbControlDescriptorRequest.TransferBufferLength);
946 Urb->UrbControlDescriptorRequest.TransferBufferLength = sizeof(USB_DEVICE_DESCRIPTOR);
947 Irp->IoStatus.Status = STATUS_INVALID_BUFFER_SIZE;
948 IoCompleteRequest(Irp, IO_NO_INCREMENT);
949 return STATUS_INVALID_BUFFER_SIZE;
950 }
951
952 //
953 // copy device descriptor
954 //
955 ASSERT(Urb->UrbControlDescriptorRequest.TransferBuffer);
956 RtlCopyMemory(Urb->UrbControlDescriptorRequest.TransferBuffer, &PDODeviceExtension->DeviceDescriptor, sizeof(USB_DEVICE_DESCRIPTOR));
957 Irp->IoStatus.Status = STATUS_SUCCESS;
958 IoCompleteRequest(Irp, IO_NO_INCREMENT);
959 return STATUS_SUCCESS;
960 }
961 else if (Urb->UrbControlDescriptorRequest.DescriptorType == USB_CONFIGURATION_DESCRIPTOR_TYPE)
962 {
963 //
964 // build configuration descriptor
965 //
966 Status = USBCCGP_BuildConfigurationDescriptor(DeviceObject, Irp);
967 Irp->IoStatus.Status = Status;
968 IoCompleteRequest(Irp, IO_NO_INCREMENT);
969 return Status;
970 }
971 }
972 else
973 {
974 IoSkipCurrentIrpStackLocation(Irp);
975 Status = IoCallDriver(PDODeviceExtension->NextDeviceObject, Irp);
976 return Status;
977 }
978 }
979 else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_INTERNAL_USB_GET_PORT_STATUS)
980 {
981 IoSkipCurrentIrpStackLocation(Irp);
982 Status = IoCallDriver(PDODeviceExtension->NextDeviceObject, Irp);
983 return Status;
984 }
985 else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_INTERNAL_USB_RESET_PORT)
986 {
987 IoSkipCurrentIrpStackLocation(Irp);
988 Status = IoCallDriver(PDODeviceExtension->NextDeviceObject, Irp);
989 return Status;
990 }
991 else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_INTERNAL_USB_CYCLE_PORT)
992 {
993 IoSkipCurrentIrpStackLocation(Irp);
994 Status = IoCallDriver(PDODeviceExtension->NextDeviceObject, Irp);
995 return Status;
996 }
997
998
999
1000 DPRINT1("IOCTL %x\n", IoStack->Parameters.DeviceIoControl.IoControlCode);
1001 DPRINT1("InputBufferLength %lu\n", IoStack->Parameters.DeviceIoControl.InputBufferLength);
1002 DPRINT1("OutputBufferLength %lu\n", IoStack->Parameters.DeviceIoControl.OutputBufferLength);
1003 DPRINT1("Type3InputBuffer %p\n", IoStack->Parameters.DeviceIoControl.Type3InputBuffer);
1004
1005 ASSERT(FALSE);
1006
1007 Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
1008 IoCompleteRequest(Irp, IO_NO_INCREMENT);
1009 return STATUS_NOT_IMPLEMENTED;
1010 }
1011
1012
1013 NTSTATUS
1014 PDO_Dispatch(
1015 PDEVICE_OBJECT DeviceObject,
1016 PIRP Irp)
1017 {
1018 PIO_STACK_LOCATION IoStack;
1019 NTSTATUS Status;
1020
1021 /* get stack location */
1022 IoStack = IoGetCurrentIrpStackLocation(Irp);
1023
1024 switch(IoStack->MajorFunction)
1025 {
1026 case IRP_MJ_PNP:
1027 return PDO_HandlePnp(DeviceObject, Irp);
1028 case IRP_MJ_INTERNAL_DEVICE_CONTROL:
1029 return PDO_HandleInternalDeviceControl(DeviceObject, Irp);
1030 default:
1031 DPRINT1("PDO_Dispatch Function %x not implemented\n", IoStack->MajorFunction);
1032 Status = Irp->IoStatus.Status;
1033 IoCompleteRequest(Irp, IO_NO_INCREMENT);
1034 return Status;
1035 }
1036
1037 }