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