]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassCbi.c
Enhance the string formatting function to take "%p" to print pointer.
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassCbi.c
CommitLineData
e237e7ae 1/** @file\r
2\r
c7e39923 3Copyright (c) 2007 - 2008, Intel Corporation\r
e237e7ae 4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 UsbMassCbi.c\r
15\r
16Abstract:\r
17\r
18 Implementation of the USB mass storage Control/Bulk/Interrupt transpor.\r
19 Notice: it is being obseleted by the standard body in favor of the BOT\r
20 (Bulk-Only Transport).\r
21\r
22Revision History\r
23\r
24\r
25**/\r
26\r
27#include "UsbMass.h"\r
28#include "UsbMassCbi.h"\r
29\r
e237e7ae 30STATIC\r
31EFI_STATUS\r
32UsbCbiResetDevice (\r
33 IN VOID *Context,\r
34 IN BOOLEAN ExtendedVerification\r
35 );\r
36\r
37\r
38/**\r
39 Initialize the USB mass storage class CBI transport protocol.\r
40 If Context isn't NULL, it will save its context in it.\r
41\r
42 @param UsbIo The USB IO to use\r
e237e7ae 43 @param Context The variable to save context in\r
44\r
45 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory\r
46 @retval EFI_UNSUPPORTED The device isn't supported\r
47 @retval EFI_SUCCESS The CBI protocol is initialized.\r
48\r
49**/\r
50STATIC\r
51EFI_STATUS\r
52UsbCbiInit (\r
53 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
e237e7ae 54 OUT VOID **Context OPTIONAL\r
55 )\r
56{\r
57 USB_CBI_PROTOCOL *UsbCbi;\r
58 EFI_USB_INTERFACE_DESCRIPTOR *Interface;\r
59 EFI_USB_ENDPOINT_DESCRIPTOR EndPoint;\r
60 EFI_STATUS Status;\r
61 UINT8 Index;\r
62\r
63 //\r
64 // Allocate the CBI context\r
65 //\r
66 UsbCbi = AllocateZeroPool (\r
67 sizeof (USB_CBI_PROTOCOL) + 3 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR)\r
68 );\r
69\r
70 if (UsbCbi == NULL) {\r
71 return EFI_OUT_OF_RESOURCES;\r
72 }\r
73\r
74 UsbCbi->UsbIo = UsbIo;\r
75\r
76 //\r
77 // Get the interface descriptor and validate that it is a USB mass\r
78 // storage class CBI interface.\r
79 //\r
80 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &UsbCbi->Interface);\r
81 if (EFI_ERROR (Status)) {\r
82 goto ON_ERROR;\r
83 }\r
84\r
85 Interface = &UsbCbi->Interface;\r
86 if ((Interface->InterfaceProtocol != USB_MASS_STORE_CBI0)\r
87 && (Interface->InterfaceProtocol != USB_MASS_STORE_CBI1)) {\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
84b5c78e 109 CopyMem(UsbCbi->BulkInEndpoint, &EndPoint, sizeof (EndPoint));;\r
e237e7ae 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
84b5c78e 116 CopyMem(UsbCbi->BulkOutEndpoint, &EndPoint, sizeof (EndPoint));\r
e237e7ae 117 }\r
118\r
119 } else if (USB_IS_INTERRUPT_ENDPOINT (EndPoint.Attributes)) {\r
120 //\r
121 // Use the first interrupt endpoint if it is CBI0\r
122 //\r
123 if ((Interface->InterfaceProtocol == USB_MASS_STORE_CBI0) &&\r
124 (UsbCbi->InterruptEndpoint == NULL)) {\r
125\r
126 UsbCbi->InterruptEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbCbi + 1) + 2;\r
84b5c78e 127 CopyMem(UsbCbi->InterruptEndpoint, &EndPoint, sizeof (EndPoint));\r
e237e7ae 128 }\r
129 }\r
130 }\r
131\r
132 if ((UsbCbi->BulkInEndpoint == NULL)\r
133 || (UsbCbi->BulkOutEndpoint == NULL)\r
134 || ((Interface->InterfaceProtocol == USB_MASS_STORE_CBI0)\r
135 && (UsbCbi->InterruptEndpoint == NULL))) {\r
136 Status = EFI_UNSUPPORTED;\r
137 goto ON_ERROR;\r
138 }\r
139\r
140 if (Context != NULL) {\r
141 *Context = UsbCbi;\r
142 } else {\r
143 gBS->FreePool (UsbCbi);\r
144 }\r
145 return EFI_SUCCESS;\r
146\r
147ON_ERROR:\r
148 gBS->FreePool (UsbCbi);\r
149 return Status;\r
150}\r
151\r
152\r
153\r
154/**\r
155 Send the command to the device using class specific control transfer.\r
156\r
157 @param UsbCbi The USB CBI protocol\r
158 @param Cmd The high level command to transfer to device\r
159 @param CmdLen The length of the command\r
160 @param Timeout The time to wait the command to finish\r
161\r
162 @retval EFI_SUCCESS The command is transferred to device\r
163 @retval Others The command failed to transfer to device\r
164\r
165**/\r
166STATIC\r
167EFI_STATUS\r
168UsbCbiSendCommand (\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
183 // Cmd" (ADSC) class specific request to send commands\r
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
196 // Use the UsbIo to send the command to the device\r
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
228 Transfer data between the device and host. The CBI contains three phase,\r
229 command, data, and status. This is data phase.\r
230\r
231 @param UsbCbi The USB CBI device\r
232 @param DataDir The direction of the data transfer\r
233 @param Data The buffer to hold the data\r
234 @param TransLen The expected transfer length\r
235 @param Timeout The time to wait the command to execute\r
236\r
237 @retval EFI_SUCCESS The data transfer succeeded\r
238 @retval Others Failed to transfer all the data\r
239\r
240**/\r
241STATIC\r
242EFI_STATUS\r
243UsbCbiDataTransfer (\r
244 IN USB_CBI_PROTOCOL *UsbCbi,\r
245 IN EFI_USB_DATA_DIRECTION DataDir,\r
246 IN OUT UINT8 *Data,\r
247 IN OUT UINTN *TransLen,\r
248 IN UINT32 Timeout\r
249 )\r
250{\r
251 EFI_USB_ENDPOINT_DESCRIPTOR *Endpoint;\r
252 EFI_STATUS Status;\r
253 UINT32 TransStatus;\r
254 UINTN Remain;\r
255 UINTN Increment;\r
256 UINT8 *Next;\r
257 UINTN Retry;\r
258\r
259 //\r
260 // It's OK if no data to transfer\r
261 //\r
262 if ((DataDir == EfiUsbNoData) || (*TransLen == 0)) {\r
263 return EFI_SUCCESS;\r
264 }\r
265\r
266 //\r
267 // Select the endpoint then issue the transfer\r
268 //\r
269 if (DataDir == EfiUsbDataIn) {\r
270 Endpoint = UsbCbi->BulkInEndpoint;\r
271 } else {\r
272 Endpoint = UsbCbi->BulkOutEndpoint;\r
273 }\r
274\r
275 Next = Data;\r
276 Remain = *TransLen;\r
277 Retry = 0;\r
278 Status = EFI_SUCCESS;\r
41e8ff27 279 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 280\r
281 //\r
282 // Transfer the data, if the device returns NAK, retry it.\r
283 //\r
284 while (Remain > 0) {\r
285 TransStatus = 0;\r
286\r
287 if (Remain > (UINTN) USB_CBI_MAX_PACKET_NUM * Endpoint->MaxPacketSize) {\r
288 Increment = USB_CBI_MAX_PACKET_NUM * Endpoint->MaxPacketSize;\r
289 } else {\r
290 Increment = Remain;\r
291 }\r
292\r
293 Status = UsbCbi->UsbIo->UsbBulkTransfer (\r
294 UsbCbi->UsbIo,\r
295 Endpoint->EndpointAddress,\r
296 Next,\r
297 &Increment,\r
298 Timeout,\r
299 &TransStatus\r
300 );\r
301 if (EFI_ERROR (Status)) {\r
302 if (TransStatus == EFI_USB_ERR_NAK) {\r
303 //\r
304 // The device can NAK the host if either the data/buffer isn't\r
305 // aviable or the command is in-progress. The data can be partly\r
306 // transferred. The transfer is aborted if several succssive data\r
307 // transfer commands are NAKed.\r
308 //\r
309 if (Increment == 0) {\r
310 if (++Retry > USB_CBI_MAX_RETRY) {\r
311 goto ON_EXIT;\r
312 }\r
313\r
314 } else {\r
315 Next += Increment;\r
316 Remain -= Increment;\r
317 Retry = 0;\r
318 }\r
319\r
320 continue;\r
321 }\r
322\r
323 //\r
324 // The device can fail the command by STALL the bulk endpoint.\r
325 // Clear the stall if that is the case.\r
326 //\r
327 if (TransStatus == EFI_USB_ERR_STALL) {\r
328 UsbClearEndpointStall (UsbCbi->UsbIo, Endpoint->EndpointAddress);\r
329 }\r
330\r
331 goto ON_EXIT;\r
332 }\r
333\r
334 Next += Increment;\r
335 Remain -= Increment;\r
336 }\r
337\r
338ON_EXIT:\r
339 *TransLen -= Remain;\r
340 return Status;\r
341}\r
342\r
343\r
344/**\r
345 Get the result of high level command execution from interrupt\r
346 endpoint. This function returns the USB transfer status, and\r
347 put the high level command execution result in Result.\r
348\r
349 @param UsbCbi The USB CBI protocol\r
350 @param Timeout The time to wait the command to execute\r
351 @param Result GC_TODO: add argument description\r
352\r
353 @retval EFI_SUCCESS The high level command execution result is\r
354 retrieved in Result.\r
355 @retval Others Failed to retrieve the result.\r
356\r
357**/\r
358STATIC\r
359EFI_STATUS\r
360UsbCbiGetStatus (\r
361 IN USB_CBI_PROTOCOL *UsbCbi,\r
362 IN UINT32 Timeout,\r
363 OUT USB_CBI_STATUS *Result\r
364 )\r
365{\r
366 UINTN Len;\r
367 UINT8 Endpoint;\r
368 EFI_STATUS Status;\r
369 UINT32 TransStatus;\r
370 INTN Retry;\r
371\r
372 Endpoint = UsbCbi->InterruptEndpoint->EndpointAddress;\r
373 Status = EFI_SUCCESS;\r
41e8ff27 374 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 375\r
376 //\r
377 // Attemp to the read the result from interrupt endpoint\r
378 //\r
379 for (Retry = 0; Retry < USB_CBI_MAX_RETRY; Retry++) {\r
380 TransStatus = 0;\r
381 Len = sizeof (USB_CBI_STATUS);\r
382\r
383 Status = UsbCbi->UsbIo->UsbSyncInterruptTransfer (\r
384 UsbCbi->UsbIo,\r
385 Endpoint,\r
386 Result,\r
387 &Len,\r
388 Timeout,\r
389 &TransStatus\r
390 );\r
391 //\r
392 // The CBI can NAK the interrupt endpoint if the command is in-progress.\r
393 //\r
394 if (EFI_ERROR (Status) && (TransStatus == EFI_USB_ERR_NAK)) {\r
395 continue;\r
396 }\r
397\r
398 break;\r
399 }\r
400\r
401 return Status;\r
402}\r
403\r
404\r
405/**\r
406 Execute USB mass storage command through the CBI0/CBI1 transport protocol\r
407\r
408 @param Context The USB CBI device\r
409 @param Cmd The command to transfer to device\r
410 @param CmdLen The length of the command\r
411 @param DataDir The direction of data transfer\r
412 @param Data The buffer to hold the data\r
413 @param DataLen The length of the buffer\r
c7e39923 414 @param Lun Should be 0, this field for bot only\r
e237e7ae 415 @param Timeout The time to wait\r
416 @param CmdStatus The result of the command execution\r
417\r
418 @retval EFI_SUCCESS The command is executed OK and result in CmdStatus.\r
419 @retval EFI_DEVICE_ERROR Failed to execute the command\r
420\r
421**/\r
422STATIC\r
423EFI_STATUS\r
424UsbCbiExecCommand (\r
425 IN VOID *Context,\r
426 IN VOID *Cmd,\r
427 IN UINT8 CmdLen,\r
428 IN EFI_USB_DATA_DIRECTION DataDir,\r
429 IN VOID *Data,\r
430 IN UINT32 DataLen,\r
c7e39923 431 IN UINT8 Lun,\r
e237e7ae 432 IN UINT32 Timeout,\r
433 OUT UINT32 *CmdStatus\r
434 )\r
435{\r
436 USB_CBI_PROTOCOL *UsbCbi;\r
437 USB_CBI_STATUS Result;\r
438 EFI_STATUS Status;\r
439 UINTN TransLen;\r
440\r
441 *CmdStatus = USB_MASS_CMD_SUCCESS;\r
442 UsbCbi = (USB_CBI_PROTOCOL *) Context;\r
443\r
444 //\r
445 // Send the command to the device. Return immediately if device\r
446 // rejects the command.\r
447 //\r
448 Status = UsbCbiSendCommand (UsbCbi, Cmd, CmdLen, Timeout);\r
449 if (EFI_ERROR (Status)) {\r
1c619535 450 DEBUG ((EFI_D_ERROR, "UsbCbiExecCommand: UsbCbiSendCommand (%r)\n",Status));\r
e237e7ae 451 return Status;\r
452 }\r
453\r
454 //\r
455 // Transfer the data, return this status if no interrupt endpoint\r
456 // is used to report the transfer status.\r
457 //\r
458 TransLen = (UINTN) DataLen;\r
459\r
460 Status = UsbCbiDataTransfer (UsbCbi, DataDir, Data, &TransLen, Timeout);\r
461 if (UsbCbi->InterruptEndpoint == NULL) {\r
1c619535 462 DEBUG ((EFI_D_ERROR, "UsbCbiExecCommand: UsbCbiDataTransfer (%r)\n",Status));\r
e237e7ae 463 return Status;\r
464 }\r
465\r
466 //\r
467 // Get the status, if that succeeds, interpret the result\r
468 //\r
469 Status = UsbCbiGetStatus (UsbCbi, Timeout, &Result);\r
470 if (EFI_ERROR (Status)) {\r
1c619535 471 DEBUG ((EFI_D_ERROR, "UsbCbiExecCommand: UsbCbiGetStatus (%r)\n",Status));\r
e237e7ae 472 return EFI_DEVICE_ERROR;\r
473 }\r
474\r
475 if (UsbCbi->Interface.InterfaceSubClass == USB_MASS_STORE_UFI) {\r
476 //\r
477 // For UFI device, ASC and ASCQ are returned.\r
478 //\r
479 if (Result.Type != 0) {\r
480 *CmdStatus = USB_MASS_CMD_FAIL;\r
481 }\r
482\r
483 } else {\r
484 //\r
485 // Check page 27, CBI spec 1.1 for vaious reture status.\r
486 //\r
487 switch (Result.Value & 0x03) {\r
488 case 0x00:\r
489 //\r
490 // Pass\r
491 //\r
492 *CmdStatus = USB_MASS_CMD_SUCCESS;\r
493 break;\r
494\r
495 case 0x02:\r
496 //\r
497 // Phase Error, response with reset. Fall through to Fail.\r
498 //\r
499 UsbCbiResetDevice (UsbCbi, FALSE);\r
500\r
501 case 0x01:\r
502 //\r
503 // Fail\r
504 //\r
505 *CmdStatus = USB_MASS_CMD_FAIL;\r
506 break;\r
507\r
508 case 0x03:\r
509 //\r
510 // Persistent Fail, need to send REQUEST SENSE.\r
511 //\r
512 *CmdStatus = USB_MASS_CMD_PERSISTENT;\r
513 break;\r
514 }\r
515 }\r
516\r
517 return EFI_SUCCESS;\r
518}\r
519\r
520\r
521/**\r
522 Call the Usb mass storage class transport protocol to\r
523 reset the device. The reset is defined as a Non-Data\r
524 command. Don't use UsbCbiExecCommand to send the command\r
525 to device because that may introduce recursive loop.\r
526\r
527 @param Context The USB CBI device protocol\r
528\r
529 @retval EFI_SUCCESS the device is reset\r
530 @retval Others Failed to reset the device\r
531\r
532**/\r
533STATIC\r
534EFI_STATUS\r
535UsbCbiResetDevice (\r
536 IN VOID *Context,\r
537 IN BOOLEAN ExtendedVerification\r
538 )\r
539{\r
540 UINT8 ResetCmd[USB_CBI_RESET_CMD_LEN];\r
541 USB_CBI_PROTOCOL *UsbCbi;\r
542 USB_CBI_STATUS Result;\r
543 EFI_STATUS Status;\r
544 UINT32 Timeout;\r
545\r
546 UsbCbi = (USB_CBI_PROTOCOL *) Context;\r
547\r
548 //\r
549 // Fill in the reset command.\r
550 //\r
551 SetMem (ResetCmd, USB_CBI_RESET_CMD_LEN, 0xFF);\r
552\r
553 ResetCmd[0] = 0x1D;\r
554 ResetCmd[1] = 0x04;\r
41e8ff27 555 Timeout = USB_CBI_RESET_DEVICE_TIMEOUT / USB_MASS_1_MILLISECOND;\r
e237e7ae 556\r
557 //\r
558 // Send the command to the device. Don't use UsbCbiExecCommand here.\r
559 //\r
560 Status = UsbCbiSendCommand (UsbCbi, ResetCmd, USB_CBI_RESET_CMD_LEN, Timeout);\r
561 if (EFI_ERROR (Status)) {\r
562 return Status;\r
563 }\r
564\r
565 //\r
566 // Just retrieve the status and ignore that. Then stall\r
567 // 50ms to wait it complete\r
568 //\r
569 UsbCbiGetStatus (UsbCbi, Timeout, &Result);\r
41e8ff27 570 gBS->Stall (USB_CBI_RESET_DEVICE_STALL);\r
e237e7ae 571\r
572 //\r
573 // Clear the Bulk-In and Bulk-Out stall condition and\r
574 // init data toggle.\r
575 //\r
576 UsbClearEndpointStall (UsbCbi->UsbIo, UsbCbi->BulkInEndpoint->EndpointAddress);\r
577 UsbClearEndpointStall (UsbCbi->UsbIo, UsbCbi->BulkOutEndpoint->EndpointAddress);\r
578 return Status;\r
579}\r
580\r
581\r
582/**\r
583 Clean up the CBI protocol's resource\r
584\r
585 @param Context The CBI protocol\r
586\r
587 @retval EFI_SUCCESS The resource is cleaned up.\r
588\r
589**/\r
590STATIC\r
591EFI_STATUS\r
592UsbCbiFini (\r
593 IN VOID *Context\r
594 )\r
595{\r
596 gBS->FreePool (Context);\r
597 return EFI_SUCCESS;\r
598}\r
599\r
600USB_MASS_TRANSPORT\r
601mUsbCbi0Transport = {\r
602 USB_MASS_STORE_CBI0,\r
603 UsbCbiInit,\r
604 UsbCbiExecCommand,\r
605 UsbCbiResetDevice,\r
c7e39923 606 NULL,\r
e237e7ae 607 UsbCbiFini\r
608};\r
609\r
610USB_MASS_TRANSPORT\r
611mUsbCbi1Transport = {\r
612 USB_MASS_STORE_CBI1,\r
613 UsbCbiInit,\r
614 UsbCbiExecCommand,\r
615 UsbCbiResetDevice,\r
c7e39923 616 NULL,\r
e237e7ae 617 UsbCbiFini\r
618};\r