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