]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Library/EdkUsbLib/UsbDxeLib.c
Rename PcdPlatformBusSpeed to PcdFSBClock and update help text.
[mirror_edk2.git] / EdkModulePkg / Library / EdkUsbLib / UsbDxeLib.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 UsbDxeLib.c
15
16 Abstract:
17
18 Common Dxe Libarary for USB
19
20 Revision History
21
22 --*/
23
24 //
25 // Get Device Descriptor
26 //
27 EFI_STATUS
28 UsbGetDescriptor (
29 IN EFI_USB_IO_PROTOCOL *UsbIo,
30 IN UINT16 Value,
31 IN UINT16 Index,
32 IN UINT16 DescriptorLength,
33 OUT VOID *Descriptor,
34 OUT UINT32 *Status
35 )
36 /*++
37
38 Routine Description:
39
40 Usb Get Descriptor
41
42 Arguments:
43
44 UsbIo - EFI_USB_IO_PROTOCOL
45 Value - Device Request Value
46 Index - Device Request Index
47 DescriptorLength - Descriptor Length
48 Descriptor - Descriptor buffer to contain result
49 Status - Transfer Status
50 Returns:
51 EFI_INVALID_PARAMETER - Parameter is error
52 EFI_SUCCESS - Success
53 EFI_TIMEOUT - Device has no response
54
55 --*/
56 {
57 EFI_USB_DEVICE_REQUEST DevReq;
58
59 if (UsbIo == NULL) {
60 return EFI_INVALID_PARAMETER;
61 }
62
63 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
64
65 DevReq.RequestType = USB_DEV_GET_DESCRIPTOR_REQ_TYPE;
66 DevReq.Request = USB_DEV_GET_DESCRIPTOR;
67 DevReq.Value = Value;
68 DevReq.Index = Index;
69 DevReq.Length = DescriptorLength;
70
71 return UsbIo->UsbControlTransfer (
72 UsbIo,
73 &DevReq,
74 EfiUsbDataIn,
75 TIMEOUT_VALUE,
76 Descriptor,
77 DescriptorLength,
78 Status
79 );
80 }
81 //
82 // Set Device Descriptor
83 //
84 EFI_STATUS
85 UsbSetDescriptor (
86 IN EFI_USB_IO_PROTOCOL *UsbIo,
87 IN UINT16 Value,
88 IN UINT16 Index,
89 IN UINT16 DescriptorLength,
90 IN VOID *Descriptor,
91 OUT UINT32 *Status
92 )
93 /*++
94
95 Routine Description:
96
97 Usb Set Descriptor
98
99 Arguments:
100
101 UsbIo - EFI_USB_IO_PROTOCOL
102 Value - Device Request Value
103 Index - Device Request Index
104 DescriptorLength - Descriptor Length
105 Descriptor - Descriptor buffer to set
106 Status - Transfer Status
107 Returns:
108 EFI_INVALID_PARAMETER - Parameter is error
109 EFI_SUCCESS - Success
110 EFI_TIMEOUT - Device has no response
111
112 --*/
113 {
114 EFI_USB_DEVICE_REQUEST DevReq;
115
116 if (UsbIo == NULL) {
117 return EFI_INVALID_PARAMETER;
118 }
119
120 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
121
122 DevReq.RequestType = USB_DEV_SET_DESCRIPTOR_REQ_TYPE;
123 DevReq.Request = USB_DEV_SET_DESCRIPTOR;
124 DevReq.Value = Value;
125 DevReq.Index = Index;
126 DevReq.Length = DescriptorLength;
127
128 return UsbIo->UsbControlTransfer (
129 UsbIo,
130 &DevReq,
131 EfiUsbDataOut,
132 TIMEOUT_VALUE,
133 Descriptor,
134 DescriptorLength,
135 Status
136 );
137 }
138
139 //
140 // Get device Interface
141 //
142 EFI_STATUS
143 UsbGetDeviceInterface (
144 IN EFI_USB_IO_PROTOCOL *UsbIo,
145 IN UINT16 Index,
146 OUT UINT8 *AltSetting,
147 OUT UINT32 *Status
148 )
149 /*++
150
151 Routine Description:
152
153 Usb Get Device Interface
154
155 Arguments:
156
157 UsbIo - EFI_USB_IO_PROTOCOL
158 Index - Interface index value
159 AltSetting - Alternate setting
160 Status - Trasnsfer status
161
162 Returns:
163
164 EFI_INVALID_PARAMETER - Parameter is error
165 EFI_SUCCESS - Success
166 EFI_TIMEOUT - Device has no response
167
168
169 --*/
170 {
171 EFI_USB_DEVICE_REQUEST DevReq;
172
173 if (UsbIo == NULL) {
174 return EFI_INVALID_PARAMETER;
175 }
176
177 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
178
179 DevReq.RequestType = USB_DEV_GET_INTERFACE_REQ_TYPE;
180 DevReq.Request = USB_DEV_GET_INTERFACE;
181 DevReq.Index = Index;
182 DevReq.Length = 1;
183
184 return UsbIo->UsbControlTransfer (
185 UsbIo,
186 &DevReq,
187 EfiUsbDataIn,
188 TIMEOUT_VALUE,
189 AltSetting,
190 1,
191 Status
192 );
193 }
194 //
195 // Set device interface
196 //
197 EFI_STATUS
198 UsbSetDeviceInterface (
199 IN EFI_USB_IO_PROTOCOL *UsbIo,
200 IN UINT16 InterfaceNo,
201 IN UINT16 AltSetting,
202 OUT UINT32 *Status
203 )
204 /*++
205
206 Routine Description:
207
208 Usb Set Device Interface
209
210 Arguments:
211
212 UsbIo - EFI_USB_IO_PROTOCOL
213 InterfaceNo - Interface Number
214 AltSetting - Alternate setting
215 Status - Trasnsfer status
216
217 Returns:
218
219 EFI_INVALID_PARAMETER - Parameter is error
220 EFI_SUCCESS - Success
221 EFI_TIMEOUT - Device has no response
222
223 --*/
224 {
225 EFI_USB_DEVICE_REQUEST DevReq;
226
227 if (UsbIo == NULL) {
228 return EFI_INVALID_PARAMETER;
229 }
230
231 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
232
233 DevReq.RequestType = USB_DEV_SET_INTERFACE_REQ_TYPE;
234 DevReq.Request = USB_DEV_SET_INTERFACE;
235 DevReq.Value = AltSetting;
236 DevReq.Index = InterfaceNo;
237
238
239 return UsbIo->UsbControlTransfer (
240 UsbIo,
241 &DevReq,
242 EfiUsbNoData,
243 TIMEOUT_VALUE,
244 NULL,
245 0,
246 Status
247 );
248 }
249 //
250 // Get device configuration
251 //
252 EFI_STATUS
253 UsbGetDeviceConfiguration (
254 IN EFI_USB_IO_PROTOCOL *UsbIo,
255 OUT UINT8 *ConfigValue,
256 OUT UINT32 *Status
257 )
258 /*++
259
260 Routine Description:
261
262 Usb Get Device Configuration
263
264 Arguments:
265
266 UsbIo - EFI_USB_IO_PROTOCOL
267 ConfigValue - Config Value
268 Status - Transfer Status
269
270 Returns:
271
272 EFI_INVALID_PARAMETER - Parameter is error
273 EFI_SUCCESS - Success
274 EFI_TIMEOUT - Device has no response
275
276 --*/
277 {
278 EFI_USB_DEVICE_REQUEST DevReq;
279
280 if (UsbIo == NULL) {
281 return EFI_INVALID_PARAMETER;
282 }
283
284 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
285
286 DevReq.RequestType = USB_DEV_GET_CONFIGURATION_REQ_TYPE;
287 DevReq.Request = USB_DEV_GET_CONFIGURATION;
288 DevReq.Length = 1;
289
290 return UsbIo->UsbControlTransfer (
291 UsbIo,
292 &DevReq,
293 EfiUsbDataIn,
294 TIMEOUT_VALUE,
295 ConfigValue,
296 1,
297 Status
298 );
299 }
300 //
301 // Set device configuration
302 //
303 EFI_STATUS
304 UsbSetDeviceConfiguration (
305 IN EFI_USB_IO_PROTOCOL *UsbIo,
306 IN UINT16 Value,
307 OUT UINT32 *Status
308 )
309 /*++
310
311 Routine Description:
312
313 Usb Set Device Configuration
314
315 Arguments:
316
317 UsbIo - EFI_USB_IO_PROTOCOL
318 Value - Configuration Value to set
319 Status - Transfer status
320
321 Returns:
322
323 EFI_INVALID_PARAMETER - Parameter is error
324 EFI_SUCCESS - Success
325 EFI_TIMEOUT - Device has no response
326
327 --*/
328 {
329 EFI_USB_DEVICE_REQUEST DevReq;
330
331 if (UsbIo == NULL) {
332 return EFI_INVALID_PARAMETER;
333 }
334
335 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
336
337 DevReq.RequestType = USB_DEV_SET_CONFIGURATION_REQ_TYPE;
338 DevReq.Request = USB_DEV_SET_CONFIGURATION;
339 DevReq.Value = Value;
340
341 return UsbIo->UsbControlTransfer (
342 UsbIo,
343 &DevReq,
344 EfiUsbNoData,
345 TIMEOUT_VALUE,
346 NULL,
347 0,
348 Status
349 );
350 }
351 //
352 // Set Device Feature
353 //
354 EFI_STATUS
355 UsbSetDeviceFeature (
356 IN EFI_USB_IO_PROTOCOL *UsbIo,
357 IN EFI_USB_RECIPIENT Recipient,
358 IN UINT16 Value,
359 IN UINT16 Target,
360 OUT UINT32 *Status
361 )
362 /*++
363
364 Routine Description:
365
366 Usb Set Device Feature
367
368 Arguments:
369
370 UsbIo - EFI_USB_IO_PROTOCOL
371 Recipient - Interface/Device/Endpoint
372 Value - Request value
373 Target - Request Index
374 Status - Transfer status
375
376 Returns:
377
378 EFI_INVALID_PARAMETER - Parameter is error
379 EFI_SUCCESS - Success
380 EFI_TIMEOUT - Device has no response
381
382 --*/
383 {
384 EFI_USB_DEVICE_REQUEST DevReq;
385
386 if (UsbIo == NULL) {
387 return EFI_INVALID_PARAMETER;
388 }
389
390 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
391
392 switch (Recipient) {
393
394 case EfiUsbDevice:
395 DevReq.RequestType = 0x00;
396 break;
397
398 case EfiUsbInterface:
399 DevReq.RequestType = 0x01;
400 break;
401
402 case EfiUsbEndpoint:
403 DevReq.RequestType = 0x02;
404 break;
405 }
406 //
407 // Fill device request, see USB1.1 spec
408 //
409 DevReq.Request = USB_DEV_SET_FEATURE;
410 DevReq.Value = Value;
411 DevReq.Index = Target;
412
413
414 return UsbIo->UsbControlTransfer (
415 UsbIo,
416 &DevReq,
417 EfiUsbNoData,
418 TIMEOUT_VALUE,
419 NULL,
420 0,
421 Status
422 );
423 }
424 //
425 // Clear Device Feature
426 //
427 EFI_STATUS
428 UsbClearDeviceFeature (
429 IN EFI_USB_IO_PROTOCOL *UsbIo,
430 IN EFI_USB_RECIPIENT Recipient,
431 IN UINT16 Value,
432 IN UINT16 Target,
433 OUT UINT32 *Status
434 )
435 /*++
436
437 Routine Description:
438
439 Usb Clear Device Feature
440
441 Arguments:
442
443 UsbIo - EFI_USB_IO_PROTOCOL
444 Recipient - Interface/Device/Endpoint
445 Value - Request value
446 Target - Request Index
447 Status - Transfer status
448
449 Returns:
450
451 EFI_INVALID_PARAMETER - Parameter is error
452 EFI_SUCCESS - Success
453 EFI_TIMEOUT - Device has no response
454
455 --*/
456 {
457 EFI_USB_DEVICE_REQUEST DevReq;
458
459 if (UsbIo == NULL) {
460 return EFI_INVALID_PARAMETER;
461 }
462
463 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
464
465 switch (Recipient) {
466
467 case EfiUsbDevice:
468 DevReq.RequestType = 0x00;
469 break;
470
471 case EfiUsbInterface:
472 DevReq.RequestType = 0x01;
473 break;
474
475 case EfiUsbEndpoint:
476 DevReq.RequestType = 0x02;
477 break;
478 }
479 //
480 // Fill device request, see USB1.1 spec
481 //
482 DevReq.Request = USB_DEV_CLEAR_FEATURE;
483 DevReq.Value = Value;
484 DevReq.Index = Target;
485
486
487 return UsbIo->UsbControlTransfer (
488 UsbIo,
489 &DevReq,
490 EfiUsbNoData,
491 TIMEOUT_VALUE,
492 NULL,
493 0,
494 Status
495 );
496 }
497 //
498 // Get Device Status
499 //
500 EFI_STATUS
501 UsbGetDeviceStatus (
502 IN EFI_USB_IO_PROTOCOL *UsbIo,
503 IN EFI_USB_RECIPIENT Recipient,
504 IN UINT16 Target,
505 OUT UINT16 *DevStatus,
506 OUT UINT32 *Status
507 )
508 /*++
509
510 Routine Description:
511
512 Usb Get Device Status
513
514 Arguments:
515
516 UsbIo - EFI_USB_IO_PROTOCOL
517 Recipient - Interface/Device/Endpoint
518 Target - Request index
519 DevStatus - Device status
520 Status - Transfer status
521
522 Returns:
523
524 EFI_INVALID_PARAMETER - Parameter is error
525 EFI_SUCCESS - Success
526 EFI_TIMEOUT - Device has no response
527
528 --*/
529 {
530 EFI_USB_DEVICE_REQUEST DevReq;
531
532 if (UsbIo == NULL) {
533 return EFI_INVALID_PARAMETER;
534 }
535
536 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
537
538 switch (Recipient) {
539
540 case EfiUsbDevice:
541 DevReq.RequestType = 0x80;
542 break;
543
544 case EfiUsbInterface:
545 DevReq.RequestType = 0x81;
546 break;
547
548 case EfiUsbEndpoint:
549 DevReq.RequestType = 0x82;
550 break;
551 }
552 //
553 // Fill device request, see USB1.1 spec
554 //
555 DevReq.Request = USB_DEV_GET_STATUS;
556 DevReq.Value = 0;
557 DevReq.Index = Target;
558 DevReq.Length = 2;
559
560 return UsbIo->UsbControlTransfer (
561 UsbIo,
562 &DevReq,
563 EfiUsbDataIn,
564 TIMEOUT_VALUE,
565 DevStatus,
566 2,
567 Status
568 );
569 }
570 //
571 // Usb Get String
572 //
573 EFI_STATUS
574 UsbGetString (
575 IN EFI_USB_IO_PROTOCOL *UsbIo,
576 IN UINT16 LangID,
577 IN UINT8 Index,
578 IN VOID *Buf,
579 IN UINTN BufSize,
580 OUT UINT32 *Status
581 )
582 /*++
583
584 Routine Description:
585
586 Usb Get String
587
588 Arguments:
589
590 UsbIo - EFI_USB_IO_PROTOCOL
591 LangID - Language ID
592 Index - Request index
593 Buf - Buffer to store string
594 BufSize - Buffer size
595 Status - Transfer status
596
597 Returns:
598
599 EFI_INVALID_PARAMETER - Parameter is error
600 EFI_SUCCESS - Success
601 EFI_TIMEOUT - Device has no response
602
603 --*/
604 {
605 UINT16 Value;
606
607 if (UsbIo == NULL) {
608 return EFI_INVALID_PARAMETER;
609 }
610 //
611 // Fill value, see USB1.1 spec
612 //
613 Value = (UINT16) ((USB_DT_STRING << 8) | Index);
614
615 return UsbGetDescriptor (
616 UsbIo,
617 Value,
618 LangID,
619 (UINT16) BufSize,
620 Buf,
621 Status
622 );
623 }
624
625 EFI_STATUS
626 UsbClearEndpointHalt (
627 IN EFI_USB_IO_PROTOCOL *UsbIo,
628 IN UINT8 EndpointNo,
629 OUT UINT32 *Status
630 )
631 /*++
632
633 Routine Description:
634
635 Clear endpoint stall
636
637 Arguments:
638
639 UsbIo - EFI_USB_IO_PROTOCOL
640 EndpointNo - Endpoint Number
641 Status - Transfer Status
642
643 Returns:
644
645 EFI_NOT_FOUND - Can't find the Endpoint
646 EFI_DEVICE_ERROR - Hardware error
647 EFI_SUCCESS - Success
648
649 --*/
650 {
651 EFI_STATUS Result;
652 EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
653 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
654 UINT8 Index;
655
656 ZeroMem (&EndpointDescriptor, sizeof (EFI_USB_ENDPOINT_DESCRIPTOR));
657 //
658 // First seach the endpoint descriptor for that endpoint addr
659 //
660 Result = UsbIo->UsbGetInterfaceDescriptor (
661 UsbIo,
662 &InterfaceDescriptor
663 );
664 if (EFI_ERROR (Result)) {
665 return Result;
666 }
667
668 for (Index = 0; Index < InterfaceDescriptor.NumEndpoints; Index++) {
669 Result = UsbIo->UsbGetEndpointDescriptor (
670 UsbIo,
671 Index,
672 &EndpointDescriptor
673 );
674 if (EFI_ERROR (Result)) {
675 continue;
676 }
677
678 if (EndpointDescriptor.EndpointAddress == EndpointNo) {
679 break;
680 }
681 }
682
683 if (Index == InterfaceDescriptor.NumEndpoints) {
684 //
685 // No such endpoint
686 //
687 return EFI_NOT_FOUND;
688 }
689
690 Result = UsbClearDeviceFeature (
691 UsbIo,
692 EfiUsbEndpoint,
693 EfiUsbEndpointHalt,
694 EndpointDescriptor.EndpointAddress,
695 Status
696 );
697
698 return Result;
699 }