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