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