1 /** @file
2 Common Libarary for PEI USB
3 
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved. <BR>
5 
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution.  The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11 
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 #ifndef _PEI_USB_LIB_H_
18 #define _PEI_USB_LIB_H_
19 
20 
21 //
22 // Standard device request and request type
23 // By [Spec-USB20/Chapter-9.4]
24 //
25 #define USB_DEV_GET_STATUS                  0x00
26 #define USB_DEV_GET_STATUS_REQ_TYPE_D       0x80 // Receiver : Device
27 #define USB_DEV_GET_STATUS_REQ_TYPE_I       0x81 // Receiver : Interface
28 #define USB_DEV_GET_STATUS_REQ_TYPE_E       0x82 // Receiver : Endpoint
29 
30 #define USB_DEV_CLEAR_FEATURE               0x01
31 #define USB_DEV_CLEAR_FEATURE_REQ_TYPE_D    0x00 // Receiver : Device
32 #define USB_DEV_CLEAR_FEATURE_REQ_TYPE_I    0x01 // Receiver : Interface
33 #define USB_DEV_CLEAR_FEATURE_REQ_TYPE_E    0x02 // Receiver : Endpoint
34 
35 #define USB_DEV_SET_FEATURE                 0x03
36 #define USB_DEV_SET_FEATURE_REQ_TYPE_D      0x00 // Receiver : Device
37 #define USB_DEV_SET_FEATURE_REQ_TYPE_I      0x01 // Receiver : Interface
38 #define USB_DEV_SET_FEATURE_REQ_TYPE_E      0x02 // Receiver : Endpoint
39 
40 #define USB_DEV_SET_ADDRESS                 0x05
41 #define USB_DEV_SET_ADDRESS_REQ_TYPE        0x00
42 
43 #define USB_DEV_GET_DESCRIPTOR              0x06
44 #define USB_DEV_GET_DESCRIPTOR_REQ_TYPE     0x80
45 
46 #define USB_DEV_SET_DESCRIPTOR              0x07
47 #define USB_DEV_SET_DESCRIPTOR_REQ_TYPE     0x00
48 
49 #define USB_DEV_GET_CONFIGURATION           0x08
50 #define USB_DEV_GET_CONFIGURATION_REQ_TYPE  0x80
51 
52 #define USB_DEV_SET_CONFIGURATION           0x09
53 #define USB_DEV_SET_CONFIGURATION_REQ_TYPE  0x00
54 
55 #define USB_DEV_GET_INTERFACE               0x0A
56 #define USB_DEV_GET_INTERFACE_REQ_TYPE      0x81
57 
58 #define USB_DEV_SET_INTERFACE               0x0B
59 #define USB_DEV_SET_INTERFACE_REQ_TYPE      0x01
60 
61 #define USB_DEV_SYNCH_FRAME                 0x0C
62 #define USB_DEV_SYNCH_FRAME_REQ_TYPE        0x82
63 
64 //
65 // USB Descriptor types
66 //
67 #define USB_DT_DEVICE     0x01
68 #define USB_DT_CONFIG     0x02
69 #define USB_DT_STRING     0x03
70 #define USB_DT_INTERFACE  0x04
71 #define USB_DT_ENDPOINT   0x05
72 #define USB_DT_HUB        0x29
73 #define USB_DT_SUPERSPEED_HUB 0x2A
74 #define USB_DT_HID        0x21
75 
76 //
77 // USB request type
78 //
79 #define USB_TYPE_STANDARD (0x00 << 5)
80 #define USB_TYPE_CLASS    (0x01 << 5)
81 #define USB_TYPE_VENDOR   (0x02 << 5)
82 #define USB_TYPE_RESERVED (0x03 << 5)
83 
84 //
85 // USB request targer device
86 //
87 #define USB_RECIP_DEVICE    0x00
88 #define USB_RECIP_INTERFACE 0x01
89 #define USB_RECIP_ENDPOINT  0x02
90 #define USB_RECIP_OTHER     0x03
91 
92 typedef enum {
93   EfiUsbEndpointHalt,
94   EfiUsbDeviceRemoteWakeup
95 } EFI_USB_STANDARD_FEATURE_SELECTOR;
96 
97 //
98 // Usb Data recipient type
99 //
100 typedef enum {
101   EfiUsbDevice,
102   EfiUsbInterface,
103   EfiUsbEndpoint
104 } EFI_USB_RECIPIENT;
105 
106 /**
107   Get a given usb descriptor.
108 
109   @param  PeiServices        General-purpose services that are available to every PEIM.
110   @param  UsbIoPpi           Indicates the PEI_USB_IO_PPI instance.
111   @param  Value              Request Value.
112   @param  Index              Request Index.
113   @param  DescriptorLength   Request descriptor Length.
114   @param  Descriptor         Request descriptor.
115 
116 
117   @retval EFI_SUCCESS       Usb descriptor is obtained successfully.
118   @retval EFI_DEVICE_ERROR  Cannot get the usb descriptor due to a hardware error.
119   @retval Others            Other failure occurs.
120 
121 **/
122 EFI_STATUS
123 PeiUsbGetDescriptor (
124   IN  EFI_PEI_SERVICES         **PeiServices,
125   IN  PEI_USB_IO_PPI           *UsbIoPpi,
126   IN  UINT16                   Value,
127   IN  UINT16                   Index,
128   IN  UINT16                   DescriptorLength,
129   OUT VOID                     *Descriptor
130   );
131 
132 /**
133   Set a usb device with a specified address.
134 
135   @param  PeiServices        General-purpose services that are available to every PEIM.
136   @param  UsbIoPpi           Indicates the PEI_USB_IO_PPI instance.
137   @param  AddressValue       The address to assign.
138 
139   @retval EFI_SUCCESS        Usb device address is set successfully.
140   @retval EFI_DEVICE_ERROR   Cannot set the usb address due to a hardware error.
141   @retval Others             Other failure occurs.
142 
143 **/
144 EFI_STATUS
145 PeiUsbSetDeviceAddress (
146   IN EFI_PEI_SERVICES         **PeiServices,
147   IN PEI_USB_IO_PPI           *UsbIoPpi,
148   IN UINT16                   AddressValue
149   );
150 
151 /**
152   Clear a given usb feature.
153 
154   @param  PeiServices       General-purpose services that are available to every PEIM.
155   @param  UsbIoPpi          Indicates the PEI_USB_IO_PPI instance.
156   @param  Recipient         The recipient of ClearFeature Request, should be one of Device/Interface/Endpoint.
157   @param  Value             Request Value.
158   @param  Target            Request Index.
159 
160   @retval EFI_SUCCESS       Usb feature is cleared successfully.
161   @retval EFI_DEVICE_ERROR  Cannot clear the usb feature due to a hardware error.
162   @retval Others            Other failure occurs.
163 
164 **/
165 EFI_STATUS
166 PeiUsbClearDeviceFeature (
167   IN EFI_PEI_SERVICES         **PeiServices,
168   IN PEI_USB_IO_PPI           *UsbIoPpi,
169   IN EFI_USB_RECIPIENT        Recipient,
170   IN UINT16                   Value,
171   IN UINT16                   Target
172   );
173 
174 /**
175   Configure a usb device to Configuration 1.
176 
177   @param  PeiServices        General-purpose services that are available to every PEIM.
178   @param  UsbIoPpi           Indicates the PEI_USB_IO_PPI instance.
179 
180   @retval EFI_SUCCESS        Usb device is set to use Configuration 1 successfully.
181   @retval EFI_DEVICE_ERROR   Cannot set the usb device due to a hardware error.
182   @retval Others             Other failure occurs.
183 
184 **/
185 EFI_STATUS
186 PeiUsbSetConfiguration (
187   IN EFI_PEI_SERVICES         **PeiServices,
188   IN PEI_USB_IO_PPI           *UsbIoPpi
189   );
190 
191 /**
192   Judge if the port is connected with a usb device or not.
193 
194   @param  PortStatus  The usb port status gotten.
195 
196   @retval TRUE        A usb device is connected with the port.
197   @retval FALSE       No usb device is connected with the port.
198 
199 **/
200 BOOLEAN
201 IsPortConnect (
202   IN UINT16  PortStatus
203   );
204 
205 /**
206   Get device speed according to port status.
207 
208   @param    PortStatus  The usb port status gotten.
209 
210   @return   Device speed value.
211 
212 **/
213 UINTN
214 PeiUsbGetDeviceSpeed (
215   IN UINT16 PortStatus
216   );
217 
218 /**
219   Judge if the port is in "connection change" status or not.
220 
221   @param  PortChangeStatus  The usb port change status gotten.
222 
223   @retval TRUE              The port is in "connection change" status.
224   @retval FALSE             The port is NOT in "connection change" status.
225 
226 **/
227 BOOLEAN
228 IsPortConnectChange (
229   IN UINT16  PortChangeStatus
230   );
231 #endif
232