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