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