]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassCbi.c
1. Added EFI_MEDIA_CHANGED and EFI_INVALID_PARAMETER returns in UsbMassReadBlocks().
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassCbi.c
CommitLineData
e237e7ae 1/** @file\r
2\r
3Copyright (c) 2007, Intel Corporation\r
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
43 @param Controller The device controller\r
44 @param Context The variable to save context in\r
45\r
46 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory\r
47 @retval EFI_UNSUPPORTED The device isn't supported\r
48 @retval EFI_SUCCESS The CBI protocol is initialized.\r
49\r
50**/\r
51STATIC\r
52EFI_STATUS\r
53UsbCbiInit (\r
54 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
55 IN EFI_HANDLE Controller,\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\r
67 //\r
68 UsbCbi = AllocateZeroPool (\r
69 sizeof (USB_CBI_PROTOCOL) + 3 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR)\r
70 );\r
71\r
72 if (UsbCbi == NULL) {\r
73 return EFI_OUT_OF_RESOURCES;\r
74 }\r
75\r
76 UsbCbi->UsbIo = UsbIo;\r
77\r
78 //\r
79 // Get the interface descriptor and validate that it is a USB mass\r
80 // storage class CBI interface.\r
81 //\r
82 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &UsbCbi->Interface);\r
83 if (EFI_ERROR (Status)) {\r
84 goto ON_ERROR;\r
85 }\r
86\r
87 Interface = &UsbCbi->Interface;\r
88 if ((Interface->InterfaceProtocol != USB_MASS_STORE_CBI0)\r
89 && (Interface->InterfaceProtocol != USB_MASS_STORE_CBI1)) {\r
90 Status = EFI_UNSUPPORTED;\r
91 goto ON_ERROR;\r
92 }\r
93\r
94 //\r
95 // Locate and save the bulk-in, bulk-out, and interrupt endpoint\r
96 //\r
97 for (Index = 0; Index < Interface->NumEndpoints; Index++) {\r
98 Status = UsbIo->UsbGetEndpointDescriptor (UsbIo, Index, &EndPoint);\r
99 if (EFI_ERROR (Status)) {\r
100 continue;\r
101 }\r
102\r
103 if (USB_IS_BULK_ENDPOINT (EndPoint.Attributes)) {\r
104 //\r
105 // Use the first Bulk-In and Bulk-Out endpoints\r
106 //\r
107 if (USB_IS_IN_ENDPOINT (EndPoint.EndpointAddress) &&\r
108 (UsbCbi->BulkInEndpoint == NULL)) {\r
109\r
110 UsbCbi->BulkInEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbCbi + 1);\r
84b5c78e 111 CopyMem(UsbCbi->BulkInEndpoint, &EndPoint, sizeof (EndPoint));;\r
e237e7ae 112 }\r
113\r
114 if (USB_IS_OUT_ENDPOINT (EndPoint.EndpointAddress) &&\r
115 (UsbCbi->BulkOutEndpoint == NULL)) {\r
116\r
117 UsbCbi->BulkOutEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbCbi + 1) + 1;\r
84b5c78e 118 CopyMem(UsbCbi->BulkOutEndpoint, &EndPoint, sizeof (EndPoint));\r
e237e7ae 119 }\r
120\r
121 } else if (USB_IS_INTERRUPT_ENDPOINT (EndPoint.Attributes)) {\r
122 //\r
123 // Use the first interrupt endpoint if it is CBI0\r
124 //\r
125 if ((Interface->InterfaceProtocol == USB_MASS_STORE_CBI0) &&\r
126 (UsbCbi->InterruptEndpoint == NULL)) {\r
127\r
128 UsbCbi->InterruptEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbCbi + 1) + 2;\r
84b5c78e 129 CopyMem(UsbCbi->InterruptEndpoint, &EndPoint, sizeof (EndPoint));\r
e237e7ae 130 }\r
131 }\r
132 }\r
133\r
134 if ((UsbCbi->BulkInEndpoint == NULL)\r
135 || (UsbCbi->BulkOutEndpoint == NULL)\r
136 || ((Interface->InterfaceProtocol == USB_MASS_STORE_CBI0)\r
137 && (UsbCbi->InterruptEndpoint == NULL))) {\r
138 Status = EFI_UNSUPPORTED;\r
139 goto ON_ERROR;\r
140 }\r
141\r
142 if (Context != NULL) {\r
143 *Context = UsbCbi;\r
144 } else {\r
145 gBS->FreePool (UsbCbi);\r
146 }\r
147 return EFI_SUCCESS;\r
148\r
149ON_ERROR:\r
150 gBS->FreePool (UsbCbi);\r
151 return Status;\r
152}\r
153\r
154\r
155\r
156/**\r
157 Send the command to the device using class specific control transfer.\r
158\r
159 @param UsbCbi The USB CBI protocol\r
160 @param Cmd The high level command to transfer to device\r
161 @param CmdLen The length of the command\r
162 @param Timeout The time to wait the command to finish\r
163\r
164 @retval EFI_SUCCESS The command is transferred to device\r
165 @retval Others The command failed to transfer to device\r
166\r
167**/\r
168STATIC\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
41e8ff27 194 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 195\r
196 for (Retry = 0; Retry < USB_CBI_MAX_RETRY; Retry++) {\r
197 //\r
198 // Use the UsbIo 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/**\r
230 Transfer data between the device and host. The CBI contains three phase,\r
231 command, data, and status. This is data phase.\r
232\r
233 @param UsbCbi The USB CBI device\r
234 @param DataDir The direction of the data transfer\r
235 @param Data The buffer to hold the data\r
236 @param TransLen The expected transfer length\r
237 @param Timeout The time to wait the command to execute\r
238\r
239 @retval EFI_SUCCESS The data transfer succeeded\r
240 @retval Others Failed to transfer all the data\r
241\r
242**/\r
243STATIC\r
244EFI_STATUS\r
245UsbCbiDataTransfer (\r
246 IN USB_CBI_PROTOCOL *UsbCbi,\r
247 IN EFI_USB_DATA_DIRECTION DataDir,\r
248 IN OUT UINT8 *Data,\r
249 IN OUT UINTN *TransLen,\r
250 IN UINT32 Timeout\r
251 )\r
252{\r
253 EFI_USB_ENDPOINT_DESCRIPTOR *Endpoint;\r
254 EFI_STATUS Status;\r
255 UINT32 TransStatus;\r
256 UINTN Remain;\r
257 UINTN Increment;\r
258 UINT8 *Next;\r
259 UINTN Retry;\r
260\r
261 //\r
262 // It's OK if no data to transfer\r
263 //\r
264 if ((DataDir == EfiUsbNoData) || (*TransLen == 0)) {\r
265 return EFI_SUCCESS;\r
266 }\r
267\r
268 //\r
269 // Select the endpoint then issue the transfer\r
270 //\r
271 if (DataDir == EfiUsbDataIn) {\r
272 Endpoint = UsbCbi->BulkInEndpoint;\r
273 } else {\r
274 Endpoint = UsbCbi->BulkOutEndpoint;\r
275 }\r
276\r
277 Next = Data;\r
278 Remain = *TransLen;\r
279 Retry = 0;\r
280 Status = EFI_SUCCESS;\r
41e8ff27 281 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 282\r
283 //\r
284 // Transfer the data, if the device returns NAK, retry it.\r
285 //\r
286 while (Remain > 0) {\r
287 TransStatus = 0;\r
288\r
289 if (Remain > (UINTN) USB_CBI_MAX_PACKET_NUM * Endpoint->MaxPacketSize) {\r
290 Increment = USB_CBI_MAX_PACKET_NUM * Endpoint->MaxPacketSize;\r
291 } else {\r
292 Increment = Remain;\r
293 }\r
294\r
295 Status = UsbCbi->UsbIo->UsbBulkTransfer (\r
296 UsbCbi->UsbIo,\r
297 Endpoint->EndpointAddress,\r
298 Next,\r
299 &Increment,\r
300 Timeout,\r
301 &TransStatus\r
302 );\r
303 if (EFI_ERROR (Status)) {\r
304 if (TransStatus == EFI_USB_ERR_NAK) {\r
305 //\r
306 // The device can NAK the host if either the data/buffer isn't\r
307 // aviable or the command is in-progress. The data can be partly\r
308 // transferred. The transfer is aborted if several succssive data\r
309 // transfer commands are NAKed.\r
310 //\r
311 if (Increment == 0) {\r
312 if (++Retry > USB_CBI_MAX_RETRY) {\r
313 goto ON_EXIT;\r
314 }\r
315\r
316 } else {\r
317 Next += Increment;\r
318 Remain -= Increment;\r
319 Retry = 0;\r
320 }\r
321\r
322 continue;\r
323 }\r
324\r
325 //\r
326 // The device can fail the command by STALL the bulk endpoint.\r
327 // Clear the stall if that is the case.\r
328 //\r
329 if (TransStatus == EFI_USB_ERR_STALL) {\r
330 UsbClearEndpointStall (UsbCbi->UsbIo, Endpoint->EndpointAddress);\r
331 }\r
332\r
333 goto ON_EXIT;\r
334 }\r
335\r
336 Next += Increment;\r
337 Remain -= Increment;\r
338 }\r
339\r
340ON_EXIT:\r
341 *TransLen -= Remain;\r
342 return Status;\r
343}\r
344\r
345\r
346/**\r
347 Get the result of high level command execution from interrupt\r
348 endpoint. This function returns the USB transfer status, and\r
349 put the high level command execution result in Result.\r
350\r
351 @param UsbCbi The USB CBI protocol\r
352 @param Timeout The time to wait the command to execute\r
353 @param Result GC_TODO: add argument description\r
354\r
355 @retval EFI_SUCCESS The high level command execution result is\r
356 retrieved in Result.\r
357 @retval Others Failed to retrieve the result.\r
358\r
359**/\r
360STATIC\r
361EFI_STATUS\r
362UsbCbiGetStatus (\r
363 IN USB_CBI_PROTOCOL *UsbCbi,\r
364 IN UINT32 Timeout,\r
365 OUT USB_CBI_STATUS *Result\r
366 )\r
367{\r
368 UINTN Len;\r
369 UINT8 Endpoint;\r
370 EFI_STATUS Status;\r
371 UINT32 TransStatus;\r
372 INTN Retry;\r
373\r
374 Endpoint = UsbCbi->InterruptEndpoint->EndpointAddress;\r
375 Status = EFI_SUCCESS;\r
41e8ff27 376 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 377\r
378 //\r
379 // Attemp to the read the result from interrupt endpoint\r
380 //\r
381 for (Retry = 0; Retry < USB_CBI_MAX_RETRY; Retry++) {\r
382 TransStatus = 0;\r
383 Len = sizeof (USB_CBI_STATUS);\r
384\r
385 Status = UsbCbi->UsbIo->UsbSyncInterruptTransfer (\r
386 UsbCbi->UsbIo,\r
387 Endpoint,\r
388 Result,\r
389 &Len,\r
390 Timeout,\r
391 &TransStatus\r
392 );\r
393 //\r
394 // The CBI can NAK the interrupt endpoint if the command is in-progress.\r
395 //\r
396 if (EFI_ERROR (Status) && (TransStatus == EFI_USB_ERR_NAK)) {\r
397 continue;\r
398 }\r
399\r
400 break;\r
401 }\r
402\r
403 return Status;\r
404}\r
405\r
406\r
407/**\r
408 Execute USB mass storage command through the CBI0/CBI1 transport protocol\r
409\r
410 @param Context The USB CBI device\r
411 @param Cmd The command to transfer to device\r
412 @param CmdLen The length of the command\r
413 @param DataDir The direction of data transfer\r
414 @param Data The buffer to hold the data\r
415 @param DataLen The length of the buffer\r
416 @param Timeout The time to wait\r
417 @param CmdStatus The result of the command execution\r
418\r
419 @retval EFI_SUCCESS The command is executed OK and result in CmdStatus.\r
420 @retval EFI_DEVICE_ERROR Failed to execute the command\r
421\r
422**/\r
423STATIC\r
424EFI_STATUS\r
425UsbCbiExecCommand (\r
426 IN VOID *Context,\r
427 IN VOID *Cmd,\r
428 IN UINT8 CmdLen,\r
429 IN EFI_USB_DATA_DIRECTION DataDir,\r
430 IN VOID *Data,\r
431 IN UINT32 DataLen,\r
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
606 UsbCbiFini\r
607};\r
608\r
609USB_MASS_TRANSPORT\r
610mUsbCbi1Transport = {\r
611 USB_MASS_STORE_CBI1,\r
612 UsbCbiInit,\r
613 UsbCbiExecCommand,\r
614 UsbCbiResetDevice,\r
615 UsbCbiFini\r
616};\r