]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassCbi.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassCbi.c
CommitLineData
e237e7ae 1/** @file\r
d80ed2a7 2 Implementation of the USB mass storage Control/Bulk/Interrupt transport,\r
3 according to USB Mass Storage Class Control/Bulk/Interrupt (CBI) Transport, Revision 1.1.\r
d7db0902 4 Notice: it is being obsoleted by the standard body in favor of the BOT\r
cc5166ff 5 (Bulk-Only Transport).\r
6\r
d1102dba 7Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 8SPDX-License-Identifier: BSD-2-Clause-Patent\r
e237e7ae 9\r
cc5166ff 10**/\r
e237e7ae 11\r
39840c50 12#include "UsbMass.h"\r
e237e7ae 13\r
d80ed2a7 14//\r
15// Definition of USB CBI0 Transport Protocol\r
16//\r
1436aea4 17USB_MASS_TRANSPORT mUsbCbi0Transport = {\r
d80ed2a7 18 USB_MASS_STORE_CBI0,\r
19 UsbCbiInit,\r
20 UsbCbiExecCommand,\r
21 UsbCbiResetDevice,\r
22 NULL,\r
23 UsbCbiCleanUp\r
24};\r
e237e7ae 25\r
d80ed2a7 26//\r
27// Definition of USB CBI1 Transport Protocol\r
28//\r
1436aea4 29USB_MASS_TRANSPORT mUsbCbi1Transport = {\r
d80ed2a7 30 USB_MASS_STORE_CBI1,\r
31 UsbCbiInit,\r
32 UsbCbiExecCommand,\r
33 UsbCbiResetDevice,\r
34 NULL,\r
35 UsbCbiCleanUp\r
36};\r
e237e7ae 37\r
38/**\r
d80ed2a7 39 Initializes USB CBI protocol.\r
40\r
41 This function initializes the USB mass storage class CBI protocol.\r
42 It will save its context which is a USB_CBI_PROTOCOL structure\r
43 in the Context if Context isn't NULL.\r
e237e7ae 44\r
d80ed2a7 45 @param UsbIo The USB I/O Protocol instance\r
46 @param Context The buffer to save the context to\r
e237e7ae 47\r
d80ed2a7 48 @retval EFI_SUCCESS The device is successfully initialized.\r
49 @retval EFI_UNSUPPORTED The transport protocol doesn't support the device.\r
50 @retval Other The USB CBI initialization fails.\r
e237e7ae 51\r
52**/\r
e237e7ae 53EFI_STATUS\r
54UsbCbiInit (\r
1436aea4
MK
55 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
56 OUT VOID **Context OPTIONAL\r
e237e7ae 57 )\r
58{\r
59 USB_CBI_PROTOCOL *UsbCbi;\r
60 EFI_USB_INTERFACE_DESCRIPTOR *Interface;\r
61 EFI_USB_ENDPOINT_DESCRIPTOR EndPoint;\r
62 EFI_STATUS Status;\r
63 UINT8 Index;\r
64\r
65 //\r
d80ed2a7 66 // Allocate the CBI context for USB_CBI_PROTOCOL and 3 endpoint descriptors.\r
e237e7ae 67 //\r
68 UsbCbi = AllocateZeroPool (\r
69 sizeof (USB_CBI_PROTOCOL) + 3 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR)\r
70 );\r
d80ed2a7 71 ASSERT (UsbCbi != NULL);\r
e237e7ae 72\r
73 UsbCbi->UsbIo = UsbIo;\r
74\r
75 //\r
d80ed2a7 76 // Get the interface descriptor and validate that it\r
77 // is a USB Mass Storage CBI interface.\r
e237e7ae 78 //\r
79 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &UsbCbi->Interface);\r
80 if (EFI_ERROR (Status)) {\r
81 goto ON_ERROR;\r
82 }\r
83\r
84 Interface = &UsbCbi->Interface;\r
1436aea4
MK
85 if ( (Interface->InterfaceProtocol != USB_MASS_STORE_CBI0)\r
86 && (Interface->InterfaceProtocol != USB_MASS_STORE_CBI1))\r
87 {\r
e237e7ae 88 Status = EFI_UNSUPPORTED;\r
89 goto ON_ERROR;\r
90 }\r
91\r
92 //\r
93 // Locate and save the bulk-in, bulk-out, and interrupt endpoint\r
94 //\r
95 for (Index = 0; Index < Interface->NumEndpoints; Index++) {\r
96 Status = UsbIo->UsbGetEndpointDescriptor (UsbIo, Index, &EndPoint);\r
97 if (EFI_ERROR (Status)) {\r
98 continue;\r
99 }\r
100\r
101 if (USB_IS_BULK_ENDPOINT (EndPoint.Attributes)) {\r
102 //\r
103 // Use the first Bulk-In and Bulk-Out endpoints\r
104 //\r
105 if (USB_IS_IN_ENDPOINT (EndPoint.EndpointAddress) &&\r
1436aea4
MK
106 (UsbCbi->BulkInEndpoint == NULL))\r
107 {\r
108 UsbCbi->BulkInEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *)(UsbCbi + 1);\r
109 CopyMem (UsbCbi->BulkInEndpoint, &EndPoint, sizeof (EndPoint));\r
e237e7ae 110 }\r
111\r
112 if (USB_IS_OUT_ENDPOINT (EndPoint.EndpointAddress) &&\r
1436aea4
MK
113 (UsbCbi->BulkOutEndpoint == NULL))\r
114 {\r
115 UsbCbi->BulkOutEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *)(UsbCbi + 1) + 1;\r
116 CopyMem (UsbCbi->BulkOutEndpoint, &EndPoint, sizeof (EndPoint));\r
e237e7ae 117 }\r
e237e7ae 118 } else if (USB_IS_INTERRUPT_ENDPOINT (EndPoint.Attributes)) {\r
119 //\r
120 // Use the first interrupt endpoint if it is CBI0\r
121 //\r
122 if ((Interface->InterfaceProtocol == USB_MASS_STORE_CBI0) &&\r
1436aea4
MK
123 (UsbCbi->InterruptEndpoint == NULL))\r
124 {\r
125 UsbCbi->InterruptEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *)(UsbCbi + 1) + 2;\r
126 CopyMem (UsbCbi->InterruptEndpoint, &EndPoint, sizeof (EndPoint));\r
e237e7ae 127 }\r
128 }\r
129 }\r
130\r
d80ed2a7 131 if ((UsbCbi->BulkInEndpoint == NULL) || (UsbCbi->BulkOutEndpoint == NULL)) {\r
132 Status = EFI_UNSUPPORTED;\r
133 goto ON_ERROR;\r
134 }\r
1436aea4 135\r
d80ed2a7 136 if ((Interface->InterfaceProtocol == USB_MASS_STORE_CBI0) && (UsbCbi->InterruptEndpoint == NULL)) {\r
e237e7ae 137 Status = EFI_UNSUPPORTED;\r
138 goto ON_ERROR;\r
139 }\r
140\r
141 if (Context != NULL) {\r
142 *Context = UsbCbi;\r
143 } else {\r
c92e277d 144 FreePool (UsbCbi);\r
e237e7ae 145 }\r
d1102dba 146\r
e237e7ae 147 return EFI_SUCCESS;\r
148\r
149ON_ERROR:\r
c92e277d 150 FreePool (UsbCbi);\r
e237e7ae 151 return Status;\r
152}\r
153\r
e237e7ae 154/**\r
155 Send the command to the device using class specific control transfer.\r
156\r
d80ed2a7 157 This function sends command to the device using class specific control transfer.\r
158 The CBI contains three phases: Command, Data, and Status. This is Command phase.\r
159\r
e237e7ae 160 @param UsbCbi The USB CBI protocol\r
161 @param Cmd The high level command to transfer to device\r
162 @param CmdLen The length of the command\r
163 @param Timeout The time to wait the command to finish\r
164\r
d80ed2a7 165 @retval EFI_SUCCESS The command is sent to the device.\r
e237e7ae 166 @retval Others The command failed to transfer to device\r
167\r
168**/\r
e237e7ae 169EFI_STATUS\r
170UsbCbiSendCommand (\r
1436aea4
MK
171 IN USB_CBI_PROTOCOL *UsbCbi,\r
172 IN UINT8 *Cmd,\r
173 IN UINT8 CmdLen,\r
174 IN UINT32 Timeout\r
e237e7ae 175 )\r
176{\r
177 EFI_USB_DEVICE_REQUEST Request;\r
178 EFI_STATUS Status;\r
179 UINT32 TransStatus;\r
180 UINTN DataLen;\r
181 INTN Retry;\r
182\r
183 //\r
184 // Fill in the device request, CBI use the "Accept Device-Specific\r
d80ed2a7 185 // Cmd" (ADSC) class specific request to send commands.\r
e237e7ae 186 //\r
187 Request.RequestType = 0x21;\r
188 Request.Request = 0;\r
189 Request.Value = 0;\r
190 Request.Index = UsbCbi->Interface.InterfaceNumber;\r
191 Request.Length = CmdLen;\r
192\r
1436aea4
MK
193 Status = EFI_SUCCESS;\r
194 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 195\r
196 for (Retry = 0; Retry < USB_CBI_MAX_RETRY; Retry++) {\r
197 //\r
d80ed2a7 198 // Use USB I/O Protocol to send the command to the device\r
e237e7ae 199 //\r
200 TransStatus = 0;\r
201 DataLen = CmdLen;\r
202\r
203 Status = UsbCbi->UsbIo->UsbControlTransfer (\r
204 UsbCbi->UsbIo,\r
205 &Request,\r
206 EfiUsbDataOut,\r
207 Timeout,\r
208 Cmd,\r
209 DataLen,\r
210 &TransStatus\r
211 );\r
212 //\r
213 // The device can fail the command by STALL the control endpoint.\r
214 // It can delay the command by NAK the data or status stage, this\r
215 // is a "class-specific exemption to the USB specification". Retry\r
216 // if the command is NAKed.\r
217 //\r
218 if (EFI_ERROR (Status) && (TransStatus == EFI_USB_ERR_NAK)) {\r
219 continue;\r
220 }\r
221\r
222 break;\r
223 }\r
224\r
225 return Status;\r
226}\r
227\r
e237e7ae 228/**\r
d80ed2a7 229 Transfer data between the device and host.\r
230\r
231 This function transfers data between the device and host.\r
232 The CBI contains three phases: Command, Data, and Status. This is Data phase.\r
e237e7ae 233\r
234 @param UsbCbi The USB CBI device\r
235 @param DataDir The direction of the data transfer\r
d80ed2a7 236 @param Data The buffer to hold the data for input or output.\r
237 @param TransLen On input, the expected transfer length.\r
238 On output, the length of data actually transferred.\r
239 @param Timeout The time to wait for the command to execute\r
e237e7ae 240\r
d80ed2a7 241 @retval EFI_SUCCESS The data transferred successfully.\r
242 @retval EFI_SUCCESS No data to transfer\r
e237e7ae 243 @retval Others Failed to transfer all the data\r
244\r
245**/\r
e237e7ae 246EFI_STATUS\r
247UsbCbiDataTransfer (\r
1436aea4
MK
248 IN USB_CBI_PROTOCOL *UsbCbi,\r
249 IN EFI_USB_DATA_DIRECTION DataDir,\r
250 IN OUT UINT8 *Data,\r
251 IN OUT UINTN *TransLen,\r
252 IN UINT32 Timeout\r
e237e7ae 253 )\r
254{\r
1436aea4
MK
255 EFI_USB_ENDPOINT_DESCRIPTOR *Endpoint;\r
256 EFI_STATUS Status;\r
257 UINT32 TransStatus;\r
258 UINTN Remain;\r
259 UINTN Increment;\r
260 UINT8 *Next;\r
261 UINTN Retry;\r
e237e7ae 262\r
263 //\r
d80ed2a7 264 // If no data to transfer, just return EFI_SUCCESS.\r
e237e7ae 265 //\r
266 if ((DataDir == EfiUsbNoData) || (*TransLen == 0)) {\r
267 return EFI_SUCCESS;\r
268 }\r
269\r
270 //\r
271 // Select the endpoint then issue the transfer\r
272 //\r
273 if (DataDir == EfiUsbDataIn) {\r
274 Endpoint = UsbCbi->BulkInEndpoint;\r
275 } else {\r
276 Endpoint = UsbCbi->BulkOutEndpoint;\r
277 }\r
278\r
279 Next = Data;\r
280 Remain = *TransLen;\r
281 Retry = 0;\r
282 Status = EFI_SUCCESS;\r
41e8ff27 283 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 284\r
285 //\r
d80ed2a7 286 // Transfer the data with a loop. The length of data transferred once is restricted.\r
e237e7ae 287 //\r
288 while (Remain > 0) {\r
289 TransStatus = 0;\r
290\r
1436aea4 291 if (Remain > (UINTN)USB_CBI_MAX_PACKET_NUM * Endpoint->MaxPacketSize) {\r
e237e7ae 292 Increment = USB_CBI_MAX_PACKET_NUM * Endpoint->MaxPacketSize;\r
293 } else {\r
294 Increment = Remain;\r
295 }\r
296\r
297 Status = UsbCbi->UsbIo->UsbBulkTransfer (\r
298 UsbCbi->UsbIo,\r
299 Endpoint->EndpointAddress,\r
300 Next,\r
301 &Increment,\r
302 Timeout,\r
303 &TransStatus\r
304 );\r
305 if (EFI_ERROR (Status)) {\r
306 if (TransStatus == EFI_USB_ERR_NAK) {\r
307 //\r
308 // The device can NAK the host if either the data/buffer isn't\r
957ca631 309 // available or the command is in-progress.\r
d80ed2a7 310 // If data are partially transferred, we just ignore NAK and continue.\r
311 // If all data have been transferred and status is NAK, then we retry for several times.\r
312 // If retry exceeds the USB_CBI_MAX_RETRY, then return error status.\r
e237e7ae 313 //\r
314 if (Increment == 0) {\r
315 if (++Retry > USB_CBI_MAX_RETRY) {\r
316 goto ON_EXIT;\r
317 }\r
e237e7ae 318 } else {\r
319 Next += Increment;\r
320 Remain -= Increment;\r
321 Retry = 0;\r
322 }\r
323\r
324 continue;\r
325 }\r
326\r
327 //\r
328 // The device can fail the command by STALL the bulk endpoint.\r
329 // Clear the stall if that is the case.\r
330 //\r
331 if (TransStatus == EFI_USB_ERR_STALL) {\r
332 UsbClearEndpointStall (UsbCbi->UsbIo, Endpoint->EndpointAddress);\r
333 }\r
334\r
335 goto ON_EXIT;\r
336 }\r
337\r
1436aea4 338 Next += Increment;\r
e237e7ae 339 Remain -= Increment;\r
340 }\r
341\r
342ON_EXIT:\r
343 *TransLen -= Remain;\r
344 return Status;\r
345}\r
346\r
e237e7ae 347/**\r
d80ed2a7 348 Gets the result of high level command execution from interrupt endpoint.\r
349\r
350 This function returns the USB transfer status, and put the high level\r
351 command execution result in Result.\r
352 The CBI contains three phases: Command, Data, and Status. This is Status phase.\r
e237e7ae 353\r
354 @param UsbCbi The USB CBI protocol\r
d80ed2a7 355 @param Timeout The time to wait for the command to execute\r
356 @param Result The result of the command execution.\r
e237e7ae 357\r
358 @retval EFI_SUCCESS The high level command execution result is\r
359 retrieved in Result.\r
360 @retval Others Failed to retrieve the result.\r
361\r
362**/\r
e237e7ae 363EFI_STATUS\r
364UsbCbiGetStatus (\r
1436aea4
MK
365 IN USB_CBI_PROTOCOL *UsbCbi,\r
366 IN UINT32 Timeout,\r
367 OUT USB_CBI_STATUS *Result\r
e237e7ae 368 )\r
369{\r
1436aea4
MK
370 UINTN Len;\r
371 UINT8 Endpoint;\r
372 EFI_STATUS Status;\r
373 UINT32 TransStatus;\r
374 INTN Retry;\r
e237e7ae 375\r
1436aea4
MK
376 Endpoint = UsbCbi->InterruptEndpoint->EndpointAddress;\r
377 Status = EFI_SUCCESS;\r
378 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 379\r
380 //\r
957ca631 381 // Attempt to the read the result from interrupt endpoint\r
e237e7ae 382 //\r
383 for (Retry = 0; Retry < USB_CBI_MAX_RETRY; Retry++) {\r
384 TransStatus = 0;\r
385 Len = sizeof (USB_CBI_STATUS);\r
386\r
387 Status = UsbCbi->UsbIo->UsbSyncInterruptTransfer (\r
388 UsbCbi->UsbIo,\r
389 Endpoint,\r
390 Result,\r
391 &Len,\r
392 Timeout,\r
393 &TransStatus\r
394 );\r
395 //\r
396 // The CBI can NAK the interrupt endpoint if the command is in-progress.\r
397 //\r
398 if (EFI_ERROR (Status) && (TransStatus == EFI_USB_ERR_NAK)) {\r
399 continue;\r
400 }\r
401\r
402 break;\r
403 }\r
404\r
405 return Status;\r
406}\r
407\r
e237e7ae 408/**\r
cc5166ff 409 Execute USB mass storage command through the CBI0/CBI1 transport protocol.\r
e237e7ae 410\r
d80ed2a7 411 @param Context The USB CBI Protocol.\r
e237e7ae 412 @param Cmd The command to transfer to device\r
413 @param CmdLen The length of the command\r
414 @param DataDir The direction of data transfer\r
415 @param Data The buffer to hold the data\r
416 @param DataLen The length of the buffer\r
c7e39923 417 @param Lun Should be 0, this field for bot only\r
e237e7ae 418 @param Timeout The time to wait\r
419 @param CmdStatus The result of the command execution\r
420\r
d80ed2a7 421 @retval EFI_SUCCESS The command is executed successfully.\r
cc5166ff 422 @retval Other Failed to execute the command\r
e237e7ae 423\r
424**/\r
e237e7ae 425EFI_STATUS\r
426UsbCbiExecCommand (\r
427 IN VOID *Context,\r
428 IN VOID *Cmd,\r
429 IN UINT8 CmdLen,\r
430 IN EFI_USB_DATA_DIRECTION DataDir,\r
431 IN VOID *Data,\r
432 IN UINT32 DataLen,\r
c7e39923 433 IN UINT8 Lun,\r
e237e7ae 434 IN UINT32 Timeout,\r
435 OUT UINT32 *CmdStatus\r
436 )\r
437{\r
1436aea4
MK
438 USB_CBI_PROTOCOL *UsbCbi;\r
439 USB_CBI_STATUS Result;\r
440 EFI_STATUS Status;\r
441 UINTN TransLen;\r
e237e7ae 442\r
1436aea4
MK
443 *CmdStatus = USB_MASS_CMD_SUCCESS;\r
444 UsbCbi = (USB_CBI_PROTOCOL *)Context;\r
e237e7ae 445\r
446 //\r
447 // Send the command to the device. Return immediately if device\r
448 // rejects the command.\r
449 //\r
450 Status = UsbCbiSendCommand (UsbCbi, Cmd, CmdLen, Timeout);\r
451 if (EFI_ERROR (Status)) {\r
1436aea4
MK
452 gBS->Stall (10 * USB_MASS_1_MILLISECOND);\r
453 DEBUG ((DEBUG_ERROR, "UsbCbiExecCommand: UsbCbiSendCommand (%r)\n", Status));\r
e237e7ae 454 return Status;\r
455 }\r
456\r
457 //\r
d80ed2a7 458 // Transfer the data. Return this status if no interrupt endpoint\r
e237e7ae 459 // is used to report the transfer status.\r
460 //\r
1436aea4 461 TransLen = (UINTN)DataLen;\r
e237e7ae 462\r
1436aea4 463 Status = UsbCbiDataTransfer (UsbCbi, DataDir, Data, &TransLen, Timeout);\r
e237e7ae 464 if (UsbCbi->InterruptEndpoint == NULL) {\r
1436aea4 465 DEBUG ((DEBUG_ERROR, "UsbCbiExecCommand: UsbCbiDataTransfer (%r)\n", Status));\r
e237e7ae 466 return Status;\r
467 }\r
468\r
469 //\r
d80ed2a7 470 // Get the status. If it succeeds, interpret the result.\r
e237e7ae 471 //\r
472 Status = UsbCbiGetStatus (UsbCbi, Timeout, &Result);\r
473 if (EFI_ERROR (Status)) {\r
1436aea4 474 DEBUG ((DEBUG_ERROR, "UsbCbiExecCommand: UsbCbiGetStatus (%r)\n", Status));\r
d80ed2a7 475 return Status;\r
e237e7ae 476 }\r
477\r
478 if (UsbCbi->Interface.InterfaceSubClass == USB_MASS_STORE_UFI) {\r
479 //\r
480 // For UFI device, ASC and ASCQ are returned.\r
481 //\r
efe9186f 482 // Do not set the USB_MASS_CMD_FAIL for a request sense command\r
483 // as a bad result type doesn't mean a cmd failure\r
484 //\r
1436aea4 485 if ((Result.Type != 0) && (*(UINT8 *)Cmd != 0x03)) {\r
e237e7ae 486 *CmdStatus = USB_MASS_CMD_FAIL;\r
487 }\r
e237e7ae 488 } else {\r
489 //\r
490 // Check page 27, CBI spec 1.1 for vaious reture status.\r
491 //\r
492 switch (Result.Value & 0x03) {\r
1436aea4
MK
493 case 0x00:\r
494 //\r
495 // Pass\r
496 //\r
497 *CmdStatus = USB_MASS_CMD_SUCCESS;\r
498 break;\r
e237e7ae 499\r
1436aea4
MK
500 case 0x02:\r
501 //\r
502 // Phase Error, response with reset.\r
503 // No break here to fall through to "Fail".\r
504 //\r
505 UsbCbiResetDevice (UsbCbi, FALSE);\r
e237e7ae 506\r
1436aea4
MK
507 case 0x01:\r
508 //\r
509 // Fail\r
510 //\r
511 *CmdStatus = USB_MASS_CMD_FAIL;\r
512 break;\r
e237e7ae 513\r
1436aea4
MK
514 case 0x03:\r
515 //\r
516 // Persistent Fail. Need to send REQUEST SENSE.\r
517 //\r
518 *CmdStatus = USB_MASS_CMD_PERSISTENT;\r
519 break;\r
e237e7ae 520 }\r
521 }\r
522\r
523 return EFI_SUCCESS;\r
524}\r
525\r
e237e7ae 526/**\r
d80ed2a7 527 Reset the USB mass storage device by CBI protocol.\r
e237e7ae 528\r
d80ed2a7 529 This function resets the USB mass storage device by CBI protocol.\r
530 The reset is defined as a non-data command. Don't use UsbCbiExecCommand\r
531 to send the command to device because that may introduce recursive loop.\r
e237e7ae 532\r
d80ed2a7 533 @param Context The USB CBI protocol\r
534 @param ExtendedVerification The flag controlling the rule of reset.\r
535 Not used here.\r
536\r
537 @retval EFI_SUCCESS The device is reset.\r
538 @retval Others Failed to reset the device.\r
e237e7ae 539\r
540**/\r
e237e7ae 541EFI_STATUS\r
542UsbCbiResetDevice (\r
1436aea4
MK
543 IN VOID *Context,\r
544 IN BOOLEAN ExtendedVerification\r
e237e7ae 545 )\r
546{\r
1436aea4
MK
547 UINT8 ResetCmd[USB_CBI_RESET_CMD_LEN];\r
548 USB_CBI_PROTOCOL *UsbCbi;\r
549 USB_CBI_STATUS Result;\r
550 EFI_STATUS Status;\r
551 UINT32 Timeout;\r
e237e7ae 552\r
1436aea4 553 UsbCbi = (USB_CBI_PROTOCOL *)Context;\r
e237e7ae 554\r
555 //\r
556 // Fill in the reset command.\r
557 //\r
558 SetMem (ResetCmd, USB_CBI_RESET_CMD_LEN, 0xFF);\r
559\r
560 ResetCmd[0] = 0x1D;\r
561 ResetCmd[1] = 0x04;\r
41e8ff27 562 Timeout = USB_CBI_RESET_DEVICE_TIMEOUT / USB_MASS_1_MILLISECOND;\r
e237e7ae 563\r
564 //\r
565 // Send the command to the device. Don't use UsbCbiExecCommand here.\r
566 //\r
567 Status = UsbCbiSendCommand (UsbCbi, ResetCmd, USB_CBI_RESET_CMD_LEN, Timeout);\r
568 if (EFI_ERROR (Status)) {\r
c6e797ae 569 return EFI_DEVICE_ERROR;\r
e237e7ae 570 }\r
571\r
572 //\r
573 // Just retrieve the status and ignore that. Then stall\r
d80ed2a7 574 // 50ms to wait for it to complete.\r
e237e7ae 575 //\r
576 UsbCbiGetStatus (UsbCbi, Timeout, &Result);\r
41e8ff27 577 gBS->Stall (USB_CBI_RESET_DEVICE_STALL);\r
e237e7ae 578\r
579 //\r
d80ed2a7 580 // Clear the Bulk-In and Bulk-Out stall condition and init data toggle.\r
e237e7ae 581 //\r
582 UsbClearEndpointStall (UsbCbi->UsbIo, UsbCbi->BulkInEndpoint->EndpointAddress);\r
583 UsbClearEndpointStall (UsbCbi->UsbIo, UsbCbi->BulkOutEndpoint->EndpointAddress);\r
d80ed2a7 584\r
e237e7ae 585 return Status;\r
586}\r
587\r
e237e7ae 588/**\r
cc5166ff 589 Clean up the CBI protocol's resource.\r
e237e7ae 590\r
cc5166ff 591 @param Context The instance of CBI protocol.\r
e237e7ae 592\r
593 @retval EFI_SUCCESS The resource is cleaned up.\r
594\r
595**/\r
e237e7ae 596EFI_STATUS\r
d80ed2a7 597UsbCbiCleanUp (\r
1436aea4 598 IN VOID *Context\r
e237e7ae 599 )\r
600{\r
c92e277d 601 FreePool (Context);\r
e237e7ae 602 return EFI_SUCCESS;\r
603}\r