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