]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBot.c
MdeModulePkg/UsbMassStorageDxe: Check Get Max LUN status/value
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassBot.c
CommitLineData
e237e7ae 1/** @file\r
d80ed2a7 2 Implementation of the USB mass storage Bulk-Only Transport protocol,\r
3 according to USB Mass Storage Class Bulk-Only Transport, Revision 1.0.\r
cc5166ff 4\r
8d92f819 5Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 6This program and the accompanying materials\r
e237e7ae 7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
cc5166ff 14**/\r
e237e7ae 15\r
39840c50 16#include "UsbMass.h"\r
e237e7ae 17\r
d80ed2a7 18//\r
19// Definition of USB BOT Transport Protocol\r
20//\r
21USB_MASS_TRANSPORT mUsbBotTransport = {\r
22 USB_MASS_STORE_BOT,\r
23 UsbBotInit,\r
24 UsbBotExecCommand,\r
25 UsbBotResetDevice,\r
26 UsbBotGetMaxLun,\r
27 UsbBotCleanUp\r
28};\r
e237e7ae 29\r
30/**\r
d80ed2a7 31 Initializes USB BOT protocol.\r
32\r
33 This function initializes the USB mass storage class BOT protocol.\r
e237e7ae 34 It will save its context which is a USB_BOT_PROTOCOL structure\r
35 in the Context if Context isn't NULL.\r
36\r
d80ed2a7 37 @param UsbIo The USB I/O Protocol instance\r
38 @param Context The buffer to save the context to\r
e237e7ae 39\r
d80ed2a7 40 @retval EFI_SUCCESS The device is successfully initialized.\r
e237e7ae 41 @retval EFI_UNSUPPORTED The transport protocol doesn't support the device.\r
d80ed2a7 42 @retval Other The USB BOT initialization fails.\r
e237e7ae 43\r
44**/\r
e237e7ae 45EFI_STATUS\r
46UsbBotInit (\r
d80ed2a7 47 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
e237e7ae 48 OUT VOID **Context OPTIONAL\r
49 )\r
50{\r
51 USB_BOT_PROTOCOL *UsbBot;\r
52 EFI_USB_INTERFACE_DESCRIPTOR *Interface;\r
53 EFI_USB_ENDPOINT_DESCRIPTOR EndPoint;\r
54 EFI_STATUS Status;\r
55 UINT8 Index;\r
56\r
57 //\r
d80ed2a7 58 // Allocate the BOT context for USB_BOT_PROTOCOL and two endpoint descriptors.\r
e237e7ae 59 //\r
3e03cb4d 60 UsbBot = AllocateZeroPool (sizeof (USB_BOT_PROTOCOL) + 2 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR));\r
d80ed2a7 61 ASSERT (UsbBot != NULL);\r
e237e7ae 62\r
63 UsbBot->UsbIo = UsbIo;\r
64\r
65 //\r
66 // Get the interface descriptor and validate that it\r
d80ed2a7 67 // is a USB Mass Storage BOT interface.\r
e237e7ae 68 //\r
69 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &UsbBot->Interface);\r
70\r
71 if (EFI_ERROR (Status)) {\r
e237e7ae 72 goto ON_ERROR;\r
73 }\r
74\r
75 Interface = &UsbBot->Interface;\r
76\r
77 if (Interface->InterfaceProtocol != USB_MASS_STORE_BOT) {\r
78 Status = EFI_UNSUPPORTED;\r
79 goto ON_ERROR;\r
80 }\r
81\r
82 //\r
83 // Locate and save the first bulk-in and bulk-out endpoint\r
84 //\r
85 for (Index = 0; Index < Interface->NumEndpoints; Index++) {\r
86 Status = UsbIo->UsbGetEndpointDescriptor (UsbIo, Index, &EndPoint);\r
87\r
88 if (EFI_ERROR (Status) || !USB_IS_BULK_ENDPOINT (EndPoint.Attributes)) {\r
89 continue;\r
90 }\r
91\r
92 if (USB_IS_IN_ENDPOINT (EndPoint.EndpointAddress) &&\r
93 (UsbBot->BulkInEndpoint == NULL)) {\r
94\r
95 UsbBot->BulkInEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbBot + 1);\r
84b5c78e 96 CopyMem(UsbBot->BulkInEndpoint, &EndPoint, sizeof (EndPoint));\r
e237e7ae 97 }\r
98\r
99 if (USB_IS_OUT_ENDPOINT (EndPoint.EndpointAddress) &&\r
100 (UsbBot->BulkOutEndpoint == NULL)) {\r
101\r
102 UsbBot->BulkOutEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbBot + 1) + 1;\r
3e03cb4d 103 CopyMem (UsbBot->BulkOutEndpoint, &EndPoint, sizeof(EndPoint));\r
e237e7ae 104 }\r
105 }\r
106\r
d80ed2a7 107 //\r
108 // If bulk-in or bulk-out endpoint is not found, report error.\r
109 //\r
e237e7ae 110 if ((UsbBot->BulkInEndpoint == NULL) || (UsbBot->BulkOutEndpoint == NULL)) {\r
e237e7ae 111 Status = EFI_UNSUPPORTED;\r
112 goto ON_ERROR;\r
113 }\r
114\r
115 //\r
d80ed2a7 116 // The USB BOT protocol uses CBWTag to match the CBW and CSW.\r
e237e7ae 117 //\r
118 UsbBot->CbwTag = 0x01;\r
119\r
120 if (Context != NULL) {\r
121 *Context = UsbBot;\r
122 } else {\r
c92e277d 123 FreePool (UsbBot);\r
e237e7ae 124 }\r
125\r
126 return EFI_SUCCESS;\r
127\r
128ON_ERROR:\r
c92e277d 129 FreePool (UsbBot);\r
e237e7ae 130 return Status;\r
131}\r
132\r
e237e7ae 133/**\r
cc5166ff 134 Send the command to the device using Bulk-Out endpoint.\r
e237e7ae 135\r
d80ed2a7 136 This function sends the command to the device using Bulk-Out endpoint.\r
137 BOT transfer is composed of three phases: Command, Data, and Status.\r
138 This is the Command phase.\r
139\r
e237e7ae 140 @param UsbBot The USB BOT device\r
141 @param Cmd The command to transfer to device\r
d80ed2a7 142 @param CmdLen The length of the command\r
e237e7ae 143 @param DataDir The direction of the data\r
144 @param TransLen The expected length of the data\r
c7e39923 145 @param Lun The number of logic unit\r
e237e7ae 146\r
e237e7ae 147 @retval EFI_SUCCESS The command is sent to the device.\r
d80ed2a7 148 @retval EFI_NOT_READY The device return NAK to the transfer\r
e237e7ae 149 @retval Others Failed to send the command to device\r
150\r
151**/\r
e237e7ae 152EFI_STATUS\r
153UsbBotSendCommand (\r
154 IN USB_BOT_PROTOCOL *UsbBot,\r
155 IN UINT8 *Cmd,\r
156 IN UINT8 CmdLen,\r
157 IN EFI_USB_DATA_DIRECTION DataDir,\r
c7e39923 158 IN UINT32 TransLen,\r
159 IN UINT8 Lun\r
e237e7ae 160 )\r
161{\r
162 USB_BOT_CBW Cbw;\r
163 EFI_STATUS Status;\r
164 UINT32 Result;\r
165 UINTN DataLen;\r
166 UINTN Timeout;\r
167\r
168 ASSERT ((CmdLen > 0) && (CmdLen <= USB_BOT_MAX_CMDLEN));\r
169\r
170 //\r
d80ed2a7 171 // Fill in the Command Block Wrapper.\r
e237e7ae 172 //\r
173 Cbw.Signature = USB_BOT_CBW_SIGNATURE;\r
174 Cbw.Tag = UsbBot->CbwTag;\r
175 Cbw.DataLen = TransLen;\r
d80ed2a7 176 Cbw.Flag = (UINT8) ((DataDir == EfiUsbDataIn) ? BIT7 : 0);\r
c7e39923 177 Cbw.Lun = Lun;\r
e237e7ae 178 Cbw.CmdLen = CmdLen;\r
179\r
180 ZeroMem (Cbw.CmdBlock, USB_BOT_MAX_CMDLEN);\r
181 CopyMem (Cbw.CmdBlock, Cmd, CmdLen);\r
182\r
d80ed2a7 183 Result = 0;\r
184 DataLen = sizeof (USB_BOT_CBW);\r
185 Timeout = USB_BOT_SEND_CBW_TIMEOUT / USB_MASS_1_MILLISECOND;\r
e237e7ae 186\r
187 //\r
d80ed2a7 188 // Use USB I/O Protocol to send the Command Block Wrapper to the device.\r
e237e7ae 189 //\r
190 Status = UsbBot->UsbIo->UsbBulkTransfer (\r
191 UsbBot->UsbIo,\r
192 UsbBot->BulkOutEndpoint->EndpointAddress,\r
193 &Cbw,\r
194 &DataLen,\r
195 Timeout,\r
196 &Result\r
197 );\r
e237e7ae 198 if (EFI_ERROR (Status)) {\r
199 if (USB_IS_ERROR (Result, EFI_USB_ERR_STALL) && DataDir == EfiUsbDataOut) {\r
d80ed2a7 200 //\r
201 // Respond to Bulk-Out endpoint stall with a Reset Recovery,\r
202 // according to section 5.3.1 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.\r
203 //\r
e237e7ae 204 UsbBotResetDevice (UsbBot, FALSE);\r
205 } else if (USB_IS_ERROR (Result, EFI_USB_ERR_NAK)) {\r
206 Status = EFI_NOT_READY;\r
207 }\r
208 }\r
209\r
210 return Status;\r
211}\r
212\r
213\r
214/**\r
d80ed2a7 215 Transfer the data between the device and host.\r
216\r
217 This function transfers the data between the device and host.\r
218 BOT transfer is composed of three phases: Command, Data, and Status.\r
219 This is the Data phase.\r
e237e7ae 220\r
221 @param UsbBot The USB BOT device\r
222 @param DataDir The direction of the data\r
223 @param Data The buffer to hold data\r
224 @param TransLen The expected length of the data\r
225 @param Timeout The time to wait the command to complete\r
226\r
227 @retval EFI_SUCCESS The data is transferred\r
d80ed2a7 228 @retval EFI_SUCCESS No data to transfer\r
229 @retval EFI_NOT_READY The device return NAK to the transfer\r
e237e7ae 230 @retval Others Failed to transfer data\r
231\r
232**/\r
e237e7ae 233EFI_STATUS\r
234UsbBotDataTransfer (\r
235 IN USB_BOT_PROTOCOL *UsbBot,\r
236 IN EFI_USB_DATA_DIRECTION DataDir,\r
237 IN OUT UINT8 *Data,\r
238 IN OUT UINTN *TransLen,\r
239 IN UINT32 Timeout\r
240 )\r
241{\r
242 EFI_USB_ENDPOINT_DESCRIPTOR *Endpoint;\r
243 EFI_STATUS Status;\r
244 UINT32 Result;\r
245\r
246 //\r
d80ed2a7 247 // If no data to transfer, just return EFI_SUCCESS.\r
e237e7ae 248 //\r
249 if ((DataDir == EfiUsbNoData) || (*TransLen == 0)) {\r
250 return EFI_SUCCESS;\r
251 }\r
252\r
253 //\r
254 // Select the endpoint then issue the transfer\r
255 //\r
256 if (DataDir == EfiUsbDataIn) {\r
257 Endpoint = UsbBot->BulkInEndpoint;\r
258 } else {\r
259 Endpoint = UsbBot->BulkOutEndpoint;\r
260 }\r
261\r
262 Result = 0;\r
41e8ff27 263 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 264\r
265 Status = UsbBot->UsbIo->UsbBulkTransfer (\r
266 UsbBot->UsbIo,\r
267 Endpoint->EndpointAddress,\r
268 Data,\r
269 TransLen,\r
270 Timeout,\r
271 &Result\r
272 );\r
273 if (EFI_ERROR (Status)) {\r
e237e7ae 274 if (USB_IS_ERROR (Result, EFI_USB_ERR_STALL)) {\r
efe9186f 275 DEBUG ((EFI_D_INFO, "UsbBotDataTransfer: (%r)\n", Status)); \r
276 DEBUG ((EFI_D_INFO, "UsbBotDataTransfer: DataIn Stall\n"));\r
e237e7ae 277 UsbClearEndpointStall (UsbBot->UsbIo, Endpoint->EndpointAddress);\r
278 } else if (USB_IS_ERROR (Result, EFI_USB_ERR_NAK)) {\r
279 Status = EFI_NOT_READY;\r
efe9186f 280 } else {\r
281 DEBUG ((EFI_D_ERROR, "UsbBotDataTransfer: (%r)\n", Status));\r
282 }\r
283 if(Status == EFI_TIMEOUT){\r
284 UsbBotResetDevice(UsbBot, FALSE);\r
e237e7ae 285 }\r
286 }\r
287\r
288 return Status;\r
289}\r
290\r
291\r
292/**\r
d80ed2a7 293 Get the command execution status from device.\r
294\r
295 This function gets the command execution status from device.\r
296 BOT transfer is composed of three phases: Command, Data, and Status.\r
297 This is the Status phase.\r
e237e7ae 298\r
d80ed2a7 299 This function returns the transfer status of the BOT's CSW status,\r
300 and returns the high level command execution result in Result. So\r
301 even if EFI_SUCCESS is returned, the command may still have failed.\r
e237e7ae 302\r
d80ed2a7 303 @param UsbBot The USB BOT device.\r
304 @param TransLen The expected length of the data.\r
305 @param CmdStatus The result of the command execution.\r
306\r
307 @retval EFI_SUCCESS Command execute result is retrieved and in the Result.\r
308 @retval Other Error occurred when trying to get status.\r
e237e7ae 309\r
310**/\r
e237e7ae 311EFI_STATUS\r
312UsbBotGetStatus (\r
313 IN USB_BOT_PROTOCOL *UsbBot,\r
314 IN UINT32 TransLen,\r
315 OUT UINT8 *CmdStatus\r
316 )\r
317{\r
318 USB_BOT_CSW Csw;\r
319 UINTN Len;\r
320 UINT8 Endpoint;\r
321 EFI_STATUS Status;\r
322 UINT32 Result;\r
323 EFI_USB_IO_PROTOCOL *UsbIo;\r
324 UINT32 Index;\r
325 UINTN Timeout;\r
1c619535 326\r
e237e7ae 327 *CmdStatus = USB_BOT_COMMAND_ERROR;\r
328 Status = EFI_DEVICE_ERROR;\r
329 Endpoint = UsbBot->BulkInEndpoint->EndpointAddress;\r
330 UsbIo = UsbBot->UsbIo;\r
41e8ff27 331 Timeout = USB_BOT_RECV_CSW_TIMEOUT / USB_MASS_1_MILLISECOND;\r
e237e7ae 332\r
41e8ff27 333 for (Index = 0; Index < USB_BOT_RECV_CSW_RETRY; Index++) {\r
e237e7ae 334 //\r
d80ed2a7 335 // Attemp to the read Command Status Wrapper from bulk in endpoint\r
e237e7ae 336 //\r
337 ZeroMem (&Csw, sizeof (USB_BOT_CSW));\r
338 Result = 0;\r
339 Len = sizeof (USB_BOT_CSW);\r
340 Status = UsbIo->UsbBulkTransfer (\r
341 UsbIo,\r
342 Endpoint,\r
343 &Csw,\r
344 &Len,\r
345 Timeout,\r
346 &Result\r
347 );\r
348 if (EFI_ERROR(Status)) {\r
e237e7ae 349 if (USB_IS_ERROR (Result, EFI_USB_ERR_STALL)) {\r
e237e7ae 350 UsbClearEndpointStall (UsbIo, Endpoint);\r
351 }\r
352 continue;\r
353 }\r
354\r
355 if (Csw.Signature != USB_BOT_CSW_SIGNATURE) {\r
356 //\r
d80ed2a7 357 // CSW is invalid, so perform reset recovery\r
e237e7ae 358 //\r
e237e7ae 359 Status = UsbBotResetDevice (UsbBot, FALSE);\r
360 } else if (Csw.CmdStatus == USB_BOT_COMMAND_ERROR) {\r
361 //\r
d80ed2a7 362 // Respond phase error also needs reset recovery\r
e237e7ae 363 //\r
e237e7ae 364 Status = UsbBotResetDevice (UsbBot, FALSE);\r
365 } else {\r
e237e7ae 366 *CmdStatus = Csw.CmdStatus;\r
367 break;\r
368 }\r
369 }\r
370 //\r
d80ed2a7 371 //The tag is increased even if there is an error.\r
e237e7ae 372 //\r
373 UsbBot->CbwTag++;\r
374\r
375 return Status;\r
376}\r
377\r
378\r
379/**\r
d80ed2a7 380 Call the USB Mass Storage Class BOT protocol to issue\r
cc5166ff 381 the command/data/status circle to execute the commands.\r
e237e7ae 382\r
383 @param Context The context of the BOT protocol, that is,\r
384 USB_BOT_PROTOCOL\r
385 @param Cmd The high level command\r
386 @param CmdLen The command length\r
387 @param DataDir The direction of the data transfer\r
388 @param Data The buffer to hold data\r
389 @param DataLen The length of the data\r
c7e39923 390 @param Lun The number of logic unit\r
e237e7ae 391 @param Timeout The time to wait command\r
392 @param CmdStatus The result of high level command execution\r
393\r
d80ed2a7 394 @retval EFI_SUCCESS The command is executed successfully.\r
ed356b9e 395 @retval Other Failed to execute command\r
e237e7ae 396\r
397**/\r
e237e7ae 398EFI_STATUS\r
399UsbBotExecCommand (\r
400 IN VOID *Context,\r
401 IN VOID *Cmd,\r
402 IN UINT8 CmdLen,\r
403 IN EFI_USB_DATA_DIRECTION DataDir,\r
404 IN VOID *Data,\r
405 IN UINT32 DataLen,\r
c7e39923 406 IN UINT8 Lun,\r
e237e7ae 407 IN UINT32 Timeout,\r
408 OUT UINT32 *CmdStatus\r
409 )\r
410{\r
411 USB_BOT_PROTOCOL *UsbBot;\r
412 EFI_STATUS Status;\r
413 UINTN TransLen;\r
414 UINT8 Result;\r
415\r
416 *CmdStatus = USB_MASS_CMD_FAIL;\r
417 UsbBot = (USB_BOT_PROTOCOL *) Context;\r
418\r
419 //\r
420 // Send the command to the device. Return immediately if device\r
421 // rejects the command.\r
422 //\r
c7e39923 423 Status = UsbBotSendCommand (UsbBot, Cmd, CmdLen, DataDir, DataLen, Lun);\r
e237e7ae 424 if (EFI_ERROR (Status)) {\r
1c619535 425 DEBUG ((EFI_D_ERROR, "UsbBotExecCommand: UsbBotSendCommand (%r)\n", Status));\r
e237e7ae 426 return Status;\r
427 }\r
428\r
429 //\r
430 // Transfer the data. Don't return immediately even data transfer\r
431 // failed. The host should attempt to receive the CSW no matter\r
d80ed2a7 432 // whether it succeeds or fails.\r
e237e7ae 433 //\r
434 TransLen = (UINTN) DataLen;\r
b17d5507 435 UsbBotDataTransfer (UsbBot, DataDir, Data, &TransLen, Timeout);\r
e237e7ae 436\r
437 //\r
438 // Get the status, if that succeeds, interpret the result\r
439 //\r
440 Status = UsbBotGetStatus (UsbBot, DataLen, &Result);\r
441 if (EFI_ERROR (Status)) {\r
1c619535 442 DEBUG ((EFI_D_ERROR, "UsbBotExecCommand: UsbBotGetStatus (%r)\n", Status));\r
e237e7ae 443 return Status;\r
444 }\r
445\r
446 if (Result == 0) {\r
447 *CmdStatus = USB_MASS_CMD_SUCCESS;\r
448 }\r
449\r
450 return EFI_SUCCESS;\r
451}\r
452\r
453\r
454/**\r
d80ed2a7 455 Reset the USB mass storage device by BOT protocol.\r
e237e7ae 456\r
457 @param Context The context of the BOT protocol, that is,\r
cc5166ff 458 USB_BOT_PROTOCOL.\r
d80ed2a7 459 @param ExtendedVerification If FALSE, just issue Bulk-Only Mass Storage Reset request.\r
460 If TRUE, additionally reset parent hub port.\r
e237e7ae 461\r
cc5166ff 462 @retval EFI_SUCCESS The device is reset.\r
463 @retval Others Failed to reset the device..\r
e237e7ae 464\r
465**/\r
e237e7ae 466EFI_STATUS\r
467UsbBotResetDevice (\r
468 IN VOID *Context,\r
d80ed2a7 469 IN BOOLEAN ExtendedVerification\r
e237e7ae 470 )\r
471{\r
472 USB_BOT_PROTOCOL *UsbBot;\r
473 EFI_USB_DEVICE_REQUEST Request;\r
474 EFI_STATUS Status;\r
475 UINT32 Result;\r
476 UINT32 Timeout;\r
477\r
478 UsbBot = (USB_BOT_PROTOCOL *) Context;\r
479\r
480 if (ExtendedVerification) {\r
481 //\r
482 // If we need to do strictly reset, reset its parent hub port\r
483 //\r
484 Status = UsbBot->UsbIo->UsbPortReset (UsbBot->UsbIo);\r
485 if (EFI_ERROR (Status)) {\r
c6e797ae 486 return EFI_DEVICE_ERROR;\r
e237e7ae 487 }\r
488 }\r
489\r
490 //\r
d80ed2a7 491 // Issue a class specific Bulk-Only Mass Storage Reset request,\r
492 // according to section 3.1 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.\r
e237e7ae 493 //\r
d80ed2a7 494 Request.RequestType = 0x21;\r
e237e7ae 495 Request.Request = USB_BOT_RESET_REQUEST;\r
496 Request.Value = 0;\r
497 Request.Index = UsbBot->Interface.InterfaceNumber;\r
498 Request.Length = 0;\r
41e8ff27 499 Timeout = USB_BOT_RESET_DEVICE_TIMEOUT / USB_MASS_1_MILLISECOND;\r
e237e7ae 500\r
501 Status = UsbBot->UsbIo->UsbControlTransfer (\r
502 UsbBot->UsbIo,\r
503 &Request,\r
504 EfiUsbNoData,\r
505 Timeout,\r
506 NULL,\r
507 0,\r
508 &Result\r
509 );\r
510\r
511 if (EFI_ERROR (Status)) {\r
c6e797ae 512 return EFI_DEVICE_ERROR;\r
e237e7ae 513 }\r
514\r
515 //\r
516 // The device shall NAK the host's request until the reset is\r
517 // complete. We can use this to sync the device and host. For\r
d80ed2a7 518 // now just stall 100ms to wait for the device.\r
e237e7ae 519 //\r
41e8ff27 520 gBS->Stall (USB_BOT_RESET_DEVICE_STALL);\r
e237e7ae 521\r
522 //\r
523 // Clear the Bulk-In and Bulk-Out stall condition.\r
524 //\r
525 UsbClearEndpointStall (UsbBot->UsbIo, UsbBot->BulkInEndpoint->EndpointAddress);\r
526 UsbClearEndpointStall (UsbBot->UsbIo, UsbBot->BulkOutEndpoint->EndpointAddress);\r
d80ed2a7 527\r
e237e7ae 528 return Status;\r
529}\r
530\r
c7e39923 531\r
cc5166ff 532/**\r
d80ed2a7 533 Get the max LUN (Logical Unit Number) of USB mass storage device.\r
c7e39923 534\r
cc5166ff 535 @param Context The context of the BOT protocol, that is, USB_BOT_PROTOCOL\r
d80ed2a7 536 @param MaxLun Return pointer to the max number of LUN. (e.g. MaxLun=1 means LUN0 and\r
537 LUN1 in all.)\r
c7e39923 538\r
d80ed2a7 539 @retval EFI_SUCCESS Max LUN is got successfully.\r
540 @retval Others Fail to execute this request.\r
c7e39923 541\r
cc5166ff 542**/\r
c7e39923 543EFI_STATUS\r
544UsbBotGetMaxLun (\r
545 IN VOID *Context,\r
d80ed2a7 546 OUT UINT8 *MaxLun\r
c7e39923 547 )\r
c7e39923 548{\r
549 USB_BOT_PROTOCOL *UsbBot;\r
550 EFI_USB_DEVICE_REQUEST Request;\r
551 EFI_STATUS Status;\r
552 UINT32 Result;\r
553 UINT32 Timeout;\r
554\r
8d92f819
MK
555 if (Context == NULL || MaxLun == NULL) {\r
556 return EFI_INVALID_PARAMETER;\r
557 }\r
558\r
c7e39923 559 UsbBot = (USB_BOT_PROTOCOL *) Context;\r
560\r
561 //\r
562 // Issue a class specific Bulk-Only Mass Storage get max lun reqest.\r
d80ed2a7 563 // according to section 3.2 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.\r
c7e39923 564 //\r
d80ed2a7 565 Request.RequestType = 0xA1;\r
c7e39923 566 Request.Request = USB_BOT_GETLUN_REQUEST;\r
567 Request.Value = 0;\r
568 Request.Index = UsbBot->Interface.InterfaceNumber;\r
569 Request.Length = 1;\r
570 Timeout = USB_BOT_RESET_DEVICE_TIMEOUT / USB_MASS_1_MILLISECOND;\r
571\r
572 Status = UsbBot->UsbIo->UsbControlTransfer (\r
573 UsbBot->UsbIo,\r
574 &Request,\r
575 EfiUsbDataIn,\r
576 Timeout,\r
d80ed2a7 577 (VOID *) MaxLun,\r
c7e39923 578 1,\r
579 &Result\r
580 );\r
8d92f819
MK
581 if (EFI_ERROR (Status) || *MaxLun > USB_BOT_MAX_LUN) {\r
582 //\r
583 // If the Get LUN request returns an error or the MaxLun is larger than\r
584 // the maximum LUN value (0x0f) supported by the USB Mass Storage Class\r
585 // Bulk-Only Transport Spec, then set MaxLun to 0.\r
586 //\r
587 // This improves compatibility with USB FLASH drives that have a single LUN\r
588 // and either do not return a max LUN value or return an invalid maximum LUN\r
589 // value.\r
590 //\r
591 *MaxLun = 0;\r
592 }\r
c7e39923 593\r
8d92f819 594 return EFI_SUCCESS;\r
c7e39923 595}\r
e237e7ae 596\r
597/**\r
cc5166ff 598 Clean up the resource used by this BOT protocol.\r
e237e7ae 599\r
d80ed2a7 600 @param Context The context of the BOT protocol, that is, USB_BOT_PROTOCOL.\r
e237e7ae 601\r
d80ed2a7 602 @retval EFI_SUCCESS The resource is cleaned up.\r
e237e7ae 603\r
604**/\r
e237e7ae 605EFI_STATUS\r
d80ed2a7 606UsbBotCleanUp (\r
e237e7ae 607 IN VOID *Context\r
608 )\r
609{\r
c92e277d 610 FreePool (Context);\r
e237e7ae 611 return EFI_SUCCESS;\r
612}\r
613\r