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