Coverage Report

Created: 2025-12-09 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/edk2/MdeModulePkg/Bus/Usb/UsbBusPei/UsbIoPeim.c
Line
Count
Source
1
/** @file
2
The module is used to implement Usb Io PPI interfaces.
3
4
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. <BR>
5
6
SPDX-License-Identifier: BSD-2-Clause-Patent
7
8
**/
9
10
#include "UsbPeim.h"
11
#include "PeiUsbLib.h"
12
13
/**
14
  Submits control transfer to a target USB device.
15
16
  @param  PeiServices            The pointer of EFI_PEI_SERVICES.
17
  @param  This                   The pointer of PEI_USB_IO_PPI.
18
  @param  Request                USB device request to send.
19
  @param  Direction              Specifies the data direction for the data stage.
20
  @param  Timeout                Indicates the maximum timeout, in millisecond. If Timeout
21
                                 is 0, then the caller must wait for the function to be
22
                                 completed until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.
23
  @param  Data                   Data buffer to be transmitted or received from USB device.
24
  @param  DataLength             The size (in bytes) of the data buffer.
25
26
  @retval EFI_SUCCESS            Transfer was completed successfully.
27
  @retval EFI_OUT_OF_RESOURCES   The transfer failed due to lack of resources.
28
  @retval EFI_INVALID_PARAMETER  Some parameters are invalid.
29
  @retval EFI_TIMEOUT            Transfer failed due to timeout.
30
  @retval EFI_DEVICE_ERROR       Transfer failed due to host controller or device error.
31
32
**/
33
EFI_STATUS
34
EFIAPI
35
PeiUsbControlTransfer (
36
  IN     EFI_PEI_SERVICES        **PeiServices,
37
  IN     PEI_USB_IO_PPI          *This,
38
  IN     EFI_USB_DEVICE_REQUEST  *Request,
39
  IN     EFI_USB_DATA_DIRECTION  Direction,
40
  IN     UINT32                  Timeout,
41
  IN OUT VOID                    *Data       OPTIONAL,
42
  IN     UINTN                   DataLength  OPTIONAL
43
  )
44
0
{
45
0
  EFI_STATUS                   Status;
46
0
  PEI_USB_DEVICE               *PeiUsbDev;
47
0
  UINT32                       TransferResult;
48
0
  EFI_USB_ENDPOINT_DESCRIPTOR  *EndpointDescriptor;
49
0
  UINT8                        EndpointIndex;
50
51
0
  PeiUsbDev = PEI_USB_DEVICE_FROM_THIS (This);
52
53
0
  EndpointDescriptor = NULL;
54
0
  EndpointIndex      = 0;
55
56
0
  if ((Request->Request     == USB_REQ_CLEAR_FEATURE) &&
57
0
      (Request->RequestType == USB_DEV_CLEAR_FEATURE_REQ_TYPE_E) &&
58
0
      (Request->Value       == USB_FEATURE_ENDPOINT_HALT))
59
0
  {
60
    //
61
    // Request->Index is the Endpoint Address, use it to get the Endpoint Index.
62
    //
63
0
    while (EndpointIndex < MAX_ENDPOINT) {
64
0
      Status = PeiUsbGetEndpointDescriptor (PeiServices, This, EndpointIndex, &EndpointDescriptor);
65
0
      if (EFI_ERROR (Status)) {
66
0
        return EFI_INVALID_PARAMETER;
67
0
      }
68
69
0
      if (EndpointDescriptor->EndpointAddress == Request->Index) {
70
0
        break;
71
0
      }
72
73
0
      EndpointIndex++;
74
0
    }
75
76
0
    if (EndpointIndex == MAX_ENDPOINT) {
77
0
      return EFI_INVALID_PARAMETER;
78
0
    }
79
0
  }
80
81
0
  Status = PeiUsbDev->Usb2HcPpi->ControlTransfer (
82
0
                                   PeiServices,
83
0
                                   PeiUsbDev->Usb2HcPpi,
84
0
                                   PeiUsbDev->DeviceAddress,
85
0
                                   PeiUsbDev->DeviceSpeed,
86
0
                                   PeiUsbDev->MaxPacketSize0,
87
0
                                   Request,
88
0
                                   Direction,
89
0
                                   Data,
90
0
                                   &DataLength,
91
0
                                   Timeout,
92
0
                                   &(PeiUsbDev->Translator),
93
0
                                   &TransferResult
94
0
                                   );
95
96
  //
97
  // Reset the endpoint toggle when endpoint stall is cleared
98
  //
99
0
  if ((Request->Request     == USB_REQ_CLEAR_FEATURE) &&
100
0
      (Request->RequestType == USB_DEV_CLEAR_FEATURE_REQ_TYPE_E) &&
101
0
      (Request->Value       == USB_FEATURE_ENDPOINT_HALT))
102
0
  {
103
0
    if ((PeiUsbDev->DataToggle & (1 << EndpointIndex)) != 0) {
104
0
      PeiUsbDev->DataToggle = (UINT16)(PeiUsbDev->DataToggle ^ (1 << EndpointIndex));
105
0
    }
106
0
  }
107
108
0
  DEBUG ((DEBUG_INFO, "PeiUsbControlTransfer: %r\n", Status));
109
0
  return Status;
110
0
}
111
112
/**
113
  Submits bulk transfer to a bulk endpoint of a USB device.
114
115
  @param  PeiServices           The pointer of EFI_PEI_SERVICES.
116
  @param  This                  The pointer of PEI_USB_IO_PPI.
117
  @param  DeviceEndpoint        Endpoint number and its direction in bit 7.
118
  @param  Data                  A pointer to the buffer of data to transmit
119
                                from or receive into.
120
  @param  DataLength            The length of the data buffer.
121
  @param  Timeout               Indicates the maximum time, in millisecond, which the
122
                                transfer is allowed to complete. If Timeout is 0, then
123
                                the caller must wait for the function to be completed
124
                                until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.
125
126
  @retval EFI_SUCCESS           The transfer was completed successfully.
127
  @retval EFI_OUT_OF_RESOURCES  The transfer failed due to lack of resource.
128
  @retval EFI_INVALID_PARAMETER Parameters are invalid.
129
  @retval EFI_TIMEOUT           The transfer failed due to timeout.
130
  @retval EFI_DEVICE_ERROR      The transfer failed due to host controller error.
131
132
**/
133
EFI_STATUS
134
EFIAPI
135
PeiUsbBulkTransfer (
136
  IN     EFI_PEI_SERVICES  **PeiServices,
137
  IN     PEI_USB_IO_PPI    *This,
138
  IN     UINT8             DeviceEndpoint,
139
  IN OUT VOID              *Data,
140
  IN OUT UINTN             *DataLength,
141
  IN     UINTN             Timeout
142
  )
143
0
{
144
0
  EFI_STATUS                   Status;
145
0
  PEI_USB_DEVICE               *PeiUsbDev;
146
0
  UINT32                       TransferResult;
147
0
  UINTN                        MaxPacketLength;
148
0
  UINT8                        DataToggle;
149
0
  UINT8                        OldToggle;
150
0
  EFI_USB_ENDPOINT_DESCRIPTOR  *EndpointDescriptor;
151
0
  UINT8                        EndpointIndex;
152
0
  VOID                         *Data2[EFI_USB_MAX_BULK_BUFFER_NUM];
153
154
0
  PeiUsbDev = PEI_USB_DEVICE_FROM_THIS (This);
155
156
0
  EndpointDescriptor = NULL;
157
0
  EndpointIndex      = 0;
158
0
  Data2[0]           = Data;
159
0
  Data2[1]           = NULL;
160
161
0
  while (EndpointIndex < MAX_ENDPOINT) {
162
0
    Status = PeiUsbGetEndpointDescriptor (PeiServices, This, EndpointIndex, &EndpointDescriptor);
163
0
    if (EFI_ERROR (Status)) {
164
0
      return EFI_INVALID_PARAMETER;
165
0
    }
166
167
0
    if (EndpointDescriptor->EndpointAddress == DeviceEndpoint) {
168
0
      break;
169
0
    }
170
171
0
    EndpointIndex++;
172
0
  }
173
174
0
  if (EndpointIndex == MAX_ENDPOINT) {
175
0
    return EFI_INVALID_PARAMETER;
176
0
  }
177
178
0
  MaxPacketLength = PeiUsbDev->EndpointDesc[EndpointIndex]->MaxPacketSize;
179
0
  if ((PeiUsbDev->DataToggle & (1 << EndpointIndex)) != 0) {
180
0
    DataToggle = 1;
181
0
  } else {
182
0
    DataToggle = 0;
183
0
  }
184
185
0
  OldToggle = DataToggle;
186
187
0
  Status = PeiUsbDev->Usb2HcPpi->BulkTransfer (
188
0
                                   PeiServices,
189
0
                                   PeiUsbDev->Usb2HcPpi,
190
0
                                   PeiUsbDev->DeviceAddress,
191
0
                                   DeviceEndpoint,
192
0
                                   PeiUsbDev->DeviceSpeed,
193
0
                                   MaxPacketLength,
194
0
                                   Data2,
195
0
                                   DataLength,
196
0
                                   &DataToggle,
197
0
                                   Timeout,
198
0
                                   &(PeiUsbDev->Translator),
199
0
                                   &TransferResult
200
0
                                   );
201
202
0
  if (OldToggle != DataToggle) {
203
0
    PeiUsbDev->DataToggle = (UINT16)(PeiUsbDev->DataToggle ^ (1 << EndpointIndex));
204
0
  }
205
206
0
  DEBUG ((DEBUG_INFO, "PeiUsbBulkTransfer: %r\n", Status));
207
0
  return Status;
208
0
}
209
210
/**
211
  Get the usb interface descriptor.
212
213
  @param  PeiServices          General-purpose services that are available to every PEIM.
214
  @param  This                 Indicates the PEI_USB_IO_PPI instance.
215
  @param  InterfaceDescriptor  Request interface descriptor.
216
217
218
  @retval EFI_SUCCESS          Usb interface descriptor is obtained successfully.
219
220
**/
221
EFI_STATUS
222
EFIAPI
223
PeiUsbGetInterfaceDescriptor (
224
  IN  EFI_PEI_SERVICES              **PeiServices,
225
  IN  PEI_USB_IO_PPI                *This,
226
  OUT EFI_USB_INTERFACE_DESCRIPTOR  **InterfaceDescriptor
227
  )
228
0
{
229
0
  PEI_USB_DEVICE  *PeiUsbDev;
230
231
0
  PeiUsbDev            = PEI_USB_DEVICE_FROM_THIS (This);
232
0
  *InterfaceDescriptor = PeiUsbDev->InterfaceDesc;
233
0
  return EFI_SUCCESS;
234
0
}
235
236
/**
237
  Get the usb endpoint descriptor.
238
239
  @param  PeiServices          General-purpose services that are available to every PEIM.
240
  @param  This                 Indicates the PEI_USB_IO_PPI instance.
241
  @param  EndpointIndex        The valid index of the specified endpoint.
242
  @param  EndpointDescriptor   Request endpoint descriptor.
243
244
  @retval EFI_SUCCESS       Usb endpoint descriptor is obtained successfully.
245
  @retval EFI_NOT_FOUND     Usb endpoint descriptor is NOT found.
246
247
**/
248
EFI_STATUS
249
EFIAPI
250
PeiUsbGetEndpointDescriptor (
251
  IN  EFI_PEI_SERVICES             **PeiServices,
252
  IN  PEI_USB_IO_PPI               *This,
253
  IN  UINT8                        EndpointIndex,
254
  OUT EFI_USB_ENDPOINT_DESCRIPTOR  **EndpointDescriptor
255
  )
256
0
{
257
0
  PEI_USB_DEVICE  *PeiUsbDev;
258
259
0
  PeiUsbDev = PEI_USB_DEVICE_FROM_THIS (This);
260
261
0
  ASSERT (EndpointDescriptor != NULL);
262
263
  //
264
  // The valid range of EndpointIndex is 0..15
265
  // If EndpointIndex is lesser than 15 but larger than the number of interfaces,
266
  // a EFI_NOT_FOUND should be returned
267
  //
268
0
  ASSERT (EndpointIndex <= 15);
269
270
0
  if (EndpointIndex >= PeiUsbDev->InterfaceDesc->NumEndpoints) {
271
0
    return EFI_NOT_FOUND;
272
0
  }
273
274
0
  *EndpointDescriptor = PeiUsbDev->EndpointDesc[EndpointIndex];
275
276
0
  return EFI_SUCCESS;
277
0
}
278
279
/**
280
  Reset the port and re-configure the usb device.
281
282
  @param  PeiServices    General-purpose services that are available to every PEIM.
283
  @param  This           Indicates the PEI_USB_IO_PPI instance.
284
285
  @retval EFI_SUCCESS    Usb device is reset and configured successfully.
286
  @retval Others         Other failure occurs.
287
288
**/
289
EFI_STATUS
290
EFIAPI
291
PeiUsbPortReset (
292
  IN EFI_PEI_SERVICES  **PeiServices,
293
  IN PEI_USB_IO_PPI    *This
294
  )
295
0
{
296
0
  PEI_USB_DEVICE  *PeiUsbDev;
297
0
  EFI_STATUS      Status;
298
0
  UINT8           Address;
299
300
0
  PeiUsbDev = PEI_USB_DEVICE_FROM_THIS (This);
301
302
0
  ResetRootPort (
303
0
    PeiServices,
304
0
    PeiUsbDev->Usb2HcPpi,
305
0
    PeiUsbDev->DeviceAddress,
306
0
    0
307
0
    );
308
309
  //
310
  // Set address
311
  //
312
0
  Address                  = PeiUsbDev->DeviceAddress;
313
0
  PeiUsbDev->DeviceAddress = 0;
314
315
0
  Status = PeiUsbSetDeviceAddress (
316
0
             PeiServices,
317
0
             This,
318
0
             Address
319
0
             );
320
321
0
  if (EFI_ERROR (Status)) {
322
0
    return Status;
323
0
  }
324
325
0
  PeiUsbDev->DeviceAddress = Address;
326
327
  //
328
  // Set default configuration
329
  //
330
0
  Status = PeiUsbSetConfiguration (
331
0
             PeiServices,
332
0
             This
333
0
             );
334
335
0
  return Status;
336
0
}