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