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