]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Bus/Usb/UsbBus/Dxe/usbutil.c
fix a typo in a comment
[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 Arguments:
39 PortStatus - The status value of that port.
40
41 Returns:
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 BOOLEAN
284 IsPortSuspendChange (
285 IN UINT16 PortChangeStatus
286 )
287 /*++
288
289 Routine Description:
290 Tell if there is a Suspend Change Status in that port.
291
292 Arguments:
293 PortChangeStatus - The status value of that port.
294
295 Returns:
296 TRUE
297 FALSE
298
299 --*/
300 {
301 //
302 // return the bit 2 value of PortChangeStatus
303 //
304 if ((PortChangeStatus & USB_PORT_STAT_C_SUSPEND) != 0) {
305 return TRUE;
306 } else {
307 return FALSE;
308 }
309 }
310
311 INTERFACE_DESC_LIST_ENTRY *
312 FindInterfaceListEntry (
313 IN EFI_USB_IO_PROTOCOL *This
314 )
315 /*++
316
317 Routine Description:
318 Find Interface ListEntry.
319
320 Arguments:
321 This - EFI_USB_IO_PROTOCOL
322
323 Returns:
324 INTERFACE_DESC_LIST_ENTRY pointer
325
326 --*/
327 {
328 USB_IO_CONTROLLER_DEVICE *UsbIoController;
329 USB_IO_DEVICE *UsbIoDev;
330 LIST_ENTRY *InterfaceListHead;
331 INTERFACE_DESC_LIST_ENTRY *InterfaceListEntry;
332
333 UsbIoController = USB_IO_CONTROLLER_DEVICE_FROM_USB_IO_THIS (This);
334 UsbIoDev = UsbIoController->UsbDevice;
335
336 if (!UsbIoDev->IsConfigured) {
337 return NULL;
338 }
339
340 InterfaceListHead = &UsbIoDev->ActiveConfig->InterfaceDescListHead;
341 InterfaceListEntry = (INTERFACE_DESC_LIST_ENTRY *) (InterfaceListHead->ForwardLink);
342
343 //
344 // Loop all interface descriptor to get match one.
345 //
346 while (InterfaceListEntry != (INTERFACE_DESC_LIST_ENTRY *) InterfaceListHead) {
347 if (InterfaceListEntry->InterfaceDescriptor.InterfaceNumber == UsbIoController->InterfaceNumber) {
348 return InterfaceListEntry;
349 }
350
351 InterfaceListEntry = (INTERFACE_DESC_LIST_ENTRY *) InterfaceListEntry->Link.ForwardLink;
352 }
353
354 return NULL;
355 }
356
357 ENDPOINT_DESC_LIST_ENTRY*
358 FindEndPointListEntry (
359 IN EFI_USB_IO_PROTOCOL *This,
360 IN UINT8 EndPointAddress
361 )
362 /*++
363
364 Routine Description:
365 Find EndPoint ListEntry.
366
367 Arguments:
368 This - EFI_USB_IO_PROTOCOL
369 EndPointAddress - Endpoint address.
370
371 Returns:
372 ENDPOINT_DESC_LIST_ENTRY pointer
373
374 --*/
375 {
376 INTERFACE_DESC_LIST_ENTRY *InterfaceListEntry;
377 LIST_ENTRY *EndpointDescListHead;
378 ENDPOINT_DESC_LIST_ENTRY *EndPointListEntry;
379
380 InterfaceListEntry = FindInterfaceListEntry (This);
381 if (InterfaceListEntry != NULL) {
382 EndpointDescListHead = &InterfaceListEntry->EndpointDescListHead;
383 EndPointListEntry = (ENDPOINT_DESC_LIST_ENTRY *) (EndpointDescListHead->ForwardLink);
384
385 //
386 // Loop all interface descriptor to get match one.
387 //
388 while (EndPointListEntry != (ENDPOINT_DESC_LIST_ENTRY *) EndpointDescListHead) {
389 if (EndPointListEntry->EndpointDescriptor.EndpointAddress == EndPointAddress) {
390 return EndPointListEntry;
391 }
392
393 EndPointListEntry = (ENDPOINT_DESC_LIST_ENTRY *) EndPointListEntry->Link.ForwardLink;
394 }
395 }
396
397 return NULL;
398 }
399
400 VOID
401 GetDataToggleBit (
402 IN EFI_USB_IO_PROTOCOL *UsbIo,
403 IN UINT8 EndpointAddr,
404 OUT UINT8 *DataToggle
405 )
406 /*++
407
408 Routine Description:
409 Get the datatoggle of a specified endpoint.
410
411 Arguments:
412 UsbIo - Given Usb Controller device.
413 EndpointAddr - Given Endpoint address.
414 DataToggle - The current data toggle of that endpoint
415
416 Returns:
417 N/A
418
419 --*/
420 {
421
422 ENDPOINT_DESC_LIST_ENTRY *EndpointListEntry;
423
424 *DataToggle = 0;
425
426 EndpointListEntry = FindEndPointListEntry (UsbIo, EndpointAddr);
427 if (EndpointListEntry == NULL) {
428 return ;
429 }
430
431 *DataToggle = (UINT8) (EndpointListEntry->Toggle);
432 return ;
433 }
434
435 VOID
436 SetDataToggleBit (
437 IN EFI_USB_IO_PROTOCOL *UsbIo,
438 IN UINT8 EndpointAddr,
439 IN UINT8 DataToggle
440 )
441 /*++
442
443 Routine Description:
444 Set the datatoggle of a specified endpoint
445
446 Arguments:
447 UsbIo - Given Usb Controller device.
448 EndpointAddr - Given Endpoint address.
449 DataToggle - The current data toggle of that endpoint to be set
450
451 Returns:
452 N/A
453
454 --*/
455 {
456
457 ENDPOINT_DESC_LIST_ENTRY *EndpointListEntry;
458
459 EndpointListEntry = FindEndPointListEntry (UsbIo, EndpointAddr);
460 if (EndpointListEntry == NULL) {
461 return ;
462 }
463
464 EndpointListEntry->Toggle = DataToggle;
465 return ;
466 }
467
468 VOID
469 GetDeviceEndPointMaxPacketLength (
470 IN EFI_USB_IO_PROTOCOL *UsbIo,
471 IN UINT8 EndpointAddr,
472 OUT UINTN *MaxPacketLength
473 )
474 /*++
475
476 Routine Description:
477 Get the Max Packet Length of the speified Endpoint.
478
479 Arguments:
480 UsbIo - Given Usb Controller device.
481 EndpointAddr - Given Endpoint address.
482 MaxPacketLength - The max packet length of that endpoint
483
484 Returns:
485 N/A
486
487 --*/
488 {
489
490 ENDPOINT_DESC_LIST_ENTRY *EndpointListEntry;
491
492 *MaxPacketLength = 0;
493
494 EndpointListEntry = FindEndPointListEntry (UsbIo, EndpointAddr);
495 if (EndpointListEntry == NULL) {
496 return ;
497 }
498
499 *MaxPacketLength = (UINTN) (EndpointListEntry->EndpointDescriptor.MaxPacketSize);
500
501 return ;
502 }
503
504
505 EFI_STATUS
506 UsbSetDeviceAddress (
507 IN EFI_USB_IO_PROTOCOL *UsbIo,
508 IN UINT16 AddressValue,
509 OUT UINT32 *Status
510 )
511 /*++
512
513 Routine Description:
514
515 Usb Set Device Address
516
517 Arguments:
518
519 UsbIo - EFI_USB_IO_PROTOCOL
520 AddressValue - Device address
521 Status - Transfer status
522
523 Returns:
524
525 EFI_INVALID_PARAMETER - Parameter is error
526 EFI_SUCCESS - Success
527 EFI_TIMEOUT - Device has no response
528
529
530 --*/
531 {
532 EFI_USB_DEVICE_REQUEST DevReq;
533
534 if (UsbIo == NULL) {
535 return EFI_INVALID_PARAMETER;
536 }
537
538 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
539
540 DevReq.RequestType = USB_DEV_SET_ADDRESS_REQ_TYPE;
541 DevReq.Request = USB_DEV_SET_ADDRESS;
542 DevReq.Value = AddressValue;
543
544 return UsbIo->UsbControlTransfer (
545 UsbIo,
546 &DevReq,
547 EfiUsbNoData,
548 TIMEOUT_VALUE,
549 NULL,
550 0,
551 Status
552 );
553 }
554