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