]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbDesc.c
Fix X64 clang warnings.
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbDesc.c
1 /** @file
2
3 Manage Usb Descriptor List
4
5 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UsbBus.h"
17
18
19 /**
20 Free the interface setting descriptor.
21
22 @param Setting The descriptor to free.
23
24 **/
25 VOID
26 UsbFreeInterfaceDesc (
27 IN USB_INTERFACE_SETTING *Setting
28 )
29 {
30 USB_ENDPOINT_DESC *Ep;
31 UINTN Index;
32
33 if (Setting->Endpoints != NULL) {
34 //
35 // Each interface setting may have several endpoints, free them first.
36 //
37 for (Index = 0; Index < Setting->Desc.NumEndpoints; Index++) {
38 Ep = Setting->Endpoints[Index];
39
40 if (Ep != NULL) {
41 FreePool (Ep);
42 }
43 }
44
45 //
46 // Only call FreePool() if NumEndpoints > 0.
47 //
48 if (Setting->Desc.NumEndpoints > 0) {
49 FreePool (Setting->Endpoints);
50 }
51 }
52
53 FreePool (Setting);
54 }
55
56
57 /**
58 Free a configuration descriptor with its interface
59 descriptors. It may be initialized partially.
60
61 @param Config The configuration descriptor to free.
62
63 **/
64 VOID
65 UsbFreeConfigDesc (
66 IN USB_CONFIG_DESC *Config
67 )
68 {
69 USB_INTERFACE_DESC *Interface;
70 UINTN Index;
71 UINTN SetIndex;
72
73 if (Config->Interfaces != NULL) {
74 //
75 // A configuration may have several interfaces, free the interface
76 //
77 for (Index = 0; Index < Config->Desc.NumInterfaces; Index++) {
78 Interface = Config->Interfaces[Index];
79
80 if (Interface == NULL) {
81 continue;
82 }
83
84 //
85 // Each interface may have several settings, free the settings
86 //
87 for (SetIndex = 0; SetIndex < Interface->NumOfSetting; SetIndex++) {
88 if (Interface->Settings[SetIndex] != NULL) {
89 UsbFreeInterfaceDesc (Interface->Settings[SetIndex]);
90 }
91 }
92
93 FreePool (Interface);
94 }
95
96 FreePool (Config->Interfaces);
97 }
98
99 FreePool (Config);
100
101 }
102
103
104 /**
105 Free a device descriptor with its configurations.
106
107 @param DevDesc The device descriptor.
108
109 **/
110 VOID
111 UsbFreeDevDesc (
112 IN USB_DEVICE_DESC *DevDesc
113 )
114 {
115 UINTN Index;
116
117 if (DevDesc->Configs != NULL) {
118 for (Index = 0; Index < DevDesc->Desc.NumConfigurations; Index++) {
119 if (DevDesc->Configs[Index] != NULL) {
120 UsbFreeConfigDesc (DevDesc->Configs[Index]);
121 }
122 }
123
124 FreePool (DevDesc->Configs);
125 }
126
127 FreePool (DevDesc);
128 }
129
130
131 /**
132 Create a descriptor.
133
134 @param DescBuf The buffer of raw descriptor.
135 @param Len The length of the raw descriptor buffer.
136 @param Type The type of descriptor to create.
137 @param Consumed Number of bytes consumed.
138
139 @return Created descriptor or NULL.
140
141 **/
142 VOID *
143 UsbCreateDesc (
144 IN UINT8 *DescBuf,
145 IN INTN Len,
146 IN UINT8 Type,
147 OUT INTN *Consumed
148 )
149 {
150 USB_DESC_HEAD *Head;
151 INTN DescLen;
152 INTN CtrlLen;
153 INTN Offset;
154 VOID *Desc;
155
156 DescLen = 0;
157 CtrlLen = 0;
158 *Consumed = 0;
159
160 switch (Type) {
161 case USB_DESC_TYPE_DEVICE:
162 DescLen = sizeof (EFI_USB_DEVICE_DESCRIPTOR);
163 CtrlLen = sizeof (USB_DEVICE_DESC);
164 break;
165
166 case USB_DESC_TYPE_CONFIG:
167 DescLen = sizeof (EFI_USB_CONFIG_DESCRIPTOR);
168 CtrlLen = sizeof (USB_CONFIG_DESC);
169 break;
170
171 case USB_DESC_TYPE_INTERFACE:
172 DescLen = sizeof (EFI_USB_INTERFACE_DESCRIPTOR);
173 CtrlLen = sizeof (USB_INTERFACE_SETTING);
174 break;
175
176 case USB_DESC_TYPE_ENDPOINT:
177 DescLen = sizeof (EFI_USB_ENDPOINT_DESCRIPTOR);
178 CtrlLen = sizeof (USB_ENDPOINT_DESC);
179 break;
180 }
181
182 //
183 // All the descriptor has a common LTV (Length, Type, Value)
184 // format. Skip the descriptor that isn't of this Type
185 //
186 Offset = 0;
187 Head = (USB_DESC_HEAD*)DescBuf;
188
189 while ((Offset < Len) && (Head->Type != Type)) {
190 Offset += Head->Len;
191 Head = (USB_DESC_HEAD*)(DescBuf + Offset);
192 }
193
194 if ((Len <= Offset) || (Len < Offset + DescLen) ||
195 (Head->Type != Type) || (Head->Len != DescLen)) {
196 DEBUG (( EFI_D_ERROR, "UsbCreateDesc: met mal-format descriptor\n"));
197 return NULL;
198 }
199
200 Desc = AllocateZeroPool ((UINTN) CtrlLen);
201 if (Desc == NULL) {
202 return NULL;
203 }
204
205 CopyMem (Desc, Head, (UINTN) DescLen);
206
207 *Consumed = Offset + Head->Len;
208
209 return Desc;
210 }
211
212
213 /**
214 Parse an interface descriptor and its endpoints.
215
216 @param DescBuf The buffer of raw descriptor.
217 @param Len The length of the raw descriptor buffer.
218 @param Consumed The number of raw descriptor consumed.
219
220 @return The create interface setting or NULL if failed.
221
222 **/
223 USB_INTERFACE_SETTING *
224 UsbParseInterfaceDesc (
225 IN UINT8 *DescBuf,
226 IN INTN Len,
227 OUT INTN *Consumed
228 )
229 {
230 USB_INTERFACE_SETTING *Setting;
231 USB_ENDPOINT_DESC *Ep;
232 UINTN Index;
233 UINTN NumEp;
234 INTN Used;
235 INTN Offset;
236
237 *Consumed = 0;
238 Setting = UsbCreateDesc (DescBuf, Len, USB_DESC_TYPE_INTERFACE, &Used);
239
240 if (Setting == NULL) {
241 DEBUG (( EFI_D_ERROR, "UsbParseInterfaceDesc: failed to create interface descriptor\n"));
242 return NULL;
243 }
244
245 Offset = Used;
246
247 //
248 // Create an array to hold the interface's endpoints
249 //
250 NumEp = Setting->Desc.NumEndpoints;
251
252 DEBUG (( EFI_D_INFO, "UsbParseInterfaceDesc: interface %d(setting %d) has %d endpoints\n",
253 Setting->Desc.InterfaceNumber, Setting->Desc.AlternateSetting, (UINT32)NumEp));
254
255 if (NumEp == 0) {
256 goto ON_EXIT;
257 }
258
259 Setting->Endpoints = AllocateZeroPool (sizeof (USB_ENDPOINT_DESC *) * NumEp);
260
261 if (Setting->Endpoints == NULL) {
262 goto ON_ERROR;
263 }
264
265 //
266 // Create the endpoints for this interface
267 //
268 for (Index = 0; Index < NumEp; Index++) {
269 Ep = UsbCreateDesc (DescBuf + Offset, Len - Offset, USB_DESC_TYPE_ENDPOINT, &Used);
270
271 if (Ep == NULL) {
272 DEBUG (( EFI_D_ERROR, "UsbParseInterfaceDesc: failed to create endpoint(index %d)\n", (UINT32)Index));
273 goto ON_ERROR;
274 }
275
276 Setting->Endpoints[Index] = Ep;
277 Offset += Used;
278 }
279
280
281 ON_EXIT:
282 *Consumed = Offset;
283 return Setting;
284
285 ON_ERROR:
286 UsbFreeInterfaceDesc (Setting);
287 return NULL;
288 }
289
290
291 /**
292 Parse the configuration descriptor and its interfaces.
293
294 @param DescBuf The buffer of raw descriptor.
295 @param Len The length of the raw descriptor buffer.
296
297 @return The created configuration descriptor.
298
299 **/
300 USB_CONFIG_DESC *
301 UsbParseConfigDesc (
302 IN UINT8 *DescBuf,
303 IN INTN Len
304 )
305 {
306 USB_CONFIG_DESC *Config;
307 USB_INTERFACE_SETTING *Setting;
308 USB_INTERFACE_DESC *Interface;
309 UINTN Index;
310 UINTN NumIf;
311 INTN Consumed;
312
313 ASSERT (DescBuf != NULL);
314
315 Config = UsbCreateDesc (DescBuf, Len, USB_DESC_TYPE_CONFIG, &Consumed);
316
317 if (Config == NULL) {
318 return NULL;
319 }
320
321 //
322 // Initialize an array of setting for the configuration's interfaces.
323 //
324 NumIf = Config->Desc.NumInterfaces;
325 Config->Interfaces = AllocateZeroPool (sizeof (USB_INTERFACE_DESC *) * NumIf);
326
327 if (Config->Interfaces == NULL) {
328 goto ON_ERROR;
329 }
330
331 DEBUG (( EFI_D_INFO, "UsbParseConfigDesc: config %d has %d interfaces\n",
332 Config->Desc.ConfigurationValue, (UINT32)NumIf));
333
334 for (Index = 0; Index < NumIf; Index++) {
335 Interface = AllocateZeroPool (sizeof (USB_INTERFACE_DESC));
336
337 if (Interface == NULL) {
338 goto ON_ERROR;
339 }
340
341 Config->Interfaces[Index] = Interface;
342 }
343
344 //
345 // If a configuration has several interfaces, these interfaces are
346 // numbered from zero to n. If a interface has several settings,
347 // these settings are also number from zero to m. The interface
348 // setting must be organized as |interface 0, setting 0|interface 0
349 // setting 1|interface 1, setting 0|interface 2, setting 0|. Check
350 // USB2.0 spec, page 267.
351 //
352 DescBuf += Consumed;
353 Len -= Consumed;
354
355 //
356 // Make allowances for devices that return extra data at the
357 // end of their config descriptors
358 //
359 while (Len >= sizeof (EFI_USB_INTERFACE_DESCRIPTOR)) {
360 Setting = UsbParseInterfaceDesc (DescBuf, Len, &Consumed);
361
362 if (Setting == NULL) {
363 DEBUG (( EFI_D_ERROR, "UsbParseConfigDesc: failed to parse interface setting\n"));
364 goto ON_ERROR;
365
366 } else if (Setting->Desc.InterfaceNumber >= NumIf) {
367 DEBUG (( EFI_D_ERROR, "UsbParseConfigDesc: mal-formated interface descriptor\n"));
368
369 UsbFreeInterfaceDesc (Setting);
370 goto ON_ERROR;
371 }
372
373 //
374 // Insert the descriptor to the corresponding set.
375 //
376 Interface = Config->Interfaces[Setting->Desc.InterfaceNumber];
377
378 if (Interface->NumOfSetting >= USB_MAX_INTERFACE_SETTING) {
379 goto ON_ERROR;
380 }
381
382 Interface->Settings[Interface->NumOfSetting] = Setting;
383 Interface->NumOfSetting++;
384
385 DescBuf += Consumed;
386 Len -= Consumed;
387 }
388
389 return Config;
390
391 ON_ERROR:
392 UsbFreeConfigDesc (Config);
393 return NULL;
394 }
395
396
397 /**
398 USB standard control transfer support routine. This
399 function is used by USB device. It is possible that
400 the device's interfaces are still waiting to be
401 enumerated.
402
403 @param UsbDev The usb device.
404 @param Direction The direction of data transfer.
405 @param Type Standard / class specific / vendor specific.
406 @param Target The receiving target.
407 @param Request Which request.
408 @param Value The wValue parameter of the request.
409 @param Index The wIndex parameter of the request.
410 @param Buf The buffer to receive data into / transmit from.
411 @param Length The length of the buffer.
412
413 @retval EFI_SUCCESS The control request is executed.
414 @retval EFI_DEVICE_ERROR Failed to execute the control transfer.
415
416 **/
417 EFI_STATUS
418 UsbCtrlRequest (
419 IN USB_DEVICE *UsbDev,
420 IN EFI_USB_DATA_DIRECTION Direction,
421 IN UINTN Type,
422 IN UINTN Target,
423 IN UINTN Request,
424 IN UINT16 Value,
425 IN UINT16 Index,
426 IN OUT VOID *Buf,
427 IN UINTN Length
428 )
429 {
430 EFI_USB_DEVICE_REQUEST DevReq;
431 EFI_STATUS Status;
432 UINT32 Result;
433 UINTN Len;
434
435 ASSERT ((UsbDev != NULL) && (UsbDev->Bus != NULL));
436
437 DevReq.RequestType = USB_REQUEST_TYPE (Direction, Type, Target);
438 DevReq.Request = (UINT8) Request;
439 DevReq.Value = Value;
440 DevReq.Index = Index;
441 DevReq.Length = (UINT16) Length;
442
443 Len = Length;
444 Status = UsbHcControlTransfer (
445 UsbDev->Bus,
446 UsbDev->Address,
447 UsbDev->Speed,
448 UsbDev->MaxPacket0,
449 &DevReq,
450 Direction,
451 Buf,
452 &Len,
453 USB_GENERAL_DEVICE_REQUEST_TIMEOUT,
454 &UsbDev->Translator,
455 &Result
456 );
457
458 return Status;
459 }
460
461
462 /**
463 Get the standard descriptors.
464
465 @param UsbDev The USB device to read descriptor from.
466 @param DescType The type of descriptor to read.
467 @param DescIndex The index of descriptor to read.
468 @param LangId Language ID, only used to get string, otherwise set
469 it to 0.
470 @param Buf The buffer to hold the descriptor read.
471 @param Length The length of the buffer.
472
473 @retval EFI_SUCCESS The descriptor is read OK.
474 @retval Others Failed to retrieve the descriptor.
475
476 **/
477 EFI_STATUS
478 UsbCtrlGetDesc (
479 IN USB_DEVICE *UsbDev,
480 IN UINTN DescType,
481 IN UINTN DescIndex,
482 IN UINT16 LangId,
483 OUT VOID *Buf,
484 IN UINTN Length
485 )
486 {
487 EFI_STATUS Status;
488
489 Status = UsbCtrlRequest (
490 UsbDev,
491 EfiUsbDataIn,
492 USB_REQ_TYPE_STANDARD,
493 USB_TARGET_DEVICE,
494 USB_REQ_GET_DESCRIPTOR,
495 (UINT16) ((DescType << 8) | DescIndex),
496 LangId,
497 Buf,
498 Length
499 );
500
501 return Status;
502 }
503
504
505 /**
506 Return the max packet size for endpoint zero. This function
507 is the first function called to get descriptors during bus
508 enumeration.
509
510 @param UsbDev The usb device.
511
512 @retval EFI_SUCCESS The max packet size of endpoint zero is retrieved.
513 @retval EFI_DEVICE_ERROR Failed to retrieve it.
514
515 **/
516 EFI_STATUS
517 UsbGetMaxPacketSize0 (
518 IN USB_DEVICE *UsbDev
519 )
520 {
521 EFI_USB_DEVICE_DESCRIPTOR DevDesc;
522 EFI_STATUS Status;
523 UINTN Index;
524
525
526 //
527 // Get the first 8 bytes of the device descriptor which contains
528 // max packet size for endpoint 0, which is at least 8.
529 //
530 UsbDev->MaxPacket0 = 8;
531
532 for (Index = 0; Index < 3; Index++) {
533 Status = UsbCtrlGetDesc (UsbDev, USB_DESC_TYPE_DEVICE, 0, 0, &DevDesc, 8);
534
535 if (!EFI_ERROR (Status)) {
536 UsbDev->MaxPacket0 = DevDesc.MaxPacketSize0;
537 return EFI_SUCCESS;
538 }
539
540 gBS->Stall (USB_RETRY_MAX_PACK_SIZE_STALL);
541 }
542
543 return EFI_DEVICE_ERROR;
544 }
545
546
547 /**
548 Get the device descriptor for the device.
549
550 @param UsbDev The Usb device to retrieve descriptor from.
551
552 @retval EFI_SUCCESS The device descriptor is returned.
553 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
554
555 **/
556 EFI_STATUS
557 UsbGetDevDesc (
558 IN USB_DEVICE *UsbDev
559 )
560 {
561 USB_DEVICE_DESC *DevDesc;
562 EFI_STATUS Status;
563
564 DevDesc = AllocateZeroPool (sizeof (USB_DEVICE_DESC));
565
566 if (DevDesc == NULL) {
567 return EFI_OUT_OF_RESOURCES;
568 }
569
570 Status = UsbCtrlGetDesc (
571 UsbDev,
572 USB_DESC_TYPE_DEVICE,
573 0,
574 0,
575 DevDesc,
576 sizeof (EFI_USB_DEVICE_DESCRIPTOR)
577 );
578
579 if (EFI_ERROR (Status)) {
580 gBS->FreePool (DevDesc);
581 } else {
582 UsbDev->DevDesc = DevDesc;
583 }
584
585 return Status;
586 }
587
588
589 /**
590 Retrieve the indexed string for the language. It requires two
591 steps to get a string, first to get the string's length. Then
592 the string itself.
593
594 @param UsbDev The usb device.
595 @param Index The index the string to retrieve.
596 @param LangId Language ID.
597
598 @return The created string descriptor or NULL.
599
600 **/
601 EFI_USB_STRING_DESCRIPTOR *
602 UsbGetOneString (
603 IN USB_DEVICE *UsbDev,
604 IN UINT8 Index,
605 IN UINT16 LangId
606 )
607 {
608 EFI_USB_STRING_DESCRIPTOR Desc;
609 EFI_STATUS Status;
610 UINT8 *Buf;
611
612 //
613 // First get two bytes which contains the string length.
614 //
615 Status = UsbCtrlGetDesc (UsbDev, USB_DESC_TYPE_STRING, Index, LangId, &Desc, 2);
616
617 if (EFI_ERROR (Status)) {
618 return NULL;
619 }
620
621 Buf = AllocateZeroPool (Desc.Length);
622
623 if (Buf == NULL) {
624 return NULL;
625 }
626
627 Status = UsbCtrlGetDesc (
628 UsbDev,
629 USB_DESC_TYPE_STRING,
630 Index,
631 LangId,
632 Buf,
633 Desc.Length
634 );
635
636 if (EFI_ERROR (Status)) {
637 FreePool (Buf);
638 return NULL;
639 }
640
641 return (EFI_USB_STRING_DESCRIPTOR *) Buf;
642 }
643
644
645 /**
646 Build the language ID table for string descriptors.
647
648 @param UsbDev The Usb device.
649
650 @retval EFI_UNSUPPORTED This device doesn't support string table.
651
652 **/
653 EFI_STATUS
654 UsbBuildLangTable (
655 IN USB_DEVICE *UsbDev
656 )
657 {
658 EFI_USB_STRING_DESCRIPTOR *Desc;
659 EFI_STATUS Status;
660 UINTN Index;
661 UINTN Max;
662 UINT16 *Point;
663
664 //
665 // The string of language ID zero returns the supported languages
666 //
667 Desc = UsbGetOneString (UsbDev, 0, 0);
668
669 if (Desc == NULL) {
670 return EFI_UNSUPPORTED;
671 }
672
673 if (Desc->Length < 4) {
674 Status = EFI_UNSUPPORTED;
675 goto ON_EXIT;
676 }
677
678 Status = EFI_SUCCESS;
679
680 Max = (Desc->Length - 2) / 2;
681 Max = MIN(Max, USB_MAX_LANG_ID);
682
683 Point = Desc->String;
684 for (Index = 0; Index < Max; Index++) {
685 UsbDev->LangId[Index] = *Point;
686 Point++;
687 }
688
689 UsbDev->TotalLangId = (UINT16)Max;
690
691 ON_EXIT:
692 gBS->FreePool (Desc);
693 return Status;
694 }
695
696
697 /**
698 Retrieve the indexed configure for the device. USB device
699 returns the configuration together with the interfaces for
700 this configuration. Configuration descriptor is also of
701 variable length.
702
703 @param UsbDev The Usb interface.
704 @param Index The index of the configuration.
705
706 @return The created configuration descriptor.
707
708 **/
709 EFI_USB_CONFIG_DESCRIPTOR *
710 UsbGetOneConfig (
711 IN USB_DEVICE *UsbDev,
712 IN UINT8 Index
713 )
714 {
715 EFI_USB_CONFIG_DESCRIPTOR Desc;
716 EFI_STATUS Status;
717 VOID *Buf;
718
719 //
720 // First get four bytes which contains the total length
721 // for this configuration.
722 //
723 Status = UsbCtrlGetDesc (UsbDev, USB_DESC_TYPE_CONFIG, Index, 0, &Desc, 8);
724
725 if (EFI_ERROR (Status)) {
726 DEBUG (( EFI_D_ERROR, "UsbGetOneConfig: failed to get descript length(%d) %r\n",
727 Desc.TotalLength, Status));
728
729 return NULL;
730 }
731
732 DEBUG (( EFI_D_INFO, "UsbGetOneConfig: total length is %d\n", Desc.TotalLength));
733
734 Buf = AllocateZeroPool (Desc.TotalLength);
735
736 if (Buf == NULL) {
737 return NULL;
738 }
739
740 Status = UsbCtrlGetDesc (UsbDev, USB_DESC_TYPE_CONFIG, Index, 0, Buf, Desc.TotalLength);
741
742 if (EFI_ERROR (Status)) {
743 DEBUG (( EFI_D_ERROR, "UsbGetOneConfig: failed to get full descript %r\n", Status));
744
745 FreePool (Buf);
746 return NULL;
747 }
748
749 return Buf;
750 }
751
752
753 /**
754 Build the whole array of descriptors. This function must
755 be called after UsbGetMaxPacketSize0 returns the max packet
756 size correctly for endpoint 0.
757
758 @param UsbDev The Usb device.
759
760 @retval EFI_SUCCESS The descriptor table is build.
761 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the descriptor.
762
763 **/
764 EFI_STATUS
765 UsbBuildDescTable (
766 IN USB_DEVICE *UsbDev
767 )
768 {
769 EFI_USB_CONFIG_DESCRIPTOR *Config;
770 USB_DEVICE_DESC *DevDesc;
771 USB_CONFIG_DESC *ConfigDesc;
772 UINT8 NumConfig;
773 EFI_STATUS Status;
774 UINT8 Index;
775
776 //
777 // Get the device descriptor, then allocate the configure
778 // descriptor pointer array to hold configurations.
779 //
780 Status = UsbGetDevDesc (UsbDev);
781
782 if (EFI_ERROR (Status)) {
783 DEBUG (( EFI_D_ERROR, "UsbBuildDescTable: failed to get device descriptor - %r\n", Status));
784 return Status;
785 }
786
787 DevDesc = UsbDev->DevDesc;
788 NumConfig = DevDesc->Desc.NumConfigurations;
789 DevDesc->Configs = AllocateZeroPool (NumConfig * sizeof (USB_CONFIG_DESC *));
790
791 if (DevDesc->Configs == NULL) {
792 return EFI_OUT_OF_RESOURCES;
793 }
794
795 DEBUG (( EFI_D_INFO, "UsbBuildDescTable: device has %d configures\n", NumConfig));
796
797 //
798 // Read each configurations, then parse them
799 //
800 for (Index = 0; Index < NumConfig; Index++) {
801 Config = UsbGetOneConfig (UsbDev, Index);
802
803 if (Config == NULL) {
804 DEBUG (( EFI_D_ERROR, "UsbBuildDescTable: failed to get configure (index %d)\n", Index));
805
806 //
807 // If we can get the default descriptor, it is likely that the
808 // device is still operational.
809 //
810 if (Index == 0) {
811 return EFI_DEVICE_ERROR;
812 }
813
814 break;
815 }
816
817 ConfigDesc = UsbParseConfigDesc ((UINT8 *) Config, Config->TotalLength);
818
819 FreePool (Config);
820
821 if (ConfigDesc == NULL) {
822 DEBUG (( EFI_D_ERROR, "UsbBuildDescTable: failed to parse configure (index %d)\n", Index));
823
824 //
825 // If we can get the default descriptor, it is likely that the
826 // device is still operational.
827 //
828 if (Index == 0) {
829 return EFI_DEVICE_ERROR;
830 }
831
832 break;
833 }
834
835 DevDesc->Configs[Index] = ConfigDesc;
836 }
837
838 //
839 // Don't return error even this function failed because
840 // it is possible for the device to not support strings.
841 //
842 Status = UsbBuildLangTable (UsbDev);
843
844 if (EFI_ERROR (Status)) {
845 DEBUG (( EFI_D_ERROR, "UsbBuildDescTable: get language ID table %r\n", Status));
846 }
847
848 return EFI_SUCCESS;
849 }
850
851
852 /**
853 Set the device's address.
854
855 @param UsbDev The device to set address to.
856 @param Address The address to set.
857
858 @retval EFI_SUCCESS The device is set to the address.
859 @retval Others Failed to set the device address.
860
861 **/
862 EFI_STATUS
863 UsbSetAddress (
864 IN USB_DEVICE *UsbDev,
865 IN UINT8 Address
866 )
867 {
868 EFI_STATUS Status;
869
870 Status = UsbCtrlRequest (
871 UsbDev,
872 EfiUsbNoData,
873 USB_REQ_TYPE_STANDARD,
874 USB_TARGET_DEVICE,
875 USB_REQ_SET_ADDRESS,
876 Address,
877 0,
878 NULL,
879 0
880 );
881
882 return Status;
883 }
884
885
886 /**
887 Set the device's configuration. This function changes
888 the device's internal state. UsbSelectConfig changes
889 the Usb bus's internal state.
890
891 @param UsbDev The USB device to set configure to.
892 @param ConfigIndex The configure index to set.
893
894 @retval EFI_SUCCESS The device is configured now.
895 @retval Others Failed to set the device configure.
896
897 **/
898 EFI_STATUS
899 UsbSetConfig (
900 IN USB_DEVICE *UsbDev,
901 IN UINT8 ConfigIndex
902 )
903 {
904 EFI_STATUS Status;
905
906 Status = UsbCtrlRequest (
907 UsbDev,
908 EfiUsbNoData,
909 USB_REQ_TYPE_STANDARD,
910 USB_TARGET_DEVICE,
911 USB_REQ_SET_CONFIG,
912 ConfigIndex,
913 0,
914 NULL,
915 0
916 );
917
918 return Status;
919 }
920
921
922 /**
923 Usb UsbIo interface to clear the feature. This is should
924 only be used by HUB which is considered a device driver
925 on top of the UsbIo interface.
926
927 @param UsbIo The UsbIo interface.
928 @param Target The target of the transfer: endpoint/device.
929 @param Feature The feature to clear.
930 @param Index The wIndex parameter.
931
932 @retval EFI_SUCCESS The device feature is cleared.
933 @retval Others Failed to clear the feature.
934
935 **/
936 EFI_STATUS
937 UsbIoClearFeature (
938 IN EFI_USB_IO_PROTOCOL *UsbIo,
939 IN UINTN Target,
940 IN UINT16 Feature,
941 IN UINT16 Index
942 )
943 {
944 EFI_USB_DEVICE_REQUEST DevReq;
945 UINT32 UsbResult;
946 EFI_STATUS Status;
947
948 DevReq.RequestType = USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD, Target);
949 DevReq.Request = USB_REQ_CLEAR_FEATURE;
950 DevReq.Value = Feature;
951 DevReq.Index = Index;
952 DevReq.Length = 0;
953
954 Status = UsbIo->UsbControlTransfer (
955 UsbIo,
956 &DevReq,
957 EfiUsbNoData,
958 USB_CLEAR_FEATURE_REQUEST_TIMEOUT,
959 NULL,
960 0,
961 &UsbResult
962 );
963
964 return Status;
965 }