]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiUsbLib/UsbDxeLib.c
Sync code with MdePkg spec for Scsi library class and CustomDecompress library class.
[mirror_edk2.git] / MdePkg / Library / UefiUsbLib / UsbDxeLib.c
1 /** @file
2
3 Copyright (c) 2004 - 2007, 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 Common Dxe Libarary for USB
13
14 **/
15
16 #include <UefiUsbLibInternal.h>
17
18
19 /**
20 Usb Get Descriptor
21
22 @param UsbIo EFI_USB_IO_PROTOCOL
23 @param Value Device Request Value
24 @param Index Device Request Index
25 @param DescriptorLength Descriptor Length
26 @param Descriptor Descriptor buffer to contain result
27 @param Status Transfer Status
28
29 @retval EFI_INVALID_PARAMETER Parameter is error
30 @retval EFI_SUCCESS Success
31 @retval EFI_TIMEOUT Device has no response
32
33 **/
34 EFI_STATUS
35 EFIAPI
36 UsbGetDescriptor (
37 IN EFI_USB_IO_PROTOCOL *UsbIo,
38 IN UINT16 Value,
39 IN UINT16 Index,
40 IN UINT16 DescriptorLength,
41 OUT VOID *Descriptor,
42 OUT UINT32 *Status
43 )
44 {
45 EFI_USB_DEVICE_REQUEST DevReq;
46
47 if (UsbIo == NULL) {
48 return EFI_INVALID_PARAMETER;
49 }
50
51 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
52
53 DevReq.RequestType = USB_DEV_GET_DESCRIPTOR_REQ_TYPE;
54 DevReq.Request = USB_REQ_GET_DESCRIPTOR;
55 DevReq.Value = Value;
56 DevReq.Index = Index;
57 DevReq.Length = DescriptorLength;
58
59 return UsbIo->UsbControlTransfer (
60 UsbIo,
61 &DevReq,
62 EfiUsbDataIn,
63 TIMEOUT_VALUE,
64 Descriptor,
65 DescriptorLength,
66 Status
67 );
68 }
69
70
71 /**
72 Usb Set Descriptor
73
74 @param UsbIo EFI_USB_IO_PROTOCOL
75 @param Value Device Request Value
76 @param Index Device Request Index
77 @param DescriptorLength Descriptor Length
78 @param Descriptor Descriptor buffer to set
79 @param Status Transfer Status
80
81 @retval EFI_INVALID_PARAMETER Parameter is error
82 @retval EFI_SUCCESS Success
83 @retval EFI_TIMEOUT Device has no response
84
85 **/
86 EFI_STATUS
87 EFIAPI
88 UsbSetDescriptor (
89 IN EFI_USB_IO_PROTOCOL *UsbIo,
90 IN UINT16 Value,
91 IN UINT16 Index,
92 IN UINT16 DescriptorLength,
93 IN VOID *Descriptor,
94 OUT UINT32 *Status
95 )
96 {
97 EFI_USB_DEVICE_REQUEST DevReq;
98
99 if (UsbIo == NULL) {
100 return EFI_INVALID_PARAMETER;
101 }
102
103 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
104
105 DevReq.RequestType = USB_DEV_SET_DESCRIPTOR_REQ_TYPE;
106 DevReq.Request = USB_REQ_SET_DESCRIPTOR;
107 DevReq.Value = Value;
108 DevReq.Index = Index;
109 DevReq.Length = DescriptorLength;
110
111 return UsbIo->UsbControlTransfer (
112 UsbIo,
113 &DevReq,
114 EfiUsbDataOut,
115 TIMEOUT_VALUE,
116 Descriptor,
117 DescriptorLength,
118 Status
119 );
120 }
121
122
123 /**
124 Usb Get Device Interface
125
126 @param UsbIo EFI_USB_IO_PROTOCOL
127 @param Index Interface index value
128 @param AltSetting Alternate setting
129 @param Status Trasnsfer status
130
131 @retval EFI_INVALID_PARAMETER Parameter is error
132 @retval EFI_SUCCESS Success
133 @retval EFI_TIMEOUT Device has no response
134
135 **/
136 EFI_STATUS
137 EFIAPI
138 UsbGetInterface (
139 IN EFI_USB_IO_PROTOCOL *UsbIo,
140 IN UINT16 Index,
141 OUT UINT8 *AltSetting,
142 OUT UINT32 *Status
143 )
144 {
145 EFI_USB_DEVICE_REQUEST DevReq;
146
147 if (UsbIo == NULL) {
148 return EFI_INVALID_PARAMETER;
149 }
150
151 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
152
153 DevReq.RequestType = USB_DEV_GET_INTERFACE_REQ_TYPE;
154 DevReq.Request = USB_REQ_GET_INTERFACE;
155 DevReq.Index = Index;
156 DevReq.Length = 1;
157
158 return UsbIo->UsbControlTransfer (
159 UsbIo,
160 &DevReq,
161 EfiUsbDataIn,
162 TIMEOUT_VALUE,
163 AltSetting,
164 1,
165 Status
166 );
167 }
168
169
170 /**
171 Usb Set Device Interface
172
173 @param UsbIo EFI_USB_IO_PROTOCOL
174 @param InterfaceNo Interface Number
175 @param AltSetting Alternate setting
176 @param Status Trasnsfer status
177
178 @retval EFI_INVALID_PARAMETER Parameter is error
179 @retval EFI_SUCCESS Success
180 @retval EFI_TIMEOUT Device has no response
181
182 **/
183 EFI_STATUS
184 EFIAPI
185 UsbSetInterface (
186 IN EFI_USB_IO_PROTOCOL *UsbIo,
187 IN UINT16 InterfaceNo,
188 IN UINT16 AltSetting,
189 OUT UINT32 *Status
190 )
191 {
192 EFI_USB_DEVICE_REQUEST DevReq;
193
194 if (UsbIo == NULL) {
195 return EFI_INVALID_PARAMETER;
196 }
197
198 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
199
200 DevReq.RequestType = USB_DEV_SET_INTERFACE_REQ_TYPE;
201 DevReq.Request = USB_REQ_SET_INTERFACE;
202 DevReq.Value = AltSetting;
203 DevReq.Index = InterfaceNo;
204
205
206 return UsbIo->UsbControlTransfer (
207 UsbIo,
208 &DevReq,
209 EfiUsbNoData,
210 TIMEOUT_VALUE,
211 NULL,
212 0,
213 Status
214 );
215 }
216
217
218 /**
219 Usb Get Device Configuration
220
221 @param UsbIo EFI_USB_IO_PROTOCOL
222 @param ConfigValue Config Value
223 @param Status Transfer Status
224
225 @retval EFI_INVALID_PARAMETER Parameter is error
226 @retval EFI_SUCCESS Success
227 @retval EFI_TIMEOUT Device has no response
228
229 **/
230 EFI_STATUS
231 EFIAPI
232 UsbGetConfiguration (
233 IN EFI_USB_IO_PROTOCOL *UsbIo,
234 OUT UINT8 *ConfigValue,
235 OUT UINT32 *Status
236 )
237 {
238 EFI_USB_DEVICE_REQUEST DevReq;
239
240 if (UsbIo == NULL) {
241 return EFI_INVALID_PARAMETER;
242 }
243
244 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
245
246 DevReq.RequestType = USB_DEV_GET_CONFIGURATION_REQ_TYPE;
247 DevReq.Request = USB_REQ_GET_CONFIG;
248 DevReq.Length = 1;
249
250 return UsbIo->UsbControlTransfer (
251 UsbIo,
252 &DevReq,
253 EfiUsbDataIn,
254 TIMEOUT_VALUE,
255 ConfigValue,
256 1,
257 Status
258 );
259 }
260
261
262 /**
263 Usb Set Device Configuration
264
265 @param UsbIo EFI_USB_IO_PROTOCOL
266 @param Value Configuration Value to set
267 @param Status Transfer status
268
269 @retval EFI_INVALID_PARAMETER Parameter is error
270 @retval EFI_SUCCESS Success
271 @retval EFI_TIMEOUT Device has no response
272
273 **/
274 EFI_STATUS
275 EFIAPI
276 UsbSetConfiguration (
277 IN EFI_USB_IO_PROTOCOL *UsbIo,
278 IN UINT16 Value,
279 OUT UINT32 *Status
280 )
281 {
282 EFI_USB_DEVICE_REQUEST DevReq;
283
284 if (UsbIo == NULL) {
285 return EFI_INVALID_PARAMETER;
286 }
287
288 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
289
290 DevReq.RequestType = USB_DEV_SET_CONFIGURATION_REQ_TYPE;
291 DevReq.Request = USB_REQ_SET_CONFIG;
292 DevReq.Value = Value;
293
294 return UsbIo->UsbControlTransfer (
295 UsbIo,
296 &DevReq,
297 EfiUsbNoData,
298 TIMEOUT_VALUE,
299 NULL,
300 0,
301 Status
302 );
303 }
304
305
306 /**
307 Usb Set Device Feature
308
309 @param UsbIo EFI_USB_IO_PROTOCOL
310 @param Recipient Interface/Device/Endpoint
311 @param Value Request value
312 @param Target Request Index
313 @param Status Transfer status
314
315 @retval EFI_INVALID_PARAMETER Parameter is error
316 @retval EFI_SUCCESS Success
317 @retval EFI_TIMEOUT Device has no response
318
319 **/
320 EFI_STATUS
321 EFIAPI
322 UsbSetFeature (
323 IN EFI_USB_IO_PROTOCOL *UsbIo,
324 IN UINTN Recipient,
325 IN UINT16 Value,
326 IN UINT16 Target,
327 OUT UINT32 *Status
328 )
329 {
330 EFI_USB_DEVICE_REQUEST DevReq;
331
332 if (UsbIo == NULL) {
333 return EFI_INVALID_PARAMETER;
334 }
335
336 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
337
338 switch (Recipient) {
339
340 case USB_TARGET_DEVICE:
341 DevReq.RequestType = USB_DEV_SET_FEATURE_REQ_TYPE_D;
342 break;
343
344 case USB_TARGET_INTERFACE:
345 DevReq.RequestType = USB_DEV_SET_FEATURE_REQ_TYPE_I;
346 break;
347
348 case USB_TARGET_ENDPOINT:
349 DevReq.RequestType = USB_DEV_SET_FEATURE_REQ_TYPE_E;
350 break;
351 }
352 //
353 // Fill device request, see USB1.1 spec
354 //
355 DevReq.Request = USB_REQ_SET_FEATURE;
356 DevReq.Value = Value;
357 DevReq.Index = Target;
358
359
360 return UsbIo->UsbControlTransfer (
361 UsbIo,
362 &DevReq,
363 EfiUsbNoData,
364 TIMEOUT_VALUE,
365 NULL,
366 0,
367 Status
368 );
369 }
370
371
372 /**
373 Usb Clear Device Feature
374
375 @param UsbIo EFI_USB_IO_PROTOCOL
376 @param Recipient Interface/Device/Endpoint
377 @param Value Request value
378 @param Target Request Index
379 @param Status Transfer status
380
381 @retval EFI_INVALID_PARAMETER Parameter is error
382 @retval EFI_SUCCESS Success
383 @retval EFI_TIMEOUT Device has no response
384
385 **/
386 EFI_STATUS
387 EFIAPI
388 UsbClearFeature (
389 IN EFI_USB_IO_PROTOCOL *UsbIo,
390 IN UINTN Recipient,
391 IN UINT16 Value,
392 IN UINT16 Target,
393 OUT UINT32 *Status
394 )
395 {
396 EFI_USB_DEVICE_REQUEST DevReq;
397
398 if (UsbIo == NULL) {
399 return EFI_INVALID_PARAMETER;
400 }
401
402 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
403
404 switch (Recipient) {
405
406 case USB_TARGET_DEVICE:
407 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_D;
408 break;
409
410 case USB_TARGET_INTERFACE:
411 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_I;
412 break;
413
414 case USB_TARGET_ENDPOINT:
415 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_E;
416 break;
417 }
418 //
419 // Fill device request, see USB1.1 spec
420 //
421 DevReq.Request = USB_REQ_CLEAR_FEATURE;
422 DevReq.Value = Value;
423 DevReq.Index = Target;
424
425
426 return UsbIo->UsbControlTransfer (
427 UsbIo,
428 &DevReq,
429 EfiUsbNoData,
430 TIMEOUT_VALUE,
431 NULL,
432 0,
433 Status
434 );
435 }
436
437
438 /**
439 Usb Get Device Status
440
441 @param UsbIo EFI_USB_IO_PROTOCOL
442 @param Recipient Interface/Device/Endpoint
443 @param Target Request index
444 @param DevStatus Device status
445 @param Status Transfer status
446
447 @retval EFI_INVALID_PARAMETER Parameter is error
448 @retval EFI_SUCCESS Success
449 @retval EFI_TIMEOUT Device has no response
450
451 **/
452 EFI_STATUS
453 EFIAPI
454 UsbGetStatus (
455 IN EFI_USB_IO_PROTOCOL *UsbIo,
456 IN UINTN Recipient,
457 IN UINT16 Target,
458 OUT UINT16 *DevStatus,
459 OUT UINT32 *Status
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 USB_TARGET_DEVICE:
473 DevReq.RequestType = USB_DEV_GET_STATUS_REQ_TYPE_D;
474 break;
475
476 case USB_TARGET_INTERFACE:
477 DevReq.RequestType = USB_DEV_GET_STATUS_REQ_TYPE_I;
478 break;
479
480 case USB_TARGET_ENDPOINT:
481 DevReq.RequestType = USB_DEV_GET_STATUS_REQ_TYPE_E;
482 break;
483 }
484 //
485 // Fill device request, see USB1.1 spec
486 //
487 DevReq.Request = USB_REQ_GET_STATUS;
488 DevReq.Value = 0;
489 DevReq.Index = Target;
490 DevReq.Length = 2;
491
492 return UsbIo->UsbControlTransfer (
493 UsbIo,
494 &DevReq,
495 EfiUsbDataIn,
496 TIMEOUT_VALUE,
497 DevStatus,
498 2,
499 Status
500 );
501 }
502
503
504
505 /**
506 Clear endpoint stall
507
508 @param UsbIo EFI_USB_IO_PROTOCOL
509 @param EndpointNo Endpoint Number
510 @param Status Transfer Status
511
512 @retval EFI_NOT_FOUND Can't find the Endpoint
513 @retval EFI_DEVICE_ERROR Hardware error
514 @retval EFI_SUCCESS Success
515
516 **/
517 EFI_STATUS
518 EFIAPI
519 UsbClearEndpointHalt (
520 IN EFI_USB_IO_PROTOCOL *UsbIo,
521 IN UINT8 EndpointNo,
522 OUT UINT32 *Status
523 )
524 {
525 EFI_STATUS Result;
526 EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
527 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
528 UINT8 Index;
529
530 ZeroMem (&EndpointDescriptor, sizeof (EFI_USB_ENDPOINT_DESCRIPTOR));
531 //
532 // First seach the endpoint descriptor for that endpoint addr
533 //
534 Result = UsbIo->UsbGetInterfaceDescriptor (
535 UsbIo,
536 &InterfaceDescriptor
537 );
538 if (EFI_ERROR (Result)) {
539 return Result;
540 }
541
542 for (Index = 0; Index < InterfaceDescriptor.NumEndpoints; Index++) {
543 Result = UsbIo->UsbGetEndpointDescriptor (
544 UsbIo,
545 Index,
546 &EndpointDescriptor
547 );
548 if (EFI_ERROR (Result)) {
549 continue;
550 }
551
552 if (EndpointDescriptor.EndpointAddress == EndpointNo) {
553 break;
554 }
555 }
556
557 if (Index == InterfaceDescriptor.NumEndpoints) {
558 //
559 // No such endpoint
560 //
561 return EFI_NOT_FOUND;
562 }
563
564 Result = UsbClearFeature (
565 UsbIo,
566 USB_TARGET_ENDPOINT,
567 USB_FEATURE_ENDPOINT_HALT,
568 EndpointDescriptor.EndpointAddress,
569 Status
570 );
571
572 return Result;
573 }