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