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