]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassCbi.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassCbi.c
... / ...
CommitLineData
1/** @file\r
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
4 Notice: it is being obsoleted by the standard body in favor of the BOT\r
5 (Bulk-Only Transport).\r
6\r
7Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
8SPDX-License-Identifier: BSD-2-Clause-Patent\r
9\r
10**/\r
11\r
12#include "UsbMass.h"\r
13\r
14//\r
15// Definition of USB CBI0 Transport Protocol\r
16//\r
17USB_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
25\r
26//\r
27// Definition of USB CBI1 Transport Protocol\r
28//\r
29USB_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
37\r
38/**\r
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
44\r
45 @param UsbIo The USB I/O Protocol instance\r
46 @param Context The buffer to save the context to\r
47\r
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
51\r
52**/\r
53EFI_STATUS\r
54UsbCbiInit (\r
55 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
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
66 // Allocate the CBI context for USB_CBI_PROTOCOL and 3 endpoint descriptors.\r
67 //\r
68 UsbCbi = AllocateZeroPool (\r
69 sizeof (USB_CBI_PROTOCOL) + 3 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR)\r
70 );\r
71 ASSERT (UsbCbi != NULL);\r
72\r
73 UsbCbi->UsbIo = UsbIo;\r
74\r
75 //\r
76 // Get the interface descriptor and validate that it\r
77 // is a USB Mass Storage CBI interface.\r
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 {\r
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
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
110 }\r
111\r
112 if (USB_IS_OUT_ENDPOINT (EndPoint.EndpointAddress) &&\r
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
117 }\r
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
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
127 }\r
128 }\r
129 }\r
130\r
131 if ((UsbCbi->BulkInEndpoint == NULL) || (UsbCbi->BulkOutEndpoint == NULL)) {\r
132 Status = EFI_UNSUPPORTED;\r
133 goto ON_ERROR;\r
134 }\r
135\r
136 if ((Interface->InterfaceProtocol == USB_MASS_STORE_CBI0) && (UsbCbi->InterruptEndpoint == NULL)) {\r
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
144 FreePool (UsbCbi);\r
145 }\r
146\r
147 return EFI_SUCCESS;\r
148\r
149ON_ERROR:\r
150 FreePool (UsbCbi);\r
151 return Status;\r
152}\r
153\r
154/**\r
155 Send the command to the device using class specific control transfer.\r
156\r
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
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
165 @retval EFI_SUCCESS The command is sent to the device.\r
166 @retval Others The command failed to transfer to device\r
167\r
168**/\r
169EFI_STATUS\r
170UsbCbiSendCommand (\r
171 IN USB_CBI_PROTOCOL *UsbCbi,\r
172 IN UINT8 *Cmd,\r
173 IN UINT8 CmdLen,\r
174 IN UINT32 Timeout\r
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
185 // Cmd" (ADSC) class specific request to send commands.\r
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
193 Status = EFI_SUCCESS;\r
194 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
195\r
196 for (Retry = 0; Retry < USB_CBI_MAX_RETRY; Retry++) {\r
197 //\r
198 // Use USB I/O Protocol to send the command to the device\r
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
228/**\r
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
233\r
234 @param UsbCbi The USB CBI device\r
235 @param DataDir The direction of the data transfer\r
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
240\r
241 @retval EFI_SUCCESS The data transferred successfully.\r
242 @retval EFI_SUCCESS No data to transfer\r
243 @retval Others Failed to transfer all the data\r
244\r
245**/\r
246EFI_STATUS\r
247UsbCbiDataTransfer (\r
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
253 )\r
254{\r
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
262\r
263 //\r
264 // If no data to transfer, just return EFI_SUCCESS.\r
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
283 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
284\r
285 //\r
286 // Transfer the data with a loop. The length of data transferred once is restricted.\r
287 //\r
288 while (Remain > 0) {\r
289 TransStatus = 0;\r
290\r
291 if (Remain > (UINTN)USB_CBI_MAX_PACKET_NUM * Endpoint->MaxPacketSize) {\r
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
309 // available or the command is in-progress.\r
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
313 //\r
314 if (Increment == 0) {\r
315 if (++Retry > USB_CBI_MAX_RETRY) {\r
316 goto ON_EXIT;\r
317 }\r
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
338 Next += Increment;\r
339 Remain -= Increment;\r
340 }\r
341\r
342ON_EXIT:\r
343 *TransLen -= Remain;\r
344 return Status;\r
345}\r
346\r
347/**\r
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
353\r
354 @param UsbCbi The USB CBI protocol\r
355 @param Timeout The time to wait for the command to execute\r
356 @param Result The result of the command execution.\r
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
363EFI_STATUS\r
364UsbCbiGetStatus (\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
378 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
379\r
380 //\r
381 // Attempt to the read the result from interrupt endpoint\r
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 Execute USB mass storage command through the CBI0/CBI1 transport protocol.\r
410\r
411 @param Context The USB CBI Protocol.\r
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
417 @param Lun Should be 0, this field for bot only\r
418 @param Timeout The time to wait\r
419 @param CmdStatus The result of the command execution\r
420\r
421 @retval EFI_SUCCESS The command is executed successfully.\r
422 @retval Other Failed to execute the command\r
423\r
424**/\r
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
433 IN UINT8 Lun,\r
434 IN UINT32 Timeout,\r
435 OUT UINT32 *CmdStatus\r
436 )\r
437{\r
438 USB_CBI_PROTOCOL *UsbCbi;\r
439 USB_CBI_STATUS Result;\r
440 EFI_STATUS Status;\r
441 UINTN TransLen;\r
442\r
443 *CmdStatus = USB_MASS_CMD_SUCCESS;\r
444 UsbCbi = (USB_CBI_PROTOCOL *)Context;\r
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
452 gBS->Stall (10 * USB_MASS_1_MILLISECOND);\r
453 DEBUG ((DEBUG_ERROR, "UsbCbiExecCommand: UsbCbiSendCommand (%r)\n", Status));\r
454 return Status;\r
455 }\r
456\r
457 //\r
458 // Transfer the data. Return this status if no interrupt endpoint\r
459 // is used to report the transfer status.\r
460 //\r
461 TransLen = (UINTN)DataLen;\r
462\r
463 Status = UsbCbiDataTransfer (UsbCbi, DataDir, Data, &TransLen, Timeout);\r
464 if (UsbCbi->InterruptEndpoint == NULL) {\r
465 DEBUG ((DEBUG_ERROR, "UsbCbiExecCommand: UsbCbiDataTransfer (%r)\n", Status));\r
466 return Status;\r
467 }\r
468\r
469 //\r
470 // Get the status. If it succeeds, interpret the result.\r
471 //\r
472 Status = UsbCbiGetStatus (UsbCbi, Timeout, &Result);\r
473 if (EFI_ERROR (Status)) {\r
474 DEBUG ((DEBUG_ERROR, "UsbCbiExecCommand: UsbCbiGetStatus (%r)\n", Status));\r
475 return Status;\r
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
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
485 if ((Result.Type != 0) && (*(UINT8 *)Cmd != 0x03)) {\r
486 *CmdStatus = USB_MASS_CMD_FAIL;\r
487 }\r
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
493 case 0x00:\r
494 //\r
495 // Pass\r
496 //\r
497 *CmdStatus = USB_MASS_CMD_SUCCESS;\r
498 break;\r
499\r
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
506\r
507 case 0x01:\r
508 //\r
509 // Fail\r
510 //\r
511 *CmdStatus = USB_MASS_CMD_FAIL;\r
512 break;\r
513\r
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
520 }\r
521 }\r
522\r
523 return EFI_SUCCESS;\r
524}\r
525\r
526/**\r
527 Reset the USB mass storage device by CBI protocol.\r
528\r
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
532\r
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
539\r
540**/\r
541EFI_STATUS\r
542UsbCbiResetDevice (\r
543 IN VOID *Context,\r
544 IN BOOLEAN ExtendedVerification\r
545 )\r
546{\r
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
552\r
553 UsbCbi = (USB_CBI_PROTOCOL *)Context;\r
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
562 Timeout = USB_CBI_RESET_DEVICE_TIMEOUT / USB_MASS_1_MILLISECOND;\r
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
569 return EFI_DEVICE_ERROR;\r
570 }\r
571\r
572 //\r
573 // Just retrieve the status and ignore that. Then stall\r
574 // 50ms to wait for it to complete.\r
575 //\r
576 UsbCbiGetStatus (UsbCbi, Timeout, &Result);\r
577 gBS->Stall (USB_CBI_RESET_DEVICE_STALL);\r
578\r
579 //\r
580 // Clear the Bulk-In and Bulk-Out stall condition and init data toggle.\r
581 //\r
582 UsbClearEndpointStall (UsbCbi->UsbIo, UsbCbi->BulkInEndpoint->EndpointAddress);\r
583 UsbClearEndpointStall (UsbCbi->UsbIo, UsbCbi->BulkOutEndpoint->EndpointAddress);\r
584\r
585 return Status;\r
586}\r
587\r
588/**\r
589 Clean up the CBI protocol's resource.\r
590\r
591 @param Context The instance of CBI protocol.\r
592\r
593 @retval EFI_SUCCESS The resource is cleaned up.\r
594\r
595**/\r
596EFI_STATUS\r
597UsbCbiCleanUp (\r
598 IN VOID *Context\r
599 )\r
600{\r
601 FreePool (Context);\r
602 return EFI_SUCCESS;\r
603}\r