]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Bus/Usb/UsbBus/Dxe/usbutil.c
Initial import.
[mirror_edk2.git] / EdkModulePkg / Bus / Usb / UsbBus / Dxe / usbutil.c
1 /*++
2
3 Copyright (c) 2006, 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 Module Name:
13 usbutil.c
14
15 Abstract:
16
17 Helper functions for USB
18
19 Revision History
20
21 --*/
22
23 #include "usbbus.h"
24
25 //
26 // Following APIs are used to query Port Status
27 //
28 BOOLEAN
29 IsPortConnect (
30 IN UINT16 PortStatus
31 )
32 /*++
33
34 Routine Description:
35 Tell if there is a device connected to that port according to
36 the Port Status.
37
38 Parameters:
39 PortStatus - The status value of that port.
40
41 Return Value:
42 TRUE
43 FALSE
44
45 --*/
46 {
47 //
48 // return the bit 0 value of PortStatus
49 //
50 if ((PortStatus & USB_PORT_STAT_CONNECTION) != 0) {
51 return TRUE;
52 } else {
53 return FALSE;
54 }
55 }
56
57 BOOLEAN
58 IsPortEnable (
59 IN UINT16 PortStatus
60 )
61 /*++
62
63 Routine Description:
64 Tell if Port is enabled.
65
66 Arguments:
67 PortStatus - The status value of that port.
68
69 Returns:
70 TRUE - Port is enable
71 FALSE - Port is disable
72
73 --*/
74 {
75 //
76 // return the bit 1 value of PortStatus
77 //
78 if ((PortStatus & USB_PORT_STAT_ENABLE) != 0) {
79 return TRUE;
80 } else {
81 return FALSE;
82 }
83 }
84
85 BOOLEAN
86 IsPortInReset (
87 IN UINT16 PortStatus
88 )
89 /*++
90
91 Routine Description:
92 Tell if the port is being reset.
93
94 Arguments:
95 PortStatus - The status value of that port.
96
97 Returns:
98 TRUE
99 FALSE
100
101 --*/
102 {
103 //
104 // return the bit 4 value of PortStatus
105 //
106 if ((PortStatus & USB_PORT_STAT_RESET) != 0) {
107 return TRUE;
108 } else {
109 return FALSE;
110 }
111 }
112
113 BOOLEAN
114 IsPortPowerApplied (
115 IN UINT16 PortStatus
116 )
117 /*++
118
119 Routine Description:
120 Tell if there is power applied to that port.
121
122 Arguments:
123 PortStatus - The status value of that port.
124
125 Returns:
126 TRUE
127 FALSE
128
129 --*/
130 {
131 //
132 // return the bit 8 value of PortStatus
133 //
134 if ((PortStatus & USB_PORT_STAT_POWER) != 0) {
135 return TRUE;
136 } else {
137 return FALSE;
138 }
139 }
140
141 BOOLEAN
142 IsPortLowSpeedDeviceAttached (
143 IN UINT16 PortStatus
144 )
145 /*++
146
147 Routine Description:
148 Tell if the connected device is a low device.
149
150 Arguments:
151 PortStatus - The status value of that port.
152
153 Returns:
154 TRUE
155 FALSE
156
157 --*/
158 {
159 //
160 // return the bit 9 value of PortStatus
161 //
162 if ((PortStatus & USB_PORT_STAT_LOW_SPEED) != 0) {
163 return TRUE;
164 } else {
165 return FALSE;
166 }
167 }
168
169 BOOLEAN
170 IsPortSuspend (
171 IN UINT16 PortStatus
172 )
173 /*++
174
175 Routine Description:
176 Tell if the port is suspend.
177
178 Arguments:
179 PortStatus - The status value of that port.
180
181 Returns:
182 TRUE
183 FALSE
184
185 --*/
186 {
187 //
188 // return the bit 2 value of PortStatus
189 //
190 if ((PortStatus & USB_PORT_STAT_SUSPEND) != 0) {
191 return TRUE;
192 } else {
193 return FALSE;
194 }
195 }
196 //
197 // Following APIs are used to query Port Change Status
198 //
199 BOOLEAN
200 IsPortConnectChange (
201 IN UINT16 PortChangeStatus
202 )
203 /*++
204
205 Routine Description:
206 Tell if there is a Connect Change status in that port.
207
208 Arguments:
209 PortChangeStatus - The status value of that port.
210
211 Returns:
212 TRUE
213 FALSE
214
215 --*/
216 {
217 //
218 // return the bit 0 value of PortChangeStatus
219 //
220 if ((PortChangeStatus & USB_PORT_STAT_C_CONNECTION) != 0) {
221 return TRUE;
222 } else {
223 return FALSE;
224 }
225 }
226
227 BOOLEAN
228 IsPortEnableDisableChange (
229 IN UINT16 PortChangeStatus
230 )
231 /*++
232
233 Routine Description:
234 Tell if there is a Enable/Disable change in that port.
235
236 Arguments:
237 PortChangeStatus - The status value of that port.
238
239 Returns:
240 TRUE
241 FALSE
242
243 --*/
244 {
245 //
246 // return the bit 1 value of PortChangeStatus
247 //
248 if ((PortChangeStatus & USB_PORT_STAT_C_ENABLE) != 0) {
249 return TRUE;
250 } else {
251 return FALSE;
252 }
253 }
254
255 BOOLEAN
256 IsPortResetChange (
257 IN UINT16 PortChangeStatus
258 )
259 /*++
260
261 Routine Description:
262 Tell if there is a Port Reset Change status in that port.
263
264 Arguments:
265 PortChangeStatus - The status value of that port.
266
267 Returns:
268 TRUE
269 FALSE
270
271 --*/
272 {
273 //
274 // return the bit 4 value of PortChangeStatus
275 //
276 if ((PortChangeStatus & USB_PORT_STAT_C_RESET) != 0) {
277 return TRUE;
278 } else {
279 return FALSE;
280 }
281 }
282
283
284 BOOLEAN
285 IsPortSuspendChange (
286 IN UINT16 PortChangeStatus
287 )
288 /*++
289
290 Routine Description:
291 Tell if there is a Suspend Change Status in that port.
292
293 Arguments:
294 PortChangeStatus - The status value of that port.
295
296 Returns:
297 TRUE
298 FALSE
299
300 --*/
301 {
302 //
303 // return the bit 2 value of PortChangeStatus
304 //
305 if ((PortChangeStatus & USB_PORT_STAT_C_SUSPEND) != 0) {
306 return TRUE;
307 } else {
308 return FALSE;
309 }
310 }
311
312
313 INTERFACE_DESC_LIST_ENTRY*
314 FindInterfaceListEntry (
315 IN EFI_USB_IO_PROTOCOL *This
316 )
317 /*++
318
319 Routine Description:
320 Find Interface ListEntry.
321
322 Arguments:
323 This - EFI_USB_IO_PROTOCOL
324
325 Returns:
326 INTERFACE_DESC_LIST_ENTRY pointer
327
328 --*/
329 {
330 USB_IO_CONTROLLER_DEVICE *UsbIoController;
331 USB_IO_DEVICE *UsbIoDev;
332 LIST_ENTRY *InterfaceListHead;
333 INTERFACE_DESC_LIST_ENTRY *InterfaceListEntry;
334
335 UsbIoController = USB_IO_CONTROLLER_DEVICE_FROM_USB_IO_THIS (This);
336 UsbIoDev = UsbIoController->UsbDevice;
337
338 if (!UsbIoDev->IsConfigured) {
339 return NULL;
340 }
341
342 InterfaceListHead = &UsbIoDev->ActiveConfig->InterfaceDescListHead;
343 InterfaceListEntry = (INTERFACE_DESC_LIST_ENTRY *) (InterfaceListHead->ForwardLink);
344
345 //
346 // Loop all interface descriptor to get match one.
347 //
348 while (InterfaceListEntry != (INTERFACE_DESC_LIST_ENTRY *) InterfaceListHead) {
349 if (InterfaceListEntry->InterfaceDescriptor.InterfaceNumber == UsbIoController->InterfaceNumber) {
350 return InterfaceListEntry;
351 }
352
353 InterfaceListEntry = (INTERFACE_DESC_LIST_ENTRY *) InterfaceListEntry->Link.ForwardLink;
354 }
355
356 return NULL;
357 }
358
359 ENDPOINT_DESC_LIST_ENTRY*
360 FindEndPointListEntry (
361 IN EFI_USB_IO_PROTOCOL *This,
362 IN UINT8 EndPointAddress
363 )
364 /*++
365
366 Routine Description:
367 Find EndPoint ListEntry.
368
369 Arguments:
370 This - EFI_USB_IO_PROTOCOL
371 EndpointAddr - Endpoint address.
372
373 Returns:
374 ENDPOINT_DESC_LIST_ENTRY pointer
375
376 --*/
377 {
378 INTERFACE_DESC_LIST_ENTRY *InterfaceListEntry;
379 LIST_ENTRY *EndpointDescListHead;
380 ENDPOINT_DESC_LIST_ENTRY *EndPointListEntry;
381
382 InterfaceListEntry = FindInterfaceListEntry (This);
383 if (InterfaceListEntry != NULL) {
384 EndpointDescListHead = &InterfaceListEntry->EndpointDescListHead;
385 EndPointListEntry = (ENDPOINT_DESC_LIST_ENTRY *) (EndpointDescListHead->ForwardLink);
386
387 //
388 // Loop all interface descriptor to get match one.
389 //
390 while (EndPointListEntry != (ENDPOINT_DESC_LIST_ENTRY *) EndpointDescListHead) {
391 if (EndPointListEntry->EndpointDescriptor.EndpointAddress == EndPointAddress) {
392 return EndPointListEntry;
393 }
394
395 EndPointListEntry = (ENDPOINT_DESC_LIST_ENTRY *) EndPointListEntry->Link.ForwardLink;
396 }
397 }
398
399 return NULL;
400 }
401
402 VOID
403 GetDataToggleBit (
404 IN EFI_USB_IO_PROTOCOL *UsbIo,
405 IN UINT8 EndpointAddr,
406 OUT UINT8 *DataToggle
407 )
408 /*++
409
410 Routine Description:
411 Get the datatoggle of a specified endpoint.
412
413 Arguments:
414 UsbIo - Given Usb Controller device.
415 EndpointAddr - Given Endpoint address.
416 DataToggle - The current data toggle of that endpoint
417
418 Returns:
419 N/A
420
421 --*/
422 {
423
424 ENDPOINT_DESC_LIST_ENTRY *EndpointListEntry;
425
426 *DataToggle = 0;
427
428 EndpointListEntry = FindEndPointListEntry (UsbIo, EndpointAddr);
429 if (EndpointListEntry == NULL) {
430 return ;
431 }
432
433 *DataToggle = (UINT8) (EndpointListEntry->Toggle);
434 return ;
435 }
436
437 VOID
438 SetDataToggleBit (
439 IN EFI_USB_IO_PROTOCOL *UsbIo,
440 IN UINT8 EndpointAddr,
441 IN UINT8 DataToggle
442 )
443 /*++
444
445 Routine Description:
446 Set the datatoggle of a specified endpoint
447
448 Arguments:
449 UsbIo - Given Usb Controller device.
450 EndpointAddr - Given Endpoint address.
451 DataToggle - The current data toggle of that endpoint to be set
452
453 Returns:
454 N/A
455
456 --*/
457 {
458
459 ENDPOINT_DESC_LIST_ENTRY *EndpointListEntry;
460
461 EndpointListEntry = FindEndPointListEntry (UsbIo, EndpointAddr);
462 if (EndpointListEntry == NULL) {
463 return ;
464 }
465
466 EndpointListEntry->Toggle = DataToggle;
467 return ;
468 }
469
470 VOID
471 GetDeviceEndPointMaxPacketLength (
472 IN EFI_USB_IO_PROTOCOL *UsbIo,
473 IN UINT8 EndpointAddr,
474 OUT UINT8 *MaxPacketLength
475 )
476 /*++
477
478 Routine Description:
479 Get the Max Packet Length of the speified Endpoint.
480
481 Arguments:
482 UsbIo - Given Usb Controller device.
483 EndpointAddr - Given Endpoint address.
484 MaxPacketLength - The max packet length of that endpoint
485
486 Returns:
487 N/A
488
489 --*/
490 {
491
492 ENDPOINT_DESC_LIST_ENTRY *EndpointListEntry;
493
494 *MaxPacketLength = 0;
495
496 EndpointListEntry = FindEndPointListEntry (UsbIo, EndpointAddr);
497 if (EndpointListEntry == NULL) {
498 return ;
499 }
500
501 *MaxPacketLength = (UINT8) (EndpointListEntry->EndpointDescriptor.MaxPacketSize);
502
503 return ;
504 }
505
506
507 EFI_STATUS
508 UsbSetDeviceAddress (
509 IN EFI_USB_IO_PROTOCOL *UsbIo,
510 IN UINT16 AddressValue,
511 OUT UINT32 *Status
512 )
513 /*++
514
515 Routine Description:
516
517 Usb Set Device Address
518
519 Arguments:
520
521 UsbIo - EFI_USB_IO_PROTOCOL
522 AddressValue - Device address
523 Status - Transfer status
524
525 Returns:
526
527 EFI_INVALID_PARAMETER - Parameter is error
528 EFI_SUCCESS - Success
529 EFI_TIMEOUT - Device has no response
530
531
532 --*/
533 {
534 EFI_USB_DEVICE_REQUEST DevReq;
535
536 if (UsbIo == NULL) {
537 return EFI_INVALID_PARAMETER;
538 }
539
540 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
541
542 DevReq.RequestType = USB_DEV_SET_ADDRESS_REQ_TYPE;
543 DevReq.Request = USB_DEV_SET_ADDRESS;
544 DevReq.Value = AddressValue;
545
546 return UsbIo->UsbControlTransfer (
547 UsbIo,
548 &DevReq,
549 EfiUsbNoData,
550 TIMEOUT_VALUE,
551 NULL,
552 0,
553 Status
554 );
555 }
556